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