@dxos/react-ui-graph 0.8.4-main.a4bbb77 → 0.8.4-main.abd8ff62ef

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 (98) hide show
  1. package/README.md +5 -7
  2. package/dist/lib/browser/chunk-EASNZZ7B.mjs +52 -0
  3. package/dist/lib/browser/chunk-EASNZZ7B.mjs.map +7 -0
  4. package/dist/lib/browser/index.mjs +1110 -396
  5. package/dist/lib/browser/index.mjs.map +4 -4
  6. package/dist/lib/browser/meta.json +1 -1
  7. package/dist/lib/browser/testing/index.mjs +16 -16
  8. package/dist/lib/browser/testing/index.mjs.map +3 -3
  9. package/dist/lib/node-esm/chunk-JSE2X4ML.mjs +54 -0
  10. package/dist/lib/node-esm/chunk-JSE2X4ML.mjs.map +7 -0
  11. package/dist/lib/node-esm/index.mjs +1110 -396
  12. package/dist/lib/node-esm/index.mjs.map +4 -4
  13. package/dist/lib/node-esm/meta.json +1 -1
  14. package/dist/lib/node-esm/testing/index.mjs +16 -16
  15. package/dist/lib/node-esm/testing/index.mjs.map +3 -3
  16. package/dist/types/src/components/Graph/Graph.d.ts +4 -5
  17. package/dist/types/src/components/Graph/Graph.d.ts.map +1 -1
  18. package/dist/types/src/components/Graph/Graph.stories.d.ts +4 -4
  19. package/dist/types/src/components/Graph/Graph.stories.d.ts.map +1 -1
  20. package/dist/types/src/components/Mesh/Mesh.d.ts.map +1 -1
  21. package/dist/types/src/components/Mesh/Mesh.stories.d.ts.map +1 -1
  22. package/dist/types/src/components/SVG/Grid.d.ts.map +1 -1
  23. package/dist/types/src/components/SVG/Markers.d.ts.map +1 -1
  24. package/dist/types/src/components/SVG/Root.d.ts +4 -4
  25. package/dist/types/src/components/SVG/Root.d.ts.map +1 -1
  26. package/dist/types/src/components/SVG/SVG.d.ts +14 -17
  27. package/dist/types/src/components/SVG/SVG.d.ts.map +1 -1
  28. package/dist/types/src/components/SVG/Svg.stories.d.ts +1 -1
  29. package/dist/types/src/components/SVG/Svg.stories.d.ts.map +1 -1
  30. package/dist/types/src/fx/fx.stories.d.ts +3 -3
  31. package/dist/types/src/fx/fx.stories.d.ts.map +1 -1
  32. package/dist/types/src/fx/pulsar.d.ts.map +1 -1
  33. package/dist/types/src/graph/markers.d.ts.map +1 -1
  34. package/dist/types/src/graph/projector/graph-drag.d.ts.map +1 -1
  35. package/dist/types/src/graph/projector/graph-force-projector.d.ts +1 -1
  36. package/dist/types/src/graph/projector/graph-force-projector.d.ts.map +1 -1
  37. package/dist/types/src/graph/projector/graph-forces.d.ts.map +1 -1
  38. package/dist/types/src/graph/projector/graph-hierarchical-projector.d.ts +1 -1
  39. package/dist/types/src/graph/projector/graph-hierarchical-projector.d.ts.map +1 -1
  40. package/dist/types/src/graph/projector/graph-projector.d.ts +2 -2
  41. package/dist/types/src/graph/projector/graph-projector.d.ts.map +1 -1
  42. package/dist/types/src/graph/projector/graph-radial-projector.d.ts +1 -1
  43. package/dist/types/src/graph/projector/graph-radial-projector.d.ts.map +1 -1
  44. package/dist/types/src/graph/projector/graph-relational-projector.d.ts +1 -1
  45. package/dist/types/src/graph/projector/graph-relational-projector.d.ts.map +1 -1
  46. package/dist/types/src/graph/projector/projector.d.ts.map +1 -1
  47. package/dist/types/src/graph/renderer/bullets.d.ts.map +1 -1
  48. package/dist/types/src/graph/renderer/graph-renderer.d.ts.map +1 -1
  49. package/dist/types/src/graph/renderer/renderer.d.ts.map +1 -1
  50. package/dist/types/src/graph/types.d.ts +1 -1
  51. package/dist/types/src/graph/types.d.ts.map +1 -1
  52. package/dist/types/src/hooks/useGrid.d.ts.map +1 -1
  53. package/dist/types/src/hooks/useStateRef.d.ts.map +1 -1
  54. package/dist/types/src/hooks/useSvgContext.d.ts.map +1 -1
  55. package/dist/types/src/hooks/useZoom.d.ts.map +1 -1
  56. package/dist/types/src/stories/hooks.stories.d.ts +4 -3
  57. package/dist/types/src/stories/hooks.stories.d.ts.map +1 -1
  58. package/dist/types/src/testing/data.d.ts +5 -5
  59. package/dist/types/src/testing/data.d.ts.map +1 -1
  60. package/dist/types/src/testing/hooks.d.ts +2 -16
  61. package/dist/types/src/testing/hooks.d.ts.map +1 -1
  62. package/dist/types/src/testing/model.d.ts +4 -8
  63. package/dist/types/src/testing/model.d.ts.map +1 -1
  64. package/dist/types/src/util/events.d.ts.map +1 -1
  65. package/dist/types/src/util/fraction.d.ts +1 -1
  66. package/dist/types/src/util/fraction.d.ts.map +1 -1
  67. package/dist/types/src/util/points.d.ts.map +1 -1
  68. package/dist/types/src/util/scale.d.ts.map +1 -1
  69. package/dist/types/src/util/screen.d.ts.map +1 -1
  70. package/dist/types/src/util/vector.d.ts.map +1 -1
  71. package/dist/types/tsconfig.tsbuildinfo +1 -1
  72. package/package.json +32 -34
  73. package/src/components/Graph/Graph.stories.tsx +50 -44
  74. package/src/components/Graph/Graph.tsx +23 -15
  75. package/src/components/Mesh/Mesh.stories.tsx +2 -3
  76. package/src/components/SVG/Grid.tsx +1 -1
  77. package/src/components/SVG/Root.tsx +14 -7
  78. package/src/components/SVG/SVG.tsx +10 -2
  79. package/src/components/SVG/Svg.stories.tsx +4 -5
  80. package/src/fx/fx.stories.tsx +7 -8
  81. package/src/graph/projector/graph-drag.ts +0 -1
  82. package/src/graph/projector/graph-force-projector.ts +1 -2
  83. package/src/graph/projector/graph-hierarchical-projector.ts +3 -4
  84. package/src/graph/projector/graph-projector.ts +2 -3
  85. package/src/graph/projector/graph-radial-projector.ts +1 -2
  86. package/src/graph/projector/graph-relational-projector.ts +3 -4
  87. package/src/graph/renderer/graph-renderer.ts +9 -4
  88. package/src/graph/types.ts +1 -1
  89. package/src/stories/hooks.stories.tsx +15 -9
  90. package/src/testing/data.ts +14 -14
  91. package/src/testing/hooks.ts +3 -4
  92. package/src/testing/model.ts +6 -6
  93. package/src/util/fraction.ts +1 -1
  94. package/styles/graph.css +292 -43
  95. package/dist/lib/browser/chunk-EX2Q43O6.mjs +0 -966
  96. package/dist/lib/browser/chunk-EX2Q43O6.mjs.map +0 -7
  97. package/dist/lib/node-esm/chunk-SWA6OT3W.mjs +0 -968
  98. package/dist/lib/node-esm/chunk-SWA6OT3W.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-EX2Q43O6.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,21 +153,581 @@ var emptyGraph = {
178
153
  edges: []
179
154
  };
