@phun-ky/arclace 0.0.0-stage → 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 +86 -0
- package/LICENSE +21 -0
- package/README.md +336 -2
- package/dist/arclace.d.ts +456 -0
- package/dist/arclace.js +839 -0
- package/dist/arclace.js.map +1 -0
- package/dist/arclace.umd.cjs +2 -0
- package/dist/arclace.umd.cjs.map +1 -0
- package/dist/style.css +52 -0
- package/package.json +119 -4
- package/src/Arclace.ts +578 -0
- package/src/Connection.ts +455 -0
- package/src/errors.ts +25 -0
- package/src/index.ts +30 -0
- package/src/style.css +52 -0
- package/src/types.ts +200 -0
- package/src/utils/geometry.ts +61 -0
- package/src/utils/id.ts +14 -0
- package/src/utils/measure.ts +64 -0
- package/src/utils/paths.ts +345 -0
- package/src/utils/resolve-element.ts +46 -0
- package/src/utils/sides.ts +108 -0
- package/src/utils/svg.ts +34 -0
- package/src/utils/validate.ts +253 -0
|
@@ -0,0 +1,46 @@
|
|
|
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
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Resolves an element or a CSS selector to an element.
|
|
22
|
+
*
|
|
23
|
+
* @throws {TypeError} When the target is neither an element nor a string.
|
|
24
|
+
* @throws {ArclaceError} When the selector is invalid or matches nothing.
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* const el = resolveElement('#box-1');
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export const resolveElement = (target: unknown, name = 'element'): Element => {
|
|
31
|
+
if (typeof target === 'string') {
|
|
32
|
+
const el = querySelector(target, name);
|
|
33
|
+
|
|
34
|
+
if (!el) throw new ArclaceError(`no ${name} matches "${target}"`);
|
|
35
|
+
|
|
36
|
+
return el;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (!(target instanceof Element)) {
|
|
40
|
+
throw new TypeError(
|
|
41
|
+
`arclace: ${name} must be an Element or a selector string`
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return target;
|
|
46
|
+
};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ArcPointInterface,
|
|
3
|
+
ArcRectInterface,
|
|
4
|
+
ArcSideOptionType,
|
|
5
|
+
ArcSideType
|
|
6
|
+
} from '../types';
|
|
7
|
+
|
|
8
|
+
import { add, center, NORMALS, OPPOSITE } from './geometry';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Picks the side of `from` that faces `to`.
|
|
12
|
+
*
|
|
13
|
+
* Boxes that are further apart horizontally than vertically connect
|
|
14
|
+
* left/right, otherwise top/bottom.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* facingSide(
|
|
19
|
+
* { left: 0, top: 0, right: 100, bottom: 50, width: 100, height: 50 },
|
|
20
|
+
* { left: 300, top: 0, right: 400, bottom: 50, width: 100, height: 50 }
|
|
21
|
+
* ); // 'right'
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export const facingSide = (
|
|
25
|
+
from: ArcRectInterface,
|
|
26
|
+
to: ArcRectInterface
|
|
27
|
+
): ArcSideType => {
|
|
28
|
+
const a = center(from);
|
|
29
|
+
const b = center(to);
|
|
30
|
+
const dx = b.x - a.x;
|
|
31
|
+
const dy = b.y - a.y;
|
|
32
|
+
// Positive when the boxes are separated on that axis, negative when they overlap
|
|
33
|
+
const gapX = Math.max(to.left - from.right, from.left - to.right);
|
|
34
|
+
const gapY = Math.max(to.top - from.bottom, from.top - to.bottom);
|
|
35
|
+
const horizontal = gapX === gapY ? Math.abs(dx) >= Math.abs(dy) : gapX > gapY;
|
|
36
|
+
|
|
37
|
+
if (horizontal) return dx >= 0 ? 'right' : 'left';
|
|
38
|
+
|
|
39
|
+
return dy >= 0 ? 'bottom' : 'top';
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Resolves `'auto'` sides for a connection between two boxes.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* resolveSides(startRect, endRect, 'auto', 'auto');
|
|
48
|
+
* // { startSide: 'right', endSide: 'left' }
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export const resolveSides = (
|
|
52
|
+
startRect: ArcRectInterface,
|
|
53
|
+
endRect: ArcRectInterface,
|
|
54
|
+
startSide: ArcSideOptionType = 'auto',
|
|
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
|
+
});
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Returns the point on a side of a box where a line attaches.
|
|
64
|
+
*
|
|
65
|
+
* When `count` lines share the side, they are spread around the middle of the
|
|
66
|
+
* side, `spacing` pixels apart, shrinking to fit the side if needed. `gap`
|
|
67
|
+
* moves the point away from the box.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* // The second of three lines on the top side
|
|
72
|
+
* getAnchorPoint(rect, 'top', { index: 1, count: 3 });
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export const getAnchorPoint = (
|
|
76
|
+
rect: ArcRectInterface,
|
|
77
|
+
side: ArcSideType,
|
|
78
|
+
{
|
|
79
|
+
index = 0,
|
|
80
|
+
count = 1,
|
|
81
|
+
spacing = 16,
|
|
82
|
+
gap = 0
|
|
83
|
+
}: { index?: number; count?: number; spacing?: number; gap?: number } = {}
|
|
84
|
+
): ArcPointInterface => {
|
|
85
|
+
const length = side === 'top' || side === 'bottom' ? rect.width : rect.height;
|
|
86
|
+
const step = count > 1 ? Math.min(spacing, length / count) : 0;
|
|
87
|
+
const shift = (index - (count - 1) / 2) * step;
|
|
88
|
+
const { x: cx, y: cy } = center(rect);
|
|
89
|
+
|
|
90
|
+
let point: ArcPointInterface;
|
|
91
|
+
|
|
92
|
+
switch (side) {
|
|
93
|
+
case 'top':
|
|
94
|
+
point = { x: cx + shift, y: rect.top };
|
|
95
|
+
break;
|
|
96
|
+
case 'bottom':
|
|
97
|
+
point = { x: cx + shift, y: rect.bottom };
|
|
98
|
+
break;
|
|
99
|
+
case 'left':
|
|
100
|
+
point = { x: rect.left, y: cy + shift };
|
|
101
|
+
break;
|
|
102
|
+
case 'right':
|
|
103
|
+
point = { x: rect.right, y: cy + shift };
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return add(point, NORMALS[side], gap);
|
|
108
|
+
};
|
package/src/utils/svg.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export const SVG_NS = 'http://www.w3.org/2000/svg';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates an SVG element with the given attributes.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* const path = createSVGElement('path', { fill: 'none' });
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
export const createSVGElement = <K extends keyof SVGElementTagNameMap>(
|
|
12
|
+
tagName: K,
|
|
13
|
+
attributes: Record<string, string | number> = {}
|
|
14
|
+
): SVGElementTagNameMap[K] => {
|
|
15
|
+
const el = document.createElementNS(SVG_NS, tagName);
|
|
16
|
+
|
|
17
|
+
setAttributes(el, attributes);
|
|
18
|
+
|
|
19
|
+
return el;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Sets attributes on an element. `null` and `undefined` values remove the
|
|
24
|
+
* attribute.
|
|
25
|
+
*/
|
|
26
|
+
export const setAttributes = (
|
|
27
|
+
el: Element,
|
|
28
|
+
attributes: Record<string, string | number | null | undefined>
|
|
29
|
+
): void => {
|
|
30
|
+
for (const [name, value] of Object.entries(attributes)) {
|
|
31
|
+
if (value === null || value === undefined) el.removeAttribute(name);
|
|
32
|
+
else el.setAttribute(name, String(value));
|
|
33
|
+
}
|
|
34
|
+
};
|
|
@@ -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
|
+
};
|