@dxos/react-ui-graph 0.8.4-main.b97322e → 0.8.4-main.bc2380dfbc

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 (118) 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 +1129 -298
  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 +1129 -298
  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 +4 -5
  18. package/dist/types/src/components/Graph/Graph.d.ts.map +1 -1
  19. package/dist/types/src/components/Graph/Graph.stories.d.ts +20 -11
  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 +9 -5
  23. package/dist/types/src/components/Mesh/Mesh.stories.d.ts.map +1 -1
  24. package/dist/types/src/components/SVG/Grid.d.ts.map +1 -1
  25. package/dist/types/src/components/SVG/Markers.d.ts.map +1 -1
  26. package/dist/types/src/components/SVG/Root.d.ts +8 -4
  27. package/dist/types/src/components/SVG/Root.d.ts.map +1 -1
  28. package/dist/types/src/components/SVG/SVG.d.ts +14 -17
  29. package/dist/types/src/components/SVG/SVG.d.ts.map +1 -1
  30. package/dist/types/src/components/SVG/Svg.stories.d.ts +11 -9
  31. package/dist/types/src/components/SVG/Svg.stories.d.ts.map +1 -1
  32. package/dist/types/src/components/SVG/Zoom.d.ts.map +1 -1
  33. package/dist/types/src/fx/fx.stories.d.ts +16 -15
  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-drag.d.ts +1 -1
  38. package/dist/types/src/graph/projector/graph-drag.d.ts.map +1 -1
  39. package/dist/types/src/graph/projector/graph-force-projector.d.ts +2 -2
  40. package/dist/types/src/graph/projector/graph-force-projector.d.ts.map +1 -1
  41. package/dist/types/src/graph/projector/graph-forces.d.ts.map +1 -1
  42. package/dist/types/src/graph/projector/graph-hierarchical-projector.d.ts +2 -2
  43. package/dist/types/src/graph/projector/graph-hierarchical-projector.d.ts.map +1 -1
  44. package/dist/types/src/graph/projector/graph-projector.d.ts +4 -4
  45. package/dist/types/src/graph/projector/graph-projector.d.ts.map +1 -1
  46. package/dist/types/src/graph/projector/graph-radial-projector.d.ts +2 -2
  47. package/dist/types/src/graph/projector/graph-radial-projector.d.ts.map +1 -1
  48. package/dist/types/src/graph/projector/graph-relational-projector.d.ts +2 -2
  49. package/dist/types/src/graph/projector/graph-relational-projector.d.ts.map +1 -1
  50. package/dist/types/src/graph/projector/projector.d.ts.map +1 -1
  51. package/dist/types/src/graph/renderer/bullets.d.ts.map +1 -1
  52. package/dist/types/src/graph/renderer/graph-renderer.d.ts +1 -1
  53. package/dist/types/src/graph/renderer/graph-renderer.d.ts.map +1 -1
  54. package/dist/types/src/graph/renderer/renderer.d.ts +6 -1
  55. package/dist/types/src/graph/renderer/renderer.d.ts.map +1 -1
  56. package/dist/types/src/graph/types.d.ts +1 -1
  57. package/dist/types/src/graph/types.d.ts.map +1 -1
  58. package/dist/types/src/hooks/useGrid.d.ts.map +1 -1
  59. package/dist/types/src/hooks/useStateRef.d.ts.map +1 -1
  60. package/dist/types/src/hooks/useSvgContext.d.ts.map +1 -1
  61. package/dist/types/src/hooks/useZoom.d.ts.map +1 -1
  62. package/dist/types/src/stories/hooks.stories.d.ts +14 -9
  63. package/dist/types/src/stories/hooks.stories.d.ts.map +1 -1
  64. package/dist/types/src/testing/data.d.ts +5 -5
  65. package/dist/types/src/testing/data.d.ts.map +1 -1
  66. package/dist/types/src/testing/hooks.d.ts +2 -16
  67. package/dist/types/src/testing/hooks.d.ts.map +1 -1
  68. package/dist/types/src/testing/model.d.ts +4 -8
  69. package/dist/types/src/testing/model.d.ts.map +1 -1
  70. package/dist/types/src/util/events.d.ts.map +1 -1
  71. package/dist/types/src/util/fraction.d.ts +1 -1
  72. package/dist/types/src/util/fraction.d.ts.map +1 -1
  73. package/dist/types/src/util/points.d.ts.map +1 -1
  74. package/dist/types/src/util/scale.d.ts.map +1 -1
  75. package/dist/types/src/util/screen.d.ts +1 -1
  76. package/dist/types/src/util/screen.d.ts.map +1 -1
  77. package/dist/types/src/util/vector.d.ts +1 -1
  78. package/dist/types/src/util/vector.d.ts.map +1 -1
  79. package/dist/types/tsconfig.tsbuildinfo +1 -1
  80. package/package.json +33 -35
  81. package/src/components/Graph/Graph.stories.tsx +73 -69
  82. package/src/components/Graph/Graph.tsx +25 -20
  83. package/src/components/Mesh/Mesh.stories.tsx +16 -13
  84. package/src/components/Mesh/Mesh.tsx +44 -36
  85. package/src/components/SVG/Grid.tsx +2 -2
  86. package/src/components/SVG/Markers.tsx +1 -1
  87. package/src/components/SVG/Root.tsx +41 -9
  88. package/src/components/SVG/SVG.tsx +13 -4
  89. package/src/components/SVG/Svg.stories.tsx +23 -19
  90. package/src/components/SVG/Zoom.tsx +1 -1
  91. package/src/fx/fx.stories.tsx +18 -15
  92. package/src/graph/projector/graph-drag.ts +1 -1
  93. package/src/graph/projector/graph-force-projector.ts +2 -2
  94. package/src/graph/projector/graph-forces.ts +1 -1
  95. package/src/graph/projector/graph-hierarchical-projector.ts +4 -4
  96. package/src/graph/projector/graph-projector.ts +5 -5
  97. package/src/graph/projector/graph-radial-projector.ts +3 -3
  98. package/src/graph/projector/graph-relational-projector.ts +4 -4
  99. package/src/graph/projector/projector.ts +1 -1
  100. package/src/graph/renderer/bullets.ts +1 -1
  101. package/src/graph/renderer/graph-renderer.ts +16 -7
  102. package/src/graph/renderer/renderer.ts +7 -5
  103. package/src/graph/types.ts +1 -1
  104. package/src/hooks/useStateRef.ts +1 -1
  105. package/src/hooks/useZoom.ts +1 -1
  106. package/src/stories/hooks.stories.tsx +49 -37
  107. package/src/testing/data.ts +14 -14
  108. package/src/testing/hooks.ts +5 -5
  109. package/src/testing/model.ts +6 -6
  110. package/src/util/fraction.ts +1 -1
  111. package/src/util/screen.test.ts +1 -1
  112. package/src/util/screen.ts +1 -1
  113. package/src/util/vector.ts +1 -1
  114. package/styles/graph.css +292 -43
  115. package/dist/lib/browser/chunk-MGVY54T5.mjs +0 -979
  116. package/dist/lib/browser/chunk-MGVY54T5.mjs.map +0 -7
  117. package/dist/lib/node-esm/chunk-X5YVOSXZ.mjs +0 -981
  118. package/dist/lib/node-esm/chunk-X5YVOSXZ.mjs.map +0 -7
@@ -1,31 +1,14 @@
1
1
  import {
2
- EventEmitter,
3
- FractionUtil,
4
- GridController,
5
- SVGContext,
6
- SVGContextProvider,
7
- Scale,
8
- Screen,
9
- Vector,
10
- ZoomHandler,
11
- getCircumferencePoints,
12
- normal,
13
- offsetPoints,
14
2
  useDynamicRef,
15
- useGrid,
16
3
  useStateRef,
17
- useSvgContext,
18
- useTimestamp,
19
- useZoom
20
- } from "./chunk-MGVY54T5.mjs";
4
+ useTimestamp
5
+ } from "./chunk-EASNZZ7B.mjs";
21
6
 
22
7
  // src/components/Graph/Graph.tsx
23
- import { useSignals as _useSignals } from "@preact-signals/safe-react/tracking";
24
- import { effect } from "@preact/signals-core";
25
- import React, { forwardRef, useEffect, useImperativeHandle, useMemo, useRef } from "react";
8
+ import React, { forwardRef, useEffect as useEffect3, useImperativeHandle, useMemo as useMemo3, useRef as useRef3 } from "react";
26
9
  import { combine } from "@dxos/async";
27
10
  import { log as log6 } from "@dxos/log";
28
- import { mx } from "@dxos/react-ui-theme";
11
+ import { mx } from "@dxos/ui-theme";
29
12
 
30
13
  // src/graph/markers.ts
