@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,528 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getLayeredLayout,
|
|
3
|
+
type LayeredLayoutOptions,
|
|
4
|
+
} from '@statelyai/layout/layered';
|
|
5
|
+
import { getLayout } from '@statelyai/layout';
|
|
6
|
+
import { getLCA, type GraphEdge, type VisualGraph } from '@statelyai/graph';
|
|
7
|
+
import type {
|
|
8
|
+
EdgeRouting,
|
|
9
|
+
FlowGraph,
|
|
10
|
+
FlowNode,
|
|
11
|
+
LayoutResult,
|
|
12
|
+
Point,
|
|
13
|
+
Rect,
|
|
14
|
+
} from '@statelyai/flow';
|
|
15
|
+
import {
|
|
16
|
+
deriveGroupGeometry,
|
|
17
|
+
fixedGroupGeometry,
|
|
18
|
+
isGroup,
|
|
19
|
+
resolveGroupPadding,
|
|
20
|
+
} from '@statelyai/flow';
|
|
21
|
+
import {
|
|
22
|
+
boundsOf,
|
|
23
|
+
graphForLayoutScope,
|
|
24
|
+
layoutRunContext,
|
|
25
|
+
mergedLayoutScope,
|
|
26
|
+
throwIfAborted,
|
|
27
|
+
type FlowLayoutAlgorithm,
|
|
28
|
+
type FlowLayoutOptions,
|
|
29
|
+
} from './shared';
|
|
30
|
+
|
|
31
|
+
export type StatelyLayoutOptions<
|
|
32
|
+
TNodeData = any,
|
|
33
|
+
TEdgeData = any,
|
|
34
|
+
TLabelData = any,
|
|
35
|
+
> = Omit<LayeredLayoutOptions, 'measure'> &
|
|
36
|
+
Pick<
|
|
37
|
+
FlowLayoutOptions<TNodeData, TEdgeData, TLabelData>,
|
|
38
|
+
'getBounds' | 'scope' | 'source' | 'metadata' | '_types'
|
|
39
|
+
>;
|
|
40
|
+
|
|
41
|
+
/** Graph-native layered layout powered by `@statelyai/layout`. */
|
|
42
|
+
export const statelyLayout = <
|
|
43
|
+
TNodeData = any,
|
|
44
|
+
TEdgeData = any,
|
|
45
|
+
TLabelData = any,
|
|
46
|
+
>(
|
|
47
|
+
options: StatelyLayoutOptions<TNodeData, TEdgeData, TLabelData> = {},
|
|
48
|
+
): FlowLayoutAlgorithm<TNodeData, TEdgeData, TLabelData> => {
|
|
49
|
+
return async (graph, runOptions = {}) => {
|
|
50
|
+
throwIfAborted(runOptions);
|
|
51
|
+
const mutableGraph = graph as FlowGraph<
|
|
52
|
+
TNodeData,
|
|
53
|
+
TEdgeData,
|
|
54
|
+
TLabelData
|
|
55
|
+
>;
|
|
56
|
+
const scope = mergedLayoutScope(mutableGraph, options.scope, runOptions);
|
|
57
|
+
const layoutGraph = graphForLayoutScope(mutableGraph, scope);
|
|
58
|
+
const context = layoutRunContext(
|
|
59
|
+
mutableGraph,
|
|
60
|
+
options,
|
|
61
|
+
runOptions,
|
|
62
|
+
'stately',
|
|
63
|
+
);
|
|
64
|
+
const input = toStatelyGraph(layoutGraph, options.getBounds);
|
|
65
|
+
const native = await getLayout({
|
|
66
|
+
graph: input,
|
|
67
|
+
algorithm: 'layered',
|
|
68
|
+
options: nativeOptions(options),
|
|
69
|
+
...(runOptions.signal
|
|
70
|
+
? { signal: runOptions.signal as unknown as AbortSignal }
|
|
71
|
+
: {}),
|
|
72
|
+
});
|
|
73
|
+
context.diagnostics.push(...native.diagnostics.map((entry) => ({ ...entry })));
|
|
74
|
+
const output = applyFlowGroupContracts(
|
|
75
|
+
native.graph,
|
|
76
|
+
layoutGraph,
|
|
77
|
+
nativeOptions(options),
|
|
78
|
+
options.getBounds,
|
|
79
|
+
);
|
|
80
|
+
throwIfAborted(runOptions);
|
|
81
|
+
return fromStatelyGraph(
|
|
82
|
+
output,
|
|
83
|
+
layoutGraph,
|
|
84
|
+
promotedRootOffsets(layoutGraph, mutableGraph, options.getBounds),
|
|
85
|
+
context.fixedIds,
|
|
86
|
+
options.getBounds,
|
|
87
|
+
new Set(layoutGraph.edges.filter((edge) => edge.label).map((edge) => edge.id)),
|
|
88
|
+
{
|
|
89
|
+
source: context.source,
|
|
90
|
+
metadata: {
|
|
91
|
+
...context.metadata,
|
|
92
|
+
algorithm: 'layered',
|
|
93
|
+
},
|
|
94
|
+
diagnostics: context.diagnostics,
|
|
95
|
+
},
|
|
96
|
+
);
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
function nativeOptions(
|
|
101
|
+
options: StatelyLayoutOptions,
|
|
102
|
+
): LayeredLayoutOptions {
|
|
103
|
+
const {
|
|
104
|
+
getBounds: _getBounds,
|
|
105
|
+
scope: _scope,
|
|
106
|
+
source: _source,
|
|
107
|
+
metadata: _metadata,
|
|
108
|
+
_types,
|
|
109
|
+
...native
|
|
110
|
+
} = options;
|
|
111
|
+
void _types;
|
|
112
|
+
return native;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function promotedRootOffsets(
|
|
116
|
+
scoped: FlowGraph,
|
|
117
|
+
full: FlowGraph,
|
|
118
|
+
getBounds: ((node: FlowNode) => Rect) | undefined,
|
|
119
|
+
): ReadonlyMap<string, Point> {
|
|
120
|
+
if (scoped === full) return new Map();
|
|
121
|
+
const scopedById = new Map(scoped.nodes.map((node) => [node.id, node]));
|
|
122
|
+
const fullById = new Map(full.nodes.map((node) => [node.id, node]));
|
|
123
|
+
return new Map(
|
|
124
|
+
scoped.nodes.flatMap((node) => {
|
|
125
|
+
const original = fullById.get(node.id);
|
|
126
|
+
if (!original?.parentId || scopedById.has(original.parentId)) return [];
|
|
127
|
+
const parent = fullById.get(original.parentId);
|
|
128
|
+
return parent ? [[node.id, boundsOf(parent, getBounds)] as const] : [];
|
|
129
|
+
}),
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function applyFlowGroupContracts(
|
|
134
|
+
output: VisualGraph,
|
|
135
|
+
input: FlowGraph,
|
|
136
|
+
options: LayeredLayoutOptions,
|
|
137
|
+
getBounds: ((node: FlowNode) => Rect) | undefined,
|
|
138
|
+
): VisualGraph {
|
|
139
|
+
const nodes = output.nodes.map((node) => ({ ...node }));
|
|
140
|
+
const edges = output.edges.map((edge) => ({ ...edge }));
|
|
141
|
+
const nodeById = new Map(nodes.map((node) => [node.id, node]));
|
|
142
|
+
const inputById = new Map(input.nodes.map((node) => [node.id, node]));
|
|
143
|
+
const edgeById = new Map(edges.map((edge) => [edge.id, edge]));
|
|
144
|
+
const depth = (id: string): number => {
|
|
145
|
+
let value = 0;
|
|
146
|
+
let parentId = nodeById.get(id)?.parentId;
|
|
147
|
+
const seen = new Set<string>();
|
|
148
|
+
while (parentId && !seen.has(parentId)) {
|
|
149
|
+
seen.add(parentId);
|
|
150
|
+
value++;
|
|
151
|
+
parentId = nodeById.get(parentId)?.parentId;
|
|
152
|
+
}
|
|
153
|
+
return value;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
const layoutSiblings = (
|
|
157
|
+
parentId: string | undefined,
|
|
158
|
+
direction: FlowGraph['direction'],
|
|
159
|
+
padding: LayeredLayoutOptions['padding'] = 0,
|
|
160
|
+
): void => {
|
|
161
|
+
const siblings = nodes.filter(
|
|
162
|
+
(node) => (node.parentId ?? undefined) === parentId,
|
|
163
|
+
);
|
|
164
|
+
if (siblings.length === 0) return;
|
|
165
|
+
const siblingIds = new Set(siblings.map((node) => node.id));
|
|
166
|
+
const owner = (id: string): string | undefined => {
|
|
167
|
+
let node = nodeById.get(id);
|
|
168
|
+
const seen = new Set<string>();
|
|
169
|
+
while (node && !seen.has(node.id)) {
|
|
170
|
+
seen.add(node.id);
|
|
171
|
+
if (siblingIds.has(node.id)) return node.id;
|
|
172
|
+
node = node.parentId ? nodeById.get(node.parentId) : undefined;
|
|
173
|
+
}
|
|
174
|
+
return undefined;
|
|
175
|
+
};
|
|
176
|
+
const siblingEdges = edges.flatMap((edge) => {
|
|
177
|
+
const sourceId = owner(edge.sourceId);
|
|
178
|
+
const targetId = owner(edge.targetId);
|
|
179
|
+
if (!sourceId || !targetId || sourceId === targetId) return [];
|
|
180
|
+
if (sourceId === edge.sourceId && targetId === edge.targetId) return [edge];
|
|
181
|
+
return [
|
|
182
|
+
{
|
|
183
|
+
...edge,
|
|
184
|
+
id: `__flow_layout_proxy__${edge.id}`,
|
|
185
|
+
sourceId,
|
|
186
|
+
targetId,
|
|
187
|
+
x: 0,
|
|
188
|
+
y: 0,
|
|
189
|
+
width: 0,
|
|
190
|
+
height: 0,
|
|
191
|
+
points: undefined,
|
|
192
|
+
},
|
|
193
|
+
];
|
|
194
|
+
});
|
|
195
|
+
const laidOut = getLayeredLayout(
|
|
196
|
+
{
|
|
197
|
+
...output,
|
|
198
|
+
direction,
|
|
199
|
+
nodes: siblings.map((node) => ({ ...node, parentId: undefined })),
|
|
200
|
+
edges: siblingEdges,
|
|
201
|
+
},
|
|
202
|
+
{ ...options, direction, padding },
|
|
203
|
+
);
|
|
204
|
+
for (const node of laidOut.nodes) {
|
|
205
|
+
Object.assign(nodeById.get(node.id)!, node, { parentId });
|
|
206
|
+
}
|
|
207
|
+
for (const edge of laidOut.edges) {
|
|
208
|
+
if (edgeById.has(edge.id)) edgeById.set(edge.id, edge);
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
const groups = input.nodes
|
|
213
|
+
.filter(isGroup)
|
|
214
|
+
.sort((left, right) => depth(right.id) - depth(left.id));
|
|
215
|
+
for (const group of groups) {
|
|
216
|
+
const node = nodeById.get(group.id);
|
|
217
|
+
if (!node) continue;
|
|
218
|
+
const children = nodes.filter((child) => child.parentId === group.id);
|
|
219
|
+
if (children.length === 0) continue;
|
|
220
|
+
layoutSiblings(group.id, group.group!.direction ?? input.direction);
|
|
221
|
+
const union = boundsUnion(children);
|
|
222
|
+
const original = boundsOf(inputById.get(group.id)!, getBounds);
|
|
223
|
+
const contentSize =
|
|
224
|
+
group.group!.type === 'fixed'
|
|
225
|
+
? (group.group!.contentSize ?? { width: 0, height: 0 })
|
|
226
|
+
: { width: original.width, height: original.height };
|
|
227
|
+
const normalizedUnion = { ...union, x: 0, y: 0 };
|
|
228
|
+
const geometry =
|
|
229
|
+
group.group!.type === 'fixed'
|
|
230
|
+
? fixedGroupGeometry(
|
|
231
|
+
{ x: 0, y: 0, width: original.width, height: original.height },
|
|
232
|
+
contentSize,
|
|
233
|
+
group.group!,
|
|
234
|
+
)
|
|
235
|
+
: deriveGroupGeometry(contentSize, normalizedUnion, group.group!);
|
|
236
|
+
const derivedPadding =
|
|
237
|
+
group.group!.type === 'derived'
|
|
238
|
+
? resolveGroupPadding(group.group!.padding)
|
|
239
|
+
: { top: 0, left: 0 };
|
|
240
|
+
const target = {
|
|
241
|
+
x: geometry.childArea.x - geometry.bounds.x + derivedPadding.left,
|
|
242
|
+
y: geometry.childArea.y - geometry.bounds.y + derivedPadding.top,
|
|
243
|
+
};
|
|
244
|
+
const dx = target.x - union.x;
|
|
245
|
+
const dy = target.y - union.y;
|
|
246
|
+
shiftChildrenAndOwnedEdges(children, edgeById, dx, dy);
|
|
247
|
+
node.width = geometry.bounds.width;
|
|
248
|
+
node.height = geometry.bounds.height;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
layoutSiblings(
|
|
252
|
+
undefined,
|
|
253
|
+
options.direction ?? input.direction,
|
|
254
|
+
options.padding,
|
|
255
|
+
);
|
|
256
|
+
routeCrossBoundaryEdges(
|
|
257
|
+
{ ...output, nodes, edges: edges.map((edge) => edgeById.get(edge.id) ?? edge) },
|
|
258
|
+
options.direction ?? input.direction ?? 'down',
|
|
259
|
+
);
|
|
260
|
+
return {
|
|
261
|
+
...output,
|
|
262
|
+
nodes,
|
|
263
|
+
edges: edges.map((edge) => edgeById.get(edge.id) ?? edge),
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function routeCrossBoundaryEdges(
|
|
268
|
+
graph: VisualGraph,
|
|
269
|
+
direction: NonNullable<FlowGraph['direction']>,
|
|
270
|
+
): void {
|
|
271
|
+
const nodes = new Map(graph.nodes.map((node) => [node.id, node]));
|
|
272
|
+
const absolute = (id: string): Point => {
|
|
273
|
+
const node = nodes.get(id);
|
|
274
|
+
if (!node) return { x: 0, y: 0 };
|
|
275
|
+
const parent = node.parentId ? absolute(node.parentId) : { x: 0, y: 0 };
|
|
276
|
+
return { x: parent.x + node.x, y: parent.y + node.y };
|
|
277
|
+
};
|
|
278
|
+
const horizontal = direction === 'left' || direction === 'right';
|
|
279
|
+
const reverse = direction === 'left' || direction === 'up';
|
|
280
|
+
for (const edge of graph.edges) {
|
|
281
|
+
const sourceNode = nodes.get(edge.sourceId);
|
|
282
|
+
const targetNode = nodes.get(edge.targetId);
|
|
283
|
+
if (!sourceNode || !targetNode || sourceNode.parentId === targetNode.parentId) {
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
const source = { ...absolute(sourceNode.id), width: sourceNode.width, height: sourceNode.height };
|
|
287
|
+
const target = { ...absolute(targetNode.id), width: targetNode.width, height: targetNode.height };
|
|
288
|
+
const start = horizontal
|
|
289
|
+
? {
|
|
290
|
+
x: reverse ? source.x : source.x + source.width,
|
|
291
|
+
y: source.y + source.height / 2,
|
|
292
|
+
}
|
|
293
|
+
: {
|
|
294
|
+
x: source.x + source.width / 2,
|
|
295
|
+
y: reverse ? source.y : source.y + source.height,
|
|
296
|
+
};
|
|
297
|
+
const end = horizontal
|
|
298
|
+
? {
|
|
299
|
+
x: reverse ? target.x + target.width : target.x,
|
|
300
|
+
y: target.y + target.height / 2,
|
|
301
|
+
}
|
|
302
|
+
: {
|
|
303
|
+
x: target.x + target.width / 2,
|
|
304
|
+
y: reverse ? target.y + target.height : target.y,
|
|
305
|
+
};
|
|
306
|
+
const track = horizontal
|
|
307
|
+
? { x: (start.x + end.x) / 2, y: 0 }
|
|
308
|
+
: { x: 0, y: (start.y + end.y) / 2 };
|
|
309
|
+
const container = getLCA(graph, edge.sourceId, edge.targetId);
|
|
310
|
+
const origin = container ? absolute(container.id) : { x: 0, y: 0 };
|
|
311
|
+
const points = horizontal
|
|
312
|
+
? [start, { x: track.x, y: start.y }, { x: track.x, y: end.y }, end]
|
|
313
|
+
: [start, { x: start.x, y: track.y }, { x: end.x, y: track.y }, end];
|
|
314
|
+
edge.points = points.map((point) => ({
|
|
315
|
+
x: point.x - origin.x,
|
|
316
|
+
y: point.y - origin.y,
|
|
317
|
+
}));
|
|
318
|
+
edge.routing = 'orthogonal';
|
|
319
|
+
edge.x = (start.x + end.x) / 2 - origin.x - edge.width / 2;
|
|
320
|
+
edge.y = (start.y + end.y) / 2 - origin.y - edge.height / 2;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function boundsUnion(nodes: readonly { x: number; y: number; width: number; height: number }[]): Rect {
|
|
325
|
+
const left = Math.min(...nodes.map((node) => node.x));
|
|
326
|
+
const top = Math.min(...nodes.map((node) => node.y));
|
|
327
|
+
const right = Math.max(...nodes.map((node) => node.x + node.width));
|
|
328
|
+
const bottom = Math.max(...nodes.map((node) => node.y + node.height));
|
|
329
|
+
return { x: left, y: top, width: right - left, height: bottom - top };
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function shiftChildrenAndOwnedEdges(
|
|
333
|
+
children: readonly { id: string; x: number; y: number }[],
|
|
334
|
+
edges: Map<string, GraphEdge & Rect>,
|
|
335
|
+
dx: number,
|
|
336
|
+
dy: number,
|
|
337
|
+
): void {
|
|
338
|
+
const ids = new Set(children.map((node) => node.id));
|
|
339
|
+
for (const child of children) {
|
|
340
|
+
child.x += dx;
|
|
341
|
+
child.y += dy;
|
|
342
|
+
}
|
|
343
|
+
for (const edge of edges.values()) {
|
|
344
|
+
if (!ids.has(edge.sourceId) || !ids.has(edge.targetId)) continue;
|
|
345
|
+
edge.x += dx;
|
|
346
|
+
edge.y += dy;
|
|
347
|
+
if (edge.points) {
|
|
348
|
+
edge.points = edge.points.map((point) => ({
|
|
349
|
+
x: point.x + dx,
|
|
350
|
+
y: point.y + dy,
|
|
351
|
+
}));
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
function toStatelyGraph<TNodeData, TEdgeData, TLabelData>(
|
|
357
|
+
graph: FlowGraph<TNodeData, TEdgeData, TLabelData>,
|
|
358
|
+
getBounds?: (node: FlowNode<TNodeData>) => Rect,
|
|
359
|
+
): VisualGraph<TNodeData, TEdgeData> {
|
|
360
|
+
const boundsById = new Map(
|
|
361
|
+
graph.nodes.map((node) => [node.id, boundsOf(node, getBounds)]),
|
|
362
|
+
);
|
|
363
|
+
return {
|
|
364
|
+
...graph,
|
|
365
|
+
id: graph.id ?? 'flow',
|
|
366
|
+
mode: graph.mode ?? 'directed',
|
|
367
|
+
data: graph.data ?? {},
|
|
368
|
+
direction: graph.direction ?? 'down',
|
|
369
|
+
nodes: graph.nodes.map((node) => {
|
|
370
|
+
const bounds = boundsById.get(node.id)!;
|
|
371
|
+
const parent = node.parentId ? boundsById.get(node.parentId) : undefined;
|
|
372
|
+
return {
|
|
373
|
+
...node,
|
|
374
|
+
...bounds,
|
|
375
|
+
x: bounds.x - (parent?.x ?? 0),
|
|
376
|
+
y: bounds.y - (parent?.y ?? 0),
|
|
377
|
+
};
|
|
378
|
+
}),
|
|
379
|
+
edges: graph.edges.map((edge) => {
|
|
380
|
+
const container = getLCA(graph as VisualGraph, edge.sourceId, edge.targetId);
|
|
381
|
+
const origin = container
|
|
382
|
+
? boundsById.get(container.id) ?? { x: 0, y: 0 }
|
|
383
|
+
: { x: 0, y: 0 };
|
|
384
|
+
return {
|
|
385
|
+
...edge,
|
|
386
|
+
x: (edge.label?.x ?? edge.x ?? 0) - origin.x,
|
|
387
|
+
y: (edge.label?.y ?? edge.y ?? 0) - origin.y,
|
|
388
|
+
width: edge.label?.width ?? edge.width ?? 0,
|
|
389
|
+
height: edge.label?.height ?? edge.height ?? 0,
|
|
390
|
+
...(edge.points
|
|
391
|
+
? {
|
|
392
|
+
points: edge.points.map((point) => ({
|
|
393
|
+
x: point.x - origin.x,
|
|
394
|
+
y: point.y - origin.y,
|
|
395
|
+
})),
|
|
396
|
+
}
|
|
397
|
+
: {}),
|
|
398
|
+
};
|
|
399
|
+
}) as (GraphEdge<TEdgeData> & Rect)[],
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
function fromStatelyGraph(
|
|
404
|
+
graph: VisualGraph,
|
|
405
|
+
input: FlowGraph,
|
|
406
|
+
rootOffsets: ReadonlyMap<string, Point>,
|
|
407
|
+
fixedIds: ReadonlySet<string>,
|
|
408
|
+
getBounds: ((node: FlowNode) => Rect) | undefined,
|
|
409
|
+
labelEdgeIds: ReadonlySet<string>,
|
|
410
|
+
details: {
|
|
411
|
+
source: string;
|
|
412
|
+
metadata: Readonly<Record<string, unknown>>;
|
|
413
|
+
diagnostics: LayoutResult['diagnostics'];
|
|
414
|
+
},
|
|
415
|
+
): LayoutResult {
|
|
416
|
+
const nodesById = new Map(graph.nodes.map((node) => [node.id, node]));
|
|
417
|
+
const inputNodesById = new Map(input.nodes.map((node) => [node.id, node]));
|
|
418
|
+
const absolutePositions = new Map<string, Point>();
|
|
419
|
+
const absolutePosition = (id: string, visiting = new Set<string>()): Point => {
|
|
420
|
+
const cached = absolutePositions.get(id);
|
|
421
|
+
if (cached) return cached;
|
|
422
|
+
const node = nodesById.get(id);
|
|
423
|
+
if (!node || visiting.has(id)) return { x: 0, y: 0 };
|
|
424
|
+
if (fixedIds.has(id)) {
|
|
425
|
+
const fixed = inputNodesById.get(id);
|
|
426
|
+
if (fixed) {
|
|
427
|
+
const position = boundsOf(fixed, getBounds);
|
|
428
|
+
absolutePositions.set(id, position);
|
|
429
|
+
return position;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
visiting.add(id);
|
|
433
|
+
const parent = node.parentId
|
|
434
|
+
? absolutePosition(node.parentId, visiting)
|
|
435
|
+
: (rootOffsets.get(node.id) ?? { x: 0, y: 0 });
|
|
436
|
+
const position = { x: parent.x + node.x, y: parent.y + node.y };
|
|
437
|
+
absolutePositions.set(id, position);
|
|
438
|
+
return position;
|
|
439
|
+
};
|
|
440
|
+
const rootAncestorId = (id: string): string => {
|
|
441
|
+
let node = nodesById.get(id);
|
|
442
|
+
const seen = new Set<string>();
|
|
443
|
+
while (node?.parentId && !seen.has(node.id)) {
|
|
444
|
+
seen.add(node.id);
|
|
445
|
+
node = nodesById.get(node.parentId);
|
|
446
|
+
}
|
|
447
|
+
return node?.id ?? id;
|
|
448
|
+
};
|
|
449
|
+
const rootOffsetOf = (id: string): Point =>
|
|
450
|
+
rootOffsets.get(rootAncestorId(id)) ?? { x: 0, y: 0 };
|
|
451
|
+
const positions = new Map<string, Point>();
|
|
452
|
+
const nodeSizes = new Map<string, { width: number; height: number }>();
|
|
453
|
+
const nodeBounds = new Map<string, Rect>();
|
|
454
|
+
for (const node of graph.nodes) {
|
|
455
|
+
if (fixedIds.has(node.id)) continue;
|
|
456
|
+
const position = absolutePosition(node.id);
|
|
457
|
+
const size = { width: node.width, height: node.height };
|
|
458
|
+
positions.set(node.id, position);
|
|
459
|
+
nodeSizes.set(node.id, size);
|
|
460
|
+
nodeBounds.set(node.id, { ...position, ...size });
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
const edgeLabelBounds = new Map<string, Rect>();
|
|
464
|
+
const edgeRoutes = new Map<
|
|
465
|
+
string,
|
|
466
|
+
{ points: Point[]; routing?: EdgeRouting }
|
|
467
|
+
>();
|
|
468
|
+
for (const edge of graph.edges) {
|
|
469
|
+
if (fixedIds.has(edge.id)) continue;
|
|
470
|
+
if (fixedIds.has(edge.sourceId) || fixedIds.has(edge.targetId)) continue;
|
|
471
|
+
const container = getLCA(graph, edge.sourceId, edge.targetId);
|
|
472
|
+
const sourceOffset = rootOffsetOf(edge.sourceId);
|
|
473
|
+
const targetOffset = rootOffsetOf(edge.targetId);
|
|
474
|
+
if (
|
|
475
|
+
!container &&
|
|
476
|
+
(sourceOffset.x !== targetOffset.x || sourceOffset.y !== targetOffset.y)
|
|
477
|
+
) {
|
|
478
|
+
continue;
|
|
479
|
+
}
|
|
480
|
+
const origin = container ? absolutePosition(container.id) : sourceOffset;
|
|
481
|
+
if (
|
|
482
|
+
labelEdgeIds.has(edge.id) &&
|
|
483
|
+
Number.isFinite(edge.x) &&
|
|
484
|
+
Number.isFinite(edge.y) &&
|
|
485
|
+
Number.isFinite(edge.width) &&
|
|
486
|
+
Number.isFinite(edge.height)
|
|
487
|
+
) {
|
|
488
|
+
edgeLabelBounds.set(edge.id, {
|
|
489
|
+
x: origin.x + edge.x,
|
|
490
|
+
y: origin.y + edge.y,
|
|
491
|
+
width: edge.width,
|
|
492
|
+
height: edge.height,
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
if (edge.points && edge.points.length >= 2) {
|
|
496
|
+
edgeRoutes.set(edge.id, {
|
|
497
|
+
points: edge.points.map((point) => ({
|
|
498
|
+
x: origin.x + point.x,
|
|
499
|
+
y: origin.y + point.y,
|
|
500
|
+
})),
|
|
501
|
+
...(edge.routing ? { routing: edge.routing } : {}),
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
return {
|
|
507
|
+
positions,
|
|
508
|
+
nodeSizes,
|
|
509
|
+
nodeBounds,
|
|
510
|
+
...(edgeLabelBounds.size > 0
|
|
511
|
+
? {
|
|
512
|
+
edgeLabelBounds,
|
|
513
|
+
edgeLabelPositions: new Map(
|
|
514
|
+
[...edgeLabelBounds].map(([id, bounds]) => [
|
|
515
|
+
id,
|
|
516
|
+
{ x: bounds.x, y: bounds.y },
|
|
517
|
+
]),
|
|
518
|
+
),
|
|
519
|
+
}
|
|
520
|
+
: {}),
|
|
521
|
+
...(edgeRoutes.size > 0 ? { edgeRoutes } : {}),
|
|
522
|
+
...(details.diagnostics?.length
|
|
523
|
+
? { diagnostics: details.diagnostics }
|
|
524
|
+
: {}),
|
|
525
|
+
source: details.source,
|
|
526
|
+
metadata: details.metadata,
|
|
527
|
+
};
|
|
528
|
+
}
|