Skip to content

Commit 8d441cb

Browse files
authored
feat: add drilling and tag display to inspector (#436)
* feat: add drilling and tag display to inspector * feat: add config option for svelte-inspector drill keys and stop key event if drilling occurs
1 parent 92c89d1 commit 8d441cb

File tree

7 files changed

+68
-8
lines changed

7 files changed

+68
-8
lines changed

.changeset/fair-dodos-dance.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': minor
3+
---
4+
5+
svelte-inspector: use arrow up/down to allow selecting parent/child elements that cannot be moused over (see #435)

.changeset/light-readers-leave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': minor
3+
---
4+
5+
svelte-inspector: display active element tag

docs/config.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,14 @@ export default {
305305
*/
306306
toggleKeyCombo?: string;
307307

308+
/**
309+
* define keys to drill from the active element (up selects parent, down selects child).
310+
* @default {up: 'ArrowUp',down: 'ArrowDown'}
311+
*
312+
* This is useful when components wrap another one without providing any hoverable area between them
313+
*/
314+
drillKeys?: { up: string; down: string };
315+
308316
/**
309317
* inspector is automatically disabled when releasing toggleKeyCombo after holding it for a longpress
310318
* @default false

packages/playground/kit-demo-app/svelte.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ const config = {
99
methodOverride: {
1010
allowed: ['PATCH', 'DELETE']
1111
}
12+
},
13+
vitePlugin: {
14+
experimental: {
15+
inspector: true
16+
}
1217
}
1318
};
1419

packages/vite-plugin-svelte/src/ui/inspector/Inspector.svelte

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,30 @@
3737
y = event.y;
3838
}
3939
40-
function findMetaEl(el) {
40+
function find_parent_with_meta(el) {
4141
while (el) {
42-
const file = el.__svelte_meta?.loc?.file;
43-
if (el !== toggle_el && file && !file.includes('node_modules/')) {
42+
if (has_meta(el)) {
4443
return el;
4544
}
4645
el = el.parentNode;
4746
}
4847
}
4948
49+
function find_child_with_meta(el) {
50+
return [...el.querySelectorAll('*')].find(has_meta);
51+
}
52+
53+
function has_meta(el) {
54+
const file = el.__svelte_meta?.loc?.file;
55+
return el !== toggle_el && file && !file.includes('node_modules/');
56+
}
57+
5058
function mouseover(event) {
51-
const el = findMetaEl(event.target);
59+
const el = find_parent_with_meta(event.target);
60+
activate(el);
61+
}
62+
63+
function activate(el) {
5264
if (options.customStyles && el !== active_el) {
5365
if (active_el) {
5466
active_el.classList.remove('svelte-inspector-active-target');
@@ -68,9 +80,7 @@
6880
6981
function click(event) {
7082
if (file_loc) {
71-
event.preventDefault();
72-
event.stopPropagation();
73-
event.stopImmediatePropagation();
83+
stop(event);
7484
fetch(`/__open-in-editor?file=${encodeURIComponent(file_loc)}`);
7585
if (options.holdMode && is_holding()) {
7686
disable();
@@ -98,6 +108,12 @@
98108
return enabled_ts && Date.now() - enabled_ts > 250;
99109
}
100110
111+
function stop(event) {
112+
event.preventDefault();
113+
event.stopPropagation();
114+
event.stopImmediatePropagation();
115+
}
116+
101117
function keydown(event) {
102118
if (event.repeat || event.key == null) {
103119
return;
@@ -108,6 +124,18 @@
108124
if (options.holdMode && enabled) {
109125
enabled_ts = Date.now();
110126
}
127+
} else if (event.key === options.drillKeys.up && active_el) {
128+
const el = find_parent_with_meta(active_el.parentNode);
129+
if (el) {
130+
activate(el);
131+
stop(event);
132+
}
133+
} else if (event.key === options.drillKeys.down && active_el) {
134+
const el = find_child_with_meta(active_el);
135+
if (el) {
136+
activate(el);
137+
stop(event);
138+
}
111139
}
112140
}
113141
@@ -203,7 +231,7 @@
203231
style:top="{y + 30}px"
204232
bind:offsetWidth={w}
205233
>
206-
{file_loc}
234+
<{active_el.tagName.toLowerCase()}> {file_loc}
207235
</div>
208236
{/if}
209237

packages/vite-plugin-svelte/src/ui/inspector/plugin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { idToFile } from './utils';
88

99
const defaultInspectorOptions: InspectorOptions = {
1010
toggleKeyCombo: process.platform === 'win32' ? 'control-shift' : 'meta-shift',
11+
drillKeys: { up: 'ArrowUp', down: 'ArrowDown' },
1112
holdMode: false,
1213
showToggleButton: 'active',
1314
toggleButtonPos: 'top-right',

packages/vite-plugin-svelte/src/utils/options.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,14 @@ export interface InspectorOptions {
643643
*/
644644
toggleKeyCombo?: string;
645645

646+
/**
647+
* define keys to drill from the active element (up selects parent, down selects child).
648+
* @default {up: 'ArrowUp',down: 'ArrowDown'}
649+
*
650+
* This is useful when components wrap another one without providing any hoverable area between them
651+
*/
652+
drillKeys?: { up: string; down: string };
653+
646654
/**
647655
* inspector is automatically disabled when releasing toggleKeyCombo after holding it for a longpress
648656
* @default false

0 commit comments

Comments
 (0)