|
| 1 | +import { ComponentInternalInstance } from 'vue' |
| 2 | + |
| 3 | +export let ctx: CanvasRenderingContext2D | null = null |
| 4 | +const updateCounters = new WeakMap<HTMLElement, number>() |
| 5 | +const elements = new Set<HTMLElement>() |
| 6 | + |
| 7 | +export function initCanvas() { |
| 8 | + const canvas = document.createElement('canvas') |
| 9 | + canvas.id = 'vue-change-marker-canvas' |
| 10 | + canvas.style.position = 'fixed' |
| 11 | + canvas.style.top = '0' |
| 12 | + canvas.style.left = '0' |
| 13 | + canvas.style.width = '100vw' |
| 14 | + canvas.style.height = '100vh' |
| 15 | + canvas.style.pointerEvents = 'none' |
| 16 | + canvas.style.zIndex = '2147483646' |
| 17 | + canvas.setAttribute('aria-hidden', 'true') |
| 18 | + |
| 19 | + const canvasCtx = canvas.getContext('2d')! |
| 20 | + ctx = canvasCtx |
| 21 | + |
| 22 | + document.documentElement.appendChild(canvas) |
| 23 | + |
| 24 | + const dpi = window.devicePixelRatio || 1 |
| 25 | + canvasCtx.canvas.width = dpi * window.innerWidth |
| 26 | + canvasCtx.canvas.height = dpi * window.innerHeight |
| 27 | + canvas.style.width = `${window.innerWidth}px` |
| 28 | + canvas.style.height = `${window.innerHeight}px` |
| 29 | + canvasCtx.resetTransform() |
| 30 | + canvasCtx.scale(dpi, dpi) |
| 31 | +} |
| 32 | + |
| 33 | +export function clearCanvas(ctx: CanvasRenderingContext2D) { |
| 34 | + ctx.save() |
| 35 | + ctx.setTransform(1, 0, 0, 1, 0, 0) |
| 36 | + const width = ctx.canvas.width |
| 37 | + const height = ctx.canvas.height |
| 38 | + ctx.clearRect(0, 0, width, height) |
| 39 | + ctx.restore() |
| 40 | +} |
| 41 | +export function resetCounters() { |
| 42 | + elements.forEach((el) => { |
| 43 | + updateCounters.set(el, 0) |
| 44 | + }) |
| 45 | +} |
| 46 | + |
| 47 | +export function highlight( |
| 48 | + ctx: CanvasRenderingContext2D, |
| 49 | + el: HTMLElement, |
| 50 | + updatedInstance: ComponentInternalInstance, |
| 51 | +) { |
| 52 | + clearCanvas(ctx!) |
| 53 | + |
| 54 | + let updateCounter = updateCounters.get(el) || 0 |
| 55 | + updateCounter++ |
| 56 | + |
| 57 | + updateCounters.set(el, updateCounter) |
| 58 | + elements.add(el) |
| 59 | + |
| 60 | + const opacity = Math.min(0.9, updateCounter * 0.2) |
| 61 | + const rect = el.getBoundingClientRect() |
| 62 | + |
| 63 | + const componentName = |
| 64 | + updatedInstance.type.__name || updatedInstance.type.__file || '' |
| 65 | + |
| 66 | + ctx.save() |
| 67 | + ctx.fillStyle = `rgba(255, 0, 0, ${opacity})` |
| 68 | + ctx.fillRect(rect.x, rect.y - 40, Math.max(250, rect.width), 40) |
| 69 | + ctx.fillStyle = `white` |
| 70 | + ctx.font = '14px Arial' |
| 71 | + ctx.fillText('updated component: ' + componentName, rect.x, rect.y - 25) |
| 72 | + ctx.fillText('updated times: ' + updateCounter, rect.x, rect.y - 5) |
| 73 | + ctx.restore() |
| 74 | + |
| 75 | + ctx.save() |
| 76 | + ctx.strokeStyle = `rgba(255, 0, 0, ${opacity})` |
| 77 | + ctx.lineWidth = 1 |
| 78 | + ctx.strokeRect(rect.x, rect.y, rect.width, rect.height) |
| 79 | + ctx.restore() |
| 80 | +} |
| 81 | + |
| 82 | +export const getRect = (domNode: Element): DOMRect | null => { |
| 83 | + const style = window.getComputedStyle(domNode) |
| 84 | + if ( |
| 85 | + style.display === 'none' || |
| 86 | + style.visibility === 'hidden' || |
| 87 | + style.opacity === '0' |
| 88 | + ) { |
| 89 | + return null |
| 90 | + } |
| 91 | + |
| 92 | + const rect = domNode.getBoundingClientRect() |
| 93 | + |
| 94 | + const isVisible = |
| 95 | + rect.bottom > 0 && |
| 96 | + rect.right > 0 && |
| 97 | + rect.top < window.innerHeight && |
| 98 | + rect.left < window.innerWidth |
| 99 | + |
| 100 | + if (!isVisible || !rect.width || !rect.height) { |
| 101 | + return null |
| 102 | + } |
| 103 | + |
| 104 | + return rect |
| 105 | +} |
0 commit comments