@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,182 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
createFlowStore,
|
|
4
|
+
createFlowEngine,
|
|
5
|
+
createEdge,
|
|
6
|
+
createGraph,
|
|
7
|
+
createNode,
|
|
8
|
+
} from "@statelyai/flow";
|
|
9
|
+
import {
|
|
10
|
+
computeRenderableIds,
|
|
11
|
+
computeSettledRenderableIds,
|
|
12
|
+
renderableIdsEqual,
|
|
13
|
+
} from "./index";
|
|
14
|
+
|
|
15
|
+
function setup(nodes: ReturnType<typeof createNode>[]) {
|
|
16
|
+
const store = createFlowStore(createGraph({ nodes, edges: [] }));
|
|
17
|
+
const engine = createFlowEngine(store);
|
|
18
|
+
return { store, engine };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
describe("computeRenderableIds", () => {
|
|
22
|
+
it("returns null when culling is disabled", () => {
|
|
23
|
+
const { store, engine } = setup([
|
|
24
|
+
createNode({ id: "a", x: 0, y: 0, width: 50, height: 50 }),
|
|
25
|
+
]);
|
|
26
|
+
store.trigger.updateViewportSize({ width: 800, height: 600 });
|
|
27
|
+
expect(
|
|
28
|
+
computeRenderableIds(engine, store.getSnapshot().context, false)
|
|
29
|
+
).toBeNull();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("returns null when the viewport is unmeasured (0x0)", () => {
|
|
33
|
+
const { store, engine } = setup([
|
|
34
|
+
createNode({ id: "a", x: 0, y: 0, width: 50, height: 50 }),
|
|
35
|
+
]);
|
|
36
|
+
expect(
|
|
37
|
+
computeRenderableIds(engine, store.getSnapshot().context, true)
|
|
38
|
+
).toBeNull();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("returns null (render all) when the whole graph fits the viewport", () => {
|
|
42
|
+
const { store, engine } = setup([
|
|
43
|
+
createNode({ id: "a", x: 0, y: 0, width: 50, height: 50 }),
|
|
44
|
+
createNode({ id: "b", x: 100, y: 0, width: 50, height: 50 }),
|
|
45
|
+
]);
|
|
46
|
+
store.trigger.updateViewportSize({ width: 2000, height: 2000 });
|
|
47
|
+
// graph (0..150 × 0..50) fits the 2000×2000 viewport → coverage gate
|
|
48
|
+
expect(
|
|
49
|
+
computeRenderableIds(engine, store.getSnapshot().context, true)
|
|
50
|
+
).toBeNull();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("culls to the viewport when the graph exceeds it", () => {
|
|
54
|
+
const { store, engine } = setup([
|
|
55
|
+
createNode({ id: "near", x: 0, y: 0, width: 50, height: 50 }),
|
|
56
|
+
createNode({ id: "far", x: 5000, y: 5000, width: 50, height: 50 }),
|
|
57
|
+
]);
|
|
58
|
+
store.trigger.updateViewportSize({ width: 400, height: 300 });
|
|
59
|
+
const ids = computeRenderableIds(engine, store.getSnapshot().context, true);
|
|
60
|
+
expect(ids).not.toBeNull();
|
|
61
|
+
expect(ids!.nodeIds.has("near")).toBe(true);
|
|
62
|
+
expect(ids!.nodeIds.has("far")).toBe(false);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("uses the configured overscan", () => {
|
|
66
|
+
const { store, engine } = setup([
|
|
67
|
+
createNode({ id: "near", x: 0, y: 0, width: 50, height: 50 }),
|
|
68
|
+
createNode({ id: "edge", x: 450, y: 0, width: 50, height: 50 }),
|
|
69
|
+
createNode({ id: "far", x: 5000, y: 5000, width: 50, height: 50 }),
|
|
70
|
+
]);
|
|
71
|
+
store.trigger.updateViewportSize({ width: 400, height: 300 });
|
|
72
|
+
|
|
73
|
+
const tight = computeRenderableIds(engine, store.getSnapshot().context, true, {
|
|
74
|
+
overscan: 0,
|
|
75
|
+
});
|
|
76
|
+
const padded = computeRenderableIds(engine, store.getSnapshot().context, true, {
|
|
77
|
+
overscan: 100,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
expect(tight!.nodeIds.has("edge")).toBe(false);
|
|
81
|
+
expect(padded!.nodeIds.has("edge")).toBe(true);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("includes authoritative edge geometry in coverage and culling", () => {
|
|
85
|
+
const nodes = [
|
|
86
|
+
createNode({ id: "a", x: 0, y: 0, width: 50, height: 50 }),
|
|
87
|
+
createNode({ id: "b", x: 100, y: 0, width: 50, height: 50 }),
|
|
88
|
+
];
|
|
89
|
+
const store = createFlowStore(
|
|
90
|
+
createGraph({
|
|
91
|
+
nodes,
|
|
92
|
+
edges: [
|
|
93
|
+
createEdge({
|
|
94
|
+
id: "route",
|
|
95
|
+
sourceId: "a",
|
|
96
|
+
targetId: "b",
|
|
97
|
+
points: [
|
|
98
|
+
{ x: 25, y: 25 },
|
|
99
|
+
{ x: 25, y: 1000 },
|
|
100
|
+
{ x: 125, y: 1000 },
|
|
101
|
+
{ x: 125, y: 25 },
|
|
102
|
+
],
|
|
103
|
+
routing: "orthogonal",
|
|
104
|
+
}),
|
|
105
|
+
],
|
|
106
|
+
}),
|
|
107
|
+
);
|
|
108
|
+
const engine = createFlowEngine(store);
|
|
109
|
+
store.trigger.updateViewportSize({ width: 300, height: 200 });
|
|
110
|
+
|
|
111
|
+
const ids = computeRenderableIds(engine, store.getSnapshot().context, true, {
|
|
112
|
+
overscan: 0,
|
|
113
|
+
});
|
|
114
|
+
expect(ids).not.toBeNull();
|
|
115
|
+
expect(ids?.edgeIds.has("route")).toBe(true);
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
describe("renderableIdsEqual", () => {
|
|
120
|
+
it("treats null as equal to null and unequal to a set", () => {
|
|
121
|
+
expect(renderableIdsEqual(null, null)).toBe(true);
|
|
122
|
+
expect(
|
|
123
|
+
renderableIdsEqual(null, { nodeIds: new Set(["a"]), edgeIds: new Set() })
|
|
124
|
+
).toBe(false);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("compares by set contents (order-independent)", () => {
|
|
128
|
+
const a = { nodeIds: new Set(["a", "b"]), edgeIds: new Set(["e"]) };
|
|
129
|
+
const b = { nodeIds: new Set(["b", "a"]), edgeIds: new Set(["e"]) };
|
|
130
|
+
const c = { nodeIds: new Set(["a"]), edgeIds: new Set(["e"]) };
|
|
131
|
+
expect(renderableIdsEqual(a, b)).toBe(true);
|
|
132
|
+
expect(renderableIdsEqual(a, c)).toBe(false);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
describe("computeSettledRenderableIds", () => {
|
|
137
|
+
function farSetup() {
|
|
138
|
+
const result = setup([
|
|
139
|
+
createNode({ id: "near", x: 0, y: 0, width: 50, height: 50 }),
|
|
140
|
+
createNode({ id: "far", x: 2000, y: 0, width: 50, height: 50 }),
|
|
141
|
+
]);
|
|
142
|
+
result.store.trigger.updateViewportSize({ width: 800, height: 600 });
|
|
143
|
+
return result;
|
|
144
|
+
}
|
|
145
|
+
const context = (store: ReturnType<typeof setup>["store"]) =>
|
|
146
|
+
store.getSnapshot().context;
|
|
147
|
+
|
|
148
|
+
it("holds the previous cull while the live zoom differs from settledZoom", () => {
|
|
149
|
+
const { store, engine } = farSetup();
|
|
150
|
+
const first = computeSettledRenderableIds(engine, context(store), true, 1, null);
|
|
151
|
+
expect(first.value?.nodeIds.has("far")).toBe(false);
|
|
152
|
+
|
|
153
|
+
store.trigger.setViewport({ x: 0, y: 0, zoom: 0.25 });
|
|
154
|
+
const held = computeSettledRenderableIds(engine, context(store), true, 1, first);
|
|
155
|
+
expect(held).toBe(first);
|
|
156
|
+
|
|
157
|
+
const settled = computeSettledRenderableIds(engine, context(store), true, 0.25, held);
|
|
158
|
+
expect(settled.value).toBeNull();
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("recomputes when settledZoom changes to a value that still differs from the live zoom", () => {
|
|
162
|
+
const { store, engine } = farSetup();
|
|
163
|
+
const first = computeSettledRenderableIds(engine, context(store), true, 1, null);
|
|
164
|
+
store.trigger.setViewport({ x: 0, y: 0, zoom: 0.25 });
|
|
165
|
+
|
|
166
|
+
const next = computeSettledRenderableIds(engine, context(store), true, 0.2501, first);
|
|
167
|
+
expect(next).not.toBe(first);
|
|
168
|
+
expect(next.value).toBeNull();
|
|
169
|
+
expect(
|
|
170
|
+
computeSettledRenderableIds(engine, context(store), true, 0.2501, next)
|
|
171
|
+
).toBe(next);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("always recomputes when settledZoom is omitted", () => {
|
|
175
|
+
const { store, engine } = farSetup();
|
|
176
|
+
const first = computeSettledRenderableIds(engine, context(store), true, undefined, null);
|
|
177
|
+
store.trigger.setViewport({ x: 0, y: 0, zoom: 0.25 });
|
|
178
|
+
const next = computeSettledRenderableIds(engine, context(store), true, undefined, first);
|
|
179
|
+
expect(next).not.toBe(first);
|
|
180
|
+
expect(next.value).toBeNull();
|
|
181
|
+
});
|
|
182
|
+
});
|
package/src/culling.ts
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import type { FlowEngine, FlowStoreContext } from "@statelyai/flow";
|
|
2
|
+
|
|
3
|
+
// Culling (framework-agnostic "what to mount" policy)
|
|
4
|
+
//
|
|
5
|
+
// Decides which entities a DOM adapter should mount for the current viewport.
|
|
6
|
+
// This is pure rendering policy with no framework or DOM dependency, so it
|
|
7
|
+
// lives here (the source of rendering truth) rather than being re-derived in
|
|
8
|
+
// every adapter — including the coverage gate that made zoomed-out drags laggy
|
|
9
|
+
// when each adapter forgot it. See ADAPTER_NOTES.md.
|
|
10
|
+
|
|
11
|
+
/** Overscan (canvas px) added around the viewport so entities just off-screen
|
|
12
|
+
* stay mounted and don't pop in/out at the edges. */
|
|
13
|
+
const CULL_OVERSCAN = 100;
|
|
14
|
+
|
|
15
|
+
export type ComputeRenderableIdsOptions = {
|
|
16
|
+
overscan?: number;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Node/edge ids worth mounting, or `null` meaning "render everything" — either
|
|
21
|
+
* because culling is disabled, the viewport hasn't been measured yet, or the
|
|
22
|
+
* whole graph already fits the viewport (so culling would mount everything
|
|
23
|
+
* anyway). Adapters mount exactly these ids; `null` means mount all.
|
|
24
|
+
*/
|
|
25
|
+
export type RenderableIds = {
|
|
26
|
+
nodeIds: Set<string>;
|
|
27
|
+
edgeIds: Set<string>;
|
|
28
|
+
} | null;
|
|
29
|
+
|
|
30
|
+
function idSetsEqual(a: Set<string>, b: Set<string>): boolean {
|
|
31
|
+
if (a.size !== b.size) return false;
|
|
32
|
+
for (const value of a) {
|
|
33
|
+
if (!b.has(value)) return false;
|
|
34
|
+
}
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Compare two `computeRenderableIds` results by contents. Adapters use this as
|
|
40
|
+
* the equality function for their reactive subscription so a layer only
|
|
41
|
+
* re-renders when the visible set actually changes — not on every store
|
|
42
|
+
* transition (e.g. every drag frame).
|
|
43
|
+
*/
|
|
44
|
+
export function renderableIdsEqual(a: RenderableIds, b: RenderableIds): boolean {
|
|
45
|
+
return (
|
|
46
|
+
a === b ||
|
|
47
|
+
(a !== null &&
|
|
48
|
+
b !== null &&
|
|
49
|
+
idSetsEqual(a.nodeIds, b.nodeIds) &&
|
|
50
|
+
idSetsEqual(a.edgeIds, b.edgeIds))
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Which entities should be mounted for the current viewport, or `null` to mount
|
|
56
|
+
* everything.
|
|
57
|
+
*
|
|
58
|
+
* Returns `null` when:
|
|
59
|
+
* - `enabled` is false (culling off),
|
|
60
|
+
* - the viewport is unmeasured (0×0 — culling against it would blank the first
|
|
61
|
+
* paint),
|
|
62
|
+
* - or the whole graph fits the viewport (+overscan). In that case culling
|
|
63
|
+
* would mount every entity anyway, so we skip the per-frame O(n+e) cull and
|
|
64
|
+
* the N-id Set build entirely — running it for zero benefit is what makes a
|
|
65
|
+
* zoomed-out drag laggy. The containment check is one O(n) `getGraphBounds`,
|
|
66
|
+
* far cheaper than the cull it avoids.
|
|
67
|
+
*
|
|
68
|
+
* Otherwise returns the visible node/edge id sets (viewport cull + overscan).
|
|
69
|
+
*
|
|
70
|
+
* `context` must be the engine's current snapshot (the engine reads its own
|
|
71
|
+
* `getGraphBounds`/`getRenderableEntities` from the live store) — which holds
|
|
72
|
+
* when called inside a store-subscription selector.
|
|
73
|
+
*/
|
|
74
|
+
export function computeRenderableIds(
|
|
75
|
+
engine: FlowEngine,
|
|
76
|
+
context: FlowStoreContext,
|
|
77
|
+
enabled: boolean | undefined,
|
|
78
|
+
options: ComputeRenderableIdsOptions = {}
|
|
79
|
+
): RenderableIds {
|
|
80
|
+
if (!enabled) return null;
|
|
81
|
+
const overscan = options.overscan ?? CULL_OVERSCAN;
|
|
82
|
+
|
|
83
|
+
const viewportSize = context.viewportSize;
|
|
84
|
+
if (viewportSize.width === 0 || viewportSize.height === 0) return null;
|
|
85
|
+
|
|
86
|
+
// Coverage gate: the graph already fits the viewport → mounting everything is
|
|
87
|
+
// unavoidable, so don't pay for the cull.
|
|
88
|
+
const viewport = context.viewport;
|
|
89
|
+
const left = -viewport.x / viewport.zoom - overscan;
|
|
90
|
+
const top = -viewport.y / viewport.zoom - overscan;
|
|
91
|
+
const right = left + viewportSize.width / viewport.zoom + overscan * 2;
|
|
92
|
+
const bottom = top + viewportSize.height / viewport.zoom + overscan * 2;
|
|
93
|
+
const bounds = engine.getGraphBounds();
|
|
94
|
+
if (
|
|
95
|
+
left <= bounds.x &&
|
|
96
|
+
top <= bounds.y &&
|
|
97
|
+
right >= bounds.x + bounds.width &&
|
|
98
|
+
bottom >= bounds.y + bounds.height
|
|
99
|
+
) {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const renderable = engine.getRenderableEntities({
|
|
104
|
+
cull: "viewport",
|
|
105
|
+
overscan,
|
|
106
|
+
});
|
|
107
|
+
return {
|
|
108
|
+
nodeIds: new Set(renderable.nodes.map((node) => node.id)),
|
|
109
|
+
edgeIds: new Set(renderable.edges.map((edge) => edge.id)),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** A cull and the inputs it was computed for. See `computeSettledRenderableIds`. */
|
|
114
|
+
export type SettledRenderableIds = {
|
|
115
|
+
value: RenderableIds;
|
|
116
|
+
graph: FlowStoreContext["graph"];
|
|
117
|
+
enabled: boolean | undefined;
|
|
118
|
+
settledZoom: number | undefined;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* `computeRenderableIds` that holds the previous cull during a zoom gesture.
|
|
123
|
+
* While the live zoom differs from `settledZoom`, it returns `previous` as long
|
|
124
|
+
* as the graph, `enabled` and `settledZoom` are unchanged: mounting what a zoom
|
|
125
|
+
* frame reveals costs far more than the transform. Any other case recomputes.
|
|
126
|
+
* Omitting `settledZoom` always recomputes. Pass the returned value back as
|
|
127
|
+
* `previous` on the next call.
|
|
128
|
+
*/
|
|
129
|
+
export function computeSettledRenderableIds(
|
|
130
|
+
engine: FlowEngine,
|
|
131
|
+
context: FlowStoreContext,
|
|
132
|
+
enabled: boolean | undefined,
|
|
133
|
+
settledZoom: number | undefined,
|
|
134
|
+
previous: SettledRenderableIds | null,
|
|
135
|
+
options: ComputeRenderableIdsOptions = {}
|
|
136
|
+
): SettledRenderableIds {
|
|
137
|
+
if (
|
|
138
|
+
settledZoom !== undefined &&
|
|
139
|
+
context.viewport.zoom !== settledZoom &&
|
|
140
|
+
previous &&
|
|
141
|
+
previous.graph === context.graph &&
|
|
142
|
+
previous.enabled === enabled &&
|
|
143
|
+
previous.settledZoom === settledZoom
|
|
144
|
+
) {
|
|
145
|
+
return previous;
|
|
146
|
+
}
|
|
147
|
+
return {
|
|
148
|
+
value: computeRenderableIds(engine, context, enabled, options),
|
|
149
|
+
graph: context.graph,
|
|
150
|
+
enabled,
|
|
151
|
+
settledZoom,
|
|
152
|
+
};
|
|
153
|
+
}
|