@n-uf/placement 26.11.12 → 26.11.14
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/CHANGELOG.md +81 -0
- package/README.md +142 -14
- package/dist/stack.d.ts +203 -9
- package/dist/stack.js +377 -31
- package/dist/stack.js.map +1 -1
- package/package.json +1 -1
package/dist/stack.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
+
clampInto,
|
|
2
3
|
rect
|
|
3
4
|
} from "./chunk-QMTNB2C3.js";
|
|
4
5
|
|
|
@@ -12,6 +13,10 @@ function limitsOf(input) {
|
|
|
12
13
|
return { min, max: Math.max(min, max), rail: Math.max(0, finite(input.cross.rail ?? DEFAULT_RAIL, DEFAULT_RAIL)) };
|
|
13
14
|
}
|
|
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
|
+
}
|
|
15
20
|
var formSetsCross = (f) => f.setsCross ?? f.kind === "open";
|
|
16
21
|
function withItem(input, item, at) {
|
|
17
22
|
const k = Math.min(Math.max(0, Math.floor(at)), input.items.length);
|
|
@@ -24,38 +29,66 @@ var isMenu = (f) => f.kind === "menu";
|
|
|
24
29
|
var finite2 = (x, fallback = 0) => Number.isFinite(x) ? x : fallback;
|
|
25
30
|
var mainOf = (f, which) => isMenu(f) ? 0 : Math.max(0, finite2(f.main[which]));
|
|
26
31
|
var crossMin = (f) => finite2(f.cross.min ?? f.cross.natural);
|
|
27
|
-
|
|
32
|
+
var rankOf = (f) => finite2(f.rank ?? 0);
|
|
33
|
+
function victim(items, to) {
|
|
28
34
|
let best = -1;
|
|
29
35
|
for (let i = 0; i < items.length; i++) {
|
|
30
|
-
if (
|
|
36
|
+
if (to[i] < 0) continue;
|
|
31
37
|
if (best < 0) {
|
|
32
38
|
best = i;
|
|
33
39
|
continue;
|
|
34
40
|
}
|
|
35
41
|
const a = items[i];
|
|
36
42
|
const b = items[best];
|
|
43
|
+
const ra = rankOf(a.forms[to[i]]);
|
|
44
|
+
const rb = rankOf(b.forms[to[best]]);
|
|
37
45
|
const ka = a.kept ? 1 : 0;
|
|
38
46
|
const kb = b.kept ? 1 : 0;
|
|
39
|
-
if (ka !== kb ? ka < kb : a.used !== b.used ? a.used < b.used : true) best = i;
|
|
47
|
+
if (ra !== rb ? ra < rb : ka !== kb ? ka < kb : a.used !== b.used ? a.used < b.used : true) best = i;
|
|
40
48
|
}
|
|
41
49
|
return best;
|
|
42
50
|
}
|
|
43
|
-
function fitStack(input) {
|
|
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) {
|
|
44
64
|
const length = Math.max(0, finite2(input.length));
|
|
45
65
|
const lim = limitsOf(input);
|
|
46
66
|
const items = input.items.filter((it) => it.forms.length > 0);
|
|
47
67
|
const start = items.map((it) => Math.min(Math.max(0, Math.floor(finite2(it.from ?? 0))), it.forms.length - 1));
|
|
48
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
|
+
}
|
|
49
80
|
const form = (i) => items[i].forms[at[i]];
|
|
50
81
|
const idx = items.map((_, i) => i);
|
|
51
82
|
const firstStep = [];
|
|
52
83
|
const menuOrder = [];
|
|
53
|
-
const step = (i) => {
|
|
84
|
+
const step = (i, j) => {
|
|
54
85
|
if (!firstStep.includes(i)) firstStep.push(i);
|
|
55
|
-
at[i]
|
|
86
|
+
at[i] = j;
|
|
56
87
|
if (isMenu(form(i)) && !menuOrder.includes(i)) menuOrder.push(i);
|
|
57
88
|
};
|
|
58
|
-
|
|
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);
|
|
59
92
|
const crossOf = () => {
|
|
60
93
|
const setters = idx.filter((i) => !isMenu(form(i)) && formSetsCross(form(i)));
|
|
61
94
|
if (setters.length === 0) return lim.rail;
|
|
@@ -63,40 +96,130 @@ function fitStack(input) {
|
|
|
63
96
|
return Math.min(Math.max(want, lim.min), lim.max);
|
|
64
97
|
};
|
|
65
98
|
const inMenu = () => idx.some((i) => isMenu(form(i)));
|
|
66
|
-
const
|
|
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
|
+
};
|
|
67
135
|
const canStep = (i) => at[i] + 1 < items[i].forms.length;
|
|
68
|
-
const tooWide = (i, cross2) =>
|
|
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
|
+
}
|
|
69
185
|
let cross = crossOf();
|
|
70
186
|
let overflow = 0;
|
|
71
|
-
const guard = items.reduce((s, it) => s + it.forms.length, 0) +
|
|
187
|
+
const guard = 2 * items.reduce((s, it) => s + it.forms.length, 0) + 2;
|
|
72
188
|
for (let n = 0; n <= guard; n++) {
|
|
73
189
|
cross = crossOf();
|
|
74
190
|
let stepped = false;
|
|
75
191
|
for (const i of idx) {
|
|
76
192
|
if (tooWide(i, cross) && canStep(i) && !isMenu(items[i].forms[at[i] + 1])) {
|
|
77
|
-
step(i);
|
|
193
|
+
step(i, at[i] + 1);
|
|
78
194
|
stepped = true;
|
|
79
195
|
}
|
|
80
196
|
}
|
|
81
197
|
if (stepped) continue;
|
|
82
198
|
const over = minSum() - length;
|
|
83
199
|
if (over <= EPS) break;
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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
|
+
}
|
|
88
212
|
}
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
overflow = over;
|
|
213
|
+
const w = v >= 0 && to[v] >= 0 ? v : victim(items, to);
|
|
214
|
+
if (w < 0) {
|
|
215
|
+
overflow = items.length > 0 ? over : 0;
|
|
93
216
|
break;
|
|
94
217
|
}
|
|
95
|
-
step(
|
|
218
|
+
step(w, to[w]);
|
|
96
219
|
}
|
|
97
220
|
cross = crossOf();
|
|
98
221
|
const menuSize = inMenu() ? menuMain(input) : 0;
|
|
99
|
-
const avail = Math.max(0, length - menuSize);
|
|
222
|
+
const avail = Math.max(0, length - sp.start - sp.end - gapsOf() - menuSize);
|
|
100
223
|
const drawn = idx.filter((i) => !isMenu(form(i)));
|
|
101
224
|
const min = (i) => mainOf(form(i), "min");
|
|
102
225
|
const nat = (i) => Math.max(min(i), mainOf(form(i), "natural"));
|
|
@@ -117,13 +240,13 @@ function fitStack(input) {
|
|
|
117
240
|
}
|
|
118
241
|
const rectAt = (from, main) => input.axis === "column" ? { x: 0, y: from, width: cross, height: main } : { x: from, y: 0, width: main, height: cross };
|
|
119
242
|
const rects = /* @__PURE__ */ new Map();
|
|
120
|
-
let cursor =
|
|
243
|
+
let cursor = sp.start;
|
|
121
244
|
for (const i of drawn) {
|
|
122
245
|
const s = size.get(i);
|
|
123
246
|
rects.set(i, rectAt(cursor, s));
|
|
124
|
-
cursor += s;
|
|
247
|
+
cursor += s + sp.gap;
|
|
125
248
|
}
|
|
126
|
-
const menuRect = rectAt(Math.max(cursor, length - menuSize), menuSize);
|
|
249
|
+
const menuRect = rectAt(Math.max(drawn.length > 0 ? cursor : sp.start, length - sp.end - menuSize), menuSize);
|
|
127
250
|
const inside = idx.filter((i) => isMenu(form(i)));
|
|
128
251
|
const menu = inside.length > 0 ? { rect: menuRect, items: inside.map((i) => items[i].id) } : null;
|
|
129
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) }));
|
|
@@ -133,11 +256,11 @@ function fitStack(input) {
|
|
|
133
256
|
for (const i of menuOrder) if (isMenu(form(i))) notes.push({ code: "to-menu", id: items[i].id });
|
|
134
257
|
for (const i of idx) if (tooWide(i, cross)) notes.push({ code: "too-wide", id: items[i].id });
|
|
135
258
|
if (overflow > EPS) notes.push({ code: "overflow", over: overflow });
|
|
136
|
-
return { axis: input.axis, length, cross, items: placed, menu, notes };
|
|
259
|
+
return { axis: input.axis, length, cross, items: placed, menu, notes, need: minSum() };
|
|
137
260
|
}
|
|
138
261
|
|
|
139
262
|
// src/stack/targets.ts
|
|
140
|
-
function bounds(input, fit) {
|
|
263
|
+
function bounds(input, fit, reach) {
|
|
141
264
|
const column = input.axis === "column";
|
|
142
265
|
const start = (r) => column ? r.y : r.x;
|
|
143
266
|
const size = (r) => column ? r.height : r.width;
|
|
@@ -155,20 +278,24 @@ function bounds(input, fit) {
|
|
|
155
278
|
}
|
|
156
279
|
b.push(fit.length);
|
|
157
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;
|
|
158
283
|
return b;
|
|
159
284
|
}
|
|
160
|
-
function stackTargets(input, dragged) {
|
|
285
|
+
function stackTargets(input, dragged, options = {}) {
|
|
161
286
|
if (dragged.forms.length === 0) return [];
|
|
162
287
|
const rest = { ...input, items: input.items.filter((it) => it.id !== dragged.id) };
|
|
163
|
-
const
|
|
164
|
-
const
|
|
288
|
+
const fitOptions = options.previous ? { previous: options.previous } : {};
|
|
289
|
+
const base = fitStack(rest, fitOptions);
|
|
290
|
+
const reach = Math.max(0, Number.isFinite(options.endReach) ? options.endReach : 0);
|
|
291
|
+
const b = bounds(rest, base, reach);
|
|
165
292
|
const column = rest.axis === "column";
|
|
166
293
|
const newest = rest.items.reduce((m, it) => Math.max(m, it.used), dragged.used) + 1;
|
|
167
294
|
const item = { ...dragged, used: newest };
|
|
168
295
|
const out = [];
|
|
169
296
|
for (let k = 0; k <= rest.items.length; k++) {
|
|
170
297
|
const solved = withItem(rest, item, k);
|
|
171
|
-
const fit = fitStack(solved);
|
|
298
|
+
const fit = fitStack(solved, fitOptions);
|
|
172
299
|
const slot = fit.items.find((p) => p.id === dragged.id).rect;
|
|
173
300
|
const from = b[k];
|
|
174
301
|
const to = b[k + 1];
|
|
@@ -180,7 +307,8 @@ function stackTargets(input, dragged) {
|
|
|
180
307
|
place: { kind: "gap", at: k },
|
|
181
308
|
hit: column ? rect(0, from, across, to - from) : rect(from, 0, to - from, across),
|
|
182
309
|
slot,
|
|
183
|
-
|
|
310
|
+
// The line spans the stack's cross size (the drop's fit: the bar's height, the column's width).
|
|
311
|
+
marker: column ? rect(0, slot.y, fit.cross, 0) : rect(slot.x, 0, 0, fit.cross),
|
|
184
312
|
input: solved,
|
|
185
313
|
fit
|
|
186
314
|
});
|
|
@@ -258,14 +386,232 @@ function read(data, known) {
|
|
|
258
386
|
}
|
|
259
387
|
return { ok: true, state: { order, kept, closed, used, forms }, dropped };
|
|
260
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
|
+
}
|
|
261
603
|
export {
|
|
262
604
|
DEFAULT_MENU,
|
|
263
605
|
DEFAULT_RAIL,
|
|
264
606
|
createStack,
|
|
607
|
+
fitFloat,
|
|
265
608
|
fitStack,
|
|
266
609
|
formSetsCross,
|
|
610
|
+
readFloats,
|
|
267
611
|
readStack,
|
|
612
|
+
stackFloatTarget,
|
|
268
613
|
stackTargets,
|
|
614
|
+
storeFloats,
|
|
269
615
|
storeStack,
|
|
270
616
|
withItem
|
|
271
617
|
};
|