@n-uf/placement 26.11.17 → 26.11.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1353 @@
1
+ import {
2
+ fitFloat,
3
+ fitStack,
4
+ formCrossMin,
5
+ formMain,
6
+ formSetsCross,
7
+ wrapLines
8
+ } from "./chunk-RUITEOZ7.js";
9
+ import {
10
+ clampInto,
11
+ rect
12
+ } from "./chunk-QMTNB2C3.js";
13
+
14
+ // src/scene/model.ts
15
+ var SIDES = ["top", "right", "bottom", "left"];
16
+ var ZONES = ["start", "center", "end"];
17
+ var zoneTakes = (edge, zone, kind) => edge.accepts.includes(kind) && (edge.zoneAccepts === void 0 || !!edge.zoneAccepts[zone]?.includes(kind));
18
+ var zonesTaking = (edge, kind) => ZONES.filter((z) => zoneTakes(edge, z, kind));
19
+ var DEFAULT_STRIP = 24;
20
+ var DEFAULT_HIT_BAND = 24;
21
+ var DEFAULT_FLOAT_SIZE = { width: 280, height: 360 };
22
+ var axisOf = (side) => side === "left" || side === "right" ? "column" : "row";
23
+ var zoneOf = (item) => item?.align === "center" || item?.align === "end" ? item.align : "start";
24
+ var formsOf = (item, axis) => item.forms[axis] ?? [];
25
+ function pastWrap(forms, from) {
26
+ for (let j = Math.max(0, Math.floor(from)); j < forms.length; j++) if (forms[j].kind !== "wrap" && !forms[j].wrap) return j;
27
+ return void 0;
28
+ }
29
+ function floatShape(item, f) {
30
+ const orient2 = f.orient === "row" ? "row" : "column";
31
+ const forms = formsOf(item, orient2);
32
+ const px2 = (v) => Number.isFinite(v) ? Math.max(0, v) : 0;
33
+ const w = px2(f.rect.width);
34
+ const h = px2(f.rect.height);
35
+ if (forms.length === 0) return { form: 0, width: w, height: h };
36
+ const asked = Math.min(Math.max(0, Math.floor(Number.isFinite(item.from) ? item.from : 0)), forms.length - 1);
37
+ const isWrap = (x) => x.kind === "wrap" || !!x.wrap;
38
+ const skipped = orient2 === "column" ? pastWrap(forms, asked) : asked;
39
+ if (skipped === void 0) return { form: asked, width: w, height: h, none: true };
40
+ const form = skipped;
41
+ const fm = forms[form];
42
+ if (orient2 === "column" && forms.slice(0, form + 1).some(isWrap)) return { form, width: px2(fm.cross.natural), height: px2(fm.main.natural) };
43
+ if (form > 0) return orient2 === "column" ? { form, width: w, height: px2(fm.main.natural) } : { form, width: px2(fm.main.natural), height: h };
44
+ return { form, width: w, height: h };
45
+ }
46
+
47
+ // src/scene/solve.ts
48
+ var EPS = 1e-6;
49
+ var finite = (x, fallback) => typeof x === "number" && Number.isFinite(x) ? x : fallback;
50
+ var px = (x) => Math.max(0, finite(x, 0));
51
+ function cutOrder(edges) {
52
+ const phase = (e) => e.side === "left" || e.side === "right" ? 1 : e.span === "between" ? 2 : 0;
53
+ const sideRank = (e) => e.side === "top" || e.side === "left" ? 0 : 1;
54
+ 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);
55
+ }
56
+ var ZONE_RANK = { start: 0, center: 1, end: 2 };
57
+ function zoneSorted(scene, ids) {
58
+ 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);
59
+ }
60
+ var stackItemOf = (item, forms, from) => ({
61
+ id: item.id,
62
+ forms,
63
+ used: finite(item.used, 0),
64
+ ...item.kept ? { kept: true } : {},
65
+ ...from !== void 0 ? { from } : item.from !== void 0 ? { from: item.from } : {},
66
+ ...item.fixed ? { fixed: true } : {}
67
+ });
68
+ function hasSetter(fit, forms) {
69
+ return fit.items.some((p) => p.kind !== "menu" && formSetsCross(forms(p.id)[p.form]));
70
+ }
71
+ function shapeOf(f, r, axis, cross) {
72
+ if (!f) return {};
73
+ if (f.wrap) {
74
+ const lead = Math.max(0, finite(f.wrap.lead ?? 0, 0));
75
+ const row = Math.max(0, finite(f.wrap.row, 0));
76
+ return { rows: wrapLines(f.wrap, cross).lines, grip: rect(r.x, r.y, lead, Math.min(row, r.height)) };
77
+ }
78
+ const g = typeof f.grip === "number" && Number.isFinite(f.grip) && f.grip > 0 ? f.grip : 0;
79
+ if (!g) return {};
80
+ 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) };
81
+ }
82
+ var TRAY_RANK = 100;
83
+ function solveEdge(scene, edge, ids, length, room, previous) {
84
+ const axis = axisOf(edge.side);
85
+ const column = axis === "column";
86
+ const ordered = zoneSorted(scene, ids);
87
+ const formsById = (id) => formsOf(scene.items[id], axis);
88
+ const maxOf = Number.isNaN(edge.cross.max) ? Infinity : edge.cross.max;
89
+ const cross = { min: Math.min(px(edge.cross.min), room), max: Math.min(maxOf, room), rail: Math.min(px(edge.cross.rail ?? 30), room) };
90
+ const base = {
91
+ axis,
92
+ length,
93
+ cross,
94
+ ...edge.menu ? { menu: edge.menu } : {},
95
+ ...edge.gap !== void 0 ? { gap: edge.gap } : {},
96
+ ...edge.pad ? { pad: edge.pad } : {},
97
+ ...column ? { ownCross: true } : {}
98
+ };
99
+ if (edge.compact) {
100
+ const iconsAt = (id) => {
101
+ const forms = formsById(id);
102
+ 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;
103
+ return forms.findIndex((f) => f.kind === "icons");
104
+ };
105
+ const shown = ordered.filter((id) => !scene.items[id].closed && iconsAt(id) >= 0);
106
+ const leftOutIcons = ordered.filter((id) => !scene.items[id].closed && iconsAt(id) < 0);
107
+ const compactInput = { ...base, ownCross: false, items: shown.map((id) => stackItemOf(scene.items[id], formsById(id), iconsAt(id))) };
108
+ const stack = fitStack(compactInput, previous ? { previous: previous.free } : {});
109
+ return { stack, free: stack, tabs: [], closedTabs: /* @__PURE__ */ new Set(), input: compactInput, leftOutIcons };
110
+ }
111
+ const open2 = ordered.filter((id) => !scene.items[id].closed);
112
+ const asStackItem = (id) => {
113
+ const it = scene.items[id];
114
+ const forms = formsById(id);
115
+ if (!column || !edge.zoneAccepts?.end || zoneOf(it) !== "end") return stackItemOf(it, forms);
116
+ const tray = (f) => f.kind === "wrap" || f.kind === "icons" || !!f.wrap;
117
+ let first = -1;
118
+ for (let j = Math.max(0, Math.floor(finite(it.from, 0))); j < forms.length && first < 0; j++) if (tray(forms[j])) first = j;
119
+ let last = -1;
120
+ forms.forEach((f, j) => {
121
+ if (tray(f)) last = j;
122
+ });
123
+ if (first < 0) return stackItemOf(it, forms);
124
+ return stackItemOf(it, forms.slice(0, last + 1).map((f) => ({ ...f, rank: finite(f.rank ?? 0, 0) + TRAY_RANK })), first);
125
+ };
126
+ const input = { ...base, items: open2.map(asStackItem) };
127
+ const free = fitStack(input, previous ? { previous: previous.free } : {});
128
+ const none = /* @__PURE__ */ new Set();
129
+ if (column && !hasSetter(free, formsById)) {
130
+ const railIndex = (item, forms) => {
131
+ if (item.fixed) return -1;
132
+ for (let j = Math.max(0, Math.floor(finite(item.from, 0))); j < forms.length; j++) if (forms[j].kind === "rail") return j;
133
+ return -1;
134
+ };
135
+ const wrapOnly = (id) => railIndex(scene.items[id], formsById(id)) < 0 && pastWrap(formsById(id), finite(scene.items[id].from, 0)) === void 0;
136
+ const shown = ordered.filter((id) => {
137
+ const it = scene.items[id];
138
+ if (!it.closed && wrapOnly(id)) return false;
139
+ return !it.closed || edge.railShowsClosed === true && railIndex(it, formsById(id)) >= 0;
140
+ });
141
+ const leftOut = ordered.filter((id) => !scene.items[id].closed && wrapOnly(id));
142
+ const anyRail = shown.some((id) => railIndex(scene.items[id], formsById(id)) >= 0);
143
+ if (anyRail) {
144
+ const railInput = {
145
+ ...base,
146
+ items: shown.map((id) => {
147
+ const it = scene.items[id];
148
+ const j = railIndex(it, formsById(id));
149
+ const forms = asStackItem(id).forms;
150
+ return stackItemOf(it, forms, j >= 0 ? j : pastWrap(forms, finite(it.from, 0)));
151
+ })
152
+ };
153
+ const stack = fitStack(railInput, previous ? { previous: previous.stack } : {});
154
+ const tabs = stack.items.filter((p) => p.kind === "rail").map((p) => p.id);
155
+ const closedTabs = new Set(shown.filter((id) => scene.items[id].closed));
156
+ return { stack, free, tabs, closedTabs, input: railInput, leftOut };
157
+ }
158
+ }
159
+ const size = edge.size;
160
+ if (typeof size === "number" && Number.isFinite(size) && hasSetter(free, formsById)) {
161
+ const setters = free.items.filter((p) => p.kind !== "menu" && formSetsCross(formsById(p.id)[p.form]));
162
+ const lo = Math.max(px(edge.cross.min), ...setters.map((p) => formCrossMin(formsById(p.id)[p.form])));
163
+ const s = Math.min(Math.max(size, lo), Math.max(lo, maxOf), room);
164
+ if (Math.abs(s - free.cross) > EPS) {
165
+ const sized = { ...input, cross: { ...cross, min: s, max: s } };
166
+ const stack = fitStack(sized, previous ? { previous: previous.stack } : {});
167
+ return { stack, free, tabs: [], closedTabs: none, input: sized };
168
+ }
169
+ }
170
+ return { stack: free, free, tabs: [], closedTabs: none, input };
171
+ }
172
+ function zoneStarts(scene, fit, input) {
173
+ const column = fit.axis === "column";
174
+ const at = (r) => column ? r.y : r.x;
175
+ const len = (r) => column ? r.height : r.width;
176
+ const out = /* @__PURE__ */ new Map();
177
+ const drawn = fit.items.filter((p) => p.kind !== "menu");
178
+ for (const p of drawn) out.set(p.id, at(p.rect));
179
+ const gap = px(input.gap);
180
+ const start = px(input.pad?.start);
181
+ const end = px(input.pad?.end);
182
+ const menu = fit.menu ? len(fit.menu.rect) + (drawn.length > 0 ? gap : 0) : 0;
183
+ const used = drawn.reduce((s2, p) => s2 + len(p.rect), 0) + Math.max(0, drawn.length - 1) * gap;
184
+ const spare = fit.length - start - end - menu - used;
185
+ if (spare <= EPS) return out;
186
+ const zone = (z) => drawn.filter((p) => zoneOf(scene.items[p.id]) === z);
187
+ const total = (ps) => ps.reduce((s2, p) => s2 + len(p.rect), 0) + Math.max(0, ps.length - 1) * gap;
188
+ const lay = (ps, from) => {
189
+ let c2 = from;
190
+ for (const p of ps) {
191
+ out.set(p.id, c2);
192
+ c2 += len(p.rect) + gap;
193
+ }
194
+ };
195
+ const s = zone("start");
196
+ const c = zone("center");
197
+ const e = zone("end");
198
+ lay(s, start);
199
+ const endStart = fit.length - end - menu - total(e);
200
+ lay(e, endStart);
201
+ const lo = start + total(s) + (s.length > 0 ? gap : 0);
202
+ const hi = endStart - (e.length > 0 ? gap : 0);
203
+ const mid = (fit.length - total(c)) / 2;
204
+ lay(c, Math.max(lo, Math.min(mid, hi - total(c))));
205
+ return out;
206
+ }
207
+ function anchored(content, w, h, anchor) {
208
+ const left = content.x;
209
+ const right = content.x + content.width - w;
210
+ const midX = content.x + (content.width - w) / 2;
211
+ const top = content.y;
212
+ const bottom = content.y + content.height - h;
213
+ const midY = content.y + (content.height - h) / 2;
214
+ const at = {
215
+ "content-top-left": [left, top],
216
+ "content-top": [midX, top],
217
+ "content-top-right": [right, top],
218
+ "content-right": [right, midY],
219
+ "content-bottom-right": [right, bottom],
220
+ "content-bottom": [midX, bottom],
221
+ "content-bottom-left": [left, bottom],
222
+ "content-left": [left, midY]
223
+ };
224
+ const [x, y] = at[anchor] ?? at["content-top-right"];
225
+ return rect(x, y, w, h);
226
+ }
227
+ function solve(given, options = {}) {
228
+ const previous = options.previous;
229
+ const opened = /* @__PURE__ */ new Set();
230
+ if (previous) {
231
+ for (const e of given.edges) {
232
+ for (const id of e.items) {
233
+ const it = Object.hasOwn(given.items, id) ? given.items[id] : void 0;
234
+ const was = previous.items[id];
235
+ if (it && !it.closed && !it.fixed && (!was || was.closed)) opened.add(id);
236
+ }
237
+ }
238
+ }
239
+ const newest = Object.values(given.items).reduce((m, it) => Math.max(m, finite(it.used, 0)), 0) + 1;
240
+ const scene = opened.size === 0 ? given : { ...given, items: { ...given.items, ...Object.fromEntries([...opened].map((id) => [id, { ...given.items[id], used: newest }])) } };
241
+ const fresh = new Set(options.fresh ?? []);
242
+ const prevEdge = (edge) => !previous || fresh.has(edge.id) || edge.items.some((id) => fresh.has(id)) ? void 0 : previous.edges[edge.id];
243
+ const notes = [];
244
+ const items = {};
245
+ const edges = {};
246
+ const stage = rect(finite(scene.stage.x, 0), finite(scene.stage.y, 0), px(scene.stage.width), px(scene.stage.height));
247
+ const order = cutOrder(scene.edges);
248
+ const claimed = /* @__PURE__ */ new Set();
249
+ const claim = (id) => {
250
+ if (!Object.hasOwn(scene.items, id)) {
251
+ notes.push({ code: "unknown-item", id });
252
+ return false;
253
+ }
254
+ if (claimed.has(id)) {
255
+ notes.push({ code: "twice", id });
256
+ return false;
257
+ }
258
+ claimed.add(id);
259
+ return true;
260
+ };
261
+ const owned = /* @__PURE__ */ new Map();
262
+ for (const e of order) owned.set(e, e.items.filter(claim));
263
+ const emit = (edge, r, solved, extra = {}) => {
264
+ const column = axisOf(edge.side) === "column";
265
+ const { stack } = solved;
266
+ const thick = column ? r.width : r.height;
267
+ const starts = zoneStarts(scene, stack, solved.input);
268
+ const toStage = (l, id) => {
269
+ const s = id !== void 0 ? starts.get(id) : void 0;
270
+ const x = column ? l.x : s ?? l.x;
271
+ const y = column ? s ?? l.y : l.y;
272
+ return rect(r.x + x, r.y + y, l.width, l.height);
273
+ };
274
+ const tabs = new Set(solved.tabs);
275
+ for (const id of solved.leftOut ?? []) notes.push({ code: "no-vertical-form", edge: edge.id, id });
276
+ for (const id of solved.leftOutIcons ?? []) notes.push({ code: "no-icons-form", edge: edge.id, id });
277
+ for (const p of stack.items) {
278
+ const it = scene.items[p.id];
279
+ const inMenu = p.kind === "menu";
280
+ const drawnRect = toStage(p.rect, inMenu ? void 0 : p.id);
281
+ const shape = inMenu ? {} : shapeOf(formsOf(it, column ? "column" : "row")[p.form], drawnRect, column ? "column" : "row", stack.cross);
282
+ items[p.id] = {
283
+ ...shape,
284
+ rect: drawnRect,
285
+ kind: it.kind,
286
+ form: p.form,
287
+ formKind: p.kind,
288
+ edge: edge.id,
289
+ zone: zoneOf(it),
290
+ ...inMenu ? { inMenu: true } : {},
291
+ ...tabs.has(p.id) ? { tab: true } : {},
292
+ ...solved.closedTabs.has(p.id) ? { closed: true } : {},
293
+ ...it.fixed ? { fixed: true } : {}
294
+ };
295
+ }
296
+ for (const n of stack.notes) notes.push({ edge: edge.id, ...n });
297
+ edges[edge.id] = {
298
+ side: edge.side,
299
+ ring: finite(edge.ring, 0),
300
+ rect: r,
301
+ cross: thick,
302
+ menu: stack.menu ? { rect: toStage(stack.menu.rect), items: stack.menu.items } : null,
303
+ tabs: solved.tabs,
304
+ stack,
305
+ free: solved.free,
306
+ ...extra
307
+ };
308
+ };
309
+ const lay = (edge, area3, cut) => {
310
+ const column = axisOf(edge.side) === "column";
311
+ const length = column ? area3.height : area3.width;
312
+ const room = Math.max(0, column ? area3.width : area3.height);
313
+ const solved = solveEdge(scene, edge, owned.get(edge), length, room, prevEdge(edge));
314
+ const thick = solved.stack.items.length === 0 ? 0 : Math.min(solved.stack.cross, room);
315
+ 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);
316
+ emit(edge, r, solved);
317
+ if (!cut) return area3;
318
+ 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);
319
+ };
320
+ const rails = {};
321
+ const gutter = px(scene.gutter);
322
+ const isSide = (e) => e.side === "left" || e.side === "right";
323
+ const laySides = (sideEdges, area3) => {
324
+ const H = area3.height;
325
+ const ride = scene.handFold === "ride";
326
+ const cols = sideEdges.map((edge) => {
327
+ const side = edge.side;
328
+ const solved = solveEdge(scene, edge, owned.get(edge), H, Math.max(0, area3.width), prevEdge(edge));
329
+ const width = solved.stack.items.length === 0 ? 0 : Math.min(solved.stack.cross, area3.width);
330
+ const visible = owned.get(edge).filter((id) => !scene.items[id].closed);
331
+ const dyn = edge.column === true && scene.rails?.[side] !== void 0 && visible.length > 0;
332
+ const forms = (id) => formsOf(scene.items[id], "column");
333
+ const setters = solved.free.items.filter((p) => p.kind !== "menu" && formSetsCross(forms(p.id)[p.form]));
334
+ const min = setters.length > 0 ? Math.min(width, Math.max(px(edge.cross.min), ...setters.map((p) => formCrossMin(forms(p.id)[p.form])))) : width;
335
+ return {
336
+ edge,
337
+ side,
338
+ solved,
339
+ width,
340
+ min,
341
+ visible,
342
+ dyn,
343
+ hand: dyn && visible.every((id) => finite(scene.items[id].from, 0) >= 1),
344
+ kept: visible.some((id) => scene.items[id].kept === true),
345
+ age: Math.max(-Infinity, ...visible.map((id) => finite(scene.items[id].used, 0)))
346
+ };
347
+ });
348
+ const cmin = scene.contentMin ? px(scene.contentMin.width) : void 0;
349
+ 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));
350
+ const shrunkBefore = (previous?.notes ?? []).flatMap((n) => n.code === "column-shrunk" ? [n.edge] : []);
351
+ const rankOf = (c) => shrunkBefore.includes(c.edge.id) ? shrunkBefore.indexOf(c.edge.id) : Infinity;
352
+ 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);
353
+ const railW = (side) => px(scene.rails?.[side]?.width);
354
+ const evaluate = (auto2) => {
355
+ const openOn = (side) => cols.some((c) => c.side === side && c.dyn && !c.hand && !auto2.has(c) && c.width > 0);
356
+ const riding = (c) => ride && c.hand && c.visible.length === 1 && openOn(c.side);
357
+ const railed = (c) => c.dyn && (auto2.has(c) || c.hand && !riding(c));
358
+ const widths = new Map(cols.filter((c) => !railed(c) && !riding(c)).map((c) => [c, c.width]));
359
+ const railSides = ["left", "right"].filter((s) => cols.some((c) => c.side === s && railed(c)));
360
+ let used = 0;
361
+ for (const w of widths.values()) used += w > 0 ? w + gutter : 0;
362
+ for (const s of railSides) used += railW(s) + gutter;
363
+ let excess = cmin === void 0 ? -Infinity : used + cmin - area3.width;
364
+ for (const c of shrinkOrder) {
365
+ if (excess <= EPS) break;
366
+ const w = widths.get(c);
367
+ if (w === void 0) continue;
368
+ const take = Math.min(excess, w - c.min);
369
+ if (take > 0) {
370
+ widths.set(c, w - take);
371
+ excess -= take;
372
+ }
373
+ }
374
+ return { auto: auto2, widths, excess, railed, riding, railSides };
375
+ };
376
+ let auto = /* @__PURE__ */ new Set();
377
+ if (cmin !== void 0) {
378
+ const unfold = () => {
379
+ for (const c of [...auto].reverse()) {
380
+ const fewer = new Set([...auto].filter((x) => x !== c));
381
+ if (evaluate(fewer).excess <= EPS) auto = fewer;
382
+ else break;
383
+ }
384
+ };
385
+ if (previous) {
386
+ const byId = new Map(order2.map((c) => [c.edge.id, c]));
387
+ const anew = (c) => fresh.has(c.edge.id) || c.visible.some((id) => opened.has(id) || fresh.has(id));
388
+ 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)] : []);
389
+ auto = new Set(was);
390
+ if (evaluate(auto).excess <= EPS) unfold();
391
+ }
392
+ for (const c of order2) {
393
+ if (evaluate(auto).excess <= EPS) break;
394
+ if (!auto.has(c)) auto = /* @__PURE__ */ new Set([...auto, c]);
395
+ }
396
+ for (let size = -1; size !== auto.size; ) {
397
+ size = auto.size;
398
+ unfold();
399
+ }
400
+ }
401
+ const g = evaluate(auto);
402
+ for (const c of shrinkOrder) {
403
+ const w = g.widths.get(c);
404
+ if (w !== void 0 && w < c.width - EPS) notes.push({ code: "column-shrunk", edge: c.edge.id, from: c.width, to: w });
405
+ }
406
+ for (const c of g.auto) notes.push({ code: c.kept ? "kept-folded" : "column-folded", edge: c.edge.id });
407
+ let x0 = area3.x;
408
+ let x1 = area3.x + area3.width;
409
+ for (const side of ["left", "right"]) {
410
+ const mine = cols.filter((c) => c.side === side);
411
+ const railed = mine.filter((c) => g.railed(c));
412
+ const riders = mine.filter((c) => g.riding(c));
413
+ const host = mine.find((c) => c.dyn && !c.hand && !g.auto.has(c) && c.width > 0);
414
+ const riderBar = (c) => {
415
+ const id = c.visible[0];
416
+ const forms = formsOf(scene.items[id], "column");
417
+ const form = forms.length === 0 ? 0 : Math.min(Math.max(0, Math.floor(finite(scene.items[id].from, 0))), forms.length - 1);
418
+ return { id, form, f: forms[form], h: px(forms[form]?.main.natural) };
419
+ };
420
+ const ridersTop = riders.reduce((s, c) => s + riderBar(c).h, 0);
421
+ let railRect = null;
422
+ for (const c of mine) {
423
+ if (!railRect && railed.length > 0 && c.edge.column === true) {
424
+ const w2 = railW(side);
425
+ if (side === "left") {
426
+ railRect = rect(x0, area3.y, w2, H);
427
+ x0 += w2 + gutter;
428
+ } else {
429
+ x1 -= w2;
430
+ railRect = rect(x1, area3.y, w2, H);
431
+ x1 -= gutter;
432
+ }
433
+ }
434
+ 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 };
435
+ if (g.railed(c)) {
436
+ edges[c.edge.id] = { ...base, rect: rect(railRect.x, area3.y, 0, H), folded: g.auto.has(c) ? "auto" : "hand" };
437
+ continue;
438
+ }
439
+ if (g.riding(c)) continue;
440
+ const w = Math.min(g.widths.get(c), Math.max(0, x1 - x0));
441
+ const top = c === host ? ridersTop : 0;
442
+ let solved = c.solved;
443
+ if (top > 0 || Math.abs(w - c.width) > EPS) {
444
+ const forced = { ...c.edge, cross: { ...c.edge.cross, min: w, max: w } };
445
+ const { size: _s, ...plain } = forced;
446
+ solved = solveEdge(scene, plain, owned.get(c.edge), H - top, w, prevEdge(c.edge));
447
+ }
448
+ let r;
449
+ if (side === "left") {
450
+ r = rect(x0, area3.y + top, w, H - top);
451
+ x0 += w > 0 ? w + gutter : 0;
452
+ } else {
453
+ x1 -= w;
454
+ r = rect(x1, area3.y + top, w, H - top);
455
+ x1 -= w > 0 ? gutter : 0;
456
+ }
457
+ emit(c.edge, r, solved, w < c.width - EPS ? { shrunk: true } : {});
458
+ }
459
+ if (host && riders.length > 0) {
460
+ const hr = edges[host.edge.id].rect;
461
+ let y = area3.y;
462
+ for (const c of riders) {
463
+ const b = riderBar(c);
464
+ const at = rect(hr.x, y, hr.width, b.h);
465
+ const it = scene.items[b.id];
466
+ 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" };
467
+ edges[c.edge.id] = { side: c.edge.side, ring: finite(c.edge.ring, 0), rect: at, cross: 0, menu: null, tabs: [], stack: c.solved.stack, free: c.solved.free, rides: host.edge.id, folded: "hand" };
468
+ y += b.h;
469
+ }
470
+ }
471
+ if (railRect) {
472
+ const segments = [];
473
+ let y = railRect.y;
474
+ for (const c of railed) {
475
+ for (const id of zoneSorted(scene, c.visible)) {
476
+ const it = scene.items[id];
477
+ const forms = formsOf(it, "column");
478
+ const ri = forms.findIndex((f) => f.kind === "rail");
479
+ const form = ri >= 0 ? ri : Math.min(Math.max(0, Math.floor(finite(it.from, 0))), Math.max(0, forms.length - 1));
480
+ const h = ri >= 0 ? px(forms[ri].main.natural) : railRect.width;
481
+ const at = rect(railRect.x, y, railRect.width, h);
482
+ 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" };
483
+ segments.push({ id, edge: c.edge.id, rect: at });
484
+ y += h;
485
+ }
486
+ }
487
+ rails[side] = { rect: railRect, segments };
488
+ }
489
+ }
490
+ return rect(x0, area3.y, Math.max(0, x1 - x0), H);
491
+ };
492
+ const inset = scene.inset ?? {};
493
+ let area2 = rect(stage.x + px(inset.left), stage.y + px(inset.top), Math.max(0, stage.width - px(inset.left) - px(inset.right)), Math.max(0, stage.height - px(inset.top) - px(inset.bottom)));
494
+ const reserved = order.filter((e) => e.mode !== "overlay");
495
+ for (const e of reserved) if (!isSide(e) && e.span !== "between") area2 = lay(e, area2, true);
496
+ area2 = laySides(reserved.filter(isSide), area2);
497
+ for (const e of reserved) if (!isSide(e) && e.span === "between") area2 = lay(e, area2, true);
498
+ const content = area2;
499
+ let over = content;
500
+ for (const e of order) if (e.mode === "overlay") over = lay(e, over, true);
501
+ if (scene.contentMin) {
502
+ const minW = px(scene.contentMin.width);
503
+ const minH = px(scene.contentMin.height);
504
+ if (content.width < minW - EPS) notes.push({ code: "content-narrow", width: content.width, min: minW });
505
+ const bars = order.filter((e) => e.mode === "overlay" && !isSide(e)).reduce((s, e) => s + (edges[e.id]?.cross ?? 0), 0);
506
+ if (content.height - bars < minH - EPS) notes.push({ code: "content-short", height: content.height - bars, min: minH });
507
+ }
508
+ if (previous) {
509
+ const was = previous.content;
510
+ 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;
511
+ if (moved) notes.push({ code: "content-changed", from: was, to: content });
512
+ }
513
+ const avoid = [];
514
+ for (const l of Object.values(items)) avoid.push(l.rect);
515
+ for (const e of Object.values(edges)) {
516
+ if (axisOf(e.side) === "row" && e.cross > 0) avoid.push(e.rect);
517
+ if (e.menu) avoid.push(e.menu.rect);
518
+ }
519
+ for (const c of scene.chrome ?? []) avoid.push(c);
520
+ const floats = [];
521
+ for (const f of scene.floats) {
522
+ if (!claim(f.id)) continue;
523
+ const it = scene.items[f.id];
524
+ if (it.closed) continue;
525
+ const shape = floatShape(it, f);
526
+ if (shape.none) {
527
+ notes.push({ code: "no-vertical-form", id: f.id });
528
+ continue;
529
+ }
530
+ const from = shape.form;
531
+ const form = formsOf(it, f.orient === "row" ? "row" : "column")[from];
532
+ const w = shape.width;
533
+ const h = shape.height;
534
+ 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);
535
+ const fit = fitFloat(want, stage, avoid);
536
+ if (fit.clamped) notes.push({ code: "float-clamped", id: f.id });
537
+ if (fit.snapped) notes.push({ code: "float-moved", id: f.id });
538
+ if (fit.covers.length > 0) notes.push({ code: "float-covers", id: f.id });
539
+ items[f.id] = { ...shapeOf(form, fit.rect, f.orient === "row" ? "row" : "column", fit.rect.width), rect: fit.rect, kind: it.kind, form: from, formKind: form?.kind ?? "open", floating: true };
540
+ floats.push(f.id);
541
+ }
542
+ return { items, edges, content, floats, rails, notes };
543
+ }
544
+
545
+ // src/scene/changes.ts
546
+ var withItem = (scene, item) => ({ ...scene, items: { ...scene.items, [item.id]: item } });
547
+ var itemOf = (scene, id) => Object.hasOwn(scene.items, id) ? scene.items[id] : void 0;
548
+ function closeRings(edges, sides) {
549
+ const out = [...edges];
550
+ for (const side of sides) {
551
+ const idx = out.map((e, i) => ({ e, i })).filter((x) => x.e.side === side);
552
+ idx.sort((a, b) => a.e.ring === b.e.ring ? a.i - b.i : a.e.ring < b.e.ring ? -1 : 1);
553
+ idx.forEach((x, ring) => {
554
+ if (x.e.ring !== ring) out[x.i] = { ...x.e, ring };
555
+ });
556
+ }
557
+ return out;
558
+ }
559
+ function prune(scene) {
560
+ const gone = scene.edges.filter((e) => e.column === true && e.items.length === 0);
561
+ if (gone.length === 0) return scene;
562
+ const sides = new Set(gone.map((e) => e.side));
563
+ return { ...scene, edges: closeRings(scene.edges.filter((e) => !gone.includes(e)), sides) };
564
+ }
565
+ function lift(scene, id) {
566
+ const inEdge = scene.edges.some((e) => e.items.includes(id));
567
+ const inFloat = scene.floats.some((f) => f.id === id);
568
+ if (!inEdge && !inFloat) return scene;
569
+ return prune({
570
+ ...scene,
571
+ edges: inEdge ? scene.edges.map((e) => e.items.includes(id) ? { ...e, items: e.items.filter((x) => x !== id) } : e) : scene.edges,
572
+ floats: inFloat ? scene.floats.filter((f) => f.id !== id) : scene.floats
573
+ });
574
+ }
575
+ function newColumn(scene, side, kind, ring, items) {
576
+ const t = scene.columns?.[side];
577
+ if (!t || !t.accepts.includes(kind)) return null;
578
+ const taken = new Set(scene.edges.map((e) => e.id));
579
+ let n = 0;
580
+ while (taken.has(`${side}-${n}`)) n++;
581
+ return {
582
+ id: `${side}-${n}`,
583
+ side,
584
+ ring,
585
+ accepts: t.accepts,
586
+ items,
587
+ cross: t.cross,
588
+ ...t.gap !== void 0 ? { gap: t.gap } : {},
589
+ ...t.pad ? { pad: t.pad } : {},
590
+ ...t.menu ? { menu: t.menu } : {},
591
+ ...t.zoneAccepts ? { zoneAccepts: t.zoneAccepts } : {},
592
+ column: true
593
+ };
594
+ }
595
+ function insertColumn(scene, id, side, ring) {
596
+ const kind = scene.items[id].kind;
597
+ const mine = scene.edges.filter((e) => e.side === side);
598
+ const r = Math.max(0, Math.min(Number.isNaN(ring) ? Infinity : Math.floor(ring), mine.length === 0 ? 0 : Math.max(...mine.map((e) => e.ring)) + 1));
599
+ const col = newColumn(scene, side, kind, r, [id]);
600
+ if (!col) return null;
601
+ if (!zoneTakes(col, "start", kind)) return null;
602
+ const { align: _a, ...plain } = scene.items[id];
603
+ const placed = withItem(scene, plain);
604
+ const shifted = placed.edges.map((e) => e.side === side && e.ring >= r ? { ...e, ring: e.ring + 1 } : e);
605
+ return { ...placed, edges: closeRings([...shifted, col], /* @__PURE__ */ new Set([side])) };
606
+ }
607
+ function insertIndex(scene, edge, zone, at) {
608
+ const rank = { start: 0, center: 1, end: 2 };
609
+ const inZone = edge.items.map((id, i) => ({ id, i })).filter((x) => zoneOf(itemOf(scene, x.id)) === zone);
610
+ const k = Math.min(Math.max(0, Math.floor(Number.isFinite(at) ? at : inZone.length)), inZone.length);
611
+ if (k < inZone.length) return inZone[k].i;
612
+ if (inZone.length > 0) return inZone[inZone.length - 1].i + 1;
613
+ let idx = 0;
614
+ edge.items.forEach((id, i) => {
615
+ if (rank[zoneOf(itemOf(scene, id))] < rank[zone]) idx = i + 1;
616
+ });
617
+ return idx;
618
+ }
619
+ function moveTo(scene, id, place) {
620
+ const item = itemOf(scene, id);
621
+ if (!item || item.fixed) return scene;
622
+ if (place.kind === "float") {
623
+ const out = floatAt(scene, id, place.rect);
624
+ const anchor = place.anchor;
625
+ if (!anchor || out === scene) return out;
626
+ return { ...out, floats: out.floats.map((f) => f.id === id ? { ...f, anchor } : f) };
627
+ }
628
+ if (place.kind === "column" || place.kind === "rail") {
629
+ if (place.kind === "rail" && !scene.rails?.[place.side]?.accepts.includes(item.kind)) return scene;
630
+ const { align: _a, ...plain } = item;
631
+ const folded = place.kind === "rail" ? { ...plain, from: Math.max(1, plain.from ?? 0) } : plain;
632
+ const made = insertColumn(withItem(lift(scene, id), folded), id, place.side, place.ring);
633
+ return made ?? scene;
634
+ }
635
+ const edge = scene.edges.find((e) => e.id === place.edge);
636
+ if (!edge || !edge.accepts.includes(item.kind)) return scene;
637
+ const zone = place.zone ?? (zoneTakes(edge, zoneOf(item), item.kind) ? zoneOf(item) : zonesTaking(edge, item.kind)[0]);
638
+ if (!zone || !zoneTakes(edge, zone, item.kind)) return scene;
639
+ const { align: _drop, ...rest } = item;
640
+ const moved = zone === "start" ? rest : { ...rest, align: zone };
641
+ const lifted = withItem(lift(scene, id), moved);
642
+ const target = lifted.edges.find((e) => e.id === place.edge);
643
+ if (!target) return scene;
644
+ const k = insertIndex(lifted, target, zone, place.at);
645
+ return { ...lifted, edges: lifted.edges.map((e) => e === target ? { ...e, items: [...e.items.slice(0, k), id, ...e.items.slice(k)] } : e) };
646
+ }
647
+ function floatAt(scene, id, at, orient2) {
648
+ const item = itemOf(scene, id);
649
+ if (!item || item.fixed || !scene.floatKinds.includes(item.kind)) return scene;
650
+ const was = scene.floats.find((f) => f.id === id);
651
+ const o = orient2 ?? was?.orient;
652
+ const size = item.floatSize ?? DEFAULT_FLOAT_SIZE;
653
+ 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 } : {} };
654
+ const next = at && at.width > 0 && at.height > 0 ? { ...item, floatSize: { width: at.width, height: at.height } } : item;
655
+ const lifted = lift(withItem(scene, next), id);
656
+ return { ...lifted, floats: [...lifted.floats, float] };
657
+ }
658
+ function fold(scene, id, on) {
659
+ const item = itemOf(scene, id);
660
+ if (!item || item.fixed) return scene;
661
+ if (on) return (item.from ?? 0) >= 1 ? scene : withItem(scene, { ...item, from: 1 });
662
+ if (item.from === void 0) return scene;
663
+ const { from: _f, ...rest } = item;
664
+ return withItem(scene, rest);
665
+ }
666
+ var nextAge = (scene) => Object.values(scene.items).reduce((m, it) => Math.max(m, Number.isFinite(it.used) ? it.used : 0), 0) + 1;
667
+ function use(scene, id) {
668
+ const item = itemOf(scene, id);
669
+ if (!item) return scene;
670
+ return withItem(scene, { ...item, used: nextAge(scene) });
671
+ }
672
+ function keep(scene, id, on) {
673
+ const item = itemOf(scene, id);
674
+ if (!item || !!item.kept === on) return scene;
675
+ if (on) return withItem(scene, { ...item, kept: true });
676
+ const { kept: _k, ...rest } = item;
677
+ return withItem(scene, rest);
678
+ }
679
+ function open(scene, id) {
680
+ const item = itemOf(scene, id);
681
+ if (!item || !item.closed) return scene;
682
+ const { closed: _c, ...rest } = item;
683
+ return withItem(scene, rest);
684
+ }
685
+ function close(scene, id) {
686
+ const item = itemOf(scene, id);
687
+ if (!item || item.closed || item.fixed) return scene;
688
+ return withItem(scene, { ...item, closed: true });
689
+ }
690
+ function raise(scene, id) {
691
+ const i = scene.floats.findIndex((f) => f.id === id);
692
+ if (i < 0 || i === scene.floats.length - 1) return scene;
693
+ return { ...scene, floats: [...scene.floats.slice(0, i), ...scene.floats.slice(i + 1), scene.floats[i]] };
694
+ }
695
+ function orient(scene, id, to) {
696
+ const i = scene.floats.findIndex((f) => f.id === id);
697
+ if (i < 0 || (scene.floats[i].orient ?? "column") === to) return scene;
698
+ return { ...scene, floats: scene.floats.map((f, k) => k === i ? { ...f, orient: to } : f) };
699
+ }
700
+ function resize(scene, edgeId, size) {
701
+ const edge = scene.edges.find((e) => e.id === edgeId);
702
+ if (!edge) return scene;
703
+ if (!Number.isFinite(size)) {
704
+ if (edge.size === void 0) return scene;
705
+ const { size: _s, ...rest } = edge;
706
+ return { ...scene, edges: scene.edges.map((e) => e === edge ? rest : e) };
707
+ }
708
+ return { ...scene, edges: scene.edges.map((e) => e === edge ? { ...e, size } : e) };
709
+ }
710
+ function homeFor(scene, id, start) {
711
+ for (const e of start.edges) {
712
+ const k = e.items.indexOf(id);
713
+ if (k < 0) continue;
714
+ const zone = zoneOf(itemOf(start, id) ?? itemOf(scene, id));
715
+ const here = scene.edges.find((x) => x.id === e.id);
716
+ if (here) {
717
+ const now = here.items.filter((x) => x !== id && zoneOf(itemOf(scene, x)) === zone);
718
+ const next = e.items.slice(k + 1).find((x) => now.includes(x));
719
+ return { kind: "gap", edge: e.id, zone, at: next ? now.indexOf(next) : now.length };
720
+ }
721
+ if (e.column === true && (e.side === "left" || e.side === "right")) return { kind: "column", side: e.side, ring: e.ring };
722
+ return null;
723
+ }
724
+ const f = start.floats.find((x) => x.id === id);
725
+ if (f) return { kind: "float", rect: f.rect, ...f.anchor ? { anchor: f.anchor } : {} };
726
+ return null;
727
+ }
728
+ function dockHome(scene, id, start) {
729
+ const place = homeFor(scene, id, start);
730
+ if (!place) return scene;
731
+ const moved = moveTo(scene, id, place);
732
+ const item = itemOf(moved, id);
733
+ if (!item) return scene;
734
+ const from = itemOf(start, id)?.from;
735
+ if (item.from === from) return moved;
736
+ const { from: _f, ...plain } = item;
737
+ return withItem(moved, from === void 0 ? plain : { ...plain, from });
738
+ }
739
+ function clampFloat(r, stage, drawn = r) {
740
+ const at = clampInto({ x: r.x, y: r.y, width: drawn.width, height: drawn.height }, stage);
741
+ return { x: at.x, y: at.y, width: r.width, height: r.height };
742
+ }
743
+ function drawnFloatSize(item, f) {
744
+ const s = floatShape(item, f);
745
+ return { width: s.width, height: s.height };
746
+ }
747
+
748
+ // src/scene/store.ts
749
+ var ANCHORS = ["content-top-left", "content-top", "content-top-right", "content-right", "content-bottom-right", "content-bottom", "content-bottom-left", "content-left"];
750
+ var isNum = (x) => typeof x === "number" && Number.isFinite(x);
751
+ var isSize = (x) => isNum(x) && x > 0;
752
+ var isObject = (x) => typeof x === "object" && x !== null && !Array.isArray(x);
753
+ var record = () => /* @__PURE__ */ Object.create(null);
754
+ function storeScene(scene) {
755
+ const items = record();
756
+ for (const [id, it] of Object.entries(scene.items)) {
757
+ if (id === "__proto__") continue;
758
+ items[id] = {
759
+ used: isNum(it.used) ? it.used : 0,
760
+ ...it.kept ? { kept: true } : {},
761
+ ...isNum(it.from) && it.from >= 1 ? { from: Math.floor(it.from) } : {},
762
+ ...it.closed ? { closed: true } : {},
763
+ ...it.align === "center" || it.align === "end" ? { align: it.align } : {},
764
+ ...it.floatSize && isSize(it.floatSize.width) && isSize(it.floatSize.height) ? { floatSize: { width: it.floatSize.width, height: it.floatSize.height } } : {}
765
+ };
766
+ }
767
+ return {
768
+ v: 1,
769
+ 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 } : {} })),
770
+ 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 } : {} })),
771
+ items
772
+ };
773
+ }
774
+ function readScene(data, base) {
775
+ try {
776
+ return read(data, base);
777
+ } catch (e) {
778
+ return { scene: base, dropped: [], error: `unreadable: ${e instanceof Error ? e.message : typeof e}` };
779
+ }
780
+ }
781
+ var words = (v) => typeof v === "number" ? String(v) : typeof v === "string" ? JSON.stringify(v.length > 24 ? `${v.slice(0, 24)}\u2026` : v) : typeof v;
782
+ function withFixed(baseIds, stored, isFixed) {
783
+ const out = [...stored];
784
+ const missing = (id) => isFixed(id) && !stored.includes(id);
785
+ let tail = baseIds.length;
786
+ while (tail > 0 && isFixed(baseIds[tail - 1])) tail--;
787
+ baseIds.forEach((id, i) => {
788
+ if (missing(id) && i < tail) out.splice(Math.min(i, out.length), 0, id);
789
+ });
790
+ return [...out, ...baseIds.slice(tail).filter(missing)];
791
+ }
792
+ function read(data, base) {
793
+ if (!isObject(data)) return { scene: base, dropped: [], error: "not an object" };
794
+ if (data.v !== 1) return { scene: base, dropped: [], error: `unknown version ${words(data.v)} (this reads version 1)` };
795
+ const dropped = [];
796
+ const drop = (what) => {
797
+ if (!dropped.includes(what)) dropped.push(what);
798
+ };
799
+ for (const k of Object.keys(data)) if (!["v", "edges", "floats", "items"].includes(k)) drop(k);
800
+ const known = (id) => typeof id === "string" && id !== "__proto__" && Object.hasOwn(base.items, id);
801
+ const isFixed = (id) => known(id) && base.items[id].fixed === true;
802
+ const items = { ...base.items };
803
+ if (data.items !== void 0 && !isObject(data.items)) drop("items");
804
+ else if (isObject(data.items)) {
805
+ for (const id of Object.keys(data.items)) {
806
+ const s = data.items[id];
807
+ if (!known(id) || !isObject(s)) {
808
+ drop(`item:${id}`);
809
+ continue;
810
+ }
811
+ const b = base.items[id];
812
+ const { kept: _k, from: _f, closed: _c, align: _a, floatSize: _s, ...plain } = b;
813
+ let it = { ...plain, used: b.used };
814
+ for (const k of Object.keys(s)) if (!["used", "kept", "from", "closed", "align", "floatSize"].includes(k)) drop(`item:${id}:${k}`);
815
+ if (isNum(s.used)) it = { ...it, used: s.used };
816
+ else if (s.used !== void 0) drop(`item:${id}:used`);
817
+ if (s.kept === true) it = { ...it, kept: true };
818
+ else if (s.kept !== void 0 && s.kept !== false) drop(`item:${id}:kept`);
819
+ if (isNum(s.from) && Number.isInteger(s.from) && s.from >= 1 && !b.fixed) it = { ...it, from: s.from };
820
+ else if (s.from !== void 0) drop(`item:${id}:from`);
821
+ if (b.fixed && b.from !== void 0) it = { ...it, from: b.from };
822
+ if (s.closed === true && !b.fixed) it = { ...it, closed: true };
823
+ else if (s.closed !== void 0 && s.closed !== false) drop(`item:${id}:closed`);
824
+ if (s.align === "center" || s.align === "end") it = { ...it, align: s.align };
825
+ else if (s.align !== void 0 && s.align !== "start") drop(`item:${id}:align`);
826
+ if (b.fixed && b.align) it = { ...it, align: b.align };
827
+ const fs = s.floatSize;
828
+ if (isObject(fs) && isSize(fs.width) && isSize(fs.height)) it = { ...it, floatSize: { width: fs.width, height: fs.height } };
829
+ else if (fs !== void 0) drop(`item:${id}:floatSize`);
830
+ items[id] = it;
831
+ }
832
+ }
833
+ const placed = /* @__PURE__ */ new Set();
834
+ const order = /* @__PURE__ */ new Map();
835
+ const sizes = /* @__PURE__ */ new Map();
836
+ const made = [];
837
+ const rings = /* @__PURE__ */ new Map();
838
+ if (data.edges !== void 0 && !Array.isArray(data.edges)) drop("edges");
839
+ for (const e of Array.isArray(data.edges) ? data.edges : []) {
840
+ if (!isObject(e) || typeof e.id !== "string") {
841
+ drop("an edge with no id");
842
+ continue;
843
+ }
844
+ let edge = base.edges.find((x) => x.id === e.id) ?? made.find((x) => x.id === e.id);
845
+ if (!edge && e.column === true && (e.side === "left" || e.side === "right") && base.columns?.[e.side]) {
846
+ const t = base.columns[e.side];
847
+ 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 };
848
+ made.push(edge);
849
+ }
850
+ if (!edge || order.has(edge.id)) {
851
+ drop(`edge:${e.id}`);
852
+ continue;
853
+ }
854
+ if (edge.column === true && isNum(e.ring)) rings.set(edge.id, e.ring);
855
+ if (e.side !== void 0 && !SIDES.includes(e.side)) drop(`edge:${e.id}:side`);
856
+ const ids = [];
857
+ for (const id of Array.isArray(e.items) ? e.items : []) {
858
+ if (!known(id) || placed.has(id)) drop(`edge:${edge.id}:${String(id)}`);
859
+ else if (isFixed(id)) {
860
+ if (edge.items.includes(id) && base.edges.some((x) => x.id === edge.id)) {
861
+ ids.push(id);
862
+ placed.add(id);
863
+ } else drop(`edge:${edge.id}:${id}`);
864
+ } else if (!edge.accepts.includes(items[id].kind)) drop(`edge:${edge.id}:${id}`);
865
+ else {
866
+ ids.push(id);
867
+ placed.add(id);
868
+ }
869
+ }
870
+ order.set(edge.id, ids);
871
+ if (isNum(e.size)) sizes.set(edge.id, e.size);
872
+ else if (e.size !== void 0) drop(`edge:${edge.id}:size`);
873
+ else sizes.set(edge.id, void 0);
874
+ }
875
+ const floats = [];
876
+ if (data.floats !== void 0 && !Array.isArray(data.floats)) drop("floats");
877
+ for (const f of Array.isArray(data.floats) ? data.floats : []) {
878
+ if (!isObject(f) || typeof f.id !== "string") {
879
+ drop("a float with no id");
880
+ continue;
881
+ }
882
+ const id = f.id;
883
+ 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)) {
884
+ drop(`float:${id}`);
885
+ continue;
886
+ }
887
+ for (const k of Object.keys(f)) if (k === "pinned") drop(`pinned:${id}`);
888
+ else if (!["id", "x", "y", "width", "height", "orient", "anchor"].includes(k)) drop(`float:${id}:${k}`);
889
+ const o = f.orient === "row" || f.orient === "column" ? f.orient : void 0;
890
+ if (f.orient !== void 0 && !o) drop(`float:${id}:orient`);
891
+ const a = ANCHORS.includes(f.anchor) ? f.anchor : void 0;
892
+ if (f.anchor !== void 0 && !a) drop(`float:${id}:anchor`);
893
+ placed.add(id);
894
+ const r = rect(f.x, f.y, f.width, f.height);
895
+ const drawn = drawnFloatSize(items[id], { rect: r, ...o ? { orient: o } : {} });
896
+ floats.push({ id, rect: clampFloat(r, base.stage, drawn), ...o ? { orient: o } : {}, ...a ? { anchor: a } : {} });
897
+ }
898
+ const edges = [...base.edges, ...made].map((edge) => {
899
+ const stored = order.get(edge.id) ?? [];
900
+ const rest = edge.items.filter((id) => !isFixed(id) && !placed.has(id));
901
+ for (const id of rest) placed.add(id);
902
+ let tail = stored.length;
903
+ while (tail > 0 && isFixed(stored[tail - 1])) tail--;
904
+ const listed = [...stored.slice(0, tail), ...rest, ...stored.slice(tail)];
905
+ const items1 = withFixed(edge.items, listed, isFixed);
906
+ const size = sizes.has(edge.id) ? sizes.get(edge.id) : edge.size;
907
+ const { size: _s, ...plain } = edge;
908
+ return { ...plain, ring: rings.get(edge.id) ?? edge.ring, items: items1, ...size !== void 0 ? { size } : {} };
909
+ });
910
+ for (const f of base.floats) if (!placed.has(f.id)) floats.push(f);
911
+ const scene = prune({ ...base, items, edges: closeRings(edges, new Set(made.length > 0 || rings.size > 0 ? ["left", "right"] : [])), floats });
912
+ return { scene, dropped };
913
+ }
914
+
915
+ // src/scene/targets.ts
916
+ var EPS2 = 1e-6;
917
+ var PLANE = rect(-1e9, -1e9, 2e9, 2e9);
918
+ var area = (r) => Math.max(0, r.width) * Math.max(0, r.height);
919
+ var overlaps = (a, b) => Math.min(a.x + a.width, b.x + b.width) - Math.max(a.x, b.x) > EPS2 && Math.min(a.y + a.height, b.y + b.height) - Math.max(a.y, b.y) > EPS2;
920
+ var contains = (r, p) => p.x >= r.x - EPS2 && p.x <= r.x + r.width + EPS2 && p.y >= r.y - EPS2 && p.y <= r.y + r.height + EPS2;
921
+ var RAIL_REACH = 24;
922
+ function cutAway(hit, blockers) {
923
+ let h = hit;
924
+ for (const b of blockers) {
925
+ if (!overlaps(h, b)) continue;
926
+ const right = h.x + h.width;
927
+ const bottom = h.y + h.height;
928
+ const parts = [
929
+ rect(h.x, h.y, Math.min(right, b.x) - h.x, h.height),
930
+ rect(Math.max(h.x, b.x + b.width), h.y, right - Math.max(h.x, b.x + b.width), h.height),
931
+ rect(h.x, h.y, h.width, Math.min(bottom, b.y) - h.y),
932
+ rect(h.x, Math.max(h.y, b.y + b.height), h.width, bottom - Math.max(h.y, b.y + b.height))
933
+ ].filter((p) => area(p) > EPS2);
934
+ if (parts.length === 0) return null;
935
+ h = parts.reduce((a, p) => area(p) > area(a) ? p : a);
936
+ }
937
+ return area(h) > EPS2 ? h : null;
938
+ }
939
+ function cutClear(hit, blockers, keep2) {
940
+ let h = hit;
941
+ for (const b of blockers) {
942
+ if (!overlaps(h, b)) continue;
943
+ const right = h.x + h.width;
944
+ const bottom = h.y + h.height;
945
+ const parts = [
946
+ rect(h.x, h.y, Math.min(right, b.x) - h.x, h.height),
947
+ rect(Math.max(h.x, b.x + b.width), h.y, right - Math.max(h.x, b.x + b.width), h.height),
948
+ rect(h.x, h.y, h.width, Math.min(bottom, b.y) - h.y),
949
+ rect(h.x, Math.max(h.y, b.y + b.height), h.width, bottom - Math.max(h.y, b.y + b.height))
950
+ ].filter((p) => area(p) > EPS2 && contains(p, keep2));
951
+ if (parts.length === 0) return null;
952
+ h = parts.reduce((a, p) => area(p) > area(a) ? p : a);
953
+ }
954
+ return area(h) > EPS2 ? h : null;
955
+ }
956
+ function targets(scene, dragged, options = {}) {
957
+ const item = Object.hasOwn(scene.items, dragged) ? scene.items[dragged] : void 0;
958
+ if (!item || item.fixed || item.closed) return [];
959
+ const prev = { ...options.previous ? { previous: options.previous } : {}, ...options.fresh ? { fresh: options.fresh } : {} };
960
+ const settle = (s) => ({ scene: s, layout: solve(s, prev) });
961
+ const now = solve(scene, prev);
962
+ const rest = lift(scene, dragged);
963
+ const base = solve(rest, prev);
964
+ const reach = Math.max(0, Number.isFinite(options.endReach) ? options.endReach : 0);
965
+ const chrome = scene.chrome ?? [];
966
+ const grows = (l, id) => {
967
+ const it = l.items[id];
968
+ const e = it.edge ? l.edges[it.edge] : void 0;
969
+ return !!e && !!formsOf(scene.items[id], axisOf(e.side))[it.form]?.main.grows;
970
+ };
971
+ const fixedRects = (l) => Object.entries(l.items).filter(([id, it]) => it.fixed && !grows(l, id)).map(([, it]) => it.rect);
972
+ const blockers = [...fixedRects(now), ...fixedRects(base), ...chrome];
973
+ const out = [];
974
+ const docked = [];
975
+ for (const edge of rest.edges) {
976
+ if (!edge.accepts.includes(item.kind)) continue;
977
+ docked.push(...edgeTargets(scene, rest, now, base, edge, dragged, reach, blockers, chrome, settle));
978
+ }
979
+ for (const side of ["left", "right"]) {
980
+ const template = scene.columns?.[side];
981
+ if (!template || !zoneTakes(template, "start", item.kind)) continue;
982
+ docked.push(...columnTargets(scene, rest, base, side, dragged, blockers, settle));
983
+ if (scene.rails?.[side]?.accepts.includes(item.kind)) docked.push(...railTargets(scene, rest, base, side, dragged, blockers, settle));
984
+ }
985
+ const here = placeKey(scene, dragged);
986
+ const ownRail = now.items[dragged]?.rail;
987
+ const noteKey = (n) => "edge" in n && (n.code === "overflow" || n.code === "too-wide") ? `${n.edge}:${n.code}:${"id" in n ? n.id : ""}` : null;
988
+ const hadNotes = new Set(base.notes.map(noteKey).filter((k) => k !== null));
989
+ const sound = (after) => {
990
+ if (after.notes.some((n) => {
991
+ const k = noteKey(n);
992
+ return k !== null && !hadNotes.has(k);
993
+ })) return false;
994
+ for (const [id, b] of Object.entries(base.items)) {
995
+ if (!b.fixed) continue;
996
+ const a = after.items[id];
997
+ const e = a?.edge ? after.edges[a.edge] : void 0;
998
+ if (!a || !e || a.form !== b.form || a.formKind !== b.formKind) return false;
999
+ const f = formsOf(scene.items[id], axisOf(e.side))[a.form];
1000
+ const len = axisOf(e.side) === "column" ? a.rect.height : a.rect.width;
1001
+ if (f && len < formMain(f, "min", e.cross) - 0.5) return false;
1002
+ }
1003
+ const me = after.items[dragged];
1004
+ if (!me) return true;
1005
+ const fixedAfter = Object.entries(after.items).filter(([id, it]) => it.fixed && id !== dragged).map(([id]) => reservedRect(scene, after, id));
1006
+ return !chrome.some((c2) => overlaps(me.rect, c2)) && !fixedAfter.some((f) => overlaps(me.rect, f));
1007
+ };
1008
+ for (const t of docked) {
1009
+ const p = t.place;
1010
+ const own2 = p.kind === "rail" && p.side === ownRail || placeKey(t.after({ x: 0, y: 0 }).scene, dragged) === here;
1011
+ if (own2) out.push({ id: t.id, priority: t.priority, hit: t.hit, item: dragged, place: { kind: "home" }, after: () => ({ scene, layout: now }) });
1012
+ else if (sound(t.after({ x: 0, y: 0 }).layout)) out.push(t);
1013
+ }
1014
+ const edgeOfGap = (t) => t.place.kind === "gap" ? rest.edges.find((e) => e.id === t.place.edge) : void 0;
1015
+ const plainGapHits = out.filter((t) => t.id.startsWith("gap:") && !edgeOfGap(t)?.compact).map((t) => t.hit);
1016
+ for (let i = 0; i < out.length; i++) {
1017
+ const t = out[i];
1018
+ const e = edgeOfGap(t);
1019
+ if (!t.id.startsWith("gap:") || !e?.compact || !plainGapHits.some((h2) => overlaps(h2, t.hit))) continue;
1020
+ const h = t.hit;
1021
+ const core = e.side === "left" ? rect(h.x, h.y, h.width - RAIL_REACH, h.height) : e.side === "right" ? rect(h.x + RAIL_REACH, h.y, h.width - RAIL_REACH, h.height) : e.side === "top" ? rect(h.x, h.y, h.width, h.height - RAIL_REACH) : rect(h.x, h.y + RAIL_REACH, h.width, h.height - RAIL_REACH);
1022
+ out[i] = { ...t, hit: core };
1023
+ }
1024
+ const gapHits = out.filter((t) => t.id.startsWith("gap:")).map((t) => t.hit);
1025
+ const c = base.content;
1026
+ for (let i = out.length - 1; i >= 0; i--) {
1027
+ const t = out[i];
1028
+ const e = edgeOfGap(t);
1029
+ if (!t.id.startsWith("strip:") || !e) continue;
1030
+ const vertical = e.side === "left" || e.side === "right";
1031
+ const x0 = Math.max(t.hit.x, c.x);
1032
+ const y0 = Math.max(t.hit.y, c.y);
1033
+ const x1 = Math.min(t.hit.x + t.hit.width, c.x + c.width);
1034
+ const y1 = Math.min(t.hit.y + t.hit.height, c.y + c.height);
1035
+ const docks = [...Object.entries(base.edges), ...Object.entries(now.edges).filter(([id]) => id !== now.items[dragged]?.edge)].filter(([id, d]) => id !== e.id && area(d.rect) > EPS2).map(([, d]) => d.rect);
1036
+ const h = e.compact ? t.hit : x1 - x0 > EPS2 && y1 - y0 > EPS2 ? cutAway(rect(x0, y0, x1 - x0, y1 - y0), docks) : null;
1037
+ if (!h) {
1038
+ out.splice(i, 1);
1039
+ continue;
1040
+ }
1041
+ let [a0, a1] = vertical ? [h.y, h.y + h.height] : [h.x, h.x + h.width];
1042
+ for (const g of gapHits) {
1043
+ if (!overlaps(g, h)) continue;
1044
+ const spans = vertical ? g.x <= h.x + EPS2 && g.x + g.width >= h.x + h.width - EPS2 : g.y <= h.y + EPS2 && g.y + g.height >= h.y + h.height - EPS2;
1045
+ if (!spans) continue;
1046
+ const [g0, g1] = vertical ? [g.y, g.y + g.height] : [g.x, g.x + g.width];
1047
+ if (g0 <= a0 + EPS2 && g1 > a0) a0 = Math.min(a1, g1);
1048
+ else if (g1 >= a1 - EPS2 && g0 < a1) a1 = Math.max(a0, g0);
1049
+ }
1050
+ if (a1 - a0 <= EPS2) out.splice(i, 1);
1051
+ else {
1052
+ const hit = vertical ? rect(h.x, a0, h.width, a1 - a0) : rect(a0, h.y, a1 - a0, h.height);
1053
+ if (hit.x !== t.hit.x || hit.y !== t.hit.y || hit.width !== t.hit.width || hit.height !== t.hit.height) out[i] = { ...t, hit };
1054
+ }
1055
+ }
1056
+ if (scene.floatKinds.includes(item.kind)) {
1057
+ const was = scene.floats.find((f) => f.id === dragged);
1058
+ const size = options.floatSize ?? (was ? { width: was.rect.width, height: was.rect.height } : item.floatSize ?? DEFAULT_FLOAT_SIZE);
1059
+ out.push({
1060
+ id: `float:${dragged}`,
1061
+ priority: 3,
1062
+ free: true,
1063
+ hit: PLANE,
1064
+ item: dragged,
1065
+ place: { kind: "float" },
1066
+ after(point, grab) {
1067
+ const g = grab ?? { x: size.width / 2, y: size.height / 2 };
1068
+ const raw = rect(point.x - g.x, point.y - g.y, size.width, size.height);
1069
+ const first = solve(use(floatAt(scene, dragged, raw), dragged), prev);
1070
+ const at = first.items[dragged]?.rect ?? raw;
1071
+ return settle(use(floatAt(scene, dragged, rect(at.x, at.y, size.width, size.height)), dragged));
1072
+ }
1073
+ });
1074
+ }
1075
+ (scene.blocked ?? []).forEach((b, i) => {
1076
+ out.push({ id: `none:${i}`, priority: 1, hit: rect(b.x, b.y, b.width, b.height), item: dragged, place: { kind: "none" }, after: () => ({ scene, layout: now }) });
1077
+ });
1078
+ const own = now.items[dragged];
1079
+ if (own && !own.floating) out.push({ id: `home:${dragged}`, priority: 1, hit: own.rect, item: dragged, place: { kind: "home" }, after: () => ({ scene, layout: now }) });
1080
+ return out;
1081
+ }
1082
+ function reservedRect(scene, l, id) {
1083
+ const it = l.items[id];
1084
+ const e = it.edge ? l.edges[it.edge] : void 0;
1085
+ const f = e ? formsOf(scene.items[id], axisOf(e.side))[it.form] : void 0;
1086
+ if (!e || !f?.main.grows) return it.rect;
1087
+ const column = axisOf(e.side) === "column";
1088
+ const len = column ? it.rect.height : it.rect.width;
1089
+ const keep2 = Math.min(len, Math.max(0, Number.isFinite(f.main.natural) ? f.main.natural : len));
1090
+ const shift = it.zone === "end" ? len - keep2 : it.zone === "center" ? (len - keep2) / 2 : 0;
1091
+ 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);
1092
+ }
1093
+ function placeKey(s, id) {
1094
+ const it = Object.hasOwn(s.items, id) ? s.items[id] : void 0;
1095
+ return JSON.stringify({
1096
+ edges: s.edges.map((e) => JSON.stringify([e.side, e.ring, e.column === true ? "" : e.id, e.items, e.size ?? null])).sort(),
1097
+ floats: s.floats.map((f) => [f.id, f.rect, f.anchor ?? null, f.orient ?? null]),
1098
+ from: it?.from ?? 0,
1099
+ align: it?.align ?? "start"
1100
+ });
1101
+ }
1102
+ var COLUMN_REACH = 22;
1103
+ var RAIL_EDGE = 10;
1104
+ function openColumns(rest, base, side) {
1105
+ return 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 }));
1106
+ }
1107
+ function sideSpan(base, side) {
1108
+ const own = Object.values(base.edges).filter((e) => e.side === side && e.rect.height > 0);
1109
+ const r = own[0]?.rect ?? base.content;
1110
+ return { y: r.y, height: r.height };
1111
+ }
1112
+ function columnTargets(scene, rest, base, side, dragged, blockers, settle) {
1113
+ const cols = openColumns(rest, base, side);
1114
+ const span = sideSpan(base, side);
1115
+ const mine = rest.edges.filter((e) => e.side === side);
1116
+ const inner = mine.length === 0 ? 0 : Math.max(...mine.map((e) => e.ring)) + 1;
1117
+ const lines = [];
1118
+ const outerX = (r) => side === "left" ? r.x : r.x + r.width;
1119
+ const innerX = (r) => side === "left" ? r.x + r.width : r.x;
1120
+ cols.forEach((c) => lines.push({ x: outerX(c.r), ring: c.edge.ring }));
1121
+ if (cols.length > 0) lines.push({ x: innerX(cols[cols.length - 1].r), ring: cols[cols.length - 1].edge.ring + 1 });
1122
+ else lines.push({ x: side === "left" ? base.content.x : base.content.x + base.content.width, ring: inner });
1123
+ const out = [];
1124
+ for (const line of lines) {
1125
+ const place = { kind: "column", side, ring: line.ring };
1126
+ const result = settle(use(moveTo(scene, dragged, place), dragged));
1127
+ const me = result.layout.items[dragged];
1128
+ const el = me?.edge ? result.layout.edges[me.edge] : void 0;
1129
+ if (!me || !el || me.inMenu || el.folded || el.rides || result.scene === scene) continue;
1130
+ const keep2 = { x: line.x, y: span.y + span.height / 2 };
1131
+ const hit = cutClear(rect(line.x - COLUMN_REACH, span.y, 2 * COLUMN_REACH, span.height), blockers, keep2);
1132
+ if (!hit) continue;
1133
+ out.push({ id: `column:${side}:${line.ring}`, priority: 2, hit, slot: me.rect, marker: rect(line.x, span.y, 0, span.height), item: dragged, place, after: () => result });
1134
+ }
1135
+ return out;
1136
+ }
1137
+ function railTargets(scene, rest, base, side, dragged, blockers, settle) {
1138
+ const rail = base.rails[side];
1139
+ const mine = rest.edges.filter((e) => e.side === side).sort((a, b) => a.ring - b.ring);
1140
+ const ringOf = (id) => rest.edges.find((e) => e.id === id)?.ring ?? 0;
1141
+ const stops2 = [];
1142
+ let zone;
1143
+ if (rail && rail.segments.length > 0) {
1144
+ zone = rail.rect;
1145
+ const groups = [];
1146
+ for (const s of rail.segments) {
1147
+ const g = groups[groups.length - 1];
1148
+ if (g && g.edge === s.edge) g.y1 = s.rect.y + s.rect.height;
1149
+ else groups.push({ edge: s.edge, y0: s.rect.y, y1: s.rect.y + s.rect.height });
1150
+ }
1151
+ for (const g of groups) stops2.push({ y: g.y0, ring: ringOf(g.edge) });
1152
+ const last = groups[groups.length - 1];
1153
+ stops2.push({ y: last.y1, ring: ringOf(last.edge) + 1 });
1154
+ } else {
1155
+ const span = sideSpan(base, side);
1156
+ const firstCol = mine.find((e) => e.column === true);
1157
+ const outer = Object.values(base.edges).filter((e) => e.side === side && e.rect.height > 0);
1158
+ const x = side === "left" ? Math.min(base.content.x, ...outer.map((e) => e.rect.x)) : Math.max(base.content.x + base.content.width, ...outer.map((e) => e.rect.x + e.rect.width));
1159
+ zone = rect(side === "left" ? x : x - RAIL_EDGE, span.y, RAIL_EDGE, span.height);
1160
+ stops2.push({ y: span.y, ring: firstCol ? firstCol.ring : mine.length === 0 ? 0 : mine[mine.length - 1].ring + 1 });
1161
+ }
1162
+ const out = [];
1163
+ stops2.forEach((s, i) => {
1164
+ const from = i === 0 ? zone.y : (stops2[i - 1].y + s.y) / 2;
1165
+ const to = i === stops2.length - 1 ? zone.y + zone.height : (s.y + stops2[i + 1].y) / 2;
1166
+ if (to - from <= EPS2) return;
1167
+ const place = { kind: "rail", side, ring: s.ring };
1168
+ const result = settle(use(moveTo(scene, dragged, place), dragged));
1169
+ const me = result.layout.items[dragged];
1170
+ if (!me || me.rail !== side || result.scene === scene) return;
1171
+ const hit = cutClear(rect(zone.x, from, zone.width, to - from), blockers, { x: zone.x + zone.width / 2, y: Math.min(Math.max(s.y, from), to) });
1172
+ if (!hit) return;
1173
+ out.push({ id: `rail:${side}:${s.ring}`, priority: 1, hit, slot: me.rect, marker: rect(zone.x, me.rect.y, zone.width, 0), item: dragged, place, after: () => result });
1174
+ });
1175
+ return out;
1176
+ }
1177
+ var SIDE_RANK = { left: 0, top: 1, bottom: 2, right: 3 };
1178
+ function stops(scene, dragged, options = {}) {
1179
+ const list = targets(scene, dragged, options).filter((t) => t.place.kind !== "home" && t.place.kind !== "none");
1180
+ const sideOf = (t) => {
1181
+ const p = t.place;
1182
+ if (p.kind === "float") return "float";
1183
+ if (p.kind === "column" || p.kind === "rail") return p.side;
1184
+ if (p.kind === "gap") return scene.edges.find((e) => e.id === p.edge)?.side ?? "float";
1185
+ return "float";
1186
+ };
1187
+ const key = (t) => {
1188
+ const side = sideOf(t);
1189
+ const r = t.slot ?? t.hit;
1190
+ const cx = r.x + r.width / 2;
1191
+ const cy = r.y + r.height / 2;
1192
+ const rank = side === "float" ? 4 : SIDE_RANK[side];
1193
+ return side === "left" ? [rank, cx, cy] : side === "right" ? [rank, -cx, cy] : [rank, cy * (side === "bottom" ? -1 : 1), cx];
1194
+ };
1195
+ 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);
1196
+ }
1197
+ function edgeTargets(scene, rest, now, base, edge, dragged, reach, blockers, chrome, settle) {
1198
+ const el = base.edges[edge.id];
1199
+ if (!el || el.folded || el.rides) return [];
1200
+ const column = axisOf(edge.side) === "column";
1201
+ const restEdge = rest.edges.find((e) => e.id === edge.id);
1202
+ const fixedIds = restEdge.items.filter((id) => rest.items[id]?.fixed && base.items[id]?.edge === edge.id);
1203
+ const tooWideBefore = new Set(base.notes.flatMap((n) => "edge" in n && n.edge === edge.id && n.code === "too-wide" ? [n.id] : []));
1204
+ const fits = (after) => {
1205
+ const me = after.items[dragged];
1206
+ if (!me || me.edge !== edge.id || me.inMenu) return false;
1207
+ for (const n of after.notes) {
1208
+ if (!("edge" in n) || n.edge !== edge.id) continue;
1209
+ if (n.code === "overflow") return false;
1210
+ if (n.code === "too-wide" && !tooWideBefore.has(n.id)) return false;
1211
+ }
1212
+ for (const id of fixedIds) {
1213
+ const a = after.items[id];
1214
+ const b = base.items[id];
1215
+ if (!a || a.form !== b.form || a.formKind !== b.formKind || Math.abs(a.rect.x - b.rect.x) > 0.5 || Math.abs(a.rect.y - b.rect.y) > 0.5) return false;
1216
+ }
1217
+ return !chrome.some((c) => overlaps(me.rect, c));
1218
+ };
1219
+ const markerOf = (after) => {
1220
+ const slot = after.items[dragged].rect;
1221
+ const e = after.edges[edge.id];
1222
+ return column ? rect(e.rect.x, slot.y, e.cross, 0) : rect(slot.x, e.rect.y, 0, e.cross);
1223
+ };
1224
+ const ownNow = now.items[dragged]?.edge;
1225
+ const otherOf = (edges, skip) => Object.entries(edges).filter(([id, e]) => id !== edge.id && id !== skip && e.rect.width > EPS2 && e.rect.height > EPS2 && !(edge.compact && e.side === edge.side)).map(([, e]) => e.rect);
1226
+ const otherEdges = otherOf(base.edges);
1227
+ const nowEdges = otherOf(now.edges, ownNow);
1228
+ const own = el.rect;
1229
+ const trimOff = (h, others, k, besideOnly = false) => {
1230
+ let [a0, a1] = column ? [h.y, h.y + h.height] : [h.x, h.x + h.width];
1231
+ let [c0, c1] = column ? [h.x, h.x + h.width] : [h.y, h.y + h.height];
1232
+ const km = column ? k.y : k.x;
1233
+ const kc = column ? k.x : k.y;
1234
+ const [w0, w1] = column ? [own.x, own.x + own.width] : [own.y, own.y + own.height];
1235
+ for (const o of others) {
1236
+ const [o0, o1] = column ? [o.y, o.y + o.height] : [o.x, o.x + o.width];
1237
+ const [p0, p1] = column ? [o.x, o.x + o.width] : [o.y, o.y + o.height];
1238
+ if (Math.min(a1, o1) - Math.max(a0, o0) <= EPS2 || Math.min(c1, p1) - Math.max(c0, p0) <= EPS2) continue;
1239
+ const across = !besideOnly && Math.min(w1, p1) - Math.max(w0, p0) > EPS2;
1240
+ if (!across) {
1241
+ if (p1 <= kc + EPS2) c0 = Math.max(c0, p1);
1242
+ else if (p0 >= kc - EPS2) c1 = Math.min(c1, p0);
1243
+ } else if (o1 <= km + EPS2) a0 = Math.max(a0, o1);
1244
+ else if (o0 >= km - EPS2) a1 = Math.min(a1, o0);
1245
+ }
1246
+ return column ? rect(c0, a0, Math.max(0, c1 - c0), Math.max(0, a1 - a0)) : rect(a0, c0, Math.max(0, a1 - a0), Math.max(0, c1 - c0));
1247
+ };
1248
+ const target = (id, place, hit, keep2, solved) => {
1249
+ const result = solved ?? settle(use(moveTo(scene, dragged, place), dragged));
1250
+ if (!fits(result.layout)) return null;
1251
+ const clear = cutClear(id.startsWith("gap:") ? trimOff(trimOff(hit, otherEdges, keep2), nowEdges, keep2, true) : hit, blockers, keep2);
1252
+ if (!clear) return null;
1253
+ return { id, priority: 2, hit: clear, slot: result.layout.items[dragged].rect, marker: markerOf(result.layout), item: dragged, place, after: () => result };
1254
+ };
1255
+ const drawn = base.edges[edge.id].stack.items.filter((p) => p.kind !== "menu").map((p) => p.id);
1256
+ if (drawn.length === 0 && !el.menu) {
1257
+ const depth = Math.max(0, Number.isFinite(edge.strip) ? edge.strip : DEFAULT_STRIP);
1258
+ const r = el.rect;
1259
+ const hit = edge.side === "left" ? rect(r.x, r.y, depth, r.height) : edge.side === "right" ? rect(r.x + r.width - depth, r.y, depth, r.height) : edge.side === "top" ? rect(r.x, r.y, r.width, depth) : rect(r.x, r.y + r.height - depth, r.width, depth);
1260
+ const keep2 = { x: hit.x + hit.width / 2, y: hit.y + hit.height / 2 };
1261
+ if (!zoneTakes(edge, "start", scene.items[dragged].kind)) return [];
1262
+ const t = target(`strip:${edge.id}`, { kind: "gap", edge: edge.id, zone: "start", at: 0 }, hit, keep2);
1263
+ return t ? [t] : [];
1264
+ }
1265
+ const main0 = column ? el.rect.y : el.rect.x;
1266
+ const length = column ? el.rect.height : el.rect.width;
1267
+ const at = (r) => column ? r.y : r.x;
1268
+ const len = (r) => column ? r.height : r.width;
1269
+ const pad = { start: Math.max(0, edge.pad?.start ?? 0), end: Math.max(0, edge.pad?.end ?? 0) };
1270
+ const menuLen = el.menu ? len(el.menu.rect) : 0;
1271
+ const occupied = (id) => base.items[id]?.fixed ? reservedRect(scene, base, id) : base.items[id].rect;
1272
+ const drawnOf = (z) => restEdge.items.filter((id) => zoneOf(rest.items[id]) === z && base.items[id]?.edge === edge.id && !base.items[id].inMenu).map(occupied);
1273
+ const startEnd = (() => {
1274
+ const s = drawnOf("start");
1275
+ return s.length ? at(s[s.length - 1]) + len(s[s.length - 1]) : main0 + pad.start;
1276
+ })();
1277
+ const endStart = (() => {
1278
+ const e = drawnOf("end");
1279
+ return e.length ? at(e[0]) : main0 + length - pad.end - menuLen;
1280
+ })();
1281
+ const empty = { start: main0 + pad.start, center: Math.min(Math.max(main0 + length / 2, startEnd), Math.max(startEnd, endStart)), end: main0 + length - pad.end - menuLen };
1282
+ const gaps = [];
1283
+ for (const zone of zonesTaking(edge, scene.items[dragged].kind)) {
1284
+ const members = restEdge.items.filter((id) => zoneOf(rest.items[id]) === zone);
1285
+ for (let k = 0; k <= members.length; k++) {
1286
+ const next = members.slice(k).find((id) => base.items[id]?.edge === edge.id && !base.items[id].inMenu);
1287
+ const before = members.slice(0, k).reverse().find((id) => base.items[id]?.edge === edge.id && !base.items[id].inMenu);
1288
+ const line = next ? at(occupied(next)) : before ? at(occupied(before)) + len(occupied(before)) : empty[zone];
1289
+ gaps.push({ zone, at: k, line });
1290
+ }
1291
+ }
1292
+ const sorted = gaps.map((g, i) => ({ ...g, i })).sort((a, b) => a.line - b.line || a.i - b.i).filter((g, i, all) => i === 0 || Math.abs(g.line - all[i - 1].line) > EPS2);
1293
+ const out = [];
1294
+ const live = sorted.map((g) => {
1295
+ const place = { kind: "gap", edge: edge.id, zone: g.zone, at: g.at };
1296
+ return { ...g, place, result: settle(use(moveTo(scene, dragged, place), dragged)) };
1297
+ }).filter((g) => fits(g.result.layout));
1298
+ const bandOpt = edge.hitBand ?? DEFAULT_HIT_BAND;
1299
+ const band = bandOpt === "middle" ? Infinity : Math.max(0, Number.isFinite(bandOpt) ? bandOpt : DEFAULT_HIT_BAND);
1300
+ const bodies = Number.isFinite(band) ? Object.values(base.items).filter((p) => p.edge === edge.id && p.formKind === "open").map((p) => ({ a: at(p.rect), b: at(p.rect) + len(p.rect) })) : [];
1301
+ const inward = edge.compact ? RAIL_REACH : 0;
1302
+ live.forEach((g, i) => {
1303
+ let from = i === 0 ? -Infinity : (live[i - 1].line + g.line) / 2;
1304
+ let to = i === live.length - 1 ? Infinity : (g.line + live[i + 1].line) / 2;
1305
+ for (const p of bodies) {
1306
+ if (p.b <= g.line + EPS2 && p.b > from) from = Math.max(from, p.b - band / 2);
1307
+ if (p.a >= g.line - EPS2 && p.a < to) to = Math.min(to, p.a + band / 2);
1308
+ }
1309
+ from = from <= main0 + pad.start + EPS2 ? main0 - reach : from;
1310
+ to = to >= main0 + length - pad.end - menuLen - EPS2 ? main0 + length + reach : to;
1311
+ from = Math.max(from, main0 - reach);
1312
+ to = Math.min(to, main0 + length + reach);
1313
+ if (to - from <= EPS2) return;
1314
+ const thick = Math.max(el.cross, g.result.layout.edges[edge.id]?.cross ?? 0) + inward;
1315
+ const r = el.rect;
1316
+ const hit = column ? rect(edge.side === "left" ? r.x : r.x + r.width - (thick - inward) - inward, from, thick, to - from) : rect(from, edge.side === "top" ? r.y : r.y + r.height - (thick - inward) - inward, to - from, thick);
1317
+ const line = Math.min(Math.max(g.line, from), to);
1318
+ const keep2 = column ? { x: r.x + r.width / 2, y: line } : { x: line, y: r.y + r.height / 2 };
1319
+ const t = target(`gap:${edge.id}:${g.zone}:${g.at}`, g.place, hit, keep2, g.result);
1320
+ if (t) out.push(t);
1321
+ });
1322
+ return out;
1323
+ }
1324
+
1325
+ export {
1326
+ SIDES,
1327
+ ZONES,
1328
+ zoneTakes,
1329
+ zonesTaking,
1330
+ DEFAULT_STRIP,
1331
+ DEFAULT_HIT_BAND,
1332
+ DEFAULT_FLOAT_SIZE,
1333
+ solve,
1334
+ lift,
1335
+ moveTo,
1336
+ floatAt,
1337
+ fold,
1338
+ use,
1339
+ keep,
1340
+ open,
1341
+ close,
1342
+ raise,
1343
+ orient,
1344
+ resize,
1345
+ homeFor,
1346
+ dockHome,
1347
+ storeScene,
1348
+ readScene,
1349
+ targets,
1350
+ reservedRect,
1351
+ stops
1352
+ };
1353
+ //# sourceMappingURL=chunk-HQE2DOWA.js.map