@multiplatform.one/node-graph 8.2.2 → 8.3.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/edges/GraphEdge.cjs +21 -50
- package/dist/cjs/edges/GraphEdge.native.js +23 -50
- package/dist/cjs/edges/GraphEdge.native.js.map +1 -1
- package/dist/cjs/edges/edgePath.cjs +94 -0
- package/dist/cjs/edges/edgePath.native.js +98 -0
- package/dist/cjs/edges/edgePath.native.js.map +1 -0
- package/dist/cjs/edges/edgeRoute.cjs +886 -0
- package/dist/cjs/edges/edgeRoute.native.js +1399 -0
- package/dist/cjs/edges/edgeRoute.native.js.map +1 -0
- package/dist/cjs/edges/useEdgeObstacles.cjs +78 -0
- package/dist/cjs/edges/useEdgeObstacles.native.js +110 -0
- package/dist/cjs/edges/useEdgeObstacles.native.js.map +1 -0
- package/dist/cjs/react-flow/xy.cjs +4 -3
- package/dist/cjs/react-flow/xy.native.js +4 -3
- package/dist/cjs/react-flow/xy.native.js.map +1 -1
- package/dist/cjs/react-flow/xyPaths.cjs +31 -0
- package/dist/cjs/react-flow/xyPaths.native.js +34 -0
- package/dist/cjs/react-flow/xyPaths.native.js.map +1 -0
- package/dist/esm/edges/GraphEdge.mjs +23 -52
- package/dist/esm/edges/GraphEdge.mjs.map +1 -1
- package/dist/esm/edges/GraphEdge.native.js +25 -52
- package/dist/esm/edges/GraphEdge.native.js.map +1 -1
- package/dist/esm/edges/edgePath.mjs +68 -0
- package/dist/esm/edges/edgePath.mjs.map +1 -0
- package/dist/esm/edges/edgePath.native.js +69 -0
- package/dist/esm/edges/edgePath.native.js.map +1 -0
- package/dist/esm/edges/edgeRoute.mjs +857 -0
- package/dist/esm/edges/edgeRoute.mjs.map +1 -0
- package/dist/esm/edges/edgeRoute.native.js +1367 -0
- package/dist/esm/edges/edgeRoute.native.js.map +1 -0
- package/dist/esm/edges/useEdgeObstacles.mjs +52 -0
- package/dist/esm/edges/useEdgeObstacles.mjs.map +1 -0
- package/dist/esm/edges/useEdgeObstacles.native.js +81 -0
- package/dist/esm/edges/useEdgeObstacles.native.js.map +1 -0
- package/dist/esm/react-flow/xy.mjs +2 -1
- package/dist/esm/react-flow/xy.mjs.map +1 -1
- package/dist/esm/react-flow/xy.native.js +2 -1
- package/dist/esm/react-flow/xy.native.js.map +1 -1
- package/dist/esm/react-flow/xyPaths.mjs +3 -0
- package/dist/esm/react-flow/xyPaths.mjs.map +1 -0
- package/dist/esm/react-flow/xyPaths.native.js +3 -0
- package/dist/esm/react-flow/xyPaths.native.js.map +1 -0
- package/dist/jsx/edges/GraphEdge.mjs +23 -52
- package/dist/jsx/edges/GraphEdge.mjs.map +1 -1
- package/dist/jsx/edges/GraphEdge.native.js +23 -50
- package/dist/jsx/edges/GraphEdge.native.js.map +1 -1
- package/dist/jsx/edges/edgePath.mjs +68 -0
- package/dist/jsx/edges/edgePath.mjs.map +1 -0
- package/dist/jsx/edges/edgePath.native.js +98 -0
- package/dist/jsx/edges/edgePath.native.js.map +1 -0
- package/dist/jsx/edges/edgeRoute.mjs +857 -0
- package/dist/jsx/edges/edgeRoute.mjs.map +1 -0
- package/dist/jsx/edges/edgeRoute.native.js +1399 -0
- package/dist/jsx/edges/edgeRoute.native.js.map +1 -0
- package/dist/jsx/edges/useEdgeObstacles.mjs +52 -0
- package/dist/jsx/edges/useEdgeObstacles.mjs.map +1 -0
- package/dist/jsx/edges/useEdgeObstacles.native.js +110 -0
- package/dist/jsx/edges/useEdgeObstacles.native.js.map +1 -0
- package/dist/jsx/react-flow/xy.mjs +2 -1
- package/dist/jsx/react-flow/xy.mjs.map +1 -1
- package/dist/jsx/react-flow/xy.native.js +4 -3
- package/dist/jsx/react-flow/xy.native.js.map +1 -1
- package/dist/jsx/react-flow/xyPaths.mjs +3 -0
- package/dist/jsx/react-flow/xyPaths.mjs.map +1 -0
- package/dist/jsx/react-flow/xyPaths.native.js +34 -0
- package/dist/jsx/react-flow/xyPaths.native.js.map +1 -0
- package/package.json +6 -6
- package/src/edges/GraphEdge.tsx +18 -61
- package/src/edges/edgePath.ts +84 -0
- package/src/edges/edgeRoute.spec.ts +631 -0
- package/src/edges/edgeRoute.ts +980 -0
- package/src/edges/useEdgeObstacles.ts +71 -0
- package/src/react-flow/NodeGraphWeb.stories.tsx +80 -0
- package/src/react-flow/xy.ts +1 -3
- package/src/react-flow/xyPaths.ts +9 -0
- package/types/edges/GraphEdge.d.ts +1 -16
- package/types/edges/GraphEdge.d.ts.map +1 -1
- package/types/edges/edgePath.d.ts +25 -0
- package/types/edges/edgePath.d.ts.map +1 -0
- package/types/edges/edgeRoute.d.ts +77 -0
- package/types/edges/edgeRoute.d.ts.map +1 -0
- package/types/edges/useEdgeObstacles.d.ts +45 -0
- package/types/edges/useEdgeObstacles.d.ts.map +1 -0
- package/types/react-flow/xy.d.ts +2 -1
- package/types/react-flow/xy.d.ts.map +1 -1
- package/types/react-flow/xyPaths.d.ts +9 -0
- package/types/react-flow/xyPaths.d.ts.map +1 -0
|
@@ -0,0 +1,631 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FZ-9208: no edge is drawn through a node box.
|
|
3
|
+
*
|
|
4
|
+
* The graph is NodeGraph → Main as the live Storybook measures it
|
|
5
|
+
* (`demoDocumentLarge(12)`: a 280 x 200 grid, Source and Sink 128 tall,
|
|
6
|
+
* Transform 176, Sink 240 wide). Two of its edges run backward, from the last
|
|
7
|
+
* column of one row to the first column of the next, and before this card one
|
|
8
|
+
* cut diagonally through two nodes while the other rode the next row's top
|
|
9
|
+
* border.
|
|
10
|
+
*
|
|
11
|
+
* The sampler here is the spec's own, independent of the router's, so the
|
|
12
|
+
* router cannot pass by agreeing with itself.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { describe, expect, it } from "vitest";
|
|
16
|
+
import { Position } from "../react-flow/xy";
|
|
17
|
+
import { buildEdgePath } from "./edgePath";
|
|
18
|
+
import type { EdgePathKind } from "./edgeStyle";
|
|
19
|
+
|
|
20
|
+
interface Box {
|
|
21
|
+
readonly id: string;
|
|
22
|
+
readonly x: number;
|
|
23
|
+
readonly y: number;
|
|
24
|
+
readonly width: number;
|
|
25
|
+
readonly height: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface Point {
|
|
29
|
+
readonly x: number;
|
|
30
|
+
readonly y: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const clearance = 8;
|
|
34
|
+
const paths: readonly EdgePathKind[] = ["bezier", "smoothstep", "step", "straight"];
|
|
35
|
+
|
|
36
|
+
const kinds = ["source", "transform", "sink"] as const;
|
|
37
|
+
const size = {
|
|
38
|
+
source: { width: 190, height: 128 },
|
|
39
|
+
transform: { width: 190, height: 176 },
|
|
40
|
+
sink: { width: 240, height: 128 },
|
|
41
|
+
} as const;
|
|
42
|
+
/** Port centre below the node's top edge, as measured: the first port row, then the second. */
|
|
43
|
+
const firstPortY = 99;
|
|
44
|
+
const secondPortY = 123;
|
|
45
|
+
|
|
46
|
+
const boxes: readonly Box[] = Array.from({ length: 12 }, (_, index) => {
|
|
47
|
+
const kind = kinds[index % kinds.length];
|
|
48
|
+
return {
|
|
49
|
+
id: `n${index}`,
|
|
50
|
+
x: (index % 4) * 280,
|
|
51
|
+
y: Math.floor(index / 4) * 200,
|
|
52
|
+
...size[kind],
|
|
53
|
+
};
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
interface MainEdge {
|
|
57
|
+
readonly id: string;
|
|
58
|
+
readonly source: Box;
|
|
59
|
+
readonly target: Box;
|
|
60
|
+
readonly sourcePortY: number;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const mainEdges: readonly MainEdge[] = boxes.flatMap((from, index) => {
|
|
64
|
+
const to = boxes[index + 1];
|
|
65
|
+
const kind = kinds[index % kinds.length];
|
|
66
|
+
if (!to || kind === "sink") return [];
|
|
67
|
+
return [
|
|
68
|
+
{
|
|
69
|
+
id: `${from.id}->${to.id}`,
|
|
70
|
+
source: from,
|
|
71
|
+
target: to,
|
|
72
|
+
sourcePortY: kind === "transform" ? secondPortY : firstPortY,
|
|
73
|
+
},
|
|
74
|
+
];
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
function numbersAndCommands(d: string): (string | number)[] {
|
|
78
|
+
const tokens = d.match(/[MLQCZ]|-?(?:\d+\.?\d*|\.\d+)(?:e[-+]?\d+)?/gi) ?? [];
|
|
79
|
+
return tokens.map((token) => (/^[MLQCZ]$/i.test(token) ? token.toUpperCase() : Number(token)));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Absolute M, L, Q and C, which is every command React Flow and the router write. */
|
|
83
|
+
function sample(d: string): Point[] {
|
|
84
|
+
const tokens = numbersAndCommands(d);
|
|
85
|
+
const points: Point[] = [];
|
|
86
|
+
let at: Point = { x: 0, y: 0 };
|
|
87
|
+
let command = "M";
|
|
88
|
+
let index = 0;
|
|
89
|
+
const take = (): number => {
|
|
90
|
+
const value = tokens[index];
|
|
91
|
+
index += 1;
|
|
92
|
+
if (typeof value !== "number") throw new Error(`expected a number in ${d}`);
|
|
93
|
+
return value;
|
|
94
|
+
};
|
|
95
|
+
while (index < tokens.length) {
|
|
96
|
+
if (typeof tokens[index] === "string") {
|
|
97
|
+
command = tokens[index] as string;
|
|
98
|
+
index += 1;
|
|
99
|
+
if (command === "Z") continue;
|
|
100
|
+
}
|
|
101
|
+
if (command === "M") {
|
|
102
|
+
at = { x: take(), y: take() };
|
|
103
|
+
points.push(at);
|
|
104
|
+
command = "L";
|
|
105
|
+
} else if (command === "L") {
|
|
106
|
+
const to = { x: take(), y: take() };
|
|
107
|
+
const steps = Math.max(1, Math.ceil(Math.hypot(to.x - at.x, to.y - at.y)));
|
|
108
|
+
for (let step = 1; step <= steps; step += 1) {
|
|
109
|
+
const t = step / steps;
|
|
110
|
+
points.push({ x: at.x + (to.x - at.x) * t, y: at.y + (to.y - at.y) * t });
|
|
111
|
+
}
|
|
112
|
+
at = to;
|
|
113
|
+
} else if (command === "Q") {
|
|
114
|
+
const control = { x: take(), y: take() };
|
|
115
|
+
const to = { x: take(), y: take() };
|
|
116
|
+
for (let step = 1; step <= 32; step += 1) {
|
|
117
|
+
const t = step / 32;
|
|
118
|
+
const u = 1 - t;
|
|
119
|
+
points.push({
|
|
120
|
+
x: u * u * at.x + 2 * u * t * control.x + t * t * to.x,
|
|
121
|
+
y: u * u * at.y + 2 * u * t * control.y + t * t * to.y,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
at = to;
|
|
125
|
+
} else if (command === "C") {
|
|
126
|
+
const first = { x: take(), y: take() };
|
|
127
|
+
const second = { x: take(), y: take() };
|
|
128
|
+
const to = { x: take(), y: take() };
|
|
129
|
+
for (let step = 1; step <= 256; step += 1) {
|
|
130
|
+
const t = step / 256;
|
|
131
|
+
const u = 1 - t;
|
|
132
|
+
points.push({
|
|
133
|
+
x:
|
|
134
|
+
u * u * u * at.x +
|
|
135
|
+
3 * u * u * t * first.x +
|
|
136
|
+
3 * u * t * t * second.x +
|
|
137
|
+
t * t * t * to.x,
|
|
138
|
+
y:
|
|
139
|
+
u * u * u * at.y +
|
|
140
|
+
3 * u * u * t * first.y +
|
|
141
|
+
3 * u * t * t * second.y +
|
|
142
|
+
t * t * t * to.y,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
at = to;
|
|
146
|
+
} else {
|
|
147
|
+
throw new Error(`unsupported command ${command} in ${d}`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return points;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function distanceToBox(point: Point, box: Box): number {
|
|
154
|
+
const dx = Math.max(box.x - point.x, 0, point.x - (box.x + box.width));
|
|
155
|
+
const dy = Math.max(box.y - point.y, 0, point.y - (box.y + box.height));
|
|
156
|
+
return Math.hypot(dx, dy);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
interface Endpoints {
|
|
160
|
+
readonly from: Point;
|
|
161
|
+
readonly to: Point;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* `inset` is how far inside its frame the port's edge point sits: 0 once ports
|
|
166
|
+
* sit on the frame edge (FZ-9012), the frame padding before that. The router
|
|
167
|
+
* has to hold for both.
|
|
168
|
+
*/
|
|
169
|
+
function endpoints(edge: MainEdge, inset: number): Endpoints {
|
|
170
|
+
return {
|
|
171
|
+
from: { x: edge.source.x + edge.source.width - inset, y: edge.source.y + edge.sourcePortY },
|
|
172
|
+
to: { x: edge.target.x + inset, y: edge.target.y + firstPortY },
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function route(edge: MainEdge, path: EdgePathKind, inset: number): { d: string; ends: Endpoints } {
|
|
177
|
+
const ends = endpoints(edge, inset);
|
|
178
|
+
const [d] = buildEdgePath(
|
|
179
|
+
path,
|
|
180
|
+
{
|
|
181
|
+
sourceX: ends.from.x,
|
|
182
|
+
sourceY: ends.from.y,
|
|
183
|
+
targetX: ends.to.x,
|
|
184
|
+
targetY: ends.to.y,
|
|
185
|
+
sourcePosition: Position.Right,
|
|
186
|
+
targetPosition: Position.Left,
|
|
187
|
+
},
|
|
188
|
+
{ boxes, clearance },
|
|
189
|
+
);
|
|
190
|
+
return { d, ends };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Each sample closer than the clearance to a node box, outside the port stubs at the ends. */
|
|
194
|
+
function intrusions(d: string, ends: Endpoints, inset: number): string[] {
|
|
195
|
+
const stub = inset + clearance;
|
|
196
|
+
const found: string[] = [];
|
|
197
|
+
for (const point of sample(d)) {
|
|
198
|
+
if (Math.hypot(point.x - ends.from.x, point.y - ends.from.y) <= stub) continue;
|
|
199
|
+
if (Math.hypot(point.x - ends.to.x, point.y - ends.to.y) <= stub) continue;
|
|
200
|
+
for (const box of boxes) {
|
|
201
|
+
const distance = distanceToBox(point, box);
|
|
202
|
+
if (distance < clearance - 0.5) {
|
|
203
|
+
found.push(
|
|
204
|
+
`${box.id} at (${point.x.toFixed(1)}, ${point.y.toFixed(1)}), ${distance.toFixed(1)} away`,
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return found;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
describe("edges keep out of node boxes (FZ-9208)", () => {
|
|
213
|
+
for (const inset of [0, 12]) {
|
|
214
|
+
for (const path of paths) {
|
|
215
|
+
it.each(mainEdges.map((edge) => [edge.id, edge] as const))(
|
|
216
|
+
`%s, ${path}, port inset ${inset}: no sample comes within the clearance of a node box`,
|
|
217
|
+
(_id, edge) => {
|
|
218
|
+
const { d, ends } = route(edge, path, inset);
|
|
219
|
+
const hits = intrusions(d, ends, inset);
|
|
220
|
+
expect(hits.slice(0, 3), `${hits.length} samples intrude; d=${d}`).toEqual([]);
|
|
221
|
+
},
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
it.each(paths)("routes the backward n7->n8 %s edge around the boxes, not across them", (path) => {
|
|
227
|
+
const edge = mainEdges.find((candidate) => candidate.id === "n7->n8");
|
|
228
|
+
if (!edge) throw new Error("Main has no n7->n8 edge");
|
|
229
|
+
const { d, ends } = route(edge, path, 0);
|
|
230
|
+
expect(intrusions(d, ends, 0)).toEqual([]);
|
|
231
|
+
const points = sample(d);
|
|
232
|
+
// Around means through the gap between the rows: the path's lowest run
|
|
233
|
+
// above row 2 sits below row 1's tallest box.
|
|
234
|
+
const between = points.filter((point) => point.x > 240 + clearance && point.x < 840);
|
|
235
|
+
expect(between.length).toBeGreaterThan(0);
|
|
236
|
+
for (const point of between) {
|
|
237
|
+
expect(point.y).toBeGreaterThanOrEqual(200 + 176 + clearance - 0.5);
|
|
238
|
+
expect(point.y).toBeLessThanOrEqual(400 - clearance + 0.5);
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
/** Every turn in `d`: a Q bend with its radius, or a sharp L-to-L corner as radius 0. */
|
|
243
|
+
function turns(d: string): number[] {
|
|
244
|
+
const tokens = numbersAndCommands(d);
|
|
245
|
+
const segments: { command: string; points: Point[] }[] = [];
|
|
246
|
+
let index = 0;
|
|
247
|
+
let command = "M";
|
|
248
|
+
while (index < tokens.length) {
|
|
249
|
+
if (typeof tokens[index] === "string") {
|
|
250
|
+
command = tokens[index] as string;
|
|
251
|
+
index += 1;
|
|
252
|
+
}
|
|
253
|
+
const count = command === "Q" ? 2 : command === "C" ? 3 : 1;
|
|
254
|
+
const points: Point[] = [];
|
|
255
|
+
for (let point = 0; point < count; point += 1) {
|
|
256
|
+
points.push({ x: tokens[index] as number, y: tokens[index + 1] as number });
|
|
257
|
+
index += 2;
|
|
258
|
+
}
|
|
259
|
+
segments.push({ command, points });
|
|
260
|
+
}
|
|
261
|
+
const found: number[] = [];
|
|
262
|
+
for (let at = 1; at < segments.length; at += 1) {
|
|
263
|
+
const segment = segments[at];
|
|
264
|
+
const start = segments[at - 1].points[segments[at - 1].points.length - 1];
|
|
265
|
+
if (segment.command === "Q") {
|
|
266
|
+
const [control] = segment.points;
|
|
267
|
+
found.push(Math.hypot(control.x - start.x, control.y - start.y));
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
const next = segments[at + 1];
|
|
271
|
+
if (segment.command !== "L" || next?.command !== "L") continue;
|
|
272
|
+
const corner = segment.points[0];
|
|
273
|
+
const after = next.points[0];
|
|
274
|
+
const cross =
|
|
275
|
+
(corner.x - start.x) * (after.y - corner.y) - (corner.y - start.y) * (after.x - corner.x);
|
|
276
|
+
if (Math.abs(cross) > 1e-6) found.push(0);
|
|
277
|
+
}
|
|
278
|
+
return found;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const rounded: readonly ["bezier" | "smoothstep", number][] = [
|
|
282
|
+
["bezier", 24],
|
|
283
|
+
["smoothstep", 5],
|
|
284
|
+
];
|
|
285
|
+
for (const inset of [0, 12]) {
|
|
286
|
+
it.each(rounded)(
|
|
287
|
+
`bends a rerouted %s edge round at every turn, port inset ${inset}: radius at least min(%d, corridor / 2)`,
|
|
288
|
+
(path, radius) => {
|
|
289
|
+
// The tightest corridor in Main: the 24-unit row gap less the clearance on both sides.
|
|
290
|
+
const corridor = 200 - 176 - clearance * 2;
|
|
291
|
+
const least = Math.min(radius, corridor / 2);
|
|
292
|
+
for (const id of ["n3->n4", "n7->n8"]) {
|
|
293
|
+
const edge = mainEdges.find((candidate) => candidate.id === id);
|
|
294
|
+
if (!edge) throw new Error(`Main has no ${id} edge`);
|
|
295
|
+
const { d, ends } = route(edge, path, inset);
|
|
296
|
+
expect(intrusions(d, ends, inset)).toEqual([]);
|
|
297
|
+
const bends = turns(d);
|
|
298
|
+
expect(bends.length, `${id} d=${d}`).toBeGreaterThan(1);
|
|
299
|
+
for (const bend of bends)
|
|
300
|
+
expect(bend, `${id} d=${d}`).toBeGreaterThanOrEqual(least - 0.01);
|
|
301
|
+
}
|
|
302
|
+
},
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
it.each(paths)("keeps the %s path's ends on the ports, so the arrowhead still lands", (path) => {
|
|
307
|
+
for (const edge of mainEdges) {
|
|
308
|
+
const { d, ends } = route(edge, path, 0);
|
|
309
|
+
const points = sample(d);
|
|
310
|
+
const first = points[0];
|
|
311
|
+
const last = points[points.length - 1];
|
|
312
|
+
expect(Math.hypot(first.x - ends.from.x, first.y - ends.from.y)).toBeLessThan(0.5);
|
|
313
|
+
expect(Math.hypot(last.x - ends.to.x, last.y - ends.to.y)).toBeLessThan(0.5);
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
it("leaves an edge with a clear run alone: n0->n1 smoothstep is React Flow's own path", () => {
|
|
318
|
+
const edge = mainEdges[0];
|
|
319
|
+
const ends = endpoints(edge, 0);
|
|
320
|
+
const geometry = {
|
|
321
|
+
sourceX: ends.from.x,
|
|
322
|
+
sourceY: ends.from.y,
|
|
323
|
+
targetX: ends.to.x,
|
|
324
|
+
targetY: ends.to.y,
|
|
325
|
+
sourcePosition: Position.Right,
|
|
326
|
+
targetPosition: Position.Left,
|
|
327
|
+
};
|
|
328
|
+
expect(buildEdgePath("smoothstep", geometry, { boxes, clearance })[0]).toBe(
|
|
329
|
+
buildEdgePath("smoothstep", geometry)[0],
|
|
330
|
+
);
|
|
331
|
+
});
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
describe("edgePath purity (FZ-8149)", () => {
|
|
335
|
+
it("draws edges without evaluating @xyflow/react or the React Flow store", async () => {
|
|
336
|
+
const { readFileSync } = await import("node:fs");
|
|
337
|
+
const { fileURLToPath } = await import("node:url");
|
|
338
|
+
const read = (rel: string) =>
|
|
339
|
+
readFileSync(fileURLToPath(new URL(rel, import.meta.url)), "utf8");
|
|
340
|
+
const imports = (src: string) => [...src.matchAll(/from "([^"]+)"/g)].map((m) => m[1]);
|
|
341
|
+
expect(imports(read("./edgePath.ts"))).toEqual([
|
|
342
|
+
"../react-flow/xyPaths",
|
|
343
|
+
"../react-flow/xyPaths",
|
|
344
|
+
"./edgeRoute",
|
|
345
|
+
"./edgeRoute",
|
|
346
|
+
"./edgeStyle",
|
|
347
|
+
]);
|
|
348
|
+
expect(imports(read("../react-flow/xyPaths.ts"))).toEqual(["@xyflow/system"]);
|
|
349
|
+
});
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* FZ-9291: edges that cross the same gap between two ranks used to take the
|
|
354
|
+
* gap's middle line, all of them, so on Ogden's apps graph eight edges into
|
|
355
|
+
* two targets read as one bus. Same-target edges now share a trunk and every
|
|
356
|
+
* other edge in the gap gets its own track.
|
|
357
|
+
*/
|
|
358
|
+
describe("edges sharing a rank gap get their own tracks (FZ-9291)", () => {
|
|
359
|
+
const width = 96;
|
|
360
|
+
const height = 72;
|
|
361
|
+
const sources: readonly Box[] = Array.from({ length: 5 }, (_, index) => ({
|
|
362
|
+
id: `s${index}`,
|
|
363
|
+
x: index * 112,
|
|
364
|
+
y: 0,
|
|
365
|
+
width,
|
|
366
|
+
height,
|
|
367
|
+
}));
|
|
368
|
+
const targets: readonly Box[] = [
|
|
369
|
+
{ id: "tA", x: 56, y: 113, width, height },
|
|
370
|
+
{ id: "tB", x: 336, y: 113, width, height },
|
|
371
|
+
];
|
|
372
|
+
const rows = [...sources, ...targets];
|
|
373
|
+
const links = [
|
|
374
|
+
{ source: "s0", target: "tA" },
|
|
375
|
+
{ source: "s1", target: "tA" },
|
|
376
|
+
{ source: "s3", target: "tA" },
|
|
377
|
+
{ source: "s2", target: "tB" },
|
|
378
|
+
{ source: "s4", target: "tB" },
|
|
379
|
+
];
|
|
380
|
+
|
|
381
|
+
/** `across` lays the ranks left to right instead of top to bottom by swapping x and y. */
|
|
382
|
+
function layout(across: boolean) {
|
|
383
|
+
const turn = (box: Box): Box =>
|
|
384
|
+
across ? { id: box.id, x: box.y, y: box.x, width: box.height, height: box.width } : box;
|
|
385
|
+
const placed = rows.map(turn);
|
|
386
|
+
const at = new Map(placed.map((box) => [box.id, box]));
|
|
387
|
+
const draw = (path: EdgePathKind, withLinks: boolean) =>
|
|
388
|
+
links.map((link) => {
|
|
389
|
+
const source = at.get(link.source)!;
|
|
390
|
+
const target = at.get(link.target)!;
|
|
391
|
+
const from = across
|
|
392
|
+
? { x: source.x + source.width, y: source.y + source.height / 2 }
|
|
393
|
+
: { x: source.x + source.width / 2, y: source.y + source.height };
|
|
394
|
+
const to = across
|
|
395
|
+
? { x: target.x, y: target.y + target.height / 2 }
|
|
396
|
+
: { x: target.x + target.width / 2, y: target.y };
|
|
397
|
+
const [d] = buildEdgePath(
|
|
398
|
+
path,
|
|
399
|
+
{
|
|
400
|
+
sourceX: from.x,
|
|
401
|
+
sourceY: from.y,
|
|
402
|
+
targetX: to.x,
|
|
403
|
+
targetY: to.y,
|
|
404
|
+
sourcePosition: across ? Position.Right : Position.Bottom,
|
|
405
|
+
targetPosition: across ? Position.Left : Position.Top,
|
|
406
|
+
},
|
|
407
|
+
withLinks ? { boxes: placed, clearance, links } : { boxes: placed, clearance },
|
|
408
|
+
);
|
|
409
|
+
return { link, d, from, to };
|
|
410
|
+
});
|
|
411
|
+
return { placed, draw };
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
interface Run {
|
|
415
|
+
readonly at: number;
|
|
416
|
+
readonly from: number;
|
|
417
|
+
readonly to: number;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/** The straight runs across the gap: perpendicular to the rank direction, longer than 2 units. */
|
|
421
|
+
function runs(d: string, across: boolean): Run[] {
|
|
422
|
+
const points = sample(d).map((point) => (across ? { x: point.y, y: point.x } : point));
|
|
423
|
+
const found: Run[] = [];
|
|
424
|
+
let start = 0;
|
|
425
|
+
for (let index = 1; index <= points.length; index += 1) {
|
|
426
|
+
const flat = index < points.length && Math.abs(points[index].y - points[start].y) < 0.01;
|
|
427
|
+
if (flat) continue;
|
|
428
|
+
const end = index - 1;
|
|
429
|
+
if (Math.abs(points[end].x - points[start].x) > 2) {
|
|
430
|
+
found.push({
|
|
431
|
+
at: points[start].y,
|
|
432
|
+
from: Math.min(points[start].x, points[end].x),
|
|
433
|
+
to: Math.max(points[start].x, points[end].x),
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
start = index;
|
|
437
|
+
}
|
|
438
|
+
return found;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
function shared(a: readonly Run[], b: readonly Run[]): Run[] {
|
|
442
|
+
return a.filter((run) =>
|
|
443
|
+
b.some(
|
|
444
|
+
(other) =>
|
|
445
|
+
Math.abs(run.at - other.at) < 2 &&
|
|
446
|
+
Math.min(run.to, other.to) - Math.max(run.from, other.from) > 1,
|
|
447
|
+
),
|
|
448
|
+
);
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
for (const across of [false, true]) {
|
|
452
|
+
const direction = across ? "LR" : "TB";
|
|
453
|
+
for (const path of ["smoothstep", "step"] as const) {
|
|
454
|
+
it(`${direction} ${path}: two edges into different targets never draw the same run`, () => {
|
|
455
|
+
const drawn = layout(across).draw(path, true);
|
|
456
|
+
for (const a of drawn) {
|
|
457
|
+
for (const b of drawn) {
|
|
458
|
+
if (a.link.target === b.link.target) continue;
|
|
459
|
+
const overlap = shared(runs(a.d, across), runs(b.d, across));
|
|
460
|
+
expect(
|
|
461
|
+
overlap,
|
|
462
|
+
`${a.link.source}->${a.link.target} and ${b.link.source}->${b.link.target}`,
|
|
463
|
+
).toEqual([]);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
});
|
|
467
|
+
|
|
468
|
+
it(`${direction} ${path}: edges into one target merge into a trunk that ends at that target`, () => {
|
|
469
|
+
const drawn = layout(across).draw(path, true);
|
|
470
|
+
for (const target of ["tA", "tB"]) {
|
|
471
|
+
const into = drawn.filter((edge) => edge.link.target === target);
|
|
472
|
+
const levels = new Set(
|
|
473
|
+
into.flatMap((edge) => runs(edge.d, across).map((run) => Math.round(run.at * 100))),
|
|
474
|
+
);
|
|
475
|
+
expect(levels.size, target).toBe(1);
|
|
476
|
+
for (const edge of into) {
|
|
477
|
+
const points = sample(edge.d);
|
|
478
|
+
const last = points[points.length - 1];
|
|
479
|
+
expect(Math.hypot(last.x - edge.to.x, last.y - edge.to.y)).toBeLessThan(0.5);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
it(`${direction} ${path}: every track keeps the clearance from every box`, () => {
|
|
485
|
+
const { placed, draw } = layout(across);
|
|
486
|
+
for (const edge of draw(path, true)) {
|
|
487
|
+
const stub = clearance;
|
|
488
|
+
for (const point of sample(edge.d)) {
|
|
489
|
+
if (Math.hypot(point.x - edge.from.x, point.y - edge.from.y) <= stub) continue;
|
|
490
|
+
if (Math.hypot(point.x - edge.to.x, point.y - edge.to.y) <= stub) continue;
|
|
491
|
+
for (const box of placed)
|
|
492
|
+
expect(distanceToBox(point, box), `${edge.link.source}->${box.id}`).toBeGreaterThan(
|
|
493
|
+
clearance - 0.5,
|
|
494
|
+
);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
it("an edge that skips a rank doesn't cost the gap its tracks", () => {
|
|
502
|
+
const far: Box = { id: "far", x: 448, y: -113, width, height };
|
|
503
|
+
const placed = [...rows, far];
|
|
504
|
+
const withSkip = [...links, { source: "far", target: "tA" }];
|
|
505
|
+
const level = (source: string, target: string) => {
|
|
506
|
+
const from = placed.find((box) => box.id === source)!;
|
|
507
|
+
const to = placed.find((box) => box.id === target)!;
|
|
508
|
+
const [d] = buildEdgePath(
|
|
509
|
+
"smoothstep",
|
|
510
|
+
{
|
|
511
|
+
sourceX: from.x + width / 2,
|
|
512
|
+
sourceY: from.y + height,
|
|
513
|
+
targetX: to.x + width / 2,
|
|
514
|
+
targetY: to.y,
|
|
515
|
+
sourcePosition: Position.Bottom,
|
|
516
|
+
targetPosition: Position.Top,
|
|
517
|
+
},
|
|
518
|
+
{ boxes: placed, clearance, links: withSkip },
|
|
519
|
+
);
|
|
520
|
+
return runs(d, false).map((run) => Math.round(run.at * 100));
|
|
521
|
+
};
|
|
522
|
+
expect(level("s0", "tA")).not.toEqual(level("s2", "tB"));
|
|
523
|
+
expect(level("s1", "tA")).toEqual(level("s3", "tA"));
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
it("an edge that skips a rank passes the rank between boxes and joins its target's trunk", () => {
|
|
527
|
+
const far: Box = { id: "far", x: 448, y: -113, width, height };
|
|
528
|
+
const placed = [...rows, far];
|
|
529
|
+
const withSkip = [...links, { source: "far", target: "tA" }];
|
|
530
|
+
const draw = (source: string, target: string) => {
|
|
531
|
+
const from = placed.find((box) => box.id === source)!;
|
|
532
|
+
const to = placed.find((box) => box.id === target)!;
|
|
533
|
+
const ends = {
|
|
534
|
+
from: { x: from.x + width / 2, y: from.y + height },
|
|
535
|
+
to: { x: to.x + width / 2, y: to.y },
|
|
536
|
+
};
|
|
537
|
+
const [d] = buildEdgePath(
|
|
538
|
+
"smoothstep",
|
|
539
|
+
{
|
|
540
|
+
sourceX: ends.from.x,
|
|
541
|
+
sourceY: ends.from.y,
|
|
542
|
+
targetX: ends.to.x,
|
|
543
|
+
targetY: ends.to.y,
|
|
544
|
+
sourcePosition: Position.Bottom,
|
|
545
|
+
targetPosition: Position.Top,
|
|
546
|
+
},
|
|
547
|
+
{ boxes: placed, clearance, links: withSkip },
|
|
548
|
+
);
|
|
549
|
+
return { d, ...ends };
|
|
550
|
+
};
|
|
551
|
+
const skip = draw("far", "tA");
|
|
552
|
+
for (const point of sample(skip.d)) {
|
|
553
|
+
if (Math.hypot(point.x - skip.from.x, point.y - skip.from.y) <= clearance) continue;
|
|
554
|
+
if (Math.hypot(point.x - skip.to.x, point.y - skip.to.y) <= clearance) continue;
|
|
555
|
+
for (const box of placed)
|
|
556
|
+
expect(distanceToBox(point, box), box.id).toBeGreaterThan(clearance - 0.5);
|
|
557
|
+
}
|
|
558
|
+
const last = runs(skip.d, false).at(-1)!;
|
|
559
|
+
const trunk = runs(draw("s0", "tA").d, false).at(-1)!;
|
|
560
|
+
expect(Math.round(last.at * 100)).toBe(Math.round(trunk.at * 100));
|
|
561
|
+
});
|
|
562
|
+
|
|
563
|
+
it("an edge that skips a rank holds one line through the channel between that rank's boxes", () => {
|
|
564
|
+
const top: Box = { id: "top", x: 0, y: 0, width, height };
|
|
565
|
+
const left: Box = { id: "left", x: 0, y: 113, width, height };
|
|
566
|
+
const right: Box = { id: "right", x: 224, y: 113, width, height };
|
|
567
|
+
const bottom: Box = { id: "bottom", x: 112, y: 226, width, height };
|
|
568
|
+
const placed = [top, left, right, bottom];
|
|
569
|
+
const chain = [
|
|
570
|
+
{ source: "top", target: "left" },
|
|
571
|
+
{ source: "top", target: "right" },
|
|
572
|
+
{ source: "left", target: "bottom" },
|
|
573
|
+
{ source: "right", target: "bottom" },
|
|
574
|
+
{ source: "top", target: "bottom" },
|
|
575
|
+
];
|
|
576
|
+
for (const path of ["smoothstep", "step"] as const) {
|
|
577
|
+
const [d] = buildEdgePath(
|
|
578
|
+
path,
|
|
579
|
+
{
|
|
580
|
+
sourceX: 48,
|
|
581
|
+
sourceY: 72,
|
|
582
|
+
targetX: 160,
|
|
583
|
+
targetY: 226,
|
|
584
|
+
sourcePosition: Position.Bottom,
|
|
585
|
+
targetPosition: Position.Top,
|
|
586
|
+
},
|
|
587
|
+
{ boxes: placed, clearance, links: chain },
|
|
588
|
+
);
|
|
589
|
+
const through = sample(d).filter((point) => point.y >= 113 && point.y <= 185);
|
|
590
|
+
expect(through.length, path).toBeGreaterThan(0);
|
|
591
|
+
for (const point of through) expect(Math.abs(point.x - 160), path).toBeLessThan(0.5);
|
|
592
|
+
expect(runs(d, false), path).toHaveLength(1);
|
|
593
|
+
}
|
|
594
|
+
});
|
|
595
|
+
|
|
596
|
+
it("without the edge list every edge keeps its own path, as before", () => {
|
|
597
|
+
const { draw } = layout(false);
|
|
598
|
+
const plain = draw("smoothstep", false);
|
|
599
|
+
for (const edge of plain) {
|
|
600
|
+
const alone = buildEdgePath("smoothstep", {
|
|
601
|
+
sourceX: edge.from.x,
|
|
602
|
+
sourceY: edge.from.y,
|
|
603
|
+
targetX: edge.to.x,
|
|
604
|
+
targetY: edge.to.y,
|
|
605
|
+
sourcePosition: Position.Bottom,
|
|
606
|
+
targetPosition: Position.Top,
|
|
607
|
+
});
|
|
608
|
+
expect(edge.d).toBe(alone[0]);
|
|
609
|
+
}
|
|
610
|
+
});
|
|
611
|
+
|
|
612
|
+
it("Main's chain keeps every path when the edge list rides along: no two of its edges share a gap", () => {
|
|
613
|
+
const mainLinks = mainEdges.map((edge) => ({ source: edge.source.id, target: edge.target.id }));
|
|
614
|
+
for (const path of paths) {
|
|
615
|
+
for (const edge of mainEdges) {
|
|
616
|
+
const ends = endpoints(edge, 0);
|
|
617
|
+
const geometry = {
|
|
618
|
+
sourceX: ends.from.x,
|
|
619
|
+
sourceY: ends.from.y,
|
|
620
|
+
targetX: ends.to.x,
|
|
621
|
+
targetY: ends.to.y,
|
|
622
|
+
sourcePosition: Position.Right,
|
|
623
|
+
targetPosition: Position.Left,
|
|
624
|
+
};
|
|
625
|
+
expect(buildEdgePath(path, geometry, { boxes, clearance, links: mainLinks })[0]).toBe(
|
|
626
|
+
buildEdgePath(path, geometry, { boxes, clearance })[0],
|
|
627
|
+
);
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
});
|
|
631
|
+
});
|