@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
package/package.json
CHANGED
|
@@ -1,6 +1,121 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@phun-ky/arclace",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Draw SVG lines between DOM elements. Framework agnostic, zero dependencies.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"connect",
|
|
7
|
+
"connector",
|
|
8
|
+
"svg",
|
|
9
|
+
"line",
|
|
10
|
+
"lines",
|
|
11
|
+
"arrow",
|
|
12
|
+
"edges",
|
|
13
|
+
"diagram",
|
|
14
|
+
"flow",
|
|
15
|
+
"flowchart",
|
|
16
|
+
"graph",
|
|
17
|
+
"dom",
|
|
18
|
+
"bezier"
|
|
19
|
+
],
|
|
20
|
+
"homepage": "https://phun-ky.net/arclace/",
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/phun-ky/arclace/issues"
|
|
23
|
+
},
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/phun-ky/arclace.git"
|
|
27
|
+
},
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"author": "Alexander Vassbotn Røyne-Helgesen <alexander@phun-ky.net>",
|
|
30
|
+
"type": "module",
|
|
31
|
+
"exports": {
|
|
32
|
+
".": {
|
|
33
|
+
"types": "./dist/arclace.d.ts",
|
|
34
|
+
"default": "./dist/arclace.js"
|
|
35
|
+
},
|
|
36
|
+
"./style.css": "./dist/style.css",
|
|
37
|
+
"./package.json": "./package.json",
|
|
38
|
+
"./dist/*": "./dist/*"
|
|
39
|
+
},
|
|
40
|
+
"main": "./dist/arclace.js",
|
|
41
|
+
"module": "./dist/arclace.js",
|
|
42
|
+
"types": "./dist/arclace.d.ts",
|
|
43
|
+
"unpkg": "./dist/arclace.umd.cjs",
|
|
44
|
+
"jsdelivr": "./dist/arclace.umd.cjs",
|
|
45
|
+
"files": [
|
|
46
|
+
"dist",
|
|
47
|
+
"src",
|
|
48
|
+
"!src/**/__tests__",
|
|
49
|
+
"CHANGELOG.md"
|
|
50
|
+
],
|
|
51
|
+
"sideEffects": [
|
|
52
|
+
"*.css"
|
|
53
|
+
],
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"access": "public"
|
|
56
|
+
},
|
|
57
|
+
"engines": {
|
|
58
|
+
"node": ">=22.12.0"
|
|
59
|
+
},
|
|
60
|
+
"scripts": {
|
|
61
|
+
"bestpractices:url": "node scripts/bestpractices-url.mjs",
|
|
62
|
+
"build": "rm -rf dts && vite build && tsc -p tsconfig.build.json && rollup -c",
|
|
63
|
+
"check:bestpractices": "node scripts/check-bestpractices.mjs",
|
|
64
|
+
"check:pack": "node scripts/check-pack.mjs",
|
|
65
|
+
"commit": "npx git-cz",
|
|
66
|
+
"dev": "vite --force",
|
|
67
|
+
"docs:gen": "typedoc",
|
|
68
|
+
"docs:check": "typedoc --emit none --treatWarningsAsErrors",
|
|
69
|
+
"predocs:dev": "npm run docs:gen",
|
|
70
|
+
"docs:dev": "vitepress dev docs",
|
|
71
|
+
"docs:build": "vitepress build docs",
|
|
72
|
+
"docs:preview": "vitepress preview docs",
|
|
73
|
+
"lint": "eslint .",
|
|
74
|
+
"prepare": "git config core.hooksPath .githooks || true",
|
|
75
|
+
"prepublishOnly": "npm run build",
|
|
76
|
+
"preview": "vite preview",
|
|
77
|
+
"release": "release-it",
|
|
78
|
+
"release:dry-run": ".github/scripts/should-release.sh && release-it --ci --dry-run --github.release=false --no-git.requireCleanWorkingDir --no-git.requireUpstream",
|
|
79
|
+
"test": "node --import tsx --import global-jsdom/register --test --no-warnings 'src/**/__tests__/**/*.spec.ts'",
|
|
80
|
+
"pretest:ci": "rm -rf coverage && mkdir -p coverage",
|
|
81
|
+
"test:ci": "node --import tsx --import global-jsdom/register --test --no-warnings --experimental-test-coverage --test-coverage-include='src/**/*.ts' --test-coverage-exclude='src/**/__tests__/**' --test-coverage-exclude='src/types.ts' --test-coverage-lines=99 --test-coverage-functions=100 --test-coverage-branches=95 --test-reporter=cobertura --test-reporter-destination=coverage/cobertura-coverage.xml --test-reporter=spec --test-reporter-destination=stdout 'src/**/__tests__/**/*.spec.ts'",
|
|
82
|
+
"typecheck": "tsc --noEmit"
|
|
83
|
+
},
|
|
84
|
+
"devDependencies": {
|
|
85
|
+
"@arethetypeswrong/cli": "^0.18.5",
|
|
86
|
+
"@release-it/conventional-changelog": "^12.0.2",
|
|
87
|
+
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
88
|
+
"@types/node": "^24.19.2",
|
|
89
|
+
"cobertura": "^1.0.3",
|
|
90
|
+
"eslint": "^10.14.0",
|
|
91
|
+
"eslint-config-phun-ky": "^2.0.10",
|
|
92
|
+
"fast-check": "^4.10.2",
|
|
93
|
+
"git-cz": "^4.9.0",
|
|
94
|
+
"global-jsdom": "^30.0.0",
|
|
95
|
+
"globals": "^17.14.0",
|
|
96
|
+
"jsdom": "^30.1.3",
|
|
97
|
+
"prettier": "^3.9.10",
|
|
98
|
+
"publint": "^0.3.25",
|
|
99
|
+
"release-it": "^21.1.2",
|
|
100
|
+
"rollup": "^4.67.4",
|
|
101
|
+
"rollup-plugin-dts": "^6.5.1",
|
|
102
|
+
"tsx": "^4.23.15",
|
|
103
|
+
"typedoc": "^0.28.20",
|
|
104
|
+
"typedoc-plugin-markdown": "^4.14.1",
|
|
105
|
+
"typedoc-vitepress-theme": "^1.1.4",
|
|
106
|
+
"typescript": "~5.7.2",
|
|
107
|
+
"vite": "^6.2.0",
|
|
108
|
+
"vitepress": "^1.6.4"
|
|
109
|
+
},
|
|
110
|
+
"overrides": {
|
|
111
|
+
"get-uri": {
|
|
112
|
+
"basic-ftp": "^6.2.5"
|
|
113
|
+
},
|
|
114
|
+
"micromark-extension-math": {
|
|
115
|
+
"katex": "^0.18.2"
|
|
116
|
+
},
|
|
117
|
+
"vitepress": {
|
|
118
|
+
"vite": "^6.4.5"
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
package/src/Arclace.ts
ADDED
|
@@ -0,0 +1,578 @@
|
|
|
1
|
+
import { Connection } from './Connection';
|
|
2
|
+
import { ArclaceError } from './errors';
|
|
3
|
+
import type {
|
|
4
|
+
ArcConnectionDefinitionType,
|
|
5
|
+
ArcConnectionOptionsType,
|
|
6
|
+
ArcElementTargetType,
|
|
7
|
+
ArcOptionsType,
|
|
8
|
+
ArcRectInterface,
|
|
9
|
+
ArcSideType
|
|
10
|
+
} from './types';
|
|
11
|
+
import { center, isHorizontalSide } from './utils/geometry';
|
|
12
|
+
import { uniqueID } from './utils/id';
|
|
13
|
+
import { getLocalRect, getOrigin, isRendered } from './utils/measure';
|
|
14
|
+
import { querySelector, resolveElement } from './utils/resolve-element';
|
|
15
|
+
import { getAnchorPoint, resolveSides } from './utils/sides';
|
|
16
|
+
import { createSVGElement } from './utils/svg';
|
|
17
|
+
import {
|
|
18
|
+
assertObject,
|
|
19
|
+
validateConnectionOptions,
|
|
20
|
+
validateOptions
|
|
21
|
+
} from './utils/validate';
|
|
22
|
+
|
|
23
|
+
type ArcLayoutItemType = {
|
|
24
|
+
connection: Connection;
|
|
25
|
+
startRect: ArcRectInterface;
|
|
26
|
+
endRect: ArcRectInterface;
|
|
27
|
+
startSide: ArcSideType;
|
|
28
|
+
endSide: ArcSideType;
|
|
29
|
+
startSlot: { index: number; count: number };
|
|
30
|
+
endSlot: { index: number; count: number };
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// A connection that has been checked and can be added
|
|
34
|
+
type ArcPreparedConnectionType = {
|
|
35
|
+
id: string;
|
|
36
|
+
startElement: Element;
|
|
37
|
+
endElement: Element;
|
|
38
|
+
options: ArcConnectionOptionsType;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
type ArcSlotEntryType = {
|
|
42
|
+
slot: { index: number; count: number };
|
|
43
|
+
// Position of the other end along the side, used to avoid crossing lines
|
|
44
|
+
key: number;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const nextFrame = (callback: () => void): number =>
|
|
48
|
+
typeof requestAnimationFrame === 'function'
|
|
49
|
+
? requestAnimationFrame(callback)
|
|
50
|
+
: (setTimeout(callback, 16) as unknown as number);
|
|
51
|
+
const cancelFrame = (handle: number): void => {
|
|
52
|
+
if (typeof cancelAnimationFrame === 'function') cancelAnimationFrame(handle);
|
|
53
|
+
else clearTimeout(handle);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Draws and maintains SVG lines between DOM elements.
|
|
58
|
+
*
|
|
59
|
+
* An instance adds one `<svg class="arc-svg">` overlay to its container and
|
|
60
|
+
* draws every connection into it.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* import { Arclace } from '@phun-ky/arclace';
|
|
65
|
+
*
|
|
66
|
+
* const arclace = new Arclace({ container: '#diagram' });
|
|
67
|
+
*
|
|
68
|
+
* arclace.connect('#a', '#b', { arrow: 'end', label: 'depends on' });
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export class Arclace {
|
|
72
|
+
/**
|
|
73
|
+
* The element the overlay was added to.
|
|
74
|
+
*/
|
|
75
|
+
readonly container: HTMLElement;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The SVG overlay all lines are drawn in.
|
|
79
|
+
*/
|
|
80
|
+
readonly svg: SVGSVGElement;
|
|
81
|
+
|
|
82
|
+
#options: ArcOptionsType;
|
|
83
|
+
#connections = new Map<string, Connection>();
|
|
84
|
+
#frame = 0;
|
|
85
|
+
#loop = 0;
|
|
86
|
+
#destroyed = false;
|
|
87
|
+
#resizeObserver: ResizeObserver | null = null;
|
|
88
|
+
#mutationObserver: MutationObserver | null = null;
|
|
89
|
+
#originalPosition: string | null = null;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* @throws {TypeError} When an option has the wrong type.
|
|
93
|
+
* @throws {RangeError} When an option is out of range.
|
|
94
|
+
* @throws {ArclaceError} When there is no DOM, or the container can't be
|
|
95
|
+
* found.
|
|
96
|
+
*/
|
|
97
|
+
constructor(options: ArcOptionsType = {}) {
|
|
98
|
+
if (typeof document === 'undefined') {
|
|
99
|
+
throw new ArclaceError('a DOM is required');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
this.#options = validateOptions(options);
|
|
103
|
+
this.container = (
|
|
104
|
+
options.container
|
|
105
|
+
? resolveElement(options.container, 'container')
|
|
106
|
+
: document.body
|
|
107
|
+
) as HTMLElement;
|
|
108
|
+
|
|
109
|
+
// The overlay is positioned absolutely, so the container has to be its
|
|
110
|
+
// containing block for it to scroll and clip with the container
|
|
111
|
+
if (
|
|
112
|
+
this.container !== document.body &&
|
|
113
|
+
getComputedStyle(this.container).position === 'static'
|
|
114
|
+
) {
|
|
115
|
+
this.#originalPosition = this.container.style.position;
|
|
116
|
+
this.container.style.position = 'relative';
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
this.svg = createSVGElement('svg', {
|
|
120
|
+
class: ['arc-svg', options.className].filter(Boolean).join(' '),
|
|
121
|
+
'aria-hidden': 'true'
|
|
122
|
+
});
|
|
123
|
+
Object.assign(this.svg.style, {
|
|
124
|
+
position: 'absolute',
|
|
125
|
+
top: '0',
|
|
126
|
+
left: '0',
|
|
127
|
+
width: '100%',
|
|
128
|
+
height: '100%',
|
|
129
|
+
overflow: 'visible',
|
|
130
|
+
pointerEvents: 'none'
|
|
131
|
+
});
|
|
132
|
+
this.container.prepend(this.svg);
|
|
133
|
+
this.#startAutoUpdate();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* All current connections, in the order they were made.
|
|
138
|
+
*/
|
|
139
|
+
get connections(): Connection[] {
|
|
140
|
+
return [...this.#connections.values()];
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Connects two elements and draws the line right away.
|
|
145
|
+
*
|
|
146
|
+
* @param startElement - The element (or selector) the line starts at.
|
|
147
|
+
* @param endElement - The element (or selector) the line ends at.
|
|
148
|
+
* @param options - Options for this connection, merged over the instance defaults.
|
|
149
|
+
* @throws {TypeError} When an element or an option has the wrong type.
|
|
150
|
+
* @throws {RangeError} When an option is out of range.
|
|
151
|
+
* @throws {ArclaceError} When an element can't be found, the id is taken
|
|
152
|
+
* or the instance has been destroyed. Nothing is added.
|
|
153
|
+
* @example
|
|
154
|
+
* ```ts
|
|
155
|
+
* const connection = arclace.connect(a, b, { type: 'orthogonal', arrow: 'end' });
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
connect(
|
|
159
|
+
startElement: ArcElementTargetType,
|
|
160
|
+
endElement: ArcElementTargetType,
|
|
161
|
+
options: ArcConnectionOptionsType = {}
|
|
162
|
+
): Connection {
|
|
163
|
+
const connection = this.#add(
|
|
164
|
+
this.#prepare(startElement, endElement, options, 'options')
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
this.update();
|
|
168
|
+
|
|
169
|
+
return connection;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Connects many pairs of elements and draws them in one go. Every
|
|
174
|
+
* definition is checked first, so either all of them are added or none.
|
|
175
|
+
*
|
|
176
|
+
* @throws {TypeError} When `definitions` isn't an array, or an element or
|
|
177
|
+
* an option has the wrong type.
|
|
178
|
+
* @throws {RangeError} When an option is out of range.
|
|
179
|
+
* @throws {ArclaceError} When an element can't be found, an id is taken or
|
|
180
|
+
* repeated, or the instance has been destroyed.
|
|
181
|
+
* @example
|
|
182
|
+
* ```ts
|
|
183
|
+
* arclace.connectAll([
|
|
184
|
+
* { startElement: '#a', endElement: '#b' },
|
|
185
|
+
* { startElement: '#a', endElement: '#c', dashed: true }
|
|
186
|
+
* ]);
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
connectAll(definitions: ArcConnectionDefinitionType[]): Connection[] {
|
|
190
|
+
if (!Array.isArray(definitions)) {
|
|
191
|
+
throw new TypeError(
|
|
192
|
+
'arclace: connectAll() needs an array of definitions'
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const ids = new Set<string>();
|
|
197
|
+
const prepared = definitions.map((definition, index) => {
|
|
198
|
+
const name = `definitions[${index}]`;
|
|
199
|
+
|
|
200
|
+
assertObject(definition, name);
|
|
201
|
+
|
|
202
|
+
const { startElement, endElement, ...options } = definition;
|
|
203
|
+
const connection = this.#prepare(startElement, endElement, options, name);
|
|
204
|
+
|
|
205
|
+
if (ids.has(connection.id)) {
|
|
206
|
+
throw new ArclaceError(
|
|
207
|
+
`${name}.id "${connection.id}" is used more than once`
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
ids.add(connection.id);
|
|
212
|
+
|
|
213
|
+
return connection;
|
|
214
|
+
});
|
|
215
|
+
const connections = prepared.map((connection) => this.#add(connection));
|
|
216
|
+
|
|
217
|
+
this.update();
|
|
218
|
+
|
|
219
|
+
return connections;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Returns the connection with the given id.
|
|
224
|
+
*/
|
|
225
|
+
get(id: string): Connection | undefined {
|
|
226
|
+
return this.#connections.get(id);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Returns every connection that starts or ends at an element. A selector
|
|
231
|
+
* that matches nothing returns an empty array.
|
|
232
|
+
*
|
|
233
|
+
* @throws {TypeError} When the target is neither an element nor a string.
|
|
234
|
+
* @throws {ArclaceError} When the selector is invalid.
|
|
235
|
+
*/
|
|
236
|
+
find(target: ArcElementTargetType): Connection[] {
|
|
237
|
+
const el =
|
|
238
|
+
typeof target === 'string'
|
|
239
|
+
? querySelector(target)
|
|
240
|
+
: resolveElement(target);
|
|
241
|
+
|
|
242
|
+
if (!el) return [];
|
|
243
|
+
|
|
244
|
+
return this.connections.filter(
|
|
245
|
+
(connection) =>
|
|
246
|
+
connection.startElement === el || connection.endElement === el
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Removes connections.
|
|
252
|
+
*
|
|
253
|
+
* @param target - A connection, a connection id, or an element (or
|
|
254
|
+
* selector) whose connections should all be removed. Targets that match
|
|
255
|
+
* nothing, and `null` or `undefined`, are ignored.
|
|
256
|
+
* @throws {TypeError} When the target is another kind of value.
|
|
257
|
+
* @throws {ArclaceError} When a string is neither an id nor a valid
|
|
258
|
+
* selector.
|
|
259
|
+
* @example
|
|
260
|
+
* ```ts
|
|
261
|
+
* arclace.disconnect(connection);
|
|
262
|
+
* arclace.disconnect('my-connection-id');
|
|
263
|
+
* arclace.disconnect(document.querySelector('#a'));
|
|
264
|
+
* ```
|
|
265
|
+
*/
|
|
266
|
+
disconnect(target: Connection | string | Element): void {
|
|
267
|
+
let removed: Connection[];
|
|
268
|
+
|
|
269
|
+
if (target === null || target === undefined) return;
|
|
270
|
+
|
|
271
|
+
if (target instanceof Connection) {
|
|
272
|
+
removed = [target];
|
|
273
|
+
} else if (typeof target === 'string' && this.#connections.has(target)) {
|
|
274
|
+
removed = [this.#connections.get(target)!];
|
|
275
|
+
} else if (typeof target === 'string' || target instanceof Element) {
|
|
276
|
+
removed = this.find(target);
|
|
277
|
+
} else {
|
|
278
|
+
throw new TypeError(
|
|
279
|
+
'arclace: disconnect() needs a connection, an id, an element or a selector'
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
for (const connection of removed) {
|
|
284
|
+
if (this.#connections.get(connection.id) !== connection) continue;
|
|
285
|
+
|
|
286
|
+
this.#connections.delete(connection.id);
|
|
287
|
+
connection._dispose();
|
|
288
|
+
this.#unobserve(connection.startElement);
|
|
289
|
+
this.#unobserve(connection.endElement);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (removed.length > 0) this.update();
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Removes every connection.
|
|
297
|
+
*/
|
|
298
|
+
clear(): void {
|
|
299
|
+
for (const connection of this.#connections.values()) {
|
|
300
|
+
connection._dispose();
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
this.#connections.clear();
|
|
304
|
+
this.#resizeObserver?.disconnect();
|
|
305
|
+
|
|
306
|
+
if (this.#resizeObserver) this.#resizeObserver.observe(this.container);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Remeasures every element and redraws every connection. Called
|
|
311
|
+
* automatically when `autoUpdate` is on; call it yourself after moving
|
|
312
|
+
* elements in ways the page can't observe.
|
|
313
|
+
*/
|
|
314
|
+
update(): void {
|
|
315
|
+
if (this.#destroyed) return;
|
|
316
|
+
|
|
317
|
+
if (this.#frame) {
|
|
318
|
+
cancelFrame(this.#frame);
|
|
319
|
+
this.#frame = 0;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// Measure everything first, then write, so the browser only lays out once
|
|
323
|
+
const origin = getOrigin(this.svg);
|
|
324
|
+
const items: ArcLayoutItemType[] = [];
|
|
325
|
+
const slots = new Map<Element, Record<ArcSideType, ArcSlotEntryType[]>>();
|
|
326
|
+
const addToSlot = (
|
|
327
|
+
el: Element,
|
|
328
|
+
side: ArcSideType,
|
|
329
|
+
otherRect: ArcRectInterface
|
|
330
|
+
): { index: number; count: number } => {
|
|
331
|
+
const slot = { index: 0, count: 1 };
|
|
332
|
+
const other = center(otherRect);
|
|
333
|
+
|
|
334
|
+
if (!slots.has(el)) {
|
|
335
|
+
slots.set(el, { top: [], right: [], bottom: [], left: [] });
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
slots.get(el)![side].push({
|
|
339
|
+
slot,
|
|
340
|
+
key: isHorizontalSide(side) ? other.y : other.x
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
return slot;
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
for (const connection of this.#connections.values()) {
|
|
347
|
+
const { startElement, endElement } = connection;
|
|
348
|
+
|
|
349
|
+
if (!isRendered(startElement) || !isRendered(endElement)) {
|
|
350
|
+
connection._hide();
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
const { spread, startSide, endSide } = connection.options;
|
|
355
|
+
const startRect = getLocalRect(startElement, origin);
|
|
356
|
+
const endRect = getLocalRect(endElement, origin);
|
|
357
|
+
const sides = resolveSides(startRect, endRect, startSide, endSide);
|
|
358
|
+
const solo = { index: 0, count: 1 };
|
|
359
|
+
|
|
360
|
+
items.push({
|
|
361
|
+
connection,
|
|
362
|
+
startRect,
|
|
363
|
+
endRect,
|
|
364
|
+
...sides,
|
|
365
|
+
startSlot: spread
|
|
366
|
+
? addToSlot(startElement, sides.startSide, endRect)
|
|
367
|
+
: solo,
|
|
368
|
+
endSlot: spread
|
|
369
|
+
? addToSlot(endElement, sides.endSide, startRect)
|
|
370
|
+
: { ...solo }
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
for (const sides of slots.values()) {
|
|
375
|
+
for (const entries of Object.values(sides)) {
|
|
376
|
+
entries
|
|
377
|
+
.sort((a, b) => a.key - b.key)
|
|
378
|
+
.forEach((entry, index) => {
|
|
379
|
+
entry.slot.index = index;
|
|
380
|
+
entry.slot.count = entries.length;
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
for (const item of items) {
|
|
386
|
+
const { connection, startRect, endRect, startSide, endSide } = item;
|
|
387
|
+
const { spacing, gap } = connection.options;
|
|
388
|
+
|
|
389
|
+
connection._draw(
|
|
390
|
+
getAnchorPoint(startRect, startSide, {
|
|
391
|
+
...item.startSlot,
|
|
392
|
+
spacing,
|
|
393
|
+
gap
|
|
394
|
+
}),
|
|
395
|
+
startSide,
|
|
396
|
+
getAnchorPoint(endRect, endSide, { ...item.endSlot, spacing, gap }),
|
|
397
|
+
endSide
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Removes every connection and the overlay, and stops listening for
|
|
404
|
+
* changes. The instance can't be used afterwards.
|
|
405
|
+
*/
|
|
406
|
+
destroy(): void {
|
|
407
|
+
if (this.#destroyed) return;
|
|
408
|
+
|
|
409
|
+
this.clear();
|
|
410
|
+
this.#destroyed = true;
|
|
411
|
+
|
|
412
|
+
if (this.#frame) cancelFrame(this.#frame);
|
|
413
|
+
|
|
414
|
+
if (this.#loop) cancelFrame(this.#loop);
|
|
415
|
+
|
|
416
|
+
window.removeEventListener('resize', this.#scheduleUpdate);
|
|
417
|
+
document.removeEventListener('scroll', this.#scheduleUpdate, {
|
|
418
|
+
capture: true
|
|
419
|
+
});
|
|
420
|
+
this.#resizeObserver?.disconnect();
|
|
421
|
+
this.#mutationObserver?.disconnect();
|
|
422
|
+
this.svg.remove();
|
|
423
|
+
|
|
424
|
+
if (this.#originalPosition !== null) {
|
|
425
|
+
this.container.style.position = this.#originalPosition;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Checks a connection without adding anything, so a bad option can't leave
|
|
431
|
+
* a connection behind that breaks every later update.
|
|
432
|
+
*/
|
|
433
|
+
#prepare(
|
|
434
|
+
startTarget: unknown,
|
|
435
|
+
endTarget: unknown,
|
|
436
|
+
options: unknown,
|
|
437
|
+
name: string
|
|
438
|
+
): ArcPreparedConnectionType {
|
|
439
|
+
if (this.#destroyed) {
|
|
440
|
+
throw new ArclaceError('this instance has been destroyed');
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
validateConnectionOptions(options, name);
|
|
444
|
+
|
|
445
|
+
const startElement = resolveElement(startTarget, 'startElement');
|
|
446
|
+
const endElement = resolveElement(endTarget, 'endElement');
|
|
447
|
+
const valid = options as ArcConnectionOptionsType;
|
|
448
|
+
const id = valid.id ?? `arc-${uniqueID()}`;
|
|
449
|
+
|
|
450
|
+
if (this.#connections.has(id)) {
|
|
451
|
+
throw new ArclaceError(`a connection with id "${id}" exists`);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
return { id, startElement, endElement, options: valid };
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
#add({
|
|
458
|
+
id,
|
|
459
|
+
startElement,
|
|
460
|
+
endElement,
|
|
461
|
+
options
|
|
462
|
+
}: ArcPreparedConnectionType): Connection {
|
|
463
|
+
const connection = Connection._create({
|
|
464
|
+
id,
|
|
465
|
+
startElement,
|
|
466
|
+
endElement,
|
|
467
|
+
options,
|
|
468
|
+
defaults: this.#options.defaults,
|
|
469
|
+
owner: this
|
|
470
|
+
});
|
|
471
|
+
|
|
472
|
+
this.#connections.set(id, connection);
|
|
473
|
+
this.svg.appendChild(connection.element);
|
|
474
|
+
this.#resizeObserver?.observe(startElement);
|
|
475
|
+
this.#resizeObserver?.observe(endElement);
|
|
476
|
+
|
|
477
|
+
return connection;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
#unobserve(el: Element): void {
|
|
481
|
+
if (!this.#resizeObserver || el === this.container) return;
|
|
482
|
+
|
|
483
|
+
const inUse = this.connections.some(
|
|
484
|
+
(connection) =>
|
|
485
|
+
connection.startElement === el || connection.endElement === el
|
|
486
|
+
);
|
|
487
|
+
|
|
488
|
+
if (!inUse) this.#resizeObserver.unobserve(el);
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
#scheduleUpdate = (): void => {
|
|
492
|
+
if (this.#frame || this.#destroyed) return;
|
|
493
|
+
|
|
494
|
+
this.#frame = nextFrame(() => {
|
|
495
|
+
this.#frame = 0;
|
|
496
|
+
this.update();
|
|
497
|
+
});
|
|
498
|
+
};
|
|
499
|
+
|
|
500
|
+
#startAutoUpdate(): void {
|
|
501
|
+
const { autoUpdate = true } = this.#options;
|
|
502
|
+
|
|
503
|
+
if (autoUpdate === false) return;
|
|
504
|
+
|
|
505
|
+
if (autoUpdate === 'frame') {
|
|
506
|
+
const loop = (): void => {
|
|
507
|
+
this.update();
|
|
508
|
+
this.#loop = nextFrame(loop);
|
|
509
|
+
};
|
|
510
|
+
|
|
511
|
+
this.#loop = nextFrame(loop);
|
|
512
|
+
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
window.addEventListener('resize', this.#scheduleUpdate);
|
|
517
|
+
// Capture scroll events from any scrolling element, not just the page
|
|
518
|
+
document.addEventListener('scroll', this.#scheduleUpdate, {
|
|
519
|
+
capture: true,
|
|
520
|
+
passive: true
|
|
521
|
+
});
|
|
522
|
+
|
|
523
|
+
if (typeof ResizeObserver !== 'undefined') {
|
|
524
|
+
this.#resizeObserver = new ResizeObserver(this.#scheduleUpdate);
|
|
525
|
+
this.#resizeObserver.observe(this.container);
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
if (typeof MutationObserver !== 'undefined') {
|
|
529
|
+
this.#mutationObserver = new MutationObserver((records) => {
|
|
530
|
+
// Ignore the changes we make to the overlay ourselves
|
|
531
|
+
if (records.some((record) => !this.svg.contains(record.target))) {
|
|
532
|
+
this.#scheduleUpdate();
|
|
533
|
+
}
|
|
534
|
+
});
|
|
535
|
+
this.#mutationObserver.observe(this.container, {
|
|
536
|
+
subtree: true,
|
|
537
|
+
childList: true,
|
|
538
|
+
attributes: true,
|
|
539
|
+
attributeFilter: ['style', 'class', 'hidden']
|
|
540
|
+
});
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
document.fonts?.ready.then(this.#scheduleUpdate);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
/**
|
|
548
|
+
* Creates a {@link Arclace} instance and connects the given pairs.
|
|
549
|
+
*
|
|
550
|
+
* @example
|
|
551
|
+
* ```ts
|
|
552
|
+
* import { createConnections } from '@phun-ky/arclace';
|
|
553
|
+
*
|
|
554
|
+
* const arclace = createConnections(
|
|
555
|
+
* [
|
|
556
|
+
* { startElement: '#a', endElement: '#b' },
|
|
557
|
+
* { startElement: '#b', endElement: '#c', arrow: 'end' }
|
|
558
|
+
* ],
|
|
559
|
+
* { defaults: { type: 'orthogonal' } }
|
|
560
|
+
* );
|
|
561
|
+
* ```
|
|
562
|
+
*/
|
|
563
|
+
export const createConnections = (
|
|
564
|
+
definitions: ArcConnectionDefinitionType[],
|
|
565
|
+
options?: ArcOptionsType
|
|
566
|
+
): Arclace => {
|
|
567
|
+
const arclace = new Arclace(options);
|
|
568
|
+
|
|
569
|
+
try {
|
|
570
|
+
arclace.connectAll(definitions);
|
|
571
|
+
} catch (error) {
|
|
572
|
+
// Don't leave an overlay and its listeners behind
|
|
573
|
+
arclace.destroy();
|
|
574
|
+
throw error;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
return arclace;
|
|
578
|
+
};
|