@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.
Files changed (115) hide show
  1. package/README.md +5 -1
  2. package/dist/esm/index.d.ts +13 -2
  3. package/dist/esm/index.d.ts.map +1 -0
  4. package/dist/esm/index.js +39 -14
  5. package/dist/esm/index.js.map +1 -0
  6. package/dist/esm/layout/dagre.d.ts +2 -1
  7. package/dist/esm/layout/dagre.d.ts.map +1 -0
  8. package/dist/esm/layout/dagre.js +2 -0
  9. package/dist/esm/layout/dagre.js.map +1 -0
  10. package/dist/esm/layout/elk.d.ts +2 -1
  11. package/dist/esm/layout/elk.d.ts.map +1 -0
  12. package/dist/esm/layout/elk.js +2 -0
  13. package/dist/esm/layout/elk.js.map +1 -0
  14. package/dist/esm/layout/stately.d.ts +2 -1
  15. package/dist/esm/layout/stately.d.ts.map +1 -0
  16. package/dist/esm/layout/stately.js +2 -0
  17. package/dist/esm/layout/stately.js.map +1 -0
  18. package/dist/esm/shared.d.ts +2 -1
  19. package/dist/esm/shared.d.ts.map +1 -0
  20. package/dist/esm/shared.js +2 -0
  21. package/dist/esm/shared.js.map +1 -0
  22. package/package.json +4 -3
  23. package/src/__fixtures__/nested.ts +106 -0
  24. package/src/appearances.test.ts +428 -0
  25. package/src/appearances.ts +144 -0
  26. package/src/authoritative-route.ts +14 -0
  27. package/src/changeSet.test.ts +132 -0
  28. package/src/changeSet.ts +337 -0
  29. package/src/config.ts +41 -0
  30. package/src/configure.test.ts +212 -0
  31. package/src/connection-lifetime.ts +1 -0
  32. package/src/connectionPreview.test.ts +102 -0
  33. package/src/connectionPreview.ts +82 -0
  34. package/src/content.ts +203 -0
  35. package/src/createFlow.fit-frame.test.ts +256 -0
  36. package/src/createFlow.modern.test.ts +743 -0
  37. package/src/createFlow.test.ts +632 -0
  38. package/src/createFlow.ts +3463 -0
  39. package/src/createFlow.view.test.ts +212 -0
  40. package/src/createFlow.viewport-controls.test.ts +221 -0
  41. package/src/edge-geometry.ts +318 -0
  42. package/src/engine.cache.test.ts +728 -0
  43. package/src/engine.hierarchy.test.ts +1023 -0
  44. package/src/engine.test.ts +1965 -0
  45. package/src/engine.ts +3385 -0
  46. package/src/entity-changes.test.ts +309 -0
  47. package/src/entity-changes.ts +395 -0
  48. package/src/examples/flow-based-math.test.ts +25 -0
  49. package/src/examples/flow-based-math.ts +210 -0
  50. package/src/external-edit.test.ts +337 -0
  51. package/src/external-edit.ts +358 -0
  52. package/src/factories.test.ts +104 -0
  53. package/src/factories.ts +70 -0
  54. package/src/geometry.ts +13 -0
  55. package/src/graph-index.test.ts +56 -0
  56. package/src/graph-index.ts +194 -0
  57. package/src/hierarchy.test.ts +175 -0
  58. package/src/hierarchy.ts +330 -0
  59. package/src/highlights.test.ts +130 -0
  60. package/src/index.ts +469 -0
  61. package/src/input/index.ts +615 -0
  62. package/src/input/input.test.ts +2577 -0
  63. package/src/input/keymap.ts +111 -0
  64. package/src/input/modes.ts +1715 -0
  65. package/src/input/reparent.test.ts +214 -0
  66. package/src/layout/dagre.ts +138 -0
  67. package/src/layout/elk.ts +1382 -0
  68. package/src/layout/index.ts +12 -0
  69. package/src/layout/layout.test.ts +1248 -0
  70. package/src/layout/shared.ts +260 -0
  71. package/src/layout/stately-globals.d.ts +9 -0
  72. package/src/layout/stately.test.ts +246 -0
  73. package/src/layout/stately.ts +528 -0
  74. package/src/layout-animation.test.ts +223 -0
  75. package/src/layout-contract.test.ts +414 -0
  76. package/src/marker.test.ts +23 -0
  77. package/src/marker.ts +23 -0
  78. package/src/measurement-anchor.test.ts +232 -0
  79. package/src/minimap.test.ts +96 -0
  80. package/src/minimap.ts +137 -0
  81. package/src/nested-fixture.test.ts +178 -0
  82. package/src/port-measurement.test.ts +27 -0
  83. package/src/port-measurements.test.ts +187 -0
  84. package/src/profile.ts +124 -0
  85. package/src/projections.test.ts +103 -0
  86. package/src/projections.ts +295 -0
  87. package/src/render-invalidation.test.ts +62 -0
  88. package/src/render-invalidation.ts +55 -0
  89. package/src/renderer-geometry.test.ts +356 -0
  90. package/src/renderer-transactions.test.ts +234 -0
  91. package/src/routing/avoid.ts +417 -0
  92. package/src/routing/index.test.ts +361 -0
  93. package/src/routing/index.ts +366 -0
  94. package/src/routing/readTracking.test.ts +103 -0
  95. package/src/routing/readTracking.ts +233 -0
  96. package/src/selection-commands.test.ts +355 -0
  97. package/src/serialize.test.ts +167 -0
  98. package/src/serialize.ts +254 -0
  99. package/src/shapes/index.test.ts +45 -0
  100. package/src/shapes/index.ts +142 -0
  101. package/src/snap.test.ts +123 -0
  102. package/src/snap.ts +395 -0
  103. package/src/spatial.test.ts +676 -0
  104. package/src/spatial.ts +457 -0
  105. package/src/store/index.ts +4121 -0
  106. package/src/store.test.ts +2903 -0
  107. package/src/toolbar.test.ts +31 -0
  108. package/src/toolbar.ts +62 -0
  109. package/src/types.test.ts +15 -0
  110. package/src/types.ts +487 -0
  111. package/src/validation.ts +558 -0
  112. package/src/view.ts +172 -0
  113. package/src/viewport/animate.ts +285 -0
  114. package/src/viewport/index.ts +296 -0
  115. package/src/viewport.test.ts +569 -0
