@vokril/ui-solid 0.19.1 → 0.20.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +38 -2
  3. package/dist/createResizableSidebar-CeksJQ9Q.cjs +1 -0
  4. package/dist/createResizableSidebar-DnHgvr7P.js +472 -0
  5. package/dist/createVirtualWindow-C8_UuF7B.cjs +1 -0
  6. package/dist/createVirtualWindow-CGPw2jm_.js +121 -0
  7. package/dist/index.cjs +1 -1
  8. package/dist/index.js +108 -104
  9. package/dist/layout/createVirtualScroller.d.cts +8 -31
  10. package/dist/layout/createVirtualScroller.d.ts +8 -31
  11. package/dist/layout/createVirtualWindow.d.cts +66 -0
  12. package/dist/layout/createVirtualWindow.d.ts +66 -0
  13. package/dist/layout/index.cjs +1 -1
  14. package/dist/layout/index.d.cts +2 -0
  15. package/dist/layout/index.d.ts +2 -0
  16. package/dist/layout/index.js +7 -6
  17. package/dist/layout/virtualScrollerUtils.d.cts +58 -22
  18. package/dist/layout/virtualScrollerUtils.d.ts +58 -22
  19. package/dist/rowSource-CfGSdY64.js +1236 -0
  20. package/dist/rowSource-D0MM14Bu.cjs +1 -0
  21. package/dist/server/createResizableSidebar-B0NDaq7D.cjs +1 -0
  22. package/dist/server/createResizableSidebar-Dn9UMUf_.js +310 -0
  23. package/dist/server/createVirtualWindow-C8_UuF7B.cjs +1 -0
  24. package/dist/server/createVirtualWindow-CGPw2jm_.js +121 -0
  25. package/dist/server/index.cjs +1 -1
  26. package/dist/server/index.js +110 -106
  27. package/dist/server/layout/index.cjs +1 -1
  28. package/dist/server/layout/index.js +7 -6
  29. package/dist/server/rowSource-BGuQROhJ.js +1122 -0
  30. package/dist/server/rowSource-IX742JOS.cjs +1 -0
  31. package/dist/server/tree/index.cjs +1 -1
  32. package/dist/server/tree/index.js +11 -8
  33. package/dist/solid/layout/createVirtualScroller.js +17 -100
  34. package/dist/solid/layout/createVirtualWindow.js +142 -0
  35. package/dist/solid/layout/index.js +1 -0
  36. package/dist/solid/layout/virtualScrollerUtils.js +31 -32
  37. package/dist/solid/tree/TreeSurface.jsx +20 -13
  38. package/dist/solid/tree/createTreeController.js +110 -62
  39. package/dist/solid/tree/createTreeSurface.js +29 -12
  40. package/dist/solid/tree/index.js +1 -0
  41. package/dist/solid/tree/rowSource.js +93 -0
  42. package/dist/solid/tree/selectAllInScope.js +31 -25
  43. package/dist/solid/tree/stickyScroll.js +12 -10
  44. package/dist/solid/tree/treeAria.js +11 -30
  45. package/dist/solid/tree/treeKeymap.js +2 -24
  46. package/dist/solid/tree/viewportAnchor.js +23 -0
  47. package/dist/style.css +1 -1
  48. package/dist/tree/TreeStickyHeader.d.cts +1 -1
  49. package/dist/tree/TreeStickyHeader.d.ts +1 -1
  50. package/dist/tree/TreeSurface.d.cts +1 -1
  51. package/dist/tree/TreeSurface.d.ts +1 -1
  52. package/dist/tree/createTreeController.d.cts +19 -8
  53. package/dist/tree/createTreeController.d.ts +19 -8
  54. package/dist/tree/createTreeSurface.d.cts +8 -3
  55. package/dist/tree/createTreeSurface.d.ts +8 -3
  56. package/dist/tree/descriptor.d.cts +8 -9
  57. package/dist/tree/descriptor.d.ts +8 -9
  58. package/dist/tree/index.cjs +1 -1
  59. package/dist/tree/index.d.cts +1 -0
  60. package/dist/tree/index.d.ts +1 -0
  61. package/dist/tree/index.js +11 -8
  62. package/dist/tree/rowSource.d.cts +41 -0
  63. package/dist/tree/rowSource.d.ts +41 -0
  64. package/dist/tree/selectAllInScope.d.cts +20 -2
  65. package/dist/tree/selectAllInScope.d.ts +20 -2
  66. package/dist/tree/stickyScroll.d.cts +32 -5
  67. package/dist/tree/stickyScroll.d.ts +32 -5
  68. package/dist/tree/treeAria.d.cts +26 -10
  69. package/dist/tree/treeAria.d.ts +26 -10
  70. package/dist/tree/treeKeymap.d.cts +2 -16
  71. package/dist/tree/treeKeymap.d.ts +2 -16
  72. package/dist/tree/viewportAnchor.d.cts +21 -0
  73. package/dist/tree/viewportAnchor.d.ts +21 -0
  74. package/dist/ui.css +1 -1
  75. package/package.json +1 -1
  76. package/dist/createResizableSidebar-Dau7TIuH.cjs +0 -1
  77. package/dist/createResizableSidebar-gvf9me4K.js +0 -449
  78. package/dist/createVirtualScroller-CL49JE2P.cjs +0 -1
  79. package/dist/createVirtualScroller-vu-l4rVv.js +0 -93
  80. package/dist/descriptor-CaHgh9N_.cjs +0 -1
  81. package/dist/descriptor-eGtcBtpI.js +0 -1106
  82. package/dist/server/createResizableSidebar-BQG5elmd.js +0 -287
  83. package/dist/server/createResizableSidebar-tU8FO2MK.cjs +0 -1
  84. package/dist/server/createVirtualScroller-CL49JE2P.cjs +0 -1
  85. package/dist/server/createVirtualScroller-vu-l4rVv.js +0 -93
  86. package/dist/server/descriptor-CEGvvJZh.cjs +0 -1
  87. package/dist/server/descriptor-GLoKPx6x.js +0 -992
