@n-uf/placement 26.11.25 → 26.11.27

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.
@@ -1,1745 +0,0 @@
1
- import {
2
- fitFloat,
3
- fitStack,
4
- formCrossMin,
5
- formMain,
6
- formSetsCross,
7
- wrapLines
8
- } from "./chunk-KGZCYLQN.js";
9
- import {
10
- COLUMN_REACH,
11
- EPS,
12
- PLANE,
13
- RAIL_EDGE,
14
- area,
15
- clampInto,
16
- contains,
17
- overlaps,
18
- rect,
19
- subtract
20
- } from "./chunk-WM5FJ3IS.js";
21
-
22
- // src/scene/model.ts
23
- var SIDES = ["top", "right", "bottom", "left"];
24
- var ZONES = ["start", "center", "end"];
25
- var zoneTakes = (edge, zone, kind) => edge.accepts.includes(kind) && (edge.zoneAccepts === void 0 || !!edge.zoneAccepts[zone]?.includes(kind));
26
- var zonesTaking = (edge, kind) => ZONES.filter((z) => zoneTakes(edge, z, kind));
27
- var DEFAULT_STRIP = 24;
28
- var DEFAULT_HIT_BAND = 24;
29
- var DEFAULT_FLOAT_SIZE = { width: 280, height: 360 };
30
- var axisOf = (side) => side === "left" || side === "right" ? "column" : "row";
31
- var zoneOf = (item) => item?.align === "center" || item?.align === "end" ? item.align : "start";
32
- var formsOf = (item, axis) => item.forms[axis] ?? [];
33
- function isOutsideSideBar(scene, e) {
34
- return e.outsideInset === true && isSideBar(scene, e);
35
- }
36
- function isSideBar(scene, e) {
37
- if (e.side !== "left" && e.side !== "right" || e.column === true || e.accepts.length === 0) return false;
38
- const template = scene.columns?.[e.side];
39
- const rail = scene.rails?.[e.side];
40
- if (!template && !rail) return false;
41
- const panelKinds = [...template?.accepts ?? [], ...rail?.accepts ?? []];
42
- return !e.accepts.some((k) => panelKinds.includes(k));
43
- }
44
- var floatOrientOf = (item) => {
45
- if (item.floatOrient === "row" || item.floatOrient === "column") return item.floatOrient;
46
- for (const k of Object.keys(item.forms)) if ((k === "row" || k === "column") && formsOf(item, k).length > 0) return k;
47
- return "column";
48
- };
49
- function floatSizeOf(item, orient2 = floatOrientOf(item)) {
50
- if (item.floatSize) return item.floatSize;
51
- const f = formsOf(item, orient2)[0];
52
- if (!f || f.main.grows) return DEFAULT_FLOAT_SIZE;
53
- const main = Number.isFinite(f.main.natural) ? Math.max(0, f.main.natural) : 0;
54
- const cross = Number.isFinite(f.cross.natural) ? Math.max(0, f.cross.natural) : 0;
55
- if (main <= 0 || cross <= 0) return DEFAULT_FLOAT_SIZE;
56
- return orient2 === "row" ? { width: main, height: cross } : { width: cross, height: main };
57
- }
58
- function pastWrap(forms, from) {
59
- for (let j = Math.max(0, Math.floor(from)); j < forms.length; j++) if (forms[j].kind !== "wrap" && !forms[j].wrap) return j;
60
- return void 0;
61
- }
62
- function floatShape(item, f) {
63
- const orient2 = f.orient ?? floatOrientOf(item);
64
- const forms = formsOf(item, orient2);
65
- const px2 = (v) => Number.isFinite(v) ? Math.max(0, v) : 0;
66
- const w = px2(f.rect.width);
67
- const h = px2(f.rect.height);
68
- if (forms.length === 0) return { form: 0, width: w, height: h };
69
- const asked = Math.min(Math.max(0, Math.floor(Number.isFinite(item.from) ? item.from : 0)), forms.length - 1);
70
- const isWrap = (x) => x.kind === "wrap" || !!x.wrap;
71
- const skipped = orient2 === "column" ? pastWrap(forms, asked) : asked;
72
- if (skipped === void 0) return { form: asked, width: w, height: h, none: true };
73
- const form = skipped;
74
- const fm = forms[form];
75
- if (orient2 === "column" && forms.slice(0, form + 1).some(isWrap)) return { form, width: px2(fm.cross.natural), height: px2(fm.main.natural) };
76
- if (form > 0) return orient2 === "column" ? { form, width: w, height: px2(fm.main.natural) } : { form, width: px2(fm.main.natural), height: h };
77
- return { form, width: w, height: h };
78
- }
79
-
80
- // src/scene/solve.ts
81
- var finite = (x, fallback) => typeof x === "number" && Number.isFinite(x) ? x : fallback;
82
- var px = (x) => Math.max(0, finite(x, 0));
83
- function cutOrder(edges) {
84
- const phase = (e) => e.side === "left" || e.side === "right" ? 1 : e.span === "between" ? 2 : 0;
85
- const sideRank = (e) => e.side === "top" || e.side === "left" ? 0 : 1;
86
- return edges.map((e, i) => ({ e, i })).sort((a, b) => phase(a.e) - phase(b.e) || sideRank(a.e) - sideRank(b.e) || finite(a.e.ring, 0) - finite(b.e.ring, 0) || a.i - b.i).map((x) => x.e);
87
- }
88
- function layKeys(scene) {
89
- const isSide = (e) => e.side === "left" || e.side === "right";
90
- const phase = (e) => e.mode === "overlay" ? 4 : isSide(e) ? 2 : e.span === "between" ? 3 : e.outsideInset === true ? 0 : 1;
91
- return new Map(cutOrder(scene.edges).map((e, at) => [e.id, { phase: phase(e), side: e.side, at }]));
92
- }
93
- function pushes(a, b) {
94
- if (a.phase === 4 || b.phase === 4) return false;
95
- if (a.phase !== b.phase) return a.phase < b.phase;
96
- return a.side === b.side && a.at < b.at;
97
- }
98
- var ZONE_RANK = { start: 0, center: 1, end: 2 };
99
- function zoneSorted(scene, ids) {
100
- return ids.map((id, i) => ({ id, i, z: ZONE_RANK[zoneOf(scene.items[id])] })).sort((a, b) => a.z - b.z || a.i - b.i).map((x) => x.id);
101
- }
102
- var stackItemOf = (item, forms, from) => ({
103
- id: item.id,
104
- forms,
105
- used: finite(item.used, 0),
106
- ...item.kept ? { kept: true } : {},
107
- ...from !== void 0 ? { from } : item.from !== void 0 ? { from: item.from } : {},
108
- ...item.fixed ? { fixed: true } : {}
109
- });
110
- function hasSetter(fit, forms) {
111
- return fit.items.some((p) => p.kind !== "menu" && formSetsCross(forms(p.id)[p.form]));
112
- }
113
- function shapeOf(f, r, axis, cross) {
114
- if (!f) return {};
115
- if (f.wrap) {
116
- const lead = Math.max(0, finite(f.wrap.lead ?? 0, 0));
117
- const row = Math.max(0, finite(f.wrap.row, 0));
118
- return { rows: wrapLines(f.wrap, cross).lines, grip: rect(r.x, r.y, lead, Math.min(row, r.height)) };
119
- }
120
- const g = typeof f.grip === "number" && Number.isFinite(f.grip) && f.grip > 0 ? f.grip : 0;
121
- if (!g) return {};
122
- return { grip: axis === "column" ? rect(r.x, r.y, r.width, Math.min(g, r.height)) : rect(r.x, r.y, Math.min(g, r.width), r.height) };
123
- }
124
- var TRAY_RANK = 100;
125
- function solveEdge(scene, edge, ids, length, room, previous) {
126
- const axis = axisOf(edge.side);
127
- const column = axis === "column";
128
- const ordered = zoneSorted(scene, ids);
129
- const priorityOf = (id) => finite(scene.items[id].priority ?? edge.priority ?? 0, 0);
130
- const formsById = (id) => {
131
- const forms = formsOf(scene.items[id], axis);
132
- const p = priorityOf(id);
133
- return p === 0 ? forms : forms.map((f) => ({ ...f, rank: finite(f.rank ?? 0, 0) + p }));
134
- };
135
- const maxOf = Number.isNaN(edge.cross.max) ? Infinity : edge.cross.max;
136
- const cross = { min: Math.min(px(edge.cross.min), room), max: Math.min(maxOf, room), rail: Math.min(px(edge.cross.rail ?? 30), room) };
137
- const base = {
138
- axis,
139
- length,
140
- cross,
141
- ...edge.menu ? { menu: edge.menu } : {},
142
- ...edge.gap !== void 0 ? { gap: edge.gap } : {},
143
- ...edge.pad ? { pad: edge.pad } : {},
144
- ...column ? { ownCross: true } : {}
145
- };
146
- if (edge.compact) {
147
- const iconsAt = (id) => {
148
- const forms = formsById(id);
149
- for (let j = Math.max(0, Math.floor(finite(scene.items[id].from, 0))); j < forms.length; j++) if (forms[j].kind === "icons") return j;
150
- return forms.findIndex((f) => f.kind === "icons");
151
- };
152
- const shown = ordered.filter((id) => !scene.items[id].closed && iconsAt(id) >= 0);
153
- const leftOutIcons = ordered.filter((id) => !scene.items[id].closed && iconsAt(id) < 0);
154
- const compactInput = { ...base, ownCross: false, items: shown.map((id) => stackItemOf(scene.items[id], formsById(id), iconsAt(id))) };
155
- const stack = fitStack(compactInput, previous ? { previous: previous.free } : {});
156
- return { stack, free: stack, tabs: [], closedTabs: /* @__PURE__ */ new Set(), input: compactInput, leftOutIcons };
157
- }
158
- const open2 = ordered.filter((id) => !scene.items[id].closed);
159
- const asStackItem = (id) => {
160
- const it = scene.items[id];
161
- const forms = formsById(id);
162
- if (!column || !edge.zoneAccepts?.end || zoneOf(it) !== "end") return stackItemOf(it, forms);
163
- const tray = (f) => f.kind === "wrap" || f.kind === "icons" || !!f.wrap;
164
- let first = -1;
165
- for (let j = Math.max(0, Math.floor(finite(it.from, 0))); j < forms.length && first < 0; j++) if (tray(forms[j])) first = j;
166
- let last = -1;
167
- forms.forEach((f, j) => {
168
- if (tray(f)) last = j;
169
- });
170
- if (first < 0) return stackItemOf(it, forms);
171
- return stackItemOf(it, forms.slice(0, last + 1).map((f) => ({ ...f, rank: finite(f.rank ?? 0, 0) + TRAY_RANK })), first);
172
- };
173
- const input = { ...base, items: open2.map(asStackItem) };
174
- const free = fitStack(input, previous ? { previous: previous.free } : {});
175
- const none = /* @__PURE__ */ new Set();
176
- if (column && !hasSetter(free, formsById)) {
177
- const railIndex = (item, forms) => {
178
- if (item.fixed) return -1;
179
- for (let j = Math.max(0, Math.floor(finite(item.from, 0))); j < forms.length; j++) if (forms[j].kind === "rail") return j;
180
- return -1;
181
- };
182
- const wrapOnly = (id) => railIndex(scene.items[id], formsById(id)) < 0 && pastWrap(formsById(id), finite(scene.items[id].from, 0)) === void 0;
183
- const shown = ordered.filter((id) => {
184
- const it = scene.items[id];
185
- if (!it.closed && wrapOnly(id)) return false;
186
- return !it.closed || edge.railShowsClosed === true && railIndex(it, formsById(id)) >= 0;
187
- });
188
- const leftOut = ordered.filter((id) => !scene.items[id].closed && wrapOnly(id));
189
- const anyRail = shown.some((id) => railIndex(scene.items[id], formsById(id)) >= 0);
190
- if (anyRail) {
191
- const railInput = {
192
- ...base,
193
- items: shown.map((id) => {
194
- const it = scene.items[id];
195
- const j = railIndex(it, formsById(id));
196
- const forms = asStackItem(id).forms;
197
- return stackItemOf(it, forms, j >= 0 ? j : pastWrap(forms, finite(it.from, 0)));
198
- })
199
- };
200
- const stack = fitStack(railInput, previous ? { previous: previous.stack } : {});
201
- const tabs = stack.items.filter((p) => p.kind === "rail").map((p) => p.id);
202
- const closedTabs = new Set(shown.filter((id) => scene.items[id].closed));
203
- return { stack, free, tabs, closedTabs, input: railInput, leftOut };
204
- }
205
- }
206
- const size = edge.size;
207
- if (typeof size === "number" && Number.isFinite(size) && hasSetter(free, formsById)) {
208
- const setters = free.items.filter((p) => p.kind !== "menu" && formSetsCross(formsById(p.id)[p.form]));
209
- const lo = Math.max(px(edge.cross.min), ...setters.map((p) => formCrossMin(formsById(p.id)[p.form])));
210
- const s = Math.min(Math.max(size, lo), Math.max(lo, maxOf), room);
211
- if (Math.abs(s - free.cross) > EPS) {
212
- const sized = { ...input, cross: { ...cross, min: s, max: s } };
213
- const stack = fitStack(sized, previous ? { previous: previous.stack } : {});
214
- return { stack, free, tabs: [], closedTabs: none, input: sized };
215
- }
216
- }
217
- return { stack: free, free, tabs: [], closedTabs: none, input };
218
- }
219
- function zoneStarts(scene, fit, input) {
220
- const column = fit.axis === "column";
221
- const at = (r) => column ? r.y : r.x;
222
- const out = /* @__PURE__ */ new Map();
223
- const lengths = /* @__PURE__ */ new Map();
224
- const drawn = fit.items.filter((p) => p.kind !== "menu");
225
- for (const p of drawn) {
226
- out.set(p.id, at(p.rect));
227
- lengths.set(p.id, column ? p.rect.height : p.rect.width);
228
- }
229
- const len = (p) => lengths.get(p.id);
230
- const gap = px(input.gap);
231
- const start = px(input.pad?.start);
232
- const end = px(input.pad?.end);
233
- const menu = fit.menu ? (column ? fit.menu.rect.height : fit.menu.rect.width) + (drawn.length > 0 ? gap : 0) : 0;
234
- const zone = (z) => drawn.filter((p) => zoneOf(scene.items[p.id]) === z);
235
- const s = zone("start");
236
- const c = zone("center");
237
- const e = zone("end");
238
- const title = column ? void 0 : s.find((p) => {
239
- const it = scene.items[p.id];
240
- const f = formsOf(it, "row")[p.form];
241
- return it.fixed && f?.main.grows && len(p) > formMain(f, "natural", fit.cross) + EPS;
242
- });
243
- if (title) lengths.set(title.id, formMain(formsOf(scene.items[title.id], "row")[title.form], "natural", fit.cross));
244
- const used = drawn.reduce((sum, p) => sum + len(p), 0) + Math.max(0, drawn.length - 1) * gap;
245
- const spare = fit.length - start - end - menu - used;
246
- if (spare <= EPS) return { starts: out, lengths };
247
- const total = (ps) => ps.reduce((sum, p) => sum + len(p), 0) + Math.max(0, ps.length - 1) * gap;
248
- const lay = (ps, from) => {
249
- let x = from;
250
- for (const p of ps) {
251
- out.set(p.id, x);
252
- x += len(p) + gap;
253
- }
254
- };
255
- lay(s, start);
256
- const endStart = fit.length - end - menu - total(e);
257
- lay(e, endStart);
258
- const lo = start + total(s) + (s.length > 0 ? gap : 0);
259
- const hi = endStart - (e.length > 0 ? gap : 0);
260
- const mid = (fit.length - total(c)) / 2;
261
- const clear = mid >= lo - EPS && mid + total(c) <= hi + EPS;
262
- lay(c, title && !clear ? Math.max(lo, (lo + hi - total(c)) / 2) : Math.max(lo, Math.min(mid, hi - total(c))));
263
- if (title && s[s.length - 1] === title) {
264
- const next = [...c, ...e].map((p) => out.get(p.id));
265
- const until = next.length ? Math.min(...next) - gap : fit.length - end - menu;
266
- lengths.set(title.id, Math.max(len(title), until - out.get(title.id)));
267
- }
268
- return { starts: out, lengths };
269
- }
270
- function anchored(content, w, h, anchor) {
271
- const left = content.x;
272
- const right = content.x + content.width - w;
273
- const midX = content.x + (content.width - w) / 2;
274
- const top = content.y;
275
- const bottom = content.y + content.height - h;
276
- const midY = content.y + (content.height - h) / 2;
277
- const at = {
278
- "content-top-left": [left, top],
279
- "content-top": [midX, top],
280
- "content-top-right": [right, top],
281
- "content-right": [right, midY],
282
- "content-bottom-right": [right, bottom],
283
- "content-bottom": [midX, bottom],
284
- "content-bottom-left": [left, bottom],
285
- "content-left": [left, midY]
286
- };
287
- const [x, y] = at[anchor] ?? at["content-top-right"];
288
- return rect(x, y, w, h);
289
- }
290
- function solve(given, options = {}) {
291
- const previous = options.previous;
292
- const opened = /* @__PURE__ */ new Set();
293
- if (previous) {
294
- for (const e of given.edges) {
295
- for (const id of e.items) {
296
- const it = Object.hasOwn(given.items, id) ? given.items[id] : void 0;
297
- const was = previous.items[id];
298
- if (it && !it.closed && !it.fixed && (!was || was.closed)) opened.add(id);
299
- }
300
- }
301
- }
302
- const newest = Object.values(given.items).reduce((m, it) => Math.max(m, finite(it.used, 0)), 0) + 1;
303
- const scene = opened.size === 0 ? given : { ...given, items: { ...given.items, ...Object.fromEntries([...opened].map((id) => [id, { ...given.items[id], used: newest }])) } };
304
- const fresh = new Set(options.fresh ?? []);
305
- const prevEdge = (edge) => !previous || fresh.has(edge.id) || edge.items.some((id) => fresh.has(id)) ? void 0 : previous.edges[edge.id];
306
- const notes = [];
307
- const items = {};
308
- const edges = {};
309
- const stage = rect(finite(scene.stage.x, 0), finite(scene.stage.y, 0), px(scene.stage.width), px(scene.stage.height));
310
- const order = cutOrder(scene.edges);
311
- const claimed = /* @__PURE__ */ new Set();
312
- const claim = (id) => {
313
- if (!Object.hasOwn(scene.items, id)) {
314
- notes.push({ code: "unknown-item", id });
315
- return false;
316
- }
317
- if (claimed.has(id)) {
318
- notes.push({ code: "twice", id });
319
- return false;
320
- }
321
- claimed.add(id);
322
- return true;
323
- };
324
- const owned = /* @__PURE__ */ new Map();
325
- for (const e of order) owned.set(e, e.items.filter(claim));
326
- const emit = (edge, r, solved, extra = {}) => {
327
- const column = axisOf(edge.side) === "column";
328
- const { stack } = solved;
329
- const thick = column ? r.width : r.height;
330
- const { starts, lengths } = zoneStarts(scene, stack, solved.input);
331
- const toStage = (l, id) => {
332
- const s = id !== void 0 ? starts.get(id) : void 0;
333
- const n = id !== void 0 ? lengths.get(id) : void 0;
334
- const x = column ? l.x : s ?? l.x;
335
- const y = column ? s ?? l.y : l.y;
336
- return rect(r.x + x, r.y + y, column ? l.width : n ?? l.width, column ? n ?? l.height : l.height);
337
- };
338
- const tabs = new Set(solved.tabs);
339
- for (const id of solved.leftOut ?? []) notes.push({ code: "no-vertical-form", edge: edge.id, id });
340
- for (const id of solved.leftOutIcons ?? []) notes.push({ code: "no-icons-form", edge: edge.id, id });
341
- for (const p of stack.items) {
342
- const it = scene.items[p.id];
343
- const inMenu = p.kind === "menu";
344
- const drawnRect = toStage(p.rect, inMenu ? void 0 : p.id);
345
- const shape = inMenu ? {} : shapeOf(formsOf(it, column ? "column" : "row")[p.form], drawnRect, column ? "column" : "row", stack.cross);
346
- items[p.id] = {
347
- ...shape,
348
- rect: drawnRect,
349
- kind: it.kind,
350
- form: p.form,
351
- formKind: p.kind,
352
- edge: edge.id,
353
- zone: zoneOf(it),
354
- ...inMenu ? { inMenu: true } : {},
355
- ...tabs.has(p.id) ? { tab: true } : {},
356
- ...solved.closedTabs.has(p.id) ? { closed: true } : {},
357
- ...it.fixed ? { fixed: true } : {}
358
- };
359
- }
360
- for (const n of stack.notes) notes.push({ edge: edge.id, ...n });
361
- edges[edge.id] = {
362
- side: edge.side,
363
- ring: finite(edge.ring, 0),
364
- rect: r,
365
- cross: thick,
366
- menu: stack.menu ? { rect: toStage(stack.menu.rect), items: stack.menu.items } : null,
367
- tabs: solved.tabs,
368
- stack,
369
- free: solved.free,
370
- ...extra
371
- };
372
- };
373
- const lay = (edge, area3, cut) => {
374
- const column = axisOf(edge.side) === "column";
375
- const length = column ? area3.height : area3.width;
376
- const room = Math.max(0, column ? area3.width : area3.height);
377
- const solved = solveEdge(scene, edge, owned.get(edge), length, room, prevEdge(edge));
378
- const thick = solved.stack.items.length === 0 ? 0 : Math.min(solved.stack.cross, room);
379
- const r = edge.side === "top" ? rect(area3.x, area3.y, area3.width, thick) : edge.side === "bottom" ? rect(area3.x, area3.y + area3.height - thick, area3.width, thick) : edge.side === "left" ? rect(area3.x, area3.y, thick, area3.height) : rect(area3.x + area3.width - thick, area3.y, thick, area3.height);
380
- emit(edge, r, solved);
381
- if (!cut) return area3;
382
- return edge.side === "top" ? rect(area3.x, area3.y + thick, area3.width, area3.height - thick) : edge.side === "bottom" ? rect(area3.x, area3.y, area3.width, area3.height - thick) : edge.side === "left" ? rect(area3.x + thick, area3.y, area3.width - thick, area3.height) : rect(area3.x, area3.y, area3.width - thick, area3.height);
383
- };
384
- const rails = {};
385
- const railPlaces = {};
386
- const gutter = px(scene.gutter);
387
- let columnRoom;
388
- let outerArea = stage;
389
- const isSide = (e) => e.side === "left" || e.side === "right";
390
- const PANEL_FORMS = /* @__PURE__ */ new Set(["open", "header", "rail"]);
391
- const isPanel = (id) => {
392
- const it = scene.items[id];
393
- return !it.closed && !it.fixed && PANEL_FORMS.has(formsOf(it, "column")[0]?.kind ?? "");
394
- };
395
- const docked = order.some((e) => isSide(e) && e.accepts.length > 0 && owned.get(e).some(isPanel));
396
- const inset = scene.insetWhen === "docked" && !docked ? {} : scene.inset ?? {};
397
- const laySides = (sideEdges, area3) => {
398
- const H = area3.height;
399
- const ride = scene.handFold === "ride";
400
- const upInset = area3.y <= outerArea.y + px(inset.top) + EPS ? px(inset.top) : 0;
401
- const downInset = area3.y + area3.height >= outerArea.y + outerArea.height - px(inset.bottom) - EPS ? px(inset.bottom) : 0;
402
- const tall = (edge) => H + (edge.outsideInset === true ? upInset + downInset : 0);
403
- const cols = sideEdges.map((edge) => {
404
- const side = edge.side;
405
- const solved = solveEdge(scene, edge, owned.get(edge), tall(edge), Math.max(0, area3.width), prevEdge(edge));
406
- const width = solved.stack.items.length === 0 ? 0 : Math.min(solved.stack.cross, area3.width);
407
- const visible = owned.get(edge).filter((id) => !scene.items[id].closed);
408
- const dyn = (edge.column === true || edge.giveWay === true) && scene.rails?.[side] !== void 0 && visible.length > 0;
409
- const forms = (id) => formsOf(scene.items[id], "column");
410
- const setters = solved.free.items.filter((p) => p.kind !== "menu" && formSetsCross(forms(p.id)[p.form]));
411
- const min = setters.length > 0 ? Math.min(width, Math.max(px(edge.cross.min), ...setters.map((p) => formCrossMin(forms(p.id)[p.form])))) : width;
412
- return {
413
- edge,
414
- side,
415
- solved,
416
- width,
417
- min,
418
- visible,
419
- dyn,
420
- hand: dyn && edge.column === true && visible.every((id) => finite(scene.items[id].from, 0) >= 1),
421
- kept: visible.some((id) => scene.items[id].kept === true),
422
- age: Math.max(-Infinity, ...visible.map((id) => finite(scene.items[id].used, 0)))
423
- };
424
- });
425
- const cmin = scene.contentMin ? px(scene.contentMin.width) : void 0;
426
- const order2 = cols.filter((c) => c.dyn && !c.hand && c.width > 0).sort((a, b) => Number(a.kept) - Number(b.kept) || a.age - b.age || finite(a.edge.ring, 0) - finite(b.edge.ring, 0) || (a.side === b.side ? 0 : a.side === "left" ? -1 : 1));
427
- const shrunkBefore = (previous?.notes ?? []).flatMap((n) => n.code === "column-shrunk" ? [n.edge] : []);
428
- const rankOf = (c) => shrunkBefore.includes(c.edge.id) ? shrunkBefore.indexOf(c.edge.id) : Infinity;
429
- const shrinkOrder = order2.map((c, i) => ({ c, i })).sort((a, b) => rankOf(a.c) === rankOf(b.c) ? a.i - b.i : rankOf(a.c) < rankOf(b.c) ? -1 : 1).map((x) => x.c);
430
- const railW = (side) => px(scene.rails?.[side]?.width);
431
- const evaluate = (auto2) => {
432
- const openOn = (side) => cols.some((c) => c.side === side && c.dyn && c.edge.column === true && !c.hand && !auto2.has(c) && c.width > 0);
433
- const riding = (c) => ride && c.hand && openOn(c.side);
434
- const railed = (c) => c.dyn && (auto2.has(c) || c.hand && !riding(c));
435
- const widths = new Map(cols.filter((c) => !railed(c) && !riding(c)).map((c) => [c, c.width]));
436
- const railSides = ["left", "right"].filter((s) => cols.some((c) => c.side === s && railed(c)));
437
- let used = 0;
438
- for (const [c, w] of widths) used += w > 0 ? w + (c.edge.outsideInset === true ? 0 : gutter) : 0;
439
- for (const s of railSides) used += railW(s) + gutter;
440
- let excess = cmin === void 0 ? -Infinity : used + cmin - area3.width;
441
- for (const c of shrinkOrder) {
442
- if (excess <= EPS) break;
443
- const w = widths.get(c);
444
- if (w === void 0) continue;
445
- const take = Math.min(excess, w - c.min);
446
- if (take > 0) {
447
- widths.set(c, w - take);
448
- excess -= take;
449
- }
450
- }
451
- return { auto: auto2, widths, excess, railed, riding, railSides };
452
- };
453
- let auto = /* @__PURE__ */ new Set();
454
- if (cmin !== void 0) {
455
- const unfold = () => {
456
- for (const c of [...auto].reverse()) {
457
- const fewer = new Set([...auto].filter((x) => x !== c));
458
- if (evaluate(fewer).excess <= EPS) auto = fewer;
459
- else break;
460
- }
461
- };
462
- if (previous) {
463
- const byId = new Map(order2.map((c) => [c.edge.id, c]));
464
- const anew = (c) => fresh.has(c.edge.id) || c.visible.some((id) => opened.has(id) || fresh.has(id));
465
- const was = previous.notes.flatMap((n) => (n.code === "column-folded" || n.code === "kept-folded") && byId.has(n.edge) && !anew(byId.get(n.edge)) ? [byId.get(n.edge)] : []);
466
- auto = new Set(was);
467
- if (evaluate(auto).excess <= EPS) unfold();
468
- }
469
- for (const c of order2) {
470
- if (evaluate(auto).excess <= EPS) break;
471
- if (!auto.has(c)) auto = /* @__PURE__ */ new Set([...auto, c]);
472
- }
473
- for (let size = -1; size !== auto.size; ) {
474
- size = auto.size;
475
- unfold();
476
- }
477
- }
478
- const g = evaluate(auto);
479
- const openMin = (ids, least) => Math.max(
480
- least,
481
- ...ids.map((id) => {
482
- const f = formsOf(scene.items[id], "column")[0];
483
- return f && formSetsCross(f) ? formCrossMin(f) : 0;
484
- })
485
- );
486
- const open2 = cols.filter((c) => c.edge.column === true && c.visible.length > 0).map((c) => c.hand ? openMin(c.visible, px(c.edge.cross.min)) : c.min);
487
- const templates = ["left", "right"].flatMap((s) => {
488
- const t = scene.columns?.[s];
489
- if (!t) return [];
490
- const takes = Object.values(scene.items).filter((it) => !it.fixed && !it.closed && t.accepts.includes(it.kind));
491
- return [openMin(takes.map((it) => it.id), px(t.cross.min))];
492
- });
493
- const colMins = open2.length > 0 ? open2 : templates;
494
- if (cmin !== void 0 && colMins.length > 0) {
495
- const bars = cols.filter((c) => c.width > 0 && !c.dyn && isSideBar(scene, c.edge)).reduce((s, c) => s + c.width + (c.edge.outsideInset === true ? 0 : gutter), 0);
496
- const width = area3.width - bars - (scene.insetWhen === "docked" && !docked ? px(scene.inset?.left) + px(scene.inset?.right) : 0);
497
- const need = cmin + Math.min(...colMins) + gutter;
498
- columnRoom = width >= need - EPS;
499
- if (!columnRoom) notes.push({ code: "no-column-room", width, need });
500
- }
501
- for (const c of shrinkOrder) {
502
- const w = g.widths.get(c);
503
- if (w !== void 0 && w < c.width - EPS) notes.push({ code: "column-shrunk", edge: c.edge.id, from: c.width, to: w });
504
- }
505
- for (const c of g.auto) notes.push({ code: c.kept ? "kept-folded" : "column-folded", edge: c.edge.id });
506
- let x0 = area3.x;
507
- let x1 = area3.x + area3.width;
508
- for (const side of ["left", "right"]) {
509
- const outsideSideBar = (c) => c.width > 0 && isOutsideSideBar(scene, c.edge) && !g.railed(c) && !g.riding(c);
510
- const all = cols.filter((c) => c.side === side);
511
- const mine = [...all.filter(outsideSideBar), ...all.filter((c) => !outsideSideBar(c))];
512
- const railed = mine.filter((c) => g.railed(c));
513
- const riders = mine.filter((c) => g.riding(c));
514
- const host = mine.find((c) => c.dyn && c.edge.column === true && !c.hand && !g.auto.has(c) && c.width > 0);
515
- const riderBars = (c) => zoneSorted(scene, c.visible).map((id) => {
516
- const forms = formsOf(scene.items[id], "column");
517
- const form = forms.length === 0 ? 0 : Math.min(Math.max(0, Math.floor(finite(scene.items[id].from, 0))), forms.length - 1);
518
- return { id, form, f: forms[form], h: px(forms[form]?.main.natural) };
519
- });
520
- const ridersTop = riders.reduce((s, c) => s + riderBars(c).reduce((t, b) => t + b.h, 0), 0);
521
- let railRect = null;
522
- let gap = px(side === "left" ? inset.left : inset.right);
523
- let barLaid = false;
524
- let railPlace = null;
525
- const placeRail = () => {
526
- const w = railW(side);
527
- const lead = barLaid ? gap : 0;
528
- return side === "left" ? rect(x0 - lead, area3.y, w, H) : rect(x1 + lead - w, area3.y, w, H);
529
- };
530
- for (const c of mine) {
531
- if (!railPlace && !outsideSideBar(c)) {
532
- railPlace = placeRail();
533
- if (railed.length > 0) {
534
- railRect = railPlace;
535
- const lead = barLaid ? gap : 0;
536
- const after = barLaid ? Math.max(gap, gutter) : gutter;
537
- if (side === "left") x0 += railPlace.width - lead + after;
538
- else x1 -= railPlace.width - lead + after;
539
- gap = after;
540
- }
541
- }
542
- if (outsideSideBar(c)) barLaid = true;
543
- const base = { side: c.edge.side, ring: finite(c.edge.ring, 0), cross: 0, menu: null, tabs: [], stack: c.solved.stack, free: c.solved.free };
544
- if (g.railed(c)) {
545
- edges[c.edge.id] = { ...base, rect: rect(railRect.x, area3.y, 0, H), folded: g.auto.has(c) ? "auto" : "hand" };
546
- continue;
547
- }
548
- if (g.riding(c)) continue;
549
- const w = Math.min(g.widths.get(c), Math.max(0, x1 - x0));
550
- const top = c === host ? ridersTop : 0;
551
- let solved = c.solved;
552
- if (top > 0 || Math.abs(w - c.width) > EPS) {
553
- const forced = { ...c.edge, cross: { ...c.edge.cross, min: w, max: w } };
554
- const { size: _s, ...plain } = forced;
555
- solved = solveEdge(scene, plain, owned.get(c.edge), H - top, w, prevEdge(c.edge));
556
- }
557
- let r;
558
- if (c.edge.outsideInset === true) {
559
- const y0 = area3.y - upInset;
560
- const h = tall(c.edge);
561
- if (side === "left") {
562
- r = rect(x0 - gap, y0, w, h);
563
- x0 += w;
564
- } else {
565
- r = rect(x1 + gap - w, y0, w, h);
566
- x1 -= w;
567
- }
568
- if (Math.abs(h - H) > EPS || Math.abs(w - c.width) > EPS) {
569
- const forced = { ...c.edge, cross: { ...c.edge.cross, min: w, max: w } };
570
- const { size: _s, ...plain } = forced;
571
- solved = solveEdge(scene, plain, owned.get(c.edge), h, w, prevEdge(c.edge));
572
- }
573
- } else if (side === "left") {
574
- r = rect(x0, area3.y + top, w, H - top);
575
- x0 += w > 0 ? w + gutter : 0;
576
- if (w > 0) gap = gutter;
577
- } else {
578
- x1 -= w;
579
- r = rect(x1, area3.y + top, w, H - top);
580
- x1 -= w > 0 ? gutter : 0;
581
- if (w > 0) gap = gutter;
582
- }
583
- emit(c.edge, r, solved, w < c.width - EPS ? { shrunk: true } : {});
584
- }
585
- if (host && riders.length > 0) {
586
- const hr = edges[host.edge.id].rect;
587
- let y = area3.y;
588
- for (const c of riders) {
589
- const bars = riderBars(c);
590
- const y0 = y;
591
- for (const b of bars) {
592
- const at = rect(hr.x, y, hr.width, b.h);
593
- const it = scene.items[b.id];
594
- items[b.id] = { rect: at, kind: it.kind, form: b.form, formKind: b.f?.kind ?? "header", edge: c.edge.id, zone: zoneOf(it), rides: host.edge.id, folded: "hand" };
595
- y += b.h;
596
- }
597
- edges[c.edge.id] = { side: c.edge.side, ring: finite(c.edge.ring, 0), rect: rect(hr.x, y0, hr.width, y - y0), cross: 0, menu: null, tabs: [], stack: c.solved.stack, free: c.solved.free, rides: host.edge.id, folded: "hand" };
598
- }
599
- }
600
- if (railRect) {
601
- const segments = [];
602
- let y = railRect.y;
603
- for (const c of railed) {
604
- for (const id of zoneSorted(scene, c.visible)) {
605
- const it = scene.items[id];
606
- const forms = formsOf(it, "column");
607
- const ri = forms.findIndex((f) => f.kind === "rail");
608
- const form = ri >= 0 ? ri : Math.min(Math.max(0, Math.floor(finite(it.from, 0))), Math.max(0, forms.length - 1));
609
- const h = ri >= 0 ? px(forms[ri].main.natural) : railRect.width;
610
- const at = rect(railRect.x, y, railRect.width, h);
611
- items[id] = { rect: at, kind: it.kind, form, formKind: "rail", edge: c.edge.id, zone: zoneOf(it), rail: side, folded: g.auto.has(c) ? "auto" : "hand" };
612
- segments.push({ id, edge: c.edge.id, rect: at });
613
- y += h;
614
- }
615
- }
616
- rails[side] = { rect: railRect, segments };
617
- }
618
- if (scene.rails?.[side]) railPlaces[side] = railPlace ?? placeRail();
619
- }
620
- return rect(x0, area3.y, Math.max(0, x1 - x0), H);
621
- };
622
- const reserved = order.filter((e) => e.mode !== "overlay");
623
- const outsideBar = (e) => !isSide(e) && e.span !== "between" && e.outsideInset === true;
624
- let outer = stage;
625
- for (const e of reserved) if (outsideBar(e)) outer = lay(e, outer, true);
626
- outerArea = outer;
627
- let area2 = rect(outer.x + px(inset.left), outer.y + px(inset.top), Math.max(0, outer.width - px(inset.left) - px(inset.right)), Math.max(0, outer.height - px(inset.top) - px(inset.bottom)));
628
- for (const e of reserved) if (!isSide(e) && e.span !== "between" && !outsideBar(e)) area2 = lay(e, area2, true);
629
- area2 = laySides(reserved.filter(isSide), area2);
630
- for (const e of reserved) if (!isSide(e) && e.span === "between") area2 = lay(e, area2, true);
631
- const content = area2;
632
- let over = content;
633
- for (const e of order) if (e.mode === "overlay") over = lay(e, over, true);
634
- if (scene.contentMin) {
635
- const minW = px(scene.contentMin.width);
636
- const minH = px(scene.contentMin.height);
637
- if (content.width < minW - EPS) notes.push({ code: "content-narrow", width: content.width, min: minW });
638
- const bars = order.filter((e) => e.mode === "overlay" && !isSide(e)).reduce((s, e) => s + (edges[e.id]?.cross ?? 0), 0);
639
- if (content.height - bars < minH - EPS) notes.push({ code: "content-short", height: content.height - bars, min: minH });
640
- }
641
- if (previous) {
642
- const was = previous.content;
643
- const moved = Math.abs(was.x - content.x) > EPS || Math.abs(was.y - content.y) > EPS || Math.abs(was.width - content.width) > EPS || Math.abs(was.height - content.height) > EPS;
644
- if (moved) notes.push({ code: "content-changed", from: was, to: content });
645
- }
646
- const avoid = [];
647
- for (const l of Object.values(items)) avoid.push(l.rect);
648
- for (const e of Object.values(edges)) {
649
- if (axisOf(e.side) === "row" && e.cross > 0) avoid.push(e.rect);
650
- if (e.menu) avoid.push(e.menu.rect);
651
- }
652
- for (const c of scene.chrome ?? []) avoid.push(c);
653
- const floatRegion = rect(stage.x + px(inset.left), stage.y, Math.max(0, stage.width - px(inset.left) - px(inset.right)), stage.height);
654
- const floats = [];
655
- for (const f of scene.floats) {
656
- if (!claim(f.id)) continue;
657
- const it = scene.items[f.id];
658
- if (it.closed) continue;
659
- const shape = floatShape(it, f);
660
- if (shape.none) {
661
- notes.push({ code: "no-vertical-form", id: f.id });
662
- continue;
663
- }
664
- const from = shape.form;
665
- const fo = f.orient ?? floatOrientOf(it);
666
- const form = formsOf(it, fo)[from];
667
- const row = fo === "row";
668
- const floor = {
669
- width: it.floatMin ? px(it.floatMin.width) : Math.min(shape.width, px(row ? form?.main.min : form?.cross.min)),
670
- height: it.floatMin ? px(it.floatMin.height) : Math.min(shape.height, px(row ? form?.cross.min : form?.main.min))
671
- };
672
- const w = Math.max(floor.width, Math.min(shape.width, floatRegion.width));
673
- const folded = form?.kind === "header";
674
- const h = folded ? shape.height : Math.max(floor.height, Math.min(shape.height, floatRegion.height));
675
- const want = f.anchor ? anchored(content, w, h, f.anchor) : rect(finite(f.rect.x, stage.x), finite(f.rect.y, stage.y), w, h);
676
- const fit0 = fitFloat(want, floatRegion, avoid);
677
- const fit = w > floatRegion.width + EPS ? { ...fit0, rect: rect(floatRegion.x, fit0.rect.y, w, fit0.rect.height) } : fit0;
678
- if (fit.clamped) notes.push({ code: "float-clamped", id: f.id });
679
- if (fit.snapped) notes.push({ code: "float-moved", id: f.id });
680
- if (fit.covers.length > 0) notes.push({ code: "float-covers", id: f.id });
681
- items[f.id] = { ...shapeOf(form, fit.rect, fo, fit.rect.width), rect: fit.rect, kind: it.kind, form: from, formKind: form?.kind ?? "open", floating: true };
682
- floats.push(f.id);
683
- }
684
- return { items, edges, content, floats, rails, railPlaces, ...columnRoom !== void 0 ? { columnRoom } : {}, notes };
685
- }
686
-
687
- // src/scene/changes.ts
688
- var withItem = (scene, item) => ({ ...scene, items: { ...scene.items, [item.id]: item } });
689
- var itemOf = (scene, id) => Object.hasOwn(scene.items, id) ? scene.items[id] : void 0;
690
- function closeRings(edges, sides) {
691
- const out = [...edges];
692
- for (const side of sides) {
693
- const idx = out.map((e, i) => ({ e, i })).filter((x) => x.e.side === side);
694
- idx.sort((a, b) => a.e.ring === b.e.ring ? a.i - b.i : a.e.ring < b.e.ring ? -1 : 1);
695
- idx.forEach((x, ring) => {
696
- if (x.e.ring !== ring) out[x.i] = { ...x.e, ring };
697
- });
698
- }
699
- return out;
700
- }
701
- function prune(scene) {
702
- const gone = scene.edges.filter((e) => e.column === true && e.items.length === 0);
703
- if (gone.length === 0) return scene;
704
- const sides = new Set(gone.map((e) => e.side));
705
- return { ...scene, edges: closeRings(scene.edges.filter((e) => !gone.includes(e)), sides) };
706
- }
707
- function lift(scene, id) {
708
- const inEdge = scene.edges.some((e) => e.items.includes(id));
709
- const inFloat = scene.floats.some((f) => f.id === id);
710
- if (!inEdge && !inFloat) return scene;
711
- return prune({
712
- ...scene,
713
- edges: inEdge ? scene.edges.map((e) => e.items.includes(id) ? { ...e, items: e.items.filter((x) => x !== id) } : e) : scene.edges,
714
- floats: inFloat ? scene.floats.filter((f) => f.id !== id) : scene.floats
715
- });
716
- }
717
- function newColumn(scene, side, kind, ring, items) {
718
- const t = scene.columns?.[side];
719
- if (!t || !t.accepts.includes(kind)) return null;
720
- const taken = new Set(scene.edges.map((e) => e.id));
721
- let n = 0;
722
- while (taken.has(`${side}-${n}`)) n++;
723
- return {
724
- id: `${side}-${n}`,
725
- side,
726
- ring,
727
- accepts: t.accepts,
728
- items,
729
- cross: t.cross,
730
- ...t.gap !== void 0 ? { gap: t.gap } : {},
731
- ...t.pad ? { pad: t.pad } : {},
732
- ...t.menu ? { menu: t.menu } : {},
733
- ...t.zoneAccepts ? { zoneAccepts: t.zoneAccepts } : {},
734
- column: true
735
- };
736
- }
737
- function sideRings(edges, side) {
738
- return new Map(closeRings(edges, /* @__PURE__ */ new Set([side])).filter((e) => e.side === side).map((e) => [e.id, e.ring]));
739
- }
740
- function columnFloor(edges, side) {
741
- const mine = edges.filter((e) => e.side === side).sort((a, b) => a.ring - b.ring);
742
- let n = 0;
743
- while (n < mine.length && mine[n].column !== true) n++;
744
- return n;
745
- }
746
- function insertColumn(scene0, id, side, ring) {
747
- const scene = { ...scene0, edges: closeRings(scene0.edges, /* @__PURE__ */ new Set([side])) };
748
- const kind = scene.items[id].kind;
749
- const mine = scene.edges.filter((e) => e.side === side);
750
- const floor = columnFloor(scene.edges, side);
751
- const r = Math.max(floor, Math.min(Number.isNaN(ring) ? Infinity : Math.floor(ring), mine.length === 0 ? 0 : Math.max(...mine.map((e) => e.ring)) + 1));
752
- const col = newColumn(scene, side, kind, r, [id]);
753
- if (!col) return null;
754
- if (!zoneTakes(col, "start", kind)) return null;
755
- const item = scene.items[id];
756
- const zone = zoneTakes(col, zoneOf(item), kind) ? zoneOf(item) : zonesTaking(col, kind)[0] ?? "start";
757
- const { align: _a, ...plain } = item;
758
- const placed = withItem(scene, zone === "start" ? plain : { ...plain, align: zone });
759
- const shifted = placed.edges.map((e) => e.side === side && e.ring >= r ? { ...e, ring: e.ring + 1 } : e);
760
- return { ...placed, edges: closeRings([...shifted, col], /* @__PURE__ */ new Set([side])) };
761
- }
762
- function insertIndex(scene, edge, zone, at) {
763
- const rank = { start: 0, center: 1, end: 2 };
764
- const inZone = edge.items.map((id, i) => ({ id, i })).filter((x) => zoneOf(itemOf(scene, x.id)) === zone);
765
- const k = Math.min(Math.max(0, Math.floor(Number.isFinite(at) ? at : inZone.length)), inZone.length);
766
- if (k < inZone.length) return inZone[k].i;
767
- if (inZone.length > 0) return inZone[inZone.length - 1].i + 1;
768
- let idx = 0;
769
- edge.items.forEach((id, i) => {
770
- if (rank[zoneOf(itemOf(scene, id))] < rank[zone]) idx = i + 1;
771
- });
772
- return idx;
773
- }
774
- function moveTo(scene, id, place) {
775
- const item = itemOf(scene, id);
776
- if (!item || item.fixed) return scene;
777
- if (place.kind === "float") {
778
- const out = floatAt(scene, id, place.rect);
779
- const anchor = place.anchor;
780
- if (!anchor || out === scene) return out;
781
- return { ...out, floats: out.floats.map((f) => f.id === id ? { ...f, anchor } : f) };
782
- }
783
- if (place.kind === "column" || place.kind === "rail") {
784
- if (place.kind === "rail" && !scene.rails?.[place.side]?.accepts.includes(item.kind)) return scene;
785
- const folded = place.kind === "rail" ? { ...item, from: Math.max(1, item.from ?? 0) } : item;
786
- const made = insertColumn(withItem(lift(scene, id), folded), id, place.side, place.ring);
787
- return made ?? scene;
788
- }
789
- const edge = scene.edges.find((e) => e.id === place.edge);
790
- if (!edge || !edge.accepts.includes(item.kind)) return scene;
791
- const zone = place.zone ?? (zoneTakes(edge, zoneOf(item), item.kind) ? zoneOf(item) : zonesTaking(edge, item.kind)[0]);
792
- if (!zone || !zoneTakes(edge, zone, item.kind)) return scene;
793
- const { align: _drop, ...rest } = item;
794
- const moved = zone === "start" ? rest : { ...rest, align: zone };
795
- const lifted = withItem(lift(scene, id), moved);
796
- const target = lifted.edges.find((e) => e.id === place.edge);
797
- if (!target) return scene;
798
- const k = insertIndex(lifted, target, zone, place.at);
799
- return { ...lifted, edges: lifted.edges.map((e) => e === target ? { ...e, items: [...e.items.slice(0, k), id, ...e.items.slice(k)] } : e) };
800
- }
801
- function floatAt(scene, id, at, orient2) {
802
- const item = itemOf(scene, id);
803
- if (!item || item.fixed || !scene.floatKinds.includes(item.kind)) return scene;
804
- const was = scene.floats.find((f) => f.id === id);
805
- const o = orient2 ?? was?.orient;
806
- const size = floatSizeOf(item, o ?? floatOrientOf(item));
807
- const float = at ? { id, rect: { x: at.x, y: at.y, width: at.width, height: at.height }, ...o ? { orient: o } : {} } : { id, rect: { x: 0, y: 0, width: size.width, height: size.height }, anchor: "content-top-right", ...o ? { orient: o } : {} };
808
- const next = at && at.width > 0 && at.height > 0 ? { ...item, floatSize: { width: at.width, height: at.height } } : item;
809
- const lifted = lift(withItem(scene, next), id);
810
- return { ...lifted, floats: [...lifted.floats, float] };
811
- }
812
- function fold(scene, id, on) {
813
- const item = itemOf(scene, id);
814
- if (!item || item.fixed) return scene;
815
- if (on) return (item.from ?? 0) >= 1 ? scene : withItem(scene, { ...item, from: 1 });
816
- if (item.from === void 0) return scene;
817
- const { from: _f, ...rest } = item;
818
- return withItem(scene, rest);
819
- }
820
- var nextAge = (scene) => Object.values(scene.items).reduce((m, it) => Math.max(m, Number.isFinite(it.used) ? it.used : 0), 0) + 1;
821
- function use(scene, id) {
822
- const item = itemOf(scene, id);
823
- if (!item) return scene;
824
- return withItem(scene, { ...item, used: nextAge(scene) });
825
- }
826
- function keep(scene, id, on) {
827
- const item = itemOf(scene, id);
828
- if (!item || !!item.kept === on) return scene;
829
- if (on) return withItem(scene, { ...item, kept: true });
830
- const { kept: _k, ...rest } = item;
831
- return withItem(scene, rest);
832
- }
833
- function open(scene, id) {
834
- const item = itemOf(scene, id);
835
- if (!item || !item.closed) return scene;
836
- const { closed: _c, ...rest } = item;
837
- return withItem(scene, rest);
838
- }
839
- function close(scene, id) {
840
- const item = itemOf(scene, id);
841
- if (!item || item.closed || item.fixed) return scene;
842
- return withItem(scene, { ...item, closed: true });
843
- }
844
- function raise(scene, id) {
845
- const i = scene.floats.findIndex((f) => f.id === id);
846
- if (i < 0 || i === scene.floats.length - 1) return scene;
847
- return { ...scene, floats: [...scene.floats.slice(0, i), ...scene.floats.slice(i + 1), scene.floats[i]] };
848
- }
849
- function orient(scene, id, to) {
850
- const i = scene.floats.findIndex((f) => f.id === id);
851
- const item = scene.items[id];
852
- if (i < 0 || !item || (scene.floats[i].orient ?? floatOrientOf(item)) === to) return scene;
853
- return { ...scene, floats: scene.floats.map((f, k) => k === i ? { ...f, orient: to } : f) };
854
- }
855
- function resize(scene, edgeId, size) {
856
- const edge = scene.edges.find((e) => e.id === edgeId);
857
- if (!edge) return scene;
858
- if (!Number.isFinite(size)) {
859
- if (edge.size === void 0) return scene;
860
- const { size: _s, ...rest } = edge;
861
- return { ...scene, edges: scene.edges.map((e) => e === edge ? rest : e) };
862
- }
863
- return { ...scene, edges: scene.edges.map((e) => e === edge ? { ...e, size } : e) };
864
- }
865
- function setSide(scene, edgeId, side) {
866
- const edge = scene.edges.find((e) => e.id === edgeId);
867
- if (!edge || edge.side === side || axisOf(edge.side) !== axisOf(side)) return scene;
868
- const moved = { ...edge, side, ring: 0 };
869
- const others = scene.edges.filter((e) => e !== edge).map((e) => e.side === side ? { ...e, ring: e.ring + 1 } : e);
870
- return { ...scene, edges: closeRings([moved, ...others], /* @__PURE__ */ new Set([edge.side, side])) };
871
- }
872
- function homeFor(scene, id, start) {
873
- for (const e of start.edges) {
874
- const k = e.items.indexOf(id);
875
- if (k < 0) continue;
876
- const zone = zoneOf(itemOf(start, id) ?? itemOf(scene, id));
877
- const found = scene.edges.find((x) => x.id === e.id);
878
- const here = found && (e.column !== true || found.items.some((x) => e.items.includes(x))) ? found : void 0;
879
- if (here) {
880
- const now = here.items.filter((x) => x !== id && zoneOf(itemOf(scene, x)) === zone);
881
- const next = e.items.slice(k + 1).find((x) => now.includes(x));
882
- return { kind: "gap", edge: e.id, zone, at: next ? now.indexOf(next) : now.length };
883
- }
884
- if (e.column === true && (e.side === "left" || e.side === "right")) return { kind: "column", side: e.side, ring: sideRings(start.edges, e.side).get(e.id) ?? e.ring };
885
- return null;
886
- }
887
- const f = start.floats.find((x) => x.id === id);
888
- if (f) return { kind: "float", rect: f.rect, ...f.anchor ? { anchor: f.anchor } : {} };
889
- return null;
890
- }
891
- function dockHome(scene, id, start) {
892
- const place = homeFor(scene, id, start);
893
- if (!place) return scene;
894
- const moved = moveTo(scene, id, place);
895
- const item = itemOf(moved, id);
896
- if (!item) return scene;
897
- const from = itemOf(start, id)?.from;
898
- if (item.from === from) return moved;
899
- const { from: _f, ...plain } = item;
900
- return withItem(moved, from === void 0 ? plain : { ...plain, from });
901
- }
902
- function clampFloat(r, stage, drawn = r) {
903
- const at = clampInto({ x: r.x, y: r.y, width: drawn.width, height: drawn.height }, stage);
904
- return { x: at.x, y: at.y, width: r.width, height: r.height };
905
- }
906
- function drawnFloatSize(item, f) {
907
- const s = floatShape(item, f);
908
- return { width: s.width, height: s.height };
909
- }
910
-
911
- // src/scene/store.ts
912
- var ANCHORS = ["content-top-left", "content-top", "content-top-right", "content-right", "content-bottom-right", "content-bottom", "content-bottom-left", "content-left"];
913
- var isNum = (x) => typeof x === "number" && Number.isFinite(x);
914
- var isSize = (x) => isNum(x) && x > 0;
915
- var isObject = (x) => typeof x === "object" && x !== null && !Array.isArray(x);
916
- var record = () => /* @__PURE__ */ Object.create(null);
917
- function storeScene(scene) {
918
- const items = record();
919
- for (const [id, it] of Object.entries(scene.items)) {
920
- if (id === "__proto__") continue;
921
- items[id] = {
922
- used: isNum(it.used) ? it.used : 0,
923
- ...it.kept ? { kept: true } : {},
924
- ...isNum(it.from) && it.from >= 1 ? { from: Math.floor(it.from) } : {},
925
- ...it.closed ? { closed: true } : {},
926
- ...it.align === "center" || it.align === "end" ? { align: it.align } : {},
927
- ...it.floatSize && isSize(it.floatSize.width) && isSize(it.floatSize.height) ? { floatSize: { width: it.floatSize.width, height: it.floatSize.height } } : {}
928
- };
929
- }
930
- return {
931
- v: 1,
932
- edges: scene.edges.map((e) => ({ id: e.id, side: e.side, ring: e.ring, items: [...e.items], ...isNum(e.size) ? { size: e.size } : {}, ...e.column === true ? { column: true } : {} })),
933
- floats: scene.floats.filter((f) => isNum(f.rect.x) && isNum(f.rect.y) && isSize(f.rect.width) && isSize(f.rect.height)).map((f) => ({ id: f.id, x: f.rect.x, y: f.rect.y, width: f.rect.width, height: f.rect.height, ...f.orient ? { orient: f.orient } : {}, ...f.anchor ? { anchor: f.anchor } : {} })),
934
- items
935
- };
936
- }
937
- function readScene(data, base) {
938
- try {
939
- return read(data, base);
940
- } catch (e) {
941
- return { scene: base, dropped: [], error: `unreadable: ${e instanceof Error ? e.message : typeof e}` };
942
- }
943
- }
944
- var words = (v) => typeof v === "number" ? String(v) : typeof v === "string" ? JSON.stringify(v.length > 24 ? `${v.slice(0, 24)}\u2026` : v) : typeof v;
945
- function withFixed(baseIds, stored, isFixed) {
946
- const out = [...stored];
947
- const missing = (id) => isFixed(id) && !stored.includes(id);
948
- let tail = baseIds.length;
949
- while (tail > 0 && isFixed(baseIds[tail - 1])) tail--;
950
- baseIds.forEach((id, i) => {
951
- if (missing(id) && i < tail) out.splice(Math.min(i, out.length), 0, id);
952
- });
953
- return [...out, ...baseIds.slice(tail).filter(missing)];
954
- }
955
- function read(data, base) {
956
- if (!isObject(data)) return { scene: base, dropped: [], error: "not an object" };
957
- if (data.v !== 1) return { scene: base, dropped: [], error: `unknown version ${words(data.v)} (this reads version 1)` };
958
- const dropped = [];
959
- const drop = (what) => {
960
- if (!dropped.includes(what)) dropped.push(what);
961
- };
962
- for (const k of Object.keys(data)) if (!["v", "edges", "floats", "items"].includes(k)) drop(k);
963
- const known = (id) => typeof id === "string" && id !== "__proto__" && Object.hasOwn(base.items, id);
964
- const isFixed = (id) => known(id) && base.items[id].fixed === true;
965
- const items = { ...base.items };
966
- if (data.items !== void 0 && !isObject(data.items)) drop("items");
967
- else if (isObject(data.items)) {
968
- for (const id of Object.keys(data.items)) {
969
- const s = data.items[id];
970
- if (!known(id) || !isObject(s)) {
971
- drop(`item:${id}`);
972
- continue;
973
- }
974
- const b = base.items[id];
975
- const { kept: _k, from: _f, closed: _c, align: _a, floatSize: _s, ...plain } = b;
976
- let it = { ...plain, used: b.used };
977
- for (const k of Object.keys(s)) if (!["used", "kept", "from", "closed", "align", "floatSize"].includes(k)) drop(`item:${id}:${k}`);
978
- if (isNum(s.used)) it = { ...it, used: s.used };
979
- else if (s.used !== void 0) drop(`item:${id}:used`);
980
- if (s.kept === true) it = { ...it, kept: true };
981
- else if (s.kept !== void 0 && s.kept !== false) drop(`item:${id}:kept`);
982
- if (isNum(s.from) && Number.isInteger(s.from) && s.from >= 1 && !b.fixed) it = { ...it, from: s.from };
983
- else if (s.from !== void 0) drop(`item:${id}:from`);
984
- if (b.fixed && b.from !== void 0) it = { ...it, from: b.from };
985
- if (s.closed === true && !b.fixed) it = { ...it, closed: true };
986
- else if (s.closed !== void 0 && s.closed !== false) drop(`item:${id}:closed`);
987
- if (s.align === "center" || s.align === "end") it = { ...it, align: s.align };
988
- else if (s.align !== void 0 && s.align !== "start") drop(`item:${id}:align`);
989
- if (b.fixed && b.align) it = { ...it, align: b.align };
990
- const fs = s.floatSize;
991
- if (isObject(fs) && isSize(fs.width) && isSize(fs.height)) it = { ...it, floatSize: { width: fs.width, height: fs.height } };
992
- else if (fs !== void 0) drop(`item:${id}:floatSize`);
993
- items[id] = it;
994
- }
995
- }
996
- const placed = /* @__PURE__ */ new Set();
997
- const order = /* @__PURE__ */ new Map();
998
- const sizes = /* @__PURE__ */ new Map();
999
- const made = [];
1000
- const rings = /* @__PURE__ */ new Map();
1001
- const moves = /* @__PURE__ */ new Map();
1002
- if (data.edges !== void 0 && !Array.isArray(data.edges)) drop("edges");
1003
- for (const e of Array.isArray(data.edges) ? data.edges : []) {
1004
- if (!isObject(e) || typeof e.id !== "string") {
1005
- drop("an edge with no id");
1006
- continue;
1007
- }
1008
- let edge = base.edges.find((x) => x.id === e.id) ?? made.find((x) => x.id === e.id);
1009
- if (!edge && e.column === true && (e.side === "left" || e.side === "right") && base.columns?.[e.side]) {
1010
- const t = base.columns[e.side];
1011
- edge = { id: e.id, side: e.side, ring: isNum(e.ring) ? e.ring : Infinity, accepts: t.accepts, items: [], cross: t.cross, ...t.gap !== void 0 ? { gap: t.gap } : {}, ...t.pad ? { pad: t.pad } : {}, ...t.menu ? { menu: t.menu } : {}, column: true };
1012
- made.push(edge);
1013
- }
1014
- if (!edge || order.has(edge.id)) {
1015
- drop(`edge:${e.id}`);
1016
- continue;
1017
- }
1018
- if (edge.column === true && isNum(e.ring)) rings.set(edge.id, e.ring);
1019
- if (e.side !== void 0 && !SIDES.includes(e.side)) drop(`edge:${e.id}:side`);
1020
- else if (edge.column !== true && typeof e.side === "string" && e.side !== edge.side && axisOf(e.side) === axisOf(edge.side)) moves.set(edge.id, e.side);
1021
- const ids = [];
1022
- for (const id of Array.isArray(e.items) ? e.items : []) {
1023
- if (!known(id) || placed.has(id)) drop(`edge:${edge.id}:${String(id)}`);
1024
- else if (isFixed(id)) {
1025
- if (edge.items.includes(id) && base.edges.some((x) => x.id === edge.id)) {
1026
- ids.push(id);
1027
- placed.add(id);
1028
- } else drop(`edge:${edge.id}:${id}`);
1029
- } else if (!edge.accepts.includes(items[id].kind)) drop(`edge:${edge.id}:${id}`);
1030
- else {
1031
- ids.push(id);
1032
- placed.add(id);
1033
- }
1034
- }
1035
- order.set(edge.id, ids);
1036
- if (isNum(e.size)) sizes.set(edge.id, e.size);
1037
- else if (e.size !== void 0) drop(`edge:${edge.id}:size`);
1038
- else sizes.set(edge.id, void 0);
1039
- }
1040
- const floats = [];
1041
- if (data.floats !== void 0 && !Array.isArray(data.floats)) drop("floats");
1042
- for (const f of Array.isArray(data.floats) ? data.floats : []) {
1043
- if (!isObject(f) || typeof f.id !== "string") {
1044
- drop("a float with no id");
1045
- continue;
1046
- }
1047
- const id = f.id;
1048
- if (!known(id) || placed.has(id) || isFixed(id) || !base.floatKinds.includes(items[id].kind) || !isNum(f.x) || !isNum(f.y) || !isSize(f.width) || !isSize(f.height)) {
1049
- drop(`float:${id}`);
1050
- continue;
1051
- }
1052
- for (const k of Object.keys(f)) if (k === "pinned") drop(`pinned:${id}`);
1053
- else if (!["id", "x", "y", "width", "height", "orient", "anchor"].includes(k)) drop(`float:${id}:${k}`);
1054
- const o = f.orient === "row" || f.orient === "column" ? f.orient : void 0;
1055
- if (f.orient !== void 0 && !o) drop(`float:${id}:orient`);
1056
- const a = ANCHORS.includes(f.anchor) ? f.anchor : void 0;
1057
- if (f.anchor !== void 0 && !a) drop(`float:${id}:anchor`);
1058
- placed.add(id);
1059
- const r = rect(f.x, f.y, f.width, f.height);
1060
- const drawn = drawnFloatSize(items[id], { rect: r, ...o ? { orient: o } : {} });
1061
- floats.push({ id, rect: clampFloat(r, base.stage, drawn), ...o ? { orient: o } : {}, ...a ? { anchor: a } : {} });
1062
- }
1063
- const edges = [...base.edges, ...made].map((edge) => {
1064
- const stored = order.get(edge.id) ?? [];
1065
- const rest = edge.items.filter((id) => !isFixed(id) && !placed.has(id));
1066
- for (const id of rest) placed.add(id);
1067
- let tail = stored.length;
1068
- while (tail > 0 && isFixed(stored[tail - 1])) tail--;
1069
- const listed = [...stored.slice(0, tail), ...rest, ...stored.slice(tail)];
1070
- const items1 = withFixed(edge.items, listed, isFixed);
1071
- const size = sizes.has(edge.id) ? sizes.get(edge.id) : edge.size;
1072
- const { size: _s, ...plain } = edge;
1073
- return { ...plain, ring: rings.get(edge.id) ?? edge.ring, items: items1, ...size !== void 0 ? { size } : {} };
1074
- });
1075
- for (const f of base.floats) if (!placed.has(f.id)) floats.push(f);
1076
- let scene = prune({ ...base, items, edges: closeRings(edges, new Set(made.length > 0 || rings.size > 0 ? ["left", "right"] : [])), floats });
1077
- for (const [id, side] of moves) scene = setSide(scene, id, side);
1078
- return { scene, dropped };
1079
- }
1080
-
1081
- // src/scene/targets.ts
1082
- var RAIL_REACH = 24;
1083
- var EDGE_KINDS = {
1084
- bar: { zoneExtent: "thirds", opens: "any", inward: 0, place: DEFAULT_HIT_BAND / 2, phase: null, layer: 10, overreach: 30, priority: 2, reach: Infinity, virtual: false },
1085
- sideBar: { zoneExtent: "thirds", opens: "any", inward: 0, place: DEFAULT_HIT_BAND / 2, phase: null, layer: 11, overreach: 31, priority: 2, reach: Infinity, virtual: false },
1086
- column: { zoneExtent: "solved", opens: "start", inward: 0, place: DEFAULT_HIT_BAND / 2, phase: null, layer: 12, overreach: 32, priority: 2, reach: Infinity, virtual: false },
1087
- barBetween: { zoneExtent: "thirds", opens: "any", inward: 0, place: DEFAULT_HIT_BAND / 2, phase: null, layer: 13, overreach: 33, priority: 2, reach: Infinity, virtual: false },
1088
- rail: { zoneExtent: "thirds", opens: "any", inward: RAIL_REACH, place: DEFAULT_HIT_BAND / 2, phase: null, layer: 14, overreach: 34, priority: 2, reach: Infinity, virtual: false },
1089
- railStop: { zoneExtent: "whole", opens: "any", inward: 0, place: DEFAULT_HIT_BAND / 2, phase: 2, layer: 9, overreach: 39, priority: 1, reach: Infinity, virtual: false },
1090
- columnLine: { zoneExtent: "whole", opens: "any", inward: 0, place: DEFAULT_HIT_BAND / 2, phase: 2, layer: 21, overreach: 41, priority: 2, reach: COLUMN_REACH, virtual: true }
1091
- };
1092
- function edgeKindOf(scene, e) {
1093
- if (e.compact) return "rail";
1094
- if (e.side === "top" || e.side === "bottom") return e.span === "between" ? "barBetween" : "bar";
1095
- return isSideBar(scene, e) ? "sideBar" : "column";
1096
- }
1097
- var opensEmpty = (scene, edge, kind) => EDGE_KINDS[edgeKindOf(scene, edge)].opens === "any" || zoneTakes(edge, "start", kind);
1098
- function reservedRect(scene, l, id) {
1099
- const it = l.items[id];
1100
- const e = it.edge ? l.edges[it.edge] : void 0;
1101
- const f = e ? formsOf(scene.items[id], axisOf(e.side))[it.form] : void 0;
1102
- if (!e || !f?.main.grows) return it.rect;
1103
- const column = axisOf(e.side) === "column";
1104
- const len = column ? it.rect.height : it.rect.width;
1105
- const keep2 = Math.min(len, Math.max(0, Number.isFinite(f.main.natural) ? f.main.natural : len));
1106
- const shift = it.zone === "end" ? len - keep2 : it.zone === "center" ? (len - keep2) / 2 : 0;
1107
- return column ? rect(it.rect.x, it.rect.y + shift, it.rect.width, keep2) : rect(it.rect.x + shift, it.rect.y, keep2, it.rect.height);
1108
- }
1109
- function placeKey(s, id) {
1110
- const it = Object.hasOwn(s.items, id) ? s.items[id] : void 0;
1111
- const byZone = (items) => ZONES.flatMap((z) => items.filter((x) => zoneOf(s.items[x]) === z));
1112
- return JSON.stringify({
1113
- edges: s.edges.map((e) => JSON.stringify([e.side, e.ring, e.column === true ? "" : e.id, byZone(e.items), e.size ?? null])).sort(),
1114
- floats: s.floats.map((f) => [f.id, f.rect, f.anchor ?? null, f.orient ?? null]),
1115
- from: it?.from ?? 0,
1116
- align: it?.align ?? "start"
1117
- });
1118
- }
1119
- function slotAt(c, zone, line, id, place, lands, home = false) {
1120
- const result = c.settle(use(moveTo(c.scene, c.dragged, place), c.dragged));
1121
- const isHome = home || c.isHome(result);
1122
- return { zone, line, id, place, result, home: isHome, ok: isHome || !!result.layout.items[c.dragged] && lands(result) && c.sound(result.layout) };
1123
- }
1124
- var less = (spans, away) => away.reduce((acc, [ra, rb]) => acc.flatMap(([a, b]) => rb <= a || ra >= b ? [[a, b]] : [[a, Math.max(a, ra)], [Math.min(b, rb), b]].filter(([u, w]) => w - u > EPS)), [...spans]);
1125
- function zoneSpans(scene, base, edge, reach, probe) {
1126
- const el = base.edges[edge.id];
1127
- const column = axisOf(edge.side) === "column";
1128
- const main0 = column ? el.rect.y : el.rect.x;
1129
- const length = column ? el.rect.height : el.rect.width;
1130
- if (EDGE_KINDS[edgeKindOf(scene, edge)].zoneExtent === "thirds")
1131
- return ZONES.map((zone, k) => ({ zone, a: k === 0 ? main0 - reach : main0 + length * k / 3, b: k === 2 ? main0 + length + reach : main0 + length * (k + 1) / 3 }));
1132
- const at = (r) => column ? [r.y, r.y + r.height] : [r.x, r.x + r.width];
1133
- const declared = new Set(edge.zoneAccepts ? ZONES.filter((z) => edge.zoneAccepts[z] !== void 0) : ["start"]);
1134
- const placed = /* @__PURE__ */ new Map();
1135
- for (const [id, it] of Object.entries(base.items)) {
1136
- if (it.edge !== edge.id || it.inMenu) continue;
1137
- const z = zoneOf(scene.items[id]);
1138
- const [a, b] = at(it.rect);
1139
- const was = placed.get(z);
1140
- placed.set(z, was ? [Math.min(was[0], a), Math.max(was[1], b)] : [a, b]);
1141
- declared.add(z);
1142
- }
1143
- const anchor = { start: main0, center: main0 + length / 2, end: main0 + length };
1144
- const spans = ZONES.filter((z) => declared.has(z)).map((zone) => ({ zone, s: placed.get(zone) ?? probe(zone) ?? [anchor[zone], anchor[zone]] }));
1145
- return spans.map(({ zone, s }, i) => ({
1146
- zone,
1147
- a: i === 0 ? main0 - reach : (spans[i - 1].s[1] + s[0]) / 2,
1148
- b: i === spans.length - 1 ? main0 + length + reach : (s[1] + spans[i + 1].s[0]) / 2
1149
- }));
1150
- }
1151
- function edgeSpec(c, edge) {
1152
- const { scene, rest, now, base, dragged, kind } = c;
1153
- const el = base.edges[edge.id];
1154
- if (!el || el.folded || el.rides) return null;
1155
- const edgeKind = edgeKindOf(rest, edge);
1156
- const data = EDGE_KINDS[edgeKind];
1157
- const column = axisOf(edge.side) === "column";
1158
- const at = (r2) => column ? r2.y : r2.x;
1159
- const len = (r2) => column ? r2.height : r2.width;
1160
- const main0 = at(el.rect);
1161
- const length = len(el.rect);
1162
- const empty = el.stack.items.every((p) => p.kind === "menu") && !el.menu;
1163
- const accepts = edge.accepts.includes(kind);
1164
- if (!accepts && (empty || area(el.rect) <= EPS)) return null;
1165
- if (accepts && empty && !opensEmpty(rest, edge, kind)) return null;
1166
- const ownNow = now.items[dragged];
1167
- const ownHere = ownNow && ownNow.edge === edge.id && !ownNow.floating && !ownNow.inMenu ? ownNow.rect : null;
1168
- const restEdge = rest.edges.find((e) => e.id === edge.id);
1169
- const slotId = (zone, at2) => restEdge.items.length === 0 ? `strip:${edge.id}:${zone}` : `gap:${edge.id}:${zone}:${at2}`;
1170
- const titles = edge.items.filter((id) => base.items[id]?.edge === edge.id && base.items[id].fixed && !!formsOf(scene.items[id], axisOf(edge.side))[base.items[id].form]?.main.grows);
1171
- const lands = ({ layout: after }) => {
1172
- const me = after.items[dragged];
1173
- if (me.edge !== edge.id || me.inMenu) return false;
1174
- if (titles.length === 0) return true;
1175
- if (me.form !== 0) return false;
1176
- return edge.items.every((id) => {
1177
- const a = after.items[id];
1178
- const b = base.items[id];
1179
- if (!a || !b || b.edge !== edge.id || id === dragged) return true;
1180
- if (!titles.includes(id)) return a.form === b.form && !!a.inMenu === !!b.inMenu;
1181
- const f = formsOf(scene.items[id], axisOf(edge.side))[a.form];
1182
- return !f || (column ? a.rect.height : a.rect.width) >= formMain(f, "natural", after.edges[edge.id].cross) - 0.5;
1183
- });
1184
- };
1185
- const pad = { start: Math.max(0, edge.pad?.start ?? 0), end: Math.max(0, edge.pad?.end ?? 0) };
1186
- const menuLen = el.menu ? len(el.menu.rect) : 0;
1187
- const occupied = (id) => base.items[id]?.fixed ? reservedRect(scene, base, id) : base.items[id].rect;
1188
- const shown = (id) => base.items[id]?.edge === edge.id && !base.items[id].inMenu;
1189
- const drawnOf = (z) => restEdge.items.filter((id) => zoneOf(rest.items[id]) === z && shown(id)).map(occupied);
1190
- const startEnd = (() => {
1191
- const s = drawnOf("start");
1192
- return s.length ? at(s[s.length - 1]) + len(s[s.length - 1]) : main0 + pad.start;
1193
- })();
1194
- const endStart = (() => {
1195
- const e = drawnOf("end");
1196
- return e.length ? at(e[0]) : main0 + length - pad.end - menuLen;
1197
- })();
1198
- const anchorLine = { start: main0 + pad.start, center: Math.min(Math.max(main0 + length / 2, startEnd), Math.max(startEnd, endStart)), end: main0 + length - pad.end - menuLen };
1199
- const slots = [];
1200
- for (const zone of ZONES) {
1201
- if (!accepts || !zoneTakes(edge, zone, kind)) continue;
1202
- const members = restEdge.items.filter((id) => zoneOf(rest.items[id]) === zone);
1203
- for (let k = 0; k <= members.length; k++) {
1204
- const next = members.slice(k).find(shown);
1205
- const before = members.slice(0, k).reverse().find(shown);
1206
- const line = next ? at(occupied(next)) : before ? at(occupied(before)) + len(occupied(before)) : anchorLine[zone];
1207
- const s = slotAt(c, zone, line, slotId(zone, k), { kind: "gap", edge: edge.id, zone, at: k }, lands);
1208
- const movesFixed = restEdge.items.some((id) => {
1209
- const b = base.items[id];
1210
- const a = s.result.layout.items[id];
1211
- return !!b?.fixed && b.edge === edge.id && (!a || Math.abs(a.rect.x - b.rect.x) > 0.5 || Math.abs(a.rect.y - b.rect.y) > 0.5);
1212
- });
1213
- if (!movesFixed) slots.push(s);
1214
- }
1215
- }
1216
- const spanOf = (r2) => [at(r2), at(r2) + len(r2)];
1217
- const grows = (id) => !!formsOf(scene.items[id], axisOf(edge.side))[base.items[id].form]?.main.grows;
1218
- const reserved = Object.entries(base.items).filter(([id, p]) => p.edge === edge.id && p.fixed && !p.inMenu && !grows(id)).map(([, p]) => spanOf(p.rect));
1219
- const zones = zoneSpans(rest, base, edge, c.reach, (zone) => {
1220
- const s = slots.find((x) => x.zone === zone && x.place.kind === "gap" && x.place.at === 0);
1221
- const me = s?.result.layout.items[dragged];
1222
- return me && me.edge === edge.id && me.zone === zone ? spanOf(me.rect) : null;
1223
- });
1224
- const r = el.rect;
1225
- const strip = Math.max(0, Number.isFinite(edge.strip) ? edge.strip : DEFAULT_STRIP);
1226
- const nowCross = now.edges[edge.id]?.cross ?? 0;
1227
- const outer = edge.side === "left" ? r.x : edge.side === "right" ? r.x + r.width : edge.side === "top" ? r.y : r.y + r.height;
1228
- const inward = (depth) => edge.side === "left" || edge.side === "top" ? [outer, outer + depth] : [outer - depth, outer];
1229
- const across = (after) => inward((el.cross > 0 || nowCross > 0 ? Math.max(el.cross, nowCross, after?.edges[edge.id]?.cross ?? 0) : strip) + data.inward);
1230
- const ownCross = ownNow?.edge === edge.id ? el.cross : Math.max(el.cross, nowCross);
1231
- const drawn = ownCross > 0;
1232
- const marker = (s) => {
1233
- const slot = s.result.layout.items[dragged].rect;
1234
- const e = s.result.layout.edges[edge.id];
1235
- return column ? rect(e.rect.x, slot.y, e.cross, 0) : rect(slot.x, e.rect.y, 0, e.cross);
1236
- };
1237
- const takes = (zone) => accepts && zoneTakes(edge, zone, kind);
1238
- return {
1239
- id: edge.id,
1240
- key: edge.id,
1241
- kind: edgeKind,
1242
- lay: c.lay.get(edge.id),
1243
- vertical: column,
1244
- zones,
1245
- slots,
1246
- refuse: (zone) => takes(zone) ? "no-room" : accepts || area(el.rect) > EPS ? "not-accepted" : null,
1247
- own: ownHere,
1248
- holds: !!ownNow && ownNow.edge === edge.id && !ownNow.floating,
1249
- reserved,
1250
- across,
1251
- owned: inward(drawn ? ownCross : Math.min(strip, data.place)),
1252
- ownedAlong: null,
1253
- drawn,
1254
- marker
1255
- };
1256
- }
1257
- function tileEdge(c, spec, cuts, lost) {
1258
- const data = EDGE_KINDS[spec.kind];
1259
- const v = spec.vertical;
1260
- const own = spec.own ? v ? [spec.own.y, spec.own.y + spec.own.height] : [spec.own.x, spec.own.x + spec.own.width] : null;
1261
- const band = (a, b, after) => {
1262
- const [c0, c1] = spec.across(after);
1263
- return v ? rect(c0, a, c1 - c0, b - a) : rect(a, c0, b - a, c1 - c0);
1264
- };
1265
- const keepAt = (line, h) => v ? { x: h.x + h.width / 2, y: line } : { x: line, y: h.y + h.height / 2 };
1266
- const tile = (zone, hit, more) => ({ edge: spec.id, key: spec.key, edgeKind: spec.kind, vertical: v, zone, hit, ...more });
1267
- const refused = (zone, hit, reason) => tile(zone, hit, { kind: "refuse", id: "", order: data.layer, priority: data.priority, keep: null, reason });
1268
- const reserved = spec.reserved;
1269
- const split = (t) => {
1270
- const h = t.hit;
1271
- const [h0, h1] = v ? [h.x, h.x + h.width] : [h.y, h.y + h.height];
1272
- const [c0, c1] = [Math.max(h0, spec.owned[0]), Math.min(h1, spec.owned[1])];
1273
- const part = (a, b) => v ? rect(a, h.y, b - a, h.height) : rect(h.x, a, h.width, b - a);
1274
- const beyond = less([[h0, h1]], [spec.owned]);
1275
- if (beyond.length === 0) return [t];
1276
- if (c1 - c0 <= EPS) return [{ ...t, order: data.overreach }];
1277
- const core = { ...t, hit: part(c0, c1), keep: t.keep && keepAt(v ? t.keep.y : t.keep.x, part(c0, c1)) };
1278
- return [core, ...beyond.map(([a, b]) => ({ ...t, kind: "reach", of: core, order: data.overreach, hit: part(a, b), keep: null }))];
1279
- };
1280
- const landing = (s) => s.result.layout.items[c.dragged]?.rect;
1281
- const sameMove = (a, b) => {
1282
- if (a.zone !== b.zone) return false;
1283
- if (a.home && b.home) return true;
1284
- const [p, q] = [landing(a), landing(b)];
1285
- return Math.abs(a.line - b.line) < EPS && (!p || !q ? p === q : Math.abs(p.x - q.x) + Math.abs(p.y - q.y) + Math.abs(p.width - q.width) + Math.abs(p.height - q.height) < 0.01);
1286
- };
1287
- const slots = spec.slots.filter((s, i) => !spec.slots.slice(0, i).some((o) => sameMove(o, s)));
1288
- const out = [];
1289
- for (const z of spec.zones) {
1290
- const room = less([[z.a, z.b]], [...cuts, ...reserved]).filter(([a, b]) => b - a > EPS);
1291
- if (room.length === 0) continue;
1292
- const pieces = own ? less(room, [own]) : room;
1293
- const mine = slots.filter((s) => s.zone === z.zone && !(spec.holds && s.home) && pieces.some(([a, b]) => b > s.line - data.reach + EPS && a < s.line + data.reach - EPS));
1294
- const reason = spec.refuse(z.zone);
1295
- for (const [pa, pb] of pieces) {
1296
- const lines = mine.map((s) => s.line);
1297
- const lo = Math.min(z.a, ...lines);
1298
- const hi = Math.max(z.b, ...lines);
1299
- const free = less([[z.a, z.b]], reserved);
1300
- const freeLen = free.reduce((n, [a, b]) => n + (b - a), 0);
1301
- const spread = (x) => {
1302
- if (hi - lo <= EPS || freeLen <= EPS) return x;
1303
- let d = (x - lo) / (hi - lo) * freeLen;
1304
- for (const [a, b] of free) {
1305
- if (d <= b - a + EPS) return a + Math.min(d, b - a);
1306
- d -= b - a;
1307
- }
1308
- return free[free.length - 1][1];
1309
- };
1310
- const past = lines.some((x) => x < z.a - EPS || x > z.b + EPS);
1311
- const nearer = (p, [a, b]) => p > a && p < b ? p - a < b - p ? a : b : p;
1312
- const proj = (x) => {
1313
- const q = reserved.reduce((r, s) => nearer(r, s), past ? spread(x) : x);
1314
- const p = own ? nearer(q, own) : q;
1315
- if (pieces.some(([a, b]) => p >= a - EPS && p <= b + EPS)) return p;
1316
- return pieces.map(([a, b]) => Math.min(Math.max(p, a), b)).reduce((m, y) => Math.abs(y - p) < Math.abs(m - p) ? y : m);
1317
- };
1318
- const inPiece = mine.filter((s) => proj(s.line) >= pa - EPS && proj(s.line) <= pb + EPS).sort((a, b) => proj(a.line) - proj(b.line) || a.line - b.line);
1319
- const projected = inPiece.map((s) => proj(s.line));
1320
- const at = projected.some((p, i) => i > 0 && p - projected[i - 1] <= EPS) ? inPiece.map((_, i) => pa + (i + 0.5) * (pb - pa) / inPiece.length) : projected;
1321
- if (inPiece.length === 0) {
1322
- const lone = reason === "no-room" && mine.length === 0 && slots.some((s) => s.zone === z.zone && s.home);
1323
- const near = mine.length === 0 || mine.some((s) => pb > s.line - data.reach + EPS && pa < s.line + data.reach - EPS);
1324
- if (reason && near) out.push(...split(refused(z.zone, band(pa, pb), lone ? "own-place" : reason)));
1325
- continue;
1326
- }
1327
- inPiece.forEach((s, i) => {
1328
- const p = at[i];
1329
- const a = Math.max(i === 0 ? pa : (at[i - 1] + p) / 2, p - data.reach);
1330
- const b = Math.min(i === inPiece.length - 1 ? pb : (p + at[i + 1]) / 2, p + data.reach);
1331
- if (b - a <= EPS) {
1332
- if (s.ok) lost.push(s.id);
1333
- return;
1334
- }
1335
- const line = Math.min(Math.max(p, a), b);
1336
- const hit = band(a, b, s.result.layout);
1337
- if (!s.ok) out.push(...split(refused(z.zone, hit, "no-room")));
1338
- else if (s.home) out.push(...split(tile(z.zone, hit, { kind: "home", id: s.id, order: data.layer, priority: data.priority, keep: keepAt(line, hit), place: { kind: "home" } })));
1339
- else out.push(...split(tile(z.zone, hit, { kind: "slot", id: s.id, order: data.layer, priority: data.priority, keep: keepAt(line, hit), place: s.place, result: s.result, marker: spec.marker(s), slot: s.result.layout.items[c.dragged].rect })));
1340
- });
1341
- }
1342
- }
1343
- if (own && spec.own) {
1344
- const home = spec.slots.find((s) => s.home);
1345
- const [a, b] = own;
1346
- const h = band(a, b);
1347
- const o = spec.own;
1348
- const hit = v ? rect(Math.min(h.x, o.x), a, Math.max(h.x + h.width, o.x + o.width) - Math.min(h.x, o.x), b - a) : rect(a, Math.min(h.y, o.y), b - a, Math.max(h.y + h.height, o.y + o.height) - Math.min(h.y, o.y));
1349
- out.push(tile(home?.zone ?? zoneOf(c.scene.items[c.dragged]), hit, { kind: "home", id: home ? home.id : `home:${c.dragged}`, order: 0, priority: home ? data.priority : 1, keep: null, place: { kind: "home" } }));
1350
- }
1351
- return out;
1352
- }
1353
- function columnLines(c, side) {
1354
- const { rest, base, dragged } = c;
1355
- const cols = rest.edges.filter((e) => e.side === side && e.column === true).map((edge) => ({ edge, l: base.edges[edge.id] })).filter((x) => x.l && !x.l.folded && !x.l.rides && x.l.cross > 0).sort((a, b) => a.edge.ring - b.edge.ring).map((x) => ({ edge: x.edge, r: x.l.rect }));
1356
- const own = Object.values(base.edges).filter((e) => e.side === side && e.rect.height > 0);
1357
- const span = own[0]?.rect ?? base.content;
1358
- const rings = sideRings(rest.edges, side);
1359
- const ringOf = (e) => rings.get(e.id) ?? e.ring;
1360
- const lines = [];
1361
- const outerX = (r) => side === "left" ? r.x : r.x + r.width;
1362
- const innerX = (r) => side === "left" ? r.x + r.width : r.x;
1363
- const atOf = (e) => c.lay.get(e.id)?.at ?? 0;
1364
- cols.forEach((x) => lines.push({ x: outerX(x.r), ring: ringOf(x.edge), at: atOf(x.edge) - 0.5 }));
1365
- if (cols.length > 0) lines.push({ x: innerX(cols[cols.length - 1].r), ring: ringOf(cols[cols.length - 1].edge) + 1, at: atOf(cols[cols.length - 1].edge) + 0.5 });
1366
- else lines.push({ x: side === "left" ? base.content.x : base.content.x + base.content.width, ring: rings.size, at: Math.max(-1, ...rest.edges.filter((e) => e.side === side).map(atOf)) + 0.5 });
1367
- const lands = (r) => {
1368
- const me = r.layout.items[dragged];
1369
- const el = me.edge ? r.layout.edges[me.edge] : void 0;
1370
- return !!el && !me.inMenu && !el.folded && !el.rides && r.scene !== c.scene;
1371
- };
1372
- const { reach, place } = EDGE_KINDS.columnLine;
1373
- return lines.map((l) => ({
1374
- id: `column:${side}`,
1375
- key: `column:${side}:${l.ring}`,
1376
- kind: "columnLine",
1377
- lay: { phase: EDGE_KINDS.columnLine.phase, side, at: l.at },
1378
- vertical: false,
1379
- zones: [{ zone: "start", a: l.x - reach, b: l.x + reach }],
1380
- slots: [slotAt(c, "start", l.x, `column:${side}:${l.ring}`, { kind: "column", side, ring: l.ring }, lands)],
1381
- refuse: () => "no-room",
1382
- own: null,
1383
- holds: false,
1384
- reserved: [],
1385
- across: () => [span.y, span.y + span.height],
1386
- owned: [span.y, span.y + span.height],
1387
- ownedAlong: [l.x - place / 2, l.x + place / 2],
1388
- drawn: false,
1389
- marker: (s) => rect(s.line, span.y, 0, span.height)
1390
- }));
1391
- }
1392
- function railStops(c, side) {
1393
- const { rest, base, now, dragged } = c;
1394
- const rail = base.rails[side];
1395
- const placeRect = base.railPlaces[side];
1396
- if (!placeRect) return null;
1397
- const rings = sideRings(rest.edges, side);
1398
- const mine = rest.edges.filter((e) => e.side === side).sort((a, b) => (rings.get(a.id) ?? 0) - (rings.get(b.id) ?? 0));
1399
- const ringOf = (id) => rings.get(id) ?? 0;
1400
- const stops2 = [];
1401
- let zone;
1402
- if (rail && rail.segments.length > 0) {
1403
- zone = rail.rect;
1404
- const groups = [];
1405
- for (const s of rail.segments) {
1406
- const g = groups[groups.length - 1];
1407
- if (g && g.edge === s.edge) g.y1 = s.rect.y + s.rect.height;
1408
- else groups.push({ edge: s.edge, y0: s.rect.y, y1: s.rect.y + s.rect.height });
1409
- }
1410
- for (const g of groups) stops2.push({ y: g.y0, ring: ringOf(g.edge) });
1411
- const last = groups[groups.length - 1];
1412
- stops2.push({ y: last.y1, ring: ringOf(last.edge) + 1 });
1413
- } else {
1414
- const firstCol = mine.find((e) => e.column === true);
1415
- zone = rect(side === "left" ? placeRect.x : placeRect.x + placeRect.width - RAIL_EDGE, placeRect.y, RAIL_EDGE, placeRect.height);
1416
- stops2.push({ y: placeRect.y, ring: firstCol ? ringOf(firstCol.id) : mine.length });
1417
- }
1418
- const ownRail = now.items[dragged]?.rail === side;
1419
- const lands = (r) => r.layout.items[dragged].rail === side && r.scene !== c.scene;
1420
- const at = zone;
1421
- return {
1422
- id: `rail:${side}`,
1423
- key: `rail:${side}`,
1424
- kind: "railStop",
1425
- // The rail lies outside every edge on its side: laid first among them.
1426
- lay: { phase: EDGE_KINDS.railStop.phase, side, at: -1 },
1427
- vertical: true,
1428
- zones: [{ zone: "start", a: at.y, b: at.y + at.height }],
1429
- slots: stops2.map((s) => slotAt(c, "start", s.y, `rail:${side}:${s.ring}`, { kind: "rail", side, ring: s.ring }, lands, ownRail)),
1430
- refuse: () => "no-room",
1431
- own: null,
1432
- holds: false,
1433
- reserved: [],
1434
- across: () => [at.x, at.x + at.width],
1435
- // A drawn rail owns its rect; where none shows, a thin band of its place at the side's outer boundary.
1436
- owned: rail && rail.segments.length > 0 ? [at.x, at.x + at.width] : side === "left" ? [at.x, at.x + Math.min(at.width, EDGE_KINDS.railStop.place)] : [at.x + at.width - Math.min(at.width, EDGE_KINDS.railStop.place), at.x + at.width],
1437
- ownedAlong: null,
1438
- drawn: !!rail && rail.segments.length > 0,
1439
- marker: (s) => rect(at.x, s.result.layout.items[dragged].rect.y, at.width, 0)
1440
- };
1441
- }
1442
- function generate(scene, dragged, options) {
1443
- const item = Object.hasOwn(scene.items, dragged) ? scene.items[dragged] : void 0;
1444
- if (!item || item.fixed || item.closed) return { targets: [], refusals: [], lost: [] };
1445
- const prev = { ...options.previous ? { previous: options.previous } : {}, ...options.fresh ? { fresh: options.fresh } : {} };
1446
- const settle = (s) => ({ scene: s, layout: solve(s, prev) });
1447
- const now = solve(scene, prev);
1448
- const rest = lift(scene, dragged);
1449
- const base = solve(rest, prev);
1450
- const chrome = scene.chrome ?? [];
1451
- const here = placeKey(scene, dragged);
1452
- const noteKey = (n) => "edge" in n && (n.code === "overflow" || n.code === "too-wide") ? `${n.edge}:${n.code}:${"id" in n ? n.id : ""}` : null;
1453
- const hadNotes = new Set(base.notes.map(noteKey).filter((k) => k !== null));
1454
- const sound = (after) => {
1455
- if (after.notes.some((n) => {
1456
- const k = noteKey(n);
1457
- return k !== null && !hadNotes.has(k);
1458
- })) return false;
1459
- for (const [id, b] of Object.entries(base.items)) {
1460
- if (!b.fixed) continue;
1461
- const a = after.items[id];
1462
- const e = a?.edge ? after.edges[a.edge] : void 0;
1463
- if (!a || !e || a.form !== b.form || a.formKind !== b.formKind) return false;
1464
- const f = formsOf(scene.items[id], axisOf(e.side))[a.form];
1465
- const len = axisOf(e.side) === "column" ? a.rect.height : a.rect.width;
1466
- if (f && len < formMain(f, "min", e.cross) - 0.5) return false;
1467
- }
1468
- const me = after.items[dragged];
1469
- if (!me) return true;
1470
- const fixedAfter = Object.entries(after.items).filter(([id, it]) => it.fixed && id !== dragged).map(([id]) => reservedRect(scene, after, id));
1471
- return !chrome.some((c2) => overlaps(me.rect, c2)) && !fixedAfter.some((f) => overlaps(me.rect, f));
1472
- };
1473
- const c = { scene, rest, now, base, dragged, kind: item.kind, reach: Math.max(0, Number.isFinite(options.endReach) ? options.endReach : 0), settle, isHome: (r) => placeKey(r.scene, dragged) === here, sound, lay: layKeys(scene) };
1474
- const specs = rest.edges.map((edge) => edgeSpec(c, edge));
1475
- for (const side of ["left", "right"]) {
1476
- const template = scene.columns?.[side];
1477
- if (!template || !zoneTakes(template, "start", item.kind)) continue;
1478
- specs.push(...columnLines(c, side));
1479
- if (scene.rails?.[side]?.accepts.includes(item.kind)) specs.push(railStops(c, side));
1480
- }
1481
- const ownEdge = now.items[dragged]?.edge;
1482
- const live = specs.filter((s) => s !== null);
1483
- const bandOf = (s) => {
1484
- const a = Math.min(...s.zones.map((z) => z.a));
1485
- const b = Math.max(...s.zones.map((z) => z.b));
1486
- return s.vertical ? rect(s.owned[0], a, s.owned[1] - s.owned[0], b - a) : rect(a, s.owned[0], b - a, s.owned[1] - s.owned[0]);
1487
- };
1488
- const ownedRectOf = (s) => {
1489
- const b = bandOf(s);
1490
- if (!s.ownedAlong) return b;
1491
- const [a0, a1] = s.ownedAlong;
1492
- return s.vertical ? rect(b.x, a0, b.width, a1 - a0) : rect(a0, b.y, a1 - a0, b.height);
1493
- };
1494
- const ownRect = now.items[dragged];
1495
- const homeRect = (() => {
1496
- if (!ownRect || ownRect.floating || !ownEdge) return null;
1497
- const o = ownRect.rect;
1498
- const s = live.find((x) => x.id === ownEdge);
1499
- if (!s || ownRect.inMenu) return o;
1500
- const [c0, c1] = s.across();
1501
- return s.vertical ? rect(Math.min(c0, o.x), o.y, Math.max(c1, o.x + o.width) - Math.min(c0, o.x), o.height) : rect(o.x, Math.min(c0, o.y), o.width, Math.max(c1, o.y + o.height) - Math.min(c0, o.y));
1502
- })();
1503
- const owners = [
1504
- // Home first: its tile is its own, for every edge but its own (which tiles it as its own interval).
1505
- ...homeRect && ownEdge ? [{ id: ownEdge, key: ownEdge, rect: homeRect, lay: { phase: -1, side: "", at: -Infinity }, place: false }] : [],
1506
- // Every edge the tiler reads: what it owns, drawn or a band of its place (a column line's band about its line).
1507
- ...live.map((s) => ({ id: s.id, key: s.key, rect: ownedRectOf(s), lay: s.lay, place: !s.drawn })).filter((o) => area(o.rect) > EPS),
1508
- // Every drawn rect, with the item lifted and as it is now (the item's own edge only lifted): the edges the tiler
1509
- // doesn't read (folded, riding, taking nothing) and the ones that move when the item lifts.
1510
- ...[...Object.entries(base.edges), ...Object.entries(now.edges).filter(([id]) => id !== ownEdge)].filter(([id, e]) => area(e.rect) > EPS && c.lay.has(id)).map(([id, e]) => ({ id, key: id, rect: e.rect, lay: c.lay.get(id), place: false }))
1511
- ];
1512
- const wins = (o, s) => {
1513
- if (o.key === s.key) return false;
1514
- if (pushes(o.lay, s.lay)) return true;
1515
- if (pushes(s.lay, o.lay)) return false;
1516
- const place = !s.drawn;
1517
- return o.place === place ? !place || o.lay.at < s.lay.at : !o.place;
1518
- };
1519
- const taken = new Map(
1520
- live.map((s) => [
1521
- s.key,
1522
- [
1523
- ...owners.filter((o) => wins(o, s)).map((o) => o.rect),
1524
- ...EDGE_KINDS[s.kind].virtual ? owners.filter((o) => o.key !== s.key && !wins(o, s)).flatMap((o) => subtract(o.rect, ownedRectOf(s))) : []
1525
- ]
1526
- ])
1527
- );
1528
- const bodySpans = (id) => {
1529
- const e = rest.edges.find((x) => x.id === id);
1530
- const bandOpt = e?.hitBand ?? DEFAULT_HIT_BAND;
1531
- if (!e || bandOpt === "middle") return [];
1532
- const column = axisOf(e.side) === "column";
1533
- const half = Math.max(0, Number.isFinite(bandOpt) ? bandOpt : DEFAULT_HIT_BAND) / 2;
1534
- return Object.entries(base.items).filter(([iid, p]) => p.edge === id && p.formKind === "open" && iid !== dragged).map(([, p]) => column ? [p.rect.y + half, p.rect.y + p.rect.height - half] : [p.rect.x + half, p.rect.x + p.rect.width - half]).filter(([a, b]) => b - a > EPS);
1535
- };
1536
- const cutsOf = (s) => {
1537
- const b = bandOf(s);
1538
- const along = (r) => s.vertical ? [r.y, r.y + r.height] : [r.x, r.x + r.width];
1539
- const across = (r) => s.vertical ? [r.x, r.x + r.width] : [r.y, r.y + r.height];
1540
- const rs = taken.get(s.key).filter((r) => overlaps(r, b));
1541
- const ends = [.../* @__PURE__ */ new Set([...along(b), ...rs.flatMap(along)])].filter((x) => x >= along(b)[0] && x <= along(b)[1]).sort((p, q) => p - q);
1542
- const owned = [];
1543
- for (let k = 1; k < ends.length; k++) {
1544
- const [u, w] = [ends[k - 1], ends[k]];
1545
- const over = rs.filter((r) => along(r)[0] <= u + EPS && along(r)[1] >= w - EPS).map(across);
1546
- if (w - u > EPS && less([across(b)], over).length === 0) owned.push([u, w]);
1547
- }
1548
- return [...owned, ...bodySpans(s.id)];
1549
- };
1550
- const lost = [];
1551
- const tiles = live.flatMap((spec) => tileEdge(c, spec, cutsOf(spec), lost));
1552
- const homeEdge = scene.edges.find((e) => e.id === ownRect?.edge);
1553
- if (ownRect && homeEdge && !ownRect.floating && !tiles.some((t) => t.order === 0))
1554
- tiles.push({ edge: homeEdge.id, key: homeEdge.id, edgeKind: edgeKindOf(scene, homeEdge), vertical: axisOf(homeEdge.side) === "column", zone: zoneOf(item), order: 0, kind: "home", id: `home:${dragged}`, priority: 1, place: { kind: "home" }, hit: ownRect.rect, keep: null });
1555
- const grows = (l, id) => {
1556
- const it = l.items[id];
1557
- const e = it.edge ? l.edges[it.edge] : void 0;
1558
- return !!e && !!formsOf(scene.items[id], axisOf(e.side))[it.form]?.main.grows;
1559
- };
1560
- const solid = [.../* @__PURE__ */ new Set([now, base])].flatMap((l) => Object.entries(l.items).filter(([id, it]) => it.fixed && !grows(l, id)).map(([, it]) => it.rect));
1561
- const bodies = (id) => {
1562
- const e = rest.edges.find((x) => x.id === id);
1563
- if (!e) return [];
1564
- const column = axisOf(e.side) === "column";
1565
- const across = (a, b) => column ? rect(-1e9, a, 2e9, b - a) : rect(a, -1e9, b - a, 2e9);
1566
- const reserved = Object.entries(base.items).filter(([iid, p]) => p.edge === id && p.fixed && !p.inMenu && !grows(base, iid)).map(([, p]) => column ? across(p.rect.y, p.rect.y + p.rect.height) : across(p.rect.x, p.rect.x + p.rect.width));
1567
- return [...reserved, ...bodySpans(id).map(([a, b]) => across(a, b))].filter((b) => area(b) > EPS);
1568
- };
1569
- const placed = [];
1570
- const later = [];
1571
- const clip = (r, cuts) => cuts.reduce((ps, cut) => ps.flatMap((p) => subtract(p, cut)), [r]);
1572
- const landed = /* @__PURE__ */ new Map();
1573
- const gone = /* @__PURE__ */ new Set();
1574
- for (const t of [...tiles].sort((a, b) => a.order - b.order)) {
1575
- if (t.order === 0) {
1576
- placed.push(t);
1577
- continue;
1578
- }
1579
- const data = EDGE_KINDS[t.edgeKind];
1580
- if (data.virtual && t.kind === "refuse") {
1581
- later.push(t);
1582
- continue;
1583
- }
1584
- if (t.kind === "reach") {
1585
- const of = t.of;
1586
- const pieces2 = clip(t.hit, [...placed.map((p) => p.hit), ...owners.filter((o) => o.key !== t.key).map((o) => o.rect), ...bodies(t.edge), ...solid, ...chrome]);
1587
- const at = landed.get(of) ?? [];
1588
- if (at.length === 0 && gone.has(of)) {
1589
- const line = t.vertical ? of.keep.y : of.keep.x;
1590
- const on = pieces2.filter((p) => t.vertical ? p.y <= line + EPS && line <= p.y + p.height + EPS : p.x <= line + EPS && line <= p.x + p.width + EPS);
1591
- const kept2 = (on.length > 0 ? on : pieces2).reduce((a, p) => !a || area(p) > area(a) ? p : a, null);
1592
- if (kept2) {
1593
- placed.push({ ...of, hit: kept2 });
1594
- gone.delete(of);
1595
- }
1596
- continue;
1597
- }
1598
- for (const i of at) {
1599
- const h = placed[i].hit;
1600
- const joint = pieces2.find(
1601
- (p) => t.vertical ? p.y <= h.y + EPS && p.y + p.height >= h.y + h.height - EPS && (Math.abs(p.x - h.x - h.width) < EPS || Math.abs(p.x + p.width - h.x) < EPS) : p.x <= h.x + EPS && p.x + p.width >= h.x + h.width - EPS && (Math.abs(p.y - h.y - h.height) < EPS || Math.abs(p.y + p.height - h.y) < EPS)
1602
- );
1603
- if (!joint) continue;
1604
- const merged = t.vertical ? rect(Math.min(h.x, joint.x), h.y, Math.max(h.x + h.width, joint.x + joint.width) - Math.min(h.x, joint.x), h.height) : rect(h.x, Math.min(h.y, joint.y), h.width, Math.max(h.y + h.height, joint.y + joint.height) - Math.min(h.y, joint.y));
1605
- placed[i] = { ...placed[i], hit: merged };
1606
- break;
1607
- }
1608
- continue;
1609
- }
1610
- const cuts = [...placed.map((p) => p.hit), ...taken.get(t.key) ?? [], ...data.virtual ? [] : bodies(t.edge), ...solid, ...chrome];
1611
- const pieces = clip(t.hit, cuts);
1612
- if (pieces.length === 0) {
1613
- if (t.kind !== "refuse") gone.add(t);
1614
- continue;
1615
- }
1616
- if (t.kind === "refuse") {
1617
- landed.set(t, pieces.map((_, k) => placed.length + k));
1618
- for (const p of pieces) placed.push({ ...t, hit: p });
1619
- continue;
1620
- }
1621
- const held = t.keep ? pieces.filter((p) => contains(p, t.keep)) : [];
1622
- const kept = (held.length > 0 ? held : pieces).reduce((a, p) => area(p) > area(a) ? p : a);
1623
- const sliver = (t.vertical ? kept.height : kept.width) < 1;
1624
- if (!sliver) {
1625
- landed.set(t, [placed.length]);
1626
- placed.push({ ...t, hit: kept });
1627
- }
1628
- for (const p of pieces) if (sliver || p !== kept) (data.virtual ? later : placed).push({ ...t, kind: "refuse", reason: "no-room", hit: p, keep: null });
1629
- }
1630
- for (const t of gone) lost.push(t.id);
1631
- for (const t of later) for (const p of clip(t.hit, [...placed.map((q) => q.hit), ...solid, ...chrome])) placed.push({ ...t, hit: p });
1632
- const targets2 = [];
1633
- const refused = [];
1634
- const homeAfter = () => ({ scene, layout: now });
1635
- const keyOf = (t) => `refuse:${t.edge}:${t.zone}`;
1636
- const pos = (t) => t.vertical ? [t.hit.y, t.hit.x] : [t.hit.x, t.hit.y];
1637
- const rank = /* @__PURE__ */ new Map();
1638
- const byKey = /* @__PURE__ */ new Map();
1639
- for (const t of placed) if (t.kind === "refuse") byKey.set(keyOf(t), [...byKey.get(keyOf(t)) ?? [], t]);
1640
- for (const group of byKey.values()) group.sort((a, b) => pos(a)[0] - pos(b)[0] || pos(a)[1] - pos(b)[1]).forEach((t, n) => rank.set(t, n));
1641
- let ownHome = null;
1642
- for (const t of placed) {
1643
- if (t.kind === "refuse") {
1644
- const n = rank.get(t);
1645
- refused.push({ id: n === 0 ? keyOf(t) : `${keyOf(t)}:${n}`, edge: t.edge, zone: t.zone, hit: t.hit, reason: t.reason });
1646
- continue;
1647
- }
1648
- if (t.kind === "home") {
1649
- if (t.order === 0) ownHome = t.hit;
1650
- if (t.id === `home:${dragged}`) continue;
1651
- targets2.push({ id: t.id, priority: t.priority, hit: t.hit, item: dragged, place: { kind: "home" }, after: homeAfter });
1652
- continue;
1653
- }
1654
- const result = t.result;
1655
- targets2.push({ id: t.id, priority: t.priority, hit: t.hit, ...t.slot ? { slot: t.slot } : {}, ...t.marker ? { marker: t.marker } : {}, item: dragged, place: t.place, after: () => result });
1656
- }
1657
- if (scene.floatKinds.includes(item.kind)) {
1658
- const was = scene.floats.find((f) => f.id === dragged);
1659
- const fo = options.floatOrient ?? was?.orient ?? floatOrientOf(item);
1660
- const size = options.floatSize ?? (was ? { width: was.rect.width, height: was.rect.height } : floatSizeOf(item, fo));
1661
- targets2.push({
1662
- id: `float:${dragged}`,
1663
- priority: 3,
1664
- free: true,
1665
- hit: PLANE,
1666
- item: dragged,
1667
- place: { kind: "float" },
1668
- after(point, grab) {
1669
- const g = grab ?? { x: size.width / 2, y: size.height / 2 };
1670
- const raw = rect(point.x - g.x, point.y - g.y, size.width, size.height);
1671
- const first = solve(use(floatAt(scene, dragged, raw, options.floatOrient), dragged), prev);
1672
- const at = first.items[dragged]?.rect ?? raw;
1673
- return settle(use(floatAt(scene, dragged, rect(at.x, at.y, size.width, size.height), options.floatOrient), dragged));
1674
- }
1675
- });
1676
- }
1677
- (scene.blocked ?? []).forEach((b, i) => {
1678
- targets2.push({ id: `none:${i}`, priority: 1, hit: rect(b.x, b.y, b.width, b.height), item: dragged, place: { kind: "none" }, after: homeAfter });
1679
- });
1680
- const own = now.items[dragged];
1681
- if (own && !own.floating) targets2.push({ id: `home:${dragged}`, priority: 1, hit: ownHome && !targets2.some((t) => t.place.kind === "home") ? ownHome : own.rect, item: dragged, place: { kind: "home" }, after: homeAfter });
1682
- return { targets: targets2, refusals: refused, lost };
1683
- }
1684
- function targets(scene, dragged, options = {}) {
1685
- return generate(scene, dragged, options).targets;
1686
- }
1687
- function refusals(scene, dragged, options = {}) {
1688
- return generate(scene, dragged, options).refusals;
1689
- }
1690
- var SIDE_RANK = { left: 0, top: 1, bottom: 2, right: 3 };
1691
- function stops(scene, dragged, options = {}) {
1692
- const list = targets(scene, dragged, options).filter((t) => t.place.kind !== "home" && t.place.kind !== "none");
1693
- const sideOf = (t) => {
1694
- const p = t.place;
1695
- if (p.kind === "float") return "float";
1696
- if (p.kind === "column" || p.kind === "rail") return p.side;
1697
- if (p.kind === "gap") return scene.edges.find((e) => e.id === p.edge)?.side ?? "float";
1698
- return "float";
1699
- };
1700
- const key = (t) => {
1701
- const side = sideOf(t);
1702
- const r = t.slot ?? t.hit;
1703
- const cx = r.x + r.width / 2;
1704
- const cy = r.y + r.height / 2;
1705
- const rank = side === "float" ? 4 : SIDE_RANK[side];
1706
- return side === "left" ? [rank, cx, cy] : side === "right" ? [rank, -cx, cy] : [rank, cy * (side === "bottom" ? -1 : 1), cx];
1707
- };
1708
- return list.map((t, i) => ({ t, i, k: key(t) })).sort((a, b) => a.k[0] - b.k[0] || a.k[1] - b.k[1] || a.k[2] - b.k[2] || a.i - b.i).map((x) => x.t);
1709
- }
1710
-
1711
- export {
1712
- SIDES,
1713
- ZONES,
1714
- zoneTakes,
1715
- zonesTaking,
1716
- DEFAULT_STRIP,
1717
- DEFAULT_HIT_BAND,
1718
- DEFAULT_FLOAT_SIZE,
1719
- isOutsideSideBar,
1720
- isSideBar,
1721
- floatOrientOf,
1722
- floatSizeOf,
1723
- solve,
1724
- lift,
1725
- moveTo,
1726
- floatAt,
1727
- fold,
1728
- use,
1729
- keep,
1730
- open,
1731
- close,
1732
- raise,
1733
- orient,
1734
- resize,
1735
- setSide,
1736
- homeFor,
1737
- dockHome,
1738
- storeScene,
1739
- readScene,
1740
- reservedRect,
1741
- targets,
1742
- refusals,
1743
- stops
1744
- };
1745
- //# sourceMappingURL=chunk-SZKU7CRX.js.map