|
1 | | -<svelte:options runes={true} /> |
2 | | - |
3 | | -<script> |
4 | | - // do not use TS here so that this component works in non-ts projects too |
| 1 | +<script lang="ts"> |
5 | 2 | import { onMount } from 'svelte'; |
6 | | -
|
7 | 3 | import options from 'virtual:svelte-inspector-options'; |
8 | | - const toggle_combo = options.toggleKeyCombo?.toLowerCase().split('-'); |
9 | | - const escape_keys = options.escapeKeys?.map((k) => k.toLowerCase()); |
10 | | - const nav_keys = Object.values(options.navKeys).map((k) => k?.toLowerCase()); |
11 | | - const open_key = options.openKey?.toLowerCase(); |
12 | | - const open_menu_key = options.openMenuKey?.toLowerCase(); |
| 4 | +
|
| 5 | + // Copied from Svelte's internal types |
| 6 | + interface DevStackEntry { |
| 7 | + file: string; |
| 8 | + type: 'component' | 'if' | 'each' | 'await' | 'key' | 'render'; |
| 9 | + line: number; |
| 10 | + column: number; |
| 11 | + parent: DevStackEntry | null; |
| 12 | + componentTag?: string; |
| 13 | + } |
| 14 | +
|
| 15 | + interface Metadata { |
| 16 | + loc: Loc; |
| 17 | + parent?: DevStackEntry; |
| 18 | + } |
| 19 | +
|
| 20 | + interface Loc { |
| 21 | + file: string; |
| 22 | + line: number; |
| 23 | + column: number; |
| 24 | + } |
| 25 | +
|
| 26 | + interface ElementWithMetadata extends Element { |
| 27 | + __svelte_meta: Metadata; |
| 28 | + } |
| 29 | +
|
| 30 | + const toggle_combo = options.toggleKeyCombo.toLowerCase().split('-'); |
| 31 | + const escape_keys = options.escapeKeys.map((k) => k.toLowerCase()); |
| 32 | + const nav_keys = Object.values(options.navKeys).map((k) => k.toLowerCase()); |
| 33 | + const open_key = options.openKey.toLowerCase(); |
| 34 | + const open_menu_key = options.openMenuKey.toLowerCase(); |
13 | 35 |
|
14 | 36 | let enabled = $state(false); |
15 | 37 | let has_opened = $state(false); |
|
27 | 49 | )}`; |
28 | 50 |
|
29 | 51 | // overlay positioning |
30 | | - let x = $state(), |
31 | | - y = $state(), |
32 | | - w = $state(), |
33 | | - h = $state(); |
| 52 | + let x = $state(0), |
| 53 | + y = $state(0), |
| 54 | + w = $state(0), |
| 55 | + h = $state(0); |
34 | 56 |
|
35 | | - let active_el = $state(); |
| 57 | + let active_el = $state<ElementWithMetadata | undefined>(); |
36 | 58 |
|
37 | | - let menu = $state(null); |
| 59 | + let menu = $state<{ x: number; y: number; stack: Array<Loc & { tag: string }> } | null>(null); |
38 | 60 |
|
39 | | - let hold_start_ts = $state(); |
| 61 | + let hold_start_ts = $state<number | null>(null); |
40 | 62 |
|
41 | 63 | let show_toggle = $derived( |
42 | 64 | options.showToggleButton === 'always' || (options.showToggleButton === 'active' && enabled) |
43 | 65 | ); |
44 | 66 |
|
45 | | - function mousemove(e) { |
| 67 | + function mousemove(e: MouseEvent) { |
46 | 68 | if (menu) return; |
47 | 69 | x = e.x; |
48 | 70 | y = e.y; |
49 | 71 | } |
50 | 72 |
|
51 | | - function find_selectable_parent(el, include_self = false) { |
| 73 | + function find_selectable_parent(el: Element | undefined, include_self = false) { |
| 74 | + let candidate: Element | undefined = el; |
| 75 | +
|
52 | 76 | if (!include_self) { |
53 | | - el = el.parentNode; |
| 77 | + candidate = (candidate?.parentNode as Element) ?? undefined; |
54 | 78 | } |
55 | | - while (el) { |
56 | | - if (is_selectable(el)) { |
57 | | - return el; |
| 79 | +
|
| 80 | + while (candidate) { |
| 81 | + if (is_selectable(candidate)) { |
| 82 | + return candidate; |
58 | 83 | } |
59 | | - el = el.parentNode; |
| 84 | +
|
| 85 | + candidate = candidate.parentNode as Element; |
60 | 86 | } |
61 | 87 | } |
62 | 88 |
|
63 | | - function find_selectable_child(el) { |
| 89 | + function find_selectable_child(el: Element) { |
64 | 90 | return [...el.querySelectorAll('*')].find(is_selectable); |
65 | 91 | } |
66 | 92 |
|
67 | | - function find_selectable_sibling(el, prev = false) { |
| 93 | + function find_selectable_sibling(el: Element, prev = false) { |
| 94 | + let candidate: Element | null = el; |
68 | 95 | do { |
69 | | - el = prev ? el.previousElementSibling : el.nextElementSibling; |
70 | | - if (is_selectable(el)) { |
71 | | - return el; |
| 96 | + candidate = prev ? candidate.previousElementSibling : candidate.nextElementSibling; |
| 97 | + if (is_selectable(candidate)) { |
| 98 | + return candidate; |
72 | 99 | } |
73 | | - } while (el); |
| 100 | + } while (candidate); |
74 | 101 | } |
75 | 102 |
|
76 | | - function find_selectable_for_nav(key) { |
| 103 | + function find_selectable_for_nav(key: string) { |
77 | 104 | const el = active_el; |
78 | 105 | if (!el) { |
79 | 106 | return find_selectable_child(document?.body); |
|
92 | 119 | } |
93 | 120 | } |
94 | 121 |
|
95 | | - function is_selectable(el) { |
96 | | - const file = el?.__svelte_meta?.loc?.file; |
| 122 | + function is_selectable(el: Element | null): el is ElementWithMetadata { |
| 123 | + if (!el) return false; |
| 124 | + const file = (el as ElementWithMetadata)?.__svelte_meta?.loc?.file; |
97 | 125 | if (!file || file.includes('node_modules/')) { |
98 | 126 | return false; // no file or 3rd party |
99 | 127 | } |
|
104 | 132 | return true; |
105 | 133 | } |
106 | 134 |
|
107 | | - function mouseover({ target }) { |
| 135 | + function mouseover(e: MouseEvent) { |
108 | 136 | if (menu) return; |
109 | | - const el = find_selectable_parent(target, true); |
| 137 | + const el = find_selectable_parent(e.target as ElementWithMetadata, true); |
110 | 138 | activate(el, false); |
111 | 139 | } |
112 | 140 |
|
113 | | - function activate(el, set_bubble_pos = true) { |
| 141 | + function activate(el: ElementWithMetadata | undefined, set_bubble_pos = true) { |
114 | 142 | if (options.customStyles && el !== active_el) { |
115 | 143 | if (active_el) { |
116 | 144 | active_el.classList.remove('svelte-inspector-active-target'); |
|
122 | 150 |
|
123 | 151 | active_el = el; |
124 | 152 | if (set_bubble_pos) { |
125 | | - const pos = el.getBoundingClientRect(); |
| 153 | + const pos = el!.getBoundingClientRect(); |
126 | 154 | x = Math.ceil(pos.left); |
127 | 155 | y = Math.ceil(pos.bottom - 20); |
128 | 156 | } |
129 | 157 | } |
130 | 158 |
|
131 | | - function open_editor(e) { |
| 159 | + function open_editor(e: Event) { |
132 | 160 | if (menu) return; |
133 | 161 |
|
134 | 162 | if (active_el) { |
|
137 | 165 | } |
138 | 166 | } |
139 | 167 |
|
140 | | - function get_stack(target) { |
141 | | - const el = find_selectable_parent(target, true); |
142 | | - const meta = el?.__svelte_meta; |
| 168 | + function get_stack(target: ElementWithMetadata) { |
| 169 | + const el = find_selectable_parent(target, true)!; |
| 170 | + const meta = el.__svelte_meta; |
143 | 171 |
|
144 | 172 | const stack = [ |
145 | 173 | { |
|
150 | 178 | } |
151 | 179 | ]; |
152 | 180 |
|
153 | | - let entry = meta; |
154 | | - while ((entry = entry.parent)) { |
| 181 | + let entry: Metadata | DevStackEntry | undefined = meta; |
| 182 | +
|
| 183 | + while ((entry = entry.parent ?? undefined)) { |
155 | 184 | if (entry.type !== 'component') continue; |
156 | 185 | if (/(^|\/)(node_modules|\.svelte-kit)\//.test(entry.file)) continue; |
157 | 186 |
|
158 | 187 | stack.push({ |
159 | 188 | file: entry.file, |
160 | 189 | line: entry.line, |
161 | 190 | column: entry.column, |
162 | | - tag: entry.componentTag |
| 191 | + tag: entry.componentTag! |
163 | 192 | }); |
164 | 193 | } |
165 | 194 |
|
166 | 195 | return stack; |
167 | 196 | } |
168 | 197 |
|
169 | | - function on_contextmenu(e) { |
| 198 | + function on_contextmenu(e: Event) { |
170 | 199 | e.preventDefault(); |
171 | 200 |
|
172 | | - active_el ??= find_selectable_parent(e.target); |
| 201 | + active_el ??= find_selectable_parent(e.target as Element); |
173 | 202 | if (!active_el) return; |
174 | 203 |
|
175 | 204 | stop(e); |
176 | | - menu = { x: e.clientX, y: e.clientY, stack: get_stack(active_el) }; |
| 205 | + menu = { x, y, stack: get_stack(active_el) }; |
177 | 206 | } |
178 | 207 |
|
179 | | - function send(loc) { |
| 208 | + function send(loc: Loc) { |
180 | 209 | fetch( |
181 | 210 | `${options.__internal.base}/__open-in-editor?file=${encodeURIComponent( |
182 | 211 | `${loc.file}:${loc.line}:${loc.column + 1}` |
|
189 | 218 | } |
190 | 219 | } |
191 | 220 |
|
192 | | - function is_active(key, e) { |
| 221 | + function is_active(key: string, e: KeyboardEvent) { |
193 | 222 | switch (key) { |
194 | 223 | case 'shift': |
195 | 224 | case 'control': |
|
201 | 230 | } |
202 | 231 | } |
203 | 232 |
|
204 | | - function is_combo(e) { |
| 233 | + function is_combo(e: KeyboardEvent) { |
205 | 234 | return toggle_combo?.every((k) => is_active(k, e)); |
206 | 235 | } |
207 | 236 |
|
208 | | - function is_escape(e) { |
| 237 | + function is_escape(e: KeyboardEvent) { |
209 | 238 | return escape_keys?.some((k) => is_active(k, e)); |
210 | 239 | } |
211 | 240 |
|
212 | | - function is_toggle(e) { |
| 241 | + function is_toggle(e: KeyboardEvent) { |
213 | 242 | return toggle_combo?.some((k) => is_active(k, e)); |
214 | 243 | } |
215 | 244 |
|
216 | | - function is_nav(e) { |
| 245 | + function is_nav(e: KeyboardEvent) { |
217 | 246 | return nav_keys?.some((k) => is_active(k, e)); |
218 | 247 | } |
219 | 248 |
|
220 | | - function is_open(e) { |
| 249 | + function is_open(e: KeyboardEvent) { |
221 | 250 | return open_key && is_active(open_key, e); |
222 | 251 | } |
223 | 252 |
|
224 | | - function is_open_menu(e) { |
| 253 | + function is_open_menu(e: KeyboardEvent) { |
225 | 254 | return open_menu_key && is_active(open_menu_key, e); |
226 | 255 | } |
227 | 256 |
|
228 | 257 | function is_holding() { |
229 | 258 | return hold_start_ts && Date.now() - hold_start_ts > 250; |
230 | 259 | } |
231 | 260 |
|
232 | | - function stop(e) { |
| 261 | + function stop(e: Event) { |
233 | 262 | e.preventDefault(); |
234 | 263 | e.stopPropagation(); |
235 | 264 | e.stopImmediatePropagation(); |
236 | 265 | } |
237 | 266 |
|
238 | | - function keydown(e) { |
| 267 | + function keydown(e: KeyboardEvent) { |
239 | 268 | if (e.repeat || e.key == null || (!enabled && !is_toggle(e))) { |
240 | 269 | return; |
241 | 270 | } |
|
265 | 294 | } |
266 | 295 | } |
267 | 296 |
|
268 | | - function keyup(e) { |
| 297 | + function keyup(e: KeyboardEvent) { |
269 | 298 | if (e.repeat || e.key == null || !enabled) { |
270 | 299 | return; |
271 | 300 | } |
|
286 | 315 | } |
287 | 316 | } |
288 | 317 |
|
289 | | - function listeners(body, enabled) { |
| 318 | + function listeners(body: HTMLBodyElement, enabled: boolean) { |
290 | 319 | const l = enabled ? body.addEventListener : body.removeEventListener; |
291 | 320 | l('mousemove', mousemove); |
292 | 321 | l('mouseover', mouseover); |
|
296 | 325 |
|
297 | 326 | function enable() { |
298 | 327 | enabled = true; |
299 | | - const b = document.body; |
| 328 | + const b = document.body as HTMLBodyElement; |
300 | 329 | if (options.customStyles) { |
301 | 330 | b.classList.add('svelte-inspector-enabled'); |
302 | 331 | } |
|
308 | 337 | const hov = innermost_hover_el(); |
309 | 338 | let el = find_selectable_parent(hov, true); |
310 | 339 | if (!el) { |
311 | | - const act = document.activeElement; |
| 340 | + const act = document.activeElement ?? undefined; |
312 | 341 | el = find_selectable_parent(act, true); |
313 | 342 | } |
314 | 343 | if (!el) { |
|
334 | 363 | has_opened = false; |
335 | 364 | hold_start_ts = null; |
336 | 365 | menu = null; |
337 | | - const b = document.body; |
| 366 | + const b = document.body as HTMLBodyElement; |
338 | 367 | listeners(b, enabled); |
339 | 368 | if (options.customStyles) { |
340 | 369 | b.classList.remove('svelte-inspector-enabled'); |
341 | 370 | active_el?.classList.remove('svelte-inspector-active-target'); |
342 | 371 | } |
343 | | - active_el = null; |
| 372 | + active_el = undefined; |
344 | 373 | } |
345 | 374 |
|
346 | 375 | function visibilityChange() { |
|
446 | 475 | </li> |
447 | 476 | {/each} |
448 | 477 | </ul> |
449 | | - {:else} |
| 478 | + {:else if active_el} |
450 | 479 | {@const loc = active_el.__svelte_meta.loc} |
451 | 480 |
|
452 | 481 | <div class="svelte-inspector-grid"> |
|
0 commit comments