@dxos/react-ui-graph 0.8.4-main.fffef41 → 0.9.0

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