@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/Arclace.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Connection } from './Connection';
|
|
2
|
+
import { ArclaceError } from './errors';
|
|
2
3
|
import type {
|
|
3
4
|
ArcConnectionDefinitionType,
|
|
4
5
|
ArcConnectionOptionsType,
|
|
@@ -10,18 +11,31 @@ import type {
|
|
|
10
11
|
import { center, isHorizontalSide } from './utils/geometry';
|
|
11
12
|
import { uniqueID } from './utils/id';
|
|
12
13
|
import { getLocalRect, getOrigin, isRendered } from './utils/measure';
|
|
13
|
-
import { resolveElement } from './utils/resolve-element';
|
|
14
|
+
import { querySelector, resolveElement } from './utils/resolve-element';
|
|
14
15
|
import { getAnchorPoint, resolveSides } from './utils/sides';
|
|
15
16
|
import { createSVGElement } from './utils/svg';
|
|
17
|
+
import {
|
|
18
|
+
assertObject,
|
|
19
|
+
validateConnectionOptions,
|
|
20
|
+
validateOptions
|
|
21
|
+
} from './utils/validate';
|
|
16
22
|
|
|
17
23
|
type ArcLayoutItemType = {
|
|
18
24
|
connection: Connection;
|
|
19
25
|
startRect: ArcRectInterface;
|
|
20
|
-
|
|
26
|
+
endRect: ArcRectInterface;
|
|
21
27
|
startSide: ArcSideType;
|
|
22
|
-
|
|
28
|
+
endSide: ArcSideType;
|
|
23
29
|
startSlot: { index: number; count: number };
|
|
24
|
-
|
|
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;
|
|
25
39
|
};
|
|
26
40
|
|
|
27
41
|
type ArcSlotEntryType = {
|
|
@@ -74,12 +88,18 @@ export class Arclace {
|
|
|
74
88
|
#mutationObserver: MutationObserver | null = null;
|
|
75
89
|
#originalPosition: string | null = null;
|
|
76
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
|
+
*/
|
|
77
97
|
constructor(options: ArcOptionsType = {}) {
|
|
78
98
|
if (typeof document === 'undefined') {
|
|
79
|
-
throw
|
|
99
|
+
throw new ArclaceError('a DOM is required');
|
|
80
100
|
}
|
|
81
101
|
|
|
82
|
-
this.#options = options;
|
|
102
|
+
this.#options = validateOptions(options);
|
|
83
103
|
this.container = (
|
|
84
104
|
options.container
|
|
85
105
|
? resolveElement(options.container, 'container')
|
|
@@ -124,9 +144,12 @@ export class Arclace {
|
|
|
124
144
|
* Connects two elements and draws the line right away.
|
|
125
145
|
*
|
|
126
146
|
* @param startElement - The element (or selector) the line starts at.
|
|
127
|
-
* @param
|
|
147
|
+
* @param endElement - The element (or selector) the line ends at.
|
|
128
148
|
* @param options - Options for this connection, merged over the instance defaults.
|
|
129
|
-
* @throws {
|
|
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.
|
|
130
153
|
* @example
|
|
131
154
|
* ```ts
|
|
132
155
|
* const connection = arclace.connect(a, b, { type: 'orthogonal', arrow: 'end' });
|
|
@@ -134,10 +157,12 @@ export class Arclace {
|
|
|
134
157
|
*/
|
|
135
158
|
connect(
|
|
136
159
|
startElement: ArcElementTargetType,
|
|
137
|
-
|
|
160
|
+
endElement: ArcElementTargetType,
|
|
138
161
|
options: ArcConnectionOptionsType = {}
|
|
139
162
|
): Connection {
|
|
140
|
-
const connection = this.#add(
|
|
163
|
+
const connection = this.#add(
|
|
164
|
+
this.#prepare(startElement, endElement, options, 'options')
|
|
165
|
+
);
|
|
141
166
|
|
|
142
167
|
this.update();
|
|
143
168
|
|
|
@@ -145,21 +170,49 @@ export class Arclace {
|
|
|
145
170
|
}
|
|
146
171
|
|
|
147
172
|
/**
|
|
148
|
-
* Connects many pairs of elements and draws them in one go.
|
|
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.
|
|
149
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.
|
|
150
181
|
* @example
|
|
151
182
|
* ```ts
|
|
152
183
|
* arclace.connectAll([
|
|
153
|
-
* { startElement: '#a',
|
|
154
|
-
* { startElement: '#a',
|
|
184
|
+
* { startElement: '#a', endElement: '#b' },
|
|
185
|
+
* { startElement: '#a', endElement: '#c', dashed: true }
|
|
155
186
|
* ]);
|
|
156
187
|
* ```
|
|
157
188
|
*/
|
|
158
189
|
connectAll(definitions: ArcConnectionDefinitionType[]): Connection[] {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
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));
|
|
163
216
|
|
|
164
217
|
this.update();
|
|
165
218
|
|
|
@@ -174,14 +227,23 @@ export class Arclace {
|
|
|
174
227
|
}
|
|
175
228
|
|
|
176
229
|
/**
|
|
177
|
-
* Returns every connection that starts or
|
|
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.
|
|
178
235
|
*/
|
|
179
236
|
find(target: ArcElementTargetType): Connection[] {
|
|
180
|
-
const el =
|
|
237
|
+
const el =
|
|
238
|
+
typeof target === 'string'
|
|
239
|
+
? querySelector(target)
|
|
240
|
+
: resolveElement(target);
|
|
241
|
+
|
|
242
|
+
if (!el) return [];
|
|
181
243
|
|
|
182
244
|
return this.connections.filter(
|
|
183
245
|
(connection) =>
|
|
184
|
-
connection.startElement === el || connection.
|
|
246
|
+
connection.startElement === el || connection.endElement === el
|
|
185
247
|
);
|
|
186
248
|
}
|
|
187
249
|
|
|
@@ -190,7 +252,10 @@ export class Arclace {
|
|
|
190
252
|
*
|
|
191
253
|
* @param target - A connection, a connection id, or an element (or
|
|
192
254
|
* selector) whose connections should all be removed. Targets that match
|
|
193
|
-
* nothing are ignored.
|
|
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.
|
|
194
259
|
* @example
|
|
195
260
|
* ```ts
|
|
196
261
|
* arclace.disconnect(connection);
|
|
@@ -199,18 +264,20 @@ export class Arclace {
|
|
|
199
264
|
* ```
|
|
200
265
|
*/
|
|
201
266
|
disconnect(target: Connection | string | Element): void {
|
|
202
|
-
let removed: Connection[]
|
|
267
|
+
let removed: Connection[];
|
|
268
|
+
|
|
269
|
+
if (target === null || target === undefined) return;
|
|
203
270
|
|
|
204
271
|
if (target instanceof Connection) {
|
|
205
272
|
removed = [target];
|
|
206
273
|
} else if (typeof target === 'string' && this.#connections.has(target)) {
|
|
207
274
|
removed = [this.#connections.get(target)!];
|
|
208
|
-
} else if (target instanceof Element) {
|
|
275
|
+
} else if (typeof target === 'string' || target instanceof Element) {
|
|
209
276
|
removed = this.find(target);
|
|
210
277
|
} else {
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
278
|
+
throw new TypeError(
|
|
279
|
+
'arclace: disconnect() needs a connection, an id, an element or a selector'
|
|
280
|
+
);
|
|
214
281
|
}
|
|
215
282
|
|
|
216
283
|
for (const connection of removed) {
|
|
@@ -219,7 +286,7 @@ export class Arclace {
|
|
|
219
286
|
this.#connections.delete(connection.id);
|
|
220
287
|
connection._dispose();
|
|
221
288
|
this.#unobserve(connection.startElement);
|
|
222
|
-
this.#unobserve(connection.
|
|
289
|
+
this.#unobserve(connection.endElement);
|
|
223
290
|
}
|
|
224
291
|
|
|
225
292
|
if (removed.length > 0) this.update();
|
|
@@ -277,29 +344,29 @@ export class Arclace {
|
|
|
277
344
|
};
|
|
278
345
|
|
|
279
346
|
for (const connection of this.#connections.values()) {
|
|
280
|
-
const { startElement,
|
|
347
|
+
const { startElement, endElement } = connection;
|
|
281
348
|
|
|
282
|
-
if (!isRendered(startElement) || !isRendered(
|
|
349
|
+
if (!isRendered(startElement) || !isRendered(endElement)) {
|
|
283
350
|
connection._hide();
|
|
284
351
|
continue;
|
|
285
352
|
}
|
|
286
353
|
|
|
287
|
-
const { spread, startSide,
|
|
354
|
+
const { spread, startSide, endSide } = connection.options;
|
|
288
355
|
const startRect = getLocalRect(startElement, origin);
|
|
289
|
-
const
|
|
290
|
-
const sides = resolveSides(startRect,
|
|
356
|
+
const endRect = getLocalRect(endElement, origin);
|
|
357
|
+
const sides = resolveSides(startRect, endRect, startSide, endSide);
|
|
291
358
|
const solo = { index: 0, count: 1 };
|
|
292
359
|
|
|
293
360
|
items.push({
|
|
294
361
|
connection,
|
|
295
362
|
startRect,
|
|
296
|
-
|
|
363
|
+
endRect,
|
|
297
364
|
...sides,
|
|
298
365
|
startSlot: spread
|
|
299
|
-
? addToSlot(startElement, sides.startSide,
|
|
366
|
+
? addToSlot(startElement, sides.startSide, endRect)
|
|
300
367
|
: solo,
|
|
301
|
-
|
|
302
|
-
? addToSlot(
|
|
368
|
+
endSlot: spread
|
|
369
|
+
? addToSlot(endElement, sides.endSide, startRect)
|
|
303
370
|
: { ...solo }
|
|
304
371
|
});
|
|
305
372
|
}
|
|
@@ -316,7 +383,7 @@ export class Arclace {
|
|
|
316
383
|
}
|
|
317
384
|
|
|
318
385
|
for (const item of items) {
|
|
319
|
-
const { connection, startRect,
|
|
386
|
+
const { connection, startRect, endRect, startSide, endSide } = item;
|
|
320
387
|
const { spacing, gap } = connection.options;
|
|
321
388
|
|
|
322
389
|
connection._draw(
|
|
@@ -326,8 +393,8 @@ export class Arclace {
|
|
|
326
393
|
gap
|
|
327
394
|
}),
|
|
328
395
|
startSide,
|
|
329
|
-
getAnchorPoint(
|
|
330
|
-
|
|
396
|
+
getAnchorPoint(endRect, endSide, { ...item.endSlot, spacing, gap }),
|
|
397
|
+
endSide
|
|
331
398
|
);
|
|
332
399
|
}
|
|
333
400
|
}
|
|
@@ -359,27 +426,44 @@ export class Arclace {
|
|
|
359
426
|
}
|
|
360
427
|
}
|
|
361
428
|
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
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 {
|
|
367
439
|
if (this.#destroyed) {
|
|
368
|
-
throw
|
|
440
|
+
throw new ArclaceError('this instance has been destroyed');
|
|
369
441
|
}
|
|
370
442
|
|
|
443
|
+
validateConnectionOptions(options, name);
|
|
444
|
+
|
|
371
445
|
const startElement = resolveElement(startTarget, 'startElement');
|
|
372
|
-
const
|
|
373
|
-
const
|
|
446
|
+
const endElement = resolveElement(endTarget, 'endElement');
|
|
447
|
+
const valid = options as ArcConnectionOptionsType;
|
|
448
|
+
const id = valid.id ?? `arc-${uniqueID()}`;
|
|
374
449
|
|
|
375
450
|
if (this.#connections.has(id)) {
|
|
376
|
-
throw
|
|
451
|
+
throw new ArclaceError(`a connection with id "${id}" exists`);
|
|
377
452
|
}
|
|
378
453
|
|
|
379
|
-
|
|
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({
|
|
380
464
|
id,
|
|
381
465
|
startElement,
|
|
382
|
-
|
|
466
|
+
endElement,
|
|
383
467
|
options,
|
|
384
468
|
defaults: this.#options.defaults,
|
|
385
469
|
owner: this
|
|
@@ -388,7 +472,7 @@ export class Arclace {
|
|
|
388
472
|
this.#connections.set(id, connection);
|
|
389
473
|
this.svg.appendChild(connection.element);
|
|
390
474
|
this.#resizeObserver?.observe(startElement);
|
|
391
|
-
this.#resizeObserver?.observe(
|
|
475
|
+
this.#resizeObserver?.observe(endElement);
|
|
392
476
|
|
|
393
477
|
return connection;
|
|
394
478
|
}
|
|
@@ -398,7 +482,7 @@ export class Arclace {
|
|
|
398
482
|
|
|
399
483
|
const inUse = this.connections.some(
|
|
400
484
|
(connection) =>
|
|
401
|
-
connection.startElement === el || connection.
|
|
485
|
+
connection.startElement === el || connection.endElement === el
|
|
402
486
|
);
|
|
403
487
|
|
|
404
488
|
if (!inUse) this.#resizeObserver.unobserve(el);
|
|
@@ -469,8 +553,8 @@ export class Arclace {
|
|
|
469
553
|
*
|
|
470
554
|
* const arclace = createConnections(
|
|
471
555
|
* [
|
|
472
|
-
* { startElement: '#a',
|
|
473
|
-
* { startElement: '#b',
|
|
556
|
+
* { startElement: '#a', endElement: '#b' },
|
|
557
|
+
* { startElement: '#b', endElement: '#c', arrow: 'end' }
|
|
474
558
|
* ],
|
|
475
559
|
* { defaults: { type: 'orthogonal' } }
|
|
476
560
|
* );
|
|
@@ -482,7 +566,13 @@ export const createConnections = (
|
|
|
482
566
|
): Arclace => {
|
|
483
567
|
const arclace = new Arclace(options);
|
|
484
568
|
|
|
485
|
-
|
|
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
|
+
}
|
|
486
576
|
|
|
487
577
|
return arclace;
|
|
488
578
|
};
|
package/src/Connection.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Arclace } from './Arclace';
|
|
2
|
+
import { ArclaceError } from './errors';
|
|
2
3
|
import type {
|
|
3
4
|
ArcConnectionOptionsType,
|
|
4
5
|
ArcGeometryType,
|
|
@@ -14,29 +15,31 @@ import {
|
|
|
14
15
|
straightPath
|
|
15
16
|
} from './utils/paths';
|
|
16
17
|
import { createSVGElement, setAttributes } from './utils/svg';
|
|
18
|
+
import { validateConnectionOptions } from './utils/validate';
|
|
17
19
|
|
|
18
20
|
/**
|
|
19
|
-
* Connection options
|
|
20
|
-
|
|
21
|
-
export type ArcOptionalKeysType = 'id' | 'label' | 'color' | 'className';
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Connection options with every default filled in.
|
|
21
|
+
* Connection options with every default filled in. `label`, `color` and
|
|
22
|
+
* `className` have no default.
|
|
25
23
|
*/
|
|
26
24
|
export type ArcResolvedConnectionOptionsType = Required<
|
|
27
|
-
Omit<ArcConnectionOptionsType,
|
|
25
|
+
Omit<ArcConnectionOptionsType, 'id' | 'label' | 'color' | 'className'>
|
|
28
26
|
> &
|
|
29
|
-
Pick<ArcConnectionOptionsType,
|
|
27
|
+
Pick<ArcConnectionOptionsType, 'label' | 'color' | 'className'> & {
|
|
28
|
+
id: string;
|
|
29
|
+
};
|
|
30
30
|
|
|
31
31
|
/**
|
|
32
|
-
* Options every connection falls back to.
|
|
32
|
+
* Options every connection falls back to. Frozen: set your own defaults with
|
|
33
|
+
* the `defaults` option of {@link Arclace} instead.
|
|
33
34
|
*/
|
|
34
|
-
export const DEFAULT_CONNECTION_OPTIONS:
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
export const DEFAULT_CONNECTION_OPTIONS: Readonly<
|
|
36
|
+
Required<
|
|
37
|
+
Omit<ArcConnectionOptionsType, 'id' | 'label' | 'color' | 'className'>
|
|
38
|
+
>
|
|
39
|
+
> = Object.freeze({
|
|
37
40
|
type: 'bezier',
|
|
38
41
|
startSide: 'auto',
|
|
39
|
-
|
|
42
|
+
endSide: 'auto',
|
|
40
43
|
arrow: 'none',
|
|
41
44
|
strokeWidth: 2,
|
|
42
45
|
dashed: false,
|
|
@@ -48,22 +51,33 @@ export const DEFAULT_CONNECTION_OPTIONS: Required<
|
|
|
48
51
|
curvature: 1,
|
|
49
52
|
radius: 8,
|
|
50
53
|
offset: 20
|
|
51
|
-
};
|
|
54
|
+
});
|
|
52
55
|
|
|
56
|
+
/**
|
|
57
|
+
* Drops options set to `undefined`, so they fall back to the defaults instead
|
|
58
|
+
* of replacing them.
|
|
59
|
+
*/
|
|
60
|
+
const definedOnly = (
|
|
61
|
+
options: ArcConnectionOptionsType
|
|
62
|
+
): ArcConnectionOptionsType =>
|
|
63
|
+
Object.fromEntries(
|
|
64
|
+
Object.entries(options).filter(([, value]) => value !== undefined)
|
|
65
|
+
);
|
|
53
66
|
// Arrow length in multiples of the stroke width
|
|
54
67
|
const ARROW_SIZE = 4;
|
|
55
68
|
|
|
56
|
-
|
|
69
|
+
type ArcConnectionParamsType = {
|
|
57
70
|
id: string;
|
|
58
71
|
startElement: Element;
|
|
59
|
-
|
|
72
|
+
endElement: Element;
|
|
60
73
|
options: ArcConnectionOptionsType;
|
|
61
74
|
defaults?: ArcConnectionOptionsType;
|
|
62
75
|
owner: Arclace;
|
|
63
76
|
};
|
|
64
77
|
|
|
65
78
|
/**
|
|
66
|
-
* A line between two elements. Created with {@link Arclace.connect}
|
|
79
|
+
* A line between two elements. Created with {@link Arclace.connect} or
|
|
80
|
+
* {@link Arclace.connectAll}, not with `new`.
|
|
67
81
|
*
|
|
68
82
|
* Each connection renders as:
|
|
69
83
|
*
|
|
@@ -73,7 +87,7 @@ export type ArcConnectionParamsType = {
|
|
|
73
87
|
* <path class="arc-path" />
|
|
74
88
|
* <text class="arc-label">…</text> <!-- when `label` is set -->
|
|
75
89
|
* <circle class="arc-anchor arc-anchor-start" /> <!-- when `anchors` is set -->
|
|
76
|
-
* <circle class="arc-anchor arc-anchor-
|
|
90
|
+
* <circle class="arc-anchor arc-anchor-end" />
|
|
77
91
|
* </g>
|
|
78
92
|
* ```
|
|
79
93
|
*
|
|
@@ -83,7 +97,7 @@ export type ArcConnectionParamsType = {
|
|
|
83
97
|
export class Connection {
|
|
84
98
|
readonly id: string;
|
|
85
99
|
readonly startElement: Element;
|
|
86
|
-
readonly
|
|
100
|
+
readonly endElement: Element;
|
|
87
101
|
|
|
88
102
|
/**
|
|
89
103
|
* The `<g>` element holding everything this connection draws.
|
|
@@ -95,12 +109,8 @@ export class Connection {
|
|
|
95
109
|
*/
|
|
96
110
|
readonly path: SVGPathElement;
|
|
97
111
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
* hidden because one of its elements is not rendered.
|
|
101
|
-
*/
|
|
102
|
-
geometry: ArcGeometryType | null = null;
|
|
103
|
-
|
|
112
|
+
#geometry: ArcGeometryType | null = null;
|
|
113
|
+
#removed = false;
|
|
104
114
|
#owner: Arclace;
|
|
105
115
|
#options: ArcConnectionOptionsType;
|
|
106
116
|
#defaults: ArcConnectionOptionsType;
|
|
@@ -108,11 +118,11 @@ export class Connection {
|
|
|
108
118
|
#label: SVGTextElement | null = null;
|
|
109
119
|
#anchors: [SVGCircleElement, SVGCircleElement] | null = null;
|
|
110
120
|
|
|
111
|
-
constructor(params: ArcConnectionParamsType) {
|
|
121
|
+
private constructor(params: ArcConnectionParamsType) {
|
|
112
122
|
const {
|
|
113
123
|
id,
|
|
114
124
|
startElement,
|
|
115
|
-
|
|
125
|
+
endElement,
|
|
116
126
|
options,
|
|
117
127
|
defaults = {},
|
|
118
128
|
owner
|
|
@@ -120,7 +130,7 @@ export class Connection {
|
|
|
120
130
|
|
|
121
131
|
this.id = id;
|
|
122
132
|
this.startElement = startElement;
|
|
123
|
-
this.
|
|
133
|
+
this.endElement = endElement;
|
|
124
134
|
this.#owner = owner;
|
|
125
135
|
this.#options = { ...options };
|
|
126
136
|
this.#defaults = defaults;
|
|
@@ -134,14 +144,31 @@ export class Connection {
|
|
|
134
144
|
this.#applyOptions();
|
|
135
145
|
}
|
|
136
146
|
|
|
147
|
+
/**
|
|
148
|
+
* Creates a connection for its owner. Options must already be validated.
|
|
149
|
+
*
|
|
150
|
+
* @internal
|
|
151
|
+
*/
|
|
152
|
+
static _create(params: ArcConnectionParamsType): Connection {
|
|
153
|
+
return new Connection(params);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* The geometry from the last update, or `null` when the connection is
|
|
158
|
+
* hidden because one of its elements is not rendered.
|
|
159
|
+
*/
|
|
160
|
+
get geometry(): Readonly<ArcGeometryType> | null {
|
|
161
|
+
return this.#geometry;
|
|
162
|
+
}
|
|
163
|
+
|
|
137
164
|
/**
|
|
138
165
|
* The connection's options merged with the instance defaults.
|
|
139
166
|
*/
|
|
140
167
|
get options(): Readonly<ArcResolvedConnectionOptionsType> {
|
|
141
168
|
return {
|
|
142
169
|
...DEFAULT_CONNECTION_OPTIONS,
|
|
143
|
-
...this.#defaults,
|
|
144
|
-
...this.#options,
|
|
170
|
+
...definedOnly(this.#defaults),
|
|
171
|
+
...definedOnly(this.#options),
|
|
145
172
|
id: this.id
|
|
146
173
|
};
|
|
147
174
|
}
|
|
@@ -153,8 +180,21 @@ export class Connection {
|
|
|
153
180
|
* ```ts
|
|
154
181
|
* connection.setOptions({ type: 'orthogonal', arrow: 'end' });
|
|
155
182
|
* ```
|
|
183
|
+
* @throws {TypeError} When an option has the wrong type, or `id` is set.
|
|
184
|
+
* @throws {RangeError} When an option is out of range.
|
|
185
|
+
* @throws {ArclaceError} When the connection has been removed.
|
|
156
186
|
*/
|
|
157
187
|
setOptions(options: Omit<ArcConnectionOptionsType, 'id'>): this {
|
|
188
|
+
if (this.#removed) {
|
|
189
|
+
throw new ArclaceError(`connection "${this.id}" has been removed`);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
validateConnectionOptions(options);
|
|
193
|
+
|
|
194
|
+
if ((options as ArcConnectionOptionsType).id !== undefined) {
|
|
195
|
+
throw new TypeError('arclace: setOptions() cannot change the id');
|
|
196
|
+
}
|
|
197
|
+
|
|
158
198
|
this.#options = { ...this.#options, ...options };
|
|
159
199
|
this.#applyOptions();
|
|
160
200
|
this.#owner.update();
|
|
@@ -184,8 +224,8 @@ export class Connection {
|
|
|
184
224
|
_draw(
|
|
185
225
|
start: ArcPointInterface,
|
|
186
226
|
startSide: ArcSideType,
|
|
187
|
-
|
|
188
|
-
|
|
227
|
+
end: ArcPointInterface,
|
|
228
|
+
endSide: ArcSideType
|
|
189
229
|
): void {
|
|
190
230
|
const { arrow, strokeWidth } = this.options;
|
|
191
231
|
const arrowLength = ARROW_SIZE * strokeWidth;
|
|
@@ -193,25 +233,32 @@ export class Connection {
|
|
|
193
233
|
// elements
|
|
194
234
|
const pathStart =
|
|
195
235
|
arrow === 'start' || arrow === 'both'
|
|
196
|
-
? this.#pullBack(start, startSide,
|
|
236
|
+
? this.#pullBack(start, startSide, end, arrowLength)
|
|
197
237
|
: start;
|
|
198
|
-
const
|
|
238
|
+
const pathEnd =
|
|
199
239
|
arrow === 'end' || arrow === 'both'
|
|
200
|
-
? this.#pullBack(
|
|
201
|
-
:
|
|
240
|
+
? this.#pullBack(end, endSide, start, arrowLength)
|
|
241
|
+
: end;
|
|
202
242
|
const { d, labelPoint } = this.#buildPath(
|
|
203
243
|
pathStart,
|
|
204
244
|
startSide,
|
|
205
|
-
|
|
206
|
-
|
|
245
|
+
pathEnd,
|
|
246
|
+
endSide
|
|
207
247
|
);
|
|
208
248
|
|
|
209
|
-
this
|
|
249
|
+
this.#geometry = Object.freeze({
|
|
250
|
+
start,
|
|
251
|
+
end,
|
|
252
|
+
startSide,
|
|
253
|
+
endSide,
|
|
254
|
+
labelPoint,
|
|
255
|
+
d
|
|
256
|
+
});
|
|
210
257
|
|
|
211
258
|
this.element.removeAttribute('display');
|
|
212
259
|
setAttributes(this.element, {
|
|
213
260
|
'data-start-side': startSide,
|
|
214
|
-
'data-
|
|
261
|
+
'data-end-side': endSide
|
|
215
262
|
});
|
|
216
263
|
this.path.setAttribute('d', d);
|
|
217
264
|
|
|
@@ -219,7 +266,7 @@ export class Connection {
|
|
|
219
266
|
|
|
220
267
|
if (this.#anchors) {
|
|
221
268
|
setAttributes(this.#anchors[0], { cx: start.x, cy: start.y });
|
|
222
|
-
setAttributes(this.#anchors[1], { cx:
|
|
269
|
+
setAttributes(this.#anchors[1], { cx: end.x, cy: end.y });
|
|
223
270
|
}
|
|
224
271
|
}
|
|
225
272
|
|
|
@@ -229,7 +276,7 @@ export class Connection {
|
|
|
229
276
|
* @internal
|
|
230
277
|
*/
|
|
231
278
|
_hide(): void {
|
|
232
|
-
this
|
|
279
|
+
this.#geometry = null;
|
|
233
280
|
this.element.setAttribute('display', 'none');
|
|
234
281
|
}
|
|
235
282
|
|
|
@@ -239,27 +286,28 @@ export class Connection {
|
|
|
239
286
|
* @internal
|
|
240
287
|
*/
|
|
241
288
|
_dispose(): void {
|
|
289
|
+
this.#removed = true;
|
|
242
290
|
this.element.remove();
|
|
243
291
|
}
|
|
244
292
|
|
|
245
293
|
#buildPath(
|
|
246
294
|
start: ArcPointInterface,
|
|
247
295
|
startSide: ArcSideType,
|
|
248
|
-
|
|
249
|
-
|
|
296
|
+
end: ArcPointInterface,
|
|
297
|
+
endSide: ArcSideType
|
|
250
298
|
): ArcPathResultType {
|
|
251
299
|
const { type, curvature, radius, offset } = this.options;
|
|
252
300
|
|
|
253
|
-
if (type === 'straight') return straightPath(start,
|
|
301
|
+
if (type === 'straight') return straightPath(start, end);
|
|
254
302
|
|
|
255
303
|
if (type === 'orthogonal') {
|
|
256
|
-
return orthogonalPath(start, startSide,
|
|
304
|
+
return orthogonalPath(start, startSide, end, endSide, {
|
|
257
305
|
radius,
|
|
258
306
|
offset
|
|
259
307
|
});
|
|
260
308
|
}
|
|
261
309
|
|
|
262
|
-
return bezierPath(start, startSide,
|
|
310
|
+
return bezierPath(start, startSide, end, endSide, curvature);
|
|
263
311
|
}
|
|
264
312
|
|
|
265
313
|
#pullBack(
|
|
@@ -377,8 +425,8 @@ export class Connection {
|
|
|
377
425
|
});
|
|
378
426
|
this.path.after(this.#label);
|
|
379
427
|
|
|
380
|
-
if (this
|
|
381
|
-
setAttributes(this.#label, { ...this
|
|
428
|
+
if (this.#geometry)
|
|
429
|
+
setAttributes(this.#label, { ...this.#geometry.labelPoint });
|
|
382
430
|
}
|
|
383
431
|
|
|
384
432
|
this.#label.textContent = label;
|
|
@@ -395,7 +443,7 @@ export class Connection {
|
|
|
395
443
|
if (!this.#anchors) {
|
|
396
444
|
this.#anchors = [
|
|
397
445
|
createSVGElement('circle', { class: 'arc-anchor arc-anchor-start' }),
|
|
398
|
-
createSVGElement('circle', { class: 'arc-anchor arc-anchor-
|
|
446
|
+
createSVGElement('circle', { class: 'arc-anchor arc-anchor-end' })
|
|
399
447
|
];
|
|
400
448
|
this.element.append(...this.#anchors);
|
|
401
449
|
}
|