Edges follow ELK's routes: layoutGraph returns each edge's start, bends, end and chip point, LiveEdge draws them square with rounded corners, moved onto the drawn handles, and keeps the curve for an edge whose node moved since; a feedback edge clears the nodes it passes
This commit is contained in:
@@ -51,14 +51,22 @@ const one = (port: string) => ({ requires: [port], provides: [port] })
|
||||
// A chain lays out along the flow, an edge at a time.
|
||||
const chain = await layoutGraph(
|
||||
["a", "b"],
|
||||
[{ source: "a", target: "b", sourceHandle: "v", targetHandle: "v" }],
|
||||
[
|
||||
{
|
||||
id: "a>b",
|
||||
source: "a",
|
||||
target: "b",
|
||||
sourceHandle: "v",
|
||||
targetHandle: "v",
|
||||
},
|
||||
],
|
||||
"LR",
|
||||
new Map([
|
||||
["a", one("v")],
|
||||
["b", one("v")],
|
||||
]),
|
||||
)
|
||||
const [ca, cb] = [chain.get("a"), chain.get("b")]
|
||||
const [ca, cb] = [chain.positions.get("a"), chain.positions.get("b")]
|
||||
assert.ok(ca && cb)
|
||||
// Two single-port nodes wired to each other have nothing to step around, so
|
||||
// the edge between them is level — which is what a straight edge means here.
|
||||
@@ -69,14 +77,22 @@ assert.ok(cb.x > ca.x)
|
||||
// itself.
|
||||
const again = await layoutGraph(
|
||||
["a", "b"],
|
||||
[{ source: "a", target: "b", sourceHandle: "v", targetHandle: "v" }],
|
||||
[
|
||||
{
|
||||
id: "a>b",
|
||||
source: "a",
|
||||
target: "b",
|
||||
sourceHandle: "v",
|
||||
targetHandle: "v",
|
||||
},
|
||||
],
|
||||
"LR",
|
||||
new Map([
|
||||
["a", one("v")],
|
||||
["b", one("v")],
|
||||
]),
|
||||
)
|
||||
assert.deepEqual([...again], [...chain])
|
||||
assert.deepEqual(again, chain)
|
||||
|
||||
/**
|
||||
* The crossing this whole thing exists for.
|
||||
@@ -89,8 +105,20 @@ assert.deepEqual([...again], [...chain])
|
||||
const fan = await layoutGraph(
|
||||
["a", "b", "c"],
|
||||
[
|
||||
{ source: "a", target: "c", sourceHandle: "out", targetHandle: "low" },
|
||||
{ source: "b", target: "c", sourceHandle: "out", targetHandle: "high" },
|
||||
{
|
||||
id: "a>c",
|
||||
source: "a",
|
||||
target: "c",
|
||||
sourceHandle: "out",
|
||||
targetHandle: "low",
|
||||
},
|
||||
{
|
||||
id: "b>c",
|
||||
source: "b",
|
||||
target: "c",
|
||||
sourceHandle: "out",
|
||||
targetHandle: "high",
|
||||
},
|
||||
],
|
||||
"LR",
|
||||
new Map([
|
||||
@@ -99,7 +127,7 @@ const fan = await layoutGraph(
|
||||
["c", { requires: ["high", "low"], provides: [] }],
|
||||
]),
|
||||
)
|
||||
const [fa, fb] = [fan.get("a"), fan.get("b")]
|
||||
const [fa, fb] = [fan.positions.get("a"), fan.positions.get("b")]
|
||||
assert.ok(fa && fb)
|
||||
assert.ok(
|
||||
fb.y < fa.y,
|
||||
@@ -111,8 +139,20 @@ assert.ok(
|
||||
const parallel = await layoutGraph(
|
||||
["a", "b"],
|
||||
[
|
||||
{ source: "a", target: "b", sourceHandle: "p1", targetHandle: "q1" },
|
||||
{ source: "a", target: "b", sourceHandle: "p2", targetHandle: "q2" },
|
||||
{
|
||||
id: "1",
|
||||
source: "a",
|
||||
target: "b",
|
||||
sourceHandle: "p1",
|
||||
targetHandle: "q1",
|
||||
},
|
||||
{
|
||||
id: "2",
|
||||
source: "a",
|
||||
target: "b",
|
||||
sourceHandle: "p2",
|
||||
targetHandle: "q2",
|
||||
},
|
||||
],
|
||||
"LR",
|
||||
new Map([
|
||||
@@ -120,7 +160,7 @@ const parallel = await layoutGraph(
|
||||
["b", { requires: ["q1", "q2"], provides: [] }],
|
||||
]),
|
||||
)
|
||||
const [pa, pb] = [parallel.get("a"), parallel.get("b")]
|
||||
const [pa, pb] = [parallel.positions.get("a"), parallel.positions.get("b")]
|
||||
assert.ok(pa && pb)
|
||||
assert.ok(pb.x > pa.x + 220, "the pair still ranks one after the other")
|
||||
|
||||
@@ -128,14 +168,25 @@ assert.ok(pb.x > pa.x + 220, "the pair still ranks one after the other")
|
||||
// a node does — 192 against 220, plus the rank gap either side of the chip.
|
||||
const labelled = await layoutGraph(
|
||||
["input:x", "n"],
|
||||
[{ source: "input:x", target: "n", sourceHandle: "x", targetHandle: "x" }],
|
||||
[
|
||||
{
|
||||
id: "input:x>n",
|
||||
source: "input:x",
|
||||
target: "n",
|
||||
sourceHandle: "x",
|
||||
targetHandle: "x",
|
||||
},
|
||||
],
|
||||
"LR",
|
||||
new Map([
|
||||
["input:x", { requires: [], provides: ["x"], endpoint: true }],
|
||||
["n", { requires: ["x"], provides: [] }],
|
||||
]),
|
||||
)
|
||||
const [ex, en] = [labelled.get("input:x"), labelled.get("n")]
|
||||
const [ex, en] = [
|
||||
labelled.positions.get("input:x"),
|
||||
labelled.positions.get("n"),
|
||||
]
|
||||
assert.ok(ex && en)
|
||||
assert.equal(ex.x, 40, "the graph starts at its margin")
|
||||
assert.equal(
|
||||
@@ -155,9 +206,22 @@ assert.equal(
|
||||
const spine = await layoutGraph(
|
||||
["start", "mid", "end", "dashboard:panel:v"],
|
||||
[
|
||||
{ source: "start", target: "mid", sourceHandle: "v", targetHandle: "v" },
|
||||
{ source: "mid", target: "end", sourceHandle: "w", targetHandle: "w" },
|
||||
{
|
||||
id: "start>mid",
|
||||
source: "start",
|
||||
target: "mid",
|
||||
sourceHandle: "v",
|
||||
targetHandle: "v",
|
||||
},
|
||||
{
|
||||
id: "mid>end",
|
||||
source: "mid",
|
||||
target: "end",
|
||||
sourceHandle: "w",
|
||||
targetHandle: "w",
|
||||
},
|
||||
{
|
||||
id: "start>dashboard:panel:v",
|
||||
source: "start",
|
||||
target: "dashboard:panel:v",
|
||||
sourceHandle: "v",
|
||||
@@ -172,7 +236,7 @@ const spine = await layoutGraph(
|
||||
["dashboard:panel:v", { requires: ["v"], provides: [], endpoint: true }],
|
||||
]),
|
||||
)
|
||||
const [s1, s2] = [spine.get("start"), spine.get("mid")]
|
||||
const [s1, s2] = [spine.positions.get("start"), spine.positions.get("mid")]
|
||||
assert.ok(s1 && s2)
|
||||
assert.equal(
|
||||
s1.x,
|
||||
@@ -184,6 +248,7 @@ assert.equal(
|
||||
const wide = await layoutGraph(
|
||||
["src", "s0", "s1", "s2", "s3", "s4", "s5"],
|
||||
["s0", "s1", "s2", "s3", "s4", "s5"].map((sink) => ({
|
||||
id: sink,
|
||||
source: "src",
|
||||
target: sink,
|
||||
sourceHandle: "v",
|
||||
@@ -195,7 +260,7 @@ const wide = await layoutGraph(
|
||||
),
|
||||
)
|
||||
const rows = new Map<number, number>()
|
||||
for (const [, at] of wide) {
|
||||
for (const [, at] of wide.positions) {
|
||||
rows.set(Math.round(at.y), (rows.get(Math.round(at.y)) ?? 0) + 1)
|
||||
}
|
||||
for (const [y, count] of rows) {
|
||||
@@ -205,4 +270,65 @@ for (const [y, count] of rows) {
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Every edge comes back routed, port to port, square, and around what is in
|
||||
* the way. `back` closes a cycle over `mid`: drawn straight between its two
|
||||
* handles it would run through `mid`, which is what the route exists to avoid.
|
||||
*/
|
||||
for (const direction of ["LR", "TB"] as const) {
|
||||
const ids = ["start", "mid", "end"]
|
||||
const loop = await layoutGraph(
|
||||
ids,
|
||||
[
|
||||
{
|
||||
id: "on",
|
||||
source: "start",
|
||||
target: "mid",
|
||||
sourceHandle: "v",
|
||||
targetHandle: "v",
|
||||
},
|
||||
{
|
||||
id: "off",
|
||||
source: "mid",
|
||||
target: "end",
|
||||
sourceHandle: "v",
|
||||
targetHandle: "v",
|
||||
},
|
||||
{
|
||||
id: "back",
|
||||
source: "end",
|
||||
target: "start",
|
||||
sourceHandle: "v",
|
||||
targetHandle: "v",
|
||||
},
|
||||
],
|
||||
direction,
|
||||
new Map(ids.map((id) => [id, one("v")])),
|
||||
)
|
||||
const back = loop.routes.get("back")
|
||||
const end = loop.positions.get("end")
|
||||
const mid = loop.positions.get("mid")
|
||||
assert.ok(back && end && mid && loop.routes.size === 3)
|
||||
// One port on each side, so it sits halfway along: the bottom or the right.
|
||||
assert.deepEqual(
|
||||
back.points[0],
|
||||
direction === "TB"
|
||||
? { x: end.x + 110, y: end.y + NODE_H }
|
||||
: { x: end.x + 220, y: end.y + NODE_H / 2 },
|
||||
)
|
||||
for (let i = 1; i < back.points.length; i += 1) {
|
||||
const [a, b] = [back.points[i - 1], back.points[i]]
|
||||
assert.ok(a.x === b.x || a.y === b.y, `a slanted run in ${direction}`)
|
||||
const [left, right] = [Math.min(a.x, b.x), Math.max(a.x, b.x)]
|
||||
const [top, bottom] = [Math.min(a.y, b.y), Math.max(a.y, b.y)]
|
||||
assert.ok(
|
||||
right <= mid.x ||
|
||||
left >= mid.x + 220 ||
|
||||
bottom <= mid.y ||
|
||||
top >= mid.y + NODE_H,
|
||||
`the feedback edge runs through the node it passes in ${direction}`,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
console.log("layout: ok")
|
||||
|
||||
Reference in New Issue
Block a user