@n-uf/placement 26.11.23 → 26.11.25

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 (49) hide show
  1. package/CHANGELOG.md +82 -0
  2. package/README.md +72 -31
  3. package/dist/{chunk-LPMZ2FFR.js → chunk-3P6VOXA6.js} +2 -2
  4. package/dist/chunk-3P6VOXA6.js.map +1 -0
  5. package/dist/{chunk-CJA6LRHN.js → chunk-7ASCUAGV.js} +5 -7
  6. package/dist/chunk-7ASCUAGV.js.map +1 -0
  7. package/dist/{chunk-RL3QB5SM.js → chunk-KGZCYLQN.js} +10 -10
  8. package/dist/chunk-KGZCYLQN.js.map +1 -0
  9. package/dist/{chunk-2H3CSPEH.js → chunk-OYGOJWAX.js} +2 -2
  10. package/dist/{chunk-6WFVXHA3.js → chunk-SZKU7CRX.js} +582 -501
  11. package/dist/chunk-SZKU7CRX.js.map +1 -0
  12. package/dist/chunk-WM5FJ3IS.js +47 -0
  13. package/dist/chunk-WM5FJ3IS.js.map +1 -0
  14. package/dist/{constructions-VDK_mwmz.d.ts → constructions-DSEjvWMP.d.ts} +1 -1
  15. package/dist/desk.d.ts +3 -3
  16. package/dist/desk.js +3 -3
  17. package/dist/dock.d.ts +3 -3
  18. package/dist/dock.js +2 -2
  19. package/dist/geometry-CkZ6Fcfb.d.ts +48 -0
  20. package/dist/index.d.ts +3 -2
  21. package/dist/index.js +21 -3
  22. package/dist/index.js.map +1 -1
  23. package/dist/{model-DPTu2EtG.d.ts → model-mm16NrSX.d.ts} +1 -1
  24. package/dist/panels.d.ts +5 -4
  25. package/dist/panels.js +6 -5
  26. package/dist/scene-cases.d.ts +4 -4
  27. package/dist/scene-cases.js +15 -16
  28. package/dist/scene-cases.js.map +1 -1
  29. package/dist/scene.d.ts +5 -39
  30. package/dist/scene.js +5 -3
  31. package/dist/stack.d.ts +4 -4
  32. package/dist/stack.js +2 -2
  33. package/dist/stack.js.map +1 -1
  34. package/dist/{target-D3PiLg44.d.ts → target-C-HJjPcz.d.ts} +1 -1
  35. package/dist/{targets-B6eLC-52.d.ts → targets-CBpO_LEy.d.ts} +4 -6
  36. package/dist/{targets-DgmHP5PY.d.ts → targets-CDR5Towj.d.ts} +83 -65
  37. package/dist/targets.d.ts +4 -4
  38. package/dist/targets.js +6 -7
  39. package/dist/targets.js.map +1 -1
  40. package/dist/toolbar.d.ts +1 -1
  41. package/package.json +1 -1
  42. package/dist/chunk-6WFVXHA3.js.map +0 -1
  43. package/dist/chunk-CJA6LRHN.js.map +0 -1
  44. package/dist/chunk-LPMZ2FFR.js.map +0 -1
  45. package/dist/chunk-QMTNB2C3.js +0 -17
  46. package/dist/chunk-QMTNB2C3.js.map +0 -1
  47. package/dist/chunk-RL3QB5SM.js.map +0 -1
  48. package/dist/geometry-mL-GTRti.d.ts +0 -28
  49. /package/dist/{chunk-2H3CSPEH.js.map → chunk-OYGOJWAX.js.map} +0 -0
@@ -5,11 +5,19 @@ import {
5
5
  formMain,
6
6
  formSetsCross,
7
7
  wrapLines
8
- } from "./chunk-RL3QB5SM.js";
8
+ } from "./chunk-KGZCYLQN.js";
9
9
  import {
10
+ COLUMN_REACH,
11
+ EPS,
12
+ PLANE,
13
+ RAIL_EDGE,
14
+ area,
10
15
  clampInto,
11
- rect
12
- } from "./chunk-QMTNB2C3.js";
16
+ contains,
17
+ overlaps,
18
+ rect,
19
+ subtract
20
+ } from "./chunk-WM5FJ3IS.js";
13
21
 
14
22
  // src/scene/model.ts
15
23
  var SIDES = ["top", "right", "bottom", "left"];
@@ -23,7 +31,10 @@ var axisOf = (side) => side === "left" || side === "right" ? "column" : "row";
23
31
  var zoneOf = (item) => item?.align === "center" || item?.align === "end" ? item.align : "start";
24
32
  var formsOf = (item, axis) => item.forms[axis] ?? [];
