@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
package/src/snap.ts ADDED
@@ -0,0 +1,395 @@
1
+ import type { Rect } from './types';
2
+
3
+
4
+ // Snap Lines — Figma-style alignment guides
5
+ //
6
+ // Pure function: takes a dragged rect + other rects, returns snap result.
7
+ // No dependencies on store or engine — safe to use anywhere.
8
+ // Adapted from @statelyai/viz snap implementation.
9
+
10
+
11
+ export interface SnapLine {
12
+ axis: 'x' | 'y';
13
+ value: number;
14
+ }
15
+
16
+ export interface SpacingGuide {
17
+ axis: 'x' | 'y';
18
+ /** Position on the perpendicular axis (y for 'x' axis, x for 'y') */
19
+ position: number;
20
+ /** The gap distance being matched */
21
+ spacing: number;
22
+ /** Segments showing where spacing applies */
23
+ ranges: Array<{ start: number; end: number }>;
24
+ }
25
+
26
+ export interface SnapResult {
27
+ snapLines: SnapLine[];
28
+ /** Exact input rect from which the snap delta was calculated */
29
+ sourceRect: Rect;
30
+ /** Adjusted rect position (size unchanged) */
31
+ snapRect: Rect;
32
+ spacingGuides?: SpacingGuide[];
33
+ }
34
+
35
+ export interface SnapOptions {
36
+ /** Distance threshold in canvas pixels (default 5) */
37
+ threshold?: number;
38
+ /** Enable equal-spacing guides (default false) */
39
+ enableSpacing?: boolean;
40
+ }
41
+
42
+ /**
43
+ * Compute snap lines for a dragged rect against other rects.
44
+ *
45
+ * Checks alignment of left/right/center-x and top/bottom/center-y
46
+ * against all target rects. Optionally checks equal spacing between elements.
47
+ *
48
+ * Returns null if no snap is within threshold.
49
+ */
50
+ export function getSnapLines(
51
+ rect: Rect,
52
+ otherRects: Rect[],
53
+ { threshold = 5, enableSpacing = false }: SnapOptions = {},
54
+ ): SnapResult | null {
55
+ if (otherRects.length === 0) return null;
56
+
57
+ const getPoints = (r: Rect) => ({
58
+ left: r.x,
59
+ right: r.x + r.width,
60
+ top: r.y,
61
+ bottom: r.y + r.height,
62
+ centerX: r.x + r.width / 2,
63
+ centerY: r.y + r.height / 2,
64
+ });
65
+
66
+ const rectPoints = getPoints(rect);
67
+ const snapLines: SnapLine[] = [];
68
+ let snapX: number | null = null;
69
+ let snapY: number | null = null;
70
+ let minDistX = threshold + 1;
71
+ let minDistY = threshold + 1;
72
+ let spacingGuides: SpacingGuide[] = [];
73
+
74
+
75
+ // Equal spacing detection
76
+
77
+ if (enableSpacing && otherRects.length >= 1) {
78
+ const hasVerticalOverlap = (a: Rect, b: Rect): boolean =>
79
+ a.y < b.y + b.height && a.y + a.height > b.y;
80
+
81
+ const hasHorizontalOverlap = (a: Rect, b: Rect): boolean =>
82
+ a.x < b.x + b.width && a.x + a.width > b.x;
83
+
84
+ // Horizontal spacing
85
+ for (const target of otherRects) {
86
+ if (!hasVerticalOverlap(rect, target)) continue;
87
+
88
+ for (const other of otherRects) {
89
+ if (other === target) continue;
90
+ if (!hasVerticalOverlap(other, target)) continue;
91
+
92
+ const gapRight = other.x - (target.x + target.width);
93
+ const gapLeft = target.x - (other.x + other.width);
94
+
95
+ // Dragged rect right of target, matching gap on left
96
+ if (gapLeft > 0) {
97
+ const snapPos = target.x + target.width + gapLeft;
98
+ const dist = Math.abs(rectPoints.left - snapPos);
99
+ if (dist <= threshold && dist < minDistX) {
100
+ const oTop = Math.max(rect.y, target.y, other.y);
101
+ const oBot = Math.min(rect.y + rect.height, target.y + target.height, other.y + other.height);
102
+ const guideY = (oTop + oBot) / 2;
103
+ minDistX = dist;
104
+ snapX = snapPos;
105
+ spacingGuides = [
106
+ {
107
+ axis: 'x',
108
+ position: guideY,
109
+ spacing: gapLeft,
110
+ ranges: [
111
+ { start: other.x + other.width, end: target.x },
112
+ { start: target.x + target.width, end: snapPos },
113
+ ],
114
+ },
115
+ ];
116
+ }
117
+ }
118
+
119
+ // Dragged rect left of target, matching gap on right
120
+ if (gapRight > 0) {
121
+ const snapPos = target.x - gapRight - rect.width;
122
+ const dist = Math.abs(rectPoints.left - snapPos);
123
+ if (dist <= threshold && dist < minDistX) {
124
+ const oTop = Math.max(rect.y, target.y, other.y);
125
+ const oBot = Math.min(rect.y + rect.height, target.y + target.height, other.y + other.height);
126
+ const guideY = (oTop + oBot) / 2;
127
+ minDistX = dist;
128
+ snapX = snapPos;
129
+ spacingGuides = [
130
+ {
131
+ axis: 'x',
132
+ position: guideY,
133
+ spacing: gapRight,
134
+ ranges: [
135
+ { start: snapPos + rect.width, end: target.x },
136
+ { start: target.x + target.width, end: other.x },
137
+ ],
138
+ },
139
+ ];
140
+ }
141
+ }
142
+ }
143
+ }
144
+
145
+ // Vertical spacing
146
+ for (const target of otherRects) {
147
+ if (!hasHorizontalOverlap(rect, target)) continue;
148
+
149
+ for (const other of otherRects) {
150
+ if (other === target) continue;
151
+ if (!hasHorizontalOverlap(other, target)) continue;
152
+
153
+ const gapBelow = other.y - (target.y + target.height);
154
+ const gapAbove = target.y - (other.y + other.height);
155
+
156
+ if (gapAbove > 0) {
157
+ const snapPos = target.y + target.height + gapAbove;
158
+ const dist = Math.abs(rectPoints.top - snapPos);
159
+ if (dist <= threshold && dist < minDistY) {
160
+ const oLeft = Math.max(rect.x, target.x, other.x);
161
+ const oRight = Math.min(rect.x + rect.width, target.x + target.width, other.x + other.width);
162
+ const guideX = (oLeft + oRight) / 2;
163
+ minDistY = dist;
164
+ snapY = snapPos;
165
+ spacingGuides = spacingGuides.filter((g) => g.axis !== 'y');
166
+ spacingGuides.push({
167
+ axis: 'y',
168
+ position: guideX,
169
+ spacing: gapAbove,
170
+ ranges: [
171
+ { start: other.y + other.height, end: target.y },
172
+ { start: target.y + target.height, end: snapPos },
173
+ ],
174
+ });
175
+ }
176
+ }
177
+
178
+ if (gapBelow > 0) {
179
+ const snapPos = target.y - gapBelow - rect.height;
180
+ const dist = Math.abs(rectPoints.top - snapPos);
181
+ if (dist <= threshold && dist < minDistY) {
182
+ const oLeft = Math.max(rect.x, target.x, other.x);
183
+ const oRight = Math.min(rect.x + rect.width, target.x + target.width, other.x + other.width);
184
+ const guideX = (oLeft + oRight) / 2;
185
+ minDistY = dist;
186
+ snapY = snapPos;
187
+ spacingGuides = spacingGuides.filter((g) => g.axis !== 'y');
188
+ spacingGuides.push({
189
+ axis: 'y',
190
+ position: guideX,
191
+ spacing: gapBelow,
192
+ ranges: [
193
+ { start: snapPos + rect.height, end: target.y },
194
+ { start: target.y + target.height, end: other.y },
195
+ ],
196
+ });
197
+ }
198
+ }
199
+ }
200
+ }
201
+
202
+ // Horizontal distribute: the dragged rect sits in the gap between a left and
203
+ // a right neighbor, equidistant from both (equal spacing on each side).
204
+ for (const left of otherRects) {
205
+ if (!hasVerticalOverlap(rect, left)) continue;
206
+ const leftEdge = left.x + left.width;
207
+ for (const right of otherRects) {
208
+ if (right === left) continue;
209
+ if (!hasVerticalOverlap(rect, right)) continue;
210
+ const rightEdge = right.x;
211
+ if (leftEdge >= rightEdge) continue; // left must sit left of right, with a gap
212
+ const snapPos = (leftEdge + rightEdge - rect.width) / 2;
213
+ const gap = snapPos - leftEdge; // == rightEdge - (snapPos + rect.width)
214
+ if (gap <= 0) continue; // no room for the rect between them
215
+ const dist = Math.abs(rectPoints.left - snapPos);
216
+ if (dist <= threshold && dist < minDistX) {
217
+ const oTop = Math.max(rect.y, left.y, right.y);
218
+ const oBot = Math.min(rect.y + rect.height, left.y + left.height, right.y + right.height);
219
+ const guideY = (oTop + oBot) / 2;
220
+ minDistX = dist;
221
+ snapX = snapPos;
222
+ spacingGuides = spacingGuides.filter((g) => g.axis !== 'x');
223
+ spacingGuides.push({
224
+ axis: 'x',
225
+ position: guideY,
226
+ spacing: gap,
227
+ ranges: [
228
+ { start: leftEdge, end: snapPos },
229
+ { start: snapPos + rect.width, end: rightEdge },
230
+ ],
231
+ });
232
+ }
233
+ }
234
+ }
235
+
236
+ // Vertical distribute: equidistant between an above and a below neighbor.
237
+ for (const above of otherRects) {
238
+ if (!hasHorizontalOverlap(rect, above)) continue;
239
+ const aboveEdge = above.y + above.height;
240
+ for (const below of otherRects) {
241
+ if (below === above) continue;
242
+ if (!hasHorizontalOverlap(rect, below)) continue;
243
+ const belowEdge = below.y;
244
+ if (aboveEdge >= belowEdge) continue;
245
+ const snapPos = (aboveEdge + belowEdge - rect.height) / 2;
246
+ const gap = snapPos - aboveEdge;
247
+ if (gap <= 0) continue;
248
+ const dist = Math.abs(rectPoints.top - snapPos);
249
+ if (dist <= threshold && dist < minDistY) {
250
+ const oLeft = Math.max(rect.x, above.x, below.x);
251
+ const oRight = Math.min(rect.x + rect.width, above.x + above.width, below.x + below.width);
252
+ const guideX = (oLeft + oRight) / 2;
253
+ minDistY = dist;
254
+ snapY = snapPos;
255
+ spacingGuides = spacingGuides.filter((g) => g.axis !== 'y');
256
+ spacingGuides.push({
257
+ axis: 'y',
258
+ position: guideX,
259
+ spacing: gap,
260
+ ranges: [
261
+ { start: aboveEdge, end: snapPos },
262
+ { start: snapPos + rect.height, end: belowEdge },
263
+ ],
264
+ });
265
+ }
266
+ }
267
+ }
268
+ }
269
+
270
+
271
+ // Edge & center alignment
272
+
273
+ for (const target of otherRects) {
274
+ const tp = getPoints(target);
275
+
276
+ // X-axis checks: left/right/centerX vs target left/right/centerX
277
+ const xChecks = [
278
+ { source: rectPoints.left, target: tp.left },
279
+ { source: rectPoints.left, target: tp.right },
280
+ { source: rectPoints.left, target: tp.centerX },
281
+ { source: rectPoints.right, target: tp.left },
282
+ { source: rectPoints.right, target: tp.right },
283
+ { source: rectPoints.right, target: tp.centerX },
284
+ { source: rectPoints.centerX, target: tp.left },
285
+ { source: rectPoints.centerX, target: tp.right },
286
+ { source: rectPoints.centerX, target: tp.centerX },
287
+ ];
288
+
289
+ for (const check of xChecks) {
290
+ const dist = Math.abs(check.source - check.target);
291
+ if (dist <= threshold && dist < minDistX) {
292
+ minDistX = dist;
293
+ snapX = check.target - (check.source - rect.x);
294
+ // Edge snapping closer than spacing → clear spacing guides for this axis
295
+ if (spacingGuides.some((g) => g.axis === 'x')) {
296
+ spacingGuides = spacingGuides.filter((g) => g.axis !== 'x');
297
+ }
298
+ }
299
+ }
300
+
301
+ // Y-axis checks
302
+ const yChecks = [
303
+ { source: rectPoints.top, target: tp.top },
304
+ { source: rectPoints.top, target: tp.bottom },
305
+ { source: rectPoints.top, target: tp.centerY },
306
+ { source: rectPoints.bottom, target: tp.top },
307
+ { source: rectPoints.bottom, target: tp.bottom },
308
+ { source: rectPoints.bottom, target: tp.centerY },
309
+ { source: rectPoints.centerY, target: tp.top },
310
+ { source: rectPoints.centerY, target: tp.bottom },
311
+ { source: rectPoints.centerY, target: tp.centerY },
312
+ ];
313
+
314
+ for (const check of yChecks) {
315
+ const dist = Math.abs(check.source - check.target);
316
+ if (dist <= threshold && dist < minDistY) {
317
+ minDistY = dist;
318
+ snapY = check.target - (check.source - rect.y);
319
+ if (spacingGuides.some((g) => g.axis === 'y')) {
320
+ spacingGuides = spacingGuides.filter((g) => g.axis !== 'y');
321
+ }
322
+ }
323
+ }
324
+ }
325
+
326
+ if (snapX === null && snapY === null) return null;
327
+
328
+ const snapRect: Rect = {
329
+ x: snapX ?? rect.x,
330
+ y: snapY ?? rect.y,
331
+ width: rect.width,
332
+ height: rect.height,
333
+ };
334
+
335
+
336
+ // Derive which alignment lines to display
337
+
338
+ const snappedPoints = getPoints(snapRect);
339
+ const seenLines = new Set<string>();
340
+
341
+ for (const target of otherRects) {
342
+ const tp = getPoints(target);
343
+
344
+ const xAligns = [
345
+ { source: snappedPoints.left, target: tp.left },
346
+ { source: snappedPoints.left, target: tp.right },
347
+ { source: snappedPoints.left, target: tp.centerX },
348
+ { source: snappedPoints.right, target: tp.left },
349
+ { source: snappedPoints.right, target: tp.right },
350
+ { source: snappedPoints.right, target: tp.centerX },
351
+ { source: snappedPoints.centerX, target: tp.left },
352
+ { source: snappedPoints.centerX, target: tp.right },
353
+ { source: snappedPoints.centerX, target: tp.centerX },
354
+ ];
355
+
356
+ for (const a of xAligns) {
357
+ if (Math.abs(a.source - a.target) < 0.01) {
358
+ const key = `x:${a.target}`;
359
+ if (!seenLines.has(key)) {
360
+ seenLines.add(key);
361
+ snapLines.push({ axis: 'x', value: a.target });
362
+ }
363
+ }
364
+ }
365
+
366
+ const yAligns = [
367
+ { source: snappedPoints.top, target: tp.top },
368
+ { source: snappedPoints.top, target: tp.bottom },
369
+ { source: snappedPoints.top, target: tp.centerY },
370
+ { source: snappedPoints.bottom, target: tp.top },
371
+ { source: snappedPoints.bottom, target: tp.bottom },
372
+ { source: snappedPoints.bottom, target: tp.centerY },
373
+ { source: snappedPoints.centerY, target: tp.top },
374
+ { source: snappedPoints.centerY, target: tp.bottom },
375
+ { source: snappedPoints.centerY, target: tp.centerY },
376
+ ];
377
+
378
+ for (const a of yAligns) {
379
+ if (Math.abs(a.source - a.target) < 0.01) {
380
+ const key = `y:${a.target}`;
381
+ if (!seenLines.has(key)) {
382
+ seenLines.add(key);
383
+ snapLines.push({ axis: 'y', value: a.target });
384
+ }
385
+ }
386
+ }
387
+ }
388
+
389
+ return {
390
+ snapLines,
391
+ sourceRect: rect,
392
+ snapRect,
393
+ spacingGuides: spacingGuides.length > 0 ? spacingGuides : undefined,
394
+ };
395
+ }