@dxos/react-ui-graph 0.9.1-main.c7dcc2e112 → 0.10.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.
Files changed (57) hide show
  1. package/dist/lib/browser/index.mjs +753 -15
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node-esm/index.mjs +753 -15
  5. package/dist/lib/node-esm/index.mjs.map +4 -4
  6. package/dist/lib/node-esm/meta.json +1 -1
  7. package/dist/types/src/components/Graph/Graph.stories.d.ts +2 -2
  8. package/dist/types/src/components/Graph/Graph.stories.d.ts.map +1 -1
  9. package/dist/types/src/components/SVG/SVG.d.ts +1 -1
  10. package/dist/types/src/components/SVG/SVG.d.ts.map +1 -1
  11. package/dist/types/src/components/Tree/Tree.d.ts +26 -0
  12. package/dist/types/src/components/Tree/Tree.d.ts.map +1 -0
  13. package/dist/types/src/components/Tree/index.d.ts +4 -0
  14. package/dist/types/src/components/Tree/index.d.ts.map +1 -0
  15. package/dist/types/src/components/Tree/layout/HierarchicalEdgeBundling.d.ts +40 -0
  16. package/dist/types/src/components/Tree/layout/HierarchicalEdgeBundling.d.ts.map +1 -0
  17. package/dist/types/src/components/Tree/layout/RadialTree.d.ts +37 -0
  18. package/dist/types/src/components/Tree/layout/RadialTree.d.ts.map +1 -0
  19. package/dist/types/src/components/Tree/layout/TidyTree.d.ts +26 -0
  20. package/dist/types/src/components/Tree/layout/TidyTree.d.ts.map +1 -0
  21. package/dist/types/src/components/Tree/layout/hierarchy.d.ts +17 -0
  22. package/dist/types/src/components/Tree/layout/hierarchy.d.ts.map +1 -0
  23. package/dist/types/src/components/Tree/layout/index.d.ts +7 -0
  24. package/dist/types/src/components/Tree/layout/index.d.ts.map +1 -0
  25. package/dist/types/src/components/Tree/layout/slots.d.ts +13 -0
  26. package/dist/types/src/components/Tree/layout/slots.d.ts.map +1 -0
  27. package/dist/types/src/components/Tree/layout/useContainerSize.d.ts +15 -0
  28. package/dist/types/src/components/Tree/layout/useContainerSize.d.ts.map +1 -0
  29. package/dist/types/src/components/Tree/types.d.ts +11 -0
  30. package/dist/types/src/components/Tree/types.d.ts.map +1 -0
  31. package/dist/types/src/components/index.d.ts +1 -0
  32. package/dist/types/src/components/index.d.ts.map +1 -1
  33. package/dist/types/src/fx/fx.stories.d.ts +2 -2
  34. package/dist/types/src/fx/fx.stories.d.ts.map +1 -1
  35. package/dist/types/src/graph/projector/graph-tree-projector.d.ts +40 -0
  36. package/dist/types/src/graph/projector/graph-tree-projector.d.ts.map +1 -0
  37. package/dist/types/src/graph/projector/index.d.ts +1 -0
  38. package/dist/types/src/graph/projector/index.d.ts.map +1 -1
  39. package/dist/types/tsconfig.tsbuildinfo +1 -1
  40. package/package.json +21 -22
  41. package/src/components/Graph/Graph.stories.tsx +2 -2
  42. package/src/components/SVG/SVG.tsx +1 -1
  43. package/src/components/Tree/Tree.tsx +85 -0
  44. package/src/components/Tree/index.ts +7 -0
  45. package/src/components/Tree/layout/HierarchicalEdgeBundling.tsx +332 -0
  46. package/src/components/Tree/layout/RadialTree.tsx +240 -0
  47. package/src/components/Tree/layout/TidyTree.tsx +246 -0
  48. package/src/components/Tree/layout/hierarchy.ts +32 -0
  49. package/src/components/Tree/layout/index.ts +10 -0
  50. package/src/components/Tree/layout/slots.ts +25 -0
  51. package/src/components/Tree/layout/useContainerSize.ts +43 -0
  52. package/src/components/Tree/types.ts +14 -0
  53. package/src/components/index.ts +1 -0
  54. package/src/fx/fx.stories.tsx +3 -3
  55. package/src/graph/projector/graph-bundle-projector.ts +1 -1
  56. package/src/graph/projector/graph-tree-projector.ts +251 -0
  57. package/src/graph/projector/index.ts +1 -0
