@particle-academy/last-word-ui 0.4.0 → 0.5.0

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 CHANGED
@@ -10,6 +10,33 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
10
10
 
11
11
  ## [Unreleased]
12
12
 
13
+ ## [0.5.0] - 2026-11-04
14
+
15
+ ### Added
16
+
17
+ - **Mixed page orientation.** A `pageBreak` carrying `page` changes the page from there on: each page is drawn at its own size and margins, with its own bands, and its text measured at its own width. `margins` merge side by side, every other key is replaced whole, `firstHeader` / `firstFooter` are never inherited and on a break mean the first page after it, and even bands follow the page number. `{section}` counts the stretches and `{sectionpages}` is the page count of the current one, as Word's `SECTION` and `SECTIONPAGES`. One scale serves every page, from the widest.
18
+ - `docStretches()`, `blockStretches()`, `packInput()`, `pageClassIn()`. `paginate()` takes a `stretch` on a break item and `firstEvenPageHeight`; `pageCapacity()` a stretch's first page index.
19
+ - `onLayout` reports `pageSizes` when the document's page changes partway. `pageWidth` and `pageHeight` stay the first page's.
20
+
21
+ ### Changed
22
+
23
+ - The notice no longer fires for a `page` on a top-level break, because that is drawn now. It still fires for what the writers refuse (a nested `page`, a background on a break, page keys straight on a break, a top-level `sections`), in new words. **Consumer action:** none, unless you compared `LAYOUT_NOTICE`'s text.
24
+ - `tests/fixtures/viewer-agreement.json` has a tenth case, recorded in headless Chromium; the first nine did not move.
25
+
26
+ ## [0.4.1] - 2026-11-03
27
+
28
+ **What you must do: nothing.** A document with a `colSpan` of 1000 or less draws
29
+ exactly as it did.
30
+
31
+ ### Security
32
+
33
+ - A table cell's column span had no upper limit here either, so a very small
34
+ document could cost a browser tab far more than its size. `tableGrid()` clamps a
35
+ `colSpan` to 1000 (`MAX_COL_SPAN`, last-word's limit) and no longer spends memory
36
+ on each slot a span covers. The viewer still never refuses a document.
37
+ **Consumer action:** upgrade. If you vendored `doc-viewer` with `fancy-cli`,
38
+ add it again to take the fix: a vendored copy does not update itself.
39
+
13
40
  ## [0.4.0] - 2026-11-03
14
41
 
15
42
  **What you must do: nothing.** A document with one page setup draws exactly as
package/dist/index.cjs CHANGED
@@ -392,28 +392,37 @@ function spacingStyle(p, defaultAfter, defaultBefore = 0) {
392
392
  }
393
393
 
394
394
  // src/table-grid.ts
395
+ var MAX_COL_SPAN = 1e3;
395
396
  var span = (v) => typeof v === "number" && Number.isInteger(v) && v > 1 ? v : 1;
396
397
  function tableGrid(rows) {
397
- const occupied = rows.map(() => /* @__PURE__ */ new Set());
398
398
  const joinPrevious = rows.map(() => false);
399
399
  let colCount = 0;
400
+ let active = [];
401
+ let reach = -1;
400
402
  const out = rows.map((row, r) => {
403
+ joinPrevious[r] = r <= reach;
404
+ active = active.filter((s) => s[0] >= r).sort((a, b) => a[1] - b[1]);
405
+ const above = active.length;
406
+ let j = 0;
401
407
  const placed = [];
402
408
  let col = 0;
403
409
  (Array.isArray(row?.cells) ? row.cells : []).forEach((cell, c) => {
404
- while (occupied[r].has(col)) col++;
405
- const colSpan = span(cell?.colSpan);
410
+ while (j < above && active[j][1] <= col) {
411
+ col = Math.max(col, active[j][2]);
412
+ j++;
413
+ }
414
+ const colSpan = Math.min(MAX_COL_SPAN, span(cell?.colSpan));
406
415
  const rowSpan = Math.min(span(cell?.rowSpan), rows.length - r);
407
- for (let dr = 0; dr < rowSpan; dr++) {
408
- for (let dc = 0; dc < colSpan; dc++) occupied[r + dr].add(col + dc);
409
- if (dr > 0) joinPrevious[r + dr] = true;
416
+ if (rowSpan > 1) {
417
+ active.push([r + rowSpan - 1, col, col + colSpan]);
418
+ reach = Math.max(reach, r + rowSpan - 1);
410
419
  }
411
420
  placed.push({ row: r, cell: c, col, colSpan, rowSpan });
412
421
  col += colSpan;
422
+ colCount = Math.max(colCount, col);
413
423
  });
414
424
  return placed;
415
425
  });
416
- for (const set of occupied) for (const c of set) colCount = Math.max(colCount, c + 1);
417
426
  let headerRows = 0;
418
427
  while (headerRows < rows.length && rows[headerRows]?.header === true) headerRows++;
419
428
  return { colCount: Math.max(1, colCount), rows: out, joinPrevious, headerRows };
@@ -751,8 +760,12 @@ var VARIANT_KEY = {
751
760
  footer: { first: "firstFooter", even: "evenFooter" }
752
761
  };
753
762
  function pageClass(index) {
754
- if (index === 0) return "first";
755
- return index % 2 === 1 ? "even" : "odd";
763
+ return pageClassIn(index, 0);
764
+ }
765
+ function pageClassIn(index, start) {
766
+ const even = index % 2 === 1;
767
+ if (index === start) return even ? "firstEven" : "first";
768
+ return even ? "even" : "odd";
756
769
  }
757
770
  var isBandObject = (v) => !!v && typeof v === "object" && !Array.isArray(v);
758
771
  function slotBand(page, kind, slot) {
@@ -762,12 +775,12 @@ function slotBand(page, kind, slot) {
762
775
  return isBandObject(v) ? v : void 0;
763
776
  }
764
777
  function bandSlot(page, kind, cls) {
765
- if (cls === "first" && slotBand(page, kind, "first") !== void 0) return "first";
766
- if (cls === "even" && slotBand(page, kind, "even") !== void 0) return "even";
778
+ if ((cls === "first" || cls === "firstEven") && slotBand(page, kind, "first") !== void 0) return "first";
779
+ if ((cls === "even" || cls === "firstEven") && slotBand(page, kind, "even") !== void 0) return "even";
767
780
  return "default";
768
781
  }
769
- function pageBand(page, kind, index) {
770
- const band = slotBand(page, kind, bandSlot(page, kind, pageClass(index)));
782
+ function pageBand(page, kind, index, start = 0) {
783
+ const band = slotBand(page, kind, bandSlot(page, kind, pageClassIn(index, start)));
771
784
  return hasBand(band) ? band : null;
772
785
  }
773
786
  function pageBodyBoxes(g, page, heights) {
@@ -776,7 +789,7 @@ function pageBodyBoxes(g, page, heights) {
776
789
  return hasBand(slotBand(page, kind, slot)) ? Math.max(0, heights[kind][slot] || 0) : 0;
777
790
  };
778
791
  const box = (cls) => bodyBox(g, height2("header", cls), height2("footer", cls));
779
- return { first: box("first"), even: box("even"), odd: box("odd") };
792
+ return { first: box("first"), even: box("even"), odd: box("odd"), firstEven: box("firstEven") };
780
793
  }
781
794
 
782
795
  // src/footnotes.ts
@@ -827,32 +840,117 @@ function numberFootnotes(doc) {
827
840
  return { marks, notes };
828
841
  }
829
842
 
843
+ // src/stretches.ts
844
+ var isObject = (v) => !!v && typeof v === "object" && !Array.isArray(v);
845
+ function isPageObject(page) {
846
+ return isObject(page) || Array.isArray(page) && page.length === 0;
847
+ }
848
+ function mergePage(page, change) {
849
+ const out = { ...page ?? {} };
850
+ delete out.firstHeader;
851
+ delete out.firstFooter;
852
+ for (const [key, value] of Object.entries(change)) {
853
+ if (value === void 0 || value === null || key === "background") continue;
854
+ if (key === "margins" && isObject(value) && isObject(out.margins)) {
855
+ const merged = { ...out.margins };
856
+ for (const [side, v] of Object.entries(value)) {
857
+ if (v !== void 0 && v !== null) merged[side] = v;
858
+ }
859
+ out.margins = merged;
860
+ } else {
861
+ out[key] = value;
862
+ }
863
+ }
864
+ return out;
865
+ }
866
+ function docStretches(doc) {
867
+ let page = doc?.page && typeof doc.page === "object" ? doc.page : void 0;
868
+ const make = (breakAt) => ({
869
+ page,
870
+ geometry: pageGeometry({ ...page ? { page } : {}}),
871
+ breakAt
872
+ });
873
+ const out = [make(null)];
874
+ (Array.isArray(doc?.blocks) ? doc.blocks : []).forEach((block, i) => {
875
+ const b = block;
876
+ if (!isObject(b) || b.type !== "pageBreak" || !isPageObject(b.page)) return;
877
+ page = mergePage(page, isObject(b.page) ? b.page : {});
878
+ out.push(make(i));
879
+ });
880
+ return out;
881
+ }
882
+ function blockStretches(stretches, count) {
883
+ const out = [];
884
+ let k = 0;
885
+ for (let i = 0; i < count; i++) {
886
+ while (k + 1 < stretches.length && stretches[k + 1].breakAt <= i) k++;
887
+ out.push(k);
888
+ }
889
+ return out;
890
+ }
891
+ var toPt = (h) => ({
892
+ default: h.default / PX_PER_PT,
893
+ first: h.first / PX_PER_PT,
894
+ even: h.even / PX_PER_PT
895
+ });
896
+ var heightsPt = (h) => ({ header: toPt(h.header), footer: toPt(h.footer) });
897
+ function packInput(stretches, measured) {
898
+ const boxes = stretches.map((s, k) => pageBodyBoxes(s.geometry, s.page, heightsPt(measured.stretchBands[k])));
899
+ const items = measured.items.map((item, i) => {
900
+ const k = stretches.findIndex((s) => s.breakAt === i);
901
+ if (k <= 0) return item;
902
+ const b = boxes[k];
903
+ const stretch = {
904
+ pageHeight: b.odd.height * PX_PER_PT,
905
+ firstPageHeight: b.first.height * PX_PER_PT,
906
+ firstEvenPageHeight: b.firstEven.height * PX_PER_PT,
907
+ evenPageHeight: b.even.height * PX_PER_PT
908
+ };
909
+ return { type: "break", stretch };
910
+ });
911
+ const first = boxes[0];
912
+ return {
913
+ items,
914
+ boxes,
915
+ options: {
916
+ pageHeight: first.odd.height * PX_PER_PT,
917
+ firstPageHeight: first.first.height * PX_PER_PT,
918
+ evenPageHeight: first.even.height * PX_PER_PT,
919
+ footnoteSeparator: measured.footnoteSeparator
920
+ }
921
+ };
922
+ }
923
+
830
924
  // src/layout-notice.ts
831
- var LAYOUT_NOTICE = "Mixed page orientation isn't shown yet. This document asks for different page settings partway through; every page here uses the document's one page setup.";
925
+ var LAYOUT_NOTICE = "Some page settings in this document aren't shown. It sets them somewhere they don't apply: on a page break directly, in a quote or a table cell, or under a sections key.";
832
926
  var NEAR_MISS_KEYS = ["size", "orientation", "margins", "header", "footer", "firstHeader", "firstFooter", "evenHeader", "evenFooter", "background", "border"];
833
- var isObject = (v) => typeof v === "object" && v !== null;
927
+ var isObject2 = (v) => typeof v === "object" && v !== null;
834
928
  function unshownLayout(doc) {
835
929
  const found = [];
836
- if (!isObject(doc)) return found;
930
+ if (!isObject2(doc)) return found;
837
931
  if (doc.sections !== void 0) found.push("/sections");
838
932
  const stack = [{ blocks: doc.blocks, at: "/blocks" }];
839
933
  while (stack.length > 0) {
840
934
  const { blocks, at } = stack.pop();
841
935
  if (!Array.isArray(blocks)) continue;
842
936
  blocks.forEach((block, i) => {
843
- if (!isObject(block)) return;
937
+ if (!isObject2(block)) return;
844
938
  const path = `${at}/${i}`;
845
939
  if (block.type === "pageBreak") {
846
- for (const key of ["page", ...NEAR_MISS_KEYS]) {
940
+ for (const key of NEAR_MISS_KEYS) {
847
941
  if (block[key] !== void 0) found.push(`${path}/${key}`);
848
942
  }
943
+ if (block.page !== void 0) {
944
+ if (at !== "/blocks" || !isPageObject(block.page)) found.push(`${path}/page`);
945
+ else if (isObject2(block.page) && block.page.background !== void 0) found.push(`${path}/page/background`);
946
+ }
849
947
  } else if (block.type === "quote") {
850
948
  stack.push({ blocks: block.blocks, at: `${path}/blocks` });
851
949
  } else if (block.type === "table" && Array.isArray(block.rows)) {
852
950
  block.rows.forEach((row, r) => {
853
- if (!isObject(row) || !Array.isArray(row.cells)) return;
951
+ if (!isObject2(row) || !Array.isArray(row.cells)) return;
854
952
  row.cells.forEach((cell, c) => {
855
- if (isObject(cell)) stack.push({ blocks: cell.blocks, at: `${path}/rows/${r}/cells/${c}/blocks` });
953
+ if (isObject2(cell)) stack.push({ blocks: cell.blocks, at: `${path}/rows/${r}/cells/${c}/blocks` });
856
954
  });
857
955
  });
858
956
  }
@@ -891,7 +989,7 @@ function lineBreakpoints(el) {
891
989
  }
892
990
  return lines.slice(0, -1).map((l) => l[1]).filter((b) => b > 0 && b < total - 0.5);
893
991
  }
894
- function measure(root, blocks) {
992
+ function measure(root, blocks, stretchCount = 1) {
895
993
  const noteHeights = /* @__PURE__ */ new Map();
896
994
  root.querySelectorAll("[data-lw-measure-footnote]").forEach((el) => {
897
995
  noteHeights.set(Number(el.dataset.lwMeasureFootnote), height(el));
@@ -935,17 +1033,23 @@ function measure(root, blocks) {
935
1033
  ...notes.length ? { footnotes: notes } : {}
936
1034
  };
937
1035
  });
938
- const bands = { header: {}, footer: {} };
939
- for (const kind of ["header", "footer"]) {
940
- for (const slot of BAND_SLOTS) {
941
- bands[kind][slot] = height(root.querySelector(`[data-lw-measure-band="${kind}:${slot}"]`));
1036
+ const stretchBands = Array.from({ length: Math.max(1, stretchCount) }, (_, k) => {
1037
+ const bands2 = { header: {}, footer: {} };
1038
+ for (const kind of ["header", "footer"]) {
1039
+ for (const slot of BAND_SLOTS) {
1040
+ const at = k === 0 ? `${kind}:${slot}` : `${k + 1}:${kind}:${slot}`;
1041
+ bands2[kind][slot] = height(root.querySelector(`[data-lw-measure-band="${at}"]`));
1042
+ }
942
1043
  }
943
- }
1044
+ return bands2;
1045
+ });
1046
+ const bands = stretchBands[0];
944
1047
  return {
945
1048
  items,
946
1049
  headerHeight: bands.header.default,
947
1050
  footerHeight: bands.footer.default,
948
1051
  bands,
1052
+ stretchBands,
949
1053
  footnoteSeparator: height(root.querySelector("[data-lw-measure-separator]"))
950
1054
  };
951
1055
  }
@@ -953,13 +1057,20 @@ function measure(root, blocks) {
953
1057
  // src/paginate.ts
954
1058
  var EPS = 1e-6;
955
1059
  var positive = (v) => typeof v === "number" && Number.isFinite(v) && v > 0;
956
- function pageCapacity(options, index) {
1060
+ function pageCapacity(options, index, start = 0) {
957
1061
  const base = Math.max(1, positive(options.pageHeight) ? options.pageHeight : 1);
958
- if (index === 0) return positive(options.firstPageHeight) ? Math.max(1, options.firstPageHeight) : base;
959
- if (index % 2 === 1) return positive(options.evenPageHeight) ? Math.max(1, options.evenPageHeight) : base;
1062
+ const even = index % 2 === 1;
1063
+ if (index === start) {
1064
+ const first = even ? options.firstEvenPageHeight : options.firstPageHeight;
1065
+ if (positive(first)) return Math.max(1, first);
1066
+ }
1067
+ if (even) return positive(options.evenPageHeight) ? Math.max(1, options.evenPageHeight) : base;
960
1068
  return base;
961
1069
  }
962
- function paginate(items, options) {
1070
+ function paginate(items, initial) {
1071
+ let options = initial;
1072
+ let stretchStart = 0;
1073
+ let stretch = 0;
963
1074
  let cap = pageCapacity(options, 0);
964
1075
  const sep = Math.max(0, options.footnoteSeparator ?? 0);
965
1076
  const pages = [];
@@ -967,8 +1078,8 @@ function paginate(items, options) {
967
1078
  let content = 0;
968
1079
  let noteHeights = 0;
969
1080
  const newPage = () => {
970
- cap = pageCapacity(options, pages.length);
971
- page = { index: pages.length, fragments: [], footnotes: [], used: 0, capacity: cap };
1081
+ cap = pageCapacity(options, pages.length, stretchStart);
1082
+ page = { index: pages.length, fragments: [], footnotes: [], used: 0, capacity: cap, ...stretch > 0 ? { stretch } : {} };
972
1083
  pages.push(page);
973
1084
  content = 0;
974
1085
  noteHeights = 0;
@@ -990,6 +1101,11 @@ function paginate(items, options) {
990
1101
  newPage();
991
1102
  items.forEach((item, index) => {
992
1103
  if (item.type === "break") {
1104
+ if (item.stretch) {
1105
+ options = { footnoteSeparator: options.footnoteSeparator, ...item.stretch };
1106
+ stretchStart = pages.length;
1107
+ stretch++;
1108
+ }
993
1109
  newPage();
994
1110
  return;
995
1111
  }
@@ -1038,7 +1154,7 @@ function paginate(items, options) {
1038
1154
  const chain = keepChainHeight(index);
1039
1155
  const next = items[index + 1];
1040
1156
  const meaningful = next !== void 0 && next.type !== "break";
1041
- const nextCap = pageCapacity(options, page.index + 1);
1157
+ const nextCap = pageCapacity(options, page.index + 1, stretchStart);
1042
1158
  if (meaningful && chain <= nextCap + EPS && !fits(chain, notesIn(item, 0, height2, true))) newPage();
1043
1159
  }
1044
1160
  let start = 0;
@@ -1272,17 +1388,25 @@ function DocViewer({
1272
1388
  className,
1273
1389
  style
1274
1390
  }) {
1275
- const geometry = react.useMemo(() => pageGeometry(doc), [doc]);
1391
+ const stretches = react.useMemo(() => docStretches(doc), [doc]);
1392
+ const geometry = stretches[0].geometry;
1276
1393
  const numbering = react.useMemo(() => numberFootnotes(doc), [doc]);
1277
1394
  const blocks = react.useMemo(() => Array.isArray(doc?.blocks) ? doc.blocks : [], [doc]);
1278
- const page = doc?.page && typeof doc.page === "object" ? doc.page : void 0;
1395
+ const blockStretch = react.useMemo(() => blockStretches(stretches, blocks.length), [stretches, blocks]);
1396
+ const noteStretch = react.useMemo(() => {
1397
+ const out = /* @__PURE__ */ new Map();
1398
+ let id2 = 0;
1399
+ blocks.forEach((block, i) => {
1400
+ for (let n = numberFootnotes({ blocks: [block] }).notes.length; n > 0; n--) out.set(++id2, blockStretch[i]);
1401
+ });
1402
+ return out;
1403
+ }, [blocks, blockStretch]);
1279
1404
  const nowKey = now instanceof Date ? now.getTime() : now;
1280
1405
  const nowDate = react.useMemo(() => resolveNow(nowKey === void 0 ? void 0 : typeof nowKey === "number" ? new Date(nowKey) : nowKey), [nowKey]);
1281
1406
  const author = doc?.meta && typeof doc.meta === "object" ? doc.meta.author : void 0;
1282
1407
  const title = doc?.title;
1283
1408
  const docFields = react.useMemo(
1284
1409
  () => ({
1285
- section: 1,
1286
1410
  ...typeof author === "string" ? { author } : {},
1287
1411
  ...typeof title === "string" ? { title } : {},
1288
1412
  ...typeof filename === "string" ? { filename } : {},
@@ -1307,6 +1431,7 @@ function DocViewer({
1307
1431
  }),
1308
1432
  [numbering, doc, geometry]
1309
1433
  );
1434
+ const ctxOf = (k) => ({ ...ctx, contentWidth: stretches[k].geometry.contentWidth });
1310
1435
  react.useEffect(() => {
1311
1436
  const state = initialFontState(faces);
1312
1437
  setFont(state);
@@ -1322,22 +1447,12 @@ function DocViewer({
1322
1447
  react.useLayoutEffect(() => {
1323
1448
  const el = measureRef.current;
1324
1449
  if (!el || font === "pending") return;
1325
- const m = measure(el, blocks);
1326
- const toPt = (h) => ({
1327
- default: h.default / PX_PER_PT,
1328
- first: h.first / PX_PER_PT,
1329
- even: h.even / PX_PER_PT
1330
- });
1331
- const boxes = pageBodyBoxes(geometry, page, { header: toPt(m.bands.header), footer: toPt(m.bands.footer) });
1332
- const pages = paginate(m.items, {
1333
- pageHeight: boxes.odd.height * PX_PER_PT,
1334
- firstPageHeight: boxes.first.height * PX_PER_PT,
1335
- evenPageHeight: boxes.even.height * PX_PER_PT,
1336
- footnoteSeparator: m.footnoteSeparator
1337
- });
1450
+ const m = measure(el, blocks, stretches.length);
1451
+ const input = packInput(stretches, m);
1452
+ const pages = paginate(input.items, input.options);
1338
1453
  const heights = m.items.map((it) => it.type === "block" ? it.height : 0);
1339
- setLayout({ pages, boxes, heights });
1340
- }, [blocks, geometry, numbering, fontEpoch, page, docFields, font]);
1454
+ setLayout({ pages, boxes: input.boxes, heights });
1455
+ }, [blocks, stretches, numbering, fontEpoch, docFields, font]);
1341
1456
  react.useEffect(() => {
1342
1457
  const fonts = typeof document !== "undefined" ? document.fonts : void 0;
1343
1458
  let live = true;
@@ -1358,7 +1473,7 @@ function DocViewer({
1358
1473
  ro.observe(el);
1359
1474
  return () => ro.disconnect();
1360
1475
  }, []);
1361
- const pageWpx = geometry.width * PX_PER_PT;
1476
+ const pageWpx = Math.max(...stretches.map((s) => s.geometry.width)) * PX_PER_PT;
1362
1477
  const scale = typeof fixedScale === "number" && fixedScale > 0 ? fixedScale : fit === "none" || !width ? 1 : Math.min(maxScale, width / pageWpx);
1363
1478
  const notShown = react.useMemo(() => unshownLayout(doc), [doc]);
1364
1479
  const pageCount = layout?.pages.length ?? 0;
@@ -1368,12 +1483,13 @@ function DocViewer({
1368
1483
  pages: layout.pages.length,
1369
1484
  pageWidth: geometry.width,
1370
1485
  pageHeight: geometry.height,
1486
+ ...stretches.length > 1 ? { pageSizes: layout.pages.map((p) => ({ width: stretches[p.stretch ?? 0].geometry.width, height: stretches[p.stretch ?? 0].geometry.height })) } : {},
1371
1487
  scale,
1372
1488
  font,
1373
1489
  ...notShown.length > 0 ? { notShown } : {}
1374
1490
  });
1375
1491
  }
1376
- }, [layout, geometry, scale, onLayout, font, notShown]);
1492
+ }, [layout, geometry, stretches, scale, onLayout, font, notShown]);
1377
1493
  const typography2 = typography(doc?.defaultFont, ctx.size);
1378
1494
  const ready = layout !== null && font !== "pending";
1379
1495
  return /* @__PURE__ */ jsxRuntime.jsxs(
@@ -1406,49 +1522,74 @@ function DocViewer({
1406
1522
  width: pt(geometry.contentWidth)
1407
1523
  },
1408
1524
  children: /* @__PURE__ */ jsxRuntime.jsxs(RenderCtx.Provider, { value: ctx, children: [
1409
- ["header", "footer"].flatMap(
1410
- (kind) => BAND_SLOTS.map((slot) => {
1411
- const band = slotBand(page, kind, slot);
1412
- if (!hasBand(band)) return null;
1413
- const n = slot === "even" ? 2 : 1;
1414
- return /* @__PURE__ */ jsxRuntime.jsx("div", { "data-lw-measure-band": `${kind}:${slot}`, children: /* @__PURE__ */ jsxRuntime.jsx(RenderCtx.Provider, { value: { ...ctx, fields: { ...docFields, page: n, pages: n } }, children: /* @__PURE__ */ jsxRuntime.jsx(BandView, { band }) }) }, `${kind}:${slot}`);
1415
- })
1525
+ stretches.flatMap(
1526
+ (stretch, k) => ["header", "footer"].flatMap(
1527
+ (kind) => BAND_SLOTS.map((slot) => {
1528
+ const band = slotBand(stretch.page, kind, slot);
1529
+ if (!hasBand(band)) return null;
1530
+ const n = slot === "even" ? 2 : 1;
1531
+ const at = k === 0 ? `${kind}:${slot}` : `${k + 1}:${kind}:${slot}`;
1532
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { "data-lw-measure-band": at, style: stretches.length > 1 ? { width: pt(stretch.geometry.contentWidth) } : void 0, children: /* @__PURE__ */ jsxRuntime.jsx(RenderCtx.Provider, { value: { ...ctxOf(k), fields: { ...docFields, section: k + 1, page: n, pages: n } }, children: /* @__PURE__ */ jsxRuntime.jsx(BandView, { band }) }) }, at);
1533
+ })
1534
+ )
1416
1535
  ),
1417
- blocks.map((b, i) => /* @__PURE__ */ jsxRuntime.jsx("div", { "data-lw-measure-block": i, style: { display: "flow-root" }, children: /* @__PURE__ */ jsxRuntime.jsx(BlockView, { block: b }) }, i)),
1418
- numbering.notes.map((n) => /* @__PURE__ */ jsxRuntime.jsx("div", { "data-lw-measure-footnote": n.id, children: /* @__PURE__ */ jsxRuntime.jsx(FootnoteView, { id: n.id, footnote: n.footnote }) }, n.id)),
1536
+ blocks.map((b, i) => {
1537
+ const k = blockStretch[i];
1538
+ return /* @__PURE__ */ jsxRuntime.jsx(
1539
+ "div",
1540
+ {
1541
+ "data-lw-measure-block": i,
1542
+ style: { display: "flow-root", ...stretches.length > 1 ? { width: pt(stretches[k].geometry.contentWidth) } : {} },
1543
+ children: /* @__PURE__ */ jsxRuntime.jsx(RenderCtx.Provider, { value: ctxOf(k), children: /* @__PURE__ */ jsxRuntime.jsx(BlockView, { block: b }) })
1544
+ },
1545
+ i
1546
+ );
1547
+ }),
1548
+ numbering.notes.map((n) => {
1549
+ const k = noteStretch.get(n.id) ?? 0;
1550
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { "data-lw-measure-footnote": n.id, style: stretches.length > 1 ? { width: pt(stretches[k].geometry.contentWidth) } : void 0, children: /* @__PURE__ */ jsxRuntime.jsx(RenderCtx.Provider, { value: ctxOf(k), children: /* @__PURE__ */ jsxRuntime.jsx(FootnoteView, { id: n.id, footnote: n.footnote }) }) }, n.id);
1551
+ }),
1419
1552
  numbering.notes.length > 0 && /* @__PURE__ */ jsxRuntime.jsx("div", { "data-lw-measure-separator": "", children: /* @__PURE__ */ jsxRuntime.jsx(FootnoteSeparator, { height: pt(FOOTNOTE_SEPARATOR_PT) }) })
1420
1553
  ] })
1421
1554
  }
1422
1555
  ),
1423
- ready && layout && /* @__PURE__ */ jsxRuntime.jsx("div", { "data-lw-page-stack": "", style: { display: "flex", flexDirection: "column", alignItems: "center", gap }, children: layout.pages.map((p) => /* @__PURE__ */ jsxRuntime.jsx(
1424
- PageView,
1425
- {
1426
- page: p,
1427
- pages: layout.pages.length,
1428
- geometry,
1429
- box: layout.boxes[pageClass(p.index)],
1430
- scale,
1431
- typography: typography2,
1432
- ctx,
1433
- docPage: page,
1434
- docFields,
1435
- numbering,
1436
- blocks,
1437
- heights: layout.heights
1438
- },
1439
- p.index
1440
- )) })
1556
+ ready && layout && /* @__PURE__ */ jsxRuntime.jsx("div", { "data-lw-page-stack": "", style: { display: "flex", flexDirection: "column", alignItems: "center", gap }, children: layout.pages.map((p) => {
1557
+ const k = p.stretch ?? 0;
1558
+ const inStretch = layout.pages.filter((x) => (x.stretch ?? 0) === k);
1559
+ const start = inStretch[0].index;
1560
+ return /* @__PURE__ */ jsxRuntime.jsx(
1561
+ PageView,
1562
+ {
1563
+ page: p,
1564
+ pages: layout.pages.length,
1565
+ section: k + 1,
1566
+ sectionPages: inStretch.length,
1567
+ start,
1568
+ stretch: stretches[k],
1569
+ box: layout.boxes[k][pageClassIn(p.index, start)],
1570
+ scale,
1571
+ typography: typography2,
1572
+ ctx: ctxOf(k),
1573
+ docFields,
1574
+ numbering,
1575
+ blocks,
1576
+ heights: layout.heights
1577
+ },
1578
+ p.index
1579
+ );
1580
+ }) })
1441
1581
  ]
1442
1582
  }
1443
1583
  );
1444
1584
  }
1445
- function PageView({ page, pages, geometry, box, scale, typography: typography2, ctx, docPage, docFields, numbering, blocks, heights }) {
1585
+ function PageView({ page, pages, section, sectionPages, start, stretch, box, scale, typography: typography2, ctx, docFields, numbering, blocks, heights }) {
1446
1586
  const n = page.index + 1;
1447
- const g = geometry;
1448
- const bandCtx = { ...ctx, fields: { ...docFields, page: n, pages, sectionPages: pages } };
1449
- const cls = pageClass(page.index);
1450
- const header = pageBand(docPage, "header", page.index);
1451
- const footer = pageBand(docPage, "footer", page.index);
1587
+ const g = stretch.geometry;
1588
+ const docPage = stretch.page;
1589
+ const bandCtx = { ...ctx, fields: { ...docFields, page: n, pages, section, sectionPages } };
1590
+ const cls = pageClassIn(page.index, start);
1591
+ const header = pageBand(docPage, "header", page.index, start);
1592
+ const footer = pageBand(docPage, "footer", page.index, start);
1452
1593
  const variant = (kind) => bandSlot(docPage, kind, cls);
1453
1594
  const notes = page.footnotes.map((fid) => numbering.notes.find((x) => x.id === fid)).filter((x) => !!x);
1454
1595
  return /* @__PURE__ */ jsxRuntime.jsx(
@@ -1578,16 +1719,20 @@ exports.PAGE_MARGIN_DEFAULT = PAGE_MARGIN_DEFAULT;
1578
1719
  exports.PAGE_SIZES_PT = PAGE_SIZES_PT;
1579
1720
  exports.PX_PER_PT = PX_PER_PT;
1580
1721
  exports.bandSlot = bandSlot;
1722
+ exports.blockStretches = blockStretches;
1581
1723
  exports.bodyBox = bodyBox;
1582
1724
  exports.carlitoFaces = carlitoFaces;
1583
1725
  exports.docFromReader = docFromReader;
1726
+ exports.docStretches = docStretches;
1584
1727
  exports.fontFaceCss = fontFaceCss;
1585
1728
  exports.formatDatePicture = formatDatePicture;
1586
1729
  exports.numberFootnotes = numberFootnotes;
1730
+ exports.packInput = packInput;
1587
1731
  exports.pageBand = pageBand;
1588
1732
  exports.pageBodyBoxes = pageBodyBoxes;
1589
1733
  exports.pageCapacity = pageCapacity;
1590
1734
  exports.pageClass = pageClass;
1735
+ exports.pageClassIn = pageClassIn;
1591
1736
  exports.pageGeometry = pageGeometry;
1592
1737
  exports.pageMargins = pageMargins;
1593
1738
  exports.paginate = paginate;