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