@dxos/react-ui-graph 0.8.4-main.fffef41 → 0.9.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.
Files changed (138) hide show
  1. package/LICENSE +102 -5
  2. package/README.md +5 -7
  3. package/dist/lib/browser/chunk-EASNZZ7B.mjs +52 -0
  4. package/dist/lib/browser/chunk-EASNZZ7B.mjs.map +7 -0
  5. package/dist/lib/browser/index.mjs +2729 -592
  6. package/dist/lib/browser/index.mjs.map +4 -4
  7. package/dist/lib/browser/meta.json +1 -1
  8. package/dist/lib/browser/testing/index.mjs +16 -16
  9. package/dist/lib/browser/testing/index.mjs.map +3 -3
  10. package/dist/lib/node-esm/chunk-JSE2X4ML.mjs +54 -0
  11. package/dist/lib/node-esm/chunk-JSE2X4ML.mjs.map +7 -0
  12. package/dist/lib/node-esm/index.mjs +2729 -592
  13. package/dist/lib/node-esm/index.mjs.map +4 -4
  14. package/dist/lib/node-esm/meta.json +1 -1
  15. package/dist/lib/node-esm/testing/index.mjs +16 -16
  16. package/dist/lib/node-esm/testing/index.mjs.map +3 -3
  17. package/dist/types/src/components/Graph/Graph.d.ts +23 -6
  18. package/dist/types/src/components/Graph/Graph.d.ts.map +1 -1
  19. package/dist/types/src/components/Graph/Graph.stories.d.ts +6 -6
  20. package/dist/types/src/components/Graph/Graph.stories.d.ts.map +1 -1
  21. package/dist/types/src/components/Mesh/Mesh.d.ts.map +1 -1
  22. package/dist/types/src/components/Mesh/Mesh.stories.d.ts.map +1 -1
  23. package/dist/types/src/components/SVG/FPS.d.ts +20 -0
  24. package/dist/types/src/components/SVG/FPS.d.ts.map +1 -0
  25. package/dist/types/src/components/SVG/Grid.d.ts.map +1 -1
  26. package/dist/types/src/components/SVG/Markers.d.ts.map +1 -1
  27. package/dist/types/src/components/SVG/Root.d.ts +8 -4
  28. package/dist/types/src/components/SVG/Root.d.ts.map +1 -1
  29. package/dist/types/src/components/SVG/SVG.d.ts +17 -18
  30. package/dist/types/src/components/SVG/SVG.d.ts.map +1 -1
  31. package/dist/types/src/components/SVG/Svg.stories.d.ts +1 -1
  32. package/dist/types/src/components/SVG/Svg.stories.d.ts.map +1 -1
  33. package/dist/types/src/fx/fx.stories.d.ts +3 -3
  34. package/dist/types/src/fx/fx.stories.d.ts.map +1 -1
  35. package/dist/types/src/fx/pulsar.d.ts.map +1 -1
  36. package/dist/types/src/graph/markers.d.ts.map +1 -1
  37. package/dist/types/src/graph/projector/graph-bundle-projector.d.ts +47 -0
  38. package/dist/types/src/graph/projector/graph-bundle-projector.d.ts.map +1 -0
  39. package/dist/types/src/graph/projector/graph-cluster-projector.d.ts +65 -0
  40. package/dist/types/src/graph/projector/graph-cluster-projector.d.ts.map +1 -0
  41. package/dist/types/src/graph/projector/graph-drag.d.ts.map +1 -1
  42. package/dist/types/src/graph/projector/graph-force-projector.d.ts +1 -1
  43. package/dist/types/src/graph/projector/graph-force-projector.d.ts.map +1 -1
  44. package/dist/types/src/graph/projector/graph-forces.d.ts.map +1 -1
  45. package/dist/types/src/graph/projector/graph-hierarchical-projector.d.ts +1 -1
  46. package/dist/types/src/graph/projector/graph-hierarchical-projector.d.ts.map +1 -1
  47. package/dist/types/src/graph/projector/graph-lattice-projector.d.ts +23 -0
  48. package/dist/types/src/graph/projector/graph-lattice-projector.d.ts.map +1 -0
  49. package/dist/types/src/graph/projector/graph-plexus-projector.d.ts +67 -0
  50. package/dist/types/src/graph/projector/graph-plexus-projector.d.ts.map +1 -0
  51. package/dist/types/src/graph/projector/graph-plexus-projector.test.d.ts +2 -0
  52. package/dist/types/src/graph/projector/graph-plexus-projector.test.d.ts.map +1 -0
  53. package/dist/types/src/graph/projector/graph-projector.d.ts +10 -2
  54. package/dist/types/src/graph/projector/graph-projector.d.ts.map +1 -1
  55. package/dist/types/src/graph/projector/graph-radial-projector.d.ts +7 -1
  56. package/dist/types/src/graph/projector/graph-radial-projector.d.ts.map +1 -1
  57. package/dist/types/src/graph/projector/graph-relational-projector.d.ts +1 -1
  58. package/dist/types/src/graph/projector/graph-relational-projector.d.ts.map +1 -1
  59. package/dist/types/src/graph/projector/graph-swarm-projector.d.ts +72 -0
  60. package/dist/types/src/graph/projector/graph-swarm-projector.d.ts.map +1 -0
  61. package/dist/types/src/graph/projector/index.d.ts +5 -0
  62. package/dist/types/src/graph/projector/index.d.ts.map +1 -1
  63. package/dist/types/src/graph/projector/projector.d.ts +10 -1
  64. package/dist/types/src/graph/projector/projector.d.ts.map +1 -1
  65. package/dist/types/src/graph/renderer/bullets.d.ts.map +1 -1
  66. package/dist/types/src/graph/renderer/graph-renderer.d.ts +41 -1
  67. package/dist/types/src/graph/renderer/graph-renderer.d.ts.map +1 -1
  68. package/dist/types/src/graph/renderer/index.d.ts +1 -0
  69. package/dist/types/src/graph/renderer/index.d.ts.map +1 -1
  70. package/dist/types/src/graph/renderer/labels.d.ts +33 -0
  71. package/dist/types/src/graph/renderer/labels.d.ts.map +1 -0
  72. package/dist/types/src/graph/renderer/renderer.d.ts +12 -1
  73. package/dist/types/src/graph/renderer/renderer.d.ts.map +1 -1
  74. package/dist/types/src/graph/types.d.ts +15 -1
  75. package/dist/types/src/graph/types.d.ts.map +1 -1
  76. package/dist/types/src/hooks/useGrid.d.ts.map +1 -1
  77. package/dist/types/src/hooks/useStateRef.d.ts.map +1 -1
  78. package/dist/types/src/hooks/useSvgContext.d.ts.map +1 -1
  79. package/dist/types/src/hooks/useZoom.d.ts.map +1 -1
  80. package/dist/types/src/stories/experimental.stories.d.ts +7 -0
  81. package/dist/types/src/stories/experimental.stories.d.ts.map +1 -0
  82. package/dist/types/src/stories/hooks.stories.d.ts +4 -3
  83. package/dist/types/src/stories/hooks.stories.d.ts.map +1 -1
  84. package/dist/types/src/testing/data.d.ts +5 -5
  85. package/dist/types/src/testing/data.d.ts.map +1 -1
  86. package/dist/types/src/testing/hooks.d.ts +2 -16
  87. package/dist/types/src/testing/hooks.d.ts.map +1 -1
  88. package/dist/types/src/testing/model.d.ts +4 -8
  89. package/dist/types/src/testing/model.d.ts.map +1 -1
  90. package/dist/types/src/util/events.d.ts.map +1 -1
  91. package/dist/types/src/util/fraction.d.ts +1 -1
  92. package/dist/types/src/util/fraction.d.ts.map +1 -1
  93. package/dist/types/src/util/points.d.ts.map +1 -1
  94. package/dist/types/src/util/scale.d.ts.map +1 -1
  95. package/dist/types/src/util/screen.d.ts.map +1 -1
  96. package/dist/types/src/util/vector.d.ts.map +1 -1
  97. package/dist/types/tsconfig.tsbuildinfo +1 -1
  98. package/package.json +33 -35
  99. package/src/components/Graph/Graph.stories.tsx +45 -39
  100. package/src/components/Graph/Graph.tsx +126 -23
  101. package/src/components/Mesh/Mesh.stories.tsx +8 -7
  102. package/src/components/Mesh/Mesh.tsx +43 -35
  103. package/src/components/SVG/FPS.tsx +91 -0
  104. package/src/components/SVG/Grid.tsx +1 -1
  105. package/src/components/SVG/Root.tsx +42 -9
  106. package/src/components/SVG/SVG.tsx +14 -3
  107. package/src/components/SVG/Svg.stories.tsx +4 -5
  108. package/src/fx/fx.stories.tsx +7 -8
  109. package/src/graph/projector/graph-bundle-projector.ts +210 -0
  110. package/src/graph/projector/graph-cluster-projector.ts +337 -0
  111. package/src/graph/projector/graph-drag.ts +0 -1
  112. package/src/graph/projector/graph-force-projector.ts +7 -3
  113. package/src/graph/projector/graph-hierarchical-projector.ts +4 -4
  114. package/src/graph/projector/graph-lattice-projector.ts +86 -0
  115. package/src/graph/projector/graph-plexus-projector.test.ts +102 -0
  116. package/src/graph/projector/graph-plexus-projector.ts +309 -0
  117. package/src/graph/projector/graph-projector.ts +24 -4
  118. package/src/graph/projector/graph-radial-projector.ts +31 -4
  119. package/src/graph/projector/graph-relational-projector.ts +4 -4
  120. package/src/graph/projector/graph-swarm-projector.ts +373 -0
  121. package/src/graph/projector/index.ts +5 -0
  122. package/src/graph/projector/projector.ts +12 -3
  123. package/src/graph/renderer/graph-renderer.ts +289 -31
  124. package/src/graph/renderer/index.ts +1 -0
  125. package/src/graph/renderer/labels.ts +138 -0
  126. package/src/graph/renderer/renderer.ts +15 -4
  127. package/src/graph/types.ts +15 -1
  128. package/src/stories/experimental.stories.tsx +446 -0
  129. package/src/stories/hooks.stories.tsx +21 -10
  130. package/src/testing/data.ts +14 -14
  131. package/src/testing/hooks.ts +3 -4
  132. package/src/testing/model.ts +6 -6
  133. package/src/util/fraction.ts +1 -1
  134. package/styles/graph.css +341 -43
  135. package/dist/lib/browser/chunk-XWA2XJDX.mjs +0 -955
  136. package/dist/lib/browser/chunk-XWA2XJDX.mjs.map +0 -7
  137. package/dist/lib/node-esm/chunk-3RLSFEEV.mjs +0 -957
  138. package/dist/lib/node-esm/chunk-3RLSFEEV.mjs.map +0 -7
