@dxos/react-ui-graph 0.8.3 → 0.8.4-main.03d5cd7b56

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 (126) 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 +1109 -319
  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 +19 -19
  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 +1109 -319
  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 +19 -19
  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 +21 -12
  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 +10 -6
  22. package/dist/types/src/components/Mesh/Mesh.stories.d.ts.map +1 -1
  23. package/dist/types/src/components/SVG/Grid.d.ts.map +1 -1
  24. package/dist/types/src/components/SVG/Markers.d.ts.map +1 -1
  25. package/dist/types/src/components/SVG/Root.d.ts +4 -4
  26. package/dist/types/src/components/SVG/Root.d.ts.map +1 -1
  27. package/dist/types/src/components/SVG/SVG.d.ts +14 -17
  28. package/dist/types/src/components/SVG/SVG.d.ts.map +1 -1
  29. package/dist/types/src/components/SVG/Svg.stories.d.ts +11 -9
  30. package/dist/types/src/components/SVG/Svg.stories.d.ts.map +1 -1
  31. package/dist/types/src/components/SVG/Zoom.d.ts.map +1 -1
  32. package/dist/types/src/fx/fx.stories.d.ts +16 -15
  33. package/dist/types/src/fx/fx.stories.d.ts.map +1 -1
  34. package/dist/types/src/fx/pulsar.d.ts.map +1 -1
  35. package/dist/types/src/graph/markers.d.ts.map +1 -1
  36. package/dist/types/src/graph/projector/graph-drag.d.ts +1 -1
  37. package/dist/types/src/graph/projector/graph-drag.d.ts.map +1 -1
  38. package/dist/types/src/graph/projector/graph-force-projector.d.ts +2 -2
  39. package/dist/types/src/graph/projector/graph-force-projector.d.ts.map +1 -1
  40. package/dist/types/src/graph/projector/graph-forces.d.ts.map +1 -1
  41. package/dist/types/src/graph/projector/graph-hierarchical-projector.d.ts +2 -2
  42. package/dist/types/src/graph/projector/graph-hierarchical-projector.d.ts.map +1 -1
  43. package/dist/types/src/graph/projector/graph-projector.d.ts +4 -4
  44. package/dist/types/src/graph/projector/graph-projector.d.ts.map +1 -1
  45. package/dist/types/src/graph/projector/graph-radial-projector.d.ts +2 -2
  46. package/dist/types/src/graph/projector/graph-radial-projector.d.ts.map +1 -1
  47. package/dist/types/src/graph/projector/graph-relational-projector.d.ts +2 -2
  48. package/dist/types/src/graph/projector/graph-relational-projector.d.ts.map +1 -1
  49. package/dist/types/src/graph/projector/projector.d.ts.map +1 -1
  50. package/dist/types/src/graph/renderer/bullets.d.ts.map +1 -1
  51. package/dist/types/src/graph/renderer/graph-renderer.d.ts +1 -1
  52. package/dist/types/src/graph/renderer/graph-renderer.d.ts.map +1 -1
  53. package/dist/types/src/graph/renderer/renderer.d.ts.map +1 -1
  54. package/dist/types/src/graph/types.d.ts +1 -1
  55. package/dist/types/src/graph/types.d.ts.map +1 -1
  56. package/dist/types/src/hooks/useGrid.d.ts.map +1 -1
  57. package/dist/types/src/hooks/useStateRef.d.ts.map +1 -1
  58. package/dist/types/src/hooks/useSvgContext.d.ts.map +1 -1
  59. package/dist/types/src/hooks/useZoom.d.ts.map +1 -1
  60. package/dist/types/src/stories/hooks.stories.d.ts +15 -10
  61. package/dist/types/src/stories/hooks.stories.d.ts.map +1 -1
  62. package/dist/types/src/testing/data.d.ts +5 -5
  63. package/dist/types/src/testing/data.d.ts.map +1 -1
  64. package/dist/types/src/testing/hooks.d.ts +2 -16
  65. package/dist/types/src/testing/hooks.d.ts.map +1 -1
  66. package/dist/types/src/testing/model.d.ts +4 -8
  67. package/dist/types/src/testing/model.d.ts.map +1 -1
  68. package/dist/types/src/util/events.d.ts.map +1 -1
  69. package/dist/types/src/util/fraction.d.ts +1 -1
  70. package/dist/types/src/util/fraction.d.ts.map +1 -1
  71. package/dist/types/src/util/index.d.ts +2 -2
  72. package/dist/types/src/util/index.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 +32 -34
  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 +10 -9
  84. package/src/components/Mesh/Mesh.tsx +1 -1
  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 +14 -7
  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 +3 -3
  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 +7 -7
  102. package/src/graph/renderer/renderer.ts +1 -1
  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 +43 -36
  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/index.ts +2 -2
  112. package/src/util/screen.test.ts +1 -1
  113. package/src/util/screen.ts +1 -1
  114. package/src/util/vector.ts +1 -1
  115. package/styles/graph.css +292 -43
  116. package/dist/lib/browser/chunk-2YMRPQU7.mjs +0 -979
  117. package/dist/lib/browser/chunk-2YMRPQU7.mjs.map +0 -7
  118. package/dist/lib/node/chunk-WFHWEMPB.cjs +0 -919
  119. package/dist/lib/node/chunk-WFHWEMPB.cjs.map +0 -7
  120. package/dist/lib/node/index.cjs +0 -1547
  121. package/dist/lib/node/index.cjs.map +0 -7
  122. package/dist/lib/node/meta.json +0 -1
  123. package/dist/lib/node/testing/index.cjs +0 -214
  124. package/dist/lib/node/testing/index.cjs.map +0 -7
  125. package/dist/lib/node-esm/chunk-ITHTKQOY.mjs +0 -981
  126. package/dist/lib/node-esm/chunk-ITHTKQOY.mjs.map +0 -7
