@statelyai/flow 0.14.0 → 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 +2 -1
- package/dist/esm/index.d.ts +5 -2
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +19 -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,260 @@
|
|
|
1
|
+
import {
|
|
2
|
+
deriveFlowView,
|
|
3
|
+
resolvedBounds,
|
|
4
|
+
type FlowEdge,
|
|
5
|
+
type FlowGraph,
|
|
6
|
+
type FlowNode,
|
|
7
|
+
type LayoutDiagnostic,
|
|
8
|
+
type LayoutPositions,
|
|
9
|
+
type LayoutResult,
|
|
10
|
+
type LayoutRunOptions,
|
|
11
|
+
type Point,
|
|
12
|
+
type Rect,
|
|
13
|
+
} from '@statelyai/flow';
|
|
14
|
+
|
|
15
|
+
export type FlowLayoutScope = {
|
|
16
|
+
ids: string[];
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type FlowLayoutOptions<TNodeData = any, TEdgeData = any, TLabelData = any> = {
|
|
20
|
+
getBounds?: (node: FlowNode<TNodeData>) => Rect;
|
|
21
|
+
scope?: FlowLayoutScope;
|
|
22
|
+
onWarning?: (message: string) => void;
|
|
23
|
+
nodeSpacing?: number;
|
|
24
|
+
rankSpacing?: number;
|
|
25
|
+
direction?: 'down' | 'right' | 'up' | 'left';
|
|
26
|
+
graph?: Record<string, unknown>;
|
|
27
|
+
node?: Record<string, unknown>;
|
|
28
|
+
edge?: Record<string, unknown>;
|
|
29
|
+
label?: Record<string, unknown>;
|
|
30
|
+
source?: string;
|
|
31
|
+
metadata?: Readonly<Record<string, unknown>>;
|
|
32
|
+
_types?: {
|
|
33
|
+
node?: TNodeData;
|
|
34
|
+
edge?: TEdgeData;
|
|
35
|
+
label?: TLabelData;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export type FlowLayoutFactory<
|
|
40
|
+
TNodeData = any,
|
|
41
|
+
TEdgeData = any,
|
|
42
|
+
TLabelData = any,
|
|
43
|
+
> = (
|
|
44
|
+
options?: FlowLayoutOptions<TNodeData, TEdgeData, TLabelData>,
|
|
45
|
+
) => FlowLayoutAlgorithm<TNodeData, TEdgeData, TLabelData>;
|
|
46
|
+
|
|
47
|
+
export type FlowLayoutAlgorithm<
|
|
48
|
+
TNodeData = any,
|
|
49
|
+
TEdgeData = any,
|
|
50
|
+
TLabelData = any,
|
|
51
|
+
> = (
|
|
52
|
+
graph: Readonly<FlowGraph<TNodeData, TEdgeData, TLabelData>>,
|
|
53
|
+
options?: LayoutRunOptions,
|
|
54
|
+
) => Promise<LayoutResult | LayoutPositions>;
|
|
55
|
+
|
|
56
|
+
export function boundsOf<TNodeData>(
|
|
57
|
+
node: FlowNode<TNodeData>,
|
|
58
|
+
getBounds?: (node: FlowNode<TNodeData>) => Rect,
|
|
59
|
+
): Rect {
|
|
60
|
+
const bounds = getBounds?.(node) ?? resolvedBounds(node);
|
|
61
|
+
if (
|
|
62
|
+
!Number.isFinite(bounds.x) ||
|
|
63
|
+
!Number.isFinite(bounds.y) ||
|
|
64
|
+
!Number.isFinite(bounds.width) ||
|
|
65
|
+
!Number.isFinite(bounds.height) ||
|
|
66
|
+
bounds.width < 0 ||
|
|
67
|
+
bounds.height < 0
|
|
68
|
+
) {
|
|
69
|
+
throw new TypeError(
|
|
70
|
+
`Layout bounds for node "${node.id}" must be finite with non-negative size.`,
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
return bounds;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Build the induced graph used by a scoped layout. Out-of-scope nodes never
|
|
78
|
+
* reach the engine; scoped nodes with an excluded parent become roots.
|
|
79
|
+
*/
|
|
80
|
+
export function graphForLayoutScope<TNodeData, TEdgeData, TLabelData>(
|
|
81
|
+
graph: FlowGraph<TNodeData, TEdgeData, TLabelData>,
|
|
82
|
+
scope?: FlowLayoutScope,
|
|
83
|
+
): FlowGraph<TNodeData, TEdgeData, TLabelData> {
|
|
84
|
+
if (!scope) return graph;
|
|
85
|
+
const ids = new Set(scope.ids);
|
|
86
|
+
const knownIds = new Set(graph.nodes.map((node) => node.id));
|
|
87
|
+
const unknownIds = [...ids].filter((id) => !knownIds.has(id)).sort();
|
|
88
|
+
if (unknownIds.length > 0) {
|
|
89
|
+
throw new TypeError(
|
|
90
|
+
`Layout scope contains unknown node ids: ${unknownIds.join(', ')}`,
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
...graph,
|
|
95
|
+
nodes: graph.nodes
|
|
96
|
+
.filter((node) => ids.has(node.id))
|
|
97
|
+
.map((node) =>
|
|
98
|
+
node.parentId && !ids.has(node.parentId)
|
|
99
|
+
? { ...node, parentId: undefined }
|
|
100
|
+
: node,
|
|
101
|
+
),
|
|
102
|
+
edges: graph.edges.filter(
|
|
103
|
+
(edge) => ids.has(edge.sourceId) && ids.has(edge.targetId),
|
|
104
|
+
),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function mergedLayoutScope(
|
|
109
|
+
graph: FlowGraph,
|
|
110
|
+
factoryScope: FlowLayoutScope | undefined,
|
|
111
|
+
runOptions: LayoutRunOptions,
|
|
112
|
+
): FlowLayoutScope | undefined {
|
|
113
|
+
if (!factoryScope && !runOptions.scope) return undefined;
|
|
114
|
+
const knownIds = new Set(graph.nodes.map((node) => node.id));
|
|
115
|
+
if (
|
|
116
|
+
runOptions.scope?.parentId &&
|
|
117
|
+
!knownIds.has(runOptions.scope.parentId)
|
|
118
|
+
) {
|
|
119
|
+
throw new TypeError(
|
|
120
|
+
`Layout scope contains unknown parent id: ${runOptions.scope.parentId}`,
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
const unknownFactoryIds = (factoryScope?.ids ?? [])
|
|
124
|
+
.filter((id) => !knownIds.has(id))
|
|
125
|
+
.sort();
|
|
126
|
+
if (unknownFactoryIds.length > 0) {
|
|
127
|
+
throw new TypeError(
|
|
128
|
+
`Layout scope contains unknown node ids: ${unknownFactoryIds.join(', ')}`,
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
const factoryIds = factoryScope ? new Set(factoryScope.ids) : null;
|
|
132
|
+
const structuralIds = runOptions.scope
|
|
133
|
+
? new Set(
|
|
134
|
+
deriveFlowView(graph, runOptions.scope, {
|
|
135
|
+
nodes: graph.nodes,
|
|
136
|
+
edges: graph.edges,
|
|
137
|
+
}).nodes.map((node) => node.id),
|
|
138
|
+
)
|
|
139
|
+
: null;
|
|
140
|
+
return {
|
|
141
|
+
ids: graph.nodes
|
|
142
|
+
.map((node) => node.id)
|
|
143
|
+
.filter(
|
|
144
|
+
(id) =>
|
|
145
|
+
(!factoryIds || factoryIds.has(id)) &&
|
|
146
|
+
(!structuralIds || structuralIds.has(id)),
|
|
147
|
+
),
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function layoutRunContext(
|
|
152
|
+
graph: FlowGraph,
|
|
153
|
+
factoryOptions: FlowLayoutOptions,
|
|
154
|
+
runOptions: LayoutRunOptions,
|
|
155
|
+
adapter: 'stately' | 'elk' | 'dagre',
|
|
156
|
+
): {
|
|
157
|
+
fixedIds: ReadonlySet<string>;
|
|
158
|
+
source: string;
|
|
159
|
+
metadata: Readonly<Record<string, unknown>>;
|
|
160
|
+
diagnostics: LayoutDiagnostic[];
|
|
161
|
+
} {
|
|
162
|
+
const knownIds = new Set([
|
|
163
|
+
...graph.nodes.map((node) => node.id),
|
|
164
|
+
...graph.edges.map((edge) => edge.id),
|
|
165
|
+
]);
|
|
166
|
+
const fixedIds = new Set([
|
|
167
|
+
...(runOptions.fixedEntityIds ?? []),
|
|
168
|
+
...(runOptions.pinnedEntityIds ?? []),
|
|
169
|
+
]);
|
|
170
|
+
const unknown = [...fixedIds].filter((id) => !knownIds.has(id)).sort();
|
|
171
|
+
if (unknown.length > 0) {
|
|
172
|
+
throw new TypeError(`Layout fixed/pinned ids are unknown: ${unknown.join(', ')}`);
|
|
173
|
+
}
|
|
174
|
+
return {
|
|
175
|
+
fixedIds,
|
|
176
|
+
source: runOptions.source ?? factoryOptions.source ?? adapter,
|
|
177
|
+
metadata: {
|
|
178
|
+
...(runOptions.previousResult?.metadata ?? {}),
|
|
179
|
+
...(factoryOptions.metadata ?? {}),
|
|
180
|
+
...(runOptions.metadata ?? {}),
|
|
181
|
+
adapter,
|
|
182
|
+
},
|
|
183
|
+
diagnostics: [],
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export function throwIfAborted(options: LayoutRunOptions): void {
|
|
188
|
+
if (!options.signal?.aborted) return;
|
|
189
|
+
const error = new Error('Layout aborted.');
|
|
190
|
+
error.name = 'AbortError';
|
|
191
|
+
throw error;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export function edgeLabelPositionsFromNodePositions<
|
|
195
|
+
TEdgeData,
|
|
196
|
+
TLabelData,
|
|
197
|
+
>(
|
|
198
|
+
graph: FlowGraph<any, TEdgeData, TLabelData>,
|
|
199
|
+
positions: Map<string, Point>,
|
|
200
|
+
getBounds?: (node: FlowNode) => Rect,
|
|
201
|
+
laidOutBounds?: ReadonlyMap<string, Rect>,
|
|
202
|
+
direction = graph.direction ?? 'down',
|
|
203
|
+
): Map<string, Point> | undefined {
|
|
204
|
+
const labels = new Map<string, Point>();
|
|
205
|
+
const nodes = new Map(graph.nodes.map((node) => [node.id, node]));
|
|
206
|
+
for (const edge of graph.edges as FlowEdge<TEdgeData, TLabelData>[]) {
|
|
207
|
+
if (!edge.label) continue;
|
|
208
|
+
const source = nodes.get(edge.sourceId);
|
|
209
|
+
const target = nodes.get(edge.targetId);
|
|
210
|
+
if (!source || !target) continue;
|
|
211
|
+
const sourceBounds = laidOutBounds?.get(source.id) ?? boundsOf(source, getBounds);
|
|
212
|
+
const targetBounds = laidOutBounds?.get(target.id) ?? boundsOf(target, getBounds);
|
|
213
|
+
const sourcePosition = positions.get(source.id) ?? sourceBounds;
|
|
214
|
+
const targetPosition = positions.get(target.id) ?? targetBounds;
|
|
215
|
+
const sourceRect = { ...sourceBounds, ...sourcePosition };
|
|
216
|
+
const targetRect = { ...targetBounds, ...targetPosition };
|
|
217
|
+
const horizontal = direction === 'left' || direction === 'right';
|
|
218
|
+
const sourceCenter = {
|
|
219
|
+
x: sourceRect.x + sourceRect.width / 2,
|
|
220
|
+
y: sourceRect.y + sourceRect.height / 2,
|
|
221
|
+
};
|
|
222
|
+
const targetCenter = {
|
|
223
|
+
x: targetRect.x + targetRect.width / 2,
|
|
224
|
+
y: targetRect.y + targetRect.height / 2,
|
|
225
|
+
};
|
|
226
|
+
const gapCenter = horizontal
|
|
227
|
+
? {
|
|
228
|
+
x:
|
|
229
|
+
direction === 'left'
|
|
230
|
+
? (targetRect.x + targetRect.width + sourceRect.x) / 2
|
|
231
|
+
: (sourceRect.x + sourceRect.width + targetRect.x) / 2,
|
|
232
|
+
y: (sourceCenter.y + targetCenter.y) / 2,
|
|
233
|
+
}
|
|
234
|
+
: {
|
|
235
|
+
x: (sourceCenter.x + targetCenter.x) / 2,
|
|
236
|
+
y:
|
|
237
|
+
direction === 'up'
|
|
238
|
+
? (targetRect.y + targetRect.height + sourceRect.y) / 2
|
|
239
|
+
: (sourceRect.y + sourceRect.height + targetRect.y) / 2,
|
|
240
|
+
};
|
|
241
|
+
labels.set(edge.id, {
|
|
242
|
+
x: gapCenter.x - edge.label.width / 2,
|
|
243
|
+
y: gapCenter.y - edge.label.height / 2,
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
return labels.size > 0 ? labels : undefined;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export function rankDirection(direction: string | undefined): 'TB' | 'LR' | 'BT' | 'RL' {
|
|
250
|
+
switch (direction) {
|
|
251
|
+
case 'right':
|
|
252
|
+
return 'LR';
|
|
253
|
+
case 'up':
|
|
254
|
+
return 'BT';
|
|
255
|
+
case 'left':
|
|
256
|
+
return 'RL';
|
|
257
|
+
default:
|
|
258
|
+
return 'TB';
|
|
259
|
+
}
|
|
260
|
+
}
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
createEdge,
|
|
4
|
+
createFlow,
|
|
5
|
+
createGraph,
|
|
6
|
+
createNode,
|
|
7
|
+
type FlowGraph,
|
|
8
|
+
type LayoutResult,
|
|
9
|
+
} from '@statelyai/flow';
|
|
10
|
+
import { statelyLayout } from './stately';
|
|
11
|
+
|
|
12
|
+
function fixture(): FlowGraph {
|
|
13
|
+
return createGraph({
|
|
14
|
+
direction: 'right',
|
|
15
|
+
nodes: [
|
|
16
|
+
createNode({ id: 'a', width: 120, height: 60 }),
|
|
17
|
+
createNode({ id: 'b', width: 120, height: 60 }),
|
|
18
|
+
],
|
|
19
|
+
edges: [
|
|
20
|
+
createEdge({
|
|
21
|
+
id: 'ab',
|
|
22
|
+
sourceId: 'a',
|
|
23
|
+
targetId: 'b',
|
|
24
|
+
label: { x: 0, y: 0, width: 80, height: 24 },
|
|
25
|
+
}),
|
|
26
|
+
],
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
describe('Stately Layout adapter', () => {
|
|
31
|
+
it('maps Graph-native layered geometry into the Flow layout contract', async () => {
|
|
32
|
+
const graph = fixture();
|
|
33
|
+
const result = (await statelyLayout({
|
|
34
|
+
spacing: { node: 80, layer: 120 },
|
|
35
|
+
})(graph)) as LayoutResult;
|
|
36
|
+
|
|
37
|
+
expect(result.positions.size).toBe(graph.nodes.length);
|
|
38
|
+
expect(result.nodeBounds?.get('a')).toMatchObject({
|
|
39
|
+
width: 120,
|
|
40
|
+
height: 60,
|
|
41
|
+
});
|
|
42
|
+
expect(result.edgeLabelBounds?.get('ab')).toMatchObject({
|
|
43
|
+
width: 80,
|
|
44
|
+
height: 24,
|
|
45
|
+
});
|
|
46
|
+
expect(result.edgeRoutes?.get('ab')).toMatchObject({
|
|
47
|
+
routing: 'orthogonal',
|
|
48
|
+
});
|
|
49
|
+
expect(result.source).toBe('stately');
|
|
50
|
+
expect(result.metadata).toMatchObject({
|
|
51
|
+
adapter: 'stately',
|
|
52
|
+
algorithm: 'layered',
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('applies Layout routes as authoritative Graph points', async () => {
|
|
57
|
+
const flow = createFlow({ graph: fixture(), layout: statelyLayout() });
|
|
58
|
+
|
|
59
|
+
const response = await flow.layout.run();
|
|
60
|
+
const edge = flow.getEdge('ab');
|
|
61
|
+
|
|
62
|
+
expect(response.applied).toBe(true);
|
|
63
|
+
expect(edge?.points?.length).toBeGreaterThanOrEqual(2);
|
|
64
|
+
expect(edge?.routing).toBe('orthogonal');
|
|
65
|
+
expect(edge?.waypoints).toBeUndefined();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('converts parent-relative compound output to Flow absolute coordinates', async () => {
|
|
69
|
+
const graph = createGraph({
|
|
70
|
+
direction: 'right',
|
|
71
|
+
nodes: [
|
|
72
|
+
createNode({ id: 'before', width: 100, height: 50 }),
|
|
73
|
+
createNode({ id: 'group', width: 200, height: 140 }),
|
|
74
|
+
createNode({
|
|
75
|
+
id: 'a',
|
|
76
|
+
parentId: 'group',
|
|
77
|
+
width: 80,
|
|
78
|
+
height: 40,
|
|
79
|
+
}),
|
|
80
|
+
createNode({
|
|
81
|
+
id: 'b',
|
|
82
|
+
parentId: 'group',
|
|
83
|
+
width: 80,
|
|
84
|
+
height: 40,
|
|
85
|
+
}),
|
|
86
|
+
],
|
|
87
|
+
edges: [
|
|
88
|
+
createEdge({ id: 'before-group', sourceId: 'before', targetId: 'group' }),
|
|
89
|
+
createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' }),
|
|
90
|
+
],
|
|
91
|
+
});
|
|
92
|
+
const result = (await statelyLayout()(graph)) as LayoutResult;
|
|
93
|
+
const group = result.nodeBounds!.get('group')!;
|
|
94
|
+
const a = result.nodeBounds!.get('a')!;
|
|
95
|
+
const b = result.nodeBounds!.get('b')!;
|
|
96
|
+
const route = result.edgeRoutes!.get('ab')!;
|
|
97
|
+
const points = Array.isArray(route)
|
|
98
|
+
? (route as readonly { x: number; y: number }[])
|
|
99
|
+
: 'points' in route
|
|
100
|
+
? route.points
|
|
101
|
+
: (route as { waypoints: readonly { x: number; y: number }[] })
|
|
102
|
+
.waypoints;
|
|
103
|
+
|
|
104
|
+
expect(group.x).toBeGreaterThan(0);
|
|
105
|
+
for (const child of [a, b]) {
|
|
106
|
+
expect(child.x).toBeGreaterThanOrEqual(group.x);
|
|
107
|
+
expect(child.y).toBeGreaterThanOrEqual(group.y);
|
|
108
|
+
expect(child.x + child.width).toBeLessThanOrEqual(group.x + group.width);
|
|
109
|
+
expect(child.y + child.height).toBeLessThanOrEqual(group.y + group.height);
|
|
110
|
+
}
|
|
111
|
+
expect(points.every((point) => point.x >= group.x && point.y >= group.y)).toBe(
|
|
112
|
+
true,
|
|
113
|
+
);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('honors induced scope and fixed entities', async () => {
|
|
117
|
+
const graph = fixture();
|
|
118
|
+
const result = (await statelyLayout({ scope: { ids: ['a'] } })(graph, {
|
|
119
|
+
fixedEntityIds: new Set(['a']),
|
|
120
|
+
})) as LayoutResult;
|
|
121
|
+
|
|
122
|
+
expect([...result.positions]).toEqual([]);
|
|
123
|
+
expect([...result.edgeRoutes ?? []]).toEqual([]);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('anchors laid-out descendants to an unchanged fixed parent', async () => {
|
|
127
|
+
const graph = createGraph({
|
|
128
|
+
nodes: [
|
|
129
|
+
createNode({ id: 'group', x: 500, y: 300, width: 240, height: 160 }),
|
|
130
|
+
createNode({
|
|
131
|
+
id: 'a',
|
|
132
|
+
parentId: 'group',
|
|
133
|
+
x: 520,
|
|
134
|
+
y: 340,
|
|
135
|
+
width: 80,
|
|
136
|
+
height: 40,
|
|
137
|
+
}),
|
|
138
|
+
createNode({
|
|
139
|
+
id: 'b',
|
|
140
|
+
parentId: 'group',
|
|
141
|
+
x: 620,
|
|
142
|
+
y: 340,
|
|
143
|
+
width: 80,
|
|
144
|
+
height: 40,
|
|
145
|
+
}),
|
|
146
|
+
],
|
|
147
|
+
edges: [createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' })],
|
|
148
|
+
});
|
|
149
|
+
const result = (await statelyLayout()(graph, {
|
|
150
|
+
fixedEntityIds: new Set(['group']),
|
|
151
|
+
})) as LayoutResult;
|
|
152
|
+
|
|
153
|
+
expect(result.positions.has('group')).toBe(false);
|
|
154
|
+
expect(result.positions.get('a')!.x).toBeGreaterThanOrEqual(500);
|
|
155
|
+
expect(result.positions.get('a')!.y).toBeGreaterThanOrEqual(300);
|
|
156
|
+
const route = result.edgeRoutes!.get('ab')!;
|
|
157
|
+
const points = 'points' in route ? route.points : [];
|
|
158
|
+
expect(points.every((point) => point.x >= 500 && point.y >= 300)).toBe(true);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('does not install a route computed against a moved copy of a fixed endpoint', async () => {
|
|
162
|
+
const graph = createGraph({
|
|
163
|
+
direction: 'right',
|
|
164
|
+
nodes: [
|
|
165
|
+
createNode({ id: 'a', x: 500, y: 100, width: 100, height: 50 }),
|
|
166
|
+
createNode({ id: 'b', x: 0, y: 0, width: 100, height: 50 }),
|
|
167
|
+
],
|
|
168
|
+
edges: [createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' })],
|
|
169
|
+
});
|
|
170
|
+
const result = (await statelyLayout()(graph, {
|
|
171
|
+
fixedEntityIds: new Set(['a']),
|
|
172
|
+
})) as LayoutResult;
|
|
173
|
+
|
|
174
|
+
expect(result.positions.has('a')).toBe(false);
|
|
175
|
+
expect(result.edgeRoutes?.has('ab')).not.toBe(true);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it.each([
|
|
179
|
+
['factory ids', statelyLayout({ scope: { ids: ['a', 'b'] } }), undefined],
|
|
180
|
+
[
|
|
181
|
+
'structural scope',
|
|
182
|
+
statelyLayout(),
|
|
183
|
+
{ scope: { parentId: 'group', depth: 1 } } as const,
|
|
184
|
+
],
|
|
185
|
+
])('anchors promoted roots for %s', async (_name, layout, runOptions) => {
|
|
186
|
+
const graph = createGraph({
|
|
187
|
+
nodes: [
|
|
188
|
+
createNode({ id: 'group', x: 500, y: 300, width: 240, height: 160 }),
|
|
189
|
+
createNode({ id: 'a', parentId: 'group', x: 520, y: 340, width: 80, height: 40 }),
|
|
190
|
+
createNode({ id: 'b', parentId: 'group', x: 620, y: 340, width: 80, height: 40 }),
|
|
191
|
+
],
|
|
192
|
+
edges: [createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' })],
|
|
193
|
+
});
|
|
194
|
+
const result = (await layout(graph, runOptions)) as LayoutResult;
|
|
195
|
+
|
|
196
|
+
expect(result.positions.get('a')!.x).toBeGreaterThanOrEqual(500);
|
|
197
|
+
expect(result.positions.get('a')!.y).toBeGreaterThanOrEqual(300);
|
|
198
|
+
const route = result.edgeRoutes!.get('ab')!;
|
|
199
|
+
const points = 'points' in route ? route.points : [];
|
|
200
|
+
expect(points.length).toBeGreaterThanOrEqual(2);
|
|
201
|
+
expect(points.every((point) => point.x >= 500 && point.y >= 300)).toBe(true);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it('keeps fixed group bounds authoritative', async () => {
|
|
205
|
+
const graph = createGraph({
|
|
206
|
+
nodes: [
|
|
207
|
+
createNode({
|
|
208
|
+
id: 'group',
|
|
209
|
+
width: 200,
|
|
210
|
+
height: 140,
|
|
211
|
+
group: { type: 'fixed', padding: 20 },
|
|
212
|
+
}),
|
|
213
|
+
createNode({ id: 'a', parentId: 'group', width: 260, height: 40 }),
|
|
214
|
+
],
|
|
215
|
+
edges: [],
|
|
216
|
+
});
|
|
217
|
+
const result = (await statelyLayout()(graph)) as LayoutResult;
|
|
218
|
+
|
|
219
|
+
expect(result.nodeSizes?.get('group')).toEqual({ width: 200, height: 140 });
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it('reserves derived-group content and honors its child direction', async () => {
|
|
223
|
+
const graph = createGraph({
|
|
224
|
+
direction: 'down',
|
|
225
|
+
nodes: [
|
|
226
|
+
createNode({
|
|
227
|
+
id: 'group',
|
|
228
|
+
width: 160,
|
|
229
|
+
height: 40,
|
|
230
|
+
group: { type: 'derived', content: 'top', padding: 20, direction: 'right' },
|
|
231
|
+
}),
|
|
232
|
+
createNode({ id: 'a', parentId: 'group', width: 80, height: 40 }),
|
|
233
|
+
createNode({ id: 'b', parentId: 'group', width: 80, height: 40 }),
|
|
234
|
+
],
|
|
235
|
+
edges: [createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' })],
|
|
236
|
+
});
|
|
237
|
+
const result = (await statelyLayout()(graph)) as LayoutResult;
|
|
238
|
+
const group = result.nodeBounds!.get('group')!;
|
|
239
|
+
const a = result.nodeBounds!.get('a')!;
|
|
240
|
+
const b = result.nodeBounds!.get('b')!;
|
|
241
|
+
|
|
242
|
+
expect(a.y).toBeGreaterThanOrEqual(group.y + 40 + 20);
|
|
243
|
+
expect(b.x).toBeGreaterThan(a.x);
|
|
244
|
+
expect(group.width).toBeGreaterThanOrEqual(b.x + b.width - group.x + 20);
|
|
245
|
+
});
|
|
246
|
+
});
|