@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,1546 @@
|
|
|
1
|
+
import {
|
|
2
|
+
EventEmitter,
|
|
3
|
+
FractionUtil,
|
|
4
|
+
GridController,
|
|
5
|
+
SVGContext,
|
|
6
|
+
SVGContextProvider,
|
|
7
|
+
Scale,
|
|
8
|
+
Screen,
|
|
9
|
+
Vector,
|
|
10
|
+
ZoomHandler,
|
|
11
|
+
getCircumferencePoints,
|
|
12
|
+
normal,
|
|
13
|
+
offsetPoints,
|
|
14
|
+
useDynamicRef,
|
|
15
|
+
useGrid,
|
|
16
|
+
useStateRef,
|
|
17
|
+
useSvgContext,
|
|
18
|
+
useTimestamp,
|
|
19
|
+
useZoom
|
|
20
|
+
} from "./chunk-2YMRPQU7.mjs";
|
|
21
|
+
|
|
22
|
+
// packages/ui/react-ui-graph/src/components/Graph/Graph.tsx
|
|
23
|
+
import { useSignals as _useSignals } from "@preact-signals/safe-react/tracking";
|
|
24
|
+
import { effect } from "@preact/signals-core";
|
|
25
|
+
import React, { forwardRef, useEffect, useImperativeHandle, useMemo, useRef } from "react";
|
|
26
|
+
import { combine } from "@dxos/async";
|
|
27
|
+
import { log as log6 } from "@dxos/log";
|
|
28
|
+
import { mx } from "@dxos/react-ui-theme";
|
|
29
|
+
|
|
30
|
+
// packages/ui/react-ui-graph/src/graph/markers.ts
|
|
31
|
+
import { line, select } from "d3";
|
|
32
|
+
var createLine = line();
|
|
33
|
+
var createArrow = (length, offset, start) => (el) => {
|
|
34
|
+
const height = length * 0.5;
|
|
35
|
+
el.attr("markerWidth", length * 2).attr("markerHeight", height * 2).attr("viewBox", `-${length},-${height},${length * 2},${height * 2}`).attr("orient", "auto").attr("refX", offset).append("path").attr("fill", "none").attr("stroke", "context-stroke").attr("d", createLine([
|
|
36
|
+
start ? [
|
|
37
|
+
length,
|
|
38
|
+
height
|
|
39
|
+
] : [
|
|
40
|
+
-length,
|
|
41
|
+
-height
|
|
42
|
+
],
|
|
43
|
+
[
|
|
44
|
+
0,
|
|
45
|
+
0
|
|
46
|
+
],
|
|
47
|
+
start ? [
|
|
48
|
+
length,
|
|
49
|
+
-height
|
|
50
|
+
] : [
|
|
51
|
+
-length,
|
|
52
|
+
height
|
|
53
|
+
]
|
|
54
|
+
]));
|
|
55
|
+
};
|
|
56
|
+
var createDot = (size) => (el) => {
|
|
57
|
+
el.attr("markerWidth", size * 2).attr("markerHeight", size * 2).attr("viewBox", `-${size},-${size},${size * 2},${size * 2}`).attr("orient", "auto").append("circle").attr("fill", "context-stroke").attr("r", size / 2 - 1);
|
|
58
|
+
};
|
|
59
|
+
var createMarkers = ({ arrowSize = 12 } = {}) => (group) => {
|
|
60
|
+
return group.selectAll("marker").data([
|
|
61
|
+
{
|
|
62
|
+
id: "marker-arrow-start",
|
|
63
|
+
generator: createArrow(arrowSize, -0.5, true),
|
|
64
|
+
className: "dx-arrow"
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
id: "marker-arrow-end",
|
|
68
|
+
generator: createArrow(arrowSize, 0.5, false),
|
|
69
|
+
className: "dx-arrow"
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
id: "marker-dot",
|
|
73
|
+
generator: createDot(6),
|
|
74
|
+
className: "dx-dot"
|
|
75
|
+
}
|
|
76
|
+
]).join("marker").attr("id", (d) => d.id).attr("markerUnits", "strokeWidth").attr("class", (d) => d.className).each((d, i, nodes) => {
|
|
77
|
+
select(nodes[i]).call(d.generator);
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
// packages/ui/react-ui-graph/src/graph/projector/graph-drag.ts
|
|
82
|
+
import { drag, pointer, select as select2 } from "d3";
|
|
83
|
+
var defaultDragOptions = {
|
|
84
|
+
linkMod: "metaKey",
|
|
85
|
+
freezeMod: "shiftKey"
|
|
86
|
+
};
|
|
87
|
+
var createGraphDrag = (context, projector, options = defaultDragOptions) => {
|
|
88
|
+
let mode;
|
|
89
|
+
let source;
|
|
90
|
+
let target;
|
|
91
|
+
let offset;
|
|
92
|
+
let moved = false;
|
|
93
|
+
const keyMod = (event, key) => {
|
|
94
|
+
const modKey = options?.[key] ?? defaultDragOptions[key];
|
|
95
|
+
return modKey === void 0 || event[modKey];
|
|
96
|
+
};
|
|
97
|
+
return drag().filter((event) => !event.ctrlKey).on("start", function(event, d) {
|
|
98
|
+
source = d;
|
|
99
|
+
offset = pointer(event, this.parentElement);
|
|
100
|
+
if (options?.onDrop && keyMod(event.sourceEvent, "linkMod")) {
|
|
101
|
+
mode = 1;
|
|
102
|
+
} else if (keyMod(event.sourceEvent, "dragMod")) {
|
|
103
|
+
mode = 0;
|
|
104
|
+
}
|
|
105
|
+
}).on("drag", function(event, d) {
|
|
106
|
+
const parent = this.closest("g.node");
|
|
107
|
+
select2(parent).classed("dragging", true).raise();
|
|
108
|
+
const [dx, dy] = pointer(event, this.parentElement);
|
|
109
|
+
const x = d.x + dx - offset[0];
|
|
110
|
+
const y = d.y + dy - offset[1];
|
|
111
|
+
const point = [
|
|
112
|
+
x,
|
|
113
|
+
y
|
|
114
|
+
];
|
|
115
|
+
switch (mode) {
|
|
116
|
+
case 0: {
|
|
117
|
+
select2(context.svg).attr("cursor", "grabbing");
|
|
118
|
+
event.subject.x = event.subject.fx = point[0];
|
|
119
|
+
event.subject.y = event.subject.fy = point[1];
|
|
120
|
+
projector.refresh(true);
|
|
121
|
+
moved = true;
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
case 1: {
|
|
125
|
+
if (options?.onDrag) {
|
|
126
|
+
target = projector.findNode(event.x, event.y, 16);
|
|
127
|
+
if (source === target) {
|
|
128
|
+
select2(context.svg).attr("cursor", void 0);
|
|
129
|
+
options?.onDrag?.();
|
|
130
|
+
} else {
|
|
131
|
+
select2(context.svg).attr("cursor", target ? "alias" : "cell");
|
|
132
|
+
options?.onDrag?.(source, target, point);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}).on("end", function(event) {
|
|
138
|
+
select2(context.svg).attr("cursor", void 0);
|
|
139
|
+
select2(this.parentElement).classed("dragging", false);
|
|
140
|
+
switch (mode) {
|
|
141
|
+
case 1: {
|
|
142
|
+
options?.onDrop?.(source, target);
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
if (!keyMod(event.sourceEvent, "freezeMod")) {
|
|
147
|
+
event.subject.fx = null;
|
|
148
|
+
event.subject.fy = null;
|
|
149
|
+
}
|
|
150
|
+
mode = void 0;
|
|
151
|
+
source = void 0;
|
|
152
|
+
target = void 0;
|
|
153
|
+
offset = void 0;
|
|
154
|
+
if (moved) {
|
|
155
|
+
projector.refresh(false);
|
|
156
|
+
moved = false;
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
// packages/ui/react-ui-graph/src/graph/types.ts
|
|
162
|
+
import { invariant } from "@dxos/invariant";
|
|
163
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/ui/react-ui-graph/src/graph/types.ts";
|
|
164
|
+
var defaultIdAccessor = (node) => {
|
|
165
|
+
invariant(node.id, void 0, {
|
|
166
|
+
F: __dxlog_file,
|
|
167
|
+
L: 18,
|
|
168
|
+
S: void 0,
|
|
169
|
+
A: [
|
|
170
|
+
"node.id",
|
|
171
|
+
""
|
|
172
|
+
]
|
|
173
|
+
});
|
|
174
|
+
return node.id;
|
|
175
|
+
};
|
|
176
|
+
var emptyGraph = {
|
|
177
|
+
nodes: [],
|
|
178
|
+
edges: []
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
// packages/ui/react-ui-graph/src/graph/projector/projector.ts
|
|
182
|
+
var Projector = class {
|
|
183
|
+
constructor(_context, options) {
|
|
184
|
+
this._context = _context;
|
|
185
|
+
this.updated = new EventEmitter();
|
|
186
|
+
this._options = Object.assign({
|
|
187
|
+
idAccessor: defaultIdAccessor
|
|
188
|
+
}, options);
|
|
189
|
+
}
|
|
190
|
+
get context() {
|
|
191
|
+
return this._context;
|
|
192
|
+
}
|
|
193
|
+
get options() {
|
|
194
|
+
return this._options;
|
|
195
|
+
}
|
|
196
|
+
refresh(dragging = false) {
|
|
197
|
+
this.onRefresh(dragging);
|
|
198
|
+
}
|
|
199
|
+
updateData(data) {
|
|
200
|
+
this.onUpdate(data);
|
|
201
|
+
}
|
|
202
|
+
emitUpdate() {
|
|
203
|
+
this.updated.emit({
|
|
204
|
+
layout: this.layout
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
async clear() {
|
|
208
|
+
await this.onClear();
|
|
209
|
+
}
|
|
210
|
+
async start() {
|
|
211
|
+
await this.onStart();
|
|
212
|
+
}
|
|
213
|
+
async stop() {
|
|
214
|
+
await this.onStop();
|
|
215
|
+
}
|
|
216
|
+
onRefresh(dragging = false) {
|
|
217
|
+
}
|
|
218
|
+
onUpdate(data) {
|
|
219
|
+
}
|
|
220
|
+
async onClear() {
|
|
221
|
+
}
|
|
222
|
+
async onStart() {
|
|
223
|
+
}
|
|
224
|
+
async onStop() {
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
// packages/ui/react-ui-graph/src/graph/projector/graph-projector.ts
|
|
229
|
+
var GraphProjector = class extends Projector {
|
|
230
|
+
// TODO(burdon): Change to object props.
|
|
231
|
+
constructor(context, options, _selection, layout) {
|
|
232
|
+
super(context, options), this._selection = _selection;
|
|
233
|
+
this._layout = {
|
|
234
|
+
id: (Math.random() * 10).toString().slice(8),
|
|
235
|
+
graph: {
|
|
236
|
+
nodes: [
|
|
237
|
+
...layout?.graph.nodes ?? []
|
|
238
|
+
],
|
|
239
|
+
edges: [
|
|
240
|
+
...layout?.graph.edges ?? []
|
|
241
|
+
]
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
get layout() {
|
|
246
|
+
return this._layout;
|
|
247
|
+
}
|
|
248
|
+
get selection() {
|
|
249
|
+
return this._selection;
|
|
250
|
+
}
|
|
251
|
+
numChildren(node) {
|
|
252
|
+
return this._layout.graph.edges.filter((edge) => edge.source.id === this.options.idAccessor(node)).length;
|
|
253
|
+
}
|
|
254
|
+
reset() {
|
|
255
|
+
this._layout.graph.nodes = [];
|
|
256
|
+
this._layout.graph.edges = [];
|
|
257
|
+
this.emitUpdate();
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Merge external data with internal representation (e.g., so force properties like position are preserved).
|
|
261
|
+
* @param data
|
|
262
|
+
*/
|
|
263
|
+
mergeData(data = emptyGraph) {
|
|
264
|
+
const nodes = data.nodes.map((node) => {
|
|
265
|
+
let current = this._layout.graph.nodes.find((n) => n.id === this.options.idAccessor(node));
|
|
266
|
+
if (!current) {
|
|
267
|
+
current = {
|
|
268
|
+
id: this.options.idAccessor(node)
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
current.data = node;
|
|
272
|
+
return current;
|
|
273
|
+
});
|
|
274
|
+
const edges = data.edges.map((edge) => ({
|
|
275
|
+
id: edge.id,
|
|
276
|
+
source: nodes.find((n) => n.id === edge.source),
|
|
277
|
+
target: nodes.find((n) => n.id === edge.target),
|
|
278
|
+
data: edge.data
|
|
279
|
+
})).filter((edge) => edge.source && edge.target);
|
|
280
|
+
Object.assign(this._layout, {
|
|
281
|
+
graph: {
|
|
282
|
+
nodes,
|
|
283
|
+
edges
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
async onClear() {
|
|
288
|
+
this.reset();
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
// packages/ui/react-ui-graph/src/graph/projector/graph-force-projector.ts
|
|
293
|
+
import { forceCenter, forceCollide, forceLink, forceManyBody, forceRadial, forceSimulation, forceX as forceX2, forceY as forceY2 } from "d3";
|
|
294
|
+
import { log } from "@dxos/log";
|
|
295
|
+
|
|
296
|
+
// packages/ui/react-ui-graph/src/graph/projector/graph-forces.ts
|
|
297
|
+
import { forceX, forceY } from "d3";
|
|
298
|
+
var forcePoint = ({ x, y, strength = 0.01 }) => {
|
|
299
|
+
const fx = x != null ? forceX(x).strength(strength) : void 0;
|
|
300
|
+
const fy = y != null ? forceY(y).strength(strength) : void 0;
|
|
301
|
+
const force = (alpha) => {
|
|
302
|
+
fx?.(alpha);
|
|
303
|
+
fy?.(alpha);
|
|
304
|
+
};
|
|
305
|
+
force.initialize = (nodes, random) => {
|
|
306
|
+
fx?.initialize(nodes, random);
|
|
307
|
+
fy?.initialize(nodes, random);
|
|
308
|
+
};
|
|
309
|
+
return force;
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
// packages/ui/react-ui-graph/src/graph/projector/graph-force-projector.ts
|
|
313
|
+
var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/ui/react-ui-graph/src/graph/projector/graph-force-projector.ts";
|
|
314
|
+
var getValue = (valueOrFunction, invoker, defaultValue) => {
|
|
315
|
+
return (typeof valueOrFunction === "function" ? invoker(valueOrFunction) : valueOrFunction) ?? defaultValue;
|
|
316
|
+
};
|
|
317
|
+
var maybeForce = (options, cb, defaultValue = void 0) => {
|
|
318
|
+
const value = typeof options === "boolean" ? options ? {} : void 0 : options ?? defaultValue;
|
|
319
|
+
if (value) {
|
|
320
|
+
return cb(value);
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
var defaultForceOptions = {
|
|
324
|
+
link: true,
|
|
325
|
+
manyBody: true,
|
|
326
|
+
// center: true,
|
|
327
|
+
point: true
|
|
328
|
+
};
|
|
329
|
+
var GraphForceProjector = class extends GraphProjector {
|
|
330
|
+
constructor() {
|
|
331
|
+
super(...arguments);
|
|
332
|
+
// https://github.com/d3/d3-force
|
|
333
|
+
this._simulation = forceSimulation();
|
|
334
|
+
}
|
|
335
|
+
get forces() {
|
|
336
|
+
return this.options.forces ?? defaultForceOptions;
|
|
337
|
+
}
|
|
338
|
+
get simulation() {
|
|
339
|
+
return this._simulation;
|
|
340
|
+
}
|
|
341
|
+
restart() {
|
|
342
|
+
this._simulation.alpha(1).restart();
|
|
343
|
+
}
|
|
344
|
+
findNode(x, y, radius) {
|
|
345
|
+
return this._simulation.find(x, y, radius);
|
|
346
|
+
}
|
|
347
|
+
async onStart() {
|
|
348
|
+
clearTimeout(this._timeout);
|
|
349
|
+
let propagating = true;
|
|
350
|
+
const { radial, ...forces } = this.forces;
|
|
351
|
+
const delay = typeof radial === "object" ? radial.delay : void 0;
|
|
352
|
+
if (delay) {
|
|
353
|
+
propagating = false;
|
|
354
|
+
this.updateForces(forces);
|
|
355
|
+
this._timeout = setTimeout(() => {
|
|
356
|
+
this.updateForces(this.forces);
|
|
357
|
+
this.restart();
|
|
358
|
+
this._timeout = setTimeout(() => {
|
|
359
|
+
propagating = true;
|
|
360
|
+
}, delay);
|
|
361
|
+
}, delay);
|
|
362
|
+
} else {
|
|
363
|
+
this.updateForces(this.forces);
|
|
364
|
+
}
|
|
365
|
+
this._simulation.on("tick", () => propagating && this.emitUpdate()).velocityDecay(0.3).alphaDecay(1 - Math.pow(1e-3, 1 / 300)).alpha(1).restart();
|
|
366
|
+
}
|
|
367
|
+
async onStop() {
|
|
368
|
+
clearTimeout(this._timeout);
|
|
369
|
+
this._simulation.stop();
|
|
370
|
+
}
|
|
371
|
+
onRefresh(dragging = false) {
|
|
372
|
+
if (this.forces.center) {
|
|
373
|
+
if (dragging) {
|
|
374
|
+
this._simulation.force("center", null);
|
|
375
|
+
} else {
|
|
376
|
+
this.updateForces(this.forces);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
this.restart();
|
|
380
|
+
}
|
|
381
|
+
onUpdate(graph) {
|
|
382
|
+
log("onUpdate", {
|
|
383
|
+
graph: {
|
|
384
|
+
nodes: graph?.nodes.length,
|
|
385
|
+
edges: graph?.edges.length
|
|
386
|
+
}
|
|
387
|
+
}, {
|
|
388
|
+
F: __dxlog_file2,
|
|
389
|
+
L: 226,
|
|
390
|
+
S: this,
|
|
391
|
+
C: (f, a) => f(...a)
|
|
392
|
+
});
|
|
393
|
+
this._simulation.stop();
|
|
394
|
+
this.mergeData(graph);
|
|
395
|
+
this.updateLayout();
|
|
396
|
+
this.updateForces(this.forces);
|
|
397
|
+
this.restart();
|
|
398
|
+
}
|
|
399
|
+
updateLayout() {
|
|
400
|
+
this.layout.guides = this.options.guides ? [
|
|
401
|
+
{
|
|
402
|
+
id: "g-1",
|
|
403
|
+
type: "circle",
|
|
404
|
+
cx: 0,
|
|
405
|
+
cy: 0,
|
|
406
|
+
r: this.context.scale.model.toValue([
|
|
407
|
+
10,
|
|
408
|
+
1
|
|
409
|
+
])
|
|
410
|
+
}
|
|
411
|
+
] : void 0;
|
|
412
|
+
this.layout.graph.nodes.forEach((node) => {
|
|
413
|
+
if (!node.initialized) {
|
|
414
|
+
const a = 2 * Math.PI * Math.random();
|
|
415
|
+
const r = this.options.radius ?? 200;
|
|
416
|
+
Object.assign(node, {
|
|
417
|
+
initialized: true,
|
|
418
|
+
// TODO(burdon): Only when new nodes added to initial layout.
|
|
419
|
+
// Position around center or parent; must have delta to avoid spike.
|
|
420
|
+
// x: edge?.source?.x ?? Math.cos(a) * r,
|
|
421
|
+
// y: edge?.source?.y ?? Math.sin(a) * r,
|
|
422
|
+
x: Math.cos(a) * r,
|
|
423
|
+
y: Math.sin(a) * r
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
Object.assign(node, {
|
|
427
|
+
r: getValue(this.options?.attributes?.radius, (fn) => fn(node, this.numChildren(node)), 6)
|
|
428
|
+
});
|
|
429
|
+
});
|
|
430
|
+
this.layout.graph.edges.forEach((edge) => {
|
|
431
|
+
Object.assign(edge, {
|
|
432
|
+
linkForce: getValue(this.options.attributes?.linkForce, (fn) => fn(edge), true)
|
|
433
|
+
});
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* Update all forces.
|
|
438
|
+
*/
|
|
439
|
+
updateForces(forces) {
|
|
440
|
+
log("updateForces", {
|
|
441
|
+
forces
|
|
442
|
+
}, {
|
|
443
|
+
F: __dxlog_file2,
|
|
444
|
+
L: 287,
|
|
445
|
+
S: this,
|
|
446
|
+
C: (f, a) => f(...a)
|
|
447
|
+
});
|
|
448
|
+
this._simulation.nodes(this.layout.graph.nodes).force("link", maybeForce(forces?.link, (config) => {
|
|
449
|
+
const force = forceLink().id((d) => d.id).links(this.layout.graph.edges.filter((edge) => edge.linkForce));
|
|
450
|
+
if (config.distance != null) {
|
|
451
|
+
force.distance(config.distance);
|
|
452
|
+
}
|
|
453
|
+
if (config.strength != null) {
|
|
454
|
+
force.strength(config.strength);
|
|
455
|
+
}
|
|
456
|
+
if (config.iterations != null) {
|
|
457
|
+
force.iterations(config.iterations);
|
|
458
|
+
}
|
|
459
|
+
return force;
|
|
460
|
+
}, forces?.link === false ? void 0 : {})).force("manyBody", maybeForce(forces?.manyBody, (config) => {
|
|
461
|
+
const force = forceManyBody();
|
|
462
|
+
if (config.distanceMax != null) {
|
|
463
|
+
force.distanceMax(config.distanceMax);
|
|
464
|
+
}
|
|
465
|
+
if (config.strength != null) {
|
|
466
|
+
force.strength(config.strength);
|
|
467
|
+
}
|
|
468
|
+
return force;
|
|
469
|
+
}, forces?.manyBody === false ? void 0 : {})).force("collide", maybeForce(forces?.collide, (config) => {
|
|
470
|
+
const force = forceCollide();
|
|
471
|
+
force.radius(config.radius ?? 8);
|
|
472
|
+
if (config.strength != null) {
|
|
473
|
+
force.strength(config.strength);
|
|
474
|
+
}
|
|
475
|
+
return force;
|
|
476
|
+
})).force("radial", maybeForce(forces?.radial, (config) => {
|
|
477
|
+
const force = forceRadial(config.radius ?? 100, config.x ?? 0, config.y ?? 0);
|
|
478
|
+
if (config.strength != null) {
|
|
479
|
+
force.strength(config.strength);
|
|
480
|
+
}
|
|
481
|
+
return force;
|
|
482
|
+
})).force("center", maybeForce(forces?.center, (config) => {
|
|
483
|
+
const force = forceCenter().x(config.x ?? 0).y(config.y ?? 0);
|
|
484
|
+
if (config.strength != null) {
|
|
485
|
+
force.strength(config.strength);
|
|
486
|
+
}
|
|
487
|
+
return force;
|
|
488
|
+
})).force("point", maybeForce(forces?.point, ({ x = 0, y = 0, strength }) => {
|
|
489
|
+
return forcePoint({
|
|
490
|
+
x,
|
|
491
|
+
y,
|
|
492
|
+
strength
|
|
493
|
+
});
|
|
494
|
+
})).force("x", maybeForce(forces?.x, (config) => {
|
|
495
|
+
const force = forceX2(config.x ?? 0);
|
|
496
|
+
if (config.strength != null) {
|
|
497
|
+
force.strength(config.strength);
|
|
498
|
+
}
|
|
499
|
+
return force;
|
|
500
|
+
})).force("y", maybeForce(forces?.y, (config) => {
|
|
501
|
+
const force = forceY2(config.y ?? 0);
|
|
502
|
+
if (config.strength != null) {
|
|
503
|
+
force.strength(config.strength);
|
|
504
|
+
}
|
|
505
|
+
return force;
|
|
506
|
+
}));
|
|
507
|
+
}
|
|
508
|
+
};
|
|
509
|
+
|
|
510
|
+
// packages/ui/react-ui-graph/src/graph/projector/graph-hierarchical-projector.ts
|
|
511
|
+
import { log as log3 } from "@dxos/log";
|
|
512
|
+
|
|
513
|
+
// packages/ui/react-ui-graph/src/graph/projector/graph-radial-projector.ts
|
|
514
|
+
import { easeCubic, timer } from "d3";
|
|
515
|
+
import { log as log2 } from "@dxos/log";
|
|
516
|
+
var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/ui/react-ui-graph/src/graph/projector/graph-radial-projector.ts";
|
|
517
|
+
var GraphRadialProjector = class extends GraphProjector {
|
|
518
|
+
findNode(x, y, radius) {
|
|
519
|
+
return void 0;
|
|
520
|
+
}
|
|
521
|
+
onUpdate(graph) {
|
|
522
|
+
log2("onUpdate", {
|
|
523
|
+
graph: {
|
|
524
|
+
nodes: graph?.nodes.length,
|
|
525
|
+
edges: graph?.edges.length
|
|
526
|
+
}
|
|
527
|
+
}, {
|
|
528
|
+
F: __dxlog_file3,
|
|
529
|
+
L: 31,
|
|
530
|
+
S: this,
|
|
531
|
+
C: (f, a) => f(...a)
|
|
532
|
+
});
|
|
533
|
+
this.mergeData(graph);
|
|
534
|
+
this.doRadialLayout();
|
|
535
|
+
}
|
|
536
|
+
animate() {
|
|
537
|
+
const start = Date.now();
|
|
538
|
+
if (this.options.duration) {
|
|
539
|
+
this._timer = timer(() => {
|
|
540
|
+
const t = Math.min(1, (Date.now() - start) / this.options.duration);
|
|
541
|
+
const d = easeCubic(t);
|
|
542
|
+
this.layout.graph.nodes.forEach((node) => {
|
|
543
|
+
node.x = node.sx + (node.tx - node.sx) * d;
|
|
544
|
+
node.y = node.sy + (node.ty - node.sy) * d;
|
|
545
|
+
node.r = node.sr + (node.tr - node.sr) * d;
|
|
546
|
+
});
|
|
547
|
+
this.emitUpdate();
|
|
548
|
+
if (t >= 1) {
|
|
549
|
+
this._timer.stop();
|
|
550
|
+
this._timer = void 0;
|
|
551
|
+
}
|
|
552
|
+
});
|
|
553
|
+
} else {
|
|
554
|
+
this.emitUpdate();
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
async onStop() {
|
|
558
|
+
this._timer?.stop();
|
|
559
|
+
this._timer = void 0;
|
|
560
|
+
}
|
|
561
|
+
doRadialLayout() {
|
|
562
|
+
if (!this.context.size) {
|
|
563
|
+
return;
|
|
564
|
+
}
|
|
565
|
+
const radial = layoutRadial(0, 0, this.options.radius ?? Math.min(this.context.size.width, this.context.size.height) / 2 - (this.options.margin ?? 80), this.layout.graph.nodes.length);
|
|
566
|
+
this.layout.graph.nodes.forEach((node, i) => {
|
|
567
|
+
const [tx, ty] = radial(i);
|
|
568
|
+
updateNode(node, [
|
|
569
|
+
tx,
|
|
570
|
+
ty
|
|
571
|
+
]);
|
|
572
|
+
});
|
|
573
|
+
this.animate();
|
|
574
|
+
}
|
|
575
|
+
};
|
|
576
|
+
var updateNode = (node, [tx, ty], tr = 8) => {
|
|
577
|
+
const sx = node.x ?? tx;
|
|
578
|
+
const sy = node.y ?? ty;
|
|
579
|
+
const sr = node.r ?? tr;
|
|
580
|
+
Object.assign(node, {
|
|
581
|
+
initialized: true,
|
|
582
|
+
// Start.
|
|
583
|
+
sx,
|
|
584
|
+
sy,
|
|
585
|
+
sr,
|
|
586
|
+
// Target.
|
|
587
|
+
tx,
|
|
588
|
+
ty,
|
|
589
|
+
tr,
|
|
590
|
+
// Current.
|
|
591
|
+
x: sx,
|
|
592
|
+
y: sy,
|
|
593
|
+
r: sr
|
|
594
|
+
});
|
|
595
|
+
};
|
|
596
|
+
var layoutRadial = (x, y, r, n, start = -Math.PI / 2) => {
|
|
597
|
+
const da = 2 * Math.PI / n;
|
|
598
|
+
return (i) => {
|
|
599
|
+
const a = start + i * da;
|
|
600
|
+
return [
|
|
601
|
+
x + Math.cos(a) * r,
|
|
602
|
+
y + Math.sin(a) * r
|
|
603
|
+
];
|
|
604
|
+
};
|
|
605
|
+
};
|
|
606
|
+
var layoutHorizontal = (x, y, w, n) => {
|
|
607
|
+
const dx = n > 1 ? w / (n - 1) : 0;
|
|
608
|
+
const sx = x - (n - 1) * dx / 2;
|
|
609
|
+
return (i) => {
|
|
610
|
+
return [
|
|
611
|
+
sx + i * dx,
|
|
612
|
+
y
|
|
613
|
+
];
|
|
614
|
+
};
|
|
615
|
+
};
|
|
616
|
+
var layoutVertical = (x, y, h, n) => {
|
|
617
|
+
const dy = n > 1 ? h / (n - 1) : 0;
|
|
618
|
+
const sy = y - (n - 1) * dy / 2;
|
|
619
|
+
return (i) => {
|
|
620
|
+
return [
|
|
621
|
+
x,
|
|
622
|
+
sy + i * dy
|
|
623
|
+
];
|
|
624
|
+
};
|
|
625
|
+
};
|
|
626
|
+
|
|
627
|
+
// packages/ui/react-ui-graph/src/graph/projector/graph-hierarchical-projector.ts
|
|
628
|
+
var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/ui/react-ui-graph/src/graph/projector/graph-hierarchical-projector.ts";
|
|
629
|
+
var GraphHierarchicalProjector = class extends GraphRadialProjector {
|
|
630
|
+
onUpdate(graph) {
|
|
631
|
+
log3("onUpdate", {
|
|
632
|
+
graph: {
|
|
633
|
+
nodes: graph?.nodes.length,
|
|
634
|
+
edges: graph?.edges.length
|
|
635
|
+
},
|
|
636
|
+
selection: this.selection?.selected.value
|
|
637
|
+
}, {
|
|
638
|
+
F: __dxlog_file4,
|
|
639
|
+
L: 23,
|
|
640
|
+
S: this,
|
|
641
|
+
C: (f, a) => f(...a)
|
|
642
|
+
});
|
|
643
|
+
this.mergeData(graph);
|
|
644
|
+
const selected = this.selection?.selected.value?.[0];
|
|
645
|
+
if (selected) {
|
|
646
|
+
const node = this.layout.graph.nodes.find((node2) => node2.id === selected);
|
|
647
|
+
if (node) {
|
|
648
|
+
this.doHierarchicalLayout(node);
|
|
649
|
+
return;
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
this.doRadialLayout();
|
|
653
|
+
}
|
|
654
|
+
doHierarchicalLayout(selected) {
|
|
655
|
+
if (!this.context.size) {
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
658
|
+
const r2 = this.options.radius ?? Math.min(this.context.size.width, this.context.size.height) / 2 - (this.options.margin ?? 80);
|
|
659
|
+
const r1 = r2 / 2;
|
|
660
|
+
const children = this.layout.graph.edges.filter((edge) => edge.source.id === selected.id).map((edge) => edge.target);
|
|
661
|
+
updateNode(selected, [
|
|
662
|
+
0,
|
|
663
|
+
0
|
|
664
|
+
], 20);
|
|
665
|
+
{
|
|
666
|
+
const radial = layoutRadial(0, 0, r1, children.length);
|
|
667
|
+
children.forEach((node, i) => {
|
|
668
|
+
const [tx, ty] = radial(i);
|
|
669
|
+
updateNode(node, [
|
|
670
|
+
tx,
|
|
671
|
+
ty
|
|
672
|
+
]);
|
|
673
|
+
});
|
|
674
|
+
}
|
|
675
|
+
{
|
|
676
|
+
const radial = layoutRadial(0, 0, r2, this.layout.graph.nodes.length - 1 - children.length);
|
|
677
|
+
this.layout.graph.nodes.filter((node) => node.id !== selected.id && !children.includes(node)).forEach((node, i) => {
|
|
678
|
+
const [tx, ty] = radial(i);
|
|
679
|
+
updateNode(node, [
|
|
680
|
+
tx,
|
|
681
|
+
ty
|
|
682
|
+
]);
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
this.animate();
|
|
686
|
+
}
|
|
687
|
+
};
|
|
688
|
+
|
|
689
|
+
// packages/ui/react-ui-graph/src/graph/projector/graph-relational-projector.ts
|
|
690
|
+
import { log as log4 } from "@dxos/log";
|
|
691
|
+
var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/ui/react-ui-graph/src/graph/projector/graph-relational-projector.ts";
|
|
692
|
+
var GraphRelationalProjector = class extends GraphRadialProjector {
|
|
693
|
+
onUpdate(graph) {
|
|
694
|
+
log4("onUpdate", {
|
|
695
|
+
graph: {
|
|
696
|
+
nodes: graph?.nodes.length,
|
|
697
|
+
edges: graph?.edges.length
|
|
698
|
+
},
|
|
699
|
+
selection: this.selection?.selected.value
|
|
700
|
+
}, {
|
|
701
|
+
F: __dxlog_file5,
|
|
702
|
+
L: 23,
|
|
703
|
+
S: this,
|
|
704
|
+
C: (f, a) => f(...a)
|
|
705
|
+
});
|
|
706
|
+
this.mergeData(graph);
|
|
707
|
+
const selected = this.selection?.selected.value?.[0];
|
|
708
|
+
if (selected) {
|
|
709
|
+
const node = this.layout.graph.nodes.find((node2) => node2.id === selected);
|
|
710
|
+
if (node) {
|
|
711
|
+
this.doRelationalLayout(node);
|
|
712
|
+
return;
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
this.doRadialLayout();
|
|
716
|
+
}
|
|
717
|
+
doRelationalLayout(selected) {
|
|
718
|
+
if (!this.context.size) {
|
|
719
|
+
return;
|
|
720
|
+
}
|
|
721
|
+
const r2 = this.options.radius ?? Math.min(this.context.size.width, this.context.size.height) / 2 - (this.options.margin ?? 80);
|
|
722
|
+
const r1 = r2 * 0.5;
|
|
723
|
+
this.layout.graph.nodes.forEach((node) => node.type = void 0);
|
|
724
|
+
this.layout.graph.edges.forEach((edge) => {
|
|
725
|
+
edge.order = 0;
|
|
726
|
+
edge.type = void 0;
|
|
727
|
+
});
|
|
728
|
+
const children = this.layout.graph.edges.filter((edge) => edge.source.id === selected.id).map((edge) => {
|
|
729
|
+
edge.order = 1;
|
|
730
|
+
edge.type = "1";
|
|
731
|
+
edge.target.type = "1";
|
|
732
|
+
return edge.target;
|
|
733
|
+
});
|
|
734
|
+
const parents = this.layout.graph.edges.filter((edge) => edge.target.id === selected.id).map((edge) => {
|
|
735
|
+
edge.order = 2;
|
|
736
|
+
edge.type = "2";
|
|
737
|
+
edge.source.type = "2";
|
|
738
|
+
return edge.source;
|
|
739
|
+
});
|
|
740
|
+
updateNode(selected, [
|
|
741
|
+
0,
|
|
742
|
+
0
|
|
743
|
+
], 30);
|
|
744
|
+
{
|
|
745
|
+
const vertical = layoutHorizontal(0, r1, r1 * 2, children.length);
|
|
746
|
+
children.forEach((node, i) => {
|
|
747
|
+
const [tx, ty] = vertical(i);
|
|
748
|
+
updateNode(node, [
|
|
749
|
+
tx,
|
|
750
|
+
ty
|
|
751
|
+
], 20);
|
|
752
|
+
});
|
|
753
|
+
}
|
|
754
|
+
{
|
|
755
|
+
const vertical = layoutHorizontal(0, -r1, r1, parents.length);
|
|
756
|
+
parents.forEach((node, i) => {
|
|
757
|
+
const [tx, ty] = vertical(i);
|
|
758
|
+
updateNode(node, [
|
|
759
|
+
tx,
|
|
760
|
+
ty
|
|
761
|
+
], 20);
|
|
762
|
+
});
|
|
763
|
+
}
|
|
764
|
+
{
|
|
765
|
+
const radial = layoutRadial(0, 0, r2, this.layout.graph.nodes.length - 1 - children.length - parents.length);
|
|
766
|
+
this.layout.graph.nodes.filter((node) => node.id !== selected.id && !children.includes(node) && !parents.includes(node)).forEach((node, i) => {
|
|
767
|
+
const [tx, ty] = radial(i);
|
|
768
|
+
updateNode(node, [
|
|
769
|
+
tx,
|
|
770
|
+
ty
|
|
771
|
+
]);
|
|
772
|
+
});
|
|
773
|
+
}
|
|
774
|
+
this.animate();
|
|
775
|
+
}
|
|
776
|
+
};
|
|
777
|
+
|
|
778
|
+
// packages/ui/react-ui-graph/src/graph/renderer/graph-renderer.ts
|
|
779
|
+
import { line as line2, select as select4, polygonHull, easeCubicOut } from "d3";
|
|
780
|
+
import * as Clipper from "js-clipper";
|
|
781
|
+
import { log as log5 } from "@dxos/log";
|
|
782
|
+
|
|
783
|
+
// packages/ui/react-ui-graph/src/graph/renderer/bullets.ts
|
|
784
|
+
import { easeLinear, interpolate, select as select3 } from "d3";
|
|
785
|
+
var createBullets = (group, nodeId, options = {}) => {
|
|
786
|
+
return (selection) => {
|
|
787
|
+
const { max = 32, radius = 3, delay = 50, minDuration = 100, maxDuration = 500 } = options;
|
|
788
|
+
selection.each(function(d, i, edges) {
|
|
789
|
+
const { source, target } = d;
|
|
790
|
+
if (source.id === nodeId) {
|
|
791
|
+
const path = select3(edges[i]);
|
|
792
|
+
const p = path.node().getPointAtLength(0);
|
|
793
|
+
const bullet = select3(group).append("circle").attr("class", "dx-bullet").attr("cx", p.x).attr("cy", p.y).attr("r", radius);
|
|
794
|
+
bullet.transition().delay(delay).duration(minDuration + Math.random() * maxDuration).ease(easeLinear).tween("pathTween", function() {
|
|
795
|
+
const length = path.node().getTotalLength();
|
|
796
|
+
const r = interpolate(0, length);
|
|
797
|
+
return (t) => {
|
|
798
|
+
const point = path.node().getPointAtLength(r(t));
|
|
799
|
+
select3(this).attr("cx", point.x).attr("cy", point.y);
|
|
800
|
+
};
|
|
801
|
+
}).on("end", function() {
|
|
802
|
+
select3(this).remove();
|
|
803
|
+
const num = select3(group).selectAll("circle.dx-bullet").size();
|
|
804
|
+
if (num < max) {
|
|
805
|
+
selection.call(createBullets(group, target.id));
|
|
806
|
+
}
|
|
807
|
+
});
|
|
808
|
+
}
|
|
809
|
+
});
|
|
810
|
+
};
|
|
811
|
+
};
|
|
812
|
+
|
|
813
|
+
// packages/ui/react-ui-graph/src/graph/renderer/renderer.ts
|
|
814
|
+
import { invariant as invariant2 } from "@dxos/invariant";
|
|
815
|
+
var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/ui/react-ui-graph/src/graph/renderer/renderer.ts";
|
|
816
|
+
var Renderer = class {
|
|
817
|
+
constructor(_context, _root, options) {
|
|
818
|
+
this._context = _context;
|
|
819
|
+
this._root = _root;
|
|
820
|
+
this._options = Object.assign({
|
|
821
|
+
idAccessor: defaultIdAccessor
|
|
822
|
+
}, options);
|
|
823
|
+
}
|
|
824
|
+
get context() {
|
|
825
|
+
return this._context;
|
|
826
|
+
}
|
|
827
|
+
get root() {
|
|
828
|
+
invariant2(this._root.current, "SVG root is not initialized", {
|
|
829
|
+
F: __dxlog_file6,
|
|
830
|
+
L: 40,
|
|
831
|
+
S: this,
|
|
832
|
+
A: [
|
|
833
|
+
"this._root.current",
|
|
834
|
+
"'SVG root is not initialized'"
|
|
835
|
+
]
|
|
836
|
+
});
|
|
837
|
+
return this._root.current;
|
|
838
|
+
}
|
|
839
|
+
get options() {
|
|
840
|
+
return this._options;
|
|
841
|
+
}
|
|
842
|
+
};
|
|
843
|
+
|
|
844
|
+
// packages/ui/react-ui-graph/src/graph/renderer/graph-renderer.ts
|
|
845
|
+
var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/ui/react-ui-graph/src/graph/renderer/graph-renderer.ts";
|
|
846
|
+
var createLine2 = line2();
|
|
847
|
+
var GraphRenderer = class extends Renderer {
|
|
848
|
+
render(layout) {
|
|
849
|
+
log5("render", layout, {
|
|
850
|
+
F: __dxlog_file7,
|
|
851
|
+
L: 62,
|
|
852
|
+
S: this,
|
|
853
|
+
C: (f, a) => f(...a)
|
|
854
|
+
});
|
|
855
|
+
const root = select4(this.root);
|
|
856
|
+
if (layout.guides) {
|
|
857
|
+
root.selectAll("g.dx-guides").data([
|
|
858
|
+
{
|
|
859
|
+
id: "guides"
|
|
860
|
+
}
|
|
861
|
+
]).join("g").classed("dx-guides", true).selectAll("circle").data(layout.guides ?? []).join((enter) => enter.append("circle").attr("r", 0), (update) => update, (exit) => exit.transition().duration(500).attr("r", 0).remove()).attr("cx", (d) => d.cx).attr("cy", (d) => d.cy).attr("r", (d) => d.r);
|
|
862
|
+
}
|
|
863
|
+
const scale = 100;
|
|
864
|
+
const offsetDistance = 24 * scale;
|
|
865
|
+
if (this._options.subgraphs) {
|
|
866
|
+
const components = this._options.subgraphs ? findConnectedComponents({
|
|
867
|
+
nodes: layout.graph?.nodes ?? [],
|
|
868
|
+
edges: (layout.graph?.edges ?? []).map(({ source, target }) => ({
|
|
869
|
+
source: source.id,
|
|
870
|
+
target: target.id
|
|
871
|
+
}))
|
|
872
|
+
}).filter((component) => component.length > 2).map((component, i) => ({
|
|
873
|
+
id: `subgraph-${i}`,
|
|
874
|
+
component
|
|
875
|
+
})) : [];
|
|
876
|
+
root.selectAll("g.dx-subgraphs").data([
|
|
877
|
+
{
|
|
878
|
+
id: "subgraphs"
|
|
879
|
+
}
|
|
880
|
+
]).join("g").classed("dx-subgraphs", true).selectAll("path").data(components).join((enter) => enter.append("path").classed("dx-subgraph", true), (update) => {
|
|
881
|
+
return update.attr("d", ({ component }) => {
|
|
882
|
+
const points = layout.graph?.nodes.filter((node) => component.includes(node.id)).map((node) => [
|
|
883
|
+
node.x,
|
|
884
|
+
node.y
|
|
885
|
+
]) ?? [];
|
|
886
|
+
const hullPoints = polygonHull(points);
|
|
887
|
+
const co = new Clipper.ClipperOffset();
|
|
888
|
+
const solution = [];
|
|
889
|
+
const clipperPath = hullPoints.map(([x, y]) => ({
|
|
890
|
+
X: x * scale,
|
|
891
|
+
Y: y * scale
|
|
892
|
+
}));
|
|
893
|
+
co.AddPath(clipperPath, Clipper.JoinType.jtRound, Clipper.EndType.etClosedPolygon);
|
|
894
|
+
co.Execute(solution, offsetDistance);
|
|
895
|
+
if (solution.length > 0) {
|
|
896
|
+
const offset = solution[0].map(({ X, Y }) => [
|
|
897
|
+
X / scale,
|
|
898
|
+
Y / scale
|
|
899
|
+
]);
|
|
900
|
+
return createLine2([
|
|
901
|
+
...offset,
|
|
902
|
+
offset[0]
|
|
903
|
+
]);
|
|
904
|
+
}
|
|
905
|
+
});
|
|
906
|
+
});
|
|
907
|
+
}
|
|
908
|
+
const edgeGroup = root.selectAll("g.dx-edges").data([
|
|
909
|
+
{
|
|
910
|
+
id: "edges"
|
|
911
|
+
}
|
|
912
|
+
], (d) => d.id).join("g").classed("dx-edges", true);
|
|
913
|
+
const nodeGroup = root.selectAll("g.dx-nodes").data([
|
|
914
|
+
{
|
|
915
|
+
id: "nodes"
|
|
916
|
+
}
|
|
917
|
+
], (d) => d.id).join("g").classed("dx-nodes", true);
|
|
918
|
+
nodeGroup.selectAll("g.dx-node").data(layout.graph?.nodes ?? [], (d) => d.id).join((enter) => enter.append("g").attr("data-id", (d) => d.id).attr("opacity", 1).classed("dx-node", true).call(createNode, this.options), (update) => update.call(updateNode2, this.options), (exit) => {
|
|
919
|
+
return exit.transition().ease(easeCubicOut).duration(300).attr("opacity", 0).on("end", function(d) {
|
|
920
|
+
select4(this).remove();
|
|
921
|
+
});
|
|
922
|
+
}).sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
|
|
923
|
+
edgeGroup.selectAll("g.dx-edge").data(layout.graph?.edges ?? [], (d) => d.id).join((enter) => enter.append("g").attr("data-id", (d) => d.id).classed("dx-edge", true).call(createEdge, this.options), (update) => update.call(updateEdge, this.options, nodeGroup).each((d) => {
|
|
924
|
+
}), (exit) => exit.remove()).sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
|
|
925
|
+
}
|
|
926
|
+
/**
|
|
927
|
+
* Trigger path bullets.
|
|
928
|
+
* @param node
|
|
929
|
+
*/
|
|
930
|
+
fireBullet(node) {
|
|
931
|
+
select4(this.root).selectAll("g.dx-edges").selectAll("path").call(createBullets(this.root, node.id));
|
|
932
|
+
}
|
|
933
|
+
};
|
|
934
|
+
var createNode = (group, options) => {
|
|
935
|
+
const circle = group.append("circle");
|
|
936
|
+
if (options.drag) {
|
|
937
|
+
circle.call(options.drag);
|
|
938
|
+
}
|
|
939
|
+
if (options.onNodeClick) {
|
|
940
|
+
group.on("click", (event) => {
|
|
941
|
+
const node = select4(event.target).datum();
|
|
942
|
+
options.onNodeClick(node, event);
|
|
943
|
+
});
|
|
944
|
+
}
|
|
945
|
+
if (options.labels && !options.onNodePointerEnter) {
|
|
946
|
+
const g = group.append("g").classed("dx-label", true);
|
|
947
|
+
g.append("line");
|
|
948
|
+
g.append("rect");
|
|
949
|
+
g.append("text").style("dominant-baseline", "middle");
|
|
950
|
+
}
|
|
951
|
+
if (options.onNodePointerEnter) {
|
|
952
|
+
circle.on("pointerenter", function(event) {
|
|
953
|
+
const node = select4(this.parentElement).datum();
|
|
954
|
+
options.onNodePointerEnter(node, event);
|
|
955
|
+
});
|
|
956
|
+
group.attr("data-hover", "handled");
|
|
957
|
+
} else if (options.highlight !== false) {
|
|
958
|
+
circle.on("pointerenter", function() {
|
|
959
|
+
select4(this.closest("g.dx-node")).raise();
|
|
960
|
+
if (options.labels) {
|
|
961
|
+
select4(this.parentElement).classed("dx-active", true).classed("dx-highlight", true);
|
|
962
|
+
}
|
|
963
|
+
});
|
|
964
|
+
group.on("pointerleave", function() {
|
|
965
|
+
if (options.labels) {
|
|
966
|
+
select4(this).classed("dx-active", false);
|
|
967
|
+
setTimeout(() => {
|
|
968
|
+
if (!select4(this).classed("dx-active")) {
|
|
969
|
+
select4(this).classed("dx-highlight", false);
|
|
970
|
+
}
|
|
971
|
+
}, 300);
|
|
972
|
+
}
|
|
973
|
+
});
|
|
974
|
+
}
|
|
975
|
+
};
|
|
976
|
+
var updateNode2 = (group, options) => {
|
|
977
|
+
group.attr("transform", (d) => {
|
|
978
|
+
return d.x != null && d.y != null ? `translate(${d.x},${d.y})` : void 0;
|
|
979
|
+
});
|
|
980
|
+
if (options.attributes?.node) {
|
|
981
|
+
try {
|
|
982
|
+
group.each((d, i, nodes) => {
|
|
983
|
+
const node = select4(nodes[i]);
|
|
984
|
+
const { classes, data } = options.attributes?.node?.(d) ?? {};
|
|
985
|
+
if (classes) {
|
|
986
|
+
applyClasses(node, classes);
|
|
987
|
+
}
|
|
988
|
+
if (data) {
|
|
989
|
+
Object.entries(data).forEach(([key, value]) => {
|
|
990
|
+
node.attr(`data-${key}`, value);
|
|
991
|
+
});
|
|
992
|
+
}
|
|
993
|
+
});
|
|
994
|
+
} catch (error) {
|
|
995
|
+
log5.error("updateNode", {
|
|
996
|
+
error: error?.message
|
|
997
|
+
}, {
|
|
998
|
+
F: __dxlog_file7,
|
|
999
|
+
L: 304,
|
|
1000
|
+
S: void 0,
|
|
1001
|
+
C: (f, a) => f(...a)
|
|
1002
|
+
});
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
const groupOrTransition = options.transition ? group.transition(options.transition()) : group;
|
|
1006
|
+
groupOrTransition.select("circle").attr("r", (d) => d.r ?? 16);
|
|
1007
|
+
if (options.labels) {
|
|
1008
|
+
const bx = 1;
|
|
1009
|
+
const px = 4;
|
|
1010
|
+
const py = 2;
|
|
1011
|
+
const offset = 16;
|
|
1012
|
+
const dx = (d, offset2 = 0) => (offset2 + (d.r ?? 0)) * (d.x > 0 ? 1 : -1);
|
|
1013
|
+
groupOrTransition.select("text").text((d) => options.labels.text(d)).style("text-anchor", (d) => d.x > 0 ? "start" : "end").attr("dx", (d) => dx(d, offset)).attr("dy", 1).text((d) => options.labels.text(d)).each(function(d) {
|
|
1014
|
+
let bbox = d.bbox;
|
|
1015
|
+
if (!bbox) {
|
|
1016
|
+
bbox = this.getBBox();
|
|
1017
|
+
d.bbox = bbox;
|
|
1018
|
+
}
|
|
1019
|
+
const width = bbox.width + (bx + px) * 2;
|
|
1020
|
+
const height = bbox.height + py * 2;
|
|
1021
|
+
select4(this.parentElement).select("rect").attr("x", dx(d, offset - (bx + px)) + (d.x > 0 ? 0 : -1) * width).attr("y", -height / 2).attr("width", width).attr("height", height).attr("rx", 4);
|
|
1022
|
+
select4(this.parentElement).select("line").attr("x1", dx(d, 1)).attr("y1", 0).attr("x2", dx(d, offset - (bx + px))).attr("y2", 0);
|
|
1023
|
+
});
|
|
1024
|
+
}
|
|
1025
|
+
};
|
|
1026
|
+
var createEdge = (group, options) => {
|
|
1027
|
+
if (options.onLinkClick) {
|
|
1028
|
+
group.append("path").classed("dx-click", true).on("click", (event) => {
|
|
1029
|
+
const edge = select4(event.target).datum();
|
|
1030
|
+
options.onLinkClick(edge, event);
|
|
1031
|
+
});
|
|
1032
|
+
}
|
|
1033
|
+
group.append("path").attr("pointer-events", "none").attr("marker-start", () => options.arrows?.start ? "url(#marker-arrow-start)" : void 0).attr("marker-end", () => options.arrows?.end ? "url(#marker-arrow-end)" : void 0);
|
|
1034
|
+
};
|
|
1035
|
+
var updateEdge = (group, options, nodeGroup) => {
|
|
1036
|
+
try {
|
|
1037
|
+
group.each((d, i, edges) => {
|
|
1038
|
+
const edge = select4(edges[i]);
|
|
1039
|
+
edge.classed("dx-dashed", d.linkForce === false);
|
|
1040
|
+
const { classes, data } = options.attributes?.edge?.(d) ?? {};
|
|
1041
|
+
if (classes) {
|
|
1042
|
+
applyClasses(edge, classes);
|
|
1043
|
+
}
|
|
1044
|
+
if (data) {
|
|
1045
|
+
Object.entries(data).forEach(([key, value]) => {
|
|
1046
|
+
edge.attr(`data-${key}`, value);
|
|
1047
|
+
});
|
|
1048
|
+
}
|
|
1049
|
+
});
|
|
1050
|
+
} catch (error) {
|
|
1051
|
+
log5.error("updateEdge", {
|
|
1052
|
+
error: error?.message
|
|
1053
|
+
}, {
|
|
1054
|
+
F: __dxlog_file7,
|
|
1055
|
+
L: 416,
|
|
1056
|
+
S: void 0,
|
|
1057
|
+
C: (f, a) => f(...a)
|
|
1058
|
+
});
|
|
1059
|
+
}
|
|
1060
|
+
const groupOrTransition = options.transition ? group.transition(options.transition()) : group;
|
|
1061
|
+
groupOrTransition.selectAll("path").attr("d", function() {
|
|
1062
|
+
const { source, target } = select4(this.parentElement).datum();
|
|
1063
|
+
if (!source.initialized || !target.initialized) {
|
|
1064
|
+
return;
|
|
1065
|
+
}
|
|
1066
|
+
return createLine2(getCircumferencePoints([
|
|
1067
|
+
source.x,
|
|
1068
|
+
source.y
|
|
1069
|
+
], [
|
|
1070
|
+
target.x,
|
|
1071
|
+
target.y
|
|
1072
|
+
], source.r, target.r));
|
|
1073
|
+
});
|
|
1074
|
+
};
|
|
1075
|
+
var applyClasses = (el, classes) => {
|
|
1076
|
+
for (const [className, value] of Object.entries(classes)) {
|
|
1077
|
+
el.classed(className, value);
|
|
1078
|
+
}
|
|
1079
|
+
};
|
|
1080
|
+
var findConnectedComponents = (graph) => {
|
|
1081
|
+
const adj = {};
|
|
1082
|
+
for (const node of graph.nodes) {
|
|
1083
|
+
adj[node.id] = [];
|
|
1084
|
+
}
|
|
1085
|
+
for (const { source, target } of graph.edges) {
|
|
1086
|
+
adj[source].push(target);
|
|
1087
|
+
adj[target].push(source);
|
|
1088
|
+
}
|
|
1089
|
+
const visited = /* @__PURE__ */ new Set();
|
|
1090
|
+
const components = [];
|
|
1091
|
+
for (const node of graph.nodes) {
|
|
1092
|
+
if (visited.has(node.id)) {
|
|
1093
|
+
continue;
|
|
1094
|
+
}
|
|
1095
|
+
const stack = [
|
|
1096
|
+
node.id
|
|
1097
|
+
];
|
|
1098
|
+
const componentIds = [];
|
|
1099
|
+
while (stack.length) {
|
|
1100
|
+
const current = stack.pop();
|
|
1101
|
+
if (visited.has(current)) {
|
|
1102
|
+
continue;
|
|
1103
|
+
}
|
|
1104
|
+
visited.add(current);
|
|
1105
|
+
componentIds.push(current);
|
|
1106
|
+
stack.push(...adj[current]);
|
|
1107
|
+
}
|
|
1108
|
+
components.push(componentIds);
|
|
1109
|
+
}
|
|
1110
|
+
return components;
|
|
1111
|
+
};
|
|
1112
|
+
|
|
1113
|
+
// packages/ui/react-ui-graph/src/graph/renderer/linker.ts
|
|
1114
|
+
import { line as line3 } from "d3";
|
|
1115
|
+
var createLine3 = line3();
|
|
1116
|
+
var linkerRenderer = (root, { source, target, point } = {}) => {
|
|
1117
|
+
root.selectAll("g.dx-linker").data([
|
|
1118
|
+
{
|
|
1119
|
+
id: "linker"
|
|
1120
|
+
}
|
|
1121
|
+
]).join("g").attr("class", "dx-linker").selectAll("path").data(source ? [
|
|
1122
|
+
{
|
|
1123
|
+
id: "edge"
|
|
1124
|
+
}
|
|
1125
|
+
] : []).join("path").attr("marker-end", () => target ? "url(#marker-arrow-end)" : "url(#marker-dot)").attr("d", () => {
|
|
1126
|
+
return createLine3(getCircumferencePoints([
|
|
1127
|
+
source.x,
|
|
1128
|
+
source.y
|
|
1129
|
+
], target ? [
|
|
1130
|
+
target.x,
|
|
1131
|
+
target.y
|
|
1132
|
+
] : point, source.r, target ? target.r : 0));
|
|
1133
|
+
});
|
|
1134
|
+
};
|
|
1135
|
+
|
|
1136
|
+
// packages/ui/react-ui-graph/src/components/Graph/Graph.tsx
|
|
1137
|
+
var __dxlog_file8 = "/home/runner/work/dxos/dxos/packages/ui/react-ui-graph/src/components/Graph/Graph.tsx";
|
|
1138
|
+
var GraphInner = ({ classNames, model, projector: _projector, renderer: _renderer, drag: drag2, arrows, onSelect, onInspect, ...props }, forwardedRef) => {
|
|
1139
|
+
var _effect = _useSignals();
|
|
1140
|
+
try {
|
|
1141
|
+
const context = useSvgContext();
|
|
1142
|
+
const graphRef = useRef();
|
|
1143
|
+
const { projector, renderer } = useMemo(() => {
|
|
1144
|
+
let projector2 = _projector;
|
|
1145
|
+
if (!projector2) {
|
|
1146
|
+
projector2 = new GraphForceProjector(context);
|
|
1147
|
+
}
|
|
1148
|
+
let renderer2 = _renderer;
|
|
1149
|
+
if (!renderer2) {
|
|
1150
|
+
renderer2 = new GraphRenderer(context, graphRef, {
|
|
1151
|
+
...props,
|
|
1152
|
+
// TODO(burdon): Replace drag when projector is updated.
|
|
1153
|
+
drag: drag2 ? createGraphDrag(context, projector2) : void 0,
|
|
1154
|
+
arrows: {
|
|
1155
|
+
end: arrows
|
|
1156
|
+
},
|
|
1157
|
+
onNodeClick: onSelect ? (node, event) => onSelect(node, event) : void 0,
|
|
1158
|
+
onNodePointerEnter: onInspect ? (node, event) => onInspect(node, event) : void 0
|
|
1159
|
+
});
|
|
1160
|
+
}
|
|
1161
|
+
return {
|
|
1162
|
+
projector: projector2,
|
|
1163
|
+
renderer: renderer2
|
|
1164
|
+
};
|
|
1165
|
+
}, [
|
|
1166
|
+
context,
|
|
1167
|
+
_projector,
|
|
1168
|
+
_renderer,
|
|
1169
|
+
drag2
|
|
1170
|
+
]);
|
|
1171
|
+
useImperativeHandle(forwardedRef, () => ({
|
|
1172
|
+
refresh: () => {
|
|
1173
|
+
projector.updateData(model?.graph);
|
|
1174
|
+
},
|
|
1175
|
+
repaint: () => {
|
|
1176
|
+
renderer.render(projector.layout);
|
|
1177
|
+
},
|
|
1178
|
+
findNode: (id) => {
|
|
1179
|
+
return graphRef.current?.querySelector(`g[data-id="${id}"]`);
|
|
1180
|
+
}
|
|
1181
|
+
}), [
|
|
1182
|
+
model,
|
|
1183
|
+
projector,
|
|
1184
|
+
renderer
|
|
1185
|
+
]);
|
|
1186
|
+
useEffect(() => {
|
|
1187
|
+
return combine(effect(() => {
|
|
1188
|
+
projector.updateData(model?.graph);
|
|
1189
|
+
}), projector.updated.on(({ layout }) => {
|
|
1190
|
+
try {
|
|
1191
|
+
renderer.render(layout);
|
|
1192
|
+
} catch (error) {
|
|
1193
|
+
void projector.stop();
|
|
1194
|
+
log6.catch(error, void 0, {
|
|
1195
|
+
F: __dxlog_file8,
|
|
1196
|
+
L: 108,
|
|
1197
|
+
S: void 0,
|
|
1198
|
+
C: (f, a) => f(...a)
|
|
1199
|
+
});
|
|
1200
|
+
}
|
|
1201
|
+
}));
|
|
1202
|
+
}, [
|
|
1203
|
+
model,
|
|
1204
|
+
projector,
|
|
1205
|
+
renderer
|
|
1206
|
+
]);
|
|
1207
|
+
useEffect(() => {
|
|
1208
|
+
void projector.start();
|
|
1209
|
+
return () => {
|
|
1210
|
+
void projector.stop();
|
|
1211
|
+
};
|
|
1212
|
+
}, [
|
|
1213
|
+
projector
|
|
1214
|
+
]);
|
|
1215
|
+
return /* @__PURE__ */ React.createElement("g", {
|
|
1216
|
+
ref: graphRef,
|
|
1217
|
+
className: mx("dx-graph", classNames)
|
|
1218
|
+
});
|
|
1219
|
+
} finally {
|
|
1220
|
+
_effect.f();
|
|
1221
|
+
}
|
|
1222
|
+
};
|
|
1223
|
+
var Graph = /* @__PURE__ */ forwardRef(GraphInner);
|
|
1224
|
+
|
|
1225
|
+
// packages/ui/react-ui-graph/src/components/Mesh/Mesh.tsx
|
|
1226
|
+
import { geoPath, select as select5 } from "d3";
|
|
1227
|
+
import { useEffect as useEffect2 } from "react";
|
|
1228
|
+
import { feature, mesh } from "topojson-client";
|
|
1229
|
+
var Mesh = ({ radius = 16, value = 0.5 }) => {
|
|
1230
|
+
const { svg, size } = useSvgContext();
|
|
1231
|
+
useEffect2(() => {
|
|
1232
|
+
if (size) {
|
|
1233
|
+
const topology = hexTopology(size, radius, value);
|
|
1234
|
+
const projection = hexProjection(radius);
|
|
1235
|
+
const path = geoPath().projection(projection);
|
|
1236
|
+
select5(svg).append("g").attr("class", "hexagon").selectAll("path").data(topology.objects.hexagons.geometries).enter().append("path").attr("d", (d) => path(feature(topology, d))).attr("class", (d) => d.properties?.fill ? "fill" : null);
|
|
1237
|
+
select5(svg).append("path").datum(mesh(topology, topology.objects.hexagons)).attr("class", "mesh").attr("d", path);
|
|
1238
|
+
const redraw = (border) => {
|
|
1239
|
+
border.attr("d", path(mesh(topology, topology.objects.hexagons, (a, b) => a.properties.fill !== b.properties.fill)));
|
|
1240
|
+
};
|
|
1241
|
+
select5(svg).append("path").attr("class", "border").call(redraw);
|
|
1242
|
+
}
|
|
1243
|
+
}, [
|
|
1244
|
+
svg,
|
|
1245
|
+
size,
|
|
1246
|
+
value
|
|
1247
|
+
]);
|
|
1248
|
+
return null;
|
|
1249
|
+
};
|
|
1250
|
+
var hexTopology = ({ width, height }, radius, value = 1) => {
|
|
1251
|
+
const dx = radius * 2 * Math.sin(Math.PI / 3);
|
|
1252
|
+
const dy = radius * 1.5;
|
|
1253
|
+
const m = Math.ceil((height + radius) / dy) + 1;
|
|
1254
|
+
const n = Math.ceil(width / dx) + 1;
|
|
1255
|
+
const geometries = [];
|
|
1256
|
+
const arcs = [];
|
|
1257
|
+
for (let j = -1; j <= m; ++j) {
|
|
1258
|
+
for (let i = -1; i <= n; ++i) {
|
|
1259
|
+
const y = j * 2;
|
|
1260
|
+
const x = (i + (j & 1) / 2) * 2;
|
|
1261
|
+
arcs.push([
|
|
1262
|
+
[
|
|
1263
|
+
x,
|
|
1264
|
+
y - 1
|
|
1265
|
+
],
|
|
1266
|
+
[
|
|
1267
|
+
1,
|
|
1268
|
+
1
|
|
1269
|
+
]
|
|
1270
|
+
], [
|
|
1271
|
+
[
|
|
1272
|
+
x + 1,
|
|
1273
|
+
y
|
|
1274
|
+
],
|
|
1275
|
+
[
|
|
1276
|
+
0,
|
|
1277
|
+
1
|
|
1278
|
+
]
|
|
1279
|
+
], [
|
|
1280
|
+
[
|
|
1281
|
+
x + 1,
|
|
1282
|
+
y + 1
|
|
1283
|
+
],
|
|
1284
|
+
[
|
|
1285
|
+
-1,
|
|
1286
|
+
1
|
|
1287
|
+
]
|
|
1288
|
+
]);
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
1291
|
+
for (let j = 0, q = 3; j < m; ++j, q += 6) {
|
|
1292
|
+
for (let i = 0; i < n; ++i, q += 3) {
|
|
1293
|
+
geometries.push({
|
|
1294
|
+
type: "Polygon",
|
|
1295
|
+
arcs: [
|
|
1296
|
+
[
|
|
1297
|
+
q,
|
|
1298
|
+
q + 1,
|
|
1299
|
+
q + 2,
|
|
1300
|
+
~(q + (n + 2 - (j & 1)) * 3),
|
|
1301
|
+
~(q - 2),
|
|
1302
|
+
~(q - (n + 2 + (j & 1)) * 3 + 2)
|
|
1303
|
+
]
|
|
1304
|
+
],
|
|
1305
|
+
properties: {
|
|
1306
|
+
fill: Math.random() > i / n * (1 / value)
|
|
1307
|
+
}
|
|
1308
|
+
});
|
|
1309
|
+
}
|
|
1310
|
+
}
|
|
1311
|
+
const hexagons = {
|
|
1312
|
+
type: "GeometryCollection",
|
|
1313
|
+
geometries
|
|
1314
|
+
};
|
|
1315
|
+
return {
|
|
1316
|
+
type: "Topology",
|
|
1317
|
+
transform: {
|
|
1318
|
+
translate: [
|
|
1319
|
+
0,
|
|
1320
|
+
0
|
|
1321
|
+
],
|
|
1322
|
+
scale: [
|
|
1323
|
+
1,
|
|
1324
|
+
1
|
|
1325
|
+
]
|
|
1326
|
+
},
|
|
1327
|
+
objects: {
|
|
1328
|
+
hexagons
|
|
1329
|
+
},
|
|
1330
|
+
arcs
|
|
1331
|
+
};
|
|
1332
|
+
};
|
|
1333
|
+
var hexProjection = (radius) => {
|
|
1334
|
+
const dx = radius * 2 * Math.sin(Math.PI / 3);
|
|
1335
|
+
const dy = radius * 1.5;
|
|
1336
|
+
return {
|
|
1337
|
+
stream: (stream) => ({
|
|
1338
|
+
point: (x, y) => {
|
|
1339
|
+
stream.point(x * dx / 2, (y - (2 - (y & 1)) / 3) * dy / 2);
|
|
1340
|
+
},
|
|
1341
|
+
lineStart: () => {
|
|
1342
|
+
stream.lineStart();
|
|
1343
|
+
},
|
|
1344
|
+
lineEnd: () => {
|
|
1345
|
+
stream.lineEnd();
|
|
1346
|
+
},
|
|
1347
|
+
polygonStart: () => {
|
|
1348
|
+
stream.polygonStart();
|
|
1349
|
+
},
|
|
1350
|
+
polygonEnd: () => {
|
|
1351
|
+
stream.polygonEnd();
|
|
1352
|
+
}
|
|
1353
|
+
})
|
|
1354
|
+
};
|
|
1355
|
+
};
|
|
1356
|
+
|
|
1357
|
+
// packages/ui/react-ui-graph/src/components/SVG/Grid.tsx
|
|
1358
|
+
import { useSignals as _useSignals2 } from "@preact-signals/safe-react/tracking";
|
|
1359
|
+
import React2 from "react";
|
|
1360
|
+
import { mx as mx2 } from "@dxos/react-ui-theme";
|
|
1361
|
+
var Grid = ({ classNames, ...props }) => {
|
|
1362
|
+
var _effect = _useSignals2();
|
|
1363
|
+
try {
|
|
1364
|
+
const { ref } = useGrid(props);
|
|
1365
|
+
return /* @__PURE__ */ React2.createElement("g", {
|
|
1366
|
+
ref,
|
|
1367
|
+
className: mx2("dx-grid", classNames)
|
|
1368
|
+
});
|
|
1369
|
+
} finally {
|
|
1370
|
+
_effect.f();
|
|
1371
|
+
}
|
|
1372
|
+
};
|
|
1373
|
+
|
|
1374
|
+
// packages/ui/react-ui-graph/src/components/SVG/Markers.tsx
|
|
1375
|
+
import { useSignals as _useSignals3 } from "@preact-signals/safe-react/tracking";
|
|
1376
|
+
import { select as select6 } from "d3";
|
|
1377
|
+
import React3, { useEffect as useEffect3, useRef as useRef2 } from "react";
|
|
1378
|
+
var Markers = ({ arrowSize, className }) => {
|
|
1379
|
+
var _effect = _useSignals3();
|
|
1380
|
+
try {
|
|
1381
|
+
const ref = useRef2();
|
|
1382
|
+
useEffect3(() => {
|
|
1383
|
+
select6(ref.current).call(createMarkers({
|
|
1384
|
+
arrowSize
|
|
1385
|
+
}));
|
|
1386
|
+
}, [
|
|
1387
|
+
ref
|
|
1388
|
+
]);
|
|
1389
|
+
return /* @__PURE__ */ React3.createElement("defs", {
|
|
1390
|
+
ref,
|
|
1391
|
+
className
|
|
1392
|
+
});
|
|
1393
|
+
} finally {
|
|
1394
|
+
_effect.f();
|
|
1395
|
+
}
|
|
1396
|
+
};
|
|
1397
|
+
|
|
1398
|
+
// packages/ui/react-ui-graph/src/components/SVG/Root.tsx
|
|
1399
|
+
import { useSignals as _useSignals4 } from "@preact-signals/safe-react/tracking";
|
|
1400
|
+
import { select as select7 } from "d3";
|
|
1401
|
+
import React4, { forwardRef as forwardRef2, useEffect as useEffect4, useMemo as useMemo2, useImperativeHandle as useImperativeHandle2 } from "react";
|
|
1402
|
+
import { useResizeDetector } from "react-resize-detector";
|
|
1403
|
+
import { mx as mx3 } from "@dxos/react-ui-theme";
|
|
1404
|
+
var Root = /* @__PURE__ */ forwardRef2(({ classNames, children, ...props }, ref) => {
|
|
1405
|
+
var _effect = _useSignals4();
|
|
1406
|
+
try {
|
|
1407
|
+
const { ref: containerRef, width = 0, height = 0 } = useResizeDetector({
|
|
1408
|
+
refreshRate: 200
|
|
1409
|
+
});
|
|
1410
|
+
const context = useMemo2(() => {
|
|
1411
|
+
return new SVGContext(props);
|
|
1412
|
+
}, [
|
|
1413
|
+
props.scale,
|
|
1414
|
+
props.centered
|
|
1415
|
+
]);
|
|
1416
|
+
useImperativeHandle2(ref, () => context, [
|
|
1417
|
+
context
|
|
1418
|
+
]);
|
|
1419
|
+
useEffect4(() => {
|
|
1420
|
+
if (width && height) {
|
|
1421
|
+
context.setSize({
|
|
1422
|
+
width,
|
|
1423
|
+
height
|
|
1424
|
+
});
|
|
1425
|
+
select7(context.svg).attr("display", "block").attr("viewBox", context.viewBox).attr("width", width).attr("height", height);
|
|
1426
|
+
}
|
|
1427
|
+
}, [
|
|
1428
|
+
context,
|
|
1429
|
+
width,
|
|
1430
|
+
height
|
|
1431
|
+
]);
|
|
1432
|
+
const ready = width !== 0 && height !== 0;
|
|
1433
|
+
return /* @__PURE__ */ React4.createElement(SVGContextProvider, {
|
|
1434
|
+
value: context
|
|
1435
|
+
}, /* @__PURE__ */ React4.createElement("div", {
|
|
1436
|
+
ref: containerRef,
|
|
1437
|
+
className: mx3("grid grow overflow-hidden", classNames)
|
|
1438
|
+
}, /* @__PURE__ */ React4.createElement("svg", {
|
|
1439
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
1440
|
+
ref: context.ref,
|
|
1441
|
+
className: mx3("transition-opacity opacity-0 duration-1000", ready && "opacity-100")
|
|
1442
|
+
}, ready && children)));
|
|
1443
|
+
} finally {
|
|
1444
|
+
_effect.f();
|
|
1445
|
+
}
|
|
1446
|
+
});
|
|
1447
|
+
|
|
1448
|
+
// packages/ui/react-ui-graph/src/components/SVG/Zoom.tsx
|
|
1449
|
+
import { useSignals as _useSignals5 } from "@preact-signals/safe-react/tracking";
|
|
1450
|
+
import React5, { memo, useMemo as useMemo3 } from "react";
|
|
1451
|
+
var Zoom = /* @__PURE__ */ memo(({ extent, className, children }) => {
|
|
1452
|
+
var _effect = _useSignals5();
|
|
1453
|
+
try {
|
|
1454
|
+
const options = useMemo3(() => ({
|
|
1455
|
+
extent
|
|
1456
|
+
}), [
|
|
1457
|
+
JSON.stringify(extent)
|
|
1458
|
+
]);
|
|
1459
|
+
const zoom = useZoom(options);
|
|
1460
|
+
return /* @__PURE__ */ React5.createElement("g", {
|
|
1461
|
+
ref: zoom.ref,
|
|
1462
|
+
className
|
|
1463
|
+
}, children);
|
|
1464
|
+
} finally {
|
|
1465
|
+
_effect.f();
|
|
1466
|
+
}
|
|
1467
|
+
});
|
|
1468
|
+
|
|
1469
|
+
// packages/ui/react-ui-graph/src/components/SVG/SVG.tsx
|
|
1470
|
+
var SVG = {
|
|
1471
|
+
Root,
|
|
1472
|
+
Grid,
|
|
1473
|
+
Markers,
|
|
1474
|
+
Mesh,
|
|
1475
|
+
Zoom,
|
|
1476
|
+
Graph
|
|
1477
|
+
};
|
|
1478
|
+
|
|
1479
|
+
// packages/ui/react-ui-graph/src/fx/pulsar.ts
|
|
1480
|
+
import { easeCubicOut as easeCubicOut2, select as select8 } from "d3";
|
|
1481
|
+
var PROP_PULSAR_ACTIVE = "dx-pulsar-active";
|
|
1482
|
+
(function(Pulsar2) {
|
|
1483
|
+
Pulsar2.has = (group) => group.select("circle.dx-pulsar").empty();
|
|
1484
|
+
Pulsar2.create = (group, r, options = {}) => {
|
|
1485
|
+
const pulsar = group.append("circle").classed("dx-pulsar", true).classed("stroke-2 stroke-red-500 fill-none", true).property(PROP_PULSAR_ACTIVE, true);
|
|
1486
|
+
startAnimation(pulsar, r, options);
|
|
1487
|
+
};
|
|
1488
|
+
Pulsar2.remove = (group) => {
|
|
1489
|
+
group.select("circle.dx-pulsar").property(PROP_PULSAR_ACTIVE, false);
|
|
1490
|
+
};
|
|
1491
|
+
})(Pulsar || (Pulsar = {}));
|
|
1492
|
+
var startAnimation = (pulsar, r, options = {}) => {
|
|
1493
|
+
const { period = 1e3, duration = 800, ratio = 3 } = options;
|
|
1494
|
+
return pulsar.attr("r", r).attr("opacity", 1).transition("pulsar").delay(period - Date.now() % period).duration(Math.min(duration, period - 20)).ease(easeCubicOut2).attr("r", r * ratio).attr("opacity", -0.1).on("end", function() {
|
|
1495
|
+
if (select8(this).property(PROP_PULSAR_ACTIVE)) {
|
|
1496
|
+
startAnimation(select8(this), r, options);
|
|
1497
|
+
} else {
|
|
1498
|
+
select8(this).remove();
|
|
1499
|
+
}
|
|
1500
|
+
});
|
|
1501
|
+
};
|
|
1502
|
+
var Pulsar;
|
|
1503
|
+
export {
|
|
1504
|
+
EventEmitter,
|
|
1505
|
+
FractionUtil,
|
|
1506
|
+
Graph,
|
|
1507
|
+
GraphForceProjector,
|
|
1508
|
+
GraphHierarchicalProjector,
|
|
1509
|
+
GraphInner,
|
|
1510
|
+
GraphProjector,
|
|
1511
|
+
GraphRadialProjector,
|
|
1512
|
+
GraphRelationalProjector,
|
|
1513
|
+
GraphRenderer,
|
|
1514
|
+
GridController,
|
|
1515
|
+
Mesh,
|
|
1516
|
+
Projector,
|
|
1517
|
+
Pulsar,
|
|
1518
|
+
Renderer,
|
|
1519
|
+
SVG,
|
|
1520
|
+
SVGContext,
|
|
1521
|
+
SVGContextProvider,
|
|
1522
|
+
Scale,
|
|
1523
|
+
Screen,
|
|
1524
|
+
Vector,
|
|
1525
|
+
ZoomHandler,
|
|
1526
|
+
createGraphDrag,
|
|
1527
|
+
createMarkers,
|
|
1528
|
+
defaultForceOptions,
|
|
1529
|
+
defaultIdAccessor,
|
|
1530
|
+
emptyGraph,
|
|
1531
|
+
getCircumferencePoints,
|
|
1532
|
+
layoutHorizontal,
|
|
1533
|
+
layoutRadial,
|
|
1534
|
+
layoutVertical,
|
|
1535
|
+
linkerRenderer,
|
|
1536
|
+
normal,
|
|
1537
|
+
offsetPoints,
|
|
1538
|
+
updateNode,
|
|
1539
|
+
useDynamicRef,
|
|
1540
|
+
useGrid,
|
|
1541
|
+
useStateRef,
|
|
1542
|
+
useSvgContext,
|
|
1543
|
+
useTimestamp,
|
|
1544
|
+
useZoom
|
|
1545
|
+
};
|
|
1546
|
+
//# sourceMappingURL=index.mjs.map
|