@folijs/view 0.4.0 → 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 +59 -0
- package/dist/clipboard.d.ts +3 -1
- package/dist/clipboard.d.ts.map +1 -1
- package/dist/clipboard.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/layout/flow.d.ts +195 -20
- package/dist/layout/flow.d.ts.map +1 -1
- package/dist/layout/flow.js +790 -102
- package/dist/layout/flow.js.map +1 -1
- package/dist/print.d.ts +22 -0
- package/dist/print.d.ts.map +1 -0
- package/dist/print.js +31 -0
- package/dist/print.js.map +1 -0
- package/dist/view.d.ts +407 -8
- package/dist/view.d.ts.map +1 -1
- package/dist/view.js +2956 -263
- package/dist/view.js.map +1 -1
- package/dist/word/blocks.d.ts +99 -2
- package/dist/word/blocks.d.ts.map +1 -1
- package/dist/word/blocks.js +535 -31
- package/dist/word/blocks.js.map +1 -1
- package/dist/word/columns.d.ts +23 -0
- package/dist/word/columns.d.ts.map +1 -0
- package/dist/word/columns.js +51 -0
- package/dist/word/columns.js.map +1 -0
- package/dist/word/controls.d.ts +63 -0
- package/dist/word/controls.d.ts.map +1 -0
- package/dist/word/controls.js +411 -0
- package/dist/word/controls.js.map +1 -0
- package/dist/word/edit.d.ts +32 -11
- package/dist/word/edit.d.ts.map +1 -1
- package/dist/word/edit.js +215 -33
- package/dist/word/edit.js.map +1 -1
- package/dist/word/matches.d.ts +23 -0
- package/dist/word/matches.d.ts.map +1 -0
- package/dist/word/matches.js +97 -0
- package/dist/word/matches.js.map +1 -0
- package/dist/word/notes.d.ts +105 -0
- package/dist/word/notes.d.ts.map +1 -0
- package/dist/word/notes.js +392 -0
- package/dist/word/notes.js.map +1 -0
- package/dist/word/pages.d.ts +26 -13
- package/dist/word/pages.d.ts.map +1 -1
- package/dist/word/pages.js +439 -203
- package/dist/word/pages.js.map +1 -1
- package/dist/word/regions.d.ts +113 -0
- package/dist/word/regions.d.ts.map +1 -0
- package/dist/word/regions.js +374 -0
- package/dist/word/regions.js.map +1 -0
- package/dist/word/table.d.ts +79 -0
- package/dist/word/table.d.ts.map +1 -0
- package/dist/word/table.js +210 -0
- package/dist/word/table.js.map +1 -0
- package/package.json +2 -2
- package/src/clipboard.ts +3 -0
- package/src/index.ts +8 -1
- package/src/layout/flow.ts +948 -111
- package/src/print.ts +48 -0
- package/src/view.ts +3291 -273
- package/src/word/blocks.ts +611 -28
- package/src/word/columns.ts +66 -0
- package/src/word/controls.ts +447 -0
- package/src/word/edit.ts +253 -30
- package/src/word/matches.ts +99 -0
- package/src/word/notes.ts +451 -0
- package/src/word/pages.ts +524 -192
- package/src/word/regions.ts +469 -0
- package/src/word/table.ts +267 -0
package/dist/word/pages.js
CHANGED
|
@@ -1,7 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* A schema v3 document on pages, as Word lays it out (8.6b, read-only): each section's page
|
|
3
|
+
* size and margins, the blocks of `blocks.ts` measured in the page's width and flowed with
|
|
4
|
+
* Word's spacing, keep rules, widow and orphan control and explicit breaks. Editing moves to
|
|
5
|
+
* v3 in 8.7.
|
|
6
|
+
*/
|
|
7
|
+
import { noteNumbers, noteProperties, noteTexts, parseContent, Resolver, } from "@folijs/core";
|
|
8
|
+
import { flowFrom, notePieces, pagesOf, spaceAfterText, } from "../layout/flow.js";
|
|
9
|
+
import { printStyles, SHEET_ATTR } from "../print.js";
|
|
10
|
+
import { NOTE_REF_ATTR, PICTURE_ATTR, PX_PER_TWIP, renderBlocks, WORD_COLUMN_BREAK_ATTR, WORD_PAGE_BREAK_ATTR, } from "./blocks.js";
|
|
11
|
+
import { bandElement, columnsPx } from "./columns.js";
|
|
12
|
+
import { lineBottoms, PageNotes } from "./notes.js";
|
|
13
|
+
import { PageRegions } from "./regions.js";
|
|
14
|
+
export { lineBottoms };
|
|
3
15
|
/** The class of each page element. */
|
|
4
16
|
export const WORD_PAGE_CLASS = "foli-word-page";
|
|
17
|
+
/** The breaks that keep a paragraph from being one line to the flow. */
|
|
18
|
+
const BREAKS = `br[${WORD_PAGE_BREAK_ATTR}],br[${WORD_COLUMN_BREAK_ATTR}]`;
|
|
5
19
|
/** Word's defaults for what a section leaves out: Letter, 1 inch margins. */
|
|
6
20
|
const PAGE_DEFAULTS = { width: 12240, height: 15840, margin: 1440 };
|
|
7
21
|
function geometry(section) {
|
|
@@ -29,6 +43,8 @@ function createPage(doc, g) {
|
|
|
29
43
|
const { style } = page;
|
|
30
44
|
style.boxSizing = "border-box";
|
|
31
45
|
style.position = "relative";
|
|
46
|
+
// The stacking context pictures behind text are drawn in (over the page's background).
|
|
47
|
+
style.isolation = "isolate";
|
|
32
48
|
style.width = `${g.width}px`;
|
|
33
49
|
style.height = `${g.height}px`;
|
|
34
50
|
style.padding = `${g.top}px ${g.right}px ${g.bottom}px ${g.left}px`;
|
|
@@ -62,56 +78,21 @@ export function measureLineRatio(container) {
|
|
|
62
78
|
};
|
|
63
79
|
}
|
|
64
80
|
/**
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
* the last line ends at the block's bottom.
|
|
68
|
-
*
|
|
69
|
-
* shortcut: line boxes are inferred from content rects (exact for even line heights); a
|
|
70
|
-
* superscript or a much larger run on a line moves that line's boundaries slightly.
|
|
81
|
+
* A table block's line ends without those inside a row that may not split (`cantSplit`), unless
|
|
82
|
+
* the row is taller than a page.
|
|
71
83
|
*/
|
|
72
|
-
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
rects.push({ rect, text: true });
|
|
85
|
-
}
|
|
86
|
-
else if (node.nodeName === "BR")
|
|
87
|
-
rects.push({ rect: node.getBoundingClientRect(), text: false });
|
|
88
|
-
}
|
|
89
|
-
rects.sort((a, b) => a.rect.top - b.rect.top);
|
|
90
|
-
const lines = [];
|
|
91
|
-
for (const { rect, text } of rects) {
|
|
92
|
-
if (rect.height <= 0)
|
|
93
|
-
continue;
|
|
94
|
-
const last = lines[lines.length - 1];
|
|
95
|
-
// Same line: it starts above the middle of the line so far.
|
|
96
|
-
if (last && rect.top < (last.top + last.bottom) / 2) {
|
|
97
|
-
// A line's text says where it is; a <br> only for a line without text (engines size
|
|
98
|
-
// its box differently).
|
|
99
|
-
if (text && !last.text)
|
|
100
|
-
Object.assign(last, { top: rect.top, bottom: rect.bottom, text });
|
|
101
|
-
else if (text === last.text) {
|
|
102
|
-
last.top = Math.min(last.top, rect.top);
|
|
103
|
-
last.bottom = Math.max(last.bottom, rect.bottom);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
else
|
|
107
|
-
lines.push({ top: rect.top, bottom: rect.bottom, text });
|
|
108
|
-
}
|
|
109
|
-
const bottoms = lines.slice(1).map((line, i) => {
|
|
110
|
-
const above = lines[i];
|
|
111
|
-
return (above.bottom + line.top) / 2 - box.top;
|
|
112
|
-
});
|
|
113
|
-
bottoms.push(box.height);
|
|
114
|
-
return bottoms;
|
|
84
|
+
function keepRows(element, bottoms, pageHeight) {
|
|
85
|
+
const rows = element.querySelectorAll(":scope > table > tbody > tr[data-foli-cant-split]");
|
|
86
|
+
if (!rows.length)
|
|
87
|
+
return bottoms;
|
|
88
|
+
const top = element.getBoundingClientRect().top;
|
|
89
|
+
const kept = [...rows]
|
|
90
|
+
.map((row) => {
|
|
91
|
+
const box = row.getBoundingClientRect();
|
|
92
|
+
return [box.top - top, box.bottom - top];
|
|
93
|
+
})
|
|
94
|
+
.filter(([from, to]) => to - from <= pageHeight);
|
|
95
|
+
return bottoms.filter((y) => !kept.some(([from, to]) => y > from + 0.5 && y < to - 0.5));
|
|
115
96
|
}
|
|
116
97
|
/** The height of a table's leading header rows (repeated on each page it continues on), or 0. */
|
|
117
98
|
function headerHeight(element) {
|
|
@@ -124,38 +105,74 @@ function headerHeight(element) {
|
|
|
124
105
|
}
|
|
125
106
|
return bottom ? bottom - element.getBoundingClientRect().top : 0;
|
|
126
107
|
}
|
|
127
|
-
/** The lines that end with
|
|
128
|
-
function breakLines(element, bottoms) {
|
|
108
|
+
/** The lines that end with one of the explicit breaks `attr` marks, by the break's position. */
|
|
109
|
+
function breakLines(element, bottoms, attr) {
|
|
129
110
|
const top = element.getBoundingClientRect().top;
|
|
130
|
-
return [...element.querySelectorAll(`br[${
|
|
111
|
+
return [...element.querySelectorAll(`br[${attr}]`)].map((br) => {
|
|
131
112
|
const y = br.getBoundingClientRect().top - top;
|
|
132
113
|
const line = bottoms.findIndex((bottom) => y < bottom);
|
|
133
114
|
return line < 0 ? bottoms.length - 1 : line;
|
|
134
115
|
});
|
|
135
116
|
}
|
|
136
117
|
/**
|
|
137
|
-
* Lays a v3 document out on pages inside `container
|
|
138
|
-
*
|
|
139
|
-
*
|
|
118
|
+
* Lays a v3 document out on pages inside `container`: every section's page setup, its columns
|
|
119
|
+
* (a band per section on a page), continuous sections, Word's blank pages, headers and footers
|
|
120
|
+
* with their page fields, footnotes at their columns' feet and endnotes after the text.
|
|
140
121
|
*/
|
|
141
122
|
export function renderDocument(document, container, options = {}) {
|
|
142
123
|
const doc = container.ownerDocument;
|
|
124
|
+
// What notes show: numbers depend on the pages only where they restart on each.
|
|
125
|
+
const model = document.stories ? parseContent(document.content) : null;
|
|
126
|
+
const hideDeleted = (options.review ?? "markup") !== "markup";
|
|
127
|
+
const numbersOf = (pageOf) => model
|
|
128
|
+
? noteNumbers(model, document, { hideDeleted, ...(pageOf ? { pageOf } : {}) }).filter((n) => !hideDeleted || !n.deleted)
|
|
129
|
+
: [];
|
|
130
|
+
let references = numbersOf();
|
|
131
|
+
const noteTextsNow = noteTexts(references);
|
|
143
132
|
const blocks = renderBlocks(document, doc, {
|
|
144
133
|
...options,
|
|
145
134
|
lineRatio: measureLineRatio(container),
|
|
135
|
+
notes: { references: noteTextsNow },
|
|
146
136
|
});
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
const
|
|
137
|
+
const sections = [];
|
|
138
|
+
const setups = [];
|
|
139
|
+
const lineRatio = measureLineRatio(container);
|
|
140
|
+
const regions = new PageRegions(doc, { ...options, lineRatio, measureIn: () => container }, new WeakMap());
|
|
141
|
+
const resolver = new Resolver(document);
|
|
142
|
+
regions.setDocument(document, setups, resolver);
|
|
143
|
+
const notes = new PageNotes(doc, { ...options, lineRatio, measureIn: () => container }, new WeakMap());
|
|
144
|
+
const geometryOf = new Map();
|
|
145
|
+
const sectionOf = new Map();
|
|
151
146
|
let current = [];
|
|
152
|
-
const close = (
|
|
153
|
-
const g = geometry(
|
|
154
|
-
const
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
147
|
+
const close = (setup) => {
|
|
148
|
+
const g = geometry(setup);
|
|
149
|
+
const columns = columnsPx(setup);
|
|
150
|
+
const index = setups.length;
|
|
151
|
+
setups.push(setup);
|
|
152
|
+
const flow = {
|
|
153
|
+
index,
|
|
154
|
+
start: setup.type ?? "nextPage",
|
|
155
|
+
columns: columns.widths.length,
|
|
156
|
+
pageStart: setup.pageNumbers?.start,
|
|
157
|
+
box: (page) => ({ height: regions.boxHeight(index, page) }),
|
|
158
|
+
samePage: (other) => sameGeometry(g, geometryOf.get(other)),
|
|
159
|
+
};
|
|
160
|
+
geometryOf.set(flow, g);
|
|
161
|
+
// Measured in a page of the section's width, out of view, in its first column's width.
|
|
162
|
+
const { page, body } = createPage(doc, g);
|
|
163
|
+
page.style.position = "absolute";
|
|
164
|
+
page.style.visibility = "hidden";
|
|
165
|
+
page.style.height = "auto";
|
|
166
|
+
const column = doc.createElement("div");
|
|
167
|
+
column.style.display = "flow-root";
|
|
168
|
+
column.style.width = `${columns.widths[0]}px`;
|
|
169
|
+
body.append(column);
|
|
170
|
+
container.append(page);
|
|
171
|
+
column.append(...current.map((block) => block.element));
|
|
172
|
+
const section = { setup, geometry: g, columns, blocks: current, flow, measuring: page, column };
|
|
173
|
+
for (const block of current)
|
|
174
|
+
sectionOf.set(block, section);
|
|
175
|
+
sections.push(section);
|
|
159
176
|
current = [];
|
|
160
177
|
};
|
|
161
178
|
for (const block of blocks) {
|
|
@@ -163,151 +180,367 @@ export function renderDocument(document, container, options = {}) {
|
|
|
163
180
|
if (block.section)
|
|
164
181
|
close(block.section);
|
|
165
182
|
}
|
|
166
|
-
if (current.length || !
|
|
183
|
+
if (current.length || !sections.length)
|
|
167
184
|
close(document.section);
|
|
168
|
-
|
|
185
|
+
notes.setDocument(document, setups, resolver, references);
|
|
186
|
+
sections.forEach((section, i) => {
|
|
187
|
+
const next = sections[i + 1];
|
|
188
|
+
section.flow.balanced =
|
|
189
|
+
!!next &&
|
|
190
|
+
section.flow.columns > 1 &&
|
|
191
|
+
next.setup.type === "continuous" &&
|
|
192
|
+
sameGeometry(section.geometry, next.geometry);
|
|
193
|
+
});
|
|
194
|
+
const measured = new Map();
|
|
195
|
+
const inWidth = (block, width) => {
|
|
196
|
+
const section = sectionOf.get(block);
|
|
197
|
+
const widths = measured.get(block) ?? new Map();
|
|
198
|
+
measured.set(block, widths);
|
|
199
|
+
const known = widths.get(width);
|
|
200
|
+
if (known)
|
|
201
|
+
return known;
|
|
202
|
+
const { column, geometry: g } = section;
|
|
203
|
+
const pageHeight = g.height - g.top - g.bottom;
|
|
204
|
+
const atWidth = (read) => {
|
|
205
|
+
const before = column.style.width;
|
|
206
|
+
column.style.width = `${width}px`;
|
|
207
|
+
const value = read();
|
|
208
|
+
column.style.width = before;
|
|
209
|
+
return value;
|
|
210
|
+
};
|
|
211
|
+
const height = atWidth(() => block.element.getBoundingClientRect().height);
|
|
212
|
+
const hasBreak = !!block.element.querySelector(BREAKS);
|
|
213
|
+
// A paragraph kept together is one line to the flow, unless it cannot be: taller than a
|
|
214
|
+
// page, or broken by a page break.
|
|
215
|
+
const kept = block.keepLines && !hasBreak && height <= pageHeight;
|
|
216
|
+
let lines;
|
|
217
|
+
const shown = () => {
|
|
218
|
+
lines ??= atWidth(() => keepRows(block.element, lineBottoms(block.element), pageHeight));
|
|
219
|
+
return lines;
|
|
220
|
+
};
|
|
221
|
+
const entry = {
|
|
222
|
+
height,
|
|
223
|
+
shown,
|
|
224
|
+
lines: () => (kept ? [height] : shown()),
|
|
225
|
+
breaks: (attr) => atWidth(() => breakLines(block.element, entry.lines(), attr)),
|
|
226
|
+
references: () => atWidth(() => {
|
|
227
|
+
const top = block.element.getBoundingClientRect().top;
|
|
228
|
+
const bottoms = entry.lines();
|
|
229
|
+
return [...block.element.querySelectorAll(`[${NOTE_REF_ATTR}]`)].map((element) => {
|
|
230
|
+
const y = element.getBoundingClientRect().bottom - top;
|
|
231
|
+
const line = bottoms.findIndex((bottom) => y <= bottom + 1);
|
|
232
|
+
return {
|
|
233
|
+
story: element.getAttribute(NOTE_REF_ATTR),
|
|
234
|
+
line: line < 0 ? bottoms.length - 1 : line,
|
|
235
|
+
};
|
|
236
|
+
});
|
|
237
|
+
}),
|
|
238
|
+
};
|
|
239
|
+
widths.set(width, entry);
|
|
240
|
+
return entry;
|
|
241
|
+
};
|
|
242
|
+
const widthIn = (section, fragment) => section.columns.widths[fragment.column ?? 0];
|
|
243
|
+
// Each cell paragraph's top in its block, measured while the blocks are laid out whole.
|
|
244
|
+
const cellTops = new Map();
|
|
245
|
+
for (const block of blocks) {
|
|
246
|
+
const cells = block.element.querySelectorAll("[data-foli-cell]");
|
|
247
|
+
if (!cells.length)
|
|
248
|
+
continue;
|
|
249
|
+
const top = block.element.getBoundingClientRect().top;
|
|
250
|
+
cellTops.set(block, [...cells].map((cell) => ({
|
|
251
|
+
top: cell.getBoundingClientRect().top - top,
|
|
252
|
+
text: cell.textContent ?? "",
|
|
253
|
+
})));
|
|
254
|
+
}
|
|
255
|
+
const headers = new Map();
|
|
256
|
+
const endnotes = notes.endnotes();
|
|
257
|
+
const lastOfSection = (block, i) => !blocks[i + 1] || sectionOf.get(blocks[i + 1]) !== sectionOf.get(block);
|
|
258
|
+
const boxes = blocks.map((block, i) => {
|
|
259
|
+
const section = sectionOf.get(block);
|
|
260
|
+
const { widths } = section.columns;
|
|
261
|
+
const whole = inWidth(block, widths[0]);
|
|
262
|
+
const repeat = block.element.querySelector("tr") ? headerHeight(block.element) : 0;
|
|
263
|
+
if (repeat)
|
|
264
|
+
headers.set(block, repeat);
|
|
265
|
+
const next = blocks[i + 1];
|
|
266
|
+
const has = (attr) => !!block.element.querySelector(`br[${attr}]`);
|
|
267
|
+
const hasPageBreak = has(WORD_PAGE_BREAK_ATTR);
|
|
268
|
+
const hasColumnBreak = has(WORD_COLUMN_BREAK_ATTR);
|
|
269
|
+
const index = section.flow.index ?? 0;
|
|
270
|
+
const refers = !!block.element.querySelector(`[${NOTE_REF_ATTR}]`);
|
|
271
|
+
return {
|
|
272
|
+
notes: refers
|
|
273
|
+
? (column) => inWidth(block, widths[column])
|
|
274
|
+
.references()
|
|
275
|
+
.flatMap(({ story, line }) => {
|
|
276
|
+
const note = notes.footnote(story, index);
|
|
277
|
+
return note ? [{ line, note }] : [];
|
|
278
|
+
})
|
|
279
|
+
: undefined,
|
|
280
|
+
endnotes: lastOfSection(block, i) ? endnotes.get(index) : undefined,
|
|
281
|
+
height: whole.height,
|
|
282
|
+
marginTop: block.before,
|
|
283
|
+
marginBottom: block.after,
|
|
284
|
+
repeat,
|
|
285
|
+
// A table splits at any line: no widow and orphan control across its rows.
|
|
286
|
+
anyLine: !!block.element.querySelector(":scope > table"),
|
|
287
|
+
keepWithNext: block.keepNext,
|
|
288
|
+
section: section.flow,
|
|
289
|
+
sectionMark: !!block.section &&
|
|
290
|
+
!block.element.textContent &&
|
|
291
|
+
!block.element.querySelector(`[${PICTURE_ATTR}]`),
|
|
292
|
+
lines: whole.lines,
|
|
293
|
+
inColumn: widths.every((width) => width === widths[0])
|
|
294
|
+
? undefined
|
|
295
|
+
: (column) => inWidth(block, widths[column]),
|
|
296
|
+
breaksAfter: (column = 0) => {
|
|
297
|
+
const measure = inWidth(block, widths[column]);
|
|
298
|
+
const breaks = hasPageBreak ? measure.breaks(WORD_PAGE_BREAK_ATTR) : [];
|
|
299
|
+
// The next block starts a page: this one's last line ends it.
|
|
300
|
+
if (next?.pageBreakBefore)
|
|
301
|
+
breaks.push(measure.lines().length - 1);
|
|
302
|
+
return breaks;
|
|
303
|
+
},
|
|
304
|
+
columnBreaksAfter: (column = 0) => hasColumnBreak
|
|
305
|
+
? inWidth(block, widths[column]).breaks(WORD_COLUMN_BREAK_ATTR)
|
|
306
|
+
: [],
|
|
307
|
+
};
|
|
308
|
+
});
|
|
309
|
+
// shortcut: widow and orphan control is on for every paragraph (Word's Normal has it; a
|
|
310
|
+
// paragraph turning it off is still kept to two lines).
|
|
311
|
+
const rules = {
|
|
312
|
+
orphans: 2,
|
|
313
|
+
widows: 2,
|
|
314
|
+
spacing: "word",
|
|
315
|
+
addSpacing: document.settings?.doNotUseHTMLParagraphAutoSpacing === true,
|
|
316
|
+
evenAndOddHeaders: document.settings?.evenAndOddHeaders === true,
|
|
317
|
+
notes: notes.flowRules(),
|
|
318
|
+
};
|
|
319
|
+
let flowed = flowFrom(boxes, 0, rules);
|
|
320
|
+
let facts = pagesOf(boxes, flowed);
|
|
321
|
+
// The page count is known once flowed; a header holding it can change what fits.
|
|
322
|
+
for (let pass = 0; pass < 2 && regions.settle(facts); pass++) {
|
|
323
|
+
flowed = flowFrom(boxes, 0, rules);
|
|
324
|
+
facts = pagesOf(boxes, flowed);
|
|
325
|
+
}
|
|
326
|
+
// Numbers restarting on each page: known once flowed; a wider one is left as it is measured.
|
|
327
|
+
if (setups.some((setup) => noteProperties(document, "footnote", setup).restart === "eachPage")) {
|
|
328
|
+
const pages = new Map();
|
|
329
|
+
for (const piece of notePieces(flowed))
|
|
330
|
+
if (!piece.fromLine && !pages.has(piece.id))
|
|
331
|
+
pages.set(piece.id, piece.page);
|
|
332
|
+
references = numbersOf((_pos, story) => pages.get(story));
|
|
333
|
+
const texts = noteTexts(references);
|
|
334
|
+
notes.setDocument(document, setups, resolver, references);
|
|
335
|
+
for (const block of blocks)
|
|
336
|
+
for (const element of block.element.querySelectorAll(`[${NOTE_REF_ATTR}]`))
|
|
337
|
+
element.textContent = texts.get(element.getAttribute(NOTE_REF_ATTR)) ?? "";
|
|
338
|
+
flowed = flowFrom(boxes, 0, rules);
|
|
339
|
+
facts = pagesOf(boxes, flowed);
|
|
340
|
+
}
|
|
341
|
+
const { fragments, gaps } = flowed;
|
|
342
|
+
const pieces = notePieces(flowed);
|
|
343
|
+
// What placing needs of the layout, read while the blocks are still laid out: split blocks' lines.
|
|
344
|
+
blocks.forEach((block, b) => {
|
|
345
|
+
const placed = fragments[b] ?? [];
|
|
346
|
+
if (placed.length < 2)
|
|
347
|
+
return;
|
|
348
|
+
const section = sectionOf.get(block);
|
|
349
|
+
for (const fragment of placed)
|
|
350
|
+
inWidth(block, widthIn(section, fragment)).lines();
|
|
351
|
+
});
|
|
352
|
+
for (const section of sections)
|
|
353
|
+
section.measuring.remove();
|
|
354
|
+
// Each page's bands, from what is on it; a page with nothing (Word's blank page before an even
|
|
355
|
+
// or odd page section) has the setup of the page after it.
|
|
356
|
+
const bandsOf = [];
|
|
357
|
+
blocks.forEach((block, b) => {
|
|
358
|
+
for (const fragment of fragments[b] ?? []) {
|
|
359
|
+
const bands = bandsOf[fragment.page] ?? [];
|
|
360
|
+
bandsOf[fragment.page] = bands;
|
|
361
|
+
bands[fragment.band ?? 0] ??= sectionOf.get(block);
|
|
362
|
+
}
|
|
363
|
+
});
|
|
364
|
+
// A note can reach pages no text is on.
|
|
365
|
+
const count = Math.max(1, bandsOf.length, ...pieces.map((piece) => piece.page + 1));
|
|
366
|
+
const pageSetups = [];
|
|
367
|
+
for (let p = count - 1; p >= 0; p--)
|
|
368
|
+
pageSetups[p] = bandsOf[p]?.[0] ?? pageSetups[p + 1] ?? sections.at(-1);
|
|
369
|
+
const created = pageSetups.map((section, p) => {
|
|
370
|
+
const made = createPage(doc, section.geometry);
|
|
371
|
+
made.page.setAttribute(SHEET_ATTR, String(section.flow.index ?? 0));
|
|
372
|
+
const page = facts[p];
|
|
373
|
+
// Word's blank page before an even or odd page section has no header or footer (its PDF).
|
|
374
|
+
if (!page?.sectionPages)
|
|
375
|
+
return made;
|
|
376
|
+
const { top, bottom } = regions.margins(page.section, page);
|
|
377
|
+
made.page.style.paddingTop = `${top}px`;
|
|
378
|
+
made.page.style.paddingBottom = `${bottom}px`;
|
|
379
|
+
for (const side of ["header", "footer"]) {
|
|
380
|
+
const drawing = regions.drawing(page.section, side, page);
|
|
381
|
+
if (!drawing)
|
|
382
|
+
continue;
|
|
383
|
+
const region = regions.element(page.section, side);
|
|
384
|
+
region.append(drawing.content.content.cloneNode(true));
|
|
385
|
+
made.page.append(region);
|
|
386
|
+
}
|
|
387
|
+
return made;
|
|
388
|
+
});
|
|
389
|
+
const bandElements = new Map();
|
|
390
|
+
/** Where a fragment goes: the page's body, or its column when the page has bands. */
|
|
391
|
+
const holder = (fragment, section) => {
|
|
392
|
+
const { body } = created[fragment.page];
|
|
393
|
+
const bands = bandsOf[fragment.page] ?? [section];
|
|
394
|
+
if (bands.length === 1 && section.flow.columns === 1)
|
|
395
|
+
return body;
|
|
396
|
+
const band = fragment.band ?? 0;
|
|
397
|
+
// Bands in order: one that nothing starts in yet still comes before the later ones.
|
|
398
|
+
for (let b = 0; b <= band; b++) {
|
|
399
|
+
const key = `${fragment.page} ${b}`;
|
|
400
|
+
if (bandElements.has(key))
|
|
401
|
+
continue;
|
|
402
|
+
const made = bandElement(doc, bands[b].columns);
|
|
403
|
+
bandElements.set(key, made);
|
|
404
|
+
body.append(made.band);
|
|
405
|
+
}
|
|
406
|
+
const made = bandElements.get(`${fragment.page} ${band}`);
|
|
407
|
+
if (fragment.lead)
|
|
408
|
+
made.band.style.marginTop = `${fragment.lead}px`;
|
|
409
|
+
return made.columns[fragment.column ?? 0];
|
|
410
|
+
};
|
|
411
|
+
/** The section a page's band is in: the one of the page's blocks, or of the page. */
|
|
412
|
+
const sectionAt = (page, band) => (bandsOf[page]?.[band] ?? pageSetups[page])?.flow.index ?? 0;
|
|
413
|
+
const groupsOf = (list) => notes.groups(list, sectionAt);
|
|
414
|
+
const footGroups = groupsOf(pieces).filter((group) => !group.inFlow);
|
|
169
415
|
const firstPage = new Map();
|
|
170
416
|
/** Every fragment placed, in order: its page and how many lines it shows (measured lazily). */
|
|
171
417
|
const placements = [];
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
keepWithNext: block.keepNext,
|
|
209
|
-
lines: () => (kept ? [height] : linesOf(block)),
|
|
210
|
-
breaksAfter: () => {
|
|
211
|
-
const breaks = hasBreak ? breakLines(block.element, linesOf(block)) : [];
|
|
212
|
-
// The next block starts a page: this one's last line ends it.
|
|
213
|
-
if (next?.pageBreakBefore)
|
|
214
|
-
breaks.push((kept ? 1 : linesOf(block).length) - 1);
|
|
215
|
-
return breaks;
|
|
216
|
-
},
|
|
217
|
-
};
|
|
218
|
-
});
|
|
219
|
-
// shortcut: widow and orphan control is on for every paragraph (Word's Normal has it; a
|
|
220
|
-
// paragraph turning it off is still kept to two lines).
|
|
221
|
-
const fragments = flow(boxes, pageHeight, { orphans: 2, widows: 2, spacing: "word" });
|
|
222
|
-
// What placing needs of the layout, read while the blocks are still laid out: the lines of
|
|
223
|
-
// split blocks, and which blocks end with an explicit page break.
|
|
224
|
-
const endsWithBreak = new Set();
|
|
225
|
-
group.blocks.forEach((block, b) => {
|
|
226
|
-
if ((fragments[b]?.length ?? 0) > 1)
|
|
227
|
-
linesOf(block);
|
|
228
|
-
if (!block.element.querySelector(`br[${WORD_PAGE_BREAK_ATTR}]`))
|
|
229
|
-
return;
|
|
230
|
-
const bottoms = linesOf(block);
|
|
231
|
-
if (breakLines(block.element, bottoms).includes(bottoms.length - 1))
|
|
232
|
-
endsWithBreak.add(block);
|
|
233
|
-
});
|
|
234
|
-
measuring.page.remove();
|
|
235
|
-
const count = Math.max(1, ...fragments.flat().map((f) => f.page + 1));
|
|
236
|
-
const groupPages = Array.from({ length: count }, () => createPage(doc, g));
|
|
237
|
-
let previousOnPage = null;
|
|
238
|
-
group.blocks.forEach((block, b) => {
|
|
239
|
-
const placed = fragments[b] ?? [];
|
|
240
|
-
const bottoms = placed.length > 1 ? linesOf(block) : [];
|
|
241
|
-
placed.forEach((fragment, f) => {
|
|
242
|
-
const target = groupPages[fragment.page];
|
|
243
|
-
let gap = 0;
|
|
244
|
-
if (fragment.fromLine === 0) {
|
|
245
|
-
if (previousOnPage?.page === fragment.page)
|
|
246
|
-
gap = group.blocks[previousOnPage.block].after + block.before;
|
|
247
|
-
else {
|
|
248
|
-
// The first block on a page: Word keeps its space before on the section's first
|
|
249
|
-
// page and after an explicit break only.
|
|
250
|
-
const prior = group.blocks[b - 1];
|
|
251
|
-
const forced = fragment.page === 0 ||
|
|
252
|
-
block.pageBreakBefore ||
|
|
253
|
-
(prior !== undefined && endsWithBreak.has(prior));
|
|
254
|
-
gap = forced ? block.before : 0;
|
|
255
|
-
}
|
|
418
|
+
const cellPages = [];
|
|
419
|
+
blocks.forEach((block, b) => {
|
|
420
|
+
const section = sectionOf.get(block);
|
|
421
|
+
const placed = fragments[b] ?? [];
|
|
422
|
+
const bottomsOf = (fragment) => inWidth(block, widthIn(section, fragment)).lines();
|
|
423
|
+
placed.forEach((fragment, f) => {
|
|
424
|
+
const target = holder(fragment, section);
|
|
425
|
+
let element = block.element;
|
|
426
|
+
if (placed.length > 1) {
|
|
427
|
+
// A split block: each fragment shows its lines of the whole block, clipped.
|
|
428
|
+
const bottoms = bottomsOf(fragment);
|
|
429
|
+
const top = fragment.fromLine ? (bottoms[fragment.fromLine - 1] ?? 0) : 0;
|
|
430
|
+
const end = fragment.toLine === Number.POSITIVE_INFINITY
|
|
431
|
+
? (bottoms[bottoms.length - 1] ?? 0)
|
|
432
|
+
: (bottoms[fragment.toLine - 1] ?? 0);
|
|
433
|
+
const clip = doc.createElement("div");
|
|
434
|
+
clip.style.overflow = "hidden";
|
|
435
|
+
clip.style.height = `${end - top}px`;
|
|
436
|
+
const inner = f === 0 ? block.element : block.element.cloneNode(true);
|
|
437
|
+
if (f > 0)
|
|
438
|
+
inner.setAttribute("aria-hidden", "true");
|
|
439
|
+
inner.style.marginTop = `${-top}px`;
|
|
440
|
+
clip.append(inner);
|
|
441
|
+
element = clip;
|
|
442
|
+
const repeat = f > 0 ? (headers.get(block) ?? 0) : 0;
|
|
443
|
+
if (repeat) {
|
|
444
|
+
// The header rows again above the continuation.
|
|
445
|
+
const header = doc.createElement("div");
|
|
446
|
+
header.style.overflow = "hidden";
|
|
447
|
+
header.style.height = `${repeat}px`;
|
|
448
|
+
const copy = block.element.cloneNode(true);
|
|
449
|
+
copy.setAttribute("aria-hidden", "true");
|
|
450
|
+
copy.style.marginTop = "0";
|
|
451
|
+
header.append(copy);
|
|
452
|
+
element = doc.createElement("div");
|
|
453
|
+
element.append(header, clip);
|
|
256
454
|
}
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
inner.style.marginTop = `${-top}px`;
|
|
271
|
-
clip.append(inner);
|
|
272
|
-
element = clip;
|
|
273
|
-
const repeat = f > 0 ? (headers.get(block) ?? 0) : 0;
|
|
274
|
-
if (repeat) {
|
|
275
|
-
// The header rows again above the continuation.
|
|
276
|
-
const header = doc.createElement("div");
|
|
277
|
-
header.style.overflow = "hidden";
|
|
278
|
-
header.style.height = `${repeat}px`;
|
|
279
|
-
const copy = block.element.cloneNode(true);
|
|
280
|
-
copy.setAttribute("aria-hidden", "true");
|
|
281
|
-
copy.style.marginTop = "0";
|
|
282
|
-
header.append(copy);
|
|
283
|
-
element = doc.createElement("div");
|
|
284
|
-
element.append(header, clip);
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
element.style.marginTop = `${gap}px`;
|
|
288
|
-
target.body.append(element);
|
|
289
|
-
previousOnPage = { page: fragment.page, block: b };
|
|
290
|
-
placements.push({
|
|
291
|
-
page: pages.length + fragment.page,
|
|
292
|
-
nodes: f === 0 ? block.nodes : null,
|
|
293
|
-
lines: () => (fragment.toLine === Number.POSITIVE_INFINITY
|
|
294
|
-
? linesOf(block).length
|
|
295
|
-
: fragment.toLine) - fragment.fromLine,
|
|
296
|
-
});
|
|
455
|
+
}
|
|
456
|
+
// A section's empty last paragraph takes no room.
|
|
457
|
+
if (fragment.mark)
|
|
458
|
+
element.style.position = "absolute";
|
|
459
|
+
element.style.marginTop = `${fragment.fromLine === 0 ? (gaps[b] ?? 0) : 0}px`;
|
|
460
|
+
target.append(element);
|
|
461
|
+
placements.push({
|
|
462
|
+
page: fragment.page,
|
|
463
|
+
nodes: f === 0 ? block.nodes : null,
|
|
464
|
+
mark: !!fragment.mark,
|
|
465
|
+
lines: () => (fragment.toLine === Number.POSITIVE_INFINITY
|
|
466
|
+
? inWidth(block, widthIn(section, fragment)).shown().length
|
|
467
|
+
: fragment.toLine) - fragment.fromLine,
|
|
297
468
|
});
|
|
298
|
-
const first = placed[0];
|
|
299
|
-
if (first)
|
|
300
|
-
for (const index of block.nodes)
|
|
301
|
-
firstPage.set(index, pages.length + first.page);
|
|
302
469
|
});
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
470
|
+
// Its cell paragraphs: on the page of the fragment their top is in.
|
|
471
|
+
for (const cell of cellTops.get(block) ?? []) {
|
|
472
|
+
let at = placed[0]?.page ?? 0;
|
|
473
|
+
for (const fragment of placed) {
|
|
474
|
+
const start = fragment.fromLine ? (bottomsOf(fragment)[fragment.fromLine - 1] ?? 0) : 0;
|
|
475
|
+
if (cell.top + 0.5 >= start)
|
|
476
|
+
at = fragment.page;
|
|
477
|
+
}
|
|
478
|
+
cellPages.push({ page: at, text: cell.text });
|
|
306
479
|
}
|
|
480
|
+
const first = placed[0];
|
|
481
|
+
if (first)
|
|
482
|
+
for (const index of block.nodes)
|
|
483
|
+
firstPage.set(index, first.page);
|
|
484
|
+
});
|
|
485
|
+
// Endnotes and footnotes set beneath the text come after the last block of their column.
|
|
486
|
+
for (const group of groupsOf(pieces))
|
|
487
|
+
if (group.inFlow)
|
|
488
|
+
holder({ page: group.page, band: group.band, column: group.column }, sections[group.section]).append(notes.groupElement(group, null, undefined, group.pieces[0]?.end
|
|
489
|
+
? 0
|
|
490
|
+
: spaceAfterText(boxes, flowed, group.page, group.band, group.column)));
|
|
491
|
+
for (const group of groupsOf(pieces)) {
|
|
492
|
+
if (group.inFlow)
|
|
493
|
+
continue;
|
|
494
|
+
const { page } = created[group.page];
|
|
495
|
+
const section = sections[group.section];
|
|
496
|
+
const fact = facts[group.page];
|
|
497
|
+
const margin = fact?.sectionPages
|
|
498
|
+
? regions.margins(fact.section, fact).bottom
|
|
499
|
+
: section.geometry.bottom;
|
|
500
|
+
const place = notes.footPlace({ left: section.geometry.left, bottom: margin }, notes.stacked(group, footGroups));
|
|
501
|
+
page.append(notes.groupElement(group, place));
|
|
307
502
|
}
|
|
503
|
+
const pages = created.map(({ page }) => {
|
|
504
|
+
container.append(page);
|
|
505
|
+
return page;
|
|
506
|
+
});
|
|
308
507
|
let firstLine = null;
|
|
508
|
+
let starts = null;
|
|
509
|
+
const fieldLayout = {
|
|
510
|
+
pages: facts.length,
|
|
511
|
+
pageAt: (pos) => {
|
|
512
|
+
if (!starts) {
|
|
513
|
+
const list = [];
|
|
514
|
+
(model ?? parseContent(document.content)).forEach((_node, offset) => {
|
|
515
|
+
list.push(offset);
|
|
516
|
+
});
|
|
517
|
+
starts = list;
|
|
518
|
+
}
|
|
519
|
+
// The top-level node holding `pos`, or the nearest one before it that is shown.
|
|
520
|
+
let index = starts.length - 1;
|
|
521
|
+
while (index > 0 && starts[index] > pos)
|
|
522
|
+
index--;
|
|
523
|
+
while (index > 0 && !firstPage.has(index))
|
|
524
|
+
index--;
|
|
525
|
+
const fact = facts[firstPage.get(index) ?? -1];
|
|
526
|
+
if (!fact)
|
|
527
|
+
return null;
|
|
528
|
+
const format = setups[fact.section]?.pageNumbers?.format;
|
|
529
|
+
return { number: fact.number, sectionPages: fact.sectionPages, format };
|
|
530
|
+
},
|
|
531
|
+
};
|
|
532
|
+
const px = (n) => `${n}px`;
|
|
309
533
|
return {
|
|
310
534
|
pages,
|
|
535
|
+
fieldLayout,
|
|
536
|
+
printStyles: () => {
|
|
537
|
+
const last = sections.at(-1).geometry;
|
|
538
|
+
return printStyles(`.${WORD_PAGE_CLASS}`, { width: px(last.width), height: px(last.height) }, sections.map(({ geometry: g, flow }) => ({
|
|
539
|
+
width: px(g.width),
|
|
540
|
+
height: px(g.height),
|
|
541
|
+
index: flow.index ?? 0,
|
|
542
|
+
})), "word");
|
|
543
|
+
},
|
|
311
544
|
pageOf: (index) => firstPage.get(index),
|
|
312
545
|
lineOf: (index) => {
|
|
313
546
|
if (!firstLine) {
|
|
@@ -319,13 +552,16 @@ export function renderDocument(document, container, options = {}) {
|
|
|
319
552
|
page = placement.page;
|
|
320
553
|
line = 1;
|
|
321
554
|
}
|
|
555
|
+
// A section's mark is on the line before it, as Word numbers it.
|
|
322
556
|
for (const node of placement.nodes ?? [])
|
|
323
|
-
firstLine.set(node, line);
|
|
324
|
-
|
|
557
|
+
firstLine.set(node, placement.mark ? Math.max(line - 1, 1) : line);
|
|
558
|
+
if (!placement.mark)
|
|
559
|
+
line += placement.lines();
|
|
325
560
|
}
|
|
326
561
|
}
|
|
327
562
|
return firstLine.get(index);
|
|
328
563
|
},
|
|
564
|
+
cellParagraphs: () => cellPages,
|
|
329
565
|
destroy: () => {
|
|
330
566
|
for (const page of pages)
|
|
331
567
|
page.remove();
|