@dxos/react-ui-graph 0.8.4-main.8360d9e660 → 0.8.4-main.8baae0fced
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 +914 -364
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +14 -14
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- 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 +914 -364
- 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 +14 -14
- package/dist/lib/node-esm/testing/index.mjs.map +3 -3
- package/dist/types/src/components/Graph/Graph.d.ts +8 -4
- package/dist/types/src/components/Graph/Graph.d.ts.map +1 -1
- package/dist/types/src/components/Graph/Graph.stories.d.ts +5 -5
- 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.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-bundle-projector.d.ts +47 -0
- package/dist/types/src/graph/projector/graph-bundle-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-cluster-projector.d.ts +60 -0
- package/dist/types/src/graph/projector/graph-cluster-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-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-lattice-projector.d.ts +23 -0
- package/dist/types/src/graph/projector/graph-lattice-projector.d.ts.map +1 -0
- package/dist/types/src/graph/projector/graph-projector.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-radial-projector.d.ts +6 -0
- package/dist/types/src/graph/projector/graph-radial-projector.d.ts.map +1 -1
- package/dist/types/src/graph/projector/graph-relational-projector.d.ts.map +1 -1
- package/dist/types/src/graph/projector/index.d.ts +3 -0
- package/dist/types/src/graph/projector/index.d.ts.map +1 -1
- package/dist/types/src/graph/projector/projector.d.ts +10 -1
- package/dist/types/src/graph/projector/projector.d.ts.map +1 -1
- package/dist/types/src/graph/renderer/bullets.d.ts.map +1 -1
- package/dist/types/src/graph/renderer/graph-renderer.d.ts +21 -1
- package/dist/types/src/graph/renderer/graph-renderer.d.ts.map +1 -1
- package/dist/types/src/graph/renderer/renderer.d.ts +12 -1
- package/dist/types/src/graph/renderer/renderer.d.ts.map +1 -1
- package/dist/types/src/graph/types.d.ts +14 -0
- package/dist/types/src/graph/types.d.ts.map +1 -1
- package/dist/types/src/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 +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 +21 -19
- package/src/components/Graph/Graph.tsx +47 -19
- package/src/components/Mesh/Mesh.stories.tsx +6 -5
- package/src/components/Mesh/Mesh.tsx +43 -35
- package/src/components/SVG/Root.tsx +30 -5
- package/src/components/SVG/SVG.tsx +0 -1
- package/src/components/SVG/Svg.stories.tsx +2 -3
- package/src/fx/fx.stories.tsx +5 -6
- package/src/graph/projector/graph-bundle-projector.ts +210 -0
- package/src/graph/projector/graph-cluster-projector.ts +325 -0
- package/src/graph/projector/graph-drag.ts +0 -1
- package/src/graph/projector/graph-force-projector.ts +5 -2
- package/src/graph/projector/graph-hierarchical-projector.ts +1 -1
- package/src/graph/projector/graph-lattice-projector.ts +86 -0
- package/src/graph/projector/graph-projector.ts +7 -1
- package/src/graph/projector/graph-radial-projector.ts +30 -3
- package/src/graph/projector/graph-relational-projector.ts +1 -1
- package/src/graph/projector/index.ts +3 -0
- package/src/graph/projector/projector.ts +12 -3
- package/src/graph/renderer/graph-renderer.ts +175 -30
- package/src/graph/renderer/renderer.ts +15 -4
- package/src/graph/types.ts +14 -0
- package/src/stories/hooks.stories.tsx +8 -3
- package/src/testing/data.ts +9 -9
- package/src/testing/hooks.ts +2 -3
- package/src/testing/model.ts +3 -3
- package/styles/graph.css +52 -3
|
@@ -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 & {};
|
|
@@ -71,6 +70,13 @@ export abstract class GraphProjector<NodeData = any, Options extends GraphProjec
|
|
|
71
70
|
}
|
|
72
71
|
|
|
73
72
|
current.data = node;
|
|
73
|
+
// `hidden` is a transient flag set by the cluster projector to fade
|
|
74
|
+
// collapsed leaves. Reset it on every merge so that (a) switching
|
|
75
|
+
// away from cluster to another projector — which inherits the
|
|
76
|
+
// previous layout via `prev` — doesn't carry forward stale hidden
|
|
77
|
+
// flags, and (b) the cluster projector itself re-applies the flag
|
|
78
|
+
// each `doClusterLayout` pass.
|
|
79
|
+
current.hidden = false;
|
|
74
80
|
return current;
|
|
75
81
|
});
|
|
76
82
|
|
|
@@ -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 & {
|
|
@@ -31,10 +30,17 @@ export class GraphRadialProjector<
|
|
|
31
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);
|
|
33
|
+
// Bind enter/exit once before the tween starts; subsequent animate frames emit 'positions'.
|
|
34
|
+
this.emitUpdate('topology');
|
|
34
35
|
this.doRadialLayout();
|
|
35
36
|
}
|
|
36
37
|
|
|
37
38
|
protected animate() {
|
|
39
|
+
// Cancel any in-flight tween so a fresh onUpdate doesn't race against the
|
|
40
|
+
// previous timer (both would be mutating x/y at different rates).
|
|
41
|
+
this._timer?.stop();
|
|
42
|
+
this._timer = undefined;
|
|
43
|
+
|
|
38
44
|
const start = Date.now();
|
|
39
45
|
if (this.options.duration) {
|
|
40
46
|
this._timer = timer(() => {
|
|
@@ -47,17 +53,38 @@ export class GraphRadialProjector<
|
|
|
47
53
|
node.r = node.sr + (node.tr - node.sr) * d;
|
|
48
54
|
});
|
|
49
55
|
|
|
50
|
-
|
|
56
|
+
// Subclass hook: derived projectors (e.g. cluster) can mutate edge.path
|
|
57
|
+
// here so curves track moving endpoints during the tween. The renderer's
|
|
58
|
+
// applyPositions reads edge.path fresh each frame.
|
|
59
|
+
this.onTickFrame(d);
|
|
60
|
+
|
|
61
|
+
// Tween frames only mutate `x/y/r`; emit 'positions' so the renderer can fast-path.
|
|
62
|
+
this.emitUpdate('positions');
|
|
51
63
|
if (t >= 1) {
|
|
52
64
|
this._timer.stop();
|
|
53
65
|
this._timer = undefined;
|
|
54
66
|
}
|
|
55
67
|
});
|
|
56
68
|
} else {
|
|
57
|
-
this
|
|
69
|
+
// No duration: snap nodes to their target. Without this the layout would
|
|
70
|
+
// sit at `sx/sy/sr` (= previous positions) and never reach the new target.
|
|
71
|
+
this.layout.graph.nodes.forEach((node: any) => {
|
|
72
|
+
node.x = node.tx;
|
|
73
|
+
node.y = node.ty;
|
|
74
|
+
node.r = node.tr;
|
|
75
|
+
});
|
|
76
|
+
this.onTickFrame(1);
|
|
77
|
+
this.emitUpdate('positions');
|
|
58
78
|
}
|
|
59
79
|
}
|
|
60
80
|
|
|
81
|
+
/**
|
|
82
|
+
* Per-frame hook called after node x/y/r are interpolated and before the
|
|
83
|
+
* 'positions' emit. Subclasses override to update edge geometry (`edge.path`)
|
|
84
|
+
* so curves stay glued to moving endpoints. `t` is the eased progress in [0, 1].
|
|
85
|
+
*/
|
|
86
|
+
protected onTickFrame(t: number): void {}
|
|
87
|
+
|
|
61
88
|
protected override async onStop() {
|
|
62
89
|
this._timer?.stop();
|
|
63
90
|
this._timer = undefined;
|
|
@@ -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
|
|
|
@@ -27,6 +26,7 @@ export class GraphRelationalProjector<
|
|
|
27
26
|
});
|
|
28
27
|
|
|
29
28
|
this.mergeData(graph);
|
|
29
|
+
this.emitUpdate('topology');
|
|
30
30
|
const selected = this.selection?.getSelectedIds()[0];
|
|
31
31
|
if (selected) {
|
|
32
32
|
const node = this.layout.graph.nodes.find((node) => node.id === selected);
|
|
@@ -2,10 +2,13 @@
|
|
|
2
2
|
// Copyright 2022 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
export * from './graph-bundle-projector';
|
|
6
|
+
export * from './graph-cluster-projector';
|
|
5
7
|
export * from './graph-drag';
|
|
6
8
|
export * from './graph-projector';
|
|
7
9
|
export * from './graph-force-projector';
|
|
8
10
|
export * from './graph-hierarchical-projector';
|
|
11
|
+
export * from './graph-lattice-projector';
|
|
9
12
|
export * from './graph-relational-projector';
|
|
10
13
|
export * from './graph-radial-projector';
|
|
11
14
|
export * from './projector';
|
|
@@ -10,11 +10,20 @@ export type ProjectorOptions = {
|
|
|
10
10
|
idAccessor?: IdAccessor;
|
|
11
11
|
};
|
|
12
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Update kinds signal what changed since the previous render.
|
|
15
|
+
* - `topology`: nodes/edges added or removed, or per-node attributes may have changed.
|
|
16
|
+
* The renderer should perform a full enter/exit join and re-run attribute callbacks.
|
|
17
|
+
* - `positions`: only `x/y` (and similar transient layout state) changed.
|
|
18
|
+
* The renderer can fast-path: just update transforms and edge geometry.
|
|
19
|
+
*/
|
|
20
|
+
export type ProjectorUpdateKind = 'topology' | 'positions';
|
|
21
|
+
|
|
13
22
|
/**
|
|
14
23
|
* Generates a layout to be rendered.
|
|
15
24
|
*/
|
|
16
25
|
export abstract class Projector<NodeData, Layout, Options extends ProjectorOptions> {
|
|
17
|
-
public readonly updated = new EventEmitter<{ layout: Layout }>();
|
|
26
|
+
public readonly updated = new EventEmitter<{ layout: Layout; kind: ProjectorUpdateKind }>();
|
|
18
27
|
|
|
19
28
|
private readonly _options: Options;
|
|
20
29
|
|
|
@@ -48,8 +57,8 @@ export abstract class Projector<NodeData, Layout, Options extends ProjectorOptio
|
|
|
48
57
|
this.onUpdate(data);
|
|
49
58
|
}
|
|
50
59
|
|
|
51
|
-
emitUpdate() {
|
|
52
|
-
this.updated.emit({ layout: this.layout });
|
|
60
|
+
emitUpdate(kind: ProjectorUpdateKind = 'topology') {
|
|
61
|
+
this.updated.emit({ layout: this.layout, kind });
|
|
53
62
|
}
|
|
54
63
|
|
|
55
64
|
async clear() {
|
|
@@ -9,12 +9,26 @@ 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
|
|
|
15
|
+
/**
|
|
16
|
+
* Replace the default `<circle>` with a custom node shape. Invoked once per
|
|
17
|
+
* entering node — append your own elements to the supplied `<g>`. Position and
|
|
18
|
+
* `attributes.node` callbacks are still applied by the renderer.
|
|
19
|
+
*/
|
|
20
|
+
export type RenderNode<NodeData = any> = (group: D3Selection<SVGGElement>, node: GraphLayoutNode<NodeData>) => void;
|
|
21
|
+
|
|
16
22
|
const createLine = line<Point>();
|
|
17
23
|
|
|
24
|
+
/** Duration for edge opacity transitions on enter / exit (ms). Matches node enter/exit timing. */
|
|
25
|
+
const EDGE_FADE_MS = 300;
|
|
26
|
+
|
|
27
|
+
/** Duration for node hide/show opacity transitions (ms). Matches the projector's tween
|
|
28
|
+
* duration so a cluster collapse/expand fades opacity over the same window the position
|
|
29
|
+
* is tweening toward / away from the parent group. */
|
|
30
|
+
const NODE_HIDE_MS = 500;
|
|
31
|
+
|
|
18
32
|
export type LabelOptions<NodeData = any> = {
|
|
19
33
|
text: (node: GraphLayoutNode<NodeData>, highlight?: boolean) => string | undefined;
|
|
20
34
|
};
|
|
@@ -41,17 +55,19 @@ export type GraphRendererOptions<NodeData = any, EdgeData = any> = RendererOptio
|
|
|
41
55
|
labels?: LabelOptions<NodeData>;
|
|
42
56
|
subgraphs?: boolean;
|
|
43
57
|
attributes?: AttributesOptions<NodeData>;
|
|
58
|
+
/**
|
|
59
|
+
* Override the default `<circle>` node shape. When set, the renderer appends
|
|
60
|
+
* whatever this callback creates instead. Use for lattice rects, custom icons, etc.
|
|
61
|
+
*/
|
|
62
|
+
renderNode?: RenderNode<NodeData>;
|
|
44
63
|
transition?: () => any;
|
|
45
64
|
onNodeClick?: (node: GraphLayoutNode<NodeData>, event: MouseEvent) => void;
|
|
46
65
|
onNodePointerEnter?: (node: GraphLayoutNode<NodeData>, event: MouseEvent) => void;
|
|
66
|
+
/** Fires on pointerleave from a node hit-target. Pair with `onNodePointerEnter` to clear hover state. */
|
|
67
|
+
onNodePointerLeave?: (node: GraphLayoutNode<NodeData>, event: MouseEvent) => void;
|
|
47
68
|
onLinkClick?: (link: GraphLayoutEdge<NodeData, EdgeData>, event: MouseEvent) => void;
|
|
48
69
|
}>;
|
|
49
70
|
|
|
50
|
-
// TODO(burdon): Perf
|
|
51
|
-
// - Leaking JS event listeners.
|
|
52
|
-
// - Don't update unless data changes.
|
|
53
|
-
// - Cache subgraph components in layout.
|
|
54
|
-
|
|
55
71
|
/**
|
|
56
72
|
* Renders the Graph layout.
|
|
57
73
|
*/
|
|
@@ -59,7 +75,26 @@ export class GraphRenderer<NodeData = any, EdgeData = any> extends Renderer<
|
|
|
59
75
|
GraphLayout<NodeData, EdgeData>,
|
|
60
76
|
GraphRendererOptions<NodeData, EdgeData>
|
|
61
77
|
> {
|
|
78
|
+
// First render of this instance — used to clear DOM left behind by a previous
|
|
79
|
+
// renderer pointing at the same `<g>` (e.g. when the consumer swaps projectors
|
|
80
|
+
// and a new renderer is constructed for the same SVG root). Without this, the
|
|
81
|
+
// previous variant's node shapes (e.g. circles) would persist as d3.join sees
|
|
82
|
+
// them in the `update` set instead of `enter`.
|
|
83
|
+
#firstRender = true;
|
|
84
|
+
|
|
62
85
|
override render(layout: GraphLayout<NodeData, EdgeData>) {
|
|
86
|
+
// The SVG group ref is unset between mount cycles and before the container has sized;
|
|
87
|
+
// skip the render rather than throw — the projector will emit again on the next tick.
|
|
88
|
+
if (!this.root) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (this.#firstRender) {
|
|
93
|
+
this.#firstRender = false;
|
|
94
|
+
// Drop any DOM left by a prior renderer so this render's `renderNode` runs from scratch.
|
|
95
|
+
this.root.replaceChildren();
|
|
96
|
+
}
|
|
97
|
+
|
|
63
98
|
log('render', layout);
|
|
64
99
|
|
|
65
100
|
const root = select(this.root);
|
|
@@ -166,10 +201,13 @@ export class GraphRenderer<NodeData = any, EdgeData = any> extends Renderer<
|
|
|
166
201
|
enter
|
|
167
202
|
.append('g')
|
|
168
203
|
.attr('data-id', (d) => d.id)
|
|
169
|
-
|
|
204
|
+
// Initial opacity matches `hidden` so a node that enters already-collapsed is
|
|
205
|
+
// invisible from the first frame; updateNode's named 'hide' transition then
|
|
206
|
+
// handles any subsequent toggles.
|
|
207
|
+
.attr('opacity', (d) => (d.hidden ? 0 : 1))
|
|
170
208
|
.classed('dx-node', true)
|
|
171
209
|
.call(createNode, this.options),
|
|
172
|
-
(update) => update
|
|
210
|
+
(update) => update,
|
|
173
211
|
(exit) => {
|
|
174
212
|
// Fade out.
|
|
175
213
|
return exit
|
|
@@ -182,6 +220,9 @@ export class GraphRenderer<NodeData = any, EdgeData = any> extends Renderer<
|
|
|
182
220
|
});
|
|
183
221
|
},
|
|
184
222
|
)
|
|
223
|
+
// Apply update (transform / attributes / labels) to enter+update so the first render
|
|
224
|
+
// also populates `data-*` attributes and transforms — not just subsequent ticks.
|
|
225
|
+
.call(updateNode, this.options)
|
|
185
226
|
.sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
|
|
186
227
|
|
|
187
228
|
//
|
|
@@ -198,17 +239,69 @@ export class GraphRenderer<NodeData = any, EdgeData = any> extends Renderer<
|
|
|
198
239
|
.attr('data-id', (d) => d.id)
|
|
199
240
|
.classed('dx-edge', true)
|
|
200
241
|
.call(createEdge, this.options),
|
|
201
|
-
(update) => update
|
|
202
|
-
(
|
|
242
|
+
(update) => update,
|
|
243
|
+
// Exit fade: edges removed by a topology change (e.g. variant switch from cluster
|
|
244
|
+
// back to force replaces hierarchy edges with data edges) ease out rather than pop.
|
|
245
|
+
(exit) =>
|
|
246
|
+
exit
|
|
247
|
+
.transition()
|
|
248
|
+
.ease(easeCubicOut)
|
|
249
|
+
.duration(EDGE_FADE_MS)
|
|
250
|
+
.attr('opacity', 0)
|
|
251
|
+
.on('end', function () {
|
|
252
|
+
select(this).remove();
|
|
253
|
+
}),
|
|
203
254
|
)
|
|
255
|
+
// Apply edge updates (paths / attributes) to enter+update — see note on nodes above.
|
|
256
|
+
.call(updateEdge, this.options, nodeGroup)
|
|
204
257
|
.sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
|
|
205
258
|
}
|
|
206
259
|
|
|
260
|
+
/**
|
|
261
|
+
* Fast positions-only update. Skips enter/exit, attribute callbacks, label sizing,
|
|
262
|
+
* and the subgraph hull pass; just writes `transform` per node and `d` per edge.
|
|
263
|
+
* Called per simulation tick / animation frame.
|
|
264
|
+
*/
|
|
265
|
+
override applyPositions(layout: GraphLayout<NodeData, EdgeData>) {
|
|
266
|
+
if (!this.root) {
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const root = select(this.root);
|
|
271
|
+
|
|
272
|
+
// Transforms only — no attribute callback, no label measurement.
|
|
273
|
+
root
|
|
274
|
+
.select('g.dx-nodes')
|
|
275
|
+
.selectAll<SVGGElement, GraphLayoutNode<NodeData>>('g.dx-node')
|
|
276
|
+
.attr('transform', (d) => (d.x != null && d.y != null ? `translate(${d.x},${d.y})` : null));
|
|
277
|
+
|
|
278
|
+
// Edge geometry — either precomputed (`edge.path`) or derived from endpoint positions.
|
|
279
|
+
root
|
|
280
|
+
.select('g.dx-edges')
|
|
281
|
+
.selectAll<SVGGElement, GraphLayoutEdge<NodeData, EdgeData>>('g.dx-edge')
|
|
282
|
+
.selectAll<SVGPathElement, GraphLayoutEdge<NodeData, EdgeData>>('path')
|
|
283
|
+
.attr('d', function () {
|
|
284
|
+
// NOTE: `d` is stale after layout is switched so get the datum from the parent element.
|
|
285
|
+
const edge = select(this.parentElement).datum() as GraphLayoutEdge<NodeData, EdgeData>;
|
|
286
|
+
if (edge.path) {
|
|
287
|
+
return edge.path;
|
|
288
|
+
}
|
|
289
|
+
const { source, target } = edge;
|
|
290
|
+
if (!source.initialized || !target.initialized) {
|
|
291
|
+
return null;
|
|
292
|
+
}
|
|
293
|
+
return createLine(getCircumferencePoints([source.x, source.y], [target.x, target.y], source.r, target.r));
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
|
|
207
297
|
/**
|
|
208
298
|
* Trigger path bullets.
|
|
209
299
|
* @param node
|
|
210
300
|
*/
|
|
211
301
|
fireBullet(node: GraphLayoutNode<NodeData>) {
|
|
302
|
+
if (!this.root) {
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
212
305
|
select(this.root).selectAll('g.dx-edges').selectAll('path').call(createBullets(this.root, node.id));
|
|
213
306
|
}
|
|
214
307
|
}
|
|
@@ -218,21 +311,45 @@ export class GraphRenderer<NodeData = any, EdgeData = any> extends Renderer<
|
|
|
218
311
|
* @param group
|
|
219
312
|
* @param options
|
|
220
313
|
*/
|
|
314
|
+
const SHAPE_CLASS = 'dx-shape';
|
|
315
|
+
|
|
316
|
+
const renderCustomShape = <Data>(group: D3Selection, options: GraphRendererOptions<Data>) => {
|
|
317
|
+
group.each(function (d) {
|
|
318
|
+
const g = select<SVGGElement, GraphLayoutNode<Data>>(this);
|
|
319
|
+
// Clear any previous custom shape so subsequent renders pick up updated
|
|
320
|
+
// node.r / node.data without leaving stale elements behind.
|
|
321
|
+
g.selectAll(`.${SHAPE_CLASS}`).remove();
|
|
322
|
+
// Wrap the consumer's drawing in a `<g class="dx-shape">` so we have a
|
|
323
|
+
// stable selector for the cleanup above.
|
|
324
|
+
const shapeGroup = g.append('g').classed(SHAPE_CLASS, true);
|
|
325
|
+
options.renderNode!(shapeGroup, d);
|
|
326
|
+
});
|
|
327
|
+
};
|
|
328
|
+
|
|
221
329
|
const createNode: D3Callable = <Data>(group: D3Selection, options: GraphRendererOptions<Data>) => {
|
|
222
|
-
//
|
|
223
|
-
|
|
330
|
+
// Custom node shape: consumer appends its own elements (e.g. <rect>) and the
|
|
331
|
+
// renderer skips the default <circle>. Drag/hover handlers attach to the
|
|
332
|
+
// wrapping <g> so the custom shape acts as the hit target.
|
|
333
|
+
const useCustom = !!options.renderNode;
|
|
334
|
+
if (useCustom) {
|
|
335
|
+
renderCustomShape(group, options);
|
|
336
|
+
}
|
|
337
|
+
const hitTarget = useCustom ? group : group.append('circle');
|
|
224
338
|
|
|
225
339
|
// Drag.
|
|
226
340
|
// TODO(burdon): Update when layout changes.
|
|
227
341
|
if (options.drag) {
|
|
228
|
-
|
|
342
|
+
hitTarget.call(options.drag);
|
|
229
343
|
}
|
|
230
344
|
|
|
231
|
-
// Click.
|
|
345
|
+
// Click. d3 invokes `on` listeners as `(event, datum)` where datum is the bound data on
|
|
346
|
+
// the listener's host element (the dx-node `<g>`). Use it directly — reading from
|
|
347
|
+
// `event.target` returned undefined for the inner circle / text (which don't inherit
|
|
348
|
+
// __data__), and `this` plus `select(this).datum()` was producing inconsistent results
|
|
349
|
+
// across HMR reloads.
|
|
232
350
|
if (options.onNodeClick) {
|
|
233
|
-
group.on('click', (event: MouseEvent) => {
|
|
234
|
-
|
|
235
|
-
options.onNodeClick(node, event);
|
|
351
|
+
group.on('click', (event: MouseEvent, d: GraphLayoutNode<Data>) => {
|
|
352
|
+
options.onNodeClick(d, event);
|
|
236
353
|
});
|
|
237
354
|
}
|
|
238
355
|
|
|
@@ -245,26 +362,30 @@ const createNode: D3Callable = <Data>(group: D3Selection, options: GraphRenderer
|
|
|
245
362
|
}
|
|
246
363
|
|
|
247
364
|
// Hover.
|
|
248
|
-
if (options.onNodePointerEnter) {
|
|
249
|
-
|
|
250
|
-
const node = select<any, GraphLayoutNode<Data>>(this.parentElement).datum();
|
|
251
|
-
options.onNodePointerEnter(node, event);
|
|
365
|
+
if (options.onNodePointerEnter || options.onNodePointerLeave) {
|
|
366
|
+
hitTarget.on('pointerenter', function (event: PointerEvent) {
|
|
367
|
+
const node = select<any, GraphLayoutNode<Data>>(useCustom ? this : this.parentElement).datum();
|
|
368
|
+
options.onNodePointerEnter?.(node, event);
|
|
369
|
+
});
|
|
370
|
+
hitTarget.on('pointerleave', function (event: PointerEvent) {
|
|
371
|
+
const node = select<any, GraphLayoutNode<Data>>(useCustom ? this : this.parentElement).datum();
|
|
372
|
+
options.onNodePointerLeave?.(node, event);
|
|
252
373
|
});
|
|
253
374
|
|
|
254
|
-
group.attr('data-hover', 'handled');
|
|
255
|
-
} else if (options.highlight !== false) {
|
|
256
|
-
|
|
375
|
+
group.attr('data-hover', 'handled');
|
|
376
|
+
} else if (options.highlight !== false && !useCustom) {
|
|
377
|
+
hitTarget.on('pointerenter', function () {
|
|
257
378
|
select(this.closest('g.dx-node')).raise();
|
|
258
379
|
if (options.labels) {
|
|
259
|
-
select(this.parentElement).classed('dx-active', true).classed('dx-highlight', true);
|
|
380
|
+
select(this.parentElement).classed('dx-node-active', true).classed('dx-highlight', true);
|
|
260
381
|
}
|
|
261
382
|
});
|
|
262
383
|
|
|
263
384
|
group.on('pointerleave', function () {
|
|
264
385
|
if (options.labels) {
|
|
265
|
-
select(this).classed('dx-active', false);
|
|
386
|
+
select(this).classed('dx-node-active', false);
|
|
266
387
|
setTimeout(() => {
|
|
267
|
-
if (!select(this).classed('dx-active')) {
|
|
388
|
+
if (!select(this).classed('dx-node-active')) {
|
|
268
389
|
select(this).classed('dx-highlight', false);
|
|
269
390
|
}
|
|
270
391
|
}, 300);
|
|
@@ -286,6 +407,24 @@ const updateNode: D3Callable = <NodeData = any, EdgeData = any>(
|
|
|
286
407
|
return d.x != null && d.y != null ? `translate(${d.x},${d.y})` : undefined;
|
|
287
408
|
});
|
|
288
409
|
|
|
410
|
+
// Hidden state: drive pointer-events + the hover opacity via the `dx-hidden` class;
|
|
411
|
+
// tween the actual opacity attribute on a named transition so it doesn't fight the
|
|
412
|
+
// CSS hover transition (different namespaces, different durations).
|
|
413
|
+
group.classed('dx-hidden', (d) => d.hidden ?? false);
|
|
414
|
+
group
|
|
415
|
+
.transition('hide')
|
|
416
|
+
.ease(easeCubicOut)
|
|
417
|
+
.duration(NODE_HIDE_MS)
|
|
418
|
+
.attr('opacity', (d) => (d.hidden ? 0 : 1));
|
|
419
|
+
|
|
420
|
+
// Re-run the custom shape on every full render so a topology / data update
|
|
421
|
+
// refreshes geometry (e.g. lattice node.r changes on resize, fill changes on
|
|
422
|
+
// typename change). The cheap fast path (`applyPositions`) doesn't touch
|
|
423
|
+
// shapes, so this only runs on the rarer topology-emit code path.
|
|
424
|
+
if (options.renderNode) {
|
|
425
|
+
renderCustomShape(group, options);
|
|
426
|
+
}
|
|
427
|
+
|
|
289
428
|
// Custom attributes.
|
|
290
429
|
if (options.attributes?.node) {
|
|
291
430
|
try {
|
|
@@ -311,8 +450,10 @@ const updateNode: D3Callable = <NodeData = any, EdgeData = any>(
|
|
|
311
450
|
? (group.transition(options.transition()) as unknown as D3Selection)
|
|
312
451
|
: group;
|
|
313
452
|
|
|
314
|
-
// Update circles.
|
|
315
|
-
|
|
453
|
+
// Update circles. Skipped when the consumer supplied a custom node shape.
|
|
454
|
+
if (!options.renderNode) {
|
|
455
|
+
groupOrTransition.select<SVGCircleElement>('circle').attr('r', (d) => d.r ?? 16);
|
|
456
|
+
}
|
|
316
457
|
|
|
317
458
|
// Update labels.
|
|
318
459
|
if (options.labels) {
|
|
@@ -424,7 +565,11 @@ const updateEdge: D3Callable = <NodeData = any, EdgeData = any>(
|
|
|
424
565
|
|
|
425
566
|
groupOrTransition.selectAll<SVGPathElement, GraphLayoutEdge<NodeData, EdgeData>>('path').attr('d', function () {
|
|
426
567
|
// NOTE: `d` is stale after layout is switched so get the datum from the parent element.
|
|
427
|
-
const
|
|
568
|
+
const edge = select(this.parentElement).datum() as GraphLayoutEdge<NodeData, EdgeData>;
|
|
569
|
+
if (edge.path) {
|
|
570
|
+
return edge.path;
|
|
571
|
+
}
|
|
572
|
+
const { source, target } = edge;
|
|
428
573
|
if (!source.initialized || !target.initialized) {
|
|
429
574
|
return;
|
|
430
575
|
}
|
|
@@ -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
|
|
|
@@ -46,4 +48,13 @@ export abstract class Renderer<LAYOUT, OPTIONS extends RendererOptions> {
|
|
|
46
48
|
}
|
|
47
49
|
|
|
48
50
|
abstract render(layout: LAYOUT): void;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Fast path invoked when only positions changed.
|
|
54
|
+
* Default falls back to a full `render`; subclasses can override to skip
|
|
55
|
+
* enter/exit joins and attribute callbacks for per-tick updates.
|
|
56
|
+
*/
|
|
57
|
+
applyPositions(layout: LAYOUT): void {
|
|
58
|
+
this.render(layout);
|
|
59
|
+
}
|
|
49
60
|
}
|
package/src/graph/types.ts
CHANGED
|
@@ -45,6 +45,14 @@ export type GraphLayoutNode<NodeData = any> = {
|
|
|
45
45
|
type?: string;
|
|
46
46
|
order?: number;
|
|
47
47
|
data?: NodeData;
|
|
48
|
+
/** Human-readable label. Synthetic nodes (e.g. cluster root / group) carry this so
|
|
49
|
+
* consumer renderNode callbacks can label them without parsing internal id formats. */
|
|
50
|
+
label?: string;
|
|
51
|
+
/** When true the node remains in the layout but is rendered with opacity 0 and no
|
|
52
|
+
* pointer events — used by the cluster's collapse animation so hidden leaves can
|
|
53
|
+
* tween to their parent's position rather than popping out, and tween back when the
|
|
54
|
+
* parent is re-expanded. */
|
|
55
|
+
hidden?: boolean;
|
|
48
56
|
x?: number;
|
|
49
57
|
y?: number;
|
|
50
58
|
r?: number;
|
|
@@ -65,6 +73,12 @@ export type GraphLayoutEdge<NodeData = any, EdgeData = any> = {
|
|
|
65
73
|
target: GraphLayoutNode<NodeData>;
|
|
66
74
|
data?: EdgeData;
|
|
67
75
|
linkForce?: boolean;
|
|
76
|
+
/**
|
|
77
|
+
* Precomputed SVG `d` attribute. When set, the renderer uses this instead of
|
|
78
|
+
* deriving a straight-line path from `source/target` positions. Lets projectors
|
|
79
|
+
* own edge geometry (e.g. radial elbows, bundled curves) without renderer changes.
|
|
80
|
+
*/
|
|
81
|
+
path?: string;
|
|
68
82
|
classes?: {
|
|
69
83
|
path?: string;
|
|
70
84
|
};
|
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
// Copyright 2020 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import '../../styles/graph.css';
|
|
6
|
+
|
|
5
7
|
import { RegistryContext } from '@effect-atom/atom-react';
|
|
6
8
|
import { type Meta, type StoryObj } from '@storybook/react-vite';
|
|
7
9
|
import { select } from 'd3';
|
|
@@ -27,8 +29,6 @@ import {
|
|
|
27
29
|
import { useGrid, useSvgContext, useZoom } from '../hooks';
|
|
28
30
|
import { TestGraphModel, type TestNode, convertTreeToGraph, createTree } from '../testing';
|
|
29
31
|
|
|
30
|
-
import '../../styles/graph.css';
|
|
31
|
-
|
|
32
32
|
type ComponentProps = PropsWithChildren<{
|
|
33
33
|
graph: () => Graph.Graph<TestNode, Graph.Edge.Any>;
|
|
34
34
|
projectorOptions?: GraphForceProjectorOptions;
|
|
@@ -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
|
);
|
package/src/testing/data.ts
CHANGED
|
@@ -3,25 +3,25 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { type Graph } from '@dxos/graph';
|
|
6
|
-
import {
|
|
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
26
|
export const createEdge = (source: TestNode, target: TestNode): Graph.Edge.Any => ({
|
|
27
27
|
id: `${source.id}-${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
|
});
|
|
@@ -82,8 +82,8 @@ export const createGraph = (numNodes = 0, numEdges = 0, types?: string[]): Graph
|
|
|
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
|
|
|
@@ -53,7 +52,7 @@ export const useGraphGenerator = (options: { data?: Graph.Any } = {}) => {
|
|
|
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
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { type Graph, GraphModel } from '@dxos/graph';
|
|
6
|
-
import {
|
|
6
|
+
import { random } from '@dxos/random';
|
|
7
7
|
|
|
8
8
|
import { createEdge, createNode } from './data';
|
|
9
9
|
|
|
@@ -14,13 +14,13 @@ export type TestNode = Graph.Node.Any & {
|
|
|
14
14
|
|
|
15
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
|
}
|