treelines 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +170 -2
- package/dist/core.cjs +207 -0
- package/dist/core.cjs.map +1 -0
- package/dist/core.d.cts +133 -0
- package/dist/core.d.ts +133 -0
- package/dist/core.js +175 -0
- package/dist/core.js.map +1 -0
- package/dist/index.cjs +548 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +300 -0
- package/dist/index.d.ts +300 -0
- package/dist/index.js +525 -0
- package/dist/index.js.map +1 -0
- package/package.json +80 -4
package/dist/core.d.ts
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The layout: where every node goes and the path of every connector, from the graph and the node sizes alone. No DOM and
|
|
3
|
+
* no React, so it runs anywhere (the server included) and gives the same numbers every time.
|
|
4
|
+
*
|
|
5
|
+
* The shape it draws: a list of lanes, each a root you can unfold. An unfolded lane shows the graph below its root
|
|
6
|
+
* beside it, in columns by depth: a node's column is its distance from the root, its row follows its parents (the average
|
|
7
|
+
* of where they are), pushed further along so nothing overlaps. Lanes stack, the open ones with room around them.
|
|
8
|
+
*
|
|
9
|
+
* "Main" is the axis the graph grows along (x when it grows to the right), "cross" the one lanes and rows stack along.
|
|
10
|
+
*/
|
|
11
|
+
type Id = string | number;
|
|
12
|
+
interface Point {
|
|
13
|
+
x: number;
|
|
14
|
+
y: number;
|
|
15
|
+
}
|
|
16
|
+
interface Size {
|
|
17
|
+
width: number;
|
|
18
|
+
height: number;
|
|
19
|
+
}
|
|
20
|
+
interface Rect extends Point, Size {
|
|
21
|
+
}
|
|
22
|
+
/** From, to, and optionally what was folded into it (see `foldPassThrough`): opaque to the layout, counted by renderers. */
|
|
23
|
+
type Edge<I extends Id = Id, V = unknown> = readonly [from: I, to: I, via?: readonly V[]];
|
|
24
|
+
/** A root and the graph that unfolds from it. The edges should only reach nodes below the root. */
|
|
25
|
+
interface Lane<I extends Id = Id, V = unknown> {
|
|
26
|
+
root: I;
|
|
27
|
+
edges: readonly Edge<I, V>[];
|
|
28
|
+
}
|
|
29
|
+
/** Which way the graph grows from its root: to the right (lanes stack downwards), or down (lanes sit side by side). */
|
|
30
|
+
type Direction = "right" | "down";
|
|
31
|
+
/** How a connector runs: a smooth S-curve, right angles, or a straight line. */
|
|
32
|
+
type ConnectorShape = "curve" | "elbow" | "straight";
|
|
33
|
+
interface LayoutOptions<I extends Id = Id, V = unknown> {
|
|
34
|
+
lanes: readonly Lane<I, V>[];
|
|
35
|
+
/** The roots whose graph is unfolded. A lane without edges has nothing to unfold and stays folded. */
|
|
36
|
+
open?: Iterable<I>;
|
|
37
|
+
/** The size of a lane's root. */
|
|
38
|
+
rootSize: Size | ((id: I) => Size);
|
|
39
|
+
/** The size of the nodes below it; `depth` is 1 for the root's children. */
|
|
40
|
+
nodeSize: Size | ((id: I, depth: number) => Size);
|
|
41
|
+
gap?: {
|
|
42
|
+
/** Between columns, from the root's column on. */
|
|
43
|
+
column?: number;
|
|
44
|
+
/** Between nodes in one column. */
|
|
45
|
+
row?: number;
|
|
46
|
+
/** Between lanes. */
|
|
47
|
+
lane?: number;
|
|
48
|
+
};
|
|
49
|
+
/** Room kept around an unfolded lane (its `band`), on both sides along the cross axis. */
|
|
50
|
+
padding?: number;
|
|
51
|
+
/** Where the first lane starts along the cross axis. */
|
|
52
|
+
start?: number;
|
|
53
|
+
direction?: Direction;
|
|
54
|
+
shape?: ConnectorShape;
|
|
55
|
+
/** Breaks ties between nodes that want the same row: by default the order they first appear in. */
|
|
56
|
+
compare?: (a: I, b: I) => number;
|
|
57
|
+
}
|
|
58
|
+
interface PlacedNode<I extends Id = Id> {
|
|
59
|
+
id: I;
|
|
60
|
+
/** 0 for the root. */
|
|
61
|
+
depth: number;
|
|
62
|
+
rect: Rect;
|
|
63
|
+
}
|
|
64
|
+
interface PlacedEdge<I extends Id = Id, V = unknown> {
|
|
65
|
+
from: I;
|
|
66
|
+
to: I;
|
|
67
|
+
via: readonly V[];
|
|
68
|
+
/** Where it leaves `from` (the middle of its far side) and enters `to` (the middle of its near side). */
|
|
69
|
+
start: Point;
|
|
70
|
+
end: Point;
|
|
71
|
+
/** Halfway between them: where a "+N" marker for the folded nodes goes. */
|
|
72
|
+
mid: Point;
|
|
73
|
+
/** An SVG path: `<path d={edge.path} />`. */
|
|
74
|
+
path: string;
|
|
75
|
+
}
|
|
76
|
+
interface PlacedLane<I extends Id = Id, V = unknown> {
|
|
77
|
+
root: I;
|
|
78
|
+
/** Asked to be open (see `LayoutOptions.open`). */
|
|
79
|
+
open: boolean;
|
|
80
|
+
/** Open and with something to show. */
|
|
81
|
+
unfolded: boolean;
|
|
82
|
+
/** The root first, then the nodes below it in reading order (breadth first), which is also a good tab order. */
|
|
83
|
+
nodes: PlacedNode<I>[];
|
|
84
|
+
edges: PlacedEdge<I, V>[];
|
|
85
|
+
/**
|
|
86
|
+
* The room an unfolded lane takes along the cross axis, padding included: for a background behind it. It runs the whole
|
|
87
|
+
* width of the layout along the main axis.
|
|
88
|
+
*/
|
|
89
|
+
band?: {
|
|
90
|
+
start: number;
|
|
91
|
+
size: number;
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
interface Layout<I extends Id = Id, V = unknown> {
|
|
95
|
+
lanes: PlacedLane<I, V>[];
|
|
96
|
+
/** The far edge of the furthest node, and where the next lane would start: the layout's size. */
|
|
97
|
+
width: number;
|
|
98
|
+
height: number;
|
|
99
|
+
}
|
|
100
|
+
/** Builds a connector's SVG path between two points, the graph growing in `direction`. */
|
|
101
|
+
declare function connectorPath(start: Point, end: Point, shape?: ConnectorShape, direction?: Direction): string;
|
|
102
|
+
declare function layout<I extends Id = Id, V = unknown>(options: LayoutOptions<I, V>): Layout<I, V>;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Working with the graph itself, apart from where it is drawn: the routes through a node, folding the nodes that only
|
|
106
|
+
* pass a route on, and turning nested data into lanes.
|
|
107
|
+
*/
|
|
108
|
+
interface Trace<I extends Id = Id> {
|
|
109
|
+
/** The node and everything that leads to it. */
|
|
110
|
+
up: Set<I>;
|
|
111
|
+
/** The node and everything it leads to. */
|
|
112
|
+
down: Set<I>;
|
|
113
|
+
}
|
|
114
|
+
/** Every route through `id`: what leads to it and what it leads to. For highlighting a node's routes on hover. */
|
|
115
|
+
declare function trace<I extends Id>(edges: readonly Edge<I, unknown>[], id: I): Trace<I>;
|
|
116
|
+
/** Whether an edge lies on a route through the traced node: both ends lead to it, or both come from it. */
|
|
117
|
+
declare const onTrace: <I extends Id>(t: Trace<I>, [a, b]: Edge<I, unknown>) => boolean;
|
|
118
|
+
/**
|
|
119
|
+
* Folds the nodes that only pass a route on: a node that is not the root, not kept, and has exactly one parent and one
|
|
120
|
+
* child becomes part of the edge around it, listed in its `via`. A long chain of such nodes becomes one edge with a "+N".
|
|
121
|
+
* A loop that folds back onto where it started is left out.
|
|
122
|
+
*/
|
|
123
|
+
declare function foldPassThrough<I extends Id>(edges: readonly Edge<I, unknown>[], root: I, keep: (id: I) => boolean): Edge<I, I>[];
|
|
124
|
+
/**
|
|
125
|
+
* Nested data as lanes: one per top-level item, its edges running from every node to its children. The same id twice
|
|
126
|
+
* is the same node, so shared children (a graph rather than a tree) work too.
|
|
127
|
+
*/
|
|
128
|
+
declare function fromTree<T, I extends Id>(roots: readonly T[], getId: (node: T) => I, getChildren: (node: T) => readonly T[] | null | undefined): {
|
|
129
|
+
lanes: Lane<I, never>[];
|
|
130
|
+
nodes: Map<I, T>;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
export { type ConnectorShape, type Direction, type Edge, type Id, type Lane, type Layout, type LayoutOptions, type PlacedEdge, type PlacedLane, type PlacedNode, type Point, type Rect, type Size, type Trace, connectorPath, foldPassThrough, fromTree, layout, onTrace, trace };
|
package/dist/core.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
// src/layout.ts
|
|
2
|
+
var sizeOf = (size, ...args) => typeof size === "function" ? size(...args) : size;
|
|
3
|
+
function connectorPath(start, end, shape = "curve", direction = "right") {
|
|
4
|
+
const { x: x1, y: y1 } = start;
|
|
5
|
+
const { x: x2, y: y2 } = end;
|
|
6
|
+
if (shape === "straight") return `M${x1} ${y1} L${x2} ${y2}`;
|
|
7
|
+
if (direction === "right") {
|
|
8
|
+
const mx = (x1 + x2) / 2;
|
|
9
|
+
return shape === "elbow" ? `M${x1} ${y1} H${mx} V${y2} H${x2}` : `M${x1} ${y1} C${mx} ${y1}, ${mx} ${y2}, ${x2} ${y2}`;
|
|
10
|
+
}
|
|
11
|
+
const my = (y1 + y2) / 2;
|
|
12
|
+
return shape === "elbow" ? `M${x1} ${y1} V${my} H${x2} V${y2}` : `M${x1} ${y1} C${x1} ${my}, ${x2} ${my}, ${x2} ${y2}`;
|
|
13
|
+
}
|
|
14
|
+
function layout(options) {
|
|
15
|
+
const { lanes, rootSize, nodeSize, padding = 0, start = 0, direction = "right", shape = "curve", compare } = options;
|
|
16
|
+
const columnGap = options.gap?.column ?? 0;
|
|
17
|
+
const rowGap = options.gap?.row ?? 0;
|
|
18
|
+
const laneGap = options.gap?.lane ?? 0;
|
|
19
|
+
const open = new Set(options.open ?? []);
|
|
20
|
+
const right = direction === "right";
|
|
21
|
+
const box = (size) => right ? { mainSize: size.width, crossSize: size.height } : { mainSize: size.height, crossSize: size.width };
|
|
22
|
+
const rect = (b) => right ? { x: b.main, y: b.cross, width: b.mainSize, height: b.crossSize } : { x: b.cross, y: b.main, width: b.crossSize, height: b.mainSize };
|
|
23
|
+
const point = (main, cross) => right ? { x: main, y: cross } : { x: cross, y: main };
|
|
24
|
+
const placed = [];
|
|
25
|
+
let cursor = start;
|
|
26
|
+
let far = 0;
|
|
27
|
+
for (const lane of lanes) {
|
|
28
|
+
const root = lane.root;
|
|
29
|
+
const isOpen = open.has(root);
|
|
30
|
+
const unfolded = isOpen && lane.edges.length > 0;
|
|
31
|
+
const pos = /* @__PURE__ */ new Map([[root, { main: 0, cross: cursor, ...box(sizeOf(rootSize, root)) }]]);
|
|
32
|
+
const depth = /* @__PURE__ */ new Map([[root, 0]]);
|
|
33
|
+
const order = [root];
|
|
34
|
+
let bottom = cursor + pos.get(root).crossSize;
|
|
35
|
+
far = Math.max(far, pos.get(root).mainSize);
|
|
36
|
+
if (unfolded) {
|
|
37
|
+
for (let i = 0; i < order.length; i++) {
|
|
38
|
+
for (const [a, b] of lane.edges) {
|
|
39
|
+
if (a === order[i] && !depth.has(b)) {
|
|
40
|
+
depth.set(b, depth.get(a) + 1);
|
|
41
|
+
order.push(b);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const columns = [];
|
|
46
|
+
for (const id of order.slice(1)) (columns[depth.get(id)] ??= []).push(id);
|
|
47
|
+
let mainAt = pos.get(root).mainSize + columnGap;
|
|
48
|
+
for (let c = 1; c < columns.length; c++) {
|
|
49
|
+
const ids = columns[c] ?? [];
|
|
50
|
+
const sizes = new Map(ids.map((id) => [id, box(sizeOf(nodeSize, id, c))]));
|
|
51
|
+
const width = Math.max(0, ...[...sizes.values()].map((s) => s.mainSize));
|
|
52
|
+
const want = ids.map((id, index) => {
|
|
53
|
+
const at = lane.edges.filter(([a, b]) => b === id && pos.has(a)).map(([a]) => pos.get(a).cross);
|
|
54
|
+
return { id, index, cross: at.length ? at.reduce((s, y) => s + y, 0) / at.length : cursor };
|
|
55
|
+
}).toSorted((a, b) => a.cross - b.cross || (compare ? compare(a.id, b.id) : a.index - b.index));
|
|
56
|
+
let next = -Infinity;
|
|
57
|
+
for (const w of want) {
|
|
58
|
+
const size = sizes.get(w.id);
|
|
59
|
+
const cross = Math.max(w.cross, next, cursor);
|
|
60
|
+
pos.set(w.id, { main: mainAt, cross, ...size });
|
|
61
|
+
next = cross + size.crossSize + rowGap;
|
|
62
|
+
bottom = Math.max(bottom, cross + size.crossSize);
|
|
63
|
+
}
|
|
64
|
+
far = Math.max(far, mainAt + width);
|
|
65
|
+
mainAt += width + columnGap;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const edges = [];
|
|
69
|
+
if (unfolded) {
|
|
70
|
+
for (const [a, b, via] of lane.edges) {
|
|
71
|
+
const p = pos.get(a);
|
|
72
|
+
const q = pos.get(b);
|
|
73
|
+
if (!p || !q) continue;
|
|
74
|
+
const startPoint = point(p.main + p.mainSize, p.cross + p.crossSize / 2);
|
|
75
|
+
const endPoint = point(q.main, q.cross + q.crossSize / 2);
|
|
76
|
+
edges.push({
|
|
77
|
+
from: a,
|
|
78
|
+
to: b,
|
|
79
|
+
via: via ?? [],
|
|
80
|
+
start: startPoint,
|
|
81
|
+
end: endPoint,
|
|
82
|
+
mid: { x: (startPoint.x + endPoint.x) / 2, y: (startPoint.y + endPoint.y) / 2 },
|
|
83
|
+
path: connectorPath(startPoint, endPoint, shape, direction)
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
placed.push({
|
|
88
|
+
root,
|
|
89
|
+
open: isOpen,
|
|
90
|
+
unfolded,
|
|
91
|
+
nodes: order.map((id) => ({ id, depth: depth.get(id), rect: rect(pos.get(id)) })),
|
|
92
|
+
edges,
|
|
93
|
+
...unfolded ? { band: { start: cursor - padding, size: bottom - cursor + 2 * padding } } : {}
|
|
94
|
+
});
|
|
95
|
+
cursor = bottom + (unfolded ? 2 * padding + laneGap : laneGap);
|
|
96
|
+
}
|
|
97
|
+
return right ? { lanes: placed, width: far, height: cursor } : { lanes: placed, width: cursor, height: far };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// src/graph.ts
|
|
101
|
+
function trace(edges, id) {
|
|
102
|
+
const up = /* @__PURE__ */ new Set([id]);
|
|
103
|
+
const down = /* @__PURE__ */ new Set([id]);
|
|
104
|
+
for (let changed = true; changed; ) {
|
|
105
|
+
changed = false;
|
|
106
|
+
for (const [a, b] of edges) {
|
|
107
|
+
if (up.has(b) && !up.has(a)) {
|
|
108
|
+
up.add(a);
|
|
109
|
+
changed = true;
|
|
110
|
+
}
|
|
111
|
+
if (down.has(a) && !down.has(b)) {
|
|
112
|
+
down.add(b);
|
|
113
|
+
changed = true;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return { up, down };
|
|
118
|
+
}
|
|
119
|
+
var onTrace = (t, [a, b]) => t.up.has(a) && t.up.has(b) || t.down.has(a) && t.down.has(b);
|
|
120
|
+
function foldPassThrough(edges, root, keep) {
|
|
121
|
+
const kids = /* @__PURE__ */ new Map();
|
|
122
|
+
const parents = /* @__PURE__ */ new Map();
|
|
123
|
+
for (const [a, b] of edges) {
|
|
124
|
+
kids.set(a, [...kids.get(a) ?? [], b]);
|
|
125
|
+
parents.set(b, [...parents.get(b) ?? [], a]);
|
|
126
|
+
}
|
|
127
|
+
const kept = (id) => id === root || keep(id) || kids.get(id)?.length !== 1 || parents.get(id)?.length !== 1;
|
|
128
|
+
const out = [];
|
|
129
|
+
for (const [a, b] of edges) {
|
|
130
|
+
if (!kept(a)) continue;
|
|
131
|
+
const via = [];
|
|
132
|
+
let at = b;
|
|
133
|
+
while (!kept(at) && !via.includes(at)) {
|
|
134
|
+
via.push(at);
|
|
135
|
+
at = kids.get(at)[0];
|
|
136
|
+
}
|
|
137
|
+
if (at !== a) out.push([a, at, via]);
|
|
138
|
+
}
|
|
139
|
+
return out;
|
|
140
|
+
}
|
|
141
|
+
function fromTree(roots, getId, getChildren) {
|
|
142
|
+
const nodes = /* @__PURE__ */ new Map();
|
|
143
|
+
const lanes = roots.map((root) => {
|
|
144
|
+
const edges = [];
|
|
145
|
+
const seen = /* @__PURE__ */ new Set();
|
|
146
|
+
const visited = /* @__PURE__ */ new Set();
|
|
147
|
+
const walk = (node) => {
|
|
148
|
+
const id = getId(node);
|
|
149
|
+
nodes.set(id, node);
|
|
150
|
+
if (visited.has(id)) return;
|
|
151
|
+
visited.add(id);
|
|
152
|
+
for (const child of getChildren(node) ?? []) {
|
|
153
|
+
const to = getId(child);
|
|
154
|
+
const key = `${String(id)}\0${String(to)}`;
|
|
155
|
+
if (!seen.has(key)) {
|
|
156
|
+
seen.add(key);
|
|
157
|
+
edges.push([id, to]);
|
|
158
|
+
}
|
|
159
|
+
walk(child);
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
walk(root);
|
|
163
|
+
return { root: getId(root), edges };
|
|
164
|
+
});
|
|
165
|
+
return { lanes, nodes };
|
|
166
|
+
}
|
|
167
|
+
export {
|
|
168
|
+
connectorPath,
|
|
169
|
+
foldPassThrough,
|
|
170
|
+
fromTree,
|
|
171
|
+
layout,
|
|
172
|
+
onTrace,
|
|
173
|
+
trace
|
|
174
|
+
};
|
|
175
|
+
//# sourceMappingURL=core.js.map
|
package/dist/core.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/layout.ts","../src/graph.ts"],"sourcesContent":["/**\n * The layout: where every node goes and the path of every connector, from the graph and the node sizes alone. No DOM and\n * no React, so it runs anywhere (the server included) and gives the same numbers every time.\n *\n * The shape it draws: a list of lanes, each a root you can unfold. An unfolded lane shows the graph below its root\n * beside it, in columns by depth: a node's column is its distance from the root, its row follows its parents (the average\n * of where they are), pushed further along so nothing overlaps. Lanes stack, the open ones with room around them.\n *\n * \"Main\" is the axis the graph grows along (x when it grows to the right), \"cross\" the one lanes and rows stack along.\n */\n\nexport type Id = string | number;\n\nexport interface Point {\n x: number;\n y: number;\n}\n\nexport interface Size {\n width: number;\n height: number;\n}\n\nexport interface Rect extends Point, Size {}\n\n/** From, to, and optionally what was folded into it (see `foldPassThrough`): opaque to the layout, counted by renderers. */\nexport type Edge<I extends Id = Id, V = unknown> = readonly [from: I, to: I, via?: readonly V[]];\n\n/** A root and the graph that unfolds from it. The edges should only reach nodes below the root. */\nexport interface Lane<I extends Id = Id, V = unknown> {\n root: I;\n edges: readonly Edge<I, V>[];\n}\n\n/** Which way the graph grows from its root: to the right (lanes stack downwards), or down (lanes sit side by side). */\nexport type Direction = \"right\" | \"down\";\n\n/** How a connector runs: a smooth S-curve, right angles, or a straight line. */\nexport type ConnectorShape = \"curve\" | \"elbow\" | \"straight\";\n\nexport interface LayoutOptions<I extends Id = Id, V = unknown> {\n lanes: readonly Lane<I, V>[];\n /** The roots whose graph is unfolded. A lane without edges has nothing to unfold and stays folded. */\n open?: Iterable<I>;\n /** The size of a lane's root. */\n rootSize: Size | ((id: I) => Size);\n /** The size of the nodes below it; `depth` is 1 for the root's children. */\n nodeSize: Size | ((id: I, depth: number) => Size);\n gap?: {\n /** Between columns, from the root's column on. */\n column?: number;\n /** Between nodes in one column. */\n row?: number;\n /** Between lanes. */\n lane?: number;\n };\n /** Room kept around an unfolded lane (its `band`), on both sides along the cross axis. */\n padding?: number;\n /** Where the first lane starts along the cross axis. */\n start?: number;\n direction?: Direction;\n shape?: ConnectorShape;\n /** Breaks ties between nodes that want the same row: by default the order they first appear in. */\n compare?: (a: I, b: I) => number;\n}\n\nexport interface PlacedNode<I extends Id = Id> {\n id: I;\n /** 0 for the root. */\n depth: number;\n rect: Rect;\n}\n\nexport interface PlacedEdge<I extends Id = Id, V = unknown> {\n from: I;\n to: I;\n via: readonly V[];\n /** Where it leaves `from` (the middle of its far side) and enters `to` (the middle of its near side). */\n start: Point;\n end: Point;\n /** Halfway between them: where a \"+N\" marker for the folded nodes goes. */\n mid: Point;\n /** An SVG path: `<path d={edge.path} />`. */\n path: string;\n}\n\nexport interface PlacedLane<I extends Id = Id, V = unknown> {\n root: I;\n /** Asked to be open (see `LayoutOptions.open`). */\n open: boolean;\n /** Open and with something to show. */\n unfolded: boolean;\n /** The root first, then the nodes below it in reading order (breadth first), which is also a good tab order. */\n nodes: PlacedNode<I>[];\n edges: PlacedEdge<I, V>[];\n /**\n * The room an unfolded lane takes along the cross axis, padding included: for a background behind it. It runs the whole\n * width of the layout along the main axis.\n */\n band?: { start: number; size: number };\n}\n\nexport interface Layout<I extends Id = Id, V = unknown> {\n lanes: PlacedLane<I, V>[];\n /** The far edge of the furthest node, and where the next lane would start: the layout's size. */\n width: number;\n height: number;\n}\n\n/** Positions along the two axes, before they become x and y. */\ninterface Box {\n main: number;\n cross: number;\n mainSize: number;\n crossSize: number;\n}\n\nconst sizeOf = <A extends unknown[]>(size: Size | ((...args: A) => Size), ...args: A): Size => (typeof size === \"function\" ? size(...args) : size);\n\n/** Builds a connector's SVG path between two points, the graph growing in `direction`. */\nexport function connectorPath(start: Point, end: Point, shape: ConnectorShape = \"curve\", direction: Direction = \"right\"): string {\n const { x: x1, y: y1 } = start;\n const { x: x2, y: y2 } = end;\n if (shape === \"straight\") return `M${x1} ${y1} L${x2} ${y2}`;\n if (direction === \"right\") {\n const mx = (x1 + x2) / 2;\n return shape === \"elbow\" ? `M${x1} ${y1} H${mx} V${y2} H${x2}` : `M${x1} ${y1} C${mx} ${y1}, ${mx} ${y2}, ${x2} ${y2}`;\n }\n const my = (y1 + y2) / 2;\n return shape === \"elbow\" ? `M${x1} ${y1} V${my} H${x2} V${y2}` : `M${x1} ${y1} C${x1} ${my}, ${x2} ${my}, ${x2} ${y2}`;\n}\n\nexport function layout<I extends Id = Id, V = unknown>(options: LayoutOptions<I, V>): Layout<I, V> {\n const { lanes, rootSize, nodeSize, padding = 0, start = 0, direction = \"right\", shape = \"curve\", compare } = options;\n const columnGap = options.gap?.column ?? 0;\n const rowGap = options.gap?.row ?? 0;\n const laneGap = options.gap?.lane ?? 0;\n const open = new Set(options.open ?? []);\n const right = direction === \"right\";\n const box = (size: Size): Pick<Box, \"mainSize\" | \"crossSize\"> => (right ? { mainSize: size.width, crossSize: size.height } : { mainSize: size.height, crossSize: size.width });\n const rect = (b: Box): Rect => (right ? { x: b.main, y: b.cross, width: b.mainSize, height: b.crossSize } : { x: b.cross, y: b.main, width: b.crossSize, height: b.mainSize });\n const point = (main: number, cross: number): Point => (right ? { x: main, y: cross } : { x: cross, y: main });\n\n const placed: PlacedLane<I, V>[] = [];\n let cursor = start;\n let far = 0;\n for (const lane of lanes) {\n const root = lane.root;\n const isOpen = open.has(root);\n const unfolded = isOpen && lane.edges.length > 0;\n const pos = new Map<I, Box>([[root, { main: 0, cross: cursor, ...box(sizeOf(rootSize, root)) }]]);\n const depth = new Map<I, number>([[root, 0]]);\n const order: I[] = [root];\n let bottom = cursor + pos.get(root)!.crossSize;\n far = Math.max(far, pos.get(root)!.mainSize);\n\n if (unfolded) {\n // breadth first, in the edges' order: a node's depth is its shortest distance from the root\n for (let i = 0; i < order.length; i++) {\n for (const [a, b] of lane.edges) {\n if (a === order[i] && !depth.has(b)) {\n depth.set(b, depth.get(a)! + 1);\n order.push(b);\n }\n }\n }\n const columns: I[][] = [];\n for (const id of order.slice(1)) (columns[depth.get(id)!] ??= []).push(id);\n let mainAt = pos.get(root)!.mainSize + columnGap;\n for (let c = 1; c < columns.length; c++) {\n const ids = columns[c] ?? [];\n const sizes = new Map(ids.map((id) => [id, box(sizeOf(nodeSize, id, c))]));\n const width = Math.max(0, ...[...sizes.values()].map((s) => s.mainSize));\n // each node wants to sit level with its parents (the ones already placed, in earlier columns)\n const want = ids\n .map((id, index) => {\n const at = lane.edges.filter(([a, b]) => b === id && pos.has(a)).map(([a]) => pos.get(a)!.cross);\n return { id, index, cross: at.length ? at.reduce((s, y) => s + y, 0) / at.length : cursor };\n })\n .toSorted((a, b) => a.cross - b.cross || (compare ? compare(a.id, b.id) : a.index - b.index));\n let next = -Infinity;\n for (const w of want) {\n const size = sizes.get(w.id)!;\n const cross = Math.max(w.cross, next, cursor);\n pos.set(w.id, { main: mainAt, cross, ...size });\n next = cross + size.crossSize + rowGap;\n bottom = Math.max(bottom, cross + size.crossSize);\n }\n far = Math.max(far, mainAt + width);\n mainAt += width + columnGap;\n }\n }\n\n const edges: PlacedEdge<I, V>[] = [];\n if (unfolded) {\n for (const [a, b, via] of lane.edges) {\n const p = pos.get(a);\n const q = pos.get(b);\n if (!p || !q) continue;\n const startPoint = point(p.main + p.mainSize, p.cross + p.crossSize / 2);\n const endPoint = point(q.main, q.cross + q.crossSize / 2);\n edges.push({\n from: a,\n to: b,\n via: via ?? [],\n start: startPoint,\n end: endPoint,\n mid: { x: (startPoint.x + endPoint.x) / 2, y: (startPoint.y + endPoint.y) / 2 },\n path: connectorPath(startPoint, endPoint, shape, direction),\n });\n }\n }\n\n placed.push({\n root,\n open: isOpen,\n unfolded,\n nodes: order.map((id) => ({ id, depth: depth.get(id)!, rect: rect(pos.get(id)!) })),\n edges,\n ...(unfolded ? { band: { start: cursor - padding, size: bottom - cursor + 2 * padding } } : {}),\n });\n cursor = bottom + (unfolded ? 2 * padding + laneGap : laneGap);\n }\n\n return right ? { lanes: placed, width: far, height: cursor } : { lanes: placed, width: cursor, height: far };\n}\n","import type { Edge, Id, Lane } from \"./layout\";\n\n/**\n * Working with the graph itself, apart from where it is drawn: the routes through a node, folding the nodes that only\n * pass a route on, and turning nested data into lanes.\n */\n\nexport interface Trace<I extends Id = Id> {\n /** The node and everything that leads to it. */\n up: Set<I>;\n /** The node and everything it leads to. */\n down: Set<I>;\n}\n\n/** Every route through `id`: what leads to it and what it leads to. For highlighting a node's routes on hover. */\nexport function trace<I extends Id>(edges: readonly Edge<I, unknown>[], id: I): Trace<I> {\n const up = new Set([id]);\n const down = new Set([id]);\n for (let changed = true; changed; ) {\n changed = false;\n for (const [a, b] of edges) {\n if (up.has(b) && !up.has(a)) {\n up.add(a);\n changed = true;\n }\n if (down.has(a) && !down.has(b)) {\n down.add(b);\n changed = true;\n }\n }\n }\n return { up, down };\n}\n\n/** Whether an edge lies on a route through the traced node: both ends lead to it, or both come from it. */\nexport const onTrace = <I extends Id>(t: Trace<I>, [a, b]: Edge<I, unknown>): boolean => (t.up.has(a) && t.up.has(b)) || (t.down.has(a) && t.down.has(b));\n\n/**\n * Folds the nodes that only pass a route on: a node that is not the root, not kept, and has exactly one parent and one\n * child becomes part of the edge around it, listed in its `via`. A long chain of such nodes becomes one edge with a \"+N\".\n * A loop that folds back onto where it started is left out.\n */\nexport function foldPassThrough<I extends Id>(edges: readonly Edge<I, unknown>[], root: I, keep: (id: I) => boolean): Edge<I, I>[] {\n const kids = new Map<I, I[]>();\n const parents = new Map<I, I[]>();\n for (const [a, b] of edges) {\n kids.set(a, [...(kids.get(a) ?? []), b]);\n parents.set(b, [...(parents.get(b) ?? []), a]);\n }\n const kept = (id: I) => id === root || keep(id) || kids.get(id)?.length !== 1 || parents.get(id)?.length !== 1;\n const out: Edge<I, I>[] = [];\n for (const [a, b] of edges) {\n if (!kept(a)) continue;\n const via: I[] = [];\n let at = b;\n // a cycle of pass-through nodes would never end: stop where the route returns\n while (!kept(at) && !via.includes(at)) {\n via.push(at);\n at = kids.get(at)![0]!;\n }\n // a loop folded down to where it started draws nothing\n if (at !== a) out.push([a, at, via]);\n }\n return out;\n}\n\n/**\n * Nested data as lanes: one per top-level item, its edges running from every node to its children. The same id twice\n * is the same node, so shared children (a graph rather than a tree) work too.\n */\nexport function fromTree<T, I extends Id>(roots: readonly T[], getId: (node: T) => I, getChildren: (node: T) => readonly T[] | null | undefined): { lanes: Lane<I, never>[]; nodes: Map<I, T> } {\n const nodes = new Map<I, T>();\n const lanes = roots.map((root) => {\n const edges: Edge<I, never>[] = [];\n const seen = new Set<string>();\n const visited = new Set<I>();\n const walk = (node: T) => {\n const id = getId(node);\n nodes.set(id, node);\n if (visited.has(id)) return;\n visited.add(id);\n for (const child of getChildren(node) ?? []) {\n const to = getId(child);\n const key = `${String(id)}\\u0000${String(to)}`;\n if (!seen.has(key)) {\n seen.add(key);\n edges.push([id, to]);\n }\n walk(child);\n }\n };\n walk(root);\n return { root: getId(root), edges };\n });\n return { lanes, nodes };\n}\n"],"mappings":";AAqHA,IAAM,SAAS,CAAsB,SAAwC,SAAmB,OAAO,SAAS,aAAa,KAAK,GAAG,IAAI,IAAI;AAGtI,SAAS,cAAc,OAAc,KAAY,QAAwB,SAAS,YAAuB,SAAiB;AAC/H,QAAM,EAAE,GAAG,IAAI,GAAG,GAAG,IAAI;AACzB,QAAM,EAAE,GA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