@n-uf/placement 26.11.14 → 26.11.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/stack.js CHANGED
@@ -1,304 +1,97 @@
1
1
  import {
2
- clampInto,
2
+ DEFAULT_MENU,
3
+ DEFAULT_RAIL,
4
+ createStack,
5
+ fitFloat,
6
+ fitStack,
7
+ formSetsCross,
8
+ readFloats,
9
+ readStack,
10
+ stackFloatTarget,
11
+ storeFloats,
12
+ storeStack,
13
+ withItem
14
+ } from "./chunk-RYFQKQPQ.js";
15
+ import {
3
16
  rect
4
17
  } from "./chunk-QMTNB2C3.js";
5
18
 
6
- // src/stack/model.ts
7
- var DEFAULT_RAIL = 30;
8
- var DEFAULT_MENU = 28;
9
- var finite = (x, fallback) => Number.isFinite(x) ? x : fallback;
10
- function limitsOf(input) {
11
- const min = Math.max(0, finite(input.cross.min, 0));
12
- const max = Number.isNaN(input.cross.max) ? Infinity : input.cross.max;
13
- return { min, max: Math.max(min, max), rail: Math.max(0, finite(input.cross.rail ?? DEFAULT_RAIL, DEFAULT_RAIL)) };
14
- }
15
- var menuMain = (input) => Math.max(0, finite(input.menu?.main ?? DEFAULT_MENU, DEFAULT_MENU));
16
- function spacingOf(input) {
17
- const px = (x) => Math.max(0, finite(x ?? 0, 0));
18
- return { gap: px(input.gap), start: px(input.pad?.start), end: px(input.pad?.end) };
19
- }
20
- var formSetsCross = (f) => f.setsCross ?? f.kind === "open";
21
- function withItem(input, item, at) {
22
- const k = Math.min(Math.max(0, Math.floor(at)), input.items.length);
23
- return { ...input, items: [...input.items.slice(0, k), item, ...input.items.slice(k)] };
24
- }
25
-
26
- // src/stack/layout.ts
27
- var EPS = 1e-6;
28
- var isMenu = (f) => f.kind === "menu";
29
- var finite2 = (x, fallback = 0) => Number.isFinite(x) ? x : fallback;
30
- var mainOf = (f, which) => isMenu(f) ? 0 : Math.max(0, finite2(f.main[which]));
31
- var crossMin = (f) => finite2(f.cross.min ?? f.cross.natural);
32
- var rankOf = (f) => finite2(f.rank ?? 0);
33
- function victim(items, to) {
34
- let best = -1;
35
- for (let i = 0; i < items.length; i++) {
36
- if (to[i] < 0) continue;
37
- if (best < 0) {
38
- best = i;
39
- continue;
40
- }
41
- const a = items[i];
42
- const b = items[best];
43
- const ra = rankOf(a.forms[to[i]]);
44
- const rb = rankOf(b.forms[to[best]]);
45
- const ka = a.kept ? 1 : 0;
46
- const kb = b.kept ? 1 : 0;
47
- if (ra !== rb ? ra < rb : ka !== kb ? ka < kb : a.used !== b.used ? a.used < b.used : true) best = i;
48
- }
49
- return best;
50
- }
51
- function fitStack(input, options = {}) {
52
- const fresh = solve(input);
53
- const previous = options.previous;
54
- if (!previous || previous.need === void 0) return fresh;
55
- const seed = { need: 0 };
56
- const kept = solve(input, previous, seed);
57
- if (kept.cross > previous.cross + EPS) return fresh;
58
- const moreLength = Math.max(0, kept.length) - seed.need > previous.length - previous.need + EPS;
59
- if (!moreLength) return kept;
60
- const gain = fresh.items.every((p, i) => p.form <= kept.items[i].form) && fresh.items.some((p, i) => p.form < kept.items[i].form);
61
- return gain ? fresh : solve(input, previous, { need: 0 }, true);
62
- }
63
- function solve(input, previous, seed, grow = false) {
64
- const length = Math.max(0, finite2(input.length));
65
- const lim = limitsOf(input);
66
- const items = input.items.filter((it) => it.forms.length > 0);
67
- const start = items.map((it) => Math.min(Math.max(0, Math.floor(finite2(it.from ?? 0))), it.forms.length - 1));
68
- const at = [...start];
69
- if (previous) {
70
- const had = new Map(previous.items.map((p) => [p.id, p]));
71
- const began = /* @__PURE__ */ new Map();
72
- for (const n of previous.notes) if (n.code === "form-down" && !began.has(n.id)) began.set(n.id, n.from);
73
- items.forEach((it, i) => {
74
- const p = had.get(it.id);
75
- if (!p || p.form < start[i] || p.form >= it.forms.length || start[i] < (began.get(it.id) ?? p.form)) return;
76
- const f = it.forms[p.form];
77
- if (f.kind === p.kind) at[i] = p.form;
78
- });
79
- }
80
- const form = (i) => items[i].forms[at[i]];
81
- const idx = items.map((_, i) => i);
82
- const firstStep = [];
83
- const menuOrder = [];
84
- const step = (i, j) => {
85
- if (!firstStep.includes(i)) firstStep.push(i);
86
- at[i] = j;
87
- if (isMenu(form(i)) && !menuOrder.includes(i)) menuOrder.push(i);
88
- };
89
- const named = (code) => previous ? previous.notes.filter((n) => n.code === code).map((n) => items.findIndex((it) => it.id === n.id)) : [];
90
- for (const i of [...named("form-down"), ...idx]) if (i >= 0 && at[i] > start[i] && !firstStep.includes(i)) firstStep.push(i);
91
- for (const i of [...named("to-menu"), ...idx]) if (i >= 0 && isMenu(form(i)) && !menuOrder.includes(i)) menuOrder.push(i);
92
- const crossOf = () => {
93
- const setters = idx.filter((i) => !isMenu(form(i)) && formSetsCross(form(i)));
94
- if (setters.length === 0) return lim.rail;
95
- const want = Math.max(...setters.map((i) => finite2(form(i).cross.natural)));
96
- return Math.min(Math.max(want, lim.min), lim.max);
97
- };
98
- const inMenu = () => idx.some((i) => isMenu(form(i)));
99
- const sp = spacingOf(input);
100
- const gapsOf = () => {
101
- const slots = idx.filter((i) => !isMenu(form(i))).length + (inMenu() ? 1 : 0);
102
- return slots > 1 ? (slots - 1) * sp.gap : 0;
103
- };
104
- const minSum = () => idx.reduce((s, i) => s + mainOf(form(i), "min"), 0) + (inMenu() ? menuMain(input) : 0) + gapsOf() + sp.start + sp.end;
105
- const stepTo = (i) => {
106
- const from = at[i];
107
- const base = minSum();
108
- let found = -1;
109
- for (let j = from + 1; j < items[i].forms.length && found < 0; j++) {
110
- at[i] = j;
111
- if (minSum() < base - EPS) found = j;
112
- }
113
- at[i] = from;
114
- return found;
115
- };
116
- const menuFormAfter = (i) => {
117
- for (let j = at[i] + 1; j < items[i].forms.length; j++) if (isMenu(items[i].forms[j])) return j;
118
- return -1;
119
- };
120
- const menuBatch = (pool) => {
121
- const saved = [...at];
122
- const base = minSum();
123
- const left = [...pool];
124
- const batch = [];
125
- let found = false;
126
- for (let b = victim(items, left); b >= 0 && !found; b = victim(items, left)) {
127
- batch.push([b, left[b]]);
128
- at[b] = left[b];
129
- left[b] = -1;
130
- found = minSum() < base - EPS;
131
- }
132
- at.splice(0, at.length, ...saved);
133
- return found ? batch : null;
134
- };
135
- const canStep = (i) => at[i] + 1 < items[i].forms.length;
136
- const tooWide = (i, cross2) => {
137
- const f = form(i);
138
- if (isMenu(f)) return false;
139
- if (formSetsCross(f)) return input.strictCross === true && crossMin(f) > lim.max + EPS;
140
- return crossMin(f) > cross2 + EPS;
141
- };
142
- if (seed) seed.need = minSum();
143
- if (grow) {
144
- const comesBefore = (a, b) => {
145
- const ra = rankOf(items[a].forms[at[a]]);
146
- const rb = rankOf(items[b].forms[at[b]]);
147
- const ka = items[a].kept ? 1 : 0;
148
- const kb = items[b].kept ? 1 : 0;
149
- return ra !== rb ? ra > rb : ka !== kb ? ka > kb : items[a].used !== items[b].used ? items[a].used > items[b].used : a < b;
150
- };
151
- for (; ; ) {
152
- const c0 = crossOf();
153
- const fitsWith = (changed) => minSum() <= length + EPS && crossOf() === c0 && changed.every((j) => !tooWide(j, c0));
154
- let best = -1;
155
- let bestTo = -1;
156
- for (const i of idx) {
157
- const cur = at[i];
158
- let to = -1;
159
- for (let k = cur - 1; k >= start[i] && to < 0; k--) {
160
- at[i] = k;
161
- if (fitsWith([i])) to = k;
162
- }
163
- at[i] = cur;
164
- if (to >= 0 && (best < 0 || comesBefore(i, best))) {
165
- best = i;
166
- bestTo = to;
167
- }
168
- }
169
- if (best >= 0) {
170
- at[best] = bestTo;
171
- continue;
172
- }
173
- const inside2 = idx.filter((i) => isMenu(form(i)) && at[i] > start[i]).sort((a, b) => comesBefore(a, b) ? -1 : 1);
174
- let out = false;
175
- for (let n = 2; n <= inside2.length && !out; n++) {
176
- const group = inside2.slice(0, n);
177
- const was = group.map((i) => at[i]);
178
- for (const i of group) at[i] = at[i] - 1;
179
- if (fitsWith(group)) out = true;
180
- else group.forEach((i, k) => at[i] = was[k]);
181
- }
182
- if (!out) break;
183
- }
184
- }
185
- let cross = crossOf();
186
- let overflow = 0;
187
- const guard = 2 * items.reduce((s, it) => s + it.forms.length, 0) + 2;
188
- for (let n = 0; n <= guard; n++) {
189
- cross = crossOf();
190
- let stepped = false;
191
- for (const i of idx) {
192
- if (tooWide(i, cross) && canStep(i) && !isMenu(items[i].forms[at[i] + 1])) {
193
- step(i, at[i] + 1);
194
- stepped = true;
195
- }
196
- }
197
- if (stepped) continue;
198
- const over = minSum() - length;
199
- if (over <= EPS) break;
200
- const to = idx.map(stepTo);
201
- const pool = idx.map((i) => to[i] < 0 && !inMenu() ? menuFormAfter(i) : -1);
202
- const v = victim(
203
- items,
204
- idx.map((i) => to[i] >= 0 ? to[i] : pool[i])
205
- );
206
- if (v >= 0 && to[v] < 0) {
207
- const batch = menuBatch(pool);
208
- if (batch) {
209
- for (const [i, j] of batch) step(i, j);
210
- continue;
211
- }
212
- }
213
- const w = v >= 0 && to[v] >= 0 ? v : victim(items, to);
214
- if (w < 0) {
215
- overflow = items.length > 0 ? over : 0;
216
- break;
217
- }
218
- step(w, to[w]);
219
- }
220
- cross = crossOf();
221
- const menuSize = inMenu() ? menuMain(input) : 0;
222
- const avail = Math.max(0, length - sp.start - sp.end - gapsOf() - menuSize);
223
- const drawn = idx.filter((i) => !isMenu(form(i)));
224
- const min = (i) => mainOf(form(i), "min");
225
- const nat = (i) => Math.max(min(i), mainOf(form(i), "natural"));
226
- const sumMin = drawn.reduce((s, i) => s + min(i), 0);
227
- const sumNat = drawn.reduce((s, i) => s + nat(i), 0);
228
- const size = /* @__PURE__ */ new Map();
229
- if (sumNat <= avail + EPS) {
230
- const growers = drawn.filter((i) => form(i).main.grows);
231
- const spare = Math.max(0, avail - sumNat);
232
- const weight = growers.reduce((s, i) => s + nat(i), 0);
233
- for (const i of drawn) {
234
- const share = growers.includes(i) ? weight > EPS ? nat(i) / weight : 1 / growers.length : 0;
235
- size.set(i, nat(i) + spare * share);
236
- }
237
- } else {
238
- const t = avail > sumMin && sumNat > sumMin ? (avail - sumMin) / (sumNat - sumMin) : 0;
239
- for (const i of drawn) size.set(i, min(i) + (nat(i) - min(i)) * t);
240
- }
241
- const rectAt = (from, main) => input.axis === "column" ? { x: 0, y: from, width: cross, height: main } : { x: from, y: 0, width: main, height: cross };
242
- const rects = /* @__PURE__ */ new Map();
243
- let cursor = sp.start;
244
- for (const i of drawn) {
245
- const s = size.get(i);
246
- rects.set(i, rectAt(cursor, s));
247
- cursor += s + sp.gap;
248
- }
249
- const menuRect = rectAt(Math.max(drawn.length > 0 ? cursor : sp.start, length - sp.end - menuSize), menuSize);
250
- const inside = idx.filter((i) => isMenu(form(i)));
251
- const menu = inside.length > 0 ? { rect: menuRect, items: inside.map((i) => items[i].id) } : null;
252
- const placed = idx.map((i) => ({ id: items[i].id, form: at[i], kind: form(i).kind, rect: isMenu(form(i)) ? menuRect : rects.get(i) }));
253
- const notes = [];
254
- for (const it of input.items) if (it.forms.length === 0) notes.push({ code: "no-forms", id: it.id });
255
- for (const i of firstStep) if (at[i] > start[i]) notes.push({ code: "form-down", id: items[i].id, from: start[i], to: at[i] });
256
- for (const i of menuOrder) if (isMenu(form(i))) notes.push({ code: "to-menu", id: items[i].id });
257
- for (const i of idx) if (tooWide(i, cross)) notes.push({ code: "too-wide", id: items[i].id });
258
- if (overflow > EPS) notes.push({ code: "overflow", over: overflow });
259
- return { axis: input.axis, length, cross, items: placed, menu, notes, need: minSum() };
260
- }
261
-
262
19
  // src/stack/targets.ts
