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

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