@dxos/react-ui-graph 0.8.4-main.e098934 → 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,20 +1,8 @@
1
1
  import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
2
 
3
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
4
  var EventEmitter = class {
5
+ _listeners = /* @__PURE__ */ new Set();
18
6
  clear() {
19
7
  this._listeners.clear();
20
8
  }
@@ -25,161 +13,202 @@ var EventEmitter = class {
25
13
  emit(value) {
26
14
  this._listeners.forEach((listener) => listener(value));
27
15
  }
28
- constructor() {
29
- _define_property(this, "_listeners", /* @__PURE__ */ new Set());
30
- }
31
16
  };
32
17
 
33
18
  // src/util/fraction.ts
34
19
  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
20
  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}`);
21
+ var FractionUtil = class _FractionUtil {
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}`);
36
+ }
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;
110
+ }
61
111
  }
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;
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
+ ];
122
+ }
105
123
  }
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
- ];
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);
117
153
  }
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
- });
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
+ };
211
+ };
183
212
 
184
213
  // src/util/points.ts
185
214
  var getCircumferencePoints = (p1, p2, r1 = 0, r2 = 0) => {
@@ -236,20 +265,12 @@ var offsetPoints = (points, distance, closed) => {
236
265
 
237
266
  // src/util/scale.ts
238
267
  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
268
  var Scale = class {
269
+ _gridSize;
270
+ _transform;
271
+ constructor(_gridSize = 32) {
272
+ this._gridSize = _gridSize;
273
+ }
253
274
  get gridSize() {
254
275
  return this._gridSize;
255
276
  }
@@ -278,289 +299,294 @@ var Scale = class {
278
299
  (point[1] - y) / k
279
300
  ];
280
301
  }
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]) => [
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([
350
311
  x,
351
312
  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
- }
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
+ ]);
344
+ return [
345
+ x,
346
+ y === 0 ? 0 : -y
347
+ ];
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) => {
403
+ const [x, y] = point;
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
+ };
404
426
  };
405
427
 
406
428
  // 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
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) {
440
+ return {
441
+ x: x - width,
442
+ y: y - height,
443
+ width: width * 2,
444
+ height: height * 2
445
+ };
446
+ } else {
447
+ return {
448
+ x,
449
+ y,
450
+ width,
451
+ height
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
+ };
421
502
  };
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
503
 
483
504
  // 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
505
  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
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
541
+ };
521
542
  };
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
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
556
+ };
532
557
  };
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
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
+ };
543
572
  };
544
- });
573
+ };
545
574
 
546
575
  // src/hooks/useSvgContext.ts
547
576
  import { createContext, useContext } from "react";
548
577
  import { createRef } from "react";
549
578
  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
579
  var SVGContext = class {
580
+ _ref = createRef();
581
+ _scale;
582
+ _centered;
583
+ _size;
584
+ _center;
585
+ resized = new EventEmitter();
586
+ constructor({ scale = new Scale(), centered = true }) {
587
+ this._scale = scale;
588
+ this._centered = centered;
589
+ }
564
590
  get ref() {
565
591
  return this._ref;
566
592
  }
@@ -610,16 +636,6 @@ var SVGContext = class {
610
636
  const [cx, cy] = this._center;
611
637
  return document.elementFromPoint(cx + x, cy + y);
612
638
  }
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
639
  };
624
640
  var SVGContextType = createContext(void 0);
625
641
  var SVGContextProvider = SVGContextType.Provider;
@@ -630,19 +646,6 @@ var useSvgContext = () => {
630
646
  // src/hooks/useGrid.ts
631
647
  import { line, range, select } from "d3";
632
648
  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
649
  var createLine = line();
647
650
  var createGrid = (context, options) => {
648
651
  if (!context.size) {
@@ -758,6 +761,16 @@ var defaultGridOptions = {
758
761
  grid: true
759
762
  };
760
763
  var GridController = class {
764
+ _ref;
765
+ _context;
766
+ _options;
767
+ _visible = false;
768
+ constructor(_ref, _context, _options) {
769
+ this._ref = _ref;
770
+ this._context = _context;
771
+ this._options = _options;
772
+ this._visible = this._options.visible ?? true;
773
+ }
761
774
  get ref() {
762
775
  return this._ref;
763
776
  }
@@ -782,17 +795,6 @@ var GridController = class {
782
795
  this.draw();
783
796
  return this;
784
797
  }
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
798
  };
797
799
  var useGrid = (options = defaultGridOptions) => {
798
800
  const ref = useRef(null);
@@ -809,7 +811,7 @@ var useGrid = (options = defaultGridOptions) => {
809
811
  import { useEffect as useEffect2, useRef as useRef2, useState } from "react";
810
812
  var useStateRef = (initialValue) => {
811
813
  const [value, setValue] = useState(initialValue);
812
- const ref = useRef2();
814
+ const ref = useRef2(null);
813
815
  useEffect2(() => {
814
816
  ref.current = value;
815
817
  }, [
@@ -855,19 +857,6 @@ var useTimestamp = (deps) => {
855
857
  import { select as select2, zoom, zoomIdentity as zoomIdentity2 } from "d3";
856
858
  import defaultsDeep from "lodash.defaultsdeep";
857
859
  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
860
  var defaultZoomOptions = {
872
861
  enabled: true,
873
862
  extent: [
@@ -877,6 +866,18 @@ var defaultZoomOptions = {
877
866
  onDblClick: (zoom2) => zoom2.reset()
878
867
  };
879
868
  var ZoomHandler = class {
869
+ _ref;
870
+ _context;
871
+ _zoom;
872
+ _options;
873
+ _enabled;
874
+ constructor(_ref, _context, options) {
875
+ this._ref = _ref;
876
+ this._context = _context;
877
+ this._options = defaultsDeep({}, options, defaultZoomOptions);
878
+ this._enabled = this._options.enabled ?? true;
879
+ this._zoom = zoom().scaleExtent(this._options.extent ?? defaultZoomOptions.extent);
880
+ }
880
881
  /**
881
882
  * Gets the reference which the transform is applied to.
882
883
  */
@@ -913,18 +914,6 @@ var ZoomHandler = class {
913
914
  select2(this._context.svg).transition().duration(duration).call(this._zoom.transform, transform);
914
915
  return this;
915
916
  }
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
917
  };
929
918
  var useZoom = (options = defaultZoomOptions) => {
930
919
  const context = useSvgContext();
@@ -965,4 +954,4 @@ export {
965
954
  ZoomHandler,
966
955
  useZoom
967
956
  };
968
- //# sourceMappingURL=chunk-NJGJKPL7.mjs.map
957
+ //# sourceMappingURL=chunk-3RLSFEEV.mjs.map