@@ -1,32 +1,15 @@
1
1
  import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
2
  import {
3
- EventEmitter,
4
- FractionUtil,
5
- GridController,
6
- SVGContext,
7
- SVGContextProvider,
8
- Scale,
9
- Screen,
10
- Vector,
11
- ZoomHandler,
12
- getCircumferencePoints,
13
- normal,
14
- offsetPoints,
15
3
  useDynamicRef,
16
- useGrid,
17
4
  useStateRef,
18
- useSvgContext,
19
- useTimestamp,
20
- useZoom
21
- } from "./chunk-3RLSFEEV.mjs";
5
+ useTimestamp
6
+ } from "./chunk-JSE2X4ML.mjs";
22
7
 
23
8
  // src/components/Graph/Graph.tsx
24
- import { useSignals as _useSignals } from "@preact-signals/safe-react/tracking";
25
- import { effect } from "@preact/signals-core";
26
- import React, { forwardRef, useEffect, useImperativeHandle, useMemo, useRef } from "react";
9
+ import React, { forwardRef, useEffect as useEffect3, useImperativeHandle, useMemo as useMemo3, useRef as useRef3 } from "react";
27
10
  import { combine } from "@dxos/async";
28
- import { log as log6 } from "@dxos/log";
29
- import { mx } from "@dxos/react-ui-theme";
11
+ import { log as log11 } from "@dxos/log";
12
+ import { mx } from "@dxos/ui-theme";
30
13
 
31
14
  // src/graph/markers.ts
32
15
  import { line, select } from "d3";
@@ -74,9 +57,1250 @@ var createMarkers = ({ arrowSize = 12 } = {}) => (group) => {
74
57
  generator: createDot(6),
75
58
  className: "dx-dot"
76
59
  }
77
- ]).join("marker").attr("id", (d) => d.id).attr("markerUnits", "strokeWidth").attr("class", (d) => d.className).each((d, i, nodes) => {
78
- select(nodes[i]).call(d.generator);
79
- });
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 { cluster as d3Cluster, curveBundle, 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
+ }
80
1304
  };
81
1305
 
82
1306
  // src/graph/projector/graph-drag.ts
@@ -159,144 +1383,9 @@ var createGraphDrag = (context, projector, options = defaultDragOptions) => {
159
1383
  });
160
1384
  };
161
1385
 
162
- // src/graph/types.ts
163
- import { invariant } from "@dxos/invariant";
164
- var __dxlog_file = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/types.ts";
165
- var defaultIdAccessor = (node) => {
166
- invariant(node.id, void 0, {
167
- F: __dxlog_file,
168
- L: 18,
169
- S: void 0,
170
- A: [
171
- "node.id",
172
- ""
173
- ]
174
- });
175
- return node.id;
176
- };
177
- var emptyGraph = {
178
- nodes: [],
179
- edges: []
180
- };
181
-
182
- // src/graph/projector/projector.ts
183
- var Projector = class {
184
- _context;
185
- updated = new EventEmitter();
186
- _options;
187
- constructor(_context, options) {
188
- this._context = _context;
189
- this._options = Object.assign({
190
- idAccessor: defaultIdAccessor
191
- }, options);
192
- }
193
- get context() {
194
- return this._context;
195
- }
196
- get options() {
197
- return this._options;
198
- }
199
- refresh(dragging = false) {
200
- this.onRefresh(dragging);
201
- }
202
- updateData(data) {
203
- this.onUpdate(data);
204
- }
205
- emitUpdate() {
206
- this.updated.emit({
207
- layout: this.layout
208
- });
209
- }
210
- async clear() {
211
- await this.onClear();
212
- }
213
- async start() {
214
- await this.onStart();
215
- }
216
- async stop() {
217
- await this.onStop();
218
- }
219
- onRefresh(dragging = false) {
220
- }
221
- onUpdate(data) {
222
- }
223
- async onClear() {
224
- }
225
- async onStart() {
226
- }
227
- async onStop() {
228
- }
229
- };
230
-
231
- // src/graph/projector/graph-projector.ts
232
- var GraphProjector = class extends Projector {
233
- _selection;
234
- _layout;
235
- // TODO(burdon): Change to object props.
236
- constructor(context, options, _selection, layout) {
237
- super(context, options), this._selection = _selection;
238
- this._layout = {
239
- id: (Math.random() * 10).toString().slice(8),
240
- graph: {
241
- nodes: [
242
- ...layout?.graph.nodes ?? []
243
- ],
244
- edges: [
245
- ...layout?.graph.edges ?? []
246
- ]
247
- }
248
- };
249
- }
250
- get layout() {
251
- return this._layout;
252
- }
253
- get selection() {
254
- return this._selection;
255
- }
256
- numChildren(node) {
257
- return this._layout.graph.edges.filter((edge) => edge.source.id === this.options.idAccessor(node)).length;
258
- }
259
- reset() {
260
- this._layout.graph.nodes = [];
261
- this._layout.graph.edges = [];
262
- this.emitUpdate();
263
- }
264
- /**
265
- * Merge external data with internal representation (e.g., so force properties like position are preserved).
266
- * @param data
267
- */
268
- mergeData(data = emptyGraph) {
269
- const nodes = data.nodes.map((node) => {
270
- let current = this._layout.graph.nodes.find((n) => n.id === this.options.idAccessor(node));
271
- if (!current) {
272
- current = {
273
- id: this.options.idAccessor(node)
274
- };
275
- }
276
- current.data = node;
277
- return current;
278
- });
279
- const edges = data.edges.map((edge) => ({
280
- id: edge.id,
281
- source: nodes.find((n) => n.id === edge.source),
282
- target: nodes.find((n) => n.id === edge.target),
283
- data: edge.data
284
- })).filter((edge) => edge.source && edge.target);
285
- Object.assign(this._layout, {
286
- graph: {
287
- nodes,
288
- edges
289
- }
290
- });
291
- }
292
- async onClear() {
293
- this.reset();
294
- }
295
- };
296
-
297
1386
  // src/graph/projector/graph-force-projector.ts
298
1387
  import { forceCenter, forceCollide, forceLink, forceManyBody, forceRadial, forceSimulation, forceX as forceX2, forceY as forceY2 } from "d3";
299
- import { log } from "@dxos/log";
1388
+ import { log as log4 } from "@dxos/log";
300
1389
 
301
1390
  // src/graph/projector/graph-forces.ts
302
1391
  import { forceX, forceY } from "d3";
@@ -315,7 +1404,7 @@ var forcePoint = ({ x, y, strength = 0.01 }) => {
315
1404
  };
316
1405
 
317
1406
  // src/graph/projector/graph-force-projector.ts
318
- var __dxlog_file2 = "/__w/dxos/dxos/packages/ui/react-ui-graph/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";
319
1408
  var getValue = (valueOrFunction, invoker, defaultValue) => {
320
1409
  return (typeof valueOrFunction === "function" ? invoker(valueOrFunction) : valueOrFunction) ?? defaultValue;
321
1410
  };
@@ -365,7 +1454,7 @@ var GraphForceProjector = class extends GraphProjector {
365
1454
  } else {
366
1455
  this.updateForces(this.forces);
367
1456
  }
368
- this._simulation.on("tick", () => propagating && this.emitUpdate()).velocityDecay(0.3).alphaDecay(1 - Math.pow(1e-3, 1 / 300)).alpha(1).restart();
1457
+ this._simulation.on("tick", () => propagating && this.emitUpdate("positions")).velocityDecay(0.3).alphaDecay(1 - Math.pow(1e-3, 1 / 300)).alpha(1).restart();
369
1458
  }
