@statelyai/flow 0.6.0 → 0.7.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 +213 -132
- package/dist/esm/index.d.ts +58 -21
- package/dist/esm/index.js +214 -183
- package/package.json +1 -1
package/dist/esm/index.js
CHANGED
|
@@ -203,16 +203,29 @@ function focalViewport(plan, t) {
|
|
|
203
203
|
zoom
|
|
204
204
|
};
|
|
205
205
|
}
|
|
206
|
+
function linearViewport(from, to, t) {
|
|
207
|
+
return {
|
|
208
|
+
x: from.x + (to.x - from.x) * t,
|
|
209
|
+
y: from.y + (to.y - from.y) * t,
|
|
210
|
+
zoom: from.zoom + (to.zoom - from.zoom) * t
|
|
211
|
+
};
|
|
212
|
+
}
|
|
206
213
|
const DEFAULT_SPRING_STIFFNESS = 170;
|
|
207
214
|
const DEFAULT_SPRING_DAMPING = 26;
|
|
208
215
|
const DEFAULT_SPRING_MASS = 1;
|
|
209
216
|
const SPRING_REST_DELTA = .001;
|
|
210
217
|
const SPRING_REST_SPEED = .001;
|
|
211
218
|
const MAX_SPRING_STEP_MS = 1e3 / 60;
|
|
219
|
+
const LERP_FRAME_MS = 1e3 / 60;
|
|
220
|
+
const LERP_POSITION_REST_DELTA = .1;
|
|
221
|
+
const LERP_ZOOM_REST_DELTA = 1e-4;
|
|
212
222
|
function animateViewport(store, target, options = {}) {
|
|
213
223
|
const useSpring = options.spring !== void 0;
|
|
224
|
+
const useLerp = options.lerp !== void 0;
|
|
225
|
+
if (useSpring && useLerp) throw new TypeError("Viewport animation cannot use both spring and lerp.");
|
|
226
|
+
if (useLerp && (!Number.isFinite(options.lerp) || options.lerp <= 0 || options.lerp > 1)) throw new TypeError("Viewport lerp factor must be finite and in (0, 1].");
|
|
214
227
|
const duration = options.duration ?? 0;
|
|
215
|
-
if (!useSpring && duration <= 0) {
|
|
228
|
+
if (!useSpring && !useLerp && duration <= 0) {
|
|
216
229
|
store.trigger.setViewport(target);
|
|
217
230
|
return {
|
|
218
231
|
cancel: () => {},
|
|
@@ -222,7 +235,8 @@ function animateViewport(store, target, options = {}) {
|
|
|
222
235
|
const raf = options.raf ?? defaultRaf;
|
|
223
236
|
const cancelRaf = options.cancelRaf ?? defaultCancelRaf;
|
|
224
237
|
const easing = options.easing ?? defaultEasing;
|
|
225
|
-
const
|
|
238
|
+
const from = { ...store.getSnapshot().context.viewport };
|
|
239
|
+
const plan = planFocalZoom(from, target, options.center, store.getSnapshot().context.viewportSize);
|
|
226
240
|
let frame = null;
|
|
227
241
|
let startTime = null;
|
|
228
242
|
let lastTime = null;
|
|
@@ -251,6 +265,7 @@ function animateViewport(store, target, options = {}) {
|
|
|
251
265
|
const mass = springConfig.mass ?? DEFAULT_SPRING_MASS;
|
|
252
266
|
let springValue = 0;
|
|
253
267
|
let springVelocity = 0;
|
|
268
|
+
let lerpValue = from;
|
|
254
269
|
const springStep = (dtMs) => {
|
|
255
270
|
let remaining = dtMs;
|
|
256
271
|
while (remaining > 0) {
|
|
@@ -280,6 +295,21 @@ function animateViewport(store, target, options = {}) {
|
|
|
280
295
|
frame = raf(step);
|
|
281
296
|
return;
|
|
282
297
|
}
|
|
298
|
+
if (useLerp) {
|
|
299
|
+
const dtMs = Math.max(0, time - (lastTime ?? time));
|
|
300
|
+
lastTime = time;
|
|
301
|
+
if (dtMs > 0) {
|
|
302
|
+
const frameFactor = 1 - Math.pow(1 - options.lerp, dtMs / LERP_FRAME_MS);
|
|
303
|
+
lerpValue = linearViewport(lerpValue, target, frameFactor);
|
|
304
|
+
}
|
|
305
|
+
if (Math.abs(target.x - lerpValue.x) <= LERP_POSITION_REST_DELTA && Math.abs(target.y - lerpValue.y) <= LERP_POSITION_REST_DELTA && Math.abs(target.zoom - lerpValue.zoom) <= LERP_ZOOM_REST_DELTA) {
|
|
306
|
+
finish();
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
store.trigger.setViewport(lerpValue);
|
|
310
|
+
frame = raf(step);
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
283
313
|
const progress = Math.min(1, (time - startTime) / duration);
|
|
284
314
|
if (progress >= 1) {
|
|
285
315
|
finish();
|
|
@@ -421,6 +451,55 @@ function validateFitFrame(frame) {
|
|
|
421
451
|
height: frame.height
|
|
422
452
|
};
|
|
423
453
|
}
|
|
454
|
+
/**
|
|
455
|
+
* Reduce a fit frame by measured floating overlays. Each overlay reserves the
|
|
456
|
+
* band between itself and its assigned (or nearest) frame edge.
|
|
457
|
+
*/
|
|
458
|
+
function fitFrameAroundExclusions(frame, exclusions) {
|
|
459
|
+
const base = validateFitFrame(frame);
|
|
460
|
+
const right = base.x + base.width;
|
|
461
|
+
const bottom = base.y + base.height;
|
|
462
|
+
let leftInset = 0;
|
|
463
|
+
let rightInset = 0;
|
|
464
|
+
let topInset = 0;
|
|
465
|
+
let bottomInset = 0;
|
|
466
|
+
for (const exclusion of exclusions) {
|
|
467
|
+
const rect = exclusion.rect;
|
|
468
|
+
if (!Number.isFinite(rect.x) || !Number.isFinite(rect.y) || !Number.isFinite(rect.width) || !Number.isFinite(rect.height) || rect.width <= 0 || rect.height <= 0) continue;
|
|
469
|
+
const clippedLeft = Math.max(base.x, rect.x);
|
|
470
|
+
const clippedTop = Math.max(base.y, rect.y);
|
|
471
|
+
const clippedRight = Math.min(right, rect.x + rect.width);
|
|
472
|
+
const clippedBottom = Math.min(bottom, rect.y + rect.height);
|
|
473
|
+
if (clippedRight <= clippedLeft || clippedBottom <= clippedTop) continue;
|
|
474
|
+
const gap = Number.isFinite(exclusion.gap) ? Math.max(0, exclusion.gap ?? 0) : 0;
|
|
475
|
+
const side = exclusion.side && exclusion.side !== "nearest" ? exclusion.side : nearestFrameEdge(base, {
|
|
476
|
+
x: clippedLeft,
|
|
477
|
+
y: clippedTop,
|
|
478
|
+
width: clippedRight - clippedLeft,
|
|
479
|
+
height: clippedBottom - clippedTop
|
|
480
|
+
});
|
|
481
|
+
if (side === "left") leftInset = Math.max(leftInset, clippedRight + gap - base.x);
|
|
482
|
+
else if (side === "right") rightInset = Math.max(rightInset, right - clippedLeft + gap);
|
|
483
|
+
else if (side === "top") topInset = Math.max(topInset, clippedBottom + gap - base.y);
|
|
484
|
+
else bottomInset = Math.max(bottomInset, bottom - clippedTop + gap);
|
|
485
|
+
}
|
|
486
|
+
const width = base.width - leftInset - rightInset;
|
|
487
|
+
const height = base.height - topInset - bottomInset;
|
|
488
|
+
return width > 0 && height > 0 ? {
|
|
489
|
+
x: base.x + leftInset,
|
|
490
|
+
y: base.y + topInset,
|
|
491
|
+
width,
|
|
492
|
+
height
|
|
493
|
+
} : base;
|
|
494
|
+
}
|
|
495
|
+
function nearestFrameEdge(frame, rect) {
|
|
496
|
+
return [
|
|
497
|
+
["left", Math.abs(rect.x - frame.x)],
|
|
498
|
+
["right", Math.abs(frame.x + frame.width - (rect.x + rect.width))],
|
|
499
|
+
["top", Math.abs(rect.y - frame.y)],
|
|
500
|
+
["bottom", Math.abs(frame.y + frame.height - (rect.y + rect.height))]
|
|
501
|
+
].reduce((best, candidate) => candidate[1] < best[1] ? candidate : best)[0];
|
|
502
|
+
}
|
|
424
503
|
//#endregion
|
|
425
504
|
//#region src/spatial.ts
|
|
426
505
|
/** Visual position: layout position + drag offset. */
|
|
@@ -1123,147 +1202,6 @@ function getSnapLines(rect, otherRects, { threshold = 5, enableSpacing = false }
|
|
|
1123
1202
|
};
|
|
1124
1203
|
}
|
|
1125
1204
|
//#endregion
|
|
1126
|
-
//#region src/edge-geometry.ts
|
|
1127
|
-
function isPointNearPath(point, path, tolerance) {
|
|
1128
|
-
const samplePoints = samplePathPoints(path);
|
|
1129
|
-
for (let i = 0; i < samplePoints.length - 1; i++) {
|
|
1130
|
-
const start = samplePoints[i];
|
|
1131
|
-
const end = samplePoints[i + 1];
|
|
1132
|
-
if (start && end && distanceToSegment(point, start, end) <= tolerance) return true;
|
|
1133
|
-
}
|
|
1134
|
-
return false;
|
|
1135
|
-
}
|
|
1136
|
-
/**
|
|
1137
|
-
* Axis-aligned bounding box of a path's control/anchor points. A cubic bezier
|
|
1138
|
-
* lies within the convex hull of its control points, so this is a conservative
|
|
1139
|
-
* superset of the rendered curve — exact enough for viewport culling (no false
|
|
1140
|
-
* negatives) and ~O(points) instead of sampling the curve. Returns null for an
|
|
1141
|
-
* empty path.
|
|
1142
|
-
*/
|
|
1143
|
-
function pathBounds(path) {
|
|
1144
|
-
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
1145
|
-
for (const point of path.points) {
|
|
1146
|
-
if (point.x < minX) minX = point.x;
|
|
1147
|
-
if (point.y < minY) minY = point.y;
|
|
1148
|
-
if (point.x > maxX) maxX = point.x;
|
|
1149
|
-
if (point.y > maxY) maxY = point.y;
|
|
1150
|
-
}
|
|
1151
|
-
if (!isFinite(minX)) return null;
|
|
1152
|
-
return {
|
|
1153
|
-
x: minX,
|
|
1154
|
-
y: minY,
|
|
1155
|
-
width: maxX - minX,
|
|
1156
|
-
height: maxY - minY
|
|
1157
|
-
};
|
|
1158
|
-
}
|
|
1159
|
-
function rectIntersectsPath(rect, path) {
|
|
1160
|
-
const samplePoints = samplePathPoints(path);
|
|
1161
|
-
for (const point of samplePoints) if (pointInRect(point, rect)) return true;
|
|
1162
|
-
const corners = rectCorners(rect);
|
|
1163
|
-
for (let i = 0; i < samplePoints.length - 1; i++) {
|
|
1164
|
-
const start = samplePoints[i];
|
|
1165
|
-
const end = samplePoints[i + 1];
|
|
1166
|
-
if (!start || !end) continue;
|
|
1167
|
-
if (pointInRect(start, rect) || pointInRect(end, rect)) return true;
|
|
1168
|
-
for (let cornerIndex = 0; cornerIndex < corners.length; cornerIndex++) {
|
|
1169
|
-
const edgeStart = corners[cornerIndex];
|
|
1170
|
-
const edgeEnd = corners[(cornerIndex + 1) % corners.length];
|
|
1171
|
-
if (edgeStart && edgeEnd && segmentsIntersect(start, end, edgeStart, edgeEnd)) return true;
|
|
1172
|
-
}
|
|
1173
|
-
}
|
|
1174
|
-
return false;
|
|
1175
|
-
}
|
|
1176
|
-
function basicEdgePath(edge, nodes) {
|
|
1177
|
-
const source = nodes.find((node) => node.id === edge.sourceId);
|
|
1178
|
-
const target = nodes.find((node) => node.id === edge.targetId);
|
|
1179
|
-
if (!source || !target) return null;
|
|
1180
|
-
return {
|
|
1181
|
-
points: [resolveBasicEndpoint(source, edge.sourcePort), resolveBasicEndpoint(target, edge.targetPort)],
|
|
1182
|
-
sourceDirection: "right",
|
|
1183
|
-
targetDirection: "left"
|
|
1184
|
-
};
|
|
1185
|
-
}
|
|
1186
|
-
function resolveBasicEndpoint(node, portName) {
|
|
1187
|
-
if (portName && portName !== "*" && node.ports) {
|
|
1188
|
-
const port = node.ports.find((candidate) => candidate.name === portName);
|
|
1189
|
-
if (port) {
|
|
1190
|
-
const position = resolvedPosition(node);
|
|
1191
|
-
return {
|
|
1192
|
-
x: position.x + port.x + port.width / 2,
|
|
1193
|
-
y: position.y + port.y + port.height / 2
|
|
1194
|
-
};
|
|
1195
|
-
}
|
|
1196
|
-
}
|
|
1197
|
-
const bounds = resolvedBounds(node);
|
|
1198
|
-
return {
|
|
1199
|
-
x: bounds.x + bounds.width / 2,
|
|
1200
|
-
y: bounds.y + bounds.height / 2
|
|
1201
|
-
};
|
|
1202
|
-
}
|
|
1203
|
-
function samplePathPoints(path) {
|
|
1204
|
-
if (path.points.length === 4) {
|
|
1205
|
-
const [p0, p1, p2, p3] = path.points;
|
|
1206
|
-
if (p0 && p1 && p2 && p3) return sampleCubicBezier(p0, p1, p2, p3, 20);
|
|
1207
|
-
}
|
|
1208
|
-
return path.points;
|
|
1209
|
-
}
|
|
1210
|
-
function sampleCubicBezier(p0, p1, p2, p3, steps) {
|
|
1211
|
-
const points = [];
|
|
1212
|
-
for (let i = 0; i <= steps; i++) {
|
|
1213
|
-
const t = i / steps;
|
|
1214
|
-
const mt = 1 - t;
|
|
1215
|
-
points.push({
|
|
1216
|
-
x: mt ** 3 * p0.x + 3 * mt ** 2 * t * p1.x + 3 * mt * t ** 2 * p2.x + t ** 3 * p3.x,
|
|
1217
|
-
y: mt ** 3 * p0.y + 3 * mt ** 2 * t * p1.y + 3 * mt * t ** 2 * p2.y + t ** 3 * p3.y
|
|
1218
|
-
});
|
|
1219
|
-
}
|
|
1220
|
-
return points;
|
|
1221
|
-
}
|
|
1222
|
-
function distanceToSegment(point, start, end) {
|
|
1223
|
-
const dx = end.x - start.x;
|
|
1224
|
-
const dy = end.y - start.y;
|
|
1225
|
-
if (dx === 0 && dy === 0) return Math.hypot(point.x - start.x, point.y - start.y);
|
|
1226
|
-
const t = Math.max(0, Math.min(1, ((point.x - start.x) * dx + (point.y - start.y) * dy) / (dx * dx + dy * dy)));
|
|
1227
|
-
return Math.hypot(point.x - (start.x + t * dx), point.y - (start.y + t * dy));
|
|
1228
|
-
}
|
|
1229
|
-
function pointInRect(point, rect) {
|
|
1230
|
-
return point.x >= rect.x && point.x <= rect.x + rect.width && point.y >= rect.y && point.y <= rect.y + rect.height;
|
|
1231
|
-
}
|
|
1232
|
-
function rectCorners(rect) {
|
|
1233
|
-
return [
|
|
1234
|
-
{
|
|
1235
|
-
x: rect.x,
|
|
1236
|
-
y: rect.y
|
|
1237
|
-
},
|
|
1238
|
-
{
|
|
1239
|
-
x: rect.x + rect.width,
|
|
1240
|
-
y: rect.y
|
|
1241
|
-
},
|
|
1242
|
-
{
|
|
1243
|
-
x: rect.x + rect.width,
|
|
1244
|
-
y: rect.y + rect.height
|
|
1245
|
-
},
|
|
1246
|
-
{
|
|
1247
|
-
x: rect.x,
|
|
1248
|
-
y: rect.y + rect.height
|
|
1249
|
-
}
|
|
1250
|
-
];
|
|
1251
|
-
}
|
|
1252
|
-
function segmentsIntersect(a1, a2, b1, b2) {
|
|
1253
|
-
const d1 = direction(b1, b2, a1);
|
|
1254
|
-
const d2 = direction(b1, b2, a2);
|
|
1255
|
-
const d3 = direction(a1, a2, b1);
|
|
1256
|
-
const d4 = direction(a1, a2, b2);
|
|
1257
|
-
if ((d1 > 0 && d2 < 0 || d1 < 0 && d2 > 0) && (d3 > 0 && d4 < 0 || d3 < 0 && d4 > 0)) return true;
|
|
1258
|
-
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);
|
|
1259
|
-
}
|
|
1260
|
-
function direction(a, b, c) {
|
|
1261
|
-
return (c.x - a.x) * (b.y - a.y) - (c.y - a.y) * (b.x - a.x);
|
|
1262
|
-
}
|
|
1263
|
-
function onSegment(a, b, c) {
|
|
1264
|
-
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);
|
|
1265
|
-
}
|
|
1266
|
-
//#endregion
|
|
1267
1205
|
//#region src/graph-index.ts
|
|
1268
1206
|
const nodeIndexCache = /* @__PURE__ */ new WeakMap();
|
|
1269
1207
|
const edgeIndexCache = /* @__PURE__ */ new WeakMap();
|
|
@@ -2283,16 +2221,15 @@ function isExplicitlyVisibleEdge(edge, nodes) {
|
|
|
2283
2221
|
return !!source && !!target && isExplicitlyVisibleNode(source) && isExplicitlyVisibleNode(target);
|
|
2284
2222
|
}
|
|
2285
2223
|
/**
|
|
2286
|
-
* Entities captured by a box-select rectangle. Node
|
|
2287
|
-
*
|
|
2224
|
+
* Entities captured by a box-select rectangle. Node and edge-label membership
|
|
2225
|
+
* is decided by `ctx.boxSelectionTest` when set,
|
|
2288
2226
|
* otherwise by the built-in `selectionMode` (partial = any overlap via
|
|
2289
|
-
* `rectsIntersect`, full = fully contained via `rectContains`).
|
|
2290
|
-
*
|
|
2227
|
+
* `rectsIntersect`, full = fully contained via `rectContains`). Edge paths are
|
|
2228
|
+
* never marquee targets; an edge without a label cannot be box-selected.
|
|
2291
2229
|
*/
|
|
2292
2230
|
function entitiesInBox(ctx, rect) {
|
|
2293
2231
|
const ids = /* @__PURE__ */ new Set();
|
|
2294
2232
|
const builtinFull = !ctx.boxSelectionTest && ctx.selectionMode === "full";
|
|
2295
|
-
const builtinPartial = !ctx.boxSelectionTest && ctx.selectionMode !== "full";
|
|
2296
2233
|
const test = ctx.boxSelectionTest ?? (builtinFull ? rectContains : rectsIntersect);
|
|
2297
2234
|
const nodes = ctx.graph.nodes;
|
|
2298
2235
|
for (const node of nodes) {
|
|
@@ -2301,21 +2238,8 @@ function entitiesInBox(ctx, rect) {
|
|
|
2301
2238
|
}
|
|
2302
2239
|
for (const edge of ctx.graph.edges) {
|
|
2303
2240
|
if (!isExplicitlyVisibleEdge(edge, nodes)) continue;
|
|
2304
|
-
if (edge.label)
|
|
2305
|
-
|
|
2306
|
-
ids.add(edge.id);
|
|
2307
|
-
continue;
|
|
2308
|
-
}
|
|
2309
|
-
}
|
|
2310
|
-
const source = nodes.find((node) => node.id === edge.sourceId);
|
|
2311
|
-
const target = nodes.find((node) => node.id === edge.targetId);
|
|
2312
|
-
const bothEndpoints = !!source && !!target && test(rect, resolvedBounds(source)) && test(rect, resolvedBounds(target));
|
|
2313
|
-
let selected;
|
|
2314
|
-
if (builtinPartial) {
|
|
2315
|
-
const edgePath = basicEdgePath(edge, nodes);
|
|
2316
|
-
selected = !!edgePath && rectIntersectsPath(rect, edgePath) && !bothEndpoints;
|
|
2317
|
-
} else selected = bothEndpoints;
|
|
2318
|
-
if (selected) ids.add(edge.id);
|
|
2241
|
+
if (!edge.label) continue;
|
|
2242
|
+
if (test(rect, resolvedEdgeLabelBounds(edge, ctx.measurements.get(edge.id)))) ids.add(edge.id);
|
|
2319
2243
|
}
|
|
2320
2244
|
return ids;
|
|
2321
2245
|
}
|
|
@@ -3031,9 +2955,10 @@ function createFlowStore(initialGraph = {
|
|
|
3031
2955
|
x: event.point.x,
|
|
3032
2956
|
y: event.point.y
|
|
3033
2957
|
};
|
|
3034
|
-
const
|
|
2958
|
+
const boxed = entitiesInBox(draft, boxToRect(draft.selectionBox[0], draft.selectionBox[1]));
|
|
3035
2959
|
const next = new Set(draft.selectionBoxBase ?? []);
|
|
3036
|
-
for (const id of
|
|
2960
|
+
for (const id of draft.selection) if (boxed.has(id)) next.add(id);
|
|
2961
|
+
for (const id of boxed) next.add(id);
|
|
3037
2962
|
if (!setsEqual(draft.selection, next)) {
|
|
3038
2963
|
draft.selection = next;
|
|
3039
2964
|
enqueue.emit.selectionChanged({
|
|
@@ -3154,8 +3079,27 @@ function createFlowStore(initialGraph = {
|
|
|
3154
3079
|
event: createEventObject("setViewport", event)
|
|
3155
3080
|
});
|
|
3156
3081
|
}),
|
|
3157
|
-
setFitFrame: (ctx, event) => produce(ctx, (draft) => {
|
|
3082
|
+
setFitFrame: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
3083
|
+
const previousFrame = draft.fitFrame ?? {
|
|
3084
|
+
x: 0,
|
|
3085
|
+
y: 0,
|
|
3086
|
+
width: draft.viewportSize.width,
|
|
3087
|
+
height: draft.viewportSize.height
|
|
3088
|
+
};
|
|
3089
|
+
const nextFrame = event.frame ?? {
|
|
3090
|
+
x: 0,
|
|
3091
|
+
y: 0,
|
|
3092
|
+
width: draft.viewportSize.width,
|
|
3093
|
+
height: draft.viewportSize.height
|
|
3094
|
+
};
|
|
3158
3095
|
draft.fitFrame = event.frame ? { ...event.frame } : null;
|
|
3096
|
+
if (!event.preserveCenter) return;
|
|
3097
|
+
draft.viewport.x += nextFrame.x + nextFrame.width / 2 - (previousFrame.x + previousFrame.width / 2);
|
|
3098
|
+
draft.viewport.y += nextFrame.y + nextFrame.height / 2 - (previousFrame.y + previousFrame.height / 2);
|
|
3099
|
+
enqueue.emit.viewportChanged({
|
|
3100
|
+
viewport: plainViewport(draft.viewport),
|
|
3101
|
+
event: createEventObject("setFitFrame", event)
|
|
3102
|
+
});
|
|
3159
3103
|
}),
|
|
3160
3104
|
panBy: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
3161
3105
|
draft.viewport.x += event.x;
|
|
@@ -3265,11 +3209,19 @@ function createFlowStore(initialGraph = {
|
|
|
3265
3209
|
setNodeExtent: (ctx, event) => produce(ctx, (draft) => {
|
|
3266
3210
|
draft.nodeExtent = event.extent;
|
|
3267
3211
|
}),
|
|
3268
|
-
updateViewportSize: (ctx, event) => produce(ctx, (draft) => {
|
|
3212
|
+
updateViewportSize: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
3213
|
+
const previousSize = { ...draft.viewportSize };
|
|
3269
3214
|
draft.viewportSize = {
|
|
3270
3215
|
width: event.width,
|
|
3271
3216
|
height: event.height
|
|
3272
3217
|
};
|
|
3218
|
+
if (!event.preserveCenter || previousSize.width <= 0 || previousSize.height <= 0) return;
|
|
3219
|
+
draft.viewport.x += (event.width - previousSize.width) / 2;
|
|
3220
|
+
draft.viewport.y += (event.height - previousSize.height) / 2;
|
|
3221
|
+
enqueue.emit.viewportChanged({
|
|
3222
|
+
viewport: plainViewport(draft.viewport),
|
|
3223
|
+
event: createEventObject("updateViewportSize", event)
|
|
3224
|
+
});
|
|
3273
3225
|
}),
|
|
3274
3226
|
fitView: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
3275
3227
|
if (draft.viewportSize.width === 0 || draft.viewportSize.height === 0) return;
|
|
@@ -4572,6 +4524,66 @@ function resolveBehavior(predicate, entity, state, defaultValue = true) {
|
|
|
4572
4524
|
return predicate(entity, state);
|
|
4573
4525
|
}
|
|
4574
4526
|
//#endregion
|
|
4527
|
+
//#region src/edge-geometry.ts
|
|
4528
|
+
function isPointNearPath(point, path, tolerance) {
|
|
4529
|
+
const samplePoints = samplePathPoints(path);
|
|
4530
|
+
for (let i = 0; i < samplePoints.length - 1; i++) {
|
|
4531
|
+
const start = samplePoints[i];
|
|
4532
|
+
const end = samplePoints[i + 1];
|
|
4533
|
+
if (start && end && distanceToSegment(point, start, end) <= tolerance) return true;
|
|
4534
|
+
}
|
|
4535
|
+
return false;
|
|
4536
|
+
}
|
|
4537
|
+
/**
|
|
4538
|
+
* Axis-aligned bounding box of a path's control/anchor points. A cubic bezier
|
|
4539
|
+
* lies within the convex hull of its control points, so this is a conservative
|
|
4540
|
+
* superset of the rendered curve — exact enough for viewport culling (no false
|
|
4541
|
+
* negatives) and ~O(points) instead of sampling the curve. Returns null for an
|
|
4542
|
+
* empty path.
|
|
4543
|
+
*/
|
|
4544
|
+
function pathBounds(path) {
|
|
4545
|
+
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
4546
|
+
for (const point of path.points) {
|
|
4547
|
+
if (point.x < minX) minX = point.x;
|
|
4548
|
+
if (point.y < minY) minY = point.y;
|
|
4549
|
+
if (point.x > maxX) maxX = point.x;
|
|
4550
|
+
if (point.y > maxY) maxY = point.y;
|
|
4551
|
+
}
|
|
4552
|
+
if (!isFinite(minX)) return null;
|
|
4553
|
+
return {
|
|
4554
|
+
x: minX,
|
|
4555
|
+
y: minY,
|
|
4556
|
+
width: maxX - minX,
|
|
4557
|
+
height: maxY - minY
|
|
4558
|
+
};
|
|
4559
|
+
}
|
|
4560
|
+
function samplePathPoints(path) {
|
|
4561
|
+
if (path.points.length === 4) {
|
|
4562
|
+
const [p0, p1, p2, p3] = path.points;
|
|
4563
|
+
if (p0 && p1 && p2 && p3) return sampleCubicBezier(p0, p1, p2, p3, 20);
|
|
4564
|
+
}
|
|
4565
|
+
return path.points;
|
|
4566
|
+
}
|
|
4567
|
+
function sampleCubicBezier(p0, p1, p2, p3, steps) {
|
|
4568
|
+
const points = [];
|
|
4569
|
+
for (let i = 0; i <= steps; i++) {
|
|
4570
|
+
const t = i / steps;
|
|
4571
|
+
const mt = 1 - t;
|
|
4572
|
+
points.push({
|
|
4573
|
+
x: mt ** 3 * p0.x + 3 * mt ** 2 * t * p1.x + 3 * mt * t ** 2 * p2.x + t ** 3 * p3.x,
|
|
4574
|
+
y: mt ** 3 * p0.y + 3 * mt ** 2 * t * p1.y + 3 * mt * t ** 2 * p2.y + t ** 3 * p3.y
|
|
4575
|
+
});
|
|
4576
|
+
}
|
|
4577
|
+
return points;
|
|
4578
|
+
}
|
|
4579
|
+
function distanceToSegment(point, start, end) {
|
|
4580
|
+
const dx = end.x - start.x;
|
|
4581
|
+
const dy = end.y - start.y;
|
|
4582
|
+
if (dx === 0 && dy === 0) return Math.hypot(point.x - start.x, point.y - start.y);
|
|
4583
|
+
const t = Math.max(0, Math.min(1, ((point.x - start.x) * dx + (point.y - start.y) * dy) / (dx * dx + dy * dy)));
|
|
4584
|
+
return Math.hypot(point.x - (start.x + t * dx), point.y - (start.y + t * dy));
|
|
4585
|
+
}
|
|
4586
|
+
//#endregion
|
|
4575
4587
|
//#region src/engine.ts
|
|
4576
4588
|
const EMPTY_FOLDED = Object.freeze({
|
|
4577
4589
|
edges: Object.freeze([]),
|
|
@@ -5120,9 +5132,15 @@ function createFlowEngine(store, initialOptions = {}) {
|
|
|
5120
5132
|
const addressable = getEntityRef(id);
|
|
5121
5133
|
if (!addressable) return void 0;
|
|
5122
5134
|
if (addressable.kind === "node") {
|
|
5135
|
+
const bounds = hierarchyBounds(addressable.node, c);
|
|
5136
|
+
if (isDerivedGroup(addressable.node)) return bounds;
|
|
5123
5137
|
const measured = c.measurements.get(id);
|
|
5124
|
-
if (measured) return
|
|
5125
|
-
return
|
|
5138
|
+
if (!measured) return bounds;
|
|
5139
|
+
return {
|
|
5140
|
+
...bounds,
|
|
5141
|
+
width: measured.width,
|
|
5142
|
+
height: measured.height
|
|
5143
|
+
};
|
|
5126
5144
|
}
|
|
5127
5145
|
if (addressable.kind === "edge") return getEdgeLabelBounds(id) ?? getEdgePathBounds(id) ?? void 0;
|
|
5128
5146
|
if (addressable.kind === "port") {
|
|
@@ -5814,19 +5832,14 @@ function createFlowEngine(store, initialOptions = {}) {
|
|
|
5814
5832
|
const c = ctx();
|
|
5815
5833
|
const { graph } = c;
|
|
5816
5834
|
const ids = [];
|
|
5817
|
-
const edgePaths = getAllEdgePaths();
|
|
5818
5835
|
for (const n of graph.nodes) {
|
|
5819
5836
|
if (!isNodeVisible(n, c)) continue;
|
|
5820
5837
|
if (rectsIntersect(rect, visualBounds(n, getVisualSnapOffset()))) ids.push(n.id);
|
|
5821
5838
|
}
|
|
5822
5839
|
for (const e of graph.edges) {
|
|
5823
5840
|
if (!isEdgeVisible(e, graph, c)) continue;
|
|
5824
|
-
if (e.label)
|
|
5825
|
-
|
|
5826
|
-
continue;
|
|
5827
|
-
}
|
|
5828
|
-
const path = edgePaths.get(e.id);
|
|
5829
|
-
if (path && path.segments.some((segment) => rectIntersectsPath(rect, segment))) ids.push(e.id);
|
|
5841
|
+
if (!e.label) continue;
|
|
5842
|
+
if (rectsIntersect(rect, getEdgeLabelBounds(e.id))) ids.push(e.id);
|
|
5830
5843
|
}
|
|
5831
5844
|
return ids;
|
|
5832
5845
|
},
|
|
@@ -6590,6 +6603,7 @@ function createFlow(options = {}) {
|
|
|
6590
6603
|
let createEdgeOnConnect = options.createEdgeOnConnect ?? false;
|
|
6591
6604
|
const projectionListeners = /* @__PURE__ */ new Set();
|
|
6592
6605
|
let activeViewportAnimation = null;
|
|
6606
|
+
const fitExclusions = /* @__PURE__ */ new Set();
|
|
6593
6607
|
const pendingInternalViewportWrites = [];
|
|
6594
6608
|
let lastViewport = store.getSnapshot().context.viewport;
|
|
6595
6609
|
let graphRevision = 0;
|
|
@@ -6660,6 +6674,7 @@ function createFlow(options = {}) {
|
|
|
6660
6674
|
activeViewportAnimation = null;
|
|
6661
6675
|
for (const dispose of cleanup) dispose();
|
|
6662
6676
|
cleanup.clear();
|
|
6677
|
+
fitExclusions.clear();
|
|
6663
6678
|
projectionListeners.clear();
|
|
6664
6679
|
commitListeners.clear();
|
|
6665
6680
|
}
|
|
@@ -7198,7 +7213,7 @@ function createFlow(options = {}) {
|
|
|
7198
7213
|
return resolvedViewportControls();
|
|
7199
7214
|
}
|
|
7200
7215
|
cancelViewportAnimation();
|
|
7201
|
-
if ((animationOptions?.duration ?? 0) <= 0 && !animationOptions?.spring) {
|
|
7216
|
+
if ((animationOptions?.duration ?? 0) <= 0 && !animationOptions?.spring && animationOptions?.lerp === void 0) {
|
|
7202
7217
|
store.trigger.setViewport(target);
|
|
7203
7218
|
return resolvedViewportControls();
|
|
7204
7219
|
}
|
|
@@ -7230,15 +7245,23 @@ function createFlow(options = {}) {
|
|
|
7230
7245
|
});
|
|
7231
7246
|
}
|
|
7232
7247
|
function getFitFrame(frame) {
|
|
7233
|
-
if (frame) return Object.freeze(validateFitFrame(frame));
|
|
7234
7248
|
const context = store.getSnapshot().context;
|
|
7235
|
-
|
|
7236
|
-
return Object.freeze({
|
|
7249
|
+
const base = frame ? validateFitFrame(frame) : context.fitFrame ? validateFitFrame(context.fitFrame) : {
|
|
7237
7250
|
x: 0,
|
|
7238
7251
|
y: 0,
|
|
7239
7252
|
width: context.viewportSize.width,
|
|
7240
7253
|
height: context.viewportSize.height
|
|
7254
|
+
};
|
|
7255
|
+
if (base.width <= 0 || base.height <= 0) return Object.freeze(base);
|
|
7256
|
+
const resolved = [...fitExclusions].flatMap((exclusion) => {
|
|
7257
|
+
const rect = exclusion.getRect();
|
|
7258
|
+
return rect ? [{
|
|
7259
|
+
rect,
|
|
7260
|
+
side: exclusion.side,
|
|
7261
|
+
gap: exclusion.gap
|
|
7262
|
+
}] : [];
|
|
7241
7263
|
});
|
|
7264
|
+
return Object.freeze(fitFrameAroundExclusions(base, resolved));
|
|
7242
7265
|
}
|
|
7243
7266
|
function frameCenter(frame) {
|
|
7244
7267
|
const target = getFitFrame(frame);
|
|
@@ -7797,8 +7820,16 @@ function createFlow(options = {}) {
|
|
|
7797
7820
|
return setViewportTarget(viewportCenteredOn(point, context.viewportSize, clamped, getFitFrame(frame)), animationOptions);
|
|
7798
7821
|
},
|
|
7799
7822
|
getFitFrame,
|
|
7800
|
-
setFitFrame: (frame) => {
|
|
7801
|
-
store.trigger.setFitFrame({
|
|
7823
|
+
setFitFrame: (frame, options) => {
|
|
7824
|
+
store.trigger.setFitFrame({
|
|
7825
|
+
frame: frame ? validateFitFrame(frame) : null,
|
|
7826
|
+
preserveCenter: options?.preserveCenter
|
|
7827
|
+
});
|
|
7828
|
+
},
|
|
7829
|
+
registerFitExclusion: (exclusion) => {
|
|
7830
|
+
if (!exclusion || typeof exclusion.getRect !== "function") throw new TypeError("Fit exclusion must provide getRect().");
|
|
7831
|
+
fitExclusions.add(exclusion);
|
|
7832
|
+
return () => fitExclusions.delete(exclusion);
|
|
7802
7833
|
}
|
|
7803
7834
|
};
|
|
7804
7835
|
const layout = {
|
|
@@ -9863,4 +9894,4 @@ function createEntityChangeNotifier(store, options = {}) {
|
|
|
9863
9894
|
};
|
|
9864
9895
|
}
|
|
9865
9896
|
//#endregion
|
|
9866
|
-
export { CONNECTION_PREVIEW_EDGE_ID, CONNECTION_PREVIEW_NODE_ID, FlowGraphValidationError, animateViewport, applyFlowPatches, areNodesMeasured, avoidRouter, bestDirection, bezierRouter, boxSelectMode, boxToRect, calcAutoPan, canvasToScreen, clampZoom, cloneJsonSafe, composeGraphProjectionAttrs, configureAvoidRouter, configureBezierRouter, configureStepRouter, connectMode, constrainViewport, createConnectionPreviewGraph, createEdge, createEntityChangeNotifier, createFlow, createFlowChangeSet, createFlowEngine, createFlowInputRuntime, createFlowStore, createGraph, createNode, createPort, createViewport, defaultConfig, defaultFlowProfile, defaultInputModes, defaultInputModesForKeymap, defaultKeymap, defaultModesForContext, defineFlowProfile, deriveFlowView, deriveGroupGeometry, entityPressMode, extractFlowContent, fixedGroupGeometry, fromJSON, getAnchorTransform, getAutoPanDelta, getConnectedEdges, getEdgeOwner, getGraphBounds, getIncomers, getMinimapProjection, getNeighbors, getOutgoers, getProjectedEdgeAttrs, getProjectedNodeAttrs, getSelectionBounds, getShape, getShapeOrDefault, getSnapLines, getToolbarPlacement, getVisibleBounds, hoverMode, indexChildren, indexEdgeAdjacency, indexEdges, indexNodes, insertFlowContent, isDerivedGroup, isGroup, isTrackpadPinch, keyboardMode, keyboardNavMode, matchesKeyBinding, mergeGraphEdgeAttrs, mergeGraphNodeAttrs, nodeDepths, normalizeWheelPan, panBy, panMode, panePressMode, parseFlowSerialized, pathToSVG, pinchMode, preventDefault, profileModesForContext, profilePoliciesForContext, readonlyConfig, readonlyInputModes, readonlyInputModesForKeymap, reconnectMode, rectContains, rectsIntersect, registerShape, resizeMode, resolveBehavior, resolveGroupPadding, resolveKeymap, resolvedBounds, resolvedEdgeLabelBounds, resolvedEdgeLabelSize, resolvedPosition, screenToCanvas, selectMode, serializeFlowGraph, snapPointToGrid, snapToGrid, snappedBounds, sortActiveGraphProjections, stepRouter, straightRouter, toSVGTransform, toViewBox, trigger, unionRect, validateAndCloneGraph, validateFitFrame, validateViewport, viewportCenteredOn, viewportForBounds, visualBounds, visualPosition, wheelInteraction, wheelMode, wheelZoomDelta, zoomTo };
|
|
9897
|
+
export { CONNECTION_PREVIEW_EDGE_ID, CONNECTION_PREVIEW_NODE_ID, FlowGraphValidationError, animateViewport, applyFlowPatches, areNodesMeasured, avoidRouter, bestDirection, bezierRouter, boxSelectMode, boxToRect, calcAutoPan, canvasToScreen, clampZoom, cloneJsonSafe, composeGraphProjectionAttrs, configureAvoidRouter, configureBezierRouter, configureStepRouter, connectMode, constrainViewport, createConnectionPreviewGraph, createEdge, createEntityChangeNotifier, createFlow, createFlowChangeSet, createFlowEngine, createFlowInputRuntime, createFlowStore, createGraph, createNode, createPort, createViewport, defaultConfig, defaultFlowProfile, defaultInputModes, defaultInputModesForKeymap, defaultKeymap, defaultModesForContext, defineFlowProfile, deriveFlowView, deriveGroupGeometry, entityPressMode, extractFlowContent, fitFrameAroundExclusions, fixedGroupGeometry, fromJSON, getAnchorTransform, getAutoPanDelta, getConnectedEdges, getEdgeOwner, getGraphBounds, getIncomers, getMinimapProjection, getNeighbors, getOutgoers, getProjectedEdgeAttrs, getProjectedNodeAttrs, getSelectionBounds, getShape, getShapeOrDefault, getSnapLines, getToolbarPlacement, getVisibleBounds, hoverMode, indexChildren, indexEdgeAdjacency, indexEdges, indexNodes, insertFlowContent, isDerivedGroup, isGroup, isTrackpadPinch, keyboardMode, keyboardNavMode, matchesKeyBinding, mergeGraphEdgeAttrs, mergeGraphNodeAttrs, nodeDepths, normalizeWheelPan, panBy, panMode, panePressMode, parseFlowSerialized, pathToSVG, pinchMode, preventDefault, profileModesForContext, profilePoliciesForContext, readonlyConfig, readonlyInputModes, readonlyInputModesForKeymap, reconnectMode, rectContains, rectsIntersect, registerShape, resizeMode, resolveBehavior, resolveGroupPadding, resolveKeymap, resolvedBounds, resolvedEdgeLabelBounds, resolvedEdgeLabelSize, resolvedPosition, screenToCanvas, selectMode, serializeFlowGraph, snapPointToGrid, snapToGrid, snappedBounds, sortActiveGraphProjections, stepRouter, straightRouter, toSVGTransform, toViewBox, trigger, unionRect, validateAndCloneGraph, validateFitFrame, validateViewport, viewportCenteredOn, viewportForBounds, visualBounds, visualPosition, wheelInteraction, wheelMode, wheelZoomDelta, zoomTo };
|