@dxos/react-ui-graph 0.8.4-main.74a063c4e0 → 0.8.4-main.765dc60934
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/{chunk-LKC455GK.mjs → chunk-EASNZZ7B.mjs} +1 -1
- package/dist/lib/browser/{chunk-LKC455GK.mjs.map → chunk-EASNZZ7B.mjs.map} +2 -2
- package/dist/lib/browser/index.mjs +92 -117
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +1 -1
- package/dist/lib/browser/testing/index.mjs.map +2 -2
- package/dist/lib/node-esm/{chunk-JE4KZCUF.mjs → chunk-JSE2X4ML.mjs} +1 -1
- package/dist/lib/node-esm/{chunk-JE4KZCUF.mjs.map → chunk-JSE2X4ML.mjs.map} +2 -2
- package/dist/lib/node-esm/index.mjs +92 -117
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +1 -1
- package/dist/lib/node-esm/testing/index.mjs.map +2 -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/Grid.d.ts.map +1 -1
- package/dist/types/src/components/SVG/Markers.d.ts.map +1 -1
- package/dist/types/src/components/SVG/Root.d.ts +5 -1
- package/dist/types/src/components/SVG/Root.d.ts.map +1 -1
- package/dist/types/src/components/SVG/Svg.stories.d.ts.map +1 -1
- package/dist/types/src/fx/fx.stories.d.ts.map +1 -1
- package/dist/types/src/fx/pulsar.d.ts.map +1 -1
- package/dist/types/src/graph/markers.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-drag.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-force-projector.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-forces.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-projector.d.ts.map +1 -1
- 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/projector.d.ts.map +1 -1
- package/dist/types/src/graph/renderer/bullets.d.ts.map +1 -1
- package/dist/types/src/graph/renderer/graph-renderer.d.ts.map +1 -1
- package/dist/types/src/graph/renderer/renderer.d.ts +6 -1
- package/dist/types/src/graph/renderer/renderer.d.ts.map +1 -1
- package/dist/types/src/hooks/useGrid.d.ts.map +1 -1
- package/dist/types/src/hooks/useStateRef.d.ts.map +1 -1
- package/dist/types/src/hooks/useSvgContext.d.ts.map +1 -1
- package/dist/types/src/hooks/useZoom.d.ts.map +1 -1
- package/dist/types/src/stories/hooks.stories.d.ts.map +1 -1
- package/dist/types/src/testing/data.d.ts +1 -1
- package/dist/types/src/testing/data.d.ts.map +1 -1
- package/dist/types/src/testing/hooks.d.ts.map +1 -1
- package/dist/types/src/testing/model.d.ts.map +1 -1
- package/dist/types/src/util/events.d.ts.map +1 -1
- package/dist/types/src/util/fraction.d.ts.map +1 -1
- package/dist/types/src/util/points.d.ts.map +1 -1
- package/dist/types/src/util/scale.d.ts.map +1 -1
- package/dist/types/src/util/screen.d.ts.map +1 -1
- package/dist/types/src/util/vector.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +19 -26
- package/src/components/Graph/Graph.stories.tsx +15 -17
- package/src/components/Graph/Graph.tsx +10 -13
- 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/renderer/graph-renderer.ts +13 -4
- package/src/graph/renderer/renderer.ts +6 -4
- package/src/stories/hooks.stories.tsx +6 -1
- package/styles/graph.css +1 -1
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.765dc60934",
|
|
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"
|
|
@@ -28,13 +28,6 @@
|
|
|
28
28
|
"./styles/graph.css": "./styles/graph.css"
|
|
29
29
|
},
|
|
30
30
|
"types": "dist/types/src/index.d.ts",
|
|
31
|
-
"typesVersions": {
|
|
32
|
-
"*": {
|
|
33
|
-
"testing": [
|
|
34
|
-
"dist/types/src/testing/index.d.ts"
|
|
35
|
-
]
|
|
36
|
-
}
|
|
37
|
-
},
|
|
38
31
|
"files": [
|
|
39
32
|
"testing.d.ts",
|
|
40
33
|
"testing.js",
|
|
@@ -51,13 +44,13 @@
|
|
|
51
44
|
"lodash.defaultsdeep": "^4.6.1",
|
|
52
45
|
"react-resize-detector": "^11.0.1",
|
|
53
46
|
"topojson-client": "^3.1.0",
|
|
54
|
-
"@dxos/
|
|
55
|
-
"@dxos/
|
|
56
|
-
"@dxos/
|
|
57
|
-
"@dxos/
|
|
58
|
-
"@dxos/node-std": "0.8.4-main.
|
|
59
|
-
"@dxos/react-ui-mosaic": "0.8.4-main.
|
|
60
|
-
"@dxos/graph": "0.8.4-main.
|
|
47
|
+
"@dxos/async": "0.8.4-main.765dc60934",
|
|
48
|
+
"@dxos/debug": "0.8.4-main.765dc60934",
|
|
49
|
+
"@dxos/invariant": "0.8.4-main.765dc60934",
|
|
50
|
+
"@dxos/log": "0.8.4-main.765dc60934",
|
|
51
|
+
"@dxos/node-std": "0.8.4-main.765dc60934",
|
|
52
|
+
"@dxos/react-ui-mosaic": "0.8.4-main.765dc60934",
|
|
53
|
+
"@dxos/graph": "0.8.4-main.765dc60934"
|
|
61
54
|
},
|
|
62
55
|
"devDependencies": {
|
|
63
56
|
"@babel/core": "^7.18.13",
|
|
@@ -67,24 +60,24 @@
|
|
|
67
60
|
"@types/react-dom": "~19.2.3",
|
|
68
61
|
"@types/topojson-client": "^3.1.4",
|
|
69
62
|
"@types/topojson-specification": "^1.0.5",
|
|
70
|
-
"postcss": "^8.
|
|
63
|
+
"postcss": "^8.5.12",
|
|
71
64
|
"react": "~19.2.3",
|
|
72
65
|
"react-dom": "~19.2.3",
|
|
73
|
-
"@dxos/random": "0.8.4-main.
|
|
74
|
-
"@dxos/react-ui
|
|
75
|
-
"@dxos/react-ui": "0.8.4-main.
|
|
76
|
-
"@dxos/
|
|
77
|
-
"@dxos/ui-theme": "0.8.4-main.
|
|
78
|
-
"@dxos/
|
|
66
|
+
"@dxos/random": "0.8.4-main.765dc60934",
|
|
67
|
+
"@dxos/react-ui": "0.8.4-main.765dc60934",
|
|
68
|
+
"@dxos/react-ui-stack": "0.8.4-main.765dc60934",
|
|
69
|
+
"@dxos/storybook-utils": "0.8.4-main.765dc60934",
|
|
70
|
+
"@dxos/ui-theme": "0.8.4-main.765dc60934",
|
|
71
|
+
"@dxos/react-ui-syntax-highlighter": "0.8.4-main.765dc60934"
|
|
79
72
|
},
|
|
80
73
|
"peerDependencies": {
|
|
81
74
|
"react": "~19.2.3",
|
|
82
75
|
"react-dom": "~19.2.3",
|
|
83
|
-
"@dxos/
|
|
84
|
-
"@dxos/ui
|
|
76
|
+
"@dxos/ui-theme": "0.8.4-main.765dc60934",
|
|
77
|
+
"@dxos/react-ui": "0.8.4-main.765dc60934"
|
|
85
78
|
},
|
|
86
79
|
"optionalDependencies": {
|
|
87
|
-
"@dxos/random": "0.8.4-main.
|
|
80
|
+
"@dxos/random": "0.8.4-main.765dc60934"
|
|
88
81
|
},
|
|
89
82
|
"publishConfig": {
|
|
90
83
|
"access": "public"
|
|
@@ -9,10 +9,10 @@ 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
|
-
import {
|
|
15
|
+
import { JsonHighlighter, Syntax } from '@dxos/react-ui-syntax-highlighter';
|
|
16
16
|
import { withLayout, withTheme } from '@dxos/react-ui/testing';
|
|
17
17
|
import { withRegistry } from '@dxos/storybook-utils';
|
|
18
18
|
import { getHashStyles, mx } from '@dxos/ui-theme';
|
|
@@ -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);
|
|
@@ -260,11 +260,7 @@ const DefaultStory = ({
|
|
|
260
260
|
<Popover.Content onOpenAutoFocus={(event) => event.preventDefault()}>
|
|
261
261
|
<Popover.Viewport>
|
|
262
262
|
<Card.Root>
|
|
263
|
-
<
|
|
264
|
-
language='json'
|
|
265
|
-
classNames='text-xs my-form-padding px-form-padding bg-transparent'
|
|
266
|
-
code={JSON.stringify(popover, null, 2)}
|
|
267
|
-
/>
|
|
263
|
+
<JsonHighlighter data={popover} classNames='text-xs my-form-padding px-form-padding bg-transparent' />
|
|
268
264
|
</Card.Root>
|
|
269
265
|
</Popover.Viewport>
|
|
270
266
|
<Popover.Arrow />
|
|
@@ -319,12 +315,14 @@ const Debug = ({
|
|
|
319
315
|
<IconButton onClick={onDelete} label='Delete' icon='ph--x--regular' iconOnly />
|
|
320
316
|
<IconButton onClick={onPing} label='Delete' icon='ph--crosshair-simple--regular' iconOnly />
|
|
321
317
|
</Toolbar.Root>
|
|
322
|
-
<
|
|
323
|
-
<
|
|
324
|
-
<
|
|
325
|
-
<
|
|
326
|
-
|
|
327
|
-
|
|
318
|
+
<Syntax.Root data={data}>
|
|
319
|
+
<Syntax.Content>
|
|
320
|
+
<Syntax.Filter />
|
|
321
|
+
<Syntax.Viewport>
|
|
322
|
+
<Syntax.Code classNames='text-sm' />
|
|
323
|
+
</Syntax.Viewport>
|
|
324
|
+
</Syntax.Content>
|
|
325
|
+
</Syntax.Root>
|
|
328
326
|
</div>
|
|
329
327
|
);
|
|
330
328
|
};
|
|
@@ -361,7 +359,7 @@ export const Projector: Story = {
|
|
|
361
359
|
debug: true,
|
|
362
360
|
drag: true,
|
|
363
361
|
arrows: true,
|
|
364
|
-
|
|
362
|
+
selectionMode: 'single',
|
|
365
363
|
projectorType: 'hierarchical',
|
|
366
364
|
projectorOptions: {
|
|
367
365
|
duration: 500,
|
|
@@ -54,8 +54,6 @@ const GraphInner = <Node extends Graph$.Node.Any = any, Edge extends Graph$.Edge
|
|
|
54
54
|
) => {
|
|
55
55
|
const context = useSvgContext();
|
|
56
56
|
const graphRef = useRef<SVGGElement>(null);
|
|
57
|
-
// TODO(wittjosiah): This doesn't work for some reason.
|
|
58
|
-
// const graph = useAtomValue(model?.graphAtom ?? EMPTY_ATOM);
|
|
59
57
|
|
|
60
58
|
const { projector, renderer } = useMemo(() => {
|
|
61
59
|
let projector = projectorProp;
|
|
@@ -70,8 +68,8 @@ const GraphInner = <Node extends Graph$.Node.Any = any, Edge extends Graph$.Edge
|
|
|
70
68
|
// TODO(burdon): Replace drag when projector is updated.
|
|
71
69
|
drag: drag ? createGraphDrag(context, projector) : undefined,
|
|
72
70
|
arrows: { end: arrows },
|
|
73
|
-
onNodeClick: onSelect
|
|
74
|
-
onNodePointerEnter: onInspect
|
|
71
|
+
onNodeClick: onSelect,
|
|
72
|
+
onNodePointerEnter: onInspect,
|
|
75
73
|
});
|
|
76
74
|
}
|
|
77
75
|
|
|
@@ -99,21 +97,20 @@ const GraphInner = <Node extends Graph$.Node.Any = any, Edge extends Graph$.Edge
|
|
|
99
97
|
useEffect(() => {
|
|
100
98
|
projector.updateData(model?.graph);
|
|
101
99
|
|
|
102
|
-
// Subscribe to model changes if reactive model.
|
|
103
|
-
const unsubscribeModel = model
|
|
104
|
-
? model.subscribe(() => {
|
|
105
|
-
projector.updateData(model?.graph);
|
|
106
|
-
})
|
|
107
|
-
: undefined;
|
|
108
|
-
|
|
109
100
|
return combine(
|
|
110
|
-
|
|
101
|
+
// Subscribe to model changes if reactive model.
|
|
102
|
+
model
|
|
103
|
+
? model.subscribe(() => {
|
|
104
|
+
projector.updateData(model?.graph);
|
|
105
|
+
})
|
|
106
|
+
: undefined,
|
|
107
|
+
|
|
111
108
|
projector.updated.on(({ layout }) => {
|
|
112
109
|
try {
|
|
113
110
|
renderer.render(layout);
|
|
114
111
|
} catch (error) {
|
|
115
|
-
void projector.stop();
|
|
116
112
|
log.catch(error);
|
|
113
|
+
void projector.stop();
|
|
117
114
|
}
|
|
118
115
|
}),
|
|
119
116
|
);
|
|
@@ -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}
|
|
@@ -59,6 +59,12 @@ export class GraphRenderer<NodeData = any, EdgeData = any> extends Renderer<
|
|
|
59
59
|
GraphRendererOptions<NodeData, EdgeData>
|
|
60
60
|
> {
|
|
61
61
|
override render(layout: GraphLayout<NodeData, EdgeData>) {
|
|
62
|
+
// The SVG group ref is unset between mount cycles and before the container has sized;
|
|
63
|
+
// skip the render rather than throw — the projector will emit again on the next tick.
|
|
64
|
+
if (!this.root) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
62
68
|
log('render', layout);
|
|
63
69
|
|
|
64
70
|
const root = select(this.root);
|
|
@@ -208,6 +214,9 @@ export class GraphRenderer<NodeData = any, EdgeData = any> extends Renderer<
|
|
|
208
214
|
* @param node
|
|
209
215
|
*/
|
|
210
216
|
fireBullet(node: GraphLayoutNode<NodeData>) {
|
|
217
|
+
if (!this.root) {
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
211
220
|
select(this.root).selectAll('g.dx-edges').selectAll('path').call(createBullets(this.root, node.id));
|
|
212
221
|
}
|
|
213
222
|
}
|
|
@@ -250,20 +259,20 @@ const createNode: D3Callable = <Data>(group: D3Selection, options: GraphRenderer
|
|
|
250
259
|
options.onNodePointerEnter(node, event);
|
|
251
260
|
});
|
|
252
261
|
|
|
253
|
-
group.attr('data-hover', 'handled');
|
|
262
|
+
group.attr('data-hover', 'handled');
|
|
254
263
|
} else if (options.highlight !== false) {
|
|
255
264
|
circle.on('pointerenter', function () {
|
|
256
265
|
select(this.closest('g.dx-node')).raise();
|
|
257
266
|
if (options.labels) {
|
|
258
|
-
select(this.parentElement).classed('dx-active', true).classed('dx-highlight', true);
|
|
267
|
+
select(this.parentElement).classed('dx-node-active', true).classed('dx-highlight', true);
|
|
259
268
|
}
|
|
260
269
|
});
|
|
261
270
|
|
|
262
271
|
group.on('pointerleave', function () {
|
|
263
272
|
if (options.labels) {
|
|
264
|
-
select(this).classed('dx-active', false);
|
|
273
|
+
select(this).classed('dx-node-active', false);
|
|
265
274
|
setTimeout(() => {
|
|
266
|
-
if (!select(this).classed('dx-active')) {
|
|
275
|
+
if (!select(this).classed('dx-node-active')) {
|
|
267
276
|
select(this).classed('dx-highlight', false);
|
|
268
277
|
}
|
|
269
278
|
}, 300);
|
|
@@ -4,8 +4,6 @@
|
|
|
4
4
|
|
|
5
5
|
import { type RefObject } from 'react';
|
|
6
6
|
|
|
7
|
-
import { invariant } from '@dxos/invariant';
|
|
8
|
-
|
|
9
7
|
import { type SVGContext } from '../../hooks';
|
|
10
8
|
import { type IdAccessor, defaultIdAccessor } from '../types';
|
|
11
9
|
|
|
@@ -36,8 +34,12 @@ export abstract class Renderer<LAYOUT, OPTIONS extends RendererOptions> {
|
|
|
36
34
|
return this._context;
|
|
37
35
|
}
|
|
38
36
|
|
|
39
|
-
|
|
40
|
-
|
|
37
|
+
/**
|
|
38
|
+
* Returns the SVG group root if mounted.
|
|
39
|
+
* The ref may be null between mount/unmount cycles or before the SVG container has sized.
|
|
40
|
+
* Callers should treat null as "skip this render" rather than as an error.
|
|
41
|
+
*/
|
|
42
|
+
get root(): SVGGElement | null {
|
|
41
43
|
return this._root.current;
|
|
42
44
|
}
|
|
43
45
|
|
|
@@ -109,9 +109,14 @@ const Component = ({
|
|
|
109
109
|
}, [markersRef]);
|
|
110
110
|
|
|
111
111
|
useEffect(() => {
|
|
112
|
+
// Prime the projector with the initial graph — `model.subscribe` only fires on changes by default,
|
|
113
|
+
// so without this the simulation starts empty and the renderer never sees any nodes.
|
|
114
|
+
// The subscribe callback receives `(model, graph)` from GraphModel; use the model reference
|
|
115
|
+
// we already have to fetch the current snapshot.
|
|
116
|
+
projector.updateData(model.graph);
|
|
112
117
|
void projector.start();
|
|
113
118
|
return combine(
|
|
114
|
-
model.subscribe((
|
|
119
|
+
model.subscribe(() => projector.updateData(model.graph)),
|
|
115
120
|
projector.updated.on(({ layout }) => renderer.render(layout)),
|
|
116
121
|
() => projector.stop(),
|
|
117
122
|
);
|