@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,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-XWA2XJDX.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,6 +153,570 @@ 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 {
183
722
  _context;
@@ -314,7 +853,7 @@ var forcePoint = ({ x, y, strength = 0.01 }) => {
314
853
  };
315
854
 
316
855
  // src/graph/projector/graph-force-projector.ts
317
- 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";
318
857
  var getValue = (valueOrFunction, invoker, defaultValue) => {
319
858
  return (typeof valueOrFunction === "function" ? invoker(valueOrFunction) : valueOrFunction) ?? defaultValue;
320
859
  };
@@ -386,12 +925,7 @@ var GraphForceProjector = class extends GraphProjector {
386
925
  nodes: graph?.nodes.length,
387
926
  edges: graph?.edges.length
388
927
  }
389
- }, {
390
- F: __dxlog_file2,
391
- L: 227,
392
- S: this,
393
- C: (f, a) => f(...a)
394
- });
928
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 89, S: this });
395
929
  this._simulation.stop();
396
930
  this.mergeData(graph);
397
931
  this.updateLayout();
@@ -441,12 +975,7 @@ var GraphForceProjector = class extends GraphProjector {
441
975
  updateForces(forces) {
442
976
  log("updateForces", {
443
977
  forces
444
- }, {
445
- F: __dxlog_file2,
446
- L: 288,
447
- S: this,
448
- C: (f, a) => f(...a)
449
- });
978
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 148, S: this });
450
979
  this._simulation.nodes(this.layout.graph.nodes).force("link", maybeForce(forces?.link, (config) => {
451
980
  const force = forceLink().id((d) => d.id).links(this.layout.graph.edges.filter((edge) => edge.linkForce));
452
981
  if (config.distance != null) {
@@ -515,7 +1044,7 @@ import { log as log3 } from "@dxos/log";
515
1044
  // src/graph/projector/graph-radial-projector.ts
516
1045
  import { easeCubic, timer } from "d3";
517
1046
  import { log as log2 } from "@dxos/log";
518
- 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";
519
1048
  var GraphRadialProjector = class extends GraphProjector {
520
1049
  _timer;
521
1050
  findNode(x, y, radius) {
@@ -527,12 +1056,7 @@ var GraphRadialProjector = class extends GraphProjector {
527
1056
  nodes: graph?.nodes.length,
528
1057
  edges: graph?.edges.length
529
1058
  }
530
- }, {
531
- F: __dxlog_file3,
532
- L: 32,
533
- S: this,
534
- C: (f, a) => f(...a)
535
- });
1059
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 13, S: this });
536
1060
  this.mergeData(graph);
537
1061
  this.doRadialLayout();
538
1062
  }
@@ -628,7 +1152,7 @@ var layoutVertical = (x, y, h, n) => {
628
1152
  };
629
1153
 
630
1154
  // src/graph/projector/graph-hierarchical-projector.ts
631
- 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";
632
1156
  var GraphHierarchicalProjector = class extends GraphRadialProjector {
633
1157
  onUpdate(graph) {
634
1158
  log3("onUpdate", {
@@ -636,15 +1160,10 @@ var GraphHierarchicalProjector = class extends GraphRadialProjector {
636
1160
  nodes: graph?.nodes.length,
637
1161
  edges: graph?.edges.length
638
1162
  },
639
- selection: this.selection?.selected.value
640
- }, {
641
- F: __dxlog_file4,
642
- L: 24,
643
- S: this,
644
- C: (f, a) => f(...a)
645
- });
1163
+ selection: this.selection?.getSelectedIds()
1164
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file5, L: 8, S: this });
646
1165
  this.mergeData(graph);
647
- const selected = this.selection?.selected.value?.[0];
1166
+ const selected = this.selection?.getSelectedIds()[0];
648
1167
  if (selected) {
649
1168
  const node = this.layout.graph.nodes.find((node2) => node2.id === selected);
650
1169
  if (node) {
@@ -691,7 +1210,7 @@ var GraphHierarchicalProjector = class extends GraphRadialProjector {
691
1210
 
692
1211
  // src/graph/projector/graph-relational-projector.ts
693
1212
  import { log as log4 } from "@dxos/log";
694
- 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";
695
1214
  var GraphRelationalProjector = class extends GraphRadialProjector {
696
1215
  onUpdate(graph) {
697
1216
  log4("onUpdate", {
@@ -699,15 +1218,10 @@ var GraphRelationalProjector = class extends GraphRadialProjector {
699
1218
  nodes: graph?.nodes.length,
700
1219
  edges: graph?.edges.length
701
1220
  },
702
- selection: this.selection?.selected.value
703
- }, {
704
- F: __dxlog_file5,
705
- L: 24,
706
- S: this,
707
- C: (f, a) => f(...a)
708
- });
1221
+ selection: this.selection?.getSelectedIds()
1222
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file6, L: 8, S: this });
709
1223
  this.mergeData(graph);
710
- const selected = this.selection?.selected.value?.[0];
1224
+ const selected = this.selection?.getSelectedIds()[0];
711
1225
  if (selected) {
712
1226
  const node = this.layout.graph.nodes.find((node2) => node2.id === selected);
713
1227
  if (node) {
@@ -814,8 +1328,6 @@ var createBullets = (group, nodeId, options = {}) => {
814
1328
  };
815
1329
 
816
1330
  // src/graph/renderer/renderer.ts
817
- import { invariant as invariant2 } from "@dxos/invariant";
818
- var __dxlog_file6 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/renderer/renderer.ts";
819
1331
  var Renderer = class {
820
1332
  _context;
821
1333
  _root;
@@ -830,16 +1342,12 @@ var Renderer = class {
830
1342
  get context() {
831
1343
  return this._context;
832
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
+ */
833
1350
  get root() {
834
- invariant2(this._root.current, "SVG root is not initialized", {
835
- F: __dxlog_file6,
836
- L: 40,
837
- S: this,
838
- A: [
839
- "this._root.current",
840
- "'SVG root is not initialized'"
841
- ]
842
- });
843
1351
  return this._root.current;
844
1352
  }
845
1353
  get options() {
@@ -852,12 +1360,10 @@ var __dxlog_file7 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/rendere
852
1360
  var createLine2 = line2();
853
1361
  var GraphRenderer = class extends Renderer {
854
1362
  render(layout) {
855
- log5("render", layout, {
856
- F: __dxlog_file7,
857
- L: 63,
858
- S: this,
859
- C: (f, a) => f(...a)
860
- });
1363
+ if (!this.root) {
1364
+ return;
1365
+ }
1366
+ log5("render", layout, { "~LogMeta": "~LogMeta", F: __dxlog_file7, L: 24, S: this });
861
1367
  const root = select4(this.root);
862
1368
  if (layout.guides) {
863
1369
  root.selectAll("g.dx-guides").data([
@@ -934,6 +1440,9 @@ var GraphRenderer = class extends Renderer {
934
1440
  * @param node
935
1441
  */
936
1442
  fireBullet(node) {
1443
+ if (!this.root) {
1444
+ return;
1445
+ }
937
1446
  select4(this.root).selectAll("g.dx-edges").selectAll("path").call(createBullets(this.root, node.id));
938
1447
  }
939
1448
  };
@@ -964,14 +1473,14 @@ var createNode = (group, options) => {
964
1473
  circle.on("pointerenter", function() {
965
1474
  select4(this.closest("g.dx-node")).raise();
966
1475
  if (options.labels) {
967
- select4(this.parentElement).classed("dx-active", true).classed("dx-highlight", true);
1476
+ select4(this.parentElement).classed("dx-node-active", true).classed("dx-highlight", true);
968
1477
  }
969
1478
  });
970
1479
  group.on("pointerleave", function() {
971
1480
  if (options.labels) {
972
- select4(this).classed("dx-active", false);
1481
+ select4(this).classed("dx-node-active", false);
973
1482
  setTimeout(() => {
974
- if (!select4(this).classed("dx-active")) {
1483
+ if (!select4(this).classed("dx-node-active")) {
975
1484
  select4(this).classed("dx-highlight", false);
976
1485
  }
977
1486
  }, 300);
@@ -1000,12 +1509,7 @@ var updateNode2 = (group, options) => {
1000
1509
  } catch (error) {
1001
1510
  log5.error("updateNode", {
1002
1511
  error: error?.message
1003
- }, {
1004
- F: __dxlog_file7,
1005
- L: 305,
1006
- S: void 0,
1007
- C: (f, a) => f(...a)
1008
- });
1512
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file7, L: 202, S: void 0 });
1009
1513
  }
1010
1514
  }
1011
1515
  const groupOrTransition = options.transition ? group.transition(options.transition()) : group;
@@ -1056,12 +1560,7 @@ var updateEdge = (group, options, nodeGroup) => {
1056
1560
  } catch (error) {
1057
1561
  log5.error("updateEdge", {
1058
1562
  error: error?.message
1059
- }, {
1060
- F: __dxlog_file7,
1061
- L: 417,
1062
- S: void 0,
1063
- C: (f, a) => f(...a)
1064
- });
1563
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file7, L: 268, S: void 0 });
1065
1564
  }
1066
1565
  const groupOrTransition = options.transition ? group.transition(options.transition()) : group;
1067
1566
  groupOrTransition.selectAll("path").attr("d", function() {
@@ -1139,117 +1638,436 @@ var linkerRenderer = (root, { source, target, point } = {}) => {
1139
1638
  });
1140
1639
  };
1141
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
+
1142
1959
  // src/components/Graph/Graph.tsx
1143
1960
  var __dxlog_file8 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/components/Graph/Graph.tsx";
1144
- var GraphInner = ({ classNames, model, projector: _projector, renderer: _renderer, drag: drag2, arrows, onSelect, onInspect, ...props }, forwardedRef) => {
1145
- var _effect = _useSignals();
1146
- try {
1147
- const context = useSvgContext();
1148
- const graphRef = useRef(null);
1149
- const { projector, renderer } = useMemo(() => {
1150
- let projector2 = _projector;
1151
- if (!projector2) {
1152
- projector2 = new GraphForceProjector(context);
1153
- }
1154
- let renderer2 = _renderer;
1155
- if (!renderer2) {
1156
- renderer2 = new GraphRenderer(context, graphRef, {
1157
- ...props,
1158
- // TODO(burdon): Replace drag when projector is updated.
1159
- drag: drag2 ? createGraphDrag(context, projector2) : void 0,
1160
- arrows: {
1161
- end: arrows
1162
- },
1163
- onNodeClick: onSelect ? (node, event) => onSelect(node, event) : void 0,
1164
- onNodePointerEnter: onInspect ? (node, event) => onInspect(node, event) : void 0
1165
- });
1166
- }
1167
- return {
1168
- projector: projector2,
1169
- renderer: renderer2
1170
- };
1171
- }, [
1172
- context,
1173
- _projector,
1174
- _renderer,
1175
- drag2
1176
- ]);
1177
- useImperativeHandle(forwardedRef, () => ({
1178
- refresh: () => {
1179
- projector.updateData(model?.graph);
1180
- },
1181
- repaint: () => {
1182
- renderer.render(projector.layout);
1183
- },
1184
- findNode: (id) => {
1185
- return graphRef.current?.querySelector(`g[data-id="${id}"]`);
1186
- }
1187
- }), [
1188
- model,
1189
- projector,
1190
- renderer
1191
- ]);
1192
- useEffect(() => {
1193
- 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(() => {
1194
2012
  projector.updateData(model?.graph);
1195
- }), projector.updated.on(({ layout }) => {
2013
+ }) : void 0,
2014
+ projector.updated.on(({ layout }) => {
1196
2015
  try {
1197
2016
  renderer.render(layout);
1198
2017
  } catch (error) {
2018
+ log6.catch(error, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file8, L: 67, S: void 0 });
1199
2019
  void projector.stop();
1200
- log6.catch(error, void 0, {
1201
- F: __dxlog_file8,
1202
- L: 108,
1203
- S: void 0,
1204
- C: (f, a) => f(...a)
1205
- });
1206
2020
  }
1207
- }));
1208
- }, [
1209
- model,
1210
- projector,
1211
- renderer
1212
- ]);
1213
- useEffect(() => {
1214
- void projector.start();
1215
- return () => {
1216
- void projector.stop();
1217
- };
1218
- }, [
1219
- projector
1220
- ]);
1221
- return /* @__PURE__ */ React.createElement("g", {
1222
- ref: graphRef,
1223
- className: mx("dx-graph", classNames)
1224
- });
1225
- } finally {
1226
- _effect.f();
1227
- }
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
+ });
1228
2040
  };
1229
2041
  var Graph = /* @__PURE__ */ forwardRef(GraphInner);
1230
2042
 
1231
2043
  // src/components/Mesh/Mesh.tsx
1232
- import { geoPath, select as select5 } from "d3";
1233
- import { useEffect as useEffect2 } from "react";
2044
+ import { geoPath, select as select7 } from "d3";
2045
+ import { useEffect as useEffect4 } from "react";
1234
2046
  import { feature, mesh } from "topojson-client";
1235
2047
  var Mesh = ({ radius = 16, value = 0.5 }) => {
1236
2048
  const { svg, size } = useSvgContext();
1237
- useEffect2(() => {
1238
- if (size) {
1239
- const topology = hexTopology(size, radius, value);
1240
- const projection = hexProjection(radius);
1241
- const path = geoPath().projection(projection);
1242
- 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);
1243
- select5(svg).append("path").datum(mesh(topology, topology.objects.hexagons)).attr("class", "mesh").attr("d", path);
1244
- const redraw = (border) => {
1245
- border.attr("d", path(mesh(topology, topology.objects.hexagons, (a, b) => a.properties.fill !== b.properties.fill)));
1246
- };
1247
- select5(svg).append("path").attr("class", "border").call(redraw);
2049
+ useEffect4(() => {
2050
+ if (!size) {
2051
+ return;
1248
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);
1249
2066
  }, [
1250
2067
  svg,
1251
2068
  size,
1252
- value
2069
+ value,
2070
+ radius
1253
2071
  ]);
1254
2072
  return null;
1255
2073
  };
@@ -1361,115 +2179,123 @@ var hexProjection = (radius) => {
1361
2179
  };
1362
2180
 
1363
2181
  // src/components/SVG/Grid.tsx
1364
- import { useSignals as _useSignals2 } from "@preact-signals/safe-react/tracking";
1365
2182
  import React2 from "react";
1366
- import { mx as mx2 } from "@dxos/react-ui-theme";
2183
+ import { mx as mx2 } from "@dxos/ui-theme";
1367
2184
  var Grid = ({ classNames, ...props }) => {
1368
- var _effect = _useSignals2();
1369
- try {
1370
- const { ref } = useGrid(props);
1371
- return /* @__PURE__ */ React2.createElement("g", {
1372
- ref,
1373
- className: mx2("dx-grid", classNames)
1374
- });
1375
- } finally {
1376
- _effect.f();
1377
- }
2185
+ const { ref } = useGrid(props);
2186
+ return /* @__PURE__ */ React2.createElement("g", {
2187
+ ref,
2188
+ className: mx2("dx-grid", classNames)
2189
+ });
1378
2190
  };
1379
2191
 
1380
2192
  // src/components/SVG/Markers.tsx
1381
- import { useSignals as _useSignals3 } from "@preact-signals/safe-react/tracking";
1382
- import { select as select6 } from "d3";
1383
- 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";
1384
2195
  var Markers = ({ arrowSize, className }) => {
1385
- var _effect = _useSignals3();
1386
- try {
1387
- const ref = useRef2(null);
1388
- useEffect3(() => {
1389
- select6(ref.current).call(createMarkers({
1390
- arrowSize
1391
- }));
1392
- }, [
1393
- ref
1394
- ]);
1395
- return /* @__PURE__ */ React3.createElement("defs", {
1396
- ref,
1397
- className
1398
- });
1399
- } finally {
1400
- _effect.f();
1401
- }
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
+ });
1402
2208
  };
1403
2209
 
1404
2210
  // src/components/SVG/Root.tsx
1405
- import { useSignals as _useSignals4 } from "@preact-signals/safe-react/tracking";
1406
- import { select as select7 } from "d3";
1407
- import React4, { forwardRef as forwardRef2, useEffect as useEffect4, useImperativeHandle as useImperativeHandle2, useMemo as useMemo2 } from "react";
1408
- import { useResizeDetector } from "react-resize-detector";
1409
- import { mx as mx3 } from "@dxos/react-ui-theme";
1410
- var Root = /* @__PURE__ */ forwardRef2(({ classNames, children, ...props }, ref) => {
1411
- var _effect = _useSignals4();
1412
- try {
1413
- const { ref: containerRef, width = 0, height = 0 } = useResizeDetector({
1414
- 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
1415
2228
  });
1416
- const context = useMemo2(() => {
1417
- return new SVGContext(props);
1418
- }, [
1419
- props.scale,
1420
- props.centered
1421
- ]);
1422
- useImperativeHandle2(ref, () => context, [
1423
- context
1424
- ]);
1425
- useEffect4(() => {
1426
- if (width && height) {
1427
- context.setSize({
1428
- width,
1429
- height
1430
- });
1431
- 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;
1432
2233
  }
1433
- }, [
1434
- context,
1435
- width,
1436
- height
1437
- ]);
1438
- const ready = width !== 0 && height !== 0;
1439
- return /* @__PURE__ */ React4.createElement(SVGContextProvider, {
1440
- value: context
1441
- }, /* @__PURE__ */ React4.createElement("div", {
1442
- ref: containerRef,
1443
- className: mx3("grid grow overflow-hidden", classNames)
1444
- }, /* @__PURE__ */ React4.createElement("svg", {
1445
- xmlns: "http://www.w3.org/2000/svg",
1446
- ref: context.ref,
1447
- className: mx3("transition-opacity opacity-0 duration-1000", ready && "opacity-100")
1448
- }, ready && children)));
1449
- } finally {
1450
- _effect.f();
1451
- }
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)));
1452
2284
  });
1453
2285
 
1454
2286
  // src/components/SVG/Zoom.tsx
1455
- import { useSignals as _useSignals5 } from "@preact-signals/safe-react/tracking";
1456
- import React5, { memo, useMemo as useMemo3 } from "react";
2287
+ import React5, { memo, useMemo as useMemo5 } from "react";
1457
2288
  var Zoom = /* @__PURE__ */ memo(({ extent, className, children }) => {
1458
- var _effect = _useSignals5();
1459
- try {
1460
- const options = useMemo3(() => ({
1461
- extent
1462
- }), [
1463
- JSON.stringify(extent)
1464
- ]);
1465
- const zoom = useZoom(options);
1466
- return /* @__PURE__ */ React5.createElement("g", {
1467
- ref: zoom.ref,
1468
- className
1469
- }, children);
1470
- } finally {
1471
- _effect.f();
1472
- }
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);
1473
2299
  });
1474
2300
 
1475
2301
  // src/components/SVG/SVG.tsx
@@ -1483,7 +2309,7 @@ var SVG = {
1483
2309
  };
1484
2310
 
1485
2311
  // src/fx/pulsar.ts
1486
- import { easeCubicOut as easeCubicOut2, select as select8 } from "d3";
2312
+ import { easeCubicOut as easeCubicOut2, select as select10 } from "d3";
1487
2313
  var PROP_PULSAR_ACTIVE = "dx-pulsar-active";
1488
2314
  (function(Pulsar2) {
1489
2315
  Pulsar2.has = (group) => group.select("circle.dx-pulsar").empty();
@@ -1498,10 +2324,10 @@ var PROP_PULSAR_ACTIVE = "dx-pulsar-active";
1498
2324
  var startAnimation = (pulsar, r, options = {}) => {
1499
2325
  const { period = 1e3, duration = 800, ratio = 3 } = options;
1500
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() {
1501
- if (select8(this).property(PROP_PULSAR_ACTIVE)) {
1502
- startAnimation(select8(this), r, options);
2327
+ if (select10(this).property(PROP_PULSAR_ACTIVE)) {
2328
+ startAnimation(select10(this), r, options);
1503
2329
  } else {
1504
- select8(this).remove();
2330
+ select10(this).remove();
1505
2331
  }
1506
2332
  });
1507
2333
  };
@@ -1512,7 +2338,6 @@ export {
1512
2338
  Graph,
1513
2339
  GraphForceProjector,
1514
2340
  GraphHierarchicalProjector,
1515
- GraphInner,
1516
2341
  GraphProjector,
1517
2342
  GraphRadialProjector,
1518
2343
  GraphRelationalProjector,