@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.
@@ -0,0 +1,839 @@
1
+ var _t = Object.defineProperty;
2
+ var mt = (e) => {
3
+ throw TypeError(e);
4
+ };
5
+ var Rt = (e, t, n) => t in e ? _t(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
6
+ var z = (e, t, n) => Rt(e, typeof t != "symbol" ? t + "" : t, n), et = (e, t, n) => t.has(e) || mt("Cannot " + n);
7
+ var r = (e, t, n) => (et(e, t, "read from private field"), n ? n.call(e) : t.get(e)), p = (e, t, n) => t.has(e) ? mt("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(e) : t.set(e, n), l = (e, t, n, s) => (et(e, t, "write to private field"), s ? s.call(e, n) : t.set(e, n), n), y = (e, t, n) => (et(e, t, "access private method"), n);
8
+ class N extends Error {
9
+ constructor(n, s) {
10
+ super(`arclace: ${n}`, s);
11
+ z(this, "name", "ArclaceError");
12
+ }
13
+ }
14
+ const B = Object.freeze({
15
+ top: Object.freeze({ x: 0, y: -1 }),
16
+ right: Object.freeze({ x: 1, y: 0 }),
17
+ bottom: Object.freeze({ x: 0, y: 1 }),
18
+ left: Object.freeze({ x: -1, y: 0 })
19
+ }), At = Object.freeze({
20
+ top: "bottom",
21
+ right: "left",
22
+ bottom: "top",
23
+ left: "right"
24
+ }), it = (e) => e === "left" || e === "right", J = (e) => ({
25
+ x: e.left + e.width / 2,
26
+ y: e.top + e.height / 2
27
+ }), q = (e, t, n = 1) => ({ x: e.x + t.x * n, y: e.y + t.y * n }), I = (e, t) => Math.hypot(t.x - e.x, t.y - e.y), bt = (e) => Math.round(e * 100) / 100, O = (e) => `${bt(e.x)} ${bt(e.y)}`, jt = () => "_" + Math.random().toString(36).substring(2, 11), ct = Object.freeze([
28
+ "top",
29
+ "right",
30
+ "bottom",
31
+ "left"
32
+ ]), xt = Object.freeze([
33
+ ...ct,
34
+ "auto"
35
+ ]), Lt = Object.freeze([
36
+ "bezier",
37
+ "straight",
38
+ "orthogonal"
39
+ ]), It = Object.freeze([
40
+ "none",
41
+ "start",
42
+ "end",
43
+ "both"
44
+ ]), C = (e) => typeof e == "string" ? JSON.stringify(e) : e == null || typeof e == "number" || typeof e == "boolean" ? String(e) : Array.isArray(e) ? "an array" : typeof e == "object" ? "an object" : `a ${typeof e}`, Z = (e, t) => {
45
+ if (typeof e != "object" || e === null || Array.isArray(e))
46
+ throw new TypeError(
47
+ `arclace: ${t} must be an object, not ${C(e)}`
48
+ );
49
+ }, F = (e, t, n) => {
50
+ const s = t.map((o) => `"${o}"`).join(", ");
51
+ if (typeof e != "string")
52
+ throw new TypeError(
53
+ `arclace: ${n} must be one of ${s}, not ${C(e)}`
54
+ );
55
+ if (!t.includes(e))
56
+ throw new RangeError(
57
+ `arclace: ${n} must be one of ${s}, not ${C(e)}`
58
+ );
59
+ return e;
60
+ }, A = (e, t, n) => {
61
+ if (typeof e != "number" || !Number.isFinite(e))
62
+ throw new TypeError(
63
+ `arclace: ${t} must be a finite number, not ${C(e)}`
64
+ );
65
+ if (n !== void 0 && e < n)
66
+ throw new RangeError(
67
+ `arclace: ${t} must be ${n} or more, not ${C(e)}`
68
+ );
69
+ return e;
70
+ }, G = (e, t) => {
71
+ if (typeof e != "boolean")
72
+ throw new TypeError(
73
+ `arclace: ${t} must be true or false, not ${C(e)}`
74
+ );
75
+ return e;
76
+ }, Y = (e, t, n = !1) => {
77
+ if (typeof e != "string" || n && e === "")
78
+ throw new TypeError(
79
+ `arclace: ${t} must be a ${n ? "non-empty " : ""}string, not ${C(e)}`
80
+ );
81
+ return e;
82
+ }, K = (e, t) => {
83
+ Z(e, t);
84
+ const { x: n, y: s } = e;
85
+ return A(n, `${t}.x`), A(s, `${t}.y`), e;
86
+ }, Ft = Object.freeze({
87
+ id: (e, t) => Y(e, t, !0),
88
+ type: (e, t) => F(e, Lt, t),
89
+ startSide: (e, t) => F(e, xt, t),
90
+ endSide: (e, t) => F(e, xt, t),
91
+ arrow: (e, t) => F(e, It, t),
92
+ label: Y,
93
+ color: Y,
94
+ className: Y,
95
+ strokeWidth: (e, t) => A(e, t, 0),
96
+ dashed: G,
97
+ animated: G,
98
+ anchors: G,
99
+ gap: (e, t) => A(e, t),
100
+ spread: G,
101
+ spacing: (e, t) => A(e, t, 0),
102
+ curvature: (e, t) => A(e, t),
103
+ radius: (e, t) => A(e, t, 0),
104
+ offset: (e, t) => A(e, t, 0)
105
+ }), dt = (e, t = "options") => {
106
+ Z(e, t);
107
+ for (const [n, s] of Object.entries(Ft)) {
108
+ const o = Object.hasOwn(e, n) ? e[n] : void 0;
109
+ o !== void 0 && s(o, `${t}.${n}`);
110
+ }
111
+ return e;
112
+ }, Wt = (e) => {
113
+ Z(e, "options");
114
+ const { autoUpdate: t, className: n, defaults: s } = e;
115
+ if (t !== void 0 && typeof t != "boolean" && t !== "frame")
116
+ throw new RangeError(
117
+ `arclace: options.autoUpdate must be true, false or "frame", not ${C(t)}`
118
+ );
119
+ if (n !== void 0 && Y(n, "options.className"), s !== void 0 && (dt(s, "options.defaults"), s.id !== void 0))
120
+ throw new TypeError(
121
+ "arclace: options.defaults cannot set an id; ids belong to single connections"
122
+ );
123
+ return e;
124
+ }, Dt = (e, t = 1e3) => Math.min(e / t, 1), Ut = (e, t, n) => t + (n - t) * e, Bt = (e, t = 1.1, n = 3.5, s = 1e3) => Ut(Dt(e, s), t, n), St = (e, t, n, s) => {
125
+ K(e, "start"), F(t, ct, "startSide"), K(n, "end"), F(s, ct, "endSide");
126
+ }, qt = (e, t) => (K(e, "start"), K(t, "end"), {
127
+ d: `M ${O(e)} L ${O(t)}`,
128
+ labelPoint: { x: (e.x + t.x) / 2, y: (e.y + t.y) / 2 }
129
+ }), Yt = (e, t, n, s, o = 1) => {
130
+ St(e, t, n, s), A(o, "curvature");
131
+ const i = I(e, n), c = i / Bt(i) * o, a = q(e, B[t], c), h = q(n, B[s], c);
132
+ return {
133
+ d: `M ${O(e)} C ${O(a)}, ${O(h)}, ${O(n)}`,
134
+ // Point on the curve at t = 0.5
135
+ labelPoint: {
136
+ x: (e.x + 3 * a.x + 3 * h.x + n.x) / 8,
137
+ y: (e.y + 3 * a.y + 3 * h.y + n.y) / 8
138
+ }
139
+ };
140
+ }, Xt = (e) => {
141
+ const t = e.filter(
142
+ (n, s) => s === 0 || I(n, e[s - 1]) > 0.01
143
+ );
144
+ return t.filter((n, s) => {
145
+ if (s === 0 || s === t.length - 1) return !0;
146
+ const o = t[s - 1], i = t[s + 1], c = (n.x - o.x) * (i.y - o.y) - (n.y - o.y) * (i.x - o.x);
147
+ return Math.abs(c) > 0.01;
148
+ });
149
+ }, Ht = (e, t) => {
150
+ if (e.length === 0) return "";
151
+ const n = (o, i, c) => {
152
+ const a = I(o, i);
153
+ return {
154
+ x: o.x + (i.x - o.x) / a * c,
155
+ y: o.y + (i.y - o.y) / a * c
156
+ };
157
+ }, s = [`M ${O(e[0])}`];
158
+ for (let o = 1; o < e.length - 1; o++) {
159
+ const i = e[o - 1], c = e[o], a = e[o + 1], h = Math.min(
160
+ t,
161
+ I(i, c) / 2,
162
+ I(c, a) / 2
163
+ );
164
+ if (h <= 0) {
165
+ s.push(`L ${O(c)}`);
166
+ continue;
167
+ }
168
+ s.push(
169
+ `L ${O(n(c, i, h))}`,
170
+ `Q ${O(c)}, ${O(n(c, a, h))}`
171
+ );
172
+ }
173
+ return e.length > 1 && s.push(`L ${O(e[e.length - 1])}`), s.join(" ");
174
+ }, Gt = (e) => {
175
+ const t = e.slice(1).map((s, o) => I(e[o], s));
176
+ let n = t.reduce((s, o) => s + o, 0) / 2;
177
+ for (let s = 0; s < t.length; s++) {
178
+ if (n <= t[s] && t[s] > 0) {
179
+ const o = n / t[s];
180
+ return {
181
+ x: e[s].x + (e[s + 1].x - e[s].x) * o,
182
+ y: e[s].y + (e[s + 1].y - e[s].y) * o
183
+ };
184
+ }
185
+ n -= t[s];
186
+ }
187
+ return { ...e[0] };
188
+ }, gt = Object.freeze({
189
+ top: "left",
190
+ bottom: "right",
191
+ left: "top",
192
+ right: "bottom"
193
+ }), nt = (e) => ({
194
+ x: e.y,
195
+ y: e.x
196
+ }), kt = (e, t, n, s, o = 20) => {
197
+ if (!it(t))
198
+ return kt(
199
+ nt(e),
200
+ gt[t],
201
+ nt(n),
202
+ gt[s],
203
+ o
204
+ ).map(nt);
205
+ const i = B[t], c = B[s], a = q(e, i, o), h = q(n, c, o);
206
+ if (it(s)) {
207
+ if (s === At[t]) {
208
+ if ((h.x - a.x) * i.x >= 0) {
209
+ const k = (e.x + n.x) / 2;
210
+ return [e, { x: k, y: e.y }, { x: k, y: n.y }, n];
211
+ }
212
+ const u = (a.y + h.y) / 2;
213
+ return [e, a, { x: a.x, y: u }, { x: h.x, y: u }, h, n];
214
+ }
215
+ const d = i.x > 0 ? Math.max(a.x, h.x) : Math.min(a.x, h.x);
216
+ return [e, { x: d, y: e.y }, { x: d, y: n.y }, n];
217
+ }
218
+ const f = { x: n.x, y: e.y };
219
+ return (f.x - e.x) * i.x > 0 && (f.y - n.y) * c.y > 0 ? [e, f, n] : [e, a, { x: a.x, y: h.y }, h, n];
220
+ }, Vt = (e, t, n, s, { radius: o = 8, offset: i = 20 } = {}) => {
221
+ St(e, t, n, s), A(o, "radius", 0), A(i, "offset", 0);
222
+ const c = Xt(
223
+ kt(e, t, n, s, i)
224
+ );
225
+ return {
226
+ d: Ht(c, o),
227
+ labelPoint: Gt(c)
228
+ };
229
+ }, Jt = "http://www.w3.org/2000/svg", M = (e, t = {}) => {
230
+ const n = document.createElementNS(Jt, e);
231
+ return $(n, t), n;
232
+ }, $ = (e, t) => {
233
+ for (const [n, s] of Object.entries(t))
234
+ s == null ? e.removeAttribute(n) : e.setAttribute(n, String(s));
235
+ }, Kt = Object.freeze({
236
+ type: "bezier",
237
+ startSide: "auto",
238
+ endSide: "auto",
239
+ arrow: "none",
240
+ strokeWidth: 2,
241
+ dashed: !1,
242
+ animated: !1,
243
+ anchors: !1,
244
+ gap: 0,
245
+ spread: !0,
246
+ spacing: 16,
247
+ curvature: 1,
248
+ radius: 8,
249
+ offset: 20
250
+ }), wt = (e) => Object.fromEntries(
251
+ Object.entries(e).filter(([, t]) => t !== void 0)
252
+ ), st = 4;
253
+ var P, X, _, R, H, j, w, E, m, zt, at, ht, Mt, Pt, Tt;
254
+ const pt = class pt {
255
+ constructor(t) {
256
+ p(this, m);
257
+ z(this, "id");
258
+ z(this, "startElement");
259
+ z(this, "endElement");
260
+ /**
261
+ * The `<g>` element holding everything this connection draws.
262
+ */
263
+ z(this, "element");
264
+ /**
265
+ * The `<path>` element of the line.
266
+ */
267
+ z(this, "path");
268
+ p(this, P, null);
269
+ p(this, X, !1);
270
+ p(this, _);
271
+ p(this, R);
272
+ p(this, H);
273
+ p(this, j, null);
274
+ p(this, w, null);
275
+ p(this, E, null);
276
+ const {
277
+ id: n,
278
+ startElement: s,
279
+ endElement: o,
280
+ options: i,
281
+ defaults: c = {},
282
+ owner: a
283
+ } = t;
284
+ this.id = n, this.startElement = s, this.endElement = o, l(this, _, a), l(this, R, { ...i }), l(this, H, c), this.element = M("g", { "data-arc-id": n }), this.path = M("path", {
285
+ class: "arc-path",
286
+ fill: "none",
287
+ stroke: "currentColor"
288
+ }), this.element.appendChild(this.path), y(this, m, ht).call(this);
289
+ }
290
+ /**
291
+ * Creates a connection for its owner. Options must already be validated.
292
+ *
293
+ * @internal
294
+ */
295
+ static _create(t) {
296
+ return new pt(t);
297
+ }
298
+ /**
299
+ * The geometry from the last update, or `null` when the connection is
300
+ * hidden because one of its elements is not rendered.
301
+ */
302
+ get geometry() {
303
+ return r(this, P);
304
+ }
305
+ /**
306
+ * The connection's options merged with the instance defaults.
307
+ */
308
+ get options() {
309
+ return {
310
+ ...Kt,
311
+ ...wt(r(this, H)),
312
+ ...wt(r(this, R)),
313
+ id: this.id
314
+ };
315
+ }
316
+ /**
317
+ * Changes options and redraws.
318
+ *
319
+ * @example
320
+ * ```ts
321
+ * connection.setOptions({ type: 'orthogonal', arrow: 'end' });
322
+ * ```
323
+ * @throws {TypeError} When an option has the wrong type, or `id` is set.
324
+ * @throws {RangeError} When an option is out of range.
325
+ * @throws {ArclaceError} When the connection has been removed.
326
+ */
327
+ setOptions(t) {
328
+ if (r(this, X))
329
+ throw new N(`connection "${this.id}" has been removed`);
330
+ if (dt(t), t.id !== void 0)
331
+ throw new TypeError("arclace: setOptions() cannot change the id");
332
+ return l(this, R, { ...r(this, R), ...t }), y(this, m, ht).call(this), r(this, _).update(), this;
333
+ }
334
+ /**
335
+ * Redraws every connection of the owning instance.
336
+ */
337
+ update() {
338
+ r(this, _).update();
339
+ }
340
+ /**
341
+ * Removes this connection.
342
+ */
343
+ remove() {
344
+ r(this, _).disconnect(this);
345
+ }
346
+ /**
347
+ * Draws the connection between two attachment points.
348
+ *
349
+ * @internal
350
+ */
351
+ _draw(t, n, s, o) {
352
+ const { arrow: i, strokeWidth: c } = this.options, a = st * c, h = i === "start" || i === "both" ? y(this, m, at).call(this, t, n, s, a) : t, f = i === "end" || i === "both" ? y(this, m, at).call(this, s, o, t, a) : s, { d, labelPoint: u } = y(this, m, zt).call(this, h, n, f, o);
353
+ l(this, P, Object.freeze({
354
+ start: t,
355
+ end: s,
356
+ startSide: n,
357
+ endSide: o,
358
+ labelPoint: u,
359
+ d
360
+ })), this.element.removeAttribute("display"), $(this.element, {
361
+ "data-start-side": n,
362
+ "data-end-side": o
363
+ }), this.path.setAttribute("d", d), r(this, w) && $(r(this, w), { ...u }), r(this, E) && ($(r(this, E)[0], { cx: t.x, cy: t.y }), $(r(this, E)[1], { cx: s.x, cy: s.y }));
364
+ }
365
+ /**
366
+ * Hides the connection.
367
+ *
368
+ * @internal
369
+ */
370
+ _hide() {
371
+ l(this, P, null), this.element.setAttribute("display", "none");
372
+ }
373
+ /**
374
+ * Removes the connection's elements from the overlay.
375
+ *
376
+ * @internal
377
+ */
378
+ _dispose() {
379
+ l(this, X, !0), this.element.remove();
380
+ }
381
+ };
382
+ P = new WeakMap(), X = new WeakMap(), _ = new WeakMap(), R = new WeakMap(), H = new WeakMap(), j = new WeakMap(), w = new WeakMap(), E = new WeakMap(), m = new WeakSet(), zt = function(t, n, s, o) {
383
+ const { type: i, curvature: c, radius: a, offset: h } = this.options;
384
+ return i === "straight" ? qt(t, s) : i === "orthogonal" ? Vt(t, n, s, o, {
385
+ radius: a,
386
+ offset: h
387
+ }) : Yt(t, n, s, o, c);
388
+ }, at = function(t, n, s, o) {
389
+ const i = I(t, s), c = Math.min(o, i / 3);
390
+ return this.options.type !== "straight" ? q(t, B[n], c) : i === 0 ? t : {
391
+ x: t.x + (s.x - t.x) / i * c,
392
+ y: t.y + (s.y - t.y) / i * c
393
+ };
394
+ }, ht = function() {
395
+ const {
396
+ type: t,
397
+ arrow: n,
398
+ label: s,
399
+ color: o,
400
+ strokeWidth: i,
401
+ dashed: c,
402
+ animated: a,
403
+ anchors: h,
404
+ className: f
405
+ } = this.options, d = ["arc-connection", `arc-${t}`];
406
+ (c || a) && d.push("arc-dashed"), a && d.push("arc-animated"), f && d.push(...f.split(/\s+/).filter(Boolean)), $(this.element, {
407
+ class: d.join(" "),
408
+ "data-type": t
409
+ }), this.element.style.color = o ?? "", $(this.path, {
410
+ "stroke-width": i,
411
+ "stroke-dasharray": c || a ? "6 6" : null
412
+ }), y(this, m, Mt).call(this, n), y(this, m, Pt).call(this, s), y(this, m, Tt).call(this, h, i);
413
+ }, Mt = function(t) {
414
+ var s;
415
+ if (t === "none") {
416
+ (s = r(this, j)) == null || s.remove(), l(this, j, null), $(this.path, { "marker-start": null, "marker-end": null });
417
+ return;
418
+ }
419
+ r(this, j) || (l(this, j, M("marker", {
420
+ id: `arc-arrow-${jt()}`,
421
+ class: "arc-marker",
422
+ viewBox: "0 0 10 10",
423
+ refX: 0,
424
+ refY: 5,
425
+ markerWidth: st,
426
+ markerHeight: st,
427
+ markerUnits: "strokeWidth",
428
+ orient: "auto-start-reverse"
429
+ })), r(this, j).appendChild(
430
+ M("path", {
431
+ d: "M 0 0 L 10 5 L 0 10 z",
432
+ fill: "currentColor"
433
+ })
434
+ ), this.element.insertBefore(r(this, j), this.path));
435
+ const n = `url(#${r(this, j).id})`;
436
+ $(this.path, {
437
+ "marker-start": t === "start" || t === "both" ? n : null,
438
+ "marker-end": t === "end" || t === "both" ? n : null
439
+ });
440
+ }, Pt = function(t) {
441
+ var n;
442
+ if (!t) {
443
+ (n = r(this, w)) == null || n.remove(), l(this, w, null);
444
+ return;
445
+ }
446
+ r(this, w) || (l(this, w, M("text", {
447
+ class: "arc-label",
448
+ fill: "currentColor",
449
+ "text-anchor": "middle",
450
+ "dominant-baseline": "central"
451
+ })), this.path.after(r(this, w)), r(this, P) && $(r(this, w), { ...r(this, P).labelPoint })), r(this, w).textContent = t;
452
+ }, Tt = function(t, n) {
453
+ var s;
454
+ if (!t) {
455
+ (s = r(this, E)) == null || s.forEach((o) => o.remove()), l(this, E, null);
456
+ return;
457
+ }
458
+ r(this, E) || (l(this, E, [
459
+ M("circle", { class: "arc-anchor arc-anchor-start" }),
460
+ M("circle", { class: "arc-anchor arc-anchor-end" })
461
+ ]), this.element.append(...r(this, E))), r(this, E).forEach(
462
+ (o) => $(o, { r: n + 2, fill: "currentColor" })
463
+ );
464
+ };
465
+ let Q = pt;
466
+ const Qt = (e) => {
467
+ const t = e.getBoundingClientRect(), { clientWidth: n, clientHeight: s } = e;
468
+ return {
469
+ left: t.left,
470
+ top: t.top,
471
+ scaleX: n > 0 && t.width > 0 ? t.width / n : 1,
472
+ scaleY: s > 0 && t.height > 0 ? t.height / s : 1
473
+ };
474
+ }, Et = (e, t) => {
475
+ const n = e.getBoundingClientRect(), s = (n.left - t.left) / t.scaleX, o = (n.top - t.top) / t.scaleY, i = n.width / t.scaleX, c = n.height / t.scaleY;
476
+ return {
477
+ left: s,
478
+ top: o,
479
+ width: i,
480
+ height: c,
481
+ right: s + i,
482
+ bottom: o + c
483
+ };
484
+ }, Ot = (e) => e.isConnected && e.getClientRects().length > 0, Ct = (e, t = "element") => {
485
+ try {
486
+ return document.querySelector(e);
487
+ } catch (n) {
488
+ throw new N(`${t} "${e}" is not a valid selector`, {
489
+ cause: n
490
+ });
491
+ }
492
+ }, V = (e, t = "element") => {
493
+ if (typeof e == "string") {
494
+ const n = Ct(e, t);
495
+ if (!n) throw new N(`no ${t} matches "${e}"`);
496
+ return n;
497
+ }
498
+ if (!(e instanceof Element))
499
+ throw new TypeError(
500
+ `arclace: ${t} must be an Element or a selector string`
501
+ );
502
+ return e;
503
+ }, $t = (e, t) => {
504
+ const n = J(e), s = J(t), o = s.x - n.x, i = s.y - n.y, c = Math.max(t.left - e.right, e.left - t.right), a = Math.max(t.top - e.bottom, e.top - t.bottom);
505
+ return (c === a ? Math.abs(o) >= Math.abs(i) : c > a) ? o >= 0 ? "right" : "left" : i >= 0 ? "bottom" : "top";
506
+ }, Zt = (e, t, n = "auto", s = "auto") => ({
507
+ startSide: n === "auto" ? $t(e, t) : n,
508
+ endSide: s === "auto" ? At[$t(e, t)] : s
509
+ }), vt = (e, t, {
510
+ index: n = 0,
511
+ count: s = 1,
512
+ spacing: o = 16,
513
+ gap: i = 0
514
+ } = {}) => {
515
+ const c = t === "top" || t === "bottom" ? e.width : e.height, a = s > 1 ? Math.min(o, c / s) : 0, h = (n - (s - 1) / 2) * a, { x: f, y: d } = J(e);
516
+ let u;
517
+ switch (t) {
518
+ case "top":
519
+ u = { x: f + h, y: e.top };
520
+ break;
521
+ case "bottom":
522
+ u = { x: f + h, y: e.bottom };
523
+ break;
524
+ case "left":
525
+ u = { x: e.left, y: d + h };
526
+ break;
527
+ case "right":
528
+ u = { x: e.right, y: d + h };
529
+ break;
530
+ }
531
+ return q(u, B[t], i);
532
+ }, ot = (e) => typeof requestAnimationFrame == "function" ? requestAnimationFrame(e) : setTimeout(e, 16), rt = (e) => {
533
+ typeof cancelAnimationFrame == "function" ? cancelAnimationFrame(e) : clearTimeout(e);
534
+ };
535
+ var W, b, v, L, T, x, D, U, g, lt, ut, ft, S, Nt;
536
+ class te {
537
+ /**
538
+ * @throws {TypeError} When an option has the wrong type.
539
+ * @throws {RangeError} When an option is out of range.
540
+ * @throws {ArclaceError} When there is no DOM, or the container can't be
541
+ * found.
542
+ */
543
+ constructor(t = {}) {
544
+ p(this, g);
545
+ /**
546
+ * The element the overlay was added to.
547
+ */
548
+ z(this, "container");
549
+ /**
550
+ * The SVG overlay all lines are drawn in.
551
+ */
552
+ z(this, "svg");
553
+ p(this, W);
554
+ p(this, b, /* @__PURE__ */ new Map());
555
+ p(this, v, 0);
556
+ p(this, L, 0);
557
+ p(this, T, !1);
558
+ p(this, x, null);
559
+ p(this, D, null);
560
+ p(this, U, null);
561
+ p(this, S, () => {
562
+ r(this, v) || r(this, T) || l(this, v, ot(() => {
563
+ l(this, v, 0), this.update();
564
+ }));
565
+ });
566
+ if (typeof document > "u")
567
+ throw new N("a DOM is required");
568
+ l(this, W, Wt(t)), this.container = t.container ? V(t.container, "container") : document.body, this.container !== document.body && getComputedStyle(this.container).position === "static" && (l(this, U, this.container.style.position), this.container.style.position = "relative"), this.svg = M("svg", {
569
+ class: ["arc-svg", t.className].filter(Boolean).join(" "),
570
+ "aria-hidden": "true"
571
+ }), Object.assign(this.svg.style, {
572
+ position: "absolute",
573
+ top: "0",
574
+ left: "0",
575
+ width: "100%",
576
+ height: "100%",
577
+ overflow: "visible",
578
+ pointerEvents: "none"
579
+ }), this.container.prepend(this.svg), y(this, g, Nt).call(this);
580
+ }
581
+ /**
582
+ * All current connections, in the order they were made.
583
+ */
584
+ get connections() {
585
+ return [...r(this, b).values()];
586
+ }
587
+ /**
588
+ * Connects two elements and draws the line right away.
589
+ *
590
+ * @param startElement - The element (or selector) the line starts at.
591
+ * @param endElement - The element (or selector) the line ends at.
592
+ * @param options - Options for this connection, merged over the instance defaults.
593
+ * @throws {TypeError} When an element or an option has the wrong type.
594
+ * @throws {RangeError} When an option is out of range.
595
+ * @throws {ArclaceError} When an element can't be found, the id is taken
596
+ * or the instance has been destroyed. Nothing is added.
597
+ * @example
598
+ * ```ts
599
+ * const connection = arclace.connect(a, b, { type: 'orthogonal', arrow: 'end' });
600
+ * ```
601
+ */
602
+ connect(t, n, s = {}) {
603
+ const o = y(this, g, ut).call(this, y(this, g, lt).call(this, t, n, s, "options"));
604
+ return this.update(), o;
605
+ }
606
+ /**
607
+ * Connects many pairs of elements and draws them in one go. Every
608
+ * definition is checked first, so either all of them are added or none.
609
+ *
610
+ * @throws {TypeError} When `definitions` isn't an array, or an element or
611
+ * an option has the wrong type.
612
+ * @throws {RangeError} When an option is out of range.
613
+ * @throws {ArclaceError} When an element can't be found, an id is taken or
614
+ * repeated, or the instance has been destroyed.
615
+ * @example
616
+ * ```ts
617
+ * arclace.connectAll([
618
+ * { startElement: '#a', endElement: '#b' },
619
+ * { startElement: '#a', endElement: '#c', dashed: true }
620
+ * ]);
621
+ * ```
622
+ */
623
+ connectAll(t) {
624
+ if (!Array.isArray(t))
625
+ throw new TypeError(
626
+ "arclace: connectAll() needs an array of definitions"
627
+ );
628
+ const n = /* @__PURE__ */ new Set(), o = t.map((i, c) => {
629
+ const a = `definitions[${c}]`;
630
+ Z(i, a);
631
+ const { startElement: h, endElement: f, ...d } = i, u = y(this, g, lt).call(this, h, f, d, a);
632
+ if (n.has(u.id))
633
+ throw new N(
634
+ `${a}.id "${u.id}" is used more than once`
635
+ );
636
+ return n.add(u.id), u;
637
+ }).map((i) => y(this, g, ut).call(this, i));
638
+ return this.update(), o;
639
+ }
640
+ /**
641
+ * Returns the connection with the given id.
642
+ */
643
+ get(t) {
644
+ return r(this, b).get(t);
645
+ }
646
+ /**
647
+ * Returns every connection that starts or ends at an element. A selector
648
+ * that matches nothing returns an empty array.
649
+ *
650
+ * @throws {TypeError} When the target is neither an element nor a string.
651
+ * @throws {ArclaceError} When the selector is invalid.
652
+ */
653
+ find(t) {
654
+ const n = typeof t == "string" ? Ct(t) : V(t);
655
+ return n ? this.connections.filter(
656
+ (s) => s.startElement === n || s.endElement === n
657
+ ) : [];
658
+ }
659
+ /**
660
+ * Removes connections.
661
+ *
662
+ * @param target - A connection, a connection id, or an element (or
663
+ * selector) whose connections should all be removed. Targets that match
664
+ * nothing, and `null` or `undefined`, are ignored.
665
+ * @throws {TypeError} When the target is another kind of value.
666
+ * @throws {ArclaceError} When a string is neither an id nor a valid
667
+ * selector.
668
+ * @example
669
+ * ```ts
670
+ * arclace.disconnect(connection);
671
+ * arclace.disconnect('my-connection-id');
672
+ * arclace.disconnect(document.querySelector('#a'));
673
+ * ```
674
+ */
675
+ disconnect(t) {
676
+ let n;
677
+ if (t != null) {
678
+ if (t instanceof Q)
679
+ n = [t];
680
+ else if (typeof t == "string" && r(this, b).has(t))
681
+ n = [r(this, b).get(t)];
682
+ else if (typeof t == "string" || t instanceof Element)
683
+ n = this.find(t);
684
+ else
685
+ throw new TypeError(
686
+ "arclace: disconnect() needs a connection, an id, an element or a selector"
687
+ );
688
+ for (const s of n)
689
+ r(this, b).get(s.id) === s && (r(this, b).delete(s.id), s._dispose(), y(this, g, ft).call(this, s.startElement), y(this, g, ft).call(this, s.endElement));
690
+ n.length > 0 && this.update();
691
+ }
692
+ }
693
+ /**
694
+ * Removes every connection.
695
+ */
696
+ clear() {
697
+ var t;
698
+ for (const n of r(this, b).values())
699
+ n._dispose();
700
+ r(this, b).clear(), (t = r(this, x)) == null || t.disconnect(), r(this, x) && r(this, x).observe(this.container);
701
+ }
702
+ /**
703
+ * Remeasures every element and redraws every connection. Called
704
+ * automatically when `autoUpdate` is on; call it yourself after moving
705
+ * elements in ways the page can't observe.
706
+ */
707
+ update() {
708
+ if (r(this, T)) return;
709
+ r(this, v) && (rt(r(this, v)), l(this, v, 0));
710
+ const t = Qt(this.svg), n = [], s = /* @__PURE__ */ new Map(), o = (i, c, a) => {
711
+ const h = { index: 0, count: 1 }, f = J(a);
712
+ return s.has(i) || s.set(i, { top: [], right: [], bottom: [], left: [] }), s.get(i)[c].push({
713
+ slot: h,
714
+ key: it(c) ? f.y : f.x
715
+ }), h;
716
+ };
717
+ for (const i of r(this, b).values()) {
718
+ const { startElement: c, endElement: a } = i;
719
+ if (!Ot(c) || !Ot(a)) {
720
+ i._hide();
721
+ continue;
722
+ }
723
+ const { spread: h, startSide: f, endSide: d } = i.options, u = Et(c, t), k = Et(a, t), tt = Zt(u, k, f, d), yt = { index: 0, count: 1 };
724
+ n.push({
725
+ connection: i,
726
+ startRect: u,
727
+ endRect: k,
728
+ ...tt,
729
+ startSlot: h ? o(c, tt.startSide, k) : yt,
730
+ endSlot: h ? o(a, tt.endSide, u) : { ...yt }
731
+ });
732
+ }
733
+ for (const i of s.values())
734
+ for (const c of Object.values(i))
735
+ c.sort((a, h) => a.key - h.key).forEach((a, h) => {
736
+ a.slot.index = h, a.slot.count = c.length;
737
+ });
738
+ for (const i of n) {
739
+ const { connection: c, startRect: a, endRect: h, startSide: f, endSide: d } = i, { spacing: u, gap: k } = c.options;
740
+ c._draw(
741
+ vt(a, f, {
742
+ ...i.startSlot,
743
+ spacing: u,
744
+ gap: k
745
+ }),
746
+ f,
747
+ vt(h, d, { ...i.endSlot, spacing: u, gap: k }),
748
+ d
749
+ );
750
+ }
751
+ }
752
+ /**
753
+ * Removes every connection and the overlay, and stops listening for
754
+ * changes. The instance can't be used afterwards.
755
+ */
756
+ destroy() {
757
+ var t, n;
758
+ r(this, T) || (this.clear(), l(this, T, !0), r(this, v) && rt(r(this, v)), r(this, L) && rt(r(this, L)), window.removeEventListener("resize", r(this, S)), document.removeEventListener("scroll", r(this, S), {
759
+ capture: !0
760
+ }), (t = r(this, x)) == null || t.disconnect(), (n = r(this, D)) == null || n.disconnect(), this.svg.remove(), r(this, U) !== null && (this.container.style.position = r(this, U)));
761
+ }
762
+ }
763
+ W = new WeakMap(), b = new WeakMap(), v = new WeakMap(), L = new WeakMap(), T = new WeakMap(), x = new WeakMap(), D = new WeakMap(), U = new WeakMap(), g = new WeakSet(), /**
764
+ * Checks a connection without adding anything, so a bad option can't leave
765
+ * a connection behind that breaks every later update.
766
+ */
767
+ lt = function(t, n, s, o) {
768
+ if (r(this, T))
769
+ throw new N("this instance has been destroyed");
770
+ dt(s, o);
771
+ const i = V(t, "startElement"), c = V(n, "endElement"), a = s, h = a.id ?? `arc-${jt()}`;
772
+ if (r(this, b).has(h))
773
+ throw new N(`a connection with id "${h}" exists`);
774
+ return { id: h, startElement: i, endElement: c, options: a };
775
+ }, ut = function({
776
+ id: t,
777
+ startElement: n,
778
+ endElement: s,
779
+ options: o
780
+ }) {
781
+ var c, a;
782
+ const i = Q._create({
783
+ id: t,
784
+ startElement: n,
785
+ endElement: s,
786
+ options: o,
787
+ defaults: r(this, W).defaults,
788
+ owner: this
789
+ });
790
+ return r(this, b).set(t, i), this.svg.appendChild(i.element), (c = r(this, x)) == null || c.observe(n), (a = r(this, x)) == null || a.observe(s), i;
791
+ }, ft = function(t) {
792
+ if (!r(this, x) || t === this.container) return;
793
+ this.connections.some(
794
+ (s) => s.startElement === t || s.endElement === t
795
+ ) || r(this, x).unobserve(t);
796
+ }, S = new WeakMap(), Nt = function() {
797
+ var n;
798
+ const { autoUpdate: t = !0 } = r(this, W);
799
+ if (t !== !1) {
800
+ if (t === "frame") {
801
+ const s = () => {
802
+ this.update(), l(this, L, ot(s));
803
+ };
804
+ l(this, L, ot(s));
805
+ return;
806
+ }
807
+ window.addEventListener("resize", r(this, S)), document.addEventListener("scroll", r(this, S), {
808
+ capture: !0,
809
+ passive: !0
810
+ }), typeof ResizeObserver < "u" && (l(this, x, new ResizeObserver(r(this, S))), r(this, x).observe(this.container)), typeof MutationObserver < "u" && (l(this, D, new MutationObserver((s) => {
811
+ s.some((o) => !this.svg.contains(o.target)) && r(this, S).call(this);
812
+ })), r(this, D).observe(this.container, {
813
+ subtree: !0,
814
+ childList: !0,
815
+ attributes: !0,
816
+ attributeFilter: ["style", "class", "hidden"]
817
+ })), (n = document.fonts) == null || n.ready.then(r(this, S));
818
+ }
819
+ };
820
+ const ne = (e, t) => {
821
+ const n = new te(t);
822
+ try {
823
+ n.connectAll(e);
824
+ } catch (s) {
825
+ throw n.destroy(), s;
826
+ }
827
+ return n;
828
+ };
829
+ export {
830
+ te as Arclace,
831
+ N as ArclaceError,
832
+ Q as Connection,
833
+ Kt as DEFAULT_CONNECTION_OPTIONS,
834
+ Yt as bezierPath,
835
+ ne as createConnections,
836
+ Vt as orthogonalPath,
837
+ qt as straightPath
838
+ };
839
+ //# sourceMappingURL=arclace.js.map