370
1459
  async onStop() {
371
1460
  clearTimeout(this._timeout);
@@ -382,21 +1471,17 @@ var GraphForceProjector = class extends GraphProjector {
382
1471
  this.restart();
383
1472
  }
384
1473
  onUpdate(graph) {
385
- log("onUpdate", {
1474
+ log4("onUpdate", {
386
1475
  graph: {
387
1476
  nodes: graph?.nodes.length,
388
1477
  edges: graph?.edges.length
389
1478
  }
390
- }, {
391
- F: __dxlog_file2,
392
- L: 227,
393
- S: this,
394
- C: (f, a) => f(...a)
395
- });
1479
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file6, L: 91, S: this });
396
1480
  this._simulation.stop();
397
1481
  this.mergeData(graph);
398
1482
  this.updateLayout();
399
1483
  this.updateForces(this.forces);
1484
+ this.emitUpdate("topology");
400
1485
  this.restart();
401
1486
  }
402
1487
  updateLayout() {
@@ -440,14 +1525,9 @@ var GraphForceProjector = class extends GraphProjector {
440
1525
  * Update all forces.
441
1526
  */
442
1527
  updateForces(forces) {
443
- log("updateForces", {
1528
+ log4("updateForces", {
444
1529
  forces
445
- }, {
446
- F: __dxlog_file2,
447
- L: 288,
448
- S: this,
449
- C: (f, a) => f(...a)
450
- });
1530
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file6, L: 153, S: this });
451
1531
  this._simulation.nodes(this.layout.graph.nodes).force("link", maybeForce(forces?.link, (config) => {
452
1532
  const force = forceLink().id((d) => d.id).links(this.layout.graph.edges.filter((edge) => edge.linkForce));
453
1533
  if (config.distance != null) {
@@ -480,172 +1560,51 @@ var GraphForceProjector = class extends GraphProjector {
480
1560
  const force = forceRadial(config.radius ?? 100, config.x ?? 0, config.y ?? 0);
481
1561
  if (config.strength != null) {
482
1562
  force.strength(config.strength);
483
- }
484
- return force;
485
- })).force("center", maybeForce(forces?.center, (config) => {
486
- const force = forceCenter().x(config.x ?? 0).y(config.y ?? 0);
487
- if (config.strength != null) {
488
- force.strength(config.strength);
489
- }
490
- return force;
491
- })).force("point", maybeForce(forces?.point, ({ x = 0, y = 0, strength }) => {
492
- return forcePoint({
493
- x,
494
- y,
495
- strength
496
- });
497
- })).force("x", maybeForce(forces?.x, (config) => {
498
- const force = forceX2(config.x ?? 0);
499
- if (config.strength != null) {
500
- force.strength(config.strength);
501
- }
502
- return force;
503
- })).force("y", maybeForce(forces?.y, (config) => {
504
- const force = forceY2(config.y ?? 0);
505
- if (config.strength != null) {
506
- force.strength(config.strength);
507
- }
508
- return force;
509
- }));
510
- }
511
- };
512
-
513
- // src/graph/projector/graph-hierarchical-projector.ts
514
- import { log as log3 } from "@dxos/log";
515
-
516
- // src/graph/projector/graph-radial-projector.ts
517
- import { easeCubic, timer } from "d3";
518
- import { log as log2 } from "@dxos/log";
519
- var __dxlog_file3 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/projector/graph-radial-projector.ts";
520
- var GraphRadialProjector = class extends GraphProjector {
521
- _timer;
522
- findNode(x, y, radius) {
523
- return void 0;
524
- }
525
- onUpdate(graph) {
526
- log2("onUpdate", {
527
- graph: {
528
- nodes: graph?.nodes.length,
529
- edges: graph?.edges.length
530
- }
531
- }, {
532
- F: __dxlog_file3,
533
- L: 32,
534
- S: this,
535
- C: (f, a) => f(...a)
536
- });
537
- this.mergeData(graph);
538
- this.doRadialLayout();
539
- }
540
- animate() {
541
- const start = Date.now();
542
- if (this.options.duration) {
543
- this._timer = timer(() => {
544
- const t = Math.min(1, (Date.now() - start) / this.options.duration);
545
- const d = easeCubic(t);
546
- this.layout.graph.nodes.forEach((node) => {
547
- node.x = node.sx + (node.tx - node.sx) * d;
548
- node.y = node.sy + (node.ty - node.sy) * d;
549
- node.r = node.sr + (node.tr - node.sr) * d;
550
- });
551
- this.emitUpdate();
552
- if (t >= 1) {
553
- this._timer.stop();
554
- this._timer = void 0;
555
- }
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
556
1576
  });
557
- } else {
558
- this.emitUpdate();
559
- }
560
- }
561
- async onStop() {
562
- this._timer?.stop();
563
- this._timer = void 0;
564
- }
565
- doRadialLayout() {
566
- if (!this.context.size) {
567
- return;
568
- }
569
- 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);
570
- this.layout.graph.nodes.forEach((node, i) => {
571
- const [tx, ty] = radial(i);
572
- updateNode(node, [
573
- tx,
574
- ty
575
- ]);
576
- });
577
- this.animate();
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
+ }));
578
1590
  }
579
1591
  };
580
- var updateNode = (node, [tx, ty], tr = 8) => {
581
- const sx = node.x ?? tx;
582
- const sy = node.y ?? ty;
583
- const sr = node.r ?? tr;
584
- Object.assign(node, {
585
- initialized: true,
586
- // Start.
587
- sx,
588
- sy,
589
- sr,
590
- // Target.
591
- tx,
592
- ty,
593
- tr,
594
- // Current.
595
- x: sx,
596
- y: sy,
597
- r: sr
598
- });
599
- };
600
- var layoutRadial = (x, y, r, n, start = -Math.PI / 2) => {
601
- const da = 2 * Math.PI / n;
602
- return (i) => {
603
- const a = start + i * da;
604
- return [
605
- x + Math.cos(a) * r,
606
- y + Math.sin(a) * r
607
- ];
608
- };
609
- };
610
- var layoutHorizontal = (x, y, w, n) => {
611
- const dx = n > 1 ? w / (n - 1) : 0;
612
- const sx = x - (n - 1) * dx / 2;
613
- return (i) => {
614
- return [
615
- sx + i * dx,
616
- y
617
- ];
618
- };
619
- };
620
- var layoutVertical = (x, y, h, n) => {
621
- const dy = n > 1 ? h / (n - 1) : 0;
622
- const sy = y - (n - 1) * dy / 2;
623
- return (i) => {
624
- return [
625
- x,
626
- sy + i * dy
627
- ];
628
- };
629
- };
630
1592
 
631
1593
  // src/graph/projector/graph-hierarchical-projector.ts
632
- var __dxlog_file4 = "/__w/dxos/dxos/packages/ui/react-ui-graph/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";
633
1596
  var GraphHierarchicalProjector = class extends GraphRadialProjector {
634
1597
  onUpdate(graph) {
635
- log3("onUpdate", {
1598
+ log5("onUpdate", {
636
1599
  graph: {
637
1600
  nodes: graph?.nodes.length,
638
1601
  edges: graph?.edges.length
639
1602
  },
640
- selection: this.selection?.selected.value
641
- }, {
642
- F: __dxlog_file4,
643
- L: 24,
644
- S: this,
645
- C: (f, a) => f(...a)
646
- });
1603
+ selection: this.selection?.getSelectedIds()
1604
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file7, L: 8, S: this });
647
1605
  this.mergeData(graph);
648
- const selected = this.selection?.selected.value?.[0];
1606
+ this.emitUpdate("topology");
1607
+ const selected = this.selection?.getSelectedIds()[0];
649
1608
  if (selected) {
650
1609
  const node = this.layout.graph.nodes.find((node2) => node2.id === selected);
651
1610
  if (node) {
@@ -690,25 +1649,307 @@ var GraphHierarchicalProjector = class extends GraphRadialProjector {
690
1649
  }
691
1650
  };
692
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
+
693
1938
  // src/graph/projector/graph-relational-projector.ts
694
- import { log as log4 } from "@dxos/log";
695
- var __dxlog_file5 = "/__w/dxos/dxos/packages/ui/react-ui-graph/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";
696
1941
  var GraphRelationalProjector = class extends GraphRadialProjector {
697
1942
  onUpdate(graph) {
698
- log4("onUpdate", {
1943
+ log8("onUpdate", {
699
1944
  graph: {
700
1945
  nodes: graph?.nodes.length,
701
1946
  edges: graph?.edges.length
702
1947
  },
703
- selection: this.selection?.selected.value
704
- }, {
705
- F: __dxlog_file5,
706
- L: 24,
707
- S: this,
708
- C: (f, a) => f(...a)
709
- });
1948
+ selection: this.selection?.getSelectedIds()
1949
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file10, L: 8, S: this });
710
1950
  this.mergeData(graph);
711
- const selected = this.selection?.selected.value?.[0];
1951
+ this.emitUpdate("topology");
1952
+ const selected = this.selection?.getSelectedIds()[0];
712
1953
  if (selected) {
713
1954
  const node = this.layout.graph.nodes.find((node2) => node2.id === selected);
714
1955
  if (node) {
@@ -775,14 +2016,271 @@ var GraphRelationalProjector = class extends GraphRadialProjector {
775
2016
  ]);
776
2017
  });
777
2018
  }
778
- this.animate();
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");
779
2277
  }
780
2278
  };
781
2279
 
782
2280
  // src/graph/renderer/graph-renderer.ts
783
2281
  import { easeCubicOut, line as line2, polygonHull, select as select4 } from "d3";
784
2282
  import * as Clipper from "js-clipper";
785
- import { log as log5 } from "@dxos/log";
2283
+ import { log as log10 } from "@dxos/log";
786
2284
 
787
2285
  // src/graph/renderer/bullets.ts
788
2286
  import { easeLinear, interpolate, select as select3 } from "d3";
@@ -815,8 +2313,6 @@ var createBullets = (group, nodeId, options = {}) => {
815
2313
  };
816
2314
 
817
2315
  // src/graph/renderer/renderer.ts
818
- import { invariant as invariant2 } from "@dxos/invariant";
819
- var __dxlog_file6 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/renderer/renderer.ts";
820
2316
  var Renderer = class {
821
2317
  _context;
822
2318
  _root;
@@ -831,34 +2327,48 @@ var Renderer = class {
831
2327
  get context() {
832
2328
  return this._context;
833
2329
  }
2330
+ /**
2331
+ * Returns the SVG group root if mounted.
2332
+ * The ref may be null between mount/unmount cycles or before the SVG container has sized.
2333
+ * Callers should treat null as "skip this render" rather than as an error.
2334
+ */
834
2335
  get root() {
835
- invariant2(this._root.current, "SVG root is not initialized", {
836
- F: __dxlog_file6,
837
- L: 40,
838
- S: this,
839
- A: [
840
- "this._root.current",
841
- "'SVG root is not initialized'"
842
- ]
843
- });
844
2336
  return this._root.current;
845
2337
  }
846
2338
  get options() {
847
2339
  return this._options;
848
2340
  }
2341
+ /**
2342
+ * Fast path invoked when only positions changed.
2343
+ * Default falls back to a full `render`; subclasses can override to skip
2344
+ * enter/exit joins and attribute callbacks for per-tick updates.
2345
+ */
2346
+ applyPositions(layout) {
2347
+ this.render(layout);
2348
+ }
849
2349
  };
850
2350
 
851
2351
  // src/graph/renderer/graph-renderer.ts
852
- var __dxlog_file7 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/renderer/graph-renderer.ts";
2352
+ var __dxlog_file12 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/renderer/graph-renderer.ts";
853
2353
  var createLine2 = line2();
2354
+ var EDGE_FADE_MS = 300;
2355
+ var NODE_HIDE_MS = 500;
854
2356
  var GraphRenderer = class extends Renderer {
2357
+ // First render of this instance — used to clear DOM left behind by a previous
2358
+ // renderer pointing at the same `<g>` (e.g. when the consumer swaps projectors
2359
+ // and a new renderer is constructed for the same SVG root). Without this, the
2360
+ // previous variant's node shapes (e.g. circles) would persist as d3.join sees
2361
+ // them in the `update` set instead of `enter`.
2362
+ #firstRender = true;
855
2363
  render(layout) {
856
- log5("render", layout, {
857
- F: __dxlog_file7,
858
- L: 63,
859
- S: this,
860
- C: (f, a) => f(...a)
861
- });
2364
+ if (!this.root) {
2365
+ return;
2366
+ }
2367
+ if (this.#firstRender) {
2368
+ this.#firstRender = false;
2369
+ this.root.replaceChildren();
2370
+ }
2371
+ log10("render", layout, { "~LogMeta": "~LogMeta", F: __dxlog_file12, L: 35, S: this });
862
2372
  const root = select4(this.root);
863
2373
  if (layout.guides) {
864
2374
  root.selectAll("g.dx-guides").data([
@@ -916,37 +2426,92 @@ var GraphRenderer = class extends Renderer {
916
2426
  {
917
2427
  id: "edges"
918
2428
  }
919
- ], (d) => d.id).join("g").classed("dx-edges", true);
2429
+ ], (d) => d.id).join("g").classed("dx-edges", true).style("opacity", this.options.edgeOpacity != null ? String(this.options.edgeOpacity) : null);
920
2430
  const nodeGroup = root.selectAll("g.dx-nodes").data([
921
2431
  {
922
2432
  id: "nodes"
923
2433
  }
924
2434
  ], (d) => d.id).join("g").classed("dx-nodes", true);
925
- nodeGroup.selectAll("g.dx-node").data(layout.graph?.nodes ?? [], (d) => d.id).join((enter) => enter.append("g").attr("data-id", (d) => d.id).attr("opacity", 1).classed("dx-node", true).call(createNode, this.options), (update) => update.call(updateNode2, this.options), (exit) => {
2435
+ 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) => {
926
2436
  return exit.transition().ease(easeCubicOut).duration(300).attr("opacity", 0).on("end", function(d) {
927
2437
  select4(this).remove();
928
2438
  });
929
- }).sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
930
- edgeGroup.selectAll("g.dx-edge").data(layout.graph?.edges ?? [], (d) => d.id).join((enter) => enter.append("g").attr("data-id", (d) => d.id).classed("dx-edge", true).call(createEdge, this.options), (update) => update.call(updateEdge, this.options, nodeGroup).each((d) => {
931
- }), (exit) => exit.remove()).sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
2439
+ }).call(updateNode2, this.options).sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
2440
+ edgeGroup.selectAll("g.dx-edge").data(layout.graph?.edges ?? [], (d) => d.id).join(
2441
+ (enter) => enter.append("g").attr("data-id", (d) => d.id).classed("dx-edge", true).call(createEdge, this.options),
2442
+ (update) => update,
2443
+ // Exit fade: edges removed by a topology change (e.g. variant switch from cluster
2444
+ // back to force replaces hierarchy edges with data edges) ease out rather than pop.
2445
+ (exit) => exit.transition().ease(easeCubicOut).duration(EDGE_FADE_MS).attr("opacity", 0).on("end", function() {
2446
+ select4(this).remove();
2447
+ })
2448
+ ).call(updateEdge, this.options, nodeGroup).sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
2449
+ }
2450
+ /**
2451
+ * Fast positions-only update. Skips enter/exit, attribute callbacks, label sizing,
2452
+ * and the subgraph hull pass; just writes `transform` per node and `d` per edge.
2453
+ * Called per simulation tick / animation frame.
2454
+ */
2455
+ applyPositions(layout) {
2456
+ if (!this.root) {
2457
+ return;
2458
+ }
2459
+ const root = select4(this.root);
2460
+ const applyNode = this.options.applyNode;
2461
+ 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) {
2462
+ applyNode(this, d);
2463
+ } : () => {
2464
+ });
2465
+ root.select("g.dx-edges").selectAll("g.dx-edge").selectAll("path").attr("d", function() {
2466
+ const edge = select4(this.parentElement).datum();
2467
+ if (edge.path) {
2468
+ return edge.path;
2469
+ }
2470
+ const { source, target } = edge;
2471
+ if (!source.initialized || !target.initialized) {
2472
+ return null;
2473
+ }
2474
+ return createLine2(getCircumferencePoints([
2475
+ source.x,
2476
+ source.y
2477
+ ], [
2478
+ target.x,
2479
+ target.y
2480
+ ], source.r, target.r));
2481
+ });
932
2482
  }
