@n-uf/placement 26.11.25 → 26.11.26

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.
@@ -5,7 +5,7 @@ import {
5
5
  formMain,
6
6
  formSetsCross,
7
7
  wrapLines
8
- } from "./chunk-KGZCYLQN.js";
8
+ } from "./chunk-IHU6IOZH.js";
9
9
  import {
10
10
  COLUMN_REACH,
11
11
  EPS,
@@ -13,7 +13,6 @@ import {
13
13
  RAIL_EDGE,
14
14
  area,
15
15
  clampInto,
16
- contains,
17
16
  overlaps,
18
17
  rect,
19
18
  subtract
@@ -22,11 +21,13 @@ import {
22
21
  // src/scene/model.ts
23
22
  var SIDES = ["top", "right", "bottom", "left"];
24
23
  var ZONES = ["start", "center", "end"];
25
- var zoneTakes = (edge, zone, kind) => edge.accepts.includes(kind) && (edge.zoneAccepts === void 0 || !!edge.zoneAccepts[zone]?.includes(kind));
26
- var zonesTaking = (edge, kind) => ZONES.filter((z) => zoneTakes(edge, z, kind));
27
24
  var DEFAULT_STRIP = 24;
28
25
  var DEFAULT_HIT_BAND = 24;
29
26
  var DEFAULT_FLOAT_SIZE = { width: 280, height: 360 };
27
+
28
+ // src/scene/helpers.ts
29
+ var zoneTakes = (edge, zone, kind) => edge.accepts.includes(kind) && (edge.zoneAccepts === void 0 || !!edge.zoneAccepts[zone]?.includes(kind));
30
+ var zonesTaking = (edge, kind) => ZONES.filter((z) => zoneTakes(edge, z, kind));
30
31
  var axisOf = (side) => side === "left" || side === "right" ? "column" : "row";
31
32
  var zoneOf = (item) => item?.align === "center" || item?.align === "end" ? item.align : "start";
32
33
  var formsOf = (item, axis) => item.forms[axis] ?? [];
@@ -77,16 +78,16 @@ function floatShape(item, f) {
77
78
  return { form, width: w, height: h };
78
79
  }
79
80
 
80
- // src/scene/solve.ts
81
+ // src/scene/solve/order.ts
81
82
  var finite = (x, fallback) => typeof x === "number" && Number.isFinite(x) ? x : fallback;
82
83
  var px = (x) => Math.max(0, finite(x, 0));
84
+ var isSide = (e) => e.side === "left" || e.side === "right";
83
85
  function cutOrder(edges) {
84
86
  const phase = (e) => e.side === "left" || e.side === "right" ? 1 : e.span === "between" ? 2 : 0;
85
87
  const sideRank = (e) => e.side === "top" || e.side === "left" ? 0 : 1;
86
88
  return edges.map((e, i) => ({ e, i })).sort((a, b) => phase(a.e) - phase(b.e) || sideRank(a.e) - sideRank(b.e) || finite(a.e.ring, 0) - finite(b.e.ring, 0) || a.i - b.i).map((x) => x.e);
87
89
  }
88
90
  function layKeys(scene) {
89
- const isSide = (e) => e.side === "left" || e.side === "right";
90
91
  const phase = (e) => e.mode === "overlay" ? 4 : isSide(e) ? 2 : e.span === "between" ? 3 : e.outsideInset === true ? 0 : 1;
91
92
  return new Map(cutOrder(scene.edges).map((e, at) => [e.id, { phase: phase(e), side: e.side, at }]));
92
93
  }
@@ -99,16 +100,44 @@ var ZONE_RANK = { start: 0, center: 1, end: 2 };
99
100
  function zoneSorted(scene, ids) {
100
101
  return ids.map((id, i) => ({ id, i, z: ZONE_RANK[zoneOf(scene.items[id])] })).sort((a, b) => a.z - b.z || a.i - b.i).map((x) => x.id);
101
102
  }
102
- var stackItemOf = (item, forms, from) => ({
103
- id: item.id,
104
- forms,
105
- used: finite(item.used, 0),
106
- ...item.kept ? { kept: true } : {},
107
- ...from !== void 0 ? { from } : item.from !== void 0 ? { from: item.from } : {},
108
- ...item.fixed ? { fixed: true } : {}
109
- });
110
- function hasSetter(fit, forms) {
111
- return fit.items.some((p) => p.kind !== "menu" && formSetsCross(forms(p.id)[p.form]));
103
+
104
+ // src/scene/solve/draft.ts
105
+ function createDraft(given, options) {
106
+ const previous = options.previous;
107
+ const opened = /* @__PURE__ */ new Set();
108
+ if (previous) {
109
+ for (const e of given.edges) {
110
+ for (const id of e.items) {
111
+ const it = Object.hasOwn(given.items, id) ? given.items[id] : void 0;
112
+ const was = previous.items[id];
113
+ if (it && !it.closed && !it.fixed && (!was || was.closed)) opened.add(id);
114
+ }
115
+ }
116
+ }
117
+ const stayedOpen = (e) => e.items.some((id) => !opened.has(id) && Object.hasOwn(given.items, id) && !given.items[id].closed && !given.items[id].fixed);
118
+ const newest = Object.values(given.items).reduce((m, it) => Math.max(m, finite(it.used, 0)), 0) + 1;
119
+ const byAge = [...opened].filter((id) => given.edges.some((e) => e.items.includes(id) && stayedOpen(e))).sort((a, b) => finite(given.items[a].used, 0) - finite(given.items[b].used, 0) || (a < b ? -1 : 1));
120
+ const scene = byAge.length === 0 ? given : { ...given, items: { ...given.items, ...Object.fromEntries(byAge.map((id, k) => [id, { ...given.items[id], used: newest + k }])) } };
121
+ const fresh = new Set(options.fresh ?? []);
122
+ const prevEdge = (edge) => !previous || fresh.has(edge.id) || edge.items.some((id) => fresh.has(id)) ? void 0 : previous.edges[edge.id];
123
+ const notes = [];
124
+ const order = cutOrder(scene.edges);
125
+ const claimed = /* @__PURE__ */ new Set();
126
+ const claim = (id) => {
127
+ if (!Object.hasOwn(scene.items, id)) {
128
+ notes.push({ code: "unknown-item", id });
129
+ return false;
130
+ }
131
+ if (claimed.has(id)) {
132
+ notes.push({ code: "twice", id });
133
+ return false;
134
+ }
135
+ claimed.add(id);
136
+ return true;
137
+ };
138
+ const owned = /* @__PURE__ */ new Map();
139
+ for (const e of order) owned.set(e, e.items.filter(claim));
140
+ return { scene, previous, opened, fresh, order, owned, claim, prevEdge, notes, items: {}, edges: {}, rails: {}, railPlaces: {} };
112
141
  }
113
142
  function shapeOf(f, r, axis, cross) {
114
143
  if (!f) return {};
@@ -121,7 +150,164 @@ function shapeOf(f, r, axis, cross) {
121
150
  if (!g) return {};
122
151
  return { grip: axis === "column" ? rect(r.x, r.y, r.width, Math.min(g, r.height)) : rect(r.x, r.y, Math.min(g, r.width), r.height) };
123
152
  }
153
+ function zoneStarts(scene, fit, input) {
154
+ const column = fit.axis === "column";
155
+ const at = (r) => column ? r.y : r.x;
156
+ const out = /* @__PURE__ */ new Map();
157
+ const lengths = /* @__PURE__ */ new Map();
158
+ const drawn = fit.items.filter((p) => p.kind !== "menu");
159
+ for (const p of drawn) {
160
+ out.set(p.id, at(p.rect));
161
+ lengths.set(p.id, column ? p.rect.height : p.rect.width);
162
+ }
163
+ const len = (p) => lengths.get(p.id);
164
+ const gap = px(input.gap);
165
+ const start = px(input.pad?.start);
166
+ const end = px(input.pad?.end);
167
+ const menu = fit.menu ? (column ? fit.menu.rect.height : fit.menu.rect.width) + (drawn.length > 0 ? gap : 0) : 0;
168
+ const zone = (z) => drawn.filter((p) => zoneOf(scene.items[p.id]) === z);
169
+ const s = zone("start");
170
+ const c = zone("center");
171
+ const e = zone("end");
172
+ const title = column ? void 0 : s.find((p) => {
173
+ const it = scene.items[p.id];
174
+ const f = formsOf(it, "row")[p.form];
175
+ return it.fixed && f?.main.grows && len(p) > formMain(f, "natural", fit.cross) + EPS;
176
+ });
177
+ if (title) lengths.set(title.id, formMain(formsOf(scene.items[title.id], "row")[title.form], "natural", fit.cross));
178
+ const used = drawn.reduce((sum, p) => sum + len(p), 0) + Math.max(0, drawn.length - 1) * gap;
179
+ const spare = fit.length - start - end - menu - used;
180
+ if (spare <= EPS) return { starts: out, lengths };
181
+ const total = (ps) => ps.reduce((sum, p) => sum + len(p), 0) + Math.max(0, ps.length - 1) * gap;
182
+ const lay = (ps, from) => {
183
+ let x = from;
184
+ for (const p of ps) {
185
+ out.set(p.id, x);
186
+ x += len(p) + gap;
187
+ }
188
+ };
189
+ lay(s, start);
190
+ const endStart = fit.length - end - menu - total(e);
191
+ lay(e, endStart);
192
+ const lo = start + total(s) + (s.length > 0 ? gap : 0);
193
+ const hi = endStart - (e.length > 0 ? gap : 0);
194
+ const mid = (fit.length - total(c)) / 2;
195
+ const clear = mid >= lo - EPS && mid + total(c) <= hi + EPS;
196
+ lay(c, title && !clear ? Math.max(lo, (lo + hi - total(c)) / 2) : Math.max(lo, Math.min(mid, hi - total(c))));
197
+ if (title && s[s.length - 1] === title) {
198
+ const next = [...c, ...e].map((p) => out.get(p.id));
199
+ const until = next.length ? Math.min(...next) - gap : fit.length - end - menu;
200
+ lengths.set(title.id, Math.max(len(title), until - out.get(title.id)));
201
+ }
202
+ return { starts: out, lengths };
203
+ }
204
+ function emit(d, edge, r, solved, extra = {}) {
205
+ const { scene, notes, items, edges } = d;
206
+ const column = axisOf(edge.side) === "column";
207
+ const { stack } = solved;
208
+ const thick = column ? r.width : r.height;
209
+ const { starts, lengths } = zoneStarts(scene, stack, solved.input);
210
+ const toStage = (l, id) => {
211
+ const s = id !== void 0 ? starts.get(id) : void 0;
212
+ const n = id !== void 0 ? lengths.get(id) : void 0;
213
+ const x = column ? l.x : s ?? l.x;
214
+ const y = column ? s ?? l.y : l.y;
215
+ return rect(r.x + x, r.y + y, column ? l.width : n ?? l.width, column ? n ?? l.height : l.height);
216
+ };
217
+ const tabs = new Set(solved.tabs);
218
+ for (const id of solved.leftOut ?? []) notes.push({ code: "no-vertical-form", edge: edge.id, id });
219
+ for (const id of solved.leftOutIcons ?? []) notes.push({ code: "no-icons-form", edge: edge.id, id });
220
+ for (const p of stack.items) {
221
+ const it = scene.items[p.id];
222
+ const inMenu = p.kind === "menu";
223
+ const drawnRect = toStage(p.rect, inMenu ? void 0 : p.id);
224
+ const f = formsOf(it, column ? "column" : "row")[p.form];
225
+ const shape = inMenu ? {} : shapeOf(f, drawnRect, column ? "column" : "row", stack.cross);
226
+ items[p.id] = {
227
+ ...shape,
228
+ hidden: inMenu ? [] : f?.hides ?? [],
229
+ rect: drawnRect,
230
+ kind: it.kind,
231
+ form: p.form,
232
+ formKind: p.kind,
233
+ edge: edge.id,
234
+ zone: zoneOf(it),
235
+ ...inMenu ? { inMenu: true } : {},
236
+ ...tabs.has(p.id) ? { tab: true } : {},
237
+ ...solved.closedTabs.has(p.id) ? { closed: true } : {},
238
+ ...it.fixed ? { fixed: true } : {}
239
+ };
240
+ }
241
+ for (const n of stack.notes) notes.push({ edge: edge.id, ...n });
242
+ edges[edge.id] = {
243
+ side: edge.side,
244
+ ring: finite(edge.ring, 0),
245
+ rect: r,
246
+ cross: thick,
247
+ menu: stack.menu ? { rect: toStage(stack.menu.rect), items: stack.menu.items } : null,
248
+ tabs: solved.tabs,
249
+ stack,
250
+ free: solved.free,
251
+ ...extra
252
+ };
253
+ }
254
+
255
+ // src/scene/solve/edge.ts
256
+ var stackItemOf = (item, forms, from) => ({
257
+ id: item.id,
258
+ forms,
259
+ used: finite(item.used, 0),
260
+ ...item.kept ? { kept: true } : {},
261
+ ...from !== void 0 ? { from } : item.from !== void 0 ? { from: item.from } : {},
262
+ ...item.fixed ? { fixed: true } : {}
263
+ });
264
+ function hasSetter(fit, forms) {
265
+ return fit.items.some((p) => p.kind !== "menu" && formSetsCross(forms(p.id)[p.form]));
266
+ }
124
267
  var TRAY_RANK = 100;
268
+ function compactEdge(f) {
269
+ const { scene, formsById } = f;
270
+ const iconsAt = (id) => {
271
+ const forms = formsById(id);
272
+ 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;
273
+ return forms.findIndex((x) => x.kind === "icons");
274
+ };
275
+ const shown = f.ordered.filter((id) => !scene.items[id].closed && iconsAt(id) >= 0);
276
+ const leftOutIcons = f.ordered.filter((id) => !scene.items[id].closed && iconsAt(id) < 0);
277
+ const compactInput = { ...f.base, ownCross: false, items: shown.map((id) => stackItemOf(scene.items[id], formsById(id), iconsAt(id))) };
278
+ const stack = fitStack(compactInput, f.previous ? { previous: f.previous.free } : {});
279
+ return { stack, free: stack, tabs: [], closedTabs: /* @__PURE__ */ new Set(), input: compactInput, leftOutIcons };
280
+ }
281
+ function railEdge(f, free, asStackItem) {
282
+ const { scene, edge, formsById } = f;
283
+ const railIndex = (item, forms) => {
284
+ if (item.fixed) return -1;
285
+ for (let j = Math.max(0, Math.floor(finite(item.from, 0))); j < forms.length; j++) if (forms[j].kind === "rail") return j;
286
+ return -1;
287
+ };
288
+ const wrapOnly = (id) => railIndex(scene.items[id], formsById(id)) < 0 && pastWrap(formsById(id), finite(scene.items[id].from, 0)) === void 0;
289
+ const shown = f.ordered.filter((id) => {
290
+ const it = scene.items[id];
291
+ if (!it.closed && wrapOnly(id)) return false;
292
+ return !it.closed || edge.railShowsClosed === true && railIndex(it, formsById(id)) >= 0;
293
+ });
294
+ const leftOut = f.ordered.filter((id) => !scene.items[id].closed && wrapOnly(id));
295
+ const anyRail = shown.some((id) => railIndex(scene.items[id], formsById(id)) >= 0);
296
+ if (!anyRail) return null;
297
+ const railInput = {
298
+ ...f.base,
299
+ items: shown.map((id) => {
300
+ const it = scene.items[id];
301
+ const j = railIndex(it, formsById(id));
302
+ const forms = asStackItem(id).forms;
303
+ return stackItemOf(it, forms, j >= 0 ? j : pastWrap(forms, finite(it.from, 0)));
304
+ })
305
+ };
306
+ const stack = fitStack(railInput, f.previous ? { previous: f.previous.stack } : {});
307
+ const tabs = stack.items.filter((p) => p.kind === "rail").map((p) => p.id);
308
+ const closedTabs = new Set(shown.filter((id) => scene.items[id].closed));
309
+ return { stack, free, tabs, closedTabs, input: railInput, leftOut };
310
+ }
125
311
  function solveEdge(scene, edge, ids, length, room, previous) {
126
312
  const axis = axisOf(edge.side);
127
313
  const column = axis === "column";
@@ -143,18 +329,8 @@ function solveEdge(scene, edge, ids, length, room, previous) {
143
329
  ...edge.pad ? { pad: edge.pad } : {},
144
330
  ...column ? { ownCross: true } : {}
145
331
  };
146
- if (edge.compact) {
147
- const iconsAt = (id) => {
148
- const forms = formsById(id);
149
- for (let j = Math.max(0, Math.floor(finite(scene.items[id].from, 0))); j < forms.length; j++) if (forms[j].kind === "icons") return j;
150
- return forms.findIndex((f) => f.kind === "icons");
151
- };
152
- const shown = ordered.filter((id) => !scene.items[id].closed && iconsAt(id) >= 0);
153
- const leftOutIcons = ordered.filter((id) => !scene.items[id].closed && iconsAt(id) < 0);
154
- const compactInput = { ...base, ownCross: false, items: shown.map((id) => stackItemOf(scene.items[id], formsById(id), iconsAt(id))) };
155
- const stack = fitStack(compactInput, previous ? { previous: previous.free } : {});
156
- return { stack, free: stack, tabs: [], closedTabs: /* @__PURE__ */ new Set(), input: compactInput, leftOutIcons };
157
- }
332
+ const frame = { scene, edge, column, ordered, formsById, base, previous };
333
+ if (edge.compact) return compactEdge(frame);
158
334
  const open2 = ordered.filter((id) => !scene.items[id].closed);
159
335
  const asStackItem = (id) => {
160
336
  const it = scene.items[id];
@@ -174,34 +350,8 @@ function solveEdge(scene, edge, ids, length, room, previous) {
174
350
  const free = fitStack(input, previous ? { previous: previous.free } : {});
175
351
  const none = /* @__PURE__ */ new Set();
176
352
  if (column && !hasSetter(free, formsById)) {
177
- const railIndex = (item, forms) => {
178
- if (item.fixed) return -1;
179
- for (let j = Math.max(0, Math.floor(finite(item.from, 0))); j < forms.length; j++) if (forms[j].kind === "rail") return j;
180
- return -1;
181
- };
182
- const wrapOnly = (id) => railIndex(scene.items[id], formsById(id)) < 0 && pastWrap(formsById(id), finite(scene.items[id].from, 0)) === void 0;
183
- const shown = ordered.filter((id) => {
184
- const it = scene.items[id];
185
- if (!it.closed && wrapOnly(id)) return false;
186
- return !it.closed || edge.railShowsClosed === true && railIndex(it, formsById(id)) >= 0;
187
- });
188
- const leftOut = ordered.filter((id) => !scene.items[id].closed && wrapOnly(id));
189
- const anyRail = shown.some((id) => railIndex(scene.items[id], formsById(id)) >= 0);
190
- if (anyRail) {
191
- const railInput = {
192
- ...base,
193
- items: shown.map((id) => {
194
- const it = scene.items[id];
195
- const j = railIndex(it, formsById(id));
196
- const forms = asStackItem(id).forms;
197
- return stackItemOf(it, forms, j >= 0 ? j : pastWrap(forms, finite(it.from, 0)));
198
- })
199
- };
200
- const stack = fitStack(railInput, previous ? { previous: previous.stack } : {});
201
- const tabs = stack.items.filter((p) => p.kind === "rail").map((p) => p.id);
202
- const closedTabs = new Set(shown.filter((id) => scene.items[id].closed));
203
- return { stack, free, tabs, closedTabs, input: railInput, leftOut };
204
- }
353
+ const rail = railEdge(frame, free, asStackItem);
354
+ if (rail) return rail;
205
355
  }
206
356
  const size = edge.size;
207
357
  if (typeof size === "number" && Number.isFinite(size) && hasSetter(free, formsById)) {
@@ -216,57 +366,21 @@ function solveEdge(scene, edge, ids, length, room, previous) {
216
366
  }
217
367
  return { stack: free, free, tabs: [], closedTabs: none, input };
218
368
  }
219
- function zoneStarts(scene, fit, input) {
220
- const column = fit.axis === "column";
221
- const at = (r) => column ? r.y : r.x;
222
- const out = /* @__PURE__ */ new Map();
223
- const lengths = /* @__PURE__ */ new Map();
224
- const drawn = fit.items.filter((p) => p.kind !== "menu");
225
- for (const p of drawn) {
226
- out.set(p.id, at(p.rect));
227
- lengths.set(p.id, column ? p.rect.height : p.rect.width);
228
- }
229
- const len = (p) => lengths.get(p.id);
230
- const gap = px(input.gap);
231
- const start = px(input.pad?.start);
232
- const end = px(input.pad?.end);
233
- const menu = fit.menu ? (column ? fit.menu.rect.height : fit.menu.rect.width) + (drawn.length > 0 ? gap : 0) : 0;
234
- const zone = (z) => drawn.filter((p) => zoneOf(scene.items[p.id]) === z);
235
- const s = zone("start");
236
- const c = zone("center");
237
- const e = zone("end");
238
- const title = column ? void 0 : s.find((p) => {
239
- const it = scene.items[p.id];
240
- const f = formsOf(it, "row")[p.form];
241
- return it.fixed && f?.main.grows && len(p) > formMain(f, "natural", fit.cross) + EPS;
242
- });
243
- if (title) lengths.set(title.id, formMain(formsOf(scene.items[title.id], "row")[title.form], "natural", fit.cross));
244
- const used = drawn.reduce((sum, p) => sum + len(p), 0) + Math.max(0, drawn.length - 1) * gap;
245
- const spare = fit.length - start - end - menu - used;
246
- if (spare <= EPS) return { starts: out, lengths };
247
- const total = (ps) => ps.reduce((sum, p) => sum + len(p), 0) + Math.max(0, ps.length - 1) * gap;
248
- const lay = (ps, from) => {
249
- let x = from;
250
- for (const p of ps) {
251
- out.set(p.id, x);
252
- x += len(p) + gap;
253
- }
254
- };
255
- lay(s, start);
256
- const endStart = fit.length - end - menu - total(e);
257
- lay(e, endStart);
258
- const lo = start + total(s) + (s.length > 0 ? gap : 0);
259
- const hi = endStart - (e.length > 0 ? gap : 0);
260
- const mid = (fit.length - total(c)) / 2;
261
- const clear = mid >= lo - EPS && mid + total(c) <= hi + EPS;
262
- lay(c, title && !clear ? Math.max(lo, (lo + hi - total(c)) / 2) : Math.max(lo, Math.min(mid, hi - total(c))));
263
- if (title && s[s.length - 1] === title) {
264
- const next = [...c, ...e].map((p) => out.get(p.id));
265
- const until = next.length ? Math.min(...next) - gap : fit.length - end - menu;
266
- lengths.set(title.id, Math.max(len(title), until - out.get(title.id)));
267
- }
268
- return { starts: out, lengths };
369
+
370
+ // src/scene/solve/bars.ts
371
+ function cutEdge(d, edge, area2, cut) {
372
+ const column = axisOf(edge.side) === "column";
373
+ const length = column ? area2.height : area2.width;
374
+ const room = Math.max(0, column ? area2.width : area2.height);
375
+ const solved = solveEdge(d.scene, edge, d.owned.get(edge), length, room, d.prevEdge(edge));
376
+ const thick = solved.stack.items.length === 0 ? 0 : Math.min(solved.stack.cross, room);
377
+ const r = edge.side === "top" ? rect(area2.x, area2.y, area2.width, thick) : edge.side === "bottom" ? rect(area2.x, area2.y + area2.height - thick, area2.width, thick) : edge.side === "left" ? rect(area2.x, area2.y, thick, area2.height) : rect(area2.x + area2.width - thick, area2.y, thick, area2.height);
378
+ emit(d, edge, r, solved);
379
+ if (!cut) return area2;
380
+ return edge.side === "top" ? rect(area2.x, area2.y + thick, area2.width, area2.height - thick) : edge.side === "bottom" ? rect(area2.x, area2.y, area2.width, area2.height - thick) : edge.side === "left" ? rect(area2.x + thick, area2.y, area2.width - thick, area2.height) : rect(area2.x, area2.y, area2.width - thick, area2.height);
269
381
  }
382
+
383
+ // src/scene/solve/floats.ts
270
384
  function anchored(content, w, h, anchor) {
271
385
  const left = content.x;
272
386
  const right = content.x + content.width - w;
@@ -287,362 +401,8 @@ function anchored(content, w, h, anchor) {
287
401
  const [x, y] = at[anchor] ?? at["content-top-right"];
288
402
  return rect(x, y, w, h);
289
403
  }
290
- function solve(given, options = {}) {
291
- const previous = options.previous;
292
- const opened = /* @__PURE__ */ new Set();
293
- if (previous) {
294
- for (const e of given.edges) {
295
- for (const id of e.items) {
296
- const it = Object.hasOwn(given.items, id) ? given.items[id] : void 0;
297
- const was = previous.items[id];
298
- if (it && !it.closed && !it.fixed && (!was || was.closed)) opened.add(id);
299
- }
300
- }
301
- }
302
- const newest = Object.values(given.items).reduce((m, it) => Math.max(m, finite(it.used, 0)), 0) + 1;
303
- const scene = opened.size === 0 ? given : { ...given, items: { ...given.items, ...Object.fromEntries([...opened].map((id) => [id, { ...given.items[id], used: newest }])) } };
304
- const fresh = new Set(options.fresh ?? []);
305
- const prevEdge = (edge) => !previous || fresh.has(edge.id) || edge.items.some((id) => fresh.has(id)) ? void 0 : previous.edges[edge.id];
306
- const notes = [];
307
- const items = {};
308
- const edges = {};
309
- const stage = rect(finite(scene.stage.x, 0), finite(scene.stage.y, 0), px(scene.stage.width), px(scene.stage.height));
310
- const order = cutOrder(scene.edges);
311
- const claimed = /* @__PURE__ */ new Set();
312
- const claim = (id) => {
313
- if (!Object.hasOwn(scene.items, id)) {
314
- notes.push({ code: "unknown-item", id });
315
- return false;
316
- }
317
- if (claimed.has(id)) {
318
- notes.push({ code: "twice", id });
319
- return false;
320
- }
321
- claimed.add(id);
322
- return true;
323
- };
324
- const owned = /* @__PURE__ */ new Map();
325
- for (const e of order) owned.set(e, e.items.filter(claim));
326
- const emit = (edge, r, solved, extra = {}) => {
327
- const column = axisOf(edge.side) === "column";
328
- const { stack } = solved;
329
- const thick = column ? r.width : r.height;
330
- const { starts, lengths } = zoneStarts(scene, stack, solved.input);
331
- const toStage = (l, id) => {
332
- const s = id !== void 0 ? starts.get(id) : void 0;
333
- const n = id !== void 0 ? lengths.get(id) : void 0;
334
- const x = column ? l.x : s ?? l.x;
335
- const y = column ? s ?? l.y : l.y;
336
- return rect(r.x + x, r.y + y, column ? l.width : n ?? l.width, column ? n ?? l.height : l.height);
337
- };
338
- const tabs = new Set(solved.tabs);
339
- for (const id of solved.leftOut ?? []) notes.push({ code: "no-vertical-form", edge: edge.id, id });
340
- for (const id of solved.leftOutIcons ?? []) notes.push({ code: "no-icons-form", edge: edge.id, id });
341
- for (const p of stack.items) {
342
- const it = scene.items[p.id];
343
- const inMenu = p.kind === "menu";
344
- const drawnRect = toStage(p.rect, inMenu ? void 0 : p.id);
345
- const shape = inMenu ? {} : shapeOf(formsOf(it, column ? "column" : "row")[p.form], drawnRect, column ? "column" : "row", stack.cross);
346
- items[p.id] = {
347
- ...shape,
348
- rect: drawnRect,
349
- kind: it.kind,
350
- form: p.form,
351
- formKind: p.kind,
352
- edge: edge.id,
353
- zone: zoneOf(it),
354
- ...inMenu ? { inMenu: true } : {},
355
- ...tabs.has(p.id) ? { tab: true } : {},
356
- ...solved.closedTabs.has(p.id) ? { closed: true } : {},
357
- ...it.fixed ? { fixed: true } : {}
358
- };
359
- }
360
- for (const n of stack.notes) notes.push({ edge: edge.id, ...n });
361
- edges[edge.id] = {
362
- side: edge.side,
363
- ring: finite(edge.ring, 0),
364
- rect: r,
365
- cross: thick,
366
- menu: stack.menu ? { rect: toStage(stack.menu.rect), items: stack.menu.items } : null,
367
- tabs: solved.tabs,
368
- stack,
369
- free: solved.free,
370
- ...extra
371
- };
372
- };
373
- const lay = (edge, area3, cut) => {
374
- const column = axisOf(edge.side) === "column";
375
- const length = column ? area3.height : area3.width;
376
- const room = Math.max(0, column ? area3.width : area3.height);
377
- const solved = solveEdge(scene, edge, owned.get(edge), length, room, prevEdge(edge));
378
- const thick = solved.stack.items.length === 0 ? 0 : Math.min(solved.stack.cross, room);
379
- const r = edge.side === "top" ? rect(area3.x, area3.y, area3.width, thick) : edge.side === "bottom" ? rect(area3.x, area3.y + area3.height - thick, area3.width, thick) : edge.side === "left" ? rect(area3.x, area3.y, thick, area3.height) : rect(area3.x + area3.width - thick, area3.y, thick, area3.height);
380
- emit(edge, r, solved);
381
- if (!cut) return area3;
382
- return edge.side === "top" ? rect(area3.x, area3.y + thick, area3.width, area3.height - thick) : edge.side === "bottom" ? rect(area3.x, area3.y, area3.width, area3.height - thick) : edge.side === "left" ? rect(area3.x + thick, area3.y, area3.width - thick, area3.height) : rect(area3.x, area3.y, area3.width - thick, area3.height);
383
- };
384
- const rails = {};
385
- const railPlaces = {};
386
- const gutter = px(scene.gutter);
387
- let columnRoom;
388
- let outerArea = stage;
389
- const isSide = (e) => e.side === "left" || e.side === "right";
390
- const PANEL_FORMS = /* @__PURE__ */ new Set(["open", "header", "rail"]);
391
- const isPanel = (id) => {
392
- const it = scene.items[id];
393
- return !it.closed && !it.fixed && PANEL_FORMS.has(formsOf(it, "column")[0]?.kind ?? "");
394
- };
395
- const docked = order.some((e) => isSide(e) && e.accepts.length > 0 && owned.get(e).some(isPanel));
396
- const inset = scene.insetWhen === "docked" && !docked ? {} : scene.inset ?? {};
397
- const laySides = (sideEdges, area3) => {
398
- const H = area3.height;
399
- const ride = scene.handFold === "ride";
400
- const upInset = area3.y <= outerArea.y + px(inset.top) + EPS ? px(inset.top) : 0;
401
- const downInset = area3.y + area3.height >= outerArea.y + outerArea.height - px(inset.bottom) - EPS ? px(inset.bottom) : 0;
402
- const tall = (edge) => H + (edge.outsideInset === true ? upInset + downInset : 0);
403
- const cols = sideEdges.map((edge) => {
404
- const side = edge.side;
405
- const solved = solveEdge(scene, edge, owned.get(edge), tall(edge), Math.max(0, area3.width), prevEdge(edge));
406
- const width = solved.stack.items.length === 0 ? 0 : Math.min(solved.stack.cross, area3.width);
407
- const visible = owned.get(edge).filter((id) => !scene.items[id].closed);
408
- const dyn = (edge.column === true || edge.giveWay === true) && scene.rails?.[side] !== void 0 && visible.length > 0;
409
- const forms = (id) => formsOf(scene.items[id], "column");
410
- const setters = solved.free.items.filter((p) => p.kind !== "menu" && formSetsCross(forms(p.id)[p.form]));
411
- const min = setters.length > 0 ? Math.min(width, Math.max(px(edge.cross.min), ...setters.map((p) => formCrossMin(forms(p.id)[p.form])))) : width;
412
- return {
413
- edge,
414
- side,
415
- solved,
416
- width,
417
- min,
418
- visible,
419
- dyn,
420
- hand: dyn && edge.column === true && visible.every((id) => finite(scene.items[id].from, 0) >= 1),
421
- kept: visible.some((id) => scene.items[id].kept === true),
422
- age: Math.max(-Infinity, ...visible.map((id) => finite(scene.items[id].used, 0)))
423
- };
424
- });
425
- const cmin = scene.contentMin ? px(scene.contentMin.width) : void 0;
426
- const order2 = cols.filter((c) => c.dyn && !c.hand && c.width > 0).sort((a, b) => Number(a.kept) - Number(b.kept) || a.age - b.age || finite(a.edge.ring, 0) - finite(b.edge.ring, 0) || (a.side === b.side ? 0 : a.side === "left" ? -1 : 1));
427
- const shrunkBefore = (previous?.notes ?? []).flatMap((n) => n.code === "column-shrunk" ? [n.edge] : []);
428
- const rankOf = (c) => shrunkBefore.includes(c.edge.id) ? shrunkBefore.indexOf(c.edge.id) : Infinity;
429
- const shrinkOrder = order2.map((c, i) => ({ c, i })).sort((a, b) => rankOf(a.c) === rankOf(b.c) ? a.i - b.i : rankOf(a.c) < rankOf(b.c) ? -1 : 1).map((x) => x.c);
430
- const railW = (side) => px(scene.rails?.[side]?.width);
431
- const evaluate = (auto2) => {
432
- const openOn = (side) => cols.some((c) => c.side === side && c.dyn && c.edge.column === true && !c.hand && !auto2.has(c) && c.width > 0);
433
- const riding = (c) => ride && c.hand && openOn(c.side);
434
- const railed = (c) => c.dyn && (auto2.has(c) || c.hand && !riding(c));
435
- const widths = new Map(cols.filter((c) => !railed(c) && !riding(c)).map((c) => [c, c.width]));
436
- const railSides = ["left", "right"].filter((s) => cols.some((c) => c.side === s && railed(c)));
437
- let used = 0;
438
- for (const [c, w] of widths) used += w > 0 ? w + (c.edge.outsideInset === true ? 0 : gutter) : 0;
439
- for (const s of railSides) used += railW(s) + gutter;
440
- let excess = cmin === void 0 ? -Infinity : used + cmin - area3.width;
441
- for (const c of shrinkOrder) {
442
- if (excess <= EPS) break;
443
- const w = widths.get(c);
444
- if (w === void 0) continue;
445
- const take = Math.min(excess, w - c.min);
446
- if (take > 0) {
447
- widths.set(c, w - take);
448
- excess -= take;
449
- }
450
- }
451
- return { auto: auto2, widths, excess, railed, riding, railSides };
452
- };
453
- let auto = /* @__PURE__ */ new Set();
454
- if (cmin !== void 0) {
455
- const unfold = () => {
456
- for (const c of [...auto].reverse()) {
457
- const fewer = new Set([...auto].filter((x) => x !== c));
458
- if (evaluate(fewer).excess <= EPS) auto = fewer;
459
- else break;
460
- }
461
- };
462
- if (previous) {
463
- const byId = new Map(order2.map((c) => [c.edge.id, c]));
464
- const anew = (c) => fresh.has(c.edge.id) || c.visible.some((id) => opened.has(id) || fresh.has(id));
465
- const was = previous.notes.flatMap((n) => (n.code === "column-folded" || n.code === "kept-folded") && byId.has(n.edge) && !anew(byId.get(n.edge)) ? [byId.get(n.edge)] : []);
466
- auto = new Set(was);
467
- if (evaluate(auto).excess <= EPS) unfold();
468
- }
469
- for (const c of order2) {
470
- if (evaluate(auto).excess <= EPS) break;
471
- if (!auto.has(c)) auto = /* @__PURE__ */ new Set([...auto, c]);
472
- }
473
- for (let size = -1; size !== auto.size; ) {
474
- size = auto.size;
475
- unfold();
476
- }
477
- }
478
- const g = evaluate(auto);
479
- const openMin = (ids, least) => Math.max(
480
- least,
481
- ...ids.map((id) => {
482
- const f = formsOf(scene.items[id], "column")[0];
483
- return f && formSetsCross(f) ? formCrossMin(f) : 0;
484
- })
485
- );
486
- const open2 = cols.filter((c) => c.edge.column === true && c.visible.length > 0).map((c) => c.hand ? openMin(c.visible, px(c.edge.cross.min)) : c.min);
487
- const templates = ["left", "right"].flatMap((s) => {
488
- const t = scene.columns?.[s];
489
- if (!t) return [];
490
- const takes = Object.values(scene.items).filter((it) => !it.fixed && !it.closed && t.accepts.includes(it.kind));
491
- return [openMin(takes.map((it) => it.id), px(t.cross.min))];
492
- });
493
- const colMins = open2.length > 0 ? open2 : templates;
494
- if (cmin !== void 0 && colMins.length > 0) {
495
- const bars = cols.filter((c) => c.width > 0 && !c.dyn && isSideBar(scene, c.edge)).reduce((s, c) => s + c.width + (c.edge.outsideInset === true ? 0 : gutter), 0);
496
- const width = area3.width - bars - (scene.insetWhen === "docked" && !docked ? px(scene.inset?.left) + px(scene.inset?.right) : 0);
497
- const need = cmin + Math.min(...colMins) + gutter;
498
- columnRoom = width >= need - EPS;
499
- if (!columnRoom) notes.push({ code: "no-column-room", width, need });
500
- }
501
- for (const c of shrinkOrder) {
502
- const w = g.widths.get(c);
503
- if (w !== void 0 && w < c.width - EPS) notes.push({ code: "column-shrunk", edge: c.edge.id, from: c.width, to: w });
504
- }
505
- for (const c of g.auto) notes.push({ code: c.kept ? "kept-folded" : "column-folded", edge: c.edge.id });
506
- let x0 = area3.x;
507
- let x1 = area3.x + area3.width;
508
- for (const side of ["left", "right"]) {
509
- const outsideSideBar = (c) => c.width > 0 && isOutsideSideBar(scene, c.edge) && !g.railed(c) && !g.riding(c);
510
- const all = cols.filter((c) => c.side === side);
511
- const mine = [...all.filter(outsideSideBar), ...all.filter((c) => !outsideSideBar(c))];
512
- const railed = mine.filter((c) => g.railed(c));
513
- const riders = mine.filter((c) => g.riding(c));
514
- const host = mine.find((c) => c.dyn && c.edge.column === true && !c.hand && !g.auto.has(c) && c.width > 0);
515
- const riderBars = (c) => zoneSorted(scene, c.visible).map((id) => {
516
- const forms = formsOf(scene.items[id], "column");
517
- const form = forms.length === 0 ? 0 : Math.min(Math.max(0, Math.floor(finite(scene.items[id].from, 0))), forms.length - 1);
518
- return { id, form, f: forms[form], h: px(forms[form]?.main.natural) };
519
- });
520
- const ridersTop = riders.reduce((s, c) => s + riderBars(c).reduce((t, b) => t + b.h, 0), 0);
521
- let railRect = null;
522
- let gap = px(side === "left" ? inset.left : inset.right);
523
- let barLaid = false;
524
- let railPlace = null;
525
- const placeRail = () => {
526
- const w = railW(side);
527
- const lead = barLaid ? gap : 0;
528
- return side === "left" ? rect(x0 - lead, area3.y, w, H) : rect(x1 + lead - w, area3.y, w, H);
529
- };
530
- for (const c of mine) {
531
- if (!railPlace && !outsideSideBar(c)) {
532
- railPlace = placeRail();
533
- if (railed.length > 0) {
534
- railRect = railPlace;
535
- const lead = barLaid ? gap : 0;
536
- const after = barLaid ? Math.max(gap, gutter) : gutter;
537
- if (side === "left") x0 += railPlace.width - lead + after;
538
- else x1 -= railPlace.width - lead + after;
539
- gap = after;
540
- }
541
- }
542
- if (outsideSideBar(c)) barLaid = true;
543
- const base = { side: c.edge.side, ring: finite(c.edge.ring, 0), cross: 0, menu: null, tabs: [], stack: c.solved.stack, free: c.solved.free };
544
- if (g.railed(c)) {
545
- edges[c.edge.id] = { ...base, rect: rect(railRect.x, area3.y, 0, H), folded: g.auto.has(c) ? "auto" : "hand" };
546
- continue;
547
- }
548
- if (g.riding(c)) continue;
549
- const w = Math.min(g.widths.get(c), Math.max(0, x1 - x0));
550
- const top = c === host ? ridersTop : 0;
551
- let solved = c.solved;
552
- if (top > 0 || Math.abs(w - c.width) > EPS) {
553
- const forced = { ...c.edge, cross: { ...c.edge.cross, min: w, max: w } };
554
- const { size: _s, ...plain } = forced;
555
- solved = solveEdge(scene, plain, owned.get(c.edge), H - top, w, prevEdge(c.edge));
556
- }
557
- let r;
558
- if (c.edge.outsideInset === true) {
559
- const y0 = area3.y - upInset;
560
- const h = tall(c.edge);
561
- if (side === "left") {
562
- r = rect(x0 - gap, y0, w, h);
563
- x0 += w;
564
- } else {
565
- r = rect(x1 + gap - w, y0, w, h);
566
- x1 -= w;
567
- }
568
- if (Math.abs(h - H) > EPS || Math.abs(w - c.width) > EPS) {
569
- const forced = { ...c.edge, cross: { ...c.edge.cross, min: w, max: w } };
570
- const { size: _s, ...plain } = forced;
571
- solved = solveEdge(scene, plain, owned.get(c.edge), h, w, prevEdge(c.edge));
572
- }
573
- } else if (side === "left") {
574
- r = rect(x0, area3.y + top, w, H - top);
575
- x0 += w > 0 ? w + gutter : 0;
576
- if (w > 0) gap = gutter;
577
- } else {
578
- x1 -= w;
579
- r = rect(x1, area3.y + top, w, H - top);
580
- x1 -= w > 0 ? gutter : 0;
581
- if (w > 0) gap = gutter;
582
- }
583
- emit(c.edge, r, solved, w < c.width - EPS ? { shrunk: true } : {});
584
- }
585
- if (host && riders.length > 0) {
586
- const hr = edges[host.edge.id].rect;
587
- let y = area3.y;
588
- for (const c of riders) {
589
- const bars = riderBars(c);
590
- const y0 = y;
591
- for (const b of bars) {
592
- const at = rect(hr.x, y, hr.width, b.h);
593
- const it = scene.items[b.id];
594
- items[b.id] = { rect: at, kind: it.kind, form: b.form, formKind: b.f?.kind ?? "header", edge: c.edge.id, zone: zoneOf(it), rides: host.edge.id, folded: "hand" };
595
- y += b.h;
596
- }
597
- edges[c.edge.id] = { side: c.edge.side, ring: finite(c.edge.ring, 0), rect: rect(hr.x, y0, hr.width, y - y0), cross: 0, menu: null, tabs: [], stack: c.solved.stack, free: c.solved.free, rides: host.edge.id, folded: "hand" };
598
- }
599
- }
600
- if (railRect) {
601
- const segments = [];
602
- let y = railRect.y;
603
- for (const c of railed) {
604
- for (const id of zoneSorted(scene, c.visible)) {
605
- const it = scene.items[id];
606
- const forms = formsOf(it, "column");
607
- const ri = forms.findIndex((f) => f.kind === "rail");
608
- const form = ri >= 0 ? ri : Math.min(Math.max(0, Math.floor(finite(it.from, 0))), Math.max(0, forms.length - 1));
609
- const h = ri >= 0 ? px(forms[ri].main.natural) : railRect.width;
610
- const at = rect(railRect.x, y, railRect.width, h);
611
- items[id] = { rect: at, kind: it.kind, form, formKind: "rail", edge: c.edge.id, zone: zoneOf(it), rail: side, folded: g.auto.has(c) ? "auto" : "hand" };
612
- segments.push({ id, edge: c.edge.id, rect: at });
613
- y += h;
614
- }
615
- }
616
- rails[side] = { rect: railRect, segments };
617
- }
618
- if (scene.rails?.[side]) railPlaces[side] = railPlace ?? placeRail();
619
- }
620
- return rect(x0, area3.y, Math.max(0, x1 - x0), H);
621
- };
622
- const reserved = order.filter((e) => e.mode !== "overlay");
623
- const outsideBar = (e) => !isSide(e) && e.span !== "between" && e.outsideInset === true;
624
- let outer = stage;
625
- for (const e of reserved) if (outsideBar(e)) outer = lay(e, outer, true);
626
- outerArea = outer;
627
- let area2 = rect(outer.x + px(inset.left), outer.y + px(inset.top), Math.max(0, outer.width - px(inset.left) - px(inset.right)), Math.max(0, outer.height - px(inset.top) - px(inset.bottom)));
628
- for (const e of reserved) if (!isSide(e) && e.span !== "between" && !outsideBar(e)) area2 = lay(e, area2, true);
629
- area2 = laySides(reserved.filter(isSide), area2);
630
- for (const e of reserved) if (!isSide(e) && e.span === "between") area2 = lay(e, area2, true);
631
- const content = area2;
632
- let over = content;
633
- for (const e of order) if (e.mode === "overlay") over = lay(e, over, true);
634
- if (scene.contentMin) {
635
- const minW = px(scene.contentMin.width);
636
- const minH = px(scene.contentMin.height);
637
- if (content.width < minW - EPS) notes.push({ code: "content-narrow", width: content.width, min: minW });
638
- const bars = order.filter((e) => e.mode === "overlay" && !isSide(e)).reduce((s, e) => s + (edges[e.id]?.cross ?? 0), 0);
639
- if (content.height - bars < minH - EPS) notes.push({ code: "content-short", height: content.height - bars, min: minH });
640
- }
641
- if (previous) {
642
- const was = previous.content;
643
- const moved = Math.abs(was.x - content.x) > EPS || Math.abs(was.y - content.y) > EPS || Math.abs(was.width - content.width) > EPS || Math.abs(was.height - content.height) > EPS;
644
- if (moved) notes.push({ code: "content-changed", from: was, to: content });
645
- }
404
+ function layFloats(d, stage, content, inset) {
405
+ const { scene, notes, items, edges } = d;
646
406
  const avoid = [];
647
407
  for (const l of Object.values(items)) avoid.push(l.rect);
648
408
  for (const e of Object.values(edges)) {
@@ -653,7 +413,7 @@ function solve(given, options = {}) {
653
413
  const floatRegion = rect(stage.x + px(inset.left), stage.y, Math.max(0, stage.width - px(inset.left) - px(inset.right)), stage.height);
654
414
  const floats = [];
655
415
  for (const f of scene.floats) {
656
- if (!claim(f.id)) continue;
416
+ if (!d.claim(f.id)) continue;
657
417
  const it = scene.items[f.id];
658
418
  if (it.closed) continue;
659
419
  const shape = floatShape(it, f);
@@ -678,10 +438,339 @@ function solve(given, options = {}) {
678
438
  if (fit.clamped) notes.push({ code: "float-clamped", id: f.id });
679
439
  if (fit.snapped) notes.push({ code: "float-moved", id: f.id });
680
440
  if (fit.covers.length > 0) notes.push({ code: "float-covers", id: f.id });
681
- items[f.id] = { ...shapeOf(form, fit.rect, fo, fit.rect.width), rect: fit.rect, kind: it.kind, form: from, formKind: form?.kind ?? "open", floating: true };
441
+ items[f.id] = { ...shapeOf(form, fit.rect, fo, fit.rect.width), hidden: form?.hides ?? [], rect: fit.rect, kind: it.kind, form: from, formKind: form?.kind ?? "open", floating: true };
682
442
  floats.push(f.id);
683
443
  }
684
- return { items, edges, content, floats, rails, railPlaces, ...columnRoom !== void 0 ? { columnRoom } : {}, notes };
444
+ return floats;
445
+ }
446
+
447
+ // src/scene/solve/insets.ts
448
+ function insetOf(d) {
449
+ const { scene } = d;
450
+ const PANEL_FORMS = /* @__PURE__ */ new Set(["open", "header", "rail"]);
451
+ const isPanel = (id) => {
452
+ const it = scene.items[id];
453
+ return !it.closed && !it.fixed && PANEL_FORMS.has(formsOf(it, "column")[0]?.kind ?? "");
454
+ };
455
+ const docked = d.order.some((e) => isSide(e) && e.accepts.length > 0 && d.owned.get(e).some(isPanel));
456
+ const inset = scene.insetWhen === "docked" && !docked ? {} : scene.inset ?? {};
457
+ return { inset, docked };
458
+ }
459
+ var insideInset = (outer, inset) => rect(outer.x + px(inset.left), outer.y + px(inset.top), Math.max(0, outer.width - px(inset.left) - px(inset.right)), Math.max(0, outer.height - px(inset.top) - px(inset.bottom)));
460
+ function insetReach(area2, f) {
461
+ const up = area2.y <= f.outer.y + px(f.inset.top) + EPS ? px(f.inset.top) : 0;
462
+ const down = area2.y + area2.height >= f.outer.y + f.outer.height - px(f.inset.bottom) - EPS ? px(f.inset.bottom) : 0;
463
+ return { up, down };
464
+ }
465
+
466
+ // src/scene/solve/notes.ts
467
+ function contentNotes(d, content) {
468
+ const { scene, notes, previous } = d;
469
+ if (scene.contentMin) {
470
+ const minW = px(scene.contentMin.width);
471
+ const minH = px(scene.contentMin.height);
472
+ if (content.width < minW - EPS) notes.push({ code: "content-narrow", width: content.width, min: minW });
473
+ const bars = d.order.filter((e) => e.mode === "overlay" && !isSide(e)).reduce((s, e) => s + (d.edges[e.id]?.cross ?? 0), 0);
474
+ if (content.height - bars < minH - EPS) notes.push({ code: "content-short", height: content.height - bars, min: minH });
475
+ }
476
+ if (previous) {
477
+ const was = previous.content;
478
+ 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;
479
+ if (moved) notes.push({ code: "content-changed", from: was, to: content });
480
+ }
481
+ }
482
+
483
+ // src/scene/solve/columns.ts
484
+ function columnsOf(d, sideEdges, area2, tall) {
485
+ const { scene } = d;
486
+ return sideEdges.map((edge) => {
487
+ const side = edge.side;
488
+ const solved = solveEdge(scene, edge, d.owned.get(edge), tall(edge), Math.max(0, area2.width), d.prevEdge(edge));
489
+ const width = solved.stack.items.length === 0 ? 0 : Math.min(solved.stack.cross, area2.width);
490
+ const visible = d.owned.get(edge).filter((id) => !scene.items[id].closed);
491
+ const dyn = (edge.column === true || edge.giveWay === true) && scene.rails?.[side] !== void 0 && visible.length > 0;
492
+ const forms = (id) => formsOf(scene.items[id], "column");
493
+ const setters = solved.free.items.filter((p) => p.kind !== "menu" && formSetsCross(forms(p.id)[p.form]));
494
+ const min = setters.length > 0 ? Math.min(width, Math.max(px(edge.cross.min), ...setters.map((p) => formCrossMin(forms(p.id)[p.form])))) : width;
495
+ return {
496
+ edge,
497
+ side,
498
+ solved,
499
+ width,
500
+ min,
501
+ visible,
502
+ dyn,
503
+ hand: dyn && edge.column === true && visible.every((id) => finite(scene.items[id].from, 0) >= 1),
504
+ kept: visible.some((id) => scene.items[id].kept === true),
505
+ age: Math.max(-Infinity, ...visible.map((id) => finite(scene.items[id].used, 0)))
506
+ };
507
+ });
508
+ }
509
+ function giveWay(d, cols, area2, gutter) {
510
+ const { scene, previous, fresh, opened } = d;
511
+ const ride = scene.handFold === "ride";
512
+ const cmin = scene.contentMin ? px(scene.contentMin.width) : void 0;
513
+ const foldOrder = 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));
514
+ const shrunkBefore = (previous?.notes ?? []).flatMap((n) => n.code === "column-shrunk" ? [n.edge] : []);
515
+ const rankOf = (c) => shrunkBefore.includes(c.edge.id) ? shrunkBefore.indexOf(c.edge.id) : Infinity;
516
+ const shrinkOrder = foldOrder.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);
517
+ const railW = (side) => px(scene.rails?.[side]?.width);
518
+ const evaluate = (auto2) => {
519
+ const openOn = (side) => cols.some((c) => c.side === side && c.dyn && c.edge.column === true && !c.hand && !auto2.has(c) && c.width > 0);
520
+ const riding = (c) => ride && c.hand && openOn(c.side);
521
+ const railed = (c) => c.dyn && (auto2.has(c) || c.hand && !riding(c));
522
+ const widths = new Map(cols.filter((c) => !railed(c) && !riding(c)).map((c) => [c, c.width]));
523
+ const railSides = ["left", "right"].filter((s) => cols.some((c) => c.side === s && railed(c)));
524
+ let used = 0;
525
+ for (const [c, w] of widths) used += w > 0 ? w + (c.edge.outsideInset === true ? 0 : gutter) : 0;
526
+ for (const s of railSides) used += railW(s) + gutter;
527
+ let excess = cmin === void 0 ? -Infinity : used + cmin - area2.width;
528
+ for (const c of shrinkOrder) {
529
+ if (excess <= EPS) break;
530
+ const w = widths.get(c);
531
+ if (w === void 0) continue;
532
+ const take = Math.min(excess, w - c.min);
533
+ if (take > 0) {
534
+ widths.set(c, w - take);
535
+ excess -= take;
536
+ }
537
+ }
538
+ return { auto: auto2, widths, excess, railed, riding, shrinkOrder };
539
+ };
540
+ let auto = /* @__PURE__ */ new Set();
541
+ if (cmin !== void 0) {
542
+ const unfold = () => {
543
+ for (const c of [...auto].reverse()) {
544
+ const fewer = new Set([...auto].filter((x) => x !== c));
545
+ if (evaluate(fewer).excess <= EPS) auto = fewer;
546
+ else break;
547
+ }
548
+ };
549
+ if (previous) {
550
+ const byId = new Map(foldOrder.map((c) => [c.edge.id, c]));
551
+ const anew = (c) => fresh.has(c.edge.id) || c.visible.some((id) => fresh.has(id)) || c.visible.some((id) => opened.has(id)) && c.visible.some((id) => !opened.has(id));
552
+ 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)] : []);
553
+ auto = new Set(was);
554
+ if (evaluate(auto).excess <= EPS) unfold();
555
+ }
556
+ for (const c of foldOrder) {
557
+ if (evaluate(auto).excess <= EPS) break;
558
+ if (!auto.has(c)) auto = /* @__PURE__ */ new Set([...auto, c]);
559
+ }
560
+ for (let size = -1; size !== auto.size; ) {
561
+ size = auto.size;
562
+ unfold();
563
+ }
564
+ }
565
+ return evaluate(auto);
566
+ }
567
+ function columnRoom(d, cols, area2, f) {
568
+ const { scene } = d;
569
+ const cmin = scene.contentMin ? px(scene.contentMin.width) : void 0;
570
+ const openMin = (ids, least) => Math.max(
571
+ least,
572
+ ...ids.map((id) => {
573
+ const form = formsOf(scene.items[id], "column")[0];
574
+ return form && formSetsCross(form) ? formCrossMin(form) : 0;
575
+ })
576
+ );
577
+ const open2 = cols.filter((c) => c.edge.column === true && c.visible.length > 0).map((c) => c.hand ? openMin(c.visible, px(c.edge.cross.min)) : c.min);
578
+ const templates = ["left", "right"].flatMap((s) => {
579
+ const t = scene.columns?.[s];
580
+ if (!t) return [];
581
+ const takes = Object.values(scene.items).filter((it) => !it.fixed && !it.closed && t.accepts.includes(it.kind));
582
+ return [openMin(takes.map((it) => it.id), px(t.cross.min))];
583
+ });
584
+ const colMins = open2.length > 0 ? open2 : templates;
585
+ if (cmin === void 0 || colMins.length === 0) return;
586
+ const bars = cols.filter((c) => c.width > 0 && !c.dyn && isSideBar(scene, c.edge)).reduce((s, c) => s + c.width + (c.edge.outsideInset === true ? 0 : f.gutter), 0);
587
+ const width = area2.width - bars - (scene.insetWhen === "docked" && !f.docked ? px(scene.inset?.left) + px(scene.inset?.right) : 0);
588
+ const need = cmin + Math.min(...colMins) + f.gutter;
589
+ d.columnRoom = width >= need - EPS;
590
+ if (!d.columnRoom) d.notes.push({ code: "no-column-room", width, need });
591
+ }
592
+ function giveWayNotes(d, g, cols) {
593
+ const { scene, previous } = d;
594
+ const dormant = new Set(cols.filter((c) => c.visible.length === 0 && (c.edge.column === true || c.edge.giveWay === true) && scene.rails?.[c.side] !== void 0).map((c) => c.edge.id));
595
+ const before = previous?.notes ?? [];
596
+ const shrunk = /* @__PURE__ */ new Map();
597
+ for (const c of g.shrinkOrder) {
598
+ const w = g.widths.get(c);
599
+ if (w !== void 0 && w < c.width - EPS) shrunk.set(c.edge.id, { code: "column-shrunk", edge: c.edge.id, from: c.width, to: w });
600
+ }
601
+ const folded = new Map([...g.auto].map((c) => [c.edge.id, { code: c.kept ? "kept-folded" : "column-folded", edge: c.edge.id }]));
602
+ const merge = (now, kept) => {
603
+ const order = [];
604
+ for (const n of before) {
605
+ const edge = kept(n) && "edge" in n ? n.edge : void 0;
606
+ if (edge === void 0) continue;
607
+ if (dormant.has(edge)) order.push(n);
608
+ else if (now.has(edge)) {
609
+ order.push(now.get(edge));
610
+ now.delete(edge);
611
+ }
612
+ }
613
+ d.notes.push(...order, ...now.values());
614
+ };
615
+ d.notes.push(...shrunk.values());
616
+ merge(folded, (n) => n.code === "column-folded" || n.code === "kept-folded");
617
+ }
618
+
619
+ // src/scene/solve/rails.ts
620
+ function riderBars(d, c) {
621
+ const { scene } = d;
622
+ return zoneSorted(scene, c.visible).map((id) => {
623
+ const forms = formsOf(scene.items[id], "column");
624
+ const form = forms.length === 0 ? 0 : Math.min(Math.max(0, Math.floor(finite(scene.items[id].from, 0))), forms.length - 1);
625
+ return { id, form, f: forms[form], h: px(forms[form]?.main.natural) };
626
+ });
627
+ }
628
+ var ridersHeight = (d, riders) => riders.reduce((s, c) => s + riderBars(d, c).reduce((t, b) => t + b.h, 0), 0);
629
+ function layRiders(d, host, riders, top) {
630
+ const { scene, items, edges } = d;
631
+ const hr = edges[host.edge.id].rect;
632
+ let y = top;
633
+ for (const c of riders) {
634
+ const bars = riderBars(d, c);
635
+ const y0 = y;
636
+ for (const b of bars) {
637
+ const at = rect(hr.x, y, hr.width, b.h);
638
+ const it = scene.items[b.id];
639
+ items[b.id] = { rect: at, hidden: b.f?.hides ?? [], kind: it.kind, form: b.form, formKind: b.f?.kind ?? "header", edge: c.edge.id, zone: zoneOf(it), rides: host.edge.id, folded: "hand" };
640
+ y += b.h;
641
+ }
642
+ edges[c.edge.id] = { side: c.edge.side, ring: finite(c.edge.ring, 0), rect: rect(hr.x, y0, hr.width, y - y0), cross: 0, menu: null, tabs: [], stack: c.solved.stack, free: c.solved.free, rides: host.edge.id, folded: "hand" };
643
+ }
644
+ }
645
+ function layRail(d, side, railRect, railed, g) {
646
+ const { scene, items } = d;
647
+ const segments = [];
648
+ let y = railRect.y;
649
+ for (const c of railed) {
650
+ for (const id of zoneSorted(scene, c.visible)) {
651
+ const it = scene.items[id];
652
+ const forms = formsOf(it, "column");
653
+ const ri = forms.findIndex((f) => f.kind === "rail");
654
+ const form = ri >= 0 ? ri : Math.min(Math.max(0, Math.floor(finite(it.from, 0))), Math.max(0, forms.length - 1));
655
+ const h = ri >= 0 ? px(forms[ri].main.natural) : railRect.width;
656
+ const at = rect(railRect.x, y, railRect.width, h);
657
+ items[id] = { rect: at, hidden: forms[form]?.hides ?? [], kind: it.kind, form, formKind: "rail", edge: c.edge.id, zone: zoneOf(it), rail: side, folded: g.auto.has(c) ? "auto" : "hand" };
658
+ segments.push({ id, edge: c.edge.id, rect: at });
659
+ y += h;
660
+ }
661
+ }
662
+ d.rails[side] = { rect: railRect, segments };
663
+ }
664
+
665
+ // src/scene/solve/sides.ts
666
+ function laySides(d, sideEdges, area2, f) {
667
+ const H = area2.height;
668
+ const reach = insetReach(area2, f);
669
+ const tall = (edge) => H + (edge.outsideInset === true ? reach.up + reach.down : 0);
670
+ const cols = columnsOf(d, sideEdges, area2, tall);
671
+ const g = giveWay(d, cols, area2, f.gutter);
672
+ columnRoom(d, cols, area2, f);
673
+ giveWayNotes(d, g, cols);
674
+ const walk = { x0: area2.x, x1: area2.x + area2.width };
675
+ for (const side of ["left", "right"]) laySide(d, side, cols, g, area2, f, walk, reach.up, tall);
676
+ return rect(walk.x0, area2.y, Math.max(0, walk.x1 - walk.x0), H);
677
+ }
678
+ function forced(d, c, length, w) {
679
+ const edge = { ...c.edge, cross: { ...c.edge.cross, min: w, max: w } };
680
+ const { size: _s, ...plain } = edge;
681
+ return solveEdge(d.scene, plain, d.owned.get(c.edge), length, w, d.prevEdge(c.edge));
682
+ }
683
+ function laySide(d, side, cols, g, area2, f, walk, upInset, tall) {
684
+ const { scene, edges } = d;
685
+ const H = area2.height;
686
+ const outsideSideBar = (c) => c.width > 0 && isOutsideSideBar(scene, c.edge) && !g.railed(c) && !g.riding(c);
687
+ const all = cols.filter((c) => c.side === side);
688
+ const mine = [...all.filter(outsideSideBar), ...all.filter((c) => !outsideSideBar(c))];
689
+ const railed = mine.filter((c) => g.railed(c));
690
+ const riders = mine.filter((c) => g.riding(c));
691
+ const host = mine.find((c) => c.dyn && c.edge.column === true && !c.hand && !g.auto.has(c) && c.width > 0);
692
+ const ridersTop = ridersHeight(d, riders);
693
+ const railW = px(scene.rails?.[side]?.width);
694
+ let railRect = null;
695
+ let gap = px(side === "left" ? f.inset.left : f.inset.right);
696
+ let barLaid = false;
697
+ let railPlace = null;
698
+ const placeRail = () => {
699
+ const lead = barLaid ? gap : 0;
700
+ return side === "left" ? rect(walk.x0 - lead, area2.y, railW, H) : rect(walk.x1 + lead - railW, area2.y, railW, H);
701
+ };
702
+ for (const c of mine) {
703
+ if (!railPlace && !outsideSideBar(c)) {
704
+ railPlace = placeRail();
705
+ if (railed.length > 0) {
706
+ railRect = railPlace;
707
+ const lead = barLaid ? gap : 0;
708
+ const after = barLaid ? Math.max(gap, f.gutter) : f.gutter;
709
+ if (side === "left") walk.x0 += railPlace.width - lead + after;
710
+ else walk.x1 -= railPlace.width - lead + after;
711
+ gap = after;
712
+ }
713
+ }
714
+ if (outsideSideBar(c)) barLaid = true;
715
+ 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 };
716
+ if (g.railed(c)) {
717
+ edges[c.edge.id] = { ...base, rect: rect(railRect.x, area2.y, 0, H), folded: g.auto.has(c) ? "auto" : "hand" };
718
+ continue;
719
+ }
720
+ if (g.riding(c)) continue;
721
+ const w = Math.min(g.widths.get(c), Math.max(0, walk.x1 - walk.x0));
722
+ const top = c === host ? ridersTop : 0;
723
+ let solved = top > 0 || Math.abs(w - c.width) > EPS ? forced(d, c, H - top, w) : c.solved;
724
+ let r;
725
+ if (c.edge.outsideInset === true) {
726
+ const y0 = area2.y - upInset;
727
+ const h = tall(c.edge);
728
+ if (side === "left") {
729
+ r = rect(walk.x0 - gap, y0, w, h);
730
+ walk.x0 += w;
731
+ } else {
732
+ r = rect(walk.x1 + gap - w, y0, w, h);
733
+ walk.x1 -= w;
734
+ }
735
+ if (Math.abs(h - H) > EPS || Math.abs(w - c.width) > EPS) solved = forced(d, c, h, w);
736
+ } else if (side === "left") {
737
+ r = rect(walk.x0, area2.y + top, w, H - top);
738
+ walk.x0 += w > 0 ? w + f.gutter : 0;
739
+ if (w > 0) gap = f.gutter;
740
+ } else {
741
+ walk.x1 -= w;
742
+ r = rect(walk.x1, area2.y + top, w, H - top);
743
+ walk.x1 -= w > 0 ? f.gutter : 0;
744
+ if (w > 0) gap = f.gutter;
745
+ }
746
+ emit(d, c.edge, r, solved, w < c.width - EPS ? { shrunk: true } : {});
747
+ }
748
+ if (host && riders.length > 0) layRiders(d, host, riders, area2.y);
749
+ if (railRect) layRail(d, side, railRect, railed, g);
750
+ if (scene.rails?.[side]) d.railPlaces[side] = railPlace ?? placeRail();
751
+ }
752
+
753
+ // src/scene/solve.ts
754
+ function solve(given, options = {}) {
755
+ const d = createDraft(given, options);
756
+ const { scene, order } = d;
757
+ const stage = rect(finite(scene.stage.x, 0), finite(scene.stage.y, 0), px(scene.stage.width), px(scene.stage.height));
758
+ const { inset, docked } = insetOf(d);
759
+ const reserved = order.filter((e) => e.mode !== "overlay");
760
+ const outsideBar = (e) => !isSide(e) && e.span !== "between" && e.outsideInset === true;
761
+ let outer = stage;
762
+ for (const e of reserved) if (outsideBar(e)) outer = cutEdge(d, e, outer, true);
763
+ let area2 = insideInset(outer, inset);
764
+ for (const e of reserved) if (!isSide(e) && e.span !== "between" && !outsideBar(e)) area2 = cutEdge(d, e, area2, true);
765
+ area2 = laySides(d, reserved.filter(isSide), area2, { inset, gutter: px(scene.gutter), docked, outer });
766
+ for (const e of reserved) if (!isSide(e) && e.span === "between") area2 = cutEdge(d, e, area2, true);
767
+ const content = area2;
768
+ let over = content;
769
+ for (const e of order) if (e.mode === "overlay") over = cutEdge(d, e, over, true);
770
+ contentNotes(d, content);
771
+ const floats = layFloats(d, stage, content, inset);
772
+ const { items, edges, rails, railPlaces, columnRoom: columnRoom2, notes } = d;
773
+ return { items, edges, content, floats, rails, railPlaces, ...columnRoom2 !== void 0 ? { columnRoom: columnRoom2 } : {}, notes };
685
774
  }
686
775
 
687
776
  // src/scene/changes.ts
@@ -1078,7 +1167,7 @@ function read(data, base) {
1078
1167
  return { scene, dropped };
1079
1168
  }
1080
1169
 
1081
- // src/scene/targets.ts
1170
+ // src/scene/targets/kinds.ts
1082
1171
  var RAIL_REACH = 24;
1083
1172
  var EDGE_KINDS = {
1084
1173
  bar: { zoneExtent: "thirds", opens: "any", inward: 0, place: DEFAULT_HIT_BAND / 2, phase: null, layer: 10, overreach: 30, priority: 2, reach: Infinity, virtual: false },
@@ -1095,6 +1184,8 @@ function edgeKindOf(scene, e) {
1095
1184
  return isSideBar(scene, e) ? "sideBar" : "column";
1096
1185
  }
1097
1186
  var opensEmpty = (scene, edge, kind) => EDGE_KINDS[edgeKindOf(scene, edge)].opens === "any" || zoneTakes(edge, "start", kind);
1187
+
1188
+ // src/scene/targets/edges.ts
1098
1189
  function reservedRect(scene, l, id) {
1099
1190
  const it = l.items[id];
1100
1191
  const e = it.edge ? l.edges[it.edge] : void 0;
@@ -1106,22 +1197,21 @@ function reservedRect(scene, l, id) {
1106
1197
  const shift = it.zone === "end" ? len - keep2 : it.zone === "center" ? (len - keep2) / 2 : 0;
1107
1198
  return column ? rect(it.rect.x, it.rect.y + shift, it.rect.width, keep2) : rect(it.rect.x + shift, it.rect.y, keep2, it.rect.height);
1108
1199
  }
1109
- function placeKey(s, id) {
1110
- const it = Object.hasOwn(s.items, id) ? s.items[id] : void 0;
1111
- const byZone = (items) => ZONES.flatMap((z) => items.filter((x) => zoneOf(s.items[x]) === z));
1112
- return JSON.stringify({
1113
- edges: s.edges.map((e) => JSON.stringify([e.side, e.ring, e.column === true ? "" : e.id, byZone(e.items), e.size ?? null])).sort(),
1114
- floats: s.floats.map((f) => [f.id, f.rect, f.anchor ?? null, f.orient ?? null]),
1115
- from: it?.from ?? 0,
1116
- align: it?.align ?? "start"
1117
- });
1118
- }
1119
1200
  function slotAt(c, zone, line, id, place, lands, home = false) {
1120
1201
  const result = c.settle(use(moveTo(c.scene, c.dragged, place), c.dragged));
1121
1202
  const isHome = home || c.isHome(result);
1122
1203
  return { zone, line, id, place, result, home: isHome, ok: isHome || !!result.layout.items[c.dragged] && lands(result) && c.sound(result.layout) };
1123
1204
  }
1124
- var less = (spans, away) => away.reduce((acc, [ra, rb]) => acc.flatMap(([a, b]) => rb <= a || ra >= b ? [[a, b]] : [[a, Math.max(a, ra)], [Math.min(b, rb), b]].filter(([u, w]) => w - u > EPS)), [...spans]);
1205
+ function distinctSlots(c, slots) {
1206
+ const landing = (s) => s.result.layout.items[c.dragged]?.rect;
1207
+ const sameMove = (a, b) => {
1208
+ if (a.zone !== b.zone) return false;
1209
+ if (a.home && b.home) return true;
1210
+ const [p, q] = [landing(a), landing(b)];
1211
+ return Math.abs(a.line - b.line) < EPS && (!p || !q ? p === q : Math.abs(p.x - q.x) + Math.abs(p.y - q.y) + Math.abs(p.width - q.width) + Math.abs(p.height - q.height) < 0.01);
1212
+ };
1213
+ return slots.filter((s, i) => !slots.slice(0, i).some((o) => sameMove(o, s)));
1214
+ }
1125
1215
  function zoneSpans(scene, base, edge, reach, probe) {
1126
1216
  const el = base.edges[edge.id];
1127
1217
  const column = axisOf(edge.side) === "column";
@@ -1148,31 +1238,23 @@ function zoneSpans(scene, base, edge, reach, probe) {
1148
1238
  b: i === spans.length - 1 ? main0 + length + reach : (s[1] + spans[i + 1].s[0]) / 2
1149
1239
  }));
1150
1240
  }
1151
- function edgeSpec(c, edge) {
1152
- const { scene, rest, now, base, dragged, kind } = c;
1241
+ function edgeSlots(c, edge, accepts) {
1242
+ const { scene, rest, base, dragged, kind } = c;
1153
1243
  const el = base.edges[edge.id];
1154
- if (!el || el.folded || el.rides) return null;
1155
- const edgeKind = edgeKindOf(rest, edge);
1156
- const data = EDGE_KINDS[edgeKind];
1157
1244
  const column = axisOf(edge.side) === "column";
1158
- const at = (r2) => column ? r2.y : r2.x;
1159
- const len = (r2) => column ? r2.height : r2.width;
1245
+ const at = (r) => column ? r.y : r.x;
1246
+ const len = (r) => column ? r.height : r.width;
1160
1247
  const main0 = at(el.rect);
1161
1248
  const length = len(el.rect);
1162
- const empty = el.stack.items.every((p) => p.kind === "menu") && !el.menu;
1163
- const accepts = edge.accepts.includes(kind);
1164
- if (!accepts && (empty || area(el.rect) <= EPS)) return null;
1165
- if (accepts && empty && !opensEmpty(rest, edge, kind)) return null;
1166
- const ownNow = now.items[dragged];
1167
- const ownHere = ownNow && ownNow.edge === edge.id && !ownNow.floating && !ownNow.inMenu ? ownNow.rect : null;
1168
1249
  const restEdge = rest.edges.find((e) => e.id === edge.id);
1169
- const slotId = (zone, at2) => restEdge.items.length === 0 ? `strip:${edge.id}:${zone}` : `gap:${edge.id}:${zone}:${at2}`;
1250
+ const slotId = (zone, k) => restEdge.items.length === 0 ? `strip:${edge.id}:${zone}` : `gap:${edge.id}:${zone}:${k}`;
1251
+ const firstKind = formsOf(scene.items[dragged], axisOf(edge.side))[0]?.kind;
1170
1252
  const titles = edge.items.filter((id) => base.items[id]?.edge === edge.id && base.items[id].fixed && !!formsOf(scene.items[id], axisOf(edge.side))[base.items[id].form]?.main.grows);
1171
1253
  const lands = ({ layout: after }) => {
1172
1254
  const me = after.items[dragged];
1173
1255
  if (me.edge !== edge.id || me.inMenu) return false;
1174
1256
  if (titles.length === 0) return true;
1175
- if (me.form !== 0) return false;
1257
+ if (formsOf(scene.items[dragged], axisOf(edge.side))[me.form]?.kind !== firstKind) return false;
1176
1258
  return edge.items.every((id) => {
1177
1259
  const a = after.items[id];
1178
1260
  const b = base.items[id];
@@ -1213,6 +1295,24 @@ function edgeSpec(c, edge) {
1213
1295
  if (!movesFixed) slots.push(s);
1214
1296
  }
1215
1297
  }
1298
+ return slots;
1299
+ }
1300
+ function edgeSpec(c, edge) {
1301
+ const { scene, rest, now, base, dragged, kind } = c;
1302
+ const el = base.edges[edge.id];
1303
+ if (!el || el.folded || el.rides) return null;
1304
+ const edgeKind = edgeKindOf(rest, edge);
1305
+ const data = EDGE_KINDS[edgeKind];
1306
+ const column = axisOf(edge.side) === "column";
1307
+ const at = (r2) => column ? r2.y : r2.x;
1308
+ const len = (r2) => column ? r2.height : r2.width;
1309
+ const empty = el.stack.items.every((p) => p.kind === "menu") && !el.menu;
1310
+ const accepts = edge.accepts.includes(kind);
1311
+ if (!accepts && (empty || area(el.rect) <= EPS)) return null;
1312
+ if (accepts && empty && !opensEmpty(rest, edge, kind)) return null;
1313
+ const ownNow = now.items[dragged];
1314
+ const ownHere = ownNow && ownNow.edge === edge.id && !ownNow.floating && !ownNow.inMenu ? ownNow.rect : null;
1315
+ const slots = edgeSlots(c, edge, accepts);
1216
1316
  const spanOf = (r2) => [at(r2), at(r2) + len(r2)];
1217
1317
  const grows = (id) => !!formsOf(scene.items[id], axisOf(edge.side))[base.items[id].form]?.main.grows;
1218
1318
  const reserved = Object.entries(base.items).filter(([id, p]) => p.edge === edge.id && p.fixed && !p.inMenu && !grows(id)).map(([, p]) => spanOf(p.rect));
@@ -1234,38 +1334,217 @@ function edgeSpec(c, edge) {
1234
1334
  const e = s.result.layout.edges[edge.id];
1235
1335
  return column ? rect(e.rect.x, slot.y, e.cross, 0) : rect(slot.x, e.rect.y, 0, e.cross);
1236
1336
  };
1237
- const takes = (zone) => accepts && zoneTakes(edge, zone, kind);
1238
- return {
1239
- id: edge.id,
1240
- key: edge.id,
1241
- kind: edgeKind,
1242
- lay: c.lay.get(edge.id),
1243
- vertical: column,
1244
- zones,
1245
- slots,
1246
- refuse: (zone) => takes(zone) ? "no-room" : accepts || area(el.rect) > EPS ? "not-accepted" : null,
1247
- own: ownHere,
1248
- holds: !!ownNow && ownNow.edge === edge.id && !ownNow.floating,
1249
- reserved,
1250
- across,
1251
- owned: inward(drawn ? ownCross : Math.min(strip, data.place)),
1252
- ownedAlong: null,
1253
- drawn,
1254
- marker
1337
+ const takes = (zone) => accepts && zoneTakes(edge, zone, kind);
1338
+ return {
1339
+ id: edge.id,
1340
+ key: edge.id,
1341
+ kind: edgeKind,
1342
+ lay: c.lay.get(edge.id),
1343
+ vertical: column,
1344
+ zones,
1345
+ slots,
1346
+ refuse: (zone) => takes(zone) ? "no-room" : accepts || area(el.rect) > EPS ? "not-accepted" : null,
1347
+ own: ownHere,
1348
+ holds: !!ownNow && ownNow.edge === edge.id && !ownNow.floating,
1349
+ reserved,
1350
+ across,
1351
+ owned: inward(drawn ? ownCross : Math.min(strip, data.place)),
1352
+ ownedAlong: null,
1353
+ drawn,
1354
+ marker
1355
+ };
1356
+ }
1357
+
1358
+ // src/scene/targets/filter.ts
1359
+ function placeKey(s, id) {
1360
+ const it = Object.hasOwn(s.items, id) ? s.items[id] : void 0;
1361
+ const byZone = (items) => ZONES.flatMap((z) => items.filter((x) => zoneOf(s.items[x]) === z));
1362
+ return JSON.stringify({
1363
+ edges: s.edges.map((e) => JSON.stringify([e.side, e.ring, e.column === true ? "" : e.id, byZone(e.items), e.size ?? null])).sort(),
1364
+ floats: s.floats.map((f) => [f.id, f.rect, f.anchor ?? null, f.orient ?? null]),
1365
+ from: it?.from ?? 0,
1366
+ align: it?.align ?? "start"
1367
+ });
1368
+ }
1369
+ function soundness(scene, base, dragged) {
1370
+ const chrome = scene.chrome ?? [];
1371
+ const noteKey = (n) => "edge" in n && (n.code === "overflow" || n.code === "too-wide") ? `${n.edge}:${n.code}:${"id" in n ? n.id : ""}` : null;
1372
+ const hadNotes = new Set(base.notes.map(noteKey).filter((k) => k !== null));
1373
+ return (after) => {
1374
+ if (after.notes.some((n) => {
1375
+ const k = noteKey(n);
1376
+ return k !== null && !hadNotes.has(k);
1377
+ })) return false;
1378
+ for (const [id, b] of Object.entries(base.items)) {
1379
+ if (!b.fixed) continue;
1380
+ const a = after.items[id];
1381
+ const e = a?.edge ? after.edges[a.edge] : void 0;
1382
+ if (!a || !e || a.form !== b.form || a.formKind !== b.formKind) return false;
1383
+ const f = formsOf(scene.items[id], axisOf(e.side))[a.form];
1384
+ const len = axisOf(e.side) === "column" ? a.rect.height : a.rect.width;
1385
+ if (f && len < formMain(f, "min", e.cross) - 0.5) return false;
1386
+ }
1387
+ const me = after.items[dragged];
1388
+ if (!me) return true;
1389
+ const fixedAfter = Object.entries(after.items).filter(([id, it]) => it.fixed && id !== dragged).map(([id]) => reservedRect(scene, after, id));
1390
+ return !chrome.some((c) => overlaps(me.rect, c)) && !fixedAfter.some((f) => overlaps(me.rect, f));
1391
+ };
1392
+ }
1393
+ function context(scene, dragged, options) {
1394
+ const item = Object.hasOwn(scene.items, dragged) ? scene.items[dragged] : void 0;
1395
+ if (!item || item.fixed || item.closed) return null;
1396
+ const prev = { ...options.previous ? { previous: options.previous } : {}, ...options.fresh ? { fresh: options.fresh } : {} };
1397
+ const settle = (s) => ({ scene: s, layout: solve(s, prev) });
1398
+ const now = solve(scene, prev);
1399
+ const rest = lift(scene, dragged);
1400
+ const base = solve(rest, prev);
1401
+ const here = placeKey(scene, dragged);
1402
+ const c = {
1403
+ scene,
1404
+ rest,
1405
+ now,
1406
+ base,
1407
+ dragged,
1408
+ kind: item.kind,
1409
+ reach: Math.max(0, Number.isFinite(options.endReach) ? options.endReach : 0),
1410
+ settle,
1411
+ isHome: (r) => placeKey(r.scene, dragged) === here,
1412
+ sound: soundness(scene, base, dragged),
1413
+ lay: layKeys(scene)
1414
+ };
1415
+ return { c, item, prev };
1416
+ }
1417
+
1418
+ // src/scene/targets/model.ts
1419
+ var less = (spans, away) => away.reduce((acc, [ra, rb]) => acc.flatMap(([a, b]) => rb <= a || ra >= b ? [[a, b]] : [[a, Math.max(a, ra)], [Math.min(b, rb), b]].filter(([u, w]) => w - u > EPS)), [...spans]);
1420
+
1421
+ // src/scene/targets/ownership.ts
1422
+ var bandOf = (s) => {
1423
+ const a = Math.min(...s.zones.map((z) => z.a));
1424
+ const b = Math.max(...s.zones.map((z) => z.b));
1425
+ return s.vertical ? rect(s.owned[0], a, s.owned[1] - s.owned[0], b - a) : rect(a, s.owned[0], b - a, s.owned[1] - s.owned[0]);
1426
+ };
1427
+ var ownedRectOf = (s) => {
1428
+ const b = bandOf(s);
1429
+ if (!s.ownedAlong) return b;
1430
+ const [a0, a1] = s.ownedAlong;
1431
+ return s.vertical ? rect(b.x, a0, b.width, a1 - a0) : rect(a0, b.y, a1 - a0, b.height);
1432
+ };
1433
+ function ownedRectsOf(s, along) {
1434
+ if (along.length === 0) return [];
1435
+ const o = ownedRectOf(s);
1436
+ if (s.ownedAlong) return [o];
1437
+ return along.map(([a, b]) => s.vertical ? rect(o.x, a, o.width, b - a) : rect(a, o.y, b - a, o.height));
1438
+ }
1439
+ function seenOf(c) {
1440
+ const { now, base, dragged } = c;
1441
+ const ownEdge = now.items[dragged]?.edge;
1442
+ const lifted = Object.values(base.edges).filter((e) => area(e.rect) > EPS).map((e) => e.rect);
1443
+ return Object.entries(now.edges).filter(([id, e]) => id !== ownEdge && area(e.rect) > EPS && c.lay.has(id)).flatMap(([id, e]) => lifted.reduce((ps, r) => ps.flatMap((p) => subtract(p, r)), [e.rect]).map((r) => ({ edge: id, rect: r })));
1444
+ }
1445
+ function ownersOf(c, live, alongOf) {
1446
+ const { now, base, dragged } = c;
1447
+ const ownEdge = now.items[dragged]?.edge;
1448
+ const ownRect = now.items[dragged];
1449
+ const homeRect = (() => {
1450
+ if (!ownRect || ownRect.floating || !ownEdge) return null;
1451
+ const o = ownRect.rect;
1452
+ const s = live.find((x) => x.id === ownEdge);
1453
+ if (!s || ownRect.inMenu) return o;
1454
+ const [c0, c1] = s.across();
1455
+ return s.vertical ? rect(Math.min(c0, o.x), o.y, Math.max(c1, o.x + o.width) - Math.min(c0, o.x), o.height) : rect(o.x, Math.min(c0, o.y), o.width, Math.max(c1, o.y + o.height) - Math.min(c0, o.y));
1456
+ })();
1457
+ return [
1458
+ // Home first: its tile is its own, for every edge but its own (which tiles it as its own interval).
1459
+ ...homeRect && ownEdge ? [{ id: ownEdge, key: ownEdge, rect: homeRect, lay: { phase: -1, side: "", at: -Infinity }, place: false }] : [],
1460
+ // Every edge the tiler reads, along each zone with a slot the filter keeps: what it owns, drawn or a band of its
1461
+ // place (a column line's band about its line). A zone whose every slot is refused owns nothing against the others
1462
+ // (its drawn rect still counts, below).
1463
+ ...live.flatMap((s) => ownedRectsOf(s, alongOf(s)).map((r) => ({ id: s.id, key: s.key, rect: r, lay: s.lay, place: !s.drawn }))).filter((o) => area(o.rect) > EPS),
1464
+ // Every drawn rect with the item lifted, the layout the tiles lie in: the edges the tiler doesn't read (folded,
1465
+ // riding, taking nothing) among them.
1466
+ ...Object.entries(base.edges).filter(([id, e]) => area(e.rect) > EPS && c.lay.has(id)).map(([id, e]) => ({ id, key: id, rect: e.rect, lay: c.lay.get(id), place: false })),
1467
+ // And where each edge is seen, as it is now, and no edge lies once lifted (`seenOf`): a stretch is owned by what the
1468
+ // user sees there. An edge that moves when the item lifts never owns, where it lay before, a stretch another edge
1469
+ // lies in once lifted.
1470
+ ...seenOf(c).map(({ edge, rect: r }) => ({ id: edge, key: edge, rect: r, lay: c.lay.get(edge), place: false }))
1471
+ ];
1472
+ }
1473
+ function ownership(c, live, alongOf) {
1474
+ const { scene, rest, now, base, dragged } = c;
1475
+ const owners = ownersOf(c, live, alongOf);
1476
+ const wins = (o, s) => {
1477
+ if (o.key === s.key) return false;
1478
+ if (pushes(o.lay, s.lay)) return true;
1479
+ if (pushes(s.lay, o.lay)) return false;
1480
+ const place = !s.drawn;
1481
+ return o.place === place ? !place || o.lay.at < s.lay.at : !o.place;
1482
+ };
1483
+ const taken = new Map(
1484
+ live.map((s) => [
1485
+ s.key,
1486
+ [
1487
+ ...owners.filter((o) => wins(o, s)).map((o) => o.rect),
1488
+ ...EDGE_KINDS[s.kind].virtual ? owners.filter((o) => o.key !== s.key && !wins(o, s)).flatMap((o) => subtract(o.rect, ownedRectOf(s))) : []
1489
+ ]
1490
+ ])
1491
+ );
1492
+ const bodySpans = (id) => {
1493
+ const e = rest.edges.find((x) => x.id === id);
1494
+ const bandOpt = e?.hitBand ?? DEFAULT_HIT_BAND;
1495
+ if (!e || bandOpt === "middle") return [];
1496
+ const column = axisOf(e.side) === "column";
1497
+ const half = Math.max(0, Number.isFinite(bandOpt) ? bandOpt : DEFAULT_HIT_BAND) / 2;
1498
+ return Object.entries(base.items).filter(([iid, p]) => p.edge === id && p.formKind === "open" && iid !== dragged).map(([, p]) => column ? [p.rect.y + half, p.rect.y + p.rect.height - half] : [p.rect.x + half, p.rect.x + p.rect.width - half]).filter(([a, b]) => b - a > EPS);
1499
+ };
1500
+ const cutsOf = (s) => {
1501
+ const b = bandOf(s);
1502
+ const along = (r) => s.vertical ? [r.y, r.y + r.height] : [r.x, r.x + r.width];
1503
+ const across = (r) => s.vertical ? [r.x, r.x + r.width] : [r.y, r.y + r.height];
1504
+ const rs = taken.get(s.key).filter((r) => overlaps(r, b));
1505
+ const ends = [.../* @__PURE__ */ new Set([...along(b), ...rs.flatMap(along)])].filter((x) => x >= along(b)[0] && x <= along(b)[1]).sort((p, q) => p - q);
1506
+ const owned = [];
1507
+ for (let k = 1; k < ends.length; k++) {
1508
+ const [u, w] = [ends[k - 1], ends[k]];
1509
+ const over = rs.filter((r) => along(r)[0] <= u + EPS && along(r)[1] >= w - EPS).map(across);
1510
+ if (w - u > EPS && less([across(b)], over).reduce((n, [p, q]) => n + q - p, 0) < 1) owned.push([u, w]);
1511
+ }
1512
+ return [...owned, ...bodySpans(s.id)];
1513
+ };
1514
+ const grows = (l, id) => {
1515
+ const it = l.items[id];
1516
+ const e = it.edge ? l.edges[it.edge] : void 0;
1517
+ return !!e && !!formsOf(scene.items[id], axisOf(e.side))[it.form]?.main.grows;
1518
+ };
1519
+ const solid = [.../* @__PURE__ */ new Set([now, base])].flatMap((l) => Object.entries(l.items).filter(([id, it]) => it.fixed && !grows(l, id)).map(([, it]) => it.rect));
1520
+ const bodies = (id) => {
1521
+ const e = rest.edges.find((x) => x.id === id);
1522
+ if (!e) return [];
1523
+ const column = axisOf(e.side) === "column";
1524
+ const across = (a, b) => column ? rect(-1e9, a, 2e9, b - a) : rect(a, -1e9, b - a, 2e9);
1525
+ const reserved = Object.entries(base.items).filter(([iid, p]) => p.edge === id && p.fixed && !p.inMenu && !grows(base, iid)).map(([, p]) => column ? across(p.rect.y, p.rect.y + p.rect.height) : across(p.rect.x, p.rect.x + p.rect.width));
1526
+ return [...reserved, ...bodySpans(id).map(([a, b]) => across(a, b))].filter((b) => area(b) > EPS);
1255
1527
  };
1528
+ const drawn = [...Object.entries(base.edges).filter(([, e]) => area(e.rect) > EPS).map(([id, e]) => ({ key: id, rect: e.rect })), ...seenOf(c).map((x) => ({ key: x.edge, rect: x.rect }))];
1529
+ const drawnByKey = /* @__PURE__ */ new Map();
1530
+ const drawnBesides = (key) => {
1531
+ let rs = drawnByKey.get(key);
1532
+ if (!rs) drawnByKey.set(key, rs = drawn.filter((d) => d.key !== key).map((d) => d.rect));
1533
+ return rs;
1534
+ };
1535
+ return { owners, taken, cutsOf, bodies, solid, drawnBesides };
1256
1536
  }
1257
- function tileEdge(c, spec, cuts, lost) {
1537
+
1538
+ // src/scene/targets/tiles.ts
1539
+ function tilerOf(spec) {
1258
1540
  const data = EDGE_KINDS[spec.kind];
1259
1541
  const v = spec.vertical;
1260
- const own = spec.own ? v ? [spec.own.y, spec.own.y + spec.own.height] : [spec.own.x, spec.own.x + spec.own.width] : null;
1261
1542
  const band = (a, b, after) => {
1262
1543
  const [c0, c1] = spec.across(after);
1263
1544
  return v ? rect(c0, a, c1 - c0, b - a) : rect(a, c0, b - a, c1 - c0);
1264
1545
  };
1265
1546
  const keepAt = (line, h) => v ? { x: h.x + h.width / 2, y: line } : { x: line, y: h.y + h.height / 2 };
1266
1547
  const tile = (zone, hit, more) => ({ edge: spec.id, key: spec.key, edgeKind: spec.kind, vertical: v, zone, hit, ...more });
1267
- const refused = (zone, hit, reason) => tile(zone, hit, { kind: "refuse", id: "", order: data.layer, priority: data.priority, keep: null, reason });
1268
- const reserved = spec.reserved;
1269
1548
  const split = (t) => {
1270
1549
  const h = t.hit;
1271
1550
  const [h0, h1] = v ? [h.x, h.x + h.width] : [h.y, h.y + h.height];
@@ -1277,47 +1556,53 @@ function tileEdge(c, spec, cuts, lost) {
1277
1556
  const core = { ...t, hit: part(c0, c1), keep: t.keep && keepAt(v ? t.keep.y : t.keep.x, part(c0, c1)) };
1278
1557
  return [core, ...beyond.map(([a, b]) => ({ ...t, kind: "reach", of: core, order: data.overreach, hit: part(a, b), keep: null }))];
1279
1558
  };
1280
- const landing = (s) => s.result.layout.items[c.dragged]?.rect;
1281
- const sameMove = (a, b) => {
1282
- if (a.zone !== b.zone) return false;
1283
- if (a.home && b.home) return true;
1284
- const [p, q] = [landing(a), landing(b)];
1285
- return Math.abs(a.line - b.line) < EPS && (!p || !q ? p === q : Math.abs(p.x - q.x) + Math.abs(p.y - q.y) + Math.abs(p.width - q.width) + Math.abs(p.height - q.height) < 0.01);
1559
+ return { band, keepAt, tile, split };
1560
+ }
1561
+ function linesIn(spec, z, mine, pieces, [pa, pb], own) {
1562
+ const reserved = spec.reserved;
1563
+ const lines = mine.map((s) => s.line);
1564
+ const lo = Math.min(z.a, ...lines);
1565
+ const hi = Math.max(z.b, ...lines);
1566
+ const free = less([[z.a, z.b]], reserved);
1567
+ const freeLen = free.reduce((n, [a, b]) => n + (b - a), 0);
1568
+ const spread = (x) => {
1569
+ if (hi - lo <= EPS || freeLen <= EPS) return x;
1570
+ let d = (x - lo) / (hi - lo) * freeLen;
1571
+ for (const [a, b] of free) {
1572
+ if (d <= b - a + EPS) return a + Math.min(d, b - a);
1573
+ d -= b - a;
1574
+ }
1575
+ return free[free.length - 1][1];
1576
+ };
1577
+ const past = lines.some((x) => x < z.a - EPS || x > z.b + EPS);
1578
+ const nearer = (p, [a, b]) => p > a && p < b ? p - a < b - p ? a : b : p;
1579
+ const proj = (x) => {
1580
+ const q = reserved.reduce((r, s) => nearer(r, s), past ? spread(x) : x);
1581
+ const p = own ? nearer(q, own) : q;
1582
+ if (pieces.some(([a, b]) => p >= a - EPS && p <= b + EPS)) return p;
1583
+ return pieces.map(([a, b]) => Math.min(Math.max(p, a), b)).reduce((m, y) => Math.abs(y - p) < Math.abs(m - p) ? y : m);
1286
1584
  };
1287
- const slots = spec.slots.filter((s, i) => !spec.slots.slice(0, i).some((o) => sameMove(o, s)));
1585
+ const inPiece = mine.filter((s) => proj(s.line) >= pa - EPS && proj(s.line) <= pb + EPS).sort((a, b) => proj(a.line) - proj(b.line) || a.line - b.line);
1586
+ const projected = inPiece.map((s) => proj(s.line));
1587
+ const at = projected.some((p, i) => i > 0 && p - projected[i - 1] <= EPS) ? inPiece.map((_, i) => pa + (i + 0.5) * (pb - pa) / inPiece.length) : projected;
1588
+ return { inPiece, at };
1589
+ }
1590
+ function tileEdge(c, spec, cuts, lost) {
1591
+ const data = EDGE_KINDS[spec.kind];
1592
+ const v = spec.vertical;
1593
+ const own = spec.own ? v ? [spec.own.y, spec.own.y + spec.own.height] : [spec.own.x, spec.own.x + spec.own.width] : null;
1594
+ const { band, keepAt, tile, split } = tilerOf(spec);
1595
+ const refused = (zone, hit, reason) => tile(zone, hit, { kind: "refuse", id: "", order: data.layer, priority: data.priority, keep: null, reason });
1596
+ const slots = distinctSlots(c, spec.slots);
1288
1597
  const out = [];
1289
1598
  for (const z of spec.zones) {
1290
- const room = less([[z.a, z.b]], [...cuts, ...reserved]).filter(([a, b]) => b - a > EPS);
1599
+ const room = less([[z.a, z.b]], [...cuts, ...spec.reserved]).filter(([a, b]) => b - a > EPS);
1291
1600
  if (room.length === 0) continue;
1292
1601
  const pieces = own ? less(room, [own]) : room;
1293
1602
  const mine = slots.filter((s) => s.zone === z.zone && !(spec.holds && s.home) && pieces.some(([a, b]) => b > s.line - data.reach + EPS && a < s.line + data.reach - EPS));
1294
1603
  const reason = spec.refuse(z.zone);
1295
1604
  for (const [pa, pb] of pieces) {
1296
- const lines = mine.map((s) => s.line);
1297
- const lo = Math.min(z.a, ...lines);
1298
- const hi = Math.max(z.b, ...lines);
1299
- const free = less([[z.a, z.b]], reserved);
1300
- const freeLen = free.reduce((n, [a, b]) => n + (b - a), 0);
1301
- const spread = (x) => {
1302
- if (hi - lo <= EPS || freeLen <= EPS) return x;
1303
- let d = (x - lo) / (hi - lo) * freeLen;
1304
- for (const [a, b] of free) {
1305
- if (d <= b - a + EPS) return a + Math.min(d, b - a);
1306
- d -= b - a;
1307
- }
1308
- return free[free.length - 1][1];
1309
- };
1310
- const past = lines.some((x) => x < z.a - EPS || x > z.b + EPS);
1311
- const nearer = (p, [a, b]) => p > a && p < b ? p - a < b - p ? a : b : p;
1312
- const proj = (x) => {
1313
- const q = reserved.reduce((r, s) => nearer(r, s), past ? spread(x) : x);
1314
- const p = own ? nearer(q, own) : q;
1315
- if (pieces.some(([a, b]) => p >= a - EPS && p <= b + EPS)) return p;
1316
- return pieces.map(([a, b]) => Math.min(Math.max(p, a), b)).reduce((m, y) => Math.abs(y - p) < Math.abs(m - p) ? y : m);
1317
- };
1318
- const inPiece = mine.filter((s) => proj(s.line) >= pa - EPS && proj(s.line) <= pb + EPS).sort((a, b) => proj(a.line) - proj(b.line) || a.line - b.line);
1319
- const projected = inPiece.map((s) => proj(s.line));
1320
- const at = projected.some((p, i) => i > 0 && p - projected[i - 1] <= EPS) ? inPiece.map((_, i) => pa + (i + 0.5) * (pb - pa) / inPiece.length) : projected;
1605
+ const { inPiece, at } = linesIn(spec, z, mine, pieces, [pa, pb], own);
1321
1606
  if (inPiece.length === 0) {
1322
1607
  const lone = reason === "no-room" && mine.length === 0 && slots.some((s) => s.zone === z.zone && s.home);
1323
1608
  const near = mine.length === 0 || mine.some((s) => pb > s.line - data.reach + EPS && pa < s.line + data.reach - EPS);
@@ -1350,6 +1635,121 @@ function tileEdge(c, spec, cuts, lost) {
1350
1635
  }
1351
1636
  return out;
1352
1637
  }
1638
+ var clip = (r, cuts) => cuts.reduce((ps, cut) => ps.flatMap((p) => subtract(p, cut)), [r]);
1639
+ var coverOf = (t) => t.hits ?? [t.hit];
1640
+ var largest = (ps) => ps.reduce((a, p) => area(p) > area(a) ? p : a);
1641
+ var withPieces = (t, ps) => {
1642
+ const { hits: _h, ...rest } = t;
1643
+ return { ...rest, hit: largest(ps), ...ps.length > 1 ? { hits: ps } : {} };
1644
+ };
1645
+ function placeReach(t, pieces, placed, landed, gone) {
1646
+ const of = t.of;
1647
+ const at = landed.get(of) ?? [];
1648
+ if (at.length === 0 && gone.has(of)) {
1649
+ const line = t.vertical ? of.keep.y : of.keep.x;
1650
+ const whole = pieces.filter((p) => p.width >= 1 && p.height >= 1);
1651
+ const on = whole.filter((p) => t.vertical ? p.y <= line + EPS && line <= p.y + p.height + EPS : p.x <= line + EPS && line <= p.x + p.width + EPS);
1652
+ const kept = (on.length > 0 ? on : whole).reduce((a, p) => !a || area(p) > area(a) ? p : a, null);
1653
+ if (kept) {
1654
+ placed.push({ ...of, hit: kept });
1655
+ gone.delete(of);
1656
+ }
1657
+ return;
1658
+ }
1659
+ for (const i of at) {
1660
+ const cover = coverOf(placed[i]);
1661
+ for (let k = 0; k < cover.length; k++) {
1662
+ const h = cover[k];
1663
+ const joint = pieces.find(
1664
+ (p) => t.vertical ? p.y <= h.y + EPS && p.y + p.height >= h.y + h.height - EPS && (Math.abs(p.x - h.x - h.width) < EPS || Math.abs(p.x + p.width - h.x) < EPS) : p.x <= h.x + EPS && p.x + p.width >= h.x + h.width - EPS && (Math.abs(p.y - h.y - h.height) < EPS || Math.abs(p.y + p.height - h.y) < EPS)
1665
+ );
1666
+ if (!joint) continue;
1667
+ const merged = t.vertical ? rect(Math.min(h.x, joint.x), h.y, Math.max(h.x + h.width, joint.x + joint.width) - Math.min(h.x, joint.x), h.height) : rect(h.x, Math.min(h.y, joint.y), h.width, Math.max(h.y + h.height, joint.y + joint.height) - Math.min(h.y, joint.y));
1668
+ placed[i] = cover.length > 1 ? withPieces(placed[i], cover.map((r, j) => j === k ? merged : r)) : { ...placed[i], hit: merged };
1669
+ return;
1670
+ }
1671
+ }
1672
+ }
1673
+ var isRefused = (t) => t.kind === "refuse" || t.kind === "reach" && t.of.kind === "refuse";
1674
+ function clipTiles(tiles, own, chrome, lost) {
1675
+ const { owners, taken, bodies, solid } = own;
1676
+ const byKey = /* @__PURE__ */ new Map();
1677
+ const others = (t) => {
1678
+ let rs = byKey.get(t.key);
1679
+ if (!rs) byKey.set(t.key, rs = owners.filter((o) => o.key !== t.key).map((o) => o.rect));
1680
+ return rs;
1681
+ };
1682
+ const placed = [];
1683
+ const later = [];
1684
+ const refused = tiles.filter(isRefused);
1685
+ const landed = /* @__PURE__ */ new Map();
1686
+ const gone = /* @__PURE__ */ new Set();
1687
+ const place = (t) => {
1688
+ if (t.order === 0) {
1689
+ placed.push(t);
1690
+ return;
1691
+ }
1692
+ const data = EDGE_KINDS[t.edgeKind];
1693
+ if (data.virtual && t.kind === "refuse") {
1694
+ later.push(t);
1695
+ return;
1696
+ }
1697
+ if (t.kind === "reach") {
1698
+ placeReach(t, clip(t.hit, [...placed.flatMap(coverOf), ...others(t), ...bodies(t.edge), ...solid, ...chrome]), placed, landed, gone);
1699
+ return;
1700
+ }
1701
+ const yields = t.kind === "refuse" ? own.drawnBesides(t.key) : [];
1702
+ const cuts = [...placed.flatMap(coverOf), ...taken.get(t.key) ?? [], ...yields, ...data.virtual ? [] : bodies(t.edge), ...solid, ...chrome];
1703
+ const pieces = clip(t.hit, cuts);
1704
+ if (pieces.length === 0) {
1705
+ if (t.kind !== "refuse") gone.add(t);
1706
+ return;
1707
+ }
1708
+ if (t.kind === "refuse") {
1709
+ landed.set(t, pieces.map((_, k) => placed.length + k));
1710
+ for (const p of pieces) placed.push({ ...t, hit: p });
1711
+ return;
1712
+ }
1713
+ const solidPieces = pieces.filter((p) => p.width >= 1 && p.height >= 1);
1714
+ for (const p of pieces) if (!solidPieces.includes(p)) (data.virtual ? later : refused).push({ ...t, kind: "refuse", reason: "no-room", hit: p, keep: null });
1715
+ if (solidPieces.length === 0) return;
1716
+ landed.set(t, [placed.length]);
1717
+ placed.push(withPieces(t, solidPieces));
1718
+ };
1719
+ const byOrder = (a, b) => a.order - b.order;
1720
+ for (const t of tiles.filter((x) => !isRefused(x)).sort(byOrder)) place(t);
1721
+ for (const t of refused.sort(byOrder)) place(t);
1722
+ for (const t of gone) lost.push(t.id);
1723
+ for (const t of later) for (const p of clip(t.hit, [...placed.flatMap(coverOf), ...own.drawnBesides(t.key), ...solid, ...chrome])) placed.push({ ...t, hit: p });
1724
+ return placed;
1725
+ }
1726
+ function seenRefusals(c, live, placed, own, chrome) {
1727
+ const out = [];
1728
+ const covered = placed.flatMap(coverOf);
1729
+ for (const { edge, rect: r } of seenOf(c)) {
1730
+ const e = c.scene.edges.find((x) => x.id === edge);
1731
+ if (!e) continue;
1732
+ const vertical = axisOf(e.side) === "column";
1733
+ const band = e.hitBand ?? DEFAULT_HIT_BAND;
1734
+ const half = band === "middle" ? 0 : Math.max(0, Number.isFinite(band) ? band : DEFAULT_HIT_BAND) / 2;
1735
+ const bodies = band === "middle" ? [] : Object.entries(c.now.items).filter(([id, p]) => p.edge === edge && p.formKind === "open" && id !== c.dragged).map(([, p]) => vertical ? rect(p.rect.x, p.rect.y + half, p.rect.width, p.rect.height - 2 * half) : rect(p.rect.x + half, p.rect.y, p.rect.width - 2 * half, p.rect.height)).filter((b) => b.width > EPS && b.height > EPS);
1736
+ const spec = live.find((s) => s.key === edge);
1737
+ const zones = spec?.zones ?? [{ zone: "start", a: -Infinity, b: Infinity }];
1738
+ const kind = edgeKindOf(c.rest, e);
1739
+ for (const p of clip(r, [...covered, ...bodies, ...own.solid, ...chrome]))
1740
+ for (const z of zones) {
1741
+ const [p0, p1] = vertical ? [p.y, p.y + p.height] : [p.x, p.x + p.width];
1742
+ const [a, b] = [Math.max(p0, z.a), Math.min(p1, z.b)];
1743
+ if (b - a <= EPS) continue;
1744
+ const hit = vertical ? rect(p.x, a, p.width, b - a) : rect(a, p.y, b - a, p.height);
1745
+ const reason = spec?.refuse(z.zone) ?? (zoneTakes(e, z.zone, c.kind) ? "no-room" : "not-accepted");
1746
+ out.push({ edge, key: edge, edgeKind: kind, vertical, zone: z.zone, order: 99, kind: "refuse", id: "", priority: EDGE_KINDS[kind].priority, hit, keep: null, reason });
1747
+ }
1748
+ }
1749
+ return out;
1750
+ }
1751
+
1752
+ // src/scene/targets/virtual.ts
1353
1753
  function columnLines(c, side) {
1354
1754
  const { rest, base, dragged } = c;
1355
1755
  const cols = rest.edges.filter((e) => e.side === side && e.column === true).map((edge) => ({ edge, l: base.edges[edge.id] })).filter((x) => x.l && !x.l.folded && !x.l.rides && x.l.cross > 0).sort((a, b) => a.edge.ring - b.edge.ring).map((x) => ({ edge: x.edge, r: x.l.rect }));
@@ -1439,196 +1839,21 @@ function railStops(c, side) {
1439
1839
  marker: (s) => rect(at.x, s.result.layout.items[dragged].rect.y, at.width, 0)
1440
1840
  };
1441
1841
  }
1442
- function generate(scene, dragged, options) {
1443
- const item = Object.hasOwn(scene.items, dragged) ? scene.items[dragged] : void 0;
1444
- if (!item || item.fixed || item.closed) return { targets: [], refusals: [], lost: [] };
1445
- const prev = { ...options.previous ? { previous: options.previous } : {}, ...options.fresh ? { fresh: options.fresh } : {} };
1446
- const settle = (s) => ({ scene: s, layout: solve(s, prev) });
1447
- const now = solve(scene, prev);
1448
- const rest = lift(scene, dragged);
1449
- const base = solve(rest, prev);
1450
- const chrome = scene.chrome ?? [];
1451
- const here = placeKey(scene, dragged);
1452
- const noteKey = (n) => "edge" in n && (n.code === "overflow" || n.code === "too-wide") ? `${n.edge}:${n.code}:${"id" in n ? n.id : ""}` : null;
1453
- const hadNotes = new Set(base.notes.map(noteKey).filter((k) => k !== null));
1454
- const sound = (after) => {
1455
- if (after.notes.some((n) => {
1456
- const k = noteKey(n);
1457
- return k !== null && !hadNotes.has(k);
1458
- })) return false;
1459
- for (const [id, b] of Object.entries(base.items)) {
1460
- if (!b.fixed) continue;
1461
- const a = after.items[id];
1462
- const e = a?.edge ? after.edges[a.edge] : void 0;
1463
- if (!a || !e || a.form !== b.form || a.formKind !== b.formKind) return false;
1464
- const f = formsOf(scene.items[id], axisOf(e.side))[a.form];
1465
- const len = axisOf(e.side) === "column" ? a.rect.height : a.rect.width;
1466
- if (f && len < formMain(f, "min", e.cross) - 0.5) return false;
1467
- }
1468
- const me = after.items[dragged];
1469
- if (!me) return true;
1470
- const fixedAfter = Object.entries(after.items).filter(([id, it]) => it.fixed && id !== dragged).map(([id]) => reservedRect(scene, after, id));
1471
- return !chrome.some((c2) => overlaps(me.rect, c2)) && !fixedAfter.some((f) => overlaps(me.rect, f));
1472
- };
1473
- const c = { scene, rest, now, base, dragged, kind: item.kind, reach: Math.max(0, Number.isFinite(options.endReach) ? options.endReach : 0), settle, isHome: (r) => placeKey(r.scene, dragged) === here, sound, lay: layKeys(scene) };
1842
+
1843
+ // src/scene/targets.ts
1844
+ function specsOf(c, kind) {
1845
+ const { scene, rest } = c;
1474
1846
  const specs = rest.edges.map((edge) => edgeSpec(c, edge));
1475
1847
  for (const side of ["left", "right"]) {
1476
1848
  const template = scene.columns?.[side];
1477
- if (!template || !zoneTakes(template, "start", item.kind)) continue;
1849
+ if (!template || !zoneTakes(template, "start", kind)) continue;
1478
1850
  specs.push(...columnLines(c, side));
1479
- if (scene.rails?.[side]?.accepts.includes(item.kind)) specs.push(railStops(c, side));
1480
- }
1481
- const ownEdge = now.items[dragged]?.edge;
1482
- const live = specs.filter((s) => s !== null);
1483
- const bandOf = (s) => {
1484
- const a = Math.min(...s.zones.map((z) => z.a));
1485
- const b = Math.max(...s.zones.map((z) => z.b));
1486
- return s.vertical ? rect(s.owned[0], a, s.owned[1] - s.owned[0], b - a) : rect(a, s.owned[0], b - a, s.owned[1] - s.owned[0]);
1487
- };
1488
- const ownedRectOf = (s) => {
1489
- const b = bandOf(s);
1490
- if (!s.ownedAlong) return b;
1491
- const [a0, a1] = s.ownedAlong;
1492
- return s.vertical ? rect(b.x, a0, b.width, a1 - a0) : rect(a0, b.y, a1 - a0, b.height);
1493
- };
1494
- const ownRect = now.items[dragged];
1495
- const homeRect = (() => {
1496
- if (!ownRect || ownRect.floating || !ownEdge) return null;
1497
- const o = ownRect.rect;
1498
- const s = live.find((x) => x.id === ownEdge);
1499
- if (!s || ownRect.inMenu) return o;
1500
- const [c0, c1] = s.across();
1501
- return s.vertical ? rect(Math.min(c0, o.x), o.y, Math.max(c1, o.x + o.width) - Math.min(c0, o.x), o.height) : rect(o.x, Math.min(c0, o.y), o.width, Math.max(c1, o.y + o.height) - Math.min(c0, o.y));
1502
- })();
1503
- const owners = [
1504
- // Home first: its tile is its own, for every edge but its own (which tiles it as its own interval).
1505
- ...homeRect && ownEdge ? [{ id: ownEdge, key: ownEdge, rect: homeRect, lay: { phase: -1, side: "", at: -Infinity }, place: false }] : [],
1506
- // Every edge the tiler reads: what it owns, drawn or a band of its place (a column line's band about its line).
1507
- ...live.map((s) => ({ id: s.id, key: s.key, rect: ownedRectOf(s), lay: s.lay, place: !s.drawn })).filter((o) => area(o.rect) > EPS),
1508
- // Every drawn rect, with the item lifted and as it is now (the item's own edge only lifted): the edges the tiler
1509
- // doesn't read (folded, riding, taking nothing) and the ones that move when the item lifts.
1510
- ...[...Object.entries(base.edges), ...Object.entries(now.edges).filter(([id]) => id !== ownEdge)].filter(([id, e]) => area(e.rect) > EPS && c.lay.has(id)).map(([id, e]) => ({ id, key: id, rect: e.rect, lay: c.lay.get(id), place: false }))
1511
- ];
1512
- const wins = (o, s) => {
1513
- if (o.key === s.key) return false;
1514
- if (pushes(o.lay, s.lay)) return true;
1515
- if (pushes(s.lay, o.lay)) return false;
1516
- const place = !s.drawn;
1517
- return o.place === place ? !place || o.lay.at < s.lay.at : !o.place;
1518
- };
1519
- const taken = new Map(
1520
- live.map((s) => [
1521
- s.key,
1522
- [
1523
- ...owners.filter((o) => wins(o, s)).map((o) => o.rect),
1524
- ...EDGE_KINDS[s.kind].virtual ? owners.filter((o) => o.key !== s.key && !wins(o, s)).flatMap((o) => subtract(o.rect, ownedRectOf(s))) : []
1525
- ]
1526
- ])
1527
- );
1528
- const bodySpans = (id) => {
1529
- const e = rest.edges.find((x) => x.id === id);
1530
- const bandOpt = e?.hitBand ?? DEFAULT_HIT_BAND;
1531
- if (!e || bandOpt === "middle") return [];
1532
- const column = axisOf(e.side) === "column";
1533
- const half = Math.max(0, Number.isFinite(bandOpt) ? bandOpt : DEFAULT_HIT_BAND) / 2;
1534
- return Object.entries(base.items).filter(([iid, p]) => p.edge === id && p.formKind === "open" && iid !== dragged).map(([, p]) => column ? [p.rect.y + half, p.rect.y + p.rect.height - half] : [p.rect.x + half, p.rect.x + p.rect.width - half]).filter(([a, b]) => b - a > EPS);
1535
- };
1536
- const cutsOf = (s) => {
1537
- const b = bandOf(s);
1538
- const along = (r) => s.vertical ? [r.y, r.y + r.height] : [r.x, r.x + r.width];
1539
- const across = (r) => s.vertical ? [r.x, r.x + r.width] : [r.y, r.y + r.height];
1540
- const rs = taken.get(s.key).filter((r) => overlaps(r, b));
1541
- const ends = [.../* @__PURE__ */ new Set([...along(b), ...rs.flatMap(along)])].filter((x) => x >= along(b)[0] && x <= along(b)[1]).sort((p, q) => p - q);
1542
- const owned = [];
1543
- for (let k = 1; k < ends.length; k++) {
1544
- const [u, w] = [ends[k - 1], ends[k]];
1545
- const over = rs.filter((r) => along(r)[0] <= u + EPS && along(r)[1] >= w - EPS).map(across);
1546
- if (w - u > EPS && less([across(b)], over).length === 0) owned.push([u, w]);
1547
- }
1548
- return [...owned, ...bodySpans(s.id)];
1549
- };
1550
- const lost = [];
1551
- const tiles = live.flatMap((spec) => tileEdge(c, spec, cutsOf(spec), lost));
1552
- const homeEdge = scene.edges.find((e) => e.id === ownRect?.edge);
1553
- if (ownRect && homeEdge && !ownRect.floating && !tiles.some((t) => t.order === 0))
1554
- tiles.push({ edge: homeEdge.id, key: homeEdge.id, edgeKind: edgeKindOf(scene, homeEdge), vertical: axisOf(homeEdge.side) === "column", zone: zoneOf(item), order: 0, kind: "home", id: `home:${dragged}`, priority: 1, place: { kind: "home" }, hit: ownRect.rect, keep: null });
1555
- const grows = (l, id) => {
1556
- const it = l.items[id];
1557
- const e = it.edge ? l.edges[it.edge] : void 0;
1558
- return !!e && !!formsOf(scene.items[id], axisOf(e.side))[it.form]?.main.grows;
1559
- };
1560
- const solid = [.../* @__PURE__ */ new Set([now, base])].flatMap((l) => Object.entries(l.items).filter(([id, it]) => it.fixed && !grows(l, id)).map(([, it]) => it.rect));
1561
- const bodies = (id) => {
1562
- const e = rest.edges.find((x) => x.id === id);
1563
- if (!e) return [];
1564
- const column = axisOf(e.side) === "column";
1565
- const across = (a, b) => column ? rect(-1e9, a, 2e9, b - a) : rect(a, -1e9, b - a, 2e9);
1566
- const reserved = Object.entries(base.items).filter(([iid, p]) => p.edge === id && p.fixed && !p.inMenu && !grows(base, iid)).map(([, p]) => column ? across(p.rect.y, p.rect.y + p.rect.height) : across(p.rect.x, p.rect.x + p.rect.width));
1567
- return [...reserved, ...bodySpans(id).map(([a, b]) => across(a, b))].filter((b) => area(b) > EPS);
1568
- };
1569
- const placed = [];
1570
- const later = [];
1571
- const clip = (r, cuts) => cuts.reduce((ps, cut) => ps.flatMap((p) => subtract(p, cut)), [r]);
1572
- const landed = /* @__PURE__ */ new Map();
1573
- const gone = /* @__PURE__ */ new Set();
1574
- for (const t of [...tiles].sort((a, b) => a.order - b.order)) {
1575
- if (t.order === 0) {
1576
- placed.push(t);
1577
- continue;
1578
- }
1579
- const data = EDGE_KINDS[t.edgeKind];
1580
- if (data.virtual && t.kind === "refuse") {
1581
- later.push(t);
1582
- continue;
1583
- }
1584
- if (t.kind === "reach") {
1585
- const of = t.of;
1586
- const pieces2 = clip(t.hit, [...placed.map((p) => p.hit), ...owners.filter((o) => o.key !== t.key).map((o) => o.rect), ...bodies(t.edge), ...solid, ...chrome]);
1587
- const at = landed.get(of) ?? [];
1588
- if (at.length === 0 && gone.has(of)) {
1589
- const line = t.vertical ? of.keep.y : of.keep.x;
1590
- const on = pieces2.filter((p) => t.vertical ? p.y <= line + EPS && line <= p.y + p.height + EPS : p.x <= line + EPS && line <= p.x + p.width + EPS);
1591
- const kept2 = (on.length > 0 ? on : pieces2).reduce((a, p) => !a || area(p) > area(a) ? p : a, null);
1592
- if (kept2) {
1593
- placed.push({ ...of, hit: kept2 });
1594
- gone.delete(of);
1595
- }
1596
- continue;
1597
- }
1598
- for (const i of at) {
1599
- const h = placed[i].hit;
1600
- const joint = pieces2.find(
1601
- (p) => t.vertical ? p.y <= h.y + EPS && p.y + p.height >= h.y + h.height - EPS && (Math.abs(p.x - h.x - h.width) < EPS || Math.abs(p.x + p.width - h.x) < EPS) : p.x <= h.x + EPS && p.x + p.width >= h.x + h.width - EPS && (Math.abs(p.y - h.y - h.height) < EPS || Math.abs(p.y + p.height - h.y) < EPS)
1602
- );
1603
- if (!joint) continue;
1604
- const merged = t.vertical ? rect(Math.min(h.x, joint.x), h.y, Math.max(h.x + h.width, joint.x + joint.width) - Math.min(h.x, joint.x), h.height) : rect(h.x, Math.min(h.y, joint.y), h.width, Math.max(h.y + h.height, joint.y + joint.height) - Math.min(h.y, joint.y));
1605
- placed[i] = { ...placed[i], hit: merged };
1606
- break;
1607
- }
1608
- continue;
1609
- }
1610
- const cuts = [...placed.map((p) => p.hit), ...taken.get(t.key) ?? [], ...data.virtual ? [] : bodies(t.edge), ...solid, ...chrome];
1611
- const pieces = clip(t.hit, cuts);
1612
- if (pieces.length === 0) {
1613
- if (t.kind !== "refuse") gone.add(t);
1614
- continue;
1615
- }
1616
- if (t.kind === "refuse") {
1617
- landed.set(t, pieces.map((_, k) => placed.length + k));
1618
- for (const p of pieces) placed.push({ ...t, hit: p });
1619
- continue;
1620
- }
1621
- const held = t.keep ? pieces.filter((p) => contains(p, t.keep)) : [];
1622
- const kept = (held.length > 0 ? held : pieces).reduce((a, p) => area(p) > area(a) ? p : a);
1623
- const sliver = (t.vertical ? kept.height : kept.width) < 1;
1624
- if (!sliver) {
1625
- landed.set(t, [placed.length]);
1626
- placed.push({ ...t, hit: kept });
1627
- }
1628
- for (const p of pieces) if (sliver || p !== kept) (data.virtual ? later : placed).push({ ...t, kind: "refuse", reason: "no-room", hit: p, keep: null });
1851
+ if (scene.rails?.[side]?.accepts.includes(kind)) specs.push(railStops(c, side));
1629
1852
  }
1630
- for (const t of gone) lost.push(t.id);
1631
- for (const t of later) for (const p of clip(t.hit, [...placed.map((q) => q.hit), ...solid, ...chrome])) placed.push({ ...t, hit: p });
1853
+ return specs.filter((s) => s !== null);
1854
+ }
1855
+ function emit2(c, placed) {
1856
+ const { scene, now, dragged } = c;
1632
1857
  const targets2 = [];
1633
1858
  const refused = [];
1634
1859
  const homeAfter = () => ({ scene, layout: now });
@@ -1648,12 +1873,42 @@ function generate(scene, dragged, options) {
1648
1873
  if (t.kind === "home") {
1649
1874
  if (t.order === 0) ownHome = t.hit;
1650
1875
  if (t.id === `home:${dragged}`) continue;
1651
- targets2.push({ id: t.id, priority: t.priority, hit: t.hit, item: dragged, place: { kind: "home" }, after: homeAfter });
1876
+ targets2.push({ id: t.id, priority: t.priority, hit: t.hit, ...t.hits ? { hits: t.hits } : {}, item: dragged, place: { kind: "home" }, after: homeAfter });
1652
1877
  continue;
1653
1878
  }
1654
1879
  const result = t.result;
1655
- targets2.push({ id: t.id, priority: t.priority, hit: t.hit, ...t.slot ? { slot: t.slot } : {}, ...t.marker ? { marker: t.marker } : {}, item: dragged, place: t.place, after: () => result });
1880
+ targets2.push({ id: t.id, priority: t.priority, hit: t.hit, ...t.hits ? { hits: t.hits } : {}, ...t.slot ? { slot: t.slot } : {}, ...t.marker ? { marker: t.marker } : {}, item: dragged, place: t.place, after: () => result });
1656
1881
  }
1882
+ return { targets: targets2, refused, ownHome };
1883
+ }
1884
+ function alongOfSlots(s, tiles) {
1885
+ const spans = tiles.filter((t) => t.kind === "slot").map((t) => s.vertical ? [t.hit.y, t.hit.y + t.hit.height] : [t.hit.x, t.hit.x + t.hit.width]).sort((a, b) => a[0] - b[0]);
1886
+ const out = [];
1887
+ for (const [a, b] of spans) {
1888
+ const last = out[out.length - 1];
1889
+ if (last && a <= last[1] + 1e-6) last[1] = Math.max(last[1], b);
1890
+ else out.push([a, b]);
1891
+ }
1892
+ return out;
1893
+ }
1894
+ function generate(scene, dragged, options) {
1895
+ const ctx = context(scene, dragged, options);
1896
+ if (!ctx) return { targets: [], refusals: [], lost: [] };
1897
+ const { c, item, prev } = ctx;
1898
+ const { now } = c;
1899
+ const live = specsOf(c, item.kind);
1900
+ const along = new Map(live.map((s) => [s.key, alongOfSlots(s, tileEdge(c, s, [], []))]));
1901
+ const own = ownership(c, live, (s) => along.get(s.key));
1902
+ const lost = [];
1903
+ const tiles = live.flatMap((spec) => tileEdge(c, spec, own.cutsOf(spec), lost));
1904
+ const ownRect = now.items[dragged];
1905
+ const homeEdge = scene.edges.find((e) => e.id === ownRect?.edge);
1906
+ if (ownRect && homeEdge && !ownRect.floating && !tiles.some((t) => t.order === 0))
1907
+ tiles.push({ edge: homeEdge.id, key: homeEdge.id, edgeKind: edgeKindOf(scene, homeEdge), vertical: axisOf(homeEdge.side) === "column", zone: zoneOf(item), order: 0, kind: "home", id: `home:${dragged}`, priority: 1, place: { kind: "home" }, hit: ownRect.rect, keep: null });
1908
+ const placed = clipTiles(tiles, own, scene.chrome ?? [], lost);
1909
+ placed.push(...seenRefusals(c, live, placed, own, scene.chrome ?? []));
1910
+ const { targets: targets2, refused, ownHome } = emit2(c, placed);
1911
+ const homeAfter = () => ({ scene, layout: now });
1657
1912
  if (scene.floatKinds.includes(item.kind)) {
1658
1913
  const was = scene.floats.find((f) => f.id === dragged);
1659
1914
  const fo = options.floatOrient ?? was?.orient ?? floatOrientOf(item);
@@ -1670,15 +1925,14 @@ function generate(scene, dragged, options) {
1670
1925
  const raw = rect(point.x - g.x, point.y - g.y, size.width, size.height);
1671
1926
  const first = solve(use(floatAt(scene, dragged, raw, options.floatOrient), dragged), prev);
1672
1927
  const at = first.items[dragged]?.rect ?? raw;
1673
- return settle(use(floatAt(scene, dragged, rect(at.x, at.y, size.width, size.height), options.floatOrient), dragged));
1928
+ return c.settle(use(floatAt(scene, dragged, rect(at.x, at.y, size.width, size.height), options.floatOrient), dragged));
1674
1929
  }
1675
1930
  });
1676
1931
  }
1677
1932
  (scene.blocked ?? []).forEach((b, i) => {
1678
1933
  targets2.push({ id: `none:${i}`, priority: 1, hit: rect(b.x, b.y, b.width, b.height), item: dragged, place: { kind: "none" }, after: homeAfter });
1679
1934
  });
1680
- const own = now.items[dragged];
1681
- if (own && !own.floating) targets2.push({ id: `home:${dragged}`, priority: 1, hit: ownHome && !targets2.some((t) => t.place.kind === "home") ? ownHome : own.rect, item: dragged, place: { kind: "home" }, after: homeAfter });
1935
+ if (ownRect && !ownRect.floating) targets2.push({ id: `home:${dragged}`, priority: 1, hit: ownHome && !targets2.some((t) => t.place.kind === "home") ? ownHome : ownRect.rect, item: dragged, place: { kind: "home" }, after: homeAfter });
1682
1936
  return { targets: targets2, refusals: refused, lost };
1683
1937
  }
1684
1938
  function targets(scene, dragged, options = {}) {
@@ -1711,11 +1965,11 @@ function stops(scene, dragged, options = {}) {
1711
1965
  export {
1712
1966
  SIDES,
1713
1967
  ZONES,
1714
- zoneTakes,
1715
- zonesTaking,
1716
1968
  DEFAULT_STRIP,
1717
1969
  DEFAULT_HIT_BAND,
1718
1970
  DEFAULT_FLOAT_SIZE,
1971
+ zoneTakes,
1972
+ zonesTaking,
1719
1973
  isOutsideSideBar,
1720
1974
  isSideBar,
1721
1975
  floatOrientOf,
@@ -1742,4 +1996,4 @@ export {
1742
1996
  refusals,
1743
1997
  stops
1744
1998
  };
1745
- //# sourceMappingURL=chunk-SZKU7CRX.js.map
1999
+ //# sourceMappingURL=chunk-CIGRI35P.js.map