@dxos/react-ui-graph 0.8.4-main.67995b8 → 0.8.4-main.72ec0f3

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 (85) hide show
  1. package/dist/lib/browser/{chunk-MGVY54T5.mjs → chunk-XWA2XJDX.mjs} +466 -490
  2. package/dist/lib/{node-esm/chunk-X5YVOSXZ.mjs.map → browser/chunk-XWA2XJDX.mjs.map} +3 -3
  3. package/dist/lib/browser/index.mjs +25 -19
  4. package/dist/lib/browser/index.mjs.map +3 -3
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/browser/testing/index.mjs +1 -1
  7. package/dist/lib/browser/testing/index.mjs.map +2 -2
  8. package/dist/lib/node-esm/{chunk-X5YVOSXZ.mjs → chunk-3RLSFEEV.mjs} +466 -490
  9. package/dist/lib/{browser/chunk-MGVY54T5.mjs.map → node-esm/chunk-3RLSFEEV.mjs.map} +3 -3
  10. package/dist/lib/node-esm/index.mjs +25 -19
  11. package/dist/lib/node-esm/index.mjs.map +3 -3
  12. package/dist/lib/node-esm/meta.json +1 -1
  13. package/dist/lib/node-esm/testing/index.mjs +1 -1
  14. package/dist/lib/node-esm/testing/index.mjs.map +2 -2
  15. package/dist/types/src/components/Graph/Graph.d.ts.map +1 -1
  16. package/dist/types/src/components/Graph/Graph.stories.d.ts +15 -6
  17. package/dist/types/src/components/Graph/Graph.stories.d.ts.map +1 -1
  18. package/dist/types/src/components/Mesh/Mesh.stories.d.ts +9 -5
  19. package/dist/types/src/components/Mesh/Mesh.stories.d.ts.map +1 -1
  20. package/dist/types/src/components/SVG/Grid.d.ts.map +1 -1
  21. package/dist/types/src/components/SVG/Root.d.ts.map +1 -1
  22. package/dist/types/src/components/SVG/SVG.d.ts +2 -2
  23. package/dist/types/src/components/SVG/SVG.d.ts.map +1 -1
  24. package/dist/types/src/components/SVG/Svg.stories.d.ts +10 -8
  25. package/dist/types/src/components/SVG/Svg.stories.d.ts.map +1 -1
  26. package/dist/types/src/components/SVG/Zoom.d.ts.map +1 -1
  27. package/dist/types/src/fx/fx.stories.d.ts +16 -15
  28. package/dist/types/src/fx/fx.stories.d.ts.map +1 -1
  29. package/dist/types/src/graph/projector/graph-drag.d.ts +1 -1
  30. package/dist/types/src/graph/projector/graph-drag.d.ts.map +1 -1
  31. package/dist/types/src/graph/projector/graph-force-projector.d.ts +1 -1
  32. package/dist/types/src/graph/projector/graph-force-projector.d.ts.map +1 -1
  33. package/dist/types/src/graph/projector/graph-forces.d.ts.map +1 -1
  34. package/dist/types/src/graph/projector/graph-hierarchical-projector.d.ts +1 -1
  35. package/dist/types/src/graph/projector/graph-hierarchical-projector.d.ts.map +1 -1
  36. package/dist/types/src/graph/projector/graph-projector.d.ts +2 -2
  37. package/dist/types/src/graph/projector/graph-projector.d.ts.map +1 -1
  38. package/dist/types/src/graph/projector/graph-radial-projector.d.ts +1 -1
  39. package/dist/types/src/graph/projector/graph-radial-projector.d.ts.map +1 -1
  40. package/dist/types/src/graph/projector/graph-relational-projector.d.ts +1 -1
  41. package/dist/types/src/graph/projector/graph-relational-projector.d.ts.map +1 -1
  42. package/dist/types/src/graph/projector/projector.d.ts.map +1 -1
  43. package/dist/types/src/graph/renderer/bullets.d.ts.map +1 -1
  44. package/dist/types/src/graph/renderer/graph-renderer.d.ts +1 -1
  45. package/dist/types/src/graph/renderer/graph-renderer.d.ts.map +1 -1
  46. package/dist/types/src/graph/renderer/renderer.d.ts.map +1 -1
  47. package/dist/types/src/hooks/useZoom.d.ts.map +1 -1
  48. package/dist/types/src/stories/hooks.stories.d.ts +10 -6
  49. package/dist/types/src/stories/hooks.stories.d.ts.map +1 -1
  50. package/dist/types/src/testing/hooks.d.ts.map +1 -1
  51. package/dist/types/src/util/screen.d.ts +1 -1
  52. package/dist/types/src/util/screen.d.ts.map +1 -1
  53. package/dist/types/src/util/vector.d.ts +1 -1
  54. package/dist/types/src/util/vector.d.ts.map +1 -1
  55. package/dist/types/tsconfig.tsbuildinfo +1 -1
  56. package/package.json +24 -24
  57. package/src/components/Graph/Graph.stories.tsx +36 -38
  58. package/src/components/Graph/Graph.tsx +2 -2
  59. package/src/components/Mesh/Mesh.stories.tsx +11 -9
  60. package/src/components/Mesh/Mesh.tsx +1 -1
  61. package/src/components/SVG/Grid.tsx +1 -1
  62. package/src/components/SVG/Markers.tsx +1 -1
  63. package/src/components/SVG/Root.tsx +1 -1
  64. package/src/components/SVG/SVG.tsx +4 -3
  65. package/src/components/SVG/Svg.stories.tsx +23 -18
  66. package/src/components/SVG/Zoom.tsx +1 -1
  67. package/src/fx/fx.stories.tsx +15 -11
  68. package/src/graph/projector/graph-drag.ts +2 -1
  69. package/src/graph/projector/graph-force-projector.ts +2 -1
  70. package/src/graph/projector/graph-forces.ts +1 -1
  71. package/src/graph/projector/graph-hierarchical-projector.ts +2 -1
  72. package/src/graph/projector/graph-projector.ts +4 -3
  73. package/src/graph/projector/graph-radial-projector.ts +3 -2
  74. package/src/graph/projector/graph-relational-projector.ts +2 -1
  75. package/src/graph/projector/projector.ts +1 -1
  76. package/src/graph/renderer/bullets.ts +1 -1
  77. package/src/graph/renderer/graph-renderer.ts +4 -3
  78. package/src/graph/renderer/renderer.ts +1 -1
  79. package/src/hooks/useStateRef.ts +1 -1
  80. package/src/hooks/useZoom.ts +1 -1
  81. package/src/stories/hooks.stories.tsx +31 -30
  82. package/src/testing/hooks.ts +3 -2
  83. package/src/util/screen.test.ts +1 -1
  84. package/src/util/screen.ts +1 -1
  85. package/src/util/vector.ts +1 -1
