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