@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,359 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Graph interaction manager.
|
|
3
|
+
*
|
|
4
|
+
* Handles mouse/touch events on the canvas and translates them into
|
|
5
|
+
* high-level graph interactions: pan, zoom, hover, select, drag nodes.
|
|
6
|
+
* Uses the spatial index for hit testing and ZoomTransform for coordinate
|
|
7
|
+
* conversion.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { SpatialIndex } from './spatial-index';
|
|
11
|
+
import { ZoomTransform } from './zoom';
|
|
12
|
+
|
|
13
|
+
// ---------------------------------------------------------------------------
|
|
14
|
+
// Constants
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
|
|
17
|
+
const ZOOM_MIN = 0.05;
|
|
18
|
+
const ZOOM_MAX = 15;
|
|
19
|
+
const ZOOM_STEP = -0.001;
|
|
20
|
+
const HIT_DISTANCE = 5;
|
|
21
|
+
|
|
22
|
+
// ---------------------------------------------------------------------------
|
|
23
|
+
// Callback interface
|
|
24
|
+
// ---------------------------------------------------------------------------
|
|
25
|
+
|
|
26
|
+
export interface InteractionCallbacks {
|
|
27
|
+
onTransformChange(transform: ZoomTransform): void;
|
|
28
|
+
onHoverChange(nodeId: string | null): void;
|
|
29
|
+
onSelectionChange(nodeIds: string[]): void;
|
|
30
|
+
onNodeDragStart(nodeId: string): void;
|
|
31
|
+
onNodeDrag(nodeId: string, x: number, y: number): void;
|
|
32
|
+
onNodeDragEnd(nodeId: string): void;
|
|
33
|
+
onDoubleClick(nodeId: string): void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// ---------------------------------------------------------------------------
|
|
37
|
+
// Internal state
|
|
38
|
+
// ---------------------------------------------------------------------------
|
|
39
|
+
|
|
40
|
+
interface DragState {
|
|
41
|
+
nodeId: string;
|
|
42
|
+
started: boolean;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface PanState {
|
|
46
|
+
startX: number;
|
|
47
|
+
startY: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// ---------------------------------------------------------------------------
|
|
51
|
+
// GraphInteractionManager
|
|
52
|
+
// ---------------------------------------------------------------------------
|
|
53
|
+
|
|
54
|
+
export class GraphInteractionManager {
|
|
55
|
+
private canvas: HTMLCanvasElement;
|
|
56
|
+
private spatialIndex: SpatialIndex;
|
|
57
|
+
private callbacks: InteractionCallbacks;
|
|
58
|
+
private transform = ZoomTransform.identity();
|
|
59
|
+
|
|
60
|
+
private dragState: DragState | null = null;
|
|
61
|
+
private panState: PanState | null = null;
|
|
62
|
+
private mousedownNodeId: string | null = null;
|
|
63
|
+
private selectedIds: Set<string> = new Set();
|
|
64
|
+
|
|
65
|
+
// Touch state
|
|
66
|
+
private lastTouchDist: number | null = null;
|
|
67
|
+
private lastTouchCenter: { x: number; y: number } | null = null;
|
|
68
|
+
|
|
69
|
+
// Bound handlers for cleanup
|
|
70
|
+
private boundWheel: (e: WheelEvent) => void;
|
|
71
|
+
private boundMouseDown: (e: MouseEvent) => void;
|
|
72
|
+
private boundMouseMove: (e: MouseEvent) => void;
|
|
73
|
+
private boundMouseUp: (e: MouseEvent) => void;
|
|
74
|
+
private boundDblClick: (e: MouseEvent) => void;
|
|
75
|
+
private boundTouchStart: (e: TouchEvent) => void;
|
|
76
|
+
private boundTouchMove: (e: TouchEvent) => void;
|
|
77
|
+
private boundTouchEnd: (e: TouchEvent) => void;
|
|
78
|
+
private boundMouseLeave: (e: MouseEvent) => void;
|
|
79
|
+
|
|
80
|
+
constructor(
|
|
81
|
+
canvas: HTMLCanvasElement,
|
|
82
|
+
spatialIndex: SpatialIndex,
|
|
83
|
+
callbacks: InteractionCallbacks,
|
|
84
|
+
) {
|
|
85
|
+
this.canvas = canvas;
|
|
86
|
+
this.spatialIndex = spatialIndex;
|
|
87
|
+
this.callbacks = callbacks;
|
|
88
|
+
|
|
89
|
+
// Bind handlers
|
|
90
|
+
this.boundWheel = this.onWheel.bind(this);
|
|
91
|
+
this.boundMouseDown = this.onMouseDown.bind(this);
|
|
92
|
+
this.boundMouseMove = this.onMouseMove.bind(this);
|
|
93
|
+
this.boundMouseUp = this.onMouseUp.bind(this);
|
|
94
|
+
this.boundMouseLeave = this.onMouseLeave.bind(this);
|
|
95
|
+
this.boundDblClick = this.onDblClick.bind(this);
|
|
96
|
+
this.boundTouchStart = this.onTouchStart.bind(this);
|
|
97
|
+
this.boundTouchMove = this.onTouchMove.bind(this);
|
|
98
|
+
this.boundTouchEnd = this.onTouchEnd.bind(this);
|
|
99
|
+
|
|
100
|
+
// Attach event listeners
|
|
101
|
+
canvas.addEventListener('wheel', this.boundWheel, { passive: false });
|
|
102
|
+
canvas.addEventListener('mousedown', this.boundMouseDown);
|
|
103
|
+
canvas.addEventListener('mousemove', this.boundMouseMove);
|
|
104
|
+
canvas.addEventListener('mouseup', this.boundMouseUp);
|
|
105
|
+
canvas.addEventListener('mouseleave', this.boundMouseLeave);
|
|
106
|
+
canvas.addEventListener('dblclick', this.boundDblClick);
|
|
107
|
+
canvas.addEventListener('touchstart', this.boundTouchStart, {
|
|
108
|
+
passive: false,
|
|
109
|
+
});
|
|
110
|
+
canvas.addEventListener('touchmove', this.boundTouchMove, {
|
|
111
|
+
passive: false,
|
|
112
|
+
});
|
|
113
|
+
canvas.addEventListener('touchend', this.boundTouchEnd);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
setTransform(transform: ZoomTransform): void {
|
|
117
|
+
this.transform = transform;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
getTransform(): ZoomTransform {
|
|
121
|
+
return this.transform;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
destroy(): void {
|
|
125
|
+
this.canvas.removeEventListener('wheel', this.boundWheel);
|
|
126
|
+
this.canvas.removeEventListener('mousedown', this.boundMouseDown);
|
|
127
|
+
this.canvas.removeEventListener('mousemove', this.boundMouseMove);
|
|
128
|
+
this.canvas.removeEventListener('mouseup', this.boundMouseUp);
|
|
129
|
+
this.canvas.removeEventListener('mouseleave', this.boundMouseLeave);
|
|
130
|
+
this.canvas.removeEventListener('dblclick', this.boundDblClick);
|
|
131
|
+
this.canvas.removeEventListener('touchstart', this.boundTouchStart);
|
|
132
|
+
this.canvas.removeEventListener('touchmove', this.boundTouchMove);
|
|
133
|
+
this.canvas.removeEventListener('touchend', this.boundTouchEnd);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// -------------------------------------------------------------------------
|
|
137
|
+
// Mouse handlers
|
|
138
|
+
// -------------------------------------------------------------------------
|
|
139
|
+
|
|
140
|
+
private canvasXY(e: MouseEvent): { x: number; y: number } {
|
|
141
|
+
const rect = this.canvas.getBoundingClientRect();
|
|
142
|
+
return { x: e.clientX - rect.left, y: e.clientY - rect.top };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
private hitTest(screenX: number, screenY: number): string | null {
|
|
146
|
+
const graph = this.transform.screenToGraph(screenX, screenY);
|
|
147
|
+
const node = this.spatialIndex.findNearest(graph.x, graph.y, HIT_DISTANCE / this.transform.k);
|
|
148
|
+
return node?.id ?? null;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
private onWheel(e: WheelEvent): void {
|
|
152
|
+
e.preventDefault();
|
|
153
|
+
const { x, y } = this.canvasXY(e);
|
|
154
|
+
const factor = e.deltaY * ZOOM_STEP;
|
|
155
|
+
const newK = Math.max(ZOOM_MIN, Math.min(ZOOM_MAX, this.transform.k * (1 + factor)));
|
|
156
|
+
this.transform = this.transform.zoomAt(newK, x, y);
|
|
157
|
+
this.callbacks.onTransformChange(this.transform);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
private onMouseDown(e: MouseEvent): void {
|
|
161
|
+
const { x, y } = this.canvasXY(e);
|
|
162
|
+
const hitId = this.hitTest(x, y);
|
|
163
|
+
|
|
164
|
+
if (hitId) {
|
|
165
|
+
// Start potential node drag
|
|
166
|
+
this.dragState = { nodeId: hitId, started: false };
|
|
167
|
+
this.mousedownNodeId = hitId;
|
|
168
|
+
} else {
|
|
169
|
+
// Start pan
|
|
170
|
+
this.panState = { startX: x, startY: y };
|
|
171
|
+
this.mousedownNodeId = null;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
private onMouseMove(e: MouseEvent): void {
|
|
176
|
+
const { x, y } = this.canvasXY(e);
|
|
177
|
+
|
|
178
|
+
if (this.dragState) {
|
|
179
|
+
const graph = this.transform.screenToGraph(x, y);
|
|
180
|
+
if (!this.dragState.started) {
|
|
181
|
+
this.dragState.started = true;
|
|
182
|
+
this.callbacks.onNodeDragStart(this.dragState.nodeId);
|
|
183
|
+
}
|
|
184
|
+
this.callbacks.onNodeDrag(this.dragState.nodeId, graph.x, graph.y);
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (this.panState) {
|
|
189
|
+
const dx = x - this.panState.startX;
|
|
190
|
+
const dy = y - this.panState.startY;
|
|
191
|
+
this.transform = this.transform.pan(dx, dy);
|
|
192
|
+
this.panState = { startX: x, startY: y };
|
|
193
|
+
this.callbacks.onTransformChange(this.transform);
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Hover detection
|
|
198
|
+
const hitId = this.hitTest(x, y);
|
|
199
|
+
this.callbacks.onHoverChange(hitId);
|
|
200
|
+
|
|
201
|
+
// Update cursor
|
|
202
|
+
this.canvas.style.cursor = hitId ? 'pointer' : 'default';
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
private onMouseUp(e: MouseEvent): void {
|
|
206
|
+
const { x, y } = this.canvasXY(e);
|
|
207
|
+
|
|
208
|
+
if (this.dragState) {
|
|
209
|
+
if (this.dragState.started) {
|
|
210
|
+
this.callbacks.onNodeDragEnd(this.dragState.nodeId);
|
|
211
|
+
} else {
|
|
212
|
+
// Was a click on a node (no drag movement)
|
|
213
|
+
this.handleNodeClick(this.dragState.nodeId, e.shiftKey);
|
|
214
|
+
}
|
|
215
|
+
this.dragState = null;
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (this.panState) {
|
|
220
|
+
this.panState = null;
|
|
221
|
+
|
|
222
|
+
// If mouse up is on background (no node), treat as background click
|
|
223
|
+
if (!this.mousedownNodeId) {
|
|
224
|
+
const hitId = this.hitTest(x, y);
|
|
225
|
+
if (!hitId) {
|
|
226
|
+
// Background click: clear selection
|
|
227
|
+
this.selectedIds.clear();
|
|
228
|
+
this.callbacks.onSelectionChange([]);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
private onDblClick(e: MouseEvent): void {
|
|
236
|
+
const { x, y } = this.canvasXY(e);
|
|
237
|
+
const hitId = this.hitTest(x, y);
|
|
238
|
+
if (hitId) {
|
|
239
|
+
this.callbacks.onDoubleClick(hitId);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
private onMouseLeave(_e: MouseEvent): void {
|
|
244
|
+
this.callbacks.onHoverChange(null);
|
|
245
|
+
this.canvas.style.cursor = 'default';
|
|
246
|
+
|
|
247
|
+
// Cancel any in-progress pan
|
|
248
|
+
if (this.panState) {
|
|
249
|
+
this.panState = null;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
private handleNodeClick(nodeId: string, shiftKey: boolean): void {
|
|
254
|
+
if (shiftKey) {
|
|
255
|
+
// Toggle node in multi-select
|
|
256
|
+
if (this.selectedIds.has(nodeId)) {
|
|
257
|
+
this.selectedIds.delete(nodeId);
|
|
258
|
+
} else {
|
|
259
|
+
this.selectedIds.add(nodeId);
|
|
260
|
+
}
|
|
261
|
+
} else {
|
|
262
|
+
// Single select
|
|
263
|
+
this.selectedIds.clear();
|
|
264
|
+
this.selectedIds.add(nodeId);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
this.callbacks.onSelectionChange([...this.selectedIds]);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// -------------------------------------------------------------------------
|
|
271
|
+
// Touch handlers
|
|
272
|
+
// -------------------------------------------------------------------------
|
|
273
|
+
|
|
274
|
+
private onTouchStart(e: TouchEvent): void {
|
|
275
|
+
e.preventDefault();
|
|
276
|
+
|
|
277
|
+
if (e.touches.length === 2) {
|
|
278
|
+
// Pinch-zoom start
|
|
279
|
+
const [t0, t1] = [e.touches[0], e.touches[1]];
|
|
280
|
+
this.lastTouchDist = Math.hypot(t1.clientX - t0.clientX, t1.clientY - t0.clientY);
|
|
281
|
+
this.lastTouchCenter = {
|
|
282
|
+
x: (t0.clientX + t1.clientX) / 2,
|
|
283
|
+
y: (t0.clientY + t1.clientY) / 2,
|
|
284
|
+
};
|
|
285
|
+
} else if (e.touches.length === 1) {
|
|
286
|
+
const touch = e.touches[0];
|
|
287
|
+
const rect = this.canvas.getBoundingClientRect();
|
|
288
|
+
const x = touch.clientX - rect.left;
|
|
289
|
+
const y = touch.clientY - rect.top;
|
|
290
|
+
|
|
291
|
+
const hitId = this.hitTest(x, y);
|
|
292
|
+
if (hitId) {
|
|
293
|
+
this.mousedownNodeId = hitId;
|
|
294
|
+
} else {
|
|
295
|
+
this.panState = { startX: x, startY: y };
|
|
296
|
+
this.mousedownNodeId = null;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
private onTouchMove(e: TouchEvent): void {
|
|
302
|
+
e.preventDefault();
|
|
303
|
+
|
|
304
|
+
if (e.touches.length === 2 && this.lastTouchDist !== null) {
|
|
305
|
+
const [t0, t1] = [e.touches[0], e.touches[1]];
|
|
306
|
+
const newDist = Math.hypot(t1.clientX - t0.clientX, t1.clientY - t0.clientY);
|
|
307
|
+
const rect = this.canvas.getBoundingClientRect();
|
|
308
|
+
const centerX = (t0.clientX + t1.clientX) / 2 - rect.left;
|
|
309
|
+
const centerY = (t0.clientY + t1.clientY) / 2 - rect.top;
|
|
310
|
+
|
|
311
|
+
const scale = newDist / this.lastTouchDist;
|
|
312
|
+
const newK = Math.max(ZOOM_MIN, Math.min(ZOOM_MAX, this.transform.k * scale));
|
|
313
|
+
this.transform = this.transform.zoomAt(newK, centerX, centerY);
|
|
314
|
+
|
|
315
|
+
// Pan from center movement
|
|
316
|
+
if (this.lastTouchCenter) {
|
|
317
|
+
const dx = centerX - (this.lastTouchCenter.x - rect.left);
|
|
318
|
+
const dy = centerY - (this.lastTouchCenter.y - rect.top);
|
|
319
|
+
this.transform = this.transform.pan(dx, dy);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
this.lastTouchDist = newDist;
|
|
323
|
+
this.lastTouchCenter = {
|
|
324
|
+
x: (t0.clientX + t1.clientX) / 2,
|
|
325
|
+
y: (t0.clientY + t1.clientY) / 2,
|
|
326
|
+
};
|
|
327
|
+
this.callbacks.onTransformChange(this.transform);
|
|
328
|
+
} else if (e.touches.length === 1 && this.panState) {
|
|
329
|
+
const touch = e.touches[0];
|
|
330
|
+
const rect = this.canvas.getBoundingClientRect();
|
|
331
|
+
const x = touch.clientX - rect.left;
|
|
332
|
+
const y = touch.clientY - rect.top;
|
|
333
|
+
|
|
334
|
+
const dx = x - this.panState.startX;
|
|
335
|
+
const dy = y - this.panState.startY;
|
|
336
|
+
this.transform = this.transform.pan(dx, dy);
|
|
337
|
+
this.panState = { startX: x, startY: y };
|
|
338
|
+
this.callbacks.onTransformChange(this.transform);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
private onTouchEnd(e: TouchEvent): void {
|
|
343
|
+
if (e.touches.length === 0) {
|
|
344
|
+
// Tap-select
|
|
345
|
+
if (this.mousedownNodeId && !this.panState) {
|
|
346
|
+
this.handleNodeClick(this.mousedownNodeId, false);
|
|
347
|
+
} else if (!this.mousedownNodeId && this.panState) {
|
|
348
|
+
// Background tap: clear selection
|
|
349
|
+
this.selectedIds.clear();
|
|
350
|
+
this.callbacks.onSelectionChange([]);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
this.panState = null;
|
|
354
|
+
this.mousedownNodeId = null;
|
|
355
|
+
this.lastTouchDist = null;
|
|
356
|
+
this.lastTouchCenter = null;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keyboard navigation for the graph canvas.
|
|
3
|
+
*
|
|
4
|
+
* Provides accessible keyboard control: Tab to focus, arrow keys to
|
|
5
|
+
* navigate between adjacent nodes (following edges), Enter to select,
|
|
6
|
+
* Escape to clear, +/- to zoom, Home to fit all, / to focus search.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { PositionedNode } from './types';
|
|
10
|
+
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
// Options
|
|
13
|
+
// ---------------------------------------------------------------------------
|
|
14
|
+
|
|
15
|
+
export interface KeyboardNavOptions {
|
|
16
|
+
canvas: HTMLCanvasElement;
|
|
17
|
+
getNodes(): PositionedNode[];
|
|
18
|
+
getSelectedIds(): string[];
|
|
19
|
+
getAdjacency(): Map<string, Set<string>>;
|
|
20
|
+
onSelect(nodeId: string): void;
|
|
21
|
+
onDeselect(): void;
|
|
22
|
+
onZoom(direction: 'in' | 'out'): void;
|
|
23
|
+
onFitAll(): void;
|
|
24
|
+
onFocusSearch?(): void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
// Implementation
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Attach keyboard navigation to a graph canvas.
|
|
33
|
+
* Returns a cleanup function that removes all listeners.
|
|
34
|
+
*/
|
|
35
|
+
export function attachGraphKeyboardNav(options: KeyboardNavOptions): () => void {
|
|
36
|
+
const {
|
|
37
|
+
canvas,
|
|
38
|
+
getNodes,
|
|
39
|
+
getSelectedIds,
|
|
40
|
+
getAdjacency,
|
|
41
|
+
onSelect,
|
|
42
|
+
onDeselect,
|
|
43
|
+
onZoom,
|
|
44
|
+
onFitAll,
|
|
45
|
+
onFocusSearch,
|
|
46
|
+
} = options;
|
|
47
|
+
|
|
48
|
+
let focusedNodeId: string | null = null;
|
|
49
|
+
|
|
50
|
+
// Make canvas focusable
|
|
51
|
+
if (!canvas.hasAttribute('tabindex')) {
|
|
52
|
+
canvas.setAttribute('tabindex', '0');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function findNodeById(id: string): PositionedNode | undefined {
|
|
56
|
+
return getNodes().find((n) => n.id === id);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Given a set of neighbor node ids, pick the one that best matches
|
|
61
|
+
* the arrow key direction relative to the current focused node.
|
|
62
|
+
*/
|
|
63
|
+
function pickDirectionalNeighbor(
|
|
64
|
+
fromNode: PositionedNode,
|
|
65
|
+
neighborIds: Set<string>,
|
|
66
|
+
direction: 'up' | 'down' | 'left' | 'right',
|
|
67
|
+
): string | null {
|
|
68
|
+
const nodes = getNodes();
|
|
69
|
+
const candidates = nodes.filter((n) => neighborIds.has(n.id));
|
|
70
|
+
if (candidates.length === 0) return null;
|
|
71
|
+
|
|
72
|
+
// Score each candidate by how well it matches the desired direction
|
|
73
|
+
let best: PositionedNode | null = null;
|
|
74
|
+
let bestScore = -Infinity;
|
|
75
|
+
|
|
76
|
+
for (const c of candidates) {
|
|
77
|
+
const dx = c.x - fromNode.x;
|
|
78
|
+
const dy = c.y - fromNode.y;
|
|
79
|
+
let score: number;
|
|
80
|
+
|
|
81
|
+
switch (direction) {
|
|
82
|
+
case 'right':
|
|
83
|
+
score = dx - Math.abs(dy) * 0.5;
|
|
84
|
+
break;
|
|
85
|
+
case 'left':
|
|
86
|
+
score = -dx - Math.abs(dy) * 0.5;
|
|
87
|
+
break;
|
|
88
|
+
case 'down':
|
|
89
|
+
score = dy - Math.abs(dx) * 0.5;
|
|
90
|
+
break;
|
|
91
|
+
case 'up':
|
|
92
|
+
score = -dy - Math.abs(dx) * 0.5;
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (score > bestScore) {
|
|
97
|
+
bestScore = score;
|
|
98
|
+
best = c;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return best?.id ?? null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function onKeyDown(e: KeyboardEvent): void {
|
|
106
|
+
switch (e.key) {
|
|
107
|
+
case 'Tab': {
|
|
108
|
+
// Focus first/selected node
|
|
109
|
+
const selected = getSelectedIds();
|
|
110
|
+
const nodes = getNodes();
|
|
111
|
+
if (nodes.length === 0) return;
|
|
112
|
+
|
|
113
|
+
if (selected.length > 0) {
|
|
114
|
+
focusedNodeId = selected[0];
|
|
115
|
+
} else if (!focusedNodeId || !findNodeById(focusedNodeId)) {
|
|
116
|
+
focusedNodeId = nodes[0].id;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
e.preventDefault();
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
case 'ArrowUp':
|
|
124
|
+
case 'ArrowDown':
|
|
125
|
+
case 'ArrowLeft':
|
|
126
|
+
case 'ArrowRight': {
|
|
127
|
+
if (!focusedNodeId) return;
|
|
128
|
+
e.preventDefault();
|
|
129
|
+
|
|
130
|
+
const focusedNode = findNodeById(focusedNodeId);
|
|
131
|
+
if (!focusedNode) return;
|
|
132
|
+
|
|
133
|
+
const adjacency = getAdjacency();
|
|
134
|
+
const neighbors = adjacency.get(focusedNodeId);
|
|
135
|
+
if (!neighbors || neighbors.size === 0) return;
|
|
136
|
+
|
|
137
|
+
const dirMap: Record<string, 'up' | 'down' | 'left' | 'right'> = {
|
|
138
|
+
ArrowUp: 'up',
|
|
139
|
+
ArrowDown: 'down',
|
|
140
|
+
ArrowLeft: 'left',
|
|
141
|
+
ArrowRight: 'right',
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const nextId = pickDirectionalNeighbor(focusedNode, neighbors, dirMap[e.key]);
|
|
145
|
+
if (nextId) {
|
|
146
|
+
focusedNodeId = nextId;
|
|
147
|
+
onSelect(nextId);
|
|
148
|
+
}
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
case 'Enter': {
|
|
153
|
+
if (focusedNodeId) {
|
|
154
|
+
e.preventDefault();
|
|
155
|
+
const selected = getSelectedIds();
|
|
156
|
+
if (selected.includes(focusedNodeId)) {
|
|
157
|
+
onDeselect();
|
|
158
|
+
} else {
|
|
159
|
+
onSelect(focusedNodeId);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
case 'Escape': {
|
|
166
|
+
e.preventDefault();
|
|
167
|
+
focusedNodeId = null;
|
|
168
|
+
onDeselect();
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
case '+':
|
|
173
|
+
case '=': {
|
|
174
|
+
e.preventDefault();
|
|
175
|
+
onZoom('in');
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
case '-':
|
|
180
|
+
case '_': {
|
|
181
|
+
e.preventDefault();
|
|
182
|
+
onZoom('out');
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
case 'Home': {
|
|
187
|
+
e.preventDefault();
|
|
188
|
+
onFitAll();
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
case '/': {
|
|
193
|
+
if (onFocusSearch) {
|
|
194
|
+
e.preventDefault();
|
|
195
|
+
onFocusSearch();
|
|
196
|
+
}
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
canvas.addEventListener('keydown', onKeyDown);
|
|
203
|
+
|
|
204
|
+
// Return cleanup function
|
|
205
|
+
return () => {
|
|
206
|
+
canvas.removeEventListener('keydown', onKeyDown);
|
|
207
|
+
};
|
|
208
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Graph search manager.
|
|
3
|
+
*
|
|
4
|
+
* Provides case-insensitive substring matching against node labels/ids.
|
|
5
|
+
* Returns a Set of matching node ids that the renderer uses to dim
|
|
6
|
+
* non-matching nodes and edges.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export class GraphSearchManager {
|
|
10
|
+
private matchedIds: Set<string> | null = null;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Search for nodes matching the query string.
|
|
14
|
+
* Returns a Set of matching node ids, or an empty set if nothing matches.
|
|
15
|
+
*/
|
|
16
|
+
search(query: string, nodes: Array<{ id: string; label?: string }>): Set<string> {
|
|
17
|
+
const q = query.toLowerCase().trim();
|
|
18
|
+
|
|
19
|
+
if (q === '') {
|
|
20
|
+
this.matchedIds = null;
|
|
21
|
+
return new Set();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const matches = new Set<string>();
|
|
25
|
+
for (const node of nodes) {
|
|
26
|
+
const label = (node.label ?? '').toLowerCase();
|
|
27
|
+
const id = node.id.toLowerCase();
|
|
28
|
+
if (label.includes(q) || id.includes(q)) {
|
|
29
|
+
matches.add(node.id);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
this.matchedIds = matches;
|
|
34
|
+
return matches;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Clear the current search.
|
|
39
|
+
* Returns null to indicate no active search.
|
|
40
|
+
*/
|
|
41
|
+
clearSearch(): Set<string> | null {
|
|
42
|
+
this.matchedIds = null;
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Get the current set of matched ids, or null if no search is active. */
|
|
47
|
+
getMatches(): Set<string> | null {
|
|
48
|
+
return this.matchedIds;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a Web Worker running the force simulation.
|
|
3
|
+
*
|
|
4
|
+
* Uses the `new URL` + `import.meta.url` pattern recognized by Vite, Webpack 5,
|
|
5
|
+
* Parcel, and esbuild. The bundler resolves the worker file path at build time
|
|
6
|
+
* and handles the asset accordingly.
|
|
7
|
+
*
|
|
8
|
+
* - Vite dev (Ladle): resolves src/graph/simulation-worker.ts directly, serves
|
|
9
|
+
* it as a native ES module worker with on-the-fly TypeScript transform.
|
|
10
|
+
* - Production (tsup + bun build): dist/simulation-worker.js is a self-contained
|
|
11
|
+
* IIFE produced by `bun build`. The consuming app's bundler copies it as an
|
|
12
|
+
* asset and rewrites the URL.
|
|
13
|
+
*
|
|
14
|
+
* Usage:
|
|
15
|
+
* import { createSimulationWorker } from '@opendata-ai/openchart-vanilla';
|
|
16
|
+
* const worker = createSimulationWorker();
|
|
17
|
+
* worker.postMessage({ type: 'init', nodes, links, width: 800, height: 600 });
|
|
18
|
+
* worker.onmessage = (e) => console.log(e.data);
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Path that resolves in Vite dev (workspace source) to the .ts file.
|
|
23
|
+
* In production dist/, the consuming bundler resolves to simulation-worker.js
|
|
24
|
+
* which sits alongside index.js in the dist folder.
|
|
25
|
+
*/
|
|
26
|
+
const workerUrl = new URL('./simulation-worker.ts', import.meta.url);
|
|
27
|
+
|
|
28
|
+
export function createSimulationWorker(): Worker {
|
|
29
|
+
return new Worker(workerUrl, { type: 'module' });
|
|
30
|
+
}
|