@dxos/react-ui-graph 0.8.4-main.5ad4a44 → 0.8.4-main.60689f5b1c

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