@statelyai/flow 0.6.0 → 0.8.2
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 +234 -140
- package/dist/esm/index.d.ts +150 -32
- package/dist/esm/index.js +3638 -3132
- package/dist/esm/layout/dagre.js +1 -1
- package/dist/esm/layout/elk.js +637 -25
- package/dist/esm/shared.js +29 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -27,11 +27,37 @@ const flow = createFlow({
|
|
|
27
27
|
graph: {
|
|
28
28
|
direction: 'down',
|
|
29
29
|
nodes: [
|
|
30
|
-
{
|
|
31
|
-
|
|
30
|
+
{
|
|
31
|
+
type: 'node',
|
|
32
|
+
id: 'a',
|
|
33
|
+
x: 0,
|
|
34
|
+
y: 0,
|
|
35
|
+
width: 200,
|
|
36
|
+
height: 80,
|
|
37
|
+
data: { label: 'Start' },
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
type: 'node',
|
|
41
|
+
id: 'b',
|
|
42
|
+
x: 0,
|
|
43
|
+
y: 150,
|
|
44
|
+
width: 200,
|
|
45
|
+
height: 80,
|
|
46
|
+
data: { label: 'End' },
|
|
47
|
+
},
|
|
32
48
|
],
|
|
33
49
|
edges: [
|
|
34
|
-
{
|
|
50
|
+
{
|
|
51
|
+
type: 'edge',
|
|
52
|
+
id: 'e1',
|
|
53
|
+
sourceId: 'a',
|
|
54
|
+
targetId: 'b',
|
|
55
|
+
x: 0,
|
|
56
|
+
y: 0,
|
|
57
|
+
width: 0,
|
|
58
|
+
height: 0,
|
|
59
|
+
data: {},
|
|
60
|
+
},
|
|
35
61
|
],
|
|
36
62
|
},
|
|
37
63
|
});
|
|
@@ -73,16 +99,27 @@ await flow.layout.run(
|
|
|
73
99
|
elkLayout({
|
|
74
100
|
nodeSpacing: 80,
|
|
75
101
|
rankSpacing: 120,
|
|
76
|
-
})
|
|
102
|
+
})
|
|
77
103
|
);
|
|
78
104
|
```
|
|
79
105
|
|
|
80
106
|
<!-- adapter contracts from src/layout/elk.ts src/layout/dagre.ts and src/layout/shared.ts -->
|
|
81
107
|
|
|
82
|
-
`elkLayout()` supports compound nodes, ports, edge labels, direction,
|
|
83
|
-
bounds via `getBounds`, and scoped
|
|
84
|
-
|
|
85
|
-
|
|
108
|
+
`elkLayout()` supports compound nodes, ports, edge labels, graph direction,
|
|
109
|
+
per-group child direction, measured bounds via `getBounds`, and scoped
|
|
110
|
+
incremental layout with `scope: { ids }`.
|
|
111
|
+
Results include computed node sizes and bounds, label bounds, routed interior
|
|
112
|
+
waypoints, source metadata, and diagnostics. Hierarchical ELK runs repair
|
|
113
|
+
invalid nested label positions by choosing the nearest node-free position, keep
|
|
114
|
+
intra-compound labels inside their owning group, reserve 24px of rank clearance
|
|
115
|
+
around native labels at both root and child levels, favor straight child edges, and
|
|
116
|
+
reserve sibling space for the final label-expanded derived bounds. Cross-level
|
|
117
|
+
ancestor/descendant edges force integrated hierarchy handling even when a caller's
|
|
118
|
+
general ELK options request separate child passes; their labels reserve a clear
|
|
119
|
+
lane between the group's content block and the descendant instead of being
|
|
120
|
+
pushed outside the compound. When integrated hierarchy would override a
|
|
121
|
+
conflicting per-group direction, a focused layered pass restores that group's
|
|
122
|
+
child ranks without changing the root direction. Scoped passes send only
|
|
86
123
|
the induced graph to ELK; fixed and pinned entities remain in the input but are
|
|
87
124
|
omitted from returned geometry.
|
|
88
125
|
|
|
@@ -111,7 +148,7 @@ The canonical data model is `VisualGraph` from `@statelyai/graph`. Flow extends
|
|
|
111
148
|
<!-- flow entity types from src/types.ts -->
|
|
112
149
|
|
|
113
150
|
- **`FlowNode`** — `VisualNode` + `{ kind, dx, dy, style, ports, autoSize, autoSizeAnchor, resize, hidden, selectable, draggable, connectable, deletable, resizable }` for application-defined rendering kinds, drag offsets, DOM-owned sizing, anchor policy, style overrides, resize constraints, and per-entity behavior. Keep the structural `type` equal to `"node"`; use `kind` for variants such as `state` or `annotation`.
|
|
114
|
-
- **`FlowEdge`** — `VisualEdge` + `{ label, waypoints, sourcePort, targetPort, kind, style, labelAnchor }` for edge labels, editable
|
|
151
|
+
- **`FlowEdge`** — `VisualEdge` + `{ label, waypoints, svgPath, sourcePort, targetPort, kind, style, labelAnchor }` for edge labels, editable router via-points, exact SVG geometry, port binding, and edge styling. Canonical `VisualEdge.points` plus `routing` are an authoritative precomputed route and bypass Flow routers; an optional `svgPath` controls exact SVG paint while points remain authoritative for interaction geometry. Reconnects, node geometry updates, committed drags, and layout application clear stale precomputed routes; layout-provided edge routes replace them with router waypoints. `label.autoSize` makes rendered DOM own transient label dimensions without overwriting authored fallback `width`/`height`; inherited edge `x/y/width/height` are reserved, while positioned edge content belongs on `label`.
|
|
115
152
|
- **`FlowGraph`** — `VisualGraph` with `FlowNode[]` and `FlowEdge[]`
|
|
116
153
|
- **`FlowEntityRef`** — descriptor for a node, edge, port, or block resolved by id. `FlowAddressable` is a deprecated alias.
|
|
117
154
|
|
|
@@ -157,10 +194,7 @@ visibility with visual projections/highlights. The default scope is
|
|
|
157
194
|
`{ parentId: null, depth: null }`, preserving the full visible graph.
|
|
158
195
|
|
|
159
196
|
```ts
|
|
160
|
-
flow.view.setScope(
|
|
161
|
-
{ parentId: 'compound', depth: 1 },
|
|
162
|
-
{ selection: 'prune' },
|
|
163
|
-
);
|
|
197
|
+
flow.view.setScope({ parentId: 'compound', depth: 1 }, { selection: 'prune' });
|
|
164
198
|
|
|
165
199
|
const { nodes, edges, boundaryEndpoints } = flow.view.getEntities();
|
|
166
200
|
```
|
|
@@ -189,19 +223,44 @@ then target that frame. The default remains the full, current viewport.
|
|
|
189
223
|
```ts
|
|
190
224
|
flow.viewport.setFitFrame({ x: 320, y: 0, width: 880, height: 800 });
|
|
191
225
|
|
|
226
|
+
// Keep the same world point centered as a docked panel changes the frame.
|
|
227
|
+
flow.viewport.setFitFrame(nextFrame, { preserveCenter: true });
|
|
228
|
+
|
|
229
|
+
// Floating chrome can register live, measured screen-space exclusions. Fit,
|
|
230
|
+
// center, and default zoom pivots use the remaining safe frame.
|
|
231
|
+
const unregister = flow.viewport.registerFitExclusion({
|
|
232
|
+
side: 'left',
|
|
233
|
+
gap: 8,
|
|
234
|
+
getRect: () => measuredPaletteRect,
|
|
235
|
+
});
|
|
236
|
+
|
|
192
237
|
flow.viewport.fit();
|
|
193
238
|
flow.viewport.fitBounds(bounds);
|
|
194
239
|
flow.viewport.centerOn({ x: 100, y: 50 });
|
|
195
240
|
flow.viewport.zoomTo(1.5);
|
|
196
241
|
|
|
197
242
|
flow.viewport.setFitFrame(null); // reset to the full viewport
|
|
243
|
+
unregister();
|
|
198
244
|
```
|
|
199
245
|
|
|
200
246
|
Frames require finite `x`/`y` and positive finite dimensions. A `frame` on
|
|
201
247
|
`fit()`, `fitBounds()`, or `centerOn()` overrides the registered frame for that
|
|
202
|
-
call. `
|
|
248
|
+
call. `preserveCenter` shifts the viewport by the fit-frame center delta, so
|
|
249
|
+
resizing or docking panels keeps the same world point centered. `fit()` still composes with the current structural view and its `nodes`
|
|
203
250
|
subset. Animation resolves the same frame-aware target before it begins.
|
|
204
251
|
|
|
252
|
+
Duration-based viewport animations use focal, log-space zooming. Set `lerp` to
|
|
253
|
+
an exponential smoothing factor in `(0, 1]` for a camera that continuously
|
|
254
|
+
approaches changing active entities. The factor is normalized to a nominal
|
|
255
|
+
60 Hz frame, so it feels consistent across display refresh rates:
|
|
256
|
+
|
|
257
|
+
```ts
|
|
258
|
+
flow.viewport.fit({
|
|
259
|
+
nodes: activeEntityIds.map((id) => ({ id })),
|
|
260
|
+
lerp: 0.15,
|
|
261
|
+
});
|
|
262
|
+
```
|
|
263
|
+
|
|
205
264
|
Every viewport mutation (`set`, `panBy`, `zoomTo`, `zoomBy`, `fit`,
|
|
206
265
|
`fitBounds`, and `centerOn`) returns `ViewportAnimationControls` with `cancel()`
|
|
207
266
|
and a `done` promise. `done` resolves on completion or cancellation. Immediate
|
|
@@ -237,7 +296,15 @@ const graph = {
|
|
|
237
296
|
width: 160,
|
|
238
297
|
height: 60,
|
|
239
298
|
ports: [
|
|
240
|
-
{
|
|
299
|
+
{
|
|
300
|
+
name: 'out',
|
|
301
|
+
side: 'right',
|
|
302
|
+
direction: 'out',
|
|
303
|
+
x: 150,
|
|
304
|
+
y: 25,
|
|
305
|
+
width: 10,
|
|
306
|
+
height: 10,
|
|
307
|
+
},
|
|
241
308
|
],
|
|
242
309
|
data: {},
|
|
243
310
|
},
|
|
@@ -305,9 +372,9 @@ durable final layout.
|
|
|
305
372
|
|
|
306
373
|
<!-- serialization API from src/serialize.ts and src/createFlow.ts -->
|
|
307
374
|
|
|
308
|
-
`flow.toJSON()` returns `{ version: 1, graph, viewport, collapsedNodeIds }`. The graph is plain persisted intent: pending node and label drag offsets are folded into `x/y`, `dx/dy` and DOM `measured` sizes are stripped, and `style.customWidth/customHeight` are kept. Selection, hover, connection state, highlights, measurements, and undo history are session state and are not serialized.
|
|
375
|
+
`flow.toJSON()` returns `{ version: 1, graph, viewport, collapsedNodeIds }`. The graph is plain persisted intent: pending node and label drag offsets are folded into `x/y`, pending edge drag offsets are folded into route waypoints, `dx/dy` and DOM `measured` sizes are stripped, and `style.customWidth/customHeight` are kept. Selection, hover, connection state, highlights, measurements, and undo history are session state and are not serialized.
|
|
309
376
|
|
|
310
|
-
Use `fromJSON(data, options?)` to create a new controller from saved data, or `flow.load(data)` to validate and replace an existing document atomically. `load()` returns `{ ok: true, value }` or `{ ok: false, error }`. User data is deep-cloned and must be JSON-safe;
|
|
377
|
+
Use `fromJSON(data, options?)` to create a new controller from saved data, or `flow.load(data)` to validate and replace an existing document atomically. `load()` returns `{ ok: true, value }` or `{ ok: false, error }`. User data is deep-cloned and must be JSON-safe; edge route points must be finite, routing must be `polyline`, `orthogonal`, or `splines`, and SVG paths accept only path commands with finite numeric parameters. Optional Standard Schema-compatible contracts can validate and migrate application data.
|
|
311
378
|
|
|
312
379
|
Use `flow.replaceGraph(graph, options?)` when another model is authoritative and
|
|
313
380
|
continuously projects its graph into Flow. It validates atomically, returns the
|
|
@@ -324,140 +391,140 @@ events.
|
|
|
324
391
|
|
|
325
392
|
### Graph mutations
|
|
326
393
|
|
|
327
|
-
| Event
|
|
328
|
-
|
|
329
|
-
| `createNode`
|
|
330
|
-
| `deleteNode`
|
|
331
|
-
| `updateNode`
|
|
332
|
-
| `updateNodeData` | `{ id, data }`
|
|
333
|
-
| `createEdge`
|
|
334
|
-
| `deleteEdge`
|
|
335
|
-
| `deleteEntities` | `{ nodeIds?, edgeIds? }`
|
|
336
|
-
| `updateEdge`
|
|
337
|
-
| `updateEdgeData` | `{ id, data }`
|
|
338
|
-
| `deleteSelected` | —
|
|
339
|
-
| `setGraph`
|
|
340
|
-
| `replaceGraph`
|
|
341
|
-
| `reset`
|
|
342
|
-
| `reparentNode`
|
|
343
|
-
| `collapseNode`
|
|
344
|
-
| `expandNode`
|
|
345
|
-
| `reconnectEdge`
|
|
394
|
+
| Event | Payload | Description |
|
|
395
|
+
| ---------------- | ------------------------------------------------------------------------ | --------------------------------------------------------------------------------- |
|
|
396
|
+
| `createNode` | `{ node: FlowNode }` | Add a node |
|
|
397
|
+
| `deleteNode` | `{ id }` | Remove node + cascade edges |
|
|
398
|
+
| `updateNode` | `{ id, patch? }` or `{ id, updater? }` | Patch/replace a node without changing id/type/parent |
|
|
399
|
+
| `updateNodeData` | `{ id, data }` | Merge into node.data |
|
|
400
|
+
| `createEdge` | `{ edge: FlowEdge }` | Add an edge |
|
|
401
|
+
| `deleteEdge` | `{ id }` | Remove an edge |
|
|
402
|
+
| `deleteEntities` | `{ nodeIds?, edgeIds? }` | Low-level synchronous deletion of a known entity set |
|
|
403
|
+
| `updateEdge` | `{ id, patch? }` or `{ id, updater? }` | Patch/replace an edge without changing id/type/endpoints/ports |
|
|
404
|
+
| `updateEdgeData` | `{ id, data }` | Merge into edge.data |
|
|
405
|
+
| `deleteSelected` | — | Remove all selected nodes + edges |
|
|
406
|
+
| `setGraph` | `{ graph: FlowGraph }` | Replace entire graph, reset interaction state |
|
|
407
|
+
| `replaceGraph` | `{ graph, retention?, history?, origin?, transactionId? }` | Replace through the external synchronization policy used by `flow.replaceGraph()` |
|
|
408
|
+
| `reset` | — | Reset store to initial state (preserves viewport size) |
|
|
409
|
+
| `reparentNode` | `{ id, parentId }` | Move node to new parent |
|
|
410
|
+
| `collapseNode` | `{ id }` | Hide descendants and derive folded boundary edges |
|
|
411
|
+
| `expandNode` | `{ id }` | Show descendants again |
|
|
412
|
+
| `reconnectEdge` | `{ edgeId, newSourceId?, newTargetId?, newSourcePort?, newTargetPort? }` | Rewire edge endpoints |
|
|
346
413
|
|
|
347
414
|
### Drag lifecycle
|
|
348
415
|
|
|
349
|
-
| Event
|
|
350
|
-
|
|
351
|
-
| `dragStart`
|
|
352
|
-
| `dragMove`
|
|
353
|
-
| `dragEnd`
|
|
354
|
-
| `commitDrag` |
|
|
355
|
-
| `moveNodes`
|
|
416
|
+
| Event | Payload | Description |
|
|
417
|
+
| ------------ | ------------------------------- | --------------------------------------------------- |
|
|
418
|
+
| `dragStart` | `{ id }` | Begin dragging the selected entity |
|
|
419
|
+
| `dragMove` | `{ dx, dy }` | Apply in-progress drag delta |
|
|
420
|
+
| `dragEnd` | — | End drag gesture |
|
|
421
|
+
| `commitDrag` | `{ snapOffset?: Point }` | Bake `dx/dy` and the visual snap into positions |
|
|
422
|
+
| `moveNodes` | `{ direction: Point, factor? }` | Move selected nodes by arrow key |
|
|
356
423
|
|
|
357
424
|
### Selection & focus
|
|
358
425
|
|
|
359
|
-
| Event
|
|
360
|
-
|
|
361
|
-
| `select`
|
|
362
|
-
| `selectAll`
|
|
363
|
-
| `clearSelection` | —
|
|
364
|
-
| `focus`
|
|
365
|
-
| `clearFocus`
|
|
426
|
+
| Event | Payload | Description |
|
|
427
|
+
| ---------------- | ------------------------------------ | ------------------------------- |
|
|
428
|
+
| `select` | `{ ids, mode: 'single' \| 'multi' }` | Select entities (multi toggles) |
|
|
429
|
+
| `selectAll` | — | Select all nodes and edges |
|
|
430
|
+
| `clearSelection` | — | Deselect everything |
|
|
431
|
+
| `focus` | `{ entityId }` | Set keyboard/a11y focus |
|
|
432
|
+
| `clearFocus` | — | Clear focus |
|
|
366
433
|
|
|
367
434
|
### Box selection
|
|
368
435
|
|
|
369
|
-
| Event
|
|
370
|
-
|
|
371
|
-
| `selectionBoxStart` | `{ point, mode? }` | Begin box-select gesture
|
|
372
|
-
| `selectionBoxMove`
|
|
373
|
-
| `selectionBoxEnd`
|
|
436
|
+
| Event | Payload | Description |
|
|
437
|
+
| ------------------- | ------------------ | -------------------------------------- |
|
|
438
|
+
| `selectionBoxStart` | `{ point, mode? }` | Begin box-select gesture |
|
|
439
|
+
| `selectionBoxMove` | `{ point }` | Update box-select corner |
|
|
440
|
+
| `selectionBoxEnd` | — | Finalize: select intersecting entities |
|
|
374
441
|
|
|
375
442
|
### Viewport
|
|
376
443
|
|
|
377
|
-
| Event
|
|
378
|
-
|
|
379
|
-
| `setViewport`
|
|
380
|
-
| `setFitFrame`
|
|
381
|
-
| `panBy`
|
|
382
|
-
| `panStart`
|
|
383
|
-
| `pan`
|
|
384
|
-
| `panEnd`
|
|
385
|
-
| `zoomTo`
|
|
386
|
-
| `setMinZoom`
|
|
387
|
-
| `setMaxZoom`
|
|
388
|
-
| `updateViewportSize`
|
|
389
|
-
| `fitView`
|
|
390
|
-
| `pushViewport` / `popViewport` | —
|
|
391
|
-
| `setTranslateExtent`
|
|
392
|
-
| `setNodeExtent`
|
|
444
|
+
| Event | Payload | Description |
|
|
445
|
+
| ------------------------------ | ------------------------------------------------------------- | ------------------------------------------------------------- |
|
|
446
|
+
| `setViewport` | `{ x, y, zoom }` | Set viewport directly |
|
|
447
|
+
| `setFitFrame` | `{ frame: { x, y, width, height } \| null, preserveCenter? }` | Set or reset the persistent screen-space fit frame |
|
|
448
|
+
| `panBy` | `{ x, y }` | Pan by offset |
|
|
449
|
+
| `panStart` | `{ x, y }` | Begin pan gesture (screen coords) |
|
|
450
|
+
| `pan` | `{ x, y }` | Continue pan gesture (screen coords) |
|
|
451
|
+
| `panEnd` | — | End pan gesture |
|
|
452
|
+
| `zoomTo` | `{ zoom, center }` | Zoom to level around center point |
|
|
453
|
+
| `setMinZoom` | `{ minZoom }` | Set minimum zoom level |
|
|
454
|
+
| `setMaxZoom` | `{ maxZoom }` | Set maximum zoom level |
|
|
455
|
+
| `updateViewportSize` | `{ width, height, preserveCenter? }` | Report container size, optionally preserving its world center |
|
|
456
|
+
| `fitView` | `{ padding?, minZoom?, maxZoom?, nodes?, frame? }` | Fit all (or specific) nodes into a target frame |
|
|
457
|
+
| `pushViewport` / `popViewport` | — | Viewport stack for drill-in/out |
|
|
458
|
+
| `setTranslateExtent` | `{ extent: [Point, Point] \| null }` | Set pan boundary |
|
|
459
|
+
| `setNodeExtent` | `{ extent: [Point, Point] \| null }` | Set node drag boundary |
|
|
393
460
|
|
|
394
461
|
### Connection lifecycle
|
|
395
462
|
|
|
396
|
-
| Event
|
|
397
|
-
|
|
398
|
-
| `connectionStart`
|
|
399
|
-
| `connectionMove`
|
|
400
|
-
| `connectionEnd`
|
|
401
|
-
| `cancelConnection`
|
|
402
|
-
| `setConnectionClickStartHandle` | `{ nodeId, port } \| null`
|
|
463
|
+
| Event | Payload | Description |
|
|
464
|
+
| ------------------------------- | ---------------------------------------------------------------- | -------------------------------------------------------------- |
|
|
465
|
+
| `connectionStart` | `{ sourceId, sourcePort, position }` | Begin connection gesture |
|
|
466
|
+
| `connectionMove` | `{ position, targetId?, targetPort?, isValid?, invalidReason? }` | Update in-progress connection |
|
|
467
|
+
| `connectionEnd` | `{ target?: ConnectEndTarget }` | End connection gesture (emits `connectionEnded` with `status`) |
|
|
468
|
+
| `cancelConnection` | — | Cancel connection without emitting |
|
|
469
|
+
| `setConnectionClickStartHandle` | `{ nodeId, port } \| null` | Set/clear click-to-connect start handle |
|
|
403
470
|
|
|
404
471
|
### Resize lifecycle
|
|
405
472
|
|
|
406
|
-
| Event
|
|
407
|
-
|
|
408
|
-
| `resizeStart`
|
|
409
|
-
| `resizeMove`
|
|
410
|
-
| `resizeEnd`
|
|
411
|
-
| `resizeCancel` | —
|
|
473
|
+
| Event | Payload | Description |
|
|
474
|
+
| -------------- | ------------------------------ | ------------------------------------------------- |
|
|
475
|
+
| `resizeStart` | `{ id, handle: ResizeHandle }` | Begin resize gesture |
|
|
476
|
+
| `resizeMove` | `{ x, y }` | Update resize position |
|
|
477
|
+
| `resizeEnd` | — | Commit resize (bake position + custom dimensions) |
|
|
478
|
+
| `resizeCancel` | — | Cancel resize without committing |
|
|
412
479
|
|
|
413
480
|
### Layout
|
|
414
481
|
|
|
415
|
-
| Event
|
|
416
|
-
|
|
417
|
-
| `requestLayout`
|
|
418
|
-
| `applyLayoutResult`
|
|
419
|
-
| `updateMeasurements` | `{ measurements: Array<{ id, rect: Rect }> }`
|
|
420
|
-
| `bakeLabelPositions` | `{ positions: Map<edgeId, Point> }`
|
|
482
|
+
| Event | Payload | Description |
|
|
483
|
+
| -------------------- | ---------------------------------------------------- | -------------------------------------------- |
|
|
484
|
+
| `requestLayout` | `{ algorithm? }` | Request auto-layout (blocked during drag) |
|
|
485
|
+
| `applyLayoutResult` | `{ positions: Map<id, {x,y}>, edgeLabelPositions? }` | Apply layout positions, reset dx/dy |
|
|
486
|
+
| `updateMeasurements` | `{ measurements: Array<{ id, rect: Rect }> }` | Renderer reports measured world-space rects |
|
|
487
|
+
| `bakeLabelPositions` | `{ positions: Map<edgeId, Point> }` | Bake computed label positions into edge data |
|
|
421
488
|
|
|
422
489
|
### Configuration
|
|
423
490
|
|
|
424
|
-
| Event
|
|
425
|
-
|
|
426
|
-
| `setCanvasState`
|
|
427
|
-
| `setMode`
|
|
428
|
-
| `setDragThreshold`
|
|
429
|
-
| `setAutoPanOptions` | `{ autoPanOnDrag?, autoPanThreshold?, autoPanSpeed? }` | Configure auto-pan behavior
|
|
430
|
-
| `updateSettings`
|
|
431
|
-
| `setSnapToGrid`
|
|
432
|
-
| `setSnapLines`
|
|
433
|
-
| `setColorMode`
|
|
491
|
+
| Event | Payload | Description |
|
|
492
|
+
| ------------------- | ------------------------------------------------------ | --------------------------------------------------------------------- |
|
|
493
|
+
| `setCanvasState` | `{ state: CanvasState }` | Set low-level canvas gesture state |
|
|
494
|
+
| `setMode` | `{ mode: string }` | Set active profile mode |
|
|
495
|
+
| `setDragThreshold` | `{ dragThreshold }` | Set pixel threshold before drag starts |
|
|
496
|
+
| `setAutoPanOptions` | `{ autoPanOnDrag?, autoPanThreshold?, autoPanSpeed? }` | Configure auto-pan behavior |
|
|
497
|
+
| `updateSettings` | `Partial<FlowRuntimeSettings>` | Update runtime store settings in one event |
|
|
498
|
+
| `setSnapToGrid` | `{ enabled, grid? }` | Enable/disable grid snapping |
|
|
499
|
+
| `setSnapLines` | `{ enabled, threshold?, spacing? }` | Enable/disable Figma-style alignment guides |
|
|
500
|
+
| `setColorMode` | `{ colorMode: FlowColorMode }` | Set the preferred UI color mode (`'light'` \| `'dark'` \| `'system'`) |
|
|
434
501
|
|
|
435
502
|
### Navigation
|
|
436
503
|
|
|
437
|
-
| Event
|
|
438
|
-
|
|
439
|
-
| `updateParentNodeId` | `{ parentNodeId }`
|
|
440
|
-
| `setViewScope`
|
|
504
|
+
| Event | Payload | Description |
|
|
505
|
+
| -------------------- | ------------------------------------------------------ | ---------------------------------------------------------------- |
|
|
506
|
+
| `updateParentNodeId` | `{ parentNodeId }` | Drill into/out of nested node |
|
|
507
|
+
| `setViewScope` | `{ parentNodeId, depth, selection, visibleEntityIds }` | Low-level structural scope update; prefer `flow.view.setScope()` |
|
|
441
508
|
|
|
442
509
|
### Emitted events
|
|
443
510
|
|
|
444
511
|
Subscribe with `flow.on(type, listener)` or, for the low-level escape hatch, `flow.internals.store.on(type, listener)`.
|
|
445
512
|
|
|
446
|
-
| Event
|
|
447
|
-
|
|
448
|
-
| `graphChanged`
|
|
449
|
-
| `viewportChanged`
|
|
450
|
-
| `selectionChanged`
|
|
451
|
-
| `viewChanged`
|
|
452
|
-
| `entitiesDeleted`
|
|
453
|
-
| `connectionEnded`
|
|
454
|
-
| `dragStarted` / `dragMoved` / `dragEnded`
|
|
455
|
-
| `resizeStarted` / `resizeEnded`
|
|
456
|
-
| `connectStarted` / `connectMoved`
|
|
457
|
-
| `paneClicked` / `paneContextMenu`
|
|
458
|
-
| `entityClicked` / `entityDoubleClicked` / `entityContextMenu` | `FlowEntityGestureEvent` | Entity click, double-click, and context menu gestures
|
|
459
|
-
| `layoutRequested`
|
|
460
|
-
| `error`
|
|
513
|
+
| Event | Payload | Description |
|
|
514
|
+
| ------------------------------------------------------------- | ------------------------ | ------------------------------------------------------------------------------- |
|
|
515
|
+
| `graphChanged` | `{ graph, event }` | Graph changed after a mutation |
|
|
516
|
+
| `viewportChanged` | `{ viewport, event }` | Viewport changed |
|
|
517
|
+
| `selectionChanged` | `{ selection, event }` | Selection changed |
|
|
518
|
+
| `viewChanged` | `{ scope, event }` | Structural parent/depth changed |
|
|
519
|
+
| `entitiesDeleted` | `{ ids, event }` | Entities were deleted |
|
|
520
|
+
| `connectionEnded` | `ConnectionEndedEvent` | Connection gesture ended with `status: 'connected' \| 'cancelled' \| 'invalid'` |
|
|
521
|
+
| `dragStarted` / `dragMoved` / `dragEnded` | `FlowDrag*Event` | Drag gesture milestones; `dragMoved` fires per processed input event |
|
|
522
|
+
| `resizeStarted` / `resizeEnded` | `FlowResize*Event` | Resize gesture milestones |
|
|
523
|
+
| `connectStarted` / `connectMoved` | `FlowConnect*Event` | Connection gesture milestones; `connectMoved` fires per processed input event |
|
|
524
|
+
| `paneClicked` / `paneContextMenu` | `FlowPaneGestureEvent` | Pane click and context menu gestures |
|
|
525
|
+
| `entityClicked` / `entityDoubleClicked` / `entityContextMenu` | `FlowEntityGestureEvent` | Entity click, double-click, and context menu gestures |
|
|
526
|
+
| `layoutRequested` | `LayoutRequestedEvent` | Layout was requested |
|
|
527
|
+
| `error` | `{ error, event? }` | Flow error emitted |
|
|
461
528
|
|
|
462
529
|
## Undo / redo
|
|
463
530
|
|
|
@@ -466,10 +533,7 @@ Subscribe with `flow.on(type, listener)` or, for the low-level escape hatch, `fl
|
|
|
466
533
|
`@statelyai/flow` uses XState Store's `undoRedo()` extension with a whitelist of committed graph mutation events. Interaction-only events such as selection, panning, live drag updates, and connection hover state are intentionally skipped. Undo/redo restores graph and collapse history while preserving current viewport and valid session state. Cancelled drag/resize gestures create no history entry.
|
|
467
534
|
|
|
468
535
|
```ts
|
|
469
|
-
import {
|
|
470
|
-
createFlowStore,
|
|
471
|
-
createNode,
|
|
472
|
-
} from '@statelyai/flow';
|
|
536
|
+
import { createFlowStore, createNode } from '@statelyai/flow';
|
|
473
537
|
|
|
474
538
|
const store = createFlowStore(undefined, {
|
|
475
539
|
historyLimit: 200,
|
|
@@ -539,7 +603,9 @@ const fitted = viewportForBounds(contentRect, containerSize, {
|
|
|
539
603
|
const centered = viewportCenteredOn(canvasPoint, containerSize, 1, frame);
|
|
540
604
|
|
|
541
605
|
// Animation
|
|
542
|
-
const animation = animateViewport(store, fitted, {
|
|
606
|
+
const animation = animateViewport(store, fitted, {
|
|
607
|
+
lerp: 0.15,
|
|
608
|
+
});
|
|
543
609
|
await animation.done;
|
|
544
610
|
```
|
|
545
611
|
|
|
@@ -547,7 +613,9 @@ await animation.done;
|
|
|
547
613
|
|
|
548
614
|
<!-- edge routers from src/routing/index.ts -->
|
|
549
615
|
|
|
550
|
-
Four built-in routers. All return `PathData` (points + source/target direction). A custom router may also return `svgPath` to preserve rounded or otherwise custom SVG geometry; `points` remain required for bounds and endpoint handles, and exported `pathToSVG` safely prefers a finite,
|
|
616
|
+
Four built-in routers. All return `PathData` (points + source/target direction). A custom router may also return `svgPath` to preserve rounded or otherwise custom SVG geometry; `points` remain required for bounds and endpoint handles, and exported `pathToSVG` safely prefers a finite override containing only SVG path commands and numeric parameters. Canonical graph `edge.points` are different from `waypoints`: they are a complete authoritative endpoint-to-endpoint route, interpreted by `edge.routing` (`polyline`, `orthogonal`, or chained `splines`) and never sent through a Flow router or split around a label. An optional edge `svgPath` preserves exact SVG paint while the authoritative points drive endpoints, bounds, hit testing, culling, transient DOM updates, and export. Core endpoint-geometry mutations invalidate precomputed route fields; layout-provided edge routes replace them with router waypoints, while consumers can apply a fresh authoritative graph to restore exact geometry. Edge `waypoints` remain ordered interior router inputs passed through `EdgeRouterContext`; built-in routers honor them. Custom `EdgeRouter`s receive the full typed edge plus the exact canonical or projection `graph` currently being routed and a detached, read-only `getEntityBounds(id)` lookup for live node/edge-label geometry. The lookup never routes another edge. Use `configureBezierRouter(options)`, `configureStepRouter(options)`, and `configureAvoidRouter(options)` for customized built-in router variants. `avoidRouter` is opt-in and routes orthogonal polylines around sibling node obstacles.
|
|
617
|
+
|
|
618
|
+
Routers must be deterministic from their supplied inputs. Flow reuses paths while routing geometry and router identity are unchanged.
|
|
551
619
|
|
|
552
620
|
```ts
|
|
553
621
|
import { configureStepRouter, type EdgeRouter } from '@statelyai/flow';
|
|
@@ -566,8 +634,8 @@ const statechartRouter: EdgeRouter<StatechartEdgeData> = (edge, context) => {
|
|
|
566
634
|
edge.data.transitionType === 'internal'
|
|
567
635
|
? 32
|
|
568
636
|
: siblingBounds.length
|
|
569
|
-
|
|
570
|
-
|
|
637
|
+
? 64
|
|
638
|
+
: 48;
|
|
571
639
|
return configureStepRouter({ offset })(edge, context);
|
|
572
640
|
};
|
|
573
641
|
```
|
|
@@ -584,8 +652,12 @@ Shape handlers own hit testing and edge anchor calculation. Register custom shap
|
|
|
584
652
|
import { registerShape, getShapeOrDefault } from '@statelyai/flow';
|
|
585
653
|
|
|
586
654
|
registerShape('hexagon', {
|
|
587
|
-
hitTest: (point, bounds) => {
|
|
588
|
-
|
|
655
|
+
hitTest: (point, bounds) => {
|
|
656
|
+
/* point-in-hexagon */
|
|
657
|
+
},
|
|
658
|
+
edgeAnchor: (bounds, direction) => {
|
|
659
|
+
/* anchor point */
|
|
660
|
+
},
|
|
589
661
|
});
|
|
590
662
|
|
|
591
663
|
// Built-in: 'rect', 'rounded', 'ellipse', 'diamond'
|
|
@@ -600,7 +672,12 @@ Figma-style alignment guides for drag operations. Exported:
|
|
|
600
672
|
|
|
601
673
|
```ts
|
|
602
674
|
import { getSnapLines } from '@statelyai/flow';
|
|
603
|
-
import type {
|
|
675
|
+
import type {
|
|
676
|
+
SnapLine,
|
|
677
|
+
SpacingGuide,
|
|
678
|
+
SnapResult,
|
|
679
|
+
SnapOptions,
|
|
680
|
+
} from '@statelyai/flow';
|
|
604
681
|
|
|
605
682
|
const result: SnapResult = getSnapLines(movingRect, otherRects, {
|
|
606
683
|
threshold: 5,
|
|
@@ -620,7 +697,11 @@ Enable via store: `store.trigger.setSnapLines({ enabled: true })`.
|
|
|
620
697
|
Behavior is controlled by predicate functions, not boolean flags:
|
|
621
698
|
|
|
622
699
|
```ts
|
|
623
|
-
import {
|
|
700
|
+
import {
|
|
701
|
+
defaultConfig,
|
|
702
|
+
readonlyConfig,
|
|
703
|
+
resolveBehavior,
|
|
704
|
+
} from '@statelyai/flow';
|
|
624
705
|
|
|
625
706
|
// Everything interactive
|
|
626
707
|
defaultConfig; // { selectable: () => true, draggable: () => true, connectable: () => true, ... }
|
|
@@ -646,9 +727,9 @@ const canDrag = resolveBehavior(config.draggable, someNode, flowState);
|
|
|
646
727
|
const store = createFlowStore(graph, {
|
|
647
728
|
minZoom: 0.1,
|
|
648
729
|
maxZoom: 4,
|
|
649
|
-
translateExtent: null,
|
|
650
|
-
nodeExtent: null,
|
|
651
|
-
dragThreshold: 3,
|
|
730
|
+
translateExtent: null, // [Point, Point] | null — pan boundary
|
|
731
|
+
nodeExtent: null, // [Point, Point] | null — node drag boundary
|
|
732
|
+
dragThreshold: 3, // px before drag starts
|
|
652
733
|
autoPanOnDrag: true,
|
|
653
734
|
autoPanThreshold: 40,
|
|
654
735
|
autoPanSpeed: 15,
|
|
@@ -659,7 +740,7 @@ const store = createFlowStore(graph, {
|
|
|
659
740
|
enableSpacingGuides: false,
|
|
660
741
|
connectOnClick: false,
|
|
661
742
|
labelAnchor: 'fixed',
|
|
662
|
-
boxSelectionTest: null,
|
|
743
|
+
boxSelectionTest: null, // custom node / edge-label bounds predicate
|
|
663
744
|
selectionMode: 'partial', // 'partial' | 'full'
|
|
664
745
|
historyLimit: 100,
|
|
665
746
|
mode: 'interactive',
|
|
@@ -668,6 +749,13 @@ const store = createFlowStore(graph, {
|
|
|
668
749
|
});
|
|
669
750
|
```
|
|
670
751
|
|
|
752
|
+
<!-- box-selection behavior from src/store/index.ts -->
|
|
753
|
+
|
|
754
|
+
Box selection tests node bounds and rendered edge-label bounds only. Crossing
|
|
755
|
+
an edge path does not select it, and edges without labels are not box-selected.
|
|
756
|
+
As a growing box captures entities, selection order records when each entity
|
|
757
|
+
first enters the box.
|
|
758
|
+
|
|
671
759
|
Use `flow.configure(patch)` for partial live updates. Use `flow.configure(current => completeNext)` for explicit full replacement and `flow.getConfiguration()` to capture a baseline. Store, engine, profile, mode, edge defaults, connection creation, and layout configuration share this path. Changing `historyLimit` after creation throws until XState exposes a supported live history-limit API.
|
|
672
760
|
|
|
673
761
|
`FlowProfile.resolvers`, `FlowProfile.policies`, and `FlowProfile.rules` are exported but experimental until the profile API settles.
|
|
@@ -695,11 +783,16 @@ store.trigger.createEdge({
|
|
|
695
783
|
id: 'e1',
|
|
696
784
|
sourceId: 'a',
|
|
697
785
|
targetId: 'b',
|
|
698
|
-
x: 0,
|
|
786
|
+
x: 0,
|
|
787
|
+
y: 0,
|
|
788
|
+
width: 0,
|
|
789
|
+
height: 0,
|
|
699
790
|
data: {},
|
|
700
791
|
label: {
|
|
701
|
-
x: 150,
|
|
702
|
-
|
|
792
|
+
x: 150,
|
|
793
|
+
y: 100,
|
|
794
|
+
width: 80,
|
|
795
|
+
height: 30,
|
|
703
796
|
autoSize: true, // optional: rendered DOM owns live width/height
|
|
704
797
|
data: { text: 'yes' },
|
|
705
798
|
},
|
|
@@ -736,6 +829,7 @@ renderer adapter (React, Vue, …) ← UI: components, DOM/SVG, event binding
|
|
|
736
829
|
**Design principle: the engine is a pure projection over the store.** DOM-specific concerns such as element measurement and container observation are intentionally out of this package. All interaction state (pan, drag, selection, connection) lives in the store and is driven by `store.trigger.*` events. The engine provides computed queries (edge paths, visual bounds, coordinate conversion, hit testing) derived from the current store snapshot.
|
|
737
830
|
|
|
738
831
|
This means:
|
|
832
|
+
|
|
739
833
|
- All state transitions are testable via `store.trigger.*` + snapshot assertions
|
|
740
834
|
- The store is the single source of truth
|
|
741
835
|
- The engine can be reconstructed from any store snapshot
|
|
@@ -753,8 +847,8 @@ store.trigger.dragEnd();
|
|
|
753
847
|
store.trigger.zoomTo({ zoom: 1.5, center: { x: 400, y: 300 } });
|
|
754
848
|
|
|
755
849
|
// Engine is purely derived:
|
|
756
|
-
const bounds = engine.getBounds('a');
|
|
757
|
-
const paths = engine.getAllEdgePaths();
|
|
850
|
+
const bounds = engine.getBounds('a'); // measured or model bounds
|
|
851
|
+
const paths = engine.getAllEdgePaths(); // computed from graph state
|
|
758
852
|
const canvas = engine.screenToCanvas({ x, y }); // from current viewport
|
|
759
853
|
```
|
|
760
854
|
|