@dxos/react-ui-graph 0.8.4-main.a4bbb77 → 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.
- package/dist/lib/browser/{chunk-EX2Q43O6.mjs → chunk-XWA2XJDX.mjs} +486 -497
- package/dist/lib/{node-esm/chunk-SWA6OT3W.mjs.map → browser/chunk-XWA2XJDX.mjs.map} +2 -2
- package/dist/lib/browser/index.mjs +41 -108
- package/dist/lib/browser/index.mjs.map +2 -2
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +1 -1
- package/dist/lib/node-esm/{chunk-SWA6OT3W.mjs → chunk-3RLSFEEV.mjs} +486 -497
- package/dist/lib/{browser/chunk-EX2Q43O6.mjs.map → node-esm/chunk-3RLSFEEV.mjs.map} +2 -2
- package/dist/lib/node-esm/index.mjs +41 -108
- package/dist/lib/node-esm/index.mjs.map +2 -2
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +19 -19
- package/src/components/Graph/Graph.stories.tsx +3 -3
|
@@ -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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
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
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
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
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
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
|
-
]
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
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
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
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
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
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
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
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);
|
|
@@ -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-
|
|
957
|
+
//# sourceMappingURL=chunk-3RLSFEEV.mjs.map
|