@folijs/view 0.3.0 → 0.5.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 +53 -0
- 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 +2 -0
- package/dist/layout/flow.d.ts.map +1 -1
- package/dist/layout/flow.js +1 -1
- package/dist/layout/flow.js.map +1 -1
- package/dist/view.d.ts +192 -4
- package/dist/view.d.ts.map +1 -1
- package/dist/view.js +1276 -101
- package/dist/view.js.map +1 -1
- package/dist/word/blocks.d.ts +84 -4
- package/dist/word/blocks.d.ts.map +1 -1
- package/dist/word/blocks.js +405 -16
- package/dist/word/blocks.js.map +1 -1
- package/dist/word/edit.d.ts +20 -9
- package/dist/word/edit.d.ts.map +1 -1
- package/dist/word/edit.js +102 -29
- package/dist/word/edit.js.map +1 -1
- package/dist/word/pages.d.ts +8 -0
- package/dist/word/pages.d.ts.map +1 -1
- package/dist/word/pages.js +47 -2
- package/dist/word/pages.js.map +1 -1
- 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/index.ts +8 -1
- package/src/layout/flow.ts +3 -1
- package/src/view.ts +1431 -102
- package/src/word/blocks.ts +465 -16
- package/src/word/edit.ts +124 -26
- package/src/word/pages.ts +53 -2
- package/src/word/table.ts +267 -0
package/dist/word/blocks.js
CHANGED
|
@@ -9,6 +9,271 @@ import { Resolver, } from "@folijs/core";
|
|
|
9
9
|
export const WORD_PAGE_BREAK_ATTR = "data-page-break";
|
|
10
10
|
/** CSS px per twip (1/1440 inch; CSS: 96 px per inch). */
|
|
11
11
|
export const PX_PER_TWIP = 96 / 1440;
|
|
12
|
+
/** EMU (DrawingML's unit: 914 400 per inch) per CSS px. */
|
|
13
|
+
export const EMU_PER_PX = 914_400 / 96;
|
|
14
|
+
/** Marks a picture's element (its resource key). */
|
|
15
|
+
export const PICTURE_ATTR = "data-foli-picture";
|
|
16
|
+
/** The accessible name of a picture with no alt text that is not decorative. */
|
|
17
|
+
export const NO_ALT_LABEL = "Picture without alt text";
|
|
18
|
+
/**
|
|
19
|
+
* The URL a resource gives on its own: an image `data:` URL, or a web or `blob:` URL; none for
|
|
20
|
+
* an image in a package part (the app's resolver gives one) or anything else.
|
|
21
|
+
*/
|
|
22
|
+
export function ownImageUrl(resource) {
|
|
23
|
+
const data = resource?.data;
|
|
24
|
+
if (data && /^data:image\//i.test(data))
|
|
25
|
+
return data;
|
|
26
|
+
const url = resource?.url;
|
|
27
|
+
return url && /^(https?|blob):/i.test(url) ? url : undefined;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* A picture as shown: its image at its size, or, when no URL is known for it, a box of that size
|
|
31
|
+
* with its alt text. A picture with no alt text that is not decorative is marked (`data-foli-
|
|
32
|
+
* no-alt`); a decorative one has an empty alt.
|
|
33
|
+
*/
|
|
34
|
+
export function pictureElement(attrs, doc, image) {
|
|
35
|
+
const key = String(attrs.resource ?? "");
|
|
36
|
+
const width = `${Number(attrs.width) / EMU_PER_PX}px`;
|
|
37
|
+
const height = `${Number(attrs.height) / EMU_PER_PX}px`;
|
|
38
|
+
const decorative = attrs.decorative === true;
|
|
39
|
+
const own = String(attrs.alt ?? "");
|
|
40
|
+
// Only a decorative picture is silent; one missing its alt text says so.
|
|
41
|
+
const alt = decorative ? "" : own || NO_ALT_LABEL;
|
|
42
|
+
const src = image?.(key);
|
|
43
|
+
let element;
|
|
44
|
+
if (src) {
|
|
45
|
+
const img = doc.createElement("img");
|
|
46
|
+
img.src = src;
|
|
47
|
+
img.alt = alt;
|
|
48
|
+
img.draggable = false;
|
|
49
|
+
// A web URL in a document is fetched as it is shown: no referrer to go with it.
|
|
50
|
+
img.referrerPolicy = "no-referrer";
|
|
51
|
+
element = img;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
element = doc.createElement("span");
|
|
55
|
+
element.setAttribute("role", "img");
|
|
56
|
+
if (alt)
|
|
57
|
+
element.setAttribute("aria-label", alt);
|
|
58
|
+
else
|
|
59
|
+
element.setAttribute("aria-hidden", "true");
|
|
60
|
+
element.style.boxSizing = "border-box";
|
|
61
|
+
element.style.border = "1px dashed GrayText";
|
|
62
|
+
}
|
|
63
|
+
element.setAttribute(PICTURE_ATTR, key);
|
|
64
|
+
if (!own && !decorative)
|
|
65
|
+
element.dataset.foliNoAlt = "";
|
|
66
|
+
if (attrs.title)
|
|
67
|
+
element.title = String(attrs.title);
|
|
68
|
+
// In the line at its size, whatever the app's CSS says of images (`display: block`…).
|
|
69
|
+
const style = element.style;
|
|
70
|
+
style.display = src ? "inline" : "inline-block";
|
|
71
|
+
style.verticalAlign = "baseline";
|
|
72
|
+
style.maxWidth = "none";
|
|
73
|
+
style.maxHeight = "none";
|
|
74
|
+
style.width = width;
|
|
75
|
+
style.height = height;
|
|
76
|
+
return element;
|
|
77
|
+
}
|
|
78
|
+
/** Marks the element holding a floating picture (its wrapping), drawn at its paragraph's start. */
|
|
79
|
+
export const FLOAT_ATTR = "data-foli-float";
|
|
80
|
+
/** The page geometry of `section` (Word's Letter page and margins where it does not say). */
|
|
81
|
+
export function floatGeometry(section) {
|
|
82
|
+
const m = section?.margins ?? {};
|
|
83
|
+
const width = section?.pageSize?.width ?? 12_240;
|
|
84
|
+
const height = section?.pageSize?.height ?? 15_840;
|
|
85
|
+
const left = (m.left ?? 1440) + (m.gutter ?? 0);
|
|
86
|
+
const top = Math.abs(m.top ?? 1440);
|
|
87
|
+
return {
|
|
88
|
+
pageWidth: width * PX_PER_TWIP,
|
|
89
|
+
pageHeight: height * PX_PER_TWIP,
|
|
90
|
+
marginLeft: left * PX_PER_TWIP,
|
|
91
|
+
marginTop: top * PX_PER_TWIP,
|
|
92
|
+
textWidth: (width - left - (m.right ?? 1440)) * PX_PER_TWIP,
|
|
93
|
+
textHeight: (height - top - Math.abs(m.bottom ?? 1440)) * PX_PER_TWIP,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
/** Where a float's position is measured from, and the box its alignment is in (px). */
|
|
97
|
+
function horizontalBox(relativeTo, g) {
|
|
98
|
+
const right = g.pageWidth - g.marginLeft - g.textWidth;
|
|
99
|
+
switch (relativeTo) {
|
|
100
|
+
case "page":
|
|
101
|
+
return [0, g.pageWidth];
|
|
102
|
+
case "leftMargin":
|
|
103
|
+
case "insideMargin":
|
|
104
|
+
return [0, g.marginLeft];
|
|
105
|
+
case "rightMargin":
|
|
106
|
+
case "outsideMargin":
|
|
107
|
+
return [g.marginLeft + g.textWidth, right];
|
|
108
|
+
default:
|
|
109
|
+
// margin, column, character (shortcut: the column).
|
|
110
|
+
return [g.marginLeft, g.textWidth];
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function verticalBox(relativeTo, g) {
|
|
114
|
+
const bottom = g.pageHeight - g.marginTop - g.textHeight;
|
|
115
|
+
switch (relativeTo) {
|
|
116
|
+
case "page":
|
|
117
|
+
return [0, g.pageHeight];
|
|
118
|
+
case "topMargin":
|
|
119
|
+
case "insideMargin":
|
|
120
|
+
return [0, g.marginTop];
|
|
121
|
+
case "bottomMargin":
|
|
122
|
+
case "outsideMargin":
|
|
123
|
+
return [g.pageHeight - bottom, bottom];
|
|
124
|
+
default:
|
|
125
|
+
return [g.marginTop, g.textHeight];
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/** Where `size` px goes in `[start, start + box)` by offset (EMU) or alignment. */
|
|
129
|
+
function placed(position, [start, box], size) {
|
|
130
|
+
if (typeof position.offset === "number")
|
|
131
|
+
return start + position.offset / EMU_PER_PX;
|
|
132
|
+
const { align } = position;
|
|
133
|
+
if (align === "right" || align === "bottom" || align === "outside")
|
|
134
|
+
return start + box - size;
|
|
135
|
+
if (align === "center")
|
|
136
|
+
return start + (box - size) / 2;
|
|
137
|
+
return start;
|
|
138
|
+
}
|
|
139
|
+
/** Where a float's left edge is, in px from the column's left edge. */
|
|
140
|
+
function floatLeft(h, width, g) {
|
|
141
|
+
return placed(h, horizontalBox(h.relativeTo, g), width) - g.marginLeft;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Where a float's top edge is: from its paragraph's top (`paragraph`), or from the page's top
|
|
145
|
+
* (`page`) for a position against the page or its margins.
|
|
146
|
+
*/
|
|
147
|
+
function floatTop(v, height, g) {
|
|
148
|
+
if (v.relativeTo === "paragraph" || v.relativeTo === "line")
|
|
149
|
+
return { paragraph: typeof v.offset === "number" ? v.offset / EMU_PER_PX : 0 };
|
|
150
|
+
return { page: placed(v, verticalBox(v.relativeTo, g), height) };
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* A floating picture (10.9, ADR-0006) as CSS floats lay it out, put first in its anchor
|
|
154
|
+
* paragraph: `holder` holds the picture (`FLOAT_ATTR`), `nodes` go in the paragraph (a
|
|
155
|
+
* zero-width spacer float before it gives its offset from the paragraph's top).
|
|
156
|
+
* - Square (tight and through shown as square): a float on the side the picture is on, its
|
|
157
|
+
* distance from the text as its margin; text goes on the other side only.
|
|
158
|
+
* - Top and bottom: a float as wide as the paragraph, the picture in it at its position.
|
|
159
|
+
* - Behind and in front of text: absolutely positioned on the page (z-index -1 or 1; the page is
|
|
160
|
+
* the containing block and its own stacking context).
|
|
161
|
+
* shortcut: a wrapping picture placed against the page or its margins vertically is drawn at
|
|
162
|
+
* its paragraph's top; horizontal positions are against the paragraph's box (its indents in);
|
|
163
|
+
* all against `geometry` (one section's page), not each page's. A float's top is never above an
|
|
164
|
+
* earlier float's (CSS), and `clear` stacks it below earlier floats on its side: several floats
|
|
165
|
+
* in a paragraph, or one above a float of an earlier paragraph, add their offsets down and do
|
|
166
|
+
* not sit side by side.
|
|
167
|
+
*/
|
|
168
|
+
export function floatElements(attrs, doc, image, geometry) {
|
|
169
|
+
const float = attrs.float;
|
|
170
|
+
const width = Number(attrs.width) / EMU_PER_PX;
|
|
171
|
+
const height = Number(attrs.height) / EMU_PER_PX;
|
|
172
|
+
const distance = (side) => (float.distance?.[side] ?? 0) / EMU_PER_PX;
|
|
173
|
+
const picture = pictureElement(attrs, doc, image);
|
|
174
|
+
picture.style.display = "block";
|
|
175
|
+
const holder = doc.createElement("span");
|
|
176
|
+
holder.setAttribute(FLOAT_ATTR, float.wrap);
|
|
177
|
+
holder.append(picture);
|
|
178
|
+
const style = holder.style;
|
|
179
|
+
style.display = "block";
|
|
180
|
+
const x = floatLeft(float.horizontal, width, geometry);
|
|
181
|
+
const top = floatTop(float.vertical, height, geometry);
|
|
182
|
+
if (float.wrap === "behind" || float.wrap === "inFront") {
|
|
183
|
+
style.position = "absolute";
|
|
184
|
+
style.zIndex = float.wrap === "behind" ? "-1" : "1";
|
|
185
|
+
style.left = `${geometry.marginLeft + x}px`;
|
|
186
|
+
// Against the paragraph: where it would be (its static position), moved by its offset.
|
|
187
|
+
if (top.page !== undefined)
|
|
188
|
+
style.top = `${top.page}px`;
|
|
189
|
+
else
|
|
190
|
+
style.marginTop = `${top.paragraph}px`;
|
|
191
|
+
return { nodes: [holder], holder };
|
|
192
|
+
}
|
|
193
|
+
// Its distance from the text above and below as its margins: lines there go around it too.
|
|
194
|
+
const above = distance("top");
|
|
195
|
+
const down = (top.paragraph ?? 0) - above;
|
|
196
|
+
const spacer = doc.createElement("span");
|
|
197
|
+
spacer.setAttribute("aria-hidden", "true");
|
|
198
|
+
spacer.style.width = "0";
|
|
199
|
+
spacer.style.height = `${Math.max(0, down)}px`;
|
|
200
|
+
style.marginTop = `${above + Math.min(0, down)}px`;
|
|
201
|
+
style.marginBottom = `${distance("bottom")}px`;
|
|
202
|
+
if (float.wrap === "topAndBottom") {
|
|
203
|
+
spacer.style.float = "left";
|
|
204
|
+
style.float = "left";
|
|
205
|
+
style.clear = "both";
|
|
206
|
+
style.width = "100%";
|
|
207
|
+
picture.style.marginLeft = `${x}px`;
|
|
208
|
+
return { nodes: [spacer, holder], holder };
|
|
209
|
+
}
|
|
210
|
+
// Text on the side with more room (or the side `side` names): the picture floats on the other.
|
|
211
|
+
const right = float.side === "left" || (float.side !== "right" && x + width / 2 > geometry.textWidth / 2);
|
|
212
|
+
const side = right ? "right" : "left";
|
|
213
|
+
spacer.style.float = side;
|
|
214
|
+
style.float = side;
|
|
215
|
+
style.clear = side;
|
|
216
|
+
if (right) {
|
|
217
|
+
style.marginRight = `${geometry.textWidth - x - width}px`;
|
|
218
|
+
style.marginLeft = `${distance("left")}px`;
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
style.marginLeft = `${x}px`;
|
|
222
|
+
style.marginRight = `${distance("right")}px`;
|
|
223
|
+
}
|
|
224
|
+
return { nodes: [spacer, holder], holder };
|
|
225
|
+
}
|
|
226
|
+
/** Colors for the authors of changes (contrast 4.5:1 or more on white), picked by name. */
|
|
227
|
+
const AUTHOR_COLORS = [
|
|
228
|
+
"#B5082E",
|
|
229
|
+
"#1F5FAD",
|
|
230
|
+
"#2E7D32",
|
|
231
|
+
"#8E24AA",
|
|
232
|
+
"#A64B00",
|
|
233
|
+
"#00737F",
|
|
234
|
+
"#AD1457",
|
|
235
|
+
"#5D4037",
|
|
236
|
+
];
|
|
237
|
+
/** The color of `author`'s changes: always the same for a name. */
|
|
238
|
+
export function authorColor(author) {
|
|
239
|
+
let hash = 0;
|
|
240
|
+
for (const c of author)
|
|
241
|
+
hash = (hash * 31 + (c.codePointAt(0) ?? 0)) >>> 0;
|
|
242
|
+
return AUTHOR_COLORS[hash % AUTHOR_COLORS.length];
|
|
243
|
+
}
|
|
244
|
+
/** The bar beside a paragraph with changes (`markup` and `simple`), drawn in the margin. */
|
|
245
|
+
export const CHANGE_BAR = "-4px 0 0 0 #6B6B6B";
|
|
246
|
+
/**
|
|
247
|
+
* The element around an inserted or deleted run: `ins` or `del` (named for screen readers by
|
|
248
|
+
* its title), styled for `review`. Deleted text not shown is hidden, not left out: the editable
|
|
249
|
+
* view maps positions through it.
|
|
250
|
+
*/
|
|
251
|
+
export function changeElement(doc, inserted, author, date, review) {
|
|
252
|
+
const element = doc.createElement(inserted ? "ins" : "del");
|
|
253
|
+
const when = date ? `, ${date.slice(0, 10)}` : "";
|
|
254
|
+
element.title = `${inserted ? "Inserted" : "Deleted"} by ${author || "an unknown author"}${when}`;
|
|
255
|
+
if (review === "markup") {
|
|
256
|
+
element.style.color = authorColor(author);
|
|
257
|
+
element.style.textDecorationLine = inserted ? "underline" : "line-through";
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
element.style.textDecorationLine = "none";
|
|
261
|
+
if (!inserted)
|
|
262
|
+
element.style.display = "none";
|
|
263
|
+
}
|
|
264
|
+
return element;
|
|
265
|
+
}
|
|
266
|
+
/** The source for a document's pictures: the app's URL, else the resource's own. */
|
|
267
|
+
export function imageSource(resources, resolve) {
|
|
268
|
+
return (key) => {
|
|
269
|
+
const resource = resources?.[key];
|
|
270
|
+
if (!resource)
|
|
271
|
+
return undefined;
|
|
272
|
+
const url = resolve?.(key, resource);
|
|
273
|
+
// The app's answer stands, a refusal (null, "") included.
|
|
274
|
+
return url === undefined ? ownImageUrl(resource) : url || undefined;
|
|
275
|
+
};
|
|
276
|
+
}
|
|
12
277
|
/**
|
|
13
278
|
* Metric-compatible open fonts for Word's common fonts: the same advance widths and line
|
|
14
279
|
* heights, so text breaks into the same lines where the original is not installed.
|
|
@@ -247,14 +512,17 @@ export function renderBlocks(document, doc, options = {}) {
|
|
|
247
512
|
return value;
|
|
248
513
|
};
|
|
249
514
|
const resolver = new Resolver(document);
|
|
515
|
+
const image = imageSource(document.resources, options.resource);
|
|
516
|
+
// shortcut: floating pictures are placed against the last section's page.
|
|
517
|
+
const geometry = floatGeometry(document.section);
|
|
250
518
|
const content = document.content.content ?? [];
|
|
251
519
|
const attrsOf = (node) => (node.attrs ?? {});
|
|
252
|
-
// In the final
|
|
253
|
-
// table, and the document's last mark cannot be deleted).
|
|
520
|
+
// In the final and simple views a paragraph whose mark was deleted joins the next paragraph
|
|
521
|
+
// (not a table, and the document's last mark cannot be deleted).
|
|
254
522
|
const joins = (index) => {
|
|
255
523
|
const node = content[index];
|
|
256
524
|
const revision = node && attrsOf(node).revision;
|
|
257
|
-
return (review
|
|
525
|
+
return (review !== "markup" &&
|
|
258
526
|
node?.type === "paragraph" &&
|
|
259
527
|
!!revision?.deletion &&
|
|
260
528
|
content[index + 1]?.type === "paragraph");
|
|
@@ -267,7 +535,7 @@ export function renderBlocks(document, doc, options = {}) {
|
|
|
267
535
|
const comments = new Set();
|
|
268
536
|
content.forEach((node, index) => {
|
|
269
537
|
if (node.type !== "paragraph") {
|
|
270
|
-
blocks.push(opaqueBlock(node, index, resolver, doc, ratio));
|
|
538
|
+
blocks.push(opaqueBlock(node, index, resolver, doc, ratio, image, geometry));
|
|
271
539
|
return;
|
|
272
540
|
}
|
|
273
541
|
const attrs = attrsOf(node);
|
|
@@ -300,6 +568,8 @@ export function renderBlocks(document, doc, options = {}) {
|
|
|
300
568
|
review,
|
|
301
569
|
comments,
|
|
302
570
|
ratio,
|
|
571
|
+
image,
|
|
572
|
+
geometry,
|
|
303
573
|
});
|
|
304
574
|
// An empty paragraph, or one ending in a line break, shows its last (empty) line; after a
|
|
305
575
|
// page break the paragraph mark stays on the break's line, as in Word.
|
|
@@ -393,12 +663,13 @@ export function markerElement(marker, paragraph, doc, ratio) {
|
|
|
393
663
|
}
|
|
394
664
|
/** A paragraph's inline content: runs as spans, breaks, comment anchors, opaque inline content. */
|
|
395
665
|
function renderInline(parent, inline, paragraphRun, paragraph, context) {
|
|
396
|
-
const { resolver, doc, review, comments, ratio } = context;
|
|
666
|
+
const { resolver, doc, review, comments, ratio, image } = context;
|
|
667
|
+
const floats = [];
|
|
397
668
|
for (const node of inline) {
|
|
398
669
|
const marks = (node.marks ?? []);
|
|
399
670
|
const inserted = marks.some((m) => m.type === "insertion");
|
|
400
671
|
const deleted = marks.some((m) => m.type === "deletion");
|
|
401
|
-
if (deleted && review
|
|
672
|
+
if (deleted && review !== "markup")
|
|
402
673
|
continue;
|
|
403
674
|
const attrs = (node.attrs ?? {});
|
|
404
675
|
switch (node.type) {
|
|
@@ -417,6 +688,12 @@ function renderInline(parent, inline, paragraphRun, paragraph, context) {
|
|
|
417
688
|
case "bookmarkStart":
|
|
418
689
|
case "bookmarkEnd":
|
|
419
690
|
continue;
|
|
691
|
+
case "picture":
|
|
692
|
+
if (attrs.float)
|
|
693
|
+
floats.push(...floatElements(attrs, doc, image, context.geometry).nodes);
|
|
694
|
+
else
|
|
695
|
+
parent.append(pictureElement(attrs, doc, image));
|
|
696
|
+
continue;
|
|
420
697
|
case "hardBreak":
|
|
421
698
|
case "columnBreak":
|
|
422
699
|
// shortcut: a column break ends the line; Foli lays out one column.
|
|
@@ -462,33 +739,37 @@ function renderInline(parent, inline, paragraphRun, paragraph, context) {
|
|
|
462
739
|
element.style.backgroundColor = "rgba(255, 213, 79, 0.35)";
|
|
463
740
|
}
|
|
464
741
|
if (review === "markup" && (inserted || deleted)) {
|
|
465
|
-
|
|
466
|
-
const
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
wrapper.style.color = "#B5082E";
|
|
471
|
-
wrapper.style.textDecorationLine = inserted ? "underline" : "line-through";
|
|
742
|
+
// Inserted, then deleted: shown as deleted, in the deleting author's color.
|
|
743
|
+
const change = marks.find((m) => m.type === (deleted ? "deletion" : "insertion"))?.attrs;
|
|
744
|
+
const author = String(change?.author ?? "");
|
|
745
|
+
const date = change?.date ?? null;
|
|
746
|
+
const wrapper = changeElement(doc, !deleted, author, date, review);
|
|
472
747
|
wrapper.append(element);
|
|
473
748
|
element = wrapper;
|
|
474
749
|
}
|
|
475
750
|
parent.append(element);
|
|
476
751
|
}
|
|
752
|
+
// Floating pictures first in the paragraph, in order: lines from its top go around them.
|
|
753
|
+
parent.prepend(...floats);
|
|
477
754
|
}
|
|
478
755
|
/**
|
|
479
756
|
* A block the model keeps opaque (tables until Phase 10, block content controls), shown from
|
|
480
757
|
* its XML: a table as a table of its cells' paragraphs, anything else as its paragraphs. Only
|
|
481
758
|
* text is read: no formatting beyond the paragraph styles.
|
|
482
759
|
*/
|
|
483
|
-
export function opaqueBlock(node, index, resolver, doc, ratio
|
|
760
|
+
export function opaqueBlock(node, index, resolver, doc, ratio, image,
|
|
761
|
+
/** The page floating pictures in its cells are placed against. */
|
|
762
|
+
geometry = floatGeometry(undefined)) {
|
|
484
763
|
const attrs = (node.attrs ?? {});
|
|
485
764
|
const element = doc.createElement("div");
|
|
486
765
|
element.dataset.foliOpaque = String(attrs.label ?? "");
|
|
487
766
|
element.style.margin = "0";
|
|
488
767
|
element.style.display = "flow-root";
|
|
489
|
-
const xml = parseOpaque(String(attrs.xml ?? ""), doc);
|
|
768
|
+
const xml = node.type === "table" ? null : parseOpaque(String(attrs.xml ?? ""), doc);
|
|
490
769
|
let after = 0;
|
|
491
|
-
if (
|
|
770
|
+
if (node.type === "table")
|
|
771
|
+
element.append(modelTable(node, resolver, doc, ratio, image, geometry));
|
|
772
|
+
else if (xml?.localName === "tbl")
|
|
492
773
|
element.append(opaqueTable(xml, resolver, doc, ratio));
|
|
493
774
|
else if (xml) {
|
|
494
775
|
const paragraphs = outermost(xml, PARAGRAPHS);
|
|
@@ -709,6 +990,114 @@ function opaqueTable(tbl, resolver, doc, ratio) {
|
|
|
709
990
|
table.append(body);
|
|
710
991
|
return table;
|
|
711
992
|
}
|
|
993
|
+
/** A border of the model (`BorderJSON`) as CSS; undefined when it says none. */
|
|
994
|
+
export function borderJsonCss(border) {
|
|
995
|
+
if (!border)
|
|
996
|
+
return undefined;
|
|
997
|
+
if (border.style === "nil" || border.style === "none")
|
|
998
|
+
return "none";
|
|
999
|
+
const width = Math.max(((border.size ?? 4) / 8) * (96 / 72), 0.5);
|
|
1000
|
+
const color = border.color && border.color !== "auto" ? `#${border.color}` : "#000";
|
|
1001
|
+
return `${width}px ${BORDER_STYLES[border.style ?? "single"] ?? "solid"} ${color}`;
|
|
1002
|
+
}
|
|
1003
|
+
/**
|
|
1004
|
+
* A table of the model (schema v4), read-only, as opaque tables are shown: rows, cells with
|
|
1005
|
+
* their table style's formatting (`Resolver.cell`), their paragraphs and nested tables.
|
|
1006
|
+
*
|
|
1007
|
+
* shortcut: a stopgap until tables are laid out on pages and edited (10.5, 10.6): one block,
|
|
1008
|
+
* not split across pages; a cell merged into the one above is shown empty, as opaque tables are.
|
|
1009
|
+
*/
|
|
1010
|
+
function modelTable(node, resolver, doc, ratio, image, geometry) {
|
|
1011
|
+
const attrs = (node.attrs ?? {});
|
|
1012
|
+
const table = doc.createElement("table");
|
|
1013
|
+
table.style.borderCollapse = "collapse";
|
|
1014
|
+
table.style.tableLayout = "fixed";
|
|
1015
|
+
const grid = attrs.grid ?? [];
|
|
1016
|
+
const total = grid.reduce((a, b) => a + b, 0);
|
|
1017
|
+
if (total)
|
|
1018
|
+
table.style.width = `${total * PX_PER_TWIP}px`;
|
|
1019
|
+
const colgroup = doc.createElement("colgroup");
|
|
1020
|
+
for (const width of grid) {
|
|
1021
|
+
const col = doc.createElement("col");
|
|
1022
|
+
col.style.width = `${width * PX_PER_TWIP}px`;
|
|
1023
|
+
colgroup.append(col);
|
|
1024
|
+
}
|
|
1025
|
+
table.append(colgroup);
|
|
1026
|
+
const body = doc.createElement("tbody");
|
|
1027
|
+
const rows = node.content ?? [];
|
|
1028
|
+
const context = {
|
|
1029
|
+
resolver,
|
|
1030
|
+
doc,
|
|
1031
|
+
review: "markup",
|
|
1032
|
+
comments: new Set(),
|
|
1033
|
+
ratio,
|
|
1034
|
+
image,
|
|
1035
|
+
// shortcut: a floating picture in a cell is placed against the page, not its cell.
|
|
1036
|
+
geometry,
|
|
1037
|
+
};
|
|
1038
|
+
rows.forEach((rowNode, r) => {
|
|
1039
|
+
const rowAttrs = (rowNode.attrs ?? {});
|
|
1040
|
+
const row = doc.createElement("tr");
|
|
1041
|
+
if (rowAttrs.header)
|
|
1042
|
+
row.dataset.foliHeader = "";
|
|
1043
|
+
if (rowAttrs.cantSplit)
|
|
1044
|
+
row.dataset.foliCantSplit = "";
|
|
1045
|
+
const cells = rowNode.content ?? [];
|
|
1046
|
+
cells.forEach((cellNode, c) => {
|
|
1047
|
+
const cellAttrs = (cellNode.attrs ?? {});
|
|
1048
|
+
const format = resolver.cell(attrs, rowAttrs, cellAttrs, {
|
|
1049
|
+
row: r,
|
|
1050
|
+
rows: rows.length,
|
|
1051
|
+
cell: c,
|
|
1052
|
+
cells: cells.length,
|
|
1053
|
+
});
|
|
1054
|
+
const cell = doc.createElement("td");
|
|
1055
|
+
const span = cellAttrs.gridSpan;
|
|
1056
|
+
if (span && span > 1)
|
|
1057
|
+
cell.colSpan = span;
|
|
1058
|
+
if (format.shading)
|
|
1059
|
+
cell.style.backgroundColor = `#${format.shading}`;
|
|
1060
|
+
const m = format.margins;
|
|
1061
|
+
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`;
|
|
1062
|
+
cell.style.verticalAlign =
|
|
1063
|
+
format.verticalAlign === "center" ? "middle" : format.verticalAlign;
|
|
1064
|
+
for (const side of ["top", "left", "bottom", "right"]) {
|
|
1065
|
+
const css = borderJsonCss(format.borders[side]);
|
|
1066
|
+
if (css)
|
|
1067
|
+
cell.style.setProperty(`border-${side}`, css);
|
|
1068
|
+
}
|
|
1069
|
+
row.append(cell);
|
|
1070
|
+
if (cellAttrs.vMerge === "continue")
|
|
1071
|
+
return;
|
|
1072
|
+
for (const block of cellNode.content ?? []) {
|
|
1073
|
+
if (block.type === "table") {
|
|
1074
|
+
cell.append(modelTable(block, resolver, doc, ratio, image, geometry));
|
|
1075
|
+
continue;
|
|
1076
|
+
}
|
|
1077
|
+
if (block.type !== "paragraph") {
|
|
1078
|
+
cell.append(opaqueBlock(block, 0, resolver, doc, ratio, image, geometry).element);
|
|
1079
|
+
continue;
|
|
1080
|
+
}
|
|
1081
|
+
const { paragraph, run } = resolver.paragraph(block.attrs ?? {}, format);
|
|
1082
|
+
const element = doc.createElement(tagOf(paragraph));
|
|
1083
|
+
// A cell's paragraph: where it is on pages is measured too (`cellParagraphs`).
|
|
1084
|
+
element.dataset.foliCell = "";
|
|
1085
|
+
styleParagraph(element, paragraph);
|
|
1086
|
+
styleRun(element, run, lineHeight(paragraph, run, ratio(fontStack(latin(run)))));
|
|
1087
|
+
renderInline(element, block.content ?? [], run, paragraph, context);
|
|
1088
|
+
if (!element.textContent)
|
|
1089
|
+
element.append(doc.createElement("br"));
|
|
1090
|
+
element.style.marginTop = `${paragraph.spacing.before * PX_PER_TWIP}px`;
|
|
1091
|
+
// Word counts the space after a cell's last paragraph in the row's height too.
|
|
1092
|
+
element.style.marginBottom = `${paragraph.spacing.after * PX_PER_TWIP}px`;
|
|
1093
|
+
cell.append(element);
|
|
1094
|
+
}
|
|
1095
|
+
});
|
|
1096
|
+
body.append(row);
|
|
1097
|
+
});
|
|
1098
|
+
table.append(body);
|
|
1099
|
+
return table;
|
|
1100
|
+
}
|
|
712
1101
|
function childValueOf(element, attribute) {
|
|
713
1102
|
for (const a of element.attributes)
|
|
714
1103
|
if (a.localName === attribute)
|