@dxos/react-ui-graph 0.10.0 → 0.11.0
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/dist/lib/chunk-useStateRef.mjs +61 -0
- package/dist/lib/chunk-useStateRef.mjs.map +1 -0
- package/dist/lib/index.mjs +3679 -0
- package/dist/lib/index.mjs.map +1 -0
- package/dist/lib/testing.mjs +158 -0
- package/dist/lib/testing.mjs.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +20 -20
- package/dist/lib/browser/chunk-EASNZZ7B.mjs +0 -52
- package/dist/lib/browser/chunk-EASNZZ7B.mjs.map +0 -7
- package/dist/lib/browser/index.mjs +0 -4427
- package/dist/lib/browser/index.mjs.map +0 -7
- package/dist/lib/browser/meta.json +0 -1
- package/dist/lib/browser/testing/index.mjs +0 -180
- package/dist/lib/browser/testing/index.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-JSE2X4ML.mjs +0 -54
- package/dist/lib/node-esm/chunk-JSE2X4ML.mjs.map +0 -7
- package/dist/lib/node-esm/index.mjs +0 -4428
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
- package/dist/lib/node-esm/testing/index.mjs +0 -181
- package/dist/lib/node-esm/testing/index.mjs.map +0 -7
|
@@ -1,4428 +0,0 @@
|
|
|
1
|
-
import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
|
|
2
|
-
import {
|
|
3
|
-
useDynamicRef,
|
|
4
|
-
useStateRef,
|
|
5
|
-
useTimestamp
|
|
6
|
-
} from "./chunk-JSE2X4ML.mjs";
|
|
7
|
-
|
|
8
|
-
// src/components/Graph/Graph.tsx
|
|
9
|
-
import React, { forwardRef, useEffect as useEffect3, useImperativeHandle, useMemo as useMemo3, useRef as useRef3 } from "react";
|
|
10
|
-
import { combine } from "@dxos/async";
|
|
11
|
-
import { log as log12 } from "@dxos/log";
|
|
12
|
-
import { mx } from "@dxos/ui-theme";
|
|
13
|
-
|
|
14
|
-
// src/graph/markers.ts
|
|
15
|
-
import { line, select } from "d3";
|
|
16
|
-
var createLine = line();
|
|
17
|
-
var createArrow = (length, offset, start) => (el) => {
|
|
18
|
-
const height = length * 0.5;
|
|
19
|
-
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([
|
|
20
|
-
start ? [
|
|
21
|
-
length,
|
|
22
|
-
height
|
|
23
|
-
] : [
|
|
24
|
-
-length,
|
|
25
|
-
-height
|
|
26
|
-
],
|
|
27
|
-
[
|
|
28
|
-
0,
|
|
29
|
-
0
|
|
30
|
-
],
|
|
31
|
-
start ? [
|
|
32
|
-
length,
|
|
33
|
-
-height
|
|
34
|
-
] : [
|
|
35
|
-
-length,
|
|
36
|
-
height
|
|
37
|
-
]
|
|
38
|
-
]));
|
|
39
|
-
};
|
|
40
|
-
var createDot = (size) => (el) => {
|
|
41
|
-
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);
|
|
42
|
-
};
|
|
43
|
-
var createMarkers = ({ arrowSize = 12 } = {}) => (group) => {
|
|
44
|
-
return group.selectAll("marker").data([
|
|
45
|
-
{
|
|
46
|
-
id: "marker-arrow-start",
|
|
47
|
-
generator: createArrow(arrowSize, -0.5, true),
|
|
48
|
-
className: "dx-arrow"
|
|
49
|
-
},
|
|
50
|
-
{
|
|
51
|
-
id: "marker-arrow-end",
|
|
52
|
-
generator: createArrow(arrowSize, 0.5, false),
|
|
53
|
-
className: "dx-arrow"
|
|
54
|
-
},
|
|
55
|
-
{
|
|
56
|
-
id: "marker-dot",
|
|
57
|
-
generator: createDot(6),
|
|
58
|
-
className: "dx-dot"
|
|
59
|
-
}
|
|
60
|
-
]).join("marker").attr("id", (d) => d.id).attr("markerUnits", "strokeWidth").attr("class", (d) => d.className).each((d, i, nodes) => {
|
|
61
|
-
select(nodes[i]).call(d.generator);
|
|
62
|
-
});
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
// src/graph/projector/graph-bundle-projector.ts
|
|
66
|
-
import { curveBundle, cluster as d3Cluster, hierarchy, lineRadial } from "d3";
|
|
67
|
-
import { log as log2 } from "@dxos/log";
|
|
68
|
-
|
|
69
|
-
// src/graph/projector/graph-radial-projector.ts
|
|
70
|
-
import { easeCubic, timer } from "d3";
|
|
71
|
-
import { log } from "@dxos/log";
|
|
72
|
-
|
|
73
|
-
// src/graph/types.ts
|
|
74
|
-
import { invariant } from "@dxos/invariant";
|
|
75
|
-
var __dxlog_file = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/types.ts";
|
|
76
|
-
var defaultIdAccessor = (node) => {
|
|
77
|
-
invariant(node.id, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 10, S: void 0, A: ["node.id", ""] });
|
|
78
|
-
return node.id;
|
|
79
|
-
};
|
|
80
|
-
var emptyGraph = {
|
|
81
|
-
nodes: [],
|
|
82
|
-
edges: []
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
// src/util/events.ts
|
|
86
|
-
var EventEmitter = class {
|
|
87
|
-
_listeners = /* @__PURE__ */ new Set();
|
|
88
|
-
clear() {
|
|
89
|
-
this._listeners.clear();
|
|
90
|
-
}
|
|
91
|
-
on(onEvent) {
|
|
92
|
-
this._listeners.add(onEvent);
|
|
93
|
-
return () => this._listeners.delete(onEvent);
|
|
94
|
-
}
|
|
95
|
-
emit(value) {
|
|
96
|
-
this._listeners.forEach((listener) => listener(value));
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
// src/util/fraction.ts
|
|
101
|
-
import { invariant as invariant2 } from "@dxos/invariant";
|
|
102
|
-
var __dxlog_file2 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/util/fraction.ts";
|
|
103
|
-
var FractionUtil = class _FractionUtil {
|
|
104
|
-
/**
|
|
105
|
-
* Convert number to fraction.
|
|
106
|
-
* @param n
|
|
107
|
-
*/
|
|
108
|
-
static toFraction = (n) => {
|
|
109
|
-
return typeof n === "number" ? [
|
|
110
|
-
n,
|
|
111
|
-
1
|
|
112
|
-
] : n;
|
|
113
|
-
};
|
|
114
|
-
static validate = (...fraction) => {
|
|
115
|
-
fraction.forEach(([n, d]) => {
|
|
116
|
-
if (!Number.isFinite(n) || !Number.isFinite(d) || d === 0) {
|
|
117
|
-
throw new Error(`Invalid fraction: ${n}/${d}`);
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
return fraction;
|
|
121
|
-
};
|
|
122
|
-
/**
|
|
123
|
-
* @param {Fraction}
|
|
124
|
-
* Convert to rounded-sm number.
|
|
125
|
-
*/
|
|
126
|
-
static toNumber = ([n, d]) => {
|
|
127
|
-
_FractionUtil.validate([
|
|
128
|
-
n,
|
|
129
|
-
d
|
|
130
|
-
]);
|
|
131
|
-
return n / d;
|
|
132
|
-
};
|
|
133
|
-
/**
|
|
134
|
-
* Round the number to the nearest fraction.
|
|
135
|
-
* Example: 3/5 => 1/1; 3/5 (precision 2) => 1/2.
|
|
136
|
-
* @param {Fraction}
|
|
137
|
-
* @param p Precision (e.g., 1/2, 1/4, etc.)
|
|
138
|
-
*/
|
|
139
|
-
static round = ([n, d], p = 1) => {
|
|
140
|
-
_FractionUtil.validate([
|
|
141
|
-
n,
|
|
142
|
-
d
|
|
143
|
-
]);
|
|
144
|
-
const v = Math.round(_FractionUtil.toNumber(_FractionUtil.divide([
|
|
145
|
-
n,
|
|
146
|
-
d
|
|
147
|
-
], [
|
|
148
|
-
1,
|
|
149
|
-
p
|
|
150
|
-
])));
|
|
151
|
-
return _FractionUtil.simplify([
|
|
152
|
-
v,
|
|
153
|
-
p
|
|
154
|
-
]);
|
|
155
|
-
};
|
|
156
|
-
//
|
|
157
|
-
// Boolean.
|
|
158
|
-
//
|
|
159
|
-
/**
|
|
160
|
-
* Tests if equals.
|
|
161
|
-
* @param n1
|
|
162
|
-
* @param n2
|
|
163
|
-
*/
|
|
164
|
-
static equals = (n1, n2) => {
|
|
165
|
-
return n1[0] * n2[1] === n1[1] * n2[0];
|
|
166
|
-
};
|
|
167
|
-
/**
|
|
168
|
-
* @param {Fraction}
|
|
169
|
-
* True if zero.
|
|
170
|
-
*/
|
|
171
|
-
static isZero = ([n]) => {
|
|
172
|
-
return n === 0;
|
|
173
|
-
};
|
|
174
|
-
//
|
|
175
|
-
// Normalization.
|
|
176
|
-
//
|
|
177
|
-
/**
|
|
178
|
-
* Simplify fraction finding LCDs.
|
|
179
|
-
* @param n
|
|
180
|
-
* @param d
|
|
181
|
-
*/
|
|
182
|
-
static simplify = ([n, d]) => {
|
|
183
|
-
_FractionUtil.validate([
|
|
184
|
-
n,
|
|
185
|
-
d
|
|
186
|
-
]);
|
|
187
|
-
if (d < 1) {
|
|
188
|
-
const t = n / d;
|
|
189
|
-
if (Math.floor(t) === t) {
|
|
190
|
-
n = t;
|
|
191
|
-
d = 1;
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
const fn = _FractionUtil.factors(n).reverse();
|
|
195
|
-
const fd = _FractionUtil.factors(d).reverse();
|
|
196
|
-
let c;
|
|
197
|
-
for (let i = 0; i < fn.length; i++) {
|
|
198
|
-
c = fd.find((n2) => n2 === fn[i]);
|
|
199
|
-
if (c) {
|
|
200
|
-
return [
|
|
201
|
-
n / c,
|
|
202
|
-
d / c
|
|
203
|
-
];
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
return [
|
|
207
|
-
n,
|
|
208
|
-
d
|
|
209
|
-
];
|
|
210
|
-
};
|
|
211
|
-
/**
|
|
212
|
-
* Return common factors.
|
|
213
|
-
* @param n
|
|
214
|
-
*/
|
|
215
|
-
static factors = (n) => {
|
|
216
|
-
invariant2(Number.isFinite(n), `Invalid number: ${n}`, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 113, S: this, A: ["Number.isFinite(n)", "`Invalid number: ${n}`"] });
|
|
217
|
-
const factors = [
|
|
218
|
-
1
|
|
219
|
-
];
|
|
220
|
-
if (Number.isSafeInteger(n)) {
|
|
221
|
-
let f = 1;
|
|
222
|
-
do {
|
|
223
|
-
if (n % ++f === 0) {
|
|
224
|
-
factors.push(f);
|
|
225
|
-
}
|
|
226
|
-
} while (n / f > 1 && f < 100);
|
|
227
|
-
}
|
|
228
|
-
return factors;
|
|
229
|
-
};
|
|
230
|
-
//
|
|
231
|
-
// Basic arithmetic.
|
|
232
|
-
//
|
|
233
|
-
/**
|
|
234
|
-
* @param n1
|
|
235
|
-
* @param n2
|
|
236
|
-
*/
|
|
237
|
-
static add = (n1, n2) => {
|
|
238
|
-
_FractionUtil.validate(n1, n2);
|
|
239
|
-
const d = n1[1] * n2[1];
|
|
240
|
-
const n = n1[0] * n2[1] + n2[0] * n1[1];
|
|
241
|
-
return _FractionUtil.simplify([
|
|
242
|
-
n,
|
|
243
|
-
d
|
|
244
|
-
]);
|
|
245
|
-
};
|
|
246
|
-
/**
|
|
247
|
-
* @param n1
|
|
248
|
-
* @param n2
|
|
249
|
-
*/
|
|
250
|
-
static subtract = (n1, n2) => {
|
|
251
|
-
_FractionUtil.validate(n1, n2);
|
|
252
|
-
const d = n1[1] * n2[1];
|
|
253
|
-
const n = n1[0] * n2[1] - n2[0] * n1[1];
|
|
254
|
-
return _FractionUtil.simplify([
|
|
255
|
-
n,
|
|
256
|
-
d
|
|
257
|
-
]);
|
|
258
|
-
};
|
|
259
|
-
/**
|
|
260
|
-
* @param n1
|
|
261
|
-
* @param n2
|
|
262
|
-
*/
|
|
263
|
-
static multiply = (n1, n2) => {
|
|
264
|
-
_FractionUtil.validate(n1, n2);
|
|
265
|
-
const n = n1[0] * n2[0];
|
|
266
|
-
const d = n1[1] * n2[1];
|
|
267
|
-
return _FractionUtil.simplify([
|
|
268
|
-
n,
|
|
269
|
-
d
|
|
270
|
-
]);
|
|
271
|
-
};
|
|
272
|
-
/**
|
|
273
|
-
* @param n1
|
|
274
|
-
* @param n2
|
|
275
|
-
*/
|
|
276
|
-
static divide = (n1, n2) => {
|
|
277
|
-
_FractionUtil.validate(n1, n2);
|
|
278
|
-
const n = n1[0] * n2[1];
|
|
279
|
-
const d = n1[1] * n2[0];
|
|
280
|
-
return _FractionUtil.simplify([
|
|
281
|
-
n,
|
|
282
|
-
d
|
|
283
|
-
]);
|
|
284
|
-
};
|
|
285
|
-
};
|
|
286
|
-
|
|
287
|
-
// src/util/points.ts
|
|
288
|
-
var getCircumferencePoints = (p1, p2, r1 = 0, r2 = 0) => {
|
|
289
|
-
const [x1, y1] = p1;
|
|
290
|
-
const [x2, y2] = p2;
|
|
291
|
-
const theta = Math.atan2(x2 - x1, y2 - y1);
|
|
292
|
-
return [
|
|
293
|
-
[
|
|
294
|
-
// p1
|
|
295
|
-
x1 + (r1 === 0 ? 0 : r1 * Math.sin(theta)),
|
|
296
|
-
y1 + (r1 === 0 ? 0 : r1 * Math.cos(theta))
|
|
297
|
-
],
|
|
298
|
-
[
|
|
299
|
-
// p2
|
|
300
|
-
x2 - (r2 === 0 ? 0 : r2 * Math.cos(Math.PI / 2 - theta)),
|
|
301
|
-
y2 - (r2 === 0 ? 0 : r2 * Math.sin(Math.PI / 2 - theta))
|
|
302
|
-
]
|
|
303
|
-
];
|
|
304
|
-
};
|
|
305
|
-
var normal = (p1, p2) => {
|
|
306
|
-
const dx = p2[0] - p1[0];
|
|
307
|
-
const dy = p2[1] - p1[1];
|
|
308
|
-
const len = Math.hypot(dx, dy);
|
|
309
|
-
return [
|
|
310
|
-
dy / len,
|
|
311
|
-
-dx / len
|
|
312
|
-
];
|
|
313
|
-
};
|
|
314
|
-
var offsetPoints = (points, distance, closed) => {
|
|
315
|
-
const len = points.length;
|
|
316
|
-
const out = [];
|
|
317
|
-
for (let i = 0; i < len; i++) {
|
|
318
|
-
const p0 = points[(i - 1 + len) % len];
|
|
319
|
-
const p1 = points[i];
|
|
320
|
-
const p2 = points[(i + 1) % len];
|
|
321
|
-
const n1 = normal(p0, p1);
|
|
322
|
-
const n2 = normal(p1, p2);
|
|
323
|
-
const nx = (n1[0] + n2[0]) / 2;
|
|
324
|
-
const ny = (n1[1] + n2[1]) / 2;
|
|
325
|
-
const norm = Math.hypot(nx, ny) || 1;
|
|
326
|
-
out.push([
|
|
327
|
-
p1[0] + distance * nx / norm,
|
|
328
|
-
p1[1] + distance * ny / norm
|
|
329
|
-
]);
|
|
330
|
-
if (!closed && (i === 0 || i === len - 1)) {
|
|
331
|
-
out[i] = [
|
|
332
|
-
p1[0] + distance * normal(p0, p1)[0],
|
|
333
|
-
p1[1] + distance * normal(p0, p1)[1]
|
|
334
|
-
];
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
return out;
|
|
338
|
-
};
|
|
339
|
-
|
|
340
|
-
// src/util/scale.ts
|
|
341
|
-
import { zoomIdentity } from "d3";
|
|
342
|
-
var Scale = class {
|
|
343
|
-
_gridSize;
|
|
344
|
-
_transform;
|
|
345
|
-
constructor(_gridSize = 32) {
|
|
346
|
-
this._gridSize = _gridSize;
|
|
347
|
-
}
|
|
348
|
-
get gridSize() {
|
|
349
|
-
return this._gridSize;
|
|
350
|
-
}
|
|
351
|
-
get transform() {
|
|
352
|
-
return this._transform || zoomIdentity;
|
|
353
|
-
}
|
|
354
|
-
setTransform(transform) {
|
|
355
|
-
if (this._transform?.x === transform.x && this._transform?.y === transform.y && this._transform?.k === transform.k) {
|
|
356
|
-
return false;
|
|
357
|
-
}
|
|
358
|
-
this._transform = transform;
|
|
359
|
-
return true;
|
|
360
|
-
}
|
|
361
|
-
/**
|
|
362
|
-
* Translate a screen point to a logical point.
|
|
363
|
-
* @param point
|
|
364
|
-
*/
|
|
365
|
-
translate(point) {
|
|
366
|
-
const { x, y, k } = this._transform ?? {
|
|
367
|
-
x: 0,
|
|
368
|
-
y: 0,
|
|
369
|
-
k: 1
|
|
370
|
-
};
|
|
371
|
-
return [
|
|
372
|
-
(point[0] - x) / k,
|
|
373
|
-
(point[1] - y) / k
|
|
374
|
-
];
|
|
375
|
-
}
|
|
376
|
-
/**
|
|
377
|
-
* Map model to screen.
|
|
378
|
-
* Model points are represented by fractions (to preserve precision) and use normal cartesian coordinates.
|
|
379
|
-
*/
|
|
380
|
-
model = {
|
|
381
|
-
// TODO(burdon): Precision depends on scale.
|
|
382
|
-
snapValues: (array) => array.map((n) => FractionUtil.round(n)),
|
|
383
|
-
snapVertex: ({ x, y }) => {
|
|
384
|
-
const [sx, sy] = this.model.snapValues([
|
|
385
|
-
x,
|
|
386
|
-
y
|
|
387
|
-
]);
|
|
388
|
-
return {
|
|
389
|
-
x: sx,
|
|
390
|
-
y: sy
|
|
391
|
-
};
|
|
392
|
-
},
|
|
393
|
-
snapBounds: ({ x, y, width, height }) => {
|
|
394
|
-
[x, y, width, height] = this.model.snapValues([
|
|
395
|
-
x,
|
|
396
|
-
y,
|
|
397
|
-
width,
|
|
398
|
-
height
|
|
399
|
-
]);
|
|
400
|
-
return {
|
|
401
|
-
x,
|
|
402
|
-
y,
|
|
403
|
-
width,
|
|
404
|
-
height
|
|
405
|
-
};
|
|
406
|
-
},
|
|
407
|
-
toValue: (value) => {
|
|
408
|
-
return Math.round(FractionUtil.toNumber(value) * this._gridSize);
|
|
409
|
-
},
|
|
410
|
-
toValues: (values) => {
|
|
411
|
-
return values.map((value) => Math.round(FractionUtil.toNumber(value) * this._gridSize));
|
|
412
|
-
},
|
|
413
|
-
toPoint: (point) => {
|
|
414
|
-
const [x, y] = this.model.toValues([
|
|
415
|
-
point.x,
|
|
416
|
-
point.y
|
|
417
|
-
]);
|
|
418
|
-
return [
|
|
419
|
-
x,
|
|
420
|
-
y === 0 ? 0 : -y
|
|
421
|
-
];
|
|
422
|
-
},
|
|
423
|
-
toBounds: ({ x, y, width, height }) => {
|
|
424
|
-
const point = this.model.toPoint({
|
|
425
|
-
x,
|
|
426
|
-
y
|
|
427
|
-
});
|
|
428
|
-
const [width2, height2] = this.model.toValues([
|
|
429
|
-
width,
|
|
430
|
-
height
|
|
431
|
-
]);
|
|
432
|
-
return {
|
|
433
|
-
x: point[0],
|
|
434
|
-
y: point[1] - height2,
|
|
435
|
-
width: width2,
|
|
436
|
-
height: height2
|
|
437
|
-
};
|
|
438
|
-
}
|
|
439
|
-
};
|
|
440
|
-
/**
|
|
441
|
-
* Map screen to model.
|
|
442
|
-
* Screen points are represented by floating numbers and use translated SVG coordinates (with the y-axis inverted).
|
|
443
|
-
*/
|
|
444
|
-
screen = {
|
|
445
|
-
snapValues: (array) => array.map((n) => Math.round(n / this._gridSize) * this._gridSize),
|
|
446
|
-
snapPoint: ([x, y]) => [
|
|
447
|
-
x,
|
|
448
|
-
y
|
|
449
|
-
].map((n) => Math.round(n / this._gridSize) * this._gridSize),
|
|
450
|
-
snapBounds: ({ x, y, width, height }) => {
|
|
451
|
-
[x, y, width, height] = this.screen.snapValues([
|
|
452
|
-
x,
|
|
453
|
-
y,
|
|
454
|
-
width,
|
|
455
|
-
height
|
|
456
|
-
]);
|
|
457
|
-
return {
|
|
458
|
-
x,
|
|
459
|
-
y,
|
|
460
|
-
width,
|
|
461
|
-
height
|
|
462
|
-
};
|
|
463
|
-
},
|
|
464
|
-
toValue: (value) => {
|
|
465
|
-
return FractionUtil.divide(FractionUtil.toFraction(value), [
|
|
466
|
-
this._gridSize,
|
|
467
|
-
1
|
|
468
|
-
]);
|
|
469
|
-
},
|
|
470
|
-
toValues: (values) => {
|
|
471
|
-
return values.map((value) => FractionUtil.divide(FractionUtil.toFraction(value), [
|
|
472
|
-
this._gridSize,
|
|
473
|
-
1
|
|
474
|
-
]));
|
|
475
|
-
},
|
|
476
|
-
toVertex: (point) => {
|
|
477
|
-
const [x, y] = point;
|
|
478
|
-
return {
|
|
479
|
-
x: FractionUtil.toFraction(x / this._gridSize),
|
|
480
|
-
y: FractionUtil.toFraction(-y / this._gridSize)
|
|
481
|
-
};
|
|
482
|
-
},
|
|
483
|
-
toBounds: (bounds) => {
|
|
484
|
-
const { x, y } = this.screen.toVertex([
|
|
485
|
-
bounds.x,
|
|
486
|
-
bounds.y
|
|
487
|
-
]);
|
|
488
|
-
const [width, height] = this.screen.toValues([
|
|
489
|
-
bounds.width,
|
|
490
|
-
bounds.height
|
|
491
|
-
]);
|
|
492
|
-
return {
|
|
493
|
-
x,
|
|
494
|
-
y: FractionUtil.subtract(y, height),
|
|
495
|
-
width,
|
|
496
|
-
height
|
|
497
|
-
};
|
|
498
|
-
}
|
|
499
|
-
};
|
|
500
|
-
};
|
|
501
|
-
|
|
502
|
-
// src/util/screen.ts
|
|
503
|
-
var Screen = class {
|
|
504
|
-
static createBounds = ([x1, y1], [x2, y2], mod = {}) => {
|
|
505
|
-
const { constrain, center } = mod;
|
|
506
|
-
const x = Math.min(x1, x2);
|
|
507
|
-
const y = Math.min(y1, y2);
|
|
508
|
-
let width = Math.abs(x2 - x1);
|
|
509
|
-
let height = Math.abs(y2 - y1);
|
|
510
|
-
if (constrain) {
|
|
511
|
-
width = height = Math.max(width, height);
|
|
512
|
-
}
|
|
513
|
-
if (center) {
|
|
514
|
-
return {
|
|
515
|
-
x: x - width,
|
|
516
|
-
y: y - height,
|
|
517
|
-
width: width * 2,
|
|
518
|
-
height: height * 2
|
|
519
|
-
};
|
|
520
|
-
} else {
|
|
521
|
-
return {
|
|
522
|
-
x,
|
|
523
|
-
y,
|
|
524
|
-
width,
|
|
525
|
-
height
|
|
526
|
-
};
|
|
527
|
-
}
|
|
528
|
-
};
|
|
529
|
-
static center = ({ x, y, width, height }) => {
|
|
530
|
-
return [
|
|
531
|
-
x + width / 2,
|
|
532
|
-
y + height / 2
|
|
533
|
-
];
|
|
534
|
-
};
|
|
535
|
-
static contains = (bounds, point) => {
|
|
536
|
-
const [x, y] = point;
|
|
537
|
-
if (x < bounds.x || y < bounds.y) {
|
|
538
|
-
return false;
|
|
539
|
-
}
|
|
540
|
-
if (x > bounds.x + bounds.width || y > bounds.y + bounds.height) {
|
|
541
|
-
return false;
|
|
542
|
-
}
|
|
543
|
-
return true;
|
|
544
|
-
};
|
|
545
|
-
/**
|
|
546
|
-
* Distance between two points.
|
|
547
|
-
* @param p1
|
|
548
|
-
* @param p2
|
|
549
|
-
*/
|
|
550
|
-
static len = (p1, p2) => {
|
|
551
|
-
return Math.sqrt(Math.pow(Math.abs(p1[0] - p2[0]), 2) + Math.pow(Math.abs(p1[1] - p2[1]), 2));
|
|
552
|
-
};
|
|
553
|
-
/**
|
|
554
|
-
* Distance to line segment.
|
|
555
|
-
* https://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment#1501725
|
|
556
|
-
* @param v1
|
|
557
|
-
* @param v2
|
|
558
|
-
* @param p
|
|
559
|
-
*/
|
|
560
|
-
static normal = (v1, v2, p) => {
|
|
561
|
-
const d2 = (p1, p2) => Math.pow(Math.abs(p1[0] - p2[0]), 2) + Math.pow(Math.abs(p1[1] - p2[1]), 2);
|
|
562
|
-
let d;
|
|
563
|
-
const len2 = d2(v1, v2);
|
|
564
|
-
if (len2 === 0) {
|
|
565
|
-
d = d2(p, v1);
|
|
566
|
-
} else {
|
|
567
|
-
let t = ((p[0] - v1[0]) * (v2[0] - v1[0]) + (p[1] - v1[1]) * (v2[1] - v1[1])) / len2;
|
|
568
|
-
t = Math.max(0, Math.min(1, t));
|
|
569
|
-
d = d2(p, [
|
|
570
|
-
v1[0] + t * (v2[0] - v1[0]),
|
|
571
|
-
v1[1] + t * (v2[1] - v1[1])
|
|
572
|
-
]);
|
|
573
|
-
}
|
|
574
|
-
return Math.sqrt(d);
|
|
575
|
-
};
|
|
576
|
-
};
|
|
577
|
-
|
|
578
|
-
// src/util/vector.ts
|
|
579
|
-
var Vector = class {
|
|
580
|
-
/**
|
|
581
|
-
* Convert to Vertex.
|
|
582
|
-
* @param x
|
|
583
|
-
* @param y
|
|
584
|
-
*/
|
|
585
|
-
static toVertex = ({ x, y }) => ({
|
|
586
|
-
x: FractionUtil.toFraction(x),
|
|
587
|
-
y: FractionUtil.toFraction(y)
|
|
588
|
-
});
|
|
589
|
-
/**
|
|
590
|
-
* Convert to Bounds.
|
|
591
|
-
* @param bounds
|
|
592
|
-
*/
|
|
593
|
-
static toBounds = ({ x, y, width, height }) => ({
|
|
594
|
-
x: FractionUtil.toFraction(x),
|
|
595
|
-
y: FractionUtil.toFraction(y),
|
|
596
|
-
width: FractionUtil.toFraction(width),
|
|
597
|
-
height: FractionUtil.toFraction(height)
|
|
598
|
-
});
|
|
599
|
-
/**
|
|
600
|
-
* Calculate center point.
|
|
601
|
-
* @param bounds
|
|
602
|
-
*/
|
|
603
|
-
static center = ({ x, y, width, height }) => {
|
|
604
|
-
const cx = FractionUtil.add(x, FractionUtil.multiply(width, [
|
|
605
|
-
1,
|
|
606
|
-
2
|
|
607
|
-
]));
|
|
608
|
-
const cy = FractionUtil.add(y, FractionUtil.multiply(height, [
|
|
609
|
-
1,
|
|
610
|
-
2
|
|
611
|
-
]));
|
|
612
|
-
return {
|
|
613
|
-
x: cx,
|
|
614
|
-
y: cy
|
|
615
|
-
};
|
|
616
|
-
};
|
|
617
|
-
/**
|
|
618
|
-
* Calculate centered left edge.
|
|
619
|
-
* @param bounds
|
|
620
|
-
*/
|
|
621
|
-
static left = ({ x, y, height }) => {
|
|
622
|
-
const cx = x;
|
|
623
|
-
const cy = FractionUtil.add(y, FractionUtil.multiply(height, [
|
|
624
|
-
1,
|
|
625
|
-
2
|
|
626
|
-
]));
|
|
627
|
-
return {
|
|
628
|
-
x: cx,
|
|
629
|
-
y: cy
|
|
630
|
-
};
|
|
631
|
-
};
|
|
632
|
-
/**
|
|
633
|
-
* Calculate centered right edge.
|
|
634
|
-
* @param bounds
|
|
635
|
-
*/
|
|
636
|
-
static right = ({ x, y, width, height }) => {
|
|
637
|
-
const cx = FractionUtil.add(x, width);
|
|
638
|
-
const cy = FractionUtil.add(y, FractionUtil.multiply(height, [
|
|
639
|
-
1,
|
|
640
|
-
2
|
|
641
|
-
]));
|
|
642
|
-
return {
|
|
643
|
-
x: cx,
|
|
644
|
-
y: cy
|
|
645
|
-
};
|
|
646
|
-
};
|
|
647
|
-
};
|
|
648
|
-
|
|
649
|
-
// src/graph/projector/projector.ts
|
|
650
|
-
var Projector = class {
|
|
651
|
-
_context;
|
|
652
|
-
updated = new EventEmitter();
|
|
653
|
-
_options;
|
|
654
|
-
constructor(_context, options) {
|
|
655
|
-
this._context = _context;
|
|
656
|
-
this._options = Object.assign({
|
|
657
|
-
idAccessor: defaultIdAccessor
|
|
658
|
-
}, options);
|
|
659
|
-
}
|
|
660
|
-
get context() {
|
|
661
|
-
return this._context;
|
|
662
|
-
}
|
|
663
|
-
get options() {
|
|
664
|
-
return this._options;
|
|
665
|
-
}
|
|
666
|
-
refresh(dragging = false) {
|
|
667
|
-
this.onRefresh(dragging);
|
|
668
|
-
}
|
|
669
|
-
updateData(data) {
|
|
670
|
-
this.onUpdate(data);
|
|
671
|
-
}
|
|
672
|
-
emitUpdate(kind = "topology") {
|
|
673
|
-
this.updated.emit({
|
|
674
|
-
layout: this.layout,
|
|
675
|
-
kind
|
|
676
|
-
});
|
|
677
|
-
}
|
|
678
|
-
async clear() {
|
|
679
|
-
await this.onClear();
|
|
680
|
-
}
|
|
681
|
-
async start() {
|
|
682
|
-
await this.onStart();
|
|
683
|
-
}
|
|
684
|
-
async stop() {
|
|
685
|
-
await this.onStop();
|
|
686
|
-
}
|
|
687
|
-
onRefresh(dragging = false) {
|
|
688
|
-
}
|
|
689
|
-
onUpdate(data) {
|
|
690
|
-
}
|
|
691
|
-
async onClear() {
|
|
692
|
-
}
|
|
693
|
-
async onStart() {
|
|
694
|
-
}
|
|
695
|
-
async onStop() {
|
|
696
|
-
}
|
|
697
|
-
};
|
|
698
|
-
|
|
699
|
-
// src/graph/projector/graph-projector.ts
|
|
700
|
-
var GraphProjector = class extends Projector {
|
|
701
|
-
_selection;
|
|
702
|
-
_layout;
|
|
703
|
-
// TODO(burdon): Change to object props.
|
|
704
|
-
constructor(context, options, _selection, layout) {
|
|
705
|
-
super(context, options), this._selection = _selection;
|
|
706
|
-
this._layout = {
|
|
707
|
-
id: (Math.random() * 10).toString().slice(8),
|
|
708
|
-
graph: {
|
|
709
|
-
nodes: [
|
|
710
|
-
...layout?.graph.nodes ?? []
|
|
711
|
-
],
|
|
712
|
-
edges: [
|
|
713
|
-
...layout?.graph.edges ?? []
|
|
714
|
-
]
|
|
715
|
-
}
|
|
716
|
-
};
|
|
717
|
-
}
|
|
718
|
-
get layout() {
|
|
719
|
-
return this._layout;
|
|
720
|
-
}
|
|
721
|
-
get selection() {
|
|
722
|
-
return this._selection;
|
|
723
|
-
}
|
|
724
|
-
numChildren(node) {
|
|
725
|
-
return this._layout.graph.edges.filter((edge) => edge.source.id === this.options.idAccessor(node)).length;
|
|
726
|
-
}
|
|
727
|
-
reset() {
|
|
728
|
-
this._layout.graph.nodes = [];
|
|
729
|
-
this._layout.graph.edges = [];
|
|
730
|
-
this.emitUpdate();
|
|
731
|
-
}
|
|
732
|
-
/**
|
|
733
|
-
* Merge external data with internal representation (e.g., so force properties like position are preserved).
|
|
734
|
-
* @param data
|
|
735
|
-
*/
|
|
736
|
-
mergeData(data = emptyGraph) {
|
|
737
|
-
const nodes = data.nodes.map((node) => {
|
|
738
|
-
let current = this._layout.graph.nodes.find((n) => n.id === this.options.idAccessor(node));
|
|
739
|
-
if (!current) {
|
|
740
|
-
current = {
|
|
741
|
-
id: this.options.idAccessor(node)
|
|
742
|
-
};
|
|
743
|
-
}
|
|
744
|
-
current.data = node;
|
|
745
|
-
current.hidden = false;
|
|
746
|
-
return current;
|
|
747
|
-
});
|
|
748
|
-
const edges = data.edges.map((edge) => ({
|
|
749
|
-
id: edge.id,
|
|
750
|
-
type: edge.type,
|
|
751
|
-
source: nodes.find((n) => n.id === edge.source),
|
|
752
|
-
target: nodes.find((n) => n.id === edge.target),
|
|
753
|
-
data: edge.data
|
|
754
|
-
})).filter((edge) => edge.source && edge.target);
|
|
755
|
-
Object.assign(this._layout, {
|
|
756
|
-
graph: {
|
|
757
|
-
nodes,
|
|
758
|
-
edges
|
|
759
|
-
}
|
|
760
|
-
});
|
|
761
|
-
}
|
|
762
|
-
async onClear() {
|
|
763
|
-
this.reset();
|
|
764
|
-
}
|
|
765
|
-
/**
|
|
766
|
-
* Optional projector-owned click intercept. The Graph component invokes this BEFORE
|
|
767
|
-
* forwarding to the consumer's `onSelect`, so projectors can implement built-in
|
|
768
|
-
* interactions (e.g. cluster collapse) without each consumer wiring them. Returning
|
|
769
|
-
* `true` indicates the click was handled — consumer's `onSelect` is suppressed.
|
|
770
|
-
* Default no-op returns `false` so consumer handlers still run.
|
|
771
|
-
*/
|
|
772
|
-
handleNodeClick(_node, _event) {
|
|
773
|
-
return false;
|
|
774
|
-
}
|
|
775
|
-
};
|
|
776
|
-
|
|
777
|
-
// src/graph/projector/graph-radial-projector.ts
|
|
778
|
-
var __dxlog_file3 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/projector/graph-radial-projector.ts";
|
|
779
|
-
var GraphRadialProjector = class extends GraphProjector {
|
|
780
|
-
_timer;
|
|
781
|
-
findNode(x, y, radius) {
|
|
782
|
-
return void 0;
|
|
783
|
-
}
|
|
784
|
-
onUpdate(graph) {
|
|
785
|
-
log("onUpdate", {
|
|
786
|
-
graph: {
|
|
787
|
-
nodes: graph?.nodes.length,
|
|
788
|
-
edges: graph?.edges.length
|
|
789
|
-
}
|
|
790
|
-
}, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 13, S: this });
|
|
791
|
-
this.mergeData(graph);
|
|
792
|
-
this.emitUpdate("topology");
|
|
793
|
-
this.doRadialLayout();
|
|
794
|
-
}
|
|
795
|
-
animate() {
|
|
796
|
-
this._timer?.stop();
|
|
797
|
-
this._timer = void 0;
|
|
798
|
-
const start = Date.now();
|
|
799
|
-
if (this.options.duration) {
|
|
800
|
-
this._timer = timer(() => {
|
|
801
|
-
const t = Math.min(1, (Date.now() - start) / this.options.duration);
|
|
802
|
-
const d = easeCubic(t);
|
|
803
|
-
this.layout.graph.nodes.forEach((node) => {
|
|
804
|
-
node.x = node.sx + (node.tx - node.sx) * d;
|
|
805
|
-
node.y = node.sy + (node.ty - node.sy) * d;
|
|
806
|
-
node.r = node.sr + (node.tr - node.sr) * d;
|
|
807
|
-
});
|
|
808
|
-
this.onTickFrame(d);
|
|
809
|
-
this.emitUpdate("positions");
|
|
810
|
-
if (t >= 1) {
|
|
811
|
-
this._timer.stop();
|
|
812
|
-
this._timer = void 0;
|
|
813
|
-
}
|
|
814
|
-
});
|
|
815
|
-
} else {
|
|
816
|
-
this.layout.graph.nodes.forEach((node) => {
|
|
817
|
-
node.x = node.tx;
|
|
818
|
-
node.y = node.ty;
|
|
819
|
-
node.r = node.tr;
|
|
820
|
-
});
|
|
821
|
-
this.onTickFrame(1);
|
|
822
|
-
this.emitUpdate("positions");
|
|
823
|
-
}
|
|
824
|
-
}
|
|
825
|
-
/**
|
|
826
|
-
* Per-frame hook called after node x/y/r are interpolated and before the
|
|
827
|
-
* 'positions' emit. Subclasses override to update edge geometry (`edge.path`)
|
|
828
|
-
* so curves stay glued to moving endpoints. `t` is the eased progress in [0, 1].
|
|
829
|
-
*/
|
|
830
|
-
onTickFrame(t) {
|
|
831
|
-
}
|
|
832
|
-
async onStop() {
|
|
833
|
-
this._timer?.stop();
|
|
834
|
-
this._timer = void 0;
|
|
835
|
-
}
|
|
836
|
-
doRadialLayout() {
|
|
837
|
-
if (!this.context.size) {
|
|
838
|
-
return;
|
|
839
|
-
}
|
|
840
|
-
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);
|
|
841
|
-
this.layout.graph.nodes.forEach((node, i) => {
|
|
842
|
-
const [tx, ty] = radial(i);
|
|
843
|
-
updateNode(node, [
|
|
844
|
-
tx,
|
|
845
|
-
ty
|
|
846
|
-
]);
|
|
847
|
-
});
|
|
848
|
-
this.animate();
|
|
849
|
-
}
|
|
850
|
-
};
|
|
851
|
-
var updateNode = (node, [tx, ty], tr = 8) => {
|
|
852
|
-
const sx = node.x ?? tx;
|
|
853
|
-
const sy = node.y ?? ty;
|
|
854
|
-
const sr = node.r ?? tr;
|
|
855
|
-
Object.assign(node, {
|
|
856
|
-
initialized: true,
|
|
857
|
-
// Start.
|
|
858
|
-
sx,
|
|
859
|
-
sy,
|
|
860
|
-
sr,
|
|
861
|
-
// Target.
|
|
862
|
-
tx,
|
|
863
|
-
ty,
|
|
864
|
-
tr,
|
|
865
|
-
// Current.
|
|
866
|
-
x: sx,
|
|
867
|
-
y: sy,
|
|
868
|
-
r: sr
|
|
869
|
-
});
|
|
870
|
-
};
|
|
871
|
-
var layoutRadial = (x, y, r, n, start = -Math.PI / 2) => {
|
|
872
|
-
const da = 2 * Math.PI / n;
|
|
873
|
-
return (i) => {
|
|
874
|
-
const a = start + i * da;
|
|
875
|
-
return [
|
|
876
|
-
x + Math.cos(a) * r,
|
|
877
|
-
y + Math.sin(a) * r
|
|
878
|
-
];
|
|
879
|
-
};
|
|
880
|
-
};
|
|
881
|
-
var layoutHorizontal = (x, y, w, n) => {
|
|
882
|
-
const dx = n > 1 ? w / (n - 1) : 0;
|
|
883
|
-
const sx = x - (n - 1) * dx / 2;
|
|
884
|
-
return (i) => {
|
|
885
|
-
return [
|
|
886
|
-
sx + i * dx,
|
|
887
|
-
y
|
|
888
|
-
];
|
|
889
|
-
};
|
|
890
|
-
};
|
|
891
|
-
var layoutVertical = (x, y, h, n) => {
|
|
892
|
-
const dy = n > 1 ? h / (n - 1) : 0;
|
|
893
|
-
const sy = y - (n - 1) * dy / 2;
|
|
894
|
-
return (i) => {
|
|
895
|
-
return [
|
|
896
|
-
x,
|
|
897
|
-
sy + i * dy
|
|
898
|
-
];
|
|
899
|
-
};
|
|
900
|
-
};
|
|
901
|
-
|
|
902
|
-
// src/graph/projector/graph-bundle-projector.ts
|
|
903
|
-
var __dxlog_file4 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/projector/graph-bundle-projector.ts";
|
|
904
|
-
var ROOT_ID = "__bundle_root__";
|
|
905
|
-
var GROUP_PREFIX = "__bundle_group__:";
|
|
906
|
-
var cartesianToPolar = (x, y) => ({
|
|
907
|
-
angle: Math.atan2(x, -y),
|
|
908
|
-
radius: Math.hypot(x, y)
|
|
909
|
-
});
|
|
910
|
-
var GraphBundleProjector = class extends GraphRadialProjector {
|
|
911
|
-
// Per-edge polar point arrays. Points 0 and N-1 are the source / target leaves and are
|
|
912
|
-
// overwritten from current cartesian on every tick; intermediate points stay fixed at
|
|
913
|
-
// their cluster-layout polar coordinates.
|
|
914
|
-
#edgePoints = /* @__PURE__ */ new Map();
|
|
915
|
-
#lineGen = lineRadial().radius((d) => d.radius).angle((d) => d.angle);
|
|
916
|
-
onUpdate(graph) {
|
|
917
|
-
log2("onUpdate", {
|
|
918
|
-
graph: {
|
|
919
|
-
nodes: graph?.nodes.length,
|
|
920
|
-
edges: graph?.edges.length
|
|
921
|
-
}
|
|
922
|
-
}, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 40, S: this });
|
|
923
|
-
this.mergeData(graph);
|
|
924
|
-
this.doBundleLayout();
|
|
925
|
-
this.emitUpdate("topology");
|
|
926
|
-
this.animate();
|
|
927
|
-
}
|
|
928
|
-
/**
|
|
929
|
-
* Refresh leaf endpoint polar values from current cartesian so the curve tracks
|
|
930
|
-
* its endpoints during the tween. Internal hierarchy positions stay constant.
|
|
931
|
-
*/
|
|
932
|
-
onTickFrame(_t) {
|
|
933
|
-
for (const edge of this.layout.graph.edges) {
|
|
934
|
-
if (edge.type !== "bundle") {
|
|
935
|
-
continue;
|
|
936
|
-
}
|
|
937
|
-
const points = this.#edgePoints.get(edge.id);
|
|
938
|
-
if (!points || points.length < 2) {
|
|
939
|
-
continue;
|
|
940
|
-
}
|
|
941
|
-
const s = cartesianToPolar(edge.source.x ?? 0, edge.source.y ?? 0);
|
|
942
|
-
const t = cartesianToPolar(edge.target.x ?? 0, edge.target.y ?? 0);
|
|
943
|
-
points[0] = {
|
|
944
|
-
angle: s.angle,
|
|
945
|
-
radius: s.radius
|
|
946
|
-
};
|
|
947
|
-
points[points.length - 1] = {
|
|
948
|
-
angle: t.angle,
|
|
949
|
-
radius: t.radius
|
|
950
|
-
};
|
|
951
|
-
edge.path = this.#lineGen(points) ?? void 0;
|
|
952
|
-
}
|
|
953
|
-
}
|
|
954
|
-
doBundleLayout() {
|
|
955
|
-
if (!this.context.size) {
|
|
956
|
-
return;
|
|
957
|
-
}
|
|
958
|
-
const dataNodes = [
|
|
959
|
-
...this.layout.graph.nodes
|
|
960
|
-
];
|
|
961
|
-
if (!dataNodes.length) {
|
|
962
|
-
return;
|
|
963
|
-
}
|
|
964
|
-
const groupOf = this.options.groupOf;
|
|
965
|
-
const items = [
|
|
966
|
-
{
|
|
967
|
-
id: ROOT_ID
|
|
968
|
-
}
|
|
969
|
-
];
|
|
970
|
-
const groupIds = /* @__PURE__ */ new Map();
|
|
971
|
-
for (const node of dataNodes) {
|
|
972
|
-
const key = groupOf?.(node);
|
|
973
|
-
let parent = ROOT_ID;
|
|
974
|
-
if (key !== void 0) {
|
|
975
|
-
let groupId = groupIds.get(key);
|
|
976
|
-
if (!groupId) {
|
|
977
|
-
groupId = `${GROUP_PREFIX}${key}`;
|
|
978
|
-
groupIds.set(key, groupId);
|
|
979
|
-
items.push({
|
|
980
|
-
id: groupId,
|
|
981
|
-
parent: ROOT_ID
|
|
982
|
-
});
|
|
983
|
-
}
|
|
984
|
-
parent = groupId;
|
|
985
|
-
}
|
|
986
|
-
items.push({
|
|
987
|
-
id: node.id,
|
|
988
|
-
parent,
|
|
989
|
-
node
|
|
990
|
-
});
|
|
991
|
-
}
|
|
992
|
-
const childrenByParent = /* @__PURE__ */ new Map();
|
|
993
|
-
for (const item of items) {
|
|
994
|
-
if (item.parent) {
|
|
995
|
-
const list = childrenByParent.get(item.parent) ?? [];
|
|
996
|
-
list.push(item);
|
|
997
|
-
childrenByParent.set(item.parent, list);
|
|
998
|
-
}
|
|
999
|
-
}
|
|
1000
|
-
const root = hierarchy({
|
|
1001
|
-
id: ROOT_ID
|
|
1002
|
-
}, (d) => childrenByParent.get(d.id) ?? []);
|
|
1003
|
-
const { width, height } = this.context.size;
|
|
1004
|
-
const margin = this.options.margin ?? 120;
|
|
1005
|
-
const ringRadius = Math.max(0, Math.min(width, height) / 2 - margin);
|
|
1006
|
-
d3Cluster().size([
|
|
1007
|
-
2 * Math.PI,
|
|
1008
|
-
ringRadius
|
|
1009
|
-
]).separation((a, b) => (a.parent === b.parent ? 1 : 2) / Math.max(1, a.depth))(root);
|
|
1010
|
-
const leafR = this.options.leafRadius ?? 4;
|
|
1011
|
-
const tension = this.options.tension ?? 0.85;
|
|
1012
|
-
this.#lineGen = lineRadial().curve(curveBundle.beta(tension)).radius((d) => d.radius).angle((d) => d.angle);
|
|
1013
|
-
const hierById = /* @__PURE__ */ new Map();
|
|
1014
|
-
root.each((d) => {
|
|
1015
|
-
hierById.set(d.data.id, d);
|
|
1016
|
-
});
|
|
1017
|
-
root.leaves().forEach((d) => {
|
|
1018
|
-
const angle = d.x;
|
|
1019
|
-
const radius = d.y;
|
|
1020
|
-
const x = Math.cos(angle - Math.PI / 2) * radius;
|
|
1021
|
-
const y = Math.sin(angle - Math.PI / 2) * radius;
|
|
1022
|
-
if (d.data.node) {
|
|
1023
|
-
updateNode(d.data.node, [
|
|
1024
|
-
x,
|
|
1025
|
-
y
|
|
1026
|
-
], leafR);
|
|
1027
|
-
}
|
|
1028
|
-
});
|
|
1029
|
-
this.layout.graph.nodes = dataNodes;
|
|
1030
|
-
this.#edgePoints.clear();
|
|
1031
|
-
const incomingEdges = [
|
|
1032
|
-
...this.layout.graph.edges
|
|
1033
|
-
];
|
|
1034
|
-
const bundleEdges = [];
|
|
1035
|
-
for (const edge of incomingEdges) {
|
|
1036
|
-
const sourceHier = hierById.get(edge.source.id);
|
|
1037
|
-
const targetHier = hierById.get(edge.target.id);
|
|
1038
|
-
if (!sourceHier || !targetHier || sourceHier === targetHier) {
|
|
1039
|
-
continue;
|
|
1040
|
-
}
|
|
1041
|
-
const pathNodes = sourceHier.path(targetHier);
|
|
1042
|
-
const points = pathNodes.map((n) => ({
|
|
1043
|
-
angle: n.x,
|
|
1044
|
-
radius: n.y
|
|
1045
|
-
}));
|
|
1046
|
-
this.#edgePoints.set(edge.id, points);
|
|
1047
|
-
bundleEdges.push({
|
|
1048
|
-
id: edge.id,
|
|
1049
|
-
type: "bundle",
|
|
1050
|
-
source: edge.source,
|
|
1051
|
-
target: edge.target,
|
|
1052
|
-
data: edge.data
|
|
1053
|
-
});
|
|
1054
|
-
}
|
|
1055
|
-
this.layout.graph.edges = bundleEdges;
|
|
1056
|
-
this.onTickFrame(0);
|
|
1057
|
-
}
|
|
1058
|
-
};
|
|
1059
|
-
|
|
1060
|
-
// src/graph/projector/graph-cluster-projector.ts
|
|
1061
|
-
import { cluster as d3Cluster2, hierarchy as hierarchy2, linkRadial } from "d3";
|
|
1062
|
-
import { log as log3 } from "@dxos/log";
|
|
1063
|
-
var __dxlog_file5 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/projector/graph-cluster-projector.ts";
|
|
1064
|
-
var ROOT_ID2 = "__cluster_root__";
|
|
1065
|
-
var GROUP_PREFIX2 = "__group__:";
|
|
1066
|
-
var HIER_EDGE_PREFIX = "__hier__:";
|
|
1067
|
-
var CLUSTER_NODE_TYPE_LEAF = "leaf";
|
|
1068
|
-
var CLUSTER_NODE_TYPE_GROUP = "group";
|
|
1069
|
-
var CLUSTER_NODE_TYPE_ROOT = "root";
|
|
1070
|
-
var linkPath = linkRadial().angle((d) => d.x).radius((d) => d.y);
|
|
1071
|
-
var cartesianToPolar2 = (x, y) => ({
|
|
1072
|
-
angle: Math.atan2(x, -y),
|
|
1073
|
-
radius: Math.hypot(x, y)
|
|
1074
|
-
});
|
|
1075
|
-
var GraphClusterProjector = class extends GraphRadialProjector {
|
|
1076
|
-
// Synthetic-node ids (root id, or `${GROUP_PREFIX}${key}`) whose subtree is currently
|
|
1077
|
-
// hidden. Clicking the corresponding rendered circle calls toggleCollapsed to flip
|
|
1078
|
-
// membership, then re-runs the layout so the renderer's exit transition fades the
|
|
1079
|
-
// hidden leaves + hierarchy edges out (and back in on expand).
|
|
1080
|
-
#collapsed = /* @__PURE__ */ new Set();
|
|
1081
|
-
// Snapshot of the full data-node set as of the last mergeData. doClusterLayout always
|
|
1082
|
-
// reads from this rather than `layout.graph.nodes` — the layout view is filtered (it
|
|
1083
|
-
// drops hidden leaves so the renderer doesn't see them), so re-reading from there
|
|
1084
|
-
// would permanently lose the hidden leaves and they couldn't reappear on expand.
|
|
1085
|
-
#dataNodes = [];
|
|
1086
|
-
/**
|
|
1087
|
-
* Built-in click semantics: clicking a synthetic root / group node toggles its subtree.
|
|
1088
|
-
* Leaf clicks fall through to the consumer's `onSelect` (returns `false`).
|
|
1089
|
-
*/
|
|
1090
|
-
handleNodeClick(node, _event) {
|
|
1091
|
-
if (node.type !== CLUSTER_NODE_TYPE_ROOT && node.type !== CLUSTER_NODE_TYPE_GROUP) {
|
|
1092
|
-
return false;
|
|
1093
|
-
}
|
|
1094
|
-
this.toggleCollapsed(node.id);
|
|
1095
|
-
return true;
|
|
1096
|
-
}
|
|
1097
|
-
/** Toggle collapse state for a synthetic node id. Triggers a topology re-emit. */
|
|
1098
|
-
toggleCollapsed(id) {
|
|
1099
|
-
if (this.#collapsed.has(id)) {
|
|
1100
|
-
this.#collapsed.delete(id);
|
|
1101
|
-
} else {
|
|
1102
|
-
this.#collapsed.add(id);
|
|
1103
|
-
}
|
|
1104
|
-
this.doClusterLayout();
|
|
1105
|
-
this.emitUpdate("topology");
|
|
1106
|
-
this.animate();
|
|
1107
|
-
}
|
|
1108
|
-
/** Whether a synthetic-node id is currently collapsed. */
|
|
1109
|
-
isCollapsed(id) {
|
|
1110
|
-
return this.#collapsed.has(id);
|
|
1111
|
-
}
|
|
1112
|
-
onUpdate(graph) {
|
|
1113
|
-
log3("onUpdate", {
|
|
1114
|
-
graph: {
|
|
1115
|
-
nodes: graph?.nodes.length,
|
|
1116
|
-
edges: graph?.edges.length
|
|
1117
|
-
}
|
|
1118
|
-
}, { "~LogMeta": "~LogMeta", F: __dxlog_file5, L: 79, S: this });
|
|
1119
|
-
this.mergeData(graph);
|
|
1120
|
-
this.#dataNodes = [
|
|
1121
|
-
...this.layout.graph.nodes
|
|
1122
|
-
];
|
|
1123
|
-
this.doClusterLayout();
|
|
1124
|
-
this.emitUpdate("topology");
|
|
1125
|
-
this.animate();
|
|
1126
|
-
}
|
|
1127
|
-
/**
|
|
1128
|
-
* Recompute each hierarchy edge's `path` from current cartesian source/target positions,
|
|
1129
|
-
* so the curves track moving endpoints during a cross-variant tween (e.g. force → cluster)
|
|
1130
|
-
* instead of being pinned to the final polar layout.
|
|
1131
|
-
*/
|
|
1132
|
-
onTickFrame(_t) {
|
|
1133
|
-
for (const edge of this.layout.graph.edges) {
|
|
1134
|
-
if (edge.type !== "hierarchy") {
|
|
1135
|
-
continue;
|
|
1136
|
-
}
|
|
1137
|
-
const { source, target } = edge;
|
|
1138
|
-
const s = cartesianToPolar2(source.x ?? 0, source.y ?? 0);
|
|
1139
|
-
const t = cartesianToPolar2(target.x ?? 0, target.y ?? 0);
|
|
1140
|
-
edge.path = linkPath({
|
|
1141
|
-
source: {
|
|
1142
|
-
x: s.angle,
|
|
1143
|
-
y: s.radius
|
|
1144
|
-
},
|
|
1145
|
-
target: {
|
|
1146
|
-
x: t.angle,
|
|
1147
|
-
y: t.radius
|
|
1148
|
-
}
|
|
1149
|
-
});
|
|
1150
|
-
}
|
|
1151
|
-
}
|
|
1152
|
-
doClusterLayout() {
|
|
1153
|
-
if (!this.context.size) {
|
|
1154
|
-
return;
|
|
1155
|
-
}
|
|
1156
|
-
const dataNodes = this.#dataNodes;
|
|
1157
|
-
if (!dataNodes.length) {
|
|
1158
|
-
return;
|
|
1159
|
-
}
|
|
1160
|
-
const groupOf = this.options.groupOf;
|
|
1161
|
-
const rootId = this.options.rootId ?? ROOT_ID2;
|
|
1162
|
-
const items = [
|
|
1163
|
-
{
|
|
1164
|
-
id: rootId
|
|
1165
|
-
}
|
|
1166
|
-
];
|
|
1167
|
-
const groupIds = /* @__PURE__ */ new Map();
|
|
1168
|
-
const rootCollapsed = this.#collapsed.has(rootId);
|
|
1169
|
-
if (!rootCollapsed) {
|
|
1170
|
-
for (const node of dataNodes) {
|
|
1171
|
-
const key = groupOf?.(node);
|
|
1172
|
-
let parent = rootId;
|
|
1173
|
-
if (key !== void 0) {
|
|
1174
|
-
let groupId = groupIds.get(key);
|
|
1175
|
-
if (!groupId) {
|
|
1176
|
-
groupId = `${GROUP_PREFIX2}${key}`;
|
|
1177
|
-
groupIds.set(key, groupId);
|
|
1178
|
-
items.push({
|
|
1179
|
-
id: groupId,
|
|
1180
|
-
parent: rootId,
|
|
1181
|
-
groupKey: key
|
|
1182
|
-
});
|
|
1183
|
-
}
|
|
1184
|
-
parent = groupId;
|
|
1185
|
-
}
|
|
1186
|
-
if (this.#collapsed.has(parent)) {
|
|
1187
|
-
continue;
|
|
1188
|
-
}
|
|
1189
|
-
items.push({
|
|
1190
|
-
id: node.id,
|
|
1191
|
-
parent,
|
|
1192
|
-
node
|
|
1193
|
-
});
|
|
1194
|
-
}
|
|
1195
|
-
}
|
|
1196
|
-
const childrenByParent = /* @__PURE__ */ new Map();
|
|
1197
|
-
for (const item of items) {
|
|
1198
|
-
if (item.parent) {
|
|
1199
|
-
const list = childrenByParent.get(item.parent) ?? [];
|
|
1200
|
-
list.push(item);
|
|
1201
|
-
childrenByParent.set(item.parent, list);
|
|
1202
|
-
}
|
|
1203
|
-
}
|
|
1204
|
-
const root = hierarchy2({
|
|
1205
|
-
id: rootId
|
|
1206
|
-
}, (d) => childrenByParent.get(d.id) ?? []);
|
|
1207
|
-
const { width, height } = this.context.size;
|
|
1208
|
-
const margin = this.options.margin ?? 80;
|
|
1209
|
-
const ringRadius = Math.max(0, Math.min(width, height) / 2 - margin);
|
|
1210
|
-
d3Cluster2().size([
|
|
1211
|
-
2 * Math.PI,
|
|
1212
|
-
ringRadius
|
|
1213
|
-
]).separation((a, b) => (a.parent === b.parent ? 1 : 2) / Math.max(1, a.depth))(root);
|
|
1214
|
-
const leafR = this.options.leafRadius ?? 4;
|
|
1215
|
-
const groupR = this.options.groupRadius ?? 3;
|
|
1216
|
-
const rootR = this.options.rootRadius ?? 5;
|
|
1217
|
-
const prevStubsById = /* @__PURE__ */ new Map();
|
|
1218
|
-
for (const node of this.layout.graph.nodes) {
|
|
1219
|
-
if (node.type === CLUSTER_NODE_TYPE_ROOT || node.type === CLUSTER_NODE_TYPE_GROUP) {
|
|
1220
|
-
prevStubsById.set(node.id, node);
|
|
1221
|
-
}
|
|
1222
|
-
}
|
|
1223
|
-
const syntheticNodes = /* @__PURE__ */ new Map();
|
|
1224
|
-
root.each((d) => {
|
|
1225
|
-
const id = d.data.id;
|
|
1226
|
-
const angle = d.x;
|
|
1227
|
-
const radius = d.y;
|
|
1228
|
-
const x = Math.cos(angle - Math.PI / 2) * radius;
|
|
1229
|
-
const y = Math.sin(angle - Math.PI / 2) * radius;
|
|
1230
|
-
if (d.data.node) {
|
|
1231
|
-
updateNode(d.data.node, [
|
|
1232
|
-
x,
|
|
1233
|
-
y
|
|
1234
|
-
], leafR);
|
|
1235
|
-
d.data.node.type = CLUSTER_NODE_TYPE_LEAF;
|
|
1236
|
-
d.data.node.hidden = false;
|
|
1237
|
-
} else {
|
|
1238
|
-
const isRoot = id === rootId;
|
|
1239
|
-
const stub = prevStubsById.get(id) ?? {
|
|
1240
|
-
id,
|
|
1241
|
-
x: 0,
|
|
1242
|
-
y: 0
|
|
1243
|
-
};
|
|
1244
|
-
updateNode(stub, [
|
|
1245
|
-
x,
|
|
1246
|
-
y
|
|
1247
|
-
], isRoot ? rootR : groupR);
|
|
1248
|
-
stub.type = isRoot ? CLUSTER_NODE_TYPE_ROOT : CLUSTER_NODE_TYPE_GROUP;
|
|
1249
|
-
if (isRoot) {
|
|
1250
|
-
stub.label = this.options.rootLabel;
|
|
1251
|
-
} else if (d.data.groupKey !== void 0) {
|
|
1252
|
-
stub.label = this.options.groupLabel ? this.options.groupLabel(d.data.groupKey) : d.data.groupKey;
|
|
1253
|
-
}
|
|
1254
|
-
syntheticNodes.set(id, stub);
|
|
1255
|
-
}
|
|
1256
|
-
});
|
|
1257
|
-
const visibleLeafIds = /* @__PURE__ */ new Set();
|
|
1258
|
-
for (const item of items) {
|
|
1259
|
-
if (item.node) {
|
|
1260
|
-
visibleLeafIds.add(item.id);
|
|
1261
|
-
}
|
|
1262
|
-
}
|
|
1263
|
-
for (const node of dataNodes) {
|
|
1264
|
-
if (visibleLeafIds.has(node.id)) {
|
|
1265
|
-
continue;
|
|
1266
|
-
}
|
|
1267
|
-
const key = groupOf?.(node);
|
|
1268
|
-
const parentId = key !== void 0 && groupIds.has(key) ? groupIds.get(key) : rootId;
|
|
1269
|
-
const parentStub = syntheticNodes.get(parentId);
|
|
1270
|
-
const targetX = parentStub?.tx ?? 0;
|
|
1271
|
-
const targetY = parentStub?.ty ?? 0;
|
|
1272
|
-
updateNode(node, [
|
|
1273
|
-
targetX,
|
|
1274
|
-
targetY
|
|
1275
|
-
], leafR);
|
|
1276
|
-
node.type = CLUSTER_NODE_TYPE_LEAF;
|
|
1277
|
-
node.hidden = true;
|
|
1278
|
-
}
|
|
1279
|
-
const nextNodes = [];
|
|
1280
|
-
for (const stub of syntheticNodes.values()) {
|
|
1281
|
-
nextNodes.push(stub);
|
|
1282
|
-
}
|
|
1283
|
-
for (const node of dataNodes) {
|
|
1284
|
-
nextNodes.push(node);
|
|
1285
|
-
}
|
|
1286
|
-
this.layout.graph.nodes = nextNodes;
|
|
1287
|
-
const hierarchyEdges = [];
|
|
1288
|
-
root.links().forEach((link) => {
|
|
1289
|
-
const sid = link.source.data.id;
|
|
1290
|
-
const tid = link.target.data.id;
|
|
1291
|
-
const sourceNode = link.source.data.node ?? syntheticNodes.get(sid);
|
|
1292
|
-
const targetNode = link.target.data.node ?? syntheticNodes.get(tid);
|
|
1293
|
-
hierarchyEdges.push({
|
|
1294
|
-
id: `${HIER_EDGE_PREFIX}${sid}->${tid}`,
|
|
1295
|
-
type: "hierarchy",
|
|
1296
|
-
source: sourceNode,
|
|
1297
|
-
target: targetNode,
|
|
1298
|
-
data: void 0
|
|
1299
|
-
});
|
|
1300
|
-
});
|
|
1301
|
-
this.layout.graph.edges = hierarchyEdges;
|
|
1302
|
-
this.onTickFrame(0);
|
|
1303
|
-
}
|
|
1304
|
-
};
|
|
1305
|
-
|
|
1306
|
-
// src/graph/projector/graph-drag.ts
|
|
1307
|
-
import { drag, pointer, select as select2 } from "d3";
|
|
1308
|
-
var defaultDragOptions = {
|
|
1309
|
-
linkMod: "metaKey",
|
|
1310
|
-
freezeMod: "shiftKey"
|
|
1311
|
-
};
|
|
1312
|
-
var createGraphDrag = (context, projector, options = defaultDragOptions) => {
|
|
1313
|
-
let mode;
|
|
1314
|
-
let source;
|
|
1315
|
-
let target;
|
|
1316
|
-
let offset;
|
|
1317
|
-
let moved = false;
|
|
1318
|
-
const keyMod = (event, key) => {
|
|
1319
|
-
const modKey = options?.[key] ?? defaultDragOptions[key];
|
|
1320
|
-
return modKey === void 0 || event[modKey];
|
|
1321
|
-
};
|
|
1322
|
-
return drag().filter((event) => !event.ctrlKey).on("start", function(event, d) {
|
|
1323
|
-
source = d;
|
|
1324
|
-
offset = pointer(event, this.parentElement);
|
|
1325
|
-
if (options?.onDrop && keyMod(event.sourceEvent, "linkMod")) {
|
|
1326
|
-
mode = 1;
|
|
1327
|
-
} else if (keyMod(event.sourceEvent, "dragMod")) {
|
|
1328
|
-
mode = 0;
|
|
1329
|
-
}
|
|
1330
|
-
}).on("drag", function(event, d) {
|
|
1331
|
-
const parent = this.closest("g.node");
|
|
1332
|
-
select2(parent).classed("dragging", true).raise();
|
|
1333
|
-
const [dx, dy] = pointer(event, this.parentElement);
|
|
1334
|
-
const x = d.x + dx - offset[0];
|
|
1335
|
-
const y = d.y + dy - offset[1];
|
|
1336
|
-
const point = [
|
|
1337
|
-
x,
|
|
1338
|
-
y
|
|
1339
|
-
];
|
|
1340
|
-
switch (mode) {
|
|
1341
|
-
case 0: {
|
|
1342
|
-
select2(context.svg).attr("cursor", "grabbing");
|
|
1343
|
-
event.subject.x = event.subject.fx = point[0];
|
|
1344
|
-
event.subject.y = event.subject.fy = point[1];
|
|
1345
|
-
projector.refresh(true);
|
|
1346
|
-
moved = true;
|
|
1347
|
-
break;
|
|
1348
|
-
}
|
|
1349
|
-
case 1: {
|
|
1350
|
-
if (options?.onDrag) {
|
|
1351
|
-
target = projector.findNode(event.x, event.y, 16);
|
|
1352
|
-
if (source === target) {
|
|
1353
|
-
select2(context.svg).attr("cursor", void 0);
|
|
1354
|
-
options?.onDrag?.();
|
|
1355
|
-
} else {
|
|
1356
|
-
select2(context.svg).attr("cursor", target ? "alias" : "cell");
|
|
1357
|
-
options?.onDrag?.(source, target, point);
|
|
1358
|
-
}
|
|
1359
|
-
}
|
|
1360
|
-
}
|
|
1361
|
-
}
|
|
1362
|
-
}).on("end", function(event) {
|
|
1363
|
-
select2(context.svg).attr("cursor", void 0);
|
|
1364
|
-
select2(this.parentElement).classed("dragging", false);
|
|
1365
|
-
switch (mode) {
|
|
1366
|
-
case 1: {
|
|
1367
|
-
options?.onDrop?.(source, target);
|
|
1368
|
-
break;
|
|
1369
|
-
}
|
|
1370
|
-
}
|
|
1371
|
-
if (!keyMod(event.sourceEvent, "freezeMod")) {
|
|
1372
|
-
event.subject.fx = null;
|
|
1373
|
-
event.subject.fy = null;
|
|
1374
|
-
}
|
|
1375
|
-
mode = void 0;
|
|
1376
|
-
source = void 0;
|
|
1377
|
-
target = void 0;
|
|
1378
|
-
offset = void 0;
|
|
1379
|
-
if (moved) {
|
|
1380
|
-
projector.refresh(false);
|
|
1381
|
-
moved = false;
|
|
1382
|
-
}
|
|
1383
|
-
});
|
|
1384
|
-
};
|
|
1385
|
-
|
|
1386
|
-
// src/graph/projector/graph-force-projector.ts
|
|
1387
|
-
import { forceCenter, forceCollide, forceLink, forceManyBody, forceRadial, forceSimulation, forceX as forceX2, forceY as forceY2 } from "d3";
|
|
1388
|
-
import { log as log4 } from "@dxos/log";
|
|
1389
|
-
|
|
1390
|
-
// src/graph/projector/graph-forces.ts
|
|
1391
|
-
import { forceX, forceY } from "d3";
|
|
1392
|
-
var forcePoint = ({ x, y, strength = 0.01 }) => {
|
|
1393
|
-
const fx = x != null ? forceX(x).strength(strength) : void 0;
|
|
1394
|
-
const fy = y != null ? forceY(y).strength(strength) : void 0;
|
|
1395
|
-
const force = (alpha) => {
|
|
1396
|
-
fx?.(alpha);
|
|
1397
|
-
fy?.(alpha);
|
|
1398
|
-
};
|
|
1399
|
-
force.initialize = (nodes, random) => {
|
|
1400
|
-
fx?.initialize(nodes, random);
|
|
1401
|
-
fy?.initialize(nodes, random);
|
|
1402
|
-
};
|
|
1403
|
-
return force;
|
|
1404
|
-
};
|
|
1405
|
-
|
|
1406
|
-
// src/graph/projector/graph-force-projector.ts
|
|
1407
|
-
var __dxlog_file6 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/projector/graph-force-projector.ts";
|
|
1408
|
-
var getValue = (valueOrFunction, invoker, defaultValue) => {
|
|
1409
|
-
return (typeof valueOrFunction === "function" ? invoker(valueOrFunction) : valueOrFunction) ?? defaultValue;
|
|
1410
|
-
};
|
|
1411
|
-
var maybeForce = (options, cb, defaultValue = void 0) => {
|
|
1412
|
-
const value = typeof options === "boolean" ? options ? {} : void 0 : options ?? defaultValue;
|
|
1413
|
-
if (value) {
|
|
1414
|
-
return cb(value);
|
|
1415
|
-
}
|
|
1416
|
-
};
|
|
1417
|
-
var defaultForceOptions = {
|
|
1418
|
-
link: true,
|
|
1419
|
-
manyBody: true,
|
|
1420
|
-
// center: true,
|
|
1421
|
-
point: true
|
|
1422
|
-
};
|
|
1423
|
-
var GraphForceProjector = class extends GraphProjector {
|
|
1424
|
-
// https://github.com/d3/d3-force
|
|
1425
|
-
_simulation = forceSimulation();
|
|
1426
|
-
_timeout;
|
|
1427
|
-
get forces() {
|
|
1428
|
-
return this.options.forces ?? defaultForceOptions;
|
|
1429
|
-
}
|
|
1430
|
-
get simulation() {
|
|
1431
|
-
return this._simulation;
|
|
1432
|
-
}
|
|
1433
|
-
restart() {
|
|
1434
|
-
this._simulation.alpha(1).restart();
|
|
1435
|
-
}
|
|
1436
|
-
findNode(x, y, radius) {
|
|
1437
|
-
return this._simulation.find(x, y, radius);
|
|
1438
|
-
}
|
|
1439
|
-
async onStart() {
|
|
1440
|
-
clearTimeout(this._timeout);
|
|
1441
|
-
let propagating = true;
|
|
1442
|
-
const { radial, ...forces } = this.forces;
|
|
1443
|
-
const delay = typeof radial === "object" ? radial.delay : void 0;
|
|
1444
|
-
if (delay) {
|
|
1445
|
-
propagating = false;
|
|
1446
|
-
this.updateForces(forces);
|
|
1447
|
-
this._timeout = setTimeout(() => {
|
|
1448
|
-
this.updateForces(this.forces);
|
|
1449
|
-
this.restart();
|
|
1450
|
-
this._timeout = setTimeout(() => {
|
|
1451
|
-
propagating = true;
|
|
1452
|
-
}, delay);
|
|
1453
|
-
}, delay);
|
|
1454
|
-
} else {
|
|
1455
|
-
this.updateForces(this.forces);
|
|
1456
|
-
}
|
|
1457
|
-
this._simulation.on("tick", () => propagating && this.emitUpdate("positions")).velocityDecay(0.3).alphaDecay(1 - Math.pow(1e-3, 1 / 300)).alpha(1).restart();
|
|
1458
|
-
}
|
|
1459
|
-
async onStop() {
|
|
1460
|
-
clearTimeout(this._timeout);
|
|
1461
|
-
this._simulation.stop();
|
|
1462
|
-
}
|
|
1463
|
-
onRefresh(dragging = false) {
|
|
1464
|
-
if (this.forces.center) {
|
|
1465
|
-
if (dragging) {
|
|
1466
|
-
this._simulation.force("center", null);
|
|
1467
|
-
} else {
|
|
1468
|
-
this.updateForces(this.forces);
|
|
1469
|
-
}
|
|
1470
|
-
}
|
|
1471
|
-
this.restart();
|
|
1472
|
-
}
|
|
1473
|
-
onUpdate(graph) {
|
|
1474
|
-
log4("onUpdate", {
|
|
1475
|
-
graph: {
|
|
1476
|
-
nodes: graph?.nodes.length,
|
|
1477
|
-
edges: graph?.edges.length
|
|
1478
|
-
}
|
|
1479
|
-
}, { "~LogMeta": "~LogMeta", F: __dxlog_file6, L: 91, S: this });
|
|
1480
|
-
this._simulation.stop();
|
|
1481
|
-
this.mergeData(graph);
|
|
1482
|
-
this.updateLayout();
|
|
1483
|
-
this.updateForces(this.forces);
|
|
1484
|
-
this.emitUpdate("topology");
|
|
1485
|
-
this.restart();
|
|
1486
|
-
}
|
|
1487
|
-
updateLayout() {
|
|
1488
|
-
this.layout.guides = this.options.guides ? [
|
|
1489
|
-
{
|
|
1490
|
-
id: "g-1",
|
|
1491
|
-
type: "circle",
|
|
1492
|
-
cx: 0,
|
|
1493
|
-
cy: 0,
|
|
1494
|
-
r: this.context.scale.model.toValue([
|
|
1495
|
-
10,
|
|
1496
|
-
1
|
|
1497
|
-
])
|
|
1498
|
-
}
|
|
1499
|
-
] : void 0;
|
|
1500
|
-
this.layout.graph.nodes.forEach((node) => {
|
|
1501
|
-
if (!node.initialized) {
|
|
1502
|
-
const a = 2 * Math.PI * Math.random();
|
|
1503
|
-
const r = this.options.radius ?? 200;
|
|
1504
|
-
Object.assign(node, {
|
|
1505
|
-
initialized: true,
|
|
1506
|
-
// TODO(burdon): Only when new nodes added to initial layout.
|
|
1507
|
-
// Position around center or parent; must have delta to avoid spike.
|
|
1508
|
-
// x: edge?.source?.x ?? Math.cos(a) * r,
|
|
1509
|
-
// y: edge?.source?.y ?? Math.sin(a) * r,
|
|
1510
|
-
x: Math.cos(a) * r,
|
|
1511
|
-
y: Math.sin(a) * r
|
|
1512
|
-
});
|
|
1513
|
-
}
|
|
1514
|
-
Object.assign(node, {
|
|
1515
|
-
r: getValue(this.options?.attributes?.radius, (fn) => fn(node, this.numChildren(node)), 6)
|
|
1516
|
-
});
|
|
1517
|
-
});
|
|
1518
|
-
this.layout.graph.edges.forEach((edge) => {
|
|
1519
|
-
Object.assign(edge, {
|
|
1520
|
-
linkForce: getValue(this.options.attributes?.linkForce, (fn) => fn(edge), true)
|
|
1521
|
-
});
|
|
1522
|
-
});
|
|
1523
|
-
}
|
|
1524
|
-
/**
|
|
1525
|
-
* Update all forces.
|
|
1526
|
-
*/
|
|
1527
|
-
updateForces(forces) {
|
|
1528
|
-
log4("updateForces", {
|
|
1529
|
-
forces
|
|
1530
|
-
}, { "~LogMeta": "~LogMeta", F: __dxlog_file6, L: 153, S: this });
|
|
1531
|
-
this._simulation.nodes(this.layout.graph.nodes).force("link", maybeForce(forces?.link, (config) => {
|
|
1532
|
-
const force = forceLink().id((d) => d.id).links(this.layout.graph.edges.filter((edge) => edge.linkForce));
|
|
1533
|
-
if (config.distance != null) {
|
|
1534
|
-
force.distance(config.distance);
|
|
1535
|
-
}
|
|
1536
|
-
if (config.strength != null) {
|
|
1537
|
-
force.strength(config.strength);
|
|
1538
|
-
}
|
|
1539
|
-
if (config.iterations != null) {
|
|
1540
|
-
force.iterations(config.iterations);
|
|
1541
|
-
}
|
|
1542
|
-
return force;
|
|
1543
|
-
}, forces?.link === false ? void 0 : {})).force("manyBody", maybeForce(forces?.manyBody, (config) => {
|
|
1544
|
-
const force = forceManyBody();
|
|
1545
|
-
if (config.distanceMax != null) {
|
|
1546
|
-
force.distanceMax(config.distanceMax);
|
|
1547
|
-
}
|
|
1548
|
-
if (config.strength != null) {
|
|
1549
|
-
force.strength(config.strength);
|
|
1550
|
-
}
|
|
1551
|
-
return force;
|
|
1552
|
-
}, forces?.manyBody === false ? void 0 : {})).force("collide", maybeForce(forces?.collide, (config) => {
|
|
1553
|
-
const force = forceCollide();
|
|
1554
|
-
force.radius(config.radius ?? 8);
|
|
1555
|
-
if (config.strength != null) {
|
|
1556
|
-
force.strength(config.strength);
|
|
1557
|
-
}
|
|
1558
|
-
return force;
|
|
1559
|
-
})).force("radial", maybeForce(forces?.radial, (config) => {
|
|
1560
|
-
const force = forceRadial(config.radius ?? 100, config.x ?? 0, config.y ?? 0);
|
|
1561
|
-
if (config.strength != null) {
|
|
1562
|
-
force.strength(config.strength);
|
|
1563
|
-
}
|
|
1564
|
-
return force;
|
|
1565
|
-
})).force("center", maybeForce(forces?.center, (config) => {
|
|
1566
|
-
const force = forceCenter().x(config.x ?? 0).y(config.y ?? 0);
|
|
1567
|
-
if (config.strength != null) {
|
|
1568
|
-
force.strength(config.strength);
|
|
1569
|
-
}
|
|
1570
|
-
return force;
|
|
1571
|
-
})).force("point", maybeForce(forces?.point, ({ x = 0, y = 0, strength }) => {
|
|
1572
|
-
return forcePoint({
|
|
1573
|
-
x,
|
|
1574
|
-
y,
|
|
1575
|
-
strength
|
|
1576
|
-
});
|
|
1577
|
-
})).force("x", maybeForce(forces?.x, (config) => {
|
|
1578
|
-
const force = forceX2(config.x ?? 0);
|
|
1579
|
-
if (config.strength != null) {
|
|
1580
|
-
force.strength(config.strength);
|
|
1581
|
-
}
|
|
1582
|
-
return force;
|
|
1583
|
-
})).force("y", maybeForce(forces?.y, (config) => {
|
|
1584
|
-
const force = forceY2(config.y ?? 0);
|
|
1585
|
-
if (config.strength != null) {
|
|
1586
|
-
force.strength(config.strength);
|
|
1587
|
-
}
|
|
1588
|
-
return force;
|
|
1589
|
-
}));
|
|
1590
|
-
}
|
|
1591
|
-
};
|
|
1592
|
-
|
|
1593
|
-
// src/graph/projector/graph-hierarchical-projector.ts
|
|
1594
|
-
import { log as log5 } from "@dxos/log";
|
|
1595
|
-
var __dxlog_file7 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/projector/graph-hierarchical-projector.ts";
|
|
1596
|
-
var GraphHierarchicalProjector = class extends GraphRadialProjector {
|
|
1597
|
-
onUpdate(graph) {
|
|
1598
|
-
log5("onUpdate", {
|
|
1599
|
-
graph: {
|
|
1600
|
-
nodes: graph?.nodes.length,
|
|
1601
|
-
edges: graph?.edges.length
|
|
1602
|
-
},
|
|
1603
|
-
selection: this.selection?.getSelectedIds()
|
|
1604
|
-
}, { "~LogMeta": "~LogMeta", F: __dxlog_file7, L: 8, S: this });
|
|
1605
|
-
this.mergeData(graph);
|
|
1606
|
-
this.emitUpdate("topology");
|
|
1607
|
-
const selected = this.selection?.getSelectedIds()[0];
|
|
1608
|
-
if (selected) {
|
|
1609
|
-
const node = this.layout.graph.nodes.find((node2) => node2.id === selected);
|
|
1610
|
-
if (node) {
|
|
1611
|
-
this.doHierarchicalLayout(node);
|
|
1612
|
-
return;
|
|
1613
|
-
}
|
|
1614
|
-
}
|
|
1615
|
-
this.doRadialLayout();
|
|
1616
|
-
}
|
|
1617
|
-
doHierarchicalLayout(selected) {
|
|
1618
|
-
if (!this.context.size) {
|
|
1619
|
-
return;
|
|
1620
|
-
}
|
|
1621
|
-
const r2 = this.options.radius ?? Math.min(this.context.size.width, this.context.size.height) / 2 - (this.options.margin ?? 80);
|
|
1622
|
-
const r1 = r2 / 2;
|
|
1623
|
-
const children = this.layout.graph.edges.filter((edge) => edge.source.id === selected.id).map((edge) => edge.target);
|
|
1624
|
-
updateNode(selected, [
|
|
1625
|
-
0,
|
|
1626
|
-
0
|
|
1627
|
-
], 20);
|
|
1628
|
-
{
|
|
1629
|
-
const radial = layoutRadial(0, 0, r1, children.length);
|
|
1630
|
-
children.forEach((node, i) => {
|
|
1631
|
-
const [tx, ty] = radial(i);
|
|
1632
|
-
updateNode(node, [
|
|
1633
|
-
tx,
|
|
1634
|
-
ty
|
|
1635
|
-
]);
|
|
1636
|
-
});
|
|
1637
|
-
}
|
|
1638
|
-
{
|
|
1639
|
-
const radial = layoutRadial(0, 0, r2, this.layout.graph.nodes.length - 1 - children.length);
|
|
1640
|
-
this.layout.graph.nodes.filter((node) => node.id !== selected.id && !children.includes(node)).forEach((node, i) => {
|
|
1641
|
-
const [tx, ty] = radial(i);
|
|
1642
|
-
updateNode(node, [
|
|
1643
|
-
tx,
|
|
1644
|
-
ty
|
|
1645
|
-
]);
|
|
1646
|
-
});
|
|
1647
|
-
}
|
|
1648
|
-
this.animate();
|
|
1649
|
-
}
|
|
1650
|
-
};
|
|
1651
|
-
|
|
1652
|
-
// src/graph/projector/graph-lattice-projector.ts
|
|
1653
|
-
import { log as log6 } from "@dxos/log";
|
|
1654
|
-
var __dxlog_file8 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/projector/graph-lattice-projector.ts";
|
|
1655
|
-
var GraphLatticeProjector = class extends GraphRadialProjector {
|
|
1656
|
-
onUpdate(graph) {
|
|
1657
|
-
log6("onUpdate", {
|
|
1658
|
-
graph: {
|
|
1659
|
-
nodes: graph?.nodes.length,
|
|
1660
|
-
edges: graph?.edges.length
|
|
1661
|
-
}
|
|
1662
|
-
}, { "~LogMeta": "~LogMeta", F: __dxlog_file8, L: 14, S: this });
|
|
1663
|
-
this.mergeData(graph);
|
|
1664
|
-
this.doLatticeLayout();
|
|
1665
|
-
this.emitUpdate("topology");
|
|
1666
|
-
this.animate();
|
|
1667
|
-
}
|
|
1668
|
-
doLatticeLayout() {
|
|
1669
|
-
if (!this.context.size) {
|
|
1670
|
-
return;
|
|
1671
|
-
}
|
|
1672
|
-
const nodes = this.layout.graph.nodes;
|
|
1673
|
-
const count = nodes.length;
|
|
1674
|
-
if (!count) {
|
|
1675
|
-
return;
|
|
1676
|
-
}
|
|
1677
|
-
const sortBy = this.options.sortBy ?? ((n) => n.data?.label ?? n.id);
|
|
1678
|
-
nodes.sort((a, b) => sortBy(a).localeCompare(sortBy(b)));
|
|
1679
|
-
const columns = Math.max(1, Math.ceil(Math.sqrt(count)));
|
|
1680
|
-
const rows = Math.ceil(count / columns);
|
|
1681
|
-
const margin = this.options.margin ?? 16;
|
|
1682
|
-
const { width, height } = this.context.size;
|
|
1683
|
-
const innerW = Math.max(0, width - 2 * margin);
|
|
1684
|
-
const innerH = Math.max(0, height - 2 * margin);
|
|
1685
|
-
const r = this.options.radius ?? 6;
|
|
1686
|
-
const fit = Math.min(innerW / columns, innerH / rows);
|
|
1687
|
-
const cellSize = Math.max(0, Math.min(fit, r * 6));
|
|
1688
|
-
const gridW = cellSize * columns;
|
|
1689
|
-
const gridH = cellSize * rows;
|
|
1690
|
-
const offsetX = -gridW / 2 + cellSize / 2;
|
|
1691
|
-
const offsetY = -gridH / 2 + cellSize / 2;
|
|
1692
|
-
nodes.forEach((node, i) => {
|
|
1693
|
-
const tx = offsetX + i % columns * cellSize;
|
|
1694
|
-
const ty = offsetY + Math.floor(i / columns) * cellSize;
|
|
1695
|
-
updateNode(node, [
|
|
1696
|
-
tx,
|
|
1697
|
-
ty
|
|
1698
|
-
], r);
|
|
1699
|
-
});
|
|
1700
|
-
}
|
|
1701
|
-
};
|
|
1702
|
-
|
|
1703
|
-
// src/graph/projector/graph-plexus-projector.ts
|
|
1704
|
-
import { cluster as d3Cluster3, hierarchy as hierarchy3, linkRadial as linkRadial2 } from "d3";
|
|
1705
|
-
import { log as log7 } from "@dxos/log";
|
|
1706
|
-
var __dxlog_file9 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/projector/graph-plexus-projector.ts";
|
|
1707
|
-
var RELATION_PREFIX = "__plexus_relation__:";
|
|
1708
|
-
var HIER_EDGE_PREFIX2 = "__plexus_hier__:";
|
|
1709
|
-
var PLEXUS_NODE_TYPE_FOCUS = "plexus-focus";
|
|
1710
|
-
var PLEXUS_NODE_TYPE_RELATION = "plexus-relation";
|
|
1711
|
-
var PLEXUS_NODE_TYPE_OBJECT = "plexus-object";
|
|
1712
|
-
var linkPath2 = linkRadial2().angle((d) => d.x).radius((d) => d.y);
|
|
1713
|
-
var cartesianToPolar3 = (x, y) => ({
|
|
1714
|
-
angle: Math.atan2(x, -y),
|
|
1715
|
-
radius: Math.hypot(x, y)
|
|
1716
|
-
});
|
|
1717
|
-
var GraphPlexusProjector = class extends GraphRadialProjector {
|
|
1718
|
-
// Full node / edge snapshots taken right after mergeData — doPlexusLayout reads from
|
|
1719
|
-
// these because it overwrites layout.graph with the filtered focus-centric view.
|
|
1720
|
-
#dataNodes = [];
|
|
1721
|
-
#dataEdges = [];
|
|
1722
|
-
onUpdate(graph) {
|
|
1723
|
-
log7("onUpdate", {
|
|
1724
|
-
graph: {
|
|
1725
|
-
nodes: graph?.nodes.length,
|
|
1726
|
-
edges: graph?.edges.length
|
|
1727
|
-
}
|
|
1728
|
-
}, { "~LogMeta": "~LogMeta", F: __dxlog_file9, L: 48, S: this });
|
|
1729
|
-
this.mergeData(graph);
|
|
1730
|
-
this.#dataNodes = [
|
|
1731
|
-
...this.layout.graph.nodes
|
|
1732
|
-
];
|
|
1733
|
-
this.#dataEdges = [
|
|
1734
|
-
...this.layout.graph.edges
|
|
1735
|
-
];
|
|
1736
|
-
this.doPlexusLayout();
|
|
1737
|
-
this.emitUpdate("topology");
|
|
1738
|
-
this.animate();
|
|
1739
|
-
}
|
|
1740
|
-
/**
|
|
1741
|
-
* Recompute each hierarchy edge's `path` from current cartesian endpoints so curves track
|
|
1742
|
-
* moving nodes during the tween (identical strategy to the cluster projector).
|
|
1743
|
-
*/
|
|
1744
|
-
onTickFrame(_t) {
|
|
1745
|
-
for (const edge of this.layout.graph.edges) {
|
|
1746
|
-
if (edge.type !== "hierarchy") {
|
|
1747
|
-
continue;
|
|
1748
|
-
}
|
|
1749
|
-
const { source, target } = edge;
|
|
1750
|
-
const s = cartesianToPolar3(source.x ?? 0, source.y ?? 0);
|
|
1751
|
-
const t = cartesianToPolar3(target.x ?? 0, target.y ?? 0);
|
|
1752
|
-
edge.path = linkPath2({
|
|
1753
|
-
source: {
|
|
1754
|
-
x: s.angle,
|
|
1755
|
-
y: s.radius
|
|
1756
|
-
},
|
|
1757
|
-
target: {
|
|
1758
|
-
x: t.angle,
|
|
1759
|
-
y: t.radius
|
|
1760
|
-
}
|
|
1761
|
-
});
|
|
1762
|
-
}
|
|
1763
|
-
}
|
|
1764
|
-
doPlexusLayout() {
|
|
1765
|
-
if (!this.context.size) {
|
|
1766
|
-
return;
|
|
1767
|
-
}
|
|
1768
|
-
const dataNodes = this.#dataNodes;
|
|
1769
|
-
if (!dataNodes.length) {
|
|
1770
|
-
return;
|
|
1771
|
-
}
|
|
1772
|
-
const focusId = this.options.focus;
|
|
1773
|
-
const focusNode = focusId ? dataNodes.find((node) => node.id === focusId) : void 0;
|
|
1774
|
-
if (!focusNode) {
|
|
1775
|
-
const radial = layoutRadial(0, 0, Math.min(this.context.size.width, this.context.size.height) / 2 - (this.options.margin ?? 100), dataNodes.length);
|
|
1776
|
-
dataNodes.forEach((node, index) => updateNode(node, radial(index), this.options.leafRadius ?? 5));
|
|
1777
|
-
this.layout.graph.nodes = dataNodes;
|
|
1778
|
-
this.layout.graph.edges = [];
|
|
1779
|
-
this.animate();
|
|
1780
|
-
return;
|
|
1781
|
-
}
|
|
1782
|
-
const relationOf = this.options.relationOf ?? defaultRelationOf;
|
|
1783
|
-
const groups = /* @__PURE__ */ new Map();
|
|
1784
|
-
const placed = /* @__PURE__ */ new Set([
|
|
1785
|
-
focusId
|
|
1786
|
-
]);
|
|
1787
|
-
for (const edge of this.#dataEdges) {
|
|
1788
|
-
const relation = relationOf(edge, focusId);
|
|
1789
|
-
if (!relation) {
|
|
1790
|
-
continue;
|
|
1791
|
-
}
|
|
1792
|
-
const neighbour = edge.source.id === focusId ? edge.target : edge.source;
|
|
1793
|
-
if (placed.has(neighbour.id)) {
|
|
1794
|
-
continue;
|
|
1795
|
-
}
|
|
1796
|
-
placed.add(neighbour.id);
|
|
1797
|
-
let group = groups.get(relation.key);
|
|
1798
|
-
if (!group) {
|
|
1799
|
-
group = {
|
|
1800
|
-
id: `${RELATION_PREFIX}${relation.key}`,
|
|
1801
|
-
label: relation.label,
|
|
1802
|
-
leaves: []
|
|
1803
|
-
};
|
|
1804
|
-
groups.set(relation.key, group);
|
|
1805
|
-
}
|
|
1806
|
-
group.leaves.push(neighbour);
|
|
1807
|
-
}
|
|
1808
|
-
const items = [
|
|
1809
|
-
{
|
|
1810
|
-
id: focusId,
|
|
1811
|
-
node: focusNode
|
|
1812
|
-
}
|
|
1813
|
-
];
|
|
1814
|
-
for (const group of groups.values()) {
|
|
1815
|
-
items.push({
|
|
1816
|
-
id: group.id,
|
|
1817
|
-
parent: focusId,
|
|
1818
|
-
relationLabel: group.label
|
|
1819
|
-
});
|
|
1820
|
-
for (const leaf of group.leaves) {
|
|
1821
|
-
items.push({
|
|
1822
|
-
id: leaf.id,
|
|
1823
|
-
parent: group.id,
|
|
1824
|
-
node: leaf
|
|
1825
|
-
});
|
|
1826
|
-
}
|
|
1827
|
-
}
|
|
1828
|
-
const childrenByParent = /* @__PURE__ */ new Map();
|
|
1829
|
-
for (const item of items) {
|
|
1830
|
-
if (item.parent) {
|
|
1831
|
-
const list = childrenByParent.get(item.parent) ?? [];
|
|
1832
|
-
list.push(item);
|
|
1833
|
-
childrenByParent.set(item.parent, list);
|
|
1834
|
-
}
|
|
1835
|
-
}
|
|
1836
|
-
const root = hierarchy3({
|
|
1837
|
-
id: focusId,
|
|
1838
|
-
node: focusNode
|
|
1839
|
-
}, (d) => childrenByParent.get(d.id) ?? []);
|
|
1840
|
-
const { width, height } = this.context.size;
|
|
1841
|
-
const margin = this.options.margin ?? 100;
|
|
1842
|
-
const ringRadius = Math.max(0, Math.min(width, height) / 2 - margin);
|
|
1843
|
-
d3Cluster3().size([
|
|
1844
|
-
2 * Math.PI,
|
|
1845
|
-
ringRadius
|
|
1846
|
-
]).separation((a, b) => (a.parent === b.parent ? 1 : 2) / Math.max(1, a.depth))(root);
|
|
1847
|
-
const leafR = this.options.leafRadius ?? 5;
|
|
1848
|
-
const relationR = this.options.relationRadius ?? leafR;
|
|
1849
|
-
const focusR = this.options.focusRadius ?? leafR * 2;
|
|
1850
|
-
const prevStubsById = /* @__PURE__ */ new Map();
|
|
1851
|
-
for (const node of this.layout.graph.nodes) {
|
|
1852
|
-
if (node.type === PLEXUS_NODE_TYPE_RELATION) {
|
|
1853
|
-
prevStubsById.set(node.id, node);
|
|
1854
|
-
}
|
|
1855
|
-
}
|
|
1856
|
-
const relationStubs = /* @__PURE__ */ new Map();
|
|
1857
|
-
root.each((d) => {
|
|
1858
|
-
const angle = d.x;
|
|
1859
|
-
const radius = d.y;
|
|
1860
|
-
const x = Math.cos(angle - Math.PI / 2) * radius;
|
|
1861
|
-
const y = Math.sin(angle - Math.PI / 2) * radius;
|
|
1862
|
-
const id = d.data.id;
|
|
1863
|
-
if (id === focusId) {
|
|
1864
|
-
updateNode(focusNode, [
|
|
1865
|
-
x,
|
|
1866
|
-
y
|
|
1867
|
-
], focusR);
|
|
1868
|
-
focusNode.type = PLEXUS_NODE_TYPE_FOCUS;
|
|
1869
|
-
focusNode.hidden = false;
|
|
1870
|
-
} else if (d.data.node) {
|
|
1871
|
-
updateNode(d.data.node, [
|
|
1872
|
-
x,
|
|
1873
|
-
y
|
|
1874
|
-
], leafR);
|
|
1875
|
-
d.data.node.type = PLEXUS_NODE_TYPE_OBJECT;
|
|
1876
|
-
d.data.node.hidden = false;
|
|
1877
|
-
} else {
|
|
1878
|
-
const stub = prevStubsById.get(id) ?? {
|
|
1879
|
-
id,
|
|
1880
|
-
x: 0,
|
|
1881
|
-
y: 0
|
|
1882
|
-
};
|
|
1883
|
-
updateNode(stub, [
|
|
1884
|
-
x,
|
|
1885
|
-
y
|
|
1886
|
-
], relationR);
|
|
1887
|
-
stub.type = PLEXUS_NODE_TYPE_RELATION;
|
|
1888
|
-
stub.label = d.data.relationLabel;
|
|
1889
|
-
relationStubs.set(id, stub);
|
|
1890
|
-
}
|
|
1891
|
-
});
|
|
1892
|
-
const nextNodes = [];
|
|
1893
|
-
for (const stub of relationStubs.values()) {
|
|
1894
|
-
nextNodes.push(stub);
|
|
1895
|
-
}
|
|
1896
|
-
for (const group of groups.values()) {
|
|
1897
|
-
for (const leaf of group.leaves) {
|
|
1898
|
-
nextNodes.push(leaf);
|
|
1899
|
-
}
|
|
1900
|
-
}
|
|
1901
|
-
nextNodes.push(focusNode);
|
|
1902
|
-
this.layout.graph.nodes = nextNodes;
|
|
1903
|
-
const edges = [];
|
|
1904
|
-
root.links().forEach((link) => {
|
|
1905
|
-
const sid = link.source.data.id;
|
|
1906
|
-
const tid = link.target.data.id;
|
|
1907
|
-
const sourceNode = sid === focusId ? focusNode : link.source.data.node ?? relationStubs.get(sid);
|
|
1908
|
-
const targetNode = link.target.data.node ?? relationStubs.get(tid);
|
|
1909
|
-
if (!sourceNode || !targetNode) {
|
|
1910
|
-
return;
|
|
1911
|
-
}
|
|
1912
|
-
edges.push({
|
|
1913
|
-
id: `${HIER_EDGE_PREFIX2}${sid}->${tid}`,
|
|
1914
|
-
type: "hierarchy",
|
|
1915
|
-
source: sourceNode,
|
|
1916
|
-
target: targetNode,
|
|
1917
|
-
data: void 0
|
|
1918
|
-
});
|
|
1919
|
-
});
|
|
1920
|
-
this.layout.graph.edges = edges;
|
|
1921
|
-
this.onTickFrame(0);
|
|
1922
|
-
}
|
|
1923
|
-
};
|
|
1924
|
-
var defaultRelationOf = (edge, focusId) => {
|
|
1925
|
-
const outgoing = edge.source.id === focusId;
|
|
1926
|
-
const incoming = edge.target.id === focusId;
|
|
1927
|
-
if (!outgoing && !incoming) {
|
|
1928
|
-
return void 0;
|
|
1929
|
-
}
|
|
1930
|
-
const type = edge.type ?? "edge";
|
|
1931
|
-
const arrow = outgoing ? "\u2192" : "\u2190";
|
|
1932
|
-
return {
|
|
1933
|
-
key: `${type}:${outgoing ? "out" : "in"}`,
|
|
1934
|
-
label: `${type} ${arrow}`
|
|
1935
|
-
};
|
|
1936
|
-
};
|
|
1937
|
-
|
|
1938
|
-
// src/graph/projector/graph-relational-projector.ts
|
|
1939
|
-
import { log as log8 } from "@dxos/log";
|
|
1940
|
-
var __dxlog_file10 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/projector/graph-relational-projector.ts";
|
|
1941
|
-
var GraphRelationalProjector = class extends GraphRadialProjector {
|
|
1942
|
-
onUpdate(graph) {
|
|
1943
|
-
log8("onUpdate", {
|
|
1944
|
-
graph: {
|
|
1945
|
-
nodes: graph?.nodes.length,
|
|
1946
|
-
edges: graph?.edges.length
|
|
1947
|
-
},
|
|
1948
|
-
selection: this.selection?.getSelectedIds()
|
|
1949
|
-
}, { "~LogMeta": "~LogMeta", F: __dxlog_file10, L: 8, S: this });
|
|
1950
|
-
this.mergeData(graph);
|
|
1951
|
-
this.emitUpdate("topology");
|
|
1952
|
-
const selected = this.selection?.getSelectedIds()[0];
|
|
1953
|
-
if (selected) {
|
|
1954
|
-
const node = this.layout.graph.nodes.find((node2) => node2.id === selected);
|
|
1955
|
-
if (node) {
|
|
1956
|
-
this.doRelationalLayout(node);
|
|
1957
|
-
return;
|
|
1958
|
-
}
|
|
1959
|
-
}
|
|
1960
|
-
this.doRadialLayout();
|
|
1961
|
-
}
|
|
1962
|
-
doRelationalLayout(selected) {
|
|
1963
|
-
if (!this.context.size) {
|
|
1964
|
-
return;
|
|
1965
|
-
}
|
|
1966
|
-
const r2 = this.options.radius ?? Math.min(this.context.size.width, this.context.size.height) / 2 - (this.options.margin ?? 80);
|
|
1967
|
-
const r1 = r2 * 0.5;
|
|
1968
|
-
this.layout.graph.nodes.forEach((node) => node.type = void 0);
|
|
1969
|
-
this.layout.graph.edges.forEach((edge) => {
|
|
1970
|
-
edge.order = 0;
|
|
1971
|
-
edge.type = void 0;
|
|
1972
|
-
});
|
|
1973
|
-
const children = this.layout.graph.edges.filter((edge) => edge.source.id === selected.id).map((edge) => {
|
|
1974
|
-
edge.order = 1;
|
|
1975
|
-
edge.type = "1";
|
|
1976
|
-
edge.target.type = "1";
|
|
1977
|
-
return edge.target;
|
|
1978
|
-
});
|
|
1979
|
-
const parents = this.layout.graph.edges.filter((edge) => edge.target.id === selected.id).map((edge) => {
|
|
1980
|
-
edge.order = 2;
|
|
1981
|
-
edge.type = "2";
|
|
1982
|
-
edge.source.type = "2";
|
|
1983
|
-
return edge.source;
|
|
1984
|
-
});
|
|
1985
|
-
updateNode(selected, [
|
|
1986
|
-
0,
|
|
1987
|
-
0
|
|
1988
|
-
], 30);
|
|
1989
|
-
{
|
|
1990
|
-
const vertical = layoutHorizontal(0, r1, r1 * 2, children.length);
|
|
1991
|
-
children.forEach((node, i) => {
|
|
1992
|
-
const [tx, ty] = vertical(i);
|
|
1993
|
-
updateNode(node, [
|
|
1994
|
-
tx,
|
|
1995
|
-
ty
|
|
1996
|
-
], 20);
|
|
1997
|
-
});
|
|
1998
|
-
}
|
|
1999
|
-
{
|
|
2000
|
-
const vertical = layoutHorizontal(0, -r1, r1, parents.length);
|
|
2001
|
-
parents.forEach((node, i) => {
|
|
2002
|
-
const [tx, ty] = vertical(i);
|
|
2003
|
-
updateNode(node, [
|
|
2004
|
-
tx,
|
|
2005
|
-
ty
|
|
2006
|
-
], 20);
|
|
2007
|
-
});
|
|
2008
|
-
}
|
|
2009
|
-
{
|
|
2010
|
-
const radial = layoutRadial(0, 0, r2, this.layout.graph.nodes.length - 1 - children.length - parents.length);
|
|
2011
|
-
this.layout.graph.nodes.filter((node) => node.id !== selected.id && !children.includes(node) && !parents.includes(node)).forEach((node, i) => {
|
|
2012
|
-
const [tx, ty] = radial(i);
|
|
2013
|
-
updateNode(node, [
|
|
2014
|
-
tx,
|
|
2015
|
-
ty
|
|
2016
|
-
]);
|
|
2017
|
-
});
|
|
2018
|
-
}
|
|
2019
|
-
this.animate();
|
|
2020
|
-
}
|
|
2021
|
-
};
|
|
2022
|
-
|
|
2023
|
-
// src/graph/projector/graph-swarm-projector.ts
|
|
2024
|
-
import { timer as timer2 } from "d3";
|
|
2025
|
-
import { log as log9 } from "@dxos/log";
|
|
2026
|
-
var __dxlog_file11 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/projector/graph-swarm-projector.ts";
|
|
2027
|
-
var SWARMMATE_RADIUS = 60;
|
|
2028
|
-
var SEPARATION_DISTANCE = 30;
|
|
2029
|
-
var DEFAULTS = {
|
|
2030
|
-
spawnRadius: 200,
|
|
2031
|
-
maxVelocity: 1.2,
|
|
2032
|
-
alignment: 3,
|
|
2033
|
-
cohesion: 3,
|
|
2034
|
-
separation: 3,
|
|
2035
|
-
cursorRepel: 120,
|
|
2036
|
-
avoidance: 3,
|
|
2037
|
-
trailLength: 30
|
|
2038
|
-
};
|
|
2039
|
-
var GraphSwarmProjector = class extends GraphProjector {
|
|
2040
|
-
#timer;
|
|
2041
|
-
/** Cursor in SVG model coordinates, or null when the pointer is outside the surface. */
|
|
2042
|
-
#cursor = null;
|
|
2043
|
-
findNode() {
|
|
2044
|
-
return void 0;
|
|
2045
|
-
}
|
|
2046
|
-
/**
|
|
2047
|
-
* Tell the projector where the pointer is, in SVG model coordinates (the same space
|
|
2048
|
-
* `node.x/y` live in — center origin, post-zoom). Pass `null` (or omit args) on
|
|
2049
|
-
* pointer-leave to disable cursor avoidance.
|
|
2050
|
-
*/
|
|
2051
|
-
setCursor(x, y) {
|
|
2052
|
-
if (x == null || y == null) {
|
|
2053
|
-
this.#cursor = null;
|
|
2054
|
-
} else {
|
|
2055
|
-
this.#cursor = {
|
|
2056
|
-
x,
|
|
2057
|
-
y
|
|
2058
|
-
};
|
|
2059
|
-
}
|
|
2060
|
-
}
|
|
2061
|
-
onUpdate(graph) {
|
|
2062
|
-
log9("onUpdate", {
|
|
2063
|
-
graph: {
|
|
2064
|
-
nodes: graph?.nodes.length,
|
|
2065
|
-
edges: graph?.edges.length
|
|
2066
|
-
}
|
|
2067
|
-
}, { "~LogMeta": "~LogMeta", F: __dxlog_file11, L: 54, S: this });
|
|
2068
|
-
this.mergeData(graph);
|
|
2069
|
-
this.initializeNodes();
|
|
2070
|
-
this.emitUpdate("topology");
|
|
2071
|
-
}
|
|
2072
|
-
async onStart() {
|
|
2073
|
-
this.startTimer();
|
|
2074
|
-
}
|
|
2075
|
-
async onStop() {
|
|
2076
|
-
this.stopTimer();
|
|
2077
|
-
}
|
|
2078
|
-
onRefresh() {
|
|
2079
|
-
if (!this.#timer) {
|
|
2080
|
-
this.startTimer();
|
|
2081
|
-
}
|
|
2082
|
-
}
|
|
2083
|
-
#opt(key) {
|
|
2084
|
-
return this.options[key] ?? DEFAULTS[key];
|
|
2085
|
-
}
|
|
2086
|
-
initializeNodes() {
|
|
2087
|
-
const spawnRadius = this.#opt("spawnRadius");
|
|
2088
|
-
const maxVelocity = this.#opt("maxVelocity");
|
|
2089
|
-
for (const node of this.layout.graph.nodes) {
|
|
2090
|
-
if (!node.initialized) {
|
|
2091
|
-
const theta = Math.random() * 2 * Math.PI;
|
|
2092
|
-
const r = Math.random() * spawnRadius;
|
|
2093
|
-
node.x = Math.cos(theta) * r;
|
|
2094
|
-
node.y = Math.sin(theta) * r;
|
|
2095
|
-
const phi = Math.random() * 2 * Math.PI;
|
|
2096
|
-
const speed = (0.3 + Math.random() * 0.7) * maxVelocity;
|
|
2097
|
-
node.vx = Math.cos(phi) * speed;
|
|
2098
|
-
node.vy = Math.sin(phi) * speed;
|
|
2099
|
-
node.ax = 0;
|
|
2100
|
-
node.ay = 0;
|
|
2101
|
-
node.initialized = true;
|
|
2102
|
-
} else {
|
|
2103
|
-
node.vx ??= 0;
|
|
2104
|
-
node.vy ??= 0;
|
|
2105
|
-
node.ax ??= 0;
|
|
2106
|
-
node.ay ??= 0;
|
|
2107
|
-
}
|
|
2108
|
-
node.r ??= 6;
|
|
2109
|
-
node.history = [
|
|
2110
|
-
{
|
|
2111
|
-
x: node.x ?? 0,
|
|
2112
|
-
y: node.y ?? 0
|
|
2113
|
-
}
|
|
2114
|
-
];
|
|
2115
|
-
}
|
|
2116
|
-
}
|
|
2117
|
-
startTimer() {
|
|
2118
|
-
this.stopTimer();
|
|
2119
|
-
this.#timer = timer2(() => this.tick());
|
|
2120
|
-
}
|
|
2121
|
-
stopTimer() {
|
|
2122
|
-
this.#timer?.stop();
|
|
2123
|
-
this.#timer = void 0;
|
|
2124
|
-
}
|
|
2125
|
-
/**
|
|
2126
|
-
* One simulation step. Modeled on `tick` in Swarm.tsx, but operating in the
|
|
2127
|
-
* SVG centered-origin coordinate space and writing to `node.x/y` directly so
|
|
2128
|
-
* the existing `GraphRenderer.applyPositions` reads them unchanged.
|
|
2129
|
-
*/
|
|
2130
|
-
tick() {
|
|
2131
|
-
const size = this.context.size;
|
|
2132
|
-
if (!size) {
|
|
2133
|
-
return;
|
|
2134
|
-
}
|
|
2135
|
-
const { width, height } = size;
|
|
2136
|
-
const halfW = width / 2;
|
|
2137
|
-
const halfH = height / 2;
|
|
2138
|
-
const nodes = this.layout.graph.nodes;
|
|
2139
|
-
const maxVelocity = this.#opt("maxVelocity");
|
|
2140
|
-
const alignmentW = this.#opt("alignment") / 100;
|
|
2141
|
-
const cohesionW = this.#opt("cohesion") / 100;
|
|
2142
|
-
const separationW = this.#opt("separation") / 100;
|
|
2143
|
-
const avoidanceW = this.#opt("avoidance") / 10;
|
|
2144
|
-
const cursorRepel = this.#opt("cursorRepel");
|
|
2145
|
-
const cursor = cursorRepel > 0 ? this.#cursor : null;
|
|
2146
|
-
for (const b1 of nodes) {
|
|
2147
|
-
let ax = 0;
|
|
2148
|
-
let ay = 0;
|
|
2149
|
-
let alignX = 0;
|
|
2150
|
-
let alignY = 0;
|
|
2151
|
-
let alignActive = false;
|
|
2152
|
-
let cohX = 0;
|
|
2153
|
-
let cohY = 0;
|
|
2154
|
-
let cohActive = false;
|
|
2155
|
-
let sepX = 0;
|
|
2156
|
-
let sepY = 0;
|
|
2157
|
-
let sepActive = false;
|
|
2158
|
-
let avoidX = 0;
|
|
2159
|
-
let avoidY = 0;
|
|
2160
|
-
let avoidActive = false;
|
|
2161
|
-
if (cursor) {
|
|
2162
|
-
const dx = cursor.x - (b1.x ?? 0);
|
|
2163
|
-
const dy = cursor.y - (b1.y ?? 0);
|
|
2164
|
-
const distSq = dx * dx + dy * dy;
|
|
2165
|
-
if (distSq > 0 && distSq < cursorRepel * cursorRepel) {
|
|
2166
|
-
const dist = Math.sqrt(distSq);
|
|
2167
|
-
avoidX = -dx / dist * (1 / dist);
|
|
2168
|
-
avoidY = -dy / dist * (1 / dist);
|
|
2169
|
-
avoidActive = true;
|
|
2170
|
-
}
|
|
2171
|
-
}
|
|
2172
|
-
if (!avoidActive) {
|
|
2173
|
-
for (const b2 of nodes) {
|
|
2174
|
-
if (b1 === b2) {
|
|
2175
|
-
continue;
|
|
2176
|
-
}
|
|
2177
|
-
const dx = (b2.x ?? 0) - (b1.x ?? 0);
|
|
2178
|
-
const dy = (b2.y ?? 0) - (b1.y ?? 0);
|
|
2179
|
-
const distSq = dx * dx + dy * dy;
|
|
2180
|
-
if (distSq === 0) {
|
|
2181
|
-
continue;
|
|
2182
|
-
}
|
|
2183
|
-
const dist = Math.sqrt(distSq);
|
|
2184
|
-
if (dist < SEPARATION_DISTANCE) {
|
|
2185
|
-
sepX += -dx / dist * (1 / dist);
|
|
2186
|
-
sepY += -dy / dist * (1 / dist);
|
|
2187
|
-
sepActive = true;
|
|
2188
|
-
}
|
|
2189
|
-
if (dist < SWARMMATE_RADIUS) {
|
|
2190
|
-
cohX += dx;
|
|
2191
|
-
cohY += dy;
|
|
2192
|
-
cohActive = true;
|
|
2193
|
-
alignX += b2.vx ?? 0;
|
|
2194
|
-
alignY += b2.vy ?? 0;
|
|
2195
|
-
alignActive = true;
|
|
2196
|
-
}
|
|
2197
|
-
}
|
|
2198
|
-
}
|
|
2199
|
-
const applyForce = (fx, fy, weight, active) => {
|
|
2200
|
-
if (!active || weight === 0) {
|
|
2201
|
-
return;
|
|
2202
|
-
}
|
|
2203
|
-
const len = Math.sqrt(fx * fx + fy * fy);
|
|
2204
|
-
if (len === 0) {
|
|
2205
|
-
return;
|
|
2206
|
-
}
|
|
2207
|
-
const sx = fx * maxVelocity / len - (b1.vx ?? 0);
|
|
2208
|
-
const sy = fy * maxVelocity / len - (b1.vy ?? 0);
|
|
2209
|
-
const slen = Math.sqrt(sx * sx + sy * sy);
|
|
2210
|
-
if (slen > weight) {
|
|
2211
|
-
ax += sx * weight / slen;
|
|
2212
|
-
ay += sy * weight / slen;
|
|
2213
|
-
} else {
|
|
2214
|
-
ax += sx;
|
|
2215
|
-
ay += sy;
|
|
2216
|
-
}
|
|
2217
|
-
};
|
|
2218
|
-
applyForce(alignX, alignY, alignmentW, alignActive);
|
|
2219
|
-
applyForce(cohX, cohY, cohesionW, cohActive);
|
|
2220
|
-
applyForce(sepX, sepY, separationW, sepActive);
|
|
2221
|
-
applyForce(avoidX, avoidY, avoidanceW, avoidActive);
|
|
2222
|
-
b1.ax = ax;
|
|
2223
|
-
b1.ay = ay;
|
|
2224
|
-
}
|
|
2225
|
-
const trailLength = Math.max(0, Math.round(this.#opt("trailLength")));
|
|
2226
|
-
for (const b of nodes) {
|
|
2227
|
-
let vx = (b.vx ?? 0) + (b.ax ?? 0);
|
|
2228
|
-
let vy = (b.vy ?? 0) + (b.ay ?? 0);
|
|
2229
|
-
const vlen = Math.sqrt(vx * vx + vy * vy);
|
|
2230
|
-
if (vlen > maxVelocity) {
|
|
2231
|
-
vx = vx * maxVelocity / vlen;
|
|
2232
|
-
vy = vy * maxVelocity / vlen;
|
|
2233
|
-
}
|
|
2234
|
-
b.vx = vx;
|
|
2235
|
-
b.vy = vy;
|
|
2236
|
-
if (trailLength > 0) {
|
|
2237
|
-
const history = b.history ?? (b.history = []);
|
|
2238
|
-
history.push({
|
|
2239
|
-
x: b.x ?? 0,
|
|
2240
|
-
y: b.y ?? 0
|
|
2241
|
-
});
|
|
2242
|
-
if (history.length > trailLength) {
|
|
2243
|
-
history.splice(0, history.length - trailLength);
|
|
2244
|
-
}
|
|
2245
|
-
}
|
|
2246
|
-
let x = (b.x ?? 0) + vx;
|
|
2247
|
-
let y = (b.y ?? 0) + vy;
|
|
2248
|
-
let wrapped = false;
|
|
2249
|
-
if (x > halfW) {
|
|
2250
|
-
x -= width;
|
|
2251
|
-
wrapped = true;
|
|
2252
|
-
} else if (x < -halfW) {
|
|
2253
|
-
x += width;
|
|
2254
|
-
wrapped = true;
|
|
2255
|
-
}
|
|
2256
|
-
if (y > halfH) {
|
|
2257
|
-
y -= height;
|
|
2258
|
-
wrapped = true;
|
|
2259
|
-
} else if (y < -halfH) {
|
|
2260
|
-
y += height;
|
|
2261
|
-
wrapped = true;
|
|
2262
|
-
}
|
|
2263
|
-
if (wrapped && b.history) {
|
|
2264
|
-
b.history.length = 0;
|
|
2265
|
-
}
|
|
2266
|
-
b.x = x;
|
|
2267
|
-
b.y = y;
|
|
2268
|
-
}
|
|
2269
|
-
for (const edge of this.layout.graph.edges) {
|
|
2270
|
-
const sx = edge.source.x ?? 0;
|
|
2271
|
-
const sy = edge.source.y ?? 0;
|
|
2272
|
-
const tx = edge.target.x ?? 0;
|
|
2273
|
-
const ty = edge.target.y ?? 0;
|
|
2274
|
-
edge.path = `M ${sx} ${sy} Q 0 0 ${tx} ${ty}`;
|
|
2275
|
-
}
|
|
2276
|
-
this.emitUpdate("positions");
|
|
2277
|
-
}
|
|
2278
|
-
};
|
|
2279
|
-
|
|
2280
|
-
// src/graph/projector/graph-tree-projector.ts
|
|
2281
|
-
import { tree as d3Tree, hierarchy as hierarchy4, linkHorizontal } from "d3";
|
|
2282
|
-
import { log as log10 } from "@dxos/log";
|
|
2283
|
-
var __dxlog_file12 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/projector/graph-tree-projector.ts";
|
|
2284
|
-
var TREE_NODE_TYPE_ROOT = "tree-root";
|
|
2285
|
-
var TREE_NODE_TYPE_NODE = "tree-node";
|
|
2286
|
-
var TREE_NODE_TYPE_INBOUND = "tree-inbound";
|
|
2287
|
-
var HIER_EDGE_PREFIX3 = "__tree_hier__:";
|
|
2288
|
-
var linkPath3 = linkHorizontal().x((d) => d.x).y((d) => d.y);
|
|
2289
|
-
var GraphTreeProjector = class extends GraphRadialProjector {
|
|
2290
|
-
// Snapshots taken right after mergeData — doTreeLayout reads from these because it overwrites
|
|
2291
|
-
// layout.graph.edges with the synthesized hierarchy edges.
|
|
2292
|
-
#dataNodes = [];
|
|
2293
|
-
#dataEdges = [];
|
|
2294
|
-
#resizeCleanup;
|
|
2295
|
-
async onStart() {
|
|
2296
|
-
this.#resizeCleanup = this.context.resized.on(() => {
|
|
2297
|
-
if (this.#dataNodes.length) {
|
|
2298
|
-
this.doTreeLayout();
|
|
2299
|
-
this.emitUpdate("topology");
|
|
2300
|
-
this.animate();
|
|
2301
|
-
}
|
|
2302
|
-
});
|
|
2303
|
-
}
|
|
2304
|
-
async onStop() {
|
|
2305
|
-
this.#resizeCleanup?.();
|
|
2306
|
-
this.#resizeCleanup = void 0;
|
|
2307
|
-
await super.onStop();
|
|
2308
|
-
}
|
|
2309
|
-
onUpdate(graph) {
|
|
2310
|
-
log10("onUpdate", {
|
|
2311
|
-
graph: {
|
|
2312
|
-
nodes: graph?.nodes.length,
|
|
2313
|
-
edges: graph?.edges.length
|
|
2314
|
-
}
|
|
2315
|
-
}, { "~LogMeta": "~LogMeta", F: __dxlog_file12, L: 46, S: this });
|
|
2316
|
-
this.mergeData(graph);
|
|
2317
|
-
this.#dataNodes = [
|
|
2318
|
-
...this.layout.graph.nodes
|
|
2319
|
-
];
|
|
2320
|
-
this.#dataEdges = [
|
|
2321
|
-
...this.layout.graph.edges
|
|
2322
|
-
];
|
|
2323
|
-
this.doTreeLayout();
|
|
2324
|
-
this.emitUpdate("topology");
|
|
2325
|
-
this.animate();
|
|
2326
|
-
}
|
|
2327
|
-
onTickFrame(_t) {
|
|
2328
|
-
for (const edge of this.layout.graph.edges) {
|
|
2329
|
-
if (edge.type !== "hierarchy") {
|
|
2330
|
-
continue;
|
|
2331
|
-
}
|
|
2332
|
-
const { source, target } = edge;
|
|
2333
|
-
edge.path = linkPath3({
|
|
2334
|
-
source: {
|
|
2335
|
-
x: source.x ?? 0,
|
|
2336
|
-
y: source.y ?? 0
|
|
2337
|
-
},
|
|
2338
|
-
target: {
|
|
2339
|
-
x: target.x ?? 0,
|
|
2340
|
-
y: target.y ?? 0
|
|
2341
|
-
}
|
|
2342
|
-
}) ?? void 0;
|
|
2343
|
-
}
|
|
2344
|
-
}
|
|
2345
|
-
doTreeLayout() {
|
|
2346
|
-
if (!this.context.size) {
|
|
2347
|
-
return;
|
|
2348
|
-
}
|
|
2349
|
-
const dataNodes = this.#dataNodes;
|
|
2350
|
-
if (!dataNodes.length) {
|
|
2351
|
-
return;
|
|
2352
|
-
}
|
|
2353
|
-
const focusId = this.options.focus;
|
|
2354
|
-
const focusNode = focusId ? dataNodes.find((node) => node.id === focusId) : void 0;
|
|
2355
|
-
if (!focusNode) {
|
|
2356
|
-
this.doRadialLayout();
|
|
2357
|
-
return;
|
|
2358
|
-
}
|
|
2359
|
-
const nodesById = new Map(dataNodes.map((node) => [
|
|
2360
|
-
node.id,
|
|
2361
|
-
node
|
|
2362
|
-
]));
|
|
2363
|
-
const makeDatum = (id) => ({
|
|
2364
|
-
id,
|
|
2365
|
-
node: nodesById.get(id)
|
|
2366
|
-
});
|
|
2367
|
-
const outgoing = /* @__PURE__ */ new Map();
|
|
2368
|
-
for (const edge of this.#dataEdges) {
|
|
2369
|
-
const list = outgoing.get(edge.source.id) ?? [];
|
|
2370
|
-
list.push(edge.target.id);
|
|
2371
|
-
outgoing.set(edge.source.id, list);
|
|
2372
|
-
}
|
|
2373
|
-
const rightChildren = /* @__PURE__ */ new Map();
|
|
2374
|
-
const visited = /* @__PURE__ */ new Set([
|
|
2375
|
-
focusId
|
|
2376
|
-
]);
|
|
2377
|
-
let frontier = [
|
|
2378
|
-
focusId
|
|
2379
|
-
];
|
|
2380
|
-
while (frontier.length) {
|
|
2381
|
-
const next = [];
|
|
2382
|
-
for (const id of frontier) {
|
|
2383
|
-
for (const target of outgoing.get(id) ?? []) {
|
|
2384
|
-
if (!visited.has(target)) {
|
|
2385
|
-
visited.add(target);
|
|
2386
|
-
const list = rightChildren.get(id) ?? [];
|
|
2387
|
-
list.push(target);
|
|
2388
|
-
rightChildren.set(id, list);
|
|
2389
|
-
next.push(target);
|
|
2390
|
-
}
|
|
2391
|
-
}
|
|
2392
|
-
}
|
|
2393
|
-
frontier = next;
|
|
2394
|
-
}
|
|
2395
|
-
const leftSources = [];
|
|
2396
|
-
for (const edge of this.#dataEdges) {
|
|
2397
|
-
const source = edge.source.id;
|
|
2398
|
-
if (edge.target.id === focusId && source !== focusId && !visited.has(source)) {
|
|
2399
|
-
visited.add(source);
|
|
2400
|
-
leftSources.push(source);
|
|
2401
|
-
}
|
|
2402
|
-
}
|
|
2403
|
-
const rightRoot = hierarchy4(makeDatum(focusId), (d) => (rightChildren.get(d.id) ?? []).map(makeDatum));
|
|
2404
|
-
const leftRoot = hierarchy4(makeDatum(focusId), (d) => d.id === focusId ? leftSources.map(makeDatum) : []);
|
|
2405
|
-
const { width } = this.context.size;
|
|
2406
|
-
const margin = this.options.margin ?? 80;
|
|
2407
|
-
const rowGap = this.options.rowGap ?? 28;
|
|
2408
|
-
const maxColumnGap = this.options.maxColumnGap ?? 300;
|
|
2409
|
-
const sideDepth = Math.max(1, rightRoot.height, leftRoot.height);
|
|
2410
|
-
const columnGap = Math.min(maxColumnGap, Math.max(0, (width / 2 - margin) / sideDepth));
|
|
2411
|
-
const layoutTree = d3Tree().nodeSize([
|
|
2412
|
-
rowGap,
|
|
2413
|
-
columnGap
|
|
2414
|
-
]);
|
|
2415
|
-
const rightPoint = layoutTree(rightRoot);
|
|
2416
|
-
const leftPoint = layoutTree(leftRoot);
|
|
2417
|
-
const nodeR = this.options.nodeRadius ?? 6;
|
|
2418
|
-
const rootR = this.options.rootRadius ?? nodeR * 2;
|
|
2419
|
-
const placed = [];
|
|
2420
|
-
updateNode(focusNode, [
|
|
2421
|
-
0,
|
|
2422
|
-
0
|
|
2423
|
-
], rootR);
|
|
2424
|
-
focusNode.type = TREE_NODE_TYPE_ROOT;
|
|
2425
|
-
placed.push(focusNode);
|
|
2426
|
-
const rightBreadth = rightPoint.x;
|
|
2427
|
-
rightPoint.each((d) => {
|
|
2428
|
-
if (d.data.id === focusId || !d.data.node) {
|
|
2429
|
-
return;
|
|
2430
|
-
}
|
|
2431
|
-
updateNode(d.data.node, [
|
|
2432
|
-
d.y,
|
|
2433
|
-
d.x - rightBreadth
|
|
2434
|
-
], nodeR);
|
|
2435
|
-
d.data.node.type = TREE_NODE_TYPE_NODE;
|
|
2436
|
-
placed.push(d.data.node);
|
|
2437
|
-
});
|
|
2438
|
-
const leftBreadth = leftPoint.x;
|
|
2439
|
-
leftPoint.each((d) => {
|
|
2440
|
-
if (d.data.id === focusId || !d.data.node) {
|
|
2441
|
-
return;
|
|
2442
|
-
}
|
|
2443
|
-
updateNode(d.data.node, [
|
|
2444
|
-
-d.y,
|
|
2445
|
-
d.x - leftBreadth
|
|
2446
|
-
], nodeR);
|
|
2447
|
-
d.data.node.type = TREE_NODE_TYPE_INBOUND;
|
|
2448
|
-
placed.push(d.data.node);
|
|
2449
|
-
});
|
|
2450
|
-
this.layout.graph.nodes = placed;
|
|
2451
|
-
const edges = [];
|
|
2452
|
-
const addLinks = (root, reverse = false) => {
|
|
2453
|
-
root.links().forEach((link) => {
|
|
2454
|
-
const parent = link.source.data;
|
|
2455
|
-
const child = link.target.data;
|
|
2456
|
-
const [from, to] = reverse ? [
|
|
2457
|
-
child,
|
|
2458
|
-
parent
|
|
2459
|
-
] : [
|
|
2460
|
-
parent,
|
|
2461
|
-
child
|
|
2462
|
-
];
|
|
2463
|
-
if (!from.node || !to.node) {
|
|
2464
|
-
return;
|
|
2465
|
-
}
|
|
2466
|
-
edges.push({
|
|
2467
|
-
id: `${HIER_EDGE_PREFIX3}${from.id}->${to.id}`,
|
|
2468
|
-
type: "hierarchy",
|
|
2469
|
-
source: from.node,
|
|
2470
|
-
target: to.node,
|
|
2471
|
-
data: void 0
|
|
2472
|
-
});
|
|
2473
|
-
});
|
|
2474
|
-
};
|
|
2475
|
-
addLinks(rightPoint);
|
|
2476
|
-
addLinks(leftPoint, true);
|
|
2477
|
-
this.layout.graph.edges = edges;
|
|
2478
|
-
this.onTickFrame(0);
|
|
2479
|
-
}
|
|
2480
|
-
};
|
|
2481
|
-
|
|
2482
|
-
// src/graph/renderer/graph-renderer.ts
|
|
2483
|
-
import { easeCubicOut, line as line2, polygonHull, select as select4 } from "d3";
|
|
2484
|
-
import * as Clipper from "js-clipper";
|
|
2485
|
-
import { log as log11 } from "@dxos/log";
|
|
2486
|
-
|
|
2487
|
-
// src/graph/renderer/bullets.ts
|
|
2488
|
-
import { easeLinear, interpolate, select as select3 } from "d3";
|
|
2489
|
-
var createBullets = (group, nodeId, options = {}) => {
|
|
2490
|
-
return (selection) => {
|
|
2491
|
-
const { max = 32, radius = 3, delay = 50, minDuration = 100, maxDuration = 500 } = options;
|
|
2492
|
-
selection.each(function(d, i, edges) {
|
|
2493
|
-
const { source, target } = d;
|
|
2494
|
-
if (source.id === nodeId) {
|
|
2495
|
-
const path = select3(edges[i]);
|
|
2496
|
-
const p = path.node().getPointAtLength(0);
|
|
2497
|
-
const bullet = select3(group).append("circle").attr("class", "dx-bullet").attr("cx", p.x).attr("cy", p.y).attr("r", radius);
|
|
2498
|
-
bullet.transition().delay(delay).duration(minDuration + Math.random() * maxDuration).ease(easeLinear).tween("pathTween", function() {
|
|
2499
|
-
const length = path.node().getTotalLength();
|
|
2500
|
-
const r = interpolate(0, length);
|
|
2501
|
-
return (t) => {
|
|
2502
|
-
const point = path.node().getPointAtLength(r(t));
|
|
2503
|
-
select3(this).attr("cx", point.x).attr("cy", point.y);
|
|
2504
|
-
};
|
|
2505
|
-
}).on("end", function() {
|
|
2506
|
-
select3(this).remove();
|
|
2507
|
-
const num = select3(group).selectAll("circle.dx-bullet").size();
|
|
2508
|
-
if (num < max) {
|
|
2509
|
-
selection.call(createBullets(group, target.id));
|
|
2510
|
-
}
|
|
2511
|
-
});
|
|
2512
|
-
}
|
|
2513
|
-
});
|
|
2514
|
-
};
|
|
2515
|
-
};
|
|
2516
|
-
|
|
2517
|
-
// src/graph/renderer/renderer.ts
|
|
2518
|
-
var Renderer = class {
|
|
2519
|
-
_context;
|
|
2520
|
-
_root;
|
|
2521
|
-
_options;
|
|
2522
|
-
constructor(_context, _root, options) {
|
|
2523
|
-
this._context = _context;
|
|
2524
|
-
this._root = _root;
|
|
2525
|
-
this._options = Object.assign({
|
|
2526
|
-
idAccessor: defaultIdAccessor
|
|
2527
|
-
}, options);
|
|
2528
|
-
}
|
|
2529
|
-
get context() {
|
|
2530
|
-
return this._context;
|
|
2531
|
-
}
|
|
2532
|
-
/**
|
|
2533
|
-
* Returns the SVG group root if mounted.
|
|
2534
|
-
* The ref may be null between mount/unmount cycles or before the SVG container has sized.
|
|
2535
|
-
* Callers should treat null as "skip this render" rather than as an error.
|
|
2536
|
-
*/
|
|
2537
|
-
get root() {
|
|
2538
|
-
return this._root.current;
|
|
2539
|
-
}
|
|
2540
|
-
get options() {
|
|
2541
|
-
return this._options;
|
|
2542
|
-
}
|
|
2543
|
-
/**
|
|
2544
|
-
* Fast path invoked when only positions changed.
|
|
2545
|
-
* Default falls back to a full `render`; subclasses can override to skip
|
|
2546
|
-
* enter/exit joins and attribute callbacks for per-tick updates.
|
|
2547
|
-
*/
|
|
2548
|
-
applyPositions(layout) {
|
|
2549
|
-
this.render(layout);
|
|
2550
|
-
}
|
|
2551
|
-
};
|
|
2552
|
-
|
|
2553
|
-
// src/graph/renderer/graph-renderer.ts
|
|
2554
|
-
var __dxlog_file13 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/renderer/graph-renderer.ts";
|
|
2555
|
-
var createLine2 = line2();
|
|
2556
|
-
var EDGE_FADE_MS = 300;
|
|
2557
|
-
var NODE_HIDE_MS = 500;
|
|
2558
|
-
var GraphRenderer = class extends Renderer {
|
|
2559
|
-
// First render of this instance — used to clear DOM left behind by a previous
|
|
2560
|
-
// renderer pointing at the same `<g>` (e.g. when the consumer swaps projectors
|
|
2561
|
-
// and a new renderer is constructed for the same SVG root). Without this, the
|
|
2562
|
-
// previous variant's node shapes (e.g. circles) would persist as d3.join sees
|
|
2563
|
-
// them in the `update` set instead of `enter`.
|
|
2564
|
-
#firstRender = true;
|
|
2565
|
-
render(layout) {
|
|
2566
|
-
if (!this.root) {
|
|
2567
|
-
return;
|
|
2568
|
-
}
|
|
2569
|
-
if (this.#firstRender) {
|
|
2570
|
-
this.#firstRender = false;
|
|
2571
|
-
this.root.replaceChildren();
|
|
2572
|
-
}
|
|
2573
|
-
log11("render", layout, { "~LogMeta": "~LogMeta", F: __dxlog_file13, L: 35, S: this });
|
|
2574
|
-
const root = select4(this.root);
|
|
2575
|
-
if (layout.guides) {
|
|
2576
|
-
root.selectAll("g.dx-guides").data([
|
|
2577
|
-
{
|
|
2578
|
-
id: "guides"
|
|
2579
|
-
}
|
|
2580
|
-
]).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);
|
|
2581
|
-
}
|
|
2582
|
-
const scale = 100;
|
|
2583
|
-
const offsetDistance = 24 * scale;
|
|
2584
|
-
if (this._options.subgraphs) {
|
|
2585
|
-
const components = this._options.subgraphs ? findConnectedComponents({
|
|
2586
|
-
nodes: layout.graph?.nodes ?? [],
|
|
2587
|
-
edges: (layout.graph?.edges ?? []).map(({ source, target }) => ({
|
|
2588
|
-
source: source.id,
|
|
2589
|
-
target: target.id
|
|
2590
|
-
}))
|
|
2591
|
-
}).filter((component) => component.length > 2).map((component, i) => ({
|
|
2592
|
-
id: `subgraph-${i}`,
|
|
2593
|
-
component
|
|
2594
|
-
})) : [];
|
|
2595
|
-
root.selectAll("g.dx-subgraphs").data([
|
|
2596
|
-
{
|
|
2597
|
-
id: "subgraphs"
|
|
2598
|
-
}
|
|
2599
|
-
]).join("g").classed("dx-subgraphs", true).selectAll("path").data(components).join((enter) => enter.append("path").classed("dx-subgraph", true), (update) => {
|
|
2600
|
-
return update.attr("d", ({ component }) => {
|
|
2601
|
-
const points = layout.graph?.nodes.filter((node) => component.includes(node.id)).map((node) => [
|
|
2602
|
-
node.x,
|
|
2603
|
-
node.y
|
|
2604
|
-
]) ?? [];
|
|
2605
|
-
const hullPoints = polygonHull(points);
|
|
2606
|
-
const co = new Clipper.ClipperOffset();
|
|
2607
|
-
const solution = [];
|
|
2608
|
-
const clipperPath = hullPoints.map(([x, y]) => ({
|
|
2609
|
-
X: x * scale,
|
|
2610
|
-
Y: y * scale
|
|
2611
|
-
}));
|
|
2612
|
-
co.AddPath(clipperPath, Clipper.JoinType.jtRound, Clipper.EndType.etClosedPolygon);
|
|
2613
|
-
co.Execute(solution, offsetDistance);
|
|
2614
|
-
if (solution.length > 0) {
|
|
2615
|
-
const offset = solution[0].map(({ X, Y }) => [
|
|
2616
|
-
X / scale,
|
|
2617
|
-
Y / scale
|
|
2618
|
-
]);
|
|
2619
|
-
return createLine2([
|
|
2620
|
-
...offset,
|
|
2621
|
-
offset[0]
|
|
2622
|
-
]);
|
|
2623
|
-
}
|
|
2624
|
-
});
|
|
2625
|
-
});
|
|
2626
|
-
}
|
|
2627
|
-
const edgeGroup = root.selectAll("g.dx-edges").data([
|
|
2628
|
-
{
|
|
2629
|
-
id: "edges"
|
|
2630
|
-
}
|
|
2631
|
-
], (d) => d.id).join("g").classed("dx-edges", true).style("opacity", this.options.edgeOpacity != null ? String(this.options.edgeOpacity) : null);
|
|
2632
|
-
const nodeGroup = root.selectAll("g.dx-nodes").data([
|
|
2633
|
-
{
|
|
2634
|
-
id: "nodes"
|
|
2635
|
-
}
|
|
2636
|
-
], (d) => d.id).join("g").classed("dx-nodes", true);
|
|
2637
|
-
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", (d) => d.hidden ? 0 : 1).classed("dx-node", true).call(createNode, this.options), (update) => update, (exit) => {
|
|
2638
|
-
return exit.transition().ease(easeCubicOut).duration(300).attr("opacity", 0).on("end", function(d) {
|
|
2639
|
-
select4(this).remove();
|
|
2640
|
-
});
|
|
2641
|
-
}).call(updateNode2, this.options).sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
|
|
2642
|
-
edgeGroup.selectAll("g.dx-edge").data(layout.graph?.edges ?? [], (d) => d.id).join(
|
|
2643
|
-
(enter) => enter.append("g").attr("data-id", (d) => d.id).classed("dx-edge", true).call(createEdge, this.options),
|
|
2644
|
-
(update) => update,
|
|
2645
|
-
// Exit fade: edges removed by a topology change (e.g. variant switch from cluster
|
|
2646
|
-
// back to force replaces hierarchy edges with data edges) ease out rather than pop.
|
|
2647
|
-
(exit) => exit.transition().ease(easeCubicOut).duration(EDGE_FADE_MS).attr("opacity", 0).on("end", function() {
|
|
2648
|
-
select4(this).remove();
|
|
2649
|
-
})
|
|
2650
|
-
).call(updateEdge, this.options, nodeGroup).sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
|
|
2651
|
-
}
|
|
2652
|
-
/**
|
|
2653
|
-
* Fast positions-only update. Skips enter/exit, attribute callbacks, label sizing,
|
|
2654
|
-
* and the subgraph hull pass; just writes `transform` per node and `d` per edge.
|
|
2655
|
-
* Called per simulation tick / animation frame.
|
|
2656
|
-
*/
|
|
2657
|
-
applyPositions(layout) {
|
|
2658
|
-
if (!this.root) {
|
|
2659
|
-
return;
|
|
2660
|
-
}
|
|
2661
|
-
const root = select4(this.root);
|
|
2662
|
-
const applyNode = this.options.applyNode;
|
|
2663
|
-
root.select("g.dx-nodes").selectAll("g.dx-node").attr("transform", (d) => d.x != null && d.y != null ? `translate(${d.x},${d.y})` : null).each(applyNode ? function(d) {
|
|
2664
|
-
applyNode(this, d);
|
|
2665
|
-
} : () => {
|
|
2666
|
-
});
|
|
2667
|
-
root.select("g.dx-edges").selectAll("g.dx-edge").selectAll("path").attr("d", function() {
|
|
2668
|
-
const edge = select4(this.parentElement).datum();
|
|
2669
|
-
if (edge.path) {
|
|
2670
|
-
return edge.path;
|
|
2671
|
-
}
|
|
2672
|
-
const { source, target } = edge;
|
|
2673
|
-
if (!source.initialized || !target.initialized) {
|
|
2674
|
-
return null;
|
|
2675
|
-
}
|
|
2676
|
-
return createLine2(getCircumferencePoints([
|
|
2677
|
-
source.x,
|
|
2678
|
-
source.y
|
|
2679
|
-
], [
|
|
2680
|
-
target.x,
|
|
2681
|
-
target.y
|
|
2682
|
-
], source.r, target.r));
|
|
2683
|
-
});
|
|
2684
|
-
}
|
|
2685
|
-
/**
|
|
2686
|
-
* Trigger path bullets.
|
|
2687
|
-
* @param node
|
|
2688
|
-
*/
|
|
2689
|
-
fireBullet(node) {
|
|
2690
|
-
if (!this.root) {
|
|
2691
|
-
return;
|
|
2692
|
-
}
|
|
2693
|
-
select4(this.root).selectAll("g.dx-edges").selectAll("path").call(createBullets(this.root, node.id));
|
|
2694
|
-
}
|
|
2695
|
-
};
|
|
2696
|
-
var SHAPE_CLASS = "dx-shape";
|
|
2697
|
-
var renderCustomShape = (group, options) => {
|
|
2698
|
-
group.each(function(d) {
|
|
2699
|
-
const g = select4(this);
|
|
2700
|
-
g.selectAll(`.${SHAPE_CLASS}`).remove();
|
|
2701
|
-
const shapeGroup = g.append("g").classed(SHAPE_CLASS, true);
|
|
2702
|
-
options.renderNode(shapeGroup, d);
|
|
2703
|
-
});
|
|
2704
|
-
};
|
|
2705
|
-
var createNode = (group, options) => {
|
|
2706
|
-
const useCustom = !!options.renderNode;
|
|
2707
|
-
if (useCustom) {
|
|
2708
|
-
renderCustomShape(group, options);
|
|
2709
|
-
}
|
|
2710
|
-
const hitTarget = useCustom ? group : group.append("circle");
|
|
2711
|
-
if (options.drag) {
|
|
2712
|
-
hitTarget.call(options.drag);
|
|
2713
|
-
}
|
|
2714
|
-
if (options.onNodeClick) {
|
|
2715
|
-
group.on("click", (event, d) => {
|
|
2716
|
-
options.onNodeClick(d, event);
|
|
2717
|
-
});
|
|
2718
|
-
}
|
|
2719
|
-
if (options.labels && !options.onNodePointerEnter) {
|
|
2720
|
-
const g = group.append("g").classed("dx-label", true);
|
|
2721
|
-
g.append("line");
|
|
2722
|
-
g.append("rect");
|
|
2723
|
-
g.append("text").style("dominant-baseline", "middle");
|
|
2724
|
-
}
|
|
2725
|
-
if (options.onNodePointerEnter || options.onNodePointerLeave || options.highlightOnHover) {
|
|
2726
|
-
hitTarget.on("pointerenter", function(event) {
|
|
2727
|
-
const node = select4(useCustom ? this : this.parentElement).datum();
|
|
2728
|
-
options.onNodePointerEnter?.(node, event);
|
|
2729
|
-
if (options.highlightOnHover) {
|
|
2730
|
-
applyHoverHighlight(this.closest("g.dx-graph"), node.id);
|
|
2731
|
-
}
|
|
2732
|
-
});
|
|
2733
|
-
hitTarget.on("pointerleave", function(event) {
|
|
2734
|
-
const node = select4(useCustom ? this : this.parentElement).datum();
|
|
2735
|
-
options.onNodePointerLeave?.(node, event);
|
|
2736
|
-
if (options.highlightOnHover) {
|
|
2737
|
-
applyHoverHighlight(this.closest("g.dx-graph"), null);
|
|
2738
|
-
}
|
|
2739
|
-
});
|
|
2740
|
-
group.attr("data-hover", "handled");
|
|
2741
|
-
} else if (options.highlight !== false && !useCustom) {
|
|
2742
|
-
hitTarget.on("pointerenter", function() {
|
|
2743
|
-
select4(this.closest("g.dx-node")).raise();
|
|
2744
|
-
if (options.labels) {
|
|
2745
|
-
select4(this.parentElement).classed("dx-node-active", true).classed("dx-highlight", true);
|
|
2746
|
-
}
|
|
2747
|
-
});
|
|
2748
|
-
group.on("pointerleave", function() {
|
|
2749
|
-
if (options.labels) {
|
|
2750
|
-
select4(this).classed("dx-node-active", false);
|
|
2751
|
-
setTimeout(() => {
|
|
2752
|
-
if (!select4(this).classed("dx-node-active")) {
|
|
2753
|
-
select4(this).classed("dx-highlight", false);
|
|
2754
|
-
}
|
|
2755
|
-
}, 300);
|
|
2756
|
-
}
|
|
2757
|
-
});
|
|
2758
|
-
}
|
|
2759
|
-
};
|
|
2760
|
-
var updateNode2 = (group, options) => {
|
|
2761
|
-
group.attr("transform", (d) => {
|
|
2762
|
-
return d.x != null && d.y != null ? `translate(${d.x},${d.y})` : void 0;
|
|
2763
|
-
});
|
|
2764
|
-
group.classed("dx-hidden", (d) => d.hidden ?? false);
|
|
2765
|
-
group.transition("hide").ease(easeCubicOut).duration(NODE_HIDE_MS).attr("opacity", (d) => d.hidden ? 0 : 1);
|
|
2766
|
-
if (options.renderNode) {
|
|
2767
|
-
renderCustomShape(group, options);
|
|
2768
|
-
}
|
|
2769
|
-
if (options.attributes?.node) {
|
|
2770
|
-
try {
|
|
2771
|
-
group.each((d, i, nodes) => {
|
|
2772
|
-
const node = select4(nodes[i]);
|
|
2773
|
-
const { classes, data } = options.attributes?.node?.(d) ?? {};
|
|
2774
|
-
if (classes) {
|
|
2775
|
-
applyClasses(node, classes);
|
|
2776
|
-
}
|
|
2777
|
-
if (data) {
|
|
2778
|
-
Object.entries(data).forEach(([key, value]) => {
|
|
2779
|
-
node.attr(`data-${key}`, value);
|
|
2780
|
-
});
|
|
2781
|
-
}
|
|
2782
|
-
});
|
|
2783
|
-
} catch (error) {
|
|
2784
|
-
log11.error("updateNode", {
|
|
2785
|
-
error: error?.message
|
|
2786
|
-
}, { "~LogMeta": "~LogMeta", F: __dxlog_file13, L: 303, S: void 0 });
|
|
2787
|
-
}
|
|
2788
|
-
}
|
|
2789
|
-
const groupOrTransition = options.transition ? group.transition(options.transition()) : group;
|
|
2790
|
-
if (!options.renderNode) {
|
|
2791
|
-
groupOrTransition.select("circle").attr("r", (d) => d.r ?? 16);
|
|
2792
|
-
}
|
|
2793
|
-
if (options.labels) {
|
|
2794
|
-
const bx = 1;
|
|
2795
|
-
const px = 4;
|
|
2796
|
-
const py = 2;
|
|
2797
|
-
const offset = 16;
|
|
2798
|
-
const dx = (d, offset2 = 0) => (offset2 + (d.r ?? 0)) * (d.x > 0 ? 1 : -1);
|
|
2799
|
-
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) {
|
|
2800
|
-
let bbox = d.bbox;
|
|
2801
|
-
if (!bbox) {
|
|
2802
|
-
bbox = this.getBBox();
|
|
2803
|
-
d.bbox = bbox;
|
|
2804
|
-
}
|
|
2805
|
-
const width = bbox.width + (bx + px) * 2;
|
|
2806
|
-
const height = bbox.height + py * 2;
|
|
2807
|
-
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);
|
|
2808
|
-
select4(this.parentElement).select("line").attr("x1", dx(d, 1)).attr("y1", 0).attr("x2", dx(d, offset - (bx + px))).attr("y2", 0);
|
|
2809
|
-
});
|
|
2810
|
-
}
|
|
2811
|
-
};
|
|
2812
|
-
var createEdge = (group, options) => {
|
|
2813
|
-
if (options.onLinkClick) {
|
|
2814
|
-
group.append("path").classed("dx-click", true).on("click", (event) => {
|
|
2815
|
-
const edge = select4(event.target).datum();
|
|
2816
|
-
options.onLinkClick(edge, event);
|
|
2817
|
-
});
|
|
2818
|
-
}
|
|
2819
|
-
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);
|
|
2820
|
-
};
|
|
2821
|
-
var updateEdge = (group, options, nodeGroup) => {
|
|
2822
|
-
try {
|
|
2823
|
-
group.each((d, i, edges) => {
|
|
2824
|
-
const edge = select4(edges[i]);
|
|
2825
|
-
edge.classed("dx-dashed", d.linkForce === false);
|
|
2826
|
-
const { classes, data } = options.attributes?.edge?.(d) ?? {};
|
|
2827
|
-
if (classes) {
|
|
2828
|
-
applyClasses(edge, classes);
|
|
2829
|
-
}
|
|
2830
|
-
if (data) {
|
|
2831
|
-
Object.entries(data).forEach(([key, value]) => {
|
|
2832
|
-
edge.attr(`data-${key}`, value);
|
|
2833
|
-
});
|
|
2834
|
-
}
|
|
2835
|
-
});
|
|
2836
|
-
} catch (error) {
|
|
2837
|
-
log11.error("updateEdge", {
|
|
2838
|
-
error: error?.message
|
|
2839
|
-
}, { "~LogMeta": "~LogMeta", F: __dxlog_file13, L: 371, S: void 0 });
|
|
2840
|
-
}
|
|
2841
|
-
const groupOrTransition = options.transition ? group.transition(options.transition()) : group;
|
|
2842
|
-
groupOrTransition.selectAll("path").attr("d", function() {
|
|
2843
|
-
const edge = select4(this.parentElement).datum();
|
|
2844
|
-
if (edge.path) {
|
|
2845
|
-
return edge.path;
|
|
2846
|
-
}
|
|
2847
|
-
const { source, target } = edge;
|
|
2848
|
-
if (!source.initialized || !target.initialized) {
|
|
2849
|
-
return;
|
|
2850
|
-
}
|
|
2851
|
-
return createLine2(getCircumferencePoints([
|
|
2852
|
-
source.x,
|
|
2853
|
-
source.y
|
|
2854
|
-
], [
|
|
2855
|
-
target.x,
|
|
2856
|
-
target.y
|
|
2857
|
-
], source.r, target.r));
|
|
2858
|
-
});
|
|
2859
|
-
};
|
|
2860
|
-
var applyHoverHighlight = (root, focusedId) => {
|
|
2861
|
-
if (!root) {
|
|
2862
|
-
return;
|
|
2863
|
-
}
|
|
2864
|
-
const r = select4(root);
|
|
2865
|
-
const on = focusedId != null;
|
|
2866
|
-
const outgoing = /* @__PURE__ */ new Set();
|
|
2867
|
-
const incoming = /* @__PURE__ */ new Set();
|
|
2868
|
-
if (on) {
|
|
2869
|
-
r.select("g.dx-edges").selectAll("g.dx-edge").each((edge) => {
|
|
2870
|
-
if (edge.source.id === focusedId) {
|
|
2871
|
-
outgoing.add(edge.target.id);
|
|
2872
|
-
}
|
|
2873
|
-
if (edge.target.id === focusedId) {
|
|
2874
|
-
incoming.add(edge.source.id);
|
|
2875
|
-
}
|
|
2876
|
-
});
|
|
2877
|
-
}
|
|
2878
|
-
r.select("g.dx-edges").selectAll("g.dx-edge").each(function(edge) {
|
|
2879
|
-
const path = select4(this).select("path:not(.dx-click)");
|
|
2880
|
-
if (!on) {
|
|
2881
|
-
path.style("stroke", null).style("stroke-width", null).style("opacity", null);
|
|
2882
|
-
return;
|
|
2883
|
-
}
|
|
2884
|
-
if (edge.source.id === focusedId) {
|
|
2885
|
-
path.style("stroke", "var(--color-orange-500)").style("stroke-width", "1.5px").style("opacity", null);
|
|
2886
|
-
} else if (edge.target.id === focusedId) {
|
|
2887
|
-
path.style("stroke", "var(--color-sky-500)").style("stroke-width", "1.5px").style("opacity", null);
|
|
2888
|
-
} else {
|
|
2889
|
-
path.style("stroke", null).style("stroke-width", null).style("opacity", "0.8");
|
|
2890
|
-
}
|
|
2891
|
-
});
|
|
2892
|
-
r.select("g.dx-nodes").selectAll("g.dx-node").each(function(node) {
|
|
2893
|
-
const groupSel = select4(this);
|
|
2894
|
-
const shape = groupSel.select("circle, rect");
|
|
2895
|
-
if (node.hidden) {
|
|
2896
|
-
return;
|
|
2897
|
-
}
|
|
2898
|
-
if (!on) {
|
|
2899
|
-
groupSel.style("opacity", null);
|
|
2900
|
-
shape.style("stroke", null).style("stroke-width", null);
|
|
2901
|
-
return;
|
|
2902
|
-
}
|
|
2903
|
-
const connected = outgoing.has(node.id) || incoming.has(node.id);
|
|
2904
|
-
if (node.id === focusedId) {
|
|
2905
|
-
groupSel.style("opacity", null);
|
|
2906
|
-
shape.style("stroke", null).style("stroke-width", null);
|
|
2907
|
-
} else if (connected) {
|
|
2908
|
-
groupSel.style("opacity", null);
|
|
2909
|
-
shape.style("stroke", "var(--color-orange-400)").style("stroke-width", "2.5px");
|
|
2910
|
-
} else {
|
|
2911
|
-
groupSel.style("opacity", "0.4");
|
|
2912
|
-
shape.style("stroke", null).style("stroke-width", null);
|
|
2913
|
-
}
|
|
2914
|
-
});
|
|
2915
|
-
};
|
|
2916
|
-
var applyClasses = (el, classes) => {
|
|
2917
|
-
for (const [className, value] of Object.entries(classes)) {
|
|
2918
|
-
el.classed(className, value);
|
|
2919
|
-
}
|
|
2920
|
-
};
|
|
2921
|
-
var findConnectedComponents = (graph) => {
|
|
2922
|
-
const adj = {};
|
|
2923
|
-
for (const node of graph.nodes) {
|
|
2924
|
-
adj[node.id] = [];
|
|
2925
|
-
}
|
|
2926
|
-
for (const { source, target } of graph.edges) {
|
|
2927
|
-
adj[source].push(target);
|
|
2928
|
-
adj[target].push(source);
|
|
2929
|
-
}
|
|
2930
|
-
const visited = /* @__PURE__ */ new Set();
|
|
2931
|
-
const components = [];
|
|
2932
|
-
for (const node of graph.nodes) {
|
|
2933
|
-
if (visited.has(node.id)) {
|
|
2934
|
-
continue;
|
|
2935
|
-
}
|
|
2936
|
-
const stack = [
|
|
2937
|
-
node.id
|
|
2938
|
-
];
|
|
2939
|
-
const componentIds = [];
|
|
2940
|
-
while (stack.length) {
|
|
2941
|
-
const current = stack.pop();
|
|
2942
|
-
if (visited.has(current)) {
|
|
2943
|
-
continue;
|
|
2944
|
-
}
|
|
2945
|
-
visited.add(current);
|
|
2946
|
-
componentIds.push(current);
|
|
2947
|
-
stack.push(...adj[current]);
|
|
2948
|
-
}
|
|
2949
|
-
components.push(componentIds);
|
|
2950
|
-
}
|
|
2951
|
-
return components;
|
|
2952
|
-
};
|
|
2953
|
-
|
|
2954
|
-
// src/graph/renderer/labels.ts
|
|
2955
|
-
var DEFAULT_DURATION = 200;
|
|
2956
|
-
var DEFAULT_OFFSET = 4;
|
|
2957
|
-
var getTarget = (node) => {
|
|
2958
|
-
const tx = node.tx;
|
|
2959
|
-
const ty = node.ty;
|
|
2960
|
-
return {
|
|
2961
|
-
x: tx ?? node.x ?? 0,
|
|
2962
|
-
y: ty ?? node.y ?? 0
|
|
2963
|
-
};
|
|
2964
|
-
};
|
|
2965
|
-
var appendRadialLeafLabel = (group, node, label, r, options = {}) => {
|
|
2966
|
-
if (!label) {
|
|
2967
|
-
return;
|
|
2968
|
-
}
|
|
2969
|
-
const { delay = 0, duration = DEFAULT_DURATION, offset = DEFAULT_OFFSET, className = "dx-cluster-label" } = options;
|
|
2970
|
-
const { x: targetX, y: targetY } = getTarget(node);
|
|
2971
|
-
const angleDeg = Math.atan2(targetY, targetX) * 180 / Math.PI;
|
|
2972
|
-
const flipped = angleDeg > 90 || angleDeg < -90;
|
|
2973
|
-
group.append("text").attr("class", className).attr("dy", "0.32em").attr("transform", `rotate(${flipped ? angleDeg + 180 : angleDeg})`).attr("x", flipped ? -(r + offset) : r + offset).attr("text-anchor", flipped ? "end" : "start").attr("opacity", 0).style("cursor", "pointer").text(label).transition().delay(delay).duration(duration).attr("opacity", 1);
|
|
2974
|
-
};
|
|
2975
|
-
var appendRadialGroupLabel = (group, node, label, r, options = {}) => {
|
|
2976
|
-
if (!label) {
|
|
2977
|
-
return;
|
|
2978
|
-
}
|
|
2979
|
-
const { delay = 0, duration = DEFAULT_DURATION, offset = DEFAULT_OFFSET, className = "dx-cluster-label dx-cluster-label-group" } = options;
|
|
2980
|
-
const { x: targetX, y: targetY } = getTarget(node);
|
|
2981
|
-
const angleDeg = Math.atan2(targetY, targetX) * 180 / Math.PI;
|
|
2982
|
-
const flipped = angleDeg > 90 || angleDeg < -90;
|
|
2983
|
-
group.append("text").attr("class", className).attr("dy", "0.32em").attr("transform", `rotate(${flipped ? angleDeg + 180 : angleDeg})`).attr("x", flipped ? r + offset : -(r + offset)).attr("text-anchor", flipped ? "start" : "end").attr("opacity", 0).style("pointer-events", "none").text(label).transition().delay(delay).duration(duration).attr("opacity", 1);
|
|
2984
|
-
};
|
|
2985
|
-
var appendRootLabel = (group, label, r, options = {}) => {
|
|
2986
|
-
if (!label) {
|
|
2987
|
-
return;
|
|
2988
|
-
}
|
|
2989
|
-
const { delay = 0, duration = DEFAULT_DURATION, offset = 6, className = "dx-cluster-label dx-cluster-label-root" } = options;
|
|
2990
|
-
group.append("text").attr("class", className).attr("text-anchor", "middle").attr("y", -(r + offset)).attr("opacity", 0).style("pointer-events", "none").text(label).transition().delay(delay).duration(duration).attr("opacity", 1);
|
|
2991
|
-
};
|
|
2992
|
-
|
|
2993
|
-
// src/graph/renderer/linker.ts
|
|
2994
|
-
import { line as line3 } from "d3";
|
|
2995
|
-
var createLine3 = line3();
|
|
2996
|
-
var linkerRenderer = (root, { source, target, point } = {}) => {
|
|
2997
|
-
root.selectAll("g.dx-linker").data([
|
|
2998
|
-
{
|
|
2999
|
-
id: "linker"
|
|
3000
|
-
}
|
|
3001
|
-
]).join("g").attr("class", "dx-linker").selectAll("path").data(source ? [
|
|
3002
|
-
{
|
|
3003
|
-
id: "edge"
|
|
3004
|
-
}
|
|
3005
|
-
] : []).join("path").attr("marker-end", () => target ? "url(#marker-arrow-end)" : "url(#marker-dot)").attr("d", () => {
|
|
3006
|
-
return createLine3(getCircumferencePoints([
|
|
3007
|
-
source.x,
|
|
3008
|
-
source.y
|
|
3009
|
-
], target ? [
|
|
3010
|
-
target.x,
|
|
3011
|
-
target.y
|
|
3012
|
-
] : point, source.r, target ? target.r : 0));
|
|
3013
|
-
});
|
|
3014
|
-
};
|
|
3015
|
-
|
|
3016
|
-
// src/hooks/useGrid.ts
|
|
3017
|
-
import { line as line4, range, select as select5 } from "d3";
|
|
3018
|
-
import { useEffect, useMemo, useRef } from "react";
|
|
3019
|
-
|
|
3020
|
-
// src/hooks/useSvgContext.ts
|
|
3021
|
-
import { createContext, useContext } from "react";
|
|
3022
|
-
import { createRef } from "react";
|
|
3023
|
-
import { raise } from "@dxos/debug";
|
|
3024
|
-
var SVGContext = class {
|
|
3025
|
-
_ref = createRef();
|
|
3026
|
-
_scale;
|
|
3027
|
-
_centered;
|
|
3028
|
-
_size;
|
|
3029
|
-
_center;
|
|
3030
|
-
resized = new EventEmitter();
|
|
3031
|
-
constructor({ scale = new Scale(), centered = true }) {
|
|
3032
|
-
this._scale = scale;
|
|
3033
|
-
this._centered = centered;
|
|
3034
|
-
}
|
|
3035
|
-
get ref() {
|
|
3036
|
-
return this._ref;
|
|
3037
|
-
}
|
|
3038
|
-
get svg() {
|
|
3039
|
-
return this._ref.current;
|
|
3040
|
-
}
|
|
3041
|
-
get scale() {
|
|
3042
|
-
return this._scale;
|
|
3043
|
-
}
|
|
3044
|
-
get center() {
|
|
3045
|
-
return this._center;
|
|
3046
|
-
}
|
|
3047
|
-
get size() {
|
|
3048
|
-
return this._size;
|
|
3049
|
-
}
|
|
3050
|
-
/**
|
|
3051
|
-
* https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/viewBox
|
|
3052
|
-
*/
|
|
3053
|
-
get viewBox() {
|
|
3054
|
-
const [x, y] = this.center;
|
|
3055
|
-
return `${x},${y},${this._size.width},${this._size.height}`;
|
|
3056
|
-
}
|
|
3057
|
-
setSize(size) {
|
|
3058
|
-
this._size = {
|
|
3059
|
-
...size
|
|
3060
|
-
};
|
|
3061
|
-
this._center = this._centered ? [
|
|
3062
|
-
-Math.floor(size.width / 2),
|
|
3063
|
-
-Math.floor(size.height / 2)
|
|
3064
|
-
] : [
|
|
3065
|
-
0,
|
|
3066
|
-
0
|
|
3067
|
-
];
|
|
3068
|
-
this.resized.emit(this);
|
|
3069
|
-
}
|
|
3070
|
-
setTransform(transform) {
|
|
3071
|
-
if (this._scale.setTransform(transform)) {
|
|
3072
|
-
this.resized.emit(this);
|
|
3073
|
-
}
|
|
3074
|
-
}
|
|
3075
|
-
/**
|
|
3076
|
-
* https://developer.mozilla.org/en-US/docs/Web/API/Document/elementFromPoint
|
|
3077
|
-
* @param point
|
|
3078
|
-
*/
|
|
3079
|
-
elementFromPoint(point) {
|
|
3080
|
-
const [x, y] = point;
|
|
3081
|
-
const [cx, cy] = this._center;
|
|
3082
|
-
return document.elementFromPoint(cx + x, cy + y);
|
|
3083
|
-
}
|
|
3084
|
-
};
|
|
3085
|
-
var SVGContextType = createContext(void 0);
|
|
3086
|
-
var SVGContextProvider = SVGContextType.Provider;
|
|
3087
|
-
var useSvgContext = () => {
|
|
3088
|
-
return useContext(SVGContextType) ?? raise(new Error("Missing SVGRoot"));
|
|
3089
|
-
};
|
|
3090
|
-
|
|
3091
|
-
// src/hooks/useGrid.ts
|
|
3092
|
-
var createLine4 = line4();
|
|
3093
|
-
var createGrid = (context, options) => {
|
|
3094
|
-
if (!context.size) {
|
|
3095
|
-
return [];
|
|
3096
|
-
}
|
|
3097
|
-
const paths = [];
|
|
3098
|
-
const { width, height } = context.size;
|
|
3099
|
-
const { x, y, k } = context.scale.transform;
|
|
3100
|
-
const s = 1 / k;
|
|
3101
|
-
const w = width * s;
|
|
3102
|
-
const h = height * s;
|
|
3103
|
-
const dx = -(x + width / 2) * s;
|
|
3104
|
-
const dy = -(y + height / 2) * s;
|
|
3105
|
-
if (options.axis) {
|
|
3106
|
-
const axis = [
|
|
3107
|
-
[
|
|
3108
|
-
[
|
|
3109
|
-
0,
|
|
3110
|
-
dy
|
|
3111
|
-
],
|
|
3112
|
-
[
|
|
3113
|
-
0,
|
|
3114
|
-
dy + h
|
|
3115
|
-
]
|
|
3116
|
-
],
|
|
3117
|
-
[
|
|
3118
|
-
[
|
|
3119
|
-
dx,
|
|
3120
|
-
0
|
|
3121
|
-
],
|
|
3122
|
-
[
|
|
3123
|
-
dx + w,
|
|
3124
|
-
0
|
|
3125
|
-
]
|
|
3126
|
-
]
|
|
3127
|
-
];
|
|
3128
|
-
paths.push({
|
|
3129
|
-
id: "axis",
|
|
3130
|
-
class: "dx-axis",
|
|
3131
|
-
path: axis.map((line5) => createLine4(line5)).join()
|
|
3132
|
-
});
|
|
3133
|
-
}
|
|
3134
|
-
if (options.grid) {
|
|
3135
|
-
const mod = (n, size, delta = 0) => Math.floor(n / size + delta) * size;
|
|
3136
|
-
const majorSize = context.scale.gridSize;
|
|
3137
|
-
const xMajor = range(-mod((x + width / 2) * s, majorSize), mod((-x + width / 2) * s, majorSize, 1), majorSize);
|
|
3138
|
-
const yMajor = range(-mod((y + height / 2) * s, majorSize), mod((-y + height / 2) * s, majorSize, 1), majorSize);
|
|
3139
|
-
const major = [
|
|
3140
|
-
...xMajor.filter((x2) => !options.axis || x2).map((x2) => [
|
|
3141
|
-
[
|
|
3142
|
-
x2,
|
|
3143
|
-
dy
|
|
3144
|
-
],
|
|
3145
|
-
[
|
|
3146
|
-
x2,
|
|
3147
|
-
dy + h
|
|
3148
|
-
]
|
|
3149
|
-
]),
|
|
3150
|
-
...yMajor.filter((y2) => !options.axis || y2).map((y2) => [
|
|
3151
|
-
[
|
|
3152
|
-
dx,
|
|
3153
|
-
y2
|
|
3154
|
-
],
|
|
3155
|
-
[
|
|
3156
|
-
dx + w,
|
|
3157
|
-
y2
|
|
3158
|
-
]
|
|
3159
|
-
])
|
|
3160
|
-
];
|
|
3161
|
-
paths.push({
|
|
3162
|
-
id: "major",
|
|
3163
|
-
class: "dx-major",
|
|
3164
|
-
path: major.map((line5) => createLine4(line5)).join()
|
|
3165
|
-
});
|
|
3166
|
-
const minorSize = Math.pow(2, Math.round(Math.log2(s * context.scale.gridSize)));
|
|
3167
|
-
if (majorSize > minorSize) {
|
|
3168
|
-
const xMinor = range(-mod((x + width / 2) * s, minorSize), mod((-x + width / 2) * s, minorSize, 1), minorSize);
|
|
3169
|
-
const yMinor = range(-mod((y + height / 2) * s, minorSize), mod((-y + height / 2) * s, minorSize, 1), minorSize);
|
|
3170
|
-
const minor = [
|
|
3171
|
-
...xMinor.filter((x2) => xMajor.indexOf(x2) === -1).map((x2) => [
|
|
3172
|
-
[
|
|
3173
|
-
x2,
|
|
3174
|
-
dy
|
|
3175
|
-
],
|
|
3176
|
-
[
|
|
3177
|
-
x2,
|
|
3178
|
-
dy + h
|
|
3179
|
-
]
|
|
3180
|
-
]),
|
|
3181
|
-
...yMinor.filter((y2) => yMajor.indexOf(y2) === -1).map((y2) => [
|
|
3182
|
-
[
|
|
3183
|
-
dx,
|
|
3184
|
-
y2
|
|
3185
|
-
],
|
|
3186
|
-
[
|
|
3187
|
-
dx + w,
|
|
3188
|
-
y2
|
|
3189
|
-
]
|
|
3190
|
-
])
|
|
3191
|
-
];
|
|
3192
|
-
paths.push({
|
|
3193
|
-
id: "minor",
|
|
3194
|
-
class: "dx-minor",
|
|
3195
|
-
path: minor.map((line5) => createLine4(line5)).join()
|
|
3196
|
-
});
|
|
3197
|
-
}
|
|
3198
|
-
}
|
|
3199
|
-
return paths;
|
|
3200
|
-
};
|
|
3201
|
-
var defaultGridOptions = {
|
|
3202
|
-
visible: true,
|
|
3203
|
-
axis: true,
|
|
3204
|
-
grid: true
|
|
3205
|
-
};
|
|
3206
|
-
var GridController = class {
|
|
3207
|
-
_ref;
|
|
3208
|
-
_context;
|
|
3209
|
-
_options;
|
|
3210
|
-
_visible = false;
|
|
3211
|
-
constructor(_ref, _context, _options) {
|
|
3212
|
-
this._ref = _ref;
|
|
3213
|
-
this._context = _context;
|
|
3214
|
-
this._options = _options;
|
|
3215
|
-
this._visible = this._options.visible ?? true;
|
|
3216
|
-
}
|
|
3217
|
-
get ref() {
|
|
3218
|
-
return this._ref;
|
|
3219
|
-
}
|
|
3220
|
-
get visible() {
|
|
3221
|
-
return this._visible;
|
|
3222
|
-
}
|
|
3223
|
-
draw() {
|
|
3224
|
-
if (!this._context.size) {
|
|
3225
|
-
return;
|
|
3226
|
-
}
|
|
3227
|
-
const paths = this._visible ? createGrid(this._context, this._options) : [];
|
|
3228
|
-
const root = select5(this._ref.current);
|
|
3229
|
-
root.selectAll("path").data(paths, (path) => path.id).join("path").style("pointer-events", "none").attr("d", (d) => d.path).attr("class", (d) => d.class);
|
|
3230
|
-
const transform = this._context.scale.transform;
|
|
3231
|
-
if (transform) {
|
|
3232
|
-
root.attr("transform", transform);
|
|
3233
|
-
root.attr("stroke-width", 1 / transform.k);
|
|
3234
|
-
}
|
|
3235
|
-
}
|
|
3236
|
-
setVisible(visible) {
|
|
3237
|
-
this._visible = visible;
|
|
3238
|
-
this.draw();
|
|
3239
|
-
return this;
|
|
3240
|
-
}
|
|
3241
|
-
};
|
|
3242
|
-
var useGrid = (options = defaultGridOptions) => {
|
|
3243
|
-
const ref = useRef(null);
|
|
3244
|
-
const context = useSvgContext();
|
|
3245
|
-
const controller = useMemo(() => new GridController(ref, context, options), []);
|
|
3246
|
-
useEffect(() => context.resized.on(() => controller.draw()), [
|
|
3247
|
-
context,
|
|
3248
|
-
controller
|
|
3249
|
-
]);
|
|
3250
|
-
return controller;
|
|
3251
|
-
};
|
|
3252
|
-
|
|
3253
|
-
// src/hooks/useZoom.ts
|
|
3254
|
-
import { select as select6, zoom, zoomIdentity as zoomIdentity2 } from "d3";
|
|
3255
|
-
import defaultsDeep from "lodash.defaultsdeep";
|
|
3256
|
-
import { useEffect as useEffect2, useMemo as useMemo2, useRef as useRef2 } from "react";
|
|
3257
|
-
var defaultZoomOptions = {
|
|
3258
|
-
enabled: true,
|
|
3259
|
-
extent: [
|
|
3260
|
-
1 / 4,
|
|
3261
|
-
4
|
|
3262
|
-
],
|
|
3263
|
-
onDblClick: (zoom2) => zoom2.reset()
|
|
3264
|
-
};
|
|
3265
|
-
var ZoomHandler = class {
|
|
3266
|
-
_ref;
|
|
3267
|
-
_context;
|
|
3268
|
-
_zoom;
|
|
3269
|
-
_options;
|
|
3270
|
-
_enabled;
|
|
3271
|
-
constructor(_ref, _context, options) {
|
|
3272
|
-
this._ref = _ref;
|
|
3273
|
-
this._context = _context;
|
|
3274
|
-
this._options = defaultsDeep({}, options, defaultZoomOptions);
|
|
3275
|
-
this._enabled = this._options.enabled ?? true;
|
|
3276
|
-
this._zoom = zoom().scaleExtent(this._options.extent ?? defaultZoomOptions.extent);
|
|
3277
|
-
}
|
|
3278
|
-
/**
|
|
3279
|
-
* Gets the reference which the transform is applied to.
|
|
3280
|
-
*/
|
|
3281
|
-
get ref() {
|
|
3282
|
-
return this._ref;
|
|
3283
|
-
}
|
|
3284
|
-
get zoom() {
|
|
3285
|
-
return this._zoom;
|
|
3286
|
-
}
|
|
3287
|
-
init() {
|
|
3288
|
-
this.setEnabled(this._enabled);
|
|
3289
|
-
this.reset(0);
|
|
3290
|
-
return this;
|
|
3291
|
-
}
|
|
3292
|
-
setEnabled(enable) {
|
|
3293
|
-
if (enable) {
|
|
3294
|
-
select6(this._context.svg).call(this._zoom).on(
|
|
3295
|
-
"dblclick.zoom",
|
|
3296
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
3297
|
-
// @ts-ignore
|
|
3298
|
-
this._options?.onDblClick ? () => {
|
|
3299
|
-
this._options?.onDblClick(this);
|
|
3300
|
-
} : null
|
|
3301
|
-
);
|
|
3302
|
-
} else {
|
|
3303
|
-
select6(this._context.svg).on(".zoom", null);
|
|
3304
|
-
}
|
|
3305
|
-
this._enabled = enable;
|
|
3306
|
-
return this;
|
|
3307
|
-
}
|
|
3308
|
-
reset(duration = 500) {
|
|
3309
|
-
const scale = 1;
|
|
3310
|
-
const transform = zoomIdentity2.scale(scale);
|
|
3311
|
-
select6(this._context.svg).transition().duration(duration).call(this._zoom.transform, transform);
|
|
3312
|
-
return this;
|
|
3313
|
-
}
|
|
3314
|
-
};
|
|
3315
|
-
var useZoom = (options = defaultZoomOptions) => {
|
|
3316
|
-
const context = useSvgContext();
|
|
3317
|
-
const ref = useRef2(null);
|
|
3318
|
-
const handler = useMemo2(() => new ZoomHandler(ref, context, options), [
|
|
3319
|
-
context,
|
|
3320
|
-
options
|
|
3321
|
-
]);
|
|
3322
|
-
useEffect2(() => {
|
|
3323
|
-
handler.zoom.on("zoom", ({ transform }) => {
|
|
3324
|
-
context.setTransform(transform);
|
|
3325
|
-
select6(ref.current).attr("transform", transform);
|
|
3326
|
-
});
|
|
3327
|
-
handler.init();
|
|
3328
|
-
}, [
|
|
3329
|
-
handler
|
|
3330
|
-
]);
|
|
3331
|
-
return handler;
|
|
3332
|
-
};
|
|
3333
|
-
|
|
3334
|
-
// src/components/Graph/Graph.tsx
|
|
3335
|
-
var __dxlog_file14 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/components/Graph/Graph.tsx";
|
|
3336
|
-
var GraphInner = ({ classNames, model, projector: projectorProp, renderer: rendererProp, drag: drag2, arrows, onSelect, onInspect, onPointerMove, onPointerLeave, ...props }, forwardedRef) => {
|
|
3337
|
-
const context = useSvgContext();
|
|
3338
|
-
const graphRef = useRef3(null);
|
|
3339
|
-
const { projector, renderer } = useMemo3(() => {
|
|
3340
|
-
let projector2 = projectorProp;
|
|
3341
|
-
if (!projector2) {
|
|
3342
|
-
projector2 = new GraphForceProjector(context);
|
|
3343
|
-
}
|
|
3344
|
-
let renderer2 = rendererProp;
|
|
3345
|
-
if (!renderer2) {
|
|
3346
|
-
renderer2 = new GraphRenderer(context, graphRef, {
|
|
3347
|
-
...props,
|
|
3348
|
-
// TODO(burdon): Replace drag when projector is updated.
|
|
3349
|
-
drag: drag2 ? createGraphDrag(context, projector2) : void 0,
|
|
3350
|
-
arrows: {
|
|
3351
|
-
end: arrows
|
|
3352
|
-
},
|
|
3353
|
-
// Projector intercept first so built-in interactions (e.g. cluster collapse) can
|
|
3354
|
-
// run without consumers wiring them. Returning true suppresses the user handler.
|
|
3355
|
-
onNodeClick: (node, event) => {
|
|
3356
|
-
if (projector2.handleNodeClick(node, event)) {
|
|
3357
|
-
return;
|
|
3358
|
-
}
|
|
3359
|
-
onSelect?.(node, event);
|
|
3360
|
-
},
|
|
3361
|
-
onNodePointerEnter: onInspect ? (node, event) => onInspect(node, event) : void 0,
|
|
3362
|
-
// Pair pointerenter with leave so consumers can clear hover-driven previews.
|
|
3363
|
-
// The callback receives `null` on leave (event still carries the source).
|
|
3364
|
-
onNodePointerLeave: onInspect ? (_node, event) => onInspect(null, event) : void 0
|
|
3365
|
-
});
|
|
3366
|
-
}
|
|
3367
|
-
return {
|
|
3368
|
-
projector: projector2,
|
|
3369
|
-
renderer: renderer2
|
|
3370
|
-
};
|
|
3371
|
-
}, [
|
|
3372
|
-
context,
|
|
3373
|
-
projectorProp,
|
|
3374
|
-
rendererProp,
|
|
3375
|
-
drag2,
|
|
3376
|
-
arrows,
|
|
3377
|
-
onSelect,
|
|
3378
|
-
onInspect,
|
|
3379
|
-
props.labels,
|
|
3380
|
-
props.subgraphs,
|
|
3381
|
-
props.attributes,
|
|
3382
|
-
props.renderNode,
|
|
3383
|
-
props.highlightOnHover,
|
|
3384
|
-
props.applyNode,
|
|
3385
|
-
props.edgeOpacity
|
|
3386
|
-
]);
|
|
3387
|
-
useImperativeHandle(forwardedRef, () => ({
|
|
3388
|
-
refresh: () => {
|
|
3389
|
-
projector.updateData(model?.graph);
|
|
3390
|
-
},
|
|
3391
|
-
repaint: () => {
|
|
3392
|
-
renderer.render(projector.layout);
|
|
3393
|
-
},
|
|
3394
|
-
findNode: (id) => {
|
|
3395
|
-
return graphRef.current?.querySelector(`g[data-id="${id}"]`);
|
|
3396
|
-
}
|
|
3397
|
-
}), [
|
|
3398
|
-
model,
|
|
3399
|
-
projector,
|
|
3400
|
-
renderer
|
|
3401
|
-
]);
|
|
3402
|
-
useEffect3(() => {
|
|
3403
|
-
const cleanup = combine(
|
|
3404
|
-
// Subscribe to model changes if reactive model.
|
|
3405
|
-
model ? model.subscribe(() => {
|
|
3406
|
-
projector.updateData(model?.graph);
|
|
3407
|
-
}) : void 0,
|
|
3408
|
-
projector.updated.on(({ layout, kind }) => {
|
|
3409
|
-
try {
|
|
3410
|
-
if (kind === "positions") {
|
|
3411
|
-
renderer.applyPositions(layout);
|
|
3412
|
-
} else {
|
|
3413
|
-
renderer.render(layout);
|
|
3414
|
-
}
|
|
3415
|
-
} catch (error) {
|
|
3416
|
-
log12.catch(error, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file14, L: 95, S: void 0 });
|
|
3417
|
-
void projector.stop();
|
|
3418
|
-
}
|
|
3419
|
-
})
|
|
3420
|
-
);
|
|
3421
|
-
projector.updateData(model?.graph);
|
|
3422
|
-
return cleanup;
|
|
3423
|
-
}, [
|
|
3424
|
-
model,
|
|
3425
|
-
projector,
|
|
3426
|
-
renderer
|
|
3427
|
-
]);
|
|
3428
|
-
useEffect3(() => {
|
|
3429
|
-
void projector.start();
|
|
3430
|
-
return () => {
|
|
3431
|
-
void projector.stop();
|
|
3432
|
-
};
|
|
3433
|
-
}, [
|
|
3434
|
-
projector
|
|
3435
|
-
]);
|
|
3436
|
-
useEffect3(() => {
|
|
3437
|
-
if (!onPointerMove && !onPointerLeave) {
|
|
3438
|
-
return;
|
|
3439
|
-
}
|
|
3440
|
-
const svg = context.svg;
|
|
3441
|
-
if (!svg) {
|
|
3442
|
-
return;
|
|
3443
|
-
}
|
|
3444
|
-
const previousPointerEvents = svg.style.pointerEvents;
|
|
3445
|
-
svg.style.pointerEvents = "all";
|
|
3446
|
-
const handleMove = (event) => {
|
|
3447
|
-
if (!onPointerMove) {
|
|
3448
|
-
return;
|
|
3449
|
-
}
|
|
3450
|
-
const target = graphRef.current;
|
|
3451
|
-
const ctm = target?.getScreenCTM();
|
|
3452
|
-
if (!target || !ctm) {
|
|
3453
|
-
return;
|
|
3454
|
-
}
|
|
3455
|
-
const point = svg.createSVGPoint();
|
|
3456
|
-
point.x = event.clientX;
|
|
3457
|
-
point.y = event.clientY;
|
|
3458
|
-
const modelPoint = point.matrixTransform(ctm.inverse());
|
|
3459
|
-
onPointerMove({
|
|
3460
|
-
x: modelPoint.x,
|
|
3461
|
-
y: modelPoint.y
|
|
3462
|
-
}, event);
|
|
3463
|
-
};
|
|
3464
|
-
const handleLeave = (event) => {
|
|
3465
|
-
onPointerLeave?.(event);
|
|
3466
|
-
};
|
|
3467
|
-
svg.addEventListener("pointermove", handleMove);
|
|
3468
|
-
svg.addEventListener("pointerleave", handleLeave);
|
|
3469
|
-
return () => {
|
|
3470
|
-
svg.removeEventListener("pointermove", handleMove);
|
|
3471
|
-
svg.removeEventListener("pointerleave", handleLeave);
|
|
3472
|
-
svg.style.pointerEvents = previousPointerEvents;
|
|
3473
|
-
};
|
|
3474
|
-
}, [
|
|
3475
|
-
context,
|
|
3476
|
-
onPointerMove,
|
|
3477
|
-
onPointerLeave
|
|
3478
|
-
]);
|
|
3479
|
-
return /* @__PURE__ */ React.createElement("g", {
|
|
3480
|
-
ref: graphRef,
|
|
3481
|
-
className: mx("dx-graph", classNames)
|
|
3482
|
-
});
|
|
3483
|
-
};
|
|
3484
|
-
var Graph = /* @__PURE__ */ forwardRef(GraphInner);
|
|
3485
|
-
|
|
3486
|
-
// src/components/Mesh/Mesh.tsx
|
|
3487
|
-
import { geoPath, select as select7 } from "d3";
|
|
3488
|
-
import { useEffect as useEffect4 } from "react";
|
|
3489
|
-
import { feature, mesh } from "topojson-client";
|
|
3490
|
-
var Mesh = ({ radius = 16, value = 0.5 }) => {
|
|
3491
|
-
const { svg, size } = useSvgContext();
|
|
3492
|
-
useEffect4(() => {
|
|
3493
|
-
if (!size) {
|
|
3494
|
-
return;
|
|
3495
|
-
}
|
|
3496
|
-
const topology = hexTopology(size, radius, value);
|
|
3497
|
-
const projection = hexProjection(radius);
|
|
3498
|
-
const path = geoPath().projection(projection);
|
|
3499
|
-
const root = select7(svg).selectAll("g.dx-mesh-root").data([
|
|
3500
|
-
null
|
|
3501
|
-
]).join("g").classed("dx-mesh-root", true);
|
|
3502
|
-
root.selectAll("*").remove();
|
|
3503
|
-
root.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);
|
|
3504
|
-
root.append("path").datum(mesh(topology, topology.objects.hexagons)).attr("class", "mesh").attr("d", path);
|
|
3505
|
-
const redraw = (border) => {
|
|
3506
|
-
border.attr("d", path(mesh(topology, topology.objects.hexagons, (a, b) => a.properties.fill !== b.properties.fill)));
|
|
3507
|
-
};
|
|
3508
|
-
root.append("path").attr("class", "border").call(redraw);
|
|
3509
|
-
}, [
|
|
3510
|
-
svg,
|
|
3511
|
-
size,
|
|
3512
|
-
value,
|
|
3513
|
-
radius
|
|
3514
|
-
]);
|
|
3515
|
-
return null;
|
|
3516
|
-
};
|
|
3517
|
-
var hexTopology = ({ width, height }, radius, value = 1) => {
|
|
3518
|
-
const dx = radius * 2 * Math.sin(Math.PI / 3);
|
|
3519
|
-
const dy = radius * 1.5;
|
|
3520
|
-
const m = Math.ceil((height + radius) / dy) + 1;
|
|
3521
|
-
const n = Math.ceil(width / dx) + 1;
|
|
3522
|
-
const geometries = [];
|
|
3523
|
-
const arcs = [];
|
|
3524
|
-
for (let j = -1; j <= m; ++j) {
|
|
3525
|
-
for (let i = -1; i <= n; ++i) {
|
|
3526
|
-
const y = j * 2;
|
|
3527
|
-
const x = (i + (j & 1) / 2) * 2;
|
|
3528
|
-
arcs.push([
|
|
3529
|
-
[
|
|
3530
|
-
x,
|
|
3531
|
-
y - 1
|
|
3532
|
-
],
|
|
3533
|
-
[
|
|
3534
|
-
1,
|
|
3535
|
-
1
|
|
3536
|
-
]
|
|
3537
|
-
], [
|
|
3538
|
-
[
|
|
3539
|
-
x + 1,
|
|
3540
|
-
y
|
|
3541
|
-
],
|
|
3542
|
-
[
|
|
3543
|
-
0,
|
|
3544
|
-
1
|
|
3545
|
-
]
|
|
3546
|
-
], [
|
|
3547
|
-
[
|
|
3548
|
-
x + 1,
|
|
3549
|
-
y + 1
|
|
3550
|
-
],
|
|
3551
|
-
[
|
|
3552
|
-
-1,
|
|
3553
|
-
1
|
|
3554
|
-
]
|
|
3555
|
-
]);
|
|
3556
|
-
}
|
|
3557
|
-
}
|
|
3558
|
-
for (let j = 0, q = 3; j < m; ++j, q += 6) {
|
|
3559
|
-
for (let i = 0; i < n; ++i, q += 3) {
|
|
3560
|
-
geometries.push({
|
|
3561
|
-
type: "Polygon",
|
|
3562
|
-
arcs: [
|
|
3563
|
-
[
|
|
3564
|
-
q,
|
|
3565
|
-
q + 1,
|
|
3566
|
-
q + 2,
|
|
3567
|
-
~(q + (n + 2 - (j & 1)) * 3),
|
|
3568
|
-
~(q - 2),
|
|
3569
|
-
~(q - (n + 2 + (j & 1)) * 3 + 2)
|
|
3570
|
-
]
|
|
3571
|
-
],
|
|
3572
|
-
properties: {
|
|
3573
|
-
fill: Math.random() > i / n * (1 / value)
|
|
3574
|
-
}
|
|
3575
|
-
});
|
|
3576
|
-
}
|
|
3577
|
-
}
|
|
3578
|
-
const hexagons = {
|
|
3579
|
-
type: "GeometryCollection",
|
|
3580
|
-
geometries
|
|
3581
|
-
};
|
|
3582
|
-
return {
|
|
3583
|
-
type: "Topology",
|
|
3584
|
-
transform: {
|
|
3585
|
-
translate: [
|
|
3586
|
-
0,
|
|
3587
|
-
0
|
|
3588
|
-
],
|
|
3589
|
-
scale: [
|
|
3590
|
-
1,
|
|
3591
|
-
1
|
|
3592
|
-
]
|
|
3593
|
-
},
|
|
3594
|
-
objects: {
|
|
3595
|
-
hexagons
|
|
3596
|
-
},
|
|
3597
|
-
arcs
|
|
3598
|
-
};
|
|
3599
|
-
};
|
|
3600
|
-
var hexProjection = (radius) => {
|
|
3601
|
-
const dx = radius * 2 * Math.sin(Math.PI / 3);
|
|
3602
|
-
const dy = radius * 1.5;
|
|
3603
|
-
return {
|
|
3604
|
-
stream: (stream) => ({
|
|
3605
|
-
point: (x, y) => {
|
|
3606
|
-
stream.point(x * dx / 2, (y - (2 - (y & 1)) / 3) * dy / 2);
|
|
3607
|
-
},
|
|
3608
|
-
lineStart: () => {
|
|
3609
|
-
stream.lineStart();
|
|
3610
|
-
},
|
|
3611
|
-
lineEnd: () => {
|
|
3612
|
-
stream.lineEnd();
|
|
3613
|
-
},
|
|
3614
|
-
polygonStart: () => {
|
|
3615
|
-
stream.polygonStart();
|
|
3616
|
-
},
|
|
3617
|
-
polygonEnd: () => {
|
|
3618
|
-
stream.polygonEnd();
|
|
3619
|
-
}
|
|
3620
|
-
})
|
|
3621
|
-
};
|
|
3622
|
-
};
|
|
3623
|
-
|
|
3624
|
-
// src/components/SVG/FPS.tsx
|
|
3625
|
-
import React2, { useEffect as useEffect5, useState } from "react";
|
|
3626
|
-
import { mx as mx2 } from "@dxos/ui-theme";
|
|
3627
|
-
var PADDING = 8;
|
|
3628
|
-
var FPS = ({ classNames, corner = "top-right", margin = PADDING }) => {
|
|
3629
|
-
const context = useSvgContext();
|
|
3630
|
-
const [size, setSize] = useState(context.size);
|
|
3631
|
-
const [fps, setFps] = useState(null);
|
|
3632
|
-
useEffect5(() => {
|
|
3633
|
-
setSize(context.size);
|
|
3634
|
-
return context.resized.on((ctx) => setSize(ctx.size));
|
|
3635
|
-
}, [
|
|
3636
|
-
context
|
|
3637
|
-
]);
|
|
3638
|
-
useEffect5(() => {
|
|
3639
|
-
let frames = 0;
|
|
3640
|
-
let windowStart = performance.now();
|
|
3641
|
-
let rafId = 0;
|
|
3642
|
-
const loop = (now) => {
|
|
3643
|
-
frames++;
|
|
3644
|
-
const elapsed = now - windowStart;
|
|
3645
|
-
if (elapsed >= 1e3) {
|
|
3646
|
-
setFps(frames * 1e3 / elapsed);
|
|
3647
|
-
frames = 0;
|
|
3648
|
-
windowStart = now;
|
|
3649
|
-
}
|
|
3650
|
-
rafId = requestAnimationFrame(loop);
|
|
3651
|
-
};
|
|
3652
|
-
rafId = requestAnimationFrame(loop);
|
|
3653
|
-
return () => cancelAnimationFrame(rafId);
|
|
3654
|
-
}, []);
|
|
3655
|
-
if (!size) {
|
|
3656
|
-
return null;
|
|
3657
|
-
}
|
|
3658
|
-
const halfW = size.width / 2;
|
|
3659
|
-
const halfH = size.height / 2;
|
|
3660
|
-
const left = corner === "top-left" || corner === "bottom-left";
|
|
3661
|
-
const top = corner === "top-left" || corner === "top-right";
|
|
3662
|
-
const x = left ? -halfW + margin : halfW - margin;
|
|
3663
|
-
const y = top ? -halfH + margin : halfH - margin;
|
|
3664
|
-
const textAnchor = left ? "start" : "end";
|
|
3665
|
-
const dominantBaseline = top ? "hanging" : "alphabetic";
|
|
3666
|
-
return /* @__PURE__ */ React2.createElement("g", {
|
|
3667
|
-
className: "dx-fps",
|
|
3668
|
-
style: {
|
|
3669
|
-
pointerEvents: "none"
|
|
3670
|
-
}
|
|
3671
|
-
}, /* @__PURE__ */ React2.createElement("text", {
|
|
3672
|
-
x,
|
|
3673
|
-
y,
|
|
3674
|
-
textAnchor,
|
|
3675
|
-
dominantBaseline,
|
|
3676
|
-
className: mx2("font-mono text-xs fill-neutral-500 tabular-nums", classNames)
|
|
3677
|
-
}, fps == null ? "\u2014 fps" : `${fps.toFixed(0)} fps`));
|
|
3678
|
-
};
|
|
3679
|
-
|
|
3680
|
-
// src/components/SVG/Grid.tsx
|
|
3681
|
-
import React3 from "react";
|
|
3682
|
-
import { mx as mx3 } from "@dxos/ui-theme";
|
|
3683
|
-
var Grid = ({ classNames, ...props }) => {
|
|
3684
|
-
const { ref } = useGrid(props);
|
|
3685
|
-
return /* @__PURE__ */ React3.createElement("g", {
|
|
3686
|
-
ref,
|
|
3687
|
-
className: mx3("dx-grid", classNames)
|
|
3688
|
-
});
|
|
3689
|
-
};
|
|
3690
|
-
|
|
3691
|
-
// src/components/SVG/Markers.tsx
|
|
3692
|
-
import { select as select8 } from "d3";
|
|
3693
|
-
import React4, { useEffect as useEffect6, useRef as useRef4 } from "react";
|
|
3694
|
-
var Markers = ({ arrowSize, className }) => {
|
|
3695
|
-
const ref = useRef4(null);
|
|
3696
|
-
useEffect6(() => {
|
|
3697
|
-
select8(ref.current).call(createMarkers({
|
|
3698
|
-
arrowSize
|
|
3699
|
-
}));
|
|
3700
|
-
}, [
|
|
3701
|
-
ref
|
|
3702
|
-
]);
|
|
3703
|
-
return /* @__PURE__ */ React4.createElement("defs", {
|
|
3704
|
-
ref,
|
|
3705
|
-
className
|
|
3706
|
-
});
|
|
3707
|
-
};
|
|
3708
|
-
|
|
3709
|
-
// src/components/SVG/Root.tsx
|
|
3710
|
-
import { select as select9 } from "d3";
|
|
3711
|
-
import React5, { forwardRef as forwardRef2, useEffect as useEffect7, useImperativeHandle as useImperativeHandle2, useMemo as useMemo4, useState as useState2 } from "react";
|
|
3712
|
-
import { composableProps } from "@dxos/react-ui";
|
|
3713
|
-
import { mx as mx4 } from "@dxos/ui-theme";
|
|
3714
|
-
var Root = /* @__PURE__ */ forwardRef2(({ children, scale, centered, ...props }, ref) => {
|
|
3715
|
-
const [container, setContainer] = useState2(null);
|
|
3716
|
-
const [size, setSize] = useState2({
|
|
3717
|
-
width: 0,
|
|
3718
|
-
height: 0
|
|
3719
|
-
});
|
|
3720
|
-
useEffect7(() => {
|
|
3721
|
-
if (!container) {
|
|
3722
|
-
return;
|
|
3723
|
-
}
|
|
3724
|
-
const rect = container.getBoundingClientRect();
|
|
3725
|
-
setSize({
|
|
3726
|
-
width: rect.width,
|
|
3727
|
-
height: rect.height
|
|
3728
|
-
});
|
|
3729
|
-
const observer = new ResizeObserver((entries) => {
|
|
3730
|
-
const entry = entries[0];
|
|
3731
|
-
if (!entry) {
|
|
3732
|
-
return;
|
|
3733
|
-
}
|
|
3734
|
-
const { width: width2, height: height2 } = entry.contentRect;
|
|
3735
|
-
setSize((prev) => prev.width === width2 && prev.height === height2 ? prev : {
|
|
3736
|
-
width: width2,
|
|
3737
|
-
height: height2
|
|
3738
|
-
});
|
|
3739
|
-
});
|
|
3740
|
-
observer.observe(container);
|
|
3741
|
-
return () => observer.disconnect();
|
|
3742
|
-
}, [
|
|
3743
|
-
container
|
|
3744
|
-
]);
|
|
3745
|
-
const { width, height } = size;
|
|
3746
|
-
const context = useMemo4(() => {
|
|
3747
|
-
return new SVGContext({
|
|
3748
|
-
scale,
|
|
3749
|
-
centered
|
|
3750
|
-
});
|
|
3751
|
-
}, [
|
|
3752
|
-
scale,
|
|
3753
|
-
centered
|
|
3754
|
-
]);
|
|
3755
|
-
useImperativeHandle2(ref, () => context, [
|
|
3756
|
-
context
|
|
3757
|
-
]);
|
|
3758
|
-
useEffect7(() => {
|
|
3759
|
-
if (width && height) {
|
|
3760
|
-
context.setSize({
|
|
3761
|
-
width,
|
|
3762
|
-
height
|
|
3763
|
-
});
|
|
3764
|
-
select9(context.svg).attr("display", "block").attr("viewBox", context.viewBox).attr("width", width).attr("height", height);
|
|
3765
|
-
}
|
|
3766
|
-
}, [
|
|
3767
|
-
context,
|
|
3768
|
-
width,
|
|
3769
|
-
height
|
|
3770
|
-
]);
|
|
3771
|
-
const ready = width !== 0 && height !== 0;
|
|
3772
|
-
return /* @__PURE__ */ React5.createElement(SVGContextProvider, {
|
|
3773
|
-
value: context
|
|
3774
|
-
}, /* @__PURE__ */ React5.createElement("div", {
|
|
3775
|
-
...composableProps(props, {
|
|
3776
|
-
classNames: "dx-expander"
|
|
3777
|
-
}),
|
|
3778
|
-
ref: setContainer
|
|
3779
|
-
}, /* @__PURE__ */ React5.createElement("svg", {
|
|
3780
|
-
xmlns: "http://www.w3.org/2000/svg",
|
|
3781
|
-
ref: context.ref,
|
|
3782
|
-
className: mx4("transition-opacity opacity-0 duration-1000", ready && "opacity-100")
|
|
3783
|
-
}, ready && children)));
|
|
3784
|
-
});
|
|
3785
|
-
|
|
3786
|
-
// src/components/SVG/Zoom.tsx
|
|
3787
|
-
import React6, { memo, useMemo as useMemo5 } from "react";
|
|
3788
|
-
var Zoom = /* @__PURE__ */ memo(({ extent, className, children }) => {
|
|
3789
|
-
const options = useMemo5(() => ({
|
|
3790
|
-
extent
|
|
3791
|
-
}), [
|
|
3792
|
-
JSON.stringify(extent)
|
|
3793
|
-
]);
|
|
3794
|
-
const zoom2 = useZoom(options);
|
|
3795
|
-
return /* @__PURE__ */ React6.createElement("g", {
|
|
3796
|
-
ref: zoom2.ref,
|
|
3797
|
-
className
|
|
3798
|
-
}, children);
|
|
3799
|
-
});
|
|
3800
|
-
|
|
3801
|
-
// src/components/SVG/SVG.tsx
|
|
3802
|
-
var SVG = {
|
|
3803
|
-
Root,
|
|
3804
|
-
Grid,
|
|
3805
|
-
Markers,
|
|
3806
|
-
Mesh,
|
|
3807
|
-
Zoom,
|
|
3808
|
-
Graph,
|
|
3809
|
-
FPS
|
|
3810
|
-
};
|
|
3811
|
-
|
|
3812
|
-
// src/components/Tree/Tree.tsx
|
|
3813
|
-
import React10, { useMemo as useMemo9 } from "react";
|
|
3814
|
-
|
|
3815
|
-
// src/components/Tree/layout/HierarchicalEdgeBundling.tsx
|
|
3816
|
-
import { cluster, curveBundle as curveBundle2, hierarchy as hierarchy5, lineRadial as lineRadial2, select as select10 } from "d3";
|
|
3817
|
-
import React7, { useEffect as useEffect9, useMemo as useMemo6, useRef as useRef5 } from "react";
|
|
3818
|
-
import { mx as mx5 } from "@dxos/ui-theme";
|
|
3819
|
-
|
|
3820
|
-
// src/components/Tree/layout/slots.ts
|
|
3821
|
-
var defaultTreeLayoutSlots = {
|
|
3822
|
-
// Cursor + transition so the hover swap reads clearly; SVG circles support the `:hover` pseudo-class
|
|
3823
|
-
// via Tailwind variants exactly like HTML elements.
|
|
3824
|
-
node: "fill-blue-600 hover:fill-orange-500 cursor-pointer transition-colors",
|
|
3825
|
-
// 0.5px is fine on a white background, but on a dark Storybook background the lines disappear.
|
|
3826
|
-
// Use stroke-1 with opacity 50% so they read in both themes; dx-bundle-dim/out/in further tune on hover.
|
|
3827
|
-
path: "fill-none stroke-blue-500/50 stroke-[1px] dark:stroke-blue-400/60",
|
|
3828
|
-
text: "fill-neutral-700 dark:fill-neutral-300 text-xs hover:fill-orange-500 cursor-pointer transition-colors"
|
|
3829
|
-
};
|
|
3830
|
-
|
|
3831
|
-
// src/components/Tree/layout/useContainerSize.ts
|
|
3832
|
-
import { useEffect as useEffect8, useState as useState3 } from "react";
|
|
3833
|
-
var useContainerSize = () => {
|
|
3834
|
-
const [el, setEl] = useState3(null);
|
|
3835
|
-
const [size, setSize] = useState3({
|
|
3836
|
-
width: 0,
|
|
3837
|
-
height: 0
|
|
3838
|
-
});
|
|
3839
|
-
useEffect8(() => {
|
|
3840
|
-
if (!el) {
|
|
3841
|
-
return;
|
|
3842
|
-
}
|
|
3843
|
-
const rect = el.getBoundingClientRect();
|
|
3844
|
-
setSize({
|
|
3845
|
-
width: rect.width,
|
|
3846
|
-
height: rect.height
|
|
3847
|
-
});
|
|
3848
|
-
const observer = new ResizeObserver((entries) => {
|
|
3849
|
-
const entry = entries[0];
|
|
3850
|
-
if (!entry) {
|
|
3851
|
-
return;
|
|
3852
|
-
}
|
|
3853
|
-
const { width, height } = entry.contentRect;
|
|
3854
|
-
setSize((prev) => prev.width === width && prev.height === height ? prev : {
|
|
3855
|
-
width,
|
|
3856
|
-
height
|
|
3857
|
-
});
|
|
3858
|
-
});
|
|
3859
|
-
observer.observe(el);
|
|
3860
|
-
return () => observer.disconnect();
|
|
3861
|
-
}, [
|
|
3862
|
-
el
|
|
3863
|
-
]);
|
|
3864
|
-
return {
|
|
3865
|
-
setRef: setEl,
|
|
3866
|
-
width: size.width,
|
|
3867
|
-
height: size.height
|
|
3868
|
-
};
|
|
3869
|
-
};
|
|
3870
|
-
|
|
3871
|
-
// src/components/Tree/layout/HierarchicalEdgeBundling.tsx
|
|
3872
|
-
var TRANSITION_MS = 350;
|
|
3873
|
-
var HierarchicalEdgeBundling = ({ classNames, data, edges = [], label = (d) => d.label ?? d.id, padding = 120, tension = 0.85, r = 4, slots = defaultTreeLayoutSlots, onNodeHover }) => {
|
|
3874
|
-
const svgRef = useRef5(null);
|
|
3875
|
-
const { setRef, width, height } = useContainerSize();
|
|
3876
|
-
const root = useMemo6(() => buildBundleHierarchy(data, edges), [
|
|
3877
|
-
data,
|
|
3878
|
-
edges
|
|
3879
|
-
]);
|
|
3880
|
-
const handleHoverRef = useRef5(() => {
|
|
3881
|
-
});
|
|
3882
|
-
handleHoverRef.current = (node, event) => onNodeHover?.(node, event);
|
|
3883
|
-
useEffect9(() => {
|
|
3884
|
-
if (!svgRef.current || !width || !height) {
|
|
3885
|
-
return;
|
|
3886
|
-
}
|
|
3887
|
-
const radius = Math.max(0, Math.min(width, height) / 2 - padding);
|
|
3888
|
-
renderBundling(svgRef.current, root, {
|
|
3889
|
-
radius,
|
|
3890
|
-
r,
|
|
3891
|
-
label,
|
|
3892
|
-
slots,
|
|
3893
|
-
tension,
|
|
3894
|
-
onNodeHover: (n, e) => handleHoverRef.current(n, e)
|
|
3895
|
-
});
|
|
3896
|
-
}, [
|
|
3897
|
-
root,
|
|
3898
|
-
width,
|
|
3899
|
-
height,
|
|
3900
|
-
padding,
|
|
3901
|
-
tension,
|
|
3902
|
-
r,
|
|
3903
|
-
label,
|
|
3904
|
-
slots
|
|
3905
|
-
]);
|
|
3906
|
-
return /* @__PURE__ */ React7.createElement("div", {
|
|
3907
|
-
ref: setRef,
|
|
3908
|
-
className: mx5("dx-expander relative", classNames)
|
|
3909
|
-
}, width > 0 && height > 0 && /* @__PURE__ */ React7.createElement("svg", {
|
|
3910
|
-
ref: svgRef,
|
|
3911
|
-
xmlns: "http://www.w3.org/2000/svg",
|
|
3912
|
-
width,
|
|
3913
|
-
height,
|
|
3914
|
-
viewBox: `${-width / 2} ${-height / 2} ${width} ${height}`
|
|
3915
|
-
}));
|
|
3916
|
-
};
|
|
3917
|
-
var buildBundleHierarchy = (data, edges) => {
|
|
3918
|
-
const root = hierarchy5(data);
|
|
3919
|
-
const byId = /* @__PURE__ */ new Map();
|
|
3920
|
-
for (const node of root.descendants()) {
|
|
3921
|
-
byId.set(node.data.id, node);
|
|
3922
|
-
node.outgoing = [];
|
|
3923
|
-
node.incoming = [];
|
|
3924
|
-
}
|
|
3925
|
-
for (const edge of edges) {
|
|
3926
|
-
const source = byId.get(edge.source);
|
|
3927
|
-
const target = byId.get(edge.target);
|
|
3928
|
-
if (!source || !target || source === target) {
|
|
3929
|
-
continue;
|
|
3930
|
-
}
|
|
3931
|
-
source.outgoing.push([
|
|
3932
|
-
source,
|
|
3933
|
-
target,
|
|
3934
|
-
edge
|
|
3935
|
-
]);
|
|
3936
|
-
target.incoming.push([
|
|
3937
|
-
source,
|
|
3938
|
-
target,
|
|
3939
|
-
edge
|
|
3940
|
-
]);
|
|
3941
|
-
}
|
|
3942
|
-
return root;
|
|
3943
|
-
};
|
|
3944
|
-
var renderBundling = (svgElement, root, options) => {
|
|
3945
|
-
const { radius, r, tension, label, slots, onNodeHover } = options;
|
|
3946
|
-
const svg = select10(svgElement);
|
|
3947
|
-
if (!root.children?.length) {
|
|
3948
|
-
svg.selectAll("g.dx-bundle-root").remove();
|
|
3949
|
-
return;
|
|
3950
|
-
}
|
|
3951
|
-
cluster().size([
|
|
3952
|
-
2 * Math.PI,
|
|
3953
|
-
radius
|
|
3954
|
-
])(root);
|
|
3955
|
-
const g = svg.selectAll("g.dx-bundle-root").data([
|
|
3956
|
-
null
|
|
3957
|
-
]).join("g").classed("dx-bundle-root", true);
|
|
3958
|
-
const linksLayer = g.selectAll("g.dx-bundle-links").data([
|
|
3959
|
-
null
|
|
3960
|
-
]).join("g").classed("dx-bundle-links", true);
|
|
3961
|
-
const nodesLayer = g.selectAll("g.dx-bundle-nodes").data([
|
|
3962
|
-
null
|
|
3963
|
-
]).join("g").classed("dx-bundle-nodes", true);
|
|
3964
|
-
const line5 = lineRadial2().curve(curveBundle2.beta(tension)).radius((d) => d.y).angle((d) => d.x);
|
|
3965
|
-
const leaves = root.leaves();
|
|
3966
|
-
const flatEdges = leaves.flatMap((leaf) => leaf.outgoing ?? []);
|
|
3967
|
-
const paths = linksLayer.selectAll("path").data(flatEdges, (d) => `${d[0].data.id}->${d[1].data.id}`).join((enter) => enter.append("path").attr("class", slots.path ?? "").attr("fill", "none").attr("opacity", 0), (update) => update, (exit) => exit.each(function() {
|
|
3968
|
-
select10(this).interrupt();
|
|
3969
|
-
}).transition().duration(TRANSITION_MS).attr("opacity", 0).remove());
|
|
3970
|
-
paths.each(function(d) {
|
|
3971
|
-
d[0].pathEl = this;
|
|
3972
|
-
}).transition().duration(TRANSITION_MS).attr("opacity", 1).attr("d", ([s, t]) => line5(s.path(t)));
|
|
3973
|
-
const labels = nodesLayer.selectAll("g.dx-bundle-leaf").data(leaves, (d) => d.data.id).join((enter) => {
|
|
3974
|
-
const ge = enter.append("g").classed("dx-bundle-leaf", true).attr("opacity", 0);
|
|
3975
|
-
ge.append("circle").style("cursor", "pointer");
|
|
3976
|
-
ge.append("text").attr("dy", "0.32em").attr("paint-order", "stroke").style("cursor", "pointer");
|
|
3977
|
-
return ge;
|
|
3978
|
-
}, (update) => update, (exit) => exit.each(function() {
|
|
3979
|
-
select10(this).interrupt();
|
|
3980
|
-
}).transition().duration(TRANSITION_MS).attr("opacity", 0).remove());
|
|
3981
|
-
labels.transition().duration(TRANSITION_MS).attr("opacity", 1).attr("transform", (d) => `rotate(${d.x * 180 / Math.PI - 90}) translate(${d.y},0)`);
|
|
3982
|
-
const onEnter = function(event, d) {
|
|
3983
|
-
onNodeHover(d.data, event);
|
|
3984
|
-
hover(linksLayer, leaves, d, true);
|
|
3985
|
-
};
|
|
3986
|
-
const onLeave = function(event, d) {
|
|
3987
|
-
onNodeHover(null);
|
|
3988
|
-
hover(linksLayer, leaves, d, false);
|
|
3989
|
-
};
|
|
3990
|
-
labels.select("circle").attr("class", [
|
|
3991
|
-
slots.node ?? "",
|
|
3992
|
-
"dx-leaf"
|
|
3993
|
-
].filter(Boolean).join(" ")).attr("r", r).style("fill", (d) => slots.nodeFill?.(d.data.data) ?? null).each(function(d) {
|
|
3994
|
-
d.circle = this;
|
|
3995
|
-
}).on("pointerenter", onEnter).on("pointerleave", onLeave);
|
|
3996
|
-
labels.select("text").attr("class", slots.text ?? "").attr("x", (d) => d.x < Math.PI ? r + 4 : -(r + 4)).attr("text-anchor", (d) => d.x < Math.PI ? "start" : "end").attr("transform", (d) => d.x >= Math.PI ? "rotate(180)" : null).each(function(d) {
|
|
3997
|
-
d.text = this;
|
|
3998
|
-
}).text((d) => label(d.data)).on("pointerenter", onEnter).on("pointerleave", onLeave);
|
|
3999
|
-
};
|
|
4000
|
-
var hover = (linksLayer, leaves, focused, on) => {
|
|
4001
|
-
const outgoing = new Set((focused.outgoing ?? []).map(([, t]) => t));
|
|
4002
|
-
const incoming = new Set((focused.incoming ?? []).map(([s]) => s));
|
|
4003
|
-
linksLayer.selectAll("path").style("stroke", (d) => {
|
|
4004
|
-
if (!on) {
|
|
4005
|
-
return null;
|
|
4006
|
-
}
|
|
4007
|
-
if (d[0] === focused) {
|
|
4008
|
-
return "var(--color-orange-500)";
|
|
4009
|
-
}
|
|
4010
|
-
if (d[1] === focused) {
|
|
4011
|
-
return "var(--color-sky-500)";
|
|
4012
|
-
}
|
|
4013
|
-
return null;
|
|
4014
|
-
}).style("stroke-width", (d) => on && (d[0] === focused || d[1] === focused) ? "1.5px" : null).style("opacity", (d) => on && d[0] !== focused && d[1] !== focused ? "0.08" : null);
|
|
4015
|
-
for (const leaf of leaves) {
|
|
4016
|
-
const isOut = outgoing.has(leaf);
|
|
4017
|
-
const isIn = incoming.has(leaf);
|
|
4018
|
-
const isConnected = isOut || isIn;
|
|
4019
|
-
if (leaf.text) {
|
|
4020
|
-
select10(leaf.text).style("fill", () => {
|
|
4021
|
-
if (!on) {
|
|
4022
|
-
return null;
|
|
4023
|
-
}
|
|
4024
|
-
if (leaf === focused) {
|
|
4025
|
-
return "var(--color-neutral-900)";
|
|
4026
|
-
}
|
|
4027
|
-
if (isOut) {
|
|
4028
|
-
return "var(--color-orange-500)";
|
|
4029
|
-
}
|
|
4030
|
-
if (isIn) {
|
|
4031
|
-
return "var(--color-sky-500)";
|
|
4032
|
-
}
|
|
4033
|
-
return null;
|
|
4034
|
-
}).style("font-weight", () => on && leaf === focused ? "600" : null);
|
|
4035
|
-
}
|
|
4036
|
-
if (leaf.circle) {
|
|
4037
|
-
select10(leaf.circle).style("stroke", () => on && isConnected ? "var(--color-orange-400)" : null).style("stroke-width", () => on && isConnected ? "2.5px" : null).style("opacity", () => on && !isConnected && leaf !== focused ? "0.15" : null);
|
|
4038
|
-
}
|
|
4039
|
-
}
|
|
4040
|
-
};
|
|
4041
|
-
|
|
4042
|
-
// src/components/Tree/layout/RadialTree.tsx
|
|
4043
|
-
import { cluster as d3Cluster4, tree as d3Tree2, linkRadial as linkRadial3, select as select11 } from "d3";
|
|
4044
|
-
import React8, { useCallback, useEffect as useEffect10, useMemo as useMemo7, useRef as useRef6, useState as useState4 } from "react";
|
|
4045
|
-
import { mx as mx6 } from "@dxos/ui-theme";
|
|
4046
|
-
|
|
4047
|
-
// src/components/Tree/layout/hierarchy.ts
|
|
4048
|
-
import { hierarchy as d3Hierarchy } from "d3";
|
|
4049
|
-
var buildHierarchy = (data, collapsed = /* @__PURE__ */ new Set()) => {
|
|
4050
|
-
return d3Hierarchy(data, (d) => {
|
|
4051
|
-
if (!d.children?.length) {
|
|
4052
|
-
return void 0;
|
|
4053
|
-
}
|
|
4054
|
-
return collapsed.has(d.id) ? void 0 : d.children;
|
|
4055
|
-
});
|
|
4056
|
-
};
|
|
4057
|
-
var isCollapsed = (data, collapsed) => Boolean(data.children?.length) && collapsed.has(data.id);
|
|
4058
|
-
var isLeaf = (data) => !data.children?.length;
|
|
4059
|
-
|
|
4060
|
-
// src/components/Tree/layout/RadialTree.tsx
|
|
4061
|
-
var TRANSITION_MS2 = 350;
|
|
4062
|
-
var RadialTree = ({ classNames, data, label = (d) => d.label ?? d.id, slots = defaultTreeLayoutSlots, r = 4, padding = 80, initialCollapsed, cluster: cluster2 = false, onNodeClick, onNodeHover }) => {
|
|
4063
|
-
const svgRef = useRef6(null);
|
|
4064
|
-
const { setRef, width, height } = useContainerSize();
|
|
4065
|
-
const [collapsed, setCollapsed] = useState4(() => new Set(initialCollapsed ?? []));
|
|
4066
|
-
const toggle = useCallback((id) => {
|
|
4067
|
-
setCollapsed((prev) => {
|
|
4068
|
-
const next = new Set(prev);
|
|
4069
|
-
if (next.has(id)) {
|
|
4070
|
-
next.delete(id);
|
|
4071
|
-
} else {
|
|
4072
|
-
next.add(id);
|
|
4073
|
-
}
|
|
4074
|
-
return next;
|
|
4075
|
-
});
|
|
4076
|
-
}, []);
|
|
4077
|
-
const handleClickRef = useRef6(() => {
|
|
4078
|
-
});
|
|
4079
|
-
handleClickRef.current = (node) => {
|
|
4080
|
-
onNodeClick?.(node);
|
|
4081
|
-
if (node.children?.length) {
|
|
4082
|
-
toggle(node.id);
|
|
4083
|
-
}
|
|
4084
|
-
};
|
|
4085
|
-
const handleHoverRef = useRef6(() => {
|
|
4086
|
-
});
|
|
4087
|
-
handleHoverRef.current = (node, event) => onNodeHover?.(node, event);
|
|
4088
|
-
const root = useMemo7(() => buildHierarchy(data, collapsed), [
|
|
4089
|
-
data,
|
|
4090
|
-
collapsed
|
|
4091
|
-
]);
|
|
4092
|
-
useEffect10(() => {
|
|
4093
|
-
if (!svgRef.current || !width || !height) {
|
|
4094
|
-
return;
|
|
4095
|
-
}
|
|
4096
|
-
const radius = Math.max(0, Math.min(width, height) / 2 - padding);
|
|
4097
|
-
renderRadialTree(svgRef.current, root, {
|
|
4098
|
-
radius,
|
|
4099
|
-
r,
|
|
4100
|
-
label,
|
|
4101
|
-
slots,
|
|
4102
|
-
collapsed,
|
|
4103
|
-
cluster: cluster2,
|
|
4104
|
-
onNodeClick: (n) => handleClickRef.current(n),
|
|
4105
|
-
onNodeHover: (n, e) => handleHoverRef.current(n, e)
|
|
4106
|
-
});
|
|
4107
|
-
}, [
|
|
4108
|
-
root,
|
|
4109
|
-
width,
|
|
4110
|
-
height,
|
|
4111
|
-
r,
|
|
4112
|
-
padding,
|
|
4113
|
-
label,
|
|
4114
|
-
slots,
|
|
4115
|
-
collapsed,
|
|
4116
|
-
cluster2
|
|
4117
|
-
]);
|
|
4118
|
-
return /* @__PURE__ */ React8.createElement("div", {
|
|
4119
|
-
ref: setRef,
|
|
4120
|
-
className: mx6("dx-expander relative", classNames)
|
|
4121
|
-
}, width > 0 && height > 0 && /* @__PURE__ */ React8.createElement("svg", {
|
|
4122
|
-
ref: svgRef,
|
|
4123
|
-
xmlns: "http://www.w3.org/2000/svg",
|
|
4124
|
-
width,
|
|
4125
|
-
height,
|
|
4126
|
-
viewBox: `${-width / 2} ${-height / 2} ${width} ${height}`
|
|
4127
|
-
}));
|
|
4128
|
-
};
|
|
4129
|
-
var renderRadialTree = (svgElement, root, options) => {
|
|
4130
|
-
const { radius, r, label, slots, collapsed, cluster: cluster2, onNodeClick, onNodeHover } = options;
|
|
4131
|
-
const svg = select11(svgElement);
|
|
4132
|
-
const layout = cluster2 ? d3Cluster4() : d3Tree2();
|
|
4133
|
-
layout.size([
|
|
4134
|
-
2 * Math.PI,
|
|
4135
|
-
radius
|
|
4136
|
-
]).separation((a, b) => (a.parent === b.parent ? 1 : 2) / Math.max(1, a.depth))(root);
|
|
4137
|
-
const g = svg.selectAll("g.dx-radial-root").data([
|
|
4138
|
-
null
|
|
4139
|
-
]).join("g").classed("dx-radial-root", true);
|
|
4140
|
-
const linksLayer = g.selectAll("g.dx-radial-links").data([
|
|
4141
|
-
null
|
|
4142
|
-
]).join("g").classed("dx-radial-links", true);
|
|
4143
|
-
const nodesLayer = g.selectAll("g.dx-radial-nodes").data([
|
|
4144
|
-
null
|
|
4145
|
-
]).join("g").classed("dx-radial-nodes", true);
|
|
4146
|
-
const linkPath4 = linkRadial3().angle((d) => d.x).radius((d) => d.y);
|
|
4147
|
-
linksLayer.selectAll("path").data(root.links(), (d) => `${d.source.data.id}->${d.target.data.id}`).join((enter) => enter.append("path").attr("class", slots.path ?? "").attr("fill", "none").attr("opacity", 0), (update) => update, (exit) => exit.transition().duration(TRANSITION_MS2).attr("opacity", 0).remove()).transition().duration(TRANSITION_MS2).attr("opacity", 1).attr("d", linkPath4);
|
|
4148
|
-
const node = nodesLayer.selectAll("g.dx-radial-node").data(root.descendants(), (d) => d.data.id);
|
|
4149
|
-
const nodeEnter = node.enter().append("g").classed("dx-radial-node", true).attr("opacity", 0).attr("transform", (d) => `rotate(${d.x * 180 / Math.PI - 90}) translate(${d.y},0)`).style("cursor", (d) => d.data.children?.length ? "pointer" : "default").on("click", (_, d) => onNodeClick(d.data));
|
|
4150
|
-
nodeEnter.append("circle").attr("r", r).on("pointerenter", (event, d) => onNodeHover(d.data, event)).on("pointerleave", (event) => onNodeHover(null, event));
|
|
4151
|
-
nodeEnter.append("text").attr("dy", "0.32em").attr("paint-order", "stroke").text((d) => label(d.data));
|
|
4152
|
-
const nodeMerge = nodeEnter.merge(node);
|
|
4153
|
-
nodeMerge.transition().duration(TRANSITION_MS2).attr("opacity", 1).attr("transform", (d) => `rotate(${d.x * 180 / Math.PI - 90}) translate(${d.y},0)`);
|
|
4154
|
-
nodeMerge.select("circle").attr("class", (d) => {
|
|
4155
|
-
const collapsedHere = isCollapsed(d.data, collapsed);
|
|
4156
|
-
const leaf = isLeaf(d.data);
|
|
4157
|
-
return [
|
|
4158
|
-
slots.node ?? "",
|
|
4159
|
-
collapsedHere ? "dx-collapsed" : leaf ? "dx-leaf" : "dx-branch"
|
|
4160
|
-
].filter(Boolean).join(" ");
|
|
4161
|
-
}).attr("r", r).style("fill", (d) => isLeaf(d.data) ? slots.nodeFill?.(d.data.data) ?? null : null);
|
|
4162
|
-
nodeMerge.select("text").attr("class", slots.text ?? "").attr("transform", (d) => d.x >= Math.PI ? "rotate(180)" : null).attr("x", (d) => d.x < Math.PI === !d.children ? r + 4 : -(r + 4)).attr("text-anchor", (d) => d.x < Math.PI === !d.children ? "start" : "end").text((d) => label(d.data));
|
|
4163
|
-
node.exit().transition().duration(TRANSITION_MS2).attr("opacity", 0).remove();
|
|
4164
|
-
};
|
|
4165
|
-
|
|
4166
|
-
// src/components/Tree/layout/TidyTree.tsx
|
|
4167
|
-
import { curveBumpX, link as d3Link, tree as d3Tree3, select as select12 } from "d3";
|
|
4168
|
-
import React9, { useCallback as useCallback2, useEffect as useEffect11, useMemo as useMemo8, useRef as useRef7, useState as useState5 } from "react";
|
|
4169
|
-
import { mx as mx7 } from "@dxos/ui-theme";
|
|
4170
|
-
var TRANSITION_MS3 = 350;
|
|
4171
|
-
var TidyTree = ({ classNames, data, label = (d) => d.label ?? d.id, slots = defaultTreeLayoutSlots, r = 4, margin = 24, initialCollapsed, onNodeClick }) => {
|
|
4172
|
-
const svgRef = useRef7(null);
|
|
4173
|
-
const { setRef, width, height } = useContainerSize();
|
|
4174
|
-
const [collapsed, setCollapsed] = useState5(() => new Set(initialCollapsed ?? []));
|
|
4175
|
-
const toggle = useCallback2((id) => {
|
|
4176
|
-
setCollapsed((prev) => {
|
|
4177
|
-
const next = new Set(prev);
|
|
4178
|
-
if (next.has(id)) {
|
|
4179
|
-
next.delete(id);
|
|
4180
|
-
} else {
|
|
4181
|
-
next.add(id);
|
|
4182
|
-
}
|
|
4183
|
-
return next;
|
|
4184
|
-
});
|
|
4185
|
-
}, []);
|
|
4186
|
-
const handleClickRef = useRef7(() => {
|
|
4187
|
-
});
|
|
4188
|
-
handleClickRef.current = (node) => {
|
|
4189
|
-
onNodeClick?.(node);
|
|
4190
|
-
if (node.children?.length) {
|
|
4191
|
-
toggle(node.id);
|
|
4192
|
-
}
|
|
4193
|
-
};
|
|
4194
|
-
const root = useMemo8(() => buildHierarchy(data, collapsed), [
|
|
4195
|
-
data,
|
|
4196
|
-
collapsed
|
|
4197
|
-
]);
|
|
4198
|
-
useEffect11(() => {
|
|
4199
|
-
if (!svgRef.current || !width || !height) {
|
|
4200
|
-
return;
|
|
4201
|
-
}
|
|
4202
|
-
renderTidyTree(svgRef.current, root, {
|
|
4203
|
-
width,
|
|
4204
|
-
height,
|
|
4205
|
-
r,
|
|
4206
|
-
margin,
|
|
4207
|
-
label,
|
|
4208
|
-
slots,
|
|
4209
|
-
collapsed,
|
|
4210
|
-
onNodeClick: (n) => handleClickRef.current(n)
|
|
4211
|
-
});
|
|
4212
|
-
}, [
|
|
4213
|
-
root,
|
|
4214
|
-
width,
|
|
4215
|
-
height,
|
|
4216
|
-
r,
|
|
4217
|
-
margin,
|
|
4218
|
-
label,
|
|
4219
|
-
slots,
|
|
4220
|
-
collapsed
|
|
4221
|
-
]);
|
|
4222
|
-
return /* @__PURE__ */ React9.createElement("div", {
|
|
4223
|
-
ref: setRef,
|
|
4224
|
-
className: mx7("dx-expander relative", classNames)
|
|
4225
|
-
}, width > 0 && height > 0 && /* @__PURE__ */ React9.createElement("svg", {
|
|
4226
|
-
ref: svgRef,
|
|
4227
|
-
xmlns: "http://www.w3.org/2000/svg",
|
|
4228
|
-
width,
|
|
4229
|
-
height,
|
|
4230
|
-
viewBox: `${-width / 2} ${-height / 2} ${width} ${height}`
|
|
4231
|
-
}));
|
|
4232
|
-
};
|
|
4233
|
-
var renderTidyTree = (svgElement, root, options) => {
|
|
4234
|
-
const { width, height, r, margin, label, slots, collapsed, onNodeClick } = options;
|
|
4235
|
-
const svg = select12(svgElement);
|
|
4236
|
-
const dx = 18;
|
|
4237
|
-
const dy = Math.max(60, (width - margin * 2) / Math.max(1, root.height + 1));
|
|
4238
|
-
d3Tree3().nodeSize([
|
|
4239
|
-
dx,
|
|
4240
|
-
dy
|
|
4241
|
-
])(root);
|
|
4242
|
-
let x0 = Infinity;
|
|
4243
|
-
let x1 = -x0;
|
|
4244
|
-
root.each((d) => {
|
|
4245
|
-
if (d.x > x1) {
|
|
4246
|
-
x1 = d.x;
|
|
4247
|
-
}
|
|
4248
|
-
if (d.x < x0) {
|
|
4249
|
-
x0 = d.x;
|
|
4250
|
-
}
|
|
4251
|
-
});
|
|
4252
|
-
const treeWidth = width - margin * 2;
|
|
4253
|
-
const treeHeight = x1 - x0;
|
|
4254
|
-
const scaleY = treeHeight > 0 ? Math.min(1, (height - margin * 2) / treeHeight) : 1;
|
|
4255
|
-
const offsetX = -treeWidth / 2;
|
|
4256
|
-
const offsetY = -(x0 + x1) / 2;
|
|
4257
|
-
const g = svg.selectAll("g.dx-tidy-root").data([
|
|
4258
|
-
null
|
|
4259
|
-
]).join("g").classed("dx-tidy-root", true);
|
|
4260
|
-
const linksLayer = g.selectAll("g.dx-tidy-links").data([
|
|
4261
|
-
null
|
|
4262
|
-
]).join("g").classed("dx-tidy-links", true);
|
|
4263
|
-
const nodesLayer = g.selectAll("g.dx-tidy-nodes").data([
|
|
4264
|
-
null
|
|
4265
|
-
]).join("g").classed("dx-tidy-nodes", true);
|
|
4266
|
-
const linkPath4 = d3Link(curveBumpX).x((d) => offsetX + d.y).y((d) => (d.x + offsetY) * scaleY);
|
|
4267
|
-
linksLayer.selectAll("path").data(root.links(), (d) => `${d.source.data.id}->${d.target.data.id}`).join((enter) => enter.append("path").attr("class", slots.path ?? "").attr("fill", "none").attr("opacity", 0), (update) => update, (exit) => exit.transition().duration(TRANSITION_MS3).attr("opacity", 0).remove()).transition().duration(TRANSITION_MS3).attr("opacity", 1).attr("d", linkPath4);
|
|
4268
|
-
const node = nodesLayer.selectAll("g.dx-tidy-node").data(root.descendants(), (d) => d.data.id);
|
|
4269
|
-
const nodeEnter = node.enter().append("g").classed("dx-tidy-node", true).attr("transform", (d) => `translate(${offsetX + d.y},${(d.x + offsetY) * scaleY})`).attr("opacity", 0).style("cursor", (d) => d.data.children?.length ? "pointer" : "default").on("click", (_, d) => onNodeClick(d.data));
|
|
4270
|
-
nodeEnter.append("circle").attr("r", r);
|
|
4271
|
-
nodeEnter.append("text").attr("dy", "0.32em").attr("x", (d) => d.children ? -(r + 4) : r + 4).attr("text-anchor", (d) => d.children ? "end" : "start").text((d) => label(d.data));
|
|
4272
|
-
const nodeMerge = nodeEnter.merge(node);
|
|
4273
|
-
nodeMerge.transition().duration(TRANSITION_MS3).attr("opacity", 1).attr("transform", (d) => `translate(${offsetX + d.y},${(d.x + offsetY) * scaleY})`);
|
|
4274
|
-
nodeMerge.select("circle").attr("class", (d) => {
|
|
4275
|
-
const collapsedHere = isCollapsed(d.data, collapsed);
|
|
4276
|
-
const leaf = isLeaf(d.data);
|
|
4277
|
-
return [
|
|
4278
|
-
slots.node ?? "",
|
|
4279
|
-
collapsedHere ? "dx-collapsed" : leaf ? "dx-leaf" : "dx-branch"
|
|
4280
|
-
].filter(Boolean).join(" ");
|
|
4281
|
-
}).attr("r", r);
|
|
4282
|
-
nodeMerge.select("text").attr("class", slots.text ?? "").attr("x", (d) => d.children ? -(r + 4) : r + 4).attr("text-anchor", (d) => d.children ? "end" : "start").text((d) => label(d.data));
|
|
4283
|
-
node.exit().each(function() {
|
|
4284
|
-
select12(this).interrupt();
|
|
4285
|
-
}).transition().duration(TRANSITION_MS3).attr("opacity", 0).remove();
|
|
4286
|
-
};
|
|
4287
|
-
|
|
4288
|
-
// src/components/Tree/Tree.tsx
|
|
4289
|
-
var Tree = ({ classNames, data, edges, variant = "tidy", label, slots, margin, initialCollapsed, onNodeClick, onNodeHover }) => {
|
|
4290
|
-
return useMemo9(() => {
|
|
4291
|
-
switch (variant) {
|
|
4292
|
-
case "tidy":
|
|
4293
|
-
return /* @__PURE__ */ React10.createElement(TidyTree, {
|
|
4294
|
-
classNames,
|
|
4295
|
-
data,
|
|
4296
|
-
label,
|
|
4297
|
-
slots,
|
|
4298
|
-
margin,
|
|
4299
|
-
initialCollapsed,
|
|
4300
|
-
onNodeClick
|
|
4301
|
-
});
|
|
4302
|
-
case "radial":
|
|
4303
|
-
return /* @__PURE__ */ React10.createElement(RadialTree, {
|
|
4304
|
-
classNames,
|
|
4305
|
-
data,
|
|
4306
|
-
label,
|
|
4307
|
-
slots,
|
|
4308
|
-
initialCollapsed,
|
|
4309
|
-
onNodeClick,
|
|
4310
|
-
onNodeHover
|
|
4311
|
-
});
|
|
4312
|
-
case "edge":
|
|
4313
|
-
return /* @__PURE__ */ React10.createElement(HierarchicalEdgeBundling, {
|
|
4314
|
-
classNames,
|
|
4315
|
-
data,
|
|
4316
|
-
edges: edges ?? [],
|
|
4317
|
-
label,
|
|
4318
|
-
slots,
|
|
4319
|
-
onNodeHover
|
|
4320
|
-
});
|
|
4321
|
-
}
|
|
4322
|
-
}, [
|
|
4323
|
-
variant,
|
|
4324
|
-
classNames,
|
|
4325
|
-
data,
|
|
4326
|
-
edges,
|
|
4327
|
-
label,
|
|
4328
|
-
slots,
|
|
4329
|
-
initialCollapsed,
|
|
4330
|
-
onNodeClick,
|
|
4331
|
-
onNodeHover
|
|
4332
|
-
]);
|
|
4333
|
-
};
|
|
4334
|
-
|
|
4335
|
-
// src/fx/pulsar.ts
|
|
4336
|
-
import { easeCubicOut as easeCubicOut2, select as select13 } from "d3";
|
|
4337
|
-
var PROP_PULSAR_ACTIVE = "dx-pulsar-active";
|
|
4338
|
-
(function(Pulsar2) {
|
|
4339
|
-
Pulsar2.has = (group) => group.select("circle.dx-pulsar").empty();
|
|
4340
|
-
Pulsar2.create = (group, r, options = {}) => {
|
|
4341
|
-
const pulsar = group.append("circle").classed("dx-pulsar", true).classed("stroke-2 stroke-red-500 fill-none", true).property(PROP_PULSAR_ACTIVE, true);
|
|
4342
|
-
startAnimation(pulsar, r, options);
|
|
4343
|
-
};
|
|
4344
|
-
Pulsar2.remove = (group) => {
|
|
4345
|
-
group.select("circle.dx-pulsar").property(PROP_PULSAR_ACTIVE, false);
|
|
4346
|
-
};
|
|
4347
|
-
})(Pulsar || (Pulsar = {}));
|
|
4348
|
-
var startAnimation = (pulsar, r, options = {}) => {
|
|
4349
|
-
const { period = 1e3, duration = 800, ratio = 3 } = options;
|
|
4350
|
-
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() {
|
|
4351
|
-
if (select13(this).property(PROP_PULSAR_ACTIVE)) {
|
|
4352
|
-
startAnimation(select13(this), r, options);
|
|
4353
|
-
} else {
|
|
4354
|
-
select13(this).remove();
|
|
4355
|
-
}
|
|
4356
|
-
});
|
|
4357
|
-
};
|
|
4358
|
-
var Pulsar;
|
|
4359
|
-
export {
|
|
4360
|
-
CLUSTER_NODE_TYPE_GROUP,
|
|
4361
|
-
CLUSTER_NODE_TYPE_LEAF,
|
|
4362
|
-
CLUSTER_NODE_TYPE_ROOT,
|
|
4363
|
-
EventEmitter,
|
|
4364
|
-
FractionUtil,
|
|
4365
|
-
Graph,
|
|
4366
|
-
GraphBundleProjector,
|
|
4367
|
-
GraphClusterProjector,
|
|
4368
|
-
GraphForceProjector,
|
|
4369
|
-
GraphHierarchicalProjector,
|
|
4370
|
-
GraphLatticeProjector,
|
|
4371
|
-
GraphPlexusProjector,
|
|
4372
|
-
GraphProjector,
|
|
4373
|
-
GraphRadialProjector,
|
|
4374
|
-
GraphRelationalProjector,
|
|
4375
|
-
GraphRenderer,
|
|
4376
|
-
GraphSwarmProjector,
|
|
4377
|
-
GraphTreeProjector,
|
|
4378
|
-
GridController,
|
|
4379
|
-
HierarchicalEdgeBundling,
|
|
4380
|
-
Mesh,
|
|
4381
|
-
PLEXUS_NODE_TYPE_FOCUS,
|
|
4382
|
-
PLEXUS_NODE_TYPE_OBJECT,
|
|
4383
|
-
PLEXUS_NODE_TYPE_RELATION,
|
|
4384
|
-
Projector,
|
|
4385
|
-
Pulsar,
|
|
4386
|
-
RadialTree,
|
|
4387
|
-
Renderer,
|
|
4388
|
-
SVG,
|
|
4389
|
-
SVGContext,
|
|
4390
|
-
SVGContextProvider,
|
|
4391
|
-
Scale,
|
|
4392
|
-
Screen,
|
|
4393
|
-
TREE_NODE_TYPE_INBOUND,
|
|
4394
|
-
TREE_NODE_TYPE_NODE,
|
|
4395
|
-
TREE_NODE_TYPE_ROOT,
|
|
4396
|
-
TidyTree,
|
|
4397
|
-
Tree,
|
|
4398
|
-
Vector,
|
|
4399
|
-
ZoomHandler,
|
|
4400
|
-
appendRadialGroupLabel,
|
|
4401
|
-
appendRadialLeafLabel,
|
|
4402
|
-
appendRootLabel,
|
|
4403
|
-
buildHierarchy,
|
|
4404
|
-
createGraphDrag,
|
|
4405
|
-
createMarkers,
|
|
4406
|
-
defaultForceOptions,
|
|
4407
|
-
defaultIdAccessor,
|
|
4408
|
-
defaultTreeLayoutSlots,
|
|
4409
|
-
emptyGraph,
|
|
4410
|
-
getCircumferencePoints,
|
|
4411
|
-
isCollapsed,
|
|
4412
|
-
isLeaf,
|
|
4413
|
-
layoutHorizontal,
|
|
4414
|
-
layoutRadial,
|
|
4415
|
-
layoutVertical,
|
|
4416
|
-
linkerRenderer,
|
|
4417
|
-
normal,
|
|
4418
|
-
offsetPoints,
|
|
4419
|
-
updateNode,
|
|
4420
|
-
useContainerSize,
|
|
4421
|
-
useDynamicRef,
|
|
4422
|
-
useGrid,
|
|
4423
|
-
useStateRef,
|
|
4424
|
-
useSvgContext,
|
|
4425
|
-
useTimestamp,
|
|
4426
|
-
useZoom
|
|
4427
|
-
};
|
|
4428
|
-
//# sourceMappingURL=index.mjs.map
|