@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,132 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { createFlowChangeSet } from './changeSet';
|
|
3
|
+
import { createFlow } from './createFlow';
|
|
4
|
+
import { createEdge, createGraph, createNode } from './factories';
|
|
5
|
+
import type { FlowGraph } from './types';
|
|
6
|
+
|
|
7
|
+
describe('createFlowChangeSet', () => {
|
|
8
|
+
it('treats undefined-valued keys as absent', () => {
|
|
9
|
+
const before = createGraph({
|
|
10
|
+
nodes: [createNode({ id: 'a', data: { label: 'A' } })],
|
|
11
|
+
edges: [createEdge({ id: 'e', sourceId: 'a', targetId: 'a', data: {} })],
|
|
12
|
+
});
|
|
13
|
+
const after = createGraph({
|
|
14
|
+
nodes: [createNode({ id: 'a', data: { label: 'A', color: undefined } })],
|
|
15
|
+
edges: [
|
|
16
|
+
createEdge({
|
|
17
|
+
id: 'e',
|
|
18
|
+
sourceId: 'a',
|
|
19
|
+
targetId: 'a',
|
|
20
|
+
data: { color: undefined },
|
|
21
|
+
}),
|
|
22
|
+
],
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
expect(createFlowChangeSet(before, after, { revision: 1 })).toBeNull();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('still reports a key that changes to or from a defined value', () => {
|
|
29
|
+
type Data = { color?: string };
|
|
30
|
+
const before = createGraph({
|
|
31
|
+
nodes: [createNode<Data>({ id: 'a', data: { color: undefined } })],
|
|
32
|
+
edges: [],
|
|
33
|
+
});
|
|
34
|
+
const after = createGraph({
|
|
35
|
+
nodes: [createNode<Data>({ id: 'a', data: { color: 'red' } })],
|
|
36
|
+
edges: [],
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
expect(
|
|
40
|
+
createFlowChangeSet(before, after, { revision: 1 })?.nodes.updated
|
|
41
|
+
).toHaveLength(1);
|
|
42
|
+
expect(
|
|
43
|
+
createFlowChangeSet(after, before, { revision: 1 })?.nodes.updated
|
|
44
|
+
).toHaveLength(1);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe('committed entity identity', () => {
|
|
49
|
+
function makeFlow() {
|
|
50
|
+
return createFlow({
|
|
51
|
+
graph: createGraph({
|
|
52
|
+
nodes: [
|
|
53
|
+
createNode({ id: 'a', data: { color: 'gray' } }),
|
|
54
|
+
createNode({ id: 'b', x: 200 }),
|
|
55
|
+
createNode({ id: 'c', x: 400 }),
|
|
56
|
+
],
|
|
57
|
+
edges: [
|
|
58
|
+
createEdge({
|
|
59
|
+
id: 'ab',
|
|
60
|
+
sourceId: 'a',
|
|
61
|
+
targetId: 'b',
|
|
62
|
+
label: { x: 0, y: 0, width: 40, height: 20, data: { text: 'go' } },
|
|
63
|
+
}),
|
|
64
|
+
createEdge({ id: 'bc', sourceId: 'b', targetId: 'c' }),
|
|
65
|
+
],
|
|
66
|
+
}),
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function expectOnlyChanged(
|
|
71
|
+
before: FlowGraph,
|
|
72
|
+
after: FlowGraph,
|
|
73
|
+
changedNodeIds: string[],
|
|
74
|
+
) {
|
|
75
|
+
for (const node of after.nodes) {
|
|
76
|
+
const previous = before.nodes.find((entry) => entry.id === node.id);
|
|
77
|
+
if (changedNodeIds.includes(node.id)) expect(node).not.toBe(previous);
|
|
78
|
+
else expect(node).toBe(previous);
|
|
79
|
+
}
|
|
80
|
+
after.edges.forEach((edge, index) => expect(edge).toBe(before.edges[index]));
|
|
81
|
+
expect(after.edges).toBe(before.edges);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
it('keeps unchanged nodes and edges across a one-node update', () => {
|
|
85
|
+
const flow = makeFlow();
|
|
86
|
+
const before = flow.getSnapshot().graph;
|
|
87
|
+
|
|
88
|
+
flow.nodes.update('a', { data: { color: 'red' } });
|
|
89
|
+
|
|
90
|
+
const after = flow.getSnapshot().graph;
|
|
91
|
+
expect(after.nodes.find((node) => node.id === 'a')?.data).toEqual({ color: 'red' });
|
|
92
|
+
expectOnlyChanged(before, after, ['a']);
|
|
93
|
+
flow.destroy();
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('keeps unchanged entities across replaceGraph and applyChangeSet', () => {
|
|
97
|
+
const flow = makeFlow();
|
|
98
|
+
const before = flow.getSnapshot().graph;
|
|
99
|
+
const replacement = {
|
|
100
|
+
...before,
|
|
101
|
+
nodes: before.nodes.map((node) => (node.id === 'b' ? { ...node, x: 250 } : { ...node })),
|
|
102
|
+
edges: before.edges.map((edge) => ({ ...edge })),
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const changeSet = flow.replaceGraph(replacement, { history: 'record' });
|
|
106
|
+
const replaced = flow.getSnapshot().graph;
|
|
107
|
+
expectOnlyChanged(before, replaced, ['b']);
|
|
108
|
+
|
|
109
|
+
flow.history.undo();
|
|
110
|
+
const target = makeFlow();
|
|
111
|
+
const targetBefore = target.getSnapshot().graph;
|
|
112
|
+
expect(target.applyChangeSet(changeSet!).ok).toBe(true);
|
|
113
|
+
expectOnlyChanged(targetBefore, target.getSnapshot().graph, ['b']);
|
|
114
|
+
flow.destroy();
|
|
115
|
+
target.destroy();
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('keeps the nodes array and an unchanged label when only an edge changes', () => {
|
|
119
|
+
const flow = makeFlow();
|
|
120
|
+
const before = flow.getSnapshot().graph;
|
|
121
|
+
|
|
122
|
+
flow.edges.update('ab', { data: { weight: 2 } });
|
|
123
|
+
|
|
124
|
+
const after = flow.getSnapshot().graph;
|
|
125
|
+
expect(after.nodes).toBe(before.nodes);
|
|
126
|
+
const edge = after.edges.find((entry) => entry.id === 'ab')!;
|
|
127
|
+
expect(edge).not.toBe(before.edges[0]);
|
|
128
|
+
expect(edge.label).toBe(before.edges[0].label);
|
|
129
|
+
expect(after.edges[1]).toBe(before.edges[1]);
|
|
130
|
+
flow.destroy();
|
|
131
|
+
});
|
|
132
|
+
});
|
package/src/changeSet.ts
ADDED
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
import { getPatches } from '@statelyai/graph';
|
|
2
|
+
import type { FlowEdge, FlowGraph, FlowNode } from './types';
|
|
3
|
+
import {
|
|
4
|
+
validateAndCloneGraph,
|
|
5
|
+
type FlowDataSchemas,
|
|
6
|
+
type FlowResult,
|
|
7
|
+
FlowGraphValidationError,
|
|
8
|
+
} from './validation';
|
|
9
|
+
|
|
10
|
+
export type FlowPatch<TNodeData = any, TEdgeData = any, TLabelData = any> =
|
|
11
|
+
| { op: 'addNode'; node: FlowNode<TNodeData> }
|
|
12
|
+
| { op: 'updateNode'; id: string; before: FlowNode<TNodeData>; after: FlowNode<TNodeData> }
|
|
13
|
+
| { op: 'deleteNode'; node: FlowNode<TNodeData> }
|
|
14
|
+
| { op: 'addEdge'; edge: FlowEdge<TEdgeData, TLabelData> }
|
|
15
|
+
| {
|
|
16
|
+
op: 'updateEdge';
|
|
17
|
+
id: string;
|
|
18
|
+
before: FlowEdge<TEdgeData, TLabelData>;
|
|
19
|
+
after: FlowEdge<TEdgeData, TLabelData>;
|
|
20
|
+
}
|
|
21
|
+
| { op: 'deleteEdge'; edge: FlowEdge<TEdgeData, TLabelData> }
|
|
22
|
+
| {
|
|
23
|
+
op: 'updateGraph';
|
|
24
|
+
before: Omit<FlowGraph<TNodeData, TEdgeData, TLabelData>, 'nodes' | 'edges'>;
|
|
25
|
+
after: Omit<FlowGraph<TNodeData, TEdgeData, TLabelData>, 'nodes' | 'edges'>;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export type FlowEntityUpdate<TEntity> = {
|
|
29
|
+
id: string;
|
|
30
|
+
before: TEntity;
|
|
31
|
+
after: TEntity;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type FlowChangeSet<TNodeData = any, TEdgeData = any, TLabelData = any> = {
|
|
35
|
+
version: 1;
|
|
36
|
+
origin?: string;
|
|
37
|
+
transactionId?: string;
|
|
38
|
+
revision: number;
|
|
39
|
+
nodes: {
|
|
40
|
+
added: FlowNode<TNodeData>[];
|
|
41
|
+
removed: FlowNode<TNodeData>[];
|
|
42
|
+
updated: FlowEntityUpdate<FlowNode<TNodeData>>[];
|
|
43
|
+
};
|
|
44
|
+
edges: {
|
|
45
|
+
added: FlowEdge<TEdgeData, TLabelData>[];
|
|
46
|
+
removed: FlowEdge<TEdgeData, TLabelData>[];
|
|
47
|
+
updated: FlowEntityUpdate<FlowEdge<TEdgeData, TLabelData>>[];
|
|
48
|
+
};
|
|
49
|
+
forward: FlowPatch<TNodeData, TEdgeData, TLabelData>[];
|
|
50
|
+
inverse: FlowPatch<TNodeData, TEdgeData, TLabelData>[];
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
function equal(a: unknown, b: unknown): boolean {
|
|
54
|
+
if (a === b) return true;
|
|
55
|
+
if (!a || !b || typeof a !== 'object' || typeof b !== 'object') return false;
|
|
56
|
+
if (Array.isArray(a) || Array.isArray(b)) {
|
|
57
|
+
return Array.isArray(a) && Array.isArray(b) &&
|
|
58
|
+
a.length === b.length && a.every((value, index) => equal(value, b[index]));
|
|
59
|
+
}
|
|
60
|
+
const aRecord = a as Record<string, unknown>;
|
|
61
|
+
const bRecord = b as Record<string, unknown>;
|
|
62
|
+
// A key holding `undefined` counts as absent: stored graphs drop such keys,
|
|
63
|
+
// so a projection that spells them out must still compare equal.
|
|
64
|
+
const aKeys = Object.keys(aRecord).filter((key) => aRecord[key] !== undefined);
|
|
65
|
+
const bKeys = Object.keys(bRecord).filter((key) => bRecord[key] !== undefined);
|
|
66
|
+
return aKeys.length === bKeys.length &&
|
|
67
|
+
aKeys.every((key) => Object.prototype.hasOwnProperty.call(bRecord, key) &&
|
|
68
|
+
equal(aRecord[key], bRecord[key]));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function graphAttrs<TNodeData, TEdgeData, TLabelData>(
|
|
72
|
+
graph: FlowGraph<TNodeData, TEdgeData, TLabelData>,
|
|
73
|
+
): Omit<FlowGraph<TNodeData, TEdgeData, TLabelData>, 'nodes' | 'edges'> {
|
|
74
|
+
const { nodes: _nodes, edges: _edges, ...attrs } = graph;
|
|
75
|
+
return attrs;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function patchKey(patch: { op: string; id?: string; node?: { id: string }; edge?: { id: string } }): string {
|
|
79
|
+
return `${patch.op}:${patch.id ?? patch.node?.id ?? patch.edge?.id ?? ''}`;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const OP_ORDER: Record<FlowPatch['op'], number> = {
|
|
83
|
+
addNode: 0,
|
|
84
|
+
updateEdge: 1,
|
|
85
|
+
deleteEdge: 2,
|
|
86
|
+
deleteNode: 3,
|
|
87
|
+
addEdge: 4,
|
|
88
|
+
updateNode: 5,
|
|
89
|
+
updateGraph: 6,
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
function orderPatches<TNodeData, TEdgeData, TLabelData>(
|
|
93
|
+
before: FlowGraph<TNodeData, TEdgeData, TLabelData>,
|
|
94
|
+
after: FlowGraph<TNodeData, TEdgeData, TLabelData>,
|
|
95
|
+
patches: FlowPatch<TNodeData, TEdgeData, TLabelData>[],
|
|
96
|
+
): FlowPatch<TNodeData, TEdgeData, TLabelData>[] {
|
|
97
|
+
const upstreamBefore = { ...before, id: before.id ?? '' };
|
|
98
|
+
const upstreamAfter = { ...after, id: after.id ?? '' };
|
|
99
|
+
const upstreamOrder = new Map(
|
|
100
|
+
getPatches(
|
|
101
|
+
upstreamBefore as unknown as Parameters<typeof getPatches>[0],
|
|
102
|
+
upstreamAfter as unknown as Parameters<typeof getPatches>[1],
|
|
103
|
+
).map((patch, index) => [patchKey(patch), index]),
|
|
104
|
+
);
|
|
105
|
+
return patches.sort((a, b) => {
|
|
106
|
+
const operationOrder = OP_ORDER[a.op] - OP_ORDER[b.op];
|
|
107
|
+
if (operationOrder !== 0) return operationOrder;
|
|
108
|
+
return (
|
|
109
|
+
(upstreamOrder.get(patchKey(a)) ?? Number.MAX_SAFE_INTEGER) -
|
|
110
|
+
(upstreamOrder.get(patchKey(b)) ?? Number.MAX_SAFE_INTEGER)
|
|
111
|
+
);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function invertPatch<TNodeData, TEdgeData, TLabelData>(
|
|
116
|
+
patch: FlowPatch<TNodeData, TEdgeData, TLabelData>,
|
|
117
|
+
): FlowPatch<TNodeData, TEdgeData, TLabelData> {
|
|
118
|
+
switch (patch.op) {
|
|
119
|
+
case 'addNode':
|
|
120
|
+
return { op: 'deleteNode', node: patch.node };
|
|
121
|
+
case 'deleteNode':
|
|
122
|
+
return { op: 'addNode', node: patch.node };
|
|
123
|
+
case 'updateNode':
|
|
124
|
+
return { ...patch, before: patch.after, after: patch.before };
|
|
125
|
+
case 'addEdge':
|
|
126
|
+
return { op: 'deleteEdge', edge: patch.edge };
|
|
127
|
+
case 'deleteEdge':
|
|
128
|
+
return { op: 'addEdge', edge: patch.edge };
|
|
129
|
+
case 'updateEdge':
|
|
130
|
+
return { ...patch, before: patch.after, after: patch.before };
|
|
131
|
+
case 'updateGraph':
|
|
132
|
+
return { ...patch, before: patch.after, after: patch.before };
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function createFlowChangeSet<
|
|
137
|
+
TNodeData = any,
|
|
138
|
+
TEdgeData = any,
|
|
139
|
+
TLabelData = any,
|
|
140
|
+
>(
|
|
141
|
+
before: FlowGraph<TNodeData, TEdgeData, TLabelData>,
|
|
142
|
+
after: FlowGraph<TNodeData, TEdgeData, TLabelData>,
|
|
143
|
+
metadata: { origin?: string; transactionId?: string; revision: number },
|
|
144
|
+
): FlowChangeSet<TNodeData, TEdgeData, TLabelData> | null {
|
|
145
|
+
const beforeNodes = new Map(before.nodes.map((node) => [node.id, node]));
|
|
146
|
+
const afterNodes = new Map(after.nodes.map((node) => [node.id, node]));
|
|
147
|
+
const beforeEdges = new Map(before.edges.map((edge) => [edge.id, edge]));
|
|
148
|
+
const afterEdges = new Map(after.edges.map((edge) => [edge.id, edge]));
|
|
149
|
+
|
|
150
|
+
const nodes = {
|
|
151
|
+
added: after.nodes.filter((node) => !beforeNodes.has(node.id)),
|
|
152
|
+
removed: before.nodes.filter((node) => !afterNodes.has(node.id)),
|
|
153
|
+
updated: before.nodes.flatMap((node) => {
|
|
154
|
+
const next = afterNodes.get(node.id);
|
|
155
|
+
return next && !equal(node, next) ? [{ id: node.id, before: node, after: next }] : [];
|
|
156
|
+
}),
|
|
157
|
+
};
|
|
158
|
+
const edges = {
|
|
159
|
+
added: after.edges.filter((edge) => !beforeEdges.has(edge.id)),
|
|
160
|
+
removed: before.edges.filter((edge) => !afterEdges.has(edge.id)),
|
|
161
|
+
updated: before.edges.flatMap((edge) => {
|
|
162
|
+
const next = afterEdges.get(edge.id);
|
|
163
|
+
return next && !equal(edge, next) ? [{ id: edge.id, before: edge, after: next }] : [];
|
|
164
|
+
}),
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
const patches: FlowPatch<TNodeData, TEdgeData, TLabelData>[] = [
|
|
168
|
+
...nodes.added.map((node) => ({ op: 'addNode' as const, node })),
|
|
169
|
+
...nodes.updated.map(({ id, before: previous, after: next }) => ({
|
|
170
|
+
op: 'updateNode' as const,
|
|
171
|
+
id,
|
|
172
|
+
before: previous,
|
|
173
|
+
after: next,
|
|
174
|
+
})),
|
|
175
|
+
...nodes.removed.map((node) => ({ op: 'deleteNode' as const, node })),
|
|
176
|
+
...edges.added.map((edge) => ({ op: 'addEdge' as const, edge })),
|
|
177
|
+
...edges.updated.map(({ id, before: previous, after: next }) => ({
|
|
178
|
+
op: 'updateEdge' as const,
|
|
179
|
+
id,
|
|
180
|
+
before: previous,
|
|
181
|
+
after: next,
|
|
182
|
+
})),
|
|
183
|
+
...edges.removed.map((edge) => ({ op: 'deleteEdge' as const, edge })),
|
|
184
|
+
];
|
|
185
|
+
const beforeAttrs = graphAttrs(before);
|
|
186
|
+
const afterAttrs = graphAttrs(after);
|
|
187
|
+
if (!equal(beforeAttrs, afterAttrs)) {
|
|
188
|
+
patches.push({ op: 'updateGraph', before: beforeAttrs, after: afterAttrs });
|
|
189
|
+
}
|
|
190
|
+
if (patches.length === 0) return null;
|
|
191
|
+
|
|
192
|
+
const forward = orderPatches(before, after, patches);
|
|
193
|
+
const inverse = orderPatches(after, before, forward.map(invertPatch));
|
|
194
|
+
return {
|
|
195
|
+
version: 1,
|
|
196
|
+
...metadata,
|
|
197
|
+
nodes,
|
|
198
|
+
edges,
|
|
199
|
+
forward,
|
|
200
|
+
inverse,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Swap each entity the change set did not add or update for its object in
|
|
206
|
+
* `before`, so memoized views keyed on entity identity skip unchanged
|
|
207
|
+
* entities. An updated edge keeps its previous label object when the label is
|
|
208
|
+
* unchanged. Arrays whose entries all match `before` are kept too. Mutates and
|
|
209
|
+
* returns `after`, which must be a fresh clone.
|
|
210
|
+
*/
|
|
211
|
+
export function reuseUnchangedEntities<TNodeData, TEdgeData, TLabelData>(
|
|
212
|
+
before: FlowGraph<TNodeData, TEdgeData, TLabelData>,
|
|
213
|
+
after: FlowGraph<TNodeData, TEdgeData, TLabelData>,
|
|
214
|
+
changeSet: FlowChangeSet<TNodeData, TEdgeData, TLabelData> | null,
|
|
215
|
+
): FlowGraph<TNodeData, TEdgeData, TLabelData> {
|
|
216
|
+
const changedNodeIds = new Set([
|
|
217
|
+
...(changeSet?.nodes.added ?? []).map((node) => node.id),
|
|
218
|
+
...(changeSet?.nodes.updated ?? []).map((update) => update.id),
|
|
219
|
+
]);
|
|
220
|
+
const changedEdgeIds = new Set([
|
|
221
|
+
...(changeSet?.edges.added ?? []).map((edge) => edge.id),
|
|
222
|
+
...(changeSet?.edges.updated ?? []).map((update) => update.id),
|
|
223
|
+
]);
|
|
224
|
+
const beforeNodes = new Map(before.nodes.map((node) => [node.id, node]));
|
|
225
|
+
const beforeEdges = new Map(before.edges.map((edge) => [edge.id, edge]));
|
|
226
|
+
const nodes = after.nodes.map((node) =>
|
|
227
|
+
changedNodeIds.has(node.id) ? node : (beforeNodes.get(node.id) ?? node),
|
|
228
|
+
);
|
|
229
|
+
const edges = after.edges.map((edge) => {
|
|
230
|
+
const previous = beforeEdges.get(edge.id);
|
|
231
|
+
if (!previous) return edge;
|
|
232
|
+
if (!changedEdgeIds.has(edge.id)) return previous;
|
|
233
|
+
if (previous.label && equal(previous.label, edge.label)) edge.label = previous.label;
|
|
234
|
+
return edge;
|
|
235
|
+
});
|
|
236
|
+
after.nodes = sameEntries(nodes, before.nodes) ? before.nodes : nodes;
|
|
237
|
+
after.edges = sameEntries(edges, before.edges) ? before.edges : edges;
|
|
238
|
+
return after;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function sameEntries<T>(left: readonly T[], right: readonly T[]): boolean {
|
|
242
|
+
return left.length === right.length && left.every((value, index) => value === right[index]);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function strictConflict(path: string, message: string): FlowGraphValidationError {
|
|
246
|
+
return new FlowGraphValidationError([{ code: 'change_set_conflict', path, message }]);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export function applyFlowPatches<TNodeData = any, TEdgeData = any, TLabelData = any>(
|
|
250
|
+
graph: FlowGraph<TNodeData, TEdgeData, TLabelData>,
|
|
251
|
+
patches: readonly FlowPatch<TNodeData, TEdgeData, TLabelData>[],
|
|
252
|
+
options: {
|
|
253
|
+
strict?: boolean;
|
|
254
|
+
schemas?: FlowDataSchemas<TNodeData, TEdgeData, TLabelData>;
|
|
255
|
+
} = {},
|
|
256
|
+
): FlowResult<FlowGraph<TNodeData, TEdgeData, TLabelData>> {
|
|
257
|
+
try {
|
|
258
|
+
const next = validateAndCloneGraph(graph, options.schemas);
|
|
259
|
+
for (const patch of patches) {
|
|
260
|
+
switch (patch.op) {
|
|
261
|
+
case 'addNode':
|
|
262
|
+
if (options.strict && next.nodes.some((node) => node.id === patch.node.id)) {
|
|
263
|
+
throw strictConflict('$.nodes', `node "${patch.node.id}" already exists`);
|
|
264
|
+
}
|
|
265
|
+
next.nodes.push(patch.node);
|
|
266
|
+
break;
|
|
267
|
+
case 'updateNode': {
|
|
268
|
+
const index = next.nodes.findIndex((node) => node.id === patch.id);
|
|
269
|
+
if (index < 0) throw strictConflict('$.nodes', `node "${patch.id}" does not exist`);
|
|
270
|
+
if (options.strict && !equal(next.nodes[index], patch.before)) {
|
|
271
|
+
throw strictConflict('$.nodes', `node "${patch.id}" no longer matches the expected value`);
|
|
272
|
+
}
|
|
273
|
+
next.nodes[index] = patch.after;
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
case 'deleteNode': {
|
|
277
|
+
const index = next.nodes.findIndex((node) => node.id === patch.node.id);
|
|
278
|
+
if (index < 0) {
|
|
279
|
+
if (options.strict) throw strictConflict('$.nodes', `node "${patch.node.id}" does not exist`);
|
|
280
|
+
break;
|
|
281
|
+
}
|
|
282
|
+
if (options.strict && !equal(next.nodes[index], patch.node)) {
|
|
283
|
+
throw strictConflict('$.nodes', `node "${patch.node.id}" no longer matches the expected value`);
|
|
284
|
+
}
|
|
285
|
+
next.nodes.splice(index, 1);
|
|
286
|
+
break;
|
|
287
|
+
}
|
|
288
|
+
case 'addEdge':
|
|
289
|
+
if (options.strict && next.edges.some((edge) => edge.id === patch.edge.id)) {
|
|
290
|
+
throw strictConflict('$.edges', `edge "${patch.edge.id}" already exists`);
|
|
291
|
+
}
|
|
292
|
+
next.edges.push(patch.edge);
|
|
293
|
+
break;
|
|
294
|
+
case 'updateEdge': {
|
|
295
|
+
const index = next.edges.findIndex((edge) => edge.id === patch.id);
|
|
296
|
+
if (index < 0) throw strictConflict('$.edges', `edge "${patch.id}" does not exist`);
|
|
297
|
+
if (options.strict && !equal(next.edges[index], patch.before)) {
|
|
298
|
+
throw strictConflict('$.edges', `edge "${patch.id}" no longer matches the expected value`);
|
|
299
|
+
}
|
|
300
|
+
next.edges[index] = patch.after;
|
|
301
|
+
break;
|
|
302
|
+
}
|
|
303
|
+
case 'deleteEdge': {
|
|
304
|
+
const index = next.edges.findIndex((edge) => edge.id === patch.edge.id);
|
|
305
|
+
if (index < 0) {
|
|
306
|
+
if (options.strict) throw strictConflict('$.edges', `edge "${patch.edge.id}" does not exist`);
|
|
307
|
+
break;
|
|
308
|
+
}
|
|
309
|
+
if (options.strict && !equal(next.edges[index], patch.edge)) {
|
|
310
|
+
throw strictConflict('$.edges', `edge "${patch.edge.id}" no longer matches the expected value`);
|
|
311
|
+
}
|
|
312
|
+
next.edges.splice(index, 1);
|
|
313
|
+
break;
|
|
314
|
+
}
|
|
315
|
+
case 'updateGraph': {
|
|
316
|
+
const current = graphAttrs(next);
|
|
317
|
+
if (options.strict && !equal(current, patch.before)) {
|
|
318
|
+
throw strictConflict('$', 'graph attributes no longer match the expected value');
|
|
319
|
+
}
|
|
320
|
+
Object.assign(next, patch.after);
|
|
321
|
+
break;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
return { ok: true, value: validateAndCloneGraph(next, options.schemas) };
|
|
326
|
+
} catch (error) {
|
|
327
|
+
return {
|
|
328
|
+
ok: false,
|
|
329
|
+
error:
|
|
330
|
+
error instanceof FlowGraphValidationError
|
|
331
|
+
? error
|
|
332
|
+
: new FlowGraphValidationError([
|
|
333
|
+
{ code: 'change_set_error', path: '$', message: String(error) },
|
|
334
|
+
]),
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
}
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { FlowConfig } from './types';
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
// Behavior config presets
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
/** Default config: supported editing behaviors enabled */
|
|
8
|
+
export const defaultConfig: FlowConfig = {
|
|
9
|
+
selectable: () => true,
|
|
10
|
+
focusable: () => true,
|
|
11
|
+
draggable: () => true,
|
|
12
|
+
connectable: () => true,
|
|
13
|
+
reconnectable: () => true,
|
|
14
|
+
deletable: () => true,
|
|
15
|
+
resizable: false,
|
|
16
|
+
visible: () => true,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/** Readonly preset: can select and view, cannot modify */
|
|
20
|
+
export const readonlyConfig: Partial<FlowConfig> = {
|
|
21
|
+
draggable: false,
|
|
22
|
+
connectable: false,
|
|
23
|
+
reconnectable: false,
|
|
24
|
+
deletable: false,
|
|
25
|
+
resizable: false,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Resolve a behavior predicate.
|
|
30
|
+
* If `false`, returns false. If function, calls it. If undefined, returns default.
|
|
31
|
+
*/
|
|
32
|
+
export function resolveBehavior<TEntity, TState>(
|
|
33
|
+
predicate: ((entity: TEntity, state: TState) => boolean) | false | undefined,
|
|
34
|
+
entity: TEntity,
|
|
35
|
+
state: TState,
|
|
36
|
+
defaultValue = true,
|
|
37
|
+
): boolean {
|
|
38
|
+
if (predicate === false) return false;
|
|
39
|
+
if (predicate === undefined) return defaultValue;
|
|
40
|
+
return predicate(entity, state);
|
|
41
|
+
}
|