@opendata-ai/openchart-vanilla 8.0.0-rc.2 → 8.0.0-rc.21

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 (145) hide show
  1. package/dist/{chunk-W34KN7FD.js → chunk-2NAQRP66.js} +2 -2
  2. package/dist/chunk-DCODCLWI.js +234 -0
  3. package/dist/chunk-DCODCLWI.js.map +1 -0
  4. package/dist/{chunk-5EOV5JQZ.js → chunk-EKAUQAAX.js} +50 -46
  5. package/dist/chunk-EKAUQAAX.js.map +1 -0
  6. package/dist/{chunk-ENXSUXC5.js → chunk-KNT5AZ2O.js} +7309 -5490
  7. package/dist/chunk-KNT5AZ2O.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 +283 -24
  16. package/dist/index.js +2012 -763
  17. package/dist/index.js.map +1 -1
  18. package/dist/{mount-CUKQLeqD.d.ts → mount-Dvv8NYIv.d.ts} +117 -3
  19. package/dist/simulation-worker.js +83 -2
  20. package/dist/static.js +7 -69
  21. package/dist/static.js.map +1 -1
  22. package/dist/story/index.d.ts +3 -2
  23. package/dist/story/index.js +5 -3
  24. package/dist/story/index.js.map +1 -1
  25. package/dist/styles.css +1 -1
  26. package/dist/styles.css.map +1 -1
  27. package/package.json +14 -6
  28. package/dist/chunk-5EOV5JQZ.js.map +0 -1
  29. package/dist/chunk-ENXSUXC5.js.map +0 -1
  30. package/src/__test-fixtures__/dom.ts +0 -70
  31. package/src/__test-fixtures__/specs.ts +0 -214
  32. package/src/__tests__/animation.test.ts +0 -358
  33. package/src/__tests__/annotations-render.test.ts +0 -201
  34. package/src/__tests__/compound-labels.test.ts +0 -122
  35. package/src/__tests__/continuous-legend.test.ts +0 -91
  36. package/src/__tests__/crosshair.test.ts +0 -130
  37. package/src/__tests__/edit-events.test.ts +0 -902
  38. package/src/__tests__/events.test.ts +0 -387
  39. package/src/__tests__/export.test.ts +0 -241
  40. package/src/__tests__/font-race.test.ts +0 -199
  41. package/src/__tests__/gradient-ids.test.ts +0 -159
  42. package/src/__tests__/gradient-utils.test.ts +0 -125
  43. package/src/__tests__/known-bugs-render.test.ts +0 -183
  44. package/src/__tests__/map.test.ts +0 -293
  45. package/src/__tests__/mount.test.ts +0 -357
  46. package/src/__tests__/pattern-fills.test.ts +0 -105
  47. package/src/__tests__/resize-timing.test.ts +0 -218
  48. package/src/__tests__/sankey.test.ts +0 -133
  49. package/src/__tests__/selection-events.test.ts +0 -869
  50. package/src/__tests__/series-search.test.ts +0 -311
  51. package/src/__tests__/static.test.ts +0 -141
  52. package/src/__tests__/svg-renderer.test.ts +0 -895
  53. package/src/__tests__/table-keyboard.test.ts +0 -361
  54. package/src/__tests__/table-mount.test.ts +0 -639
  55. package/src/__tests__/tilemap.test.ts +0 -158
  56. package/src/__tests__/tooltip.test.ts +0 -337
  57. package/src/__tests__/transition.test.ts +0 -1777
  58. package/src/__tests__/you-draw-it.test.ts +0 -273
  59. package/src/animation-vars.ts +0 -42
  60. package/src/animation.ts +0 -76
  61. package/src/barlist-mount.ts +0 -383
  62. package/src/barlist-renderer.ts +0 -267
  63. package/src/dom-helpers.ts +0 -17
  64. package/src/export.ts +0 -482
  65. package/src/gradient-utils.ts +0 -138
  66. package/src/graph/__tests__/canvas-renderer.test.ts +0 -713
  67. package/src/graph/__tests__/graph-mount.test.ts +0 -213
  68. package/src/graph/__tests__/interaction.test.ts +0 -205
  69. package/src/graph/__tests__/keyboard.test.ts +0 -653
  70. package/src/graph/__tests__/search.test.ts +0 -88
  71. package/src/graph/__tests__/simulation.test.ts +0 -233
  72. package/src/graph/__tests__/spatial-index.test.ts +0 -142
  73. package/src/graph/__tests__/zoom.test.ts +0 -203
  74. package/src/graph/canvas-renderer.ts +0 -747
  75. package/src/graph/interaction.ts +0 -367
  76. package/src/graph/keyboard.ts +0 -208
  77. package/src/graph/search.ts +0 -50
  78. package/src/graph/simulation-worker-url.ts +0 -12
  79. package/src/graph/simulation-worker.ts +0 -278
  80. package/src/graph/simulation.ts +0 -422
  81. package/src/graph/spatial-index.ts +0 -121
  82. package/src/graph/types.ts +0 -47
  83. package/src/graph/worker-protocol.ts +0 -70
  84. package/src/graph/zoom.ts +0 -134
  85. package/src/graph-mount.ts +0 -888
  86. package/src/index.ts +0 -76
  87. package/src/interactions/chart-events.ts +0 -139
  88. package/src/interactions/crosshair.ts +0 -233
  89. package/src/interactions/drag-handler.ts +0 -175
  90. package/src/interactions/editing-drags.ts +0 -515
  91. package/src/interactions/index.ts +0 -25
  92. package/src/interactions/keyboard-nav.ts +0 -111
  93. package/src/interactions/legend-interaction.ts +0 -46
  94. package/src/interactions/selection.ts +0 -280
  95. package/src/interactions/tooltip-events.ts +0 -72
  96. package/src/map-camera.ts +0 -88
  97. package/src/map-mount.ts +0 -701
  98. package/src/map-renderer.ts +0 -338
  99. package/src/map-transition.ts +0 -83
  100. package/src/measure-text.ts +0 -134
  101. package/src/mount.ts +0 -1382
  102. package/src/pattern-utils.ts +0 -110
  103. package/src/renderers/annotations.ts +0 -388
  104. package/src/renderers/axes.ts +0 -351
  105. package/src/renderers/brand.ts +0 -87
  106. package/src/renderers/chrome.ts +0 -196
  107. package/src/renderers/endpoint-labels.ts +0 -150
  108. package/src/renderers/legend.ts +0 -311
  109. package/src/renderers/marks.ts +0 -561
  110. package/src/renderers/metrics.ts +0 -63
  111. package/src/renderers/svg-dom.ts +0 -40
  112. package/src/renderers/table-cells.ts +0 -446
  113. package/src/resize-observer.ts +0 -46
  114. package/src/sankey-mount.ts +0 -622
  115. package/src/sankey-renderer.ts +0 -614
  116. package/src/series-search.ts +0 -392
  117. package/src/static.ts +0 -251
  118. package/src/story/__tests__/camera-math.test.ts +0 -145
  119. package/src/story/__tests__/create-chart-story.test.ts +0 -317
  120. package/src/story/__tests__/progress-math.test.ts +0 -95
  121. package/src/story/__tests__/scroll-driver.test.ts +0 -130
  122. package/src/story/__tests__/tween.test.ts +0 -159
  123. package/src/story/camera-math.ts +0 -125
  124. package/src/story/create-chart-story.ts +0 -353
  125. package/src/story/crossfade.ts +0 -70
  126. package/src/story/index.ts +0 -39
  127. package/src/story/progress-math.ts +0 -85
  128. package/src/story/resolve-camera-target.ts +0 -119
  129. package/src/story/scroll-driver.ts +0 -206
  130. package/src/story/story-camera.ts +0 -49
  131. package/src/story/tween.ts +0 -112
  132. package/src/story/types.ts +0 -76
  133. package/src/svg-ids.ts +0 -23
  134. package/src/svg-renderer.ts +0 -383
  135. package/src/table-keyboard.ts +0 -266
  136. package/src/table-mount.ts +0 -567
  137. package/src/table-renderer.ts +0 -414
  138. package/src/text-edit-overlay.ts +0 -255
  139. package/src/theme-tokens.ts +0 -51
  140. package/src/tilemap-mount.ts +0 -511
  141. package/src/tilemap-renderer.ts +0 -396
  142. package/src/tooltip.ts +0 -177
  143. package/src/transition.ts +0 -2816
  144. package/src/you-draw-it.ts +0 -480
  145. /package/dist/{chunk-W34KN7FD.js.map → chunk-2NAQRP66.js.map} +0 -0
