@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,194 @@
|
|
|
1
|
+
import type { FlowEdge, FlowGraph, FlowNode } from './types';
|
|
2
|
+
|
|
3
|
+
// Id → entity indices, cached on array identity.
|
|
4
|
+
//
|
|
5
|
+
// The store's immer-based transitions produce a new nodes/edges array whenever
|
|
6
|
+
// any entity changes, so array identity is a correct cache key: an index is
|
|
7
|
+
// built at most once per store transition, turning the engine's per-query
|
|
8
|
+
// O(N) `find` calls into O(1) lookups without changing any semantics.
|
|
9
|
+
//
|
|
10
|
+
// First occurrence wins on duplicate ids, matching `Array.prototype.find`.
|
|
11
|
+
|
|
12
|
+
const nodeIndexCache = new WeakMap<FlowNode[], Map<string, FlowNode>>();
|
|
13
|
+
const nodePositionCache = new WeakMap<FlowNode[], Map<string, number>>();
|
|
14
|
+
const childrenIndexCache = new WeakMap<FlowNode[], Map<string, FlowNode[]>>();
|
|
15
|
+
const edgeIndexCache = new WeakMap<FlowEdge[], Map<string, FlowEdge>>();
|
|
16
|
+
const edgePositionCache = new WeakMap<FlowEdge[], Map<string, number>>();
|
|
17
|
+
const edgeAdjacencyCache = new WeakMap<FlowEdge[], EdgeAdjacencyIndex>();
|
|
18
|
+
|
|
19
|
+
export type EdgeAdjacencyIndex = {
|
|
20
|
+
bySource: Map<string, FlowEdge[]>;
|
|
21
|
+
byTarget: Map<string, FlowEdge[]>;
|
|
22
|
+
byNode: Map<string, FlowEdge[]>;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export function indexNodes(nodes: FlowNode[]): Map<string, FlowNode> {
|
|
26
|
+
let index = nodeIndexCache.get(nodes);
|
|
27
|
+
if (!index) {
|
|
28
|
+
index = new Map();
|
|
29
|
+
const positions = new Map<string, number>();
|
|
30
|
+
const children = new Map<string, FlowNode[]>();
|
|
31
|
+
for (let position = 0; position < nodes.length; position++) {
|
|
32
|
+
const node = nodes[position];
|
|
33
|
+
if (!index.has(node.id)) index.set(node.id, node);
|
|
34
|
+
if (!positions.has(node.id)) positions.set(node.id, position);
|
|
35
|
+
const parentId = node.parentId ?? null;
|
|
36
|
+
if (parentId == null || parentId === node.id) continue;
|
|
37
|
+
const bucket = children.get(parentId);
|
|
38
|
+
if (bucket) bucket.push(node);
|
|
39
|
+
else children.set(parentId, [node]);
|
|
40
|
+
}
|
|
41
|
+
nodeIndexCache.set(nodes, index);
|
|
42
|
+
nodePositionCache.set(nodes, positions);
|
|
43
|
+
childrenIndexCache.set(nodes, children);
|
|
44
|
+
}
|
|
45
|
+
return index;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function indexNodePositions(nodes: FlowNode[]): Map<string, number> {
|
|
49
|
+
if (!nodePositionCache.has(nodes)) indexNodes(nodes);
|
|
50
|
+
return nodePositionCache.get(nodes)!;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function indexEdges(edges: FlowEdge[]): Map<string, FlowEdge> {
|
|
54
|
+
let index = edgeIndexCache.get(edges);
|
|
55
|
+
if (!index) {
|
|
56
|
+
index = new Map();
|
|
57
|
+
const positions = new Map<string, number>();
|
|
58
|
+
const adjacency: EdgeAdjacencyIndex = {
|
|
59
|
+
bySource: new Map(),
|
|
60
|
+
byTarget: new Map(),
|
|
61
|
+
byNode: new Map(),
|
|
62
|
+
};
|
|
63
|
+
for (let position = 0; position < edges.length; position++) {
|
|
64
|
+
const edge = edges[position];
|
|
65
|
+
if (!index.has(edge.id)) index.set(edge.id, edge);
|
|
66
|
+
if (!positions.has(edge.id)) positions.set(edge.id, position);
|
|
67
|
+
pushEdge(adjacency.bySource, edge.sourceId, edge);
|
|
68
|
+
pushEdge(adjacency.byTarget, edge.targetId, edge);
|
|
69
|
+
pushEdge(adjacency.byNode, edge.sourceId, edge);
|
|
70
|
+
if (edge.targetId !== edge.sourceId) {
|
|
71
|
+
pushEdge(adjacency.byNode, edge.targetId, edge);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
edgeIndexCache.set(edges, index);
|
|
75
|
+
edgePositionCache.set(edges, positions);
|
|
76
|
+
edgeAdjacencyCache.set(edges, adjacency);
|
|
77
|
+
}
|
|
78
|
+
return index;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function indexEdgePositions(edges: FlowEdge[]): Map<string, number> {
|
|
82
|
+
if (!edgePositionCache.has(edges)) indexEdges(edges);
|
|
83
|
+
return edgePositionCache.get(edges)!;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function indexEdgeAdjacency(edges: FlowEdge[]): EdgeAdjacencyIndex {
|
|
87
|
+
let index = edgeAdjacencyCache.get(edges);
|
|
88
|
+
if (!index) {
|
|
89
|
+
indexEdges(edges);
|
|
90
|
+
index = edgeAdjacencyCache.get(edges)!;
|
|
91
|
+
}
|
|
92
|
+
return index;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function pushEdge(
|
|
96
|
+
index: Map<string, FlowEdge[]>,
|
|
97
|
+
nodeId: string,
|
|
98
|
+
edge: FlowEdge,
|
|
99
|
+
): void {
|
|
100
|
+
const bucket = index.get(nodeId);
|
|
101
|
+
if (bucket) bucket.push(edge);
|
|
102
|
+
else index.set(nodeId, [edge]);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function getIncomers(
|
|
106
|
+
graph: FlowGraph,
|
|
107
|
+
nodeId: string,
|
|
108
|
+
): FlowNode[] {
|
|
109
|
+
const nodes = indexNodes(graph.nodes);
|
|
110
|
+
const sourceIds = new Set<string>();
|
|
111
|
+
for (const edge of indexEdgeAdjacency(graph.edges).byTarget.get(nodeId) ?? []) {
|
|
112
|
+
if (nodes.has(edge.sourceId)) sourceIds.add(edge.sourceId);
|
|
113
|
+
}
|
|
114
|
+
return graph.nodes.filter((node) => sourceIds.has(node.id));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function getOutgoers(
|
|
118
|
+
graph: FlowGraph,
|
|
119
|
+
nodeId: string,
|
|
120
|
+
): FlowNode[] {
|
|
121
|
+
const nodes = indexNodes(graph.nodes);
|
|
122
|
+
const targetIds = new Set<string>();
|
|
123
|
+
for (const edge of indexEdgeAdjacency(graph.edges).bySource.get(nodeId) ?? []) {
|
|
124
|
+
if (nodes.has(edge.targetId)) targetIds.add(edge.targetId);
|
|
125
|
+
}
|
|
126
|
+
return graph.nodes.filter((node) => targetIds.has(node.id));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function getConnectedEdges(
|
|
130
|
+
graph: FlowGraph,
|
|
131
|
+
nodeIds: Iterable<string>,
|
|
132
|
+
): FlowEdge[] {
|
|
133
|
+
const ids = new Set(nodeIds);
|
|
134
|
+
const nodes = indexNodes(graph.nodes);
|
|
135
|
+
return graph.edges.filter(
|
|
136
|
+
(edge) =>
|
|
137
|
+
nodes.has(edge.sourceId) &&
|
|
138
|
+
nodes.has(edge.targetId) &&
|
|
139
|
+
(ids.has(edge.sourceId) || ids.has(edge.targetId)),
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function getNeighbors(
|
|
144
|
+
graph: FlowGraph,
|
|
145
|
+
nodeId: string,
|
|
146
|
+
): FlowNode[] {
|
|
147
|
+
const neighborIds = new Set<string>();
|
|
148
|
+
for (const node of getIncomers(graph, nodeId)) neighborIds.add(node.id);
|
|
149
|
+
for (const node of getOutgoers(graph, nodeId)) neighborIds.add(node.id);
|
|
150
|
+
return graph.nodes.filter((node) => neighborIds.has(node.id));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** parentId → child nodes, cached on array identity. Self-parents ignored. */
|
|
154
|
+
export function indexChildren(nodes: FlowNode[]): Map<string, FlowNode[]> {
|
|
155
|
+
let index = childrenIndexCache.get(nodes);
|
|
156
|
+
if (!index) {
|
|
157
|
+
indexNodes(nodes);
|
|
158
|
+
index = childrenIndexCache.get(nodes)!;
|
|
159
|
+
}
|
|
160
|
+
return index;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const depthCache = new WeakMap<FlowNode[], Map<string, number>>();
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Hierarchy depth per node id (roots = 0), cached on array identity.
|
|
167
|
+
* Cycle-safe; missing parents count as roots. Renderers can sort by this so
|
|
168
|
+
* parents paint beneath children regardless of array order.
|
|
169
|
+
*/
|
|
170
|
+
export function nodeDepths(nodes: FlowNode[]): Map<string, number> {
|
|
171
|
+
let depths = depthCache.get(nodes);
|
|
172
|
+
if (!depths) {
|
|
173
|
+
const index = indexNodes(nodes);
|
|
174
|
+
const resolved = new Map<string, number>();
|
|
175
|
+
const depthOf = (node: FlowNode, seen: Set<string>): number => {
|
|
176
|
+
const existing = resolved.get(node.id);
|
|
177
|
+
if (existing !== undefined) return existing;
|
|
178
|
+
const parentId = node.parentId ?? null;
|
|
179
|
+
if (parentId == null || parentId === node.id || seen.has(node.id)) {
|
|
180
|
+
resolved.set(node.id, 0);
|
|
181
|
+
return 0;
|
|
182
|
+
}
|
|
183
|
+
seen.add(node.id);
|
|
184
|
+
const parent = index.get(parentId);
|
|
185
|
+
const depth = parent ? depthOf(parent, seen) + 1 : 0;
|
|
186
|
+
resolved.set(node.id, depth);
|
|
187
|
+
return depth;
|
|
188
|
+
};
|
|
189
|
+
for (const node of nodes) depthOf(node, new Set());
|
|
190
|
+
depths = resolved;
|
|
191
|
+
depthCache.set(nodes, depths);
|
|
192
|
+
}
|
|
193
|
+
return depths;
|
|
194
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
deriveGroupGeometry,
|
|
4
|
+
fixedGroupGeometry,
|
|
5
|
+
getEdgeOwner,
|
|
6
|
+
resolveGroupPadding,
|
|
7
|
+
} from './hierarchy';
|
|
8
|
+
import { createNode } from './factories';
|
|
9
|
+
|
|
10
|
+
const content = { width: 120, height: 40 };
|
|
11
|
+
const children = { x: 100, y: 200, width: 300, height: 150 };
|
|
12
|
+
|
|
13
|
+
describe('deriveGroupGeometry', () => {
|
|
14
|
+
it('content on top: child area padded, content block above it (Stately default)', () => {
|
|
15
|
+
const geometry = deriveGroupGeometry(content, children, {
|
|
16
|
+
type: 'derived',
|
|
17
|
+
padding: 16,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
expect(geometry.childArea).toEqual({
|
|
21
|
+
x: 84,
|
|
22
|
+
y: 184,
|
|
23
|
+
width: 332,
|
|
24
|
+
height: 182,
|
|
25
|
+
});
|
|
26
|
+
// content sits directly above the child area, full group width
|
|
27
|
+
expect(geometry.contentBounds.y).toBe(184 - 40);
|
|
28
|
+
expect(geometry.bounds).toEqual({
|
|
29
|
+
x: 84,
|
|
30
|
+
y: 144,
|
|
31
|
+
width: 332,
|
|
32
|
+
height: 40 + 182,
|
|
33
|
+
});
|
|
34
|
+
// group bounds always contain children + padding
|
|
35
|
+
expect(geometry.bounds.x).toBeLessThanOrEqual(children.x - 16);
|
|
36
|
+
expect(geometry.bounds.y).toBeLessThanOrEqual(children.y - 16 - 40);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('is a pure function of children — moving a child out and back is reversible', () => {
|
|
40
|
+
const layout = { type: 'derived' } as const;
|
|
41
|
+
const before = deriveGroupGeometry(content, children, layout);
|
|
42
|
+
const stretched = deriveGroupGeometry(
|
|
43
|
+
content,
|
|
44
|
+
{ ...children, width: children.width + 400 },
|
|
45
|
+
layout,
|
|
46
|
+
);
|
|
47
|
+
const after = deriveGroupGeometry(content, children, layout);
|
|
48
|
+
|
|
49
|
+
expect(stretched.bounds.width).toBeGreaterThan(before.bounds.width);
|
|
50
|
+
expect(after).toEqual(before); // no ratchet
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('content on the left spans the group height', () => {
|
|
54
|
+
const geometry = deriveGroupGeometry(content, children, {
|
|
55
|
+
type: 'derived',
|
|
56
|
+
content: 'left',
|
|
57
|
+
padding: 10,
|
|
58
|
+
});
|
|
59
|
+
expect(geometry.contentBounds.x).toBe(children.x - 10 - content.width);
|
|
60
|
+
expect(geometry.contentBounds.height).toBe(geometry.bounds.height);
|
|
61
|
+
expect(geometry.bounds.width).toBe(
|
|
62
|
+
content.width + children.width + 20,
|
|
63
|
+
);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('content on the bottom sits under the child area', () => {
|
|
67
|
+
const geometry = deriveGroupGeometry(content, children, {
|
|
68
|
+
type: 'derived',
|
|
69
|
+
content: 'bottom',
|
|
70
|
+
padding: 0,
|
|
71
|
+
});
|
|
72
|
+
expect(geometry.contentBounds.y).toBe(children.y + children.height);
|
|
73
|
+
expect(geometry.bounds.height).toBe(children.height + content.height);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('asymmetric padding insets are honored', () => {
|
|
77
|
+
const geometry = deriveGroupGeometry(content, children, {
|
|
78
|
+
type: 'derived',
|
|
79
|
+
padding: { top: 4, right: 40, bottom: 8, left: 2 },
|
|
80
|
+
});
|
|
81
|
+
expect(geometry.childArea.x).toBe(children.x - 2);
|
|
82
|
+
expect(geometry.childArea.width).toBe(children.width + 2 + 40);
|
|
83
|
+
expect(geometry.childArea.height).toBe(children.height + 4 + 8);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('wide content centers over a narrow child area', () => {
|
|
87
|
+
const geometry = deriveGroupGeometry(
|
|
88
|
+
{ width: 600, height: 40 },
|
|
89
|
+
children,
|
|
90
|
+
{ type: 'derived', padding: 0 },
|
|
91
|
+
);
|
|
92
|
+
expect(geometry.bounds.width).toBe(600);
|
|
93
|
+
const childCenter = children.x + children.width / 2;
|
|
94
|
+
expect(geometry.bounds.x + geometry.bounds.width / 2).toBeCloseTo(
|
|
95
|
+
childCenter,
|
|
96
|
+
);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('empty group anchors at the given point with the minimum child area', () => {
|
|
100
|
+
const geometry = deriveGroupGeometry(content, null, {
|
|
101
|
+
type: 'derived',
|
|
102
|
+
minChildArea: { width: 200, height: 100 },
|
|
103
|
+
}, { x: 50, y: 60 });
|
|
104
|
+
expect(geometry.contentBounds.y).toBe(60);
|
|
105
|
+
expect(geometry.childArea.y).toBe(60 + content.height);
|
|
106
|
+
expect(geometry.childArea.width).toBeGreaterThanOrEqual(200);
|
|
107
|
+
expect(geometry.childArea.height).toBeGreaterThanOrEqual(100);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
describe('fixedGroupGeometry', () => {
|
|
112
|
+
const bounds = { x: 0, y: 0, width: 400, height: 300 };
|
|
113
|
+
|
|
114
|
+
it('carves the content block and insets the child area', () => {
|
|
115
|
+
const geometry = fixedGroupGeometry(bounds, content, {
|
|
116
|
+
type: 'fixed',
|
|
117
|
+
padding: 12,
|
|
118
|
+
});
|
|
119
|
+
expect(geometry.bounds).toEqual(bounds);
|
|
120
|
+
expect(geometry.contentBounds).toEqual({
|
|
121
|
+
x: 0,
|
|
122
|
+
y: 0,
|
|
123
|
+
width: 400,
|
|
124
|
+
height: 40,
|
|
125
|
+
});
|
|
126
|
+
expect(geometry.childArea).toEqual({
|
|
127
|
+
x: 12,
|
|
128
|
+
y: 40 + 12,
|
|
129
|
+
width: 400 - 24,
|
|
130
|
+
height: 300 - 40 - 24,
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('content on the right', () => {
|
|
135
|
+
const geometry = fixedGroupGeometry(bounds, content, {
|
|
136
|
+
type: 'fixed',
|
|
137
|
+
content: 'right',
|
|
138
|
+
padding: 0,
|
|
139
|
+
});
|
|
140
|
+
expect(geometry.contentBounds.x).toBe(400 - content.width);
|
|
141
|
+
expect(geometry.childArea.width).toBe(400 - content.width);
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
describe('resolveGroupPadding', () => {
|
|
146
|
+
it('expands numbers and fills partial insets with the default', () => {
|
|
147
|
+
expect(resolveGroupPadding(5)).toEqual({ top: 5, right: 5, bottom: 5, left: 5 });
|
|
148
|
+
expect(resolveGroupPadding({ left: 2 })).toEqual({ top: 16, right: 16, bottom: 16, left: 2 });
|
|
149
|
+
expect(resolveGroupPadding(undefined)).toEqual({ top: 16, right: 16, bottom: 16, left: 16 });
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
describe('getEdgeOwner (studio digraph containment)', () => {
|
|
154
|
+
// machine A { X { T }, Y { Q, T2 } }, machine B { Y2 }
|
|
155
|
+
const n = (id: string, parentId?: string) =>
|
|
156
|
+
({ ...createNode({ id }), ...(parentId ? { parentId } : {}) });
|
|
157
|
+
const nodes = [
|
|
158
|
+
n('A'), n('B'),
|
|
159
|
+
n('X', 'A'), n('Y', 'A'),
|
|
160
|
+
n('T', 'X'), n('Q', 'Y'), n('T2', 'Y'),
|
|
161
|
+
n('Y2', 'B'),
|
|
162
|
+
];
|
|
163
|
+
const owner = (sourceId: string, targetId: string) =>
|
|
164
|
+
getEdgeOwner({ sourceId, targetId }, nodes);
|
|
165
|
+
|
|
166
|
+
it('A.X → A.Y belongs to A', () => expect(owner('X', 'Y')).toBe('A'));
|
|
167
|
+
it('A.X → A.Y.Q belongs to A', () => expect(owner('X', 'Q')).toBe('A'));
|
|
168
|
+
it('A.X → B.Y belongs to the root', () => expect(owner('X', 'Y2')).toBeNull());
|
|
169
|
+
it('A.X.T → A.Y.T belongs to A', () => expect(owner('T', 'T2')).toBe('A'));
|
|
170
|
+
it('A.X → A.X (self) belongs to A', () => expect(owner('X', 'X')).toBe('A'));
|
|
171
|
+
it('A.X → A.X.T (own descendant) belongs to X', () => expect(owner('X', 'T')).toBe('X'));
|
|
172
|
+
it('A.X.T → A.X (own ancestor) belongs to X', () => expect(owner('T', 'X')).toBe('X'));
|
|
173
|
+
it('top-level self-edge belongs to the root', () => expect(owner('A', 'A')).toBeNull());
|
|
174
|
+
it('A → B (top level) belongs to the root', () => expect(owner('A', 'B')).toBeNull());
|
|
175
|
+
});
|
package/src/hierarchy.ts
ADDED
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
import type { EdgeDirection, FlowNode, Point, Rect, Size } from './types';
|
|
2
|
+
import { indexNodes } from './graph-index';
|
|
3
|
+
|
|
4
|
+
// Group hierarchy — pure geometry, fully policy-driven.
|
|
5
|
+
//
|
|
6
|
+
// Two sizing policies, configurable per node (`node.group`):
|
|
7
|
+
//
|
|
8
|
+
// - `derived` (default semantics à la the Stately editor): the parent's
|
|
9
|
+
// bounds are a pure function of its children — content block of inherent
|
|
10
|
+
// size on one side, children area (children union + padding) on the other.
|
|
11
|
+
// Because nothing is stored, containment can never be violated and sizing
|
|
12
|
+
// is perfectly reversible: move a child out and back, and the parent's
|
|
13
|
+
// bounds are bit-identical. (Contrast React Flow's `expandParent`, which
|
|
14
|
+
// bakes growth into the parent and has no auto-collapse.)
|
|
15
|
+
//
|
|
16
|
+
// - `fixed`: the parent's stored geometry is authoritative (resizable
|
|
17
|
+
// container); children can optionally be clamped to the child area per
|
|
18
|
+
// axis.
|
|
19
|
+
//
|
|
20
|
+
// Constraint-based child positioning ("child A always at parent center-x")
|
|
21
|
+
// is intentionally out of scope for now; `deriveGroupGeometry` is the seam
|
|
22
|
+
// where a constraint pass will slot in (adjust child rects before the union
|
|
23
|
+
// is taken).
|
|
24
|
+
|
|
25
|
+
export type GroupContentSide = 'top' | 'left' | 'bottom' | 'right';
|
|
26
|
+
|
|
27
|
+
export type GroupInsets = {
|
|
28
|
+
top: number;
|
|
29
|
+
right: number;
|
|
30
|
+
bottom: number;
|
|
31
|
+
left: number;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type GroupLayout =
|
|
35
|
+
| {
|
|
36
|
+
type: 'derived';
|
|
37
|
+
/** Which side the content block occupies (default 'top'). */
|
|
38
|
+
content?: GroupContentSide;
|
|
39
|
+
/** Padding around the children union (default 16). */
|
|
40
|
+
padding?: number | Partial<GroupInsets>;
|
|
41
|
+
/** Minimum child-area size when the group is empty or tiny. */
|
|
42
|
+
minChildArea?: Size;
|
|
43
|
+
/**
|
|
44
|
+
* Width of the connectable boundary inside the child surface. When set,
|
|
45
|
+
* the content block and this boundary are connectable, while the child
|
|
46
|
+
* surface interior remains canvas. Omit to keep the full group body
|
|
47
|
+
* connectable.
|
|
48
|
+
*/
|
|
49
|
+
connectionBoundaryWidth?: number;
|
|
50
|
+
/** Rank direction for this group's children during hierarchical layout. */
|
|
51
|
+
direction?: EdgeDirection;
|
|
52
|
+
}
|
|
53
|
+
| {
|
|
54
|
+
type: 'fixed';
|
|
55
|
+
content?: GroupContentSide;
|
|
56
|
+
padding?: number | Partial<GroupInsets>;
|
|
57
|
+
/**
|
|
58
|
+
* Size of the content block. Unlike `derived` groups (whose stored
|
|
59
|
+
* width/height ARE the content size), a fixed group's stored
|
|
60
|
+
* width/height describe the overall bounds, so the content block is
|
|
61
|
+
* configured here (default: no content block).
|
|
62
|
+
*/
|
|
63
|
+
contentSize?: Size;
|
|
64
|
+
/** Clamp child drags to the child area (default 'none'). */
|
|
65
|
+
clampChildren?: 'x' | 'y' | 'both' | 'none';
|
|
66
|
+
/**
|
|
67
|
+
* Width of the connectable boundary inside the child surface. When set,
|
|
68
|
+
* the content block and this boundary are connectable, while the child
|
|
69
|
+
* surface interior remains canvas. Omit to keep the full group body
|
|
70
|
+
* connectable.
|
|
71
|
+
*/
|
|
72
|
+
connectionBoundaryWidth?: number;
|
|
73
|
+
/** Rank direction for this group's children during hierarchical layout. */
|
|
74
|
+
direction?: EdgeDirection;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export type GroupGeometry = {
|
|
78
|
+
/** Overall group bounds. */
|
|
79
|
+
bounds: Rect;
|
|
80
|
+
/** The content block (title/body — the drag handle by convention). */
|
|
81
|
+
contentBounds: Rect;
|
|
82
|
+
/** Where children live (already inset by padding for `derived`). */
|
|
83
|
+
childArea: Rect;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export function resolveGroupPadding(
|
|
87
|
+
padding: number | Partial<GroupInsets> | undefined,
|
|
88
|
+
): GroupInsets {
|
|
89
|
+
if (typeof padding === 'number') {
|
|
90
|
+
return { top: padding, right: padding, bottom: padding, left: padding };
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
top: padding?.top ?? 16,
|
|
94
|
+
right: padding?.right ?? 16,
|
|
95
|
+
bottom: padding?.bottom ?? 16,
|
|
96
|
+
left: padding?.left ?? 16,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function isGroup(node: FlowNode): boolean {
|
|
101
|
+
return node.group !== undefined;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function isDerivedGroup(node: FlowNode): boolean {
|
|
105
|
+
return node.group?.type === 'derived';
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function getNodeIdsWithDescendants(
|
|
109
|
+
nodes: readonly FlowNode[],
|
|
110
|
+
rootIds: ReadonlySet<string>,
|
|
111
|
+
): Set<string> {
|
|
112
|
+
const ids = new Set(rootIds);
|
|
113
|
+
let changed = true;
|
|
114
|
+
while (changed) {
|
|
115
|
+
changed = false;
|
|
116
|
+
for (const node of nodes) {
|
|
117
|
+
if (node.parentId && ids.has(node.parentId) && !ids.has(node.id)) {
|
|
118
|
+
ids.add(node.id);
|
|
119
|
+
changed = true;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return ids;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Derive a `derived` group's geometry from its content size and the union of
|
|
128
|
+
* its children's bounds (pass null when the group has no children — the
|
|
129
|
+
* anchor point then places the group's top-left).
|
|
130
|
+
*/
|
|
131
|
+
export function deriveGroupGeometry(
|
|
132
|
+
contentSize: Size,
|
|
133
|
+
childrenBounds: Rect | null,
|
|
134
|
+
layout: Extract<GroupLayout, { type: 'derived' }>,
|
|
135
|
+
/** Anchor (stored node position) used only when there are no children. */
|
|
136
|
+
anchor: Point = { x: 0, y: 0 },
|
|
137
|
+
): GroupGeometry {
|
|
138
|
+
const side = layout.content ?? 'top';
|
|
139
|
+
const padding = resolveGroupPadding(layout.padding);
|
|
140
|
+
const minArea = layout.minChildArea ?? { width: 0, height: 0 };
|
|
141
|
+
|
|
142
|
+
// Children area = children union expanded by padding (or the minimum).
|
|
143
|
+
let childArea: Rect;
|
|
144
|
+
if (childrenBounds) {
|
|
145
|
+
childArea = {
|
|
146
|
+
x: childrenBounds.x - padding.left,
|
|
147
|
+
y: childrenBounds.y - padding.top,
|
|
148
|
+
width: Math.max(
|
|
149
|
+
childrenBounds.width + padding.left + padding.right,
|
|
150
|
+
minArea.width,
|
|
151
|
+
),
|
|
152
|
+
height: Math.max(
|
|
153
|
+
childrenBounds.height + padding.top + padding.bottom,
|
|
154
|
+
minArea.height,
|
|
155
|
+
),
|
|
156
|
+
};
|
|
157
|
+
} else {
|
|
158
|
+
// Anchored placement for empty groups: content at the anchor, child area
|
|
159
|
+
// adjacent to it.
|
|
160
|
+
const horizontal = side === 'top' || side === 'bottom';
|
|
161
|
+
childArea = {
|
|
162
|
+
x: anchor.x,
|
|
163
|
+
y: anchor.y,
|
|
164
|
+
width: Math.max(
|
|
165
|
+
minArea.width,
|
|
166
|
+
padding.left + padding.right,
|
|
167
|
+
horizontal ? contentSize.width : 0,
|
|
168
|
+
),
|
|
169
|
+
height: Math.max(
|
|
170
|
+
minArea.height,
|
|
171
|
+
padding.top + padding.bottom,
|
|
172
|
+
horizontal ? 0 : contentSize.height,
|
|
173
|
+
),
|
|
174
|
+
};
|
|
175
|
+
if (side === 'top') childArea.y += contentSize.height;
|
|
176
|
+
if (side === 'left') childArea.x += contentSize.width;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return composeGeometry(contentSize, childArea, side);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Geometry for a `fixed` group: stored bounds are authoritative; the content
|
|
184
|
+
* block is carved from the configured side and the child area is the
|
|
185
|
+
* remainder inset by padding.
|
|
186
|
+
*/
|
|
187
|
+
export function fixedGroupGeometry(
|
|
188
|
+
bounds: Rect,
|
|
189
|
+
contentSize: Size,
|
|
190
|
+
layout: Extract<GroupLayout, { type: 'fixed' }>,
|
|
191
|
+
): GroupGeometry {
|
|
192
|
+
const side = layout.content ?? 'top';
|
|
193
|
+
const padding = resolveGroupPadding(layout.padding);
|
|
194
|
+
|
|
195
|
+
let contentBounds: Rect;
|
|
196
|
+
let childArea: Rect;
|
|
197
|
+
if (side === 'top' || side === 'bottom') {
|
|
198
|
+
const contentHeight = Math.min(contentSize.height, bounds.height);
|
|
199
|
+
contentBounds = {
|
|
200
|
+
x: bounds.x,
|
|
201
|
+
y: side === 'top' ? bounds.y : bounds.y + bounds.height - contentHeight,
|
|
202
|
+
width: bounds.width,
|
|
203
|
+
height: contentHeight,
|
|
204
|
+
};
|
|
205
|
+
childArea = {
|
|
206
|
+
x: bounds.x + padding.left,
|
|
207
|
+
y:
|
|
208
|
+
(side === 'top' ? bounds.y + contentHeight : bounds.y) + padding.top,
|
|
209
|
+
width: Math.max(bounds.width - padding.left - padding.right, 0),
|
|
210
|
+
height: Math.max(
|
|
211
|
+
bounds.height - contentHeight - padding.top - padding.bottom,
|
|
212
|
+
0,
|
|
213
|
+
),
|
|
214
|
+
};
|
|
215
|
+
} else {
|
|
216
|
+
const contentWidth = Math.min(contentSize.width, bounds.width);
|
|
217
|
+
contentBounds = {
|
|
218
|
+
x: side === 'left' ? bounds.x : bounds.x + bounds.width - contentWidth,
|
|
219
|
+
y: bounds.y,
|
|
220
|
+
width: contentWidth,
|
|
221
|
+
height: bounds.height,
|
|
222
|
+
};
|
|
223
|
+
childArea = {
|
|
224
|
+
x:
|
|
225
|
+
(side === 'left' ? bounds.x + contentWidth : bounds.x) + padding.left,
|
|
226
|
+
y: bounds.y + padding.top,
|
|
227
|
+
width: Math.max(
|
|
228
|
+
bounds.width - contentWidth - padding.left - padding.right,
|
|
229
|
+
0,
|
|
230
|
+
),
|
|
231
|
+
height: Math.max(bounds.height - padding.top - padding.bottom, 0),
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return { bounds, contentBounds, childArea };
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function composeGeometry(
|
|
239
|
+
contentSize: Size,
|
|
240
|
+
childArea: Rect,
|
|
241
|
+
side: GroupContentSide,
|
|
242
|
+
): GroupGeometry {
|
|
243
|
+
if (side === 'top' || side === 'bottom') {
|
|
244
|
+
const width = Math.max(childArea.width, contentSize.width);
|
|
245
|
+
const x = childArea.x - (width - childArea.width) / 2;
|
|
246
|
+
const contentY =
|
|
247
|
+
side === 'top'
|
|
248
|
+
? childArea.y - contentSize.height
|
|
249
|
+
: childArea.y + childArea.height;
|
|
250
|
+
return {
|
|
251
|
+
bounds: {
|
|
252
|
+
x,
|
|
253
|
+
y: side === 'top' ? contentY : childArea.y,
|
|
254
|
+
width,
|
|
255
|
+
height: contentSize.height + childArea.height,
|
|
256
|
+
},
|
|
257
|
+
contentBounds: { x, y: contentY, width, height: contentSize.height },
|
|
258
|
+
childArea,
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const height = Math.max(childArea.height, contentSize.height);
|
|
263
|
+
const y = childArea.y - (height - childArea.height) / 2;
|
|
264
|
+
const contentX =
|
|
265
|
+
side === 'left'
|
|
266
|
+
? childArea.x - contentSize.width
|
|
267
|
+
: childArea.x + childArea.width;
|
|
268
|
+
return {
|
|
269
|
+
bounds: {
|
|
270
|
+
x: side === 'left' ? contentX : childArea.x,
|
|
271
|
+
y,
|
|
272
|
+
width: contentSize.width + childArea.width,
|
|
273
|
+
height,
|
|
274
|
+
},
|
|
275
|
+
contentBounds: { x: contentX, y, width: contentSize.width, height },
|
|
276
|
+
childArea,
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// Edge containment (studio digraph semantics)
|
|
281
|
+
//
|
|
282
|
+
// An edge "belongs to" the deepest node that contains both endpoints, where
|
|
283
|
+
// an endpoint counts as containing itself — so A.X → A.Y belongs to A,
|
|
284
|
+
// A.X → A.Y.Q belongs to A, A.X → A.X.Q belongs to X (the source contains
|
|
285
|
+
// the target), A.X → B.Y belongs to the root (null), and a self-edge
|
|
286
|
+
// A.X → A.X belongs to A (the source's parent). Renderless rule — derived
|
|
287
|
+
// groups use it to include owned edge labels in their child bounds, with
|
|
288
|
+
// one exception mirrored from the Stately editor: a node's self-edge never
|
|
289
|
+
// constrains that node's own bounds (it may hang outside).
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* The id of the node that contains this edge, or null for the root.
|
|
293
|
+
* Override per engine via `FlowEngineOptions.edgeOwner`.
|
|
294
|
+
*/
|
|
295
|
+
export function getEdgeOwner(
|
|
296
|
+
edge: { sourceId: string; targetId: string },
|
|
297
|
+
nodes: FlowNode[],
|
|
298
|
+
): string | null {
|
|
299
|
+
return getEdgeOwnerFromIndex(edge, indexNodes(nodes));
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** Internal indexed variant for hot paths that already index the graph. */
|
|
303
|
+
export function getEdgeOwnerFromIndex(
|
|
304
|
+
edge: { sourceId: string; targetId: string },
|
|
305
|
+
index: ReadonlyMap<string, FlowNode>,
|
|
306
|
+
): string | null {
|
|
307
|
+
const source = index.get(edge.sourceId);
|
|
308
|
+
const target = index.get(edge.targetId);
|
|
309
|
+
if (!source) return null;
|
|
310
|
+
if (!target || source.id === target.id) return source.parentId ?? null;
|
|
311
|
+
|
|
312
|
+
// Ancestor chain of the source, including the source itself.
|
|
313
|
+
const sourceChain = new Set<string>([source.id]);
|
|
314
|
+
let marker = source.parentId ?? null;
|
|
315
|
+
while (marker && !sourceChain.has(marker)) {
|
|
316
|
+
sourceChain.add(marker);
|
|
317
|
+
marker = index.get(marker)?.parentId ?? null;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// First node on the target's chain (including the target) also on the
|
|
321
|
+
// source's chain is the container.
|
|
322
|
+
let candidate: string | null = target.id;
|
|
323
|
+
const seen = new Set<string>();
|
|
324
|
+
while (candidate && !seen.has(candidate)) {
|
|
325
|
+
if (sourceChain.has(candidate)) return candidate;
|
|
326
|
+
seen.add(candidate);
|
|
327
|
+
candidate = index.get(candidate)?.parentId ?? null;
|
|
328
|
+
}
|
|
329
|
+
return null;
|
|
330
|
+
}
|