|
| 1 | +import { ComponentHighLighterOptions, VueAppInstance } from '../../../types' |
| 2 | +import { getComponentBoundingRect } from '../component/state/bounding-rect' |
| 3 | +import { getInstanceName } from '../component/utils' |
| 4 | + |
| 5 | +const CONTAINER_ELEMENT_ID = '__vue-devtools-flash__' |
| 6 | +const REMOVE_DELAY_MS = 1000 |
| 7 | + |
| 8 | +const containerStyles = { |
| 9 | + border: '2px rgba(65, 184, 131, 0.7) solid', |
| 10 | + position: 'fixed', |
| 11 | + zIndex: '2147483645', |
| 12 | + pointerEvents: 'none', |
| 13 | + borderRadius: '3px', |
| 14 | + boxSizing: 'border-box', |
| 15 | + transition: 'none', |
| 16 | + opacity: '1', |
| 17 | +} |
| 18 | + |
| 19 | +function getStyles(bounds: ComponentHighLighterOptions['bounds']) { |
| 20 | + return { |
| 21 | + left: `${Math.round(bounds.left)}px`, |
| 22 | + top: `${Math.round(bounds.top)}px`, |
| 23 | + width: `${Math.round(bounds.width)}px`, |
| 24 | + height: `${Math.round(bounds.height)}px`, |
| 25 | + } satisfies Partial<CSSStyleDeclaration> |
| 26 | +} |
| 27 | + |
| 28 | +function create(options: ComponentHighLighterOptions & { elementId?: string, style?: Partial<CSSStyleDeclaration> }) { |
| 29 | + const containerEl = document.createElement('div') |
| 30 | + containerEl.id = options?.elementId ?? CONTAINER_ELEMENT_ID |
| 31 | + |
| 32 | + Object.assign(containerEl.style, { |
| 33 | + ...containerStyles, |
| 34 | + ...getStyles(options.bounds), |
| 35 | + ...options.style, |
| 36 | + }) |
| 37 | + |
| 38 | + document.body.appendChild(containerEl) |
| 39 | + |
| 40 | + requestAnimationFrame(() => { |
| 41 | + containerEl.style.transition = 'opacity 1s' |
| 42 | + containerEl.style.opacity = '0' |
| 43 | + }) |
| 44 | + |
| 45 | + clearTimeout((containerEl as any)?._timer); |
| 46 | + (containerEl as any)._timer = setTimeout(() => { |
| 47 | + document.body.removeChild(containerEl) |
| 48 | + }, REMOVE_DELAY_MS) |
| 49 | + |
| 50 | + return containerEl |
| 51 | +} |
| 52 | + |
| 53 | +export function flashComponent(instance: VueAppInstance) { |
| 54 | + const bounds = getComponentBoundingRect(instance) |
| 55 | + |
| 56 | + if (!bounds.width && !bounds.height) |
| 57 | + return |
| 58 | + |
| 59 | + const name = getInstanceName(instance) |
| 60 | + create({ bounds, name }) |
| 61 | +} |
0 commit comments