@folijs/view 0.2.0 → 0.3.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 +28 -0
- package/README.md +4 -3
- package/dist/clipboard.d.ts +28 -9
- package/dist/clipboard.d.ts.map +1 -1
- package/dist/clipboard.js +53 -25
- package/dist/clipboard.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/layout/flow.d.ts +18 -1
- package/dist/layout/flow.d.ts.map +1 -1
- package/dist/layout/flow.js +20 -11
- package/dist/layout/flow.js.map +1 -1
- package/dist/render.d.ts +0 -20
- package/dist/render.d.ts.map +1 -1
- package/dist/render.js +0 -163
- package/dist/render.js.map +1 -1
- package/dist/view.d.ts +37 -16
- package/dist/view.d.ts.map +1 -1
- package/dist/view.js +348 -273
- package/dist/view.js.map +1 -1
- package/dist/word/blocks.d.ts +72 -0
- package/dist/word/blocks.d.ts.map +1 -0
- package/dist/word/blocks.js +718 -0
- package/dist/word/blocks.js.map +1 -0
- package/dist/word/edit.d.ts +61 -0
- package/dist/word/edit.d.ts.map +1 -0
- package/dist/word/edit.js +296 -0
- package/dist/word/edit.js.map +1 -0
- package/dist/word/pages.d.ts +45 -0
- package/dist/word/pages.d.ts.map +1 -0
- package/dist/word/pages.js +335 -0
- package/dist/word/pages.js.map +1 -0
- package/package.json +2 -2
- package/src/clipboard.ts +75 -25
- package/src/index.ts +6 -0
- package/src/layout/flow.ts +36 -13
- package/src/render.ts +1 -171
- package/src/view.ts +394 -281
- package/src/word/blocks.ts +777 -0
- package/src/word/edit.ts +360 -0
- package/src/word/pages.ts +372 -0
|
@@ -0,0 +1,718 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Schema v3 blocks as DOM, formatted as Word formats them (8.6b): every paragraph's and run's
|
|
3
|
+
* effective formatting from `Resolver` as inline CSS, list markers, tracked changes, comment
|
|
4
|
+
* anchors, and content the model keeps opaque shown as text. Read-only: built with
|
|
5
|
+
* createElement/textContent only (never innerHTML); the page layout is in `pages.ts`.
|
|
6
|
+
*/
|
|
7
|
+
import { Resolver, } from "@folijs/core";
|
|
8
|
+
/** Marks the <br> of an explicit page break (the page flow ends the page after its line). */
|
|
9
|
+
export const WORD_PAGE_BREAK_ATTR = "data-page-break";
|
|
10
|
+
/** CSS px per twip (1/1440 inch; CSS: 96 px per inch). */
|
|
11
|
+
export const PX_PER_TWIP = 96 / 1440;
|
|
12
|
+
/**
|
|
13
|
+
* Metric-compatible open fonts for Word's common fonts: the same advance widths and line
|
|
14
|
+
* heights, so text breaks into the same lines where the original is not installed.
|
|
15
|
+
*/
|
|
16
|
+
const SUBSTITUTES = {
|
|
17
|
+
calibri: ["Carlito"],
|
|
18
|
+
cambria: ["Caladea"],
|
|
19
|
+
arial: ["Liberation Sans", "Arimo"],
|
|
20
|
+
"times new roman": ["Liberation Serif", "Tinos"],
|
|
21
|
+
"courier new": ["Liberation Mono", "Cousine"],
|
|
22
|
+
georgia: ["Gelasio"],
|
|
23
|
+
};
|
|
24
|
+
const SERIF = new Set(["cambria", "times new roman", "georgia", "garamond", "book antiqua"]);
|
|
25
|
+
const MONOSPACE = new Set(["courier new", "consolas", "courier"]);
|
|
26
|
+
/** A CSS font-family list for a Word font name: the font, its substitutes, a generic family. */
|
|
27
|
+
export function fontStack(name) {
|
|
28
|
+
const font = name ?? "Times New Roman";
|
|
29
|
+
const key = font.toLowerCase();
|
|
30
|
+
const generic = SERIF.has(key) ? "serif" : MONOSPACE.has(key) ? "monospace" : "sans-serif";
|
|
31
|
+
const quote = (f) => `"${f.replace(/["\\]/g, "")}"`;
|
|
32
|
+
const substitutes = Object.hasOwn(SUBSTITUTES, key) ? SUBSTITUTES[key] : [];
|
|
33
|
+
return [font, ...substitutes].map(quote).concat(generic).join(", ");
|
|
34
|
+
}
|
|
35
|
+
/** Word's highlight colors (`w:highlight`). */
|
|
36
|
+
const HIGHLIGHTS = {
|
|
37
|
+
black: "#000000",
|
|
38
|
+
blue: "#0000FF",
|
|
39
|
+
cyan: "#00FFFF",
|
|
40
|
+
green: "#00FF00",
|
|
41
|
+
magenta: "#FF00FF",
|
|
42
|
+
red: "#FF0000",
|
|
43
|
+
yellow: "#FFFF00",
|
|
44
|
+
white: "#FFFFFF",
|
|
45
|
+
darkBlue: "#000080",
|
|
46
|
+
darkCyan: "#008080",
|
|
47
|
+
darkGreen: "#008000",
|
|
48
|
+
darkMagenta: "#800080",
|
|
49
|
+
darkRed: "#800000",
|
|
50
|
+
darkYellow: "#808000",
|
|
51
|
+
darkGray: "#808080",
|
|
52
|
+
lightGray: "#C0C0C0",
|
|
53
|
+
};
|
|
54
|
+
const UNDERLINES = {
|
|
55
|
+
double: "double",
|
|
56
|
+
dotted: "dotted",
|
|
57
|
+
dottedHeavy: "dotted",
|
|
58
|
+
dash: "dashed",
|
|
59
|
+
dashedHeavy: "dashed",
|
|
60
|
+
dashLong: "dashed",
|
|
61
|
+
wave: "wavy",
|
|
62
|
+
wavyHeavy: "wavy",
|
|
63
|
+
wavyDouble: "wavy",
|
|
64
|
+
};
|
|
65
|
+
const ALIGN = {
|
|
66
|
+
left: "left",
|
|
67
|
+
start: "left",
|
|
68
|
+
right: "right",
|
|
69
|
+
end: "right",
|
|
70
|
+
center: "center",
|
|
71
|
+
both: "justify",
|
|
72
|
+
distribute: "justify",
|
|
73
|
+
};
|
|
74
|
+
const BORDER_STYLES = {
|
|
75
|
+
double: "double",
|
|
76
|
+
dotted: "dotted",
|
|
77
|
+
dashed: "dashed",
|
|
78
|
+
dashSmallGap: "dashed",
|
|
79
|
+
none: "none",
|
|
80
|
+
nil: "none",
|
|
81
|
+
};
|
|
82
|
+
/** The one font name a run shows for Latin text. */
|
|
83
|
+
export const latin = (run) => run.font.ascii ?? run.font.hAnsi;
|
|
84
|
+
/** A line height in CSS for text in `run` (`ratio`: its font's normal line height ratio). */
|
|
85
|
+
export function lineHeight(paragraph, run, ratio) {
|
|
86
|
+
const sizePx = (run.size / 2) * (96 / 72);
|
|
87
|
+
const { line, lineRule } = paragraph.spacing;
|
|
88
|
+
if (line === null)
|
|
89
|
+
return String(ratio);
|
|
90
|
+
if (lineRule === "exact")
|
|
91
|
+
return `${line * PX_PER_TWIP}px`;
|
|
92
|
+
if (lineRule === "atLeast")
|
|
93
|
+
return `${Math.max(line * PX_PER_TWIP, ratio * sizePx)}px`;
|
|
94
|
+
return String((ratio * line) / 240);
|
|
95
|
+
}
|
|
96
|
+
/** Run formatting as inline CSS on `element`. */
|
|
97
|
+
export function styleRun(element, run, line) {
|
|
98
|
+
const { style } = element;
|
|
99
|
+
style.fontFamily = fontStack(latin(run));
|
|
100
|
+
style.fontSize = `${run.size / 2}pt`;
|
|
101
|
+
style.lineHeight = line;
|
|
102
|
+
style.fontWeight = run.bold ? "700" : "400";
|
|
103
|
+
style.fontStyle = run.italic ? "italic" : "normal";
|
|
104
|
+
const decorations = [];
|
|
105
|
+
if (run.underline)
|
|
106
|
+
decorations.push("underline");
|
|
107
|
+
if (run.strike || run.doubleStrike)
|
|
108
|
+
decorations.push("line-through");
|
|
109
|
+
if (decorations.length) {
|
|
110
|
+
style.textDecorationLine = decorations.join(" ");
|
|
111
|
+
if (run.doubleStrike || run.underline === "double")
|
|
112
|
+
style.textDecorationStyle = "double";
|
|
113
|
+
else if (run.underline && UNDERLINES[run.underline])
|
|
114
|
+
style.textDecorationStyle = UNDERLINES[run.underline];
|
|
115
|
+
if (run.underlineColor && run.underlineColor !== "auto")
|
|
116
|
+
style.textDecorationColor = `#${run.underlineColor}`;
|
|
117
|
+
}
|
|
118
|
+
if (run.caps)
|
|
119
|
+
style.textTransform = "uppercase";
|
|
120
|
+
if (run.smallCaps)
|
|
121
|
+
style.fontVariant = "small-caps";
|
|
122
|
+
if (run.color)
|
|
123
|
+
style.color = `#${run.color}`;
|
|
124
|
+
const background = run.highlight ? HIGHLIGHTS[run.highlight] : run.shading && `#${run.shading}`;
|
|
125
|
+
if (background)
|
|
126
|
+
style.backgroundColor = background;
|
|
127
|
+
if (run.verticalAlign !== "baseline") {
|
|
128
|
+
// Word raises or lowers a smaller copy without growing the line.
|
|
129
|
+
style.verticalAlign = run.verticalAlign === "superscript" ? "super" : "sub";
|
|
130
|
+
style.fontSize = `${(run.size / 2) * (2 / 3)}pt`;
|
|
131
|
+
style.lineHeight = "0";
|
|
132
|
+
}
|
|
133
|
+
if (run.characterSpacing)
|
|
134
|
+
style.letterSpacing = `${run.characterSpacing * PX_PER_TWIP}px`;
|
|
135
|
+
if (run.hidden)
|
|
136
|
+
style.display = "none";
|
|
137
|
+
}
|
|
138
|
+
/** Paragraph properties as inline CSS on `element` (spacing is the flow's). */
|
|
139
|
+
export function styleParagraph(element, paragraph) {
|
|
140
|
+
const { style } = element;
|
|
141
|
+
style.margin = "0";
|
|
142
|
+
style.whiteSpace = "pre-wrap";
|
|
143
|
+
style.overflowWrap = "break-word";
|
|
144
|
+
// Word's default tab stops: every half inch.
|
|
145
|
+
style.tabSize = `${720 * PX_PER_TWIP}px`;
|
|
146
|
+
const { left, right, firstLine, hanging } = paragraph.indent;
|
|
147
|
+
style.paddingLeft = `${left * PX_PER_TWIP}px`;
|
|
148
|
+
style.paddingRight = `${right * PX_PER_TWIP}px`;
|
|
149
|
+
style.textIndent = `${(firstLine - hanging) * PX_PER_TWIP}px`;
|
|
150
|
+
const align = paragraph.justification && ALIGN[paragraph.justification];
|
|
151
|
+
if (align)
|
|
152
|
+
style.textAlign = align;
|
|
153
|
+
if (paragraph.shading?.fill && paragraph.shading.fill !== "auto")
|
|
154
|
+
style.backgroundColor = `#${paragraph.shading.fill}`;
|
|
155
|
+
for (const [side, border] of Object.entries(paragraph.borders ?? {})) {
|
|
156
|
+
if (!border || side === "between" || side === "bar")
|
|
157
|
+
continue;
|
|
158
|
+
const kind = BORDER_STYLES[border.style] ?? "solid";
|
|
159
|
+
// Sizes in eighths of a point; the space between text and border in points.
|
|
160
|
+
const width = kind === "none" ? 0 : Math.max(((border.size ?? 4) / 8) * (96 / 72), 1);
|
|
161
|
+
const color = border.color && border.color !== "auto" ? `#${border.color}` : "currentColor";
|
|
162
|
+
style.setProperty(`border-${side}`, `${width}px ${kind} ${color}`);
|
|
163
|
+
style.setProperty(`padding-${side}`, `${((border.space ?? 0) * 96) / 72}px`);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Opaque XML not shown in the line: a text box's content (it floats; shortcut: not shown until
|
|
168
|
+
* Phase 11 lays out drawings) and the fallback copy of alternate content (`mc:Fallback`, the
|
|
169
|
+
* same drawing again for older readers).
|
|
170
|
+
*/
|
|
171
|
+
const NOT_INLINE = new Set(["txbxContent", "Fallback"]);
|
|
172
|
+
/** Text a run of opaque XML shows: its `w:t` elements (field results, content controls). */
|
|
173
|
+
export function opaqueText(element) {
|
|
174
|
+
let text = "";
|
|
175
|
+
for (const node of element.children) {
|
|
176
|
+
if (NOT_INLINE.has(node.localName))
|
|
177
|
+
continue;
|
|
178
|
+
if (node.localName === "t")
|
|
179
|
+
text += node.textContent;
|
|
180
|
+
else if (node.localName === "tab")
|
|
181
|
+
text += "\t";
|
|
182
|
+
else
|
|
183
|
+
text += opaqueText(node);
|
|
184
|
+
}
|
|
185
|
+
return text;
|
|
186
|
+
}
|
|
187
|
+
/** Opaque XML's text, a line per paragraph (`w:p`), or its runs' text when it has none. */
|
|
188
|
+
export function opaqueParagraphsText(element) {
|
|
189
|
+
const paragraphs = [...element.getElementsByTagName("*")].filter((e) => e.localName === "p");
|
|
190
|
+
if (element.localName === "p")
|
|
191
|
+
paragraphs.unshift(element);
|
|
192
|
+
return paragraphs.length ? paragraphs.map(opaqueText).join("\n") : opaqueText(element);
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* The outermost descendants of opaque XML with one of `names` (local names), in order: not those
|
|
196
|
+
* inside a match (a run's text box, a cell's nested table) or content not shown in the line.
|
|
197
|
+
*/
|
|
198
|
+
function outermost(element, names) {
|
|
199
|
+
const found = [];
|
|
200
|
+
for (const node of element.children) {
|
|
201
|
+
if (names.has(node.localName))
|
|
202
|
+
found.push(node);
|
|
203
|
+
else if (!NOT_INLINE.has(node.localName))
|
|
204
|
+
found.push(...outermost(node, names));
|
|
205
|
+
}
|
|
206
|
+
return found;
|
|
207
|
+
}
|
|
208
|
+
const PARAGRAPHS = new Set(["p"]);
|
|
209
|
+
const RUNS = new Set(["r"]);
|
|
210
|
+
const ROWS = new Set(["tr"]);
|
|
211
|
+
const CELLS = new Set(["tc"]);
|
|
212
|
+
const CELL_CONTENT = new Set(["p", "tbl"]);
|
|
213
|
+
/**
|
|
214
|
+
* Parses verbatim WordprocessingML (opaque content) into an inert XML document, declaring the
|
|
215
|
+
* prefixes it uses; null when it is not well-formed.
|
|
216
|
+
*/
|
|
217
|
+
export function parseOpaque(xml, doc) {
|
|
218
|
+
const view = doc.defaultView;
|
|
219
|
+
if (!view?.DOMParser)
|
|
220
|
+
return null;
|
|
221
|
+
const prefixes = new Set();
|
|
222
|
+
for (const match of xml.matchAll(/<\/?([A-Za-z_][\w.-]*):|\s([A-Za-z_][\w.-]*):[\w.-]+\s*=/g)) {
|
|
223
|
+
const prefix = match[1] ?? match[2];
|
|
224
|
+
if (prefix && prefix !== "xml" && prefix !== "xmlns")
|
|
225
|
+
prefixes.add(prefix);
|
|
226
|
+
}
|
|
227
|
+
const declarations = [...prefixes].map((p) => ` xmlns:${p}="urn:foli:${p}"`).join("");
|
|
228
|
+
const parsed = new view.DOMParser().parseFromString(`<foli${declarations}>${xml}</foli>`, "application/xml");
|
|
229
|
+
const root = parsed.documentElement;
|
|
230
|
+
if (root.localName !== "foli" || parsed.getElementsByTagName("parsererror").length)
|
|
231
|
+
return null;
|
|
232
|
+
return root.firstElementChild;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Renders a document's blocks. `final` review merges a paragraph whose mark was deleted into
|
|
236
|
+
* the next one, as accepting the deletion does.
|
|
237
|
+
*/
|
|
238
|
+
export function renderBlocks(document, doc, options = {}) {
|
|
239
|
+
const review = options.review ?? "markup";
|
|
240
|
+
const ratios = new Map();
|
|
241
|
+
const ratio = (family) => {
|
|
242
|
+
let value = ratios.get(family);
|
|
243
|
+
if (value === undefined) {
|
|
244
|
+
value = options.lineRatio?.(family) ?? 1.2;
|
|
245
|
+
ratios.set(family, value);
|
|
246
|
+
}
|
|
247
|
+
return value;
|
|
248
|
+
};
|
|
249
|
+
const resolver = new Resolver(document);
|
|
250
|
+
const content = document.content.content ?? [];
|
|
251
|
+
const attrsOf = (node) => (node.attrs ?? {});
|
|
252
|
+
// In the final view a paragraph whose mark was deleted joins the next paragraph (not a
|
|
253
|
+
// table, and the document's last mark cannot be deleted).
|
|
254
|
+
const joins = (index) => {
|
|
255
|
+
const node = content[index];
|
|
256
|
+
const revision = node && attrsOf(node).revision;
|
|
257
|
+
return (review === "final" &&
|
|
258
|
+
node?.type === "paragraph" &&
|
|
259
|
+
!!revision?.deletion &&
|
|
260
|
+
content[index + 1]?.type === "paragraph");
|
|
261
|
+
};
|
|
262
|
+
// A joined paragraph is numbered once, with the first paragraph's marker.
|
|
263
|
+
const markers = resolver.markers(content.map((node, index) => node.type === "paragraph" && !joins(index - 1) ? attrsOf(node) : { numbering: { id: 0 } }));
|
|
264
|
+
const blocks = [];
|
|
265
|
+
let carried = null;
|
|
266
|
+
/** Comment ranges open at this point of the document. */
|
|
267
|
+
const comments = new Set();
|
|
268
|
+
content.forEach((node, index) => {
|
|
269
|
+
if (node.type !== "paragraph") {
|
|
270
|
+
blocks.push(opaqueBlock(node, index, resolver, doc, ratio));
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
const attrs = attrsOf(node);
|
|
274
|
+
if (joins(index)) {
|
|
275
|
+
const previous = carried ?? {
|
|
276
|
+
nodes: [],
|
|
277
|
+
inline: [],
|
|
278
|
+
};
|
|
279
|
+
carried = {
|
|
280
|
+
nodes: [...previous.nodes, index],
|
|
281
|
+
inline: [...previous.inline, ...(node.content ?? [])],
|
|
282
|
+
};
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
const merged = carried ?? { nodes: [], inline: [] };
|
|
286
|
+
carried = null;
|
|
287
|
+
const { paragraph, run } = resolver.paragraph(attrs);
|
|
288
|
+
const element = doc.createElement(tagOf(paragraph));
|
|
289
|
+
styleParagraph(element, paragraph);
|
|
290
|
+
styleRun(element, run, lineHeight(paragraph, run, ratio(fontStack(latin(run)))));
|
|
291
|
+
const marker = markers[index];
|
|
292
|
+
const firstMerged = merged.nodes[0];
|
|
293
|
+
const shownMarker = firstMerged !== undefined ? (markers[firstMerged] ?? null) : marker;
|
|
294
|
+
if (shownMarker)
|
|
295
|
+
element.append(markerElement(shownMarker, paragraph, doc, ratio));
|
|
296
|
+
const inline = [...merged.inline, ...(node.content ?? [])];
|
|
297
|
+
renderInline(element, inline, run, paragraph, {
|
|
298
|
+
resolver,
|
|
299
|
+
doc,
|
|
300
|
+
review,
|
|
301
|
+
comments,
|
|
302
|
+
ratio,
|
|
303
|
+
});
|
|
304
|
+
// An empty paragraph, or one ending in a line break, shows its last (empty) line; after a
|
|
305
|
+
// page break the paragraph mark stays on the break's line, as in Word.
|
|
306
|
+
const last = element.lastChild;
|
|
307
|
+
const endsInBreak = last?.nodeName === "BR";
|
|
308
|
+
if ((endsInBreak && !last?.hasAttribute(WORD_PAGE_BREAK_ATTR)) ||
|
|
309
|
+
(!endsInBreak && !element.textContent))
|
|
310
|
+
element.append(doc.createElement("br"));
|
|
311
|
+
const pt = (twips) => twips * PX_PER_TWIP;
|
|
312
|
+
const { before, after } = paragraph.spacing;
|
|
313
|
+
// Word's automatic spacing (HTML): 14 pt.
|
|
314
|
+
const spaceBefore = paragraph.spacing.beforeAutospacing ? 280 : before;
|
|
315
|
+
const spaceAfter = paragraph.spacing.afterAutospacing ? 280 : after;
|
|
316
|
+
blocks.push({
|
|
317
|
+
element,
|
|
318
|
+
nodes: [...merged.nodes, index],
|
|
319
|
+
before: pt(spaceBefore),
|
|
320
|
+
after: pt(spaceAfter),
|
|
321
|
+
keepNext: paragraph.keepNext,
|
|
322
|
+
keepLines: paragraph.keepLines,
|
|
323
|
+
pageBreakBefore: paragraph.pageBreakBefore,
|
|
324
|
+
section: attrs.section ?? null,
|
|
325
|
+
});
|
|
326
|
+
element.dataset.style = paragraph.style ?? "";
|
|
327
|
+
if (paragraph.contextualSpacing)
|
|
328
|
+
element.dataset.contextual = "";
|
|
329
|
+
});
|
|
330
|
+
applyContextualSpacing(blocks);
|
|
331
|
+
return blocks;
|
|
332
|
+
}
|
|
333
|
+
/** A paragraph's element: a heading of its outline level (1-6), else a paragraph. */
|
|
334
|
+
export function tagOf(paragraph) {
|
|
335
|
+
const level = paragraph.outlineLevel;
|
|
336
|
+
return level !== null && level >= 0 && level < 6 ? `h${level + 1}` : "p";
|
|
337
|
+
}
|
|
338
|
+
/** Word ignores the space between paragraphs of the same style that ask for it. */
|
|
339
|
+
function applyContextualSpacing(blocks) {
|
|
340
|
+
for (let i = 1; i < blocks.length; i++) {
|
|
341
|
+
const a = blocks[i - 1];
|
|
342
|
+
const b = blocks[i];
|
|
343
|
+
const style = a.element.dataset.style;
|
|
344
|
+
if (style === undefined || style !== b.element.dataset.style)
|
|
345
|
+
continue;
|
|
346
|
+
if ("contextual" in a.element.dataset)
|
|
347
|
+
a.after = 0;
|
|
348
|
+
if ("contextual" in b.element.dataset)
|
|
349
|
+
b.before = 0;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
/** Symbol and Wingdings bullets (`w:lvlText`, private-use code points) as Unicode. */
|
|
353
|
+
const SYMBOLS = {
|
|
354
|
+
"\uF0B7": "\u2022", // Symbol: bullet
|
|
355
|
+
"\uF0A7": "\u25AA", // Wingdings: small black square
|
|
356
|
+
"\uF0A8": "\u25FB", // Wingdings: white medium square
|
|
357
|
+
"\uF06C": "\u25CF", // Wingdings: black circle
|
|
358
|
+
"\uF06E": "\u25A0", // Wingdings: black square
|
|
359
|
+
"\uF071": "\u2751", // Wingdings: shadowed white square
|
|
360
|
+
"\uF075": "\u25C6", // Wingdings: black diamond
|
|
361
|
+
"\uF076": "\u2756", // Wingdings: black diamond minus white X
|
|
362
|
+
"\uF0D8": "\u27A2", // Wingdings: arrowhead
|
|
363
|
+
"\uF0FC": "\u2713", // Wingdings: check mark
|
|
364
|
+
};
|
|
365
|
+
/** A list marker: its text, then what follows it (a tab to the text's indent, a space). */
|
|
366
|
+
export function markerElement(marker, paragraph, doc, ratio) {
|
|
367
|
+
const span = doc.createElement("span");
|
|
368
|
+
span.dataset.foliMarker = "";
|
|
369
|
+
span.setAttribute("aria-hidden", "true");
|
|
370
|
+
styleRun(span, marker.run, lineHeight(paragraph, marker.run, ratio(fontStack(latin(marker.run)))));
|
|
371
|
+
span.textContent = marker.text;
|
|
372
|
+
// Bullets in Symbol and Wingdings are private-use code points: shown as the Unicode
|
|
373
|
+
// character they draw, in a font that has it.
|
|
374
|
+
const font = latin(marker.run)?.toLowerCase();
|
|
375
|
+
const glyph = font === "symbol" || font?.startsWith("wingdings") ? SYMBOLS[marker.text] : undefined;
|
|
376
|
+
if (glyph) {
|
|
377
|
+
span.textContent = glyph;
|
|
378
|
+
span.style.fontFamily = "sans-serif";
|
|
379
|
+
}
|
|
380
|
+
const hanging = paragraph.indent.hanging * PX_PER_TWIP;
|
|
381
|
+
if (marker.suffix === "tab" && hanging > 0) {
|
|
382
|
+
// shortcut: a marker wider than the hanging indent goes to the next default tab stop in
|
|
383
|
+
// Word; here the text follows it directly.
|
|
384
|
+
span.style.display = "inline-block";
|
|
385
|
+
span.style.minWidth = `${hanging}px`;
|
|
386
|
+
span.style.textIndent = "0";
|
|
387
|
+
}
|
|
388
|
+
else if (marker.suffix === "tab")
|
|
389
|
+
span.textContent += "\t";
|
|
390
|
+
else if (marker.suffix === "space")
|
|
391
|
+
span.textContent += " ";
|
|
392
|
+
return span;
|
|
393
|
+
}
|
|
394
|
+
/** A paragraph's inline content: runs as spans, breaks, comment anchors, opaque inline content. */
|
|
395
|
+
function renderInline(parent, inline, paragraphRun, paragraph, context) {
|
|
396
|
+
const { resolver, doc, review, comments, ratio } = context;
|
|
397
|
+
for (const node of inline) {
|
|
398
|
+
const marks = (node.marks ?? []);
|
|
399
|
+
const inserted = marks.some((m) => m.type === "insertion");
|
|
400
|
+
const deleted = marks.some((m) => m.type === "deletion");
|
|
401
|
+
if (deleted && review === "final")
|
|
402
|
+
continue;
|
|
403
|
+
const attrs = (node.attrs ?? {});
|
|
404
|
+
switch (node.type) {
|
|
405
|
+
case "commentStart":
|
|
406
|
+
comments.add(attrs.id);
|
|
407
|
+
continue;
|
|
408
|
+
case "commentEnd":
|
|
409
|
+
comments.delete(attrs.id);
|
|
410
|
+
continue;
|
|
411
|
+
case "commentReference": {
|
|
412
|
+
const anchor = doc.createElement("span");
|
|
413
|
+
anchor.dataset.foliCommentReference = String(attrs.id);
|
|
414
|
+
parent.append(anchor);
|
|
415
|
+
continue;
|
|
416
|
+
}
|
|
417
|
+
case "bookmarkStart":
|
|
418
|
+
case "bookmarkEnd":
|
|
419
|
+
continue;
|
|
420
|
+
case "hardBreak":
|
|
421
|
+
case "columnBreak":
|
|
422
|
+
// shortcut: a column break ends the line; Foli lays out one column.
|
|
423
|
+
parent.append(doc.createElement("br"));
|
|
424
|
+
continue;
|
|
425
|
+
case "pageBreak": {
|
|
426
|
+
const br = doc.createElement("br");
|
|
427
|
+
br.setAttribute(WORD_PAGE_BREAK_ATTR, "");
|
|
428
|
+
parent.append(br);
|
|
429
|
+
continue;
|
|
430
|
+
}
|
|
431
|
+
default:
|
|
432
|
+
}
|
|
433
|
+
let text;
|
|
434
|
+
if (node.type === "text")
|
|
435
|
+
text = node.text ?? "";
|
|
436
|
+
else if (node.type === "tab")
|
|
437
|
+
text = "\t";
|
|
438
|
+
else if (node.type === "opaqueInline") {
|
|
439
|
+
const xml = parseOpaque(String(attrs.xml ?? ""), doc);
|
|
440
|
+
// A run's content (a drawing, a field character) shows as nothing; a content control or
|
|
441
|
+
// simple field as its text.
|
|
442
|
+
text = xml ? opaqueText(xml) : "";
|
|
443
|
+
if (!text) {
|
|
444
|
+
const placeholder = doc.createElement("span");
|
|
445
|
+
placeholder.dataset.foliOpaque = String(attrs.label ?? "");
|
|
446
|
+
parent.append(placeholder);
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
else
|
|
451
|
+
continue;
|
|
452
|
+
const run = resolver.run(paragraphRun, marks);
|
|
453
|
+
let element = doc.createElement("span");
|
|
454
|
+
styleRun(element, run, lineHeight(paragraph, run, ratio(fontStack(latin(run)))));
|
|
455
|
+
element.textContent = text;
|
|
456
|
+
const link = marks.find((m) => m.type === "link");
|
|
457
|
+
if (link)
|
|
458
|
+
element.dataset.href = String(link.attrs?.href ?? `#${String(link.attrs?.anchor)}`);
|
|
459
|
+
if (comments.size) {
|
|
460
|
+
element.dataset.foliComments = [...comments].join(" ");
|
|
461
|
+
if (!run.highlight && !run.shading)
|
|
462
|
+
element.style.backgroundColor = "rgba(255, 213, 79, 0.35)";
|
|
463
|
+
}
|
|
464
|
+
if (review === "markup" && (inserted || deleted)) {
|
|
465
|
+
const wrapper = doc.createElement(inserted ? "ins" : "del");
|
|
466
|
+
const author = marks.find((m) => m.type === (inserted ? "insertion" : "deletion"))?.attrs
|
|
467
|
+
?.author;
|
|
468
|
+
if (author)
|
|
469
|
+
wrapper.title = String(author);
|
|
470
|
+
wrapper.style.color = "#B5082E";
|
|
471
|
+
wrapper.style.textDecorationLine = inserted ? "underline" : "line-through";
|
|
472
|
+
wrapper.append(element);
|
|
473
|
+
element = wrapper;
|
|
474
|
+
}
|
|
475
|
+
parent.append(element);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* A block the model keeps opaque (tables until Phase 10, block content controls), shown from
|
|
480
|
+
* its XML: a table as a table of its cells' paragraphs, anything else as its paragraphs. Only
|
|
481
|
+
* text is read: no formatting beyond the paragraph styles.
|
|
482
|
+
*/
|
|
483
|
+
export function opaqueBlock(node, index, resolver, doc, ratio) {
|
|
484
|
+
const attrs = (node.attrs ?? {});
|
|
485
|
+
const element = doc.createElement("div");
|
|
486
|
+
element.dataset.foliOpaque = String(attrs.label ?? "");
|
|
487
|
+
element.style.margin = "0";
|
|
488
|
+
element.style.display = "flow-root";
|
|
489
|
+
const xml = parseOpaque(String(attrs.xml ?? ""), doc);
|
|
490
|
+
let after = 0;
|
|
491
|
+
if (xml?.localName === "tbl")
|
|
492
|
+
element.append(opaqueTable(xml, resolver, doc, ratio));
|
|
493
|
+
else if (xml) {
|
|
494
|
+
const paragraphs = outermost(xml, PARAGRAPHS);
|
|
495
|
+
paragraphs.forEach((p, i) => {
|
|
496
|
+
const rendered = opaqueParagraph(p, resolver, doc, ratio);
|
|
497
|
+
if (i < paragraphs.length - 1)
|
|
498
|
+
rendered.element.style.marginBottom = `${rendered.after}px`;
|
|
499
|
+
else
|
|
500
|
+
after = rendered.after;
|
|
501
|
+
element.append(rendered.element);
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
if (!element.firstChild) {
|
|
505
|
+
// Nothing to show: a placeholder line.
|
|
506
|
+
element.style.minHeight = "1em";
|
|
507
|
+
element.style.outline = "1px dashed #999";
|
|
508
|
+
}
|
|
509
|
+
return {
|
|
510
|
+
element,
|
|
511
|
+
nodes: [index],
|
|
512
|
+
before: 0,
|
|
513
|
+
after,
|
|
514
|
+
keepNext: false,
|
|
515
|
+
keepLines: false,
|
|
516
|
+
pageBreakBefore: false,
|
|
517
|
+
section: null,
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
/** The value of a WordprocessingML child's `w:val` (by local names). */
|
|
521
|
+
function childValue(element, name, attribute = "val") {
|
|
522
|
+
for (const child of element?.children ?? []) {
|
|
523
|
+
if (child.localName !== name)
|
|
524
|
+
continue;
|
|
525
|
+
for (const a of child.attributes)
|
|
526
|
+
if (a.localName === attribute)
|
|
527
|
+
return a.value;
|
|
528
|
+
}
|
|
529
|
+
return undefined;
|
|
530
|
+
}
|
|
531
|
+
function child(element, name) {
|
|
532
|
+
for (const c of element?.children ?? [])
|
|
533
|
+
if (c.localName === name)
|
|
534
|
+
return c;
|
|
535
|
+
return undefined;
|
|
536
|
+
}
|
|
537
|
+
/** A paragraph of opaque XML: its style's formatting and its text. */
|
|
538
|
+
function opaqueParagraph(p, resolver, doc, ratio) {
|
|
539
|
+
const pPr = child(p, "pPr");
|
|
540
|
+
const style = childValue(pPr, "pStyle");
|
|
541
|
+
const justification = childValue(pPr, "jc");
|
|
542
|
+
const { paragraph, run } = resolver.paragraph({
|
|
543
|
+
...(style ? { style } : {}),
|
|
544
|
+
...(justification ? { justification } : {}),
|
|
545
|
+
});
|
|
546
|
+
const element = doc.createElement(tagOf(paragraph));
|
|
547
|
+
styleParagraph(element, paragraph);
|
|
548
|
+
styleRun(element, run, lineHeight(paragraph, run, ratio(fontStack(latin(run)))));
|
|
549
|
+
for (const r of outermost(p, RUNS)) {
|
|
550
|
+
// Deleted text is `w:delText`: not read, in markup too (shortcut: shown as accepted).
|
|
551
|
+
const text = opaqueText(r);
|
|
552
|
+
const formatted = resolver.run(run, runMarks(child(r, "rPr")));
|
|
553
|
+
const span = doc.createElement("span");
|
|
554
|
+
styleRun(span, formatted, lineHeight(paragraph, formatted, ratio(fontStack(latin(formatted)))));
|
|
555
|
+
span.textContent = text;
|
|
556
|
+
element.append(span);
|
|
557
|
+
}
|
|
558
|
+
if (!element.textContent)
|
|
559
|
+
element.append(doc.createElement("br"));
|
|
560
|
+
if (paragraph.spacing.before)
|
|
561
|
+
element.style.marginTop = `${paragraph.spacing.before * PX_PER_TWIP}px`;
|
|
562
|
+
return { element, after: paragraph.spacing.after * PX_PER_TWIP };
|
|
563
|
+
}
|
|
564
|
+
/** The run properties of verbatim XML Foli reads for showing it (`w:rPr`) as marks. */
|
|
565
|
+
function runMarks(rPr) {
|
|
566
|
+
const marks = [];
|
|
567
|
+
const on = (name) => {
|
|
568
|
+
const value = childValue(rPr, name);
|
|
569
|
+
return value === undefined || !["0", "false", "off"].includes(value);
|
|
570
|
+
};
|
|
571
|
+
const style = childValue(rPr, "rStyle");
|
|
572
|
+
if (style)
|
|
573
|
+
marks.push({ type: "characterStyle", attrs: { id: style } });
|
|
574
|
+
for (const [name, type] of [
|
|
575
|
+
["b", "bold"],
|
|
576
|
+
["i", "italic"],
|
|
577
|
+
["caps", "caps"],
|
|
578
|
+
["smallCaps", "smallCaps"],
|
|
579
|
+
["strike", "strike"],
|
|
580
|
+
])
|
|
581
|
+
if (child(rPr, name))
|
|
582
|
+
marks.push({ type, attrs: { value: on(name) } });
|
|
583
|
+
const underline = childValue(rPr, "u");
|
|
584
|
+
if (underline)
|
|
585
|
+
marks.push({ type: "underline", attrs: { style: underline } });
|
|
586
|
+
const color = childValue(rPr, "color");
|
|
587
|
+
if (color)
|
|
588
|
+
marks.push({ type: "color", attrs: { value: color } });
|
|
589
|
+
const size = Number(childValue(rPr, "sz"));
|
|
590
|
+
if (size > 0)
|
|
591
|
+
marks.push({ type: "fontSize", attrs: { value: size } });
|
|
592
|
+
const highlight = childValue(rPr, "highlight");
|
|
593
|
+
if (highlight)
|
|
594
|
+
marks.push({ type: "highlight", attrs: { value: highlight } });
|
|
595
|
+
return marks;
|
|
596
|
+
}
|
|
597
|
+
/** A WordprocessingML border (`w:top`, `w:insideH`…) as CSS; undefined when it says none. */
|
|
598
|
+
function borderCss(border) {
|
|
599
|
+
if (!border)
|
|
600
|
+
return undefined;
|
|
601
|
+
const value = childValueOf(border, "val") ?? "single";
|
|
602
|
+
if (value === "nil" || value === "none")
|
|
603
|
+
return "none";
|
|
604
|
+
const size = Number(childValueOf(border, "sz") ?? 4);
|
|
605
|
+
const color = childValueOf(border, "color");
|
|
606
|
+
const kind = BORDER_STYLES[value] ?? "solid";
|
|
607
|
+
// Sizes in eighths of a point.
|
|
608
|
+
const width = Math.max((size / 8) * (96 / 72), 0.5);
|
|
609
|
+
return `${width}px ${kind} ${color && color !== "auto" ? `#${color}` : "#000"}`;
|
|
610
|
+
}
|
|
611
|
+
/** Cell margins (`w:tblCellMar`, `w:tcMar`) in px by side, over `base`. */
|
|
612
|
+
function margins(element, base) {
|
|
613
|
+
const out = { ...base };
|
|
614
|
+
for (const side of element?.children ?? []) {
|
|
615
|
+
const name = side.localName === "start" ? "left" : side.localName === "end" ? "right" : side.localName;
|
|
616
|
+
const width = Number(childValueOf(side, "w"));
|
|
617
|
+
if (name in out && Number.isFinite(width))
|
|
618
|
+
out[name] = width * PX_PER_TWIP;
|
|
619
|
+
}
|
|
620
|
+
return out;
|
|
621
|
+
}
|
|
622
|
+
/** A table of opaque XML: rows, cells (spans, widths, margins, borders, shading) and their paragraphs. */
|
|
623
|
+
function opaqueTable(tbl, resolver, doc, ratio) {
|
|
624
|
+
const table = doc.createElement("table");
|
|
625
|
+
const { style } = table;
|
|
626
|
+
style.borderCollapse = "collapse";
|
|
627
|
+
style.tableLayout = "fixed";
|
|
628
|
+
const tblPr = child(tbl, "tblPr");
|
|
629
|
+
const borders = child(tblPr, "tblBorders");
|
|
630
|
+
for (const side of ["top", "left", "bottom", "right"]) {
|
|
631
|
+
const css = borderCss(child(borders, side));
|
|
632
|
+
if (css)
|
|
633
|
+
style.setProperty(`border-${side}`, css);
|
|
634
|
+
}
|
|
635
|
+
// Word's default cell margins: 0.08 inch left and right.
|
|
636
|
+
const tableMargins = margins(child(tblPr, "tblCellMar"), {
|
|
637
|
+
top: 0,
|
|
638
|
+
bottom: 0,
|
|
639
|
+
left: 108 * PX_PER_TWIP,
|
|
640
|
+
right: 108 * PX_PER_TWIP,
|
|
641
|
+
});
|
|
642
|
+
const grid = [...(child(tbl, "tblGrid")?.children ?? [])]
|
|
643
|
+
.filter((c) => c.localName === "gridCol")
|
|
644
|
+
.map((c) => Number(childValueOf(c, "w")) || 0);
|
|
645
|
+
const total = grid.reduce((a, b) => a + b, 0);
|
|
646
|
+
if (total)
|
|
647
|
+
style.width = `${total * PX_PER_TWIP}px`;
|
|
648
|
+
const colgroup = doc.createElement("colgroup");
|
|
649
|
+
for (const width of grid) {
|
|
650
|
+
const col = doc.createElement("col");
|
|
651
|
+
col.style.width = `${width * PX_PER_TWIP}px`;
|
|
652
|
+
colgroup.append(col);
|
|
653
|
+
}
|
|
654
|
+
table.append(colgroup);
|
|
655
|
+
const inside = {
|
|
656
|
+
horizontal: borderCss(child(borders, "insideH")),
|
|
657
|
+
vertical: borderCss(child(borders, "insideV")),
|
|
658
|
+
};
|
|
659
|
+
const body = doc.createElement("tbody");
|
|
660
|
+
for (const tr of outermost(tbl, ROWS)) {
|
|
661
|
+
const row = doc.createElement("tr");
|
|
662
|
+
const trPr = child(tr, "trPr");
|
|
663
|
+
// A header row, repeated at the top of each page the table continues on.
|
|
664
|
+
const header = childValue(trPr, "tblHeader");
|
|
665
|
+
if (child(trPr, "tblHeader") && header !== "0" && header !== "false")
|
|
666
|
+
row.dataset.foliHeader = "";
|
|
667
|
+
const rowMargins = margins(child(child(tr, "tblPrEx"), "tblCellMar"), tableMargins);
|
|
668
|
+
for (const tc of outermost(tr, CELLS)) {
|
|
669
|
+
const tcPr = child(tc, "tcPr");
|
|
670
|
+
const cell = doc.createElement("td");
|
|
671
|
+
const span = Number(childValue(tcPr, "gridSpan"));
|
|
672
|
+
if (span > 1)
|
|
673
|
+
cell.colSpan = span;
|
|
674
|
+
const fill = childValue(tcPr, "shd", "fill");
|
|
675
|
+
if (fill && fill !== "auto")
|
|
676
|
+
cell.style.backgroundColor = `#${fill}`;
|
|
677
|
+
const m = margins(child(tcPr, "tcMar"), rowMargins);
|
|
678
|
+
cell.style.padding = `${m.top}px ${m.right}px ${m.bottom}px ${m.left}px`;
|
|
679
|
+
cell.style.verticalAlign = "top";
|
|
680
|
+
if (inside.horizontal)
|
|
681
|
+
cell.style.borderTop = cell.style.borderBottom = inside.horizontal;
|
|
682
|
+
if (inside.vertical)
|
|
683
|
+
cell.style.borderLeft = cell.style.borderRight = inside.vertical;
|
|
684
|
+
const own = child(tcPr, "tcBorders");
|
|
685
|
+
for (const side of ["top", "left", "bottom", "right"]) {
|
|
686
|
+
const css = borderCss(child(own, side));
|
|
687
|
+
if (css)
|
|
688
|
+
cell.style.setProperty(`border-${side}`, css);
|
|
689
|
+
}
|
|
690
|
+
// A cell merged into the one above shows nothing of its own.
|
|
691
|
+
if (child(tcPr, "vMerge") && childValue(tcPr, "vMerge") !== "restart") {
|
|
692
|
+
row.append(cell);
|
|
693
|
+
continue;
|
|
694
|
+
}
|
|
695
|
+
for (const p of outermost(tc, CELL_CONTENT)) {
|
|
696
|
+
if (p.localName === "tbl") {
|
|
697
|
+
cell.append(opaqueTable(p, resolver, doc, ratio));
|
|
698
|
+
continue;
|
|
699
|
+
}
|
|
700
|
+
// Word counts the space after a cell's last paragraph in the row's height too.
|
|
701
|
+
const rendered = opaqueParagraph(p, resolver, doc, ratio);
|
|
702
|
+
rendered.element.style.marginBottom = `${rendered.after}px`;
|
|
703
|
+
cell.append(rendered.element);
|
|
704
|
+
}
|
|
705
|
+
row.append(cell);
|
|
706
|
+
}
|
|
707
|
+
body.append(row);
|
|
708
|
+
}
|
|
709
|
+
table.append(body);
|
|
710
|
+
return table;
|
|
711
|
+
}
|
|
712
|
+
function childValueOf(element, attribute) {
|
|
713
|
+
for (const a of element.attributes)
|
|
714
|
+
if (a.localName === attribute)
|
|
715
|
+
return a.value;
|
|
716
|
+
return undefined;
|
|
717
|
+
}
|
|
718
|
+
//# sourceMappingURL=blocks.js.map
|