Skip to content

Commit a39f95c

Browse files
authored
fix: mimic native devtools elements navigation (#141)
1 parent ac82edf commit a39f95c

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

src/routes/+layout.svelte

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
} else if ($root.length) {
3232
selected.set($root[0]);
3333
}
34+
35+
function interactive({ type }: (typeof $root)[number]) {
36+
return $visibility[type] && type !== 'text' && type !== 'anchor';
37+
}
3438
</script>
3539

3640
<svelte:window
@@ -49,25 +53,22 @@
4953
$selected.expanded = false;
5054
$selected.invalidate();
5155
} else if (key === 'ArrowUp') {
52-
const nodes = $selected.parent === undefined ? $root : $selected.parent.children;
53-
const siblings = nodes.filter(
54-
(o) => $visibility[o.type] && o.type !== 'text' && o.type !== 'anchor',
55-
);
56-
const index = siblings.findIndex((o) => o.id === $selected?.id);
57-
$selected = index > 0 ? siblings[index - 1] : $selected.parent ?? $selected;
56+
let nodes = ($selected.parent?.children || $root).filter(interactive);
57+
let sibling = nodes[nodes.findIndex((o) => o.id === $selected?.id) - 1];
58+
while (sibling?.expanded) {
59+
nodes = sibling.children.filter(interactive);
60+
sibling = nodes[nodes.length - 1];
61+
}
62+
$selected = sibling ?? $selected.parent ?? $selected;
5863
} else if (key === 'ArrowDown') {
59-
const children = $selected.children.filter(
60-
(o) => $visibility[o.type] && o.type !== 'text' && o.type !== 'anchor',
61-
);
64+
const children = $selected.children.filter(interactive);
6265

63-
if ($selected.expanded || children.length === 0) {
66+
if (!$selected.expanded || children.length === 0) {
6467
let next = $selected;
6568
let current = $selected;
6669
do {
6770
const nodes = current.parent ? current.parent.children : $root;
68-
const siblings = nodes.filter(
69-
(o) => $visibility[o.type] && o.type !== 'text' && o.type !== 'anchor',
70-
);
71+
const siblings = nodes.filter(interactive);
7172
const index = siblings.findIndex((o) => o.id === current.id);
7273
next = siblings[index + 1];
7374
current = current.parent;

0 commit comments

Comments
 (0)