@n-uf/placement 26.11.53 → 26.11.54

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.
Files changed (62) hide show
  1. package/CHANGELOG.md +75 -1
  2. package/README.md +27 -58
  3. package/dist/{chunk-VLTYJHUA.js → chunk-2UVUFEBP.js} +4 -8
  4. package/dist/{chunk-VLTYJHUA.js.map → chunk-2UVUFEBP.js.map} +1 -1
  5. package/dist/{geometry-Bf37LbCT.d.ts → geometry-L5gCoiZV.d.ts} +2 -8
  6. package/dist/index.d.ts +64 -42
  7. package/dist/index.js +6 -66
  8. package/dist/index.js.map +1 -1
  9. package/dist/popup.d.ts +49 -0
  10. package/dist/popup.js +58 -0
  11. package/dist/popup.js.map +1 -0
  12. package/dist/stack.d.ts +529 -228
  13. package/dist/stack.js +701 -28
  14. package/dist/stack.js.map +1 -1
  15. package/dist/tiling.d.ts +48 -5
  16. package/dist/tiling.js +198 -19
  17. package/dist/tiling.js.map +1 -1
  18. package/package.json +9 -36
  19. package/dist/chunk-2GGJ4GVR.js +0 -9
  20. package/dist/chunk-2GGJ4GVR.js.map +0 -1
  21. package/dist/chunk-3ZICYLWL.js +0 -791
  22. package/dist/chunk-3ZICYLWL.js.map +0 -1
  23. package/dist/chunk-AE3BWTAE.js +0 -838
  24. package/dist/chunk-AE3BWTAE.js.map +0 -1
  25. package/dist/chunk-JVAR67OL.js +0 -172
  26. package/dist/chunk-JVAR67OL.js.map +0 -1
  27. package/dist/chunk-NF7RYK3U.js +0 -429
  28. package/dist/chunk-NF7RYK3U.js.map +0 -1
  29. package/dist/chunk-R3AAANRY.js +0 -220
  30. package/dist/chunk-R3AAANRY.js.map +0 -1
  31. package/dist/chunk-TWI644JK.js +0 -221
  32. package/dist/chunk-TWI644JK.js.map +0 -1
  33. package/dist/chunk-ZTH5FCUO.js +0 -4190
  34. package/dist/chunk-ZTH5FCUO.js.map +0 -1
  35. package/dist/constructions-Cbz7uEAZ.d.ts +0 -251
  36. package/dist/desk.d.ts +0 -51
  37. package/dist/desk.js +0 -111
  38. package/dist/desk.js.map +0 -1
  39. package/dist/dock.d.ts +0 -25
  40. package/dist/dock.js +0 -397
  41. package/dist/dock.js.map +0 -1
  42. package/dist/float-fit-DCYxRMru.d.ts +0 -37
  43. package/dist/lanes-DIwUqchd.d.ts +0 -1321
  44. package/dist/model-X7eUaKlA.d.ts +0 -337
  45. package/dist/panels.d.ts +0 -60
  46. package/dist/panels.js +0 -99
  47. package/dist/panels.js.map +0 -1
  48. package/dist/regions-Db_RyT4P.d.ts +0 -48
  49. package/dist/scene-cases.d.ts +0 -888
  50. package/dist/scene-cases.js +0 -3149
  51. package/dist/scene-cases.js.map +0 -1
  52. package/dist/scene.d.ts +0 -290
  53. package/dist/scene.js +0 -128
  54. package/dist/scene.js.map +0 -1
  55. package/dist/target-BBOCeez2.d.ts +0 -69
  56. package/dist/targets-C9ZVhmCl.d.ts +0 -621
  57. package/dist/targets.d.ts +0 -212
  58. package/dist/targets.js +0 -447
  59. package/dist/targets.js.map +0 -1
  60. package/dist/toolbar.d.ts +0 -167
  61. package/dist/toolbar.js +0 -55
  62. package/dist/toolbar.js.map +0 -1