180
155
 
181
- // src/graph/projector/projector.ts
182
- function _define_property(obj, key, value) {
183
- if (key in obj) {
184
- Object.defineProperty(obj, key, {
185
- value,
186
- enumerable: true,
187
- configurable: true,
188
- writable: true
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
+ }
189
190
  });
190
- } else {
191
- obj[key] = value;
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
+ }
192
407
  }
193
- return obj;
194
- }
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
+
720
+ // src/graph/projector/projector.ts
195
721
  var Projector = class {
722
+ _context;
723
+ updated = new EventEmitter();
724
+ _options;
725
+ constructor(_context, options) {
726
+ this._context = _context;
727
+ this._options = Object.assign({
728
+ idAccessor: defaultIdAccessor
729
+ }, options);
730
+ }
196
731
  get context() {
197
732
  return this._context;
198
733
  }
@@ -229,33 +764,27 @@ var Projector = class {
229
764
  }
230
765
  async onStop() {
231
766
  }
232
- constructor(_context, options) {
233
- _define_property(this, "_context", void 0);
234
- _define_property(this, "updated", void 0);
235
- _define_property(this, "_options", void 0);
236
- this._context = _context;
237
- this.updated = new EventEmitter();
238
- this._options = Object.assign({
239
- idAccessor: defaultIdAccessor
240
- }, options);
241
- }
242
767
  };
243
768
 
244
769
  // src/graph/projector/graph-projector.ts
245
- function _define_property2(obj, key, value) {
246
- if (key in obj) {
247
- Object.defineProperty(obj, key, {
248
- value,
249
- enumerable: true,
250
- configurable: true,
251
- writable: true
252
- });
253
- } else {
254
- obj[key] = value;
255
- }
256
- return obj;
257
- }
258
770
  var GraphProjector = class extends Projector {
771
+ _selection;
772
+ _layout;
773
+ // TODO(burdon): Change to object props.
774
+ constructor(context, options, _selection, layout) {
775
+ super(context, options), this._selection = _selection;
776
+ this._layout = {
777
+ id: (Math.random() * 10).toString().slice(8),
778
+ graph: {
779
+ nodes: [
780
+ ...layout?.graph.nodes ?? []
781
+ ],
782
+ edges: [
783
+ ...layout?.graph.edges ?? []
784
+ ]
785
+ }
786
+ };
787
+ }
259
788
  get layout() {
260
789
  return this._layout;
261
790
  }
@@ -301,21 +830,6 @@ var GraphProjector = class extends Projector {
301
830
  async onClear() {
302
831
  this.reset();
303
832
  }
304
- // TODO(burdon): Change to object props.
305
- constructor(context, options, _selection, layout) {
306
- super(context, options), _define_property2(this, "_selection", void 0), _define_property2(this, "_layout", void 0), this._selection = _selection;
307
- this._layout = {
308
- id: (Math.random() * 10).toString().slice(8),
309
- graph: {
310
- nodes: [
311
- ...layout?.graph.nodes ?? []
312
- ],
313
- edges: [
314
- ...layout?.graph.edges ?? []
315
- ]
316
- }
317
- };
318
- }
319
833
  };
320
834
 
321
835
  // src/graph/projector/graph-force-projector.ts
@@ -339,20 +853,7 @@ var forcePoint = ({ x, y, strength = 0.01 }) => {
339
853
  };
340
854
 
341
855
  // src/graph/projector/graph-force-projector.ts
342
- function _define_property3(obj, key, value) {
343
- if (key in obj) {
344
- Object.defineProperty(obj, key, {
345
- value,
346
- enumerable: true,
347
- configurable: true,
348
- writable: true
349
- });
350
- } else {
351
- obj[key] = value;
352
- }
353
- return obj;
354
- }
355
- 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";
356
857
  var getValue = (valueOrFunction, invoker, defaultValue) => {
357
858
  return (typeof valueOrFunction === "function" ? invoker(valueOrFunction) : valueOrFunction) ?? defaultValue;
358
859
  };
@@ -369,6 +870,9 @@ var defaultForceOptions = {
369
870
  point: true
370
871
  };
371
872
  var GraphForceProjector = class extends GraphProjector {
873
+ // https://github.com/d3/d3-force
874
+ _simulation = forceSimulation();
875
+ _timeout;
372
876
  get forces() {
373
877
  return this.options.forces ?? defaultForceOptions;
374
878
  }
@@ -421,12 +925,7 @@ var GraphForceProjector = class extends GraphProjector {
421
925
  nodes: graph?.nodes.length,
422
926
  edges: graph?.edges.length
423
927
  }
424
- }, {
425
- F: __dxlog_file2,
426
- L: 227,
427
- S: this,
428
- C: (f, a) => f(...a)
429
- });
928
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 89, S: this });
430
929
  this._simulation.stop();
431
930
  this.mergeData(graph);
432
931
  this.updateLayout();