@@ -1,515 +0,0 @@
1
- import type {
2
- Annotation,
3
- AnnotationOffset,
4
- ChartSpec,
5
- ChromeKey,
6
- ElementEdit,
7
- GraphSpec,
8
- RangeAnnotation,
9
- RefLineAnnotation,
10
- TextAnnotation,
11
- } from '@opendata-ai/openchart-core';
12
- import { createDragHandler } from './drag-handler';
13
-
14
- /**
15
- * Wire drag-to-reposition on text annotation labels.
16
- * Returns a cleanup function to remove all listeners.
17
- */
18
- export function wireAnnotationDrag(
19
- svg: SVGElement,
20
- specAnnotations: Annotation[],
21
- onAnnotationEdit:
22
- | ((annotation: TextAnnotation, updatedOffset: AnnotationOffset) => void)
23
- | undefined,
24
- onEdit: ((edit: ElementEdit) => void) | undefined,
25
- setDragging: (dragging: boolean) => void,
26
- ): () => void {
27
- const annotationElements = svg.querySelectorAll('.oc-annotation-text');
28
- const cleanups: Array<() => void> = [];
29
-
30
- for (const el of annotationElements) {
31
- const indexStr = el.getAttribute('data-annotation-index');
32
- if (indexStr === null) continue;
33
-
34
- const index = Number(indexStr);
35
- const specAnnotation = specAnnotations[index];
36
- if (!specAnnotation || specAnnotation.type !== 'text') continue;
37
-
38
- const textAnnotation = specAnnotation as TextAnnotation;
39
- const annotationG = el as SVGGElement;
40
-
41
- annotationG.style.cursor = 'grab';
42
-
43
- const connectorLine = annotationG.querySelector('line.oc-annotation-connector');
44
- const origX2 = connectorLine ? Number(connectorLine.getAttribute('x2')) : 0;
45
- const origY2 = connectorLine ? Number(connectorLine.getAttribute('y2')) : 0;
46
-
47
- const curvedPath = annotationG.querySelector('path.oc-annotation-connector');
48
- const arrowhead = annotationG.querySelector('polyline.oc-annotation-arrowhead');
49
- const hasCurvedConnector = curvedPath !== null;
50
-
51
- const origDx = textAnnotation.offset?.dx ?? 0;
52
- const origDy = textAnnotation.offset?.dy ?? 0;
53
-
54
- const cleanup = createDragHandler({
55
- element: annotationG,
56
- svg: svg as unknown as SVGSVGElement,
57
- onMove: (dx, dy) => {
58
- annotationG.setAttribute('transform', `translate(${dx}, ${dy})`);
59
-
60
- if (connectorLine && !hasCurvedConnector) {
61
- connectorLine.setAttribute('x2', String(origX2 - dx));
62
- connectorLine.setAttribute('y2', String(origY2 - dy));
63
- }
64
-
65
- if (hasCurvedConnector) {
66
- if (curvedPath) curvedPath.setAttribute('display', 'none');
67
- if (arrowhead) arrowhead.setAttribute('display', 'none');
68
- }
69
- },
70
- onEnd: (dx, dy, moved) => {
71
- annotationG.removeAttribute('transform');
72
-
73
- if (connectorLine && !hasCurvedConnector) {
74
- connectorLine.setAttribute('x2', String(origX2));
75
- connectorLine.setAttribute('y2', String(origY2));
76
- }
77
-
78
- if (hasCurvedConnector) {
79
- if (curvedPath) curvedPath.removeAttribute('display');
80
- if (arrowhead) arrowhead.removeAttribute('display');
81
- }
82
-
83
- if (moved) {
84
- const newOffset: AnnotationOffset = {
85
- dx: origDx + dx,
86
- dy: origDy + dy,
87
- };
88
- onAnnotationEdit?.(textAnnotation, newOffset);
89
- onEdit?.({ type: 'annotation', annotation: textAnnotation, offset: newOffset });
90
- }
91
- },
92
- setDragging,
93
- });
94
-
95
- cleanups.push(cleanup);
96
- }
97
-
98
- return () => {
99
- for (const cleanup of cleanups) {
100
- cleanup();
101
- }
102
- };
103
- }
104
-
105
- /**
106
- * Wire drag on connector endpoint handles for text annotations.
107
- * Returns a cleanup function that removes handles and all listeners.
108
- */
109
- export function wireConnectorEndpointDrag(
110
- svg: SVGElement,
111
- specAnnotations: Annotation[],
112
- onEdit: (edit: ElementEdit) => void,
113
- setDragging: (dragging: boolean) => void,
114
- ): () => void {
115
- const SVG_NS = 'http://www.w3.org/2000/svg';
116
- const cleanups: Array<() => void> = [];
117
- const annotationGroups = svg.querySelectorAll('.oc-annotation-text');
118
-
119
- for (const el of annotationGroups) {
120
- const annotationG = el as SVGGElement;
121
- const indexStr = annotationG.getAttribute('data-annotation-index');
122
- if (indexStr === null) continue;
123
-
124
- const index = Number(indexStr);
125
- const specAnnotation = specAnnotations[index];
126
- if (!specAnnotation || specAnnotation.type !== 'text') continue;
127
-
128
- const textAnnotation = specAnnotation as TextAnnotation;
129
-
130
- const connectorLine = annotationG.querySelector('line.oc-annotation-connector');
131
- const curvedPath = annotationG.querySelector('path.oc-annotation-connector');
132
- if (!connectorLine && !curvedPath) continue;
133
-
134
- let fromX: number, fromY: number, toX: number, toY: number;
135
- if (connectorLine) {
136
- fromX = Number(connectorLine.getAttribute('x1')) || 0;
137
- fromY = Number(connectorLine.getAttribute('y1')) || 0;
138
- toX = Number(connectorLine.getAttribute('x2')) || 0;
139
- toY = Number(connectorLine.getAttribute('y2')) || 0;
140
- } else {
141
- const pathD = curvedPath!.getAttribute('d') ?? '';
142
- const mMatch = pathD.match(/M\s*([\d.e+-]+)\s+([\d.e+-]+)/);
143
- fromX = mMatch ? Number(mMatch[1]) : 0;
144
- fromY = mMatch ? Number(mMatch[2]) : 0;
145
- // The open-V arrowhead is a polyline "baseLeft tip baseRight": the tip is
146
- // the middle point.
147
- const arrowhead = annotationG.querySelector('polyline.oc-annotation-arrowhead');
148
- const points = arrowhead?.getAttribute('points')?.split(' ') ?? [];
149
- const [px, py] = (points[1] ?? '0,0').split(',');
150
- toX = Number(px) || 0;
151
- toY = Number(py) || 0;
152
- }
153
-
154
- const endpoints: Array<{ name: 'from' | 'to'; cx: number; cy: number }> = [
155
- { name: 'from', cx: fromX, cy: fromY },
156
- { name: 'to', cx: toX, cy: toY },
157
- ];
158
-
159
- const createdHandles: SVGCircleElement[] = [];
160
-
161
- for (const ep of endpoints) {
162
- if (!Number.isFinite(ep.cx) || !Number.isFinite(ep.cy)) continue;
163
-
164
- const handleEl = document.createElementNS(SVG_NS, 'circle') as SVGCircleElement;
165
- handleEl.setAttribute('class', 'oc-connector-handle');
166
- handleEl.setAttribute('data-endpoint', ep.name);
167
- handleEl.setAttribute('cx', String(ep.cx));
168
- handleEl.setAttribute('cy', String(ep.cy));
169
- handleEl.setAttribute('r', '4');
170
- handleEl.setAttribute('opacity', '0');
171
- handleEl.setAttribute('fill', 'currentColor');
172
- handleEl.setAttribute('stroke', 'currentColor');
173
- annotationG.appendChild(handleEl);
174
- createdHandles.push(handleEl);
175
-
176
- const origCx = ep.cx;
177
- const origCy = ep.cy;
178
-
179
- const stopProp = (e: Event) => {
180
- e.stopPropagation();
181
- };
182
- handleEl.addEventListener('mousedown', stopProp);
183
- handleEl.addEventListener('touchstart', stopProp);
184
- cleanups.push(() => {
185
- handleEl.removeEventListener('mousedown', stopProp);
186
- handleEl.removeEventListener('touchstart', stopProp);
187
- });
188
-
189
- const cleanup = createDragHandler({
190
- element: handleEl,
191
- svg: svg as unknown as SVGSVGElement,
192
- onMove: (dx, dy) => {
193
- handleEl.setAttribute('cx', String(origCx + dx));
194
- handleEl.setAttribute('cy', String(origCy + dy));
195
-
196
- if (connectorLine) {
197
- if (ep.name === 'from') {
198
- connectorLine.setAttribute('x1', String(origCx + dx));
199
- connectorLine.setAttribute('y1', String(origCy + dy));
200
- } else {
201
- connectorLine.setAttribute('x2', String(origCx + dx));
202
- connectorLine.setAttribute('y2', String(origCy + dy));
203
- }
204
- }
205
- },
206
- onEnd: (dx, dy, moved) => {
207
- handleEl.setAttribute('cx', String(origCx));
208
- handleEl.setAttribute('cy', String(origCy));
209
-
210
- if (connectorLine) {
211
- if (ep.name === 'from') {
212
- connectorLine.setAttribute('x1', String(origCx));
213
- connectorLine.setAttribute('y1', String(origCy));
214
- } else {
215
- connectorLine.setAttribute('x2', String(origCx));
216
- connectorLine.setAttribute('y2', String(origCy));
217
- }
218
- }
219
-
220
- if (moved) {
221
- const existingOffset = textAnnotation.connectorOffset?.[ep.name];
222
- const origEndDx = existingOffset?.dx ?? 0;
223
- const origEndDy = existingOffset?.dy ?? 0;
224
- onEdit({
225
- type: 'annotation-connector',
226
- annotation: textAnnotation,
227
- endpoint: ep.name,
228
- offset: { dx: origEndDx + dx, dy: origEndDy + dy },
229
- });
230
- }
231
- },
232
- setDragging,
233
- });
234
-
235
- cleanups.push(cleanup);
236
- }
237
-
238
- const showHandles = () => {
239
- for (const h of createdHandles) {
240
- h.setAttribute('opacity', '0.6');
241
- }
242
- };
243
- const hideHandles = () => {
244
- for (const h of createdHandles) {
245
- h.setAttribute('opacity', '0');
246
- }
247
- };
248
-
249
- annotationG.addEventListener('mouseenter', showHandles);
250
- annotationG.addEventListener('mouseleave', hideHandles);
251
- cleanups.push(() => {
252
- annotationG.removeEventListener('mouseenter', showHandles);
253
- annotationG.removeEventListener('mouseleave', hideHandles);
254
- for (const h of createdHandles) {
255
- h.remove();
256
- }
257
- });
258
- }
259
-
260
- return () => {
261
- for (const cleanup of cleanups) {
262
- cleanup();
263
- }
264
- };
265
- }
266
-
267
- /**
268
- * Wire drag on range and refline annotation labels.
269
- * Returns a cleanup function.
270
- */
271
- export function wireAnnotationLabelDrag(
272
- svg: SVGElement,
273
- specAnnotations: Annotation[],
274
- onEdit: (edit: ElementEdit) => void,
275
- setDragging: (dragging: boolean) => void,
276
- ): () => void {
277
- const cleanups: Array<() => void> = [];
278
-
279
- const selectors = [
280
- '.oc-annotation-range .oc-annotation-label',
281
- '.oc-annotation-refline .oc-annotation-label',
282
- ];
283
-
284
- for (const selector of selectors) {
285
- const labels = svg.querySelectorAll(selector);
286
-
287
- for (const label of labels) {
288
- const annotationG = label.closest('.oc-annotation') as SVGGElement | null;
289
- if (!annotationG) continue;
290
-
291
- const indexStr = annotationG.getAttribute('data-annotation-index');
292
- if (indexStr === null) continue;
293
-
294
- const index = Number(indexStr);
295
- const specAnnotation = specAnnotations[index];
296
- if (!specAnnotation) continue;
297
-
298
- const labelEl = label as SVGTextElement;
299
- labelEl.style.cursor = 'grab';
300
-
301
- const isRange = specAnnotation.type === 'range';
302
- const existingLabelOffset = isRange
303
- ? (specAnnotation as RangeAnnotation).labelOffset
304
- : (specAnnotation as RefLineAnnotation).labelOffset;
305
- const origLabelDx = existingLabelOffset?.dx ?? 0;
306
- const origLabelDy = existingLabelOffset?.dy ?? 0;
307
-
308
- const cleanup = createDragHandler({
309
- element: labelEl,
310
- svg: svg as unknown as SVGSVGElement,
311
- onMove: (dx, dy) => {
312
- (labelEl as SVGElement & ElementCSSInlineStyle).style.transform =
313
- `translate(${dx}px, ${dy}px)`;
314
- },
315
- onEnd: (dx, dy, moved) => {
316
- (labelEl as SVGElement & ElementCSSInlineStyle).style.transform = '';
317
-
318
- if (moved) {
319
- if (isRange) {
320
- onEdit({
321
- type: 'range-label',
322
- annotation: specAnnotation as RangeAnnotation,
323
- labelOffset: { dx: origLabelDx + dx, dy: origLabelDy + dy },
324
- });
325
- } else {
326
- onEdit({
327
- type: 'refline-label',
328
- annotation: specAnnotation as RefLineAnnotation,
329
- labelOffset: { dx: origLabelDx + dx, dy: origLabelDy + dy },
330
- });
331
- }
332
- }
333
- },
334
- setDragging,
335
- });
336
-
337
- cleanups.push(cleanup);
338
- }
339
- }
340
-
341
- return () => {
342
- for (const cleanup of cleanups) {
343
- cleanup();
344
- }
345
- };
346
- }
347
-
348
- /**
349
- * Wire drag on chrome text elements (title, subtitle, source, byline, footer).
350
- * Returns a cleanup function.
351
- */
352
- export function wireChromeDrag(
353
- svg: SVGElement,
354
- spec: ChartSpec | GraphSpec,
355
- onEdit: (edit: ElementEdit) => void,
356
- setDragging: (dragging: boolean) => void,
357
- ): () => void {
358
- const chromeTexts = svg.querySelectorAll('.oc-chrome text[data-chrome-key]');
359
- const cleanups: Array<() => void> = [];
360
-
361
- const chromeConfig = 'chrome' in spec ? spec.chrome : undefined;
362
-
363
- for (const el of chromeTexts) {
364
- const textEl = el as SVGTextElement;
365
- const key = textEl.getAttribute('data-chrome-key') as ChromeKey;
366
- if (!key) continue;
367
-
368
- const chromeEntry = chromeConfig?.[key];
369
- const existingOffset =
370
- typeof chromeEntry === 'object' && chromeEntry !== null ? chromeEntry.offset : undefined;
371
- const origChromeDx = existingOffset?.dx ?? 0;
372
- const origChromeDy = existingOffset?.dy ?? 0;
373
-
374
- textEl.style.cursor = 'grab';
375
-
376
- const cleanup = createDragHandler({
377
- element: textEl,
378
- svg: svg as unknown as SVGSVGElement,
379
- onMove: (dx, dy) => {
380
- (textEl as SVGElement & ElementCSSInlineStyle).style.transform =
381
- `translate(${dx}px, ${dy}px)`;
382
- },
383
- onEnd: (dx, dy, moved) => {
384
- (textEl as SVGElement & ElementCSSInlineStyle).style.transform = '';
385
-
386
- if (moved) {
387
- onEdit({
388
- type: 'chrome',
389
- key,
390
- text: textEl.textContent ?? '',
391
- offset: { dx: origChromeDx + dx, dy: origChromeDy + dy },
392
- });
393
- }
394
- },
395
- setDragging,
396
- });
397
-
398
- cleanups.push(cleanup);
399
- }
400
-
401
- return () => {
402
- for (const cleanup of cleanups) {
403
- cleanup();
404
- }
405
- };
406
- }
407
-
408
- /**
409
- * Wire drag on the legend group.
410
- * Returns a cleanup function.
411
- */
412
- export function wireLegendDrag(
413
- svg: SVGElement,
414
- spec: ChartSpec | GraphSpec,
415
- onEdit: (edit: ElementEdit) => void,
416
- setDragging: (dragging: boolean) => void,
417
- ): () => void {
418
- // The color legend: it owns `spec.legend.offset`, which is what a drag writes
419
- // back. The size legend is not draggable and must not be picked up here.
420
- const legendG = svg.querySelector('.oc-legend:not(.oc-legend--size)') as SVGGElement | null;
421
- if (!legendG) return () => {};
422
-
423
- const cleanups: Array<() => void> = [];
424
-
425
- const legendConfig = 'legend' in spec ? spec.legend : undefined;
426
- const origLegendDx = legendConfig?.offset?.dx ?? 0;
427
- const origLegendDy = legendConfig?.offset?.dy ?? 0;
428
-
429
- legendG.style.cursor = 'grab';
430
-
431
- const cleanup = createDragHandler({
432
- element: legendG,
433
- svg: svg as unknown as SVGSVGElement,
434
- onMove: (dx, dy) => {
435
- (legendG as SVGElement & ElementCSSInlineStyle).style.transform =
436
- `translate(${dx}px, ${dy}px)`;
437
- },
438
- onEnd: (dx, dy, moved) => {
439
- (legendG as SVGElement & ElementCSSInlineStyle).style.transform = '';
440
-
441
- if (moved) {
442
- onEdit({ type: 'legend', offset: { dx: origLegendDx + dx, dy: origLegendDy + dy } });
443
- }
444
- },
445
- setDragging,
446
- });
447
-
448
- cleanups.push(cleanup);
449
-
450
- return () => {
451
- for (const cleanup of cleanups) {
452
- cleanup();
453
- }
454
- };
455
- }
456
-
457
- /**
458
- * Wire drag on series label elements (.oc-mark-label[data-series]).
459
- * Returns a cleanup function.
460
- */
461
- export function wireSeriesLabelDrag(
462
- svg: SVGElement,
463
- spec: ChartSpec | GraphSpec,
464
- onEdit: (edit: ElementEdit) => void,
465
- setDragging: (dragging: boolean) => void,
466
- ): () => void {
467
- const labels = svg.querySelectorAll('.oc-mark-label');
468
- const cleanups: Array<() => void> = [];
469
-
470
- const rawLabels = 'labels' in spec ? spec.labels : undefined;
471
- const labelsConfig = typeof rawLabels === 'object' ? rawLabels : undefined;
472
-
473
- for (const label of labels) {
474
- const labelEl = label as SVGTextElement;
475
- const series =
476
- labelEl.getAttribute('data-series') ??
477
- labelEl.closest('[data-series]')?.getAttribute('data-series');
478
- if (!series) continue;
479
-
480
- const existingSeriesOffset = labelsConfig?.offsets?.[series];
481
- const origSeriesDx = existingSeriesOffset?.dx ?? 0;
482
- const origSeriesDy = existingSeriesOffset?.dy ?? 0;
483
-
484
- labelEl.style.cursor = 'grab';
485
-
486
- const cleanup = createDragHandler({
487
- element: labelEl,
488
- svg: svg as unknown as SVGSVGElement,
489
- onMove: (dx, dy) => {
490
- (labelEl as SVGElement & ElementCSSInlineStyle).style.transform =
491
- `translate(${dx}px, ${dy}px)`;
492
- },
493
- onEnd: (dx, dy, moved) => {
494
- (labelEl as SVGElement & ElementCSSInlineStyle).style.transform = '';
495
-
496
- if (moved) {
497
- onEdit({
498
- type: 'series-label',
499
- series,
500
- offset: { dx: origSeriesDx + dx, dy: origSeriesDy + dy },
501
- });
502
- }
503
- },
504
- setDragging,
505
- });
506
-
507
- cleanups.push(cleanup);
508
- }
509
-
510
- return () => {
511
- for (const cleanup of cleanups) {
512
- cleanup();
513
- }
514
- };
515
- }
@@ -1,25 +0,0 @@
1
- export { wireChartEvents } from './chart-events';
2
- export { wireVoronoiTooltipEvents } from './crosshair';
3
- export type { DragConfig } from './drag-handler';
4
- export { createDragHandler } from './drag-handler';
5
- export {
6
- wireAnnotationDrag,
7
- wireAnnotationLabelDrag,
8
- wireChromeDrag,
9
- wireConnectorEndpointDrag,
10
- wireLegendDrag,
11
- wireSeriesLabelDrag,
12
- } from './editing-drags';
13
- export { wireKeyboardNav } from './keyboard-nav';
14
- export { wireLegendInteraction } from './legend-interaction';
15
- export {
16
- buildElementRef,
17
- createScreenReaderTable,
18
- findElementByRef,
19
- getEditableElements,
20
- getElementText,
21
- isTextEditable,
22
- refsEqual,
23
- renderSelectionOverlay,
24
- } from './selection';
25
- export { wireTooltipEvents } from './tooltip-events';
@@ -1,111 +0,0 @@
1
- import type { ChartLayout, TooltipContent } from '@opendata-ai/openchart-core';
2
- import type { TooltipManager } from '../tooltip';
3
-
4
- /**
5
- * Wire keyboard navigation on the SVG element.
6
- * Arrow keys move focus between mark elements. Enter/Space shows tooltip.
7
- * Escape hides tooltip. Returns a cleanup function.
8
- */
9
- export function wireKeyboardNav(
10
- svg: SVGElement,
11
- container: HTMLElement,
12
- tooltipDescriptors: Map<string, TooltipContent>,
13
- tooltipManager: TooltipManager,
14
- layout: ChartLayout,
15
- ): () => void {
16
- container.setAttribute('tabindex', '0');
17
- container.setAttribute('aria-roledescription', 'chart');
18
- container.setAttribute('aria-label', layout.a11y.altText);
19
-
20
- const markElements: SVGElement[] = [];
21
- const allMarkEls = svg.querySelectorAll('[data-mark-id]');
22
- for (const el of allMarkEls) {
23
- const markId = el.getAttribute('data-mark-id');
24
- if (markId && tooltipDescriptors.has(markId)) {
25
- markElements.push(el as SVGElement);
26
- }
27
- }
28
-
29
- let focusIndex = -1;
30
-
31
- function highlightMark(index: number): void {
32
- if (focusIndex >= 0 && focusIndex < markElements.length) {
33
- markElements[focusIndex].classList.remove('oc-mark-focused');
34
- markElements[focusIndex].removeAttribute('aria-selected');
35
- }
36
-
37
- focusIndex = index;
38
-
39
- if (focusIndex >= 0 && focusIndex < markElements.length) {
40
- const el = markElements[focusIndex];
41
- el.classList.add('oc-mark-focused');
42
- el.setAttribute('aria-selected', 'true');
43
- }
44
- }
45
-
46
- function showTooltipForFocused(): void {
47
- if (focusIndex < 0 || focusIndex >= markElements.length) return;
48
-
49
- const el = markElements[focusIndex];
50
- const markId = el.getAttribute('data-mark-id');
51
- if (!markId) return;
52
-
53
- const content = tooltipDescriptors.get(markId);
54
- if (!content) return;
55
-
56
- const bbox = el.getBoundingClientRect();
57
- const containerRect = container.getBoundingClientRect();
58
- const x = bbox.left + bbox.width / 2 - containerRect.left;
59
- const y = bbox.top - containerRect.top;
60
- tooltipManager.show(content, x, y);
61
- }
62
-
63
- const handleKeyDown = (e: KeyboardEvent) => {
64
- if (markElements.length === 0) return;
65
-
66
- switch (e.key) {
67
- case 'ArrowRight':
68
- case 'ArrowDown': {
69
- e.preventDefault();
70
- const next = focusIndex < markElements.length - 1 ? focusIndex + 1 : 0;
71
- highlightMark(next);
72
- showTooltipForFocused();
73
- break;
74
- }
75
- case 'ArrowLeft':
76
- case 'ArrowUp': {
77
- e.preventDefault();
78
- const prev = focusIndex > 0 ? focusIndex - 1 : markElements.length - 1;
79
- highlightMark(prev);
80
- showTooltipForFocused();
81
- break;
82
- }
83
- case 'Enter':
84
- case ' ': {
85
- e.preventDefault();
86
- if (focusIndex >= 0) {
87
- showTooltipForFocused();
88
- } else if (markElements.length > 0) {
89
- highlightMark(0);
90
- showTooltipForFocused();
91
- }
92
- break;
93
- }
94
- case 'Escape': {
95
- e.preventDefault();
96
- tooltipManager.hide();
97
- highlightMark(-1);
98
- break;
99
- }
100
- }
101
- };
102
-
103
- container.addEventListener('keydown', handleKeyDown);
104
-
105
- return () => {
106
- container.removeEventListener('keydown', handleKeyDown);
107
- container.removeAttribute('tabindex');
108
- container.removeAttribute('aria-roledescription');
109
- container.removeAttribute('aria-label');
110
- };
111
- }
@@ -1,46 +0,0 @@
1
- import type { ChartLayout, ElementEdit } from '@opendata-ai/openchart-core';
2
-
3
- /**
4
- * Wire click handlers on legend entries to toggle series visibility.
5
- * Returns a cleanup function.
6
- */
7
- export function wireLegendInteraction(
8
- svg: SVGElement,
9
- _layout: ChartLayout,
10
- toggleSeries: (series: string) => boolean,
11
- onLegendToggle?: (series: string, visible: boolean) => void,
12
- onEdit?: (edit: ElementEdit) => void,
13
- ): () => void {
14
- // Scoped OUT of the size legend explicitly. Clicking a legend entry toggles a
15
- // *series*, and a size legend's circles are values, not series -- clicking
16
- // "500M" must not try to hide a series by that name. The size legend already
17
- // emits no `data-legend-index`, but a bare `[data-legend-index]` sweep would
18
- // silently pick one up the moment that changed, and the failure would be a
19
- // click that quietly hides the wrong thing rather than an error.
20
- const legendEntries = svg.querySelectorAll(
21
- '.oc-legend:not(.oc-legend--size) [data-legend-index]',
22
- );
23
- const cleanups: Array<() => void> = [];
24
-
25
- for (const entry of legendEntries) {
26
- if (entry.getAttribute('data-legend-overflow') === 'true') continue;
27
-
28
- const handleClick = () => {
29
- const label = entry.getAttribute('data-legend-label');
30
- if (!label) return;
31
-
32
- const nowHidden = toggleSeries(label);
33
- onLegendToggle?.(label, !nowHidden);
34
- onEdit?.({ type: 'legend-toggle', series: label, hidden: nowHidden });
35
- };
36
-
37
- entry.addEventListener('click', handleClick);
38
- cleanups.push(() => entry.removeEventListener('click', handleClick));
39
- }
40
-
41
- return () => {
42
- for (const cleanup of cleanups) {
43
- cleanup();
44
- }
45
- };
46
- }