sliced-areas 1.0.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,1732 @@
1
+ var INTERNAL_ATTR = "data-sliced-areas-internal", AUTO_ATTR = "data-sliced-areas-auto", DEFAULT_RATIO = .5, MIN_RATIO = .08, EPS = 1e-6, ALIGN_TOLERANCE = 1e-4, JOIN_TOLERANCE = .02, DEFAULT_SPLITTER_SIZE = 2, JOIN_ZONE_DEPTH = .18, SPLIT_GESTURE_THRESHOLD_PX = 6, SPLIT_AXIS_TOLERANCE_PX = 12, DRAG_PARK_TOLERANCE_PX = 20, CLICK_TOLERANCE_PX = 4, MOVE_SNAP_PX = 12, REPLACE_THRESHOLD_RATIO = MIN_RATIO * 3, SlicedAreasElement = class extends HTMLElement {
2
+ graph = null;
3
+ rootEl = null;
4
+ stashEl = null;
5
+ resizeObserver = null;
6
+ dragState = null;
7
+ areaDragState = null;
8
+ lastPointer = null;
9
+ dropOverlay = null;
10
+ dropShade = null;
11
+ dragLabel = null;
12
+ dragSnapshot = null;
13
+ dragCursor = null;
14
+ storedGraph = null;
15
+ storedTags = null;
16
+ areaResolver = null;
17
+ resolvedNodes = /* @__PURE__ */ new Map();
18
+ areaTags = /* @__PURE__ */ new Map();
19
+ vertCounter = 0;
20
+ edgeCounter = 0;
21
+ areaCounter = 0;
22
+ get layout() {
23
+ return this.graph ? this.serializeLayout(this.graph) : null;
24
+ }
25
+ set layout(e) {
26
+ if (!e) {
27
+ this.graph = null, this.areaTags.clear(), this.storedGraph = null, this.storedTags = null, this.resolvedNodes.clear(), this.render();
28
+ return;
29
+ }
30
+ if (e.areas.length === 0) {
31
+ this.graph = null, this.areaTags.clear(), this.storedGraph = null, this.storedTags = null, this.resolvedNodes.clear(), this.render();
32
+ return;
33
+ }
34
+ this.storedGraph = null, this.storedTags = null, this.graph = this.buildGraphFromLayout(e), this.resolvedNodes.clear(), this.render();
35
+ }
36
+ setResolver(e) {
37
+ this.areaResolver = e, this.resolvedNodes.clear(), e && this.render();
38
+ }
39
+ connectedCallback() {
40
+ this.ensureRoot(), this.addEventListener("sliced-areas:retag", this.onRetagRequest), this.ensureResizeObserver(), this.render();
41
+ }
42
+ disconnectedCallback() {
43
+ this.resizeObserver &&= (this.resizeObserver.disconnect(), null), this.detachDragListeners(), this.removeEventListener("sliced-areas:retag", this.onRetagRequest);
44
+ }
45
+ split(e, t = "right", r = 0, i = 0) {
46
+ if (!this.graph) return;
47
+ let a = this.nextAreaId(), o = Number.isFinite(r) && Number.isFinite(i) && this.rootEl ? this.splitAreaAtPointer(this.graph, e, t, r, i, a) : this.splitAreaByZone(this.graph, e, t, DEFAULT_RATIO, a);
48
+ o && (this.ensureAreaNode(a, e, !0), this.graph = this.normalizeGraph(o), this.emitLayoutChange(), this.render());
49
+ }
50
+ join(e, t) {
51
+ if (!this.graph || !this.canJoin(e, t)) return;
52
+ let n = this.joinAreas(this.graph, e, t);
53
+ n && (this.graph = this.normalizeGraph(n), this.pruneAreaTags(this.graph), this.emitLayoutChange(), this.render());
54
+ }
55
+ replace(e, t) {
56
+ if (!this.graph) return;
57
+ let n = this.replaceArea(this.graph, e, t);
58
+ n && (this.graph = this.normalizeGraph(n), this.removeAreaNode(t), this.pruneAreaTags(this.graph), this.emitLayoutChange(), this.render());
59
+ }
60
+ swap(e, t) {
61
+ if (!this.graph) return;
62
+ let n = this.swapAreaIds(this.graph, e, t);
63
+ n && (this.graph = n, this.emitLayoutChange(), this.render());
64
+ }
65
+ move(e, t, n, r) {
66
+ if (!this.graph) return;
67
+ let i = this.moveArea(this.graph, e, t, n, r);
68
+ i && (this.graph = this.normalizeGraph(i), this.emitLayoutChange(), this.render());
69
+ }
70
+ close(e) {
71
+ if (!this.graph || !this.graph.areas[e] || Object.keys(this.graph.areas).length <= 1) return;
72
+ let t = {
73
+ verts: { ...this.graph.verts },
74
+ edges: { ...this.graph.edges },
75
+ areas: { ...this.graph.areas }
76
+ };
77
+ delete t.areas[e], this.graph = this.normalizeGraph(t), this.removeAreaNode(e), this.pruneAreaTags(this.graph), this.emitLayoutChange(), this.render();
78
+ }
79
+ retag(e, t) {
80
+ this.graph && this.graph.areas[e] && this.areaTags.get(e) !== t && (this.areaTags.set(e, t), this.resolvedNodes.delete(e), this.detachAreaNode(e), this.emitLayoutChange(), this.render());
81
+ }
82
+ maximize(e) {
83
+ if (!this.graph || this.storedGraph || !this.graph.areas[e]) return;
84
+ this.storedGraph = this.graph, this.storedTags = new Map(this.areaTags), this.graph = this.createBaseGraph(e), this.areaTags = /* @__PURE__ */ new Map();
85
+ let t = this.storedTags.get(e);
86
+ t && this.areaTags.set(e, t), this.emitLayoutChange(), this.render();
87
+ }
88
+ restore() {
89
+ this.storedGraph && (this.graph = this.storedGraph, this.storedTags && (this.areaTags = new Map(this.storedTags)), this.storedGraph = null, this.storedTags = null, this.emitLayoutChange(), this.render());
90
+ }
91
+ ensureRoot() {
92
+ if (this.rootEl) return;
93
+ let t = this.querySelector(":scope > .sliced-areas-root");
94
+ this.rootEl = t ?? document.createElement("div"), this.rootEl.classList.add("sliced-areas-root"), this.rootEl.setAttribute(INTERNAL_ATTR, "true"), t || this.appendChild(this.rootEl), this.rootEl.addEventListener("pointerdown", this.onPointerDown), this.ensureStash();
95
+ }
96
+ ensureStash() {
97
+ if (this.stashEl) return;
98
+ let t = this.querySelector(":scope > .sliced-areas-stash");
99
+ this.stashEl = t ?? document.createElement("div"), this.stashEl.classList.add("sliced-areas-stash"), this.stashEl.setAttribute(INTERNAL_ATTR, "true"), t || this.appendChild(this.stashEl);
100
+ }
101
+ ensureResizeObserver() {
102
+ this.resizeObserver || (this.resizeObserver = new ResizeObserver(() => {
103
+ this.render();
104
+ }), this.resizeObserver.observe(this));
105
+ }
106
+ emitLayoutChange() {
107
+ this.graph && this.dispatchEvent(new CustomEvent("sliced-areas:layoutchange", { detail: { layout: this.serializeLayout(this.graph) } }));
108
+ }
109
+ emitCornerClick(e) {
110
+ e.corner === "top-left" && this.dispatchEvent(new CustomEvent("sliced-areas:cornerclick", { detail: e }));
111
+ }
112
+ render() {
113
+ if (!this.rootEl || !this.graph) return;
114
+ let n = this.collectAreaNodes();
115
+ this.ensureStash(), this.stashEl && (this.stashEl.innerHTML = "");
116
+ let r = Object.values(this.graph.areas).filter((e) => !n.has(e.id));
117
+ if (r.length > 0 && this.areaResolver) {
118
+ for (let e of r) {
119
+ let r = this.resolvedNodes.get(e.id);
120
+ if (r) {
121
+ n.set(e.id, r);
122
+ continue;
123
+ }
124
+ let i = this.areaTags.get(e.id) ?? e.id, a = this.areaResolver(i);
125
+ a && (a.dataset.areaId = e.id, a.setAttribute(AUTO_ATTR, "true"), this.resolvedNodes.set(e.id, a), n.set(e.id, a));
126
+ }
127
+ r = Object.values(this.graph.areas).filter((e) => !n.has(e.id));
128
+ }
129
+ if (r.length > 0) {
130
+ let e = {
131
+ missing: r.map((e) => e.id),
132
+ areas: Object.values(this.graph.areas).map((e) => ({
133
+ id: e.id,
134
+ rect: this.formatRect(this.getAreaRect(this.graph, e))
135
+ }))
136
+ };
137
+ throw Error(`Missing area content detected: ${JSON.stringify(e)}`);
138
+ }
139
+ let i = this.rootEl.getBoundingClientRect(), a = Math.max(i.width, 1), o = Math.max(i.height, 1), c = DEFAULT_SPLITTER_SIZE / 2;
140
+ this.rootEl.innerHTML = "";
141
+ for (let t of Object.values(this.graph.areas)) {
142
+ let r = this.getAreaRect(this.graph, t), i = document.createElement("div");
143
+ i.classList.add("sliced-areas-area"), i.setAttribute(INTERNAL_ATTR, "true");
144
+ let s = r.left * a + c, l = (1 - r.top) * o + c, u = (r.right - r.left) * a - c * 2, d = (r.top - r.bottom) * o - c * 2;
145
+ i.style.left = `${s}px`, i.style.top = `${l}px`, i.style.width = `${Math.max(u, 0)}px`, i.style.height = `${Math.max(d, 0)}px`;
146
+ let f = document.createElement("div");
147
+ f.classList.add("sliced-areas-overlay"), f.setAttribute(INTERNAL_ATTR, "true"), i.appendChild(f);
148
+ for (let n of [
149
+ "top-left",
150
+ "top-right",
151
+ "bottom-left",
152
+ "bottom-right"
153
+ ]) {
154
+ let r = document.createElement("div");
155
+ r.classList.add("sliced-areas-corner"), r.classList.add(`is-${n}`), r.setAttribute(INTERNAL_ATTR, "true"), r.dataset.areaId = t.id, r.dataset.corner = n, f.appendChild(r);
156
+ }
157
+ let p = n.get(t.id);
158
+ if (p) i.appendChild(p);
159
+ else throw Error(`Missing area content for ${t.id}`);
160
+ this.rootEl.appendChild(i);
161
+ }
162
+ if (this.stashEl) for (let [e, t] of n.entries()) this.graph.areas[e] || this.stashEl.appendChild(t);
163
+ for (let e of this.buildResizeHandles(this.graph, a, o)) this.rootEl.appendChild(e);
164
+ }
165
+ collectAreaNodes() {
166
+ let n = /* @__PURE__ */ new Map(), r = Array.from(this.querySelectorAll("[data-area-id]"));
167
+ for (let i of r) {
168
+ if (i.hasAttribute(INTERNAL_ATTR)) continue;
169
+ let r = i.dataset.areaId;
170
+ if (!r) continue;
171
+ let a = i.hasAttribute(AUTO_ATTR), o = n.get(r);
172
+ if (o) {
173
+ o.hasAttribute(AUTO_ATTR) && !a && (o.remove(), n.set(r, i));
174
+ continue;
175
+ }
176
+ n.set(r, i);
177
+ }
178
+ return n;
179
+ }
180
+ ensureAreaNode(n, r, i = !0) {
181
+ if (Array.from(this.querySelectorAll(`[data-area-id="${n}"]`)).find((t) => !t.hasAttribute(INTERNAL_ATTR))) return;
182
+ this.inheritAreaTag(n, r);
183
+ let a = this.areaTags.get(r) ?? this.areaTags.get(n), o = Array.from(this.querySelectorAll(`[data-area-id="${r}"]`)).find((t) => !t.hasAttribute(INTERNAL_ATTR)), s = o?.hasAttribute(AUTO_ATTR) ?? !1;
184
+ if (i && o && (!s || !this.areaResolver)) {
185
+ let e = o.cloneNode(!0);
186
+ e.dataset.areaId = n, e.setAttribute(AUTO_ATTR, "true"), this.appendChild(e);
187
+ return;
188
+ }
189
+ if (a && this.areaResolver) {
190
+ let e = this.areaResolver(a);
191
+ if (e) {
192
+ e.dataset.areaId = n, e.setAttribute(AUTO_ATTR, "true"), this.appendChild(e);
193
+ return;
194
+ }
195
+ }
196
+ let c = document.createElement("div");
197
+ c.dataset.areaId = n, c.classList.add("sliced-areas-auto-content"), c.setAttribute(AUTO_ATTR, "true"), c.textContent = a ?? "Area", this.appendChild(c);
198
+ }
199
+ inheritAreaTag(e, t) {
200
+ if (this.areaTags.has(e)) return;
201
+ let n = this.areaTags.get(t);
202
+ if (n) {
203
+ this.areaTags.set(e, n);
204
+ return;
205
+ }
206
+ this.areaTags.set(e, t);
207
+ }
208
+ buildGraphFromLayout(e) {
209
+ this.areaTags.clear();
210
+ let t = [];
211
+ for (let n of e.areas) {
212
+ let e = this.nextAreaId();
213
+ t.push({
214
+ id: e,
215
+ rect: n.rect
216
+ }), this.areaTags.set(e, n.tag);
217
+ }
218
+ let n = this.buildGraphFromRects(t);
219
+ return this.normalizeGraph(n);
220
+ }
221
+ serializeLayout(e) {
222
+ return { areas: Object.values(e.areas).map((t) => ({
223
+ tag: this.areaTags.get(t.id) ?? t.id,
224
+ rect: this.formatRect(this.getAreaRect(e, t))
225
+ })) };
226
+ }
227
+ pruneAreaTags(e) {
228
+ for (let t of this.areaTags.keys()) e.areas[t] || (this.areaTags.delete(t), this.resolvedNodes.delete(t));
229
+ }
230
+ onRetagRequest = (e) => {
231
+ let t = e.detail?.tag;
232
+ if (!t || typeof t != "string") return;
233
+ let n = e.target;
234
+ if (!(n instanceof HTMLElement)) return;
235
+ let r = n.closest("[data-area-id]");
236
+ if (!r || !(r instanceof HTMLElement)) return;
237
+ let i = r.dataset.areaId;
238
+ i && this.graph?.areas[i] && (e.stopPropagation(), this.retag(i, t));
239
+ };
240
+ normalizeRectsToUnit(e) {
241
+ let t = Infinity, n = -Infinity, r = Infinity, a = -Infinity;
242
+ for (let i of e) t = Math.min(t, i.rect.left), n = Math.max(n, i.rect.right), r = Math.min(r, i.rect.bottom), a = Math.max(a, i.rect.top);
243
+ if (!Number.isFinite(t) || !Number.isFinite(n) || !Number.isFinite(r) || !Number.isFinite(a)) return e;
244
+ let o = n - t, s = a - r;
245
+ return o <= EPS || s <= EPS || Math.abs(t) <= EPS && Math.abs(n - 1) <= EPS && Math.abs(r) <= EPS && Math.abs(a - 1) <= EPS ? e : e.map((e) => ({
246
+ id: e.id,
247
+ rect: {
248
+ left: (e.rect.left - t) / o,
249
+ right: (e.rect.right - t) / o,
250
+ bottom: (e.rect.bottom - r) / s,
251
+ top: (e.rect.top - r) / s
252
+ }
253
+ }));
254
+ }
255
+ buildGraphFromRects(e) {
256
+ let t = {}, n = {}, r = {}, i = /* @__PURE__ */ new Map(), a = (e, n) => {
257
+ let r = `${e.toFixed(6)}|${n.toFixed(6)}`, a = i.get(r);
258
+ if (a) return a;
259
+ let o = this.addVert(t, e, n);
260
+ return i.set(r, o), o;
261
+ };
262
+ for (let t of e) {
263
+ let e = t.rect, i = a(e.left, e.bottom), o = a(e.left, e.top), s = a(e.right, e.top), c = a(e.right, e.bottom);
264
+ r[t.id] = {
265
+ id: t.id,
266
+ v1: i,
267
+ v2: o,
268
+ v3: s,
269
+ v4: c
270
+ }, this.addEdge(n, i, o), this.addEdge(n, o, s), this.addEdge(n, s, c), this.addEdge(n, c, i);
271
+ }
272
+ return {
273
+ verts: t,
274
+ edges: n,
275
+ areas: r
276
+ };
277
+ }
278
+ createBaseGraph(e) {
279
+ let t = {}, n = {}, r = {}, i = this.addVert(t, 0, 0), a = this.addVert(t, 0, 1), o = this.addVert(t, 1, 1), s = this.addVert(t, 1, 0), c = this.nextEdgeId(n);
280
+ n[c] = {
281
+ id: c,
282
+ v1: i,
283
+ v2: a,
284
+ border: !0
285
+ };
286
+ let l = this.nextEdgeId(n);
287
+ n[l] = {
288
+ id: l,
289
+ v1: a,
290
+ v2: o,
291
+ border: !0
292
+ };
293
+ let u = this.nextEdgeId(n);
294
+ n[u] = {
295
+ id: u,
296
+ v1: o,
297
+ v2: s,
298
+ border: !0
299
+ };
300
+ let d = this.nextEdgeId(n);
301
+ return n[d] = {
302
+ id: d,
303
+ v1: s,
304
+ v2: i,
305
+ border: !0
306
+ }, r[e] = {
307
+ id: e,
308
+ v1: i,
309
+ v2: a,
310
+ v3: o,
311
+ v4: s
312
+ }, {
313
+ verts: t,
314
+ edges: n,
315
+ areas: r
316
+ };
317
+ }
318
+ splitAreaByZone(e, t, n, i, a) {
319
+ let o = e.areas[t];
320
+ if (!o) return null;
321
+ let s = this.getAreaRect(e, o), c = Math.max(MIN_RATIO, Math.min(1 - MIN_RATIO, i));
322
+ if (n === "left" || n === "right") {
323
+ let r = s.right - s.left, i = n === "left" ? s.left + r * c : s.left + r * (1 - c), o = n === "left" ? "max" : "min";
324
+ return this.splitAreaAt(e, t, "vertical", i, a, o);
325
+ }
326
+ let l = s.top - s.bottom, u = n === "bottom" ? s.bottom + l * c : s.bottom + l * (1 - c), d = n === "bottom" ? "max" : "min";
327
+ return this.splitAreaAt(e, t, "horizontal", u, a, d);
328
+ }
329
+ splitAreaAtPointer(e, t, n, i, a, o) {
330
+ let s = e.areas[t];
331
+ if (!s || !this.rootEl) return null;
332
+ let c = this.rootEl.getBoundingClientRect(), l = (i - c.left) / c.width, u = 1 - (a - c.top) / c.height, d = this.getAreaRect(e, s), f = Math.min(Math.max(l, d.left + MIN_RATIO), d.right - MIN_RATIO), p = Math.min(Math.max(u, d.bottom + MIN_RATIO), d.top - MIN_RATIO);
333
+ if (n === "left" || n === "right") {
334
+ let r = n === "left" ? "max" : "min";
335
+ return this.splitAreaAt(e, t, "vertical", f, o, r);
336
+ }
337
+ let m = n === "bottom" ? "max" : "min";
338
+ return this.splitAreaAt(e, t, "horizontal", p, o, m);
339
+ }
340
+ splitAreaAt(e, t, n, r, a, o) {
341
+ let s = e.areas[t];
342
+ if (!s) return null;
343
+ let c = this.getAreaRect(e, s);
344
+ if (n === "vertical") {
345
+ if (r <= c.left + EPS || r >= c.right - EPS) return null;
346
+ } else if (r <= c.bottom + EPS || r >= c.top - EPS) return null;
347
+ let l = {
348
+ verts: { ...e.verts },
349
+ edges: { ...e.edges },
350
+ areas: { ...e.areas }
351
+ };
352
+ if (n === "horizontal") {
353
+ let e = this.addVert(l.verts, c.left, r), t = this.addVert(l.verts, c.right, r);
354
+ this.addEdge(l.edges, s.v1, e), this.addEdge(l.edges, e, s.v2), this.addEdge(l.edges, s.v3, t), this.addEdge(l.edges, t, s.v4), this.addEdge(l.edges, e, t);
355
+ let n = {
356
+ id: o === "min" ? s.id : a,
357
+ v1: s.v1,
358
+ v2: e,
359
+ v3: t,
360
+ v4: s.v4
361
+ }, i = {
362
+ id: o === "max" ? s.id : a,
363
+ v1: e,
364
+ v2: s.v2,
365
+ v3: s.v3,
366
+ v4: t
367
+ };
368
+ l.areas[n.id] = n, l.areas[i.id] = i, n.id !== s.id && i.id !== s.id && delete l.areas[s.id];
369
+ } else {
370
+ let e = this.addVert(l.verts, r, c.bottom), t = this.addVert(l.verts, r, c.top);
371
+ this.addEdge(l.edges, s.v1, e), this.addEdge(l.edges, e, s.v4), this.addEdge(l.edges, s.v2, t), this.addEdge(l.edges, t, s.v3), this.addEdge(l.edges, e, t);
372
+ let n = {
373
+ id: o === "min" ? s.id : a,
374
+ v1: s.v1,
375
+ v2: s.v2,
376
+ v3: t,
377
+ v4: e
378
+ }, i = {
379
+ id: o === "max" ? s.id : a,
380
+ v1: e,
381
+ v2: t,
382
+ v3: s.v3,
383
+ v4: s.v4
384
+ };
385
+ l.areas[n.id] = n, l.areas[i.id] = i, n.id !== s.id && i.id !== s.id && delete l.areas[s.id];
386
+ }
387
+ return l;
388
+ }
389
+ joinAreas(e, t, n) {
390
+ let r = e, a = r.areas[t], o = r.areas[n];
391
+ if (!a || !o) return null;
392
+ let s = this.getOrientation(r, a, o);
393
+ if (s === "none") return null;
394
+ let c = this.getAreaRect(r, a), l = this.getAreaRect(r, o), u = [];
395
+ if (s === "west" || s === "east") {
396
+ let e = Math.max(c.bottom, l.bottom), s = Math.min(c.top, l.top);
397
+ if (s - e <= EPS) return null;
398
+ let d = this.trimAreaToRange(r, t, "horizontal", e, s);
399
+ if (r = d.graph, a = r.areas[d.keptAreaId], !a) return null;
400
+ u = u.concat(d.created);
401
+ let f = this.trimAreaToRange(r, n, "horizontal", e, s);
402
+ if (r = f.graph, o = r.areas[f.keptAreaId], !o) return null;
403
+ u = u.concat(f.created);
404
+ } else {
405
+ let e = Math.max(c.left, l.left), s = Math.min(c.right, l.right);
406
+ if (s - e <= EPS) return null;
407
+ let d = this.trimAreaToRange(r, t, "vertical", e, s);
408
+ if (r = d.graph, a = r.areas[d.keptAreaId], !a) return null;
409
+ u = u.concat(d.created);
410
+ let f = this.trimAreaToRange(r, n, "vertical", e, s);
411
+ if (r = f.graph, o = r.areas[f.keptAreaId], !o) return null;
412
+ u = u.concat(f.created);
413
+ }
414
+ let d = this.joinAreasAligned(r, t, n);
415
+ return d ? u.length === 0 ? d : this.processRemainders(d, u, n) : null;
416
+ }
417
+ trimAreaToRange(e, t, n, r, a) {
418
+ let o = e, s = t, c = [], l = o.areas[s];
419
+ if (!l) return {
420
+ graph: o,
421
+ keptAreaId: s,
422
+ created: c
423
+ };
424
+ let u = this.getAreaRect(o, l);
425
+ if (n === "horizontal") {
426
+ if (u.bottom < r - EPS) {
427
+ let e = this.nextAreaId(), i = this.splitAreaAt(o, s, n, r, e, "max");
428
+ i && (o = i, c.push({
429
+ id: e,
430
+ sourceAreaId: t
431
+ }));
432
+ }
433
+ let e = o.areas[s];
434
+ if (e && this.getAreaRect(o, e).top > a + EPS) {
435
+ let e = this.nextAreaId(), r = this.splitAreaAt(o, s, n, a, e, "min");
436
+ r && (o = r, c.push({
437
+ id: e,
438
+ sourceAreaId: t
439
+ }));
440
+ }
441
+ } else {
442
+ if (u.left < r - EPS) {
443
+ let e = this.nextAreaId(), i = this.splitAreaAt(o, s, n, r, e, "max");
444
+ i && (o = i, c.push({
445
+ id: e,
446
+ sourceAreaId: t
447
+ }));
448
+ }
449
+ let e = o.areas[s];
450
+ if (e && this.getAreaRect(o, e).right > a + EPS) {
451
+ let e = this.nextAreaId(), r = this.splitAreaAt(o, s, n, a, e, "min");
452
+ r && (o = r, c.push({
453
+ id: e,
454
+ sourceAreaId: t
455
+ }));
456
+ }
457
+ }
458
+ return {
459
+ graph: o,
460
+ keptAreaId: s,
461
+ created: c
462
+ };
463
+ }
464
+ processRemainders(e, t, n) {
465
+ let r = e, i = t.filter((e) => e.sourceAreaId === n), a = t.filter((e) => e.sourceAreaId !== n);
466
+ if (i.length > 0) {
467
+ let e = i[0], t = i.slice(1);
468
+ e && r.areas[e.id] && !r.areas[n] && (r = this.renameAreaId(r, e.id, n)), this.ensureAreaNode(n, n, !0);
469
+ for (let e of t) this.ensureAreaNode(e.id, n, !0);
470
+ } else this.removeAreaNode(n);
471
+ for (let e of a) delete r.areas[e.id], this.removeAreaNode(e.id);
472
+ return r;
473
+ }
474
+ setAreaRect(e, t, n) {
475
+ if (!e.areas[t]) return e;
476
+ let r = {
477
+ verts: { ...e.verts },
478
+ edges: { ...e.edges },
479
+ areas: { ...e.areas }
480
+ }, i = this.addVert(r.verts, n.left, n.bottom), a = this.addVert(r.verts, n.left, n.top), o = this.addVert(r.verts, n.right, n.top), s = this.addVert(r.verts, n.right, n.bottom);
481
+ return this.addEdge(r.edges, i, a), this.addEdge(r.edges, a, o), this.addEdge(r.edges, o, s), this.addEdge(r.edges, s, i), r.areas[t] = {
482
+ id: t,
483
+ v1: i,
484
+ v2: a,
485
+ v3: o,
486
+ v4: s
487
+ }, r;
488
+ }
489
+ moveArea(e, t, n, r, a) {
490
+ let o = e.areas[t], s = e.areas[n];
491
+ if (!o || !s || t === n) return null;
492
+ let c = {
493
+ verts: { ...e.verts },
494
+ edges: { ...e.edges },
495
+ areas: { ...e.areas }
496
+ }, l = r.right - r.left, u = r.top - r.bottom, d = a.right - a.left, f = a.top - a.bottom;
497
+ if (l <= EPS || u <= EPS || d <= EPS || f <= EPS) return null;
498
+ let p = this.setAreaRect(c, t, r);
499
+ return this.setAreaRect(p, n, a);
500
+ }
501
+ replaceArea(e, t, n) {
502
+ let r = e.areas[t], i = e.areas[n];
503
+ if (!r || !i || t === n) return null;
504
+ let a = this.getAreaRect(e, i), o = {
505
+ verts: { ...e.verts },
506
+ edges: { ...e.edges },
507
+ areas: { ...e.areas }
508
+ }, s = this.setAreaRect(o, t, a);
509
+ return delete s.areas[n], s;
510
+ }
511
+ renameAreaId(t, n, r) {
512
+ if (n === r) return t;
513
+ let i = t.areas[n];
514
+ if (!i) return t;
515
+ if (t.areas[r]) throw Error(`Cannot rename area ${n} to ${r}: target id already exists`);
516
+ let a = {
517
+ verts: { ...t.verts },
518
+ edges: { ...t.edges },
519
+ areas: { ...t.areas }
520
+ };
521
+ a.areas[r] = {
522
+ ...i,
523
+ id: r
524
+ }, delete a.areas[n];
525
+ let o = Array.from(this.querySelectorAll(`[data-area-id="${r}"]`)).find((t) => !t.hasAttribute(INTERNAL_ATTR)), s = Array.from(this.querySelectorAll(`[data-area-id="${n}"]`)).find((t) => !t.hasAttribute(INTERNAL_ATTR));
526
+ s && (o ? s.remove() : s.dataset.areaId = r);
527
+ let c = this.areaTags.get(n);
528
+ c && (this.areaTags.set(r, c), this.areaTags.delete(n));
529
+ let l = this.resolvedNodes.get(n);
530
+ return l && (this.resolvedNodes.set(r, l), this.resolvedNodes.delete(n)), a;
531
+ }
532
+ removeAreaNode(t) {
533
+ let n = Array.from(this.querySelectorAll(`[data-area-id="${t}"]`)).find((t) => !t.hasAttribute(INTERNAL_ATTR));
534
+ n && n.remove(), this.areaTags.delete(t), this.resolvedNodes.delete(t);
535
+ }
536
+ detachAreaNode(t) {
537
+ let n = Array.from(this.querySelectorAll(`[data-area-id="${t}"]`)).find((t) => !t.hasAttribute(INTERNAL_ATTR));
538
+ n && n.remove(), this.resolvedNodes.delete(t);
539
+ }
540
+ joinAreasAligned(e, t, n) {
541
+ let r = e.areas[t], i = e.areas[n];
542
+ if (!r || !i) return null;
543
+ let a = this.getOrientation(e, r, i);
544
+ if (a === "none" || !this.areAreasAligned(e, r, i, a)) return null;
545
+ let o = {
546
+ verts: { ...e.verts },
547
+ edges: { ...e.edges },
548
+ areas: { ...e.areas }
549
+ }, s = { ...r };
550
+ return a === "west" ? (s.v1 = i.v1, s.v2 = i.v2, this.addEdge(o.edges, s.v2, s.v3), this.addEdge(o.edges, s.v1, s.v4)) : a === "north" ? (s.v2 = i.v2, s.v3 = i.v3, this.addEdge(o.edges, s.v1, s.v2), this.addEdge(o.edges, s.v3, s.v4)) : a === "east" ? (s.v3 = i.v3, s.v4 = i.v4, this.addEdge(o.edges, s.v2, s.v3), this.addEdge(o.edges, s.v1, s.v4)) : a === "south" && (s.v1 = i.v1, s.v4 = i.v4, this.addEdge(o.edges, s.v1, s.v2), this.addEdge(o.edges, s.v3, s.v4)), o.areas[t] = s, delete o.areas[n], o;
551
+ }
552
+ swapAreaIds(e, t, n) {
553
+ let r = e.areas[t], i = e.areas[n];
554
+ if (!r || !i) return null;
555
+ let a = {
556
+ verts: { ...e.verts },
557
+ edges: { ...e.edges },
558
+ areas: { ...e.areas }
559
+ };
560
+ return a.areas[t] = {
561
+ ...i,
562
+ id: t
563
+ }, a.areas[n] = {
564
+ ...r,
565
+ id: n
566
+ }, a;
567
+ }
568
+ canJoin(e, t) {
569
+ if (!this.graph) return !1;
570
+ let n = this.graph.areas[e], r = this.graph.areas[t];
571
+ if (!n || !r) return !1;
572
+ let a = this.getOrientation(this.graph, n, r);
573
+ if (a === "none") return !1;
574
+ let o = this.getAreaRect(this.graph, n), s = this.getAreaRect(this.graph, r);
575
+ return a === "west" || a === "east" ? Math.min(o.top, s.top) - Math.max(o.bottom, s.bottom) > EPS : Math.min(o.right, s.right) - Math.max(o.left, s.left) > EPS;
576
+ }
577
+ areAreasAligned(e, t, n, r) {
578
+ let i = this.getOffsets(e, t, n, r);
579
+ return i ? Math.abs(i.offset1) <= JOIN_TOLERANCE && Math.abs(i.offset2) <= JOIN_TOLERANCE : !1;
580
+ }
581
+ getOrientation(e, t, n) {
582
+ let r = this.getAreaRect(e, t), a = this.getAreaRect(e, n), s = Math.min(r.right, a.right) - Math.max(r.left, a.left), c = Math.min(r.top, a.top) - Math.max(r.bottom, a.bottom), l = Math.min(JOIN_TOLERANCE, r.right - r.left, a.right - a.left), u = Math.min(JOIN_TOLERANCE, r.top - r.bottom, a.top - a.bottom);
583
+ return Math.abs(r.top - a.bottom) <= EPS && s >= l ? "north" : Math.abs(r.bottom - a.top) <= EPS && s >= l ? "south" : Math.abs(r.left - a.right) <= EPS && c >= u ? "west" : Math.abs(r.right - a.left) <= EPS && c >= u ? "east" : "none";
584
+ }
585
+ getOffsets(e, t, n, r) {
586
+ let i = e.verts[t.v1], a = e.verts[t.v2], o = e.verts[t.v3], s = e.verts[t.v4], c = e.verts[n.v1], l = e.verts[n.v2], u = e.verts[n.v3], d = e.verts[n.v4];
587
+ return !i || !a || !o || !s || !c || !l || !u || !d ? null : r === "west" ? {
588
+ offset1: u.y - a.y,
589
+ offset2: d.y - i.y
590
+ } : r === "north" ? {
591
+ offset1: a.x - c.x,
592
+ offset2: o.x - d.x
593
+ } : r === "east" ? {
594
+ offset1: l.y - o.y,
595
+ offset2: c.y - s.y
596
+ } : r === "south" ? {
597
+ offset1: i.x - l.x,
598
+ offset2: s.x - u.x
599
+ } : null;
600
+ }
601
+ buildResizeHandles(t, n, r) {
602
+ let a = [], o = DEFAULT_SPLITTER_SIZE + 2, c = /* @__PURE__ */ new Map();
603
+ for (let e of Object.values(t.areas)) {
604
+ let n = this.getAreaRect(t, e);
605
+ this.registerEdge(c, "vertical", n.left, n.bottom, n.top), this.registerEdge(c, "vertical", n.right, n.bottom, n.top), this.registerEdge(c, "horizontal", n.bottom, n.left, n.right), this.registerEdge(c, "horizontal", n.top, n.left, n.right);
606
+ }
607
+ for (let t of c.values()) {
608
+ if (t.axis === "vertical" && (t.coord <= EPS || t.coord >= 1 - EPS) || t.axis === "horizontal" && (t.coord <= EPS || t.coord >= 1 - EPS)) continue;
609
+ let s = this.computeSharedSegments(t.segments);
610
+ for (let i of s) {
611
+ let s = document.createElement("div");
612
+ s.classList.add("sliced-areas-handle"), s.setAttribute(INTERNAL_ATTR, "true"), s.dataset.axis = t.axis, s.dataset.coord = t.coord.toFixed(6), s.dataset.start = i.start.toFixed(6), s.dataset.end = i.end.toFixed(6), t.axis === "vertical" ? (s.classList.add("is-vertical"), s.style.left = `${t.coord * n - o / 2}px`, s.style.top = `${(1 - i.end) * r}px`, s.style.width = `${o}px`, s.style.height = `${(i.end - i.start) * r}px`) : (s.classList.add("is-horizontal"), s.style.left = `${i.start * n}px`, s.style.top = `${(1 - t.coord) * r - o / 2}px`, s.style.width = `${(i.end - i.start) * n}px`, s.style.height = `${o}px`), a.push(s);
613
+ }
614
+ }
615
+ return a;
616
+ }
617
+ registerEdge(e, t, n, r, i) {
618
+ let a = Math.min(r, i), o = Math.max(r, i), s = `${t}|${n.toFixed(6)}`, c = e.get(s), l = c ?? {
619
+ axis: t,
620
+ coord: n,
621
+ segments: []
622
+ };
623
+ l.segments.push({
624
+ start: a,
625
+ end: o
626
+ }), c || e.set(s, l);
627
+ }
628
+ computeSharedSegments(e) {
629
+ let t = [];
630
+ for (let n of e) {
631
+ let e = Math.min(n.start, n.end), r = Math.max(n.start, n.end);
632
+ r - e <= EPS || (t.push({
633
+ pos: e,
634
+ delta: 1
635
+ }), t.push({
636
+ pos: r,
637
+ delta: -1
638
+ }));
639
+ }
640
+ t.sort((e, t) => Math.abs(e.pos - t.pos) > EPS ? e.pos - t.pos : e.delta - t.delta);
641
+ let n = [], r = 0, a = null;
642
+ for (let e of t) {
643
+ let t = r;
644
+ r += e.delta, t < 2 && r >= 2 ? a = e.pos : t >= 2 && r < 2 && a !== null && (e.pos - a > EPS && n.push({
645
+ start: a,
646
+ end: e.pos
647
+ }), a = null);
648
+ }
649
+ return n;
650
+ }
651
+ onPointerDown = (e) => {
652
+ if (!this.rootEl || !this.graph) return;
653
+ let t = e.target;
654
+ if (!(t instanceof HTMLElement)) return;
655
+ if (t.classList.contains("sliced-areas-grab")) {
656
+ let n = t.dataset.areaId;
657
+ if (!n) return;
658
+ e.preventDefault(), this.startAreaDrag(e, n);
659
+ return;
660
+ }
661
+ if (t.classList.contains("sliced-areas-corner")) {
662
+ let n = t.dataset.areaId;
663
+ if (!n) return;
664
+ let r = t.dataset.corner;
665
+ e.preventDefault(), this.startAreaDrag(e, n, r);
666
+ return;
667
+ }
668
+ if (!t.classList.contains("sliced-areas-handle")) return;
669
+ e.preventDefault();
670
+ let n = t.dataset.axis ?? "", r = Number(t.dataset.coord), i = Number(t.dataset.start), a = Number(t.dataset.end);
671
+ if (!n || !Number.isFinite(r) || !Number.isFinite(i) || !Number.isFinite(a)) return;
672
+ let o = this.getEdgeDragBounds(this.graph, n, r, i, a);
673
+ o && (t.setPointerCapture(e.pointerId), this.dragState = {
674
+ axis: n,
675
+ coord: r,
676
+ start: i,
677
+ end: a,
678
+ min: o.min,
679
+ max: o.max,
680
+ pointerId: e.pointerId,
681
+ originX: e.clientX,
682
+ originY: e.clientY
683
+ }, this.dragSnapshot = this.cloneGraph(this.graph), window.addEventListener("pointermove", this.onPointerMove), window.addEventListener("pointerup", this.onPointerUp), window.addEventListener("keydown", this.onKeyDown), window.addEventListener("keyup", this.onKeyUp));
684
+ };
685
+ onPointerMove = (e) => {
686
+ if (this.areaDragState) {
687
+ this.updateAreaDrag(e);
688
+ return;
689
+ }
690
+ if (!this.rootEl || !this.graph || !this.dragState) return;
691
+ let t = this.rootEl.getBoundingClientRect(), n = Math.max(t.width, 1), r = Math.max(t.height, 1), i = e.clientX - this.dragState.originX, a = e.clientY - this.dragState.originY, o = this.dragState.axis === "vertical" ? i / n : -a / r, s = Math.max(this.dragState.min, Math.min(this.dragState.max, this.dragState.coord + o)), c = this.moveEdge(this.graph, this.dragState.axis, this.dragState.coord, s, this.dragState.start, this.dragState.end);
692
+ c && (this.graph = c, this.dragState.coord = s, this.dragState.originX = e.clientX, this.dragState.originY = e.clientY, this.render());
693
+ };
694
+ onPointerUp = (e) => {
695
+ if (this.areaDragState) {
696
+ let t = this.areaDragState.originCorner, n = this.areaDragState.moved ?? !1;
697
+ t === "top-left" && !n && this.emitCornerClick({
698
+ areaId: this.areaDragState.sourceAreaId,
699
+ corner: t,
700
+ clientX: e.clientX,
701
+ clientY: e.clientY
702
+ }), this.finishAreaDrag();
703
+ return;
704
+ }
705
+ this.dragState && (this.dragState = null, this.detachDragListeners(), this.detachKeyListener(), this.dragSnapshot = null, this.graph &&= this.normalizeGraph(this.graph), this.emitLayoutChange());
706
+ };
707
+ onKeyDown = (e) => {
708
+ if (e.key === "Control") {
709
+ this.areaDragState && (this.areaDragState.swapMode = !0, this.refreshAreaDrag());
710
+ return;
711
+ }
712
+ if (e.key === "Escape") {
713
+ if (this.areaDragState) {
714
+ e.preventDefault(), this.cancelAreaDrag();
715
+ return;
716
+ }
717
+ this.dragState && (e.preventDefault(), this.cancelResizeDrag());
718
+ }
719
+ };
720
+ onKeyUp = (e) => {
721
+ e.key === "Control" && this.areaDragState && (this.areaDragState.swapMode = !1, this.refreshAreaDrag());
722
+ };
723
+ cancelAreaDrag() {
724
+ this.areaDragState = null, this.hideDropOverlay(), this.hideJoinShade(), this.hideDragLabel(), this.detachDragListeners(), this.detachKeyListener(), this.resetDragCursor(!0), this.lastPointer = null;
725
+ }
726
+ cancelResizeDrag() {
727
+ this.dragSnapshot && (this.graph = this.dragSnapshot, this.dragSnapshot = null, this.render()), this.dragState = null, this.detachDragListeners(), this.detachKeyListener();
728
+ }
729
+ refreshAreaDrag() {
730
+ !this.areaDragState || !this.lastPointer || this.updateAreaDragAt(this.lastPointer.x, this.lastPointer.y);
731
+ }
732
+ setDragCursor(e) {
733
+ this.rootEl && (this.dragCursor === null && (this.dragCursor = this.rootEl.style.cursor || ""), this.rootEl.style.cursor = e);
734
+ }
735
+ resetDragCursor(e = !1) {
736
+ this.rootEl && (this.dragCursor !== null || e) && (this.rootEl.style.cursor = this.dragCursor ?? "", this.dragCursor = null);
737
+ }
738
+ cloneGraph(e) {
739
+ let t = {};
740
+ for (let [n, r] of Object.entries(e.verts)) t[n] = { ...r };
741
+ let n = {};
742
+ for (let [t, r] of Object.entries(e.edges)) n[t] = { ...r };
743
+ let r = {};
744
+ for (let [t, n] of Object.entries(e.areas)) r[t] = { ...n };
745
+ return {
746
+ verts: t,
747
+ edges: n,
748
+ areas: r
749
+ };
750
+ }
751
+ detachDragListeners() {
752
+ window.removeEventListener("pointermove", this.onPointerMove), window.removeEventListener("pointerup", this.onPointerUp);
753
+ }
754
+ detachKeyListener() {
755
+ window.removeEventListener("keydown", this.onKeyDown), window.removeEventListener("keyup", this.onKeyUp);
756
+ }
757
+ startAreaDrag(e, t, n) {
758
+ this.rootEl && (this.areaDragState = {
759
+ sourceAreaId: t,
760
+ pointerId: e.pointerId,
761
+ startX: e.clientX,
762
+ startY: e.clientY,
763
+ lastX: e.clientX,
764
+ lastY: e.clientY,
765
+ axis: null,
766
+ swapMode: e.ctrlKey,
767
+ originCorner: n,
768
+ moved: !1
769
+ }, e.target instanceof HTMLElement && e.target.setPointerCapture(e.pointerId), window.addEventListener("pointermove", this.onPointerMove), window.addEventListener("pointerup", this.onPointerUp), window.addEventListener("keydown", this.onKeyDown), window.addEventListener("keyup", this.onKeyUp));
770
+ }
771
+ updateAreaDrag(e) {
772
+ !this.rootEl || !this.graph || !this.areaDragState || (this.areaDragState.swapMode = e.ctrlKey, this.updateAreaDragAt(e.clientX, e.clientY));
773
+ }
774
+ updateAreaDragAt(e, t) {
775
+ if (!this.rootEl || !this.graph || !this.areaDragState) return;
776
+ this.lastPointer = {
777
+ x: e,
778
+ y: t
779
+ };
780
+ let n = e - this.areaDragState.startX, r = t - this.areaDragState.startY;
781
+ if (Math.hypot(n, r) > CLICK_TOLERANCE_PX && (this.areaDragState.moved = !0), Math.hypot(n, r) <= DRAG_PARK_TOLERANCE_PX) {
782
+ this.hideDropOverlay(), this.hideJoinShade(), this.hideDragLabel(), this.resetDragCursor();
783
+ return;
784
+ }
785
+ let i = this.areaDragState.sourceAreaId, a = this.findAreaAtPoint(e, t);
786
+ if (!a) {
787
+ this.hideDropOverlay(), this.hideJoinShade(), this.hideDragLabel(), this.resetDragCursor();
788
+ return;
789
+ }
790
+ if (a.areaId === i) {
791
+ let n = this.canSplitRect(a.rect, "vertical"), r = this.canSplitRect(a.rect, "horizontal");
792
+ if (!n && !r) {
793
+ this.hideDropOverlay(), this.hideDragLabel(), this.resetDragCursor();
794
+ return;
795
+ }
796
+ if (n !== r) {
797
+ let r = n ? "vertical" : "horizontal", i = this.getSplitZoneByAxis(a.rect, e, t, r), o = this.getSplitOverlayRect(a.rect, i, e, t);
798
+ this.showSplitOverlay(a, i, o), this.hideJoinShade(), this.hideDragLabel(), this.setDragCursor(r === "vertical" ? "col-resize" : "row-resize");
799
+ return;
800
+ }
801
+ let i = this.resolveSplitGesture(this.areaDragState, e, t);
802
+ if (!i) {
803
+ this.hideDropOverlay(), this.hideDragLabel(), this.resetDragCursor();
804
+ return;
805
+ }
806
+ let o = this.getSplitZone(a.rect, e, t, i), s = this.getSplitOverlayRect(a.rect, o, e, t);
807
+ this.showSplitOverlay(a, o, s), this.hideJoinShade(), this.hideDragLabel(), this.setDragCursor(i.axis === "vertical" ? "col-resize" : "row-resize");
808
+ return;
809
+ }
810
+ if (this.areaDragState.swapMode) {
811
+ this.showDropOverlay({
812
+ areaId: a.areaId,
813
+ rect: a.rect
814
+ }, "center", "swap", a.rect), this.hideJoinShade(), this.showDragLabel(e, t, "Swap"), this.setDragCursor("copy"), this.lastDropTarget = {
815
+ areaId: a.areaId,
816
+ rect: a.rect,
817
+ zone: "center",
818
+ mode: "swap"
819
+ };
820
+ return;
821
+ }
822
+ let o = this.findJoinTargetAtPoint(i, e, t);
823
+ if (!o) {
824
+ let n = this.getMoveZone(a.rect, e, t);
825
+ if (n === "center") {
826
+ this.hideDropOverlay(), this.hideJoinShade(), this.hideDragLabel(), this.resetDragCursor();
827
+ return;
828
+ }
829
+ let r = this.getMovePreview(a.rect, n, e, t);
830
+ if (r.replace) {
831
+ this.showDropOverlay({
832
+ areaId: a.areaId,
833
+ rect: a.rect
834
+ }, n, "replace", a.rect), this.hideJoinShade(), this.showDragLabel(e, t, "Replace"), this.setDragCursor("alias"), this.lastDropTarget = {
835
+ areaId: a.areaId,
836
+ rect: a.rect,
837
+ zone: n,
838
+ mode: "replace"
839
+ };
840
+ return;
841
+ }
842
+ this.showDropOverlay({
843
+ areaId: a.areaId,
844
+ rect: a.rect
845
+ }, n, "move", r.overlay), this.hideJoinShade(), this.showDragLabel(e, t, "Move"), this.setDragCursor("move"), this.lastDropTarget = {
846
+ areaId: a.areaId,
847
+ rect: a.rect,
848
+ zone: n,
849
+ mode: "move",
850
+ moveRect: r.overlay,
851
+ remainderRect: r.remainder
852
+ };
853
+ return;
854
+ }
855
+ this.showDropOverlay({
856
+ areaId: o.areaId,
857
+ rect: o.rect
858
+ }, o.zone, "join", o.result, o.direction), this.showJoinShade(o.result, o.sourceRect, o.rect), this.showDragLabel(e, t, this.getJoinLabel(o.direction)), this.setDragCursor("pointer");
859
+ }
860
+ finishAreaDrag() {
861
+ if (!this.areaDragState || !this.graph) return;
862
+ let { sourceAreaId: e } = this.areaDragState, t = this.lastPointer, n = this.lastDropTarget;
863
+ if (this.areaDragState = null, this.hideDropOverlay(), this.hideJoinShade(), this.hideDragLabel(), this.detachDragListeners(), this.detachKeyListener(), this.resetDragCursor(!0), this.lastPointer = null, n) {
864
+ if (n.mode === "split") {
865
+ if (n.zone !== "center") {
866
+ let r = n.zone === "left" || n.zone === "right" ? "vertical" : "horizontal";
867
+ if (!this.canSplitRect(n.rect, r)) return;
868
+ this.split(e, n.zone, t?.x ?? 0, t?.y ?? 0);
869
+ }
870
+ return;
871
+ }
872
+ if (n.mode === "replace") {
873
+ this.replace(e, n.areaId);
874
+ return;
875
+ }
876
+ if (n.mode === "move") {
877
+ if (!n.moveRect || !n.remainderRect || n.zone === "center") return;
878
+ this.move(e, n.areaId, n.moveRect, n.remainderRect);
879
+ return;
880
+ }
881
+ if (n.mode === "swap") {
882
+ this.swap(e, n.areaId);
883
+ return;
884
+ }
885
+ if (n.areaId !== e) {
886
+ if (n.zone === "center") {
887
+ this.swap(e, n.areaId);
888
+ return;
889
+ }
890
+ (n.zone === "left" || n.zone === "right" || n.zone === "top" || n.zone === "bottom") && this.join(e, n.areaId);
891
+ }
892
+ }
893
+ }
894
+ lastDropTarget = null;
895
+ showDropOverlay(t, n, r, i, a) {
896
+ if (!this.rootEl) return;
897
+ let o = this.rootEl.getBoundingClientRect(), s = Math.max(o.width, 1), c = Math.max(o.height, 1), l = i ?? this.getZoneRect(t.rect, n, r);
898
+ this.dropOverlay || (this.dropOverlay = document.createElement("div"), this.dropOverlay.classList.add("sliced-areas-drop"), this.dropOverlay.setAttribute(INTERNAL_ATTR, "true")), this.dropOverlay.isConnected || this.rootEl.appendChild(this.dropOverlay), this.dropOverlay.innerHTML = "", this.dropOverlay.style.left = `${l.left * s}px`, this.dropOverlay.style.top = `${(1 - l.top) * c}px`, this.dropOverlay.style.width = `${(l.right - l.left) * s}px`, this.dropOverlay.style.height = `${(l.top - l.bottom) * c}px`, this.dropOverlay.dataset.zone = n, this.dropOverlay.dataset.mode = r, delete this.dropOverlay.dataset.splitMode, this.dropOverlay.style.display = "block", this.lastDropTarget = {
899
+ areaId: t.areaId,
900
+ rect: t.rect,
901
+ zone: n,
902
+ mode: r,
903
+ direction: a
904
+ };
905
+ }
906
+ showSplitOverlay(t, n, r) {
907
+ if (!this.rootEl) return;
908
+ let i = this.rootEl.getBoundingClientRect(), a = Math.max(i.width, 1), o = Math.max(i.height, 1);
909
+ this.dropOverlay || (this.dropOverlay = document.createElement("div"), this.dropOverlay.classList.add("sliced-areas-drop"), this.dropOverlay.setAttribute(INTERNAL_ATTR, "true")), this.dropOverlay.isConnected || this.rootEl.appendChild(this.dropOverlay), this.dropOverlay.style.left = "0px", this.dropOverlay.style.top = "0px", this.dropOverlay.style.width = `${a}px`, this.dropOverlay.style.height = `${o}px`, this.dropOverlay.dataset.zone = "split", this.dropOverlay.dataset.mode = "split", this.dropOverlay.dataset.splitMode = "true", this.dropOverlay.innerHTML = "";
910
+ let s = r, c;
911
+ c = n === "left" ? {
912
+ left: r.right,
913
+ right: t.rect.right,
914
+ top: t.rect.top,
915
+ bottom: t.rect.bottom
916
+ } : n === "right" ? {
917
+ left: t.rect.left,
918
+ right: r.left,
919
+ top: t.rect.top,
920
+ bottom: t.rect.bottom
921
+ } : n === "bottom" ? {
922
+ left: t.rect.left,
923
+ right: t.rect.right,
924
+ top: t.rect.top,
925
+ bottom: r.top
926
+ } : {
927
+ left: t.rect.left,
928
+ right: t.rect.right,
929
+ top: r.bottom,
930
+ bottom: t.rect.bottom
931
+ };
932
+ for (let e of [s, c]) {
933
+ let t = document.createElement("div");
934
+ t.classList.add("sliced-areas-drop-piece"), t.style.left = `${e.left * a}px`, t.style.top = `${(1 - e.top) * o}px`, t.style.width = `${(e.right - e.left) * a}px`, t.style.height = `${(e.top - e.bottom) * o}px`, this.dropOverlay.appendChild(t);
935
+ }
936
+ this.dropOverlay.style.display = "block", this.lastDropTarget = {
937
+ areaId: t.areaId,
938
+ rect: t.rect,
939
+ zone: n,
940
+ mode: "split"
941
+ };
942
+ }
943
+ getMovePreview(e, t, n, a) {
944
+ if (!this.rootEl) return {
945
+ overlay: e,
946
+ remainder: e,
947
+ replace: !0
948
+ };
949
+ let o = this.rootEl.getBoundingClientRect(), s = (n - o.left) / o.width, c = 1 - (a - o.top) / o.height, l = MOVE_SNAP_PX / Math.max(o.width, 1), u = MOVE_SNAP_PX / Math.max(o.height, 1), d = (e.left + e.right) / 2, f = (e.bottom + e.top) / 2, h = e.right - e.left, g = e.top - e.bottom, _ = Math.min(h, g) * REPLACE_THRESHOLD_RATIO / 2, v = d - _, y = d + _, b = f - _, x = f + _, S = s >= v && s <= y && c >= b && c <= x;
950
+ if (t === "left" || t === "right") {
951
+ let n = t === "left" ? 2 * s - e.left : 2 * s - e.right;
952
+ Math.abs(n - d) <= l && (n = d), n = Math.min(Math.max(n, e.left + MIN_RATIO), e.right - MIN_RATIO);
953
+ let a = t === "left" ? {
954
+ left: e.left,
955
+ right: n,
956
+ top: e.top,
957
+ bottom: e.bottom
958
+ } : {
959
+ left: n,
960
+ right: e.right,
961
+ top: e.top,
962
+ bottom: e.bottom
963
+ }, o = t === "left" ? {
964
+ left: n,
965
+ right: e.right,
966
+ top: e.top,
967
+ bottom: e.bottom
968
+ } : {
969
+ left: e.left,
970
+ right: n,
971
+ top: e.top,
972
+ bottom: e.bottom
973
+ }, c = h <= MIN_RATIO * 2 + EPS, u = S || c;
974
+ return {
975
+ overlay: u ? e : a,
976
+ remainder: u ? e : o,
977
+ replace: u
978
+ };
979
+ }
980
+ let C = t === "bottom" ? 2 * c - e.bottom : 2 * c - e.top;
981
+ Math.abs(C - f) <= u && (C = f), C = Math.min(Math.max(C, e.bottom + MIN_RATIO), e.top - MIN_RATIO);
982
+ let w = t === "bottom" ? {
983
+ left: e.left,
984
+ right: e.right,
985
+ top: C,
986
+ bottom: e.bottom
987
+ } : {
988
+ left: e.left,
989
+ right: e.right,
990
+ top: e.top,
991
+ bottom: C
992
+ }, T = t === "bottom" ? {
993
+ left: e.left,
994
+ right: e.right,
995
+ top: e.top,
996
+ bottom: C
997
+ } : {
998
+ left: e.left,
999
+ right: e.right,
1000
+ top: C,
1001
+ bottom: e.bottom
1002
+ }, E = g <= MIN_RATIO * 2 + EPS, D = S || E;
1003
+ return {
1004
+ overlay: D ? e : w,
1005
+ remainder: D ? e : T,
1006
+ replace: D
1007
+ };
1008
+ }
1009
+ hideDropOverlay() {
1010
+ this.dropOverlay && (this.dropOverlay.style.display = "none"), this.lastDropTarget = null;
1011
+ }
1012
+ showJoinShade(t, n, r) {
1013
+ if (!this.rootEl) return;
1014
+ this.dropShade || (this.dropShade = document.createElement("div"), this.dropShade.classList.add("sliced-areas-drop-dim"), this.dropShade.setAttribute(INTERNAL_ATTR, "true")), this.dropShade.isConnected || this.rootEl.appendChild(this.dropShade);
1015
+ let i = this.rootEl.getBoundingClientRect(), a = Math.max(i.width, 1), o = Math.max(i.height, 1);
1016
+ this.dropShade.innerHTML = "";
1017
+ let s = [...this.subtractRect(n, t), ...this.subtractRect(r, t)];
1018
+ for (let e of s) {
1019
+ let t = document.createElement("div");
1020
+ t.classList.add("sliced-areas-drop-dim-piece"), t.style.left = `${e.left * a}px`, t.style.top = `${(1 - e.top) * o}px`, t.style.width = `${(e.right - e.left) * a}px`, t.style.height = `${(e.top - e.bottom) * o}px`, this.dropShade.appendChild(t);
1021
+ }
1022
+ this.dropShade.style.display = s.length > 0 ? "block" : "none";
1023
+ }
1024
+ hideJoinShade() {
1025
+ this.dropShade && (this.dropShade.style.display = "none");
1026
+ }
1027
+ showDragLabel(t, n, r) {
1028
+ this.rootEl && (this.dragLabel || (this.dragLabel = document.createElement("div"), this.dragLabel.classList.add("sliced-areas-drag-label"), this.dragLabel.setAttribute(INTERNAL_ATTR, "true")), this.dragLabel.isConnected || this.rootEl.appendChild(this.dragLabel), this.dragLabel.textContent = r, this.dragLabel.style.left = `${t}px`, this.dragLabel.style.top = `${n}px`, this.dragLabel.style.display = "block");
1029
+ }
1030
+ hideDragLabel() {
1031
+ this.dragLabel && (this.dragLabel.style.display = "none");
1032
+ }
1033
+ getSplitLabel(e) {
1034
+ return e === "left" ? "Split Left" : e === "right" ? "Split Right" : e === "top" ? "Split Top" : e === "bottom" ? "Split Bottom" : "Split";
1035
+ }
1036
+ getJoinLabel(e) {
1037
+ return e === "left" ? "Join Left" : e === "right" ? "Join Right" : e === "up" ? "Join Up" : "Join Down";
1038
+ }
1039
+ findJoinTargetAtPoint(e, t, n) {
1040
+ if (!this.graph || !this.rootEl) return null;
1041
+ let r = this.graph.areas[e];
1042
+ if (!r) return null;
1043
+ let i = this.rootEl.getBoundingClientRect(), a = (t - i.left) / i.width, o = 1 - (n - i.top) / i.height, s = this.getAreaRect(this.graph, r), l = null;
1044
+ for (let t of Object.values(this.graph.areas)) {
1045
+ if (t.id === e) continue;
1046
+ let n = this.getAreaRect(this.graph, t);
1047
+ if (a < n.left || a > n.right || o < n.bottom || o > n.top) continue;
1048
+ let i = this.getOrientation(this.graph, r, t);
1049
+ if (i !== "none") if (i === "east" || i === "west") {
1050
+ let e = Math.max(s.bottom, n.bottom), r = Math.min(s.top, n.top);
1051
+ if (o < e || o > r) continue;
1052
+ let u = (n.right - n.left) * JOIN_ZONE_DEPTH, d = i === "east" ? a - n.left : n.right - a;
1053
+ if (d < 0 || d > u) continue;
1054
+ (!l || d < l.distance) && (l = {
1055
+ target: t,
1056
+ rect: n,
1057
+ distance: d,
1058
+ orientation: i
1059
+ });
1060
+ } else {
1061
+ let e = Math.max(s.left, n.left), r = Math.min(s.right, n.right);
1062
+ if (a < e || a > r) continue;
1063
+ let u = (n.top - n.bottom) * JOIN_ZONE_DEPTH, d = i === "north" ? o - n.bottom : n.top - o;
1064
+ if (d < 0 || d > u) continue;
1065
+ (!l || d < l.distance) && (l = {
1066
+ target: t,
1067
+ rect: n,
1068
+ distance: d,
1069
+ orientation: i
1070
+ });
1071
+ }
1072
+ }
1073
+ if (!l) return null;
1074
+ let u = l.orientation, d = u === "east" ? "left" : u === "west" ? "right" : u === "north" ? "bottom" : "top", f = u === "east" ? "right" : u === "west" ? "left" : u === "north" ? "up" : "down", p = this.getJoinOverlayRect(s, l.rect, u), m = this.getJoinResultRect(s, l.rect, u);
1075
+ return {
1076
+ areaId: l.target.id,
1077
+ rect: l.rect,
1078
+ zone: d,
1079
+ direction: f,
1080
+ overlay: p,
1081
+ result: m,
1082
+ sourceRect: s
1083
+ };
1084
+ }
1085
+ getJoinOverlayRect(e, t, n) {
1086
+ let r = Math.max(e.bottom, t.bottom), i = Math.min(e.top, t.top), a = Math.max(e.left, t.left), o = Math.min(e.right, t.right);
1087
+ return n === "east" || n === "west" ? {
1088
+ left: t.left,
1089
+ right: t.right,
1090
+ top: i,
1091
+ bottom: r
1092
+ } : {
1093
+ left: a,
1094
+ right: o,
1095
+ top: t.top,
1096
+ bottom: t.bottom
1097
+ };
1098
+ }
1099
+ getJoinResultRect(e, t, n) {
1100
+ let r = Math.max(e.bottom, t.bottom), i = Math.min(e.top, t.top), a = Math.max(e.left, t.left), o = Math.min(e.right, t.right);
1101
+ return n === "east" || n === "west" ? {
1102
+ left: Math.min(e.left, t.left),
1103
+ right: Math.max(e.right, t.right),
1104
+ top: i,
1105
+ bottom: r
1106
+ } : {
1107
+ left: a,
1108
+ right: o,
1109
+ top: Math.max(e.top, t.top),
1110
+ bottom: Math.min(e.bottom, t.bottom)
1111
+ };
1112
+ }
1113
+ subtractRect(e, t) {
1114
+ let n = Math.max(e.left, t.left), r = Math.min(e.right, t.right), i = Math.max(e.bottom, t.bottom), a = Math.min(e.top, t.top);
1115
+ if (n >= r || i >= a) return [e];
1116
+ let o = [];
1117
+ return e.top > a && o.push({
1118
+ left: e.left,
1119
+ right: e.right,
1120
+ top: e.top,
1121
+ bottom: a
1122
+ }), e.bottom < i && o.push({
1123
+ left: e.left,
1124
+ right: e.right,
1125
+ top: i,
1126
+ bottom: e.bottom
1127
+ }), e.left < n && o.push({
1128
+ left: e.left,
1129
+ right: n,
1130
+ top: a,
1131
+ bottom: i
1132
+ }), e.right > r && o.push({
1133
+ left: r,
1134
+ right: e.right,
1135
+ top: a,
1136
+ bottom: i
1137
+ }), o;
1138
+ }
1139
+ findAreaAtPoint(e, t) {
1140
+ if (!this.rootEl || !this.graph) return null;
1141
+ let n = this.rootEl.getBoundingClientRect(), r = (e - n.left) / n.width, i = 1 - (t - n.top) / n.height;
1142
+ for (let e of Object.values(this.graph.areas)) {
1143
+ let t = this.getAreaRect(this.graph, e);
1144
+ if (r >= t.left && r <= t.right && i >= t.bottom && i <= t.top) return {
1145
+ areaId: e.id,
1146
+ rect: t
1147
+ };
1148
+ }
1149
+ return null;
1150
+ }
1151
+ getMoveZone(e, t, n) {
1152
+ if (!this.rootEl) return "center";
1153
+ let r = this.rootEl.getBoundingClientRect(), a = (t - r.left) / r.width, o = 1 - (n - r.top) / r.height, s = (a - e.left) / Math.max(e.right - e.left, EPS), c = (o - e.bottom) / Math.max(e.top - e.bottom, EPS);
1154
+ return c >= s && c >= 1 - s ? "top" : c <= s && c <= 1 - s ? "bottom" : c >= s && c <= 1 - s ? "left" : "right";
1155
+ }
1156
+ resolveSplitGesture(e, t, n) {
1157
+ let r = t - e.startX, i = n - e.startY, a = Math.abs(r), o = Math.abs(i);
1158
+ if (e.axis) {
1159
+ let r = t - e.lastX, i = n - e.lastY, a = Math.abs(r), o = Math.abs(i);
1160
+ Math.max(a, o) >= SPLIT_GESTURE_THRESHOLD_PX && (e.axis === "vertical" && o - a >= SPLIT_AXIS_TOLERANCE_PX ? e.axis = "horizontal" : e.axis === "horizontal" && a - o >= SPLIT_AXIS_TOLERANCE_PX && (e.axis = "vertical"));
1161
+ } else {
1162
+ if (Math.max(a, o) < SPLIT_GESTURE_THRESHOLD_PX) return null;
1163
+ if (a - o >= SPLIT_AXIS_TOLERANCE_PX) e.axis = "vertical";
1164
+ else if (o - a >= SPLIT_AXIS_TOLERANCE_PX) e.axis = "horizontal";
1165
+ else return null;
1166
+ }
1167
+ return e.lastX = t, e.lastY = n, { axis: e.axis };
1168
+ }
1169
+ getSplitZone(e, t, n, r) {
1170
+ if (!this.rootEl) return r.axis === "vertical" ? "right" : "top";
1171
+ let a = this.rootEl.getBoundingClientRect(), o = (t - a.left) / a.width, s = 1 - (n - a.top) / a.height, c = (o - e.left) / Math.max(e.right - e.left, EPS), l = (s - e.bottom) / Math.max(e.top - e.bottom, EPS), u = c, d = l < .5, f = u < .5, p = !d, m = !f, h = r.axis === "horizontal" ? u : l;
1172
+ return (m && d || f && p) && (h = 1 - h), r.axis === "vertical" ? h > .5 ? "right" : "left" : h > .5 ? "top" : "bottom";
1173
+ }
1174
+ getSplitZoneByAxis(e, t, n, r) {
1175
+ if (!this.rootEl) return r === "vertical" ? "right" : "top";
1176
+ let a = this.rootEl.getBoundingClientRect(), o = (t - a.left) / a.width, s = 1 - (n - a.top) / a.height, c = (o - e.left) / Math.max(e.right - e.left, EPS), l = (s - e.bottom) / Math.max(e.top - e.bottom, EPS);
1177
+ return r === "vertical" ? c < .5 ? "left" : "right" : l < .5 ? "bottom" : "top";
1178
+ }
1179
+ canSplitRect(e, t) {
1180
+ let n = e.right - e.left, i = e.top - e.bottom;
1181
+ return t === "vertical" ? n > MIN_RATIO * 2 : i > MIN_RATIO * 2;
1182
+ }
1183
+ getZoneRect(e, t, n) {
1184
+ let r = n === "join" ? .25 : .5;
1185
+ return t === "left" ? {
1186
+ left: e.left,
1187
+ right: e.left + (e.right - e.left) * r,
1188
+ top: e.top,
1189
+ bottom: e.bottom
1190
+ } : t === "right" ? {
1191
+ left: e.right - (e.right - e.left) * r,
1192
+ right: e.right,
1193
+ top: e.top,
1194
+ bottom: e.bottom
1195
+ } : t === "bottom" ? {
1196
+ left: e.left,
1197
+ right: e.right,
1198
+ top: e.bottom + (e.top - e.bottom) * r,
1199
+ bottom: e.bottom
1200
+ } : t === "top" ? {
1201
+ left: e.left,
1202
+ right: e.right,
1203
+ top: e.top,
1204
+ bottom: e.top - (e.top - e.bottom) * r
1205
+ } : e;
1206
+ }
1207
+ getSplitOverlayRect(e, t, n, i) {
1208
+ if (!this.rootEl) return e;
1209
+ let a = this.rootEl.getBoundingClientRect(), o = (n - a.left) / a.width, s = 1 - (i - a.top) / a.height, c = Math.min(Math.max(o, e.left + MIN_RATIO), e.right - MIN_RATIO), l = Math.min(Math.max(s, e.bottom + MIN_RATIO), e.top - MIN_RATIO);
1210
+ return t === "left" ? {
1211
+ left: e.left,
1212
+ right: c,
1213
+ top: e.top,
1214
+ bottom: e.bottom
1215
+ } : t === "right" ? {
1216
+ left: c,
1217
+ right: e.right,
1218
+ top: e.top,
1219
+ bottom: e.bottom
1220
+ } : t === "bottom" ? {
1221
+ left: e.left,
1222
+ right: e.right,
1223
+ top: l,
1224
+ bottom: e.bottom
1225
+ } : t === "top" ? {
1226
+ left: e.left,
1227
+ right: e.right,
1228
+ top: e.top,
1229
+ bottom: l
1230
+ } : e;
1231
+ }
1232
+ moveEdge(e, t, n, r, a, o) {
1233
+ if (Math.abs(n - r) <= EPS) return null;
1234
+ let s = {
1235
+ verts: { ...e.verts },
1236
+ edges: { ...e.edges },
1237
+ areas: { ...e.areas }
1238
+ }, c = this.collectConnectedVerts(s, t, n, a, o);
1239
+ for (let e of c) {
1240
+ let n = s.verts[e];
1241
+ n && (t === "vertical" ? n.x = r : n.y = r);
1242
+ }
1243
+ return s;
1244
+ }
1245
+ getEdgeDragBounds(e, t, n, a, o) {
1246
+ let s = 0, c = 1, l = Math.min(a, o) - EPS, u = Math.max(a, o) + EPS, d = !1;
1247
+ for (let a of Object.values(e.areas)) {
1248
+ let o = this.getAreaRect(e, a);
1249
+ if (t === "vertical") {
1250
+ if (!(o.bottom <= u && o.top >= l)) continue;
1251
+ Math.abs(o.right - n) <= EPS && (s = Math.max(s, o.left + MIN_RATIO), d = !0), Math.abs(o.left - n) <= EPS && (c = Math.min(c, o.right - MIN_RATIO), d = !0);
1252
+ } else {
1253
+ if (!(o.left <= u && o.right >= l)) continue;
1254
+ Math.abs(o.top - n) <= EPS && (s = Math.max(s, o.bottom + MIN_RATIO), d = !0), Math.abs(o.bottom - n) <= EPS && (c = Math.min(c, o.top - MIN_RATIO), d = !0);
1255
+ }
1256
+ }
1257
+ return !d || s >= c ? null : {
1258
+ min: s,
1259
+ max: c
1260
+ };
1261
+ }
1262
+ getAreaRect(e, t) {
1263
+ let n = e.verts[t.v1], r = e.verts[t.v2], i = e.verts[t.v3], a = e.verts[t.v4];
1264
+ if (!n || !r || !i || !a) throw Error(`Invalid area vertices for ${t.id}`);
1265
+ return {
1266
+ left: Math.min(n.x, r.x, i.x, a.x),
1267
+ right: Math.max(n.x, r.x, i.x, a.x),
1268
+ bottom: Math.min(n.y, r.y, i.y, a.y),
1269
+ top: Math.max(n.y, r.y, i.y, a.y)
1270
+ };
1271
+ }
1272
+ addVert(e, t, n) {
1273
+ let r = this.nextVertId(e);
1274
+ return e[r] = {
1275
+ id: r,
1276
+ x: t,
1277
+ y: n
1278
+ }, r;
1279
+ }
1280
+ addEdge(e, t, n) {
1281
+ let r = this.nextEdgeId(e);
1282
+ return e[r] = {
1283
+ id: r,
1284
+ v1: t,
1285
+ v2: n,
1286
+ border: !1
1287
+ }, r;
1288
+ }
1289
+ nextVertId(e) {
1290
+ let t = "";
1291
+ do
1292
+ this.vertCounter += 1, t = `v${this.vertCounter}`;
1293
+ while (e[t]);
1294
+ return t;
1295
+ }
1296
+ nextEdgeId(e) {
1297
+ let t = "";
1298
+ do
1299
+ this.edgeCounter += 1, t = `e${this.edgeCounter}`;
1300
+ while (e[t]);
1301
+ return t;
1302
+ }
1303
+ nextAreaId() {
1304
+ let e = "";
1305
+ do
1306
+ this.areaCounter += 1, e = `area-${this.areaCounter}`;
1307
+ while (this.graph?.areas[e] || this.querySelector(`[data-area-id="${e}"]`));
1308
+ return e;
1309
+ }
1310
+ normalizeGraph(e) {
1311
+ let t = this.normalizeGraphInternal(e), n = this.fillHoles(t);
1312
+ return this.normalizeGraphInternal(n);
1313
+ }
1314
+ normalizeGraphInternal(e) {
1315
+ let t = (e) => `${e.x.toFixed(6)}|${e.y.toFixed(6)}`, n = /* @__PURE__ */ new Map(), r = {}, i = {};
1316
+ for (let a of Object.values(e.verts)) {
1317
+ let e = t(a), o = n.get(e);
1318
+ o ? r[a.id] = o : (n.set(e, a.id), i[a.id] = { ...a });
1319
+ }
1320
+ let a = {}, o = /* @__PURE__ */ new Map();
1321
+ for (let t of Object.values(e.edges)) {
1322
+ let e = r[t.v1] ?? t.v1, n = r[t.v2] ?? t.v2, i = e < n ? `${e}|${n}` : `${n}|${e}`;
1323
+ if (o.has(i)) continue;
1324
+ let s = this.nextEdgeId(a);
1325
+ a[s] = {
1326
+ id: s,
1327
+ v1: e,
1328
+ v2: n,
1329
+ border: t.border
1330
+ }, o.set(i, s);
1331
+ }
1332
+ let s = {};
1333
+ for (let t of Object.values(e.areas)) s[t.id] = {
1334
+ id: t.id,
1335
+ v1: r[t.v1] ?? t.v1,
1336
+ v2: r[t.v2] ?? t.v2,
1337
+ v3: r[t.v3] ?? t.v3,
1338
+ v4: r[t.v4] ?? t.v4
1339
+ };
1340
+ let c = /* @__PURE__ */ new Set(), l = /* @__PURE__ */ new Set();
1341
+ for (let e of Object.values(s)) {
1342
+ let t = [
1343
+ [e.v1, e.v2],
1344
+ [e.v2, e.v3],
1345
+ [e.v3, e.v4],
1346
+ [e.v4, e.v1]
1347
+ ];
1348
+ for (let [e, n] of t) l.add(e < n ? `${e}|${n}` : `${n}|${e}`);
1349
+ }
1350
+ for (let e of Object.values(a)) {
1351
+ let t = e.v1 < e.v2 ? `${e.v1}|${e.v2}` : `${e.v2}|${e.v1}`;
1352
+ l.has(t) && c.add(e.id);
1353
+ }
1354
+ let u = {};
1355
+ for (let e of Object.values(a)) c.has(e.id) && (u[e.id] = e);
1356
+ let d = /* @__PURE__ */ new Set();
1357
+ for (let e of Object.values(u)) d.add(e.v1), d.add(e.v2);
1358
+ let f = {};
1359
+ for (let e of Object.values(i)) d.has(e.id) && (f[e.id] = e);
1360
+ return {
1361
+ verts: f,
1362
+ edges: u,
1363
+ areas: s
1364
+ };
1365
+ }
1366
+ fillHoles(e) {
1367
+ let t = e, n = 0;
1368
+ for (; n < 50;) {
1369
+ let e = this.findHoleCells(t);
1370
+ if (e.length === 0) {
1371
+ let e = this.findOverlaps(t);
1372
+ return e.length > 0 && this.throwOverlapError(t, e), t;
1373
+ }
1374
+ let r = !1;
1375
+ for (let n of e) {
1376
+ let e = this.findHoleFillPlan(t, n);
1377
+ if (!e) {
1378
+ let e = this.tryAlignHoleNeighbors(t, n);
1379
+ if (e.changed) {
1380
+ t = e.graph, r = !0;
1381
+ break;
1382
+ }
1383
+ continue;
1384
+ }
1385
+ for (let r of e.neighbors) {
1386
+ let i = this.expandRectIntoHole(r.rect, n, e.side);
1387
+ t = this.setAreaRect(t, r.areaId, i);
1388
+ }
1389
+ r = !0;
1390
+ }
1391
+ r || this.throwHoleError(t, e), n += 1;
1392
+ }
1393
+ this.throwHoleError(t, this.findHoleCells(t));
1394
+ }
1395
+ findHoleCells(e) {
1396
+ let t = Object.values(e.areas).map((t) => this.getAreaRect(e, t)), n = this.collectAxisCoords(t, "x"), r = this.collectAxisCoords(t, "y"), a = [];
1397
+ for (let e = 0; e < n.length - 1; e += 1) {
1398
+ let o = n[e], s = n[e + 1];
1399
+ if (!(o === void 0 || s === void 0)) for (let e = 0; e < r.length - 1; e += 1) {
1400
+ let n = r[e], c = r[e + 1];
1401
+ if (n === void 0 || c === void 0) continue;
1402
+ let l = {
1403
+ left: o,
1404
+ right: s,
1405
+ bottom: n,
1406
+ top: c
1407
+ };
1408
+ l.right - l.left <= EPS || l.top - l.bottom <= EPS || this.isCellCovered(t, l) || a.push(l);
1409
+ }
1410
+ }
1411
+ return this.mergeHoleCells(a);
1412
+ }
1413
+ collectAxisCoords(e, t) {
1414
+ let n = [0, 1];
1415
+ for (let r of e) t === "x" ? n.push(r.left, r.right) : n.push(r.bottom, r.top);
1416
+ n.sort((e, t) => e - t);
1417
+ let r = [];
1418
+ for (let e of n) {
1419
+ let t = r[r.length - 1];
1420
+ (t === void 0 || Math.abs(e - t) > ALIGN_TOLERANCE) && r.push(e);
1421
+ }
1422
+ return r;
1423
+ }
1424
+ mergeHoleCells(e) {
1425
+ let t = [...e], n = !0;
1426
+ for (; n;) {
1427
+ n = !1;
1428
+ outer: for (let e = 0; e < t.length; e += 1) for (let r = e + 1; r < t.length; r += 1) {
1429
+ let i = t[e], o = t[r];
1430
+ if (!i || !o) continue;
1431
+ let s = Math.abs(i.left - o.left) <= ALIGN_TOLERANCE && Math.abs(i.right - o.right) <= ALIGN_TOLERANCE && (Math.abs(i.top - o.bottom) <= ALIGN_TOLERANCE || Math.abs(o.top - i.bottom) <= ALIGN_TOLERANCE), c = Math.abs(i.bottom - o.bottom) <= ALIGN_TOLERANCE && Math.abs(i.top - o.top) <= ALIGN_TOLERANCE && (Math.abs(i.right - o.left) <= ALIGN_TOLERANCE || Math.abs(o.right - i.left) <= ALIGN_TOLERANCE);
1432
+ if (s || c) {
1433
+ let a = {
1434
+ left: Math.min(i.left, o.left),
1435
+ right: Math.max(i.right, o.right),
1436
+ bottom: Math.min(i.bottom, o.bottom),
1437
+ top: Math.max(i.top, o.top)
1438
+ };
1439
+ t.splice(r, 1), t.splice(e, 1, a), n = !0;
1440
+ break outer;
1441
+ }
1442
+ }
1443
+ }
1444
+ return t;
1445
+ }
1446
+ tryAlignHoleNeighbors(e, t) {
1447
+ let n = e, r = !1;
1448
+ for (let e of [
1449
+ "west",
1450
+ "east",
1451
+ "south",
1452
+ "north"
1453
+ ]) {
1454
+ let i = this.collectAdjacentNeighbors(n, t, e);
1455
+ for (let a of i) {
1456
+ let i = this.alignNeighborToHole(n, a.areaId, t, e);
1457
+ i.changed && (n = i.graph, r = !0);
1458
+ }
1459
+ }
1460
+ return {
1461
+ graph: n,
1462
+ changed: r
1463
+ };
1464
+ }
1465
+ collectAdjacentNeighbors(e, t, n) {
1466
+ let r = [];
1467
+ for (let o of Object.values(e.areas)) {
1468
+ let s = this.getAreaRect(e, o);
1469
+ if (n === "west") {
1470
+ if (Math.abs(s.right - t.left) > ALIGN_TOLERANCE || Math.min(s.top, t.top) - Math.max(s.bottom, t.bottom) <= EPS) continue;
1471
+ r.push({
1472
+ areaId: o.id,
1473
+ rect: s
1474
+ });
1475
+ } else if (n === "east") {
1476
+ if (Math.abs(s.left - t.right) > ALIGN_TOLERANCE || Math.min(s.top, t.top) - Math.max(s.bottom, t.bottom) <= EPS) continue;
1477
+ r.push({
1478
+ areaId: o.id,
1479
+ rect: s
1480
+ });
1481
+ } else if (n === "south") {
1482
+ if (Math.abs(s.top - t.bottom) > ALIGN_TOLERANCE || Math.min(s.right, t.right) - Math.max(s.left, t.left) <= EPS) continue;
1483
+ r.push({
1484
+ areaId: o.id,
1485
+ rect: s
1486
+ });
1487
+ } else {
1488
+ if (Math.abs(s.bottom - t.top) > ALIGN_TOLERANCE || Math.min(s.right, t.right) - Math.max(s.left, t.left) <= EPS) continue;
1489
+ r.push({
1490
+ areaId: o.id,
1491
+ rect: s
1492
+ });
1493
+ }
1494
+ }
1495
+ return r;
1496
+ }
1497
+ alignNeighborToHole(e, t, n, r) {
1498
+ let i = e, o = !1, s = i.areas[t];
1499
+ if (!s) return {
1500
+ graph: i,
1501
+ changed: o
1502
+ };
1503
+ let c = this.getAreaRect(i, s);
1504
+ if (r === "west" || r === "east") {
1505
+ if (c.bottom < n.bottom - ALIGN_TOLERANCE) {
1506
+ let e = this.nextAreaId(), r = this.splitAreaAt(i, t, "horizontal", n.bottom, e, "max");
1507
+ if (r) {
1508
+ if (i = r, this.ensureAreaNode(e, t, !0), o = !0, s = i.areas[t], !s) return {
1509
+ graph: i,
1510
+ changed: o
1511
+ };
1512
+ c = this.getAreaRect(i, s);
1513
+ }
1514
+ }
1515
+ if (c.top > n.top + ALIGN_TOLERANCE) {
1516
+ let e = this.nextAreaId(), r = this.splitAreaAt(i, t, "horizontal", n.top, e, "min");
1517
+ r && (i = r, this.ensureAreaNode(e, t, !0), o = !0);
1518
+ }
1519
+ } else {
1520
+ if (c.left < n.left - ALIGN_TOLERANCE) {
1521
+ let e = this.nextAreaId(), r = this.splitAreaAt(i, t, "vertical", n.left, e, "max");
1522
+ if (r) {
1523
+ if (i = r, this.ensureAreaNode(e, t, !0), o = !0, s = i.areas[t], !s) return {
1524
+ graph: i,
1525
+ changed: o
1526
+ };
1527
+ c = this.getAreaRect(i, s);
1528
+ }
1529
+ }
1530
+ if (c.right > n.right + ALIGN_TOLERANCE) {
1531
+ let e = this.nextAreaId(), r = this.splitAreaAt(i, t, "vertical", n.right, e, "min");
1532
+ r && (i = r, this.ensureAreaNode(e, t, !0), o = !0);
1533
+ }
1534
+ }
1535
+ return {
1536
+ graph: i,
1537
+ changed: o
1538
+ };
1539
+ }
1540
+ isCellCovered(e, t) {
1541
+ let n = (t.left + t.right) / 2, r = (t.bottom + t.top) / 2;
1542
+ for (let t of e) if (n >= t.left - ALIGN_TOLERANCE && n <= t.right + ALIGN_TOLERANCE && r >= t.bottom - ALIGN_TOLERANCE && r <= t.top + ALIGN_TOLERANCE) return !0;
1543
+ return !1;
1544
+ }
1545
+ findHoleFillPlan(e, t) {
1546
+ let n = [
1547
+ this.collectHoleNeighbors(e, t, "west"),
1548
+ this.collectHoleNeighbors(e, t, "east"),
1549
+ this.collectHoleNeighbors(e, t, "south"),
1550
+ this.collectHoleNeighbors(e, t, "north")
1551
+ ];
1552
+ for (let e of n) if (e && this.isHoleSideCovered(t, e.side, e.segments)) return {
1553
+ side: e.side,
1554
+ neighbors: e.neighbors
1555
+ };
1556
+ return null;
1557
+ }
1558
+ collectHoleNeighbors(e, t, n) {
1559
+ let r = [], o = [];
1560
+ for (let s of Object.values(e.areas)) {
1561
+ let c = this.getAreaRect(e, s);
1562
+ if (n === "west") {
1563
+ if (Math.abs(c.right - t.left) > ALIGN_TOLERANCE || c.bottom < t.bottom - ALIGN_TOLERANCE || c.top > t.top + ALIGN_TOLERANCE) continue;
1564
+ let e = Math.max(c.bottom, t.bottom), n = Math.min(c.top, t.top);
1565
+ if (n - e <= EPS) continue;
1566
+ r.push({
1567
+ areaId: s.id,
1568
+ rect: c
1569
+ }), o.push({
1570
+ start: e,
1571
+ end: n
1572
+ });
1573
+ } else if (n === "east") {
1574
+ if (Math.abs(c.left - t.right) > ALIGN_TOLERANCE || c.bottom < t.bottom - ALIGN_TOLERANCE || c.top > t.top + ALIGN_TOLERANCE) continue;
1575
+ let e = Math.max(c.bottom, t.bottom), n = Math.min(c.top, t.top);
1576
+ if (n - e <= EPS) continue;
1577
+ r.push({
1578
+ areaId: s.id,
1579
+ rect: c
1580
+ }), o.push({
1581
+ start: e,
1582
+ end: n
1583
+ });
1584
+ } else if (n === "south") {
1585
+ if (Math.abs(c.top - t.bottom) > ALIGN_TOLERANCE || c.left < t.left - ALIGN_TOLERANCE || c.right > t.right + ALIGN_TOLERANCE) continue;
1586
+ let e = Math.max(c.left, t.left), n = Math.min(c.right, t.right);
1587
+ if (n - e <= EPS) continue;
1588
+ r.push({
1589
+ areaId: s.id,
1590
+ rect: c
1591
+ }), o.push({
1592
+ start: e,
1593
+ end: n
1594
+ });
1595
+ } else {
1596
+ if (Math.abs(c.bottom - t.top) > ALIGN_TOLERANCE || c.left < t.left - ALIGN_TOLERANCE || c.right > t.right + ALIGN_TOLERANCE) continue;
1597
+ let e = Math.max(c.left, t.left), n = Math.min(c.right, t.right);
1598
+ if (n - e <= EPS) continue;
1599
+ r.push({
1600
+ areaId: s.id,
1601
+ rect: c
1602
+ }), o.push({
1603
+ start: e,
1604
+ end: n
1605
+ });
1606
+ }
1607
+ }
1608
+ return r.length === 0 ? null : {
1609
+ side: n,
1610
+ neighbors: r,
1611
+ segments: o
1612
+ };
1613
+ }
1614
+ isHoleSideCovered(e, t, n) {
1615
+ let r = t === "west" || t === "east" ? e.bottom : e.left, i = t === "west" || t === "east" ? e.top : e.right, o = this.mergeSegments(n);
1616
+ if (o.length === 0) return !1;
1617
+ let s = o[0];
1618
+ if (!s || s.start > r + ALIGN_TOLERANCE) return !1;
1619
+ let c = s.end;
1620
+ for (let e = 1; e < o.length; e += 1) {
1621
+ let t = o[e];
1622
+ if (t) {
1623
+ if (t.start > c + ALIGN_TOLERANCE) return !1;
1624
+ c = Math.max(c, t.end);
1625
+ }
1626
+ }
1627
+ return c >= i - ALIGN_TOLERANCE;
1628
+ }
1629
+ mergeSegments(e) {
1630
+ let t = e.map((e) => ({
1631
+ start: Math.min(e.start, e.end),
1632
+ end: Math.max(e.start, e.end)
1633
+ })).sort((e, t) => e.start - t.start), n = [];
1634
+ for (let e of t) {
1635
+ let t = n[n.length - 1];
1636
+ !t || e.start > t.end + ALIGN_TOLERANCE ? n.push({ ...e }) : t.end = Math.max(t.end, e.end);
1637
+ }
1638
+ return n;
1639
+ }
1640
+ expandRectIntoHole(e, t, n) {
1641
+ return n === "west" ? {
1642
+ ...e,
1643
+ right: t.right
1644
+ } : n === "east" ? {
1645
+ ...e,
1646
+ left: t.left
1647
+ } : n === "south" ? {
1648
+ ...e,
1649
+ top: t.top
1650
+ } : {
1651
+ ...e,
1652
+ bottom: t.bottom
1653
+ };
1654
+ }
1655
+ findOverlaps(e) {
1656
+ let t = Object.values(e.areas), n = [];
1657
+ for (let r = 0; r < t.length; r += 1) {
1658
+ let i = t[r];
1659
+ if (!i) continue;
1660
+ let o = this.getAreaRect(e, i);
1661
+ for (let s = r + 1; s < t.length; s += 1) {
1662
+ let r = t[s];
1663
+ if (!r) continue;
1664
+ let c = this.getAreaRect(e, r), l = Math.max(o.left, c.left), u = Math.min(o.right, c.right), d = Math.max(o.bottom, c.bottom), f = Math.min(o.top, c.top);
1665
+ u - l > ALIGN_TOLERANCE && f - d > ALIGN_TOLERANCE && n.push({
1666
+ a: i.id,
1667
+ b: r.id,
1668
+ rect: {
1669
+ left: l,
1670
+ right: u,
1671
+ bottom: d,
1672
+ top: f
1673
+ }
1674
+ });
1675
+ }
1676
+ }
1677
+ return n;
1678
+ }
1679
+ throwOverlapError(e, t) {
1680
+ let n = {
1681
+ overlaps: t.map((e) => ({
1682
+ a: e.a,
1683
+ b: e.b,
1684
+ rect: this.formatRect(e.rect)
1685
+ })),
1686
+ areas: Object.values(e.areas).map((t) => ({
1687
+ id: t.id,
1688
+ rect: this.formatRect(this.getAreaRect(e, t))
1689
+ }))
1690
+ };
1691
+ throw Error(`Overlapping areas detected in layout: ${JSON.stringify(n)}`);
1692
+ }
1693
+ throwHoleError(e, t) {
1694
+ let n = {
1695
+ holes: t.map((e) => this.formatRect(e)),
1696
+ areas: Object.values(e.areas).map((t) => ({
1697
+ id: t.id,
1698
+ rect: this.formatRect(this.getAreaRect(e, t))
1699
+ }))
1700
+ };
1701
+ throw Error(`Unfillable hole(s) detected in layout: ${JSON.stringify(n)}`);
1702
+ }
1703
+ formatRect(e) {
1704
+ return {
1705
+ left: Number(e.left.toFixed(6)),
1706
+ right: Number(e.right.toFixed(6)),
1707
+ top: Number(e.top.toFixed(6)),
1708
+ bottom: Number(e.bottom.toFixed(6))
1709
+ };
1710
+ }
1711
+ collectConnectedVerts(e, t, n, r, a) {
1712
+ let o = Math.min(r, a) - EPS, s = Math.max(r, a) + EPS, c = (e) => t === "vertical" ? Math.abs(e.x - n) <= EPS : Math.abs(e.y - n) <= EPS, l = (e) => t === "vertical" ? e.y >= o && e.y <= s : e.x >= o && e.x <= s, u = [], d = /* @__PURE__ */ new Set();
1713
+ for (let t of Object.values(e.verts)) c(t) && l(t) && (u.push(t.id), d.add(t.id));
1714
+ if (u.length === 0) return d;
1715
+ let f = Object.values(e.edges);
1716
+ for (; u.length > 0;) {
1717
+ let t = u.shift();
1718
+ if (t) for (let n of f) {
1719
+ if (n.v1 !== t && n.v2 !== t) continue;
1720
+ let r = e.verts[n.v1], i = e.verts[n.v2];
1721
+ if (!r || !i || !c(r) || !c(i)) continue;
1722
+ let a = n.v1 === t ? n.v2 : n.v1;
1723
+ d.has(a) || (d.add(a), u.push(a));
1724
+ }
1725
+ }
1726
+ return d;
1727
+ }
1728
+ };
1729
+ customElements.get("sliced-areas") || customElements.define("sliced-areas", SlicedAreasElement);
1730
+ export { SlicedAreasElement as t };
1731
+
1732
+ //# sourceMappingURL=sliced-areas-WnMrbsZg.js.map