@@ -1,8 +1,6 @@
1
1
  // src/util/events.ts
2
2
  var EventEmitter = class {
3
- constructor() {
4
- this._listeners = /* @__PURE__ */ new Set();
5
- }
3
+ _listeners = /* @__PURE__ */ new Set();
6
4
  clear() {
7
5
  this._listeners.clear();
8
6
  }
@@ -19,219 +17,195 @@ var EventEmitter = class {
19
17
  import { invariant } from "@dxos/invariant";
20
18
  var __dxlog_file = "/__w/dxos/dxos/packages/ui/react-ui-graph/src/util/fraction.ts";
21
19
  var FractionUtil = class _FractionUtil {
22
- static {
23
- /**
24
- * Convert number to fraction.
25
- * @param n
26
- */
27
- this.toFraction = (n) => {
28
- return typeof n === "number" ? [
29
- n,
30
- 1
31
- ] : n;
32
- };
33
- }
34
- static {
35
- this.validate = (...fraction) => {
36
- fraction.forEach(([n, d]) => {
37
- if (!Number.isFinite(n) || !Number.isFinite(d) || d === 0) {
38
- throw new Error(`Invalid fraction: ${n}/${d}`);
39
- }
40
- });
41
- return fraction;
42
- };
43
- }
44
- static {
45
- /**
46
- * @param {Fraction}
47
- * Convert to rounded number.
48
- */
49
- this.toNumber = ([n, d]) => {
50
- _FractionUtil.validate([
51
- n,
52
- d
53
- ]);
54
- return n / d;
55
- };
56
- }
57
- static {
58
- /**
59
- * Round the number to the nearest fraction.
60
- * Example: 3/5 => 1/1; 3/5 (precision 2) => 1/2.
61
- * @param {Fraction}
62
- * @param p Precision (e.g., 1/2, 1/4, etc.)
63
- */
64
- this.round = ([n, d], p = 1) => {
65
- _FractionUtil.validate([
66
- n,
67
- d
68
- ]);
69
- const v = Math.round(_FractionUtil.toNumber(_FractionUtil.divide([
70
- n,
71
- d
72
- ], [
73
- 1,
74
- p
75
- ])));
76
- return _FractionUtil.simplify([
77
- v,
78
- p
79
- ]);
80
- };
81
- }
82
- static {
83
- //
84
- // Boolean.
85
- //
86
- /**
87
- * Tests if equals.
88
- * @param n1
89
- * @param n2
90
- */
91
- this.equals = (n1, n2) => {
92
- return n1[0] * n2[1] === n1[1] * n2[0];
93
- };
94
- }
95
- static {
96
- /**
97
- * @param {Fraction}
98
- * True if zero.
99
- */
100
- this.isZero = ([n]) => {
101
- return n === 0;
102
- };
103
- }
104
- static {
105
- //
106
- // Normalization.
107
- //
108
- /**
109
- * Simplify fraction finding LCDs.
110
- * @param n
111
- * @param d
112
- */
113
- this.simplify = ([n, d]) => {
114
- _FractionUtil.validate([
115
- n,
116
- d
117
- ]);
118
- if (d < 1) {
119
- const t = n / d;
120
- if (Math.floor(t) === t) {
121
- n = t;
122
- d = 1;
123
- }
20
+ /**
21
+ * Convert number to fraction.
22
+ * @param n
23
+ */
24
+ static toFraction = (n) => {
25
+ return typeof n === "number" ? [
26
+ n,
27
+ 1
28
+ ] : n;
29
+ };
30
+ static validate = (...fraction) => {
31
+ fraction.forEach(([n, d]) => {
32
+ if (!Number.isFinite(n) || !Number.isFinite(d) || d === 0) {
33
+ throw new Error(`Invalid fraction: ${n}/${d}`);
124
34
  }
125
- const fn = _FractionUtil.factors(n).reverse();
126
- const fd = _FractionUtil.factors(d).reverse();
127
- let c;
128
- for (let i = 0; i < fn.length; i++) {
129
- c = fd.find((n2) => n2 === fn[i]);
130
- if (c) {
131
- return [
132
- n / c,
133
- d / c
134
- ];
135
- }
35
+ });
36
+ return fraction;
37
+ };
38
+ /**
39
+ * @param {Fraction}
40
+ * Convert to rounded number.
41
+ */
42
+ static toNumber = ([n, d]) => {
43
+ _FractionUtil.validate([
44
+ n,
45
+ d
46
+ ]);
47
+ return n / d;
48
+ };
49
+ /**
50
+ * Round the number to the nearest fraction.
51
+ * Example: 3/5 => 1/1; 3/5 (precision 2) => 1/2.
52
+ * @param {Fraction}
53
+ * @param p Precision (e.g., 1/2, 1/4, etc.)
54
+ */
55
+ static round = ([n, d], p = 1) => {
56
+ _FractionUtil.validate([
57
+ n,
58
+ d
59
+ ]);
60
+ const v = Math.round(_FractionUtil.toNumber(_FractionUtil.divide([
61
+ n,
62
+ d
63
+ ], [
64
+ 1,
65
+ p
66
+ ])));
67
+ return _FractionUtil.simplify([
68
+ v,
69
+ p
70
+ ]);
71
+ };
72
+ //
73
+ // Boolean.
74
+ //
75
+ /**
76
+ * Tests if equals.
77
+ * @param n1
78
+ * @param n2
79
+ */
80
+ static equals = (n1, n2) => {
81
+ return n1[0] * n2[1] === n1[1] * n2[0];
82
+ };
83
+ /**
84
+ * @param {Fraction}
85
+ * True if zero.
86
+ */
87
+ static isZero = ([n]) => {
88
+ return n === 0;
89
+ };
90
+ //
91
+ // Normalization.
92
+ //
93
+ /**
94
+ * Simplify fraction finding LCDs.
95
+ * @param n
96
+ * @param d
97
+ */
98
+ static simplify = ([n, d]) => {
99
+ _FractionUtil.validate([
100
+ n,
101
+ d
102
+ ]);
103
+ if (d < 1) {
104
+ const t = n / d;
105
+ if (Math.floor(t) === t) {
106
+ n = t;
107
+ d = 1;
136
108
  }
137
- return [
138
- n,
139
- d
140
- ];
141
- };
142
- }
143
- static {
144
- /**
145
- * Return common factors.
146
- * @param n
147
- */
148
- this.factors = (n) => {
149
- invariant(Number.isFinite(n), `Invalid number: ${n}`, {
150
- F: __dxlog_file,
151
- L: 113,
152
- S: this,
153
- A: [
154
- "Number.isFinite(n)",
155
- "`Invalid number: ${n}`"
156
- ]
157
- });
158
- const factors = [
159
- 1
160
- ];
161
- if (Number.isSafeInteger(n)) {
162
- let f = 1;
163
- do {
164
- if (n % ++f === 0) {
165
- factors.push(f);
166
- }
167
- } while (n / f > 1 && f < 100);
109
+ }
110
+ const fn = _FractionUtil.factors(n).reverse();
111
+ const fd = _FractionUtil.factors(d).reverse();
112
+ let c;
113
+ for (let i = 0; i < fn.length; i++) {
114
+ c = fd.find((n2) => n2 === fn[i]);
115
+ if (c) {
116
+ return [
117
+ n / c,
118
+ d / c
119
+ ];
168
120
  }
169
- return factors;
170
- };
171
- }
172
- static {
173
- //
174
- // Basic arithmetic.
175
- //
176
- /**
177
- * @param n1
178
- * @param n2
179
- */
180
- this.add = (n1, n2) => {
181
- _FractionUtil.validate(n1, n2);
182
- const d = n1[1] * n2[1];
183
- const n = n1[0] * n2[1] + n2[0] * n1[1];
184
- return _FractionUtil.simplify([
185
- n,
186
- d
187
- ]);
188
- };
189
- }
190
- static {
191
- /**
192
- * @param n1
193
- * @param n2
194
- */
195
- this.subtract = (n1, n2) => {
196
- _FractionUtil.validate(n1, n2);
197
- const d = n1[1] * n2[1];
198
- const n = n1[0] * n2[1] - n2[0] * n1[1];
199
- return _FractionUtil.simplify([
200
- n,
201
- d
202
- ]);
203
- };
204
- }
205
- static {
206
- /**
207
- * @param n1
208
- * @param n2
209
- */
210
- this.multiply = (n1, n2) => {
211
- _FractionUtil.validate(n1, n2);
212
- const n = n1[0] * n2[0];
213
- const d = n1[1] * n2[1];
214
- return _FractionUtil.simplify([
215
- n,
216
- d
217
- ]);
218
- };
219
- }
220
- static {
221
- /**
222
- * @param n1
223
- * @param n2
224
- */
225
- this.divide = (n1, n2) => {
226
- _FractionUtil.validate(n1, n2);
227
- const n = n1[0] * n2[1];
228
- const d = n1[1] * n2[0];
229
- return _FractionUtil.simplify([
230
- n,
231
- d
232
- ]);
233
- };
234
- }
121
+ }
122
+ return [
123
+ n,
124
+ d
125
+ ];
126
+ };
127
+ /**
128
+ * Return common factors.
129
+ * @param n
130
+ */
131
+ static factors = (n) => {
132
+ invariant(Number.isFinite(n), `Invalid number: ${n}`, {
133
+ F: __dxlog_file,
134
+ L: 113,
135
+ S: this,
136
+ A: [
137
+ "Number.isFinite(n)",
138
+ "`Invalid number: ${n}`"
139
+ ]
140
+ });
141
+ const factors = [
142
+ 1
143
+ ];
144
+ if (Number.isSafeInteger(n)) {
145
+ let f = 1;
146
+ do {
147
+ if (n % ++f === 0) {
148
+ factors.push(f);
149
+ }
150
+ } while (n / f > 1 && f < 100);
151
+ }
152
+ return factors;
153
+ };
154
+ //
155
+ // Basic arithmetic.
156
+ //
157
+ /**
158
+ * @param n1
159
+ * @param n2
160
+ */
161
+ static add = (n1, n2) => {
162
+ _FractionUtil.validate(n1, n2);
163
+ const d = n1[1] * n2[1];
164
+ const n = n1[0] * n2[1] + n2[0] * n1[1];
165
+ return _FractionUtil.simplify([
166
+ n,
167
+ d
168
+ ]);
169
+ };
170
+ /**
171
+ * @param n1
172
+ * @param n2
173
+ */
174
+ static subtract = (n1, n2) => {
175
+ _FractionUtil.validate(n1, n2);
176
+ const d = n1[1] * n2[1];
177
+ const n = n1[0] * n2[1] - n2[0] * n1[1];
178
+ return _FractionUtil.simplify([
179
+ n,
180
+ d
181
+ ]);
182
+ };
183
+ /**
184
+ * @param n1
185
+ * @param n2
186
+ */
187
+ static multiply = (n1, n2) => {
188
+ _FractionUtil.validate(n1, n2);
189
+ const n = n1[0] * n2[0];
190
+ const d = n1[1] * n2[1];
191
+ return _FractionUtil.simplify([
192
+ n,
193
+ d
194
+ ]);
195
+ };
196
+ /**
197
+ * @param n1
198
+ * @param n2
199
+ */
200
+ static divide = (n1, n2) => {
201
+ _FractionUtil.validate(n1, n2);
202
+ const n = n1[0] * n2[1];
203
+ const d = n1[1] * n2[0];
204
+ return _FractionUtil.simplify([
205
+ n,
206
+ d
207
+ ]);
208
+ };
235
209
  };
236
210
 
237
211
  // src/util/points.ts
@@ -290,124 +264,10 @@ var offsetPoints = (points, distance, closed) => {
290
264
  // src/util/scale.ts
291
265
  import { zoomIdentity } from "d3";
292
266
  var Scale = class {
267
+ _gridSize;
268
+ _transform;
293
269
  constructor(_gridSize = 32) {
294
270
  this._gridSize = _gridSize;
295
- this.model = {
296
- // TODO(burdon): Precision depends on scale.
297
- snapValues: (array) => array.map((n) => FractionUtil.round(n)),
298
- snapVertex: ({ x, y }) => {
299
- const [sx, sy] = this.model.snapValues([
300
- x,
301
- y
302
- ]);
303
- return {
304
- x: sx,
305
- y: sy
306
- };
307
- },
308
- snapBounds: ({ x, y, width, height }) => {
309
- [x, y, width, height] = this.model.snapValues([
310
- x,
311
- y,
312
- width,
313
- height
314
- ]);
315
- return {
316
- x,
317
- y,
318
- width,
319
- height
320
- };
321
- },
322
- toValue: (value) => {
323
- return Math.round(FractionUtil.toNumber(value) * this._gridSize);
324
- },
325
- toValues: (values) => {
326
- return values.map((value) => Math.round(FractionUtil.toNumber(value) * this._gridSize));
327
- },
328
- toPoint: (point) => {
329
- const [x, y] = this.model.toValues([
330
- point.x,
331
- point.y
332
- ]);
333
- return [
334
- x,
335
- y === 0 ? 0 : -y
336
- ];
337
- },
338
- toBounds: ({ x, y, width, height }) => {
339
- const point = this.model.toPoint({
340
- x,
341
- y
342
- });
343
- const [width2, height2] = this.model.toValues([
344
- width,
345
- height
346
- ]);
347
- return {
348
- x: point[0],
349
- y: point[1] - height2,
350
- width: width2,
351
- height: height2
352
- };
353
- }
354
- };
355
- this.screen = {
356
- snapValues: (array) => array.map((n) => Math.round(n / this._gridSize) * this._gridSize),
357
- snapPoint: ([x, y]) => [
358
- x,
359
- y
360
- ].map((n) => Math.round(n / this._gridSize) * this._gridSize),
361
- snapBounds: ({ x, y, width, height }) => {
362
- [x, y, width, height] = this.screen.snapValues([
363
- x,
364
- y,
365
- width,
366
- height
367
- ]);
368
- return {
369
- x,
370
- y,
371
- width,
372
- height
373
- };
374
- },
375
- toValue: (value) => {
376
- return FractionUtil.divide(FractionUtil.toFraction(value), [
377
- this._gridSize,
378
- 1
379
- ]);
380
- },
381
- toValues: (values) => {
382
- return values.map((value) => FractionUtil.divide(FractionUtil.toFraction(value), [
383
- this._gridSize,
384
- 1
385
- ]));
386
- },
387
- toVertex: (point) => {
388
- const [x, y] = point;
389
- return {
390
- x: FractionUtil.toFraction(x / this._gridSize),
391
- y: FractionUtil.toFraction(-y / this._gridSize)
392
- };
393
- },
394
- toBounds: (bounds) => {
395
- const { x, y } = this.screen.toVertex([
396
- bounds.x,
397
- bounds.y
398
- ]);
399
- const [width, height] = this.screen.toValues([
400
- bounds.width,
401
- bounds.height
402
- ]);
403
- return {
404
- x,
405
- y: FractionUtil.subtract(y, height),
406
- width,
407
- height
408
- };
409
- }
410
- };
411
271
  }
412
272
  get gridSize() {
413
273
  return this._gridSize;
@@ -437,173 +297,277 @@ var Scale = class {
437
297
  (point[1] - y) / k
438
298
  ];
439
299
  }
440
- };
441
-
442
- // src/util/screen.ts
443
- var Screen = class {
444
- static {
445
- this.createBounds = ([x1, y1], [x2, y2], mod = {}) => {
446
- const { constrain, center } = mod;
447
- const x = Math.min(x1, x2);
448
- const y = Math.min(y1, y2);
449
- let width = Math.abs(x2 - x1);
450
- let height = Math.abs(y2 - y1);
451
- if (constrain) {
452
- width = height = Math.max(width, height);
453
- }
454
- if (center) {
455
- return {
456
- x: x - width,
457
- y: y - height,
458
- width: width * 2,
459
- height: height * 2
460
- };
461
- } else {
462
- return {
463
- x,
464
- y,
465
- width,
466
- height
467
- };
468
- }
469
- };
470
- }
471
- static {
472
- this.center = ({ x, y, width, height }) => {
300
+ /**
301
+ * Map model to screen.
302
+ * Model points are represented by fractions (to preserve precision) and use normal cartesian coordinates.
303
+ */
304
+ model = {
305
+ // TODO(burdon): Precision depends on scale.
306
+ snapValues: (array) => array.map((n) => FractionUtil.round(n)),
307
+ snapVertex: ({ x, y }) => {
308
+ const [sx, sy] = this.model.snapValues([
309
+ x,
310
+ y
311
+ ]);
312
+ return {
313
+ x: sx,
314
+ y: sy
315
+ };
316
+ },
317
+ snapBounds: ({ x, y, width, height }) => {
318
+ [x, y, width, height] = this.model.snapValues([
319
+ x,
320
+ y,
321
+ width,
322
+ height
323
+ ]);
324
+ return {
325
+ x,
326
+ y,
327
+ width,
328
+ height
329
+ };
330
+ },
331
+ toValue: (value) => {
332
+ return Math.round(FractionUtil.toNumber(value) * this._gridSize);
333
+ },
334
+ toValues: (values) => {
335
+ return values.map((value) => Math.round(FractionUtil.toNumber(value) * this._gridSize));
336
+ },
337
+ toPoint: (point) => {
338
+ const [x, y] = this.model.toValues([
339
+ point.x,
340
+ point.y
341
+ ]);
473
342
  return [
474
- x + width / 2,
475
- y + height / 2
343
+ x,
344
+ y === 0 ? 0 : -y
476
345
  ];
477
- };
478
- }
479
- static {
480
- this.contains = (bounds, point) => {
346
+ },
347
+ toBounds: ({ x, y, width, height }) => {
348
+ const point = this.model.toPoint({
349
+ x,
350
+ y
351
+ });
352
+ const [width2, height2] = this.model.toValues([
353
+ width,
354
+ height
355
+ ]);
356
+ return {
357
+ x: point[0],
358
+ y: point[1] - height2,
359
+ width: width2,
360
+ height: height2
361
+ };
362
+ }
363
+ };
364
+ /**
365
+ * Map screen to model.
366
+ * Screen points are represented by floating numbers and use translated SVG coordinates (with the y-axis inverted).
367
+ */
368
+ screen = {
369
+ snapValues: (array) => array.map((n) => Math.round(n / this._gridSize) * this._gridSize),
370
+ snapPoint: ([x, y]) => [
371
+ x,
372
+ y
373
+ ].map((n) => Math.round(n / this._gridSize) * this._gridSize),
374
+ snapBounds: ({ x, y, width, height }) => {
375
+ [x, y, width, height] = this.screen.snapValues([
376
+ x,
377
+ y,
378
+ width,
379
+ height
380
+ ]);
381
+ return {
382
+ x,
383
+ y,
384
+ width,
385
+ height
386
+ };
387
+ },
388
+ toValue: (value) => {
389
+ return FractionUtil.divide(FractionUtil.toFraction(value), [
390
+ this._gridSize,
391
+ 1
392
+ ]);
393
+ },
394
+ toValues: (values) => {
395
+ return values.map((value) => FractionUtil.divide(FractionUtil.toFraction(value), [
396
+ this._gridSize,
397
+ 1
398
+ ]));
399
+ },
400
+ toVertex: (point) => {
481
401
  const [x, y] = point;
482
- if (x < bounds.x || y < bounds.y) {
483
- return false;
484
- }
485
- if (x > bounds.x + bounds.width || y > bounds.y + bounds.height) {
486
- return false;
487
- }
488
- return true;
489
- };
490
- }
491
- static {
492
- /**
493
- * Distance between two points.
494
- * @param p1
495
- * @param p2
496
- */
497
- this.len = (p1, p2) => {
498
- return Math.sqrt(Math.pow(Math.abs(p1[0] - p2[0]), 2) + Math.pow(Math.abs(p1[1] - p2[1]), 2));
499
- };
500
- }
501
- static {
502
- /**
503
- * Distance to line segment.
504
- * https://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment#1501725
505
- * @param v1
506
- * @param v2
507
- * @param p
508
- */
509
- this.normal = (v1, v2, p) => {
510
- const d2 = (p1, p2) => Math.pow(Math.abs(p1[0] - p2[0]), 2) + Math.pow(Math.abs(p1[1] - p2[1]), 2);
511
- let d;
512
- const len2 = d2(v1, v2);
513
- if (len2 === 0) {
514
- d = d2(p, v1);
515
- } else {
516
- let t = ((p[0] - v1[0]) * (v2[0] - v1[0]) + (p[1] - v1[1]) * (v2[1] - v1[1])) / len2;
517
- t = Math.max(0, Math.min(1, t));
518
- d = d2(p, [
519
- v1[0] + t * (v2[0] - v1[0]),
520
- v1[1] + t * (v2[1] - v1[1])
521
- ]);
522
- }
523
- return Math.sqrt(d);
524
- };
525
- }
402
+ return {
403
+ x: FractionUtil.toFraction(x / this._gridSize),
404
+ y: FractionUtil.toFraction(-y / this._gridSize)
405
+ };
406
+ },
407
+ toBounds: (bounds) => {
408
+ const { x, y } = this.screen.toVertex([
409
+ bounds.x,
410
+ bounds.y
411
+ ]);
412
+ const [width, height] = this.screen.toValues([
413
+ bounds.width,
414
+ bounds.height
415
+ ]);
416
+ return {
417
+ x,
418
+ y: FractionUtil.subtract(y, height),
419
+ width,
420
+ height
421
+ };
422
+ }
423
+ };
526
424
  };
527
425
 
528
- // src/util/vector.ts
529
- var Vector = class {
530
- static {
531
- /**
532
- * Convert to Vertex.
533
- * @param x
534
- * @param y
535
- */
536
- this.toVertex = ({ x, y }) => ({
537
- x: FractionUtil.toFraction(x),
538
- y: FractionUtil.toFraction(y)
539
- });
540
- }
541
- static {
542
- /**
543
- * Convert to Bounds.
544
- * @param bounds
545
- */
546
- this.toBounds = ({ x, y, width, height }) => ({
547
- x: FractionUtil.toFraction(x),
548
- y: FractionUtil.toFraction(y),
549
- width: FractionUtil.toFraction(width),
550
- height: FractionUtil.toFraction(height)
551
- });
552
- }
553
- static {
554
- /**
555
- * Calculate center point.
556
- * @param bounds
557
- */
558
- this.center = ({ x, y, width, height }) => {
559
- const cx = FractionUtil.add(x, FractionUtil.multiply(width, [
560
- 1,
561
- 2
562
- ]));
563
- const cy = FractionUtil.add(y, FractionUtil.multiply(height, [
564
- 1,
565
- 2
566
- ]));
426
+ // src/util/screen.ts
427
+ var Screen = class {
428
+ static createBounds = ([x1, y1], [x2, y2], mod = {}) => {
429
+ const { constrain, center } = mod;
430
+ const x = Math.min(x1, x2);
431
+ const y = Math.min(y1, y2);
432
+ let width = Math.abs(x2 - x1);
433
+ let height = Math.abs(y2 - y1);
434
+ if (constrain) {
435
+ width = height = Math.max(width, height);
436
+ }
437
+ if (center) {
567
438
  return {
568
- x: cx,
569
- y: cy
439
+ x: x - width,
440
+ y: y - height,
441
+ width: width * 2,
442
+ height: height * 2
570
443
  };
571
- };
572
- }
573
- static {
574
- /**
575
- * Calculate centered left edge.
576
- * @param bounds
577
- */
578
- this.left = ({ x, y, height }) => {
579
- const cx = x;
580
- const cy = FractionUtil.add(y, FractionUtil.multiply(height, [
581
- 1,
582
- 2
583
- ]));
444
+ } else {
584
445
  return {
585
- x: cx,
586
- y: cy
446
+ x,
447
+ y,
448
+ width,
449
+ height
587
450
  };
451
+ }
452
+ };
453
+ static center = ({ x, y, width, height }) => {
454
+ return [
455
+ x + width / 2,
456
+ y + height / 2
457
+ ];
458
+ };
459
+ static contains = (bounds, point) => {
460
+ const [x, y] = point;
461
+ if (x < bounds.x || y < bounds.y) {
462
+ return false;
463
+ }
464
+ if (x > bounds.x + bounds.width || y > bounds.y + bounds.height) {
465
+ return false;
466
+ }
467
+ return true;
468
+ };
469
+ /**
470
+ * Distance between two points.
471
+ * @param p1
472
+ * @param p2
473
+ */
474
+ static len = (p1, p2) => {
475
+ return Math.sqrt(Math.pow(Math.abs(p1[0] - p2[0]), 2) + Math.pow(Math.abs(p1[1] - p2[1]), 2));
476
+ };
477
+ /**
478
+ * Distance to line segment.
479
+ * https://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment#1501725
480
+ * @param v1
481
+ * @param v2
482
+ * @param p
483
+ */
484
+ static normal = (v1, v2, p) => {
485
+ const d2 = (p1, p2) => Math.pow(Math.abs(p1[0] - p2[0]), 2) + Math.pow(Math.abs(p1[1] - p2[1]), 2);
486
+ let d;
487
+ const len2 = d2(v1, v2);
488
+ if (len2 === 0) {
489
+ d = d2(p, v1);
490
+ } else {
491
+ let t = ((p[0] - v1[0]) * (v2[0] - v1[0]) + (p[1] - v1[1]) * (v2[1] - v1[1])) / len2;
492
+ t = Math.max(0, Math.min(1, t));
493
+ d = d2(p, [
494
+ v1[0] + t * (v2[0] - v1[0]),
495
+ v1[1] + t * (v2[1] - v1[1])
496
+ ]);
497
+ }
498
+ return Math.sqrt(d);
499
+ };
500
+ };
501
+
502
+ // src/util/vector.ts
503
+ var Vector = class {
504
+ /**
505
+ * Convert to Vertex.
506
+ * @param x
507
+ * @param y
508
+ */
509
+ static toVertex = ({ x, y }) => ({
510
+ x: FractionUtil.toFraction(x),
511
+ y: FractionUtil.toFraction(y)
512
+ });
513
+ /**
514
+ * Convert to Bounds.
515
+ * @param bounds
516
+ */
517
+ static toBounds = ({ x, y, width, height }) => ({
518
+ x: FractionUtil.toFraction(x),
519
+ y: FractionUtil.toFraction(y),
520
+ width: FractionUtil.toFraction(width),
521
+ height: FractionUtil.toFraction(height)
522
+ });
523
+ /**
524
+ * Calculate center point.
525
+ * @param bounds
526
+ */
527
+ static center = ({ x, y, width, height }) => {
528
+ const cx = FractionUtil.add(x, FractionUtil.multiply(width, [
529
+ 1,
530
+ 2
531
+ ]));
532
+ const cy = FractionUtil.add(y, FractionUtil.multiply(height, [
533
+ 1,
534
+ 2
535
+ ]));
536
+ return {
537
+ x: cx,
538
+ y: cy
588
539
  };
589
- }
590
- static {
591
- /**
592
- * Calculate centered right edge.
593
- * @param bounds
594
- */
595
- this.right = ({ x, y, width, height }) => {
596
- const cx = FractionUtil.add(x, width);
597
- const cy = FractionUtil.add(y, FractionUtil.multiply(height, [
598
- 1,
599
- 2
600
- ]));
601
- return {
602
- x: cx,
603
- y: cy
604
- };
540
+ };
541
+ /**
542
+ * Calculate centered left edge.
543
+ * @param bounds
544
+ */
545
+ static left = ({ x, y, height }) => {
546
+ const cx = x;
547
+ const cy = FractionUtil.add(y, FractionUtil.multiply(height, [
548
+ 1,
549
+ 2
550
+ ]));
551
+ return {
552
+ x: cx,
553
+ y: cy
605
554
  };
606
- }
555
+ };
556
+ /**
557
+ * Calculate centered right edge.
558
+ * @param bounds
559
+ */
560
+ static right = ({ x, y, width, height }) => {
561
+ const cx = FractionUtil.add(x, width);
562
+ const cy = FractionUtil.add(y, FractionUtil.multiply(height, [
563
+ 1,
564
+ 2
565
+ ]));
566
+ return {
567
+ x: cx,
568
+ y: cy
569
+ };
570
+ };
607
571
  };
608
572
 
609
573
  // src/hooks/useSvgContext.ts
@@ -611,9 +575,13 @@ import { createContext, useContext } from "react";
611
575
  import { createRef } from "react";
612
576
  import { raise } from "@dxos/debug";
613
577
  var SVGContext = class {
578
+ _ref = createRef();
579
+ _scale;
580
+ _centered;
581
+ _size;
582
+ _center;
583
+ resized = new EventEmitter();
614
584
  constructor({ scale = new Scale(), centered = true }) {
615
- this._ref = createRef();
616
- this.resized = new EventEmitter();
617
585
  this._scale = scale;
618
586
  this._centered = centered;
619
587
  }
@@ -791,11 +759,14 @@ var defaultGridOptions = {
791
759
  grid: true
792
760
  };
793
761
  var GridController = class {
762
+ _ref;
763
+ _context;
764
+ _options;
765
+ _visible = false;
794
766
  constructor(_ref, _context, _options) {
795
767
  this._ref = _ref;
796
768
  this._context = _context;
797
769
  this._options = _options;
798
- this._visible = false;
799
770
  this._visible = this._options.visible ?? true;
800
771
  }
801
772
  get ref() {
@@ -838,7 +809,7 @@ var useGrid = (options = defaultGridOptions) => {
838
809
  import { useEffect as useEffect2, useRef as useRef2, useState } from "react";
839
810
  var useStateRef = (initialValue) => {
840
811
  const [value, setValue] = useState(initialValue);
841
- const ref = useRef2();
812
+ const ref = useRef2(null);
842
813
  useEffect2(() => {
843
814
  ref.current = value;
844
815
  }, [
@@ -893,6 +864,11 @@ var defaultZoomOptions = {
893
864
  onDblClick: (zoom2) => zoom2.reset()
894
865
  };
895
866
  var ZoomHandler = class {
867
+ _ref;
868
+ _context;
869
+ _zoom;
870
+ _options;
871
+ _enabled;
896
872
  constructor(_ref, _context, options) {
897
873
  this._ref = _ref;
898
874
  this._context = _context;
@@ -976,4 +952,4 @@ export {
976
952
  ZoomHandler,
977
953
  useZoom
978
954
  };
979
- //# sourceMappingURL=chunk-MGVY54T5.mjs.map
955
+ //# sourceMappingURL=chunk-XWA2XJDX.mjs.map