@@ -1,18 +1,61 @@
1
- import { createMemo, createSignal, onCleanup } from 'solid-js';
1
+ import { createEffect, createMemo, createSignal, onCleanup, } from 'solid-js';
2
2
  import { DEFAULT_TREE_CAPABILITIES, } from './types';
3
- import { findParentIndex, findTypeAheadIndex, resolveTreeIntent, } from './treeKeymap';
3
+ import { findTypeAheadIndex, resolveTreeIntent } from './treeKeymap';
4
4
  import { selectAllInScope } from './selectAllInScope';
5
5
  import { isKey } from '../_internal/base/keys';
6
6
  export const TYPE_AHEAD_RESET_MS = 1000;
7
+ function positionAt(rows, index) {
8
+ const ancestors = [];
9
+ for (let parent = rows.parentIndex(index); parent !== -1; parent = rows.parentIndex(parent))
10
+ ancestors.push(rows.at(parent).key);
11
+ return { key: rows.at(index).key, ancestors, index };
12
+ }
13
+ function resolvePosition(rows, position) {
14
+ if (position === null || rows.length === 0)
15
+ return -1;
16
+ if (position.index < rows.length &&
17
+ rows.at(position.index).key === position.key)
18
+ return position.index;
19
+ const own = rows.indexOfKey(position.key);
20
+ if (own >= 0)
21
+ return own;
22
+ for (const key of position.ancestors) {
23
+ const ancestor = rows.indexOfKey(key);
24
+ if (ancestor >= 0)
25
+ return ancestor;
26
+ }
27
+ return Math.min(position.index, rows.length - 1);
28
+ }
7
29
  export function createTreeController(options) {
8
30
  const caps = () => ({
9
31
  ...DEFAULT_TREE_CAPABILITIES,
10
32
  ...options.capabilities,
11
33
  expandAll: false,
12
34
  });
13
- const [cursorIndex, setCursorIndex] = createSignal(-1);
35
+ const [cursor, setCursor] = createSignal(null);
14
36
  const [selectedKeys, setSelectedKeys] = createSignal(new Set());
15
- const [anchorIndex, setAnchorIndex] = createSignal(-1);
37
+ const [anchor, setAnchor] = createSignal(null);
38
+ const cursorIndex = createMemo(() => resolvePosition(options.rows(), cursor()));
39
+ const anchorIndex = () => resolvePosition(options.rows(), anchor());
40
+ const cursorKey = createMemo(() => {
41
+ const index = cursorIndex();
42
+ return index < 0 ? null : options.rows().at(index).key;
43
+ });
44
+ createEffect(() => {
45
+ const position = cursor();
46
+ const index = cursorIndex();
47
+ if (position === null || index < 0)
48
+ return;
49
+ const rows = options.rows();
50
+ const key = rows.at(index).key;
51
+ if (key === position.key) {
52
+ if (index !== position.index)
53
+ setCursor(positionAt(rows, index));
54
+ return;
55
+ }
56
+ if (position.ancestors.includes(key))
57
+ setCursor(positionAt(rows, index));
58
+ });
16
59
  const [typeAheadQuery, setTypeAheadQuery] = createSignal('');
17
60
  let lastTypeAheadAt = 0;
18
61
  let typeAheadResetTimer;
@@ -41,24 +84,39 @@ export function createTreeController(options) {
41
84
  typeAheadResetTimer = undefined;
42
85
  }
43
86
  });
