@statelyai/flow-dom 3.6.2 → 3.7.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/dist/esm/index.d.ts +14 -4
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +93 -12
- package/dist/esm/index.js.map +1 -0
- package/package.json +4 -3
- package/src/appearance-bounds.test.ts +97 -0
- package/src/appearance-bounds.ts +78 -0
- package/src/clipboard.test.ts +85 -0
- package/src/clipboard.ts +145 -0
- package/src/color-mode.test.ts +54 -0
- package/src/color-mode.ts +68 -0
- package/src/container.test.ts +167 -0
- package/src/culling.test.ts +182 -0
- package/src/culling.ts +153 -0
- package/src/entity-geometry.test.ts +391 -0
- package/src/entity-geometry.ts +227 -0
- package/src/exportImage.test.ts +70 -0
- package/src/exportImage.ts +213 -0
- package/src/index.ts +1062 -0
- package/src/input.test.ts +705 -0
- package/src/interaction-cursor.test.ts +20 -0
- package/src/interaction-cursor.ts +20 -0
- package/src/measurement-lifecycle.test.ts +287 -0
- package/src/measurement.test.ts +99 -0
- package/src/render-styles.test.ts +42 -0
- package/src/render-styles.ts +71 -0
- package/src/viewport-transform.test.ts +148 -0
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
createEdge,
|
|
4
|
+
createFlowEngine,
|
|
5
|
+
createFlowStore,
|
|
6
|
+
createGraph,
|
|
7
|
+
createNode,
|
|
8
|
+
type EdgeRouter,
|
|
9
|
+
} from "@statelyai/flow";
|
|
10
|
+
import { createEntityGeometryObserver } from "./entity-geometry";
|
|
11
|
+
|
|
12
|
+
function makeRig(edgeRouter?: EdgeRouter) {
|
|
13
|
+
const store = createFlowStore(
|
|
14
|
+
createGraph({
|
|
15
|
+
nodes: [
|
|
16
|
+
createNode({ id: "a", x: 0, y: 0, width: 100, height: 50 }),
|
|
17
|
+
createNode({ id: "b", x: 300, y: 0, width: 100, height: 50 }),
|
|
18
|
+
],
|
|
19
|
+
edges: [createEdge({ id: "ab", sourceId: "a", targetId: "b" })],
|
|
20
|
+
}),
|
|
21
|
+
);
|
|
22
|
+
const engine = createFlowEngine(store, { edgeRouter });
|
|
23
|
+
const frames: FrameRequestCallback[] = [];
|
|
24
|
+
const observer = createEntityGeometryObserver(store, {
|
|
25
|
+
requestFrame: (callback) => {
|
|
26
|
+
frames.push(callback);
|
|
27
|
+
return frames.length;
|
|
28
|
+
},
|
|
29
|
+
cancelFrame: () => {},
|
|
30
|
+
getEdgeDependents: engine.getEdgeSourceDependents,
|
|
31
|
+
});
|
|
32
|
+
const flushFrame = () => frames.shift()?.(0);
|
|
33
|
+
return { store, engine, observer, flushFrame };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
describe("createEntityGeometryObserver", () => {
|
|
37
|
+
it("can observe again after disconnect", () => {
|
|
38
|
+
const { store, engine, observer, flushFrame } = makeRig();
|
|
39
|
+
const first = document.createElement("div");
|
|
40
|
+
observer.observeRect(first, {
|
|
41
|
+
id: "a",
|
|
42
|
+
getRect: () => engine.visualBounds("a"),
|
|
43
|
+
});
|
|
44
|
+
observer.disconnect();
|
|
45
|
+
|
|
46
|
+
const second = document.createElement("div");
|
|
47
|
+
observer.observeRect(second, {
|
|
48
|
+
id: "a",
|
|
49
|
+
getRect: () => engine.visualBounds("a"),
|
|
50
|
+
});
|
|
51
|
+
store.trigger.select({ ids: ["a"] });
|
|
52
|
+
store.trigger.dragStart({ id: "a" });
|
|
53
|
+
flushFrame();
|
|
54
|
+
store.trigger.dragMove({ dx: 25, dy: 15 });
|
|
55
|
+
flushFrame();
|
|
56
|
+
|
|
57
|
+
expect(second.style.transform).toBe("translate(25px, 15px)");
|
|
58
|
+
observer.disconnect();
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("batches drag transforms into one imperative frame", () => {
|
|
62
|
+
const { store, engine, observer, flushFrame } = makeRig();
|
|
63
|
+
const element = document.createElement("div");
|
|
64
|
+
observer.observeRect(element, {
|
|
65
|
+
id: "a",
|
|
66
|
+
getRect: () => engine.visualBounds("a"),
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
store.trigger.select({ ids: ["a"] });
|
|
70
|
+
store.trigger.dragStart({ id: "a" });
|
|
71
|
+
flushFrame();
|
|
72
|
+
store.trigger.dragMove({ dx: 10, dy: 5 });
|
|
73
|
+
store.trigger.dragMove({ dx: 15, dy: 10 });
|
|
74
|
+
|
|
75
|
+
expect(element.style.transform).toBe("translate(0px, 0px)");
|
|
76
|
+
flushFrame();
|
|
77
|
+
expect(element.style.transform).toBe("translate(25px, 15px)");
|
|
78
|
+
expect(element.style.willChange).toBe("transform");
|
|
79
|
+
|
|
80
|
+
observer.disconnect();
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("batches multi-entity bounds into one imperative drag frame", () => {
|
|
84
|
+
const { store, engine, observer, flushFrame } = makeRig();
|
|
85
|
+
const element = document.createElement("div");
|
|
86
|
+
store.trigger.select({ ids: ["a", "b"] });
|
|
87
|
+
observer.observeBounds(element, {
|
|
88
|
+
ids: ["a", "b"],
|
|
89
|
+
getRect: () => engine.getSelectionBounds(),
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
expect(element.style.transform).toBe("translate(0px, 0px)");
|
|
93
|
+
expect(element.style.width).toBe("400px");
|
|
94
|
+
expect(element.style.height).toBe("50px");
|
|
95
|
+
|
|
96
|
+
store.trigger.dragStart({ id: "a" });
|
|
97
|
+
flushFrame();
|
|
98
|
+
store.trigger.dragMove({ dx: 25, dy: 15 });
|
|
99
|
+
flushFrame();
|
|
100
|
+
|
|
101
|
+
expect(element.style.transform).toBe("translate(25px, 15px)");
|
|
102
|
+
expect(element.style.width).toBe("400px");
|
|
103
|
+
expect(element.style.height).toBe("50px");
|
|
104
|
+
expect(element.style.willChange).toBe("transform, width, height");
|
|
105
|
+
|
|
106
|
+
observer.disconnect();
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("writes transient resize position and size without graph commits", () => {
|
|
110
|
+
const { store, engine, observer, flushFrame } = makeRig();
|
|
111
|
+
const element = document.createElement("div");
|
|
112
|
+
observer.observeRect(element, {
|
|
113
|
+
id: "a",
|
|
114
|
+
getRect: () =>
|
|
115
|
+
store.getSnapshot().context.resizingEntityId === "a"
|
|
116
|
+
? engine.getResizeBounds() ?? engine.visualBounds("a")
|
|
117
|
+
: engine.visualBounds("a"),
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
store.trigger.resizeStart({ id: "a", handle: "top-left" });
|
|
121
|
+
flushFrame();
|
|
122
|
+
store.trigger.resizeMove({ x: -20, y: -10 });
|
|
123
|
+
flushFrame();
|
|
124
|
+
|
|
125
|
+
expect(element.style.transform).toBe("translate(-20px, -10px)");
|
|
126
|
+
expect(element.style.width).toBe("120px");
|
|
127
|
+
expect(element.style.height).toBe("60px");
|
|
128
|
+
expect(element.style.willChange).toBe("transform, width, height");
|
|
129
|
+
|
|
130
|
+
observer.disconnect();
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("writes transient dimensions while resizing a content-sized node", () => {
|
|
134
|
+
const { store, engine, observer, flushFrame } = makeRig();
|
|
135
|
+
const element = document.createElement("div");
|
|
136
|
+
observer.observeRect(element, {
|
|
137
|
+
id: "a",
|
|
138
|
+
autoSize: true,
|
|
139
|
+
getRect: () =>
|
|
140
|
+
store.getSnapshot().context.resizingEntityId === "a"
|
|
141
|
+
? engine.getResizeBounds() ?? engine.visualBounds("a")
|
|
142
|
+
: engine.visualBounds("a"),
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
expect(element.style.width).toBe("");
|
|
146
|
+
store.trigger.resizeStart({ id: "a", handle: "bottom-right" });
|
|
147
|
+
flushFrame();
|
|
148
|
+
store.trigger.resizeMove({ x: 180, y: 90 });
|
|
149
|
+
flushFrame();
|
|
150
|
+
|
|
151
|
+
expect(element.style.width).toBe("180px");
|
|
152
|
+
expect(element.style.height).toBe("90px");
|
|
153
|
+
|
|
154
|
+
observer.disconnect();
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("updates default edge segments and endpoint handles imperatively", () => {
|
|
158
|
+
const { store, engine, observer, flushFrame } = makeRig();
|
|
159
|
+
const group = document.createElementNS("http://www.w3.org/2000/svg", "g");
|
|
160
|
+
const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
|
|
161
|
+
path.dataset.flowEdgeSegment = "0";
|
|
162
|
+
const source = document.createElementNS("http://www.w3.org/2000/svg", "circle");
|
|
163
|
+
source.dataset.flowEdgeEndpoint = "source";
|
|
164
|
+
const target = document.createElementNS("http://www.w3.org/2000/svg", "circle");
|
|
165
|
+
target.dataset.flowEdgeEndpoint = "target";
|
|
166
|
+
group.append(path, source, target);
|
|
167
|
+
observer.observeEdge(group, {
|
|
168
|
+
id: "ab",
|
|
169
|
+
getPathData: () => engine.getAllEdgePaths().get("ab") ?? null,
|
|
170
|
+
});
|
|
171
|
+
const initial = path.getAttribute("d");
|
|
172
|
+
|
|
173
|
+
store.trigger.select({ ids: ["a"] });
|
|
174
|
+
store.trigger.dragStart({ id: "a" });
|
|
175
|
+
flushFrame();
|
|
176
|
+
store.trigger.dragMove({ dx: 50, dy: 20 });
|
|
177
|
+
flushFrame();
|
|
178
|
+
|
|
179
|
+
expect(path.getAttribute("d")).not.toBe(initial);
|
|
180
|
+
expect(source.getAttribute("cx")).toBe("150");
|
|
181
|
+
|
|
182
|
+
observer.disconnect();
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it("retains endpoint hit offsets while routed geometry moves", () => {
|
|
186
|
+
const { store, engine, observer, flushFrame } = makeRig();
|
|
187
|
+
const group = document.createElementNS("http://www.w3.org/2000/svg", "g");
|
|
188
|
+
const source = document.createElementNS("http://www.w3.org/2000/svg", "circle");
|
|
189
|
+
source.dataset.flowEdgeEndpoint = "source";
|
|
190
|
+
source.dataset.flowEdgeEndpointOffsetX = "10";
|
|
191
|
+
source.dataset.flowEdgeEndpointOffsetY = "-2";
|
|
192
|
+
source.setAttribute("r", "10");
|
|
193
|
+
const marker = document.createElementNS("http://www.w3.org/2000/svg", "circle");
|
|
194
|
+
marker.dataset.flowEdgeEndpoint = "source";
|
|
195
|
+
const target = document.createElementNS("http://www.w3.org/2000/svg", "circle");
|
|
196
|
+
target.dataset.flowEdgeEndpoint = "target";
|
|
197
|
+
target.dataset.flowEdgeEndpointOffsetX = "-10";
|
|
198
|
+
group.append(source, marker, target);
|
|
199
|
+
observer.observeEdge(group, {
|
|
200
|
+
id: "ab",
|
|
201
|
+
getPathData: () => engine.getAllEdgePaths().get("ab") ?? null,
|
|
202
|
+
});
|
|
203
|
+
expect(source.getAttribute("cx")).toBe("110");
|
|
204
|
+
expect(marker.getAttribute("cx")).toBe("100");
|
|
205
|
+
expect(target.getAttribute("cx")).toBe("290");
|
|
206
|
+
|
|
207
|
+
store.trigger.select({ ids: ["a"] });
|
|
208
|
+
store.trigger.dragStart({ id: "a" });
|
|
209
|
+
flushFrame();
|
|
210
|
+
store.trigger.dragMove({ dx: 50, dy: 20 });
|
|
211
|
+
flushFrame();
|
|
212
|
+
|
|
213
|
+
expect(source.getAttribute("cx")).toBe("160");
|
|
214
|
+
expect(source.getAttribute("cy")).toBe("43");
|
|
215
|
+
expect(source.getAttribute("r")).toBe("10");
|
|
216
|
+
expect(marker.getAttribute("cx")).toBe("150");
|
|
217
|
+
expect(target.getAttribute("cx")).toBe("290");
|
|
218
|
+
observer.disconnect();
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it("moves an owned edge label imperatively with its dragged group", () => {
|
|
222
|
+
const store = createFlowStore(
|
|
223
|
+
createGraph({
|
|
224
|
+
nodes: [
|
|
225
|
+
createNode({ id: "group", x: 0, y: 0, width: 400, height: 240 }),
|
|
226
|
+
createNode({ id: "a", parentId: "group", x: 40, y: 80 }),
|
|
227
|
+
createNode({ id: "b", parentId: "group", x: 240, y: 80 }),
|
|
228
|
+
],
|
|
229
|
+
edges: [
|
|
230
|
+
createEdge({
|
|
231
|
+
id: "ab",
|
|
232
|
+
sourceId: "a",
|
|
233
|
+
targetId: "b",
|
|
234
|
+
labelAnchor: "fixed",
|
|
235
|
+
label: { x: 160, y: 90, width: 60, height: 20 },
|
|
236
|
+
}),
|
|
237
|
+
],
|
|
238
|
+
}),
|
|
239
|
+
);
|
|
240
|
+
const engine = createFlowEngine(store);
|
|
241
|
+
const frames: FrameRequestCallback[] = [];
|
|
242
|
+
const observer = createEntityGeometryObserver(store, {
|
|
243
|
+
requestFrame: (callback) => {
|
|
244
|
+
frames.push(callback);
|
|
245
|
+
return frames.length;
|
|
246
|
+
},
|
|
247
|
+
cancelFrame: () => {},
|
|
248
|
+
getEdgeDependents: engine.getEdgeSourceDependents,
|
|
249
|
+
});
|
|
250
|
+
const label = document.createElement("div");
|
|
251
|
+
observer.observeRect(label, {
|
|
252
|
+
id: "ab",
|
|
253
|
+
getRect: () => engine.getAllEdgePaths().get("ab")?.labelBounds ?? null,
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
store.trigger.select({ ids: ["group"] });
|
|
257
|
+
store.trigger.dragStart({ id: "group" });
|
|
258
|
+
frames.shift()?.(0);
|
|
259
|
+
store.trigger.dragMove({ dx: 60, dy: 40 });
|
|
260
|
+
frames.shift()?.(0);
|
|
261
|
+
|
|
262
|
+
expect(label.style.transform).toBe("translate(220px, 130px)");
|
|
263
|
+
observer.disconnect();
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it("writes layout-animation node, label, and edge geometry from transient offsets", () => {
|
|
267
|
+
const store = createFlowStore(
|
|
268
|
+
createGraph({
|
|
269
|
+
nodes: [
|
|
270
|
+
createNode({ id: "a", x: 100, y: 40, width: 100, height: 50 }),
|
|
271
|
+
createNode({ id: "b", x: 300, y: 0, width: 100, height: 50 }),
|
|
272
|
+
],
|
|
273
|
+
edges: [
|
|
274
|
+
createEdge({
|
|
275
|
+
id: "ab",
|
|
276
|
+
sourceId: "a",
|
|
277
|
+
targetId: "b",
|
|
278
|
+
label: { x: 220, y: 50, width: 40, height: 20 },
|
|
279
|
+
}),
|
|
280
|
+
],
|
|
281
|
+
}),
|
|
282
|
+
);
|
|
283
|
+
store.trigger.setLayoutAnimationOffsets({
|
|
284
|
+
offsets: new Map([
|
|
285
|
+
["a", { x: -100, y: -40 }],
|
|
286
|
+
["ab", { x: -100, y: -40 }],
|
|
287
|
+
]),
|
|
288
|
+
});
|
|
289
|
+
const engine = createFlowEngine(store);
|
|
290
|
+
const frames: FrameRequestCallback[] = [];
|
|
291
|
+
const observer = createEntityGeometryObserver(store, {
|
|
292
|
+
requestFrame: (callback) => {
|
|
293
|
+
frames.push(callback);
|
|
294
|
+
return frames.length;
|
|
295
|
+
},
|
|
296
|
+
cancelFrame: () => {},
|
|
297
|
+
getEdgeDependents: engine.getEdgeSourceDependents,
|
|
298
|
+
});
|
|
299
|
+
const node = document.createElement("div");
|
|
300
|
+
const label = document.createElement("div");
|
|
301
|
+
const edge = document.createElementNS("http://www.w3.org/2000/svg", "g");
|
|
302
|
+
for (const index of [0, 1]) {
|
|
303
|
+
const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
|
|
304
|
+
path.dataset.flowEdgeSegment = String(index);
|
|
305
|
+
edge.append(path);
|
|
306
|
+
}
|
|
307
|
+
observer.observeRect(node, {
|
|
308
|
+
id: "a",
|
|
309
|
+
getRect: () => engine.visualBounds("a"),
|
|
310
|
+
});
|
|
311
|
+
observer.observeRect(label, {
|
|
312
|
+
id: "ab",
|
|
313
|
+
getRect: () => engine.getAllEdgePaths().get("ab")?.labelBounds ?? null,
|
|
314
|
+
});
|
|
315
|
+
observer.observeEdge(edge, {
|
|
316
|
+
id: "ab",
|
|
317
|
+
getPathData: () => engine.getAllEdgePaths().get("ab") ?? null,
|
|
318
|
+
});
|
|
319
|
+
const initialPath = edge
|
|
320
|
+
.querySelector<SVGPathElement>('[data-flow-edge-segment="0"]')!
|
|
321
|
+
.getAttribute("d");
|
|
322
|
+
|
|
323
|
+
store.trigger.setLayoutAnimationOffsets({
|
|
324
|
+
offsets: new Map([
|
|
325
|
+
["a", { x: -50, y: -20 }],
|
|
326
|
+
["ab", { x: -50, y: -20 }],
|
|
327
|
+
]),
|
|
328
|
+
});
|
|
329
|
+
frames.shift()?.(0);
|
|
330
|
+
|
|
331
|
+
expect(node.style.transform).toBe("translate(50px, 20px)");
|
|
332
|
+
expect(node.style.willChange).toBe("transform");
|
|
333
|
+
expect(label.style.transform).toBe("translate(170px, 30px)");
|
|
334
|
+
expect(
|
|
335
|
+
edge
|
|
336
|
+
.querySelector<SVGPathElement>('[data-flow-edge-segment="0"]')!
|
|
337
|
+
.getAttribute("d"),
|
|
338
|
+
).not.toBe(initialPath);
|
|
339
|
+
|
|
340
|
+
store.trigger.clearLayoutAnimation();
|
|
341
|
+
frames.shift()?.(0);
|
|
342
|
+
expect(node.style.transform).toBe("translate(100px, 40px)");
|
|
343
|
+
expect(node.style.willChange).toBe("");
|
|
344
|
+
expect(label.style.transform).toBe("translate(220px, 50px)");
|
|
345
|
+
observer.disconnect();
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
it("writes router-provided SVG paths during imperative edge updates", () => {
|
|
349
|
+
const router: EdgeRouter = (_edge, context) => {
|
|
350
|
+
const source = {
|
|
351
|
+
x: context.sourceBounds.x + context.sourceBounds.width,
|
|
352
|
+
y: context.sourceBounds.y + context.sourceBounds.height / 2,
|
|
353
|
+
};
|
|
354
|
+
const target = {
|
|
355
|
+
x: context.targetBounds.x,
|
|
356
|
+
y: context.targetBounds.y + context.targetBounds.height / 2,
|
|
357
|
+
};
|
|
358
|
+
const control = {
|
|
359
|
+
x: (source.x + target.x) / 2,
|
|
360
|
+
y: source.y - 30,
|
|
361
|
+
};
|
|
362
|
+
return {
|
|
363
|
+
points: [source, target],
|
|
364
|
+
sourceDirection: context.sourceDirection,
|
|
365
|
+
targetDirection: context.targetDirection,
|
|
366
|
+
svgPath: `M ${source.x} ${source.y} Q ${control.x} ${control.y} ${target.x} ${target.y}`,
|
|
367
|
+
};
|
|
368
|
+
};
|
|
369
|
+
const { store, engine, observer, flushFrame } = makeRig(router);
|
|
370
|
+
const group = document.createElementNS("http://www.w3.org/2000/svg", "g");
|
|
371
|
+
const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
|
|
372
|
+
path.dataset.flowEdgeSegment = "0";
|
|
373
|
+
group.append(path);
|
|
374
|
+
observer.observeEdge(group, {
|
|
375
|
+
id: "ab",
|
|
376
|
+
getPathData: () => engine.getAllEdgePaths().get("ab") ?? null,
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
expect(path.getAttribute("d")).toBe("M 100 25 Q 200 -5 300 25");
|
|
380
|
+
|
|
381
|
+
store.trigger.select({ ids: ["a"] });
|
|
382
|
+
store.trigger.dragStart({ id: "a" });
|
|
383
|
+
flushFrame();
|
|
384
|
+
store.trigger.dragMove({ dx: 50, dy: 20 });
|
|
385
|
+
flushFrame();
|
|
386
|
+
|
|
387
|
+
expect(path.getAttribute("d")).toBe("M 150 45 Q 225 15 300 25");
|
|
388
|
+
|
|
389
|
+
observer.disconnect();
|
|
390
|
+
});
|
|
391
|
+
});
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createEntityChangeNotifier,
|
|
3
|
+
pathToSVG,
|
|
4
|
+
type EdgePathData,
|
|
5
|
+
type FlowEntityChangeNotifierOptions,
|
|
6
|
+
type FlowStore,
|
|
7
|
+
type Rect,
|
|
8
|
+
} from "@statelyai/flow";
|
|
9
|
+
|
|
10
|
+
export type EntityRectTarget = {
|
|
11
|
+
id: string;
|
|
12
|
+
getRect: () => Rect | null;
|
|
13
|
+
autoSize?: boolean;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type EntityBoundsTarget = {
|
|
17
|
+
ids: readonly string[];
|
|
18
|
+
getRect: () => Rect | null;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type EntityEdgeTarget = {
|
|
22
|
+
id: string;
|
|
23
|
+
getPathData: () => EdgePathData | null;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type EntityGeometryObserverOptions =
|
|
27
|
+
FlowEntityChangeNotifierOptions & {
|
|
28
|
+
requestFrame?: (callback: FrameRequestCallback) => number;
|
|
29
|
+
cancelFrame?: (handle: number) => void;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type EntityGeometryObserver = {
|
|
33
|
+
observeRect(element: HTMLElement, target: EntityRectTarget): () => void;
|
|
34
|
+
observeBounds(element: HTMLElement, target: EntityBoundsTarget): () => void;
|
|
35
|
+
observeEdge(element: SVGElement, target: EntityEdgeTarget): () => void;
|
|
36
|
+
/** Flush pending writes synchronously. Primarily useful for deterministic hosts/tests. */
|
|
37
|
+
flush(): void;
|
|
38
|
+
disconnect(): void;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
type Observation = {
|
|
42
|
+
write: () => void;
|
|
43
|
+
unsubscribe: () => void;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Batches transient entity geometry into imperative DOM writes. One core
|
|
48
|
+
* entity-change notifier keeps dispatch O(changed); framework adapters retain
|
|
49
|
+
* ownership of content, selection, and other durable rendering.
|
|
50
|
+
*/
|
|
51
|
+
export function createEntityGeometryObserver(
|
|
52
|
+
store: FlowStore,
|
|
53
|
+
options: EntityGeometryObserverOptions = {},
|
|
54
|
+
): EntityGeometryObserver {
|
|
55
|
+
const requestFrame =
|
|
56
|
+
options.requestFrame ?? ((callback) => requestAnimationFrame(callback));
|
|
57
|
+
const cancelFrame =
|
|
58
|
+
options.cancelFrame ?? ((handle) => cancelAnimationFrame(handle));
|
|
59
|
+
const createNotifier = () =>
|
|
60
|
+
createEntityChangeNotifier(store, {
|
|
61
|
+
getEdgeDependents: options.getEdgeDependents,
|
|
62
|
+
});
|
|
63
|
+
let notifier: ReturnType<typeof createEntityChangeNotifier> | null =
|
|
64
|
+
createNotifier();
|
|
65
|
+
const getNotifier = () => (notifier ??= createNotifier());
|
|
66
|
+
const observations = new Set<Observation>();
|
|
67
|
+
const pending = new Set<Observation>();
|
|
68
|
+
let frame: number | null = null;
|
|
69
|
+
|
|
70
|
+
const flush = () => {
|
|
71
|
+
if (frame !== null) {
|
|
72
|
+
cancelFrame(frame);
|
|
73
|
+
frame = null;
|
|
74
|
+
}
|
|
75
|
+
const current = [...pending];
|
|
76
|
+
pending.clear();
|
|
77
|
+
for (const observation of current) observation.write();
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const schedule = (observation: Observation) => {
|
|
81
|
+
pending.add(observation);
|
|
82
|
+
if (frame === null) {
|
|
83
|
+
frame = requestFrame(() => {
|
|
84
|
+
frame = null;
|
|
85
|
+
const current = [...pending];
|
|
86
|
+
pending.clear();
|
|
87
|
+
for (const pendingObservation of current) pendingObservation.write();
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const observeIds = (
|
|
93
|
+
ids: Iterable<string>,
|
|
94
|
+
write: () => void,
|
|
95
|
+
): (() => void) => {
|
|
96
|
+
const observation = {
|
|
97
|
+
write,
|
|
98
|
+
unsubscribe: () => {},
|
|
99
|
+
};
|
|
100
|
+
const unsubscribers = [...new Set(ids)].map((id) =>
|
|
101
|
+
getNotifier().subscribe(id, () => schedule(observation)),
|
|
102
|
+
);
|
|
103
|
+
observation.unsubscribe = () => {
|
|
104
|
+
for (const unsubscribe of unsubscribers) unsubscribe();
|
|
105
|
+
};
|
|
106
|
+
observations.add(observation);
|
|
107
|
+
write();
|
|
108
|
+
|
|
109
|
+
return () => {
|
|
110
|
+
observation.unsubscribe();
|
|
111
|
+
observations.delete(observation);
|
|
112
|
+
pending.delete(observation);
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const observe = (id: string, write: () => void): (() => void) =>
|
|
117
|
+
observeIds([id], write);
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
observeRect(element, target) {
|
|
121
|
+
return observe(target.id, () => {
|
|
122
|
+
const rect = target.getRect();
|
|
123
|
+
if (!rect) return;
|
|
124
|
+
const context = store.getSnapshot().context;
|
|
125
|
+
const dragging =
|
|
126
|
+
context.canvasState === "dragging" && context.selection.has(target.id);
|
|
127
|
+
const resizing = context.resizingEntityId === target.id;
|
|
128
|
+
const layoutAnimating =
|
|
129
|
+
context.layoutAnimation.status === "running" &&
|
|
130
|
+
context.layoutAnimation.offsets.has(target.id);
|
|
131
|
+
|
|
132
|
+
element.style.transform = `translate(${rect.x}px, ${rect.y}px)`;
|
|
133
|
+
// An auto-sized node is content-owned until an explicit resize begins.
|
|
134
|
+
// During the gesture Flow owns the live rectangle, so the hot path must
|
|
135
|
+
// still write width/height without waiting for a framework render.
|
|
136
|
+
if (!target.autoSize || resizing) {
|
|
137
|
+
element.style.width = `${rect.width}px`;
|
|
138
|
+
element.style.height = `${rect.height}px`;
|
|
139
|
+
}
|
|
140
|
+
if (dragging || resizing || layoutAnimating) {
|
|
141
|
+
element.style.willChange = resizing
|
|
142
|
+
? "transform, width, height"
|
|
143
|
+
: "transform";
|
|
144
|
+
} else {
|
|
145
|
+
element.style.removeProperty("will-change");
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
},
|
|
149
|
+
|
|
150
|
+
observeBounds(element, target) {
|
|
151
|
+
return observeIds(target.ids, () => {
|
|
152
|
+
const rect = target.getRect();
|
|
153
|
+
if (!rect) return;
|
|
154
|
+
const context = store.getSnapshot().context;
|
|
155
|
+
|
|
156
|
+
element.style.transform = `translate(${rect.x}px, ${rect.y}px)`;
|
|
157
|
+
element.style.width = `${rect.width}px`;
|
|
158
|
+
element.style.height = `${rect.height}px`;
|
|
159
|
+
if (
|
|
160
|
+
context.canvasState === "dragging" ||
|
|
161
|
+
context.canvasState === "resizing" ||
|
|
162
|
+
context.layoutAnimation.status === "running"
|
|
163
|
+
) {
|
|
164
|
+
element.style.willChange = "transform, width, height";
|
|
165
|
+
} else {
|
|
166
|
+
element.style.removeProperty("will-change");
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
},
|
|
170
|
+
|
|
171
|
+
observeEdge(element, target) {
|
|
172
|
+
// Legacy custom renderers emit route paths without segment attributes.
|
|
173
|
+
// Bind exact route copies (including hit strokes) once, then retain the
|
|
174
|
+
// binding as their d values change. Unrelated decoration paths stay owned
|
|
175
|
+
// by the renderer; explicit segment attributes always take precedence.
|
|
176
|
+
const inferredSegments = new WeakMap<SVGPathElement, number>();
|
|
177
|
+
return observe(target.id, () => {
|
|
178
|
+
const pathData = target.getPathData();
|
|
179
|
+
if (!pathData) return;
|
|
180
|
+
|
|
181
|
+
const svgSegments = pathData.segments.map(pathToSVG);
|
|
182
|
+
element
|
|
183
|
+
.querySelectorAll<SVGPathElement>("path")
|
|
184
|
+
.forEach((path) => {
|
|
185
|
+
const explicitIndex = path.dataset.flowEdgeSegment;
|
|
186
|
+
let index = explicitIndex === undefined
|
|
187
|
+
? inferredSegments.get(path)
|
|
188
|
+
: Number(explicitIndex);
|
|
189
|
+
if (index === undefined) {
|
|
190
|
+
const matchedIndex = svgSegments.indexOf(path.getAttribute("d") ?? "");
|
|
191
|
+
if (matchedIndex < 0) return;
|
|
192
|
+
index = matchedIndex;
|
|
193
|
+
inferredSegments.set(path, index);
|
|
194
|
+
}
|
|
195
|
+
const segment = svgSegments[index];
|
|
196
|
+
if (segment !== undefined) path.setAttribute("d", segment);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
const first = pathData.segments[0]?.points[0];
|
|
200
|
+
const last = pathData.segments.at(-1)?.points.at(-1);
|
|
201
|
+
for (const [end, point] of [["source", first], ["target", last]] as const) {
|
|
202
|
+
if (!point) continue;
|
|
203
|
+
element.querySelectorAll<SVGCircleElement>(
|
|
204
|
+
`[data-flow-edge-endpoint="${end}"]`,
|
|
205
|
+
).forEach((handle) => {
|
|
206
|
+
const x = Number(handle.dataset.flowEdgeEndpointOffsetX ?? 0);
|
|
207
|
+
const y = Number(handle.dataset.flowEdgeEndpointOffsetY ?? 0);
|
|
208
|
+
handle.setAttribute("cx", String(point.x + (Number.isFinite(x) ? x : 0)));
|
|
209
|
+
handle.setAttribute("cy", String(point.y + (Number.isFinite(y) ? y : 0)));
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
},
|
|
214
|
+
|
|
215
|
+
flush,
|
|
216
|
+
|
|
217
|
+
disconnect() {
|
|
218
|
+
if (frame !== null) cancelFrame(frame);
|
|
219
|
+
frame = null;
|
|
220
|
+
pending.clear();
|
|
221
|
+
for (const observation of observations) observation.unsubscribe();
|
|
222
|
+
observations.clear();
|
|
223
|
+
notifier?.destroy();
|
|
224
|
+
notifier = null;
|
|
225
|
+
},
|
|
226
|
+
};
|
|
227
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { exportFlowImage } from './exportImage';
|
|
3
|
+
|
|
4
|
+
function readBlob(blob: Blob): Promise<string> {
|
|
5
|
+
return new Promise((resolve, reject) => {
|
|
6
|
+
const reader = new FileReader();
|
|
7
|
+
reader.onload = () => resolve(String(reader.result));
|
|
8
|
+
reader.onerror = () => reject(reader.error);
|
|
9
|
+
reader.readAsText(blob);
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
describe('exportFlowImage', () => {
|
|
14
|
+
it('returns an SVG blob without mutating the view', async () => {
|
|
15
|
+
vi.spyOn(window, 'requestAnimationFrame').mockImplementation((callback) => {
|
|
16
|
+
callback(0);
|
|
17
|
+
return 1;
|
|
18
|
+
});
|
|
19
|
+
const container = document.createElement('div');
|
|
20
|
+
container.dataset.flowRoot = '';
|
|
21
|
+
container.innerHTML = '<div data-entity-id="a">A</div>';
|
|
22
|
+
Object.defineProperty(container, 'getBoundingClientRect', {
|
|
23
|
+
value: () => ({ x: 0, y: 0, left: 0, top: 0, right: 320, bottom: 200, width: 320, height: 200, toJSON() {} }),
|
|
24
|
+
});
|
|
25
|
+
const entity = container.firstElementChild as HTMLElement;
|
|
26
|
+
Object.defineProperty(entity, 'getBoundingClientRect', {
|
|
27
|
+
value: () => ({ x: 10, y: 20, left: 10, top: 20, right: 110, bottom: 60, width: 100, height: 40, toJSON() {} }),
|
|
28
|
+
});
|
|
29
|
+
const before = container.outerHTML;
|
|
30
|
+
const result = await exportFlowImage(container, { format: 'svg', padding: 5 });
|
|
31
|
+
expect(result).toMatchObject({ width: 110, height: 50 });
|
|
32
|
+
expect(result.blob.type).toBe('image/svg+xml');
|
|
33
|
+
const markup = await readBlob(result.blob);
|
|
34
|
+
expect(markup).toContain('data-entity-id="a"');
|
|
35
|
+
expect(container.outerHTML).toBe(before);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('inlines computed styles by default with an opt-out', async () => {
|
|
39
|
+
vi.spyOn(window, 'requestAnimationFrame').mockImplementation((callback) => {
|
|
40
|
+
callback(0);
|
|
41
|
+
return 1;
|
|
42
|
+
});
|
|
43
|
+
const stylesheet = document.createElement('style');
|
|
44
|
+
stylesheet.textContent = '.external-theme { color: rgb(1, 2, 3); }';
|
|
45
|
+
document.head.append(stylesheet);
|
|
46
|
+
const container = document.createElement('div');
|
|
47
|
+
container.innerHTML =
|
|
48
|
+
'<div class="external-theme" data-entity-id="a">Styled</div>';
|
|
49
|
+
Object.defineProperty(container, 'getBoundingClientRect', {
|
|
50
|
+
value: () => ({ x: 0, y: 0, left: 0, top: 0, right: 100, bottom: 50, width: 100, height: 50, toJSON() {} }),
|
|
51
|
+
});
|
|
52
|
+
Object.defineProperty(container.firstElementChild, 'getBoundingClientRect', {
|
|
53
|
+
value: () => ({ x: 0, y: 0, left: 0, top: 0, right: 100, bottom: 50, width: 100, height: 50, toJSON() {} }),
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
try {
|
|
57
|
+
const withStyles = await readBlob(
|
|
58
|
+
(await exportFlowImage(container, { format: 'svg' })).blob,
|
|
59
|
+
);
|
|
60
|
+
const withoutStyles = await readBlob(
|
|
61
|
+
(await exportFlowImage(container, { format: 'svg', styles: 'none' })).blob,
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
expect(withStyles).toContain('color: rgb(1, 2, 3)');
|
|
65
|
+
expect(withoutStyles).not.toContain('color: rgb(1, 2, 3)');
|
|
66
|
+
} finally {
|
|
67
|
+
stylesheet.remove();
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
});
|