@dxos/react-ui-graph 0.8.4-main.cb12b3f963 → 0.8.4-main.d05539e30a
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 +102 -5
- package/dist/lib/browser/index.mjs +903 -285
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +903 -285
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/components/Graph/Graph.d.ts +6 -2
- package/dist/types/src/components/Graph/Graph.d.ts.map +1 -1
- package/dist/types/src/components/Graph/Graph.stories.d.ts +3 -3
- package/dist/types/src/components/Graph/Graph.stories.d.ts.map +1 -1
- package/dist/types/src/components/Mesh/Mesh.d.ts.map +1 -1
- package/dist/types/src/components/Mesh/Mesh.stories.d.ts.map +1 -1
- package/dist/types/src/components/SVG/Root.d.ts +4 -0
- package/dist/types/src/components/SVG/Root.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-bundle-projector.d.ts +47 -0
- package/dist/types/src/graph/projector/graph-bundle-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-cluster-projector.d.ts +60 -0
- package/dist/types/src/graph/projector/graph-cluster-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-force-projector.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-hierarchical-projector.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-lattice-projector.d.ts +23 -0
- package/dist/types/src/graph/projector/graph-lattice-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-projector.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-radial-projector.d.ts +6 -0
- package/dist/types/src/graph/projector/graph-radial-projector.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-relational-projector.d.ts.map +1 -1
- package/dist/types/src/graph/projector/index.d.ts +3 -0
- package/dist/types/src/graph/projector/index.d.ts.map +1 -1
- package/dist/types/src/graph/projector/projector.d.ts +10 -1
- package/dist/types/src/graph/projector/projector.d.ts.map +1 -1
- package/dist/types/src/graph/renderer/graph-renderer.d.ts +21 -1
- package/dist/types/src/graph/renderer/graph-renderer.d.ts.map +1 -1
- package/dist/types/src/graph/renderer/renderer.d.ts +12 -1
- package/dist/types/src/graph/renderer/renderer.d.ts.map +1 -1
- package/dist/types/src/graph/types.d.ts +14 -0
- package/dist/types/src/graph/types.d.ts.map +1 -1
- package/dist/types/src/stories/hooks.stories.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +18 -18
- package/src/components/Graph/Graph.stories.tsx +5 -5
- package/src/components/Graph/Graph.tsx +48 -21
- package/src/components/Mesh/Mesh.stories.tsx +6 -4
- package/src/components/Mesh/Mesh.tsx +43 -35
- package/src/components/SVG/Root.tsx +28 -3
- package/src/graph/projector/graph-bundle-projector.ts +210 -0
- package/src/graph/projector/graph-cluster-projector.ts +325 -0
- package/src/graph/projector/graph-force-projector.ts +5 -1
- package/src/graph/projector/graph-hierarchical-projector.ts +1 -0
- package/src/graph/projector/graph-lattice-projector.ts +86 -0
- package/src/graph/projector/graph-projector.ts +7 -0
- package/src/graph/projector/graph-radial-projector.ts +30 -2
- package/src/graph/projector/graph-relational-projector.ts +1 -0
- package/src/graph/projector/index.ts +3 -0
- package/src/graph/projector/projector.ts +12 -3
- package/src/graph/renderer/graph-renderer.ts +172 -32
- package/src/graph/renderer/renderer.ts +15 -4
- package/src/graph/types.ts +14 -0
- package/src/stories/hooks.stories.tsx +6 -1
- package/styles/graph.css +51 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/react-ui-graph",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.d05539e30a",
|
|
4
4
|
"description": "GEM Graph.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"type": "git",
|
|
9
9
|
"url": "https://github.com/dxos/dxos"
|
|
10
10
|
},
|
|
11
|
-
"license": "
|
|
11
|
+
"license": "FSL-1.1-Apache-2.0",
|
|
12
12
|
"author": "DXOS.org",
|
|
13
13
|
"sideEffects": [
|
|
14
14
|
"*.css"
|
|
@@ -44,13 +44,13 @@
|
|
|
44
44
|
"lodash.defaultsdeep": "^4.6.1",
|
|
45
45
|
"react-resize-detector": "^11.0.1",
|
|
46
46
|
"topojson-client": "^3.1.0",
|
|
47
|
-
"@dxos/debug": "0.8.4-main.
|
|
48
|
-
"@dxos/
|
|
49
|
-
"@dxos/
|
|
50
|
-
"@dxos/
|
|
51
|
-
"@dxos/
|
|
52
|
-
"@dxos/
|
|
53
|
-
"@dxos/
|
|
47
|
+
"@dxos/debug": "0.8.4-main.d05539e30a",
|
|
48
|
+
"@dxos/async": "0.8.4-main.d05539e30a",
|
|
49
|
+
"@dxos/log": "0.8.4-main.d05539e30a",
|
|
50
|
+
"@dxos/graph": "0.8.4-main.d05539e30a",
|
|
51
|
+
"@dxos/node-std": "0.8.4-main.d05539e30a",
|
|
52
|
+
"@dxos/invariant": "0.8.4-main.d05539e30a",
|
|
53
|
+
"@dxos/react-ui-mosaic": "0.8.4-main.d05539e30a"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
56
|
"@babel/core": "^7.18.13",
|
|
@@ -63,21 +63,21 @@
|
|
|
63
63
|
"postcss": "^8.5.12",
|
|
64
64
|
"react": "~19.2.3",
|
|
65
65
|
"react-dom": "~19.2.3",
|
|
66
|
-
"@dxos/random": "0.8.4-main.
|
|
67
|
-
"@dxos/react-ui": "0.8.4-main.
|
|
68
|
-
"@dxos/react-ui-stack": "0.8.4-main.
|
|
69
|
-
"@dxos/react-ui-syntax-highlighter": "0.8.4-main.
|
|
70
|
-
"@dxos/
|
|
71
|
-
"@dxos/
|
|
66
|
+
"@dxos/random": "0.8.4-main.d05539e30a",
|
|
67
|
+
"@dxos/react-ui": "0.8.4-main.d05539e30a",
|
|
68
|
+
"@dxos/react-ui-stack": "0.8.4-main.d05539e30a",
|
|
69
|
+
"@dxos/react-ui-syntax-highlighter": "0.8.4-main.d05539e30a",
|
|
70
|
+
"@dxos/storybook-utils": "0.8.4-main.d05539e30a",
|
|
71
|
+
"@dxos/ui-theme": "0.8.4-main.d05539e30a"
|
|
72
72
|
},
|
|
73
73
|
"peerDependencies": {
|
|
74
74
|
"react": "~19.2.3",
|
|
75
75
|
"react-dom": "~19.2.3",
|
|
76
|
-
"@dxos/react-ui": "0.8.4-main.
|
|
77
|
-
"@dxos/ui-theme": "0.8.4-main.
|
|
76
|
+
"@dxos/react-ui": "0.8.4-main.d05539e30a",
|
|
77
|
+
"@dxos/ui-theme": "0.8.4-main.d05539e30a"
|
|
78
78
|
},
|
|
79
79
|
"optionalDependencies": {
|
|
80
|
-
"@dxos/random": "0.8.4-main.
|
|
80
|
+
"@dxos/random": "0.8.4-main.d05539e30a"
|
|
81
81
|
},
|
|
82
82
|
"publishConfig": {
|
|
83
83
|
"access": "public"
|
|
@@ -9,7 +9,7 @@ import { type Meta, type StoryObj } from '@storybook/react-vite';
|
|
|
9
9
|
import { select } from 'd3';
|
|
10
10
|
import React, { useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
|
|
11
11
|
|
|
12
|
-
import { type Graph, type GraphModel, SelectionModel } from '@dxos/graph';
|
|
12
|
+
import { type Graph, type GraphModel, type SelectionMode, SelectionModel } from '@dxos/graph';
|
|
13
13
|
import { IconButton, Popover, Toolbar } from '@dxos/react-ui';
|
|
14
14
|
import { Card } from '@dxos/react-ui';
|
|
15
15
|
import { JsonHighlighter, Syntax } from '@dxos/react-ui-syntax-highlighter';
|
|
@@ -53,8 +53,8 @@ type DefaultStoryProps = GraphProps & {
|
|
|
53
53
|
debug?: boolean;
|
|
54
54
|
grid?: boolean | SVGGridProps;
|
|
55
55
|
inspect?: boolean;
|
|
56
|
-
singleSelect?: boolean;
|
|
57
56
|
graph: () => Graph.Any;
|
|
57
|
+
selectionMode?: SelectionMode;
|
|
58
58
|
projectorType?: ProjectorType;
|
|
59
59
|
projectorOptions?:
|
|
60
60
|
| GraphForceProjectorOptions
|
|
@@ -67,7 +67,7 @@ const DefaultStory = ({
|
|
|
67
67
|
debug,
|
|
68
68
|
grid,
|
|
69
69
|
inspect,
|
|
70
|
-
|
|
70
|
+
selectionMode,
|
|
71
71
|
graph: _graph,
|
|
72
72
|
projectorType: _projectorType = 'force',
|
|
73
73
|
projectorOptions,
|
|
@@ -78,11 +78,11 @@ const DefaultStory = ({
|
|
|
78
78
|
const registry = useContext(RegistryContext);
|
|
79
79
|
|
|
80
80
|
// Models.
|
|
81
|
+
const selection = useMemo(() => new SelectionModel({ mode: selectionMode }), [selectionMode]);
|
|
81
82
|
const [model, setModel] = useState<GraphModel.ReactiveGraphModel | undefined>(() => {
|
|
82
83
|
const graph = _graph?.();
|
|
83
84
|
return graph ? new TestGraphModel(registry, graph) : undefined;
|
|
84
85
|
});
|
|
85
|
-
const selection = useMemo(() => new SelectionModel(singleSelect), [singleSelect]);
|
|
86
86
|
|
|
87
87
|
// Projector.
|
|
88
88
|
const [projectorType, setProjectorType] = useState<ProjectorType>(_projectorType);
|
|
@@ -359,7 +359,7 @@ export const Projector: Story = {
|
|
|
359
359
|
debug: true,
|
|
360
360
|
drag: true,
|
|
361
361
|
arrows: true,
|
|
362
|
-
|
|
362
|
+
selectionMode: 'single',
|
|
363
363
|
projectorType: 'hierarchical',
|
|
364
364
|
projectorOptions: {
|
|
365
365
|
duration: 500,
|
|
@@ -27,14 +27,18 @@ export type GraphController = {
|
|
|
27
27
|
};
|
|
28
28
|
|
|
29
29
|
export type GraphProps<Node extends Graph$.Node.Any = any, Edge extends Graph$.Edge.Any = any> = ThemedClassName<
|
|
30
|
-
Pick<GraphRendererOptions<Node>, 'labels' | 'subgraphs' | 'attributes'> & {
|
|
30
|
+
Pick<GraphRendererOptions<Node>, 'labels' | 'subgraphs' | 'attributes' | 'renderNode'> & {
|
|
31
31
|
model?: GraphModel.ReactiveGraphModel<Node, Edge>;
|
|
32
32
|
projector?: GraphProjector<Node>;
|
|
33
33
|
renderer?: GraphRenderer<Node>;
|
|
34
34
|
drag?: boolean;
|
|
35
35
|
arrows?: boolean;
|
|
36
36
|
onSelect?: (node: GraphLayoutNode<Node>, event: MouseEvent) => void;
|
|
37
|
-
|
|
37
|
+
/**
|
|
38
|
+
* Fires on pointerenter with the hovered node, and on pointerleave with `null`.
|
|
39
|
+
* Use the `null` signal to clear hover-driven previews / highlights.
|
|
40
|
+
*/
|
|
41
|
+
onInspect?: (node: GraphLayoutNode<Node> | null, event: MouseEvent) => void;
|
|
38
42
|
}
|
|
39
43
|
>;
|
|
40
44
|
|
|
@@ -54,8 +58,6 @@ const GraphInner = <Node extends Graph$.Node.Any = any, Edge extends Graph$.Edge
|
|
|
54
58
|
) => {
|
|
55
59
|
const context = useSvgContext();
|
|
56
60
|
const graphRef = useRef<SVGGElement>(null);
|
|
57
|
-
// TODO(wittjosiah): This doesn't work for some reason.
|
|
58
|
-
// const graph = useAtomValue(model?.graphAtom ?? EMPTY_ATOM);
|
|
59
61
|
|
|
60
62
|
const { projector, renderer } = useMemo(() => {
|
|
61
63
|
let projector = projectorProp;
|
|
@@ -70,13 +72,30 @@ const GraphInner = <Node extends Graph$.Node.Any = any, Edge extends Graph$.Edge
|
|
|
70
72
|
// TODO(burdon): Replace drag when projector is updated.
|
|
71
73
|
drag: drag ? createGraphDrag(context, projector) : undefined,
|
|
72
74
|
arrows: { end: arrows },
|
|
73
|
-
onNodeClick: onSelect
|
|
74
|
-
onNodePointerEnter: onInspect ? (node
|
|
75
|
+
onNodeClick: onSelect,
|
|
76
|
+
onNodePointerEnter: onInspect ? (node, event) => onInspect(node, event) : undefined,
|
|
77
|
+
// Pair pointerenter with leave so consumers can clear hover-driven previews.
|
|
78
|
+
// The callback receives `null` on leave (event still carries the source).
|
|
79
|
+
onNodePointerLeave: onInspect ? (_node, event) => onInspect(null, event) : undefined,
|
|
75
80
|
});
|
|
76
81
|
}
|
|
77
82
|
|
|
78
83
|
return { projector, renderer };
|
|
79
|
-
|
|
84
|
+
// Renderer options are baked into the instance — re-memo when any of them change
|
|
85
|
+
// so a swapped `renderNode` / handler is actually applied.
|
|
86
|
+
}, [
|
|
87
|
+
context,
|
|
88
|
+
projectorProp,
|
|
89
|
+
rendererProp,
|
|
90
|
+
drag,
|
|
91
|
+
arrows,
|
|
92
|
+
onSelect,
|
|
93
|
+
onInspect,
|
|
94
|
+
props.labels,
|
|
95
|
+
props.subgraphs,
|
|
96
|
+
props.attributes,
|
|
97
|
+
props.renderNode,
|
|
98
|
+
]);
|
|
80
99
|
|
|
81
100
|
// External API.
|
|
82
101
|
useImperativeHandle(
|
|
@@ -97,26 +116,34 @@ const GraphInner = <Node extends Graph$.Node.Any = any, Edge extends Graph$.Edge
|
|
|
97
116
|
|
|
98
117
|
// Subscriptions.
|
|
99
118
|
useEffect(() => {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
projector.updated.on(({ layout }) => {
|
|
119
|
+
// Wire the renderer listener BEFORE calling updateData so the synchronous
|
|
120
|
+
// 'topology' emit (fired from inside onUpdate) reaches the renderer.
|
|
121
|
+
const cleanup = combine(
|
|
122
|
+
// Subscribe to model changes if reactive model.
|
|
123
|
+
model
|
|
124
|
+
? model.subscribe(() => {
|
|
125
|
+
projector.updateData(model?.graph);
|
|
126
|
+
})
|
|
127
|
+
: undefined,
|
|
128
|
+
|
|
129
|
+
projector.updated.on(({ layout, kind }) => {
|
|
112
130
|
try {
|
|
113
|
-
|
|
131
|
+
// 'positions' is the per-tick fast path; 'topology' (and unset, for back-compat) is the full render.
|
|
132
|
+
if (kind === 'positions') {
|
|
133
|
+
renderer.applyPositions(layout);
|
|
134
|
+
} else {
|
|
135
|
+
renderer.render(layout);
|
|
136
|
+
}
|
|
114
137
|
} catch (error) {
|
|
115
|
-
void projector.stop();
|
|
116
138
|
log.catch(error);
|
|
139
|
+
void projector.stop();
|
|
117
140
|
}
|
|
118
141
|
}),
|
|
119
142
|
);
|
|
143
|
+
|
|
144
|
+
projector.updateData(model?.graph);
|
|
145
|
+
|
|
146
|
+
return cleanup;
|
|
120
147
|
}, [model, projector, renderer]);
|
|
121
148
|
|
|
122
149
|
// Start.
|
|
@@ -46,18 +46,20 @@ export default meta;
|
|
|
46
46
|
|
|
47
47
|
type Story = StoryObj<typeof meta>;
|
|
48
48
|
|
|
49
|
+
// Selectors use the descendant combinator (`[&_X]`) — the mesh / hexagon / border elements
|
|
50
|
+
// live inside the SVG inside the div, so direct-child (`>`) selectors don't match.
|
|
49
51
|
export const Default: Story = {
|
|
50
52
|
args: {
|
|
51
53
|
classNames: [
|
|
52
|
-
'[
|
|
53
|
-
'[
|
|
54
|
-
'[
|
|
54
|
+
'[&_.mesh]:fill-none',
|
|
55
|
+
'[&_.hexagon>path.fill]:fill-blue-800 [&_.hexagon]:stroke-blue-700',
|
|
56
|
+
'[&_.border]:fill-none [&_.border]:stroke-blue-500 [&_.border]:stroke-2',
|
|
55
57
|
],
|
|
56
58
|
},
|
|
57
59
|
};
|
|
58
60
|
|
|
59
61
|
export const Outline: Story = {
|
|
60
62
|
args: {
|
|
61
|
-
classNames: ['[
|
|
63
|
+
classNames: ['[&_.mesh]:fill-none', '[&_.border]:fill-none [&_.border]:stroke-red-500 [&_.border]:stroke-2'],
|
|
62
64
|
},
|
|
63
65
|
};
|
|
@@ -20,43 +20,51 @@ export const Mesh = ({ radius = 16, value = 0.5 }: MeshProps) => {
|
|
|
20
20
|
const { svg, size } = useSvgContext();
|
|
21
21
|
|
|
22
22
|
useEffect(() => {
|
|
23
|
-
if (size) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
23
|
+
if (!size) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const topology = hexTopology(size, radius, value);
|
|
28
|
+
const projection = hexProjection(radius);
|
|
29
|
+
const path = geoPath().projection(projection);
|
|
30
|
+
|
|
31
|
+
// Replace the previous render: idempotent join on a single keyed root group, then
|
|
32
|
+
// recreate the inner layers. Without this the effect re-runs (e.g. animating `value`
|
|
33
|
+
// or on resize) appended new groups on top of the old ones instead of replacing them.
|
|
34
|
+
const root = select(svg)
|
|
35
|
+
.selectAll<SVGGElement, null>('g.dx-mesh-root')
|
|
36
|
+
.data([null])
|
|
37
|
+
.join('g')
|
|
38
|
+
.classed('dx-mesh-root', true);
|
|
39
|
+
root.selectAll('*').remove();
|
|
40
|
+
|
|
41
|
+
root
|
|
42
|
+
.append('g')
|
|
43
|
+
.attr('class', 'hexagon')
|
|
44
|
+
.selectAll('path')
|
|
45
|
+
.data(topology.objects.hexagons.geometries)
|
|
46
|
+
.enter()
|
|
47
|
+
.append('path')
|
|
48
|
+
.attr('d', (d) => path(feature(topology, d)))
|
|
49
|
+
.attr('class', (d) => ((d.properties as Custom)?.fill ? 'fill' : null));
|
|
50
|
+
|
|
51
|
+
root.append('path').datum(mesh(topology, topology.objects.hexagons)).attr('class', 'mesh').attr('d', path);
|
|
52
|
+
|
|
53
|
+
const redraw = (border: any) => {
|
|
54
|
+
border.attr(
|
|
55
|
+
'd',
|
|
56
|
+
path(
|
|
57
|
+
mesh(
|
|
58
|
+
topology,
|
|
59
|
+
topology.objects.hexagons,
|
|
60
|
+
(a, b) => (a.properties as Custom).fill !== (b.properties as Custom).fill,
|
|
53
61
|
),
|
|
54
|
-
)
|
|
55
|
-
|
|
62
|
+
),
|
|
63
|
+
);
|
|
64
|
+
};
|
|
56
65
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}, [svg, size, value]);
|
|
66
|
+
root.append('path').attr('class', 'border').call(redraw);
|
|
67
|
+
}, [svg, size, value, radius]);
|
|
60
68
|
|
|
61
69
|
return null;
|
|
62
70
|
};
|
|
@@ -10,8 +10,8 @@ import React, {
|
|
|
10
10
|
useEffect,
|
|
11
11
|
useImperativeHandle,
|
|
12
12
|
useMemo,
|
|
13
|
+
useState,
|
|
13
14
|
} from 'react';
|
|
14
|
-
import { useResizeDetector } from 'react-resize-detector';
|
|
15
15
|
|
|
16
16
|
import { type ThemedClassName } from '@dxos/react-ui';
|
|
17
17
|
import { composableProps, mx } from '@dxos/ui-theme';
|
|
@@ -23,9 +23,34 @@ export type RootProps = ThemedClassName<PropsWithChildren<SVGContextOptions & Co
|
|
|
23
23
|
/**
|
|
24
24
|
* Makes the SVG context available to child nodes.
|
|
25
25
|
* Automatically resizes the SVG element, which expands to fit the container.
|
|
26
|
+
*
|
|
27
|
+
* NOTE: Uses `ResizeObserver` directly rather than `react-resize-detector` — the latter's
|
|
28
|
+
* `useResizeDetector` was flaky in our Storybook environment (sometimes never fired its
|
|
29
|
+
* initial measurement), leaving the SVG stuck at `opacity-0` with no rendered content.
|
|
26
30
|
*/
|
|
27
31
|
export const Root = forwardRef<SVGContext, RootProps>(({ children, scale, centered, ...props }, ref) => {
|
|
28
|
-
const
|
|
32
|
+
const [container, setContainer] = useState<HTMLDivElement | null>(null);
|
|
33
|
+
const [size, setSize] = useState<{ width: number; height: number }>({ width: 0, height: 0 });
|
|
34
|
+
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
if (!container) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const rect = container.getBoundingClientRect();
|
|
40
|
+
setSize({ width: rect.width, height: rect.height });
|
|
41
|
+
const observer = new ResizeObserver((entries) => {
|
|
42
|
+
const entry = entries[0];
|
|
43
|
+
if (!entry) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const { width, height } = entry.contentRect;
|
|
47
|
+
setSize((prev) => (prev.width === width && prev.height === height ? prev : { width, height }));
|
|
48
|
+
});
|
|
49
|
+
observer.observe(container);
|
|
50
|
+
return () => observer.disconnect();
|
|
51
|
+
}, [container]);
|
|
52
|
+
|
|
53
|
+
const { width, height } = size;
|
|
29
54
|
|
|
30
55
|
const context = useMemo<SVGContext>(() => {
|
|
31
56
|
return new SVGContext({ scale, centered });
|
|
@@ -48,7 +73,7 @@ export const Root = forwardRef<SVGContext, RootProps>(({ children, scale, center
|
|
|
48
73
|
|
|
49
74
|
return (
|
|
50
75
|
<SVGContextProvider value={context}>
|
|
51
|
-
<div {...composableProps(props, { classNames: 'dx-expander' })} ref={
|
|
76
|
+
<div {...composableProps(props, { classNames: 'dx-expander' })} ref={setContainer}>
|
|
52
77
|
<svg
|
|
53
78
|
xmlns='http://www.w3.org/2000/svg'
|
|
54
79
|
ref={context.ref}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type HierarchyNode, cluster as d3Cluster, curveBundle, hierarchy, lineRadial } 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 GraphBundleProjectorOptions = GraphRadialProjectorOptions & {
|
|
14
|
+
/** Reserved space around the bundle (screen pixels). */
|
|
15
|
+
margin?: number;
|
|
16
|
+
/** Radius for leaf nodes. Default 4. */
|
|
17
|
+
leafRadius?: number;
|
|
18
|
+
/**
|
|
19
|
+
* Group key for each node — synthetic intermediate nodes are created per unique key
|
|
20
|
+
* to give the bundling layout a routing point (lowest common ancestor) for edges
|
|
21
|
+
* between leaves in different groups. Return `undefined` to attach the node directly
|
|
22
|
+
* to the root (all such leaves bundle through the root).
|
|
23
|
+
*/
|
|
24
|
+
groupOf?: (node: GraphLayoutNode) => string | undefined;
|
|
25
|
+
/** Bundling tension passed to `d3.curveBundle.beta`. 0 = straight chord, 1 = max bundled. */
|
|
26
|
+
tension?: number;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const ROOT_ID = '__bundle_root__';
|
|
30
|
+
const GROUP_PREFIX = '__bundle_group__:';
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Cartesian → d3-radial polar. d3 measures angle clockwise from "up" (the -y direction).
|
|
34
|
+
* Inverse of `[cos(a - π/2) * r, sin(a - π/2) * r]`.
|
|
35
|
+
*/
|
|
36
|
+
const cartesianToPolar = (x: number, y: number): { angle: number; radius: number } => ({
|
|
37
|
+
angle: Math.atan2(x, -y),
|
|
38
|
+
radius: Math.hypot(x, y),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
type PolarPoint = { angle: number; radius: number };
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Hierarchical edge bundling — Holten 2006 / d3 observable notebook
|
|
45
|
+
* (https://observablehq.com/@d3/hierarchical-edge-bundling).
|
|
46
|
+
*
|
|
47
|
+
* Synthesizes the same root → group → leaf hierarchy as `GraphClusterProjector`, runs
|
|
48
|
+
* d3.cluster to give every node a polar position, but renders ONLY the leaves
|
|
49
|
+
* (root + groups are routing anchors, not visible nodes). Each data edge is replaced
|
|
50
|
+
* with a curve generated by `lineRadial(curveBundle.beta(tension))` over the polar
|
|
51
|
+
* positions of `source.path(target)` — i.e. the chain of ancestors through the lowest
|
|
52
|
+
* common ancestor.
|
|
53
|
+
*
|
|
54
|
+
* Per-frame `onTickFrame` refreshes the leaf endpoints (first + last point of every
|
|
55
|
+
* path) from current cartesian positions so the curves stay glued to leaves during a
|
|
56
|
+
* cross-variant tween (force → bundle, lattice → bundle). Internal ancestor positions
|
|
57
|
+
* are fixed at d3.cluster's final polar layout, which means during the tween curves
|
|
58
|
+
* route through the destination "pivot points" while the leaf endpoints catch up —
|
|
59
|
+
* visually the bundle "snaps into shape" as the leaves arrive.
|
|
60
|
+
*/
|
|
61
|
+
export class GraphBundleProjector<
|
|
62
|
+
NodeData = any,
|
|
63
|
+
Options extends GraphBundleProjectorOptions = any,
|
|
64
|
+
> extends GraphRadialProjector<NodeData, Options> {
|
|
65
|
+
// Per-edge polar point arrays. Points 0 and N-1 are the source / target leaves and are
|
|
66
|
+
// overwritten from current cartesian on every tick; intermediate points stay fixed at
|
|
67
|
+
// their cluster-layout polar coordinates.
|
|
68
|
+
#edgePoints = new Map<string, PolarPoint[]>();
|
|
69
|
+
#lineGen = lineRadial<PolarPoint>()
|
|
70
|
+
.radius((d) => d.radius)
|
|
71
|
+
.angle((d) => d.angle);
|
|
72
|
+
|
|
73
|
+
protected override onUpdate(graph?: Graph.Any) {
|
|
74
|
+
log('onUpdate', { graph: { nodes: graph?.nodes.length, edges: graph?.edges.length } });
|
|
75
|
+
this.mergeData(graph);
|
|
76
|
+
this.doBundleLayout();
|
|
77
|
+
this.emitUpdate('topology');
|
|
78
|
+
this.animate();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Refresh leaf endpoint polar values from current cartesian so the curve tracks
|
|
83
|
+
* its endpoints during the tween. Internal hierarchy positions stay constant.
|
|
84
|
+
*/
|
|
85
|
+
protected override onTickFrame(_t: number): void {
|
|
86
|
+
for (const edge of this.layout.graph.edges) {
|
|
87
|
+
if (edge.type !== 'bundle') {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
const points = this.#edgePoints.get(edge.id);
|
|
91
|
+
if (!points || points.length < 2) {
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
const s = cartesianToPolar(edge.source.x ?? 0, edge.source.y ?? 0);
|
|
95
|
+
const t = cartesianToPolar(edge.target.x ?? 0, edge.target.y ?? 0);
|
|
96
|
+
points[0] = { angle: s.angle, radius: s.radius };
|
|
97
|
+
points[points.length - 1] = { angle: t.angle, radius: t.radius };
|
|
98
|
+
edge.path = this.#lineGen(points) ?? undefined;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
private doBundleLayout() {
|
|
103
|
+
if (!this.context.size) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
const dataNodes = [...this.layout.graph.nodes];
|
|
107
|
+
if (!dataNodes.length) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const groupOf = this.options.groupOf;
|
|
112
|
+
|
|
113
|
+
// Synthesize root → groups → leaves hierarchy so d3.cluster gives every node
|
|
114
|
+
// (including the routing-only internal ones) a polar coordinate.
|
|
115
|
+
type HierNode = { id: string; parent?: string; node?: GraphLayoutNode<NodeData> };
|
|
116
|
+
const items: HierNode[] = [{ id: ROOT_ID }];
|
|
117
|
+
const groupIds = new Map<string, string>();
|
|
118
|
+
for (const node of dataNodes) {
|
|
119
|
+
const key = groupOf?.(node);
|
|
120
|
+
let parent = ROOT_ID;
|
|
121
|
+
if (key !== undefined) {
|
|
122
|
+
let groupId = groupIds.get(key);
|
|
123
|
+
if (!groupId) {
|
|
124
|
+
groupId = `${GROUP_PREFIX}${key}`;
|
|
125
|
+
groupIds.set(key, groupId);
|
|
126
|
+
items.push({ id: groupId, parent: ROOT_ID });
|
|
127
|
+
}
|
|
128
|
+
parent = groupId;
|
|
129
|
+
}
|
|
130
|
+
items.push({ id: node.id, parent, node });
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const childrenByParent = new Map<string, HierNode[]>();
|
|
134
|
+
for (const item of items) {
|
|
135
|
+
if (item.parent) {
|
|
136
|
+
const list = childrenByParent.get(item.parent) ?? [];
|
|
137
|
+
list.push(item);
|
|
138
|
+
childrenByParent.set(item.parent, list);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const root = hierarchy<HierNode>({ id: ROOT_ID }, (d) => childrenByParent.get(d.id) ?? []);
|
|
143
|
+
|
|
144
|
+
const { width, height } = this.context.size;
|
|
145
|
+
const margin = this.options.margin ?? 120;
|
|
146
|
+
const ringRadius = Math.max(0, Math.min(width, height) / 2 - margin);
|
|
147
|
+
|
|
148
|
+
d3Cluster<HierNode>()
|
|
149
|
+
.size([2 * Math.PI, ringRadius])
|
|
150
|
+
.separation((a: any, b: any) => (a.parent === b.parent ? 1 : 2) / Math.max(1, a.depth))(root);
|
|
151
|
+
|
|
152
|
+
const leafR = this.options.leafRadius ?? 4;
|
|
153
|
+
|
|
154
|
+
// Re-bake the line generator with the configured tension. d3 curve factories return
|
|
155
|
+
// a new curve when `.beta` is called, so this must happen whenever options change.
|
|
156
|
+
const tension = this.options.tension ?? 0.85;
|
|
157
|
+
this.#lineGen = lineRadial<PolarPoint>()
|
|
158
|
+
.curve(curveBundle.beta(tension))
|
|
159
|
+
.radius((d) => d.radius)
|
|
160
|
+
.angle((d) => d.angle);
|
|
161
|
+
|
|
162
|
+
// Index hierarchy nodes by data id for fast lookup when building edge paths.
|
|
163
|
+
const hierById = new Map<string, HierarchyNode<HierNode>>();
|
|
164
|
+
root.each((d) => {
|
|
165
|
+
hierById.set(d.data.id, d);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
// Position the leaf data nodes (cartesian); internal nodes stay layout-only.
|
|
169
|
+
root.leaves().forEach((d: any) => {
|
|
170
|
+
const angle = d.x;
|
|
171
|
+
const radius = d.y;
|
|
172
|
+
const x = Math.cos(angle - Math.PI / 2) * radius;
|
|
173
|
+
const y = Math.sin(angle - Math.PI / 2) * radius;
|
|
174
|
+
if (d.data.node) {
|
|
175
|
+
updateNode(d.data.node, [x, y], leafR);
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
this.layout.graph.nodes = dataNodes;
|
|
179
|
+
|
|
180
|
+
// Build bundle edges. `mergeData` already populated `layout.graph.edges` with data
|
|
181
|
+
// edges referencing the leaf GraphLayoutNodes — keep those references (so node
|
|
182
|
+
// animations & hover wiring continue to work) and enhance each with a routed path
|
|
183
|
+
// through the hierarchy.
|
|
184
|
+
this.#edgePoints.clear();
|
|
185
|
+
const incomingEdges = [...this.layout.graph.edges];
|
|
186
|
+
const bundleEdges: GraphLayoutEdge<NodeData>[] = [];
|
|
187
|
+
for (const edge of incomingEdges) {
|
|
188
|
+
const sourceHier = hierById.get(edge.source.id);
|
|
189
|
+
const targetHier = hierById.get(edge.target.id);
|
|
190
|
+
if (!sourceHier || !targetHier || sourceHier === targetHier) {
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
// hierarchy.path returns [source, ...up to LCA, ...down to target] (inclusive endpoints).
|
|
194
|
+
const pathNodes = sourceHier.path(targetHier);
|
|
195
|
+
const points: PolarPoint[] = pathNodes.map((n: any) => ({ angle: n.x, radius: n.y }));
|
|
196
|
+
this.#edgePoints.set(edge.id, points);
|
|
197
|
+
bundleEdges.push({
|
|
198
|
+
id: edge.id,
|
|
199
|
+
type: 'bundle',
|
|
200
|
+
source: edge.source,
|
|
201
|
+
target: edge.target,
|
|
202
|
+
data: edge.data,
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
this.layout.graph.edges = bundleEdges;
|
|
206
|
+
|
|
207
|
+
// Seed initial paths so the topology emit has correct geometry to enter into the DOM.
|
|
208
|
+
this.onTickFrame(0);
|
|
209
|
+
}
|
|
210
|
+
}
|