@dxos/react-ui-graph 0.8.2-main.10c050d
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 +8 -0
- package/README.md +25 -0
- package/dist/lib/browser/chunk-2YMRPQU7.mjs +979 -0
- package/dist/lib/browser/chunk-2YMRPQU7.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +1546 -0
- package/dist/lib/browser/index.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -0
- package/dist/lib/browser/testing/index.mjs +180 -0
- package/dist/lib/browser/testing/index.mjs.map +7 -0
- package/dist/lib/node/chunk-WFHWEMPB.cjs +919 -0
- package/dist/lib/node/chunk-WFHWEMPB.cjs.map +7 -0
- package/dist/lib/node/index.cjs +1547 -0
- package/dist/lib/node/index.cjs.map +7 -0
- package/dist/lib/node/meta.json +1 -0
- package/dist/lib/node/testing/index.cjs +214 -0
- package/dist/lib/node/testing/index.cjs.map +7 -0
- package/dist/lib/node-esm/chunk-ITHTKQOY.mjs +981 -0
- package/dist/lib/node-esm/chunk-ITHTKQOY.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +1547 -0
- package/dist/lib/node-esm/index.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -0
- package/dist/lib/node-esm/testing/index.mjs +181 -0
- package/dist/lib/node-esm/testing/index.mjs.map +7 -0
- package/dist/types/src/components/Graph/Graph.d.ts +26 -0
- package/dist/types/src/components/Graph/Graph.d.ts.map +1 -0
- package/dist/types/src/components/Graph/Graph.stories.d.ts +29 -0
- package/dist/types/src/components/Graph/Graph.stories.d.ts.map +1 -0
- package/dist/types/src/components/Graph/index.d.ts +2 -0
- package/dist/types/src/components/Graph/index.d.ts.map +1 -0
- package/dist/types/src/components/Mesh/Mesh.d.ts +6 -0
- package/dist/types/src/components/Mesh/Mesh.d.ts.map +1 -0
- package/dist/types/src/components/Mesh/Mesh.stories.d.ts +12 -0
- package/dist/types/src/components/Mesh/Mesh.stories.d.ts.map +1 -0
- package/dist/types/src/components/Mesh/index.d.ts +2 -0
- package/dist/types/src/components/Mesh/index.d.ts.map +1 -0
- package/dist/types/src/components/SVG/Grid.d.ts +9 -0
- package/dist/types/src/components/SVG/Grid.d.ts.map +1 -0
- package/dist/types/src/components/SVG/Markers.d.ts +10 -0
- package/dist/types/src/components/SVG/Markers.d.ts.map +1 -0
- package/dist/types/src/components/SVG/Root.d.ts +12 -0
- package/dist/types/src/components/SVG/Root.d.ts.map +1 -0
- package/dist/types/src/components/SVG/SVG.d.ts +23 -0
- package/dist/types/src/components/SVG/SVG.d.ts.map +1 -0
- package/dist/types/src/components/SVG/Svg.stories.d.ts +17 -0
- package/dist/types/src/components/SVG/Svg.stories.d.ts.map +1 -0
- package/dist/types/src/components/SVG/Zoom.d.ts +12 -0
- package/dist/types/src/components/SVG/Zoom.d.ts.map +1 -0
- package/dist/types/src/components/SVG/index.d.ts +2 -0
- package/dist/types/src/components/SVG/index.d.ts.map +1 -0
- package/dist/types/src/components/index.d.ts +4 -0
- package/dist/types/src/components/index.d.ts.map +1 -0
- package/dist/types/src/fx/fx.stories.d.ts +19 -0
- package/dist/types/src/fx/fx.stories.d.ts.map +1 -0
- package/dist/types/src/fx/index.d.ts +2 -0
- package/dist/types/src/fx/index.d.ts.map +1 -0
- package/dist/types/src/fx/pulsar.d.ts +17 -0
- package/dist/types/src/fx/pulsar.d.ts.map +1 -0
- package/dist/types/src/graph/index.d.ts +5 -0
- package/dist/types/src/graph/index.d.ts.map +1 -0
- package/dist/types/src/graph/markers.d.ts +11 -0
- package/dist/types/src/graph/markers.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-drag.d.ts +16 -0
- package/dist/types/src/graph/projector/graph-drag.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-force-projector.d.ts +96 -0
- package/dist/types/src/graph/projector/graph-force-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-forces.d.ts +8 -0
- package/dist/types/src/graph/projector/graph-forces.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-hierarchical-projector.d.ts +14 -0
- package/dist/types/src/graph/projector/graph-hierarchical-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-projector.d.ts +25 -0
- package/dist/types/src/graph/projector/graph-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-radial-projector.d.ts +34 -0
- package/dist/types/src/graph/projector/graph-radial-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-relational-projector.d.ts +14 -0
- package/dist/types/src/graph/projector/graph-relational-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/index.d.ts +8 -0
- package/dist/types/src/graph/projector/index.d.ts.map +1 -0
- package/dist/types/src/graph/projector/projector.d.ts +32 -0
- package/dist/types/src/graph/projector/projector.d.ts.map +1 -0
- package/dist/types/src/graph/renderer/bullets.d.ts +16 -0
- package/dist/types/src/graph/renderer/bullets.d.ts.map +1 -0
- package/dist/types/src/graph/renderer/graph-renderer.d.ts +43 -0
- package/dist/types/src/graph/renderer/graph-renderer.d.ts.map +1 -0
- package/dist/types/src/graph/renderer/index.d.ts +4 -0
- package/dist/types/src/graph/renderer/index.d.ts.map +1 -0
- package/dist/types/src/graph/renderer/linker.d.ts +12 -0
- package/dist/types/src/graph/renderer/linker.d.ts.map +1 -0
- package/dist/types/src/graph/renderer/renderer.d.ts +20 -0
- package/dist/types/src/graph/renderer/renderer.d.ts.map +1 -0
- package/dist/types/src/graph/types.d.ts +52 -0
- package/dist/types/src/graph/types.d.ts.map +1 -0
- package/dist/types/src/hooks/index.d.ts +5 -0
- package/dist/types/src/hooks/index.d.ts.map +1 -0
- package/dist/types/src/hooks/useGrid.d.ts +24 -0
- package/dist/types/src/hooks/useGrid.d.ts.map +1 -0
- package/dist/types/src/hooks/useStateRef.d.ts +22 -0
- package/dist/types/src/hooks/useStateRef.d.ts.map +1 -0
- package/dist/types/src/hooks/useSvgContext.d.ts +41 -0
- package/dist/types/src/hooks/useSvgContext.d.ts.map +1 -0
- package/dist/types/src/hooks/useZoom.d.ts +34 -0
- package/dist/types/src/hooks/useZoom.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +6 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/stories/hooks.stories.d.ts +22 -0
- package/dist/types/src/stories/hooks.stories.d.ts.map +1 -0
- package/dist/types/src/testing/data.d.ts +27 -0
- package/dist/types/src/testing/data.d.ts.map +1 -0
- package/dist/types/src/testing/data.test.d.ts +2 -0
- package/dist/types/src/testing/data.test.d.ts.map +1 -0
- package/dist/types/src/testing/hooks.d.ts +42 -0
- package/dist/types/src/testing/hooks.d.ts.map +1 -0
- package/dist/types/src/testing/index.d.ts +4 -0
- package/dist/types/src/testing/index.d.ts.map +1 -0
- package/dist/types/src/testing/model.d.ts +14 -0
- package/dist/types/src/testing/model.d.ts.map +1 -0
- package/dist/types/src/util/d3-types.d.ts +5 -0
- package/dist/types/src/util/d3-types.d.ts.map +1 -0
- package/dist/types/src/util/events.d.ts +10 -0
- package/dist/types/src/util/events.d.ts.map +1 -0
- package/dist/types/src/util/fraction.d.ts +65 -0
- package/dist/types/src/util/fraction.d.ts.map +1 -0
- package/dist/types/src/util/fraction.test.d.ts +2 -0
- package/dist/types/src/util/fraction.test.d.ts.map +1 -0
- package/dist/types/src/util/index.d.ts +9 -0
- package/dist/types/src/util/index.d.ts.map +1 -0
- package/dist/types/src/util/points.d.ts +15 -0
- package/dist/types/src/util/points.d.ts.map +1 -0
- package/dist/types/src/util/scale.d.ts +57 -0
- package/dist/types/src/util/scale.d.ts.map +1 -0
- package/dist/types/src/util/scale.test.d.ts +2 -0
- package/dist/types/src/util/scale.test.d.ts.map +1 -0
- package/dist/types/src/util/screen.d.ts +25 -0
- package/dist/types/src/util/screen.d.ts.map +1 -0
- package/dist/types/src/util/screen.test.d.ts +2 -0
- package/dist/types/src/util/screen.test.d.ts.map +1 -0
- package/dist/types/src/util/types.d.ts +4 -0
- package/dist/types/src/util/types.d.ts.map +1 -0
- package/dist/types/src/util/vector.d.ts +48 -0
- package/dist/types/src/util/vector.d.ts.map +1 -0
- package/dist/types/src/util/vector.test.d.ts +2 -0
- package/dist/types/src/util/vector.test.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -0
- package/package.json +86 -0
- package/src/components/Graph/Graph.stories.tsx +457 -0
- package/src/components/Graph/Graph.tsx +130 -0
- package/src/components/Graph/index.ts +5 -0
- package/src/components/Mesh/Mesh.stories.tsx +62 -0
- package/src/components/Mesh/Mesh.tsx +143 -0
- package/src/components/Mesh/index.ts +5 -0
- package/src/components/SVG/Grid.tsx +20 -0
- package/src/components/SVG/Markers.tsx +25 -0
- package/src/components/SVG/Root.tsx +55 -0
- package/src/components/SVG/SVG.tsx +27 -0
- package/src/components/SVG/Svg.stories.tsx +57 -0
- package/src/components/SVG/Zoom.tsx +27 -0
- package/src/components/SVG/index.ts +5 -0
- package/src/components/index.ts +7 -0
- package/src/fx/fx.stories.tsx +116 -0
- package/src/fx/index.ts +5 -0
- package/src/fx/pulsar.ts +58 -0
- package/src/graph/index.ts +8 -0
- package/src/graph/markers.ts +85 -0
- package/src/graph/projector/graph-drag.ts +124 -0
- package/src/graph/projector/graph-force-projector.ts +414 -0
- package/src/graph/projector/graph-forces.ts +35 -0
- package/src/graph/projector/graph-hierarchical-projector.ts +80 -0
- package/src/graph/projector/graph-projector.ts +99 -0
- package/src/graph/projector/graph-radial-projector.ts +142 -0
- package/src/graph/projector/graph-relational-projector.ts +110 -0
- package/src/graph/projector/index.ts +11 -0
- package/src/graph/projector/projector.ts +72 -0
- package/src/graph/renderer/bullets.ts +72 -0
- package/src/graph/renderer/graph-renderer.ts +484 -0
- package/src/graph/renderer/index.ts +7 -0
- package/src/graph/renderer/linker.ts +42 -0
- package/src/graph/renderer/renderer.ts +49 -0
- package/src/graph/types.ts +80 -0
- package/src/hooks/index.ts +8 -0
- package/src/hooks/useGrid.ts +194 -0
- package/src/hooks/useStateRef.ts +60 -0
- package/src/hooks/useSvgContext.ts +96 -0
- package/src/hooks/useZoom.ts +118 -0
- package/src/index.ts +9 -0
- package/src/stories/hooks.stories.tsx +212 -0
- package/src/testing/data.test.ts +12 -0
- package/src/testing/data.ts +100 -0
- package/src/testing/hooks.ts +103 -0
- package/src/testing/index.ts +7 -0
- package/src/testing/model.ts +29 -0
- package/src/typings.d.ts +5 -0
- package/src/util/d3-types.ts +26 -0
- package/src/util/events.ts +24 -0
- package/src/util/fraction.test.ts +76 -0
- package/src/util/fraction.ts +175 -0
- package/src/util/index.ts +12 -0
- package/src/util/points.ts +68 -0
- package/src/util/scale.test.ts +31 -0
- package/src/util/scale.ts +137 -0
- package/src/util/screen.test.ts +41 -0
- package/src/util/screen.ts +91 -0
- package/src/util/types.ts +9 -0
- package/src/util/vector.test.ts +15 -0
- package/src/util/vector.ts +67 -0
- package/styles/graph.css +142 -0
- package/testing.d.ts +11 -0
- package/testing.js +5 -0
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2020 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { line, range, select } from 'd3';
|
|
6
|
+
import { type RefObject, useEffect, useMemo, useRef } from 'react';
|
|
7
|
+
|
|
8
|
+
import { type SVGContext, useSvgContext } from './useSvgContext';
|
|
9
|
+
|
|
10
|
+
const createLine = line();
|
|
11
|
+
|
|
12
|
+
type PathGroup = {
|
|
13
|
+
id: string;
|
|
14
|
+
class: string;
|
|
15
|
+
path: string;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Create grid based on size and current zoom transform.
|
|
20
|
+
*/
|
|
21
|
+
const createGrid = (context: SVGContext, options: GridOptions): PathGroup[] => {
|
|
22
|
+
if (!context.size) {
|
|
23
|
+
return [];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const paths = [];
|
|
27
|
+
const { width, height } = context.size;
|
|
28
|
+
const { x, y, k } = context.scale.transform;
|
|
29
|
+
const s = 1 / k;
|
|
30
|
+
|
|
31
|
+
// Offset.
|
|
32
|
+
const w = width * s;
|
|
33
|
+
const h = height * s;
|
|
34
|
+
const dx = -(x + width / 2) * s;
|
|
35
|
+
const dy = -(y + height / 2) * s;
|
|
36
|
+
|
|
37
|
+
// TODO(burdon): Use the transform to create the range?
|
|
38
|
+
// https://github.com/d3/d3-zoom#transform_rescaleX
|
|
39
|
+
|
|
40
|
+
// Axis.
|
|
41
|
+
if (options.axis) {
|
|
42
|
+
const axis = [
|
|
43
|
+
[
|
|
44
|
+
[0, dy],
|
|
45
|
+
[0, dy + h],
|
|
46
|
+
],
|
|
47
|
+
[
|
|
48
|
+
[dx, 0],
|
|
49
|
+
[dx + w, 0],
|
|
50
|
+
],
|
|
51
|
+
];
|
|
52
|
+
|
|
53
|
+
paths.push({
|
|
54
|
+
id: 'axis',
|
|
55
|
+
class: 'dx-axis',
|
|
56
|
+
path: axis.map((line) => createLine(line as any)).join(),
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Grid.
|
|
61
|
+
if (options.grid) {
|
|
62
|
+
// Scale grid size.
|
|
63
|
+
const mod = (n: number, size: number, delta = 0) => Math.floor(n / size + delta) * size;
|
|
64
|
+
|
|
65
|
+
// Major grid lines.
|
|
66
|
+
const majorSize = context.scale.gridSize;
|
|
67
|
+
const xMajor = range(-mod((x + width / 2) * s, majorSize), mod((-x + width / 2) * s, majorSize, 1), majorSize);
|
|
68
|
+
const yMajor = range(-mod((y + height / 2) * s, majorSize), mod((-y + height / 2) * s, majorSize, 1), majorSize);
|
|
69
|
+
const major = [
|
|
70
|
+
...xMajor
|
|
71
|
+
.filter((x) => !options.axis || x)
|
|
72
|
+
.map((x) => [
|
|
73
|
+
[x, dy],
|
|
74
|
+
[x, dy + h],
|
|
75
|
+
]),
|
|
76
|
+
...yMajor
|
|
77
|
+
.filter((y) => !options.axis || y)
|
|
78
|
+
.map((y) => [
|
|
79
|
+
[dx, y],
|
|
80
|
+
[dx + w, y],
|
|
81
|
+
]),
|
|
82
|
+
];
|
|
83
|
+
|
|
84
|
+
paths.push({
|
|
85
|
+
id: 'major',
|
|
86
|
+
class: 'dx-major',
|
|
87
|
+
path: major.map((line) => createLine(line as any)).join(),
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
// Minor grid lines.
|
|
91
|
+
// Find nearest power of 2 to gridSize.
|
|
92
|
+
// TODO(burdon): Doesn't work is scale is not power of 2.
|
|
93
|
+
const minorSize = Math.pow(2, Math.round(Math.log2(s * context.scale.gridSize)));
|
|
94
|
+
if (majorSize > minorSize) {
|
|
95
|
+
const xMinor = range(-mod((x + width / 2) * s, minorSize), mod((-x + width / 2) * s, minorSize, 1), minorSize);
|
|
96
|
+
const yMinor = range(-mod((y + height / 2) * s, minorSize), mod((-y + height / 2) * s, minorSize, 1), minorSize);
|
|
97
|
+
const minor = [
|
|
98
|
+
...xMinor
|
|
99
|
+
.filter((x) => xMajor.indexOf(x) === -1)
|
|
100
|
+
.map((x) => [
|
|
101
|
+
[x, dy],
|
|
102
|
+
[x, dy + h],
|
|
103
|
+
]),
|
|
104
|
+
...yMinor
|
|
105
|
+
.filter((y) => yMajor.indexOf(y) === -1)
|
|
106
|
+
.map((y) => [
|
|
107
|
+
[dx, y],
|
|
108
|
+
[dx + w, y],
|
|
109
|
+
]),
|
|
110
|
+
];
|
|
111
|
+
|
|
112
|
+
paths.push({
|
|
113
|
+
id: 'minor',
|
|
114
|
+
class: 'dx-minor',
|
|
115
|
+
path: minor.map((line) => createLine(line as any)).join(),
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return paths;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
export type GridOptions = {
|
|
124
|
+
visible?: boolean;
|
|
125
|
+
axis?: boolean;
|
|
126
|
+
grid?: boolean;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const defaultGridOptions: GridOptions = {
|
|
130
|
+
visible: true,
|
|
131
|
+
axis: true,
|
|
132
|
+
grid: true,
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
export class GridController {
|
|
136
|
+
_visible = false;
|
|
137
|
+
|
|
138
|
+
constructor(
|
|
139
|
+
private readonly _ref: RefObject<SVGGElement>,
|
|
140
|
+
private readonly _context: SVGContext,
|
|
141
|
+
private readonly _options: GridOptions,
|
|
142
|
+
) {
|
|
143
|
+
this._visible = this._options.visible ?? true;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
get ref() {
|
|
147
|
+
return this._ref;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
get visible() {
|
|
151
|
+
return this._visible;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
draw(): void {
|
|
155
|
+
if (!this._context.size) {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const paths = this._visible ? createGrid(this._context, this._options) : [];
|
|
160
|
+
const root = select(this._ref.current!);
|
|
161
|
+
|
|
162
|
+
root
|
|
163
|
+
.selectAll<SVGPathElement, PathGroup>('path')
|
|
164
|
+
.data(paths, (path) => path.id)
|
|
165
|
+
.join('path')
|
|
166
|
+
.style('pointer-events', 'none')
|
|
167
|
+
.attr('d', (d) => d.path)
|
|
168
|
+
.attr('class', (d) => d.class);
|
|
169
|
+
|
|
170
|
+
const transform = this._context.scale.transform;
|
|
171
|
+
if (transform) {
|
|
172
|
+
root.attr('transform', transform as any);
|
|
173
|
+
root.attr('stroke-width', 1 / transform.k);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
setVisible(visible: boolean): this {
|
|
178
|
+
this._visible = visible;
|
|
179
|
+
this.draw();
|
|
180
|
+
return this;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Creates a reference to a SVG Group element which renders a grid.
|
|
186
|
+
* @param options
|
|
187
|
+
*/
|
|
188
|
+
export const useGrid = (options: GridOptions = defaultGridOptions): GridController => {
|
|
189
|
+
const ref = useRef<SVGGElement>(null);
|
|
190
|
+
const context = useSvgContext();
|
|
191
|
+
const controller = useMemo(() => new GridController(ref, context, options), []);
|
|
192
|
+
useEffect(() => context.resized.on(() => controller.draw()), [context, controller]);
|
|
193
|
+
return controller;
|
|
194
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2021 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Dispatch, type RefObject, type SetStateAction, useEffect, useRef, useState } from 'react';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Extension of useState to return an up-to-date reference.
|
|
9
|
+
* E.g., to use in callbacks where the state value is stale.
|
|
10
|
+
* @param initialValue
|
|
11
|
+
*
|
|
12
|
+
* @deprecated This function has been deprecated.
|
|
13
|
+
*/
|
|
14
|
+
// TODO(burdon): Remove.
|
|
15
|
+
export const useStateRef = <V>(
|
|
16
|
+
initialValue?: V | (() => V),
|
|
17
|
+
): [V | undefined, Dispatch<SetStateAction<V | undefined>>, RefObject<V | undefined>] => {
|
|
18
|
+
const [value, setValue] = useState<V | undefined>(initialValue);
|
|
19
|
+
const ref = useRef<V>();
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
ref.current = value;
|
|
22
|
+
}, [initialValue, value]);
|
|
23
|
+
|
|
24
|
+
return [value, setValue, ref];
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Extension of useRef that contains computed values based on dependencies.
|
|
29
|
+
* E.g., to use in callbacks where the state value is stale.
|
|
30
|
+
* @param initialValue
|
|
31
|
+
* @param deps
|
|
32
|
+
*/
|
|
33
|
+
// TODO(burdon): Remove.
|
|
34
|
+
export const useDynamicRef = <V>(initialValue: () => V, deps: any[]): RefObject<V> => {
|
|
35
|
+
const [, setValue] = useState<V>(initialValue);
|
|
36
|
+
const ref = useRef<V>(initialValue());
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
ref.current = initialValue();
|
|
39
|
+
// Must update state to trigger render cycle.
|
|
40
|
+
setValue(ref.current);
|
|
41
|
+
}, deps);
|
|
42
|
+
|
|
43
|
+
return ref;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* State setters to force repaint.
|
|
48
|
+
* @param deps
|
|
49
|
+
*/
|
|
50
|
+
export const useTimestamp = (deps?: any[]): [number, () => void, number?] => {
|
|
51
|
+
const [{ timestamp, previous }, setTimestamp] = useState<{
|
|
52
|
+
timestamp: number;
|
|
53
|
+
previous?: number;
|
|
54
|
+
}>({ timestamp: Date.now() });
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
setTimestamp({ timestamp: Date.now(), previous: timestamp });
|
|
57
|
+
}, deps ?? []);
|
|
58
|
+
|
|
59
|
+
return [timestamp, () => setTimestamp({ timestamp: Date.now(), previous: timestamp }), previous];
|
|
60
|
+
};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2022 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type ZoomTransform } from 'd3';
|
|
6
|
+
import { createContext, useContext } from 'react';
|
|
7
|
+
import { type RefObject, createRef } from 'react';
|
|
8
|
+
|
|
9
|
+
import { raise } from '@dxos/debug';
|
|
10
|
+
|
|
11
|
+
import { EventEmitter, type Point, Scale, type Size } from '../util';
|
|
12
|
+
|
|
13
|
+
export type SVGContextOptions = {
|
|
14
|
+
scale?: Scale;
|
|
15
|
+
centered?: boolean;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Contains a reference to the root SVG element and objects and configuration required by child nodes.
|
|
20
|
+
*/
|
|
21
|
+
export class SVGContext {
|
|
22
|
+
private readonly _ref = createRef<SVGSVGElement>();
|
|
23
|
+
|
|
24
|
+
private _scale: Scale;
|
|
25
|
+
private _centered: boolean;
|
|
26
|
+
private _size?: Size;
|
|
27
|
+
private _center?: Point;
|
|
28
|
+
|
|
29
|
+
readonly resized = new EventEmitter<SVGContext>();
|
|
30
|
+
|
|
31
|
+
constructor({ scale = new Scale(), centered = true }: SVGContextOptions) {
|
|
32
|
+
this._scale = scale;
|
|
33
|
+
this._centered = centered;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
get ref(): RefObject<SVGSVGElement> {
|
|
37
|
+
return this._ref;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
get svg(): SVGSVGElement {
|
|
41
|
+
return this._ref.current!;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
get scale(): Scale {
|
|
45
|
+
return this._scale;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
get center(): Point {
|
|
49
|
+
return this._center!;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
get size(): Size | undefined {
|
|
53
|
+
return this._size;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/viewBox
|
|
58
|
+
*/
|
|
59
|
+
get viewBox() {
|
|
60
|
+
const [x, y] = this.center;
|
|
61
|
+
return `${x},${y},${this._size!.width},${this._size!.height}`;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
setSize(size: Size): void {
|
|
65
|
+
this._size = { ...size };
|
|
66
|
+
this._center = this._centered ? [-Math.floor(size.width / 2), -Math.floor(size.height / 2)] : [0, 0];
|
|
67
|
+
this.resized.emit(this);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
setTransform(transform: ZoomTransform): void {
|
|
71
|
+
if (this._scale.setTransform(transform)) {
|
|
72
|
+
this.resized.emit(this);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/Document/elementFromPoint
|
|
78
|
+
* @param point
|
|
79
|
+
*/
|
|
80
|
+
elementFromPoint(point: Point): Element {
|
|
81
|
+
const [x, y] = point;
|
|
82
|
+
const [cx, cy] = this._center!;
|
|
83
|
+
return document.elementFromPoint(cx + x, cy + y)!; // TODO(burdon): Scale?
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const SVGContextType = createContext<SVGContext | undefined>(undefined);
|
|
88
|
+
|
|
89
|
+
export const SVGContextProvider = SVGContextType.Provider;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Get SVG context from the React context.
|
|
93
|
+
*/
|
|
94
|
+
export const useSvgContext = (): SVGContext => {
|
|
95
|
+
return useContext(SVGContextType) ?? raise(new Error('Missing SVGRoot'));
|
|
96
|
+
};
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2022 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { select, zoom, type ZoomBehavior, zoomIdentity, type ZoomTransform } from 'd3';
|
|
6
|
+
import defaultsDeep from 'lodash.defaultsdeep';
|
|
7
|
+
import { type RefObject, useEffect, useMemo, useRef } from 'react';
|
|
8
|
+
|
|
9
|
+
import { type SVGContext, useSvgContext } from './useSvgContext';
|
|
10
|
+
|
|
11
|
+
export type ZoomExtent = [min: number, max: number];
|
|
12
|
+
|
|
13
|
+
export type ZoomOptions = {
|
|
14
|
+
enabled?: boolean;
|
|
15
|
+
extent?: ZoomExtent;
|
|
16
|
+
onDblClick?: (zoom: ZoomHandler) => void;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const defaultZoomOptions: ZoomOptions = {
|
|
20
|
+
enabled: true,
|
|
21
|
+
extent: [1 / 4, 4],
|
|
22
|
+
onDblClick: (zoom: ZoomHandler) => zoom.reset(),
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Zoom API.
|
|
27
|
+
*/
|
|
28
|
+
export class ZoomHandler {
|
|
29
|
+
private readonly _zoom: ZoomBehavior<SVGGElement, unknown>;
|
|
30
|
+
private readonly _options: ZoomOptions;
|
|
31
|
+
private _enabled: boolean;
|
|
32
|
+
|
|
33
|
+
constructor(
|
|
34
|
+
private readonly _ref: RefObject<SVGGElement>,
|
|
35
|
+
private readonly _context: SVGContext,
|
|
36
|
+
options: ZoomOptions,
|
|
37
|
+
) {
|
|
38
|
+
this._options = defaultsDeep({}, options, defaultZoomOptions);
|
|
39
|
+
this._enabled = this._options.enabled ?? true;
|
|
40
|
+
|
|
41
|
+
// https://github.com/d3/d3-zoom#zoom
|
|
42
|
+
this._zoom = zoom<SVGGElement, unknown>().scaleExtent(this._options.extent ?? (defaultZoomOptions.extent as any));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Gets the reference which the transform is applied to.
|
|
47
|
+
*/
|
|
48
|
+
get ref() {
|
|
49
|
+
return this._ref;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
get zoom() {
|
|
53
|
+
return this._zoom;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
init(): this {
|
|
57
|
+
this.setEnabled(this._enabled);
|
|
58
|
+
this.reset(0);
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
setEnabled(enable: boolean): this {
|
|
63
|
+
if (enable) {
|
|
64
|
+
select(this._context.svg)
|
|
65
|
+
.call(this._zoom as any)
|
|
66
|
+
.on(
|
|
67
|
+
'dblclick.zoom',
|
|
68
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
69
|
+
// @ts-ignore
|
|
70
|
+
this._options?.onDblClick
|
|
71
|
+
? () => {
|
|
72
|
+
this._options?.onDblClick!(this);
|
|
73
|
+
}
|
|
74
|
+
: null,
|
|
75
|
+
);
|
|
76
|
+
} else {
|
|
77
|
+
select(this._context.svg).on('.zoom', null); // Unbind the internal event handler.
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
this._enabled = enable;
|
|
81
|
+
return this;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
reset(duration = 500): this {
|
|
85
|
+
// TODO(burdon): Scale to midpoint in extent.
|
|
86
|
+
const scale = 1; // this._options.extent?.[0] ?? 1;
|
|
87
|
+
const transform = zoomIdentity.scale(scale);
|
|
88
|
+
select(this._context.svg)
|
|
89
|
+
.transition()
|
|
90
|
+
.duration(duration)
|
|
91
|
+
.call(this._zoom.transform as any, transform);
|
|
92
|
+
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Creates the zoom handler.
|
|
99
|
+
* @param options
|
|
100
|
+
*/
|
|
101
|
+
export const useZoom = (options: ZoomOptions = defaultZoomOptions): ZoomHandler => {
|
|
102
|
+
const context = useSvgContext();
|
|
103
|
+
const ref = useRef<SVGGElement>(null);
|
|
104
|
+
const handler = useMemo(() => new ZoomHandler(ref, context, options), [context, options]);
|
|
105
|
+
|
|
106
|
+
// TODO(burdon): Distinguish between zoom and pan.
|
|
107
|
+
useEffect(() => {
|
|
108
|
+
// Transform container.
|
|
109
|
+
handler.zoom.on('zoom', ({ transform }: { transform: ZoomTransform }) => {
|
|
110
|
+
context.setTransform(transform); // Fires the resize event (e.g., to update grid).
|
|
111
|
+
select(ref.current!).attr('transform', transform as any);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
handler.init();
|
|
115
|
+
}, [handler]);
|
|
116
|
+
|
|
117
|
+
return handler;
|
|
118
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2020 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import '@dxos-theme';
|
|
6
|
+
|
|
7
|
+
import { type StoryObj } from '@storybook/react';
|
|
8
|
+
import { select } from 'd3';
|
|
9
|
+
import React, { type PropsWithChildren, useEffect, useMemo, useRef } from 'react';
|
|
10
|
+
|
|
11
|
+
import { combine } from '@dxos/async';
|
|
12
|
+
import { log } from '@dxos/log';
|
|
13
|
+
import { type Meta, withLayout, withTheme } from '@dxos/storybook-utils';
|
|
14
|
+
|
|
15
|
+
import { SVG } from '../components';
|
|
16
|
+
import {
|
|
17
|
+
GraphForceProjector,
|
|
18
|
+
type GraphLayoutNode,
|
|
19
|
+
type GraphLayoutEdge,
|
|
20
|
+
GraphRenderer,
|
|
21
|
+
type GraphForceProjectorOptions,
|
|
22
|
+
createMarkers,
|
|
23
|
+
createGraphDrag,
|
|
24
|
+
linkerRenderer,
|
|
25
|
+
} from '../graph';
|
|
26
|
+
import { useSvgContext, useZoom, useGrid } from '../hooks';
|
|
27
|
+
import { convertTreeToGraph, createTree, TestGraphModel, type TestNode } from '../testing';
|
|
28
|
+
|
|
29
|
+
import '../../styles/graph.css';
|
|
30
|
+
|
|
31
|
+
type StoryProps = PropsWithChildren<{
|
|
32
|
+
model: TestGraphModel;
|
|
33
|
+
projectorOptions?: GraphForceProjectorOptions;
|
|
34
|
+
count?: number;
|
|
35
|
+
interval?: number;
|
|
36
|
+
link?: boolean;
|
|
37
|
+
grid?: boolean;
|
|
38
|
+
}>;
|
|
39
|
+
|
|
40
|
+
const DefaultStory = ({ children, ...props }: StoryProps) => {
|
|
41
|
+
return (
|
|
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,
|
|
53
|
+
projectorOptions,
|
|
54
|
+
count = 0,
|
|
55
|
+
interval = 200,
|
|
56
|
+
link = false,
|
|
57
|
+
grid: showGrid = false,
|
|
58
|
+
}: StoryProps) => {
|
|
59
|
+
const context = useSvgContext();
|
|
60
|
+
const graphRef = useRef<SVGGElement>();
|
|
61
|
+
const markersRef = useRef<SVGGElement>();
|
|
62
|
+
const grid = useGrid({ visible: showGrid });
|
|
63
|
+
const zoom = useZoom();
|
|
64
|
+
|
|
65
|
+
const { projector, renderer } = useMemo(() => {
|
|
66
|
+
const projector = new GraphForceProjector(context, projectorOptions);
|
|
67
|
+
let renderer: GraphRenderer<TestNode>;
|
|
68
|
+
if (!link) {
|
|
69
|
+
renderer = new GraphRenderer(context, graphRef);
|
|
70
|
+
} else {
|
|
71
|
+
const drag = createGraphDrag(context, projector, {
|
|
72
|
+
onDrag: (source, target, point) => {
|
|
73
|
+
select(graphRef.current).call(linkerRenderer, { source, target, point });
|
|
74
|
+
},
|
|
75
|
+
onDrop: (source, target) => {
|
|
76
|
+
log('onDrop', { source: source.id, target: target?.id });
|
|
77
|
+
select(graphRef.current).call(linkerRenderer);
|
|
78
|
+
const parent = model.getNode(source.id);
|
|
79
|
+
if (target) {
|
|
80
|
+
const child = model.getNode(target.id);
|
|
81
|
+
model.addEdge({ source: parent.id, target: child.id });
|
|
82
|
+
} else {
|
|
83
|
+
// TODO(burdon): Set start position.
|
|
84
|
+
model.createNodes(parent);
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
renderer = new GraphRenderer<TestNode>(context, graphRef, {
|
|
90
|
+
drag,
|
|
91
|
+
arrows: {
|
|
92
|
+
end: true,
|
|
93
|
+
},
|
|
94
|
+
labels: {
|
|
95
|
+
text: (node: GraphLayoutNode<TestNode>) => node.id.substring(0, 4),
|
|
96
|
+
},
|
|
97
|
+
onNodeClick: (node: GraphLayoutNode<TestNode>, event: MouseEvent) => {
|
|
98
|
+
renderer.fireBullet(node);
|
|
99
|
+
},
|
|
100
|
+
onLinkClick: (edge: GraphLayoutEdge<TestNode>, event: MouseEvent) => {
|
|
101
|
+
if (event.metaKey) {
|
|
102
|
+
model.removeEdge(edge.id);
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return {
|
|
109
|
+
projector,
|
|
110
|
+
renderer,
|
|
111
|
+
};
|
|
112
|
+
}, [link]);
|
|
113
|
+
|
|
114
|
+
useEffect(() => {
|
|
115
|
+
select(markersRef.current).call(createMarkers());
|
|
116
|
+
}, [markersRef]);
|
|
117
|
+
|
|
118
|
+
useEffect(() => {
|
|
119
|
+
void projector.start();
|
|
120
|
+
return combine(
|
|
121
|
+
model.subscribe((graph) => projector.updateData(graph)),
|
|
122
|
+
projector.updated.on(({ layout }) => renderer.render(layout)),
|
|
123
|
+
() => projector.stop(),
|
|
124
|
+
);
|
|
125
|
+
}, []);
|
|
126
|
+
|
|
127
|
+
useEffect(() => {
|
|
128
|
+
if (!count) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const i = setInterval(() => {
|
|
133
|
+
if (model.graph.nodes.length > count) {
|
|
134
|
+
clearInterval(i);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
model.createNodes(model.getRandomNode());
|
|
138
|
+
}, interval);
|
|
139
|
+
|
|
140
|
+
return () => clearInterval(i);
|
|
141
|
+
}, [count, interval]);
|
|
142
|
+
|
|
143
|
+
return (
|
|
144
|
+
<>
|
|
145
|
+
<defs ref={markersRef} />
|
|
146
|
+
<g ref={grid.ref} className='dx-grid' />
|
|
147
|
+
<g ref={zoom.ref}>
|
|
148
|
+
<g ref={graphRef} className='dx-graph' />
|
|
149
|
+
</g>
|
|
150
|
+
</>
|
|
151
|
+
);
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const meta: Meta = {
|
|
155
|
+
title: 'ui/react-ui-graph/hooks',
|
|
156
|
+
render: DefaultStory,
|
|
157
|
+
decorators: [withTheme, withLayout({ fullscreen: true })],
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
export default meta;
|
|
161
|
+
|
|
162
|
+
type Story = StoryObj<typeof DefaultStory>;
|
|
163
|
+
|
|
164
|
+
export const Default: Story = {
|
|
165
|
+
args: {
|
|
166
|
+
model: new TestGraphModel(convertTreeToGraph(createTree({ depth: 3 }))),
|
|
167
|
+
grid: true,
|
|
168
|
+
count: 300,
|
|
169
|
+
interval: 20,
|
|
170
|
+
projectorOptions: {
|
|
171
|
+
guides: true,
|
|
172
|
+
forces: {
|
|
173
|
+
link: {
|
|
174
|
+
distance: 30,
|
|
175
|
+
iterations: 5,
|
|
176
|
+
},
|
|
177
|
+
manyBody: {
|
|
178
|
+
strength: -50,
|
|
179
|
+
},
|
|
180
|
+
radial: {
|
|
181
|
+
strength: 0.2,
|
|
182
|
+
radius: 150,
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
export const Bullets: Story = {
|
|
190
|
+
args: {
|
|
191
|
+
model: new TestGraphModel(convertTreeToGraph(createTree({ depth: 4 }))),
|
|
192
|
+
children: <span>⌘-DRAG to edge or create node; ⌘-CLICK to delete edge.</span>,
|
|
193
|
+
link: true,
|
|
194
|
+
grid: false,
|
|
195
|
+
projectorOptions: {
|
|
196
|
+
guides: true,
|
|
197
|
+
forces: {
|
|
198
|
+
link: {
|
|
199
|
+
distance: 50,
|
|
200
|
+
iterations: 5,
|
|
201
|
+
},
|
|
202
|
+
manyBody: {
|
|
203
|
+
strength: -50,
|
|
204
|
+
},
|
|
205
|
+
radial: {
|
|
206
|
+
strength: 0.1,
|
|
207
|
+
radius: 150,
|
|
208
|
+
},
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
},
|
|
212
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2020 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { expect, test } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import { convertTreeToGraph, createTree } from './data';
|
|
8
|
+
|
|
9
|
+
test('createTree', () => {
|
|
10
|
+
const data = convertTreeToGraph(createTree());
|
|
11
|
+
expect(data).to.exist; // TODO(burdon): Test lengths.
|
|
12
|
+
});
|