263
20
  function bounds(input, fit, reach) {
264
21
  const column = input.axis === "column";
265
22
  const start = (r) => column ? r.y : r.x;
266
23
  const size = (r) => column ? r.height : r.width;
267
24
  const placed = new Map(fit.items.map((p) => [p.id, p]));
268
- const b = [0];
25
+ const c = [0];
269
26
  let end = 0;
27
+ let cap = fit.length;
270
28
  for (const it of input.items) {
271
29
  const p = placed.get(it.id);
272
30
  if (!p || p.kind === "menu") {
273
- b.push(end);
31
+ c.push(end, end);
274
32
  continue;
275
33
  }
276
- b.push(start(p.rect) + size(p.rect) / 2);
277
- end = start(p.rect) + size(p.rect);
278
- }
279
- b.push(fit.length);
280
- for (let i = 1; i < b.length; i++) b[i] = Math.min(Math.max(b[i], b[i - 1]), fit.length);
281
- b[0] = 0 - reach;
282
- b[b.length - 1] = fit.length + reach;
283
- return b;
34
+ const s = start(p.rect);
35
+ const mid = s + size(p.rect) / 2;
36
+ c.push(it.fixed ? s : mid, it.fixed ? s + size(p.rect) : mid);
37
+ end = s + size(p.rect);
38
+ if (it.fixed) cap = Math.max(cap, end);
39
+ }
40
+ c.push(fit.length);
41
+ for (let i = 1; i < c.length; i++) c[i] = Math.min(Math.max(c[i], c[i - 1]), cap);
42
+ c[0] = 0 - reach;
43
+ c[c.length - 1] = Math.max(fit.length + reach, c[c.length - 2]);
44
+ const from = [];
45
+ const to = [];
46
+ for (let k = 0; k <= input.items.length; k++) {
47
+ from.push(c[2 * k]);
48
+ to.push(c[2 * k + 1]);
49
+ }
50
+ return { from, to };
51
+ }
52
+ function fitsBeside(base, fit, dragged, fixed, axis, tooWideBefore) {
53
+ if (dragged.kind === "menu") return false;
54
+ for (const n of fit.notes) {
55
+ if (n.code === "overflow") return false;
56
+ if (n.code === "too-wide" && !tooWideBefore.has(n.id)) return false;
57
+ }
58
+ const main = (r) => axis === "column" ? r.height : r.width;
59
+ const across = (r) => axis === "column" ? r.width : r.height;
60
+ const was = new Map(base.items.map((p) => [p.id, p]));
61
+ for (const p of fit.items) {
62
+ const item = fixed.get(p.id);
63
+ const q = was.get(p.id);
64
+ if (!item || !q || p.kind === "menu" || q.kind === "menu") continue;
65
+ if (p.form !== q.form) return false;
66
+ if (main(p.rect) < Math.max(0, item.forms[p.form].main.min) - 1e-6) return false;
67
+ if (across(p.rect) < across(q.rect) - 1e-6) return false;
68
+ }
69
+ return true;
284
70
  }