44
- const cursorKey = createMemo(() => options.rows()[cursorIndex()]?.key ?? null);
87
+ const rowAt = (index) => {
88
+ const rows = options.rows();
89
+ return index >= 0 && index < rows.length ? rows.at(index) : undefined;
90
+ };
91
+ const placeCursor = (index, moveAnchor) => {
92
+ const position = positionAt(options.rows(), index);
93
+ setCursor(position);
94
+ if (moveAnchor)
95
+ setAnchor(position);
96
+ options.onScrollToIndex?.(index);
97
+ };
45
98
  const focusIndex = (index) => {
46
99
  const rows = options.rows();
47
100
  if (index < 0 || index >= rows.length || index === cursorIndex())
48
101
  return;
49
- setCursorIndex(index);
50
- setAnchorIndex(index);
51
- options.onScrollToIndex?.(index);
102
+ placeCursor(index, true);
103
+ };
104
+ const focusKey = (key) => {
105
+ const index = options.rows().indexOfKey(key);
106
+ if (index < 0)
107
+ return;
108
+ if (index === cursorIndex())
109
+ options.onScrollToIndex?.(index);
110
+ else
111
+ focusIndex(index);
52
112
  };
53
113
  const rangeKeys = (from, to) => {
54
114
  const rows = options.rows();
55
- const anchorKind = rows[from]?.kind;
115
+ const anchorKind = rowAt(from)?.kind;
56
116
  const [lo, hi] = from <= to ? [from, to] : [to, from];
57
117
  const keys = new Set();
58
- for (let i = lo; i <= hi; i++) {
59
- const row = rows[i];
60
- if (!row)
61
- continue;
118
+ for (let i = Math.max(lo, 0); i <= Math.min(hi, rows.length - 1); i++) {
119
+ const row = rows.at(i);
62
120
  if (row.selectable === false)
63
121
  continue;
64
122
  if (anchorKind !== undefined && row.kind !== anchorKind)
@@ -69,8 +127,7 @@ export function createTreeController(options) {
69
127
  };
70
128
  const selectIndex = (index, modifier) => {
71
129
  clearTypeAhead();
72
- const rows = options.rows();
73
- const row = rows[index];
130
+ const row = rowAt(index);
74
131
  if (!row)
75
132
  return;
76
133
  if (row.selectable === false && modifier !== 'range') {
@@ -84,82 +141,72 @@ export function createTreeController(options) {
84
141
  else
85
142
  next.add(row.key);
86
143
  setSelectedKeys(next);
87
- setCursorIndex(index);
88
- setAnchorIndex(index);
89
- options.onScrollToIndex?.(index);
144
+ placeCursor(index, true);
90
145
  return;
91
146
  }
92
147
  if (modifier === 'range' && caps().canMultiSelect) {
93
- const from = anchorIndex() >= 0 ? anchorIndex() : index;
94
- setSelectedKeys(rangeKeys(from, index));
95
- setCursorIndex(index);
96
- options.onScrollToIndex?.(index);
148
+ const anchorAt = anchorIndex();
149
+ setSelectedKeys(rangeKeys(anchorAt >= 0 ? anchorAt : index, index));
150
+ placeCursor(index, false);
97
151
  return;
98
152
  }
99
153
  setSelectedKeys(new Set([row.key]));
100
- setCursorIndex(index);
101
- setAnchorIndex(index);
102
- options.onScrollToIndex?.(index);
154
+ placeCursor(index, true);
155
+ };
156
+ const selectKey = (key, modifier) => {
157
+ const index = options.rows().indexOfKey(key);
158
+ if (index >= 0)
159
+ selectIndex(index, modifier);
103
160
  };
104
161
  const clearSelection = () => {
105
162
  clearTypeAhead();
106
163
  if (selectedKeys().size > 0)
107
164
  setSelectedKeys(new Set());
108
165
  };
109
- const selectChildren = (index) => {
166
+ const forEachDirectChild = (index, visit) => {
110
167
  const rows = options.rows();
111
- const parent = rows[index];
168
+ const parent = rowAt(index);
112
169
  if (!parent)
113
170
  return;
114
171
  const childDepth = parent.depth + 1;
172
+ const end = rows.subtreeEnd(index);
173
+ for (let i = index + 1; i <= end; i = rows.subtreeEnd(i) + 1) {
174
+ const row = rows.at(i);
175
+ if (row.depth === childDepth)
176
+ visit(row, i);
177
+ }
178
+ };
179
+ const selectChildren = (index) => {
115
180
  const keys = new Set();
116
181
  let anchorKind;
117
182
  let seen = false;
118
183
  let firstChild = -1;
119
- for (let i = index + 1; i < rows.length; i++) {
120
- const row = rows[i];
121
- if (row.depth <= parent.depth)
122
- break;
123
- if (row.depth !== childDepth)
124
- continue;
184
+ forEachDirectChild(index, (row, childIndex) => {
125
185
  if (row.selectable === false)
126
- continue;
186
+ return;
127
187
  if (!seen) {
128
188
  anchorKind = row.kind;
129
- firstChild = i;
189
+ firstChild = childIndex;
130
190
  seen = true;
131
191
  }
132
192
  else if (anchorKind !== undefined && row.kind !== anchorKind)
133
- continue;
193
+ return;
134
194
  keys.add(row.key);
135
- }
195
+ });
136
196
  if (keys.size === 0)
137
197
  return;
138
198
  setSelectedKeys(keys);
139
- if (firstChild >= 0) {
140
- setCursorIndex(firstChild);
141
- setAnchorIndex(firstChild);
142
- options.onScrollToIndex?.(firstChild);
143
- }
199
+ if (firstChild >= 0)
200
+ placeCursor(firstChild, true);
144
201
  };
145
202
  const childKindsHomogeneous = (index) => {
146
- const rows = options.rows();
147
- const parent = rows[index];
148
- if (!parent)
149
- return { homogeneous: false, total: 0, selectable: 0 };
150
- const childDepth = parent.depth + 1;
151
203
  let anchorKind;
152
204
  let seen = false;
153
205
  let total = 0;
154
206
  let selectable = 0;
155
- for (let i = index + 1; i < rows.length; i++) {
156
- const row = rows[i];
157
- if (row.depth <= parent.depth)
158
- break;
159
- if (row.depth !== childDepth)
160
- continue;
207
+ forEachDirectChild(index, (row) => {
161
208
  if (row.selectable === false)
162
- continue;
209
+ return;
163
210
  total++;
164
211
  if (!seen) {
165
212
  anchorKind = row.kind;
@@ -168,7 +215,7 @@ export function createTreeController(options) {
168
215
  }
169
216
  else if (anchorKind === undefined || row.kind === anchorKind)
170
217
  selectable++;
171
- }
218
+ });
172
219
  return {
173
220
  homogeneous: total > 0 && total === selectable,
174
221
  total,
@@ -181,7 +228,7 @@ export function createTreeController(options) {
181
228
  setSelectedKeys(new Set(selectAllInScope(options.rows(), cursorIndex(), selectedKeys())));
182
229
  };
183
230
  const openMenuAt = (index, point) => {
184
- const row = options.rows()[index];
231
+ const row = rowAt(index);
185
232
  if (row)
186
233
  options.onOpenMenu?.(row, point);
187
234
  };
@@ -201,7 +248,7 @@ export function createTreeController(options) {
201
248
  });
202
249
  if (!intent)
203
250
  return;
204
- const current = rows[index];
251
+ const current = rowAt(index);
205
252
  switch (intent.type) {
206
253
  case 'move':
207
254
  event.preventDefault();
@@ -209,10 +256,9 @@ export function createTreeController(options) {
209
256
  return;
210
257
  case 'extend': {
211
258
  event.preventDefault();
212
- const from = anchorIndex() >= 0 ? anchorIndex() : index;
213
- setSelectedKeys(rangeKeys(from, intent.to));
214
- setCursorIndex(intent.to);
215
- options.onScrollToIndex?.(intent.to);
259
+ const anchorAt = anchorIndex();
260
+ setSelectedKeys(rangeKeys(anchorAt >= 0 ? anchorAt : index, intent.to));
261
+ placeCursor(intent.to, false);
216
262
  return;
217
263
  }
218
264
  case 'expandOrDescend':
@@ -233,7 +279,7 @@ export function createTreeController(options) {
233
279
  options.onToggle?.(current);
234
280
  return;
235
281
  }
236
- const parent = findParentIndex(rows, index);
282
+ const parent = rows.parentIndex(index);
237
283
  if (parent >= 0)
238
284
  focusIndex(parent);
239
285
  return;
@@ -302,7 +348,9 @@ export function createTreeController(options) {
302
348
  return caps();
303
349
  },
304
350
  focusIndex,
351
+ focusKey,
305
352
  selectIndex,
353
+ selectKey,
306
354
  clearSelection,
307
355
  selectChildren,
308
356
  selectAll,
@@ -1,17 +1,18 @@
1
- import { createMemo, useContext } from 'solid-js';
2
- import { createVirtualScroller, } from '../layout/createVirtualScroller';
1
+ import { createEffect, createMemo, on, untrack, useContext } from 'solid-js';
2
+ import { createVirtualWindow, } from '../layout/createVirtualWindow';
3
3
  import { DesktopContextMenuContext } from '../context/DesktopContextMenuContext';
4
4
  import { createTreeController, } from './createTreeController';
5
5
  import { computeStickyTreeState, stickyInsetForIndex, } from './stickyScroll';
6
+ import { anchoredContentTop, viewportAnchorAt, } from './viewportAnchor';
6
7
  import { BugIndicatingError } from '../_internal/base/errors/bugIndicatingError';
7
8
  const DEFAULT_OVERSCAN = 8;
8
9
  const DEFAULT_STICKY_MAX_COUNT = 7;
9
10
  const EMPTY_STICKY_STATE = { rows: [], height: 0 };
10
11
  export function createTreeSurface(descriptor) {
11
12
  const menu = useContext(DesktopContextMenuContext);
12
- const rows = createMemo(() => [...descriptor.rows()]);
13
- const scroller = createVirtualScroller({
14
- items: rows,
13
+ const source = createMemo(() => descriptor.rows());
14
+ const scroller = createVirtualWindow({
15
+ count: () => source().length,
15
16
  itemHeight: descriptor.rowHeight,
16
17
  overscan: descriptor.overscan ?? DEFAULT_OVERSCAN,
17
18
  });
@@ -19,7 +20,7 @@ export function createTreeSurface(descriptor) {
19
20
  if (descriptor.stickyScroll !== true)
20
21
  return EMPTY_STICKY_STATE;
21
22
  return computeStickyTreeState({
22
- rows: rows(),
23
+ rows: source(),
23
24
  scrollTop: scroller.scrollTop(),
24
25
  rowHeight: descriptor.rowHeight,
25
26
  viewportHeight: scroller.viewportHeight(),
@@ -27,10 +28,10 @@ export function createTreeSurface(descriptor) {
27
28
  });
28
29
  });
29
30
  const stickyInsetFor = (index) => descriptor.stickyScroll === true
30
- ? stickyInsetForIndex(rows(), index, descriptor.rowHeight, descriptor.stickyScrollMaxCount ?? DEFAULT_STICKY_MAX_COUNT)
31
+ ? stickyInsetForIndex(source(), index, descriptor.rowHeight, descriptor.stickyScrollMaxCount ?? DEFAULT_STICKY_MAX_COUNT)
31
32
  : 0;
32
33
  const controller = createTreeController({
33
- rows,
34
+ rows: source,
34
35
  ...(descriptor.capabilities
35
36
  ? { capabilities: descriptor.capabilities }
36
37
  : {}),
@@ -43,9 +44,24 @@ export function createTreeSurface(descriptor) {
43
44
  onDestroy: (row) => descriptor.onDestroy?.(row),
44
45
  onScrollToIndex: (index) => scroller.scrollToIndex(index, 'auto', stickyInsetFor(index)),
45
46
  });
46
- function openMenu(row, point, source) {
47
+ const cursorRow = () => {
48
+ const index = controller.cursorIndex();
49
+ return index < 0 ? undefined : source().at(index);
50
+ };
51
+ let viewportAnchor = null;
52
+ createEffect(() => {
53
+ const contentTop = scroller.scrollTop();
54
+ viewportAnchor = viewportAnchorAt(untrack(source), contentTop, descriptor.rowHeight);
55
+ });
56
+ createEffect(on(source, (rows) => {
57
+ const target = anchoredContentTop(rows, viewportAnchor, scroller.readScrollTop(), descriptor.rowHeight);
58
+ if (target !== null)
59
+ scroller.scrollToContentTop(target);
60
+ viewportAnchor = viewportAnchorAt(rows, scroller.readScrollTop(), descriptor.rowHeight);
61
+ }, { defer: true }));
62
+ function openMenu(row, point, origin) {
47
63
  if (descriptor.openMenu) {
48
- descriptor.openMenu(row, point, source);
64
+ descriptor.openMenu(row, point, origin);
49
65
  return;
50
66
  }
51
67
  if (!descriptor.buildMenu)
@@ -56,13 +72,14 @@ export function createTreeSurface(descriptor) {
56
72
  if (!menu) {
57
73
  throw new BugIndicatingError('createTreeSurface: descriptor builds a context menu but no DesktopContextMenuProvider is mounted');
58
74
  }
59
- menu.open(point.x, point.y, items, source);
75
+ menu.open(point.x, point.y, items, origin);
60
76
  }
61
77
  return {
62
78
  descriptor,
63
79
  controller,
64
80
  scroller,
65
- rows,
81
+ source,
82
+ cursorRow,
66
83
  stickyState,
67
84
  stickyInsetFor,
68
85
  openMenu,
@@ -6,3 +6,4 @@ export { DEFAULT_TREE_CAPABILITIES, } from './types';
6
6
  export * from './treeAria';
7
7
  export { createTreeController } from './createTreeController';
8
8
  export { selectAllInScope } from './selectAllInScope';
9
+ export { assertRowIndex, treeRowsFrom, } from './rowSource';
@@ -0,0 +1,93 @@
1
+ import { BugIndicatingError } from '../_internal/base/errors/bugIndicatingError';
2
+ export function assertRowIndex(source, index) {
3
+ if (!Number.isInteger(index) || index < 0 || index >= source.length) {
4
+ throw new BugIndicatingError(`tree row source: index ${index} is outside 0..${source.length - 1}`);
5
+ }
6
+ }
7
+ function structureTables(rows) {
8
+ const length = rows.length;
9
+ const parent = new Int32Array(length);
10
+ const subtreeEnd = new Int32Array(length);
11
+ const posInSet = new Int32Array(length);
12
+ const group = new Int32Array(length);
13
+ const groupSizes = [];
14
+ const stack = [];
15
+ const openGroupDepth = [];
16
+ const openGroupId = [];
17
+ let rootGroupDepth = -1;
18
+ let rootGroupId = -1;
19
+ for (let index = 0; index < length; index++) {
20
+ const depth = rows[index].depth;
21
+ while (stack.length > 0 && rows[stack[stack.length - 1]].depth >= depth) {
22
+ subtreeEnd[stack.pop()] = index - 1;
23
+ openGroupDepth.pop();
24
+ openGroupId.pop();
25
+ }
26
+ const slot = stack.length - 1;
27
+ parent[index] = slot < 0 ? -1 : stack[slot];
28
+ const currentDepth = slot < 0 ? rootGroupDepth : openGroupDepth[slot];
29
+ let id = slot < 0 ? rootGroupId : openGroupId[slot];
30
+ if (currentDepth !== depth) {
31
+ id = groupSizes.length;
32
+ groupSizes.push(0);
33
+ if (slot < 0) {
34
+ rootGroupDepth = depth;
35
+ rootGroupId = id;
36
+ }
37
+ else {
38
+ openGroupDepth[slot] = depth;
39
+ openGroupId[slot] = id;
40
+ }
41
+ }
42
+ groupSizes[id] = groupSizes[id] + 1;
43
+ group[index] = id;
44
+ posInSet[index] = groupSizes[id];
45
+ stack.push(index);
46
+ openGroupDepth.push(-1);
47
+ openGroupId.push(-1);
48
+ }
49
+ for (const open of stack)
50
+ subtreeEnd[open] = length - 1;
51
+ return { parent, subtreeEnd, posInSet, group, groupSizes };
52
+ }
53
+ function keyIndex(rows) {
54
+ const index = new Map();
55
+ for (let row = 0; row < rows.length; row++) {
56
+ const key = rows[row].key;
57
+ const seen = index.get(key);
58
+ if (seen !== undefined) {
59
+ throw new BugIndicatingError(`tree row source: key "${key}" occurs at rows ${seen} and ${row}`);
60
+ }
61
+ index.set(key, row);
62
+ }
63
+ return index;
64
+ }
65
+ export function treeRowsFrom(input) {
66
+ const rows = input.slice();
67
+ const tables = structureTables(rows);
68
+ const keys = keyIndex(rows);
69
+ const source = {
70
+ length: rows.length,
71
+ at: (index) => {
72
+ assertRowIndex(source, index);
73
+ return rows[index];
74
+ },
75
+ parentIndex: (index) => {
76
+ assertRowIndex(source, index);
77
+ return tables.parent[index];
78
+ },
79
+ subtreeEnd: (index) => {
80
+ assertRowIndex(source, index);
81
+ return tables.subtreeEnd[index];
82
+ },
83
+ siblingPosition: (index) => {
84
+ assertRowIndex(source, index);
85
+ return {
86
+ posInSet: tables.posInSet[index],
87
+ setSize: tables.groupSizes[tables.group[index]],
88
+ };
89
+ },
90
+ indexOfKey: (key) => keys.get(key) ?? -1,
91
+ };
92
+ return source;
93
+ }
@@ -1,47 +1,53 @@
1
- import { findChildRange, findParentIndex } from './treeKeymap';
2
1
  export function selectAllInScope(rows, cursor, selection) {
3
- const indexByKey = new Map(rows.map((row, index) => [row.key, index]));
4
- const selected = [...selection].filter((key) => indexByKey.has(key));
5
- const start = startIndex(rows, cursor, selected, indexByKey);
2
+ const selected = [...selection].filter((key) => rows.indexOfKey(key) >= 0);
3
+ const start = startIndex(rows, cursor, selected);
6
4
  const kind = lockKind(rows, start);
7
5
  const pickable = (row) => row.selectable !== false && (kind === undefined || row.kind === kind);
8
6
  const selectedSet = new Set(selected);
9
- let scope = start < 0 ? -1 : findParentIndex(rows, start);
7
+ let scope = start < 0 ? -1 : rows.parentIndex(start);
10
8
  for (;;) {
11
- const picked = descendantsOf(rows, scope)
12
- .filter(pickable)
13
- .map((row) => row.key);
9
+ const picked = pickedUnder(rows, scope, pickable);
14
10
  if (scope < 0)
15
11
  return picked;
16
12
  const covered = picked.length === selectedSet.size &&
17
13
  picked.every((key) => selectedSet.has(key));
18
14
  if (picked.length > 0 && !covered)
19
15
  return picked;
20
- const scopeRow = rows[scope];
16
+ const scopeRow = rows.at(scope);
21
17
  if (picked.length > 0 && pickable(scopeRow))
22
18
  return [scopeRow.key, ...picked];
23
- scope = findParentIndex(rows, scope);
19
+ scope = rows.parentIndex(scope);
24
20
  }
25
21
  }
26
- function startIndex(rows, cursor, selected, indexByKey) {
27
- const cursorKey = rows[cursor]?.key;
22
+ function startIndex(rows, cursor, selected) {
23
+ const cursorKey = cursor >= 0 && cursor < rows.length ? rows.at(cursor).key : undefined;
28
24
  if (cursorKey !== undefined && !selected.includes(cursorKey))
29
25
  return cursor;
30
26
  const first = selected[0];
31
- return first === undefined ? -1 : (indexByKey.get(first) ?? -1);
27
+ return first === undefined ? -1 : rows.indexOfKey(first);
32
28
  }
33
29
  function lockKind(rows, start) {
34
- const own = rows[start];
35
- if (own && own.selectable !== false)
36
- return own.kind;
37
- const isSelectable = (row) => row.selectable !== false;
38
- const next = rows.slice(Math.max(start, 0)).find(isSelectable) ??
39
- rows.find(isSelectable);
40
- return next?.kind;
30
+ if (start >= 0) {
31
+ const own = rows.at(start);
32
+ if (own.selectable !== false)
33
+ return own.kind;
34
+ }
35
+ const from = Math.max(start, 0);
36
+ for (let step = 0; step < rows.length; step++) {
37
+ const row = rows.at((from + step) % rows.length);
38
+ if (row.selectable !== false)
39
+ return row.kind;
40
+ }
41
+ return undefined;
41
42
  }
42
- function descendantsOf(rows, scope) {
43
- if (scope < 0)
44
- return rows;
45
- const range = findChildRange(rows, scope);
46
- return range ? rows.slice(range.start, range.end + 1) : [];
43
+ function pickedUnder(rows, scope, pickable) {
44
+ const first = scope < 0 ? 0 : scope + 1;
45
+ const last = scope < 0 ? rows.length - 1 : rows.subtreeEnd(scope);
46
+ const picked = [];
47
+ for (let index = first; index <= last; index++) {
48
+ const row = rows.at(index);
49
+ if (pickable(row))
50
+ picked.push(row.key);
51
+ }
52
+ return picked;
47
53
  }
@@ -1,4 +1,3 @@
1
- import { findChildRange, findParentIndex } from './treeKeymap';
2
1
  const MAX_WIDGET_VIEW_RATIO = 0.4;
3
2
  const EMPTY_STATE = { rows: [], height: 0 };
4
3
  function clampMaxCount(maxCount) {
@@ -6,12 +5,12 @@ function clampMaxCount(maxCount) {
6
5
  }
7
6
  function ancestorUnderPrevious(rows, index, prevIndex) {
8
7
  let cur = index;
9
- let parent = findParentIndex(rows, cur);
8
+ let parent = rows.parentIndex(cur);
10
9
  while (parent !== -1) {
11
10
  if (parent === prevIndex)
12
11
  return cur;
13
12
  cur = parent;
14
- parent = findParentIndex(rows, cur);
13
+ parent = rows.parentIndex(cur);
15
14
  }
16
15
  return prevIndex === -1 ? cur : -1;
17
16
  }
@@ -44,18 +43,18 @@ export function computeStickyTreeState(params) {
44
43
  const i = ancestorUnderPrevious(rows, underIndex, prevIndex);
45
44
  if (i === -1)
46
45
  break;
47
- const range = findChildRange(rows, i);
48
- if (range === null)
46
+ const end = rows.subtreeEnd(i);
47
+ if (end === i)
49
48
  break;
50
49
  if (i === underIndex && scrollTop === i * rowHeight - height)
51
50
  break;
52
- const bottomOfLastChild = (range.end + 1) * rowHeight - scrollTop;
51
+ const bottomOfLastChild = (end + 1) * rowHeight - scrollTop;
53
52
  const desiredTop = height;
54
53
  const top = desiredTop + rowHeight > bottomOfLastChild &&
55
54
  desiredTop <= bottomOfLastChild
56
55
  ? bottomOfLastChild - rowHeight
57
56
  : desiredTop;
58
- sticky.push({ index: i, row: rows[i], top });
57
+ sticky.push({ index: i, row: rows.at(i), top });
59
58
  height = top + rowHeight;
60
59
  prevIndex = i;
61
60
  if (sticky.length >= maxCount)
@@ -66,11 +65,14 @@ export function computeStickyTreeState(params) {
66
65
  export function stickyInsetForIndex(rows, index, rowHeight, maxCount) {
67
66
  if (index < 0 || index >= rows.length)
68
67
  return 0;
68
+ const limit = clampMaxCount(maxCount);
69
69
  let ancestors = 0;
70
- let cur = findParentIndex(rows, index);
70
+ let cur = rows.parentIndex(index);
71
71
  while (cur !== -1) {
72
72
  ancestors++;
73
- cur = findParentIndex(rows, cur);
73
+ if (ancestors === limit)
74
+ break;
75
+ cur = rows.parentIndex(cur);
74
76
  }
75
- return Math.min(ancestors, clampMaxCount(maxCount)) * rowHeight;
77
+ return ancestors * rowHeight;
76
78
  }
@@ -13,44 +13,25 @@ export function buildTreeContainerAria(params) {
13
13
  : undefined,
14
14
  };
15
15
  }
16
- export function buildTreeRowAria(params) {
17
- const { treeId, rows, index, focusMode, isCursor, isSelected } = params;
18
- const row = rows[index];
16
+ export function buildTreeRowStructureAria(params) {
17
+ const { treeId, rows, index } = params;
18
+ const row = rows.at(index);
19
19
  const flat = params.flat === true;
20
- let start = index;
21
- for (let i = index - 1; i >= 0; i--) {
22
- const depth = rows[i].depth;
23
- if (depth < row.depth)
24
- break;
25
- if (depth === row.depth)
26
- start = i;
27
- }
28
- let end = index;
29
- for (let i = index + 1; i < rows.length; i++) {
30
- const depth = rows[i].depth;
31
- if (depth < row.depth)
32
- break;
33
- if (depth === row.depth)
34
- end = i;
35
- }
36
- let setSize = 0;
37
- let posInSet = 0;
38
- for (let i = start; i <= end; i++) {
39
- if (rows[i].depth !== row.depth)
40
- continue;
41
- setSize++;
42
- if (i <= index)
43
- posInSet++;
44
- }
20
+ const { posInSet, setSize } = rows.siblingPosition(index);
45
21
  return {
46
22
  role: flat ? 'option' : 'treeitem',
47
23
  id: treeRowDomId(treeId, row.key),
48
- tabIndex: focusMode === 'roving-tabindex' && isCursor ? 0 : -1,
49
24
  'aria-level': flat ? undefined : row.depth + 1,
50
25
  'aria-setsize': setSize,
51
26
  'aria-posinset': posInSet,
52
- 'aria-selected': isSelected,
53
27
  'aria-expanded': !flat && row.expandable ? row.expanded : undefined,
54
28
  'aria-busy': row.busy === true ? true : undefined,
55
29
  };
56
30
  }
31
+ export function buildTreeRowAria(params) {
32
+ return {
33
+ ...params.structure,
34
+ tabIndex: params.focusMode === 'roving-tabindex' && params.isCursor ? 0 : -1,
35
+ 'aria-selected': params.isSelected,
36
+ };
37
+ }