31
14
  import { line, select } from "d3";
@@ -162,15 +145,7 @@ var createGraphDrag = (context, projector, options = defaultDragOptions) => {
162
145
  import { invariant } from "@dxos/invariant";
163
146
  var __dxlog_file = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/types.ts";
164
147
  var defaultIdAccessor = (node) => {
165
- invariant(node.id, void 0, {
166
- F: __dxlog_file,
167
- L: 18,
168
- S: void 0,
169
- A: [
170
- "node.id",
171
- ""
172
- ]
173
- });
148
+ invariant(node.id, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 10, S: void 0, A: ["node.id", ""] });
174
149
  return node.id;
175
150
  };
176
151
  var emptyGraph = {
@@ -178,11 +153,577 @@ var emptyGraph = {
178
153
  edges: []
179
154
  };
180
155
 
156
+ // src/util/events.ts
157
+ var EventEmitter = class {
158
+ _listeners = /* @__PURE__ */ new Set();
159
+ clear() {
160
+ this._listeners.clear();
161
+ }
162
+ on(onEvent) {
163
+ this._listeners.add(onEvent);
164
+ return () => this._listeners.delete(onEvent);
165
+ }
166
+ emit(value) {
167
+ this._listeners.forEach((listener) => listener(value));
168
+ }
169
+ };
170
+
171
+ // src/util/fraction.ts
172
+ import { invariant as invariant2 } from "@dxos/invariant";
173
+ var __dxlog_file2 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/util/fraction.ts";
174
+ var FractionUtil = class _FractionUtil {
175
+ /**
176
+ * Convert number to fraction.
177
+ * @param n
178
+ */
179
+ static toFraction = (n) => {
180
+ return typeof n === "number" ? [
181
+ n,
182
+ 1
183
+ ] : n;
184
+ };
185
+ static validate = (...fraction) => {
186
+ fraction.forEach(([n, d]) => {
187
+ if (!Number.isFinite(n) || !Number.isFinite(d) || d === 0) {
188
+ throw new Error(`Invalid fraction: ${n}/${d}`);
189
+ }
190
+ });
191
+ return fraction;
192
+ };
193
+ /**
194
+ * @param {Fraction}
195
+ * Convert to rounded-sm number.
196
+ */
197
+ static toNumber = ([n, d]) => {
198
+ _FractionUtil.validate([
199
+ n,
200
+ d
201
+ ]);
202
+ return n / d;
203
+ };
204
+ /**
205
+ * Round the number to the nearest fraction.
206
+ * Example: 3/5 => 1/1; 3/5 (precision 2) => 1/2.
207
+ * @param {Fraction}
208
+ * @param p Precision (e.g., 1/2, 1/4, etc.)
209
+ */
210
+ static round = ([n, d], p = 1) => {
211
+ _FractionUtil.validate([
212
+ n,
213
+ d
214
+ ]);
215
+ const v = Math.round(_FractionUtil.toNumber(_FractionUtil.divide([
216
+ n,
217
+ d
218
+ ], [
219
+ 1,
220
+ p
221
+ ])));
222
+ return _FractionUtil.simplify([
223
+ v,
224
+ p
225
+ ]);
226
+ };
227
+ //
228
+ // Boolean.
229
+ //
230
+ /**
231
+ * Tests if equals.
232
+ * @param n1
233
+ * @param n2
234
+ */
235
+ static equals = (n1, n2) => {
236
+ return n1[0] * n2[1] === n1[1] * n2[0];
237
+ };
238
+ /**
239
+ * @param {Fraction}
240
+ * True if zero.
241
+ */
242
+ static isZero = ([n]) => {
243
+ return n === 0;
244
+ };
245
+ //
246
+ // Normalization.
247
+ //
248
+ /**
249
+ * Simplify fraction finding LCDs.
250
+ * @param n
251
+ * @param d
252
+ */
253
+ static simplify = ([n, d]) => {
254
+ _FractionUtil.validate([
255
+ n,
256
+ d
257
+ ]);
258
+ if (d < 1) {
259
+ const t = n / d;
260
+ if (Math.floor(t) === t) {
261
+ n = t;
262
+ d = 1;
263
+ }
264
+ }
265
+ const fn = _FractionUtil.factors(n).reverse();
266
+ const fd = _FractionUtil.factors(d).reverse();
267
+ let c;
268
+ for (let i = 0; i < fn.length; i++) {
269
+ c = fd.find((n2) => n2 === fn[i]);
270
+ if (c) {
271
+ return [
272
+ n / c,
273
+ d / c
274
+ ];
275
+ }
276
+ }
277
+ return [
278
+ n,
279
+ d
280
+ ];
281
+ };
282
+ /**
283
+ * Return common factors.
284
+ * @param n
285
+ */
286
+ static factors = (n) => {
287
+ invariant2(Number.isFinite(n), `Invalid number: ${n}`, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 113, S: this, A: ["Number.isFinite(n)", "`Invalid number: ${n}`"] });
288
+ const factors = [
289
+ 1
290
+ ];
291
+ if (Number.isSafeInteger(n)) {
292
+ let f = 1;
293
+ do {
294
+ if (n % ++f === 0) {
295
+ factors.push(f);
296
+ }
297
+ } while (n / f > 1 && f < 100);
298
+ }
299
+ return factors;
300
+ };
301
+ //
302
+ // Basic arithmetic.
303
+ //
304
+ /**
305
+ * @param n1
306
+ * @param n2
307
+ */
308
+ static add = (n1, n2) => {
309
+ _FractionUtil.validate(n1, n2);
310
+ const d = n1[1] * n2[1];
311
+ const n = n1[0] * n2[1] + n2[0] * n1[1];
312
+ return _FractionUtil.simplify([
313
+ n,
314
+ d
315
+ ]);
316
+ };
317
+ /**
318
+ * @param n1
319
+ * @param n2
320
+ */
321
+ static subtract = (n1, n2) => {
322
+ _FractionUtil.validate(n1, n2);
323
+ const d = n1[1] * n2[1];
324
+ const n = n1[0] * n2[1] - n2[0] * n1[1];
325
+ return _FractionUtil.simplify([
326
+ n,
327
+ d
328
+ ]);
329
+ };
330
+ /**
331
+ * @param n1
332
+ * @param n2
333
+ */
334
+ static multiply = (n1, n2) => {
335
+ _FractionUtil.validate(n1, n2);
336
+ const n = n1[0] * n2[0];
337
+ const d = n1[1] * n2[1];
338
+ return _FractionUtil.simplify([
339
+ n,
340
+ d
341
+ ]);
342
+ };
343
+ /**
344
+ * @param n1
345
+ * @param n2
346
+ */
347
+ static divide = (n1, n2) => {
348
+ _FractionUtil.validate(n1, n2);
349
+ const n = n1[0] * n2[1];
350
+ const d = n1[1] * n2[0];
351
+ return _FractionUtil.simplify([
352
+ n,
353
+ d
354
+ ]);
355
+ };
356
+ };
357
+
358
+ // src/util/points.ts
359
+ var getCircumferencePoints = (p1, p2, r1 = 0, r2 = 0) => {
360
+ const [x1, y1] = p1;
361
+ const [x2, y2] = p2;
362
+ const theta = Math.atan2(x2 - x1, y2 - y1);
363
+ return [
364
+ [
365
+ // p1
366
+ x1 + (r1 === 0 ? 0 : r1 * Math.sin(theta)),
367
+ y1 + (r1 === 0 ? 0 : r1 * Math.cos(theta))
368
+ ],
369
+ [
370
+ // p2
371
+ x2 - (r2 === 0 ? 0 : r2 * Math.cos(Math.PI / 2 - theta)),
372
+ y2 - (r2 === 0 ? 0 : r2 * Math.sin(Math.PI / 2 - theta))
373
+ ]
374
+ ];
375
+ };
376
+ var normal = (p1, p2) => {
377
+ const dx = p2[0] - p1[0];
378
+ const dy = p2[1] - p1[1];
379
+ const len = Math.hypot(dx, dy);
380
+ return [
381
+ dy / len,
382
+ -dx / len
383
+ ];
384
+ };
385
+ var offsetPoints = (points, distance, closed) => {
386
+ const len = points.length;
387
+ const out = [];
388
+ for (let i = 0; i < len; i++) {
389
+ const p0 = points[(i - 1 + len) % len];
390
+ const p1 = points[i];
391
+ const p2 = points[(i + 1) % len];
392
+ const n1 = normal(p0, p1);
393
+ const n2 = normal(p1, p2);
394
+ const nx = (n1[0] + n2[0]) / 2;
395
+ const ny = (n1[1] + n2[1]) / 2;
396
+ const norm = Math.hypot(nx, ny) || 1;
397
+ out.push([
398
+ p1[0] + distance * nx / norm,
399
+ p1[1] + distance * ny / norm
400
+ ]);
401
+ if (!closed && (i === 0 || i === len - 1)) {
402
+ out[i] = [
403
+ p1[0] + distance * normal(p0, p1)[0],
404
+ p1[1] + distance * normal(p0, p1)[1]
405
+ ];
406
+ }
407
+ }
408
+ return out;
409
+ };
410
+
411
+ // src/util/scale.ts
412
+ import { zoomIdentity } from "d3";
413
+ var Scale = class {
414
+ _gridSize;
415
+ _transform;
416
+ constructor(_gridSize = 32) {
417
+ this._gridSize = _gridSize;
418
+ }
419
+ get gridSize() {
420
+ return this._gridSize;
421
+ }
422
+ get transform() {
423
+ return this._transform || zoomIdentity;
424
+ }
425
+ setTransform(transform) {
426
+ if (this._transform?.x === transform.x && this._transform?.y === transform.y && this._transform?.k === transform.k) {
427
+ return false;
428
+ }
429
+ this._transform = transform;
430
+ return true;
431
+ }
432
+ /**
433
+ * Translate a screen point to a logical point.
434
+ * @param point
435
+ */
436
+ translate(point) {
437
+ const { x, y, k } = this._transform ?? {
438
+ x: 0,
439
+ y: 0,
440
+ k: 1
441
+ };
442
+ return [
443
+ (point[0] - x) / k,
444
+ (point[1] - y) / k
445
+ ];
446
+ }
447
+ /**
448
+ * Map model to screen.
449
+ * Model points are represented by fractions (to preserve precision) and use normal cartesian coordinates.
450
+ */
451
+ model = {
452
+ // TODO(burdon): Precision depends on scale.
453
+ snapValues: (array) => array.map((n) => FractionUtil.round(n)),
454
+ snapVertex: ({ x, y }) => {
455
+ const [sx, sy] = this.model.snapValues([
456
+ x,
457
+ y
458
+ ]);
459
+ return {
460
+ x: sx,
461
+ y: sy
462
+ };
463
+ },
464
+ snapBounds: ({ x, y, width, height }) => {
465
+ [x, y, width, height] = this.model.snapValues([
466
+ x,
467
+ y,
468
+ width,
469
+ height
470
+ ]);
471
+ return {
472
+ x,
473
+ y,
474
+ width,
475
+ height
476
+ };
477
+ },
478
+ toValue: (value) => {
479
+ return Math.round(FractionUtil.toNumber(value) * this._gridSize);
480
+ },
481
+ toValues: (values) => {
482
+ return values.map((value) => Math.round(FractionUtil.toNumber(value) * this._gridSize));
483
+ },
484
+ toPoint: (point) => {
485
+ const [x, y] = this.model.toValues([
486
+ point.x,
487
+ point.y
488
+ ]);
489
+ return [
490
+ x,
491
+ y === 0 ? 0 : -y
492
+ ];
493
+ },
494
+ toBounds: ({ x, y, width, height }) => {
495
+ const point = this.model.toPoint({
496
+ x,
497
+ y
498
+ });
499
+ const [width2, height2] = this.model.toValues([
500
+ width,
501
+ height
502
+ ]);
503
+ return {
504
+ x: point[0],
505
+ y: point[1] - height2,
506
+ width: width2,
507
+ height: height2
508
+ };
509
+ }
510
+ };
511
+ /**
512
+ * Map screen to model.
513
+ * Screen points are represented by floating numbers and use translated SVG coordinates (with the y-axis inverted).
514
+ */
515
+ screen = {
516
+ snapValues: (array) => array.map((n) => Math.round(n / this._gridSize) * this._gridSize),
517
+ snapPoint: ([x, y]) => [
518
+ x,
519
+ y
520
+ ].map((n) => Math.round(n / this._gridSize) * this._gridSize),
521
+ snapBounds: ({ x, y, width, height }) => {
522
+ [x, y, width, height] = this.screen.snapValues([
523
+ x,
524
+ y,
525
+ width,
526
+ height
527
+ ]);
528
+ return {
529
+ x,
530
+ y,
531
+ width,
532
+ height
533
+ };
534
+ },
535
+ toValue: (value) => {
536
+ return FractionUtil.divide(FractionUtil.toFraction(value), [
537
+ this._gridSize,
538
+ 1
539
+ ]);
540
+ },
541
+ toValues: (values) => {
542
+ return values.map((value) => FractionUtil.divide(FractionUtil.toFraction(value), [
543
+ this._gridSize,
544
+ 1
545
+ ]));
546
+ },
547
+ toVertex: (point) => {
548
+ const [x, y] = point;
549
+ return {
550
+ x: FractionUtil.toFraction(x / this._gridSize),
551
+ y: FractionUtil.toFraction(-y / this._gridSize)
552
+ };
553
+ },
554
+ toBounds: (bounds) => {
555
+ const { x, y } = this.screen.toVertex([
556
+ bounds.x,
557
+ bounds.y
558
+ ]);
559
+ const [width, height] = this.screen.toValues([
560
+ bounds.width,
561
+ bounds.height
562
+ ]);
563
+ return {
564
+ x,
565
+ y: FractionUtil.subtract(y, height),
566
+ width,
567
+ height
568
+ };
569
+ }
570
+ };
571
+ };
572
+
573
+ // src/util/screen.ts
574
+ var Screen = class {
575
+ static createBounds = ([x1, y1], [x2, y2], mod = {}) => {
576
+ const { constrain, center } = mod;
577
+ const x = Math.min(x1, x2);
578
+ const y = Math.min(y1, y2);
579
+ let width = Math.abs(x2 - x1);
580
+ let height = Math.abs(y2 - y1);
581
+ if (constrain) {
582
+ width = height = Math.max(width, height);
583
+ }
584
+ if (center) {
585
+ return {
586
+ x: x - width,
587
+ y: y - height,
588
+ width: width * 2,
589
+ height: height * 2
590
+ };
591
+ } else {
592
+ return {
593
+ x,
594
+ y,
595
+ width,
596
+ height
597
+ };
598
+ }
599
+ };
600
+ static center = ({ x, y, width, height }) => {
601
+ return [
602
+ x + width / 2,
603
+ y + height / 2
604
+ ];
605
+ };
606
+ static contains = (bounds, point) => {
607
+ const [x, y] = point;
608
+ if (x < bounds.x || y < bounds.y) {
609
+ return false;
610
+ }
611
+ if (x > bounds.x + bounds.width || y > bounds.y + bounds.height) {
612
+ return false;
613
+ }
614
+ return true;
615
+ };
616
+ /**
617
+ * Distance between two points.
618
+ * @param p1
619
+ * @param p2
620
+ */
621
+ static len = (p1, p2) => {
622
+ return Math.sqrt(Math.pow(Math.abs(p1[0] - p2[0]), 2) + Math.pow(Math.abs(p1[1] - p2[1]), 2));
623
+ };
624
+ /**
625
+ * Distance to line segment.
626
+ * https://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment#1501725
627
+ * @param v1
628
+ * @param v2
629
+ * @param p
630
+ */
631
+ static normal = (v1, v2, p) => {
632
+ const d2 = (p1, p2) => Math.pow(Math.abs(p1[0] - p2[0]), 2) + Math.pow(Math.abs(p1[1] - p2[1]), 2);
633
+ let d;
634
+ const len2 = d2(v1, v2);
635
+ if (len2 === 0) {
636
+ d = d2(p, v1);
637
+ } else {
638
+ let t = ((p[0] - v1[0]) * (v2[0] - v1[0]) + (p[1] - v1[1]) * (v2[1] - v1[1])) / len2;
639
+ t = Math.max(0, Math.min(1, t));
640
+ d = d2(p, [
641
+ v1[0] + t * (v2[0] - v1[0]),
642
+ v1[1] + t * (v2[1] - v1[1])
643
+ ]);
644
+ }
645
+ return Math.sqrt(d);
646
+ };
647
+ };
648
+
649
+ // src/util/vector.ts
650
+ var Vector = class {
651
+ /**
652
+ * Convert to Vertex.
653
+ * @param x
654
+ * @param y
655
+ */
656
+ static toVertex = ({ x, y }) => ({
657
+ x: FractionUtil.toFraction(x),
658
+ y: FractionUtil.toFraction(y)
659
+ });
660
+ /**
661
+ * Convert to Bounds.
662
+ * @param bounds
663
+ */
664
+ static toBounds = ({ x, y, width, height }) => ({
665
+ x: FractionUtil.toFraction(x),
666
+ y: FractionUtil.toFraction(y),
667
+ width: FractionUtil.toFraction(width),
668
+ height: FractionUtil.toFraction(height)
669
+ });
670
+ /**
671
+ * Calculate center point.
672
+ * @param bounds
673
+ */
674
+ static center = ({ x, y, width, height }) => {
675
+ const cx = FractionUtil.add(x, FractionUtil.multiply(width, [
676
+ 1,
677
+ 2
678
+ ]));
679
+ const cy = FractionUtil.add(y, FractionUtil.multiply(height, [
680
+ 1,
681
+ 2
682
+ ]));
683
+ return {
684
+ x: cx,
685
+ y: cy
686
+ };
687
+ };
688
+ /**
689
+ * Calculate centered left edge.
690
+ * @param bounds
691
+ */
692
+ static left = ({ x, y, height }) => {
693
+ const cx = x;
694
+ const cy = FractionUtil.add(y, FractionUtil.multiply(height, [
695
+ 1,
696
+ 2
697
+ ]));
698
+ return {
699
+ x: cx,
700
+ y: cy
701
+ };
702
+ };
703
+ /**
704
+ * Calculate centered right edge.
705
+ * @param bounds
706
+ */
707
+ static right = ({ x, y, width, height }) => {
708
+ const cx = FractionUtil.add(x, width);
709
+ const cy = FractionUtil.add(y, FractionUtil.multiply(height, [
710
+ 1,
711
+ 2
712
+ ]));
713
+ return {
714
+ x: cx,
715
+ y: cy
716
+ };
717
+ };
718
+ };
719
+
181
720
  // src/graph/projector/projector.ts
