@particle-academy/last-word-ui 0.4.1 → 0.6.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/dist/index.cjs CHANGED
@@ -53,6 +53,122 @@ function bodyBox(g, headerHeight, footerHeight) {
53
53
  const bottom = Math.max(g.margins.bottom, footerHeight > 0 ? g.margins.footer + footerHeight : 0);
54
54
  return { top, bottom, height: Math.max(1, g.height - top - bottom) };
55
55
  }
56
+ var OFFICE_PALETTE = ["#4472C4", "#ED7D31", "#A5A5A5", "#FFC000", "#5B9BD5", "#70AD47", "#264478", "#9E480E"];
57
+ var isObj = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
58
+ function pageChartOption(option) {
59
+ const o = isObj(option) ? option : {};
60
+ const hasAxes = o.xAxis !== void 0 || o.yAxis !== void 0;
61
+ const legendShown = isObj(o.legend) && o.legend.show !== false;
62
+ return {
63
+ backgroundColor: "transparent",
64
+ color: OFFICE_PALETTE,
65
+ animation: false,
66
+ ...o,
67
+ ...hasAxes ? {
68
+ grid: o.grid ?? {
69
+ top: o.title ? 36 : 16,
70
+ right: 16,
71
+ bottom: 8 + (legendShown ? 28 : 0),
72
+ left: 8,
73
+ outerBoundsMode: "same",
74
+ outerBoundsContain: "all"
75
+ }
76
+ } : {}
77
+ };
78
+ }
79
+ function chartTitle(block) {
80
+ const title = isObj(block.option) ? block.option.title : void 0;
81
+ if (typeof title === "string") return title;
82
+ if (isObj(title) && typeof title.text === "string") return title.text;
83
+ return typeof block.alt === "string" ? block.alt : "";
84
+ }
85
+ var PENDING = { status: "pending" };
86
+ var MISSING = { status: "missing" };
87
+ var peer = PENDING;
88
+ var loading = null;
89
+ var listeners = /* @__PURE__ */ new Set();
90
+ function loadPeer() {
91
+ if (loading) return;
92
+ loading = import('@particle-academy/fancy-echarts').then((ns) => {
93
+ const mod = ns;
94
+ const EChart = mod.EChart;
95
+ if (typeof EChart !== "function" && typeof EChart !== "object") return MISSING;
96
+ mod.registerAll?.();
97
+ return { status: "ready", EChart };
98
+ }).catch(() => MISSING).then((state) => {
99
+ peer = state;
100
+ listeners.forEach((l) => l());
101
+ });
102
+ }
103
+ function subscribe(listener) {
104
+ listeners.add(listener);
105
+ loadPeer();
106
+ return () => listeners.delete(listener);
107
+ }
108
+ var getSnapshot = () => peer;
109
+ var getServerSnapshot = () => PENDING;
110
+ var warned = false;
111
+ function warnMissing() {
112
+ if (warned) return;
113
+ const env = globalThis.process?.env;
114
+ if (env?.NODE_ENV === "production") return;
115
+ warned = true;
116
+ console.warn(
117
+ "[last-word-ui] This document has charts, but @particle-academy/fancy-echarts is not installed, so they draw as placeholders. Install it (npm i @particle-academy/fancy-echarts echarts) to draw them, or pass DocViewer a renderChart prop."
118
+ );
119
+ }
120
+ var PAPER_THEME = {};
121
+ function ChartBox({ block, width, height: height2, measuring, renderChart }) {
122
+ const title = chartTitle(block);
123
+ return /* @__PURE__ */ jsxRuntime.jsx(
124
+ "div",
125
+ {
126
+ "data-lw-chart": "",
127
+ role: measuring ? void 0 : "img",
128
+ "aria-label": measuring || !title ? void 0 : title,
129
+ style: { width, height: height2, overflow: "hidden" },
130
+ children: measuring ? null : /* @__PURE__ */ jsxRuntime.jsx(ChartContent, { block, width, height: height2, title, renderChart })
131
+ }
132
+ );
133
+ }
134
+ function ChartContent({ block, width, height: height2, title, renderChart }) {
135
+ const option = react.useMemo(() => pageChartOption(block.option), [block.option]);
136
+ if (renderChart) {
137
+ const custom = renderChart({ block, option, width, height: height2, title });
138
+ if (custom !== void 0) return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: custom });
139
+ }
140
+ return /* @__PURE__ */ jsxRuntime.jsx(DefaultChart, { option, width, height: height2, title });
141
+ }
142
+ function DefaultChart({ option, width, height: height2, title }) {
143
+ const state = react.useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
144
+ if (state.status === "ready") {
145
+ const { EChart } = state;
146
+ return /* @__PURE__ */ jsxRuntime.jsx(EChart, { option, theme: PAPER_THEME, renderer: "svg", autoResize: false, style: { width, height: height2 } });
147
+ }
148
+ if (state.status === "missing") return /* @__PURE__ */ jsxRuntime.jsx(Placeholder, { width, height: height2, title });
149
+ return null;
150
+ }
151
+ function Placeholder({ width, height: height2, title }) {
152
+ react.useEffect(warnMissing, []);
153
+ return /* @__PURE__ */ jsxRuntime.jsx(
154
+ "div",
155
+ {
156
+ "data-lw-chart-placeholder": "",
157
+ style: {
158
+ width,
159
+ height: height2,
160
+ border: "0.75pt dashed #9CA3AF",
161
+ color: "#6B7280",
162
+ fontSize: "9pt",
163
+ display: "flex",
164
+ alignItems: "center",
165
+ justifyContent: "center",
166
+ boxSizing: "border-box"
167
+ },
168
+ children: title ? `Chart: ${title}` : "Chart"
169
+ }
170
+ );
171
+ }
56
172
 
