@dxos/react-ui-graph 0.8.4-main.dedc0f3 → 0.8.4-main.e8ec1fe

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 (35) hide show
  1. package/dist/lib/browser/{chunk-PDEA4KBO.mjs → chunk-XWA2XJDX.mjs} +487 -498
  2. package/dist/lib/browser/chunk-XWA2XJDX.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +43 -110
  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/node-esm/{chunk-NJGJKPL7.mjs → chunk-3RLSFEEV.mjs} +487 -498
  8. package/dist/lib/node-esm/chunk-3RLSFEEV.mjs.map +7 -0
  9. package/dist/lib/node-esm/index.mjs +43 -110
  10. package/dist/lib/node-esm/index.mjs.map +3 -3
  11. package/dist/lib/node-esm/meta.json +1 -1
  12. package/dist/lib/node-esm/testing/index.mjs +1 -1
  13. package/dist/types/src/components/Graph/Graph.stories.d.ts +1 -1
  14. package/dist/types/src/components/Graph/Graph.stories.d.ts.map +1 -1
  15. package/dist/types/src/components/Mesh/Mesh.stories.d.ts +3 -1
  16. package/dist/types/src/components/Mesh/Mesh.stories.d.ts.map +1 -1
  17. package/dist/types/src/components/SVG/SVG.d.ts.map +1 -1
  18. package/dist/types/src/components/SVG/Svg.stories.d.ts +3 -1
  19. package/dist/types/src/components/SVG/Svg.stories.d.ts.map +1 -1
  20. package/dist/types/src/fx/fx.stories.d.ts +3 -1
  21. package/dist/types/src/fx/fx.stories.d.ts.map +1 -1
  22. package/dist/types/src/stories/hooks.stories.d.ts +3 -1
  23. package/dist/types/src/stories/hooks.stories.d.ts.map +1 -1
  24. package/dist/types/tsconfig.tsbuildinfo +1 -1
  25. package/package.json +24 -24
  26. package/src/components/Graph/Graph.stories.tsx +15 -16
  27. package/src/components/Graph/Graph.tsx +1 -1
  28. package/src/components/Mesh/Mesh.stories.tsx +5 -4
  29. package/src/components/SVG/Markers.tsx +1 -1
  30. package/src/components/SVG/Svg.stories.tsx +5 -4
  31. package/src/fx/fx.stories.tsx +6 -5
  32. package/src/hooks/useStateRef.ts +1 -1
  33. package/src/stories/hooks.stories.tsx +7 -6
  34. package/dist/lib/browser/chunk-PDEA4KBO.mjs.map +0 -7
  35. package/dist/lib/node-esm/chunk-NJGJKPL7.mjs.map +0 -7
@@ -1,18 +1,6 @@
1
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
2
  var EventEmitter = class {
3
+ _listeners = /* @__PURE__ */ new Set();
16
4
  clear() {
17
5
  this._listeners.clear();
18
6
  }
@@ -23,161 +11,202 @@ var EventEmitter = class {
23
11
  emit(value) {
24
12
  this._listeners.forEach((listener) => listener(value));
25
13
  }
26
- constructor() {
27
- _define_property(this, "_listeners", /* @__PURE__ */ new Set());
28
- }
29
14
  };
30
15
 
31
16
  // src/util/fraction.ts
32
17
  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
18
  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}`);
19
+ var FractionUtil = class _FractionUtil {
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}`);
34
+ }
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;
108
+ }
59
109
  }
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;
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
+ ];
120
+ }
103
121
  }
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
- ];
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);
115
151
  }
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
- });
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
+ };
209
+ };
181
210
 
182
211
  // src/util/points.ts
183
212
  var getCircumferencePoints = (p1, p2, r1 = 0, r2 = 0) => {
@@ -234,20 +263,12 @@ var offsetPoints = (points, distance, closed) => {
234
263
 
235
264
  // src/util/scale.ts
236
265
  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
266
  var Scale = class {
267
+ _gridSize;
268
+ _transform;
269
+ constructor(_gridSize = 32) {
270
+ this._gridSize = _gridSize;
271
+ }
251
272
  get gridSize() {
252
273
  return this._gridSize;
253
274
  }
@@ -276,289 +297,294 @@ var Scale = class {
276
297
  (point[1] - y) / k
277
298
  ];
278
299
  }
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]) => [
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([
348
309
  x,
349
310
  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
- }
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
+ ]);
342
+ return [
343
+ x,
344
+ y === 0 ? 0 : -y
345
+ ];
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) => {
401
+ const [x, y] = point;
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
+ };
402
424
  };
403
425
 
