@multiplatform.one/node-graph 9.0.11 → 9.2.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/cjs/graphLayout/GraphDots.cjs +60 -0
- package/dist/cjs/graphLayout/GraphDots.native.js +66 -0
- package/dist/cjs/graphLayout/GraphDots.native.js.map +1 -0
- package/dist/cjs/graphLayout/GraphLayout.cjs +153 -0
- package/dist/cjs/graphLayout/GraphLayout.native.js +163 -0
- package/dist/cjs/graphLayout/GraphLayout.native.js.map +1 -0
- package/dist/cjs/graphLayout/GraphSvg.cjs +68 -0
- package/dist/cjs/graphLayout/GraphSvg.native.js +73 -0
- package/dist/cjs/graphLayout/GraphSvg.native.js.map +1 -0
- package/dist/cjs/graphLayout/GraphViewport.cjs +264 -0
- package/dist/cjs/graphLayout/GraphViewport.native.js +280 -0
- package/dist/cjs/graphLayout/GraphViewport.native.js.map +1 -0
- package/dist/cjs/graphLayout/index.cjs +51 -0
- package/dist/cjs/graphLayout/index.native.js +54 -0
- package/dist/cjs/graphLayout/index.native.js.map +1 -0
- package/dist/cjs/graphLayout/ogden.fixture.cjs +30 -0
- package/dist/cjs/graphLayout/ogden.fixture.native.js +33 -0
- package/dist/cjs/graphLayout/ogden.fixture.native.js.map +1 -0
- package/dist/cjs/graphLayout/plan.cjs +263 -0
- package/dist/cjs/graphLayout/plan.native.js +372 -0
- package/dist/cjs/graphLayout/plan.native.js.map +1 -0
- package/dist/cjs/graphLayout/viewMath.cjs +142 -0
- package/dist/cjs/graphLayout/viewMath.native.js +147 -0
- package/dist/cjs/graphLayout/viewMath.native.js.map +1 -0
- package/dist/esm/NodeGraph.native.js.map +1 -1
- package/dist/esm/bench/frameSampler.native.js.map +1 -1
- package/dist/esm/bench/generateGraph.native.js.map +1 -1
- package/dist/esm/diagnostics/GraphDiagnostics.native.js.map +1 -1
- package/dist/esm/diagnostics/LiveGraphDiagnostics.native.js.map +1 -1
- package/dist/esm/diagnostics/fromWire.native.js.map +1 -1
- package/dist/esm/diagnostics/lastError.native.js.map +1 -1
- package/dist/esm/diagnostics/types.native.js.map +1 -1
- package/dist/esm/diagnostics/useGraphDiagnostics.native.js.map +1 -1
- package/dist/esm/dom/GraphDomLayout.native.js.map +1 -1
- package/dist/esm/dom/boot.native.js.map +1 -1
- package/dist/esm/dom/wire.native.js.map +1 -1
- package/dist/esm/edges/GraphEdge.native.js.map +1 -1
- package/dist/esm/edges/edgeKinds.native.js.map +1 -1
- package/dist/esm/edges/edgePath.native.js.map +1 -1
- package/dist/esm/edges/edgeRoute.native.js.map +1 -1
- package/dist/esm/edges/edgeStyle.native.js.map +1 -1
- package/dist/esm/edges/useEdgeObstacles.native.js.map +1 -1
- package/dist/esm/graphLayout/GraphDots.mjs +35 -0
- package/dist/esm/graphLayout/GraphDots.mjs.map +1 -0
- package/dist/esm/graphLayout/GraphDots.native.js +38 -0
- package/dist/esm/graphLayout/GraphDots.native.js.map +1 -0
- package/dist/esm/graphLayout/GraphLayout.mjs +127 -0
- package/dist/esm/graphLayout/GraphLayout.mjs.map +1 -0
- package/dist/esm/graphLayout/GraphLayout.native.js +134 -0
- package/dist/esm/graphLayout/GraphLayout.native.js.map +1 -0
- package/dist/esm/graphLayout/GraphSvg.mjs +42 -0
- package/dist/esm/graphLayout/GraphSvg.mjs.map +1 -0
- package/dist/esm/graphLayout/GraphSvg.native.js +44 -0
- package/dist/esm/graphLayout/GraphSvg.native.js.map +1 -0
- package/dist/esm/graphLayout/GraphViewport.mjs +239 -0
- package/dist/esm/graphLayout/GraphViewport.mjs.map +1 -0
- package/dist/esm/graphLayout/GraphViewport.native.js +252 -0
- package/dist/esm/graphLayout/GraphViewport.native.js.map +1 -0
- package/dist/esm/graphLayout/index.mjs +7 -0
- package/dist/esm/graphLayout/index.mjs.map +1 -0
- package/dist/esm/graphLayout/index.native.js +7 -0
- package/dist/esm/graphLayout/index.native.js.map +1 -0
- package/dist/esm/graphLayout/ogden.fixture.mjs +4 -0
- package/dist/esm/graphLayout/ogden.fixture.mjs.map +1 -0
- package/dist/esm/graphLayout/ogden.fixture.native.js +4 -0
- package/dist/esm/graphLayout/ogden.fixture.native.js.map +1 -0
- package/dist/esm/graphLayout/plan.mjs +235 -0
- package/dist/esm/graphLayout/plan.mjs.map +1 -0
- package/dist/esm/graphLayout/plan.native.js +341 -0
- package/dist/esm/graphLayout/plan.native.js.map +1 -0
- package/dist/esm/graphLayout/viewMath.mjs +105 -0
- package/dist/esm/graphLayout/viewMath.mjs.map +1 -0
- package/dist/esm/graphLayout/viewMath.native.js +107 -0
- package/dist/esm/graphLayout/viewMath.native.js.map +1 -0
- package/dist/esm/index.native.js.map +1 -1
- package/dist/esm/interaction/lassoIntercept.native.js.map +1 -1
- package/dist/esm/interaction/legalTargets.native.js.map +1 -1
- package/dist/esm/interaction/snapAlignment.native.js.map +1 -1
- package/dist/esm/interaction/snapChanges.native.js.map +1 -1
- package/dist/esm/interaction/spatialHash.native.js.map +1 -1
- package/dist/esm/interaction/useLassoSelect.native.js.map +1 -1
- package/dist/esm/knobs/KnobEnvelopeProvider.native.js.map +1 -1
- package/dist/esm/knobs/envelope.native.js.map +1 -1
- package/dist/esm/knobs/graphKnobs.native.js.map +1 -1
- package/dist/esm/knobs/useKnobEnvelope.native.js.map +1 -1
- package/dist/esm/layout/dagre.native.js.map +1 -1
- package/dist/esm/layout/layout.native.js.map +1 -1
- package/dist/esm/layout/runner.native.js.map +1 -1
- package/dist/esm/layout/structuredCloneShim.native.js.map +1 -1
- package/dist/esm/layout/types.native.js.map +1 -1
- package/dist/esm/layout/worker.entry.native.js.map +1 -1
- package/dist/esm/layout/worker.generated.native.js.map +1 -1
- package/dist/esm/model/commands.native.js.map +1 -1
- package/dist/esm/model/connect.native.js.map +1 -1
- package/dist/esm/model/document.native.js.map +1 -1
- package/dist/esm/model/history.native.js.map +1 -1
- package/dist/esm/model/index.native.js.map +1 -1
- package/dist/esm/model/schema.native.js.map +1 -1
- package/dist/esm/model/select.native.js.map +1 -1
- package/dist/esm/model/serialize.native.js.map +1 -1
- package/dist/esm/native/DomOnlyStory.native.js.map +1 -1
- package/dist/esm/native/GraphToolbar.native.js.map +1 -1
- package/dist/esm/native/domEntryRegistry.native.js.map +1 -1
- package/dist/esm/native/graphChildren.native.js.map +1 -1
- package/dist/esm/nodes/GraphNode.native.js.map +1 -1
- package/dist/esm/nodes/GraphPort.native.js.map +1 -1
- package/dist/esm/nodes/handleGeometry.native.js.map +1 -1
- package/dist/esm/nodes/lod.native.js.map +1 -1
- package/dist/esm/nodes/nodeMeasure.native.js.map +1 -1
- package/dist/esm/nodes/overviewNode.native.js.map +1 -1
- package/dist/esm/react-flow/ConnectionPuck.native.js.map +1 -1
- package/dist/esm/react-flow/GraphActions.native.js.map +1 -1
- package/dist/esm/react-flow/GraphCommandBridge.native.js.map +1 -1
- package/dist/esm/react-flow/GraphNodeHost.native.js.map +1 -1
- package/dist/esm/react-flow/GraphPaintProbe.native.js.map +1 -1
- package/dist/esm/react-flow/GraphZoomVar.native.js.map +1 -1
- package/dist/esm/react-flow/LassoOverlay.native.js.map +1 -1
- package/dist/esm/react-flow/NodeGraphWeb.native.js.map +1 -1
- package/dist/esm/react-flow/SnapGuides.native.js.map +1 -1
- package/dist/esm/react-flow/demoControlBody.native.js.map +1 -1
- package/dist/esm/react-flow/demoNodeBody.native.js.map +1 -1
- package/dist/esm/react-flow/fixtures.native.js.map +1 -1
- package/dist/esm/react-flow/flowAdapter.native.js.map +1 -1
- package/dist/esm/react-flow/graphHandle.native.js.map +1 -1
- package/dist/esm/react-flow/nodeCensus.native.js.map +1 -1
- package/dist/esm/react-flow/useGraphController.native.js.map +1 -1
- package/dist/esm/react-flow/xy.native.js.map +1 -1
- package/dist/esm/react-flow/xyPaths.native.js.map +1 -1
- package/dist/esm/theme/GraphFaces.native.js.map +1 -1
- package/dist/esm/theme/GraphVarsStyle.native.js.map +1 -1
- package/dist/esm/theme/faces.generated.native.js.map +1 -1
- package/dist/esm/theme/faces.native.js.map +1 -1
- package/dist/esm/theme/graphVars.native.js.map +1 -1
- package/dist/jsx/graphLayout/GraphDots.mjs +35 -0
- package/dist/jsx/graphLayout/GraphDots.mjs.map +1 -0
- package/dist/jsx/graphLayout/GraphDots.native.js +66 -0
- package/dist/jsx/graphLayout/GraphDots.native.js.map +1 -0
- package/dist/jsx/graphLayout/GraphLayout.mjs +127 -0
- package/dist/jsx/graphLayout/GraphLayout.mjs.map +1 -0
- package/dist/jsx/graphLayout/GraphLayout.native.js +163 -0
- package/dist/jsx/graphLayout/GraphLayout.native.js.map +1 -0
- package/dist/jsx/graphLayout/GraphSvg.mjs +42 -0
- package/dist/jsx/graphLayout/GraphSvg.mjs.map +1 -0
- package/dist/jsx/graphLayout/GraphSvg.native.js +73 -0
- package/dist/jsx/graphLayout/GraphSvg.native.js.map +1 -0
- package/dist/jsx/graphLayout/GraphViewport.mjs +239 -0
- package/dist/jsx/graphLayout/GraphViewport.mjs.map +1 -0
- package/dist/jsx/graphLayout/GraphViewport.native.js +280 -0
- package/dist/jsx/graphLayout/GraphViewport.native.js.map +1 -0
- package/dist/jsx/graphLayout/index.mjs +7 -0
- package/dist/jsx/graphLayout/index.mjs.map +1 -0
- package/dist/jsx/graphLayout/index.native.js +54 -0
- package/dist/jsx/graphLayout/index.native.js.map +1 -0
- package/dist/jsx/graphLayout/ogden.fixture.mjs +4 -0
- package/dist/jsx/graphLayout/ogden.fixture.mjs.map +1 -0
- package/dist/jsx/graphLayout/ogden.fixture.native.js +33 -0
- package/dist/jsx/graphLayout/ogden.fixture.native.js.map +1 -0
- package/dist/jsx/graphLayout/plan.mjs +235 -0
- package/dist/jsx/graphLayout/plan.mjs.map +1 -0
- package/dist/jsx/graphLayout/plan.native.js +372 -0
- package/dist/jsx/graphLayout/plan.native.js.map +1 -0
- package/dist/jsx/graphLayout/viewMath.mjs +105 -0
- package/dist/jsx/graphLayout/viewMath.mjs.map +1 -0
- package/dist/jsx/graphLayout/viewMath.native.js +147 -0
- package/dist/jsx/graphLayout/viewMath.native.js.map +1 -0
- package/graph-layout.ts +1 -0
- package/package.json +21 -6
- package/src/graphLayout/GraphDots.tsx +32 -0
- package/src/graphLayout/GraphLayout.stories.tsx +86 -0
- package/src/graphLayout/GraphLayout.tsx +152 -0
- package/src/graphLayout/GraphSvg.native.tsx +31 -0
- package/src/graphLayout/GraphSvg.tsx +45 -0
- package/src/graphLayout/GraphViewport.native.tsx +231 -0
- package/src/graphLayout/GraphViewport.tsx +247 -0
- package/src/graphLayout/index.ts +31 -0
- package/src/graphLayout/ogden.fixture.ts +120 -0
- package/src/graphLayout/plan.spec.ts +202 -0
- package/src/graphLayout/plan.ts +223 -0
- package/src/graphLayout/purity.spec.ts +67 -0
- package/src/graphLayout/viewMath.spec.ts +185 -0
- package/src/graphLayout/viewMath.ts +151 -0
- package/types/graphLayout/GraphDots.d.ts +9 -0
- package/types/graphLayout/GraphDots.d.ts.map +1 -0
- package/types/graphLayout/GraphLayout.d.ts +22 -0
- package/types/graphLayout/GraphLayout.d.ts.map +1 -0
- package/types/graphLayout/GraphSvg.d.ts +17 -0
- package/types/graphLayout/GraphSvg.d.ts.map +1 -0
- package/types/graphLayout/GraphSvg.native.d.ts +5 -0
- package/types/graphLayout/GraphSvg.native.d.ts.map +1 -0
- package/types/graphLayout/GraphViewport.d.ts +11 -0
- package/types/graphLayout/GraphViewport.d.ts.map +1 -0
- package/types/graphLayout/GraphViewport.native.d.ts +4 -0
- package/types/graphLayout/GraphViewport.native.d.ts.map +1 -0
- package/types/graphLayout/index.d.ts +11 -0
- package/types/graphLayout/index.d.ts.map +1 -0
- package/types/graphLayout/ogden.fixture.d.ts +3 -0
- package/types/graphLayout/ogden.fixture.d.ts.map +1 -0
- package/types/graphLayout/plan.d.ts +52 -0
- package/types/graphLayout/plan.d.ts.map +1 -0
- package/types/graphLayout/viewMath.d.ts +47 -0
- package/types/graphLayout/viewMath.d.ts.map +1 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { samplePath } from "../edges/edgeRoute";
|
|
3
|
+
import { ogdenApps, ogdenLinks } from "./ogden.fixture";
|
|
4
|
+
import { arrowHead, planGraphLayout, usableEdges } from "./plan";
|
|
5
|
+
import type {
|
|
6
|
+
GraphDirection,
|
|
7
|
+
GraphLayoutEdge,
|
|
8
|
+
GraphLayoutNode,
|
|
9
|
+
GraphPlan,
|
|
10
|
+
PlannedNode,
|
|
11
|
+
} from "./plan";
|
|
12
|
+
|
|
13
|
+
const spacing = { nodeSpacing: 16, rankSpacing: 32, margin: 16, strokeWidth: 1.5 };
|
|
14
|
+
|
|
15
|
+
function ogden(): { nodes: GraphLayoutNode[]; edges: GraphLayoutEdge[] } {
|
|
16
|
+
return {
|
|
17
|
+
nodes: ogdenApps.map((id) => ({ id, width: 90, height: 110 })),
|
|
18
|
+
edges: ogdenLinks.map(([source, target]) => ({ source, target })),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function randomDag(
|
|
23
|
+
count: number,
|
|
24
|
+
seed: number,
|
|
25
|
+
): { nodes: GraphLayoutNode[]; edges: GraphLayoutEdge[] } {
|
|
26
|
+
let state = seed;
|
|
27
|
+
const next = () => {
|
|
28
|
+
state = (state * 1103515245 + 12345) % 2147483648;
|
|
29
|
+
return state / 2147483648;
|
|
30
|
+
};
|
|
31
|
+
const nodes = Array.from({ length: count }, (_, index) => ({
|
|
32
|
+
id: `n${index}`,
|
|
33
|
+
width: 60 + Math.floor(next() * 60),
|
|
34
|
+
height: 40 + Math.floor(next() * 40),
|
|
35
|
+
}));
|
|
36
|
+
const edges: GraphLayoutEdge[] = [];
|
|
37
|
+
for (let index = 1; index < count; index++) {
|
|
38
|
+
if (next() < 0.15) continue;
|
|
39
|
+
const links = 1 + Math.floor(next() * 2);
|
|
40
|
+
for (let link = 0; link < links; link++) {
|
|
41
|
+
edges.push({
|
|
42
|
+
source: `n${Math.floor(next() * index)}`,
|
|
43
|
+
target: `n${index}`,
|
|
44
|
+
emphasis: next() < 0.1,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return { nodes, edges };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function overlaps(a: PlannedNode, b: PlannedNode): boolean {
|
|
52
|
+
return a.x < b.x + b.width && b.x < a.x + a.width && a.y < b.y + b.height && b.y < a.y + a.height;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function onEdge(point: { x: number; y: number }, node: PlannedNode): boolean {
|
|
56
|
+
const within = (value: number, from: number, to: number) =>
|
|
57
|
+
value >= from - 0.5 && value <= to + 0.5;
|
|
58
|
+
const onX = within(point.x, node.x, node.x + node.width);
|
|
59
|
+
const onY = within(point.y, node.y, node.y + node.height);
|
|
60
|
+
const nearSide =
|
|
61
|
+
Math.abs(point.x - node.x) <= 0.5 ||
|
|
62
|
+
Math.abs(point.x - node.x - node.width) <= 0.5 ||
|
|
63
|
+
Math.abs(point.y - node.y) <= 0.5 ||
|
|
64
|
+
Math.abs(point.y - node.y - node.height) <= 0.5;
|
|
65
|
+
return onX && onY && nearSide;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function strictlyInside(point: { x: number; y: number }, node: PlannedNode): boolean {
|
|
69
|
+
return (
|
|
70
|
+
point.x > node.x + 0.5 &&
|
|
71
|
+
point.x < node.x + node.width - 0.5 &&
|
|
72
|
+
point.y > node.y + 0.5 &&
|
|
73
|
+
point.y < node.y + node.height - 0.5
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function audit(plan: GraphPlan, direction: GraphDirection) {
|
|
78
|
+
const byId = new Map(plan.nodes.map((node) => [node.id, node]));
|
|
79
|
+
let overlapCount = 0;
|
|
80
|
+
for (let a = 0; a < plan.nodes.length; a++) {
|
|
81
|
+
for (let b = a + 1; b < plan.nodes.length; b++)
|
|
82
|
+
if (overlaps(plan.nodes[a]!, plan.nodes[b]!)) overlapCount++;
|
|
83
|
+
}
|
|
84
|
+
let offEdge = 0;
|
|
85
|
+
let headsUnder = 0;
|
|
86
|
+
let inversions = 0;
|
|
87
|
+
for (const edge of plan.edges) {
|
|
88
|
+
const source = byId.get(edge.source)!;
|
|
89
|
+
const target = byId.get(edge.target)!;
|
|
90
|
+
const points = samplePath(edge.d);
|
|
91
|
+
if (
|
|
92
|
+
!onEdge(points[0]!, source) ||
|
|
93
|
+
!onEdge(points[points.length - 1]!, target) ||
|
|
94
|
+
!onEdge(edge.tip, target)
|
|
95
|
+
)
|
|
96
|
+
offEdge++;
|
|
97
|
+
const head = samplePath(edge.head);
|
|
98
|
+
if (plan.nodes.some((node) => head.some((point) => strictlyInside(point, node)))) headsUnder++;
|
|
99
|
+
if (
|
|
100
|
+
direction === "LR" ? source.x + source.width > target.x : source.y + source.height > target.y
|
|
101
|
+
)
|
|
102
|
+
inversions++;
|
|
103
|
+
}
|
|
104
|
+
return { overlapCount, offEdge, headsUnder, inversions };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
describe("planGraphLayout (FZ-8149, LC-294)", () => {
|
|
108
|
+
for (const direction of ["LR", "TB"] as const) {
|
|
109
|
+
it(`lays Ogden corp's 57 apps and 59 links out ${direction} with no overlap, inversion or stray head`, () => {
|
|
110
|
+
const { nodes, edges } = ogden();
|
|
111
|
+
const plan = planGraphLayout({ nodes, edges, direction, ...spacing, wrapWidth: 390 });
|
|
112
|
+
expect(plan.nodes).toHaveLength(57);
|
|
113
|
+
expect(plan.edges).toHaveLength(usableEdges(nodes, edges).length);
|
|
114
|
+
expect(audit(plan, direction)).toEqual({
|
|
115
|
+
overlapCount: 0,
|
|
116
|
+
offEdge: 0,
|
|
117
|
+
headsUnder: 0,
|
|
118
|
+
inversions: 0,
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it(`holds the same on a 120-node DAG ${direction}`, () => {
|
|
123
|
+
const { nodes, edges } = randomDag(120, 7);
|
|
124
|
+
const plan = planGraphLayout({ nodes, edges, direction, ...spacing });
|
|
125
|
+
expect(plan.edges.length).toBeGreaterThan(100);
|
|
126
|
+
expect(audit(plan, direction)).toEqual({
|
|
127
|
+
overlapCount: 0,
|
|
128
|
+
offEdge: 0,
|
|
129
|
+
headsUnder: 0,
|
|
130
|
+
inversions: 0,
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
it("packs isolated nodes in rows under the graph, wrapping at the wider of the graph and wrapWidth", () => {
|
|
136
|
+
const { nodes, edges } = ogden();
|
|
137
|
+
const plan = planGraphLayout({ nodes, edges, direction: "LR", ...spacing, wrapWidth: 390 });
|
|
138
|
+
const linked = new Set(usableEdges(nodes, edges).flatMap((edge) => [edge.source, edge.target]));
|
|
139
|
+
const graph = plan.nodes.filter((node) => linked.has(node.id));
|
|
140
|
+
const loose = plan.nodes.filter((node) => !linked.has(node.id));
|
|
141
|
+
expect(loose.length).toBeGreaterThan(0);
|
|
142
|
+
const graphBottom = Math.max(...graph.map((node) => node.y + node.height));
|
|
143
|
+
const graphRight = Math.max(...graph.map((node) => node.x + node.width));
|
|
144
|
+
for (const node of loose) {
|
|
145
|
+
expect(node.y).toBeGreaterThanOrEqual(graphBottom);
|
|
146
|
+
expect(node.x + node.width).toBeLessThanOrEqual(
|
|
147
|
+
Math.max(graphRight, 390 + spacing.margin) + 0.5,
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("drops self-loops, unknown ends and repeats, and keeps a host's own positions", () => {
|
|
153
|
+
const nodes = [
|
|
154
|
+
{ id: "a", width: 40, height: 20 },
|
|
155
|
+
{ id: "b", width: 40, height: 20 },
|
|
156
|
+
];
|
|
157
|
+
const edges = [
|
|
158
|
+
{ source: "a", target: "a" },
|
|
159
|
+
{ source: "a", target: "zz" },
|
|
160
|
+
{ source: "a", target: "b" },
|
|
161
|
+
{ source: "a", target: "b" },
|
|
162
|
+
];
|
|
163
|
+
const plan = planGraphLayout({
|
|
164
|
+
nodes,
|
|
165
|
+
edges,
|
|
166
|
+
direction: "LR",
|
|
167
|
+
...spacing,
|
|
168
|
+
positions: { a: { x: 0, y: 0 }, b: { x: 200, y: 50 } },
|
|
169
|
+
});
|
|
170
|
+
expect(plan.edges.map((edge) => edge.id)).toEqual(["a\nb"]);
|
|
171
|
+
expect(plan.nodes.map(({ id, x, y }) => ({ id, x, y }))).toEqual([
|
|
172
|
+
{ id: "a", x: 16, y: 16 },
|
|
173
|
+
{ id: "b", x: 216, y: 66 },
|
|
174
|
+
]);
|
|
175
|
+
expect(plan).toMatchObject({ width: 272, height: 102 });
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("paints emphasis in the theme accent and a kind in its token, never a literal", () => {
|
|
179
|
+
const nodes = [
|
|
180
|
+
{ id: "a", width: 40, height: 20 },
|
|
181
|
+
{ id: "b", width: 40, height: 20 },
|
|
182
|
+
{ id: "c", width: 40, height: 20 },
|
|
183
|
+
];
|
|
184
|
+
const plan = planGraphLayout({
|
|
185
|
+
nodes,
|
|
186
|
+
edges: [
|
|
187
|
+
{ source: "a", target: "b", emphasis: true },
|
|
188
|
+
{ source: "a", target: "c", kind: "error" },
|
|
189
|
+
],
|
|
190
|
+
direction: "LR",
|
|
191
|
+
...spacing,
|
|
192
|
+
});
|
|
193
|
+
expect(plan.edges.map((edge) => edge.stroke)).toEqual(["$accentColor", "$red9"]);
|
|
194
|
+
expect(plan.edges[1]!.dash).toBe("6 4");
|
|
195
|
+
for (const edge of plan.edges) expect(edge.stroke.startsWith("$")).toBe(true);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it("draws the head as a closed triangle whose tip is the end point, four strokes long", () => {
|
|
199
|
+
expect(arrowHead({ x: 100, y: 50 }, "left" as never, 2)).toBe("M 100 50 L 92 53 L 92 47 Z");
|
|
200
|
+
expect(arrowHead({ x: 100, y: 50 }, "top" as never, 2)).toBe("M 100 50 L 97 42 L 103 42 Z");
|
|
201
|
+
});
|
|
202
|
+
});
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import type { EdgeObstacle } from "../edges/edgeRoute";
|
|
2
|
+
import { edgeClearance } from "../edges/edgeRoute";
|
|
3
|
+
import { buildEdgePath } from "../edges/edgePath";
|
|
4
|
+
import type { PathGeometry } from "../edges/edgePath";
|
|
5
|
+
import { resolveEdgeStyle } from "../edges/edgeStyle";
|
|
6
|
+
import { layoutWithDagre } from "../layout/dagre";
|
|
7
|
+
import type { XY } from "../model/document";
|
|
8
|
+
import { Position } from "../react-flow/xyPaths";
|
|
9
|
+
|
|
10
|
+
export type GraphDirection = "LR" | "TB";
|
|
11
|
+
|
|
12
|
+
export interface GraphLayoutNode {
|
|
13
|
+
readonly id: string;
|
|
14
|
+
readonly width: number;
|
|
15
|
+
readonly height: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface GraphLayoutEdge {
|
|
19
|
+
readonly id?: string;
|
|
20
|
+
readonly source: string;
|
|
21
|
+
readonly target: string;
|
|
22
|
+
readonly kind?: string;
|
|
23
|
+
readonly emphasis?: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface GraphPlanInput {
|
|
27
|
+
readonly nodes: readonly GraphLayoutNode[];
|
|
28
|
+
readonly edges: readonly GraphLayoutEdge[];
|
|
29
|
+
readonly direction: GraphDirection;
|
|
30
|
+
readonly nodeSpacing: number;
|
|
31
|
+
readonly rankSpacing: number;
|
|
32
|
+
readonly margin: number;
|
|
33
|
+
readonly strokeWidth: number;
|
|
34
|
+
readonly wrapWidth?: number;
|
|
35
|
+
readonly positions?: Readonly<Record<string, XY>>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface PlannedNode extends GraphLayoutNode {
|
|
39
|
+
readonly x: number;
|
|
40
|
+
readonly y: number;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface PlannedEdge {
|
|
44
|
+
readonly id: string;
|
|
45
|
+
readonly source: string;
|
|
46
|
+
readonly target: string;
|
|
47
|
+
readonly d: string;
|
|
48
|
+
readonly head: string;
|
|
49
|
+
readonly tip: XY;
|
|
50
|
+
readonly stroke: string;
|
|
51
|
+
readonly dash?: string;
|
|
52
|
+
readonly emphasis: boolean;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface GraphPlan {
|
|
56
|
+
readonly width: number;
|
|
57
|
+
readonly height: number;
|
|
58
|
+
readonly nodes: readonly PlannedNode[];
|
|
59
|
+
readonly edges: readonly PlannedEdge[];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const sides: Record<GraphDirection, { readonly out: Position; readonly in: Position }> = {
|
|
63
|
+
LR: { out: Position.Right, in: Position.Left },
|
|
64
|
+
TB: { out: Position.Bottom, in: Position.Top },
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const inward: Record<Position, XY> = {
|
|
68
|
+
[Position.Left]: { x: 1, y: 0 },
|
|
69
|
+
[Position.Right]: { x: -1, y: 0 },
|
|
70
|
+
[Position.Top]: { x: 0, y: 1 },
|
|
71
|
+
[Position.Bottom]: { x: 0, y: -1 },
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export function graphEdgeId(edge: GraphLayoutEdge): string {
|
|
75
|
+
return edge.id ?? `${edge.source}\n${edge.target}`;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function usableEdges(
|
|
79
|
+
nodes: readonly GraphLayoutNode[],
|
|
80
|
+
edges: readonly GraphLayoutEdge[],
|
|
81
|
+
): GraphLayoutEdge[] {
|
|
82
|
+
const known = new Set(nodes.map((node) => node.id));
|
|
83
|
+
const seen = new Set<string>();
|
|
84
|
+
const kept: GraphLayoutEdge[] = [];
|
|
85
|
+
for (const edge of edges) {
|
|
86
|
+
if (edge.source === edge.target || !known.has(edge.source) || !known.has(edge.target)) continue;
|
|
87
|
+
const id = graphEdgeId(edge);
|
|
88
|
+
if (seen.has(id)) continue;
|
|
89
|
+
seen.add(id);
|
|
90
|
+
kept.push(edge);
|
|
91
|
+
}
|
|
92
|
+
return kept;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function placeNodes(input: GraphPlanInput, edges: readonly GraphLayoutEdge[]): Map<string, XY> {
|
|
96
|
+
const placed = new Map<string, XY>();
|
|
97
|
+
if (input.positions) {
|
|
98
|
+
for (const node of input.nodes) placed.set(node.id, input.positions[node.id] ?? { x: 0, y: 0 });
|
|
99
|
+
return placed;
|
|
100
|
+
}
|
|
101
|
+
const linked = new Set(edges.flatMap((edge) => [edge.source, edge.target]));
|
|
102
|
+
const graphNodes = input.nodes.filter((node) => linked.has(node.id));
|
|
103
|
+
const isolated = input.nodes.filter((node) => !linked.has(node.id));
|
|
104
|
+
let dagWidth = 0;
|
|
105
|
+
let dagHeight = 0;
|
|
106
|
+
if (graphNodes.length > 0) {
|
|
107
|
+
const result = layoutWithDagre({
|
|
108
|
+
nodes: graphNodes.map((node) => ({ ...node, position: { x: 0, y: 0 } })),
|
|
109
|
+
edges: edges.map((edge) => ({
|
|
110
|
+
id: graphEdgeId(edge),
|
|
111
|
+
source: edge.source,
|
|
112
|
+
target: edge.target,
|
|
113
|
+
})),
|
|
114
|
+
options: {
|
|
115
|
+
direction: input.direction,
|
|
116
|
+
nodeSpacing: input.nodeSpacing,
|
|
117
|
+
rankSpacing: input.rankSpacing,
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
for (const node of graphNodes) {
|
|
121
|
+
const at = result.positions[node.id] ?? { x: 0, y: 0 };
|
|
122
|
+
placed.set(node.id, at);
|
|
123
|
+
dagWidth = Math.max(dagWidth, at.x + node.width);
|
|
124
|
+
dagHeight = Math.max(dagHeight, at.y + node.height);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
const wrap = Math.max(dagWidth, input.wrapWidth ?? 0, ...isolated.map((node) => node.width));
|
|
128
|
+
let x = 0;
|
|
129
|
+
let y = graphNodes.length > 0 ? dagHeight + input.rankSpacing : 0;
|
|
130
|
+
let rowHeight = 0;
|
|
131
|
+
for (const node of isolated) {
|
|
132
|
+
if (x > 0 && x + node.width > wrap) {
|
|
133
|
+
x = 0;
|
|
134
|
+
y += rowHeight + input.nodeSpacing;
|
|
135
|
+
rowHeight = 0;
|
|
136
|
+
}
|
|
137
|
+
placed.set(node.id, { x, y });
|
|
138
|
+
x += node.width + input.nodeSpacing;
|
|
139
|
+
rowHeight = Math.max(rowHeight, node.height);
|
|
140
|
+
}
|
|
141
|
+
return placed;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function anchor(node: PlannedNode, side: Position): XY {
|
|
145
|
+
switch (side) {
|
|
146
|
+
case Position.Left:
|
|
147
|
+
return { x: node.x, y: node.y + node.height / 2 };
|
|
148
|
+
case Position.Right:
|
|
149
|
+
return { x: node.x + node.width, y: node.y + node.height / 2 };
|
|
150
|
+
case Position.Top:
|
|
151
|
+
return { x: node.x + node.width / 2, y: node.y };
|
|
152
|
+
default:
|
|
153
|
+
return { x: node.x + node.width / 2, y: node.y + node.height };
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function round(value: number): number {
|
|
158
|
+
return Math.round(value * 100) / 100;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function arrowHead(tip: XY, side: Position, strokeWidth: number): string {
|
|
162
|
+
const into = inward[side];
|
|
163
|
+
const length = strokeWidth * 4;
|
|
164
|
+
const half = (strokeWidth * 3) / 2;
|
|
165
|
+
const base = { x: tip.x - into.x * length, y: tip.y - into.y * length };
|
|
166
|
+
const across = { x: -into.y, y: into.x };
|
|
167
|
+
const a = { x: base.x + across.x * half, y: base.y + across.y * half };
|
|
168
|
+
const b = { x: base.x - across.x * half, y: base.y - across.y * half };
|
|
169
|
+
return `M ${round(tip.x)} ${round(tip.y)} L ${round(a.x)} ${round(a.y)} L ${round(b.x)} ${round(b.y)} Z`;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export function planGraphLayout(input: GraphPlanInput): GraphPlan {
|
|
173
|
+
const edges = usableEdges(input.nodes, input.edges);
|
|
174
|
+
const placed = placeNodes(input, edges);
|
|
175
|
+
const { margin } = input;
|
|
176
|
+
const nodes: PlannedNode[] = input.nodes.map((node) => {
|
|
177
|
+
const at = placed.get(node.id) ?? { x: 0, y: 0 };
|
|
178
|
+
return { ...node, x: round(at.x + margin), y: round(at.y + margin) };
|
|
179
|
+
});
|
|
180
|
+
const byId = new Map(nodes.map((node) => [node.id, node]));
|
|
181
|
+
const boxes: EdgeObstacle[] = nodes.map((node) => ({
|
|
182
|
+
id: node.id,
|
|
183
|
+
x: node.x,
|
|
184
|
+
y: node.y,
|
|
185
|
+
width: node.width,
|
|
186
|
+
height: node.height,
|
|
187
|
+
}));
|
|
188
|
+
const side = sides[input.direction];
|
|
189
|
+
const planned: PlannedEdge[] = edges.map((edge) => {
|
|
190
|
+
const source = byId.get(edge.source)!;
|
|
191
|
+
const target = byId.get(edge.target)!;
|
|
192
|
+
const from = anchor(source, side.out);
|
|
193
|
+
const tip = anchor(target, side.in);
|
|
194
|
+
const style = resolveEdgeStyle({
|
|
195
|
+
kind: edge.kind ?? "flow",
|
|
196
|
+
borderWidth: "small",
|
|
197
|
+
animation: "none",
|
|
198
|
+
});
|
|
199
|
+
const geometry: PathGeometry = {
|
|
200
|
+
sourceX: from.x,
|
|
201
|
+
sourceY: from.y,
|
|
202
|
+
targetX: tip.x,
|
|
203
|
+
targetY: tip.y,
|
|
204
|
+
sourcePosition: side.out,
|
|
205
|
+
targetPosition: side.in,
|
|
206
|
+
};
|
|
207
|
+
const [d] = buildEdgePath(style.path, geometry, { boxes, clearance: edgeClearance });
|
|
208
|
+
return {
|
|
209
|
+
id: graphEdgeId(edge),
|
|
210
|
+
source: edge.source,
|
|
211
|
+
target: edge.target,
|
|
212
|
+
d,
|
|
213
|
+
head: arrowHead(tip, side.in, input.strokeWidth * (edge.emphasis ? 2 : 1)),
|
|
214
|
+
tip,
|
|
215
|
+
stroke: edge.emphasis ? "$accentColor" : style.stroke,
|
|
216
|
+
...(style.strokeDasharray ? { dash: style.strokeDasharray } : {}),
|
|
217
|
+
emphasis: edge.emphasis ?? false,
|
|
218
|
+
};
|
|
219
|
+
});
|
|
220
|
+
const width = nodes.reduce((most, node) => Math.max(most, node.x + node.width), 0) + margin;
|
|
221
|
+
const height = nodes.reduce((most, node) => Math.max(most, node.y + node.height), 0) + margin;
|
|
222
|
+
return { width: round(width), height: round(height), nodes, edges: planned };
|
|
223
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { describe, expect, it } from "vitest";
|
|
5
|
+
|
|
6
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
7
|
+
const src = path.resolve(here, "..");
|
|
8
|
+
const candidates = [".ts", ".tsx", ".native.ts", ".native.tsx", "/index.ts", "/index.native.ts"];
|
|
9
|
+
|
|
10
|
+
function resolveLocal(from: string, specifier: string): string[] {
|
|
11
|
+
const base = path.resolve(path.dirname(from), specifier);
|
|
12
|
+
return candidates.map((suffix) => base + suffix).filter((file) => existsSync(file));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function walk(): { files: Set<string>; packages: Set<string> } {
|
|
16
|
+
const files = new Set<string>();
|
|
17
|
+
const packages = new Set<string>();
|
|
18
|
+
const queue = readdirSync(here)
|
|
19
|
+
.filter(
|
|
20
|
+
(name) => /\.tsx?$/.test(name) && !name.includes(".spec.") && !name.includes(".fixture."),
|
|
21
|
+
)
|
|
22
|
+
.map((name) => path.join(here, name));
|
|
23
|
+
while (queue.length > 0) {
|
|
24
|
+
const file = queue.pop()!;
|
|
25
|
+
if (files.has(file)) continue;
|
|
26
|
+
files.add(file);
|
|
27
|
+
const text = readFileSync(file, "utf8");
|
|
28
|
+
for (const match of text.matchAll(/(?:from|import)\s*\(?\s*"([^"]+)"/g)) {
|
|
29
|
+
const specifier = match[1]!;
|
|
30
|
+
if (specifier.startsWith(".")) queue.push(...resolveLocal(file, specifier));
|
|
31
|
+
else packages.add(specifier);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return { files, packages };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
describe("the graph-layout entry's import graph (FZ-8149, LC-294)", () => {
|
|
38
|
+
const { files, packages } = walk();
|
|
39
|
+
const relative = [...files].map((file) => path.relative(src, file)).sort();
|
|
40
|
+
|
|
41
|
+
it("never reaches React Flow's React runtime, its store or the webview", () => {
|
|
42
|
+
expect(
|
|
43
|
+
[...packages].filter(
|
|
44
|
+
(name) => name === "@xyflow/react" || name.startsWith("react-native-webview"),
|
|
45
|
+
),
|
|
46
|
+
).toEqual([]);
|
|
47
|
+
expect(
|
|
48
|
+
relative.filter(
|
|
49
|
+
(file) =>
|
|
50
|
+
file === "react-flow/xy.ts" ||
|
|
51
|
+
file.startsWith("dom/") ||
|
|
52
|
+
file.includes("useEdgeObstacles"),
|
|
53
|
+
),
|
|
54
|
+
).toEqual([]);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("draws through NodeGraph's own layout engine and edge path, so there is one of each", () => {
|
|
58
|
+
for (const shared of [
|
|
59
|
+
"layout/dagre.ts",
|
|
60
|
+
"edges/edgePath.ts",
|
|
61
|
+
"edges/edgeRoute.ts",
|
|
62
|
+
"edges/edgeStyle.ts",
|
|
63
|
+
]) {
|
|
64
|
+
expect(relative).toContain(shared);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
});
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
clampOffset,
|
|
4
|
+
clampView,
|
|
5
|
+
dotPhase,
|
|
6
|
+
dotPitch,
|
|
7
|
+
graphZoom,
|
|
8
|
+
keyedView,
|
|
9
|
+
leastScale,
|
|
10
|
+
toggledView,
|
|
11
|
+
zoomAt,
|
|
12
|
+
dotArea,
|
|
13
|
+
dotPath,
|
|
14
|
+
} from "./viewMath";
|
|
15
|
+
|
|
16
|
+
const viewport = { width: 390, height: 700 };
|
|
17
|
+
const tall = { width: 830, height: 4300 };
|
|
18
|
+
|
|
19
|
+
describe("the graph's zoom (FZ-8449)", () => {
|
|
20
|
+
it("zooms out no further than the whole graph on screen, and never in past 1:1", () => {
|
|
21
|
+
const least = leastScale(tall, viewport);
|
|
22
|
+
expect(least).toBeCloseTo(Math.min(390 / 830, 700 / 4300));
|
|
23
|
+
expect(tall.width * least).toBeLessThanOrEqual(viewport.width + 1e-6);
|
|
24
|
+
expect(tall.height * least).toBeLessThanOrEqual(viewport.height + 1e-6);
|
|
25
|
+
expect(leastScale({ width: 300, height: 600 }, viewport)).toBe(1);
|
|
26
|
+
expect(leastScale({ width: 400, height: 100_000 }, viewport)).toBe(graphZoom.floor);
|
|
27
|
+
const view = { scale: 1, x: 0, y: 0 };
|
|
28
|
+
expect(zoomAt(view, 0.01, { x: 0, y: 0 }, tall, viewport).scale).toBeCloseTo(least);
|
|
29
|
+
expect(zoomAt(view, 3, { x: 0, y: 0 }, tall, viewport).scale).toBe(1);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("keeps the point under the fingers where it is", () => {
|
|
33
|
+
const view = { scale: 1, x: -200, y: -1500 };
|
|
34
|
+
const focal = { x: 180, y: 320 };
|
|
35
|
+
const next = zoomAt(view, 0.5, focal, tall, viewport);
|
|
36
|
+
const before = { x: (focal.x - view.x) / view.scale, y: (focal.y - view.y) / view.scale };
|
|
37
|
+
const after = { x: (focal.x - next.x) / next.scale, y: (focal.y - next.y) / next.scale };
|
|
38
|
+
expect(after.x).toBeCloseTo(before.x);
|
|
39
|
+
expect(after.y).toBeCloseTo(before.y);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it.each([1, 0.6, 0.3, 0.17])(
|
|
43
|
+
"stops a pan at the outermost apps at scale %s, never past them into empty space",
|
|
44
|
+
(scale) => {
|
|
45
|
+
for (const [x, y] of [
|
|
46
|
+
[10_000, 10_000],
|
|
47
|
+
[-10_000, -10_000],
|
|
48
|
+
[-123, -456],
|
|
49
|
+
]) {
|
|
50
|
+
const view = clampView({ scale, x, y }, tall, viewport);
|
|
51
|
+
const wide = tall.width * view.scale;
|
|
52
|
+
const high = tall.height * view.scale;
|
|
53
|
+
if (wide > viewport.width) {
|
|
54
|
+
expect(view.x).toBeLessThanOrEqual(0);
|
|
55
|
+
expect(view.x + wide).toBeGreaterThanOrEqual(viewport.width);
|
|
56
|
+
} else expect(view.x).toBeCloseTo((viewport.width - wide) / 2);
|
|
57
|
+
if (high > viewport.height) {
|
|
58
|
+
expect(view.y).toBeLessThanOrEqual(0);
|
|
59
|
+
expect(view.y + high).toBeGreaterThanOrEqual(viewport.height);
|
|
60
|
+
} else expect(view.y).toBeCloseTo((viewport.height - high) / 2);
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
it("centres a graph smaller than the screen on that axis", () => {
|
|
66
|
+
expect(clampOffset(-50, 200, 390)).toBe(95);
|
|
67
|
+
expect(clampOffset(50, 800, 390)).toBe(0);
|
|
68
|
+
expect(clampOffset(-900, 800, 390)).toBe(-410);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("double-tap from 1:1 shows the whole graph, and from anywhere zoomed out comes back to 1:1 at the tap", () => {
|
|
72
|
+
const out = toggledView({ scale: 1, x: 0, y: -900 }, { x: 100, y: 100 }, tall, viewport);
|
|
73
|
+
expect(out.scale).toBeCloseTo(leastScale(tall, viewport));
|
|
74
|
+
const tap = { x: 120, y: 400 };
|
|
75
|
+
const back = toggledView({ scale: 0.4, x: 10, y: -300 }, tap, tall, viewport);
|
|
76
|
+
expect(back.scale).toBe(1);
|
|
77
|
+
const under = { x: (tap.x - 10) / 0.4, y: (tap.y + 300) / 0.4 };
|
|
78
|
+
expect(back.x).toBeCloseTo(Math.min(0, Math.max(viewport.width - tall.width, tap.x - under.x)));
|
|
79
|
+
expect(back.y).toBeCloseTo(
|
|
80
|
+
Math.min(0, Math.max(viewport.height - tall.height, tap.y - under.y)),
|
|
81
|
+
);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("zooms with + and - on a keyboard and comes back to 1:1 on 0", () => {
|
|
85
|
+
const start = { scale: 1, x: 0, y: 0 };
|
|
86
|
+
const out = keyedView("-", start, tall, viewport)!;
|
|
87
|
+
expect(out.scale).toBeCloseTo(1 / graphZoom.step);
|
|
88
|
+
expect(keyedView("+", out, tall, viewport)!.scale).toBeCloseTo(1);
|
|
89
|
+
expect(keyedView("+", start, tall, viewport)!.scale).toBe(1);
|
|
90
|
+
expect(keyedView("0", { scale: 0.3, x: 0, y: 0 }, tall, viewport)!.scale).toBe(1);
|
|
91
|
+
expect(keyedView("a", start, tall, viewport)).toBeUndefined();
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("thins the dot grid as it zooms out, so dots stay apart on screen", () => {
|
|
95
|
+
const pitch = 22;
|
|
96
|
+
for (const scale of [1, 0.8, 0.5, 0.3, 0.17, 0.1]) {
|
|
97
|
+
const spaced = dotPitch(pitch, scale);
|
|
98
|
+
expect(spaced * scale).toBeGreaterThanOrEqual(pitch / 2);
|
|
99
|
+
expect(spaced * scale).toBeLessThanOrEqual(pitch);
|
|
100
|
+
expect(Math.log2(spaced / pitch) % 1).toBe(0);
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("puts the first dot on the canvas's own grid, one step at most before the screen's edge", () => {
|
|
105
|
+
const pitch = 22;
|
|
106
|
+
for (const [offset, scale] of [
|
|
107
|
+
[0, 1],
|
|
108
|
+
[-345.5, 1],
|
|
109
|
+
[-1200, 0.4],
|
|
110
|
+
[37, 0.2],
|
|
111
|
+
]) {
|
|
112
|
+
const spaced = dotPitch(pitch, scale);
|
|
113
|
+
const phase = dotPhase(offset, scale, pitch, spaced);
|
|
114
|
+
expect(phase).toBeLessThan(1e-9);
|
|
115
|
+
expect(phase).toBeGreaterThanOrEqual(-spaced * scale - 1e-9);
|
|
116
|
+
const content = (phase - offset) / scale - pitch / 2;
|
|
117
|
+
expect(Math.abs(content / spaced - Math.round(content / spaced))).toBeLessThan(1e-6);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
const canvas = { width: 900, height: 5000 };
|
|
123
|
+
const pitch = 22;
|
|
124
|
+
const chunk = pitch * 4;
|
|
125
|
+
|
|
126
|
+
const dots = (path: string) =>
|
|
127
|
+
[...path.matchAll(/M([-\d.]+) ([-\d.]+)h0\.01/g)].map((match) => ({
|
|
128
|
+
x: Number(match[1]),
|
|
129
|
+
y: Number(match[2]),
|
|
130
|
+
}));
|
|
131
|
+
|
|
132
|
+
describe("the graph's dot grid (FZ-8138)", () => {
|
|
133
|
+
it.each([
|
|
134
|
+
{ x: 0, y: 0 },
|
|
135
|
+
{ x: 130, y: 2400 },
|
|
136
|
+
{ x: canvas.width - viewport.width, y: canvas.height - viewport.height },
|
|
137
|
+
])("covers the screen and a margin round it wherever the canvas is panned to (%o)", (offset) => {
|
|
138
|
+
const area = dotArea(offset, viewport, canvas, chunk);
|
|
139
|
+
expect(area.x).toBeLessThanOrEqual(Math.max(0, offset.x - chunk / 2));
|
|
140
|
+
expect(area.y).toBeLessThanOrEqual(Math.max(0, offset.y - chunk / 2));
|
|
141
|
+
expect(area.x + area.width).toBeGreaterThanOrEqual(
|
|
142
|
+
Math.min(canvas.width, offset.x + viewport.width + chunk),
|
|
143
|
+
);
|
|
144
|
+
expect(area.y + area.height).toBeGreaterThanOrEqual(
|
|
145
|
+
Math.min(canvas.height, offset.y + viewport.height + chunk),
|
|
146
|
+
);
|
|
147
|
+
expect(area.x + area.width).toBeLessThanOrEqual(canvas.width);
|
|
148
|
+
expect(area.y + area.height).toBeLessThanOrEqual(canvas.height);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("keeps the same area for small pans, so a scroll does not redraw it every frame", () => {
|
|
152
|
+
const first = dotArea({ x: 0, y: 1000 }, viewport, canvas, chunk);
|
|
153
|
+
const areas = new Set<string>();
|
|
154
|
+
for (let y = 1000; y < 1000 + chunk / 2; y += 3)
|
|
155
|
+
areas.add(JSON.stringify(dotArea({ x: 0, y }, viewport, canvas, chunk)));
|
|
156
|
+
expect(areas.size).toBeLessThanOrEqual(2);
|
|
157
|
+
expect(areas.has(JSON.stringify(first))).toBe(true);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("puts every dot on the canvas's own grid, so dots line up as the area moves", () => {
|
|
161
|
+
for (const offset of [
|
|
162
|
+
{ x: 0, y: 0 },
|
|
163
|
+
{ x: 200, y: 1300 },
|
|
164
|
+
]) {
|
|
165
|
+
const area = dotArea(offset, viewport, canvas, chunk);
|
|
166
|
+
const placed = dots(dotPath(area, pitch));
|
|
167
|
+
expect(placed.length).toBeGreaterThan(100);
|
|
168
|
+
for (const dot of placed) {
|
|
169
|
+
const x = area.x + dot.x - pitch / 2;
|
|
170
|
+
const y = area.y + dot.y - pitch / 2;
|
|
171
|
+
expect(Math.abs(x / pitch - Math.round(x / pitch))).toBeLessThan(1e-6);
|
|
172
|
+
expect(Math.abs(y / pitch - Math.round(y / pitch))).toBeLessThan(1e-6);
|
|
173
|
+
expect(dot.x).toBeGreaterThanOrEqual(0);
|
|
174
|
+
expect(dot.y).toBeGreaterThanOrEqual(0);
|
|
175
|
+
expect(dot.x).toBeLessThan(area.width);
|
|
176
|
+
expect(dot.y).toBeLessThan(area.height);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("draws dots only, no lines", () => {
|
|
182
|
+
const path = dotPath({ x: 0, y: 0, width: 200, height: 200 }, pitch);
|
|
183
|
+
expect(path.replace(/M[-\d.]+ [-\d.]+h0\.01/g, "")).toBe("");
|
|
184
|
+
});
|
|
185
|
+
});
|