25
33
  function isOutsideSideBar(scene, e) {
26
- if (e.side !== "left" && e.side !== "right" || e.outsideInset !== true || e.column === true || e.accepts.length === 0) return false;
34
+ return e.outsideInset === true && isSideBar(scene, e);
35
+ }
36
+ function isSideBar(scene, e) {
37
+ if (e.side !== "left" && e.side !== "right" || e.column === true || e.accepts.length === 0) return false;
27
38
  const template = scene.columns?.[e.side];
28
39
  const rail = scene.rails?.[e.side];
29
40
  if (!template && !rail) return false;
@@ -67,7 +78,6 @@ function floatShape(item, f) {
67
78
  }
68
79
 
69
80
  // src/scene/solve.ts
70
- var EPS = 1e-6;
71
81
  var finite = (x, fallback) => typeof x === "number" && Number.isFinite(x) ? x : fallback;
72
82
  var px = (x) => Math.max(0, finite(x, 0));
73
83
  function cutOrder(edges) {
@@ -75,6 +85,16 @@ function cutOrder(edges) {
75
85
  const sideRank = (e) => e.side === "top" || e.side === "left" ? 0 : 1;
76
86
  return edges.map((e, i) => ({ e, i })).sort((a, b) => phase(a.e) - phase(b.e) || sideRank(a.e) - sideRank(b.e) || finite(a.e.ring, 0) - finite(b.e.ring, 0) || a.i - b.i).map((x) => x.e);
77
87
  }
88
+ function layKeys(scene) {
89
+ const isSide = (e) => e.side === "left" || e.side === "right";
90
+ const phase = (e) => e.mode === "overlay" ? 4 : isSide(e) ? 2 : e.span === "between" ? 3 : e.outsideInset === true ? 0 : 1;
91
+ return new Map(cutOrder(scene.edges).map((e, at) => [e.id, { phase: phase(e), side: e.side, at }]));
92
+ }
93
+ function pushes(a, b) {
94
+ if (a.phase === 4 || b.phase === 4) return false;
95
+ if (a.phase !== b.phase) return a.phase < b.phase;
96
+ return a.side === b.side && a.at < b.at;
97
+ }
78
98
  var ZONE_RANK = { start: 0, center: 1, end: 2 };
79
99
  function zoneSorted(scene, ids) {
80
100
  return ids.map((id, i) => ({ id, i, z: ZONE_RANK[zoneOf(scene.items[id])] })).sort((a, b) => a.z - b.z || a.i - b.i).map((x) => x.id);
@@ -362,6 +382,7 @@ function solve(given, options = {}) {
362
382
  return edge.side === "top" ? rect(area3.x, area3.y + thick, area3.width, area3.height - thick) : edge.side === "bottom" ? rect(area3.x, area3.y, area3.width, area3.height - thick) : edge.side === "left" ? rect(area3.x + thick, area3.y, area3.width - thick, area3.height) : rect(area3.x, area3.y, area3.width - thick, area3.height);
363
383
  };
364
384
  const rails = {};
385
+ const railPlaces = {};
365
386
  const gutter = px(scene.gutter);
366
387
  let columnRoom;
367
388
  let outerArea = stage;
@@ -471,7 +492,8 @@ function solve(given, options = {}) {
471
492
  });
472
493
  const colMins = open2.length > 0 ? open2 : templates;
473
494
  if (cmin !== void 0 && colMins.length > 0) {
474
- const width = area3.width - (scene.insetWhen === "docked" && !docked ? px(scene.inset?.left) + px(scene.inset?.right) : 0);
495
+ const bars = cols.filter((c) => c.width > 0 && !c.dyn && isSideBar(scene, c.edge)).reduce((s, c) => s + c.width + (c.edge.outsideInset === true ? 0 : gutter), 0);
496
+ const width = area3.width - bars - (scene.insetWhen === "docked" && !docked ? px(scene.inset?.left) + px(scene.inset?.right) : 0);
475
497
  const need = cmin + Math.min(...colMins) + gutter;
476
498
  columnRoom = width >= need - EPS;
477
499
  if (!columnRoom) notes.push({ code: "no-column-room", width, need });
@@ -499,19 +521,23 @@ function solve(given, options = {}) {
499
521
  let railRect = null;
500
522
  let gap = px(side === "left" ? inset.left : inset.right);
501
523
  let barLaid = false;
524
+ let railPlace = null;
525
+ const placeRail = () => {
526
+ const w = railW(side);
527
+ const lead = barLaid ? gap : 0;
528
+ return side === "left" ? rect(x0 - lead, area3.y, w, H) : rect(x1 + lead - w, area3.y, w, H);
529
+ };
502
530
  for (const c of mine) {
503
- if (!railRect && railed.length > 0 && !outsideSideBar(c)) {
504
- const w2 = railW(side);
505
- const lead = barLaid ? gap : 0;
506
- const after = barLaid ? Math.max(gap, gutter) : gutter;
507
- if (side === "left") {
508
- railRect = rect(x0 - lead, area3.y, w2, H);
509
- x0 += w2 - lead + after;
510
- } else {
511
- railRect = rect(x1 + lead - w2, area3.y, w2, H);
512
- x1 -= w2 - lead + after;
531
+ if (!railPlace && !outsideSideBar(c)) {
532
+ railPlace = placeRail();
533
+ if (railed.length > 0) {
534
+ railRect = railPlace;
535
+ const lead = barLaid ? gap : 0;
536
+ const after = barLaid ? Math.max(gap, gutter) : gutter;
537
+ if (side === "left") x0 += railPlace.width - lead + after;
538
+ else x1 -= railPlace.width - lead + after;
539
+ gap = after;
513
540
  }
514
- gap = after;
515
541
  }
516
542
  if (outsideSideBar(c)) barLaid = true;
517
543
  const base = { side: c.edge.side, ring: finite(c.edge.ring, 0), cross: 0, menu: null, tabs: [], stack: c.solved.stack, free: c.solved.free };
@@ -589,6 +615,7 @@ function solve(given, options = {}) {
589
615
  }
590
616
  rails[side] = { rect: railRect, segments };
591
617
  }
618
+ if (scene.rails?.[side]) railPlaces[side] = railPlace ?? placeRail();
592
619
  }
593
620
  return rect(x0, area3.y, Math.max(0, x1 - x0), H);
594
621
  };
@@ -654,7 +681,7 @@ function solve(given, options = {}) {
654
681
  items[f.id] = { ...shapeOf(form, fit.rect, fo, fit.rect.width), rect: fit.rect, kind: it.kind, form: from, formKind: form?.kind ?? "open", floating: true };
655
682
  floats.push(f.id);
656
683
  }
657
- return { items, edges, content, floats, rails, ...columnRoom !== void 0 ? { columnRoom } : {}, notes };
684
+ return { items, edges, content, floats, rails, railPlaces, ...columnRoom !== void 0 ? { columnRoom } : {}, notes };
658
685
  }
659
686
 
660
687
  // src/scene/changes.ts
@@ -1052,145 +1079,376 @@ function read(data, base) {
1052
1079
  }
1053
1080
 
1054
1081
  // src/scene/targets.ts
1055
- var ZONE_ORDER = { start: 0, center: 1, end: 2 };
1056
- var EPS2 = 1e-6;
1057
- var PLANE = rect(-1e9, -1e9, 2e9, 2e9);
1058
- var area = (r) => Math.max(0, r.width) * Math.max(0, r.height);
1059
- var overlaps = (a, b) => Math.min(a.x + a.width, b.x + b.width) - Math.max(a.x, b.x) > EPS2 && Math.min(a.y + a.height, b.y + b.height) - Math.max(a.y, b.y) > EPS2;
1060
- var contains = (r, p) => p.x >= r.x - EPS2 && p.x <= r.x + r.width + EPS2 && p.y >= r.y - EPS2 && p.y <= r.y + r.height + EPS2;
1061
1082
  var RAIL_REACH = 24;
1062
- function cutAway(hit, blockers) {
1063
- let h = hit;
1064
- for (const b of blockers) {
1065
- if (!overlaps(h, b)) continue;
1066
- const right = h.x + h.width;
1067
- const bottom = h.y + h.height;
1068
- const parts = [
1069
- rect(h.x, h.y, Math.min(right, b.x) - h.x, h.height),
1070
- rect(Math.max(h.x, b.x + b.width), h.y, right - Math.max(h.x, b.x + b.width), h.height),
1071
- rect(h.x, h.y, h.width, Math.min(bottom, b.y) - h.y),
1072
- rect(h.x, Math.max(h.y, b.y + b.height), h.width, bottom - Math.max(h.y, b.y + b.height))
1073
- ].filter((p) => area(p) > EPS2);
1074
- if (parts.length === 0) return null;
1075
- h = parts.reduce((a, p) => area(p) > area(a) ? p : a);
1076
- }
1077
- return area(h) > EPS2 ? h : null;
1083
+ var EDGE_KINDS = {
1084
+ bar: { zoneExtent: "thirds", opens: "any", inward: 0, place: DEFAULT_HIT_BAND / 2, phase: null, layer: 10, overreach: 30, priority: 2, reach: Infinity, virtual: false },
1085
+ sideBar: { zoneExtent: "thirds", opens: "any", inward: 0, place: DEFAULT_HIT_BAND / 2, phase: null, layer: 11, overreach: 31, priority: 2, reach: Infinity, virtual: false },
1086
+ column: { zoneExtent: "solved", opens: "start", inward: 0, place: DEFAULT_HIT_BAND / 2, phase: null, layer: 12, overreach: 32, priority: 2, reach: Infinity, virtual: false },
1087
+ barBetween: { zoneExtent: "thirds", opens: "any", inward: 0, place: DEFAULT_HIT_BAND / 2, phase: null, layer: 13, overreach: 33, priority: 2, reach: Infinity, virtual: false },
1088
+ rail: { zoneExtent: "thirds", opens: "any", inward: RAIL_REACH, place: DEFAULT_HIT_BAND / 2, phase: null, layer: 14, overreach: 34, priority: 2, reach: Infinity, virtual: false },
1089
+ railStop: { zoneExtent: "whole", opens: "any", inward: 0, place: DEFAULT_HIT_BAND / 2, phase: 2, layer: 9, overreach: 39, priority: 1, reach: Infinity, virtual: false },
1090
+ columnLine: { zoneExtent: "whole", opens: "any", inward: 0, place: DEFAULT_HIT_BAND / 2, phase: 2, layer: 21, overreach: 41, priority: 2, reach: COLUMN_REACH, virtual: true }
1091
+ };
1092
+ function edgeKindOf(scene, e) {
1093
+ if (e.compact) return "rail";
1094
+ if (e.side === "top" || e.side === "bottom") return e.span === "between" ? "barBetween" : "bar";
1095
+ return isSideBar(scene, e) ? "sideBar" : "column";
1078
1096
  }
1079
- function cutClear(hit, blockers, keep2) {
1080
- let h = hit;
1081
- for (const b of blockers) {
1082
- if (!overlaps(h, b)) continue;
1083
- const right = h.x + h.width;
1084
- const bottom = h.y + h.height;
1085
- const parts = [
1086
- rect(h.x, h.y, Math.min(right, b.x) - h.x, h.height),
1087
- rect(Math.max(h.x, b.x + b.width), h.y, right - Math.max(h.x, b.x + b.width), h.height),
1088
- rect(h.x, h.y, h.width, Math.min(bottom, b.y) - h.y),
1089
- rect(h.x, Math.max(h.y, b.y + b.height), h.width, bottom - Math.max(h.y, b.y + b.height))
1090
- ].filter((p) => area(p) > EPS2 && contains(p, keep2));
1091
- if (parts.length === 0) return null;
1092
- h = parts.reduce((a, p) => area(p) > area(a) ? p : a);
1097
+ var opensEmpty = (scene, edge, kind) => EDGE_KINDS[edgeKindOf(scene, edge)].opens === "any" || zoneTakes(edge, "start", kind);
1098
+ function reservedRect(scene, l, id) {
1099
+ const it = l.items[id];
1100
+ const e = it.edge ? l.edges[it.edge] : void 0;
1101
+ const f = e ? formsOf(scene.items[id], axisOf(e.side))[it.form] : void 0;
1102
+ if (!e || !f?.main.grows) return it.rect;
1103
+ const column = axisOf(e.side) === "column";
1104
+ const len = column ? it.rect.height : it.rect.width;
1105
+ const keep2 = Math.min(len, Math.max(0, Number.isFinite(f.main.natural) ? f.main.natural : len));
1106
+ const shift = it.zone === "end" ? len - keep2 : it.zone === "center" ? (len - keep2) / 2 : 0;
1107
+ return column ? rect(it.rect.x, it.rect.y + shift, it.rect.width, keep2) : rect(it.rect.x + shift, it.rect.y, keep2, it.rect.height);
1108
+ }
1109
+ function placeKey(s, id) {
1110
+ const it = Object.hasOwn(s.items, id) ? s.items[id] : void 0;
1111
+ const byZone = (items) => ZONES.flatMap((z) => items.filter((x) => zoneOf(s.items[x]) === z));
1112
+ return JSON.stringify({
1113
+ edges: s.edges.map((e) => JSON.stringify([e.side, e.ring, e.column === true ? "" : e.id, byZone(e.items), e.size ?? null])).sort(),
1114
+ floats: s.floats.map((f) => [f.id, f.rect, f.anchor ?? null, f.orient ?? null]),
1115
+ from: it?.from ?? 0,
1116
+ align: it?.align ?? "start"
1117
+ });
1118
+ }
1119
+ function slotAt(c, zone, line, id, place, lands, home = false) {
1120
+ const result = c.settle(use(moveTo(c.scene, c.dragged, place), c.dragged));
1121
+ const isHome = home || c.isHome(result);
1122
+ return { zone, line, id, place, result, home: isHome, ok: isHome || !!result.layout.items[c.dragged] && lands(result) && c.sound(result.layout) };
1123
+ }
1124
+ var less = (spans, away) => away.reduce((acc, [ra, rb]) => acc.flatMap(([a, b]) => rb <= a || ra >= b ? [[a, b]] : [[a, Math.max(a, ra)], [Math.min(b, rb), b]].filter(([u, w]) => w - u > EPS)), [...spans]);
1125
+ function zoneSpans(scene, base, edge, reach, probe) {
1126
+ const el = base.edges[edge.id];
1127
+ const column = axisOf(edge.side) === "column";
1128
+ const main0 = column ? el.rect.y : el.rect.x;
1129
+ const length = column ? el.rect.height : el.rect.width;
1130
+ if (EDGE_KINDS[edgeKindOf(scene, edge)].zoneExtent === "thirds")
1131
+ return ZONES.map((zone, k) => ({ zone, a: k === 0 ? main0 - reach : main0 + length * k / 3, b: k === 2 ? main0 + length + reach : main0 + length * (k + 1) / 3 }));
1132
+ const at = (r) => column ? [r.y, r.y + r.height] : [r.x, r.x + r.width];
1133
+ const declared = new Set(edge.zoneAccepts ? ZONES.filter((z) => edge.zoneAccepts[z] !== void 0) : ["start"]);
1134
+ const placed = /* @__PURE__ */ new Map();
1135
+ for (const [id, it] of Object.entries(base.items)) {
1136
+ if (it.edge !== edge.id || it.inMenu) continue;
1137
+ const z = zoneOf(scene.items[id]);
1138
+ const [a, b] = at(it.rect);
1139
+ const was = placed.get(z);
1140
+ placed.set(z, was ? [Math.min(was[0], a), Math.max(was[1], b)] : [a, b]);
1141
+ declared.add(z);
1093
1142
  }
1094
- return area(h) > EPS2 ? h : null;
1143
+ const anchor = { start: main0, center: main0 + length / 2, end: main0 + length };
1144
+ const spans = ZONES.filter((z) => declared.has(z)).map((zone) => ({ zone, s: placed.get(zone) ?? probe(zone) ?? [anchor[zone], anchor[zone]] }));
1145
+ return spans.map(({ zone, s }, i) => ({
1146
+ zone,
1147
+ a: i === 0 ? main0 - reach : (spans[i - 1].s[1] + s[0]) / 2,
1148
+ b: i === spans.length - 1 ? main0 + length + reach : (s[1] + spans[i + 1].s[0]) / 2
1149
+ }));
1095
1150
  }
1096
- var GAP_KEEP = 16;
1097
- function homeOnlyOverItsPlace(out, now) {
1098
- const dragged = out[0]?.item;
1099
- const mine = dragged !== void 0 ? now.items[dragged] : void 0;
1100
- const side = mine?.edge ? now.edges[mine.edge]?.side : void 0;
1101
- if (!mine || !side) return;
1102
- const column = side === "left" || side === "right";
1103
- const r = mine.rect;
1104
- const band = DEFAULT_HIT_BAND / 2;
1105
- const edgeOf = (t) => t.id.startsWith("gap:") ? t.id.split(":")[1] : void 0;
1106
- const zoneOfId = (t) => t.id.startsWith("gap:") ? t.id.split(":")[2] : void 0;
1107
- const span = (x) => column ? [x.hit.y, x.hit.y + x.hit.height] : [x.hit.x, x.hit.x + x.hit.width];
1108
- const cross = (x) => column ? [x.hit.x, x.hit.x + x.hit.width] : [x.hit.y, x.hit.y + x.hit.height];
1109
- const make = (x, from, to, c) => {
1110
- const [c02, c12] = c ?? cross(x);
1111
- return { ...x, hit: column ? rect(c02, from, c12 - c02, to - from) : rect(from, c02, to - from, c12 - c02) };
1151
+ function edgeSpec(c, edge) {
1152
+ const { scene, rest, now, base, dragged, kind } = c;
1153
+ const el = base.edges[edge.id];
1154
+ if (!el || el.folded || el.rides) return null;
1155
+ const edgeKind = edgeKindOf(rest, edge);
1156
+ const data = EDGE_KINDS[edgeKind];
1157
+ const column = axisOf(edge.side) === "column";
1158
+ const at = (r2) => column ? r2.y : r2.x;
1159
+ const len = (r2) => column ? r2.height : r2.width;
1160
+ const main0 = at(el.rect);
1161
+ const length = len(el.rect);
1162
+ const empty = el.stack.items.every((p) => p.kind === "menu") && !el.menu;
1163
+ const accepts = edge.accepts.includes(kind);
1164
+ if (!accepts && (empty || area(el.rect) <= EPS)) return null;
1165
+ if (accepts && empty && !opensEmpty(rest, edge, kind)) return null;
1166
+ const ownNow = now.items[dragged];
1167
+ const ownHere = ownNow && ownNow.edge === edge.id && !ownNow.floating && !ownNow.inMenu ? ownNow.rect : null;
1168
+ const restEdge = rest.edges.find((e) => e.id === edge.id);
1169
+ const slotId = (zone, at2) => restEdge.items.length === 0 ? `strip:${edge.id}:${zone}` : `gap:${edge.id}:${zone}:${at2}`;
1170
+ const titles = edge.items.filter((id) => base.items[id]?.edge === edge.id && base.items[id].fixed && !!formsOf(scene.items[id], axisOf(edge.side))[base.items[id].form]?.main.grows);
1171
+ const lands = ({ layout: after }) => {
1172
+ const me = after.items[dragged];
1173
+ if (me.edge !== edge.id || me.inMenu) return false;
1174
+ if (titles.length === 0) return true;
1175
+ if (me.form !== 0) return false;
1176
+ return edge.items.every((id) => {
1177
+ const a = after.items[id];
1178
+ const b = base.items[id];
1179
+ if (!a || !b || b.edge !== edge.id || id === dragged) return true;
1180
+ if (!titles.includes(id)) return a.form === b.form && !!a.inMenu === !!b.inMenu;
1181
+ const f = formsOf(scene.items[id], axisOf(edge.side))[a.form];
1182
+ return !f || (column ? a.rect.height : a.rect.width) >= formMain(f, "natural", after.edges[edge.id].cross) - 0.5;
1183
+ });
1112
1184
  };
1113
- const homes = out.filter((t) => t.place.kind === "home" && edgeOf(t) === mine.edge);
1114
- const mid = column ? r.y + r.height / 2 : r.x + r.width / 2;
1115
- let h0 = (column ? r.y : r.x) - band;
1116
- let h1 = (column ? r.y + r.height : r.x + r.width) + band;
1117
- const c0 = Math.min(...homes.map((h) => cross(h)[0]));
1118
- const c1 = Math.max(...homes.map((h) => cross(h)[1]));
1119
- const first = homes[0];
1120
- for (const h of homes.slice(1)) out.splice(out.indexOf(h), 1);
1121
- const at = first ? out.indexOf(first) : -1;
1122
- if (first) out.splice(at, 1);
1123
- for (let i = 0; i < out.length; i++) {
1124
- const x = out[i];
1125
- if (x.place.kind !== "gap" || edgeOf(x) !== mine.edge) continue;
1126
- const [s0, s1] = span(x);
1127
- if (s1 <= h0 + EPS2 || s0 >= h1 - EPS2) continue;
1128
- const keep2 = Math.min(GAP_KEEP, s1 - s0);
1129
- if (s0 < h0) {
1130
- const e = Math.min(s1, Math.max(h0, s0 + keep2));
1131
- out[i] = make(x, s0, e);
1132
- h0 = Math.max(h0, e);
1133
- } else if (s1 > h1) {
1134
- const b = Math.max(s0, Math.min(h1, s1 - keep2));
1135
- out[i] = make(x, b, s1);
1136
- h1 = Math.min(h1, b);
1137
- } else {
1138
- const line = x.marker ? column ? x.marker.y : x.marker.x : (s0 + s1) / 2;
1139
- let a = Math.min(Math.max(line - keep2 / 2, s0), s1 - keep2);
1140
- if (a <= mid + 1 && a + keep2 >= mid - 1) a = (s0 + s1) / 2 < mid ? mid - 1 - keep2 : mid + 1;
1141
- out[i] = make(x, a, a + keep2);
1142
- if (a + keep2 <= mid + EPS2) h0 = Math.max(h0, a + keep2);
1143
- else h1 = Math.min(h1, a);
1185
+ const pad = { start: Math.max(0, edge.pad?.start ?? 0), end: Math.max(0, edge.pad?.end ?? 0) };
1186
+ const menuLen = el.menu ? len(el.menu.rect) : 0;
1187
+ const occupied = (id) => base.items[id]?.fixed ? reservedRect(scene, base, id) : base.items[id].rect;
1188
+ const shown = (id) => base.items[id]?.edge === edge.id && !base.items[id].inMenu;
1189
+ const drawnOf = (z) => restEdge.items.filter((id) => zoneOf(rest.items[id]) === z && shown(id)).map(occupied);
1190
+ const startEnd = (() => {
1191
+ const s = drawnOf("start");
1192
+ return s.length ? at(s[s.length - 1]) + len(s[s.length - 1]) : main0 + pad.start;
1193
+ })();
1194
+ const endStart = (() => {
1195
+ const e = drawnOf("end");
1196
+ return e.length ? at(e[0]) : main0 + length - pad.end - menuLen;
1197
+ })();
1198
+ const anchorLine = { start: main0 + pad.start, center: Math.min(Math.max(main0 + length / 2, startEnd), Math.max(startEnd, endStart)), end: main0 + length - pad.end - menuLen };
1199
+ const slots = [];
1200
+ for (const zone of ZONES) {
1201
+ if (!accepts || !zoneTakes(edge, zone, kind)) continue;
1202
+ const members = restEdge.items.filter((id) => zoneOf(rest.items[id]) === zone);
1203
+ for (let k = 0; k <= members.length; k++) {
1204
+ const next = members.slice(k).find(shown);
1205
+ const before = members.slice(0, k).reverse().find(shown);
1206
+ const line = next ? at(occupied(next)) : before ? at(occupied(before)) + len(occupied(before)) : anchorLine[zone];
1207
+ const s = slotAt(c, zone, line, slotId(zone, k), { kind: "gap", edge: edge.id, zone, at: k }, lands);
1208
+ const movesFixed = restEdge.items.some((id) => {
1209
+ const b = base.items[id];
1210
+ const a = s.result.layout.items[id];
1211
+ return !!b?.fixed && b.edge === edge.id && (!a || Math.abs(a.rect.x - b.rect.x) > 0.5 || Math.abs(a.rect.y - b.rect.y) > 0.5);
1212
+ });
1213
+ if (!movesFixed) slots.push(s);
1144
1214
  }
1145
1215
  }
1146
- const others = (x) => out.filter((y) => y !== x && y.place.kind === "gap" && edgeOf(y) === mine.edge).map(span);
1147
- for (const h of homes) {
1148
- const [a0, a1] = span(h);
1149
- const near = out.filter((x) => x.place.kind === "gap" && edgeOf(x) === mine.edge && zoneOfId(x) === zoneOfId(h));
1150
- const before = near.find((x) => Math.abs(span(x)[1] - a0) < 1);
1151
- const after = near.find((x) => Math.abs(span(x)[0] - a1) < 1);
1152
- if (before && a0 < h0 - EPS2) {
1153
- const stop = Math.min(a1, h0, ...others(before).filter(([b]) => b >= span(before)[1] - EPS2).map(([b]) => b));
1154
- if (stop > span(before)[1]) out[out.indexOf(before)] = make(before, span(before)[0], stop);
1216
+ const spanOf = (r2) => [at(r2), at(r2) + len(r2)];
1217
+ const grows = (id) => !!formsOf(scene.items[id], axisOf(edge.side))[base.items[id].form]?.main.grows;
1218
+ const reserved = Object.entries(base.items).filter(([id, p]) => p.edge === edge.id && p.fixed && !p.inMenu && !grows(id)).map(([, p]) => spanOf(p.rect));
1219
+ const zones = zoneSpans(rest, base, edge, c.reach, (zone) => {
1220
+ const s = slots.find((x) => x.zone === zone && x.place.kind === "gap" && x.place.at === 0);
1221
+ const me = s?.result.layout.items[dragged];
1222
+ return me && me.edge === edge.id && me.zone === zone ? spanOf(me.rect) : null;
1223
+ });
1224
+ const r = el.rect;
1225
+ const strip = Math.max(0, Number.isFinite(edge.strip) ? edge.strip : DEFAULT_STRIP);
1226
+ const nowCross = now.edges[edge.id]?.cross ?? 0;
1227
+ const outer = edge.side === "left" ? r.x : edge.side === "right" ? r.x + r.width : edge.side === "top" ? r.y : r.y + r.height;
1228
+ const inward = (depth) => edge.side === "left" || edge.side === "top" ? [outer, outer + depth] : [outer - depth, outer];
1229
+ const across = (after) => inward((el.cross > 0 || nowCross > 0 ? Math.max(el.cross, nowCross, after?.edges[edge.id]?.cross ?? 0) : strip) + data.inward);
1230
+ const ownCross = ownNow?.edge === edge.id ? el.cross : Math.max(el.cross, nowCross);
1231
+ const drawn = ownCross > 0;
1232
+ const marker = (s) => {
1233
+ const slot = s.result.layout.items[dragged].rect;
1234
+ const e = s.result.layout.edges[edge.id];
1235
+ return column ? rect(e.rect.x, slot.y, e.cross, 0) : rect(slot.x, e.rect.y, 0, e.cross);
1236
+ };
1237
+ const takes = (zone) => accepts && zoneTakes(edge, zone, kind);
1238
+ return {
1239
+ id: edge.id,
1240
+ key: edge.id,
1241
+ kind: edgeKind,
1242
+ lay: c.lay.get(edge.id),
1243
+ vertical: column,
1244
+ zones,
1245
+ slots,
1246
+ refuse: (zone) => takes(zone) ? "no-room" : accepts || area(el.rect) > EPS ? "not-accepted" : null,
1247
+ own: ownHere,
1248
+ holds: !!ownNow && ownNow.edge === edge.id && !ownNow.floating,
1249
+ reserved,
1250
+ across,
1251
+ owned: inward(drawn ? ownCross : Math.min(strip, data.place)),
1252
+ ownedAlong: null,
1253
+ drawn,
1254
+ marker
1255
+ };
1256
+ }
1257
+ function tileEdge(c, spec, cuts, lost) {
1258
+ const data = EDGE_KINDS[spec.kind];
1259
+ const v = spec.vertical;
1260
+ const own = spec.own ? v ? [spec.own.y, spec.own.y + spec.own.height] : [spec.own.x, spec.own.x + spec.own.width] : null;
1261
+ const band = (a, b, after) => {
1262
+ const [c0, c1] = spec.across(after);
1263
+ return v ? rect(c0, a, c1 - c0, b - a) : rect(a, c0, b - a, c1 - c0);
1264
+ };
1265
+ const keepAt = (line, h) => v ? { x: h.x + h.width / 2, y: line } : { x: line, y: h.y + h.height / 2 };
1266
+ const tile = (zone, hit, more) => ({ edge: spec.id, key: spec.key, edgeKind: spec.kind, vertical: v, zone, hit, ...more });
1267
+ const refused = (zone, hit, reason) => tile(zone, hit, { kind: "refuse", id: "", order: data.layer, priority: data.priority, keep: null, reason });
1268
+ const reserved = spec.reserved;
1269
+ const split = (t) => {
1270
+ const h = t.hit;
1271
+ const [h0, h1] = v ? [h.x, h.x + h.width] : [h.y, h.y + h.height];
1272
+ const [c0, c1] = [Math.max(h0, spec.owned[0]), Math.min(h1, spec.owned[1])];
1273
+ const part = (a, b) => v ? rect(a, h.y, b - a, h.height) : rect(h.x, a, h.width, b - a);
1274
+ const beyond = less([[h0, h1]], [spec.owned]);
1275
+ if (beyond.length === 0) return [t];
1276
+ if (c1 - c0 <= EPS) return [{ ...t, order: data.overreach }];
1277
+ const core = { ...t, hit: part(c0, c1), keep: t.keep && keepAt(v ? t.keep.y : t.keep.x, part(c0, c1)) };
1278
+ return [core, ...beyond.map(([a, b]) => ({ ...t, kind: "reach", of: core, order: data.overreach, hit: part(a, b), keep: null }))];
1279
+ };
1280
+ const landing = (s) => s.result.layout.items[c.dragged]?.rect;
1281
+ const sameMove = (a, b) => {
1282
+ if (a.zone !== b.zone) return false;
1283
+ if (a.home && b.home) return true;
1284
+ const [p, q] = [landing(a), landing(b)];
1285
+ return Math.abs(a.line - b.line) < EPS && (!p || !q ? p === q : Math.abs(p.x - q.x) + Math.abs(p.y - q.y) + Math.abs(p.width - q.width) + Math.abs(p.height - q.height) < 0.01);
1286
+ };
1287
+ const slots = spec.slots.filter((s, i) => !spec.slots.slice(0, i).some((o) => sameMove(o, s)));
1288
+ const out = [];
1289
+ for (const z of spec.zones) {
1290
+ const room = less([[z.a, z.b]], [...cuts, ...reserved]).filter(([a, b]) => b - a > EPS);
1291
+ if (room.length === 0) continue;
1292
+ const pieces = own ? less(room, [own]) : room;
1293
+ const mine = slots.filter((s) => s.zone === z.zone && !(spec.holds && s.home) && pieces.some(([a, b]) => b > s.line - data.reach + EPS && a < s.line + data.reach - EPS));
1294
+ const reason = spec.refuse(z.zone);
1295
+ for (const [pa, pb] of pieces) {
1296
+ const lines = mine.map((s) => s.line);
1297
+ const lo = Math.min(z.a, ...lines);
1298
+ const hi = Math.max(z.b, ...lines);
1299
+ const free = less([[z.a, z.b]], reserved);
1300
+ const freeLen = free.reduce((n, [a, b]) => n + (b - a), 0);
1301
+ const spread = (x) => {
1302
+ if (hi - lo <= EPS || freeLen <= EPS) return x;
1303
+ let d = (x - lo) / (hi - lo) * freeLen;
1304
+ for (const [a, b] of free) {
1305
+ if (d <= b - a + EPS) return a + Math.min(d, b - a);
1306
+ d -= b - a;
1307
+ }
1308
+ return free[free.length - 1][1];
1309
+ };
1310
+ const past = lines.some((x) => x < z.a - EPS || x > z.b + EPS);
1311
+ const nearer = (p, [a, b]) => p > a && p < b ? p - a < b - p ? a : b : p;
1312
+ const proj = (x) => {
1313
+ const q = reserved.reduce((r, s) => nearer(r, s), past ? spread(x) : x);
1314
+ const p = own ? nearer(q, own) : q;
1315
+ if (pieces.some(([a, b]) => p >= a - EPS && p <= b + EPS)) return p;
1316
+ return pieces.map(([a, b]) => Math.min(Math.max(p, a), b)).reduce((m, y) => Math.abs(y - p) < Math.abs(m - p) ? y : m);
1317
+ };
1318
+ const inPiece = mine.filter((s) => proj(s.line) >= pa - EPS && proj(s.line) <= pb + EPS).sort((a, b) => proj(a.line) - proj(b.line) || a.line - b.line);
1319
+ const projected = inPiece.map((s) => proj(s.line));
1320
+ const at = projected.some((p, i) => i > 0 && p - projected[i - 1] <= EPS) ? inPiece.map((_, i) => pa + (i + 0.5) * (pb - pa) / inPiece.length) : projected;
1321
+ if (inPiece.length === 0) {
1322
+ const lone = reason === "no-room" && mine.length === 0 && slots.some((s) => s.zone === z.zone && s.home);
1323
+ const near = mine.length === 0 || mine.some((s) => pb > s.line - data.reach + EPS && pa < s.line + data.reach - EPS);
1324
+ if (reason && near) out.push(...split(refused(z.zone, band(pa, pb), lone ? "own-place" : reason)));
1325
+ continue;
1326
+ }
1327
+ inPiece.forEach((s, i) => {
1328
+ const p = at[i];
1329
+ const a = Math.max(i === 0 ? pa : (at[i - 1] + p) / 2, p - data.reach);
1330
+ const b = Math.min(i === inPiece.length - 1 ? pb : (p + at[i + 1]) / 2, p + data.reach);
1331
+ if (b - a <= EPS) {
1332
+ if (s.ok) lost.push(s.id);
1333
+ return;
1334
+ }
1335
+ const line = Math.min(Math.max(p, a), b);
1336
+ const hit = band(a, b, s.result.layout);
1337
+ if (!s.ok) out.push(...split(refused(z.zone, hit, "no-room")));
1338
+ else if (s.home) out.push(...split(tile(z.zone, hit, { kind: "home", id: s.id, order: data.layer, priority: data.priority, keep: keepAt(line, hit), place: { kind: "home" } })));
1339
+ else out.push(...split(tile(z.zone, hit, { kind: "slot", id: s.id, order: data.layer, priority: data.priority, keep: keepAt(line, hit), place: s.place, result: s.result, marker: spec.marker(s), slot: s.result.layout.items[c.dragged].rect })));
1340
+ });
1155
1341
  }
1156
- if (after && a1 > h1 + EPS2) {
1157
- const stop = Math.max(a0, h1, ...others(after).filter(([, e]) => e <= span(after)[0] + EPS2).map(([, e]) => e));
1158
- if (stop < span(after)[0]) out[out.indexOf(after)] = make(after, stop, span(after)[1]);
1342
+ }
1343
+ if (own && spec.own) {
1344
+ const home = spec.slots.find((s) => s.home);
1345
+ const [a, b] = own;
1346
+ const h = band(a, b);
1347
+ const o = spec.own;
1348
+ const hit = v ? rect(Math.min(h.x, o.x), a, Math.max(h.x + h.width, o.x + o.width) - Math.min(h.x, o.x), b - a) : rect(a, Math.min(h.y, o.y), b - a, Math.max(h.y + h.height, o.y + o.height) - Math.min(h.y, o.y));
1349
+ out.push(tile(home?.zone ?? zoneOf(c.scene.items[c.dragged]), hit, { kind: "home", id: home ? home.id : `home:${c.dragged}`, order: 0, priority: home ? data.priority : 1, keep: null, place: { kind: "home" } }));
1350
+ }
1351
+ return out;
1352
+ }
1353
+ function columnLines(c, side) {
1354
+ const { rest, base, dragged } = c;
1355
+ const cols = rest.edges.filter((e) => e.side === side && e.column === true).map((edge) => ({ edge, l: base.edges[edge.id] })).filter((x) => x.l && !x.l.folded && !x.l.rides && x.l.cross > 0).sort((a, b) => a.edge.ring - b.edge.ring).map((x) => ({ edge: x.edge, r: x.l.rect }));
1356
+ const own = Object.values(base.edges).filter((e) => e.side === side && e.rect.height > 0);
1357
+ const span = own[0]?.rect ?? base.content;
1358
+ const rings = sideRings(rest.edges, side);
1359
+ const ringOf = (e) => rings.get(e.id) ?? e.ring;
1360
+ const lines = [];
1361
+ const outerX = (r) => side === "left" ? r.x : r.x + r.width;
1362
+ const innerX = (r) => side === "left" ? r.x + r.width : r.x;
1363
+ const atOf = (e) => c.lay.get(e.id)?.at ?? 0;
1364
+ cols.forEach((x) => lines.push({ x: outerX(x.r), ring: ringOf(x.edge), at: atOf(x.edge) - 0.5 }));
1365
+ if (cols.length > 0) lines.push({ x: innerX(cols[cols.length - 1].r), ring: ringOf(cols[cols.length - 1].edge) + 1, at: atOf(cols[cols.length - 1].edge) + 0.5 });
1366
+ else lines.push({ x: side === "left" ? base.content.x : base.content.x + base.content.width, ring: rings.size, at: Math.max(-1, ...rest.edges.filter((e) => e.side === side).map(atOf)) + 0.5 });
1367
+ const lands = (r) => {
1368
+ const me = r.layout.items[dragged];
1369
+ const el = me.edge ? r.layout.edges[me.edge] : void 0;
1370
+ return !!el && !me.inMenu && !el.folded && !el.rides && r.scene !== c.scene;
1371
+ };
1372
+ const { reach, place } = EDGE_KINDS.columnLine;
1373
+ return lines.map((l) => ({
1374
+ id: `column:${side}`,
1375
+ key: `column:${side}:${l.ring}`,
1376
+ kind: "columnLine",
1377
+ lay: { phase: EDGE_KINDS.columnLine.phase, side, at: l.at },
1378
+ vertical: false,
1379
+ zones: [{ zone: "start", a: l.x - reach, b: l.x + reach }],
1380
+ slots: [slotAt(c, "start", l.x, `column:${side}:${l.ring}`, { kind: "column", side, ring: l.ring }, lands)],
1381
+ refuse: () => "no-room",
1382
+ own: null,
1383
+ holds: false,
1384
+ reserved: [],
1385
+ across: () => [span.y, span.y + span.height],
1386
+ owned: [span.y, span.y + span.height],
1387
+ ownedAlong: [l.x - place / 2, l.x + place / 2],
1388
+ drawn: false,
1389
+ marker: (s) => rect(s.line, span.y, 0, span.height)
1390
+ }));
1391
+ }
1392
+ function railStops(c, side) {
1393
+ const { rest, base, now, dragged } = c;
1394
+ const rail = base.rails[side];
1395
+ const placeRect = base.railPlaces[side];
1396
+ if (!placeRect) return null;
1397
+ const rings = sideRings(rest.edges, side);
1398
+ const mine = rest.edges.filter((e) => e.side === side).sort((a, b) => (rings.get(a.id) ?? 0) - (rings.get(b.id) ?? 0));
1399
+ const ringOf = (id) => rings.get(id) ?? 0;
1400
+ const stops2 = [];
1401
+ let zone;
1402
+ if (rail && rail.segments.length > 0) {
1403
+ zone = rail.rect;
1404
+ const groups = [];
1405
+ for (const s of rail.segments) {
1406
+ const g = groups[groups.length - 1];
1407
+ if (g && g.edge === s.edge) g.y1 = s.rect.y + s.rect.height;
1408
+ else groups.push({ edge: s.edge, y0: s.rect.y, y1: s.rect.y + s.rect.height });
1159
1409
  }
1410
+ for (const g of groups) stops2.push({ y: g.y0, ring: ringOf(g.edge) });
1411
+ const last = groups[groups.length - 1];
1412
+ stops2.push({ y: last.y1, ring: ringOf(last.edge) + 1 });
1413
+ } else {
1414
+ const firstCol = mine.find((e) => e.column === true);
1415
+ zone = rect(side === "left" ? placeRect.x : placeRect.x + placeRect.width - RAIL_EDGE, placeRect.y, RAIL_EDGE, placeRect.height);
1416
+ stops2.push({ y: placeRect.y, ring: firstCol ? ringOf(firstCol.id) : mine.length });
1160
1417
  }
1161
- if (first && h1 - h0 > EPS2) out.splice(Math.min(at, out.length), 0, make(first, h0, h1, [c0, c1]));
1418
+ const ownRail = now.items[dragged]?.rail === side;
1419
+ const lands = (r) => r.layout.items[dragged].rail === side && r.scene !== c.scene;
1420
+ const at = zone;
1421
+ return {
1422
+ id: `rail:${side}`,
1423
+ key: `rail:${side}`,
1424
+ kind: "railStop",
1425
+ // The rail lies outside every edge on its side: laid first among them.
1426
+ lay: { phase: EDGE_KINDS.railStop.phase, side, at: -1 },
1427
+ vertical: true,
1428
+ zones: [{ zone: "start", a: at.y, b: at.y + at.height }],
1429
+ slots: stops2.map((s) => slotAt(c, "start", s.y, `rail:${side}:${s.ring}`, { kind: "rail", side, ring: s.ring }, lands, ownRail)),
1430
+ refuse: () => "no-room",
1431
+ own: null,
1432
+ holds: false,
1433
+ reserved: [],
1434
+ across: () => [at.x, at.x + at.width],
1435
+ // A drawn rail owns its rect; where none shows, a thin band of its place at the side's outer boundary.
1436
+ owned: rail && rail.segments.length > 0 ? [at.x, at.x + at.width] : side === "left" ? [at.x, at.x + Math.min(at.width, EDGE_KINDS.railStop.place)] : [at.x + at.width - Math.min(at.width, EDGE_KINDS.railStop.place), at.x + at.width],
1437
+ ownedAlong: null,
1438
+ drawn: !!rail && rail.segments.length > 0,
1439
+ marker: (s) => rect(at.x, s.result.layout.items[dragged].rect.y, at.width, 0)
1440
+ };
1162
1441
  }
1163
- function targets(scene, dragged, options = {}) {
1442
+ function generate(scene, dragged, options) {
1164
1443
  const item = Object.hasOwn(scene.items, dragged) ? scene.items[dragged] : void 0;
1165
- if (!item || item.fixed || item.closed) return [];
1444
+ if (!item || item.fixed || item.closed) return { targets: [], refusals: [], lost: [] };
1166
1445
  const prev = { ...options.previous ? { previous: options.previous } : {}, ...options.fresh ? { fresh: options.fresh } : {} };
1167
1446
  const settle = (s) => ({ scene: s, layout: solve(s, prev) });
1168
1447
  const now = solve(scene, prev);
1169
1448
  const rest = lift(scene, dragged);
1170
1449
  const base = solve(rest, prev);
1171
- const reach = Math.max(0, Number.isFinite(options.endReach) ? options.endReach : 0);
1172
1450
  const chrome = scene.chrome ?? [];
1173
- const grows = (l, id) => {
1174
- const it = l.items[id];
1175
- const e = it.edge ? l.edges[it.edge] : void 0;
1176
- return !!e && !!formsOf(scene.items[id], axisOf(e.side))[it.form]?.main.grows;
1177
- };
1178
- const fixedRects = (l) => Object.entries(l.items).filter(([id, it]) => it.fixed && !grows(l, id)).map(([, it]) => it.rect);
1179
- const blockers = [...fixedRects(now), ...fixedRects(base), ...chrome];
1180
- const out = [];
1181
- const docked = [];
1182
- for (const edge of rest.edges) {
1183
- if (!edge.accepts.includes(item.kind)) continue;
1184
- docked.push(...edgeTargets(scene, rest, now, base, edge, dragged, reach, blockers, chrome, settle));
1185
- }
1186
- for (const side of ["left", "right"]) {
1187
- const template = scene.columns?.[side];
1188
- if (!template || !zoneTakes(template, "start", item.kind)) continue;
1189
- docked.push(...columnTargets(scene, rest, base, side, dragged, blockers, settle));
1190
- if (scene.rails?.[side]?.accepts.includes(item.kind)) docked.push(...railTargets(scene, rest, base, side, dragged, blockers, settle));
1191
- }
1192
1451
  const here = placeKey(scene, dragged);
1193
- const ownRail = now.items[dragged]?.rail;
1194
1452
  const noteKey = (n) => "edge" in n && (n.code === "overflow" || n.code === "too-wide") ? `${n.edge}:${n.code}:${"id" in n ? n.id : ""}` : null;
1195
1453
  const hadNotes = new Set(base.notes.map(noteKey).filter((k) => k !== null));
1196
1454
  const sound = (after) => {
@@ -1212,72 +1470,195 @@ function targets(scene, dragged, options = {}) {
1212
1470
  const fixedAfter = Object.entries(after.items).filter(([id, it]) => it.fixed && id !== dragged).map(([id]) => reservedRect(scene, after, id));
1213
1471
  return !chrome.some((c2) => overlaps(me.rect, c2)) && !fixedAfter.some((f) => overlaps(me.rect, f));
1214
1472
  };
1215
- for (const t of docked) {
1216
- const p = t.place;
1217
- const own2 = p.kind === "rail" && p.side === ownRail || placeKey(t.after({ x: 0, y: 0 }).scene, dragged) === here;
1218
- if (own2) out.push({ id: t.id, priority: t.priority, hit: t.hit, item: dragged, place: { kind: "home" }, after: () => ({ scene, layout: now }) });
1219
- else if (sound(t.after({ x: 0, y: 0 }).layout)) out.push(t);
1220
- }
1221
- homeOnlyOverItsPlace(out, now);
1222
- const edgeOfGap = (t) => t.place.kind === "gap" ? rest.edges.find((e) => e.id === t.place.edge) : void 0;
1223
- const plainGapHits = out.filter((t) => t.id.startsWith("gap:") && !edgeOfGap(t)?.compact).map((t) => t.hit);
1224
- for (let i = 0; i < out.length; i++) {
1225
- const t = out[i];
1226
- const e = edgeOfGap(t);
1227
- if (!t.id.startsWith("gap:") || !e?.compact || !plainGapHits.some((h2) => overlaps(h2, t.hit))) continue;
1228
- const h = t.hit;
1229
- const core = e.side === "left" ? rect(h.x, h.y, h.width - RAIL_REACH, h.height) : e.side === "right" ? rect(h.x + RAIL_REACH, h.y, h.width - RAIL_REACH, h.height) : e.side === "top" ? rect(h.x, h.y, h.width, h.height - RAIL_REACH) : rect(h.x, h.y + RAIL_REACH, h.width, h.height - RAIL_REACH);
1230
- out[i] = { ...t, hit: core };
1473
+ const c = { scene, rest, now, base, dragged, kind: item.kind, reach: Math.max(0, Number.isFinite(options.endReach) ? options.endReach : 0), settle, isHome: (r) => placeKey(r.scene, dragged) === here, sound, lay: layKeys(scene) };
1474
+ const specs = rest.edges.map((edge) => edgeSpec(c, edge));
1475
+ for (const side of ["left", "right"]) {
1476
+ const template = scene.columns?.[side];
1477
+ if (!template || !zoneTakes(template, "start", item.kind)) continue;
1478
+ specs.push(...columnLines(c, side));
1479
+ if (scene.rails?.[side]?.accepts.includes(item.kind)) specs.push(railStops(c, side));
1231
1480
  }
1232
- const gapHits = out.filter((t) => t.id.startsWith("gap:")).map((t) => t.hit);
1233
- const c = base.content;
1234
- for (let i = out.length - 1; i >= 0; i--) {
1235
- const t = out[i];
1236
- const e = edgeOfGap(t);
1237
- if (!t.id.startsWith("strip:") || !e) continue;
1238
- const vertical = e.side === "left" || e.side === "right";
1239
- const x0 = Math.max(t.hit.x, c.x);
1240
- const y0 = Math.max(t.hit.y, c.y);
1241
- const x1 = Math.min(t.hit.x + t.hit.width, c.x + c.width);
1242
- const y1 = Math.min(t.hit.y + t.hit.height, c.y + c.height);
1243
- const docks = [...Object.entries(base.edges), ...Object.entries(now.edges).filter(([id]) => id !== now.items[dragged]?.edge)].filter(([id, d]) => id !== e.id && area(d.rect) > EPS2).map(([, d]) => d.rect);
1244
- const mineNow = now.items[dragged];
1245
- if (mineNow?.edge && !mineNow.floating) docks.push(mineNow.rect);
1246
- const h = e.compact ? t.hit : x1 - x0 > EPS2 && y1 - y0 > EPS2 ? cutAway(rect(x0, y0, x1 - x0, y1 - y0), docks) : null;
1247
- if (!h) {
1248
- out.splice(i, 1);
1481
+ const ownEdge = now.items[dragged]?.edge;
1482
+ const live = specs.filter((s) => s !== null);
1483
+ const bandOf = (s) => {
1484
+ const a = Math.min(...s.zones.map((z) => z.a));
1485
+ const b = Math.max(...s.zones.map((z) => z.b));
1486
+ return s.vertical ? rect(s.owned[0], a, s.owned[1] - s.owned[0], b - a) : rect(a, s.owned[0], b - a, s.owned[1] - s.owned[0]);
1487
+ };
1488
+ const ownedRectOf = (s) => {
1489
+ const b = bandOf(s);
1490
+ if (!s.ownedAlong) return b;
1491
+ const [a0, a1] = s.ownedAlong;
1492
+ return s.vertical ? rect(b.x, a0, b.width, a1 - a0) : rect(a0, b.y, a1 - a0, b.height);
1493
+ };
1494
+ const ownRect = now.items[dragged];
1495
+ const homeRect = (() => {
1496
+ if (!ownRect || ownRect.floating || !ownEdge) return null;
1497
+ const o = ownRect.rect;
1498
+ const s = live.find((x) => x.id === ownEdge);
1499
+ if (!s || ownRect.inMenu) return o;
1500
+ const [c0, c1] = s.across();
1501
+ return s.vertical ? rect(Math.min(c0, o.x), o.y, Math.max(c1, o.x + o.width) - Math.min(c0, o.x), o.height) : rect(o.x, Math.min(c0, o.y), o.width, Math.max(c1, o.y + o.height) - Math.min(c0, o.y));
1502
+ })();
1503
+ const owners = [
1504
+ // Home first: its tile is its own, for every edge but its own (which tiles it as its own interval).
1505
+ ...homeRect && ownEdge ? [{ id: ownEdge, key: ownEdge, rect: homeRect, lay: { phase: -1, side: "", at: -Infinity }, place: false }] : [],
1506
+ // Every edge the tiler reads: what it owns, drawn or a band of its place (a column line's band about its line).
1507
+ ...live.map((s) => ({ id: s.id, key: s.key, rect: ownedRectOf(s), lay: s.lay, place: !s.drawn })).filter((o) => area(o.rect) > EPS),
1508
+ // Every drawn rect, with the item lifted and as it is now (the item's own edge only lifted): the edges the tiler
1509
+ // doesn't read (folded, riding, taking nothing) and the ones that move when the item lifts.
1510
+ ...[...Object.entries(base.edges), ...Object.entries(now.edges).filter(([id]) => id !== ownEdge)].filter(([id, e]) => area(e.rect) > EPS && c.lay.has(id)).map(([id, e]) => ({ id, key: id, rect: e.rect, lay: c.lay.get(id), place: false }))
1511
+ ];
1512
+ const wins = (o, s) => {
1513
+ if (o.key === s.key) return false;
1514
+ if (pushes(o.lay, s.lay)) return true;
1515
+ if (pushes(s.lay, o.lay)) return false;
1516
+ const place = !s.drawn;
1517
+ return o.place === place ? !place || o.lay.at < s.lay.at : !o.place;
1518
+ };
1519
+ const taken = new Map(
1520
+ live.map((s) => [
1521
+ s.key,
1522
+ [
1523
+ ...owners.filter((o) => wins(o, s)).map((o) => o.rect),
1524
+ ...EDGE_KINDS[s.kind].virtual ? owners.filter((o) => o.key !== s.key && !wins(o, s)).flatMap((o) => subtract(o.rect, ownedRectOf(s))) : []
1525
+ ]
1526
+ ])
1527
+ );
1528
+ const bodySpans = (id) => {
1529
+ const e = rest.edges.find((x) => x.id === id);
1530
+ const bandOpt = e?.hitBand ?? DEFAULT_HIT_BAND;
1531
+ if (!e || bandOpt === "middle") return [];
1532
+ const column = axisOf(e.side) === "column";
1533
+ const half = Math.max(0, Number.isFinite(bandOpt) ? bandOpt : DEFAULT_HIT_BAND) / 2;
1534
+ return Object.entries(base.items).filter(([iid, p]) => p.edge === id && p.formKind === "open" && iid !== dragged).map(([, p]) => column ? [p.rect.y + half, p.rect.y + p.rect.height - half] : [p.rect.x + half, p.rect.x + p.rect.width - half]).filter(([a, b]) => b - a > EPS);
1535
+ };
1536
+ const cutsOf = (s) => {
1537
+ const b = bandOf(s);
1538
+ const along = (r) => s.vertical ? [r.y, r.y + r.height] : [r.x, r.x + r.width];
1539
+ const across = (r) => s.vertical ? [r.x, r.x + r.width] : [r.y, r.y + r.height];
1540
+ const rs = taken.get(s.key).filter((r) => overlaps(r, b));
1541
+ const ends = [.../* @__PURE__ */ new Set([...along(b), ...rs.flatMap(along)])].filter((x) => x >= along(b)[0] && x <= along(b)[1]).sort((p, q) => p - q);
1542
+ const owned = [];
1543
+ for (let k = 1; k < ends.length; k++) {
1544
+ const [u, w] = [ends[k - 1], ends[k]];
1545
+ const over = rs.filter((r) => along(r)[0] <= u + EPS && along(r)[1] >= w - EPS).map(across);
1546
+ if (w - u > EPS && less([across(b)], over).length === 0) owned.push([u, w]);
1547
+ }
1548
+ return [...owned, ...bodySpans(s.id)];
1549
+ };
1550
+ const lost = [];
1551
+ const tiles = live.flatMap((spec) => tileEdge(c, spec, cutsOf(spec), lost));
1552
+ const homeEdge = scene.edges.find((e) => e.id === ownRect?.edge);
1553
+ if (ownRect && homeEdge && !ownRect.floating && !tiles.some((t) => t.order === 0))
1554
+ tiles.push({ edge: homeEdge.id, key: homeEdge.id, edgeKind: edgeKindOf(scene, homeEdge), vertical: axisOf(homeEdge.side) === "column", zone: zoneOf(item), order: 0, kind: "home", id: `home:${dragged}`, priority: 1, place: { kind: "home" }, hit: ownRect.rect, keep: null });
1555
+ const grows = (l, id) => {
1556
+ const it = l.items[id];
1557
+ const e = it.edge ? l.edges[it.edge] : void 0;
1558
+ return !!e && !!formsOf(scene.items[id], axisOf(e.side))[it.form]?.main.grows;
1559
+ };
1560
+ const solid = [.../* @__PURE__ */ new Set([now, base])].flatMap((l) => Object.entries(l.items).filter(([id, it]) => it.fixed && !grows(l, id)).map(([, it]) => it.rect));
1561
+ const bodies = (id) => {
1562
+ const e = rest.edges.find((x) => x.id === id);
1563
+ if (!e) return [];
1564
+ const column = axisOf(e.side) === "column";
1565
+ const across = (a, b) => column ? rect(-1e9, a, 2e9, b - a) : rect(a, -1e9, b - a, 2e9);
1566
+ const reserved = Object.entries(base.items).filter(([iid, p]) => p.edge === id && p.fixed && !p.inMenu && !grows(base, iid)).map(([, p]) => column ? across(p.rect.y, p.rect.y + p.rect.height) : across(p.rect.x, p.rect.x + p.rect.width));
1567
+ return [...reserved, ...bodySpans(id).map(([a, b]) => across(a, b))].filter((b) => area(b) > EPS);
1568
+ };
1569
+ const placed = [];
1570
+ const later = [];
1571
+ const clip = (r, cuts) => cuts.reduce((ps, cut) => ps.flatMap((p) => subtract(p, cut)), [r]);
1572
+ const landed = /* @__PURE__ */ new Map();
1573
+ const gone = /* @__PURE__ */ new Set();
1574
+ for (const t of [...tiles].sort((a, b) => a.order - b.order)) {
1575
+ if (t.order === 0) {
1576
+ placed.push(t);
1249
1577
  continue;
1250
1578
  }
1251
- let [a0, a1] = vertical ? [h.y, h.y + h.height] : [h.x, h.x + h.width];
1252
- for (const g of gapHits) {
1253
- if (!overlaps(g, h)) continue;
1254
- const spans = vertical ? g.x <= h.x + EPS2 && g.x + g.width >= h.x + h.width - EPS2 : g.y <= h.y + EPS2 && g.y + g.height >= h.y + h.height - EPS2;
1255
- if (!spans) continue;
1256
- const [g0, g1] = vertical ? [g.y, g.y + g.height] : [g.x, g.x + g.width];
1257
- if (g0 <= a0 + EPS2 && g1 > a0) a0 = Math.min(a1, g1);
1258
- else if (g1 >= a1 - EPS2 && g0 < a1) a1 = Math.max(a0, g0);
1579
+ const data = EDGE_KINDS[t.edgeKind];
1580
+ if (data.virtual && t.kind === "refuse") {
1581
+ later.push(t);
1582
+ continue;
1259
1583
  }
1260
- if (a1 - a0 < 1) out.splice(i, 1);
1261
- else {
1262
- const hit = vertical ? rect(h.x, a0, h.width, a1 - a0) : rect(a0, h.y, a1 - a0, h.height);
1263
- if (hit.x !== t.hit.x || hit.y !== t.hit.y || hit.width !== t.hit.width || hit.height !== t.hit.height) out[i] = { ...t, hit };
1584
+ if (t.kind === "reach") {
1585
+ const of = t.of;
1586
+ const pieces2 = clip(t.hit, [...placed.map((p) => p.hit), ...owners.filter((o) => o.key !== t.key).map((o) => o.rect), ...bodies(t.edge), ...solid, ...chrome]);
1587
+ const at = landed.get(of) ?? [];
1588
+ if (at.length === 0 && gone.has(of)) {
1589
+ const line = t.vertical ? of.keep.y : of.keep.x;
1590
+ const on = pieces2.filter((p) => t.vertical ? p.y <= line + EPS && line <= p.y + p.height + EPS : p.x <= line + EPS && line <= p.x + p.width + EPS);
1591
+ const kept2 = (on.length > 0 ? on : pieces2).reduce((a, p) => !a || area(p) > area(a) ? p : a, null);
1592
+ if (kept2) {
1593
+ placed.push({ ...of, hit: kept2 });
1594
+ gone.delete(of);
1595
+ }
1596
+ continue;
1597
+ }
1598
+ for (const i of at) {
1599
+ const h = placed[i].hit;
1600
+ const joint = pieces2.find(
1601
+ (p) => t.vertical ? p.y <= h.y + EPS && p.y + p.height >= h.y + h.height - EPS && (Math.abs(p.x - h.x - h.width) < EPS || Math.abs(p.x + p.width - h.x) < EPS) : p.x <= h.x + EPS && p.x + p.width >= h.x + h.width - EPS && (Math.abs(p.y - h.y - h.height) < EPS || Math.abs(p.y + p.height - h.y) < EPS)
1602
+ );
1603
+ if (!joint) continue;
1604
+ const merged = t.vertical ? rect(Math.min(h.x, joint.x), h.y, Math.max(h.x + h.width, joint.x + joint.width) - Math.min(h.x, joint.x), h.height) : rect(h.x, Math.min(h.y, joint.y), h.width, Math.max(h.y + h.height, joint.y + joint.height) - Math.min(h.y, joint.y));
1605
+ placed[i] = { ...placed[i], hit: merged };
1606
+ break;
1607
+ }
1608
+ continue;
1609
+ }
1610
+ const cuts = [...placed.map((p) => p.hit), ...taken.get(t.key) ?? [], ...data.virtual ? [] : bodies(t.edge), ...solid, ...chrome];
1611
+ const pieces = clip(t.hit, cuts);
1612
+ if (pieces.length === 0) {
1613
+ if (t.kind !== "refuse") gone.add(t);
1614
+ continue;
1264
1615
  }
1616
+ if (t.kind === "refuse") {
1617
+ landed.set(t, pieces.map((_, k) => placed.length + k));
1618
+ for (const p of pieces) placed.push({ ...t, hit: p });
1619
+ continue;
1620
+ }
1621
+ const held = t.keep ? pieces.filter((p) => contains(p, t.keep)) : [];
1622
+ const kept = (held.length > 0 ? held : pieces).reduce((a, p) => area(p) > area(a) ? p : a);
1623
+ const sliver = (t.vertical ? kept.height : kept.width) < 1;
1624
+ if (!sliver) {
1625
+ landed.set(t, [placed.length]);
1626
+ placed.push({ ...t, hit: kept });
1627
+ }
1628
+ for (const p of pieces) if (sliver || p !== kept) (data.virtual ? later : placed).push({ ...t, kind: "refuse", reason: "no-room", hit: p, keep: null });
1265
1629
  }
1266
- const mineHere = now.items[dragged];
1267
- if (mineHere?.edge && !mineHere.floating) {
1268
- for (let i = out.length - 1; i >= 0; i--) {
1269
- const t = out[i];
1270
- if (t.place.kind !== "rail" || !overlaps(t.hit, mineHere.rect)) continue;
1271
- const h = cutAway(t.hit, [mineHere.rect]);
1272
- if (!h || h.width < 1 || h.height < 1) out.splice(i, 1);
1273
- else out[i] = { ...t, hit: h };
1630
+ for (const t of gone) lost.push(t.id);
1631
+ for (const t of later) for (const p of clip(t.hit, [...placed.map((q) => q.hit), ...solid, ...chrome])) placed.push({ ...t, hit: p });
1632
+ const targets2 = [];
1633
+ const refused = [];
1634
+ const homeAfter = () => ({ scene, layout: now });
1635
+ const keyOf = (t) => `refuse:${t.edge}:${t.zone}`;
1636
+ const pos = (t) => t.vertical ? [t.hit.y, t.hit.x] : [t.hit.x, t.hit.y];
1637
+ const rank = /* @__PURE__ */ new Map();
1638
+ const byKey = /* @__PURE__ */ new Map();
1639
+ for (const t of placed) if (t.kind === "refuse") byKey.set(keyOf(t), [...byKey.get(keyOf(t)) ?? [], t]);
1640
+ for (const group of byKey.values()) group.sort((a, b) => pos(a)[0] - pos(b)[0] || pos(a)[1] - pos(b)[1]).forEach((t, n) => rank.set(t, n));
1641
+ let ownHome = null;
1642
+ for (const t of placed) {
1643
+ if (t.kind === "refuse") {
1644
+ const n = rank.get(t);
1645
+ refused.push({ id: n === 0 ? keyOf(t) : `${keyOf(t)}:${n}`, edge: t.edge, zone: t.zone, hit: t.hit, reason: t.reason });
1646
+ continue;
1274
1647
  }
1648
+ if (t.kind === "home") {
1649
+ if (t.order === 0) ownHome = t.hit;
1650
+ if (t.id === `home:${dragged}`) continue;
1651
+ targets2.push({ id: t.id, priority: t.priority, hit: t.hit, item: dragged, place: { kind: "home" }, after: homeAfter });
1652
+ continue;
1653
+ }
1654
+ const result = t.result;
1655
+ targets2.push({ id: t.id, priority: t.priority, hit: t.hit, ...t.slot ? { slot: t.slot } : {}, ...t.marker ? { marker: t.marker } : {}, item: dragged, place: t.place, after: () => result });
1275
1656
  }
1276
1657
  if (scene.floatKinds.includes(item.kind)) {
1277
1658
  const was = scene.floats.find((f) => f.id === dragged);
1278
1659
  const fo = options.floatOrient ?? was?.orient ?? floatOrientOf(item);
1279
1660
  const size = options.floatSize ?? (was ? { width: was.rect.width, height: was.rect.height } : floatSizeOf(item, fo));
1280
- out.push({
1661
+ targets2.push({
1281
1662
  id: `float:${dragged}`,
1282
1663
  priority: 3,
1283
1664
  free: true,
@@ -1294,139 +1675,17 @@ function targets(scene, dragged, options = {}) {
1294
1675
  });
1295
1676
  }
1296
1677
  (scene.blocked ?? []).forEach((b, i) => {
1297
- out.push({ id: `none:${i}`, priority: 1, hit: rect(b.x, b.y, b.width, b.height), item: dragged, place: { kind: "none" }, after: () => ({ scene, layout: now }) });
1678
+ targets2.push({ id: `none:${i}`, priority: 1, hit: rect(b.x, b.y, b.width, b.height), item: dragged, place: { kind: "none" }, after: homeAfter });
1298
1679
  });
1299
1680
  const own = now.items[dragged];
1300
- if (own && !own.floating) out.push({ id: `home:${dragged}`, priority: 1, hit: own.rect, item: dragged, place: { kind: "home" }, after: () => ({ scene, layout: now }) });
1301
- return out;
1302
- }
1303
- function reservedRect(scene, l, id) {
1304
- const it = l.items[id];
1305
- const e = it.edge ? l.edges[it.edge] : void 0;
1306
- const f = e ? formsOf(scene.items[id], axisOf(e.side))[it.form] : void 0;
1307
- if (!e || !f?.main.grows) return it.rect;
1308
- const column = axisOf(e.side) === "column";
1309
- const len = column ? it.rect.height : it.rect.width;
1310
- const keep2 = Math.min(len, Math.max(0, Number.isFinite(f.main.natural) ? f.main.natural : len));
1311
- const shift = it.zone === "end" ? len - keep2 : it.zone === "center" ? (len - keep2) / 2 : 0;
1312
- 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);
1313
- }
1314
- function placeKey(s, id) {
1315
- const it = Object.hasOwn(s.items, id) ? s.items[id] : void 0;
1316
- return JSON.stringify({
1317
- edges: s.edges.map((e) => JSON.stringify([e.side, e.ring, e.column === true ? "" : e.id, e.items, e.size ?? null])).sort(),
1318
- floats: s.floats.map((f) => [f.id, f.rect, f.anchor ?? null, f.orient ?? null]),
1319
- from: it?.from ?? 0,
1320
- align: it?.align ?? "start"
1321
- });
1322
- }
1323
- var COLUMN_REACH = 22;
1324
- var RAIL_EDGE = 10;
1325
- function openColumns(rest, base, side) {
1326
- return rest.edges.filter((e) => e.side === side && e.column === true).map((edge) => ({ edge, l: base.edges[edge.id] })).filter((x) => x.l && !x.l.folded && !x.l.rides && x.l.cross > 0).sort((a, b) => a.edge.ring - b.edge.ring).map((x) => ({ edge: x.edge, r: x.l.rect }));
1327
- }
1328
- function sideSpan(base, side) {
1329
- const own = Object.values(base.edges).filter((e) => e.side === side && e.rect.height > 0);
1330
- const r = own[0]?.rect ?? base.content;
1331
- return { y: r.y, height: r.height };
1681
+ if (own && !own.floating) targets2.push({ id: `home:${dragged}`, priority: 1, hit: ownHome && !targets2.some((t) => t.place.kind === "home") ? ownHome : own.rect, item: dragged, place: { kind: "home" }, after: homeAfter });
1682
+ return { targets: targets2, refusals: refused, lost };
1332
1683
  }
1333
- function columnTargets(scene, rest, base, side, dragged, blockers, settle) {
1334
- const cols = openColumns(rest, base, side);
1335
- const span = sideSpan(base, side);
1336
- const rings = sideRings(rest.edges, side);
1337
- const ringOf = (e) => rings.get(e.id) ?? e.ring;
1338
- const inner = rings.size;
1339
- const lines = [];
1340
- const outerX = (r) => side === "left" ? r.x : r.x + r.width;
1341
- const innerX = (r) => side === "left" ? r.x + r.width : r.x;
1342
- cols.forEach((c) => lines.push({ x: outerX(c.r), ring: ringOf(c.edge) }));
1343
- if (cols.length > 0) lines.push({ x: innerX(cols[cols.length - 1].r), ring: ringOf(cols[cols.length - 1].edge) + 1 });
1344
- else lines.push({ x: side === "left" ? base.content.x : base.content.x + base.content.width, ring: inner });
1345
- const out = [];
1346
- for (const line of lines) {
1347
- const place = { kind: "column", side, ring: line.ring };
1348
- const result = settle(use(moveTo(scene, dragged, place), dragged));
1349
- const me = result.layout.items[dragged];
1350
- const el = me?.edge ? result.layout.edges[me.edge] : void 0;
1351
- if (!me || !el || me.inMenu || el.folded || el.rides || result.scene === scene) continue;
1352
- const keep2 = { x: line.x, y: span.y + span.height / 2 };
1353
- const hit = cutClear(rect(line.x - COLUMN_REACH, span.y, 2 * COLUMN_REACH, span.height), blockers, keep2);
1354
- if (!hit) continue;
1355
- out.push({ id: `column:${side}:${line.ring}`, priority: 2, hit, slot: me.rect, marker: rect(line.x, span.y, 0, span.height), item: dragged, place, after: () => result });
1356
- }
1357
- return out;
1358
- }
1359
- function railTargets(scene, rest, base, side, dragged, blockers, settle) {
1360
- const rail = base.rails[side];
1361
- const rings = sideRings(rest.edges, side);
1362
- const mine = rest.edges.filter((e) => e.side === side).sort((a, b) => (rings.get(a.id) ?? 0) - (rings.get(b.id) ?? 0));
1363
- const ringOf = (id) => rings.get(id) ?? 0;
1364
- const stops2 = [];
1365
- let zone;
1366
- if (rail && rail.segments.length > 0) {
1367
- zone = rail.rect;
1368
- const groups = [];
1369
- for (const s of rail.segments) {
1370
- const g = groups[groups.length - 1];
1371
- if (g && g.edge === s.edge) g.y1 = s.rect.y + s.rect.height;
1372
- else groups.push({ edge: s.edge, y0: s.rect.y, y1: s.rect.y + s.rect.height });
1373
- }
1374
- for (const g of groups) stops2.push({ y: g.y0, ring: ringOf(g.edge) });
1375
- const last = groups[groups.length - 1];
1376
- stops2.push({ y: last.y1, ring: ringOf(last.edge) + 1 });
1377
- } else {
1378
- const span = sideSpan(base, side);
1379
- const firstCol = mine.find((e) => e.column === true);
1380
- const bars = new Set(rest.edges.filter((e) => e.side === side && isOutsideSideBar(rest, e)).map((e) => e.id));
1381
- const inner = Object.entries(base.edges).filter(([id, e]) => bars.has(id) && e.rect.height > 0 && e.rect.width > 0).map(([, e]) => side === "left" ? e.rect.x + e.rect.width : e.rect.x);
1382
- const pad = Number.isFinite(rest.inset?.[side]) ? Math.max(0, rest.inset[side]) : 0;
1383
- const x = inner.length > 0 ? side === "left" ? Math.max(...inner) : Math.min(...inner) : side === "left" ? rest.stage.x + pad : rest.stage.x + rest.stage.width - pad;
1384
- zone = rect(side === "left" ? x : x - RAIL_EDGE, span.y, RAIL_EDGE, span.height);
1385
- stops2.push({ y: span.y, ring: firstCol ? ringOf(firstCol.id) : mine.length });
1386
- }
1387
- const out = [];
1388
- stops2.forEach((s, i) => {
1389
- const from = i === 0 ? zone.y : (stops2[i - 1].y + s.y) / 2;
1390
- const to = i === stops2.length - 1 ? zone.y + zone.height : (s.y + stops2[i + 1].y) / 2;
1391
- if (to - from <= EPS2) return;
1392
- const place = { kind: "rail", side, ring: s.ring };
1393
- const result = settle(use(moveTo(scene, dragged, place), dragged));
1394
- const me = result.layout.items[dragged];
1395
- if (!me || me.rail !== side || result.scene === scene) return;
1396
- const hit = cutClear(rect(zone.x, from, zone.width, to - from), blockers, { x: zone.x + zone.width / 2, y: Math.min(Math.max(s.y, from), to) });
1397
- if (!hit) return;
1398
- out.push({ id: `rail:${side}:${s.ring}`, priority: 1, hit, slot: me.rect, marker: rect(zone.x, me.rect.y, zone.width, 0), item: dragged, place, after: () => result });
1399
- });
1400
- return out;
1684
+ function targets(scene, dragged, options = {}) {
1685
+ return generate(scene, dragged, options).targets;
1401
1686
  }
1402
1687
  function refusals(scene, dragged, options = {}) {
1403
- const item = Object.hasOwn(scene.items, dragged) ? scene.items[dragged] : void 0;
1404
- if (!item || item.fixed || item.closed) return [];
1405
- const prev = { ...options.previous ? { previous: options.previous } : {}, ...options.fresh ? { fresh: options.fresh } : {} };
1406
- const ts = targets(scene, dragged, options).filter((t) => !t.free);
1407
- const base = solve(lift(scene, dragged), prev);
1408
- const hits = ts.map((t) => t.hit);
1409
- const out = [];
1410
- const add = (edge, zone, hit, reason) => {
1411
- const clear = cutAway(hit, hits);
1412
- if (clear) out.push({ id: `refuse:${edge}:${zone}`, edge, zone, hit: clear, reason });
1413
- };
1414
- for (const e of scene.edges) {
1415
- const r = base.edges[e.id]?.rect;
1416
- if (!r || r.width <= EPS2 || r.height <= EPS2) continue;
1417
- const bar = (e.side === "top" || e.side === "bottom") && !e.compact;
1418
- if (!bar) {
1419
- if (!e.accepts.includes(item.kind)) add(e.id, "start", r, "not-accepted");
1420
- continue;
1421
- }
1422
- ZONES.forEach((zone, k) => {
1423
- const third = rect(r.x + r.width * k / 3, r.y, r.width / 3, r.height);
1424
- const mine = ts.some((t) => (t.place.kind === "gap" || t.place.kind === "home") && t.id.startsWith(`gap:${e.id}:`) && overlaps(t.hit, third));
1425
- if (mine) return;
1426
- add(e.id, zone, third, zoneTakes(e, zone, item.kind) ? "no-room" : "not-accepted");
1427
- });
1428
- }
1429
- return out;
1688
+ return generate(scene, dragged, options).refusals;
1430
1689
  }
1431
1690
  var SIDE_RANK = { left: 0, top: 1, bottom: 2, right: 3 };
1432
1691
  function stops(scene, dragged, options = {}) {
@@ -1448,185 +1707,6 @@ function stops(scene, dragged, options = {}) {
1448
1707
  };
1449
1708
  return list.map((t, i) => ({ t, i, k: key(t) })).sort((a, b) => a.k[0] - b.k[0] || a.k[1] - b.k[1] || a.k[2] - b.k[2] || a.i - b.i).map((x) => x.t);
1450
1709
  }
1451
- function edgeTargets(scene, rest, now, base, edge, dragged, reach, blockers, chrome, settle) {
1452
- const el = base.edges[edge.id];
1453
- if (!el || el.folded || el.rides) return [];
1454
- const column = axisOf(edge.side) === "column";
1455
- const restEdge = rest.edges.find((e) => e.id === edge.id);
1456
- const fixedIds = restEdge.items.filter((id) => rest.items[id]?.fixed && base.items[id]?.edge === edge.id);
1457
- const tooWideBefore = new Set(base.notes.flatMap((n) => "edge" in n && n.edge === edge.id && n.code === "too-wide" ? [n.id] : []));
1458
- const thirds = !column && !edge.compact;
1459
- const natural = (id, form) => {
1460
- const f = formsOf(scene.items[id], "row")[form];
1461
- return f ? formMain(f, "natural", el.cross) : 0;
1462
- };
1463
- const titles = thirds ? fixedIds.filter((id) => !!formsOf(scene.items[id], "row")[base.items[id].form]?.main.grows) : [];
1464
- const titled = titles.length > 0;
1465
- const fits = (after) => {
1466
- const me = after.items[dragged];
1467
- if (!me || me.edge !== edge.id || me.inMenu) return false;
1468
- for (const n of after.notes) {
1469
- if (!("edge" in n) || n.edge !== edge.id) continue;
1470
- if (n.code === "overflow") return false;
1471
- if (n.code === "too-wide" && !tooWideBefore.has(n.id)) return false;
1472
- }
1473
- for (const id of fixedIds) {
1474
- const a = after.items[id];
1475
- const b = base.items[id];
1476
- if (!a || a.form !== b.form || a.formKind !== b.formKind || Math.abs(a.rect.x - b.rect.x) > 0.5 || Math.abs(a.rect.y - b.rect.y) > 0.5) return false;
1477
- }
1478
- if (titled) {
1479
- if (me.form !== 0) return false;
1480
- for (const id of restEdge.items) {
1481
- const a = after.items[id];
1482
- const b = base.items[id];
1483
- if (!a || !b || b.edge !== edge.id) continue;
1484
- if (titles.includes(id) ? a.rect.width < natural(id, a.form) - 0.5 : a.form !== b.form || !!a.inMenu !== !!b.inMenu) return false;
1485
- }
1486
- }
1487
- return !chrome.some((c) => overlaps(me.rect, c));
1488
- };
1489
- const markerOf = (after) => {
1490
- const slot = after.items[dragged].rect;
1491
- const e = after.edges[edge.id];
1492
- return column ? rect(e.rect.x, slot.y, e.cross, 0) : rect(slot.x, e.rect.y, 0, e.cross);
1493
- };
1494
- const ownNow = now.items[dragged]?.edge;
1495
- const otherOf = (edges, skip) => Object.entries(edges).filter(([id, e]) => id !== edge.id && id !== skip && e.rect.width > EPS2 && e.rect.height > EPS2 && !(edge.compact && e.side === edge.side)).map(([, e]) => e.rect);
1496
- const otherEdges = otherOf(base.edges);
1497
- const nowEdges = otherOf(now.edges, ownNow);
1498
- const own = el.rect;
1499
- const trimOff = (h, others, k, besideOnly = false) => {
1500
- let [a0, a1] = column ? [h.y, h.y + h.height] : [h.x, h.x + h.width];
1501
- let [c0, c1] = column ? [h.x, h.x + h.width] : [h.y, h.y + h.height];
1502
- const km = column ? k.y : k.x;
1503
- const kc = column ? k.x : k.y;
1504
- const [w0, w1] = column ? [own.x, own.x + own.width] : [own.y, own.y + own.height];
1505
- for (const o of others) {
1506
- const [o0, o1] = column ? [o.y, o.y + o.height] : [o.x, o.x + o.width];
1507
- const [p0, p1] = column ? [o.x, o.x + o.width] : [o.y, o.y + o.height];
1508
- if (Math.min(a1, o1) - Math.max(a0, o0) <= EPS2 || Math.min(c1, p1) - Math.max(c0, p0) <= EPS2) continue;
1509
- const across = !besideOnly && Math.min(w1, p1) - Math.max(w0, p0) > EPS2;
1510
- if (!across) {
1511
- if (p1 <= kc + EPS2) c0 = Math.max(c0, p1);
1512
- else if (p0 >= kc - EPS2) c1 = Math.min(c1, p0);
1513
- } else if (o1 <= km + EPS2) a0 = Math.max(a0, o1);
1514
- else if (o0 >= km - EPS2) a1 = Math.min(a1, o0);
1515
- }
1516
- return column ? rect(c0, a0, Math.max(0, c1 - c0), Math.max(0, a1 - a0)) : rect(a0, c0, Math.max(0, a1 - a0), Math.max(0, c1 - c0));
1517
- };
1518
- const target = (id, place, hit, keep2, solved) => {
1519
- const result = solved ?? settle(use(moveTo(scene, dragged, place), dragged));
1520
- if (!fits(result.layout)) return null;
1521
- const clear = cutClear(id.startsWith("gap:") ? trimOff(trimOff(hit, otherEdges, keep2), nowEdges, keep2, true) : hit, blockers, keep2);
1522
- if (!clear) return null;
1523
- return { id, priority: 2, hit: clear, slot: result.layout.items[dragged].rect, marker: markerOf(result.layout), item: dragged, place, after: () => result };
1524
- };
1525
- const drawn = base.edges[edge.id].stack.items.filter((p) => p.kind !== "menu").map((p) => p.id);
1526
- if (drawn.length === 0 && !el.menu) {
1527
- const depth = Math.max(0, Number.isFinite(edge.strip) ? edge.strip : DEFAULT_STRIP);
1528
- const r = el.rect;
1529
- const hit = edge.side === "left" ? rect(r.x, r.y, depth, r.height) : edge.side === "right" ? rect(r.x + r.width - depth, r.y, depth, r.height) : edge.side === "top" ? rect(r.x, r.y, r.width, depth) : rect(r.x, r.y + r.height - depth, r.width, depth);
1530
- const keep2 = { x: hit.x + hit.width / 2, y: hit.y + hit.height / 2 };
1531
- if (thirds) {
1532
- const c = base.content;
1533
- const x0 = Math.max(hit.x, c.x);
1534
- const x1 = Math.min(hit.x + hit.width, c.x + c.width);
1535
- if (x1 - x0 <= EPS2) return [];
1536
- const w = (x1 - x0) / 3;
1537
- const out2 = [];
1538
- ZONES.forEach((zone, k) => {
1539
- if (!zoneTakes(edge, zone, scene.items[dragged].kind)) return;
1540
- const part = rect(x0 + k * w, hit.y, w, hit.height);
1541
- const t2 = target(`strip:${edge.id}:${zone}`, { kind: "gap", edge: edge.id, zone, at: 0 }, part, { x: part.x + part.width / 2, y: part.y + part.height / 2 });
1542
- if (t2) out2.push(t2);
1543
- });
1544
- return out2;
1545
- }
1546
- if (!zoneTakes(edge, "start", scene.items[dragged].kind)) return [];
1547
- const t = target(`strip:${edge.id}`, { kind: "gap", edge: edge.id, zone: "start", at: 0 }, hit, keep2);
1548
- return t ? [t] : [];
1549
- }
1550
- const main0 = column ? el.rect.y : el.rect.x;
1551
- const length = column ? el.rect.height : el.rect.width;
1552
- const at = (r) => column ? r.y : r.x;
1553
- const len = (r) => column ? r.height : r.width;
1554
- const pad = { start: Math.max(0, edge.pad?.start ?? 0), end: Math.max(0, edge.pad?.end ?? 0) };
1555
- const menuLen = el.menu ? len(el.menu.rect) : 0;
1556
- const occupied = (id) => base.items[id]?.fixed ? reservedRect(scene, base, id) : base.items[id].rect;
1557
- const drawnOf = (z) => restEdge.items.filter((id) => zoneOf(rest.items[id]) === z && base.items[id]?.edge === edge.id && !base.items[id].inMenu).map(occupied);
1558
- const startEnd = (() => {
1559
- const s = drawnOf("start");
1560
- return s.length ? at(s[s.length - 1]) + len(s[s.length - 1]) : main0 + pad.start;
1561
- })();
1562
- const endStart = (() => {
1563
- const e = drawnOf("end");
1564
- return e.length ? at(e[0]) : main0 + length - pad.end - menuLen;
1565
- })();
1566
- const empty = { start: main0 + pad.start, center: Math.min(Math.max(main0 + length / 2, startEnd), Math.max(startEnd, endStart)), end: main0 + length - pad.end - menuLen };
1567
- const gaps = [];
1568
- for (const zone of zonesTaking(edge, scene.items[dragged].kind)) {
1569
- const members = restEdge.items.filter((id) => zoneOf(rest.items[id]) === zone);
1570
- for (let k = 0; k <= members.length; k++) {
1571
- const next = members.slice(k).find((id) => base.items[id]?.edge === edge.id && !base.items[id].inMenu);
1572
- const before = members.slice(0, k).reverse().find((id) => base.items[id]?.edge === edge.id && !base.items[id].inMenu);
1573
- const line = next ? at(occupied(next)) : before ? at(occupied(before)) + len(occupied(before)) : empty[zone];
1574
- gaps.push({ zone, at: k, line });
1575
- }
1576
- }
1577
- const sorted = gaps.map((g, i) => ({ ...g, i })).sort((a, b) => (thirds ? ZONE_ORDER[a.zone] - ZONE_ORDER[b.zone] : 0) || a.line - b.line || a.i - b.i).filter((g, i, all) => i === 0 || thirds && g.zone !== all[i - 1].zone || Math.abs(g.line - all[i - 1].line) > EPS2);
1578
- const out = [];
1579
- const live = sorted.map((g) => {
1580
- const place = { kind: "gap", edge: edge.id, zone: g.zone, at: g.at };
1581
- return { ...g, place, result: settle(use(moveTo(scene, dragged, place), dragged)) };
1582
- }).filter((g) => fits(g.result.layout));
1583
- const bandOpt = edge.hitBand ?? DEFAULT_HIT_BAND;
1584
- const band = bandOpt === "middle" ? Infinity : Math.max(0, Number.isFinite(bandOpt) ? bandOpt : DEFAULT_HIT_BAND);
1585
- const bodies = Number.isFinite(band) ? Object.values(base.items).filter((p) => p.edge === edge.id && p.formKind === "open").map((p) => ({ a: at(p.rect), b: at(p.rect) + len(p.rect) })) : [];
1586
- const inward = edge.compact ? RAIL_REACH : 0;
1587
- const solidSpans = fixedIds.filter((id) => !formsOf(scene.items[id], axisOf(edge.side))[base.items[id].form]?.main.grows).map((id) => ({ a: at(base.items[id].rect), b: at(base.items[id].rect) + len(base.items[id].rect) }));
1588
- live.forEach((g, i) => {
1589
- let from = i === 0 ? -Infinity : (live[i - 1].line + g.line) / 2;
1590
- let to = i === live.length - 1 ? Infinity : (g.line + live[i + 1].line) / 2;
1591
- if (thirds) {
1592
- const k = ZONE_ORDER[g.zone];
1593
- const prev = live[i - 1]?.zone === g.zone ? live[i - 1] : void 0;
1594
- const next = live[i + 1]?.zone === g.zone ? live[i + 1] : void 0;
1595
- const a0 = main0 + length * k / 3;
1596
- const b0 = main0 + length * (k + 1) / 3;
1597
- const lines = live.filter((x) => x.zone === g.zone).map((x) => x.line);
1598
- const first = Math.min(...lines);
1599
- const last = Math.max(...lines);
1600
- const a1 = Math.max(a0, ...solidSpans.filter((s) => s.b <= first + EPS2).map((s) => s.b));
1601
- const b1 = Math.min(b0, ...solidSpans.filter((s) => s.a >= last - EPS2).map((s) => s.a));
1602
- const [a, b] = b1 - a1 > EPS2 ? [a1, b1] : [a0, b0];
1603
- const span0 = Math.min(a, ...lines);
1604
- const span1 = Math.max(b, ...lines);
1605
- const inThird = (x) => span1 - span0 > EPS2 ? a + (x - span0) / (span1 - span0) * (b - a) : x;
1606
- const lo = k === 0 ? -Infinity : a0;
1607
- const hi = k === 2 ? Infinity : b0;
1608
- from = Math.max(lo, prev ? (inThird(prev.line) + inThird(g.line)) / 2 : lo);
1609
- to = Math.min(hi, next ? (inThird(g.line) + inThird(next.line)) / 2 : hi);
1610
- }
1611
- for (const p of bodies) {
1612
- if (p.b <= g.line + EPS2 && p.b > from) from = Math.max(from, p.b - band / 2);
1613
- if (p.a >= g.line - EPS2 && p.a < to) to = Math.min(to, p.a + band / 2);
1614
- }
1615
- from = from <= main0 + pad.start + EPS2 ? main0 - reach : from;
1616
- to = to >= main0 + length - pad.end - menuLen - EPS2 ? main0 + length + reach : to;
1617
- from = Math.max(from, main0 - reach);
1618
- to = Math.min(to, main0 + length + reach);
1619
- if (to - from <= EPS2) return;
1620
- const thick = Math.max(el.cross, g.result.layout.edges[edge.id]?.cross ?? 0) + inward;
1621
- const r = el.rect;
1622
- const hit = column ? rect(edge.side === "left" ? r.x : r.x + r.width - (thick - inward) - inward, from, thick, to - from) : rect(from, edge.side === "top" ? r.y : r.y + r.height - (thick - inward) - inward, to - from, thick);
1623
- const line = Math.min(Math.max(g.line, from), to);
1624
- const keep2 = column ? { x: r.x + r.width / 2, y: line } : { x: line, y: r.y + r.height / 2 };
1625
- const t = target(`gap:${edge.id}:${g.zone}:${g.at}`, g.place, hit, keep2, g.result);
1626
- if (t) out.push(t);
1627
- });
1628
- return out;
1629
- }
1630
1710
 
1631
1711
  export {
1632
1712
  SIDES,
@@ -1637,6 +1717,7 @@ export {
1637
1717
  DEFAULT_HIT_BAND,
1638
1718
  DEFAULT_FLOAT_SIZE,
1639
1719
  isOutsideSideBar,
1720
+ isSideBar,
1640
1721
  floatOrientOf,
1641
1722
  floatSizeOf,
1642
1723
  solve,
@@ -1656,9 +1737,9 @@ export {
1656
1737
  dockHome,
1657
1738
  storeScene,
1658
1739
  readScene,
1659
- targets,
1660
1740
  reservedRect,
1741
+ targets,
1661
1742
  refusals,
1662
1743
  stops
1663
1744
  };
1664
- //# sourceMappingURL=chunk-6WFVXHA3.js.map
1745
+ //# sourceMappingURL=chunk-SZKU7CRX.js.map