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