285
71
  function stackTargets(input, dragged, options = {}) {
286
72
  if (dragged.forms.length === 0) return [];
73
+ if (dragged.fixed) return [];
74
+ if (input.accepts && !input.accepts(dragged)) return [];
287
75
  const rest = { ...input, items: input.items.filter((it) => it.id !== dragged.id) };
288
76
  const fitOptions = options.previous ? { previous: options.previous } : {};
289
77
  const base = fitStack(rest, fitOptions);
290
78
  const reach = Math.max(0, Number.isFinite(options.endReach) ? options.endReach : 0);
291
79
  const b = bounds(rest, base, reach);
292
80
  const column = rest.axis === "column";
81
+ const fixedItems = new Map(rest.items.filter((it) => it.fixed).map((it) => [it.id, it]));
82
+ const tooWideBefore = new Set(base.notes.flatMap((n) => n.code === "too-wide" ? [n.id] : []));
293
83
  const newest = rest.items.reduce((m, it) => Math.max(m, it.used), dragged.used) + 1;
294
84
  const item = { ...dragged, used: newest };
295
85
  const out = [];
296
86
  for (let k = 0; k <= rest.items.length; k++) {
87
+ if (options.allows && !options.allows(dragged, k)) continue;
297
88
  const solved = withItem(rest, item, k);
298
89
  const fit = fitStack(solved, fitOptions);
299
- const slot = fit.items.find((p) => p.id === dragged.id).rect;
300
- const from = b[k];
301
- const to = b[k + 1];
90
+ const placed = fit.items.find((p) => p.id === dragged.id);
91
+ const slot = placed.rect;
92
+ if (options.onlyFits && !fitsBeside(base, fit, placed, fixedItems, rest.axis, tooWideBefore)) continue;
93
+ const from = b.from[k];
94
+ const to = b.to[k];
302
95
  const across = Math.max(base.cross, fit.cross);
303
96
  out.push({
304
97
  id: `gap:${k}`,
@@ -315,291 +108,6 @@ function stackTargets(input, dragged, options = {}) {
315
108
  }
316
109
  return out;
317
110
  }
318
-
319
- // src/stack/store.ts
320
- var createStack = (order) => ({ order: [...order], kept: [], closed: [], used: [...order], forms: {} });
321
- var isForm = (f) => typeof f === "number" && Number.isInteger(f) && f >= 1;
322
- var uniq = (ids, order) => [...new Set(ids)].filter((id) => order.includes(id));
323
- var uniqLast = (ids, order) => ids.filter((id, i) => order.includes(id) && ids.lastIndexOf(id) === i);
324
- var noForms = () => /* @__PURE__ */ Object.create(null);
325
- function storeStack(state) {
326
- const forms = noForms();
327
- for (const id of state.order) {
328
- const f = state.forms[id];
329
- if (id !== "__proto__" && Object.hasOwn(state.forms, id) && isForm(f)) forms[id] = f;
330
- }
331
- return { v: 1, order: [...state.order], kept: uniq(state.kept, state.order), closed: uniq(state.closed, state.order), used: uniqLast(state.used, state.order), forms };
332
- }
333
- var isObject = (x) => typeof x === "object" && x !== null && !Array.isArray(x);
334
- function readStack(data, known) {
335
- try {
336
- return read(data, known);
337
- } catch (e) {
338
- return { ok: false, error: `unreadable: ${e instanceof Error ? e.message : typeof e}` };
339
- }
340
- }
341
- var versionWords = (v) => typeof v === "number" ? String(v) : typeof v === "string" ? JSON.stringify(v.length > 24 ? `${v.slice(0, 24)}\u2026` : v) : typeof v;
342
- function read(data, known) {
343
- if (!isObject(data)) return { ok: false, error: "not an object" };
344
- if (data.v !== 1) return { ok: false, error: `unknown version ${versionWords(data.v)} (this reads version 1)` };
345
- if (!Array.isArray(data.order)) return { ok: false, error: "order is missing" };
346
- const dropped = [];
347
- const drop = (what) => {
348
- if (!dropped.includes(what)) dropped.push(what);
349
- };
350
- for (const k of Object.keys(data)) if (!["v", "order", "kept", "closed", "used", "forms"].includes(k)) drop(k);
351
- const order = [];
352
- for (const x of data.order) {
353
- if (typeof x !== "string") drop(`a ${typeof x} where an id goes`);
354
- else if (known && !known.includes(x) || order.includes(x)) drop(x);
355
- else order.push(x);
356
- }
357
- const list = (key) => {
358
- const out = [];
359
- const x = data[key];
360
- if (x === void 0) return out;
361
- if (!Array.isArray(x)) {
362
- drop(key);
363
- return out;
364
- }
365
- for (const id of x) {
366
- if (typeof id === "string" && order.includes(id)) {
367
- if (key === "used" && out.includes(id)) out.splice(out.indexOf(id), 1);
368
- if (!out.includes(id)) out.push(id);
369
- } else drop(typeof id === "string" ? `${key}:${id}` : `a ${typeof id} in ${key}`);
370
- }
371
- return out;
372
- };
373
- const kept = list("kept");
374
- const closed = list("closed");
375
- const used = list("used");
376
- const forms = noForms();
377
- if (data.forms !== void 0) {
378
- if (!isObject(data.forms)) drop("forms");
379
- else {
380
- for (const id of Object.keys(data.forms)) {
381
- const f = data.forms[id];
382
- if (id === "__proto__" || !Object.hasOwn(data.forms, id) || !order.includes(id) || !isForm(f)) drop(`forms:${id}`);
383
- else forms[id] = f;
384
- }
385
- }
386
- }
387
- return { ok: true, state: { order, kept, closed, used, forms }, dropped };
388
- }
389
-
390
- // src/stack/floats.ts
391
- var EPS2 = 1e-3;
392
- var finite3 = (x, fallback) => Number.isFinite(x) ? x : fallback;
393
- 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;
394
- var shared = (a, b) => Math.max(0, Math.min(a.x + a.width, b.x + b.width) - Math.max(a.x, b.x)) * Math.max(0, Math.min(a.y + a.height, b.y + b.height) - Math.max(a.y, b.y));
395
- var clean = (r, at) => rect(finite3(r.x, at.x), finite3(r.y, at.y), Math.max(0, finite3(r.width, 0)), Math.max(0, finite3(r.height, 0)));
396
- var WORK = 3e6;
397
- var COVERED_PLACES = 2048;
398
- var Nearest = class {
399
- h = [];
400
- less(a, b) {
401
- return a[2] !== b[2] ? a[2] < b[2] : a[0] !== b[0] ? a[0] < b[0] : a[1] < b[1];
402
- }
403
- get size() {
404
- return this.h.length;
405
- }
406
- push(t) {
407
- const h = this.h;
408
- let i = h.push(t) - 1;
409
- while (i > 0) {
410
- const p = i - 1 >> 1;
411
- if (!this.less(h[i], h[p])) break;
412
- [h[i], h[p]] = [h[p], h[i]];
413
- i = p;
414
- }
415
- }
416
- pop() {
417
- const h = this.h;
418
- const top = h[0];
419
- const last = h.pop();
420
- if (h.length > 0) {
421
- h[0] = last;
422
- let i = 0;
423
- for (; ; ) {
424
- const l = 2 * i + 1;
425
- const r = l + 1;
426
- let m = i;
427
- if (l < h.length && this.less(h[l], h[m])) m = l;
428
- if (r < h.length && this.less(h[r], h[m])) m = r;
429
- if (m === i) break;
430
- [h[i], h[m]] = [h[m], h[i]];
431
- i = m;
432
- }
433
- }
434
- return top;
435
- }
436
- };
437
- function* byDistance(xs, ys, fx, fy) {
438
- const sx = [...xs].sort((a, b) => Math.abs(a - fx) - Math.abs(b - fx) || a - b);
439
- const sy = [...ys].sort((a, b) => Math.abs(a - fy) - Math.abs(b - fy) || a - b);
440
- const q = new Nearest();
441
- const d = (i, j) => Math.hypot(sx[i] - fx, sy[j] - fy);
442
- if (sy.length > 0) for (let i = 0; i < sx.length; i++) q.push([i, 0, d(i, 0)]);
443
- while (q.size > 0) {
444
- const [i, j, far] = q.pop();
445
- yield { x: sx[i], y: sy[j], far };
446
- if (j + 1 < sy.length) q.push([i, j + 1, d(i, j + 1)]);
447
- }
448
- }
449
- function fitFloat(want, stage, avoid = []) {
450
- const area = clean(stage, rect(0, 0, 0, 0));
451
- const r = clean(want, area);
452
- const inside = clampInto(r, area);
453
- const clamped = inside.x !== want.x || inside.y !== want.y;
454
- const reach = rect(area.x, area.y, Math.max(area.width, r.width), Math.max(area.height, r.height));
455
- const near = [];
456
- avoid.forEach((a, index) => {
457
- const at = clean(a, area);
458
- if (at.width > EPS2 && at.height > EPS2 && overlaps(at, reach)) near.push({ at, index });
459
- });
460
- const under = (cand) => near.filter((n) => overlaps(cand, n.at)).map((n) => n.index);
461
- const blocked = (cand) => near.some((n) => overlaps(cand, n.at));
462
- if (!blocked(inside)) return { rect: inside, clamped, snapped: false, covers: [], searchCapped: false };
463
- const lo = (v, from, to) => Math.min(Math.max(v, from), Math.max(from, to));
464
- const xs = [inside.x, area.x, area.x + area.width - r.width];
465
- const ys = [inside.y, area.y, area.y + area.height - r.height];
466
- for (const { at } of near) {
467
- xs.push(at.x - r.width, at.x + at.width);
468
- ys.push(at.y - r.height, at.y + at.height);
469
- }
470
- const places = (list, from, size, extent) => [...new Set(list.map((v) => lo(v, from, from + extent - size)))];
471
- const px = places(xs, area.x, r.width, area.width);
472
- const py = places(ys, area.y, r.height, area.height);
473
- const ylo = area.y;
474
- const yhi = Math.max(area.y, area.y + area.height - r.height);
475
- const byX = [...px].sort((a, b) => Math.abs(a - inside.x) - Math.abs(b - inside.x) || a - b);
476
- let clear = null;
477
- let work = 0;
478
- let searchCapped = false;
479
- for (const x of byX) {
480
- const dx = Math.abs(x - inside.x);
481
- if (clear && dx >= clear.far) break;
482
- work += near.length + 1;
483
- if (work > WORK) {
484
- searchCapped = true;
485
- break;
486
- }
487
- const stretches = [];
488
- for (const { at } of near) {
489
- if (Math.min(x + r.width, at.x + at.width) - Math.max(x, at.x) > EPS2) stretches.push([at.y - r.height, at.y + at.height]);
490
- }
491
- stretches.sort((a, b) => a[0] - b[0]);
492
- let cursor = ylo;
493
- let bestY = null;
494
- const gap = (from, to) => {
495
- if (from > to) return;
496
- const y = Math.min(Math.max(inside.y, from), to);
497
- if (bestY === null || Math.abs(y - inside.y) < Math.abs(bestY - inside.y)) bestY = y;
498
- };
499
- for (const [a, b] of stretches) {
500
- gap(cursor, Math.min(a, yhi));
501
- cursor = Math.max(cursor, b);
502
- if (cursor > yhi) break;
503
- }
504
- if (cursor <= yhi) gap(cursor, yhi);
505
- if (bestY === null) continue;
506
- const far = Math.hypot(dx, bestY - inside.y);
507
- if (!clear || far < clear.far) clear = { x, y: bestY, far };
508
- }
509
- if (clear) return { rect: rect(clear.x, clear.y, r.width, r.height), clamped, snapped: clear.far > EPS2, covers: [], searchCapped };
510
- let best = null;
511
- let tried = 0;
512
- for (const p of byDistance(px, py, inside.x, inside.y)) {
513
- if (tried++ >= COVERED_PLACES) break;
514
- const cand = rect(p.x, p.y, r.width, r.height);
515
- const left = near.reduce((s, n) => s + shared(cand, n.at), 0);
516
- if (!best || left < best.left - EPS2) best = { rect: cand, left, far: p.far, hits: under(cand) };
517
- }
518
- const out = best ?? { rect: inside, far: 0, hits: under(inside) };
519
- return { rect: out.rect, clamped, snapped: out.far > EPS2, covers: out.hits, searchCapped };
520
- }
521
- var PLANE = rect(-1e9, -1e9, 2e9, 2e9);
522
- function stackFloatTarget(input) {
523
- const { stage, size } = input;
524
- const avoid = input.avoid ?? [];
525
- const w = Math.max(0, finite3(size.width, 0));
526
- const h = Math.max(0, finite3(size.height, 0));
527
- return {
528
- // Not "float": `./desk` has a target of that id, and a consumer may hold both.
529
- id: `float:${input.id}`,
530
- kind: "float",
531
- priority: 3,
532
- free: true,
533
- hit: PLANE,
534
- label: input.label ?? "Float here",
535
- item: input.id,
536
- make(point, grab) {
537
- const g = grab ?? { x: w / 2, y: h / 2 };
538
- const fit = fitFloat(rect(point.x - g.x, point.y - g.y, w, h), stage, avoid);
539
- return { rect: fit.rect, fit };
540
- }
541
- };
542
- }
543
- var isSize = (n) => typeof n === "number" && Number.isFinite(n) && n > 0;
544
- var isAt = (n) => typeof n === "number" && Number.isFinite(n);
545
- function storeFloats(floats) {
546
- const last = /* @__PURE__ */ new Map();
547
- floats.forEach((f, i) => {
548
- const r = f.rect;
549
- if (isAt(r.x) && isAt(r.y) && isSize(r.width) && isSize(r.height)) last.set(f.id, i);
550
- });
551
- const out = [];
552
- floats.forEach((f, i) => {
553
- if (last.get(f.id) === i) out.push({ id: f.id, x: f.rect.x, y: f.rect.y, width: f.rect.width, height: f.rect.height });
554
- });
555
- return { v: 1, floats: out };
556
- }
557
- var isObject2 = (x) => typeof x === "object" && x !== null && !Array.isArray(x);
558
- var FIELDS = ["id", "x", "y", "width", "height"];
559
- function readFloats(data, known) {
560
- try {
561
- return read2(data, known);
562
- } catch (e) {
563
- return { ok: false, error: `unreadable: ${e instanceof Error ? e.message : typeof e}` };
564
- }
565
- }
566
- function read2(data, known) {
567
- if (!isObject2(data)) return { ok: false, error: "not an object" };
568
- if (data.v !== 1) return { ok: false, error: `unknown version ${versionWords(data.v)} (this reads version 1)` };
569
- if (!Array.isArray(data.floats)) return { ok: false, error: "floats is missing" };
570
- const dropped = [];
571
- const drop = (what) => {
572
- if (!dropped.includes(what)) dropped.push(what);
573
- };
574
- for (const k of Object.keys(data)) if (k !== "v" && k !== "floats") drop(k);
575
- const floats = [];
576
- for (const x of data.floats) {
577
- if (!isObject2(x)) {
578
- drop(`a ${x === null ? "null" : Array.isArray(x) ? "list" : typeof x} where a float goes`);
579
- continue;
580
- }
581
- const id = x.id;
582
- if (typeof id !== "string") {
583
- drop("a float with no id");
584
- continue;
585
- }
586
- if (known && !known.includes(id) || !isAt(x.x) || !isAt(x.y) || !isSize(x.width) || !isSize(x.height)) {
587
- drop(`float:${id}`);
588
- continue;
589
- }
590
- for (const k of Object.keys(x)) {
591
- if (k === "pinned") drop(`pinned:${id}`);
592
- else if (!FIELDS.includes(k)) drop(`float:${id}:${k}`);
593
- }
594
- const before = floats.findIndex((f) => f.id === id);
595
- if (before >= 0) {
596
- floats.splice(before, 1);
597
- drop(`float:${id}`);
598
- }
599
- floats.push({ id, rect: rect(x.x, x.y, x.width, x.height) });
600
- }
601
- return { ok: true, floats, dropped };
602
- }
603
111
  export {
604
112
  DEFAULT_MENU,
605
113
  DEFAULT_RAIL,