@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.
Files changed (115) hide show
  1. package/README.md +2 -1
  2. package/dist/esm/index.d.ts +5 -2
  3. package/dist/esm/index.d.ts.map +1 -0
  4. package/dist/esm/index.js +19 -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,214 @@
1
+ import { describe, it, expect, vi, afterEach } from 'vitest';
2
+ import { createFlowStore } from '../store';
3
+ import { createFlowEngine } from '../engine';
4
+ import { createNode, createGraph } from '../factories';
5
+ import {
6
+ createFlowInputRuntime,
7
+ entityPressMode,
8
+ keyboardMode,
9
+ panePressMode,
10
+ type FlowInputEvent,
11
+ type FlowInputModifiers,
12
+ type FlowInputTarget,
13
+ } from './index';
14
+ import type { ReparentOptions } from './modes';
15
+ import type { FlowNode, Point } from '../types';
16
+
17
+ // Reparent-on-drop: fully policy-driven. These tests cover the documented
18
+ // configurability axes: off by default, overlap thresholds, hover delay,
19
+ // modifier-gesture unparenting (with cancellation by releasing the key),
20
+ // geometric unparenting (fixed groups only — derived groups stretch), custom
21
+ // `decide`, whole-selection reparenting, and no self/descendant parenting.
22
+
23
+ const NO_MODS: FlowInputModifiers = { shift: false, meta: false, ctrl: false, alt: false };
24
+
25
+ function makeRig(reparent?: boolean | ReparentOptions) {
26
+ const store = createFlowStore(
27
+ createGraph({
28
+ nodes: [
29
+ // fixed container: childArea x 510..890, y 550..790
30
+ createNode({
31
+ id: 'box',
32
+ x: 500,
33
+ y: 500,
34
+ width: 400,
35
+ height: 300,
36
+ group: { type: 'fixed', padding: 10, contentSize: { width: 0, height: 40 } },
37
+ }),
38
+ // derived group with one child
39
+ createNode({ id: 'g', width: 120, height: 40, group: { type: 'derived', padding: 16 } }),
40
+ { ...createNode({ id: 'child', x: 100, y: 100, width: 100, height: 50 }), parentId: 'g' } as FlowNode,
41
+ createNode({ id: 'free', x: 1000, y: 50, width: 100, height: 50 }),
42
+ createNode({ id: 'free2', x: 1150, y: 50, width: 100, height: 50 }),
43
+ ],
44
+ edges: [],
45
+ }),
46
+ { dragThreshold: 3 },
47
+ );
48
+ const engine = createFlowEngine(store);
49
+ const runtime = createFlowInputRuntime({
50
+ store,
51
+ engine,
52
+ modes: [keyboardMode(), entityPressMode({ reparent }), panePressMode()],
53
+ });
54
+ const ctx = () => store.getSnapshot().context;
55
+ const parentOf = (id: string) =>
56
+ ctx().graph.nodes.find((n) => n.id === id)?.parentId ?? null;
57
+ const pointer = (
58
+ kind: 'pointer.down' | 'pointer.move' | 'pointer.up',
59
+ point: Point,
60
+ extra: { target?: FlowInputTarget; modifiers?: Partial<FlowInputModifiers> } = {},
61
+ ): FlowInputEvent => ({
62
+ kind,
63
+ pointerId: 1,
64
+ pointerType: 'mouse',
65
+ button: kind === 'pointer.down' ? 0 : -1,
66
+ point,
67
+ screenPoint: point,
68
+ target: extra.target ?? { kind: 'pane' },
69
+ modifiers: { ...NO_MODS, ...extra.modifiers },
70
+ });
71
+ const node = (id: string): FlowInputTarget => ({ kind: 'entity', id });
72
+
73
+ /** Drag entity by (dx, dy) from `from`, with optional drop modifiers. */
74
+ function drag(
75
+ id: string,
76
+ from: Point,
77
+ dx: number,
78
+ dy: number,
79
+ dropModifiers: Partial<FlowInputModifiers> = {},
80
+ ) {
81
+ runtime.send(pointer('pointer.down', from, { target: node(id) }));
82
+ runtime.send(pointer('pointer.move', { x: from.x + dx / 2, y: from.y + dy / 2 }));
83
+ runtime.send(pointer('pointer.move', { x: from.x + dx, y: from.y + dy }));
84
+ runtime.send(
85
+ pointer('pointer.up', { x: from.x + dx, y: from.y + dy }, { modifiers: dropModifiers }),
86
+ );
87
+ }
88
+
89
+ return { store, engine, runtime, ctx, parentOf, pointer, node, drag };
90
+ }
91
+
92
+ afterEach(() => {
93
+ vi.useRealTimers();
94
+ });
95
+
96
+ describe('reparent-on-drop', () => {
97
+ it('is OFF by default: full overlap never reparents', () => {
98
+ const { drag, parentOf } = makeRig();
99
+ drag('free', { x: 1050, y: 75 }, -450, 550); // node lands at (600, 600), fully inside box
100
+ expect(parentOf('free')).toBeNull();
101
+ });
102
+
103
+ it('reparent: true adopts on sufficient overlap, as one undo step with the move', () => {
104
+ const { store, drag, parentOf, ctx } = makeRig(true);
105
+ drag('free', { x: 1050, y: 75 }, -450, 550);
106
+ expect(parentOf('free')).toBe('box');
107
+ expect(ctx().graph.nodes.find((n) => n.id === 'free')!.x).toBe(550);
108
+
109
+ store.trigger.undo();
110
+ expect(parentOf('free')).toBeNull();
111
+ expect(ctx().graph.nodes.find((n) => n.id === 'free')!.x).toBe(1000);
112
+ });
113
+
114
+ it('highlights the candidate while dragging and clears it after the drop', () => {
115
+ const { runtime, pointer, node, ctx } = makeRig(true);
116
+ runtime.send(pointer('pointer.down', { x: 1050, y: 75 }, { target: node('free') }));
117
+ runtime.send(pointer('pointer.move', { x: 600, y: 625 }));
118
+ expect(ctx().highlights.get('drop-target')?.has('box')).toBe(true);
119
+ runtime.send(pointer('pointer.up', { x: 600, y: 625 }));
120
+ expect(ctx().highlights.get('drop-target')).toBeUndefined();
121
+ });
122
+
123
+ it('minOverlap gates adoption', () => {
124
+ const strict = makeRig({ minOverlap: 0.9 });
125
+ // land half-inside: node at (840, 600) spans x 840..940, childArea ends at 890 → 50% overlap
126
+ strict.drag('free', { x: 1050, y: 75 }, -160, 550);
127
+ expect(strict.parentOf('free')).toBeNull();
128
+
129
+ const lax = makeRig({ minOverlap: 0.3 });
130
+ lax.drag('free', { x: 1050, y: 75 }, -160, 550);
131
+ expect(lax.parentOf('free')).toBe('box');
132
+ });
133
+
134
+ it('delayMs requires hovering the candidate before the drop counts', () => {
135
+ vi.useFakeTimers({ toFake: ['Date'] });
136
+ vi.setSystemTime(1_000_000);
137
+
138
+ const quick = makeRig({ delayMs: 500 });
139
+ quick.drag('free', { x: 1050, y: 75 }, -450, 550); // instant drop
140
+ expect(quick.parentOf('free')).toBeNull();
141
+
142
+ const patient = makeRig({ delayMs: 500 });
143
+ patient.runtime.send(
144
+ patient.pointer('pointer.down', { x: 1050, y: 75 }, { target: patient.node('free') }),
145
+ );
146
+ patient.runtime.send(patient.pointer('pointer.move', { x: 600, y: 625 }));
147
+ vi.setSystemTime(1_000_600); // hover 600ms over the candidate
148
+ patient.runtime.send(patient.pointer('pointer.up', { x: 600, y: 625 }));
149
+ expect(patient.parentOf('free')).toBe('box');
150
+ });
151
+
152
+ it('modifier gesture unparents; releasing it before the drop cancels', () => {
153
+ const { drag, parentOf } = makeRig(true);
154
+ // small drag inside the derived group, alt held at drop → unparent
155
+ drag('child', { x: 150, y: 125 }, 10, 0, { alt: true });
156
+ expect(parentOf('child')).toBeNull();
157
+
158
+ const second = makeRig(true);
159
+ // same drag, no modifier at drop (gesture "cancelled") → stays parented
160
+ second.drag('child', { x: 150, y: 125 }, 10, 0);
161
+ expect(second.parentOf('child')).toBe('g');
162
+ });
163
+
164
+ it('derived groups never geometrically unparent (bounds follow the child)', () => {
165
+ const { drag, parentOf } = makeRig(true);
166
+ drag('child', { x: 150, y: 125 }, 2000, 2000); // far away from everything
167
+ expect(parentOf('child')).toBe('g');
168
+ });
169
+
170
+ it('fixed groups geometrically unparent when escaped — unless disabled', () => {
171
+ const escape = makeRig(true);
172
+ // adopt first, then drag far out
173
+ escape.drag('free', { x: 1050, y: 75 }, -450, 550);
174
+ expect(escape.parentOf('free')).toBe('box');
175
+ escape.drag('free', { x: 600, y: 625 }, 1500, -400);
176
+ expect(escape.parentOf('free')).toBeNull();
177
+
178
+ const sticky = makeRig({ unparent: false });
179
+ sticky.drag('free', { x: 1050, y: 75 }, -450, 550);
180
+ expect(sticky.parentOf('free')).toBe('box');
181
+ sticky.drag('free', { x: 600, y: 625 }, 1500, -400);
182
+ expect(sticky.parentOf('free')).toBe('box');
183
+ });
184
+
185
+ it('a custom decide() replaces the built-in policy entirely', () => {
186
+ const { drag, parentOf } = makeRig({
187
+ decide: ({ modifiers }) => (modifiers.shift ? 'box' : undefined),
188
+ });
189
+ drag('free', { x: 1050, y: 75 }, 10, 0, { shift: true }); // nowhere near the box
190
+ expect(parentOf('free')).toBe('box');
191
+
192
+ const ignored = makeRig({ decide: () => undefined });
193
+ ignored.drag('free', { x: 1050, y: 75 }, -450, 550); // fully inside
194
+ expect(ignored.parentOf('free')).toBeNull();
195
+ });
196
+
197
+ it('reparents the whole selection (top-level nodes only)', () => {
198
+ const { store, runtime, pointer, node, parentOf } = makeRig(true);
199
+ store.trigger.select({ ids: ['g', 'child'], mode: 'multi' });
200
+ // drag g (and its selected child) into the box; union must land inside
201
+ runtime.send(pointer('pointer.down', { x: 150, y: 125 }, { target: node('g') }));
202
+ runtime.send(pointer('pointer.move', { x: 600, y: 700 }));
203
+ runtime.send(pointer('pointer.up', { x: 600, y: 700 }));
204
+
205
+ expect(parentOf('g')).toBe('box');
206
+ expect(parentOf('child')).toBe('g'); // nested selection keeps its parent
207
+ });
208
+
209
+ it('never parents a group into its own subtree', () => {
210
+ const { drag, parentOf } = makeRig(true);
211
+ drag('g', { x: 150, y: 70 }, 5, 5); // tiny move over its own child area
212
+ expect(parentOf('g')).toBeNull();
213
+ });
214
+ });
@@ -0,0 +1,138 @@
1
+ import * as dagre from '@dagrejs/dagre';
2
+ import type { FlowGraph, FlowNode, LayoutResult, Rect } from '@statelyai/flow';
3
+ import {
4
+ boundsOf,
5
+ edgeLabelPositionsFromNodePositions,
6
+ layoutRunContext,
7
+ rankDirection,
8
+ throwIfAborted,
9
+ type FlowLayoutFactory,
10
+ type FlowLayoutOptions,
11
+ } from './shared';
12
+
13
+ export type DagreLayoutOptions<TNodeData = any, TEdgeData = any, TLabelData = any> =
14
+ FlowLayoutOptions<TNodeData, TEdgeData, TLabelData>;
15
+
16
+ export const dagreLayout: FlowLayoutFactory = (options = {}) => {
17
+ return async (graph, runOptions = {}) => {
18
+ throwIfAborted(runOptions);
19
+ if (options.scope || runOptions.scope) {
20
+ throw new Error('dagreLayout does not support scope/incremental layout.');
21
+ }
22
+ const mutableGraph = graph as FlowGraph;
23
+ const context = layoutRunContext(mutableGraph, options, runOptions, 'dagre');
24
+ if (graph.nodes.some((node) => node.parentId)) {
25
+ const message = 'dagreLayout flattens parentId hierarchy; use elkLayout for compound layout.';
26
+ options.onWarning?.(message);
27
+ context.diagnostics.push({
28
+ severity: 'warning',
29
+ code: 'flattened-hierarchy',
30
+ message,
31
+ });
32
+ }
33
+
34
+ const g = new dagre.graphlib.Graph({ multigraph: true });
35
+ g.setGraph({
36
+ rankdir: rankDirection(options.direction ?? graph.direction),
37
+ nodesep: options.nodeSpacing ?? 60,
38
+ ranksep: options.rankSpacing ?? 90,
39
+ ...options.graph,
40
+ });
41
+ g.setDefaultEdgeLabel(() => ({}));
42
+
43
+ for (const node of graph.nodes) {
44
+ const bounds = boundsOf(node, options.getBounds as (node: FlowNode) => any);
45
+ g.setNode(node.id, {
46
+ width: bounds.width,
47
+ height: bounds.height,
48
+ ...options.node,
49
+ });
50
+ }
51
+
52
+ for (const edge of graph.edges) {
53
+ g.setEdge(edge.sourceId, edge.targetId, {
54
+ width: edge.label?.width ?? 0,
55
+ height: edge.label?.height ?? 0,
56
+ ...options.edge,
57
+ }, edge.id);
58
+ }
59
+
60
+ dagre.layout(g);
61
+ throwIfAborted(runOptions);
62
+
63
+ const positions = new Map<string, { x: number; y: number }>();
64
+ const nodeSizes = new Map<string, { width: number; height: number }>();
65
+ const nodeBounds = new Map<string, Rect>();
66
+ for (const node of graph.nodes) {
67
+ if (context.fixedIds.has(node.id)) continue;
68
+ const laidOut = g.node(node.id);
69
+ if (!laidOut) continue;
70
+ const position = {
71
+ x: laidOut.x - laidOut.width / 2,
72
+ y: laidOut.y - laidOut.height / 2,
73
+ };
74
+ const size = { width: laidOut.width, height: laidOut.height };
75
+ positions.set(node.id, position);
76
+ nodeSizes.set(node.id, size);
77
+ nodeBounds.set(node.id, { ...position, ...size });
78
+ }
79
+
80
+ const edgeLabelPositions = dagreEdgeLabelPositions(mutableGraph, g) ??
81
+ edgeLabelPositionsFromNodePositions(
82
+ mutableGraph,
83
+ positions,
84
+ options.getBounds as ((node: FlowNode) => Rect) | undefined,
85
+ nodeBounds,
86
+ options.direction ?? graph.direction,
87
+ );
88
+ const edgeLabelBounds = new Map<string, Rect>();
89
+ for (const edge of graph.edges) {
90
+ if (!edge.label || context.fixedIds.has(edge.id)) continue;
91
+ const position = edgeLabelPositions?.get(edge.id);
92
+ if (!position) continue;
93
+ edgeLabelBounds.set(edge.id, {
94
+ ...position,
95
+ width: edge.label.width,
96
+ height: edge.label.height,
97
+ });
98
+ }
99
+
100
+ return {
101
+ positions,
102
+ nodeSizes,
103
+ nodeBounds,
104
+ ...(edgeLabelPositions
105
+ ? {
106
+ edgeLabelPositions: new Map(
107
+ [...edgeLabelPositions].filter(
108
+ ([id]) => !context.fixedIds.has(id),
109
+ ),
110
+ ),
111
+ }
112
+ : {}),
113
+ ...(edgeLabelBounds.size > 0 ? { edgeLabelBounds } : {}),
114
+ ...(context.diagnostics.length > 0
115
+ ? { diagnostics: context.diagnostics }
116
+ : {}),
117
+ source: context.source,
118
+ metadata: context.metadata,
119
+ } satisfies LayoutResult;
120
+ };
121
+ };
122
+
123
+ function dagreEdgeLabelPositions(
124
+ graph: FlowGraph,
125
+ g: dagre.graphlib.Graph,
126
+ ): Map<string, { x: number; y: number }> | undefined {
127
+ const labels = new Map<string, { x: number; y: number }>();
128
+ for (const edge of graph.edges) {
129
+ if (!edge.label) continue;
130
+ const laidOut = g.edge({ v: edge.sourceId, w: edge.targetId, name: edge.id });
131
+ if (!laidOut || typeof laidOut.x !== 'number' || typeof laidOut.y !== 'number') continue;
132
+ labels.set(edge.id, {
133
+ x: laidOut.x - edge.label.width / 2,
134
+ y: laidOut.y - edge.label.height / 2,
135
+ });
136
+ }
137
+ return labels.size > 0 ? labels : undefined;
138
+ }