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/index.cjs
ADDED
|
@@ -0,0 +1,548 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/index.ts
|
|
22
|
+
var src_exports = {};
|
|
23
|
+
__export(src_exports, {
|
|
24
|
+
Graph: () => Graph,
|
|
25
|
+
connectorPath: () => connectorPath,
|
|
26
|
+
foldPassThrough: () => foldPassThrough,
|
|
27
|
+
fromTree: () => fromTree,
|
|
28
|
+
layout: () => layout,
|
|
29
|
+
onTrace: () => onTrace,
|
|
30
|
+
trace: () => trace,
|
|
31
|
+
useGraph: () => useGraph,
|
|
32
|
+
useNode: () => useNode
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(src_exports);
|
|
35
|
+
|
|
36
|
+
// src/layout.ts
|
|
37
|
+
var sizeOf = (size, ...args) => typeof size === "function" ? size(...args) : size;
|
|
38
|
+
function connectorPath(start, end, shape = "curve", direction = "right") {
|
|
39
|
+
const { x: x1, y: y1 } = start;
|
|
40
|
+
const { x: x2, y: y2 } = end;
|
|
41
|
+
if (shape === "straight") return `M${x1} ${y1} L${x2} ${y2}`;
|
|
42
|
+
if (direction === "right") {
|
|
43
|
+
const mx = (x1 + x2) / 2;
|
|
44
|
+
return shape === "elbow" ? `M${x1} ${y1} H${mx} V${y2} H${x2}` : `M${x1} ${y1} C${mx} ${y1}, ${mx} ${y2}, ${x2} ${y2}`;
|
|
45
|
+
}
|
|
46
|
+
const my = (y1 + y2) / 2;
|
|
47
|
+
return shape === "elbow" ? `M${x1} ${y1} V${my} H${x2} V${y2}` : `M${x1} ${y1} C${x1} ${my}, ${x2} ${my}, ${x2} ${y2}`;
|
|
48
|
+
}
|
|
49
|
+
function layout(options) {
|
|
50
|
+
const { lanes, rootSize, nodeSize, padding = 0, start = 0, direction = "right", shape = "curve", compare } = options;
|
|
51
|
+
const columnGap = options.gap?.column ?? 0;
|
|
52
|
+
const rowGap = options.gap?.row ?? 0;
|
|
53
|
+
const laneGap = options.gap?.lane ?? 0;
|
|
54
|
+
const open = new Set(options.open ?? []);
|
|
55
|
+
const right = direction === "right";
|
|
56
|
+
const box = (size) => right ? { mainSize: size.width, crossSize: size.height } : { mainSize: size.height, crossSize: size.width };
|
|
57
|
+
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 };
|
|
58
|
+
const point = (main, cross) => right ? { x: main, y: cross } : { x: cross, y: main };
|
|
59
|
+
const placed = [];
|
|
60
|
+
let cursor = start;
|
|
61
|
+
let far = 0;
|
|
62
|
+
for (const lane of lanes) {
|
|
63
|
+
const root = lane.root;
|
|
64
|
+
const isOpen = open.has(root);
|
|
65
|
+
const unfolded = isOpen && lane.edges.length > 0;
|
|
66
|
+
const pos = /* @__PURE__ */ new Map([[root, { main: 0, cross: cursor, ...box(sizeOf(rootSize, root)) }]]);
|
|
67
|
+
const depth = /* @__PURE__ */ new Map([[root, 0]]);
|
|
68
|
+
const order = [root];
|
|
69
|
+
let bottom = cursor + pos.get(root).crossSize;
|
|
70
|
+
far = Math.max(far, pos.get(root).mainSize);
|
|
71
|
+
if (unfolded) {
|
|
72
|
+
for (let i = 0; i < order.length; i++) {
|
|
73
|
+
for (const [a, b] of lane.edges) {
|
|
74
|
+
if (a === order[i] && !depth.has(b)) {
|
|
75
|
+
depth.set(b, depth.get(a) + 1);
|
|
76
|
+
order.push(b);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const columns = [];
|
|
81
|
+
for (const id of order.slice(1)) (columns[depth.get(id)] ??= []).push(id);
|
|
82
|
+
let mainAt = pos.get(root).mainSize + columnGap;
|
|
83
|
+
for (let c = 1; c < columns.length; c++) {
|
|
84
|
+
const ids = columns[c] ?? [];
|
|
85
|
+
const sizes = new Map(ids.map((id) => [id, box(sizeOf(nodeSize, id, c))]));
|
|
86
|
+
const width = Math.max(0, ...[...sizes.values()].map((s) => s.mainSize));
|
|
87
|
+
const want = ids.map((id, index) => {
|
|
88
|
+
const at = lane.edges.filter(([a, b]) => b === id && pos.has(a)).map(([a]) => pos.get(a).cross);
|
|
89
|
+
return { id, index, cross: at.length ? at.reduce((s, y) => s + y, 0) / at.length : cursor };
|
|
90
|
+
}).toSorted((a, b) => a.cross - b.cross || (compare ? compare(a.id, b.id) : a.index - b.index));
|
|
91
|
+
let next = -Infinity;
|
|
92
|
+
for (const w of want) {
|
|
93
|
+
const size = sizes.get(w.id);
|
|
94
|
+
const cross = Math.max(w.cross, next, cursor);
|
|
95
|
+
pos.set(w.id, { main: mainAt, cross, ...size });
|
|
96
|
+
next = cross + size.crossSize + rowGap;
|
|
97
|
+
bottom = Math.max(bottom, cross + size.crossSize);
|
|
98
|
+
}
|
|
99
|
+
far = Math.max(far, mainAt + width);
|
|
100
|
+
mainAt += width + columnGap;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const edges = [];
|
|
104
|
+
if (unfolded) {
|
|
105
|
+
for (const [a, b, via] of lane.edges) {
|
|
106
|
+
const p = pos.get(a);
|
|
107
|
+
const q = pos.get(b);
|
|
108
|
+
if (!p || !q) continue;
|
|
109
|
+
const startPoint = point(p.main + p.mainSize, p.cross + p.crossSize / 2);
|
|
110
|
+
const endPoint = point(q.main, q.cross + q.crossSize / 2);
|
|
111
|
+
edges.push({
|
|
112
|
+
from: a,
|
|
113
|
+
to: b,
|
|
114
|
+
via: via ?? [],
|
|
115
|
+
start: startPoint,
|
|
116
|
+
end: endPoint,
|
|
117
|
+
mid: { x: (startPoint.x + endPoint.x) / 2, y: (startPoint.y + endPoint.y) / 2 },
|
|
118
|
+
path: connectorPath(startPoint, endPoint, shape, direction)
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
placed.push({
|
|
123
|
+
root,
|
|
124
|
+
open: isOpen,
|
|
125
|
+
unfolded,
|
|
126
|
+
nodes: order.map((id) => ({ id, depth: depth.get(id), rect: rect(pos.get(id)) })),
|
|
127
|
+
edges,
|
|
128
|
+
...unfolded ? { band: { start: cursor - padding, size: bottom - cursor + 2 * padding } } : {}
|
|
129
|
+
});
|
|
130
|
+
cursor = bottom + (unfolded ? 2 * padding + laneGap : laneGap);
|
|
131
|
+
}
|
|
132
|
+
return right ? { lanes: placed, width: far, height: cursor } : { lanes: placed, width: cursor, height: far };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// src/graph.ts
|
|
136
|
+
function trace(edges, id) {
|
|
137
|
+
const up = /* @__PURE__ */ new Set([id]);
|
|
138
|
+
const down = /* @__PURE__ */ new Set([id]);
|
|
139
|
+
for (let changed = true; changed; ) {
|
|
140
|
+
changed = false;
|
|
141
|
+
for (const [a, b] of edges) {
|
|
142
|
+
if (up.has(b) && !up.has(a)) {
|
|
143
|
+
up.add(a);
|
|
144
|
+
changed = true;
|
|
145
|
+
}
|
|
146
|
+
if (down.has(a) && !down.has(b)) {
|
|
147
|
+
down.add(b);
|
|
148
|
+
changed = true;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return { up, down };
|
|
153
|
+
}
|
|
154
|
+
var onTrace = (t, [a, b]) => t.up.has(a) && t.up.has(b) || t.down.has(a) && t.down.has(b);
|
|
155
|
+
function foldPassThrough(edges, root, keep) {
|
|
156
|
+
const kids = /* @__PURE__ */ new Map();
|
|
157
|
+
const parents = /* @__PURE__ */ new Map();
|
|
158
|
+
for (const [a, b] of edges) {
|
|
159
|
+
kids.set(a, [...kids.get(a) ?? [], b]);
|
|
160
|
+
parents.set(b, [...parents.get(b) ?? [], a]);
|
|
161
|
+
}
|
|
162
|
+
const kept = (id) => id === root || keep(id) || kids.get(id)?.length !== 1 || parents.get(id)?.length !== 1;
|
|
163
|
+
const out = [];
|
|
164
|
+
for (const [a, b] of edges) {
|
|
165
|
+
if (!kept(a)) continue;
|
|
166
|
+
const via = [];
|
|
167
|
+
let at = b;
|
|
168
|
+
while (!kept(at) && !via.includes(at)) {
|
|
169
|
+
via.push(at);
|
|
170
|
+
at = kids.get(at)[0];
|
|
171
|
+
}
|
|
172
|
+
if (at !== a) out.push([a, at, via]);
|
|
173
|
+
}
|
|
174
|
+
return out;
|
|
175
|
+
}
|
|
176
|
+
function fromTree(roots, getId, getChildren) {
|
|
177
|
+
const nodes = /* @__PURE__ */ new Map();
|
|
178
|
+
const lanes = roots.map((root) => {
|
|
179
|
+
const edges = [];
|
|
180
|
+
const seen = /* @__PURE__ */ new Set();
|
|
181
|
+
const visited = /* @__PURE__ */ new Set();
|
|
182
|
+
const walk = (node) => {
|
|
183
|
+
const id = getId(node);
|
|
184
|
+
nodes.set(id, node);
|
|
185
|
+
if (visited.has(id)) return;
|
|
186
|
+
visited.add(id);
|
|
187
|
+
for (const child of getChildren(node) ?? []) {
|
|
188
|
+
const to = getId(child);
|
|
189
|
+
const key = `${String(id)}\0${String(to)}`;
|
|
190
|
+
if (!seen.has(key)) {
|
|
191
|
+
seen.add(key);
|
|
192
|
+
edges.push([id, to]);
|
|
193
|
+
}
|
|
194
|
+
walk(child);
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
walk(root);
|
|
198
|
+
return { root: getId(root), edges };
|
|
199
|
+
});
|
|
200
|
+
return { lanes, nodes };
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// src/react.tsx
|
|
204
|
+
var import_react = require("react");
|
|
205
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
206
|
+
var resolve = (value, state) => typeof value === "function" ? value(state) : value;
|
|
207
|
+
var useIsomorphicLayoutEffect = typeof window === "undefined" ? import_react.useEffect : import_react.useLayoutEffect;
|
|
208
|
+
function useControllable(value, defaultValue, onChange) {
|
|
209
|
+
const [own, setOwn] = (0, import_react.useState)(defaultValue);
|
|
210
|
+
const controlled = value !== void 0;
|
|
211
|
+
const current = controlled ? value : own;
|
|
212
|
+
const set = (0, import_react.useCallback)(
|
|
213
|
+
(next) => {
|
|
214
|
+
if (!controlled) setOwn(next);
|
|
215
|
+
onChange?.(next);
|
|
216
|
+
},
|
|
217
|
+
[controlled, onChange]
|
|
218
|
+
);
|
|
219
|
+
return [current, set];
|
|
220
|
+
}
|
|
221
|
+
var GraphContext = (0, import_react.createContext)(null);
|
|
222
|
+
var NodeContext = (0, import_react.createContext)(null);
|
|
223
|
+
function useGraphContext(part) {
|
|
224
|
+
const ctx = (0, import_react.useContext)(GraphContext);
|
|
225
|
+
if (!ctx) throw new Error(`treelines: <Graph.${part}> must be inside <Graph.Root>`);
|
|
226
|
+
return ctx;
|
|
227
|
+
}
|
|
228
|
+
var keyOf = (lane, id) => `${String(lane)}\0${String(id)}`;
|
|
229
|
+
var sizeKey = (id, root) => `${root ? "r" : "n"}\0${String(id)}`;
|
|
230
|
+
function Root(props) {
|
|
231
|
+
const { lanes, rootSize, nodeSize, gap, padding, start, direction = "right", shape, compare, multiple = false, keyboardNavigation = true, children } = props;
|
|
232
|
+
const [open, setOpen] = useControllable(props.open, props.defaultOpen ?? [], props.onOpenChange);
|
|
233
|
+
const [selected, setSelected] = useControllable(props.selected, props.defaultSelected ?? null, props.onSelectedChange);
|
|
234
|
+
const [hovered, setHovered] = (0, import_react.useState)(null);
|
|
235
|
+
const [sizes, setSizes] = (0, import_react.useState)(() => /* @__PURE__ */ new Map());
|
|
236
|
+
const measuredRoots = rootSize === void 0;
|
|
237
|
+
const measuredNodes = nodeSize === void 0;
|
|
238
|
+
const result = (0, import_react.useMemo)(
|
|
239
|
+
() => layout({
|
|
240
|
+
lanes,
|
|
241
|
+
open,
|
|
242
|
+
rootSize: rootSize ?? ((id) => sizes.get(sizeKey(id, true)) ?? { width: 0, height: 0 }),
|
|
243
|
+
nodeSize: nodeSize ?? ((id) => sizes.get(sizeKey(id, false)) ?? { width: 0, height: 0 }),
|
|
244
|
+
...gap ? { gap } : {},
|
|
245
|
+
...padding !== void 0 ? { padding } : {},
|
|
246
|
+
...start !== void 0 ? { start } : {},
|
|
247
|
+
direction,
|
|
248
|
+
...shape ? { shape } : {},
|
|
249
|
+
...compare ? { compare } : {}
|
|
250
|
+
}),
|
|
251
|
+
[lanes, open, rootSize, nodeSize, sizes, gap, padding, start, direction, shape, compare]
|
|
252
|
+
);
|
|
253
|
+
const measuring = (measuredRoots || measuredNodes) && result.lanes.some((lane) => lane.nodes.some((n) => (n.depth === 0 ? measuredRoots : measuredNodes) && !sizes.has(sizeKey(n.id, n.depth === 0))));
|
|
254
|
+
const report = (0, import_react.useCallback)((id, root, element) => {
|
|
255
|
+
if (!element) return;
|
|
256
|
+
const size = { width: element.offsetWidth, height: element.offsetHeight };
|
|
257
|
+
const key = sizeKey(id, root);
|
|
258
|
+
setSizes((current) => {
|
|
259
|
+
const known = current.get(key);
|
|
260
|
+
if (known && known.width === size.width && known.height === size.height) return current;
|
|
261
|
+
return new Map(current).set(key, size);
|
|
262
|
+
});
|
|
263
|
+
}, []);
|
|
264
|
+
const active = hovered ?? selected;
|
|
265
|
+
const traced = (0, import_react.useMemo)(() => {
|
|
266
|
+
if (!active) return null;
|
|
267
|
+
const lane = result.lanes.find((l) => l.root === active.lane);
|
|
268
|
+
if (!lane?.unfolded) return null;
|
|
269
|
+
const edges = lanes.find((l) => l.root === active.lane)?.edges ?? [];
|
|
270
|
+
return { lane: active.lane, trace: trace(edges, active.id) };
|
|
271
|
+
}, [active, result.lanes, lanes]);
|
|
272
|
+
const nodeState = (0, import_react.useCallback)(
|
|
273
|
+
(lane, id, depth) => {
|
|
274
|
+
const placed = result.lanes.find((l) => l.root === lane);
|
|
275
|
+
const root = depth === 0;
|
|
276
|
+
const expandable = root && (lanes.find((l) => l.root === lane)?.edges.length ?? 0) > 0;
|
|
277
|
+
const highlighted = !!traced && (traced.trace.up.has(id) || traced.trace.down.has(id));
|
|
278
|
+
return {
|
|
279
|
+
root,
|
|
280
|
+
depth,
|
|
281
|
+
expandable,
|
|
282
|
+
expanded: root && !!placed?.unfolded,
|
|
283
|
+
selected: !!selected && selected.lane === lane && selected.id === id,
|
|
284
|
+
highlighted,
|
|
285
|
+
dimmed: !!traced && !highlighted,
|
|
286
|
+
measuring: (root ? measuredRoots : measuredNodes) && !sizes.has(sizeKey(id, root))
|
|
287
|
+
};
|
|
288
|
+
},
|
|
289
|
+
[result.lanes, lanes, traced, selected, measuredRoots, measuredNodes, sizes]
|
|
290
|
+
);
|
|
291
|
+
const edgeState = (0, import_react.useCallback)(
|
|
292
|
+
(lane, edge) => {
|
|
293
|
+
const highlighted = !!traced && onTrace(traced.trace, [edge.from, edge.to]);
|
|
294
|
+
return { highlighted, dimmed: !!traced && !highlighted };
|
|
295
|
+
},
|
|
296
|
+
[traced]
|
|
297
|
+
);
|
|
298
|
+
const toggleLane = (0, import_react.useCallback)(
|
|
299
|
+
(root) => {
|
|
300
|
+
const expandable = (lanes.find((l) => l.root === root)?.edges.length ?? 0) > 0;
|
|
301
|
+
const isOpen = open.includes(root);
|
|
302
|
+
if (multiple) {
|
|
303
|
+
if (expandable) setOpen(isOpen ? open.filter((r) => r !== root) : [...open, root]);
|
|
304
|
+
} else {
|
|
305
|
+
setOpen(isOpen || !expandable ? [] : [root]);
|
|
306
|
+
}
|
|
307
|
+
setSelected({ lane: root, id: root });
|
|
308
|
+
setHovered(null);
|
|
309
|
+
},
|
|
310
|
+
[lanes, open, multiple, setOpen, setSelected]
|
|
311
|
+
);
|
|
312
|
+
const select = (0, import_react.useCallback)(
|
|
313
|
+
(lane, id) => setSelected(selected && selected.lane === lane && selected.id === id ? null : { lane, id }),
|
|
314
|
+
[selected, setSelected]
|
|
315
|
+
);
|
|
316
|
+
const elements = (0, import_react.useRef)(/* @__PURE__ */ new Map());
|
|
317
|
+
const register = (0, import_react.useCallback)((key, element) => {
|
|
318
|
+
if (element) elements.current.set(key, element);
|
|
319
|
+
else elements.current.delete(key);
|
|
320
|
+
}, []);
|
|
321
|
+
const focusFrom = (0, import_react.useCallback)(
|
|
322
|
+
(lane, id, key) => {
|
|
323
|
+
const placed = result.lanes.find((l) => l.root === lane);
|
|
324
|
+
const here = placed?.nodes.find((n) => n.id === id);
|
|
325
|
+
if (!placed || !here) return false;
|
|
326
|
+
const right = direction === "right";
|
|
327
|
+
const main = (r) => right ? r.x : r.y;
|
|
328
|
+
const cross = (r) => right ? r.y : r.x;
|
|
329
|
+
let target;
|
|
330
|
+
if (key === "next" || key === "previous") {
|
|
331
|
+
const sign = key === "next" ? 1 : -1;
|
|
332
|
+
if (here.depth === 0) {
|
|
333
|
+
const at = result.lanes.indexOf(placed);
|
|
334
|
+
const other = result.lanes[at + sign];
|
|
335
|
+
if (other) target = { lane: other.root, id: other.root };
|
|
336
|
+
} else {
|
|
337
|
+
const column = placed.nodes.filter((n) => n.depth === here.depth && sign * (cross(n.rect) - cross(here.rect)) > 0);
|
|
338
|
+
const nearest = column.toSorted((a, b) => sign * (cross(a.rect) - cross(b.rect)))[0];
|
|
339
|
+
if (nearest) target = { lane, id: nearest.id };
|
|
340
|
+
}
|
|
341
|
+
} else {
|
|
342
|
+
const depth = here.depth + (key === "deeper" ? 1 : -1);
|
|
343
|
+
if (key === "deeper" && here.depth === 0 && !placed.unfolded) {
|
|
344
|
+
if ((lanes.find((l) => l.root === lane)?.edges.length ?? 0) > 0) toggleLane(lane);
|
|
345
|
+
return true;
|
|
346
|
+
}
|
|
347
|
+
const column = placed.nodes.filter((n) => n.depth === depth);
|
|
348
|
+
const nearest = column.toSorted((a, b) => Math.abs(cross(a.rect) - cross(here.rect)) - Math.abs(cross(b.rect) - cross(here.rect)) || main(a.rect) - main(b.rect))[0];
|
|
349
|
+
if (nearest) target = { lane, id: nearest.id };
|
|
350
|
+
}
|
|
351
|
+
if (!target) return false;
|
|
352
|
+
elements.current.get(keyOf(target.lane, target.id))?.focus();
|
|
353
|
+
return true;
|
|
354
|
+
},
|
|
355
|
+
[result.lanes, direction, lanes, toggleLane]
|
|
356
|
+
);
|
|
357
|
+
const value = (0, import_react.useMemo)(() => ({
|
|
358
|
+
layout: result,
|
|
359
|
+
lanes,
|
|
360
|
+
direction,
|
|
361
|
+
open,
|
|
362
|
+
selected,
|
|
363
|
+
traced,
|
|
364
|
+
measuredRoots,
|
|
365
|
+
measuredNodes,
|
|
366
|
+
measuring,
|
|
367
|
+
keyboardNavigation,
|
|
368
|
+
nodeState,
|
|
369
|
+
edgeState,
|
|
370
|
+
toggleLane,
|
|
371
|
+
select,
|
|
372
|
+
hover: setHovered,
|
|
373
|
+
report,
|
|
374
|
+
register,
|
|
375
|
+
focusFrom
|
|
376
|
+
}), [result, lanes, direction, open, selected, traced, measuredRoots, measuredNodes, measuring, keyboardNavigation, nodeState, edgeState, toggleLane, select, report, register, focusFrom]);
|
|
377
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(GraphContext.Provider, { value, children });
|
|
378
|
+
}
|
|
379
|
+
function Canvas({ className, style, children, ...rest }) {
|
|
380
|
+
const ctx = useGraphContext("Canvas");
|
|
381
|
+
const state = { width: ctx.layout.width, height: ctx.layout.height, measuring: ctx.measuring };
|
|
382
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
383
|
+
"div",
|
|
384
|
+
{
|
|
385
|
+
...rest,
|
|
386
|
+
"data-measuring": ctx.measuring ? "" : void 0,
|
|
387
|
+
className: resolve(className, state),
|
|
388
|
+
style: { position: "relative", width: state.width, height: state.height, ...ctx.measuring ? { visibility: "hidden" } : {}, ...resolve(style, state) },
|
|
389
|
+
children
|
|
390
|
+
}
|
|
391
|
+
);
|
|
392
|
+
}
|
|
393
|
+
function Bands({ children }) {
|
|
394
|
+
const ctx = useGraphContext("Bands");
|
|
395
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: ctx.layout.lanes.flatMap(
|
|
396
|
+
(lane) => lane.band ? [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(BandSlot, { children: children({ lane: lane.root, start: lane.band.start, size: lane.band.size }) }, keyOf(lane.root, "band"))] : []
|
|
397
|
+
) });
|
|
398
|
+
}
|
|
399
|
+
var BandSlot = ({ children }) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children });
|
|
400
|
+
function Connectors({ pathClassName, pathStyle, children, ...rest }) {
|
|
401
|
+
const ctx = useGraphContext("Connectors");
|
|
402
|
+
const edges = useEdges();
|
|
403
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("svg", { "aria-hidden": true, width: ctx.layout.width, height: ctx.layout.height, ...rest, children: edges.map(
|
|
404
|
+
(edge) => children ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(EdgeSlot, { children: children(edge) }, keyOf(edge.lane, `${String(edge.from)}>${String(edge.to)}`)) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
405
|
+
"path",
|
|
406
|
+
{
|
|
407
|
+
d: edge.path,
|
|
408
|
+
fill: "none",
|
|
409
|
+
"data-highlighted": edge.state.highlighted ? "" : void 0,
|
|
410
|
+
"data-dimmed": edge.state.dimmed ? "" : void 0,
|
|
411
|
+
className: resolve(pathClassName, edge),
|
|
412
|
+
style: resolve(pathStyle, edge)
|
|
413
|
+
},
|
|
414
|
+
keyOf(edge.lane, `${String(edge.from)}>${String(edge.to)}`)
|
|
415
|
+
)
|
|
416
|
+
) });
|
|
417
|
+
}
|
|
418
|
+
var EdgeSlot = ({ children }) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children });
|
|
419
|
+
function useEdges() {
|
|
420
|
+
const ctx = useGraphContext("Connectors");
|
|
421
|
+
return ctx.layout.lanes.flatMap((lane) => lane.edges.map((edge) => ({ ...edge, lane: lane.root, state: ctx.edgeState(lane.root, edge) })));
|
|
422
|
+
}
|
|
423
|
+
function Markers({ children }) {
|
|
424
|
+
const edges = useEdges();
|
|
425
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: edges.filter((edge) => edge.via.length > 0).map((edge) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(EdgeSlot, { children: children(edge) }, keyOf(edge.lane, `${String(edge.from)}>${String(edge.to)}`))) });
|
|
426
|
+
}
|
|
427
|
+
function Nodes({ children }) {
|
|
428
|
+
const ctx = useGraphContext("Nodes");
|
|
429
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: ctx.layout.lanes.flatMap(
|
|
430
|
+
(lane) => lane.nodes.map((n) => {
|
|
431
|
+
const info = { lane: lane.root, id: n.id, depth: n.depth, rect: n.rect, state: ctx.nodeState(lane.root, n.id, n.depth) };
|
|
432
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(NodeSlot, { info, render: children }, keyOf(lane.root, n.id));
|
|
433
|
+
})
|
|
434
|
+
) });
|
|
435
|
+
}
|
|
436
|
+
function NodeSlot({ info, render }) {
|
|
437
|
+
const { lane, id, depth, rect, state } = info;
|
|
438
|
+
const value = (0, import_react.useMemo)(
|
|
439
|
+
() => ({ lane, id, depth, rect, state }),
|
|
440
|
+
// a new object only when something in it changed
|
|
441
|
+
// oxlint-disable-next-line react-hooks/exhaustive-deps
|
|
442
|
+
[lane, id, depth, rect.x, rect.y, rect.width, rect.height, state.root, state.expandable, state.expanded, state.selected, state.highlighted, state.dimmed, state.measuring]
|
|
443
|
+
);
|
|
444
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(NodeContext.Provider, { value, children: render(value) });
|
|
445
|
+
}
|
|
446
|
+
function Node({ render, className, style, onClick, onMouseEnter, onMouseLeave, onFocus, onBlur, onKeyDown, children, ...rest }) {
|
|
447
|
+
const ctx = useGraphContext("Node");
|
|
448
|
+
const node = (0, import_react.useContext)(NodeContext);
|
|
449
|
+
if (!node) throw new Error("treelines: <Graph.Node> must be rendered from <Graph.Nodes>");
|
|
450
|
+
const { lane, id, rect, state } = node;
|
|
451
|
+
const key = keyOf(lane, id);
|
|
452
|
+
const measured = state.root ? ctx.measuredRoots : ctx.measuredNodes;
|
|
453
|
+
const { register, report } = ctx;
|
|
454
|
+
const [element, setElement] = (0, import_react.useState)(null);
|
|
455
|
+
const ref = (0, import_react.useCallback)(
|
|
456
|
+
(el) => {
|
|
457
|
+
setElement(el);
|
|
458
|
+
register(key, el);
|
|
459
|
+
},
|
|
460
|
+
[key, register]
|
|
461
|
+
);
|
|
462
|
+
const root = state.root;
|
|
463
|
+
useIsomorphicLayoutEffect(() => {
|
|
464
|
+
if (!measured || !element) return;
|
|
465
|
+
report(id, root, element);
|
|
466
|
+
if (typeof ResizeObserver === "undefined") return;
|
|
467
|
+
const observer = new ResizeObserver(() => report(id, root, element));
|
|
468
|
+
observer.observe(element);
|
|
469
|
+
return () => observer.disconnect();
|
|
470
|
+
}, [measured, element, id, root, report]);
|
|
471
|
+
const position = measured ? { position: "absolute", left: rect.x, top: rect.y } : { position: "absolute", left: rect.x, top: rect.y, width: rect.width, height: rect.height };
|
|
472
|
+
const props = {
|
|
473
|
+
...rest,
|
|
474
|
+
"data-root": state.root ? "" : void 0,
|
|
475
|
+
"data-expanded": state.expanded ? "" : void 0,
|
|
476
|
+
"data-selected": state.selected ? "" : void 0,
|
|
477
|
+
"data-highlighted": state.highlighted ? "" : void 0,
|
|
478
|
+
"data-dimmed": state.dimmed ? "" : void 0,
|
|
479
|
+
"data-depth": String(state.depth),
|
|
480
|
+
...state.root ? { "aria-expanded": state.expandable ? state.expanded : void 0 } : { "aria-pressed": state.selected },
|
|
481
|
+
className: resolve(className, state),
|
|
482
|
+
style: { ...position, ...resolve(style, state) },
|
|
483
|
+
onClick: (event) => {
|
|
484
|
+
onClick?.(event);
|
|
485
|
+
if (event.defaultPrevented) return;
|
|
486
|
+
if (state.root) ctx.toggleLane(lane);
|
|
487
|
+
else ctx.select(lane, id);
|
|
488
|
+
},
|
|
489
|
+
onMouseEnter: (event) => {
|
|
490
|
+
onMouseEnter?.(event);
|
|
491
|
+
if (!event.defaultPrevented) ctx.hover({ lane, id });
|
|
492
|
+
},
|
|
493
|
+
onMouseLeave: (event) => {
|
|
494
|
+
onMouseLeave?.(event);
|
|
495
|
+
if (!event.defaultPrevented) ctx.hover(null);
|
|
496
|
+
},
|
|
497
|
+
onFocus: (event) => {
|
|
498
|
+
onFocus?.(event);
|
|
499
|
+
if (!event.defaultPrevented) ctx.hover({ lane, id });
|
|
500
|
+
},
|
|
501
|
+
onBlur: (event) => {
|
|
502
|
+
onBlur?.(event);
|
|
503
|
+
if (!event.defaultPrevented) ctx.hover(null);
|
|
504
|
+
},
|
|
505
|
+
onKeyDown: (event) => {
|
|
506
|
+
onKeyDown?.(event);
|
|
507
|
+
if (event.defaultPrevented || !ctx.keyboardNavigation) return;
|
|
508
|
+
const right = ctx.direction === "right";
|
|
509
|
+
const move = { ArrowDown: right ? "next" : "deeper", ArrowUp: right ? "previous" : "shallower", ArrowRight: right ? "deeper" : "next", ArrowLeft: right ? "shallower" : "previous" }[event.key];
|
|
510
|
+
if (move && ctx.focusFrom(lane, id, move)) event.preventDefault();
|
|
511
|
+
}
|
|
512
|
+
};
|
|
513
|
+
if (typeof render === "function") return render({ ...props, ref }, state);
|
|
514
|
+
if ((0, import_react.isValidElement)(render)) return (0, import_react.cloneElement)(render, { ...props, ref }, children ?? render.props.children);
|
|
515
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { type: "button", ...props, ref, children });
|
|
516
|
+
}
|
|
517
|
+
function useGraph() {
|
|
518
|
+
const ctx = useGraphContext("useGraph");
|
|
519
|
+
return {
|
|
520
|
+
layout: ctx.layout,
|
|
521
|
+
open: ctx.open,
|
|
522
|
+
selected: ctx.selected,
|
|
523
|
+
/** The routes being traced, and in which lane. */
|
|
524
|
+
traced: ctx.traced,
|
|
525
|
+
measuring: ctx.measuring,
|
|
526
|
+
toggleLane: ctx.toggleLane,
|
|
527
|
+
select: ctx.select
|
|
528
|
+
};
|
|
529
|
+
}
|
|
530
|
+
function useNode() {
|
|
531
|
+
const node = (0, import_react.useContext)(NodeContext);
|
|
532
|
+
if (!node) throw new Error("treelines: useNode() must be used inside <Graph.Nodes>");
|
|
533
|
+
return node;
|
|
534
|
+
}
|
|
535
|
+
var Graph = { Root, Canvas, Bands, Connectors, Markers, Nodes, Node };
|
|
536
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
537
|
+
0 && (module.exports = {
|
|
538
|
+
Graph,
|
|
539
|
+
connectorPath,
|
|
540
|
+
foldPassThrough,
|
|
541
|
+
fromTree,
|
|
542
|
+
layout,
|
|
543
|
+
onTrace,
|
|
544
|
+
trace,
|
|
545
|
+
useGraph,
|
|
546
|
+
useNode
|
|
547
|
+
});
|
|
548
|
+
//# sourceMappingURL=index.cjs.map
|