@statelyai/flow 0.13.4 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -1
- package/dist/esm/index.d.ts +13 -2
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +39 -14
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/layout/dagre.d.ts +2 -1
- package/dist/esm/layout/dagre.d.ts.map +1 -0
- package/dist/esm/layout/dagre.js +2 -0
- package/dist/esm/layout/dagre.js.map +1 -0
- package/dist/esm/layout/elk.d.ts +2 -1
- package/dist/esm/layout/elk.d.ts.map +1 -0
- package/dist/esm/layout/elk.js +2 -0
- package/dist/esm/layout/elk.js.map +1 -0
- package/dist/esm/layout/stately.d.ts +2 -1
- package/dist/esm/layout/stately.d.ts.map +1 -0
- package/dist/esm/layout/stately.js +2 -0
- package/dist/esm/layout/stately.js.map +1 -0
- package/dist/esm/shared.d.ts +2 -1
- package/dist/esm/shared.d.ts.map +1 -0
- package/dist/esm/shared.js +2 -0
- package/dist/esm/shared.js.map +1 -0
- package/package.json +4 -3
- package/src/__fixtures__/nested.ts +106 -0
- package/src/appearances.test.ts +428 -0
- package/src/appearances.ts +144 -0
- package/src/authoritative-route.ts +14 -0
- package/src/changeSet.test.ts +132 -0
- package/src/changeSet.ts +337 -0
- package/src/config.ts +41 -0
- package/src/configure.test.ts +212 -0
- package/src/connection-lifetime.ts +1 -0
- package/src/connectionPreview.test.ts +102 -0
- package/src/connectionPreview.ts +82 -0
- package/src/content.ts +203 -0
- package/src/createFlow.fit-frame.test.ts +256 -0
- package/src/createFlow.modern.test.ts +743 -0
- package/src/createFlow.test.ts +632 -0
- package/src/createFlow.ts +3463 -0
- package/src/createFlow.view.test.ts +212 -0
- package/src/createFlow.viewport-controls.test.ts +221 -0
- package/src/edge-geometry.ts +318 -0
- package/src/engine.cache.test.ts +728 -0
- package/src/engine.hierarchy.test.ts +1023 -0
- package/src/engine.test.ts +1965 -0
- package/src/engine.ts +3385 -0
- package/src/entity-changes.test.ts +309 -0
- package/src/entity-changes.ts +395 -0
- package/src/examples/flow-based-math.test.ts +25 -0
- package/src/examples/flow-based-math.ts +210 -0
- package/src/external-edit.test.ts +337 -0
- package/src/external-edit.ts +358 -0
- package/src/factories.test.ts +104 -0
- package/src/factories.ts +70 -0
- package/src/geometry.ts +13 -0
- package/src/graph-index.test.ts +56 -0
- package/src/graph-index.ts +194 -0
- package/src/hierarchy.test.ts +175 -0
- package/src/hierarchy.ts +330 -0
- package/src/highlights.test.ts +130 -0
- package/src/index.ts +469 -0
- package/src/input/index.ts +615 -0
- package/src/input/input.test.ts +2577 -0
- package/src/input/keymap.ts +111 -0
- package/src/input/modes.ts +1715 -0
- package/src/input/reparent.test.ts +214 -0
- package/src/layout/dagre.ts +138 -0
- package/src/layout/elk.ts +1382 -0
- package/src/layout/index.ts +12 -0
- package/src/layout/layout.test.ts +1248 -0
- package/src/layout/shared.ts +260 -0
- package/src/layout/stately-globals.d.ts +9 -0
- package/src/layout/stately.test.ts +246 -0
- package/src/layout/stately.ts +528 -0
- package/src/layout-animation.test.ts +223 -0
- package/src/layout-contract.test.ts +414 -0
- package/src/marker.test.ts +23 -0
- package/src/marker.ts +23 -0
- package/src/measurement-anchor.test.ts +232 -0
- package/src/minimap.test.ts +96 -0
- package/src/minimap.ts +137 -0
- package/src/nested-fixture.test.ts +178 -0
- package/src/port-measurement.test.ts +27 -0
- package/src/port-measurements.test.ts +187 -0
- package/src/profile.ts +124 -0
- package/src/projections.test.ts +103 -0
- package/src/projections.ts +295 -0
- package/src/render-invalidation.test.ts +62 -0
- package/src/render-invalidation.ts +55 -0
- package/src/renderer-geometry.test.ts +356 -0
- package/src/renderer-transactions.test.ts +234 -0
- package/src/routing/avoid.ts +417 -0
- package/src/routing/index.test.ts +361 -0
- package/src/routing/index.ts +366 -0
- package/src/routing/readTracking.test.ts +103 -0
- package/src/routing/readTracking.ts +233 -0
- package/src/selection-commands.test.ts +355 -0
- package/src/serialize.test.ts +167 -0
- package/src/serialize.ts +254 -0
- package/src/shapes/index.test.ts +45 -0
- package/src/shapes/index.ts +142 -0
- package/src/snap.test.ts +123 -0
- package/src/snap.ts +395 -0
- package/src/spatial.test.ts +676 -0
- package/src/spatial.ts +457 -0
- package/src/store/index.ts +4121 -0
- package/src/store.test.ts +2903 -0
- package/src/toolbar.test.ts +31 -0
- package/src/toolbar.ts +62 -0
- package/src/types.test.ts +15 -0
- package/src/types.ts +487 -0
- package/src/validation.ts +558 -0
- package/src/view.ts +172 -0
- package/src/viewport/animate.ts +285 -0
- package/src/viewport/index.ts +296 -0
- package/src/viewport.test.ts +569 -0
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import type { FlowEdge, FlowNode, PathData, Point, Rect } from './types';
|
|
2
|
+
import { authoritativeEdgePoints } from './authoritative-route';
|
|
3
|
+
import { isDerivedGroup } from './hierarchy';
|
|
4
|
+
import { resolvedBounds, resolvedPosition } from './spatial';
|
|
5
|
+
|
|
6
|
+
/** A graph-owned route is authoritative whenever it contains valid endpoints. */
|
|
7
|
+
export function authoritativeEdgePath(edge: FlowEdge): PathData | null {
|
|
8
|
+
const storedPoints = authoritativeEdgePoints(edge);
|
|
9
|
+
if (!storedPoints) return null;
|
|
10
|
+
const points = storedPoints.map((point) => ({ ...point }));
|
|
11
|
+
|
|
12
|
+
const source = points[0]!;
|
|
13
|
+
const sourceNext =
|
|
14
|
+
points.find((point) => point.x !== source.x || point.y !== source.y) ??
|
|
15
|
+
points[1]!;
|
|
16
|
+
const target = points.at(-1)!;
|
|
17
|
+
const targetPrevious =
|
|
18
|
+
points.findLast((point) => point.x !== target.x || point.y !== target.y) ??
|
|
19
|
+
points.at(-2)!;
|
|
20
|
+
return {
|
|
21
|
+
points,
|
|
22
|
+
routing: edge.routing ?? 'polyline',
|
|
23
|
+
svgPath: edge.svgPath,
|
|
24
|
+
sourceDirection: directionBetween(source, sourceNext),
|
|
25
|
+
targetDirection: directionBetween(target, targetPrevious),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Remove layout-owned route geometry after an endpoint's geometry changes. */
|
|
30
|
+
export function invalidateAuthoritativeEdgePath(edge: FlowEdge): void {
|
|
31
|
+
delete edge.points;
|
|
32
|
+
delete edge.routing;
|
|
33
|
+
delete edge.svgPath;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Whether a node mutation changes any position an incident edge can anchor to. */
|
|
37
|
+
export function nodeEndpointGeometryChanged(
|
|
38
|
+
previous: FlowNode,
|
|
39
|
+
next: FlowNode,
|
|
40
|
+
): boolean {
|
|
41
|
+
if (JSON.stringify(previous.appearance) !== JSON.stringify(next.appearance)) return true;
|
|
42
|
+
const previousBounds = resolvedBounds(previous);
|
|
43
|
+
const nextBounds = resolvedBounds(next);
|
|
44
|
+
if (
|
|
45
|
+
previousBounds.x !== nextBounds.x ||
|
|
46
|
+
previousBounds.y !== nextBounds.y ||
|
|
47
|
+
previousBounds.width !== nextBounds.width ||
|
|
48
|
+
previousBounds.height !== nextBounds.height
|
|
49
|
+
) {
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const previousPorts = previous.ports ?? [];
|
|
54
|
+
const nextPorts = next.ports ?? [];
|
|
55
|
+
if (previousPorts.length !== nextPorts.length) return true;
|
|
56
|
+
return previousPorts.some((port) => {
|
|
57
|
+
const nextPort = nextPorts.find((candidate) => candidate.name === port.name);
|
|
58
|
+
return (
|
|
59
|
+
!nextPort ||
|
|
60
|
+
port.x !== nextPort.x ||
|
|
61
|
+
port.y !== nextPort.y ||
|
|
62
|
+
port.width !== nextPort.width ||
|
|
63
|
+
port.height !== nextPort.height
|
|
64
|
+
);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Include derived groups whose rendered bounds depend on changed descendants. */
|
|
69
|
+
export function withDerivedGroupAncestors(
|
|
70
|
+
nodes: readonly FlowNode[],
|
|
71
|
+
changedNodeIds: ReadonlySet<string>,
|
|
72
|
+
): Set<string> {
|
|
73
|
+
const affected = new Set(changedNodeIds);
|
|
74
|
+
const nodeIndex = new Map(nodes.map((node) => [node.id, node]));
|
|
75
|
+
for (const changedNodeId of changedNodeIds) {
|
|
76
|
+
let parentId = nodeIndex.get(changedNodeId)?.parentId;
|
|
77
|
+
const visited = new Set<string>();
|
|
78
|
+
while (parentId && !visited.has(parentId)) {
|
|
79
|
+
visited.add(parentId);
|
|
80
|
+
const parent = nodeIndex.get(parentId);
|
|
81
|
+
if (!parent) break;
|
|
82
|
+
if (isDerivedGroup(parent)) affected.add(parent.id);
|
|
83
|
+
parentId = parent.parentId;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return affected;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function isPointNearPath(point: Point, path: PathData, tolerance: number): boolean {
|
|
90
|
+
const samplePoints = samplePathPoints(path);
|
|
91
|
+
|
|
92
|
+
for (let i = 0; i < samplePoints.length - 1; i++) {
|
|
93
|
+
const start = samplePoints[i];
|
|
94
|
+
const end = samplePoints[i + 1];
|
|
95
|
+
if (start && end && distanceToSegment(point, start, end) <= tolerance) {
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Axis-aligned bounding box of a path's control/anchor points. A cubic bezier
|
|
105
|
+
* lies within the convex hull of its control points, so this is a conservative
|
|
106
|
+
* superset of the rendered curve — exact enough for viewport culling (no false
|
|
107
|
+
* negatives) and ~O(points) instead of sampling the curve. Returns null for an
|
|
108
|
+
* empty path.
|
|
109
|
+
*/
|
|
110
|
+
export function pathBounds(path: PathData): Rect | null {
|
|
111
|
+
let minX = Infinity,
|
|
112
|
+
minY = Infinity,
|
|
113
|
+
maxX = -Infinity,
|
|
114
|
+
maxY = -Infinity;
|
|
115
|
+
for (const point of path.points) {
|
|
116
|
+
if (point.x < minX) minX = point.x;
|
|
117
|
+
if (point.y < minY) minY = point.y;
|
|
118
|
+
if (point.x > maxX) maxX = point.x;
|
|
119
|
+
if (point.y > maxY) maxY = point.y;
|
|
120
|
+
}
|
|
121
|
+
if (!isFinite(minX)) return null;
|
|
122
|
+
return { x: minX, y: minY, width: maxX - minX, height: maxY - minY };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function rectIntersectsPath(rect: Rect, path: PathData): boolean {
|
|
126
|
+
const samplePoints = samplePathPoints(path);
|
|
127
|
+
|
|
128
|
+
for (const point of samplePoints) {
|
|
129
|
+
if (pointInRect(point, rect)) {
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const corners = rectCorners(rect);
|
|
135
|
+
for (let i = 0; i < samplePoints.length - 1; i++) {
|
|
136
|
+
const start = samplePoints[i];
|
|
137
|
+
const end = samplePoints[i + 1];
|
|
138
|
+
if (!start || !end) continue;
|
|
139
|
+
|
|
140
|
+
if (pointInRect(start, rect) || pointInRect(end, rect)) {
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
for (let cornerIndex = 0; cornerIndex < corners.length; cornerIndex++) {
|
|
145
|
+
const edgeStart = corners[cornerIndex];
|
|
146
|
+
const edgeEnd = corners[(cornerIndex + 1) % corners.length];
|
|
147
|
+
if (edgeStart && edgeEnd && segmentsIntersect(start, end, edgeStart, edgeEnd)) {
|
|
148
|
+
return true;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function basicEdgePath(edge: FlowEdge, nodes: FlowNode[]): PathData | null {
|
|
157
|
+
const authoritative = authoritativeEdgePath(edge);
|
|
158
|
+
if (authoritative) return authoritative;
|
|
159
|
+
|
|
160
|
+
const source = nodes.find((node) => node.id === edge.sourceId);
|
|
161
|
+
const target = nodes.find((node) => node.id === edge.targetId);
|
|
162
|
+
if (!source || !target) {
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return {
|
|
167
|
+
points: [
|
|
168
|
+
resolveBasicEndpoint(source, edge.sourcePort),
|
|
169
|
+
resolveBasicEndpoint(target, edge.targetPort),
|
|
170
|
+
],
|
|
171
|
+
sourceDirection: 'right',
|
|
172
|
+
targetDirection: 'left',
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function directionBetween(from: Point, to: Point): PathData['sourceDirection'] {
|
|
177
|
+
const dx = to.x - from.x;
|
|
178
|
+
const dy = to.y - from.y;
|
|
179
|
+
if (Math.abs(dx) >= Math.abs(dy)) return dx >= 0 ? 'right' : 'left';
|
|
180
|
+
return dy >= 0 ? 'bottom' : 'top';
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function resolveBasicEndpoint(node: FlowNode, portName: string | null | undefined): Point {
|
|
184
|
+
if (portName && portName !== '*' && node.ports) {
|
|
185
|
+
const port = node.ports.find((candidate) => candidate.name === portName);
|
|
186
|
+
if (port) {
|
|
187
|
+
const position = resolvedPosition(node);
|
|
188
|
+
return {
|
|
189
|
+
x: position.x + port.x + port.width / 2,
|
|
190
|
+
y: position.y + port.y + port.height / 2,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const bounds = resolvedBounds(node);
|
|
196
|
+
return {
|
|
197
|
+
x: bounds.x + bounds.width / 2,
|
|
198
|
+
y: bounds.y + bounds.height / 2,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function samplePathPoints(path: PathData): Point[] {
|
|
203
|
+
if (
|
|
204
|
+
path.routing === 'splines' &&
|
|
205
|
+
path.points.length >= 4 &&
|
|
206
|
+
(path.points.length - 1) % 3 === 0
|
|
207
|
+
) {
|
|
208
|
+
const sampled: Point[] = [];
|
|
209
|
+
for (let index = 0; index + 3 < path.points.length; index += 3) {
|
|
210
|
+
const [p0, p1, p2, p3] = path.points.slice(index, index + 4);
|
|
211
|
+
if (!p0 || !p1 || !p2 || !p3) continue;
|
|
212
|
+
const curve = sampleCubicBezier(p0, p1, p2, p3, 20);
|
|
213
|
+
sampled.push(...(sampled.length ? curve.slice(1) : curve));
|
|
214
|
+
}
|
|
215
|
+
return sampled.length ? sampled : path.points;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (!path.routing && path.points.length === 4) {
|
|
219
|
+
const [p0, p1, p2, p3] = path.points;
|
|
220
|
+
if (p0 && p1 && p2 && p3) {
|
|
221
|
+
return sampleCubicBezier(p0, p1, p2, p3, 20);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return path.points;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function sampleCubicBezier(
|
|
229
|
+
p0: Point,
|
|
230
|
+
p1: Point,
|
|
231
|
+
p2: Point,
|
|
232
|
+
p3: Point,
|
|
233
|
+
steps: number,
|
|
234
|
+
): Point[] {
|
|
235
|
+
const points: Point[] = [];
|
|
236
|
+
for (let i = 0; i <= steps; i++) {
|
|
237
|
+
const t = i / steps;
|
|
238
|
+
const mt = 1 - t;
|
|
239
|
+
points.push({
|
|
240
|
+
x:
|
|
241
|
+
mt ** 3 * p0.x +
|
|
242
|
+
3 * mt ** 2 * t * p1.x +
|
|
243
|
+
3 * mt * t ** 2 * p2.x +
|
|
244
|
+
t ** 3 * p3.x,
|
|
245
|
+
y:
|
|
246
|
+
mt ** 3 * p0.y +
|
|
247
|
+
3 * mt ** 2 * t * p1.y +
|
|
248
|
+
3 * mt * t ** 2 * p2.y +
|
|
249
|
+
t ** 3 * p3.y,
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
return points;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function distanceToSegment(point: Point, start: Point, end: Point): number {
|
|
256
|
+
const dx = end.x - start.x;
|
|
257
|
+
const dy = end.y - start.y;
|
|
258
|
+
|
|
259
|
+
if (dx === 0 && dy === 0) {
|
|
260
|
+
return Math.hypot(point.x - start.x, point.y - start.y);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const t = Math.max(
|
|
264
|
+
0,
|
|
265
|
+
Math.min(1, ((point.x - start.x) * dx + (point.y - start.y) * dy) / (dx * dx + dy * dy)),
|
|
266
|
+
);
|
|
267
|
+
|
|
268
|
+
return Math.hypot(point.x - (start.x + t * dx), point.y - (start.y + t * dy));
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function pointInRect(point: Point, rect: Rect): boolean {
|
|
272
|
+
return (
|
|
273
|
+
point.x >= rect.x &&
|
|
274
|
+
point.x <= rect.x + rect.width &&
|
|
275
|
+
point.y >= rect.y &&
|
|
276
|
+
point.y <= rect.y + rect.height
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function rectCorners(rect: Rect): Point[] {
|
|
281
|
+
return [
|
|
282
|
+
{ x: rect.x, y: rect.y },
|
|
283
|
+
{ x: rect.x + rect.width, y: rect.y },
|
|
284
|
+
{ x: rect.x + rect.width, y: rect.y + rect.height },
|
|
285
|
+
{ x: rect.x, y: rect.y + rect.height },
|
|
286
|
+
];
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function segmentsIntersect(a1: Point, a2: Point, b1: Point, b2: Point): boolean {
|
|
290
|
+
const d1 = direction(b1, b2, a1);
|
|
291
|
+
const d2 = direction(b1, b2, a2);
|
|
292
|
+
const d3 = direction(a1, a2, b1);
|
|
293
|
+
const d4 = direction(a1, a2, b2);
|
|
294
|
+
|
|
295
|
+
if (((d1 > 0 && d2 < 0) || (d1 < 0 && d2 > 0)) && ((d3 > 0 && d4 < 0) || (d3 < 0 && d4 > 0))) {
|
|
296
|
+
return true;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
return (
|
|
300
|
+
(d1 === 0 && onSegment(b1, b2, a1)) ||
|
|
301
|
+
(d2 === 0 && onSegment(b1, b2, a2)) ||
|
|
302
|
+
(d3 === 0 && onSegment(a1, a2, b1)) ||
|
|
303
|
+
(d4 === 0 && onSegment(a1, a2, b2))
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function direction(a: Point, b: Point, c: Point): number {
|
|
308
|
+
return (c.x - a.x) * (b.y - a.y) - (c.y - a.y) * (b.x - a.x);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function onSegment(a: Point, b: Point, c: Point): boolean {
|
|
312
|
+
return (
|
|
313
|
+
Math.min(a.x, b.x) <= c.x &&
|
|
314
|
+
c.x <= Math.max(a.x, b.x) &&
|
|
315
|
+
Math.min(a.y, b.y) <= c.y &&
|
|
316
|
+
c.y <= Math.max(a.y, b.y)
|
|
317
|
+
);
|
|
318
|
+
}
|