@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,632 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { createFlow, fromJSON } from './createFlow';
|
|
3
|
+
import { createEdge, createGraph, createNode, createPort } from './factories';
|
|
4
|
+
import {
|
|
5
|
+
createFlowInputRuntime,
|
|
6
|
+
type FlowInputEvent,
|
|
7
|
+
type FlowInputModifiers,
|
|
8
|
+
} from './input';
|
|
9
|
+
|
|
10
|
+
const NO_MODS: FlowInputModifiers = {
|
|
11
|
+
shift: false,
|
|
12
|
+
meta: false,
|
|
13
|
+
ctrl: false,
|
|
14
|
+
alt: false,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
function keyDown(key: string): FlowInputEvent {
|
|
18
|
+
return { kind: 'key.down', key, editableTarget: false, modifiers: NO_MODS };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function stubRaf() {
|
|
22
|
+
let now = 0;
|
|
23
|
+
let nextId = 1;
|
|
24
|
+
const callbacks = new Map<number, (time: number) => void>();
|
|
25
|
+
vi.stubGlobal('requestAnimationFrame', (callback: (time: number) => void) => {
|
|
26
|
+
const id = nextId++;
|
|
27
|
+
callbacks.set(id, callback);
|
|
28
|
+
return id;
|
|
29
|
+
});
|
|
30
|
+
vi.stubGlobal('cancelAnimationFrame', (id: number) => {
|
|
31
|
+
callbacks.delete(id);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
tick(ms: number) {
|
|
36
|
+
now += ms;
|
|
37
|
+
const pending = [...callbacks.values()];
|
|
38
|
+
callbacks.clear();
|
|
39
|
+
for (const callback of pending) callback(now);
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
afterEach(() => {
|
|
45
|
+
vi.unstubAllGlobals();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe('createFlow', () => {
|
|
49
|
+
it('creates a controller with store, engine, trigger, and passthrough queries', () => {
|
|
50
|
+
const graph = createGraph({
|
|
51
|
+
nodes: [createNode({ id: 'a', x: 10, y: 20 })],
|
|
52
|
+
edges: [],
|
|
53
|
+
});
|
|
54
|
+
const flow = createFlow({
|
|
55
|
+
graph,
|
|
56
|
+
store: { snapToGrid: true },
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
expect(flow.internals.store.getSnapshot().context.snapToGrid).toBe(true);
|
|
60
|
+
expect(flow.getContext().graph.nodes).toHaveLength(1);
|
|
61
|
+
expect(flow.getNode('a')?.x).toBe(10);
|
|
62
|
+
|
|
63
|
+
const graphEvents: string[] = [];
|
|
64
|
+
const subscription = flow.on('graphChanged', (event) => {
|
|
65
|
+
graphEvents.push(event.event.type);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
flow.internals.store.trigger.createNode({ node: createNode({ id: 'b' }) });
|
|
69
|
+
subscription.unsubscribe();
|
|
70
|
+
|
|
71
|
+
expect(graphEvents).toEqual(['createNode']);
|
|
72
|
+
|
|
73
|
+
flow.internals.store.trigger.select({ ids: ['a'] });
|
|
74
|
+
|
|
75
|
+
expect(flow.getSelectionBounds()).toEqual({
|
|
76
|
+
x: 10,
|
|
77
|
+
y: 20,
|
|
78
|
+
width: 150,
|
|
79
|
+
height: 50,
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('forwards connectionEnded events until destroyed', () => {
|
|
84
|
+
const graph = createGraph({
|
|
85
|
+
nodes: [createNode({ id: 'a' }), createNode({ id: 'b' })],
|
|
86
|
+
edges: [],
|
|
87
|
+
});
|
|
88
|
+
const events: unknown[] = [];
|
|
89
|
+
const flow = createFlow({
|
|
90
|
+
graph,
|
|
91
|
+
onConnectionEnd: (event) => events.push(event),
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
flow.internals.store.trigger.connectionStart({
|
|
95
|
+
sourceId: 'a',
|
|
96
|
+
sourcePort: null,
|
|
97
|
+
position: { x: 0, y: 0 },
|
|
98
|
+
});
|
|
99
|
+
flow.internals.store.trigger.connectionEnd({
|
|
100
|
+
target: { kind: 'node', nodeId: 'b', port: null },
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
expect(events).toHaveLength(1);
|
|
104
|
+
|
|
105
|
+
flow.destroy();
|
|
106
|
+
flow.internals.store.trigger.connectionStart({
|
|
107
|
+
sourceId: 'a',
|
|
108
|
+
sourcePort: null,
|
|
109
|
+
position: { x: 0, y: 0 },
|
|
110
|
+
});
|
|
111
|
+
flow.internals.store.trigger.connectionEnd({
|
|
112
|
+
target: { kind: 'node', nodeId: 'b', port: null },
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
expect(events).toHaveLength(1);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('runs the configured layout', async () => {
|
|
119
|
+
const graph = createGraph({
|
|
120
|
+
nodes: [createNode({ id: 'a' }), createNode({ id: 'b' })],
|
|
121
|
+
edges: [createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' })],
|
|
122
|
+
});
|
|
123
|
+
const flow = createFlow({
|
|
124
|
+
graph,
|
|
125
|
+
layout: async (graph) => {
|
|
126
|
+
return new Map(
|
|
127
|
+
graph.nodes.map((node, index) => [
|
|
128
|
+
node.id,
|
|
129
|
+
{ x: index * 100, y: index * 50 },
|
|
130
|
+
]),
|
|
131
|
+
);
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
await flow.layout.run();
|
|
136
|
+
|
|
137
|
+
expect(flow.getNode('a')?.x).toBe(0);
|
|
138
|
+
expect(flow.getNode('a')?.y).toBe(0);
|
|
139
|
+
expect(flow.getNode('b')?.x).toBe(100);
|
|
140
|
+
expect(flow.getNode('b')?.y).toBe(50);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('applies structured layout results including edge label positions', async () => {
|
|
144
|
+
const graph = createGraph({
|
|
145
|
+
nodes: [createNode({ id: 'a' }), createNode({ id: 'b' })],
|
|
146
|
+
edges: [
|
|
147
|
+
createEdge({
|
|
148
|
+
id: 'ab',
|
|
149
|
+
sourceId: 'a',
|
|
150
|
+
targetId: 'b',
|
|
151
|
+
label: { x: 0, y: 0, width: 40, height: 20 },
|
|
152
|
+
}),
|
|
153
|
+
],
|
|
154
|
+
});
|
|
155
|
+
const flow = createFlow({
|
|
156
|
+
graph,
|
|
157
|
+
layout: async (graph) => ({
|
|
158
|
+
positions: new Map(
|
|
159
|
+
graph.nodes.map((node, index) => [
|
|
160
|
+
node.id,
|
|
161
|
+
{ x: index * 100, y: index * 50 },
|
|
162
|
+
]),
|
|
163
|
+
),
|
|
164
|
+
edgeLabelPositions: new Map([['ab', { x: 30, y: 40 }]]),
|
|
165
|
+
}),
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
const result = await flow.layout.run();
|
|
169
|
+
|
|
170
|
+
expect(result.result.edgeLabelPositions?.get('ab')).toEqual({ x: 30, y: 40 });
|
|
171
|
+
expect(result.applied).toBe(true);
|
|
172
|
+
expect(flow.getEdge('ab')?.label).toMatchObject({ x: 30, y: 40 });
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it('manages controller-level projections', () => {
|
|
176
|
+
const graph = createGraph({
|
|
177
|
+
nodes: [createNode({ id: 'a' })],
|
|
178
|
+
edges: [],
|
|
179
|
+
});
|
|
180
|
+
const flow = createFlow({
|
|
181
|
+
graph,
|
|
182
|
+
projections: [
|
|
183
|
+
{
|
|
184
|
+
projection: { id: 'one', nodes: { a: true } },
|
|
185
|
+
mapper: { getNodeAttrs: () => ({ className: 'one' }) },
|
|
186
|
+
},
|
|
187
|
+
],
|
|
188
|
+
});
|
|
189
|
+
const notifications: string[][] = [];
|
|
190
|
+
const unsubscribe = flow.projections.subscribe((projections) => {
|
|
191
|
+
notifications.push(projections.map((entry) => entry.projection.id));
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
expect(flow.projections.getAttrs().nodes.get('a')?.className).toBe('one');
|
|
195
|
+
|
|
196
|
+
flow.projections.add({
|
|
197
|
+
projection: { id: 'two', nodes: { a: true } },
|
|
198
|
+
mapper: { getNodeAttrs: () => ({ className: 'two' }) },
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
expect(flow.projections.getAttrs().nodes.get('a')?.className).toBe(
|
|
202
|
+
'one two',
|
|
203
|
+
);
|
|
204
|
+
expect(notifications).toEqual([['one', 'two']]);
|
|
205
|
+
|
|
206
|
+
flow.projections.remove('one');
|
|
207
|
+
expect(flow.projections.getAttrs().nodes.get('a')?.className).toBe('two');
|
|
208
|
+
|
|
209
|
+
unsubscribe();
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it('throws when layout.run has no algorithm name', async () => {
|
|
213
|
+
const flow = createFlow();
|
|
214
|
+
|
|
215
|
+
await expect(flow.layout.run()).rejects.toThrow(
|
|
216
|
+
'flow.layout.run() was called without a layout algorithm',
|
|
217
|
+
);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it('exposes app-level mutation namespaces', () => {
|
|
221
|
+
const flow = createFlow({
|
|
222
|
+
graph: createGraph({ nodes: [createNode({ id: 'a' })], edges: [] }),
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
const node = flow.nodes.create({ id: 'b', x: 10 });
|
|
226
|
+
expect(node?.x).toBe(10);
|
|
227
|
+
|
|
228
|
+
expect(flow.nodes.update('b', { width: 220 })?.width).toBe(220);
|
|
229
|
+
expect(flow.nodes.setData('b', { label: 'B' })?.data).toMatchObject({
|
|
230
|
+
label: 'B',
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
const edge = flow.edges.create({ id: 'ab', sourceId: 'a', targetId: 'b' });
|
|
234
|
+
expect(edge?.sourceId).toBe('a');
|
|
235
|
+
|
|
236
|
+
expect(flow.edges.update('ab', { reconnectable: false })?.reconnectable).toBe(
|
|
237
|
+
false,
|
|
238
|
+
);
|
|
239
|
+
expect(flow.edges.setData('ab', { label: 'A to B' })?.data).toMatchObject({
|
|
240
|
+
label: 'A to B',
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
flow.selection.set(['b']);
|
|
244
|
+
expect(flow.getContext().selection.has('b')).toBe(true);
|
|
245
|
+
flow.selection.clear();
|
|
246
|
+
expect(flow.getContext().selection.size).toBe(0);
|
|
247
|
+
|
|
248
|
+
flow.viewport.set({ x: 1, y: 2, zoom: 3 });
|
|
249
|
+
expect(flow.getContext().viewport).toEqual({ x: 1, y: 2, zoom: 3 });
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it('exposes graph traversal helpers through the controller and engine', () => {
|
|
253
|
+
const flow = createFlow({
|
|
254
|
+
graph: createGraph({
|
|
255
|
+
nodes: [
|
|
256
|
+
createNode({ id: 'a' }),
|
|
257
|
+
createNode({ id: 'b' }),
|
|
258
|
+
createNode({ id: 'c' }),
|
|
259
|
+
],
|
|
260
|
+
edges: [
|
|
261
|
+
createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' }),
|
|
262
|
+
createEdge({ id: 'ca', sourceId: 'c', targetId: 'a' }),
|
|
263
|
+
],
|
|
264
|
+
}),
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
expect(flow.getIncomers('a').map((node) => node.id)).toEqual(['c']);
|
|
268
|
+
expect(flow.getOutgoers('a').map((node) => node.id)).toEqual(['b']);
|
|
269
|
+
expect(flow.getNeighbors('a').map((node) => node.id)).toEqual(['b', 'c']);
|
|
270
|
+
expect(flow.getConnectedEdges(['a']).map((edge) => edge.id)).toEqual([
|
|
271
|
+
'ab',
|
|
272
|
+
'ca',
|
|
273
|
+
]);
|
|
274
|
+
expect(flow.internals.engine.getIncomers('b').map((node) => node.id)).toEqual(['a']);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it('animates viewport fit when duration is provided', () => {
|
|
278
|
+
const raf = stubRaf();
|
|
279
|
+
const flow = createFlow({
|
|
280
|
+
graph: createGraph({
|
|
281
|
+
nodes: [createNode({ id: 'a', x: 0, y: 0, width: 100, height: 100 })],
|
|
282
|
+
edges: [],
|
|
283
|
+
}),
|
|
284
|
+
});
|
|
285
|
+
flow.internals.store.trigger.updateViewportSize({ width: 400, height: 400 });
|
|
286
|
+
|
|
287
|
+
flow.viewport.fit({ padding: 0, duration: 100, easing: (t) => t });
|
|
288
|
+
expect(flow.getContext().viewport).toEqual({ x: 0, y: 0, zoom: 1 });
|
|
289
|
+
|
|
290
|
+
raf.tick(0);
|
|
291
|
+
raf.tick(50);
|
|
292
|
+
expect(flow.getContext().viewport.zoom).toBeCloseTo(Math.sqrt(2));
|
|
293
|
+
|
|
294
|
+
raf.tick(50);
|
|
295
|
+
expect(flow.getContext().viewport).toEqual({ x: 100, y: 100, zoom: 2 });
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
it('cancels an in-flight viewport animation when a user viewport call happens', () => {
|
|
299
|
+
const raf = stubRaf();
|
|
300
|
+
const flow = createFlow();
|
|
301
|
+
|
|
302
|
+
flow.viewport.set(
|
|
303
|
+
{ x: 100, y: 0, zoom: 1 },
|
|
304
|
+
{ duration: 100, easing: (t) => t },
|
|
305
|
+
);
|
|
306
|
+
raf.tick(0);
|
|
307
|
+
raf.tick(25);
|
|
308
|
+
expect(flow.getContext().viewport.x).toBe(25);
|
|
309
|
+
|
|
310
|
+
flow.viewport.panBy({ x: 10, y: 0 });
|
|
311
|
+
expect(flow.getContext().viewport.x).toBe(35);
|
|
312
|
+
|
|
313
|
+
raf.tick(75);
|
|
314
|
+
expect(flow.getContext().viewport.x).toBe(35);
|
|
315
|
+
});
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
describe('createFlow undo API', () => {
|
|
319
|
+
it('undo/redo with canUndo/canRedo reflect history state', () => {
|
|
320
|
+
const flow = createFlow({
|
|
321
|
+
graph: createGraph({ nodes: [createNode({ id: 'a' })], edges: [] }),
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
expect(flow.history.canUndo()).toBe(false);
|
|
325
|
+
expect(flow.history.canRedo()).toBe(false);
|
|
326
|
+
|
|
327
|
+
flow.nodes.create({ id: 'b', x: 50 });
|
|
328
|
+
expect(flow.history.canUndo()).toBe(true);
|
|
329
|
+
|
|
330
|
+
flow.history.undo();
|
|
331
|
+
expect(flow.getContext().graph.nodes.map((n) => n.id)).toEqual(['a']);
|
|
332
|
+
expect(flow.history.canUndo()).toBe(false);
|
|
333
|
+
expect(flow.history.canRedo()).toBe(true);
|
|
334
|
+
|
|
335
|
+
flow.history.redo();
|
|
336
|
+
expect(flow.getContext().graph.nodes.map((n) => n.id)).toEqual(['a', 'b']);
|
|
337
|
+
expect(flow.history.canRedo()).toBe(false);
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
it('a pointer drag is a single undo step via transactions', () => {
|
|
341
|
+
const flow = createFlow({
|
|
342
|
+
graph: createGraph({
|
|
343
|
+
nodes: [createNode({ id: 'a', x: 0, y: 0 })],
|
|
344
|
+
edges: [],
|
|
345
|
+
}),
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
flow.internals.store.trigger.select({ ids: ['a'] });
|
|
349
|
+
flow.internals.store.trigger.dragStart({ id: 'a', transactionId: 't1' });
|
|
350
|
+
flow.internals.store.trigger.dragMove({ dx: 10, dy: 0 });
|
|
351
|
+
flow.internals.store.trigger.dragMove({ dx: 10, dy: 0 });
|
|
352
|
+
flow.internals.store.trigger.dragEnd();
|
|
353
|
+
flow.internals.store.trigger.commitDrag();
|
|
354
|
+
expect(flow.getNode('a')!.x).toBe(20);
|
|
355
|
+
|
|
356
|
+
flow.history.undo();
|
|
357
|
+
expect(flow.getNode('a')!.x).toBe(0);
|
|
358
|
+
expect(flow.history.canUndo()).toBe(false);
|
|
359
|
+
});
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
describe('createFlow edge defaults and delete hooks', () => {
|
|
363
|
+
it('applies defaultEdgeOptions to controller-created and gesture-created edges', () => {
|
|
364
|
+
const flow = createFlow({
|
|
365
|
+
graph: createGraph({
|
|
366
|
+
nodes: [createNode({ id: 'a' }), createNode({ id: 'b' })],
|
|
367
|
+
edges: [],
|
|
368
|
+
}),
|
|
369
|
+
defaultEdgeOptions: {
|
|
370
|
+
kind: 'default',
|
|
371
|
+
style: { stroke: '#f00' },
|
|
372
|
+
},
|
|
373
|
+
createEdgeOnConnect: true,
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
flow.edges.create({ id: 'manual', sourceId: 'a', targetId: 'b' });
|
|
377
|
+
expect(flow.getEdge('manual')).toMatchObject({
|
|
378
|
+
kind: 'default',
|
|
379
|
+
style: { stroke: '#f00' },
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
flow.configure({ defaultEdgeOptions: { kind: 'gesture' } });
|
|
383
|
+
flow.internals.store.trigger.connectionStart({
|
|
384
|
+
sourceId: 'a',
|
|
385
|
+
sourcePort: null,
|
|
386
|
+
position: { x: 0, y: 0 },
|
|
387
|
+
});
|
|
388
|
+
flow.internals.store.trigger.connectionMove({
|
|
389
|
+
position: { x: 10, y: 0 },
|
|
390
|
+
targetId: 'b',
|
|
391
|
+
targetPort: null,
|
|
392
|
+
isValid: true,
|
|
393
|
+
});
|
|
394
|
+
flow.internals.store.trigger.connectionEnd({
|
|
395
|
+
target: { kind: 'node', nodeId: 'b', port: null },
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
expect(flow.getContext().graph.edges.find((edge) => edge.kind === 'gesture')).toMatchObject({
|
|
399
|
+
sourceId: 'a',
|
|
400
|
+
targetId: 'b',
|
|
401
|
+
kind: 'gesture',
|
|
402
|
+
});
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
it('does not auto-create an edge on connect by default', () => {
|
|
406
|
+
const flow = createFlow({
|
|
407
|
+
graph: createGraph({
|
|
408
|
+
nodes: [createNode({ id: 'a' }), createNode({ id: 'b' })],
|
|
409
|
+
edges: [],
|
|
410
|
+
}),
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
flow.internals.store.trigger.connectionStart({
|
|
414
|
+
sourceId: 'a',
|
|
415
|
+
sourcePort: null,
|
|
416
|
+
position: { x: 0, y: 0 },
|
|
417
|
+
});
|
|
418
|
+
flow.internals.store.trigger.connectionMove({
|
|
419
|
+
position: { x: 10, y: 0 },
|
|
420
|
+
targetId: 'b',
|
|
421
|
+
targetPort: null,
|
|
422
|
+
isValid: true,
|
|
423
|
+
});
|
|
424
|
+
flow.internals.store.trigger.connectionEnd({
|
|
425
|
+
target: { kind: 'node', nodeId: 'b', port: null },
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
expect(flow.getEdge('a->b')).toBeUndefined();
|
|
429
|
+
expect(flow.getContext().graph.edges).toHaveLength(0);
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
it('auto-creates an edge on connect with defaultEdgeOptions when createEdgeOnConnect is true', () => {
|
|
433
|
+
const flow = createFlow({
|
|
434
|
+
graph: createGraph({
|
|
435
|
+
nodes: [createNode({ id: 'a' }), createNode({ id: 'b' })],
|
|
436
|
+
edges: [],
|
|
437
|
+
}),
|
|
438
|
+
createEdgeOnConnect: true,
|
|
439
|
+
defaultEdgeOptions: { kind: 'opt-in' },
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
flow.internals.store.trigger.connectionStart({
|
|
443
|
+
sourceId: 'a',
|
|
444
|
+
sourcePort: null,
|
|
445
|
+
position: { x: 0, y: 0 },
|
|
446
|
+
});
|
|
447
|
+
flow.internals.store.trigger.connectionMove({
|
|
448
|
+
position: { x: 10, y: 0 },
|
|
449
|
+
targetId: 'b',
|
|
450
|
+
targetPort: null,
|
|
451
|
+
isValid: true,
|
|
452
|
+
});
|
|
453
|
+
flow.internals.store.trigger.connectionEnd({
|
|
454
|
+
target: { kind: 'node', nodeId: 'b', port: null },
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
expect(flow.getContext().graph.edges.find((edge) => edge.kind === 'opt-in')).toMatchObject({
|
|
458
|
+
sourceId: 'a',
|
|
459
|
+
targetId: 'b',
|
|
460
|
+
kind: 'opt-in',
|
|
461
|
+
});
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
it('merges defaultEdgeOptions into explicit flow.edges.create calls', () => {
|
|
465
|
+
const flow = createFlow({
|
|
466
|
+
graph: createGraph({
|
|
467
|
+
nodes: [createNode({ id: 'a' }), createNode({ id: 'b' })],
|
|
468
|
+
edges: [],
|
|
469
|
+
}),
|
|
470
|
+
defaultEdgeOptions: { kind: 'default', style: { stroke: '#0f0' } },
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
flow.edges.create({ id: 'explicit', sourceId: 'a', targetId: 'b' });
|
|
474
|
+
expect(flow.getEdge('explicit')).toMatchObject({
|
|
475
|
+
kind: 'default',
|
|
476
|
+
style: { stroke: '#0f0' },
|
|
477
|
+
});
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
it('deletes synchronously and returns whether an entity existed', () => {
|
|
481
|
+
const flow = createFlow({
|
|
482
|
+
graph: createGraph({
|
|
483
|
+
nodes: [createNode({ id: 'a' }), createNode({ id: 'b' })],
|
|
484
|
+
edges: [createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' })],
|
|
485
|
+
}),
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
expect(flow.nodes.delete('a')).toBe(true);
|
|
489
|
+
expect(flow.nodes.delete('a')).toBe(false);
|
|
490
|
+
expect(flow.getNode('a')).toBeUndefined();
|
|
491
|
+
expect(flow.getEdge('ab')).toBeUndefined();
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
it('deletes synchronously through the input runtime', () => {
|
|
495
|
+
const flow = createFlow({
|
|
496
|
+
graph: createGraph({
|
|
497
|
+
nodes: [createNode({ id: 'a' }), createNode({ id: 'b' })],
|
|
498
|
+
edges: [createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' })],
|
|
499
|
+
}),
|
|
500
|
+
});
|
|
501
|
+
const runtime = createFlowInputRuntime({
|
|
502
|
+
store: flow.internals.store,
|
|
503
|
+
engine: flow.internals.engine,
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
flow.selection.set(['a'], 'single');
|
|
507
|
+
runtime.send(keyDown('Delete'));
|
|
508
|
+
expect(flow.getNode('a')).toBeUndefined();
|
|
509
|
+
expect(flow.getEdge('ab')).toBeUndefined();
|
|
510
|
+
});
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
describe('createFlow serialization', () => {
|
|
514
|
+
it('round-trips graph and viewport while stripping transient interaction state', () => {
|
|
515
|
+
const flow = createFlow({
|
|
516
|
+
graph: createGraph({
|
|
517
|
+
nodes: [
|
|
518
|
+
createNode({
|
|
519
|
+
id: 'group',
|
|
520
|
+
x: 0,
|
|
521
|
+
y: 0,
|
|
522
|
+
width: 80,
|
|
523
|
+
height: 40,
|
|
524
|
+
group: { type: 'derived', content: 'top' },
|
|
525
|
+
}),
|
|
526
|
+
createNode({
|
|
527
|
+
id: 'a',
|
|
528
|
+
parentId: 'group',
|
|
529
|
+
x: 10,
|
|
530
|
+
y: 20,
|
|
531
|
+
width: 100,
|
|
532
|
+
height: 50,
|
|
533
|
+
autoSize: true,
|
|
534
|
+
measured: { width: 110, height: 60 },
|
|
535
|
+
style: { customWidth: 120, customHeight: 55 },
|
|
536
|
+
ports: [
|
|
537
|
+
createPort({ name: 'out', side: 'right', direction: 'out', x: 100, y: 20 }),
|
|
538
|
+
],
|
|
539
|
+
}),
|
|
540
|
+
createNode({
|
|
541
|
+
id: 'b',
|
|
542
|
+
x: 300,
|
|
543
|
+
y: 20,
|
|
544
|
+
width: 90,
|
|
545
|
+
height: 50,
|
|
546
|
+
ports: [
|
|
547
|
+
createPort({ name: 'in', side: 'left', direction: 'in', x: 0, y: 20 }),
|
|
548
|
+
],
|
|
549
|
+
}),
|
|
550
|
+
],
|
|
551
|
+
edges: [
|
|
552
|
+
createEdge({
|
|
553
|
+
id: 'ab',
|
|
554
|
+
sourceId: 'a',
|
|
555
|
+
targetId: 'b',
|
|
556
|
+
sourcePort: 'out',
|
|
557
|
+
targetPort: 'in',
|
|
558
|
+
label: { x: 150, y: 30, dx: 5, dy: 6, width: 60, height: 20 },
|
|
559
|
+
}),
|
|
560
|
+
],
|
|
561
|
+
}),
|
|
562
|
+
});
|
|
563
|
+
flow.viewport.set({ x: 12, y: 34, zoom: 1.5 });
|
|
564
|
+
flow.internals.store.trigger.select({ ids: ['a'] });
|
|
565
|
+
flow.internals.store.trigger.dragStart({ id: 'a' });
|
|
566
|
+
flow.internals.store.trigger.dragMove({ dx: 25, dy: 10 });
|
|
567
|
+
flow.nodes.collapse('group');
|
|
568
|
+
|
|
569
|
+
const data = flow.toJSON();
|
|
570
|
+
const loaded = fromJSON(data);
|
|
571
|
+
|
|
572
|
+
expect(data.version).toBe(1);
|
|
573
|
+
expect(data.viewport).toEqual({ x: 12, y: 34, zoom: 1.5 });
|
|
574
|
+
expect(data.collapsedNodeIds).toEqual(['group']);
|
|
575
|
+
expect(data.graph.nodes.find((node) => node.id === 'a')).toMatchObject({
|
|
576
|
+
x: 35,
|
|
577
|
+
y: 30,
|
|
578
|
+
style: { customWidth: 120, customHeight: 55 },
|
|
579
|
+
});
|
|
580
|
+
expect(data.graph.nodes.find((node) => node.id === 'a')?.dx).toBeUndefined();
|
|
581
|
+
expect(data.graph.nodes.find((node) => node.id === 'a')?.measured).toBeUndefined();
|
|
582
|
+
expect(data.graph.edges[0].label).toMatchObject({ x: 155, y: 36 });
|
|
583
|
+
|
|
584
|
+
expect(loaded.getContext().viewport).toEqual({ x: 12, y: 34, zoom: 1.5 });
|
|
585
|
+
expect(loaded.getNode('a')?.x).toBe(35);
|
|
586
|
+
expect(loaded.internals.engine.getPortPosition('a', 'out')).toEqual({ x: 141, y: 56 });
|
|
587
|
+
expect(loaded.getEdge('ab')?.label).toMatchObject({ x: 155, y: 36 });
|
|
588
|
+
expect(loaded.internals.engine.getGroupGeometry('group')?.bounds.width).toBeGreaterThan(0);
|
|
589
|
+
expect(loaded.internals.engine.getVisibleEntities().nodes.map((node) => node.id)).toEqual(['group', 'b']);
|
|
590
|
+
loaded.nodes.expand('group');
|
|
591
|
+
expect(loaded.internals.engine.getVisibleEntities().nodes.map((node) => node.id)).toEqual([
|
|
592
|
+
'group',
|
|
593
|
+
'b',
|
|
594
|
+
'a',
|
|
595
|
+
]);
|
|
596
|
+
});
|
|
597
|
+
|
|
598
|
+
it('restores a serialized viewport synchronously despite animation defaults', () => {
|
|
599
|
+
const source = createFlow();
|
|
600
|
+
source.viewport.set({ x: 12, y: 34, zoom: 1.5 });
|
|
601
|
+
|
|
602
|
+
const loaded = fromJSON(source.toJSON(), {
|
|
603
|
+
store: { viewportAnimation: { lerp: 0.15 } },
|
|
604
|
+
});
|
|
605
|
+
|
|
606
|
+
expect(loaded.getContext().viewport).toEqual({ x: 12, y: 34, zoom: 1.5 });
|
|
607
|
+
});
|
|
608
|
+
|
|
609
|
+
it('load atomically replaces graph, viewport, and session state', () => {
|
|
610
|
+
const flow = createFlow({
|
|
611
|
+
graph: createGraph({ nodes: [createNode({ id: 'a' })], edges: [] }),
|
|
612
|
+
});
|
|
613
|
+
flow.nodes.create({ id: 'b' });
|
|
614
|
+
flow.internals.store.trigger.select({ ids: ['b'] });
|
|
615
|
+
expect(flow.history.canUndo()).toBe(true);
|
|
616
|
+
|
|
617
|
+
flow.load({
|
|
618
|
+
version: 1,
|
|
619
|
+
graph: createGraph({
|
|
620
|
+
nodes: [createNode({ id: 'loaded', x: 20, y: 30 })],
|
|
621
|
+
edges: [],
|
|
622
|
+
}),
|
|
623
|
+
viewport: { x: 1, y: 2, zoom: 3 },
|
|
624
|
+
});
|
|
625
|
+
|
|
626
|
+
expect(flow.getContext().graph.nodes.map((node) => node.id)).toEqual(['loaded']);
|
|
627
|
+
expect(flow.getContext().viewport).toEqual({ x: 1, y: 2, zoom: 3 });
|
|
628
|
+
expect(flow.getContext().selection.size).toBe(0);
|
|
629
|
+
// History reset remains blocked on a supported @xstate/store API.
|
|
630
|
+
expect(flow.history.canUndo()).toBe(true);
|
|
631
|
+
});
|
|
632
|
+
});
|