@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,484 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2021 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { line, select, polygonHull, easeCubicOut } from 'd3';
|
|
6
|
+
import * as Clipper from 'js-clipper';
|
|
7
|
+
|
|
8
|
+
import { log } from '@dxos/log';
|
|
9
|
+
|
|
10
|
+
import { createBullets } from './bullets';
|
|
11
|
+
import { Renderer, type RendererOptions } from './renderer';
|
|
12
|
+
import { getCircumferencePoints, type D3Selection, type D3Callable, type Point } from '../../util';
|
|
13
|
+
import { type GraphLayout, type GraphLayoutEdge, type GraphLayoutNode } from '../types';
|
|
14
|
+
|
|
15
|
+
const createLine = line<Point>();
|
|
16
|
+
|
|
17
|
+
export type LabelOptions<NodeData = any> = {
|
|
18
|
+
text: (node: GraphLayoutNode<NodeData>, highlight?: boolean) => string | undefined;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type AttributesOptions<NodeData = any, EdgeData = any> = {
|
|
22
|
+
node?: (node: GraphLayoutNode<NodeData>) => {
|
|
23
|
+
classes?: Record<string, boolean>;
|
|
24
|
+
data?: Record<string, string | undefined>;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
edge?: (edge: GraphLayoutEdge<NodeData, EdgeData>) => {
|
|
28
|
+
classes?: Record<string, boolean>;
|
|
29
|
+
data?: Record<string, string | undefined>;
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type GraphRendererOptions<NodeData = any, EdgeData = any> = RendererOptions<{
|
|
34
|
+
drag?: D3Callable;
|
|
35
|
+
arrows?: {
|
|
36
|
+
start?: boolean; // TODO(burdon): Replace with marker id.
|
|
37
|
+
end?: boolean;
|
|
38
|
+
};
|
|
39
|
+
highlight?: boolean;
|
|
40
|
+
labels?: LabelOptions<NodeData>;
|
|
41
|
+
subgraphs?: boolean;
|
|
42
|
+
attributes?: AttributesOptions<NodeData>;
|
|
43
|
+
transition?: () => any;
|
|
44
|
+
onNodeClick?: (node: GraphLayoutNode<NodeData>, event: MouseEvent) => void;
|
|
45
|
+
onNodePointerEnter?: (node: GraphLayoutNode<NodeData>, event: MouseEvent) => void;
|
|
46
|
+
onLinkClick?: (link: GraphLayoutEdge<NodeData, EdgeData>, event: MouseEvent) => void;
|
|
47
|
+
}>;
|
|
48
|
+
|
|
49
|
+
// TODO(burdon): Perf
|
|
50
|
+
// - Leaking JS event listeners.
|
|
51
|
+
// - Don't update unless data changes.
|
|
52
|
+
// - Cache subgraph components in layout.
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Renders the Graph layout.
|
|
56
|
+
*/
|
|
57
|
+
export class GraphRenderer<NodeData = any, EdgeData = any> extends Renderer<
|
|
58
|
+
GraphLayout<NodeData, EdgeData>,
|
|
59
|
+
GraphRendererOptions<NodeData, EdgeData>
|
|
60
|
+
> {
|
|
61
|
+
override render(layout: GraphLayout<NodeData, EdgeData>) {
|
|
62
|
+
log('render', layout);
|
|
63
|
+
|
|
64
|
+
const root = select(this.root);
|
|
65
|
+
|
|
66
|
+
//
|
|
67
|
+
// Guides
|
|
68
|
+
//
|
|
69
|
+
|
|
70
|
+
if (layout.guides) {
|
|
71
|
+
root
|
|
72
|
+
.selectAll('g.dx-guides')
|
|
73
|
+
.data([{ id: 'guides' }])
|
|
74
|
+
.join('g')
|
|
75
|
+
.classed('dx-guides', true)
|
|
76
|
+
.selectAll<SVGCircleElement, { cx: number; cy: number; r: number }>('circle')
|
|
77
|
+
.data(layout.guides ?? [])
|
|
78
|
+
.join(
|
|
79
|
+
(enter) => enter.append('circle').attr('r', 0),
|
|
80
|
+
(update) => update,
|
|
81
|
+
(exit) => exit.transition().duration(500).attr('r', 0).remove(),
|
|
82
|
+
)
|
|
83
|
+
.attr('cx', (d) => d.cx)
|
|
84
|
+
.attr('cy', (d) => d.cy)
|
|
85
|
+
.attr('r', (d) => d.r);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
//
|
|
89
|
+
// Subgraphs (aka components)
|
|
90
|
+
//
|
|
91
|
+
|
|
92
|
+
const scale = 100;
|
|
93
|
+
const offsetDistance = 24 * scale;
|
|
94
|
+
|
|
95
|
+
if (this._options.subgraphs) {
|
|
96
|
+
// TODO(burdon): Cache components in layout.
|
|
97
|
+
// TODO(burdon): Separate force system for each subgraph.
|
|
98
|
+
const components = this._options.subgraphs
|
|
99
|
+
? findConnectedComponents({
|
|
100
|
+
nodes: layout.graph?.nodes ?? [],
|
|
101
|
+
edges: (layout.graph?.edges ?? []).map(({ source, target }) => ({ source: source.id, target: target.id })),
|
|
102
|
+
})
|
|
103
|
+
.filter((component) => component.length > 2)
|
|
104
|
+
.map((component, i) => ({ id: `subgraph-${i}`, component }))
|
|
105
|
+
: [];
|
|
106
|
+
|
|
107
|
+
root
|
|
108
|
+
.selectAll('g.dx-subgraphs')
|
|
109
|
+
.data([{ id: 'subgraphs' }])
|
|
110
|
+
.join('g')
|
|
111
|
+
.classed('dx-subgraphs', true)
|
|
112
|
+
.selectAll<SVGPathElement, { id: string }>('path')
|
|
113
|
+
.data(components)
|
|
114
|
+
.join(
|
|
115
|
+
(enter) => enter.append('path').classed('dx-subgraph', true),
|
|
116
|
+
(update) => {
|
|
117
|
+
return update.attr('d', ({ component }) => {
|
|
118
|
+
const points: Point[] =
|
|
119
|
+
layout.graph?.nodes.filter((node) => component.includes(node.id)).map((node) => [node.x, node.y]) ?? [];
|
|
120
|
+
|
|
121
|
+
// https://d3js.org/d3-polygon
|
|
122
|
+
const hullPoints = polygonHull(points);
|
|
123
|
+
|
|
124
|
+
// https://www.npmjs.com/package/js-clipper
|
|
125
|
+
const co = new Clipper.ClipperOffset();
|
|
126
|
+
const solution = [];
|
|
127
|
+
const clipperPath = hullPoints.map(([x, y]) => ({ X: x * scale, Y: y * scale }));
|
|
128
|
+
co.AddPath(clipperPath, Clipper.JoinType.jtRound, Clipper.EndType.etClosedPolygon);
|
|
129
|
+
co.Execute(solution, offsetDistance);
|
|
130
|
+
if (solution.length > 0) {
|
|
131
|
+
const offset = solution[0].map(({ X, Y }) => [X / scale, Y / scale]);
|
|
132
|
+
return createLine([...offset, offset[0]]);
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
},
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
//
|
|
140
|
+
// Edges and nodes
|
|
141
|
+
// TODO(burdon): Only call join when data changes (otherwise exit transitions are called multiple times).
|
|
142
|
+
//
|
|
143
|
+
|
|
144
|
+
const edgeGroup = root
|
|
145
|
+
.selectAll('g.dx-edges')
|
|
146
|
+
.data([{ id: 'edges' }], (d: any) => d.id)
|
|
147
|
+
.join('g')
|
|
148
|
+
.classed('dx-edges', true);
|
|
149
|
+
|
|
150
|
+
const nodeGroup = root
|
|
151
|
+
.selectAll('g.dx-nodes')
|
|
152
|
+
.data([{ id: 'nodes' }], (d: any) => d.id)
|
|
153
|
+
.join('g')
|
|
154
|
+
.classed('dx-nodes', true);
|
|
155
|
+
|
|
156
|
+
//
|
|
157
|
+
// Nodes
|
|
158
|
+
//
|
|
159
|
+
|
|
160
|
+
nodeGroup
|
|
161
|
+
.selectAll<SVGCircleElement, GraphLayoutNode<NodeData>>('g.dx-node')
|
|
162
|
+
.data(layout.graph?.nodes ?? [], (d) => d.id)
|
|
163
|
+
.join(
|
|
164
|
+
(enter) =>
|
|
165
|
+
enter
|
|
166
|
+
.append('g')
|
|
167
|
+
.attr('data-id', (d) => d.id)
|
|
168
|
+
.attr('opacity', 1)
|
|
169
|
+
.classed('dx-node', true)
|
|
170
|
+
.call(createNode, this.options),
|
|
171
|
+
(update) => update.call(updateNode, this.options),
|
|
172
|
+
(exit) => {
|
|
173
|
+
// Fade out.
|
|
174
|
+
return exit
|
|
175
|
+
.transition()
|
|
176
|
+
.ease(easeCubicOut)
|
|
177
|
+
.duration(300)
|
|
178
|
+
.attr('opacity', 0)
|
|
179
|
+
.on('end', function (d) {
|
|
180
|
+
select(this).remove();
|
|
181
|
+
});
|
|
182
|
+
},
|
|
183
|
+
)
|
|
184
|
+
.sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
|
|
185
|
+
|
|
186
|
+
//
|
|
187
|
+
// Edges
|
|
188
|
+
//
|
|
189
|
+
|
|
190
|
+
edgeGroup
|
|
191
|
+
.selectAll<SVGPathElement, GraphLayoutEdge<NodeData, EdgeData>>('g.dx-edge')
|
|
192
|
+
.data(layout.graph?.edges ?? [], (d) => d.id)
|
|
193
|
+
.join(
|
|
194
|
+
(enter) =>
|
|
195
|
+
enter
|
|
196
|
+
.append('g')
|
|
197
|
+
.attr('data-id', (d) => d.id)
|
|
198
|
+
.classed('dx-edge', true)
|
|
199
|
+
.call(createEdge, this.options),
|
|
200
|
+
(update) => update.call(updateEdge, this.options, nodeGroup).each((d) => {}),
|
|
201
|
+
(exit) => exit.remove(),
|
|
202
|
+
)
|
|
203
|
+
.sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Trigger path bullets.
|
|
208
|
+
* @param node
|
|
209
|
+
*/
|
|
210
|
+
fireBullet(node: GraphLayoutNode<NodeData>) {
|
|
211
|
+
select(this.root).selectAll('g.dx-edges').selectAll('path').call(createBullets(this.root, node.id));
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Create node elements.
|
|
217
|
+
* @param group
|
|
218
|
+
* @param options
|
|
219
|
+
*/
|
|
220
|
+
const createNode: D3Callable = <Data>(group: D3Selection, options: GraphRendererOptions<Data>) => {
|
|
221
|
+
// Circle.
|
|
222
|
+
const circle = group.append('circle');
|
|
223
|
+
|
|
224
|
+
// Drag.
|
|
225
|
+
// TODO(burdon): Update when layout changes.
|
|
226
|
+
if (options.drag) {
|
|
227
|
+
circle.call(options.drag);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Click.
|
|
231
|
+
if (options.onNodeClick) {
|
|
232
|
+
group.on('click', (event: MouseEvent) => {
|
|
233
|
+
const node = select<SVGElement, GraphLayoutNode<Data>>(event.target as SVGGElement).datum();
|
|
234
|
+
options.onNodeClick(node, event);
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// Label.
|
|
239
|
+
if (options.labels && !options.onNodePointerEnter) {
|
|
240
|
+
const g = group.append('g').classed('dx-label', true);
|
|
241
|
+
g.append('line');
|
|
242
|
+
g.append('rect');
|
|
243
|
+
g.append('text').style('dominant-baseline', 'middle');
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// Hover.
|
|
247
|
+
if (options.onNodePointerEnter) {
|
|
248
|
+
circle.on('pointerenter', function (event: PointerEvent) {
|
|
249
|
+
const node = select<any, GraphLayoutNode<Data>>(this.parentElement).datum();
|
|
250
|
+
options.onNodePointerEnter(node, event);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
group.attr('data-hover', 'handled'); // TODO(burdon): ???
|
|
254
|
+
} else if (options.highlight !== false) {
|
|
255
|
+
circle.on('pointerenter', function () {
|
|
256
|
+
select(this.closest('g.dx-node')).raise();
|
|
257
|
+
if (options.labels) {
|
|
258
|
+
select(this.parentElement).classed('dx-active', true).classed('dx-highlight', true);
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
group.on('pointerleave', function () {
|
|
263
|
+
if (options.labels) {
|
|
264
|
+
select(this).classed('dx-active', false);
|
|
265
|
+
setTimeout(() => {
|
|
266
|
+
if (!select(this).classed('dx-active')) {
|
|
267
|
+
select(this).classed('dx-highlight', false);
|
|
268
|
+
}
|
|
269
|
+
}, 300);
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Update node elements.
|
|
277
|
+
* @param group
|
|
278
|
+
* @param options
|
|
279
|
+
*/
|
|
280
|
+
const updateNode: D3Callable = <NodeData = any, EdgeData = any>(
|
|
281
|
+
group: D3Selection,
|
|
282
|
+
options: GraphRendererOptions<NodeData, EdgeData>,
|
|
283
|
+
) => {
|
|
284
|
+
group.attr('transform', (d) => {
|
|
285
|
+
return d.x != null && d.y != null ? `translate(${d.x},${d.y})` : undefined;
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
// Custom attributes.
|
|
289
|
+
if (options.attributes?.node) {
|
|
290
|
+
try {
|
|
291
|
+
group.each((d, i, nodes) => {
|
|
292
|
+
const node = select(nodes[i]);
|
|
293
|
+
const { classes, data } = options.attributes?.node?.(d) ?? {};
|
|
294
|
+
if (classes) {
|
|
295
|
+
applyClasses(node, classes);
|
|
296
|
+
}
|
|
297
|
+
if (data) {
|
|
298
|
+
Object.entries(data).forEach(([key, value]) => {
|
|
299
|
+
node.attr(`data-${key}`, value);
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
} catch (error: any) {
|
|
304
|
+
log.error('updateNode', { error: error?.message });
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// Optional transition.
|
|
309
|
+
const groupOrTransition: D3Selection = options.transition
|
|
310
|
+
? (group.transition(options.transition()) as unknown as D3Selection)
|
|
311
|
+
: group;
|
|
312
|
+
|
|
313
|
+
// Update circles.
|
|
314
|
+
groupOrTransition.select<SVGCircleElement>('circle').attr('r', (d) => d.r ?? 16);
|
|
315
|
+
|
|
316
|
+
// Update labels.
|
|
317
|
+
if (options.labels) {
|
|
318
|
+
const bx = 1;
|
|
319
|
+
const px = 4;
|
|
320
|
+
const py = 2;
|
|
321
|
+
const offset = 16;
|
|
322
|
+
const dx = (d: any, offset = 0) => (offset + (d.r ?? 0)) * (d.x > 0 ? 1 : -1);
|
|
323
|
+
|
|
324
|
+
groupOrTransition
|
|
325
|
+
.select<SVGTextElement>('text')
|
|
326
|
+
.text((d) => options.labels.text(d))
|
|
327
|
+
.style('text-anchor', (d) => (d.x > 0 ? 'start' : 'end'))
|
|
328
|
+
.attr('dx', (d) => dx(d, offset))
|
|
329
|
+
.attr('dy', 1)
|
|
330
|
+
.text((d) => options.labels.text(d))
|
|
331
|
+
.each(function (d) {
|
|
332
|
+
// Cache bounding box.
|
|
333
|
+
let bbox = d.bbox;
|
|
334
|
+
if (!bbox) {
|
|
335
|
+
bbox = this.getBBox();
|
|
336
|
+
d.bbox = bbox;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const width = bbox.width + (bx + px) * 2;
|
|
340
|
+
const height = bbox.height + py * 2;
|
|
341
|
+
|
|
342
|
+
select(this.parentElement)
|
|
343
|
+
.select('rect')
|
|
344
|
+
.attr('x', dx(d, offset - (bx + px)) + (d.x > 0 ? 0 : -1) * width)
|
|
345
|
+
.attr('y', -height / 2)
|
|
346
|
+
.attr('width', width)
|
|
347
|
+
.attr('height', height)
|
|
348
|
+
.attr('rx', 4);
|
|
349
|
+
|
|
350
|
+
select(this.parentElement)
|
|
351
|
+
.select('line')
|
|
352
|
+
.attr('x1', dx(d, 1))
|
|
353
|
+
.attr('y1', 0)
|
|
354
|
+
.attr('x2', dx(d, offset - (bx + px)))
|
|
355
|
+
.attr('y2', 0);
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Create edge elements.
|
|
362
|
+
* @param group
|
|
363
|
+
* @param options
|
|
364
|
+
* @param nodes
|
|
365
|
+
*/
|
|
366
|
+
const createEdge: D3Callable = <NodeData = any, EdgeData = any>(
|
|
367
|
+
group: D3Selection,
|
|
368
|
+
options: GraphRendererOptions<NodeData, EdgeData>,
|
|
369
|
+
) => {
|
|
370
|
+
if (options.onLinkClick) {
|
|
371
|
+
// Shadow path with wide stroke for click handler.
|
|
372
|
+
group
|
|
373
|
+
.append('path')
|
|
374
|
+
.classed('dx-click', true)
|
|
375
|
+
.on('click', (event: MouseEvent) => {
|
|
376
|
+
const edge = select<SVGLineElement, GraphLayoutEdge<NodeData, EdgeData>>(
|
|
377
|
+
event.target as SVGLineElement,
|
|
378
|
+
).datum();
|
|
379
|
+
options.onLinkClick(edge, event);
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
group
|
|
384
|
+
.append('path')
|
|
385
|
+
.attr('pointer-events', 'none')
|
|
386
|
+
.attr('marker-start', () => (options.arrows?.start ? 'url(#marker-arrow-start)' : undefined))
|
|
387
|
+
.attr('marker-end', () => (options.arrows?.end ? 'url(#marker-arrow-end)' : undefined));
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Update edge elements.
|
|
392
|
+
* @param group
|
|
393
|
+
* @param options
|
|
394
|
+
*/
|
|
395
|
+
const updateEdge: D3Callable = <NodeData = any, EdgeData = any>(
|
|
396
|
+
group: D3Selection,
|
|
397
|
+
options: GraphRendererOptions<NodeData, EdgeData>,
|
|
398
|
+
nodeGroup: D3Selection,
|
|
399
|
+
) => {
|
|
400
|
+
// Custom attributes.
|
|
401
|
+
try {
|
|
402
|
+
group.each((d, i, edges) => {
|
|
403
|
+
const edge = select(edges[i]);
|
|
404
|
+
edge.classed('dx-dashed', d.linkForce === false);
|
|
405
|
+
const { classes, data } = options.attributes?.edge?.(d) ?? {};
|
|
406
|
+
if (classes) {
|
|
407
|
+
applyClasses(edge, classes);
|
|
408
|
+
}
|
|
409
|
+
if (data) {
|
|
410
|
+
Object.entries(data).forEach(([key, value]) => {
|
|
411
|
+
edge.attr(`data-${key}`, value);
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
} catch (error: any) {
|
|
416
|
+
log.error('updateEdge', { error: error?.message });
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
// Optional transition.
|
|
420
|
+
const groupOrTransition: D3Selection = options.transition
|
|
421
|
+
? (group.transition(options.transition()) as unknown as D3Selection)
|
|
422
|
+
: group;
|
|
423
|
+
|
|
424
|
+
groupOrTransition.selectAll<SVGPathElement, GraphLayoutEdge<NodeData, EdgeData>>('path').attr('d', function () {
|
|
425
|
+
// NOTE: `d` is stale after layout is switched so get the datum from the parent element.
|
|
426
|
+
const { source, target } = select(this.parentElement).datum() as GraphLayoutEdge<NodeData, EdgeData>;
|
|
427
|
+
if (!source.initialized || !target.initialized) {
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
return createLine(getCircumferencePoints([source.x, source.y], [target.x, target.y], source.r, target.r));
|
|
432
|
+
});
|
|
433
|
+
};
|
|
434
|
+
|
|
435
|
+
// TODO(burdon): Factor out.
|
|
436
|
+
|
|
437
|
+
const applyClasses = (el: D3Selection, classes: Record<string, boolean>) => {
|
|
438
|
+
for (const [className, value] of Object.entries(classes)) {
|
|
439
|
+
el.classed(className, value);
|
|
440
|
+
}
|
|
441
|
+
};
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* Find connected components (subgraphs) in a graph.
|
|
445
|
+
*/
|
|
446
|
+
const findConnectedComponents = (graph: {
|
|
447
|
+
nodes: { id: string }[];
|
|
448
|
+
edges: { source: string; target: string }[];
|
|
449
|
+
}): string[][] => {
|
|
450
|
+
const adj: Record<string, string[]> = {};
|
|
451
|
+
for (const node of graph.nodes) {
|
|
452
|
+
adj[node.id] = [];
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
for (const { source, target } of graph.edges) {
|
|
456
|
+
adj[source].push(target);
|
|
457
|
+
adj[target].push(source);
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
const visited = new Set<string>();
|
|
461
|
+
const components: string[][] = [];
|
|
462
|
+
for (const node of graph.nodes) {
|
|
463
|
+
if (visited.has(node.id)) {
|
|
464
|
+
continue;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
const stack = [node.id];
|
|
468
|
+
const componentIds: string[] = [];
|
|
469
|
+
|
|
470
|
+
while (stack.length) {
|
|
471
|
+
const current = stack.pop()!;
|
|
472
|
+
if (visited.has(current)) {
|
|
473
|
+
continue;
|
|
474
|
+
}
|
|
475
|
+
visited.add(current);
|
|
476
|
+
componentIds.push(current);
|
|
477
|
+
stack.push(...adj[current]);
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
components.push(componentIds);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
return components;
|
|
484
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2022 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { line } from 'd3';
|
|
6
|
+
|
|
7
|
+
import { type D3Callable, type Point } from '../../util';
|
|
8
|
+
import { getCircumferencePoints } from '../../util';
|
|
9
|
+
import { type GraphLayoutNode } from '../types';
|
|
10
|
+
|
|
11
|
+
const createLine = line();
|
|
12
|
+
|
|
13
|
+
export type LinkOptions = {
|
|
14
|
+
source?: GraphLayoutNode<any>;
|
|
15
|
+
target?: GraphLayoutNode<any>;
|
|
16
|
+
point?: Point;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Render linker while dragging.
|
|
21
|
+
*/
|
|
22
|
+
export const linkerRenderer: D3Callable = (root, { source, target, point }: LinkOptions = {}) => {
|
|
23
|
+
root
|
|
24
|
+
.selectAll('g.dx-linker')
|
|
25
|
+
.data([{ id: 'linker' }])
|
|
26
|
+
.join('g')
|
|
27
|
+
.attr('class', 'dx-linker')
|
|
28
|
+
.selectAll<SVGPathElement, any>('path')
|
|
29
|
+
.data(source ? [{ id: 'edge' }] : [])
|
|
30
|
+
.join('path')
|
|
31
|
+
.attr('marker-end', () => (target ? 'url(#marker-arrow-end)' : 'url(#marker-dot)'))
|
|
32
|
+
.attr('d', () => {
|
|
33
|
+
return createLine(
|
|
34
|
+
getCircumferencePoints(
|
|
35
|
+
[source.x, source.y],
|
|
36
|
+
target ? [target.x, target.y] : point,
|
|
37
|
+
source.r,
|
|
38
|
+
target ? target.r : 0,
|
|
39
|
+
),
|
|
40
|
+
);
|
|
41
|
+
});
|
|
42
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2021 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type RefObject } from 'react';
|
|
6
|
+
|
|
7
|
+
import { invariant } from '@dxos/invariant';
|
|
8
|
+
|
|
9
|
+
import { type SVGContext } from '../../hooks';
|
|
10
|
+
import { defaultIdAccessor, type IdAccessor } from '../types';
|
|
11
|
+
|
|
12
|
+
export type RendererOptions<T = any> = {
|
|
13
|
+
idAccessor: IdAccessor;
|
|
14
|
+
} & T;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Base class for renderer that draw layouts.
|
|
18
|
+
*/
|
|
19
|
+
export abstract class Renderer<LAYOUT, OPTIONS extends RendererOptions> {
|
|
20
|
+
protected readonly _options?: OPTIONS;
|
|
21
|
+
|
|
22
|
+
constructor(
|
|
23
|
+
protected readonly _context: SVGContext,
|
|
24
|
+
protected readonly _root: RefObject<SVGGElement>,
|
|
25
|
+
options?: Partial<OPTIONS>,
|
|
26
|
+
) {
|
|
27
|
+
this._options = Object.assign(
|
|
28
|
+
{
|
|
29
|
+
idAccessor: defaultIdAccessor,
|
|
30
|
+
},
|
|
31
|
+
options,
|
|
32
|
+
) as OPTIONS;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
get context(): SVGContext {
|
|
36
|
+
return this._context;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
get root(): SVGGElement {
|
|
40
|
+
invariant(this._root.current, 'SVG root is not initialized');
|
|
41
|
+
return this._root.current;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
get options() {
|
|
45
|
+
return this._options;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
abstract render(layout: LAYOUT): void;
|
|
49
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2021 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
//
|
|
6
|
+
// Data
|
|
7
|
+
// Generic graph data type.
|
|
8
|
+
//
|
|
9
|
+
|
|
10
|
+
import { type Graph } from '@dxos/graph';
|
|
11
|
+
import { invariant } from '@dxos/invariant';
|
|
12
|
+
|
|
13
|
+
import { type Point } from '../util';
|
|
14
|
+
|
|
15
|
+
export type IdAccessor<N = any> = (node: N) => string;
|
|
16
|
+
|
|
17
|
+
export const defaultIdAccessor: IdAccessor = (node: any) => {
|
|
18
|
+
invariant(node.id);
|
|
19
|
+
return node.id;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export const emptyGraph: Graph = {
|
|
23
|
+
nodes: [],
|
|
24
|
+
edges: [],
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
//
|
|
28
|
+
// Layout
|
|
29
|
+
// Graph layout used by graph renderers.
|
|
30
|
+
//
|
|
31
|
+
|
|
32
|
+
export type GraphGuide = {
|
|
33
|
+
id: string;
|
|
34
|
+
type: string;
|
|
35
|
+
cx: number;
|
|
36
|
+
cy: number;
|
|
37
|
+
r: number;
|
|
38
|
+
classes?: {
|
|
39
|
+
circle?: string;
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export type GraphLayoutNode<NodeData = any> = {
|
|
44
|
+
id: string;
|
|
45
|
+
type?: string;
|
|
46
|
+
order?: number;
|
|
47
|
+
data?: NodeData;
|
|
48
|
+
x?: number;
|
|
49
|
+
y?: number;
|
|
50
|
+
r?: number;
|
|
51
|
+
classes?: {
|
|
52
|
+
circle?: string;
|
|
53
|
+
text?: string;
|
|
54
|
+
};
|
|
55
|
+
children?: number;
|
|
56
|
+
initialized?: boolean;
|
|
57
|
+
last?: Point;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export type GraphLayoutEdge<NodeData = any, EdgeData = any> = {
|
|
61
|
+
id: string;
|
|
62
|
+
type?: string;
|
|
63
|
+
order?: number;
|
|
64
|
+
source: GraphLayoutNode<NodeData>;
|
|
65
|
+
target: GraphLayoutNode<NodeData>;
|
|
66
|
+
data?: EdgeData;
|
|
67
|
+
linkForce?: boolean;
|
|
68
|
+
classes?: {
|
|
69
|
+
path?: string;
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
export type GraphLayout<NodeData = any, EdgeData = any> = {
|
|
74
|
+
id?: string;
|
|
75
|
+
guides?: GraphGuide[];
|
|
76
|
+
graph: {
|
|
77
|
+
nodes: GraphLayoutNode<NodeData>[];
|
|
78
|
+
edges: GraphLayoutEdge<NodeData, EdgeData>[];
|
|
79
|
+
};
|
|
80
|
+
};
|