@@ -476,12 +975,7 @@ var GraphForceProjector = class extends GraphProjector {
476
975
  updateForces(forces) {
477
976
  log("updateForces", {
478
977
  forces
479
- }, {
480
- F: __dxlog_file2,
481
- L: 288,
482
- S: this,
483
- C: (f, a) => f(...a)
484
- });
978
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 148, S: this });
485
979
  this._simulation.nodes(this.layout.graph.nodes).force("link", maybeForce(forces?.link, (config) => {
486
980
  const force = forceLink().id((d) => d.id).links(this.layout.graph.edges.filter((edge) => edge.linkForce));
487
981
  if (config.distance != null) {
@@ -542,10 +1036,6 @@ var GraphForceProjector = class extends GraphProjector {
542
1036
  return force;
543
1037
  }));
544
1038
  }
545
- constructor(...args) {
546
- super(...args), // https://github.com/d3/d3-force
547
- _define_property3(this, "_simulation", forceSimulation()), _define_property3(this, "_timeout", void 0);
548
- }
549
1039
  };
550
1040
 
551
1041
  // src/graph/projector/graph-hierarchical-projector.ts
@@ -554,21 +1044,9 @@ import { log as log3 } from "@dxos/log";
554
1044
  // src/graph/projector/graph-radial-projector.ts
555
1045
  import { easeCubic, timer } from "d3";
556
1046
  import { log as log2 } from "@dxos/log";
557
- function _define_property4(obj, key, value) {
558
- if (key in obj) {
559
- Object.defineProperty(obj, key, {
560
- value,
561
- enumerable: true,
562
- configurable: true,
563
- writable: true
564
- });
565
- } else {
566
- obj[key] = value;
567
- }
568
- return obj;
569
- }
570
- 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";
571
1048
  var GraphRadialProjector = class extends GraphProjector {
1049
+ _timer;
572
1050
  findNode(x, y, radius) {
573
1051
  return void 0;
574
1052
  }
@@ -578,12 +1056,7 @@ var GraphRadialProjector = class extends GraphProjector {
578
1056
  nodes: graph?.nodes.length,
579
1057
  edges: graph?.edges.length
580
1058
  }
581
- }, {
582
- F: __dxlog_file3,
583
- L: 32,
584
- S: this,
585
- C: (f, a) => f(...a)
586
- });
1059
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 13, S: this });
587
1060
  this.mergeData(graph);
588
1061
  this.doRadialLayout();
589
1062
  }
@@ -626,9 +1099,6 @@ var GraphRadialProjector = class extends GraphProjector {
626
1099
  });
627
1100
  this.animate();
628
1101
  }
629
- constructor(...args) {
630
- super(...args), _define_property4(this, "_timer", void 0);
631
- }
632
1102
  };
