@phun-ky/arclace 0.2.0 → 0.3.0
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/CHANGELOG.md +42 -0
- package/README.md +66 -45
- package/dist/arclace.d.ts +88 -43
- package/dist/arclace.js +497 -298
- package/dist/arclace.js.map +1 -1
- package/dist/arclace.umd.cjs +1 -1
- package/dist/arclace.umd.cjs.map +1 -1
- package/package.json +4 -1
- package/src/Arclace.ts +145 -55
- package/src/Connection.ts +98 -50
- package/src/errors.ts +25 -0
- package/src/index.ts +2 -2
- package/src/types.ts +6 -6
- package/src/utils/geometry.ts +16 -13
- package/src/utils/paths.ts +71 -36
- package/src/utils/resolve-element.ts +26 -9
- package/src/utils/sides.ts +8 -8
- package/src/utils/validate.ts +253 -0
package/src/errors.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thrown when Arclace can't do what was asked, for example when a selector
|
|
3
|
+
* matches nothing, an id is taken, or the instance has been destroyed.
|
|
4
|
+
*
|
|
5
|
+
* Arguments of the wrong type throw a `TypeError`, and values out of range a
|
|
6
|
+
* `RangeError`, instead. When another error is behind the failure, such as
|
|
7
|
+
* an invalid selector, it is the `cause`.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* try {
|
|
12
|
+
* arclace.connect('#a', '#b', { id: 'a-to-b' });
|
|
13
|
+
* arclace.connect('#b', '#c', { id: 'a-to-b' }); // the id is taken
|
|
14
|
+
* } catch (error) {
|
|
15
|
+
* if (error instanceof ArclaceError) console.warn(error.message);
|
|
16
|
+
* }
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export class ArclaceError extends Error {
|
|
20
|
+
override name = 'ArclaceError';
|
|
21
|
+
|
|
22
|
+
constructor(message: string, options?: ErrorOptions) {
|
|
23
|
+
super(`arclace: ${message}`, options);
|
|
24
|
+
}
|
|
25
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -3,11 +3,11 @@ export { Arclace, createConnections } from './Arclace';
|
|
|
3
3
|
export {
|
|
4
4
|
Connection,
|
|
5
5
|
DEFAULT_CONNECTION_OPTIONS,
|
|
6
|
-
type ArcConnectionParamsType,
|
|
7
|
-
type ArcOptionalKeysType,
|
|
8
6
|
type ArcResolvedConnectionOptionsType
|
|
9
7
|
} from './Connection';
|
|
10
8
|
|
|
9
|
+
export { ArclaceError } from './errors';
|
|
10
|
+
|
|
11
11
|
export type {
|
|
12
12
|
ArcArrowType,
|
|
13
13
|
ArcConnectionDefinitionType,
|
package/src/types.ts
CHANGED
|
@@ -62,9 +62,9 @@ export type ArcConnectionOptionsType = {
|
|
|
62
62
|
startSide?: ArcSideOptionType;
|
|
63
63
|
|
|
64
64
|
/**
|
|
65
|
-
* The side of the
|
|
65
|
+
* The side of the end element the line arrives at. Defaults to `'auto'`.
|
|
66
66
|
*/
|
|
67
|
-
|
|
67
|
+
endSide?: ArcSideOptionType;
|
|
68
68
|
|
|
69
69
|
/**
|
|
70
70
|
* Where to draw arrow heads. Defaults to `'none'`.
|
|
@@ -93,7 +93,7 @@ export type ArcConnectionOptionsType = {
|
|
|
93
93
|
dashed?: boolean;
|
|
94
94
|
|
|
95
95
|
/**
|
|
96
|
-
* Animate the dashes from start to
|
|
96
|
+
* Animate the dashes from start to end. Implies `dashed`. Requires the
|
|
97
97
|
* stylesheet. Defaults to `false`.
|
|
98
98
|
*/
|
|
99
99
|
animated?: boolean;
|
|
@@ -152,7 +152,7 @@ export type ArcConnectionOptionsType = {
|
|
|
152
152
|
*/
|
|
153
153
|
export type ArcConnectionDefinitionType = ArcConnectionOptionsType & {
|
|
154
154
|
startElement: ArcElementTargetType;
|
|
155
|
-
|
|
155
|
+
endElement: ArcElementTargetType;
|
|
156
156
|
};
|
|
157
157
|
|
|
158
158
|
/**
|
|
@@ -192,9 +192,9 @@ export type ArcOptionsType = {
|
|
|
192
192
|
*/
|
|
193
193
|
export type ArcGeometryType = {
|
|
194
194
|
start: ArcPointInterface;
|
|
195
|
-
|
|
195
|
+
end: ArcPointInterface;
|
|
196
196
|
startSide: ArcSideType;
|
|
197
|
-
|
|
197
|
+
endSide: ArcSideType;
|
|
198
198
|
labelPoint: ArcPointInterface;
|
|
199
199
|
d: string;
|
|
200
200
|
};
|
package/src/utils/geometry.ts
CHANGED
|
@@ -7,19 +7,22 @@ import type {
|
|
|
7
7
|
/**
|
|
8
8
|
* Unit vectors pointing away from each side of a box.
|
|
9
9
|
*/
|
|
10
|
-
export const NORMALS:
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
10
|
+
export const NORMALS: Readonly<
|
|
11
|
+
Record<ArcSideType, Readonly<ArcPointInterface>>
|
|
12
|
+
> = Object.freeze({
|
|
13
|
+
top: Object.freeze({ x: 0, y: -1 }),
|
|
14
|
+
right: Object.freeze({ x: 1, y: 0 }),
|
|
15
|
+
bottom: Object.freeze({ x: 0, y: 1 }),
|
|
16
|
+
left: Object.freeze({ x: -1, y: 0 })
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
export const OPPOSITE: Readonly<Record<ArcSideType, ArcSideType>> =
|
|
20
|
+
Object.freeze({
|
|
21
|
+
top: 'bottom',
|
|
22
|
+
right: 'left',
|
|
23
|
+
bottom: 'top',
|
|
24
|
+
left: 'right'
|
|
25
|
+
});
|
|
23
26
|
|
|
24
27
|
export const isHorizontalSide = (side: ArcSideType): boolean =>
|
|
25
28
|
side === 'left' || side === 'right';
|
package/src/utils/paths.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
NORMALS,
|
|
9
9
|
OPPOSITE
|
|
10
10
|
} from './geometry';
|
|
11
|
+
import { assertNumber, assertOneOf, assertPoint, SIDES } from './validate';
|
|
11
12
|
|
|
12
13
|
export type ArcPathResultType = {
|
|
13
14
|
/**
|
|
@@ -61,9 +62,26 @@ export const curveMultiplierFromDistance = (
|
|
|
61
62
|
maxDistance: number = 1000
|
|
62
63
|
): number => lerp(normalizeDistance(distance, maxDistance), min, max);
|
|
63
64
|
|
|
65
|
+
/**
|
|
66
|
+
* Checks the ends of a path, so a bad side or point fails with a clear error
|
|
67
|
+
* instead of path data with `NaN` in it.
|
|
68
|
+
*/
|
|
69
|
+
const checkEnds = (
|
|
70
|
+
start: unknown,
|
|
71
|
+
startSide: unknown,
|
|
72
|
+
end: unknown,
|
|
73
|
+
endSide: unknown
|
|
74
|
+
): void => {
|
|
75
|
+
assertPoint(start, 'start');
|
|
76
|
+
assertOneOf(startSide, SIDES, 'startSide');
|
|
77
|
+
assertPoint(end, 'end');
|
|
78
|
+
assertOneOf(endSide, SIDES, 'endSide');
|
|
79
|
+
};
|
|
80
|
+
|
|
64
81
|
/**
|
|
65
82
|
* A straight line between two points.
|
|
66
83
|
*
|
|
84
|
+
* @throws {TypeError} When a point doesn't have finite `x` and `y`.
|
|
67
85
|
* @example
|
|
68
86
|
* ```ts
|
|
69
87
|
* straightPath({ x: 0, y: 0 }, { x: 100, y: 0 }).d; // 'M 0 0 L 100 0'
|
|
@@ -71,16 +89,23 @@ export const curveMultiplierFromDistance = (
|
|
|
71
89
|
*/
|
|
72
90
|
export const straightPath = (
|
|
73
91
|
start: ArcPointInterface,
|
|
74
|
-
|
|
75
|
-
): ArcPathResultType =>
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
92
|
+
end: ArcPointInterface
|
|
93
|
+
): ArcPathResultType => {
|
|
94
|
+
assertPoint(start, 'start');
|
|
95
|
+
assertPoint(end, 'end');
|
|
96
|
+
|
|
97
|
+
return {
|
|
98
|
+
d: `M ${fmt(start)} L ${fmt(end)}`,
|
|
99
|
+
labelPoint: { x: (start.x + end.x) / 2, y: (start.y + end.y) / 2 }
|
|
100
|
+
};
|
|
101
|
+
};
|
|
79
102
|
|
|
80
103
|
/**
|
|
81
|
-
* A cubic bezier curve that leaves `start` and arrives at `
|
|
104
|
+
* A cubic bezier curve that leaves `start` and arrives at `end`
|
|
82
105
|
* perpendicular to their sides.
|
|
83
106
|
*
|
|
107
|
+
* @throws {TypeError} When a point or `curvature` isn't finite.
|
|
108
|
+
* @throws {RangeError} When a side isn't `top`, `right`, `bottom` or `left`.
|
|
84
109
|
* @example
|
|
85
110
|
* ```ts
|
|
86
111
|
* bezierPath({ x: 0, y: 0 }, 'right', { x: 200, y: 100 }, 'left').d;
|
|
@@ -89,21 +114,24 @@ export const straightPath = (
|
|
|
89
114
|
export const bezierPath = (
|
|
90
115
|
start: ArcPointInterface,
|
|
91
116
|
startSide: ArcSideType,
|
|
92
|
-
|
|
93
|
-
|
|
117
|
+
end: ArcPointInterface,
|
|
118
|
+
endSide: ArcSideType,
|
|
94
119
|
curvature = 1
|
|
95
120
|
): ArcPathResultType => {
|
|
96
|
-
|
|
121
|
+
checkEnds(start, startSide, end, endSide);
|
|
122
|
+
assertNumber(curvature, 'curvature');
|
|
123
|
+
|
|
124
|
+
const length = distance(start, end);
|
|
97
125
|
const bulge = (length / curveMultiplierFromDistance(length)) * curvature;
|
|
98
126
|
const c1 = add(start, NORMALS[startSide], bulge);
|
|
99
|
-
const c2 = add(
|
|
127
|
+
const c2 = add(end, NORMALS[endSide], bulge);
|
|
100
128
|
|
|
101
129
|
return {
|
|
102
|
-
d: `M ${fmt(start)} C ${fmt(c1)}, ${fmt(c2)}, ${fmt(
|
|
130
|
+
d: `M ${fmt(start)} C ${fmt(c1)}, ${fmt(c2)}, ${fmt(end)}`,
|
|
103
131
|
// Point on the curve at t = 0.5
|
|
104
132
|
labelPoint: {
|
|
105
|
-
x: (start.x + 3 * c1.x + 3 * c2.x +
|
|
106
|
-
y: (start.y + 3 * c1.y + 3 * c2.y +
|
|
133
|
+
x: (start.x + 3 * c1.x + 3 * c2.x + end.x) / 8,
|
|
134
|
+
y: (start.y + 3 * c1.y + 3 * c2.y + end.y) / 8
|
|
107
135
|
}
|
|
108
136
|
};
|
|
109
137
|
};
|
|
@@ -209,81 +237,84 @@ export const polylineMidpoint = (
|
|
|
209
237
|
|
|
210
238
|
// Swapping x and y turns vertical sides into horizontal ones, so only the
|
|
211
239
|
// horizontal case has to be routed by hand.
|
|
212
|
-
const SWAPPED_SIDE: Record<ArcSideType, ArcSideType
|
|
240
|
+
const SWAPPED_SIDE: Readonly<Record<ArcSideType, ArcSideType>> = Object.freeze({
|
|
213
241
|
top: 'left',
|
|
214
242
|
bottom: 'right',
|
|
215
243
|
left: 'top',
|
|
216
244
|
right: 'bottom'
|
|
217
|
-
};
|
|
245
|
+
});
|
|
218
246
|
const swap = (point: ArcPointInterface): ArcPointInterface => ({
|
|
219
247
|
x: point.y,
|
|
220
248
|
y: point.x
|
|
221
249
|
});
|
|
222
250
|
|
|
223
251
|
/**
|
|
224
|
-
* Routes a line from `start` to `
|
|
252
|
+
* Routes a line from `start` to `end` using only horizontal and vertical
|
|
225
253
|
* segments. The line leaves and arrives perpendicular to the given sides,
|
|
226
254
|
* travelling at least `offset` pixels straight out before turning back.
|
|
227
255
|
*
|
|
228
|
-
* @returns The corner points of the route, including `start` and `
|
|
256
|
+
* @returns The corner points of the route, including `start` and `end`.
|
|
229
257
|
*/
|
|
230
258
|
export const routeOrthogonal = (
|
|
231
259
|
start: ArcPointInterface,
|
|
232
260
|
startSide: ArcSideType,
|
|
233
|
-
|
|
234
|
-
|
|
261
|
+
end: ArcPointInterface,
|
|
262
|
+
endSide: ArcSideType,
|
|
235
263
|
offset = 20
|
|
236
264
|
): ArcPointInterface[] => {
|
|
237
265
|
if (!isHorizontalSide(startSide)) {
|
|
238
266
|
return routeOrthogonal(
|
|
239
267
|
swap(start),
|
|
240
268
|
SWAPPED_SIDE[startSide],
|
|
241
|
-
swap(
|
|
242
|
-
SWAPPED_SIDE[
|
|
269
|
+
swap(end),
|
|
270
|
+
SWAPPED_SIDE[endSide],
|
|
243
271
|
offset
|
|
244
272
|
).map(swap);
|
|
245
273
|
}
|
|
246
274
|
|
|
247
275
|
const n1 = NORMALS[startSide];
|
|
248
|
-
const n2 = NORMALS[
|
|
276
|
+
const n2 = NORMALS[endSide];
|
|
249
277
|
const s1 = add(start, n1, offset);
|
|
250
|
-
const s2 = add(
|
|
278
|
+
const s2 = add(end, n2, offset);
|
|
251
279
|
|
|
252
|
-
if (isHorizontalSide(
|
|
253
|
-
if (
|
|
280
|
+
if (isHorizontalSide(endSide)) {
|
|
281
|
+
if (endSide === OPPOSITE[startSide]) {
|
|
254
282
|
// Facing each other with room in between: turn halfway
|
|
255
283
|
if ((s2.x - s1.x) * n1.x >= 0) {
|
|
256
|
-
const midX = (start.x +
|
|
284
|
+
const midX = (start.x + end.x) / 2;
|
|
257
285
|
|
|
258
|
-
return [start, { x: midX, y: start.y }, { x: midX, y:
|
|
286
|
+
return [start, { x: midX, y: start.y }, { x: midX, y: end.y }, end];
|
|
259
287
|
}
|
|
260
288
|
|
|
261
289
|
// Facing away from each other: step out, cross over halfway, step in
|
|
262
290
|
const midY = (s1.y + s2.y) / 2;
|
|
263
291
|
|
|
264
|
-
return [start, s1, { x: s1.x, y: midY }, { x: s2.x, y: midY }, s2,
|
|
292
|
+
return [start, s1, { x: s1.x, y: midY }, { x: s2.x, y: midY }, s2, end];
|
|
265
293
|
}
|
|
266
294
|
|
|
267
295
|
// Same side: go around the outside of both
|
|
268
296
|
const x = n1.x > 0 ? Math.max(s1.x, s2.x) : Math.min(s1.x, s2.x);
|
|
269
297
|
|
|
270
|
-
return [start, { x, y: start.y }, { x, y:
|
|
298
|
+
return [start, { x, y: start.y }, { x, y: end.y }, end];
|
|
271
299
|
}
|
|
272
300
|
|
|
273
301
|
// One horizontal and one vertical side: a single corner if it lies in
|
|
274
302
|
// front of both sides, otherwise step out of both and meet
|
|
275
|
-
const corner = { x:
|
|
303
|
+
const corner = { x: end.x, y: start.y };
|
|
276
304
|
|
|
277
|
-
if ((corner.x - start.x) * n1.x > 0 && (corner.y -
|
|
278
|
-
return [start, corner,
|
|
305
|
+
if ((corner.x - start.x) * n1.x > 0 && (corner.y - end.y) * n2.y > 0) {
|
|
306
|
+
return [start, corner, end];
|
|
279
307
|
}
|
|
280
308
|
|
|
281
|
-
return [start, s1, { x: s1.x, y: s2.y }, s2,
|
|
309
|
+
return [start, s1, { x: s1.x, y: s2.y }, s2, end];
|
|
282
310
|
};
|
|
283
311
|
|
|
284
312
|
/**
|
|
285
313
|
* An orthogonal line with rounded corners.
|
|
286
314
|
*
|
|
315
|
+
* @throws {TypeError} When a point, `radius` or `offset` isn't finite.
|
|
316
|
+
* @throws {RangeError} When a side isn't `top`, `right`, `bottom` or `left`,
|
|
317
|
+
* or `radius` or `offset` is negative.
|
|
287
318
|
* @example
|
|
288
319
|
* ```ts
|
|
289
320
|
* orthogonalPath({ x: 0, y: 0 }, 'right', { x: 200, y: 100 }, 'left', {
|
|
@@ -295,12 +326,16 @@ export const routeOrthogonal = (
|
|
|
295
326
|
export const orthogonalPath = (
|
|
296
327
|
start: ArcPointInterface,
|
|
297
328
|
startSide: ArcSideType,
|
|
298
|
-
|
|
299
|
-
|
|
329
|
+
end: ArcPointInterface,
|
|
330
|
+
endSide: ArcSideType,
|
|
300
331
|
{ radius = 8, offset = 20 }: { radius?: number; offset?: number } = {}
|
|
301
332
|
): ArcPathResultType => {
|
|
333
|
+
checkEnds(start, startSide, end, endSide);
|
|
334
|
+
assertNumber(radius, 'radius', 0);
|
|
335
|
+
assertNumber(offset, 'offset', 0);
|
|
336
|
+
|
|
302
337
|
const points = simplifyPolyline(
|
|
303
|
-
routeOrthogonal(start, startSide,
|
|
338
|
+
routeOrthogonal(start, startSide, end, endSide, offset)
|
|
304
339
|
);
|
|
305
340
|
|
|
306
341
|
return {
|
|
@@ -1,28 +1,45 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { ArclaceError } from '../errors';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Runs `document.querySelector`, turning an invalid selector into an
|
|
5
|
+
* {@link ArclaceError} with the browser's `SyntaxError` as the cause.
|
|
6
|
+
*/
|
|
7
|
+
export const querySelector = (
|
|
8
|
+
selector: string,
|
|
9
|
+
name = 'element'
|
|
10
|
+
): Element | null => {
|
|
11
|
+
try {
|
|
12
|
+
return document.querySelector(selector);
|
|
13
|
+
} catch (error) {
|
|
14
|
+
throw new ArclaceError(`${name} "${selector}" is not a valid selector`, {
|
|
15
|
+
cause: error
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
};
|
|
2
19
|
|
|
3
20
|
/**
|
|
4
21
|
* Resolves an element or a CSS selector to an element.
|
|
5
22
|
*
|
|
6
|
-
* @throws {
|
|
23
|
+
* @throws {TypeError} When the target is neither an element nor a string.
|
|
24
|
+
* @throws {ArclaceError} When the selector is invalid or matches nothing.
|
|
7
25
|
* @example
|
|
8
26
|
* ```ts
|
|
9
27
|
* const el = resolveElement('#box-1');
|
|
10
28
|
* ```
|
|
11
29
|
*/
|
|
12
|
-
export const resolveElement = (
|
|
13
|
-
target: ArcElementTargetType,
|
|
14
|
-
name = 'element'
|
|
15
|
-
): Element => {
|
|
30
|
+
export const resolveElement = (target: unknown, name = 'element'): Element => {
|
|
16
31
|
if (typeof target === 'string') {
|
|
17
|
-
const el =
|
|
32
|
+
const el = querySelector(target, name);
|
|
18
33
|
|
|
19
|
-
if (!el) throw
|
|
34
|
+
if (!el) throw new ArclaceError(`no ${name} matches "${target}"`);
|
|
20
35
|
|
|
21
36
|
return el;
|
|
22
37
|
}
|
|
23
38
|
|
|
24
39
|
if (!(target instanceof Element)) {
|
|
25
|
-
throw
|
|
40
|
+
throw new TypeError(
|
|
41
|
+
`arclace: ${name} must be an Element or a selector string`
|
|
42
|
+
);
|
|
26
43
|
}
|
|
27
44
|
|
|
28
45
|
return target;
|
package/src/utils/sides.ts
CHANGED
|
@@ -44,19 +44,19 @@ export const facingSide = (
|
|
|
44
44
|
*
|
|
45
45
|
* @example
|
|
46
46
|
* ```ts
|
|
47
|
-
* resolveSides(startRect,
|
|
48
|
-
* // { startSide: 'right',
|
|
47
|
+
* resolveSides(startRect, endRect, 'auto', 'auto');
|
|
48
|
+
* // { startSide: 'right', endSide: 'left' }
|
|
49
49
|
* ```
|
|
50
50
|
*/
|
|
51
51
|
export const resolveSides = (
|
|
52
52
|
startRect: ArcRectInterface,
|
|
53
|
-
|
|
53
|
+
endRect: ArcRectInterface,
|
|
54
54
|
startSide: ArcSideOptionType = 'auto',
|
|
55
|
-
|
|
56
|
-
): { startSide: ArcSideType;
|
|
57
|
-
startSide: startSide === 'auto' ? facingSide(startRect,
|
|
58
|
-
|
|
59
|
-
|
|
55
|
+
endSide: ArcSideOptionType = 'auto'
|
|
56
|
+
): { startSide: ArcSideType; endSide: ArcSideType } => ({
|
|
57
|
+
startSide: startSide === 'auto' ? facingSide(startRect, endRect) : startSide,
|
|
58
|
+
endSide:
|
|
59
|
+
endSide === 'auto' ? OPPOSITE[facingSide(startRect, endRect)] : endSide
|
|
60
60
|
});
|
|
61
61
|
|
|
62
62
|
/**
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ArcArrowType,
|
|
3
|
+
ArcConnectionOptionsType,
|
|
4
|
+
ArcOptionsType,
|
|
5
|
+
ArcPathType,
|
|
6
|
+
ArcPointInterface,
|
|
7
|
+
ArcSideOptionType,
|
|
8
|
+
ArcSideType
|
|
9
|
+
} from '../types';
|
|
10
|
+
|
|
11
|
+
export const SIDES: readonly ArcSideType[] = Object.freeze([
|
|
12
|
+
'top',
|
|
13
|
+
'right',
|
|
14
|
+
'bottom',
|
|
15
|
+
'left'
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
export const SIDE_OPTIONS: readonly ArcSideOptionType[] = Object.freeze([
|
|
19
|
+
...SIDES,
|
|
20
|
+
'auto'
|
|
21
|
+
]);
|
|
22
|
+
|
|
23
|
+
export const PATH_TYPES: readonly ArcPathType[] = Object.freeze([
|
|
24
|
+
'bezier',
|
|
25
|
+
'straight',
|
|
26
|
+
'orthogonal'
|
|
27
|
+
]);
|
|
28
|
+
|
|
29
|
+
export const ARROWS: readonly ArcArrowType[] = Object.freeze([
|
|
30
|
+
'none',
|
|
31
|
+
'start',
|
|
32
|
+
'end',
|
|
33
|
+
'both'
|
|
34
|
+
]);
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* A short description of a value for an error message, which never calls
|
|
38
|
+
* the value's own `toString`.
|
|
39
|
+
*/
|
|
40
|
+
const show = (value: unknown): string => {
|
|
41
|
+
if (typeof value === 'string') return JSON.stringify(value);
|
|
42
|
+
|
|
43
|
+
if (
|
|
44
|
+
value === null ||
|
|
45
|
+
value === undefined ||
|
|
46
|
+
typeof value === 'number' ||
|
|
47
|
+
typeof value === 'boolean'
|
|
48
|
+
)
|
|
49
|
+
return String(value);
|
|
50
|
+
|
|
51
|
+
if (Array.isArray(value)) return 'an array';
|
|
52
|
+
|
|
53
|
+
return typeof value === 'object' ? 'an object' : `a ${typeof value}`;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Throws a `TypeError` unless `value` is a plain object (not `null` or an
|
|
58
|
+
* array).
|
|
59
|
+
*/
|
|
60
|
+
export const assertObject = (value: unknown, name: string): void => {
|
|
61
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
62
|
+
throw new TypeError(
|
|
63
|
+
`arclace: ${name} must be an object, not ${show(value)}`
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Throws a `TypeError` unless `value` is a string, and a `RangeError` unless
|
|
70
|
+
* it is one of `allowed`.
|
|
71
|
+
*/
|
|
72
|
+
export const assertOneOf = <T extends string>(
|
|
73
|
+
value: unknown,
|
|
74
|
+
allowed: readonly T[],
|
|
75
|
+
name: string
|
|
76
|
+
): T => {
|
|
77
|
+
const list = allowed.map((item) => `"${item}"`).join(', ');
|
|
78
|
+
|
|
79
|
+
if (typeof value !== 'string') {
|
|
80
|
+
throw new TypeError(
|
|
81
|
+
`arclace: ${name} must be one of ${list}, not ${show(value)}`
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (!allowed.includes(value as T)) {
|
|
86
|
+
throw new RangeError(
|
|
87
|
+
`arclace: ${name} must be one of ${list}, not ${show(value)}`
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return value as T;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Throws a `TypeError` unless `value` is a finite number, and a `RangeError`
|
|
96
|
+
* when it is below `min`.
|
|
97
|
+
*/
|
|
98
|
+
export const assertNumber = (
|
|
99
|
+
value: unknown,
|
|
100
|
+
name: string,
|
|
101
|
+
min?: number
|
|
102
|
+
): number => {
|
|
103
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
104
|
+
throw new TypeError(
|
|
105
|
+
`arclace: ${name} must be a finite number, not ${show(value)}`
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (min !== undefined && value < min) {
|
|
110
|
+
throw new RangeError(
|
|
111
|
+
`arclace: ${name} must be ${min} or more, not ${show(value)}`
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return value;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Throws a `TypeError` unless `value` is a boolean.
|
|
120
|
+
*/
|
|
121
|
+
export const assertBoolean = (value: unknown, name: string): boolean => {
|
|
122
|
+
if (typeof value !== 'boolean') {
|
|
123
|
+
throw new TypeError(
|
|
124
|
+
`arclace: ${name} must be true or false, not ${show(value)}`
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return value;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Throws a `TypeError` unless `value` is a string, or, with `nonEmpty`, a
|
|
133
|
+
* string with at least one character.
|
|
134
|
+
*/
|
|
135
|
+
export const assertString = (
|
|
136
|
+
value: unknown,
|
|
137
|
+
name: string,
|
|
138
|
+
nonEmpty = false
|
|
139
|
+
): string => {
|
|
140
|
+
if (typeof value !== 'string' || (nonEmpty && value === '')) {
|
|
141
|
+
throw new TypeError(
|
|
142
|
+
`arclace: ${name} must be a ${nonEmpty ? 'non-empty ' : ''}string, not ${show(value)}`
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return value;
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Throws a `TypeError` unless `value` is a point with finite `x` and `y`.
|
|
151
|
+
*/
|
|
152
|
+
export const assertPoint = (
|
|
153
|
+
value: unknown,
|
|
154
|
+
name: string
|
|
155
|
+
): ArcPointInterface => {
|
|
156
|
+
assertObject(value, name);
|
|
157
|
+
|
|
158
|
+
const { x, y } = value as Record<string, unknown>;
|
|
159
|
+
|
|
160
|
+
assertNumber(x, `${name}.x`);
|
|
161
|
+
assertNumber(y, `${name}.y`);
|
|
162
|
+
|
|
163
|
+
return value as ArcPointInterface;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
// How each connection option is checked. Keys that aren't listed are ignored.
|
|
167
|
+
const CONNECTION_OPTION_CHECKS: Readonly<
|
|
168
|
+
Record<
|
|
169
|
+
keyof ArcConnectionOptionsType,
|
|
170
|
+
(value: unknown, name: string) => unknown
|
|
171
|
+
>
|
|
172
|
+
> = Object.freeze({
|
|
173
|
+
id: (value, name) => assertString(value, name, true),
|
|
174
|
+
type: (value, name) => assertOneOf(value, PATH_TYPES, name),
|
|
175
|
+
startSide: (value, name) => assertOneOf(value, SIDE_OPTIONS, name),
|
|
176
|
+
endSide: (value, name) => assertOneOf(value, SIDE_OPTIONS, name),
|
|
177
|
+
arrow: (value, name) => assertOneOf(value, ARROWS, name),
|
|
178
|
+
label: assertString,
|
|
179
|
+
color: assertString,
|
|
180
|
+
className: assertString,
|
|
181
|
+
strokeWidth: (value, name) => assertNumber(value, name, 0),
|
|
182
|
+
dashed: assertBoolean,
|
|
183
|
+
animated: assertBoolean,
|
|
184
|
+
anchors: assertBoolean,
|
|
185
|
+
gap: (value, name) => assertNumber(value, name),
|
|
186
|
+
spread: assertBoolean,
|
|
187
|
+
spacing: (value, name) => assertNumber(value, name, 0),
|
|
188
|
+
curvature: (value, name) => assertNumber(value, name),
|
|
189
|
+
radius: (value, name) => assertNumber(value, name, 0),
|
|
190
|
+
offset: (value, name) => assertNumber(value, name, 0)
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Checks every known connection option that is set. `undefined` means "use
|
|
195
|
+
* the default" and is always allowed; unknown keys are ignored.
|
|
196
|
+
*
|
|
197
|
+
* @throws {TypeError} When `options` isn't an object, or an option has the
|
|
198
|
+
* wrong type.
|
|
199
|
+
* @throws {RangeError} When an option is out of range.
|
|
200
|
+
*/
|
|
201
|
+
export const validateConnectionOptions = (
|
|
202
|
+
options: unknown,
|
|
203
|
+
name = 'options'
|
|
204
|
+
): ArcConnectionOptionsType => {
|
|
205
|
+
assertObject(options, name);
|
|
206
|
+
|
|
207
|
+
for (const [key, check] of Object.entries(CONNECTION_OPTION_CHECKS)) {
|
|
208
|
+
const value = Object.hasOwn(options as object, key)
|
|
209
|
+
? (options as Record<string, unknown>)[key]
|
|
210
|
+
: undefined;
|
|
211
|
+
|
|
212
|
+
if (value !== undefined) check(value, `${name}.${key}`);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return options as ArcConnectionOptionsType;
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Checks the options of an instance, including its connection defaults.
|
|
220
|
+
*/
|
|
221
|
+
export const validateOptions = (options: unknown): ArcOptionsType => {
|
|
222
|
+
assertObject(options, 'options');
|
|
223
|
+
|
|
224
|
+
const { autoUpdate, className, defaults } = options as Record<
|
|
225
|
+
string,
|
|
226
|
+
unknown
|
|
227
|
+
>;
|
|
228
|
+
|
|
229
|
+
if (
|
|
230
|
+
autoUpdate !== undefined &&
|
|
231
|
+
typeof autoUpdate !== 'boolean' &&
|
|
232
|
+
autoUpdate !== 'frame'
|
|
233
|
+
) {
|
|
234
|
+
throw new RangeError(
|
|
235
|
+
`arclace: options.autoUpdate must be true, false or "frame", not ${show(autoUpdate)}`
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (className !== undefined) assertString(className, 'options.className');
|
|
240
|
+
|
|
241
|
+
if (defaults !== undefined) {
|
|
242
|
+
validateConnectionOptions(defaults, 'options.defaults');
|
|
243
|
+
|
|
244
|
+
// Every connection would get the same id, so only the first could be made
|
|
245
|
+
if ((defaults as ArcConnectionOptionsType).id !== undefined) {
|
|
246
|
+
throw new TypeError(
|
|
247
|
+
'arclace: options.defaults cannot set an id; ids belong to single connections'
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
return options as ArcOptionsType;
|
|
253
|
+
};
|