@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
package/src/index.ts DELETED
@@ -1,76 +0,0 @@
1
- /**
2
- * @opendata-ai/openchart-vanilla
3
- *
4
- * Vanilla JS adapter for openchart. Renders ChartLayout and TableLayout
5
- * objects into real DOM elements using imperative SVG/HTML creation.
6
- *
7
- * Provides createChart() for mounting visualizations, ResizeObserver integration
8
- * for responsive behavior, tooltip management, and SVG/PNG/CSV export.
9
- */
10
-
11
- // Re-export core types for convenience
12
- export type {
13
- ChartLayout,
14
- ChartSpec,
15
- CompileOptions,
16
- TableLayout,
17
- TableSpec,
18
- VizSpec,
19
- } from '@opendata-ai/openchart-engine';
20
- export type { BarListInstance, BarListMountOptions } from './barlist-mount';
21
- // BarList mount API
22
- export { createBarList } from './barlist-mount';
23
- export type { JPGExportOptions, PNGExportOptions, SVGExportOptions } from './export';
24
- // Export utilities
25
- export { exportCSV, exportJPG, exportPNG, exportSVG, exportSVGWithFonts } from './export';
26
- // Graph simulation worker
27
- export { createSimulationWorker } from './graph/simulation-worker-url';
28
- export type { GraphInstance, GraphMountOptions } from './graph-mount';
29
- // Graph mount API
30
- export { createGraph } from './graph-mount';
31
- export type { Camera, MapCameraOptions } from './map-camera';
32
- export type { MapInstance, MapMountOptions } from './map-mount';
33
- // Map mount API
34
- export { createMap } from './map-mount';
35
- export type { ChartInstance, ExportOptions, MountOptions, UpdateOptions } from './mount';
36
- // Main mount API
37
- export { createChart } from './mount';
38
- // Geometry helpers for mark path reconstruction
39
- export { rectPathWithCorners } from './renderers/marks';
40
- // Cell renderers
41
- export {
42
- renderBarCell,
43
- renderCategoryCell,
44
- renderCell,
45
- renderFlagCell,
46
- renderHeatmapCell,
47
- renderImageCell,
48
- renderSparklineCell,
49
- renderTextCell,
50
- } from './renderers/table-cells';
51
- // Resize observer
52
- export { observeResize } from './resize-observer';
53
- export type { SankeyInstance, SankeyMountOptions } from './sankey-mount';
54
- // Sankey mount API
55
- export { createSankey } from './sankey-mount';
56
- // SVG renderer (for advanced usage / custom rendering)
57
- export { registerMarkRenderer, renderChartSVG } from './svg-renderer';
58
- export type { KeyboardNavOptions } from './table-keyboard';
59
- // Table keyboard navigation
60
- export { attachKeyboardNav } from './table-keyboard';
61
- export type { TableInstance, TableMountOptions, TableState } from './table-mount';
62
- // Table mount API
63
- export { createTable } from './table-mount';
64
- // Table renderer (for advanced usage / custom rendering)
65
- export { renderTable } from './table-renderer';
66
- export type { TextEditOverlayConfig } from './text-edit-overlay';
67
- export { createTextEditOverlay } from './text-edit-overlay';
68
- export type { TileMapInstance, TileMapMountOptions } from './tilemap-mount';
69
- // TileMap mount API
70
- export { createTileMap } from './tilemap-mount';
71
- export type { TooltipManager } from './tooltip';
72
- // Tooltip
73
- export { createTooltipManager } from './tooltip';
74
- export type { YouDrawItController, YouDrawItOptions } from './you-draw-it';
75
- // You draw it (interactive draw-then-reveal)
76
- export { createYouDrawIt } from './you-draw-it';
@@ -1,139 +0,0 @@
1
- import type { Annotation, ChartEventHandlers, ChartLayout } from '@opendata-ai/openchart-core';
2
-
3
- /**
4
- * Build a map from data-mark-id to { datum, series } so event handlers
5
- * can look up the data row associated with a clicked/hovered mark element.
6
- */
7
- function buildMarkDataMap(
8
- layout: ChartLayout,
9
- ): Map<string, { datum: Record<string, unknown>; series?: string }> {
10
- const map = new Map<string, { datum: Record<string, unknown>; series?: string }>();
11
-
12
- for (let i = 0; i < layout.marks.length; i++) {
13
- const mark = layout.marks[i];
14
- switch (mark.type) {
15
- case 'line':
16
- map.set(`line-${mark.seriesKey ?? i}`, {
17
- datum: mark.data[0] ?? {},
18
- series: mark.seriesKey,
19
- });
20
- break;
21
- case 'area':
22
- map.set(`area-${mark.seriesKey ?? i}`, {
23
- datum: mark.data[0] ?? {},
24
- series: mark.seriesKey,
25
- });
26
- break;
27
- case 'rect':
28
- map.set(`rect-${i}`, { datum: mark.data });
29
- break;
30
- case 'arc':
31
- map.set(`arc-${i}`, { datum: mark.data });
32
- break;
33
- case 'point':
34
- map.set(`point-${i}`, { datum: mark.data });
35
- break;
36
- }
37
- }
38
-
39
- return map;
40
- }
41
-
42
- /**
43
- * Wire chart event handlers (onMarkClick, onMarkHover, onMarkLeave) to mark
44
- * elements, onLegendToggle to legend entries, and onAnnotationClick to annotation
45
- * elements inside an SVG.
46
- *
47
- * Returns a cleanup function to remove all listeners.
48
- */
49
- export function wireChartEvents(
50
- svg: SVGElement,
51
- layout: ChartLayout,
52
- specAnnotations: Annotation[],
53
- handlers: ChartEventHandlers,
54
- ): () => void {
55
- const cleanups: Array<() => void> = [];
56
- const markDataMap = buildMarkDataMap(layout);
57
-
58
- if (handlers.onMarkClick || handlers.onMarkHover || handlers.onMarkLeave) {
59
- const markElements = svg.querySelectorAll('[data-mark-id]');
60
-
61
- for (const el of markElements) {
62
- const markId = el.getAttribute('data-mark-id');
63
- if (!markId) continue;
64
-
65
- const markInfo = markDataMap.get(markId);
66
- if (!markInfo) continue;
67
-
68
- const series = markInfo.series ?? el.getAttribute('data-series') ?? undefined;
69
-
70
- if (handlers.onMarkClick) {
71
- const handleClick = (e: Event) => {
72
- const mouseEvent = e as MouseEvent;
73
- const svgRect = svg.getBoundingClientRect();
74
- handlers.onMarkClick!({
75
- datum: markInfo.datum,
76
- series,
77
- position: {
78
- x: mouseEvent.clientX - svgRect.left,
79
- y: mouseEvent.clientY - svgRect.top,
80
- },
81
- event: mouseEvent,
82
- });
83
- };
84
- el.addEventListener('click', handleClick);
85
- cleanups.push(() => el.removeEventListener('click', handleClick));
86
- }
87
-
88
- if (handlers.onMarkHover) {
89
- const handleEnter = (e: Event) => {
90
- const mouseEvent = e as MouseEvent;
91
- const svgRect = svg.getBoundingClientRect();
92
- handlers.onMarkHover!({
93
- datum: markInfo.datum,
94
- series,
95
- position: {
96
- x: mouseEvent.clientX - svgRect.left,
97
- y: mouseEvent.clientY - svgRect.top,
98
- },
99
- event: mouseEvent,
100
- });
101
- };
102
- el.addEventListener('mouseenter', handleEnter);
103
- cleanups.push(() => el.removeEventListener('mouseenter', handleEnter));
104
- }
105
-
106
- if (handlers.onMarkLeave) {
107
- const handleLeave = () => {
108
- handlers.onMarkLeave!();
109
- };
110
- el.addEventListener('mouseleave', handleLeave);
111
- cleanups.push(() => el.removeEventListener('mouseleave', handleLeave));
112
- }
113
- }
114
- }
115
-
116
- if (handlers.onAnnotationClick) {
117
- const annotationElements = svg.querySelectorAll('.oc-annotation');
118
-
119
- for (let i = 0; i < annotationElements.length; i++) {
120
- const el = annotationElements[i];
121
- const specAnnotation = specAnnotations[i];
122
- if (!specAnnotation) continue;
123
-
124
- const handleClick = (e: Event) => {
125
- const mouseEvent = e as MouseEvent;
126
- handlers.onAnnotationClick!(specAnnotation, mouseEvent);
127
- };
128
-
129
- el.addEventListener('click', handleClick);
130
- cleanups.push(() => el.removeEventListener('click', handleClick));
131
- }
132
- }
133
-
134
- return () => {
135
- for (const cleanup of cleanups) {
136
- cleanup();
137
- }
138
- };
139
- }
@@ -1,233 +0,0 @@
1
- import type { ChartLayout, TooltipContent } from '@opendata-ai/openchart-core';
2
- import { getRepresentativeColor } from '@opendata-ai/openchart-core';
3
- import type { TooltipManager } from '../tooltip';
4
-
5
- interface SeriesPoint {
6
- x: number;
7
- y: number;
8
- datum: Record<string, unknown>;
9
- tooltip?: TooltipContent;
10
- }
11
-
12
- interface SeriesGroup {
13
- seriesKey: string;
14
- color: string;
15
- pointsByX: Map<number, SeriesPoint>;
16
- }
17
-
18
- function snapKey(x: number): number {
19
- return Math.round(x);
20
- }
21
-
22
- function collectSeriesGroups(layout: ChartLayout): SeriesGroup[] {
23
- // Dedupe by seriesKey: area charts emit BOTH an AreaMark and a derived
24
- // LineMark per series. Without dedupe, single-series area charts show
25
- // "line-0" / "area-1" in the tooltip instead of the actual data fields.
26
- // Prefer the line mark (same logic as endpoint-labels).
27
- const byKey = new Map<string, SeriesGroup>();
28
- const markTypeByKey = new Map<string, 'line' | 'area'>();
29
- for (let i = 0; i < layout.marks.length; i++) {
30
- const mark = layout.marks[i];
31
- if ((mark.type === 'line' || mark.type === 'area') && mark.dataPoints?.length) {
32
- const key = mark.seriesKey ?? '__default__';
33
- const existingType = markTypeByKey.get(key);
34
- if (existingType === 'line' && mark.type === 'area') continue;
35
- const color = mark.type === 'line' ? mark.stroke : getRepresentativeColor(mark.fill);
36
- const pointsByX = new Map<number, SeriesPoint>();
37
- for (const dp of mark.dataPoints) {
38
- pointsByX.set(snapKey(dp.x), { ...dp });
39
- }
40
- byKey.set(key, { seriesKey: key, color, pointsByX });
41
- markTypeByKey.set(key, mark.type);
42
- }
43
- }
44
- return Array.from(byKey.values());
45
- }
46
-
47
- function collectSnapXs(groups: SeriesGroup[]): number[] {
48
- const seen = new Set<number>();
49
- for (const g of groups) {
50
- for (const k of g.pointsByX.keys()) seen.add(k);
51
- }
52
- return Array.from(seen).sort((a, b) => a - b);
53
- }
54
-
55
- function findNearestX(sortedXs: number[], x: number): number | null {
56
- if (sortedXs.length === 0) return null;
57
- let lo = 0;
58
- let hi = sortedXs.length - 1;
59
- while (lo < hi) {
60
- const mid = (lo + hi) >> 1;
61
- if (sortedXs[mid] < x) lo = mid + 1;
62
- else hi = mid;
63
- }
64
- const candidate = sortedXs[lo];
65
- if (lo > 0) {
66
- const prev = sortedXs[lo - 1];
67
- if (Math.abs(prev - x) < Math.abs(candidate - x)) return prev;
68
- }
69
- return candidate;
70
- }
71
-
72
- function buildSliceTooltip(
73
- hits: Array<{ group: SeriesGroup; point: SeriesPoint }>,
74
- ): TooltipContent | null {
75
- if (hits.length === 0) return null;
76
-
77
- const title = hits[0].point.tooltip?.title;
78
- const fields: Array<{ label: string; value: string; color?: string }> = [];
79
-
80
- const isMulti = hits.length > 1;
81
- for (const { group, point } of hits) {
82
- const tip = point.tooltip;
83
- if (!tip) continue;
84
- if (isMulti) {
85
- const yField =
86
- tip.fields.find((f) => !f.color && f.label !== title) ??
87
- tip.fields[tip.fields.length - 1] ??
88
- null;
89
- if (!yField) continue;
90
- fields.push({
91
- label: group.seriesKey,
92
- value: yField.value,
93
- color: group.color,
94
- });
95
- } else {
96
- for (const f of tip.fields) {
97
- fields.push({ ...f, color: f.color ?? group.color });
98
- }
99
- }
100
- }
101
-
102
- if (fields.length === 0) return null;
103
- return { title, fields };
104
- }
105
-
106
- /**
107
- * Wire snap-to-x multi-series tooltip events for line/area charts.
108
- * On mousemove over the chart area we find the nearest x in the union of all
109
- * series, render one snap dot per series at that x, and show one merged
110
- * tooltip listing every series' value.
111
- */
112
- export function wireVoronoiTooltipEvents(
113
- svg: SVGElement,
114
- layout: ChartLayout,
115
- tooltipManager: TooltipManager,
116
- ): () => void {
117
- const overlay = svg.querySelector('[data-voronoi-overlay]');
118
- if (!overlay) return () => {};
119
-
120
- const groups = collectSeriesGroups(layout);
121
- if (groups.length === 0) return () => {};
122
-
123
- const snapXs = collectSnapXs(groups);
124
- if (snapXs.length === 0) return () => {};
125
-
126
- const crosshair = svg.querySelector('[data-crosshair]') as SVGLineElement | null;
127
- const dotsLayer = svg.querySelector('[data-snap-dots]') as SVGGElement | null;
128
-
129
- const dots: SVGCircleElement[] = [];
130
- if (dotsLayer) {
131
- while (dotsLayer.firstChild) dotsLayer.removeChild(dotsLayer.firstChild);
132
- for (const group of groups) {
133
- const circle = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
134
- circle.setAttribute('r', '4');
135
- circle.setAttribute('fill', layout.theme.colors.background);
136
- circle.setAttribute('stroke', group.color);
137
- circle.setAttribute('stroke-width', '2');
138
- circle.setAttribute('pointer-events', 'none');
139
- circle.style.display = 'none';
140
- dotsLayer.appendChild(circle);
141
- dots.push(circle);
142
- }
143
- }
144
-
145
- const positionAt = (svgX: number, _svgY: number, viewBoxToContainer: number): boolean => {
146
- const snappedX = findNearestX(snapXs, svgX);
147
- if (snappedX === null) return false;
148
-
149
- const hits: Array<{ group: SeriesGroup; point: SeriesPoint }> = [];
150
- let anchorY = 0;
151
- let anchorCount = 0;
152
- for (let i = 0; i < groups.length; i++) {
153
- const group = groups[i];
154
- const point = group.pointsByX.get(snappedX);
155
- const dot = dots[i];
156
- if (point) {
157
- hits.push({ group, point });
158
- anchorY += point.y;
159
- anchorCount += 1;
160
- if (dot) {
161
- dot.setAttribute('cx', String(point.x));
162
- dot.setAttribute('cy', String(point.y));
163
- dot.style.display = '';
164
- }
165
- } else if (dot) {
166
- dot.style.display = 'none';
167
- }
168
- }
169
-
170
- if (crosshair) {
171
- crosshair.setAttribute('x1', String(snappedX));
172
- crosshair.setAttribute('x2', String(snappedX));
173
- crosshair.style.display = '';
174
- }
175
-
176
- const tooltip = buildSliceTooltip(hits);
177
- if (!tooltip) return false;
178
-
179
- const containerAnchorX = snappedX * viewBoxToContainer;
180
- const containerAnchorY = anchorCount > 0 ? (anchorY / anchorCount) * viewBoxToContainer : 0;
181
- tooltipManager.show(tooltip, containerAnchorX, containerAnchorY, {
182
- placement: 'right',
183
- });
184
- return true;
185
- };
186
-
187
- const toSvgCoords = (clientX: number, clientY: number) => {
188
- const svgEl = svg as unknown as SVGSVGElement;
189
- const svgRect = svgEl.getBoundingClientRect();
190
- const viewBox = svgEl.viewBox?.baseVal;
191
- const scaleX = viewBox?.width && svgRect.width ? viewBox.width / svgRect.width : 1;
192
- const scaleY = viewBox?.height && svgRect.height ? viewBox.height / svgRect.height : 1;
193
- const svgX = (clientX - svgRect.left) * scaleX;
194
- const svgY = (clientY - svgRect.top) * scaleY;
195
- const viewBoxToContainer = scaleX > 0 ? 1 / scaleX : 1;
196
- return { svgX, svgY, viewBoxToContainer };
197
- };
198
-
199
- const handleMouseMove = (e: Event) => {
200
- const me = e as MouseEvent;
201
- const { svgX, svgY, viewBoxToContainer } = toSvgCoords(me.clientX, me.clientY);
202
- positionAt(svgX, svgY, viewBoxToContainer);
203
- };
204
-
205
- const hideAll = () => {
206
- if (crosshair) crosshair.style.display = 'none';
207
- for (const dot of dots) dot.style.display = 'none';
208
- tooltipManager.hide();
209
- };
210
-
211
- const handleTouch = (e: Event) => {
212
- const te = e as TouchEvent;
213
- if (te.touches.length === 0) return;
214
- const t = te.touches[0];
215
- const { svgX, svgY, viewBoxToContainer } = toSvgCoords(t.clientX, t.clientY);
216
- if (te.cancelable) te.preventDefault();
217
- positionAt(svgX, svgY, viewBoxToContainer);
218
- };
219
-
220
- overlay.addEventListener('mousemove', handleMouseMove);
221
- overlay.addEventListener('mouseleave', hideAll);
222
- overlay.addEventListener('touchstart', handleTouch, { passive: false });
223
- overlay.addEventListener('touchmove', handleTouch, { passive: false });
224
- overlay.addEventListener('touchend', hideAll);
225
-
226
- return () => {
227
- overlay.removeEventListener('mousemove', handleMouseMove);
228
- overlay.removeEventListener('mouseleave', hideAll);
229
- overlay.removeEventListener('touchstart', handleTouch);
230
- overlay.removeEventListener('touchmove', handleTouch);
231
- overlay.removeEventListener('touchend', hideAll);
232
- };
233
- }
@@ -1,175 +0,0 @@
1
- /**
2
- * Reusable drag handler for SVG elements.
3
- * Handles mouse and touch events, viewBox scaling, threshold detection,
4
- * click suppression after drag, and cursor state.
5
- */
6
-
7
- export interface DragConfig {
8
- element: SVGElement;
9
- svg: SVGSVGElement;
10
- onMove: (dx: number, dy: number) => void;
11
- onEnd: (dx: number, dy: number, moved: boolean) => void;
12
- setDragging: (dragging: boolean) => void;
13
- threshold?: number;
14
- }
15
-
16
- export function createDragHandler(config: DragConfig): () => void {
17
- const { element, svg, onMove, onEnd, setDragging, threshold = 3 } = config;
18
- const cleanups: Array<() => void> = [];
19
-
20
- let activeDocMouseMove: ((e: MouseEvent) => void) | null = null;
21
- let activeDocMouseUp: ((e: MouseEvent) => void) | null = null;
22
- let activeDocTouchMove: ((e: TouchEvent) => void) | null = null;
23
- let activeDocTouchEnd: ((e: TouchEvent) => void) | null = null;
24
- let activeDocTouchCancel: ((e: TouchEvent) => void) | null = null;
25
-
26
- function getScale(): { scaleX: number; scaleY: number } {
27
- const viewBox = svg.viewBox?.baseVal;
28
- const svgRect = svg.getBoundingClientRect();
29
- return {
30
- scaleX: viewBox?.width && svgRect.width ? viewBox.width / svgRect.width : 1,
31
- scaleY: viewBox?.height && svgRect.height ? viewBox.height / svgRect.height : 1,
32
- };
33
- }
34
-
35
- function startDrag(startX: number, startY: number): void {
36
- setDragging(true);
37
- const { scaleX, scaleY } = getScale();
38
-
39
- element.style.cursor = 'grabbing';
40
- svg.style.userSelect = 'none';
41
-
42
- const handleMove = (clientX: number, clientY: number) => {
43
- const dx = (clientX - startX) * scaleX;
44
- const dy = (clientY - startY) * scaleY;
45
- onMove(dx, dy);
46
- };
47
-
48
- const cleanupDocListeners = () => {
49
- if (activeDocMouseMove) {
50
- document.removeEventListener('mousemove', activeDocMouseMove);
51
- activeDocMouseMove = null;
52
- }
53
- if (activeDocMouseUp) {
54
- document.removeEventListener('mouseup', activeDocMouseUp);
55
- activeDocMouseUp = null;
56
- }
57
- if (activeDocTouchMove) {
58
- document.removeEventListener('touchmove', activeDocTouchMove);
59
- activeDocTouchMove = null;
60
- }
61
- if (activeDocTouchEnd) {
62
- document.removeEventListener('touchend', activeDocTouchEnd);
63
- activeDocTouchEnd = null;
64
- }
65
- if (activeDocTouchCancel) {
66
- document.removeEventListener('touchcancel', activeDocTouchCancel);
67
- activeDocTouchCancel = null;
68
- }
69
- };
70
-
71
- const handleEnd = (clientX: number, clientY: number) => {
72
- const dx = (clientX - startX) * scaleX;
73
- const dy = (clientY - startY) * scaleY;
74
- const moved = Math.abs(dx) > threshold || Math.abs(dy) > threshold;
75
-
76
- onEnd(dx, dy, moved);
77
-
78
- if (moved) {
79
- element.addEventListener(
80
- 'click',
81
- (clickE) => {
82
- clickE.stopPropagation();
83
- },
84
- { capture: true, once: true },
85
- );
86
- }
87
-
88
- element.style.cursor = 'grab';
89
- svg.style.userSelect = '';
90
-
91
- cleanupDocListeners();
92
- setDragging(false);
93
- };
94
-
95
- const onMouseMove = (moveEvent: MouseEvent) => {
96
- handleMove(moveEvent.clientX, moveEvent.clientY);
97
- };
98
- const onMouseUp = (upEvent: MouseEvent) => {
99
- handleEnd(upEvent.clientX, upEvent.clientY);
100
- };
101
- document.addEventListener('mousemove', onMouseMove);
102
- document.addEventListener('mouseup', onMouseUp);
103
- activeDocMouseMove = onMouseMove;
104
- activeDocMouseUp = onMouseUp;
105
-
106
- const onTouchMove = (moveEvent: TouchEvent) => {
107
- if (moveEvent.touches.length > 0) {
108
- moveEvent.preventDefault();
109
- handleMove(moveEvent.touches[0].clientX, moveEvent.touches[0].clientY);
110
- }
111
- };
112
- const onTouchEnd = (endEvent: TouchEvent) => {
113
- const touch = endEvent.changedTouches[0];
114
- if (touch) {
115
- handleEnd(touch.clientX, touch.clientY);
116
- } else {
117
- handleEnd(startX, startY);
118
- }
119
- };
120
- document.addEventListener('touchmove', onTouchMove, { passive: false });
121
- document.addEventListener('touchend', onTouchEnd);
122
- document.addEventListener('touchcancel', onTouchEnd);
123
- activeDocTouchMove = onTouchMove;
124
- activeDocTouchEnd = onTouchEnd;
125
- activeDocTouchCancel = onTouchEnd;
126
- }
127
-
128
- const handleMouseDown = (e: Event) => {
129
- const mouseEvent = e as MouseEvent;
130
- mouseEvent.preventDefault();
131
- startDrag(mouseEvent.clientX, mouseEvent.clientY);
132
- };
133
-
134
- const handleTouchStart = (e: Event) => {
135
- const touchEvent = e as TouchEvent;
136
- if (touchEvent.touches.length === 1) {
137
- touchEvent.preventDefault();
138
- startDrag(touchEvent.touches[0].clientX, touchEvent.touches[0].clientY);
139
- }
140
- };
141
-
142
- element.addEventListener('mousedown', handleMouseDown);
143
- element.addEventListener('touchstart', handleTouchStart, { passive: false });
144
- cleanups.push(() => {
145
- element.removeEventListener('mousedown', handleMouseDown);
146
- element.removeEventListener('touchstart', handleTouchStart);
147
- });
148
-
149
- return () => {
150
- for (const cleanup of cleanups) {
151
- cleanup();
152
- }
153
- if (activeDocMouseMove) {
154
- document.removeEventListener('mousemove', activeDocMouseMove);
155
- activeDocMouseMove = null;
156
- }
157
- if (activeDocMouseUp) {
158
- document.removeEventListener('mouseup', activeDocMouseUp);
159
- activeDocMouseUp = null;
160
- }
161
- if (activeDocTouchMove) {
162
- document.removeEventListener('touchmove', activeDocTouchMove);
163
- activeDocTouchMove = null;
164
- }
165
- if (activeDocTouchEnd) {
166
- document.removeEventListener('touchend', activeDocTouchEnd);
167
- activeDocTouchEnd = null;
168
- }
169
- if (activeDocTouchCancel) {
170
- document.removeEventListener('touchcancel', activeDocTouchCancel);
171
- activeDocTouchCancel = null;
172
- }
173
- svg.style.userSelect = '';
174
- };
175
- }