@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,212 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { createFlow } from './createFlow';
|
|
3
|
+
import { createFlowInputRuntime, type FlowInputEvent } from './input';
|
|
4
|
+
import { createEdge, createGraph, createNode } from './factories';
|
|
5
|
+
import { straightRouter } from './routing';
|
|
6
|
+
import type { EdgeRouter } from './routing';
|
|
7
|
+
import { createEntityChangeNotifier } from './entity-changes';
|
|
8
|
+
|
|
9
|
+
function pointer(
|
|
10
|
+
kind: 'pointer.down' | 'pointer.move' | 'pointer.up',
|
|
11
|
+
point: { x: number; y: number },
|
|
12
|
+
): FlowInputEvent {
|
|
13
|
+
return {
|
|
14
|
+
kind,
|
|
15
|
+
pointerId: 1,
|
|
16
|
+
pointerType: 'mouse',
|
|
17
|
+
button: kind === 'pointer.move' ? -1 : 0,
|
|
18
|
+
point,
|
|
19
|
+
screenPoint: point,
|
|
20
|
+
target: kind === 'pointer.down'
|
|
21
|
+
? { kind: 'entity', id: 'a' }
|
|
22
|
+
: { kind: 'none' },
|
|
23
|
+
modifiers: { shift: false, meta: false, ctrl: false, alt: false },
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
describe('flow runtime configuration', () => {
|
|
28
|
+
it('publishes external route changes without reconfiguring nodes or replacing unchanged paths', () => {
|
|
29
|
+
let bend = 40;
|
|
30
|
+
const flow = createFlow({
|
|
31
|
+
graph: createGraph({
|
|
32
|
+
nodes: [createNode({ id: 'a' }), createNode({ id: 'b', x: 200 })],
|
|
33
|
+
edges: [
|
|
34
|
+
createEdge({ id: 'changed', sourceId: 'a', targetId: 'b' }),
|
|
35
|
+
createEdge({ id: 'unchanged', sourceId: 'a', targetId: 'b' }),
|
|
36
|
+
],
|
|
37
|
+
}),
|
|
38
|
+
engine: {
|
|
39
|
+
edgeRouter: (edge, context) => ({
|
|
40
|
+
...straightRouter(edge, context),
|
|
41
|
+
svgPath: `M 0 0 L 100 ${edge.id === 'changed' ? bend : 20} L 200 0`,
|
|
42
|
+
}),
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
const before = flow.getSnapshot();
|
|
46
|
+
const paths = flow.getAllEdgePaths();
|
|
47
|
+
const notifications: string[] = [];
|
|
48
|
+
const dependents = vi.fn(() => []);
|
|
49
|
+
const notifier = createEntityChangeNotifier(flow.internals.store, {
|
|
50
|
+
getEdgeDependents: dependents,
|
|
51
|
+
});
|
|
52
|
+
for (const id of ['a', 'b', 'changed', 'unchanged', 'folded:edge']) {
|
|
53
|
+
notifier.subscribe(id, () => notifications.push(id));
|
|
54
|
+
}
|
|
55
|
+
bend = 80;
|
|
56
|
+
flow.invalidateRoutes();
|
|
57
|
+
const next = flow.getAllEdgePaths();
|
|
58
|
+
expect(next.get('changed')!.segments[0].svgPath).toContain('100 80');
|
|
59
|
+
expect(next.get('unchanged')).toBe(paths.get('unchanged'));
|
|
60
|
+
expect(flow.getSnapshot().graph).toBe(before.graph);
|
|
61
|
+
expect(flow.getSnapshot().configRevision).toBe(before.configRevision);
|
|
62
|
+
expect(notifications.sort()).toEqual(['changed', 'folded:edge', 'unchanged']);
|
|
63
|
+
expect(flow.history.canUndo()).toBe(false);
|
|
64
|
+
expect(dependents).not.toHaveBeenCalled();
|
|
65
|
+
notifier.destroy();
|
|
66
|
+
flow.destroy();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('swaps the edge router and invalidates cached paths', () => {
|
|
70
|
+
const flow = createFlow({
|
|
71
|
+
graph: createGraph({
|
|
72
|
+
nodes: [createNode({ id: 'a' }), createNode({ id: 'b', x: 200 })],
|
|
73
|
+
edges: [createEdge({ id: 'edge-ab', sourceId: 'a', targetId: 'b' })],
|
|
74
|
+
}),
|
|
75
|
+
engine: { edgeRouter: straightRouter },
|
|
76
|
+
});
|
|
77
|
+
const customRouter: EdgeRouter = (_edge, context) => ({
|
|
78
|
+
points: [
|
|
79
|
+
{ x: 1, y: 1 },
|
|
80
|
+
{ x: 2, y: 2 },
|
|
81
|
+
{ x: 3, y: 3 },
|
|
82
|
+
],
|
|
83
|
+
sourceDirection: context.sourceDirection,
|
|
84
|
+
targetDirection: context.targetDirection,
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
expect(flow.getAllEdgePaths().get('edge-ab')?.segments[0].points).toHaveLength(2);
|
|
88
|
+
|
|
89
|
+
flow.configure({ engine: { edgeRouter: customRouter } });
|
|
90
|
+
|
|
91
|
+
expect(flow.getAllEdgePaths().get('edge-ab')?.segments[0].points).toEqual([
|
|
92
|
+
{ x: 1, y: 1 },
|
|
93
|
+
{ x: 2, y: 2 },
|
|
94
|
+
{ x: 3, y: 3 },
|
|
95
|
+
]);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('emits a new store snapshot on an engine-only reconfigure', () => {
|
|
99
|
+
const flow = createFlow({
|
|
100
|
+
graph: createGraph({
|
|
101
|
+
nodes: [createNode({ id: 'a' }), createNode({ id: 'b', x: 200 })],
|
|
102
|
+
edges: [createEdge({ id: 'edge-ab', sourceId: 'a', targetId: 'b' })],
|
|
103
|
+
}),
|
|
104
|
+
engine: { edgeRouter: straightRouter },
|
|
105
|
+
});
|
|
106
|
+
const customRouter: EdgeRouter = (_edge, context) => ({
|
|
107
|
+
points: [
|
|
108
|
+
{ x: 1, y: 1 },
|
|
109
|
+
{ x: 2, y: 2 },
|
|
110
|
+
{ x: 3, y: 3 },
|
|
111
|
+
],
|
|
112
|
+
sourceDirection: context.sourceDirection,
|
|
113
|
+
targetDirection: context.targetDirection,
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
const snapshots: unknown[] = [];
|
|
117
|
+
const subscription = flow.internals.store.subscribe((snapshot) => {
|
|
118
|
+
snapshots.push(snapshot.context);
|
|
119
|
+
});
|
|
120
|
+
const before = flow.internals.store.getSnapshot().context;
|
|
121
|
+
|
|
122
|
+
// No graph change — only engine options. A store subscriber must still see
|
|
123
|
+
// a NEW snapshot so React consumers re-derive edge paths from router B.
|
|
124
|
+
flow.configure({ engine: { edgeRouter: customRouter } });
|
|
125
|
+
|
|
126
|
+
const after = flow.internals.store.getSnapshot().context;
|
|
127
|
+
expect(after).not.toBe(before);
|
|
128
|
+
expect(after.configRevision).toBe(before.configRevision + 1);
|
|
129
|
+
expect(snapshots.length).toBeGreaterThan(0);
|
|
130
|
+
expect(flow.getAllEdgePaths().get('edge-ab')?.segments[0].points).toEqual([
|
|
131
|
+
{ x: 1, y: 1 },
|
|
132
|
+
{ x: 2, y: 2 },
|
|
133
|
+
{ x: 3, y: 3 },
|
|
134
|
+
]);
|
|
135
|
+
|
|
136
|
+
subscription.unsubscribe();
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('does not double-bump when a store setting also changes', () => {
|
|
140
|
+
const flow = createFlow({
|
|
141
|
+
graph: createGraph({
|
|
142
|
+
nodes: [createNode({ id: 'a' })],
|
|
143
|
+
edges: [],
|
|
144
|
+
}),
|
|
145
|
+
});
|
|
146
|
+
const before = flow.internals.store.getSnapshot().context.configRevision;
|
|
147
|
+
let emissions = 0;
|
|
148
|
+
const subscription = flow.internals.store.subscribe(() => {
|
|
149
|
+
emissions += 1;
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
flow.configure({
|
|
153
|
+
engine: { edgeRouter: straightRouter },
|
|
154
|
+
store: { dragThreshold: 10 },
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
// One configure call produces one revision and one snapshot.
|
|
158
|
+
expect(emissions).toBe(1);
|
|
159
|
+
expect(flow.internals.store.getSnapshot().context.configRevision).toBe(before + 1);
|
|
160
|
+
|
|
161
|
+
subscription.unsubscribe();
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('swaps canConnect immediately', () => {
|
|
165
|
+
const flow = createFlow({
|
|
166
|
+
graph: createGraph({
|
|
167
|
+
nodes: [createNode({ id: 'a' }), createNode({ id: 'b' })],
|
|
168
|
+
edges: [],
|
|
169
|
+
}),
|
|
170
|
+
engine: { canConnect: () => true },
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
expect(flow.internals.engine.validateConnection({ sourceId: 'a', targetId: 'b' })).toBe(true);
|
|
174
|
+
|
|
175
|
+
flow.configure({ engine: { canConnect: () => false } });
|
|
176
|
+
|
|
177
|
+
expect(flow.internals.engine.validateConnection({ sourceId: 'a', targetId: 'b' })).toBe(false);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it('applies store runtime settings to the next gesture', () => {
|
|
181
|
+
const flow = createFlow({
|
|
182
|
+
graph: createGraph({
|
|
183
|
+
nodes: [createNode({ id: 'a' })],
|
|
184
|
+
edges: [],
|
|
185
|
+
}),
|
|
186
|
+
store: { dragThreshold: 3 },
|
|
187
|
+
});
|
|
188
|
+
const runtime = createFlowInputRuntime({
|
|
189
|
+
store: flow.internals.store,
|
|
190
|
+
engine: flow.internals.engine,
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
flow.configure({ store: { dragThreshold: 10 } });
|
|
194
|
+
runtime.send(pointer('pointer.down', { x: 0, y: 0 }));
|
|
195
|
+
runtime.send(pointer('pointer.move', { x: 5, y: 0 }));
|
|
196
|
+
expect(flow.getContext().canvasState).toBe('idle');
|
|
197
|
+
|
|
198
|
+
runtime.send(pointer('pointer.move', { x: 11, y: 0 }));
|
|
199
|
+
expect(flow.getContext().canvasState).toBe('dragging');
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it('rejects unsupported live history-limit changes without lying about configuration', () => {
|
|
203
|
+
const flow = createFlow({ store: { historyLimit: 1 } });
|
|
204
|
+
|
|
205
|
+
expect(() => flow.configure({ historyLimit: 1 })).not.toThrow();
|
|
206
|
+
expect(() => flow.configure({ historyLimit: 3 })).toThrow(
|
|
207
|
+
/historyLimit cannot be changed/i,
|
|
208
|
+
);
|
|
209
|
+
expect(flow.getConfiguration().historyLimit).toBe(1);
|
|
210
|
+
expect(flow.getConfiguration().store.historyLimit).toBe(1);
|
|
211
|
+
});
|
|
212
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const connectionLifetime = Symbol('flow.connectionLifetime');
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { createFlow } from './createFlow';
|
|
3
|
+
import {
|
|
4
|
+
CONNECTION_PREVIEW_EDGE_ID,
|
|
5
|
+
CONNECTION_PREVIEW_NODE_ID,
|
|
6
|
+
createConnectionPreviewGraph,
|
|
7
|
+
} from './connectionPreview';
|
|
8
|
+
import { createGraph, createNode } from './factories';
|
|
9
|
+
import type { ConnectionState } from './types';
|
|
10
|
+
|
|
11
|
+
describe('createConnectionPreviewGraph', () => {
|
|
12
|
+
it('returns the original graph when no connection is active', () => {
|
|
13
|
+
const graph = createGraph({
|
|
14
|
+
nodes: [createNode({ id: 'a' })],
|
|
15
|
+
edges: [],
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
expect(createConnectionPreviewGraph(graph, null)).toBe(graph);
|
|
19
|
+
expect(
|
|
20
|
+
createConnectionPreviewGraph(graph, {
|
|
21
|
+
...connection(),
|
|
22
|
+
active: false,
|
|
23
|
+
}),
|
|
24
|
+
).toBe(graph);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('adds a tiny temp target node and temp edge for canvas targets', () => {
|
|
28
|
+
const graph = createGraph({
|
|
29
|
+
nodes: [createNode({ id: 'a' })],
|
|
30
|
+
edges: [],
|
|
31
|
+
});
|
|
32
|
+
const preview = createConnectionPreviewGraph(graph, connection());
|
|
33
|
+
|
|
34
|
+
expect(preview.nodes.map((node) => node.id)).toEqual([
|
|
35
|
+
'a',
|
|
36
|
+
CONNECTION_PREVIEW_NODE_ID,
|
|
37
|
+
]);
|
|
38
|
+
expect(preview.edges.map((edge) => edge.id)).toEqual([
|
|
39
|
+
CONNECTION_PREVIEW_EDGE_ID,
|
|
40
|
+
]);
|
|
41
|
+
expect(preview.nodes.at(-1)).toMatchObject({
|
|
42
|
+
x: 99,
|
|
43
|
+
y: 119,
|
|
44
|
+
width: 2,
|
|
45
|
+
height: 2,
|
|
46
|
+
data: { temp: { kind: 'connection-preview' } },
|
|
47
|
+
});
|
|
48
|
+
expect(preview.edges[0]).toMatchObject({
|
|
49
|
+
sourceId: 'a',
|
|
50
|
+
targetId: CONNECTION_PREVIEW_NODE_ID,
|
|
51
|
+
sourcePort: 'out',
|
|
52
|
+
data: { temp: { kind: 'connection-preview' } },
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('uses the hovered target node instead of a temp node', () => {
|
|
57
|
+
const graph = createGraph({
|
|
58
|
+
nodes: [createNode({ id: 'a' }), createNode({ id: 'b' })],
|
|
59
|
+
edges: [],
|
|
60
|
+
});
|
|
61
|
+
const preview = createConnectionPreviewGraph(graph, {
|
|
62
|
+
...connection(),
|
|
63
|
+
targetId: 'b',
|
|
64
|
+
targetPort: 'in',
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
expect(preview.nodes.map((node) => node.id)).toEqual(['a', 'b']);
|
|
68
|
+
expect(preview.edges[0]).toMatchObject({
|
|
69
|
+
sourceId: 'a',
|
|
70
|
+
targetId: 'b',
|
|
71
|
+
targetPort: 'in',
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('lets the engine route the temp edge without committing it to the store graph', () => {
|
|
76
|
+
const graph = createGraph({
|
|
77
|
+
nodes: [
|
|
78
|
+
createNode({ id: 'a', x: 0, y: 0, width: 100, height: 50 }),
|
|
79
|
+
],
|
|
80
|
+
edges: [],
|
|
81
|
+
});
|
|
82
|
+
const flow = createFlow({ graph });
|
|
83
|
+
const preview = createConnectionPreviewGraph(graph, connection());
|
|
84
|
+
const paths = flow.getAllEdgePaths({ graph: preview });
|
|
85
|
+
|
|
86
|
+
expect(paths.has(CONNECTION_PREVIEW_EDGE_ID)).toBe(true);
|
|
87
|
+
expect(flow.getContext().graph.edges).toEqual([]);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
function connection(): ConnectionState {
|
|
92
|
+
return {
|
|
93
|
+
sourceId: 'a',
|
|
94
|
+
sourcePort: 'out',
|
|
95
|
+
targetId: null,
|
|
96
|
+
targetPort: null,
|
|
97
|
+
startPosition: { x: 0, y: 0 },
|
|
98
|
+
pointerPosition: { x: 100, y: 120 },
|
|
99
|
+
active: true,
|
|
100
|
+
isValid: true,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { createEdge, createNode } from './factories';
|
|
2
|
+
import type { ConnectionState, FlowEdge, FlowGraph, FlowNode, Point } from './types';
|
|
3
|
+
|
|
4
|
+
export const CONNECTION_PREVIEW_NODE_ID = 'flow:connection-preview-node';
|
|
5
|
+
export const CONNECTION_PREVIEW_EDGE_ID = 'flow:connection-preview-edge';
|
|
6
|
+
|
|
7
|
+
export type ConnectionPreviewData = {
|
|
8
|
+
/** canConnect result for the hovered target (undefined over empty space). */
|
|
9
|
+
isValid?: boolean;
|
|
10
|
+
temp: { kind: 'connection-preview' };
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export type ConnectionPreviewGraphOptions = {
|
|
14
|
+
nodeId?: string;
|
|
15
|
+
edgeId?: string;
|
|
16
|
+
nodeSize?: number;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export function createConnectionPreviewGraph<
|
|
20
|
+
TNodeData = any,
|
|
21
|
+
TEdgeData = any,
|
|
22
|
+
>(
|
|
23
|
+
graph: FlowGraph<TNodeData, TEdgeData>,
|
|
24
|
+
connection: ConnectionState | null,
|
|
25
|
+
options: ConnectionPreviewGraphOptions = {},
|
|
26
|
+
): FlowGraph<TNodeData | ConnectionPreviewData, TEdgeData | ConnectionPreviewData> {
|
|
27
|
+
if (!connection?.active || connection.preview === false) {
|
|
28
|
+
return graph as FlowGraph<TNodeData | ConnectionPreviewData, TEdgeData | ConnectionPreviewData>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const nodeId = options.nodeId ?? CONNECTION_PREVIEW_NODE_ID;
|
|
32
|
+
const edgeId = options.edgeId ?? CONNECTION_PREVIEW_EDGE_ID;
|
|
33
|
+
const nodeSize = options.nodeSize ?? 2;
|
|
34
|
+
const targetId = connection.targetId ?? nodeId;
|
|
35
|
+
const nodes: FlowNode<TNodeData | ConnectionPreviewData>[] =
|
|
36
|
+
connection.targetId
|
|
37
|
+
? [...graph.nodes]
|
|
38
|
+
: [
|
|
39
|
+
...graph.nodes,
|
|
40
|
+
createPreviewNode(nodeId, connection.pointerPosition, nodeSize),
|
|
41
|
+
];
|
|
42
|
+
const edges: FlowEdge<TEdgeData | ConnectionPreviewData>[] = [
|
|
43
|
+
...graph.edges,
|
|
44
|
+
createEdge<ConnectionPreviewData>({
|
|
45
|
+
id: edgeId,
|
|
46
|
+
sourceId: connection.sourceId,
|
|
47
|
+
targetId,
|
|
48
|
+
sourcePort: connection.sourcePort ?? undefined,
|
|
49
|
+
targetPort: connection.targetPort ?? undefined,
|
|
50
|
+
selectable: false,
|
|
51
|
+
draggable: false,
|
|
52
|
+
deletable: false,
|
|
53
|
+
// Validity travels with the preview so renderers can color it
|
|
54
|
+
// (e.g. red when the hovered target would reject the connection).
|
|
55
|
+
data: { temp: { kind: 'connection-preview' }, isValid: connection.isValid },
|
|
56
|
+
}),
|
|
57
|
+
];
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
...graph,
|
|
61
|
+
nodes,
|
|
62
|
+
edges,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function createPreviewNode(
|
|
67
|
+
id: string,
|
|
68
|
+
point: Point,
|
|
69
|
+
size: number,
|
|
70
|
+
): FlowNode<ConnectionPreviewData> {
|
|
71
|
+
return createNode<ConnectionPreviewData>({
|
|
72
|
+
id,
|
|
73
|
+
x: point.x - size / 2,
|
|
74
|
+
y: point.y - size / 2,
|
|
75
|
+
width: size,
|
|
76
|
+
height: size,
|
|
77
|
+
selectable: false,
|
|
78
|
+
draggable: false,
|
|
79
|
+
deletable: false,
|
|
80
|
+
data: { temp: { kind: 'connection-preview' } },
|
|
81
|
+
});
|
|
82
|
+
}
|
package/src/content.ts
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { serializeFlowGraph, parseFlowSerialized } from './serialize';
|
|
2
|
+
import { invalidateAuthoritativeEdgePath } from './edge-geometry';
|
|
3
|
+
import type { FlowEdge, FlowGraph, FlowNode, Point } from './types';
|
|
4
|
+
import {
|
|
5
|
+
currentDataVersions,
|
|
6
|
+
type FlowDataSchemas,
|
|
7
|
+
type FlowDataVersions,
|
|
8
|
+
} from './validation';
|
|
9
|
+
|
|
10
|
+
export type FlowContent<TNodeData = any, TEdgeData = any, TLabelData = any> = {
|
|
11
|
+
type: 'stately-flow-content';
|
|
12
|
+
version: 1;
|
|
13
|
+
nodes: FlowNode<TNodeData>[];
|
|
14
|
+
edges: FlowEdge<TEdgeData, TLabelData>[];
|
|
15
|
+
dataVersions?: FlowDataVersions;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type FlowContentInsertResult<
|
|
19
|
+
TNodeData = any,
|
|
20
|
+
TEdgeData = any,
|
|
21
|
+
TLabelData = any,
|
|
22
|
+
> = {
|
|
23
|
+
graph: FlowGraph<TNodeData, TEdgeData, TLabelData>;
|
|
24
|
+
nodeIds: string[];
|
|
25
|
+
edgeIds: string[];
|
|
26
|
+
idMap: ReadonlyMap<string, string>;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export function extractFlowContent<
|
|
30
|
+
TNodeData = any,
|
|
31
|
+
TEdgeData = any,
|
|
32
|
+
TLabelData = any,
|
|
33
|
+
>(
|
|
34
|
+
graph: FlowGraph<TNodeData, TEdgeData, TLabelData>,
|
|
35
|
+
targets: Iterable<string>,
|
|
36
|
+
schemas?: FlowDataSchemas<TNodeData, TEdgeData, TLabelData>,
|
|
37
|
+
): FlowContent<TNodeData, TEdgeData, TLabelData> | null {
|
|
38
|
+
const requested = new Set(targets);
|
|
39
|
+
const nodeIds = new Set(
|
|
40
|
+
graph.nodes.filter((node) => requested.has(node.id)).map((node) => node.id),
|
|
41
|
+
);
|
|
42
|
+
let changed = true;
|
|
43
|
+
while (changed) {
|
|
44
|
+
changed = false;
|
|
45
|
+
for (const node of graph.nodes) {
|
|
46
|
+
if (node.parentId && nodeIds.has(node.parentId) && !nodeIds.has(node.id)) {
|
|
47
|
+
nodeIds.add(node.id);
|
|
48
|
+
changed = true;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const edgeIds = new Set(
|
|
53
|
+
graph.edges
|
|
54
|
+
.filter(
|
|
55
|
+
(edge) =>
|
|
56
|
+
requested.has(edge.id) ||
|
|
57
|
+
(nodeIds.has(edge.sourceId) && nodeIds.has(edge.targetId)),
|
|
58
|
+
)
|
|
59
|
+
.map((edge) => edge.id),
|
|
60
|
+
);
|
|
61
|
+
const selectedGraph = serializeFlowGraph({
|
|
62
|
+
...graph,
|
|
63
|
+
nodes: graph.nodes
|
|
64
|
+
.filter((node) => nodeIds.has(node.id))
|
|
65
|
+
.map((node) =>
|
|
66
|
+
node.parentId && !nodeIds.has(node.parentId)
|
|
67
|
+
? { ...node, parentId: null }
|
|
68
|
+
: node,
|
|
69
|
+
),
|
|
70
|
+
edges: graph.edges.filter(
|
|
71
|
+
(edge) =>
|
|
72
|
+
edgeIds.has(edge.id) &&
|
|
73
|
+
nodeIds.has(edge.sourceId) &&
|
|
74
|
+
nodeIds.has(edge.targetId),
|
|
75
|
+
),
|
|
76
|
+
});
|
|
77
|
+
if (selectedGraph.nodes.length === 0 && selectedGraph.edges.length === 0) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
type: 'stately-flow-content',
|
|
82
|
+
version: 1,
|
|
83
|
+
nodes: selectedGraph.nodes,
|
|
84
|
+
edges: selectedGraph.edges,
|
|
85
|
+
...(currentDataVersions(schemas)
|
|
86
|
+
? { dataVersions: currentDataVersions(schemas) }
|
|
87
|
+
: {}),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function insertFlowContent<
|
|
92
|
+
TNodeData = any,
|
|
93
|
+
TEdgeData = any,
|
|
94
|
+
TLabelData = any,
|
|
95
|
+
>(
|
|
96
|
+
graph: FlowGraph<TNodeData, TEdgeData, TLabelData>,
|
|
97
|
+
content: unknown,
|
|
98
|
+
options: {
|
|
99
|
+
point?: Point;
|
|
100
|
+
preservePosition?: boolean;
|
|
101
|
+
schemas?: FlowDataSchemas<TNodeData, TEdgeData, TLabelData>;
|
|
102
|
+
createId: (kind: 'node' | 'edge', sourceId: string, attempt: number) => string;
|
|
103
|
+
},
|
|
104
|
+
): FlowContentInsertResult<TNodeData, TEdgeData, TLabelData> {
|
|
105
|
+
if (
|
|
106
|
+
!content ||
|
|
107
|
+
typeof content !== 'object' ||
|
|
108
|
+
(content as { type?: unknown }).type !== 'stately-flow-content' ||
|
|
109
|
+
(content as { version?: unknown }).version !== 1
|
|
110
|
+
) {
|
|
111
|
+
throw new TypeError('Expected a supported stately-flow-content document.');
|
|
112
|
+
}
|
|
113
|
+
const raw = content as FlowContent<TNodeData, TEdgeData, TLabelData>;
|
|
114
|
+
const parsed = parseFlowSerialized<TNodeData, TEdgeData, TLabelData>(
|
|
115
|
+
{
|
|
116
|
+
version: 1,
|
|
117
|
+
graph: { nodes: raw.nodes, edges: raw.edges, direction: graph.direction },
|
|
118
|
+
viewport: { x: 0, y: 0, zoom: 1 },
|
|
119
|
+
dataVersions: raw.dataVersions,
|
|
120
|
+
},
|
|
121
|
+
options.schemas,
|
|
122
|
+
);
|
|
123
|
+
const existing = new Set([
|
|
124
|
+
...graph.nodes.map((node) => node.id),
|
|
125
|
+
...graph.edges.map((edge) => edge.id),
|
|
126
|
+
]);
|
|
127
|
+
const idMap = new Map<string, string>();
|
|
128
|
+
const allocate = (kind: 'node' | 'edge', sourceId: string): string => {
|
|
129
|
+
for (let attempt = 0; attempt < 100; attempt++) {
|
|
130
|
+
const id = options.createId(kind, sourceId, attempt);
|
|
131
|
+
if (id && !existing.has(id)) {
|
|
132
|
+
existing.add(id);
|
|
133
|
+
idMap.set(sourceId, id);
|
|
134
|
+
return id;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
throw new Error(`Unable to allocate a unique id for "${sourceId}".`);
|
|
138
|
+
};
|
|
139
|
+
for (const node of parsed.graph.nodes) allocate('node', node.id);
|
|
140
|
+
for (const edge of parsed.graph.edges) allocate('edge', edge.id);
|
|
141
|
+
|
|
142
|
+
const minX = parsed.graph.nodes.length
|
|
143
|
+
? Math.min(...parsed.graph.nodes.map((node) => node.x))
|
|
144
|
+
: 0;
|
|
145
|
+
const minY = parsed.graph.nodes.length
|
|
146
|
+
? Math.min(...parsed.graph.nodes.map((node) => node.y))
|
|
147
|
+
: 0;
|
|
148
|
+
const offset =
|
|
149
|
+
options.point && !options.preservePosition
|
|
150
|
+
? { x: options.point.x - minX, y: options.point.y - minY }
|
|
151
|
+
: { x: 0, y: 0 };
|
|
152
|
+
const remapData = <TData>(
|
|
153
|
+
data: TData,
|
|
154
|
+
contract: { remap?: (value: TData, context: { idMap: ReadonlyMap<string, string> }) => TData } | undefined,
|
|
155
|
+
): TData => contract?.remap ? contract.remap(data, { idMap }) : data;
|
|
156
|
+
const nodes = parsed.graph.nodes.map((node) => ({
|
|
157
|
+
...node,
|
|
158
|
+
id: idMap.get(node.id)!,
|
|
159
|
+
parentId: node.parentId ? idMap.get(node.parentId) ?? null : node.parentId,
|
|
160
|
+
x: node.x + offset.x,
|
|
161
|
+
y: node.y + offset.y,
|
|
162
|
+
data: remapData(node.data, options.schemas?.node),
|
|
163
|
+
}));
|
|
164
|
+
const edges = parsed.graph.edges.map((edge) => {
|
|
165
|
+
const inserted = {
|
|
166
|
+
...edge,
|
|
167
|
+
id: idMap.get(edge.id)!,
|
|
168
|
+
sourceId: idMap.get(edge.sourceId)!,
|
|
169
|
+
targetId: idMap.get(edge.targetId)!,
|
|
170
|
+
x: edge.x + offset.x,
|
|
171
|
+
y: edge.y + offset.y,
|
|
172
|
+
data: remapData(edge.data, options.schemas?.edge),
|
|
173
|
+
...(edge.waypoints
|
|
174
|
+
? {
|
|
175
|
+
waypoints: edge.waypoints.map((point) => ({
|
|
176
|
+
x: point.x + offset.x,
|
|
177
|
+
y: point.y + offset.y,
|
|
178
|
+
})),
|
|
179
|
+
}
|
|
180
|
+
: {}),
|
|
181
|
+
...(edge.label
|
|
182
|
+
? {
|
|
183
|
+
label: {
|
|
184
|
+
...edge.label,
|
|
185
|
+
x: edge.label.x + offset.x,
|
|
186
|
+
y: edge.label.y + offset.y,
|
|
187
|
+
data: remapData(edge.label.data, options.schemas?.label),
|
|
188
|
+
},
|
|
189
|
+
}
|
|
190
|
+
: {}),
|
|
191
|
+
};
|
|
192
|
+
if (offset.x !== 0 || offset.y !== 0) {
|
|
193
|
+
invalidateAuthoritativeEdgePath(inserted);
|
|
194
|
+
}
|
|
195
|
+
return inserted;
|
|
196
|
+
});
|
|
197
|
+
return {
|
|
198
|
+
graph: { ...graph, nodes: [...graph.nodes, ...nodes], edges: [...graph.edges, ...edges] },
|
|
199
|
+
nodeIds: nodes.map((node) => node.id),
|
|
200
|
+
edgeIds: edges.map((edge) => edge.id),
|
|
201
|
+
idMap,
|
|
202
|
+
};
|
|
203
|
+
}
|