182
721
  var Projector = class {
722
+ _context;
723
+ updated = new EventEmitter();
724
+ _options;
183
725
  constructor(_context, options) {
184
726
  this._context = _context;
185
- this.updated = new EventEmitter();
186
727
  this._options = Object.assign({
187
728
  idAccessor: defaultIdAccessor
188
729
  }, options);
@@ -227,6 +768,8 @@ var Projector = class {
227
768
 
228
769
  // src/graph/projector/graph-projector.ts
229
770
  var GraphProjector = class extends Projector {
771
+ _selection;
772
+ _layout;
230
773
  // TODO(burdon): Change to object props.
231
774
  constructor(context, options, _selection, layout) {
232
775
  super(context, options), this._selection = _selection;
@@ -310,7 +853,7 @@ var forcePoint = ({ x, y, strength = 0.01 }) => {
310
853
  };
311
854
 
312
855
  // src/graph/projector/graph-force-projector.ts
313
- var __dxlog_file2 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/projector/graph-force-projector.ts";
856
+ var __dxlog_file3 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/projector/graph-force-projector.ts";
314
857
  var getValue = (valueOrFunction, invoker, defaultValue) => {
315
858
  return (typeof valueOrFunction === "function" ? invoker(valueOrFunction) : valueOrFunction) ?? defaultValue;
316
859
  };
@@ -327,11 +870,9 @@ var defaultForceOptions = {
327
870
  point: true
328
871
  };
329
872
  var GraphForceProjector = class extends GraphProjector {
330
- constructor() {
331
- super(...arguments);
332
- // https://github.com/d3/d3-force
333
- this._simulation = forceSimulation();
334
- }
873
+ // https://github.com/d3/d3-force
874
+ _simulation = forceSimulation();
875
+ _timeout;
335
876
  get forces() {
336
877
  return this.options.forces ?? defaultForceOptions;
337
878
  }
@@ -384,12 +925,7 @@ var GraphForceProjector = class extends GraphProjector {
384
925
  nodes: graph?.nodes.length,
385
926
  edges: graph?.edges.length
386
927
  }
387
- }, {
388
- F: __dxlog_file2,
389
- L: 226,
390
- S: this,
391
- C: (f, a) => f(...a)
392
- });
928
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 89, S: this });
393
929
  this._simulation.stop();
394
930
  this.mergeData(graph);
395
931
  this.updateLayout();
@@ -439,12 +975,7 @@ var GraphForceProjector = class extends GraphProjector {
439
975
  updateForces(forces) {
440
976
  log("updateForces", {
441
977
  forces
442
- }, {
443
- F: __dxlog_file2,
444
- L: 287,
445
- S: this,
446
- C: (f, a) => f(...a)
447
- });
978
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 148, S: this });
448
979
  this._simulation.nodes(this.layout.graph.nodes).force("link", maybeForce(forces?.link, (config) => {
449
980
  const force = forceLink().id((d) => d.id).links(this.layout.graph.edges.filter((edge) => edge.linkForce));
450
981
  if (config.distance != null) {
@@ -513,8 +1044,9 @@ import { log as log3 } from "@dxos/log";
513
1044
  // src/graph/projector/graph-radial-projector.ts
514
1045
  import { easeCubic, timer } from "d3";
515
1046
  import { log as log2 } from "@dxos/log";
516
- var __dxlog_file3 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/projector/graph-radial-projector.ts";
1047
+ var __dxlog_file4 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/projector/graph-radial-projector.ts";
517
1048
  var GraphRadialProjector = class extends GraphProjector {
1049
+ _timer;
518
1050
  findNode(x, y, radius) {
519
1051
  return void 0;
520
1052
  }
@@ -524,12 +1056,7 @@ var GraphRadialProjector = class extends GraphProjector {
524
1056
  nodes: graph?.nodes.length,
525
1057
  edges: graph?.edges.length
526
1058
  }
527
- }, {
528
- F: __dxlog_file3,
529
- L: 31,
530
- S: this,
531
- C: (f, a) => f(...a)
532
- });
1059
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 13, S: this });
533
1060
  this.mergeData(graph);
534
1061
  this.doRadialLayout();
535
1062
  }
@@ -625,7 +1152,7 @@ var layoutVertical = (x, y, h, n) => {
625
1152
  };
626
1153
 
627
1154
  // src/graph/projector/graph-hierarchical-projector.ts
628
- var __dxlog_file4 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/projector/graph-hierarchical-projector.ts";
1155
+ var __dxlog_file5 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/projector/graph-hierarchical-projector.ts";
629
1156
  var GraphHierarchicalProjector = class extends GraphRadialProjector {
630
1157
  onUpdate(graph) {
631
1158
  log3("onUpdate", {
@@ -633,15 +1160,10 @@ var GraphHierarchicalProjector = class extends GraphRadialProjector {
633
1160
  nodes: graph?.nodes.length,
634
1161
  edges: graph?.edges.length
635
1162
  },
636
- selection: this.selection?.selected.value
637
- }, {
638
- F: __dxlog_file4,
639
- L: 23,
640
- S: this,
641
- C: (f, a) => f(...a)
642
- });
1163
+ selection: this.selection?.getSelectedIds()
1164
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file5, L: 8, S: this });
643
1165
  this.mergeData(graph);
644
- const selected = this.selection?.selected.value?.[0];
1166
+ const selected = this.selection?.getSelectedIds()[0];
645
1167
  if (selected) {
646
1168
  const node = this.layout.graph.nodes.find((node2) => node2.id === selected);
647
1169
  if (node) {
@@ -688,7 +1210,7 @@ var GraphHierarchicalProjector = class extends GraphRadialProjector {
688
1210
 
689
1211
  // src/graph/projector/graph-relational-projector.ts
690
1212
  import { log as log4 } from "@dxos/log";
691
- var __dxlog_file5 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/projector/graph-relational-projector.ts";
1213
+ var __dxlog_file6 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/projector/graph-relational-projector.ts";
692
1214
  var GraphRelationalProjector = class extends GraphRadialProjector {
693
1215
  onUpdate(graph) {
694
1216
  log4("onUpdate", {
@@ -696,15 +1218,10 @@ var GraphRelationalProjector = class extends GraphRadialProjector {
696
1218
  nodes: graph?.nodes.length,
697
1219
  edges: graph?.edges.length
698
1220
  },
699
- selection: this.selection?.selected.value
700
- }, {
701
- F: __dxlog_file5,
702
- L: 23,
703
- S: this,
704
- C: (f, a) => f(...a)
705
- });
1221
+ selection: this.selection?.getSelectedIds()
1222
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file6, L: 8, S: this });
706
1223
  this.mergeData(graph);
707
- const selected = this.selection?.selected.value?.[0];
1224
+ const selected = this.selection?.getSelectedIds()[0];
708
1225
  if (selected) {
709
1226
  const node = this.layout.graph.nodes.find((node2) => node2.id === selected);
710
1227
  if (node) {
@@ -776,7 +1293,7 @@ var GraphRelationalProjector = class extends GraphRadialProjector {
776
1293
  };
777
1294
 
778
1295
  // src/graph/renderer/graph-renderer.ts
779
- import { line as line2, select as select4, polygonHull, easeCubicOut } from "d3";
1296
+ import { easeCubicOut, line as line2, polygonHull, select as select4 } from "d3";
780
1297
  import * as Clipper from "js-clipper";
781
1298
  import { log as log5 } from "@dxos/log";
782
1299
 
@@ -811,9 +1328,10 @@ var createBullets = (group, nodeId, options = {}) => {
811
1328
  };
812
1329
 
813
1330
  // src/graph/renderer/renderer.ts
814
- import { invariant as invariant2 } from "@dxos/invariant";
815
- var __dxlog_file6 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/renderer/renderer.ts";
816
1331
  var Renderer = class {
1332
+ _context;
1333
+ _root;
1334
+ _options;
817
1335
  constructor(_context, _root, options) {
818
1336
  this._context = _context;
819
1337
  this._root = _root;
@@ -824,16 +1342,12 @@ var Renderer = class {
824
1342
  get context() {
825
1343
  return this._context;
826
1344
  }
1345
+ /**
1346
+ * Returns the SVG group root if mounted.
1347
+ * The ref may be null between mount/unmount cycles or before the SVG container has sized.
1348
+ * Callers should treat null as "skip this render" rather than as an error.
1349
+ */
827
1350
  get root() {
828
- invariant2(this._root.current, "SVG root is not initialized", {
829
- F: __dxlog_file6,
830
- L: 40,
831
- S: this,
832
- A: [
833
- "this._root.current",
834
- "'SVG root is not initialized'"
835
- ]
836
- });
837
1351
  return this._root.current;
838
1352
  }
839
1353
  get options() {
@@ -846,12 +1360,10 @@ var __dxlog_file7 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/rendere
846
1360
  var createLine2 = line2();
847
1361
  var GraphRenderer = class extends Renderer {
848
1362
  render(layout) {
849
- log5("render", layout, {
850
- F: __dxlog_file7,
851
- L: 62,
852
- S: this,
853
- C: (f, a) => f(...a)
854
- });
1363
+ if (!this.root) {
1364
+ return;
1365
+ }
1366
+ log5("render", layout, { "~LogMeta": "~LogMeta", F: __dxlog_file7, L: 24, S: this });
855
1367
  const root = select4(this.root);
856
1368
  if (layout.guides) {
857
1369
  root.selectAll("g.dx-guides").data([
@@ -928,6 +1440,9 @@ var GraphRenderer = class extends Renderer {
928
1440
  * @param node
929
1441
  */
930
1442
  fireBullet(node) {
1443
+ if (!this.root) {
1444
+ return;
1445
+ }
931
1446
  select4(this.root).selectAll("g.dx-edges").selectAll("path").call(createBullets(this.root, node.id));
932
1447
  }
933
1448
  };
@@ -958,14 +1473,14 @@ var createNode = (group, options) => {
958
1473
  circle.on("pointerenter", function() {
959
1474
  select4(this.closest("g.dx-node")).raise();
960
1475
  if (options.labels) {
961
- select4(this.parentElement).classed("dx-active", true).classed("dx-highlight", true);
1476
+ select4(this.parentElement).classed("dx-node-active", true).classed("dx-highlight", true);
962
1477
  }
963
1478
  });
964
1479
  group.on("pointerleave", function() {
965
1480
  if (options.labels) {
966
- select4(this).classed("dx-active", false);
1481
+ select4(this).classed("dx-node-active", false);
967
1482
  setTimeout(() => {
968
- if (!select4(this).classed("dx-active")) {
1483
+ if (!select4(this).classed("dx-node-active")) {
969
1484
  select4(this).classed("dx-highlight", false);
970
1485
  }
971
1486
  }, 300);
@@ -994,12 +1509,7 @@ var updateNode2 = (group, options) => {
994
1509
  } catch (error) {
995
1510
  log5.error("updateNode", {
996
1511
  error: error?.message
997
- }, {
998
- F: __dxlog_file7,
999
- L: 304,
1000
- S: void 0,
1001
- C: (f, a) => f(...a)
1002
- });
1512
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file7, L: 202, S: void 0 });
1003
1513
  }
1004
1514
  }
1005
1515
  const groupOrTransition = options.transition ? group.transition(options.transition()) : group;
@@ -1050,12 +1560,7 @@ var updateEdge = (group, options, nodeGroup) => {
1050
1560
  } catch (error) {
1051
1561
  log5.error("updateEdge", {
1052
1562
  error: error?.message
1053
- }, {
1054
- F: __dxlog_file7,
1055
- L: 416,
1056
- S: void 0,
1057
- C: (f, a) => f(...a)
1058
- });
1563
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file7, L: 268, S: void 0 });
1059
1564
  }
1060
1565
  const groupOrTransition = options.transition ? group.transition(options.transition()) : group;
1061
1566
  groupOrTransition.selectAll("path").attr("d", function() {
@@ -1133,117 +1638,436 @@ var linkerRenderer = (root, { source, target, point } = {}) => {
1133
1638
  });
1134
1639
  };
1135
1640
 
1641
+ // src/hooks/useGrid.ts
1642
+ import { line as line4, range, select as select5 } from "d3";
1643
+ import { useEffect, useMemo, useRef } from "react";
1644
+
1645
+ // src/hooks/useSvgContext.ts
1646
+ import { createContext, useContext } from "react";
1647
+ import { createRef } from "react";
1648
+ import { raise } from "@dxos/debug";
1649
+ var SVGContext = class {
1650
+ _ref = createRef();
1651
+ _scale;
1652
+ _centered;
1653
+ _size;
1654
+ _center;
1655
+ resized = new EventEmitter();
1656
+ constructor({ scale = new Scale(), centered = true }) {
1657
+ this._scale = scale;
1658
+ this._centered = centered;
1659
+ }
1660
+ get ref() {
1661
+ return this._ref;
1662
+ }
1663
+ get svg() {
1664
+ return this._ref.current;
1665
+ }
1666
+ get scale() {
1667
+ return this._scale;
1668
+ }
1669
+ get center() {
1670
+ return this._center;
1671
+ }
1672
+ get size() {
1673
+ return this._size;
1674
+ }
1675
+ /**
1676
+ * https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/viewBox
1677
+ */
1678
+ get viewBox() {
1679
+ const [x, y] = this.center;
1680
+ return `${x},${y},${this._size.width},${this._size.height}`;
1681
+ }
1682
+ setSize(size) {
1683
+ this._size = {
1684
+ ...size
1685
+ };
1686
+ this._center = this._centered ? [
1687
+ -Math.floor(size.width / 2),
1688
+ -Math.floor(size.height / 2)
1689
+ ] : [
1690
+ 0,
1691
+ 0
1692
+ ];
1693
+ this.resized.emit(this);
1694
+ }
1695
+ setTransform(transform) {
1696
+ if (this._scale.setTransform(transform)) {
1697
+ this.resized.emit(this);
1698
+ }
1699
+ }
1700
+ /**
1701
+ * https://developer.mozilla.org/en-US/docs/Web/API/Document/elementFromPoint
1702
+ * @param point
1703
+ */
1704
+ elementFromPoint(point) {
1705
+ const [x, y] = point;
1706
+ const [cx, cy] = this._center;
1707
+ return document.elementFromPoint(cx + x, cy + y);
1708
+ }
1709
+ };
1710
+ var SVGContextType = createContext(void 0);
1711
+ var SVGContextProvider = SVGContextType.Provider;
1712
+ var useSvgContext = () => {
1713
+ return useContext(SVGContextType) ?? raise(new Error("Missing SVGRoot"));
1714
+ };
1715
+
1716
+ // src/hooks/useGrid.ts
1717
+ var createLine4 = line4();
1718
+ var createGrid = (context, options) => {
1719
+ if (!context.size) {
1720
+ return [];
1721
+ }
1722
+ const paths = [];
1723
+ const { width, height } = context.size;
1724
+ const { x, y, k } = context.scale.transform;
1725
+ const s = 1 / k;
1726
+ const w = width * s;
1727
+ const h = height * s;
1728
+ const dx = -(x + width / 2) * s;
1729
+ const dy = -(y + height / 2) * s;
1730
+ if (options.axis) {
1731
+ const axis = [
1732
+ [
1733
+ [
1734
+ 0,
1735
+ dy
1736
+ ],
1737
+ [
1738
+ 0,
1739
+ dy + h
1740
+ ]
1741
+ ],
1742
+ [
1743
+ [
1744
+ dx,
1745
+ 0
1746
+ ],
1747
+ [
1748
+ dx + w,
1749
+ 0
1750
+ ]
1751
+ ]
1752
+ ];
1753
+ paths.push({
1754
+ id: "axis",
1755
+ class: "dx-axis",
1756
+ path: axis.map((line5) => createLine4(line5)).join()
1757
+ });
1758
+ }
1759
+ if (options.grid) {
1760
+ const mod = (n, size, delta = 0) => Math.floor(n / size + delta) * size;
1761
+ const majorSize = context.scale.gridSize;
1762
+ const xMajor = range(-mod((x + width / 2) * s, majorSize), mod((-x + width / 2) * s, majorSize, 1), majorSize);
1763
+ const yMajor = range(-mod((y + height / 2) * s, majorSize), mod((-y + height / 2) * s, majorSize, 1), majorSize);
1764
+ const major = [
1765
+ ...xMajor.filter((x2) => !options.axis || x2).map((x2) => [
1766
+ [
1767
+ x2,
1768
+ dy
1769
+ ],
1770
+ [
1771
+ x2,
1772
+ dy + h
1773
+ ]
1774
+ ]),
1775
+ ...yMajor.filter((y2) => !options.axis || y2).map((y2) => [
1776
+ [
1777
+ dx,
1778
+ y2
1779
+ ],
1780
+ [
1781
+ dx + w,
1782
+ y2
1783
+ ]
1784
+ ])
1785
+ ];
1786
+ paths.push({
1787
+ id: "major",
1788
+ class: "dx-major",
1789
+ path: major.map((line5) => createLine4(line5)).join()
1790
+ });
1791
+ const minorSize = Math.pow(2, Math.round(Math.log2(s * context.scale.gridSize)));
1792
+ if (majorSize > minorSize) {
1793
+ const xMinor = range(-mod((x + width / 2) * s, minorSize), mod((-x + width / 2) * s, minorSize, 1), minorSize);
1794
+ const yMinor = range(-mod((y + height / 2) * s, minorSize), mod((-y + height / 2) * s, minorSize, 1), minorSize);
1795
+ const minor = [
1796
+ ...xMinor.filter((x2) => xMajor.indexOf(x2) === -1).map((x2) => [
1797
+ [
1798
+ x2,
1799
+ dy
1800
+ ],
1801
+ [
1802
+ x2,
1803
+ dy + h
1804
+ ]
1805
+ ]),
1806
+ ...yMinor.filter((y2) => yMajor.indexOf(y2) === -1).map((y2) => [
1807
+ [
1808
+ dx,
1809
+ y2
1810
+ ],
1811
+ [
1812
+ dx + w,
1813
+ y2
1814
+ ]
1815
+ ])
1816
+ ];
1817
+ paths.push({
1818
+ id: "minor",
1819
+ class: "dx-minor",
1820
+ path: minor.map((line5) => createLine4(line5)).join()
1821
+ });
1822
+ }
1823
+ }
1824
+ return paths;
1825
+ };
1826
+ var defaultGridOptions = {
1827
+ visible: true,
1828
+ axis: true,
1829
+ grid: true
1830
+ };
1831
+ var GridController = class {
1832
+ _ref;
1833
+ _context;
1834
+ _options;
1835
+ _visible = false;
1836
+ constructor(_ref, _context, _options) {
1837
+ this._ref = _ref;
1838
+ this._context = _context;
1839
+ this._options = _options;
1840
+ this._visible = this._options.visible ?? true;
1841
+ }
1842
+ get ref() {
1843
+ return this._ref;
1844
+ }
1845
+ get visible() {
1846
+ return this._visible;
1847
+ }
1848
+ draw() {
1849
+ if (!this._context.size) {
1850
+ return;
1851
+ }
1852
+ const paths = this._visible ? createGrid(this._context, this._options) : [];
1853
+ const root = select5(this._ref.current);
1854
+ root.selectAll("path").data(paths, (path) => path.id).join("path").style("pointer-events", "none").attr("d", (d) => d.path).attr("class", (d) => d.class);
1855
+ const transform = this._context.scale.transform;
1856
+ if (transform) {
1857
+ root.attr("transform", transform);
1858
+ root.attr("stroke-width", 1 / transform.k);
1859
+ }
1860
+ }
1861
+ setVisible(visible) {
1862
+ this._visible = visible;
1863
+ this.draw();
1864
+ return this;
1865
+ }
1866
+ };
1867
+ var useGrid = (options = defaultGridOptions) => {
1868
+ const ref = useRef(null);
1869
+ const context = useSvgContext();
1870
+ const controller = useMemo(() => new GridController(ref, context, options), []);
1871
+ useEffect(() => context.resized.on(() => controller.draw()), [
1872
+ context,
1873
+ controller
1874
+ ]);
1875
+ return controller;
1876
+ };
1877
+
1878
+ // src/hooks/useZoom.ts
1879
+ import { select as select6, zoom, zoomIdentity as zoomIdentity2 } from "d3";
1880
+ import defaultsDeep from "lodash.defaultsdeep";
1881
+ import { useEffect as useEffect2, useMemo as useMemo2, useRef as useRef2 } from "react";
1882
+ var defaultZoomOptions = {
1883
+ enabled: true,
1884
+ extent: [
1885
+ 1 / 4,
1886
+ 4
1887
+ ],
1888
+ onDblClick: (zoom2) => zoom2.reset()
1889
+ };
1890
+ var ZoomHandler = class {
1891
+ _ref;
1892
+ _context;
1893
+ _zoom;
1894
+ _options;
1895
+ _enabled;
1896
+ constructor(_ref, _context, options) {
1897
+ this._ref = _ref;
1898
+ this._context = _context;
1899
+ this._options = defaultsDeep({}, options, defaultZoomOptions);
1900
+ this._enabled = this._options.enabled ?? true;
1901
+ this._zoom = zoom().scaleExtent(this._options.extent ?? defaultZoomOptions.extent);
1902
+ }
1903
+ /**
1904
+ * Gets the reference which the transform is applied to.
1905
+ */
1906
+ get ref() {
1907
+ return this._ref;
1908
+ }
1909
+ get zoom() {
1910
+ return this._zoom;
1911
+ }
1912
+ init() {
1913
+ this.setEnabled(this._enabled);
1914
+ this.reset(0);
1915
+ return this;
1916
+ }
1917
+ setEnabled(enable) {
1918
+ if (enable) {
1919
+ select6(this._context.svg).call(this._zoom).on(
1920
+ "dblclick.zoom",
1921
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1922
+ // @ts-ignore
1923
+ this._options?.onDblClick ? () => {
1924
+ this._options?.onDblClick(this);
1925
+ } : null
1926
+ );
1927
+ } else {
1928
+ select6(this._context.svg).on(".zoom", null);
1929
+ }
1930
+ this._enabled = enable;
1931
+ return this;
1932
+ }
1933
+ reset(duration = 500) {
1934
+ const scale = 1;
1935
+ const transform = zoomIdentity2.scale(scale);
1936
+ select6(this._context.svg).transition().duration(duration).call(this._zoom.transform, transform);
1937
+ return this;
1938
+ }
1939
+ };
1940
+ var useZoom = (options = defaultZoomOptions) => {
1941
+ const context = useSvgContext();
1942
+ const ref = useRef2(null);
1943
+ const handler = useMemo2(() => new ZoomHandler(ref, context, options), [
1944
+ context,
1945
+ options
1946
+ ]);
1947
+ useEffect2(() => {
1948
+ handler.zoom.on("zoom", ({ transform }) => {
1949
+ context.setTransform(transform);
1950
+ select6(ref.current).attr("transform", transform);
1951
+ });
1952
+ handler.init();
1953
+ }, [
1954
+ handler
1955
+ ]);
1956
+ return handler;
1957
+ };
1958
+
1136
1959
  // src/components/Graph/Graph.tsx
1137
1960
  var __dxlog_file8 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/components/Graph/Graph.tsx";
1138
- var GraphInner = ({ classNames, model, projector: _projector, renderer: _renderer, drag: drag2, arrows, onSelect, onInspect, ...props }, forwardedRef) => {
1139
- var _effect = _useSignals();
1140
- try {
1141
- const context = useSvgContext();
1142
- const graphRef = useRef();
1143
- const { projector, renderer } = useMemo(() => {
1144
- let projector2 = _projector;
1145
- if (!projector2) {
1146
- projector2 = new GraphForceProjector(context);
1147
- }
1148
- let renderer2 = _renderer;
1149
- if (!renderer2) {
1150
- renderer2 = new GraphRenderer(context, graphRef, {
1151
- ...props,
1152
- // TODO(burdon): Replace drag when projector is updated.
1153
- drag: drag2 ? createGraphDrag(context, projector2) : void 0,
1154
- arrows: {
1155
- end: arrows
1156
- },
1157
- onNodeClick: onSelect ? (node, event) => onSelect(node, event) : void 0,
1158
- onNodePointerEnter: onInspect ? (node, event) => onInspect(node, event) : void 0
1159
- });
1160
- }
1161
- return {
1162
- projector: projector2,
1163
- renderer: renderer2
1164
- };
1165
- }, [
1166
- context,
1167
- _projector,
1168
- _renderer,
1169
- drag2
1170
- ]);
1171
- useImperativeHandle(forwardedRef, () => ({
1172
- refresh: () => {
1173
- projector.updateData(model?.graph);
1174
- },
1175
- repaint: () => {
1176
- renderer.render(projector.layout);
1177
- },
1178
- findNode: (id) => {
1179
- return graphRef.current?.querySelector(`g[data-id="${id}"]`);
1180
- }
1181
- }), [
1182
- model,
1183
- projector,
1184
- renderer
1185
- ]);
1186
- useEffect(() => {
1187
- return combine(effect(() => {
1961
+ var GraphInner = ({ classNames, model, projector: projectorProp, renderer: rendererProp, drag: drag2, arrows, onSelect, onInspect, ...props }, forwardedRef) => {
1962
+ const context = useSvgContext();
1963
+ const graphRef = useRef3(null);
1964
+ const { projector, renderer } = useMemo3(() => {
1965
+ let projector2 = projectorProp;
1966
+ if (!projector2) {
1967
+ projector2 = new GraphForceProjector(context);
1968
+ }
1969
+ let renderer2 = rendererProp;
1970
+ if (!renderer2) {
1971
+ renderer2 = new GraphRenderer(context, graphRef, {
1972
+ ...props,
1973
+ // TODO(burdon): Replace drag when projector is updated.
1974
+ drag: drag2 ? createGraphDrag(context, projector2) : void 0,
1975
+ arrows: {
1976
+ end: arrows
1977
+ },
1978
+ onNodeClick: onSelect,
1979
+ onNodePointerEnter: onInspect
1980
+ });
1981
+ }
1982
+ return {
1983
+ projector: projector2,
1984
+ renderer: renderer2
1985
+ };
1986
+ }, [
1987
+ context,
1988
+ projectorProp,
1989
+ rendererProp,
1990
+ drag2
1991
+ ]);
1992
+ useImperativeHandle(forwardedRef, () => ({
1993
+ refresh: () => {
1994
+ projector.updateData(model?.graph);
1995
+ },
1996
+ repaint: () => {
1997
+ renderer.render(projector.layout);
1998
+ },
1999
+ findNode: (id) => {
2000
+ return graphRef.current?.querySelector(`g[data-id="${id}"]`);
2001
+ }
2002
+ }), [
2003
+ model,
2004
+ projector,
2005
+ renderer
2006
+ ]);
2007
+ useEffect3(() => {
2008
+ projector.updateData(model?.graph);
2009
+ return combine(
2010
+ // Subscribe to model changes if reactive model.
2011
+ model ? model.subscribe(() => {
1188
2012
  projector.updateData(model?.graph);
1189
- }), projector.updated.on(({ layout }) => {
2013
+ }) : void 0,
2014
+ projector.updated.on(({ layout }) => {
1190
2015
  try {
1191
2016
  renderer.render(layout);
1192
2017
  } catch (error) {
2018
+ log6.catch(error, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file8, L: 67, S: void 0 });
1193
2019
  void projector.stop();
1194
- log6.catch(error, void 0, {
1195
- F: __dxlog_file8,
1196
- L: 108,
1197
- S: void 0,
1198
- C: (f, a) => f(...a)
1199
- });
1200
2020
  }
1201
- }));
1202
- }, [
1203
- model,
1204
- projector,
1205
- renderer
1206
- ]);
1207
- useEffect(() => {
1208
- void projector.start();
1209
- return () => {
1210
- void projector.stop();
1211
- };
1212
- }, [
1213
- projector
1214
- ]);
1215
- return /* @__PURE__ */ React.createElement("g", {
1216
- ref: graphRef,
1217
- className: mx("dx-graph", classNames)
1218
- });
1219
- } finally {
1220
- _effect.f();
1221
- }
2021
+ })
2022
+ );
2023
+ }, [
2024
+ model,
2025
+ projector,
2026
+ renderer
2027
+ ]);
2028
+ useEffect3(() => {
2029
+ void projector.start();
2030
+ return () => {
2031
+ void projector.stop();
2032
+ };
2033
+ }, [
2034
+ projector
2035
+ ]);
2036
+ return /* @__PURE__ */ React.createElement("g", {
2037
+ ref: graphRef,
2038
+ className: mx("dx-graph", classNames)
2039
+ });
1222
2040
  };
1223
2041
  var Graph = /* @__PURE__ */ forwardRef(GraphInner);
1224
2042
 
1225
2043
  // src/components/Mesh/Mesh.tsx
1226
- import { geoPath, select as select5 } from "d3";
1227
- import { useEffect as useEffect2 } from "react";
2044
+ import { geoPath, select as select7 } from "d3";
2045
+ import { useEffect as useEffect4 } from "react";
1228
2046
  import { feature, mesh } from "topojson-client";
1229
2047
  var Mesh = ({ radius = 16, value = 0.5 }) => {
1230
2048
  const { svg, size } = useSvgContext();
1231
- useEffect2(() => {
1232
- if (size) {
1233
- const topology = hexTopology(size, radius, value);
1234
- const projection = hexProjection(radius);
1235
- const path = geoPath().projection(projection);
1236
- select5(svg).append("g").attr("class", "hexagon").selectAll("path").data(topology.objects.hexagons.geometries).enter().append("path").attr("d", (d) => path(feature(topology, d))).attr("class", (d) => d.properties?.fill ? "fill" : null);
1237
- select5(svg).append("path").datum(mesh(topology, topology.objects.hexagons)).attr("class", "mesh").attr("d", path);
1238
- const redraw = (border) => {
1239
- border.attr("d", path(mesh(topology, topology.objects.hexagons, (a, b) => a.properties.fill !== b.properties.fill)));
1240
- };
1241
- select5(svg).append("path").attr("class", "border").call(redraw);
2049
+ useEffect4(() => {
2050
+ if (!size) {
2051
+ return;
1242
2052
  }
2053
+ const topology = hexTopology(size, radius, value);
2054
+ const projection = hexProjection(radius);
2055
+ const path = geoPath().projection(projection);
2056
+ const root = select7(svg).selectAll("g.dx-mesh-root").data([
2057
+ null
2058
+ ]).join("g").classed("dx-mesh-root", true);
2059
+ root.selectAll("*").remove();
2060
+ 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);
2061
+ root.append("path").datum(mesh(topology, topology.objects.hexagons)).attr("class", "mesh").attr("d", path);
2062
+ const redraw = (border) => {
2063
+ border.attr("d", path(mesh(topology, topology.objects.hexagons, (a, b) => a.properties.fill !== b.properties.fill)));
2064
+ };
2065
+ root.append("path").attr("class", "border").call(redraw);
1243
2066
  }, [
1244
2067
  svg,
1245
2068
  size,
1246
- value
2069
+ value,
2070
+ radius
1247
2071
  ]);
1248
2072
  return null;
1249
2073
  };
@@ -1355,115 +2179,123 @@ var hexProjection = (radius) => {
1355
2179
  };
1356
2180
 
1357
2181
  // src/components/SVG/Grid.tsx
1358
- import { useSignals as _useSignals2 } from "@preact-signals/safe-react/tracking";
1359
2182
  import React2 from "react";
1360
- import { mx as mx2 } from "@dxos/react-ui-theme";
2183
+ import { mx as mx2 } from "@dxos/ui-theme";
1361
2184
  var Grid = ({ classNames, ...props }) => {
1362
- var _effect = _useSignals2();
1363
- try {
1364
- const { ref } = useGrid(props);
1365
- return /* @__PURE__ */ React2.createElement("g", {
1366
- ref,
1367
- className: mx2("dx-grid", classNames)
1368
- });
1369
- } finally {
1370
- _effect.f();
1371
- }
2185
+ const { ref } = useGrid(props);
2186
+ return /* @__PURE__ */ React2.createElement("g", {
2187
+ ref,
2188
+ className: mx2("dx-grid", classNames)
2189
+ });
1372
2190
  };
1373
2191
 
1374
2192
  // src/components/SVG/Markers.tsx
1375
- import { useSignals as _useSignals3 } from "@preact-signals/safe-react/tracking";
1376
- import { select as select6 } from "d3";
1377
- import React3, { useEffect as useEffect3, useRef as useRef2 } from "react";
2193
+ import { select as select8 } from "d3";
2194
+ import React3, { useEffect as useEffect5, useRef as useRef4 } from "react";
1378
2195
  var Markers = ({ arrowSize, className }) => {
1379
- var _effect = _useSignals3();
1380
- try {
1381
- const ref = useRef2();
1382
- useEffect3(() => {
1383
- select6(ref.current).call(createMarkers({
1384
- arrowSize
1385
- }));
1386
- }, [
1387
- ref
1388
- ]);
1389
- return /* @__PURE__ */ React3.createElement("defs", {
1390
- ref,
1391
- className
1392
- });
1393
- } finally {
1394
- _effect.f();
1395
- }
2196
+ const ref = useRef4(null);
2197
+ useEffect5(() => {
2198
+ select8(ref.current).call(createMarkers({
2199
+ arrowSize
2200
+ }));
2201
+ }, [
2202
+ ref
2203
+ ]);
2204
+ return /* @__PURE__ */ React3.createElement("defs", {
2205
+ ref,
2206
+ className
2207
+ });
1396
2208
  };
1397
2209
 
1398
2210
  // src/components/SVG/Root.tsx
1399
- import { useSignals as _useSignals4 } from "@preact-signals/safe-react/tracking";
1400
- import { select as select7 } from "d3";
1401
- import React4, { forwardRef as forwardRef2, useEffect as useEffect4, useMemo as useMemo2, useImperativeHandle as useImperativeHandle2 } from "react";
1402
- import { useResizeDetector } from "react-resize-detector";
1403
- import { mx as mx3 } from "@dxos/react-ui-theme";
1404
- var Root = /* @__PURE__ */ forwardRef2(({ classNames, children, ...props }, ref) => {
1405
- var _effect = _useSignals4();
1406
- try {
1407
- const { ref: containerRef, width = 0, height = 0 } = useResizeDetector({
1408
- refreshRate: 200
2211
+ import { select as select9 } from "d3";
2212
+ import React4, { forwardRef as forwardRef2, useEffect as useEffect6, useImperativeHandle as useImperativeHandle2, useMemo as useMemo4, useState } from "react";
2213
+ import { composableProps, mx as mx3 } from "@dxos/ui-theme";
2214
+ var Root = /* @__PURE__ */ forwardRef2(({ children, scale, centered, ...props }, ref) => {
2215
+ const [container, setContainer] = useState(null);
2216
+ const [size, setSize] = useState({
2217
+ width: 0,
2218
+ height: 0
2219
+ });
2220
+ useEffect6(() => {
2221
+ if (!container) {
2222
+ return;
2223
+ }
2224
+ const rect = container.getBoundingClientRect();
2225
+ setSize({
2226
+ width: rect.width,
2227
+ height: rect.height
1409
2228
  });
1410
- const context = useMemo2(() => {
1411
- return new SVGContext(props);
1412
- }, [
1413
- props.scale,
1414
- props.centered
1415
- ]);
1416
- useImperativeHandle2(ref, () => context, [
1417
- context
1418
- ]);
1419
- useEffect4(() => {
1420
- if (width && height) {
1421
- context.setSize({
1422
- width,
1423
- height
1424
- });
1425
- select7(context.svg).attr("display", "block").attr("viewBox", context.viewBox).attr("width", width).attr("height", height);
2229
+ const observer = new ResizeObserver((entries) => {
2230
+ const entry = entries[0];
2231
+ if (!entry) {
2232
+ return;
1426
2233
  }
1427
- }, [
1428
- context,
1429
- width,
1430
- height
1431
- ]);
1432
- const ready = width !== 0 && height !== 0;
1433
- return /* @__PURE__ */ React4.createElement(SVGContextProvider, {
1434
- value: context
1435
- }, /* @__PURE__ */ React4.createElement("div", {
1436
- ref: containerRef,
1437
- className: mx3("grid grow overflow-hidden", classNames)
1438
- }, /* @__PURE__ */ React4.createElement("svg", {
1439
- xmlns: "http://www.w3.org/2000/svg",
1440
- ref: context.ref,
1441
- className: mx3("transition-opacity opacity-0 duration-1000", ready && "opacity-100")
1442
- }, ready && children)));
1443
- } finally {
1444
- _effect.f();
1445
- }
2234
+ const { width: width2, height: height2 } = entry.contentRect;
2235
+ setSize((prev) => prev.width === width2 && prev.height === height2 ? prev : {
2236
+ width: width2,
2237
+ height: height2
2238
+ });
2239
+ });
2240
+ observer.observe(container);
2241
+ return () => observer.disconnect();
2242
+ }, [
2243
+ container
2244
+ ]);
2245
+ const { width, height } = size;
2246
+ const context = useMemo4(() => {
2247
+ return new SVGContext({
2248
+ scale,
2249
+ centered
2250
+ });
2251
+ }, [
2252
+ scale,
2253
+ centered
2254
+ ]);
2255
+ useImperativeHandle2(ref, () => context, [
2256
+ context
2257
+ ]);
2258
+ useEffect6(() => {
2259
+ if (width && height) {
2260
+ context.setSize({
2261
+ width,
2262
+ height
2263
+ });
2264
+ select9(context.svg).attr("display", "block").attr("viewBox", context.viewBox).attr("width", width).attr("height", height);
2265
+ }
2266
+ }, [
2267
+ context,
2268
+ width,
2269
+ height
2270
+ ]);
2271
+ const ready = width !== 0 && height !== 0;
2272
+ return /* @__PURE__ */ React4.createElement(SVGContextProvider, {
2273
+ value: context
2274
+ }, /* @__PURE__ */ React4.createElement("div", {
2275
+ ...composableProps(props, {
2276
+ classNames: "dx-expander"
2277
+ }),
2278
+ ref: setContainer
2279
+ }, /* @__PURE__ */ React4.createElement("svg", {
2280
+ xmlns: "http://www.w3.org/2000/svg",
2281
+ ref: context.ref,
2282
+ className: mx3("transition-opacity opacity-0 duration-1000", ready && "opacity-100")
2283
+ }, ready && children)));
1446
2284
  });
1447
2285
 
1448
2286
  // src/components/SVG/Zoom.tsx
1449
- import { useSignals as _useSignals5 } from "@preact-signals/safe-react/tracking";
1450
- import React5, { memo, useMemo as useMemo3 } from "react";
2287
+ import React5, { memo, useMemo as useMemo5 } from "react";
1451
2288
  var Zoom = /* @__PURE__ */ memo(({ extent, className, children }) => {
1452
- var _effect = _useSignals5();
1453
- try {
1454
- const options = useMemo3(() => ({
1455
- extent
1456
- }), [
1457
- JSON.stringify(extent)
1458
- ]);
1459
- const zoom = useZoom(options);
1460
- return /* @__PURE__ */ React5.createElement("g", {
1461
- ref: zoom.ref,
1462
- className
1463
- }, children);
1464
- } finally {
1465
- _effect.f();
1466
- }
2289
+ const options = useMemo5(() => ({
2290
+ extent
2291
+ }), [
2292
+ JSON.stringify(extent)
2293
+ ]);
2294
+ const zoom2 = useZoom(options);
2295
+ return /* @__PURE__ */ React5.createElement("g", {
2296
+ ref: zoom2.ref,
2297
+ className
2298
+ }, children);
1467
2299
  });
1468
2300
 
1469
2301
  // src/components/SVG/SVG.tsx
@@ -1477,7 +2309,7 @@ var SVG = {
1477
2309
  };
1478
2310
 
1479
2311
  // src/fx/pulsar.ts
1480
- import { easeCubicOut as easeCubicOut2, select as select8 } from "d3";
2312
+ import { easeCubicOut as easeCubicOut2, select as select10 } from "d3";
1481
2313
  var PROP_PULSAR_ACTIVE = "dx-pulsar-active";
1482
2314
  (function(Pulsar2) {
1483
2315
  Pulsar2.has = (group) => group.select("circle.dx-pulsar").empty();
@@ -1492,10 +2324,10 @@ var PROP_PULSAR_ACTIVE = "dx-pulsar-active";
1492
2324
  var startAnimation = (pulsar, r, options = {}) => {
1493
2325
  const { period = 1e3, duration = 800, ratio = 3 } = options;
1494
2326
  return pulsar.attr("r", r).attr("opacity", 1).transition("pulsar").delay(period - Date.now() % period).duration(Math.min(duration, period - 20)).ease(easeCubicOut2).attr("r", r * ratio).attr("opacity", -0.1).on("end", function() {
1495
- if (select8(this).property(PROP_PULSAR_ACTIVE)) {
1496
- startAnimation(select8(this), r, options);
2327
+ if (select10(this).property(PROP_PULSAR_ACTIVE)) {
2328
+ startAnimation(select10(this), r, options);
1497
2329
  } else {
1498
- select8(this).remove();
2330
+ select10(this).remove();
1499
2331
  }
1500
2332
  });
1501
2333
  };
@@ -1506,7 +2338,6 @@ export {
1506
2338
  Graph,
1507
2339
  GraphForceProjector,
1508
2340
  GraphHierarchicalProjector,
1509
- GraphInner,
1510
2341
  GraphProjector,
1511
2342
  GraphRadialProjector,
1512
2343
  GraphRelationalProjector,