@statelyai/flow 0.13.4 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -1
- package/dist/esm/index.d.ts +13 -2
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +39 -14
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/layout/dagre.d.ts +2 -1
- package/dist/esm/layout/dagre.d.ts.map +1 -0
- package/dist/esm/layout/dagre.js +2 -0
- package/dist/esm/layout/dagre.js.map +1 -0
- package/dist/esm/layout/elk.d.ts +2 -1
- package/dist/esm/layout/elk.d.ts.map +1 -0
- package/dist/esm/layout/elk.js +2 -0
- package/dist/esm/layout/elk.js.map +1 -0
- package/dist/esm/layout/stately.d.ts +2 -1
- package/dist/esm/layout/stately.d.ts.map +1 -0
- package/dist/esm/layout/stately.js +2 -0
- package/dist/esm/layout/stately.js.map +1 -0
- package/dist/esm/shared.d.ts +2 -1
- package/dist/esm/shared.d.ts.map +1 -0
- package/dist/esm/shared.js +2 -0
- package/dist/esm/shared.js.map +1 -0
- package/package.json +4 -3
- package/src/__fixtures__/nested.ts +106 -0
- package/src/appearances.test.ts +428 -0
- package/src/appearances.ts +144 -0
- package/src/authoritative-route.ts +14 -0
- package/src/changeSet.test.ts +132 -0
- package/src/changeSet.ts +337 -0
- package/src/config.ts +41 -0
- package/src/configure.test.ts +212 -0
- package/src/connection-lifetime.ts +1 -0
- package/src/connectionPreview.test.ts +102 -0
- package/src/connectionPreview.ts +82 -0
- package/src/content.ts +203 -0
- package/src/createFlow.fit-frame.test.ts +256 -0
- package/src/createFlow.modern.test.ts +743 -0
- package/src/createFlow.test.ts +632 -0
- package/src/createFlow.ts +3463 -0
- package/src/createFlow.view.test.ts +212 -0
- package/src/createFlow.viewport-controls.test.ts +221 -0
- package/src/edge-geometry.ts +318 -0
- package/src/engine.cache.test.ts +728 -0
- package/src/engine.hierarchy.test.ts +1023 -0
- package/src/engine.test.ts +1965 -0
- package/src/engine.ts +3385 -0
- package/src/entity-changes.test.ts +309 -0
- package/src/entity-changes.ts +395 -0
- package/src/examples/flow-based-math.test.ts +25 -0
- package/src/examples/flow-based-math.ts +210 -0
- package/src/external-edit.test.ts +337 -0
- package/src/external-edit.ts +358 -0
- package/src/factories.test.ts +104 -0
- package/src/factories.ts +70 -0
- package/src/geometry.ts +13 -0
- package/src/graph-index.test.ts +56 -0
- package/src/graph-index.ts +194 -0
- package/src/hierarchy.test.ts +175 -0
- package/src/hierarchy.ts +330 -0
- package/src/highlights.test.ts +130 -0
- package/src/index.ts +469 -0
- package/src/input/index.ts +615 -0
- package/src/input/input.test.ts +2577 -0
- package/src/input/keymap.ts +111 -0
- package/src/input/modes.ts +1715 -0
- package/src/input/reparent.test.ts +214 -0
- package/src/layout/dagre.ts +138 -0
- package/src/layout/elk.ts +1382 -0
- package/src/layout/index.ts +12 -0
- package/src/layout/layout.test.ts +1248 -0
- package/src/layout/shared.ts +260 -0
- package/src/layout/stately-globals.d.ts +9 -0
- package/src/layout/stately.test.ts +246 -0
- package/src/layout/stately.ts +528 -0
- package/src/layout-animation.test.ts +223 -0
- package/src/layout-contract.test.ts +414 -0
- package/src/marker.test.ts +23 -0
- package/src/marker.ts +23 -0
- package/src/measurement-anchor.test.ts +232 -0
- package/src/minimap.test.ts +96 -0
- package/src/minimap.ts +137 -0
- package/src/nested-fixture.test.ts +178 -0
- package/src/port-measurement.test.ts +27 -0
- package/src/port-measurements.test.ts +187 -0
- package/src/profile.ts +124 -0
- package/src/projections.test.ts +103 -0
- package/src/projections.ts +295 -0
- package/src/render-invalidation.test.ts +62 -0
- package/src/render-invalidation.ts +55 -0
- package/src/renderer-geometry.test.ts +356 -0
- package/src/renderer-transactions.test.ts +234 -0
- package/src/routing/avoid.ts +417 -0
- package/src/routing/index.test.ts +361 -0
- package/src/routing/index.ts +366 -0
- package/src/routing/readTracking.test.ts +103 -0
- package/src/routing/readTracking.ts +233 -0
- package/src/selection-commands.test.ts +355 -0
- package/src/serialize.test.ts +167 -0
- package/src/serialize.ts +254 -0
- package/src/shapes/index.test.ts +45 -0
- package/src/shapes/index.ts +142 -0
- package/src/snap.test.ts +123 -0
- package/src/snap.ts +395 -0
- package/src/spatial.test.ts +676 -0
- package/src/spatial.ts +457 -0
- package/src/store/index.ts +4121 -0
- package/src/store.test.ts +2903 -0
- package/src/toolbar.test.ts +31 -0
- package/src/toolbar.ts +62 -0
- package/src/types.test.ts +15 -0
- package/src/types.ts +487 -0
- package/src/validation.ts +558 -0
- package/src/view.ts +172 -0
- package/src/viewport/animate.ts +285 -0
- package/src/viewport/index.ts +296 -0
- package/src/viewport.test.ts +569 -0
package/src/serialize.ts
ADDED
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
FlowEdge,
|
|
3
|
+
FlowEdgeLabel,
|
|
4
|
+
FlowGraph,
|
|
5
|
+
FlowNode,
|
|
6
|
+
Viewport,
|
|
7
|
+
} from './types';
|
|
8
|
+
import {
|
|
9
|
+
cloneJsonSafe,
|
|
10
|
+
currentDataVersions,
|
|
11
|
+
FlowGraphValidationError,
|
|
12
|
+
validateAndCloneGraph,
|
|
13
|
+
validateViewport,
|
|
14
|
+
type FlowDataKind,
|
|
15
|
+
type FlowDataSchemas,
|
|
16
|
+
type FlowDataVersions,
|
|
17
|
+
type FlowValidationIssue,
|
|
18
|
+
} from './validation';
|
|
19
|
+
|
|
20
|
+
export type FlowSerialized<
|
|
21
|
+
TNodeData = any,
|
|
22
|
+
TEdgeData = any,
|
|
23
|
+
TLabelData = any,
|
|
24
|
+
> = {
|
|
25
|
+
version: 1;
|
|
26
|
+
graph: FlowGraph<TNodeData, TEdgeData, TLabelData>;
|
|
27
|
+
viewport: Viewport;
|
|
28
|
+
collapsedNodeIds?: string[];
|
|
29
|
+
dataVersions?: FlowDataVersions;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
function migrateDataValue<TData>(
|
|
33
|
+
value: unknown,
|
|
34
|
+
kind: FlowDataKind,
|
|
35
|
+
fromVersion: number,
|
|
36
|
+
contract: import('./validation').FlowDataSchema<TData> | undefined,
|
|
37
|
+
path: string,
|
|
38
|
+
): unknown {
|
|
39
|
+
if (!contract || fromVersion === contract.version) return value;
|
|
40
|
+
if (fromVersion > contract.version) {
|
|
41
|
+
throw new FlowGraphValidationError([
|
|
42
|
+
{
|
|
43
|
+
code: 'unsupported_data_version',
|
|
44
|
+
path,
|
|
45
|
+
message: `${kind} data version ${fromVersion} is newer than supported version ${contract.version}`,
|
|
46
|
+
},
|
|
47
|
+
]);
|
|
48
|
+
}
|
|
49
|
+
if (!contract.migrate) {
|
|
50
|
+
throw new FlowGraphValidationError([
|
|
51
|
+
{
|
|
52
|
+
code: 'missing_data_migration',
|
|
53
|
+
path,
|
|
54
|
+
message: `${kind} data requires migration from version ${fromVersion} to ${contract.version}`,
|
|
55
|
+
},
|
|
56
|
+
]);
|
|
57
|
+
}
|
|
58
|
+
const migrated = contract.migrate(value, {
|
|
59
|
+
fromVersion,
|
|
60
|
+
toVersion: contract.version,
|
|
61
|
+
});
|
|
62
|
+
if (migrated && typeof (migrated as { then?: unknown }).then === 'function') {
|
|
63
|
+
throw new FlowGraphValidationError([
|
|
64
|
+
{
|
|
65
|
+
code: 'async_data_migration',
|
|
66
|
+
path,
|
|
67
|
+
message: 'Flow data migrations must be synchronous',
|
|
68
|
+
},
|
|
69
|
+
]);
|
|
70
|
+
}
|
|
71
|
+
return migrated;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Parse, migrate, and validate a persisted Flow document.
|
|
76
|
+
*
|
|
77
|
+
* Versionless documents are treated as version 0. Data migrations must be
|
|
78
|
+
* synchronous. The returned graph is detached from `input`; invalid documents
|
|
79
|
+
* throw `FlowGraphValidationError` with stable issue paths.
|
|
80
|
+
*/
|
|
81
|
+
export function parseFlowSerialized<
|
|
82
|
+
TNodeData = any,
|
|
83
|
+
TEdgeData = any,
|
|
84
|
+
TLabelData = any,
|
|
85
|
+
>(
|
|
86
|
+
input: unknown,
|
|
87
|
+
schemas?: FlowDataSchemas<TNodeData, TEdgeData, TLabelData>,
|
|
88
|
+
): FlowSerialized<TNodeData, TEdgeData, TLabelData> {
|
|
89
|
+
if (!input || typeof input !== 'object' || Array.isArray(input)) {
|
|
90
|
+
throw new FlowGraphValidationError([
|
|
91
|
+
{ code: 'invalid_document', path: '$', message: 'expected a Flow document object' },
|
|
92
|
+
]);
|
|
93
|
+
}
|
|
94
|
+
const document = cloneJsonSafe(input, '$') as Record<string, unknown>;
|
|
95
|
+
const version = document.version ?? 0;
|
|
96
|
+
if (version !== 0 && version !== 1) {
|
|
97
|
+
throw new FlowGraphValidationError([
|
|
98
|
+
{
|
|
99
|
+
code: 'unsupported_version',
|
|
100
|
+
path: '$.version',
|
|
101
|
+
message: `unsupported Flow document version ${String(version)}`,
|
|
102
|
+
},
|
|
103
|
+
]);
|
|
104
|
+
}
|
|
105
|
+
if (!document.graph || typeof document.graph !== 'object') {
|
|
106
|
+
throw new FlowGraphValidationError([
|
|
107
|
+
{ code: 'invalid_graph', path: '$.graph', message: 'expected a graph object' },
|
|
108
|
+
]);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const rawGraph = validateAndCloneGraph<TNodeData, TEdgeData, TLabelData>(
|
|
112
|
+
document.graph,
|
|
113
|
+
);
|
|
114
|
+
const storedVersions =
|
|
115
|
+
document.dataVersions && typeof document.dataVersions === 'object'
|
|
116
|
+
? (document.dataVersions as FlowDataVersions)
|
|
117
|
+
: {};
|
|
118
|
+
if (schemas) {
|
|
119
|
+
rawGraph.nodes?.forEach((node, index) => {
|
|
120
|
+
node.data = migrateDataValue(
|
|
121
|
+
node.data,
|
|
122
|
+
'node',
|
|
123
|
+
storedVersions.node ?? 0,
|
|
124
|
+
schemas.node,
|
|
125
|
+
`$.graph.nodes[${index}].data`,
|
|
126
|
+
) as TNodeData;
|
|
127
|
+
});
|
|
128
|
+
rawGraph.edges?.forEach((edge, index) => {
|
|
129
|
+
edge.data = migrateDataValue(
|
|
130
|
+
edge.data,
|
|
131
|
+
'edge',
|
|
132
|
+
storedVersions.edge ?? 0,
|
|
133
|
+
schemas.edge,
|
|
134
|
+
`$.graph.edges[${index}].data`,
|
|
135
|
+
) as TEdgeData;
|
|
136
|
+
if (edge.label) {
|
|
137
|
+
edge.label.data = migrateDataValue(
|
|
138
|
+
edge.label.data,
|
|
139
|
+
'label',
|
|
140
|
+
storedVersions.label ?? 0,
|
|
141
|
+
schemas.label,
|
|
142
|
+
`$.graph.edges[${index}].label.data`,
|
|
143
|
+
) as TLabelData;
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const graph = validateAndCloneGraph(rawGraph, schemas);
|
|
149
|
+
const viewport = validateViewport(document.viewport ?? { x: 0, y: 0, zoom: 1 });
|
|
150
|
+
const collapsedNodeIds = Array.isArray(document.collapsedNodeIds)
|
|
151
|
+
? document.collapsedNodeIds
|
|
152
|
+
: [];
|
|
153
|
+
const nodeIds = new Set(graph.nodes.map((node) => node.id));
|
|
154
|
+
const issues: FlowValidationIssue[] = [];
|
|
155
|
+
collapsedNodeIds.forEach((id, index) => {
|
|
156
|
+
if (typeof id !== 'string' || !nodeIds.has(id)) {
|
|
157
|
+
issues.push({
|
|
158
|
+
code: 'missing_collapsed_node',
|
|
159
|
+
path: `$.collapsedNodeIds[${index}]`,
|
|
160
|
+
message: `node "${String(id)}" does not exist`,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
if (issues.length > 0) throw new FlowGraphValidationError(issues);
|
|
165
|
+
|
|
166
|
+
return {
|
|
167
|
+
version: 1,
|
|
168
|
+
graph,
|
|
169
|
+
viewport,
|
|
170
|
+
...(collapsedNodeIds.length > 0 ? { collapsedNodeIds: [...collapsedNodeIds] } : {}),
|
|
171
|
+
...(currentDataVersions(schemas) ? { dataVersions: currentDataVersions(schemas) } : {}),
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Create a persistence-safe graph snapshot.
|
|
177
|
+
*
|
|
178
|
+
* Transient drag and measurement state is folded into authored coordinates or
|
|
179
|
+
* removed. Nested geometry objects are copied so callers can mutate the result
|
|
180
|
+
* without changing the live graph.
|
|
181
|
+
*/
|
|
182
|
+
export function serializeFlowGraph<
|
|
183
|
+
TNodeData = any,
|
|
184
|
+
TEdgeData = any,
|
|
185
|
+
TLabelData = any,
|
|
186
|
+
>(
|
|
187
|
+
graph: FlowGraph<TNodeData, TEdgeData, TLabelData>,
|
|
188
|
+
): FlowGraph<TNodeData, TEdgeData, TLabelData> {
|
|
189
|
+
return {
|
|
190
|
+
...graph,
|
|
191
|
+
nodes: graph.nodes.map(serializeNode) as FlowNode<TNodeData>[],
|
|
192
|
+
edges: graph.edges.map(serializeEdge) as FlowEdge<TEdgeData, TLabelData>[],
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function serializeNode<TNodeData>(node: FlowNode<TNodeData>): FlowNode<TNodeData> {
|
|
197
|
+
const anchorOffset = node.measured?.anchorOffset;
|
|
198
|
+
const serialized: FlowNode<TNodeData> = {
|
|
199
|
+
...node,
|
|
200
|
+
x: node.x + (node.dx ?? 0) - (anchorOffset?.x ?? 0),
|
|
201
|
+
y: node.y + (node.dy ?? 0) - (anchorOffset?.y ?? 0),
|
|
202
|
+
...(node.style ? { style: { ...node.style } } : {}),
|
|
203
|
+
...(node.ports
|
|
204
|
+
? { ports: node.ports.map((port) => ({ ...port })) as FlowNode<TNodeData>['ports'] }
|
|
205
|
+
: {}),
|
|
206
|
+
...(node.group ? { group: { ...node.group } } : {}),
|
|
207
|
+
...(node.resize ? { resize: { ...node.resize } } : {}),
|
|
208
|
+
};
|
|
209
|
+
delete serialized.dx;
|
|
210
|
+
delete serialized.dy;
|
|
211
|
+
delete serialized.measured;
|
|
212
|
+
return serialized;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function serializeEdge<TEdgeData, TLabelData>(
|
|
216
|
+
edge: FlowEdge<TEdgeData, TLabelData>,
|
|
217
|
+
): FlowEdge<TEdgeData, TLabelData> {
|
|
218
|
+
const dx = edge.dx ?? 0;
|
|
219
|
+
const dy = edge.dy ?? 0;
|
|
220
|
+
const serialized: FlowEdge<TEdgeData, TLabelData> = {
|
|
221
|
+
...edge,
|
|
222
|
+
...(edge.label ? { label: serializeLabel(edge.label) } : {}),
|
|
223
|
+
...(edge.style ? { style: { ...edge.style } } : {}),
|
|
224
|
+
...(edge.points ? { points: edge.points.map((point) => ({ ...point })) } : {}),
|
|
225
|
+
...(edge.waypoints
|
|
226
|
+
? {
|
|
227
|
+
waypoints: edge.waypoints.map((point) => ({
|
|
228
|
+
x: point.x + dx,
|
|
229
|
+
y: point.y + dy,
|
|
230
|
+
})),
|
|
231
|
+
}
|
|
232
|
+
: {}),
|
|
233
|
+
};
|
|
234
|
+
delete serialized.dx;
|
|
235
|
+
delete serialized.dy;
|
|
236
|
+
if (typeof serialized.labelAnchor === 'function') {
|
|
237
|
+
delete serialized.labelAnchor;
|
|
238
|
+
}
|
|
239
|
+
return serialized;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function serializeLabel<TLabelData>(
|
|
243
|
+
label: FlowEdgeLabel<TLabelData>,
|
|
244
|
+
): FlowEdgeLabel<TLabelData> {
|
|
245
|
+
const serialized: FlowEdgeLabel<TLabelData> = {
|
|
246
|
+
...label,
|
|
247
|
+
x: label.x + (label.dx ?? 0),
|
|
248
|
+
y: label.y + (label.dy ?? 0),
|
|
249
|
+
...(label.style ? { style: { ...label.style } } : {}),
|
|
250
|
+
};
|
|
251
|
+
delete serialized.dx;
|
|
252
|
+
delete serialized.dy;
|
|
253
|
+
return serialized;
|
|
254
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { getShape, getShapeOrDefault, registerShape } from './index';
|
|
3
|
+
|
|
4
|
+
const bounds = { x: 10, y: 20, width: 100, height: 60 };
|
|
5
|
+
|
|
6
|
+
describe('built-in shapes', () => {
|
|
7
|
+
it.each([
|
|
8
|
+
['rect', { x: 10, y: 20 }, { x: 9, y: 20 }],
|
|
9
|
+
['ellipse', { x: 60, y: 20 }, { x: 10, y: 20 }],
|
|
10
|
+
['diamond', { x: 60, y: 20 }, { x: 10, y: 20 }],
|
|
11
|
+
])('%s includes its boundary and excludes an outside point', (name, inside, outside) => {
|
|
12
|
+
const shape = getShapeOrDefault(name);
|
|
13
|
+
expect(shape.hitTest(inside, bounds)).toBe(true);
|
|
14
|
+
expect(shape.hitTest(outside, bounds)).toBe(false);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it.each(['rect', 'rounded', 'ellipse', 'diamond'])('%s anchors every side', (name) => {
|
|
18
|
+
const shape = getShapeOrDefault(name);
|
|
19
|
+
expect(shape.edgeAnchor(bounds, 'top')).toEqual({ x: 60, y: 20 });
|
|
20
|
+
expect(shape.edgeAnchor(bounds, 'right')).toEqual({ x: 110, y: 50 });
|
|
21
|
+
expect(shape.edgeAnchor(bounds, 'bottom')).toEqual({ x: 60, y: 80 });
|
|
22
|
+
expect(shape.edgeAnchor(bounds, 'left')).toEqual({ x: 10, y: 50 });
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it.each(['ellipse', 'diamond'])('%s rejects degenerate bounds', (name) => {
|
|
26
|
+
expect(
|
|
27
|
+
getShapeOrDefault(name).hitTest(
|
|
28
|
+
{ x: 0, y: 0 },
|
|
29
|
+
{ x: 0, y: 0, width: 0, height: 10 },
|
|
30
|
+
),
|
|
31
|
+
).toBe(false);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('supports custom replacement and defaults unknown names to rectangles', () => {
|
|
35
|
+
const custom = {
|
|
36
|
+
hitTest: () => true,
|
|
37
|
+
edgeAnchor: () => ({ x: 1, y: 2 }),
|
|
38
|
+
};
|
|
39
|
+
registerShape('custom-test', custom);
|
|
40
|
+
|
|
41
|
+
expect(getShape('custom-test')).toBe(custom);
|
|
42
|
+
expect(getShapeOrDefault('custom-test')).toBe(custom);
|
|
43
|
+
expect(getShapeOrDefault('unknown').hitTest({ x: 10, y: 20 }, bounds)).toBe(true);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import type { Direction, Point, Rect, ShapeHandler } from '../types';
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
// Shape handlers
|
|
5
|
+
//
|
|
6
|
+
// Shapes are engine logic: hit testing + edge anchoring.
|
|
7
|
+
// Renderers handle drawing.
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
const shapeRegistry = new Map<string, ShapeHandler>();
|
|
11
|
+
|
|
12
|
+
export function registerShape(name: string, handler: ShapeHandler): void {
|
|
13
|
+
shapeRegistry.set(name, handler);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function getShape(name: string): ShapeHandler | undefined {
|
|
17
|
+
return shapeRegistry.get(name);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function getShapeOrDefault(name: string): ShapeHandler {
|
|
21
|
+
return shapeRegistry.get(name) ?? rectHandler;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
// Built-in: rect
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
function rectHitTest(point: Point, bounds: Rect): boolean {
|
|
29
|
+
return (
|
|
30
|
+
point.x >= bounds.x &&
|
|
31
|
+
point.x <= bounds.x + bounds.width &&
|
|
32
|
+
point.y >= bounds.y &&
|
|
33
|
+
point.y <= bounds.y + bounds.height
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function rectEdgeAnchor(bounds: Rect, direction: Direction): Point {
|
|
38
|
+
switch (direction) {
|
|
39
|
+
case 'top':
|
|
40
|
+
return { x: bounds.x + bounds.width / 2, y: bounds.y };
|
|
41
|
+
case 'bottom':
|
|
42
|
+
return { x: bounds.x + bounds.width / 2, y: bounds.y + bounds.height };
|
|
43
|
+
case 'left':
|
|
44
|
+
return { x: bounds.x, y: bounds.y + bounds.height / 2 };
|
|
45
|
+
case 'right':
|
|
46
|
+
return { x: bounds.x + bounds.width, y: bounds.y + bounds.height / 2 };
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const rectHandler: ShapeHandler = {
|
|
51
|
+
hitTest: rectHitTest,
|
|
52
|
+
edgeAnchor: rectEdgeAnchor,
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
// Built-in: rounded (same geometry as rect for hit/anchor, renderer rounds corners)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
const roundedHandler: ShapeHandler = {
|
|
60
|
+
hitTest: rectHitTest,
|
|
61
|
+
edgeAnchor: rectEdgeAnchor,
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
// Built-in: ellipse
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
function ellipseHitTest(point: Point, bounds: Rect): boolean {
|
|
69
|
+
const cx = bounds.x + bounds.width / 2;
|
|
70
|
+
const cy = bounds.y + bounds.height / 2;
|
|
71
|
+
const rx = bounds.width / 2;
|
|
72
|
+
const ry = bounds.height / 2;
|
|
73
|
+
if (rx === 0 || ry === 0) return false;
|
|
74
|
+
const dx = point.x - cx;
|
|
75
|
+
const dy = point.y - cy;
|
|
76
|
+
return (dx * dx) / (rx * rx) + (dy * dy) / (ry * ry) <= 1;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function ellipseEdgeAnchor(bounds: Rect, direction: Direction): Point {
|
|
80
|
+
const cx = bounds.x + bounds.width / 2;
|
|
81
|
+
const cy = bounds.y + bounds.height / 2;
|
|
82
|
+
const rx = bounds.width / 2;
|
|
83
|
+
const ry = bounds.height / 2;
|
|
84
|
+
switch (direction) {
|
|
85
|
+
case 'top':
|
|
86
|
+
return { x: cx, y: cy - ry };
|
|
87
|
+
case 'bottom':
|
|
88
|
+
return { x: cx, y: cy + ry };
|
|
89
|
+
case 'left':
|
|
90
|
+
return { x: cx - rx, y: cy };
|
|
91
|
+
case 'right':
|
|
92
|
+
return { x: cx + rx, y: cy };
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const ellipseHandler: ShapeHandler = {
|
|
97
|
+
hitTest: ellipseHitTest,
|
|
98
|
+
edgeAnchor: ellipseEdgeAnchor,
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
// Built-in: diamond
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
function diamondHitTest(point: Point, bounds: Rect): boolean {
|
|
106
|
+
const cx = bounds.x + bounds.width / 2;
|
|
107
|
+
const cy = bounds.y + bounds.height / 2;
|
|
108
|
+
const hw = bounds.width / 2;
|
|
109
|
+
const hh = bounds.height / 2;
|
|
110
|
+
if (hw === 0 || hh === 0) return false;
|
|
111
|
+
// Manhattan distance normalized to diamond
|
|
112
|
+
return Math.abs(point.x - cx) / hw + Math.abs(point.y - cy) / hh <= 1;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function diamondEdgeAnchor(bounds: Rect, direction: Direction): Point {
|
|
116
|
+
const cx = bounds.x + bounds.width / 2;
|
|
117
|
+
const cy = bounds.y + bounds.height / 2;
|
|
118
|
+
switch (direction) {
|
|
119
|
+
case 'top':
|
|
120
|
+
return { x: cx, y: bounds.y };
|
|
121
|
+
case 'bottom':
|
|
122
|
+
return { x: cx, y: bounds.y + bounds.height };
|
|
123
|
+
case 'left':
|
|
124
|
+
return { x: bounds.x, y: cy };
|
|
125
|
+
case 'right':
|
|
126
|
+
return { x: bounds.x + bounds.width, y: cy };
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const diamondHandler: ShapeHandler = {
|
|
131
|
+
hitTest: diamondHitTest,
|
|
132
|
+
edgeAnchor: diamondEdgeAnchor,
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
// Register built-in shapes
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
registerShape('rect', rectHandler);
|
|
140
|
+
registerShape('rounded', roundedHandler);
|
|
141
|
+
registerShape('ellipse', ellipseHandler);
|
|
142
|
+
registerShape('diamond', diamondHandler);
|
package/src/snap.test.ts
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { getSnapLines } from './snap';
|
|
3
|
+
import type { Rect } from './types';
|
|
4
|
+
|
|
5
|
+
describe('getSnapLines', () => {
|
|
6
|
+
it('returns null with no other rects', () => {
|
|
7
|
+
expect(getSnapLines({ x: 10, y: 10, width: 50, height: 50 }, [])).toBeNull();
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it('returns null when nothing is within threshold', () => {
|
|
11
|
+
const rect: Rect = { x: 0, y: 0, width: 50, height: 50 };
|
|
12
|
+
const others: Rect[] = [{ x: 500, y: 500, width: 50, height: 50 }];
|
|
13
|
+
expect(getSnapLines(rect, others, { threshold: 5 })).toBeNull();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('snaps left edge to left edge of target', () => {
|
|
17
|
+
const rect: Rect = { x: 102, y: 0, width: 50, height: 50 };
|
|
18
|
+
const target: Rect = { x: 100, y: 200, width: 80, height: 40 };
|
|
19
|
+
const result = getSnapLines(rect, [target], { threshold: 5 });
|
|
20
|
+
expect(result).not.toBeNull();
|
|
21
|
+
expect(result!.sourceRect).toBe(rect);
|
|
22
|
+
expect(result!.snapRect.x).toBe(100); // snapped to target left
|
|
23
|
+
expect(result!.snapLines.some(l => l.axis === 'x' && l.value === 100)).toBe(true);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('snaps center to center', () => {
|
|
27
|
+
// rect center X = 51, target center X = 50
|
|
28
|
+
const rect: Rect = { x: 26, y: 0, width: 50, height: 50 };
|
|
29
|
+
const target: Rect = { x: 25, y: 200, width: 50, height: 50 };
|
|
30
|
+
const result = getSnapLines(rect, [target], { threshold: 5 });
|
|
31
|
+
expect(result).not.toBeNull();
|
|
32
|
+
expect(result!.snapRect.x).toBe(25); // centers aligned
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('snaps on Y axis (top to top)', () => {
|
|
36
|
+
const rect: Rect = { x: 0, y: 102, width: 50, height: 50 };
|
|
37
|
+
const target: Rect = { x: 200, y: 100, width: 50, height: 50 };
|
|
38
|
+
const result = getSnapLines(rect, [target], { threshold: 5 });
|
|
39
|
+
expect(result).not.toBeNull();
|
|
40
|
+
expect(result!.snapRect.y).toBe(100);
|
|
41
|
+
expect(result!.snapLines.some(l => l.axis === 'y' && l.value === 100)).toBe(true);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('snaps on both axes simultaneously', () => {
|
|
45
|
+
const rect: Rect = { x: 101, y: 202, width: 50, height: 50 };
|
|
46
|
+
const target: Rect = { x: 100, y: 200, width: 50, height: 50 };
|
|
47
|
+
const result = getSnapLines(rect, [target], { threshold: 5 });
|
|
48
|
+
expect(result).not.toBeNull();
|
|
49
|
+
expect(result!.snapRect.x).toBe(100);
|
|
50
|
+
expect(result!.snapRect.y).toBe(200);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('preserves rect size after snap', () => {
|
|
54
|
+
const rect: Rect = { x: 102, y: 0, width: 73, height: 41 };
|
|
55
|
+
const target: Rect = { x: 100, y: 200, width: 50, height: 50 };
|
|
56
|
+
const result = getSnapLines(rect, [target], { threshold: 5 });
|
|
57
|
+
expect(result!.snapRect.width).toBe(73);
|
|
58
|
+
expect(result!.snapRect.height).toBe(41);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('snaps to closest target when multiple are within threshold', () => {
|
|
62
|
+
const rect: Rect = { x: 52, y: 0, width: 50, height: 50 };
|
|
63
|
+
const targets: Rect[] = [
|
|
64
|
+
{ x: 50, y: 100, width: 50, height: 50 }, // dist = 2
|
|
65
|
+
{ x: 55, y: 200, width: 50, height: 50 }, // dist = 3
|
|
66
|
+
];
|
|
67
|
+
const result = getSnapLines(rect, targets, { threshold: 5 });
|
|
68
|
+
expect(result!.snapRect.x).toBe(50); // snaps to closer
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('edge labels and nodes are interchangeable rects', () => {
|
|
72
|
+
// This test verifies snap lines work the same regardless of rect source
|
|
73
|
+
const dragged: Rect = { x: 101, y: 50, width: 60, height: 20 }; // an edge label rect
|
|
74
|
+
const nodeRect: Rect = { x: 100, y: 200, width: 150, height: 50 }; // a node rect
|
|
75
|
+
const result = getSnapLines(dragged, [nodeRect], { threshold: 5 });
|
|
76
|
+
expect(result).not.toBeNull();
|
|
77
|
+
expect(result!.snapRect.x).toBe(100);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
describe('getSnapLines with spacing guides', () => {
|
|
82
|
+
it('detects equal horizontal spacing', () => {
|
|
83
|
+
// Three rects in a row: A, gap, B, gap, dragged
|
|
84
|
+
const a: Rect = { x: 0, y: 0, width: 50, height: 50 };
|
|
85
|
+
const b: Rect = { x: 80, y: 0, width: 50, height: 50 };
|
|
86
|
+
// gap between a and b = 80 - 50 = 30
|
|
87
|
+
// dragged should snap so gap from b to dragged is also 30 → x = 160
|
|
88
|
+
const dragged: Rect = { x: 161, y: 0, width: 50, height: 50 };
|
|
89
|
+
const result = getSnapLines(dragged, [a, b], { threshold: 5, enableSpacing: true });
|
|
90
|
+
expect(result).not.toBeNull();
|
|
91
|
+
expect(result!.snapRect.x).toBe(160);
|
|
92
|
+
expect(result!.spacingGuides).toBeDefined();
|
|
93
|
+
expect(result!.spacingGuides!.length).toBeGreaterThan(0);
|
|
94
|
+
expect(result!.spacingGuides![0].axis).toBe('x');
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('detects equal spacing when distributed between two neighbors (horizontal)', () => {
|
|
98
|
+
// A ... dragged ... C — dragged should center in the gap with equal spacing.
|
|
99
|
+
const a: Rect = { x: 0, y: 0, width: 50, height: 50 };
|
|
100
|
+
const c: Rect = { x: 180, y: 0, width: 50, height: 50 };
|
|
101
|
+
// leftEdge = 50, rightEdge = 180 → centered x = (50 + 180 - 50) / 2 = 90, gap = 40
|
|
102
|
+
const dragged: Rect = { x: 92, y: 0, width: 50, height: 50 };
|
|
103
|
+
const result = getSnapLines(dragged, [a, c], { threshold: 5, enableSpacing: true });
|
|
104
|
+
expect(result).not.toBeNull();
|
|
105
|
+
expect(result!.snapRect.x).toBe(90);
|
|
106
|
+
const xGuide = result!.spacingGuides?.find((g) => g.axis === 'x');
|
|
107
|
+
expect(xGuide).toBeDefined();
|
|
108
|
+
expect(xGuide!.spacing).toBe(40);
|
|
109
|
+
expect(xGuide!.ranges).toHaveLength(2);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('detects equal spacing when distributed between two neighbors (vertical)', () => {
|
|
113
|
+
const a: Rect = { x: 0, y: 0, width: 50, height: 50 };
|
|
114
|
+
const c: Rect = { x: 0, y: 180, width: 50, height: 50 };
|
|
115
|
+
const dragged: Rect = { x: 0, y: 92, width: 50, height: 50 };
|
|
116
|
+
const result = getSnapLines(dragged, [a, c], { threshold: 5, enableSpacing: true });
|
|
117
|
+
expect(result).not.toBeNull();
|
|
118
|
+
expect(result!.snapRect.y).toBe(90);
|
|
119
|
+
const yGuide = result!.spacingGuides?.find((g) => g.axis === 'y');
|
|
120
|
+
expect(yGuide).toBeDefined();
|
|
121
|
+
expect(yGuide!.spacing).toBe(40);
|
|
122
|
+
});
|
|
123
|
+
});
|