@statelyai/flow 0.13.4 → 0.15.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/README.md +5 -1
- package/dist/esm/index.d.ts +13 -2
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +39 -14
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/layout/dagre.d.ts +2 -1
- package/dist/esm/layout/dagre.d.ts.map +1 -0
- package/dist/esm/layout/dagre.js +2 -0
- package/dist/esm/layout/dagre.js.map +1 -0
- package/dist/esm/layout/elk.d.ts +2 -1
- package/dist/esm/layout/elk.d.ts.map +1 -0
- package/dist/esm/layout/elk.js +2 -0
- package/dist/esm/layout/elk.js.map +1 -0
- package/dist/esm/layout/stately.d.ts +2 -1
- package/dist/esm/layout/stately.d.ts.map +1 -0
- package/dist/esm/layout/stately.js +2 -0
- package/dist/esm/layout/stately.js.map +1 -0
- package/dist/esm/shared.d.ts +2 -1
- package/dist/esm/shared.d.ts.map +1 -0
- package/dist/esm/shared.js +2 -0
- package/dist/esm/shared.js.map +1 -0
- package/package.json +4 -3
- package/src/__fixtures__/nested.ts +106 -0
- package/src/appearances.test.ts +428 -0
- package/src/appearances.ts +144 -0
- package/src/authoritative-route.ts +14 -0
- package/src/changeSet.test.ts +132 -0
- package/src/changeSet.ts +337 -0
- package/src/config.ts +41 -0
- package/src/configure.test.ts +212 -0
- package/src/connection-lifetime.ts +1 -0
- package/src/connectionPreview.test.ts +102 -0
- package/src/connectionPreview.ts +82 -0
- package/src/content.ts +203 -0
- package/src/createFlow.fit-frame.test.ts +256 -0
- package/src/createFlow.modern.test.ts +743 -0
- package/src/createFlow.test.ts +632 -0
- package/src/createFlow.ts +3463 -0
- package/src/createFlow.view.test.ts +212 -0
- package/src/createFlow.viewport-controls.test.ts +221 -0
- package/src/edge-geometry.ts +318 -0
- package/src/engine.cache.test.ts +728 -0
- package/src/engine.hierarchy.test.ts +1023 -0
- package/src/engine.test.ts +1965 -0
- package/src/engine.ts +3385 -0
- package/src/entity-changes.test.ts +309 -0
- package/src/entity-changes.ts +395 -0
- package/src/examples/flow-based-math.test.ts +25 -0
- package/src/examples/flow-based-math.ts +210 -0
- package/src/external-edit.test.ts +337 -0
- package/src/external-edit.ts +358 -0
- package/src/factories.test.ts +104 -0
- package/src/factories.ts +70 -0
- package/src/geometry.ts +13 -0
- package/src/graph-index.test.ts +56 -0
- package/src/graph-index.ts +194 -0
- package/src/hierarchy.test.ts +175 -0
- package/src/hierarchy.ts +330 -0
- package/src/highlights.test.ts +130 -0
- package/src/index.ts +469 -0
- package/src/input/index.ts +615 -0
- package/src/input/input.test.ts +2577 -0
- package/src/input/keymap.ts +111 -0
- package/src/input/modes.ts +1715 -0
- package/src/input/reparent.test.ts +214 -0
- package/src/layout/dagre.ts +138 -0
- package/src/layout/elk.ts +1382 -0
- package/src/layout/index.ts +12 -0
- package/src/layout/layout.test.ts +1248 -0
- package/src/layout/shared.ts +260 -0
- package/src/layout/stately-globals.d.ts +9 -0
- package/src/layout/stately.test.ts +246 -0
- package/src/layout/stately.ts +528 -0
- package/src/layout-animation.test.ts +223 -0
- package/src/layout-contract.test.ts +414 -0
- package/src/marker.test.ts +23 -0
- package/src/marker.ts +23 -0
- package/src/measurement-anchor.test.ts +232 -0
- package/src/minimap.test.ts +96 -0
- package/src/minimap.ts +137 -0
- package/src/nested-fixture.test.ts +178 -0
- package/src/port-measurement.test.ts +27 -0
- package/src/port-measurements.test.ts +187 -0
- package/src/profile.ts +124 -0
- package/src/projections.test.ts +103 -0
- package/src/projections.ts +295 -0
- package/src/render-invalidation.test.ts +62 -0
- package/src/render-invalidation.ts +55 -0
- package/src/renderer-geometry.test.ts +356 -0
- package/src/renderer-transactions.test.ts +234 -0
- package/src/routing/avoid.ts +417 -0
- package/src/routing/index.test.ts +361 -0
- package/src/routing/index.ts +366 -0
- package/src/routing/readTracking.test.ts +103 -0
- package/src/routing/readTracking.ts +233 -0
- package/src/selection-commands.test.ts +355 -0
- package/src/serialize.test.ts +167 -0
- package/src/serialize.ts +254 -0
- package/src/shapes/index.test.ts +45 -0
- package/src/shapes/index.ts +142 -0
- package/src/snap.test.ts +123 -0
- package/src/snap.ts +395 -0
- package/src/spatial.test.ts +676 -0
- package/src/spatial.ts +457 -0
- package/src/store/index.ts +4121 -0
- package/src/store.test.ts +2903 -0
- package/src/toolbar.test.ts +31 -0
- package/src/toolbar.ts +62 -0
- package/src/types.test.ts +15 -0
- package/src/types.ts +487 -0
- package/src/validation.ts +558 -0
- package/src/view.ts +172 -0
- package/src/viewport/animate.ts +285 -0
- package/src/viewport/index.ts +296 -0
- package/src/viewport.test.ts +569 -0
package/src/spatial.ts
ADDED
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
import type { FlowEdge, FlowNode, Point, Rect, Size, Viewport } from './types';
|
|
2
|
+
import { authoritativeEdgePoints } from './authoritative-route';
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
// Spatial helpers
|
|
6
|
+
//
|
|
7
|
+
// Pure functions for resolved positions, bounds, and hit testing.
|
|
8
|
+
// No dependencies on store or engine — safe to import anywhere.
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
/** Visual position: layout position + drag offset. */
|
|
12
|
+
export function visualPosition(node: FlowNode): Point {
|
|
13
|
+
return {
|
|
14
|
+
x: node.x + (node.dx ?? 0),
|
|
15
|
+
y: node.y + (node.dy ?? 0),
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** @deprecated Use `visualPosition`. */
|
|
20
|
+
export const resolvedPosition = visualPosition;
|
|
21
|
+
|
|
22
|
+
/** Resolved visual bounds: layout position + drag offset + dimensions.
|
|
23
|
+
* Uses style.customWidth/customHeight when set (from resize), otherwise inherent width/height. */
|
|
24
|
+
/** Size the geometry should use, in priority order:
|
|
25
|
+
* 1. `style.customWidth/customHeight` (an explicit resize override),
|
|
26
|
+
* 2. for `autoSize` nodes, the DOM-`measured` size (the renderer owns it),
|
|
27
|
+
* 3. the authored `width/height`.
|
|
28
|
+
* Authored `width/height` are never mutated — measured size lives on
|
|
29
|
+
* `node.measured`, so this is the single place that reconciles the two. */
|
|
30
|
+
export function resolvedSize(node: FlowNode): Size {
|
|
31
|
+
const measuredAppearance = node.measured?.appearanceKey !== undefined && node.measured.appearanceKey === JSON.stringify(node.appearance ?? null);
|
|
32
|
+
const autoSize = node.autoSize ?? (measuredAppearance && node.measured?.appearanceAutoSize);
|
|
33
|
+
return {
|
|
34
|
+
width:
|
|
35
|
+
node.style?.customWidth ??
|
|
36
|
+
(autoSize ? node.measured?.width ?? node.width : node.width),
|
|
37
|
+
height:
|
|
38
|
+
node.style?.customHeight ??
|
|
39
|
+
(autoSize ? node.measured?.height ?? node.height : node.height),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function visualBounds(node: FlowNode): Rect {
|
|
44
|
+
const size = resolvedSize(node);
|
|
45
|
+
return {
|
|
46
|
+
x: node.x + (node.dx ?? 0),
|
|
47
|
+
y: node.y + (node.dy ?? 0),
|
|
48
|
+
width: size.width,
|
|
49
|
+
height: size.height,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** @deprecated Use `visualBounds`. */
|
|
54
|
+
export const resolvedBounds = visualBounds;
|
|
55
|
+
|
|
56
|
+
/** Bounds used by marquee selection, including transient node movement. */
|
|
57
|
+
export function resolvedSelectionBounds(node: FlowNode): Rect {
|
|
58
|
+
if (!node.selectionBounds) return visualBounds(node);
|
|
59
|
+
return {
|
|
60
|
+
...node.selectionBounds,
|
|
61
|
+
x: node.selectionBounds.x + (node.dx ?? 0),
|
|
62
|
+
y: node.selectionBounds.y + (node.dy ?? 0),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Size used for edge-label geometry. `autoSize` labels retain authored
|
|
68
|
+
* dimensions as a fallback while a transient DOM measurement owns the live
|
|
69
|
+
* size. The measurement never becomes authored graph data.
|
|
70
|
+
*/
|
|
71
|
+
export function resolvedEdgeLabelSize(
|
|
72
|
+
label: NonNullable<FlowEdge['label']>,
|
|
73
|
+
measurement?: Rect,
|
|
74
|
+
): Size {
|
|
75
|
+
return label.autoSize && measurement
|
|
76
|
+
? { width: measurement.width, height: measurement.height }
|
|
77
|
+
: { width: label.width, height: label.height };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Fixed-position visual bounds for an edge label. */
|
|
81
|
+
export function resolvedEdgeLabelBounds(
|
|
82
|
+
edge: FlowEdge,
|
|
83
|
+
measurement?: Rect,
|
|
84
|
+
): Rect | null {
|
|
85
|
+
if (!edge.label) return null;
|
|
86
|
+
const size = resolvedEdgeLabelSize(edge.label, measurement);
|
|
87
|
+
return {
|
|
88
|
+
x: edge.label.x + (edge.label.dx ?? 0),
|
|
89
|
+
y: edge.label.y + (edge.label.dy ?? 0),
|
|
90
|
+
...size,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Visual bounds with grid snap and/or entity snap-line offset. */
|
|
95
|
+
export function snappedBounds(
|
|
96
|
+
node: FlowNode,
|
|
97
|
+
grid: [number, number] | null,
|
|
98
|
+
/** Optional snap offset from entity snap lines (snapRect.x - selectionRect.x) */
|
|
99
|
+
snapOffset?: Point | null,
|
|
100
|
+
): Rect {
|
|
101
|
+
let x = node.x + (node.dx ?? 0);
|
|
102
|
+
let y = node.y + (node.dy ?? 0);
|
|
103
|
+
if (snapOffset) {
|
|
104
|
+
x += snapOffset.x;
|
|
105
|
+
y += snapOffset.y;
|
|
106
|
+
}
|
|
107
|
+
if (grid) {
|
|
108
|
+
x = snapToGrid(x, grid[0]);
|
|
109
|
+
y = snapToGrid(y, grid[1]);
|
|
110
|
+
}
|
|
111
|
+
const size = resolvedSize(node);
|
|
112
|
+
return { x, y, width: size.width, height: size.height };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Compute bounding rect of all nodes + edge labels (resolved positions) */
|
|
116
|
+
export function getGraphBounds(
|
|
117
|
+
nodes: FlowNode[],
|
|
118
|
+
edges?: FlowEdge[],
|
|
119
|
+
measurements?: ReadonlyMap<string, Rect>,
|
|
120
|
+
): Rect {
|
|
121
|
+
let minX = Infinity,
|
|
122
|
+
minY = Infinity,
|
|
123
|
+
maxX = -Infinity,
|
|
124
|
+
maxY = -Infinity;
|
|
125
|
+
for (const n of nodes) {
|
|
126
|
+
const b = resolvedBounds(n);
|
|
127
|
+
minX = Math.min(minX, b.x);
|
|
128
|
+
minY = Math.min(minY, b.y);
|
|
129
|
+
maxX = Math.max(maxX, b.x + b.width);
|
|
130
|
+
maxY = Math.max(maxY, b.y + b.height);
|
|
131
|
+
}
|
|
132
|
+
if (edges) {
|
|
133
|
+
for (const e of edges) {
|
|
134
|
+
for (const point of authoritativeEdgePoints(e) ?? []) {
|
|
135
|
+
minX = Math.min(minX, point.x);
|
|
136
|
+
minY = Math.min(minY, point.y);
|
|
137
|
+
maxX = Math.max(maxX, point.x);
|
|
138
|
+
maxY = Math.max(maxY, point.y);
|
|
139
|
+
}
|
|
140
|
+
const labelBounds = resolvedEdgeLabelBounds(e, measurements?.get(e.id));
|
|
141
|
+
if (!labelBounds) continue;
|
|
142
|
+
const lx = labelBounds.x;
|
|
143
|
+
const ly = labelBounds.y;
|
|
144
|
+
minX = Math.min(minX, lx);
|
|
145
|
+
minY = Math.min(minY, ly);
|
|
146
|
+
maxX = Math.max(maxX, lx + labelBounds.width);
|
|
147
|
+
maxY = Math.max(maxY, ly + labelBounds.height);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (!isFinite(minX)) return { x: 0, y: 0, width: 0, height: 0 };
|
|
151
|
+
return { x: minX, y: minY, width: maxX - minX, height: maxY - minY };
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** AABB intersection test */
|
|
155
|
+
export function rectsIntersect(a: Rect, b: Rect): boolean {
|
|
156
|
+
return (
|
|
157
|
+
a.x < b.x + b.width &&
|
|
158
|
+
a.x + a.width > b.x &&
|
|
159
|
+
a.y < b.y + b.height &&
|
|
160
|
+
a.y + a.height > b.y
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Smallest axis-aligned rect containing both `a` and `b`. */
|
|
165
|
+
export function unionRect(a: Rect, b: Rect): Rect {
|
|
166
|
+
const x = Math.min(a.x, b.x);
|
|
167
|
+
const y = Math.min(a.y, b.y);
|
|
168
|
+
return {
|
|
169
|
+
x,
|
|
170
|
+
y,
|
|
171
|
+
width: Math.max(a.x + a.width, b.x + b.width) - x,
|
|
172
|
+
height: Math.max(a.y + a.height, b.y + b.height) - y,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Snap a value to the nearest grid increment */
|
|
177
|
+
export function snapToGrid(value: number, gridSize: number): number {
|
|
178
|
+
return Math.round(value / gridSize) * gridSize;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Snap a point to grid */
|
|
182
|
+
export function snapPointToGrid(point: Point, gridSize: number): Point {
|
|
183
|
+
return {
|
|
184
|
+
x: snapToGrid(point.x, gridSize),
|
|
185
|
+
y: snapToGrid(point.y, gridSize),
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Normalize a box (two points) into a Rect with positive width/height */
|
|
190
|
+
export function boxToRect(p1: Point, p2: Point): Rect {
|
|
191
|
+
return {
|
|
192
|
+
x: Math.min(p1.x, p2.x),
|
|
193
|
+
y: Math.min(p1.y, p2.y),
|
|
194
|
+
width: Math.abs(p2.x - p1.x),
|
|
195
|
+
height: Math.abs(p2.y - p1.y),
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** Get bounding rect of specific entities by id */
|
|
200
|
+
export function getSelectionBounds(
|
|
201
|
+
nodes: FlowNode[],
|
|
202
|
+
edges: FlowEdge[],
|
|
203
|
+
selection: Set<string>,
|
|
204
|
+
measurements?: ReadonlyMap<string, Rect>,
|
|
205
|
+
options: { includeEdgePoints?: boolean } = {},
|
|
206
|
+
): Rect | null {
|
|
207
|
+
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
208
|
+
|
|
209
|
+
for (const n of nodes) {
|
|
210
|
+
if (!selection.has(n.id)) continue;
|
|
211
|
+
const b = resolvedBounds(n);
|
|
212
|
+
minX = Math.min(minX, b.x);
|
|
213
|
+
minY = Math.min(minY, b.y);
|
|
214
|
+
maxX = Math.max(maxX, b.x + b.width);
|
|
215
|
+
maxY = Math.max(maxY, b.y + b.height);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
for (const e of edges) {
|
|
219
|
+
if (!selection.has(e.id)) continue;
|
|
220
|
+
if (options.includeEdgePoints === true) {
|
|
221
|
+
for (const point of authoritativeEdgePoints(e) ?? []) {
|
|
222
|
+
minX = Math.min(minX, point.x);
|
|
223
|
+
minY = Math.min(minY, point.y);
|
|
224
|
+
maxX = Math.max(maxX, point.x);
|
|
225
|
+
maxY = Math.max(maxY, point.y);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
if (e.label) {
|
|
229
|
+
const labelBounds = resolvedEdgeLabelBounds(e, measurements?.get(e.id));
|
|
230
|
+
if (!labelBounds) continue;
|
|
231
|
+
const lx = labelBounds.x;
|
|
232
|
+
const ly = labelBounds.y;
|
|
233
|
+
minX = Math.min(minX, lx);
|
|
234
|
+
minY = Math.min(minY, ly);
|
|
235
|
+
maxX = Math.max(maxX, lx + labelBounds.width);
|
|
236
|
+
maxY = Math.max(maxY, ly + labelBounds.height);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (!isFinite(minX)) return null;
|
|
241
|
+
return { x: minX, y: minY, width: maxX - minX, height: maxY - minY };
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** Determine select mode from a pointer/keyboard event */
|
|
245
|
+
export function selectMode(e: { shiftKey?: boolean; metaKey?: boolean; ctrlKey?: boolean }): 'single' | 'multi' {
|
|
246
|
+
return (e.shiftKey || e.metaKey || e.ctrlKey) ? 'multi' : 'single';
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** Does rect `a` fully contain rect `b`? */
|
|
250
|
+
export function rectContains(a: Rect, b: Rect): boolean {
|
|
251
|
+
return b.x >= a.x && b.y >= a.y &&
|
|
252
|
+
b.x + b.width <= a.x + a.width &&
|
|
253
|
+
b.y + b.height <= a.y + a.height;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// ---------------------------------------------------------------------------
|
|
257
|
+
// Wheel event utilities (ported from d3-zoom's wheelDelta logic)
|
|
258
|
+
// ---------------------------------------------------------------------------
|
|
259
|
+
|
|
260
|
+
const platform =
|
|
261
|
+
(globalThis as { navigator?: { platform?: string } }).navigator?.platform ?? '';
|
|
262
|
+
const isMac = /Mac|iPhone|iPad/.test(platform);
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Convert a wheel event into a logarithmic zoom exponent.
|
|
266
|
+
* Accounts for deltaMode (pixels/lines/pages) and trackpad pinch (ctrlKey on macOS).
|
|
267
|
+
* Use as: `newZoom = currentZoom * Math.pow(2, wheelZoomDelta(e))`
|
|
268
|
+
*/
|
|
269
|
+
export function wheelZoomDelta(e: { deltaY: number; deltaMode: number; ctrlKey: boolean }): number {
|
|
270
|
+
// ctrlKey + macOS = trackpad pinch — needs higher sensitivity
|
|
271
|
+
const factor = e.ctrlKey && isMac ? 10 : 1;
|
|
272
|
+
// deltaMode: 0=pixels, 1=lines(Firefox), 2=pages
|
|
273
|
+
const modeScale = e.deltaMode === 1 ? 0.05 : e.deltaMode ? 1 : 0.002;
|
|
274
|
+
return -e.deltaY * modeScale * factor;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Normalize wheel deltas for panning across browsers.
|
|
279
|
+
* Firefox fires deltaMode=1 (lines) with small values; Chrome fires deltaMode=0 (pixels) with large values.
|
|
280
|
+
*/
|
|
281
|
+
export function normalizeWheelPan(e: { deltaX: number; deltaY: number; deltaMode: number }): { x: number; y: number } {
|
|
282
|
+
// Firefox line mode: ~3 lines per tick, multiply by ~20px/line to match Chrome pixel deltas
|
|
283
|
+
const scale = e.deltaMode === 1 ? 20 : 1;
|
|
284
|
+
return { x: -e.deltaX * scale, y: -e.deltaY * scale };
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Detect whether a wheel event is a trackpad pinch-to-zoom gesture.
|
|
289
|
+
* Browsers send wheel events with ctrlKey=true for trackpad pinch.
|
|
290
|
+
*/
|
|
291
|
+
export function isTrackpadPinch(e: { ctrlKey: boolean; metaKey: boolean }): boolean {
|
|
292
|
+
return e.ctrlKey || e.metaKey;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
export type WheelInteraction = 'zoom' | 'pan' | 'block' | 'ignore';
|
|
296
|
+
|
|
297
|
+
export type WheelInteractionOptions = {
|
|
298
|
+
/**
|
|
299
|
+
* Whether ordinary wheel gestures pan the canvas.
|
|
300
|
+
*/
|
|
301
|
+
panOnScroll?: boolean;
|
|
302
|
+
/**
|
|
303
|
+
* Whether browser pinch gestures zoom the canvas.
|
|
304
|
+
*
|
|
305
|
+
* Even when disabled, pinch wheels should still be blocked while the pointer is
|
|
306
|
+
* over the canvas so the browser page does not zoom behind the app.
|
|
307
|
+
*/
|
|
308
|
+
zoomOnPinch?: boolean;
|
|
309
|
+
/**
|
|
310
|
+
* Whether a floating layer such as a context menu, popover, or dialog is open.
|
|
311
|
+
*/
|
|
312
|
+
floatingLayerOpen?: boolean;
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Decide how a wheel event should be handled by a flow canvas.
|
|
317
|
+
*
|
|
318
|
+
* This mirrors the important React Flow / d3-zoom behavior: browser pinch
|
|
319
|
+
* gestures are always consumed over the canvas. Otherwise the whole page can
|
|
320
|
+
* zoom even though the user is interacting with the diagram.
|
|
321
|
+
*/
|
|
322
|
+
export function wheelInteraction(
|
|
323
|
+
e: { ctrlKey: boolean; metaKey: boolean },
|
|
324
|
+
options: WheelInteractionOptions = {},
|
|
325
|
+
): WheelInteraction {
|
|
326
|
+
if (isTrackpadPinch(e)) {
|
|
327
|
+
if (options.floatingLayerOpen || options.zoomOnPinch === false) {
|
|
328
|
+
return 'block';
|
|
329
|
+
}
|
|
330
|
+
return 'zoom';
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
if (options.floatingLayerOpen) {
|
|
334
|
+
return options.panOnScroll ? 'block' : 'ignore';
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
return options.panOnScroll ? 'pan' : 'ignore';
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Constrain viewport so content can't be panned entirely off-screen.
|
|
342
|
+
* When zoomed out (content smaller than view), centers the content.
|
|
343
|
+
* Matches d3-zoom's defaultConstrain behavior.
|
|
344
|
+
*/
|
|
345
|
+
export function constrainViewport(
|
|
346
|
+
viewport: Viewport,
|
|
347
|
+
viewportSize: { width: number; height: number },
|
|
348
|
+
translateExtent: [Point, Point],
|
|
349
|
+
): Viewport {
|
|
350
|
+
// Visible canvas bounds
|
|
351
|
+
const vx0 = -viewport.x / viewport.zoom;
|
|
352
|
+
const vy0 = -viewport.y / viewport.zoom;
|
|
353
|
+
const vx1 = vx0 + viewportSize.width / viewport.zoom;
|
|
354
|
+
const vy1 = vy0 + viewportSize.height / viewport.zoom;
|
|
355
|
+
|
|
356
|
+
// Offsets from translate extent boundaries
|
|
357
|
+
const dx0 = vx0 - translateExtent[0].x;
|
|
358
|
+
const dx1 = vx1 - translateExtent[1].x;
|
|
359
|
+
const dy0 = vy0 - translateExtent[0].y;
|
|
360
|
+
const dy1 = vy1 - translateExtent[1].y;
|
|
361
|
+
|
|
362
|
+
// If visible area exceeds extent, center; otherwise clamp to nearest boundary
|
|
363
|
+
const tx = dx1 > dx0 ? (dx0 + dx1) / 2 : Math.min(0, dx0) || Math.max(0, dx1);
|
|
364
|
+
const ty = dy1 > dy0 ? (dy0 + dy1) / 2 : Math.min(0, dy0) || Math.max(0, dy1);
|
|
365
|
+
|
|
366
|
+
if (tx === 0 && ty === 0) return viewport;
|
|
367
|
+
return {
|
|
368
|
+
x: viewport.x + tx * viewport.zoom,
|
|
369
|
+
y: viewport.y + ty * viewport.zoom,
|
|
370
|
+
zoom: viewport.zoom,
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Compute auto-pan velocity when dragging near the viewport edge.
|
|
376
|
+
* Returns screen-space delta to apply per frame. Returns {0,0} if cursor isn't near an edge.
|
|
377
|
+
*/
|
|
378
|
+
export function calcAutoPan(
|
|
379
|
+
mousePosition: Point,
|
|
380
|
+
containerBounds: { width: number; height: number },
|
|
381
|
+
edgeWidth = 25,
|
|
382
|
+
speed = 10,
|
|
383
|
+
): Point {
|
|
384
|
+
if (edgeWidth <= 0 || speed <= 0) {
|
|
385
|
+
return { x: 0, y: 0 };
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
const clamp01 = (value: number) => Math.min(1, Math.max(0, value));
|
|
389
|
+
const getAxisDelta = (position: number, size: number) => {
|
|
390
|
+
if (position < edgeWidth) {
|
|
391
|
+
return -speed * clamp01(1 - position / edgeWidth);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
if (position > size - edgeWidth) {
|
|
395
|
+
return speed * clamp01(1 - (size - position) / edgeWidth);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
return 0;
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
return {
|
|
402
|
+
x: getAxisDelta(mousePosition.x, containerBounds.width),
|
|
403
|
+
y: getAxisDelta(mousePosition.y, containerBounds.height),
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
// Anchor transform (for toolbars, tooltips — accounts for inverse zoom)
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
export type AnchorSpec = {
|
|
412
|
+
x: number | string; // px from left, or 'N%' for percentage
|
|
413
|
+
y: number | string;
|
|
414
|
+
offsetX?: number; // screen px (stays constant regardless of zoom)
|
|
415
|
+
offsetY?: number;
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
export type AnchorTransform = {
|
|
419
|
+
x: number; // canvas x
|
|
420
|
+
y: number; // canvas y
|
|
421
|
+
scale: number; // 1/zoom — apply to keep constant screen size
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* Compute anchor transform for positioning overlays relative to a rect.
|
|
426
|
+
* Returns canvas-space position + inverse zoom scale.
|
|
427
|
+
*
|
|
428
|
+
* Offsets are in screen pixels — divided by zoom so they stay constant.
|
|
429
|
+
*
|
|
430
|
+
* Usage (SVG): `transform="translate(${t.x}, ${t.y}) scale(${t.scale})"`
|
|
431
|
+
* Usage (CSS): `transform: translate(${t.x}px, ${t.y}px) scale(${t.scale})`
|
|
432
|
+
*/
|
|
433
|
+
export function getAnchorTransform(
|
|
434
|
+
bounds: Rect,
|
|
435
|
+
viewport: Viewport,
|
|
436
|
+
anchor: AnchorSpec,
|
|
437
|
+
): AnchorTransform {
|
|
438
|
+
const scale = 1 / viewport.zoom;
|
|
439
|
+
|
|
440
|
+
// Parse anchor values
|
|
441
|
+
const ax = parseAnchorValue(anchor.x, bounds.width);
|
|
442
|
+
const ay = parseAnchorValue(anchor.y, bounds.height);
|
|
443
|
+
|
|
444
|
+
// Canvas position = bounds origin + anchor offset + screen-px offset / zoom
|
|
445
|
+
const x = bounds.x + ax + (anchor.offsetX ?? 0) * scale;
|
|
446
|
+
const y = bounds.y + ay + (anchor.offsetY ?? 0) * scale;
|
|
447
|
+
|
|
448
|
+
return { x, y, scale };
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
function parseAnchorValue(value: number | string, dimension: number): number {
|
|
452
|
+
if (typeof value === 'number') return value;
|
|
453
|
+
if (typeof value === 'string' && value.endsWith('%')) {
|
|
454
|
+
return (parseFloat(value) / 100) * dimension;
|
|
455
|
+
}
|
|
456
|
+
return parseFloat(value) || 0;
|
|
457
|
+
}
|