@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
|
@@ -0,0 +1,2903 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { createFlowStore } from './store';
|
|
3
|
+
import { createNode, createEdge, createGraph, createPort } from './factories';
|
|
4
|
+
import { defineFlowProfile } from './profile';
|
|
5
|
+
import { resolvedSize } from './spatial';
|
|
6
|
+
import { fixedGroupGeometry } from './hierarchy';
|
|
7
|
+
import type { FlowNode, FlowEdge, Rect } from './types';
|
|
8
|
+
|
|
9
|
+
function ctx(store: ReturnType<typeof createFlowStore>) {
|
|
10
|
+
return store.getSnapshot().context;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function makeStore(nodes: FlowNode[] = [], edges: FlowEdge[] = [], opts = {}) {
|
|
14
|
+
return createFlowStore(createGraph({ nodes, edges }), opts);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function undo(store: ReturnType<typeof createFlowStore>) {
|
|
18
|
+
store.trigger.undo();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function redo(store: ReturnType<typeof createFlowStore>) {
|
|
22
|
+
store.trigger.redo();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function history(store: ReturnType<typeof createFlowStore>) {
|
|
26
|
+
const snapshot = store.getSnapshot() as ReturnType<
|
|
27
|
+
typeof createFlowStore
|
|
28
|
+
>['getSnapshot'] extends () => infer T
|
|
29
|
+
? T
|
|
30
|
+
: never;
|
|
31
|
+
return {
|
|
32
|
+
past: (snapshot as any).past.length,
|
|
33
|
+
future: (snapshot as any).future.length,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Store creation
|
|
38
|
+
|
|
39
|
+
describe('createFlowStore', () => {
|
|
40
|
+
it('creates store with default state', () => {
|
|
41
|
+
const store = makeStore();
|
|
42
|
+
const c = ctx(store);
|
|
43
|
+
expect(c.canvasState).toBe('idle');
|
|
44
|
+
expect(c.selection.size).toBe(0);
|
|
45
|
+
expect(c.viewport).toEqual({ x: 0, y: 0, zoom: 1 });
|
|
46
|
+
expect(c.mode).toBe('interactive');
|
|
47
|
+
expect(c.graph.nodes).toEqual([]);
|
|
48
|
+
expect(c.graph.edges).toEqual([]);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('accepts initial graph', () => {
|
|
52
|
+
const node = createNode({ id: 'a', x: 10, y: 20 });
|
|
53
|
+
const store = makeStore([node]);
|
|
54
|
+
expect(ctx(store).graph.nodes).toHaveLength(1);
|
|
55
|
+
expect(ctx(store).graph.nodes[0].id).toBe('a');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('accepts options', () => {
|
|
59
|
+
const store = makeStore([], [], {
|
|
60
|
+
snapToGrid: true,
|
|
61
|
+
snapGrid: [20, 20] as [number, number],
|
|
62
|
+
enableSnapLines: true,
|
|
63
|
+
});
|
|
64
|
+
const c = ctx(store);
|
|
65
|
+
expect(c.snapToGrid).toBe(true);
|
|
66
|
+
expect(c.snapGrid).toEqual([20, 20]);
|
|
67
|
+
expect(c.enableSnapLines).toBe(true);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('sets profile modes and falls back to the default mode', () => {
|
|
71
|
+
const store = makeStore();
|
|
72
|
+
|
|
73
|
+
store.trigger.setMode({ mode: 'readonly' });
|
|
74
|
+
expect(ctx(store).mode).toBe('readonly');
|
|
75
|
+
|
|
76
|
+
store.trigger.setMode({ mode: 'missing' });
|
|
77
|
+
expect(ctx(store).mode).toBe('interactive');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('routes setMode through profile mode transitions', () => {
|
|
81
|
+
const profile = defineFlowProfile({
|
|
82
|
+
defaultMode: 'interactive',
|
|
83
|
+
modes: {
|
|
84
|
+
interactive: {
|
|
85
|
+
on: {
|
|
86
|
+
setMode: (context) => context,
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
readonly: {},
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
const store = createFlowStore(undefined, { profile });
|
|
93
|
+
|
|
94
|
+
store.trigger.setMode({ mode: 'readonly' });
|
|
95
|
+
|
|
96
|
+
expect(ctx(store).mode).toBe('interactive');
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
describe('gesture lifecycle events', () => {
|
|
100
|
+
it('emits dragStarted, dragMoved, and dragEnded with ids and positions', () => {
|
|
101
|
+
const store = makeStore([createNode({ id: 'a', x: 0, y: 0 })]);
|
|
102
|
+
const events: unknown[] = [];
|
|
103
|
+
store.on('dragStarted', (event) => events.push(['start', event]));
|
|
104
|
+
store.on('dragMoved', (event) => events.push(['move', event]));
|
|
105
|
+
store.on('dragEnded', (event) => events.push(['end', event]));
|
|
106
|
+
|
|
107
|
+
store.trigger.dragStart({ id: 'a' });
|
|
108
|
+
store.trigger.dragMove({ dx: 10, dy: 5 });
|
|
109
|
+
store.trigger.dragEnd();
|
|
110
|
+
|
|
111
|
+
expect(events).toEqual([
|
|
112
|
+
['start', { type: 'dragStarted', ids: ['a'], positions: { a: { x: 0, y: 0 } } }],
|
|
113
|
+
[
|
|
114
|
+
'move',
|
|
115
|
+
{
|
|
116
|
+
type: 'dragMoved',
|
|
117
|
+
ids: ['a'],
|
|
118
|
+
positions: { a: { x: 10, y: 5 } },
|
|
119
|
+
delta: { x: 10, y: 5 },
|
|
120
|
+
},
|
|
121
|
+
],
|
|
122
|
+
['end', { type: 'dragEnded', ids: ['a'], positions: { a: { x: 10, y: 5 } } }],
|
|
123
|
+
]);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('does not emit dragStarted for a non-draggable node', () => {
|
|
127
|
+
const store = makeStore([createNode({ id: 'a', draggable: false })]);
|
|
128
|
+
const events: unknown[] = [];
|
|
129
|
+
store.on('dragStarted', (event) => events.push(event));
|
|
130
|
+
|
|
131
|
+
store.trigger.dragStart({ id: 'a' });
|
|
132
|
+
|
|
133
|
+
expect(events).toEqual([]);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('emits connectStarted and connectMoved', () => {
|
|
137
|
+
const store = makeStore([createNode({ id: 'a' }), createNode({ id: 'b' })]);
|
|
138
|
+
const events: unknown[] = [];
|
|
139
|
+
store.on('connectStarted', (event) => events.push(['start', event]));
|
|
140
|
+
store.on('connectMoved', (event) => events.push(['move', event]));
|
|
141
|
+
|
|
142
|
+
store.trigger.connectionStart({
|
|
143
|
+
sourceId: 'a',
|
|
144
|
+
sourcePort: null,
|
|
145
|
+
position: { x: 0, y: 0 },
|
|
146
|
+
});
|
|
147
|
+
store.trigger.connectionMove({
|
|
148
|
+
position: { x: 10, y: 5 },
|
|
149
|
+
targetId: 'b',
|
|
150
|
+
targetPort: null,
|
|
151
|
+
isValid: true,
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
expect(events).toEqual([
|
|
155
|
+
['start', { type: 'connectStarted', sourceId: 'a', sourcePort: null, point: { x: 0, y: 0 } }],
|
|
156
|
+
[
|
|
157
|
+
'move',
|
|
158
|
+
{
|
|
159
|
+
type: 'connectMoved',
|
|
160
|
+
point: { x: 10, y: 5 },
|
|
161
|
+
targetId: 'b',
|
|
162
|
+
targetPort: null,
|
|
163
|
+
isValid: true,
|
|
164
|
+
},
|
|
165
|
+
],
|
|
166
|
+
]);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it('emits resizeStarted and resizeEnded', () => {
|
|
170
|
+
const store = makeStore([createNode({ id: 'a', x: 0, y: 0, width: 100, height: 50 })]);
|
|
171
|
+
const events: unknown[] = [];
|
|
172
|
+
store.on('resizeStarted', (event) => events.push(['start', event]));
|
|
173
|
+
store.on('resizeEnded', (event) => events.push(['end', event]));
|
|
174
|
+
|
|
175
|
+
store.trigger.resizeStart({ id: 'a', handle: 'bottom-right' });
|
|
176
|
+
store.trigger.resizeMove({ x: 120, y: 70 });
|
|
177
|
+
store.trigger.resizeEnd();
|
|
178
|
+
|
|
179
|
+
expect(events).toEqual([
|
|
180
|
+
[
|
|
181
|
+
'start',
|
|
182
|
+
{
|
|
183
|
+
type: 'resizeStarted',
|
|
184
|
+
id: 'a',
|
|
185
|
+
handle: 'bottom-right',
|
|
186
|
+
bounds: { x: 0, y: 0, width: 100, height: 50 },
|
|
187
|
+
},
|
|
188
|
+
],
|
|
189
|
+
['end', { type: 'resizeEnded', id: 'a', bounds: { x: 0, y: 0, width: 120, height: 70 } }],
|
|
190
|
+
]);
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
// Graph mutations
|
|
195
|
+
|
|
196
|
+
describe('graph mutations', () => {
|
|
197
|
+
it('createNode adds a node', () => {
|
|
198
|
+
const store = makeStore();
|
|
199
|
+
store.trigger.createNode({ node: createNode({ id: 'n1', x: 50, y: 60 }) });
|
|
200
|
+
expect(ctx(store).graph.nodes).toHaveLength(1);
|
|
201
|
+
expect(ctx(store).graph.nodes[0].x).toBe(50);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it('deleteNode removes node and connected edges', () => {
|
|
205
|
+
const a = createNode({ id: 'a' });
|
|
206
|
+
const b = createNode({ id: 'b' });
|
|
207
|
+
const e = createEdge({ id: 'e1', sourceId: 'a', targetId: 'b' });
|
|
208
|
+
const store = makeStore([a, b], [e]);
|
|
209
|
+
store.trigger.deleteNode({ id: 'a' });
|
|
210
|
+
expect(ctx(store).graph.nodes).toHaveLength(1);
|
|
211
|
+
expect(ctx(store).graph.edges).toHaveLength(0); // cascade
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it('deleteNode prunes selection and focus owned by its ports', () => {
|
|
215
|
+
const store = makeStore([
|
|
216
|
+
createNode({
|
|
217
|
+
id: 'a',
|
|
218
|
+
ports: [
|
|
219
|
+
createPort({
|
|
220
|
+
name: 'out',
|
|
221
|
+
side: 'right',
|
|
222
|
+
direction: 'out',
|
|
223
|
+
x: 0,
|
|
224
|
+
y: 0,
|
|
225
|
+
}),
|
|
226
|
+
],
|
|
227
|
+
}),
|
|
228
|
+
]);
|
|
229
|
+
store.trigger.select({ ids: ['a:out'] });
|
|
230
|
+
store.trigger.focus({ entityId: 'a:out' });
|
|
231
|
+
|
|
232
|
+
store.trigger.deleteNode({ id: 'a' });
|
|
233
|
+
|
|
234
|
+
expect([...ctx(store).selection]).toEqual([]);
|
|
235
|
+
expect(ctx(store).focusedEntityId).toBeNull();
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it('deleteNode invalidates routes attached to a changed derived group', () => {
|
|
239
|
+
const store = makeStore(
|
|
240
|
+
[
|
|
241
|
+
createNode({ id: 'group', group: { type: 'derived', padding: 10 } }),
|
|
242
|
+
createNode({ id: 'child', parentId: 'group', x: 20, y: 20 }),
|
|
243
|
+
createNode({ id: 'target', x: 300 }),
|
|
244
|
+
],
|
|
245
|
+
[
|
|
246
|
+
createEdge({
|
|
247
|
+
id: 'group-target',
|
|
248
|
+
sourceId: 'group',
|
|
249
|
+
targetId: 'target',
|
|
250
|
+
points: [{ x: 100, y: 25 }, { x: 300, y: 25 }],
|
|
251
|
+
routing: 'orthogonal',
|
|
252
|
+
svgPath: 'M 100 25 L 300 25',
|
|
253
|
+
}),
|
|
254
|
+
],
|
|
255
|
+
);
|
|
256
|
+
|
|
257
|
+
store.trigger.deleteNode({ id: 'child' });
|
|
258
|
+
|
|
259
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('points');
|
|
260
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('routing');
|
|
261
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('svgPath');
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it('createNode invalidates routes attached to a changed derived group', () => {
|
|
265
|
+
const store = makeStore(
|
|
266
|
+
[
|
|
267
|
+
createNode({ id: 'group', group: { type: 'derived', padding: 10 } }),
|
|
268
|
+
createNode({ id: 'target', x: 300 }),
|
|
269
|
+
],
|
|
270
|
+
[
|
|
271
|
+
createEdge({
|
|
272
|
+
id: 'group-target',
|
|
273
|
+
sourceId: 'group',
|
|
274
|
+
targetId: 'target',
|
|
275
|
+
points: [{ x: 100, y: 25 }, { x: 300, y: 25 }],
|
|
276
|
+
routing: 'orthogonal',
|
|
277
|
+
svgPath: 'M 100 25 L 300 25',
|
|
278
|
+
}),
|
|
279
|
+
],
|
|
280
|
+
);
|
|
281
|
+
|
|
282
|
+
store.trigger.createNode({
|
|
283
|
+
node: createNode({ id: 'child', parentId: 'group', x: 20, y: 20 }),
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('points');
|
|
287
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('routing');
|
|
288
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('svgPath');
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
it('preserves addressable port state across retained graph replacement', () => {
|
|
292
|
+
const graph = createGraph({
|
|
293
|
+
nodes: [
|
|
294
|
+
createNode({
|
|
295
|
+
id: 'a',
|
|
296
|
+
ports: [
|
|
297
|
+
createPort({
|
|
298
|
+
name: 'out',
|
|
299
|
+
side: 'right',
|
|
300
|
+
direction: 'out',
|
|
301
|
+
x: 0,
|
|
302
|
+
y: 0,
|
|
303
|
+
}),
|
|
304
|
+
],
|
|
305
|
+
}),
|
|
306
|
+
],
|
|
307
|
+
edges: [],
|
|
308
|
+
});
|
|
309
|
+
const store = createFlowStore(graph);
|
|
310
|
+
store.trigger.select({ ids: ['a:out'] });
|
|
311
|
+
store.trigger.focus({ entityId: 'a:out' });
|
|
312
|
+
|
|
313
|
+
store.trigger.replaceGraph({
|
|
314
|
+
graph: createGraph({ nodes: graph.nodes, edges: [] }),
|
|
315
|
+
retention: 'preserve',
|
|
316
|
+
history: 'ignore',
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
expect([...ctx(store).selection]).toEqual(['a:out']);
|
|
320
|
+
expect(ctx(store).focusedEntityId).toBe('a:out');
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it('createEdge adds an edge', () => {
|
|
324
|
+
const store = makeStore([createNode({ id: 'a' }), createNode({ id: 'b' })]);
|
|
325
|
+
store.trigger.createEdge({
|
|
326
|
+
edge: createEdge({ id: 'e1', sourceId: 'a', targetId: 'b' }),
|
|
327
|
+
});
|
|
328
|
+
expect(ctx(store).graph.edges).toHaveLength(1);
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
it('undo reverts whitelisted graph mutations and redo restores them', () => {
|
|
332
|
+
const store = makeStore();
|
|
333
|
+
|
|
334
|
+
store.trigger.createNode({ node: createNode({ id: 'n1', x: 50, y: 60 }) });
|
|
335
|
+
expect(ctx(store).graph.nodes.map((node) => node.id)).toEqual(['n1']);
|
|
336
|
+
|
|
337
|
+
undo(store);
|
|
338
|
+
expect(ctx(store).graph.nodes).toHaveLength(0);
|
|
339
|
+
|
|
340
|
+
redo(store);
|
|
341
|
+
expect(ctx(store).graph.nodes.map((node) => node.id)).toEqual(['n1']);
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
it('does not track non-whitelisted interaction events', () => {
|
|
345
|
+
const store = makeStore([createNode({ id: 'a' }), createNode({ id: 'b' })]);
|
|
346
|
+
|
|
347
|
+
store.trigger.select({ ids: ['a'] });
|
|
348
|
+
expect(ctx(store).selection.has('a')).toBe(true);
|
|
349
|
+
|
|
350
|
+
undo(store);
|
|
351
|
+
expect(ctx(store).selection.has('a')).toBe(true);
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
it('groups whitelisted events in the same transaction', () => {
|
|
355
|
+
const store = makeStore();
|
|
356
|
+
|
|
357
|
+
const transactionId = 'create-pair';
|
|
358
|
+
store.trigger.createNode({ node: createNode({ id: 'a', x: 0, y: 0 }) });
|
|
359
|
+
store.trigger.createNode({
|
|
360
|
+
node: createNode({ id: 'b', x: 100, y: 0 }),
|
|
361
|
+
transactionId,
|
|
362
|
+
});
|
|
363
|
+
store.trigger.createNode({
|
|
364
|
+
node: createNode({ id: 'c', x: 200, y: 0 }),
|
|
365
|
+
transactionId,
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
expect(ctx(store).graph.nodes.map((node) => node.id)).toEqual(['a', 'b', 'c']);
|
|
369
|
+
|
|
370
|
+
undo(store);
|
|
371
|
+
expect(ctx(store).graph.nodes.map((node) => node.id)).toEqual(['a']);
|
|
372
|
+
|
|
373
|
+
redo(store);
|
|
374
|
+
expect(ctx(store).graph.nodes.map((node) => node.id)).toEqual(['a', 'b', 'c']);
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
it('undo restores entities removed by deleteSelected', () => {
|
|
378
|
+
const a = createNode({ id: 'a' });
|
|
379
|
+
const b = createNode({ id: 'b' });
|
|
380
|
+
const e = createEdge({ id: 'e1', sourceId: 'a', targetId: 'b' });
|
|
381
|
+
const store = makeStore([a, b], [e]);
|
|
382
|
+
|
|
383
|
+
store.trigger.select({ ids: ['a'] });
|
|
384
|
+
store.trigger.deleteSelected();
|
|
385
|
+
|
|
386
|
+
expect(ctx(store).graph.nodes.map((node) => node.id)).toEqual(['b']);
|
|
387
|
+
expect(ctx(store).graph.edges).toHaveLength(0);
|
|
388
|
+
|
|
389
|
+
undo(store);
|
|
390
|
+
|
|
391
|
+
expect(ctx(store).graph.nodes.map((node) => node.id)).toEqual(['a', 'b']);
|
|
392
|
+
expect(ctx(store).graph.edges.map((edge) => edge.id)).toEqual(['e1']);
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
it('keeps a port-only selection when deleteSelected has no entities', () => {
|
|
396
|
+
const store = makeStore([
|
|
397
|
+
createNode({
|
|
398
|
+
id: 'a',
|
|
399
|
+
ports: [
|
|
400
|
+
createPort({
|
|
401
|
+
name: 'out',
|
|
402
|
+
side: 'right',
|
|
403
|
+
direction: 'out',
|
|
404
|
+
x: 0,
|
|
405
|
+
y: 0,
|
|
406
|
+
}),
|
|
407
|
+
],
|
|
408
|
+
}),
|
|
409
|
+
]);
|
|
410
|
+
const deleted: unknown[] = [];
|
|
411
|
+
const changed: unknown[] = [];
|
|
412
|
+
store.on('entitiesDeleted', (event) => deleted.push(event));
|
|
413
|
+
store.on('graphChanged', (event) => changed.push(event));
|
|
414
|
+
store.trigger.select({ ids: ['a:out'] });
|
|
415
|
+
store.trigger.focus({ entityId: 'a:out' });
|
|
416
|
+
|
|
417
|
+
store.trigger.deleteSelected();
|
|
418
|
+
|
|
419
|
+
expect([...ctx(store).selection]).toEqual(['a:out']);
|
|
420
|
+
expect(ctx(store).focusedEntityId).toBe('a:out');
|
|
421
|
+
expect(deleted).toEqual([]);
|
|
422
|
+
expect(changed).toEqual([]);
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
it('does not track deleteSelected when nothing is selected', () => {
|
|
426
|
+
const store = makeStore([createNode({ id: 'a' })]);
|
|
427
|
+
|
|
428
|
+
expect(history(store).past).toBe(0);
|
|
429
|
+
store.trigger.deleteSelected();
|
|
430
|
+
|
|
431
|
+
expect(history(store).past).toBe(0);
|
|
432
|
+
undo(store);
|
|
433
|
+
expect(ctx(store).graph.nodes.map((node) => node.id)).toEqual(['a']);
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
it('does not track commitDrag when nothing actually moved', () => {
|
|
437
|
+
const store = makeStore([createNode({ id: 'a' })]);
|
|
438
|
+
|
|
439
|
+
store.trigger.select({ ids: ['a'] });
|
|
440
|
+
store.trigger.dragStart({ id: 'a' });
|
|
441
|
+
store.trigger.dragEnd();
|
|
442
|
+
store.trigger.commitDrag();
|
|
443
|
+
|
|
444
|
+
expect(history(store).past).toBe(0);
|
|
445
|
+
undo(store);
|
|
446
|
+
expect(ctx(store).graph.nodes[0].x).toBe(0);
|
|
447
|
+
expect(ctx(store).selection.has('a')).toBe(true);
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
it('does not create history for cancelled drag or resize gestures', () => {
|
|
451
|
+
const store = makeStore([createNode({ id: 'a', x: 0, y: 0, width: 100, height: 80 })]);
|
|
452
|
+
|
|
453
|
+
store.trigger.select({ ids: ['a'] });
|
|
454
|
+
store.trigger.dragStart({ id: 'a', transactionId: 'drag-a' });
|
|
455
|
+
store.trigger.dragMove({ dx: 20, dy: 10 });
|
|
456
|
+
store.trigger.dragCancel();
|
|
457
|
+
expect(history(store).past).toBe(0);
|
|
458
|
+
|
|
459
|
+
store.trigger.resizeStart({
|
|
460
|
+
id: 'a',
|
|
461
|
+
handle: 'bottom-right',
|
|
462
|
+
transactionId: 'resize-a',
|
|
463
|
+
});
|
|
464
|
+
store.trigger.resizeMove({ x: 160, y: 120 });
|
|
465
|
+
store.trigger.resizeCancel();
|
|
466
|
+
expect(history(store).past).toBe(0);
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
it('preserves orthogonal session state and emits graph changes on restore', () => {
|
|
470
|
+
const store = makeStore([createNode({ id: 'a' }), createNode({ id: 'b', x: 100 })]);
|
|
471
|
+
const changes: string[] = [];
|
|
472
|
+
store.on('graphChanged', ({ event }) => changes.push(event.type));
|
|
473
|
+
|
|
474
|
+
store.trigger.createNode({ node: createNode({ id: 'created', x: 200 }) });
|
|
475
|
+
store.trigger.setViewport({ x: 30, y: 40, zoom: 2 });
|
|
476
|
+
store.trigger.select({ ids: ['b'] });
|
|
477
|
+
store.trigger.focus({ entityId: 'b' });
|
|
478
|
+
store.trigger.undo();
|
|
479
|
+
|
|
480
|
+
expect(ctx(store).graph.nodes.map((node) => node.id)).toEqual(['a', 'b']);
|
|
481
|
+
expect(ctx(store).viewport).toEqual({ x: 30, y: 40, zoom: 2 });
|
|
482
|
+
expect([...ctx(store).selection]).toEqual(['b']);
|
|
483
|
+
expect(ctx(store).focusedEntityId).toBe('b');
|
|
484
|
+
expect(changes.at(-1)).toBe('undo');
|
|
485
|
+
|
|
486
|
+
store.trigger.redo();
|
|
487
|
+
expect(ctx(store).graph.nodes.map((node) => node.id)).toEqual(['a', 'b', 'created']);
|
|
488
|
+
expect(ctx(store).viewport).toEqual({ x: 30, y: 40, zoom: 2 });
|
|
489
|
+
expect(changes.at(-1)).toBe('redo');
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
it('does not track reconnectEdge when the endpoints are unchanged', () => {
|
|
493
|
+
const edge = createEdge({ id: 'e1', sourceId: 'a', targetId: 'b' });
|
|
494
|
+
const store = makeStore([createNode({ id: 'a' }), createNode({ id: 'b' })], [edge]);
|
|
495
|
+
|
|
496
|
+
store.trigger.reconnectEdge({ edgeId: 'e1', newSourceId: 'a', newTargetId: 'b' });
|
|
497
|
+
|
|
498
|
+
expect(history(store).past).toBe(0);
|
|
499
|
+
undo(store);
|
|
500
|
+
expect(ctx(store).graph.edges[0].sourceId).toBe('a');
|
|
501
|
+
expect(ctx(store).graph.edges[0].targetId).toBe('b');
|
|
502
|
+
});
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
// Selection
|
|
506
|
+
|
|
507
|
+
describe('selection', () => {
|
|
508
|
+
it('select single clears previous', () => {
|
|
509
|
+
const store = makeStore([createNode({ id: 'a' }), createNode({ id: 'b' })]);
|
|
510
|
+
store.trigger.select({ ids: ['a'] });
|
|
511
|
+
expect(ctx(store).selection.has('a')).toBe(true);
|
|
512
|
+
store.trigger.select({ ids: ['b'] });
|
|
513
|
+
expect(ctx(store).selection.has('a')).toBe(false);
|
|
514
|
+
expect(ctx(store).selection.has('b')).toBe(true);
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
it('select multi adds to selection', () => {
|
|
518
|
+
const store = makeStore([createNode({ id: 'a' }), createNode({ id: 'b' })]);
|
|
519
|
+
store.trigger.select({ ids: ['a'] });
|
|
520
|
+
store.trigger.select({ ids: ['b'], mode: 'multi' });
|
|
521
|
+
expect(ctx(store).selection.has('a')).toBe(true);
|
|
522
|
+
expect(ctx(store).selection.has('b')).toBe(true);
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
it('multi-select toggles off when all ids already selected', () => {
|
|
526
|
+
const store = makeStore([createNode({ id: 'a' })]);
|
|
527
|
+
store.trigger.select({ ids: ['a'] });
|
|
528
|
+
store.trigger.select({ ids: ['a'], mode: 'multi' });
|
|
529
|
+
expect(ctx(store).selection.has('a')).toBe(false);
|
|
530
|
+
});
|
|
531
|
+
|
|
532
|
+
it('selectAll selects everything', () => {
|
|
533
|
+
const store = makeStore(
|
|
534
|
+
[createNode({ id: 'a' }), createNode({ id: 'b' })],
|
|
535
|
+
[createEdge({ id: 'e1', sourceId: 'a', targetId: 'b' })]
|
|
536
|
+
);
|
|
537
|
+
store.trigger.selectAll();
|
|
538
|
+
const s = ctx(store).selection;
|
|
539
|
+
expect(s.has('a')).toBe(true);
|
|
540
|
+
expect(s.has('b')).toBe(true);
|
|
541
|
+
expect(s.has('e1')).toBe(true);
|
|
542
|
+
});
|
|
543
|
+
|
|
544
|
+
it('selectAll skips hidden nodes and their connected edges', () => {
|
|
545
|
+
const store = makeStore(
|
|
546
|
+
[createNode({ id: 'a', hidden: true }), createNode({ id: 'b' }), createNode({ id: 'c' })],
|
|
547
|
+
[
|
|
548
|
+
createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' }),
|
|
549
|
+
createEdge({ id: 'bc', sourceId: 'b', targetId: 'c' }),
|
|
550
|
+
]
|
|
551
|
+
);
|
|
552
|
+
|
|
553
|
+
store.trigger.selectAll();
|
|
554
|
+
|
|
555
|
+
expect([...ctx(store).selection].sort()).toEqual(['b', 'bc', 'c']);
|
|
556
|
+
});
|
|
557
|
+
|
|
558
|
+
it('clearSelection empties selection', () => {
|
|
559
|
+
const store = makeStore([createNode({ id: 'a' })]);
|
|
560
|
+
store.trigger.select({ ids: ['a'] });
|
|
561
|
+
store.trigger.clearSelection();
|
|
562
|
+
expect(ctx(store).selection.size).toBe(0);
|
|
563
|
+
});
|
|
564
|
+
|
|
565
|
+
it('can select edges (including those with labels)', () => {
|
|
566
|
+
const edge = createEdge({
|
|
567
|
+
id: 'e1',
|
|
568
|
+
sourceId: 'a',
|
|
569
|
+
targetId: 'b',
|
|
570
|
+
label: { x: 50, y: 50, width: 60, height: 20, data: {} },
|
|
571
|
+
});
|
|
572
|
+
const store = makeStore([createNode({ id: 'a' }), createNode({ id: 'b' })], [edge]);
|
|
573
|
+
store.trigger.select({ ids: ['e1'] });
|
|
574
|
+
expect(ctx(store).selection.has('e1')).toBe(true);
|
|
575
|
+
});
|
|
576
|
+
});
|
|
577
|
+
|
|
578
|
+
// Box selection includes edge labels
|
|
579
|
+
|
|
580
|
+
describe('box selection', () => {
|
|
581
|
+
it('selects nodes in box', () => {
|
|
582
|
+
const store = makeStore([
|
|
583
|
+
createNode({ id: 'a', x: 10, y: 10, width: 20, height: 20 }),
|
|
584
|
+
createNode({ id: 'b', x: 500, y: 500, width: 20, height: 20 }),
|
|
585
|
+
]);
|
|
586
|
+
store.trigger.selectionBoxStart({ point: { x: 0, y: 0 } });
|
|
587
|
+
store.trigger.selectionBoxMove({ point: { x: 50, y: 50 } });
|
|
588
|
+
store.trigger.selectionBoxEnd();
|
|
589
|
+
expect(ctx(store).selection.has('a')).toBe(true);
|
|
590
|
+
expect(ctx(store).selection.has('b')).toBe(false);
|
|
591
|
+
});
|
|
592
|
+
|
|
593
|
+
it('uses a node selection rectangle instead of its full visual family', () => {
|
|
594
|
+
const store = makeStore([
|
|
595
|
+
createNode({
|
|
596
|
+
id: 'parent',
|
|
597
|
+
x: 0,
|
|
598
|
+
y: 0,
|
|
599
|
+
width: 300,
|
|
600
|
+
height: 240,
|
|
601
|
+
selectionBounds: { x: 0, y: 0, width: 300, height: 60 },
|
|
602
|
+
}),
|
|
603
|
+
]);
|
|
604
|
+
|
|
605
|
+
store.trigger.selectionBoxStart({ point: { x: 20, y: 100 } });
|
|
606
|
+
store.trigger.selectionBoxMove({ point: { x: 280, y: 220 } });
|
|
607
|
+
store.trigger.selectionBoxEnd();
|
|
608
|
+
expect(ctx(store).selection.has('parent')).toBe(false);
|
|
609
|
+
|
|
610
|
+
store.trigger.selectionBoxStart({ point: { x: 20, y: 20 } });
|
|
611
|
+
store.trigger.selectionBoxMove({ point: { x: 280, y: 40 } });
|
|
612
|
+
store.trigger.selectionBoxEnd();
|
|
613
|
+
expect(ctx(store).selection.has('parent')).toBe(true);
|
|
614
|
+
});
|
|
615
|
+
|
|
616
|
+
it('selects a derived group by its content block, not its child area', () => {
|
|
617
|
+
const store = makeStore([
|
|
618
|
+
createNode({
|
|
619
|
+
id: 'parent',
|
|
620
|
+
x: 0,
|
|
621
|
+
y: 0,
|
|
622
|
+
width: 100,
|
|
623
|
+
height: 60,
|
|
624
|
+
group: { type: 'derived', content: 'top', padding: 20 },
|
|
625
|
+
}),
|
|
626
|
+
createNode({
|
|
627
|
+
id: 'child',
|
|
628
|
+
parentId: 'parent',
|
|
629
|
+
x: 100,
|
|
630
|
+
y: 120,
|
|
631
|
+
width: 100,
|
|
632
|
+
height: 60,
|
|
633
|
+
}),
|
|
634
|
+
]);
|
|
635
|
+
|
|
636
|
+
store.trigger.selectionBoxStart({ point: { x: 90, y: 110 } });
|
|
637
|
+
store.trigger.selectionBoxMove({ point: { x: 210, y: 190 } });
|
|
638
|
+
store.trigger.selectionBoxEnd();
|
|
639
|
+
expect(ctx(store).selection.has('parent')).toBe(false);
|
|
640
|
+
expect(ctx(store).selection.has('child')).toBe(true);
|
|
641
|
+
|
|
642
|
+
store.trigger.selectionBoxStart({ point: { x: 90, y: 40 } });
|
|
643
|
+
store.trigger.selectionBoxMove({ point: { x: 210, y: 90 } });
|
|
644
|
+
store.trigger.selectionBoxEnd();
|
|
645
|
+
expect(ctx(store).selection.has('parent')).toBe(true);
|
|
646
|
+
});
|
|
647
|
+
|
|
648
|
+
it('selects a collapsed derived group by its visible header, not its hidden child area', () => {
|
|
649
|
+
const store = makeStore([
|
|
650
|
+
createNode({
|
|
651
|
+
id: 'parent',
|
|
652
|
+
x: 0,
|
|
653
|
+
y: 0,
|
|
654
|
+
width: 100,
|
|
655
|
+
height: 60,
|
|
656
|
+
group: { type: 'derived', content: 'top', padding: 20 },
|
|
657
|
+
}),
|
|
658
|
+
createNode({
|
|
659
|
+
id: 'child',
|
|
660
|
+
parentId: 'parent',
|
|
661
|
+
x: 100,
|
|
662
|
+
y: 200,
|
|
663
|
+
width: 100,
|
|
664
|
+
height: 60,
|
|
665
|
+
}),
|
|
666
|
+
]);
|
|
667
|
+
|
|
668
|
+
store.trigger.collapseNode({ id: 'parent' });
|
|
669
|
+
|
|
670
|
+
store.trigger.selectionBoxStart({ point: { x: 0, y: 0 } });
|
|
671
|
+
store.trigger.selectionBoxMove({ point: { x: 100, y: 60 } });
|
|
672
|
+
store.trigger.selectionBoxEnd();
|
|
673
|
+
expect(ctx(store).selection.has('parent')).toBe(true);
|
|
674
|
+
expect(ctx(store).selection.has('child')).toBe(false);
|
|
675
|
+
|
|
676
|
+
store.trigger.selectionBoxStart({ point: { x: 85, y: 125 } });
|
|
677
|
+
store.trigger.selectionBoxMove({ point: { x: 200, y: 175 } });
|
|
678
|
+
store.trigger.selectionBoxEnd();
|
|
679
|
+
expect(ctx(store).selection.has('parent')).toBe(false);
|
|
680
|
+
expect(ctx(store).selection.has('child')).toBe(false);
|
|
681
|
+
});
|
|
682
|
+
|
|
683
|
+
it('keeps a collapsed derived ancestor bounded by its content block with a derived child', () => {
|
|
684
|
+
const store = makeStore([
|
|
685
|
+
createNode({
|
|
686
|
+
id: 'parent',
|
|
687
|
+
x: 0,
|
|
688
|
+
y: 0,
|
|
689
|
+
width: 100,
|
|
690
|
+
height: 60,
|
|
691
|
+
group: { type: 'derived', content: 'top', padding: 20 },
|
|
692
|
+
}),
|
|
693
|
+
createNode({
|
|
694
|
+
id: 'child-group',
|
|
695
|
+
parentId: 'parent',
|
|
696
|
+
x: 100,
|
|
697
|
+
y: 200,
|
|
698
|
+
width: 100,
|
|
699
|
+
height: 60,
|
|
700
|
+
group: { type: 'derived', content: 'top', padding: 20 },
|
|
701
|
+
}),
|
|
702
|
+
createNode({
|
|
703
|
+
id: 'grandchild',
|
|
704
|
+
parentId: 'child-group',
|
|
705
|
+
x: 200,
|
|
706
|
+
y: 400,
|
|
707
|
+
width: 100,
|
|
708
|
+
height: 60,
|
|
709
|
+
}),
|
|
710
|
+
]);
|
|
711
|
+
|
|
712
|
+
store.trigger.collapseNode({ id: 'parent' });
|
|
713
|
+
|
|
714
|
+
store.trigger.selectionBoxStart({ point: { x: 0, y: 0 } });
|
|
715
|
+
store.trigger.selectionBoxMove({ point: { x: 100, y: 60 } });
|
|
716
|
+
store.trigger.selectionBoxEnd();
|
|
717
|
+
expect(ctx(store).selection.has('parent')).toBe(true);
|
|
718
|
+
expect(ctx(store).selection.has('child-group')).toBe(false);
|
|
719
|
+
expect(ctx(store).selection.has('grandchild')).toBe(false);
|
|
720
|
+
});
|
|
721
|
+
|
|
722
|
+
it('selects edges whose labels are in box', () => {
|
|
723
|
+
const edge = createEdge({
|
|
724
|
+
id: 'e1',
|
|
725
|
+
sourceId: 'a',
|
|
726
|
+
targetId: 'b',
|
|
727
|
+
label: { x: 25, y: 25, width: 10, height: 10, data: {} },
|
|
728
|
+
});
|
|
729
|
+
const store = makeStore(
|
|
730
|
+
[
|
|
731
|
+
createNode({ id: 'a', x: 200, y: 200, width: 20, height: 20 }),
|
|
732
|
+
createNode({ id: 'b', x: 300, y: 300, width: 20, height: 20 }),
|
|
733
|
+
],
|
|
734
|
+
[edge]
|
|
735
|
+
);
|
|
736
|
+
store.trigger.selectionBoxStart({ point: { x: 0, y: 0 } });
|
|
737
|
+
store.trigger.selectionBoxMove({ point: { x: 50, y: 50 } });
|
|
738
|
+
store.trigger.selectionBoxEnd();
|
|
739
|
+
// Edge label is at (25,25) w=10 h=10, box is (0,0)-(50,50) → intersects
|
|
740
|
+
expect(ctx(store).selection.has('e1')).toBe(true);
|
|
741
|
+
// Nodes are outside box
|
|
742
|
+
expect(ctx(store).selection.has('a')).toBe(false);
|
|
743
|
+
});
|
|
744
|
+
|
|
745
|
+
it('does NOT auto-select edge when both endpoints are selected', () => {
|
|
746
|
+
const edge = createEdge({
|
|
747
|
+
id: 'e1',
|
|
748
|
+
sourceId: 'a',
|
|
749
|
+
targetId: 'b',
|
|
750
|
+
});
|
|
751
|
+
const store = makeStore(
|
|
752
|
+
[
|
|
753
|
+
createNode({ id: 'a', x: 10, y: 10, width: 20, height: 20 }),
|
|
754
|
+
createNode({ id: 'b', x: 30, y: 30, width: 20, height: 20 }),
|
|
755
|
+
],
|
|
756
|
+
[edge]
|
|
757
|
+
);
|
|
758
|
+
store.trigger.selectionBoxStart({ point: { x: 0, y: 0 } });
|
|
759
|
+
store.trigger.selectionBoxMove({ point: { x: 100, y: 100 } });
|
|
760
|
+
store.trigger.selectionBoxEnd();
|
|
761
|
+
// Both nodes selected but edge (no label) should NOT be auto-selected
|
|
762
|
+
expect(ctx(store).selection.has('a')).toBe(true);
|
|
763
|
+
expect(ctx(store).selection.has('b')).toBe(true);
|
|
764
|
+
expect(ctx(store).selection.has('e1')).toBe(false);
|
|
765
|
+
});
|
|
766
|
+
|
|
767
|
+
it('does not select an unlabeled edge when its path intersects the box', () => {
|
|
768
|
+
const edge = createEdge({
|
|
769
|
+
id: 'e1',
|
|
770
|
+
sourceId: 'a',
|
|
771
|
+
targetId: 'b',
|
|
772
|
+
});
|
|
773
|
+
const store = makeStore(
|
|
774
|
+
[
|
|
775
|
+
createNode({ id: 'a', x: 0, y: 0, width: 50, height: 50 }),
|
|
776
|
+
createNode({ id: 'b', x: 200, y: 0, width: 50, height: 50 }),
|
|
777
|
+
],
|
|
778
|
+
[edge]
|
|
779
|
+
);
|
|
780
|
+
|
|
781
|
+
store.trigger.selectionBoxStart({ point: { x: 100, y: 20 } });
|
|
782
|
+
store.trigger.selectionBoxMove({ point: { x: 150, y: 40 } });
|
|
783
|
+
store.trigger.selectionBoxEnd();
|
|
784
|
+
|
|
785
|
+
expect(ctx(store).selection.has('e1')).toBe(false);
|
|
786
|
+
});
|
|
787
|
+
|
|
788
|
+
it('does not select a labeled edge when only its path intersects the box', () => {
|
|
789
|
+
const edge = createEdge({
|
|
790
|
+
id: 'e1',
|
|
791
|
+
sourceId: 'a',
|
|
792
|
+
targetId: 'b',
|
|
793
|
+
label: { x: 300, y: 0, width: 50, height: 20, data: {} },
|
|
794
|
+
});
|
|
795
|
+
const store = makeStore(
|
|
796
|
+
[
|
|
797
|
+
createNode({ id: 'a', x: 0, y: 0, width: 50, height: 50 }),
|
|
798
|
+
createNode({ id: 'b', x: 200, y: 0, width: 50, height: 50 }),
|
|
799
|
+
],
|
|
800
|
+
[edge]
|
|
801
|
+
);
|
|
802
|
+
|
|
803
|
+
store.trigger.selectionBoxStart({ point: { x: 100, y: 20 } });
|
|
804
|
+
store.trigger.selectionBoxMove({ point: { x: 150, y: 40 } });
|
|
805
|
+
store.trigger.selectionBoxEnd();
|
|
806
|
+
|
|
807
|
+
expect(ctx(store).selection.has('e1')).toBe(false);
|
|
808
|
+
});
|
|
809
|
+
|
|
810
|
+
it('supports full selection mode for box select', () => {
|
|
811
|
+
const store = makeStore(
|
|
812
|
+
[
|
|
813
|
+
createNode({ id: 'inside', x: 10, y: 10, width: 20, height: 20 }),
|
|
814
|
+
createNode({ id: 'partial', x: 40, y: 40, width: 20, height: 20 }),
|
|
815
|
+
],
|
|
816
|
+
[],
|
|
817
|
+
{ selectionMode: 'full' }
|
|
818
|
+
);
|
|
819
|
+
|
|
820
|
+
store.trigger.selectionBoxStart({ point: { x: 0, y: 0 } });
|
|
821
|
+
store.trigger.selectionBoxMove({ point: { x: 50, y: 50 } });
|
|
822
|
+
store.trigger.selectionBoxEnd();
|
|
823
|
+
|
|
824
|
+
expect(ctx(store).selection.has('inside')).toBe(true);
|
|
825
|
+
expect(ctx(store).selection.has('partial')).toBe(false);
|
|
826
|
+
});
|
|
827
|
+
|
|
828
|
+
it('updates the selection live as the box grows and shrinks', () => {
|
|
829
|
+
const store = makeStore([
|
|
830
|
+
createNode({ id: 'a', x: 10, y: 10, width: 20, height: 20 }),
|
|
831
|
+
createNode({ id: 'b', x: 100, y: 10, width: 20, height: 20 }),
|
|
832
|
+
]);
|
|
833
|
+
store.trigger.selectionBoxStart({ point: { x: 0, y: 0 } });
|
|
834
|
+
store.trigger.selectionBoxMove({ point: { x: 40, y: 40 } });
|
|
835
|
+
// selection reflects the box mid-drag, before end
|
|
836
|
+
expect(ctx(store).selection.has('a')).toBe(true);
|
|
837
|
+
expect(ctx(store).selection.has('b')).toBe(false);
|
|
838
|
+
store.trigger.selectionBoxMove({ point: { x: 130, y: 40 } });
|
|
839
|
+
expect(ctx(store).selection.has('b')).toBe(true);
|
|
840
|
+
// shrinking the box deselects what it no longer covers
|
|
841
|
+
store.trigger.selectionBoxMove({ point: { x: 40, y: 40 } });
|
|
842
|
+
expect(ctx(store).selection.has('b')).toBe(false);
|
|
843
|
+
store.trigger.selectionBoxEnd();
|
|
844
|
+
expect(ctx(store).selection.has('a')).toBe(true);
|
|
845
|
+
});
|
|
846
|
+
|
|
847
|
+
it('preserves the order entities enter a growing box', () => {
|
|
848
|
+
const store = makeStore([
|
|
849
|
+
// Deliberately reverse graph order so it cannot determine selection order.
|
|
850
|
+
createNode({ id: 'second', x: 10, y: 100, width: 20, height: 20 }),
|
|
851
|
+
createNode({ id: 'first', x: 10, y: 10, width: 20, height: 20 }),
|
|
852
|
+
]);
|
|
853
|
+
|
|
854
|
+
store.trigger.selectionBoxStart({ point: { x: 0, y: 0 } });
|
|
855
|
+
store.trigger.selectionBoxMove({ point: { x: 40, y: 40 } });
|
|
856
|
+
expect([...ctx(store).selection]).toEqual(['first']);
|
|
857
|
+
|
|
858
|
+
store.trigger.selectionBoxMove({ point: { x: 40, y: 130 } });
|
|
859
|
+
expect([...ctx(store).selection]).toEqual(['first', 'second']);
|
|
860
|
+
});
|
|
861
|
+
|
|
862
|
+
it('additive (multi) box keeps the prior selection and adds to it', () => {
|
|
863
|
+
const store = makeStore([
|
|
864
|
+
createNode({ id: 'a', x: 10, y: 10, width: 20, height: 20 }),
|
|
865
|
+
createNode({ id: 'b', x: 100, y: 10, width: 20, height: 20 }),
|
|
866
|
+
]);
|
|
867
|
+
store.trigger.select({ ids: ['b'] });
|
|
868
|
+
store.trigger.selectionBoxStart({ point: { x: 0, y: 0 }, mode: 'multi' });
|
|
869
|
+
store.trigger.selectionBoxMove({ point: { x: 40, y: 40 } });
|
|
870
|
+
store.trigger.selectionBoxEnd();
|
|
871
|
+
expect(ctx(store).selection.has('a')).toBe(true);
|
|
872
|
+
expect(ctx(store).selection.has('b')).toBe(true);
|
|
873
|
+
});
|
|
874
|
+
|
|
875
|
+
it('honors a custom boxSelectionTest predicate', () => {
|
|
876
|
+
// Only select when the box covers the node's center.
|
|
877
|
+
const coversCenter = (box: Rect, b: Rect) => {
|
|
878
|
+
const cx = b.x + b.width / 2;
|
|
879
|
+
const cy = b.y + b.height / 2;
|
|
880
|
+
return cx >= box.x && cx <= box.x + box.width && cy >= box.y && cy <= box.y + box.height;
|
|
881
|
+
};
|
|
882
|
+
const store = makeStore(
|
|
883
|
+
[createNode({ id: 'a', x: 0, y: 0, width: 100, height: 100 })], // center 50,50
|
|
884
|
+
[],
|
|
885
|
+
{ boxSelectionTest: coversCenter }
|
|
886
|
+
);
|
|
887
|
+
store.trigger.selectionBoxStart({ point: { x: -10, y: -10 } });
|
|
888
|
+
store.trigger.selectionBoxMove({ point: { x: 20, y: 20 } }); // overlaps, center not covered
|
|
889
|
+
expect(ctx(store).selection.has('a')).toBe(false);
|
|
890
|
+
store.trigger.selectionBoxMove({ point: { x: 60, y: 60 } }); // now covers center
|
|
891
|
+
expect(ctx(store).selection.has('a')).toBe(true);
|
|
892
|
+
store.trigger.selectionBoxEnd();
|
|
893
|
+
});
|
|
894
|
+
});
|
|
895
|
+
|
|
896
|
+
// Drag + commitDrag
|
|
897
|
+
|
|
898
|
+
describe('drag lifecycle', () => {
|
|
899
|
+
it('dragStart + dragMove + dragEnd + commitDrag bakes position', () => {
|
|
900
|
+
const store = makeStore([createNode({ id: 'a', x: 100, y: 100, width: 50, height: 50 })]);
|
|
901
|
+
store.trigger.select({ ids: ['a'] });
|
|
902
|
+
store.trigger.dragStart({ id: 'a' });
|
|
903
|
+
store.trigger.dragMove({ dx: 30, dy: 20 });
|
|
904
|
+
store.trigger.dragEnd();
|
|
905
|
+
store.trigger.commitDrag();
|
|
906
|
+
const node = ctx(store).graph.nodes[0];
|
|
907
|
+
expect(node.x).toBe(130);
|
|
908
|
+
expect(node.y).toBe(120);
|
|
909
|
+
expect(node.dx).toBe(0);
|
|
910
|
+
expect(node.dy).toBe(0);
|
|
911
|
+
});
|
|
912
|
+
|
|
913
|
+
it('undo/redo restores committed drag without restoring pending dx/dy', () => {
|
|
914
|
+
const store = makeStore([createNode({ id: 'a', x: 100, y: 100 })]);
|
|
915
|
+
const transactionId = 'drag-a';
|
|
916
|
+
|
|
917
|
+
store.trigger.select({ ids: ['a'] });
|
|
918
|
+
store.trigger.dragStart({ id: 'a', transactionId });
|
|
919
|
+
store.trigger.dragMove({ dx: 30, dy: 20 });
|
|
920
|
+
store.trigger.dragEnd();
|
|
921
|
+
store.trigger.commitDrag();
|
|
922
|
+
|
|
923
|
+
expect(ctx(store).graph.nodes[0]).toMatchObject({ x: 130, y: 120, dx: 0, dy: 0 });
|
|
924
|
+
|
|
925
|
+
undo(store);
|
|
926
|
+
expect(ctx(store).graph.nodes[0]).toMatchObject({ x: 100, y: 100 });
|
|
927
|
+
expect(ctx(store).graph.nodes[0].dx ?? 0).toBe(0);
|
|
928
|
+
expect(ctx(store).graph.nodes[0].dy ?? 0).toBe(0);
|
|
929
|
+
|
|
930
|
+
redo(store);
|
|
931
|
+
expect(ctx(store).graph.nodes[0]).toMatchObject({ x: 130, y: 120 });
|
|
932
|
+
expect(ctx(store).graph.nodes[0].dx ?? 0).toBe(0);
|
|
933
|
+
expect(ctx(store).graph.nodes[0].dy ?? 0).toBe(0);
|
|
934
|
+
});
|
|
935
|
+
|
|
936
|
+
it('dragMove applies to the current selection', () => {
|
|
937
|
+
const store = makeStore([
|
|
938
|
+
createNode({ id: 'a', x: 100, y: 100 }),
|
|
939
|
+
createNode({ id: 'b', x: 200, y: 200 }),
|
|
940
|
+
]);
|
|
941
|
+
store.trigger.select({ ids: ['a', 'b'] });
|
|
942
|
+
store.trigger.dragStart({ id: 'a' });
|
|
943
|
+
store.trigger.dragMove({ dx: 30, dy: 20 });
|
|
944
|
+
store.trigger.dragEnd();
|
|
945
|
+
store.trigger.commitDrag();
|
|
946
|
+
|
|
947
|
+
const nodes = ctx(store).graph.nodes;
|
|
948
|
+
expect(nodes.find((node) => node.id === 'a')).toMatchObject({
|
|
949
|
+
x: 130,
|
|
950
|
+
y: 120,
|
|
951
|
+
});
|
|
952
|
+
expect(nodes.find((node) => node.id === 'b')).toMatchObject({
|
|
953
|
+
x: 230,
|
|
954
|
+
y: 220,
|
|
955
|
+
});
|
|
956
|
+
});
|
|
957
|
+
|
|
958
|
+
it('does not drag entities with draggable false', () => {
|
|
959
|
+
const store = makeStore([
|
|
960
|
+
createNode({ id: 'a', x: 100, y: 100, draggable: false }),
|
|
961
|
+
createNode({ id: 'b', x: 200, y: 200 }),
|
|
962
|
+
]);
|
|
963
|
+
|
|
964
|
+
store.trigger.dragStart({ id: 'a' });
|
|
965
|
+
store.trigger.dragMove({ dx: 30, dy: 20 });
|
|
966
|
+
|
|
967
|
+
expect(ctx(store).canvasState).toBe('idle');
|
|
968
|
+
expect(ctx(store).graph.nodes.find((node) => node.id === 'a')?.dx).toBeUndefined();
|
|
969
|
+
|
|
970
|
+
store.trigger.select({ ids: ['a', 'b'] });
|
|
971
|
+
store.trigger.dragStart({ id: 'b' });
|
|
972
|
+
store.trigger.dragMove({ dx: 30, dy: 20 });
|
|
973
|
+
|
|
974
|
+
expect(ctx(store).graph.nodes.find((node) => node.id === 'a')?.dx).toBeUndefined();
|
|
975
|
+
expect(ctx(store).graph.nodes.find((node) => node.id === 'b')?.dx).toBe(30);
|
|
976
|
+
});
|
|
977
|
+
|
|
978
|
+
it('commitDrag bakes edge label dx/dy', () => {
|
|
979
|
+
const edge = createEdge({
|
|
980
|
+
id: 'e1',
|
|
981
|
+
sourceId: 'a',
|
|
982
|
+
targetId: 'b',
|
|
983
|
+
label: { x: 50, y: 50, width: 60, height: 20, data: {} },
|
|
984
|
+
});
|
|
985
|
+
const store = makeStore(
|
|
986
|
+
[createNode({ id: 'a' }), createNode({ id: 'b', x: 200, y: 0 })],
|
|
987
|
+
[edge]
|
|
988
|
+
);
|
|
989
|
+
store.trigger.select({ ids: ['e1'] });
|
|
990
|
+
store.trigger.dragStart({ id: 'e1' });
|
|
991
|
+
store.trigger.dragMove({ dx: 10, dy: 5 });
|
|
992
|
+
store.trigger.dragEnd();
|
|
993
|
+
store.trigger.commitDrag();
|
|
994
|
+
const label = ctx(store).graph.edges[0].label!;
|
|
995
|
+
expect(label.x).toBe(60);
|
|
996
|
+
expect(label.y).toBe(55);
|
|
997
|
+
expect(label.dx).toBe(0);
|
|
998
|
+
expect(label.dy).toBe(0);
|
|
999
|
+
});
|
|
1000
|
+
|
|
1001
|
+
it('does not carry unselected fixed labels when endpoints move', () => {
|
|
1002
|
+
const store = makeStore(
|
|
1003
|
+
[createNode({ id: 'a', x: 100, y: 100 })],
|
|
1004
|
+
[
|
|
1005
|
+
createEdge({
|
|
1006
|
+
id: 'branch',
|
|
1007
|
+
sourceId: 'a',
|
|
1008
|
+
targetId: 'a',
|
|
1009
|
+
label: { x: 160, y: 130, width: 60, height: 20, data: {} },
|
|
1010
|
+
}),
|
|
1011
|
+
]
|
|
1012
|
+
);
|
|
1013
|
+
|
|
1014
|
+
store.trigger.select({ ids: ['a'] });
|
|
1015
|
+
store.trigger.dragStart({ id: 'a' });
|
|
1016
|
+
store.trigger.dragMove({ dx: 30, dy: 20 });
|
|
1017
|
+
|
|
1018
|
+
const label = ctx(store).graph.edges[0].label!;
|
|
1019
|
+
expect(label.dx ?? 0).toBe(0);
|
|
1020
|
+
expect(label.dy ?? 0).toBe(0);
|
|
1021
|
+
});
|
|
1022
|
+
|
|
1023
|
+
it('carries owned edge geometry while its owner group moves and undo restores it', () => {
|
|
1024
|
+
const store = makeStore(
|
|
1025
|
+
[
|
|
1026
|
+
createNode({ id: 'group', x: 0, y: 0, width: 260, height: 160 }),
|
|
1027
|
+
createNode({ id: 'a', parentId: 'group', x: 40, y: 40 }),
|
|
1028
|
+
createNode({ id: 'b', parentId: 'group', x: 180, y: 40 }),
|
|
1029
|
+
],
|
|
1030
|
+
[
|
|
1031
|
+
createEdge({
|
|
1032
|
+
id: 'ab',
|
|
1033
|
+
sourceId: 'a',
|
|
1034
|
+
targetId: 'b',
|
|
1035
|
+
label: { x: 120, y: 70, width: 60, height: 20, data: {} },
|
|
1036
|
+
waypoints: [{ x: 120, y: 120 }],
|
|
1037
|
+
points: [{ x: 90, y: 65 }, { x: 210, y: 65 }],
|
|
1038
|
+
routing: 'polyline',
|
|
1039
|
+
svgPath: 'M 90 65 L 210 65',
|
|
1040
|
+
}),
|
|
1041
|
+
]
|
|
1042
|
+
);
|
|
1043
|
+
|
|
1044
|
+
store.trigger.select({ ids: ['group'] });
|
|
1045
|
+
store.trigger.dragStart({ id: 'group', transactionId: 'drag:group' });
|
|
1046
|
+
store.trigger.dragMove({ dx: 10, dy: 5 });
|
|
1047
|
+
|
|
1048
|
+
expect(ctx(store).graph.edges[0]).toMatchObject({
|
|
1049
|
+
dx: 10,
|
|
1050
|
+
dy: 5,
|
|
1051
|
+
label: { dx: 10, dy: 5 },
|
|
1052
|
+
});
|
|
1053
|
+
|
|
1054
|
+
store.trigger.dragEnd();
|
|
1055
|
+
store.trigger.commitDrag();
|
|
1056
|
+
|
|
1057
|
+
expect(ctx(store).graph.edges[0].waypoints).toEqual([{ x: 130, y: 125 }]);
|
|
1058
|
+
expect(ctx(store).graph.edges[0].label).toMatchObject({
|
|
1059
|
+
x: 130,
|
|
1060
|
+
y: 75,
|
|
1061
|
+
dx: 0,
|
|
1062
|
+
dy: 0,
|
|
1063
|
+
});
|
|
1064
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('points');
|
|
1065
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('routing');
|
|
1066
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('svgPath');
|
|
1067
|
+
|
|
1068
|
+
undo(store);
|
|
1069
|
+
expect(ctx(store).graph.edges[0].waypoints).toEqual([{ x: 120, y: 120 }]);
|
|
1070
|
+
expect(ctx(store).graph.edges[0].label).toMatchObject({ x: 120, y: 70 });
|
|
1071
|
+
expect(ctx(store).graph.edges[0].points).toEqual([
|
|
1072
|
+
{ x: 90, y: 65 },
|
|
1073
|
+
{ x: 210, y: 65 },
|
|
1074
|
+
]);
|
|
1075
|
+
expect(ctx(store).graph.edges[0].routing).toBe('polyline');
|
|
1076
|
+
expect(ctx(store).graph.edges[0].svgPath).toBe('M 90 65 L 210 65');
|
|
1077
|
+
});
|
|
1078
|
+
|
|
1079
|
+
it('commits owned edge geometry by the owner node post-snap delta', () => {
|
|
1080
|
+
const store = makeStore(
|
|
1081
|
+
[
|
|
1082
|
+
createNode({ id: 'group', x: 0, y: 0, width: 260, height: 160 }),
|
|
1083
|
+
createNode({ id: 'a', parentId: 'group', x: 20, y: 40 }),
|
|
1084
|
+
createNode({ id: 'b', parentId: 'group', x: 180, y: 40 }),
|
|
1085
|
+
],
|
|
1086
|
+
[
|
|
1087
|
+
createEdge({
|
|
1088
|
+
id: 'ab',
|
|
1089
|
+
sourceId: 'a',
|
|
1090
|
+
targetId: 'b',
|
|
1091
|
+
label: { x: 123, y: 77, width: 60, height: 20, data: {} },
|
|
1092
|
+
waypoints: [{ x: 120, y: 120 }],
|
|
1093
|
+
}),
|
|
1094
|
+
],
|
|
1095
|
+
{ snapToGrid: true, snapGrid: [10, 10] as [number, number] },
|
|
1096
|
+
);
|
|
1097
|
+
|
|
1098
|
+
store.trigger.select({ ids: ['group'] });
|
|
1099
|
+
store.trigger.dragStart({ id: 'group' });
|
|
1100
|
+
store.trigger.dragMove({ dx: 13, dy: 17 });
|
|
1101
|
+
store.trigger.dragEnd();
|
|
1102
|
+
store.trigger.commitDrag();
|
|
1103
|
+
|
|
1104
|
+
expect(ctx(store).graph.nodes.find((node) => node.id === 'group')).toMatchObject({
|
|
1105
|
+
x: 10,
|
|
1106
|
+
y: 20,
|
|
1107
|
+
});
|
|
1108
|
+
expect(ctx(store).graph.edges[0]).toMatchObject({
|
|
1109
|
+
waypoints: [{ x: 130, y: 140 }],
|
|
1110
|
+
label: { x: 133, y: 97, dx: 0, dy: 0 },
|
|
1111
|
+
});
|
|
1112
|
+
});
|
|
1113
|
+
|
|
1114
|
+
it('commitDrag invalidates authoritative routes when an endpoint moves', () => {
|
|
1115
|
+
const store = makeStore(
|
|
1116
|
+
[
|
|
1117
|
+
createNode({ id: 'a', x: 0, y: 0 }),
|
|
1118
|
+
createNode({ id: 'b', x: 200, y: 0 }),
|
|
1119
|
+
],
|
|
1120
|
+
[
|
|
1121
|
+
createEdge({
|
|
1122
|
+
id: 'ab',
|
|
1123
|
+
sourceId: 'a',
|
|
1124
|
+
targetId: 'b',
|
|
1125
|
+
points: [{ x: 50, y: 25 }, { x: 150, y: 25 }],
|
|
1126
|
+
routing: 'orthogonal',
|
|
1127
|
+
svgPath: 'M 50 25 L 150 25',
|
|
1128
|
+
}),
|
|
1129
|
+
],
|
|
1130
|
+
);
|
|
1131
|
+
|
|
1132
|
+
store.trigger.select({ ids: ['a'] });
|
|
1133
|
+
store.trigger.dragStart({ id: 'a' });
|
|
1134
|
+
store.trigger.dragMove({ dx: 20, dy: 10 });
|
|
1135
|
+
store.trigger.dragEnd();
|
|
1136
|
+
store.trigger.commitDrag();
|
|
1137
|
+
|
|
1138
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('points');
|
|
1139
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('routing');
|
|
1140
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('svgPath');
|
|
1141
|
+
});
|
|
1142
|
+
|
|
1143
|
+
it('commitDrag invalidates routes attached to a changed derived group', () => {
|
|
1144
|
+
const store = makeStore(
|
|
1145
|
+
[
|
|
1146
|
+
createNode({ id: 'group', group: { type: 'derived', padding: 10 } }),
|
|
1147
|
+
createNode({ id: 'child', parentId: 'group', x: 20, y: 20 }),
|
|
1148
|
+
createNode({ id: 'target', x: 300, y: 0 }),
|
|
1149
|
+
],
|
|
1150
|
+
[
|
|
1151
|
+
createEdge({
|
|
1152
|
+
id: 'group-target',
|
|
1153
|
+
sourceId: 'group',
|
|
1154
|
+
targetId: 'target',
|
|
1155
|
+
points: [{ x: 100, y: 25 }, { x: 300, y: 25 }],
|
|
1156
|
+
routing: 'orthogonal',
|
|
1157
|
+
svgPath: 'M 100 25 L 300 25',
|
|
1158
|
+
}),
|
|
1159
|
+
],
|
|
1160
|
+
);
|
|
1161
|
+
|
|
1162
|
+
store.trigger.select({ ids: ['child'] });
|
|
1163
|
+
store.trigger.dragStart({ id: 'child' });
|
|
1164
|
+
store.trigger.dragMove({ dx: 50, dy: 0 });
|
|
1165
|
+
store.trigger.dragEnd();
|
|
1166
|
+
store.trigger.commitDrag();
|
|
1167
|
+
|
|
1168
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('points');
|
|
1169
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('routing');
|
|
1170
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('svgPath');
|
|
1171
|
+
});
|
|
1172
|
+
|
|
1173
|
+
it('ignores fixed authoritative points when snapping a moving selection', () => {
|
|
1174
|
+
const store = makeStore(
|
|
1175
|
+
[
|
|
1176
|
+
createNode({ id: 'moving', x: 0, y: 0, width: 50, height: 50 }),
|
|
1177
|
+
createNode({ id: 'target', x: 105, y: 0, width: 50, height: 50 }),
|
|
1178
|
+
],
|
|
1179
|
+
[
|
|
1180
|
+
createEdge({
|
|
1181
|
+
id: 'route',
|
|
1182
|
+
sourceId: 'moving',
|
|
1183
|
+
targetId: 'moving',
|
|
1184
|
+
points: [{ x: -1000, y: -1000 }, { x: 1000, y: 1000 }],
|
|
1185
|
+
}),
|
|
1186
|
+
],
|
|
1187
|
+
{ enableSnapLines: true, snapThreshold: 10 },
|
|
1188
|
+
);
|
|
1189
|
+
|
|
1190
|
+
store.trigger.select({ ids: ['moving', 'route'] });
|
|
1191
|
+
store.trigger.dragStart({ id: 'moving' });
|
|
1192
|
+
store.trigger.dragMove({ dx: 50, dy: 0 });
|
|
1193
|
+
store.trigger.dragEnd();
|
|
1194
|
+
store.trigger.commitDrag();
|
|
1195
|
+
|
|
1196
|
+
expect(ctx(store).graph.nodes.find((node) => node.id === 'moving')?.x).toBe(55);
|
|
1197
|
+
});
|
|
1198
|
+
|
|
1199
|
+
it('updateMeasurements updates edge label dimensions by edge id', () => {
|
|
1200
|
+
const edge = createEdge({
|
|
1201
|
+
id: 'e1',
|
|
1202
|
+
sourceId: 'a',
|
|
1203
|
+
targetId: 'b',
|
|
1204
|
+
label: { x: 50, y: 50, width: 60, height: 20, data: {} },
|
|
1205
|
+
});
|
|
1206
|
+
const store = makeStore(
|
|
1207
|
+
[createNode({ id: 'a' }), createNode({ id: 'b', x: 200, y: 0 })],
|
|
1208
|
+
[edge]
|
|
1209
|
+
);
|
|
1210
|
+
|
|
1211
|
+
store.trigger.updateMeasurements({
|
|
1212
|
+
measurements: [{ id: 'e1', rect: { x: 50, y: 50, width: 120, height: 40 } }],
|
|
1213
|
+
});
|
|
1214
|
+
|
|
1215
|
+
const label = ctx(store).graph.edges[0].label!;
|
|
1216
|
+
expect(label.width).toBe(120);
|
|
1217
|
+
expect(label.height).toBe(40);
|
|
1218
|
+
});
|
|
1219
|
+
|
|
1220
|
+
it('keeps auto-size edge label measurements out of authored dimensions', () => {
|
|
1221
|
+
const edge = createEdge({
|
|
1222
|
+
id: 'e1',
|
|
1223
|
+
sourceId: 'a',
|
|
1224
|
+
targetId: 'b',
|
|
1225
|
+
label: {
|
|
1226
|
+
x: 50,
|
|
1227
|
+
y: 50,
|
|
1228
|
+
width: 0,
|
|
1229
|
+
height: 0,
|
|
1230
|
+
autoSize: true,
|
|
1231
|
+
data: {},
|
|
1232
|
+
},
|
|
1233
|
+
});
|
|
1234
|
+
const store = makeStore(
|
|
1235
|
+
[createNode({ id: 'a' }), createNode({ id: 'b', x: 200, y: 0 })],
|
|
1236
|
+
[edge]
|
|
1237
|
+
);
|
|
1238
|
+
const measured = { x: 50, y: 50, width: 65.875, height: 40 };
|
|
1239
|
+
|
|
1240
|
+
store.trigger.updateMeasurements({
|
|
1241
|
+
measurements: [{ id: 'e1', rect: measured }],
|
|
1242
|
+
});
|
|
1243
|
+
|
|
1244
|
+
expect(ctx(store).graph.edges[0].label).toMatchObject({
|
|
1245
|
+
width: 0,
|
|
1246
|
+
height: 0,
|
|
1247
|
+
autoSize: true,
|
|
1248
|
+
});
|
|
1249
|
+
expect(ctx(store).measurements.get('e1')).toEqual(measured);
|
|
1250
|
+
});
|
|
1251
|
+
|
|
1252
|
+
it('resolves mixed node, edge-label, port, and unknown measurement batches', () => {
|
|
1253
|
+
const store = makeStore(
|
|
1254
|
+
[
|
|
1255
|
+
createNode({
|
|
1256
|
+
id: 'a',
|
|
1257
|
+
x: 100,
|
|
1258
|
+
y: 80,
|
|
1259
|
+
ports: [createPort({ name: 'out', side: 'right', direction: 'out' })],
|
|
1260
|
+
}),
|
|
1261
|
+
createNode({ id: 'b', x: 300, y: 80 }),
|
|
1262
|
+
],
|
|
1263
|
+
[
|
|
1264
|
+
createEdge({
|
|
1265
|
+
id: 'e1',
|
|
1266
|
+
sourceId: 'a',
|
|
1267
|
+
targetId: 'b',
|
|
1268
|
+
label: { width: 40, height: 20 },
|
|
1269
|
+
}),
|
|
1270
|
+
]
|
|
1271
|
+
);
|
|
1272
|
+
|
|
1273
|
+
store.trigger.updateMeasurements({
|
|
1274
|
+
measurements: [
|
|
1275
|
+
{ id: 'a', rect: { x: 100, y: 80, width: 150, height: 50 } },
|
|
1276
|
+
{ id: 'e1', rect: { x: 200, y: 90, width: 90, height: 30 } },
|
|
1277
|
+
{ id: 'a:out', rect: { x: 244, y: 99, width: 12, height: 12 } },
|
|
1278
|
+
{ id: 'external-overlay', rect: { x: 1, y: 2, width: 3, height: 4 } },
|
|
1279
|
+
],
|
|
1280
|
+
});
|
|
1281
|
+
|
|
1282
|
+
expect(ctx(store).measurements.get('a')?.width).toBe(150);
|
|
1283
|
+
expect(ctx(store).graph.edges[0].label).toMatchObject({ width: 90, height: 30 });
|
|
1284
|
+
expect(ctx(store).graph.nodes[0].ports?.[0]).toMatchObject({
|
|
1285
|
+
x: 144,
|
|
1286
|
+
y: 19,
|
|
1287
|
+
width: 12,
|
|
1288
|
+
height: 12,
|
|
1289
|
+
});
|
|
1290
|
+
expect(ctx(store).measurements.get('external-overlay')).toEqual({
|
|
1291
|
+
x: 1,
|
|
1292
|
+
y: 2,
|
|
1293
|
+
width: 3,
|
|
1294
|
+
height: 4,
|
|
1295
|
+
});
|
|
1296
|
+
});
|
|
1297
|
+
|
|
1298
|
+
it('undo/redo restores committed resize without restoring active resize state', () => {
|
|
1299
|
+
const store = makeStore([createNode({ id: 'a', x: 100, y: 100, width: 100, height: 80 })]);
|
|
1300
|
+
const transactionId = 'resize-a';
|
|
1301
|
+
|
|
1302
|
+
store.trigger.resizeStart({ id: 'a', handle: 'bottom-right', transactionId });
|
|
1303
|
+
store.trigger.resizeMove({ x: 240, y: 210 });
|
|
1304
|
+
store.trigger.resizeEnd();
|
|
1305
|
+
|
|
1306
|
+
expect(ctx(store).graph.nodes[0].style).toMatchObject({
|
|
1307
|
+
customWidth: 140,
|
|
1308
|
+
customHeight: 110,
|
|
1309
|
+
});
|
|
1310
|
+
|
|
1311
|
+
undo(store);
|
|
1312
|
+
expect(ctx(store).canvasState).toBe('idle');
|
|
1313
|
+
expect(ctx(store).resizeCorners).toBeNull();
|
|
1314
|
+
expect(ctx(store).graph.nodes[0].style?.customWidth).toBeUndefined();
|
|
1315
|
+
expect(ctx(store).graph.nodes[0].style?.customHeight).toBeUndefined();
|
|
1316
|
+
|
|
1317
|
+
redo(store);
|
|
1318
|
+
expect(ctx(store).canvasState).toBe('idle');
|
|
1319
|
+
expect(ctx(store).graph.nodes[0].style).toMatchObject({
|
|
1320
|
+
customWidth: 140,
|
|
1321
|
+
customHeight: 110,
|
|
1322
|
+
});
|
|
1323
|
+
});
|
|
1324
|
+
|
|
1325
|
+
it('commitDrag with grid snapping', () => {
|
|
1326
|
+
const store = makeStore([createNode({ id: 'a', x: 100, y: 100 })], [], {
|
|
1327
|
+
snapToGrid: true,
|
|
1328
|
+
snapGrid: [10, 10] as [number, number],
|
|
1329
|
+
});
|
|
1330
|
+
store.trigger.select({ ids: ['a'] });
|
|
1331
|
+
store.trigger.dragStart({ id: 'a' });
|
|
1332
|
+
store.trigger.dragMove({ dx: 13, dy: 17 });
|
|
1333
|
+
store.trigger.dragEnd();
|
|
1334
|
+
store.trigger.commitDrag();
|
|
1335
|
+
const node = ctx(store).graph.nodes[0];
|
|
1336
|
+
expect(node.x).toBe(110); // 113 snapped to 110
|
|
1337
|
+
expect(node.y).toBe(120); // 117 snapped to 120
|
|
1338
|
+
});
|
|
1339
|
+
});
|
|
1340
|
+
|
|
1341
|
+
// Layout effects
|
|
1342
|
+
|
|
1343
|
+
describe('layout effects', () => {
|
|
1344
|
+
it('requestLayout emits layoutRequested with the layout graph contract', () => {
|
|
1345
|
+
const store = makeStore([createNode({ id: 'a', x: 10, y: 20 })]);
|
|
1346
|
+
const events: Array<{ algorithm: string; graph: unknown }> = [];
|
|
1347
|
+
|
|
1348
|
+
store.on('layoutRequested', (event) => {
|
|
1349
|
+
events.push(event);
|
|
1350
|
+
});
|
|
1351
|
+
|
|
1352
|
+
store.trigger.requestLayout({ algorithm: 'elk' });
|
|
1353
|
+
|
|
1354
|
+
expect(events).toHaveLength(1);
|
|
1355
|
+
expect(events[0].algorithm).toBe('elk');
|
|
1356
|
+
expect(events[0].graph).toEqual(ctx(store).graph);
|
|
1357
|
+
expect(ctx(store).autoLayout.status).toBe('pending');
|
|
1358
|
+
});
|
|
1359
|
+
|
|
1360
|
+
it('requestLayout does not enter pending state when no algorithm can be resolved', () => {
|
|
1361
|
+
const store = makeStore([createNode({ id: 'a', x: 10, y: 20 })]);
|
|
1362
|
+
const events: Array<{ algorithm: string; graph: unknown }> = [];
|
|
1363
|
+
|
|
1364
|
+
store.on('layoutRequested', (event) => {
|
|
1365
|
+
events.push(event);
|
|
1366
|
+
});
|
|
1367
|
+
|
|
1368
|
+
store.trigger.requestLayout({});
|
|
1369
|
+
|
|
1370
|
+
expect(events).toHaveLength(0);
|
|
1371
|
+
expect(ctx(store).autoLayout.status).toBe('idle');
|
|
1372
|
+
expect(ctx(store).autoLayout.algorithm).toBeNull();
|
|
1373
|
+
});
|
|
1374
|
+
|
|
1375
|
+
it('applyLayoutResult applies positions without recording wall-clock state', () => {
|
|
1376
|
+
const store = makeStore(
|
|
1377
|
+
[createNode({ id: 'a', x: 10, y: 20 })],
|
|
1378
|
+
[
|
|
1379
|
+
createEdge({
|
|
1380
|
+
id: 'aa',
|
|
1381
|
+
sourceId: 'a',
|
|
1382
|
+
targetId: 'a',
|
|
1383
|
+
dx: 12,
|
|
1384
|
+
dy: 8,
|
|
1385
|
+
waypoints: [{ x: 40, y: 40 }],
|
|
1386
|
+
}),
|
|
1387
|
+
],
|
|
1388
|
+
);
|
|
1389
|
+
|
|
1390
|
+
store.trigger.applyLayoutResult({
|
|
1391
|
+
positions: new Map([['a', { x: 100, y: 200 }]]),
|
|
1392
|
+
});
|
|
1393
|
+
|
|
1394
|
+
expect(ctx(store).graph.nodes[0].x).toBe(100);
|
|
1395
|
+
expect(ctx(store).graph.nodes[0].y).toBe(200);
|
|
1396
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('dx');
|
|
1397
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('dy');
|
|
1398
|
+
expect(ctx(store).autoLayout).toEqual({
|
|
1399
|
+
status: 'idle',
|
|
1400
|
+
algorithm: null,
|
|
1401
|
+
});
|
|
1402
|
+
});
|
|
1403
|
+
|
|
1404
|
+
it('applyLayoutResult invalidates routes incident to moved nodes', () => {
|
|
1405
|
+
const store = makeStore(
|
|
1406
|
+
[createNode({ id: 'a', x: 10 }), createNode({ id: 'b', x: 200 })],
|
|
1407
|
+
[
|
|
1408
|
+
createEdge({
|
|
1409
|
+
id: 'ab',
|
|
1410
|
+
sourceId: 'a',
|
|
1411
|
+
targetId: 'b',
|
|
1412
|
+
points: [{ x: 10, y: 0 }, { x: 200, y: 0 }],
|
|
1413
|
+
routing: 'polyline',
|
|
1414
|
+
svgPath: 'M 10 0 L 200 0',
|
|
1415
|
+
}),
|
|
1416
|
+
],
|
|
1417
|
+
);
|
|
1418
|
+
|
|
1419
|
+
store.trigger.applyLayoutResult({
|
|
1420
|
+
positions: new Map([['a', { x: 100, y: 200 }]]),
|
|
1421
|
+
});
|
|
1422
|
+
|
|
1423
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('points');
|
|
1424
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('routing');
|
|
1425
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('svgPath');
|
|
1426
|
+
});
|
|
1427
|
+
|
|
1428
|
+
it('applyLayoutResult preserves untouched edge identity', () => {
|
|
1429
|
+
const store = makeStore(
|
|
1430
|
+
[
|
|
1431
|
+
createNode({ id: 'a', x: 10 }),
|
|
1432
|
+
createNode({ id: 'b', x: 200 }),
|
|
1433
|
+
createNode({ id: 'c', x: 400 }),
|
|
1434
|
+
createNode({ id: 'd', x: 600 }),
|
|
1435
|
+
],
|
|
1436
|
+
[
|
|
1437
|
+
createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' }),
|
|
1438
|
+
createEdge({ id: 'cd', sourceId: 'c', targetId: 'd' }),
|
|
1439
|
+
],
|
|
1440
|
+
);
|
|
1441
|
+
const untouched = ctx(store).graph.edges[1];
|
|
1442
|
+
|
|
1443
|
+
store.trigger.applyLayoutResult({
|
|
1444
|
+
positions: new Map([['a', { x: 100, y: 200 }]]),
|
|
1445
|
+
});
|
|
1446
|
+
|
|
1447
|
+
expect(ctx(store).graph.edges[1]).toBe(untouched);
|
|
1448
|
+
});
|
|
1449
|
+
});
|
|
1450
|
+
|
|
1451
|
+
describe('connection lifecycle', () => {
|
|
1452
|
+
it('emits connectionEnded with the thing the gesture ended on', () => {
|
|
1453
|
+
const store = makeStore([createNode({ id: 'a' }), createNode({ id: 'b' })]);
|
|
1454
|
+
const events: any[] = [];
|
|
1455
|
+
|
|
1456
|
+
store.on('connectionEnded', (event) => {
|
|
1457
|
+
events.push(event);
|
|
1458
|
+
});
|
|
1459
|
+
|
|
1460
|
+
store.trigger.connectionStart({
|
|
1461
|
+
sourceId: 'a',
|
|
1462
|
+
sourcePort: null,
|
|
1463
|
+
position: { x: 0, y: 0 },
|
|
1464
|
+
});
|
|
1465
|
+
store.trigger.connectionMove({
|
|
1466
|
+
position: { x: 100, y: 0 },
|
|
1467
|
+
targetId: 'b',
|
|
1468
|
+
targetPort: null,
|
|
1469
|
+
isValid: true,
|
|
1470
|
+
});
|
|
1471
|
+
store.trigger.connectionEnd({
|
|
1472
|
+
target: { kind: 'node', nodeId: 'b', port: null },
|
|
1473
|
+
});
|
|
1474
|
+
|
|
1475
|
+
expect(events).toHaveLength(1);
|
|
1476
|
+
expect(events[0].status).toBe('connected');
|
|
1477
|
+
expect(events[0].source).toEqual({ nodeId: 'a', port: null });
|
|
1478
|
+
expect(events[0].target).toEqual({ kind: 'node', nodeId: 'b', port: null });
|
|
1479
|
+
});
|
|
1480
|
+
|
|
1481
|
+
it('tracks invalid connection reasons and ended status', () => {
|
|
1482
|
+
const store = makeStore([createNode({ id: 'a' }), createNode({ id: 'b' })]);
|
|
1483
|
+
const events: any[] = [];
|
|
1484
|
+
store.on('connectionEnded', (event) => events.push(event));
|
|
1485
|
+
|
|
1486
|
+
store.trigger.connectionStart({
|
|
1487
|
+
sourceId: 'a',
|
|
1488
|
+
sourcePort: null,
|
|
1489
|
+
position: { x: 0, y: 0 },
|
|
1490
|
+
});
|
|
1491
|
+
store.trigger.connectionMove({
|
|
1492
|
+
position: { x: 100, y: 0 },
|
|
1493
|
+
targetId: 'b',
|
|
1494
|
+
targetPort: null,
|
|
1495
|
+
isValid: false,
|
|
1496
|
+
invalidReason: 'policy-denied',
|
|
1497
|
+
});
|
|
1498
|
+
store.trigger.connectionEnd({ target: { kind: 'node', nodeId: 'b', port: null } });
|
|
1499
|
+
|
|
1500
|
+
expect(events[0].status).toBe('invalid');
|
|
1501
|
+
expect(events[0].connection.invalidReason).toBe('policy-denied');
|
|
1502
|
+
});
|
|
1503
|
+
|
|
1504
|
+
it('honors explicit connection activity supplied by an input session', () => {
|
|
1505
|
+
const store = makeStore([createNode({ id: 'a' })]);
|
|
1506
|
+
store.trigger.connectionStart({ sourceId: 'a', sourcePort: null, position: { x: 0, y: 0 } });
|
|
1507
|
+
store.trigger.connectionMove({ position: { x: 20, y: 0 }, active: false });
|
|
1508
|
+
expect(ctx(store).connection?.active).toBe(false);
|
|
1509
|
+
store.trigger.connectionMove({ position: { x: 0, y: 0 }, active: true });
|
|
1510
|
+
expect(ctx(store).connection?.active).toBe(true);
|
|
1511
|
+
});
|
|
1512
|
+
|
|
1513
|
+
it('activates connection drag only after the configured threshold', () => {
|
|
1514
|
+
const store = makeStore([createNode({ id: 'a' })], [], {
|
|
1515
|
+
connectionDragThreshold: 10,
|
|
1516
|
+
});
|
|
1517
|
+
|
|
1518
|
+
store.trigger.connectionStart({
|
|
1519
|
+
sourceId: 'a',
|
|
1520
|
+
sourcePort: null,
|
|
1521
|
+
position: { x: 0, y: 0 },
|
|
1522
|
+
});
|
|
1523
|
+
store.trigger.connectionMove({
|
|
1524
|
+
position: { x: 3, y: 4 },
|
|
1525
|
+
});
|
|
1526
|
+
expect(ctx(store).connection?.active).toBe(false);
|
|
1527
|
+
|
|
1528
|
+
store.trigger.connectionMove({
|
|
1529
|
+
position: { x: 12, y: 0 },
|
|
1530
|
+
});
|
|
1531
|
+
expect(ctx(store).connection?.active).toBe(true);
|
|
1532
|
+
});
|
|
1533
|
+
});
|
|
1534
|
+
|
|
1535
|
+
// Viewport
|
|
1536
|
+
|
|
1537
|
+
describe('viewport', () => {
|
|
1538
|
+
it('setViewport updates viewport', () => {
|
|
1539
|
+
const store = makeStore();
|
|
1540
|
+
store.trigger.setViewport({ x: 100, y: 200, zoom: 2 });
|
|
1541
|
+
expect(ctx(store).viewport).toEqual({ x: 100, y: 200, zoom: 2 });
|
|
1542
|
+
});
|
|
1543
|
+
|
|
1544
|
+
it('can preserve the world point at the viewport center when its size changes', () => {
|
|
1545
|
+
const store = makeStore();
|
|
1546
|
+
store.trigger.updateViewportSize({ width: 800, height: 600 });
|
|
1547
|
+
store.trigger.setViewport({ x: 100, y: -40, zoom: 2 });
|
|
1548
|
+
|
|
1549
|
+
store.trigger.updateViewportSize({
|
|
1550
|
+
width: 600,
|
|
1551
|
+
height: 500,
|
|
1552
|
+
preserveCenter: true,
|
|
1553
|
+
});
|
|
1554
|
+
|
|
1555
|
+
expect(ctx(store).viewportSize).toEqual({ width: 600, height: 500 });
|
|
1556
|
+
expect(ctx(store).viewport).toEqual({ x: 0, y: -90, zoom: 2 });
|
|
1557
|
+
});
|
|
1558
|
+
|
|
1559
|
+
it('does not shift the viewport on its first measured size', () => {
|
|
1560
|
+
const store = makeStore();
|
|
1561
|
+
|
|
1562
|
+
store.trigger.updateViewportSize({
|
|
1563
|
+
width: 800,
|
|
1564
|
+
height: 600,
|
|
1565
|
+
preserveCenter: true,
|
|
1566
|
+
});
|
|
1567
|
+
|
|
1568
|
+
expect(ctx(store).viewport).toEqual({ x: 0, y: 0, zoom: 1 });
|
|
1569
|
+
});
|
|
1570
|
+
|
|
1571
|
+
it('panBy adds delta', () => {
|
|
1572
|
+
const store = makeStore();
|
|
1573
|
+
store.trigger.panBy({ x: 50, y: -30 });
|
|
1574
|
+
expect(ctx(store).viewport.x).toBe(50);
|
|
1575
|
+
expect(ctx(store).viewport.y).toBe(-30);
|
|
1576
|
+
});
|
|
1577
|
+
|
|
1578
|
+
it('panBy constrains when translateExtent is set', () => {
|
|
1579
|
+
const store = makeStore([], [], {});
|
|
1580
|
+
store.trigger.updateViewportSize({ width: 800, height: 600 });
|
|
1581
|
+
store.trigger.setTranslateExtent({
|
|
1582
|
+
extent: [
|
|
1583
|
+
{ x: 0, y: 0 },
|
|
1584
|
+
{ x: 2000, y: 2000 },
|
|
1585
|
+
],
|
|
1586
|
+
});
|
|
1587
|
+
// Pan far right — should be constrained (extent wider than viewport)
|
|
1588
|
+
store.trigger.panBy({ x: 9999, y: 0 });
|
|
1589
|
+
// Visible left = -9999/1 = -9999, way before extent start (0)
|
|
1590
|
+
// Constrain should pull it back so visible left >= 0 → viewport.x <= 0
|
|
1591
|
+
expect(ctx(store).viewport.x).toBeLessThanOrEqual(0);
|
|
1592
|
+
});
|
|
1593
|
+
|
|
1594
|
+
it('zoomTo constrains when translateExtent is set', () => {
|
|
1595
|
+
const store = makeStore([], [], {});
|
|
1596
|
+
store.trigger.updateViewportSize({ width: 800, height: 600 });
|
|
1597
|
+
store.trigger.setTranslateExtent({
|
|
1598
|
+
extent: [
|
|
1599
|
+
{ x: 0, y: 0 },
|
|
1600
|
+
{ x: 500, y: 500 },
|
|
1601
|
+
],
|
|
1602
|
+
});
|
|
1603
|
+
// Zoom out then check constraint
|
|
1604
|
+
store.trigger.zoomTo({ zoom: 0.2, center: { x: 400, y: 300 } });
|
|
1605
|
+
// At low zoom, constraint should prevent content from going off-screen
|
|
1606
|
+
const vp = ctx(store).viewport;
|
|
1607
|
+
expect(vp.zoom).toBe(0.2);
|
|
1608
|
+
});
|
|
1609
|
+
|
|
1610
|
+
it('fitView centers on graph including edge labels', () => {
|
|
1611
|
+
const node = createNode({ id: 'a', x: 0, y: 0, width: 100, height: 100 });
|
|
1612
|
+
const edge = createEdge({
|
|
1613
|
+
id: 'e1',
|
|
1614
|
+
sourceId: 'a',
|
|
1615
|
+
targetId: 'a',
|
|
1616
|
+
label: { x: 200, y: 200, width: 50, height: 50, data: {} },
|
|
1617
|
+
});
|
|
1618
|
+
const store = makeStore([node], [edge]);
|
|
1619
|
+
store.trigger.updateViewportSize({ width: 800, height: 600 });
|
|
1620
|
+
store.trigger.fitView({ padding: 0 });
|
|
1621
|
+
const vp = ctx(store).viewport;
|
|
1622
|
+
// Graph bounds: (0,0)-(250,250) — includes edge label
|
|
1623
|
+
// viewportForBounds uses 10% default padding → padded = 275x275
|
|
1624
|
+
// zoom = min(800/275, 600/275) ≈ 2.18, within maxZoom=4
|
|
1625
|
+
// The key assertion: zoom accounts for edge label extending bounds
|
|
1626
|
+
expect(vp.zoom).toBeLessThan(4); // label pulled bounds out, so zoom < max
|
|
1627
|
+
});
|
|
1628
|
+
|
|
1629
|
+
it('fitView can fit a node subset', () => {
|
|
1630
|
+
const a = createNode({ id: 'a', x: 0, y: 0, width: 100, height: 100 });
|
|
1631
|
+
const b = createNode({ id: 'b', x: 1000, y: 0, width: 100, height: 100 });
|
|
1632
|
+
const store = makeStore([a, b]);
|
|
1633
|
+
store.trigger.updateViewportSize({ width: 800, height: 600 });
|
|
1634
|
+
|
|
1635
|
+
store.trigger.fitView({ nodes: ['a'], padding: 0, maxZoom: 10 });
|
|
1636
|
+
|
|
1637
|
+
const vp = ctx(store).viewport;
|
|
1638
|
+
expect(vp.zoom).toBe(6);
|
|
1639
|
+
expect(vp.x).toBe(100);
|
|
1640
|
+
expect(vp.y).toBe(0);
|
|
1641
|
+
});
|
|
1642
|
+
|
|
1643
|
+
it('fitView subset includes edge labels for selected edges', () => {
|
|
1644
|
+
const a = createNode({ id: 'a', x: 0, y: 0, width: 100, height: 100 });
|
|
1645
|
+
const b = createNode({ id: 'b', x: 0, y: 1000, width: 100, height: 100 });
|
|
1646
|
+
const edge = createEdge({
|
|
1647
|
+
id: 'e1',
|
|
1648
|
+
sourceId: 'a',
|
|
1649
|
+
targetId: 'a',
|
|
1650
|
+
label: { x: 500, y: 0, width: 50, height: 50, data: {} },
|
|
1651
|
+
});
|
|
1652
|
+
const store = makeStore([a, b], [edge]);
|
|
1653
|
+
store.trigger.updateViewportSize({ width: 800, height: 600 });
|
|
1654
|
+
|
|
1655
|
+
// Subset: node 'a' alone — bounds are 0..100 horizontally
|
|
1656
|
+
store.trigger.fitView({ nodes: ['a'], padding: 0, maxZoom: 100 });
|
|
1657
|
+
const zoomNodeOnly = ctx(store).viewport.zoom;
|
|
1658
|
+
|
|
1659
|
+
// Subset: node 'a' + edge 'e1' — label at x=500..550 extends bounds
|
|
1660
|
+
store.trigger.fitView({ nodes: ['a', 'e1'], padding: 0, maxZoom: 100 });
|
|
1661
|
+
const zoomWithEdge = ctx(store).viewport.zoom;
|
|
1662
|
+
|
|
1663
|
+
expect(zoomWithEdge).toBeLessThan(zoomNodeOnly);
|
|
1664
|
+
});
|
|
1665
|
+
|
|
1666
|
+
it('fitView accepts node-like objects for React Flow compatibility', () => {
|
|
1667
|
+
const a = createNode({ id: 'a', x: 0, y: 0, width: 100, height: 100 });
|
|
1668
|
+
const b = createNode({ id: 'b', x: 1000, y: 0, width: 100, height: 100 });
|
|
1669
|
+
const store = makeStore([a, b]);
|
|
1670
|
+
store.trigger.updateViewportSize({ width: 800, height: 600 });
|
|
1671
|
+
|
|
1672
|
+
store.trigger.fitView({ nodes: [{ id: 'b' }], padding: 0, maxZoom: 10 });
|
|
1673
|
+
|
|
1674
|
+
const vp = ctx(store).viewport;
|
|
1675
|
+
expect(vp.zoom).toBe(6);
|
|
1676
|
+
expect(vp.x).toBe(-5900);
|
|
1677
|
+
expect(vp.y).toBe(0);
|
|
1678
|
+
});
|
|
1679
|
+
|
|
1680
|
+
it('fitView is no-op when viewportSize is zero', () => {
|
|
1681
|
+
const store = makeStore([createNode({ id: 'a' })]);
|
|
1682
|
+
// viewportSize defaults to 0x0
|
|
1683
|
+
store.trigger.fitView({});
|
|
1684
|
+
expect(ctx(store).viewport).toEqual({ x: 0, y: 0, zoom: 1 });
|
|
1685
|
+
});
|
|
1686
|
+
|
|
1687
|
+
it('can update the drag threshold', () => {
|
|
1688
|
+
const store = makeStore([], [], { dragThreshold: 3 });
|
|
1689
|
+
|
|
1690
|
+
expect(ctx(store).dragThreshold).toBe(3);
|
|
1691
|
+
store.trigger.setDragThreshold({ dragThreshold: 8 });
|
|
1692
|
+
expect(ctx(store).dragThreshold).toBe(8);
|
|
1693
|
+
store.trigger.setDragThreshold({ dragThreshold: -1 });
|
|
1694
|
+
expect(ctx(store).dragThreshold).toBe(0);
|
|
1695
|
+
});
|
|
1696
|
+
|
|
1697
|
+
it('uses React Flow compatible auto-pan defaults', () => {
|
|
1698
|
+
const store = makeStore();
|
|
1699
|
+
|
|
1700
|
+
expect(ctx(store).autoPanOnDrag).toBe(true);
|
|
1701
|
+
expect(ctx(store).autoPanThreshold).toBe(40);
|
|
1702
|
+
expect(ctx(store).autoPanSpeed).toBe(15);
|
|
1703
|
+
});
|
|
1704
|
+
|
|
1705
|
+
it('can update auto-pan options', () => {
|
|
1706
|
+
const store = makeStore();
|
|
1707
|
+
|
|
1708
|
+
store.trigger.setAutoPanOptions({
|
|
1709
|
+
autoPanOnDrag: false,
|
|
1710
|
+
autoPanThreshold: 24,
|
|
1711
|
+
autoPanSpeed: 8,
|
|
1712
|
+
});
|
|
1713
|
+
expect(ctx(store).autoPanOnDrag).toBe(false);
|
|
1714
|
+
expect(ctx(store).autoPanThreshold).toBe(24);
|
|
1715
|
+
expect(ctx(store).autoPanSpeed).toBe(8);
|
|
1716
|
+
|
|
1717
|
+
store.trigger.setAutoPanOptions({
|
|
1718
|
+
autoPanThreshold: -1,
|
|
1719
|
+
autoPanSpeed: -1,
|
|
1720
|
+
});
|
|
1721
|
+
expect(ctx(store).autoPanThreshold).toBe(0);
|
|
1722
|
+
expect(ctx(store).autoPanSpeed).toBe(0);
|
|
1723
|
+
});
|
|
1724
|
+
|
|
1725
|
+
it('push/pop viewport stack', () => {
|
|
1726
|
+
const store = makeStore();
|
|
1727
|
+
store.trigger.setViewport({ x: 10, y: 20, zoom: 1.5 });
|
|
1728
|
+
store.trigger.pushViewport();
|
|
1729
|
+
store.trigger.setViewport({ x: 100, y: 200, zoom: 3 });
|
|
1730
|
+
expect(ctx(store).viewport.x).toBe(100);
|
|
1731
|
+
store.trigger.popViewport();
|
|
1732
|
+
expect(ctx(store).viewport).toEqual({ x: 10, y: 20, zoom: 1.5 });
|
|
1733
|
+
});
|
|
1734
|
+
});
|
|
1735
|
+
|
|
1736
|
+
// Pan lifecycle
|
|
1737
|
+
|
|
1738
|
+
describe('pan lifecycle', () => {
|
|
1739
|
+
it('panStart → pan → panEnd', () => {
|
|
1740
|
+
const store = makeStore();
|
|
1741
|
+
store.trigger.panStart({ x: 100, y: 100 });
|
|
1742
|
+
expect(ctx(store).canvasState).toBe('panning');
|
|
1743
|
+
store.trigger.pan({ x: 150, y: 120 });
|
|
1744
|
+
expect(ctx(store).viewport.x).toBe(50);
|
|
1745
|
+
expect(ctx(store).viewport.y).toBe(20);
|
|
1746
|
+
store.trigger.panEnd();
|
|
1747
|
+
expect(ctx(store).canvasState).toBe('idle');
|
|
1748
|
+
});
|
|
1749
|
+
});
|
|
1750
|
+
|
|
1751
|
+
// Graph mutation guards (validation + error events)
|
|
1752
|
+
|
|
1753
|
+
describe('graph mutation guards', () => {
|
|
1754
|
+
it('rejects createNode with a duplicate id and emits an error event', () => {
|
|
1755
|
+
const store = makeStore([createNode({ id: 'a' })]);
|
|
1756
|
+
const errors: any[] = [];
|
|
1757
|
+
store.on('error', (event) => errors.push(event));
|
|
1758
|
+
|
|
1759
|
+
store.trigger.createNode({ node: createNode({ id: 'a', x: 50 }) });
|
|
1760
|
+
|
|
1761
|
+
expect(ctx(store).graph.nodes).toHaveLength(1);
|
|
1762
|
+
expect(ctx(store).graph.nodes[0].x).toBe(0);
|
|
1763
|
+
expect(errors).toHaveLength(1);
|
|
1764
|
+
expect(String((errors[0].error as Error).message)).toMatch(/already exists/i);
|
|
1765
|
+
expect(String((errors[0].error as Error).message)).toContain('"a"');
|
|
1766
|
+
});
|
|
1767
|
+
|
|
1768
|
+
it('rejects createEdge with a duplicate id and emits an error event', () => {
|
|
1769
|
+
const store = makeStore(
|
|
1770
|
+
[createNode({ id: 'a' }), createNode({ id: 'b' })],
|
|
1771
|
+
[createEdge({ id: 'e1', sourceId: 'a', targetId: 'b' })]
|
|
1772
|
+
);
|
|
1773
|
+
const errors: any[] = [];
|
|
1774
|
+
store.on('error', (event) => errors.push(event));
|
|
1775
|
+
|
|
1776
|
+
store.trigger.createEdge({
|
|
1777
|
+
edge: createEdge({ id: 'e1', sourceId: 'b', targetId: 'a' }),
|
|
1778
|
+
});
|
|
1779
|
+
|
|
1780
|
+
expect(ctx(store).graph.edges).toHaveLength(1);
|
|
1781
|
+
expect(ctx(store).graph.edges[0].sourceId).toBe('a');
|
|
1782
|
+
expect(errors).toHaveLength(1);
|
|
1783
|
+
});
|
|
1784
|
+
|
|
1785
|
+
it('rejects createEdge whose endpoints do not exist and emits an error event', () => {
|
|
1786
|
+
const store = makeStore([createNode({ id: 'a' })]);
|
|
1787
|
+
const errors: any[] = [];
|
|
1788
|
+
store.on('error', (event) => errors.push(event));
|
|
1789
|
+
|
|
1790
|
+
store.trigger.createEdge({
|
|
1791
|
+
edge: createEdge({ id: 'e1', sourceId: 'a', targetId: 'ghost' }),
|
|
1792
|
+
});
|
|
1793
|
+
|
|
1794
|
+
expect(ctx(store).graph.edges).toHaveLength(0);
|
|
1795
|
+
expect(errors).toHaveLength(1);
|
|
1796
|
+
expect(String((errors[0].error as Error).message)).toContain('"ghost"');
|
|
1797
|
+
});
|
|
1798
|
+
|
|
1799
|
+
it('does not record rejected creates in undo history', () => {
|
|
1800
|
+
const store = makeStore([createNode({ id: 'a' })]);
|
|
1801
|
+
|
|
1802
|
+
store.trigger.createNode({ node: createNode({ id: 'a' }) });
|
|
1803
|
+
undo(store);
|
|
1804
|
+
|
|
1805
|
+
// Undo should not have reverted anything else; graph still has node 'a'
|
|
1806
|
+
expect(ctx(store).graph.nodes.map((n) => n.id)).toEqual(['a']);
|
|
1807
|
+
});
|
|
1808
|
+
|
|
1809
|
+
it('updateNodeData initializes data when the node has none', () => {
|
|
1810
|
+
const node = createNode({ id: 'a' });
|
|
1811
|
+
delete (node as any).data;
|
|
1812
|
+
const store = makeStore([node]);
|
|
1813
|
+
|
|
1814
|
+
store.trigger.updateNodeData({ id: 'a', data: { label: 'hi' } });
|
|
1815
|
+
|
|
1816
|
+
expect(ctx(store).graph.nodes[0].data).toEqual({ label: 'hi' });
|
|
1817
|
+
});
|
|
1818
|
+
|
|
1819
|
+
it('updateEdgeData initializes data when the edge has none', () => {
|
|
1820
|
+
const edge = createEdge({ id: 'e1', sourceId: 'a', targetId: 'b' });
|
|
1821
|
+
delete (edge as any).data;
|
|
1822
|
+
const store = makeStore([createNode({ id: 'a' }), createNode({ id: 'b' })], [edge]);
|
|
1823
|
+
|
|
1824
|
+
store.trigger.updateEdgeData({ id: 'e1', data: { weight: 2 } });
|
|
1825
|
+
|
|
1826
|
+
expect(ctx(store).graph.edges[0].data).toEqual({ weight: 2 });
|
|
1827
|
+
});
|
|
1828
|
+
|
|
1829
|
+
it('updateNode patches and replaces nodes with undo support', () => {
|
|
1830
|
+
const store = makeStore([createNode({ id: 'a', x: 0, width: 100 })]);
|
|
1831
|
+
|
|
1832
|
+
store.trigger.updateNode({ id: 'a', patch: { x: 10 } });
|
|
1833
|
+
expect(ctx(store).graph.nodes[0].x).toBe(10);
|
|
1834
|
+
|
|
1835
|
+
store.trigger.updateNode({
|
|
1836
|
+
id: 'a',
|
|
1837
|
+
updater: (node) => ({ ...node, width: 220 }),
|
|
1838
|
+
});
|
|
1839
|
+
expect(ctx(store).graph.nodes[0].width).toBe(220);
|
|
1840
|
+
|
|
1841
|
+
undo(store);
|
|
1842
|
+
expect(ctx(store).graph.nodes[0]).toMatchObject({ x: 10, width: 100 });
|
|
1843
|
+
redo(store);
|
|
1844
|
+
expect(ctx(store).graph.nodes[0].width).toBe(220);
|
|
1845
|
+
});
|
|
1846
|
+
|
|
1847
|
+
it('updateNode invalidates routes incident to geometry changes', () => {
|
|
1848
|
+
const store = makeStore(
|
|
1849
|
+
[createNode({ id: 'a', x: 0 }), createNode({ id: 'b', x: 200 })],
|
|
1850
|
+
[
|
|
1851
|
+
createEdge({
|
|
1852
|
+
id: 'ab',
|
|
1853
|
+
sourceId: 'a',
|
|
1854
|
+
targetId: 'b',
|
|
1855
|
+
points: [{ x: 0, y: 0 }, { x: 200, y: 0 }],
|
|
1856
|
+
routing: 'orthogonal',
|
|
1857
|
+
svgPath: 'M 0 0 L 200 0',
|
|
1858
|
+
}),
|
|
1859
|
+
],
|
|
1860
|
+
);
|
|
1861
|
+
|
|
1862
|
+
store.trigger.updateNode({ id: 'a', patch: { x: 50 } });
|
|
1863
|
+
|
|
1864
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('points');
|
|
1865
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('routing');
|
|
1866
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('svgPath');
|
|
1867
|
+
});
|
|
1868
|
+
|
|
1869
|
+
it('updateNode rejects identity and parent changes', () => {
|
|
1870
|
+
const store = makeStore([createNode({ id: 'a', parentId: null })]);
|
|
1871
|
+
const errors: any[] = [];
|
|
1872
|
+
store.on('error', (event) => errors.push(event));
|
|
1873
|
+
|
|
1874
|
+
store.trigger.updateNode({
|
|
1875
|
+
id: 'a',
|
|
1876
|
+
patch: { parentId: 'group' } as any,
|
|
1877
|
+
});
|
|
1878
|
+
expect(ctx(store).graph.nodes[0].parentId).toBeNull();
|
|
1879
|
+
|
|
1880
|
+
store.trigger.updateNode({
|
|
1881
|
+
id: 'a',
|
|
1882
|
+
updater: (node) => ({ ...node, id: 'b' }),
|
|
1883
|
+
});
|
|
1884
|
+
expect(ctx(store).graph.nodes[0].id).toBe('a');
|
|
1885
|
+
expect(errors).toHaveLength(2);
|
|
1886
|
+
});
|
|
1887
|
+
|
|
1888
|
+
it('updateEdge patches and replaces edges with undo support', () => {
|
|
1889
|
+
const store = makeStore(
|
|
1890
|
+
[createNode({ id: 'a' }), createNode({ id: 'b' })],
|
|
1891
|
+
[
|
|
1892
|
+
createEdge({
|
|
1893
|
+
id: 'e1',
|
|
1894
|
+
sourceId: 'a',
|
|
1895
|
+
targetId: 'b',
|
|
1896
|
+
label: { x: 0, y: 0, width: 10, height: 10, data: {} },
|
|
1897
|
+
}),
|
|
1898
|
+
]
|
|
1899
|
+
);
|
|
1900
|
+
|
|
1901
|
+
store.trigger.updateEdge({ id: 'e1', patch: { reconnectable: false } });
|
|
1902
|
+
expect(ctx(store).graph.edges[0].reconnectable).toBe(false);
|
|
1903
|
+
|
|
1904
|
+
store.trigger.updateEdge({
|
|
1905
|
+
id: 'e1',
|
|
1906
|
+
updater: (edge) => ({ ...edge, label: { ...edge.label!, x: 40, y: 50 } }),
|
|
1907
|
+
});
|
|
1908
|
+
expect(ctx(store).graph.edges[0].label).toMatchObject({ x: 40, y: 50 });
|
|
1909
|
+
|
|
1910
|
+
undo(store);
|
|
1911
|
+
expect(ctx(store).graph.edges[0].label).toMatchObject({ x: 0, y: 0 });
|
|
1912
|
+
redo(store);
|
|
1913
|
+
expect(ctx(store).graph.edges[0].label).toMatchObject({ x: 40, y: 50 });
|
|
1914
|
+
});
|
|
1915
|
+
|
|
1916
|
+
it('updateEdge rejects endpoint and port changes', () => {
|
|
1917
|
+
const store = makeStore(
|
|
1918
|
+
[createNode({ id: 'a' }), createNode({ id: 'b' }), createNode({ id: 'c' })],
|
|
1919
|
+
[createEdge({ id: 'e1', sourceId: 'a', targetId: 'b' })]
|
|
1920
|
+
);
|
|
1921
|
+
const errors: any[] = [];
|
|
1922
|
+
store.on('error', (event) => errors.push(event));
|
|
1923
|
+
|
|
1924
|
+
store.trigger.updateEdge({
|
|
1925
|
+
id: 'e1',
|
|
1926
|
+
patch: { targetId: 'c' } as any,
|
|
1927
|
+
});
|
|
1928
|
+
expect(ctx(store).graph.edges[0].targetId).toBe('b');
|
|
1929
|
+
|
|
1930
|
+
store.trigger.updateEdge({
|
|
1931
|
+
id: 'e1',
|
|
1932
|
+
updater: (edge) => ({ ...edge, sourcePort: 'out' }),
|
|
1933
|
+
});
|
|
1934
|
+
expect(ctx(store).graph.edges[0].sourcePort).toBeUndefined();
|
|
1935
|
+
expect(errors).toHaveLength(2);
|
|
1936
|
+
});
|
|
1937
|
+
|
|
1938
|
+
it('reconnectEdge to a non-existent newTargetId emits error and leaves edge unchanged', () => {
|
|
1939
|
+
const store = makeStore(
|
|
1940
|
+
[createNode({ id: 'a' }), createNode({ id: 'b' })],
|
|
1941
|
+
[createEdge({ id: 'e1', sourceId: 'a', targetId: 'b' })]
|
|
1942
|
+
);
|
|
1943
|
+
const errors: any[] = [];
|
|
1944
|
+
store.on('error', (event) => errors.push(event));
|
|
1945
|
+
|
|
1946
|
+
store.trigger.reconnectEdge({ edgeId: 'e1', newTargetId: 'ghost' });
|
|
1947
|
+
|
|
1948
|
+
expect(ctx(store).graph.edges[0].targetId).toBe('b');
|
|
1949
|
+
expect(errors).toHaveLength(1);
|
|
1950
|
+
expect(String((errors[0].error as Error).message)).toContain('"ghost"');
|
|
1951
|
+
});
|
|
1952
|
+
|
|
1953
|
+
it('reconnectEdge to an existing node updates targetId', () => {
|
|
1954
|
+
const store = makeStore(
|
|
1955
|
+
[createNode({ id: 'a' }), createNode({ id: 'b' }), createNode({ id: 'c' })],
|
|
1956
|
+
[createEdge({ id: 'e1', sourceId: 'a', targetId: 'b' })]
|
|
1957
|
+
);
|
|
1958
|
+
const errors: any[] = [];
|
|
1959
|
+
store.on('error', (event) => errors.push(event));
|
|
1960
|
+
|
|
1961
|
+
store.trigger.reconnectEdge({ edgeId: 'e1', newTargetId: 'c' });
|
|
1962
|
+
|
|
1963
|
+
expect(ctx(store).graph.edges[0].targetId).toBe('c');
|
|
1964
|
+
expect(errors).toHaveLength(0);
|
|
1965
|
+
});
|
|
1966
|
+
|
|
1967
|
+
it('reparentNode with parentId === id emits error and leaves parentId unchanged', () => {
|
|
1968
|
+
const node = createNode({ id: 'a' });
|
|
1969
|
+
const store = makeStore([node]);
|
|
1970
|
+
const errors: any[] = [];
|
|
1971
|
+
store.on('error', (event) => errors.push(event));
|
|
1972
|
+
|
|
1973
|
+
store.trigger.reparentNode({ id: 'a', parentId: 'a' });
|
|
1974
|
+
|
|
1975
|
+
expect(ctx(store).graph.nodes[0].parentId).toBeUndefined();
|
|
1976
|
+
expect(errors).toHaveLength(1);
|
|
1977
|
+
expect(String((errors[0].error as Error).message)).toMatch(/cannot be its own parent/i);
|
|
1978
|
+
});
|
|
1979
|
+
|
|
1980
|
+
it('reparentNode to a descendant emits error', () => {
|
|
1981
|
+
// a -> b -> c (b is child of a, c is child of b)
|
|
1982
|
+
const a = createNode({ id: 'a' });
|
|
1983
|
+
const b = { ...createNode({ id: 'b' }), parentId: 'a' };
|
|
1984
|
+
const c = { ...createNode({ id: 'c' }), parentId: 'b' };
|
|
1985
|
+
const store = makeStore([a, b, c]);
|
|
1986
|
+
const errors: any[] = [];
|
|
1987
|
+
store.on('error', (event) => errors.push(event));
|
|
1988
|
+
|
|
1989
|
+
// Attempt to parent 'a' under 'c' (c is a descendant of a)
|
|
1990
|
+
store.trigger.reparentNode({ id: 'a', parentId: 'c' });
|
|
1991
|
+
|
|
1992
|
+
expect(ctx(store).graph.nodes.find((n) => n.id === 'a')?.parentId).toBeUndefined();
|
|
1993
|
+
expect(errors).toHaveLength(1);
|
|
1994
|
+
expect(String((errors[0].error as Error).message)).toMatch(/cycle/i);
|
|
1995
|
+
});
|
|
1996
|
+
|
|
1997
|
+
it('reparentNode to a valid unrelated parent works', () => {
|
|
1998
|
+
const store = makeStore([createNode({ id: 'a' }), createNode({ id: 'b' })]);
|
|
1999
|
+
const errors: any[] = [];
|
|
2000
|
+
store.on('error', (event) => errors.push(event));
|
|
2001
|
+
|
|
2002
|
+
store.trigger.reparentNode({ id: 'a', parentId: 'b' });
|
|
2003
|
+
|
|
2004
|
+
expect(ctx(store).graph.nodes.find((n) => n.id === 'a')?.parentId).toBe('b');
|
|
2005
|
+
expect(errors).toHaveLength(0);
|
|
2006
|
+
});
|
|
2007
|
+
});
|
|
2008
|
+
|
|
2009
|
+
// deleteNode strategies (must match deleteSelected semantics)
|
|
2010
|
+
|
|
2011
|
+
describe('deleteNode strategies', () => {
|
|
2012
|
+
function makeTriangle(opts = {}) {
|
|
2013
|
+
return makeStore(
|
|
2014
|
+
[createNode({ id: 'a' }), createNode({ id: 'b' }), createNode({ id: 'c' })],
|
|
2015
|
+
[
|
|
2016
|
+
createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' }),
|
|
2017
|
+
createEdge({ id: 'bc', sourceId: 'b', targetId: 'c' }),
|
|
2018
|
+
],
|
|
2019
|
+
opts
|
|
2020
|
+
);
|
|
2021
|
+
}
|
|
2022
|
+
|
|
2023
|
+
it('cascade (default): deleting a node removes its edges', () => {
|
|
2024
|
+
const store = makeTriangle();
|
|
2025
|
+
store.trigger.deleteNode({ id: 'b' });
|
|
2026
|
+
expect(ctx(store).graph.nodes.map((n) => n.id)).toEqual(['a', 'c']);
|
|
2027
|
+
expect(ctx(store).graph.edges).toHaveLength(0);
|
|
2028
|
+
});
|
|
2029
|
+
|
|
2030
|
+
it('cascade (default): deleting a parent removes its descendants', () => {
|
|
2031
|
+
const store = makeStore(
|
|
2032
|
+
[
|
|
2033
|
+
createNode({ id: 'parent' }),
|
|
2034
|
+
createNode({ id: 'child', parentId: 'parent' }),
|
|
2035
|
+
createNode({ id: 'grandchild', parentId: 'child' }),
|
|
2036
|
+
createNode({ id: 'outside' }),
|
|
2037
|
+
],
|
|
2038
|
+
[
|
|
2039
|
+
createEdge({
|
|
2040
|
+
id: 'boundary',
|
|
2041
|
+
sourceId: 'grandchild',
|
|
2042
|
+
targetId: 'outside',
|
|
2043
|
+
}),
|
|
2044
|
+
]
|
|
2045
|
+
);
|
|
2046
|
+
|
|
2047
|
+
store.trigger.deleteNode({ id: 'parent' });
|
|
2048
|
+
|
|
2049
|
+
expect(ctx(store).graph.nodes.map((node) => node.id)).toEqual(['outside']);
|
|
2050
|
+
expect(ctx(store).graph.edges).toEqual([]);
|
|
2051
|
+
});
|
|
2052
|
+
|
|
2053
|
+
it('retarget: deleting a node turns its edges into self-loops on the survivor', () => {
|
|
2054
|
+
const store = makeTriangle({ onDeleteNodes: 'retarget' });
|
|
2055
|
+
store.trigger.deleteNode({ id: 'b' });
|
|
2056
|
+
expect(ctx(store).graph.nodes.map((n) => n.id)).toEqual(['a', 'c']);
|
|
2057
|
+
expect(ctx(store).graph.edges.map((e) => [e.id, e.sourceId, e.targetId])).toEqual([
|
|
2058
|
+
['ab', 'a', 'a'],
|
|
2059
|
+
['bc', 'c', 'c'],
|
|
2060
|
+
]);
|
|
2061
|
+
});
|
|
2062
|
+
|
|
2063
|
+
it('retarget invalidates authoritative geometry on surviving edges', () => {
|
|
2064
|
+
const store = makeStore(
|
|
2065
|
+
[createNode({ id: 'a' }), createNode({ id: 'b' })],
|
|
2066
|
+
[
|
|
2067
|
+
createEdge({
|
|
2068
|
+
id: 'ab',
|
|
2069
|
+
sourceId: 'a',
|
|
2070
|
+
targetId: 'b',
|
|
2071
|
+
points: [{ x: 0, y: 0 }, { x: 100, y: 0 }],
|
|
2072
|
+
routing: 'polyline',
|
|
2073
|
+
svgPath: 'M 0 0 L 100 0',
|
|
2074
|
+
}),
|
|
2075
|
+
],
|
|
2076
|
+
{ onDeleteNodes: 'retarget' },
|
|
2077
|
+
);
|
|
2078
|
+
|
|
2079
|
+
store.trigger.deleteNode({ id: 'b' });
|
|
2080
|
+
|
|
2081
|
+
expect(ctx(store).graph.edges[0]).toMatchObject({
|
|
2082
|
+
sourceId: 'a',
|
|
2083
|
+
targetId: 'a',
|
|
2084
|
+
});
|
|
2085
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('points');
|
|
2086
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('routing');
|
|
2087
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('svgPath');
|
|
2088
|
+
});
|
|
2089
|
+
|
|
2090
|
+
it('custom function: deleteNode delegates edge handling to the strategy', () => {
|
|
2091
|
+
const store = makeTriangle({
|
|
2092
|
+
onDeleteNodes: (graph: any, nodeIds: Set<string>) => ({
|
|
2093
|
+
nodes: graph.nodes.filter((n: any) => !nodeIds.has(n.id)),
|
|
2094
|
+
// keep all edges to prove the strategy was consulted
|
|
2095
|
+
edges: graph.edges,
|
|
2096
|
+
}),
|
|
2097
|
+
});
|
|
2098
|
+
store.trigger.deleteNode({ id: 'b' });
|
|
2099
|
+
expect(ctx(store).graph.nodes.map((n) => n.id)).toEqual(['a', 'c']);
|
|
2100
|
+
expect(ctx(store).graph.edges.map((e) => e.id)).toEqual(['ab', 'bc']);
|
|
2101
|
+
});
|
|
2102
|
+
|
|
2103
|
+
it('retarget via deleteSelected and deleteNode agree', () => {
|
|
2104
|
+
const viaSelected = makeTriangle({ onDeleteNodes: 'retarget' });
|
|
2105
|
+
viaSelected.trigger.select({ ids: ['b'] });
|
|
2106
|
+
viaSelected.trigger.deleteSelected();
|
|
2107
|
+
|
|
2108
|
+
const viaDelete = makeTriangle({ onDeleteNodes: 'retarget' });
|
|
2109
|
+
viaDelete.trigger.deleteNode({ id: 'b' });
|
|
2110
|
+
|
|
2111
|
+
expect(ctx(viaDelete).graph.edges).toEqual(ctx(viaSelected).graph.edges);
|
|
2112
|
+
});
|
|
2113
|
+
});
|
|
2114
|
+
|
|
2115
|
+
// collapsedNodeIds pruning on delete (F5: set must not leak deleted ids)
|
|
2116
|
+
|
|
2117
|
+
describe('collapsedNodeIds pruning', () => {
|
|
2118
|
+
it('deleteNode removes the deleted node from collapsedNodeIds', () => {
|
|
2119
|
+
const store = makeStore([createNode({ id: 'g' })]);
|
|
2120
|
+
store.trigger.collapseNode({ id: 'g' });
|
|
2121
|
+
expect(ctx(store).collapsedNodeIds.has('g')).toBe(true);
|
|
2122
|
+
|
|
2123
|
+
store.trigger.deleteNode({ id: 'g' });
|
|
2124
|
+
expect(ctx(store).collapsedNodeIds.has('g')).toBe(false);
|
|
2125
|
+
});
|
|
2126
|
+
|
|
2127
|
+
it('deleteSelected removes collapsed ids from collapsedNodeIds', () => {
|
|
2128
|
+
const store = makeStore([createNode({ id: 'g' })]);
|
|
2129
|
+
store.trigger.collapseNode({ id: 'g' });
|
|
2130
|
+
store.trigger.select({ ids: ['g'] });
|
|
2131
|
+
store.trigger.deleteSelected();
|
|
2132
|
+
expect(ctx(store).collapsedNodeIds.has('g')).toBe(false);
|
|
2133
|
+
});
|
|
2134
|
+
|
|
2135
|
+
it('a new node reusing a deleted collapsed id is not treated as collapsed', () => {
|
|
2136
|
+
const store = makeStore([createNode({ id: 'g' })]);
|
|
2137
|
+
store.trigger.collapseNode({ id: 'g' });
|
|
2138
|
+
store.trigger.deleteNode({ id: 'g' });
|
|
2139
|
+
|
|
2140
|
+
store.trigger.createNode({ node: createNode({ id: 'g' }) });
|
|
2141
|
+
expect(ctx(store).collapsedNodeIds.has('g')).toBe(false);
|
|
2142
|
+
});
|
|
2143
|
+
|
|
2144
|
+
it('undo of the delete restores the collapsed state', () => {
|
|
2145
|
+
const store = makeStore([createNode({ id: 'g' })]);
|
|
2146
|
+
store.trigger.collapseNode({ id: 'g' });
|
|
2147
|
+
store.trigger.deleteNode({ id: 'g' });
|
|
2148
|
+
expect(ctx(store).collapsedNodeIds.has('g')).toBe(false);
|
|
2149
|
+
|
|
2150
|
+
undo(store);
|
|
2151
|
+
expect(ctx(store).graph.nodes.map((n) => n.id)).toContain('g');
|
|
2152
|
+
expect(ctx(store).collapsedNodeIds.has('g')).toBe(true);
|
|
2153
|
+
});
|
|
2154
|
+
|
|
2155
|
+
it('redoing collapse removes newly selected hidden descendants', () => {
|
|
2156
|
+
const store = makeStore([
|
|
2157
|
+
createNode({ id: 'group' }),
|
|
2158
|
+
{ ...createNode({ id: 'child' }), parentId: 'group' },
|
|
2159
|
+
]);
|
|
2160
|
+
store.trigger.collapseNode({ id: 'group' });
|
|
2161
|
+
undo(store);
|
|
2162
|
+
store.trigger.select({ ids: ['child'] });
|
|
2163
|
+
|
|
2164
|
+
redo(store);
|
|
2165
|
+
|
|
2166
|
+
expect(ctx(store).collapsedNodeIds.has('group')).toBe(true);
|
|
2167
|
+
expect(ctx(store).selection.has('child')).toBe(false);
|
|
2168
|
+
});
|
|
2169
|
+
});
|
|
2170
|
+
|
|
2171
|
+
// moveNodes guards (keyboard movement must respect drag rules)
|
|
2172
|
+
|
|
2173
|
+
describe('moveNodes guards', () => {
|
|
2174
|
+
it('does not move nodes with draggable: false', () => {
|
|
2175
|
+
const store = makeStore([
|
|
2176
|
+
createNode({ id: 'a', x: 0, y: 0, draggable: false }),
|
|
2177
|
+
createNode({ id: 'b', x: 100, y: 0 }),
|
|
2178
|
+
]);
|
|
2179
|
+
store.trigger.select({ ids: ['a', 'b'], mode: 'multi' });
|
|
2180
|
+
store.trigger.moveNodes({ direction: { x: 1, y: 0 }, factor: 10 });
|
|
2181
|
+
|
|
2182
|
+
const [a, b] = ctx(store).graph.nodes;
|
|
2183
|
+
expect(a.dx ?? 0).toBe(0);
|
|
2184
|
+
expect(b.dx).toBe(10);
|
|
2185
|
+
});
|
|
2186
|
+
|
|
2187
|
+
it('clamps movement to nodeExtent like pointer drags do', () => {
|
|
2188
|
+
const store = makeStore([createNode({ id: 'a', x: 0, y: 0, width: 100, height: 50 })], [], {
|
|
2189
|
+
nodeExtent: [
|
|
2190
|
+
{ x: 0, y: 0 },
|
|
2191
|
+
{ x: 200, y: 200 },
|
|
2192
|
+
],
|
|
2193
|
+
});
|
|
2194
|
+
store.trigger.select({ ids: ['a'] });
|
|
2195
|
+
store.trigger.moveNodes({ direction: { x: 1, y: 0 }, factor: 500 });
|
|
2196
|
+
|
|
2197
|
+
const a = ctx(store).graph.nodes[0];
|
|
2198
|
+
// visual x must not exceed extent right edge minus width
|
|
2199
|
+
expect(a.x + (a.dx ?? 0)).toBeLessThanOrEqual(100);
|
|
2200
|
+
});
|
|
2201
|
+
});
|
|
2202
|
+
|
|
2203
|
+
// Resize lifecycle
|
|
2204
|
+
|
|
2205
|
+
describe('resize lifecycle', () => {
|
|
2206
|
+
it('resizeStart → resizeMove → resizeEnd commits position and custom size', () => {
|
|
2207
|
+
const store = makeStore([createNode({ id: 'a', x: 10, y: 10, width: 100, height: 50 })]);
|
|
2208
|
+
|
|
2209
|
+
store.trigger.resizeStart({ id: 'a', handle: 'bottom-right' });
|
|
2210
|
+
expect(ctx(store).canvasState).toBe('resizing');
|
|
2211
|
+
expect(ctx(store).resizingEntityId).toBe('a');
|
|
2212
|
+
|
|
2213
|
+
store.trigger.resizeMove({ x: 200, y: 100 });
|
|
2214
|
+
store.trigger.resizeEnd();
|
|
2215
|
+
|
|
2216
|
+
const a = ctx(store).graph.nodes[0];
|
|
2217
|
+
expect(ctx(store).canvasState).toBe('idle');
|
|
2218
|
+
expect(a.x).toBe(10);
|
|
2219
|
+
expect(a.y).toBe(10);
|
|
2220
|
+
expect(a.style?.customWidth).toBe(190);
|
|
2221
|
+
expect(a.style?.customHeight).toBe(90);
|
|
2222
|
+
});
|
|
2223
|
+
|
|
2224
|
+
it('resizeEnd invalidates routes incident to the resized node', () => {
|
|
2225
|
+
const store = makeStore(
|
|
2226
|
+
[
|
|
2227
|
+
createNode({ id: 'a', x: 10, y: 10, width: 100, height: 50 }),
|
|
2228
|
+
createNode({ id: 'b', x: 300, y: 10 }),
|
|
2229
|
+
],
|
|
2230
|
+
[
|
|
2231
|
+
createEdge({
|
|
2232
|
+
id: 'ab',
|
|
2233
|
+
sourceId: 'a',
|
|
2234
|
+
targetId: 'b',
|
|
2235
|
+
points: [{ x: 110, y: 35 }, { x: 300, y: 35 }],
|
|
2236
|
+
routing: 'polyline',
|
|
2237
|
+
svgPath: 'M 110 35 L 300 35',
|
|
2238
|
+
}),
|
|
2239
|
+
],
|
|
2240
|
+
);
|
|
2241
|
+
|
|
2242
|
+
store.trigger.resizeStart({ id: 'a', handle: 'bottom-right' });
|
|
2243
|
+
store.trigger.resizeMove({ x: 200, y: 100 });
|
|
2244
|
+
store.trigger.resizeEnd();
|
|
2245
|
+
|
|
2246
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('points');
|
|
2247
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('routing');
|
|
2248
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('svgPath');
|
|
2249
|
+
});
|
|
2250
|
+
|
|
2251
|
+
it('free resize (no resize.min) shrinks below the old 10px floor', () => {
|
|
2252
|
+
const store = makeStore([createNode({ id: 'a', x: 0, y: 0, width: 100, height: 50 })]);
|
|
2253
|
+
|
|
2254
|
+
// Drag the bottom-right corner almost back to the origin — with no explicit
|
|
2255
|
+
// resize.min the node is free to shrink to a few px (down to the 1px guard).
|
|
2256
|
+
store.trigger.resizeStart({ id: 'a', handle: 'bottom-right' });
|
|
2257
|
+
store.trigger.resizeMove({ x: 3, y: 2 });
|
|
2258
|
+
store.trigger.resizeEnd();
|
|
2259
|
+
|
|
2260
|
+
const a = ctx(store).graph.nodes[0];
|
|
2261
|
+
expect(a.style?.customWidth).toBe(3);
|
|
2262
|
+
expect(a.style?.customHeight).toBe(2);
|
|
2263
|
+
});
|
|
2264
|
+
|
|
2265
|
+
it('honors an explicit resize.min floor', () => {
|
|
2266
|
+
const store = makeStore([
|
|
2267
|
+
{
|
|
2268
|
+
...createNode({ id: 'a', x: 0, y: 0, width: 100, height: 50 }),
|
|
2269
|
+
resize: { min: { width: 40, height: 30 } },
|
|
2270
|
+
},
|
|
2271
|
+
]);
|
|
2272
|
+
|
|
2273
|
+
store.trigger.resizeStart({ id: 'a', handle: 'bottom-right' });
|
|
2274
|
+
store.trigger.resizeMove({ x: 3, y: 2 });
|
|
2275
|
+
store.trigger.resizeEnd();
|
|
2276
|
+
|
|
2277
|
+
const a = ctx(store).graph.nodes[0];
|
|
2278
|
+
expect(a.style?.customWidth).toBe(40);
|
|
2279
|
+
expect(a.style?.customHeight).toBe(30);
|
|
2280
|
+
});
|
|
2281
|
+
|
|
2282
|
+
it('resize from top-left moves origin and keeps the opposite corner anchored', () => {
|
|
2283
|
+
const store = makeStore([createNode({ id: 'a', x: 10, y: 10, width: 100, height: 50 })]);
|
|
2284
|
+
|
|
2285
|
+
store.trigger.resizeStart({ id: 'a', handle: 'top-left' });
|
|
2286
|
+
store.trigger.resizeMove({ x: 0, y: 0 });
|
|
2287
|
+
store.trigger.resizeEnd();
|
|
2288
|
+
|
|
2289
|
+
const a = ctx(store).graph.nodes[0];
|
|
2290
|
+
expect(a.x).toBe(0);
|
|
2291
|
+
expect(a.y).toBe(0);
|
|
2292
|
+
expect(a.style?.customWidth).toBe(110);
|
|
2293
|
+
expect(a.style?.customHeight).toBe(60);
|
|
2294
|
+
});
|
|
2295
|
+
|
|
2296
|
+
it('resizeCancel restores idle state without committing', () => {
|
|
2297
|
+
const store = makeStore([createNode({ id: 'a', x: 10, y: 10, width: 100, height: 50 })]);
|
|
2298
|
+
|
|
2299
|
+
store.trigger.resizeStart({ id: 'a', handle: 'bottom-right' });
|
|
2300
|
+
store.trigger.resizeMove({ x: 300, y: 300 });
|
|
2301
|
+
store.trigger.resizeCancel();
|
|
2302
|
+
|
|
2303
|
+
const a = ctx(store).graph.nodes[0];
|
|
2304
|
+
expect(ctx(store).canvasState).toBe('idle');
|
|
2305
|
+
expect(a.style?.customWidth).toBeUndefined();
|
|
2306
|
+
expect(ctx(store).resizeCorners).toBeNull();
|
|
2307
|
+
});
|
|
2308
|
+
|
|
2309
|
+
it('keeps a 1px degeneracy guard when collapsing a node to nothing', () => {
|
|
2310
|
+
const store = makeStore([createNode({ id: 'a', x: 0, y: 0, width: 100, height: 50 })]);
|
|
2311
|
+
|
|
2312
|
+
// Free by default (no resize.min), but never commit a zero-area box.
|
|
2313
|
+
store.trigger.resizeStart({ id: 'a', handle: 'bottom-right' });
|
|
2314
|
+
store.trigger.resizeMove({ x: 0, y: 0 });
|
|
2315
|
+
store.trigger.resizeEnd();
|
|
2316
|
+
|
|
2317
|
+
const a = ctx(store).graph.nodes[0];
|
|
2318
|
+
expect(a.style?.customWidth).toBe(1);
|
|
2319
|
+
expect(a.style?.customHeight).toBe(1);
|
|
2320
|
+
});
|
|
2321
|
+
|
|
2322
|
+
it('clamps resize by node min/max constraints', () => {
|
|
2323
|
+
const store = makeStore([
|
|
2324
|
+
createNode({
|
|
2325
|
+
id: 'a',
|
|
2326
|
+
x: 0,
|
|
2327
|
+
y: 0,
|
|
2328
|
+
width: 100,
|
|
2329
|
+
height: 50,
|
|
2330
|
+
resize: {
|
|
2331
|
+
min: { width: 80, height: 40 },
|
|
2332
|
+
max: { width: 160, height: 120 },
|
|
2333
|
+
},
|
|
2334
|
+
}),
|
|
2335
|
+
]);
|
|
2336
|
+
|
|
2337
|
+
store.trigger.resizeStart({ id: 'a', handle: 'bottom-right' });
|
|
2338
|
+
store.trigger.resizeMove({ x: 300, y: 300 });
|
|
2339
|
+
store.trigger.resizeEnd();
|
|
2340
|
+
|
|
2341
|
+
let a = ctx(store).graph.nodes[0];
|
|
2342
|
+
expect(a.style?.customWidth).toBe(160);
|
|
2343
|
+
expect(a.style?.customHeight).toBe(120);
|
|
2344
|
+
|
|
2345
|
+
store.trigger.resizeStart({ id: 'a', handle: 'bottom-right' });
|
|
2346
|
+
store.trigger.resizeMove({ x: 10, y: 10 });
|
|
2347
|
+
store.trigger.resizeEnd();
|
|
2348
|
+
|
|
2349
|
+
a = ctx(store).graph.nodes[0];
|
|
2350
|
+
expect(a.style?.customWidth).toBe(80);
|
|
2351
|
+
expect(a.style?.customHeight).toBe(40);
|
|
2352
|
+
});
|
|
2353
|
+
|
|
2354
|
+
it('preserves aspect ratio when node resize.aspect is true', () => {
|
|
2355
|
+
const store = makeStore([
|
|
2356
|
+
createNode({
|
|
2357
|
+
id: 'a',
|
|
2358
|
+
x: 0,
|
|
2359
|
+
y: 0,
|
|
2360
|
+
width: 100,
|
|
2361
|
+
height: 50,
|
|
2362
|
+
resize: { aspect: true },
|
|
2363
|
+
}),
|
|
2364
|
+
]);
|
|
2365
|
+
|
|
2366
|
+
store.trigger.resizeStart({ id: 'a', handle: 'bottom-right' });
|
|
2367
|
+
store.trigger.resizeMove({ x: 200, y: 70 });
|
|
2368
|
+
store.trigger.resizeEnd();
|
|
2369
|
+
|
|
2370
|
+
const a = ctx(store).graph.nodes[0];
|
|
2371
|
+
expect(a.style?.customWidth).toBe(200);
|
|
2372
|
+
expect(a.style?.customHeight).toBe(100);
|
|
2373
|
+
});
|
|
2374
|
+
});
|
|
2375
|
+
|
|
2376
|
+
describe('resize interaction with groups + autoSize', () => {
|
|
2377
|
+
// A fixed group clamps child drags to its child area (per axis). The child
|
|
2378
|
+
// area is derived from the group's CURRENT size, which resize writes to
|
|
2379
|
+
// style.customWidth/customHeight — so shrinking the group by resize must
|
|
2380
|
+
// tighten the clamp on the configured axis.
|
|
2381
|
+
it('fixed-group resize respects clampChildren', () => {
|
|
2382
|
+
const group = createNode({
|
|
2383
|
+
id: 'g',
|
|
2384
|
+
x: 0,
|
|
2385
|
+
y: 0,
|
|
2386
|
+
width: 300,
|
|
2387
|
+
height: 300,
|
|
2388
|
+
group: {
|
|
2389
|
+
type: 'fixed',
|
|
2390
|
+
content: 'top',
|
|
2391
|
+
padding: 0,
|
|
2392
|
+
clampChildren: 'x',
|
|
2393
|
+
},
|
|
2394
|
+
});
|
|
2395
|
+
const child = createNode({
|
|
2396
|
+
id: 'c',
|
|
2397
|
+
parentId: 'g',
|
|
2398
|
+
x: 10,
|
|
2399
|
+
y: 40,
|
|
2400
|
+
width: 40,
|
|
2401
|
+
height: 40,
|
|
2402
|
+
draggable: true,
|
|
2403
|
+
});
|
|
2404
|
+
const store = makeStore([group, child]);
|
|
2405
|
+
|
|
2406
|
+
// Shrink the group from 300 → 120 wide via resize.
|
|
2407
|
+
store.trigger.resizeStart({ id: 'g', handle: 'bottom-right' });
|
|
2408
|
+
store.trigger.resizeMove({ x: 120, y: 200 });
|
|
2409
|
+
store.trigger.resizeEnd();
|
|
2410
|
+
|
|
2411
|
+
const g = ctx(store).graph.nodes.find((n) => n.id === 'g')!;
|
|
2412
|
+
expect(g.style?.customWidth).toBe(120);
|
|
2413
|
+
|
|
2414
|
+
// Child area now derives from the shrunken bounds (padding 0).
|
|
2415
|
+
const area = fixedGroupGeometry(
|
|
2416
|
+
{
|
|
2417
|
+
x: g.x,
|
|
2418
|
+
y: g.y,
|
|
2419
|
+
width: g.style!.customWidth!,
|
|
2420
|
+
height: g.style!.customHeight!,
|
|
2421
|
+
},
|
|
2422
|
+
{ width: 0, height: 0 },
|
|
2423
|
+
g.group as Extract<FlowNode['group'], { type: 'fixed' }>
|
|
2424
|
+
).childArea;
|
|
2425
|
+
|
|
2426
|
+
// Drag the child far to the right; the x-clamp must pin it inside the
|
|
2427
|
+
// (now smaller) child area, while y is free (clampChildren: 'x').
|
|
2428
|
+
store.trigger.select({ ids: ['c'] });
|
|
2429
|
+
store.trigger.dragStart({ id: 'c' });
|
|
2430
|
+
store.trigger.dragMove({ dx: 500, dy: 30 });
|
|
2431
|
+
store.trigger.dragEnd();
|
|
2432
|
+
store.trigger.commitDrag();
|
|
2433
|
+
|
|
2434
|
+
const c = ctx(store).graph.nodes.find((n) => n.id === 'c')!;
|
|
2435
|
+
const size = resolvedSize(c);
|
|
2436
|
+
const maxX = area.x + area.width - size.width;
|
|
2437
|
+
// x is clamped inside the child area's right edge...
|
|
2438
|
+
expect(c.x + (c.dx ?? 0)).toBeCloseTo(maxX, 5);
|
|
2439
|
+
expect(c.x + (c.dx ?? 0)).toBeLessThanOrEqual(area.x + area.width);
|
|
2440
|
+
// ...and never crosses the resized group's right edge.
|
|
2441
|
+
expect(c.x + (c.dx ?? 0) + size.width).toBeLessThanOrEqual(g.x + g.style!.customWidth!);
|
|
2442
|
+
// y axis is NOT clamped (config is 'x' only): it moved by the full delta.
|
|
2443
|
+
expect(c.y + (c.dy ?? 0)).toBe(40 + 30);
|
|
2444
|
+
});
|
|
2445
|
+
|
|
2446
|
+
// For a derived group the stored width/height ARE the content block. A resize
|
|
2447
|
+
// therefore edits the content-block size (via style.customWidth/customHeight),
|
|
2448
|
+
// leaving the children's authored geometry untouched.
|
|
2449
|
+
it('derived-group content block resize', () => {
|
|
2450
|
+
const group = createNode({
|
|
2451
|
+
id: 'g',
|
|
2452
|
+
x: 0,
|
|
2453
|
+
y: 0,
|
|
2454
|
+
width: 200,
|
|
2455
|
+
height: 40,
|
|
2456
|
+
group: { type: 'derived', content: 'top', padding: 16 },
|
|
2457
|
+
});
|
|
2458
|
+
const child = createNode({
|
|
2459
|
+
id: 'c',
|
|
2460
|
+
parentId: 'g',
|
|
2461
|
+
x: 20,
|
|
2462
|
+
y: 60,
|
|
2463
|
+
width: 80,
|
|
2464
|
+
height: 30,
|
|
2465
|
+
});
|
|
2466
|
+
const store = makeStore([group, child]);
|
|
2467
|
+
|
|
2468
|
+
store.trigger.resizeStart({ id: 'g', handle: 'bottom-right' });
|
|
2469
|
+
store.trigger.resizeMove({ x: 260, y: 40 });
|
|
2470
|
+
store.trigger.resizeEnd();
|
|
2471
|
+
|
|
2472
|
+
const g = ctx(store).graph.nodes.find((n) => n.id === 'g')!;
|
|
2473
|
+
// Content-block size (the stored size for a derived group) changed.
|
|
2474
|
+
expect(g.style?.customWidth).toBe(260);
|
|
2475
|
+
expect(g.style?.customHeight).toBe(40);
|
|
2476
|
+
// The group is still a derived group; overall bounds derive from children
|
|
2477
|
+
// union + content + padding at query time, not from stored width/height.
|
|
2478
|
+
expect(g.group?.type).toBe('derived');
|
|
2479
|
+
|
|
2480
|
+
// Children are untouched by resizing the content block.
|
|
2481
|
+
const c = ctx(store).graph.nodes.find((n) => n.id === 'c')!;
|
|
2482
|
+
expect(c.x).toBe(20);
|
|
2483
|
+
expect(c.y).toBe(60);
|
|
2484
|
+
expect(c.style?.customWidth).toBeUndefined();
|
|
2485
|
+
expect(c.style?.customHeight).toBeUndefined();
|
|
2486
|
+
});
|
|
2487
|
+
|
|
2488
|
+
// resolvedSize precedence: custom (resize) > measured (autoSize) > authored.
|
|
2489
|
+
it('autoSize node resize precedence', () => {
|
|
2490
|
+
const store = makeStore([
|
|
2491
|
+
createNode({
|
|
2492
|
+
id: 'a',
|
|
2493
|
+
x: 0,
|
|
2494
|
+
y: 0,
|
|
2495
|
+
width: 150,
|
|
2496
|
+
height: 50,
|
|
2497
|
+
autoSize: true,
|
|
2498
|
+
measured: { width: 220, height: 90 },
|
|
2499
|
+
}),
|
|
2500
|
+
]);
|
|
2501
|
+
|
|
2502
|
+
// Before resize: measured wins over authored for an autoSize node.
|
|
2503
|
+
let a = ctx(store).graph.nodes[0];
|
|
2504
|
+
expect(resolvedSize(a)).toEqual({ width: 220, height: 90 });
|
|
2505
|
+
|
|
2506
|
+
// Manual resize writes style.customWidth/customHeight.
|
|
2507
|
+
store.trigger.resizeStart({ id: 'a', handle: 'bottom-right' });
|
|
2508
|
+
store.trigger.resizeMove({ x: 300, y: 300 });
|
|
2509
|
+
store.trigger.resizeEnd();
|
|
2510
|
+
|
|
2511
|
+
a = ctx(store).graph.nodes[0];
|
|
2512
|
+
expect(a.style?.customWidth).toBe(300);
|
|
2513
|
+
expect(a.style?.customHeight).toBe(300);
|
|
2514
|
+
// After resize: custom size takes precedence over both measured and authored.
|
|
2515
|
+
expect(resolvedSize(a)).toEqual({ width: 300, height: 300 });
|
|
2516
|
+
});
|
|
2517
|
+
});
|
|
2518
|
+
|
|
2519
|
+
// Edge reconnection
|
|
2520
|
+
|
|
2521
|
+
describe('reconnectEdge', () => {
|
|
2522
|
+
it('updates endpoints and ports', () => {
|
|
2523
|
+
const store = makeStore(
|
|
2524
|
+
[createNode({ id: 'a' }), createNode({ id: 'b' }), createNode({ id: 'c' })],
|
|
2525
|
+
[createEdge({ id: 'e1', sourceId: 'a', targetId: 'b' })]
|
|
2526
|
+
);
|
|
2527
|
+
|
|
2528
|
+
store.trigger.reconnectEdge({
|
|
2529
|
+
edgeId: 'e1',
|
|
2530
|
+
newTargetId: 'c',
|
|
2531
|
+
newTargetPort: 'in',
|
|
2532
|
+
});
|
|
2533
|
+
|
|
2534
|
+
const e = ctx(store).graph.edges[0];
|
|
2535
|
+
expect(e.sourceId).toBe('a');
|
|
2536
|
+
expect(e.targetId).toBe('c');
|
|
2537
|
+
expect(e.targetPort).toBe('in');
|
|
2538
|
+
});
|
|
2539
|
+
|
|
2540
|
+
it('invalidates authoritative route geometry when an endpoint changes', () => {
|
|
2541
|
+
const store = makeStore(
|
|
2542
|
+
[createNode({ id: 'a' }), createNode({ id: 'b' }), createNode({ id: 'c' })],
|
|
2543
|
+
[
|
|
2544
|
+
createEdge({
|
|
2545
|
+
id: 'e1',
|
|
2546
|
+
sourceId: 'a',
|
|
2547
|
+
targetId: 'b',
|
|
2548
|
+
points: [{ x: 0, y: 0 }, { x: 100, y: 0 }],
|
|
2549
|
+
routing: 'polyline',
|
|
2550
|
+
svgPath: 'M 0 0 L 100 0',
|
|
2551
|
+
}),
|
|
2552
|
+
],
|
|
2553
|
+
);
|
|
2554
|
+
|
|
2555
|
+
store.trigger.reconnectEdge({ edgeId: 'e1', newTargetId: 'c' });
|
|
2556
|
+
|
|
2557
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('points');
|
|
2558
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('routing');
|
|
2559
|
+
expect(ctx(store).graph.edges[0]).not.toHaveProperty('svgPath');
|
|
2560
|
+
});
|
|
2561
|
+
|
|
2562
|
+
it('preserves authoritative geometry for a no-op reconnect with null ports', () => {
|
|
2563
|
+
const edge = createEdge({
|
|
2564
|
+
id: 'e1',
|
|
2565
|
+
sourceId: 'a',
|
|
2566
|
+
targetId: 'b',
|
|
2567
|
+
points: [{ x: 0, y: 0 }, { x: 100, y: 0 }],
|
|
2568
|
+
routing: 'polyline',
|
|
2569
|
+
svgPath: 'M 0 0 L 100 0',
|
|
2570
|
+
});
|
|
2571
|
+
(edge as any).sourcePort = null;
|
|
2572
|
+
(edge as any).targetPort = null;
|
|
2573
|
+
const store = makeStore(
|
|
2574
|
+
[createNode({ id: 'a' }), createNode({ id: 'b' })],
|
|
2575
|
+
[edge],
|
|
2576
|
+
);
|
|
2577
|
+
|
|
2578
|
+
store.trigger.reconnectEdge({ edgeId: 'e1' });
|
|
2579
|
+
|
|
2580
|
+
expect(ctx(store).graph.edges[0]).toMatchObject({
|
|
2581
|
+
points: [{ x: 0, y: 0 }, { x: 100, y: 0 }],
|
|
2582
|
+
routing: 'polyline',
|
|
2583
|
+
svgPath: 'M 0 0 L 100 0',
|
|
2584
|
+
});
|
|
2585
|
+
});
|
|
2586
|
+
|
|
2587
|
+
it('is undoable when endpoints change', () => {
|
|
2588
|
+
const store = makeStore(
|
|
2589
|
+
[createNode({ id: 'a' }), createNode({ id: 'b' }), createNode({ id: 'c' })],
|
|
2590
|
+
[createEdge({ id: 'e1', sourceId: 'a', targetId: 'b' })]
|
|
2591
|
+
);
|
|
2592
|
+
|
|
2593
|
+
store.trigger.reconnectEdge({ edgeId: 'e1', newTargetId: 'c' });
|
|
2594
|
+
expect(ctx(store).graph.edges[0].targetId).toBe('c');
|
|
2595
|
+
undo(store);
|
|
2596
|
+
expect(ctx(store).graph.edges[0].targetId).toBe('b');
|
|
2597
|
+
});
|
|
2598
|
+
|
|
2599
|
+
it('ignores unknown edge ids', () => {
|
|
2600
|
+
const store = makeStore([createNode({ id: 'a' })]);
|
|
2601
|
+
store.trigger.reconnectEdge({ edgeId: 'ghost', newTargetId: 'a' });
|
|
2602
|
+
expect(ctx(store).graph.edges).toHaveLength(0);
|
|
2603
|
+
});
|
|
2604
|
+
});
|
|
2605
|
+
|
|
2606
|
+
// Node hierarchy — parent drags propagate to descendants
|
|
2607
|
+
// (STRATEGY_REVIEW.md generalization gap: parentId must not be an inert attribute)
|
|
2608
|
+
|
|
2609
|
+
describe('node hierarchy (parent-child drag propagation)', () => {
|
|
2610
|
+
function makeFamily(opts = {}) {
|
|
2611
|
+
return makeStore(
|
|
2612
|
+
[
|
|
2613
|
+
createNode({ id: 'p', x: 0, y: 0, width: 400, height: 300 }),
|
|
2614
|
+
{ ...createNode({ id: 'c1', x: 20, y: 40 }), parentId: 'p' },
|
|
2615
|
+
{ ...createNode({ id: 'c2', x: 200, y: 40 }), parentId: 'p' },
|
|
2616
|
+
{ ...createNode({ id: 'g1', x: 30, y: 60 }), parentId: 'c1' },
|
|
2617
|
+
createNode({ id: 'other', x: 800, y: 0 }),
|
|
2618
|
+
],
|
|
2619
|
+
[],
|
|
2620
|
+
opts
|
|
2621
|
+
);
|
|
2622
|
+
}
|
|
2623
|
+
|
|
2624
|
+
function nodeById(store: ReturnType<typeof makeStore>, id: string) {
|
|
2625
|
+
return ctx(store).graph.nodes.find((n) => n.id === id)!;
|
|
2626
|
+
}
|
|
2627
|
+
|
|
2628
|
+
it('dragging a parent moves its children and grandchildren', () => {
|
|
2629
|
+
const store = makeFamily();
|
|
2630
|
+
store.trigger.select({ ids: ['p'] });
|
|
2631
|
+
store.trigger.dragStart({ id: 'p' });
|
|
2632
|
+
store.trigger.dragMove({ dx: 10, dy: 5 });
|
|
2633
|
+
|
|
2634
|
+
expect(nodeById(store, 'c1').dx).toBe(10);
|
|
2635
|
+
expect(nodeById(store, 'c1').dy).toBe(5);
|
|
2636
|
+
expect(nodeById(store, 'c2').dx).toBe(10);
|
|
2637
|
+
expect(nodeById(store, 'g1').dx).toBe(10);
|
|
2638
|
+
expect(nodeById(store, 'other').dx ?? 0).toBe(0);
|
|
2639
|
+
});
|
|
2640
|
+
|
|
2641
|
+
it('does not double-move a selected child of a selected parent', () => {
|
|
2642
|
+
const store = makeFamily();
|
|
2643
|
+
store.trigger.select({ ids: ['p', 'c1'], mode: 'multi' });
|
|
2644
|
+
store.trigger.dragStart({ id: 'p' });
|
|
2645
|
+
store.trigger.dragMove({ dx: 10, dy: 0 });
|
|
2646
|
+
|
|
2647
|
+
expect(nodeById(store, 'c1').dx).toBe(10);
|
|
2648
|
+
// grandchild follows its (selected) parent exactly once
|
|
2649
|
+
expect(nodeById(store, 'g1').dx).toBe(10);
|
|
2650
|
+
});
|
|
2651
|
+
|
|
2652
|
+
it('commitDrag bakes descendant positions', () => {
|
|
2653
|
+
const store = makeFamily();
|
|
2654
|
+
store.trigger.select({ ids: ['p'] });
|
|
2655
|
+
store.trigger.dragStart({ id: 'p' });
|
|
2656
|
+
store.trigger.dragMove({ dx: 10, dy: 5 });
|
|
2657
|
+
store.trigger.dragEnd();
|
|
2658
|
+
store.trigger.commitDrag();
|
|
2659
|
+
|
|
2660
|
+
expect(nodeById(store, 'c1').x).toBe(30);
|
|
2661
|
+
expect(nodeById(store, 'c1').y).toBe(45);
|
|
2662
|
+
expect(nodeById(store, 'g1').x).toBe(40);
|
|
2663
|
+
expect(nodeById(store, 'c1').dx).toBe(0);
|
|
2664
|
+
});
|
|
2665
|
+
|
|
2666
|
+
it('children follow the clamped parent delta under nodeExtent', () => {
|
|
2667
|
+
const store = makeFamily({
|
|
2668
|
+
nodeExtent: [
|
|
2669
|
+
{ x: 0, y: 0 },
|
|
2670
|
+
{ x: 500, y: 500 },
|
|
2671
|
+
],
|
|
2672
|
+
});
|
|
2673
|
+
store.trigger.select({ ids: ['p'] });
|
|
2674
|
+
store.trigger.dragStart({ id: 'p' });
|
|
2675
|
+
// parent (width 400) can move at most 100 right before clamping
|
|
2676
|
+
store.trigger.dragMove({ dx: 250, dy: 0 });
|
|
2677
|
+
|
|
2678
|
+
expect(nodeById(store, 'p').dx).toBe(100);
|
|
2679
|
+
expect(nodeById(store, 'c1').dx).toBe(100);
|
|
2680
|
+
});
|
|
2681
|
+
|
|
2682
|
+
it('moveNodes (arrow keys) propagates to descendants', () => {
|
|
2683
|
+
const store = makeFamily();
|
|
2684
|
+
store.trigger.select({ ids: ['p'] });
|
|
2685
|
+
store.trigger.moveNodes({ direction: { x: 1, y: 0 }, factor: 10 });
|
|
2686
|
+
|
|
2687
|
+
expect(nodeById(store, 'p').dx).toBe(10);
|
|
2688
|
+
expect(nodeById(store, 'c1').dx).toBe(10);
|
|
2689
|
+
expect(nodeById(store, 'g1').dx).toBe(10);
|
|
2690
|
+
});
|
|
2691
|
+
|
|
2692
|
+
it('rejects a parentId cycle at the graph boundary', () => {
|
|
2693
|
+
expect(() =>
|
|
2694
|
+
makeStore([
|
|
2695
|
+
{ ...createNode({ id: 'a' }), parentId: 'b' },
|
|
2696
|
+
{ ...createNode({ id: 'b' }), parentId: 'a' },
|
|
2697
|
+
])
|
|
2698
|
+
).toThrow(/parent cycle/);
|
|
2699
|
+
});
|
|
2700
|
+
});
|
|
2701
|
+
|
|
2702
|
+
// Keyboard movement undo (arrow-key moves must be undoable like drags)
|
|
2703
|
+
|
|
2704
|
+
describe('moveNodes undo', () => {
|
|
2705
|
+
it('each moveNodes burst is undoable back to the pre-move position', () => {
|
|
2706
|
+
const store = makeStore([createNode({ id: 'a', x: 0, y: 0 })]);
|
|
2707
|
+
store.trigger.select({ ids: ['a'] });
|
|
2708
|
+
|
|
2709
|
+
store.trigger.moveNodes({ direction: { x: 1, y: 0 }, factor: 10 });
|
|
2710
|
+
store.trigger.commitDrag();
|
|
2711
|
+
expect(ctx(store).graph.nodes[0].x).toBe(10);
|
|
2712
|
+
|
|
2713
|
+
undo(store);
|
|
2714
|
+
expect(ctx(store).graph.nodes[0].x).toBe(0);
|
|
2715
|
+
expect(ctx(store).graph.nodes[0].dx ?? 0).toBe(0);
|
|
2716
|
+
});
|
|
2717
|
+
|
|
2718
|
+
it('does not record a history entry when nothing can move', () => {
|
|
2719
|
+
const store = makeStore([createNode({ id: 'a', x: 0, y: 0, draggable: false })]);
|
|
2720
|
+
store.trigger.select({ ids: ['a'] });
|
|
2721
|
+
const before = history(store).past;
|
|
2722
|
+
|
|
2723
|
+
store.trigger.moveNodes({ direction: { x: 1, y: 0 } });
|
|
2724
|
+
|
|
2725
|
+
expect(history(store).past).toBe(before);
|
|
2726
|
+
});
|
|
2727
|
+
});
|
|
2728
|
+
|
|
2729
|
+
// Emitted event payloads must be plain values, not immer draft proxies —
|
|
2730
|
+
// drafts are revoked when the transition finalizes, so a listener touching
|
|
2731
|
+
// event.graph / event.connection / event.viewport would throw.
|
|
2732
|
+
|
|
2733
|
+
describe('emitted payloads are usable after the transition', () => {
|
|
2734
|
+
it('graphChanged carries a readable graph', () => {
|
|
2735
|
+
const store = makeStore([createNode({ id: 'a', x: 1, y: 2 })]);
|
|
2736
|
+
const seen: number[] = [];
|
|
2737
|
+
store.on('graphChanged', (event) => {
|
|
2738
|
+
// deep access + serialization both throw on revoked proxies
|
|
2739
|
+
seen.push(event.graph.nodes[0].x);
|
|
2740
|
+
JSON.stringify(event.graph);
|
|
2741
|
+
});
|
|
2742
|
+
|
|
2743
|
+
store.trigger.createNode({ node: createNode({ id: 'b', x: 50 }) });
|
|
2744
|
+
store.trigger.select({ ids: ['b'] });
|
|
2745
|
+
store.trigger.dragStart({ id: 'b' });
|
|
2746
|
+
store.trigger.dragMove({ dx: 5, dy: 0 });
|
|
2747
|
+
store.trigger.commitDrag();
|
|
2748
|
+
|
|
2749
|
+
expect(seen.length).toBeGreaterThan(1);
|
|
2750
|
+
expect(seen[0]).toBe(1);
|
|
2751
|
+
});
|
|
2752
|
+
|
|
2753
|
+
it('connectionEnded carries a readable connection snapshot', () => {
|
|
2754
|
+
const store = makeStore([createNode({ id: 'a' }), createNode({ id: 'b' })]);
|
|
2755
|
+
let pointerX: number | null = null;
|
|
2756
|
+
let status: string | null = null;
|
|
2757
|
+
store.on('connectionEnded', (event) => {
|
|
2758
|
+
pointerX = event.connection.pointerPosition.x;
|
|
2759
|
+
status = event.status;
|
|
2760
|
+
JSON.stringify(event.connection);
|
|
2761
|
+
});
|
|
2762
|
+
|
|
2763
|
+
store.trigger.connectionStart({
|
|
2764
|
+
sourceId: 'a',
|
|
2765
|
+
sourcePort: null,
|
|
2766
|
+
position: { x: 0, y: 0 },
|
|
2767
|
+
});
|
|
2768
|
+
store.trigger.connectionMove({ position: { x: 42, y: 7 } });
|
|
2769
|
+
store.trigger.connectionEnd({ target: { kind: 'none' } });
|
|
2770
|
+
|
|
2771
|
+
expect(pointerX).toBe(42);
|
|
2772
|
+
expect(status).toBe('cancelled');
|
|
2773
|
+
});
|
|
2774
|
+
|
|
2775
|
+
it('viewportChanged carries a readable viewport (panBy mutation path)', () => {
|
|
2776
|
+
const store = makeStore();
|
|
2777
|
+
let x: number | null = null;
|
|
2778
|
+
store.on('viewportChanged', (event) => {
|
|
2779
|
+
x = event.viewport.x;
|
|
2780
|
+
JSON.stringify(event.viewport);
|
|
2781
|
+
});
|
|
2782
|
+
|
|
2783
|
+
store.trigger.panBy({ x: 10, y: 0 });
|
|
2784
|
+
expect(x).toBe(10);
|
|
2785
|
+
});
|
|
2786
|
+
});
|
|
2787
|
+
|
|
2788
|
+
describe('zoomBy', () => {
|
|
2789
|
+
it('zooms around the viewport center by default', () => {
|
|
2790
|
+
const store = makeStore();
|
|
2791
|
+
store.trigger.updateViewportSize({ width: 800, height: 600 });
|
|
2792
|
+
store.trigger.zoomBy({ factor: 2 });
|
|
2793
|
+
|
|
2794
|
+
const vp = ctx(store).viewport;
|
|
2795
|
+
expect(vp.zoom).toBe(2);
|
|
2796
|
+
// the canvas point under the screen center must be unchanged:
|
|
2797
|
+
// before: (400 - 0) / 1 = 400; after: (400 - vp.x) / 2
|
|
2798
|
+
expect((400 - vp.x) / vp.zoom).toBeCloseTo(400);
|
|
2799
|
+
expect((300 - vp.y) / vp.zoom).toBeCloseTo(300);
|
|
2800
|
+
});
|
|
2801
|
+
|
|
2802
|
+
it('respects min/max zoom', () => {
|
|
2803
|
+
const store = makeStore([], [], { maxZoom: 2.5 });
|
|
2804
|
+
store.trigger.updateViewportSize({ width: 800, height: 600 });
|
|
2805
|
+
store.trigger.zoomBy({ factor: 10 });
|
|
2806
|
+
expect(ctx(store).viewport.zoom).toBe(2.5);
|
|
2807
|
+
store.trigger.zoomBy({ factor: 0.0001 });
|
|
2808
|
+
expect(ctx(store).viewport.zoom).toBe(0.1); // default minZoom
|
|
2809
|
+
});
|
|
2810
|
+
|
|
2811
|
+
it('accepts an explicit center', () => {
|
|
2812
|
+
const store = makeStore();
|
|
2813
|
+
store.trigger.updateViewportSize({ width: 800, height: 600 });
|
|
2814
|
+
store.trigger.zoomBy({ factor: 2, center: { x: 0, y: 0 } });
|
|
2815
|
+
const vp = ctx(store).viewport;
|
|
2816
|
+
expect(vp.x).toBe(0); // zooming around origin keeps it fixed
|
|
2817
|
+
expect(vp.y).toBe(0);
|
|
2818
|
+
});
|
|
2819
|
+
});
|
|
2820
|
+
|
|
2821
|
+
describe('constrainNodeMovement', () => {
|
|
2822
|
+
it('clamps proposed positions during drags (idempotent with the virtual pointer)', () => {
|
|
2823
|
+
const store = createFlowStore(
|
|
2824
|
+
createGraph({
|
|
2825
|
+
nodes: [createNode({ id: 'a', x: 0, y: 0, width: 100, height: 50 })],
|
|
2826
|
+
edges: [],
|
|
2827
|
+
}),
|
|
2828
|
+
{
|
|
2829
|
+
// lock to the x-axis, never past x=200
|
|
2830
|
+
constrainNodeMovement: (node, proposed) => ({
|
|
2831
|
+
x: Math.min(proposed.x, 200),
|
|
2832
|
+
y: 0,
|
|
2833
|
+
}),
|
|
2834
|
+
}
|
|
2835
|
+
);
|
|
2836
|
+
store.trigger.select({ ids: ['a'] });
|
|
2837
|
+
store.trigger.dragStart({ id: 'a' });
|
|
2838
|
+
store.trigger.dragMove({ dx: 500, dy: 300 });
|
|
2839
|
+
store.trigger.dragEnd();
|
|
2840
|
+
store.trigger.commitDrag();
|
|
2841
|
+
|
|
2842
|
+
const a = ctx(store).graph.nodes[0];
|
|
2843
|
+
expect(a.x).toBe(200);
|
|
2844
|
+
expect(a.y).toBe(0);
|
|
2845
|
+
});
|
|
2846
|
+
|
|
2847
|
+
it('applies to keyboard nudges too', () => {
|
|
2848
|
+
const store = createFlowStore(
|
|
2849
|
+
createGraph({ nodes: [createNode({ id: 'a', x: 195, y: 0 })], edges: [] }),
|
|
2850
|
+
{
|
|
2851
|
+
constrainNodeMovement: (node, proposed) => ({
|
|
2852
|
+
x: Math.min(proposed.x, 200),
|
|
2853
|
+
y: proposed.y,
|
|
2854
|
+
}),
|
|
2855
|
+
}
|
|
2856
|
+
);
|
|
2857
|
+
store.trigger.select({ ids: ['a'] });
|
|
2858
|
+
store.trigger.moveNodes({ direction: { x: 1, y: 0 }, factor: 20 });
|
|
2859
|
+
store.trigger.commitDrag();
|
|
2860
|
+
expect(ctx(store).graph.nodes[0].x).toBe(200);
|
|
2861
|
+
});
|
|
2862
|
+
});
|
|
2863
|
+
|
|
2864
|
+
describe('updateMeasurements during drags', () => {
|
|
2865
|
+
it('port offsets are drag-invariant (measured rects include dx)', () => {
|
|
2866
|
+
const store = createFlowStore(
|
|
2867
|
+
createGraph({
|
|
2868
|
+
nodes: [
|
|
2869
|
+
{
|
|
2870
|
+
...createNode({ id: 'a', x: 100, y: 100, width: 100, height: 50 }),
|
|
2871
|
+
ports: [
|
|
2872
|
+
{
|
|
2873
|
+
name: 'in',
|
|
2874
|
+
side: 'top' as const,
|
|
2875
|
+
direction: 'in' as const,
|
|
2876
|
+
x: 44,
|
|
2877
|
+
y: -6,
|
|
2878
|
+
width: 12,
|
|
2879
|
+
height: 12,
|
|
2880
|
+
data: {},
|
|
2881
|
+
},
|
|
2882
|
+
],
|
|
2883
|
+
},
|
|
2884
|
+
],
|
|
2885
|
+
edges: [],
|
|
2886
|
+
})
|
|
2887
|
+
);
|
|
2888
|
+
store.trigger.select({ ids: ['a'] });
|
|
2889
|
+
store.trigger.dragStart({ id: 'a' });
|
|
2890
|
+
store.trigger.dragMove({ dx: 150, dy: 30 });
|
|
2891
|
+
|
|
2892
|
+
// what the DOM reports mid-drag: node visual position + port offset
|
|
2893
|
+
store.trigger.updateMeasurements({
|
|
2894
|
+
measurements: [
|
|
2895
|
+
{ id: 'a:in', rect: { x: 100 + 150 + 44, y: 100 + 30 - 6, width: 12, height: 12 } },
|
|
2896
|
+
],
|
|
2897
|
+
});
|
|
2898
|
+
|
|
2899
|
+
const port = ctx(store).graph.nodes[0].ports![0];
|
|
2900
|
+
expect(port.x).toBe(44);
|
|
2901
|
+
expect(port.y).toBe(-6);
|
|
2902
|
+
});
|
|
2903
|
+
});
|