933
2483
  /**
934
2484
  * Trigger path bullets.
935
2485
  * @param node
936
2486
  */
937
2487
  fireBullet(node) {
2488
+ if (!this.root) {
2489
+ return;
2490
+ }
938
2491
  select4(this.root).selectAll("g.dx-edges").selectAll("path").call(createBullets(this.root, node.id));
939
2492
  }
940
2493
  };
2494
+ var SHAPE_CLASS = "dx-shape";
2495
+ var renderCustomShape = (group, options) => {
2496
+ group.each(function(d) {
2497
+ const g = select4(this);
2498
+ g.selectAll(`.${SHAPE_CLASS}`).remove();
2499
+ const shapeGroup = g.append("g").classed(SHAPE_CLASS, true);
2500
+ options.renderNode(shapeGroup, d);
2501
+ });
2502
+ };
941
2503
  var createNode = (group, options) => {
942
- const circle = group.append("circle");
2504
+ const useCustom = !!options.renderNode;
2505
+ if (useCustom) {
2506
+ renderCustomShape(group, options);
2507
+ }
2508
+ const hitTarget = useCustom ? group : group.append("circle");
943
2509
  if (options.drag) {
944
- circle.call(options.drag);
2510
+ hitTarget.call(options.drag);
945
2511
  }
946
2512
  if (options.onNodeClick) {
947
- group.on("click", (event) => {
948
- const node = select4(event.target).datum();
949
- options.onNodeClick(node, event);
2513
+ group.on("click", (event, d) => {
2514
+ options.onNodeClick(d, event);
950
2515
  });
951
2516
  }
952
2517
  if (options.labels && !options.onNodePointerEnter) {
@@ -955,24 +2520,34 @@ var createNode = (group, options) => {
955
2520
  g.append("rect");
956
2521
  g.append("text").style("dominant-baseline", "middle");
957
2522
  }
958
- if (options.onNodePointerEnter) {
959
- circle.on("pointerenter", function(event) {
960
- const node = select4(this.parentElement).datum();
961
- options.onNodePointerEnter(node, event);
2523
+ if (options.onNodePointerEnter || options.onNodePointerLeave || options.highlightOnHover) {
2524
+ hitTarget.on("pointerenter", function(event) {
2525
+ const node = select4(useCustom ? this : this.parentElement).datum();
2526
+ options.onNodePointerEnter?.(node, event);
2527
+ if (options.highlightOnHover) {
2528
+ applyHoverHighlight(this.closest("g.dx-graph"), node.id);
2529
+ }
2530
+ });
2531
+ hitTarget.on("pointerleave", function(event) {
2532
+ const node = select4(useCustom ? this : this.parentElement).datum();
2533
+ options.onNodePointerLeave?.(node, event);
2534
+ if (options.highlightOnHover) {
2535
+ applyHoverHighlight(this.closest("g.dx-graph"), null);
2536
+ }
962
2537
  });
963
2538
  group.attr("data-hover", "handled");
964
- } else if (options.highlight !== false) {
965
- circle.on("pointerenter", function() {
2539
+ } else if (options.highlight !== false && !useCustom) {
2540
+ hitTarget.on("pointerenter", function() {
966
2541
  select4(this.closest("g.dx-node")).raise();
967
2542
  if (options.labels) {
968
- select4(this.parentElement).classed("dx-active", true).classed("dx-highlight", true);
2543
+ select4(this.parentElement).classed("dx-node-active", true).classed("dx-highlight", true);
969
2544
  }
970
2545
  });
971
2546
  group.on("pointerleave", function() {
972
2547
  if (options.labels) {
973
- select4(this).classed("dx-active", false);
2548
+ select4(this).classed("dx-node-active", false);
974
2549
  setTimeout(() => {
975
- if (!select4(this).classed("dx-active")) {
2550
+ if (!select4(this).classed("dx-node-active")) {
976
2551
  select4(this).classed("dx-highlight", false);
977
2552
  }
978
2553
  }, 300);
@@ -984,6 +2559,11 @@ var updateNode2 = (group, options) => {
984
2559
  group.attr("transform", (d) => {
985
2560
  return d.x != null && d.y != null ? `translate(${d.x},${d.y})` : void 0;
986
2561
  });
2562
+ group.classed("dx-hidden", (d) => d.hidden ?? false);
2563
+ group.transition("hide").ease(easeCubicOut).duration(NODE_HIDE_MS).attr("opacity", (d) => d.hidden ? 0 : 1);
2564
+ if (options.renderNode) {
2565
+ renderCustomShape(group, options);
2566
+ }
987
2567
  if (options.attributes?.node) {
988
2568
  try {
989
2569
  group.each((d, i, nodes) => {
@@ -999,18 +2579,15 @@ var updateNode2 = (group, options) => {
999
2579
  }
1000
2580
  });
1001
2581
  } catch (error) {
1002
- log5.error("updateNode", {
2582
+ log10.error("updateNode", {
1003
2583
  error: error?.message
1004
- }, {
1005
- F: __dxlog_file7,
1006
- L: 305,
1007
- S: void 0,
1008
- C: (f, a) => f(...a)
1009
- });
2584
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file12, L: 303, S: void 0 });
1010
2585
  }
1011
2586
  }
1012
2587
  const groupOrTransition = options.transition ? group.transition(options.transition()) : group;
1013
- groupOrTransition.select("circle").attr("r", (d) => d.r ?? 16);
2588
+ if (!options.renderNode) {
2589
+ groupOrTransition.select("circle").attr("r", (d) => d.r ?? 16);
2590
+ }
1014
2591
  if (options.labels) {
1015
2592
  const bx = 1;
1016
2593
  const px = 4;
@@ -1055,18 +2632,17 @@ var updateEdge = (group, options, nodeGroup) => {
1055
2632
  }
1056
2633
  });
1057
2634
  } catch (error) {
1058
- log5.error("updateEdge", {
2635
+ log10.error("updateEdge", {
1059
2636
  error: error?.message
1060
- }, {
1061
- F: __dxlog_file7,
1062
- L: 417,
1063
- S: void 0,
1064
- C: (f, a) => f(...a)
1065
- });
2637
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file12, L: 371, S: void 0 });
1066
2638
  }
1067
2639
  const groupOrTransition = options.transition ? group.transition(options.transition()) : group;
1068
2640
  groupOrTransition.selectAll("path").attr("d", function() {
1069
- const { source, target } = select4(this.parentElement).datum();
2641
+ const edge = select4(this.parentElement).datum();
2642
+ if (edge.path) {
2643
+ return edge.path;
2644
+ }
2645
+ const { source, target } = edge;
1070
2646
  if (!source.initialized || !target.initialized) {
1071
2647
  return;
1072
2648
  }
@@ -1079,6 +2655,62 @@ var updateEdge = (group, options, nodeGroup) => {
1079
2655
  ], source.r, target.r));
1080
2656
  });
1081
2657
  };
2658
+ var applyHoverHighlight = (root, focusedId) => {
2659
+ if (!root) {
2660
+ return;
2661
+ }
2662
+ const r = select4(root);
2663
+ const on = focusedId != null;
2664
+ const outgoing = /* @__PURE__ */ new Set();
2665
+ const incoming = /* @__PURE__ */ new Set();
2666
+ if (on) {
2667
+ r.select("g.dx-edges").selectAll("g.dx-edge").each((edge) => {
2668
+ if (edge.source.id === focusedId) {
2669
+ outgoing.add(edge.target.id);
2670
+ }
2671
+ if (edge.target.id === focusedId) {
2672
+ incoming.add(edge.source.id);
2673
+ }
2674
+ });
2675
+ }
2676
+ r.select("g.dx-edges").selectAll("g.dx-edge").each(function(edge) {
2677
+ const path = select4(this).select("path:not(.dx-click)");
2678
+ if (!on) {
2679
+ path.style("stroke", null).style("stroke-width", null).style("opacity", null);
2680
+ return;
2681
+ }
2682
+ if (edge.source.id === focusedId) {
2683
+ path.style("stroke", "var(--color-orange-500)").style("stroke-width", "1.5px").style("opacity", null);
2684
+ } else if (edge.target.id === focusedId) {
2685
+ path.style("stroke", "var(--color-sky-500)").style("stroke-width", "1.5px").style("opacity", null);
2686
+ } else {
2687
+ path.style("stroke", null).style("stroke-width", null).style("opacity", "0.8");
2688
+ }
2689
+ });
2690
+ r.select("g.dx-nodes").selectAll("g.dx-node").each(function(node) {
2691
+ const groupSel = select4(this);
2692
+ const shape = groupSel.select("circle, rect");
2693
+ if (node.hidden) {
2694
+ return;
2695
+ }
2696
+ if (!on) {
2697
+ groupSel.style("opacity", null);
2698
+ shape.style("stroke", null).style("stroke-width", null);
2699
+ return;
2700
+ }
2701
+ const connected = outgoing.has(node.id) || incoming.has(node.id);
2702
+ if (node.id === focusedId) {
2703
+ groupSel.style("opacity", null);
2704
+ shape.style("stroke", null).style("stroke-width", null);
2705
+ } else if (connected) {
2706
+ groupSel.style("opacity", null);
2707
+ shape.style("stroke", "var(--color-orange-400)").style("stroke-width", "2.5px");
2708
+ } else {
2709
+ groupSel.style("opacity", "0.4");
2710
+ shape.style("stroke", null).style("stroke-width", null);
2711
+ }
2712
+ });
2713
+ };
1082
2714
  var applyClasses = (el, classes) => {
1083
2715
  for (const [className, value] of Object.entries(classes)) {
1084
2716
  el.classed(className, value);
@@ -1117,6 +2749,45 @@ var findConnectedComponents = (graph) => {
1117
2749
  return components;
1118
2750
  };
1119
2751
 
2752
+ // src/graph/renderer/labels.ts
2753
+ var DEFAULT_DURATION = 200;
2754
+ var DEFAULT_OFFSET = 4;
2755
+ var getTarget = (node) => {
2756
+ const tx = node.tx;
2757
+ const ty = node.ty;
2758
+ return {
2759
+ x: tx ?? node.x ?? 0,
2760
+ y: ty ?? node.y ?? 0
2761
+ };
2762
+ };
2763
+ var appendRadialLeafLabel = (group, node, label, r, options = {}) => {
2764
+ if (!label) {
2765
+ return;
2766
+ }
2767
+ const { delay = 0, duration = DEFAULT_DURATION, offset = DEFAULT_OFFSET, className = "dx-cluster-label" } = options;
2768
+ const { x: targetX, y: targetY } = getTarget(node);
2769
+ const angleDeg = Math.atan2(targetY, targetX) * 180 / Math.PI;
2770
+ const flipped = angleDeg > 90 || angleDeg < -90;
2771
+ 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);
2772
+ };
2773
+ var appendRadialGroupLabel = (group, node, label, r, options = {}) => {
2774
+ if (!label) {
2775
+ return;
2776
+ }
2777
+ const { delay = 0, duration = DEFAULT_DURATION, offset = DEFAULT_OFFSET, className = "dx-cluster-label dx-cluster-label-group" } = options;
2778
+ const { x: targetX, y: targetY } = getTarget(node);
2779
+ const angleDeg = Math.atan2(targetY, targetX) * 180 / Math.PI;
2780
+ const flipped = angleDeg > 90 || angleDeg < -90;
2781
+ 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);
2782
+ };
2783
+ var appendRootLabel = (group, label, r, options = {}) => {
2784
+ if (!label) {
2785
+ return;
2786
+ }
2787
+ const { delay = 0, duration = DEFAULT_DURATION, offset = 6, className = "dx-cluster-label dx-cluster-label-root" } = options;
2788
+ 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);
2789
+ };
2790
+
1120
2791
  // src/graph/renderer/linker.ts
1121
2792
  import { line as line3 } from "d3";
1122
2793
  var createLine3 = line3();
@@ -1140,117 +2811,504 @@ var linkerRenderer = (root, { source, target, point } = {}) => {
1140
2811
  });
1141
2812
  };
1142
2813
 
2814
+ // src/hooks/useGrid.ts
2815
+ import { line as line4, range, select as select5 } from "d3";
2816
+ import { useEffect, useMemo, useRef } from "react";
2817
+
2818
+ // src/hooks/useSvgContext.ts
2819
+ import { createContext, useContext } from "react";
2820
+ import { createRef } from "react";
2821
+ import { raise } from "@dxos/debug";
2822
+ var SVGContext = class {
2823
+ _ref = createRef();
2824
+ _scale;
2825
+ _centered;
2826
+ _size;
2827
+ _center;
2828
+ resized = new EventEmitter();
2829
+ constructor({ scale = new Scale(), centered = true }) {
2830
+ this._scale = scale;
2831
+ this._centered = centered;
2832
+ }
2833
+ get ref() {
2834
+ return this._ref;
2835
+ }
2836
+ get svg() {
2837
+ return this._ref.current;
2838
+ }
2839
+ get scale() {
2840
+ return this._scale;
2841
+ }
2842
+ get center() {
2843
+ return this._center;
2844
+ }
2845
+ get size() {
2846
+ return this._size;
2847
+ }
2848
+ /**
2849
+ * https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/viewBox
2850
+ */
2851
+ get viewBox() {
2852
+ const [x, y] = this.center;
2853
+ return `${x},${y},${this._size.width},${this._size.height}`;
2854
+ }
2855
+ setSize(size) {
2856
+ this._size = {
2857
+ ...size
2858
+ };
2859
+ this._center = this._centered ? [
2860
+ -Math.floor(size.width / 2),
2861
+ -Math.floor(size.height / 2)
2862
+ ] : [
2863
+ 0,
2864
+ 0
2865
+ ];
2866
+ this.resized.emit(this);
2867
+ }
2868
+ setTransform(transform) {
2869
+ if (this._scale.setTransform(transform)) {
2870
+ this.resized.emit(this);
2871
+ }
2872
+ }
2873
+ /**
2874
+ * https://developer.mozilla.org/en-US/docs/Web/API/Document/elementFromPoint
2875
+ * @param point
2876
+ */
2877
+ elementFromPoint(point) {
2878
+ const [x, y] = point;
2879
+ const [cx, cy] = this._center;
2880
+ return document.elementFromPoint(cx + x, cy + y);
2881
+ }
2882
+ };
2883
+ var SVGContextType = createContext(void 0);
2884
+ var SVGContextProvider = SVGContextType.Provider;
2885
+ var useSvgContext = () => {
2886
+ return useContext(SVGContextType) ?? raise(new Error("Missing SVGRoot"));
2887
+ };
2888
+
2889
+ // src/hooks/useGrid.ts
2890
+ var createLine4 = line4();
2891
+ var createGrid = (context, options) => {
2892
+ if (!context.size) {
2893
+ return [];
2894
+ }
2895
+ const paths = [];
2896
+ const { width, height } = context.size;
2897
+ const { x, y, k } = context.scale.transform;
2898
+ const s = 1 / k;
2899
+ const w = width * s;
2900
+ const h = height * s;
2901
+ const dx = -(x + width / 2) * s;
2902
+ const dy = -(y + height / 2) * s;
2903
+ if (options.axis) {
2904
+ const axis = [
2905
+ [
2906
+ [
2907
+ 0,
2908
+ dy
2909
+ ],
2910
+ [
2911
+ 0,
2912
+ dy + h
2913
+ ]
2914
+ ],
2915
+ [
2916
+ [
2917
+ dx,
2918
+ 0
2919
+ ],
2920
+ [
2921
+ dx + w,
2922
+ 0
2923
+ ]
2924
+ ]
2925
+ ];
2926
+ paths.push({
2927
+ id: "axis",
2928
+ class: "dx-axis",
2929
+ path: axis.map((line5) => createLine4(line5)).join()
2930
+ });
2931
+ }
2932
+ if (options.grid) {
2933
+ const mod = (n, size, delta = 0) => Math.floor(n / size + delta) * size;
2934
+ const majorSize = context.scale.gridSize;
2935
+ const xMajor = range(-mod((x + width / 2) * s, majorSize), mod((-x + width / 2) * s, majorSize, 1), majorSize);
2936
+ const yMajor = range(-mod((y + height / 2) * s, majorSize), mod((-y + height / 2) * s, majorSize, 1), majorSize);
2937
+ const major = [
2938
+ ...xMajor.filter((x2) => !options.axis || x2).map((x2) => [
2939
+ [
2940
+ x2,
2941
+ dy
2942
+ ],
2943
+ [
2944
+ x2,
2945
+ dy + h
2946
+ ]
2947
+ ]),
2948
+ ...yMajor.filter((y2) => !options.axis || y2).map((y2) => [
2949
+ [
2950
+ dx,
2951
+ y2
2952
+ ],
2953
+ [
2954
+ dx + w,
2955
+ y2
2956
+ ]
2957
+ ])
2958
+ ];
2959
+ paths.push({
2960
+ id: "major",
2961
+ class: "dx-major",
2962
+ path: major.map((line5) => createLine4(line5)).join()
2963
+ });
2964
+ const minorSize = Math.pow(2, Math.round(Math.log2(s * context.scale.gridSize)));
2965
+ if (majorSize > minorSize) {
2966
+ const xMinor = range(-mod((x + width / 2) * s, minorSize), mod((-x + width / 2) * s, minorSize, 1), minorSize);
2967
+ const yMinor = range(-mod((y + height / 2) * s, minorSize), mod((-y + height / 2) * s, minorSize, 1), minorSize);
2968
+ const minor = [
2969
+ ...xMinor.filter((x2) => xMajor.indexOf(x2) === -1).map((x2) => [
2970
+ [
2971
+ x2,
2972
+ dy
2973
+ ],
2974
+ [
2975
+ x2,
2976
+ dy + h
2977
+ ]
2978
+ ]),
2979
+ ...yMinor.filter((y2) => yMajor.indexOf(y2) === -1).map((y2) => [
2980
+ [
2981
+ dx,
2982
+ y2
2983
+ ],
2984
+ [
2985
+ dx + w,
2986
+ y2
2987
+ ]
2988
+ ])
2989
+ ];
2990
+ paths.push({
2991
+ id: "minor",
2992
+ class: "dx-minor",
2993
+ path: minor.map((line5) => createLine4(line5)).join()
2994
+ });
2995
+ }
2996
+ }
2997
+ return paths;
2998
+ };
2999
+ var defaultGridOptions = {
3000
+ visible: true,
3001
+ axis: true,
3002
+ grid: true
3003
+ };
3004
+ var GridController = class {
3005
+ _ref;
3006
+ _context;
3007
+ _options;
3008
+ _visible = false;
3009
+ constructor(_ref, _context, _options) {
3010
+ this._ref = _ref;
3011
+ this._context = _context;
3012
+ this._options = _options;
3013
+ this._visible = this._options.visible ?? true;
3014
+ }
3015
+ get ref() {
3016
+ return this._ref;
3017
+ }
3018
+ get visible() {
3019
+ return this._visible;
3020
+ }
3021
+ draw() {
3022
+ if (!this._context.size) {
3023
+ return;
3024
+ }
3025
+ const paths = this._visible ? createGrid(this._context, this._options) : [];
3026
+ const root = select5(this._ref.current);
3027
+ root.selectAll("path").data(paths, (path) => path.id).join("path").style("pointer-events", "none").attr("d", (d) => d.path).attr("class", (d) => d.class);
3028
+ const transform = this._context.scale.transform;
3029
+ if (transform) {
3030
+ root.attr("transform", transform);
3031
+ root.attr("stroke-width", 1 / transform.k);
3032
+ }
3033
+ }
3034
+ setVisible(visible) {
3035
+ this._visible = visible;
3036
+ this.draw();
3037
+ return this;
3038
+ }
3039
+ };
3040
+ var useGrid = (options = defaultGridOptions) => {
3041
+ const ref = useRef(null);
3042
+ const context = useSvgContext();
3043
+ const controller = useMemo(() => new GridController(ref, context, options), []);
3044
+ useEffect(() => context.resized.on(() => controller.draw()), [
3045
+ context,
3046
+ controller
3047
+ ]);
3048
+ return controller;
3049
+ };
3050
+
3051
+ // src/hooks/useZoom.ts
3052
+ import { select as select6, zoom, zoomIdentity as zoomIdentity2 } from "d3";
3053
+ import defaultsDeep from "lodash.defaultsdeep";
3054
+ import { useEffect as useEffect2, useMemo as useMemo2, useRef as useRef2 } from "react";
3055
+ var defaultZoomOptions = {
3056
+ enabled: true,
3057
+ extent: [
3058
+ 1 / 4,
3059
+ 4
3060
+ ],
3061
+ onDblClick: (zoom2) => zoom2.reset()
3062
+ };
3063
+ var ZoomHandler = class {
3064
+ _ref;
3065
+ _context;
3066
+ _zoom;
3067
+ _options;
3068
+ _enabled;
3069
+ constructor(_ref, _context, options) {
3070
+ this._ref = _ref;
3071
+ this._context = _context;
3072
+ this._options = defaultsDeep({}, options, defaultZoomOptions);
3073
+ this._enabled = this._options.enabled ?? true;
3074
+ this._zoom = zoom().scaleExtent(this._options.extent ?? defaultZoomOptions.extent);
3075
+ }
3076
+ /**
3077
+ * Gets the reference which the transform is applied to.
3078
+ */
3079
+ get ref() {
3080
+ return this._ref;
3081
+ }
3082
+ get zoom() {
3083
+ return this._zoom;
3084
+ }
3085
+ init() {
3086
+ this.setEnabled(this._enabled);
3087
+ this.reset(0);
3088
+ return this;
3089
+ }
3090
+ setEnabled(enable) {
3091
+ if (enable) {
3092
+ select6(this._context.svg).call(this._zoom).on(
3093
+ "dblclick.zoom",
3094
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
3095
+ // @ts-ignore
3096
+ this._options?.onDblClick ? () => {
3097
+ this._options?.onDblClick(this);
3098
+ } : null
3099
+ );
3100
+ } else {
3101
+ select6(this._context.svg).on(".zoom", null);
3102
+ }
3103
+ this._enabled = enable;
3104
+ return this;
3105
+ }
3106
+ reset(duration = 500) {
3107
+ const scale = 1;
3108
+ const transform = zoomIdentity2.scale(scale);
3109
+ select6(this._context.svg).transition().duration(duration).call(this._zoom.transform, transform);
3110
+ return this;
3111
+ }
3112
+ };
3113
+ var useZoom = (options = defaultZoomOptions) => {
3114
+ const context = useSvgContext();
3115
+ const ref = useRef2(null);
3116
+ const handler = useMemo2(() => new ZoomHandler(ref, context, options), [
3117
+ context,
3118
+ options
3119
+ ]);
3120
+ useEffect2(() => {
3121
+ handler.zoom.on("zoom", ({ transform }) => {
3122
+ context.setTransform(transform);
3123
+ select6(ref.current).attr("transform", transform);
3124
+ });
3125
+ handler.init();
3126
+ }, [
3127
+ handler
3128
+ ]);
3129
+ return handler;
3130
+ };
3131
+
1143
3132
  // src/components/Graph/Graph.tsx
1144
- var __dxlog_file8 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/components/Graph/Graph.tsx";
1145
- var GraphInner = ({ classNames, model, projector: _projector, renderer: _renderer, drag: drag2, arrows, onSelect, onInspect, ...props }, forwardedRef) => {
1146
- var _effect = _useSignals();
1147
- try {
1148
- const context = useSvgContext();
1149
- const graphRef = useRef(null);
1150
- const { projector, renderer } = useMemo(() => {
1151
- let projector2 = _projector;
1152
- if (!projector2) {
1153
- projector2 = new GraphForceProjector(context);
1154
- }
1155
- let renderer2 = _renderer;
1156
- if (!renderer2) {
1157
- renderer2 = new GraphRenderer(context, graphRef, {
1158
- ...props,
1159
- // TODO(burdon): Replace drag when projector is updated.
1160
- drag: drag2 ? createGraphDrag(context, projector2) : void 0,
1161
- arrows: {
1162
- end: arrows
1163
- },
1164
- onNodeClick: onSelect ? (node, event) => onSelect(node, event) : void 0,
1165
- onNodePointerEnter: onInspect ? (node, event) => onInspect(node, event) : void 0
1166
- });
1167
- }
1168
- return {
1169
- projector: projector2,
1170
- renderer: renderer2
1171
- };
1172
- }, [
1173
- context,
1174
- _projector,
1175
- _renderer,
1176
- drag2
1177
- ]);
1178
- useImperativeHandle(forwardedRef, () => ({
1179
- refresh: () => {
1180
- projector.updateData(model?.graph);
1181
- },
1182
- repaint: () => {
1183
- renderer.render(projector.layout);
1184
- },
1185
- findNode: (id) => {
1186
- return graphRef.current?.querySelector(`g[data-id="${id}"]`);
1187
- }
1188
- }), [
1189
- model,
1190
- projector,
1191
- renderer
1192
- ]);
1193
- useEffect(() => {
1194
- return combine(effect(() => {
3133
+ var __dxlog_file13 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/components/Graph/Graph.tsx";
3134
+ var GraphInner = ({ classNames, model, projector: projectorProp, renderer: rendererProp, drag: drag2, arrows, onSelect, onInspect, onPointerMove, onPointerLeave, ...props }, forwardedRef) => {
3135
+ const context = useSvgContext();
3136
+ const graphRef = useRef3(null);
3137
+ const { projector, renderer } = useMemo3(() => {
3138
+ let projector2 = projectorProp;
3139
+ if (!projector2) {
3140
+ projector2 = new GraphForceProjector(context);
3141
+ }
3142
+ let renderer2 = rendererProp;
3143
+ if (!renderer2) {
3144
+ renderer2 = new GraphRenderer(context, graphRef, {
3145
+ ...props,
3146
+ // TODO(burdon): Replace drag when projector is updated.
3147
+ drag: drag2 ? createGraphDrag(context, projector2) : void 0,
3148
+ arrows: {
3149
+ end: arrows
3150
+ },
3151
+ // Projector intercept first so built-in interactions (e.g. cluster collapse) can
3152
+ // run without consumers wiring them. Returning true suppresses the user handler.
3153
+ onNodeClick: (node, event) => {
3154
+ if (projector2.handleNodeClick(node, event)) {
3155
+ return;
3156
+ }
3157
+ onSelect?.(node, event);
3158
+ },
3159
+ onNodePointerEnter: onInspect ? (node, event) => onInspect(node, event) : void 0,
3160
+ // Pair pointerenter with leave so consumers can clear hover-driven previews.
3161
+ // The callback receives `null` on leave (event still carries the source).
3162
+ onNodePointerLeave: onInspect ? (_node, event) => onInspect(null, event) : void 0
3163
+ });
3164
+ }
3165
+ return {
3166
+ projector: projector2,
3167
+ renderer: renderer2
3168
+ };
3169
+ }, [
3170
+ context,
3171
+ projectorProp,
3172
+ rendererProp,
3173
+ drag2,
3174
+ arrows,
3175
+ onSelect,
3176
+ onInspect,
3177
+ props.labels,
3178
+ props.subgraphs,
3179
+ props.attributes,
3180
+ props.renderNode,
3181
+ props.highlightOnHover,
3182
+ props.applyNode,
3183
+ props.edgeOpacity
3184
+ ]);
3185
+ useImperativeHandle(forwardedRef, () => ({
3186
+ refresh: () => {
3187
+ projector.updateData(model?.graph);
3188
+ },
3189
+ repaint: () => {
3190
+ renderer.render(projector.layout);
3191
+ },
3192
+ findNode: (id) => {
3193
+ return graphRef.current?.querySelector(`g[data-id="${id}"]`);
3194
+ }
3195
+ }), [
3196
+ model,
3197
+ projector,
3198
+ renderer
3199
+ ]);
3200
+ useEffect3(() => {
3201
+ const cleanup = combine(
3202
+ // Subscribe to model changes if reactive model.
3203
+ model ? model.subscribe(() => {
1195
3204
  projector.updateData(model?.graph);
1196
- }), projector.updated.on(({ layout }) => {
3205
+ }) : void 0,
3206
+ projector.updated.on(({ layout, kind }) => {
1197
3207
  try {
1198
- renderer.render(layout);
3208
+ if (kind === "positions") {
3209
+ renderer.applyPositions(layout);
3210
+ } else {
3211
+ renderer.render(layout);
3212
+ }
1199
3213
  } catch (error) {
3214
+ log11.catch(error, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file13, L: 95, S: void 0 });
1200
3215
  void projector.stop();
1201
- log6.catch(error, void 0, {
1202
- F: __dxlog_file8,
1203
- L: 108,
1204
- S: void 0,
1205
- C: (f, a) => f(...a)
1206
- });
1207
3216
  }
1208
- }));
1209
- }, [
1210
- model,
1211
- projector,
1212
- renderer
1213
- ]);
1214
- useEffect(() => {
1215
- void projector.start();
1216
- return () => {
1217
- void projector.stop();
1218
- };
1219
- }, [
1220
- projector
1221
- ]);
1222
- return /* @__PURE__ */ React.createElement("g", {
1223
- ref: graphRef,
1224
- className: mx("dx-graph", classNames)
1225
- });
1226
- } finally {
1227
- _effect.f();
1228
- }
3217
+ })
3218
+ );
3219
+ projector.updateData(model?.graph);
3220
+ return cleanup;
3221
+ }, [
3222
+ model,
3223
+ projector,
3224
+ renderer
3225
+ ]);
3226
+ useEffect3(() => {
3227
+ void projector.start();
3228
+ return () => {
3229
+ void projector.stop();
3230
+ };
3231
+ }, [
3232
+ projector
3233
+ ]);
3234
+ useEffect3(() => {
3235
+ if (!onPointerMove && !onPointerLeave) {
3236
+ return;
3237
+ }
3238
+ const svg = context.svg;
3239
+ if (!svg) {
3240
+ return;
3241
+ }
3242
+ const previousPointerEvents = svg.style.pointerEvents;
3243
+ svg.style.pointerEvents = "all";
3244
+ const handleMove = (event) => {
3245
+ if (!onPointerMove) {
3246
+ return;
3247
+ }
3248
+ const target = graphRef.current;
3249
+ const ctm = target?.getScreenCTM();
3250
+ if (!target || !ctm) {
3251
+ return;
3252
+ }
3253
+ const point = svg.createSVGPoint();
3254
+ point.x = event.clientX;
3255
+ point.y = event.clientY;
3256
+ const modelPoint = point.matrixTransform(ctm.inverse());
3257
+ onPointerMove({
3258
+ x: modelPoint.x,
3259
+ y: modelPoint.y
3260
+ }, event);
3261
+ };
3262
+ const handleLeave = (event) => {
3263
+ onPointerLeave?.(event);
3264
+ };
3265
+ svg.addEventListener("pointermove", handleMove);
3266
+ svg.addEventListener("pointerleave", handleLeave);
3267
+ return () => {
3268
+ svg.removeEventListener("pointermove", handleMove);
3269
+ svg.removeEventListener("pointerleave", handleLeave);
3270
+ svg.style.pointerEvents = previousPointerEvents;
3271
+ };
3272
+ }, [
3273
+ context,
3274
+ onPointerMove,
3275
+ onPointerLeave
3276
+ ]);
3277
+ return /* @__PURE__ */ React.createElement("g", {
3278
+ ref: graphRef,
3279
+ className: mx("dx-graph", classNames)
3280
+ });
1229
3281
  };
1230
3282
  var Graph = /* @__PURE__ */ forwardRef(GraphInner);
1231
3283
 
1232
3284
  // src/components/Mesh/Mesh.tsx
1233
- import { geoPath, select as select5 } from "d3";
1234
- import { useEffect as useEffect2 } from "react";
3285
+ import { geoPath, select as select7 } from "d3";
3286
+ import { useEffect as useEffect4 } from "react";
1235
3287
  import { feature, mesh } from "topojson-client";
1236
3288
  var Mesh = ({ radius = 16, value = 0.5 }) => {
1237
3289
  const { svg, size } = useSvgContext();
1238
- useEffect2(() => {
1239
- if (size) {
1240
- const topology = hexTopology(size, radius, value);
1241
- const projection = hexProjection(radius);
1242
- const path = geoPath().projection(projection);
1243
- select5(svg).append("g").attr("class", "hexagon").selectAll("path").data(topology.objects.hexagons.geometries).enter().append("path").attr("d", (d) => path(feature(topology, d))).attr("class", (d) => d.properties?.fill ? "fill" : null);
1244
- select5(svg).append("path").datum(mesh(topology, topology.objects.hexagons)).attr("class", "mesh").attr("d", path);
1245
- const redraw = (border) => {
1246
- border.attr("d", path(mesh(topology, topology.objects.hexagons, (a, b) => a.properties.fill !== b.properties.fill)));
1247
- };
1248
- select5(svg).append("path").attr("class", "border").call(redraw);
3290
+ useEffect4(() => {
3291
+ if (!size) {
3292
+ return;
1249
3293
  }
3294
+ const topology = hexTopology(size, radius, value);
3295
+ const projection = hexProjection(radius);
3296
+ const path = geoPath().projection(projection);
3297
+ const root = select7(svg).selectAll("g.dx-mesh-root").data([
3298
+ null
3299
+ ]).join("g").classed("dx-mesh-root", true);
3300
+ root.selectAll("*").remove();
3301
+ 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);
3302
+ root.append("path").datum(mesh(topology, topology.objects.hexagons)).attr("class", "mesh").attr("d", path);
3303
+ const redraw = (border) => {
3304
+ border.attr("d", path(mesh(topology, topology.objects.hexagons, (a, b) => a.properties.fill !== b.properties.fill)));
3305
+ };
3306
+ root.append("path").attr("class", "border").call(redraw);
1250
3307
  }, [
1251
3308
  svg,
1252
3309
  size,
1253
- value
3310
+ value,
3311
+ radius
1254
3312
  ]);
1255
3313
  return null;
1256
3314
  };
@@ -1361,116 +3419,181 @@ var hexProjection = (radius) => {
1361
3419
  };
1362
3420
  };
1363
3421
 
3422
+ // src/components/SVG/FPS.tsx
3423
+ import React2, { useEffect as useEffect5, useState } from "react";
3424
+ import { mx as mx2 } from "@dxos/ui-theme";
3425
+ var PADDING = 8;
3426
+ var FPS = ({ classNames, corner = "top-right", margin = PADDING }) => {
3427
+ const context = useSvgContext();
3428
+ const [size, setSize] = useState(context.size);
3429
+ const [fps, setFps] = useState(null);
3430
+ useEffect5(() => {
3431
+ setSize(context.size);
3432
+ return context.resized.on((ctx) => setSize(ctx.size));
3433
+ }, [
3434
+ context
3435
+ ]);
3436
+ useEffect5(() => {
3437
+ let frames = 0;
3438
+ let windowStart = performance.now();
3439
+ let rafId = 0;
3440
+ const loop = (now) => {
3441
+ frames++;
3442
+ const elapsed = now - windowStart;
3443
+ if (elapsed >= 1e3) {
3444
+ setFps(frames * 1e3 / elapsed);
3445
+ frames = 0;
3446
+ windowStart = now;
3447
+ }
3448
+ rafId = requestAnimationFrame(loop);
3449
+ };
3450
+ rafId = requestAnimationFrame(loop);
3451
+ return () => cancelAnimationFrame(rafId);
3452
+ }, []);
3453
+ if (!size) {
3454
+ return null;
3455
+ }
3456
+ const halfW = size.width / 2;
3457
+ const halfH = size.height / 2;
3458
+ const left = corner === "top-left" || corner === "bottom-left";
3459
+ const top = corner === "top-left" || corner === "top-right";
3460
+ const x = left ? -halfW + margin : halfW - margin;
3461
+ const y = top ? -halfH + margin : halfH - margin;
3462
+ const textAnchor = left ? "start" : "end";
3463
+ const dominantBaseline = top ? "hanging" : "alphabetic";
3464
+ return /* @__PURE__ */ React2.createElement("g", {
3465
+ className: "dx-fps",
3466
+ style: {
3467
+ pointerEvents: "none"
3468
+ }
3469
+ }, /* @__PURE__ */ React2.createElement("text", {
3470
+ x,
3471
+ y,
3472
+ textAnchor,
3473
+ dominantBaseline,
3474
+ className: mx2("font-mono text-xs fill-neutral-500 tabular-nums", classNames)
3475
+ }, fps == null ? "\u2014 fps" : `${fps.toFixed(0)} fps`));
3476
+ };
3477
+
1364
3478
  // src/components/SVG/Grid.tsx
1365
- import { useSignals as _useSignals2 } from "@preact-signals/safe-react/tracking";
1366
- import React2 from "react";
1367
- import { mx as mx2 } from "@dxos/react-ui-theme";
3479
+ import React3 from "react";
3480
+ import { mx as mx3 } from "@dxos/ui-theme";
1368
3481
  var Grid = ({ classNames, ...props }) => {
1369
- var _effect = _useSignals2();
1370
- try {
1371
- const { ref } = useGrid(props);
1372
- return /* @__PURE__ */ React2.createElement("g", {
1373
- ref,
1374
- className: mx2("dx-grid", classNames)
1375
- });
1376
- } finally {
1377
- _effect.f();
1378
- }
3482
+ const { ref } = useGrid(props);
3483
+ return /* @__PURE__ */ React3.createElement("g", {
3484
+ ref,
3485
+ className: mx3("dx-grid", classNames)
3486
+ });
1379
3487
  };
1380
3488
 
1381
3489
  // src/components/SVG/Markers.tsx
1382
- import { useSignals as _useSignals3 } from "@preact-signals/safe-react/tracking";
1383
- import { select as select6 } from "d3";
1384
- import React3, { useEffect as useEffect3, useRef as useRef2 } from "react";
3490
+ import { select as select8 } from "d3";
3491
+ import React4, { useEffect as useEffect6, useRef as useRef4 } from "react";
1385
3492
  var Markers = ({ arrowSize, className }) => {
1386
- var _effect = _useSignals3();
1387
- try {
1388
- const ref = useRef2(null);
1389
- useEffect3(() => {
1390
- select6(ref.current).call(createMarkers({
1391
- arrowSize
1392
- }));
1393
- }, [
1394
- ref
1395
- ]);
1396
- return /* @__PURE__ */ React3.createElement("defs", {
1397
- ref,
1398
- className
1399
- });
1400
- } finally {
1401
- _effect.f();
1402
- }
3493
+ const ref = useRef4(null);
3494
+ useEffect6(() => {
3495
+ select8(ref.current).call(createMarkers({
3496
+ arrowSize
3497
+ }));
3498
+ }, [
3499
+ ref
3500
+ ]);
3501
+ return /* @__PURE__ */ React4.createElement("defs", {
3502
+ ref,
3503
+ className
3504
+ });
1403
3505
  };
1404
3506
 
1405
3507
  // src/components/SVG/Root.tsx
1406
- import { useSignals as _useSignals4 } from "@preact-signals/safe-react/tracking";
1407
- import { select as select7 } from "d3";
1408
- import React4, { forwardRef as forwardRef2, useEffect as useEffect4, useImperativeHandle as useImperativeHandle2, useMemo as useMemo2 } from "react";
1409
- import { useResizeDetector } from "react-resize-detector";
1410
- import { mx as mx3 } from "@dxos/react-ui-theme";
1411
- var Root = /* @__PURE__ */ forwardRef2(({ classNames, children, ...props }, ref) => {
1412
- var _effect = _useSignals4();
1413
- try {
1414
- const { ref: containerRef, width = 0, height = 0 } = useResizeDetector({
1415
- refreshRate: 200
3508
+ import { select as select9 } from "d3";
3509
+ import React5, { forwardRef as forwardRef2, useEffect as useEffect7, useImperativeHandle as useImperativeHandle2, useMemo as useMemo4, useState as useState2 } from "react";
3510
+ import { composableProps } from "@dxos/react-ui";
3511
+ import { mx as mx4 } from "@dxos/ui-theme";
3512
+ var Root = /* @__PURE__ */ forwardRef2(({ children, scale, centered, ...props }, ref) => {
3513
+ const [container, setContainer] = useState2(null);
3514
+ const [size, setSize] = useState2({
3515
+ width: 0,
3516
+ height: 0
3517
+ });
3518
+ useEffect7(() => {
3519
+ if (!container) {
3520
+ return;
3521
+ }
3522
+ const rect = container.getBoundingClientRect();
3523
+ setSize({
3524
+ width: rect.width,
3525
+ height: rect.height
1416
3526
  });
1417
- const context = useMemo2(() => {
1418
- return new SVGContext(props);
1419
- }, [
1420
- props.scale,
1421
- props.centered
1422
- ]);
1423
- useImperativeHandle2(ref, () => context, [
1424
- context
1425
- ]);
1426
- useEffect4(() => {
1427
- if (width && height) {
1428
- context.setSize({
1429
- width,
1430
- height
1431
- });
1432
- select7(context.svg).attr("display", "block").attr("viewBox", context.viewBox).attr("width", width).attr("height", height);
3527
+ const observer = new ResizeObserver((entries) => {
3528
+ const entry = entries[0];
3529
+ if (!entry) {
3530
+ return;
1433
3531
  }
1434
- }, [
1435
- context,
1436
- width,
1437
- height
1438
- ]);
1439
- const ready = width !== 0 && height !== 0;
1440
- return /* @__PURE__ */ React4.createElement(SVGContextProvider, {
1441
- value: context
1442
- }, /* @__PURE__ */ React4.createElement("div", {
1443
- ref: containerRef,
1444
- className: mx3("grid grow overflow-hidden", classNames)
1445
- }, /* @__PURE__ */ React4.createElement("svg", {
1446
- xmlns: "http://www.w3.org/2000/svg",
1447
- ref: context.ref,
1448
- className: mx3("transition-opacity opacity-0 duration-1000", ready && "opacity-100")
1449
- }, ready && children)));
1450
- } finally {
1451
- _effect.f();
1452
- }
3532
+ const { width: width2, height: height2 } = entry.contentRect;
3533
+ setSize((prev) => prev.width === width2 && prev.height === height2 ? prev : {
3534
+ width: width2,
3535
+ height: height2
3536
+ });
3537
+ });
3538
+ observer.observe(container);
3539
+ return () => observer.disconnect();
3540
+ }, [
3541
+ container
3542
+ ]);
3543
+ const { width, height } = size;
3544
+ const context = useMemo4(() => {
3545
+ return new SVGContext({
3546
+ scale,
3547
+ centered
3548
+ });
3549
+ }, [
3550
+ scale,
3551
+ centered
3552
+ ]);
3553
+ useImperativeHandle2(ref, () => context, [
3554
+ context
3555
+ ]);
3556
+ useEffect7(() => {
3557
+ if (width && height) {
3558
+ context.setSize({
3559
+ width,
3560
+ height
3561
+ });
3562
+ select9(context.svg).attr("display", "block").attr("viewBox", context.viewBox).attr("width", width).attr("height", height);
3563
+ }
3564
+ }, [
3565
+ context,
3566
+ width,
3567
+ height
3568
+ ]);
3569
+ const ready = width !== 0 && height !== 0;
3570
+ return /* @__PURE__ */ React5.createElement(SVGContextProvider, {
3571
+ value: context
3572
+ }, /* @__PURE__ */ React5.createElement("div", {
3573
+ ...composableProps(props, {
3574
+ classNames: "dx-expander"
3575
+ }),
3576
+ ref: setContainer
3577
+ }, /* @__PURE__ */ React5.createElement("svg", {
3578
+ xmlns: "http://www.w3.org/2000/svg",
3579
+ ref: context.ref,
3580
+ className: mx4("transition-opacity opacity-0 duration-1000", ready && "opacity-100")
3581
+ }, ready && children)));
1453
3582
  });
1454
3583
 
1455
3584
  // src/components/SVG/Zoom.tsx
1456
- import { useSignals as _useSignals5 } from "@preact-signals/safe-react/tracking";
1457
- import React5, { memo, useMemo as useMemo3 } from "react";
3585
+ import React6, { memo, useMemo as useMemo5 } from "react";
1458
3586
  var Zoom = /* @__PURE__ */ memo(({ extent, className, children }) => {
1459
- var _effect = _useSignals5();
1460
- try {
1461
- const options = useMemo3(() => ({
1462
- extent
1463
- }), [
1464
- JSON.stringify(extent)
1465
- ]);
1466
- const zoom = useZoom(options);
1467
- return /* @__PURE__ */ React5.createElement("g", {
1468
- ref: zoom.ref,
1469
- className
1470
- }, children);
1471
- } finally {
1472
- _effect.f();
1473
- }
3587
+ const options = useMemo5(() => ({
3588
+ extent
3589
+ }), [
3590
+ JSON.stringify(extent)
3591
+ ]);
3592
+ const zoom2 = useZoom(options);
3593
+ return /* @__PURE__ */ React6.createElement("g", {
3594
+ ref: zoom2.ref,
3595
+ className
3596
+ }, children);
1474
3597
  });
1475
3598
 
1476
3599
  // src/components/SVG/SVG.tsx
@@ -1480,11 +3603,12 @@ var SVG = {
1480
3603
  Markers,
1481
3604
  Mesh,
1482
3605
  Zoom,
1483
- Graph
3606
+ Graph,
3607
+ FPS
1484
3608
  };
1485
3609
 
1486
3610
  // src/fx/pulsar.ts
1487
- import { easeCubicOut as easeCubicOut2, select as select8 } from "d3";
3611
+ import { easeCubicOut as easeCubicOut2, select as select10 } from "d3";
1488
3612
  var PROP_PULSAR_ACTIVE = "dx-pulsar-active";
1489
3613
  (function(Pulsar2) {
1490
3614
  Pulsar2.has = (group) => group.select("circle.dx-pulsar").empty();
@@ -1499,27 +3623,37 @@ var PROP_PULSAR_ACTIVE = "dx-pulsar-active";
1499
3623
  var startAnimation = (pulsar, r, options = {}) => {
1500
3624
  const { period = 1e3, duration = 800, ratio = 3 } = options;
1501
3625
  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() {
1502
- if (select8(this).property(PROP_PULSAR_ACTIVE)) {
1503
- startAnimation(select8(this), r, options);
3626
+ if (select10(this).property(PROP_PULSAR_ACTIVE)) {
3627
+ startAnimation(select10(this), r, options);
1504
3628
  } else {
1505
- select8(this).remove();
3629
+ select10(this).remove();
1506
3630
  }
1507
3631
  });
1508
3632
  };
1509
3633
  var Pulsar;
1510
3634
  export {
3635
+ CLUSTER_NODE_TYPE_GROUP,
3636
+ CLUSTER_NODE_TYPE_LEAF,
3637
+ CLUSTER_NODE_TYPE_ROOT,
1511
3638
  EventEmitter,
1512
3639
  FractionUtil,
1513
3640
  Graph,
3641
+ GraphBundleProjector,
3642
+ GraphClusterProjector,
1514
3643
  GraphForceProjector,
1515
3644
  GraphHierarchicalProjector,
1516
- GraphInner,
3645
+ GraphLatticeProjector,
3646
+ GraphPlexusProjector,
1517
3647
  GraphProjector,
1518
3648
  GraphRadialProjector,
1519
3649
  GraphRelationalProjector,
1520
3650
  GraphRenderer,
3651
+ GraphSwarmProjector,
1521
3652
  GridController,
1522
3653
  Mesh,
3654
+ PLEXUS_NODE_TYPE_FOCUS,
3655
+ PLEXUS_NODE_TYPE_OBJECT,
3656
+ PLEXUS_NODE_TYPE_RELATION,
1523
3657
  Projector,
1524
3658
  Pulsar,
1525
3659
  Renderer,
@@ -1530,6 +3664,9 @@ export {
1530
3664
  Screen,
1531
3665
  Vector,
1532
3666
  ZoomHandler,
3667
+ appendRadialGroupLabel,
3668
+ appendRadialLeafLabel,
3669
+ appendRootLabel,
1533
3670
  createGraphDrag,
1534
3671
  createMarkers,
1535
3672
  defaultForceOptions,