@dxos/react-ui-graph 0.8.4-main.406dc2a → 0.8.4-main.422d1c7879

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