633
1103
  var updateNode = (node, [tx, ty], tr = 8) => {
634
1104
  const sx = node.x ?? tx;
@@ -682,7 +1152,7 @@ var layoutVertical = (x, y, h, n) => {
682
1152
  };
683
1153
 
684
1154
  // src/graph/projector/graph-hierarchical-projector.ts
685
- 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";
686
1156
  var GraphHierarchicalProjector = class extends GraphRadialProjector {
687
1157
  onUpdate(graph) {
688
1158
  log3("onUpdate", {
@@ -690,15 +1160,10 @@ var GraphHierarchicalProjector = class extends GraphRadialProjector {
690
1160
  nodes: graph?.nodes.length,
691
1161
  edges: graph?.edges.length
692
1162
  },
693
- selection: this.selection?.selected.value
694
- }, {
695
- F: __dxlog_file4,
696
- L: 24,
697
- S: this,
698
- C: (f, a) => f(...a)
699
- });
1163
+ selection: this.selection?.getSelectedIds()
1164
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file5, L: 8, S: this });
700
1165
  this.mergeData(graph);
701
- const selected = this.selection?.selected.value?.[0];
1166
+ const selected = this.selection?.getSelectedIds()[0];
702
1167
  if (selected) {
703
1168
  const node = this.layout.graph.nodes.find((node2) => node2.id === selected);
704
1169
  if (node) {
@@ -745,7 +1210,7 @@ var GraphHierarchicalProjector = class extends GraphRadialProjector {
745
1210
 
746
1211
  // src/graph/projector/graph-relational-projector.ts
747
1212
  import { log as log4 } from "@dxos/log";
748
- 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";
749
1214
  var GraphRelationalProjector = class extends GraphRadialProjector {
750
1215
  onUpdate(graph) {
751
1216
  log4("onUpdate", {
@@ -753,15 +1218,10 @@ var GraphRelationalProjector = class extends GraphRadialProjector {
753
1218
  nodes: graph?.nodes.length,
754
1219
  edges: graph?.edges.length
755
1220
  },
756
- selection: this.selection?.selected.value
757
- }, {
758
- F: __dxlog_file5,
759
- L: 24,
760
- S: this,
761
- C: (f, a) => f(...a)
762
- });
1221
+ selection: this.selection?.getSelectedIds()
1222
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file6, L: 8, S: this });
763
1223
  this.mergeData(graph);
764
- const selected = this.selection?.selected.value?.[0];
1224
+ const selected = this.selection?.getSelectedIds()[0];
765
1225
  if (selected) {
766
1226
  const node = this.layout.graph.nodes.find((node2) => node2.id === selected);
767
1227
  if (node) {
@@ -868,63 +1328,37 @@ var createBullets = (group, nodeId, options = {}) => {
868
1328
  };
869
1329
 
870
1330
  // src/graph/renderer/renderer.ts
871
- import { invariant as invariant2 } from "@dxos/invariant";
872
- function _define_property5(obj, key, value) {
873
- if (key in obj) {
874
- Object.defineProperty(obj, key, {
875
- value,
876
- enumerable: true,
877
- configurable: true,
878
- writable: true
879
- });
880
- } else {
881
- obj[key] = value;
882
- }
883
- return obj;
884
- }
885
- var __dxlog_file6 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/renderer/renderer.ts";
1331
+ import { invariant as invariant3 } from "@dxos/invariant";
1332
+ var __dxlog_file7 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/renderer/renderer.ts";
886
1333
  var Renderer = class {
1334
+ _context;
1335
+ _root;
1336
+ _options;
1337
+ constructor(_context, _root, options) {
1338
+ this._context = _context;
1339
+ this._root = _root;
1340
+ this._options = Object.assign({
1341
+ idAccessor: defaultIdAccessor
1342
+ }, options);
1343
+ }
887
1344
  get context() {
888
1345
  return this._context;
889
1346
  }
890
1347
  get root() {
891
- invariant2(this._root.current, "SVG root is not initialized", {
892
- F: __dxlog_file6,
893
- L: 40,
894
- S: this,
895
- A: [
896
- "this._root.current",
897
- "'SVG root is not initialized'"
898
- ]
899
- });
1348
+ invariant3(this._root.current, "SVG root is not initialized", { "~LogMeta": "~LogMeta", F: __dxlog_file7, L: 23, S: this, A: ["this._root.current", "'SVG root is not initialized'"] });
900
1349
  return this._root.current;
901
1350
  }
902
1351
  get options() {
903
1352
  return this._options;
904
1353
  }
905
- constructor(_context, _root, options) {
906
- _define_property5(this, "_context", void 0);
907
- _define_property5(this, "_root", void 0);
908
- _define_property5(this, "_options", void 0);
909
- this._context = _context;
910
- this._root = _root;
911
- this._options = Object.assign({
912
- idAccessor: defaultIdAccessor
913
- }, options);
914
- }
915
1354
  };
916
1355
 
917
1356
  // src/graph/renderer/graph-renderer.ts
918
- var __dxlog_file7 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/renderer/graph-renderer.ts";
1357
+ var __dxlog_file8 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/graph/renderer/graph-renderer.ts";
919
1358
  var createLine2 = line2();
920
1359
  var GraphRenderer = class extends Renderer {
921
1360
  render(layout) {
922
- log5("render", layout, {
923
- F: __dxlog_file7,
924
- L: 63,
925
- S: this,
926
- C: (f, a) => f(...a)
927
- });
1361
+ log5("render", layout, { "~LogMeta": "~LogMeta", F: __dxlog_file8, L: 19, S: this });
928
1362
  const root = select4(this.root);
929
1363
  if (layout.guides) {
930
1364
  root.selectAll("g.dx-guides").data([
@@ -1031,14 +1465,14 @@ var createNode = (group, options) => {
1031
1465
  circle.on("pointerenter", function() {
1032
1466
  select4(this.closest("g.dx-node")).raise();
1033
1467
  if (options.labels) {
1034
- select4(this.parentElement).classed("dx-active", true).classed("dx-highlight", true);
1468
+ select4(this.parentElement).classed("dx-node-active", true).classed("dx-highlight", true);
1035
1469
  }
1036
1470
  });
1037
1471
  group.on("pointerleave", function() {
1038
1472
  if (options.labels) {
1039
- select4(this).classed("dx-active", false);
1473
+ select4(this).classed("dx-node-active", false);
1040
1474
  setTimeout(() => {
1041
- if (!select4(this).classed("dx-active")) {
1475
+ if (!select4(this).classed("dx-node-active")) {
1042
1476
  select4(this).classed("dx-highlight", false);
1043
1477
  }
1044
1478
  }, 300);
@@ -1067,12 +1501,7 @@ var updateNode2 = (group, options) => {
1067
1501
  } catch (error) {
1068
1502
  log5.error("updateNode", {
1069
1503
  error: error?.message
1070
- }, {
1071
- F: __dxlog_file7,
1072
- L: 305,
1073
- S: void 0,
1074
- C: (f, a) => f(...a)
1075
- });
1504
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file8, L: 200, S: void 0 });
1076
1505
  }
1077
1506
  }
1078
1507
  const groupOrTransition = options.transition ? group.transition(options.transition()) : group;
@@ -1123,12 +1552,7 @@ var updateEdge = (group, options, nodeGroup) => {
1123
1552
  } catch (error) {
1124
1553
  log5.error("updateEdge", {
1125
1554
  error: error?.message
1126
- }, {
1127
- F: __dxlog_file7,
1128
- L: 417,
1129
- S: void 0,
1130
- C: (f, a) => f(...a)
1131
- });
1555
+ }, { "~LogMeta": "~LogMeta", F: __dxlog_file8, L: 266, S: void 0 });
1132
1556
  }
1133
1557
  const groupOrTransition = options.transition ? group.transition(options.transition()) : group;
1134
1558
  groupOrTransition.selectAll("path").attr("d", function() {
@@ -1206,112 +1630,422 @@ var linkerRenderer = (root, { source, target, point } = {}) => {
1206
1630
  });
1207
1631
  };
1208
1632
 
1209
- // src/components/Graph/Graph.tsx
1210
- var __dxlog_file8 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/components/Graph/Graph.tsx";
1211
- var GraphInner = ({ classNames, model, projector: _projector, renderer: _renderer, drag: drag2, arrows, onSelect, onInspect, ...props }, forwardedRef) => {
1212
- var _effect = _useSignals();
1213
- try {
1214
- const context = useSvgContext();
1215
- const graphRef = useRef(null);
1216
- const { projector, renderer } = useMemo(() => {
1217
- let projector2 = _projector;
1218
- if (!projector2) {
1219
- projector2 = new GraphForceProjector(context);
1220
- }
1221
- let renderer2 = _renderer;
1222
- if (!renderer2) {
1223
- renderer2 = new GraphRenderer(context, graphRef, {
1224
- ...props,
1225
- // TODO(burdon): Replace drag when projector is updated.
1226
- drag: drag2 ? createGraphDrag(context, projector2) : void 0,
1227
- arrows: {
1228
- end: arrows
1229
- },
1230
- onNodeClick: onSelect ? (node, event) => onSelect(node, event) : void 0,
1231
- onNodePointerEnter: onInspect ? (node, event) => onInspect(node, event) : void 0
1232
- });
1233
- }
1234
- return {
1235
- projector: projector2,
1236
- renderer: renderer2
1237
- };
1238
- }, [
1239
- context,
1240
- _projector,
1241
- _renderer,
1242
- drag2
1243
- ]);
1244
- useImperativeHandle(forwardedRef, () => ({
1245
- refresh: () => {
1246
- projector.updateData(model?.graph);
1247
- },
1248
- repaint: () => {
1249
- renderer.render(projector.layout);
1250
- },
1251
- findNode: (id) => {
1252
- return graphRef.current?.querySelector(`g[data-id="${id}"]`);
1253
- }
1254
- }), [
1255
- model,
1256
- projector,
1257
- renderer
1258
- ]);
1259
- useEffect(() => {
1260
- return combine(effect(() => {
1261
- projector.updateData(model?.graph);
1262
- }), projector.updated.on(({ layout }) => {
1263
- try {
1264
- renderer.render(layout);
1265
- } catch (error) {
1266
- void projector.stop();
1267
- log6.catch(error, void 0, {
1268
- F: __dxlog_file8,
1269
- L: 108,
1270
- S: void 0,
1271
- C: (f, a) => f(...a)
1272
- });
1273
- }
1274
- }));
1275
- }, [
1276
- model,
1277
- projector,
1278
- renderer
1279
- ]);
1280
- useEffect(() => {
1281
- void projector.start();
1282
- return () => {
1283
- void projector.stop();
1284
- };
1285
- }, [
1286
- projector
1287
- ]);
1288
- return /* @__PURE__ */ React.createElement("g", {
1289
- ref: graphRef,
1290
- className: mx("dx-graph", classNames)
1633
+ // src/hooks/useGrid.ts
1634
+ import { line as line4, range, select as select5 } from "d3";
1635
+ import { useEffect, useMemo, useRef } from "react";
1636
+
1637
+ // src/hooks/useSvgContext.ts
1638
+ import { createContext, useContext } from "react";
1639
+ import { createRef } from "react";
1640
+ import { raise } from "@dxos/debug";
1641
+ var SVGContext = class {
1642
+ _ref = createRef();
1643
+ _scale;
1644
+ _centered;
1645
+ _size;
1646
+ _center;
1647
+ resized = new EventEmitter();
1648
+ constructor({ scale = new Scale(), centered = true }) {
1649
+ this._scale = scale;
1650
+ this._centered = centered;
1651
+ }
1652
+ get ref() {
1653
+ return this._ref;
1654
+ }
1655
+ get svg() {
1656
+ return this._ref.current;
1657
+ }
1658
+ get scale() {
1659
+ return this._scale;
1660
+ }
1661
+ get center() {
1662
+ return this._center;
1663
+ }
1664
+ get size() {
1665
+ return this._size;
1666
+ }
1667
+ /**
1668
+ * https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/viewBox
1669
+ */
1670
+ get viewBox() {
1671
+ const [x, y] = this.center;
1672
+ return `${x},${y},${this._size.width},${this._size.height}`;
1673
+ }
1674
+ setSize(size) {
1675
+ this._size = {
1676
+ ...size
1677
+ };
1678
+ this._center = this._centered ? [
1679
+ -Math.floor(size.width / 2),
1680
+ -Math.floor(size.height / 2)
1681
+ ] : [
1682
+ 0,
1683
+ 0
1684
+ ];
1685
+ this.resized.emit(this);
1686
+ }
1687
+ setTransform(transform) {
1688
+ if (this._scale.setTransform(transform)) {
1689
+ this.resized.emit(this);
1690
+ }
1691
+ }
1692
+ /**
1693
+ * https://developer.mozilla.org/en-US/docs/Web/API/Document/elementFromPoint
1694
+ * @param point
1695
+ */
1696
+ elementFromPoint(point) {
1697
+ const [x, y] = point;
1698
+ const [cx, cy] = this._center;
1699
+ return document.elementFromPoint(cx + x, cy + y);
1700
+ }
1701
+ };
1702
+ var SVGContextType = createContext(void 0);
1703
+ var SVGContextProvider = SVGContextType.Provider;
1704
+ var useSvgContext = () => {
1705
+ return useContext(SVGContextType) ?? raise(new Error("Missing SVGRoot"));
1706
+ };
1707
+
1708
+ // src/hooks/useGrid.ts
1709
+ var createLine4 = line4();
1710
+ var createGrid = (context, options) => {
1711
+ if (!context.size) {
1712
+ return [];
1713
+ }
1714
+ const paths = [];
1715
+ const { width, height } = context.size;
1716
+ const { x, y, k } = context.scale.transform;
1717
+ const s = 1 / k;
1718
+ const w = width * s;
1719
+ const h = height * s;
1720
+ const dx = -(x + width / 2) * s;
1721
+ const dy = -(y + height / 2) * s;
1722
+ if (options.axis) {
1723
+ const axis = [
1724
+ [
1725
+ [
1726
+ 0,
1727
+ dy
1728
+ ],
1729
+ [
1730
+ 0,
1731
+ dy + h
1732
+ ]
1733
+ ],
1734
+ [
1735
+ [
1736
+ dx,
1737
+ 0
1738
+ ],
1739
+ [
1740
+ dx + w,
1741
+ 0
1742
+ ]
1743
+ ]
1744
+ ];
1745
+ paths.push({
1746
+ id: "axis",
1747
+ class: "dx-axis",
1748
+ path: axis.map((line5) => createLine4(line5)).join()
1749
+ });
1750
+ }
1751
+ if (options.grid) {
1752
+ const mod = (n, size, delta = 0) => Math.floor(n / size + delta) * size;
1753
+ const majorSize = context.scale.gridSize;
1754
+ const xMajor = range(-mod((x + width / 2) * s, majorSize), mod((-x + width / 2) * s, majorSize, 1), majorSize);
1755
+ const yMajor = range(-mod((y + height / 2) * s, majorSize), mod((-y + height / 2) * s, majorSize, 1), majorSize);
1756
+ const major = [
1757
+ ...xMajor.filter((x2) => !options.axis || x2).map((x2) => [
1758
+ [
1759
+ x2,
1760
+ dy
1761
+ ],
1762
+ [
1763
+ x2,
1764
+ dy + h
1765
+ ]
1766
+ ]),
1767
+ ...yMajor.filter((y2) => !options.axis || y2).map((y2) => [
1768
+ [
1769
+ dx,
1770
+ y2
1771
+ ],
1772
+ [
1773
+ dx + w,
1774
+ y2
1775
+ ]
1776
+ ])
1777
+ ];
1778
+ paths.push({
1779
+ id: "major",
1780
+ class: "dx-major",
1781
+ path: major.map((line5) => createLine4(line5)).join()
1291
1782
  });
1292
- } finally {
1293
- _effect.f();
1783
+ const minorSize = Math.pow(2, Math.round(Math.log2(s * context.scale.gridSize)));
1784
+ if (majorSize > minorSize) {
1785
+ const xMinor = range(-mod((x + width / 2) * s, minorSize), mod((-x + width / 2) * s, minorSize, 1), minorSize);
1786
+ const yMinor = range(-mod((y + height / 2) * s, minorSize), mod((-y + height / 2) * s, minorSize, 1), minorSize);
1787
+ const minor = [
1788
+ ...xMinor.filter((x2) => xMajor.indexOf(x2) === -1).map((x2) => [
1789
+ [
1790
+ x2,
1791
+ dy
1792
+ ],
1793
+ [
1794
+ x2,
1795
+ dy + h
1796
+ ]
1797
+ ]),
1798
+ ...yMinor.filter((y2) => yMajor.indexOf(y2) === -1).map((y2) => [
1799
+ [
1800
+ dx,
1801
+ y2
1802
+ ],
1803
+ [
1804
+ dx + w,
1805
+ y2
1806
+ ]
1807
+ ])
1808
+ ];
1809
+ paths.push({
1810
+ id: "minor",
1811
+ class: "dx-minor",
1812
+ path: minor.map((line5) => createLine4(line5)).join()
1813
+ });
1814
+ }
1815
+ }
1816
+ return paths;
1817
+ };
1818
+ var defaultGridOptions = {
1819
+ visible: true,
1820
+ axis: true,
1821
+ grid: true
1822
+ };
1823
+ var GridController = class {
1824
+ _ref;
1825
+ _context;
1826
+ _options;
1827
+ _visible = false;
1828
+ constructor(_ref, _context, _options) {
1829
+ this._ref = _ref;
1830
+ this._context = _context;
1831
+ this._options = _options;
1832
+ this._visible = this._options.visible ?? true;
1294
1833
  }
1834
+ get ref() {
1835
+ return this._ref;
1836
+ }
1837
+ get visible() {
1838
+ return this._visible;
1839
+ }
1840
+ draw() {
1841
+ if (!this._context.size) {
1842
+ return;
1843
+ }
1844
+ const paths = this._visible ? createGrid(this._context, this._options) : [];
1845
+ const root = select5(this._ref.current);
1846
+ root.selectAll("path").data(paths, (path) => path.id).join("path").style("pointer-events", "none").attr("d", (d) => d.path).attr("class", (d) => d.class);
1847
+ const transform = this._context.scale.transform;
1848
+ if (transform) {
1849
+ root.attr("transform", transform);
1850
+ root.attr("stroke-width", 1 / transform.k);
1851
+ }
1852
+ }
1853
+ setVisible(visible) {
1854
+ this._visible = visible;
1855
+ this.draw();
1856
+ return this;
1857
+ }
1858
+ };
1859
+ var useGrid = (options = defaultGridOptions) => {
1860
+ const ref = useRef(null);
1861
+ const context = useSvgContext();
1862
+ const controller = useMemo(() => new GridController(ref, context, options), []);
1863
+ useEffect(() => context.resized.on(() => controller.draw()), [
1864
+ context,
1865
+ controller
1866
+ ]);
1867
+ return controller;
1868
+ };
1869
+
1870
+ // src/hooks/useZoom.ts
1871
+ import { select as select6, zoom, zoomIdentity as zoomIdentity2 } from "d3";
1872
+ import defaultsDeep from "lodash.defaultsdeep";
1873
+ import { useEffect as useEffect2, useMemo as useMemo2, useRef as useRef2 } from "react";
1874
+ var defaultZoomOptions = {
1875
+ enabled: true,
1876
+ extent: [
1877
+ 1 / 4,
1878
+ 4
1879
+ ],
1880
+ onDblClick: (zoom2) => zoom2.reset()
1881
+ };
1882
+ var ZoomHandler = class {
1883
+ _ref;
1884
+ _context;
1885
+ _zoom;
1886
+ _options;
1887
+ _enabled;
1888
+ constructor(_ref, _context, options) {
1889
+ this._ref = _ref;
1890
+ this._context = _context;
1891
+ this._options = defaultsDeep({}, options, defaultZoomOptions);
1892
+ this._enabled = this._options.enabled ?? true;
1893
+ this._zoom = zoom().scaleExtent(this._options.extent ?? defaultZoomOptions.extent);
1894
+ }
1895
+ /**
1896
+ * Gets the reference which the transform is applied to.
1897
+ */
1898
+ get ref() {
1899
+ return this._ref;
1900
+ }
1901
+ get zoom() {
1902
+ return this._zoom;
1903
+ }
1904
+ init() {
1905
+ this.setEnabled(this._enabled);
1906
+ this.reset(0);
1907
+ return this;
1908
+ }
1909
+ setEnabled(enable) {
1910
+ if (enable) {
1911
+ select6(this._context.svg).call(this._zoom).on(
1912
+ "dblclick.zoom",
1913
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1914
+ // @ts-ignore
1915
+ this._options?.onDblClick ? () => {
1916
+ this._options?.onDblClick(this);
1917
+ } : null
1918
+ );
1919
+ } else {
1920
+ select6(this._context.svg).on(".zoom", null);
1921
+ }
1922
+ this._enabled = enable;
1923
+ return this;
1924
+ }
1925
+ reset(duration = 500) {
1926
+ const scale = 1;
1927
+ const transform = zoomIdentity2.scale(scale);
1928
+ select6(this._context.svg).transition().duration(duration).call(this._zoom.transform, transform);
1929
+ return this;
1930
+ }
1931
+ };
1932
+ var useZoom = (options = defaultZoomOptions) => {
1933
+ const context = useSvgContext();
1934
+ const ref = useRef2(null);
1935
+ const handler = useMemo2(() => new ZoomHandler(ref, context, options), [
1936
+ context,
1937
+ options
1938
+ ]);
1939
+ useEffect2(() => {
1940
+ handler.zoom.on("zoom", ({ transform }) => {
1941
+ context.setTransform(transform);
1942
+ select6(ref.current).attr("transform", transform);
1943
+ });
1944
+ handler.init();
1945
+ }, [
1946
+ handler
1947
+ ]);
1948
+ return handler;
1949
+ };
1950
+
1951
+ // src/components/Graph/Graph.tsx
1952
+ var __dxlog_file9 = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/components/Graph/Graph.tsx";
1953
+ var GraphInner = ({ classNames, model, projector: projectorProp, renderer: rendererProp, drag: drag2, arrows, onSelect, onInspect, ...props }, forwardedRef) => {
1954
+ const context = useSvgContext();
1955
+ const graphRef = useRef3(null);
1956
+ const { projector, renderer } = useMemo3(() => {
1957
+ let projector2 = projectorProp;
1958
+ if (!projector2) {
1959
+ projector2 = new GraphForceProjector(context);
1960
+ }
1961
+ let renderer2 = rendererProp;
1962
+ if (!renderer2) {
1963
+ renderer2 = new GraphRenderer(context, graphRef, {
1964
+ ...props,
1965
+ // TODO(burdon): Replace drag when projector is updated.
1966
+ drag: drag2 ? createGraphDrag(context, projector2) : void 0,
1967
+ arrows: {
1968
+ end: arrows
1969
+ },
1970
+ onNodeClick: onSelect ? (node, event) => onSelect(node, event) : void 0,
1971
+ onNodePointerEnter: onInspect ? (node, event) => onInspect(node, event) : void 0
1972
+ });
1973
+ }
1974
+ return {
1975
+ projector: projector2,
1976
+ renderer: renderer2
1977
+ };
1978
+ }, [
1979
+ context,
1980
+ projectorProp,
1981
+ rendererProp,
1982
+ drag2
1983
+ ]);
1984
+ useImperativeHandle(forwardedRef, () => ({
1985
+ refresh: () => {
1986
+ projector.updateData(model?.graph);
1987
+ },
1988
+ repaint: () => {
1989
+ renderer.render(projector.layout);
1990
+ },
1991
+ findNode: (id) => {
1992
+ return graphRef.current?.querySelector(`g[data-id="${id}"]`);
1993
+ }
1994
+ }), [
1995
+ model,
1996
+ projector,
1997
+ renderer
1998
+ ]);
1999
+ useEffect3(() => {
2000
+ projector.updateData(model?.graph);
2001
+ const unsubscribeModel = model ? model.subscribe(() => {
2002
+ projector.updateData(model?.graph);
2003
+ }) : void 0;
2004
+ return combine(unsubscribeModel, projector.updated.on(({ layout }) => {
2005
+ try {
2006
+ renderer.render(layout);
2007
+ } catch (error) {
2008
+ void projector.stop();
2009
+ log6.catch(error, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file9, L: 71, S: void 0 });
2010
+ }
2011
+ }));
2012
+ }, [
2013
+ model,
2014
+ projector,
2015
+ renderer
2016
+ ]);
2017
+ useEffect3(() => {
2018
+ void projector.start();
2019
+ return () => {
2020
+ void projector.stop();
2021
+ };
2022
+ }, [
2023
+ projector
2024
+ ]);
2025
+ return /* @__PURE__ */ React.createElement("g", {
2026
+ ref: graphRef,
2027
+ className: mx("dx-graph", classNames)
2028
+ });
1295
2029
  };
1296
2030
  var Graph = /* @__PURE__ */ forwardRef(GraphInner);
1297
2031
 
1298
2032
  // src/components/Mesh/Mesh.tsx
1299
- import { geoPath, select as select5 } from "d3";
1300
- import { useEffect as useEffect2 } from "react";
2033
+ import { geoPath, select as select7 } from "d3";
2034
+ import { useEffect as useEffect4 } from "react";
1301
2035
  import { feature, mesh } from "topojson-client";
1302
2036
  var Mesh = ({ radius = 16, value = 0.5 }) => {
1303
2037
  const { svg, size } = useSvgContext();
1304
- useEffect2(() => {
2038
+ useEffect4(() => {
1305
2039
  if (size) {
1306
2040
  const topology = hexTopology(size, radius, value);
1307
2041
  const projection = hexProjection(radius);
1308
2042
  const path = geoPath().projection(projection);
1309
- 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);
1310
- select5(svg).append("path").datum(mesh(topology, topology.objects.hexagons)).attr("class", "mesh").attr("d", path);
2043
+ select7(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);
2044
+ select7(svg).append("path").datum(mesh(topology, topology.objects.hexagons)).attr("class", "mesh").attr("d", path);
1311
2045
  const redraw = (border) => {
1312
2046
  border.attr("d", path(mesh(topology, topology.objects.hexagons, (a, b) => a.properties.fill !== b.properties.fill)));
1313
2047
  };
1314
- select5(svg).append("path").attr("class", "border").call(redraw);
2048
+ select7(svg).append("path").attr("class", "border").call(redraw);
1315
2049
  }
1316
2050
  }, [
1317
2051
  svg,
@@ -1428,115 +2162,96 @@ var hexProjection = (radius) => {
1428
2162
  };
1429
2163
 
1430
2164
  // src/components/SVG/Grid.tsx
1431
- import { useSignals as _useSignals2 } from "@preact-signals/safe-react/tracking";
1432
2165
  import React2 from "react";
1433
- import { mx as mx2 } from "@dxos/react-ui-theme";
2166
+ import { mx as mx2 } from "@dxos/ui-theme";
1434
2167
  var Grid = ({ classNames, ...props }) => {
1435
- var _effect = _useSignals2();
1436
- try {
1437
- const { ref } = useGrid(props);
1438
- return /* @__PURE__ */ React2.createElement("g", {
1439
- ref,
1440
- className: mx2("dx-grid", classNames)
1441
- });
1442
- } finally {
1443
- _effect.f();
1444
- }
2168
+ const { ref } = useGrid(props);
2169
+ return /* @__PURE__ */ React2.createElement("g", {
2170
+ ref,
2171
+ className: mx2("dx-grid", classNames)
2172
+ });
1445
2173
  };
1446
2174
 
1447
2175
  // src/components/SVG/Markers.tsx
1448
- import { useSignals as _useSignals3 } from "@preact-signals/safe-react/tracking";
1449
- import { select as select6 } from "d3";
1450
- import React3, { useEffect as useEffect3, useRef as useRef2 } from "react";
2176
+ import { select as select8 } from "d3";
2177
+ import React3, { useEffect as useEffect5, useRef as useRef4 } from "react";
1451
2178
  var Markers = ({ arrowSize, className }) => {
1452
- var _effect = _useSignals3();
1453
- try {
1454
- const ref = useRef2(null);
1455
- useEffect3(() => {
1456
- select6(ref.current).call(createMarkers({
1457
- arrowSize
1458
- }));
1459
- }, [
1460
- ref
1461
- ]);
1462
- return /* @__PURE__ */ React3.createElement("defs", {
1463
- ref,
1464
- className
1465
- });
1466
- } finally {
1467
- _effect.f();
1468
- }
2179
+ const ref = useRef4(null);
2180
+ useEffect5(() => {
2181
+ select8(ref.current).call(createMarkers({
2182
+ arrowSize
2183
+ }));
2184
+ }, [
2185
+ ref
2186
+ ]);
2187
+ return /* @__PURE__ */ React3.createElement("defs", {
2188
+ ref,
2189
+ className
2190
+ });
1469
2191
  };
1470
2192
 
1471
2193
  // src/components/SVG/Root.tsx
1472
- import { useSignals as _useSignals4 } from "@preact-signals/safe-react/tracking";
1473
- import { select as select7 } from "d3";
1474
- import React4, { forwardRef as forwardRef2, useEffect as useEffect4, useImperativeHandle as useImperativeHandle2, useMemo as useMemo2 } from "react";
2194
+ import { select as select9 } from "d3";
2195
+ import React4, { forwardRef as forwardRef2, useEffect as useEffect6, useImperativeHandle as useImperativeHandle2, useMemo as useMemo4 } from "react";
1475
2196
  import { useResizeDetector } from "react-resize-detector";
1476
- import { mx as mx3 } from "@dxos/react-ui-theme";
1477
- var Root = /* @__PURE__ */ forwardRef2(({ classNames, children, ...props }, ref) => {
1478
- var _effect = _useSignals4();
1479
- try {
1480
- const { ref: containerRef, width = 0, height = 0 } = useResizeDetector({
1481
- refreshRate: 200
2197
+ import { composableProps, mx as mx3 } from "@dxos/ui-theme";
2198
+ var Root = /* @__PURE__ */ forwardRef2(({ children, scale, centered, ...props }, ref) => {
2199
+ const { ref: containerRef, width = 0, height = 0 } = useResizeDetector({
2200
+ refreshRate: 200
2201
+ });
2202
+ const context = useMemo4(() => {
2203
+ return new SVGContext({
2204
+ scale,
2205
+ centered
1482
2206
  });
1483
- const context = useMemo2(() => {
1484
- return new SVGContext(props);
1485
- }, [
1486
- props.scale,
1487
- props.centered
1488
- ]);
1489
- useImperativeHandle2(ref, () => context, [
1490
- context
1491
- ]);
1492
- useEffect4(() => {
1493
- if (width && height) {
1494
- context.setSize({
1495
- width,
1496
- height
1497
- });
1498
- select7(context.svg).attr("display", "block").attr("viewBox", context.viewBox).attr("width", width).attr("height", height);
1499
- }
1500
- }, [
1501
- context,
1502
- width,
1503
- height
1504
- ]);
1505
- const ready = width !== 0 && height !== 0;
1506
- return /* @__PURE__ */ React4.createElement(SVGContextProvider, {
1507
- value: context
1508
- }, /* @__PURE__ */ React4.createElement("div", {
1509
- ref: containerRef,
1510
- className: mx3("grid grow overflow-hidden", classNames)
1511
- }, /* @__PURE__ */ React4.createElement("svg", {
1512
- xmlns: "http://www.w3.org/2000/svg",
1513
- ref: context.ref,
1514
- className: mx3("transition-opacity opacity-0 duration-1000", ready && "opacity-100")
1515
- }, ready && children)));
1516
- } finally {
1517
- _effect.f();
1518
- }
2207
+ }, [
2208
+ scale,
2209
+ centered
2210
+ ]);
2211
+ useImperativeHandle2(ref, () => context, [
2212
+ context
2213
+ ]);
2214
+ useEffect6(() => {
2215
+ if (width && height) {
2216
+ context.setSize({
2217
+ width,
2218
+ height
2219
+ });
2220
+ select9(context.svg).attr("display", "block").attr("viewBox", context.viewBox).attr("width", width).attr("height", height);
2221
+ }
2222
+ }, [
2223
+ context,
2224
+ width,
2225
+ height
2226
+ ]);
2227
+ const ready = width !== 0 && height !== 0;
2228
+ return /* @__PURE__ */ React4.createElement(SVGContextProvider, {
2229
+ value: context
2230
+ }, /* @__PURE__ */ React4.createElement("div", {
2231
+ ...composableProps(props, {
2232
+ classNames: "dx-expander"
2233
+ }),
2234
+ ref: containerRef
2235
+ }, /* @__PURE__ */ React4.createElement("svg", {
2236
+ xmlns: "http://www.w3.org/2000/svg",
2237
+ ref: context.ref,
2238
+ className: mx3("transition-opacity opacity-0 duration-1000", ready && "opacity-100")
2239
+ }, ready && children)));
1519
2240
  });
1520
2241
 
1521
2242
  // src/components/SVG/Zoom.tsx
1522
- import { useSignals as _useSignals5 } from "@preact-signals/safe-react/tracking";
1523
- import React5, { memo, useMemo as useMemo3 } from "react";
2243
+ import React5, { memo, useMemo as useMemo5 } from "react";
1524
2244
  var Zoom = /* @__PURE__ */ memo(({ extent, className, children }) => {
1525
- var _effect = _useSignals5();
1526
- try {
1527
- const options = useMemo3(() => ({
1528
- extent
1529
- }), [
1530
- JSON.stringify(extent)
1531
- ]);
1532
- const zoom = useZoom(options);
1533
- return /* @__PURE__ */ React5.createElement("g", {
1534
- ref: zoom.ref,
1535
- className
1536
- }, children);
1537
- } finally {
1538
- _effect.f();
1539
- }
2245
+ const options = useMemo5(() => ({
2246
+ extent
2247
+ }), [
2248
+ JSON.stringify(extent)
2249
+ ]);
2250
+ const zoom2 = useZoom(options);
2251
+ return /* @__PURE__ */ React5.createElement("g", {
2252
+ ref: zoom2.ref,
2253
+ className
2254
+ }, children);
1540
2255
  });
1541
2256
 
1542
2257
  // src/components/SVG/SVG.tsx
@@ -1550,7 +2265,7 @@ var SVG = {
1550
2265
  };
1551
2266
 
1552
2267
  // src/fx/pulsar.ts
1553
- import { easeCubicOut as easeCubicOut2, select as select8 } from "d3";
2268
+ import { easeCubicOut as easeCubicOut2, select as select10 } from "d3";
1554
2269
  var PROP_PULSAR_ACTIVE = "dx-pulsar-active";
1555
2270
  (function(Pulsar2) {
1556
2271
  Pulsar2.has = (group) => group.select("circle.dx-pulsar").empty();
@@ -1565,10 +2280,10 @@ var PROP_PULSAR_ACTIVE = "dx-pulsar-active";
1565
2280
  var startAnimation = (pulsar, r, options = {}) => {
1566
2281
  const { period = 1e3, duration = 800, ratio = 3 } = options;
1567
2282
  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() {
1568
- if (select8(this).property(PROP_PULSAR_ACTIVE)) {
1569
- startAnimation(select8(this), r, options);
2283
+ if (select10(this).property(PROP_PULSAR_ACTIVE)) {
2284
+ startAnimation(select10(this), r, options);
1570
2285
  } else {
1571
- select8(this).remove();
2286
+ select10(this).remove();
1572
2287
  }
1573
2288
  });
1574
2289
  };
@@ -1579,7 +2294,6 @@ export {
1579
2294
  Graph,
1580
2295
  GraphForceProjector,
1581
2296
  GraphHierarchicalProjector,
1582
- GraphInner,
1583
2297
  GraphProjector,
1584
2298
  GraphRadialProjector,
1585
2299
  GraphRelationalProjector,