@statelyai/flow 0.0.1 → 0.4.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 +498 -10
- package/dist/esm/index.d.ts +2035 -0
- package/dist/esm/index.js +4739 -0
- package/package.json +50 -15
- package/.vscode/launch.json +0 -34
- package/LICENSE +0 -21
- package/src/createFlow.test.ts +0 -204
- package/src/index.ts +0 -62
|
@@ -0,0 +1,4739 @@
|
|
|
1
|
+
import { createStore } from "@xstate/store";
|
|
2
|
+
import { undoRedo } from "@xstate/store/undo";
|
|
3
|
+
import { current, enableMapSet, isDraft, produce, setAutoFreeze } from "immer";
|
|
4
|
+
//#region src/profile.ts
|
|
5
|
+
function defineFlowProfile(profile) {
|
|
6
|
+
return profile;
|
|
7
|
+
}
|
|
8
|
+
const defaultFlowProfile = defineFlowProfile({
|
|
9
|
+
defaultMode: "interactive",
|
|
10
|
+
modes: {
|
|
11
|
+
interactive: {},
|
|
12
|
+
readonly: {}
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/viewport/index.ts
|
|
17
|
+
/** Create a default viewport (origin, no zoom) */
|
|
18
|
+
function createViewport(x = 0, y = 0, zoom = 1) {
|
|
19
|
+
return {
|
|
20
|
+
x,
|
|
21
|
+
y,
|
|
22
|
+
zoom
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Convert viewport to SVG transform string.
|
|
27
|
+
* Use on the root <g> wrapping all entities.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* <g transform={toSVGTransform(viewport)}>...</g>
|
|
31
|
+
*/
|
|
32
|
+
function toSVGTransform(viewport) {
|
|
33
|
+
return `translate(${viewport.x}, ${viewport.y}) scale(${viewport.zoom})`;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Convert viewport + container size to an SVG viewBox string.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* <svg viewBox={toViewBox(viewport, { width: 800, height: 600 })}>...</svg>
|
|
40
|
+
*/
|
|
41
|
+
function toViewBox(viewport, size) {
|
|
42
|
+
return `${-viewport.x / viewport.zoom} ${-viewport.y / viewport.zoom} ${size.width / viewport.zoom} ${size.height / viewport.zoom}`;
|
|
43
|
+
}
|
|
44
|
+
/** Convert a screen-space point to canvas-space */
|
|
45
|
+
function screenToCanvas(point, viewport) {
|
|
46
|
+
return {
|
|
47
|
+
x: (point.x - viewport.x) / viewport.zoom,
|
|
48
|
+
y: (point.y - viewport.y) / viewport.zoom
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/** Convert a canvas-space point to screen-space */
|
|
52
|
+
function canvasToScreen(point, viewport) {
|
|
53
|
+
return {
|
|
54
|
+
x: point.x * viewport.zoom + viewport.x,
|
|
55
|
+
y: point.y * viewport.zoom + viewport.y
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/** Get the canvas-space bounding rect of what's visible on screen */
|
|
59
|
+
function getVisibleBounds(viewport, size) {
|
|
60
|
+
return {
|
|
61
|
+
x: -viewport.x / viewport.zoom,
|
|
62
|
+
y: -viewport.y / viewport.zoom,
|
|
63
|
+
width: size.width / viewport.zoom,
|
|
64
|
+
height: size.height / viewport.zoom
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Compute a viewport that fits the given bounds into the given screen size.
|
|
69
|
+
*/
|
|
70
|
+
function viewportForBounds(bounds, size, options = {}) {
|
|
71
|
+
const { padding = .1, minZoom = .1, maxZoom = 2 } = options;
|
|
72
|
+
const paddedWidth = bounds.width * (1 + padding);
|
|
73
|
+
const paddedHeight = bounds.height * (1 + padding);
|
|
74
|
+
if (paddedWidth === 0 || paddedHeight === 0) return {
|
|
75
|
+
x: 0,
|
|
76
|
+
y: 0,
|
|
77
|
+
zoom: 1
|
|
78
|
+
};
|
|
79
|
+
const zoomX = size.width / paddedWidth;
|
|
80
|
+
const zoomY = size.height / paddedHeight;
|
|
81
|
+
const zoom = Math.min(Math.max(Math.min(zoomX, zoomY), minZoom), maxZoom);
|
|
82
|
+
const centerX = bounds.x + bounds.width / 2;
|
|
83
|
+
const centerY = bounds.y + bounds.height / 2;
|
|
84
|
+
return {
|
|
85
|
+
x: size.width / 2 - centerX * zoom,
|
|
86
|
+
y: size.height / 2 - centerY * zoom,
|
|
87
|
+
zoom
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
/** Pan viewport by a delta */
|
|
91
|
+
function panBy(viewport, delta) {
|
|
92
|
+
return {
|
|
93
|
+
x: viewport.x + delta.x,
|
|
94
|
+
y: viewport.y + delta.y,
|
|
95
|
+
zoom: viewport.zoom
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
/** Zoom to a specific level, keeping a screen-space point fixed */
|
|
99
|
+
function zoomTo(viewport, zoom, center) {
|
|
100
|
+
const clampedZoom = zoom;
|
|
101
|
+
const ratio = clampedZoom / viewport.zoom;
|
|
102
|
+
return {
|
|
103
|
+
x: center.x - (center.x - viewport.x) * ratio,
|
|
104
|
+
y: center.y - (center.y - viewport.y) * ratio,
|
|
105
|
+
zoom: clampedZoom
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
/** Clamp zoom to min/max bounds */
|
|
109
|
+
function clampZoom(zoom, minZoom, maxZoom) {
|
|
110
|
+
return Math.min(Math.max(zoom, minZoom), maxZoom);
|
|
111
|
+
}
|
|
112
|
+
/** Viewport that places the given canvas point at the center of the screen. */
|
|
113
|
+
function viewportCenteredOn(center, size, zoom) {
|
|
114
|
+
return {
|
|
115
|
+
x: size.width / 2 - center.x * zoom,
|
|
116
|
+
y: size.height / 2 - center.y * zoom,
|
|
117
|
+
zoom
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
//#endregion
|
|
121
|
+
//#region src/spatial.ts
|
|
122
|
+
/** Resolved visual position: layout position + drag offset */
|
|
123
|
+
function resolvedPosition(node) {
|
|
124
|
+
return {
|
|
125
|
+
x: node.x + (node.dx ?? 0),
|
|
126
|
+
y: node.y + (node.dy ?? 0)
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
/** Resolved visual bounds: layout position + drag offset + dimensions.
|
|
130
|
+
* Uses style.customWidth/customHeight when set (from resize), otherwise inherent width/height. */
|
|
131
|
+
/** Size the geometry should use, in priority order:
|
|
132
|
+
* 1. `style.customWidth/customHeight` (an explicit resize override),
|
|
133
|
+
* 2. for `autoSize` nodes, the DOM-`measured` size (the renderer owns it),
|
|
134
|
+
* 3. the authored `width/height`.
|
|
135
|
+
* Authored `width/height` are never mutated — measured size lives on
|
|
136
|
+
* `node.measured`, so this is the single place that reconciles the two. */
|
|
137
|
+
function resolvedSize(node) {
|
|
138
|
+
return {
|
|
139
|
+
width: node.style?.customWidth ?? (node.autoSize ? node.measured?.width ?? node.width : node.width),
|
|
140
|
+
height: node.style?.customHeight ?? (node.autoSize ? node.measured?.height ?? node.height : node.height)
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
function resolvedBounds(node) {
|
|
144
|
+
const size = resolvedSize(node);
|
|
145
|
+
return {
|
|
146
|
+
x: node.x + (node.dx ?? 0),
|
|
147
|
+
y: node.y + (node.dy ?? 0),
|
|
148
|
+
width: size.width,
|
|
149
|
+
height: size.height
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
/** Resolved bounds with visual snap — for rendering during drag.
|
|
153
|
+
* Applies grid snap and/or entity snap offset. */
|
|
154
|
+
function snappedBounds(node, grid, snapOffset) {
|
|
155
|
+
let x = node.x + (node.dx ?? 0);
|
|
156
|
+
let y = node.y + (node.dy ?? 0);
|
|
157
|
+
if (snapOffset) {
|
|
158
|
+
x += snapOffset.x;
|
|
159
|
+
y += snapOffset.y;
|
|
160
|
+
}
|
|
161
|
+
if (grid) {
|
|
162
|
+
x = snapToGrid(x, grid[0]);
|
|
163
|
+
y = snapToGrid(y, grid[1]);
|
|
164
|
+
}
|
|
165
|
+
const size = resolvedSize(node);
|
|
166
|
+
return {
|
|
167
|
+
x,
|
|
168
|
+
y,
|
|
169
|
+
width: size.width,
|
|
170
|
+
height: size.height
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
/** Compute bounding rect of all nodes + edge labels (resolved positions) */
|
|
174
|
+
function getGraphBounds(nodes, edges) {
|
|
175
|
+
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
176
|
+
for (const n of nodes) {
|
|
177
|
+
const b = resolvedBounds(n);
|
|
178
|
+
minX = Math.min(minX, b.x);
|
|
179
|
+
minY = Math.min(minY, b.y);
|
|
180
|
+
maxX = Math.max(maxX, b.x + b.width);
|
|
181
|
+
maxY = Math.max(maxY, b.y + b.height);
|
|
182
|
+
}
|
|
183
|
+
if (edges) for (const e of edges) {
|
|
184
|
+
if (!e.label) continue;
|
|
185
|
+
const lx = e.label.x + (e.label.dx ?? 0);
|
|
186
|
+
const ly = e.label.y + (e.label.dy ?? 0);
|
|
187
|
+
minX = Math.min(minX, lx);
|
|
188
|
+
minY = Math.min(minY, ly);
|
|
189
|
+
maxX = Math.max(maxX, lx + e.label.width);
|
|
190
|
+
maxY = Math.max(maxY, ly + e.label.height);
|
|
191
|
+
}
|
|
192
|
+
if (!isFinite(minX)) return {
|
|
193
|
+
x: 0,
|
|
194
|
+
y: 0,
|
|
195
|
+
width: 0,
|
|
196
|
+
height: 0
|
|
197
|
+
};
|
|
198
|
+
return {
|
|
199
|
+
x: minX,
|
|
200
|
+
y: minY,
|
|
201
|
+
width: maxX - minX,
|
|
202
|
+
height: maxY - minY
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
/** AABB intersection test */
|
|
206
|
+
function rectsIntersect(a, b) {
|
|
207
|
+
return a.x < b.x + b.width && a.x + a.width > b.x && a.y < b.y + b.height && a.y + a.height > b.y;
|
|
208
|
+
}
|
|
209
|
+
/** Smallest axis-aligned rect containing both `a` and `b`. */
|
|
210
|
+
function unionRect(a, b) {
|
|
211
|
+
const x = Math.min(a.x, b.x);
|
|
212
|
+
const y = Math.min(a.y, b.y);
|
|
213
|
+
return {
|
|
214
|
+
x,
|
|
215
|
+
y,
|
|
216
|
+
width: Math.max(a.x + a.width, b.x + b.width) - x,
|
|
217
|
+
height: Math.max(a.y + a.height, b.y + b.height) - y
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
/** Snap a value to the nearest grid increment */
|
|
221
|
+
function snapToGrid(value, gridSize) {
|
|
222
|
+
return Math.round(value / gridSize) * gridSize;
|
|
223
|
+
}
|
|
224
|
+
/** Snap a point to grid */
|
|
225
|
+
function snapPointToGrid(point, gridSize) {
|
|
226
|
+
return {
|
|
227
|
+
x: snapToGrid(point.x, gridSize),
|
|
228
|
+
y: snapToGrid(point.y, gridSize)
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
/** Normalize a box (two points) into a Rect with positive width/height */
|
|
232
|
+
function boxToRect(p1, p2) {
|
|
233
|
+
return {
|
|
234
|
+
x: Math.min(p1.x, p2.x),
|
|
235
|
+
y: Math.min(p1.y, p2.y),
|
|
236
|
+
width: Math.abs(p2.x - p1.x),
|
|
237
|
+
height: Math.abs(p2.y - p1.y)
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
/** Get bounding rect of specific entities by id */
|
|
241
|
+
function getSelectionBounds(nodes, edges, selection) {
|
|
242
|
+
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
243
|
+
for (const n of nodes) {
|
|
244
|
+
if (!selection.has(n.id)) continue;
|
|
245
|
+
const b = resolvedBounds(n);
|
|
246
|
+
minX = Math.min(minX, b.x);
|
|
247
|
+
minY = Math.min(minY, b.y);
|
|
248
|
+
maxX = Math.max(maxX, b.x + b.width);
|
|
249
|
+
maxY = Math.max(maxY, b.y + b.height);
|
|
250
|
+
}
|
|
251
|
+
for (const e of edges) {
|
|
252
|
+
if (!selection.has(e.id) || !e.label) continue;
|
|
253
|
+
const lx = e.label.x + (e.label.dx ?? 0);
|
|
254
|
+
const ly = e.label.y + (e.label.dy ?? 0);
|
|
255
|
+
minX = Math.min(minX, lx);
|
|
256
|
+
minY = Math.min(minY, ly);
|
|
257
|
+
maxX = Math.max(maxX, lx + e.label.width);
|
|
258
|
+
maxY = Math.max(maxY, ly + e.label.height);
|
|
259
|
+
}
|
|
260
|
+
if (!isFinite(minX)) return null;
|
|
261
|
+
return {
|
|
262
|
+
x: minX,
|
|
263
|
+
y: minY,
|
|
264
|
+
width: maxX - minX,
|
|
265
|
+
height: maxY - minY
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
/** Determine select mode from a pointer/keyboard event */
|
|
269
|
+
function selectMode(e) {
|
|
270
|
+
return e.shiftKey || e.metaKey || e.ctrlKey ? "multi" : "single";
|
|
271
|
+
}
|
|
272
|
+
/** Does rect `a` fully contain rect `b`? */
|
|
273
|
+
function rectContains(a, b) {
|
|
274
|
+
return b.x >= a.x && b.y >= a.y && b.x + b.width <= a.x + a.width && b.y + b.height <= a.y + a.height;
|
|
275
|
+
}
|
|
276
|
+
const platform = globalThis.navigator?.platform ?? "";
|
|
277
|
+
const isMac = /Mac|iPhone|iPad/.test(platform);
|
|
278
|
+
/**
|
|
279
|
+
* Convert a wheel event into a logarithmic zoom exponent.
|
|
280
|
+
* Accounts for deltaMode (pixels/lines/pages) and trackpad pinch (ctrlKey on macOS).
|
|
281
|
+
* Use as: `newZoom = currentZoom * Math.pow(2, wheelZoomDelta(e))`
|
|
282
|
+
*/
|
|
283
|
+
function wheelZoomDelta(e) {
|
|
284
|
+
const factor = e.ctrlKey && isMac ? 10 : 1;
|
|
285
|
+
const modeScale = e.deltaMode === 1 ? .05 : e.deltaMode ? 1 : .002;
|
|
286
|
+
return -e.deltaY * modeScale * factor;
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Normalize wheel deltas for panning across browsers.
|
|
290
|
+
* Firefox fires deltaMode=1 (lines) with small values; Chrome fires deltaMode=0 (pixels) with large values.
|
|
291
|
+
*/
|
|
292
|
+
function normalizeWheelPan(e) {
|
|
293
|
+
const scale = e.deltaMode === 1 ? 20 : 1;
|
|
294
|
+
return {
|
|
295
|
+
x: -e.deltaX * scale,
|
|
296
|
+
y: -e.deltaY * scale
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Detect whether a wheel event is a trackpad pinch-to-zoom gesture.
|
|
301
|
+
* Browsers send wheel events with ctrlKey=true for trackpad pinch.
|
|
302
|
+
*/
|
|
303
|
+
function isTrackpadPinch(e) {
|
|
304
|
+
return e.ctrlKey || e.metaKey;
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Decide how a wheel event should be handled by a flow canvas.
|
|
308
|
+
*
|
|
309
|
+
* This mirrors the important React Flow / d3-zoom behavior: browser pinch
|
|
310
|
+
* gestures are always consumed over the canvas. Otherwise the whole page can
|
|
311
|
+
* zoom even though the user is interacting with the diagram.
|
|
312
|
+
*/
|
|
313
|
+
function wheelInteraction(e, options = {}) {
|
|
314
|
+
if (isTrackpadPinch(e)) {
|
|
315
|
+
if (options.floatingLayerOpen || options.zoomOnPinch === false) return "block";
|
|
316
|
+
return "zoom";
|
|
317
|
+
}
|
|
318
|
+
if (options.floatingLayerOpen) return options.panOnScroll ? "block" : "ignore";
|
|
319
|
+
return options.panOnScroll ? "pan" : "ignore";
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Constrain viewport so content can't be panned entirely off-screen.
|
|
323
|
+
* When zoomed out (content smaller than view), centers the content.
|
|
324
|
+
* Matches d3-zoom's defaultConstrain behavior.
|
|
325
|
+
*/
|
|
326
|
+
function constrainViewport(viewport, viewportSize, translateExtent) {
|
|
327
|
+
const vx0 = -viewport.x / viewport.zoom;
|
|
328
|
+
const vy0 = -viewport.y / viewport.zoom;
|
|
329
|
+
const vx1 = vx0 + viewportSize.width / viewport.zoom;
|
|
330
|
+
const vy1 = vy0 + viewportSize.height / viewport.zoom;
|
|
331
|
+
const dx0 = vx0 - translateExtent[0].x;
|
|
332
|
+
const dx1 = vx1 - translateExtent[1].x;
|
|
333
|
+
const dy0 = vy0 - translateExtent[0].y;
|
|
334
|
+
const dy1 = vy1 - translateExtent[1].y;
|
|
335
|
+
const tx = dx1 > dx0 ? (dx0 + dx1) / 2 : Math.min(0, dx0) || Math.max(0, dx1);
|
|
336
|
+
const ty = dy1 > dy0 ? (dy0 + dy1) / 2 : Math.min(0, dy0) || Math.max(0, dy1);
|
|
337
|
+
if (tx === 0 && ty === 0) return viewport;
|
|
338
|
+
return {
|
|
339
|
+
x: viewport.x + tx * viewport.zoom,
|
|
340
|
+
y: viewport.y + ty * viewport.zoom,
|
|
341
|
+
zoom: viewport.zoom
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Compute auto-pan velocity when dragging near the viewport edge.
|
|
346
|
+
* Returns screen-space delta to apply per frame. Returns {0,0} if cursor isn't near an edge.
|
|
347
|
+
*/
|
|
348
|
+
function calcAutoPan(mousePosition, containerBounds, edgeWidth = 25, speed = 10) {
|
|
349
|
+
let dx = 0, dy = 0;
|
|
350
|
+
if (mousePosition.x < edgeWidth) dx = -speed * (1 - mousePosition.x / edgeWidth);
|
|
351
|
+
else if (mousePosition.x > containerBounds.width - edgeWidth) dx = speed * (1 - (containerBounds.width - mousePosition.x) / edgeWidth);
|
|
352
|
+
if (mousePosition.y < edgeWidth) dy = -speed * (1 - mousePosition.y / edgeWidth);
|
|
353
|
+
else if (mousePosition.y > containerBounds.height - edgeWidth) dy = speed * (1 - (containerBounds.height - mousePosition.y) / edgeWidth);
|
|
354
|
+
return {
|
|
355
|
+
x: dx,
|
|
356
|
+
y: dy
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Compute anchor transform for positioning overlays relative to a rect.
|
|
361
|
+
* Returns canvas-space position + inverse zoom scale.
|
|
362
|
+
*
|
|
363
|
+
* Offsets are in screen pixels — divided by zoom so they stay constant.
|
|
364
|
+
*
|
|
365
|
+
* Usage (SVG): `transform="translate(${t.x}, ${t.y}) scale(${t.scale})"`
|
|
366
|
+
* Usage (CSS): `transform: translate(${t.x}px, ${t.y}px) scale(${t.scale})`
|
|
367
|
+
*/
|
|
368
|
+
function getAnchorTransform(bounds, viewport, anchor) {
|
|
369
|
+
const scale = 1 / viewport.zoom;
|
|
370
|
+
const ax = parseAnchorValue(anchor.x, bounds.width);
|
|
371
|
+
const ay = parseAnchorValue(anchor.y, bounds.height);
|
|
372
|
+
return {
|
|
373
|
+
x: bounds.x + ax + (anchor.offsetX ?? 0) * scale,
|
|
374
|
+
y: bounds.y + ay + (anchor.offsetY ?? 0) * scale,
|
|
375
|
+
scale
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
function parseAnchorValue(value, dimension) {
|
|
379
|
+
if (typeof value === "number") return value;
|
|
380
|
+
if (typeof value === "string" && value.endsWith("%")) return parseFloat(value) / 100 * dimension;
|
|
381
|
+
return parseFloat(value) || 0;
|
|
382
|
+
}
|
|
383
|
+
//#endregion
|
|
384
|
+
//#region src/snap.ts
|
|
385
|
+
/**
|
|
386
|
+
* Compute snap lines for a dragged rect against other rects.
|
|
387
|
+
*
|
|
388
|
+
* Checks alignment of left/right/center-x and top/bottom/center-y
|
|
389
|
+
* against all target rects. Optionally checks equal spacing between elements.
|
|
390
|
+
*
|
|
391
|
+
* Returns null if no snap is within threshold.
|
|
392
|
+
*/
|
|
393
|
+
function getSnapLines(rect, otherRects, { threshold = 5, enableSpacing = false } = {}) {
|
|
394
|
+
if (otherRects.length === 0) return null;
|
|
395
|
+
const getPoints = (r) => ({
|
|
396
|
+
left: r.x,
|
|
397
|
+
right: r.x + r.width,
|
|
398
|
+
top: r.y,
|
|
399
|
+
bottom: r.y + r.height,
|
|
400
|
+
centerX: r.x + r.width / 2,
|
|
401
|
+
centerY: r.y + r.height / 2
|
|
402
|
+
});
|
|
403
|
+
const rectPoints = getPoints(rect);
|
|
404
|
+
const snapLines = [];
|
|
405
|
+
let snapX = null;
|
|
406
|
+
let snapY = null;
|
|
407
|
+
let minDistX = threshold + 1;
|
|
408
|
+
let minDistY = threshold + 1;
|
|
409
|
+
let spacingGuides = [];
|
|
410
|
+
if (enableSpacing && otherRects.length >= 1) {
|
|
411
|
+
const hasVerticalOverlap = (a, b) => a.y < b.y + b.height && a.y + a.height > b.y;
|
|
412
|
+
const hasHorizontalOverlap = (a, b) => a.x < b.x + b.width && a.x + a.width > b.x;
|
|
413
|
+
for (const target of otherRects) {
|
|
414
|
+
if (!hasVerticalOverlap(rect, target)) continue;
|
|
415
|
+
for (const other of otherRects) {
|
|
416
|
+
if (other === target) continue;
|
|
417
|
+
if (!hasVerticalOverlap(other, target)) continue;
|
|
418
|
+
const gapRight = other.x - (target.x + target.width);
|
|
419
|
+
const gapLeft = target.x - (other.x + other.width);
|
|
420
|
+
if (gapLeft > 0) {
|
|
421
|
+
const snapPos = target.x + target.width + gapLeft;
|
|
422
|
+
const dist = Math.abs(rectPoints.left - snapPos);
|
|
423
|
+
if (dist <= threshold && dist < minDistX) {
|
|
424
|
+
const guideY = (Math.max(rect.y, target.y, other.y) + Math.min(rect.y + rect.height, target.y + target.height, other.y + other.height)) / 2;
|
|
425
|
+
minDistX = dist;
|
|
426
|
+
snapX = snapPos;
|
|
427
|
+
spacingGuides = [{
|
|
428
|
+
axis: "x",
|
|
429
|
+
position: guideY,
|
|
430
|
+
spacing: gapLeft,
|
|
431
|
+
ranges: [{
|
|
432
|
+
start: other.x + other.width,
|
|
433
|
+
end: target.x
|
|
434
|
+
}, {
|
|
435
|
+
start: target.x + target.width,
|
|
436
|
+
end: snapPos
|
|
437
|
+
}]
|
|
438
|
+
}];
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
if (gapRight > 0) {
|
|
442
|
+
const snapPos = target.x - gapRight - rect.width;
|
|
443
|
+
const dist = Math.abs(rectPoints.left - snapPos);
|
|
444
|
+
if (dist <= threshold && dist < minDistX) {
|
|
445
|
+
const guideY = (Math.max(rect.y, target.y, other.y) + Math.min(rect.y + rect.height, target.y + target.height, other.y + other.height)) / 2;
|
|
446
|
+
minDistX = dist;
|
|
447
|
+
snapX = snapPos;
|
|
448
|
+
spacingGuides = [{
|
|
449
|
+
axis: "x",
|
|
450
|
+
position: guideY,
|
|
451
|
+
spacing: gapRight,
|
|
452
|
+
ranges: [{
|
|
453
|
+
start: snapPos + rect.width,
|
|
454
|
+
end: target.x
|
|
455
|
+
}, {
|
|
456
|
+
start: target.x + target.width,
|
|
457
|
+
end: other.x
|
|
458
|
+
}]
|
|
459
|
+
}];
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
for (const target of otherRects) {
|
|
465
|
+
if (!hasHorizontalOverlap(rect, target)) continue;
|
|
466
|
+
for (const other of otherRects) {
|
|
467
|
+
if (other === target) continue;
|
|
468
|
+
if (!hasHorizontalOverlap(other, target)) continue;
|
|
469
|
+
const gapBelow = other.y - (target.y + target.height);
|
|
470
|
+
const gapAbove = target.y - (other.y + other.height);
|
|
471
|
+
if (gapAbove > 0) {
|
|
472
|
+
const snapPos = target.y + target.height + gapAbove;
|
|
473
|
+
const dist = Math.abs(rectPoints.top - snapPos);
|
|
474
|
+
if (dist <= threshold && dist < minDistY) {
|
|
475
|
+
const guideX = (Math.max(rect.x, target.x, other.x) + Math.min(rect.x + rect.width, target.x + target.width, other.x + other.width)) / 2;
|
|
476
|
+
minDistY = dist;
|
|
477
|
+
snapY = snapPos;
|
|
478
|
+
spacingGuides = spacingGuides.filter((g) => g.axis !== "y");
|
|
479
|
+
spacingGuides.push({
|
|
480
|
+
axis: "y",
|
|
481
|
+
position: guideX,
|
|
482
|
+
spacing: gapAbove,
|
|
483
|
+
ranges: [{
|
|
484
|
+
start: other.y + other.height,
|
|
485
|
+
end: target.y
|
|
486
|
+
}, {
|
|
487
|
+
start: target.y + target.height,
|
|
488
|
+
end: snapPos
|
|
489
|
+
}]
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
if (gapBelow > 0) {
|
|
494
|
+
const snapPos = target.y - gapBelow - rect.height;
|
|
495
|
+
const dist = Math.abs(rectPoints.top - snapPos);
|
|
496
|
+
if (dist <= threshold && dist < minDistY) {
|
|
497
|
+
const guideX = (Math.max(rect.x, target.x, other.x) + Math.min(rect.x + rect.width, target.x + target.width, other.x + other.width)) / 2;
|
|
498
|
+
minDistY = dist;
|
|
499
|
+
snapY = snapPos;
|
|
500
|
+
spacingGuides = spacingGuides.filter((g) => g.axis !== "y");
|
|
501
|
+
spacingGuides.push({
|
|
502
|
+
axis: "y",
|
|
503
|
+
position: guideX,
|
|
504
|
+
spacing: gapBelow,
|
|
505
|
+
ranges: [{
|
|
506
|
+
start: snapPos + rect.height,
|
|
507
|
+
end: target.y
|
|
508
|
+
}, {
|
|
509
|
+
start: target.y + target.height,
|
|
510
|
+
end: other.y
|
|
511
|
+
}]
|
|
512
|
+
});
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
for (const target of otherRects) {
|
|
519
|
+
const tp = getPoints(target);
|
|
520
|
+
const xChecks = [
|
|
521
|
+
{
|
|
522
|
+
source: rectPoints.left,
|
|
523
|
+
target: tp.left
|
|
524
|
+
},
|
|
525
|
+
{
|
|
526
|
+
source: rectPoints.left,
|
|
527
|
+
target: tp.right
|
|
528
|
+
},
|
|
529
|
+
{
|
|
530
|
+
source: rectPoints.left,
|
|
531
|
+
target: tp.centerX
|
|
532
|
+
},
|
|
533
|
+
{
|
|
534
|
+
source: rectPoints.right,
|
|
535
|
+
target: tp.left
|
|
536
|
+
},
|
|
537
|
+
{
|
|
538
|
+
source: rectPoints.right,
|
|
539
|
+
target: tp.right
|
|
540
|
+
},
|
|
541
|
+
{
|
|
542
|
+
source: rectPoints.right,
|
|
543
|
+
target: tp.centerX
|
|
544
|
+
},
|
|
545
|
+
{
|
|
546
|
+
source: rectPoints.centerX,
|
|
547
|
+
target: tp.left
|
|
548
|
+
},
|
|
549
|
+
{
|
|
550
|
+
source: rectPoints.centerX,
|
|
551
|
+
target: tp.right
|
|
552
|
+
},
|
|
553
|
+
{
|
|
554
|
+
source: rectPoints.centerX,
|
|
555
|
+
target: tp.centerX
|
|
556
|
+
}
|
|
557
|
+
];
|
|
558
|
+
for (const check of xChecks) {
|
|
559
|
+
const dist = Math.abs(check.source - check.target);
|
|
560
|
+
if (dist <= threshold && dist < minDistX) {
|
|
561
|
+
minDistX = dist;
|
|
562
|
+
snapX = check.target - (check.source - rect.x);
|
|
563
|
+
if (spacingGuides.some((g) => g.axis === "x")) spacingGuides = spacingGuides.filter((g) => g.axis !== "x");
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
const yChecks = [
|
|
567
|
+
{
|
|
568
|
+
source: rectPoints.top,
|
|
569
|
+
target: tp.top
|
|
570
|
+
},
|
|
571
|
+
{
|
|
572
|
+
source: rectPoints.top,
|
|
573
|
+
target: tp.bottom
|
|
574
|
+
},
|
|
575
|
+
{
|
|
576
|
+
source: rectPoints.top,
|
|
577
|
+
target: tp.centerY
|
|
578
|
+
},
|
|
579
|
+
{
|
|
580
|
+
source: rectPoints.bottom,
|
|
581
|
+
target: tp.top
|
|
582
|
+
},
|
|
583
|
+
{
|
|
584
|
+
source: rectPoints.bottom,
|
|
585
|
+
target: tp.bottom
|
|
586
|
+
},
|
|
587
|
+
{
|
|
588
|
+
source: rectPoints.bottom,
|
|
589
|
+
target: tp.centerY
|
|
590
|
+
},
|
|
591
|
+
{
|
|
592
|
+
source: rectPoints.centerY,
|
|
593
|
+
target: tp.top
|
|
594
|
+
},
|
|
595
|
+
{
|
|
596
|
+
source: rectPoints.centerY,
|
|
597
|
+
target: tp.bottom
|
|
598
|
+
},
|
|
599
|
+
{
|
|
600
|
+
source: rectPoints.centerY,
|
|
601
|
+
target: tp.centerY
|
|
602
|
+
}
|
|
603
|
+
];
|
|
604
|
+
for (const check of yChecks) {
|
|
605
|
+
const dist = Math.abs(check.source - check.target);
|
|
606
|
+
if (dist <= threshold && dist < minDistY) {
|
|
607
|
+
minDistY = dist;
|
|
608
|
+
snapY = check.target - (check.source - rect.y);
|
|
609
|
+
if (spacingGuides.some((g) => g.axis === "y")) spacingGuides = spacingGuides.filter((g) => g.axis !== "y");
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
if (snapX === null && snapY === null) return null;
|
|
614
|
+
const snapRect = {
|
|
615
|
+
x: snapX ?? rect.x,
|
|
616
|
+
y: snapY ?? rect.y,
|
|
617
|
+
width: rect.width,
|
|
618
|
+
height: rect.height
|
|
619
|
+
};
|
|
620
|
+
const snappedPoints = getPoints(snapRect);
|
|
621
|
+
const seenLines = /* @__PURE__ */ new Set();
|
|
622
|
+
for (const target of otherRects) {
|
|
623
|
+
const tp = getPoints(target);
|
|
624
|
+
const xAligns = [
|
|
625
|
+
{
|
|
626
|
+
source: snappedPoints.left,
|
|
627
|
+
target: tp.left
|
|
628
|
+
},
|
|
629
|
+
{
|
|
630
|
+
source: snappedPoints.left,
|
|
631
|
+
target: tp.right
|
|
632
|
+
},
|
|
633
|
+
{
|
|
634
|
+
source: snappedPoints.left,
|
|
635
|
+
target: tp.centerX
|
|
636
|
+
},
|
|
637
|
+
{
|
|
638
|
+
source: snappedPoints.right,
|
|
639
|
+
target: tp.left
|
|
640
|
+
},
|
|
641
|
+
{
|
|
642
|
+
source: snappedPoints.right,
|
|
643
|
+
target: tp.right
|
|
644
|
+
},
|
|
645
|
+
{
|
|
646
|
+
source: snappedPoints.right,
|
|
647
|
+
target: tp.centerX
|
|
648
|
+
},
|
|
649
|
+
{
|
|
650
|
+
source: snappedPoints.centerX,
|
|
651
|
+
target: tp.left
|
|
652
|
+
},
|
|
653
|
+
{
|
|
654
|
+
source: snappedPoints.centerX,
|
|
655
|
+
target: tp.right
|
|
656
|
+
},
|
|
657
|
+
{
|
|
658
|
+
source: snappedPoints.centerX,
|
|
659
|
+
target: tp.centerX
|
|
660
|
+
}
|
|
661
|
+
];
|
|
662
|
+
for (const a of xAligns) if (Math.abs(a.source - a.target) < .01) {
|
|
663
|
+
const key = `x:${a.target}`;
|
|
664
|
+
if (!seenLines.has(key)) {
|
|
665
|
+
seenLines.add(key);
|
|
666
|
+
snapLines.push({
|
|
667
|
+
axis: "x",
|
|
668
|
+
value: a.target
|
|
669
|
+
});
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
const yAligns = [
|
|
673
|
+
{
|
|
674
|
+
source: snappedPoints.top,
|
|
675
|
+
target: tp.top
|
|
676
|
+
},
|
|
677
|
+
{
|
|
678
|
+
source: snappedPoints.top,
|
|
679
|
+
target: tp.bottom
|
|
680
|
+
},
|
|
681
|
+
{
|
|
682
|
+
source: snappedPoints.top,
|
|
683
|
+
target: tp.centerY
|
|
684
|
+
},
|
|
685
|
+
{
|
|
686
|
+
source: snappedPoints.bottom,
|
|
687
|
+
target: tp.top
|
|
688
|
+
},
|
|
689
|
+
{
|
|
690
|
+
source: snappedPoints.bottom,
|
|
691
|
+
target: tp.bottom
|
|
692
|
+
},
|
|
693
|
+
{
|
|
694
|
+
source: snappedPoints.bottom,
|
|
695
|
+
target: tp.centerY
|
|
696
|
+
},
|
|
697
|
+
{
|
|
698
|
+
source: snappedPoints.centerY,
|
|
699
|
+
target: tp.top
|
|
700
|
+
},
|
|
701
|
+
{
|
|
702
|
+
source: snappedPoints.centerY,
|
|
703
|
+
target: tp.bottom
|
|
704
|
+
},
|
|
705
|
+
{
|
|
706
|
+
source: snappedPoints.centerY,
|
|
707
|
+
target: tp.centerY
|
|
708
|
+
}
|
|
709
|
+
];
|
|
710
|
+
for (const a of yAligns) if (Math.abs(a.source - a.target) < .01) {
|
|
711
|
+
const key = `y:${a.target}`;
|
|
712
|
+
if (!seenLines.has(key)) {
|
|
713
|
+
seenLines.add(key);
|
|
714
|
+
snapLines.push({
|
|
715
|
+
axis: "y",
|
|
716
|
+
value: a.target
|
|
717
|
+
});
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
return {
|
|
722
|
+
snapLines,
|
|
723
|
+
snapRect,
|
|
724
|
+
spacingGuides: spacingGuides.length > 0 ? spacingGuides : void 0
|
|
725
|
+
};
|
|
726
|
+
}
|
|
727
|
+
//#endregion
|
|
728
|
+
//#region src/edge-geometry.ts
|
|
729
|
+
function isPointNearPath(point, path, tolerance) {
|
|
730
|
+
const samplePoints = samplePathPoints(path);
|
|
731
|
+
for (let i = 0; i < samplePoints.length - 1; i++) {
|
|
732
|
+
const start = samplePoints[i];
|
|
733
|
+
const end = samplePoints[i + 1];
|
|
734
|
+
if (start && end && distanceToSegment(point, start, end) <= tolerance) return true;
|
|
735
|
+
}
|
|
736
|
+
return false;
|
|
737
|
+
}
|
|
738
|
+
/**
|
|
739
|
+
* Axis-aligned bounding box of a path's control/anchor points. A cubic bezier
|
|
740
|
+
* lies within the convex hull of its control points, so this is a conservative
|
|
741
|
+
* superset of the rendered curve — exact enough for viewport culling (no false
|
|
742
|
+
* negatives) and ~O(points) instead of sampling the curve. Returns null for an
|
|
743
|
+
* empty path.
|
|
744
|
+
*/
|
|
745
|
+
function pathBounds(path) {
|
|
746
|
+
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
747
|
+
for (const point of path.points) {
|
|
748
|
+
if (point.x < minX) minX = point.x;
|
|
749
|
+
if (point.y < minY) minY = point.y;
|
|
750
|
+
if (point.x > maxX) maxX = point.x;
|
|
751
|
+
if (point.y > maxY) maxY = point.y;
|
|
752
|
+
}
|
|
753
|
+
if (!isFinite(minX)) return null;
|
|
754
|
+
return {
|
|
755
|
+
x: minX,
|
|
756
|
+
y: minY,
|
|
757
|
+
width: maxX - minX,
|
|
758
|
+
height: maxY - minY
|
|
759
|
+
};
|
|
760
|
+
}
|
|
761
|
+
function rectIntersectsPath(rect, path) {
|
|
762
|
+
const samplePoints = samplePathPoints(path);
|
|
763
|
+
for (const point of samplePoints) if (pointInRect(point, rect)) return true;
|
|
764
|
+
const corners = rectCorners(rect);
|
|
765
|
+
for (let i = 0; i < samplePoints.length - 1; i++) {
|
|
766
|
+
const start = samplePoints[i];
|
|
767
|
+
const end = samplePoints[i + 1];
|
|
768
|
+
if (!start || !end) continue;
|
|
769
|
+
if (pointInRect(start, rect) || pointInRect(end, rect)) return true;
|
|
770
|
+
for (let cornerIndex = 0; cornerIndex < corners.length; cornerIndex++) {
|
|
771
|
+
const edgeStart = corners[cornerIndex];
|
|
772
|
+
const edgeEnd = corners[(cornerIndex + 1) % corners.length];
|
|
773
|
+
if (edgeStart && edgeEnd && segmentsIntersect(start, end, edgeStart, edgeEnd)) return true;
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
return false;
|
|
777
|
+
}
|
|
778
|
+
function basicEdgePath(edge, nodes) {
|
|
779
|
+
const source = nodes.find((node) => node.id === edge.sourceId);
|
|
780
|
+
const target = nodes.find((node) => node.id === edge.targetId);
|
|
781
|
+
if (!source || !target) return null;
|
|
782
|
+
return {
|
|
783
|
+
points: [resolveBasicEndpoint(source, edge.sourcePort), resolveBasicEndpoint(target, edge.targetPort)],
|
|
784
|
+
sourceDirection: "right",
|
|
785
|
+
targetDirection: "left"
|
|
786
|
+
};
|
|
787
|
+
}
|
|
788
|
+
function resolveBasicEndpoint(node, portName) {
|
|
789
|
+
if (portName && portName !== "*" && node.ports) {
|
|
790
|
+
const port = node.ports.find((candidate) => candidate.name === portName);
|
|
791
|
+
if (port) {
|
|
792
|
+
const position = resolvedPosition(node);
|
|
793
|
+
return {
|
|
794
|
+
x: position.x + port.x + port.width / 2,
|
|
795
|
+
y: position.y + port.y + port.height / 2
|
|
796
|
+
};
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
const bounds = resolvedBounds(node);
|
|
800
|
+
return {
|
|
801
|
+
x: bounds.x + bounds.width / 2,
|
|
802
|
+
y: bounds.y + bounds.height / 2
|
|
803
|
+
};
|
|
804
|
+
}
|
|
805
|
+
function samplePathPoints(path) {
|
|
806
|
+
if (path.points.length === 4) {
|
|
807
|
+
const [p0, p1, p2, p3] = path.points;
|
|
808
|
+
if (p0 && p1 && p2 && p3) return sampleCubicBezier(p0, p1, p2, p3, 20);
|
|
809
|
+
}
|
|
810
|
+
return path.points;
|
|
811
|
+
}
|
|
812
|
+
function sampleCubicBezier(p0, p1, p2, p3, steps) {
|
|
813
|
+
const points = [];
|
|
814
|
+
for (let i = 0; i <= steps; i++) {
|
|
815
|
+
const t = i / steps;
|
|
816
|
+
const mt = 1 - t;
|
|
817
|
+
points.push({
|
|
818
|
+
x: mt ** 3 * p0.x + 3 * mt ** 2 * t * p1.x + 3 * mt * t ** 2 * p2.x + t ** 3 * p3.x,
|
|
819
|
+
y: mt ** 3 * p0.y + 3 * mt ** 2 * t * p1.y + 3 * mt * t ** 2 * p2.y + t ** 3 * p3.y
|
|
820
|
+
});
|
|
821
|
+
}
|
|
822
|
+
return points;
|
|
823
|
+
}
|
|
824
|
+
function distanceToSegment(point, start, end) {
|
|
825
|
+
const dx = end.x - start.x;
|
|
826
|
+
const dy = end.y - start.y;
|
|
827
|
+
if (dx === 0 && dy === 0) return Math.hypot(point.x - start.x, point.y - start.y);
|
|
828
|
+
const t = Math.max(0, Math.min(1, ((point.x - start.x) * dx + (point.y - start.y) * dy) / (dx * dx + dy * dy)));
|
|
829
|
+
return Math.hypot(point.x - (start.x + t * dx), point.y - (start.y + t * dy));
|
|
830
|
+
}
|
|
831
|
+
function pointInRect(point, rect) {
|
|
832
|
+
return point.x >= rect.x && point.x <= rect.x + rect.width && point.y >= rect.y && point.y <= rect.y + rect.height;
|
|
833
|
+
}
|
|
834
|
+
function rectCorners(rect) {
|
|
835
|
+
return [
|
|
836
|
+
{
|
|
837
|
+
x: rect.x,
|
|
838
|
+
y: rect.y
|
|
839
|
+
},
|
|
840
|
+
{
|
|
841
|
+
x: rect.x + rect.width,
|
|
842
|
+
y: rect.y
|
|
843
|
+
},
|
|
844
|
+
{
|
|
845
|
+
x: rect.x + rect.width,
|
|
846
|
+
y: rect.y + rect.height
|
|
847
|
+
},
|
|
848
|
+
{
|
|
849
|
+
x: rect.x,
|
|
850
|
+
y: rect.y + rect.height
|
|
851
|
+
}
|
|
852
|
+
];
|
|
853
|
+
}
|
|
854
|
+
function segmentsIntersect(a1, a2, b1, b2) {
|
|
855
|
+
const d1 = direction(b1, b2, a1);
|
|
856
|
+
const d2 = direction(b1, b2, a2);
|
|
857
|
+
const d3 = direction(a1, a2, b1);
|
|
858
|
+
const d4 = direction(a1, a2, b2);
|
|
859
|
+
if ((d1 > 0 && d2 < 0 || d1 < 0 && d2 > 0) && (d3 > 0 && d4 < 0 || d3 < 0 && d4 > 0)) return true;
|
|
860
|
+
return d1 === 0 && onSegment(b1, b2, a1) || d2 === 0 && onSegment(b1, b2, a2) || d3 === 0 && onSegment(a1, a2, b1) || d4 === 0 && onSegment(a1, a2, b2);
|
|
861
|
+
}
|
|
862
|
+
function direction(a, b, c) {
|
|
863
|
+
return (c.x - a.x) * (b.y - a.y) - (c.y - a.y) * (b.x - a.x);
|
|
864
|
+
}
|
|
865
|
+
function onSegment(a, b, c) {
|
|
866
|
+
return Math.min(a.x, b.x) <= c.x && c.x <= Math.max(a.x, b.x) && Math.min(a.y, b.y) <= c.y && c.y <= Math.max(a.y, b.y);
|
|
867
|
+
}
|
|
868
|
+
//#endregion
|
|
869
|
+
//#region src/graph-index.ts
|
|
870
|
+
const nodeIndexCache = /* @__PURE__ */ new WeakMap();
|
|
871
|
+
const edgeIndexCache = /* @__PURE__ */ new WeakMap();
|
|
872
|
+
function indexNodes(nodes) {
|
|
873
|
+
let index = nodeIndexCache.get(nodes);
|
|
874
|
+
if (!index) {
|
|
875
|
+
index = /* @__PURE__ */ new Map();
|
|
876
|
+
for (const node of nodes) if (!index.has(node.id)) index.set(node.id, node);
|
|
877
|
+
nodeIndexCache.set(nodes, index);
|
|
878
|
+
}
|
|
879
|
+
return index;
|
|
880
|
+
}
|
|
881
|
+
function indexEdges(edges) {
|
|
882
|
+
let index = edgeIndexCache.get(edges);
|
|
883
|
+
if (!index) {
|
|
884
|
+
index = /* @__PURE__ */ new Map();
|
|
885
|
+
for (const edge of edges) if (!index.has(edge.id)) index.set(edge.id, edge);
|
|
886
|
+
edgeIndexCache.set(edges, index);
|
|
887
|
+
}
|
|
888
|
+
return index;
|
|
889
|
+
}
|
|
890
|
+
const childrenIndexCache = /* @__PURE__ */ new WeakMap();
|
|
891
|
+
/** parentId → child nodes, cached on array identity. Self-parents ignored. */
|
|
892
|
+
function indexChildren(nodes) {
|
|
893
|
+
let index = childrenIndexCache.get(nodes);
|
|
894
|
+
if (!index) {
|
|
895
|
+
index = /* @__PURE__ */ new Map();
|
|
896
|
+
for (const node of nodes) {
|
|
897
|
+
const parentId = node.parentId ?? null;
|
|
898
|
+
if (parentId == null || parentId === node.id) continue;
|
|
899
|
+
const children = index.get(parentId);
|
|
900
|
+
if (children) children.push(node);
|
|
901
|
+
else index.set(parentId, [node]);
|
|
902
|
+
}
|
|
903
|
+
childrenIndexCache.set(nodes, index);
|
|
904
|
+
}
|
|
905
|
+
return index;
|
|
906
|
+
}
|
|
907
|
+
const depthCache = /* @__PURE__ */ new WeakMap();
|
|
908
|
+
/**
|
|
909
|
+
* Hierarchy depth per node id (roots = 0), cached on array identity.
|
|
910
|
+
* Cycle-safe; missing parents count as roots. Renderers can sort by this so
|
|
911
|
+
* parents paint beneath children regardless of array order.
|
|
912
|
+
*/
|
|
913
|
+
function nodeDepths(nodes) {
|
|
914
|
+
let depths = depthCache.get(nodes);
|
|
915
|
+
if (!depths) {
|
|
916
|
+
const index = indexNodes(nodes);
|
|
917
|
+
const resolved = /* @__PURE__ */ new Map();
|
|
918
|
+
const depthOf = (node, seen) => {
|
|
919
|
+
const existing = resolved.get(node.id);
|
|
920
|
+
if (existing !== void 0) return existing;
|
|
921
|
+
const parentId = node.parentId ?? null;
|
|
922
|
+
if (parentId == null || parentId === node.id || seen.has(node.id)) {
|
|
923
|
+
resolved.set(node.id, 0);
|
|
924
|
+
return 0;
|
|
925
|
+
}
|
|
926
|
+
seen.add(node.id);
|
|
927
|
+
const parent = index.get(parentId);
|
|
928
|
+
const depth = parent ? depthOf(parent, seen) + 1 : 0;
|
|
929
|
+
resolved.set(node.id, depth);
|
|
930
|
+
return depth;
|
|
931
|
+
};
|
|
932
|
+
for (const node of nodes) depthOf(node, /* @__PURE__ */ new Set());
|
|
933
|
+
depths = resolved;
|
|
934
|
+
depthCache.set(nodes, depths);
|
|
935
|
+
}
|
|
936
|
+
return depths;
|
|
937
|
+
}
|
|
938
|
+
//#endregion
|
|
939
|
+
//#region src/hierarchy.ts
|
|
940
|
+
function resolveGroupPadding(padding) {
|
|
941
|
+
if (typeof padding === "number") return {
|
|
942
|
+
top: padding,
|
|
943
|
+
right: padding,
|
|
944
|
+
bottom: padding,
|
|
945
|
+
left: padding
|
|
946
|
+
};
|
|
947
|
+
return {
|
|
948
|
+
top: padding?.top ?? 16,
|
|
949
|
+
right: padding?.right ?? 16,
|
|
950
|
+
bottom: padding?.bottom ?? 16,
|
|
951
|
+
left: padding?.left ?? 16
|
|
952
|
+
};
|
|
953
|
+
}
|
|
954
|
+
function isGroup(node) {
|
|
955
|
+
return node.group !== void 0;
|
|
956
|
+
}
|
|
957
|
+
function isDerivedGroup(node) {
|
|
958
|
+
return node.group?.type === "derived";
|
|
959
|
+
}
|
|
960
|
+
/**
|
|
961
|
+
* Derive a `derived` group's geometry from its content size and the union of
|
|
962
|
+
* its children's bounds (pass null when the group has no children — the
|
|
963
|
+
* anchor point then places the group's top-left).
|
|
964
|
+
*/
|
|
965
|
+
function deriveGroupGeometry(contentSize, childrenBounds, layout, anchor = {
|
|
966
|
+
x: 0,
|
|
967
|
+
y: 0
|
|
968
|
+
}) {
|
|
969
|
+
const side = layout.content ?? "top";
|
|
970
|
+
const padding = resolveGroupPadding(layout.padding);
|
|
971
|
+
const minArea = layout.minChildArea ?? {
|
|
972
|
+
width: 0,
|
|
973
|
+
height: 0
|
|
974
|
+
};
|
|
975
|
+
let childArea;
|
|
976
|
+
if (childrenBounds) childArea = {
|
|
977
|
+
x: childrenBounds.x - padding.left,
|
|
978
|
+
y: childrenBounds.y - padding.top,
|
|
979
|
+
width: Math.max(childrenBounds.width + padding.left + padding.right, minArea.width),
|
|
980
|
+
height: Math.max(childrenBounds.height + padding.top + padding.bottom, minArea.height)
|
|
981
|
+
};
|
|
982
|
+
else {
|
|
983
|
+
const horizontal = side === "top" || side === "bottom";
|
|
984
|
+
childArea = {
|
|
985
|
+
x: anchor.x,
|
|
986
|
+
y: anchor.y,
|
|
987
|
+
width: Math.max(minArea.width, padding.left + padding.right, horizontal ? contentSize.width : 0),
|
|
988
|
+
height: Math.max(minArea.height, padding.top + padding.bottom, horizontal ? 0 : contentSize.height)
|
|
989
|
+
};
|
|
990
|
+
if (side === "top") childArea.y += contentSize.height;
|
|
991
|
+
if (side === "left") childArea.x += contentSize.width;
|
|
992
|
+
}
|
|
993
|
+
return composeGeometry(contentSize, childArea, side);
|
|
994
|
+
}
|
|
995
|
+
/**
|
|
996
|
+
* Geometry for a `fixed` group: stored bounds are authoritative; the content
|
|
997
|
+
* block is carved from the configured side and the child area is the
|
|
998
|
+
* remainder inset by padding.
|
|
999
|
+
*/
|
|
1000
|
+
function fixedGroupGeometry(bounds, contentSize, layout) {
|
|
1001
|
+
const side = layout.content ?? "top";
|
|
1002
|
+
const padding = resolveGroupPadding(layout.padding);
|
|
1003
|
+
let contentBounds;
|
|
1004
|
+
let childArea;
|
|
1005
|
+
if (side === "top" || side === "bottom") {
|
|
1006
|
+
const contentHeight = Math.min(contentSize.height, bounds.height);
|
|
1007
|
+
contentBounds = {
|
|
1008
|
+
x: bounds.x,
|
|
1009
|
+
y: side === "top" ? bounds.y : bounds.y + bounds.height - contentHeight,
|
|
1010
|
+
width: bounds.width,
|
|
1011
|
+
height: contentHeight
|
|
1012
|
+
};
|
|
1013
|
+
childArea = {
|
|
1014
|
+
x: bounds.x + padding.left,
|
|
1015
|
+
y: (side === "top" ? bounds.y + contentHeight : bounds.y) + padding.top,
|
|
1016
|
+
width: Math.max(bounds.width - padding.left - padding.right, 0),
|
|
1017
|
+
height: Math.max(bounds.height - contentHeight - padding.top - padding.bottom, 0)
|
|
1018
|
+
};
|
|
1019
|
+
} else {
|
|
1020
|
+
const contentWidth = Math.min(contentSize.width, bounds.width);
|
|
1021
|
+
contentBounds = {
|
|
1022
|
+
x: side === "left" ? bounds.x : bounds.x + bounds.width - contentWidth,
|
|
1023
|
+
y: bounds.y,
|
|
1024
|
+
width: contentWidth,
|
|
1025
|
+
height: bounds.height
|
|
1026
|
+
};
|
|
1027
|
+
childArea = {
|
|
1028
|
+
x: (side === "left" ? bounds.x + contentWidth : bounds.x) + padding.left,
|
|
1029
|
+
y: bounds.y + padding.top,
|
|
1030
|
+
width: Math.max(bounds.width - contentWidth - padding.left - padding.right, 0),
|
|
1031
|
+
height: Math.max(bounds.height - padding.top - padding.bottom, 0)
|
|
1032
|
+
};
|
|
1033
|
+
}
|
|
1034
|
+
return {
|
|
1035
|
+
bounds,
|
|
1036
|
+
contentBounds,
|
|
1037
|
+
childArea
|
|
1038
|
+
};
|
|
1039
|
+
}
|
|
1040
|
+
function composeGeometry(contentSize, childArea, side) {
|
|
1041
|
+
if (side === "top" || side === "bottom") {
|
|
1042
|
+
const width = Math.max(childArea.width, contentSize.width);
|
|
1043
|
+
const x = childArea.x - (width - childArea.width) / 2;
|
|
1044
|
+
const contentY = side === "top" ? childArea.y - contentSize.height : childArea.y + childArea.height;
|
|
1045
|
+
return {
|
|
1046
|
+
bounds: {
|
|
1047
|
+
x,
|
|
1048
|
+
y: side === "top" ? contentY : childArea.y,
|
|
1049
|
+
width,
|
|
1050
|
+
height: contentSize.height + childArea.height
|
|
1051
|
+
},
|
|
1052
|
+
contentBounds: {
|
|
1053
|
+
x,
|
|
1054
|
+
y: contentY,
|
|
1055
|
+
width,
|
|
1056
|
+
height: contentSize.height
|
|
1057
|
+
},
|
|
1058
|
+
childArea
|
|
1059
|
+
};
|
|
1060
|
+
}
|
|
1061
|
+
const height = Math.max(childArea.height, contentSize.height);
|
|
1062
|
+
const y = childArea.y - (height - childArea.height) / 2;
|
|
1063
|
+
const contentX = side === "left" ? childArea.x - contentSize.width : childArea.x + childArea.width;
|
|
1064
|
+
return {
|
|
1065
|
+
bounds: {
|
|
1066
|
+
x: side === "left" ? contentX : childArea.x,
|
|
1067
|
+
y,
|
|
1068
|
+
width: contentSize.width + childArea.width,
|
|
1069
|
+
height
|
|
1070
|
+
},
|
|
1071
|
+
contentBounds: {
|
|
1072
|
+
x: contentX,
|
|
1073
|
+
y,
|
|
1074
|
+
width: contentSize.width,
|
|
1075
|
+
height
|
|
1076
|
+
},
|
|
1077
|
+
childArea
|
|
1078
|
+
};
|
|
1079
|
+
}
|
|
1080
|
+
/**
|
|
1081
|
+
* The id of the node that contains this edge, or null for the root.
|
|
1082
|
+
* Override per engine via `FlowEngineOptions.edgeOwner`.
|
|
1083
|
+
*/
|
|
1084
|
+
function getEdgeOwner(edge, nodes) {
|
|
1085
|
+
const index = indexNodes(nodes);
|
|
1086
|
+
const source = index.get(edge.sourceId);
|
|
1087
|
+
const target = index.get(edge.targetId);
|
|
1088
|
+
if (!source) return null;
|
|
1089
|
+
if (!target || source.id === target.id) return source.parentId ?? null;
|
|
1090
|
+
const sourceChain = new Set([source.id]);
|
|
1091
|
+
let marker = source.parentId ?? null;
|
|
1092
|
+
while (marker && !sourceChain.has(marker)) {
|
|
1093
|
+
sourceChain.add(marker);
|
|
1094
|
+
marker = index.get(marker)?.parentId ?? null;
|
|
1095
|
+
}
|
|
1096
|
+
let candidate = target.id;
|
|
1097
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1098
|
+
while (candidate && !seen.has(candidate)) {
|
|
1099
|
+
if (sourceChain.has(candidate)) return candidate;
|
|
1100
|
+
seen.add(candidate);
|
|
1101
|
+
candidate = index.get(candidate)?.parentId ?? null;
|
|
1102
|
+
}
|
|
1103
|
+
return null;
|
|
1104
|
+
}
|
|
1105
|
+
//#endregion
|
|
1106
|
+
//#region src/store/index.ts
|
|
1107
|
+
enableMapSet();
|
|
1108
|
+
if (process.env.NODE_ENV === "production") setAutoFreeze(false);
|
|
1109
|
+
const UNDOABLE_EVENT_TYPES = [
|
|
1110
|
+
"createNode",
|
|
1111
|
+
"deleteNode",
|
|
1112
|
+
"updateNodeData",
|
|
1113
|
+
"createEdge",
|
|
1114
|
+
"deleteEdge",
|
|
1115
|
+
"updateEdgeData",
|
|
1116
|
+
"deleteSelected",
|
|
1117
|
+
"reparentNode",
|
|
1118
|
+
"dragStart",
|
|
1119
|
+
"resizeStart",
|
|
1120
|
+
"reconnectEdge",
|
|
1121
|
+
"moveNodes",
|
|
1122
|
+
"applyLayoutResult",
|
|
1123
|
+
"bakeLabelPositions"
|
|
1124
|
+
];
|
|
1125
|
+
function applyBeforeDelete(options, draft, reason, nodes, edges) {
|
|
1126
|
+
if (!options.beforeDelete) return false;
|
|
1127
|
+
const result = options.beforeDelete({
|
|
1128
|
+
graph: {
|
|
1129
|
+
...draft.graph,
|
|
1130
|
+
nodes: [...draft.graph.nodes],
|
|
1131
|
+
edges: [...draft.graph.edges]
|
|
1132
|
+
},
|
|
1133
|
+
reason,
|
|
1134
|
+
nodes,
|
|
1135
|
+
edges
|
|
1136
|
+
});
|
|
1137
|
+
if (result === false) return true;
|
|
1138
|
+
if (result && (result.nodes || result.edges)) {
|
|
1139
|
+
if (result.nodes) draft.graph.nodes = result.nodes;
|
|
1140
|
+
if (result.edges) draft.graph.edges = result.edges;
|
|
1141
|
+
draft.selection.clear();
|
|
1142
|
+
draft.focusedEntityId = null;
|
|
1143
|
+
if (draft.draggedEntityId && !draft.graph.nodes.some((node) => node.id === draft.draggedEntityId)) draft.draggedEntityId = null;
|
|
1144
|
+
return true;
|
|
1145
|
+
}
|
|
1146
|
+
return false;
|
|
1147
|
+
}
|
|
1148
|
+
/**
|
|
1149
|
+
* Validate an edge about to be inserted. Returns an actionable error message,
|
|
1150
|
+
* or null when the edge is valid. Used by `createEdge` and undo tracking.
|
|
1151
|
+
*/
|
|
1152
|
+
function validateNewEdge(ctx, edge) {
|
|
1153
|
+
if (ctx.graph.edges.some((e) => e.id === edge.id)) return `createEdge rejected: an edge with id "${edge.id}" already exists. Edge ids must be unique — use reconnectEdge or updateEdgeData to modify the existing edge, or create the edge with a different id.`;
|
|
1154
|
+
const missing = [];
|
|
1155
|
+
if (!ctx.graph.nodes.some((n) => n.id === edge.sourceId)) missing.push(`source node "${edge.sourceId}"`);
|
|
1156
|
+
if (!ctx.graph.nodes.some((n) => n.id === edge.targetId)) missing.push(`target node "${edge.targetId}"`);
|
|
1157
|
+
if (missing.length > 0) return `createEdge rejected: edge "${edge.id}" references ${missing.join(" and ")}, which ${missing.length === 1 ? "does" : "do"} not exist in the graph. Create the node(s) first, then the edge.`;
|
|
1158
|
+
return null;
|
|
1159
|
+
}
|
|
1160
|
+
/**
|
|
1161
|
+
* Validate a reparent operation. Returns an error message or null when valid.
|
|
1162
|
+
* Rejects self-parenting, non-existent parents, and cycles (parentId is a
|
|
1163
|
+
* descendant of nodeId).
|
|
1164
|
+
*/
|
|
1165
|
+
function reparentProblem(graph, nodeId, parentId) {
|
|
1166
|
+
if (parentId === null) return null;
|
|
1167
|
+
if (parentId === nodeId) return `reparentNode rejected: node "${nodeId}" cannot be its own parent.`;
|
|
1168
|
+
if (!graph.nodes.some((n) => n.id === parentId)) return `reparentNode rejected: parent node "${parentId}" does not exist in the graph.`;
|
|
1169
|
+
const nodeIndex = new Map(graph.nodes.map((n) => [n.id, n]));
|
|
1170
|
+
let candidate = parentId;
|
|
1171
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1172
|
+
while (candidate && !seen.has(candidate)) {
|
|
1173
|
+
if (candidate === nodeId) return `reparentNode rejected: node "${parentId}" is a descendant of "${nodeId}", which would create a cycle.`;
|
|
1174
|
+
seen.add(candidate);
|
|
1175
|
+
candidate = nodeIndex.get(candidate)?.parentId ?? null;
|
|
1176
|
+
}
|
|
1177
|
+
return null;
|
|
1178
|
+
}
|
|
1179
|
+
/** Drop hover/highlight references to entities that no longer exist. */
|
|
1180
|
+
function pruneVisualState(draft, deletedIds) {
|
|
1181
|
+
for (const id of deletedIds) draft.hoveredEntityIds.delete(id);
|
|
1182
|
+
for (const [kind, bucket] of draft.highlights) {
|
|
1183
|
+
for (const id of deletedIds) bucket.delete(id);
|
|
1184
|
+
if (bucket.size === 0) draft.highlights.delete(kind);
|
|
1185
|
+
}
|
|
1186
|
+
}
|
|
1187
|
+
function isUndoableEventType(type) {
|
|
1188
|
+
return UNDOABLE_EVENT_TYPES.includes(type);
|
|
1189
|
+
}
|
|
1190
|
+
function planNodeMovement(ctx, dx, dy, options = {}) {
|
|
1191
|
+
const nodes = ctx.graph.nodes;
|
|
1192
|
+
const edges = ctx.graph.edges;
|
|
1193
|
+
const nodeIndex = indexNodes(nodes);
|
|
1194
|
+
const edgeIndex = indexEdges(edges);
|
|
1195
|
+
const movingNodeIds = /* @__PURE__ */ new Set();
|
|
1196
|
+
const labelEdgeIds = /* @__PURE__ */ new Set();
|
|
1197
|
+
for (const id of ctx.selection) {
|
|
1198
|
+
const node = nodeIndex.get(id);
|
|
1199
|
+
if (node) {
|
|
1200
|
+
if (node.draggable !== false) movingNodeIds.add(id);
|
|
1201
|
+
continue;
|
|
1202
|
+
}
|
|
1203
|
+
if (options.includeLabels) {
|
|
1204
|
+
const edge = edgeIndex.get(id);
|
|
1205
|
+
if (edge && edge.draggable !== false && edge.label) labelEdgeIds.add(id);
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
if (movingNodeIds.size === 0 && labelEdgeIds.size === 0) return null;
|
|
1209
|
+
let childrenByParent = null;
|
|
1210
|
+
for (const node of nodes) {
|
|
1211
|
+
const parentId = node.parentId ?? null;
|
|
1212
|
+
if (parentId == null || parentId === node.id) continue;
|
|
1213
|
+
if (!childrenByParent) childrenByParent = /* @__PURE__ */ new Map();
|
|
1214
|
+
const children = childrenByParent.get(parentId);
|
|
1215
|
+
if (children) children.push(node.id);
|
|
1216
|
+
else childrenByParent.set(parentId, [node.id]);
|
|
1217
|
+
}
|
|
1218
|
+
const deltasById = /* @__PURE__ */ new Map();
|
|
1219
|
+
for (const id of movingNodeIds) {
|
|
1220
|
+
const node = nodeIndex.get(id);
|
|
1221
|
+
const oldDx = node.dx ?? 0;
|
|
1222
|
+
const oldDy = node.dy ?? 0;
|
|
1223
|
+
let newDx = oldDx + dx;
|
|
1224
|
+
let newDy = oldDy + dy;
|
|
1225
|
+
if (ctx.nodeExtent) {
|
|
1226
|
+
const nodeSize = resolvedSize(node);
|
|
1227
|
+
const clampedX = Math.max(ctx.nodeExtent[0].x, Math.min(node.x + newDx, ctx.nodeExtent[1].x - nodeSize.width));
|
|
1228
|
+
const clampedY = Math.max(ctx.nodeExtent[0].y, Math.min(node.y + newDy, ctx.nodeExtent[1].y - nodeSize.height));
|
|
1229
|
+
newDx = clampedX - node.x;
|
|
1230
|
+
newDy = clampedY - node.y;
|
|
1231
|
+
}
|
|
1232
|
+
const parent = node.parentId ? nodeIndex.get(node.parentId) : void 0;
|
|
1233
|
+
if (parent?.group?.type === "fixed") {
|
|
1234
|
+
const axes = parent.group.clampChildren ?? "none";
|
|
1235
|
+
if (axes !== "none") {
|
|
1236
|
+
const area = fixedGroupGeometry({
|
|
1237
|
+
x: parent.x + (parent.dx ?? 0),
|
|
1238
|
+
y: parent.y + (parent.dy ?? 0),
|
|
1239
|
+
width: parent.style?.customWidth ?? parent.width,
|
|
1240
|
+
height: parent.style?.customHeight ?? parent.height
|
|
1241
|
+
}, parent.group.contentSize ?? {
|
|
1242
|
+
width: 0,
|
|
1243
|
+
height: 0
|
|
1244
|
+
}, parent.group).childArea;
|
|
1245
|
+
const { width: nodeWidth, height: nodeHeight } = resolvedSize(node);
|
|
1246
|
+
if (axes === "x" || axes === "both") newDx = Math.max(area.x, Math.min(node.x + newDx, area.x + area.width - nodeWidth)) - node.x;
|
|
1247
|
+
if (axes === "y" || axes === "both") newDy = Math.max(area.y, Math.min(node.y + newDy, area.y + area.height - nodeHeight)) - node.y;
|
|
1248
|
+
}
|
|
1249
|
+
}
|
|
1250
|
+
if (ctx.constrainNodeMovement) {
|
|
1251
|
+
const adjusted = ctx.constrainNodeMovement(node, {
|
|
1252
|
+
x: node.x + newDx,
|
|
1253
|
+
y: node.y + newDy
|
|
1254
|
+
}, ctx);
|
|
1255
|
+
if (adjusted) {
|
|
1256
|
+
newDx = adjusted.x - node.x;
|
|
1257
|
+
newDy = adjusted.y - node.y;
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
const applied = {
|
|
1261
|
+
x: newDx - oldDx,
|
|
1262
|
+
y: newDy - oldDy
|
|
1263
|
+
};
|
|
1264
|
+
if (applied.x === 0 && applied.y === 0) continue;
|
|
1265
|
+
deltasById.set(id, applied);
|
|
1266
|
+
if (childrenByParent) collectDescendantDeltas(childrenByParent, id, applied, movingNodeIds, deltasById);
|
|
1267
|
+
}
|
|
1268
|
+
const nodeDeltasByIndex = /* @__PURE__ */ new Map();
|
|
1269
|
+
if (deltasById.size > 0) for (let i = 0; i < nodes.length; i++) {
|
|
1270
|
+
const delta = deltasById.get(nodes[i].id);
|
|
1271
|
+
if (delta) nodeDeltasByIndex.set(i, delta);
|
|
1272
|
+
}
|
|
1273
|
+
const labelEdgeIndexes = [];
|
|
1274
|
+
if (labelEdgeIds.size > 0) {
|
|
1275
|
+
for (let i = 0; i < edges.length; i++) if (labelEdgeIds.has(edges[i].id)) labelEdgeIndexes.push(i);
|
|
1276
|
+
}
|
|
1277
|
+
const labelDeltasByIndex = /* @__PURE__ */ new Map();
|
|
1278
|
+
if (deltasById.size > 0) for (let i = 0; i < edges.length; i++) {
|
|
1279
|
+
const edge = edges[i];
|
|
1280
|
+
if (!edge.label || edge.draggable === false) continue;
|
|
1281
|
+
if (labelEdgeIds.has(edge.id)) continue;
|
|
1282
|
+
const source = deltasById.get(edge.sourceId);
|
|
1283
|
+
if (!source) continue;
|
|
1284
|
+
const target = edge.targetId === edge.sourceId ? source : deltasById.get(edge.targetId);
|
|
1285
|
+
if (!target || target.x !== source.x || target.y !== source.y) continue;
|
|
1286
|
+
labelDeltasByIndex.set(i, source);
|
|
1287
|
+
}
|
|
1288
|
+
if (nodeDeltasByIndex.size === 0 && labelEdgeIndexes.length === 0 && labelDeltasByIndex.size === 0) return null;
|
|
1289
|
+
return {
|
|
1290
|
+
nodeDeltasByIndex,
|
|
1291
|
+
labelEdgeIndexes,
|
|
1292
|
+
labelDeltasByIndex
|
|
1293
|
+
};
|
|
1294
|
+
}
|
|
1295
|
+
function collectDescendantDeltas(childrenByParent, parentId, delta, movingIds, out, visited = new Set([parentId])) {
|
|
1296
|
+
const children = childrenByParent.get(parentId);
|
|
1297
|
+
if (!children) return;
|
|
1298
|
+
for (const childId of children) {
|
|
1299
|
+
if (visited.has(childId) || movingIds.has(childId)) continue;
|
|
1300
|
+
visited.add(childId);
|
|
1301
|
+
if (!out.has(childId)) out.set(childId, delta);
|
|
1302
|
+
collectDescendantDeltas(childrenByParent, childId, delta, movingIds, out, visited);
|
|
1303
|
+
}
|
|
1304
|
+
}
|
|
1305
|
+
function applyMovementPlan(draft, plan, labelDx, labelDy) {
|
|
1306
|
+
for (const [index, delta] of plan.nodeDeltasByIndex) {
|
|
1307
|
+
const node = draft.graph.nodes[index];
|
|
1308
|
+
if (!node) continue;
|
|
1309
|
+
node.dx = (node.dx ?? 0) + delta.x;
|
|
1310
|
+
node.dy = (node.dy ?? 0) + delta.y;
|
|
1311
|
+
}
|
|
1312
|
+
for (const index of plan.labelEdgeIndexes) {
|
|
1313
|
+
const edge = draft.graph.edges[index];
|
|
1314
|
+
if (!edge?.label) continue;
|
|
1315
|
+
edge.label.dx = (edge.label.dx ?? 0) + labelDx;
|
|
1316
|
+
edge.label.dy = (edge.label.dy ?? 0) + labelDy;
|
|
1317
|
+
}
|
|
1318
|
+
for (const [index, delta] of plan.labelDeltasByIndex) {
|
|
1319
|
+
const edge = draft.graph.edges[index];
|
|
1320
|
+
if (!edge?.label) continue;
|
|
1321
|
+
edge.label.dx = (edge.label.dx ?? 0) + delta.x;
|
|
1322
|
+
edge.label.dy = (edge.label.dy ?? 0) + delta.y;
|
|
1323
|
+
}
|
|
1324
|
+
}
|
|
1325
|
+
/**
|
|
1326
|
+
* Remove nodes from the draft graph and resolve connected edges according to
|
|
1327
|
+
* the configured `onDeleteNodes` strategy. Shared by `deleteNode` and
|
|
1328
|
+
* `deleteSelected` so both events always agree on edge handling.
|
|
1329
|
+
*
|
|
1330
|
+
* `edgesToDelete` are edge ids that must be removed regardless of strategy
|
|
1331
|
+
* (explicitly selected/targeted edges).
|
|
1332
|
+
*/
|
|
1333
|
+
function applyNodeDeletionStrategy(draft, nodesToDelete, edgesToDelete, strategy) {
|
|
1334
|
+
if (typeof strategy === "function") {
|
|
1335
|
+
const result = strategy({
|
|
1336
|
+
...draft.graph,
|
|
1337
|
+
nodes: [...draft.graph.nodes],
|
|
1338
|
+
edges: [...draft.graph.edges]
|
|
1339
|
+
}, nodesToDelete);
|
|
1340
|
+
draft.graph.nodes = result.nodes;
|
|
1341
|
+
draft.graph.edges = result.edges;
|
|
1342
|
+
return;
|
|
1343
|
+
}
|
|
1344
|
+
draft.graph.nodes = draft.graph.nodes.filter((n) => !nodesToDelete.has(n.id));
|
|
1345
|
+
if (strategy === "retarget") {
|
|
1346
|
+
const kept = [];
|
|
1347
|
+
for (const edge of draft.graph.edges) {
|
|
1348
|
+
if (edgesToDelete.has(edge.id)) continue;
|
|
1349
|
+
const srcDel = nodesToDelete.has(edge.sourceId);
|
|
1350
|
+
const tgtDel = nodesToDelete.has(edge.targetId);
|
|
1351
|
+
if (srcDel && tgtDel) continue;
|
|
1352
|
+
if (srcDel) edge.sourceId = edge.targetId;
|
|
1353
|
+
else if (tgtDel) edge.targetId = edge.sourceId;
|
|
1354
|
+
kept.push(edge);
|
|
1355
|
+
}
|
|
1356
|
+
draft.graph.edges = kept;
|
|
1357
|
+
return;
|
|
1358
|
+
}
|
|
1359
|
+
draft.graph.edges = draft.graph.edges.filter((e) => !edgesToDelete.has(e.id) && !nodesToDelete.has(e.sourceId) && !nodesToDelete.has(e.targetId));
|
|
1360
|
+
}
|
|
1361
|
+
function hasSelectedEntitiesToDelete(ctx) {
|
|
1362
|
+
for (const id of ctx.selection) if (ctx.graph.nodes.some((node) => node.id === id) || ctx.graph.edges.some((edge) => edge.id === id)) return true;
|
|
1363
|
+
return false;
|
|
1364
|
+
}
|
|
1365
|
+
function shouldTrackUndoEvent(event, snapshot) {
|
|
1366
|
+
if (!isUndoableEventType(event.type)) return false;
|
|
1367
|
+
const ctx = snapshot.context;
|
|
1368
|
+
switch (event.type) {
|
|
1369
|
+
case "createNode": return !ctx.graph.nodes.some((node) => node.id === event.node.id);
|
|
1370
|
+
case "createEdge": return validateNewEdge(ctx, event.edge) === null;
|
|
1371
|
+
case "deleteNode": return ctx.graph.nodes.some((node) => node.id === event.id);
|
|
1372
|
+
case "deleteEdge": return ctx.graph.edges.some((edge) => edge.id === event.id);
|
|
1373
|
+
case "updateNodeData": {
|
|
1374
|
+
const node = ctx.graph.nodes.find((candidate) => candidate.id === event.id);
|
|
1375
|
+
return !!node && Object.entries(event.data).some(([key, value]) => node.data?.[key] !== value);
|
|
1376
|
+
}
|
|
1377
|
+
case "updateEdgeData": {
|
|
1378
|
+
const edge = ctx.graph.edges.find((candidate) => candidate.id === event.id);
|
|
1379
|
+
return !!edge && Object.entries(event.data).some(([key, value]) => edge.data?.[key] !== value);
|
|
1380
|
+
}
|
|
1381
|
+
case "deleteSelected": return hasSelectedEntitiesToDelete(ctx);
|
|
1382
|
+
case "reparentNode": {
|
|
1383
|
+
const node = ctx.graph.nodes.find((candidate) => candidate.id === event.id);
|
|
1384
|
+
return !!node && (node.parentId ?? null) !== event.parentId;
|
|
1385
|
+
}
|
|
1386
|
+
case "dragStart": return !!event.transactionId && (ctx.graph.nodes.some((node) => node.id === event.id) || ctx.graph.edges.some((edge) => edge.id === event.id));
|
|
1387
|
+
case "moveNodes": return (event.direction.x !== 0 || event.direction.y !== 0) && [...ctx.selection].some((id) => isExplicitlyDraggable(id, ctx.graph) && ctx.graph.nodes.some((node) => node.id === id));
|
|
1388
|
+
case "resizeStart": return !!event.transactionId && ctx.graph.nodes.some((node) => node.id === event.id);
|
|
1389
|
+
case "reconnectEdge": {
|
|
1390
|
+
const edge = ctx.graph.edges.find((candidate) => candidate.id === event.edgeId);
|
|
1391
|
+
if (!edge) return false;
|
|
1392
|
+
return event.newSourceId !== void 0 && event.newSourceId !== edge.sourceId || event.newTargetId !== void 0 && event.newTargetId !== edge.targetId || event.newSourcePort !== void 0 && event.newSourcePort !== edge.sourcePort || event.newTargetPort !== void 0 && event.newTargetPort !== edge.targetPort;
|
|
1393
|
+
}
|
|
1394
|
+
case "applyLayoutResult": return event.positions.size > 0 || (event.edgeLabelPositions?.size ?? 0) > 0;
|
|
1395
|
+
case "bakeLabelPositions": return event.positions.size > 0;
|
|
1396
|
+
default: return true;
|
|
1397
|
+
}
|
|
1398
|
+
}
|
|
1399
|
+
/**
|
|
1400
|
+
* Snapshot a (possibly drafted) graph into plain data for emission — immer
|
|
1401
|
+
* drafts are revoked before listeners run. Plain values (e.g. right after
|
|
1402
|
+
* `draft.graph = event.graph`) pass through untouched.
|
|
1403
|
+
*/
|
|
1404
|
+
function snapshotGraph(graph) {
|
|
1405
|
+
return isDraft(graph) ? current(graph) : graph;
|
|
1406
|
+
}
|
|
1407
|
+
/** Copy a (possibly drafted) viewport into plain data for emission. */
|
|
1408
|
+
function plainViewport(viewport) {
|
|
1409
|
+
return {
|
|
1410
|
+
x: viewport.x,
|
|
1411
|
+
y: viewport.y,
|
|
1412
|
+
zoom: viewport.zoom
|
|
1413
|
+
};
|
|
1414
|
+
}
|
|
1415
|
+
function createEventObject(type, event) {
|
|
1416
|
+
return {
|
|
1417
|
+
type,
|
|
1418
|
+
...event ?? {}
|
|
1419
|
+
};
|
|
1420
|
+
}
|
|
1421
|
+
function isExplicitlyVisibleNode(node) {
|
|
1422
|
+
return !node.hidden;
|
|
1423
|
+
}
|
|
1424
|
+
function isExplicitlyVisibleEdge(edge, nodes) {
|
|
1425
|
+
if (edge.hidden) return false;
|
|
1426
|
+
const source = nodes.find((node) => node.id === edge.sourceId);
|
|
1427
|
+
const target = nodes.find((node) => node.id === edge.targetId);
|
|
1428
|
+
return !!source && !!target && isExplicitlyVisibleNode(source) && isExplicitlyVisibleNode(target);
|
|
1429
|
+
}
|
|
1430
|
+
function isExplicitlyDraggable(id, graph) {
|
|
1431
|
+
const node = graph.nodes.find((candidate) => candidate.id === id);
|
|
1432
|
+
if (node) return node.draggable !== false;
|
|
1433
|
+
const edge = graph.edges.find((candidate) => candidate.id === id);
|
|
1434
|
+
if (edge) return edge.draggable !== false;
|
|
1435
|
+
return false;
|
|
1436
|
+
}
|
|
1437
|
+
function getMode(profile, mode) {
|
|
1438
|
+
return profile.modes[mode] ? mode : profile.defaultMode;
|
|
1439
|
+
}
|
|
1440
|
+
function setModeTransition(profile) {
|
|
1441
|
+
return (ctx, event) => produce(ctx, (draft) => {
|
|
1442
|
+
draft.mode = getMode(profile, event.mode);
|
|
1443
|
+
});
|
|
1444
|
+
}
|
|
1445
|
+
function dispatchProfile(profile, ctx, event, enqueue, fallback) {
|
|
1446
|
+
const mode = getMode(profile, ctx.mode);
|
|
1447
|
+
const transition = profile.modes[mode]?.on?.[event.type] ?? profile.on?.[event.type] ?? fallback;
|
|
1448
|
+
if (!transition) return ctx;
|
|
1449
|
+
return transition(ctx, event, enqueue);
|
|
1450
|
+
}
|
|
1451
|
+
function createFlowStore(initialGraph = {
|
|
1452
|
+
nodes: [],
|
|
1453
|
+
edges: [],
|
|
1454
|
+
direction: "down"
|
|
1455
|
+
}, options = {}) {
|
|
1456
|
+
const profile = options.profile ?? defaultFlowProfile;
|
|
1457
|
+
const initialMode = getMode(profile, options.mode ?? profile.defaultMode);
|
|
1458
|
+
const initialContext = {
|
|
1459
|
+
graph: initialGraph,
|
|
1460
|
+
parentNodeId: null,
|
|
1461
|
+
depth: null,
|
|
1462
|
+
viewport: createViewport(),
|
|
1463
|
+
viewportSize: {
|
|
1464
|
+
width: 0,
|
|
1465
|
+
height: 0
|
|
1466
|
+
},
|
|
1467
|
+
viewportStack: [],
|
|
1468
|
+
minZoom: options.minZoom ?? .1,
|
|
1469
|
+
maxZoom: options.maxZoom ?? 4,
|
|
1470
|
+
translateExtent: options.translateExtent ?? null,
|
|
1471
|
+
nodeExtent: options.nodeExtent ?? null,
|
|
1472
|
+
selection: /* @__PURE__ */ new Set(),
|
|
1473
|
+
selectionBox: null,
|
|
1474
|
+
hoveredEntityIds: /* @__PURE__ */ new Set(),
|
|
1475
|
+
highlights: /* @__PURE__ */ new Map(),
|
|
1476
|
+
focusedEntityId: null,
|
|
1477
|
+
measurements: /* @__PURE__ */ new Map(),
|
|
1478
|
+
canvasState: "idle",
|
|
1479
|
+
draggedEntityId: null,
|
|
1480
|
+
dragThreshold: options.dragThreshold ?? 3,
|
|
1481
|
+
constrainNodeMovement: options.constrainNodeMovement ?? null,
|
|
1482
|
+
autoPanOnDrag: options.autoPanOnDrag ?? true,
|
|
1483
|
+
autoPanThreshold: options.autoPanThreshold ?? 40,
|
|
1484
|
+
autoPanSpeed: options.autoPanSpeed ?? 15,
|
|
1485
|
+
connection: null,
|
|
1486
|
+
connectOnClick: options.connectOnClick ?? false,
|
|
1487
|
+
connectionClickStartHandle: null,
|
|
1488
|
+
panOrigin: null,
|
|
1489
|
+
panViewportOrigin: null,
|
|
1490
|
+
resizingEntityId: null,
|
|
1491
|
+
resizeHandle: null,
|
|
1492
|
+
resizeCorners: null,
|
|
1493
|
+
mode: initialMode,
|
|
1494
|
+
selectionMode: options.selectionMode ?? "partial",
|
|
1495
|
+
snapToGrid: options.snapToGrid ?? false,
|
|
1496
|
+
snapGrid: options.snapGrid ?? [10, 10],
|
|
1497
|
+
enableSnapLines: options.enableSnapLines ?? false,
|
|
1498
|
+
snapThreshold: options.snapThreshold ?? 5,
|
|
1499
|
+
enableSpacingGuides: options.enableSpacingGuides ?? false,
|
|
1500
|
+
autoLayout: {
|
|
1501
|
+
status: "idle",
|
|
1502
|
+
algorithm: null
|
|
1503
|
+
},
|
|
1504
|
+
colorMode: options.colorMode ?? "system"
|
|
1505
|
+
};
|
|
1506
|
+
return createStore({
|
|
1507
|
+
context: initialContext,
|
|
1508
|
+
on: {
|
|
1509
|
+
createNode: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1510
|
+
if (draft.graph.nodes.some((n) => n.id === event.node.id)) {
|
|
1511
|
+
enqueue.emit.error({
|
|
1512
|
+
error: /* @__PURE__ */ new Error(`createNode rejected: a node with id "${event.node.id}" already exists. Node ids must be unique — use updateNodeData to modify the existing node, or create the node with a different id.`),
|
|
1513
|
+
event: createEventObject("createNode", event)
|
|
1514
|
+
});
|
|
1515
|
+
return;
|
|
1516
|
+
}
|
|
1517
|
+
draft.graph.nodes.push(event.node);
|
|
1518
|
+
enqueue.emit.graphChanged({
|
|
1519
|
+
graph: snapshotGraph(draft.graph),
|
|
1520
|
+
event: createEventObject("createNode", event)
|
|
1521
|
+
});
|
|
1522
|
+
}),
|
|
1523
|
+
deleteNode: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1524
|
+
const node = draft.graph.nodes.find((n) => n.id === event.id);
|
|
1525
|
+
const edges = draft.graph.edges.filter((e) => e.sourceId === event.id || e.targetId === event.id);
|
|
1526
|
+
if (node && applyBeforeDelete(options, draft, "deleteNode", [node], edges)) return;
|
|
1527
|
+
const edgeIdsBefore = draft.graph.edges.map((e) => e.id);
|
|
1528
|
+
applyNodeDeletionStrategy(draft, new Set([event.id]), /* @__PURE__ */ new Set(), options.onDeleteNodes ?? "cascade");
|
|
1529
|
+
const remainingEdgeIds = new Set(draft.graph.edges.map((e) => e.id));
|
|
1530
|
+
const deletedEdgeIds = edgeIdsBefore.filter((id) => !remainingEdgeIds.has(id));
|
|
1531
|
+
draft.selection.delete(event.id);
|
|
1532
|
+
draft.measurements.delete(event.id);
|
|
1533
|
+
for (const edgeId of deletedEdgeIds) draft.measurements.delete(edgeId);
|
|
1534
|
+
if (draft.focusedEntityId === event.id) draft.focusedEntityId = null;
|
|
1535
|
+
pruneVisualState(draft, [event.id, ...deletedEdgeIds]);
|
|
1536
|
+
enqueue.emit.entitiesDeleted({
|
|
1537
|
+
ids: [event.id, ...deletedEdgeIds],
|
|
1538
|
+
event: createEventObject("deleteNode", event)
|
|
1539
|
+
});
|
|
1540
|
+
enqueue.emit.graphChanged({
|
|
1541
|
+
graph: snapshotGraph(draft.graph),
|
|
1542
|
+
event: createEventObject("deleteNode", event)
|
|
1543
|
+
});
|
|
1544
|
+
enqueue.emit.selectionChanged({
|
|
1545
|
+
selection: new Set(draft.selection),
|
|
1546
|
+
event: createEventObject("deleteNode", event)
|
|
1547
|
+
});
|
|
1548
|
+
}),
|
|
1549
|
+
updateNodeData: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1550
|
+
const node = draft.graph.nodes.find((n) => n.id === event.id);
|
|
1551
|
+
if (node) {
|
|
1552
|
+
if (!node.data) node.data = {};
|
|
1553
|
+
Object.assign(node.data, event.data);
|
|
1554
|
+
enqueue.emit.graphChanged({
|
|
1555
|
+
graph: snapshotGraph(draft.graph),
|
|
1556
|
+
event: createEventObject("updateNodeData", event)
|
|
1557
|
+
});
|
|
1558
|
+
}
|
|
1559
|
+
}),
|
|
1560
|
+
createEdge: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1561
|
+
const problem = validateNewEdge(draft, event.edge);
|
|
1562
|
+
if (problem) {
|
|
1563
|
+
enqueue.emit.error({
|
|
1564
|
+
error: new Error(problem),
|
|
1565
|
+
event: createEventObject("createEdge", event)
|
|
1566
|
+
});
|
|
1567
|
+
return;
|
|
1568
|
+
}
|
|
1569
|
+
draft.graph.edges.push(event.edge);
|
|
1570
|
+
enqueue.emit.graphChanged({
|
|
1571
|
+
graph: snapshotGraph(draft.graph),
|
|
1572
|
+
event: createEventObject("createEdge", event)
|
|
1573
|
+
});
|
|
1574
|
+
}),
|
|
1575
|
+
deleteEdge: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1576
|
+
const edge = draft.graph.edges.find((e) => e.id === event.id);
|
|
1577
|
+
if (edge && applyBeforeDelete(options, draft, "deleteEdge", [], [edge])) return;
|
|
1578
|
+
draft.graph.edges = draft.graph.edges.filter((e) => e.id !== event.id);
|
|
1579
|
+
draft.selection.delete(event.id);
|
|
1580
|
+
draft.measurements.delete(event.id);
|
|
1581
|
+
pruneVisualState(draft, [event.id]);
|
|
1582
|
+
enqueue.emit.entitiesDeleted({
|
|
1583
|
+
ids: [event.id],
|
|
1584
|
+
event: createEventObject("deleteEdge", event)
|
|
1585
|
+
});
|
|
1586
|
+
enqueue.emit.graphChanged({
|
|
1587
|
+
graph: snapshotGraph(draft.graph),
|
|
1588
|
+
event: createEventObject("deleteEdge", event)
|
|
1589
|
+
});
|
|
1590
|
+
enqueue.emit.selectionChanged({
|
|
1591
|
+
selection: new Set(draft.selection),
|
|
1592
|
+
event: createEventObject("deleteEdge", event)
|
|
1593
|
+
});
|
|
1594
|
+
}),
|
|
1595
|
+
updateEdgeData: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1596
|
+
const edge = draft.graph.edges.find((e) => e.id === event.id);
|
|
1597
|
+
if (edge) {
|
|
1598
|
+
if (!edge.data) edge.data = {};
|
|
1599
|
+
Object.assign(edge.data, event.data);
|
|
1600
|
+
enqueue.emit.graphChanged({
|
|
1601
|
+
graph: snapshotGraph(draft.graph),
|
|
1602
|
+
event: createEventObject("updateEdgeData", event)
|
|
1603
|
+
});
|
|
1604
|
+
}
|
|
1605
|
+
}),
|
|
1606
|
+
deleteSelected: (ctx, _event, enqueue) => produce(ctx, (draft) => {
|
|
1607
|
+
const sel = draft.selection;
|
|
1608
|
+
const nodesToDelete = /* @__PURE__ */ new Set();
|
|
1609
|
+
const edgesToDelete = /* @__PURE__ */ new Set();
|
|
1610
|
+
for (const id of sel) {
|
|
1611
|
+
if (draft.graph.nodes.some((n) => n.id === id)) nodesToDelete.add(id);
|
|
1612
|
+
if (draft.graph.edges.some((e) => e.id === id)) edgesToDelete.add(id);
|
|
1613
|
+
}
|
|
1614
|
+
const selectedNodes = draft.graph.nodes.filter((node) => nodesToDelete.has(node.id));
|
|
1615
|
+
const selectedEdges = draft.graph.edges.filter((edge) => edgesToDelete.has(edge.id) || nodesToDelete.has(edge.sourceId) || nodesToDelete.has(edge.targetId));
|
|
1616
|
+
if (applyBeforeDelete(options, draft, "deleteSelected", selectedNodes, selectedEdges)) return;
|
|
1617
|
+
applyNodeDeletionStrategy(draft, nodesToDelete, edgesToDelete, options.onDeleteNodes ?? "cascade");
|
|
1618
|
+
draft.selection.clear();
|
|
1619
|
+
draft.focusedEntityId = null;
|
|
1620
|
+
for (const node of selectedNodes) draft.measurements.delete(node.id);
|
|
1621
|
+
for (const edge of selectedEdges) draft.measurements.delete(edge.id);
|
|
1622
|
+
pruneVisualState(draft, [...selectedNodes.map((node) => node.id), ...selectedEdges.map((edge) => edge.id)]);
|
|
1623
|
+
enqueue.emit.entitiesDeleted({
|
|
1624
|
+
ids: [...selectedNodes.map((node) => node.id), ...selectedEdges.map((edge) => edge.id)],
|
|
1625
|
+
event: createEventObject("deleteSelected", _event)
|
|
1626
|
+
});
|
|
1627
|
+
enqueue.emit.graphChanged({
|
|
1628
|
+
graph: snapshotGraph(draft.graph),
|
|
1629
|
+
event: createEventObject("deleteSelected", _event)
|
|
1630
|
+
});
|
|
1631
|
+
enqueue.emit.selectionChanged({
|
|
1632
|
+
selection: new Set(draft.selection),
|
|
1633
|
+
event: createEventObject("deleteSelected", _event)
|
|
1634
|
+
});
|
|
1635
|
+
}),
|
|
1636
|
+
setGraph: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1637
|
+
draft.graph = event.graph;
|
|
1638
|
+
draft.selection.clear();
|
|
1639
|
+
draft.hoveredEntityIds = /* @__PURE__ */ new Set();
|
|
1640
|
+
draft.highlights = /* @__PURE__ */ new Map();
|
|
1641
|
+
draft.focusedEntityId = null;
|
|
1642
|
+
draft.canvasState = "idle";
|
|
1643
|
+
draft.draggedEntityId = null;
|
|
1644
|
+
draft.connection = null;
|
|
1645
|
+
draft.selectionBox = null;
|
|
1646
|
+
draft.measurements.clear();
|
|
1647
|
+
enqueue.emit.graphChanged({
|
|
1648
|
+
graph: snapshotGraph(draft.graph),
|
|
1649
|
+
event: createEventObject("setGraph", event)
|
|
1650
|
+
});
|
|
1651
|
+
enqueue.emit.selectionChanged({
|
|
1652
|
+
selection: new Set(draft.selection),
|
|
1653
|
+
event: createEventObject("setGraph", event)
|
|
1654
|
+
});
|
|
1655
|
+
}),
|
|
1656
|
+
reset: (ctx, _event, enqueue) => produce(ctx, (draft) => {
|
|
1657
|
+
Object.assign(draft, {
|
|
1658
|
+
...initialContext,
|
|
1659
|
+
viewportSize: draft.viewportSize
|
|
1660
|
+
});
|
|
1661
|
+
enqueue.emit.graphChanged({
|
|
1662
|
+
graph: snapshotGraph(draft.graph),
|
|
1663
|
+
event: createEventObject("reset", _event)
|
|
1664
|
+
});
|
|
1665
|
+
enqueue.emit.selectionChanged({
|
|
1666
|
+
selection: new Set(draft.selection),
|
|
1667
|
+
event: createEventObject("reset", _event)
|
|
1668
|
+
});
|
|
1669
|
+
}),
|
|
1670
|
+
reparentNode: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1671
|
+
const node = draft.graph.nodes.find((n) => n.id === event.id);
|
|
1672
|
+
if (!node) return;
|
|
1673
|
+
const problem = reparentProblem(draft.graph, event.id, event.parentId);
|
|
1674
|
+
if (problem) {
|
|
1675
|
+
enqueue.emit.error({
|
|
1676
|
+
error: new Error(problem),
|
|
1677
|
+
event: createEventObject("reparentNode", event)
|
|
1678
|
+
});
|
|
1679
|
+
return;
|
|
1680
|
+
}
|
|
1681
|
+
node.parentId = event.parentId;
|
|
1682
|
+
enqueue.emit.graphChanged({
|
|
1683
|
+
graph: snapshotGraph(draft.graph),
|
|
1684
|
+
event: createEventObject("reparentNode", event)
|
|
1685
|
+
});
|
|
1686
|
+
}),
|
|
1687
|
+
dragStart: (ctx, event) => produce(ctx, (draft) => {
|
|
1688
|
+
if (!isExplicitlyDraggable(event.id, draft.graph)) return;
|
|
1689
|
+
draft.canvasState = "dragging";
|
|
1690
|
+
draft.draggedEntityId = event.id;
|
|
1691
|
+
draft.selection.add(event.id);
|
|
1692
|
+
}),
|
|
1693
|
+
dragMove: (ctx, event) => {
|
|
1694
|
+
if (ctx.canvasState !== "dragging") return ctx;
|
|
1695
|
+
const { dx, dy } = event;
|
|
1696
|
+
if (dx === 0 && dy === 0) return ctx;
|
|
1697
|
+
const plan = planNodeMovement(ctx, dx, dy, { includeLabels: true });
|
|
1698
|
+
if (!plan) return ctx;
|
|
1699
|
+
return produce(ctx, (draft) => {
|
|
1700
|
+
applyMovementPlan(draft, plan, dx, dy);
|
|
1701
|
+
});
|
|
1702
|
+
},
|
|
1703
|
+
dragEnd: (ctx) => produce(ctx, (draft) => {
|
|
1704
|
+
draft.canvasState = "idle";
|
|
1705
|
+
draft.draggedEntityId = null;
|
|
1706
|
+
}),
|
|
1707
|
+
commitDrag: (ctx, _event, enqueue) => produce(ctx, (draft) => {
|
|
1708
|
+
let snapOffX = 0;
|
|
1709
|
+
let snapOffY = 0;
|
|
1710
|
+
if (draft.enableSnapLines && draft.selection.size > 0) {
|
|
1711
|
+
const selBounds = getSelectionBounds(draft.graph.nodes, draft.graph.edges, draft.selection);
|
|
1712
|
+
if (selBounds) {
|
|
1713
|
+
const sel = draft.selection;
|
|
1714
|
+
const otherRects = draft.graph.nodes.filter((n) => !sel.has(n.id) && !n.hidden).map((n) => resolvedBounds(n));
|
|
1715
|
+
for (const edge of draft.graph.edges) {
|
|
1716
|
+
if (sel.has(edge.id) || !edge.label) continue;
|
|
1717
|
+
otherRects.push({
|
|
1718
|
+
x: edge.label.x + (edge.label.dx ?? 0),
|
|
1719
|
+
y: edge.label.y + (edge.label.dy ?? 0),
|
|
1720
|
+
width: edge.label.width,
|
|
1721
|
+
height: edge.label.height
|
|
1722
|
+
});
|
|
1723
|
+
}
|
|
1724
|
+
const snapResult = getSnapLines(selBounds, otherRects, {
|
|
1725
|
+
threshold: draft.snapThreshold / draft.viewport.zoom,
|
|
1726
|
+
enableSpacing: draft.enableSpacingGuides
|
|
1727
|
+
});
|
|
1728
|
+
if (snapResult) {
|
|
1729
|
+
snapOffX = snapResult.snapRect.x - selBounds.x;
|
|
1730
|
+
snapOffY = snapResult.snapRect.y - selBounds.y;
|
|
1731
|
+
}
|
|
1732
|
+
}
|
|
1733
|
+
}
|
|
1734
|
+
const gridSnap = draft.snapToGrid;
|
|
1735
|
+
const [gx, gy] = draft.snapGrid;
|
|
1736
|
+
for (const node of draft.graph.nodes) if (node.dx || node.dy) {
|
|
1737
|
+
let x = node.x + (node.dx ?? 0) + snapOffX;
|
|
1738
|
+
let y = node.y + (node.dy ?? 0) + snapOffY;
|
|
1739
|
+
if (gridSnap) {
|
|
1740
|
+
x = snapToGrid(x, gx);
|
|
1741
|
+
y = snapToGrid(y, gy);
|
|
1742
|
+
}
|
|
1743
|
+
node.x = x;
|
|
1744
|
+
node.y = y;
|
|
1745
|
+
node.dx = 0;
|
|
1746
|
+
node.dy = 0;
|
|
1747
|
+
}
|
|
1748
|
+
for (const edge of draft.graph.edges) {
|
|
1749
|
+
if (!edge.label) continue;
|
|
1750
|
+
if (edge.label.dx || edge.label.dy) {
|
|
1751
|
+
let x = edge.label.x + (edge.label.dx ?? 0) + snapOffX;
|
|
1752
|
+
let y = edge.label.y + (edge.label.dy ?? 0) + snapOffY;
|
|
1753
|
+
if (gridSnap) {
|
|
1754
|
+
x = snapToGrid(x, gx);
|
|
1755
|
+
y = snapToGrid(y, gy);
|
|
1756
|
+
}
|
|
1757
|
+
edge.label.x = x;
|
|
1758
|
+
edge.label.y = y;
|
|
1759
|
+
edge.label.dx = 0;
|
|
1760
|
+
edge.label.dy = 0;
|
|
1761
|
+
}
|
|
1762
|
+
}
|
|
1763
|
+
enqueue.emit.graphChanged({
|
|
1764
|
+
graph: snapshotGraph(draft.graph),
|
|
1765
|
+
event: createEventObject("commitDrag", _event)
|
|
1766
|
+
});
|
|
1767
|
+
}),
|
|
1768
|
+
select: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1769
|
+
const mode = event.mode ?? "single";
|
|
1770
|
+
if (mode === "single") draft.selection.clear();
|
|
1771
|
+
if (mode === "multi" && event.ids.every((id) => draft.selection.has(id))) for (const id of event.ids) draft.selection.delete(id);
|
|
1772
|
+
else for (const id of event.ids) draft.selection.add(id);
|
|
1773
|
+
enqueue.emit.selectionChanged({
|
|
1774
|
+
selection: new Set(draft.selection),
|
|
1775
|
+
event: createEventObject("select", event)
|
|
1776
|
+
});
|
|
1777
|
+
}),
|
|
1778
|
+
selectAll: (ctx, _event, enqueue) => produce(ctx, (draft) => {
|
|
1779
|
+
for (const node of draft.graph.nodes) {
|
|
1780
|
+
if (!isExplicitlyVisibleNode(node)) continue;
|
|
1781
|
+
draft.selection.add(node.id);
|
|
1782
|
+
}
|
|
1783
|
+
for (const edge of draft.graph.edges) {
|
|
1784
|
+
if (!isExplicitlyVisibleEdge(edge, draft.graph.nodes)) continue;
|
|
1785
|
+
draft.selection.add(edge.id);
|
|
1786
|
+
}
|
|
1787
|
+
enqueue.emit.selectionChanged({
|
|
1788
|
+
selection: new Set(draft.selection),
|
|
1789
|
+
event: createEventObject("selectAll", _event)
|
|
1790
|
+
});
|
|
1791
|
+
}),
|
|
1792
|
+
clearSelection: (ctx, _event, enqueue) => produce(ctx, (draft) => {
|
|
1793
|
+
draft.selection.clear();
|
|
1794
|
+
enqueue.emit.selectionChanged({
|
|
1795
|
+
selection: new Set(draft.selection),
|
|
1796
|
+
event: createEventObject("clearSelection", _event)
|
|
1797
|
+
});
|
|
1798
|
+
}),
|
|
1799
|
+
selectionBoxStart: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1800
|
+
draft.canvasState = "selecting";
|
|
1801
|
+
if ((event.mode ?? "single") === "single") draft.selection.clear();
|
|
1802
|
+
enqueue.emit.selectionChanged({
|
|
1803
|
+
selection: new Set(draft.selection),
|
|
1804
|
+
event: createEventObject("selectionBoxStart", event)
|
|
1805
|
+
});
|
|
1806
|
+
draft.selectionBox = [{
|
|
1807
|
+
x: event.point.x,
|
|
1808
|
+
y: event.point.y
|
|
1809
|
+
}, {
|
|
1810
|
+
x: event.point.x,
|
|
1811
|
+
y: event.point.y
|
|
1812
|
+
}];
|
|
1813
|
+
}),
|
|
1814
|
+
selectionBoxMove: (ctx, event) => produce(ctx, (draft) => {
|
|
1815
|
+
if (draft.canvasState !== "selecting" || !draft.selectionBox) return;
|
|
1816
|
+
draft.selectionBox[1] = {
|
|
1817
|
+
x: event.point.x,
|
|
1818
|
+
y: event.point.y
|
|
1819
|
+
};
|
|
1820
|
+
}),
|
|
1821
|
+
selectionBoxEnd: (ctx, _event, enqueue) => produce(ctx, (draft) => {
|
|
1822
|
+
if (!draft.selectionBox) return;
|
|
1823
|
+
const rect = boxToRect(draft.selectionBox[0], draft.selectionBox[1]);
|
|
1824
|
+
const contains = draft.selectionMode === "full" ? rectContains : rectsIntersect;
|
|
1825
|
+
for (const node of draft.graph.nodes) {
|
|
1826
|
+
if (!isExplicitlyVisibleNode(node)) continue;
|
|
1827
|
+
if (contains(rect, resolvedBounds(node))) draft.selection.add(node.id);
|
|
1828
|
+
}
|
|
1829
|
+
for (const edge of draft.graph.edges) {
|
|
1830
|
+
if (!isExplicitlyVisibleEdge(edge, draft.graph.nodes)) continue;
|
|
1831
|
+
if (edge.label) {
|
|
1832
|
+
if (contains(rect, {
|
|
1833
|
+
x: edge.label.x + (edge.label.dx ?? 0),
|
|
1834
|
+
y: edge.label.y + (edge.label.dy ?? 0),
|
|
1835
|
+
width: edge.label.width,
|
|
1836
|
+
height: edge.label.height
|
|
1837
|
+
})) {
|
|
1838
|
+
draft.selection.add(edge.id);
|
|
1839
|
+
continue;
|
|
1840
|
+
}
|
|
1841
|
+
}
|
|
1842
|
+
const edgePath = basicEdgePath(edge, draft.graph.nodes);
|
|
1843
|
+
const sourceNode = draft.graph.nodes.find((node) => node.id === edge.sourceId);
|
|
1844
|
+
const targetNode = draft.graph.nodes.find((node) => node.id === edge.targetId);
|
|
1845
|
+
const sourceSelectedByBox = sourceNode ? contains(rect, resolvedBounds(sourceNode)) : false;
|
|
1846
|
+
const targetSelectedByBox = targetNode ? contains(rect, resolvedBounds(targetNode)) : false;
|
|
1847
|
+
if (draft.selectionMode === "full" ? sourceSelectedByBox && targetSelectedByBox : !!edgePath && rectIntersectsPath(rect, edgePath) && !(sourceSelectedByBox && targetSelectedByBox)) draft.selection.add(edge.id);
|
|
1848
|
+
}
|
|
1849
|
+
draft.selectionBox = null;
|
|
1850
|
+
draft.canvasState = "idle";
|
|
1851
|
+
enqueue.emit.selectionChanged({
|
|
1852
|
+
selection: new Set(draft.selection),
|
|
1853
|
+
event: createEventObject("selectionBoxEnd", _event)
|
|
1854
|
+
});
|
|
1855
|
+
}),
|
|
1856
|
+
focus: (ctx, event) => produce(ctx, (draft) => {
|
|
1857
|
+
draft.focusedEntityId = event.entityId;
|
|
1858
|
+
}),
|
|
1859
|
+
clearFocus: (ctx) => produce(ctx, (draft) => {
|
|
1860
|
+
draft.focusedEntityId = null;
|
|
1861
|
+
}),
|
|
1862
|
+
setHover: (ctx, event) => {
|
|
1863
|
+
if (ctx.hoveredEntityIds.size === event.ids.length && event.ids.every((id) => ctx.hoveredEntityIds.has(id))) return ctx;
|
|
1864
|
+
return produce(ctx, (draft) => {
|
|
1865
|
+
draft.hoveredEntityIds = new Set(event.ids);
|
|
1866
|
+
});
|
|
1867
|
+
},
|
|
1868
|
+
clearHover: (ctx) => {
|
|
1869
|
+
if (ctx.hoveredEntityIds.size === 0) return ctx;
|
|
1870
|
+
return produce(ctx, (draft) => {
|
|
1871
|
+
draft.hoveredEntityIds = /* @__PURE__ */ new Set();
|
|
1872
|
+
});
|
|
1873
|
+
},
|
|
1874
|
+
setHighlights: (ctx, event) => produce(ctx, (draft) => {
|
|
1875
|
+
const bucket = /* @__PURE__ */ new Map();
|
|
1876
|
+
for (const entry of event.entries) bucket.set(entry.id, entry.meta);
|
|
1877
|
+
if (bucket.size === 0) draft.highlights.delete(event.kind);
|
|
1878
|
+
else draft.highlights.set(event.kind, bucket);
|
|
1879
|
+
}),
|
|
1880
|
+
addHighlights: (ctx, event) => produce(ctx, (draft) => {
|
|
1881
|
+
let bucket = draft.highlights.get(event.kind);
|
|
1882
|
+
if (!bucket) {
|
|
1883
|
+
bucket = /* @__PURE__ */ new Map();
|
|
1884
|
+
draft.highlights.set(event.kind, bucket);
|
|
1885
|
+
}
|
|
1886
|
+
for (const entry of event.entries) bucket.set(entry.id, entry.meta);
|
|
1887
|
+
}),
|
|
1888
|
+
removeHighlights: (ctx, event) => produce(ctx, (draft) => {
|
|
1889
|
+
const bucket = draft.highlights.get(event.kind);
|
|
1890
|
+
if (!bucket) return;
|
|
1891
|
+
for (const id of event.ids) bucket.delete(id);
|
|
1892
|
+
if (bucket.size === 0) draft.highlights.delete(event.kind);
|
|
1893
|
+
}),
|
|
1894
|
+
clearHighlights: (ctx, event) => produce(ctx, (draft) => {
|
|
1895
|
+
if (event?.kind !== void 0) draft.highlights.delete(event.kind);
|
|
1896
|
+
else draft.highlights.clear();
|
|
1897
|
+
}),
|
|
1898
|
+
updateParentNodeId: (ctx, event) => produce(ctx, (draft) => {
|
|
1899
|
+
draft.parentNodeId = event.parentNodeId;
|
|
1900
|
+
draft.selection.clear();
|
|
1901
|
+
draft.focusedEntityId = null;
|
|
1902
|
+
}),
|
|
1903
|
+
setViewport: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1904
|
+
draft.viewport = {
|
|
1905
|
+
x: event.x,
|
|
1906
|
+
y: event.y,
|
|
1907
|
+
zoom: event.zoom
|
|
1908
|
+
};
|
|
1909
|
+
enqueue.emit.viewportChanged({
|
|
1910
|
+
viewport: plainViewport(draft.viewport),
|
|
1911
|
+
event: createEventObject("setViewport", event)
|
|
1912
|
+
});
|
|
1913
|
+
}),
|
|
1914
|
+
panBy: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1915
|
+
draft.viewport.x += event.x;
|
|
1916
|
+
draft.viewport.y += event.y;
|
|
1917
|
+
if (draft.translateExtent) draft.viewport = constrainViewport(draft.viewport, draft.viewportSize, draft.translateExtent);
|
|
1918
|
+
enqueue.emit.viewportChanged({
|
|
1919
|
+
viewport: plainViewport(draft.viewport),
|
|
1920
|
+
event: createEventObject("panBy", event)
|
|
1921
|
+
});
|
|
1922
|
+
}),
|
|
1923
|
+
zoomTo: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1924
|
+
const clamped = Math.min(Math.max(event.zoom, draft.minZoom), draft.maxZoom);
|
|
1925
|
+
const ratio = clamped / draft.viewport.zoom;
|
|
1926
|
+
draft.viewport = {
|
|
1927
|
+
x: event.center.x - (event.center.x - draft.viewport.x) * ratio,
|
|
1928
|
+
y: event.center.y - (event.center.y - draft.viewport.y) * ratio,
|
|
1929
|
+
zoom: clamped
|
|
1930
|
+
};
|
|
1931
|
+
if (draft.translateExtent) draft.viewport = constrainViewport(draft.viewport, draft.viewportSize, draft.translateExtent);
|
|
1932
|
+
enqueue.emit.viewportChanged({
|
|
1933
|
+
viewport: plainViewport(draft.viewport),
|
|
1934
|
+
event: createEventObject("zoomTo", event)
|
|
1935
|
+
});
|
|
1936
|
+
}),
|
|
1937
|
+
zoomBy: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1938
|
+
const center = event.center ?? {
|
|
1939
|
+
x: draft.viewportSize.width / 2,
|
|
1940
|
+
y: draft.viewportSize.height / 2
|
|
1941
|
+
};
|
|
1942
|
+
const clamped = Math.min(Math.max(draft.viewport.zoom * event.factor, draft.minZoom), draft.maxZoom);
|
|
1943
|
+
const ratio = clamped / draft.viewport.zoom;
|
|
1944
|
+
draft.viewport = {
|
|
1945
|
+
x: center.x - (center.x - draft.viewport.x) * ratio,
|
|
1946
|
+
y: center.y - (center.y - draft.viewport.y) * ratio,
|
|
1947
|
+
zoom: clamped
|
|
1948
|
+
};
|
|
1949
|
+
if (draft.translateExtent) draft.viewport = constrainViewport(draft.viewport, draft.viewportSize, draft.translateExtent);
|
|
1950
|
+
enqueue.emit.viewportChanged({
|
|
1951
|
+
viewport: plainViewport(draft.viewport),
|
|
1952
|
+
event: createEventObject("zoomBy", event)
|
|
1953
|
+
});
|
|
1954
|
+
}),
|
|
1955
|
+
setMinZoom: (ctx, event) => produce(ctx, (draft) => {
|
|
1956
|
+
draft.minZoom = event.minZoom;
|
|
1957
|
+
if (draft.viewport.zoom < event.minZoom) draft.viewport.zoom = event.minZoom;
|
|
1958
|
+
}),
|
|
1959
|
+
setMaxZoom: (ctx, event) => produce(ctx, (draft) => {
|
|
1960
|
+
draft.maxZoom = event.maxZoom;
|
|
1961
|
+
if (draft.viewport.zoom > event.maxZoom) draft.viewport.zoom = event.maxZoom;
|
|
1962
|
+
}),
|
|
1963
|
+
setDragThreshold: (ctx, event) => produce(ctx, (draft) => {
|
|
1964
|
+
draft.dragThreshold = Math.max(0, event.dragThreshold);
|
|
1965
|
+
}),
|
|
1966
|
+
setAutoPanOptions: (ctx, event) => produce(ctx, (draft) => {
|
|
1967
|
+
if (event.autoPanOnDrag !== void 0) draft.autoPanOnDrag = event.autoPanOnDrag;
|
|
1968
|
+
if (event.autoPanThreshold !== void 0) draft.autoPanThreshold = Math.max(0, event.autoPanThreshold);
|
|
1969
|
+
if (event.autoPanSpeed !== void 0) draft.autoPanSpeed = Math.max(0, event.autoPanSpeed);
|
|
1970
|
+
}),
|
|
1971
|
+
setTranslateExtent: (ctx, event) => produce(ctx, (draft) => {
|
|
1972
|
+
draft.translateExtent = event.extent;
|
|
1973
|
+
}),
|
|
1974
|
+
setNodeExtent: (ctx, event) => produce(ctx, (draft) => {
|
|
1975
|
+
draft.nodeExtent = event.extent;
|
|
1976
|
+
}),
|
|
1977
|
+
updateViewportSize: (ctx, event) => produce(ctx, (draft) => {
|
|
1978
|
+
draft.viewportSize = {
|
|
1979
|
+
width: event.width,
|
|
1980
|
+
height: event.height
|
|
1981
|
+
};
|
|
1982
|
+
}),
|
|
1983
|
+
fitView: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1984
|
+
if (draft.viewportSize.width === 0 || draft.viewportSize.height === 0) return;
|
|
1985
|
+
const ids = event?.nodes?.map((node) => typeof node === "string" ? node : node.id);
|
|
1986
|
+
const hasIds = ids && ids.length > 0;
|
|
1987
|
+
const idSet = hasIds ? new Set(ids) : null;
|
|
1988
|
+
const bounds = getGraphBounds(hasIds ? draft.graph.nodes.filter((node) => idSet.has(node.id) && isExplicitlyVisibleNode(node)) : draft.graph.nodes.filter((node) => isExplicitlyVisibleNode(node)), hasIds ? draft.graph.edges.filter((edge) => idSet.has(edge.id) && isExplicitlyVisibleEdge(edge, draft.graph.nodes)) : draft.graph.edges.filter((edge) => isExplicitlyVisibleEdge(edge, draft.graph.nodes)));
|
|
1989
|
+
if (bounds.width === 0 && bounds.height === 0) return;
|
|
1990
|
+
const { nodes: _nodes, ...options } = event ?? {};
|
|
1991
|
+
draft.viewport = viewportForBounds(bounds, draft.viewportSize, options);
|
|
1992
|
+
enqueue.emit.viewportChanged({
|
|
1993
|
+
viewport: plainViewport(draft.viewport),
|
|
1994
|
+
event: createEventObject("fitView", event)
|
|
1995
|
+
});
|
|
1996
|
+
}),
|
|
1997
|
+
pushViewport: (ctx) => produce(ctx, (draft) => {
|
|
1998
|
+
draft.viewportStack.push({ ...draft.viewport });
|
|
1999
|
+
}),
|
|
2000
|
+
popViewport: (ctx, _event, enqueue) => produce(ctx, (draft) => {
|
|
2001
|
+
const prev = draft.viewportStack.pop();
|
|
2002
|
+
if (prev) {
|
|
2003
|
+
draft.viewport = prev;
|
|
2004
|
+
enqueue.emit.viewportChanged({
|
|
2005
|
+
viewport: plainViewport(draft.viewport),
|
|
2006
|
+
event: createEventObject("popViewport", _event)
|
|
2007
|
+
});
|
|
2008
|
+
}
|
|
2009
|
+
}),
|
|
2010
|
+
panStart: (ctx, event) => produce(ctx, (draft) => {
|
|
2011
|
+
draft.canvasState = "panning";
|
|
2012
|
+
draft.panOrigin = {
|
|
2013
|
+
x: event.x,
|
|
2014
|
+
y: event.y
|
|
2015
|
+
};
|
|
2016
|
+
draft.panViewportOrigin = { ...draft.viewport };
|
|
2017
|
+
}),
|
|
2018
|
+
pan: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
2019
|
+
if (!draft.panOrigin || !draft.panViewportOrigin) return;
|
|
2020
|
+
let newX = draft.panViewportOrigin.x + (event.x - draft.panOrigin.x);
|
|
2021
|
+
let newY = draft.panViewportOrigin.y + (event.y - draft.panOrigin.y);
|
|
2022
|
+
if (draft.translateExtent) {
|
|
2023
|
+
const z = draft.viewport.zoom;
|
|
2024
|
+
newX = Math.max(-(draft.translateExtent[1].x * z - draft.viewportSize.width), Math.min(newX, -draft.translateExtent[0].x * z));
|
|
2025
|
+
newY = Math.max(-(draft.translateExtent[1].y * z - draft.viewportSize.height), Math.min(newY, -draft.translateExtent[0].y * z));
|
|
2026
|
+
}
|
|
2027
|
+
draft.viewport = {
|
|
2028
|
+
x: newX,
|
|
2029
|
+
y: newY,
|
|
2030
|
+
zoom: draft.panViewportOrigin.zoom
|
|
2031
|
+
};
|
|
2032
|
+
enqueue.emit.viewportChanged({
|
|
2033
|
+
viewport: plainViewport(draft.viewport),
|
|
2034
|
+
event: createEventObject("pan", event)
|
|
2035
|
+
});
|
|
2036
|
+
}),
|
|
2037
|
+
panEnd: (ctx) => produce(ctx, (draft) => {
|
|
2038
|
+
draft.canvasState = "idle";
|
|
2039
|
+
draft.panOrigin = null;
|
|
2040
|
+
draft.panViewportOrigin = null;
|
|
2041
|
+
}),
|
|
2042
|
+
setCanvasState: (ctx, event) => produce(ctx, (draft) => {
|
|
2043
|
+
draft.canvasState = event.state;
|
|
2044
|
+
}),
|
|
2045
|
+
connectionStart: (ctx, event) => produce(ctx, (draft) => {
|
|
2046
|
+
draft.canvasState = "connecting";
|
|
2047
|
+
draft.connection = {
|
|
2048
|
+
sourceId: event.sourceId,
|
|
2049
|
+
sourcePort: event.sourcePort,
|
|
2050
|
+
targetId: null,
|
|
2051
|
+
targetPort: null,
|
|
2052
|
+
startPosition: event.position,
|
|
2053
|
+
pointerPosition: event.position,
|
|
2054
|
+
active: false,
|
|
2055
|
+
isValid: false
|
|
2056
|
+
};
|
|
2057
|
+
}),
|
|
2058
|
+
connectionMove: (ctx, event) => produce(ctx, (draft) => {
|
|
2059
|
+
if (!draft.connection) return;
|
|
2060
|
+
draft.connection.pointerPosition = event.position;
|
|
2061
|
+
if (event.targetId !== void 0) draft.connection.targetId = event.targetId;
|
|
2062
|
+
if (event.targetPort !== void 0) draft.connection.targetPort = event.targetPort;
|
|
2063
|
+
if (event.isValid !== void 0) draft.connection.isValid = event.isValid;
|
|
2064
|
+
const dx = event.position.x - draft.connection.startPosition.x;
|
|
2065
|
+
const dy = event.position.y - draft.connection.startPosition.y;
|
|
2066
|
+
const threshold = options.connectionDragThreshold ?? draft.dragThreshold;
|
|
2067
|
+
if (!draft.connection.active && Math.hypot(dx, dy) >= threshold) draft.connection.active = true;
|
|
2068
|
+
}),
|
|
2069
|
+
connectionEnd: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
2070
|
+
const connection = ctx.connection;
|
|
2071
|
+
if (connection) enqueue.emit.connectionEnded({
|
|
2072
|
+
source: {
|
|
2073
|
+
nodeId: connection.sourceId,
|
|
2074
|
+
port: connection.sourcePort
|
|
2075
|
+
},
|
|
2076
|
+
target: event?.target ?? { kind: "none" },
|
|
2077
|
+
connection
|
|
2078
|
+
});
|
|
2079
|
+
draft.canvasState = "idle";
|
|
2080
|
+
draft.connection = null;
|
|
2081
|
+
}),
|
|
2082
|
+
cancelConnection: (ctx) => produce(ctx, (draft) => {
|
|
2083
|
+
draft.connection = null;
|
|
2084
|
+
draft.connectionClickStartHandle = null;
|
|
2085
|
+
if (draft.canvasState === "connecting") draft.canvasState = "idle";
|
|
2086
|
+
}),
|
|
2087
|
+
setConnectionClickStartHandle: (ctx, event) => produce(ctx, (draft) => {
|
|
2088
|
+
draft.connectionClickStartHandle = event;
|
|
2089
|
+
}),
|
|
2090
|
+
resizeStart: (ctx, event) => produce(ctx, (draft) => {
|
|
2091
|
+
const node = draft.graph.nodes.find((n) => n.id === event.id);
|
|
2092
|
+
if (!node) return;
|
|
2093
|
+
draft.canvasState = "resizing";
|
|
2094
|
+
draft.resizingEntityId = event.id;
|
|
2095
|
+
draft.resizeHandle = event.handle;
|
|
2096
|
+
const x = node.x + (node.dx ?? 0);
|
|
2097
|
+
const y = node.y + (node.dy ?? 0);
|
|
2098
|
+
const { width: w, height: h } = resolvedSize(node);
|
|
2099
|
+
const tl = {
|
|
2100
|
+
x,
|
|
2101
|
+
y
|
|
2102
|
+
};
|
|
2103
|
+
const br = {
|
|
2104
|
+
x: x + w,
|
|
2105
|
+
y: y + h
|
|
2106
|
+
};
|
|
2107
|
+
const handle = event.handle;
|
|
2108
|
+
if (handle === "bottom-right") draft.resizeCorners = [tl, br];
|
|
2109
|
+
else if (handle === "bottom-left") draft.resizeCorners = [{
|
|
2110
|
+
x: br.x,
|
|
2111
|
+
y: tl.y
|
|
2112
|
+
}, {
|
|
2113
|
+
x: tl.x,
|
|
2114
|
+
y: br.y
|
|
2115
|
+
}];
|
|
2116
|
+
else if (handle === "top-right") draft.resizeCorners = [{
|
|
2117
|
+
x: tl.x,
|
|
2118
|
+
y: br.y
|
|
2119
|
+
}, {
|
|
2120
|
+
x: br.x,
|
|
2121
|
+
y: tl.y
|
|
2122
|
+
}];
|
|
2123
|
+
else if (handle === "top-left") draft.resizeCorners = [br, tl];
|
|
2124
|
+
else if (handle === "right") draft.resizeCorners = [tl, {
|
|
2125
|
+
x: br.x,
|
|
2126
|
+
y: br.y
|
|
2127
|
+
}];
|
|
2128
|
+
else if (handle === "left") draft.resizeCorners = [{
|
|
2129
|
+
x: br.x,
|
|
2130
|
+
y: tl.y
|
|
2131
|
+
}, {
|
|
2132
|
+
x: tl.x,
|
|
2133
|
+
y: br.y
|
|
2134
|
+
}];
|
|
2135
|
+
else if (handle === "bottom") draft.resizeCorners = [tl, {
|
|
2136
|
+
x: br.x,
|
|
2137
|
+
y: br.y
|
|
2138
|
+
}];
|
|
2139
|
+
else if (handle === "top") draft.resizeCorners = [{
|
|
2140
|
+
x: tl.x,
|
|
2141
|
+
y: br.y
|
|
2142
|
+
}, {
|
|
2143
|
+
x: br.x,
|
|
2144
|
+
y: tl.y
|
|
2145
|
+
}];
|
|
2146
|
+
}),
|
|
2147
|
+
resizeMove: (ctx, event) => produce(ctx, (draft) => {
|
|
2148
|
+
if (!draft.resizeCorners || !draft.resizingEntityId) return;
|
|
2149
|
+
const handle = draft.resizeHandle;
|
|
2150
|
+
if (handle === "left" || handle === "right") draft.resizeCorners[1].x = event.x;
|
|
2151
|
+
else if (handle === "top" || handle === "bottom") draft.resizeCorners[1].y = event.y;
|
|
2152
|
+
else draft.resizeCorners[1] = {
|
|
2153
|
+
x: event.x,
|
|
2154
|
+
y: event.y
|
|
2155
|
+
};
|
|
2156
|
+
}),
|
|
2157
|
+
resizeEnd: (ctx, _event, enqueue) => produce(ctx, (draft) => {
|
|
2158
|
+
if (draft.resizingEntityId && draft.resizeCorners) {
|
|
2159
|
+
const node = draft.graph.nodes.find((n) => n.id === draft.resizingEntityId);
|
|
2160
|
+
if (node) {
|
|
2161
|
+
const [a, b] = draft.resizeCorners;
|
|
2162
|
+
const x = Math.min(a.x, b.x);
|
|
2163
|
+
const y = Math.min(a.y, b.y);
|
|
2164
|
+
const w = Math.abs(b.x - a.x);
|
|
2165
|
+
const h = Math.abs(b.y - a.y);
|
|
2166
|
+
node.x = x;
|
|
2167
|
+
node.y = y;
|
|
2168
|
+
node.dx = 0;
|
|
2169
|
+
node.dy = 0;
|
|
2170
|
+
if (!node.style) node.style = {};
|
|
2171
|
+
node.style.customWidth = Math.max(10, w);
|
|
2172
|
+
node.style.customHeight = Math.max(10, h);
|
|
2173
|
+
}
|
|
2174
|
+
}
|
|
2175
|
+
draft.canvasState = "idle";
|
|
2176
|
+
draft.resizingEntityId = null;
|
|
2177
|
+
draft.resizeHandle = null;
|
|
2178
|
+
draft.resizeCorners = null;
|
|
2179
|
+
enqueue.emit.graphChanged({
|
|
2180
|
+
graph: snapshotGraph(draft.graph),
|
|
2181
|
+
event: createEventObject("resizeEnd", _event)
|
|
2182
|
+
});
|
|
2183
|
+
}),
|
|
2184
|
+
resizeCancel: (ctx) => produce(ctx, (draft) => {
|
|
2185
|
+
draft.canvasState = "idle";
|
|
2186
|
+
draft.resizingEntityId = null;
|
|
2187
|
+
draft.resizeHandle = null;
|
|
2188
|
+
draft.resizeCorners = null;
|
|
2189
|
+
}),
|
|
2190
|
+
reconnectEdge: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
2191
|
+
const edge = draft.graph.edges.find((e) => e.id === event.edgeId);
|
|
2192
|
+
if (!edge) return;
|
|
2193
|
+
const resultingSourceId = event.newSourceId ?? edge.sourceId;
|
|
2194
|
+
const resultingTargetId = event.newTargetId ?? edge.targetId;
|
|
2195
|
+
const missing = [];
|
|
2196
|
+
if (!draft.graph.nodes.some((n) => n.id === resultingSourceId)) missing.push(`source node "${resultingSourceId}"`);
|
|
2197
|
+
if (!draft.graph.nodes.some((n) => n.id === resultingTargetId)) missing.push(`target node "${resultingTargetId}"`);
|
|
2198
|
+
if (missing.length > 0) {
|
|
2199
|
+
enqueue.emit.error({
|
|
2200
|
+
error: /* @__PURE__ */ new Error(`reconnectEdge rejected: edge "${event.edgeId}" references ${missing.join(" and ")}, which ${missing.length === 1 ? "does" : "do"} not exist in the graph.`),
|
|
2201
|
+
event: createEventObject("reconnectEdge", event)
|
|
2202
|
+
});
|
|
2203
|
+
return;
|
|
2204
|
+
}
|
|
2205
|
+
if (event.newSourceId !== void 0) edge.sourceId = event.newSourceId;
|
|
2206
|
+
if (event.newTargetId !== void 0) edge.targetId = event.newTargetId;
|
|
2207
|
+
if (event.newSourcePort !== void 0) edge.sourcePort = event.newSourcePort;
|
|
2208
|
+
if (event.newTargetPort !== void 0) edge.targetPort = event.newTargetPort;
|
|
2209
|
+
enqueue.emit.graphChanged({
|
|
2210
|
+
graph: snapshotGraph(draft.graph),
|
|
2211
|
+
event: createEventObject("reconnectEdge", event)
|
|
2212
|
+
});
|
|
2213
|
+
}),
|
|
2214
|
+
setMode: (ctx, event, enqueue) => dispatchProfile(profile, ctx, createEventObject("setMode", event), enqueue, setModeTransition(profile)),
|
|
2215
|
+
setSnapToGrid: (ctx, event) => produce(ctx, (draft) => {
|
|
2216
|
+
draft.snapToGrid = event.enabled;
|
|
2217
|
+
if (event.grid) draft.snapGrid = event.grid;
|
|
2218
|
+
}),
|
|
2219
|
+
setSnapLines: (ctx, event) => produce(ctx, (draft) => {
|
|
2220
|
+
draft.enableSnapLines = event.enabled;
|
|
2221
|
+
if (event.threshold !== void 0) draft.snapThreshold = event.threshold;
|
|
2222
|
+
if (event.spacing !== void 0) draft.enableSpacingGuides = event.spacing;
|
|
2223
|
+
}),
|
|
2224
|
+
setColorMode: (ctx, event) => produce(ctx, (draft) => {
|
|
2225
|
+
draft.colorMode = event.colorMode;
|
|
2226
|
+
}),
|
|
2227
|
+
moveNodes: (ctx, event) => {
|
|
2228
|
+
const f = event.factor ?? 1;
|
|
2229
|
+
let dx = event.direction.x * f;
|
|
2230
|
+
let dy = event.direction.y * f;
|
|
2231
|
+
if (ctx.snapToGrid) {
|
|
2232
|
+
dx = dx !== 0 ? Math.sign(dx) * ctx.snapGrid[0] : 0;
|
|
2233
|
+
dy = dy !== 0 ? Math.sign(dy) * ctx.snapGrid[1] : 0;
|
|
2234
|
+
}
|
|
2235
|
+
if (dx === 0 && dy === 0) return ctx;
|
|
2236
|
+
const plan = planNodeMovement(ctx, dx, dy, { includeLabels: true });
|
|
2237
|
+
if (!plan) return ctx;
|
|
2238
|
+
return produce(ctx, (draft) => {
|
|
2239
|
+
applyMovementPlan(draft, plan, dx, dy);
|
|
2240
|
+
});
|
|
2241
|
+
},
|
|
2242
|
+
requestLayout: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
2243
|
+
if (draft.canvasState === "dragging") return;
|
|
2244
|
+
const algorithm = event.algorithm ?? draft.autoLayout.algorithm;
|
|
2245
|
+
if (!algorithm) return;
|
|
2246
|
+
draft.autoLayout = {
|
|
2247
|
+
status: "pending",
|
|
2248
|
+
algorithm
|
|
2249
|
+
};
|
|
2250
|
+
enqueue.emit.layoutRequested({
|
|
2251
|
+
algorithm,
|
|
2252
|
+
graph: ctx.graph
|
|
2253
|
+
});
|
|
2254
|
+
}),
|
|
2255
|
+
applyLayoutResult: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
2256
|
+
for (const [id, pos] of event.positions) {
|
|
2257
|
+
const node = draft.graph.nodes.find((n) => n.id === id);
|
|
2258
|
+
if (node) {
|
|
2259
|
+
node.x = pos.x;
|
|
2260
|
+
node.y = pos.y;
|
|
2261
|
+
node.dx = 0;
|
|
2262
|
+
node.dy = 0;
|
|
2263
|
+
}
|
|
2264
|
+
}
|
|
2265
|
+
if (event.edgeLabelPositions) for (const [id, pos] of event.edgeLabelPositions) {
|
|
2266
|
+
const edge = draft.graph.edges.find((e) => e.id === id);
|
|
2267
|
+
if (edge?.label) {
|
|
2268
|
+
edge.label.x = pos.x;
|
|
2269
|
+
edge.label.y = pos.y;
|
|
2270
|
+
edge.label.dx = 0;
|
|
2271
|
+
edge.label.dy = 0;
|
|
2272
|
+
}
|
|
2273
|
+
}
|
|
2274
|
+
draft.autoLayout.status = "idle";
|
|
2275
|
+
enqueue.emit.graphChanged({
|
|
2276
|
+
graph: snapshotGraph(draft.graph),
|
|
2277
|
+
event: createEventObject("applyLayoutResult", event)
|
|
2278
|
+
});
|
|
2279
|
+
}),
|
|
2280
|
+
updateMeasurements: (ctx, event) => produce(ctx, (draft) => {
|
|
2281
|
+
for (const measurement of event.measurements) {
|
|
2282
|
+
draft.measurements.set(measurement.id, measurement.rect);
|
|
2283
|
+
const node = draft.graph.nodes.find((n) => n.id === measurement.id);
|
|
2284
|
+
if (node) {
|
|
2285
|
+
if (node.autoSize) node.measured = {
|
|
2286
|
+
width: measurement.rect.width,
|
|
2287
|
+
height: measurement.rect.height
|
|
2288
|
+
};
|
|
2289
|
+
continue;
|
|
2290
|
+
}
|
|
2291
|
+
const edge = draft.graph.edges.find((e) => e.id === measurement.id);
|
|
2292
|
+
if (edge?.label) {
|
|
2293
|
+
edge.label.width = measurement.rect.width;
|
|
2294
|
+
edge.label.height = measurement.rect.height;
|
|
2295
|
+
continue;
|
|
2296
|
+
}
|
|
2297
|
+
for (const candidate of draft.graph.nodes) {
|
|
2298
|
+
const port = candidate.ports?.find((p) => p.id === measurement.id || `${candidate.id}:${p.name}` === measurement.id);
|
|
2299
|
+
if (port) {
|
|
2300
|
+
port.x = measurement.rect.x - (candidate.x + (candidate.dx ?? 0));
|
|
2301
|
+
port.y = measurement.rect.y - (candidate.y + (candidate.dy ?? 0));
|
|
2302
|
+
port.width = measurement.rect.width;
|
|
2303
|
+
port.height = measurement.rect.height;
|
|
2304
|
+
break;
|
|
2305
|
+
}
|
|
2306
|
+
}
|
|
2307
|
+
}
|
|
2308
|
+
}),
|
|
2309
|
+
removeMeasurements: (ctx, event) => produce(ctx, (draft) => {
|
|
2310
|
+
for (const id of event.ids) draft.measurements.delete(id);
|
|
2311
|
+
}),
|
|
2312
|
+
bakeLabelPositions: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
2313
|
+
for (const [edgeId, center] of event.positions) {
|
|
2314
|
+
const edge = draft.graph.edges.find((e) => e.id === edgeId);
|
|
2315
|
+
if (edge?.label) {
|
|
2316
|
+
edge.label.x = center.x - edge.label.width / 2;
|
|
2317
|
+
edge.label.y = center.y - edge.label.height / 2;
|
|
2318
|
+
edge.label.dx = 0;
|
|
2319
|
+
edge.label.dy = 0;
|
|
2320
|
+
}
|
|
2321
|
+
}
|
|
2322
|
+
enqueue.emit.graphChanged({
|
|
2323
|
+
graph: snapshotGraph(draft.graph),
|
|
2324
|
+
event: createEventObject("bakeLabelPositions", event)
|
|
2325
|
+
});
|
|
2326
|
+
})
|
|
2327
|
+
}
|
|
2328
|
+
}).with(undoRedo({
|
|
2329
|
+
strategy: "snapshot",
|
|
2330
|
+
historyLimit: options.historyLimit ?? 100,
|
|
2331
|
+
getTransactionId: (event) => event.transactionId,
|
|
2332
|
+
skipEvent: (event, snapshot) => !shouldTrackUndoEvent(event, snapshot)
|
|
2333
|
+
}));
|
|
2334
|
+
}
|
|
2335
|
+
//#endregion
|
|
2336
|
+
//#region src/shapes/index.ts
|
|
2337
|
+
const shapeRegistry = /* @__PURE__ */ new Map();
|
|
2338
|
+
function registerShape(name, handler) {
|
|
2339
|
+
shapeRegistry.set(name, handler);
|
|
2340
|
+
}
|
|
2341
|
+
function getShape(name) {
|
|
2342
|
+
return shapeRegistry.get(name);
|
|
2343
|
+
}
|
|
2344
|
+
function getShapeOrDefault(name) {
|
|
2345
|
+
return shapeRegistry.get(name) ?? rectHandler;
|
|
2346
|
+
}
|
|
2347
|
+
function rectHitTest(point, bounds) {
|
|
2348
|
+
return point.x >= bounds.x && point.x <= bounds.x + bounds.width && point.y >= bounds.y && point.y <= bounds.y + bounds.height;
|
|
2349
|
+
}
|
|
2350
|
+
function rectEdgeAnchor(bounds, direction) {
|
|
2351
|
+
switch (direction) {
|
|
2352
|
+
case "top": return {
|
|
2353
|
+
x: bounds.x + bounds.width / 2,
|
|
2354
|
+
y: bounds.y
|
|
2355
|
+
};
|
|
2356
|
+
case "bottom": return {
|
|
2357
|
+
x: bounds.x + bounds.width / 2,
|
|
2358
|
+
y: bounds.y + bounds.height
|
|
2359
|
+
};
|
|
2360
|
+
case "left": return {
|
|
2361
|
+
x: bounds.x,
|
|
2362
|
+
y: bounds.y + bounds.height / 2
|
|
2363
|
+
};
|
|
2364
|
+
case "right": return {
|
|
2365
|
+
x: bounds.x + bounds.width,
|
|
2366
|
+
y: bounds.y + bounds.height / 2
|
|
2367
|
+
};
|
|
2368
|
+
}
|
|
2369
|
+
}
|
|
2370
|
+
const rectHandler = {
|
|
2371
|
+
hitTest: rectHitTest,
|
|
2372
|
+
edgeAnchor: rectEdgeAnchor
|
|
2373
|
+
};
|
|
2374
|
+
const roundedHandler = {
|
|
2375
|
+
hitTest: rectHitTest,
|
|
2376
|
+
edgeAnchor: rectEdgeAnchor
|
|
2377
|
+
};
|
|
2378
|
+
function ellipseHitTest(point, bounds) {
|
|
2379
|
+
const cx = bounds.x + bounds.width / 2;
|
|
2380
|
+
const cy = bounds.y + bounds.height / 2;
|
|
2381
|
+
const rx = bounds.width / 2;
|
|
2382
|
+
const ry = bounds.height / 2;
|
|
2383
|
+
if (rx === 0 || ry === 0) return false;
|
|
2384
|
+
const dx = point.x - cx;
|
|
2385
|
+
const dy = point.y - cy;
|
|
2386
|
+
return dx * dx / (rx * rx) + dy * dy / (ry * ry) <= 1;
|
|
2387
|
+
}
|
|
2388
|
+
function ellipseEdgeAnchor(bounds, direction) {
|
|
2389
|
+
const cx = bounds.x + bounds.width / 2;
|
|
2390
|
+
const cy = bounds.y + bounds.height / 2;
|
|
2391
|
+
const rx = bounds.width / 2;
|
|
2392
|
+
const ry = bounds.height / 2;
|
|
2393
|
+
switch (direction) {
|
|
2394
|
+
case "top": return {
|
|
2395
|
+
x: cx,
|
|
2396
|
+
y: cy - ry
|
|
2397
|
+
};
|
|
2398
|
+
case "bottom": return {
|
|
2399
|
+
x: cx,
|
|
2400
|
+
y: cy + ry
|
|
2401
|
+
};
|
|
2402
|
+
case "left": return {
|
|
2403
|
+
x: cx - rx,
|
|
2404
|
+
y: cy
|
|
2405
|
+
};
|
|
2406
|
+
case "right": return {
|
|
2407
|
+
x: cx + rx,
|
|
2408
|
+
y: cy
|
|
2409
|
+
};
|
|
2410
|
+
}
|
|
2411
|
+
}
|
|
2412
|
+
const ellipseHandler = {
|
|
2413
|
+
hitTest: ellipseHitTest,
|
|
2414
|
+
edgeAnchor: ellipseEdgeAnchor
|
|
2415
|
+
};
|
|
2416
|
+
function diamondHitTest(point, bounds) {
|
|
2417
|
+
const cx = bounds.x + bounds.width / 2;
|
|
2418
|
+
const cy = bounds.y + bounds.height / 2;
|
|
2419
|
+
const hw = bounds.width / 2;
|
|
2420
|
+
const hh = bounds.height / 2;
|
|
2421
|
+
if (hw === 0 || hh === 0) return false;
|
|
2422
|
+
return Math.abs(point.x - cx) / hw + Math.abs(point.y - cy) / hh <= 1;
|
|
2423
|
+
}
|
|
2424
|
+
function diamondEdgeAnchor(bounds, direction) {
|
|
2425
|
+
const cx = bounds.x + bounds.width / 2;
|
|
2426
|
+
const cy = bounds.y + bounds.height / 2;
|
|
2427
|
+
switch (direction) {
|
|
2428
|
+
case "top": return {
|
|
2429
|
+
x: cx,
|
|
2430
|
+
y: bounds.y
|
|
2431
|
+
};
|
|
2432
|
+
case "bottom": return {
|
|
2433
|
+
x: cx,
|
|
2434
|
+
y: bounds.y + bounds.height
|
|
2435
|
+
};
|
|
2436
|
+
case "left": return {
|
|
2437
|
+
x: bounds.x,
|
|
2438
|
+
y: cy
|
|
2439
|
+
};
|
|
2440
|
+
case "right": return {
|
|
2441
|
+
x: bounds.x + bounds.width,
|
|
2442
|
+
y: cy
|
|
2443
|
+
};
|
|
2444
|
+
}
|
|
2445
|
+
}
|
|
2446
|
+
const diamondHandler = {
|
|
2447
|
+
hitTest: diamondHitTest,
|
|
2448
|
+
edgeAnchor: diamondEdgeAnchor
|
|
2449
|
+
};
|
|
2450
|
+
registerShape("rect", rectHandler);
|
|
2451
|
+
registerShape("rounded", roundedHandler);
|
|
2452
|
+
registerShape("ellipse", ellipseHandler);
|
|
2453
|
+
registerShape("diamond", diamondHandler);
|
|
2454
|
+
//#endregion
|
|
2455
|
+
//#region src/routing/index.ts
|
|
2456
|
+
const straightRouter = (_edge, ctx) => {
|
|
2457
|
+
return {
|
|
2458
|
+
points: [ctx.sourcePoint ?? centerOf(ctx.sourceBounds), ctx.targetPoint ?? centerOf(ctx.targetBounds)],
|
|
2459
|
+
sourceDirection: ctx.sourceDirection,
|
|
2460
|
+
targetDirection: ctx.targetDirection
|
|
2461
|
+
};
|
|
2462
|
+
};
|
|
2463
|
+
const stepRouter = (_edge, ctx) => {
|
|
2464
|
+
const source = ctx.sourcePoint ?? edgeMidpoint(ctx.sourceBounds, ctx.sourceDirection);
|
|
2465
|
+
const target = ctx.targetPoint ?? edgeMidpoint(ctx.targetBounds, ctx.targetDirection);
|
|
2466
|
+
return {
|
|
2467
|
+
points: [
|
|
2468
|
+
source,
|
|
2469
|
+
computeStepMidpoint(source, target, ctx.sourceDirection),
|
|
2470
|
+
target
|
|
2471
|
+
],
|
|
2472
|
+
sourceDirection: ctx.sourceDirection,
|
|
2473
|
+
targetDirection: ctx.targetDirection
|
|
2474
|
+
};
|
|
2475
|
+
};
|
|
2476
|
+
const bezierRouter = (_edge, ctx) => {
|
|
2477
|
+
const source = ctx.sourcePoint ?? edgeMidpoint(ctx.sourceBounds, ctx.sourceDirection);
|
|
2478
|
+
const target = ctx.targetPoint ?? edgeMidpoint(ctx.targetBounds, ctx.targetDirection);
|
|
2479
|
+
const offset = Math.max(50, Math.abs(target.x - source.x) * .25, Math.abs(target.y - source.y) * .25);
|
|
2480
|
+
return {
|
|
2481
|
+
points: [
|
|
2482
|
+
source,
|
|
2483
|
+
offsetPoint(source, ctx.sourceDirection, offset),
|
|
2484
|
+
offsetPoint(target, ctx.targetDirection, offset),
|
|
2485
|
+
target
|
|
2486
|
+
],
|
|
2487
|
+
sourceDirection: ctx.sourceDirection,
|
|
2488
|
+
targetDirection: ctx.targetDirection
|
|
2489
|
+
};
|
|
2490
|
+
};
|
|
2491
|
+
function centerOf(rect) {
|
|
2492
|
+
return {
|
|
2493
|
+
x: rect.x + rect.width / 2,
|
|
2494
|
+
y: rect.y + rect.height / 2
|
|
2495
|
+
};
|
|
2496
|
+
}
|
|
2497
|
+
function edgeMidpoint(rect, direction) {
|
|
2498
|
+
switch (direction) {
|
|
2499
|
+
case "top": return {
|
|
2500
|
+
x: rect.x + rect.width / 2,
|
|
2501
|
+
y: rect.y
|
|
2502
|
+
};
|
|
2503
|
+
case "bottom": return {
|
|
2504
|
+
x: rect.x + rect.width / 2,
|
|
2505
|
+
y: rect.y + rect.height
|
|
2506
|
+
};
|
|
2507
|
+
case "left": return {
|
|
2508
|
+
x: rect.x,
|
|
2509
|
+
y: rect.y + rect.height / 2
|
|
2510
|
+
};
|
|
2511
|
+
case "right": return {
|
|
2512
|
+
x: rect.x + rect.width,
|
|
2513
|
+
y: rect.y + rect.height / 2
|
|
2514
|
+
};
|
|
2515
|
+
}
|
|
2516
|
+
}
|
|
2517
|
+
function offsetPoint(point, direction, offset) {
|
|
2518
|
+
switch (direction) {
|
|
2519
|
+
case "top": return {
|
|
2520
|
+
x: point.x,
|
|
2521
|
+
y: point.y - offset
|
|
2522
|
+
};
|
|
2523
|
+
case "bottom": return {
|
|
2524
|
+
x: point.x,
|
|
2525
|
+
y: point.y + offset
|
|
2526
|
+
};
|
|
2527
|
+
case "left": return {
|
|
2528
|
+
x: point.x - offset,
|
|
2529
|
+
y: point.y
|
|
2530
|
+
};
|
|
2531
|
+
case "right": return {
|
|
2532
|
+
x: point.x + offset,
|
|
2533
|
+
y: point.y
|
|
2534
|
+
};
|
|
2535
|
+
}
|
|
2536
|
+
}
|
|
2537
|
+
function computeStepMidpoint(source, target, sourceDir) {
|
|
2538
|
+
if (sourceDir === "left" || sourceDir === "right") return {
|
|
2539
|
+
x: (source.x + target.x) / 2,
|
|
2540
|
+
y: source.y
|
|
2541
|
+
};
|
|
2542
|
+
return {
|
|
2543
|
+
x: source.x,
|
|
2544
|
+
y: (source.y + target.y) / 2
|
|
2545
|
+
};
|
|
2546
|
+
}
|
|
2547
|
+
/**
|
|
2548
|
+
* Determine the best side of `from` to connect toward `to`,
|
|
2549
|
+
* based on relative center positions.
|
|
2550
|
+
*/
|
|
2551
|
+
function bestDirection(from, to) {
|
|
2552
|
+
const fromCx = from.x + from.width / 2;
|
|
2553
|
+
const fromCy = from.y + from.height / 2;
|
|
2554
|
+
const toCx = to.x + to.width / 2;
|
|
2555
|
+
const toCy = to.y + to.height / 2;
|
|
2556
|
+
const dx = toCx - fromCx;
|
|
2557
|
+
const dy = toCy - fromCy;
|
|
2558
|
+
if (Math.abs(dx) > Math.abs(dy)) return dx > 0 ? "right" : "left";
|
|
2559
|
+
return dy > 0 ? "bottom" : "top";
|
|
2560
|
+
}
|
|
2561
|
+
/**
|
|
2562
|
+
* Convert PathData points to an SVG path `d` attribute.
|
|
2563
|
+
* Handles 2 points (line), 3 points (quadratic), 4 points (cubic bezier).
|
|
2564
|
+
*/
|
|
2565
|
+
function pathToSVG(path) {
|
|
2566
|
+
const pts = path.points;
|
|
2567
|
+
if (pts.length < 2) return "";
|
|
2568
|
+
const [p0, p1, p2, p3] = pts;
|
|
2569
|
+
if (!p0) return "";
|
|
2570
|
+
const start = `M ${p0.x} ${p0.y}`;
|
|
2571
|
+
if (pts.length === 2 && p1) return `${start} L ${p1.x} ${p1.y}`;
|
|
2572
|
+
if (pts.length === 3 && p1 && p2) return `${start} L ${p1.x} ${p1.y} L ${p2.x} ${p2.y}`;
|
|
2573
|
+
if (pts.length === 4 && p1 && p2 && p3) return `${start} C ${p1.x} ${p1.y}, ${p2.x} ${p2.y}, ${p3.x} ${p3.y}`;
|
|
2574
|
+
return start + pts.slice(1).map((p) => ` L ${p.x} ${p.y}`).join("");
|
|
2575
|
+
}
|
|
2576
|
+
//#endregion
|
|
2577
|
+
//#region src/config.ts
|
|
2578
|
+
/** Default config: everything enabled */
|
|
2579
|
+
const defaultConfig = {
|
|
2580
|
+
selectable: () => true,
|
|
2581
|
+
draggable: () => true,
|
|
2582
|
+
connectable: () => true,
|
|
2583
|
+
reconnectable: () => true,
|
|
2584
|
+
deletable: () => true,
|
|
2585
|
+
resizable: () => true,
|
|
2586
|
+
visible: () => true
|
|
2587
|
+
};
|
|
2588
|
+
/** Readonly preset: can select and view, cannot modify */
|
|
2589
|
+
const readonlyConfig = {
|
|
2590
|
+
draggable: false,
|
|
2591
|
+
connectable: false,
|
|
2592
|
+
reconnectable: false,
|
|
2593
|
+
deletable: false,
|
|
2594
|
+
resizable: false
|
|
2595
|
+
};
|
|
2596
|
+
/**
|
|
2597
|
+
* Resolve a behavior predicate.
|
|
2598
|
+
* If `false`, returns false. If function, calls it. If undefined, returns default.
|
|
2599
|
+
*/
|
|
2600
|
+
function resolveBehavior(predicate, entity, state, defaultValue = true) {
|
|
2601
|
+
if (predicate === false) return false;
|
|
2602
|
+
if (predicate === void 0) return defaultValue;
|
|
2603
|
+
return predicate(entity, state);
|
|
2604
|
+
}
|
|
2605
|
+
//#endregion
|
|
2606
|
+
//#region src/engine.ts
|
|
2607
|
+
function createFlowEngine(store, options = {}) {
|
|
2608
|
+
const router = options.edgeRouter ?? bezierRouter;
|
|
2609
|
+
const defaultLabelAnchor = options.labelAnchor ?? "fixed";
|
|
2610
|
+
const canConnectHook = options.canConnect;
|
|
2611
|
+
const globalConfig = options.config ?? {};
|
|
2612
|
+
const isVisibleHook = options.isVisible;
|
|
2613
|
+
const edgeHitTolerance = 8;
|
|
2614
|
+
function ctx() {
|
|
2615
|
+
return store.getSnapshot().context;
|
|
2616
|
+
}
|
|
2617
|
+
const edgePathsCache = /* @__PURE__ */ new WeakMap();
|
|
2618
|
+
const snapOffsetCache = /* @__PURE__ */ new WeakMap();
|
|
2619
|
+
const renderableCache = /* @__PURE__ */ new WeakMap();
|
|
2620
|
+
const groupGeometryCache = /* @__PURE__ */ new WeakMap();
|
|
2621
|
+
const edgeOwnerCache = /* @__PURE__ */ new WeakMap();
|
|
2622
|
+
function ownedEdges(c) {
|
|
2623
|
+
let index = edgeOwnerCache.get(c);
|
|
2624
|
+
if (!index) {
|
|
2625
|
+
index = /* @__PURE__ */ new Map();
|
|
2626
|
+
for (const edge of c.graph.edges) {
|
|
2627
|
+
const custom = options.edgeOwner?.(edge, c);
|
|
2628
|
+
const owner = options.edgeOwner && custom !== void 0 ? custom : getEdgeOwner(edge, c.graph.nodes);
|
|
2629
|
+
if (owner == null) continue;
|
|
2630
|
+
let bucket = index.get(owner);
|
|
2631
|
+
if (!bucket) index.set(owner, bucket = []);
|
|
2632
|
+
bucket.push(edge);
|
|
2633
|
+
}
|
|
2634
|
+
edgeOwnerCache.set(c, index);
|
|
2635
|
+
}
|
|
2636
|
+
return index;
|
|
2637
|
+
}
|
|
2638
|
+
const highlightIndexCache = /* @__PURE__ */ new WeakMap();
|
|
2639
|
+
const NO_HIGHLIGHTS = [];
|
|
2640
|
+
/**
|
|
2641
|
+
* Group geometry per snapshot, derived bottom-up. `derived` groups are a
|
|
2642
|
+
* pure function of their children's (visual) bounds — recomputed per
|
|
2643
|
+
* transition, so containment can't drift and sizing is reversible.
|
|
2644
|
+
*/
|
|
2645
|
+
function groupGeometryFor(c, node, visiting) {
|
|
2646
|
+
if (!node.group) return null;
|
|
2647
|
+
let byId = groupGeometryCache.get(c);
|
|
2648
|
+
const cached = byId?.get(node.id);
|
|
2649
|
+
if (cached !== void 0) return cached;
|
|
2650
|
+
const geometry = computeGroupGeometryRaw(c, node, visiting, void 0);
|
|
2651
|
+
if (!byId) {
|
|
2652
|
+
byId = /* @__PURE__ */ new Map();
|
|
2653
|
+
groupGeometryCache.set(c, byId);
|
|
2654
|
+
}
|
|
2655
|
+
byId.set(node.id, geometry);
|
|
2656
|
+
return geometry;
|
|
2657
|
+
}
|
|
2658
|
+
function computeGroupGeometryRaw(c, node, visiting, excludeChildren) {
|
|
2659
|
+
const contentSize = resolvedSize(node);
|
|
2660
|
+
if (node.group.type === "fixed") return fixedGroupGeometry(resolvedBounds(node), node.group.contentSize ?? {
|
|
2661
|
+
width: 0,
|
|
2662
|
+
height: 0
|
|
2663
|
+
}, node.group);
|
|
2664
|
+
const seen = visiting ?? /* @__PURE__ */ new Set();
|
|
2665
|
+
seen.add(node.id);
|
|
2666
|
+
let union = null;
|
|
2667
|
+
const children = indexChildren(c.graph.nodes).get(node.id);
|
|
2668
|
+
for (const child of children ?? []) {
|
|
2669
|
+
if (child.hidden || seen.has(child.id)) continue;
|
|
2670
|
+
if (excludeChildren?.has(child.id)) continue;
|
|
2671
|
+
const b = isDerivedGroup(child) ? excludeChildren ? computeGroupGeometryRaw(c, child, seen, excludeChildren).bounds : groupGeometryFor(c, child, seen)?.bounds ?? resolvedBounds(child) : hierarchyBounds(child, c);
|
|
2672
|
+
union = uniteRects(union, b);
|
|
2673
|
+
}
|
|
2674
|
+
for (const edge of ownedEdges(c).get(node.id) ?? []) {
|
|
2675
|
+
if (edge.hidden || !edge.label) continue;
|
|
2676
|
+
if (edge.sourceId === node.id && edge.targetId === node.id) continue;
|
|
2677
|
+
const label = edge.label;
|
|
2678
|
+
if (!label.width && !label.height) continue;
|
|
2679
|
+
union = uniteRects(union, {
|
|
2680
|
+
x: label.x + (label.dx ?? 0),
|
|
2681
|
+
y: label.y + (label.dy ?? 0),
|
|
2682
|
+
width: label.width,
|
|
2683
|
+
height: label.height
|
|
2684
|
+
});
|
|
2685
|
+
}
|
|
2686
|
+
return deriveGroupGeometry(contentSize, union, node.group, resolvedPosition(node));
|
|
2687
|
+
}
|
|
2688
|
+
function uniteRects(union, b) {
|
|
2689
|
+
return union ? unionRect(union, b) : { ...b };
|
|
2690
|
+
}
|
|
2691
|
+
/** Bounds with group hierarchy applied (derived groups wrap their children). */
|
|
2692
|
+
function hierarchyBounds(node, c) {
|
|
2693
|
+
if (isDerivedGroup(node)) {
|
|
2694
|
+
const geometry = groupGeometryFor(c, node);
|
|
2695
|
+
if (geometry) return geometry.bounds;
|
|
2696
|
+
}
|
|
2697
|
+
return resolvedBounds(node);
|
|
2698
|
+
}
|
|
2699
|
+
const edgePathMemo = /* @__PURE__ */ new Map();
|
|
2700
|
+
let edgePathMemoInputs = null;
|
|
2701
|
+
function getNode(id) {
|
|
2702
|
+
return indexNodes(ctx().graph.nodes).get(id);
|
|
2703
|
+
}
|
|
2704
|
+
function getEdge(id) {
|
|
2705
|
+
return indexEdges(ctx().graph.edges).get(id);
|
|
2706
|
+
}
|
|
2707
|
+
function getEntity(id) {
|
|
2708
|
+
return getNode(id) ?? getEdge(id);
|
|
2709
|
+
}
|
|
2710
|
+
function isNodeVisible(node, c, viewport = c.viewport) {
|
|
2711
|
+
if (node.hidden) return false;
|
|
2712
|
+
if (globalConfig.visible && !resolveBehavior(globalConfig.visible, node, c)) return false;
|
|
2713
|
+
if (isVisibleHook && !isVisibleHook(node, viewport)) return false;
|
|
2714
|
+
return true;
|
|
2715
|
+
}
|
|
2716
|
+
function isEdgeVisible(edge, graph, c, viewport = c.viewport) {
|
|
2717
|
+
if (edge.hidden) return false;
|
|
2718
|
+
const nodeIndex = indexNodes(graph.nodes);
|
|
2719
|
+
const source = nodeIndex.get(edge.sourceId);
|
|
2720
|
+
const target = nodeIndex.get(edge.targetId);
|
|
2721
|
+
if (!source || !target) return false;
|
|
2722
|
+
if (!isNodeVisible(source, c, viewport) || !isNodeVisible(target, c, viewport)) return false;
|
|
2723
|
+
if (globalConfig.visible && !resolveBehavior(globalConfig.visible, edge, c)) return false;
|
|
2724
|
+
if (isVisibleHook && !isVisibleHook(edge, viewport)) return false;
|
|
2725
|
+
return true;
|
|
2726
|
+
}
|
|
2727
|
+
function getAddressable(id) {
|
|
2728
|
+
const node = getNode(id);
|
|
2729
|
+
if (node) return {
|
|
2730
|
+
kind: "node",
|
|
2731
|
+
id,
|
|
2732
|
+
node
|
|
2733
|
+
};
|
|
2734
|
+
const edge = getEdge(id);
|
|
2735
|
+
if (edge) return {
|
|
2736
|
+
kind: "edge",
|
|
2737
|
+
id,
|
|
2738
|
+
edge
|
|
2739
|
+
};
|
|
2740
|
+
for (const candidate of ctx().graph.nodes) {
|
|
2741
|
+
const port = candidate.ports?.find((p) => p.id === id || `${candidate.id}:${p.name}` === id);
|
|
2742
|
+
if (port) return {
|
|
2743
|
+
kind: "port",
|
|
2744
|
+
id,
|
|
2745
|
+
nodeId: candidate.id,
|
|
2746
|
+
portName: port.name,
|
|
2747
|
+
port
|
|
2748
|
+
};
|
|
2749
|
+
}
|
|
2750
|
+
return null;
|
|
2751
|
+
}
|
|
2752
|
+
function getOwnerEntity(id) {
|
|
2753
|
+
const addressable = getAddressable(id);
|
|
2754
|
+
if (!addressable) return null;
|
|
2755
|
+
if (addressable.kind === "node") return addressable.node;
|
|
2756
|
+
if (addressable.kind === "edge") return addressable.edge;
|
|
2757
|
+
if (addressable.kind === "port") return getNode(addressable.nodeId) ?? null;
|
|
2758
|
+
return getEntity(addressable.ownerId) ?? null;
|
|
2759
|
+
}
|
|
2760
|
+
function screenToCanvas$1(screenPoint) {
|
|
2761
|
+
return screenToCanvas(screenPoint, ctx().viewport);
|
|
2762
|
+
}
|
|
2763
|
+
function canvasToScreen$1(canvasPoint) {
|
|
2764
|
+
return canvasToScreen(canvasPoint, ctx().viewport);
|
|
2765
|
+
}
|
|
2766
|
+
function entityAtPoint(canvasPoint) {
|
|
2767
|
+
const { graph } = ctx();
|
|
2768
|
+
const c = ctx();
|
|
2769
|
+
for (let i = graph.nodes.length - 1; i >= 0; i--) {
|
|
2770
|
+
const node = graph.nodes[i];
|
|
2771
|
+
if (!node) continue;
|
|
2772
|
+
const bounds = hierarchyBounds(node, c);
|
|
2773
|
+
if (getShapeOrDefault(node.shape ?? "rect").hitTest(canvasPoint, bounds)) return node;
|
|
2774
|
+
}
|
|
2775
|
+
const nodeMap = /* @__PURE__ */ new Map();
|
|
2776
|
+
for (const n of graph.nodes) nodeMap.set(n.id, n);
|
|
2777
|
+
for (let i = graph.edges.length - 1; i >= 0; i--) {
|
|
2778
|
+
const edge = graph.edges[i];
|
|
2779
|
+
if (!edge) continue;
|
|
2780
|
+
const lb = resolveEdgeLabelBounds(edge, nodeMap);
|
|
2781
|
+
if (lb && canvasPoint.x >= lb.x && canvasPoint.x <= lb.x + lb.width && canvasPoint.y >= lb.y && canvasPoint.y <= lb.y + lb.height) return edge;
|
|
2782
|
+
}
|
|
2783
|
+
const edgePaths = getAllEdgePaths();
|
|
2784
|
+
for (let i = graph.edges.length - 1; i >= 0; i--) {
|
|
2785
|
+
const edge = graph.edges[i];
|
|
2786
|
+
if (!edge) continue;
|
|
2787
|
+
const path = edgePaths.get(edge.id);
|
|
2788
|
+
if (!path) continue;
|
|
2789
|
+
if (path.segments.some((segment) => isPointNearPath(canvasPoint, segment, edgeHitTolerance))) return edge;
|
|
2790
|
+
}
|
|
2791
|
+
return null;
|
|
2792
|
+
}
|
|
2793
|
+
function entityIdAtPoint(canvasPoint) {
|
|
2794
|
+
return entityAtPoint(canvasPoint)?.id ?? null;
|
|
2795
|
+
}
|
|
2796
|
+
function getEdgePath(edgeId) {
|
|
2797
|
+
const { graph } = ctx();
|
|
2798
|
+
const edge = indexEdges(graph.edges).get(edgeId);
|
|
2799
|
+
if (!edge) return null;
|
|
2800
|
+
const nodeIndex = indexNodes(graph.nodes);
|
|
2801
|
+
const sourceNode = nodeIndex.get(edge.sourceId);
|
|
2802
|
+
const targetNode = nodeIndex.get(edge.targetId);
|
|
2803
|
+
if (!sourceNode || !targetNode) return null;
|
|
2804
|
+
const srcBounds = resolvedBounds(sourceNode);
|
|
2805
|
+
const tgtBounds = resolvedBounds(targetNode);
|
|
2806
|
+
const sourceDirection = bestDirection(srcBounds, tgtBounds);
|
|
2807
|
+
const targetDirection = bestDirection(tgtBounds, srcBounds);
|
|
2808
|
+
return router(edge, {
|
|
2809
|
+
sourceBounds: srcBounds,
|
|
2810
|
+
targetBounds: tgtBounds,
|
|
2811
|
+
sourceDirection,
|
|
2812
|
+
targetDirection,
|
|
2813
|
+
sourcePoint: getShapeAnchor(sourceNode, srcBounds, sourceDirection),
|
|
2814
|
+
targetPoint: getShapeAnchor(targetNode, tgtBounds, targetDirection),
|
|
2815
|
+
obstacles: []
|
|
2816
|
+
});
|
|
2817
|
+
}
|
|
2818
|
+
function getEdgePathBounds(edgeId) {
|
|
2819
|
+
const path = getAllEdgePaths().get(edgeId);
|
|
2820
|
+
if (!path) return null;
|
|
2821
|
+
return boundsForPoints(path.segments.flatMap((segment) => segment.points));
|
|
2822
|
+
}
|
|
2823
|
+
function getEdgeLabelBounds(edgeId) {
|
|
2824
|
+
const measured = ctx().measurements.get(edgeId);
|
|
2825
|
+
if (measured) return measured;
|
|
2826
|
+
const edge = getEdge(edgeId);
|
|
2827
|
+
if (!edge?.label) return null;
|
|
2828
|
+
const nodeMap = /* @__PURE__ */ new Map();
|
|
2829
|
+
for (const n of ctx().graph.nodes) nodeMap.set(n.id, n);
|
|
2830
|
+
return resolveEdgeLabelBounds(edge, nodeMap);
|
|
2831
|
+
}
|
|
2832
|
+
function getBounds(id) {
|
|
2833
|
+
const measured = ctx().measurements.get(id);
|
|
2834
|
+
if (measured) return measured;
|
|
2835
|
+
const addressable = getAddressable(id);
|
|
2836
|
+
if (!addressable) return null;
|
|
2837
|
+
if (addressable.kind === "node") return resolvedBounds(addressable.node);
|
|
2838
|
+
if (addressable.kind === "edge") return getEdgeLabelBounds(id) ?? getEdgePathBounds(id);
|
|
2839
|
+
if (addressable.kind === "port") {
|
|
2840
|
+
const owner = getNode(addressable.nodeId);
|
|
2841
|
+
if (!owner) return null;
|
|
2842
|
+
const ownerPosition = resolvedPosition(owner);
|
|
2843
|
+
return {
|
|
2844
|
+
x: ownerPosition.x + addressable.port.x,
|
|
2845
|
+
y: ownerPosition.y + addressable.port.y,
|
|
2846
|
+
width: addressable.port.width,
|
|
2847
|
+
height: addressable.port.height
|
|
2848
|
+
};
|
|
2849
|
+
}
|
|
2850
|
+
return null;
|
|
2851
|
+
}
|
|
2852
|
+
/** Resolve edge label bounds using committed (static) node positions */
|
|
2853
|
+
function resolveEdgeLabelBounds(edge, nodeMap) {
|
|
2854
|
+
if (!edge.label) return null;
|
|
2855
|
+
const src = nodeMap.get(edge.sourceId);
|
|
2856
|
+
const tgt = nodeMap.get(edge.targetId);
|
|
2857
|
+
if (!src || !tgt) return null;
|
|
2858
|
+
const srcB = {
|
|
2859
|
+
x: src.x,
|
|
2860
|
+
y: src.y,
|
|
2861
|
+
width: src.style?.customWidth ?? src.width,
|
|
2862
|
+
height: src.style?.customHeight ?? src.height
|
|
2863
|
+
};
|
|
2864
|
+
const tgtB = {
|
|
2865
|
+
x: tgt.x,
|
|
2866
|
+
y: tgt.y,
|
|
2867
|
+
width: tgt.style?.customWidth ?? tgt.width,
|
|
2868
|
+
height: tgt.style?.customHeight ?? tgt.height
|
|
2869
|
+
};
|
|
2870
|
+
const basePos = resolveLabelAnchor(edge.labelAnchor ?? defaultLabelAnchor, edge.label, srcB, tgtB);
|
|
2871
|
+
return {
|
|
2872
|
+
x: basePos.x + (edge.label.dx ?? 0),
|
|
2873
|
+
y: basePos.y + (edge.label.dy ?? 0),
|
|
2874
|
+
width: edge.label.width,
|
|
2875
|
+
height: edge.label.height
|
|
2876
|
+
};
|
|
2877
|
+
}
|
|
2878
|
+
/** Build selection bounding rect with properly resolved edge labels */
|
|
2879
|
+
function getSelectionSnapBounds(c) {
|
|
2880
|
+
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
2881
|
+
const nodeMap = /* @__PURE__ */ new Map();
|
|
2882
|
+
for (const n of c.graph.nodes) nodeMap.set(n.id, n);
|
|
2883
|
+
for (const n of c.graph.nodes) {
|
|
2884
|
+
if (!c.selection.has(n.id)) continue;
|
|
2885
|
+
const b = resolvedBounds(n);
|
|
2886
|
+
minX = Math.min(minX, b.x);
|
|
2887
|
+
minY = Math.min(minY, b.y);
|
|
2888
|
+
maxX = Math.max(maxX, b.x + b.width);
|
|
2889
|
+
maxY = Math.max(maxY, b.y + b.height);
|
|
2890
|
+
}
|
|
2891
|
+
for (const e of c.graph.edges) {
|
|
2892
|
+
if (!c.selection.has(e.id)) continue;
|
|
2893
|
+
const lb = resolveEdgeLabelBounds(e, nodeMap);
|
|
2894
|
+
if (!lb) continue;
|
|
2895
|
+
minX = Math.min(minX, lb.x);
|
|
2896
|
+
minY = Math.min(minY, lb.y);
|
|
2897
|
+
maxX = Math.max(maxX, lb.x + lb.width);
|
|
2898
|
+
maxY = Math.max(maxY, lb.y + lb.height);
|
|
2899
|
+
}
|
|
2900
|
+
if (!isFinite(minX)) return null;
|
|
2901
|
+
return {
|
|
2902
|
+
x: minX,
|
|
2903
|
+
y: minY,
|
|
2904
|
+
width: maxX - minX,
|
|
2905
|
+
height: maxY - minY
|
|
2906
|
+
};
|
|
2907
|
+
}
|
|
2908
|
+
/** Collect snap target rects from non-selected nodes + resolved edge label bounds.
|
|
2909
|
+
* Filters to entities at least partially in viewport and not overlapped by selection. */
|
|
2910
|
+
function getSnapTargetRects(c, selection, selBounds) {
|
|
2911
|
+
const vp = c.viewport;
|
|
2912
|
+
const vs = c.viewportSize;
|
|
2913
|
+
const viewRect = {
|
|
2914
|
+
x: -vp.x / vp.zoom,
|
|
2915
|
+
y: -vp.y / vp.zoom,
|
|
2916
|
+
width: vs.width / vp.zoom,
|
|
2917
|
+
height: vs.height / vp.zoom
|
|
2918
|
+
};
|
|
2919
|
+
const rects = [];
|
|
2920
|
+
const nodeMap = /* @__PURE__ */ new Map();
|
|
2921
|
+
for (const n of c.graph.nodes) {
|
|
2922
|
+
nodeMap.set(n.id, n);
|
|
2923
|
+
if (!selection.has(n.id) && !n.hidden) {
|
|
2924
|
+
const b = resolvedBounds(n);
|
|
2925
|
+
if (rectsIntersect(b, viewRect) && (!selBounds || !rectContains(selBounds, b))) rects.push(b);
|
|
2926
|
+
}
|
|
2927
|
+
}
|
|
2928
|
+
for (const edge of c.graph.edges) {
|
|
2929
|
+
if (selection.has(edge.id)) continue;
|
|
2930
|
+
const lb = resolveEdgeLabelBounds(edge, nodeMap);
|
|
2931
|
+
if (lb && rectsIntersect(lb, viewRect) && (!selBounds || !rectContains(selBounds, lb))) rects.push(lb);
|
|
2932
|
+
}
|
|
2933
|
+
return rects;
|
|
2934
|
+
}
|
|
2935
|
+
/**
|
|
2936
|
+
* Compute visual snap offset for edge routing.
|
|
2937
|
+
* Returns the offset to apply to selected nodes so edges follow the snapped visual position.
|
|
2938
|
+
* Memoized per store snapshot — callers invoke this once per node per frame.
|
|
2939
|
+
*/
|
|
2940
|
+
function getVisualSnapOffset() {
|
|
2941
|
+
const c = ctx();
|
|
2942
|
+
const cached = snapOffsetCache.get(c);
|
|
2943
|
+
if (cached) return cached;
|
|
2944
|
+
const result = computeVisualSnapOffset(c);
|
|
2945
|
+
snapOffsetCache.set(c, result);
|
|
2946
|
+
return result;
|
|
2947
|
+
}
|
|
2948
|
+
function computeVisualSnapOffset(c) {
|
|
2949
|
+
const selection = c.selection;
|
|
2950
|
+
const grid = c.snapToGrid ? c.snapGrid : null;
|
|
2951
|
+
let offset = {
|
|
2952
|
+
x: 0,
|
|
2953
|
+
y: 0
|
|
2954
|
+
};
|
|
2955
|
+
const nodesMoved = c.canvasState === "dragging" && c.graph.nodes.some((n) => selection.has(n.id) && ((n.dx ?? 0) !== 0 || (n.dy ?? 0) !== 0));
|
|
2956
|
+
const edgesMoved = c.canvasState === "dragging" && c.graph.edges.some((e) => selection.has(e.id) && e.label && ((e.label.dx ?? 0) !== 0 || (e.label.dy ?? 0) !== 0));
|
|
2957
|
+
if ((nodesMoved || edgesMoved) && c.enableSnapLines) {
|
|
2958
|
+
const selBounds = getSelectionSnapBounds(c);
|
|
2959
|
+
if (selBounds) {
|
|
2960
|
+
const snapResult = getSnapLines(selBounds, getSnapTargetRects(c, selection, selBounds), {
|
|
2961
|
+
threshold: c.snapThreshold / c.viewport.zoom,
|
|
2962
|
+
enableSpacing: c.enableSpacingGuides
|
|
2963
|
+
});
|
|
2964
|
+
if (snapResult) offset = {
|
|
2965
|
+
x: snapResult.snapRect.x - selBounds.x,
|
|
2966
|
+
y: snapResult.snapRect.y - selBounds.y
|
|
2967
|
+
};
|
|
2968
|
+
}
|
|
2969
|
+
}
|
|
2970
|
+
return {
|
|
2971
|
+
offset,
|
|
2972
|
+
grid,
|
|
2973
|
+
selection
|
|
2974
|
+
};
|
|
2975
|
+
}
|
|
2976
|
+
/** Get visual bounds for a node — applies snap offset for selected nodes during drag */
|
|
2977
|
+
function visualBounds(node, snap) {
|
|
2978
|
+
const c = ctx();
|
|
2979
|
+
if (c.canvasState === "resizing" && c.resizingEntityId === node.id) {
|
|
2980
|
+
const resizeBounds = getResizeBoundsFromContext(c);
|
|
2981
|
+
if (resizeBounds) return resizeBounds;
|
|
2982
|
+
}
|
|
2983
|
+
if (isDerivedGroup(node)) {
|
|
2984
|
+
const geometry = groupGeometryFor(c, node);
|
|
2985
|
+
if (geometry) return geometry.bounds;
|
|
2986
|
+
}
|
|
2987
|
+
const entityOffset = snap.selection.has(node.id) ? snap.offset : null;
|
|
2988
|
+
return snappedBounds(node, snap.grid, entityOffset);
|
|
2989
|
+
}
|
|
2990
|
+
function visualLabelBounds(edge, basePos, snap) {
|
|
2991
|
+
const isSelected = snap.selection.has(edge.id);
|
|
2992
|
+
let x = basePos.x + (edge.label?.dx ?? 0);
|
|
2993
|
+
let y = basePos.y + (edge.label?.dy ?? 0);
|
|
2994
|
+
if (isSelected) {
|
|
2995
|
+
x += snap.offset.x;
|
|
2996
|
+
y += snap.offset.y;
|
|
2997
|
+
if (snap.grid) {
|
|
2998
|
+
x = snapToGrid(x, snap.grid[0]);
|
|
2999
|
+
y = snapToGrid(y, snap.grid[1]);
|
|
3000
|
+
}
|
|
3001
|
+
}
|
|
3002
|
+
return {
|
|
3003
|
+
x,
|
|
3004
|
+
y,
|
|
3005
|
+
width: edge.label?.width ?? 0,
|
|
3006
|
+
height: edge.label?.height ?? 0
|
|
3007
|
+
};
|
|
3008
|
+
}
|
|
3009
|
+
/** Get visual position for a node — applies snap offset for selected nodes during drag */
|
|
3010
|
+
function visualPosition(node, snap) {
|
|
3011
|
+
const b = visualBounds(node, snap);
|
|
3012
|
+
return {
|
|
3013
|
+
x: b.x,
|
|
3014
|
+
y: b.y
|
|
3015
|
+
};
|
|
3016
|
+
}
|
|
3017
|
+
/** Get bounds for edge endpoint — port bounds if specified, otherwise node bounds */
|
|
3018
|
+
function getEndpointBounds(node, portName, snap) {
|
|
3019
|
+
if (portName && portName !== "*" && node.ports) {
|
|
3020
|
+
const port = node.ports.find((p) => p.name === portName);
|
|
3021
|
+
if (port) {
|
|
3022
|
+
const np = snap ? visualPosition(node, snap) : resolvedPosition(node);
|
|
3023
|
+
return {
|
|
3024
|
+
x: np.x + port.x,
|
|
3025
|
+
y: np.y + port.y,
|
|
3026
|
+
width: port.width,
|
|
3027
|
+
height: port.height
|
|
3028
|
+
};
|
|
3029
|
+
}
|
|
3030
|
+
}
|
|
3031
|
+
if (snap) return visualBounds(node, snap);
|
|
3032
|
+
return resolvedBounds(node);
|
|
3033
|
+
}
|
|
3034
|
+
function portSideOf(node, portName) {
|
|
3035
|
+
if (!portName || portName === "*" || !node.ports) return null;
|
|
3036
|
+
return node.ports.find((p) => p.name === portName)?.side ?? null;
|
|
3037
|
+
}
|
|
3038
|
+
function getShapeAnchor(node, bounds, direction) {
|
|
3039
|
+
return getShapeOrDefault(node.shape ?? "rect").edgeAnchor(bounds, direction);
|
|
3040
|
+
}
|
|
3041
|
+
function resolveEndpointPoint(node, nodeBounds, portName, otherBounds, fallbackDirection, snap) {
|
|
3042
|
+
if (portName === "*" && node.ports?.length) return closestPortPosition(node, {
|
|
3043
|
+
x: otherBounds.x + otherBounds.width / 2,
|
|
3044
|
+
y: otherBounds.y + otherBounds.height / 2
|
|
3045
|
+
});
|
|
3046
|
+
if (portName && portName !== "*" && node.ports) {
|
|
3047
|
+
const port = node.ports.find((p) => p.name === portName);
|
|
3048
|
+
if (port) {
|
|
3049
|
+
const position = snap ? visualPosition(node, snap) : resolvedPosition(node);
|
|
3050
|
+
return {
|
|
3051
|
+
x: position.x + port.x + port.width / 2,
|
|
3052
|
+
y: position.y + port.y + port.height / 2
|
|
3053
|
+
};
|
|
3054
|
+
}
|
|
3055
|
+
}
|
|
3056
|
+
return getShapeAnchor(node, nodeBounds, fallbackDirection);
|
|
3057
|
+
}
|
|
3058
|
+
/**
|
|
3059
|
+
* Resolve the geometry shared by edge rendering and endpoint queries: the
|
|
3060
|
+
* endpoint bounds, the facing direction of each end, and the anchor points.
|
|
3061
|
+
* Single source of truth so rendered paths and `getEdgeEndpoints` can never
|
|
3062
|
+
* disagree on which side of a node an edge attaches to.
|
|
3063
|
+
*/
|
|
3064
|
+
function resolveEdgeGeometry(edge, source, target, snap) {
|
|
3065
|
+
const sourceBounds = getEndpointBounds(source, edge.sourcePort, snap);
|
|
3066
|
+
const targetBounds = getEndpointBounds(target, edge.targetPort, snap);
|
|
3067
|
+
const sourceDirection = portSideOf(source, edge.sourcePort) ?? bestDirection(sourceBounds, targetBounds);
|
|
3068
|
+
const targetDirection = portSideOf(target, edge.targetPort) ?? bestDirection(targetBounds, sourceBounds);
|
|
3069
|
+
return {
|
|
3070
|
+
sourceBounds,
|
|
3071
|
+
targetBounds,
|
|
3072
|
+
sourceDirection,
|
|
3073
|
+
targetDirection,
|
|
3074
|
+
sourcePoint: resolveEndpointPoint(source, sourceBounds, edge.sourcePort, targetBounds, sourceDirection, snap),
|
|
3075
|
+
targetPoint: resolveEndpointPoint(target, targetBounds, edge.targetPort, sourceBounds, targetDirection, snap)
|
|
3076
|
+
};
|
|
3077
|
+
}
|
|
3078
|
+
/**
|
|
3079
|
+
* Route all edges. Memoized per (store snapshot, graph) pair — repeated
|
|
3080
|
+
* calls within one transition (edge layer, label layer, hit testing) reuse
|
|
3081
|
+
* the same Map. Callers must treat the result as read-only.
|
|
3082
|
+
*/
|
|
3083
|
+
function getAllEdgePaths(options = {}) {
|
|
3084
|
+
const c = ctx();
|
|
3085
|
+
const graph = options.graph ?? c.graph;
|
|
3086
|
+
let byGraph = edgePathsCache.get(c);
|
|
3087
|
+
const cached = byGraph?.get(graph);
|
|
3088
|
+
if (cached) return cached;
|
|
3089
|
+
const paths = computeAllEdgePaths(c, graph);
|
|
3090
|
+
if (!byGraph) {
|
|
3091
|
+
byGraph = /* @__PURE__ */ new WeakMap();
|
|
3092
|
+
edgePathsCache.set(c, byGraph);
|
|
3093
|
+
}
|
|
3094
|
+
byGraph.set(graph, paths);
|
|
3095
|
+
return paths;
|
|
3096
|
+
}
|
|
3097
|
+
function computeAllEdgePaths(c, graph) {
|
|
3098
|
+
const paths = /* @__PURE__ */ new Map();
|
|
3099
|
+
const snap = getVisualSnapOffset();
|
|
3100
|
+
const nodeMap = indexNodes(graph.nodes);
|
|
3101
|
+
const memoUsable = !isVisibleHook && !globalConfig.visible;
|
|
3102
|
+
if (memoUsable) {
|
|
3103
|
+
const inputs = edgePathMemoInputs;
|
|
3104
|
+
if (!inputs || inputs.canvasState !== c.canvasState || inputs.selection !== c.selection || inputs.offsetX !== snap.offset.x || inputs.offsetY !== snap.offset.y || inputs.grid !== snap.grid || inputs.resizingEntityId !== c.resizingEntityId || inputs.resizeCorners !== c.resizeCorners) {
|
|
3105
|
+
edgePathMemo.clear();
|
|
3106
|
+
edgePathMemoInputs = {
|
|
3107
|
+
canvasState: c.canvasState,
|
|
3108
|
+
selection: c.selection,
|
|
3109
|
+
offsetX: snap.offset.x,
|
|
3110
|
+
offsetY: snap.offset.y,
|
|
3111
|
+
grid: snap.grid,
|
|
3112
|
+
resizingEntityId: c.resizingEntityId,
|
|
3113
|
+
resizeCorners: c.resizeCorners
|
|
3114
|
+
};
|
|
3115
|
+
}
|
|
3116
|
+
}
|
|
3117
|
+
for (const edge of graph.edges) {
|
|
3118
|
+
const source = nodeMap.get(edge.sourceId);
|
|
3119
|
+
const target = nodeMap.get(edge.targetId);
|
|
3120
|
+
if (!source || !target) continue;
|
|
3121
|
+
if (!isEdgeVisible(edge, graph, c)) continue;
|
|
3122
|
+
const memoizableEdge = memoUsable && !isDerivedGroup(source) && !isDerivedGroup(target);
|
|
3123
|
+
if (memoizableEdge) {
|
|
3124
|
+
const entry = edgePathMemo.get(edge.id);
|
|
3125
|
+
if (entry && entry.edge === edge && entry.source === source && entry.target === target) {
|
|
3126
|
+
paths.set(edge.id, entry.path);
|
|
3127
|
+
continue;
|
|
3128
|
+
}
|
|
3129
|
+
}
|
|
3130
|
+
const { sourceBounds, targetBounds, sourcePoint, targetPoint } = resolveEdgeGeometry(edge, source, target, snap);
|
|
3131
|
+
let pathData;
|
|
3132
|
+
if (edge.label) {
|
|
3133
|
+
const labelBounds = visualLabelBounds(edge, resolveLabelAnchor(edge.labelAnchor ?? defaultLabelAnchor, edge.label, sourceBounds, targetBounds), snap);
|
|
3134
|
+
pathData = {
|
|
3135
|
+
segments: [router(edge, {
|
|
3136
|
+
sourceBounds,
|
|
3137
|
+
targetBounds: labelBounds,
|
|
3138
|
+
sourceDirection: portSideOf(source, edge.sourcePort) ?? bestDirection(sourceBounds, labelBounds),
|
|
3139
|
+
targetDirection: bestDirection(labelBounds, sourceBounds),
|
|
3140
|
+
sourcePoint,
|
|
3141
|
+
obstacles: []
|
|
3142
|
+
}), router(edge, {
|
|
3143
|
+
sourceBounds: labelBounds,
|
|
3144
|
+
targetBounds,
|
|
3145
|
+
sourceDirection: bestDirection(labelBounds, targetBounds),
|
|
3146
|
+
targetDirection: portSideOf(target, edge.targetPort) ?? bestDirection(targetBounds, labelBounds),
|
|
3147
|
+
targetPoint,
|
|
3148
|
+
obstacles: []
|
|
3149
|
+
})],
|
|
3150
|
+
labelBounds
|
|
3151
|
+
};
|
|
3152
|
+
} else pathData = {
|
|
3153
|
+
segments: [router(edge, {
|
|
3154
|
+
sourceBounds,
|
|
3155
|
+
targetBounds,
|
|
3156
|
+
sourceDirection: portSideOf(source, edge.sourcePort) ?? bestDirection(sourceBounds, targetBounds),
|
|
3157
|
+
targetDirection: portSideOf(target, edge.targetPort) ?? bestDirection(targetBounds, sourceBounds),
|
|
3158
|
+
sourcePoint,
|
|
3159
|
+
targetPoint,
|
|
3160
|
+
obstacles: []
|
|
3161
|
+
})],
|
|
3162
|
+
labelBounds: null
|
|
3163
|
+
};
|
|
3164
|
+
paths.set(edge.id, pathData);
|
|
3165
|
+
if (memoizableEdge) edgePathMemo.set(edge.id, {
|
|
3166
|
+
edge,
|
|
3167
|
+
source,
|
|
3168
|
+
target,
|
|
3169
|
+
path: pathData
|
|
3170
|
+
});
|
|
3171
|
+
}
|
|
3172
|
+
if (memoUsable && edgePathMemo.size > graph.edges.length * 2 + 16) edgePathMemo.clear();
|
|
3173
|
+
return paths;
|
|
3174
|
+
}
|
|
3175
|
+
function subscribe(listener) {
|
|
3176
|
+
const subscription = store.subscribe((snapshot) => {
|
|
3177
|
+
listener(snapshot.context);
|
|
3178
|
+
});
|
|
3179
|
+
return () => subscription.unsubscribe();
|
|
3180
|
+
}
|
|
3181
|
+
function computeRenderableEntities(c, options) {
|
|
3182
|
+
const vp = c.viewport;
|
|
3183
|
+
const overscan = options.overscan ?? 0;
|
|
3184
|
+
const visibleRect = getVisibleBounds(vp, c.viewportSize);
|
|
3185
|
+
const cullRect = {
|
|
3186
|
+
x: visibleRect.x - overscan,
|
|
3187
|
+
y: visibleRect.y - overscan,
|
|
3188
|
+
width: visibleRect.width + overscan * 2,
|
|
3189
|
+
height: visibleRect.height + overscan * 2
|
|
3190
|
+
};
|
|
3191
|
+
const cullMode = options.cull === "viewport" && rectContains(cullRect, getGraphBounds(c.graph.nodes, c.graph.edges)) ? "none" : options.cull;
|
|
3192
|
+
const nodes = c.graph.nodes.filter((n) => {
|
|
3193
|
+
if (!isNodeVisible(n, c, vp)) return false;
|
|
3194
|
+
if (cullMode === "viewport" && !rectsIntersect(hierarchyBounds(n, c), cullRect)) return false;
|
|
3195
|
+
return true;
|
|
3196
|
+
});
|
|
3197
|
+
const edgePaths = getAllEdgePaths();
|
|
3198
|
+
return {
|
|
3199
|
+
nodes,
|
|
3200
|
+
edges: c.graph.edges.filter((e) => {
|
|
3201
|
+
if (!isEdgeVisible(e, c.graph, c, vp)) return false;
|
|
3202
|
+
if (cullMode !== "viewport") return true;
|
|
3203
|
+
if (e.label) {
|
|
3204
|
+
if (rectsIntersect({
|
|
3205
|
+
x: e.label.x + (e.label.dx ?? 0),
|
|
3206
|
+
y: e.label.y + (e.label.dy ?? 0),
|
|
3207
|
+
width: e.label.width,
|
|
3208
|
+
height: e.label.height
|
|
3209
|
+
}, cullRect)) return true;
|
|
3210
|
+
}
|
|
3211
|
+
if (edgePaths.get(e.id)?.segments.some((segment) => {
|
|
3212
|
+
const bounds = pathBounds(segment);
|
|
3213
|
+
return bounds !== null && rectsIntersect(bounds, cullRect);
|
|
3214
|
+
})) return true;
|
|
3215
|
+
const srcNode = getNode(e.sourceId);
|
|
3216
|
+
const tgtNode = getNode(e.targetId);
|
|
3217
|
+
return !!srcNode && !!tgtNode && (rectsIntersect(resolvedBounds(srcNode), cullRect) || rectsIntersect(resolvedBounds(tgtNode), cullRect));
|
|
3218
|
+
})
|
|
3219
|
+
};
|
|
3220
|
+
}
|
|
3221
|
+
return {
|
|
3222
|
+
store,
|
|
3223
|
+
trigger: store.trigger,
|
|
3224
|
+
getContext: ctx,
|
|
3225
|
+
getNode,
|
|
3226
|
+
getEdge,
|
|
3227
|
+
getEntity,
|
|
3228
|
+
getAddressable,
|
|
3229
|
+
getOwnerEntity,
|
|
3230
|
+
getBounds,
|
|
3231
|
+
screenToCanvas: screenToCanvas$1,
|
|
3232
|
+
canvasToScreen: canvasToScreen$1,
|
|
3233
|
+
entityAtPoint,
|
|
3234
|
+
entityIdAtPoint,
|
|
3235
|
+
getEdgePath,
|
|
3236
|
+
getEdgePathBounds,
|
|
3237
|
+
getEdgeLabelBounds,
|
|
3238
|
+
getAllEdgePaths,
|
|
3239
|
+
resolvedPosition: (nodeOrId) => {
|
|
3240
|
+
const node = typeof nodeOrId === "string" ? getNode(nodeOrId) : nodeOrId;
|
|
3241
|
+
if (!node) return {
|
|
3242
|
+
x: 0,
|
|
3243
|
+
y: 0
|
|
3244
|
+
};
|
|
3245
|
+
return resolvedPosition(node);
|
|
3246
|
+
},
|
|
3247
|
+
resolvedBounds: (nodeOrId) => {
|
|
3248
|
+
const node = typeof nodeOrId === "string" ? getNode(nodeOrId) : nodeOrId;
|
|
3249
|
+
if (!node) return {
|
|
3250
|
+
x: 0,
|
|
3251
|
+
y: 0,
|
|
3252
|
+
width: 0,
|
|
3253
|
+
height: 0
|
|
3254
|
+
};
|
|
3255
|
+
const c = ctx();
|
|
3256
|
+
if (isDerivedGroup(node)) return hierarchyBounds(node, c);
|
|
3257
|
+
if (c.canvasState === "dragging" && c.selection.has(node.id) && ((node.dx ?? 0) !== 0 || (node.dy ?? 0) !== 0)) return visualBounds(node, getVisualSnapOffset());
|
|
3258
|
+
return resolvedBounds(node);
|
|
3259
|
+
},
|
|
3260
|
+
/**
|
|
3261
|
+
* All highlights on an entity as `{ kind, meta }` entries. Returns a
|
|
3262
|
+
* referentially stable array (the same empty array when none), so
|
|
3263
|
+
* subscribers can use identity equality.
|
|
3264
|
+
*/
|
|
3265
|
+
getHighlights: (entityId) => {
|
|
3266
|
+
const highlights = ctx().highlights;
|
|
3267
|
+
if (highlights.size === 0) return NO_HIGHLIGHTS;
|
|
3268
|
+
let index = highlightIndexCache.get(highlights);
|
|
3269
|
+
if (!index) {
|
|
3270
|
+
index = /* @__PURE__ */ new Map();
|
|
3271
|
+
for (const [kind, bucket] of highlights) for (const [id, meta] of bucket) {
|
|
3272
|
+
let entries = index.get(id);
|
|
3273
|
+
if (!entries) index.set(id, entries = []);
|
|
3274
|
+
entries.push({
|
|
3275
|
+
kind,
|
|
3276
|
+
meta
|
|
3277
|
+
});
|
|
3278
|
+
}
|
|
3279
|
+
highlightIndexCache.set(highlights, index);
|
|
3280
|
+
}
|
|
3281
|
+
return index.get(entityId) ?? NO_HIGHLIGHTS;
|
|
3282
|
+
},
|
|
3283
|
+
/** Whether the entity is currently hovered. */
|
|
3284
|
+
isHovered: (entityId) => ctx().hoveredEntityIds.has(entityId),
|
|
3285
|
+
/**
|
|
3286
|
+
* Deepest group whose child area intersects `bounds`, with the overlap
|
|
3287
|
+
* ratio (intersection / bounds area). Groups in `exclude` (and groups
|
|
3288
|
+
* inside excluded subtrees) are skipped, and excluded nodes do not
|
|
3289
|
+
* contribute to derived child areas — i.e. each group is evaluated as it
|
|
3290
|
+
* would be without the moving nodes. Drives reparent-on-drop.
|
|
3291
|
+
*/
|
|
3292
|
+
getGroupDropTarget: (bounds, options = {}) => {
|
|
3293
|
+
const c = ctx();
|
|
3294
|
+
const excluded = new Set(options.exclude ?? []);
|
|
3295
|
+
const nodeIndex = indexNodes(c.graph.nodes);
|
|
3296
|
+
const depths = nodeDepths(c.graph.nodes);
|
|
3297
|
+
const area = Math.max(bounds.width * bounds.height, 1);
|
|
3298
|
+
let best = null;
|
|
3299
|
+
for (const node of c.graph.nodes) {
|
|
3300
|
+
if (!node.group || node.hidden || excluded.has(node.id)) continue;
|
|
3301
|
+
let ancestorId = node.parentId ?? null;
|
|
3302
|
+
let insideExcluded = false;
|
|
3303
|
+
const seen = /* @__PURE__ */ new Set();
|
|
3304
|
+
while (ancestorId && !seen.has(ancestorId)) {
|
|
3305
|
+
if (excluded.has(ancestorId)) {
|
|
3306
|
+
insideExcluded = true;
|
|
3307
|
+
break;
|
|
3308
|
+
}
|
|
3309
|
+
seen.add(ancestorId);
|
|
3310
|
+
ancestorId = nodeIndex.get(ancestorId)?.parentId ?? null;
|
|
3311
|
+
}
|
|
3312
|
+
if (insideExcluded) continue;
|
|
3313
|
+
const childArea = computeGroupGeometryRaw(c, node, void 0, excluded).childArea;
|
|
3314
|
+
const overlap = Math.max(0, Math.min(bounds.x + bounds.width, childArea.x + childArea.width) - Math.max(bounds.x, childArea.x)) * Math.max(0, Math.min(bounds.y + bounds.height, childArea.y + childArea.height) - Math.max(bounds.y, childArea.y)) / area;
|
|
3315
|
+
if (overlap <= 0) continue;
|
|
3316
|
+
const depth = depths.get(node.id) ?? 0;
|
|
3317
|
+
if (!best || depth > best.depth || depth === best.depth && overlap > best.overlap) best = {
|
|
3318
|
+
id: node.id,
|
|
3319
|
+
overlap,
|
|
3320
|
+
childArea,
|
|
3321
|
+
depth
|
|
3322
|
+
};
|
|
3323
|
+
}
|
|
3324
|
+
return best ? {
|
|
3325
|
+
id: best.id,
|
|
3326
|
+
overlap: best.overlap,
|
|
3327
|
+
childArea: best.childArea
|
|
3328
|
+
} : null;
|
|
3329
|
+
},
|
|
3330
|
+
/**
|
|
3331
|
+
* Canvas-space anchor rect for toolbars: a node's (hierarchy-aware)
|
|
3332
|
+
* bounds, an edge's label bounds — or, with no label, an assumed 1×1
|
|
3333
|
+
* label at the path midpoint — and, given several ids (e.g. the
|
|
3334
|
+
* selection), the union of their anchors. Null when nothing resolves.
|
|
3335
|
+
*/
|
|
3336
|
+
getToolbarAnchor: (idOrIds) => {
|
|
3337
|
+
const ids = typeof idOrIds === "string" ? [idOrIds] : [...idOrIds];
|
|
3338
|
+
const c = ctx();
|
|
3339
|
+
let union = null;
|
|
3340
|
+
for (const id of ids) {
|
|
3341
|
+
let rect = null;
|
|
3342
|
+
const node = getNode(id);
|
|
3343
|
+
if (node) rect = hierarchyBounds(node, c);
|
|
3344
|
+
else {
|
|
3345
|
+
const edge = getEdge(id);
|
|
3346
|
+
if (edge?.label && (edge.label.width || edge.label.height)) rect = {
|
|
3347
|
+
x: edge.label.x + (edge.label.dx ?? 0),
|
|
3348
|
+
y: edge.label.y + (edge.label.dy ?? 0),
|
|
3349
|
+
width: edge.label.width,
|
|
3350
|
+
height: edge.label.height
|
|
3351
|
+
};
|
|
3352
|
+
else if (edge) {
|
|
3353
|
+
const points = getAllEdgePaths().get(id)?.segments[0]?.points ?? [];
|
|
3354
|
+
if (points.length > 0) {
|
|
3355
|
+
const mid = points.length % 2 === 1 ? points[(points.length - 1) / 2] : {
|
|
3356
|
+
x: (points[points.length / 2 - 1].x + points[points.length / 2].x) / 2,
|
|
3357
|
+
y: (points[points.length / 2 - 1].y + points[points.length / 2].y) / 2
|
|
3358
|
+
};
|
|
3359
|
+
rect = {
|
|
3360
|
+
x: mid.x,
|
|
3361
|
+
y: mid.y,
|
|
3362
|
+
width: 1,
|
|
3363
|
+
height: 1
|
|
3364
|
+
};
|
|
3365
|
+
}
|
|
3366
|
+
}
|
|
3367
|
+
}
|
|
3368
|
+
if (!rect) continue;
|
|
3369
|
+
union = union ? uniteRects(union, rect) : rect;
|
|
3370
|
+
}
|
|
3371
|
+
return union;
|
|
3372
|
+
},
|
|
3373
|
+
/** The node containing this edge (null = root). See `edgeOwner` option. */
|
|
3374
|
+
getEdgeOwner: (edgeId) => {
|
|
3375
|
+
const c = ctx();
|
|
3376
|
+
const edge = indexEdges(c.graph.edges).get(edgeId);
|
|
3377
|
+
if (!edge) return null;
|
|
3378
|
+
const custom = options.edgeOwner?.(edge, c);
|
|
3379
|
+
return options.edgeOwner && custom !== void 0 ? custom : getEdgeOwner(edge, c.graph.nodes);
|
|
3380
|
+
},
|
|
3381
|
+
/** Group geometry (overall bounds, content block, child area), or null. */
|
|
3382
|
+
getGroupGeometry: (nodeOrId) => {
|
|
3383
|
+
const node = typeof nodeOrId === "string" ? getNode(nodeOrId) : nodeOrId;
|
|
3384
|
+
if (!node) return null;
|
|
3385
|
+
return groupGeometryFor(ctx(), node);
|
|
3386
|
+
},
|
|
3387
|
+
getGraphBounds: () => getGraphBounds(ctx().graph.nodes, ctx().graph.edges),
|
|
3388
|
+
getSelectionBounds: () => {
|
|
3389
|
+
const c = ctx();
|
|
3390
|
+
return getSelectionBounds(c.graph.nodes, c.graph.edges, c.selection);
|
|
3391
|
+
},
|
|
3392
|
+
entitiesInRect: (rect) => {
|
|
3393
|
+
const c = ctx();
|
|
3394
|
+
const { graph } = c;
|
|
3395
|
+
const ids = [];
|
|
3396
|
+
const edgePaths = getAllEdgePaths();
|
|
3397
|
+
for (const n of graph.nodes) {
|
|
3398
|
+
if (!isNodeVisible(n, c)) continue;
|
|
3399
|
+
if (rectsIntersect(rect, resolvedBounds(n))) ids.push(n.id);
|
|
3400
|
+
}
|
|
3401
|
+
for (const e of graph.edges) {
|
|
3402
|
+
if (!isEdgeVisible(e, graph, c)) continue;
|
|
3403
|
+
if (e.label) {
|
|
3404
|
+
if (rectsIntersect(rect, {
|
|
3405
|
+
x: e.label.x + (e.label.dx ?? 0),
|
|
3406
|
+
y: e.label.y + (e.label.dy ?? 0),
|
|
3407
|
+
width: e.label.width,
|
|
3408
|
+
height: e.label.height
|
|
3409
|
+
})) ids.push(e.id);
|
|
3410
|
+
continue;
|
|
3411
|
+
}
|
|
3412
|
+
const path = edgePaths.get(e.id);
|
|
3413
|
+
if (path && path.segments.some((segment) => rectIntersectsPath(rect, segment))) ids.push(e.id);
|
|
3414
|
+
}
|
|
3415
|
+
return ids;
|
|
3416
|
+
},
|
|
3417
|
+
getPortPosition: (nodeId, portName) => {
|
|
3418
|
+
const node = getNode(nodeId);
|
|
3419
|
+
if (!node?.ports) return null;
|
|
3420
|
+
const port = node.ports.find((p) => p.name === portName);
|
|
3421
|
+
if (!port) return null;
|
|
3422
|
+
const np = resolvedPosition(node);
|
|
3423
|
+
return {
|
|
3424
|
+
x: np.x + port.x + port.width / 2,
|
|
3425
|
+
y: np.y + port.y + port.height / 2
|
|
3426
|
+
};
|
|
3427
|
+
},
|
|
3428
|
+
getPortPositions: (nodeId) => {
|
|
3429
|
+
const result = /* @__PURE__ */ new Map();
|
|
3430
|
+
const node = getNode(nodeId);
|
|
3431
|
+
if (!node?.ports) return result;
|
|
3432
|
+
const np = resolvedPosition(node);
|
|
3433
|
+
for (const port of node.ports) result.set(port.name, {
|
|
3434
|
+
x: np.x + port.x + port.width / 2,
|
|
3435
|
+
y: np.y + port.y + port.height / 2
|
|
3436
|
+
});
|
|
3437
|
+
return result;
|
|
3438
|
+
},
|
|
3439
|
+
getEdgeEndpoints: (edgeId) => {
|
|
3440
|
+
const { graph } = ctx();
|
|
3441
|
+
const edge = graph.edges.find((e) => e.id === edgeId);
|
|
3442
|
+
if (!edge) return null;
|
|
3443
|
+
const srcNode = graph.nodes.find((n) => n.id === edge.sourceId);
|
|
3444
|
+
const tgtNode = graph.nodes.find((n) => n.id === edge.targetId);
|
|
3445
|
+
if (!srcNode || !tgtNode) return null;
|
|
3446
|
+
const { sourcePoint, targetPoint } = resolveEdgeGeometry(edge, srcNode, tgtNode, getVisualSnapOffset());
|
|
3447
|
+
return {
|
|
3448
|
+
source: sourcePoint,
|
|
3449
|
+
target: targetPoint
|
|
3450
|
+
};
|
|
3451
|
+
},
|
|
3452
|
+
validateConnection: (proposed) => {
|
|
3453
|
+
const { graph } = ctx();
|
|
3454
|
+
const srcNode = graph.nodes.find((n) => n.id === proposed.sourceId);
|
|
3455
|
+
const tgtNode = graph.nodes.find((n) => n.id === proposed.targetId);
|
|
3456
|
+
if (!srcNode || !tgtNode) return false;
|
|
3457
|
+
if (proposed.sourcePort && srcNode.ports) {
|
|
3458
|
+
const port = srcNode.ports.find((p) => p.name === proposed.sourcePort);
|
|
3459
|
+
if (port && port.direction === "in") return false;
|
|
3460
|
+
}
|
|
3461
|
+
if (proposed.targetPort && tgtNode.ports) {
|
|
3462
|
+
const port = tgtNode.ports.find((p) => p.name === proposed.targetPort);
|
|
3463
|
+
if (port && port.direction === "out") return false;
|
|
3464
|
+
}
|
|
3465
|
+
if (canConnectHook && !canConnectHook(proposed)) return false;
|
|
3466
|
+
return true;
|
|
3467
|
+
},
|
|
3468
|
+
resolveEntityConfig: (entityOrId) => {
|
|
3469
|
+
const entity = typeof entityOrId === "string" ? getEntity(entityOrId) : entityOrId;
|
|
3470
|
+
if (!entity) return {
|
|
3471
|
+
hidden: false,
|
|
3472
|
+
selectable: true,
|
|
3473
|
+
draggable: true,
|
|
3474
|
+
connectable: true,
|
|
3475
|
+
reconnectable: true,
|
|
3476
|
+
deletable: true,
|
|
3477
|
+
resizable: true
|
|
3478
|
+
};
|
|
3479
|
+
const c = ctx();
|
|
3480
|
+
return {
|
|
3481
|
+
hidden: entity.type === "edge" ? !isEdgeVisible(entity, c.graph, c) : !isNodeVisible(entity, c),
|
|
3482
|
+
selectable: entity.selectable ?? resolveBehavior(globalConfig.selectable, entity, c, true),
|
|
3483
|
+
draggable: entity.draggable ?? resolveBehavior(globalConfig.draggable, entity, c, true),
|
|
3484
|
+
connectable: entity.connectable ?? resolveBehavior(globalConfig.connectable, entity, c, true),
|
|
3485
|
+
reconnectable: entity.reconnectable ?? resolveBehavior(globalConfig.reconnectable, entity, c, true),
|
|
3486
|
+
deletable: entity.deletable ?? resolveBehavior(globalConfig.deletable, entity, c, true),
|
|
3487
|
+
resizable: entity.resizable ?? resolveBehavior(globalConfig.resizable, entity, c, false)
|
|
3488
|
+
};
|
|
3489
|
+
},
|
|
3490
|
+
getVisibleEntities: () => {
|
|
3491
|
+
const c = ctx();
|
|
3492
|
+
return {
|
|
3493
|
+
nodes: c.graph.nodes.filter((n) => isNodeVisible(n, c)),
|
|
3494
|
+
edges: c.graph.edges.filter((e) => isEdgeVisible(e, c.graph, c))
|
|
3495
|
+
};
|
|
3496
|
+
},
|
|
3497
|
+
/**
|
|
3498
|
+
* Renderable (visible, optionally viewport-culled) entities. Memoized per
|
|
3499
|
+
* (store snapshot, cull/overscan) so several render layers can share one
|
|
3500
|
+
* computation per frame. Treat the result as read-only.
|
|
3501
|
+
*/
|
|
3502
|
+
getRenderableEntities: (options = {}) => {
|
|
3503
|
+
const c = ctx();
|
|
3504
|
+
const cacheKey = `${options.cull ?? "none"}:${options.overscan ?? 0}`;
|
|
3505
|
+
let byKey = renderableCache.get(c);
|
|
3506
|
+
const cached = byKey?.get(cacheKey);
|
|
3507
|
+
if (cached) return cached;
|
|
3508
|
+
const result = computeRenderableEntities(c, options);
|
|
3509
|
+
if (!byKey) {
|
|
3510
|
+
byKey = /* @__PURE__ */ new Map();
|
|
3511
|
+
renderableCache.set(c, byKey);
|
|
3512
|
+
}
|
|
3513
|
+
byKey.set(cacheKey, result);
|
|
3514
|
+
return result;
|
|
3515
|
+
},
|
|
3516
|
+
getIntersectingNodes: (nodeId) => {
|
|
3517
|
+
const node = getNode(nodeId);
|
|
3518
|
+
if (!node) return [];
|
|
3519
|
+
const bounds = resolvedBounds(node);
|
|
3520
|
+
return ctx().graph.nodes.filter((n) => n.id !== nodeId && rectsIntersect(bounds, resolvedBounds(n)));
|
|
3521
|
+
},
|
|
3522
|
+
getAriaLabel: (entityId) => {
|
|
3523
|
+
const { graph } = ctx();
|
|
3524
|
+
const node = graph.nodes.find((n) => n.id === entityId);
|
|
3525
|
+
if (node) return `Node: ${node.label ?? node.id}`;
|
|
3526
|
+
const edge = graph.edges.find((e) => e.id === entityId);
|
|
3527
|
+
if (edge) {
|
|
3528
|
+
const src = graph.nodes.find((n) => n.id === edge.sourceId);
|
|
3529
|
+
const tgt = graph.nodes.find((n) => n.id === edge.targetId);
|
|
3530
|
+
const label = edge.label ? ` (${edge.label.data?.text ?? ""})` : "";
|
|
3531
|
+
return `Edge: ${src?.label ?? edge.sourceId} → ${tgt?.label ?? edge.targetId}${label}`;
|
|
3532
|
+
}
|
|
3533
|
+
return entityId;
|
|
3534
|
+
},
|
|
3535
|
+
/** Compute snap result during drag. Returns null if not dragging or snap lines disabled.
|
|
3536
|
+
* Only activates after actual movement (dx/dy != 0) to avoid showing on click. */
|
|
3537
|
+
getSnapResult: () => {
|
|
3538
|
+
const c = ctx();
|
|
3539
|
+
if (c.canvasState !== "dragging" || !c.enableSnapLines) return null;
|
|
3540
|
+
if (c.selection.size === 0) return null;
|
|
3541
|
+
const nodesMoved = c.graph.nodes.some((n) => c.selection.has(n.id) && ((n.dx ?? 0) !== 0 || (n.dy ?? 0) !== 0));
|
|
3542
|
+
const edgesMoved = c.graph.edges.some((e) => c.selection.has(e.id) && e.label && ((e.label.dx ?? 0) !== 0 || (e.label.dy ?? 0) !== 0));
|
|
3543
|
+
if (!nodesMoved && !edgesMoved) return null;
|
|
3544
|
+
const selBounds = getSelectionSnapBounds(c);
|
|
3545
|
+
if (!selBounds) return null;
|
|
3546
|
+
const otherRects = getSnapTargetRects(c, c.selection, selBounds);
|
|
3547
|
+
if (otherRects.length === 0) return null;
|
|
3548
|
+
return getSnapLines(selBounds, otherRects, {
|
|
3549
|
+
threshold: c.snapThreshold / c.viewport.zoom,
|
|
3550
|
+
enableSpacing: c.enableSpacingGuides
|
|
3551
|
+
});
|
|
3552
|
+
},
|
|
3553
|
+
/** Get in-progress resize bounds, or null if not resizing */
|
|
3554
|
+
getResizeBounds: () => {
|
|
3555
|
+
return getResizeBoundsFromContext(ctx());
|
|
3556
|
+
},
|
|
3557
|
+
subscribe
|
|
3558
|
+
};
|
|
3559
|
+
}
|
|
3560
|
+
function getResizeBoundsFromContext(c) {
|
|
3561
|
+
if (!c.resizeCorners || !c.resizingEntityId) return null;
|
|
3562
|
+
const [a, b] = c.resizeCorners;
|
|
3563
|
+
return {
|
|
3564
|
+
x: Math.min(a.x, b.x),
|
|
3565
|
+
y: Math.min(a.y, b.y),
|
|
3566
|
+
width: Math.max(10, Math.abs(b.x - a.x)),
|
|
3567
|
+
height: Math.max(10, Math.abs(b.y - a.y))
|
|
3568
|
+
};
|
|
3569
|
+
}
|
|
3570
|
+
function boundsForPoints(points) {
|
|
3571
|
+
if (points.length === 0) return null;
|
|
3572
|
+
let minX = Infinity;
|
|
3573
|
+
let minY = Infinity;
|
|
3574
|
+
let maxX = -Infinity;
|
|
3575
|
+
let maxY = -Infinity;
|
|
3576
|
+
for (const point of points) {
|
|
3577
|
+
minX = Math.min(minX, point.x);
|
|
3578
|
+
minY = Math.min(minY, point.y);
|
|
3579
|
+
maxX = Math.max(maxX, point.x);
|
|
3580
|
+
maxY = Math.max(maxY, point.y);
|
|
3581
|
+
}
|
|
3582
|
+
if (!isFinite(minX)) return null;
|
|
3583
|
+
return {
|
|
3584
|
+
x: minX,
|
|
3585
|
+
y: minY,
|
|
3586
|
+
width: maxX - minX,
|
|
3587
|
+
height: maxY - minY
|
|
3588
|
+
};
|
|
3589
|
+
}
|
|
3590
|
+
function closestPortPosition(node, target) {
|
|
3591
|
+
if (!node.ports?.length) {
|
|
3592
|
+
const b = resolvedBounds(node);
|
|
3593
|
+
return {
|
|
3594
|
+
x: b.x + b.width / 2,
|
|
3595
|
+
y: b.y + b.height / 2
|
|
3596
|
+
};
|
|
3597
|
+
}
|
|
3598
|
+
const np = resolvedPosition(node);
|
|
3599
|
+
let best = {
|
|
3600
|
+
x: np.x,
|
|
3601
|
+
y: np.y
|
|
3602
|
+
};
|
|
3603
|
+
let bestDist = Infinity;
|
|
3604
|
+
for (const port of node.ports) {
|
|
3605
|
+
const px = np.x + port.x + port.width / 2;
|
|
3606
|
+
const py = np.y + port.y + port.height / 2;
|
|
3607
|
+
const d = (px - target.x) ** 2 + (py - target.y) ** 2;
|
|
3608
|
+
if (d < bestDist) {
|
|
3609
|
+
bestDist = d;
|
|
3610
|
+
best = {
|
|
3611
|
+
x: px,
|
|
3612
|
+
y: py
|
|
3613
|
+
};
|
|
3614
|
+
}
|
|
3615
|
+
}
|
|
3616
|
+
return best;
|
|
3617
|
+
}
|
|
3618
|
+
/**
|
|
3619
|
+
* Resolve label anchor position. Returns the TOP-LEFT corner of the label.
|
|
3620
|
+
* For 'fixed': label.x/y IS the top-left (like a node).
|
|
3621
|
+
* For number: interpolates between source/target centers, then offsets to top-left.
|
|
3622
|
+
* For function: calls the function, then offsets to top-left.
|
|
3623
|
+
*/
|
|
3624
|
+
function resolveLabelAnchor(anchor, label, sourceBounds, targetBounds) {
|
|
3625
|
+
if (anchor === "fixed") return {
|
|
3626
|
+
x: label.x,
|
|
3627
|
+
y: label.y
|
|
3628
|
+
};
|
|
3629
|
+
if (typeof anchor === "function") {
|
|
3630
|
+
const center = anchor(sourceBounds, targetBounds);
|
|
3631
|
+
return {
|
|
3632
|
+
x: center.x - label.width / 2,
|
|
3633
|
+
y: center.y - label.height / 2
|
|
3634
|
+
};
|
|
3635
|
+
}
|
|
3636
|
+
const t = anchor;
|
|
3637
|
+
const srcCx = sourceBounds.x + sourceBounds.width / 2;
|
|
3638
|
+
const srcCy = sourceBounds.y + sourceBounds.height / 2;
|
|
3639
|
+
const tgtCx = targetBounds.x + targetBounds.width / 2;
|
|
3640
|
+
const tgtCy = targetBounds.y + targetBounds.height / 2;
|
|
3641
|
+
return {
|
|
3642
|
+
x: srcCx + (tgtCx - srcCx) * t - label.width / 2,
|
|
3643
|
+
y: srcCy + (tgtCy - srcCy) * t - label.height / 2
|
|
3644
|
+
};
|
|
3645
|
+
}
|
|
3646
|
+
//#endregion
|
|
3647
|
+
//#region src/createFlow.ts
|
|
3648
|
+
/**
|
|
3649
|
+
* The undoRedo store extension keeps `past`/`future` history on the snapshot
|
|
3650
|
+
* at runtime without exposing them in its public types — read them defensively.
|
|
3651
|
+
*/
|
|
3652
|
+
function getHistory(store) {
|
|
3653
|
+
return store.getSnapshot();
|
|
3654
|
+
}
|
|
3655
|
+
function createFlow(options = {}) {
|
|
3656
|
+
const store = createFlowStore(options.graph, options.store);
|
|
3657
|
+
const engine = createFlowEngine(store, options.engine);
|
|
3658
|
+
const cleanup = /* @__PURE__ */ new Set();
|
|
3659
|
+
if (options.onConnectionEnd) {
|
|
3660
|
+
const subscription = store.on("connectionEnded", options.onConnectionEnd);
|
|
3661
|
+
cleanup.add(() => subscription.unsubscribe());
|
|
3662
|
+
}
|
|
3663
|
+
function destroy() {
|
|
3664
|
+
for (const dispose of cleanup) dispose();
|
|
3665
|
+
cleanup.clear();
|
|
3666
|
+
}
|
|
3667
|
+
async function runLayout(layout = options.layout ?? void 0) {
|
|
3668
|
+
if (!layout) throw new Error("runLayout() was called without a layout algorithm. Pass one to createFlow({ layout }) or call flow.runLayout(myLayout) directly.");
|
|
3669
|
+
const positions = await layout(store.getSnapshot().context.graph);
|
|
3670
|
+
store.trigger.applyLayoutResult({ positions });
|
|
3671
|
+
}
|
|
3672
|
+
return {
|
|
3673
|
+
store,
|
|
3674
|
+
engine,
|
|
3675
|
+
trigger: store.trigger,
|
|
3676
|
+
on: store.on,
|
|
3677
|
+
destroy,
|
|
3678
|
+
getContext: engine.getContext,
|
|
3679
|
+
subscribe: engine.subscribe,
|
|
3680
|
+
getNode: engine.getNode,
|
|
3681
|
+
getEdge: engine.getEdge,
|
|
3682
|
+
getEntity: engine.getEntity,
|
|
3683
|
+
getAddressable: engine.getAddressable,
|
|
3684
|
+
getOwnerEntity: engine.getOwnerEntity,
|
|
3685
|
+
getBounds: engine.getBounds,
|
|
3686
|
+
screenToCanvas: engine.screenToCanvas,
|
|
3687
|
+
canvasToScreen: engine.canvasToScreen,
|
|
3688
|
+
getAllEdgePaths: engine.getAllEdgePaths,
|
|
3689
|
+
getGraphBounds: engine.getGraphBounds,
|
|
3690
|
+
getSelectionBounds: engine.getSelectionBounds,
|
|
3691
|
+
getRenderableEntities: engine.getRenderableEntities,
|
|
3692
|
+
runLayout,
|
|
3693
|
+
undo: () => store.trigger.undo(),
|
|
3694
|
+
redo: () => store.trigger.redo(),
|
|
3695
|
+
canUndo: () => (getHistory(store).past?.length ?? 0) > 0,
|
|
3696
|
+
canRedo: () => (getHistory(store).future?.length ?? 0) > 0
|
|
3697
|
+
};
|
|
3698
|
+
}
|
|
3699
|
+
//#endregion
|
|
3700
|
+
//#region src/factories.ts
|
|
3701
|
+
function createNode(opts) {
|
|
3702
|
+
return {
|
|
3703
|
+
type: "node",
|
|
3704
|
+
x: 0,
|
|
3705
|
+
y: 0,
|
|
3706
|
+
width: 150,
|
|
3707
|
+
height: 50,
|
|
3708
|
+
data: {},
|
|
3709
|
+
...opts
|
|
3710
|
+
};
|
|
3711
|
+
}
|
|
3712
|
+
function createEdge(opts) {
|
|
3713
|
+
const { label, ...rest } = opts;
|
|
3714
|
+
return {
|
|
3715
|
+
type: "edge",
|
|
3716
|
+
x: 0,
|
|
3717
|
+
y: 0,
|
|
3718
|
+
width: 0,
|
|
3719
|
+
height: 0,
|
|
3720
|
+
data: {},
|
|
3721
|
+
...rest,
|
|
3722
|
+
label: label ? {
|
|
3723
|
+
x: 0,
|
|
3724
|
+
y: 0,
|
|
3725
|
+
data: {},
|
|
3726
|
+
...label
|
|
3727
|
+
} : void 0
|
|
3728
|
+
};
|
|
3729
|
+
}
|
|
3730
|
+
function createGraph(opts) {
|
|
3731
|
+
return {
|
|
3732
|
+
direction: "down",
|
|
3733
|
+
...opts
|
|
3734
|
+
};
|
|
3735
|
+
}
|
|
3736
|
+
function createPort(opts) {
|
|
3737
|
+
return {
|
|
3738
|
+
x: 0,
|
|
3739
|
+
y: 0,
|
|
3740
|
+
width: 12,
|
|
3741
|
+
height: 12,
|
|
3742
|
+
data: {},
|
|
3743
|
+
...opts
|
|
3744
|
+
};
|
|
3745
|
+
}
|
|
3746
|
+
//#endregion
|
|
3747
|
+
//#region src/connectionPreview.ts
|
|
3748
|
+
const CONNECTION_PREVIEW_NODE_ID = "flow:connection-preview-node";
|
|
3749
|
+
const CONNECTION_PREVIEW_EDGE_ID = "flow:connection-preview-edge";
|
|
3750
|
+
function createConnectionPreviewGraph(graph, connection, options = {}) {
|
|
3751
|
+
if (!connection?.active) return graph;
|
|
3752
|
+
const nodeId = options.nodeId ?? "flow:connection-preview-node";
|
|
3753
|
+
const edgeId = options.edgeId ?? "flow:connection-preview-edge";
|
|
3754
|
+
const nodeSize = options.nodeSize ?? 2;
|
|
3755
|
+
const targetId = connection.targetId ?? nodeId;
|
|
3756
|
+
const nodes = connection.targetId ? [...graph.nodes] : [...graph.nodes, createPreviewNode(nodeId, connection.pointerPosition, nodeSize)];
|
|
3757
|
+
const edges = [...graph.edges, createEdge({
|
|
3758
|
+
id: edgeId,
|
|
3759
|
+
sourceId: connection.sourceId,
|
|
3760
|
+
targetId,
|
|
3761
|
+
sourcePort: connection.sourcePort ?? void 0,
|
|
3762
|
+
targetPort: connection.targetPort ?? void 0,
|
|
3763
|
+
selectable: false,
|
|
3764
|
+
draggable: false,
|
|
3765
|
+
deletable: false,
|
|
3766
|
+
data: {
|
|
3767
|
+
temp: { kind: "connection-preview" },
|
|
3768
|
+
isValid: connection.isValid
|
|
3769
|
+
}
|
|
3770
|
+
})];
|
|
3771
|
+
return {
|
|
3772
|
+
...graph,
|
|
3773
|
+
nodes,
|
|
3774
|
+
edges
|
|
3775
|
+
};
|
|
3776
|
+
}
|
|
3777
|
+
function createPreviewNode(id, point, size) {
|
|
3778
|
+
return createNode({
|
|
3779
|
+
id,
|
|
3780
|
+
x: point.x - size / 2,
|
|
3781
|
+
y: point.y - size / 2,
|
|
3782
|
+
width: size,
|
|
3783
|
+
height: size,
|
|
3784
|
+
selectable: false,
|
|
3785
|
+
draggable: false,
|
|
3786
|
+
deletable: false,
|
|
3787
|
+
data: { temp: { kind: "connection-preview" } }
|
|
3788
|
+
});
|
|
3789
|
+
}
|
|
3790
|
+
//#endregion
|
|
3791
|
+
//#region src/toolbar.ts
|
|
3792
|
+
function getToolbarPlacement(input) {
|
|
3793
|
+
const offset = input.offset ?? 10;
|
|
3794
|
+
const padding = input.padding ?? 8;
|
|
3795
|
+
const { toolbarWidth: tw, toolbarHeight: th, anchor } = input;
|
|
3796
|
+
const topPlacementTop = anchor.y - offset - th;
|
|
3797
|
+
const bottomPlacementTop = anchor.y + anchor.height + offset;
|
|
3798
|
+
const topFits = topPlacementTop >= padding;
|
|
3799
|
+
const bottomFits = bottomPlacementTop + th <= input.viewportHeight - padding;
|
|
3800
|
+
let side;
|
|
3801
|
+
if (topFits) side = "top";
|
|
3802
|
+
else if (bottomFits) side = "bottom";
|
|
3803
|
+
else side = anchor.y >= input.viewportHeight - (anchor.y + anchor.height) ? "top" : "bottom";
|
|
3804
|
+
let top = side === "top" ? topPlacementTop : bottomPlacementTop;
|
|
3805
|
+
const maxTop = Math.max(input.viewportHeight - padding - th, padding);
|
|
3806
|
+
top = Math.min(Math.max(top, padding), maxTop);
|
|
3807
|
+
let left = anchor.x + anchor.width / 2 - tw / 2;
|
|
3808
|
+
const maxLeft = Math.max(input.viewportWidth - padding - tw, padding);
|
|
3809
|
+
left = Math.min(Math.max(left, padding), maxLeft);
|
|
3810
|
+
return {
|
|
3811
|
+
left,
|
|
3812
|
+
top,
|
|
3813
|
+
side
|
|
3814
|
+
};
|
|
3815
|
+
}
|
|
3816
|
+
//#endregion
|
|
3817
|
+
//#region src/minimap.ts
|
|
3818
|
+
function getMinimapProjection(options) {
|
|
3819
|
+
const padding = options.padding ?? .1;
|
|
3820
|
+
const content = options.contentBounds;
|
|
3821
|
+
const visible = getVisibleBounds(options.viewport, options.viewportSize);
|
|
3822
|
+
const padX = content.width * padding;
|
|
3823
|
+
const padY = content.height * padding;
|
|
3824
|
+
let bounds = {
|
|
3825
|
+
x: content.x - padX,
|
|
3826
|
+
y: content.y - padY,
|
|
3827
|
+
width: content.width + padX * 2,
|
|
3828
|
+
height: content.height + padY * 2
|
|
3829
|
+
};
|
|
3830
|
+
if (visible.width > 0 && visible.height > 0) bounds = unionRect(bounds, visible);
|
|
3831
|
+
if (bounds.width <= 0 || bounds.height <= 0) bounds = {
|
|
3832
|
+
x: bounds.x,
|
|
3833
|
+
y: bounds.y,
|
|
3834
|
+
width: 1,
|
|
3835
|
+
height: 1
|
|
3836
|
+
};
|
|
3837
|
+
const scale = Math.min(options.width / bounds.width, options.height / bounds.height);
|
|
3838
|
+
const offsetX = (options.width - bounds.width * scale) / 2;
|
|
3839
|
+
const offsetY = (options.height - bounds.height * scale) / 2;
|
|
3840
|
+
const toMinimap = (point) => ({
|
|
3841
|
+
x: (point.x - bounds.x) * scale + offsetX,
|
|
3842
|
+
y: (point.y - bounds.y) * scale + offsetY
|
|
3843
|
+
});
|
|
3844
|
+
const toCanvas = (point) => ({
|
|
3845
|
+
x: (point.x - offsetX) / scale + bounds.x,
|
|
3846
|
+
y: (point.y - offsetY) / scale + bounds.y
|
|
3847
|
+
});
|
|
3848
|
+
const rectToMinimap = (rect) => {
|
|
3849
|
+
const origin = toMinimap(rect);
|
|
3850
|
+
return {
|
|
3851
|
+
x: origin.x,
|
|
3852
|
+
y: origin.y,
|
|
3853
|
+
width: rect.width * scale,
|
|
3854
|
+
height: rect.height * scale
|
|
3855
|
+
};
|
|
3856
|
+
};
|
|
3857
|
+
return {
|
|
3858
|
+
scale,
|
|
3859
|
+
bounds,
|
|
3860
|
+
viewportRect: rectToMinimap(visible),
|
|
3861
|
+
toMinimap,
|
|
3862
|
+
toCanvas,
|
|
3863
|
+
rectToMinimap
|
|
3864
|
+
};
|
|
3865
|
+
}
|
|
3866
|
+
//#endregion
|
|
3867
|
+
//#region src/input/modes.ts
|
|
3868
|
+
const preventDefault$1 = { type: "preventDefault" };
|
|
3869
|
+
function distance(a, b) {
|
|
3870
|
+
return Math.hypot(b.x - a.x, b.y - a.y);
|
|
3871
|
+
}
|
|
3872
|
+
function isMulti(event) {
|
|
3873
|
+
return event.modifiers.shift || event.modifiers.meta || event.modifiers.ctrl;
|
|
3874
|
+
}
|
|
3875
|
+
function keyboardMode(options = {}) {
|
|
3876
|
+
const allowDelete = options.allowDelete ?? true;
|
|
3877
|
+
const allowUndo = options.allowUndo ?? true;
|
|
3878
|
+
return {
|
|
3879
|
+
id: "keyboard",
|
|
3880
|
+
priority: 100,
|
|
3881
|
+
start(event, { context }) {
|
|
3882
|
+
if (event.kind !== "key.down") return null;
|
|
3883
|
+
if (event.editableTarget) return null;
|
|
3884
|
+
if (event.key === " ") return { commands: [preventDefault$1] };
|
|
3885
|
+
if (event.key === "Escape") return { commands: context.canvasState === "idle" ? [
|
|
3886
|
+
trigger("cancelConnection"),
|
|
3887
|
+
trigger("clearSelection"),
|
|
3888
|
+
trigger("clearFocus")
|
|
3889
|
+
] : [trigger("cancelConnection")] };
|
|
3890
|
+
const mod = event.modifiers.meta || event.modifiers.ctrl;
|
|
3891
|
+
if (allowUndo && mod && event.key.toLowerCase() === "z") return { commands: [preventDefault$1, event.modifiers.shift ? trigger("redo") : trigger("undo")] };
|
|
3892
|
+
if (allowDelete && (event.key === "Delete" || event.key === "Backspace")) {
|
|
3893
|
+
if (context.selection.size === 0) return null;
|
|
3894
|
+
return { commands: [preventDefault$1, trigger("deleteSelected")] };
|
|
3895
|
+
}
|
|
3896
|
+
return null;
|
|
3897
|
+
}
|
|
3898
|
+
};
|
|
3899
|
+
}
|
|
3900
|
+
const ARROW_DIRECTIONS = {
|
|
3901
|
+
ArrowLeft: {
|
|
3902
|
+
x: -1,
|
|
3903
|
+
y: 0
|
|
3904
|
+
},
|
|
3905
|
+
ArrowRight: {
|
|
3906
|
+
x: 1,
|
|
3907
|
+
y: 0
|
|
3908
|
+
},
|
|
3909
|
+
ArrowUp: {
|
|
3910
|
+
x: 0,
|
|
3911
|
+
y: -1
|
|
3912
|
+
},
|
|
3913
|
+
ArrowDown: {
|
|
3914
|
+
x: 0,
|
|
3915
|
+
y: 1
|
|
3916
|
+
}
|
|
3917
|
+
};
|
|
3918
|
+
function keyboardNavMode(options = {}) {
|
|
3919
|
+
const allowMove = options.allowMove ?? true;
|
|
3920
|
+
const step = options.step ?? 5;
|
|
3921
|
+
const shiftStep = options.shiftStep ?? 20;
|
|
3922
|
+
return {
|
|
3923
|
+
id: "keyboard-nav",
|
|
3924
|
+
priority: 95,
|
|
3925
|
+
start(event, { context }) {
|
|
3926
|
+
if (event.kind !== "key.down" || event.editableTarget) return null;
|
|
3927
|
+
if (event.key === "Enter" && context.focusedEntityId) return { commands: [preventDefault$1, trigger("select", {
|
|
3928
|
+
ids: [context.focusedEntityId],
|
|
3929
|
+
mode: event.modifiers.shift ? "multi" : "single"
|
|
3930
|
+
})] };
|
|
3931
|
+
if (!allowMove) return null;
|
|
3932
|
+
const direction = ARROW_DIRECTIONS[event.key];
|
|
3933
|
+
if (!direction || context.selection.size === 0) return null;
|
|
3934
|
+
return { commands: [
|
|
3935
|
+
preventDefault$1,
|
|
3936
|
+
trigger("moveNodes", {
|
|
3937
|
+
direction,
|
|
3938
|
+
factor: event.modifiers.shift ? shiftStep : step
|
|
3939
|
+
}),
|
|
3940
|
+
trigger("commitDrag")
|
|
3941
|
+
] };
|
|
3942
|
+
}
|
|
3943
|
+
};
|
|
3944
|
+
}
|
|
3945
|
+
function wheelMode() {
|
|
3946
|
+
return {
|
|
3947
|
+
id: "wheel",
|
|
3948
|
+
priority: 90,
|
|
3949
|
+
start(event, { context }) {
|
|
3950
|
+
if (event.kind !== "wheel") return null;
|
|
3951
|
+
if (isTrackpadPinch({
|
|
3952
|
+
ctrlKey: event.modifiers.ctrl,
|
|
3953
|
+
metaKey: event.modifiers.meta
|
|
3954
|
+
})) return { commands: [preventDefault$1, trigger("zoomTo", {
|
|
3955
|
+
zoom: context.viewport.zoom * Math.pow(2, wheelZoomDelta({
|
|
3956
|
+
deltaY: event.deltaY,
|
|
3957
|
+
deltaMode: event.deltaMode,
|
|
3958
|
+
ctrlKey: event.modifiers.ctrl
|
|
3959
|
+
})),
|
|
3960
|
+
center: event.screenPoint
|
|
3961
|
+
})] };
|
|
3962
|
+
return { commands: [preventDefault$1, trigger("panBy", normalizeWheelPan({
|
|
3963
|
+
deltaX: event.deltaX,
|
|
3964
|
+
deltaY: event.deltaY,
|
|
3965
|
+
deltaMode: event.deltaMode
|
|
3966
|
+
}))] };
|
|
3967
|
+
}
|
|
3968
|
+
};
|
|
3969
|
+
}
|
|
3970
|
+
function panMode() {
|
|
3971
|
+
return {
|
|
3972
|
+
id: "pan",
|
|
3973
|
+
priority: 80,
|
|
3974
|
+
start(event, { input }) {
|
|
3975
|
+
if (event.kind !== "pointer.down") return null;
|
|
3976
|
+
const middle = event.button === 1;
|
|
3977
|
+
const space = input.spaceHeld && event.button === 0;
|
|
3978
|
+
if (!middle && !space) return null;
|
|
3979
|
+
return {
|
|
3980
|
+
commands: [trigger("panStart", event.screenPoint)],
|
|
3981
|
+
session: panSession()
|
|
3982
|
+
};
|
|
3983
|
+
}
|
|
3984
|
+
};
|
|
3985
|
+
}
|
|
3986
|
+
function panSession() {
|
|
3987
|
+
return {
|
|
3988
|
+
update(event) {
|
|
3989
|
+
if (event.kind === "pointer.move") return { commands: [trigger("pan", event.screenPoint)] };
|
|
3990
|
+
if (event.kind === "pointer.up" || event.kind === "pointer.cancel") return {
|
|
3991
|
+
commands: [trigger("panEnd")],
|
|
3992
|
+
status: "end"
|
|
3993
|
+
};
|
|
3994
|
+
return {};
|
|
3995
|
+
},
|
|
3996
|
+
cancel() {
|
|
3997
|
+
return [trigger("panEnd")];
|
|
3998
|
+
}
|
|
3999
|
+
};
|
|
4000
|
+
}
|
|
4001
|
+
const REPARENT_HIGHLIGHT_KIND = "drop-target";
|
|
4002
|
+
function entityPressMode(options = {}) {
|
|
4003
|
+
const allowDrag = options.drag ?? true;
|
|
4004
|
+
const reparent = options.reparent === true ? {} : typeof options.reparent === "object" ? options.reparent : null;
|
|
4005
|
+
return {
|
|
4006
|
+
id: "entity-press",
|
|
4007
|
+
priority: 50,
|
|
4008
|
+
start(event, { context, engine }) {
|
|
4009
|
+
if (event.kind !== "pointer.down" || event.button !== 0) return null;
|
|
4010
|
+
if (event.target.kind !== "entity") return null;
|
|
4011
|
+
const id = event.target.id;
|
|
4012
|
+
const entity = engine.getEntity(id);
|
|
4013
|
+
if (!entity) return null;
|
|
4014
|
+
const config = engine.resolveEntityConfig(entity);
|
|
4015
|
+
const commands = [];
|
|
4016
|
+
if (config.selectable) {
|
|
4017
|
+
const selection = context.selection;
|
|
4018
|
+
if (!(selection.size > 1 && selection.has(id))) commands.push(trigger("select", {
|
|
4019
|
+
ids: [id],
|
|
4020
|
+
mode: isMulti(event) ? "multi" : "single"
|
|
4021
|
+
}));
|
|
4022
|
+
}
|
|
4023
|
+
const draggablePart = entity.type === "node" || event.target.part === "label";
|
|
4024
|
+
const handleAllows = !event.target.entityHasDragHandle || event.target.pressedDragHandle === true;
|
|
4025
|
+
if (!allowDrag || !draggablePart || !handleAllows || !config.draggable) return { commands };
|
|
4026
|
+
return {
|
|
4027
|
+
commands,
|
|
4028
|
+
session: dragSession(id, event, entity.type === "node" ? reparent : null)
|
|
4029
|
+
};
|
|
4030
|
+
}
|
|
4031
|
+
};
|
|
4032
|
+
}
|
|
4033
|
+
function dragSession(id, down, reparent) {
|
|
4034
|
+
const transactionId = `drag:${id}:${down.pointerId}:${Date.now()}`;
|
|
4035
|
+
let dragging = false;
|
|
4036
|
+
let baseOffset = {
|
|
4037
|
+
x: 0,
|
|
4038
|
+
y: 0
|
|
4039
|
+
};
|
|
4040
|
+
let candidateId = null;
|
|
4041
|
+
let candidateOverlap = 0;
|
|
4042
|
+
let candidateSince = 0;
|
|
4043
|
+
const minOverlap = reparent?.minOverlap ?? .5;
|
|
4044
|
+
const unparentModifier = reparent?.unparentModifier === void 0 ? "alt" : reparent.unparentModifier;
|
|
4045
|
+
/**
|
|
4046
|
+
* Currently applied drag offset of the primary entity. The session anchors
|
|
4047
|
+
* requests to the virtual pointer (`base + pointerTravel`) and asks the
|
|
4048
|
+
* store for the difference — so store-side clamping (extents, fixed-group
|
|
4049
|
+
* child areas) is idempotent against pointer position: overshoot stays
|
|
4050
|
+
* virtual and the entity doesn't move again until the pointer returns.
|
|
4051
|
+
*/
|
|
4052
|
+
function appliedOffset(ctx) {
|
|
4053
|
+
const node = ctx.engine.getNode(id);
|
|
4054
|
+
if (node) return {
|
|
4055
|
+
x: node.dx ?? 0,
|
|
4056
|
+
y: node.dy ?? 0
|
|
4057
|
+
};
|
|
4058
|
+
const edge = ctx.engine.getEdge(id);
|
|
4059
|
+
if (edge?.label) return {
|
|
4060
|
+
x: edge.label.dx ?? 0,
|
|
4061
|
+
y: edge.label.dy ?? 0
|
|
4062
|
+
};
|
|
4063
|
+
return {
|
|
4064
|
+
x: 0,
|
|
4065
|
+
y: 0
|
|
4066
|
+
};
|
|
4067
|
+
}
|
|
4068
|
+
/** Top-level moving nodes + the full excluded (moving) subtree. */
|
|
4069
|
+
function movingInfo(ctx) {
|
|
4070
|
+
const { context, engine } = ctx;
|
|
4071
|
+
const nodes = context.graph.nodes;
|
|
4072
|
+
const nodeIndex = indexNodes(nodes);
|
|
4073
|
+
const childrenIndex = indexChildren(nodes);
|
|
4074
|
+
const movingIds = /* @__PURE__ */ new Set();
|
|
4075
|
+
for (const selectedId of context.selection) {
|
|
4076
|
+
const node = nodeIndex.get(selectedId);
|
|
4077
|
+
if (node && node.draggable !== false) movingIds.add(selectedId);
|
|
4078
|
+
}
|
|
4079
|
+
const topLevel = [];
|
|
4080
|
+
for (const movingId of movingIds) {
|
|
4081
|
+
let ancestor = nodeIndex.get(movingId)?.parentId ?? null;
|
|
4082
|
+
let nested = false;
|
|
4083
|
+
const seen = /* @__PURE__ */ new Set();
|
|
4084
|
+
while (ancestor && !seen.has(ancestor)) {
|
|
4085
|
+
if (movingIds.has(ancestor)) {
|
|
4086
|
+
nested = true;
|
|
4087
|
+
break;
|
|
4088
|
+
}
|
|
4089
|
+
seen.add(ancestor);
|
|
4090
|
+
ancestor = nodeIndex.get(ancestor)?.parentId ?? null;
|
|
4091
|
+
}
|
|
4092
|
+
if (!nested) topLevel.push(nodeIndex.get(movingId));
|
|
4093
|
+
}
|
|
4094
|
+
const exclude = new Set(movingIds);
|
|
4095
|
+
const queue = [...movingIds];
|
|
4096
|
+
while (queue.length > 0) {
|
|
4097
|
+
const current = queue.pop();
|
|
4098
|
+
for (const child of childrenIndex.get(current) ?? []) if (!exclude.has(child.id)) {
|
|
4099
|
+
exclude.add(child.id);
|
|
4100
|
+
queue.push(child.id);
|
|
4101
|
+
}
|
|
4102
|
+
}
|
|
4103
|
+
let bounds = null;
|
|
4104
|
+
for (const node of topLevel) {
|
|
4105
|
+
const b = engine.resolvedBounds(node.id);
|
|
4106
|
+
bounds = bounds ? {
|
|
4107
|
+
x: Math.min(bounds.x, b.x),
|
|
4108
|
+
y: Math.min(bounds.y, b.y),
|
|
4109
|
+
width: Math.max(bounds.x + bounds.width, b.x + b.width) - Math.min(bounds.x, b.x),
|
|
4110
|
+
height: Math.max(bounds.y + bounds.height, b.y + b.height) - Math.min(bounds.y, b.y)
|
|
4111
|
+
} : { ...b };
|
|
4112
|
+
}
|
|
4113
|
+
return {
|
|
4114
|
+
topLevel,
|
|
4115
|
+
exclude,
|
|
4116
|
+
bounds
|
|
4117
|
+
};
|
|
4118
|
+
}
|
|
4119
|
+
function trackCandidate(ctx, pending) {
|
|
4120
|
+
const { exclude, bounds } = movingInfo(ctx);
|
|
4121
|
+
const projected = bounds ? {
|
|
4122
|
+
...bounds,
|
|
4123
|
+
x: bounds.x + pending.x,
|
|
4124
|
+
y: bounds.y + pending.y
|
|
4125
|
+
} : null;
|
|
4126
|
+
const found = projected ? ctx.engine.getGroupDropTarget(projected, { exclude }) : null;
|
|
4127
|
+
const nextId = found && found.overlap >= minOverlap ? found.id : null;
|
|
4128
|
+
candidateOverlap = found?.overlap ?? 0;
|
|
4129
|
+
if (nextId === candidateId) return [];
|
|
4130
|
+
const commands = [];
|
|
4131
|
+
if (candidateId) commands.push(trigger("removeHighlights", {
|
|
4132
|
+
kind: REPARENT_HIGHLIGHT_KIND,
|
|
4133
|
+
ids: [candidateId]
|
|
4134
|
+
}));
|
|
4135
|
+
if (nextId) commands.push(trigger("addHighlights", {
|
|
4136
|
+
kind: REPARENT_HIGHLIGHT_KIND,
|
|
4137
|
+
entries: [{ id: nextId }]
|
|
4138
|
+
}));
|
|
4139
|
+
candidateId = nextId;
|
|
4140
|
+
candidateSince = Date.now();
|
|
4141
|
+
return commands;
|
|
4142
|
+
}
|
|
4143
|
+
function decideAndApply(event, ctx) {
|
|
4144
|
+
const commands = [];
|
|
4145
|
+
if (candidateId) commands.push(trigger("removeHighlights", {
|
|
4146
|
+
kind: REPARENT_HIGHLIGHT_KIND,
|
|
4147
|
+
ids: [candidateId]
|
|
4148
|
+
}));
|
|
4149
|
+
if (event.kind !== "pointer.up" || !reparent) return commands;
|
|
4150
|
+
const { topLevel, exclude, bounds } = movingInfo(ctx);
|
|
4151
|
+
if (!bounds || topLevel.length === 0) return commands;
|
|
4152
|
+
const found = ctx.engine.getGroupDropTarget(bounds, { exclude });
|
|
4153
|
+
const finalId = found && found.overlap >= minOverlap ? found.id : null;
|
|
4154
|
+
const hoverMs = finalId && finalId === candidateId ? Date.now() - candidateSince : 0;
|
|
4155
|
+
candidateId = finalId;
|
|
4156
|
+
candidateOverlap = found?.overlap ?? 0;
|
|
4157
|
+
const drop = {
|
|
4158
|
+
nodes: topLevel,
|
|
4159
|
+
bounds,
|
|
4160
|
+
candidate: candidateId ? {
|
|
4161
|
+
id: candidateId,
|
|
4162
|
+
overlap: candidateOverlap
|
|
4163
|
+
} : null,
|
|
4164
|
+
hoverMs,
|
|
4165
|
+
modifiers: event.modifiers
|
|
4166
|
+
};
|
|
4167
|
+
let decision;
|
|
4168
|
+
if (reparent.decide) decision = reparent.decide(drop, ctx);
|
|
4169
|
+
else if (unparentModifier && event.modifiers[unparentModifier]) decision = null;
|
|
4170
|
+
else if (candidateId && hoverMs >= (reparent.delayMs ?? 0)) decision = candidateId;
|
|
4171
|
+
else if (!candidateId && (reparent.unparent ?? true)) decision = topLevel.some((node) => {
|
|
4172
|
+
if (!node.parentId) return false;
|
|
4173
|
+
const geometry = ctx.engine.getGroupGeometry(node.parentId);
|
|
4174
|
+
return geometry ? !rectsIntersect(bounds, geometry.childArea) : false;
|
|
4175
|
+
}) ? null : void 0;
|
|
4176
|
+
if (decision !== void 0) for (const node of topLevel) {
|
|
4177
|
+
if ((node.parentId ?? null) === decision || node.id === decision) continue;
|
|
4178
|
+
commands.push(trigger("reparentNode", {
|
|
4179
|
+
id: node.id,
|
|
4180
|
+
parentId: decision,
|
|
4181
|
+
transactionId
|
|
4182
|
+
}));
|
|
4183
|
+
}
|
|
4184
|
+
return commands;
|
|
4185
|
+
}
|
|
4186
|
+
return {
|
|
4187
|
+
update(event, ctx) {
|
|
4188
|
+
const { context } = ctx;
|
|
4189
|
+
if (event.kind === "pointer.move") {
|
|
4190
|
+
if (!dragging) {
|
|
4191
|
+
if (distance(down.screenPoint, event.screenPoint) < context.dragThreshold) return {};
|
|
4192
|
+
dragging = true;
|
|
4193
|
+
baseOffset = appliedOffset(ctx);
|
|
4194
|
+
}
|
|
4195
|
+
const applied = appliedOffset(ctx);
|
|
4196
|
+
const dx = baseOffset.x + (event.point.x - down.point.x) - applied.x;
|
|
4197
|
+
const dy = baseOffset.y + (event.point.y - down.point.y) - applied.y;
|
|
4198
|
+
return { commands: [
|
|
4199
|
+
...!context.draggedEntityId ? [trigger("dragStart", {
|
|
4200
|
+
id,
|
|
4201
|
+
transactionId
|
|
4202
|
+
})] : [],
|
|
4203
|
+
trigger("dragMove", {
|
|
4204
|
+
dx,
|
|
4205
|
+
dy
|
|
4206
|
+
}),
|
|
4207
|
+
...reparent ? trackCandidate(ctx, {
|
|
4208
|
+
x: dx,
|
|
4209
|
+
y: dy
|
|
4210
|
+
}) : []
|
|
4211
|
+
] };
|
|
4212
|
+
}
|
|
4213
|
+
if (event.kind === "pointer.up" || event.kind === "pointer.cancel") return {
|
|
4214
|
+
commands: dragging ? [
|
|
4215
|
+
trigger("dragEnd"),
|
|
4216
|
+
trigger("commitDrag"),
|
|
4217
|
+
...reparent ? decideAndApply(event, ctx) : []
|
|
4218
|
+
] : [],
|
|
4219
|
+
status: "end"
|
|
4220
|
+
};
|
|
4221
|
+
return {};
|
|
4222
|
+
},
|
|
4223
|
+
cancel() {
|
|
4224
|
+
const commands = dragging ? [trigger("dragEnd"), trigger("commitDrag")] : [];
|
|
4225
|
+
if (candidateId) commands.push(trigger("removeHighlights", {
|
|
4226
|
+
kind: REPARENT_HIGHLIGHT_KIND,
|
|
4227
|
+
ids: [candidateId]
|
|
4228
|
+
}));
|
|
4229
|
+
return commands;
|
|
4230
|
+
}
|
|
4231
|
+
};
|
|
4232
|
+
}
|
|
4233
|
+
function connectMode(options = {}) {
|
|
4234
|
+
return {
|
|
4235
|
+
id: "connect",
|
|
4236
|
+
priority: 60,
|
|
4237
|
+
start(event, { engine }) {
|
|
4238
|
+
if (event.kind !== "pointer.down" || event.button !== 0) return null;
|
|
4239
|
+
if (event.target.kind !== "port") return null;
|
|
4240
|
+
const { nodeId, portName } = event.target;
|
|
4241
|
+
const node = engine.getNode(nodeId);
|
|
4242
|
+
if (!node?.ports?.some((port) => port.name === portName)) return null;
|
|
4243
|
+
if (!engine.resolveEntityConfig(node).connectable) return null;
|
|
4244
|
+
return {
|
|
4245
|
+
commands: [trigger("connectionStart", {
|
|
4246
|
+
sourceId: nodeId,
|
|
4247
|
+
sourcePort: portName,
|
|
4248
|
+
position: event.point
|
|
4249
|
+
})],
|
|
4250
|
+
session: connectSession({
|
|
4251
|
+
nodeId,
|
|
4252
|
+
port: portName
|
|
4253
|
+
}, options)
|
|
4254
|
+
};
|
|
4255
|
+
}
|
|
4256
|
+
};
|
|
4257
|
+
}
|
|
4258
|
+
function defaultConnectionValidity(args, ctx) {
|
|
4259
|
+
const { source, target } = args;
|
|
4260
|
+
if (target.kind === "port") {
|
|
4261
|
+
if (target.nodeId === source.nodeId) return false;
|
|
4262
|
+
return ctx.engine.validateConnection({
|
|
4263
|
+
sourceId: source.nodeId,
|
|
4264
|
+
sourcePort: source.port,
|
|
4265
|
+
targetId: target.nodeId,
|
|
4266
|
+
targetPort: target.portName
|
|
4267
|
+
});
|
|
4268
|
+
}
|
|
4269
|
+
if (target.kind === "entity") {
|
|
4270
|
+
if (target.id === source.nodeId) return false;
|
|
4271
|
+
if (!ctx.engine.getNode(target.id)) return false;
|
|
4272
|
+
return ctx.engine.validateConnection({
|
|
4273
|
+
sourceId: source.nodeId,
|
|
4274
|
+
sourcePort: source.port,
|
|
4275
|
+
targetId: target.id,
|
|
4276
|
+
targetPort: null
|
|
4277
|
+
});
|
|
4278
|
+
}
|
|
4279
|
+
return false;
|
|
4280
|
+
}
|
|
4281
|
+
function connectEndTargetFor(target, point, ctx) {
|
|
4282
|
+
if (target.kind === "port") return {
|
|
4283
|
+
kind: "node",
|
|
4284
|
+
nodeId: target.nodeId,
|
|
4285
|
+
port: target.portName
|
|
4286
|
+
};
|
|
4287
|
+
if (target.kind === "entity") {
|
|
4288
|
+
const entity = ctx.engine.getEntity(target.id);
|
|
4289
|
+
if (entity?.type === "node") return {
|
|
4290
|
+
kind: "node",
|
|
4291
|
+
nodeId: target.id,
|
|
4292
|
+
port: null
|
|
4293
|
+
};
|
|
4294
|
+
if (entity?.type === "edge") return {
|
|
4295
|
+
kind: "edge",
|
|
4296
|
+
edgeId: target.id
|
|
4297
|
+
};
|
|
4298
|
+
}
|
|
4299
|
+
if (target.kind === "pane") return {
|
|
4300
|
+
kind: "canvas",
|
|
4301
|
+
position: point
|
|
4302
|
+
};
|
|
4303
|
+
return { kind: "none" };
|
|
4304
|
+
}
|
|
4305
|
+
function connectSession(source, options) {
|
|
4306
|
+
const isValid = options.isValid ?? defaultConnectionValidity;
|
|
4307
|
+
return {
|
|
4308
|
+
update(event, ctx) {
|
|
4309
|
+
if (!ctx.context.connection) return { status: "end" };
|
|
4310
|
+
if (event.kind === "pointer.move") {
|
|
4311
|
+
const target = event.target;
|
|
4312
|
+
let targetId = null;
|
|
4313
|
+
let targetPort = null;
|
|
4314
|
+
if (target.kind === "port") {
|
|
4315
|
+
targetId = target.nodeId;
|
|
4316
|
+
targetPort = target.portName;
|
|
4317
|
+
} else if (target.kind === "entity" && ctx.engine.getNode(target.id)) targetId = target.id;
|
|
4318
|
+
return { commands: [trigger("connectionMove", {
|
|
4319
|
+
position: event.point,
|
|
4320
|
+
targetId,
|
|
4321
|
+
targetPort,
|
|
4322
|
+
isValid: isValid({
|
|
4323
|
+
source,
|
|
4324
|
+
target,
|
|
4325
|
+
point: event.point
|
|
4326
|
+
}, ctx)
|
|
4327
|
+
})] };
|
|
4328
|
+
}
|
|
4329
|
+
if (event.kind === "pointer.up") return {
|
|
4330
|
+
commands: [trigger("connectionEnd", { target: connectEndTargetFor(event.target, event.point, ctx) })],
|
|
4331
|
+
status: "end"
|
|
4332
|
+
};
|
|
4333
|
+
if (event.kind === "pointer.cancel") return {
|
|
4334
|
+
commands: [trigger("cancelConnection")],
|
|
4335
|
+
status: "end"
|
|
4336
|
+
};
|
|
4337
|
+
return {};
|
|
4338
|
+
},
|
|
4339
|
+
cancel() {
|
|
4340
|
+
return [trigger("cancelConnection")];
|
|
4341
|
+
}
|
|
4342
|
+
};
|
|
4343
|
+
}
|
|
4344
|
+
function hoverMode() {
|
|
4345
|
+
return {
|
|
4346
|
+
id: "hover",
|
|
4347
|
+
priority: 5,
|
|
4348
|
+
start(event, { context }) {
|
|
4349
|
+
if (event.kind !== "pointer.move") return null;
|
|
4350
|
+
const target = event.target;
|
|
4351
|
+
const id = target.kind === "entity" ? target.id : target.kind === "port" ? target.nodeId : null;
|
|
4352
|
+
const current = context.hoveredEntityIds;
|
|
4353
|
+
if (id) {
|
|
4354
|
+
if (current.size === 1 && current.has(id)) return null;
|
|
4355
|
+
return { commands: [trigger("setHover", { ids: [id] })] };
|
|
4356
|
+
}
|
|
4357
|
+
if (current.size === 0) return null;
|
|
4358
|
+
return { commands: [trigger("clearHover")] };
|
|
4359
|
+
}
|
|
4360
|
+
};
|
|
4361
|
+
}
|
|
4362
|
+
function panePressMode() {
|
|
4363
|
+
return {
|
|
4364
|
+
id: "pane-press",
|
|
4365
|
+
priority: 10,
|
|
4366
|
+
start(event) {
|
|
4367
|
+
if (event.kind !== "pointer.down" || event.button !== 0) return null;
|
|
4368
|
+
if (event.target.kind !== "pane") return null;
|
|
4369
|
+
return { session: panePressSession(event) };
|
|
4370
|
+
}
|
|
4371
|
+
};
|
|
4372
|
+
}
|
|
4373
|
+
function panePressSession(down) {
|
|
4374
|
+
let panning = false;
|
|
4375
|
+
return {
|
|
4376
|
+
update(event, { context }) {
|
|
4377
|
+
if (event.kind === "pointer.move") {
|
|
4378
|
+
if (!panning) {
|
|
4379
|
+
if (distance(down.screenPoint, event.screenPoint) < context.dragThreshold) return {};
|
|
4380
|
+
panning = true;
|
|
4381
|
+
return { commands: [
|
|
4382
|
+
trigger("clearSelection"),
|
|
4383
|
+
trigger("panStart", down.screenPoint),
|
|
4384
|
+
trigger("pan", event.screenPoint)
|
|
4385
|
+
] };
|
|
4386
|
+
}
|
|
4387
|
+
return { commands: [trigger("pan", event.screenPoint)] };
|
|
4388
|
+
}
|
|
4389
|
+
if (event.kind === "pointer.up") return {
|
|
4390
|
+
commands: panning ? [trigger("panEnd")] : [trigger("clearSelection")],
|
|
4391
|
+
status: "end"
|
|
4392
|
+
};
|
|
4393
|
+
if (event.kind === "pointer.cancel") return {
|
|
4394
|
+
commands: panning ? [trigger("panEnd")] : [],
|
|
4395
|
+
status: "end"
|
|
4396
|
+
};
|
|
4397
|
+
return {};
|
|
4398
|
+
},
|
|
4399
|
+
cancel() {
|
|
4400
|
+
return panning ? [trigger("panEnd")] : [];
|
|
4401
|
+
}
|
|
4402
|
+
};
|
|
4403
|
+
}
|
|
4404
|
+
/** Interactive default: full editing gestures. */
|
|
4405
|
+
const defaultInputModes = [
|
|
4406
|
+
keyboardMode(),
|
|
4407
|
+
keyboardNavMode(),
|
|
4408
|
+
wheelMode(),
|
|
4409
|
+
panMode(),
|
|
4410
|
+
connectMode(),
|
|
4411
|
+
entityPressMode(),
|
|
4412
|
+
panePressMode(),
|
|
4413
|
+
hoverMode()
|
|
4414
|
+
];
|
|
4415
|
+
/** Readonly: select and navigate, no mutation gestures. */
|
|
4416
|
+
const readonlyInputModes = [
|
|
4417
|
+
keyboardMode({
|
|
4418
|
+
allowDelete: false,
|
|
4419
|
+
allowUndo: false
|
|
4420
|
+
}),
|
|
4421
|
+
keyboardNavMode({ allowMove: false }),
|
|
4422
|
+
wheelMode(),
|
|
4423
|
+
panMode(),
|
|
4424
|
+
entityPressMode({ drag: false }),
|
|
4425
|
+
panePressMode(),
|
|
4426
|
+
hoverMode()
|
|
4427
|
+
];
|
|
4428
|
+
//#endregion
|
|
4429
|
+
//#region src/input/index.ts
|
|
4430
|
+
/** Typed constructor for trigger commands. */
|
|
4431
|
+
function trigger(name, ...params) {
|
|
4432
|
+
return {
|
|
4433
|
+
type: "trigger",
|
|
4434
|
+
name,
|
|
4435
|
+
params: params[0]
|
|
4436
|
+
};
|
|
4437
|
+
}
|
|
4438
|
+
const preventDefault = { type: "preventDefault" };
|
|
4439
|
+
function createFlowInputRuntime(options) {
|
|
4440
|
+
const { store, engine } = options;
|
|
4441
|
+
const resolveModes = typeof options.modes === "function" ? options.modes : options.modes ? () => options.modes : defaultModesForContext;
|
|
4442
|
+
const input = { spaceHeld: false };
|
|
4443
|
+
let session = null;
|
|
4444
|
+
function modeContext() {
|
|
4445
|
+
return {
|
|
4446
|
+
context: store.getSnapshot().context,
|
|
4447
|
+
engine,
|
|
4448
|
+
input
|
|
4449
|
+
};
|
|
4450
|
+
}
|
|
4451
|
+
function apply(commands, effects) {
|
|
4452
|
+
if (!commands) return;
|
|
4453
|
+
for (const command of commands) if (command.type === "preventDefault") effects.preventDefault = true;
|
|
4454
|
+
else if (command.type === "stopPropagation") effects.stopPropagation = true;
|
|
4455
|
+
else {
|
|
4456
|
+
const fire = store.trigger[command.name];
|
|
4457
|
+
fire?.(command.params);
|
|
4458
|
+
}
|
|
4459
|
+
}
|
|
4460
|
+
function send(event) {
|
|
4461
|
+
const effects = {
|
|
4462
|
+
preventDefault: false,
|
|
4463
|
+
stopPropagation: false,
|
|
4464
|
+
sessionStarted: false,
|
|
4465
|
+
sessionEnded: false,
|
|
4466
|
+
handledBy: null
|
|
4467
|
+
};
|
|
4468
|
+
if (event.kind === "key.down" && event.key === " " && !event.editableTarget) input.spaceHeld = true;
|
|
4469
|
+
else if (event.kind === "key.up" && event.key === " ") input.spaceHeld = false;
|
|
4470
|
+
const isPointer = event.kind === "pointer.down" || event.kind === "pointer.move" || event.kind === "pointer.up" || event.kind === "pointer.cancel";
|
|
4471
|
+
if (session && isPointer) {
|
|
4472
|
+
const pointer = event;
|
|
4473
|
+
if (session.pointerId === null || pointer.pointerId === session.pointerId) {
|
|
4474
|
+
const result = session.handlers.update(event, modeContext());
|
|
4475
|
+
apply(result?.commands, effects);
|
|
4476
|
+
effects.handledBy = session.modeId;
|
|
4477
|
+
if (result?.status === "end") {
|
|
4478
|
+
session = null;
|
|
4479
|
+
effects.sessionEnded = true;
|
|
4480
|
+
}
|
|
4481
|
+
return effects;
|
|
4482
|
+
}
|
|
4483
|
+
if (event.kind === "pointer.down") {
|
|
4484
|
+
effects.handledBy = session.modeId;
|
|
4485
|
+
return effects;
|
|
4486
|
+
}
|
|
4487
|
+
return effects;
|
|
4488
|
+
}
|
|
4489
|
+
const ctx = modeContext();
|
|
4490
|
+
const modes = [...resolveModes(ctx.context)].sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));
|
|
4491
|
+
for (const mode of modes) {
|
|
4492
|
+
const reaction = mode.start(event, ctx);
|
|
4493
|
+
if (!reaction) continue;
|
|
4494
|
+
apply(reaction.commands, effects);
|
|
4495
|
+
effects.handledBy = mode.id;
|
|
4496
|
+
if (reaction.session) {
|
|
4497
|
+
session = {
|
|
4498
|
+
handlers: reaction.session,
|
|
4499
|
+
modeId: mode.id,
|
|
4500
|
+
pointerId: isPointer ? event.pointerId : null
|
|
4501
|
+
};
|
|
4502
|
+
effects.sessionStarted = true;
|
|
4503
|
+
}
|
|
4504
|
+
break;
|
|
4505
|
+
}
|
|
4506
|
+
return effects;
|
|
4507
|
+
}
|
|
4508
|
+
function cancelSession() {
|
|
4509
|
+
if (!session) return;
|
|
4510
|
+
const effects = {
|
|
4511
|
+
preventDefault: false,
|
|
4512
|
+
stopPropagation: false,
|
|
4513
|
+
sessionStarted: false,
|
|
4514
|
+
sessionEnded: true,
|
|
4515
|
+
handledBy: session.modeId
|
|
4516
|
+
};
|
|
4517
|
+
const commands = session.handlers.cancel?.(modeContext());
|
|
4518
|
+
session = null;
|
|
4519
|
+
apply(commands ?? void 0, effects);
|
|
4520
|
+
}
|
|
4521
|
+
return {
|
|
4522
|
+
send,
|
|
4523
|
+
cancelSession,
|
|
4524
|
+
getActiveSession: () => session?.modeId ?? null
|
|
4525
|
+
};
|
|
4526
|
+
}
|
|
4527
|
+
/** Default mode resolution: follow the store's `context.mode`. */
|
|
4528
|
+
function defaultModesForContext(context) {
|
|
4529
|
+
return context.mode === "readonly" ? readonlyInputModes : defaultInputModes;
|
|
4530
|
+
}
|
|
4531
|
+
/**
|
|
4532
|
+
* Mode resolution driven by a store profile: the active store mode's
|
|
4533
|
+
* `input.modes` wins, then the profile-level `input.modes`, then the
|
|
4534
|
+
* built-in defaults.
|
|
4535
|
+
*/
|
|
4536
|
+
function profileModesForContext(profile) {
|
|
4537
|
+
return (context) => {
|
|
4538
|
+
const mode = profile.modes[context.mode] ? context.mode : profile.defaultMode;
|
|
4539
|
+
return profile.modes[mode]?.input?.modes ?? profile.input?.modes ?? defaultModesForContext(context);
|
|
4540
|
+
};
|
|
4541
|
+
}
|
|
4542
|
+
/**
|
|
4543
|
+
* Per-frame auto-pan step while a drag or connection gesture is near the
|
|
4544
|
+
* container edge. Returns the `calcAutoPan` screen-space delta (negate it for
|
|
4545
|
+
* `panBy`), or null when auto-pan should not run. Adapters call this from a
|
|
4546
|
+
* frame ticker while a gesture session is active and re-send the stationary
|
|
4547
|
+
* pointer afterwards so the dragged entity follows the panned canvas.
|
|
4548
|
+
*/
|
|
4549
|
+
function getAutoPanDelta(context, screenPoint, engine) {
|
|
4550
|
+
if (!context.autoPanOnDrag) return null;
|
|
4551
|
+
if (context.canvasState !== "dragging" && context.canvasState !== "connecting") return null;
|
|
4552
|
+
const size = context.viewportSize;
|
|
4553
|
+
if (size.width === 0 || size.height === 0) return null;
|
|
4554
|
+
const delta = calcAutoPan(screenPoint, size, context.autoPanThreshold, context.autoPanSpeed);
|
|
4555
|
+
if (delta.x === 0 && delta.y === 0) return null;
|
|
4556
|
+
if (engine && context.canvasState === "dragging") {
|
|
4557
|
+
let union = null;
|
|
4558
|
+
for (const selectedId of context.selection) {
|
|
4559
|
+
const node = engine.getNode(selectedId);
|
|
4560
|
+
if (!node || node.draggable === false) continue;
|
|
4561
|
+
const b = engine.resolvedBounds(selectedId);
|
|
4562
|
+
const x0 = b.x * context.viewport.zoom + context.viewport.x;
|
|
4563
|
+
const y0 = b.y * context.viewport.zoom + context.viewport.y;
|
|
4564
|
+
const x1 = x0 + b.width * context.viewport.zoom;
|
|
4565
|
+
const y1 = y0 + b.height * context.viewport.zoom;
|
|
4566
|
+
union = union ? {
|
|
4567
|
+
x0: Math.min(union.x0, x0),
|
|
4568
|
+
y0: Math.min(union.y0, y0),
|
|
4569
|
+
x1: Math.max(union.x1, x1),
|
|
4570
|
+
y1: Math.max(union.y1, y1)
|
|
4571
|
+
} : {
|
|
4572
|
+
x0,
|
|
4573
|
+
y0,
|
|
4574
|
+
x1,
|
|
4575
|
+
y1
|
|
4576
|
+
};
|
|
4577
|
+
}
|
|
4578
|
+
if (union) {
|
|
4579
|
+
const edge = context.autoPanThreshold;
|
|
4580
|
+
if (delta.x < 0 && union.x0 > edge) delta.x = 0;
|
|
4581
|
+
if (delta.x > 0 && union.x1 < size.width - edge) delta.x = 0;
|
|
4582
|
+
if (delta.y < 0 && union.y0 > edge) delta.y = 0;
|
|
4583
|
+
if (delta.y > 0 && union.y1 < size.height - edge) delta.y = 0;
|
|
4584
|
+
if (delta.x === 0 && delta.y === 0) return null;
|
|
4585
|
+
}
|
|
4586
|
+
}
|
|
4587
|
+
return delta;
|
|
4588
|
+
}
|
|
4589
|
+
//#endregion
|
|
4590
|
+
//#region src/entity-changes.ts
|
|
4591
|
+
const adjacencyCache = /* @__PURE__ */ new WeakMap();
|
|
4592
|
+
function adjacency(edges) {
|
|
4593
|
+
let index = adjacencyCache.get(edges);
|
|
4594
|
+
if (!index) {
|
|
4595
|
+
index = /* @__PURE__ */ new Map();
|
|
4596
|
+
for (const edge of edges) {
|
|
4597
|
+
let source = index.get(edge.sourceId);
|
|
4598
|
+
if (!source) index.set(edge.sourceId, source = []);
|
|
4599
|
+
source.push(edge.id);
|
|
4600
|
+
if (edge.targetId !== edge.sourceId) {
|
|
4601
|
+
let target = index.get(edge.targetId);
|
|
4602
|
+
if (!target) index.set(edge.targetId, target = []);
|
|
4603
|
+
target.push(edge.id);
|
|
4604
|
+
}
|
|
4605
|
+
}
|
|
4606
|
+
adjacencyCache.set(edges, index);
|
|
4607
|
+
}
|
|
4608
|
+
return index;
|
|
4609
|
+
}
|
|
4610
|
+
function computeDirty(prev, next) {
|
|
4611
|
+
if (next.canvasState !== prev.canvasState) return "all";
|
|
4612
|
+
if (next.mode !== prev.mode) return "all";
|
|
4613
|
+
if (next.enableSnapLines && next.canvasState === "dragging") return "all";
|
|
4614
|
+
const dirty = /* @__PURE__ */ new Set();
|
|
4615
|
+
const markIncidentEdges = (nodeId, edges) => {
|
|
4616
|
+
const incident = adjacency(edges).get(nodeId);
|
|
4617
|
+
if (incident) for (const edgeId of incident) dirty.add(edgeId);
|
|
4618
|
+
};
|
|
4619
|
+
const markAncestors = (node) => {
|
|
4620
|
+
const nextIndex = indexNodes(next.graph.nodes);
|
|
4621
|
+
const seen = /* @__PURE__ */ new Set();
|
|
4622
|
+
let parentId = node.parentId ?? null;
|
|
4623
|
+
while (parentId && !seen.has(parentId)) {
|
|
4624
|
+
seen.add(parentId);
|
|
4625
|
+
dirty.add(parentId);
|
|
4626
|
+
markIncidentEdges(parentId, next.graph.edges);
|
|
4627
|
+
parentId = nextIndex.get(parentId)?.parentId ?? null;
|
|
4628
|
+
}
|
|
4629
|
+
};
|
|
4630
|
+
if (next.graph.nodes !== prev.graph.nodes) {
|
|
4631
|
+
const prevIndex = indexNodes(prev.graph.nodes);
|
|
4632
|
+
const nextIndex = indexNodes(next.graph.nodes);
|
|
4633
|
+
for (const node of next.graph.nodes) {
|
|
4634
|
+
const before = prevIndex.get(node.id);
|
|
4635
|
+
if (before !== node) {
|
|
4636
|
+
dirty.add(node.id);
|
|
4637
|
+
markIncidentEdges(node.id, next.graph.edges);
|
|
4638
|
+
markAncestors(node);
|
|
4639
|
+
if (before && (before.parentId ?? null) !== (node.parentId ?? null)) markAncestors(before);
|
|
4640
|
+
}
|
|
4641
|
+
}
|
|
4642
|
+
for (const node of prev.graph.nodes) if (!nextIndex.has(node.id)) {
|
|
4643
|
+
dirty.add(node.id);
|
|
4644
|
+
markIncidentEdges(node.id, prev.graph.edges);
|
|
4645
|
+
markAncestors(node);
|
|
4646
|
+
}
|
|
4647
|
+
}
|
|
4648
|
+
if (next.graph.edges !== prev.graph.edges) {
|
|
4649
|
+
const prevIndex = indexEdges(prev.graph.edges);
|
|
4650
|
+
const nextIndex = indexEdges(next.graph.edges);
|
|
4651
|
+
const nodeIndex = indexNodes(next.graph.nodes);
|
|
4652
|
+
const markEndpointAncestors = (edge) => {
|
|
4653
|
+
const source = nodeIndex.get(edge.sourceId);
|
|
4654
|
+
const target = nodeIndex.get(edge.targetId);
|
|
4655
|
+
markAncestors(source ?? {});
|
|
4656
|
+
markAncestors(target ?? {});
|
|
4657
|
+
if (source?.group) dirty.add(source.id);
|
|
4658
|
+
if (target?.group) dirty.add(target.id);
|
|
4659
|
+
};
|
|
4660
|
+
for (const edge of next.graph.edges) if (prevIndex.get(edge.id) !== edge) {
|
|
4661
|
+
dirty.add(edge.id);
|
|
4662
|
+
markEndpointAncestors(edge);
|
|
4663
|
+
}
|
|
4664
|
+
for (const edge of prev.graph.edges) if (!nextIndex.has(edge.id)) {
|
|
4665
|
+
dirty.add(edge.id);
|
|
4666
|
+
markEndpointAncestors(edge);
|
|
4667
|
+
}
|
|
4668
|
+
}
|
|
4669
|
+
if (next.selection !== prev.selection) {
|
|
4670
|
+
for (const id of next.selection) if (!prev.selection.has(id)) dirty.add(id);
|
|
4671
|
+
for (const id of prev.selection) if (!next.selection.has(id)) dirty.add(id);
|
|
4672
|
+
}
|
|
4673
|
+
if (next.hoveredEntityIds !== prev.hoveredEntityIds) {
|
|
4674
|
+
for (const id of next.hoveredEntityIds) if (!prev.hoveredEntityIds.has(id)) dirty.add(id);
|
|
4675
|
+
for (const id of prev.hoveredEntityIds) if (!next.hoveredEntityIds.has(id)) dirty.add(id);
|
|
4676
|
+
}
|
|
4677
|
+
if (next.highlights !== prev.highlights) {
|
|
4678
|
+
const kinds = new Set([...prev.highlights.keys(), ...next.highlights.keys()]);
|
|
4679
|
+
for (const kind of kinds) {
|
|
4680
|
+
const before = prev.highlights.get(kind);
|
|
4681
|
+
const after = next.highlights.get(kind);
|
|
4682
|
+
if (before === after) continue;
|
|
4683
|
+
if (before) for (const id of before.keys()) dirty.add(id);
|
|
4684
|
+
if (after) for (const id of after.keys()) dirty.add(id);
|
|
4685
|
+
}
|
|
4686
|
+
}
|
|
4687
|
+
const idPairs = [
|
|
4688
|
+
[prev.draggedEntityId, next.draggedEntityId],
|
|
4689
|
+
[prev.focusedEntityId, next.focusedEntityId],
|
|
4690
|
+
[prev.resizingEntityId, next.resizingEntityId]
|
|
4691
|
+
];
|
|
4692
|
+
for (const [before, after] of idPairs) if (before !== after) {
|
|
4693
|
+
if (before) dirty.add(before);
|
|
4694
|
+
if (after) dirty.add(after);
|
|
4695
|
+
}
|
|
4696
|
+
if (next.resizeCorners !== prev.resizeCorners && next.resizingEntityId) {
|
|
4697
|
+
dirty.add(next.resizingEntityId);
|
|
4698
|
+
markIncidentEdges(next.resizingEntityId, next.graph.edges);
|
|
4699
|
+
}
|
|
4700
|
+
return dirty;
|
|
4701
|
+
}
|
|
4702
|
+
function createEntityChangeNotifier(store) {
|
|
4703
|
+
const listeners = /* @__PURE__ */ new Map();
|
|
4704
|
+
let prev = store.getSnapshot().context;
|
|
4705
|
+
const subscription = store.subscribe((snapshot) => {
|
|
4706
|
+
const next = snapshot.context;
|
|
4707
|
+
if (next === prev) return;
|
|
4708
|
+
const dirty = computeDirty(prev, next);
|
|
4709
|
+
prev = next;
|
|
4710
|
+
if (dirty === "all") {
|
|
4711
|
+
for (const set of listeners.values()) for (const listener of [...set]) listener();
|
|
4712
|
+
return;
|
|
4713
|
+
}
|
|
4714
|
+
for (const id of dirty) {
|
|
4715
|
+
const set = listeners.get(id);
|
|
4716
|
+
if (set) for (const listener of [...set]) listener();
|
|
4717
|
+
}
|
|
4718
|
+
});
|
|
4719
|
+
return {
|
|
4720
|
+
subscribe(id, listener) {
|
|
4721
|
+
let set = listeners.get(id);
|
|
4722
|
+
if (!set) {
|
|
4723
|
+
set = /* @__PURE__ */ new Set();
|
|
4724
|
+
listeners.set(id, set);
|
|
4725
|
+
}
|
|
4726
|
+
set.add(listener);
|
|
4727
|
+
return () => {
|
|
4728
|
+
set.delete(listener);
|
|
4729
|
+
if (set.size === 0) listeners.delete(id);
|
|
4730
|
+
};
|
|
4731
|
+
},
|
|
4732
|
+
destroy() {
|
|
4733
|
+
subscription.unsubscribe();
|
|
4734
|
+
listeners.clear();
|
|
4735
|
+
}
|
|
4736
|
+
};
|
|
4737
|
+
}
|
|
4738
|
+
//#endregion
|
|
4739
|
+
export { CONNECTION_PREVIEW_EDGE_ID, CONNECTION_PREVIEW_NODE_ID, bestDirection, bezierRouter, boxToRect, calcAutoPan, canvasToScreen, clampZoom, connectMode, constrainViewport, createConnectionPreviewGraph, createEdge, createEntityChangeNotifier, createFlow, createFlowEngine, createFlowInputRuntime, createFlowStore, createGraph, createNode, createPort, createViewport, defaultConfig, defaultFlowProfile, defaultInputModes, defaultModesForContext, defineFlowProfile, deriveGroupGeometry, entityPressMode, fixedGroupGeometry, getAnchorTransform, getAutoPanDelta, getEdgeOwner, getGraphBounds, getMinimapProjection, getSelectionBounds, getShape, getShapeOrDefault, getSnapLines, getToolbarPlacement, getVisibleBounds, hoverMode, indexChildren, indexEdges, indexNodes, isDerivedGroup, isGroup, isTrackpadPinch, keyboardMode, keyboardNavMode, nodeDepths, normalizeWheelPan, panBy, panMode, panePressMode, pathToSVG, preventDefault, profileModesForContext, readonlyConfig, readonlyInputModes, rectContains, rectsIntersect, registerShape, resolveBehavior, resolveGroupPadding, resolvedBounds, resolvedPosition, screenToCanvas, selectMode, snapPointToGrid, snapToGrid, snappedBounds, stepRouter, straightRouter, toSVGTransform, toViewBox, trigger, unionRect, viewportCenteredOn, viewportForBounds, wheelInteraction, wheelMode, wheelZoomDelta, zoomTo };
|