|
| 1 | +import { highlight } from './highlight.js'; |
| 2 | +import { send } from './runtime.js'; |
| 3 | +import { index as v4 } from './svelte-4.js'; |
| 4 | +import { serialize } from './utils.js'; |
| 5 | + |
| 6 | +// @ts-ignore - for the app to call with `eval` |
| 7 | +window['#SvelteDevTools'] = { |
| 8 | + /** |
| 9 | + * @param {string} id |
| 10 | + * @param {string} key |
| 11 | + * @param {any} value |
| 12 | + */ |
| 13 | + inject(id, key, value) { |
| 14 | + const { detail: component } = v4.map.get(id) || {}; |
| 15 | + component && component.$inject_state({ [key]: value }); |
| 16 | + }, |
| 17 | +}; |
| 18 | + |
| 19 | +const previous = { |
| 20 | + /** @type {HTMLElement | null} */ |
| 21 | + target: null, |
| 22 | + style: { |
| 23 | + cursor: '', |
| 24 | + background: '', |
| 25 | + outline: '', |
| 26 | + }, |
| 27 | + |
| 28 | + clear() { |
| 29 | + if (this.target) { |
| 30 | + for (const key in this.style) { |
| 31 | + // @ts-expect-error - trust me TS |
| 32 | + this.target.style[key] = this.style[key]; |
| 33 | + } |
| 34 | + } |
| 35 | + this.target = null; |
| 36 | + }, |
| 37 | +}; |
| 38 | + |
| 39 | +const inspect = { |
| 40 | + /** @param {MouseEvent} event */ |
| 41 | + handle({ target }) { |
| 42 | + const same = previous.target && previous.target === target; |
| 43 | + const html = target instanceof HTMLElement; |
| 44 | + if (same || !html) return; |
| 45 | + |
| 46 | + if (previous.target) previous.clear(); |
| 47 | + previous.target = target; |
| 48 | + previous.style = { |
| 49 | + cursor: target.style.cursor, |
| 50 | + background: target.style.background, |
| 51 | + outline: target.style.outline, |
| 52 | + }; |
| 53 | + target.style.cursor = 'pointer'; |
| 54 | + target.style.background = 'rgba(0, 136, 204, 0.2)'; |
| 55 | + target.style.outline = '1px dashed rgb(0, 136, 204)'; |
| 56 | + }, |
| 57 | + /** @param {MouseEvent} event */ |
| 58 | + click(event) { |
| 59 | + event.preventDefault(); |
| 60 | + document.removeEventListener('mousemove', inspect.handle, true); |
| 61 | + const node = v4.map.get(/** @type {Node} */ (event.target)); |
| 62 | + if (node) send('bridge::ext/inspect', { node: serialize(node) }); |
| 63 | + previous.clear(); |
| 64 | + }, |
| 65 | +}; |
| 66 | + |
| 67 | +window.addEventListener('message', ({ data, source }) => { |
| 68 | + // only accept messages from our application or script |
| 69 | + if (source !== window || data?.source !== 'svelte-devtools') return; |
| 70 | + |
| 71 | + if (data.type === 'bridge::ext/select') { |
| 72 | + const node = v4.map.get(data.payload); |
| 73 | + // @ts-expect-error - saved for `devtools.inspect()` |
| 74 | + if (node) window.$n = node.detail; |
| 75 | + } else if (data.type === 'bridge::ext/highlight') { |
| 76 | + const node = v4.map.get(data.payload); |
| 77 | + return highlight(node); |
| 78 | + } else if (data.type === 'bridge::ext/inspect') { |
| 79 | + switch (data.payload) { |
| 80 | + case 'start': { |
| 81 | + document.addEventListener('mousemove', inspect.handle, true); |
| 82 | + document.addEventListener('click', inspect.click, { |
| 83 | + capture: true, |
| 84 | + once: true, |
| 85 | + }); |
| 86 | + break; |
| 87 | + } |
| 88 | + default: { |
| 89 | + document.removeEventListener('mousemove', inspect.handle, true); |
| 90 | + document.removeEventListener('click', inspect.click, true); |
| 91 | + previous.clear(); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +}); |
0 commit comments