@opendata-ai/openchart-vanilla 2.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.
- package/dist/index.d.ts +327 -0
- package/dist/index.js +4745 -0
- package/dist/index.js.map +1 -0
- package/dist/simulation-worker.js +1196 -0
- package/package.json +58 -0
- package/src/__test-fixtures__/dom.ts +42 -0
- package/src/__test-fixtures__/specs.ts +187 -0
- package/src/__tests__/edit-events.test.ts +747 -0
- package/src/__tests__/events.test.ts +336 -0
- package/src/__tests__/export.test.ts +150 -0
- package/src/__tests__/mount.test.ts +219 -0
- package/src/__tests__/svg-renderer.test.ts +609 -0
- package/src/__tests__/table-mount.test.ts +484 -0
- package/src/__tests__/tooltip.test.ts +201 -0
- package/src/export.ts +105 -0
- package/src/graph/__tests__/canvas-renderer.test.ts +704 -0
- package/src/graph/__tests__/graph-mount.test.ts +213 -0
- package/src/graph/__tests__/interaction.test.ts +205 -0
- package/src/graph/__tests__/keyboard.test.ts +653 -0
- package/src/graph/__tests__/search.test.ts +88 -0
- package/src/graph/__tests__/simulation.test.ts +233 -0
- package/src/graph/__tests__/spatial-index.test.ts +142 -0
- package/src/graph/__tests__/zoom.test.ts +195 -0
- package/src/graph/canvas-renderer.ts +660 -0
- package/src/graph/interaction.ts +359 -0
- package/src/graph/keyboard.ts +208 -0
- package/src/graph/search.ts +50 -0
- package/src/graph/simulation-worker-url.ts +30 -0
- package/src/graph/simulation-worker.ts +265 -0
- package/src/graph/simulation.ts +350 -0
- package/src/graph/spatial-index.ts +121 -0
- package/src/graph/types.ts +44 -0
- package/src/graph/worker-protocol.ts +67 -0
- package/src/graph/zoom.ts +104 -0
- package/src/graph-mount.ts +675 -0
- package/src/index.ts +56 -0
- package/src/mount.ts +1639 -0
- package/src/renderers/table-cells.ts +444 -0
- package/src/resize-observer.ts +46 -0
- package/src/svg-renderer.ts +914 -0
- package/src/table-keyboard.ts +266 -0
- package/src/table-mount.ts +532 -0
- package/src/table-renderer.ts +350 -0
- package/src/tooltip.ts +120 -0
|
@@ -0,0 +1,653 @@
|
|
|
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
|
+
});
|