@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
|
@@ -0,0 +1,469 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Headers and footers on pages (11.7, ADR-0007), for both renderers: which story a page shows, how
|
|
3
|
+
* tall it is with that page's fields filled in, how far it pushes the text, and its elements.
|
|
4
|
+
* A story is always drawn from the model here, by the read-only blocks renderer; `PAGE`, `NUMPAGES`
|
|
5
|
+
* and `SECTIONPAGES` are filled in the copy drawn, never in the document.
|
|
6
|
+
*/
|
|
7
|
+
import {
|
|
8
|
+
type FoliDocument,
|
|
9
|
+
formatNumber,
|
|
10
|
+
type NodeJSON,
|
|
11
|
+
pageVariant,
|
|
12
|
+
type Resolver,
|
|
13
|
+
type SectionJSON,
|
|
14
|
+
type StoryJSON,
|
|
15
|
+
type StorySide,
|
|
16
|
+
storyOfSection,
|
|
17
|
+
} from "@folijs/core";
|
|
18
|
+
import type { FlowPage, PageFacts } from "../layout/flow.js";
|
|
19
|
+
import { type BlockOptions, PX_PER_TWIP, renderBlocks, type WordBlock } from "./blocks.js";
|
|
20
|
+
|
|
21
|
+
/** On what shows a page field's number in a drawn copy of a story. */
|
|
22
|
+
export const PAGE_FIELD_ATTR = "data-foli-page-field";
|
|
23
|
+
|
|
24
|
+
/** The class of a page's header or footer element. */
|
|
25
|
+
export const REGION_CLASS = "foli-region";
|
|
26
|
+
export const REGION_CONTENT_CLASS = "foli-region-content";
|
|
27
|
+
|
|
28
|
+
/** Word's distance from the page's edge when a section does not give one: half an inch. */
|
|
29
|
+
const EDGE = 720;
|
|
30
|
+
|
|
31
|
+
/** What the page fields of a page show. */
|
|
32
|
+
export interface FieldValues {
|
|
33
|
+
page: string;
|
|
34
|
+
pages: string;
|
|
35
|
+
sectionPages: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const PAGE_FIELDS = new Set(["PAGE", "NUMPAGES", "SECTIONPAGES"]);
|
|
39
|
+
/** `\\*` switches: `arabic` in any case, the others as Word spells their case. */
|
|
40
|
+
const FORMATS = new Map([
|
|
41
|
+
["roman", "lowerRoman"],
|
|
42
|
+
["ROMAN", "upperRoman"],
|
|
43
|
+
["alphabetic", "lowerLetter"],
|
|
44
|
+
["ALPHABETIC", "upperLetter"],
|
|
45
|
+
]);
|
|
46
|
+
|
|
47
|
+
/** A page field in a story: which, and the format its own switch asks for. */
|
|
48
|
+
interface FieldSlot {
|
|
49
|
+
name: string;
|
|
50
|
+
format: string | undefined;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Stands in the drawn story for a slot's text: the private use area is not document text. */
|
|
54
|
+
const SLOT_MARK = "\uE000";
|
|
55
|
+
const SLOT_TEXT = /\uE000([\uE100-\uF8FF])/g;
|
|
56
|
+
|
|
57
|
+
/** The page field `code` (its instruction) is, or null. */
|
|
58
|
+
function pageField(code: string): FieldSlot | null {
|
|
59
|
+
const name = code.trim().split(/\s+/)[0]?.toUpperCase() ?? "";
|
|
60
|
+
if (!PAGE_FIELDS.has(name)) return null;
|
|
61
|
+
const asked = /\\\*\s*(\S+)/.exec(code)?.[1];
|
|
62
|
+
const format = asked?.toLowerCase() === "arabic" ? "decimal" : FORMATS.get(asked ?? "");
|
|
63
|
+
return { name, format };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** What `slot` shows; the section's number format is the page number's, a switch goes first. */
|
|
67
|
+
function slotText(slot: FieldSlot, values: FieldValues, numbers: string | undefined): string {
|
|
68
|
+
const raw =
|
|
69
|
+
slot.name === "PAGE"
|
|
70
|
+
? values.page
|
|
71
|
+
: slot.name === "NUMPAGES"
|
|
72
|
+
? values.pages
|
|
73
|
+
: values.sectionPages;
|
|
74
|
+
return formatNumber(Number(raw), slot.format ?? (slot.name === "PAGE" ? numbers : undefined));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* A paragraph's inline nodes with each page field's result replaced by a mark for its slot (one
|
|
79
|
+
* run, formatted as the result's first). Only whole fields inside the list: a page field does not
|
|
80
|
+
* span paragraphs.
|
|
81
|
+
*/
|
|
82
|
+
function markInline(nodes: readonly NodeJSON[], slots: FieldSlot[]): NodeJSON[] {
|
|
83
|
+
const out: NodeJSON[] = [];
|
|
84
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
85
|
+
const node = nodes[i] as NodeJSON;
|
|
86
|
+
out.push(node);
|
|
87
|
+
if (node.type !== "fieldStart") continue;
|
|
88
|
+
let depth = 1;
|
|
89
|
+
let separator = -1;
|
|
90
|
+
let end = -1;
|
|
91
|
+
let code = "";
|
|
92
|
+
for (let j = i + 1; j < nodes.length && end < 0; j++) {
|
|
93
|
+
const inner = nodes[j] as NodeJSON;
|
|
94
|
+
if (inner.type === "fieldStart") depth++;
|
|
95
|
+
else if (inner.type === "fieldEnd" && --depth === 0) end = j;
|
|
96
|
+
else if (depth === 1 && inner.type === "fieldSeparator" && separator < 0) separator = j;
|
|
97
|
+
else if (depth === 1 && separator < 0 && inner.type === "fieldCode")
|
|
98
|
+
code += String(inner.attrs?.text ?? "");
|
|
99
|
+
}
|
|
100
|
+
const slot = end < 0 ? null : pageField(code);
|
|
101
|
+
if (!slot) continue;
|
|
102
|
+
const codeEnd = separator < 0 ? end : separator + 1;
|
|
103
|
+
const result = nodes.slice(codeEnd, end);
|
|
104
|
+
const model = result.find((n) => n.type === "text") ?? nodes[codeEnd - 1];
|
|
105
|
+
out.push(...nodes.slice(i + 1, codeEnd));
|
|
106
|
+
out.push({
|
|
107
|
+
type: "text",
|
|
108
|
+
text: SLOT_MARK + String.fromCharCode(0xe100 + slots.length),
|
|
109
|
+
...(model?.marks ? { marks: model.marks } : {}),
|
|
110
|
+
});
|
|
111
|
+
slots.push(slot);
|
|
112
|
+
out.push(nodes[end] as NodeJSON);
|
|
113
|
+
i = end;
|
|
114
|
+
}
|
|
115
|
+
return out;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** `node` with the page fields in it marked. */
|
|
119
|
+
function marked(node: NodeJSON, slots: FieldSlot[]): NodeJSON {
|
|
120
|
+
if (!node.content) return node;
|
|
121
|
+
const content =
|
|
122
|
+
node.type === "paragraph"
|
|
123
|
+
? markInline(node.content, slots)
|
|
124
|
+
: node.content.map((child) => marked(child, slots));
|
|
125
|
+
return { ...node, content };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** A copy of `template` with each slot's text in it. */
|
|
129
|
+
function filledCopy(template: HTMLElement, texts: readonly string[]): HTMLElement {
|
|
130
|
+
const copy = template.cloneNode(true) as HTMLElement;
|
|
131
|
+
if (!texts.length) return copy;
|
|
132
|
+
const walker = template.ownerDocument.createTreeWalker(copy, NodeFilter.SHOW_TEXT);
|
|
133
|
+
for (let node = walker.nextNode(); node; node = walker.nextNode()) {
|
|
134
|
+
const text = node as Text;
|
|
135
|
+
// A page's own number: not document text (find leaves it alone).
|
|
136
|
+
if (text.data.replace(SLOT_TEXT, "") === "" && text.data)
|
|
137
|
+
text.parentElement?.setAttribute(PAGE_FIELD_ATTR, "");
|
|
138
|
+
if (text.data.includes(SLOT_MARK))
|
|
139
|
+
text.data = text.data.replace(
|
|
140
|
+
SLOT_TEXT,
|
|
141
|
+
(_, slot: string) => texts[slot.charCodeAt(0) - 0xe100] ?? "",
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
return copy;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** A story with some values: its height, and its content to draw (made when asked for). */
|
|
148
|
+
interface Drawn {
|
|
149
|
+
height: number;
|
|
150
|
+
readonly content: HTMLElement;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** A story once drawn, its page fields left to fill: what every page starts from. */
|
|
154
|
+
interface StoryDrawing {
|
|
155
|
+
slots: FieldSlot[];
|
|
156
|
+
template: HTMLElement;
|
|
157
|
+
/** The last block's space after, which ends the story. */
|
|
158
|
+
after: number;
|
|
159
|
+
drawn: Map<string, Drawn>;
|
|
160
|
+
heights: Map<string, number>;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Per story (an object replaced when the story changes), what was measured and drawn of it. */
|
|
164
|
+
export type RegionCache = WeakMap<StoryJSON, StoryDrawing>;
|
|
165
|
+
|
|
166
|
+
/** A section's page as the regions need it, in CSS px. */
|
|
167
|
+
interface Edges {
|
|
168
|
+
top: number;
|
|
169
|
+
bottom: number;
|
|
170
|
+
/** Whether a header or footer can push the text (a negative margin is exact, as in Word). */
|
|
171
|
+
pushesTop: boolean;
|
|
172
|
+
pushesBottom: boolean;
|
|
173
|
+
header: number;
|
|
174
|
+
footer: number;
|
|
175
|
+
left: number;
|
|
176
|
+
right: number;
|
|
177
|
+
height: number;
|
|
178
|
+
width: number;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function edgesOf(section: SectionJSON): Edges {
|
|
182
|
+
const m = section.margins ?? {};
|
|
183
|
+
const top = m.top ?? 1440;
|
|
184
|
+
const bottom = m.bottom ?? 1440;
|
|
185
|
+
const left = (m.left ?? 1440) + (m.gutter ?? 0);
|
|
186
|
+
const right = m.right ?? 1440;
|
|
187
|
+
const width = section.pageSize?.width ?? 12_240;
|
|
188
|
+
return {
|
|
189
|
+
top: Math.abs(top) * PX_PER_TWIP,
|
|
190
|
+
bottom: Math.abs(bottom) * PX_PER_TWIP,
|
|
191
|
+
pushesTop: top >= 0,
|
|
192
|
+
pushesBottom: bottom >= 0,
|
|
193
|
+
header: (m.header ?? EDGE) * PX_PER_TWIP,
|
|
194
|
+
footer: (m.footer ?? EDGE) * PX_PER_TWIP,
|
|
195
|
+
left: left * PX_PER_TWIP,
|
|
196
|
+
right: right * PX_PER_TWIP,
|
|
197
|
+
height: (section.pageSize?.height ?? 15_840) * PX_PER_TWIP,
|
|
198
|
+
width: Math.max(width - left - right, 1) * PX_PER_TWIP,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Lays `blocks` out as a story: spacing as between the paragraphs of a page. */
|
|
203
|
+
export function stack(blocks: readonly WordBlock[], parent: HTMLElement, adds: boolean): number {
|
|
204
|
+
let previous = 0;
|
|
205
|
+
blocks.forEach((block, i) => {
|
|
206
|
+
const gap = !i
|
|
207
|
+
? block.before
|
|
208
|
+
: adds
|
|
209
|
+
? previous + block.before
|
|
210
|
+
: Math.max(previous, block.before);
|
|
211
|
+
block.element.style.marginTop = `${gap}px`;
|
|
212
|
+
block.element.style.marginBottom = "0";
|
|
213
|
+
previous = block.after;
|
|
214
|
+
parent.append(block.element);
|
|
215
|
+
});
|
|
216
|
+
return previous;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export interface RegionOptions extends Omit<BlockOptions, "resolver"> {
|
|
220
|
+
/** Where stories are measured (hidden, for a moment). */
|
|
221
|
+
measureIn(): HTMLElement;
|
|
222
|
+
/** Layout px per measured px's inverse: what a scaled ancestor multiplies sizes by. */
|
|
223
|
+
scale?(): number;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** What the layout of a document's pages needs to know of their headers and footers. */
|
|
227
|
+
export class PageRegions {
|
|
228
|
+
/** The page count and each section's, as of the last flow (the fields' values). */
|
|
229
|
+
private pages = 1;
|
|
230
|
+
private sectionPages = new Map<number, number>();
|
|
231
|
+
private edges: Edges[] = [];
|
|
232
|
+
/** The pages of the last flow. */
|
|
233
|
+
private flowed: readonly PageFacts[] = [];
|
|
234
|
+
private document: Omit<FoliDocument, "content"> | null = null;
|
|
235
|
+
/** The document's sections, in order. */
|
|
236
|
+
private sections: readonly SectionJSON[] = [];
|
|
237
|
+
private resolver: Resolver | null = null;
|
|
238
|
+
|
|
239
|
+
constructor(
|
|
240
|
+
private readonly doc: Document,
|
|
241
|
+
private readonly options: RegionOptions,
|
|
242
|
+
/** Kept across layouts by the view; replaced when what stories look like changes. */
|
|
243
|
+
public cache: RegionCache,
|
|
244
|
+
) {}
|
|
245
|
+
|
|
246
|
+
/** What is laid out now. */
|
|
247
|
+
setDocument(
|
|
248
|
+
document: Omit<FoliDocument, "content">,
|
|
249
|
+
sections: readonly SectionJSON[],
|
|
250
|
+
resolver: Resolver,
|
|
251
|
+
): void {
|
|
252
|
+
this.document = document;
|
|
253
|
+
this.sections = sections;
|
|
254
|
+
this.resolver = resolver;
|
|
255
|
+
this.edges = [];
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/** A story by id: ids come from the file, and may name what every object inherits. */
|
|
259
|
+
private story(id: string): StoryJSON | undefined {
|
|
260
|
+
const stories = this.document?.stories;
|
|
261
|
+
return stories && Object.hasOwn(stories, id) ? stories[id] : undefined;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
private edgesAt(index: number): Edges {
|
|
265
|
+
let found = this.edges[index];
|
|
266
|
+
if (!found) {
|
|
267
|
+
found = edgesOf(this.sections[index] ?? {});
|
|
268
|
+
this.edges[index] = found;
|
|
269
|
+
}
|
|
270
|
+
return found;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** The story a page of section `index` shows, as `first` and its number say. */
|
|
274
|
+
storyId(index: number, side: StorySide, page: { first: boolean; number: number }): string | null {
|
|
275
|
+
const section = this.sections[index];
|
|
276
|
+
if (!section) return null;
|
|
277
|
+
const even = this.document?.settings?.evenAndOddHeaders === true;
|
|
278
|
+
const variant = pageVariant(section, page, even);
|
|
279
|
+
const found = storyOfSection(this.sections, index, side, variant);
|
|
280
|
+
return found && this.story(found.id) ? found.id : null;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
private entry(story: StoryJSON): StoryDrawing {
|
|
284
|
+
let entry = this.cache.get(story);
|
|
285
|
+
if (!entry) {
|
|
286
|
+
const slots: FieldSlot[] = [];
|
|
287
|
+
const content = marked(story.content, slots);
|
|
288
|
+
const template = this.doc.createElement("div");
|
|
289
|
+
template.className = REGION_CONTENT_CLASS;
|
|
290
|
+
template.style.paddingTop = "0.01px";
|
|
291
|
+
const blocks = renderBlocks({ ...this.document, content } as FoliDocument, this.doc, {
|
|
292
|
+
...this.options,
|
|
293
|
+
...(this.resolver ? { resolver: this.resolver } : {}),
|
|
294
|
+
});
|
|
295
|
+
const after = stack(blocks, template, !!this.resolver?.addsParagraphSpacing);
|
|
296
|
+
// The last space after is the story's too: a footer sits above it.
|
|
297
|
+
template.style.paddingBottom = `${after}px`;
|
|
298
|
+
entry = { slots, template, after, drawn: new Map(), heights: new Map() };
|
|
299
|
+
this.cache.set(story, entry);
|
|
300
|
+
}
|
|
301
|
+
return entry;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
private valuesFor(index: number, number: number): FieldValues {
|
|
305
|
+
return {
|
|
306
|
+
page: String(number),
|
|
307
|
+
pages: String(this.pages),
|
|
308
|
+
sectionPages: String(this.sectionPages.get(index) ?? 1),
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
private drawn(id: string, index: number, number: number): Drawn {
|
|
313
|
+
const entry = this.entry(this.story(id) as StoryJSON);
|
|
314
|
+
const edges = this.edgesAt(index);
|
|
315
|
+
const values = this.valuesFor(index, number);
|
|
316
|
+
const numbers = this.sections[index]?.pageNumbers?.format;
|
|
317
|
+
const texts = entry.slots.map((slot) => slotText(slot, values, numbers));
|
|
318
|
+
const key = `${edges.width}|${texts.join("|")}`;
|
|
319
|
+
let found = entry.drawn.get(key);
|
|
320
|
+
if (!found) {
|
|
321
|
+
let content: HTMLElement | null = null;
|
|
322
|
+
const made = () => {
|
|
323
|
+
content ??= filledCopy(entry.template, texts);
|
|
324
|
+
return content;
|
|
325
|
+
};
|
|
326
|
+
// shortcut: numbers of as many digits are taken to wrap alike (digits are about as wide);
|
|
327
|
+
// measuring each page's costs a layout per page per keystroke. Letters are not: `iii`, `vii`.
|
|
328
|
+
const shape = `${edges.width}|${texts
|
|
329
|
+
.map((text) => (/^\d*$/.test(text) ? text.length : text))
|
|
330
|
+
.join(" ")}`;
|
|
331
|
+
let height = entry.heights.get(shape);
|
|
332
|
+
if (height === undefined) {
|
|
333
|
+
height = this.measure(made(), edges.width, entry.after);
|
|
334
|
+
entry.heights.set(shape, height);
|
|
335
|
+
}
|
|
336
|
+
found = {
|
|
337
|
+
height,
|
|
338
|
+
get content() {
|
|
339
|
+
return made();
|
|
340
|
+
},
|
|
341
|
+
};
|
|
342
|
+
entry.drawn.set(key, found);
|
|
343
|
+
}
|
|
344
|
+
return found;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/** A story's height: down to its last block (floating pictures add none) and its space after. */
|
|
348
|
+
private measure(content: HTMLElement, width: number, after: number): number {
|
|
349
|
+
const { style } = content;
|
|
350
|
+
style.position = "absolute";
|
|
351
|
+
style.visibility = "hidden";
|
|
352
|
+
style.width = `${width}px`;
|
|
353
|
+
this.options.measureIn().append(content);
|
|
354
|
+
const last = content.lastElementChild;
|
|
355
|
+
const scale = this.options.scale?.() ?? 1;
|
|
356
|
+
const top = content.getBoundingClientRect().top;
|
|
357
|
+
const bottom = last ? last.getBoundingClientRect().bottom : top;
|
|
358
|
+
content.remove();
|
|
359
|
+
style.position = "";
|
|
360
|
+
style.visibility = "";
|
|
361
|
+
style.width = "";
|
|
362
|
+
return (bottom - top) / scale + after;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/** How tall the header or footer of a page is (0: none). */
|
|
366
|
+
private heightOf(index: number, side: StorySide, page: FlowPage): number {
|
|
367
|
+
const id = this.storyId(index, side, page);
|
|
368
|
+
return id ? this.drawn(id, index, page.number).height : 0;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/** The margins of a page, a header or footer taller than its own pushing the text. */
|
|
372
|
+
margins(index: number, page: FlowPage): { top: number; bottom: number } {
|
|
373
|
+
const edges = this.edgesAt(index);
|
|
374
|
+
const header = edges.pushesTop ? this.heightOf(index, "header", page) : 0;
|
|
375
|
+
const footer = edges.pushesBottom ? this.heightOf(index, "footer", page) : 0;
|
|
376
|
+
return {
|
|
377
|
+
top: header ? Math.max(edges.top, edges.header + header) : edges.top,
|
|
378
|
+
bottom: footer ? Math.max(edges.bottom, edges.footer + footer) : edges.bottom,
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/** The height of a page's text. */
|
|
383
|
+
boxHeight(index: number, page: FlowPage): number {
|
|
384
|
+
const { top, bottom } = this.margins(index, page);
|
|
385
|
+
return this.edgesAt(index).height - top - bottom;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Takes the page counts of a finished flow. Whether its pages' text heights change with them
|
|
390
|
+
* (a `NUMPAGES` that wraps): the flow is then made again, at most twice (ADR-0007).
|
|
391
|
+
*/
|
|
392
|
+
settle(pages: readonly PageFacts[]): boolean {
|
|
393
|
+
const heights = () =>
|
|
394
|
+
pages
|
|
395
|
+
.filter((p) => p.sectionPages)
|
|
396
|
+
.map((p) => this.boxHeight(p.section, p))
|
|
397
|
+
.join(" ");
|
|
398
|
+
const before = heights();
|
|
399
|
+
this.flowed = pages;
|
|
400
|
+
this.pages = pages.length;
|
|
401
|
+
this.sectionPages = new Map();
|
|
402
|
+
for (const page of pages)
|
|
403
|
+
if (page.sectionPages) this.sectionPages.set(page.section, page.sectionPages);
|
|
404
|
+
return heights() !== before;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* The header and footer heights of what a section can show and of every flowed page (a page
|
|
409
|
+
* number can wrap from some page on): a change means a new flow.
|
|
410
|
+
*/
|
|
411
|
+
key(): string {
|
|
412
|
+
const sides = ["header", "footer"] as const;
|
|
413
|
+
const sampled = this.sections.flatMap((_, index) =>
|
|
414
|
+
sides.flatMap((side) =>
|
|
415
|
+
[
|
|
416
|
+
{ first: true, number: 1 },
|
|
417
|
+
{ first: false, number: 1 },
|
|
418
|
+
{ first: false, number: 2 },
|
|
419
|
+
].map((page) => this.heightOf(index, side, { ...page, index: 0 })),
|
|
420
|
+
),
|
|
421
|
+
);
|
|
422
|
+
const flowed = this.flowed
|
|
423
|
+
.filter((page) => page.sectionPages && page.section < this.sections.length)
|
|
424
|
+
.flatMap((page) => sides.map((side) => this.heightOf(page.section, side, page)));
|
|
425
|
+
return [...sampled, "|", ...flowed].join(" ");
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/** What a page's region shows: equal keys, the same drawing. */
|
|
429
|
+
drawing(
|
|
430
|
+
index: number,
|
|
431
|
+
side: StorySide,
|
|
432
|
+
page: FlowPage,
|
|
433
|
+
): { id: string; content: Drawn; story: StoryJSON } | null {
|
|
434
|
+
const id = this.storyId(index, side, page);
|
|
435
|
+
if (!id) return null;
|
|
436
|
+
return {
|
|
437
|
+
id,
|
|
438
|
+
content: this.drawn(id, index, page.number),
|
|
439
|
+
story: this.story(id) as StoryJSON,
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/** A new header or footer element for a page of section `index`. */
|
|
444
|
+
element(index: number, side: StorySide): HTMLElement {
|
|
445
|
+
const edges = this.edgesAt(index);
|
|
446
|
+
const region = this.doc.createElement("div");
|
|
447
|
+
region.className = `${REGION_CLASS} foli-${side}`;
|
|
448
|
+
region.setAttribute("contenteditable", "false");
|
|
449
|
+
const { style } = region;
|
|
450
|
+
// As large as the page: floating pictures in it are placed against the page.
|
|
451
|
+
style.position = "absolute";
|
|
452
|
+
style.inset = "0";
|
|
453
|
+
style.boxSizing = "border-box";
|
|
454
|
+
style.pointerEvents = "none";
|
|
455
|
+
// Not part of what the body's selection shows.
|
|
456
|
+
style.userSelect = "none";
|
|
457
|
+
style.setProperty("-webkit-user-select", "none");
|
|
458
|
+
style.paddingLeft = `${edges.left}px`;
|
|
459
|
+
style.paddingRight = `${edges.right}px`;
|
|
460
|
+
if (side === "header") style.paddingTop = `${edges.header}px`;
|
|
461
|
+
else {
|
|
462
|
+
style.display = "flex";
|
|
463
|
+
style.flexDirection = "column";
|
|
464
|
+
style.justifyContent = "flex-end";
|
|
465
|
+
style.paddingBottom = `${edges.footer}px`;
|
|
466
|
+
}
|
|
467
|
+
return region;
|
|
468
|
+
}
|
|
469
|
+
}
|