404
426
  // 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
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) {
438
+ return {
439
+ x: x - width,
440
+ y: y - height,
441
+ width: width * 2,
442
+ height: height * 2
443
+ };
444
+ } else {
445
+ return {
446
+ x,
447
+ y,
448
+ width,
449
+ height
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
+ };
419
500
  };
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
501
 
481
502
  // 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
503
  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
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
539
+ };
519
540
  };
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
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
554
+ };
530
555
  };
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
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
+ };
541
570
  };
542
- });
571
+ };
543
572
 
544
573
  // src/hooks/useSvgContext.ts
545
574
  import { createContext, useContext } from "react";
546
575
  import { createRef } from "react";
547
576
  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
577
  var SVGContext = class {
578
+ _ref = createRef();
579
+ _scale;
580
+ _centered;
581
+ _size;
582
+ _center;
583
+ resized = new EventEmitter();
584
+ constructor({ scale = new Scale(), centered = true }) {
585
+ this._scale = scale;
586
+ this._centered = centered;
587
+ }
562
588
  get ref() {
563
589
  return this._ref;
564
590
  }
@@ -608,16 +634,6 @@ var SVGContext = class {
608
634
  const [cx, cy] = this._center;
609
635
  return document.elementFromPoint(cx + x, cy + y);
610
636
  }
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
637
  };
622
638
  var SVGContextType = createContext(void 0);
623
639
  var SVGContextProvider = SVGContextType.Provider;
@@ -628,19 +644,6 @@ var useSvgContext = () => {
628
644
  // src/hooks/useGrid.ts
629
645
  import { line, range, select } from "d3";
630
646
  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
647
  var createLine = line();
645
648
  var createGrid = (context, options) => {
646
649
  if (!context.size) {
@@ -756,6 +759,16 @@ var defaultGridOptions = {
756
759
  grid: true
757
760
  };
758
761
  var GridController = class {
762
+ _ref;
763
+ _context;
764
+ _options;
765
+ _visible = false;
766
+ constructor(_ref, _context, _options) {
767
+ this._ref = _ref;
768
+ this._context = _context;
769
+ this._options = _options;
770
+ this._visible = this._options.visible ?? true;
771
+ }
759
772
  get ref() {
760
773
  return this._ref;
761
774
  }
@@ -780,17 +793,6 @@ var GridController = class {
780
793
  this.draw();
781
794
  return this;
782
795
  }
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
796
  };
795
797
  var useGrid = (options = defaultGridOptions) => {
796
798
  const ref = useRef(null);
@@ -807,7 +809,7 @@ var useGrid = (options = defaultGridOptions) => {
807
809
  import { useEffect as useEffect2, useRef as useRef2, useState } from "react";
808
810
  var useStateRef = (initialValue) => {
809
811
  const [value, setValue] = useState(initialValue);
810
- const ref = useRef2();
812
+ const ref = useRef2(null);
811
813
  useEffect2(() => {
812
814
  ref.current = value;
813
815
  }, [
@@ -853,19 +855,6 @@ var useTimestamp = (deps) => {
853
855
  import { select as select2, zoom, zoomIdentity as zoomIdentity2 } from "d3";
854
856
  import defaultsDeep from "lodash.defaultsdeep";
855
857
  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
858
  var defaultZoomOptions = {
870
859
  enabled: true,
871
860
  extent: [
@@ -875,6 +864,18 @@ var defaultZoomOptions = {
875
864
  onDblClick: (zoom2) => zoom2.reset()
876
865
  };
877
866
  var ZoomHandler = class {
867
+ _ref;
868
+ _context;
869
+ _zoom;
870
+ _options;
871
+ _enabled;
872
+ constructor(_ref, _context, options) {
873
+ this._ref = _ref;
874
+ this._context = _context;
875
+ this._options = defaultsDeep({}, options, defaultZoomOptions);
876
+ this._enabled = this._options.enabled ?? true;
877
+ this._zoom = zoom().scaleExtent(this._options.extent ?? defaultZoomOptions.extent);
878
+ }
878
879
  /**
879
880
  * Gets the reference which the transform is applied to.
880
881
  */
@@ -911,18 +912,6 @@ var ZoomHandler = class {
911
912
  select2(this._context.svg).transition().duration(duration).call(this._zoom.transform, transform);
912
913
  return this;
913
914
  }
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
915
  };
927
916
  var useZoom = (options = defaultZoomOptions) => {
928
917
  const context = useSvgContext();
@@ -963,4 +952,4 @@ export {
963
952
  ZoomHandler,
964
953
  useZoom
965
954
  };
966
- //# sourceMappingURL=chunk-PDEA4KBO.mjs.map
955
+ //# sourceMappingURL=chunk-XWA2XJDX.mjs.map