@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,358 @@
|
|
|
1
|
+
import type { FlowChangeSet } from './changeSet';
|
|
2
|
+
import type {
|
|
3
|
+
CreateEdgeInput,
|
|
4
|
+
CreateNodeInput,
|
|
5
|
+
FlowController,
|
|
6
|
+
FlowGeometryOptions,
|
|
7
|
+
FlowTransactionOptions,
|
|
8
|
+
} from './createFlow';
|
|
9
|
+
import { normalizeGeometryPosition } from './geometry';
|
|
10
|
+
import type { FlowEdge, FlowNode, Point, Rect, Size } from './types';
|
|
11
|
+
import { FlowGraphValidationError, type FlowResult } from './validation';
|
|
12
|
+
|
|
13
|
+
export type FlowExternalEditOperation<
|
|
14
|
+
TNodeData = unknown,
|
|
15
|
+
TEdgeData = unknown,
|
|
16
|
+
TLabelData = unknown
|
|
17
|
+
> =
|
|
18
|
+
| { kind: 'node.create'; node: CreateNodeInput<TNodeData> & { id: string } }
|
|
19
|
+
| { kind: 'node.delete'; id: string }
|
|
20
|
+
| {
|
|
21
|
+
kind: 'node.move';
|
|
22
|
+
id: string;
|
|
23
|
+
position: Partial<Point>;
|
|
24
|
+
options?: FlowGeometryOptions;
|
|
25
|
+
}
|
|
26
|
+
| {
|
|
27
|
+
kind: 'node.resize';
|
|
28
|
+
id: string;
|
|
29
|
+
bounds: Partial<Rect>;
|
|
30
|
+
options?: FlowGeometryOptions;
|
|
31
|
+
}
|
|
32
|
+
| {
|
|
33
|
+
kind: 'node.contentSize';
|
|
34
|
+
id: string;
|
|
35
|
+
size: Partial<Size>;
|
|
36
|
+
position?: Partial<Point>;
|
|
37
|
+
options?: FlowGeometryOptions;
|
|
38
|
+
}
|
|
39
|
+
| {
|
|
40
|
+
kind: 'node.data';
|
|
41
|
+
id: string;
|
|
42
|
+
update: (data: TNodeData, node: FlowNode<TNodeData>) => TNodeData;
|
|
43
|
+
}
|
|
44
|
+
| {
|
|
45
|
+
kind: 'edge.create';
|
|
46
|
+
edge: CreateEdgeInput<TEdgeData, TLabelData> & { id: string };
|
|
47
|
+
}
|
|
48
|
+
| { kind: 'edge.delete'; id: string }
|
|
49
|
+
| {
|
|
50
|
+
kind: 'edge.reconnect';
|
|
51
|
+
id: string;
|
|
52
|
+
endpoints: {
|
|
53
|
+
sourceId?: string;
|
|
54
|
+
targetId?: string;
|
|
55
|
+
sourcePort?: string | null;
|
|
56
|
+
targetPort?: string | null;
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
| {
|
|
60
|
+
kind: 'edge.data';
|
|
61
|
+
id: string;
|
|
62
|
+
update: (
|
|
63
|
+
data: TEdgeData,
|
|
64
|
+
edge: FlowEdge<TEdgeData, TLabelData>
|
|
65
|
+
) => TEdgeData;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export type FlowExternalEdit<
|
|
69
|
+
TNodeData = unknown,
|
|
70
|
+
TEdgeData = unknown,
|
|
71
|
+
TLabelData = unknown
|
|
72
|
+
> = {
|
|
73
|
+
commit: (
|
|
74
|
+
operations: readonly FlowExternalEditOperation<
|
|
75
|
+
TNodeData,
|
|
76
|
+
TEdgeData,
|
|
77
|
+
TLabelData
|
|
78
|
+
>[],
|
|
79
|
+
options?: FlowTransactionOptions
|
|
80
|
+
) => FlowResult<FlowChangeSet<TNodeData, TEdgeData, TLabelData> | null>;
|
|
81
|
+
cancel: () => void;
|
|
82
|
+
dispose: () => void;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/** Native hosts own previews. Supplied fields differing from the baseline win at commit. */
|
|
86
|
+
export function createFlowExternalEdit<TNodeData, TEdgeData, TLabelData>(
|
|
87
|
+
flow: FlowController<TNodeData, TEdgeData, TLabelData>,
|
|
88
|
+
options: FlowTransactionOptions = {}
|
|
89
|
+
): FlowExternalEdit<TNodeData, TEdgeData, TLabelData> {
|
|
90
|
+
const graph = flow.getContext().graph;
|
|
91
|
+
const baselineNodes = new Map(
|
|
92
|
+
graph.nodes.map((node) => [node.id, flow.getBounds(node.id)])
|
|
93
|
+
);
|
|
94
|
+
const baselineEdges = new Map(
|
|
95
|
+
graph.edges.map((edge) => [
|
|
96
|
+
edge.id,
|
|
97
|
+
{
|
|
98
|
+
sourceId: edge.sourceId,
|
|
99
|
+
targetId: edge.targetId,
|
|
100
|
+
sourcePort: edge.sourcePort,
|
|
101
|
+
targetPort: edge.targetPort,
|
|
102
|
+
},
|
|
103
|
+
])
|
|
104
|
+
);
|
|
105
|
+
const removedNodes = new Set<string>();
|
|
106
|
+
const removedEdges = new Set<string>();
|
|
107
|
+
let closed = false;
|
|
108
|
+
let disposed = false;
|
|
109
|
+
const commits = flow.on('commit', ({ changeSet }) => {
|
|
110
|
+
for (const node of changeSet.nodes.removed) removedNodes.add(node.id);
|
|
111
|
+
for (const edge of changeSet.edges.removed) removedEdges.add(edge.id);
|
|
112
|
+
});
|
|
113
|
+
const destruction = flow.on('destroy', () => dispose());
|
|
114
|
+
function dispose() {
|
|
115
|
+
if (disposed) return;
|
|
116
|
+
disposed = true;
|
|
117
|
+
closed = true;
|
|
118
|
+
commits.unsubscribe();
|
|
119
|
+
destruction.unsubscribe();
|
|
120
|
+
baselineNodes.clear();
|
|
121
|
+
baselineEdges.clear();
|
|
122
|
+
removedNodes.clear();
|
|
123
|
+
removedEdges.clear();
|
|
124
|
+
}
|
|
125
|
+
function failure(message: string): FlowResult<never> {
|
|
126
|
+
return {
|
|
127
|
+
ok: false,
|
|
128
|
+
error: new FlowGraphValidationError([
|
|
129
|
+
{ code: 'external_edit_failed', path: '$', message },
|
|
130
|
+
]),
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
cancel: dispose,
|
|
135
|
+
dispose,
|
|
136
|
+
commit(operations, commitOptions) {
|
|
137
|
+
if (closed) return failure('Flow external edit session is closed.');
|
|
138
|
+
closed = true;
|
|
139
|
+
try {
|
|
140
|
+
if (operations.length && !flow.can('edit'))
|
|
141
|
+
return failure('Flow external edits are not allowed.');
|
|
142
|
+
const finalFields = new Map<string, unknown>();
|
|
143
|
+
for (const operation of operations) {
|
|
144
|
+
const fields =
|
|
145
|
+
operation.kind === 'node.move'
|
|
146
|
+
? operation.position
|
|
147
|
+
: operation.kind === 'node.resize'
|
|
148
|
+
? operation.bounds
|
|
149
|
+
: operation.kind === 'node.contentSize'
|
|
150
|
+
? { ...operation.size, ...operation.position }
|
|
151
|
+
: operation.kind === 'edge.reconnect'
|
|
152
|
+
? operation.endpoints
|
|
153
|
+
: undefined;
|
|
154
|
+
if (!fields || !('id' in operation)) continue;
|
|
155
|
+
const kind = operation.kind.startsWith('node.') ? 'node' : 'edge';
|
|
156
|
+
for (const [key, value] of Object.entries(fields)) {
|
|
157
|
+
if (value !== undefined)
|
|
158
|
+
finalFields.set(`${kind}:${operation.id}:${key}`, value);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
function reconcile<T>(
|
|
162
|
+
id: string,
|
|
163
|
+
key: string,
|
|
164
|
+
value: T | undefined,
|
|
165
|
+
baseline: T,
|
|
166
|
+
current: T,
|
|
167
|
+
fresh = false
|
|
168
|
+
): T {
|
|
169
|
+
if (fresh) return value === undefined ? current : value;
|
|
170
|
+
if (
|
|
171
|
+
value === undefined ||
|
|
172
|
+
finalFields.get(`${id}:${key}`) === baseline ||
|
|
173
|
+
value === baseline
|
|
174
|
+
)
|
|
175
|
+
return current;
|
|
176
|
+
return value;
|
|
177
|
+
}
|
|
178
|
+
const result = flow.transaction(
|
|
179
|
+
(draft) => {
|
|
180
|
+
const createdNodes = new Set<string>();
|
|
181
|
+
const createdEdges = new Set<string>();
|
|
182
|
+
for (const operation of operations) {
|
|
183
|
+
switch (operation.kind) {
|
|
184
|
+
case 'node.create':
|
|
185
|
+
if (removedNodes.has(operation.node.id)) break;
|
|
186
|
+
draft.nodes.create(operation.node);
|
|
187
|
+
createdNodes.add(operation.node.id);
|
|
188
|
+
break;
|
|
189
|
+
case 'edge.create':
|
|
190
|
+
if (removedEdges.has(operation.edge.id)) break;
|
|
191
|
+
draft.edges.create(operation.edge);
|
|
192
|
+
createdEdges.add(operation.edge.id);
|
|
193
|
+
break;
|
|
194
|
+
case 'node.delete':
|
|
195
|
+
case 'node.move':
|
|
196
|
+
case 'node.resize':
|
|
197
|
+
case 'node.contentSize':
|
|
198
|
+
case 'node.data': {
|
|
199
|
+
const { id } = operation;
|
|
200
|
+
if (
|
|
201
|
+
!createdNodes.has(id) &&
|
|
202
|
+
(!baselineNodes.has(id) || removedNodes.has(id))
|
|
203
|
+
)
|
|
204
|
+
break;
|
|
205
|
+
const node = draft.getNode(id);
|
|
206
|
+
if (!node) break;
|
|
207
|
+
if (operation.kind === 'node.delete') {
|
|
208
|
+
draft.nodes.delete(id);
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
if (operation.kind === 'node.data') {
|
|
212
|
+
draft.nodes.setData(id, operation.update);
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
const current = draft.getNodeBounds(id);
|
|
216
|
+
if (!current) break;
|
|
217
|
+
const baseline = createdNodes.has(id)
|
|
218
|
+
? current
|
|
219
|
+
: baselineNodes.get(id) ?? current;
|
|
220
|
+
const supplied =
|
|
221
|
+
operation.kind === 'node.move'
|
|
222
|
+
? operation.position
|
|
223
|
+
: operation.kind === 'node.resize'
|
|
224
|
+
? operation.bounds
|
|
225
|
+
: { ...operation.size, ...operation.position };
|
|
226
|
+
const next = { ...current };
|
|
227
|
+
for (const key of ['x', 'y', 'width', 'height'] as const) {
|
|
228
|
+
const raw = supplied[key as keyof typeof supplied] as
|
|
229
|
+
| number
|
|
230
|
+
| undefined;
|
|
231
|
+
const value =
|
|
232
|
+
baselineNodes.has(id) && !createdNodes.has(id)
|
|
233
|
+
? reconcile(
|
|
234
|
+
`node:${id}`,
|
|
235
|
+
key,
|
|
236
|
+
raw,
|
|
237
|
+
baseline[key],
|
|
238
|
+
current[key]
|
|
239
|
+
)
|
|
240
|
+
: raw ?? current[key];
|
|
241
|
+
next[key] =
|
|
242
|
+
raw !== undefined &&
|
|
243
|
+
raw !== baseline[key] &&
|
|
244
|
+
value !== current[key] &&
|
|
245
|
+
(key === 'x' || key === 'y')
|
|
246
|
+
? normalizeGeometryPosition(
|
|
247
|
+
value,
|
|
248
|
+
operation.options?.positionPrecision
|
|
249
|
+
)
|
|
250
|
+
: value;
|
|
251
|
+
}
|
|
252
|
+
const moved = next.x !== current.x || next.y !== current.y;
|
|
253
|
+
const resized =
|
|
254
|
+
next.width !== current.width ||
|
|
255
|
+
next.height !== current.height;
|
|
256
|
+
const geometryOptions = {
|
|
257
|
+
...operation.options,
|
|
258
|
+
positionPrecision: null,
|
|
259
|
+
};
|
|
260
|
+
if (operation.kind === 'node.move') {
|
|
261
|
+
if (moved) draft.nodes.move(id, next, geometryOptions);
|
|
262
|
+
} else if (operation.kind === 'node.resize') {
|
|
263
|
+
if (moved || resized)
|
|
264
|
+
draft.nodes.resize(id, next, geometryOptions);
|
|
265
|
+
} else if (moved || resized) {
|
|
266
|
+
draft.nodes.setContentSize(id, next, {
|
|
267
|
+
...geometryOptions,
|
|
268
|
+
...(moved ? { position: { x: next.x, y: next.y } } : {}),
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
break;
|
|
272
|
+
}
|
|
273
|
+
case 'edge.delete':
|
|
274
|
+
case 'edge.reconnect':
|
|
275
|
+
case 'edge.data': {
|
|
276
|
+
const { id } = operation;
|
|
277
|
+
if (
|
|
278
|
+
!createdEdges.has(id) &&
|
|
279
|
+
(!baselineEdges.has(id) || removedEdges.has(id))
|
|
280
|
+
)
|
|
281
|
+
break;
|
|
282
|
+
const edge = draft.getEdge(id);
|
|
283
|
+
if (!edge) break;
|
|
284
|
+
if (operation.kind === 'edge.delete') {
|
|
285
|
+
draft.edges.delete(id);
|
|
286
|
+
} else if (operation.kind === 'edge.data') {
|
|
287
|
+
draft.edges.setData(id, operation.update);
|
|
288
|
+
} else {
|
|
289
|
+
const baseline = baselineEdges.get(id) ?? edge;
|
|
290
|
+
const endpoints = operation.endpoints;
|
|
291
|
+
const sourceId = reconcile(
|
|
292
|
+
`edge:${id}`,
|
|
293
|
+
'sourceId',
|
|
294
|
+
endpoints.sourceId,
|
|
295
|
+
baseline.sourceId,
|
|
296
|
+
edge.sourceId,
|
|
297
|
+
createdEdges.has(id)
|
|
298
|
+
);
|
|
299
|
+
const targetId = reconcile(
|
|
300
|
+
`edge:${id}`,
|
|
301
|
+
'targetId',
|
|
302
|
+
endpoints.targetId,
|
|
303
|
+
baseline.targetId,
|
|
304
|
+
edge.targetId,
|
|
305
|
+
createdEdges.has(id)
|
|
306
|
+
);
|
|
307
|
+
const sourcePort = reconcile(
|
|
308
|
+
`edge:${id}`,
|
|
309
|
+
'sourcePort',
|
|
310
|
+
endpoints.sourcePort,
|
|
311
|
+
baseline.sourcePort ?? null,
|
|
312
|
+
edge.sourcePort ?? null,
|
|
313
|
+
createdEdges.has(id)
|
|
314
|
+
);
|
|
315
|
+
const targetPort = reconcile(
|
|
316
|
+
`edge:${id}`,
|
|
317
|
+
'targetPort',
|
|
318
|
+
endpoints.targetPort,
|
|
319
|
+
baseline.targetPort ?? null,
|
|
320
|
+
edge.targetPort ?? null,
|
|
321
|
+
createdEdges.has(id)
|
|
322
|
+
);
|
|
323
|
+
if (
|
|
324
|
+
sourceId !== edge.sourceId ||
|
|
325
|
+
targetId !== edge.targetId ||
|
|
326
|
+
sourcePort !== (edge.sourcePort ?? null) ||
|
|
327
|
+
targetPort !== (edge.targetPort ?? null)
|
|
328
|
+
) {
|
|
329
|
+
draft.edges.reconnect(id, {
|
|
330
|
+
newSourceId: sourceId,
|
|
331
|
+
newTargetId: targetId,
|
|
332
|
+
newSourcePort: sourcePort,
|
|
333
|
+
newTargetPort: targetPort,
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
break;
|
|
338
|
+
}
|
|
339
|
+
default: {
|
|
340
|
+
const exhaustive: never = operation;
|
|
341
|
+
throw new Error(`Unknown external operation: ${exhaustive}`);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
},
|
|
346
|
+
{ ...options, ...commitOptions }
|
|
347
|
+
);
|
|
348
|
+
return { ok: true, value: result.changeSet };
|
|
349
|
+
} catch (error) {
|
|
350
|
+
if (error instanceof FlowGraphValidationError)
|
|
351
|
+
return { ok: false, error };
|
|
352
|
+
return failure(error instanceof Error ? error.message : String(error));
|
|
353
|
+
} finally {
|
|
354
|
+
dispose();
|
|
355
|
+
}
|
|
356
|
+
},
|
|
357
|
+
};
|
|
358
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { createNode, createEdge, createGraph, createPort } from './factories';
|
|
3
|
+
|
|
4
|
+
describe('createNode', () => {
|
|
5
|
+
it('creates node with defaults', () => {
|
|
6
|
+
const node = createNode({ id: 'n1' });
|
|
7
|
+
expect(node.id).toBe('n1');
|
|
8
|
+
expect(node.type).toBe('node');
|
|
9
|
+
expect(node.x).toBe(0);
|
|
10
|
+
expect(node.y).toBe(0);
|
|
11
|
+
expect(node.width).toBe(150);
|
|
12
|
+
expect(node.height).toBe(50);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('overrides defaults', () => {
|
|
16
|
+
const node = createNode({ id: 'n1', x: 100, y: 200, width: 300, height: 80 });
|
|
17
|
+
expect(node.x).toBe(100);
|
|
18
|
+
expect(node.y).toBe(200);
|
|
19
|
+
expect(node.width).toBe(300);
|
|
20
|
+
expect(node.height).toBe(80);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('preserves custom data', () => {
|
|
24
|
+
const node = createNode({ id: 'n1', data: { label: 'hello' } });
|
|
25
|
+
expect(node.data.label).toBe('hello');
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('preserves an application-defined node kind separately from its structural type', () => {
|
|
29
|
+
const node = createNode({ id: 'n1', kind: 'annotation' });
|
|
30
|
+
expect(node).toMatchObject({ type: 'node', kind: 'annotation' });
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe('createEdge', () => {
|
|
35
|
+
it('creates edge with required fields', () => {
|
|
36
|
+
const edge = createEdge({ id: 'e1', sourceId: 'a', targetId: 'b' });
|
|
37
|
+
expect(edge.id).toBe('e1');
|
|
38
|
+
expect(edge.type).toBe('edge');
|
|
39
|
+
expect(edge.sourceId).toBe('a');
|
|
40
|
+
expect(edge.targetId).toBe('b');
|
|
41
|
+
expect(edge.label).toBeUndefined();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('creates edge with label', () => {
|
|
45
|
+
const edge = createEdge({
|
|
46
|
+
id: 'e1',
|
|
47
|
+
sourceId: 'a',
|
|
48
|
+
targetId: 'b',
|
|
49
|
+
label: { width: 60, height: 20, data: { text: 'hi' } },
|
|
50
|
+
});
|
|
51
|
+
expect(edge.label).toBeDefined();
|
|
52
|
+
expect(edge.label!.x).toBe(0);
|
|
53
|
+
expect(edge.label!.y).toBe(0);
|
|
54
|
+
expect(edge.label!.width).toBe(60);
|
|
55
|
+
expect(edge.label!.height).toBe(20);
|
|
56
|
+
expect(edge.label!.data.text).toBe('hi');
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('label has explicit x/y when provided', () => {
|
|
60
|
+
const edge = createEdge({
|
|
61
|
+
id: 'e1',
|
|
62
|
+
sourceId: 'a',
|
|
63
|
+
targetId: 'b',
|
|
64
|
+
label: { x: 100, y: 200, width: 60, height: 20, data: {} },
|
|
65
|
+
});
|
|
66
|
+
expect(edge.label!.x).toBe(100);
|
|
67
|
+
expect(edge.label!.y).toBe(200);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe('createGraph', () => {
|
|
72
|
+
it('creates graph with direction default', () => {
|
|
73
|
+
const g = createGraph({ nodes: [], edges: [] });
|
|
74
|
+
expect(g.direction).toBe('down');
|
|
75
|
+
expect(g.nodes).toEqual([]);
|
|
76
|
+
expect(g.edges).toEqual([]);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('overrides direction', () => {
|
|
80
|
+
const g = createGraph({ nodes: [], edges: [], direction: 'right' });
|
|
81
|
+
expect(g.direction).toBe('right');
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
describe('createPort', () => {
|
|
86
|
+
it('creates graph-first port data', () => {
|
|
87
|
+
const port = createPort({
|
|
88
|
+
name: 'out',
|
|
89
|
+
side: 'bottom',
|
|
90
|
+
direction: 'out',
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
expect(port).toEqual({
|
|
94
|
+
name: 'out',
|
|
95
|
+
side: 'bottom',
|
|
96
|
+
direction: 'out',
|
|
97
|
+
x: 0,
|
|
98
|
+
y: 0,
|
|
99
|
+
width: 12,
|
|
100
|
+
height: 12,
|
|
101
|
+
data: {},
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
});
|
package/src/factories.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
FlowEdge,
|
|
3
|
+
FlowEdgeLabel,
|
|
4
|
+
FlowGraph,
|
|
5
|
+
FlowNode,
|
|
6
|
+
FlowPort,
|
|
7
|
+
} from './types';
|
|
8
|
+
|
|
9
|
+
// Factory functions — convenient constructors with sensible defaults
|
|
10
|
+
|
|
11
|
+
export function createNode<TData = any>(
|
|
12
|
+
opts: Partial<FlowNode<TData>> & { id: string },
|
|
13
|
+
): FlowNode<TData> {
|
|
14
|
+
return {
|
|
15
|
+
type: 'node',
|
|
16
|
+
x: 0,
|
|
17
|
+
y: 0,
|
|
18
|
+
width: 150,
|
|
19
|
+
height: 50,
|
|
20
|
+
data: {} as TData,
|
|
21
|
+
...opts,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function createEdge<TEdgeData = any, TLabelData = any>(
|
|
26
|
+
opts: Omit<Partial<FlowEdge<TEdgeData, TLabelData>>, 'label'> &
|
|
27
|
+
Pick<FlowEdge<TEdgeData, TLabelData>, 'id' | 'sourceId' | 'targetId'> & {
|
|
28
|
+
label?: Partial<FlowEdgeLabel<TLabelData>> & { width: number; height: number };
|
|
29
|
+
},
|
|
30
|
+
): FlowEdge<TEdgeData, TLabelData> {
|
|
31
|
+
const { label, ...rest } = opts;
|
|
32
|
+
return {
|
|
33
|
+
type: 'edge',
|
|
34
|
+
x: 0,
|
|
35
|
+
y: 0,
|
|
36
|
+
width: 0,
|
|
37
|
+
height: 0,
|
|
38
|
+
data: {} as TEdgeData,
|
|
39
|
+
...rest,
|
|
40
|
+
label: label
|
|
41
|
+
? { x: 0, y: 0, data: {} as TLabelData, ...label }
|
|
42
|
+
: undefined,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function createGraph<TNodeData = any, TEdgeData = any>(
|
|
47
|
+
opts: Partial<FlowGraph<TNodeData, TEdgeData>> & {
|
|
48
|
+
nodes: FlowNode<TNodeData>[];
|
|
49
|
+
edges: FlowEdge<TEdgeData>[];
|
|
50
|
+
},
|
|
51
|
+
): FlowGraph<TNodeData, TEdgeData> {
|
|
52
|
+
return {
|
|
53
|
+
direction: 'down',
|
|
54
|
+
...opts,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function createPort<TData = any>(
|
|
59
|
+
opts: Partial<FlowPort<TData>> &
|
|
60
|
+
Pick<FlowPort<TData>, 'name' | 'side' | 'direction'>,
|
|
61
|
+
): FlowPort<TData> {
|
|
62
|
+
return {
|
|
63
|
+
x: 0,
|
|
64
|
+
y: 0,
|
|
65
|
+
width: 12,
|
|
66
|
+
height: 12,
|
|
67
|
+
data: {} as TData,
|
|
68
|
+
...opts,
|
|
69
|
+
};
|
|
70
|
+
}
|
package/src/geometry.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export function normalizeGeometryPosition(
|
|
2
|
+
value: number,
|
|
3
|
+
precision: 0 | 1 | 2 | 3 | 4 | 5 | 6 | null = 0
|
|
4
|
+
): number {
|
|
5
|
+
if (!Number.isFinite(value)) throw new TypeError('Position must be finite.');
|
|
6
|
+
if (precision === null) return value || 0;
|
|
7
|
+
if (!Number.isInteger(precision) || precision < 0 || precision > 6) {
|
|
8
|
+
throw new TypeError(
|
|
9
|
+
'positionPrecision must be null or an integer from 0 to 6.'
|
|
10
|
+
);
|
|
11
|
+
}
|
|
12
|
+
return Math.round(value * 10 ** precision) / 10 ** precision || 0;
|
|
13
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { createEdge, createGraph, createNode } from './factories';
|
|
3
|
+
import {
|
|
4
|
+
getConnectedEdges,
|
|
5
|
+
getIncomers,
|
|
6
|
+
getNeighbors,
|
|
7
|
+
getOutgoers,
|
|
8
|
+
indexEdgeAdjacency,
|
|
9
|
+
} from './graph-index';
|
|
10
|
+
|
|
11
|
+
describe('graph traversal helpers', () => {
|
|
12
|
+
const graph = createGraph({
|
|
13
|
+
nodes: [
|
|
14
|
+
createNode({ id: 'a' }),
|
|
15
|
+
createNode({ id: 'b' }),
|
|
16
|
+
createNode({ id: 'c' }),
|
|
17
|
+
createNode({ id: 'd' }),
|
|
18
|
+
],
|
|
19
|
+
edges: [
|
|
20
|
+
createEdge({ id: 'ab1', sourceId: 'a', targetId: 'b' }),
|
|
21
|
+
createEdge({ id: 'ab2', sourceId: 'a', targetId: 'b' }),
|
|
22
|
+
createEdge({ id: 'ca', sourceId: 'c', targetId: 'a' }),
|
|
23
|
+
createEdge({ id: 'aa', sourceId: 'a', targetId: 'a' }),
|
|
24
|
+
createEdge({ id: 'missing-source', sourceId: 'missing', targetId: 'a' }),
|
|
25
|
+
createEdge({ id: 'missing-target', sourceId: 'a', targetId: 'missing' }),
|
|
26
|
+
],
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('returns unique incomers and outgoers in graph node order', () => {
|
|
30
|
+
expect(getIncomers(graph, 'a').map((node) => node.id)).toEqual(['a', 'c']);
|
|
31
|
+
expect(getOutgoers(graph, 'a').map((node) => node.id)).toEqual(['a', 'b']);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('returns connected edges in graph edge order and skips dangling edges', () => {
|
|
35
|
+
expect(getConnectedEdges(graph, ['a']).map((edge) => edge.id)).toEqual([
|
|
36
|
+
'ab1',
|
|
37
|
+
'ab2',
|
|
38
|
+
'ca',
|
|
39
|
+
'aa',
|
|
40
|
+
]);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('returns unique neighbors for incoming, outgoing, and self edges', () => {
|
|
44
|
+
expect(getNeighbors(graph, 'a').map((node) => node.id)).toEqual([
|
|
45
|
+
'a',
|
|
46
|
+
'b',
|
|
47
|
+
'c',
|
|
48
|
+
]);
|
|
49
|
+
expect(getNeighbors(graph, 'd')).toEqual([]);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('reuses the adjacency index for the same edges array identity', () => {
|
|
53
|
+
expect(indexEdgeAdjacency(graph.edges)).toBe(indexEdgeAdjacency(graph.edges));
|
|
54
|
+
expect(indexEdgeAdjacency([...graph.edges])).not.toBe(indexEdgeAdjacency(graph.edges));
|
|
55
|
+
});
|
|
56
|
+
});
|