@dxos/react-ui-graph 0.8.4-main.72ec0f3 → 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/README.md +5 -7
- package/dist/lib/browser/chunk-EASNZZ7B.mjs +52 -0
- package/dist/lib/browser/chunk-EASNZZ7B.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +1116 -291
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +16 -16
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/node-esm/chunk-JSE2X4ML.mjs +54 -0
- package/dist/lib/node-esm/chunk-JSE2X4ML.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +1116 -291
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +16 -16
- package/dist/lib/node-esm/testing/index.mjs.map +3 -3
- package/dist/types/src/components/Graph/Graph.d.ts +4 -5
- package/dist/types/src/components/Graph/Graph.d.ts.map +1 -1
- package/dist/types/src/components/Graph/Graph.stories.d.ts +6 -6
- 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 +8 -4
- package/dist/types/src/components/SVG/Root.d.ts.map +1 -1
- package/dist/types/src/components/SVG/SVG.d.ts +14 -17
- package/dist/types/src/components/SVG/SVG.d.ts.map +1 -1
- package/dist/types/src/components/SVG/Svg.stories.d.ts +1 -1
- package/dist/types/src/components/SVG/Svg.stories.d.ts.map +1 -1
- package/dist/types/src/fx/fx.stories.d.ts +3 -3
- 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 +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 +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 +2 -2
- package/dist/types/src/graph/projector/graph-projector.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-radial-projector.d.ts +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 +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/graph/types.d.ts +1 -1
- package/dist/types/src/graph/types.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 +4 -3
- package/dist/types/src/stories/hooks.stories.d.ts.map +1 -1
- package/dist/types/src/testing/data.d.ts +5 -5
- package/dist/types/src/testing/data.d.ts.map +1 -1
- package/dist/types/src/testing/hooks.d.ts +2 -16
- package/dist/types/src/testing/hooks.d.ts.map +1 -1
- package/dist/types/src/testing/model.d.ts +4 -8
- 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 +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 +33 -35
- package/src/components/Graph/Graph.stories.tsx +45 -39
- package/src/components/Graph/Graph.tsx +23 -18
- package/src/components/Mesh/Mesh.stories.tsx +8 -7
- package/src/components/Mesh/Mesh.tsx +43 -35
- package/src/components/SVG/Grid.tsx +1 -1
- package/src/components/SVG/Root.tsx +41 -9
- package/src/components/SVG/SVG.tsx +10 -2
- package/src/components/SVG/Svg.stories.tsx +4 -5
- package/src/fx/fx.stories.tsx +7 -8
- package/src/graph/projector/graph-drag.ts +0 -1
- package/src/graph/projector/graph-force-projector.ts +1 -2
- package/src/graph/projector/graph-hierarchical-projector.ts +3 -4
- package/src/graph/projector/graph-projector.ts +2 -3
- package/src/graph/projector/graph-radial-projector.ts +1 -2
- package/src/graph/projector/graph-relational-projector.ts +3 -4
- package/src/graph/renderer/graph-renderer.ts +13 -5
- package/src/graph/renderer/renderer.ts +6 -4
- package/src/graph/types.ts +1 -1
- package/src/stories/hooks.stories.tsx +21 -10
- package/src/testing/data.ts +14 -14
- package/src/testing/hooks.ts +3 -4
- package/src/testing/model.ts +6 -6
- package/src/util/fraction.ts +1 -1
- package/styles/graph.css +292 -43
- package/dist/lib/browser/chunk-XWA2XJDX.mjs +0 -955
- package/dist/lib/browser/chunk-XWA2XJDX.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-3RLSFEEV.mjs +0 -957
- package/dist/lib/node-esm/chunk-3RLSFEEV.mjs.map +0 -7
|
@@ -2,17 +2,16 @@
|
|
|
2
2
|
// Copyright 2022 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import '../../../styles/graph.css';
|
|
6
|
+
|
|
5
7
|
import { type Meta, type StoryObj } from '@storybook/react-vite';
|
|
6
8
|
import React, { useRef } from 'react';
|
|
7
9
|
|
|
8
|
-
import { withTheme } from '@dxos/react-ui/testing';
|
|
10
|
+
import { withLayout, withTheme } from '@dxos/react-ui/testing';
|
|
9
11
|
|
|
10
12
|
import { type SVGContext, useGrid, useZoom } from '../../hooks';
|
|
11
|
-
|
|
12
13
|
import { SVG } from './SVG';
|
|
13
14
|
|
|
14
|
-
import '../../../styles/graph.css';
|
|
15
|
-
|
|
16
15
|
type ComponentProps = {
|
|
17
16
|
grid?: boolean;
|
|
18
17
|
zoom?: boolean;
|
|
@@ -45,7 +44,7 @@ const DefaultStory = (props: ComponentProps) => {
|
|
|
45
44
|
const meta = {
|
|
46
45
|
title: 'ui/react-ui-graph/SVGRoot',
|
|
47
46
|
render: DefaultStory,
|
|
48
|
-
decorators: [withTheme],
|
|
47
|
+
decorators: [withTheme(), withLayout({ layout: 'fullscreen' })],
|
|
49
48
|
parameters: {
|
|
50
49
|
layout: 'fullscreen',
|
|
51
50
|
},
|
package/src/fx/fx.stories.tsx
CHANGED
|
@@ -2,31 +2,30 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import '../../styles/graph.css';
|
|
6
|
+
|
|
5
7
|
import { type Meta, type StoryObj } from '@storybook/react-vite';
|
|
6
8
|
import { select } from 'd3';
|
|
7
9
|
import React, { type FC, useEffect, useMemo, useRef } from 'react';
|
|
8
10
|
|
|
9
|
-
import { withTheme } from '@dxos/react-ui/testing';
|
|
11
|
+
import { withLayout, withTheme } from '@dxos/react-ui/testing';
|
|
10
12
|
|
|
11
13
|
import { SVG } from '../components';
|
|
12
14
|
import { useGrid, useZoom } from '../hooks';
|
|
13
15
|
import { type D3Callable } from '../util';
|
|
14
|
-
|
|
15
16
|
import { Pulsar } from './pulsar';
|
|
16
17
|
|
|
17
|
-
import '../../styles/graph.css';
|
|
18
|
-
|
|
19
18
|
type Datum = {
|
|
20
19
|
x: number;
|
|
21
20
|
y: number;
|
|
22
21
|
r: number;
|
|
23
22
|
};
|
|
24
23
|
|
|
25
|
-
type
|
|
24
|
+
type DefaultStoryProps = {
|
|
26
25
|
count: number;
|
|
27
26
|
} & Pulsar.Options;
|
|
28
27
|
|
|
29
|
-
const DefaultStory = (props:
|
|
28
|
+
const DefaultStory = (props: DefaultStoryProps) => {
|
|
30
29
|
return (
|
|
31
30
|
<SVG.Root>
|
|
32
31
|
<StoryComponent {...props} />
|
|
@@ -34,7 +33,7 @@ const DefaultStory = (props: StoryProps) => {
|
|
|
34
33
|
);
|
|
35
34
|
};
|
|
36
35
|
|
|
37
|
-
const StoryComponent: FC<
|
|
36
|
+
const StoryComponent: FC<DefaultStoryProps> = ({ count = 1, ...options }) => {
|
|
38
37
|
const items = useMemo<Datum[]>(
|
|
39
38
|
() =>
|
|
40
39
|
Array.from({ length: count }, () => {
|
|
@@ -94,7 +93,7 @@ const createNode: D3Callable<SVGGElement, Datum> = (group, classNames, options)
|
|
|
94
93
|
const meta = {
|
|
95
94
|
title: 'ui/react-ui-graph/fx',
|
|
96
95
|
render: DefaultStory,
|
|
97
|
-
decorators: [withTheme],
|
|
96
|
+
decorators: [withTheme(), withLayout({ layout: 'fullscreen' })],
|
|
98
97
|
parameters: {
|
|
99
98
|
layout: 'fullscreen',
|
|
100
99
|
},
|
|
@@ -7,7 +7,6 @@ import { drag, pointer, select } from 'd3';
|
|
|
7
7
|
import { type SVGContext } from '../../hooks';
|
|
8
8
|
import { type Point } from '../../util';
|
|
9
9
|
import { type GraphLayoutNode } from '../types';
|
|
10
|
-
|
|
11
10
|
import { type GraphProjector } from './graph-projector';
|
|
12
11
|
|
|
13
12
|
enum Mode {
|
|
@@ -18,7 +18,6 @@ import { type Graph } from '@dxos/graph';
|
|
|
18
18
|
import { log } from '@dxos/log';
|
|
19
19
|
|
|
20
20
|
import { type GraphLayoutEdge, type GraphLayoutNode } from '../types';
|
|
21
|
-
|
|
22
21
|
import { forcePoint } from './graph-forces';
|
|
23
22
|
import { GraphProjector, type GraphProjectorOptions } from './graph-projector';
|
|
24
23
|
|
|
@@ -223,7 +222,7 @@ export class GraphForceProjector<NodeData = any> extends GraphProjector<NodeData
|
|
|
223
222
|
this.restart();
|
|
224
223
|
}
|
|
225
224
|
|
|
226
|
-
override onUpdate(graph?: Graph): void {
|
|
225
|
+
override onUpdate(graph?: Graph.Any): void {
|
|
227
226
|
log('onUpdate', { graph: { nodes: graph?.nodes.length, edges: graph?.edges.length } });
|
|
228
227
|
this._simulation.stop();
|
|
229
228
|
this.mergeData(graph);
|
|
@@ -6,7 +6,6 @@ import { type Graph } from '@dxos/graph';
|
|
|
6
6
|
import { log } from '@dxos/log';
|
|
7
7
|
|
|
8
8
|
import { type GraphLayoutNode } from '../types';
|
|
9
|
-
|
|
10
9
|
import { type GraphProjectorOptions } from './graph-projector';
|
|
11
10
|
import { GraphRadialProjector, layoutRadial, updateNode } from './graph-radial-projector';
|
|
12
11
|
|
|
@@ -20,14 +19,14 @@ export class GraphHierarchicalProjector<
|
|
|
20
19
|
NodeData = any,
|
|
21
20
|
Options extends GraphHierarchicalProjectorOptions = any,
|
|
22
21
|
> extends GraphRadialProjector<NodeData, Options> {
|
|
23
|
-
protected override onUpdate(graph?: Graph) {
|
|
22
|
+
protected override onUpdate(graph?: Graph.Any) {
|
|
24
23
|
log('onUpdate', {
|
|
25
24
|
graph: { nodes: graph?.nodes.length, edges: graph?.edges.length },
|
|
26
|
-
selection: this.selection?.
|
|
25
|
+
selection: this.selection?.getSelectedIds(),
|
|
27
26
|
});
|
|
28
27
|
|
|
29
28
|
this.mergeData(graph);
|
|
30
|
-
const selected = this.selection?.
|
|
29
|
+
const selected = this.selection?.getSelectedIds()[0];
|
|
31
30
|
if (selected) {
|
|
32
31
|
const node = this.layout.graph.nodes.find((node) => node.id === selected);
|
|
33
32
|
if (node) {
|
|
@@ -6,7 +6,6 @@ import { type Graph, type SelectionModel } from '@dxos/graph';
|
|
|
6
6
|
|
|
7
7
|
import { type SVGContext } from '../../hooks';
|
|
8
8
|
import { type GraphLayout, type GraphLayoutNode, emptyGraph } from '../types';
|
|
9
|
-
|
|
10
9
|
import { Projector, type ProjectorOptions } from './projector';
|
|
11
10
|
|
|
12
11
|
export type GraphProjectorOptions = ProjectorOptions & {};
|
|
@@ -15,7 +14,7 @@ export type GraphProjectorOptions = ProjectorOptions & {};
|
|
|
15
14
|
* Base class for graph projectors.
|
|
16
15
|
*/
|
|
17
16
|
export abstract class GraphProjector<NodeData = any, Options extends GraphProjectorOptions = any> extends Projector<
|
|
18
|
-
Graph,
|
|
17
|
+
Graph.Any,
|
|
19
18
|
GraphLayout<NodeData>,
|
|
20
19
|
Options
|
|
21
20
|
> {
|
|
@@ -60,7 +59,7 @@ export abstract class GraphProjector<NodeData = any, Options extends GraphProjec
|
|
|
60
59
|
* Merge external data with internal representation (e.g., so force properties like position are preserved).
|
|
61
60
|
* @param data
|
|
62
61
|
*/
|
|
63
|
-
protected mergeData(data: Graph = emptyGraph) {
|
|
62
|
+
protected mergeData(data: Graph.Any = emptyGraph) {
|
|
64
63
|
// Merge nodes.
|
|
65
64
|
const nodes: GraphLayoutNode[] = data.nodes.map((node) => {
|
|
66
65
|
let current: GraphLayoutNode = this._layout.graph.nodes.find((n) => n.id === this.options.idAccessor(node));
|
|
@@ -9,7 +9,6 @@ import { log } from '@dxos/log';
|
|
|
9
9
|
|
|
10
10
|
import { type Point } from '../../util';
|
|
11
11
|
import { type GraphLayoutNode } from '../types';
|
|
12
|
-
|
|
13
12
|
import { GraphProjector, type GraphProjectorOptions } from './graph-projector';
|
|
14
13
|
|
|
15
14
|
export type GraphRadialProjectorOptions = GraphProjectorOptions & {
|
|
@@ -28,7 +27,7 @@ export class GraphRadialProjector<
|
|
|
28
27
|
return undefined;
|
|
29
28
|
}
|
|
30
29
|
|
|
31
|
-
protected override onUpdate(graph?: Graph) {
|
|
30
|
+
protected override onUpdate(graph?: Graph.Any) {
|
|
32
31
|
log('onUpdate', { graph: { nodes: graph?.nodes.length, edges: graph?.edges.length } });
|
|
33
32
|
this.mergeData(graph);
|
|
34
33
|
this.doRadialLayout();
|
|
@@ -6,7 +6,6 @@ import { type Graph } from '@dxos/graph';
|
|
|
6
6
|
import { log } from '@dxos/log';
|
|
7
7
|
|
|
8
8
|
import { type GraphLayoutNode } from '../types';
|
|
9
|
-
|
|
10
9
|
import { type GraphProjectorOptions } from './graph-projector';
|
|
11
10
|
import { GraphRadialProjector, layoutHorizontal, layoutRadial, updateNode } from './graph-radial-projector';
|
|
12
11
|
|
|
@@ -20,14 +19,14 @@ export class GraphRelationalProjector<
|
|
|
20
19
|
NodeData = any,
|
|
21
20
|
Options extends GraphRelationalProjectorOptions = any,
|
|
22
21
|
> extends GraphRadialProjector<NodeData, Options> {
|
|
23
|
-
protected override onUpdate(graph?: Graph) {
|
|
22
|
+
protected override onUpdate(graph?: Graph.Any) {
|
|
24
23
|
log('onUpdate', {
|
|
25
24
|
graph: { nodes: graph?.nodes.length, edges: graph?.edges.length },
|
|
26
|
-
selection: this.selection?.
|
|
25
|
+
selection: this.selection?.getSelectedIds(),
|
|
27
26
|
});
|
|
28
27
|
|
|
29
28
|
this.mergeData(graph);
|
|
30
|
-
const selected = this.selection?.
|
|
29
|
+
const selected = this.selection?.getSelectedIds()[0];
|
|
31
30
|
if (selected) {
|
|
32
31
|
const node = this.layout.graph.nodes.find((node) => node.id === selected);
|
|
33
32
|
if (node) {
|
|
@@ -9,7 +9,6 @@ import { log } from '@dxos/log';
|
|
|
9
9
|
|
|
10
10
|
import { type D3Callable, type D3Selection, type Point, getCircumferencePoints } from '../../util';
|
|
11
11
|
import { type GraphLayout, type GraphLayoutEdge, type GraphLayoutNode } from '../types';
|
|
12
|
-
|
|
13
12
|
import { createBullets } from './bullets';
|
|
14
13
|
import { Renderer, type RendererOptions } from './renderer';
|
|
15
14
|
|
|
@@ -60,6 +59,12 @@ export class GraphRenderer<NodeData = any, EdgeData = any> extends Renderer<
|
|
|
60
59
|
GraphRendererOptions<NodeData, EdgeData>
|
|
61
60
|
> {
|
|
62
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
|
+
|
|
63
68
|
log('render', layout);
|
|
64
69
|
|
|
65
70
|
const root = select(this.root);
|
|
@@ -209,6 +214,9 @@ export class GraphRenderer<NodeData = any, EdgeData = any> extends Renderer<
|
|
|
209
214
|
* @param node
|
|
210
215
|
*/
|
|
211
216
|
fireBullet(node: GraphLayoutNode<NodeData>) {
|
|
217
|
+
if (!this.root) {
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
212
220
|
select(this.root).selectAll('g.dx-edges').selectAll('path').call(createBullets(this.root, node.id));
|
|
213
221
|
}
|
|
214
222
|
}
|
|
@@ -251,20 +259,20 @@ const createNode: D3Callable = <Data>(group: D3Selection, options: GraphRenderer
|
|
|
251
259
|
options.onNodePointerEnter(node, event);
|
|
252
260
|
});
|
|
253
261
|
|
|
254
|
-
group.attr('data-hover', 'handled');
|
|
262
|
+
group.attr('data-hover', 'handled');
|
|
255
263
|
} else if (options.highlight !== false) {
|
|
256
264
|
circle.on('pointerenter', function () {
|
|
257
265
|
select(this.closest('g.dx-node')).raise();
|
|
258
266
|
if (options.labels) {
|
|
259
|
-
select(this.parentElement).classed('dx-active', true).classed('dx-highlight', true);
|
|
267
|
+
select(this.parentElement).classed('dx-node-active', true).classed('dx-highlight', true);
|
|
260
268
|
}
|
|
261
269
|
});
|
|
262
270
|
|
|
263
271
|
group.on('pointerleave', function () {
|
|
264
272
|
if (options.labels) {
|
|
265
|
-
select(this).classed('dx-active', false);
|
|
273
|
+
select(this).classed('dx-node-active', false);
|
|
266
274
|
setTimeout(() => {
|
|
267
|
-
if (!select(this).classed('dx-active')) {
|
|
275
|
+
if (!select(this).classed('dx-node-active')) {
|
|
268
276
|
select(this).classed('dx-highlight', false);
|
|
269
277
|
}
|
|
270
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
|
|
package/src/graph/types.ts
CHANGED
|
@@ -2,13 +2,18 @@
|
|
|
2
2
|
// Copyright 2020 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import '../../styles/graph.css';
|
|
6
|
+
|
|
7
|
+
import { RegistryContext } from '@effect-atom/atom-react';
|
|
5
8
|
import { type Meta, type StoryObj } from '@storybook/react-vite';
|
|
6
9
|
import { select } from 'd3';
|
|
7
|
-
import React, { type PropsWithChildren, useEffect, useMemo, useRef } from 'react';
|
|
10
|
+
import React, { type PropsWithChildren, useContext, useEffect, useMemo, useRef } from 'react';
|
|
8
11
|
|
|
9
12
|
import { combine } from '@dxos/async';
|
|
13
|
+
import { type Graph } from '@dxos/graph';
|
|
10
14
|
import { log } from '@dxos/log';
|
|
11
|
-
import { withTheme } from '@dxos/react-ui/testing';
|
|
15
|
+
import { withLayout, withTheme } from '@dxos/react-ui/testing';
|
|
16
|
+
import { withRegistry } from '@dxos/storybook-utils';
|
|
12
17
|
|
|
13
18
|
import { SVG } from '../components';
|
|
14
19
|
import {
|
|
@@ -24,10 +29,8 @@ import {
|
|
|
24
29
|
import { useGrid, useSvgContext, useZoom } from '../hooks';
|
|
25
30
|
import { TestGraphModel, type TestNode, convertTreeToGraph, createTree } from '../testing';
|
|
26
31
|
|
|
27
|
-
import '../../styles/graph.css';
|
|
28
|
-
|
|
29
32
|
type ComponentProps = PropsWithChildren<{
|
|
30
|
-
|
|
33
|
+
graph: () => Graph.Graph<TestNode, Graph.Edge.Any>;
|
|
31
34
|
projectorOptions?: GraphForceProjectorOptions;
|
|
32
35
|
count?: number;
|
|
33
36
|
interval?: number;
|
|
@@ -36,13 +39,16 @@ type ComponentProps = PropsWithChildren<{
|
|
|
36
39
|
}>;
|
|
37
40
|
|
|
38
41
|
const Component = ({
|
|
39
|
-
|
|
42
|
+
graph: graphFactory,
|
|
40
43
|
projectorOptions,
|
|
41
44
|
count = 0,
|
|
42
45
|
interval = 200,
|
|
43
46
|
link = false,
|
|
44
47
|
grid: showGrid = false,
|
|
45
48
|
}: ComponentProps) => {
|
|
49
|
+
const registry = useContext(RegistryContext);
|
|
50
|
+
const model = useMemo(() => new TestGraphModel(registry, graphFactory()), [registry, graphFactory]);
|
|
51
|
+
|
|
46
52
|
const context = useSvgContext();
|
|
47
53
|
const graphRef = useRef<SVGGElement>(null);
|
|
48
54
|
const markersRef = useRef<SVGGElement>(null);
|
|
@@ -103,9 +109,14 @@ const Component = ({
|
|
|
103
109
|
}, [markersRef]);
|
|
104
110
|
|
|
105
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);
|
|
106
117
|
void projector.start();
|
|
107
118
|
return combine(
|
|
108
|
-
model.subscribe((
|
|
119
|
+
model.subscribe(() => projector.updateData(model.graph)),
|
|
109
120
|
projector.updated.on(({ layout }) => renderer.render(layout)),
|
|
110
121
|
() => projector.stop(),
|
|
111
122
|
);
|
|
@@ -152,7 +163,7 @@ const DefaultStory = ({ children, ...props }: ComponentProps) => {
|
|
|
152
163
|
const meta = {
|
|
153
164
|
title: 'ui/react-ui-graph/hooks',
|
|
154
165
|
render: DefaultStory,
|
|
155
|
-
decorators: [withTheme],
|
|
166
|
+
decorators: [withRegistry, withTheme(), withLayout({ layout: 'fullscreen' })],
|
|
156
167
|
parameters: {
|
|
157
168
|
layout: 'fullscreen',
|
|
158
169
|
},
|
|
@@ -164,7 +175,7 @@ type Story = StoryObj<typeof meta>;
|
|
|
164
175
|
|
|
165
176
|
export const Default: Story = {
|
|
166
177
|
args: {
|
|
167
|
-
|
|
178
|
+
graph: () => convertTreeToGraph(createTree({ depth: 3 })),
|
|
168
179
|
grid: true,
|
|
169
180
|
count: 300,
|
|
170
181
|
interval: 20,
|
|
@@ -189,7 +200,7 @@ export const Default: Story = {
|
|
|
189
200
|
|
|
190
201
|
export const Bullets: Story = {
|
|
191
202
|
args: {
|
|
192
|
-
|
|
203
|
+
graph: () => convertTreeToGraph(createTree({ depth: 4 })),
|
|
193
204
|
children: <span>⌘-DRAG to edge or create node; ⌘-CLICK to delete edge.</span>,
|
|
194
205
|
link: true,
|
|
195
206
|
grid: false,
|
package/src/testing/data.ts
CHANGED
|
@@ -2,28 +2,28 @@
|
|
|
2
2
|
// Copyright 2020 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { type
|
|
6
|
-
import {
|
|
5
|
+
import { type Graph } from '@dxos/graph';
|
|
6
|
+
import { random } from '@dxos/random';
|
|
7
7
|
|
|
8
8
|
import { type TestNode } from './model';
|
|
9
9
|
|
|
10
|
-
// https://www.npmjs.com/package/
|
|
11
|
-
export const seed = (seed: number) =>
|
|
10
|
+
// https://www.npmjs.com/package/random#setting-a-randomness-seed
|
|
11
|
+
export const seed = (seed: number) => random.seed(seed);
|
|
12
12
|
|
|
13
13
|
//
|
|
14
14
|
// Create data.
|
|
15
15
|
//
|
|
16
16
|
|
|
17
17
|
export const createNode = (type: string = undefined): TestNode => ({
|
|
18
|
-
id:
|
|
18
|
+
id: random.string.uuid(),
|
|
19
19
|
type,
|
|
20
|
-
label:
|
|
20
|
+
label: random.lorem.words(3).replace(/ /g, '-'),
|
|
21
21
|
});
|
|
22
22
|
|
|
23
23
|
export const createNodes = (n = 0, types?: string[]): TestNode[] =>
|
|
24
|
-
Array.from({ length: n }, () => createNode(types ?
|
|
24
|
+
Array.from({ length: n }, () => createNode(types ? random.helpers.arrayElement(types) : undefined));
|
|
25
25
|
|
|
26
|
-
export const createEdge = (source: TestNode, target: TestNode):
|
|
26
|
+
export const createEdge = (source: TestNode, target: TestNode): Graph.Edge.Any => ({
|
|
27
27
|
id: `${source.id}-${target.id}`,
|
|
28
28
|
source: source.id,
|
|
29
29
|
target: target.id,
|
|
@@ -38,7 +38,7 @@ export const createTree = ({ depth = 2, children = 3 } = {}): TestNode => {
|
|
|
38
38
|
const createChildren = (root: TestNode, d = 0) => {
|
|
39
39
|
if (d < depth) {
|
|
40
40
|
const max = Math.round(Math.log(depth + 1 - d) * children);
|
|
41
|
-
const num =
|
|
41
|
+
const num = random.number.int({ min: 1, max });
|
|
42
42
|
root.children = [...new Array(num)].map(() => {
|
|
43
43
|
return createChildren(createNode(), d + 1);
|
|
44
44
|
});
|
|
@@ -54,8 +54,8 @@ export const createTree = ({ depth = 2, children = 3 } = {}): TestNode => {
|
|
|
54
54
|
* Converts a tree into a graph.
|
|
55
55
|
* @param root
|
|
56
56
|
*/
|
|
57
|
-
export const convertTreeToGraph = (root: TestNode): Graph => {
|
|
58
|
-
const traverse = (node: TestNode, graph: Graph) => {
|
|
57
|
+
export const convertTreeToGraph = (root: TestNode): Graph.Any => {
|
|
58
|
+
const traverse = (node: TestNode, graph: Graph.Any) => {
|
|
59
59
|
graph.nodes.push(node);
|
|
60
60
|
node.children?.forEach((child) => {
|
|
61
61
|
graph.edges.push(createEdge(node, child));
|
|
@@ -76,14 +76,14 @@ export const convertTreeToGraph = (root: TestNode): Graph => {
|
|
|
76
76
|
* @param numNodes
|
|
77
77
|
* @param numEdges
|
|
78
78
|
*/
|
|
79
|
-
export const createGraph = (numNodes = 0, numEdges = 0, types?: string[]): Graph => {
|
|
79
|
+
export const createGraph = (numNodes = 0, numEdges = 0, types?: string[]): Graph.Any => {
|
|
80
80
|
const nodes = createNodes(numNodes, types);
|
|
81
81
|
|
|
82
82
|
const edges = new Map();
|
|
83
83
|
if (numEdges && nodes.length >= 2) {
|
|
84
84
|
for (let i = 0; i < numEdges; i++) {
|
|
85
|
-
const source =
|
|
86
|
-
const target =
|
|
85
|
+
const source = random.helpers.arrayElement(nodes);
|
|
86
|
+
const target = random.helpers.arrayElement(nodes);
|
|
87
87
|
if (source.id !== target.id) {
|
|
88
88
|
const edge = createEdge(source, target);
|
|
89
89
|
if (!edges.get(edge.id)) {
|
package/src/testing/hooks.ts
CHANGED
|
@@ -5,10 +5,9 @@
|
|
|
5
5
|
import update from 'immutability-helper';
|
|
6
6
|
|
|
7
7
|
import { type Graph } from '@dxos/graph';
|
|
8
|
-
import {
|
|
8
|
+
import { random } from '@dxos/random';
|
|
9
9
|
|
|
10
10
|
import { useStateRef } from '../hooks';
|
|
11
|
-
|
|
12
11
|
import { createEdge, createNode } from './data';
|
|
13
12
|
import { type TestNode } from './model';
|
|
14
13
|
|
|
@@ -47,13 +46,13 @@ export const useObjectMutator = <T>(initalValue: T): ObjectMutator<T> => {
|
|
|
47
46
|
/**
|
|
48
47
|
* Test data set generator and mutator.
|
|
49
48
|
*/
|
|
50
|
-
export const useGraphGenerator = (options: { data?: Graph } = {}) => {
|
|
49
|
+
export const useGraphGenerator = (options: { data?: Graph.Any } = {}) => {
|
|
51
50
|
const [data, setData, updateData] = useObjectMutator(options.data || { nodes: [], edges: [] });
|
|
52
51
|
|
|
53
52
|
let interval;
|
|
54
53
|
|
|
55
54
|
const mutator = () => {
|
|
56
|
-
const parent = data.nodes.length ?
|
|
55
|
+
const parent = data.nodes.length ? random.helpers.arrayElement(data.nodes) : undefined;
|
|
57
56
|
const node = createNode();
|
|
58
57
|
|
|
59
58
|
updateData({
|
package/src/testing/model.ts
CHANGED
|
@@ -2,25 +2,25 @@
|
|
|
2
2
|
// Copyright 2022 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { type
|
|
6
|
-
import {
|
|
5
|
+
import { type Graph, GraphModel } from '@dxos/graph';
|
|
6
|
+
import { random } from '@dxos/random';
|
|
7
7
|
|
|
8
8
|
import { createEdge, createNode } from './data';
|
|
9
9
|
|
|
10
|
-
export type TestNode =
|
|
10
|
+
export type TestNode = Graph.Node.Any & {
|
|
11
11
|
label?: string;
|
|
12
12
|
children?: TestNode[];
|
|
13
13
|
};
|
|
14
14
|
|
|
15
|
-
export class TestGraphModel extends ReactiveGraphModel<TestNode> {
|
|
15
|
+
export class TestGraphModel extends GraphModel.ReactiveGraphModel<TestNode> {
|
|
16
16
|
getRandomNode() {
|
|
17
|
-
return
|
|
17
|
+
return random.helpers.arrayElement(this._graph.nodes);
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
createNodes(node: TestNode = undefined, n = 1): void {
|
|
21
21
|
Array.from({ length: n }).forEach(() => {
|
|
22
22
|
const child = this.addNode(createNode());
|
|
23
|
-
const parent = node ||
|
|
23
|
+
const parent = node || random.helpers.arrayElement(this._graph.nodes);
|
|
24
24
|
if (parent) {
|
|
25
25
|
this.addEdge(createEdge(parent, child));
|
|
26
26
|
}
|