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/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Agnes (https://agnes.dev)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,171 @@
|
|
|
1
|
-
|
|
1
|
+
<h1>
|
|
2
|
+
<picture>
|
|
3
|
+
<source media="(prefers-color-scheme: dark)" srcset="./assets/logo-dark.svg">
|
|
4
|
+
<img src="./assets/logo-light.svg" alt="treelines" width="227" height="64">
|
|
5
|
+
</picture>
|
|
6
|
+
</h1>
|
|
2
7
|
|
|
3
|
-
|
|
8
|
+
Headless layout, connector lines and behaviour for trees and layered graphs in React. **You render the nodes.**
|
|
9
|
+
|
|
10
|
+
Give it nested data, or nodes and edges, and render each node as whatever element you like. treelines places them in
|
|
11
|
+
columns, draws the lines between them and handles unfolding, selection, route tracing and the keyboard. It has no styles
|
|
12
|
+
and no cards of its own: state reaches your markup as data attributes, so you style it with plain CSS or Tailwind.
|
|
13
|
+
|
|
14
|
+
- **Your elements, in the page:** ordinary DOM nodes in the document flow, not a canvas. No pan and zoom, no `foreignObject`.
|
|
15
|
+
- **Layered layout:** a node's column is its distance from the root, and its row follows its parents. Trees and
|
|
16
|
+
graphs with shared nodes both work.
|
|
17
|
+
- **Connectors:** SVG paths, curved, right-angled or straight, or drawn by you. Chains of pass-through nodes can fold into a "+N".
|
|
18
|
+
- **Behaviour:** unfold one lane at a time (or several), select, trace a node's routes on hover or focus, and move with the arrow keys.
|
|
19
|
+
- **Fixed or measured sizes:** fixed sizes lay out on the server too; measured ones adapt to whatever you render.
|
|
20
|
+
- **Small:** no runtime dependencies, React 18 and 19, MIT.
|
|
21
|
+
|
|
22
|
+
It powers the dependency graph in [Agnes](https://agnes.dev), which shows which of a repository's dependencies bring in
|
|
23
|
+
vulnerable, outdated or deprecated packages.
|
|
24
|
+
|
|
25
|
+
## Install
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
npm install treelines
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Quick start
|
|
32
|
+
|
|
33
|
+
```tsx
|
|
34
|
+
import { fromTree, Graph } from "treelines";
|
|
35
|
+
|
|
36
|
+
type Person = { id: string; name: string; reports?: Person[] };
|
|
37
|
+
|
|
38
|
+
const team: Person = { id: "ada", name: "Ada", reports: [{ id: "bo", name: "Bo" }, { id: "cy", name: "Cy" }] };
|
|
39
|
+
const { lanes, nodes } = fromTree([team], (p) => p.id, (p) => p.reports);
|
|
40
|
+
|
|
41
|
+
export function Team() {
|
|
42
|
+
return (
|
|
43
|
+
<Graph.Root lanes={lanes} rootSize={{ width: 160, height: 48 }} nodeSize={{ width: 160, height: 48 }} gap={{ column: 40, row: 12 }} defaultOpen={["ada"]}>
|
|
44
|
+
<Graph.Canvas>
|
|
45
|
+
<Graph.Connectors className="absolute inset-0 overflow-visible" pathClassName="stroke-gray-300 data-[highlighted]:stroke-black" />
|
|
46
|
+
<Graph.Nodes<string>>
|
|
47
|
+
{(node) => (
|
|
48
|
+
<Graph.Node className="rounded-lg border bg-white px-3 data-[dimmed]:opacity-40 data-[selected]:ring-2">
|
|
49
|
+
{nodes.get(node.id)!.name}
|
|
50
|
+
</Graph.Node>
|
|
51
|
+
)}
|
|
52
|
+
</Graph.Nodes>
|
|
53
|
+
</Graph.Canvas>
|
|
54
|
+
</Graph.Root>
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
The type argument on `Graph.Nodes` (and `Graph.Connectors`, `Graph.Markers`, `Graph.Bands`) is the type of your ids.
|
|
60
|
+
This code is [`examples/quick-start.tsx`](./examples/quick-start.tsx).
|
|
61
|
+
|
|
62
|
+
## How it lays things out
|
|
63
|
+
|
|
64
|
+
The data is a list of **lanes**. Each lane is a **root** and the graph that unfolds from it, given as edges
|
|
65
|
+
`[from, to]`. Lanes stack along the cross axis. A folded lane shows only its root; an unfolded lane shows its graph beside
|
|
66
|
+
it, in columns by depth.
|
|
67
|
+
|
|
68
|
+
```
|
|
69
|
+
lane "app" ──┬── core ──── util (unfolded)
|
|
70
|
+
└── ui
|
|
71
|
+
lane "lint" (folded: just its root)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
`fromTree(roots, getId, getChildren)` turns nested data into lanes: one lane per top-level item. If the same id appears
|
|
75
|
+
twice, it is one shared node, so data with shared children works too. You can also build lanes yourself, for example
|
|
76
|
+
one per direct dependency with only the routes you care about.
|
|
77
|
+
|
|
78
|
+
## API
|
|
79
|
+
|
|
80
|
+
### `<Graph.Root>`
|
|
81
|
+
|
|
82
|
+
| Prop | | |
|
|
83
|
+
|---|---|---|
|
|
84
|
+
| `lanes` | `Lane[]` | `{ root, edges }` per lane; an edge is `[from, to]` or `[from, to, via]` |
|
|
85
|
+
| `rootSize`, `nodeSize` | `Size \| (id, depth) => Size` | Leave one out to **measure** those nodes instead. Fixed sizes need no measuring and also lay out on the server. |
|
|
86
|
+
| `gap` | `{ column, row, lane }` | Pixels between columns, between nodes in a column, and between lanes |
|
|
87
|
+
| `padding` | `number` | Room around an unfolded lane (its band) |
|
|
88
|
+
| `start` | `number` | Where the first lane starts |
|
|
89
|
+
| `direction` | `"right" \| "down"` | Which way graphs grow (default `"right"`) |
|
|
90
|
+
| `shape` | `"curve" \| "elbow" \| "straight"` | The connector shape (default `"curve"`) |
|
|
91
|
+
| `compare` | `(a, b) => number` | Breaks ties between nodes that want the same row (default: first seen first) |
|
|
92
|
+
| `open`, `defaultOpen`, `onOpenChange` | `Id[]` | The unfolded lanes, by root; controlled or not |
|
|
93
|
+
| `multiple` | `boolean` | Several lanes open at once. By default, opening one closes the other. |
|
|
94
|
+
| `selected`, `defaultSelected`, `onSelectedChange` | `{ lane, id } \| null` | The selected node; controlled or not |
|
|
95
|
+
| `keyboardNavigation` | `boolean` | Arrow keys between nodes; the right arrow unfolds a folded root (default `true`) |
|
|
96
|
+
|
|
97
|
+
### Parts
|
|
98
|
+
|
|
99
|
+
- **`<Graph.Canvas>`**: the positioned area, as big as the layout. While measured sizes are still unknown it is
|
|
100
|
+
hidden and carries `data-measuring`. `className` and `style` may be functions of `{ width, height, measuring }`.
|
|
101
|
+
- **`<Graph.Connectors>`**: one SVG over the canvas with a `<path>` per connector, marked `data-highlighted` or
|
|
102
|
+
`data-dimmed`. Use `pathClassName` or `pathStyle` as functions of the edge, or render each connector yourself with
|
|
103
|
+
`{(edge) => …}`. The SVG is decorative and hidden from assistive technology.
|
|
104
|
+
- **`<Graph.Markers>`**: `{(edge) => …}` for every connector with folded nodes in it; place it at `edge.mid`.
|
|
105
|
+
- **`<Graph.Bands>`**: `{(band) => …}` for every unfolded lane: `band.start` and `band.size` along the cross axis, for
|
|
106
|
+
a background behind it.
|
|
107
|
+
- **`<Graph.Nodes>`**: `{(node) => …}` for every node in reading order (the root, then breadth first).
|
|
108
|
+
`node` has `lane`, `id`, `depth`, `rect` and `state`.
|
|
109
|
+
- **`<Graph.Node>`**: renders a `<button>` (or your own element via `render`) where the layout puts it.
|
|
110
|
+
- Clicking a root opens and closes its lane and selects it. Clicking a node below toggles its selection.
|
|
111
|
+
- Hover and focus trace its routes.
|
|
112
|
+
- Your own handlers run first; call `event.preventDefault()` to skip the graph's.
|
|
113
|
+
|
|
114
|
+
### State on your elements
|
|
115
|
+
|
|
116
|
+
| Attribute | On | Meaning |
|
|
117
|
+
|---|---|---|
|
|
118
|
+
| `data-root` | nodes | The lane's root |
|
|
119
|
+
| `data-expanded` | roots | The lane is unfolded (`aria-expanded` too, when there is something to unfold) |
|
|
120
|
+
| `data-selected` | nodes | The selected node (`aria-pressed` on nodes below a root) |
|
|
121
|
+
| `data-highlighted` | nodes, paths | On a route through the hovered or focused node, or the selected one when nothing is hovered. Matched by id, so the same node lights up wherever it appears. |
|
|
122
|
+
| `data-dimmed` | nodes, paths | Routes are being traced and this is not on one |
|
|
123
|
+
| `data-depth` | nodes | 0 for a root |
|
|
124
|
+
| `data-measuring` | canvas | Sizes are still being measured |
|
|
125
|
+
|
|
126
|
+
`className` and `style` on `<Graph.Node>` also take a function of the same state.
|
|
127
|
+
|
|
128
|
+
### Hooks
|
|
129
|
+
|
|
130
|
+
- **`useGraph()`**: `{ layout, open, selected, traced, measuring, toggleLane, select }`, for parts of your own such as a
|
|
131
|
+
details panel or a legend.
|
|
132
|
+
- **`useNode()`**: the node a `<Graph.Node>` renders, for components inside it.
|
|
133
|
+
|
|
134
|
+
### Without React: `treelines/core`
|
|
135
|
+
|
|
136
|
+
The layout alone, for the server or other frameworks:
|
|
137
|
+
|
|
138
|
+
- `layout(options)` returns rects, connector paths, marker points, bands and the overall size. Same options as
|
|
139
|
+
`<Graph.Root>`, plus `open`.
|
|
140
|
+
- `connectorPath(start, end, shape, direction)` builds one SVG path.
|
|
141
|
+
- `trace(edges, id)` and `onTrace(trace, edge)` find the routes through a node.
|
|
142
|
+
- `foldPassThrough(edges, root, keep)` folds nodes with one parent and one child into the edge around them, listed in
|
|
143
|
+
its `via`.
|
|
144
|
+
- `fromTree(roots, getId, getChildren)` turns nested data into lanes.
|
|
145
|
+
|
|
146
|
+
## Examples
|
|
147
|
+
|
|
148
|
+
In [`examples/`](./examples):
|
|
149
|
+
|
|
150
|
+
- **[Org chart](./examples/org-chart.tsx):** nested data, growing downwards, right-angled connectors.
|
|
151
|
+
- **[File tree](./examples/file-tree.tsx):** measured sizes, several folders open at once, a band behind each.
|
|
152
|
+
- **[Dependency graph](./examples/dependency-graph.tsx):** a lane per direct dependency, pass-through packages folded
|
|
153
|
+
into "+N", route tracing, and a details panel through `useGraph()`.
|
|
154
|
+
|
|
155
|
+
## Accessibility
|
|
156
|
+
|
|
157
|
+
- Nodes are buttons by default, so they are focusable and work with Enter and Space.
|
|
158
|
+
- Roots say whether they are expanded; nodes below say whether they are selected.
|
|
159
|
+
- Give each node an `aria-label` when its visible text doesn't say enough.
|
|
160
|
+
- The connectors are hidden from assistive technology: describe the relationships in your labels or a details panel.
|
|
161
|
+
- Arrow-key navigation can be turned off with `keyboardNavigation={false}` if your page already handles the keys.
|
|
162
|
+
|
|
163
|
+
## When not to use it
|
|
164
|
+
|
|
165
|
+
For editing graphs, dragging nodes, or a pan-and-zoom canvas with thousands of nodes, use
|
|
166
|
+
[React Flow](https://reactflow.dev). treelines is for read-only views inside the page where you want your own
|
|
167
|
+
markup and styling.
|
|
168
|
+
|
|
169
|
+
## Licence
|
|
170
|
+
|
|
171
|
+
MIT © [Agnes](https://agnes.dev)
|
package/dist/core.cjs
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/core.ts
|
|
21
|
+
var core_exports = {};
|
|
22
|
+
__export(core_exports, {
|
|
23
|
+
connectorPath: () => connectorPath,
|
|
24
|
+
foldPassThrough: () => foldPassThrough,
|
|
25
|
+
fromTree: () => fromTree,
|
|
26
|
+
layout: () => layout,
|
|
27
|
+
onTrace: () => onTrace,
|
|
28
|
+
trace: () => trace
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(core_exports);
|
|
31
|
+
|
|
32
|
+
// src/layout.ts
|
|
33
|
+
var sizeOf = (size, ...args) => typeof size === "function" ? size(...args) : size;
|
|
34
|
+
function connectorPath(start, end, shape = "curve", direction = "right") {
|
|
35
|
+
const { x: x1, y: y1 } = start;
|
|
36
|
+
const { x: x2, y: y2 } = end;
|
|
37
|
+
if (shape === "straight") return `M${x1} ${y1} L${x2} ${y2}`;
|
|
38
|
+
if (direction === "right") {
|
|
39
|
+
const mx = (x1 + x2) / 2;
|
|
40
|
+
return shape === "elbow" ? `M${x1} ${y1} H${mx} V${y2} H${x2}` : `M${x1} ${y1} C${mx} ${y1}, ${mx} ${y2}, ${x2} ${y2}`;
|
|
41
|
+
}
|
|
42
|
+
const my = (y1 + y2) / 2;
|
|
43
|
+
return shape === "elbow" ? `M${x1} ${y1} V${my} H${x2} V${y2}` : `M${x1} ${y1} C${x1} ${my}, ${x2} ${my}, ${x2} ${y2}`;
|
|
44
|
+
}
|
|
45
|
+
function layout(options) {
|
|
46
|
+
const { lanes, rootSize, nodeSize, padding = 0, start = 0, direction = "right", shape = "curve", compare } = options;
|
|
47
|
+
const columnGap = options.gap?.column ?? 0;
|
|
48
|
+
const rowGap = options.gap?.row ?? 0;
|
|
49
|
+
const laneGap = options.gap?.lane ?? 0;
|
|
50
|
+
const open = new Set(options.open ?? []);
|
|
51
|
+
const right = direction === "right";
|
|
52
|
+
const box = (size) => right ? { mainSize: size.width, crossSize: size.height } : { mainSize: size.height, crossSize: size.width };
|
|
53
|
+
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 };
|
|
54
|
+
const point = (main, cross) => right ? { x: main, y: cross } : { x: cross, y: main };
|
|
55
|
+
const placed = [];
|
|
56
|
+
let cursor = start;
|
|
57
|
+
let far = 0;
|
|
58
|
+
for (const lane of lanes) {
|
|
59
|
+
const root = lane.root;
|
|
60
|
+
const isOpen = open.has(root);
|
|
61
|
+
const unfolded = isOpen && lane.edges.length > 0;
|
|
62
|
+
const pos = /* @__PURE__ */ new Map([[root, { main: 0, cross: cursor, ...box(sizeOf(rootSize, root)) }]]);
|
|
63
|
+
const depth = /* @__PURE__ */ new Map([[root, 0]]);
|
|
64
|
+
const order = [root];
|
|
65
|
+
let bottom = cursor + pos.get(root).crossSize;
|
|
66
|
+
far = Math.max(far, pos.get(root).mainSize);
|
|
67
|
+
if (unfolded) {
|
|
68
|
+
for (let i = 0; i < order.length; i++) {
|
|
69
|
+
for (const [a, b] of lane.edges) {
|
|
70
|
+
if (a === order[i] && !depth.has(b)) {
|
|
71
|
+
depth.set(b, depth.get(a) + 1);
|
|
72
|
+
order.push(b);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const columns = [];
|
|
77
|
+
for (const id of order.slice(1)) (columns[depth.get(id)] ??= []).push(id);
|
|
78
|
+
let mainAt = pos.get(root).mainSize + columnGap;
|
|
79
|
+
for (let c = 1; c < columns.length; c++) {
|
|
80
|
+
const ids = columns[c] ?? [];
|
|
81
|
+
const sizes = new Map(ids.map((id) => [id, box(sizeOf(nodeSize, id, c))]));
|
|
82
|
+
const width = Math.max(0, ...[...sizes.values()].map((s) => s.mainSize));
|
|
83
|
+
const want = ids.map((id, index) => {
|
|
84
|
+
const at = lane.edges.filter(([a, b]) => b === id && pos.has(a)).map(([a]) => pos.get(a).cross);
|
|
85
|
+
return { id, index, cross: at.length ? at.reduce((s, y) => s + y, 0) / at.length : cursor };
|
|
86
|
+
}).toSorted((a, b) => a.cross - b.cross || (compare ? compare(a.id, b.id) : a.index - b.index));
|
|
87
|
+
let next = -Infinity;
|
|
88
|
+
for (const w of want) {
|
|
89
|
+
const size = sizes.get(w.id);
|
|
90
|
+
const cross = Math.max(w.cross, next, cursor);
|
|
91
|
+
pos.set(w.id, { main: mainAt, cross, ...size });
|
|
92
|
+
next = cross + size.crossSize + rowGap;
|
|
93
|
+
bottom = Math.max(bottom, cross + size.crossSize);
|
|
94
|
+
}
|
|
95
|
+
far = Math.max(far, mainAt + width);
|
|
96
|
+
mainAt += width + columnGap;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const edges = [];
|
|
100
|
+
if (unfolded) {
|
|
101
|
+
for (const [a, b, via] of lane.edges) {
|
|
102
|
+
const p = pos.get(a);
|
|
103
|
+
const q = pos.get(b);
|
|
104
|
+
if (!p || !q) continue;
|
|
105
|
+
const startPoint = point(p.main + p.mainSize, p.cross + p.crossSize / 2);
|
|
106
|
+
const endPoint = point(q.main, q.cross + q.crossSize / 2);
|
|
107
|
+
edges.push({
|
|
108
|
+
from: a,
|
|
109
|
+
to: b,
|
|
110
|
+
via: via ?? [],
|
|
111
|
+
start: startPoint,
|
|
112
|
+
end: endPoint,
|
|
113
|
+
mid: { x: (startPoint.x + endPoint.x) / 2, y: (startPoint.y + endPoint.y) / 2 },
|
|
114
|
+
path: connectorPath(startPoint, endPoint, shape, direction)
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
placed.push({
|
|
119
|
+
root,
|
|
120
|
+
open: isOpen,
|
|
121
|
+
unfolded,
|
|
122
|
+
nodes: order.map((id) => ({ id, depth: depth.get(id), rect: rect(pos.get(id)) })),
|
|
123
|
+
edges,
|
|
124
|
+
...unfolded ? { band: { start: cursor - padding, size: bottom - cursor + 2 * padding } } : {}
|
|
125
|
+
});
|
|
126
|
+
cursor = bottom + (unfolded ? 2 * padding + laneGap : laneGap);
|
|
127
|
+
}
|
|
128
|
+
return right ? { lanes: placed, width: far, height: cursor } : { lanes: placed, width: cursor, height: far };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// src/graph.ts
|
|
132
|
+
function trace(edges, id) {
|
|
133
|
+
const up = /* @__PURE__ */ new Set([id]);
|
|
134
|
+
const down = /* @__PURE__ */ new Set([id]);
|
|
135
|
+
for (let changed = true; changed; ) {
|
|
136
|
+
changed = false;
|
|
137
|
+
for (const [a, b] of edges) {
|
|
138
|
+
if (up.has(b) && !up.has(a)) {
|
|
139
|
+
up.add(a);
|
|
140
|
+
changed = true;
|
|
141
|
+
}
|
|
142
|
+
if (down.has(a) && !down.has(b)) {
|
|
143
|
+
down.add(b);
|
|
144
|
+
changed = true;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return { up, down };
|
|
149
|
+
}
|
|
150
|
+
var onTrace = (t, [a, b]) => t.up.has(a) && t.up.has(b) || t.down.has(a) && t.down.has(b);
|
|
151
|
+
function foldPassThrough(edges, root, keep) {
|
|
152
|
+
const kids = /* @__PURE__ */ new Map();
|
|
153
|
+
const parents = /* @__PURE__ */ new Map();
|
|
154
|
+
for (const [a, b] of edges) {
|
|
155
|
+
kids.set(a, [...kids.get(a) ?? [], b]);
|
|
156
|
+
parents.set(b, [...parents.get(b) ?? [], a]);
|
|
157
|
+
}
|
|
158
|
+
const kept = (id) => id === root || keep(id) || kids.get(id)?.length !== 1 || parents.get(id)?.length !== 1;
|
|
159
|
+
const out = [];
|
|
160
|
+
for (const [a, b] of edges) {
|
|
161
|
+
if (!kept(a)) continue;
|
|
162
|
+
const via = [];
|
|
163
|
+
let at = b;
|
|
164
|
+
while (!kept(at) && !via.includes(at)) {
|
|
165
|
+
via.push(at);
|
|
166
|
+
at = kids.get(at)[0];
|
|
167
|
+
}
|
|
168
|
+
if (at !== a) out.push([a, at, via]);
|
|
169
|
+
}
|
|
170
|
+
return out;
|
|
171
|
+
}
|
|
172
|
+
function fromTree(roots, getId, getChildren) {
|
|
173
|
+
const nodes = /* @__PURE__ */ new Map();
|
|
174
|
+
const lanes = roots.map((root) => {
|
|
175
|
+
const edges = [];
|
|
176
|
+
const seen = /* @__PURE__ */ new Set();
|
|
177
|
+
const visited = /* @__PURE__ */ new Set();
|
|
178
|
+
const walk = (node) => {
|
|
179
|
+
const id = getId(node);
|
|
180
|
+
nodes.set(id, node);
|
|
181
|
+
if (visited.has(id)) return;
|
|
182
|
+
visited.add(id);
|
|
183
|
+
for (const child of getChildren(node) ?? []) {
|
|
184
|
+
const to = getId(child);
|
|
185
|
+
const key = `${String(id)}\0${String(to)}`;
|
|
186
|
+
if (!seen.has(key)) {
|
|
187
|
+
seen.add(key);
|
|
188
|
+
edges.push([id, to]);
|
|
189
|
+
}
|
|
190
|
+
walk(child);
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
walk(root);
|
|
194
|
+
return { root: getId(root), edges };
|
|
195
|
+
});
|
|
196
|
+
return { lanes, nodes };
|
|
197
|
+
}
|
|
198
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
199
|
+
0 && (module.exports = {
|
|
200
|
+
connectorPath,
|
|
201
|
+
foldPassThrough,
|
|
202
|
+
fromTree,
|
|
203
|
+
layout,
|
|
204
|
+
onTrace,
|
|
205
|
+
trace
|
|
206
|
+
});
|
|
207
|
+
//# sourceMappingURL=core.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/core.ts","../src/layout.ts","../src/graph.ts"],"sourcesContent":["/**\n * treelines: the layout, the connector lines and the behaviour for trees and layered graphs. You render the nodes.\n *\n * The core, without React (also for the server): `layout` places nodes and connectors, `trace` finds the routes through a node,\n * `foldPassThrough` folds nodes that only pass a route on, `fromTree` turns nested data into lanes.\n */\nexport { connectorPath, layout } from \"./layout\";\nexport type { ConnectorShape, Direction, Edge, Id, Lane, Layout, LayoutOptions, PlacedEdge, PlacedLane, PlacedNode, Point, Rect, Size } from \"./layout\";\nexport { foldPassThrough, fromTree, onTrace, trace } from \"./graph\";\nexport type { Trace } from \"./graph\";\n","/**\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":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACqHA,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,GAAG,IAAI,GAAG,GAAG,IAAI;AACzB,MAAI,UAAU,WAAY,QAAO,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE;AAC1D,MAAI,cAAc,SAAS;AACzB,UAAM,MAAM,KAAK,MAAM;AACvB,WAAO,UAAU,UAAU,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE;AAAA,EACtH;AACA,QAAM,MAAM,KAAK,MAAM;AACvB,SAAO,UAAU,UAAU,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE;AACtH;AAEO,SAAS,OAAuC,SAA4C;AACjG,QAAM,EAAE,OAAO,UAAU,UAAU,UAAU,GAAG,QAAQ,GAAG,YAAY,SAAS,QAAQ,SAAS,QAAQ,IAAI;AAC7G,QAAM,YAAY,QAAQ,KAAK,UAAU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,aAAK,KAAK;AAAA,MACZ;AAAA,IACF;AACA,SAAK,IAAI;AACT,WAAO,EAAE,MAAM,MAAM,IAAI,GAAG,MAAM;AAAA,EACpC,CAAC;AACD,SAAO,EAAE,OAAO,MAAM;AACxB;","names":[]}
|
package/dist/core.d.cts
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 };
|