@dxos/react-ui-graph 0.8.4-main.b97322e → 0.8.4-main.bc2380dfbc
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 +1129 -298
- 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 +1129 -298
- 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 +20 -11
- 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 +9 -5
- 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 +11 -9
- package/dist/types/src/components/SVG/Svg.stories.d.ts.map +1 -1
- package/dist/types/src/components/SVG/Zoom.d.ts.map +1 -1
- package/dist/types/src/fx/fx.stories.d.ts +16 -15
- 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 +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 +2 -2
- 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 +2 -2
- package/dist/types/src/graph/projector/graph-hierarchical-projector.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-projector.d.ts +4 -4
- package/dist/types/src/graph/projector/graph-projector.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-radial-projector.d.ts +2 -2
- 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 +2 -2
- 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 +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 +14 -9
- 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 +1 -1
- package/dist/types/src/util/screen.d.ts.map +1 -1
- package/dist/types/src/util/vector.d.ts +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 +73 -69
- package/src/components/Graph/Graph.tsx +25 -20
- package/src/components/Mesh/Mesh.stories.tsx +16 -13
- package/src/components/Mesh/Mesh.tsx +44 -36
- package/src/components/SVG/Grid.tsx +2 -2
- package/src/components/SVG/Markers.tsx +1 -1
- package/src/components/SVG/Root.tsx +41 -9
- package/src/components/SVG/SVG.tsx +13 -4
- package/src/components/SVG/Svg.stories.tsx +23 -19
- package/src/components/SVG/Zoom.tsx +1 -1
- package/src/fx/fx.stories.tsx +18 -15
- package/src/graph/projector/graph-drag.ts +1 -1
- package/src/graph/projector/graph-force-projector.ts +2 -2
- package/src/graph/projector/graph-forces.ts +1 -1
- package/src/graph/projector/graph-hierarchical-projector.ts +4 -4
- package/src/graph/projector/graph-projector.ts +5 -5
- package/src/graph/projector/graph-radial-projector.ts +3 -3
- package/src/graph/projector/graph-relational-projector.ts +4 -4
- package/src/graph/projector/projector.ts +1 -1
- package/src/graph/renderer/bullets.ts +1 -1
- package/src/graph/renderer/graph-renderer.ts +16 -7
- package/src/graph/renderer/renderer.ts +7 -5
- package/src/graph/types.ts +1 -1
- package/src/hooks/useStateRef.ts +1 -1
- package/src/hooks/useZoom.ts +1 -1
- package/src/stories/hooks.stories.tsx +49 -37
- package/src/testing/data.ts +14 -14
- package/src/testing/hooks.ts +5 -5
- package/src/testing/model.ts +6 -6
- package/src/util/fraction.ts +1 -1
- package/src/util/screen.test.ts +1 -1
- package/src/util/screen.ts +1 -1
- package/src/util/vector.ts +1 -1
- package/styles/graph.css +292 -43
- package/dist/lib/browser/chunk-MGVY54T5.mjs +0 -979
- package/dist/lib/browser/chunk-MGVY54T5.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-X5YVOSXZ.mjs +0 -981
- package/dist/lib/node-esm/chunk-X5YVOSXZ.mjs.map +0 -7
|
@@ -4,10 +4,8 @@
|
|
|
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
|
-
import {
|
|
8
|
+
import { type IdAccessor, defaultIdAccessor } from '../types';
|
|
11
9
|
|
|
12
10
|
export type RendererOptions<T = any> = {
|
|
13
11
|
idAccessor: IdAccessor;
|
|
@@ -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
package/src/hooks/useStateRef.ts
CHANGED
|
@@ -16,7 +16,7 @@ export const useStateRef = <V>(
|
|
|
16
16
|
initialValue?: V | (() => V),
|
|
17
17
|
): [V | undefined, Dispatch<SetStateAction<V | undefined>>, RefObject<V | undefined>] => {
|
|
18
18
|
const [value, setValue] = useState<V | undefined>(initialValue);
|
|
19
|
-
const ref = useRef<V>();
|
|
19
|
+
const ref = useRef<V>(null);
|
|
20
20
|
useEffect(() => {
|
|
21
21
|
ref.current = value;
|
|
22
22
|
}, [initialValue, value]);
|
package/src/hooks/useZoom.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2022 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { type ZoomBehavior, type ZoomTransform, select, zoom, zoomIdentity } from 'd3';
|
|
6
6
|
import defaultsDeep from 'lodash.defaultsdeep';
|
|
7
7
|
import { type RefObject, useEffect, useMemo, useRef } from 'react';
|
|
8
8
|
|
|
@@ -2,34 +2,35 @@
|
|
|
2
2
|
// Copyright 2020 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import '
|
|
5
|
+
import '../../styles/graph.css';
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import { RegistryContext } from '@effect-atom/atom-react';
|
|
8
|
+
import { type Meta, type StoryObj } from '@storybook/react-vite';
|
|
8
9
|
import { select } from 'd3';
|
|
9
|
-
import React, { type PropsWithChildren, useEffect, useMemo, useRef } from 'react';
|
|
10
|
+
import React, { type PropsWithChildren, useContext, useEffect, useMemo, useRef } from 'react';
|
|
10
11
|
|
|
11
12
|
import { combine } from '@dxos/async';
|
|
13
|
+
import { type Graph } from '@dxos/graph';
|
|
12
14
|
import { log } from '@dxos/log';
|
|
13
|
-
import {
|
|
15
|
+
import { withLayout, withTheme } from '@dxos/react-ui/testing';
|
|
16
|
+
import { withRegistry } from '@dxos/storybook-utils';
|
|
14
17
|
|
|
15
18
|
import { SVG } from '../components';
|
|
16
19
|
import {
|
|
17
20
|
GraphForceProjector,
|
|
18
|
-
type
|
|
21
|
+
type GraphForceProjectorOptions,
|
|
19
22
|
type GraphLayoutEdge,
|
|
23
|
+
type GraphLayoutNode,
|
|
20
24
|
GraphRenderer,
|
|
21
|
-
type GraphForceProjectorOptions,
|
|
22
|
-
createMarkers,
|
|
23
25
|
createGraphDrag,
|
|
26
|
+
createMarkers,
|
|
24
27
|
linkerRenderer,
|
|
25
28
|
} from '../graph';
|
|
26
|
-
import { useSvgContext, useZoom
|
|
27
|
-
import {
|
|
29
|
+
import { useGrid, useSvgContext, useZoom } from '../hooks';
|
|
30
|
+
import { TestGraphModel, type TestNode, convertTreeToGraph, createTree } from '../testing';
|
|
28
31
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
type StoryProps = PropsWithChildren<{
|
|
32
|
-
model: TestGraphModel;
|
|
32
|
+
type ComponentProps = PropsWithChildren<{
|
|
33
|
+
graph: () => Graph.Graph<TestNode, Graph.Edge.Any>;
|
|
33
34
|
projectorOptions?: GraphForceProjectorOptions;
|
|
34
35
|
count?: number;
|
|
35
36
|
interval?: number;
|
|
@@ -37,28 +38,20 @@ type StoryProps = PropsWithChildren<{
|
|
|
37
38
|
grid?: boolean;
|
|
38
39
|
}>;
|
|
39
40
|
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
<>
|
|
43
|
-
<SVG.Root>
|
|
44
|
-
<StoryComponent {...props} />
|
|
45
|
-
</SVG.Root>
|
|
46
|
-
{children && <div className='absolute left-4 bottom-4 font-mono text-green-500 text-xs'>{children}</div>}
|
|
47
|
-
</>
|
|
48
|
-
);
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
const StoryComponent = ({
|
|
52
|
-
model,
|
|
41
|
+
const Component = ({
|
|
42
|
+
graph: graphFactory,
|
|
53
43
|
projectorOptions,
|
|
54
44
|
count = 0,
|
|
55
45
|
interval = 200,
|
|
56
46
|
link = false,
|
|
57
47
|
grid: showGrid = false,
|
|
58
|
-
}:
|
|
48
|
+
}: ComponentProps) => {
|
|
49
|
+
const registry = useContext(RegistryContext);
|
|
50
|
+
const model = useMemo(() => new TestGraphModel(registry, graphFactory()), [registry, graphFactory]);
|
|
51
|
+
|
|
59
52
|
const context = useSvgContext();
|
|
60
|
-
const graphRef = useRef<SVGGElement>();
|
|
61
|
-
const markersRef = useRef<SVGGElement>();
|
|
53
|
+
const graphRef = useRef<SVGGElement>(null);
|
|
54
|
+
const markersRef = useRef<SVGGElement>(null);
|
|
62
55
|
const grid = useGrid({ visible: showGrid });
|
|
63
56
|
const zoom = useZoom();
|
|
64
57
|
|
|
@@ -94,7 +87,7 @@ const StoryComponent = ({
|
|
|
94
87
|
labels: {
|
|
95
88
|
text: (node: GraphLayoutNode<TestNode>) => node.id.substring(0, 4),
|
|
96
89
|
},
|
|
97
|
-
onNodeClick: (node: GraphLayoutNode<TestNode
|
|
90
|
+
onNodeClick: (node: GraphLayoutNode<TestNode>) => {
|
|
98
91
|
renderer.fireBullet(node);
|
|
99
92
|
},
|
|
100
93
|
onLinkClick: (edge: GraphLayoutEdge<TestNode>, event: MouseEvent) => {
|
|
@@ -116,9 +109,14 @@ const StoryComponent = ({
|
|
|
116
109
|
}, [markersRef]);
|
|
117
110
|
|
|
118
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);
|
|
119
117
|
void projector.start();
|
|
120
118
|
return combine(
|
|
121
|
-
model.subscribe((
|
|
119
|
+
model.subscribe(() => projector.updateData(model.graph)),
|
|
122
120
|
projector.updated.on(({ layout }) => renderer.render(layout)),
|
|
123
121
|
() => projector.stop(),
|
|
124
122
|
);
|
|
@@ -151,19 +149,33 @@ const StoryComponent = ({
|
|
|
151
149
|
);
|
|
152
150
|
};
|
|
153
151
|
|
|
154
|
-
const
|
|
152
|
+
const DefaultStory = ({ children, ...props }: ComponentProps) => {
|
|
153
|
+
return (
|
|
154
|
+
<>
|
|
155
|
+
<SVG.Root>
|
|
156
|
+
<Component {...props} />
|
|
157
|
+
</SVG.Root>
|
|
158
|
+
{children && <div className='absolute left-4 bottom-4 font-mono text-green-500 text-xs'>{children}</div>}
|
|
159
|
+
</>
|
|
160
|
+
);
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
const meta = {
|
|
155
164
|
title: 'ui/react-ui-graph/hooks',
|
|
156
165
|
render: DefaultStory,
|
|
157
|
-
decorators: [withTheme, withLayout({
|
|
158
|
-
|
|
166
|
+
decorators: [withRegistry, withTheme(), withLayout({ layout: 'fullscreen' })],
|
|
167
|
+
parameters: {
|
|
168
|
+
layout: 'fullscreen',
|
|
169
|
+
},
|
|
170
|
+
} satisfies Meta<typeof DefaultStory>;
|
|
159
171
|
|
|
160
172
|
export default meta;
|
|
161
173
|
|
|
162
|
-
type Story = StoryObj<typeof
|
|
174
|
+
type Story = StoryObj<typeof meta>;
|
|
163
175
|
|
|
164
176
|
export const Default: Story = {
|
|
165
177
|
args: {
|
|
166
|
-
|
|
178
|
+
graph: () => convertTreeToGraph(createTree({ depth: 3 })),
|
|
167
179
|
grid: true,
|
|
168
180
|
count: 300,
|
|
169
181
|
interval: 20,
|
|
@@ -188,7 +200,7 @@ export const Default: Story = {
|
|
|
188
200
|
|
|
189
201
|
export const Bullets: Story = {
|
|
190
202
|
args: {
|
|
191
|
-
|
|
203
|
+
graph: () => convertTreeToGraph(createTree({ depth: 4 })),
|
|
192
204
|
children: <span>⌘-DRAG to edge or create node; ⌘-CLICK to delete edge.</span>,
|
|
193
205
|
link: true,
|
|
194
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,11 +5,11 @@
|
|
|
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
|
-
import { createNode, createEdge } from './data';
|
|
11
|
-
import { type TestNode } from './model';
|
|
12
10
|
import { useStateRef } from '../hooks';
|
|
11
|
+
import { createEdge, createNode } from './data';
|
|
12
|
+
import { type TestNode } from './model';
|
|
13
13
|
|
|
14
14
|
export type ObjectMutator<T> = [
|
|
15
15
|
T, // Current value.
|
|
@@ -46,13 +46,13 @@ export const useObjectMutator = <T>(initalValue: T): ObjectMutator<T> => {
|
|
|
46
46
|
/**
|
|
47
47
|
* Test data set generator and mutator.
|
|
48
48
|
*/
|
|
49
|
-
export const useGraphGenerator = (options: { data?: Graph } = {}) => {
|
|
49
|
+
export const useGraphGenerator = (options: { data?: Graph.Any } = {}) => {
|
|
50
50
|
const [data, setData, updateData] = useObjectMutator(options.data || { nodes: [], edges: [] });
|
|
51
51
|
|
|
52
52
|
let interval;
|
|
53
53
|
|
|
54
54
|
const mutator = () => {
|
|
55
|
-
const parent = data.nodes.length ?
|
|
55
|
+
const parent = data.nodes.length ? random.helpers.arrayElement(data.nodes) : undefined;
|
|
56
56
|
const node = createNode();
|
|
57
57
|
|
|
58
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
|
}
|
package/src/util/fraction.ts
CHANGED
package/src/util/screen.test.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import { expect, test } from 'vitest';
|
|
6
6
|
|
|
7
7
|
import { Screen } from './screen';
|
|
8
|
-
import { type
|
|
8
|
+
import { type Point, type Rect } from './types';
|
|
9
9
|
|
|
10
10
|
test('createBounds', () => {
|
|
11
11
|
const tests: { p1: Point; p2: Point; bounds: Rect }[] = [
|
package/src/util/screen.ts
CHANGED