@n-uf/placement 26.11.54 → 26.11.56

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
@@ -50,6 +50,11 @@ function cutEdges(stage, bands) {
50
50
  }
51
51
 
52
52
  // src/stack/model.ts
53
+ var itemSteps = (item) => item.fixed !== true || item.stepping === true;
54
+ var formMenu = (f) => f.menu ?? f.kind === "menu";
55
+ var formOwnCross = (f) => f.ownCross ?? GROUP_KINDS.has(f.kind);
56
+ var formStepGroup = (f) => f.stepGroup ?? f.kind;
57
+ var GROUP_KINDS = /* @__PURE__ */ new Set(["row", "stacked", "wrap", "icons", "fold"]);
53
58
  var nonNeg = (x) => typeof x === "number" && Number.isFinite(x) ? Math.max(0, x) : 0;
54
59
  function formCrossMin(f) {
55
60
  if (f.wrap) return wrapCross(f.wrap).min;
@@ -57,7 +62,7 @@ function formCrossMin(f) {
57
62
  return Number.isFinite(v) ? v : 0;
58
63
  }
59
64
  function formMain(f, which, cross) {
60
- if (f.kind === "menu") return 0;
65
+ if (formMenu(f)) return 0;
61
66
  if (f.wrap) return wrapRows(f.wrap, cross).rows * nonNeg(f.wrap.row);
62
67
  return nonNeg(f.main[which]);
63
68
  }
@@ -145,14 +150,18 @@ var formBody = (f) => f.body ?? f.kind === "open";
145
150
  var formFolded = (f) => f.folded ?? (f.kind === "header" || f.kind === "fold");
146
151
  var formCompact = (f) => f.compact ?? f.kind === "icons";
147
152
  var formSetsCross = (f) => f.setsCross ?? formBody(f);
153
+ function askedLength(item, form, axis) {
154
+ const v = item.lengths?.[axis];
155
+ if (v === void 0 || !Number.isFinite(v) || v <= EPS) return void 0;
156
+ return !formMenu(form) && form.main.grows === true ? v : void 0;
157
+ }
148
158
  function withItem(input, item, at) {
149
159
  const k = Math.min(Math.max(0, Math.floor(at)), input.items.length);
150
160
  return { ...input, items: [...input.items.slice(0, k), item, ...input.items.slice(k)] };
151
161
  }
152
162
 
153
163
  // src/stack/layout.ts
154
- var isMenu = (f) => f.kind === "menu";
155
- var GROUP_KINDS = /* @__PURE__ */ new Set(["row", "stacked", "wrap", "icons", "fold"]);
164
+ var isMenu = formMenu;
156
165
  var finite2 = (x, fallback = 0) => Number.isFinite(x) ? x : fallback;
157
166
  var mainOf = formMain;
158
167
  var crossMin = formCrossMin;
@@ -161,7 +170,7 @@ var rankOf = (f) => finite2(f.rank ?? 0);
161
170
  function victim(items, to) {
162
171
  let best = -1;
163
172
  for (let i = 0; i < items.length; i++) {
164
- if (to[i] < 0 || items[i].fixed) continue;
173
+ if (to[i] < 0 || !itemSteps(items[i])) continue;
165
174
  if (best < 0) {
166
175
  best = i;
167
176
  continue;
@@ -205,7 +214,7 @@ function solve(input, previous, seed, grow = false) {
205
214
  for (const n of previous.notes) if (n.code === "form-down" && !began.has(n.id)) began.set(n.id, n.from);
206
215
  items.forEach((it, i) => {
207
216
  const p = had.get(it.id);
208
- if (it.fixed || !p || p.form < start[i] || p.form >= it.forms.length || start[i] < (began.get(it.id) ?? p.form)) return;
217
+ if (!itemSteps(it) || !p || p.form < start[i] || p.form >= it.forms.length || start[i] < (began.get(it.id) ?? p.form)) return;
209
218
  const f = it.forms[p.form];
210
219
  if (f.kind === p.kind && offered(f)) at[i] = p.form;
211
220
  });
@@ -258,14 +267,14 @@ function solve(input, previous, seed, grow = false) {
258
267
  const slots = idx.filter((i) => !isMenu(form(i))).length + (inMenu() ? 1 : 0);
259
268
  return slots > 1 ? (slots - 1) * sp.gap : 0;
260
269
  };
261
- const holds = (i) => items[i].fixed === true && form(i).main.grows === true;
270
+ const holds = (i) => !itemSteps(items[i]) && form(i).main.grows === true;
262
271
  let hold = idx.some(holds);
263
272
  const minSumWith = (held) => {
264
273
  const c = items.some((it) => it.forms.some((f) => f.wrap)) ? crossOf() : 0;
265
274
  return idx.reduce((s, i) => s + mainOf(form(i), held && holds(i) ? "natural" : "min", c), 0) + (inMenu() ? menuMain(input) : 0) + gapsOf() + sp.start + sp.end;
266
275
  };
267
276
  const minSum = () => minSumWith(hold);
268
- const sameKind = (i, j) => items[i].forms[j].kind === form(i).kind;
277
+ const sameKind = (i, j) => formStepGroup(items[i].forms[j]) === formStepGroup(form(i));
269
278
  const stepTo = (i) => {
270
279
  const from = at[i];
271
280
  const base = minSum();
@@ -325,7 +334,7 @@ function solve(input, previous, seed, grow = false) {
325
334
  const changed = [...new Set(group.map((s) => index.get(s.id)))];
326
335
  const was = changed.map((i) => at[i]);
327
336
  const forms = (s) => items[index.get(s.id)].forms;
328
- hold = group.every((s) => forms(s)[s.from].kind === forms(s)[s.to].kind) && idx.some(holds);
337
+ hold = group.every((s) => formStepGroup(forms(s)[s.from]) === formStepGroup(forms(s)[s.to])) && idx.some(holds);
329
338
  for (const s of [...group].reverse()) at[index.get(s.id)] = s.from;
330
339
  if (minSum() <= length + EPS && crossOf() === c0 && changed.every((j) => !tooWide(j, c0))) return true;
331
340
  changed.forEach((i, k) => at[i] = was[k]);
@@ -352,7 +361,7 @@ function solve(input, previous, seed, grow = false) {
352
361
  if (stuck) break;
353
362
  const c0 = crossOf();
354
363
  const fitsWith = (changed) => minSum() <= length + EPS && crossOf() === c0 && changed.every((j) => !tooWide(j, c0));
355
- const queue = idx.filter((i) => !items[i].fixed && at[i] > start[i] && !withHistory.has(items[i].id)).sort((a, b) => comesBefore(a, b) ? -1 : 1);
364
+ const queue = idx.filter((i) => itemSteps(items[i]) && at[i] > start[i] && !withHistory.has(items[i].id)).sort((a, b) => comesBefore(a, b) ? -1 : 1);
356
365
  const next = queue[0];
357
366
  if (next === void 0) break;
358
367
  const cur = at[next];
@@ -393,7 +402,7 @@ function solve(input, previous, seed, grow = false) {
393
402
  cross = crossOf();
394
403
  let stepped = false;
395
404
  for (const i of idx) {
396
- if (!items[i].fixed && tooWide(i, cross) && canStep(i) && !isMenu(items[i].forms[nextOffered(i)])) {
405
+ if (itemSteps(items[i]) && tooWide(i, cross) && canStep(i) && !isMenu(items[i].forms[nextOffered(i)])) {
397
406
  step(i, nextOffered(i));
398
407
  stepped = true;
399
408
  }
@@ -401,7 +410,7 @@ function solve(input, previous, seed, grow = false) {
401
410
  if (stepped) continue;
402
411
  const over = minSum() - length;
403
412
  if (over <= EPS) break;
404
- const to = idx.map((i) => items[i].fixed ? -1 : stepTo(i));
413
+ const to = idx.map((i) => itemSteps(items[i]) ? stepTo(i) : -1);
405
414
  if (hold) {
406
415
  const variant = victim(
407
416
  items,
@@ -411,7 +420,7 @@ function solve(input, previous, seed, grow = false) {
411
420
  else hold = false;
412
421
  continue;
413
422
  }
414
- const pool = idx.map((i) => to[i] < 0 && !inMenu() && !items[i].fixed ? menuFormAfter(i) : -1);
423
+ const pool = idx.map((i) => to[i] < 0 && !inMenu() && itemSteps(items[i]) ? menuFormAfter(i) : -1);
415
424
  const v = victim(
416
425
  items,
417
426
  idx.map((i) => to[i] >= 0 ? to[i] : pool[i])
@@ -436,26 +445,52 @@ function solve(input, previous, seed, grow = false) {
436
445
  const avail = Math.max(0, length - sp.start - sp.end - gapsOf() - menuSize);
437
446
  const drawn = idx.filter((i) => !isMenu(form(i)));
438
447
  const min = (i) => mainOf(form(i), hold && holds(i) ? "natural" : "min", cross);
439
- const nat = (i) => Math.max(min(i), mainOf(form(i), "natural", cross));
448
+ const nat = (i) => Math.max(min(i), askedLength(items[i], form(i), input.axis) ?? mainOf(form(i), "natural", cross));
440
449
  const sumMin = drawn.reduce((s, i) => s + min(i), 0);
441
450
  const sumNat = drawn.reduce((s, i) => s + nat(i), 0);
442
451
  const size = /* @__PURE__ */ new Map();
452
+ let tight = 1;
453
+ let scale = 1;
443
454
  if (sumNat <= avail + EPS) {
444
- const growers = drawn.filter((i) => form(i).main.grows);
455
+ const growers2 = drawn.filter((i) => form(i).main.grows);
445
456
  const spare = Math.max(0, avail - sumNat);
446
- const weight = growers.reduce((s, i) => s + nat(i), 0);
457
+ const weight = growers2.reduce((s, i) => s + nat(i), 0);
458
+ if (weight > EPS) scale = 1 + spare / weight;
447
459
  for (const i of drawn) {
448
- const share = growers.includes(i) ? weight > EPS ? nat(i) / weight : 1 / growers.length : 0;
460
+ const share = growers2.includes(i) ? weight > EPS ? nat(i) / weight : 1 / growers2.length : 0;
449
461
  size.set(i, nat(i) + spare * share);
450
462
  }
463
+ const ask = (i) => askedLength(items[i], form(i), input.axis) ?? nat(i);
464
+ if (weight > EPS && growers2.some((i) => ask(i) < min(i))) {
465
+ const pinned = /* @__PURE__ */ new Set();
466
+ const room = avail - drawn.reduce((s2, i) => growers2.includes(i) ? s2 : s2 + nat(i), 0);
467
+ let s = scale;
468
+ for (; ; ) {
469
+ const free = growers2.filter((i) => !pinned.has(i));
470
+ const weights = free.reduce((sum, i) => sum + ask(i), 0);
471
+ if (weights <= EPS) {
472
+ for (const i of free) pinned.add(i);
473
+ break;
474
+ }
475
+ s = (room - [...pinned].reduce((sum, i) => sum + min(i), 0)) / weights;
476
+ const under = free.filter((i) => ask(i) * s < min(i));
477
+ if (under.length === 0) break;
478
+ for (const i of under) pinned.add(i);
479
+ }
480
+ if (pinned.size < growers2.length) {
481
+ scale = s;
482
+ for (const i of growers2) size.set(i, pinned.has(i) ? min(i) : ask(i) * s);
483
+ }
484
+ }
451
485
  } else {
452
486
  const t = avail > sumMin && sumNat > sumMin ? (avail - sumMin) / (sumNat - sumMin) : 0;
487
+ tight = t;
453
488
  for (const i of drawn) size.set(i, min(i) + (nat(i) - min(i)) * t);
454
489
  }
455
490
  const rectAt = (from, main, across = cross) => input.axis === "column" ? { x: 0, y: from, width: across, height: main } : { x: from, y: 0, width: main, height: across };
456
491
  const acrossOf = (i) => {
457
492
  const f = form(i);
458
- if (input.ownCross !== true || !GROUP_KINDS.has(f.kind)) return cross;
493
+ if (input.ownCross !== true || !formOwnCross(f)) return cross;
459
494
  if (f.wrap) return Math.min(cross, wrapRows(f.wrap, cross).width);
460
495
  return Math.min(cross, Math.max(0, finite2(f.cross.natural, cross)));
461
496
  };
@@ -476,8 +511,12 @@ function solve(input, previous, seed, grow = false) {
476
511
  for (const i of menuOrder) if (isMenu(form(i))) notes.push({ code: "to-menu", id: items[i].id });
477
512
  for (const i of idx) if (tooWide(i, cross)) notes.push({ code: "too-wide", id: items[i].id });
478
513
  if (overflow > EPS) notes.push({ code: "overflow", over: overflow });
479
- return { axis: input.axis, length, cross, items: placed, menu, notes, need: minSumWith(false), steps: hist };
514
+ const fit = { axis: input.axis, length, cross, items: placed, menu, notes, need: minSumWith(false), steps: hist };
515
+ sizes.set(fit, { tight, scale, least: new Map(drawn.map((i) => [items[i].id, min(i)])) });
516
+ return fit;
480
517
  }
518
+ var sizes = /* @__PURE__ */ new WeakMap();
519
+ var sizesOf = (fit) => sizes.get(fit);
481
520
 
482
521
  // src/stack/ladder.ts
483
522
  var nonNeg2 = (x) => typeof x === "number" && Number.isFinite(x) ? Math.max(0, x) : 0;
@@ -524,9 +563,114 @@ function controlLadder(spec) {
524
563
  return { row, ...column ? { column } : {} };
525
564
  }
526
565
 
566
+ // src/stack/divide.ts
567
+ var mainOf2 = (r, axis) => axis === "column" ? r.height : r.width;
568
+ var startOf = (r, axis) => axis === "column" ? r.y : r.x;
569
+ function growers(input, fit) {
570
+ const byId = new Map(input.items.map((it) => [it.id, it]));
571
+ const known = sizesOf(fit);
572
+ const out = [];
573
+ fit.items.forEach((p, at) => {
574
+ const item = byId.get(p.id);
575
+ const f = item?.forms[p.form];
576
+ if (!item || !f || formMenu(f) || f.main.grows !== true) return;
577
+ const least = known?.least.get(p.id) ?? formMain(f, "min", fit.cross);
578
+ out.push({ id: p.id, end: item.fixed !== true && !f.wrap, least, natural: Math.max(least, formMain(f, "natural", fit.cross)), drawn: mainOf2(p.rect, fit.axis), at });
579
+ });
580
+ return out;
581
+ }
582
+ function pair(all, before, after) {
583
+ const i = all.findIndex((g) => g.id === before);
584
+ const next = all[i + 1];
585
+ return i >= 0 && next !== void 0 && next.id === after && all[i].end && next.end ? [all[i], next] : null;
586
+ }
587
+ function written(input, fit, all, to) {
588
+ const known = sizesOf(fit);
589
+ const tight = known?.tight;
590
+ const share = tight !== void 0 && tight > EPS && tight < 1 ? tight : 1;
591
+ const scale = known?.scale ?? 1;
592
+ const byId = new Map(all.map((g) => [g.id, g]));
593
+ const ask = (g, drawn) => scale > 1 + EPS ? drawn / scale : g.least + (drawn - g.least) / share;
594
+ let changed = false;
595
+ const items = input.items.map((it) => {
596
+ const drawn = to.get(it.id);
597
+ if (drawn === void 0) return it;
598
+ const length = Math.max(ask(byId.get(it.id), drawn), 2 * EPS);
599
+ const was = it.lengths?.[input.axis];
600
+ if (was !== void 0 && was > EPS && Math.abs(was - length) <= EPS) return it;
601
+ changed = true;
602
+ return { ...it, lengths: { ...it.lengths, [input.axis]: length } };
603
+ });
604
+ return changed ? { ...input, items } : input;
605
+ }
606
+ function trade(input, before, after, options, to) {
607
+ const fit = fitStack(input, options);
608
+ const all = growers(input, fit);
609
+ const ends = pair(all, before, after);
610
+ if (!ends) return input;
611
+ const [b, a] = ends;
612
+ const first = to(b, a);
613
+ if (Math.abs(first - b.drawn) <= EPS) return input;
614
+ return written(
615
+ input,
616
+ fit,
617
+ all,
618
+ /* @__PURE__ */ new Map([
619
+ [b.id, first],
620
+ [a.id, b.drawn + a.drawn - first]
621
+ ])
622
+ );
623
+ }
624
+ function dividers(input, fit) {
625
+ const all = growers(input, fit);
626
+ const out = [];
627
+ for (let k = 0; k + 1 < all.length; k++) {
628
+ const b = all[k];
629
+ const a = all[k + 1];
630
+ if (!b.end || !a.end) continue;
631
+ let prev = a.at - 1;
632
+ while (prev > b.at && fit.menu?.items.includes(fit.items[prev].id)) prev--;
633
+ const last = fit.items[prev].rect;
634
+ const from = startOf(last, fit.axis) + mainOf2(last, fit.axis);
635
+ const thick = Math.max(0, startOf(fit.items[a.at].rect, fit.axis) - from);
636
+ const across = fit.items[b.at].rect;
637
+ const rect2 = fit.axis === "column" ? { x: across.x, y: from, width: across.width, height: thick } : { x: from, y: across.y, width: thick, height: across.height };
638
+ out.push({ before: b.id, after: a.id, rect: rect2, range: { min: 0 - Math.max(0, b.drawn - b.least), max: Math.max(0, a.drawn - a.least) } });
639
+ }
640
+ return out;
641
+ }
642
+ function resizeBetween(input, before, after, delta, options = {}) {
643
+ if (!Number.isFinite(delta)) return input;
644
+ return trade(input, before, after, options, (b, a) => b.drawn + Math.min(Math.max(delta, 0 - Math.max(0, b.drawn - b.least)), Math.max(0, a.drawn - a.least)));
645
+ }
646
+ function resetBetween(input, before, after, options = {}) {
647
+ return trade(input, before, after, options, (b, a) => {
648
+ const both = b.drawn + a.drawn;
649
+ const natural = b.natural + a.natural;
650
+ const least = b.least + a.least;
651
+ let want;
652
+ if (both >= natural - EPS) want = natural > EPS ? both * b.natural / natural : both / 2;
653
+ else want = b.least + (b.natural - b.least) * (natural - least > EPS ? (both - least) / (natural - least) : 0);
654
+ return Math.min(Math.max(want, b.least), Math.max(b.least, both - a.least));
655
+ });
656
+ }
657
+ function resetLengths(input) {
658
+ let changed = false;
659
+ const items = input.items.map((it) => {
660
+ if (it.lengths?.[input.axis] === void 0) return it;
661
+ changed = true;
662
+ const { [input.axis]: _dropped, ...rest } = it.lengths;
663
+ const { lengths: _lengths, ...bare } = it;
664
+ return Object.keys(rest).length > 0 ? { ...bare, lengths: rest } : bare;
665
+ });
666
+ return changed ? { ...input, items } : input;
667
+ }
668
+
527
669
  // src/stack/gaps.ts
528
670
  function refusalOf(base, fit, dragged, fixed, axis, tooWideBefore) {
529
- if (dragged.kind === "menu") return "to-menu";
671
+ const inMenu = new Set(fit.menu?.items ?? []);
672
+ const wasInMenu = new Set(base.menu?.items ?? []);
673
+ if (inMenu.has(dragged.id)) return "to-menu";
530
674
  for (const n of fit.notes) {
531
675
  if (n.code === "overflow") return "overflow";
532
676
  if (n.code === "too-wide" && !tooWideBefore.has(n.id)) return "too-wide";
@@ -537,10 +681,10 @@ function refusalOf(base, fit, dragged, fixed, axis, tooWideBefore) {
537
681
  for (const p of fit.items) {
538
682
  const item = fixed.get(p.id);
539
683
  const q = was.get(p.id);
540
- if (!item || !q || p.kind === "menu" || q.kind === "menu") continue;
541
- if (p.form !== q.form) return "squeezes-fixed";
684
+ if (!item || !q || inMenu.has(p.id) || wasInMenu.has(q.id)) continue;
685
+ if (p.form !== q.form && !itemSteps(item)) return "squeezes-fixed";
542
686
  if (main(p.rect) < formMain(item.forms[p.form], "min", fit.cross) - 1e-6) return "squeezes-fixed";
543
- if (across(p.rect) < across(q.rect) - 1e-6) return "squeezes-fixed";
687
+ if (p.form === q.form && across(p.rect) < across(q.rect) - 1e-6) return "squeezes-fixed";
544
688
  }
545
689
  return null;
546
690
  }
@@ -622,7 +766,7 @@ function* byDistance(xs, ys, fx, fy) {
622
766
  if (j + 1 < sy.length) q.push([i, j + 1, d(i, j + 1)]);
623
767
  }
624
768
  }
625
- function fitFloat(want, stage, avoid = []) {
769
+ function fitFloat(want, stage, avoid = [], options = {}) {
626
770
  const area = clean(stage, rect(0, 0, 0, 0));
627
771
  const r = clean(want, area);
628
772
  const inside = clampInto(r, area);
@@ -635,6 +779,7 @@ function fitFloat(want, stage, avoid = []) {
635
779
  });
636
780
  const under = (cand) => near.filter((n) => overlaps(cand, n.at, EPS2)).map((n) => n.index);
637
781
  const blocked = (cand) => near.some((n) => overlaps(cand, n.at, EPS2));
782
+ if (options.stay) return { rect: inside, clamped, snapped: false, covers: under(inside), searchCapped: false };
638
783
  if (!blocked(inside)) return { rect: inside, clamped, snapped: false, covers: [], searchCapped: false };
639
784
  const lo = (v, from, to) => Math.min(Math.max(v, from), Math.max(from, to));
640
785
  const xs = [inside.x, area.x, area.x + area.width - r.width];
@@ -699,6 +844,7 @@ export {
699
844
  DEFAULT_RAIL,
700
845
  controlLadder,
701
846
  cutEdges,
847
+ dividers,
702
848
  fitFloat,
703
849
  fitStack,
704
850
  formBody,
@@ -707,7 +853,14 @@ export {
707
853
  formFitsCross,
708
854
  formFolded,
709
855
  formMain,
856
+ formMenu,
857
+ formOwnCross,
710
858
  formSetsCross,
859
+ formStepGroup,
860
+ itemSteps,
861
+ resetBetween,
862
+ resetLengths,
863
+ resizeBetween,
711
864
  stackGaps,
712
865
  withItem,
713
866
  wrapCross,