@opendata-ai/openchart-vanilla 6.28.6 → 7.0.2
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 +13 -8
- package/dist/index.js +2800 -2349
- package/dist/index.js.map +1 -1
- package/dist/styles.css +1 -1
- package/package.json +3 -3
- package/src/__tests__/crosshair.test.ts +11 -2
- package/src/__tests__/events.test.ts +55 -10
- package/src/barlist-mount.ts +15 -1
- package/src/graph/__tests__/canvas-renderer.test.ts +1 -0
- package/src/interactions/chart-events.ts +139 -0
- package/src/interactions/crosshair.ts +233 -0
- package/src/interactions/drag-handler.ts +175 -0
- package/src/interactions/editing-drags.ts +512 -0
- package/src/interactions/index.ts +25 -0
- package/src/interactions/keyboard-nav.ts +111 -0
- package/src/interactions/legend-interaction.ts +38 -0
- package/src/interactions/selection.ts +271 -0
- package/src/interactions/tooltip-events.ts +72 -0
- package/src/mount.ts +182 -1761
- package/src/renderers/annotations.ts +82 -2
- package/src/renderers/axes.ts +18 -1
- package/src/renderers/brand.ts +7 -1
- package/src/renderers/chrome.ts +50 -3
- package/src/renderers/endpoint-labels.ts +164 -0
- package/src/renderers/legend.ts +32 -27
- package/src/renderers/marks.ts +65 -17
- package/src/renderers/metrics.ts +50 -0
- package/src/svg-renderer.ts +80 -20
- package/src/tilemap-mount.ts +6 -6
- package/src/tilemap-renderer.ts +0 -2
- package/src/tooltip.ts +27 -7
|
@@ -0,0 +1,175 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,512 @@
|
|
|
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('polygon.oc-annotation-connector');
|
|
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
|
+
const arrowhead = annotationG.querySelector('polygon.oc-annotation-connector');
|
|
146
|
+
const points = arrowhead?.getAttribute('points') ?? '';
|
|
147
|
+
const firstPoint = points.split(' ')[0] ?? '0,0';
|
|
148
|
+
const [px, py] = firstPoint.split(',');
|
|
149
|
+
toX = Number(px) || 0;
|
|
150
|
+
toY = Number(py) || 0;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const endpoints: Array<{ name: 'from' | 'to'; cx: number; cy: number }> = [
|
|
154
|
+
{ name: 'from', cx: fromX, cy: fromY },
|
|
155
|
+
{ name: 'to', cx: toX, cy: toY },
|
|
156
|
+
];
|
|
157
|
+
|
|
158
|
+
const createdHandles: SVGCircleElement[] = [];
|
|
159
|
+
|
|
160
|
+
for (const ep of endpoints) {
|
|
161
|
+
if (!Number.isFinite(ep.cx) || !Number.isFinite(ep.cy)) continue;
|
|
162
|
+
|
|
163
|
+
const handleEl = document.createElementNS(SVG_NS, 'circle') as SVGCircleElement;
|
|
164
|
+
handleEl.setAttribute('class', 'oc-connector-handle');
|
|
165
|
+
handleEl.setAttribute('data-endpoint', ep.name);
|
|
166
|
+
handleEl.setAttribute('cx', String(ep.cx));
|
|
167
|
+
handleEl.setAttribute('cy', String(ep.cy));
|
|
168
|
+
handleEl.setAttribute('r', '4');
|
|
169
|
+
handleEl.setAttribute('opacity', '0');
|
|
170
|
+
handleEl.setAttribute('fill', 'currentColor');
|
|
171
|
+
handleEl.setAttribute('stroke', 'currentColor');
|
|
172
|
+
annotationG.appendChild(handleEl);
|
|
173
|
+
createdHandles.push(handleEl);
|
|
174
|
+
|
|
175
|
+
const origCx = ep.cx;
|
|
176
|
+
const origCy = ep.cy;
|
|
177
|
+
|
|
178
|
+
const stopProp = (e: Event) => {
|
|
179
|
+
e.stopPropagation();
|
|
180
|
+
};
|
|
181
|
+
handleEl.addEventListener('mousedown', stopProp);
|
|
182
|
+
handleEl.addEventListener('touchstart', stopProp);
|
|
183
|
+
cleanups.push(() => {
|
|
184
|
+
handleEl.removeEventListener('mousedown', stopProp);
|
|
185
|
+
handleEl.removeEventListener('touchstart', stopProp);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
const cleanup = createDragHandler({
|
|
189
|
+
element: handleEl,
|
|
190
|
+
svg: svg as unknown as SVGSVGElement,
|
|
191
|
+
onMove: (dx, dy) => {
|
|
192
|
+
handleEl.setAttribute('cx', String(origCx + dx));
|
|
193
|
+
handleEl.setAttribute('cy', String(origCy + dy));
|
|
194
|
+
|
|
195
|
+
if (connectorLine) {
|
|
196
|
+
if (ep.name === 'from') {
|
|
197
|
+
connectorLine.setAttribute('x1', String(origCx + dx));
|
|
198
|
+
connectorLine.setAttribute('y1', String(origCy + dy));
|
|
199
|
+
} else {
|
|
200
|
+
connectorLine.setAttribute('x2', String(origCx + dx));
|
|
201
|
+
connectorLine.setAttribute('y2', String(origCy + dy));
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
},
|
|
205
|
+
onEnd: (dx, dy, moved) => {
|
|
206
|
+
handleEl.setAttribute('cx', String(origCx));
|
|
207
|
+
handleEl.setAttribute('cy', String(origCy));
|
|
208
|
+
|
|
209
|
+
if (connectorLine) {
|
|
210
|
+
if (ep.name === 'from') {
|
|
211
|
+
connectorLine.setAttribute('x1', String(origCx));
|
|
212
|
+
connectorLine.setAttribute('y1', String(origCy));
|
|
213
|
+
} else {
|
|
214
|
+
connectorLine.setAttribute('x2', String(origCx));
|
|
215
|
+
connectorLine.setAttribute('y2', String(origCy));
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (moved) {
|
|
220
|
+
const existingOffset = textAnnotation.connectorOffset?.[ep.name];
|
|
221
|
+
const origEndDx = existingOffset?.dx ?? 0;
|
|
222
|
+
const origEndDy = existingOffset?.dy ?? 0;
|
|
223
|
+
onEdit({
|
|
224
|
+
type: 'annotation-connector',
|
|
225
|
+
annotation: textAnnotation,
|
|
226
|
+
endpoint: ep.name,
|
|
227
|
+
offset: { dx: origEndDx + dx, dy: origEndDy + dy },
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
},
|
|
231
|
+
setDragging,
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
cleanups.push(cleanup);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const showHandles = () => {
|
|
238
|
+
for (const h of createdHandles) {
|
|
239
|
+
h.setAttribute('opacity', '0.6');
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
const hideHandles = () => {
|
|
243
|
+
for (const h of createdHandles) {
|
|
244
|
+
h.setAttribute('opacity', '0');
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
annotationG.addEventListener('mouseenter', showHandles);
|
|
249
|
+
annotationG.addEventListener('mouseleave', hideHandles);
|
|
250
|
+
cleanups.push(() => {
|
|
251
|
+
annotationG.removeEventListener('mouseenter', showHandles);
|
|
252
|
+
annotationG.removeEventListener('mouseleave', hideHandles);
|
|
253
|
+
for (const h of createdHandles) {
|
|
254
|
+
h.remove();
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return () => {
|
|
260
|
+
for (const cleanup of cleanups) {
|
|
261
|
+
cleanup();
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Wire drag on range and refline annotation labels.
|
|
268
|
+
* Returns a cleanup function.
|
|
269
|
+
*/
|
|
270
|
+
export function wireAnnotationLabelDrag(
|
|
271
|
+
svg: SVGElement,
|
|
272
|
+
specAnnotations: Annotation[],
|
|
273
|
+
onEdit: (edit: ElementEdit) => void,
|
|
274
|
+
setDragging: (dragging: boolean) => void,
|
|
275
|
+
): () => void {
|
|
276
|
+
const cleanups: Array<() => void> = [];
|
|
277
|
+
|
|
278
|
+
const selectors = [
|
|
279
|
+
'.oc-annotation-range .oc-annotation-label',
|
|
280
|
+
'.oc-annotation-refline .oc-annotation-label',
|
|
281
|
+
];
|
|
282
|
+
|
|
283
|
+
for (const selector of selectors) {
|
|
284
|
+
const labels = svg.querySelectorAll(selector);
|
|
285
|
+
|
|
286
|
+
for (const label of labels) {
|
|
287
|
+
const annotationG = label.closest('.oc-annotation') as SVGGElement | null;
|
|
288
|
+
if (!annotationG) continue;
|
|
289
|
+
|
|
290
|
+
const indexStr = annotationG.getAttribute('data-annotation-index');
|
|
291
|
+
if (indexStr === null) continue;
|
|
292
|
+
|
|
293
|
+
const index = Number(indexStr);
|
|
294
|
+
const specAnnotation = specAnnotations[index];
|
|
295
|
+
if (!specAnnotation) continue;
|
|
296
|
+
|
|
297
|
+
const labelEl = label as SVGTextElement;
|
|
298
|
+
labelEl.style.cursor = 'grab';
|
|
299
|
+
|
|
300
|
+
const isRange = specAnnotation.type === 'range';
|
|
301
|
+
const existingLabelOffset = isRange
|
|
302
|
+
? (specAnnotation as RangeAnnotation).labelOffset
|
|
303
|
+
: (specAnnotation as RefLineAnnotation).labelOffset;
|
|
304
|
+
const origLabelDx = existingLabelOffset?.dx ?? 0;
|
|
305
|
+
const origLabelDy = existingLabelOffset?.dy ?? 0;
|
|
306
|
+
|
|
307
|
+
const cleanup = createDragHandler({
|
|
308
|
+
element: labelEl,
|
|
309
|
+
svg: svg as unknown as SVGSVGElement,
|
|
310
|
+
onMove: (dx, dy) => {
|
|
311
|
+
(labelEl as SVGElement & ElementCSSInlineStyle).style.transform =
|
|
312
|
+
`translate(${dx}px, ${dy}px)`;
|
|
313
|
+
},
|
|
314
|
+
onEnd: (dx, dy, moved) => {
|
|
315
|
+
(labelEl as SVGElement & ElementCSSInlineStyle).style.transform = '';
|
|
316
|
+
|
|
317
|
+
if (moved) {
|
|
318
|
+
if (isRange) {
|
|
319
|
+
onEdit({
|
|
320
|
+
type: 'range-label',
|
|
321
|
+
annotation: specAnnotation as RangeAnnotation,
|
|
322
|
+
labelOffset: { dx: origLabelDx + dx, dy: origLabelDy + dy },
|
|
323
|
+
});
|
|
324
|
+
} else {
|
|
325
|
+
onEdit({
|
|
326
|
+
type: 'refline-label',
|
|
327
|
+
annotation: specAnnotation as RefLineAnnotation,
|
|
328
|
+
labelOffset: { dx: origLabelDx + dx, dy: origLabelDy + dy },
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
},
|
|
333
|
+
setDragging,
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
cleanups.push(cleanup);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
return () => {
|
|
341
|
+
for (const cleanup of cleanups) {
|
|
342
|
+
cleanup();
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Wire drag on chrome text elements (title, subtitle, source, byline, footer).
|
|
349
|
+
* Returns a cleanup function.
|
|
350
|
+
*/
|
|
351
|
+
export function wireChromeDrag(
|
|
352
|
+
svg: SVGElement,
|
|
353
|
+
spec: ChartSpec | GraphSpec,
|
|
354
|
+
onEdit: (edit: ElementEdit) => void,
|
|
355
|
+
setDragging: (dragging: boolean) => void,
|
|
356
|
+
): () => void {
|
|
357
|
+
const chromeTexts = svg.querySelectorAll('.oc-chrome text[data-chrome-key]');
|
|
358
|
+
const cleanups: Array<() => void> = [];
|
|
359
|
+
|
|
360
|
+
const chromeConfig = 'chrome' in spec ? spec.chrome : undefined;
|
|
361
|
+
|
|
362
|
+
for (const el of chromeTexts) {
|
|
363
|
+
const textEl = el as SVGTextElement;
|
|
364
|
+
const key = textEl.getAttribute('data-chrome-key') as ChromeKey;
|
|
365
|
+
if (!key) continue;
|
|
366
|
+
|
|
367
|
+
const chromeEntry = chromeConfig?.[key];
|
|
368
|
+
const existingOffset =
|
|
369
|
+
typeof chromeEntry === 'object' && chromeEntry !== null ? chromeEntry.offset : undefined;
|
|
370
|
+
const origChromeDx = existingOffset?.dx ?? 0;
|
|
371
|
+
const origChromeDy = existingOffset?.dy ?? 0;
|
|
372
|
+
|
|
373
|
+
textEl.style.cursor = 'grab';
|
|
374
|
+
|
|
375
|
+
const cleanup = createDragHandler({
|
|
376
|
+
element: textEl,
|
|
377
|
+
svg: svg as unknown as SVGSVGElement,
|
|
378
|
+
onMove: (dx, dy) => {
|
|
379
|
+
(textEl as SVGElement & ElementCSSInlineStyle).style.transform =
|
|
380
|
+
`translate(${dx}px, ${dy}px)`;
|
|
381
|
+
},
|
|
382
|
+
onEnd: (dx, dy, moved) => {
|
|
383
|
+
(textEl as SVGElement & ElementCSSInlineStyle).style.transform = '';
|
|
384
|
+
|
|
385
|
+
if (moved) {
|
|
386
|
+
onEdit({
|
|
387
|
+
type: 'chrome',
|
|
388
|
+
key,
|
|
389
|
+
text: textEl.textContent ?? '',
|
|
390
|
+
offset: { dx: origChromeDx + dx, dy: origChromeDy + dy },
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
},
|
|
394
|
+
setDragging,
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
cleanups.push(cleanup);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
return () => {
|
|
401
|
+
for (const cleanup of cleanups) {
|
|
402
|
+
cleanup();
|
|
403
|
+
}
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* Wire drag on the legend group.
|
|
409
|
+
* Returns a cleanup function.
|
|
410
|
+
*/
|
|
411
|
+
export function wireLegendDrag(
|
|
412
|
+
svg: SVGElement,
|
|
413
|
+
spec: ChartSpec | GraphSpec,
|
|
414
|
+
onEdit: (edit: ElementEdit) => void,
|
|
415
|
+
setDragging: (dragging: boolean) => void,
|
|
416
|
+
): () => void {
|
|
417
|
+
const legendG = svg.querySelector('.oc-legend') as SVGGElement | null;
|
|
418
|
+
if (!legendG) return () => {};
|
|
419
|
+
|
|
420
|
+
const cleanups: Array<() => void> = [];
|
|
421
|
+
|
|
422
|
+
const legendConfig = 'legend' in spec ? spec.legend : undefined;
|
|
423
|
+
const origLegendDx = legendConfig?.offset?.dx ?? 0;
|
|
424
|
+
const origLegendDy = legendConfig?.offset?.dy ?? 0;
|
|
425
|
+
|
|
426
|
+
legendG.style.cursor = 'grab';
|
|
427
|
+
|
|
428
|
+
const cleanup = createDragHandler({
|
|
429
|
+
element: legendG,
|
|
430
|
+
svg: svg as unknown as SVGSVGElement,
|
|
431
|
+
onMove: (dx, dy) => {
|
|
432
|
+
(legendG as SVGElement & ElementCSSInlineStyle).style.transform =
|
|
433
|
+
`translate(${dx}px, ${dy}px)`;
|
|
434
|
+
},
|
|
435
|
+
onEnd: (dx, dy, moved) => {
|
|
436
|
+
(legendG as SVGElement & ElementCSSInlineStyle).style.transform = '';
|
|
437
|
+
|
|
438
|
+
if (moved) {
|
|
439
|
+
onEdit({ type: 'legend', offset: { dx: origLegendDx + dx, dy: origLegendDy + dy } });
|
|
440
|
+
}
|
|
441
|
+
},
|
|
442
|
+
setDragging,
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
cleanups.push(cleanup);
|
|
446
|
+
|
|
447
|
+
return () => {
|
|
448
|
+
for (const cleanup of cleanups) {
|
|
449
|
+
cleanup();
|
|
450
|
+
}
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* Wire drag on series label elements (.oc-mark-label[data-series]).
|
|
456
|
+
* Returns a cleanup function.
|
|
457
|
+
*/
|
|
458
|
+
export function wireSeriesLabelDrag(
|
|
459
|
+
svg: SVGElement,
|
|
460
|
+
spec: ChartSpec | GraphSpec,
|
|
461
|
+
onEdit: (edit: ElementEdit) => void,
|
|
462
|
+
setDragging: (dragging: boolean) => void,
|
|
463
|
+
): () => void {
|
|
464
|
+
const labels = svg.querySelectorAll('.oc-mark-label');
|
|
465
|
+
const cleanups: Array<() => void> = [];
|
|
466
|
+
|
|
467
|
+
const rawLabels = 'labels' in spec ? spec.labels : undefined;
|
|
468
|
+
const labelsConfig = typeof rawLabels === 'object' ? rawLabels : undefined;
|
|
469
|
+
|
|
470
|
+
for (const label of labels) {
|
|
471
|
+
const labelEl = label as SVGTextElement;
|
|
472
|
+
const series =
|
|
473
|
+
labelEl.getAttribute('data-series') ??
|
|
474
|
+
labelEl.closest('[data-series]')?.getAttribute('data-series');
|
|
475
|
+
if (!series) continue;
|
|
476
|
+
|
|
477
|
+
const existingSeriesOffset = labelsConfig?.offsets?.[series];
|
|
478
|
+
const origSeriesDx = existingSeriesOffset?.dx ?? 0;
|
|
479
|
+
const origSeriesDy = existingSeriesOffset?.dy ?? 0;
|
|
480
|
+
|
|
481
|
+
labelEl.style.cursor = 'grab';
|
|
482
|
+
|
|
483
|
+
const cleanup = createDragHandler({
|
|
484
|
+
element: labelEl,
|
|
485
|
+
svg: svg as unknown as SVGSVGElement,
|
|
486
|
+
onMove: (dx, dy) => {
|
|
487
|
+
(labelEl as SVGElement & ElementCSSInlineStyle).style.transform =
|
|
488
|
+
`translate(${dx}px, ${dy}px)`;
|
|
489
|
+
},
|
|
490
|
+
onEnd: (dx, dy, moved) => {
|
|
491
|
+
(labelEl as SVGElement & ElementCSSInlineStyle).style.transform = '';
|
|
492
|
+
|
|
493
|
+
if (moved) {
|
|
494
|
+
onEdit({
|
|
495
|
+
type: 'series-label',
|
|
496
|
+
series,
|
|
497
|
+
offset: { dx: origSeriesDx + dx, dy: origSeriesDy + dy },
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
},
|
|
501
|
+
setDragging,
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
cleanups.push(cleanup);
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
return () => {
|
|
508
|
+
for (const cleanup of cleanups) {
|
|
509
|
+
cleanup();
|
|
510
|
+
}
|
|
511
|
+
};
|
|
512
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
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';
|