@@ -1,4190 +0,0 @@
1
- import {
2
- fitFloat,
3
- fitStack,
4
- formCrossMin,
5
- formMain,
6
- formSetsCross,
7
- wrapLines
8
- } from "./chunk-AE3BWTAE.js";
9
- import {
10
- COLUMN_REACH,
11
- EPS,
12
- PLANE,
13
- RAIL_EDGE,
14
- area,
15
- clamp,
16
- clampInto,
17
- overlaps,
18
- rect,
19
- subtract
20
- } from "./chunk-VLTYJHUA.js";
21
-
22
- // src/scene/model.ts
23
- var SIDES = ["top", "right", "bottom", "left"];
24
- var ZONES = ["start", "center", "end"];
25
- var FLOAT_ANCHORS = ["content-top-left", "content-top", "content-top-right", "content-right", "content-bottom-right", "content-bottom", "content-bottom-left", "content-left"];
26
- var RAIL_MODES = ["titles", "icons"];
27
- var RAIL_VISIBILITIES = ["whileDragging", "always", "never"];
28
- var RAIL_STRIP_MIN = 24;
29
- var DEFAULT_RAIL_MAGNET = 6;
30
- var EDGE_ROOM_HOLD = 0;
31
- var DEFAULT_STRIP = 24;
32
- var DEFAULT_HIT_BAND = 24;
33
- var DEFAULT_FLOAT_SIZE = { width: 280, height: 360 };
34
-
35
- // src/scene/helpers.ts
36
- var insetPx = (v) => Number.isFinite(v) ? Math.max(0, v) : 0;
37
- function floatRegion(scene, lifted) {
38
- const floating = new Set(scene.floats.map((f) => f.id));
39
- const docked = scene.edges.some((e) => (e.side === "left" || e.side === "right") && e.accepts.length > 0 && e.items.some((id) => id !== lifted && !floating.has(id) && !!scene.items[id] && isDockingPanel(scene.items[id])));
40
- const inset = scene.insetWhen === "docked" && !docked ? {} : scene.inset ?? {};
41
- const { stage } = scene;
42
- return { x: stage.x + insetPx(inset.left), y: stage.y, width: Math.max(0, stage.width - insetPx(inset.left) - insetPx(inset.right)), height: stage.height };
43
- }
44
- var heldRect = (scene, id, size, pointer, grab) => clampInto({ x: pointer.x - grab.x, y: pointer.y - grab.y, width: size.width, height: size.height }, floatRegion(scene, id));
45
- var anchorName = (anchor) => typeof anchor === "string" ? anchor : anchor.at;
46
- var isSince = (x) => typeof x === "number" && x >= 0 && x <= Number.MAX_SAFE_INTEGER;
47
- var zonesOf = (edge) => {
48
- if (edge.zones === void 0) return ZONES;
49
- const has = ZONES.filter((z) => edge.zones.includes(z));
50
- return has.length > 0 ? has : ZONES;
51
- };
52
- var zoneTakes = (edge, zone, kind) => edge.accepts.includes(kind) && (edge.zones === void 0 || zonesOf(edge).includes(zone)) && (edge.zoneAccepts === void 0 || !!edge.zoneAccepts[zone]?.includes(kind));
53
- var zonesTaking = (edge, kind) => ZONES.filter((z) => zoneTakes(edge, z, kind));
54
- var edgeTakes = (edge, kind) => zonesTaking(edge, kind).length > 0;
55
- var hitBandOf = (scene, edge) => edge.hitBand ?? (edge.column === true && (edge.side === "left" || edge.side === "right") ? scene.columns?.[edge.side]?.hitBand : void 0) ?? DEFAULT_HIT_BAND;
56
- var columnFromTemplate = (t, id, side, ring, items) => ({
57
- id,
58
- side,
59
- ring,
60
- accepts: t.accepts,
61
- items,
62
- cross: t.cross,
63
- ...t.gap !== void 0 ? { gap: t.gap } : {},
64
- ...t.pad ? { pad: t.pad } : {},
65
- ...t.menu ? { menu: t.menu } : {},
66
- ...t.zoneAccepts ? { zoneAccepts: t.zoneAccepts } : {},
67
- ...t.hitBand !== void 0 ? { hitBand: t.hitBand } : {},
68
- column: true
69
- });
70
- var railModeOf = (scene, side) => side === "left" || side === "right" ? scene.railModes?.[side] === "icons" ? "icons" : "titles" : "icons";
71
- var axisOf = (side) => side === "left" || side === "right" ? "column" : "row";
72
- var chromeFor = (scene, who) => (scene.chrome ?? []).filter((c) => who === "floats" || c.for !== "floats").map((c) => ({ x: c.x, y: c.y, width: c.width, height: c.height }));
73
- function railColumnOf(scene, side, kind) {
74
- if (scene.rails?.[side]) return void 0;
75
- return scene.edges.filter((e) => e.side === side && !e.compact && e.hidden !== true && zoneTakes(e, "start", kind)).reduce((m, e) => !m || e.ring < m.ring ? e : m, void 0);
76
- }
77
- function zoneIn(edge, item) {
78
- const own = zoneOf(item);
79
- if (item.fixed) {
80
- const has = zonesOf(edge);
81
- return has.includes(own) ? own : has[0] ?? own;
82
- }
83
- if (zoneTakes(edge, own, item.kind)) return own;
84
- return zonesTaking(edge, item.kind)[0] ?? own;
85
- }
86
- var namesZone = (edge, kind) => zonesTaking(edge, kind).length > 1;
87
- var showInOf = (scene, e) => {
88
- if (e.hidden !== true || e.showIn === void 0) return void 0;
89
- const to = scene.edges.find((x) => x.id === e.showIn);
90
- return to && to !== e && to.hidden !== true && to.column !== true ? to : void 0;
91
- };
92
- function viewOf(scene) {
93
- const lent = /* @__PURE__ */ new Map();
94
- const ignored = [];
95
- const added = /* @__PURE__ */ new Map();
96
- const hidden = scene.edges.some((e) => e.hidden === true);
97
- if (hidden) {
98
- for (const e of scene.edges) {
99
- if (e.hidden !== true || e.showIn === void 0) continue;
100
- const to = showInOf(scene, e);
101
- if (!to) {
102
- ignored.push(e.id);
103
- continue;
104
- }
105
- for (const id of e.items) {
106
- const item = Object.hasOwn(scene.items, id) ? scene.items[id] : void 0;
107
- if (!item || item.fixed || lent.has(id) || to.items.includes(id) || !edgeTakes(to, item.kind)) continue;
108
- lent.set(id, e.id);
109
- added.set(to.id, [...added.get(to.id) ?? [], id]);
110
- }
111
- }
112
- }
113
- const edges = hidden ? scene.edges.filter((e) => e.hidden !== true).map((e) => added.has(e.id) ? { ...e, items: [...e.items, ...added.get(e.id)] } : e) : scene.edges;
114
- const rezoned = /* @__PURE__ */ new Set();
115
- let items;
116
- for (const e of edges) {
117
- for (const id of e.items) {
118
- const item = Object.hasOwn(scene.items, id) ? scene.items[id] : void 0;
119
- if (!item) continue;
120
- const zone = zoneIn(e, item);
121
- if (zone === zoneOf(item)) continue;
122
- const { align: _a, ...plain } = item;
123
- items ??= { ...scene.items };
124
- items[id] = zone === "start" ? plain : { ...plain, align: zone };
125
- rezoned.add(id);
126
- }
127
- }
128
- if (!hidden && !items) return { view: scene, lent, ignored, rezoned };
129
- return { view: { ...scene, edges, ...items ? { items } : {} }, lent, ignored, rezoned };
130
- }
131
- var inHiddenEdge = (scene, id) => scene.edges.some((e) => e.hidden === true && e.items.includes(id));
132
- function withHiddenOf(scene, shown, lent = /* @__PURE__ */ new Map(), moved, zoned = false, rezoned = /* @__PURE__ */ new Set()) {
133
- if (shown === scene) return shown;
134
- const back = [...lent.keys()].filter((id) => id !== moved);
135
- const edges = shown.edges.filter((e) => e.hidden !== true).map((e) => back.some((id) => e.items.includes(id)) ? { ...e, items: e.items.filter((id) => !back.includes(id)) } : e);
136
- scene.edges.forEach((e, i) => {
137
- if (e.hidden !== true) return;
138
- const own = moved !== void 0 && lent.get(moved) === e.id ? { ...e, items: e.items.filter((id) => id !== moved) } : e;
139
- edges.splice(Math.min(i, edges.length), 0, own);
140
- });
141
- const items = back.length > 0 ? { ...shown.items, ...Object.fromEntries(back.map((id) => [id, scene.items[id]])) } : { ...shown.items };
142
- const keep2 = new Set([...rezoned].filter((id) => !back.includes(id) && !(id === moved && zoned)));
143
- if (moved !== void 0 && !zoned) keep2.add(moved);
144
- for (const id of keep2) {
145
- const was = Object.hasOwn(scene.items, id) ? scene.items[id] : void 0;
146
- const now = Object.hasOwn(items, id) ? items[id] : void 0;
147
- if (!was || !now || now.align === was.align) continue;
148
- const { align: _a, ...plain } = now;
149
- items[id] = was.align === void 0 ? plain : { ...plain, align: was.align };
150
- }
151
- const ringBefore = new Map(scene.edges.filter((e) => e.hidden !== true).map((e) => [e.id, e.ring]));
152
- for (const side of new Set(scene.edges.filter((e) => e.hidden === true).map((e) => e.side))) {
153
- const was = scene.edges.filter((e) => e.side === side && e.hidden !== true).map((e) => e.id).sort();
154
- const now = edges.filter((e) => e.side === side && e.hidden !== true);
155
- if (now.length === was.length && now.every((e) => was.includes(e.id))) continue;
156
- const order = [...now].sort((a, b) => a.ring - b.ring || edges.indexOf(a) - edges.indexOf(b));
157
- for (const h of edges.filter((e) => e.side === side && e.hidden === true).sort((a, b) => a.ring - b.ring)) {
158
- const k = order.findIndex((e) => e.hidden !== true && (ringBefore.get(e.id) ?? -Infinity) > h.ring);
159
- order.splice(k < 0 ? order.length : k, 0, h);
160
- }
161
- order.forEach((e, ring) => {
162
- if (e.ring !== ring) edges[edges.indexOf(e)] = { ...e, ring };
163
- });
164
- }
165
- return { ...shown, edges, items };
166
- }
167
- function railPolicyOf(scene, kind) {
168
- const p = Object.hasOwn(scene.railDrop ?? {}, kind) ? scene.railDrop[kind] : void 0;
169
- if (!p) return null;
170
- const visibility = p.railVisibility === "always" || p.railVisibility === "never" ? p.railVisibility : "whileDragging";
171
- const width = p.railStripWidth;
172
- if (visibility === "never" || width !== void 0 && !(Number.isFinite(width) && width > 0)) return null;
173
- return {
174
- railStripWidth: width === void 0 ? null : Math.max(RAIL_STRIP_MIN, width),
175
- railVisibility: visibility,
176
- magnet: Number.isFinite(p.magnet) ? Math.max(0, p.magnet) : DEFAULT_RAIL_MAGNET,
177
- floatNearEdge: p.floatNearEdge !== false
178
- };
179
- }
180
- var zoneOf = (item) => item?.align === "center" || item?.align === "end" ? item.align : "start";
181
- var formsOf = (item, axis) => item.forms[axis] ?? [];
182
- var PANEL_FORMS = /* @__PURE__ */ new Set(["open", "header", "rail"]);
183
- var isDockingPanel = (item) => !item.closed && !item.fixed && PANEL_FORMS.has(formsOf(item, "column")[0]?.kind ?? "");
184
- function isOutsideSideBar(scene, e) {
185
- return e.outsideInset === true && isSideBar(scene, e);
186
- }
187
- function isSideBar(scene, e) {
188
- if (e.side !== "left" && e.side !== "right" || e.column === true || e.accepts.length === 0) return false;
189
- const template = scene.columns?.[e.side];
190
- const rail = scene.rails?.[e.side];
191
- if (!template && !rail) return false;
192
- const panelKinds = [...template?.accepts ?? [], ...rail?.accepts ?? []];
193
- return !e.accepts.some((k) => panelKinds.includes(k));
194
- }
195
- var isSideColumn = (scene, e) => (e.side === "left" || e.side === "right") && !e.compact && e.accepts.length > 0 && !isSideBar(scene, e);
196
- var floatOrientOf = (item) => {
197
- if (item.floatOrient === "row" || item.floatOrient === "column") return item.floatOrient;
198
- for (const k of Object.keys(item.forms)) if ((k === "row" || k === "column") && formsOf(item, k).length > 0) return k;
199
- return "column";
200
- };
201
- var sized = (s) => !!s && Number.isFinite(s.width) && Number.isFinite(s.height) && s.width > 0 && s.height > 0;
202
- var declaresFloatSizes = (item) => item.floatSizes !== void 0 && item.floatSizes !== null;
203
- function naturalFloatSize(item, orient2) {
204
- const f = formsOf(item, orient2)[0];
205
- if (!f || f.main.grows) return void 0;
206
- const main = Number.isFinite(f.main.natural) ? Math.max(0, f.main.natural) : 0;
207
- const cross = Number.isFinite(f.cross.natural) ? Math.max(0, f.cross.natural) : 0;
208
- if (main <= 0 || cross <= 0) return void 0;
209
- return orient2 === "row" ? { width: main, height: cross } : { width: cross, height: main };
210
- }
211
- function floatSizesOf(item, floatOrient = floatOrientOf(item)) {
212
- const out = {};
213
- if (sized(item.floatSize)) out[floatOrient] = item.floatSize;
214
- for (const o of ["row", "column"]) {
215
- const s = item.floatSizes?.[o];
216
- if (sized(s)) out[o] = s;
217
- }
218
- return out.row || out.column ? out : void 0;
219
- }
220
- function floatSizeOf(item, orient2 = floatOrientOf(item), floatOrient = floatOrientOf(item)) {
221
- if (!declaresFloatSizes(item)) return sized(item.floatSize) ? item.floatSize : naturalFloatSize(item, orient2) ?? DEFAULT_FLOAT_SIZE;
222
- const own = item.floatSizes?.[orient2];
223
- if (sized(own)) return own;
224
- if (sized(item.floatSize) && orient2 === floatOrient) return item.floatSize;
225
- return naturalFloatSize(item, orient2) ?? DEFAULT_FLOAT_SIZE;
226
- }
227
- function foldedByOf(item, forms, form, start, edge) {
228
- if (item.closed || form < 1) return void 0;
229
- if (startOf(item, forms, edge) > 0) return "hand";
230
- return start !== void 0 && form > start ? "auto" : void 0;
231
- }
232
- var foldedByHand = (item) => Number.isFinite(item.from) && Math.floor(item.from) >= 1;
233
- var foldsToRail = (item, edge) => !!item.fixed && edge?.foldable === true;
234
- function foldedIndex(item, forms, edge) {
235
- const bar = forms.findIndex((f) => f.kind === "header" || f.kind === "fold");
236
- return bar >= 0 || !foldsToRail(item, edge) ? bar : forms.findIndex((f) => f.kind === "rail");
237
- }
238
- function startOf(item, forms, edge) {
239
- if (!foldedByHand(item)) return 0;
240
- const folded = foldedIndex(item, forms, edge);
241
- return folded < 0 ? 0 : Math.min(Math.max(folded, Math.floor(item.from)), forms.length - 1);
242
- }
243
- function placingOf(scene, id) {
244
- if (!scene.edges.some((e) => e.hidden === true && e.showIn !== void 0 && e.items.includes(id))) return scene;
245
- const { view, lent } = viewOf(scene);
246
- return lent.has(id) ? view : scene;
247
- }
248
- function axesOf(given, id) {
249
- const scene = placingOf(given, id);
250
- const item = scene.items[id];
251
- const edge = scene.edges.find((e) => e.items.includes(id));
252
- if (edge) return [axisOf(edge.side)];
253
- const float = scene.floats.find((f) => f.id === id);
254
- return float && item ? [float.orient ?? floatOrientOf(item)] : ["row", "column"];
255
- }
256
- function isHandFolded(given, id) {
257
- const scene = placingOf(given, id);
258
- const item = Object.hasOwn(given.items, id) ? given.items[id] : void 0;
259
- const edge = scene.edges.find((e) => e.items.includes(id));
260
- return !!item && axesOf(scene, id).some((a) => startOf(item, formsOf(item, a), edge) > 0);
261
- }
262
- function pastWrap(forms, from) {
263
- for (let j = Math.max(0, Math.floor(from)); j < forms.length; j++) if (forms[j].kind !== "wrap" && !forms[j].wrap) return j;
264
- return void 0;
265
- }
266
- function floatShape(item, f) {
267
- const orient2 = f.orient ?? floatOrientOf(item);
268
- const forms = formsOf(item, orient2);
269
- const px2 = (v) => Number.isFinite(v) ? Math.max(0, v) : 0;
270
- const w = px2(f.rect.width);
271
- const h = px2(f.rect.height);
272
- if (forms.length === 0) return { form: 0, width: w, height: h };
273
- const asked = startOf(item, forms);
274
- const isWrap = (x) => x.kind === "wrap" || !!x.wrap;
275
- const skipped = orient2 === "column" ? pastWrap(forms, asked) : asked;
276
- if (skipped === void 0) return { form: asked, width: w, height: h, none: true };
277
- const form = skipped;
278
- const fm = forms[form];
279
- if (orient2 === "column" && forms.slice(0, form + 1).some(isWrap)) return { form, width: px2(fm.cross.natural), height: px2(fm.main.natural) };
280
- if (form > 0) return orient2 === "column" ? { form, width: w, height: px2(fm.main.natural) } : { form, width: px2(fm.main.natural), height: h };
281
- return { form, ...fm.main.grows || !declaresFloatSizes(item) ? { width: w, height: h } : floatSizeOf(item, orient2, f.orient ?? floatOrientOf(item)) };
282
- }
283
-
284
- // src/scene/targets/ids.ts
285
- function targetId(parts) {
286
- switch (parts.kind) {
287
- case "gap":
288
- return `gap:${parts.edge}:${parts.zone}:${parts.at}`;
289
- case "strip":
290
- return `strip:${parts.edge}:${parts.zone}`;
291
- case "column":
292
- case "rail":
293
- return `${parts.kind}:${parts.side}:${parts.ring}`;
294
- case "side":
295
- return `side:${parts.side}`;
296
- case "home":
297
- case "float":
298
- return `${parts.kind}:${parts.item}`;
299
- case "none":
300
- return `none:${parts.index}`;
301
- case "refuse":
302
- return parts.n === 0 ? `refuse:${parts.edge}:${parts.zone}` : `refuse:${parts.edge}:${parts.zone}:${parts.n}`;
303
- }
304
- }
305
- var ZONE = "(start|center|end)";
306
- var INT = "(-?\\d+)";
307
- var GAP = new RegExp(`^gap:(.+):${ZONE}:${INT}$`);
308
- var STRIP = new RegExp(`^strip:(.+):${ZONE}$`);
309
- var LINE = new RegExp(`^(column|rail):(left|right):${INT}$`);
310
- var SIDE = /^side:(left|right|top|bottom)$/;
311
- var ITEM = /^(home|float):(.+)$/;
312
- var NONE = new RegExp(`^none:${INT}$`);
313
- var REFUSE = new RegExp(`^refuse:(.+):${ZONE}(?::${INT})?$`);
314
- function parseTargetId(id) {
315
- let m;
316
- if (m = GAP.exec(id)) return { kind: "gap", edge: m[1], zone: m[2], at: Number(m[3]) };
317
- if (m = STRIP.exec(id)) return { kind: "strip", edge: m[1], zone: m[2] };
318
- if (m = LINE.exec(id)) return { kind: m[1], side: m[2], ring: Number(m[3]) };
319
- if (m = SIDE.exec(id)) return { kind: "side", side: m[1] };
320
- if (m = ITEM.exec(id)) return { kind: m[1], item: m[2] };
321
- if (m = NONE.exec(id)) return { kind: "none", index: Number(m[1]) };
322
- if (m = REFUSE.exec(id)) return { kind: "refuse", edge: m[1], zone: m[2], n: m[3] === void 0 ? 0 : Number(m[3]) };
323
- return null;
324
- }
325
- var virtualEdgeId = (kind, side) => `${kind}:${side}`;
326
- var RESERVED_EDGE_IDS = new Set(["column", "rail"].flatMap((k) => ["left", "right"].map((s) => virtualEdgeId(k, s))));
327
- var isReservedEdgeId = (id) => RESERVED_EDGE_IDS.has(id);
328
- function checkEdgeIds(scene) {
329
- for (const e of scene.edges) {
330
- if (isReservedEdgeId(e.id)) throw new Error(`placement: the edge id "${e.id}" is reserved for a side's virtual edge (column:<side>, rail:<side>); name the edge otherwise`);
331
- }
332
- }
333
- function placeTargetId(place, item) {
334
- switch (place.kind) {
335
- case "gap":
336
- return targetId(place);
337
- case "column":
338
- case "rail":
339
- return targetId(place);
340
- case "side":
341
- return targetId({ kind: "side", side: place.side });
342
- case "float":
343
- case "home":
344
- return targetId({ kind: place.kind, item });
345
- default:
346
- return null;
347
- }
348
- }
349
-
350
- // src/scene/solve/order.ts
351
- var finite = (x, fallback) => typeof x === "number" && Number.isFinite(x) ? x : fallback;
352
- var px = (x) => Math.max(0, finite(x, 0));
353
- var isSide = (e) => e.side === "left" || e.side === "right";
354
- var giveWayOrder = (a, b) => Number(a.kept) - Number(b.kept) || a.age - b.age || finite(a.edge.ring, 0) - finite(b.edge.ring, 0);
355
- function cutOrder(edges) {
356
- const phase = (e) => e.side === "left" || e.side === "right" ? 1 : e.span === "between" ? 2 : 0;
357
- const sideRank = (e) => e.side === "top" || e.side === "left" ? 0 : 1;
358
- 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);
359
- }
360
- function layKeys(scene) {
361
- const phase = (e) => e.mode === "overlay" ? 4 : isSide(e) ? 2 : e.span === "between" ? 3 : e.outsideInset === true ? 0 : 1;
362
- return new Map(cutOrder(scene.edges).map((e, at) => [e.id, { phase: phase(e), side: e.side, at }]));
363
- }
364
- function pushes(a, b) {
365
- if (a.phase === 4 || b.phase === 4) return false;
366
- if (a.phase !== b.phase) return a.phase < b.phase;
367
- return a.side === b.side && a.at < b.at;
368
- }
369
- var ZONE_RANK = { start: 0, center: 1, end: 2 };
370
- function zoneSorted(scene, ids) {
371
- 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);
372
- }
373
-
374
- // src/scene/solve/bar-giveway.ts
375
- var UNDER = 0.5;
376
- function roomOf(scene, l) {
377
- const bars = scene.edges.filter((e) => e.mode === "overlay" && !isSide(e)).reduce((s, e) => s + (l.edges[e.id]?.cross ?? 0), 0);
378
- return l.content.height - bars;
379
- }
380
- function settersOf(scene, l, edge) {
381
- return (l.edges[edge.id]?.items ?? []).flatMap((id) => {
382
- const at = l.items[id];
383
- const item = scene.items[id];
384
- if (!at || !item || at.inMenu) return [];
385
- const f = formsOf(item, "row")[at.form];
386
- return f && formSetsCross(f) ? [formCrossMin(f)] : [];
387
- });
388
- }
389
- function barGiveWay(scene, first, lay) {
390
- if (!scene.contentMin) return null;
391
- const minH = px(scene.contentMin.height);
392
- const shortOf = (l) => minH - roomOf(scene, l);
393
- if (shortOf(first) <= EPS) return null;
394
- const rankOf = (edge) => {
395
- const items = (first.edges[edge.id]?.items ?? []).filter((id) => scene.items[id] !== void 0);
396
- return {
397
- edge,
398
- kept: items.some((id) => scene.items[id].kept === true),
399
- age: Math.max(-Infinity, ...items.map((id) => finite(scene.items[id].used, 0)))
400
- };
401
- };
402
- const bars = cutOrder(scene.edges).filter((e) => !isSide(e) && e.giveWay === true && (first.edges[e.id]?.cross ?? 0) > EPS).map(rankOf).sort(giveWayOrder).map((r) => r.edge);
403
- if (bars.length === 0) return null;
404
- const folds = /* @__PURE__ */ new Map();
405
- const shrinkFrom = (l) => {
406
- const caps = new Map(folds);
407
- const notes = [];
408
- let left = shortOf(l);
409
- for (const e of bars) {
410
- if (left <= EPS) break;
411
- if (folds.has(e.id)) continue;
412
- const cross = l.edges[e.id]?.cross ?? 0;
413
- const min = Math.max(px(e.cross.min), ...settersOf(scene, l, e));
414
- const cut2 = Math.min(left, cross - min);
415
- if (cut2 <= EPS) continue;
416
- caps.set(e.id, cross - cut2);
417
- notes.push({ code: "bar-shrunk", edge: e.id, from: cross, to: cross - cut2 });
418
- left -= cut2;
419
- }
420
- return { caps, notes, left };
421
- };
422
- let base = first;
423
- let way = shrinkFrom(base);
424
- for (const e of bars) {
425
- if (way.left <= EPS) break;
426
- const setters = settersOf(scene, first, e);
427
- if (setters.length === 0) continue;
428
- folds.set(e.id, Math.min(...setters) - UNDER);
429
- const l = lay(folds);
430
- if ((l.edges[e.id]?.cross ?? 0) >= (base.edges[e.id]?.cross ?? 0) - EPS) {
431
- folds.delete(e.id);
432
- continue;
433
- }
434
- base = l;
435
- way = shrinkFrom(base);
436
- }
437
- if (way.caps.size === 0) return null;
438
- return { caps: way.caps, notes: [...way.notes, ...[...folds.keys()].map((edge) => ({ code: "bar-folded", edge }))] };
439
- }
440
-
441
- // src/scene/solve/draft.ts
442
- function createDraft(given, options, caps = /* @__PURE__ */ new Map()) {
443
- const previous = options.previous;
444
- const opened = /* @__PURE__ */ new Set();
445
- if (previous) {
446
- for (const e of given.edges) {
447
- for (const id of e.items) {
448
- const it = Object.hasOwn(given.items, id) ? given.items[id] : void 0;
449
- const was = previous.items[id];
450
- if (it && !it.closed && !it.fixed && (!was || was.closed)) opened.add(id);
451
- }
452
- }
453
- }
454
- const stayedOpen = (e) => e.items.some((id) => !opened.has(id) && Object.hasOwn(given.items, id) && !given.items[id].closed && !given.items[id].fixed);
455
- const newest = Object.values(given.items).reduce((m, it) => Math.max(m, finite(it.used, 0)), 0) + 1;
456
- 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));
457
- const scene = byAge.length === 0 ? given : { ...given, items: { ...given.items, ...Object.fromEntries(byAge.map((id, k) => [id, { ...given.items[id], used: newest + k }])) } };
458
- const fresh = new Set(options.fresh ?? []);
459
- const prevEdge = (edge) => !previous || fresh.has(edge.id) || edge.items.some((id) => fresh.has(id)) ? void 0 : previous.edges[edge.id];
460
- const notes = [];
461
- const order = cutOrder(scene.edges);
462
- const claimed = /* @__PURE__ */ new Set();
463
- const claim = (id) => {
464
- if (!Object.hasOwn(scene.items, id)) {
465
- notes.push({ code: "unknown-item", id });
466
- return false;
467
- }
468
- if (claimed.has(id)) {
469
- notes.push({ code: "twice", id });
470
- return false;
471
- }
472
- claimed.add(id);
473
- return true;
474
- };
475
- const owned = /* @__PURE__ */ new Map();
476
- for (const e of order) owned.set(e, e.items.filter(claim));
477
- return { scene, previous, opened, fresh, order, owned, claim, prevEdge, notes, items: {}, edges: {}, rails: {}, railPlaces: {}, caps };
478
- }
479
- function shapeOf(f, r, axis, cross) {
480
- if (!f) return {};
481
- if (f.wrap) {
482
- const lead = Math.max(0, finite(f.wrap.lead ?? 0, 0));
483
- const row = Math.max(0, finite(f.wrap.row, 0));
484
- return { rows: wrapLines(f.wrap, cross).lines, grip: rect(r.x, r.y, lead, Math.min(row, r.height)) };
485
- }
486
- const g = typeof f.grip === "number" && Number.isFinite(f.grip) && f.grip > 0 ? f.grip : 0;
487
- if (!g) return {};
488
- 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) };
489
- }
490
- function zoneStarts(scene, fit, input) {
491
- const column = fit.axis === "column";
492
- const at = (r) => column ? r.y : r.x;
493
- const out = /* @__PURE__ */ new Map();
494
- const lengths = /* @__PURE__ */ new Map();
495
- const drawn = fit.items.filter((p) => p.kind !== "menu");
496
- for (const p of drawn) {
497
- out.set(p.id, at(p.rect));
498
- lengths.set(p.id, column ? p.rect.height : p.rect.width);
499
- }
500
- const len2 = (p) => lengths.get(p.id);
501
- const gap = px(input.gap);
502
- const start = px(input.pad?.start);
503
- const end = px(input.pad?.end);
504
- const menu = fit.menu ? (column ? fit.menu.rect.height : fit.menu.rect.width) + (drawn.length > 0 ? gap : 0) : 0;
505
- const zone = (z) => drawn.filter((p) => zoneOf(scene.items[p.id]) === z);
506
- const s = zone("start");
507
- const c = zone("center");
508
- const e = zone("end");
509
- const title = column ? void 0 : s.find((p) => {
510
- const it = scene.items[p.id];
511
- const f = formsOf(it, "row")[p.form];
512
- return it.fixed && f?.main.grows && len2(p) > formMain(f, "natural", fit.cross) + EPS;
513
- });
514
- if (title) lengths.set(title.id, formMain(formsOf(scene.items[title.id], "row")[title.form], "natural", fit.cross));
515
- const used = drawn.reduce((sum, p) => sum + len2(p), 0) + Math.max(0, drawn.length - 1) * gap;
516
- const spare = fit.length - start - end - menu - used;
517
- if (spare <= EPS) return { starts: out, lengths };
518
- const spaceOf = (p) => {
519
- const v = scene.items[p.id]?.space;
520
- return Number.isFinite(v) && v > 0 ? v : 0;
521
- };
522
- const spacing = new Set([...s, ...e].map((p) => p.id));
523
- const asked = [...s, ...e].reduce((sum, p) => sum + spaceOf(p), 0);
524
- const share = asked > spare ? spare / asked : 1;
525
- const room = (p) => spacing.has(p.id) ? spaceOf(p) * share : 0;
526
- const total = (ps) => ps.reduce((sum, p) => sum + len2(p), 0) + Math.max(0, ps.length - 1) * gap;
527
- const lay = (ps, from, lead) => {
528
- let x = from;
529
- for (const p of ps) {
530
- if (lead) x += room(p);
531
- out.set(p.id, x);
532
- x += len2(p) + gap + (lead ? 0 : room(p));
533
- }
534
- };
535
- const spaced = (ps) => total(ps) + ps.reduce((sum, p) => sum + room(p), 0);
536
- lay(s, start, true);
537
- const endStart = fit.length - end - menu - spaced(e);
538
- lay(e, endStart, false);
539
- const lo2 = start + spaced(s) + (s.length > 0 ? gap : 0);
540
- const hi = endStart - (e.length > 0 ? gap : 0);
541
- const mid2 = (fit.length - total(c)) / 2;
542
- const clear = mid2 >= lo2 - EPS && mid2 + total(c) <= hi + EPS;
543
- lay(c, title && !clear ? Math.max(lo2, (lo2 + hi - total(c)) / 2) : Math.max(lo2, Math.min(mid2, hi - total(c))), false);
544
- if (title && s[s.length - 1] === title) {
545
- const next = [...c, ...e].map((p) => out.get(p.id));
546
- const until = next.length ? Math.min(...next) - gap : fit.length - end - menu;
547
- lengths.set(title.id, Math.max(len2(title), until - out.get(title.id)));
548
- }
549
- return { starts: out, lengths };
550
- }
551
- function emit(d, edge, r, solved, extra = {}) {
552
- const { scene, notes, items, edges } = d;
553
- const column = axisOf(edge.side) === "column";
554
- const { stack } = solved;
555
- const thick = column ? r.width : r.height;
556
- const { starts, lengths } = zoneStarts(scene, stack, solved.input);
557
- const toStage = (l, id) => {
558
- const s = id !== void 0 ? starts.get(id) : void 0;
559
- const n = id !== void 0 ? lengths.get(id) : void 0;
560
- const x = column ? l.x : s ?? l.x;
561
- const y = column ? s ?? l.y : l.y;
562
- return rect(r.x + x, r.y + y, column ? l.width : n ?? l.width, column ? n ?? l.height : l.height);
563
- };
564
- const tabs = new Set(solved.tabs);
565
- for (const id of solved.leftOut ?? []) notes.push({ code: "no-vertical-form", edge: edge.id, id });
566
- for (const id of solved.leftOutIcons ?? []) notes.push({ code: "no-icons-form", edge: edge.id, id });
567
- for (const p of stack.items) {
568
- const it = scene.items[p.id];
569
- const inMenu = p.kind === "menu";
570
- const drawnRect = toStage(p.rect, inMenu ? void 0 : p.id);
571
- const f = formsOf(it, column ? "column" : "row")[p.form];
572
- const shape = inMenu ? {} : shapeOf(f, drawnRect, column ? "column" : "row", stack.cross);
573
- const foldedBy = foldedByOf(it, formsOf(it, column ? "column" : "row"), p.form, solved.starts.get(p.id), edge);
574
- items[p.id] = {
575
- ...shape,
576
- hidden: inMenu ? [] : f?.hides ?? [],
577
- rect: drawnRect,
578
- kind: it.kind,
579
- axis: column ? "column" : "row",
580
- form: p.form,
581
- formKind: p.kind,
582
- edge: edge.id,
583
- zone: zoneOf(it),
584
- ...inMenu ? { inMenu: true } : {},
585
- ...tabs.has(p.id) ? { tab: true } : {},
586
- ...tabs.has(p.id) && f?.title === true && (edge.side === "left" || edge.side === "right") ? { railTitle: edge.side } : {},
587
- ...solved.closedTabs.has(p.id) ? { closed: true } : {},
588
- ...foldedBy ? { foldedBy } : {},
589
- ...it.fixed ? { fixed: true } : {}
590
- };
591
- }
592
- for (const n of stack.notes) notes.push({ edge: edge.id, ...n });
593
- edges[edge.id] = {
594
- side: edge.side,
595
- ring: finite(edge.ring, 0),
596
- rect: r,
597
- cross: thick,
598
- menu: stack.menu ? { rect: toStage(stack.menu.rect), items: stack.menu.items } : null,
599
- // The stack's items are in zone order, the order `emit` drew them in.
600
- items: stack.items.map((p) => p.id),
601
- tabs: solved.tabs,
602
- stack,
603
- free: solved.free,
604
- ...extra
605
- };
606
- }
607
-
608
- // src/scene/solve/edge.ts
609
- var stackItemOf = (item, forms, edge, from) => ({
610
- id: item.id,
611
- forms,
612
- used: finite(item.used, 0),
613
- ...item.kept ? { kept: true } : {},
614
- // Where its ladder starts: as asked, else its hand fold resolved on this axis (`startOf`: its folded form).
615
- ...from !== void 0 ? { from } : startOf(item, forms, edge) > 0 ? { from: startOf(item, forms, edge) } : {},
616
- ...item.fixed ? { fixed: true } : {}
617
- });
618
- function hasSetter(fit, forms) {
619
- return fit.items.some((p) => p.kind !== "menu" && formSetsCross(forms(p.id)[p.form]));
620
- }
621
- var TRAY_RANK = 100;
622
- var startsOf = (input) => new Map(input.items.map((i) => [i.id, i.from ?? 0]));
623
- function compactEdge(f) {
624
- const { scene, edge, formsById } = f;
625
- const iconsAt = (id) => {
626
- const forms = formsById(id);
627
- for (let j = startOf(scene.items[id], forms, edge); j < forms.length; j++) if (forms[j].kind === "icons") return j;
628
- return forms.findIndex((x) => x.kind === "icons");
629
- };
630
- const shown = f.ordered.filter((id) => !scene.items[id].closed && iconsAt(id) >= 0);
631
- const leftOutIcons = f.ordered.filter((id) => !scene.items[id].closed && iconsAt(id) < 0);
632
- const compactInput = { ...f.base, ownCross: false, items: shown.map((id) => stackItemOf(scene.items[id], formsById(id), edge, iconsAt(id))) };
633
- const stack = fitStack(compactInput, f.previous ? { previous: f.previous.free } : {});
634
- return { stack, free: stack, tabs: [], closedTabs: /* @__PURE__ */ new Set(), input: compactInput, starts: startsOf(compactInput), leftOutIcons };
635
- }
636
- function railEdge(f, free, asStackItem, starts) {
637
- const { scene, edge, formsById } = f;
638
- const railIndex = (item, forms) => {
639
- if (item.fixed) return -1;
640
- for (let j = startOf(item, forms, edge); j < forms.length; j++) if (forms[j].kind === "rail") return j;
641
- return -1;
642
- };
643
- const wrapOnly = (id) => railIndex(scene.items[id], formsById(id)) < 0 && pastWrap(formsById(id), startOf(scene.items[id], formsById(id), edge)) === void 0;
644
- const shown = f.ordered.filter((id) => {
645
- const it = scene.items[id];
646
- if (!it.closed && wrapOnly(id)) return false;
647
- return !it.closed || edge.railShowsClosed === true && railIndex(it, formsById(id)) >= 0;
648
- });
649
- const leftOut = f.ordered.filter((id) => !scene.items[id].closed && wrapOnly(id));
650
- const anyRail = shown.some((id) => railIndex(scene.items[id], formsById(id)) >= 0);
651
- if (!anyRail) return null;
652
- const railInput = {
653
- ...f.base,
654
- // The side's rail mode: a title rail form is offered only while the side shows titles.
655
- ...railModeOf(scene, edge.side) === "titles" ? { titles: true } : {},
656
- items: shown.map((id) => {
657
- const it = scene.items[id];
658
- const j = railIndex(it, formsById(id));
659
- const forms = asStackItem(id).forms;
660
- return stackItemOf(it, forms, edge, j >= 0 ? j : pastWrap(forms, startOf(it, forms, edge)));
661
- })
662
- };
663
- const stack = fitStack(railInput, f.previous ? { previous: f.previous.stack } : {});
664
- const tabs = stack.items.filter((p) => p.kind === "rail").map((p) => p.id);
665
- const closedTabs = new Set(shown.filter((id) => scene.items[id].closed));
666
- return { stack, free, tabs, closedTabs, input: railInput, starts, leftOut };
667
- }
668
- function solveEdge(scene, edge, ids, length, room, previous) {
669
- const axis = axisOf(edge.side);
670
- const column = axis === "column";
671
- const ordered = zoneSorted(scene, ids);
672
- const priorityOf = (id) => finite(scene.items[id].priority ?? edge.priority ?? 0, 0);
673
- const formsById = (id) => {
674
- const forms = formsOf(scene.items[id], axis);
675
- const p = priorityOf(id);
676
- return p === 0 ? forms : forms.map((f) => ({ ...f, rank: finite(f.rank ?? 0, 0) + p }));
677
- };
678
- const maxOf = Number.isNaN(edge.cross.max) ? Infinity : edge.cross.max;
679
- const cross = { min: Math.min(px(edge.cross.min), room), max: Math.min(maxOf, room), rail: Math.min(px(edge.cross.rail ?? 30), room) };
680
- const base = {
681
- axis,
682
- length,
683
- cross,
684
- ...edge.menu ? { menu: edge.menu } : {},
685
- ...edge.gap !== void 0 ? { gap: edge.gap } : {},
686
- ...edge.pad ? { pad: edge.pad } : {},
687
- ...column ? { ownCross: true } : {}
688
- };
689
- const frame = { scene, edge, column, ordered, formsById, base, previous };
690
- if (edge.compact) return compactEdge(frame);
691
- const open2 = ordered.filter((id) => !scene.items[id].closed);
692
- const asStackItem = (id) => {
693
- const it = scene.items[id];
694
- const forms = formsById(id);
695
- if (!column || !edge.zoneAccepts?.end || zoneOf(it) !== "end") return stackItemOf(it, forms, edge);
696
- const tray = (f) => f.kind === "wrap" || f.kind === "icons" || !!f.wrap;
697
- let first = -1;
698
- for (let j = startOf(it, forms, edge); j < forms.length && first < 0; j++) if (tray(forms[j])) first = j;
699
- let last = -1;
700
- forms.forEach((f, j) => {
701
- if (tray(f)) last = j;
702
- });
703
- if (first < 0) return stackItemOf(it, forms, edge);
704
- return stackItemOf(it, forms.slice(0, last + 1).map((f) => ({ ...f, rank: finite(f.rank ?? 0, 0) + TRAY_RANK })), edge, first);
705
- };
706
- const input = { ...base, items: open2.map(asStackItem) };
707
- const free = fitStack(input, previous ? { previous: previous.free } : {});
708
- const none = /* @__PURE__ */ new Set();
709
- const starts = startsOf(input);
710
- if (column && !hasSetter(free, formsById)) {
711
- const rail = railEdge(frame, free, asStackItem, starts);
712
- if (rail) return rail;
713
- }
714
- if (!column && edge.giveWay === true) {
715
- const fits = (f) => !formSetsCross(f) || formCrossMin(f) <= cross.max + EPS;
716
- let moved = false;
717
- const items = input.items.map((it) => {
718
- const from = it.from ?? 0;
719
- if (it.fixed || fits(it.forms[from])) return it;
720
- const j = it.forms.findIndex((f, k) => k > from && f.kind !== "menu" && fits(f));
721
- if (j < 0) return it;
722
- moved = true;
723
- return { ...it, from: j };
724
- });
725
- if (moved) {
726
- const folded = { ...input, items };
727
- return { stack: fitStack(folded), free, tabs: [], closedTabs: none, input: folded, starts };
728
- }
729
- }
730
- const size = edge.size;
731
- if (typeof size === "number" && Number.isFinite(size) && hasSetter(free, formsById)) {
732
- const setters = free.items.filter((p) => p.kind !== "menu" && formSetsCross(formsById(p.id)[p.form]));
733
- const lo2 = Math.max(px(edge.cross.min), ...setters.map((p) => formCrossMin(formsById(p.id)[p.form])));
734
- const s = Math.min(Math.max(size, lo2), Math.max(lo2, maxOf), room);
735
- if (Math.abs(s - free.cross) > EPS) {
736
- const sized2 = { ...input, cross: { ...cross, min: s, max: s } };
737
- const stack = fitStack(sized2, previous ? { previous: previous.stack } : {});
738
- return { stack, free, tabs: [], closedTabs: none, input: sized2, starts };
739
- }
740
- }
741
- return { stack: free, free, tabs: [], closedTabs: none, input, starts };
742
- }
743
-
744
- // src/scene/solve/bars.ts
745
- function cutEdge(d, edge, area2, cut2) {
746
- const column = axisOf(edge.side) === "column";
747
- const length = column ? area2.height : area2.width;
748
- const room = Math.max(0, column ? area2.width : area2.height);
749
- const cap = d.caps.get(edge.id);
750
- const solvedAs = cap === void 0 ? edge : { ...edge, cross: { ...edge.cross, max: Math.min(edge.cross.max, cap) } };
751
- const solved = solveEdge(d.scene, solvedAs, d.owned.get(edge), length, room, d.prevEdge(edge));
752
- const thick = solved.stack.items.length === 0 ? 0 : Math.min(solved.stack.cross, room);
753
- 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);
754
- emit(d, edge, r, solved);
755
- if (!cut2) return area2;
756
- 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);
757
- }
758
-
759
- // src/scene/solve/floats.ts
760
- var sinceOf = (f) => isSince(f.since) ? f.since : 0;
761
- function anchored(content, w, h, given) {
762
- const anchor = anchorName(given);
763
- const dx = typeof given === "string" ? 0 : finite(given.dx ?? 0, 0);
764
- const dy = typeof given === "string" ? 0 : finite(given.dy ?? 0, 0);
765
- const left = content.x;
766
- const right = content.x + content.width - w;
767
- const midX = content.x + (content.width - w) / 2;
768
- const top = content.y;
769
- const bottom = content.y + content.height - h;
770
- const midY = content.y + (content.height - h) / 2;
771
- const at = {
772
- "content-top-left": [left, top],
773
- "content-top": [midX, top],
774
- "content-top-right": [right, top],
775
- "content-right": [right, midY],
776
- "content-bottom-right": [right, bottom],
777
- "content-bottom": [midX, bottom],
778
- "content-bottom-left": [left, bottom],
779
- "content-left": [left, midY]
780
- };
781
- const [x, y] = at[anchor] ?? at["content-top-right"];
782
- return rect(x + dx, y + dy, w, h);
783
- }
784
- function floatFloor(item, form, orient2, size) {
785
- const row = orient2 === "row";
786
- return {
787
- width: item.floatMin ? px(item.floatMin.width) : Math.min(size.width, px(row ? form?.main.min : form?.cross.min)),
788
- height: item.floatMin ? px(item.floatMin.height) : Math.min(size.height, px(row ? form?.cross.min : form?.main.min))
789
- };
790
- }
791
- function clearSize(region, avoid) {
792
- const span = (axis) => {
793
- const lo2 = axis === "y" ? region.y : region.x;
794
- const hi = lo2 + (axis === "y" ? region.height : region.width);
795
- const mid2 = (lo2 + hi) / 2;
796
- let a = lo2;
797
- let b = hi;
798
- for (const r of avoid) {
799
- const whole = axis === "y" ? r.x <= region.x + EPS && r.x + r.width >= region.x + region.width - EPS : r.y <= region.y + EPS && r.y + r.height >= region.y + region.height - EPS;
800
- const s = axis === "y" ? r.y : r.x;
801
- const e = s + (axis === "y" ? r.height : r.width);
802
- if (!whole || e <= lo2 || s >= hi) continue;
803
- if (s + e <= 2 * mid2) a = Math.max(a, e);
804
- else b = Math.min(b, s);
805
- }
806
- return Math.max(0, b - a);
807
- };
808
- return { width: span("x"), height: span("y") };
809
- }
810
- function fitBeside(want, region, avoid, earlier) {
811
- if (earlier.length === 0) return fitFloat(want, region, avoid);
812
- const both = fitFloat(want, region, [...avoid, ...earlier]);
813
- return both.covers.length === 0 ? both : fitFloat(want, region, avoid);
814
- }
815
- function placeFloat(item, f, region, avoid, content, stage, earlier = []) {
816
- const shape = floatShape(item, f);
817
- const from = shape.form;
818
- const fo = f.orient ?? floatOrientOf(item);
819
- const form = formsOf(item, fo)[from];
820
- const floor = floatFloor(item, form, fo, shape);
821
- const cap = { width: Number.isFinite(item.floatMax?.width) ? item.floatMax.width : Infinity, height: Number.isFinite(item.floatMax?.height) ? item.floatMax.height : Infinity };
822
- const clear = clearSize(region, avoid);
823
- const w = Math.max(floor.width, Math.min(shape.width, clear.width, cap.width));
824
- const folded = form?.kind === "header" || form?.kind === "fold";
825
- const h = folded ? shape.height : Math.max(floor.height, Math.min(shape.height, clear.height, cap.height));
826
- const want = f.anchor ? anchored(content, w, h, f.anchor) : rect(finite(f.rect.x, stage.x), finite(f.rect.y, stage.y), w, h);
827
- const fit0 = f.anchor ? fitBeside(want, region, avoid, earlier) : fitFloat(want, region, avoid);
828
- const fit = w > region.width + EPS ? { ...fit0, rect: rect(region.x, fit0.rect.y, w, fit0.rect.height) } : fit0;
829
- return { from, fo, form, fit };
830
- }
831
- function layFloats(d, stage, content, inset) {
832
- const { scene, notes, items, edges } = d;
833
- const avoid = [];
834
- for (const l of Object.values(items)) avoid.push(l.rect);
835
- for (const e of Object.values(edges)) {
836
- if (axisOf(e.side) === "row" && e.cross > 0) avoid.push(e.rect);
837
- if (e.menu) avoid.push(e.menu.rect);
838
- }
839
- for (const c of chromeFor(scene, "floats")) avoid.push(c);
840
- const floatRegion2 = rect(stage.x + px(inset.left), stage.y, Math.max(0, stage.width - px(inset.left) - px(inset.right)), stage.height);
841
- const drawn = [];
842
- const noForm = /* @__PURE__ */ new Set();
843
- for (const f of scene.floats) {
844
- if (!d.claim(f.id)) continue;
845
- const it = scene.items[f.id];
846
- if (it.closed) continue;
847
- if (floatShape(it, f).none) noForm.add(f.id);
848
- drawn.push(f);
849
- }
850
- const placed = /* @__PURE__ */ new Map();
851
- const place = (f, earlier = []) => {
852
- placed.set(f.id, placeFloat(scene.items[f.id], f, floatRegion2, avoid, content, stage, earlier));
853
- };
854
- const live = drawn.filter((f) => !noForm.has(f.id));
855
- for (const f of live) if (!f.anchor) place(f);
856
- const own = [...placed.values()].map((p) => p.fit.rect);
857
- const anchored2 = live.filter((f) => f.anchor).sort((a, b) => sinceOf(a) - sinceOf(b) || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
858
- anchored2.forEach((f, i) => place(f, [...own, ...anchored2.slice(0, i).map((a) => placed.get(a.id).fit.rect)]));
859
- const floats = [];
860
- for (const f of drawn) {
861
- if (noForm.has(f.id)) {
862
- notes.push({ code: "no-vertical-form", id: f.id });
863
- continue;
864
- }
865
- const it = scene.items[f.id];
866
- const { from, fo, form, fit } = placed.get(f.id);
867
- if (fit.clamped) notes.push({ code: "float-clamped", id: f.id });
868
- if (fit.snapped) notes.push({ code: "float-moved", id: f.id });
869
- if (fit.covers.length > 0) notes.push({ code: "float-covers", id: f.id });
870
- const foldedBy = foldedByOf(it, formsOf(it, fo), from);
871
- items[f.id] = { ...shapeOf(form, fit.rect, fo, fit.rect.width), hidden: form?.hides ?? [], rect: fit.rect, kind: it.kind, axis: fo, form: from, formKind: form?.kind ?? "open", floating: true, ...foldedBy ? { foldedBy } : {} };
872
- floats.push(f.id);
873
- }
874
- return floats;
875
- }
876
-
877
- // src/scene/solve/insets.ts
878
- function insetOf(d) {
879
- const { scene } = d;
880
- const docked = d.order.some((e) => isSide(e) && e.accepts.length > 0 && d.owned.get(e).some((id) => isDockingPanel(scene.items[id])));
881
- const inset = scene.insetWhen === "docked" && !docked ? {} : scene.inset ?? {};
882
- return { inset, docked };
883
- }
884
- 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)));
885
- function insetReach(area2, f) {
886
- const up = area2.y <= f.outer.y + px(f.inset.top) + EPS ? px(f.inset.top) : 0;
887
- const down = area2.y + area2.height >= f.outer.y + f.outer.height - px(f.inset.bottom) - EPS ? px(f.inset.bottom) : 0;
888
- return { up, down };
889
- }
890
-
891
- // src/scene/solve/notes.ts
892
- function contentNotes(d, content) {
893
- const { scene, notes, previous } = d;
894
- if (scene.contentMin) {
895
- const minW = px(scene.contentMin.width) - (d.chromeAllowance ?? 0);
896
- const minH = px(scene.contentMin.height);
897
- if (content.width < minW - EPS) notes.push({ code: "content-narrow", width: content.width, min: minW });
898
- const bars = d.order.filter((e) => e.mode === "overlay" && !isSide(e)).reduce((s, e) => s + (d.edges[e.id]?.cross ?? 0), 0);
899
- if (content.height - bars < minH - EPS) notes.push({ code: "content-short", height: content.height - bars, min: minH });
900
- }
901
- if (previous) {
902
- const was = previous.content;
903
- 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;
904
- if (moved) notes.push({ code: "content-changed", from: was, to: content });
905
- }
906
- }
907
-
908
- // src/scene/solve/columns.ts
909
- var isChrome = (scene, c) => !c.dyn && isSideBar(scene, c.edge);
910
- function columnsOf(d, sideEdges, area2, tall) {
911
- const { scene } = d;
912
- return sideEdges.map((edge) => {
913
- const side = edge.side;
914
- const solved = solveEdge(scene, edge, d.owned.get(edge), tall(edge), Math.max(0, area2.width), d.prevEdge(edge));
915
- const width = solved.stack.items.length === 0 ? 0 : Math.min(solved.stack.cross, area2.width);
916
- const visible = d.owned.get(edge).filter((id) => !scene.items[id].closed);
917
- const dyn = (edge.column === true || edge.giveWay === true) && scene.rails?.[side] !== void 0 && visible.length > 0;
918
- const forms = (id) => formsOf(scene.items[id], "column");
919
- const setters = solved.free.items.filter((p) => p.kind !== "menu" && formSetsCross(forms(p.id)[p.form]));
920
- const min = setters.length > 0 ? Math.min(width, Math.max(px(edge.cross.min), ...setters.map((p) => formCrossMin(forms(p.id)[p.form])))) : width;
921
- return {
922
- edge,
923
- side,
924
- solved,
925
- width,
926
- min,
927
- visible,
928
- dyn,
929
- hand: dyn && edge.column === true && visible.every((id) => startOf(scene.items[id], formsOf(scene.items[id], "column")) > 0),
930
- kept: visible.some((id) => scene.items[id].kept === true),
931
- age: Math.max(-Infinity, ...visible.map((id) => finite(scene.items[id].used, 0)))
932
- };
933
- });
934
- }
935
- function giveWay(d, cols, area2, gutter) {
936
- const { scene, previous, fresh, opened } = d;
937
- const ride = scene.handFold === "ride";
938
- const cmin = scene.contentMin ? px(scene.contentMin.width) : void 0;
939
- const chromeWidth = scene.contentMin?.chromeWidth;
940
- const floor = cmin !== void 0 && typeof chromeWidth === "number" && Number.isFinite(chromeWidth) ? Math.min(cmin, px(chromeWidth)) : void 0;
941
- const chrome = floor === void 0 ? 0 : cols.filter((c) => isChrome(scene, c) && c.width > 0).reduce((s, c) => s + c.width + (c.edge.outsideInset === true ? 0 : gutter), 0);
942
- if (floor !== void 0) d.chromeAllowance = Math.min(chrome, cmin - floor);
943
- const foldOrder = cols.filter((c) => c.dyn && !c.hand && c.width > 0).sort((a, b) => giveWayOrder(a, b) || (a.side === b.side ? 0 : a.side === "left" ? -1 : 1));
944
- const shrunkBefore = (previous?.notes ?? []).flatMap((n) => n.code === "column-shrunk" ? [n.edge] : []);
945
- const rankOf = (c) => shrunkBefore.includes(c.edge.id) ? shrunkBefore.indexOf(c.edge.id) : Infinity;
946
- 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);
947
- const railW = (side) => px(scene.rails?.[side]?.width);
948
- const evaluate = (auto2) => {
949
- const openOn = (side) => cols.some((c) => c.side === side && c.dyn && c.edge.column === true && !c.hand && !auto2.has(c) && c.width > 0);
950
- const riding = (c) => ride && c.hand && openOn(c.side);
951
- const railed = (c) => c.dyn && (auto2.has(c) || c.hand && !riding(c));
952
- const widths = new Map(cols.filter((c) => !railed(c) && !riding(c)).map((c) => [c, c.width]));
953
- const railSides = ["left", "right"].filter((s) => cols.some((c) => c.side === s && railed(c)));
954
- let used = 0;
955
- for (const [c, w] of widths) used += w > 0 ? w + (c.edge.outsideInset === true ? 0 : gutter) : 0;
956
- for (const s of railSides) used += railW(s) + gutter;
957
- let excess = cmin === void 0 ? -Infinity : floor === void 0 ? used + cmin - area2.width : Math.max(used - chrome + cmin, used + floor) - area2.width;
958
- for (const c of shrinkOrder) {
959
- if (excess <= EPS) break;
960
- const w = widths.get(c);
961
- if (w === void 0) continue;
962
- const take = Math.min(excess, w - c.min);
963
- if (take > 0) {
964
- widths.set(c, w - take);
965
- excess -= take;
966
- }
967
- }
968
- return { auto: auto2, widths, excess, railed, riding, shrinkOrder };
969
- };
970
- let auto = /* @__PURE__ */ new Set();
971
- if (cmin !== void 0) {
972
- const unfold = () => {
973
- for (const c of [...auto].reverse()) {
974
- const fewer = new Set([...auto].filter((x) => x !== c));
975
- if (evaluate(fewer).excess <= EPS) auto = fewer;
976
- else break;
977
- }
978
- };
979
- if (previous) {
980
- const byId = new Map(foldOrder.map((c) => [c.edge.id, c]));
981
- 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));
982
- 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)] : []);
983
- auto = new Set(was);
984
- if (evaluate(auto).excess <= EPS) unfold();
985
- }
986
- for (const c of foldOrder) {
987
- if (evaluate(auto).excess <= EPS) break;
988
- if (!auto.has(c)) auto = /* @__PURE__ */ new Set([...auto, c]);
989
- }
990
- for (let size = -1; size !== auto.size; ) {
991
- size = auto.size;
992
- unfold();
993
- }
994
- }
995
- return evaluate(auto);
996
- }
997
- function columnRoom(d, cols, area2, f) {
998
- const { scene } = d;
999
- const cmin = scene.contentMin ? px(scene.contentMin.width) : void 0;
1000
- const openMin = (ids, least) => Math.max(
1001
- least,
1002
- ...ids.map((id) => {
1003
- const form = formsOf(scene.items[id], "column")[0];
1004
- return form && formSetsCross(form) ? formCrossMin(form) : 0;
1005
- })
1006
- );
1007
- 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);
1008
- const templates = ["left", "right"].flatMap((s) => {
1009
- const t = scene.columns?.[s];
1010
- if (!t) return [];
1011
- const takes = Object.values(scene.items).filter((it) => !it.fixed && !it.closed && t.accepts.includes(it.kind));
1012
- return [openMin(takes.map((it) => it.id), px(t.cross.min))];
1013
- });
1014
- const colMins = open2.length > 0 ? open2 : templates;
1015
- if (cmin === void 0 || colMins.length === 0) return;
1016
- const allBars = cols.filter((c) => c.width > 0 && isChrome(scene, c)).reduce((s, c) => s + c.width + (c.edge.outsideInset === true ? 0 : f.gutter), 0);
1017
- const bars = d.chromeAllowance === void 0 ? allBars : allBars - Math.min(allBars, d.chromeAllowance);
1018
- const width = area2.width - bars - (scene.insetWhen === "docked" && !f.docked ? px(scene.inset?.left) + px(scene.inset?.right) : 0);
1019
- const need = cmin + Math.min(...colMins) + f.gutter;
1020
- d.columnRoom = width >= need - EPS;
1021
- if (!d.columnRoom) d.notes.push({ code: "no-column-room", width, need });
1022
- }
1023
- function giveWayNotes(d, g, cols) {
1024
- const { scene, previous } = d;
1025
- 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));
1026
- const before = previous?.notes ?? [];
1027
- const shrunk = /* @__PURE__ */ new Map();
1028
- for (const c of g.shrinkOrder) {
1029
- const w = g.widths.get(c);
1030
- 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 });
1031
- }
1032
- const folded = new Map([...g.auto].map((c) => [c.edge.id, { code: c.kept ? "kept-folded" : "column-folded", edge: c.edge.id }]));
1033
- const merge = (now, kept) => {
1034
- const order = [];
1035
- for (const n of before) {
1036
- const edge = kept(n) && "edge" in n ? n.edge : void 0;
1037
- if (edge === void 0) continue;
1038
- if (dormant.has(edge)) order.push(n);
1039
- else if (now.has(edge)) {
1040
- order.push(now.get(edge));
1041
- now.delete(edge);
1042
- }
1043
- }
1044
- d.notes.push(...order, ...now.values());
1045
- };
1046
- d.notes.push(...shrunk.values());
1047
- merge(folded, (n) => n.code === "column-folded" || n.code === "kept-folded");
1048
- }
1049
-
1050
- // src/scene/solve/rails.ts
1051
- function riderBars(d, c) {
1052
- const { scene } = d;
1053
- return zoneSorted(scene, c.visible).map((id) => {
1054
- const forms = formsOf(scene.items[id], "column");
1055
- const form = forms.length === 0 ? 0 : startOf(scene.items[id], forms);
1056
- return { id, form, f: forms[form], h: px(forms[form]?.main.natural) };
1057
- });
1058
- }
1059
- var ridersHeight = (d, riders) => riders.reduce((s, c) => s + riderBars(d, c).reduce((t, b) => t + b.h, 0), 0);
1060
- function layRiders(d, host, riders, top) {
1061
- const { scene, items, edges } = d;
1062
- const hr = edges[host.edge.id].rect;
1063
- let y = top;
1064
- for (const c of riders) {
1065
- const bars = riderBars(d, c);
1066
- const y0 = y;
1067
- for (const b of bars) {
1068
- const at = rect(hr.x, y, hr.width, b.h);
1069
- const it = scene.items[b.id];
1070
- items[b.id] = { rect: at, hidden: b.f?.hides ?? [], kind: it.kind, axis: "column", form: b.form, formKind: b.f?.kind ?? "header", edge: c.edge.id, zone: zoneOf(it), rides: host.edge.id, folded: "hand", foldedBy: "hand" };
1071
- y += b.h;
1072
- }
1073
- 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, items: bars.map((b) => b.id), tabs: [], stack: c.solved.stack, free: c.solved.free, rides: host.edge.id, folded: "hand" };
1074
- }
1075
- }
1076
- function layRail(d, side, railRect, railed, g) {
1077
- const { scene, items } = d;
1078
- const segments = [];
1079
- const members = railed.flatMap((c) => zoneSorted(scene, c.visible).map((id) => ({ id, c })));
1080
- const formsById = (id) => formsOf(scene.items[id], "column");
1081
- const tileOf = (id) => formsById(id).findIndex((f) => f.kind === "rail" && f.title !== true);
1082
- const titleOf = (id) => {
1083
- const t = tileOf(id);
1084
- return t > 0 && formsById(id)[t - 1].kind === "rail" && formsById(id)[t - 1].title === true ? t - 1 : -1;
1085
- };
1086
- const titles = railModeOf(scene, side) === "titles" && members.some((m) => titleOf(m.id) >= 0) && members.every((m) => tileOf(m.id) >= 0);
1087
- let fit;
1088
- if (titles) {
1089
- const input = {
1090
- axis: "column",
1091
- length: railRect.height,
1092
- cross: { min: railRect.width, max: railRect.width, rail: railRect.width },
1093
- titles: true,
1094
- items: members.map(({ id }) => {
1095
- const it = scene.items[id];
1096
- const p = finite(it.priority ?? 0, 0);
1097
- const forms = formsById(id).slice(0, tileOf(id) + 1).map((f) => p === 0 ? f : { ...f, rank: finite(f.rank ?? 0, 0) + p });
1098
- return { id, forms, used: finite(it.used, 0), ...it.kept ? { kept: true } : {}, from: titleOf(id) >= 0 ? titleOf(id) : tileOf(id) };
1099
- })
1100
- };
1101
- const fresh = members.some((m) => d.fresh.has(m.id) || d.fresh.has(m.c.edge.id));
1102
- const previous = d.previous && !fresh ? d.previous.rails[side]?.stack : void 0;
1103
- fit = fitStack(input, previous ? { previous } : {});
1104
- }
1105
- const placed = new Map(fit?.items.map((p) => [p.id, p]) ?? []);
1106
- let y = railRect.y;
1107
- for (const { id, c } of members) {
1108
- const it = scene.items[id];
1109
- const forms = formsById(id);
1110
- const ri = tileOf(id);
1111
- const p = placed.get(id);
1112
- const form = p ? p.form : ri >= 0 ? ri : forms.length === 0 ? 0 : startOf(it, forms);
1113
- const at = p ? rect(railRect.x, railRect.y + p.rect.y, railRect.width, p.rect.height) : rect(railRect.x, y, railRect.width, ri >= 0 ? px(forms[ri].main.natural) : railRect.width);
1114
- const title = forms[form]?.title === true;
1115
- items[id] = { rect: at, hidden: forms[form]?.hides ?? [], kind: it.kind, axis: "column", form, formKind: "rail", edge: c.edge.id, zone: zoneOf(it), rail: side, ...title ? { railTitle: side } : {}, folded: g.auto.has(c) ? "auto" : "hand", foldedBy: startOf(it, forms) > 0 ? "hand" : "auto" };
1116
- segments.push({ id, edge: c.edge.id, rect: at });
1117
- y += at.height;
1118
- }
1119
- d.rails[side] = { rect: railRect, segments, ...fit ? { stack: fit } : {} };
1120
- }
1121
-
1122
- // src/scene/solve/sides.ts
1123
- function laySides(d, sideEdges, area2, f) {
1124
- const H = area2.height;
1125
- const reach = insetReach(area2, f);
1126
- const tall = (edge) => H + (edge.outsideInset === true ? reach.up + reach.down : 0);
1127
- const cols = columnsOf(d, sideEdges, area2, tall);
1128
- const g = giveWay(d, cols, area2, f.gutter);
1129
- columnRoom(d, cols, area2, f);
1130
- giveWayNotes(d, g, cols);
1131
- const walk = { x0: area2.x, x1: area2.x + area2.width };
1132
- for (const side of ["left", "right"]) laySide(d, side, cols, g, area2, f, walk, reach.up, tall);
1133
- return rect(walk.x0, area2.y, Math.max(0, walk.x1 - walk.x0), H);
1134
- }
1135
- function forced(d, c, length, w) {
1136
- const edge = { ...c.edge, cross: { ...c.edge.cross, min: w, max: w } };
1137
- const { size: _s, ...plain } = edge;
1138
- return solveEdge(d.scene, plain, d.owned.get(c.edge), length, w, d.prevEdge(c.edge));
1139
- }
1140
- function laySide(d, side, cols, g, area2, f, walk, upInset, tall) {
1141
- const { scene, edges } = d;
1142
- const H = area2.height;
1143
- const outsideSideBar = (c) => c.width > 0 && isOutsideSideBar(scene, c.edge) && !g.railed(c) && !g.riding(c);
1144
- const all = cols.filter((c) => c.side === side);
1145
- const mine = [...all.filter(outsideSideBar), ...all.filter((c) => !outsideSideBar(c))];
1146
- const railed = mine.filter((c) => g.railed(c));
1147
- const riders = mine.filter((c) => g.riding(c));
1148
- const host = mine.find((c) => c.dyn && c.edge.column === true && !c.hand && !g.auto.has(c) && c.width > 0);
1149
- const ridersTop = ridersHeight(d, riders);
1150
- const railW = px(scene.rails?.[side]?.width);
1151
- let railRect = null;
1152
- let gap = px(side === "left" ? f.inset.left : f.inset.right);
1153
- let barLaid = false;
1154
- let railPlace = null;
1155
- const placeRail = () => {
1156
- const lead = barLaid ? gap : 0;
1157
- return side === "left" ? rect(walk.x0 - lead, area2.y, railW, H) : rect(walk.x1 + lead - railW, area2.y, railW, H);
1158
- };
1159
- for (const c of mine) {
1160
- if (!railPlace && !outsideSideBar(c)) {
1161
- railPlace = placeRail();
1162
- if (railed.length > 0) {
1163
- railRect = railPlace;
1164
- const lead = barLaid ? gap : 0;
1165
- const after = barLaid ? Math.max(gap, f.gutter) : f.gutter;
1166
- if (side === "left") walk.x0 += railPlace.width - lead + after;
1167
- else walk.x1 -= railPlace.width - lead + after;
1168
- gap = after;
1169
- }
1170
- }
1171
- if (outsideSideBar(c)) barLaid = true;
1172
- const base = { side: c.edge.side, ring: finite(c.edge.ring, 0), cross: 0, menu: null, items: zoneSorted(scene, c.visible), tabs: [], stack: c.solved.stack, free: c.solved.free };
1173
- if (g.railed(c)) {
1174
- edges[c.edge.id] = { ...base, rect: rect(railRect.x, area2.y, 0, H), folded: g.auto.has(c) ? "auto" : "hand" };
1175
- continue;
1176
- }
1177
- if (g.riding(c)) continue;
1178
- const w = Math.min(g.widths.get(c), Math.max(0, walk.x1 - walk.x0));
1179
- const top = c === host ? ridersTop : 0;
1180
- let solved = top > 0 || Math.abs(w - c.width) > EPS ? forced(d, c, H - top, w) : c.solved;
1181
- let r;
1182
- if (c.edge.outsideInset === true) {
1183
- const y0 = area2.y - upInset;
1184
- const h = tall(c.edge);
1185
- if (side === "left") {
1186
- r = rect(walk.x0 - gap, y0, w, h);
1187
- walk.x0 += w;
1188
- } else {
1189
- r = rect(walk.x1 + gap - w, y0, w, h);
1190
- walk.x1 -= w;
1191
- }
1192
- if (Math.abs(h - H) > EPS || Math.abs(w - c.width) > EPS) solved = forced(d, c, h, w);
1193
- } else if (side === "left") {
1194
- r = rect(walk.x0, area2.y + top, w, H - top);
1195
- walk.x0 += w > 0 ? w + f.gutter : 0;
1196
- if (w > 0) gap = f.gutter;
1197
- } else {
1198
- walk.x1 -= w;
1199
- r = rect(walk.x1, area2.y + top, w, H - top);
1200
- walk.x1 -= w > 0 ? f.gutter : 0;
1201
- if (w > 0) gap = f.gutter;
1202
- }
1203
- emit(d, c.edge, r, solved, w < c.width - EPS ? { shrunk: true } : {});
1204
- }
1205
- if (host && riders.length > 0) layRiders(d, host, riders, area2.y);
1206
- if (railRect) layRail(d, side, railRect, railed, g);
1207
- if (scene.rails?.[side]) d.railPlaces[side] = railPlace ?? placeRail();
1208
- }
1209
-
1210
- // src/scene/solve.ts
1211
- function solve(given, options = {}) {
1212
- checkEdgeIds(given);
1213
- const { view: shown, ignored } = viewOf(given);
1214
- const lay = (caps, gave = []) => layOut(shown, options, caps, gave);
1215
- const first = lay(/* @__PURE__ */ new Map());
1216
- const way = barGiveWay(shown, first, lay);
1217
- const l = way ? lay(way.caps, way.notes) : first;
1218
- return ignored.length === 0 ? l : { ...l, notes: [...l.notes, ...ignored.map((edge) => ({ code: "show-in-ignored", edge }))] };
1219
- }
1220
- var withHeightOf = (area2, over) => rect(over.x, area2.y, over.width, area2.height);
1221
- function layOut(given, options, caps, gave) {
1222
- const d = createDraft(given, options, caps);
1223
- const { scene, order } = d;
1224
- const stage = rect(finite(scene.stage.x, 0), finite(scene.stage.y, 0), px(scene.stage.width), px(scene.stage.height));
1225
- const { inset, docked } = insetOf(d);
1226
- const reserved = order.filter((e) => e.mode !== "overlay");
1227
- const outsideBar = (e) => !isSide(e) && e.span !== "between" && e.outsideInset === true;
1228
- let outer = stage;
1229
- for (const e of reserved) if (outsideBar(e)) outer = cutEdge(d, e, outer, true);
1230
- let area2 = insideInset(outer, inset);
1231
- for (const e of reserved) if (!isSide(e) && e.span !== "between" && !outsideBar(e)) area2 = cutEdge(d, e, area2, true);
1232
- const wide = area2;
1233
- area2 = laySides(d, reserved.filter(isSide), area2, { inset, gutter: px(scene.gutter), docked, outer });
1234
- for (const e of reserved) if (!isSide(e) && e.span === "between") area2 = cutEdge(d, e, area2, true);
1235
- const content = area2;
1236
- let over = content;
1237
- for (const e of order) {
1238
- if (e.mode !== "overlay") continue;
1239
- if (isSide(e) || e.span !== "full") over = cutEdge(d, e, over, true);
1240
- else {
1241
- const across2 = e.outsideInset === true ? outer : wide;
1242
- over = withHeightOf(cutEdge(d, e, rect(across2.x, over.y, across2.width, over.height), true), over);
1243
- }
1244
- }
1245
- for (const n of gave) {
1246
- d.notes.push(n);
1247
- if (n.code === "bar-shrunk" && d.edges[n.edge]) d.edges[n.edge] = { ...d.edges[n.edge], shrunk: true };
1248
- }
1249
- contentNotes(d, content);
1250
- const floats = layFloats(d, stage, content, inset);
1251
- const { items, edges, rails, railPlaces, columnRoom: columnRoom2, notes } = d;
1252
- return { items, edges, content, floats, rails, railPlaces, ...columnRoom2 !== void 0 ? { columnRoom: columnRoom2 } : {}, notes };
1253
- }
1254
-
1255
- // src/scene/float-resize.ts
1256
- var LIMITS = ["min", "max", "region", "docked"];
1257
- var EPS2 = 0.5;
1258
- var TOUCH = 1e-3;
1259
- var fin = (v, d) => Number.isFinite(v) ? v : d;
1260
- function floatObstacles(scene, l, id) {
1261
- const out = [];
1262
- for (const [k, p] of Object.entries(l.items)) if (k !== id && !p.floating) out.push(p.rect);
1263
- for (const e of Object.values(l.edges)) {
1264
- if (axisOf(e.side) === "row" && e.cross > 0) out.push(e.rect);
1265
- if (e.menu) out.push(e.menu.rect);
1266
- }
1267
- for (const c of chromeFor(scene, "floats")) out.push(c);
1268
- return out.filter((r) => r.width > 0 && r.height > 0);
1269
- }
1270
- function obstacleStop(drawn, obstacles, axis, drag, span) {
1271
- const none = drag === "start" ? -Infinity : Infinity;
1272
- if (!drawn || !drag) return none;
1273
- const lo2 = (r) => axis === "x" ? r.x : r.y;
1274
- const len2 = (r) => axis === "x" ? r.width : r.height;
1275
- const across2 = (r) => axis === "x" ? [r.y, r.y + r.height] : [r.x, r.x + r.width];
1276
- let at = none;
1277
- for (const o of obstacles) {
1278
- const [c0, c1] = across2(o);
1279
- if (c0 >= span[1] - TOUCH || c1 <= span[0] + TOUCH) continue;
1280
- const begin = lo2(o);
1281
- const finish = begin + len2(o);
1282
- const edge = drag === "start" ? lo2(drawn) : lo2(drawn) + len2(drawn);
1283
- if (drag === "start") {
1284
- if (finish <= edge + EPS2) at = Math.max(at, finish);
1285
- else if (begin < edge - TOUCH) at = edge;
1286
- } else {
1287
- if (begin >= edge - EPS2) at = Math.min(at, begin);
1288
- else if (finish > edge + TOUCH) at = edge;
1289
- }
1290
- }
1291
- return drag === "start" ? Math.min(at, lo2(drawn)) : Math.max(at, lo2(drawn) + len2(drawn));
1292
- }
1293
- function fitAxis(start, length, b, drag, hit) {
1294
- const { min, lo: lo2, hi, stop } = b;
1295
- const max = Math.max(min, b.max);
1296
- const raw = fin(length, min);
1297
- if (min >= hi - lo2) {
1298
- hit.add("region");
1299
- return [lo2, min];
1300
- }
1301
- const bind = (cands, pick) => {
1302
- const at = cands.reduce((m, [v]) => pick(m, v), cands[0][0]);
1303
- return [at, cands.filter(([v]) => Math.abs(v - at) <= EPS2).map(([, k]) => k)];
1304
- };
1305
- if (drag === "start") {
1306
- const asked2 = fin(start, lo2) + raw;
1307
- const end = Math.min(hi, Math.max(lo2 + min, asked2));
1308
- if (Math.abs(end - asked2) > EPS2) hit.add("region");
1309
- const [down, why2] = bind([[end - max, "max"], [lo2, "region"], [stop, "docked"]], Math.max);
1310
- let from = end - raw;
1311
- if (from > end - min) {
1312
- from = end - min;
1313
- hit.add("min");
1314
- } else if (from < down - TOUCH) {
1315
- from = why2.includes("docked") ? Math.min(down, end) : Math.min(down, end - min);
1316
- for (const k of from === down ? why2 : ["min"]) hit.add(k);
1317
- }
1318
- return [from, end - from];
1319
- }
1320
- const asked = fin(start, lo2);
1321
- const a = Math.max(lo2, Math.min(hi - min, asked));
1322
- if (Math.abs(a - asked) > EPS2) hit.add("region");
1323
- const [up, why] = bind([[a + max, "max"], [hi, "region"], [stop, "docked"]], Math.min);
1324
- let to = a + raw;
1325
- if (to < a + min) {
1326
- to = a + min;
1327
- hit.add("min");
1328
- } else if (to > up + TOUCH) {
1329
- to = why.includes("docked") ? Math.max(up, a) : Math.max(up, a + min);
1330
- for (const k of to === up ? why : ["min"]) hit.add(k);
1331
- }
1332
- return [a, to - a];
1333
- }
1334
- function fitResize(scene, id, rect2, moving) {
1335
- const f = scene.floats.find((x2) => x2.id === id);
1336
- const item = Object.hasOwn(scene.items, id) ? scene.items[id] : void 0;
1337
- if (!f || !item || item.fixed) return null;
1338
- const fo = f.orient ?? floatOrientOf(item);
1339
- const form = formsOf(item, fo)[0];
1340
- if (floatShape(item, f).form !== 0 || form && !form.main.grows) return null;
1341
- const l = solve(scene);
1342
- const drawn = l.items[id]?.rect;
1343
- if (!drawn) return null;
1344
- const min = floatFloor(item, form, fo, drawn);
1345
- const max = { width: fin(item.floatMax?.width, Infinity), height: fin(item.floatMax?.height, Infinity) };
1346
- const region = floatRegion(scene);
1347
- const dragX = moving.left ? "start" : moving.right ? "end" : null;
1348
- const dragY = moving.top ? "start" : moving.bottom ? "end" : null;
1349
- const obstacles = floatObstacles(scene, l, id);
1350
- const hit = /* @__PURE__ */ new Set();
1351
- const [x, width] = fitAxis(rect2.x, rect2.width, { min: min.width, max: max.width, lo: region.x, hi: region.x + region.width, stop: obstacleStop(drawn, obstacles, "x", dragX ?? "end", [drawn.y, drawn.y + drawn.height]) }, dragX, hit);
1352
- const [y, height] = fitAxis(rect2.y, rect2.height, { min: min.height, max: max.height, lo: region.y, hi: region.y + region.height, stop: obstacleStop(drawn, obstacles, "y", dragY ?? "end", [x, x + width]) }, dragY, hit);
1353
- const { anchor: _a, ...plain } = f;
1354
- const placed = placeFloat(item, { ...plain, rect: { x, y, width, height } }, region, obstacles, l.content, scene.stage).fit.rect;
1355
- if (Math.abs(placed.x - x) > EPS2 || Math.abs(placed.y - y) > EPS2) hit.add("docked");
1356
- if (Math.abs(placed.width - width) > EPS2 || Math.abs(placed.height - height) > EPS2) hit.add("min");
1357
- return { rect: placed, limits: LIMITS.filter((k) => hit.has(k)) };
1358
- }
1359
-
1360
- // src/scene/changes.ts
1361
- var withItem = (scene, item) => ({ ...scene, items: { ...scene.items, [item.id]: item } });
1362
- var itemOf = (scene, id) => Object.hasOwn(scene.items, id) ? scene.items[id] : void 0;
1363
- function closeRings(edges, sides) {
1364
- const out = [...edges];
1365
- for (const side of sides) {
1366
- const idx = out.map((e, i) => ({ e, i })).filter((x) => x.e.side === side);
1367
- idx.sort((a, b) => a.e.ring === b.e.ring ? a.i - b.i : a.e.ring < b.e.ring ? -1 : 1);
1368
- idx.forEach((x, ring) => {
1369
- if (x.e.ring !== ring) out[x.i] = { ...x.e, ring };
1370
- });
1371
- }
1372
- return out;
1373
- }
1374
- function prune(scene) {
1375
- const gone = scene.edges.filter((e) => e.column === true && e.items.length === 0);
1376
- if (gone.length === 0) return scene;
1377
- const sides = new Set(gone.map((e) => e.side));
1378
- return { ...scene, edges: closeRings(scene.edges.filter((e) => !gone.includes(e)), sides) };
1379
- }
1380
- function mergeSideColumns(scene) {
1381
- let edges = [...scene.edges];
1382
- const sides = /* @__PURE__ */ new Set();
1383
- for (const side of ["left", "right"]) {
1384
- const cols = edges.filter((e) => e.side === side && isSideColumn(scene, e)).sort((a, b) => a.ring - b.ring);
1385
- if (cols.length < 2) continue;
1386
- const [keep2, ...rest] = cols;
1387
- const merged = { ...keep2, items: [...keep2.items, ...rest.flatMap((e) => e.items)] };
1388
- edges = edges.filter((e) => !rest.includes(e)).map((e) => e === keep2 ? merged : e);
1389
- sides.add(side);
1390
- }
1391
- return sides.size === 0 ? scene : { ...scene, edges: closeRings(edges, sides) };
1392
- }
1393
- function lift(scene, id) {
1394
- const inEdge = scene.edges.some((e) => e.items.includes(id));
1395
- const inFloat = scene.floats.some((f) => f.id === id);
1396
- if (!inEdge && !inFloat) return scene;
1397
- let s = scene;
1398
- const from = scene.edges.find((e) => e.items.includes(id));
1399
- const owner = from ? roomOwner(scene, from, from.items.indexOf(id)) : void 0;
1400
- if (from && owner) {
1401
- const length = lengthOn(scene, from, id);
1402
- if (length > 0) s = withSpace(s, owner, (scene.items[owner].space ?? 0) + (scene.items[id]?.space ?? 0) + length + nonNegative(from.gap));
1403
- }
1404
- return prune({
1405
- ...s,
1406
- edges: inEdge ? s.edges.map((e) => e.items.includes(id) ? { ...e, items: e.items.filter((x) => x !== id) } : e) : s.edges,
1407
- floats: inFloat ? s.floats.filter((f) => f.id !== id) : s.floats
1408
- });
1409
- }
1410
- function roomOwner(scene, edge, k) {
1411
- const id = edge.items[k];
1412
- if (id === void 0) return void 0;
1413
- const zone = zoneAt(scene, edge, id);
1414
- if (zone === "center") return void 0;
1415
- const step = zone === "start" ? 1 : -1;
1416
- for (let j = k + step; j >= 0 && j < edge.items.length; j += step) {
1417
- const nb = edge.items[j];
1418
- if (zoneAt(scene, edge, nb) !== zone || scene.items[nb]?.closed) continue;
1419
- if ((scene.items[nb]?.space ?? 0) > 0) return nb;
1420
- }
1421
- return void 0;
1422
- }
1423
- function lengthOn(scene, edge, id) {
1424
- const it = solve(scene).items[id];
1425
- if (!it || it.inMenu || it.edge !== edge.id) return 0;
1426
- return axisOf(edge.side) === "column" ? it.rect.height : it.rect.width;
1427
- }
1428
- var nonNegative = (n) => Number.isFinite(n) && n > 0 ? n : 0;
1429
- function withSpace(scene, id, space) {
1430
- const { space: _old, ...bare } = scene.items[id];
1431
- return { ...scene, items: { ...scene.items, [id]: space > 1e-6 ? { ...bare, space } : bare } };
1432
- }
1433
- function newColumn(scene, side, kind, ring, items, others) {
1434
- const t = scene.columns?.[side];
1435
- if (!t || !t.accepts.includes(kind)) return null;
1436
- const taken = new Set([...scene.edges, ...others].map((e) => e.id));
1437
- let n = 0;
1438
- while (taken.has(`${side}-${n}`)) n++;
1439
- return columnFromTemplate(t, `${side}-${n}`, side, ring, items);
1440
- }
1441
- function sideRings(edges, side) {
1442
- return new Map(closeRings(edges, /* @__PURE__ */ new Set([side])).filter((e) => e.side === side).map((e) => [e.id, e.ring]));
1443
- }
1444
- function columnFloor(edges, side) {
1445
- const mine = edges.filter((e) => e.side === side).sort((a, b) => a.ring - b.ring);
1446
- let n = 0;
1447
- while (n < mine.length && mine[n].column !== true) n++;
1448
- return n;
1449
- }
1450
- function insertColumn(scene0, id, side, ring, others) {
1451
- const scene = { ...scene0, edges: closeRings(scene0.edges, /* @__PURE__ */ new Set([side])) };
1452
- const kind = scene.items[id].kind;
1453
- const mine = scene.edges.filter((e) => e.side === side);
1454
- const floor = columnFloor(scene.edges, side);
1455
- const r = Math.max(floor, Math.min(Number.isNaN(ring) ? Infinity : Math.floor(ring), mine.length === 0 ? 0 : Math.max(...mine.map((e) => e.ring)) + 1));
1456
- const col = newColumn(scene, side, kind, r, [id], others);
1457
- if (!col) return null;
1458
- if (!zoneTakes(col, "start", kind)) return null;
1459
- const shifted = scene.edges.map((e) => e.side === side && e.ring >= r ? { ...e, ring: e.ring + 1 } : e);
1460
- return { ...scene, edges: closeRings([...shifted, col], /* @__PURE__ */ new Set([side])) };
1461
- }
1462
- var zoneAt = (scene, edge, id) => {
1463
- const it = itemOf(scene, id);
1464
- return it ? zoneIn(edge, it) : "start";
1465
- };
1466
- function insertIndex(scene, edge, zone, at) {
1467
- const rank = { start: 0, center: 1, end: 2 };
1468
- const inZone = edge.items.map((id, i) => ({ id, i })).filter((x) => zoneAt(scene, edge, x.id) === zone);
1469
- const k = Math.min(Math.max(0, Math.floor(Number.isFinite(at) ? at : inZone.length)), inZone.length);
1470
- if (k < inZone.length) return inZone[k].i;
1471
- if (inZone.length > 0) return inZone[inZone.length - 1].i + 1;
1472
- let idx = 0;
1473
- edge.items.forEach((id, i) => {
1474
- if (rank[zoneAt(scene, edge, id)] < rank[zone]) idx = i + 1;
1475
- });
1476
- return idx;
1477
- }
1478
- function moveTo(scene, id, place) {
1479
- return moveAmong(scene, id, place, []);
1480
- }
1481
- function moveAmong(scene, id, place, others) {
1482
- const item = itemOf(scene, id);
1483
- if (!item || item.fixed) return scene;
1484
- if (place.kind === "float") {
1485
- const out = floatAt(scene, id, place.rect);
1486
- const anchor = place.anchor;
1487
- if (!anchor || out === scene) return out;
1488
- return { ...out, floats: out.floats.map((f) => f.id === id ? { ...f, anchor } : f) };
1489
- }
1490
- const railColumn = place.kind === "rail" ? railColumnOf(scene, place.side, item.kind) : void 0;
1491
- if (railColumn) {
1492
- const docked = moveAmong(scene, id, { edge: railColumn.id, at: Infinity }, others);
1493
- return docked === scene ? scene : withItem(docked, { ...docked.items[id], from: Math.max(1, item.from ?? 0) });
1494
- }
1495
- if (place.kind === "column" || place.kind === "rail") {
1496
- if (place.kind === "rail" && !scene.rails?.[place.side]?.accepts.includes(item.kind)) return scene;
1497
- const { space: _space2, ...unspaced2 } = item;
1498
- const folded = place.kind === "rail" ? { ...unspaced2, from: Math.max(1, item.from ?? 0) } : unspaced2;
1499
- const made = insertColumn(withItem(lift(scene, id), folded), id, place.side, place.ring, others);
1500
- return made ?? scene;
1501
- }
1502
- const edge = scene.edges.find((e) => e.id === place.edge);
1503
- if (!edge || edge.hidden === true || !edge.accepts.includes(item.kind)) return scene;
1504
- const zone = place.zone ?? zoneIn(edge, item);
1505
- if (!zoneTakes(edge, zone, item.kind)) return scene;
1506
- const { space: _space, ...unspaced } = item;
1507
- const { align: _drop, ...rest } = unspaced;
1508
- const kept = Number.isFinite(place.space) && place.space > 0 ? { space: place.space } : {};
1509
- const moved = { ...place.zone === void 0 || !namesZone(edge, item.kind) ? unspaced : zone === "start" ? rest : { ...rest, align: zone }, ...kept };
1510
- const lifted = withItem(lift(scene, id), moved);
1511
- const target = lifted.edges.find((e) => e.id === place.edge);
1512
- if (!target) return scene;
1513
- const k = insertIndex(lifted, target, zone, place.at);
1514
- const into = { ...target, items: [...target.items.slice(0, k), id, ...target.items.slice(k)] };
1515
- const placed = { ...lifted, edges: lifted.edges.map((e) => e === target ? into : e) };
1516
- const owner = roomOwner(placed, into, k);
1517
- if (!owner) return placed;
1518
- const length = lengthOn(placed, into, id);
1519
- if (length <= 0) return placed;
1520
- const room = placed.items[owner].space ?? 0;
1521
- return withSpace(placed, owner, room - (placed.items[id].space ?? 0) - length - nonNegative(into.gap));
1522
- }
1523
- function nextSince(scene) {
1524
- const most = scene.floats.reduce((m, f) => Math.max(m, isSince(f.since) ? f.since : 0), 0);
1525
- return Math.min(most + 1, Number.MAX_SAFE_INTEGER);
1526
- }
1527
- function floatAt(scene, id, at, orient2) {
1528
- const item = itemOf(scene, id);
1529
- if (!item || item.fixed || !scene.floatKinds.includes(item.kind)) return scene;
1530
- const was = scene.floats.find((f) => f.id === id);
1531
- const o = orient2 ?? was?.orient;
1532
- const fo = o ?? floatOrientOf(item);
1533
- const size = floatSizeOf(item, fo, was?.orient ?? floatOrientOf(item));
1534
- const since = was ? was.since : nextSince(scene);
1535
- const seq = since !== void 0 ? { since } : {};
1536
- const float = at ? { id, rect: { x: at.x, y: at.y, width: at.width, height: at.height }, ...o ? { orient: o } : {}, ...seq } : { id, rect: { x: 0, y: 0, width: size.width, height: size.height }, anchor: "content-top-right", ...o ? { orient: o } : {}, ...seq };
1537
- const { space: _space, ...unspaced } = item;
1538
- const set = at && at.width > 0 && at.height > 0 ? { orient: fo, size: { width: at.width, height: at.height } } : void 0;
1539
- const next = set || declaresFloatSizes(item) ? withFloatSizes(unspaced, was?.orient ?? floatOrientOf(item), set) : unspaced;
1540
- const lifted = withItem(lift(scene, id), next);
1541
- return { ...lifted, floats: [...lifted.floats, float] };
1542
- }
1543
- function withFloatSizes(item, floatOrient, set) {
1544
- const { floatSize: _legacy, floatSizes: _sizes, ...rest } = item;
1545
- const sizes = { ...floatSizesOf(item, floatOrient), ...set ? { [set.orient]: set.size } : {} };
1546
- return sizes.row || sizes.column ? { ...rest, floatSizes: sizes } : rest;
1547
- }
1548
- function canFold(scene, id) {
1549
- const item = itemOf(scene, id);
1550
- if (!item || item.fixed && !scene.edges.some((e) => e.foldable === true && e.items.includes(id))) return false;
1551
- const edge = placingOf(scene, id).edges.find((e) => e.items.includes(id));
1552
- return axesOf(scene, id).some((a) => foldedIndex(item, formsOf(item, a), edge) >= 0);
1553
- }
1554
- function fold(scene, id, on) {
1555
- const item = itemOf(scene, id);
1556
- if (!item || item.fixed && !scene.edges.some((e) => e.foldable === true && e.items.includes(id))) return scene;
1557
- if (on) return isHandFolded(scene, id) || !canFold(scene, id) ? scene : withItem(scene, { ...item, from: 1 });
1558
- if (item.from === void 0) return scene;
1559
- const { from: _f, ...rest } = item;
1560
- return withItem(scene, rest);
1561
- }
1562
- var nextAge = (scene) => Object.values(scene.items).reduce((m, it) => Math.max(m, Number.isFinite(it.used) ? it.used : 0), 0) + 1;
1563
- function use(scene, id) {
1564
- const item = itemOf(scene, id);
1565
- if (!item) return scene;
1566
- const age = (it) => Number.isFinite(it.used) ? it.used : 0;
1567
- if (Number.isFinite(item.used) && Object.values(scene.items).every((it) => it.id === id || age(it) < item.used)) return scene;
1568
- return withItem(scene, { ...item, used: nextAge(scene) });
1569
- }
1570
- function keep(scene, id, on) {
1571
- const item = itemOf(scene, id);
1572
- if (!item || !!item.kept === on) return scene;
1573
- if (on) return withItem(scene, { ...item, kept: true });
1574
- const { kept: _k, ...rest } = item;
1575
- return withItem(scene, rest);
1576
- }
1577
- function open(scene, id) {
1578
- const item = itemOf(scene, id);
1579
- if (!item || !item.closed) return scene;
1580
- const { closed: _c, ...rest } = item;
1581
- return withItem(scene, rest);
1582
- }
1583
- function close(scene, id) {
1584
- const item = itemOf(scene, id);
1585
- if (!item || item.closed || item.fixed) return scene;
1586
- return withItem(scene, { ...item, closed: true });
1587
- }
1588
- function raise(scene, id) {
1589
- const i = scene.floats.findIndex((f) => f.id === id);
1590
- if (i < 0 || i === scene.floats.length - 1) return scene;
1591
- return { ...scene, floats: [...scene.floats.slice(0, i), ...scene.floats.slice(i + 1), scene.floats[i]] };
1592
- }
1593
- function orient(scene, id, to) {
1594
- const i = scene.floats.findIndex((f2) => f2.id === id);
1595
- const item = scene.items[id];
1596
- const f = scene.floats[i];
1597
- if (!f || !item) return scene;
1598
- const from = f.orient ?? floatOrientOf(item);
1599
- if (from === to) return scene;
1600
- if (!declaresFloatSizes(item)) return { ...scene, floats: scene.floats.map((g, k) => k === i ? { ...g, orient: to } : g) };
1601
- const grows = formsOf(item, from)[0]?.main.grows === true;
1602
- const kept = withFloatSizes(item, from, grows && f.rect.width > 0 && f.rect.height > 0 ? { orient: from, size: { width: f.rect.width, height: f.rect.height } } : void 0);
1603
- const size = drawnFloatSize(kept, { rect: { ...f.rect, ...floatSizeOf(kept, to, from) }, orient: to });
1604
- const turned = { ...f, rect: { x: f.rect.x, y: f.rect.y, width: size.width, height: size.height }, orient: to };
1605
- return withItem({ ...scene, floats: scene.floats.map((g, k) => k === i ? turned : g) }, kept);
1606
- }
1607
- function resize(scene, id, size, moving = { right: true, bottom: true }) {
1608
- if (typeof size === "number") return resizeEdge(scene, id, size);
1609
- const fit = fitResize(scene, id, size, moving);
1610
- const i = scene.floats.findIndex((f) => f.id === id);
1611
- const item = itemOf(scene, id);
1612
- if (!fit || i < 0 || !item) return scene;
1613
- const { anchor: _a, ...plain } = scene.floats[i];
1614
- const float = { ...plain, rect: fit.rect };
1615
- const fo = float.orient ?? floatOrientOf(item);
1616
- return withItem({ ...scene, floats: scene.floats.map((g, k) => k === i ? float : g) }, withFloatSizes(item, fo, { orient: fo, size: { width: fit.rect.width, height: fit.rect.height } }));
1617
- }
1618
- function resizeEdge(scene, edgeId, size) {
1619
- const edge = scene.edges.find((e) => e.id === edgeId);
1620
- if (!edge) return scene;
1621
- if (!Number.isFinite(size)) {
1622
- if (edge.size === void 0) return scene;
1623
- const { size: _s, ...rest } = edge;
1624
- return { ...scene, edges: scene.edges.map((e) => e === edge ? rest : e) };
1625
- }
1626
- return { ...scene, edges: scene.edges.map((e) => e === edge ? { ...e, size } : e) };
1627
- }
1628
- function setRailMode(scene, side, mode) {
1629
- if (side !== "left" && side !== "right" || !RAIL_MODES.includes(mode) || railModeOf(scene, side) === mode) return scene;
1630
- return { ...scene, railModes: { ...scene.railModes, [side]: mode } };
1631
- }
1632
- function setSide(scene, edgeId, side) {
1633
- const edge = scene.edges.find((e) => e.id === edgeId);
1634
- if (!edge || edge.side === side || axisOf(edge.side) !== axisOf(side)) return scene;
1635
- const moved = { ...edge, side, ring: 0 };
1636
- const others = scene.edges.filter((e) => e !== edge).map((e) => e.side === side ? { ...e, ring: e.ring + 1 } : e);
1637
- return { ...scene, edges: closeRings([moved, ...others], /* @__PURE__ */ new Set([edge.side, side])) };
1638
- }
1639
- function homeFor(scene, id, start) {
1640
- for (const e of start.edges) {
1641
- const k = e.items.indexOf(id);
1642
- if (k < 0) continue;
1643
- const own = itemOf(start, id) ?? itemOf(scene, id);
1644
- const found = scene.edges.find((x) => x.id === e.id);
1645
- const here = found && (e.column !== true || found.items.some((x) => e.items.includes(x))) ? found : void 0;
1646
- if (here) {
1647
- const zone = own ? zoneIn(here, own) : "start";
1648
- const now = here.items.filter((x) => x !== id && zoneAt(scene, here, x) === zone);
1649
- const next = e.items.slice(k + 1).find((x) => now.includes(x));
1650
- return { kind: "gap", edge: e.id, zone, at: next ? now.indexOf(next) : now.length };
1651
- }
1652
- if (e.column === true && (e.side === "left" || e.side === "right")) return { kind: "column", side: e.side, ring: sideRings(start.edges, e.side).get(e.id) ?? e.ring };
1653
- return null;
1654
- }
1655
- const f = start.floats.find((x) => x.id === id);
1656
- if (f) return { kind: "float", rect: f.rect, ...f.anchor ? { anchor: f.anchor } : {} };
1657
- return null;
1658
- }
1659
- function dockHome(scene, id, start) {
1660
- const place = homeFor(scene, id, start);
1661
- if (!place) return scene;
1662
- const moved = moveTo(scene, id, place);
1663
- const item = itemOf(moved, id);
1664
- if (!item) return scene;
1665
- const was = itemOf(start, id);
1666
- const from = was?.from;
1667
- let out = item;
1668
- if (was && out.align !== was.align) {
1669
- const { align: _a, ...plain } = out;
1670
- out = was.align === void 0 ? plain : { ...plain, align: was.align };
1671
- }
1672
- if (out.from !== from) {
1673
- const { from: _f, ...plain } = out;
1674
- out = from === void 0 ? plain : { ...plain, from };
1675
- }
1676
- return out === item ? moved : withItem(moved, out);
1677
- }
1678
- function clampFloat(r, stage, drawn = r) {
1679
- const at = clampInto({ x: r.x, y: r.y, width: drawn.width, height: drawn.height }, stage);
1680
- return { x: at.x, y: at.y, width: r.width, height: r.height };
1681
- }
1682
- function drawnFloatSize(item, f) {
1683
- const s = floatShape(item, f);
1684
- return { width: s.width, height: s.height };
1685
- }
1686
-
1687
- // src/scene/store.ts
1688
- var isNum = (x) => typeof x === "number" && Number.isFinite(x);
1689
- var isSize = (x) => isNum(x) && x > 0;
1690
- var isObject = (x) => typeof x === "object" && x !== null && !Array.isArray(x);
1691
- var isAnchor = (x) => FLOAT_ANCHORS.includes(x);
1692
- function anchorOf(v, drop) {
1693
- if (v === void 0) return void 0;
1694
- if (isAnchor(v)) return v;
1695
- if (!isObject(v) || !isAnchor(v.at)) {
1696
- drop?.("");
1697
- return void 0;
1698
- }
1699
- for (const k of Object.keys(v)) if (k !== "at" && k !== "dx" && k !== "dy") drop?.(`:${k}`);
1700
- for (const k of ["dx", "dy"]) if (v[k] !== void 0 && !isNum(v[k])) drop?.(`:${k}`);
1701
- return { at: v.at, ...isNum(v.dx) ? { dx: v.dx } : {}, ...isNum(v.dy) ? { dy: v.dy } : {} };
1702
- }
1703
- var record = () => /* @__PURE__ */ Object.create(null);
1704
- function railModesOf(v, drop) {
1705
- if (v === void 0) return null;
1706
- if (!isObject(v)) {
1707
- drop?.("railModes");
1708
- return null;
1709
- }
1710
- const out = {};
1711
- for (const k of Object.keys(v)) {
1712
- const m = v[k];
1713
- if ((k === "left" || k === "right") && RAIL_MODES.includes(m)) out[k] = m;
1714
- else drop?.(`railModes:${k}`);
1715
- }
1716
- return Object.keys(out).length > 0 ? out : null;
1717
- }
1718
- function floatSizesStored(it) {
1719
- const out = {};
1720
- for (const o of ["row", "column"]) {
1721
- const s = it.floatSizes?.[o];
1722
- if (s && isSize(s.width) && isSize(s.height)) out[o] = { width: s.width, height: s.height };
1723
- }
1724
- return out.row || out.column ? out : void 0;
1725
- }
1726
- function storeScene(scene) {
1727
- const items = record();
1728
- for (const [id, it] of Object.entries(scene.items)) {
1729
- if (id === "__proto__") continue;
1730
- const sizes = floatSizesStored(it);
1731
- items[id] = {
1732
- used: isNum(it.used) ? it.used : 0,
1733
- ...it.kept ? { kept: true } : {},
1734
- ...isNum(it.from) && it.from >= 1 ? { from: Math.floor(it.from) } : {},
1735
- ...it.closed ? { closed: true } : {},
1736
- ...it.align === "center" || it.align === "end" ? { align: it.align } : {},
1737
- ...isNum(it.space) && it.space > 0 ? { space: it.space } : {},
1738
- ...it.floatSize && isSize(it.floatSize.width) && isSize(it.floatSize.height) ? { floatSize: { width: it.floatSize.width, height: it.floatSize.height } } : {},
1739
- ...sizes ? { floatSizes: sizes } : {}
1740
- };
1741
- }
1742
- const modes = railModesOf(scene.railModes);
1743
- return {
1744
- v: 1,
1745
- edges: scene.edges.map((e) => ({ id: e.id, side: e.side, ring: e.ring, items: [...e.items], ...isNum(e.size) ? { size: e.size } : {}, ...e.column === true ? { column: true } : {} })),
1746
- floats: scene.floats.filter((f) => isNum(f.rect.x) && isNum(f.rect.y) && isSize(f.rect.width) && isSize(f.rect.height)).map((f) => {
1747
- const anchor = anchorOf(f.anchor);
1748
- return { id: f.id, x: f.rect.x, y: f.rect.y, width: f.rect.width, height: f.rect.height, ...f.orient ? { orient: f.orient } : {}, ...anchor ? { anchor } : {}, ...isSince(f.since) ? { since: f.since } : {} };
1749
- }),
1750
- items,
1751
- ...modes ? { railModes: modes } : {}
1752
- };
1753
- }
1754
- function readScene(data, base) {
1755
- checkEdgeIds(base);
1756
- try {
1757
- return read(data, base);
1758
- } catch (e) {
1759
- return { scene: base, dropped: [], error: `unreadable: ${e instanceof Error ? e.message : typeof e}` };
1760
- }
1761
- }
1762
- var words = (v) => typeof v === "number" ? String(v) : typeof v === "string" ? JSON.stringify(v.length > 24 ? `${v.slice(0, 24)}\u2026` : v) : typeof v;
1763
- function withFixed(baseIds, stored, isFixed) {
1764
- const out = [...stored];
1765
- const missing = (id) => isFixed(id) && !stored.includes(id);
1766
- let tail = baseIds.length;
1767
- while (tail > 0 && isFixed(baseIds[tail - 1])) tail--;
1768
- baseIds.forEach((id, i) => {
1769
- if (missing(id) && i < tail) out.splice(Math.min(i, out.length), 0, id);
1770
- });
1771
- return [...out, ...baseIds.slice(tail).filter(missing)];
1772
- }
1773
- function read(data, base) {
1774
- if (!isObject(data)) return { scene: base, dropped: [], error: "not an object" };
1775
- if (data.v !== 1) return { scene: base, dropped: [], error: `unknown version ${words(data.v)} (this reads version 1)` };
1776
- const dropped = [];
1777
- const drop = (what) => {
1778
- if (!dropped.includes(what)) dropped.push(what);
1779
- };
1780
- for (const k of Object.keys(data)) if (!["v", "edges", "floats", "items", "railModes"].includes(k)) drop(k);
1781
- const storedModes = railModesOf(data.railModes, drop);
1782
- const railModes = storedModes ? { ...base.railModes, ...storedModes } : base.railModes;
1783
- const known = (id) => typeof id === "string" && id !== "__proto__" && Object.hasOwn(base.items, id);
1784
- const isFixed = (id) => known(id) && base.items[id].fixed === true;
1785
- const items = { ...base.items };
1786
- const legacy = /* @__PURE__ */ new Set();
1787
- if (data.items !== void 0 && !isObject(data.items)) drop("items");
1788
- else if (isObject(data.items)) {
1789
- for (const id of Object.keys(data.items)) {
1790
- const s = data.items[id];
1791
- if (!known(id) || !isObject(s)) {
1792
- drop(`item:${id}`);
1793
- continue;
1794
- }
1795
- const b = base.items[id];
1796
- const { kept: _k, from: _f, closed: _c, align: _a, space: _sp, floatSize: _s, floatSizes: _ss, ...plain } = b;
1797
- let it = { ...plain, used: b.used };
1798
- for (const k of Object.keys(s)) if (!["used", "kept", "from", "closed", "align", "space", "floatSize", "floatSizes"].includes(k)) drop(`item:${id}:${k}`);
1799
- if (isNum(s.used)) it = { ...it, used: s.used };
1800
- else if (s.used !== void 0) drop(`item:${id}:used`);
1801
- if (s.kept === true) it = { ...it, kept: true };
1802
- else if (s.kept !== void 0 && s.kept !== false) drop(`item:${id}:kept`);
1803
- const byHand = !b.fixed || base.edges.some((e) => e.foldable === true && e.items.includes(id));
1804
- if (isNum(s.from) && Number.isInteger(s.from) && s.from >= 1 && byHand) it = { ...it, from: s.from };
1805
- else if (s.from !== void 0) drop(`item:${id}:from`);
1806
- if (!byHand && b.from !== void 0) it = { ...it, from: b.from };
1807
- if (s.closed === true && !b.fixed) it = { ...it, closed: true };
1808
- else if (s.closed !== void 0 && s.closed !== false) drop(`item:${id}:closed`);
1809
- if (s.align === "center" || s.align === "end") it = { ...it, align: s.align };
1810
- else if (s.align !== void 0 && s.align !== "start") drop(`item:${id}:align`);
1811
- if (b.fixed && b.align) it = { ...it, align: b.align };
1812
- if (isNum(s.space) && s.space > 0 && !b.fixed) it = { ...it, space: s.space };
1813
- else if (s.space !== void 0) drop(`item:${id}:space`);
1814
- const fs = s.floatSize;
1815
- if (isObject(fs) && isSize(fs.width) && isSize(fs.height)) {
1816
- it = { ...it, floatSize: { width: fs.width, height: fs.height } };
1817
- legacy.add(id);
1818
- } else if (fs !== void 0) drop(`item:${id}:floatSize`);
1819
- const sizes2 = s.floatSizes;
1820
- if (isObject(sizes2)) {
1821
- const own = {};
1822
- for (const k of Object.keys(sizes2)) {
1823
- const v = sizes2[k];
1824
- if ((k === "row" || k === "column") && isObject(v) && isSize(v.width) && isSize(v.height)) own[k] = { width: v.width, height: v.height };
1825
- else drop(`item:${id}:floatSizes:${k}`);
1826
- }
1827
- if (own.row || own.column) it = { ...it, floatSizes: own };
1828
- } else if (sizes2 !== void 0) drop(`item:${id}:floatSizes`);
1829
- items[id] = it;
1830
- }
1831
- }
1832
- const placed = /* @__PURE__ */ new Set();
1833
- const order = /* @__PURE__ */ new Map();
1834
- const sizes = /* @__PURE__ */ new Map();
1835
- const made = [];
1836
- const rings = /* @__PURE__ */ new Map();
1837
- const moves = /* @__PURE__ */ new Map();
1838
- const anchorMoves = /* @__PURE__ */ new Map();
1839
- const anchorSizes = /* @__PURE__ */ new Map();
1840
- if (data.edges !== void 0 && !Array.isArray(data.edges)) drop("edges");
1841
- for (const e of Array.isArray(data.edges) ? data.edges : []) {
1842
- if (!isObject(e) || typeof e.id !== "string") {
1843
- drop("an edge with no id");
1844
- continue;
1845
- }
1846
- let edge = base.edges.find((x) => x.id === e.id) ?? made.find((x) => x.id === e.id);
1847
- const listed = Array.isArray(e.items) ? e.items : [];
1848
- const anchorEdges = base.edges.filter((b) => b.column !== true && b.accepts.length === 0 && b.id !== edge?.id && b.items.some((id) => isFixed(id) && listed.includes(id)));
1849
- for (const b of anchorEdges) {
1850
- const side = SIDES.includes(e.side) ? e.side : edge?.side;
1851
- if (side && side !== b.side && axisOf(side) === axisOf(b.side)) anchorMoves.set(b.id, side);
1852
- if (isNum(e.size)) anchorSizes.set(b.id, e.size);
1853
- }
1854
- const describesAnchor = anchorEdges.length > 0;
1855
- if (!edge && describesAnchor) continue;
1856
- if (!edge && e.column === true && (e.side === "left" || e.side === "right") && base.columns?.[e.side] && !isReservedEdgeId(e.id)) {
1857
- edge = columnFromTemplate(base.columns[e.side], e.id, e.side, isNum(e.ring) ? e.ring : Infinity, []);
1858
- made.push(edge);
1859
- }
1860
- if (!edge || order.has(edge.id)) {
1861
- drop(`edge:${e.id}`);
1862
- continue;
1863
- }
1864
- if (edge.column === true && isNum(e.ring)) rings.set(edge.id, e.ring);
1865
- if (e.hidden !== void 0) drop(`edge:${edge.id}:hidden`);
1866
- if (e.showIn !== void 0) drop(`edge:${edge.id}:showIn`);
1867
- if (e.side !== void 0 && !SIDES.includes(e.side)) drop(`edge:${e.id}:side`);
1868
- else if (!describesAnchor && edge.column !== true && typeof e.side === "string" && e.side !== edge.side && axisOf(e.side) === axisOf(edge.side)) moves.set(edge.id, e.side);
1869
- const ids = [];
1870
- for (const id of Array.isArray(e.items) ? e.items : []) {
1871
- if (!known(id) || placed.has(id)) drop(`edge:${edge.id}:${String(id)}`);
1872
- else if (isFixed(id)) {
1873
- if (edge.items.includes(id) && base.edges.some((x) => x.id === edge.id)) {
1874
- ids.push(id);
1875
- placed.add(id);
1876
- } else if (!anchorEdges.some((b) => b.items.includes(id))) drop(`edge:${edge.id}:${id}`);
1877
- } else if (!edge.accepts.includes(items[id].kind)) drop(`edge:${edge.id}:${id}`);
1878
- else {
1879
- ids.push(id);
1880
- placed.add(id);
1881
- }
1882
- }
1883
- order.set(edge.id, ids);
1884
- if (describesAnchor) {
1885
- if (!sizes.has(edge.id)) sizes.set(edge.id, edge.size);
1886
- } else if (isNum(e.size)) sizes.set(edge.id, e.size);
1887
- else if (e.size !== void 0) drop(`edge:${edge.id}:size`);
1888
- else sizes.set(edge.id, void 0);
1889
- }
1890
- for (const [id, side] of anchorMoves) moves.set(id, side);
1891
- for (const [id, size] of anchorSizes) sizes.set(id, size);
1892
- const floats = [];
1893
- if (data.floats !== void 0 && !Array.isArray(data.floats)) drop("floats");
1894
- for (const f of Array.isArray(data.floats) ? data.floats : []) {
1895
- if (!isObject(f) || typeof f.id !== "string") {
1896
- drop("a float with no id");
1897
- continue;
1898
- }
1899
- const id = f.id;
1900
- if (!known(id) || placed.has(id) || isFixed(id) || !base.floatKinds.includes(items[id].kind) || !isNum(f.x) || !isNum(f.y) || !isSize(f.width) || !isSize(f.height)) {
1901
- drop(`float:${id}`);
1902
- continue;
1903
- }
1904
- for (const k of Object.keys(f)) if (k === "pinned") drop(`pinned:${id}`);
1905
- else if (!["id", "x", "y", "width", "height", "orient", "anchor", "since"].includes(k)) drop(`float:${id}:${k}`);
1906
- const o = f.orient === "row" || f.orient === "column" ? f.orient : void 0;
1907
- if (f.orient !== void 0 && !o) drop(`float:${id}:orient`);
1908
- const a = anchorOf(f.anchor, (what) => drop(`float:${id}:anchor${what}`));
1909
- const since = isSince(f.since) ? f.since : void 0;
1910
- if (f.since !== void 0 && since === void 0) drop(`float:${id}:since`);
1911
- placed.add(id);
1912
- const r = rect(f.x, f.y, f.width, f.height);
1913
- const drawn = drawnFloatSize(items[id], { rect: r, ...o ? { orient: o } : {} });
1914
- floats.push({ id, rect: clampFloat(r, base.stage, drawn), ...o ? { orient: o } : {}, ...a ? { anchor: a } : {}, ...since !== void 0 ? { since } : {} });
1915
- }
1916
- const edges = [...base.edges, ...made].map((edge) => {
1917
- const stored = order.get(edge.id) ?? [];
1918
- const rest = edge.items.filter((id) => !isFixed(id) && !placed.has(id));
1919
- for (const id of rest) placed.add(id);
1920
- let tail = stored.length;
1921
- while (tail > 0 && isFixed(stored[tail - 1])) tail--;
1922
- const listed = [...stored.slice(0, tail), ...rest, ...stored.slice(tail)];
1923
- const items1 = withFixed(edge.items, listed, isFixed);
1924
- const size = sizes.has(edge.id) ? sizes.get(edge.id) : edge.size;
1925
- const { size: _s, ...plain } = edge;
1926
- return { ...plain, ring: rings.get(edge.id) ?? edge.ring, items: items1, ...size !== void 0 ? { size } : {} };
1927
- });
1928
- for (const f of base.floats) if (!placed.has(f.id)) floats.push(f);
1929
- for (const id of Object.keys(items)) if ((legacy.has(id) || items[id].floatSize !== void 0) && declaresFloatSizes(items[id])) items[id] = withFloatSizes(items[id], floats.find((f) => f.id === id)?.orient ?? floatOrientOf(items[id]));
1930
- let scene = prune({ ...base, ...railModes ? { railModes } : {}, items, edges: closeRings(edges, new Set(made.length > 0 || rings.size > 0 ? ["left", "right"] : [])), floats });
1931
- for (const [id, side] of moves) scene = setSide(scene, id, side);
1932
- return { scene, dropped };
1933
- }
1934
-
1935
- // src/scene/targets/anchor.ts
1936
- var OTHER = { left: "right", right: "left", top: "bottom", bottom: "top" };
1937
- function anchorTargets(scene, dragged, options) {
1938
- const none = { targets: [], refusals: [], lost: [] };
1939
- const prev = { ...options.previous ? { previous: options.previous } : {}, ...options.fresh ? { fresh: options.fresh } : {} };
1940
- const now = solve(scene, prev);
1941
- const me = now.items[dragged];
1942
- const edge = me?.edge ? scene.edges.find((e) => e.id === me.edge) : void 0;
1943
- if (!me || !edge || edge.moves !== "side" || me.floating) return none;
1944
- const homeAfter = () => ({ scene, layout: now });
1945
- const targets2 = [];
1946
- const side = OTHER[edge.side];
1947
- const moved = use(setSide(scene, edge.id, side), dragged);
1948
- const layout = solve(moved, prev);
1949
- const placed = layout.edges[edge.id]?.rect;
1950
- const slot = layout.items[dragged]?.rect;
1951
- if (placed && slot && area(placed) > EPS) {
1952
- const pieces = chromeFor(scene, "targets").reduce((ps, c) => ps.flatMap((p) => subtract(p, rect(c.x, c.y, c.width, c.height))), [placed]).filter((p) => p.width >= 1 && p.height >= 1);
1953
- if (pieces.length > 0) {
1954
- const hit = pieces.reduce((a, p) => area(p) > area(a) ? p : a);
1955
- const result = { scene: moved, layout };
1956
- targets2.push({ id: targetId({ kind: "side", side }), priority: 2, hit, ...pieces.length > 1 ? { hits: pieces } : {}, slot, item: dragged, place: { kind: "side", edge: edge.id, side }, after: () => result });
1957
- }
1958
- }
1959
- targets2.push({ id: targetId({ kind: "home", item: dragged }), priority: 1, hit: me.rect, item: dragged, place: { kind: "home" }, after: homeAfter });
1960
- return { targets: targets2, refusals: [], lost: [] };
1961
- }
1962
-
1963
- // src/scene/targets/kinds.ts
1964
- var RAIL_REACH = 24;
1965
- var MIN_HIT = { across: 12, along: 24 };
1966
- var EDGE_KINDS = {
1967
- bar: { zoneExtent: "thirds", opens: "any", inward: 0, place: DEFAULT_HIT_BAND / 2, phase: null, layer: 10, overreach: 30, priority: 2, reach: Infinity, virtual: false, minHit: MIN_HIT },
1968
- sideBar: { zoneExtent: "thirds", opens: "any", inward: 0, place: DEFAULT_HIT_BAND / 2, phase: null, layer: 11, overreach: 31, priority: 2, reach: Infinity, virtual: false, minHit: MIN_HIT },
1969
- column: { zoneExtent: "solved", opens: "start", inward: 0, place: DEFAULT_HIT_BAND / 2, phase: null, layer: 12, overreach: 32, priority: 2, reach: Infinity, virtual: false, minHit: MIN_HIT },
1970
- barBetween: { zoneExtent: "thirds", opens: "any", inward: 0, place: DEFAULT_HIT_BAND / 2, phase: null, layer: 13, overreach: 33, priority: 2, reach: Infinity, virtual: false, minHit: MIN_HIT },
1971
- rail: { zoneExtent: "thirds", opens: "any", inward: RAIL_REACH, place: DEFAULT_HIT_BAND / 2, phase: null, layer: 14, overreach: 34, priority: 2, reach: Infinity, virtual: false, minHit: MIN_HIT },
1972
- railStop: { zoneExtent: "whole", opens: "any", inward: 0, place: DEFAULT_HIT_BAND / 2, phase: 2, layer: 9, overreach: 39, priority: 1, reach: Infinity, virtual: false, minHit: MIN_HIT },
1973
- columnLine: { zoneExtent: "whole", opens: "any", inward: 0, place: DEFAULT_HIT_BAND / 2, edgePlace: DEFAULT_HIT_BAND, phase: 2, layer: 21, overreach: 41, priority: 2, reach: COLUMN_REACH, virtual: true, minHit: MIN_HIT }
1974
- };
1975
- function edgeKindOf(scene, e) {
1976
- if (e.compact) return "rail";
1977
- if (e.side === "top" || e.side === "bottom") return e.span === "between" ? "barBetween" : "bar";
1978
- return isSideBar(scene, e) ? "sideBar" : "column";
1979
- }
1980
- var opensEmpty = (scene, edge, kind) => EDGE_KINDS[edgeKindOf(scene, edge)].opens === "any" || zoneTakes(edge, "start", kind);
1981
-
1982
- // src/scene/targets/edges.ts
1983
- function reservedRect(scene, l, id) {
1984
- const it = l.items[id];
1985
- const e = it.edge ? l.edges[it.edge] : void 0;
1986
- const f = e ? formsOf(scene.items[id], axisOf(e.side))[it.form] : void 0;
1987
- if (!e || !f?.main.grows) return it.rect;
1988
- const column = axisOf(e.side) === "column";
1989
- const len2 = column ? it.rect.height : it.rect.width;
1990
- const keep2 = Math.min(len2, Math.max(0, Number.isFinite(f.main.natural) ? f.main.natural : len2));
1991
- const shift = it.zone === "end" ? len2 - keep2 : it.zone === "center" ? (len2 - keep2) / 2 : 0;
1992
- 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);
1993
- }
1994
- function slotAt(c, zone, line, id, place, lands, home = false) {
1995
- const result = c.settle(use(moveAmong(c.scene, c.dragged, place, c.others), c.dragged));
1996
- const isHome = home || c.isHome(result);
1997
- return { zone, line, id, place, result, home: isHome, ok: isHome || !!result.layout.items[c.dragged] && lands(result) && c.sound(result.layout) };
1998
- }
1999
- function distinctSlots(c, slots) {
2000
- const landing = (s) => s.result.layout.items[c.dragged]?.rect;
2001
- const sameMove = (a, b) => {
2002
- if (a.zone !== b.zone) return false;
2003
- if (a.home && b.home) return true;
2004
- const [p, q] = [landing(a), landing(b)];
2005
- const sameLine = a.place.kind === "rail" && b.place.kind === "rail" || Math.abs(a.line - b.line) < EPS;
2006
- return sameLine && (!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);
2007
- };
2008
- const wins = (o, j, s, i) => o.home !== s.home ? o.home : j < i;
2009
- return slots.filter((s, i) => !slots.some((o, j) => j !== i && sameMove(o, s) && wins(o, j, s, i)));
2010
- }
2011
- function zoneSpans(scene, seen, edge, reach, probe) {
2012
- const el = seen.edges[edge.id];
2013
- const column = axisOf(edge.side) === "column";
2014
- const main0 = column ? el.rect.y : el.rect.x;
2015
- const length = column ? el.rect.height : el.rect.width;
2016
- const thirds = EDGE_KINDS[edgeKindOf(scene, edge)].zoneExtent === "thirds";
2017
- const at = (r) => column ? [r.y, r.y + r.height] : [r.x, r.x + r.width];
2018
- const declared = new Set(edge.zoneAccepts ? ZONES.filter((z) => edge.zoneAccepts[z] !== void 0) : ["start"]);
2019
- const placed = /* @__PURE__ */ new Map();
2020
- for (const [id, it] of Object.entries(seen.items)) {
2021
- if (it.edge !== edge.id || it.inMenu) continue;
2022
- const z = it.zone ?? zoneOf(scene.items[id]);
2023
- const [a, b] = at(thirds && it.fixed ? reservedRect(scene, seen, id) : it.rect);
2024
- const was = placed.get(z);
2025
- placed.set(z, was ? [Math.min(was[0], a), Math.max(was[1], b)] : [a, b]);
2026
- declared.add(z);
2027
- }
2028
- const lo2 = (z) => placed.get(z)?.[0] ?? Infinity;
2029
- const hi = (z) => placed.get(z)?.[1] ?? -Infinity;
2030
- if (edge.zones !== void 0) {
2031
- const has = zonesOf(edge);
2032
- let prev = main0 - reach;
2033
- return has.map((zone, i) => {
2034
- const last = i === has.length - 1;
2035
- const mark = main0 + length * (i + 1) / has.length;
2036
- const b = last ? main0 + length + reach : Math.max(prev, Math.min(Math.max(mark, ...has.slice(0, i + 1).map(hi)), ...has.slice(i + 1).map(lo2)));
2037
- const span = { zone, a: prev, b };
2038
- prev = b;
2039
- return span;
2040
- });
2041
- }
2042
- if (thirds) {
2043
- const nearest = scene.barZones === "nearest";
2044
- const landing = (z, side) => placed.has(z) ? side === "after" ? [hi(z), hi(z)] : [lo2(z), lo2(z)] : probe(z);
2045
- const between = (l, r, fallback) => {
2046
- const a = landing(l, "after");
2047
- const b = landing(r, "before");
2048
- return nearest && a && b ? (a[1] + b[0]) / 2 : fallback;
2049
- };
2050
- const b1 = Math.min(Math.max(between("start", "center", main0 + length / 3), hi("start")), lo2("center"), lo2("end"));
2051
- const b2 = Math.max(Math.min(between("center", "end", main0 + 2 * length / 3), lo2("end")), hi("center"), hi("start"), b1);
2052
- return [
2053
- { zone: "start", a: main0 - reach, b: b1 },
2054
- { zone: "center", a: b1, b: b2 },
2055
- { zone: "end", a: b2, b: main0 + length + reach }
2056
- ];
2057
- }
2058
- const anchor = { start: main0, center: main0 + length / 2, end: main0 + length };
2059
- const spans = ZONES.filter((z) => declared.has(z)).map((zone) => ({ zone, s: placed.get(zone) ?? probe(zone) ?? [anchor[zone], anchor[zone]] }));
2060
- return spans.map(({ zone, s }, i) => ({
2061
- zone,
2062
- a: i === 0 ? main0 - reach : (spans[i - 1].s[1] + s[0]) / 2,
2063
- b: i === spans.length - 1 ? main0 + length + reach : (s[1] + spans[i + 1].s[0]) / 2
2064
- }));
2065
- }
2066
- function edgeSlots(c, edge, accepts) {
2067
- const { scene, rest, now, base, dragged, kind } = c;
2068
- const el = now.edges[edge.id];
2069
- const column = axisOf(edge.side) === "column";
2070
- const at = (r) => column ? r.y : r.x;
2071
- const len2 = (r) => column ? r.height : r.width;
2072
- const main0 = at(el.rect);
2073
- const length = len2(el.rect);
2074
- const members0 = rest.edges.find((e) => e.id === edge.id)?.items ?? [];
2075
- const slotId = (zone, k) => targetId(members0.length === 0 ? { kind: "strip", edge: edge.id, zone } : { kind: "gap", edge: edge.id, zone, at: k });
2076
- const firstKind = formsOf(scene.items[dragged], axisOf(edge.side))[0]?.kind;
2077
- 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);
2078
- const lands = ({ layout: after }) => {
2079
- const me = after.items[dragged];
2080
- if (me.edge !== edge.id || me.inMenu) return false;
2081
- if (titles.length === 0) return true;
2082
- if (formsOf(scene.items[dragged], axisOf(edge.side))[me.form]?.kind !== firstKind) return false;
2083
- return edge.items.every((id) => {
2084
- const a = after.items[id];
2085
- const b = base.items[id];
2086
- if (!a || !b || b.edge !== edge.id || id === dragged) return true;
2087
- if (!titles.includes(id)) return a.form === b.form && !!a.inMenu === !!b.inMenu;
2088
- const f = formsOf(scene.items[id], axisOf(edge.side))[a.form];
2089
- return !f || (column ? a.rect.height : a.rect.width) >= formMain(f, "natural", after.edges[edge.id].cross) - 0.5;
2090
- });
2091
- };
2092
- const pad = { start: Math.max(0, edge.pad?.start ?? 0), end: Math.max(0, edge.pad?.end ?? 0) };
2093
- const menuLen = el.menu ? len2(el.menu.rect) : 0;
2094
- const occupied = (id) => now.items[id]?.fixed ? reservedRect(scene, now, id) : now.items[id].rect;
2095
- const shown = (id) => now.items[id]?.edge === edge.id && !now.items[id].inMenu;
2096
- const drawnOf2 = (z) => members0.filter((id) => zoneOf(rest.items[id]) === z && shown(id)).map(occupied);
2097
- const startEnd = (() => {
2098
- const s = drawnOf2("start");
2099
- return s.length ? at(s[s.length - 1]) + len2(s[s.length - 1]) : main0 + pad.start;
2100
- })();
2101
- const endStart = (() => {
2102
- const e = drawnOf2("end");
2103
- return e.length ? at(e[0]) : main0 + length - pad.end - menuLen;
2104
- })();
2105
- const anchorLine = { start: main0 + pad.start, center: Math.min(Math.max(main0 + length / 2, startEnd), Math.max(startEnd, endStart)), end: main0 + length - pad.end - menuLen };
2106
- const slots = [];
2107
- const aimed = (id) => shown(id) && !now.items[id].fixed;
2108
- let earlier = 0;
2109
- for (const zone of ZONES) {
2110
- const members = members0.filter((id) => zoneOf(rest.items[id]) === zone);
2111
- const before0 = earlier;
2112
- earlier += members.filter(aimed).length;
2113
- if (!accepts || !zoneTakes(edge, zone, kind)) continue;
2114
- const fixedHere = (id) => !!base.items[id]?.fixed && base.items[id].edge === edge.id;
2115
- const fromAnchor = (zone === "start" ? members : zone === "end" ? [...members].reverse() : []).filter((id) => fixedHere(id) || shown(id));
2116
- const firstMovable = fromAnchor.findIndex((id) => !fixedHere(id));
2117
- const anchored2 = new Set(firstMovable < 0 ? fromAnchor : fromAnchor.slice(0, firstMovable));
2118
- const pushable = (id) => fixedHere(id) && scene.items[id]?.pushable === true;
2119
- const isProtected = (id) => fixedHere(id) && (zone === "center" || zoneOf(rest.items[id]) !== zone || anchored2.has(id) && !pushable(id));
2120
- for (let k = 0; k <= members.length; k++) {
2121
- const next = members.slice(k).find(shown);
2122
- const before = members.slice(0, k).reverse().find(shown);
2123
- if (before && next && pushable(before) && pushable(next)) continue;
2124
- const line = next ? at(occupied(next)) : before ? at(occupied(before)) + len2(occupied(before)) : anchorLine[zone];
2125
- const s = { ...slotAt(c, zone, line, slotId(zone, k), { kind: "gap", edge: edge.id, zone, at: k }, lands), order: before0 + members.slice(0, k).filter(aimed).length };
2126
- const movesFixed = members0.some((id) => {
2127
- const b = base.items[id];
2128
- const a = s.result.layout.items[id];
2129
- if (fixedHere(id) && !a) return true;
2130
- return isProtected(id) && (Math.abs(a.rect.x - b.rect.x) > 0.5 || Math.abs(a.rect.y - b.rect.y) > 0.5);
2131
- });
2132
- if (!movesFixed) slots.push(s);
2133
- }
2134
- }
2135
- return slots;
2136
- }
2137
- function thickerThan(forms, cross) {
2138
- const shown = forms.filter((f) => f.kind !== "menu");
2139
- return shown.length > 0 && shown.every((f) => !formSetsCross(f) && formCrossMin(f) > cross + EPS);
2140
- }
2141
- var roomReason = (spec, zone) => spec.refuse(zone) === "too-thick" ? "too-thick" : "no-room";
2142
- function edgeSpec(c, edge) {
2143
- const { scene, rest, now, dragged, kind } = c;
2144
- const el = now.edges[edge.id];
2145
- if (!el || el.folded || el.rides || !rest.edges.some((e) => e.id === edge.id)) return null;
2146
- const edgeKind = edgeKindOf(scene, edge);
2147
- const data = EDGE_KINDS[edgeKind];
2148
- const column = axisOf(edge.side) === "column";
2149
- const at = (r2) => column ? r2.y : r2.x;
2150
- const len2 = (r2) => column ? r2.height : r2.width;
2151
- const empty = el.stack.items.every((p) => p.kind === "menu") && !el.menu;
2152
- const accepts = edgeTakes(edge, kind);
2153
- if (!accepts && (empty || area(el.rect) <= EPS)) return null;
2154
- if (accepts && empty && !opensEmpty(scene, edge, kind)) return null;
2155
- const ownNow = now.items[dragged];
2156
- const ownHere = ownNow && ownNow.edge === edge.id && !ownNow.floating && area(ownNow.rect) > EPS ? ownNow.rect : null;
2157
- const oneEnd = scene.foldedColumnDrop === "end" && column && el.tabs.length > 0 && ownNow?.edge !== edge.id && zoneTakes(edge, "start", kind);
2158
- const allSlots = edgeSlots(c, edge, accepts);
2159
- const slots = oneEnd ? allSlots.filter((s) => s.zone === "start").sort((a, b) => a.line - b.line).slice(-1) : allSlots;
2160
- const spanOf = (r2) => [at(r2), at(r2) + len2(r2)];
2161
- const grows = (id) => !!formsOf(scene.items[id], axisOf(edge.side))[now.items[id].form]?.main.grows;
2162
- const reserved = Object.entries(now.items).filter(([id, p]) => p.edge === edge.id && p.fixed && !p.inMenu && !grows(id)).map(([, p]) => spanOf(p.rect));
2163
- const cards = Object.entries(now.items).filter(([id, p]) => id !== dragged && p.edge === edge.id && !p.fixed && !p.inMenu && !p.floating && area(p.rect) > EPS).map(([, p]) => spanOf(p.rect));
2164
- const zones = oneEnd ? [{ zone: "start", a: at(el.rect) - c.reach, b: at(el.rect) + len2(el.rect) + c.reach }] : zoneSpans(scene, now, edge, c.reach, (zone) => {
2165
- const s = slots.find((x) => x.zone === zone && x.place.kind === "gap" && x.place.at === 0);
2166
- const me = s?.result.layout.items[dragged];
2167
- return me && me.edge === edge.id && me.zone === zone ? spanOf(me.rect) : null;
2168
- });
2169
- const r = el.rect;
2170
- const strip = Math.max(0, Number.isFinite(edge.strip) ? edge.strip : DEFAULT_STRIP);
2171
- const outer = edge.side === "left" ? r.x : edge.side === "right" ? r.x + r.width : edge.side === "top" ? r.y : r.y + r.height;
2172
- const inward = (depth) => edge.side === "left" || edge.side === "top" ? [outer, outer + depth] : [outer - depth, outer];
2173
- const thin = edgeKind === "rail" || edgeKind === "bar" || edgeKind === "barBetween";
2174
- const landed = el.cross > 0 || !thin ? 0 : Math.max(0, ...slots.map((s) => s.result.layout.edges[edge.id]?.cross ?? 0));
2175
- const across2 = (after) => inward((el.cross > 0 ? Math.max(el.cross, after?.edges[edge.id]?.cross ?? 0) : Math.max(strip, landed)) + data.inward);
2176
- const drawn = el.cross > 0;
2177
- const marker = (s) => {
2178
- const slot = s.result.layout.items[dragged].rect;
2179
- const e = s.result.layout.edges[edge.id];
2180
- return column ? rect(e.rect.x, slot.y, e.cross, 0) : rect(slot.x, e.rect.y, 0, e.cross);
2181
- };
2182
- const takes = (zone) => accepts && zoneTakes(edge, zone, kind);
2183
- const tooThick = thickerThan(formsOf(scene.items[dragged], axisOf(edge.side)), el.stack.cross);
2184
- return {
2185
- id: edge.id,
2186
- edge,
2187
- kind: edgeKind,
2188
- lay: c.lay.get(edge.id),
2189
- vertical: column,
2190
- zones,
2191
- slots,
2192
- refuse: (zone) => takes(zone) ? tooThick ? "too-thick" : "no-room" : accepts || area(el.rect) > EPS ? "not-accepted" : null,
2193
- own: ownHere,
2194
- holds: !!ownNow && ownNow.edge === edge.id && !ownNow.floating,
2195
- reserved,
2196
- cards,
2197
- across: across2,
2198
- owned: inward(drawn ? el.cross : landed > 0 ? landed : Math.min(strip, data.place)),
2199
- ...!drawn && landed > Math.min(strip, data.place) ? { thinOwned: inward(Math.min(strip, data.place)) } : {},
2200
- ownedAlong: null,
2201
- drawn,
2202
- marker
2203
- };
2204
- }
2205
-
2206
- // src/scene/targets/filter.ts
2207
- function placeKey(s, id) {
2208
- const it = Object.hasOwn(s.items, id) ? s.items[id] : void 0;
2209
- const byZone = (items) => ZONES.flatMap((z) => items.filter((x) => zoneOf(s.items[x]) === z));
2210
- return JSON.stringify({
2211
- edges: s.edges.map((e) => JSON.stringify([e.side, e.ring, e.column === true ? "" : e.id, byZone(e.items), e.size ?? null])).sort(),
2212
- floats: s.floats.map((f) => [f.id, f.rect, f.anchor ?? null, f.orient ?? null]),
2213
- folded: isHandFolded(s, id),
2214
- align: it?.align ?? "start"
2215
- });
2216
- }
2217
- function soundness(scene, base, dragged) {
2218
- const chrome = chromeFor(scene, "targets");
2219
- const noteKey = (n) => "edge" in n && (n.code === "overflow" || n.code === "too-wide") ? `${n.edge}:${n.code}:${"id" in n ? n.id : ""}` : null;
2220
- const hadNotes = new Set(base.notes.map(noteKey).filter((k) => k !== null));
2221
- return (after) => {
2222
- if (after.notes.some((n) => {
2223
- const k = noteKey(n);
2224
- return k !== null && !hadNotes.has(k);
2225
- })) return false;
2226
- for (const [id, b] of Object.entries(base.items)) {
2227
- if (!b.fixed) continue;
2228
- const a = after.items[id];
2229
- const e = a?.edge ? after.edges[a.edge] : void 0;
2230
- if (!a || !e || a.form !== b.form || a.formKind !== b.formKind) return false;
2231
- const f = formsOf(scene.items[id], axisOf(e.side))[a.form];
2232
- const len2 = axisOf(e.side) === "column" ? a.rect.height : a.rect.width;
2233
- if (f && len2 < formMain(f, "min", e.cross) - 0.5) return false;
2234
- }
2235
- const me = after.items[dragged];
2236
- if (!me) return true;
2237
- const fixedAfter = Object.entries(after.items).filter(([id, it]) => it.fixed && id !== dragged).map(([id]) => reservedRect(scene, after, id));
2238
- return !chrome.some((c) => overlaps(me.rect, c)) && !fixedAfter.some((f) => overlaps(me.rect, f));
2239
- };
2240
- }
2241
- function context(scene, dragged, options, others = []) {
2242
- const item = Object.hasOwn(scene.items, dragged) ? scene.items[dragged] : void 0;
2243
- if (!item || item.fixed || item.closed) return null;
2244
- const prev = { ...options.previous ? { previous: options.previous } : {}, ...options.fresh ? { fresh: options.fresh } : {} };
2245
- const settle2 = (s) => ({ scene: s, layout: solve(s, prev) });
2246
- const now = solve(scene, prev);
2247
- const rest = lift(scene, dragged);
2248
- const base = solve(rest, prev);
2249
- const here = placeKey(prune(scene), dragged);
2250
- const c = {
2251
- scene,
2252
- others,
2253
- rest,
2254
- now,
2255
- base,
2256
- dragged,
2257
- kind: item.kind,
2258
- reach: Math.max(0, Number.isFinite(options.endReach) ? options.endReach : 0),
2259
- settle: settle2,
2260
- isHome: (r) => placeKey(r.scene, dragged) === here,
2261
- sound: soundness(scene, base, dragged),
2262
- lay: layKeys(scene)
2263
- };
2264
- return { c, item, prev };
2265
- }
2266
-
2267
- // src/scene/targets/model.ts
2268
- 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]);
2269
-
2270
- // src/scene/targets/ownership.ts
2271
- var bandOf = (s) => {
2272
- const a = Math.min(...s.zones.map((z) => z.a));
2273
- const b = Math.max(...s.zones.map((z) => z.b));
2274
- 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]);
2275
- };
2276
- var ownedRectOf = (s) => {
2277
- const b = bandOf(s);
2278
- if (!s.ownedAlong) return b;
2279
- const [a0, a1] = s.ownedAlong;
2280
- return s.vertical ? rect(b.x, a0, b.width, a1 - a0) : rect(a0, b.y, a1 - a0, b.height);
2281
- };
2282
- function ownedRectsOf(s, along2) {
2283
- if (along2.length === 0) return [];
2284
- const o = ownedRectOf(s);
2285
- if (s.ownedAlong) return [o];
2286
- return along2.map(([a, b]) => s.vertical ? rect(o.x, a, o.width, b - a) : rect(a, o.y, b - a, o.height));
2287
- }
2288
- function drawnOf(c, live) {
2289
- return Object.entries(c.now.edges).filter(([id, e]) => area(e.rect) > EPS && c.lay.has(id)).map(([id, e]) => ({ spec: live.find((s) => s.edge?.id === id) ?? null, rect: e.rect, lay: c.lay.get(id) }));
2290
- }
2291
- function homeRectOf(c, ownSpec) {
2292
- const own = c.now.items[c.dragged];
2293
- if (!own || own.floating || !own.edge) return null;
2294
- const o = own.rect;
2295
- const hull3 = (a, b) => rect(Math.min(a.x, b.x), Math.min(a.y, b.y), Math.max(a.x + a.width, b.x + b.width) - Math.min(a.x, b.x), Math.max(a.y + a.height, b.y + b.height) - Math.min(a.y, b.y));
2296
- if (own.inMenu) return o;
2297
- if (!ownSpec) {
2298
- const e = c.now.edges[own.edge];
2299
- const vanishes = !c.rest.edges.some((x) => x.id === own.edge);
2300
- return e && vanishes && !e.folded && !e.rides && area(e.rect) > EPS ? hull3(o, e.rect) : o;
2301
- }
2302
- const [c0, c1] = ownSpec.across();
2303
- return ownSpec.vertical ? hull3(o, rect(c0, o.y, c1 - c0, o.height)) : hull3(o, rect(o.x, c0, o.width, c1 - c0));
2304
- }
2305
- function ownersOf(c, live, drawn, alongOf) {
2306
- const ownEdge = c.now.items[c.dragged]?.edge;
2307
- const ownSpec = ownEdge && live.find((s) => s.edge?.id === ownEdge) || null;
2308
- const homeRect = homeRectOf(c, ownSpec);
2309
- return [
2310
- // Home first: its tile is its own, for every edge but its own (which tiles it as its own interval).
2311
- ...homeRect ? [{ spec: ownSpec, rect: homeRect, lay: { phase: -1, side: "", at: -Infinity }, place: false }] : [],
2312
- // Every edge the tiler reads, along each zone with a slot the filter keeps: what it owns, drawn or a band of its
2313
- // place (a column line's band about its line). A zone whose every slot is refused owns nothing against the others
2314
- // (its drawn rect still counts, below).
2315
- ...live.flatMap((s) => {
2316
- const rects = ownedRectsOf(s, alongOf(s));
2317
- if (!s.thinOwned) return rects.map((r) => ({ spec: s, rect: r, lay: s.lay, place: !s.drawn }));
2318
- const thin = (r) => s.vertical ? rect(Math.max(r.x, s.thinOwned[0]), r.y, Math.min(r.x + r.width, s.thinOwned[1]) - Math.max(r.x, s.thinOwned[0]), r.height) : rect(r.x, Math.max(r.y, s.thinOwned[0]), r.width, Math.min(r.y + r.height, s.thinOwned[1]) - Math.max(r.y, s.thinOwned[0]));
2319
- return rects.flatMap((r) => [{ spec: s, rect: thin(r), lay: s.lay, place: true }, ...subtract(r, thin(r)).map((d) => ({ spec: s, rect: d, lay: s.lay, place: true, deep: true }))]);
2320
- }).filter((o) => area(o.rect) > EPS),
2321
- // Every seen rect: the edges the tiler doesn't read (folded, riding, taking nothing) among them.
2322
- ...drawn.map((d) => ({ ...d, place: false }))
2323
- ];
2324
- }
2325
- function ownership(c, live, alongOf) {
2326
- const { scene, now, dragged } = c;
2327
- const drawn = drawnOf(c, live);
2328
- const owners = ownersOf(c, live, drawn, alongOf);
2329
- const wins = (o, s) => {
2330
- if (o.spec === s) return false;
2331
- if (pushes(o.lay, s.lay)) return true;
2332
- if (pushes(s.lay, o.lay)) return false;
2333
- const place = !s.drawn;
2334
- return o.place === place ? !place || o.lay.at < s.lay.at : !o.place;
2335
- };
2336
- const takingRects = (s) => s.drawn ? s.zones.filter((z) => s.slots.some((sl) => sl.ok && sl.zone === z.zone)).map((z) => s.vertical ? rect(s.owned[0], z.a, s.owned[1] - s.owned[0], z.b - z.a) : rect(z.a, s.owned[0], z.b - z.a, s.owned[1] - s.owned[0])) : [];
2337
- const thinRectOf = (s) => {
2338
- const b = bandOf(s);
2339
- const [t0, t1] = s.thinOwned;
2340
- return s.vertical ? rect(t0, b.y, t1 - t0, b.height) : rect(b.x, t0, b.width, t1 - t0);
2341
- };
2342
- const taken = new Map(
2343
- live.map((s) => [
2344
- s,
2345
- [
2346
- ...owners.filter((o) => wins(o, s)).flatMap((o) => o.deep ? takingRects(s).reduce((acc, t) => acc.flatMap((r) => subtract(r, t)), [o.rect]) : [o.rect]),
2347
- ...EDGE_KINDS[s.kind].virtual ? owners.filter((o) => o.spec !== s && !wins(o, s)).flatMap((o) => subtract(o.rect, ownedRectOf(s))) : [],
2348
- ...s.thinOwned ? live.filter((d) => d !== s).flatMap((d) => takingRects(d).flatMap((t) => subtract(t, thinRectOf(s)))) : []
2349
- ]
2350
- ])
2351
- );
2352
- const halfBandOf = (e) => {
2353
- const bandOpt = hitBandOf(scene, e);
2354
- return bandOpt === "middle" ? null : Math.max(0, Number.isFinite(bandOpt) ? bandOpt : DEFAULT_HIT_BAND) / 2;
2355
- };
2356
- const bodySpans = (e) => {
2357
- const half = e ? halfBandOf(e) : null;
2358
- if (!e || half === null) return [];
2359
- const column = axisOf(e.side) === "column";
2360
- return Object.entries(now.items).filter(([iid, p]) => p.edge === e.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);
2361
- };
2362
- const rooms = /* @__PURE__ */ new Map();
2363
- const cutsOf = (s) => {
2364
- let room = rooms.get(s);
2365
- if (!room) rooms.set(s, room = roomOf2(s));
2366
- return room;
2367
- };
2368
- const roomOf2 = (s) => {
2369
- const b = bandOf(s);
2370
- const along2 = (r) => s.vertical ? [r.y, r.y + r.height] : [r.x, r.x + r.width];
2371
- const across2 = (r) => s.vertical ? [r.x, r.x + r.width] : [r.y, r.y + r.height];
2372
- const rs = taken.get(s).filter((r) => overlaps(r, b));
2373
- const ends = [.../* @__PURE__ */ new Set([...along2(b), ...rs.flatMap(along2)])].filter((x) => x >= along2(b)[0] && x <= along2(b)[1]).sort((p, q) => p - q);
2374
- const owned = [];
2375
- for (let k = 1; k < ends.length; k++) {
2376
- const [u, w] = [ends[k - 1], ends[k]];
2377
- const over = rs.filter((r) => along2(r)[0] <= u + EPS && along2(r)[1] >= w - EPS).map(across2);
2378
- if (w - u > EPS && less([across2(b)], over).reduce((n, [p, q]) => n + q - p, 0) < 1) owned.push([u, w]);
2379
- }
2380
- const half = s.edge ? halfBandOf(s.edge) : null;
2381
- const raw = bodySpans(s.edge);
2382
- if (half === null || raw.length === 0) return { cuts: owned, bodies: raw };
2383
- const merged = owned.reduce((m, [u, w]) => {
2384
- const last = m[m.length - 1];
2385
- if (last && u <= last[1] + EPS) last[1] = Math.max(last[1], w);
2386
- else m.push([u, w]);
2387
- return m;
2388
- }, []);
2389
- const [lo2, hi] = along2(b);
2390
- const placed = (p) => {
2391
- const cut2 = merged.find(([u, w]) => p >= u - EPS && p <= w + EPS);
2392
- if (!cut2) return p;
2393
- const sides = [...cut2[0] > lo2 + EPS ? [cut2[0]] : [], ...cut2[1] < hi - EPS ? [cut2[1]] : []];
2394
- return sides.length === 0 ? p : sides.reduce((m, x) => Math.abs(x - p) < Math.abs(m - p) ? x : m);
2395
- };
2396
- const bands = s.slots.map((sl) => [placed(sl.line) - half, placed(sl.line) + half]);
2397
- return { cuts: owned, bodies: less(raw, bands) };
2398
- };
2399
- const grows = (id) => {
2400
- const it = now.items[id];
2401
- const e = it.edge ? now.edges[it.edge] : void 0;
2402
- return !!e && !!formsOf(scene.items[id], axisOf(e.side))[it.form]?.main.grows;
2403
- };
2404
- const solid = Object.entries(now.items).filter(([id, it]) => it.fixed && !grows(id)).map(([, it]) => it.rect);
2405
- const bodies = (s) => {
2406
- const e = s.edge;
2407
- if (!e) return [];
2408
- const column = axisOf(e.side) === "column";
2409
- const across2 = (a, b) => column ? rect(-1e9, a, 2e9, b - a) : rect(a, -1e9, b - a, 2e9);
2410
- const reserved = Object.entries(now.items).filter(([iid, p]) => p.edge === e.id && p.fixed && !p.inMenu && !grows(iid)).map(([, p]) => column ? across2(p.rect.y, p.rect.y + p.rect.height) : across2(p.rect.x, p.rect.x + p.rect.width));
2411
- return [...reserved, ...cutsOf(s).bodies.map(([a, b]) => across2(a, b))].filter((b) => area(b) > EPS);
2412
- };
2413
- const drawnBySpec = /* @__PURE__ */ new Map();
2414
- const drawnBesides = (s) => {
2415
- let rs = drawnBySpec.get(s);
2416
- if (!rs) drawnBySpec.set(s, rs = drawn.filter((d) => d.spec !== s).map((d) => d.rect));
2417
- return rs;
2418
- };
2419
- return { owners, taken, cutsOf, bodies, solid, drawnBesides };
2420
- }
2421
-
2422
- // src/scene/targets/tiles.ts
2423
- function tilerOf(spec) {
2424
- const data = EDGE_KINDS[spec.kind];
2425
- const v = spec.vertical;
2426
- const band = (a, b, after) => {
2427
- const [c0, c1] = spec.across(after);
2428
- return v ? rect(c0, a, c1 - c0, b - a) : rect(a, c0, b - a, c1 - c0);
2429
- };
2430
- const keepAt = (line, h) => v ? { x: h.x + h.width / 2, y: line } : { x: line, y: h.y + h.height / 2 };
2431
- const tile = (zone, hit, more) => ({ edge: spec.id, spec, edgeKind: spec.kind, vertical: v, zone, hit, ...more });
2432
- const split = (t) => {
2433
- const h = t.hit;
2434
- const [h0, h1] = v ? [h.x, h.x + h.width] : [h.y, h.y + h.height];
2435
- const [c0, c1] = [Math.max(h0, spec.owned[0]), Math.min(h1, spec.owned[1])];
2436
- const part = (a, b) => v ? rect(a, h.y, b - a, h.height) : rect(h.x, a, h.width, b - a);
2437
- const beyond = less([[h0, h1]], [spec.owned]);
2438
- if (beyond.length === 0) return [t];
2439
- if (c1 - c0 <= EPS) return [{ ...t, order: data.overreach }];
2440
- const core = { ...t, hit: part(c0, c1), keep: t.keep && keepAt(v ? t.keep.y : t.keep.x, part(c0, c1)) };
2441
- return [core, ...beyond.map(([a, b]) => ({ ...t, kind: "reach", of: core, order: data.overreach, hit: part(a, b), keep: null }))];
2442
- };
2443
- return { band, keepAt, tile, split };
2444
- }
2445
- function meet(a, b) {
2446
- const x = Math.max(a.x, b.x);
2447
- const y = Math.max(a.y, b.y);
2448
- const w = Math.min(a.x + a.width, b.x + b.width) - x;
2449
- const h = Math.min(a.y + a.height, b.y + b.height) - y;
2450
- return w > EPS && h > EPS ? rect(x, y, w, h) : null;
2451
- }
2452
- function wholeRefusal(c, spec) {
2453
- if (!spec.edge || spec.own || spec.holds || ZONES.some((z) => spec.refuse(z) !== "not-accepted")) return null;
2454
- const drawn = c.now.edges[spec.id]?.rect;
2455
- const hit = drawn ? meet(drawn, c.scene.stage) : null;
2456
- if (!hit) return null;
2457
- const data = EDGE_KINDS[spec.kind];
2458
- return { edge: spec.id, spec, edgeKind: spec.kind, vertical: spec.vertical, zone: "start", hit, kind: "refuse", id: "", order: data.layer, priority: data.priority, keep: null, reason: "not-accepted", whole: true };
2459
- }
2460
- var hasRoom = (spans, away) => less(spans, away).some(([a, b]) => b - a > EPS);
2461
- function linesIn(spec, z, mine, pieces, [pa, pb], own) {
2462
- const reserved = spec.reserved;
2463
- const lines = mine.map((s) => s.line);
2464
- const lo2 = Math.min(z.a, ...lines);
2465
- const hi = Math.max(z.b, ...lines);
2466
- const free = less([[z.a, z.b]], reserved);
2467
- const freeLen = free.reduce((n, [a, b]) => n + (b - a), 0);
2468
- const spread = (x) => {
2469
- if (hi - lo2 <= EPS || freeLen <= EPS) return x;
2470
- let d = (x - lo2) / (hi - lo2) * freeLen;
2471
- for (const [a, b] of free) {
2472
- if (d <= b - a + EPS) return a + Math.min(d, b - a);
2473
- d -= b - a;
2474
- }
2475
- return free[free.length - 1][1];
2476
- };
2477
- const past = lines.some((x) => x < z.a - EPS || x > z.b + EPS);
2478
- const nearer = (p, [a, b]) => p > a && p < b ? p - a < b - p ? a : b : p;
2479
- const proj = (x) => {
2480
- const q = reserved.reduce((r, s) => nearer(r, s), past ? spread(x) : x);
2481
- const p = own ? nearer(q, own) : q;
2482
- if (pieces.some(([a, b]) => p >= a - EPS && p <= b + EPS)) return p;
2483
- 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);
2484
- };
2485
- const ownEnd = (s) => !!own && s.home && (Math.abs(s.line - own[0]) < EPS || Math.abs(s.line - own[1]) < EPS);
2486
- const inPiece = mine.filter((s) => ownEnd(s) ? s.line >= pa - EPS && s.line <= pb + EPS : proj(s.line) >= pa - EPS && proj(s.line) <= pb + EPS).sort((a, b) => proj(a.line) - proj(b.line) || a.line - b.line);
2487
- const projected = inPiece.map((s) => proj(s.line));
2488
- 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;
2489
- return { inPiece, at };
2490
- }
2491
- function tileEdge(c, spec, { cuts, bodies }, lost) {
2492
- const whole = wholeRefusal(c, spec);
2493
- if (whole) return [whole];
2494
- const data = EDGE_KINDS[spec.kind];
2495
- const v = spec.vertical;
2496
- const own = spec.own ? v ? [spec.own.y, spec.own.y + spec.own.height] : [spec.own.x, spec.own.x + spec.own.width] : null;
2497
- const { band, keepAt, tile, split } = tilerOf(spec);
2498
- const drawn = spec.edge ? c.now.edges[spec.id]?.rect : void 0;
2499
- const alongLimit = drawn && (v ? drawn.height : drawn.width) > EPS ? v ? rect(-1e9, drawn.y, 2e9, drawn.height) : rect(drawn.x, -1e9, drawn.width, 2e9) : null;
2500
- const refused = (zone, hit, reason) => {
2501
- const inEdge = alongLimit ? meet(hit, alongLimit) : hit;
2502
- const shown = inEdge && meet(inEdge, c.scene.stage);
2503
- return shown ? split(tile(zone, shown, { kind: "refuse", id: "", order: data.layer, priority: data.priority, keep: null, reason })) : [];
2504
- };
2505
- const slots = distinctSlots(c, spec.slots);
2506
- const runs = [...spec.reserved].sort((p, q) => p[0] - q[0]).reduce((acc, [a, b]) => {
2507
- const last = acc[acc.length - 1];
2508
- if (last && a <= last[1] + EPS) acc[acc.length - 1] = [last[0], Math.max(last[1], b)];
2509
- else acc.push([a, b]);
2510
- return acc;
2511
- }, []);
2512
- const wedged = (line) => runs.some(([a, b]) => line > a + EPS && line < b - EPS);
2513
- const homeSlot = spec.holds && own ? slots.find((s) => s.home && s.order !== void 0) : void 0;
2514
- const out = [];
2515
- for (const z of spec.zones) {
2516
- const room = less([[z.a, z.b]], [...cuts, ...spec.reserved]).filter(([a, b]) => b - a > EPS);
2517
- const pieces = own ? less(room, [own]) : room;
2518
- if (!hasRoom(pieces, bodies)) continue;
2519
- const lined = slots.flatMap((s) => spec.holds && s.home ? own ? [{ ...s, line: own[0] }, { ...s, line: own[1] }] : [] : [s]);
2520
- const mine = lined.filter((s) => s.zone === z.zone && !wedged(s.line) && pieces.some(([a, b]) => b > s.line - data.reach + EPS && a < s.line + data.reach - EPS));
2521
- const reason = spec.refuse(z.zone);
2522
- for (const [pa, pb] of pieces) {
2523
- const { inPiece, at } = linesIn(spec, z, mine, pieces, [pa, pb], own);
2524
- if (inPiece.length === 0) {
2525
- const lone = (reason === "no-room" || reason === "too-thick") && mine.length === 0 && slots.some((s) => s.zone === z.zone && s.home);
2526
- const near = mine.length === 0 || data.virtual || mine.some((s) => pb > s.line - data.reach + EPS && pa < s.line + data.reach - EPS);
2527
- const joins = data.virtual ? mine.filter((s) => s.ok && !s.home) : [];
2528
- if (joins.length > 0) {
2529
- const s = joins.reduce((m, x) => Math.abs(x.line - (pa + pb) / 2) < Math.abs(m.line - (pa + pb) / 2) ? x : m);
2530
- const hit = band(pa, pb, s.result.layout);
2531
- out.push(...split(tile(z.zone, hit, { kind: "slot", id: s.id, order: data.layer, priority: data.priority, keep: keepAt(Math.min(Math.max(s.line, pa), pb), hit), place: s.place, result: s.result, marker: spec.marker(s), slot: s.result.layout.items[c.dragged].rect })));
2532
- continue;
2533
- }
2534
- if (reason && near) out.push(...refused(z.zone, band(pa, pb), lone ? "own-place" : reason));
2535
- continue;
2536
- }
2537
- const meet2 = (i) => {
2538
- const [lo2, hi] = [at[i - 1], at[i]];
2539
- const between = spec.cards.filter(([u, w]) => u >= inPiece[i - 1].line - EPS && w <= inPiece[i].line + EPS);
2540
- const mid2 = between.length === 1 ? (between[0][0] + between[0][1]) / 2 : NaN;
2541
- return mid2 > lo2 + EPS && mid2 < hi - EPS ? mid2 : (lo2 + hi) / 2;
2542
- };
2543
- inPiece.forEach((s, i) => {
2544
- const p = at[i];
2545
- const lim = data.virtual ? Infinity : data.reach;
2546
- const a = Math.max(i === 0 ? pa : meet2(i), p - lim);
2547
- const b = Math.min(i === inPiece.length - 1 ? pb : meet2(i + 1), p + lim);
2548
- if (b - a <= EPS) {
2549
- if (s.ok && !s.home) lost.push(s.id);
2550
- return;
2551
- }
2552
- if (!hasRoom([[a, b]], bodies)) return;
2553
- const homeTile = (id, u, w, line) => {
2554
- const hit = band(u, w, s.home ? s.result.layout : void 0);
2555
- return tile(z.zone, hit, { kind: "home", id, order: data.layer, priority: data.priority, keep: keepAt(Math.min(Math.max(line, u), w), hit), place: { kind: "home" } });
2556
- };
2557
- if (s.home) {
2558
- out.push(...split(homeTile(s.id, a, b, p)));
2559
- return;
2560
- }
2561
- const faced = homeSlot && s.order === homeSlot.order ? spec.cards.filter(([u, w]) => w > a + EPS && u < b - EPS) : [];
2562
- for (const [u, w] of faced) out.push(...split(homeTile(homeSlot.id, Math.max(u, a), Math.min(w, b), p)));
2563
- for (const [u, w] of less([[a, b]], faced)) {
2564
- const line = Math.min(Math.max(p, u), w);
2565
- const hit = band(u, w, s.result.layout);
2566
- if (!s.ok) out.push(...refused(z.zone, hit, roomReason(spec, z.zone)));
2567
- else out.push(...split(tile(z.zone, hit, { kind: "slot", id: s.id, order: data.layer, priority: data.priority, keep: keepAt(line, hit), place: s.place, result: s.result, marker: spec.marker(s), slot: s.result.layout.items[c.dragged].rect })));
2568
- }
2569
- });
2570
- }
2571
- }
2572
- if (own && spec.own) {
2573
- const home = spec.slots.find((s) => s.home);
2574
- const [a, b] = own;
2575
- const h = band(a, b);
2576
- const o = spec.own;
2577
- const hit = v ? rect(Math.min(h.x, o.x), a, Math.max(h.x + h.width, o.x + o.width) - Math.min(h.x, o.x), b - a) : rect(a, Math.min(h.y, o.y), b - a, Math.max(h.y + h.height, o.y + o.height) - Math.min(h.y, o.y));
2578
- out.push(tile(home?.zone ?? zoneOf(c.scene.items[c.dragged]), hit, { kind: "home", id: home ? home.id : targetId({ kind: "home", item: c.dragged }), order: 0, priority: home ? data.priority : 1, keep: null, place: { kind: "home" } }));
2579
- }
2580
- return out;
2581
- }
2582
- var clip = (r, cuts) => cuts.reduce((ps, cut2) => ps.flatMap((p) => subtract(p, cut2)), [r]);
2583
- var coverOf = (t) => t.hits ?? [t.hit];
2584
- var largest = (ps) => ps.reduce((a, p) => area(p) > area(a) ? p : a);
2585
- var withPieces = (t, ps) => {
2586
- const { hits: _h, ...rest } = t;
2587
- return { ...rest, hit: largest(ps), ...ps.length > 1 ? { hits: ps } : {} };
2588
- };
2589
- function placeReach(t, pieces, placed, landed, gone) {
2590
- const of = t.of;
2591
- const at = landed.get(of) ?? [];
2592
- if (at.length === 0 && gone.has(of)) {
2593
- const line = t.vertical ? of.keep.y : of.keep.x;
2594
- const whole = pieces.filter((p) => p.width >= 1 && p.height >= 1);
2595
- 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);
2596
- const kept = (on.length > 0 ? on : whole).reduce((a, p) => !a || area(p) > area(a) ? p : a, null);
2597
- if (kept) {
2598
- placed.push({ ...of, hit: kept });
2599
- gone.delete(of);
2600
- }
2601
- return;
2602
- }
2603
- for (const i of at) {
2604
- const cover = coverOf(placed[i]);
2605
- for (let k = 0; k < cover.length; k++) {
2606
- const h = cover[k];
2607
- const joint = pieces.find(
2608
- (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)
2609
- );
2610
- if (!joint) continue;
2611
- 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));
2612
- placed[i] = cover.length > 1 ? withPieces(placed[i], cover.map((r, j) => j === k ? merged : r)) : { ...placed[i], hit: merged };
2613
- return;
2614
- }
2615
- }
2616
- }
2617
- var isRefused = (t) => t.kind === "refuse" || t.kind === "reach" && t.of.kind === "refuse";
2618
- function clipTiles(tiles, own, chrome, lost) {
2619
- const { owners, taken, bodies, solid } = own;
2620
- const bySpec = /* @__PURE__ */ new Map();
2621
- const others = (t) => {
2622
- let rs = bySpec.get(t.spec);
2623
- if (!rs) bySpec.set(t.spec, rs = owners.filter((o) => o.spec !== t.spec).map((o) => o.rect));
2624
- return rs;
2625
- };
2626
- const besides = (t) => t.spec ? own.drawnBesides(t.spec) : [];
2627
- const bodiesOf = (t) => t.spec ? bodies(t.spec) : [];
2628
- const placed = [];
2629
- const later = [];
2630
- const refused = tiles.filter(isRefused);
2631
- const landed = /* @__PURE__ */ new Map();
2632
- const gone = /* @__PURE__ */ new Set();
2633
- const place = (t) => {
2634
- if (t.order === 0) {
2635
- placed.push(t);
2636
- return;
2637
- }
2638
- if (t.whole) {
2639
- const pieces2 = clip(t.hit, placed.flatMap(coverOf)).filter((p) => p.width >= 1 && p.height >= 1);
2640
- if (pieces2.length > 0) placed.push(withPieces(t, pieces2));
2641
- return;
2642
- }
2643
- const data = EDGE_KINDS[t.edgeKind];
2644
- if (data.virtual && t.kind === "refuse") {
2645
- later.push(t);
2646
- return;
2647
- }
2648
- if (t.kind === "reach") {
2649
- placeReach(t, clip(t.hit, [...placed.flatMap(coverOf), ...others(t), ...bodiesOf(t), ...solid, ...chrome]), placed, landed, gone);
2650
- return;
2651
- }
2652
- const yields = t.kind === "refuse" ? besides(t) : [];
2653
- const cuts = [...placed.flatMap(coverOf), ...(t.spec && taken.get(t.spec)) ?? [], ...yields, ...data.virtual || t.kind === "refuse" ? [] : bodiesOf(t), ...solid, ...chrome];
2654
- const pieces = clip(t.hit, cuts);
2655
- if (pieces.length === 0) {
2656
- if (t.kind !== "refuse") gone.add(t);
2657
- return;
2658
- }
2659
- if (t.kind === "refuse") {
2660
- landed.set(t, pieces.map((_, k) => placed.length + k));
2661
- for (const p of pieces) placed.push({ ...t, hit: p });
2662
- return;
2663
- }
2664
- const solidPieces = pieces.filter((p) => p.width >= 1 && p.height >= 1);
2665
- for (const p of pieces) if (!solidPieces.includes(p)) (data.virtual ? later : refused).push({ ...t, kind: "refuse", reason: t.spec ? roomReason(t.spec, t.zone) : "no-room", hit: p, keep: null });
2666
- if (solidPieces.length === 0) return;
2667
- landed.set(t, [placed.length]);
2668
- placed.push(withPieces(t, solidPieces));
2669
- };
2670
- const byOrder = (a, b) => a.order - b.order;
2671
- for (const t of tiles.filter((x) => !isRefused(x)).sort(byOrder)) place(t);
2672
- for (const t of refused.sort(byOrder)) place(t);
2673
- for (const t of gone) if (t.kind !== "home") lost.push(t.id);
2674
- for (const t of later) for (const p of clip(t.hit, [...placed.flatMap(coverOf), ...besides(t), ...solid, ...chrome])) placed.push({ ...t, hit: p });
2675
- return placed;
2676
- }
2677
-
2678
- // src/scene/targets/virtual.ts
2679
- function ringsOf(c, side) {
2680
- const seen = sideRings(c.scene.edges, side);
2681
- const kept = c.rest.edges.filter((e) => e.side === side).map((e) => seen.get(e.id) ?? e.ring);
2682
- const at = (e) => seen.get(e.id) ?? e.ring;
2683
- return { before: (e) => kept.filter((r) => r < at(e)).length, after: (e) => kept.filter((r) => r <= at(e)).length, count: kept.length };
2684
- }
2685
- function railOuter(c, side) {
2686
- const { now, scene } = c;
2687
- const place = now.railPlaces[side];
2688
- const windowEdge = side === "left" ? scene.stage.x : scene.stage.x + scene.stage.width;
2689
- if (!place) return windowEdge;
2690
- const placeOuter = side === "left" ? place.x : place.x + place.width;
2691
- const barOutside = Object.values(now.edges).some((e) => e.side === side && e.rect.width > 0 && e.rect.height > 0 && (side === "left" ? e.rect.x + e.rect.width <= placeOuter + EPS : e.rect.x >= placeOuter - EPS));
2692
- return barOutside ? placeOuter : windowEdge;
2693
- }
2694
- function collapsesAt(c, result, side) {
2695
- const me = result.layout.items[c.dragged];
2696
- if (!me || me.floating || me.inMenu || !isHandFolded(result.scene, c.dragged) || c.isHome(result) || !c.sound(result.layout)) return false;
2697
- const sideOf = (edgeId) => edgeId ? result.layout.edges[edgeId]?.side : void 0;
2698
- return me.rail === side || sideOf(me.rides) === side || sideOf(me.edge) === side;
2699
- }
2700
- function columnLines(c, side, railLands) {
2701
- const { scene, now, dragged } = c;
2702
- if (scene.sideColumns === "one" && c.rest.edges.some((e) => e.side === side && isSideColumn(c.rest, e) && (e.items.length > 0 || e.column !== true))) return [];
2703
- const cols = scene.edges.filter((e) => e.side === side && e.column === true).map((edge) => ({ edge, l: now.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 }));
2704
- const own = Object.values(now.edges).filter((e) => e.side === side && e.rect.height > 0);
2705
- const span = own[0]?.rect ?? now.content;
2706
- const rings = ringsOf(c, side);
2707
- const lines = [];
2708
- const outerX = (r) => side === "left" ? r.x : r.x + r.width;
2709
- const innerX = (r) => side === "left" ? r.x + r.width : r.x;
2710
- const atOf = (e) => c.lay.get(e.id)?.at ?? 0;
2711
- cols.forEach((x) => lines.push({ x: outerX(x.r), ring: rings.before(x.edge), at: atOf(x.edge) - 0.5 }));
2712
- if (cols.length > 0) lines.push({ x: innerX(cols[cols.length - 1].r), ring: rings.after(cols[cols.length - 1].edge), at: atOf(cols[cols.length - 1].edge) + 0.5 });
2713
- else lines.push({ x: side === "left" ? now.content.x : now.content.x + now.content.width, ring: rings.count, at: Math.max(-1, ...scene.edges.filter((e) => e.side === side).map(atOf)) + 0.5 });
2714
- const lands = (r) => {
2715
- const me = r.layout.items[dragged];
2716
- const el = me.edge ? r.layout.edges[me.edge] : void 0;
2717
- return !!el && !me.inMenu && !el.folded && !el.rides && r.scene !== c.scene;
2718
- };
2719
- const { reach, place, edgePlace } = EDGE_KINDS.columnLine;
2720
- const stage = c.scene.stage;
2721
- const dir = side === "left" ? 1 : -1;
2722
- const outside2 = (x) => Object.values(now.edges).some((e) => e.side === side && e.rect.width > 0 && e.rect.height > 0 && (side === "left" ? e.rect.x + e.rect.width <= x + EPS : e.rect.x >= x - EPS));
2723
- const railFirst = railLands && !!c.scene.rails?.[side]?.accepts.includes(c.kind) && !!now.railPlaces[side] && !(now.rails[side]?.segments.length ?? 0);
2724
- const placeOf = (l, i) => {
2725
- const atEdge = side === "left" ? l.x <= stage.x + EPS : l.x >= stage.x + stage.width - EPS;
2726
- if (i > 0 || outside2(l.x) || !(atEdge || railFirst)) return { line: l.x, zone: [l.x - reach, l.x + reach], owned: [l.x - place / 2, l.x + place / 2] };
2727
- const windowEdge = side === "left" ? stage.x : stage.x + stage.width;
2728
- const boundary = clamp(l.x, stage.x, stage.x + stage.width);
2729
- const band0 = railFirst ? railOuter(c, side) + dir * RAIL_EDGE : boundary;
2730
- const line = band0 + dir * edgePlace / 2;
2731
- const span2 = (a, b) => [Math.min(a, b), Math.max(a, b)];
2732
- return { line, zone: span2(railFirst ? band0 : windowEdge, line + dir * reach), owned: span2(band0, band0 + dir * edgePlace) };
2733
- };
2734
- const most = scene.columns?.[side]?.cross.max;
2735
- const tooThick = most !== void 0 && thickerThan(formsOf(scene.items[dragged], "column"), most);
2736
- return lines.map((l, i) => {
2737
- const p = placeOf(l, i);
2738
- return {
2739
- id: virtualEdgeId("column", side),
2740
- edge: null,
2741
- kind: "columnLine",
2742
- lay: { phase: EDGE_KINDS.columnLine.phase, side, at: l.at },
2743
- vertical: false,
2744
- zones: [{ zone: "start", a: Math.max(p.zone[0], stage.x), b: Math.min(p.zone[1], stage.x + stage.width) }],
2745
- slots: [slotAt(c, "start", p.line, targetId({ kind: "column", side, ring: l.ring }), { kind: "column", side, ring: l.ring }, lands)],
2746
- refuse: () => tooThick ? "too-thick" : "no-room",
2747
- own: null,
2748
- holds: false,
2749
- reserved: [],
2750
- cards: [],
2751
- across: () => [span.y, span.y + span.height],
2752
- owned: [span.y, span.y + span.height],
2753
- ownedAlong: p.owned,
2754
- drawn: false,
2755
- // Where the new column's edge lands: the line itself, inside the stage.
2756
- marker: () => rect(clamp(l.x, stage.x, stage.x + stage.width), span.y, 0, span.height)
2757
- };
2758
- });
2759
- }
2760
- function railEnd(c, side, segments, y) {
2761
- const { scene, rest, dragged } = c;
2762
- const edge = [...segments].reverse().map((s) => rest.edges.find((e) => e.id === s.edge)).find((e) => !!e);
2763
- if (!edge) return null;
2764
- const at = edge.items.filter((id) => zoneOf(rest.items[id]) === "start").length;
2765
- const place = { kind: "gap", edge: edge.id, zone: "start", at };
2766
- const collapsed = isHandFolded(scene, dragged);
2767
- const lands = ({ layout }) => {
2768
- const me = layout.items[dragged];
2769
- return !me.inMenu && (collapsed ? me.rail === side : me.edge === edge.id && !me.rail && !me.rides && !layout.edges[edge.id]?.folded);
2770
- };
2771
- return slotAt(c, "start", y, targetId(place), place, lands);
2772
- }
2773
- function railStops(c, side) {
2774
- const { scene, now, dragged } = c;
2775
- const rail = now.rails[side];
2776
- const placeRect = now.railPlaces[side];
2777
- if (!placeRect) return null;
2778
- const rings = ringsOf(c, side);
2779
- const seenRings = sideRings(scene.edges, side);
2780
- const mine = scene.edges.filter((e) => e.side === side).sort((a, b) => (seenRings.get(a.id) ?? 0) - (seenRings.get(b.id) ?? 0));
2781
- const edgeOf = (id) => mine.find((e) => e.id === id);
2782
- const ringBefore = (id) => {
2783
- const e = edgeOf(id);
2784
- return e ? rings.before(e) : 0;
2785
- };
2786
- const ringAfter = (id) => {
2787
- const e = edgeOf(id);
2788
- return e ? rings.after(e) : 1;
2789
- };
2790
- const stops2 = [];
2791
- let zone;
2792
- if (rail && rail.segments.length > 0) {
2793
- zone = rail.rect;
2794
- const groups = [];
2795
- for (const s of rail.segments) {
2796
- const g = groups[groups.length - 1];
2797
- if (g && g.edge === s.edge) g.y1 = s.rect.y + s.rect.height;
2798
- else groups.push({ edge: s.edge, y0: s.rect.y, y1: s.rect.y + s.rect.height });
2799
- }
2800
- for (const g of groups) stops2.push({ y: g.y0, ring: ringBefore(g.edge) });
2801
- const last = groups[groups.length - 1];
2802
- stops2.push({ y: last.y1, ring: ringAfter(last.edge) });
2803
- } else {
2804
- const firstCol = mine.find((e) => e.column === true);
2805
- const outer = railOuter(c, side);
2806
- zone = rect(side === "left" ? outer : outer - RAIL_EDGE, placeRect.y, RAIL_EDGE, placeRect.height);
2807
- stops2.push({ y: placeRect.y, ring: firstCol ? rings.before(firstCol) : rings.count });
2808
- }
2809
- const ownRail = now.items[dragged]?.rail === side;
2810
- const shown = !!rail && rail.segments.length > 0;
2811
- const lands = (r) => shown ? collapsesAt(c, r, side) : r.layout.items[dragged].rail === side && r.scene !== c.scene;
2812
- const at = zone;
2813
- const arriving = scene.foldedColumnDrop === "end" && !ownRail;
2814
- const end = arriving && shown ? railEnd(c, side, rail.segments, at.y + at.height) : null;
2815
- if (arriving && (shown ? !end : !isHandFolded(scene, dragged))) return null;
2816
- const width = scene.rails?.[side]?.width;
2817
- const tooThick = width !== void 0 && thickerThan(formsOf(scene.items[dragged], "column"), width);
2818
- return {
2819
- id: virtualEdgeId("rail", side),
2820
- edge: null,
2821
- kind: "railStop",
2822
- // The rail lies outside every edge on its side: laid first among them.
2823
- lay: { phase: EDGE_KINDS.railStop.phase, side, at: -1 },
2824
- vertical: true,
2825
- zones: [{ zone: "start", a: at.y, b: at.y + at.height }],
2826
- slots: end ? [end] : stops2.map((s) => slotAt(c, "start", s.y, targetId({ kind: "rail", side, ring: s.ring }), { kind: "rail", side, ring: s.ring }, lands, ownRail)),
2827
- refuse: () => tooThick ? "too-thick" : "no-room",
2828
- own: null,
2829
- holds: false,
2830
- reserved: [],
2831
- cards: [],
2832
- across: () => [at.x, at.x + at.width],
2833
- // A drawn rail owns its rect; where none shows, a thin band of its place at the side's outer boundary.
2834
- owned: rail && rail.segments.length > 0 ? [at.x, at.x + at.width] : side === "left" ? [at.x, at.x + Math.min(at.width, EDGE_KINDS.railStop.place)] : [at.x + at.width - Math.min(at.width, EDGE_KINDS.railStop.place), at.x + at.width],
2835
- ownedAlong: null,
2836
- drawn: !!rail && rail.segments.length > 0,
2837
- marker: (s) => rect(at.x, s.result.layout.items[dragged].rect.y, at.width, 0)
2838
- };
2839
- }
2840
-
2841
- // src/scene/targets/rail-strip.ts
2842
- var across = (side, outer, width) => side === "left" ? [outer, width] : [outer - width, width];
2843
- function columnStrip(c, side, policy) {
2844
- const edge = railColumnOf(c.scene, side, c.kind);
2845
- if (!edge) return null;
2846
- const own = c.now.items[c.dragged];
2847
- if (own?.edge === edge.id && isHandFolded(c.scene, c.dragged)) return null;
2848
- const place = { kind: "rail", side, ring: edge.ring };
2849
- const moved = moveAmong(c.scene, c.dragged, place, c.others);
2850
- if (moved === c.scene) return null;
2851
- const result = c.settle(use(moved, c.dragged));
2852
- const el = result.layout.edges[edge.id];
2853
- if (!el || el.rect.width <= EPS || !collapsesAt(c, result, side)) return null;
2854
- const r = el.rect;
2855
- const railWidth = result.layout.items[c.dragged].tab ? r.width : edge.cross.rail ?? 30;
2856
- const [x, w] = across(side, side === "left" ? r.x : r.x + r.width, policy.railStripWidth ?? Math.max(RAIL_STRIP_MIN, railWidth));
2857
- return { side, rect: rect(x, r.y, w, r.height), slots: [{ id: targetId({ kind: "rail", side, ring: edge.ring }), place, along: [r.y, r.y + r.height], result }] };
2858
- }
2859
- function declaredStrip(c, side, policy) {
2860
- const template = c.scene.columns?.[side];
2861
- const spec = c.scene.rails?.[side];
2862
- if (!template || !spec || !zoneTakes(template, "start", c.kind) || !spec.accepts.includes(c.kind)) return null;
2863
- const stops2 = railStops(c, side);
2864
- const slots = (stops2?.slots ?? []).filter((s) => !s.home && s.place.kind === "rail" && collapsesAt(c, s.result, side)).sort((a2, b2) => a2.line - b2.line);
2865
- if (!stops2 || slots.length === 0) return null;
2866
- const a = Math.min(...stops2.zones.map((z) => z.a));
2867
- const b = Math.max(...stops2.zones.map((z) => z.b));
2868
- const drawn = c.now.rails[side];
2869
- const outer = drawn && drawn.segments.length > 0 ? side === "left" ? drawn.rect.x : drawn.rect.x + drawn.rect.width : railOuter(c, side);
2870
- const [x, w] = across(side, outer, policy.railStripWidth ?? Math.max(RAIL_STRIP_MIN, spec.width));
2871
- const cuts = [a, ...slots.slice(1).map((s, i) => (slots[i].line + s.line) / 2), b];
2872
- return {
2873
- side,
2874
- rect: rect(x, a, w, b - a),
2875
- slots: slots.map((s, i) => ({ id: s.id, place: s.place, along: [cuts[i], cuts[i + 1]], result: s.result }))
2876
- };
2877
- }
2878
- var outside = (pieces, strips) => strips.reduce((acc, s) => acc.flatMap((p) => overlaps(p, s) ? subtract(p, s) : [p]), [...pieces]).filter((p) => p.width >= 1 && p.height >= 1);
2879
- var largest2 = (rs) => rs.reduce((m, r) => r.width * r.height > m.width * m.height ? r : m);
2880
- var sameRects = (a, b) => a.length === b.length && a.every((r, i) => r.x === b[i].x && r.y === b[i].y && r.width === b[i].width && r.height === b[i].height);
2881
- function withRailStrips(c, targets2, refusals2) {
2882
- const policy = c.scene.foldedColumnDrop === "end" ? null : railPolicyOf(c.scene, c.kind);
2883
- const strips = policy ? ["left", "right"].map((side) => c.scene.rails?.[side] ? declaredStrip(c, side, policy) : columnStrip(c, side, policy)).filter((s) => s !== null) : [];
2884
- if (!policy || strips.length === 0) return { targets: [...targets2], refusals: [...refusals2], strips: [] };
2885
- const sides = new Set(strips.map((s) => s.side));
2886
- const rects = strips.map((s) => s.rect);
2887
- const out = [];
2888
- const lines = [];
2889
- for (const t of targets2) {
2890
- if (t.place.kind === "rail" && sides.has(t.place.side)) continue;
2891
- if (t.free || t.place.kind === "none") {
2892
- out.push(t.free && !policy.floatNearEdge ? clearOfStrips(t, c.dragged, rects) : t);
2893
- continue;
2894
- }
2895
- const was = t.hits ?? [t.hit];
2896
- const pieces = outside(was, rects);
2897
- if (pieces.length === 0) {
2898
- let moved = t.id.startsWith("strip:") || t.place.kind === "column" ? insideStrips(t.hit, strips) : null;
2899
- let movedPieces = moved ? [moved] : [];
2900
- if (moved && t.place.kind === "column") {
2901
- const others = targets2.filter((x) => x !== t && (x.place.kind === "home" || x.place.kind === "column" || x.place.kind === "none" && !x.free)).flatMap((x) => x.hits ?? [x.hit]);
2902
- movedPieces = outside([moved], others);
2903
- moved = movedPieces.length ? largest2(movedPieces) : null;
2904
- if (moved) lines.push({ side: t.place.side, pieces: movedPieces });
2905
- }
2906
- if (moved) out.push({ ...withoutHits(t), hit: moved, ...movedPieces.length > 1 ? { hits: movedPieces } : {} });
2907
- continue;
2908
- }
2909
- if (sameRects(pieces, was)) {
2910
- out.push(t);
2911
- continue;
2912
- }
2913
- out.push({ ...withoutHits(t), hit: largest2(pieces), ...pieces.length > 1 ? { hits: pieces } : {} });
2914
- }
2915
- const columnSide = (edge) => {
2916
- const e = c.scene.edges.find((x) => x.id === edge);
2917
- return e?.column && (e.side === "left" || e.side === "right") ? e.side : null;
2918
- };
2919
- const lineCut = (edge) => lines.filter((l) => l.side === columnSide(edge)).flatMap((l) => l.pieces);
2920
- for (let i = 0; i < out.length; i++) {
2921
- const t = out[i];
2922
- if (t.place.kind !== "gap") continue;
2923
- const cut2 = lineCut(t.place.edge);
2924
- if (!cut2.length) continue;
2925
- const was = t.hits ?? [t.hit];
2926
- const pieces = outside(was, cut2);
2927
- if (sameRects(pieces, was)) continue;
2928
- if (pieces.length === 0) out.splice(i--, 1);
2929
- else out[i] = { ...withoutHits(t), hit: largest2(pieces), ...pieces.length > 1 ? { hits: pieces } : {} };
2930
- }
2931
- const refused = [];
2932
- const railEdges = new Set([...sides].map((side) => virtualEdgeId("rail", side)));
2933
- for (const r of refusals2) {
2934
- if (railEdges.has(r.edge)) continue;
2935
- const was = r.hits ?? [r.hit];
2936
- const pieces = outside(was, [...rects, ...lineCut(r.edge)]);
2937
- if (!pieces.length || sameRects(pieces, was)) {
2938
- if (pieces.length) refused.push(r);
2939
- continue;
2940
- }
2941
- const kept = r.hits ? pieces.filter((p) => p.width >= 1 && p.height >= 1) : [largest2(pieces)];
2942
- const { hits: _h, ...one } = r;
2943
- if (kept.length) refused.push({ ...one, hit: largest2(kept), ...kept.length > 1 ? { hits: kept } : {} });
2944
- }
2945
- const vertical = (r) => {
2946
- const side = c.scene.edges.find((e) => e.id === r.edge)?.side;
2947
- return side ? side === "left" || side === "right" : r.edge === virtualEdgeId("rail", "left") || r.edge === virtualEdgeId("rail", "right");
2948
- };
2949
- const pos = (r) => vertical(r) ? [r.hit.y, r.hit.x] : [r.hit.x, r.hit.y];
2950
- const groups = /* @__PURE__ */ new Map();
2951
- for (const r of refused) groups.set(`${r.edge}
2952
- ${r.zone}`, [...groups.get(`${r.edge}
2953
- ${r.zone}`) ?? [], r]);
2954
- const numbered = /* @__PURE__ */ new Map();
2955
- for (const group of groups.values())
2956
- group.sort((a, b) => pos(a)[0] - pos(b)[0] || pos(a)[1] - pos(b)[1]).forEach((r, n) => numbered.set(r, targetId({ kind: "refuse", edge: r.edge, zone: r.zone, n })));
2957
- const renumbered = refused.map((r) => numbered.get(r) === r.id ? r : { ...r, id: numbered.get(r) });
2958
- for (const s of strips) {
2959
- const grown = s.side === "left" ? rect(s.rect.x, s.rect.y, s.rect.width + policy.magnet, s.rect.height) : rect(s.rect.x - policy.magnet, s.rect.y, s.rect.width + policy.magnet, s.rect.height);
2960
- for (const sl of s.slots) {
2961
- const [a, b] = sl.along;
2962
- const slot = sl.result.layout.items[c.dragged].rect;
2963
- const result = sl.result;
2964
- out.push({
2965
- id: sl.id,
2966
- priority: 1,
2967
- hit: rect(s.rect.x, a, s.rect.width, b - a),
2968
- armedHit: rect(grown.x, a, grown.width, b - a),
2969
- arm: "dwell",
2970
- // Armed, its room opens at once: a host that draws only a marker reads that from this one flag, as for any target.
2971
- opensRoom: true,
2972
- strip: s.rect,
2973
- slot,
2974
- item: c.dragged,
2975
- place: sl.place,
2976
- after: () => result
2977
- });
2978
- }
2979
- }
2980
- return { targets: out, refusals: renumbered, strips: rects };
2981
- }
2982
- var withoutHits = (t) => {
2983
- const { hits: _pieces, ...rest } = t;
2984
- return rest;
2985
- };
2986
- function insideStrips(r, strips) {
2987
- const s = strips.find((x) => overlaps(x.rect, r));
2988
- if (!s) return null;
2989
- return s.side === "left" ? rect(s.rect.x + s.rect.width, r.y, r.width, r.height) : rect(s.rect.x - r.width, r.y, r.width, r.height);
2990
- }
2991
- function clearOfStrips(t, dragged, strips) {
2992
- return {
2993
- ...t,
2994
- after(point, grab) {
2995
- const first = t.after(point, grab);
2996
- const at = first.layout.items[dragged]?.rect;
2997
- if (!at) return first;
2998
- let dx = 0;
2999
- for (const s of strips) {
3000
- const r = rect(at.x + dx, at.y, at.width, at.height);
3001
- if (!overlaps(r, s)) continue;
3002
- dx += s.x + s.width / 2 < r.x + r.width / 2 ? s.x + s.width - r.x : s.x - (r.x + r.width);
3003
- }
3004
- return dx === 0 ? first : t.after({ x: at.x + dx + (grab?.x ?? at.width / 2), y: point.y }, grab);
3005
- }
3006
- };
3007
- }
3008
-
3009
- // src/scene/targets/min-hit.ts
3010
- function kindOf(c, t) {
3011
- const p = parseTargetId(t.id);
3012
- if (!p) return null;
3013
- if (p.kind === "column") return "columnLine";
3014
- if (p.kind === "rail") return "railStop";
3015
- if (p.kind === "gap" || p.kind === "strip") {
3016
- const e = c.scene.edges.find((x) => x.id === p.edge);
3017
- return e ? edgeKindOf(c.scene, e) : null;
3018
- }
3019
- return null;
3020
- }
3021
- var largest3 = (ps) => ps.reduce((a, p) => area(p) > area(a) ? p : a);
3022
- var keepsMinHit = (r, m) => Math.min(r.width, r.height) >= m.across - EPS && Math.max(r.width, r.height) >= m.along - EPS;
3023
- var overlaps2 = (a, b) => Math.min(a.x + a.width, b.x + b.width) - Math.max(a.x, b.x) > EPS && Math.min(a.y + a.height, b.y + b.height) - Math.max(a.y, b.y) > EPS;
3024
- var cut = (ps, g) => ps.flatMap((p) => subtract(p, g)).filter((p) => p.width >= 1 && p.height >= 1);
3025
- var piecesOf = (t) => t.hits ?? [t.hit];
3026
- var withPieces2 = (t, ps) => {
3027
- const { hits: _h, ...rest } = t;
3028
- return { ...rest, hit: largest3(ps), ...ps.length > 1 ? { hits: ps } : {} };
3029
- };
3030
- function growths(r, axis, d) {
3031
- const [lo2, hi] = axis === "x" ? [rect(r.x - d, r.y, d, r.height), rect(r.x + r.width, r.y, d, r.height)] : [rect(r.x, r.y - d, r.width, d), rect(r.x, r.y + r.height, r.width, d)];
3032
- const h = d / 2;
3033
- const halves = axis === "x" ? [rect(r.x - h, r.y, h, r.height), rect(r.x + r.width, r.y, h, r.height)] : [rect(r.x, r.y - h, r.width, h), rect(r.x, r.y + r.height, r.width, h)];
3034
- return [[lo2], [hi], halves];
3035
- }
3036
- var union = (a, gs) => {
3037
- const x0 = Math.min(a.x, ...gs.map((g) => g.x));
3038
- const y0 = Math.min(a.y, ...gs.map((g) => g.y));
3039
- return rect(x0, y0, Math.max(a.x + a.width, ...gs.map((g) => g.x + g.width)) - x0, Math.max(a.y + a.height, ...gs.map((g) => g.y + g.height)) - y0);
3040
- };
3041
- function mayGrow(c, targets2, i, grow, minOf, zone, away) {
3042
- const s = c.scene.stage;
3043
- for (const g of grow) {
3044
- if (g.x < s.x - EPS || g.y < s.y - EPS || g.x + g.width > s.x + s.width + EPS || g.y + g.height > s.y + s.height + EPS) return false;
3045
- if ([...chromeFor(c.scene, "targets"), ...away].some((r) => overlaps2(r, g))) return false;
3046
- if (zone.reduce((ps, z) => ps.flatMap((p) => subtract(p, z)), [g]).some((p) => area(p) > EPS)) return false;
3047
- }
3048
- return targets2.every((o, j) => {
3049
- if (j === i || o.free || !piecesOf(o).some((p) => grow.some((g) => overlaps2(p, g)))) return true;
3050
- if (o.place.kind === "home" || o.place.kind === "none" || o.strip) return false;
3051
- const m = minOf(o);
3052
- const left = grow.reduce((ps, g) => cut(ps, g), [...piecesOf(o)]);
3053
- return !!m && left.length > 0 && keepsMinHit(largest3(left), m);
3054
- });
3055
- }
3056
- function partialGrowth(c, targets2, i, l, axis, d, m, minOf, zone, away) {
3057
- const along2 = axis === "x" ? [l.y, l.y + l.height] : [l.x, l.x + l.width];
3058
- let best = null;
3059
- for (const [side] of growths(l, axis, d)) {
3060
- const g = side;
3061
- const strip = (a, b) => axis === "x" ? rect(g.x, a, g.width, b - a) : rect(a, g.y, b - a, g.height);
3062
- const cuts = /* @__PURE__ */ new Set([along2[0], along2[1]]);
3063
- for (const o of targets2)
3064
- for (const p of piecesOf(o))
3065
- if (overlaps2(p, g)) {
3066
- for (const v of axis === "x" ? [p.y, p.y + p.height] : [p.x, p.x + p.width]) if (v > along2[0] && v < along2[1]) cuts.add(v);
3067
- }
3068
- const marks = [...cuts].sort((a, b) => a - b);
3069
- let runStart = null;
3070
- const runs = [];
3071
- for (let k = 0; k < marks.length - 1; k++) {
3072
- const ok = mayGrow(c, targets2, i, [strip(marks[k], marks[k + 1])], minOf, zone, away);
3073
- if (ok && runStart === null) runStart = marks[k];
3074
- if (!ok && runStart !== null) {
3075
- runs.push([runStart, marks[k]]);
3076
- runStart = null;
3077
- }
3078
- }
3079
- if (runStart !== null) runs.push([runStart, marks[marks.length - 1]]);
3080
- for (const [a, b] of runs.sort((p, q) => q[1] - q[0] - (p[1] - p[0]))) {
3081
- if (b - a < m.along - EPS) break;
3082
- const r = strip(a, b);
3083
- if (mayGrow(c, targets2, i, [r], minOf, zone, away) && (!best || area(r) > area(best))) best = r;
3084
- break;
3085
- }
3086
- }
3087
- return best ? [best] : null;
3088
- }
3089
- function withMinHit(c, targets2, refusals2, zones) {
3090
- const out = [...targets2];
3091
- const refused = refusals2.flatMap(piecesOf);
3092
- const minOf = (t) => {
3093
- const k = kindOf(c, t);
3094
- return k ? EDGE_KINDS[k].minHit : null;
3095
- };
3096
- const drawn = Object.entries(c.now.edges).filter(([, e]) => e.rect.width > EPS && e.rect.height > EPS);
3097
- for (let i = 0; i < out.length; i++) {
3098
- const t = out[i];
3099
- const m = minOf(t);
3100
- const zone = zones.get(t.id);
3101
- if (!m || !zone || t.free || t.strip || t.place.kind === "home" || t.place.kind === "none") continue;
3102
- const ownEdge = t.place.kind === "gap" ? t.place.edge : parseTargetId(t.id)?.edge;
3103
- const k = kindOf(c, t);
3104
- const away = [...refused, ...k && EDGE_KINDS[k].virtual ? [] : drawn.filter(([id]) => id !== ownEdge).map(([, e]) => e.rect)];
3105
- let l = largest3(piecesOf(t));
3106
- if (keepsMinHit(l, m)) continue;
3107
- const steps = ["across", "along"];
3108
- for (const step of steps) {
3109
- const narrowX = l.width <= l.height;
3110
- const axis = step === "across" ? narrowX ? "x" : "y" : narrowX ? "y" : "x";
3111
- const size = axis === "x" ? l.width : l.height;
3112
- const d = (step === "across" ? m.across : m.along) - size;
3113
- if (d <= EPS) continue;
3114
- let grow = growths(l, axis, d).find((g) => mayGrow(c, out, i, g, minOf, zone, away));
3115
- if (!grow && step === "across") grow = partialGrowth(c, out, i, l, axis, d, m, minOf, zone, away) ?? void 0;
3116
- if (!grow) continue;
3117
- for (let j = 0; j < out.length; j++) {
3118
- if (j === i || out[j].free) continue;
3119
- const o = out[j];
3120
- if (!piecesOf(o).some((p) => grow.some((g) => overlaps2(p, g)))) continue;
3121
- out[j] = withPieces2(o, grow.reduce((ps, g) => cut(ps, g), [...piecesOf(o)]));
3122
- }
3123
- const g0 = grow[0];
3124
- const span = axis === "x" ? rect(l.x, g0.y, l.width, g0.height) : rect(g0.x, l.y, g0.width, l.height);
3125
- const grown = union(span, grow);
3126
- const others = piecesOf(out[i]).filter((p) => p !== l);
3127
- out[i] = withPieces2(out[i], [...cut([...others, l], grown), grown]);
3128
- l = largest3(piecesOf(out[i]));
3129
- }
3130
- }
3131
- return out;
3132
- }
3133
-
3134
- // src/scene/targets.ts
3135
- function specsOf(c, kind) {
3136
- const { scene } = c;
3137
- const specs = scene.edges.map((edge) => edgeSpec(c, edge));
3138
- for (const side of ["left", "right"]) {
3139
- const template = scene.columns?.[side];
3140
- if (!template || !zoneTakes(template, "start", kind)) continue;
3141
- const rail = scene.rails?.[side]?.accepts.includes(kind) ? railStops(c, side) : null;
3142
- specs.push(...columnLines(c, side, !!rail?.slots.some((s) => s.ok)));
3143
- specs.push(rail);
3144
- }
3145
- return specs.filter((s) => s !== null);
3146
- }
3147
- function emit2(c, placed) {
3148
- const { scene, now, base, dragged } = c;
3149
- const homeId = targetId({ kind: "home", item: dragged });
3150
- const targets2 = [];
3151
- const refused = [];
3152
- const homeAfter = () => ({ scene, layout: now });
3153
- const pos = (t) => t.vertical ? [t.hit.y, t.hit.x] : [t.hit.x, t.hit.y];
3154
- const rank = /* @__PURE__ */ new Map();
3155
- const edgeOf = (t) => t.spec?.edge ? t.spec : t.edge;
3156
- const byZone = /* @__PURE__ */ new Map();
3157
- for (const t of placed) {
3158
- if (t.kind !== "refuse") continue;
3159
- const zones = byZone.get(edgeOf(t)) ?? /* @__PURE__ */ new Map();
3160
- byZone.set(edgeOf(t), zones.set(t.zone, [...zones.get(t.zone) ?? [], t]));
3161
- }
3162
- for (const zones of byZone.values()) for (const group of zones.values()) group.sort((a, b) => pos(a)[0] - pos(b)[0] || pos(a)[1] - pos(b)[1]).forEach((t, n) => rank.set(t, n));
3163
- let ownHome = null;
3164
- for (const t of placed) {
3165
- if (t.kind === "refuse") {
3166
- const n = rank.get(t);
3167
- refused.push({ id: targetId({ kind: "refuse", edge: t.edge, zone: t.zone, n }), edge: t.edge, zone: t.zone, hit: t.hit, ...t.hits ? { hits: t.hits } : {}, reason: t.reason });
3168
- continue;
3169
- }
3170
- if (t.kind === "home" && t.id === homeId) {
3171
- if (t.order === 0) ownHome = t.hit;
3172
- continue;
3173
- }
3174
- const first = targets2.findIndex((x) => x.id === t.id);
3175
- if (first >= 0) {
3176
- const was = targets2[first];
3177
- const pieces = [...was.hits ?? [was.hit], ...t.hits ?? [t.hit]];
3178
- targets2[first] = { ...was, hit: pieces.reduce((m, p) => area(p) > area(m) ? p : m), hits: pieces };
3179
- continue;
3180
- }
3181
- if (t.kind === "home") {
3182
- targets2.push({ id: t.id, priority: t.priority, hit: t.hit, ...t.hits ? { hits: t.hits } : {}, item: dragged, place: { kind: "home" }, after: homeAfter });
3183
- continue;
3184
- }
3185
- const result = t.result;
3186
- const atEdge = WINDOW_EDGE_KINDS.has(t.edgeKind) && touchesSide(scene.stage, t.hits ?? [t.hit]);
3187
- const thickens = thicknessChanges(now, result.layout);
3188
- const room = t.slot && slotOverMoved(now, base, result.layout, t.slot, dragged, t.edge) ? { opensRoom: true, ...atEdge ? { roomHold: EDGE_ROOM_HOLD } : {}, ...thickens ? { roomHit: hull(t.hit, t.slot) } : {} } : {};
3189
- 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 } : {}, ...room, item: dragged, place: t.place, after: () => result });
3190
- }
3191
- return { targets: targets2, refused, ownHome };
3192
- }
3193
- function hull(a, b) {
3194
- const x = Math.min(a.x, b.x);
3195
- const y = Math.min(a.y, b.y);
3196
- return rect(x, y, Math.max(a.x + a.width, b.x + b.width) - x, Math.max(a.y + a.height, b.y + b.height) - y);
3197
- }
3198
- var thicknessChanges = (now, after) => {
3199
- const a = now.content;
3200
- const b = after.content;
3201
- return Math.abs(b.x - a.x) > EPS || Math.abs(b.y - a.y) > EPS || Math.abs(b.x + b.width - (a.x + a.width)) > EPS || Math.abs(b.y + b.height - (a.y + a.height)) > EPS;
3202
- };
3203
- var WINDOW_EDGE_KINDS = /* @__PURE__ */ new Set(["rail", "railStop", "sideBar", "columnLine"]);
3204
- var touchesSide = (stage, pieces) => pieces.some((r) => r.x <= stage.x + EPS || r.x + r.width >= stage.x + stage.width - EPS);
3205
- function slotOverMoved(now, lifted, after, slot, dragged, onto) {
3206
- const from = now.items[dragged]?.edge;
3207
- return Object.entries(now.items).some(([id, was]) => {
3208
- if (!overlaps(slot, was.rect) || id === dragged || was.floating || was.edge === onto) return false;
3209
- const r = after.items[id]?.rect;
3210
- const start = (from !== void 0 && was.edge === from ? lifted.items[id]?.rect : void 0) ?? was.rect;
3211
- return !!r && (Math.abs(r.x - start.x) > EPS || Math.abs(r.y - start.y) > EPS || Math.abs(r.width - start.width) > EPS || Math.abs(r.height - start.height) > EPS);
3212
- });
3213
- }
3214
- function alongOfSlots(s, tiles) {
3215
- 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]);
3216
- const out = [];
3217
- for (const [a, b] of spans) {
3218
- const last = out[out.length - 1];
3219
- if (last && a <= last[1] + 1e-6) last[1] = Math.max(last[1], b);
3220
- else out.push([a, b]);
3221
- }
3222
- return out;
3223
- }
3224
- function generate(scene, dragged, options) {
3225
- checkEdgeIds(scene);
3226
- const { view: shown, lent, rezoned } = viewOf(scene);
3227
- if (shown === scene) return generateShown(scene, dragged, options, []);
3228
- if (inHiddenEdge(scene, dragged) && !lent.has(dragged)) return { targets: [], refusals: [], lost: [] };
3229
- const hidden = scene.edges.filter((e) => e.hidden === true);
3230
- const g = generateShown(shown, dragged, options, hidden);
3231
- const prev = { ...options.previous ? { previous: options.previous } : {}, ...options.fresh ? { fresh: options.fresh } : {} };
3232
- const showsIn = hidden.some((e) => e.showIn !== void 0);
3233
- return {
3234
- ...g,
3235
- targets: g.targets.map((t) => ({
3236
- ...t,
3237
- after: (point, grab) => {
3238
- const a = t.after(point, grab);
3239
- if (a.scene === shown) return { scene, layout: showsIn ? solve(scene, prev) : a.layout };
3240
- const on = t.place.kind === "gap" ? shown.edges.find((e) => t.place.kind === "gap" && e.id === t.place.edge) : void 0;
3241
- const named = !!on && namesZone(on, scene.items[dragged].kind);
3242
- const back = withHiddenOf(scene, a.scene, lent, dragged, named, rezoned);
3243
- const restored = showsIn ? prune(back) : back;
3244
- const ringOf = new Map(a.scene.edges.map((e) => [e.id, e.ring]));
3245
- const renumbered = restored.edges.some((e) => e.hidden !== true && ringOf.get(e.id) !== e.ring);
3246
- const layout = renumbered || showsIn || rezoned.size > 0 ? solve(restored, prev) : a.layout;
3247
- return { scene: restored, layout };
3248
- }
3249
- }))
3250
- };
3251
- }
3252
- function generateShown(scene, dragged, options, hidden) {
3253
- if (Object.hasOwn(scene.items, dragged) && scene.items[dragged].fixed) return anchorTargets(scene, dragged, options);
3254
- const ctx = context(scene, dragged, options, hidden);
3255
- if (!ctx) return { targets: [], refusals: [], lost: [] };
3256
- const { c, item, prev } = ctx;
3257
- const { now } = c;
3258
- const live = specsOf(c, item.kind);
3259
- const alone = new Map(live.map((s) => [s, tileEdge(c, s, { cuts: [], bodies: [] }, [])]));
3260
- const along2 = new Map(live.map((s) => [s, alongOfSlots(s, alone.get(s))]));
3261
- const own = ownership(c, live, (s) => along2.get(s));
3262
- const lost = [];
3263
- const tiles = live.flatMap((spec) => tileEdge(c, spec, own.cutsOf(spec), lost));
3264
- const ownRect = now.items[dragged];
3265
- const homeEdge = scene.edges.find((e) => e.id === ownRect?.edge);
3266
- const homeHit = homeRectOf(c, null);
3267
- if (ownRect && homeEdge && homeHit && !tiles.some((t) => t.order === 0))
3268
- tiles.push({ edge: homeEdge.id, spec: null, edgeKind: edgeKindOf(scene, homeEdge), vertical: axisOf(homeEdge.side) === "column", zone: zoneOf(item), order: 0, kind: "home", id: targetId({ kind: "home", item: dragged }), priority: 1, place: { kind: "home" }, hit: homeHit, keep: null });
3269
- const placed = clipTiles(tiles, own, chromeFor(scene, "targets"), lost);
3270
- const { targets: targets2, refused, ownHome } = emit2(c, placed);
3271
- const homeAfter = () => ({ scene, layout: now });
3272
- if (scene.floatKinds.includes(item.kind)) {
3273
- const was = scene.floats.find((f) => f.id === dragged);
3274
- const fo = options.floatOrient ?? was?.orient ?? floatOrientOf(item);
3275
- const wasOrient = was?.orient ?? floatOrientOf(item);
3276
- const size = options.floatSize ?? (was && wasOrient === fo ? { width: was.rect.width, height: was.rect.height } : floatSizeOf(item, fo, wasOrient));
3277
- targets2.push({
3278
- id: targetId({ kind: "float", item: dragged }),
3279
- priority: 3,
3280
- free: true,
3281
- hit: PLANE,
3282
- // Over the whole plane, but only strictly inside the stage: its edge belongs to the targets drawn against it.
3283
- bounds: stageOf(scene),
3284
- item: dragged,
3285
- place: { kind: "float" },
3286
- after(point, grab) {
3287
- const g = grab ?? { x: size.width / 2, y: size.height / 2 };
3288
- const raw = options.floatFromHeld ? heldRect(scene, dragged, size, point, g) : rect(point.x - g.x, point.y - g.y, size.width, size.height);
3289
- const first = solve(use(floatAt(scene, dragged, raw, options.floatOrient), dragged), prev);
3290
- const at = first.items[dragged]?.rect ?? raw;
3291
- return c.settle(use(floatAt(scene, dragged, rect(at.x, at.y, size.width, size.height), options.floatOrient), dragged));
3292
- }
3293
- });
3294
- }
3295
- (scene.blocked ?? []).forEach((b, i) => {
3296
- targets2.push({ id: targetId({ kind: "none", index: i }), priority: 1, hit: rect(b.x, b.y, b.width, b.height), item: dragged, place: { kind: "none" }, after: homeAfter });
3297
- });
3298
- const sameAsHome = targets2.filter((t) => t.place.kind === "home" && parseTargetId(t.id)?.kind === "column");
3299
- const homePieces = sameAsHome.flatMap((t) => t.hits ?? [t.hit]);
3300
- if (sameAsHome.length) targets2.splice(0, targets2.length, ...targets2.filter((t) => !sameAsHome.includes(t)));
3301
- const homeSlot = targets2.findIndex((t) => t.place.kind === "home");
3302
- if (homeSlot >= 0 && homePieces.length) {
3303
- const t = targets2[homeSlot];
3304
- targets2[homeSlot] = { ...t, hits: [...t.hits ?? [t.hit], ...homePieces] };
3305
- }
3306
- if (ownRect && !ownRect.floating) {
3307
- const hit = ownHome ?? ownRect.rect;
3308
- const pieces = homeSlot < 0 && homePieces.length ? { hits: [hit, ...homePieces] } : {};
3309
- targets2.push({ id: targetId({ kind: "home", item: dragged }), priority: 1, hit, ...pieces, item: dragged, place: { kind: "home" }, after: homeAfter });
3310
- }
3311
- const lostIds = [...new Set(lost)].filter((id) => !targets2.some((t) => t.id === id));
3312
- const layered = withRailStrips(c, targets2, refused);
3313
- const zones = /* @__PURE__ */ new Map();
3314
- for (const t of [...alone.values()].flat()) if (t.kind !== "refuse" && t.id) zones.set(t.id, [...zones.get(t.id) ?? [], t.hit]);
3315
- const bounds = stageOf(scene);
3316
- const held = withMinHit(c, layered.targets, layered.refusals, zones).map((t) => t.roomHit && t.slot ? { ...t, roomHit: hull(t.hit, t.slot) } : t);
3317
- return { targets: held, refusals: layered.refusals.map((r) => ({ ...r, bounds })), lost: lostIds, zones };
3318
- }
3319
- var stageOf = (scene) => rect(scene.stage.x, scene.stage.y, scene.stage.width, scene.stage.height);
3320
- function targets(scene, dragged, options = {}) {
3321
- return generate(scene, dragged, options).targets;
3322
- }
3323
- function targetsAndRefusals(scene, dragged, options = {}) {
3324
- const g = generate(scene, dragged, options);
3325
- return { targets: g.targets, refusals: g.refusals };
3326
- }
3327
- function refusals(scene, dragged, options = {}) {
3328
- return generate(scene, dragged, options).refusals;
3329
- }
3330
- function refusalTargets(list) {
3331
- return list.map((r) => ({ id: r.id, priority: 2, hit: r.hit, ...r.hits ? { hits: r.hits } : {}, ...r.bounds ? { bounds: r.bounds } : {}, refuse: true, refusal: r }));
3332
- }
3333
- var SIDE_RANK = { left: 0, top: 1, bottom: 2, right: 3 };
3334
- function stops(scene, dragged, options = {}) {
3335
- const key = stopsKey(dragged, options);
3336
- let memo2 = stopsMemo.get(scene);
3337
- if (!memo2) stopsMemo.set(scene, memo2 = /* @__PURE__ */ new Map());
3338
- let list = memo2.get(key);
3339
- if (!list) {
3340
- if (memo2.size >= STOPS_PER_SCENE) memo2.clear();
3341
- memo2.set(key, list = orderedStops(scene, dragged, options));
3342
- }
3343
- return [...list];
3344
- }
3345
- var stopsMemo = /* @__PURE__ */ new WeakMap();
3346
- var STOPS_PER_SCENE = 512;
3347
- var layoutIds = /* @__PURE__ */ new WeakMap();
3348
- var nextLayoutId = 0;
3349
- function stopsKey(dragged, o) {
3350
- let prev = null;
3351
- if (o.previous) {
3352
- prev = layoutIds.get(o.previous) ?? nextLayoutId++;
3353
- layoutIds.set(o.previous, prev);
3354
- }
3355
- return JSON.stringify([dragged, prev, o.endReach ?? null, o.floatOrient ?? null, o.floatSize ? [o.floatSize.width, o.floatSize.height] : null, o.fresh ?? null]);
3356
- }
3357
- function orderedStops(scene, dragged, options) {
3358
- const list = targets(scene, dragged, options).filter((t) => t.place.kind !== "home" && t.place.kind !== "none");
3359
- const sideOf = (t) => {
3360
- const p = t.place;
3361
- if (p.kind === "float") return "float";
3362
- if (p.kind === "column" || p.kind === "rail" || p.kind === "side") return p.side;
3363
- if (p.kind === "gap") return scene.edges.find((e) => e.id === p.edge)?.side ?? "float";
3364
- return "float";
3365
- };
3366
- const key = (t) => {
3367
- const side = sideOf(t);
3368
- const r = t.slot ?? t.hit;
3369
- const cx = r.x + r.width / 2;
3370
- const cy = r.y + r.height / 2;
3371
- const rank = side === "float" ? 4 : SIDE_RANK[side];
3372
- return side === "left" ? [rank, cx, cy] : side === "right" ? [rank, -cx, cy] : [rank, cy * (side === "bottom" ? -1 : 1), cx];
3373
- };
3374
- return list.map((t, i) => ({ t, i, k: key(t) })).sort((a, b) => a.k[0] - b.k[0] || a.k[1] - b.k[1] || a.k[2] - b.k[2] || a.i - b.i).map((x) => x.t);
3375
- }
3376
-
3377
- // src/scene/lanes.ts
3378
- var SPOT_PITCH = 40;
3379
- var SNAP_DISTANCE = 8;
3380
- var STEP = 1;
3381
- var mid = (r, axis) => axis === "x" ? r.x + r.width / 2 : r.y + r.height / 2;
3382
- var lo = (r, axis) => axis === "x" ? r.x : r.y;
3383
- var len = (r, axis) => axis === "x" ? r.width : r.height;
3384
- var hull2 = (a, b) => {
3385
- const x = Math.min(a.x, b.x);
3386
- const y = Math.min(a.y, b.y);
3387
- return rect(x, y, Math.max(a.x + a.width, b.x + b.width) - x, Math.max(a.y + a.height, b.y + b.height) - y);
3388
- };
3389
- var nonNeg = (v) => Number.isFinite(v) && v > 0 ? v : 0;
3390
- var sameRect = (a, b) => Math.max(Math.abs(a.x - b.x), Math.abs(a.y - b.y), Math.abs(a.width - b.width), Math.abs(a.height - b.height)) < 0.5;
3391
- var ORIGIN = { x: 0, y: 0 };
3392
- function onEdge(l, edge, dragged, axis) {
3393
- return Object.entries(l.items).filter(([id, it]) => id !== dragged && it.edge === edge && !it.inMenu && !it.floating && it.rect.width > EPS && it.rect.height > EPS).map(([id, it]) => ({ id, r: it.rect, zone: it.zone })).sort((a, b) => lo(a.r, axis) - lo(b.r, axis));
3394
- }
3395
- function neighbors(after, edge, dragged, axis) {
3396
- const me = after.items[dragged];
3397
- const others = onEdge(after, edge, dragged, axis);
3398
- const at = me ? mid(me.rect, axis) : 0;
3399
- return { before: [...others].reverse().find((o) => mid(o.r, axis) < at)?.id ?? "^", next: others.find((o) => mid(o.r, axis) >= at)?.id ?? "$" };
3400
- }
3401
- function givesWay(now, after, lane, side, extent) {
3402
- if (!now) return null;
3403
- if (!after || after.inMenu || after.floating || after.edge !== lane) return "leaves";
3404
- const [p0, p1] = extent(now.rect);
3405
- const [q0, q1] = extent(after.rect);
3406
- if (after.form !== now.form && q1 - q0 < p1 - p0 - 0.5) return "steps";
3407
- return (side === "before" ? p1 - q1 : q0 - p0) > 0.5 ? "moves" : null;
3408
- }
3409
- function liftedLayout(scene, dragged, options) {
3410
- return solve(lift(scene, dragged), options.previous ? { previous: options.previous } : {});
3411
- }
3412
- function othersKey(layout, dragged) {
3413
- return Object.keys(layout.items).filter((id) => id !== dragged).sort().map((id) => {
3414
- const it = layout.items[id];
3415
- return it.inMenu ? `${id}:m` : `${id}:${Math.round(it.rect.x)},${Math.round(it.rect.y)},${Math.round(it.rect.width)},${Math.round(it.rect.height)}`;
3416
- }).join(";");
3417
- }
3418
- function clipAtDrawn(band, axis, cuts) {
3419
- let a = lo(band, axis);
3420
- let b = a + len(band, axis);
3421
- const center = (a + b) / 2;
3422
- const x = axis === "x" ? "y" : "x";
3423
- const line = lo(band, x) + len(band, x) / 2;
3424
- for (const c of cuts) {
3425
- if (!(lo(c, x) < line - EPS && line < lo(c, x) + len(c, x) - EPS)) continue;
3426
- const c0 = lo(c, axis);
3427
- const c1 = c0 + len(c, axis);
3428
- if ((c0 + c1) / 2 <= center) a = Math.max(a, c1);
3429
- else b = Math.min(b, c0);
3430
- }
3431
- return b - a > EPS ? along(band, axis, a, b) : band;
3432
- }
3433
- var memo = /* @__PURE__ */ new WeakMap();
3434
- var previousIds = /* @__PURE__ */ new WeakMap();
3435
- var nextPrevious = 1;
3436
- function keyOf(dragged, o) {
3437
- const { previous, spot: _spot, ...rest } = o;
3438
- let p = 0;
3439
- if (previous) {
3440
- if (!previousIds.has(previous)) previousIds.set(previous, nextPrevious++);
3441
- p = previousIds.get(previous);
3442
- }
3443
- return `${dragged}\0${p}\0${JSON.stringify(rest)}`;
3444
- }
3445
- function lanes(scene, dragged, options = {}) {
3446
- const key = keyOf(dragged, options);
3447
- let perScene = memo.get(scene);
3448
- if (!perScene) memo.set(scene, perScene = /* @__PURE__ */ new Map());
3449
- let base = perScene.get(key);
3450
- if (!base) perScene.set(key, base = build(scene, dragged, options));
3451
- if (!options.spot) return { ...base.regions, lanes: base.lanes };
3452
- const found = [...base.raws].find(([, rs]) => rs.some((r) => r.id === options.spot));
3453
- if (!found) return { ...base.regions, lanes: base.lanes };
3454
- const [heldLane, list] = found;
3455
- const held = list.find((r) => r.id === options.spot);
3456
- const rebuilt = base.lanes.map((l) => {
3457
- const raws = base.raws.get(l.id);
3458
- if (!raws) return asDrawn(l);
3459
- const again = l.id === heldLane ? heldLaneIn(l.id, raws, held, base, dragged) : laneFrom(l.id, raws, held.layout, base.seen, dragged, base.refusals, base.halves.has(l.id), base.share, base.marking, false, true);
3460
- if (!again) return asDrawn(l);
3461
- return l.id === heldLane ? again : { ...asDrawn(l), marks: again.marks };
3462
- });
3463
- const drawn = rebuilt.map((l) => settle(l, held.layout, scene, dragged, rebuilt, options.emptyLanes, base.raws.get(l.id) ? laneRectOver(base.raws.get(l.id), held.layout, base.seen, base.marking).rect : l.rect));
3464
- const heldMarks = drawn.find((l) => l.id === heldLane)?.marks.map((m) => m.rect) ?? [];
3465
- return {
3466
- ...base.regions,
3467
- lanes: drawn.map((l, i) => {
3468
- if (l.id === heldLane || !base.predicted.has(l.id)) return l;
3469
- return { ...l, marks: marksLess(base.lanes[i].marks, l.axis, heldMarks) };
3470
- })
3471
- };
3472
- }
3473
- function asDrawn(l) {
3474
- if (!l.entry && !l.corners) return l;
3475
- const { entry: _entry, corners: _corners, ...rest } = l;
3476
- return rest;
3477
- }
3478
- function heldLaneIn(id, raws, held, base, dragged) {
3479
- return laneFrom(id, raws, held.layout, base.seen, dragged, base.refusals, base.halves.has(id), base.share, base.marking, !held.slide, true);
3480
- }
3481
- function build(scene, dragged, options) {
3482
- const seen = solve(scene, options.previous ? { previous: options.previous } : {});
3483
- const { targets: ts, refusals: rs } = targetsAndRefusals(scene, dragged, options);
3484
- const own = seen.items[dragged];
3485
- const ownEdge = own && !own.floating && !own.inMenu ? own.edge : void 0;
3486
- const pitch = options.pitch === void 0 ? SPOT_PITCH : options.pitch;
3487
- const raws = [];
3488
- const axisOfEdge = (id) => {
3489
- const e = scene.edges.find((x) => x.id === id);
3490
- return e && axisOf(e.side) === "column" ? "y" : "x";
3491
- };
3492
- const columnKey = (side, ring) => scene.edges.find((e) => e.side === side && e.column === true && e.ring === ring)?.id ?? "$";
3493
- for (const t of ts) {
3494
- if (t.free) continue;
3495
- const p = t.place;
3496
- if (p.kind === "gap" || p.kind === "home" && ownEdge) {
3497
- const edge = p.kind === "gap" ? p.edge : ownEdge;
3498
- const axis = axisOfEdge(edge);
3499
- const layout = p.kind === "home" ? seen : t.after(ORIGIN, ORIGIN).layout;
3500
- const landing = p.kind === "home" ? own.rect : layout.items[dragged]?.rect;
3501
- if (!landing) continue;
3502
- const zone = p.kind === "gap" ? p.zone : own?.zone ?? "start";
3503
- const n = neighbors(layout, edge, dragged, axis);
3504
- const id = n.before === "^" && n.next === "$" ? `${edge}/${zone}` : `${edge}/${zone}/${n.before}|${n.next}`;
3505
- raws.push({ lane: edge, edge, axis, id, ...n, landing, layout, target: t, hit: t.hit, ...p.kind === "home" ? { home: true } : {} });
3506
- } else if (p.kind === "column" || p.kind === "rail") {
3507
- const layout = t.after(ORIGIN, ORIGIN).layout;
3508
- const landing = layout.items[dragged]?.rect;
3509
- if (!landing) continue;
3510
- const lane = p.kind === "column" ? `column:${p.side}:${columnKey(p.side, p.ring)}` : `rail:${p.side}`;
3511
- const strip = p.kind === "rail" && t.strip ? t.strip : void 0;
3512
- raws.push({ lane, edge: null, axis: "y", id: `${lane}/${p.kind === "rail" ? "end" : "line"}`, before: "^", next: "$", landing, layout, target: t, hit: strip ?? t.hit, ...strip ? { strip: true } : {} });
3513
- }
3514
- }
3515
- const lifted = liftedLayout(scene, dragged, options);
3516
- if (options.stretchPositions !== "pitched") {
3517
- const { spots, absorbed } = slidingSpots(scene, dragged, options, seen, lifted, raws, pitch > 0 ? pitch : SPOT_PITCH);
3518
- for (let i = raws.length - 1; i >= 0; i--) if (absorbed.has(raws[i])) raws.splice(i, 1);
3519
- raws.push(...spots);
3520
- } else if (pitch > 0) raws.push(...pitchedSpots(scene, dragged, options, seen, lifted, raws, pitch));
3521
- raws.push(...takeoverSpots(scene, dragged, options, seen, lifted, raws, pitch > 0 ? pitch : SPOT_PITCH));
3522
- const byLane = /* @__PURE__ */ new Map();
3523
- for (const r of raws) byLane.set(r.lane, [...byLane.get(r.lane) ?? [], r]);
3524
- const halves = new Set(scene.edges.filter((e) => (e.side === "left" || e.side === "right") && !e.compact).map((e) => e.id));
3525
- const marking = {
3526
- pitch,
3527
- natural: (id, axis) => {
3528
- const main = scene.items[id]?.forms[axis === "x" ? "row" : "column"]?.[0]?.main;
3529
- return main?.grows ? main.natural : void 0;
3530
- },
3531
- gap: (edge) => nonNeg(scene.edges.find((e) => e.id === edge)?.gap),
3532
- lifted,
3533
- crossing: (edge, over) => scene.edges.filter((e) => e.id !== edge && byLane.has(e.id) && (over.edges[e.id]?.cross ?? 0) > EPS && axisOfEdge(e.id) !== axisOfEdge(edge)).map((e) => over.edges[e.id].rect)
3534
- };
3535
- const shareOf = (g, size) => g === void 0 || !Number.isFinite(g) || size <= EPS ? 0.5 : clamp(g / size, 0, 1);
3536
- const w = own?.rect.width ?? 0;
3537
- const h = own?.rect.height ?? 0;
3538
- const share = { x: w >= h ? shareOf(options.grab?.x, w) : 0.5, y: h > w ? shareOf(options.grab?.y, h) : 0.5 };
3539
- const built = [...byLane].map(([id, list]) => laneFrom(id, list, seen, seen, dragged, rs, halves.has(id), share, marking)).filter((l) => !!l);
3540
- const settled = built.map((l) => settle(l, seen, scene, dragged, built, options.emptyLanes));
3541
- const laneList = options.emptyLanes === "edge" ? settled : predict(settled, byLane, { seen, refusals: rs, halves, share, marking }, scene, dragged, built, options.emptyLanes);
3542
- const predicted = new Set(laneList.filter((l, i) => l !== settled[i]).map((l) => l.id));
3543
- const onLanes = new Set(laneList.map((l) => l.id));
3544
- const rails = Object.values(seen.rails).flatMap((r) => r && r.rect.width > EPS && r.rect.height > EPS ? [r.rect] : []);
3545
- const blocked = [
3546
- ...Object.values(seen.edges).filter((e) => e.cross > EPS).map((e) => e.rect),
3547
- ...Object.entries(seen.items).filter(([id, it]) => id !== dragged && !it.floating && !it.inMenu).map(([, it]) => it.rect),
3548
- ...chromeFor(scene, "targets"),
3549
- ...rails,
3550
- // Spots where a release cancels (`Scene.blocked`): no float lands there.
3551
- ...scene.blocked ?? []
3552
- ];
3553
- return {
3554
- raws: byLane,
3555
- lanes: laneList,
3556
- seen,
3557
- refusals: rs,
3558
- halves,
3559
- share,
3560
- marking,
3561
- predicted,
3562
- regions: {
3563
- refused: rs.filter((f) => !onLanes.has(f.edge)).flatMap((f) => f.hits ?? [f.hit]),
3564
- content: floatRegion(scene, dragged),
3565
- blocked,
3566
- stage: scene.stage,
3567
- floatRegion: floatRegion(scene, dragged),
3568
- item: own ? { width: own.rect.width, height: own.rect.height } : { width: 0, height: 0 },
3569
- float: ts.find((t) => t.free) ?? null,
3570
- refusals: rs
3571
- }
3572
- };
3573
- }
3574
- function laneRectOver(list, over, seen, marking) {
3575
- const first = list[0];
3576
- const axis = first.axis;
3577
- const edgeLayout = first.edge ? over.edges[first.edge] : void 0;
3578
- const band = list.map((r) => r.landing).reduce(hull2);
3579
- if (!edgeLayout) return { rect: list.every((r) => r.strip) ? list.map((r) => r.hit).reduce(hull2) : list.map((r) => r.hit).reduce(hull2, band) };
3580
- const drawnHere = edgeLayout.cross > EPS;
3581
- const whole = drawnHere ? edgeLayout.rect : axis === "x" ? rect(edgeLayout.rect.x, band.y, edgeLayout.rect.width, band.height) : rect(band.x, edgeLayout.rect.y, band.width, edgeLayout.rect.height);
3582
- const emptyAsSeen = (seen.edges[first.edge]?.cross ?? 0) <= EPS;
3583
- const clipped = emptyAsSeen ? clipAtDrawn(whole, axis, marking.crossing(first.edge, seen)) : whole;
3584
- return drawnHere ? { rect: clipped } : { rect: clipped, band: whole };
3585
- }
3586
- function laneFrom(id, list, drawn, seen, dragged, rs, halves, share, marking, heldHere = false, spotHeld = false) {
3587
- if (list.length === 0) return null;
3588
- const first = list[0];
3589
- const axis = first.axis;
3590
- const { rect: laneRect, band: markBand } = laneRectOver(list, seen, seen, marking);
3591
- const firstOf = (r) => r.slide?.first ?? r.landing;
3592
- const lastOf = (r) => r.slide?.last ?? r.landing;
3593
- const sorted = [...list].sort((a, b) => mid(firstOf(a), axis) - mid(firstOf(b), axis) || (a.home ? -1 : b.home ? 1 : 0));
3594
- const homeAt = halves ? void 0 : list.find((r) => r.home)?.landing;
3595
- const nearHome = (r) => !!homeAt && !r.home && !r.slide && Math.abs(lo(r.landing, axis) - lo(homeAt, axis)) < Math.max(0, marking.pitch) / 2;
3596
- const unique = [];
3597
- for (const x of sorted) {
3598
- if (nearHome(x)) continue;
3599
- const k = unique.findIndex((y) => !x.slide && !y.slide && sameRect(y.landing, x.landing) || y.id === x.id);
3600
- if (k < 0) unique.push(x);
3601
- else if (x.home && !unique[k].home && !unique.some((y) => y.id === x.id && y !== unique[k])) unique[k] = x;
3602
- }
3603
- const start = lo(laneRect, axis);
3604
- const end = start + len(laneRect, axis);
3605
- const whole = markBand ?? laneRect;
3606
- const bandStart = lo(whole, axis);
3607
- const bandEnd = bandStart + len(whole, axis);
3608
- const drawnItems = first.edge && halves ? new Map(onEdge(drawn, first.edge, dragged, axis).map((o) => [o.id, o.r])) : /* @__PURE__ */ new Map();
3609
- const held = halves ? 0.5 : share[axis];
3610
- const cuts = [];
3611
- for (let i = 0; i + 1 < unique.length; i++) {
3612
- const a = unique[i];
3613
- const b = unique[i + 1];
3614
- const shared = a.next === b.before && a.next !== "$" ? drawnItems.get(a.next) : void 0;
3615
- const aLanding = lastOf(a);
3616
- const bLanding = firstOf(b);
3617
- const aEnd = lo(aLanding, axis) + len(aLanding, axis);
3618
- const across2 = (aEnd + lo(bLanding, axis)) / 2;
3619
- const overlap = lo(bLanding, axis) < aEnd - EPS;
3620
- const slack = Math.max(0, marking.pitch) / 2;
3621
- const atGrab = clamp(across2 + (held - 0.5) * (len(aLanding, axis) + len(bLanding, axis)) / 2, lo(bLanding, axis) - slack, aEnd + slack);
3622
- const raw = shared ? mid(shared, axis) : overlap ? atGrab : across2;
3623
- cuts.push(clamp(Math.max(raw, cuts[i - 1] ?? bandStart), bandStart, bandEnd));
3624
- }
3625
- const spots = unique.flatMap((x, i) => {
3626
- const a = i === 0 ? bandStart : cuts[i - 1];
3627
- const b = i === unique.length - 1 ? bandEnd : cuts[i];
3628
- const along2 = [clamp(a, start, end), clamp(b, start, end)];
3629
- return along2[1] - along2[0] > EPS || b - a <= EPS || x.home ? [{ x, along: along2 }] : [];
3630
- }).map(({ x, along: along2 }) => ({
3631
- id: x.id,
3632
- along: along2,
3633
- landing: x.landing,
3634
- target: x.target,
3635
- ...x.home ? { home: true } : {},
3636
- ...x.pitched ? { pitched: true } : {},
3637
- ...x.slide ? { slide: { from: x.slide.from, to: x.slide.to, snaps: x.slide.snaps, snapDistance: x.slide.snapDistance, step: x.slide.step } } : {},
3638
- others: othersKey(x.layout, dragged)
3639
- }));
3640
- const itemLen = Math.min(...unique.map((x) => len(x.landing, axis)));
3641
- const refused = first.edge ? rs.filter((f) => f.edge === first.edge).flatMap((f) => (f.hits ?? [f.hit]).map((h) => ({ along: [clamp(lo(h, axis), start, end), clamp(lo(h, axis) + len(h, axis), start, end)], reason: f.reason }))).filter((f) => f.along[1] - f.along[0] >= itemLen - EPS) : [];
3642
- const places = new Set(unique.map((x) => `${x.before}|${x.next}`));
3643
- const drawnEdge = first.edge ? drawn.edges[first.edge] : void 0;
3644
- const markRect = drawnEdge && drawnEdge.cross > EPS ? drawnEdge.rect : markBand ?? laneRect;
3645
- const marked = first.edge ? onEdge(drawn, first.edge, heldHere ? "" : dragged, axis).map((o) => {
3646
- const n = o.id === dragged || halves ? void 0 : marking.natural(o.id, axis);
3647
- return n !== void 0 && n < len(o.r, axis) ? { ...o, r: along(o.r, axis, lo(o.r, axis), lo(o.r, axis) + n) } : o;
3648
- }) : [];
3649
- const landings = /* @__PURE__ */ new Map();
3650
- if (!halves) {
3651
- for (const x of unique) if (!x.pitched && !x.home && !landings.has(`${x.before}|${x.next}`)) landings.set(`${x.before}|${x.next}`, { landing: x.landing, layout: x.layout });
3652
- }
3653
- const extent = (iid, r) => {
3654
- const n = halves ? void 0 : marking.natural(iid, axis);
3655
- return [lo(r, axis), lo(r, axis) + (n !== void 0 && n < len(r, axis) ? n : len(r, axis))];
3656
- };
3657
- const marks = first.edge ? edgeMarks(id, markRect, axis, marked, refused, itemLen, (pair) => places.has(pair) || pair.split("|").includes(dragged), dragged, {
3658
- besideMin: halves || marking.pitch <= 0 ? itemLen : marking.pitch,
3659
- gap: marking.gap(first.edge),
3660
- landingOf: (pair) => landings.get(pair),
3661
- // Judged against the layout the marks are drawn in; with a spot held, against the layout as seen with the held
3662
- // item lifted (in the spot's own a neighbor has already stepped down or moved).
3663
- givesWay: (iid, side, after) => givesWay((spotHeld ? marking.lifted : drawn).items[iid], after.items[iid], id, side, (r) => extent(iid, r))
3664
- }) : virtualMarks(id, unique, drawn.items[dragged]);
3665
- return { id, rect: laneRect, axis, spots, refused, marks, ...list.every((r) => r.strip) ? { owns: true } : {} };
3666
- }
3667
- var along = (r, axis, a, b) => axis === "x" ? rect(a, r.y, b - a, r.height) : rect(r.x, a, r.width, b - a);
3668
- var LINE2 = 2;
3669
- var MIN_BOX = 8;
3670
- var HATCH_END = 8;
3671
- var HATCH_EDGE = 8;
3672
- function edgeMarks(lane, laneRect, axis, items, refused, itemLen, lands, held, more) {
3673
- const { besideMin } = more;
3674
- const hatches = (pair, a, b, p, q, gl, gr) => {
3675
- if (gl && Math.abs(p - a) < EPS) out.push({ id: `${lane}/hatch/${pair}:before`, kind: "hatch", rect: along(laneRect, axis, p, p + Math.min(HATCH_END, (q - p) / 2)), givesWay: gl });
3676
- if (gr && Math.abs(q - b) < EPS) out.push({ id: `${lane}/hatch/${pair}:after`, kind: "hatch", rect: along(laneRect, axis, q - Math.min(HATCH_END, (q - p) / 2), q), givesWay: gr });
3677
- };
3678
- const heldAt = items.find((o) => o.id === held);
3679
- const heldSpan = heldAt ? [lo(heldAt.r, axis), lo(heldAt.r, axis) + len(heldAt.r, axis)] : null;
3680
- const out = [];
3681
- const start = lo(laneRect, axis);
3682
- const end = start + len(laneRect, axis);
3683
- const ends = [{ id: "^", a: start, b: start }, ...items.map((o) => ({ id: o.id, a: lo(o.r, axis), b: lo(o.r, axis) + len(o.r, axis) })), { id: "$", a: end, b: end }];
3684
- const isRefused2 = (v) => refused.some((f) => v >= f.along[0] && v <= f.along[1]);
3685
- for (let i = 0; i + 1 < ends.length; i++) {
3686
- const a = clamp(ends[i].b, start, end);
3687
- const b = clamp(ends[i + 1].a, start, end);
3688
- const pair = `${ends[i].id}|${ends[i + 1].id}`;
3689
- if (ends[i + 1].a < start - EPS || ends[i].b > end + EPS || !lands(pair)) continue;
3690
- const left = ends[i].id === held ? ends[i - 1]?.id ?? "^" : ends[i].id;
3691
- const right = ends[i + 1].id === held ? ends[i + 2]?.id ?? "$" : ends[i + 1].id;
3692
- const named = `${left}|${right}`;
3693
- const beside = ends[i + 1].id === held ? "before" : ends[i].id === held ? "after" : null;
3694
- const min = beside ? Math.min(itemLen, besideMin) : itemLen;
3695
- const spot = !beside && named === pair && items.length > 0 && !isRefused2((a + b) / 2) ? more.landingOf(pair) : void 0;
3696
- const gl = spot && left !== "^" && more.givesWay(left, "before", spot.layout) ? left : null;
3697
- const gr = spot && right !== "$" && more.givesWay(right, "after", spot.layout) ? right : null;
3698
- if (b - a < min - EPS) {
3699
- if (spot && b - a >= besideMin - EPS) {
3700
- let ps = [[a, b]];
3701
- for (const f of refused) ps = ps.flatMap(([p, q]) => [[p, Math.min(q, f.along[0])], [Math.max(p, f.along[1]), q]]);
3702
- if (heldSpan) ps = ps.flatMap(([p, q]) => [[p, Math.min(q, heldSpan[0])], [Math.max(p, heldSpan[1]), q]]);
3703
- ps = ps.filter(([p, q]) => q - p >= MIN_BOX - EPS);
3704
- ps.forEach(([p, q], k2) => {
3705
- out.push({ id: `${lane}/box/${pair}${k2 ? `#${k2}` : ""}`, kind: "box", rect: along(laneRect, axis, p, q) });
3706
- hatches(pair, a, b, p, q, gl, gr);
3707
- });
3708
- if (ps.length > 0) continue;
3709
- }
3710
- const t = Math.max(0, Math.min(LINE2, b - a)) / 2;
3711
- if (items.length > 0 && named === pair && !isRefused2((a + b) / 2)) out.push({ id: `${lane}/line/${pair}`, kind: "line", rect: along(laneRect, axis, (a + b) / 2 - t, (a + b) / 2 + t) });
3712
- continue;
3713
- }
3714
- let pieces = [[a, b]];
3715
- for (const f of refused) pieces = pieces.flatMap(([p, q]) => [[p, Math.min(q, f.along[0])], [Math.max(p, f.along[1]), q]]).filter(([p, q]) => q - p > EPS);
3716
- let k = 0;
3717
- for (const [p, q] of pieces) {
3718
- if (q - p < min - EPS) continue;
3719
- const base = `${lane}/box/${named}${beside ? `:${beside}` : ""}${k++ ? `#${k - 1}` : ""}`;
3720
- out.push({ id: out.some((m) => m.id === base) ? `${base}~2` : base, kind: "box", rect: along(laneRect, axis, p, q) });
3721
- hatches(pair, a, b, p, q, gl, gr);
3722
- }
3723
- }
3724
- return out;
3725
- }
3726
- function virtualMarks(lane, spots, held) {
3727
- return spots.map((s) => {
3728
- const l = s.landing;
3729
- if (!held || held.floating || !overlaps3(l, held.rect)) return { id: `${lane}/box/${s.id}`, kind: "box", rect: l };
3730
- const x = mid(held.rect, "x") < mid(l, "x") ? l.x + l.width : l.x;
3731
- return { id: `${lane}/line/${s.id}`, kind: "line", rect: rect(x, l.y, 0, l.height) };
3732
- });
3733
- }
3734
- function inLane(l, r) {
3735
- const marks = l.marks.flatMap((m) => {
3736
- const x0 = Math.max(m.rect.x, r.x);
3737
- const y0 = Math.max(m.rect.y, r.y);
3738
- const x1 = Math.min(m.rect.x + m.rect.width, r.x + r.width);
3739
- const y1 = Math.min(m.rect.y + m.rect.height, r.y + r.height);
3740
- if (m.kind === "line") return x1 - x0 >= m.rect.width - EPS && y1 - y0 >= m.rect.height - EPS ? [m] : [];
3741
- if (x1 - x0 <= EPS || y1 - y0 <= EPS) return [];
3742
- return x1 - x0 >= m.rect.width - EPS && y1 - y0 >= m.rect.height - EPS ? [m] : [{ ...m, rect: rect(x0, y0, x1 - x0, y1 - y0) }];
3743
- });
3744
- return marks.length === l.marks.length && marks.every((m, i) => m === l.marks[i]) ? l : { ...l, marks };
3745
- }
3746
- function settle(lane, drawn, scene, dragged, all, emptyLanes = "real", within = lane.rect) {
3747
- const l = inLane(lane, within);
3748
- const edgeOf = (id) => scene.edges.find((e) => e.id === id);
3749
- const isDrawn = (id) => !!edgeOf(id) && (drawn.edges[id]?.cross ?? 0) > EPS;
3750
- const across2 = (side) => side === "top" || side === "bottom" ? "x" : "y";
3751
- const cuts = [];
3752
- if (!isDrawn(l.id)) {
3753
- for (const e of scene.edges) {
3754
- const de = drawn.edges[e.id];
3755
- if (e.id !== l.id && de && de.cross > EPS && across2(e.side) !== l.axis) cuts.push(de.rect);
3756
- }
3757
- if (l.axis === "y") {
3758
- for (const o of all) if (o.id !== l.id && o.axis === "x" && !isDrawn(o.id)) cuts.push(o.rect);
3759
- }
3760
- }
3761
- const empty = !isDrawn(l.id);
3762
- const barOrRail = (edge) => {
3763
- const e = edge ? edgeOf(edge) : void 0;
3764
- return !!e && (e.side === "top" || e.side === "bottom" || !!e.compact);
3765
- };
3766
- const stays = empty ? Object.entries(drawn.items).filter(([id, it]) => id !== dragged && it.edge !== l.id && barOrRail(it.edge) && !it.floating && !it.inMenu && it.rect.width > EPS && it.rect.height > EPS).map(([, it]) => it.rect) : [];
3767
- if (cuts.length === 0 && !(empty && (emptyLanes === "edge" || l.marks.some((m) => m.kind === "box" && stays.some((s) => overlaps3(m.rect, s)))))) return l;
3768
- const cut2 = (r, cs) => piecesLess(r, l.axis, cs);
3769
- const marks = [];
3770
- for (const m of l.marks) {
3771
- const kept = cut2(m.rect, m.kind === "box" ? [...cuts, ...stays] : cuts);
3772
- if (m.kind === "line") {
3773
- if (kept.length === 1 && Math.abs(kept[0][1] - kept[0][0] - len(m.rect, l.axis)) < EPS) marks.push(m);
3774
- continue;
3775
- }
3776
- kept.forEach(([p, q], i) => {
3777
- const piece = along(m.rect, l.axis, p, q);
3778
- const id = i ? `${m.id}~c${i}` : m.id;
3779
- if (m.kind !== "box" || !empty || emptyLanes === "real") return void marks.push({ ...m, id, rect: piece });
3780
- const side = edgeOf(l.id)?.side ?? (/^(rail|column):/.test(l.id) ? l.id.split(":")[1] : void 0);
3781
- const st = scene.stage;
3782
- let r;
3783
- if (l.axis === "y") {
3784
- const outerRight = side === "right" ? true : side === "left" ? false : Math.abs(piece.x + piece.width - (st.x + st.width)) <= Math.abs(piece.x - st.x);
3785
- const t = Math.min(HATCH_EDGE, piece.width);
3786
- r = rect(outerRight ? piece.x + piece.width - t : piece.x, piece.y, t, piece.height);
3787
- } else {
3788
- const outerBottom = side === "bottom" ? true : side === "top" ? false : Math.abs(piece.y + piece.height - (st.y + st.height)) <= Math.abs(piece.y - st.y);
3789
- const t = Math.min(HATCH_EDGE, piece.height);
3790
- r = rect(piece.x, outerBottom ? piece.y + piece.height - t : piece.y, piece.width, t);
3791
- }
3792
- marks.push({ id: id.replace("/box/", "/hatch/"), kind: "hatch", rect: r });
3793
- });
3794
- }
3795
- return { ...l, marks };
3796
- }
3797
- function piecesLess(r, axis, cs) {
3798
- const span = (x) => [lo(x, axis), lo(x, axis) + len(x, axis)];
3799
- const crosses = (c) => axis === "x" ? c.y < r.y + r.height - EPS && r.y < c.y + c.height - EPS : c.x < r.x + r.width - EPS && r.x < c.x + c.width - EPS;
3800
- let ps = [span(r)];
3801
- for (const c of cs) {
3802
- if (!crosses(c)) continue;
3803
- const [c0, c1] = span(c);
3804
- ps = ps.flatMap(([p, q]) => [[p, Math.min(q, c0)], [Math.max(p, c1), q]]).filter(([p, q]) => q - p > EPS);
3805
- }
3806
- return ps;
3807
- }
3808
- function marksLess(marks, axis, cs) {
3809
- return marks.flatMap((m) => {
3810
- const over = cs.filter((c) => overlaps3(m.rect, c));
3811
- if (over.length === 0) return [m];
3812
- const kept = piecesLess(m.rect, axis, over);
3813
- if (m.kind === "line") return kept.length === 1 && Math.abs(kept[0][1] - kept[0][0] - len(m.rect, axis)) < EPS ? [m] : [];
3814
- return kept.map(([p, q], i) => ({ ...m, id: i ? `${m.id}~c${i}` : m.id, rect: along(m.rect, axis, p, q) }));
3815
- });
3816
- }
3817
- function predict(settled, byLane, base, scene, dragged, built, emptyLanes) {
3818
- const whole = /* @__PURE__ */ new Map();
3819
- for (const l of settled) {
3820
- const raws = byLane.get(l.id);
3821
- if (!raws || l.spots.length !== 1 || !scene.edges.some((e) => e.id === l.id) || (base.seen.edges[l.id]?.cross ?? 0) > EPS) continue;
3822
- const held = raws.find((r) => r.id === l.spots[0].id);
3823
- if (!held || (held.layout.edges[l.id]?.cross ?? 0) <= EPS) continue;
3824
- const again = heldLaneIn(l.id, raws, held, base, dragged);
3825
- if (!again) continue;
3826
- const after = settle(again, held.layout, scene, dragged, built, emptyLanes, laneRectOver(raws, held.layout, base.seen, base.marking).rect).marks;
3827
- whole.set(l.id, after.length === 0 && l.marks.length > 0 ? l.marks : after);
3828
- }
3829
- if (whole.size === 0) return [...settled];
3830
- const order = [...settled].filter((l) => whole.has(l.id)).sort((a, b) => a.axis === b.axis ? 0 : a.axis === "x" ? -1 : 1);
3831
- const final = new Map(settled.filter((l) => !whole.has(l.id)).map((l) => [l.id, l.marks]));
3832
- const meets = /* @__PURE__ */ new Map();
3833
- for (const l of order) {
3834
- const mine = whole.get(l.id);
3835
- const others = [...final].filter(([id, ms]) => id !== l.id && ms.some((m) => mine.some((x) => overlaps3(x.rect, m.rect))));
3836
- meets.set(l.id, others.map(([id]) => id));
3837
- final.set(l.id, marksLess(mine, l.axis, others.flatMap(([, ms]) => ms.map((m) => m.rect))));
3838
- }
3839
- return settled.map((l) => {
3840
- const marks = final.get(l.id);
3841
- const mine = whole.get(l.id);
3842
- if (!mine) return l;
3843
- const corners = meets.get(l.id);
3844
- return corners.length > 0 ? { ...l, marks, entry: mine, corners } : { ...l, marks };
3845
- });
3846
- }
3847
- var overlaps3 = (a, b) => a.x < b.x + b.width - EPS && b.x < a.x + a.width - EPS && a.y < b.y + b.height - EPS && b.y < a.y + a.height - EPS;
3848
- function pitchedSpots(scene, dragged, options, seen, lifted, raws, pitch) {
3849
- const out = [];
3850
- stretches(scene, dragged, options, seen, lifted, raws, (run) => {
3851
- const { e, axis, gap, middle } = run;
3852
- const taken = run.mine.map((r) => r.landing);
3853
- const emit3 = (zone, at, space, x0, flush) => {
3854
- const hit = landsAt(scene, dragged, run, { edge: e.id, zone, at, space }, x0);
3855
- if (!hit) return false;
3856
- const { next, layout } = hit;
3857
- const landed = layout.items[dragged];
3858
- const nb = flush !== void 0 ? layout.items[flush] : void 0;
3859
- if (nb && Math.abs(lo(nb.rect, axis) + len(nb.rect, axis) + gap - lo(landed.rect, axis)) > 1 && Math.abs(lo(landed.rect, axis) + len(landed.rect, axis) + gap - lo(nb.rect, axis)) > 1) return false;
3860
- const n = neighbors(layout, e.id, dragged, axis);
3861
- if (taken.some((r) => Math.abs(lo(r, axis) - lo(landed.rect, axis)) < pitch / 2)) return true;
3862
- taken.push(landed.rect);
3863
- const result = { scene: next, layout };
3864
- const kept = Math.round(next.items[dragged]?.space ?? 0);
3865
- const target = { id: `pitch:${e.id}:${zone}:${at}:${Math.round(space)}`, priority: 2, hit: landed.rect, slot: landed.rect, item: dragged, place: { kind: "gap", edge: e.id, zone, at }, after: () => result };
3866
- const id = `${e.id}/${zone}/${n.before}|${n.next}@${flush !== void 0 && kept > 0 ? `to:${flush}` : Math.round(kept / pitch)}`;
3867
- out.push({ lane: e.id, edge: e.id, axis, id, ...n, landing: landed.rect, layout, target, hit: landed.rect, pitched: true });
3868
- return true;
3869
- };
3870
- return ({ a, b, w, zones, atOf, spaceAt, before, after }) => {
3871
- const end = (x0, order, flush) => {
3872
- for (const zone of order) if (spaceAt(zone, x0) >= -EPS && emit3(zone, atOf(zone), Math.max(0, spaceAt(zone, x0)), x0, flush)) return;
3873
- };
3874
- end(a, zones, before?.id ?? "^");
3875
- if (b - w > a + EPS) end(b - w, [...zones].reverse(), after?.id ?? "$");
3876
- if (b - a < w + pitch) return;
3877
- for (let x0 = a + pitch; x0 + w <= b - pitch + EPS; x0 += pitch) {
3878
- const zone = x0 + w / 2 < middle ? "start" : "end";
3879
- if (!zones.includes(zone)) continue;
3880
- emit3(zone, atOf(zone), spaceAt(zone, x0), x0);
3881
- }
3882
- };
3883
- });
3884
- return out;
3885
- }
3886
- function stretches(scene, dragged, options, seen, lifted, raws, visit) {
3887
- if (!scene.items[dragged]) return;
3888
- const prev = options.previous ? { previous: options.previous } : {};
3889
- const base = lifted;
3890
- for (const e of scene.edges) {
3891
- const bar = e.side === "top" || e.side === "bottom";
3892
- if (!bar && !e.compact) continue;
3893
- const axis = bar ? "x" : "y";
3894
- const mine = raws.filter((r) => r.edge === e.id);
3895
- const drawn = base.edges[e.id] ?? seen.edges[e.id];
3896
- if (mine.length === 0 || !drawn) continue;
3897
- const gap = nonNeg(e.gap);
3898
- const others = onEdge(base, e.id, dragged, axis);
3899
- const zoneOf2 = (o) => o.zone ?? "start";
3900
- const w = len(mine[0].landing, axis);
3901
- const each = visit({ e, axis, w, gap, middle: mid(drawn.rect, axis), mine, fixed: others.filter((o) => scene.items[o.id]?.fixed), prev });
3902
- const slack = (o) => {
3903
- const main = o ? scene.items[o.id]?.forms[axis === "x" ? "row" : "column"]?.[0]?.main : void 0;
3904
- return o && main?.grows ? Math.max(0, len(o.r, axis) - main.natural) : 0;
3905
- };
3906
- const ends = (l, before, after) => {
3907
- const at = (o) => {
3908
- const it = l.items[o.id];
3909
- return it && it.edge === e.id && !it.inMenu && !it.floating ? it.rect : null;
3910
- };
3911
- const rb = before ? at(before) : void 0;
3912
- const ra = after ? at(after) : void 0;
3913
- if (rb === null || ra === null) return null;
3914
- const el = l.edges[e.id]?.rect ?? drawn.rect;
3915
- return {
3916
- a: before && rb ? lo(rb, axis) + len(rb, axis) - slack({ ...before, r: rb }) + gap : lo(el, axis) + nonNeg(e.pad?.start),
3917
- // (Not before a growing item: a dock there keeps no space, the item it precedes takes all that is spare.)
3918
- b: ra ? lo(ra, axis) - gap : lo(el, axis) + len(el, axis) - nonNeg(e.pad?.end)
3919
- };
3920
- };
3921
- for (let i = 0; i <= others.length; i++) {
3922
- const before = others[i - 1];
3923
- const after = others[i];
3924
- const zones = [...!before || zoneOf2(before) === "start" ? ["start"] : [], ...!after || zoneOf2(after) === "end" ? ["end"] : []];
3925
- const atOf = (zone) => others.slice(0, i).filter((o) => zoneOf2(o) === zone).length;
3926
- const probe = (() => {
3927
- const zone = zones[0];
3928
- if (!zone) return null;
3929
- const next = moveTo(scene, dragged, { edge: e.id, zone, at: atOf(zone), space: 0 });
3930
- if (next === scene) return null;
3931
- const l = solve(next, prev);
3932
- const landed = l.items[dragged];
3933
- if (!landed || landed.inMenu || landed.edge !== e.id) return null;
3934
- const n = neighbors(l, e.id, dragged, axis);
3935
- if (n.before !== (before?.id ?? "^") || n.next !== (after?.id ?? "$")) return null;
3936
- const r = ends(l, before, after);
3937
- return r && { ...r, w: len(landed.rect, axis), layout: l };
3938
- })();
3939
- const seenEnds = ends(base, before, after);
3940
- const pushed = (o, side) => !!o && !!probe && givesWay(base.items[o.id], probe.layout.items[o.id], e.id, side, (r) => [lo(r, axis), lo(r, axis) + len(r, axis) - slack({ ...o, r })]) === "moves";
3941
- const a = probe && (zones.includes("start") || !pushed(before, "before")) ? probe.a : seenEnds.a;
3942
- const b = probe && (zones.includes("end") || !pushed(after, "after")) ? probe.b : seenEnds.b;
3943
- const wi = probe?.w ?? w;
3944
- each({ before, after, a, b, w: wi, zones, atOf, spaceAt: (zone, x0) => zone === "start" ? x0 - a : b - (x0 + wi) });
3945
- }
3946
- }
3947
- }
3948
- function takeoverSpots(scene, dragged, options, seen, lifted, raws, pitch) {
3949
- const out = [];
3950
- stretches(scene, dragged, options, seen, lifted, raws, (run) => {
3951
- const { e, axis } = run;
3952
- const taken = [...run.mine.map((r) => r.landing)];
3953
- const emit3 = (zone, at, space, x0, nb) => {
3954
- const hit = landsAt(scene, dragged, run, { edge: e.id, zone, at, space }, x0);
3955
- if (!hit) return;
3956
- const { next, layout } = hit;
3957
- const landed = layout.items[dragged];
3958
- if (taken.some((r) => Math.abs(lo(r, axis) - lo(landed.rect, axis)) < pitch / 2)) return;
3959
- taken.push(landed.rect);
3960
- const n = neighbors(layout, e.id, dragged, axis);
3961
- const result = { scene: next, layout };
3962
- const target = { id: `pitch:${e.id}:${zone}:${at}:in:${nb}`, priority: 2, hit: landed.rect, slot: landed.rect, item: dragged, place: { kind: "gap", edge: e.id, zone, at }, after: () => result };
3963
- out.push({ lane: e.id, edge: e.id, axis, id: `${e.id}/${zone}/${n.before}|${n.next}@in:${nb}`, ...n, landing: landed.rect, layout, target, hit: landed.rect });
3964
- };
3965
- return ({ w, zones, atOf, spaceAt, before, after }) => {
3966
- const zoneOf2 = (id) => scene.items[id]?.align ?? "start";
3967
- if (after && zones.includes("start") && zoneOf2(after.id) === "start" && !scene.items[after.id]?.fixed) {
3968
- const x0 = lo(after.r, axis);
3969
- emit3("start", atOf("start"), Math.max(0, spaceAt("start", x0)), x0, after.id);
3970
- }
3971
- if (before && zones.includes("end") && zoneOf2(before.id) === "end" && !scene.items[before.id]?.fixed) {
3972
- const x0 = lo(before.r, axis) + len(before.r, axis) - w;
3973
- emit3("end", atOf("end"), Math.max(0, spaceAt("end", x0)), x0, before.id);
3974
- }
3975
- };
3976
- });
3977
- return out;
3978
- }
3979
- function landsAt(scene, dragged, run, place, x0) {
3980
- const next = moveTo(scene, dragged, place);
3981
- if (next === scene) return null;
3982
- const layout = solve(next, run.prev);
3983
- const landed = layout.items[dragged];
3984
- const { axis } = run;
3985
- if (!landed || landed.inMenu || landed.edge !== run.e.id || Math.abs(lo(landed.rect, axis) - x0) > 1) return null;
3986
- if (run.fixed.some((f) => {
3987
- const r = layout.items[f.id]?.rect;
3988
- return !r || Math.abs(lo(r, axis) - lo(f.r, axis)) > 1;
3989
- })) return null;
3990
- return { next, layout };
3991
- }
3992
- function slidingSpots(scene, dragged, options, seen, lifted, raws, sample) {
3993
- const spots = [];
3994
- const absorbed = /* @__PURE__ */ new Set();
3995
- const snapDistance = Number.isFinite(options.snapDistance) && options.snapDistance >= 0 ? options.snapDistance : SNAP_DISTANCE;
3996
- const own = seen.items[dragged];
3997
- const ownEdge = own && !own.floating && !own.inMenu ? own.edge : void 0;
3998
- stretches(scene, dragged, options, seen, lifted, raws, (run) => ({ a, b, w, zones, atOf, spaceAt, before, after }) => {
3999
- const { e, axis, middle, mine } = run;
4000
- if (zones.length === 0) return;
4001
- {
4002
- const land = (x0) => {
4003
- const near = x0 + w / 2 < middle ? "start" : "end";
4004
- for (const zone2 of [near, near === "start" ? "end" : "start"]) {
4005
- if (!zones.includes(zone2)) continue;
4006
- const space = spaceAt(zone2, x0);
4007
- if (space < -EPS) continue;
4008
- const place = { edge: e.id, zone: zone2, at: atOf(zone2), space: Math.max(0, space) };
4009
- const hit = landsAt(scene, dragged, run, place, x0);
4010
- if (hit) return { place, result: { scene: hit.next, layout: hit.layout } };
4011
- }
4012
- return null;
4013
- };
4014
- const tries = [a];
4015
- for (let x0 = a + sample; x0 < b - w - EPS; x0 += sample) tries.push(x0);
4016
- if (b - w > a + EPS) tries.push(b - w);
4017
- const ok = tries.map((x0) => ({ x0, hit: land(x0) })).filter((t) => t.hit);
4018
- if (ok.length === 0) return;
4019
- const from = ok[0].x0;
4020
- const to = ok.at(-1).x0;
4021
- const bId = before?.id ?? "^";
4022
- const nId = after?.id ?? "$";
4023
- if (to - from < STEP) {
4024
- if (mine.some((r) => !r.slide && Math.abs(lo(r.landing, axis) - from) <= 1)) return;
4025
- const firstZone = (() => {
4026
- for (const zone3 of zones) {
4027
- const space = spaceAt(zone3, from);
4028
- if (space < -EPS) continue;
4029
- const place2 = { edge: e.id, zone: zone3, at: atOf(zone3), space: Math.max(0, space) };
4030
- const hit = landsAt(scene, dragged, run, place2, from);
4031
- if (hit) return { place: place2, result: { scene: hit.next, layout: hit.layout } };
4032
- }
4033
- return null;
4034
- })();
4035
- const { place, result } = firstZone ?? ok[0].hit;
4036
- const landed = result.layout.items[dragged].rect;
4037
- const n = neighbors(result.layout, e.id, dragged, axis);
4038
- const zone2 = place.zone;
4039
- const isHome = ownEdge === e.id && !!own && Math.abs(lo(own.rect, axis) - lo(landed, axis)) <= 1 && Math.abs(len(own.rect, axis) - len(landed, axis)) <= 1;
4040
- const target2 = { id: `pitch:${e.id}:${zone2}:${place.at}:${Math.round(place.space ?? 0)}`, priority: 2, hit: landed, slot: landed, item: dragged, place: { kind: "gap", edge: e.id, zone: zone2, at: place.at }, after: () => result };
4041
- const plain = `${e.id}/${zone2}/${n.before}|${n.next}`;
4042
- const id = mine.some((r) => r.id === plain) ? `${plain}@${Math.round((place.space ?? 0) / sample)}` : plain;
4043
- spots.push({ lane: e.id, edge: e.id, axis, id, ...n, landing: landed, layout: result.layout, target: target2, hit: landed, ...isHome ? { home: true } : {} });
4044
- return;
4045
- }
4046
- const inside = mine.filter((r) => !r.slide && r.before === bId && r.next === nId && Math.abs(len(r.landing, axis) - w) <= 1 && lo(r.landing, axis) >= from - 1 && lo(r.landing, axis) <= to + 1);
4047
- for (const r of inside) absorbed.add(r);
4048
- const homeRaw = inside.find((r) => r.home);
4049
- const points = [];
4050
- const put = (at2, raw) => {
4051
- const same = points.find((p) => Math.abs(p.at - at2) < 1e-6);
4052
- if (!same) points.push({ at: at2, ...raw ? { raw } : {} });
4053
- else if (raw && !same.raw) same.raw = raw;
4054
- };
4055
- put(from);
4056
- put(to);
4057
- for (const r of [...inside].sort((p, q) => p.home ? -1 : q.home ? 1 : 0)) put(lo(r.landing, axis), r);
4058
- const homeAt = homeRaw ? lo(homeRaw.landing, axis) : void 0;
4059
- const kept = points.filter((p) => homeAt === void 0 || Math.abs(p.at - homeAt) < 1e-6 || Math.abs(p.at - homeAt) > 2 * snapDistance).sort((p, q) => p.at - q.at);
4060
- const snaps = kept.map((p) => p.at);
4061
- const dockAt = (x0) => kept.find((p) => Math.abs(p.at - x0) < 1e-6)?.raw;
4062
- const shape = ok[0].hit.result.layout.items[dragged].rect;
4063
- const at = (x0) => along(shape, axis, x0, x0 + w);
4064
- const snapped = (x0) => {
4065
- let best = x0;
4066
- let d = Infinity;
4067
- for (const q of snaps) if (Math.abs(q - x0) <= snapDistance && Math.abs(q - x0) < d) [best, d] = [q, Math.abs(q - x0)];
4068
- return best;
4069
- };
4070
- const startAt = (raw) => {
4071
- const c = clamp(raw, from, to);
4072
- const s = snapped(c);
4073
- return snaps.includes(s) ? s : clamp(Math.round(c / STEP) * STEP, from, to);
4074
- };
4075
- const results = /* @__PURE__ */ new Map();
4076
- const commitAt = (x0) => {
4077
- const r = dockAt(x0);
4078
- const k = `${r ? "dock " : ""}${Math.round(x0 * 1e3) / 1e3}`;
4079
- const known = results.get(k);
4080
- if (known) return known;
4081
- const result = r ? r.target.after(ORIGIN, ORIGIN) : land(x0)?.result ?? ok.reduce((p, q) => Math.abs(q.x0 - x0) < Math.abs(p.x0 - x0) ? q : p).hit.result;
4082
- results.set(k, result);
4083
- return result;
4084
- };
4085
- const rest = homeRaw ? lo(homeRaw.landing, axis) : ownEdge === e.id && own ? snapped(clamp(lo(own.rect, axis), from, to)) : from;
4086
- const restResult = commitAt(rest);
4087
- const ownZone = own?.zone;
4088
- const restPlace = (() => {
4089
- if (homeRaw) return ownZone && zones.includes(ownZone) ? { zone: ownZone, at: atOf(ownZone), space: Math.max(0, spaceAt(ownZone, rest)) } : land(rest)?.place;
4090
- const p = dockAt(rest)?.target.place;
4091
- return p && p.kind === "gap" ? p : land(rest)?.place;
4092
- })();
4093
- const landing = homeRaw ? homeRaw.landing : restResult.layout.items[dragged]?.rect ?? at(rest);
4094
- const ownLen = own ? Math.max(own.rect.width, own.rect.height) : 0;
4095
- const long = own && own.rect.width >= own.rect.height ? "x" : "y";
4096
- const target = {
4097
- id: `slide:${e.id}:${bId}|${nId}`,
4098
- priority: 2,
4099
- hit: hull2(at(from), at(to)),
4100
- slot: landing,
4101
- item: dragged,
4102
- place: { kind: "gap", edge: e.id, zone: restPlace?.zone ?? zones[0], at: restPlace?.at ?? atOf(zones[0]), ...restPlace?.space ? { space: restPlace.space } : {} },
4103
- after: (point, grab) => {
4104
- const g = grab ? grab[long] : ownLen / 2;
4105
- const share = axis === long && ownLen > EPS && Number.isFinite(g) ? clamp(g / ownLen, 0, 1) : 0.5;
4106
- return commitAt(startAt((axis === "x" ? point.x : point.y) - share * w));
4107
- }
4108
- };
4109
- const zone = zones[0];
4110
- spots.push({
4111
- lane: e.id,
4112
- edge: e.id,
4113
- axis,
4114
- id: `${e.id}/${zone}/${bId}|${nId}@slide`,
4115
- before: bId,
4116
- next: nId,
4117
- landing,
4118
- layout: restResult.layout,
4119
- target,
4120
- hit: target.hit,
4121
- ...homeRaw ? { home: true } : {},
4122
- slide: { from, to, snaps, snapDistance, step: STEP, first: at(from), last: at(to) }
4123
- });
4124
- }
4125
- });
4126
- return { spots, absorbed };
4127
- }
4128
-
4129
- export {
4130
- SIDES,
4131
- ZONES,
4132
- FLOAT_ANCHORS,
4133
- RAIL_MODES,
4134
- RAIL_VISIBILITIES,
4135
- RAIL_STRIP_MIN,
4136
- DEFAULT_RAIL_MAGNET,
4137
- EDGE_ROOM_HOLD,
4138
- DEFAULT_STRIP,
4139
- DEFAULT_HIT_BAND,
4140
- DEFAULT_FLOAT_SIZE,
4141
- heldRect,
4142
- anchorName,
4143
- zoneTakes,
4144
- zonesTaking,
4145
- edgeTakes,
4146
- railModeOf,
4147
- zoneIn,
4148
- namesZone,
4149
- isOutsideSideBar,
4150
- isSideBar,
4151
- isSideColumn,
4152
- floatOrientOf,
4153
- declaresFloatSizes,
4154
- floatSizeOf,
4155
- targetId,
4156
- parseTargetId,
4157
- virtualEdgeId,
4158
- placeTargetId,
4159
- solve,
4160
- fitResize,
4161
- mergeSideColumns,
4162
- lift,
4163
- moveTo,
4164
- floatAt,
4165
- canFold,
4166
- fold,
4167
- use,
4168
- keep,
4169
- open,
4170
- close,
4171
- raise,
4172
- orient,
4173
- resize,
4174
- setRailMode,
4175
- setSide,
4176
- homeFor,
4177
- dockHome,
4178
- storeScene,
4179
- readScene,
4180
- reservedRect,
4181
- targets,
4182
- refusals,
4183
- refusalTargets,
4184
- stops,
4185
- SPOT_PITCH,
4186
- lanes,
4187
- HATCH_END,
4188
- HATCH_EDGE
4189
- };
4190
- //# sourceMappingURL=chunk-ZTH5FCUO.js.map