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