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