@folijs/view 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +59 -0
- package/dist/clipboard.d.ts +3 -1
- package/dist/clipboard.d.ts.map +1 -1
- package/dist/clipboard.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/layout/flow.d.ts +195 -20
- package/dist/layout/flow.d.ts.map +1 -1
- package/dist/layout/flow.js +790 -102
- package/dist/layout/flow.js.map +1 -1
- package/dist/print.d.ts +22 -0
- package/dist/print.d.ts.map +1 -0
- package/dist/print.js +31 -0
- package/dist/print.js.map +1 -0
- package/dist/view.d.ts +407 -8
- package/dist/view.d.ts.map +1 -1
- package/dist/view.js +2956 -263
- package/dist/view.js.map +1 -1
- package/dist/word/blocks.d.ts +99 -2
- package/dist/word/blocks.d.ts.map +1 -1
- package/dist/word/blocks.js +535 -31
- package/dist/word/blocks.js.map +1 -1
- package/dist/word/columns.d.ts +23 -0
- package/dist/word/columns.d.ts.map +1 -0
- package/dist/word/columns.js +51 -0
- package/dist/word/columns.js.map +1 -0
- package/dist/word/controls.d.ts +63 -0
- package/dist/word/controls.d.ts.map +1 -0
- package/dist/word/controls.js +411 -0
- package/dist/word/controls.js.map +1 -0
- package/dist/word/edit.d.ts +32 -11
- package/dist/word/edit.d.ts.map +1 -1
- package/dist/word/edit.js +215 -33
- package/dist/word/edit.js.map +1 -1
- package/dist/word/matches.d.ts +23 -0
- package/dist/word/matches.d.ts.map +1 -0
- package/dist/word/matches.js +97 -0
- package/dist/word/matches.js.map +1 -0
- package/dist/word/notes.d.ts +105 -0
- package/dist/word/notes.d.ts.map +1 -0
- package/dist/word/notes.js +392 -0
- package/dist/word/notes.js.map +1 -0
- package/dist/word/pages.d.ts +26 -13
- package/dist/word/pages.d.ts.map +1 -1
- package/dist/word/pages.js +439 -203
- package/dist/word/pages.js.map +1 -1
- package/dist/word/regions.d.ts +113 -0
- package/dist/word/regions.d.ts.map +1 -0
- package/dist/word/regions.js +374 -0
- package/dist/word/regions.js.map +1 -0
- package/dist/word/table.d.ts +79 -0
- package/dist/word/table.d.ts.map +1 -0
- package/dist/word/table.js +210 -0
- package/dist/word/table.js.map +1 -0
- package/package.json +2 -2
- package/src/clipboard.ts +3 -0
- package/src/index.ts +8 -1
- package/src/layout/flow.ts +948 -111
- package/src/print.ts +48 -0
- package/src/view.ts +3291 -273
- package/src/word/blocks.ts +611 -28
- package/src/word/columns.ts +66 -0
- package/src/word/controls.ts +447 -0
- package/src/word/edit.ts +253 -30
- package/src/word/matches.ts +99 -0
- package/src/word/notes.ts +451 -0
- package/src/word/pages.ts +524 -192
- package/src/word/regions.ts +469 -0
- package/src/word/table.ts +267 -0
package/src/word/blocks.ts
CHANGED
|
@@ -5,21 +5,276 @@
|
|
|
5
5
|
* createElement/textContent only (never innerHTML); the page layout is in `pages.ts`.
|
|
6
6
|
*/
|
|
7
7
|
import {
|
|
8
|
+
type FieldDisplay,
|
|
8
9
|
type FoliDocument,
|
|
10
|
+
fieldDisplay,
|
|
9
11
|
type ListMarker,
|
|
10
12
|
type MarkJSON,
|
|
11
13
|
type NodeJSON,
|
|
12
14
|
type ParagraphFormat,
|
|
13
15
|
Resolver,
|
|
16
|
+
type ResourceJSON,
|
|
14
17
|
type RunFormat,
|
|
15
18
|
type SectionJSON,
|
|
16
19
|
} from "@folijs/core";
|
|
17
20
|
|
|
18
21
|
/** Marks the <br> of an explicit page break (the page flow ends the page after its line). */
|
|
19
22
|
export const WORD_PAGE_BREAK_ATTR = "data-page-break";
|
|
23
|
+
/** Marks the <br> of an explicit column break (the flow ends the column after its line). */
|
|
24
|
+
export const WORD_COLUMN_BREAK_ATTR = "data-column-break";
|
|
20
25
|
|
|
21
26
|
/** CSS px per twip (1/1440 inch; CSS: 96 px per inch). */
|
|
22
27
|
export const PX_PER_TWIP = 96 / 1440;
|
|
28
|
+
/** EMU (DrawingML's unit: 914 400 per inch) per CSS px. */
|
|
29
|
+
export const EMU_PER_PX = 914_400 / 96;
|
|
30
|
+
|
|
31
|
+
/** The URL to show a picture's image from (its resource key), if one is known. */
|
|
32
|
+
export type ImageSource = (key: string) => string | undefined;
|
|
33
|
+
|
|
34
|
+
/** Marks a picture's element (its resource key). */
|
|
35
|
+
export const PICTURE_ATTR = "data-foli-picture";
|
|
36
|
+
|
|
37
|
+
/** The accessible name of a picture with no alt text that is not decorative. */
|
|
38
|
+
export const NO_ALT_LABEL = "Picture without alt text";
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The URL a resource gives on its own: an image `data:` URL, or a web or `blob:` URL; none for
|
|
42
|
+
* an image in a package part (the app's resolver gives one) or anything else.
|
|
43
|
+
*/
|
|
44
|
+
export function ownImageUrl(resource: ResourceJSON | undefined): string | undefined {
|
|
45
|
+
const data = resource?.data;
|
|
46
|
+
if (data && /^data:image\//i.test(data)) return data;
|
|
47
|
+
const url = resource?.url;
|
|
48
|
+
return url && /^(https?|blob):/i.test(url) ? url : undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* A picture as shown: its image at its size, or, when no URL is known for it, a box of that size
|
|
53
|
+
* with its alt text. A picture with no alt text that is not decorative is marked (`data-foli-
|
|
54
|
+
* no-alt`); a decorative one has an empty alt.
|
|
55
|
+
*/
|
|
56
|
+
export function pictureElement(
|
|
57
|
+
attrs: Record<string, unknown>,
|
|
58
|
+
doc: Document,
|
|
59
|
+
image?: ImageSource,
|
|
60
|
+
): HTMLElement {
|
|
61
|
+
const key = String(attrs.resource ?? "");
|
|
62
|
+
const width = `${Number(attrs.width) / EMU_PER_PX}px`;
|
|
63
|
+
const height = `${Number(attrs.height) / EMU_PER_PX}px`;
|
|
64
|
+
const decorative = attrs.decorative === true;
|
|
65
|
+
const own = String(attrs.alt ?? "");
|
|
66
|
+
// Only a decorative picture is silent; one missing its alt text says so.
|
|
67
|
+
const alt = decorative ? "" : own || NO_ALT_LABEL;
|
|
68
|
+
const src = image?.(key);
|
|
69
|
+
let element: HTMLElement;
|
|
70
|
+
if (src) {
|
|
71
|
+
const img = doc.createElement("img");
|
|
72
|
+
img.src = src;
|
|
73
|
+
img.alt = alt;
|
|
74
|
+
img.draggable = false;
|
|
75
|
+
// A web URL in a document is fetched as it is shown: no referrer to go with it.
|
|
76
|
+
img.referrerPolicy = "no-referrer";
|
|
77
|
+
element = img;
|
|
78
|
+
} else {
|
|
79
|
+
element = doc.createElement("span");
|
|
80
|
+
element.setAttribute("role", "img");
|
|
81
|
+
if (alt) element.setAttribute("aria-label", alt);
|
|
82
|
+
else element.setAttribute("aria-hidden", "true");
|
|
83
|
+
element.style.boxSizing = "border-box";
|
|
84
|
+
element.style.border = "1px dashed GrayText";
|
|
85
|
+
}
|
|
86
|
+
element.setAttribute(PICTURE_ATTR, key);
|
|
87
|
+
if (!own && !decorative) element.dataset.foliNoAlt = "";
|
|
88
|
+
if (attrs.title) element.title = String(attrs.title);
|
|
89
|
+
// In the line at its size, whatever the app's CSS says of images (`display: block`…).
|
|
90
|
+
const style = element.style;
|
|
91
|
+
style.display = src ? "inline" : "inline-block";
|
|
92
|
+
style.verticalAlign = "baseline";
|
|
93
|
+
style.maxWidth = "none";
|
|
94
|
+
style.maxHeight = "none";
|
|
95
|
+
style.width = width;
|
|
96
|
+
style.height = height;
|
|
97
|
+
return element;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Marks the element holding a floating picture (its wrapping), drawn at its paragraph's start. */
|
|
101
|
+
export const FLOAT_ATTR = "data-foli-float";
|
|
102
|
+
|
|
103
|
+
/** A section's page, in CSS px: what a floating picture is positioned against. */
|
|
104
|
+
export interface FloatGeometry {
|
|
105
|
+
readonly pageWidth: number;
|
|
106
|
+
readonly pageHeight: number;
|
|
107
|
+
readonly marginLeft: number;
|
|
108
|
+
readonly marginTop: number;
|
|
109
|
+
readonly textWidth: number;
|
|
110
|
+
readonly textHeight: number;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** The page geometry of `section` (Word's Letter page and margins where it does not say). */
|
|
114
|
+
export function floatGeometry(section: SectionJSON | undefined): FloatGeometry {
|
|
115
|
+
const m = section?.margins ?? {};
|
|
116
|
+
const width = section?.pageSize?.width ?? 12_240;
|
|
117
|
+
const height = section?.pageSize?.height ?? 15_840;
|
|
118
|
+
const left = (m.left ?? 1440) + (m.gutter ?? 0);
|
|
119
|
+
const top = Math.abs(m.top ?? 1440);
|
|
120
|
+
return {
|
|
121
|
+
pageWidth: width * PX_PER_TWIP,
|
|
122
|
+
pageHeight: height * PX_PER_TWIP,
|
|
123
|
+
marginLeft: left * PX_PER_TWIP,
|
|
124
|
+
marginTop: top * PX_PER_TWIP,
|
|
125
|
+
textWidth: (width - left - (m.right ?? 1440)) * PX_PER_TWIP,
|
|
126
|
+
textHeight: (height - top - Math.abs(m.bottom ?? 1440)) * PX_PER_TWIP,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
type FloatPosition = { relativeTo: string; offset?: number; align?: string };
|
|
131
|
+
|
|
132
|
+
/** Where a float's position is measured from, and the box its alignment is in (px). */
|
|
133
|
+
function horizontalBox(relativeTo: string, g: FloatGeometry): [start: number, size: number] {
|
|
134
|
+
const right = g.pageWidth - g.marginLeft - g.textWidth;
|
|
135
|
+
switch (relativeTo) {
|
|
136
|
+
case "page":
|
|
137
|
+
return [0, g.pageWidth];
|
|
138
|
+
case "leftMargin":
|
|
139
|
+
case "insideMargin":
|
|
140
|
+
return [0, g.marginLeft];
|
|
141
|
+
case "rightMargin":
|
|
142
|
+
case "outsideMargin":
|
|
143
|
+
return [g.marginLeft + g.textWidth, right];
|
|
144
|
+
default:
|
|
145
|
+
// margin, column, character (shortcut: the column).
|
|
146
|
+
return [g.marginLeft, g.textWidth];
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function verticalBox(relativeTo: string, g: FloatGeometry): [start: number, size: number] {
|
|
151
|
+
const bottom = g.pageHeight - g.marginTop - g.textHeight;
|
|
152
|
+
switch (relativeTo) {
|
|
153
|
+
case "page":
|
|
154
|
+
return [0, g.pageHeight];
|
|
155
|
+
case "topMargin":
|
|
156
|
+
case "insideMargin":
|
|
157
|
+
return [0, g.marginTop];
|
|
158
|
+
case "bottomMargin":
|
|
159
|
+
case "outsideMargin":
|
|
160
|
+
return [g.pageHeight - bottom, bottom];
|
|
161
|
+
default:
|
|
162
|
+
return [g.marginTop, g.textHeight];
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Where `size` px goes in `[start, start + box)` by offset (EMU) or alignment. */
|
|
167
|
+
function placed(position: FloatPosition, [start, box]: [number, number], size: number): number {
|
|
168
|
+
if (typeof position.offset === "number") return start + position.offset / EMU_PER_PX;
|
|
169
|
+
const { align } = position;
|
|
170
|
+
if (align === "right" || align === "bottom" || align === "outside") return start + box - size;
|
|
171
|
+
if (align === "center") return start + (box - size) / 2;
|
|
172
|
+
return start;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Where a float's left edge is, in px from the column's left edge. */
|
|
176
|
+
function floatLeft(h: FloatPosition, width: number, g: FloatGeometry): number {
|
|
177
|
+
return placed(h, horizontalBox(h.relativeTo, g), width) - g.marginLeft;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Where a float's top edge is: from its paragraph's top (`paragraph`), or from the page's top
|
|
182
|
+
* (`page`) for a position against the page or its margins.
|
|
183
|
+
*/
|
|
184
|
+
function floatTop(
|
|
185
|
+
v: FloatPosition,
|
|
186
|
+
height: number,
|
|
187
|
+
g: FloatGeometry,
|
|
188
|
+
): { paragraph?: number; page?: number } {
|
|
189
|
+
if (v.relativeTo === "paragraph" || v.relativeTo === "line")
|
|
190
|
+
return { paragraph: typeof v.offset === "number" ? v.offset / EMU_PER_PX : 0 };
|
|
191
|
+
return { page: placed(v, verticalBox(v.relativeTo, g), height) };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* A floating picture (10.9, ADR-0006) as CSS floats lay it out, put first in its anchor
|
|
196
|
+
* paragraph: `holder` holds the picture (`FLOAT_ATTR`), `nodes` go in the paragraph (a
|
|
197
|
+
* zero-width spacer float before it gives its offset from the paragraph's top).
|
|
198
|
+
* - Square (tight and through shown as square): a float on the side the picture is on, its
|
|
199
|
+
* distance from the text as its margin; text goes on the other side only.
|
|
200
|
+
* - Top and bottom: a float as wide as the paragraph, the picture in it at its position.
|
|
201
|
+
* - Behind and in front of text: absolutely positioned on the page (z-index -1 or 1; the page is
|
|
202
|
+
* the containing block and its own stacking context).
|
|
203
|
+
* shortcut: a wrapping picture placed against the page or its margins vertically is drawn at
|
|
204
|
+
* its paragraph's top; horizontal positions are against the paragraph's box (its indents in);
|
|
205
|
+
* all against `geometry` (one section's page), not each page's. A float's top is never above an
|
|
206
|
+
* earlier float's (CSS), and `clear` stacks it below earlier floats on its side: several floats
|
|
207
|
+
* in a paragraph, or one above a float of an earlier paragraph, add their offsets down and do
|
|
208
|
+
* not sit side by side.
|
|
209
|
+
*/
|
|
210
|
+
export function floatElements(
|
|
211
|
+
attrs: Record<string, unknown>,
|
|
212
|
+
doc: Document,
|
|
213
|
+
image: ImageSource | undefined,
|
|
214
|
+
geometry: FloatGeometry,
|
|
215
|
+
): { nodes: HTMLElement[]; holder: HTMLElement } {
|
|
216
|
+
const float = attrs.float as {
|
|
217
|
+
horizontal: FloatPosition;
|
|
218
|
+
vertical: FloatPosition;
|
|
219
|
+
wrap: string;
|
|
220
|
+
side?: string;
|
|
221
|
+
distance?: { top?: number; bottom?: number; left?: number; right?: number };
|
|
222
|
+
};
|
|
223
|
+
const width = Number(attrs.width) / EMU_PER_PX;
|
|
224
|
+
const height = Number(attrs.height) / EMU_PER_PX;
|
|
225
|
+
const distance = (side: "top" | "bottom" | "left" | "right") =>
|
|
226
|
+
(float.distance?.[side] ?? 0) / EMU_PER_PX;
|
|
227
|
+
const picture = pictureElement(attrs, doc, image);
|
|
228
|
+
picture.style.display = "block";
|
|
229
|
+
const holder = doc.createElement("span");
|
|
230
|
+
holder.setAttribute(FLOAT_ATTR, float.wrap);
|
|
231
|
+
holder.append(picture);
|
|
232
|
+
const style = holder.style;
|
|
233
|
+
style.display = "block";
|
|
234
|
+
const x = floatLeft(float.horizontal, width, geometry);
|
|
235
|
+
const top = floatTop(float.vertical, height, geometry);
|
|
236
|
+
if (float.wrap === "behind" || float.wrap === "inFront") {
|
|
237
|
+
style.position = "absolute";
|
|
238
|
+
style.zIndex = float.wrap === "behind" ? "-1" : "1";
|
|
239
|
+
style.left = `${geometry.marginLeft + x}px`;
|
|
240
|
+
// Against the paragraph: where it would be (its static position), moved by its offset.
|
|
241
|
+
if (top.page !== undefined) style.top = `${top.page}px`;
|
|
242
|
+
else style.marginTop = `${top.paragraph}px`;
|
|
243
|
+
return { nodes: [holder], holder };
|
|
244
|
+
}
|
|
245
|
+
// Its distance from the text above and below as its margins: lines there go around it too.
|
|
246
|
+
const above = distance("top");
|
|
247
|
+
const down = (top.paragraph ?? 0) - above;
|
|
248
|
+
const spacer = doc.createElement("span");
|
|
249
|
+
spacer.setAttribute("aria-hidden", "true");
|
|
250
|
+
spacer.style.width = "0";
|
|
251
|
+
spacer.style.height = `${Math.max(0, down)}px`;
|
|
252
|
+
style.marginTop = `${above + Math.min(0, down)}px`;
|
|
253
|
+
style.marginBottom = `${distance("bottom")}px`;
|
|
254
|
+
if (float.wrap === "topAndBottom") {
|
|
255
|
+
spacer.style.float = "left";
|
|
256
|
+
style.float = "left";
|
|
257
|
+
style.clear = "both";
|
|
258
|
+
style.width = "100%";
|
|
259
|
+
picture.style.marginLeft = `${x}px`;
|
|
260
|
+
return { nodes: [spacer, holder], holder };
|
|
261
|
+
}
|
|
262
|
+
// Text on the side with more room (or the side `side` names): the picture floats on the other.
|
|
263
|
+
const right =
|
|
264
|
+
float.side === "left" || (float.side !== "right" && x + width / 2 > geometry.textWidth / 2);
|
|
265
|
+
const side = right ? "right" : "left";
|
|
266
|
+
spacer.style.float = side;
|
|
267
|
+
style.float = side;
|
|
268
|
+
style.clear = side;
|
|
269
|
+
if (right) {
|
|
270
|
+
style.marginRight = `${geometry.textWidth - x - width}px`;
|
|
271
|
+
style.marginLeft = `${distance("left")}px`;
|
|
272
|
+
} else {
|
|
273
|
+
style.marginLeft = `${x}px`;
|
|
274
|
+
style.marginRight = `${distance("right")}px`;
|
|
275
|
+
}
|
|
276
|
+
return { nodes: [spacer, holder], holder };
|
|
277
|
+
}
|
|
23
278
|
|
|
24
279
|
/** Which version of tracked changes to show. */
|
|
25
280
|
/** How tracked changes show (`@folijs/core`'s, for `renderDocument` too). */
|
|
@@ -77,12 +332,65 @@ export function changeElement(
|
|
|
77
332
|
export interface BlockOptions {
|
|
78
333
|
/** `markup` (the default): insertions underlined, deletions struck through; `simple`, `final`: as if accepted. */
|
|
79
334
|
review?: ReviewView;
|
|
335
|
+
/** Fields show their codes instead of their results (Word's Alt+F9). */
|
|
336
|
+
fieldCodes?: boolean;
|
|
80
337
|
/**
|
|
81
338
|
* The ratio of a font's normal line height to its size (`fontFamily`: a CSS font-family
|
|
82
339
|
* list). Word's single line spacing is the font's own line height; multiples scale it.
|
|
83
340
|
* Measured by the page layout; 1.2 when nothing measures (no layout engine).
|
|
84
341
|
*/
|
|
85
342
|
lineRatio?: (fontFamily: string) => number;
|
|
343
|
+
/**
|
|
344
|
+
* The URL to show a picture's image from (`key` in `resources`): the app's (a package part's
|
|
345
|
+
* object URL…); `null` (or "") shows the picture's box. Left out, or undefined for a key: the
|
|
346
|
+
* resource's own image `data:` URL or web URL (fetched as it is shown).
|
|
347
|
+
*/
|
|
348
|
+
resource?: (key: string, resource: ResourceJSON) => string | null | undefined;
|
|
349
|
+
/** The document's resolver, when the caller has one (a story of it, drawn per page). */
|
|
350
|
+
resolver?: Resolver;
|
|
351
|
+
/** What note references show, by story, and what a note's own mark shows (never stored). */
|
|
352
|
+
notes?: { references?: ReadonlyMap<string, string>; mark?: string };
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/** On the element showing a note reference's number: the note's story. */
|
|
356
|
+
export const NOTE_REF_ATTR = "data-foli-note-ref";
|
|
357
|
+
/** On a number drawn for a note, at its reference or its mark: not text of the document. */
|
|
358
|
+
export const NOTE_NUMBER_ATTR = "data-foli-note-number";
|
|
359
|
+
|
|
360
|
+
/** Word's footnote separator is two inches long; its continuation line here is the same. */
|
|
361
|
+
const SEPARATOR_WIDTH = 192;
|
|
362
|
+
|
|
363
|
+
/** The line a separator run draws, when `xml` is one (`w:separator`, `w:continuationSeparator`). */
|
|
364
|
+
export function separatorElement(xml: Element | null, doc: Document): HTMLElement | null {
|
|
365
|
+
if (!xml) return null;
|
|
366
|
+
const found = [xml, ...xml.getElementsByTagName("*")].some(
|
|
367
|
+
(e) => e.localName === "separator" || e.localName === "continuationSeparator",
|
|
368
|
+
);
|
|
369
|
+
if (!found) return null;
|
|
370
|
+
const line = doc.createElement("span");
|
|
371
|
+
line.dataset.foliSeparator = "";
|
|
372
|
+
const { style } = line;
|
|
373
|
+
style.display = "inline-block";
|
|
374
|
+
style.width = `${SEPARATOR_WIDTH}px`;
|
|
375
|
+
style.maxWidth = "100%";
|
|
376
|
+
style.height = "0";
|
|
377
|
+
style.borderTop = "1px solid #000";
|
|
378
|
+
style.verticalAlign = "middle";
|
|
379
|
+
return line;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/** The source for a document's pictures: the app's URL, else the resource's own. */
|
|
383
|
+
export function imageSource(
|
|
384
|
+
resources: Readonly<Record<string, ResourceJSON>> | undefined,
|
|
385
|
+
resolve?: (key: string, resource: ResourceJSON) => string | null | undefined,
|
|
386
|
+
): ImageSource {
|
|
387
|
+
return (key) => {
|
|
388
|
+
const resource = resources && Object.hasOwn(resources, key) ? resources[key] : undefined;
|
|
389
|
+
if (!resource) return undefined;
|
|
390
|
+
const url = resolve?.(key, resource);
|
|
391
|
+
// The app's answer stands, a refusal (null, "") included.
|
|
392
|
+
return url === undefined ? ownImageUrl(resource) : url || undefined;
|
|
393
|
+
};
|
|
86
394
|
}
|
|
87
395
|
|
|
88
396
|
/** A rendered block and what the page flow needs to know about it. */
|
|
@@ -326,6 +634,7 @@ export function renderBlocks(
|
|
|
326
634
|
options: BlockOptions = {},
|
|
327
635
|
): WordBlock[] {
|
|
328
636
|
const review = options.review ?? "markup";
|
|
637
|
+
const codes = options.fieldCodes === true;
|
|
329
638
|
const ratios = new Map<string, number>();
|
|
330
639
|
const ratio = (family: string) => {
|
|
331
640
|
let value = ratios.get(family);
|
|
@@ -335,7 +644,10 @@ export function renderBlocks(
|
|
|
335
644
|
}
|
|
336
645
|
return value;
|
|
337
646
|
};
|
|
338
|
-
const resolver = new Resolver(document);
|
|
647
|
+
const resolver = options.resolver ?? new Resolver(document);
|
|
648
|
+
const image = imageSource(document.resources, options.resource);
|
|
649
|
+
// shortcut: floating pictures are placed against the last section's page.
|
|
650
|
+
const geometry = floatGeometry(document.section);
|
|
339
651
|
const content = document.content.content ?? [];
|
|
340
652
|
const attrsOf = (node: NodeJSON) => (node.attrs ?? {}) as Record<string, unknown>;
|
|
341
653
|
// In the final and simple views a paragraph whose mark was deleted joins the next paragraph
|
|
@@ -357,33 +669,38 @@ export function renderBlocks(
|
|
|
357
669
|
),
|
|
358
670
|
);
|
|
359
671
|
const blocks: WordBlock[] = [];
|
|
360
|
-
|
|
672
|
+
const displays = fieldDisplay(content.map(inlineTypes), codes);
|
|
673
|
+
let carried: { nodes: number[]; inline: NodeJSON[]; display: FieldDisplay } | null = null;
|
|
361
674
|
/** Comment ranges open at this point of the document. */
|
|
362
675
|
const comments = new Set<number>();
|
|
363
676
|
|
|
364
677
|
content.forEach((node, index) => {
|
|
365
678
|
if (node.type !== "paragraph") {
|
|
366
|
-
blocks.push(
|
|
679
|
+
blocks.push(
|
|
680
|
+
opaqueBlock(node, index, resolver, doc, ratio, image, geometry, codes, options.notes),
|
|
681
|
+
);
|
|
367
682
|
return;
|
|
368
683
|
}
|
|
369
684
|
const attrs = attrsOf(node);
|
|
685
|
+
const own = displays[index] as FieldDisplay;
|
|
370
686
|
if (joins(index)) {
|
|
371
|
-
const previous
|
|
372
|
-
nodes: [],
|
|
373
|
-
inline: [],
|
|
374
|
-
};
|
|
687
|
+
const previous = carried ?? { nodes: [], inline: [], display: NO_DISPLAY };
|
|
375
688
|
carried = {
|
|
376
689
|
nodes: [...previous.nodes, index],
|
|
377
690
|
inline: [...previous.inline, ...(node.content ?? [])],
|
|
691
|
+
display: joinDisplays(previous.display, previous.inline.length, own),
|
|
378
692
|
};
|
|
379
693
|
return;
|
|
380
694
|
}
|
|
381
|
-
const merged
|
|
695
|
+
const merged = carried ?? { nodes: [], inline: [] as NodeJSON[], display: NO_DISPLAY };
|
|
382
696
|
carried = null;
|
|
697
|
+
const display = joinDisplays(merged.display, merged.inline.length, own);
|
|
383
698
|
const { paragraph, run } = resolver.paragraph(attrs);
|
|
384
699
|
const element = doc.createElement(tagOf(paragraph));
|
|
385
700
|
styleParagraph(element, paragraph);
|
|
386
|
-
|
|
701
|
+
// The line's own height is the paragraph mark's (Word has none but its runs' and the mark's).
|
|
702
|
+
const mark = resolver.run(run, (attrs.markFormat ?? []) as MarkJSON[]);
|
|
703
|
+
styleRun(element, mark, lineHeight(paragraph, mark, ratio(fontStack(latin(mark)))));
|
|
387
704
|
const marker = markers[index];
|
|
388
705
|
const firstMerged = merged.nodes[0];
|
|
389
706
|
const shownMarker = firstMerged !== undefined ? (markers[firstMerged] ?? null) : marker;
|
|
@@ -395,7 +712,22 @@ export function renderBlocks(
|
|
|
395
712
|
review,
|
|
396
713
|
comments,
|
|
397
714
|
ratio,
|
|
715
|
+
image,
|
|
716
|
+
geometry,
|
|
717
|
+
codes,
|
|
718
|
+
display,
|
|
719
|
+
notes: options.notes,
|
|
398
720
|
});
|
|
721
|
+
if (display.gone) element.style.display = "none";
|
|
722
|
+
if (
|
|
723
|
+
onlyPictures(
|
|
724
|
+
inline,
|
|
725
|
+
(node) => node.type,
|
|
726
|
+
(node) => !!node.attrs?.float,
|
|
727
|
+
)
|
|
728
|
+
)
|
|
729
|
+
for (const picture of element.querySelectorAll<HTMLElement>(`[${PICTURE_ATTR}]`))
|
|
730
|
+
picture.style.verticalAlign = "bottom";
|
|
399
731
|
// An empty paragraph, or one ending in a line break, shows its last (empty) line; after a
|
|
400
732
|
// page break the paragraph mark stays on the break's line, as in Word.
|
|
401
733
|
const last = element.lastChild as Element | null;
|
|
@@ -413,8 +745,8 @@ export function renderBlocks(
|
|
|
413
745
|
blocks.push({
|
|
414
746
|
element,
|
|
415
747
|
nodes: [...merged.nodes, index],
|
|
416
|
-
before: pt(spaceBefore),
|
|
417
|
-
after: pt(spaceAfter),
|
|
748
|
+
before: display.gone ? 0 : pt(spaceBefore),
|
|
749
|
+
after: display.gone ? 0 : pt(spaceAfter),
|
|
418
750
|
keepNext: paragraph.keepNext,
|
|
419
751
|
keepLines: paragraph.keepLines,
|
|
420
752
|
pageBreakBefore: paragraph.pageBreakBefore,
|
|
@@ -423,25 +755,91 @@ export function renderBlocks(
|
|
|
423
755
|
element.dataset.style = paragraph.style ?? "";
|
|
424
756
|
if (paragraph.contextualSpacing) element.dataset.contextual = "";
|
|
425
757
|
});
|
|
426
|
-
applyContextualSpacing(blocks);
|
|
758
|
+
applyContextualSpacing(blocks, resolver.addsParagraphSpacing);
|
|
427
759
|
return blocks;
|
|
428
760
|
}
|
|
429
761
|
|
|
762
|
+
const NO_DISPLAY = fieldDisplay([[]], false)[0] as FieldDisplay;
|
|
763
|
+
const inlineTypes = (node: NodeJSON) =>
|
|
764
|
+
node.type === "paragraph" ? (node.content ?? []).map((child) => child.type) : [];
|
|
765
|
+
|
|
766
|
+
/** `after`, whose nodes follow `length` others, added to `before` (paragraphs drawn as one). */
|
|
767
|
+
function joinDisplays(before: FieldDisplay, length: number, after: FieldDisplay): FieldDisplay {
|
|
768
|
+
if (!length) return after;
|
|
769
|
+
return {
|
|
770
|
+
hidden: new Set([...before.hidden, ...[...after.hidden].map((i) => i + length)]),
|
|
771
|
+
braces: new Map([
|
|
772
|
+
...before.braces,
|
|
773
|
+
...[...after.braces].map(([i, b]) => [i + length, b] as const),
|
|
774
|
+
]),
|
|
775
|
+
gone: before.gone && after.gone,
|
|
776
|
+
};
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
/**
|
|
780
|
+
* Whether a paragraph's line holds nothing but inline pictures: Word's line then ends at the
|
|
781
|
+
* picture's foot (its PDFs of the corpus: no room below it for the text it does not have).
|
|
782
|
+
*/
|
|
783
|
+
export function onlyPictures<T>(
|
|
784
|
+
inline: Iterable<T>,
|
|
785
|
+
type: (node: T) => string,
|
|
786
|
+
floats: (node: T) => boolean,
|
|
787
|
+
): boolean {
|
|
788
|
+
let found = false;
|
|
789
|
+
for (const node of inline) {
|
|
790
|
+
const name = type(node);
|
|
791
|
+
if (name === "picture") found ||= !floats(node);
|
|
792
|
+
else if (!INVISIBLE.has(name)) return false;
|
|
793
|
+
}
|
|
794
|
+
return found;
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
const INVISIBLE = new Set([
|
|
798
|
+
"bookmarkStart",
|
|
799
|
+
"bookmarkEnd",
|
|
800
|
+
"commentStart",
|
|
801
|
+
"commentEnd",
|
|
802
|
+
"commentReference",
|
|
803
|
+
"controlStart",
|
|
804
|
+
"controlEnd",
|
|
805
|
+
"fieldStart",
|
|
806
|
+
"fieldCode",
|
|
807
|
+
"fieldSeparator",
|
|
808
|
+
"fieldEnd",
|
|
809
|
+
]);
|
|
810
|
+
|
|
811
|
+
/**
|
|
812
|
+
* The top margin of a paragraph after another (whose space after is `above`) in a box the page
|
|
813
|
+
* flow does not lay out (a table cell). Adjacent margins collapse to the larger: Word's rule. In
|
|
814
|
+
* a document where the two spaces add up this margin holds both, so the collapse gives the sum
|
|
815
|
+
* and no paragraph's shading or border grows.
|
|
816
|
+
*/
|
|
817
|
+
export function marginAbove(before: number, above: number, resolver: Resolver): number {
|
|
818
|
+
return resolver.addsParagraphSpacing ? before + above : before;
|
|
819
|
+
}
|
|
820
|
+
|
|
430
821
|
/** A paragraph's element: a heading of its outline level (1-6), else a paragraph. */
|
|
431
822
|
export function tagOf(paragraph: ParagraphFormat): string {
|
|
432
823
|
const level = paragraph.outlineLevel;
|
|
433
824
|
return level !== null && level >= 0 && level < 6 ? `h${level + 1}` : "p";
|
|
434
825
|
}
|
|
435
826
|
|
|
436
|
-
/**
|
|
437
|
-
|
|
827
|
+
/**
|
|
828
|
+
* Word ignores the space between paragraphs of the same style that ask for it. When only the
|
|
829
|
+
* first asks, its space after still eats into the second's space before, unless the document
|
|
830
|
+
* adds the two (`adds`).
|
|
831
|
+
*/
|
|
832
|
+
function applyContextualSpacing(blocks: WordBlock[], adds: boolean): void {
|
|
438
833
|
for (let i = 1; i < blocks.length; i++) {
|
|
439
834
|
const a = blocks[i - 1] as WordBlock;
|
|
440
835
|
const b = blocks[i] as WordBlock;
|
|
441
836
|
const style = a.element.dataset.style;
|
|
442
837
|
if (style === undefined || style !== b.element.dataset.style) continue;
|
|
443
|
-
|
|
444
|
-
|
|
838
|
+
const first = "contextual" in a.element.dataset;
|
|
839
|
+
const second = "contextual" in b.element.dataset;
|
|
840
|
+
if (second) b.before = 0;
|
|
841
|
+
else if (first && !adds) b.before = Math.max(0, b.before - a.after);
|
|
842
|
+
if (first) a.after = 0;
|
|
445
843
|
}
|
|
446
844
|
}
|
|
447
845
|
|
|
@@ -502,6 +900,14 @@ interface InlineContext {
|
|
|
502
900
|
review: ReviewView;
|
|
503
901
|
comments: Set<number>;
|
|
504
902
|
ratio: (family: string) => number;
|
|
903
|
+
image?: ImageSource | undefined;
|
|
904
|
+
/** The page floating pictures are positioned against. */
|
|
905
|
+
geometry: FloatGeometry;
|
|
906
|
+
/** Whether fields show their codes. */
|
|
907
|
+
codes: boolean;
|
|
908
|
+
/** What these nodes show of their fields. */
|
|
909
|
+
display: FieldDisplay;
|
|
910
|
+
notes: BlockOptions["notes"] | undefined;
|
|
505
911
|
}
|
|
506
912
|
|
|
507
913
|
/** A paragraph's inline content: runs as spans, breaks, comment anchors, opaque inline content. */
|
|
@@ -512,8 +918,10 @@ function renderInline(
|
|
|
512
918
|
paragraph: ParagraphFormat,
|
|
513
919
|
context: InlineContext,
|
|
514
920
|
): void {
|
|
515
|
-
const { resolver, doc, review, comments, ratio } = context;
|
|
516
|
-
|
|
921
|
+
const { resolver, doc, review, comments, ratio, image, codes, display } = context;
|
|
922
|
+
const floats: HTMLElement[] = [];
|
|
923
|
+
for (const [index, node] of inline.entries()) {
|
|
924
|
+
if (display.hidden.has(index)) continue;
|
|
517
925
|
const marks = (node.marks ?? []) as MarkJSON[];
|
|
518
926
|
const inserted = marks.some((m) => m.type === "insertion");
|
|
519
927
|
const deleted = marks.some((m) => m.type === "deletion");
|
|
@@ -532,14 +940,31 @@ function renderInline(
|
|
|
532
940
|
parent.append(anchor);
|
|
533
941
|
continue;
|
|
534
942
|
}
|
|
535
|
-
case "bookmarkStart":
|
|
943
|
+
case "bookmarkStart": {
|
|
944
|
+
// Where a link to it goes (`@folijs/pdf`); out of the flow, so no line changes.
|
|
945
|
+
const mark = doc.createElement("span");
|
|
946
|
+
mark.dataset.foliBookmark = String(attrs.name ?? "");
|
|
947
|
+
mark.style.position = "absolute";
|
|
948
|
+
parent.append(mark);
|
|
949
|
+
continue;
|
|
950
|
+
}
|
|
536
951
|
case "bookmarkEnd":
|
|
952
|
+
case "controlStart":
|
|
953
|
+
case "controlEnd":
|
|
954
|
+
continue;
|
|
955
|
+
case "picture":
|
|
956
|
+
if (attrs.float) floats.push(...floatElements(attrs, doc, image, context.geometry).nodes);
|
|
957
|
+
else parent.append(pictureElement(attrs, doc, image));
|
|
537
958
|
continue;
|
|
538
959
|
case "hardBreak":
|
|
539
|
-
case "columnBreak":
|
|
540
|
-
// shortcut: a column break ends the line; Foli lays out one column.
|
|
541
960
|
parent.append(doc.createElement("br"));
|
|
542
961
|
continue;
|
|
962
|
+
case "columnBreak": {
|
|
963
|
+
const br = doc.createElement("br");
|
|
964
|
+
br.setAttribute(WORD_COLUMN_BREAK_ATTR, "");
|
|
965
|
+
parent.append(br);
|
|
966
|
+
continue;
|
|
967
|
+
}
|
|
543
968
|
case "pageBreak": {
|
|
544
969
|
const br = doc.createElement("br");
|
|
545
970
|
br.setAttribute(WORD_PAGE_BREAK_ATTR, "");
|
|
@@ -548,11 +973,22 @@ function renderInline(
|
|
|
548
973
|
}
|
|
549
974
|
default:
|
|
550
975
|
}
|
|
976
|
+
const code =
|
|
977
|
+
display.braces.get(index) ?? (codes && node.type === "fieldCode" ? String(attrs.text) : "");
|
|
551
978
|
let text: string;
|
|
552
|
-
if (
|
|
979
|
+
if (code) text = code;
|
|
980
|
+
else if (node.type === "text") text = node.text ?? "";
|
|
553
981
|
else if (node.type === "tab") text = "\t";
|
|
982
|
+
else if (node.type === "noteReference")
|
|
983
|
+
text = context.notes?.references?.get(String(attrs.story)) ?? "";
|
|
984
|
+
else if (node.type === "noteMark") text = context.notes?.mark ?? "";
|
|
554
985
|
else if (node.type === "opaqueInline") {
|
|
555
986
|
const xml = parseOpaque(String(attrs.xml ?? ""), doc);
|
|
987
|
+
const separator = separatorElement(xml, doc);
|
|
988
|
+
if (separator) {
|
|
989
|
+
parent.append(separator);
|
|
990
|
+
continue;
|
|
991
|
+
}
|
|
556
992
|
// A run's content (a drawing, a field character) shows as nothing; a content control or
|
|
557
993
|
// simple field as its text.
|
|
558
994
|
text = xml ? opaqueText(xml) : "";
|
|
@@ -567,6 +1003,9 @@ function renderInline(
|
|
|
567
1003
|
let element: HTMLElement = doc.createElement("span");
|
|
568
1004
|
styleRun(element, run, lineHeight(paragraph, run, ratio(fontStack(latin(run)))));
|
|
569
1005
|
element.textContent = text;
|
|
1006
|
+
if (node.type === "noteReference") element.setAttribute(NOTE_REF_ATTR, String(attrs.story));
|
|
1007
|
+
if (node.type === "noteReference" || node.type === "noteMark")
|
|
1008
|
+
element.setAttribute(NOTE_NUMBER_ATTR, "");
|
|
570
1009
|
const link = marks.find((m) => m.type === "link");
|
|
571
1010
|
if (link) element.dataset.href = String(link.attrs?.href ?? `#${String(link.attrs?.anchor)}`);
|
|
572
1011
|
if (comments.size) {
|
|
@@ -585,6 +1024,8 @@ function renderInline(
|
|
|
585
1024
|
}
|
|
586
1025
|
parent.append(element);
|
|
587
1026
|
}
|
|
1027
|
+
// Floating pictures first in the paragraph, in order: lines from its top go around them.
|
|
1028
|
+
parent.prepend(...floats);
|
|
588
1029
|
}
|
|
589
1030
|
|
|
590
1031
|
/**
|
|
@@ -598,21 +1039,30 @@ export function opaqueBlock(
|
|
|
598
1039
|
resolver: Resolver,
|
|
599
1040
|
doc: Document,
|
|
600
1041
|
ratio: (family: string) => number,
|
|
1042
|
+
image?: ImageSource,
|
|
1043
|
+
/** The page floating pictures in its cells are placed against. */
|
|
1044
|
+
geometry: FloatGeometry = floatGeometry(undefined),
|
|
1045
|
+
codes = false,
|
|
1046
|
+
notes?: BlockOptions["notes"],
|
|
601
1047
|
): WordBlock {
|
|
602
1048
|
const attrs = (node.attrs ?? {}) as Record<string, unknown>;
|
|
603
1049
|
const element = doc.createElement("div");
|
|
604
1050
|
element.dataset.foliOpaque = String(attrs.label ?? "");
|
|
605
1051
|
element.style.margin = "0";
|
|
606
1052
|
element.style.display = "flow-root";
|
|
607
|
-
const xml = parseOpaque(String(attrs.xml ?? ""), doc);
|
|
1053
|
+
const xml = node.type === "table" ? null : parseOpaque(String(attrs.xml ?? ""), doc);
|
|
608
1054
|
let after = 0;
|
|
609
|
-
if (
|
|
1055
|
+
if (node.type === "table")
|
|
1056
|
+
element.append(modelTable(node, resolver, doc, ratio, image, geometry, codes, notes));
|
|
1057
|
+
else if (xml?.localName === "tbl") element.append(opaqueTable(xml, resolver, doc, ratio));
|
|
610
1058
|
else if (xml) {
|
|
611
1059
|
const paragraphs = outermost(xml, PARAGRAPHS);
|
|
1060
|
+
let above = 0;
|
|
612
1061
|
paragraphs.forEach((p, i) => {
|
|
613
|
-
const rendered = opaqueParagraph(p, resolver, doc, ratio);
|
|
1062
|
+
const rendered = opaqueParagraph(p, resolver, doc, ratio, above);
|
|
614
1063
|
if (i < paragraphs.length - 1) rendered.element.style.marginBottom = `${rendered.after}px`;
|
|
615
1064
|
else after = rendered.after;
|
|
1065
|
+
above = rendered.after;
|
|
616
1066
|
element.append(rendered.element);
|
|
617
1067
|
});
|
|
618
1068
|
}
|
|
@@ -652,6 +1102,8 @@ function opaqueParagraph(
|
|
|
652
1102
|
resolver: Resolver,
|
|
653
1103
|
doc: Document,
|
|
654
1104
|
ratio: (family: string) => number,
|
|
1105
|
+
/** The space after the paragraph before it in its box. */
|
|
1106
|
+
above = 0,
|
|
655
1107
|
): { element: HTMLElement; after: number } {
|
|
656
1108
|
const pPr = child(p, "pPr");
|
|
657
1109
|
const style = childValue(pPr, "pStyle");
|
|
@@ -673,8 +1125,8 @@ function opaqueParagraph(
|
|
|
673
1125
|
element.append(span);
|
|
674
1126
|
}
|
|
675
1127
|
if (!element.textContent) element.append(doc.createElement("br"));
|
|
676
|
-
|
|
677
|
-
|
|
1128
|
+
const top = marginAbove(paragraph.spacing.before * PX_PER_TWIP, above, resolver);
|
|
1129
|
+
if (top) element.style.marginTop = `${top}px`;
|
|
678
1130
|
return { element, after: paragraph.spacing.after * PX_PER_TWIP };
|
|
679
1131
|
}
|
|
680
1132
|
|
|
@@ -802,14 +1254,17 @@ function opaqueTable(
|
|
|
802
1254
|
row.append(cell);
|
|
803
1255
|
continue;
|
|
804
1256
|
}
|
|
1257
|
+
let above = 0;
|
|
805
1258
|
for (const p of outermost(tc, CELL_CONTENT)) {
|
|
806
1259
|
if (p.localName === "tbl") {
|
|
807
1260
|
cell.append(opaqueTable(p, resolver, doc, ratio));
|
|
1261
|
+
above = 0;
|
|
808
1262
|
continue;
|
|
809
1263
|
}
|
|
810
1264
|
// Word counts the space after a cell's last paragraph in the row's height too.
|
|
811
|
-
const rendered = opaqueParagraph(p, resolver, doc, ratio);
|
|
1265
|
+
const rendered = opaqueParagraph(p, resolver, doc, ratio, above);
|
|
812
1266
|
rendered.element.style.marginBottom = `${rendered.after}px`;
|
|
1267
|
+
above = rendered.after;
|
|
813
1268
|
cell.append(rendered.element);
|
|
814
1269
|
}
|
|
815
1270
|
row.append(cell);
|
|
@@ -820,6 +1275,134 @@ function opaqueTable(
|
|
|
820
1275
|
return table;
|
|
821
1276
|
}
|
|
822
1277
|
|
|
1278
|
+
/** A border of the model (`BorderJSON`) as CSS; undefined when it says none. */
|
|
1279
|
+
export function borderJsonCss(
|
|
1280
|
+
border: { style?: string; size?: number; color?: string } | undefined,
|
|
1281
|
+
) {
|
|
1282
|
+
if (!border) return undefined;
|
|
1283
|
+
if (border.style === "nil" || border.style === "none") return "none";
|
|
1284
|
+
const width = Math.max(((border.size ?? 4) / 8) * (96 / 72), 0.5);
|
|
1285
|
+
const color = border.color && border.color !== "auto" ? `#${border.color}` : "#000";
|
|
1286
|
+
return `${width}px ${BORDER_STYLES[border.style ?? "single"] ?? "solid"} ${color}`;
|
|
1287
|
+
}
|
|
1288
|
+
|
|
1289
|
+
/**
|
|
1290
|
+
* A table of the model (schema v4), read-only, as opaque tables are shown: rows, cells with
|
|
1291
|
+
* their table style's formatting (`Resolver.cell`), their paragraphs and nested tables.
|
|
1292
|
+
*
|
|
1293
|
+
* shortcut: `renderDocument`'s tables (the editing view draws its own, `table.ts`): one block,
|
|
1294
|
+
* split across pages as clips; a cell merged into the one above is shown empty, as opaque
|
|
1295
|
+
* tables are.
|
|
1296
|
+
*/
|
|
1297
|
+
function modelTable(
|
|
1298
|
+
node: NodeJSON,
|
|
1299
|
+
resolver: Resolver,
|
|
1300
|
+
doc: Document,
|
|
1301
|
+
ratio: (family: string) => number,
|
|
1302
|
+
image: ImageSource | undefined,
|
|
1303
|
+
geometry: FloatGeometry,
|
|
1304
|
+
codes: boolean,
|
|
1305
|
+
notes: BlockOptions["notes"],
|
|
1306
|
+
): HTMLElement {
|
|
1307
|
+
const attrs = (node.attrs ?? {}) as Record<string, unknown>;
|
|
1308
|
+
const table = doc.createElement("table");
|
|
1309
|
+
table.style.borderCollapse = "collapse";
|
|
1310
|
+
table.style.tableLayout = "fixed";
|
|
1311
|
+
const grid = (attrs.grid as number[] | undefined) ?? [];
|
|
1312
|
+
const total = grid.reduce((a, b) => a + b, 0);
|
|
1313
|
+
if (total) table.style.width = `${total * PX_PER_TWIP}px`;
|
|
1314
|
+
const colgroup = doc.createElement("colgroup");
|
|
1315
|
+
for (const width of grid) {
|
|
1316
|
+
const col = doc.createElement("col");
|
|
1317
|
+
col.style.width = `${width * PX_PER_TWIP}px`;
|
|
1318
|
+
colgroup.append(col);
|
|
1319
|
+
}
|
|
1320
|
+
table.append(colgroup);
|
|
1321
|
+
const body = doc.createElement("tbody");
|
|
1322
|
+
const rows = node.content ?? [];
|
|
1323
|
+
const context = {
|
|
1324
|
+
resolver,
|
|
1325
|
+
doc,
|
|
1326
|
+
review: "markup" as const,
|
|
1327
|
+
comments: new Set<number>(),
|
|
1328
|
+
ratio,
|
|
1329
|
+
image,
|
|
1330
|
+
// shortcut: a floating picture in a cell is placed against the page, not its cell.
|
|
1331
|
+
geometry,
|
|
1332
|
+
codes,
|
|
1333
|
+
};
|
|
1334
|
+
rows.forEach((rowNode, r) => {
|
|
1335
|
+
const rowAttrs = (rowNode.attrs ?? {}) as Record<string, unknown>;
|
|
1336
|
+
const row = doc.createElement("tr");
|
|
1337
|
+
if (rowAttrs.header) row.dataset.foliHeader = "";
|
|
1338
|
+
if (rowAttrs.cantSplit) row.dataset.foliCantSplit = "";
|
|
1339
|
+
const cells = rowNode.content ?? [];
|
|
1340
|
+
cells.forEach((cellNode, c) => {
|
|
1341
|
+
const cellAttrs = (cellNode.attrs ?? {}) as Record<string, unknown>;
|
|
1342
|
+
const format = resolver.cell(attrs, rowAttrs, cellAttrs, {
|
|
1343
|
+
row: r,
|
|
1344
|
+
rows: rows.length,
|
|
1345
|
+
cell: c,
|
|
1346
|
+
cells: cells.length,
|
|
1347
|
+
});
|
|
1348
|
+
const cell = doc.createElement("td");
|
|
1349
|
+
const span = cellAttrs.gridSpan as number | undefined;
|
|
1350
|
+
if (span && span > 1) cell.colSpan = span;
|
|
1351
|
+
if (format.shading) cell.style.backgroundColor = `#${format.shading}`;
|
|
1352
|
+
const m = format.margins;
|
|
1353
|
+
cell.style.padding = `${m.top * PX_PER_TWIP}px ${m.right * PX_PER_TWIP}px ${m.bottom * PX_PER_TWIP}px ${m.left * PX_PER_TWIP}px`;
|
|
1354
|
+
cell.style.verticalAlign =
|
|
1355
|
+
format.verticalAlign === "center" ? "middle" : format.verticalAlign;
|
|
1356
|
+
for (const side of ["top", "left", "bottom", "right"] as const) {
|
|
1357
|
+
const css = borderJsonCss(format.borders[side]);
|
|
1358
|
+
if (css) cell.style.setProperty(`border-${side}`, css);
|
|
1359
|
+
}
|
|
1360
|
+
row.append(cell);
|
|
1361
|
+
if (cellAttrs.vMerge === "continue") return;
|
|
1362
|
+
let above = 0;
|
|
1363
|
+
const blocks = cellNode.content ?? [];
|
|
1364
|
+
const displays = fieldDisplay(blocks.map(inlineTypes), codes);
|
|
1365
|
+
for (const [b, block] of blocks.entries()) {
|
|
1366
|
+
if (block.type === "table") {
|
|
1367
|
+
cell.append(modelTable(block, resolver, doc, ratio, image, geometry, codes, notes));
|
|
1368
|
+
above = 0;
|
|
1369
|
+
continue;
|
|
1370
|
+
}
|
|
1371
|
+
if (block.type !== "paragraph") {
|
|
1372
|
+
cell.append(
|
|
1373
|
+
opaqueBlock(block, 0, resolver, doc, ratio, image, geometry, codes, notes).element,
|
|
1374
|
+
);
|
|
1375
|
+
above = 0;
|
|
1376
|
+
continue;
|
|
1377
|
+
}
|
|
1378
|
+
const { paragraph, run } = resolver.paragraph(block.attrs ?? {}, format);
|
|
1379
|
+
const element = doc.createElement(tagOf(paragraph));
|
|
1380
|
+
// A cell's paragraph: where it is on pages is measured too (`cellParagraphs`).
|
|
1381
|
+
element.dataset.foliCell = "";
|
|
1382
|
+
styleParagraph(element, paragraph);
|
|
1383
|
+
styleRun(element, run, lineHeight(paragraph, run, ratio(fontStack(latin(run)))));
|
|
1384
|
+
const display = displays[b] as FieldDisplay;
|
|
1385
|
+
renderInline(element, block.content ?? [], run, paragraph, {
|
|
1386
|
+
...context,
|
|
1387
|
+
display,
|
|
1388
|
+
notes,
|
|
1389
|
+
});
|
|
1390
|
+
if (display.gone) element.style.display = "none";
|
|
1391
|
+
if (!element.textContent) element.append(doc.createElement("br"));
|
|
1392
|
+
const before = paragraph.spacing.before * PX_PER_TWIP;
|
|
1393
|
+
element.style.marginTop = `${marginAbove(before, above, resolver)}px`;
|
|
1394
|
+
// Word counts the space after a cell's last paragraph in the row's height too.
|
|
1395
|
+
above = paragraph.spacing.after * PX_PER_TWIP;
|
|
1396
|
+
element.style.marginBottom = `${above}px`;
|
|
1397
|
+
cell.append(element);
|
|
1398
|
+
}
|
|
1399
|
+
});
|
|
1400
|
+
body.append(row);
|
|
1401
|
+
});
|
|
1402
|
+
table.append(body);
|
|
1403
|
+
return table;
|
|
1404
|
+
}
|
|
1405
|
+
|
|
823
1406
|
function childValueOf(element: Element, attribute: string): string | undefined {
|
|
824
1407
|
for (const a of element.attributes) if (a.localName === attribute) return a.value;
|
|
825
1408
|
return undefined;
|