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