57
173
  // src/fields.ts
58
174
  var FIELD = /\{(?:(page|pages|section|sectionpages|author|title|filename)|(date|time)(?::([^{}"\\]+))?)\}/g;
@@ -536,7 +652,7 @@ function BlockView({ block }) {
536
652
  case "image":
537
653
  return /* @__PURE__ */ jsxRuntime.jsx(ImageView, { block });
538
654
  case "chart":
539
- return /* @__PURE__ */ jsxRuntime.jsx(ChartPlaceholder, { block });
655
+ return /* @__PURE__ */ jsxRuntime.jsx(ChartView, { block });
540
656
  case "hr":
541
657
  return /* @__PURE__ */ jsxRuntime.jsx("div", { "data-lw-kind": "hr", style: { display: "flow-root", paddingBottom: pt(ctx.spaceAfter) }, children: /* @__PURE__ */ jsxRuntime.jsx("div", { style: { height: pt(ctx.size * SINGLE_LINE), display: "flex", alignItems: "flex-end" }, children: /* @__PURE__ */ jsxRuntime.jsx(reactFancy.Separator, { className: "w-full" }) }) });
542
658
  case "pageBreak":
@@ -588,7 +704,7 @@ function ImageView({ block }) {
588
704
  }
589
705
  ) });
590
706
  }
591
- function ChartPlaceholder({ block }) {
707
+ function ChartView({ block }) {
592
708
  const ctx = react.useContext(RenderCtx);
593
709
  const maxPx = ctx.contentWidth * PX_PER_PT;
594
710
  const width = Math.min(
@@ -596,26 +712,7 @@ function ChartPlaceholder({ block }) {
596
712
  num(block.widthPx, typeof block.width === "number" && block.width > 0 ? maxPx * Math.min(100, block.width) / 100 : maxPx)
597
713
  );
598
714
  const height2 = num(block.heightPx, width * 0.6);
599
- const option = block.option ?? {};
600
- const title = typeof option.title === "string" ? option.title : option.title && typeof option.title.text === "string" ? option.title.text : typeof block.alt === "string" ? block.alt : "";
601
- return /* @__PURE__ */ jsxRuntime.jsx("div", { "data-lw-kind": "chart", style: { display: "flow-root", paddingBottom: pt(ctx.spaceAfter) }, children: /* @__PURE__ */ jsxRuntime.jsx(
602
- "div",
603
- {
604
- "data-lw-chart-placeholder": "",
605
- style: {
606
- width,
607
- height: height2,
608
- border: "0.75pt dashed #9CA3AF",
609
- color: "#6B7280",
610
- fontSize: pt(9),
611
- display: "flex",
612
- alignItems: "center",
613
- justifyContent: "center",
614
- boxSizing: "border-box"
615
- },
616
- children: title ? `Chart: ${title}` : "Chart"
617
- }
618
- ) });
715
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { "data-lw-kind": "chart", style: { display: "flow-root", paddingBottom: pt(ctx.spaceAfter) }, children: /* @__PURE__ */ jsxRuntime.jsx(ChartBox, { block, width, height: height2, measuring: ctx.measuring, renderChart: ctx.renderChart }) });
619
716
  }
620
717
  var DEFAULT_TABLE_BORDER = { style: "single", width: 0.5 };
621
718
  var DEFAULT_CELL_PADDING = { top: 3, right: 5.4, bottom: 3, left: 5.4 };
@@ -760,8 +857,12 @@ var VARIANT_KEY = {
760
857
  footer: { first: "firstFooter", even: "evenFooter" }
761
858
  };
762
859
  function pageClass(index) {
763
- if (index === 0) return "first";
764
- return index % 2 === 1 ? "even" : "odd";
860
+ return pageClassIn(index, 0);
861
+ }
862
+ function pageClassIn(index, start) {
863
+ const even = index % 2 === 1;
864
+ if (index === start) return even ? "firstEven" : "first";
865
+ return even ? "even" : "odd";
765
866
  }
766
867
  var isBandObject = (v) => !!v && typeof v === "object" && !Array.isArray(v);
767
868
  function slotBand(page, kind, slot) {
@@ -771,12 +872,12 @@ function slotBand(page, kind, slot) {
771
872
  return isBandObject(v) ? v : void 0;
772
873
  }
773
874
  function bandSlot(page, kind, cls) {
774
- if (cls === "first" && slotBand(page, kind, "first") !== void 0) return "first";
775
- if (cls === "even" && slotBand(page, kind, "even") !== void 0) return "even";
875
+ if ((cls === "first" || cls === "firstEven") && slotBand(page, kind, "first") !== void 0) return "first";
876
+ if ((cls === "even" || cls === "firstEven") && slotBand(page, kind, "even") !== void 0) return "even";
776
877
  return "default";
777
878
  }
778
- function pageBand(page, kind, index) {
779
- const band = slotBand(page, kind, bandSlot(page, kind, pageClass(index)));
879
+ function pageBand(page, kind, index, start = 0) {
880
+ const band = slotBand(page, kind, bandSlot(page, kind, pageClassIn(index, start)));
780
881
  return hasBand(band) ? band : null;
781
882
  }
782
883
  function pageBodyBoxes(g, page, heights) {
@@ -785,7 +886,7 @@ function pageBodyBoxes(g, page, heights) {
785
886
  return hasBand(slotBand(page, kind, slot)) ? Math.max(0, heights[kind][slot] || 0) : 0;
786
887
  };
787
888
  const box = (cls) => bodyBox(g, height2("header", cls), height2("footer", cls));
788
- return { first: box("first"), even: box("even"), odd: box("odd") };
889
+ return { first: box("first"), even: box("even"), odd: box("odd"), firstEven: box("firstEven") };
789
890
  }
790
891
 
791
892
  // src/footnotes.ts
@@ -836,32 +937,117 @@ function numberFootnotes(doc) {
836
937
  return { marks, notes };
837
938
  }
838
939
 
940
+ // src/stretches.ts
941
+ var isObject = (v) => !!v && typeof v === "object" && !Array.isArray(v);
942
+ function isPageObject(page) {
943
+ return isObject(page) || Array.isArray(page) && page.length === 0;
944
+ }
945
+ function mergePage(page, change) {
946
+ const out = { ...page ?? {} };
947
+ delete out.firstHeader;
948
+ delete out.firstFooter;
949
+ for (const [key, value] of Object.entries(change)) {
950
+ if (value === void 0 || value === null || key === "background") continue;
951
+ if (key === "margins" && isObject(value) && isObject(out.margins)) {
952
+ const merged = { ...out.margins };
953
+ for (const [side, v] of Object.entries(value)) {
954
+ if (v !== void 0 && v !== null) merged[side] = v;
955
+ }
956
+ out.margins = merged;
957
+ } else {
958
+ out[key] = value;
959
+ }
960
+ }
961
+ return out;
962
+ }
963
+ function docStretches(doc) {
964
+ let page = doc?.page && typeof doc.page === "object" ? doc.page : void 0;
965
+ const make = (breakAt) => ({
966
+ page,
967
+ geometry: pageGeometry({ ...page ? { page } : {}}),
968
+ breakAt
969
+ });
970
+ const out = [make(null)];
971
+ (Array.isArray(doc?.blocks) ? doc.blocks : []).forEach((block, i) => {
972
+ const b = block;
973
+ if (!isObject(b) || b.type !== "pageBreak" || !isPageObject(b.page)) return;
974
+ page = mergePage(page, isObject(b.page) ? b.page : {});
975
+ out.push(make(i));
976
+ });
977
+ return out;
978
+ }
979
+ function blockStretches(stretches, count) {
980
+ const out = [];
981
+ let k = 0;
982
+ for (let i = 0; i < count; i++) {
983
+ while (k + 1 < stretches.length && stretches[k + 1].breakAt <= i) k++;
984
+ out.push(k);
985
+ }
986
+ return out;
987
+ }
988
+ var toPt = (h) => ({
989
+ default: h.default / PX_PER_PT,
990
+ first: h.first / PX_PER_PT,
991
+ even: h.even / PX_PER_PT
992
+ });
993
+ var heightsPt = (h) => ({ header: toPt(h.header), footer: toPt(h.footer) });
994
+ function packInput(stretches, measured) {
995
+ const boxes = stretches.map((s, k) => pageBodyBoxes(s.geometry, s.page, heightsPt(measured.stretchBands[k])));
996
+ const items = measured.items.map((item, i) => {
997
+ const k = stretches.findIndex((s) => s.breakAt === i);
998
+ if (k <= 0) return item;
999
+ const b = boxes[k];
1000
+ const stretch = {
1001
+ pageHeight: b.odd.height * PX_PER_PT,
1002
+ firstPageHeight: b.first.height * PX_PER_PT,
1003
+ firstEvenPageHeight: b.firstEven.height * PX_PER_PT,
1004
+ evenPageHeight: b.even.height * PX_PER_PT
1005
+ };
1006
+ return { type: "break", stretch };
1007
+ });
1008
+ const first = boxes[0];
1009
+ return {
1010
+ items,
1011
+ boxes,
1012
+ options: {
1013
+ pageHeight: first.odd.height * PX_PER_PT,
1014
+ firstPageHeight: first.first.height * PX_PER_PT,
1015
+ evenPageHeight: first.even.height * PX_PER_PT,
1016
+ footnoteSeparator: measured.footnoteSeparator
1017
+ }
1018
+ };
1019
+ }
1020
+
839
1021
  // src/layout-notice.ts
840
- 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.";
1022
+ 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.";
841
1023
  var NEAR_MISS_KEYS = ["size", "orientation", "margins", "header", "footer", "firstHeader", "firstFooter", "evenHeader", "evenFooter", "background", "border"];
842
- var isObject = (v) => typeof v === "object" && v !== null;
1024
+ var isObject2 = (v) => typeof v === "object" && v !== null;
843
1025
  function unshownLayout(doc) {
844
1026
  const found = [];
845
- if (!isObject(doc)) return found;
1027
+ if (!isObject2(doc)) return found;
846
1028
  if (doc.sections !== void 0) found.push("/sections");
847
1029
  const stack = [{ blocks: doc.blocks, at: "/blocks" }];
848
1030
  while (stack.length > 0) {
849
1031
  const { blocks, at } = stack.pop();
850
1032
  if (!Array.isArray(blocks)) continue;
851
1033
  blocks.forEach((block, i) => {
852
- if (!isObject(block)) return;
1034
+ if (!isObject2(block)) return;
853
1035
  const path = `${at}/${i}`;
854
1036
  if (block.type === "pageBreak") {
855
- for (const key of ["page", ...NEAR_MISS_KEYS]) {
1037
+ for (const key of NEAR_MISS_KEYS) {
856
1038
  if (block[key] !== void 0) found.push(`${path}/${key}`);
857
1039
  }
1040
+ if (block.page !== void 0) {
1041
+ if (at !== "/blocks" || !isPageObject(block.page)) found.push(`${path}/page`);
1042
+ else if (isObject2(block.page) && block.page.background !== void 0) found.push(`${path}/page/background`);
1043
+ }
858
1044
  } else if (block.type === "quote") {
859
1045
  stack.push({ blocks: block.blocks, at: `${path}/blocks` });
860
1046
  } else if (block.type === "table" && Array.isArray(block.rows)) {
861
1047
  block.rows.forEach((row, r) => {
862
- if (!isObject(row) || !Array.isArray(row.cells)) return;
1048
+ if (!isObject2(row) || !Array.isArray(row.cells)) return;
863
1049
  row.cells.forEach((cell, c) => {
864
- if (isObject(cell)) stack.push({ blocks: cell.blocks, at: `${path}/rows/${r}/cells/${c}/blocks` });
1050
+ if (isObject2(cell)) stack.push({ blocks: cell.blocks, at: `${path}/rows/${r}/cells/${c}/blocks` });
865
1051
  });
866
1052
  });
867
1053
  }
@@ -900,7 +1086,7 @@ function lineBreakpoints(el) {
900
1086
  }
901
1087
  return lines.slice(0, -1).map((l) => l[1]).filter((b) => b > 0 && b < total - 0.5);
902
1088
  }
903
- function measure(root, blocks) {
1089
+ function measure(root, blocks, stretchCount = 1) {
904
1090
  const noteHeights = /* @__PURE__ */ new Map();
905
1091
  root.querySelectorAll("[data-lw-measure-footnote]").forEach((el) => {
906
1092
  noteHeights.set(Number(el.dataset.lwMeasureFootnote), height(el));
@@ -944,17 +1130,23 @@ function measure(root, blocks) {
944
1130
  ...notes.length ? { footnotes: notes } : {}
945
1131
  };
946
1132
  });
947
- const bands = { header: {}, footer: {} };
948
- for (const kind of ["header", "footer"]) {
949
- for (const slot of BAND_SLOTS) {
950
- bands[kind][slot] = height(root.querySelector(`[data-lw-measure-band="${kind}:${slot}"]`));
1133
+ const stretchBands = Array.from({ length: Math.max(1, stretchCount) }, (_, k) => {
1134
+ const bands2 = { header: {}, footer: {} };
1135
+ for (const kind of ["header", "footer"]) {
1136
+ for (const slot of BAND_SLOTS) {
1137
+ const at = k === 0 ? `${kind}:${slot}` : `${k + 1}:${kind}:${slot}`;
1138
+ bands2[kind][slot] = height(root.querySelector(`[data-lw-measure-band="${at}"]`));
1139
+ }
951
1140
  }
952
- }
1141
+ return bands2;
1142
+ });
1143
+ const bands = stretchBands[0];
953
1144
  return {
954
1145
  items,
955
1146
  headerHeight: bands.header.default,
956
1147
  footerHeight: bands.footer.default,
957
1148
  bands,
1149
+ stretchBands,
958
1150
  footnoteSeparator: height(root.querySelector("[data-lw-measure-separator]"))
959
1151
  };
960
1152
  }
@@ -962,13 +1154,20 @@ function measure(root, blocks) {
962
1154
  // src/paginate.ts
963
1155
  var EPS = 1e-6;
964
1156
  var positive = (v) => typeof v === "number" && Number.isFinite(v) && v > 0;
965
- function pageCapacity(options, index) {
1157
+ function pageCapacity(options, index, start = 0) {
966
1158
  const base = Math.max(1, positive(options.pageHeight) ? options.pageHeight : 1);
967
- if (index === 0) return positive(options.firstPageHeight) ? Math.max(1, options.firstPageHeight) : base;
968
- if (index % 2 === 1) return positive(options.evenPageHeight) ? Math.max(1, options.evenPageHeight) : base;
1159
+ const even = index % 2 === 1;
1160
+ if (index === start) {
1161
+ const first = even ? options.firstEvenPageHeight : options.firstPageHeight;
1162
+ if (positive(first)) return Math.max(1, first);
1163
+ }
1164
+ if (even) return positive(options.evenPageHeight) ? Math.max(1, options.evenPageHeight) : base;
969
1165
  return base;
970
1166
  }
971
- function paginate(items, options) {
1167
+ function paginate(items, initial) {
1168
+ let options = initial;
1169
+ let stretchStart = 0;
1170
+ let stretch = 0;
972
1171
  let cap = pageCapacity(options, 0);
973
1172
  const sep = Math.max(0, options.footnoteSeparator ?? 0);
974
1173
  const pages = [];
@@ -976,8 +1175,8 @@ function paginate(items, options) {
976
1175
  let content = 0;
977
1176
  let noteHeights = 0;
978
1177
  const newPage = () => {
979
- cap = pageCapacity(options, pages.length);
980
- page = { index: pages.length, fragments: [], footnotes: [], used: 0, capacity: cap };
1178
+ cap = pageCapacity(options, pages.length, stretchStart);
1179
+ page = { index: pages.length, fragments: [], footnotes: [], used: 0, capacity: cap, ...stretch > 0 ? { stretch } : {} };
981
1180
  pages.push(page);
982
1181
  content = 0;
983
1182
  noteHeights = 0;
@@ -999,6 +1198,11 @@ function paginate(items, options) {
999
1198
  newPage();
1000
1199
  items.forEach((item, index) => {
1001
1200
  if (item.type === "break") {
1201
+ if (item.stretch) {
1202
+ options = { footnoteSeparator: options.footnoteSeparator, ...item.stretch };
1203
+ stretchStart = pages.length;
1204
+ stretch++;
1205
+ }
1002
1206
  newPage();
1003
1207
  return;
1004
1208
  }
@@ -1047,7 +1251,7 @@ function paginate(items, options) {
1047
1251
  const chain = keepChainHeight(index);
1048
1252
  const next = items[index + 1];
1049
1253
  const meaningful = next !== void 0 && next.type !== "break";
1050
- const nextCap = pageCapacity(options, page.index + 1);
1254
+ const nextCap = pageCapacity(options, page.index + 1, stretchStart);
1051
1255
  if (meaningful && chain <= nextCap + EPS && !fits(chain, notesIn(item, 0, height2, true))) newPage();
1052
1256
  }
1053
1257
  let start = 0;
@@ -1277,21 +1481,30 @@ function DocViewer({
1277
1481
  now,
1278
1482
  onLayout,
1279
1483
  injectStyles = true,
1484
+ renderChart,
1280
1485
  id,
1281
1486
  className,
1282
1487
  style
1283
1488
  }) {
1284
- const geometry = react.useMemo(() => pageGeometry(doc), [doc]);
1489
+ const stretches = react.useMemo(() => docStretches(doc), [doc]);
1490
+ const geometry = stretches[0].geometry;
1285
1491
  const numbering = react.useMemo(() => numberFootnotes(doc), [doc]);
1286
1492
  const blocks = react.useMemo(() => Array.isArray(doc?.blocks) ? doc.blocks : [], [doc]);
1287
- const page = doc?.page && typeof doc.page === "object" ? doc.page : void 0;
1493
+ const blockStretch = react.useMemo(() => blockStretches(stretches, blocks.length), [stretches, blocks]);
1494
+ const noteStretch = react.useMemo(() => {
1495
+ const out = /* @__PURE__ */ new Map();
1496
+ let id2 = 0;
1497
+ blocks.forEach((block, i) => {
1498
+ for (let n = numberFootnotes({ blocks: [block] }).notes.length; n > 0; n--) out.set(++id2, blockStretch[i]);
1499
+ });
1500
+ return out;
1501
+ }, [blocks, blockStretch]);
1288
1502
  const nowKey = now instanceof Date ? now.getTime() : now;
1289
1503
  const nowDate = react.useMemo(() => resolveNow(nowKey === void 0 ? void 0 : typeof nowKey === "number" ? new Date(nowKey) : nowKey), [nowKey]);
1290
1504
  const author = doc?.meta && typeof doc.meta === "object" ? doc.meta.author : void 0;
1291
1505
  const title = doc?.title;
1292
1506
  const docFields = react.useMemo(
1293
1507
  () => ({
1294
- section: 1,
1295
1508
  ...typeof author === "string" ? { author } : {},
1296
1509
  ...typeof title === "string" ? { title } : {},
1297
1510
  ...typeof filename === "string" ? { filename } : {},
@@ -1312,10 +1525,12 @@ function DocViewer({
1312
1525
  marks: numbering.marks,
1313
1526
  size: num(doc?.defaultSize, DEFAULT_SIZE),
1314
1527
  spaceAfter: DEFAULT_SPACE_AFTER,
1315
- contentWidth: geometry.contentWidth
1528
+ contentWidth: geometry.contentWidth,
1529
+ ...renderChart ? { renderChart } : {}
1316
1530
  }),
1317
- [numbering, doc, geometry]
1531
+ [numbering, doc, geometry, renderChart]
1318
1532
  );
1533
+ const ctxOf = (k) => ({ ...ctx, contentWidth: stretches[k].geometry.contentWidth });
1319
1534
  react.useEffect(() => {
1320
1535
  const state = initialFontState(faces);
1321
1536
  setFont(state);
@@ -1331,22 +1546,12 @@ function DocViewer({
1331
1546
  react.useLayoutEffect(() => {
1332
1547
  const el = measureRef.current;
1333
1548
  if (!el || font === "pending") return;
1334
- const m = measure(el, blocks);
1335
- const toPt = (h) => ({
1336
- default: h.default / PX_PER_PT,
1337
- first: h.first / PX_PER_PT,
1338
- even: h.even / PX_PER_PT
1339
- });
1340
- const boxes = pageBodyBoxes(geometry, page, { header: toPt(m.bands.header), footer: toPt(m.bands.footer) });
1341
- const pages = paginate(m.items, {
1342
- pageHeight: boxes.odd.height * PX_PER_PT,
1343
- firstPageHeight: boxes.first.height * PX_PER_PT,
1344
- evenPageHeight: boxes.even.height * PX_PER_PT,
1345
- footnoteSeparator: m.footnoteSeparator
1346
- });
1549
+ const m = measure(el, blocks, stretches.length);
1550
+ const input = packInput(stretches, m);
1551
+ const pages = paginate(input.items, input.options);
1347
1552
  const heights = m.items.map((it) => it.type === "block" ? it.height : 0);
1348
- setLayout({ pages, boxes, heights });
1349
- }, [blocks, geometry, numbering, fontEpoch, page, docFields, font]);
1553
+ setLayout({ pages, boxes: input.boxes, heights });
1554
+ }, [blocks, stretches, numbering, fontEpoch, docFields, font]);
1350
1555
  react.useEffect(() => {
1351
1556
  const fonts = typeof document !== "undefined" ? document.fonts : void 0;
1352
1557
  let live = true;
@@ -1367,7 +1572,7 @@ function DocViewer({
1367
1572
  ro.observe(el);
1368
1573
  return () => ro.disconnect();
1369
1574
  }, []);
1370
- const pageWpx = geometry.width * PX_PER_PT;
1575
+ const pageWpx = Math.max(...stretches.map((s) => s.geometry.width)) * PX_PER_PT;
1371
1576
  const scale = typeof fixedScale === "number" && fixedScale > 0 ? fixedScale : fit === "none" || !width ? 1 : Math.min(maxScale, width / pageWpx);
1372
1577
  const notShown = react.useMemo(() => unshownLayout(doc), [doc]);
1373
1578
  const pageCount = layout?.pages.length ?? 0;
@@ -1377,12 +1582,13 @@ function DocViewer({
1377
1582
  pages: layout.pages.length,
1378
1583
  pageWidth: geometry.width,
1379
1584
  pageHeight: geometry.height,
1585
+ ...stretches.length > 1 ? { pageSizes: layout.pages.map((p) => ({ width: stretches[p.stretch ?? 0].geometry.width, height: stretches[p.stretch ?? 0].geometry.height })) } : {},
1380
1586
  scale,
1381
1587
  font,
1382
1588
  ...notShown.length > 0 ? { notShown } : {}
1383
1589
  });
1384
1590
  }
1385
- }, [layout, geometry, scale, onLayout, font, notShown]);
1591
+ }, [layout, geometry, stretches, scale, onLayout, font, notShown]);
1386
1592
  const typography2 = typography(doc?.defaultFont, ctx.size);
1387
1593
  const ready = layout !== null && font !== "pending";
1388
1594
  return /* @__PURE__ */ jsxRuntime.jsxs(
@@ -1393,6 +1599,7 @@ function DocViewer({
1393
1599
  "data-lw-viewer": "",
1394
1600
  "data-lw-pages": ready && pageCount || void 0,
1395
1601
  "data-lw-font": ready ? font : void 0,
1602
+ "aria-busy": ready ? void 0 : true,
1396
1603
  className,
1397
1604
  style: { position: "relative", ...style },
1398
1605
  children: [
@@ -1415,49 +1622,86 @@ function DocViewer({
1415
1622
  width: pt(geometry.contentWidth)
1416
1623
  },
1417
1624
  children: /* @__PURE__ */ jsxRuntime.jsxs(RenderCtx.Provider, { value: ctx, children: [
1418
- ["header", "footer"].flatMap(
1419
- (kind) => BAND_SLOTS.map((slot) => {
1420
- const band = slotBand(page, kind, slot);
1421
- if (!hasBand(band)) return null;
1422
- const n = slot === "even" ? 2 : 1;
1423
- 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}`);
1424
- })
1625
+ stretches.flatMap(
1626
+ (stretch, k) => ["header", "footer"].flatMap(
1627
+ (kind) => BAND_SLOTS.map((slot) => {
1628
+ const band = slotBand(stretch.page, kind, slot);
1629
+ if (!hasBand(band)) return null;
1630
+ const n = slot === "even" ? 2 : 1;
1631
+ const at = k === 0 ? `${kind}:${slot}` : `${k + 1}:${kind}:${slot}`;
1632
+ 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), measuring: true, fields: { ...docFields, section: k + 1, page: n, pages: n } }, children: /* @__PURE__ */ jsxRuntime.jsx(BandView, { band }) }) }, at);
1633
+ })
1634
+ )
1425
1635
  ),
1426
- 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)),
1427
- 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)),
1636
+ blocks.map((b, i) => {
1637
+ const k = blockStretch[i];
1638
+ return /* @__PURE__ */ jsxRuntime.jsx(
1639
+ "div",
1640
+ {
1641
+ "data-lw-measure-block": i,
1642
+ style: { display: "flow-root", ...stretches.length > 1 ? { width: pt(stretches[k].geometry.contentWidth) } : {} },
1643
+ children: /* @__PURE__ */ jsxRuntime.jsx(RenderCtx.Provider, { value: { ...ctxOf(k), measuring: true }, children: /* @__PURE__ */ jsxRuntime.jsx(BlockView, { block: b }) })
1644
+ },
1645
+ i
1646
+ );
1647
+ }),
1648
+ numbering.notes.map((n) => {
1649
+ const k = noteStretch.get(n.id) ?? 0;
1650
+ 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), measuring: true }, children: /* @__PURE__ */ jsxRuntime.jsx(FootnoteView, { id: n.id, footnote: n.footnote }) }) }, n.id);
1651
+ }),
1428
1652
  numbering.notes.length > 0 && /* @__PURE__ */ jsxRuntime.jsx("div", { "data-lw-measure-separator": "", children: /* @__PURE__ */ jsxRuntime.jsx(FootnoteSeparator, { height: pt(FOOTNOTE_SEPARATOR_PT) }) })
1429
1653
  ] })
1430
1654
  }
1431
1655
  ),
1432
- 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(
1433
- PageView,
1656
+ !ready && /* @__PURE__ */ jsxRuntime.jsx(
1657
+ "div",
1434
1658
  {
1435
- page: p,
1436
- pages: layout.pages.length,
1437
- geometry,
1438
- box: layout.boxes[pageClass(p.index)],
1439
- scale,
1440
- typography: typography2,
1441
- ctx,
1442
- docPage: page,
1443
- docFields,
1444
- numbering,
1445
- blocks,
1446
- heights: layout.heights
1447
- },
1448
- p.index
1449
- )) })
1659
+ "data-lw-pending": "",
1660
+ style: {
1661
+ width: px(stretches[0].geometry.width * PX_PER_PT * scale),
1662
+ maxWidth: "100%",
1663
+ height: px(stretches[0].geometry.height * PX_PER_PT * scale),
1664
+ margin: "0 auto"
1665
+ }
1666
+ }
1667
+ ),
1668
+ ready && layout && /* @__PURE__ */ jsxRuntime.jsx("div", { "data-lw-page-stack": "", style: { display: "flex", flexDirection: "column", alignItems: "center", gap }, children: layout.pages.map((p) => {
1669
+ const k = p.stretch ?? 0;
1670
+ const inStretch = layout.pages.filter((x) => (x.stretch ?? 0) === k);
1671
+ const start = inStretch[0].index;
1672
+ return /* @__PURE__ */ jsxRuntime.jsx(
1673
+ PageView,
1674
+ {
1675
+ page: p,
1676
+ pages: layout.pages.length,
1677
+ section: k + 1,
1678
+ sectionPages: inStretch.length,
1679
+ start,
1680
+ stretch: stretches[k],
1681
+ box: layout.boxes[k][pageClassIn(p.index, start)],
1682
+ scale,
1683
+ typography: typography2,
1684
+ ctx: ctxOf(k),
1685
+ docFields,
1686
+ numbering,
1687
+ blocks,
1688
+ heights: layout.heights
1689
+ },
1690
+ p.index
1691
+ );
1692
+ }) })
1450
1693
  ]
1451
1694
  }
1452
1695
  );
1453
1696
  }
1454
- function PageView({ page, pages, geometry, box, scale, typography: typography2, ctx, docPage, docFields, numbering, blocks, heights }) {
1697
+ function PageView({ page, pages, section, sectionPages, start, stretch, box, scale, typography: typography2, ctx, docFields, numbering, blocks, heights }) {
1455
1698
  const n = page.index + 1;
1456
- const g = geometry;
1457
- const bandCtx = { ...ctx, fields: { ...docFields, page: n, pages, sectionPages: pages } };
1458
- const cls = pageClass(page.index);
1459
- const header = pageBand(docPage, "header", page.index);
1460
- const footer = pageBand(docPage, "footer", page.index);
1699
+ const g = stretch.geometry;
1700
+ const docPage = stretch.page;
1701
+ const bandCtx = { ...ctx, fields: { ...docFields, page: n, pages, section, sectionPages } };
1702
+ const cls = pageClassIn(page.index, start);
1703
+ const header = pageBand(docPage, "header", page.index, start);
1704
+ const footer = pageBand(docPage, "footer", page.index, start);
1461
1705
  const variant = (kind) => bandSlot(docPage, kind, cls);
1462
1706
  const notes = page.footnotes.map((fid) => numbering.notes.find((x) => x.id === fid)).filter((x) => !!x);
1463
1707
  return /* @__PURE__ */ jsxRuntime.jsx(
@@ -1582,21 +1826,27 @@ exports.CARLITO_FAMILY = CARLITO_FAMILY;
1582
1826
  exports.DocViewer = DocViewer;
1583
1827
  exports.FONT_LOAD_TIMEOUT_MS = FONT_LOAD_TIMEOUT_MS;
1584
1828
  exports.LAYOUT_NOTICE = LAYOUT_NOTICE;
1829
+ exports.OFFICE_PALETTE = OFFICE_PALETTE;
1585
1830
  exports.PAGE_BAND_DISTANCE_DEFAULT = PAGE_BAND_DISTANCE_DEFAULT;
1586
1831
  exports.PAGE_MARGIN_DEFAULT = PAGE_MARGIN_DEFAULT;
1587
1832
  exports.PAGE_SIZES_PT = PAGE_SIZES_PT;
1588
1833
  exports.PX_PER_PT = PX_PER_PT;
1589
1834
  exports.bandSlot = bandSlot;
1835
+ exports.blockStretches = blockStretches;
1590
1836
  exports.bodyBox = bodyBox;
1591
1837
  exports.carlitoFaces = carlitoFaces;
1592
1838
  exports.docFromReader = docFromReader;
1839
+ exports.docStretches = docStretches;
1593
1840
  exports.fontFaceCss = fontFaceCss;
1594
1841
  exports.formatDatePicture = formatDatePicture;
1595
1842
  exports.numberFootnotes = numberFootnotes;
1843
+ exports.packInput = packInput;
1596
1844
  exports.pageBand = pageBand;
1597
1845
  exports.pageBodyBoxes = pageBodyBoxes;
1598
1846
  exports.pageCapacity = pageCapacity;
1847
+ exports.pageChartOption = pageChartOption;
1599
1848
  exports.pageClass = pageClass;
1849
+ exports.pageClassIn = pageClassIn;
1600
1850
  exports.pageGeometry = pageGeometry;
1601
1851
  exports.pageMargins = pageMargins;
1602
1852
  exports.paginate = paginate;