@statelyai/flow 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +481 -10
- package/dist/esm/index.d.ts +1285 -0
- package/dist/esm/index.js +2534 -0
- package/package.json +46 -13
- package/.vscode/launch.json +0 -34
- package/LICENSE +0 -21
- package/src/createFlow.test.ts +0 -204
- package/src/index.ts +0 -62
package/README.md
CHANGED
|
@@ -1,15 +1,486 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @statelyai/flow
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Framework-agnostic flow visualization engine. Built on [`@statelyai/graph`](https://github.com/statelyai/graph) VisualGraph + [`@xstate/store`](https://github.com/statelyai/xstate) + [Immer](https://immerjs.github.io/immer/).
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Render adapters (React, Vue, tldraw, etc.) sit on top. `@statelyai/flow` owns the pure state, layout, routing, and interaction logic. DOM-specific helpers such as `ResizeObserver` integration live in `@statelyai/flow-dom`.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
## Install
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
<!-- install command matching package.json#name and peerDependencies -->
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
```bash
|
|
12
|
+
npm install @statelyai/flow @statelyai/graph @xstate/store
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
`@statelyai/graph` is a peer dependency — you bring your own version.
|
|
16
|
+
|
|
17
|
+
If you want DOM helpers such as `attachContainer()` and `createMeasurementObserver()`, also install `@statelyai/flow-dom`.
|
|
18
|
+
|
|
19
|
+
## Quick start
|
|
20
|
+
|
|
21
|
+
<!-- primary controller quick start matching src/createFlow.ts and src/index.ts exports -->
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { createFlow } from '@statelyai/flow';
|
|
25
|
+
|
|
26
|
+
const flow = createFlow({
|
|
27
|
+
graph: {
|
|
28
|
+
direction: 'down',
|
|
29
|
+
nodes: [
|
|
30
|
+
{ type: 'node', id: 'a', x: 0, y: 0, width: 200, height: 80, data: { label: 'Start' } },
|
|
31
|
+
{ type: 'node', id: 'b', x: 0, y: 150, width: 200, height: 80, data: { label: 'End' } },
|
|
32
|
+
],
|
|
33
|
+
edges: [
|
|
34
|
+
{ type: 'edge', id: 'e1', sourceId: 'a', targetId: 'b', x: 0, y: 0, width: 0, height: 0, data: {} },
|
|
35
|
+
],
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// Read state
|
|
40
|
+
const { graph, viewport, selection } = flow.getContext();
|
|
41
|
+
|
|
42
|
+
// Trigger events
|
|
43
|
+
flow.trigger.select({ ids: ['a'], mode: 'single' });
|
|
44
|
+
flow.trigger.panBy({ x: 100, y: 0 });
|
|
45
|
+
flow.trigger.dragStart({ id: 'a' });
|
|
46
|
+
flow.trigger.dragMove({ dx: 10, dy: 20 });
|
|
47
|
+
flow.trigger.dragEnd();
|
|
48
|
+
flow.trigger.commitDrag();
|
|
49
|
+
|
|
50
|
+
// Query derived state through the engine-backed controller
|
|
51
|
+
const selectedBounds = flow.getSelectionBounds();
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
`createFlow()` owns the store and engine together. Use `createFlowStore()` and
|
|
55
|
+
`createFlowEngine()` directly when you need lower-level control.
|
|
56
|
+
|
|
57
|
+
## Data model
|
|
58
|
+
|
|
59
|
+
The canonical data model is `VisualGraph` from `@statelyai/graph`. Flow extends it with:
|
|
60
|
+
|
|
61
|
+
<!-- flow entity types from src/types.ts -->
|
|
62
|
+
|
|
63
|
+
- **`FlowNode`** — `VisualNode` + `{ dx, dy, style, ports, hidden, selectable, draggable, connectable, deletable, resizable }` for drag offsets, style overrides, and per-entity behavior
|
|
64
|
+
- **`FlowEdge`** — `VisualEdge` + `{ label, sourcePort, targetPort, kind, style, labelAnchor }` for edge labels, port binding, and edge styling
|
|
65
|
+
- **`FlowGraph`** — `VisualGraph` with `FlowNode[]` and `FlowEdge[]`
|
|
66
|
+
|
|
67
|
+
Positions use a two-layer system: `x/y` from layout, `dx/dy` from user drag. Visual position = `(x + dx, y + dy)`.
|
|
68
|
+
|
|
69
|
+
## Ports
|
|
70
|
+
|
|
71
|
+
Ports are based on the visual node port model from `@statelyai/graph`.
|
|
72
|
+
|
|
73
|
+
- `FlowNode` inherits `ports` from `VisualNode`
|
|
74
|
+
- `FlowPort` is `VisualPort` plus flow's `side` hint
|
|
75
|
+
- `sourcePort` / `targetPort` on `FlowEdge` reference those port names
|
|
76
|
+
- `'*'` means "choose the closest available port"
|
|
77
|
+
- `null` means "use the node shape anchor"
|
|
78
|
+
|
|
79
|
+
At runtime, flow expects port rectangles to be available for routing and hit geometry: `x`, `y`, `width`, `height` relative to the node origin. In practice this means the port objects in flow are graph visual ports, with one extra flow-specific field: `side`.
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
const graph = {
|
|
83
|
+
nodes: [
|
|
84
|
+
{
|
|
85
|
+
type: 'node',
|
|
86
|
+
id: 'a',
|
|
87
|
+
x: 0,
|
|
88
|
+
y: 0,
|
|
89
|
+
width: 160,
|
|
90
|
+
height: 60,
|
|
91
|
+
ports: [
|
|
92
|
+
{ name: 'out', side: 'right', direction: 'out', x: 150, y: 25, width: 10, height: 10 },
|
|
93
|
+
],
|
|
94
|
+
data: {},
|
|
95
|
+
},
|
|
96
|
+
],
|
|
97
|
+
edges: [
|
|
98
|
+
{
|
|
99
|
+
type: 'edge',
|
|
100
|
+
id: 'e1',
|
|
101
|
+
sourceId: 'a',
|
|
102
|
+
targetId: 'b',
|
|
103
|
+
sourcePort: 'out',
|
|
104
|
+
targetPort: '*',
|
|
105
|
+
x: 0,
|
|
106
|
+
y: 0,
|
|
107
|
+
width: 0,
|
|
108
|
+
height: 0,
|
|
109
|
+
data: {},
|
|
110
|
+
},
|
|
111
|
+
],
|
|
112
|
+
};
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Store events
|
|
116
|
+
|
|
117
|
+
<!-- all store events from src/store/index.ts -->
|
|
118
|
+
|
|
119
|
+
### Graph mutations
|
|
120
|
+
|
|
121
|
+
| Event | Payload | Description |
|
|
122
|
+
|---|---|---|
|
|
123
|
+
| `createNode` | `{ node: FlowNode }` | Add a node |
|
|
124
|
+
| `deleteNode` | `{ id }` | Remove node + cascade edges |
|
|
125
|
+
| `updateNodeData` | `{ id, data }` | Merge into node.data |
|
|
126
|
+
| `createEdge` | `{ edge: FlowEdge }` | Add an edge |
|
|
127
|
+
| `deleteEdge` | `{ id }` | Remove an edge |
|
|
128
|
+
| `updateEdgeData` | `{ id, data }` | Merge into edge.data |
|
|
129
|
+
| `deleteSelected` | — | Remove all selected nodes + edges |
|
|
130
|
+
| `setGraph` | `{ graph: FlowGraph }` | Replace entire graph, reset interaction state |
|
|
131
|
+
| `reset` | — | Reset store to initial state (preserves viewport size) |
|
|
132
|
+
| `reparentNode` | `{ id, parentId }` | Move node to new parent |
|
|
133
|
+
| `reconnectEdge` | `{ edgeId, newSourceId?, newTargetId?, newSourcePort?, newTargetPort? }` | Rewire edge endpoints |
|
|
134
|
+
|
|
135
|
+
### Drag lifecycle
|
|
136
|
+
|
|
137
|
+
| Event | Payload | Description |
|
|
138
|
+
|---|---|---|
|
|
139
|
+
| `dragStart` | `{ id }` | Begin dragging the selected entity |
|
|
140
|
+
| `dragMove` | `{ dx, dy }` | Apply in-progress drag delta |
|
|
141
|
+
| `dragEnd` | — | End drag gesture |
|
|
142
|
+
| `commitDrag` | — | Bake `dx/dy` into committed positions |
|
|
143
|
+
| `moveNodes` | `{ direction: Point, factor? }` | Move selected nodes by arrow key |
|
|
144
|
+
|
|
145
|
+
### Selection & focus
|
|
146
|
+
|
|
147
|
+
| Event | Payload | Description |
|
|
148
|
+
|---|---|---|
|
|
149
|
+
| `select` | `{ ids, mode: 'single' \| 'multi' }` | Select entities (multi toggles) |
|
|
150
|
+
| `selectAll` | — | Select all nodes and edges |
|
|
151
|
+
| `clearSelection` | — | Deselect everything |
|
|
152
|
+
| `focus` | `{ entityId }` | Set keyboard/a11y focus |
|
|
153
|
+
| `clearFocus` | — | Clear focus |
|
|
154
|
+
|
|
155
|
+
### Box selection
|
|
156
|
+
|
|
157
|
+
| Event | Payload | Description |
|
|
158
|
+
|---|---|---|
|
|
159
|
+
| `selectionBoxStart` | `{ point, mode? }` | Begin box-select gesture |
|
|
160
|
+
| `selectionBoxMove` | `{ point }` | Update box-select corner |
|
|
161
|
+
| `selectionBoxEnd` | — | Finalize: select intersecting entities |
|
|
162
|
+
|
|
163
|
+
### Viewport
|
|
164
|
+
|
|
165
|
+
| Event | Payload | Description |
|
|
166
|
+
|---|---|---|
|
|
167
|
+
| `setViewport` | `{ x, y, zoom }` | Set viewport directly |
|
|
168
|
+
| `panBy` | `{ x, y }` | Pan by offset |
|
|
169
|
+
| `panStart` | `{ x, y }` | Begin pan gesture (screen coords) |
|
|
170
|
+
| `pan` | `{ x, y }` | Continue pan gesture (screen coords) |
|
|
171
|
+
| `panEnd` | — | End pan gesture |
|
|
172
|
+
| `zoomTo` | `{ zoom, center }` | Zoom to level around center point |
|
|
173
|
+
| `setMinZoom` | `{ minZoom }` | Set minimum zoom level |
|
|
174
|
+
| `setMaxZoom` | `{ maxZoom }` | Set maximum zoom level |
|
|
175
|
+
| `updateViewportSize` | `{ width, height }` | Report container size |
|
|
176
|
+
| `fitView` | `{ padding?, minZoom?, maxZoom?, nodes? }` | Fit all (or specific) nodes in view |
|
|
177
|
+
| `pushViewport` / `popViewport` | — | Viewport stack for drill-in/out |
|
|
178
|
+
| `setTranslateExtent` | `{ extent: [Point, Point] \| null }` | Set pan boundary |
|
|
179
|
+
| `setNodeExtent` | `{ extent: [Point, Point] \| null }` | Set node drag boundary |
|
|
180
|
+
|
|
181
|
+
### Connection lifecycle
|
|
182
|
+
|
|
183
|
+
| Event | Payload | Description |
|
|
184
|
+
|---|---|---|
|
|
185
|
+
| `connectionStart` | `{ sourceId, sourcePort, position }` | Begin connection gesture |
|
|
186
|
+
| `connectionMove` | `{ position, targetId?, targetPort?, isValid? }` | Update in-progress connection |
|
|
187
|
+
| `connectionEnd` | `{ target?: ConnectEndTarget }` | End connection gesture (emits `connectionEnded`) |
|
|
188
|
+
| `cancelConnection` | — | Cancel connection without emitting |
|
|
189
|
+
| `setConnectionClickStartHandle` | `{ nodeId, port } \| null` | Set/clear click-to-connect start handle |
|
|
190
|
+
|
|
191
|
+
### Resize lifecycle
|
|
192
|
+
|
|
193
|
+
| Event | Payload | Description |
|
|
194
|
+
|---|---|---|
|
|
195
|
+
| `resizeStart` | `{ id, handle: ResizeHandle }` | Begin resize gesture |
|
|
196
|
+
| `resizeMove` | `{ x, y }` | Update resize position |
|
|
197
|
+
| `resizeEnd` | — | Commit resize (bake position + custom dimensions) |
|
|
198
|
+
| `resizeCancel` | — | Cancel resize without committing |
|
|
199
|
+
|
|
200
|
+
### Layout
|
|
201
|
+
|
|
202
|
+
| Event | Payload | Description |
|
|
203
|
+
|---|---|---|
|
|
204
|
+
| `requestLayout` | `{ algorithm? }` | Request auto-layout (blocked during drag) |
|
|
205
|
+
| `applyLayoutResult` | `{ positions: Map<id, {x,y}>, edgeLabelPositions? }` | Apply layout positions, reset dx/dy |
|
|
206
|
+
| `reportMeasurements` | `{ measurements: Map<id, {w,h}> }` | Renderer reports measured sizes |
|
|
207
|
+
| `reportPortMeasurements` | `{ measurements: Array<{nodeId, portName, x, y, width, height}> }` | Renderer reports port positions |
|
|
208
|
+
| `bakeLabelPositions` | `{ positions: Map<edgeId, Point> }` | Bake computed label positions into edge data |
|
|
209
|
+
|
|
210
|
+
### Configuration
|
|
211
|
+
|
|
212
|
+
| Event | Payload | Description |
|
|
213
|
+
|---|---|---|
|
|
214
|
+
| `setCanvasState` | `{ state: CanvasState }` | Set interaction mode |
|
|
215
|
+
| `setInteractionMode` | `{ mode: InteractionMode }` | Set pointer gesture mode (`'default'` \| `'pan'` \| `'connect'` \| `'select'`) |
|
|
216
|
+
| `setDragThreshold` | `{ dragThreshold }` | Set pixel threshold before drag starts |
|
|
217
|
+
| `setAutoPanOptions` | `{ autoPanOnDrag?, autoPanThreshold?, autoPanSpeed? }` | Configure auto-pan behavior |
|
|
218
|
+
| `setSnapToGrid` | `{ enabled, grid? }` | Enable/disable grid snapping |
|
|
219
|
+
| `setSnapLines` | `{ enabled, threshold?, spacing? }` | Enable/disable Figma-style alignment guides |
|
|
220
|
+
|
|
221
|
+
### Navigation
|
|
222
|
+
|
|
223
|
+
| Event | Payload | Description |
|
|
224
|
+
|---|---|---|
|
|
225
|
+
| `updateParentNodeId` | `{ parentNodeId }` | Drill into/out of nested node |
|
|
226
|
+
|
|
227
|
+
## Undo / redo
|
|
228
|
+
|
|
229
|
+
<!-- undo/redo config from src/store/index.ts UNDOABLE_EVENT_TYPES -->
|
|
230
|
+
|
|
231
|
+
`@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.
|
|
232
|
+
|
|
233
|
+
```ts
|
|
234
|
+
import {
|
|
235
|
+
createFlowStore,
|
|
236
|
+
createNode,
|
|
237
|
+
} from '@statelyai/flow';
|
|
238
|
+
|
|
239
|
+
const store = createFlowStore(undefined, {
|
|
240
|
+
historyLimit: 200,
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
store.trigger.createNode({
|
|
244
|
+
node: createNode({ id: 'a', x: 0, y: 0 }),
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
store.trigger.undo();
|
|
248
|
+
store.trigger.redo();
|
|
249
|
+
|
|
250
|
+
const transactionId = 'create-node-pair';
|
|
251
|
+
store.trigger.createNode({
|
|
252
|
+
node: createNode({ id: 'b', x: 0, y: 100 }),
|
|
253
|
+
transactionId,
|
|
254
|
+
});
|
|
255
|
+
store.trigger.createNode({
|
|
256
|
+
node: createNode({ id: 'c', x: 180, y: 100 }),
|
|
257
|
+
transactionId,
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
// A single undo reverts both node creations.
|
|
261
|
+
store.trigger.undo();
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
Tracked events: `createNode`, `deleteNode`, `updateNodeData`, `createEdge`, `deleteEdge`, `updateEdgeData`, `deleteSelected`, `reparentNode`, `commitDrag`, `resizeEnd`, `reconnectEdge`, `applyLayoutResult`, `bakeLabelPositions`.
|
|
265
|
+
|
|
266
|
+
## Viewport utilities
|
|
267
|
+
|
|
268
|
+
<!-- viewport functions exported from src/viewport/index.ts -->
|
|
269
|
+
|
|
270
|
+
```ts
|
|
271
|
+
import {
|
|
272
|
+
createViewport,
|
|
273
|
+
toSVGTransform,
|
|
274
|
+
toViewBox,
|
|
275
|
+
screenToCanvas,
|
|
276
|
+
canvasToScreen,
|
|
277
|
+
getVisibleBounds,
|
|
278
|
+
viewportForBounds,
|
|
279
|
+
panBy,
|
|
280
|
+
zoomTo,
|
|
281
|
+
clampZoom,
|
|
282
|
+
} from '@statelyai/flow';
|
|
283
|
+
|
|
284
|
+
const vp = createViewport(); // { x: 0, y: 0, zoom: 1 }
|
|
285
|
+
|
|
286
|
+
// SVG rendering
|
|
287
|
+
const transform = toSVGTransform(vp); // "translate(0, 0) scale(1)"
|
|
288
|
+
const viewBox = toViewBox(vp, { width: 800, height: 600 });
|
|
289
|
+
|
|
290
|
+
// Coordinate conversion
|
|
291
|
+
const canvasPoint = screenToCanvas(vp, { x: 400, y: 300 });
|
|
292
|
+
const screenPoint = canvasToScreen(vp, { x: 100, y: 50 });
|
|
293
|
+
|
|
294
|
+
// Fit content
|
|
295
|
+
const bounds = getVisibleBounds(vp, { width: 800, height: 600 });
|
|
296
|
+
const fitted = viewportForBounds(contentRect, containerSize, { padding: 0.1 });
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
## Edge routing
|
|
300
|
+
|
|
301
|
+
<!-- edge routers from src/routing/index.ts -->
|
|
302
|
+
|
|
303
|
+
Three built-in routers. All return `PathData` (points + source/target direction).
|
|
304
|
+
|
|
305
|
+
```ts
|
|
306
|
+
import { straightRouter, stepRouter, bezierRouter, pathToSVG } from '@statelyai/flow';
|
|
307
|
+
|
|
308
|
+
const path = stepRouter({
|
|
309
|
+
sourceRect: { x: 0, y: 0, width: 200, height: 80 },
|
|
310
|
+
targetRect: { x: 300, y: 200, width: 200, height: 80 },
|
|
311
|
+
sourceDirection: 'bottom',
|
|
312
|
+
targetDirection: 'top',
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
const d = pathToSVG(path); // SVG path d attribute
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
When an edge uses ports, routing prefers the explicit port position. When no port is specified, routing uses the registered shape anchor for the node.
|
|
319
|
+
|
|
320
|
+
## Shapes
|
|
321
|
+
|
|
322
|
+
<!-- shape registry from src/shapes/index.ts -->
|
|
323
|
+
|
|
324
|
+
Shape handlers own hit testing and edge anchor calculation. Register custom shapes:
|
|
325
|
+
|
|
326
|
+
```ts
|
|
327
|
+
import { registerShape, getShapeOrDefault } from '@statelyai/flow';
|
|
328
|
+
|
|
329
|
+
registerShape('hexagon', {
|
|
330
|
+
hitTest: (point, bounds) => { /* point-in-hexagon */ },
|
|
331
|
+
edgeAnchor: (bounds, direction) => { /* anchor point */ },
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
// Built-in: 'rect', 'rounded', 'ellipse', 'diamond'
|
|
335
|
+
const shape = getShapeOrDefault('hexagon');
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
## Snap lines
|
|
339
|
+
|
|
340
|
+
<!-- snap module exports from src/snap.ts -->
|
|
341
|
+
|
|
342
|
+
Figma-style alignment guides for drag operations. Exported:
|
|
343
|
+
|
|
344
|
+
```ts
|
|
345
|
+
import { getSnapLines } from '@statelyai/flow';
|
|
346
|
+
import type { SnapLine, SpacingGuide, SnapResult, SnapOptions } from '@statelyai/flow';
|
|
347
|
+
|
|
348
|
+
const result: SnapResult = getSnapLines(movingRect, otherRects, {
|
|
349
|
+
threshold: 5,
|
|
350
|
+
enableSpacing: true,
|
|
351
|
+
});
|
|
352
|
+
// result.snapLines — alignment lines
|
|
353
|
+
// result.snapRect — snapped rectangle position
|
|
354
|
+
// result.spacingGuides — equal spacing indicators
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
Enable via store: `store.trigger.setSnapLines({ enabled: true })`.
|
|
358
|
+
|
|
359
|
+
## Config & behavior
|
|
360
|
+
|
|
361
|
+
<!-- behavior config from src/config.ts -->
|
|
362
|
+
|
|
363
|
+
Behavior is controlled by predicate functions, not boolean flags:
|
|
364
|
+
|
|
365
|
+
```ts
|
|
366
|
+
import { defaultConfig, readonlyConfig, resolveBehavior } from '@statelyai/flow';
|
|
367
|
+
|
|
368
|
+
// Everything interactive
|
|
369
|
+
defaultConfig; // { selectable: () => true, draggable: () => true, connectable: () => true, ... }
|
|
370
|
+
|
|
371
|
+
// View-only (can still select)
|
|
372
|
+
readonlyConfig; // { draggable: false, connectable: false, reconnectable: false, deletable: false, resizable: false }
|
|
373
|
+
|
|
374
|
+
// Custom: nodes draggable, edges not
|
|
375
|
+
const config = {
|
|
376
|
+
...defaultConfig,
|
|
377
|
+
draggable: (entity) => entity.type === 'node',
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
// Resolve for a specific entity
|
|
381
|
+
const canDrag = resolveBehavior(config.draggable, someNode, flowState);
|
|
382
|
+
```
|
|
383
|
+
|
|
384
|
+
## Store options
|
|
385
|
+
|
|
386
|
+
<!-- FlowStoreOptions from src/store/index.ts -->
|
|
387
|
+
|
|
388
|
+
```ts
|
|
389
|
+
const store = createFlowStore(graph, {
|
|
390
|
+
minZoom: 0.1,
|
|
391
|
+
maxZoom: 4,
|
|
392
|
+
translateExtent: null, // [Point, Point] | null — pan boundary
|
|
393
|
+
nodeExtent: null, // [Point, Point] | null — node drag boundary
|
|
394
|
+
dragThreshold: 3, // px before drag starts
|
|
395
|
+
autoPanOnDrag: true,
|
|
396
|
+
autoPanThreshold: 40,
|
|
397
|
+
autoPanSpeed: 15,
|
|
398
|
+
snapToGrid: false,
|
|
399
|
+
snapGrid: [10, 10],
|
|
400
|
+
enableSnapLines: false,
|
|
401
|
+
snapThreshold: 5,
|
|
402
|
+
enableSpacingGuides: false,
|
|
403
|
+
connectOnClick: false,
|
|
404
|
+
selectionMode: 'partial', // 'partial' | 'full'
|
|
405
|
+
historyLimit: 100,
|
|
406
|
+
beforeDelete: ({ graph, reason, nodes, edges }) => { /* return false to cancel */ },
|
|
407
|
+
onDeleteNodes: 'cascade', // 'cascade' | 'retarget' | custom function
|
|
408
|
+
});
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
## Edge labels
|
|
412
|
+
|
|
413
|
+
Labels are first-class citizens — part of the edge data, positioned like nodes:
|
|
414
|
+
|
|
415
|
+
```ts
|
|
416
|
+
store.trigger.createEdge({
|
|
417
|
+
edge: {
|
|
418
|
+
type: 'edge',
|
|
419
|
+
id: 'e1',
|
|
420
|
+
sourceId: 'a',
|
|
421
|
+
targetId: 'b',
|
|
422
|
+
x: 0, y: 0, width: 0, height: 0,
|
|
423
|
+
data: {},
|
|
424
|
+
label: {
|
|
425
|
+
x: 150, y: 100,
|
|
426
|
+
width: 80, height: 30,
|
|
427
|
+
data: { text: 'yes' },
|
|
428
|
+
},
|
|
429
|
+
},
|
|
430
|
+
});
|
|
431
|
+
```
|
|
432
|
+
|
|
433
|
+
Selecting a label selects its edge (same ID). The renderer draws two path segments: source → label, label → target.
|
|
434
|
+
|
|
435
|
+
## Factory functions
|
|
436
|
+
|
|
437
|
+
<!-- factory functions from src/factories.ts -->
|
|
438
|
+
|
|
439
|
+
```ts
|
|
440
|
+
import { createNode, createEdge, createGraph } from '@statelyai/flow';
|
|
441
|
+
|
|
442
|
+
const node = createNode({ id: 'a', x: 0, y: 0 }); // defaults: width=150, height=50
|
|
443
|
+
const edge = createEdge({ id: 'e1', sourceId: 'a', targetId: 'b' });
|
|
444
|
+
const graph = createGraph({ nodes: [node], edges: [edge] }); // defaults: direction='down'
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
## Architecture
|
|
448
|
+
|
|
449
|
+
```
|
|
450
|
+
@statelyai/graph (VisualGraph) ← canonical data model
|
|
451
|
+
↓
|
|
452
|
+
FlowStore (@xstate/store) ← all state + transitions (pure, testable)
|
|
453
|
+
↓
|
|
454
|
+
FlowEngine ← purely derived queries (no mutable state)
|
|
455
|
+
↓
|
|
456
|
+
renderer adapter (React, Vue, …) ← UI: components, DOM/SVG, event binding
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
**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, resolved bounds, coordinate conversion, hit testing) derived from the current store snapshot.
|
|
460
|
+
|
|
461
|
+
This means:
|
|
462
|
+
- All state transitions are testable via `store.trigger.*` + snapshot assertions
|
|
463
|
+
- The store is the single source of truth
|
|
464
|
+
- The engine can be reconstructed from any store snapshot
|
|
465
|
+
- Input handling (gestures, keyboard) is a separate concern — wire `store.trigger.*` to whatever input you want
|
|
466
|
+
|
|
467
|
+
```ts
|
|
468
|
+
// All interaction goes through the store:
|
|
469
|
+
store.trigger.panStart({ x: e.clientX, y: e.clientY });
|
|
470
|
+
store.trigger.pan({ x: e.clientX, y: e.clientY });
|
|
471
|
+
store.trigger.panEnd();
|
|
472
|
+
store.trigger.select({ ids: ['a'], mode: 'single' });
|
|
473
|
+
store.trigger.dragStart({ id: 'a' });
|
|
474
|
+
store.trigger.dragMove({ dx: 10, dy: 5 });
|
|
475
|
+
store.trigger.dragEnd();
|
|
476
|
+
store.trigger.zoomTo({ zoom: 1.5, center: { x: 400, y: 300 } });
|
|
477
|
+
|
|
478
|
+
// Engine is purely derived:
|
|
479
|
+
const bounds = engine.resolvedBounds('a'); // from store snapshot
|
|
480
|
+
const paths = engine.getAllEdgePaths(); // computed from graph state
|
|
481
|
+
const canvas = engine.screenToCanvas({ x, y }); // from current viewport
|
|
482
|
+
```
|
|
483
|
+
|
|
484
|
+
## License
|
|
485
|
+
|
|
486
|
+
MIT
|