@@ -1,979 +0,0 @@
1
- // packages/ui/react-ui-graph/src/util/events.ts
2
- var EventEmitter = class {
3
- constructor() {
4
- this._listeners = /* @__PURE__ */ new Set();
5
- }
6
- clear() {
7
- this._listeners.clear();
8
- }
9
- on(onEvent) {
10
- this._listeners.add(onEvent);
11
- return () => this._listeners.delete(onEvent);
12
- }
13
- emit(value) {
14
- this._listeners.forEach((listener) => listener(value));
15
- }
16
- };
17
-
18
- // packages/ui/react-ui-graph/src/util/fraction.ts
19
- import { invariant } from "@dxos/invariant";
20
- var __dxlog_file = "/home/runner/work/dxos/dxos/packages/ui/react-ui-graph/src/util/fraction.ts";
21
- var FractionUtil = class _FractionUtil {
22
- static {
23
- /**
24
- * Convert number to fraction.
25
- * @param n
26
- */
27
- this.toFraction = (n) => {
28
- return typeof n === "number" ? [
29
- n,
30
- 1
31
- ] : n;
32
- };
33
- }
34
- static {
35
- this.validate = (...fraction) => {
36
- fraction.forEach(([n, d]) => {
37
- if (!Number.isFinite(n) || !Number.isFinite(d) || d === 0) {
38
- throw new Error(`Invalid fraction: ${n}/${d}`);
39
- }
40
- });
41
- return fraction;
42
- };
43
- }
44
- static {
45
- /**
46
- * @param {Fraction}
47
- * Convert to rounded number.
48
- */
49
- this.toNumber = ([n, d]) => {
50
- _FractionUtil.validate([
51
- n,
52
- d
53
- ]);
54
- return n / d;
55
- };
56
- }
57
- static {
58
- /**
59
- * Round the number to the nearest fraction.
60
- * Example: 3/5 => 1/1; 3/5 (precision 2) => 1/2.
61
- * @param {Fraction}
62
- * @param p Precision (e.g., 1/2, 1/4, etc.)
63
- */
64
- this.round = ([n, d], p = 1) => {
65
- _FractionUtil.validate([
66
- n,
67
- d
68
- ]);
69
- const v = Math.round(_FractionUtil.toNumber(_FractionUtil.divide([
70
- n,
71
- d
72
- ], [
73
- 1,
74
- p
75
- ])));
76
- return _FractionUtil.simplify([
77
- v,
78
- p
79
- ]);
80
- };
81
- }
82
- static {
83
- //
84
- // Boolean.
85
- //
86
- /**
87
- * Tests if equals.
88
- * @param n1
89
- * @param n2
90
- */
91
- this.equals = (n1, n2) => {
92
- return n1[0] * n2[1] === n1[1] * n2[0];
93
- };
94
- }
95
- static {
96
- /**
97
- * @param {Fraction}
98
- * True if zero.
99
- */
100
- this.isZero = ([n]) => {
101
- return n === 0;
102
- };
103
- }
104
- static {
105
- //
106
- // Normalization.
107
- //
108
- /**
109
- * Simplify fraction finding LCDs.
110
- * @param n
111
- * @param d
112
- */
113
- this.simplify = ([n, d]) => {
114
- _FractionUtil.validate([
115
- n,
116
- d
117
- ]);
118
- if (d < 1) {
119
- const t = n / d;
120
- if (Math.floor(t) === t) {
121
- n = t;
122
- d = 1;
123
- }
124
- }
125
- const fn = _FractionUtil.factors(n).reverse();
126
- const fd = _FractionUtil.factors(d).reverse();
127
- let c;
128
- for (let i = 0; i < fn.length; i++) {
129
- c = fd.find((n2) => n2 === fn[i]);
130
- if (c) {
131
- return [
132
- n / c,
133
- d / c
134
- ];
135
- }
136
- }
137
- return [
138
- n,
139
- d
140
- ];
141
- };
142
- }
143
- static {
144
- /**
145
- * Return common factors.
146
- * @param n
147
- */
148
- this.factors = (n) => {
149
- invariant(Number.isFinite(n), `Invalid number: ${n}`, {
150
- F: __dxlog_file,
151
- L: 113,
152
- S: this,
153
- A: [
154
- "Number.isFinite(n)",
155
- "`Invalid number: ${n}`"
156
- ]
157
- });
158
- const factors = [
159
- 1
160
- ];
161
- if (Number.isSafeInteger(n)) {
162
- let f = 1;
163
- do {
164
- if (n % ++f === 0) {
165
- factors.push(f);
166
- }
167
- } while (n / f > 1 && f < 100);
168
- }
169
- return factors;
170
- };
171
- }
172
- static {
173
- //
174
- // Basic arithmetic.
175
- //
176
- /**
177
- * @param n1
178
- * @param n2
179
- */
180
- this.add = (n1, n2) => {
181
- _FractionUtil.validate(n1, n2);
182
- const d = n1[1] * n2[1];
183
- const n = n1[0] * n2[1] + n2[0] * n1[1];
184
- return _FractionUtil.simplify([
185
- n,
186
- d
187
- ]);
188
- };
189
- }
190
- static {
191
- /**
192
- * @param n1
193
- * @param n2
194
- */
195
- this.subtract = (n1, n2) => {
196
- _FractionUtil.validate(n1, n2);
197
- const d = n1[1] * n2[1];
198
- const n = n1[0] * n2[1] - n2[0] * n1[1];
199
- return _FractionUtil.simplify([
200
- n,
201
- d
202
- ]);
203
- };
204
- }
205
- static {
206
- /**
207
- * @param n1
208
- * @param n2
209
- */
210
- this.multiply = (n1, n2) => {
211
- _FractionUtil.validate(n1, n2);
212
- const n = n1[0] * n2[0];
213
- const d = n1[1] * n2[1];
214
- return _FractionUtil.simplify([
215
- n,
216
- d
217
- ]);
218
- };
219
- }
220
- static {
221
- /**
222
- * @param n1
223
- * @param n2
224
- */
225
- this.divide = (n1, n2) => {
226
- _FractionUtil.validate(n1, n2);
227
- const n = n1[0] * n2[1];
228
- const d = n1[1] * n2[0];
229
- return _FractionUtil.simplify([
230
- n,
231
- d
232
- ]);
233
- };
234
- }
235
- };
236
-
237
- // packages/ui/react-ui-graph/src/util/points.ts
238
- var getCircumferencePoints = (p1, p2, r1 = 0, r2 = 0) => {
239
- const [x1, y1] = p1;
240
- const [x2, y2] = p2;
241
- const theta = Math.atan2(x2 - x1, y2 - y1);
242
- return [
243
- [
244
- // p1
245
- x1 + (r1 === 0 ? 0 : r1 * Math.sin(theta)),
246
- y1 + (r1 === 0 ? 0 : r1 * Math.cos(theta))
247
- ],
248
- [
249
- // p2
250
- x2 - (r2 === 0 ? 0 : r2 * Math.cos(Math.PI / 2 - theta)),
251
- y2 - (r2 === 0 ? 0 : r2 * Math.sin(Math.PI / 2 - theta))
252
- ]
253
- ];
254
- };
255
- var normal = (p1, p2) => {
256
- const dx = p2[0] - p1[0];
257
- const dy = p2[1] - p1[1];
258
- const len = Math.hypot(dx, dy);
259
- return [
260
- dy / len,
261
- -dx / len
262
- ];
263
- };
264
- var offsetPoints = (points, distance, closed) => {
265
- const len = points.length;
266
- const out = [];
267
- for (let i = 0; i < len; i++) {
268
- const p0 = points[(i - 1 + len) % len];
269
- const p1 = points[i];
270
- const p2 = points[(i + 1) % len];
271
- const n1 = normal(p0, p1);
272
- const n2 = normal(p1, p2);
273
- const nx = (n1[0] + n2[0]) / 2;
274
- const ny = (n1[1] + n2[1]) / 2;
275
- const norm = Math.hypot(nx, ny) || 1;
276
- out.push([
277
- p1[0] + distance * nx / norm,
278
- p1[1] + distance * ny / norm
279
- ]);
280
- if (!closed && (i === 0 || i === len - 1)) {
281
- out[i] = [
282
- p1[0] + distance * normal(p0, p1)[0],
283
- p1[1] + distance * normal(p0, p1)[1]
284
- ];
285
- }
286
- }
287
- return out;
288
- };
289
-
290
- // packages/ui/react-ui-graph/src/util/scale.ts
291
- import { zoomIdentity } from "d3";
292
- var Scale = class {
293
- constructor(_gridSize = 32) {
294
- this._gridSize = _gridSize;
295
- this.model = {
296
- // TODO(burdon): Precision depends on scale.
297
- snapValues: (array) => array.map((n) => FractionUtil.round(n)),
298
- snapVertex: ({ x, y }) => {
299
- const [sx, sy] = this.model.snapValues([
300
- x,
301
- y
302
- ]);
303
- return {
304
- x: sx,
305
- y: sy
306
- };
307
- },
308
- snapBounds: ({ x, y, width, height }) => {
309
- [x, y, width, height] = this.model.snapValues([
310
- x,
311
- y,
312
- width,
313
- height
314
- ]);
315
- return {
316
- x,
317
- y,
318
- width,
319
- height
320
- };
321
- },
322
- toValue: (value) => {
323
- return Math.round(FractionUtil.toNumber(value) * this._gridSize);
324
- },
325
- toValues: (values) => {
326
- return values.map((value) => Math.round(FractionUtil.toNumber(value) * this._gridSize));
327
- },
328
- toPoint: (point) => {
329
- const [x, y] = this.model.toValues([
330
- point.x,
331
- point.y
332
- ]);
333
- return [
334
- x,
335
- y === 0 ? 0 : -y
336
- ];
337
- },
338
- toBounds: ({ x, y, width, height }) => {
339
- const point = this.model.toPoint({
340
- x,
341
- y
342
- });
343
- const [width2, height2] = this.model.toValues([
344
- width,
345
- height
346
- ]);
347
- return {
348
- x: point[0],
349
- y: point[1] - height2,
350
- width: width2,
351
- height: height2
352
- };
353
- }
354
- };
355
- this.screen = {
356
- snapValues: (array) => array.map((n) => Math.round(n / this._gridSize) * this._gridSize),
357
- snapPoint: ([x, y]) => [
358
- x,
359
- y
360
- ].map((n) => Math.round(n / this._gridSize) * this._gridSize),
361
- snapBounds: ({ x, y, width, height }) => {
362
- [x, y, width, height] = this.screen.snapValues([
363
- x,
364
- y,
365
- width,
366
- height
367
- ]);
368
- return {
369
- x,
370
- y,
371
- width,
372
- height
373
- };
374
- },
375
- toValue: (value) => {
376
- return FractionUtil.divide(FractionUtil.toFraction(value), [
377
- this._gridSize,
378
- 1
379
- ]);
380
- },
381
- toValues: (values) => {
382
- return values.map((value) => FractionUtil.divide(FractionUtil.toFraction(value), [
383
- this._gridSize,
384
- 1
385
- ]));
386
- },
387
- toVertex: (point) => {
388
- const [x, y] = point;
389
- return {
390
- x: FractionUtil.toFraction(x / this._gridSize),
391
- y: FractionUtil.toFraction(-y / this._gridSize)
392
- };
393
- },
394
- toBounds: (bounds) => {
395
- const { x, y } = this.screen.toVertex([
396
- bounds.x,
397
- bounds.y
398
- ]);
399
- const [width, height] = this.screen.toValues([
400
- bounds.width,
401
- bounds.height
402
- ]);
403
- return {
404
- x,
405
- y: FractionUtil.subtract(y, height),
406
- width,
407
- height
408
- };
409
- }
410
- };
411
- }
412
- get gridSize() {
413
- return this._gridSize;
414
- }
415
- get transform() {
416
- return this._transform || zoomIdentity;
417
- }
418
- setTransform(transform) {
419
- if (this._transform?.x === transform.x && this._transform?.y === transform.y && this._transform?.k === transform.k) {
420
- return false;
421
- }
422
- this._transform = transform;
423
- return true;
424
- }
425
- /**
426
- * Translate a screen point to a logical point.
427
- * @param point
428
- */
429
- translate(point) {
430
- const { x, y, k } = this._transform ?? {
431
- x: 0,
432
- y: 0,
433
- k: 1
434
- };
435
- return [
436
- (point[0] - x) / k,
437
- (point[1] - y) / k
438
- ];
439
- }
440
- };
441
-
442
- // packages/ui/react-ui-graph/src/util/screen.ts
443
- var Screen = class {
444
- static {
445
- this.createBounds = ([x1, y1], [x2, y2], mod = {}) => {
446
- const { constrain, center } = mod;
447
- const x = Math.min(x1, x2);
448
- const y = Math.min(y1, y2);
449
- let width = Math.abs(x2 - x1);
450
- let height = Math.abs(y2 - y1);
451
- if (constrain) {
452
- width = height = Math.max(width, height);
453
- }
454
- if (center) {
455
- return {
456
- x: x - width,
457
- y: y - height,
458
- width: width * 2,
459
- height: height * 2
460
- };
461
- } else {
462
- return {
463
- x,
464
- y,
465
- width,
466
- height
467
- };
468
- }
469
- };
470
- }
471
- static {
472
- this.center = ({ x, y, width, height }) => {
473
- return [
474
- x + width / 2,
475
- y + height / 2
476
- ];
477
- };
478
- }
479
- static {
480
- this.contains = (bounds, point) => {
481
- const [x, y] = point;
482
- if (x < bounds.x || y < bounds.y) {
483
- return false;
484
- }
485
- if (x > bounds.x + bounds.width || y > bounds.y + bounds.height) {
486
- return false;
487
- }
488
- return true;
489
- };
490
- }
491
- static {
492
- /**
493
- * Distance between two points.
494
- * @param p1
495
- * @param p2
496
- */
497
- this.len = (p1, p2) => {
498
- return Math.sqrt(Math.pow(Math.abs(p1[0] - p2[0]), 2) + Math.pow(Math.abs(p1[1] - p2[1]), 2));
499
- };
500
- }
501
- static {
502
- /**
503
- * Distance to line segment.
504
- * https://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment#1501725
505
- * @param v1
506
- * @param v2
507
- * @param p
508
- */
509
- this.normal = (v1, v2, p) => {
510
- const d2 = (p1, p2) => Math.pow(Math.abs(p1[0] - p2[0]), 2) + Math.pow(Math.abs(p1[1] - p2[1]), 2);
511
- let d;
512
- const len2 = d2(v1, v2);
513
- if (len2 === 0) {
514
- d = d2(p, v1);
515
- } else {
516
- let t = ((p[0] - v1[0]) * (v2[0] - v1[0]) + (p[1] - v1[1]) * (v2[1] - v1[1])) / len2;
517
- t = Math.max(0, Math.min(1, t));
518
- d = d2(p, [
519
- v1[0] + t * (v2[0] - v1[0]),
520
- v1[1] + t * (v2[1] - v1[1])
521
- ]);
522
- }
523
- return Math.sqrt(d);
524
- };
525
- }
526
- };
527
-
528
- // packages/ui/react-ui-graph/src/util/vector.ts
529
- var Vector = class {
530
- static {
531
- /**
532
- * Convert to Vertex.
533
- * @param x
534
- * @param y
535
- */
536
- this.toVertex = ({ x, y }) => ({
537
- x: FractionUtil.toFraction(x),
538
- y: FractionUtil.toFraction(y)
539
- });
540
- }
541
- static {
542
- /**
543
- * Convert to Bounds.
544
- * @param bounds
545
- */
546
- this.toBounds = ({ x, y, width, height }) => ({
547
- x: FractionUtil.toFraction(x),
548
- y: FractionUtil.toFraction(y),
549
- width: FractionUtil.toFraction(width),
550
- height: FractionUtil.toFraction(height)
551
- });
552
- }
553
- static {
554
- /**
555
- * Calculate center point.
556
- * @param bounds
557
- */
558
- this.center = ({ x, y, width, height }) => {
559
- const cx = FractionUtil.add(x, FractionUtil.multiply(width, [
560
- 1,
561
- 2
562
- ]));
563
- const cy = FractionUtil.add(y, FractionUtil.multiply(height, [
564
- 1,
565
- 2
566
- ]));
567
- return {
568
- x: cx,
569
- y: cy
570
- };
571
- };
572
- }
573
- static {
574
- /**
575
- * Calculate centered left edge.
576
- * @param bounds
577
- */
578
- this.left = ({ x, y, height }) => {
579
- const cx = x;
580
- const cy = FractionUtil.add(y, FractionUtil.multiply(height, [
581
- 1,
582
- 2
583
- ]));
584
- return {
585
- x: cx,
586
- y: cy
587
- };
588
- };
589
- }
590
- static {
591
- /**
592
- * Calculate centered right edge.
593
- * @param bounds
594
- */
595
- this.right = ({ x, y, width, height }) => {
596
- const cx = FractionUtil.add(x, width);
597
- const cy = FractionUtil.add(y, FractionUtil.multiply(height, [
598
- 1,
599
- 2
600
- ]));
601
- return {
602
- x: cx,
603
- y: cy
604
- };
605
- };
606
- }
607
- };
608
-
609
- // packages/ui/react-ui-graph/src/hooks/useSvgContext.ts
610
- import { createContext, useContext } from "react";
611
- import { createRef } from "react";
612
- import { raise } from "@dxos/debug";
613
- var SVGContext = class {
614
- constructor({ scale = new Scale(), centered = true }) {
615
- this._ref = createRef();
616
- this.resized = new EventEmitter();
617
- this._scale = scale;
618
- this._centered = centered;
619
- }
620
- get ref() {
621
- return this._ref;
622
- }
623
- get svg() {
624
- return this._ref.current;
625
- }
626
- get scale() {
627
- return this._scale;
628
- }
629
- get center() {
630
- return this._center;
631
- }
632
- get size() {
633
- return this._size;
634
- }
635
- /**
636
- * https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/viewBox
637
- */
638
- get viewBox() {
639
- const [x, y] = this.center;
640
- return `${x},${y},${this._size.width},${this._size.height}`;
641
- }
642
- setSize(size) {
643
- this._size = {
644
- ...size
645
- };
646
- this._center = this._centered ? [
647
- -Math.floor(size.width / 2),
648
- -Math.floor(size.height / 2)
649
- ] : [
650
- 0,
651
- 0
652
- ];
653
- this.resized.emit(this);
654
- }
655
- setTransform(transform) {
656
- if (this._scale.setTransform(transform)) {
657
- this.resized.emit(this);
658
- }
659
- }
660
- /**
661
- * https://developer.mozilla.org/en-US/docs/Web/API/Document/elementFromPoint
662
- * @param point
663
- */
664
- elementFromPoint(point) {
665
- const [x, y] = point;
666
- const [cx, cy] = this._center;
667
- return document.elementFromPoint(cx + x, cy + y);
668
- }
669
- };
670
- var SVGContextType = createContext(void 0);
671
- var SVGContextProvider = SVGContextType.Provider;
672
- var useSvgContext = () => {
673
- return useContext(SVGContextType) ?? raise(new Error("Missing SVGRoot"));
674
- };
675
-
676
- // packages/ui/react-ui-graph/src/hooks/useGrid.ts
677
- import { line, range, select } from "d3";
678
- import { useEffect, useMemo, useRef } from "react";
679
- var createLine = line();
680
- var createGrid = (context, options) => {
681
- if (!context.size) {
682
- return [];
683
- }
684
- const paths = [];
685
- const { width, height } = context.size;
686
- const { x, y, k } = context.scale.transform;
687
- const s = 1 / k;
688
- const w = width * s;
689
- const h = height * s;
690
- const dx = -(x + width / 2) * s;
691
- const dy = -(y + height / 2) * s;
692
- if (options.axis) {
693
- const axis = [
694
- [
695
- [
696
- 0,
697
- dy
698
- ],
699
- [
700
- 0,
701
- dy + h
702
- ]
703
- ],
704
- [
705
- [
706
- dx,
707
- 0
708
- ],
709
- [
710
- dx + w,
711
- 0
712
- ]
713
- ]
714
- ];
715
- paths.push({
716
- id: "axis",
717
- class: "dx-axis",
718
- path: axis.map((line2) => createLine(line2)).join()
719
- });
720
- }
721
- if (options.grid) {
722
- const mod = (n, size, delta = 0) => Math.floor(n / size + delta) * size;
723
- const majorSize = context.scale.gridSize;
724
- const xMajor = range(-mod((x + width / 2) * s, majorSize), mod((-x + width / 2) * s, majorSize, 1), majorSize);
725
- const yMajor = range(-mod((y + height / 2) * s, majorSize), mod((-y + height / 2) * s, majorSize, 1), majorSize);
726
- const major = [
727
- ...xMajor.filter((x2) => !options.axis || x2).map((x2) => [
728
- [
729
- x2,
730
- dy
731
- ],
732
- [
733
- x2,
734
- dy + h
735
- ]
736
- ]),
737
- ...yMajor.filter((y2) => !options.axis || y2).map((y2) => [
738
- [
739
- dx,
740
- y2
741
- ],
742
- [
743
- dx + w,
744
- y2
745
- ]
746
- ])
747
- ];
748
- paths.push({
749
- id: "major",
750
- class: "dx-major",
751
- path: major.map((line2) => createLine(line2)).join()
752
- });
753
- const minorSize = Math.pow(2, Math.round(Math.log2(s * context.scale.gridSize)));
754
- if (majorSize > minorSize) {
755
- const xMinor = range(-mod((x + width / 2) * s, minorSize), mod((-x + width / 2) * s, minorSize, 1), minorSize);
756
- const yMinor = range(-mod((y + height / 2) * s, minorSize), mod((-y + height / 2) * s, minorSize, 1), minorSize);
757
- const minor = [
758
- ...xMinor.filter((x2) => xMajor.indexOf(x2) === -1).map((x2) => [
759
- [
760
- x2,
761
- dy
762
- ],
763
- [
764
- x2,
765
- dy + h
766
- ]
767
- ]),
768
- ...yMinor.filter((y2) => yMajor.indexOf(y2) === -1).map((y2) => [
769
- [
770
- dx,
771
- y2
772
- ],
773
- [
774
- dx + w,
775
- y2
776
- ]
777
- ])
778
- ];
779
- paths.push({
780
- id: "minor",
781
- class: "dx-minor",
782
- path: minor.map((line2) => createLine(line2)).join()
783
- });
784
- }
785
- }
786
- return paths;
787
- };
788
- var defaultGridOptions = {
789
- visible: true,
790
- axis: true,
791
- grid: true
792
- };
793
- var GridController = class {
794
- constructor(_ref, _context, _options) {
795
- this._ref = _ref;
796
- this._context = _context;
797
- this._options = _options;
798
- this._visible = false;
799
- this._visible = this._options.visible ?? true;
800
- }
801
- get ref() {
802
- return this._ref;
803
- }
804
- get visible() {
805
- return this._visible;
806
- }
807
- draw() {
808
- if (!this._context.size) {
809
- return;
810
- }
811
- const paths = this._visible ? createGrid(this._context, this._options) : [];
812
- const root = select(this._ref.current);
813
- root.selectAll("path").data(paths, (path) => path.id).join("path").style("pointer-events", "none").attr("d", (d) => d.path).attr("class", (d) => d.class);
814
- const transform = this._context.scale.transform;
815
- if (transform) {
816
- root.attr("transform", transform);
817
- root.attr("stroke-width", 1 / transform.k);
818
- }
819
- }
820
- setVisible(visible) {
821
- this._visible = visible;
822
- this.draw();
823
- return this;
824
- }
825
- };
826
- var useGrid = (options = defaultGridOptions) => {
827
- const ref = useRef(null);
828
- const context = useSvgContext();
829
- const controller = useMemo(() => new GridController(ref, context, options), []);
830
- useEffect(() => context.resized.on(() => controller.draw()), [
831
- context,
832
- controller
833
- ]);
834
- return controller;
835
- };
836
-
837
- // packages/ui/react-ui-graph/src/hooks/useStateRef.ts
838
- import { useEffect as useEffect2, useRef as useRef2, useState } from "react";
839
- var useStateRef = (initialValue) => {
840
- const [value, setValue] = useState(initialValue);
841
- const ref = useRef2();
842
- useEffect2(() => {
843
- ref.current = value;
844
- }, [
845
- initialValue,
846
- value
847
- ]);
848
- return [
849
- value,
850
- setValue,
851
- ref
852
- ];
853
- };
854
- var useDynamicRef = (initialValue, deps) => {
855
- const [, setValue] = useState(initialValue);
856
- const ref = useRef2(initialValue());
857
- useEffect2(() => {
858
- ref.current = initialValue();
859
- setValue(ref.current);
860
- }, deps);
861
- return ref;
862
- };
863
- var useTimestamp = (deps) => {
864
- const [{ timestamp, previous }, setTimestamp] = useState({
865
- timestamp: Date.now()
866
- });
867
- useEffect2(() => {
868
- setTimestamp({
869
- timestamp: Date.now(),
870
- previous: timestamp
871
- });
872
- }, deps ?? []);
873
- return [
874
- timestamp,
875
- () => setTimestamp({
876
- timestamp: Date.now(),
877
- previous: timestamp
878
- }),
879
- previous
880
- ];
881
- };
882
-
883
- // packages/ui/react-ui-graph/src/hooks/useZoom.ts
884
- import { select as select2, zoom, zoomIdentity as zoomIdentity2 } from "d3";
885
- import defaultsDeep from "lodash.defaultsdeep";
886
- import { useEffect as useEffect3, useMemo as useMemo2, useRef as useRef3 } from "react";
887
- var defaultZoomOptions = {
888
- enabled: true,
889
- extent: [
890
- 1 / 4,
891
- 4
892
- ],
893
- onDblClick: (zoom2) => zoom2.reset()
894
- };
895
- var ZoomHandler = class {
896
- constructor(_ref, _context, options) {
897
- this._ref = _ref;
898
- this._context = _context;
899
- this._options = defaultsDeep({}, options, defaultZoomOptions);
900
- this._enabled = this._options.enabled ?? true;
901
- this._zoom = zoom().scaleExtent(this._options.extent ?? defaultZoomOptions.extent);
902
- }
903
- /**
904
- * Gets the reference which the transform is applied to.
905
- */
906
- get ref() {
907
- return this._ref;
908
- }
909
- get zoom() {
910
- return this._zoom;
911
- }
912
- init() {
913
- this.setEnabled(this._enabled);
914
- this.reset(0);
915
- return this;
916
- }
917
- setEnabled(enable) {
918
- if (enable) {
919
- select2(this._context.svg).call(this._zoom).on(
920
- "dblclick.zoom",
921
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
922
- // @ts-ignore
923
- this._options?.onDblClick ? () => {
924
- this._options?.onDblClick(this);
925
- } : null
926
- );
927
- } else {
928
- select2(this._context.svg).on(".zoom", null);
929
- }
930
- this._enabled = enable;
931
- return this;
932
- }
933
- reset(duration = 500) {
934
- const scale = 1;
935
- const transform = zoomIdentity2.scale(scale);
936
- select2(this._context.svg).transition().duration(duration).call(this._zoom.transform, transform);
937
- return this;
938
- }
939
- };
940
- var useZoom = (options = defaultZoomOptions) => {
941
- const context = useSvgContext();
942
- const ref = useRef3(null);
943
- const handler = useMemo2(() => new ZoomHandler(ref, context, options), [
944
- context,
945
- options
946
- ]);
947
- useEffect3(() => {
948
- handler.zoom.on("zoom", ({ transform }) => {
949
- context.setTransform(transform);
950
- select2(ref.current).attr("transform", transform);
951
- });
952
- handler.init();
953
- }, [
954
- handler
955
- ]);
956
- return handler;
957
- };
958
-
959
- export {
960
- EventEmitter,
961
- FractionUtil,
962
- getCircumferencePoints,
963
- normal,
964
- offsetPoints,
965
- Scale,
966
- Screen,
967
- Vector,
968
- SVGContext,
969
- SVGContextProvider,
970
- useSvgContext,
971
- GridController,
972
- useGrid,
973
- useStateRef,
974
- useDynamicRef,
975
- useTimestamp,
976
- ZoomHandler,
977
- useZoom
978
- };
979
- //# sourceMappingURL=chunk-2YMRPQU7.mjs.map