@opendata-ai/openchart-vanilla 8.0.0-rc.8 → 8.0.0

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 (147) hide show
  1. package/dist/{chunk-W34KN7FD.js → chunk-2NAQRP66.js} +2 -2
  2. package/dist/{chunk-UZ7J3NOF.js → chunk-6BUCYCLD.js} +7314 -5605
  3. package/dist/chunk-6BUCYCLD.js.map +1 -0
  4. package/dist/chunk-DCODCLWI.js +234 -0
  5. package/dist/chunk-DCODCLWI.js.map +1 -0
  6. package/dist/{chunk-5EOV5JQZ.js → chunk-EKAUQAAX.js} +50 -46
  7. package/dist/chunk-EKAUQAAX.js.map +1 -0
  8. package/dist/chunk-RM5XS6NR.js +113 -0
  9. package/dist/chunk-RM5XS6NR.js.map +1 -0
  10. package/dist/chunk-T32UHMZA.js +282 -0
  11. package/dist/chunk-T32UHMZA.js.map +1 -0
  12. package/dist/export-gif.d.ts +113 -0
  13. package/dist/export-gif.js +21 -0
  14. package/dist/export-gif.js.map +1 -0
  15. package/dist/index.d.ts +309 -27
  16. package/dist/index.js +2020 -836
  17. package/dist/index.js.map +1 -1
  18. package/dist/{mount-CUKQLeqD.d.ts → mount-CL-nA0f-.d.ts} +85 -2
  19. package/dist/simulation-worker.js +83 -2
  20. package/dist/static.d.ts +2 -0
  21. package/dist/static.js +8 -69
  22. package/dist/static.js.map +1 -1
  23. package/dist/story/index.d.ts +25 -9
  24. package/dist/story/index.js +9 -7
  25. package/dist/story/index.js.map +1 -1
  26. package/dist/styles.css +1 -1
  27. package/dist/styles.css.map +1 -1
  28. package/package.json +14 -6
  29. package/dist/chunk-5EOV5JQZ.js.map +0 -1
  30. package/dist/chunk-UZ7J3NOF.js.map +0 -1
  31. package/src/__test-fixtures__/dom.ts +0 -70
  32. package/src/__test-fixtures__/specs.ts +0 -214
  33. package/src/__tests__/animation.test.ts +0 -358
  34. package/src/__tests__/annotations-render.test.ts +0 -201
  35. package/src/__tests__/compound-labels.test.ts +0 -122
  36. package/src/__tests__/continuous-legend.test.ts +0 -91
  37. package/src/__tests__/crosshair.test.ts +0 -130
  38. package/src/__tests__/edit-events.test.ts +0 -950
  39. package/src/__tests__/events.test.ts +0 -387
  40. package/src/__tests__/export.test.ts +0 -241
  41. package/src/__tests__/font-race.test.ts +0 -199
  42. package/src/__tests__/gradient-ids.test.ts +0 -159
  43. package/src/__tests__/gradient-utils.test.ts +0 -125
  44. package/src/__tests__/known-bugs-render.test.ts +0 -183
  45. package/src/__tests__/map.test.ts +0 -636
  46. package/src/__tests__/mount.test.ts +0 -357
  47. package/src/__tests__/pattern-fills.test.ts +0 -105
  48. package/src/__tests__/resize-timing.test.ts +0 -218
  49. package/src/__tests__/sankey.test.ts +0 -133
  50. package/src/__tests__/selection-events.test.ts +0 -869
  51. package/src/__tests__/series-search.test.ts +0 -311
  52. package/src/__tests__/static.test.ts +0 -141
  53. package/src/__tests__/svg-renderer.test.ts +0 -895
  54. package/src/__tests__/table-keyboard.test.ts +0 -361
  55. package/src/__tests__/table-mount.test.ts +0 -639
  56. package/src/__tests__/tilemap.test.ts +0 -158
  57. package/src/__tests__/tooltip.test.ts +0 -337
  58. package/src/__tests__/transition.test.ts +0 -1792
  59. package/src/__tests__/update-scrolly-matrix.test.ts +0 -563
  60. package/src/__tests__/you-draw-it.test.ts +0 -273
  61. package/src/animation-vars.ts +0 -42
  62. package/src/animation.ts +0 -76
  63. package/src/barlist-mount.ts +0 -383
  64. package/src/barlist-renderer.ts +0 -267
  65. package/src/dom-helpers.ts +0 -17
  66. package/src/export.ts +0 -482
  67. package/src/gradient-utils.ts +0 -138
  68. package/src/graph/__tests__/canvas-renderer.test.ts +0 -713
  69. package/src/graph/__tests__/graph-mount.test.ts +0 -213
  70. package/src/graph/__tests__/interaction.test.ts +0 -205
  71. package/src/graph/__tests__/keyboard.test.ts +0 -653
  72. package/src/graph/__tests__/search.test.ts +0 -88
  73. package/src/graph/__tests__/simulation.test.ts +0 -233
  74. package/src/graph/__tests__/spatial-index.test.ts +0 -142
  75. package/src/graph/__tests__/zoom.test.ts +0 -203
  76. package/src/graph/canvas-renderer.ts +0 -747
  77. package/src/graph/interaction.ts +0 -367
  78. package/src/graph/keyboard.ts +0 -208
  79. package/src/graph/search.ts +0 -50
  80. package/src/graph/simulation-worker-url.ts +0 -12
  81. package/src/graph/simulation-worker.ts +0 -278
  82. package/src/graph/simulation.ts +0 -422
  83. package/src/graph/spatial-index.ts +0 -121
  84. package/src/graph/types.ts +0 -47
  85. package/src/graph/worker-protocol.ts +0 -70
  86. package/src/graph/zoom.ts +0 -134
  87. package/src/graph-mount.ts +0 -888
  88. package/src/index.ts +0 -76
  89. package/src/interactions/chart-events.ts +0 -139
  90. package/src/interactions/crosshair.ts +0 -233
  91. package/src/interactions/drag-handler.ts +0 -175
  92. package/src/interactions/editing-drags.ts +0 -515
  93. package/src/interactions/index.ts +0 -25
  94. package/src/interactions/keyboard-nav.ts +0 -111
  95. package/src/interactions/legend-interaction.ts +0 -46
  96. package/src/interactions/selection.ts +0 -280
  97. package/src/interactions/tooltip-events.ts +0 -72
  98. package/src/map-camera.ts +0 -111
  99. package/src/map-mount.ts +0 -792
  100. package/src/map-renderer.ts +0 -460
  101. package/src/map-transition.ts +0 -99
  102. package/src/measure-text.ts +0 -134
  103. package/src/mount.ts +0 -1382
  104. package/src/pattern-utils.ts +0 -110
  105. package/src/renderers/annotations.ts +0 -388
  106. package/src/renderers/axes.ts +0 -351
  107. package/src/renderers/brand.ts +0 -87
  108. package/src/renderers/chrome.ts +0 -196
  109. package/src/renderers/endpoint-labels.ts +0 -150
  110. package/src/renderers/legend.ts +0 -311
  111. package/src/renderers/marks.ts +0 -561
  112. package/src/renderers/metrics.ts +0 -63
  113. package/src/renderers/svg-dom.ts +0 -40
  114. package/src/renderers/table-cells.ts +0 -446
  115. package/src/resize-observer.ts +0 -46
  116. package/src/sankey-mount.ts +0 -622
  117. package/src/sankey-renderer.ts +0 -614
  118. package/src/series-search.ts +0 -392
  119. package/src/static.ts +0 -251
  120. package/src/story/__tests__/camera-math.test.ts +0 -145
  121. package/src/story/__tests__/create-chart-story.test.ts +0 -317
  122. package/src/story/__tests__/progress-math.test.ts +0 -95
  123. package/src/story/__tests__/scroll-driver.test.ts +0 -130
  124. package/src/story/__tests__/tween.test.ts +0 -159
  125. package/src/story/camera-math.ts +0 -125
  126. package/src/story/create-chart-story.ts +0 -353
  127. package/src/story/crossfade.ts +0 -70
  128. package/src/story/index.ts +0 -39
  129. package/src/story/progress-math.ts +0 -85
  130. package/src/story/resolve-camera-target.ts +0 -119
  131. package/src/story/scroll-driver.ts +0 -206
  132. package/src/story/story-camera.ts +0 -49
  133. package/src/story/tween.ts +0 -112
  134. package/src/story/types.ts +0 -76
  135. package/src/svg-ids.ts +0 -23
  136. package/src/svg-renderer.ts +0 -383
  137. package/src/table-keyboard.ts +0 -266
  138. package/src/table-mount.ts +0 -567
  139. package/src/table-renderer.ts +0 -414
  140. package/src/text-edit-overlay.ts +0 -255
  141. package/src/theme-tokens.ts +0 -51
  142. package/src/tilemap-mount.ts +0 -511
  143. package/src/tilemap-renderer.ts +0 -396
  144. package/src/tooltip.ts +0 -177
  145. package/src/transition.ts +0 -2816
  146. package/src/you-draw-it.ts +0 -480
  147. /package/dist/{chunk-W34KN7FD.js.map → chunk-2NAQRP66.js.map} +0 -0
