|
31 | 31 | } else if ($root.length) {
|
32 | 32 | selected.set($root[0]);
|
33 | 33 | }
|
| 34 | +
|
| 35 | + function interactive({ type }: (typeof $root)[number]) { |
| 36 | + return $visibility[type] && type !== 'text' && type !== 'anchor'; |
| 37 | + } |
34 | 38 | </script>
|
35 | 39 |
|
36 | 40 | <svelte:window
|
|
49 | 53 | $selected.expanded = false;
|
50 | 54 | $selected.invalidate();
|
51 | 55 | } 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; |
58 | 63 | } 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); |
62 | 65 |
|
63 |
| - if ($selected.expanded || children.length === 0) { |
| 66 | + if (!$selected.expanded || children.length === 0) { |
64 | 67 | let next = $selected;
|
65 | 68 | let current = $selected;
|
66 | 69 | do {
|
67 | 70 | 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); |
71 | 72 | const index = siblings.findIndex((o) => o.id === current.id);
|
72 | 73 | next = siblings[index + 1];
|
73 | 74 | current = current.parent;
|
|
0 commit comments