@folijs/view 0.5.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 +33 -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/layout/flow.d.ts +193 -20
- package/dist/layout/flow.d.ts.map +1 -1
- package/dist/layout/flow.js +789 -101
- 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 +220 -7
- package/dist/view.d.ts.map +1 -1
- package/dist/view.js +1753 -208
- package/dist/view.js.map +1 -1
- package/dist/word/blocks.d.ts +30 -1
- package/dist/word/blocks.d.ts.map +1 -1
- package/dist/word/blocks.js +193 -40
- 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 +17 -5
- package/dist/word/edit.d.ts.map +1 -1
- package/dist/word/edit.js +157 -26
- 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 +18 -13
- package/dist/word/pages.d.ts.map +1 -1
- package/dist/word/pages.js +420 -229
- 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/package.json +2 -2
- package/src/clipboard.ts +3 -0
- package/src/layout/flow.ts +945 -110
- package/src/print.ts +48 -0
- package/src/view.ts +1957 -229
- package/src/word/blocks.ts +219 -36
- package/src/word/columns.ts +66 -0
- package/src/word/controls.ts +447 -0
- package/src/word/edit.ts +175 -23
- package/src/word/matches.ts +99 -0
- package/src/word/notes.ts +451 -0
- package/src/word/pages.ts +501 -220
- package/src/word/regions.ts +469 -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) {
|
|
@@ -63,58 +77,6 @@ export function measureLineRatio(container) {
|
|
|
63
77
|
return height > 0 ? height / 100 : 1.2;
|
|
64
78
|
};
|
|
65
79
|
}
|
|
66
|
-
/**
|
|
67
|
-
* The bottom of each line of a rendered block, from its top. Lines are the vertical clusters of
|
|
68
|
-
* its contents' client rects; a boundary is halfway between one line's content and the next's,
|
|
69
|
-
* the last line ends at the block's bottom.
|
|
70
|
-
*
|
|
71
|
-
* shortcut: line boxes are inferred from content rects (exact for even line heights); a
|
|
72
|
-
* superscript or a much larger run on a line moves that line's boundaries slightly.
|
|
73
|
-
*/
|
|
74
|
-
export function lineBottoms(element) {
|
|
75
|
-
const box = element.getBoundingClientRect();
|
|
76
|
-
const doc = element.ownerDocument;
|
|
77
|
-
const range = doc.createRange();
|
|
78
|
-
const rects = [];
|
|
79
|
-
// Text and line breaks only: a range over the whole block also has the boxes of the
|
|
80
|
-
// elements inside it (a table's), which span many lines.
|
|
81
|
-
const walker = doc.createTreeWalker(element, NodeFilter.SHOW_TEXT | NodeFilter.SHOW_ELEMENT);
|
|
82
|
-
for (let node = walker.nextNode(); node; node = walker.nextNode()) {
|
|
83
|
-
if (node.nodeType === Node.TEXT_NODE) {
|
|
84
|
-
range.selectNodeContents(node);
|
|
85
|
-
for (const rect of range.getClientRects())
|
|
86
|
-
rects.push({ rect, text: true });
|
|
87
|
-
}
|
|
88
|
-
else if (node.nodeName === "BR")
|
|
89
|
-
rects.push({ rect: node.getBoundingClientRect(), text: false });
|
|
90
|
-
}
|
|
91
|
-
rects.sort((a, b) => a.rect.top - b.rect.top);
|
|
92
|
-
const lines = [];
|
|
93
|
-
for (const { rect, text } of rects) {
|
|
94
|
-
if (rect.height <= 0)
|
|
95
|
-
continue;
|
|
96
|
-
const last = lines[lines.length - 1];
|
|
97
|
-
// Same line: it starts above the middle of the line so far.
|
|
98
|
-
if (last && rect.top < (last.top + last.bottom) / 2) {
|
|
99
|
-
// A line's text says where it is; a <br> only for a line without text (engines size
|
|
100
|
-
// its box differently).
|
|
101
|
-
if (text && !last.text)
|
|
102
|
-
Object.assign(last, { top: rect.top, bottom: rect.bottom, text });
|
|
103
|
-
else if (text === last.text) {
|
|
104
|
-
last.top = Math.min(last.top, rect.top);
|
|
105
|
-
last.bottom = Math.max(last.bottom, rect.bottom);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
else
|
|
109
|
-
lines.push({ top: rect.top, bottom: rect.bottom, text });
|
|
110
|
-
}
|
|
111
|
-
const bottoms = lines.slice(1).map((line, i) => {
|
|
112
|
-
const above = lines[i];
|
|
113
|
-
return (above.bottom + line.top) / 2 - box.top;
|
|
114
|
-
});
|
|
115
|
-
bottoms.push(box.height);
|
|
116
|
-
return bottoms;
|
|
117
|
-
}
|
|
118
80
|
/**
|
|
119
81
|
* A table block's line ends without those inside a row that may not split (`cantSplit`), unless
|
|
120
82
|
* the row is taller than a page.
|
|
@@ -143,38 +105,74 @@ function headerHeight(element) {
|
|
|
143
105
|
}
|
|
144
106
|
return bottom ? bottom - element.getBoundingClientRect().top : 0;
|
|
145
107
|
}
|
|
146
|
-
/** The lines that end with
|
|
147
|
-
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) {
|
|
148
110
|
const top = element.getBoundingClientRect().top;
|
|
149
|
-
return [...element.querySelectorAll(`br[${
|
|
111
|
+
return [...element.querySelectorAll(`br[${attr}]`)].map((br) => {
|
|
150
112
|
const y = br.getBoundingClientRect().top - top;
|
|
151
113
|
const line = bottoms.findIndex((bottom) => y < bottom);
|
|
152
114
|
return line < 0 ? bottoms.length - 1 : line;
|
|
153
115
|
});
|
|
154
116
|
}
|
|
155
117
|
/**
|
|
156
|
-
* Lays a v3 document out on pages inside `container
|
|
157
|
-
*
|
|
158
|
-
*
|
|
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.
|
|
159
121
|
*/
|
|
160
122
|
export function renderDocument(document, container, options = {}) {
|
|
161
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);
|
|
162
132
|
const blocks = renderBlocks(document, doc, {
|
|
163
133
|
...options,
|
|
164
134
|
lineRatio: measureLineRatio(container),
|
|
135
|
+
notes: { references: noteTextsNow },
|
|
165
136
|
});
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
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();
|
|
170
146
|
let current = [];
|
|
171
|
-
const close = (
|
|
172
|
-
const g = geometry(
|
|
173
|
-
const
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
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);
|
|
178
176
|
current = [];
|
|
179
177
|
};
|
|
180
178
|
for (const block of blocks) {
|
|
@@ -182,176 +180,367 @@ export function renderDocument(document, container, options = {}) {
|
|
|
182
180
|
if (block.section)
|
|
183
181
|
close(block.section);
|
|
184
182
|
}
|
|
185
|
-
if (current.length || !
|
|
183
|
+
if (current.length || !sections.length)
|
|
186
184
|
close(document.section);
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
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
203
|
const pageHeight = g.height - g.top - g.bottom;
|
|
204
|
-
const
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
}
|
|
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;
|
|
210
209
|
return value;
|
|
211
210
|
};
|
|
212
|
-
|
|
213
|
-
const
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
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)
|
|
217
382
|
continue;
|
|
218
|
-
const
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
text: cell.textContent ?? "",
|
|
222
|
-
})));
|
|
383
|
+
const region = regions.element(page.section, side);
|
|
384
|
+
region.append(drawing.content.content.cloneNode(true));
|
|
385
|
+
made.page.append(region);
|
|
223
386
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
const
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
let element = block.element;
|
|
292
|
-
if (placed.length > 1) {
|
|
293
|
-
// A split block: each fragment shows its lines of the whole block, clipped.
|
|
294
|
-
const top = fragment.fromLine ? (bottoms[fragment.fromLine - 1] ?? 0) : 0;
|
|
295
|
-
const end = fragment.toLine === Number.POSITIVE_INFINITY
|
|
296
|
-
? (bottoms[bottoms.length - 1] ?? 0)
|
|
297
|
-
: (bottoms[fragment.toLine - 1] ?? 0);
|
|
298
|
-
const clip = doc.createElement("div");
|
|
299
|
-
clip.style.overflow = "hidden";
|
|
300
|
-
clip.style.height = `${end - top}px`;
|
|
301
|
-
const inner = f === 0 ? block.element : block.element.cloneNode(true);
|
|
302
|
-
if (f > 0)
|
|
303
|
-
inner.setAttribute("aria-hidden", "true");
|
|
304
|
-
inner.style.marginTop = `${-top}px`;
|
|
305
|
-
clip.append(inner);
|
|
306
|
-
element = clip;
|
|
307
|
-
const repeat = f > 0 ? (headers.get(block) ?? 0) : 0;
|
|
308
|
-
if (repeat) {
|
|
309
|
-
// The header rows again above the continuation.
|
|
310
|
-
const header = doc.createElement("div");
|
|
311
|
-
header.style.overflow = "hidden";
|
|
312
|
-
header.style.height = `${repeat}px`;
|
|
313
|
-
const copy = block.element.cloneNode(true);
|
|
314
|
-
copy.setAttribute("aria-hidden", "true");
|
|
315
|
-
copy.style.marginTop = "0";
|
|
316
|
-
header.append(copy);
|
|
317
|
-
element = doc.createElement("div");
|
|
318
|
-
element.append(header, clip);
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
element.style.marginTop = `${gap}px`;
|
|
322
|
-
target.body.append(element);
|
|
323
|
-
previousOnPage = { page: fragment.page, block: b };
|
|
324
|
-
placements.push({
|
|
325
|
-
page: pages.length + fragment.page,
|
|
326
|
-
nodes: f === 0 ? block.nodes : null,
|
|
327
|
-
lines: () => (fragment.toLine === Number.POSITIVE_INFINITY
|
|
328
|
-
? linesOf(block).length
|
|
329
|
-
: fragment.toLine) - fragment.fromLine,
|
|
330
|
-
});
|
|
331
|
-
});
|
|
332
|
-
// Its cell paragraphs: on the page of the fragment their top is in.
|
|
333
|
-
for (const cell of cellTops.get(block) ?? []) {
|
|
334
|
-
let at = placed[0]?.page ?? 0;
|
|
335
|
-
for (const fragment of placed) {
|
|
336
|
-
const start = fragment.fromLine ? (bottoms[fragment.fromLine - 1] ?? 0) : 0;
|
|
337
|
-
if (cell.top + 0.5 >= start)
|
|
338
|
-
at = fragment.page;
|
|
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);
|
|
415
|
+
const firstPage = new Map();
|
|
416
|
+
/** Every fragment placed, in order: its page and how many lines it shows (measured lazily). */
|
|
417
|
+
const placements = [];
|
|
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);
|
|
339
454
|
}
|
|
340
|
-
cellPages.push({ page: pages.length + at, text: cell.text });
|
|
341
455
|
}
|
|
342
|
-
|
|
343
|
-
if (
|
|
344
|
-
|
|
345
|
-
|
|
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,
|
|
468
|
+
});
|
|
346
469
|
});
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
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 });
|
|
350
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));
|
|
351
502
|
}
|
|
503
|
+
const pages = created.map(({ page }) => {
|
|
504
|
+
container.append(page);
|
|
505
|
+
return page;
|
|
506
|
+
});
|
|
352
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`;
|
|
353
533
|
return {
|
|
354
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
|
+
},
|
|
355
544
|
pageOf: (index) => firstPage.get(index),
|
|
356
545
|
lineOf: (index) => {
|
|
357
546
|
if (!firstLine) {
|
|
@@ -363,9 +552,11 @@ export function renderDocument(document, container, options = {}) {
|
|
|
363
552
|
page = placement.page;
|
|
364
553
|
line = 1;
|
|
365
554
|
}
|
|
555
|
+
// A section's mark is on the line before it, as Word numbers it.
|
|
366
556
|
for (const node of placement.nodes ?? [])
|
|
367
|
-
firstLine.set(node, line);
|
|
368
|
-
|
|
557
|
+
firstLine.set(node, placement.mark ? Math.max(line - 1, 1) : line);
|
|
558
|
+
if (!placement.mark)
|
|
559
|
+
line += placement.lines();
|
|
369
560
|
}
|
|
370
561
|
}
|
|
371
562
|
return firstLine.get(index);
|