@@ -0,0 +1,251 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ import { type HierarchyPointNode, tree as d3Tree, hierarchy, linkHorizontal } from 'd3';
6
+
7
+ import { type Graph } from '@dxos/graph';
8
+ import { log } from '@dxos/log';
9
+
10
+ import { type GraphLayoutEdge, type GraphLayoutNode } from '../types';
11
+ import { GraphRadialProjector, type GraphRadialProjectorOptions, updateNode } from './graph-radial-projector';
12
+
13
+ export type GraphTreeProjectorOptions = GraphRadialProjectorOptions & {
14
+ /** Id of the root node, placed at the centre. When unset, falls back to a ring. */
15
+ focus?: string;
16
+ /** Reserved space around the tree (screen pixels). Default 80. */
17
+ margin?: number;
18
+ /** Radius for non-root nodes. Default 6. */
19
+ nodeRadius?: number;
20
+ /** Radius for the root node. Defaults to double the node radius. */
21
+ rootRadius?: number;
22
+ /** Vertical spacing between adjacent rows (px). Default 28. */
23
+ rowGap?: number;
24
+ /** Maximum horizontal spacing between hops (px); the tree centres rather than stretching past this. Default 300. */
25
+ maxColumnGap?: number;
26
+ };
27
+
28
+ /** Node type tags so consumer renderers can distinguish the root, outgoing, and inbound nodes. */
29
+ export const TREE_NODE_TYPE_ROOT = 'tree-root';
30
+ /** Outgoing descendant (root is an ancestor source), on the right. */
31
+ export const TREE_NODE_TYPE_NODE = 'tree-node';
32
+ /** Inbound source (an edge points at the root), on the left. */
33
+ export const TREE_NODE_TYPE_INBOUND = 'tree-inbound';
34
+
35
+ const HIER_EDGE_PREFIX = '__tree_hier__:';
36
+
37
+ /**
38
+ * Horizontal link generator: x is the horizontal (depth) axis, y the vertical (breadth) axis.
39
+ * Our cartesian coordinates already map x→horizontal / y→vertical, so feed them straight through.
40
+ */
41
+ const linkPath = linkHorizontal<any, { x: number; y: number }>()
42
+ .x((d) => d.x)
43
+ .y((d) => d.y);
44
+
45
+ /**
46
+ * Bidirectional Reingold–Tilford "tidy tree" layout (see
47
+ * https://observablehq.com/@observablehq/plot-tree-tidy). The focus node is the root, drawn at the
48
+ * centre. Outgoing edges (the root, then each descendant, is the edge source) form a spanning tree
49
+ * fanning out to the **right**, one column per hop. Inbound edges (an edge whose target is the root)
50
+ * place their source on the **left** as a terminal leaf — the graph is not explored beyond those
51
+ * sources. Cross edges and nodes the directed view does not reach are dropped. Hierarchy links get a
52
+ * `linkHorizontal` `path` recomputed from current endpoints every tick so curves track moving nodes.
53
+ */
54
+ export class GraphTreeProjector<
55
+ NodeData = any,
56
+ Options extends GraphTreeProjectorOptions = any,
57
+ > extends GraphRadialProjector<NodeData, Options> {
58
+ // Snapshots taken right after mergeData — doTreeLayout reads from these because it overwrites
59
+ // layout.graph.edges with the synthesized hierarchy edges.
60
+ #dataNodes: GraphLayoutNode<NodeData>[] = [];
61
+ #dataEdges: GraphLayoutEdge<NodeData>[] = [];
62
+ #resizeCleanup?: () => void;
63
+
64
+ protected override async onStart(): Promise<void> {
65
+ // Re-fit and re-centre on container resize so column spacing (capped at maxColumnGap) stays
66
+ // responsive to the available width; the layout is otherwise only recomputed on data changes.
67
+ this.#resizeCleanup = this.context.resized.on(() => {
68
+ if (this.#dataNodes.length) {
69
+ this.doTreeLayout();
70
+ this.emitUpdate('topology');
71
+ this.animate();
72
+ }
73
+ });
74
+ }
75
+
76
+ protected override async onStop(): Promise<void> {
77
+ this.#resizeCleanup?.();
78
+ this.#resizeCleanup = undefined;
79
+ await super.onStop();
80
+ }
81
+
82
+ protected override onUpdate(graph?: Graph.Any) {
83
+ log('onUpdate', { graph: { nodes: graph?.nodes.length, edges: graph?.edges.length } });
84
+ this.mergeData(graph);
85
+ this.#dataNodes = [...this.layout.graph.nodes];
86
+ this.#dataEdges = [...this.layout.graph.edges];
87
+ this.doTreeLayout();
88
+ this.emitUpdate('topology');
89
+ this.animate();
90
+ }
91
+
92
+ protected override onTickFrame(_t: number): void {
93
+ for (const edge of this.layout.graph.edges) {
94
+ if (edge.type !== 'hierarchy') {
95
+ continue;
96
+ }
97
+ const { source, target } = edge;
98
+ edge.path =
99
+ linkPath({
100
+ source: { x: source.x ?? 0, y: source.y ?? 0 },
101
+ target: { x: target.x ?? 0, y: target.y ?? 0 },
102
+ }) ?? undefined;
103
+ }
104
+ }
105
+
106
+ private doTreeLayout(): void {
107
+ if (!this.context.size) {
108
+ return;
109
+ }
110
+
111
+ const dataNodes = this.#dataNodes;
112
+ if (!dataNodes.length) {
113
+ return;
114
+ }
115
+
116
+ const focusId = this.options.focus;
117
+ const focusNode = focusId ? dataNodes.find((node) => node.id === focusId) : undefined;
118
+
119
+ // No focus (or it dropped out of the graph): degrade to a plain ring so the surface is never blank.
120
+ if (!focusNode) {
121
+ this.doRadialLayout();
122
+ return;
123
+ }
124
+
125
+ type HierDatum = { id: string; node?: GraphLayoutNode<NodeData> };
126
+ const nodesById = new Map(dataNodes.map((node) => [node.id, node]));
127
+ const makeDatum = (id: string): HierDatum => ({ id, node: nodesById.get(id) });
128
+
129
+ // Directed outgoing adjacency (edge.source → edge.target), in stable edge order.
130
+ const outgoing = new Map<string, string[]>();
131
+ for (const edge of this.#dataEdges) {
132
+ const list = outgoing.get(edge.source.id) ?? [];
133
+ list.push(edge.target.id);
134
+ outgoing.set(edge.source.id, list);
135
+ }
136
+
137
+ // Right side: outgoing spanning tree from the focus; the first edge to reach a node fixes its parent.
138
+ const rightChildren = new Map<string, string[]>();
139
+ const visited = new Set<string>([focusId!]);
140
+ let frontier = [focusId!];
141
+ while (frontier.length) {
142
+ const next: string[] = [];
143
+ for (const id of frontier) {
144
+ for (const target of outgoing.get(id) ?? []) {
145
+ if (!visited.has(target)) {
146
+ visited.add(target);
147
+ const list = rightChildren.get(id) ?? [];
148
+ list.push(target);
149
+ rightChildren.set(id, list);
150
+ next.push(target);
151
+ }
152
+ }
153
+ }
154
+ frontier = next;
155
+ }
156
+
157
+ // Left side: sources of edges whose target is the focus (inbound), placed as terminal leaves — the
158
+ // graph is not explored beyond them. Skip any already reached by the outgoing tree.
159
+ const leftSources: string[] = [];
160
+ for (const edge of this.#dataEdges) {
161
+ const source = edge.source.id;
162
+ if (edge.target.id === focusId && source !== focusId && !visited.has(source)) {
163
+ visited.add(source);
164
+ leftSources.push(source);
165
+ }
166
+ }
167
+
168
+ const rightRoot = hierarchy<HierDatum>(makeDatum(focusId!), (d) => (rightChildren.get(d.id) ?? []).map(makeDatum));
169
+ const leftRoot = hierarchy<HierDatum>(makeDatum(focusId!), (d) =>
170
+ d.id === focusId ? leftSources.map(makeDatum) : [],
171
+ );
172
+
173
+ const { width } = this.context.size;
174
+ const margin = this.options.margin ?? 80;
175
+ const rowGap = this.options.rowGap ?? 28;
176
+ // The root is centred, so each side gets half the width. Cap the per-hop spacing and shrink it to
177
+ // fit the deeper side, keeping the layout responsive.
178
+ const maxColumnGap = this.options.maxColumnGap ?? 300;
179
+ const sideDepth = Math.max(1, rightRoot.height, leftRoot.height);
180
+ const columnGap = Math.min(maxColumnGap, Math.max(0, (width / 2 - margin) / sideDepth));
181
+
182
+ // nodeSize keeps row/column spacing constant: `x` is the breadth (vertical) coordinate, `y` is
183
+ // `depth * columnGap` (horizontal). `tree()` returns the same root augmented with x/y.
184
+ const layoutTree = d3Tree<HierDatum>().nodeSize([rowGap, columnGap]);
185
+ const rightPoint = layoutTree(rightRoot);
186
+ const leftPoint = layoutTree(leftRoot);
187
+
188
+ const nodeR = this.options.nodeRadius ?? 6;
189
+ const rootR = this.options.rootRadius ?? nodeR * 2;
190
+
191
+ const placed: GraphLayoutNode<NodeData>[] = [];
192
+
193
+ // Root at the centre (origin).
194
+ updateNode(focusNode, [0, 0], rootR);
195
+ focusNode.type = TREE_NODE_TYPE_ROOT;
196
+ placed.push(focusNode);
197
+
198
+ // Right side: depth → +x; breadth shifted so the root's breadth maps to the vertical centre.
199
+ const rightBreadth = rightPoint.x;
200
+ rightPoint.each((d) => {
201
+ if (d.data.id === focusId || !d.data.node) {
202
+ return;
203
+ }
204
+ updateNode(d.data.node, [d.y, d.x - rightBreadth], nodeR);
205
+ d.data.node.type = TREE_NODE_TYPE_NODE;
206
+ placed.push(d.data.node);
207
+ });
208
+
209
+ // Left side: depth → -x (mirrored); breadth shifted so the root maps to the vertical centre.
210
+ const leftBreadth = leftPoint.x;
211
+ leftPoint.each((d) => {
212
+ if (d.data.id === focusId || !d.data.node) {
213
+ return;
214
+ }
215
+ updateNode(d.data.node, [-d.y, d.x - leftBreadth], nodeR);
216
+ d.data.node.type = TREE_NODE_TYPE_INBOUND;
217
+ placed.push(d.data.node);
218
+ });
219
+
220
+ // Only render placed nodes — neighbourhood nodes the directed view doesn't reach are dropped.
221
+ this.layout.graph.nodes = placed;
222
+
223
+ // Synthesize hierarchy edges for both sides; paths are filled by onTickFrame from current positions.
224
+ // Left-side links are reversed so the rendered edge keeps the real inbound direction (source → focus)
225
+ // rather than the layout's parent → child (focus → source) orientation.
226
+ const edges: GraphLayoutEdge<NodeData>[] = [];
227
+ const addLinks = (root: HierarchyPointNode<HierDatum>, reverse = false) => {
228
+ root.links().forEach((link) => {
229
+ const parent = link.source.data;
230
+ const child = link.target.data;
231
+ const [from, to] = reverse ? [child, parent] : [parent, child];
232
+ if (!from.node || !to.node) {
233
+ return;
234
+ }
235
+ edges.push({
236
+ id: `${HIER_EDGE_PREFIX}${from.id}->${to.id}`,
237
+ type: 'hierarchy',
238
+ source: from.node,
239
+ target: to.node,
240
+ data: undefined,
241
+ });
242
+ });
243
+ };
244
+ addLinks(rightPoint);
245
+ addLinks(leftPoint, true);
246
+ this.layout.graph.edges = edges;
247
+
248
+ // Seed initial paths so the topology emit has correct geometry to enter into the DOM.
249
+ this.onTickFrame(0);
250
+ }
251
+ }
@@ -13,4 +13,5 @@ export * from './graph-plexus-projector';
13
13
  export * from './graph-relational-projector';
14
14
  export * from './graph-radial-projector';
15
15
  export * from './graph-swarm-projector';
16
+ export * from './graph-tree-projector';
16
17
  export * from './projector';