@@ -1,653 +0,0 @@
1
- import { beforeEach, describe, expect, it, vi } from 'vitest';
2
- import { attachGraphKeyboardNav, type KeyboardNavOptions } from '../keyboard';
3
- import type { PositionedNode } from '../types';
4
-
5
- // ---------------------------------------------------------------------------
6
- // Helpers
7
- // ---------------------------------------------------------------------------
8
-
9
- function makeNode(id: string, x: number, y: number, radius = 10): PositionedNode {
10
- return {
11
- id,
12
- x,
13
- y,
14
- radius,
15
- fill: '#3b82f6',
16
- stroke: '#2563eb',
17
- strokeWidth: 1,
18
- label: id,
19
- labelPriority: 0.5,
20
- community: undefined,
21
- data: {},
22
- };
23
- }
24
-
25
- function createMockCanvas(): HTMLCanvasElement {
26
- const canvas = document.createElement('canvas');
27
- canvas.width = 800;
28
- canvas.height = 600;
29
- return canvas;
30
- }
31
-
32
- /**
33
- * Build a standard set of positioned nodes for directional testing.
34
- *
35
- * up (400, 100)
36
- * |
37
- * left (100, 300) -- center (400, 300) -- right (700, 300)
38
- * |
39
- * down (400, 500)
40
- */
41
- function makeCrossNodes(): PositionedNode[] {
42
- return [
43
- makeNode('center', 400, 300),
44
- makeNode('right', 700, 300),
45
- makeNode('left', 100, 300),
46
- makeNode('up', 400, 100),
47
- makeNode('down', 400, 500),
48
- ];
49
- }
50
-
51
- /** Adjacency where center connects to all four directions. */
52
- function makeCrossAdjacency(): Map<string, Set<string>> {
53
- const adj = new Map<string, Set<string>>();
54
- adj.set('center', new Set(['right', 'left', 'up', 'down']));
55
- adj.set('right', new Set(['center']));
56
- adj.set('left', new Set(['center']));
57
- adj.set('up', new Set(['center']));
58
- adj.set('down', new Set(['center']));
59
- return adj;
60
- }
61
-
62
- interface MockCallbacks {
63
- onSelect: ReturnType<typeof vi.fn>;
64
- onDeselect: ReturnType<typeof vi.fn>;
65
- onZoom: ReturnType<typeof vi.fn>;
66
- onFitAll: ReturnType<typeof vi.fn>;
67
- onFocusSearch: ReturnType<typeof vi.fn>;
68
- }
69
-
70
- function createOptions(overrides?: Partial<KeyboardNavOptions>): {
71
- options: KeyboardNavOptions;
72
- callbacks: MockCallbacks;
73
- canvas: HTMLCanvasElement;
74
- } {
75
- const canvas = overrides?.canvas ?? createMockCanvas();
76
- const nodes = makeCrossNodes();
77
- const adjacency = makeCrossAdjacency();
78
-
79
- const callbacks: MockCallbacks = {
80
- onSelect: vi.fn(),
81
- onDeselect: vi.fn(),
82
- onZoom: vi.fn(),
83
- onFitAll: vi.fn(),
84
- onFocusSearch: vi.fn(),
85
- };
86
-
87
- const options: KeyboardNavOptions = {
88
- canvas,
89
- getNodes: () => nodes,
90
- getSelectedIds: () => [],
91
- getAdjacency: () => adjacency,
92
- onSelect: callbacks.onSelect,
93
- onDeselect: callbacks.onDeselect,
94
- onZoom: callbacks.onZoom,
95
- onFitAll: callbacks.onFitAll,
96
- onFocusSearch: callbacks.onFocusSearch,
97
- ...overrides,
98
- };
99
-
100
- return { options, callbacks, canvas };
101
- }
102
-
103
- function keydown(canvas: HTMLCanvasElement, key: string): void {
104
- canvas.dispatchEvent(new KeyboardEvent('keydown', { key, bubbles: true }));
105
- }
106
-
107
- // ---------------------------------------------------------------------------
108
- // Tests
109
- // ---------------------------------------------------------------------------
110
-
111
- describe('attachGraphKeyboardNav', () => {
112
- let canvas: HTMLCanvasElement;
113
- let callbacks: MockCallbacks;
114
- let cleanup: () => void;
115
-
116
- beforeEach(() => {
117
- const setup = createOptions();
118
- canvas = setup.canvas;
119
- callbacks = setup.callbacks;
120
- cleanup = attachGraphKeyboardNav(setup.options);
121
- });
122
-
123
- // -----------------------------------------------------------------------
124
- // Tab focus
125
- // -----------------------------------------------------------------------
126
-
127
- describe('Tab', () => {
128
- it('focuses the first node when none selected', () => {
129
- // Tab should set internal focus to first node (center). It does not
130
- // call onSelect on its own -- the focus is internal. Arrow keys after
131
- // Tab will use the focused node for navigation.
132
- keydown(canvas, 'Tab');
133
-
134
- // Verify focus by pressing ArrowRight, which should navigate from
135
- // center to right and call onSelect('right').
136
- keydown(canvas, 'ArrowRight');
137
- expect(callbacks.onSelect).toHaveBeenCalledWith('right');
138
- });
139
-
140
- it('focuses the selected node when one is already selected', () => {
141
- const {
142
- options,
143
- callbacks: cbs,
144
- canvas: c,
145
- } = createOptions({
146
- getSelectedIds: () => ['left'],
147
- });
148
- const cl = attachGraphKeyboardNav(options);
149
-
150
- keydown(c, 'Tab');
151
- // Now focused on 'left'. ArrowRight from left should navigate to center.
152
- keydown(c, 'ArrowRight');
153
- expect(cbs.onSelect).toHaveBeenCalledWith('center');
154
-
155
- cl();
156
- });
157
-
158
- it('does nothing when there are no nodes', () => {
159
- const {
160
- options,
161
- callbacks: cbs,
162
- canvas: c,
163
- } = createOptions({
164
- getNodes: () => [],
165
- });
166
- const cl = attachGraphKeyboardNav(options);
167
-
168
- keydown(c, 'Tab');
169
- // No error, no callbacks fired
170
- expect(cbs.onSelect).not.toHaveBeenCalled();
171
-
172
- cl();
173
- });
174
- });
175
-
176
- // -----------------------------------------------------------------------
177
- // Arrow key navigation
178
- // -----------------------------------------------------------------------
179
-
180
- describe('ArrowKey navigation', () => {
181
- // First Tab to set focus to center, then test arrow keys
182
- function focusCenter(): void {
183
- keydown(canvas, 'Tab');
184
- }
185
-
186
- it('ArrowRight navigates to the right neighbor', () => {
187
- focusCenter();
188
- keydown(canvas, 'ArrowRight');
189
- expect(callbacks.onSelect).toHaveBeenCalledWith('right');
190
- });
191
-
192
- it('ArrowLeft navigates to the left neighbor', () => {
193
- focusCenter();
194
- keydown(canvas, 'ArrowLeft');
195
- expect(callbacks.onSelect).toHaveBeenCalledWith('left');
196
- });
197
-
198
- it('ArrowUp navigates to the up neighbor', () => {
199
- focusCenter();
200
- keydown(canvas, 'ArrowUp');
201
- expect(callbacks.onSelect).toHaveBeenCalledWith('up');
202
- });
203
-
204
- it('ArrowDown navigates to the down neighbor', () => {
205
- focusCenter();
206
- keydown(canvas, 'ArrowDown');
207
- expect(callbacks.onSelect).toHaveBeenCalledWith('down');
208
- });
209
-
210
- it('does nothing when no node is focused', () => {
211
- // No Tab pressed, so no focus. Arrow keys should be no-ops.
212
- keydown(canvas, 'ArrowRight');
213
- expect(callbacks.onSelect).not.toHaveBeenCalled();
214
- });
215
-
216
- it('does nothing when there are no neighbors', () => {
217
- const isolated = [makeNode('alone', 400, 300)];
218
- const emptyAdj = new Map<string, Set<string>>();
219
- emptyAdj.set('alone', new Set());
220
-
221
- const {
222
- options,
223
- callbacks: cbs,
224
- canvas: c,
225
- } = createOptions({
226
- getNodes: () => isolated,
227
- getAdjacency: () => emptyAdj,
228
- });
229
- const cl = attachGraphKeyboardNav(options);
230
-
231
- keydown(c, 'Tab');
232
- keydown(c, 'ArrowRight');
233
- expect(cbs.onSelect).not.toHaveBeenCalled();
234
-
235
- cl();
236
- });
237
-
238
- it('does nothing when adjacency has no entry for focused node', () => {
239
- const isolated = [makeNode('alone', 400, 300)];
240
- const emptyAdj = new Map<string, Set<string>>();
241
- // No entry at all for 'alone'
242
-
243
- const {
244
- options,
245
- callbacks: cbs,
246
- canvas: c,
247
- } = createOptions({
248
- getNodes: () => isolated,
249
- getAdjacency: () => emptyAdj,
250
- });
251
- const cl = attachGraphKeyboardNav(options);
252
-
253
- keydown(c, 'Tab');
254
- keydown(c, 'ArrowRight');
255
- expect(cbs.onSelect).not.toHaveBeenCalled();
256
-
257
- cl();
258
- });
259
- });
260
-
261
- // -----------------------------------------------------------------------
262
- // Enter toggles selection
263
- // -----------------------------------------------------------------------
264
-
265
- describe('Enter', () => {
266
- it('selects the focused node when not already selected', () => {
267
- keydown(canvas, 'Tab'); // focus center
268
- keydown(canvas, 'Enter');
269
- expect(callbacks.onSelect).toHaveBeenCalledWith('center');
270
- });
271
-
272
- it('deselects the focused node when already selected', () => {
273
- const {
274
- options,
275
- callbacks: cbs,
276
- canvas: c,
277
- } = createOptions({
278
- getSelectedIds: () => ['center'],
279
- });
280
- const cl = attachGraphKeyboardNav(options);
281
-
282
- keydown(c, 'Tab'); // focus goes to selected node 'center'
283
- keydown(c, 'Enter');
284
- expect(cbs.onDeselect).toHaveBeenCalled();
285
- expect(cbs.onSelect).not.toHaveBeenCalled();
286
-
287
- cl();
288
- });
289
-
290
- it('does nothing when no node is focused', () => {
291
- keydown(canvas, 'Enter');
292
- expect(callbacks.onSelect).not.toHaveBeenCalled();
293
- expect(callbacks.onDeselect).not.toHaveBeenCalled();
294
- });
295
- });
296
-
297
- // -----------------------------------------------------------------------
298
- // Escape clears focus and selection
299
- // -----------------------------------------------------------------------
300
-
301
- describe('Escape', () => {
302
- it('clears focus and calls onDeselect', () => {
303
- keydown(canvas, 'Tab'); // focus center
304
- keydown(canvas, 'Escape');
305
-
306
- expect(callbacks.onDeselect).toHaveBeenCalled();
307
-
308
- // Focus is cleared, so arrow keys should do nothing
309
- keydown(canvas, 'ArrowRight');
310
- expect(callbacks.onSelect).not.toHaveBeenCalled();
311
- });
312
- });
313
-
314
- // -----------------------------------------------------------------------
315
- // Zoom keys
316
- // -----------------------------------------------------------------------
317
-
318
- describe('zoom keys', () => {
319
- it('+ triggers zoom in', () => {
320
- keydown(canvas, '+');
321
- expect(callbacks.onZoom).toHaveBeenCalledWith('in');
322
- });
323
-
324
- it('= triggers zoom in', () => {
325
- keydown(canvas, '=');
326
- expect(callbacks.onZoom).toHaveBeenCalledWith('in');
327
- });
328
-
329
- it('- triggers zoom out', () => {
330
- keydown(canvas, '-');
331
- expect(callbacks.onZoom).toHaveBeenCalledWith('out');
332
- });
333
-
334
- it('_ triggers zoom out', () => {
335
- keydown(canvas, '_');
336
- expect(callbacks.onZoom).toHaveBeenCalledWith('out');
337
- });
338
- });
339
-
340
- // -----------------------------------------------------------------------
341
- // Home / fit all
342
- // -----------------------------------------------------------------------
343
-
344
- describe('Home', () => {
345
- it('triggers fitAll', () => {
346
- keydown(canvas, 'Home');
347
- expect(callbacks.onFitAll).toHaveBeenCalled();
348
- });
349
- });
350
-
351
- // -----------------------------------------------------------------------
352
- // / search focus
353
- // -----------------------------------------------------------------------
354
-
355
- describe('/ (search)', () => {
356
- it('triggers focusSearch when provided', () => {
357
- keydown(canvas, '/');
358
- expect(callbacks.onFocusSearch).toHaveBeenCalled();
359
- });
360
-
361
- it('does not throw when onFocusSearch is not provided', () => {
362
- const { options, canvas: c } = createOptions();
363
- // Remove the onFocusSearch callback
364
- delete (options as Partial<KeyboardNavOptions>).onFocusSearch;
365
- const cl = attachGraphKeyboardNav(options);
366
-
367
- expect(() => keydown(c, '/')).not.toThrow();
368
-
369
- cl();
370
- });
371
- });
372
-
373
- // -----------------------------------------------------------------------
374
- // Canvas tabindex
375
- // -----------------------------------------------------------------------
376
-
377
- describe('tabindex', () => {
378
- it('sets tabindex="0" on canvas if not already set', () => {
379
- const c = createMockCanvas();
380
- const { options } = createOptions({ canvas: c });
381
- const cl = attachGraphKeyboardNav(options);
382
-
383
- expect(c.getAttribute('tabindex')).toBe('0');
384
-
385
- cl();
386
- });
387
-
388
- it('preserves existing tabindex', () => {
389
- const c = createMockCanvas();
390
- c.setAttribute('tabindex', '-1');
391
- const { options } = createOptions({ canvas: c });
392
- const cl = attachGraphKeyboardNav(options);
393
-
394
- expect(c.getAttribute('tabindex')).toBe('-1');
395
-
396
- cl();
397
- });
398
- });
399
-
400
- // -----------------------------------------------------------------------
401
- // Cleanup
402
- // -----------------------------------------------------------------------
403
-
404
- describe('cleanup', () => {
405
- it('removes the keydown listener', () => {
406
- cleanup();
407
-
408
- keydown(canvas, '+');
409
- expect(callbacks.onZoom).not.toHaveBeenCalled();
410
-
411
- keydown(canvas, 'Home');
412
- expect(callbacks.onFitAll).not.toHaveBeenCalled();
413
- });
414
- });
415
-
416
- // -----------------------------------------------------------------------
417
- // pickDirectionalNeighbor scoring
418
- // -----------------------------------------------------------------------
419
-
420
- describe('directional scoring', () => {
421
- it('right prefers positive dx even with some dy offset', () => {
422
- // Center at (0, 0). Two neighbors: one slightly up-right, one slightly down-left.
423
- const nodes = [
424
- makeNode('center', 0, 0),
425
- makeNode('up-right', 100, -30), // dx=100, dy=-30 -> score = 100 - 15 = 85
426
- makeNode('down-left', -50, 20), // dx=-50, dy=20 -> score = -50 - 10 = -60
427
- ];
428
- const adj = new Map<string, Set<string>>();
429
- adj.set('center', new Set(['up-right', 'down-left']));
430
- adj.set('up-right', new Set(['center']));
431
- adj.set('down-left', new Set(['center']));
432
-
433
- const {
434
- options,
435
- callbacks: cbs,
436
- canvas: c,
437
- } = createOptions({
438
- getNodes: () => nodes,
439
- getAdjacency: () => adj,
440
- });
441
- const cl = attachGraphKeyboardNav(options);
442
-
443
- keydown(c, 'Tab');
444
- keydown(c, 'ArrowRight');
445
- expect(cbs.onSelect).toHaveBeenCalledWith('up-right');
446
-
447
- cl();
448
- });
449
-
450
- it('left prefers negative dx', () => {
451
- const nodes = [
452
- makeNode('center', 0, 0),
453
- makeNode('far-left', -200, 10), // -dx = 200, penalty = 5 -> 195
454
- makeNode('slight-right', 50, -10), // -dx = -50, penalty = 5 -> -55
455
- ];
456
- const adj = new Map<string, Set<string>>();
457
- adj.set('center', new Set(['far-left', 'slight-right']));
458
- adj.set('far-left', new Set(['center']));
459
- adj.set('slight-right', new Set(['center']));
460
-
461
- const {
462
- options,
463
- callbacks: cbs,
464
- canvas: c,
465
- } = createOptions({
466
- getNodes: () => nodes,
467
- getAdjacency: () => adj,
468
- });
469
- const cl = attachGraphKeyboardNav(options);
470
-
471
- keydown(c, 'Tab');
472
- keydown(c, 'ArrowLeft');
473
- expect(cbs.onSelect).toHaveBeenCalledWith('far-left');
474
-
475
- cl();
476
- });
477
-
478
- it('down prefers positive dy', () => {
479
- const nodes = [
480
- makeNode('center', 0, 0),
481
- makeNode('below', 20, 150), // dy=150, penalty for dx=10 -> 140
482
- makeNode('above', -10, -100), // dy=-100, penalty for dx=5 -> -105
483
- ];
484
- const adj = new Map<string, Set<string>>();
485
- adj.set('center', new Set(['below', 'above']));
486
- adj.set('below', new Set(['center']));
487
- adj.set('above', new Set(['center']));
488
-
489
- const {
490
- options,
491
- callbacks: cbs,
492
- canvas: c,
493
- } = createOptions({
494
- getNodes: () => nodes,
495
- getAdjacency: () => adj,
496
- });
497
- const cl = attachGraphKeyboardNav(options);
498
-
499
- keydown(c, 'Tab');
500
- keydown(c, 'ArrowDown');
501
- expect(cbs.onSelect).toHaveBeenCalledWith('below');
502
-
503
- cl();
504
- });
505
-
506
- it('up prefers negative dy', () => {
507
- const nodes = [
508
- makeNode('center', 0, 0),
509
- makeNode('above', 15, -200), // -dy=200, penalty for dx=7.5 -> 192.5
510
- makeNode('below', -5, 80), // -dy=-80, penalty for dx=2.5 -> -82.5
511
- ];
512
- const adj = new Map<string, Set<string>>();
513
- adj.set('center', new Set(['above', 'below']));
514
- adj.set('above', new Set(['center']));
515
- adj.set('below', new Set(['center']));
516
-
517
- const {
518
- options,
519
- callbacks: cbs,
520
- canvas: c,
521
- } = createOptions({
522
- getNodes: () => nodes,
523
- getAdjacency: () => adj,
524
- });
525
- const cl = attachGraphKeyboardNav(options);
526
-
527
- keydown(c, 'Tab');
528
- keydown(c, 'ArrowUp');
529
- expect(cbs.onSelect).toHaveBeenCalledWith('above');
530
-
531
- cl();
532
- });
533
-
534
- it('picks closest match when two neighbors are in the same direction', () => {
535
- // Two nodes to the right, one closer and better aligned
536
- const nodes = [
537
- makeNode('center', 0, 0),
538
- makeNode('near-right', 100, 0), // dx=100, dy=0 -> score 100
539
- makeNode('far-right', 300, 50), // dx=300, dy=50 -> score 300 - 25 = 275
540
- ];
541
- const adj = new Map<string, Set<string>>();
542
- adj.set('center', new Set(['near-right', 'far-right']));
543
- adj.set('near-right', new Set(['center']));
544
- adj.set('far-right', new Set(['center']));
545
-
546
- const {
547
- options,
548
- callbacks: cbs,
549
- canvas: c,
550
- } = createOptions({
551
- getNodes: () => nodes,
552
- getAdjacency: () => adj,
553
- });
554
- const cl = attachGraphKeyboardNav(options);
555
-
556
- keydown(c, 'Tab');
557
- keydown(c, 'ArrowRight');
558
- // far-right has higher score (275 > 100) due to larger dx
559
- expect(cbs.onSelect).toHaveBeenCalledWith('far-right');
560
-
561
- cl();
562
- });
563
-
564
- it('penalizes perpendicular offset in scoring', () => {
565
- // Two candidates to the right: one perfectly aligned, one with big y offset
566
- const nodes = [
567
- makeNode('center', 0, 0),
568
- makeNode('aligned', 80, 0), // dx=80, dy=0 -> score 80
569
- makeNode('offset', 100, 200), // dx=100, dy=200 -> score 100 - 100 = 0
570
- ];
571
- const adj = new Map<string, Set<string>>();
572
- adj.set('center', new Set(['aligned', 'offset']));
573
- adj.set('aligned', new Set(['center']));
574
- adj.set('offset', new Set(['center']));
575
-
576
- const {
577
- options,
578
- callbacks: cbs,
579
- canvas: c,
580
- } = createOptions({
581
- getNodes: () => nodes,
582
- getAdjacency: () => adj,
583
- });
584
- const cl = attachGraphKeyboardNav(options);
585
-
586
- keydown(c, 'Tab');
587
- keydown(c, 'ArrowRight');
588
- // aligned (80) beats offset (0) because the perpendicular penalty kicks in
589
- expect(cbs.onSelect).toHaveBeenCalledWith('aligned');
590
-
591
- cl();
592
- });
593
- });
594
-
595
- // -----------------------------------------------------------------------
596
- // Edge cases
597
- // -----------------------------------------------------------------------
598
-
599
- describe('edge cases', () => {
600
- it('Tab keeps focus if the focused node still exists', () => {
601
- // Tab once to focus center
602
- keydown(canvas, 'Tab');
603
- // Tab again -- focusedNodeId is still valid, should keep it
604
- keydown(canvas, 'Tab');
605
- // Arrow right from center should still go to right
606
- keydown(canvas, 'ArrowRight');
607
- expect(callbacks.onSelect).toHaveBeenCalledWith('right');
608
- });
609
-
610
- it('Tab refocuses to first node if focused node was removed', () => {
611
- const mutableNodes = [...makeCrossNodes()];
612
- const {
613
- options,
614
- callbacks: cbs,
615
- canvas: c,
616
- } = createOptions({
617
- getNodes: () => mutableNodes,
618
- });
619
- const cl = attachGraphKeyboardNav(options);
620
-
621
- // Tab to focus center (first node)
622
- keydown(c, 'Tab');
623
- // Navigate right to focus on 'right'
624
- keydown(c, 'ArrowRight');
625
- expect(cbs.onSelect).toHaveBeenCalledWith('right');
626
-
627
- // Remove the 'right' node from the list
628
- const rightIdx = mutableNodes.findIndex((n) => n.id === 'right');
629
- mutableNodes.splice(rightIdx, 1);
630
-
631
- // Tab again -- focused node 'right' no longer exists via findNodeById,
632
- // so Tab falls through to refocus on nodes[0] which is 'center'
633
- keydown(c, 'Tab');
634
-
635
- // Enter to confirm we're focused on center
636
- keydown(c, 'Enter');
637
- expect(cbs.onSelect).toHaveBeenLastCalledWith('center');
638
-
639
- cl();
640
- });
641
-
642
- it('sequential navigation updates focus correctly', () => {
643
- // Tab to center, then right, then the focus should be on 'right'
644
- keydown(canvas, 'Tab');
645
- keydown(canvas, 'ArrowRight');
646
- expect(callbacks.onSelect).toHaveBeenCalledWith('right');
647
-
648
- // From 'right', the only neighbor is 'center'
649
- keydown(canvas, 'ArrowLeft');
650
- expect(callbacks.onSelect).toHaveBeenCalledWith('center');
651
- });
652
- });
653
- });