@@ -0,0 +1,187 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { createFlow } from './createFlow';
3
+ import { createEdge, createGraph, createNode, createPort } from './factories';
4
+
5
+ function makeFlow(sourcePort = 'out') {
6
+ return createFlow({
7
+ graph: createGraph({
8
+ nodes: [
9
+ createNode({
10
+ id: 'a',
11
+ x: 0,
12
+ y: 0,
13
+ ports: [
14
+ {
15
+ ...createPort({
16
+ name: 'out',
17
+ x: 10,
18
+ y: 10,
19
+ width: 8,
20
+ height: 8,
21
+ direction: 'out',
22
+ side: 'right',
23
+ }),
24
+ id: 'explicit-out',
25
+ },
26
+ ],
27
+ }),
28
+ createNode({ id: 'b', x: 300, y: 0 }),
29
+ ],
30
+ edges: [
31
+ createEdge({ id: 'ab', sourceId: 'a', targetId: 'b', sourcePort }),
32
+ ],
33
+ }),
34
+ });
35
+ }
36
+ const rect = { x: 100, y: 20, width: 8, height: 8 };
37
+
38
+ describe('renderer port measurement positions', () => {
39
+ it.each(['out', '*'])(
40
+ 'moves measured %s endpoints with their owner',
41
+ (sourcePort) => {
42
+ const flow = makeFlow(sourcePort);
43
+ const before = flow.toJSON();
44
+ flow.measurements.update([{ id: 'a:out', rect }]);
45
+ expect(flow.toJSON()).toEqual(before);
46
+ flow.nodes.move('a', { x: 50, y: 0 });
47
+ expect(flow.getBounds('a:out')).toEqual({ ...rect, x: 150 });
48
+ expect(flow.internals.engine.getEdgeEndpoints('ab')?.source).toEqual({
49
+ x: 154,
50
+ y: 24,
51
+ });
52
+ expect(
53
+ flow.internals.engine.getAllEdgePaths().get('ab')?.segments[0].points[0]
54
+ ).toEqual({ x: 154, y: 24 });
55
+ expect(flow.internals.engine.getPortPosition('a', 'out')).toEqual({
56
+ x: 154,
57
+ y: 24,
58
+ });
59
+ expect(flow.measurements.get('a:out')).toEqual(rect);
60
+ expect(flow.internals.engine.getNode('a')?.ports).toEqual(
61
+ before.graph.nodes[0].ports
62
+ );
63
+ flow.measurements.update([{ id: 'a:out', rect: { ...rect, x: 160 } }]);
64
+ flow.nodes.move('a', { x: 70, y: 0 });
65
+ expect(flow.getBounds('a:out')?.x).toBe(180);
66
+ flow.measurements.remove(['a:out']);
67
+ expect(flow.getBounds('a:out')?.x).toBe(80);
68
+ flow.destroy();
69
+ }
70
+ );
71
+
72
+ it('uses both port address forms for the same observation', () => {
73
+ const flow = makeFlow();
74
+ flow.measurements.update([{ id: 'explicit-out', rect }]);
75
+ flow.nodes.move('a', { x: 50, y: 0 });
76
+ expect(flow.getBounds('explicit-out')).toMatchObject({ x: 150, y: 20 });
77
+ expect(flow.internals.engine.getEdgeEndpoints('ab')?.source).toEqual({
78
+ x: 154,
79
+ y: 24,
80
+ });
81
+ flow.measurements.update([{ id: 'a:out', rect: { ...rect, x: 180 } }]);
82
+ expect(flow.measurements.get('explicit-out')?.x).toBe(180);
83
+ expect(flow.getBounds('explicit-out')?.x).toBe(180);
84
+ flow.measurements.remove(['explicit-out']);
85
+ expect(flow.measurements.get('a:out')).toBeUndefined();
86
+ flow.destroy();
87
+ });
88
+
89
+ it('tracks transient drag and grid snapping, then returns on cancellation', () => {
90
+ const flow = makeFlow();
91
+ flow.configure({ store: { snapToGrid: true, snapGrid: [16, 16] } });
92
+ flow.measurements.update([{ id: 'a:out', rect }]);
93
+ flow.internals.store.trigger.dragStart({ id: 'a' });
94
+ flow.internals.store.trigger.dragMove({ dx: 22, dy: 0 });
95
+ expect(flow.internals.engine.getEdgeEndpoints('ab')?.source).toEqual({
96
+ x: 120,
97
+ y: 24,
98
+ });
99
+ expect(flow.getBounds('a:out')?.x).toBe(116);
100
+ flow.internals.store.trigger.dragCancel();
101
+ expect(flow.getBounds('a:out')?.x).toBe(100);
102
+ flow.destroy();
103
+ });
104
+
105
+ it('chooses the nearest measured wildcard port and exposes it to hit testing', () => {
106
+ const flow = makeFlow('*');
107
+ flow.nodes.update('a', {
108
+ ports: [
109
+ createPort({
110
+ name: 'out',
111
+ direction: 'out',
112
+ side: 'right',
113
+ x: 0,
114
+ y: 0,
115
+ width: 8,
116
+ height: 8,
117
+ }),
118
+ createPort({
119
+ name: 'other',
120
+ direction: 'out',
121
+ side: 'right',
122
+ x: 200,
123
+ y: 0,
124
+ width: 8,
125
+ height: 8,
126
+ }),
127
+ ],
128
+ });
129
+ flow.measurements.update([
130
+ { id: 'a:out', rect: { ...rect, x: 240 } },
131
+ { id: 'a:other', rect: { ...rect, x: -100 } },
132
+ ]);
133
+ expect(flow.internals.engine.getEdgeEndpoints('ab')?.source).toEqual({
134
+ x: 244,
135
+ y: 24,
136
+ });
137
+ expect(
138
+ flow.internals.engine.getConnectionCandidate(
139
+ { x: 244, y: 24 },
140
+ { radius: 1 }
141
+ )
142
+ ).toMatchObject({ nodeId: 'a', portName: 'out' });
143
+ flow.destroy();
144
+ });
145
+
146
+ it('uses the supplied visual origin without absorbing layout offsets', () => {
147
+ const flow = makeFlow();
148
+ flow.nodes.move('a', { x: 50, y: 0 });
149
+ flow.internals.store.trigger.updateMeasurements({
150
+ derivedOnly: true,
151
+ measurements: [{ id: 'a:out', rect, origin: { x: 70, y: 0 } }],
152
+ });
153
+ expect(flow.getBounds('a:out')?.x).toBe(80);
154
+ flow.internals.store.trigger.updateMeasurements({
155
+ measurements: [{ id: 'a:out', rect }],
156
+ });
157
+ expect(flow.getBounds('a:out')).toEqual(rect);
158
+ expect(
159
+ flow.internals.store.getSnapshot().context.derivedPortMeasurements.size
160
+ ).toBe(0);
161
+ flow.destroy();
162
+ });
163
+
164
+ it('drops local observations with removed ports', () => {
165
+ const flow = makeFlow();
166
+ flow.measurements.update([{ id: 'a:out', rect }]);
167
+ flow.edges.delete('ab');
168
+ flow.nodes.update('a', { ports: [] });
169
+ expect(
170
+ flow.internals.store.getSnapshot().context.derivedPortMeasurements.size
171
+ ).toBe(0);
172
+ flow.destroy();
173
+ });
174
+
175
+ it('retains surviving port observations across unrelated undo', () => {
176
+ const flow = makeFlow();
177
+ flow.measurements.update([{ id: 'a:out', rect }]);
178
+ flow.nodes.move('b', { x: 350, y: 0 });
179
+ flow.history.undo();
180
+ expect(flow.getBounds('a:out')).toEqual(rect);
181
+ expect(flow.internals.engine.getEdgeEndpoints('ab')?.source).toEqual({
182
+ x: 104,
183
+ y: 24,
184
+ });
185
+ flow.destroy();
186
+ });
187
+ });
package/src/profile.ts ADDED
@@ -0,0 +1,124 @@
1
+ import type { FlowStoreContext } from "./store";
2
+ import type { FlowInputMode } from "./input";
3
+
4
+ export type FlowEventObject = {
5
+ type: string;
6
+ [key: string]: unknown;
7
+ };
8
+
9
+ export type FlowTransition<TEvent extends FlowEventObject = FlowEventObject> = (
10
+ context: FlowStoreContext,
11
+ event: TEvent,
12
+ enqueue: unknown
13
+ ) => FlowStoreContext;
14
+
15
+ export type FlowTransitions = Record<string, FlowTransition>;
16
+
17
+ export type FlowInputProfile = {
18
+ /**
19
+ * Input modes for this profile/mode. Resolved by
20
+ * `profileModesForContext(profile)`: the active store mode's modes win,
21
+ * then the profile-level modes, then the built-in defaults.
22
+ */
23
+ modes?: FlowInputMode[];
24
+ options?: {
25
+ dragThreshold?: number;
26
+ clickDistance?: number;
27
+ longPressDelay?: number;
28
+ autoPan?: {
29
+ enabled?: boolean;
30
+ threshold?: number;
31
+ speed?: number;
32
+ };
33
+ };
34
+ };
35
+
36
+ export type FlowOrigin =
37
+ | "top-left"
38
+ | "center"
39
+ | "center-x"
40
+ | "center-y"
41
+ | { x: number; y: number };
42
+
43
+ export type FlowSettings = {
44
+ nodeMeasurementOrigin?: FlowOrigin;
45
+ edgeLabelMeasurementOrigin?: FlowOrigin;
46
+ snapToGrid?: boolean;
47
+ snapGrid?: [number, number];
48
+ };
49
+
50
+ export type FlowPolicies = {
51
+ canSelect?: boolean;
52
+ canFocus?: boolean;
53
+ canHover?: boolean;
54
+ canEdit?: boolean;
55
+ canCreate?: boolean;
56
+ canDelete?: boolean;
57
+ canMove?: boolean;
58
+ canResize?: boolean;
59
+ canConnect?: boolean;
60
+ canReconnect?: boolean;
61
+ canReparent?: boolean;
62
+ canCopy?: boolean;
63
+ canPaste?: boolean;
64
+ canExport?: boolean;
65
+ canUndo?: boolean;
66
+ canRedo?: boolean;
67
+ canPan?: boolean;
68
+ canZoom?: boolean;
69
+ };
70
+
71
+ /** @experimental Rule slots are intentionally loose until the profile API stabilizes. */
72
+ export type FlowRules = {
73
+ delete?: unknown[];
74
+ move?: unknown[];
75
+ reparent?: unknown[];
76
+ };
77
+
78
+ /** @experimental Resolver slots are intentionally loose until the profile API stabilizes. */
79
+ export type FlowResolvers = {
80
+ blocks?: unknown;
81
+ bounds?: unknown;
82
+ owner?: unknown;
83
+ ports?: unknown;
84
+ };
85
+
86
+ export type FlowModeDefinition = {
87
+ on?: FlowTransitions;
88
+ input?: FlowInputProfile;
89
+ /** @experimental */
90
+ policies?: Partial<FlowPolicies>;
91
+ /** @experimental */
92
+ rules?: Partial<FlowRules>;
93
+ /** @experimental */
94
+ settings?: Partial<FlowSettings>;
95
+ };
96
+
97
+ export type FlowProfile = {
98
+ defaultMode: string;
99
+ on?: FlowTransitions;
100
+ input?: FlowInputProfile;
101
+ modes: Record<string, FlowModeDefinition>;
102
+ /** @experimental */
103
+ resolvers?: FlowResolvers;
104
+ /** @experimental */
105
+ policies?: FlowPolicies;
106
+ /** @experimental */
107
+ rules?: FlowRules;
108
+ /** @experimental */
109
+ settings?: FlowSettings;
110
+ };
111
+
112
+ export function defineFlowProfile<TProfile extends FlowProfile>(
113
+ profile: TProfile
114
+ ): TProfile {
115
+ return profile;
116
+ }
117
+
118
+ export const defaultFlowProfile = defineFlowProfile({
119
+ defaultMode: "interactive",
120
+ modes: {
121
+ interactive: {},
122
+ readonly: { policies: { canEdit: false } },
123
+ },
124
+ });
@@ -0,0 +1,103 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { createEdge, createGraph, createNode } from "./factories";
3
+ import {
4
+ composeGraphProjectionAttrs,
5
+ getProjectedNodeAttrs,
6
+ type ActiveGraphProjection,
7
+ } from "./projections";
8
+
9
+ const graph = createGraph({
10
+ nodes: [
11
+ createNode({ id: "a", x: 0, y: 0 }),
12
+ createNode({ id: "b", x: 100, y: 0 }),
13
+ ],
14
+ edges: [createEdge({ id: "a-b", sourceId: "a", targetId: "b" })],
15
+ });
16
+
17
+ describe("graph projections", () => {
18
+ it("composes attrs by priority, then array order", () => {
19
+ const projections: ActiveGraphProjection<number, number>[] = [
20
+ {
21
+ priority: 20,
22
+ projection: { id: "path", nodes: { a: 1 }, edges: { "a-b": 1 } },
23
+ mapper: {
24
+ getNodeAttrs: () => ({
25
+ className: "path",
26
+ style: { outline: "1px solid red" },
27
+ color: "red",
28
+ badge: "path",
29
+ }),
30
+ getEdgeAttrs: () => ({
31
+ className: "path-edge",
32
+ color: "red",
33
+ strokeWidth: 5,
34
+ badge: "path-edge",
35
+ }),
36
+ },
37
+ },
38
+ {
39
+ priority: 10,
40
+ projection: { id: "heat", nodes: { a: 7 }, edges: { "a-b": 7 } },
41
+ mapper: {
42
+ getNodeAttrs: ({ datum }) => ({
43
+ className: "heat",
44
+ style: { opacity: datum / 10, outline: "1px solid orange" },
45
+ color: "orange",
46
+ badge: "heat",
47
+ }),
48
+ getEdgeAttrs: ({ datum }) => ({
49
+ className: "heat-edge",
50
+ style: { opacity: datum / 10 },
51
+ color: "orange",
52
+ strokeWidth: 2,
53
+ badge: "heat-edge",
54
+ }),
55
+ },
56
+ },
57
+ {
58
+ priority: 20,
59
+ projection: { id: "review", nodes: { a: 1 } },
60
+ mapper: {
61
+ getNodeAttrs: () => ({
62
+ className: "review",
63
+ tooltip: "needs review",
64
+ }),
65
+ },
66
+ },
67
+ ];
68
+
69
+ const attrs = composeGraphProjectionAttrs({ graph, projections });
70
+
71
+ expect(attrs.nodes.get("a")).toEqual({
72
+ className: "heat path review",
73
+ style: { opacity: 0.7, outline: "1px solid red" },
74
+ color: "red",
75
+ badge: ["heat", "path"],
76
+ tooltip: "needs review",
77
+ });
78
+ expect(attrs.edges.get("a-b")).toEqual({
79
+ className: "heat-edge path-edge",
80
+ style: { opacity: 0.7 },
81
+ color: "red",
82
+ strokeWidth: 5,
83
+ badge: ["heat-edge", "path-edge"],
84
+ tooltip: undefined,
85
+ });
86
+ });
87
+
88
+ it("ignores projection data for missing graph ids", () => {
89
+ const projections: ActiveGraphProjection<number>[] = [
90
+ {
91
+ projection: { id: "missing", nodes: { missing: 1 } },
92
+ mapper: { getNodeAttrs: () => ({ color: "red" }) },
93
+ },
94
+ ];
95
+
96
+ const attrs = composeGraphProjectionAttrs({ graph, projections });
97
+
98
+ expect(attrs.nodes.size).toBe(0);
99
+ expect(getProjectedNodeAttrs({ graph, node: graph.nodes[0], projections })).toEqual(
100
+ {},
101
+ );
102
+ });
103
+ });
@@ -0,0 +1,295 @@
1
+ import type { FlowEdge, FlowGraph, FlowNode } from "./types";
2
+
3
+ export type GraphDecoration<TDecoration = unknown> =
4
+ | TDecoration
5
+ | readonly TDecoration[];
6
+
7
+ export type GraphNodeAttrs<
8
+ TStyle extends object = object,
9
+ TDecoration = unknown,
10
+ > = {
11
+ className?: string;
12
+ style?: TStyle;
13
+ color?: string;
14
+ opacity?: number;
15
+ badge?: GraphDecoration<TDecoration>;
16
+ tooltip?: GraphDecoration<TDecoration>;
17
+ };
18
+
19
+ export type GraphEdgeAttrs<
20
+ TStyle extends object = object,
21
+ TDecoration = unknown,
22
+ > = {
23
+ className?: string;
24
+ style?: TStyle;
25
+ color?: string;
26
+ opacity?: number;
27
+ strokeWidth?: number;
28
+ label?: TDecoration;
29
+ badge?: GraphDecoration<TDecoration>;
30
+ tooltip?: GraphDecoration<TDecoration>;
31
+ };
32
+
33
+ export type GraphProjection<TNodeDatum = unknown, TEdgeDatum = unknown> = {
34
+ id: string;
35
+ label?: string;
36
+ nodes?: Record<string, TNodeDatum>;
37
+ edges?: Record<string, TEdgeDatum>;
38
+ };
39
+
40
+ export type GraphProjectionMapper<
41
+ TNodeDatum = unknown,
42
+ TEdgeDatum = unknown,
43
+ TStyle extends object = object,
44
+ TDecoration = unknown,
45
+ > = {
46
+ getNodeAttrs?: (args: {
47
+ node: FlowNode;
48
+ datum: TNodeDatum;
49
+ projection: GraphProjection<TNodeDatum, TEdgeDatum>;
50
+ graph: FlowGraph;
51
+ }) => GraphNodeAttrs<TStyle, TDecoration> | null;
52
+ getEdgeAttrs?: (args: {
53
+ edge: FlowEdge;
54
+ datum: TEdgeDatum;
55
+ projection: GraphProjection<TNodeDatum, TEdgeDatum>;
56
+ graph: FlowGraph;
57
+ }) => GraphEdgeAttrs<TStyle, TDecoration> | null;
58
+ };
59
+
60
+ export type ActiveGraphProjection<
61
+ TNodeDatum = unknown,
62
+ TEdgeDatum = unknown,
63
+ TStyle extends object = object,
64
+ TDecoration = unknown,
65
+ > = {
66
+ projection: GraphProjection<TNodeDatum, TEdgeDatum>;
67
+ mapper: GraphProjectionMapper<TNodeDatum, TEdgeDatum, TStyle, TDecoration>;
68
+ priority?: number;
69
+ };
70
+
71
+ export type ComposedGraphProjectionAttrs<
72
+ TStyle extends object = object,
73
+ TDecoration = unknown,
74
+ > = {
75
+ nodes: Map<string, GraphNodeAttrs<TStyle, TDecoration>>;
76
+ edges: Map<string, GraphEdgeAttrs<TStyle, TDecoration>>;
77
+ };
78
+
79
+ type IndexedActiveProjection<TStyle extends object, TDecoration> =
80
+ ActiveGraphProjection<any, any, TStyle, TDecoration> & {
81
+ index: number;
82
+ };
83
+
84
+ export function sortActiveGraphProjections<
85
+ TStyle extends object = object,
86
+ TDecoration = unknown,
87
+ >(
88
+ active: readonly ActiveGraphProjection<
89
+ any,
90
+ any,
91
+ TStyle,
92
+ TDecoration
93
+ >[],
94
+ ): ActiveGraphProjection<any, any, TStyle, TDecoration>[] {
95
+ return active
96
+ .map((entry, index) => ({ ...entry, index }))
97
+ .sort(compareActiveGraphProjections)
98
+ .map(({ index: _index, ...entry }) => entry);
99
+ }
100
+
101
+ export function mergeGraphNodeAttrs<
102
+ TStyle extends object = object,
103
+ TDecoration = unknown,
104
+ >(
105
+ base: GraphNodeAttrs<TStyle, TDecoration> | undefined,
106
+ next: GraphNodeAttrs<TStyle, TDecoration> | null | undefined,
107
+ ): GraphNodeAttrs<TStyle, TDecoration> {
108
+ if (!next) return base ? { ...base } : {};
109
+
110
+ return {
111
+ ...base,
112
+ ...next,
113
+ className: mergeClassName(base?.className, next.className),
114
+ style: mergeStyle(base?.style, next.style),
115
+ badge: mergeDecoration(base?.badge, next.badge),
116
+ tooltip: mergeDecoration(base?.tooltip, next.tooltip),
117
+ };
118
+ }
119
+
120
+ export function mergeGraphEdgeAttrs<
121
+ TStyle extends object = object,
122
+ TDecoration = unknown,
123
+ >(
124
+ base: GraphEdgeAttrs<TStyle, TDecoration> | undefined,
125
+ next: GraphEdgeAttrs<TStyle, TDecoration> | null | undefined,
126
+ ): GraphEdgeAttrs<TStyle, TDecoration> {
127
+ if (!next) return base ? { ...base } : {};
128
+
129
+ return {
130
+ ...base,
131
+ ...next,
132
+ className: mergeClassName(base?.className, next.className),
133
+ style: mergeStyle(base?.style, next.style),
134
+ badge: mergeDecoration(base?.badge, next.badge),
135
+ tooltip: mergeDecoration(base?.tooltip, next.tooltip),
136
+ };
137
+ }
138
+
139
+ export function getProjectedNodeAttrs<
140
+ TStyle extends object = object,
141
+ TDecoration = unknown,
142
+ >(args: {
143
+ graph: FlowGraph;
144
+ node: FlowNode;
145
+ projections?: readonly ActiveGraphProjection<
146
+ any,
147
+ any,
148
+ TStyle,
149
+ TDecoration
150
+ >[];
151
+ }): GraphNodeAttrs<TStyle, TDecoration> {
152
+ if (!args.projections?.length) return {};
153
+
154
+ let attrs: GraphNodeAttrs<TStyle, TDecoration> | undefined;
155
+ for (const entry of sortActiveGraphProjections(args.projections)) {
156
+ const records = entry.projection.nodes;
157
+ if (!records || !Object.prototype.hasOwnProperty.call(records, args.node.id)) {
158
+ continue;
159
+ }
160
+ const datum = records[args.node.id];
161
+ if (datum === undefined) continue;
162
+ const next = entry.mapper.getNodeAttrs?.({
163
+ node: args.node,
164
+ datum,
165
+ projection: entry.projection,
166
+ graph: args.graph,
167
+ });
168
+ attrs = mergeGraphNodeAttrs(attrs, next);
169
+ }
170
+ return attrs ?? {};
171
+ }
172
+
173
+ export function getProjectedEdgeAttrs<
174
+ TStyle extends object = object,
175
+ TDecoration = unknown,
176
+ >(args: {
177
+ graph: FlowGraph;
178
+ edge: FlowEdge;
179
+ projections?: readonly ActiveGraphProjection<
180
+ any,
181
+ any,
182
+ TStyle,
183
+ TDecoration
184
+ >[];
185
+ }): GraphEdgeAttrs<TStyle, TDecoration> {
186
+ if (!args.projections?.length) return {};
187
+
188
+ let attrs: GraphEdgeAttrs<TStyle, TDecoration> | undefined;
189
+ for (const entry of sortActiveGraphProjections(args.projections)) {
190
+ const records = entry.projection.edges;
191
+ if (!records || !Object.prototype.hasOwnProperty.call(records, args.edge.id)) {
192
+ continue;
193
+ }
194
+ const datum = records[args.edge.id];
195
+ if (datum === undefined) continue;
196
+ const next = entry.mapper.getEdgeAttrs?.({
197
+ edge: args.edge,
198
+ datum,
199
+ projection: entry.projection,
200
+ graph: args.graph,
201
+ });
202
+ attrs = mergeGraphEdgeAttrs(attrs, next);
203
+ }
204
+ return attrs ?? {};
205
+ }
206
+
207
+ export function composeGraphProjectionAttrs<
208
+ TStyle extends object = object,
209
+ TDecoration = unknown,
210
+ >(args: {
211
+ graph: FlowGraph;
212
+ projections?: readonly ActiveGraphProjection<
213
+ any,
214
+ any,
215
+ TStyle,
216
+ TDecoration
217
+ >[];
218
+ }): ComposedGraphProjectionAttrs<TStyle, TDecoration> {
219
+ const nodes = new Map<string, GraphNodeAttrs<TStyle, TDecoration>>();
220
+ const edges = new Map<string, GraphEdgeAttrs<TStyle, TDecoration>>();
221
+ if (!args.projections?.length) return { nodes, edges };
222
+
223
+ const nodeById = new Map(args.graph.nodes.map((node) => [node.id, node]));
224
+ const edgeById = new Map(args.graph.edges.map((edge) => [edge.id, edge]));
225
+
226
+ for (const entry of sortActiveGraphProjections(args.projections)) {
227
+ for (const [id, datum] of Object.entries(entry.projection.nodes ?? {})) {
228
+ const node = nodeById.get(id);
229
+ if (!node) continue;
230
+ const next = entry.mapper.getNodeAttrs?.({
231
+ node,
232
+ datum,
233
+ projection: entry.projection,
234
+ graph: args.graph,
235
+ });
236
+ nodes.set(id, mergeGraphNodeAttrs(nodes.get(id), next));
237
+ }
238
+
239
+ for (const [id, datum] of Object.entries(entry.projection.edges ?? {})) {
240
+ const edge = edgeById.get(id);
241
+ if (!edge) continue;
242
+ const next = entry.mapper.getEdgeAttrs?.({
243
+ edge,
244
+ datum,
245
+ projection: entry.projection,
246
+ graph: args.graph,
247
+ });
248
+ edges.set(id, mergeGraphEdgeAttrs(edges.get(id), next));
249
+ }
250
+ }
251
+
252
+ return { nodes, edges };
253
+ }
254
+
255
+ function compareActiveGraphProjections<TStyle extends object, TDecoration>(
256
+ a: IndexedActiveProjection<TStyle, TDecoration>,
257
+ b: IndexedActiveProjection<TStyle, TDecoration>,
258
+ ): number {
259
+ return (a.priority ?? 0) - (b.priority ?? 0) || a.index - b.index;
260
+ }
261
+
262
+ function mergeClassName(
263
+ base: string | undefined,
264
+ next: string | undefined,
265
+ ): string | undefined {
266
+ if (!base) return next;
267
+ if (!next) return base;
268
+ return `${base} ${next}`;
269
+ }
270
+
271
+ function mergeStyle<TStyle extends object>(
272
+ base: TStyle | undefined,
273
+ next: TStyle | undefined,
274
+ ): TStyle | undefined {
275
+ if (!base) return next;
276
+ if (!next) return base;
277
+ return { ...base, ...next };
278
+ }
279
+
280
+ function mergeDecoration<TDecoration>(
281
+ base: GraphDecoration<TDecoration> | undefined,
282
+ next: GraphDecoration<TDecoration> | undefined,
283
+ ): GraphDecoration<TDecoration> | undefined {
284
+ if (base === undefined) return next;
285
+ if (next === undefined) return base;
286
+ return [...toDecorationArray(base), ...toDecorationArray(next)];
287
+ }
288
+
289
+ function toDecorationArray<TDecoration>(
290
+ value: GraphDecoration<TDecoration>,
291
+ ): TDecoration[] {
292
+ return Array.isArray(value)
293
+ ? [...(value as readonly TDecoration[])]
294
+ : [value as TDecoration];
295
+ }