@dxos/react-ui-graph 0.8.4-main.84f28bd → 0.8.4-main.ae835ea

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