@folijs/view 0.5.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +194 -0
- package/README.md +1 -1
- package/dist/clipboard.d.ts +1 -49
- package/dist/clipboard.d.ts.map +1 -1
- package/dist/clipboard.js +65 -13
- package/dist/clipboard.js.map +1 -1
- package/dist/extension.d.ts +90 -0
- package/dist/extension.d.ts.map +1 -0
- package/dist/extension.js +152 -0
- package/dist/extension.js.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/layout/flow.d.ts +1 -109
- package/dist/layout/flow.d.ts.map +1 -1
- package/dist/layout/flow.js +808 -103
- package/dist/layout/flow.js.map +1 -1
- package/dist/print.d.ts +6 -0
- package/dist/print.d.ts.map +1 -0
- package/dist/print.js +35 -0
- package/dist/print.js.map +1 -0
- package/dist/render.d.ts +1 -24
- package/dist/render.d.ts.map +1 -1
- package/dist/render.js +4 -1
- package/dist/render.js.map +1 -1
- package/dist/view.d.ts +244 -64
- package/dist/view.d.ts.map +1 -1
- package/dist/view.js +1945 -215
- package/dist/view.js.map +1 -1
- package/dist/word/blocks.d.ts +0 -146
- package/dist/word/blocks.d.ts.map +1 -1
- package/dist/word/blocks.js +294 -59
- package/dist/word/blocks.js.map +1 -1
- package/dist/word/columns.d.ts +2 -0
- package/dist/word/columns.d.ts.map +1 -0
- package/dist/word/columns.js +63 -0
- package/dist/word/columns.js.map +1 -0
- package/dist/word/controls.d.ts +2 -0
- package/dist/word/controls.d.ts.map +1 -0
- package/dist/word/controls.js +416 -0
- package/dist/word/controls.js.map +1 -0
- package/dist/word/edit.d.ts +1 -71
- package/dist/word/edit.d.ts.map +1 -1
- package/dist/word/edit.js +338 -29
- package/dist/word/edit.js.map +1 -1
- package/dist/word/matches.d.ts +2 -0
- package/dist/word/matches.d.ts.map +1 -0
- package/dist/word/matches.js +102 -0
- package/dist/word/matches.js.map +1 -0
- package/dist/word/notes.d.ts +2 -0
- package/dist/word/notes.d.ts.map +1 -0
- package/dist/word/notes.js +403 -0
- package/dist/word/notes.js.map +1 -0
- package/dist/word/pages.d.ts +30 -20
- package/dist/word/pages.d.ts.map +1 -1
- package/dist/word/pages.js +421 -229
- package/dist/word/pages.js.map +1 -1
- package/dist/word/regions.d.ts +2 -0
- package/dist/word/regions.d.ts.map +1 -0
- package/dist/word/regions.js +388 -0
- package/dist/word/regions.js.map +1 -0
- package/dist/word/table.d.ts +1 -78
- package/dist/word/table.d.ts.map +1 -1
- package/dist/word/table.js +13 -3
- package/dist/word/table.js.map +1 -1
- package/package.json +6 -3
- package/src/clipboard.ts +93 -20
- package/src/extension.ts +267 -0
- package/src/index.ts +10 -0
- package/src/layout/flow.ts +997 -116
- package/src/print.ts +55 -0
- package/src/render.ts +6 -1
- package/src/view.ts +2178 -245
- package/src/word/blocks.ts +339 -62
- package/src/word/columns.ts +81 -0
- package/src/word/controls.ts +457 -0
- package/src/word/edit.ts +381 -31
- package/src/word/matches.ts +104 -0
- package/src/word/notes.ts +464 -0
- package/src/word/pages.ts +518 -221
- package/src/word/regions.ts +488 -0
- package/src/word/table.ts +33 -8
package/dist/word/blocks.js
CHANGED
|
@@ -4,20 +4,39 @@
|
|
|
4
4
|
* anchors, and content the model keeps opaque shown as text. Read-only: built with
|
|
5
5
|
* createElement/textContent only (never innerHTML); the page layout is in `pages.ts`.
|
|
6
6
|
*/
|
|
7
|
-
import {
|
|
8
|
-
|
|
7
|
+
import { fieldDisplay, } from "@folijs/core";
|
|
8
|
+
import { CUSTOM_TYPE, foreignTextOf, Resolver, } from "@folijs/core/internal";
|
|
9
|
+
/**
|
|
10
|
+
* Marks the <br> of an explicit page break (the page flow ends the page after its line).
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
9
13
|
export const WORD_PAGE_BREAK_ATTR = "data-page-break";
|
|
10
|
-
/**
|
|
14
|
+
/**
|
|
15
|
+
* Marks the <br> of an explicit column break (the flow ends the column after its line).
|
|
16
|
+
* @internal
|
|
17
|
+
*/
|
|
18
|
+
export const WORD_COLUMN_BREAK_ATTR = "data-column-break";
|
|
19
|
+
/**
|
|
20
|
+
* CSS px per twip (1/1440 inch; CSS: 96 px per inch).
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
11
23
|
export const PX_PER_TWIP = 96 / 1440;
|
|
12
24
|
/** EMU (DrawingML's unit: 914 400 per inch) per CSS px. */
|
|
13
25
|
export const EMU_PER_PX = 914_400 / 96;
|
|
14
|
-
/**
|
|
26
|
+
/**
|
|
27
|
+
* Marks a picture's element (its resource key).
|
|
28
|
+
* @internal
|
|
29
|
+
*/
|
|
15
30
|
export const PICTURE_ATTR = "data-foli-picture";
|
|
16
|
-
/**
|
|
31
|
+
/**
|
|
32
|
+
* The accessible name of a picture with no alt text that is not decorative.
|
|
33
|
+
* @internal
|
|
34
|
+
*/
|
|
17
35
|
export const NO_ALT_LABEL = "Picture without alt text";
|
|
18
36
|
/**
|
|
19
37
|
* The URL a resource gives on its own: an image `data:` URL, or a web or `blob:` URL; none for
|
|
20
38
|
* an image in a package part (the app's resolver gives one) or anything else.
|
|
39
|
+
* @internal
|
|
21
40
|
*/
|
|
22
41
|
export function ownImageUrl(resource) {
|
|
23
42
|
const data = resource?.data;
|
|
@@ -30,6 +49,7 @@ export function ownImageUrl(resource) {
|
|
|
30
49
|
* A picture as shown: its image at its size, or, when no URL is known for it, a box of that size
|
|
31
50
|
* with its alt text. A picture with no alt text that is not decorative is marked (`data-foli-
|
|
32
51
|
* no-alt`); a decorative one has an empty alt.
|
|
52
|
+
* @internal
|
|
33
53
|
*/
|
|
34
54
|
export function pictureElement(attrs, doc, image) {
|
|
35
55
|
const key = String(attrs.resource ?? "");
|
|
@@ -75,9 +95,15 @@ export function pictureElement(attrs, doc, image) {
|
|
|
75
95
|
style.height = height;
|
|
76
96
|
return element;
|
|
77
97
|
}
|
|
78
|
-
/**
|
|
98
|
+
/**
|
|
99
|
+
* Marks the element holding a floating picture (its wrapping), drawn at its paragraph's start.
|
|
100
|
+
* @internal
|
|
101
|
+
*/
|
|
79
102
|
export const FLOAT_ATTR = "data-foli-float";
|
|
80
|
-
/**
|
|
103
|
+
/**
|
|
104
|
+
* The page geometry of `section` (Word's Letter page and margins where it does not say).
|
|
105
|
+
* @internal
|
|
106
|
+
*/
|
|
81
107
|
export function floatGeometry(section) {
|
|
82
108
|
const m = section?.margins ?? {};
|
|
83
109
|
const width = section?.pageSize?.width ?? 12_240;
|
|
@@ -164,6 +190,7 @@ function floatTop(v, height, g) {
|
|
|
164
190
|
* earlier float's (CSS), and `clear` stacks it below earlier floats on its side: several floats
|
|
165
191
|
* in a paragraph, or one above a float of an earlier paragraph, add their offsets down and do
|
|
166
192
|
* not sit side by side.
|
|
193
|
+
* @internal
|
|
167
194
|
*/
|
|
168
195
|
export function floatElements(attrs, doc, image, geometry) {
|
|
169
196
|
const float = attrs.float;
|
|
@@ -234,19 +261,26 @@ const AUTHOR_COLORS = [
|
|
|
234
261
|
"#AD1457",
|
|
235
262
|
"#5D4037",
|
|
236
263
|
];
|
|
237
|
-
/**
|
|
264
|
+
/**
|
|
265
|
+
* The color of `author`'s changes: always the same for a name.
|
|
266
|
+
* @internal
|
|
267
|
+
*/
|
|
238
268
|
export function authorColor(author) {
|
|
239
269
|
let hash = 0;
|
|
240
270
|
for (const c of author)
|
|
241
271
|
hash = (hash * 31 + (c.codePointAt(0) ?? 0)) >>> 0;
|
|
242
272
|
return AUTHOR_COLORS[hash % AUTHOR_COLORS.length];
|
|
243
273
|
}
|
|
244
|
-
/**
|
|
274
|
+
/**
|
|
275
|
+
* The bar beside a paragraph with changes (`markup` and `simple`), drawn in the margin.
|
|
276
|
+
* @internal
|
|
277
|
+
*/
|
|
245
278
|
export const CHANGE_BAR = "-4px 0 0 0 #6B6B6B";
|
|
246
279
|
/**
|
|
247
280
|
* The element around an inserted or deleted run: `ins` or `del` (named for screen readers by
|
|
248
281
|
* its title), styled for `review`. Deleted text not shown is hidden, not left out: the editable
|
|
249
282
|
* view maps positions through it.
|
|
283
|
+
* @internal
|
|
250
284
|
*/
|
|
251
285
|
export function changeElement(doc, inserted, author, date, review) {
|
|
252
286
|
const element = doc.createElement(inserted ? "ins" : "del");
|
|
@@ -263,10 +297,46 @@ export function changeElement(doc, inserted, author, date, review) {
|
|
|
263
297
|
}
|
|
264
298
|
return element;
|
|
265
299
|
}
|
|
266
|
-
/**
|
|
300
|
+
/**
|
|
301
|
+
* On the element showing a note reference's number: the note's story.
|
|
302
|
+
* @internal
|
|
303
|
+
*/
|
|
304
|
+
export const NOTE_REF_ATTR = "data-foli-note-ref";
|
|
305
|
+
/**
|
|
306
|
+
* On a number drawn for a note, at its reference or its mark: not text of the document.
|
|
307
|
+
* @internal
|
|
308
|
+
*/
|
|
309
|
+
export const NOTE_NUMBER_ATTR = "data-foli-note-number";
|
|
310
|
+
/** Word's footnote separator is two inches long; its continuation line here is the same. */
|
|
311
|
+
const SEPARATOR_WIDTH = 192;
|
|
312
|
+
/**
|
|
313
|
+
* The line a separator run draws, when `xml` is one (`w:separator`, `w:continuationSeparator`).
|
|
314
|
+
* @internal
|
|
315
|
+
*/
|
|
316
|
+
export function separatorElement(xml, doc) {
|
|
317
|
+
if (!xml)
|
|
318
|
+
return null;
|
|
319
|
+
const found = [xml, ...xml.getElementsByTagName("*")].some((e) => e.localName === "separator" || e.localName === "continuationSeparator");
|
|
320
|
+
if (!found)
|
|
321
|
+
return null;
|
|
322
|
+
const line = doc.createElement("span");
|
|
323
|
+
line.dataset.foliSeparator = "";
|
|
324
|
+
const { style } = line;
|
|
325
|
+
style.display = "inline-block";
|
|
326
|
+
style.width = `${SEPARATOR_WIDTH}px`;
|
|
327
|
+
style.maxWidth = "100%";
|
|
328
|
+
style.height = "0";
|
|
329
|
+
style.borderTop = "1px solid #000";
|
|
330
|
+
style.verticalAlign = "middle";
|
|
331
|
+
return line;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* The source for a document's pictures: the app's URL, else the resource's own.
|
|
335
|
+
* @internal
|
|
336
|
+
*/
|
|
267
337
|
export function imageSource(resources, resolve) {
|
|
268
338
|
return (key) => {
|
|
269
|
-
const resource = resources
|
|
339
|
+
const resource = resources && Object.hasOwn(resources, key) ? resources[key] : undefined;
|
|
270
340
|
if (!resource)
|
|
271
341
|
return undefined;
|
|
272
342
|
const url = resolve?.(key, resource);
|
|
@@ -288,7 +358,10 @@ const SUBSTITUTES = {
|
|
|
288
358
|
};
|
|
289
359
|
const SERIF = new Set(["cambria", "times new roman", "georgia", "garamond", "book antiqua"]);
|
|
290
360
|
const MONOSPACE = new Set(["courier new", "consolas", "courier"]);
|
|
291
|
-
/**
|
|
361
|
+
/**
|
|
362
|
+
* A CSS font-family list for a Word font name: the font, its substitutes, a generic family.
|
|
363
|
+
* @internal
|
|
364
|
+
*/
|
|
292
365
|
export function fontStack(name) {
|
|
293
366
|
const font = name ?? "Times New Roman";
|
|
294
367
|
const key = font.toLowerCase();
|
|
@@ -344,9 +417,15 @@ const BORDER_STYLES = {
|
|
|
344
417
|
none: "none",
|
|
345
418
|
nil: "none",
|
|
346
419
|
};
|
|
347
|
-
/**
|
|
420
|
+
/**
|
|
421
|
+
* The one font name a run shows for Latin text.
|
|
422
|
+
* @internal
|
|
423
|
+
*/
|
|
348
424
|
export const latin = (run) => run.font.ascii ?? run.font.hAnsi;
|
|
349
|
-
/**
|
|
425
|
+
/**
|
|
426
|
+
* A line height in CSS for text in `run` (`ratio`: its font's normal line height ratio).
|
|
427
|
+
* @internal
|
|
428
|
+
*/
|
|
350
429
|
export function lineHeight(paragraph, run, ratio) {
|
|
351
430
|
const sizePx = (run.size / 2) * (96 / 72);
|
|
352
431
|
const { line, lineRule } = paragraph.spacing;
|
|
@@ -358,7 +437,10 @@ export function lineHeight(paragraph, run, ratio) {
|
|
|
358
437
|
return `${Math.max(line * PX_PER_TWIP, ratio * sizePx)}px`;
|
|
359
438
|
return String((ratio * line) / 240);
|
|
360
439
|
}
|
|
361
|
-
/**
|
|
440
|
+
/**
|
|
441
|
+
* Run formatting as inline CSS on `element`.
|
|
442
|
+
* @internal
|
|
443
|
+
*/
|
|
362
444
|
export function styleRun(element, run, line) {
|
|
363
445
|
const { style } = element;
|
|
364
446
|
style.fontFamily = fontStack(latin(run));
|
|
@@ -400,7 +482,10 @@ export function styleRun(element, run, line) {
|
|
|
400
482
|
if (run.hidden)
|
|
401
483
|
style.display = "none";
|
|
402
484
|
}
|
|
403
|
-
/**
|
|
485
|
+
/**
|
|
486
|
+
* Paragraph properties as inline CSS on `element` (spacing is the flow's).
|
|
487
|
+
* @internal
|
|
488
|
+
*/
|
|
404
489
|
export function styleParagraph(element, paragraph) {
|
|
405
490
|
const { style } = element;
|
|
406
491
|
style.margin = "0";
|
|
@@ -434,7 +519,10 @@ export function styleParagraph(element, paragraph) {
|
|
|
434
519
|
* same drawing again for older readers).
|
|
435
520
|
*/
|
|
436
521
|
const NOT_INLINE = new Set(["txbxContent", "Fallback"]);
|
|
437
|
-
/**
|
|
522
|
+
/**
|
|
523
|
+
* Text a run of opaque XML shows: its `w:t` elements (field results, content controls).
|
|
524
|
+
* @internal
|
|
525
|
+
*/
|
|
438
526
|
export function opaqueText(element) {
|
|
439
527
|
let text = "";
|
|
440
528
|
for (const node of element.children) {
|
|
@@ -449,7 +537,10 @@ export function opaqueText(element) {
|
|
|
449
537
|
}
|
|
450
538
|
return text;
|
|
451
539
|
}
|
|
452
|
-
/**
|
|
540
|
+
/**
|
|
541
|
+
* Opaque XML's text, a line per paragraph (`w:p`), or its runs' text when it has none.
|
|
542
|
+
* @internal
|
|
543
|
+
*/
|
|
453
544
|
export function opaqueParagraphsText(element) {
|
|
454
545
|
const paragraphs = [...element.getElementsByTagName("*")].filter((e) => e.localName === "p");
|
|
455
546
|
if (element.localName === "p")
|
|
@@ -478,6 +569,7 @@ const CELL_CONTENT = new Set(["p", "tbl"]);
|
|
|
478
569
|
/**
|
|
479
570
|
* Parses verbatim WordprocessingML (opaque content) into an inert XML document, declaring the
|
|
480
571
|
* prefixes it uses; null when it is not well-formed.
|
|
572
|
+
* @internal
|
|
481
573
|
*/
|
|
482
574
|
export function parseOpaque(xml, doc) {
|
|
483
575
|
const view = doc.defaultView;
|
|
@@ -499,9 +591,11 @@ export function parseOpaque(xml, doc) {
|
|
|
499
591
|
/**
|
|
500
592
|
* Renders a document's blocks. `final` review merges a paragraph whose mark was deleted into
|
|
501
593
|
* the next one, as accepting the deletion does.
|
|
594
|
+
* @internal
|
|
502
595
|
*/
|
|
503
596
|
export function renderBlocks(document, doc, options = {}) {
|
|
504
597
|
const review = options.review ?? "markup";
|
|
598
|
+
const codes = options.fieldCodes === true;
|
|
505
599
|
const ratios = new Map();
|
|
506
600
|
const ratio = (family) => {
|
|
507
601
|
let value = ratios.get(family);
|
|
@@ -511,7 +605,7 @@ export function renderBlocks(document, doc, options = {}) {
|
|
|
511
605
|
}
|
|
512
606
|
return value;
|
|
513
607
|
};
|
|
514
|
-
const resolver = new Resolver(document);
|
|
608
|
+
const resolver = options.resolver ?? new Resolver(document);
|
|
515
609
|
const image = imageSource(document.resources, options.resource);
|
|
516
610
|
// shortcut: floating pictures are placed against the last section's page.
|
|
517
611
|
const geometry = floatGeometry(document.section);
|
|
@@ -530,32 +624,35 @@ export function renderBlocks(document, doc, options = {}) {
|
|
|
530
624
|
// A joined paragraph is numbered once, with the first paragraph's marker.
|
|
531
625
|
const markers = resolver.markers(content.map((node, index) => node.type === "paragraph" && !joins(index - 1) ? attrsOf(node) : { numbering: { id: 0 } }));
|
|
532
626
|
const blocks = [];
|
|
627
|
+
const displays = fieldDisplay(content.map(inlineTypes), codes);
|
|
533
628
|
let carried = null;
|
|
534
629
|
/** Comment ranges open at this point of the document. */
|
|
535
630
|
const comments = new Set();
|
|
536
631
|
content.forEach((node, index) => {
|
|
537
632
|
if (node.type !== "paragraph") {
|
|
538
|
-
blocks.push(opaqueBlock(node, index, resolver, doc, ratio, image, geometry));
|
|
633
|
+
blocks.push(opaqueBlock(node, index, resolver, doc, ratio, image, geometry, codes, options.notes));
|
|
539
634
|
return;
|
|
540
635
|
}
|
|
541
636
|
const attrs = attrsOf(node);
|
|
637
|
+
const own = displays[index];
|
|
542
638
|
if (joins(index)) {
|
|
543
|
-
const previous = carried ?? {
|
|
544
|
-
nodes: [],
|
|
545
|
-
inline: [],
|
|
546
|
-
};
|
|
639
|
+
const previous = carried ?? { nodes: [], inline: [], display: NO_DISPLAY };
|
|
547
640
|
carried = {
|
|
548
641
|
nodes: [...previous.nodes, index],
|
|
549
642
|
inline: [...previous.inline, ...(node.content ?? [])],
|
|
643
|
+
display: joinDisplays(previous.display, previous.inline.length, own),
|
|
550
644
|
};
|
|
551
645
|
return;
|
|
552
646
|
}
|
|
553
|
-
const merged = carried ?? { nodes: [], inline: [] };
|
|
647
|
+
const merged = carried ?? { nodes: [], inline: [], display: NO_DISPLAY };
|
|
554
648
|
carried = null;
|
|
649
|
+
const display = joinDisplays(merged.display, merged.inline.length, own);
|
|
555
650
|
const { paragraph, run } = resolver.paragraph(attrs);
|
|
556
651
|
const element = doc.createElement(tagOf(paragraph));
|
|
557
652
|
styleParagraph(element, paragraph);
|
|
558
|
-
|
|
653
|
+
// The line's own height is the paragraph mark's (Word has none but its runs' and the mark's).
|
|
654
|
+
const mark = resolver.run(run, (attrs.markFormat ?? []));
|
|
655
|
+
styleRun(element, mark, lineHeight(paragraph, mark, ratio(fontStack(latin(mark)))));
|
|
559
656
|
const marker = markers[index];
|
|
560
657
|
const firstMerged = merged.nodes[0];
|
|
561
658
|
const shownMarker = firstMerged !== undefined ? (markers[firstMerged] ?? null) : marker;
|
|
@@ -570,7 +667,15 @@ export function renderBlocks(document, doc, options = {}) {
|
|
|
570
667
|
ratio,
|
|
571
668
|
image,
|
|
572
669
|
geometry,
|
|
670
|
+
codes,
|
|
671
|
+
display,
|
|
672
|
+
notes: options.notes,
|
|
573
673
|
});
|
|
674
|
+
if (display.gone)
|
|
675
|
+
element.style.display = "none";
|
|
676
|
+
if (onlyPictures(inline, (node) => node.type, (node) => !!node.attrs?.float))
|
|
677
|
+
for (const picture of element.querySelectorAll(`[${PICTURE_ATTR}]`))
|
|
678
|
+
picture.style.verticalAlign = "bottom";
|
|
574
679
|
// An empty paragraph, or one ending in a line break, shows its last (empty) line; after a
|
|
575
680
|
// page break the paragraph mark stays on the break's line, as in Word.
|
|
576
681
|
const last = element.lastChild;
|
|
@@ -586,8 +691,8 @@ export function renderBlocks(document, doc, options = {}) {
|
|
|
586
691
|
blocks.push({
|
|
587
692
|
element,
|
|
588
693
|
nodes: [...merged.nodes, index],
|
|
589
|
-
before: pt(spaceBefore),
|
|
590
|
-
after: pt(spaceAfter),
|
|
694
|
+
before: display.gone ? 0 : pt(spaceBefore),
|
|
695
|
+
after: display.gone ? 0 : pt(spaceAfter),
|
|
591
696
|
keepNext: paragraph.keepNext,
|
|
592
697
|
keepLines: paragraph.keepLines,
|
|
593
698
|
pageBreakBefore: paragraph.pageBreakBefore,
|
|
@@ -597,26 +702,91 @@ export function renderBlocks(document, doc, options = {}) {
|
|
|
597
702
|
if (paragraph.contextualSpacing)
|
|
598
703
|
element.dataset.contextual = "";
|
|
599
704
|
});
|
|
600
|
-
applyContextualSpacing(blocks);
|
|
705
|
+
applyContextualSpacing(blocks, resolver.addsParagraphSpacing);
|
|
601
706
|
return blocks;
|
|
602
707
|
}
|
|
603
|
-
|
|
708
|
+
const NO_DISPLAY = fieldDisplay([[]], false)[0];
|
|
709
|
+
const inlineTypes = (node) => node.type === "paragraph" ? (node.content ?? []).map((child) => child.type) : [];
|
|
710
|
+
/** `after`, whose nodes follow `length` others, added to `before` (paragraphs drawn as one). */
|
|
711
|
+
function joinDisplays(before, length, after) {
|
|
712
|
+
if (!length)
|
|
713
|
+
return after;
|
|
714
|
+
return {
|
|
715
|
+
hidden: new Set([...before.hidden, ...[...after.hidden].map((i) => i + length)]),
|
|
716
|
+
braces: new Map([
|
|
717
|
+
...before.braces,
|
|
718
|
+
...[...after.braces].map(([i, b]) => [i + length, b]),
|
|
719
|
+
]),
|
|
720
|
+
gone: before.gone && after.gone,
|
|
721
|
+
};
|
|
722
|
+
}
|
|
723
|
+
/**
|
|
724
|
+
* Whether a paragraph's line holds nothing but inline pictures: Word's line then ends at the
|
|
725
|
+
* picture's foot (its PDFs of the corpus: no room below it for the text it does not have).
|
|
726
|
+
* @internal
|
|
727
|
+
*/
|
|
728
|
+
export function onlyPictures(inline, type, floats) {
|
|
729
|
+
let found = false;
|
|
730
|
+
for (const node of inline) {
|
|
731
|
+
const name = type(node);
|
|
732
|
+
if (name === "picture")
|
|
733
|
+
found ||= !floats(node);
|
|
734
|
+
else if (!INVISIBLE.has(name))
|
|
735
|
+
return false;
|
|
736
|
+
}
|
|
737
|
+
return found;
|
|
738
|
+
}
|
|
739
|
+
const INVISIBLE = new Set([
|
|
740
|
+
"bookmarkStart",
|
|
741
|
+
"bookmarkEnd",
|
|
742
|
+
"commentStart",
|
|
743
|
+
"commentEnd",
|
|
744
|
+
"commentReference",
|
|
745
|
+
"controlStart",
|
|
746
|
+
"controlEnd",
|
|
747
|
+
"fieldStart",
|
|
748
|
+
"fieldCode",
|
|
749
|
+
"fieldSeparator",
|
|
750
|
+
"fieldEnd",
|
|
751
|
+
]);
|
|
752
|
+
/**
|
|
753
|
+
* The top margin of a paragraph after another (whose space after is `above`) in a box the page
|
|
754
|
+
* flow does not lay out (a table cell). Adjacent margins collapse to the larger: Word's rule. In
|
|
755
|
+
* a document where the two spaces add up this margin holds both, so the collapse gives the sum
|
|
756
|
+
* and no paragraph's shading or border grows.
|
|
757
|
+
* @internal
|
|
758
|
+
*/
|
|
759
|
+
export function marginAbove(before, above, resolver) {
|
|
760
|
+
return resolver.addsParagraphSpacing ? before + above : before;
|
|
761
|
+
}
|
|
762
|
+
/**
|
|
763
|
+
* A paragraph's element: a heading of its outline level (1-6), else a paragraph.
|
|
764
|
+
* @internal
|
|
765
|
+
*/
|
|
604
766
|
export function tagOf(paragraph) {
|
|
605
767
|
const level = paragraph.outlineLevel;
|
|
606
768
|
return level !== null && level >= 0 && level < 6 ? `h${level + 1}` : "p";
|
|
607
769
|
}
|
|
608
|
-
/**
|
|
609
|
-
|
|
770
|
+
/**
|
|
771
|
+
* Word ignores the space between paragraphs of the same style that ask for it. When only the
|
|
772
|
+
* first asks, its space after still eats into the second's space before, unless the document
|
|
773
|
+
* adds the two (`adds`).
|
|
774
|
+
*/
|
|
775
|
+
function applyContextualSpacing(blocks, adds) {
|
|
610
776
|
for (let i = 1; i < blocks.length; i++) {
|
|
611
777
|
const a = blocks[i - 1];
|
|
612
778
|
const b = blocks[i];
|
|
613
779
|
const style = a.element.dataset.style;
|
|
614
780
|
if (style === undefined || style !== b.element.dataset.style)
|
|
615
781
|
continue;
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
if (
|
|
782
|
+
const first = "contextual" in a.element.dataset;
|
|
783
|
+
const second = "contextual" in b.element.dataset;
|
|
784
|
+
if (second)
|
|
619
785
|
b.before = 0;
|
|
786
|
+
else if (first && !adds)
|
|
787
|
+
b.before = Math.max(0, b.before - a.after);
|
|
788
|
+
if (first)
|
|
789
|
+
a.after = 0;
|
|
620
790
|
}
|
|
621
791
|
}
|
|
622
792
|
/** Symbol and Wingdings bullets (`w:lvlText`, private-use code points) as Unicode. */
|
|
@@ -632,7 +802,10 @@ const SYMBOLS = {
|
|
|
632
802
|
"\uF0D8": "\u27A2", // Wingdings: arrowhead
|
|
633
803
|
"\uF0FC": "\u2713", // Wingdings: check mark
|
|
634
804
|
};
|
|
635
|
-
/**
|
|
805
|
+
/**
|
|
806
|
+
* A list marker: its text, then what follows it (a tab to the text's indent, a space).
|
|
807
|
+
* @internal
|
|
808
|
+
*/
|
|
636
809
|
export function markerElement(marker, paragraph, doc, ratio) {
|
|
637
810
|
const span = doc.createElement("span");
|
|
638
811
|
span.dataset.foliMarker = "";
|
|
@@ -663,9 +836,11 @@ export function markerElement(marker, paragraph, doc, ratio) {
|
|
|
663
836
|
}
|
|
664
837
|
/** A paragraph's inline content: runs as spans, breaks, comment anchors, opaque inline content. */
|
|
665
838
|
function renderInline(parent, inline, paragraphRun, paragraph, context) {
|
|
666
|
-
const { resolver, doc, review, comments, ratio, image } = context;
|
|
839
|
+
const { resolver, doc, review, comments, ratio, image, codes, display } = context;
|
|
667
840
|
const floats = [];
|
|
668
|
-
for (const node of inline) {
|
|
841
|
+
for (const [index, node] of inline.entries()) {
|
|
842
|
+
if (display.hidden.has(index))
|
|
843
|
+
continue;
|
|
669
844
|
const marks = (node.marks ?? []);
|
|
670
845
|
const inserted = marks.some((m) => m.type === "insertion");
|
|
671
846
|
const deleted = marks.some((m) => m.type === "deletion");
|
|
@@ -685,8 +860,17 @@ function renderInline(parent, inline, paragraphRun, paragraph, context) {
|
|
|
685
860
|
parent.append(anchor);
|
|
686
861
|
continue;
|
|
687
862
|
}
|
|
688
|
-
case "bookmarkStart":
|
|
863
|
+
case "bookmarkStart": {
|
|
864
|
+
// Where a link to it goes (`@folijs/pdf`); out of the flow, so no line changes.
|
|
865
|
+
const mark = doc.createElement("span");
|
|
866
|
+
mark.dataset.foliBookmark = String(attrs.name ?? "");
|
|
867
|
+
mark.style.position = "absolute";
|
|
868
|
+
parent.append(mark);
|
|
869
|
+
continue;
|
|
870
|
+
}
|
|
689
871
|
case "bookmarkEnd":
|
|
872
|
+
case "controlStart":
|
|
873
|
+
case "controlEnd":
|
|
690
874
|
continue;
|
|
691
875
|
case "picture":
|
|
692
876
|
if (attrs.float)
|
|
@@ -695,10 +879,14 @@ function renderInline(parent, inline, paragraphRun, paragraph, context) {
|
|
|
695
879
|
parent.append(pictureElement(attrs, doc, image));
|
|
696
880
|
continue;
|
|
697
881
|
case "hardBreak":
|
|
698
|
-
case "columnBreak":
|
|
699
|
-
// shortcut: a column break ends the line; Foli lays out one column.
|
|
700
882
|
parent.append(doc.createElement("br"));
|
|
701
883
|
continue;
|
|
884
|
+
case "columnBreak": {
|
|
885
|
+
const br = doc.createElement("br");
|
|
886
|
+
br.setAttribute(WORD_COLUMN_BREAK_ATTR, "");
|
|
887
|
+
parent.append(br);
|
|
888
|
+
continue;
|
|
889
|
+
}
|
|
702
890
|
case "pageBreak": {
|
|
703
891
|
const br = doc.createElement("br");
|
|
704
892
|
br.setAttribute(WORD_PAGE_BREAK_ATTR, "");
|
|
@@ -707,13 +895,28 @@ function renderInline(parent, inline, paragraphRun, paragraph, context) {
|
|
|
707
895
|
}
|
|
708
896
|
default:
|
|
709
897
|
}
|
|
898
|
+
const code = display.braces.get(index) ?? (codes && node.type === "fieldCode" ? String(attrs.text) : "");
|
|
710
899
|
let text;
|
|
711
|
-
if (
|
|
900
|
+
if (code)
|
|
901
|
+
text = code;
|
|
902
|
+
else if (node.type === "text")
|
|
712
903
|
text = node.text ?? "";
|
|
713
904
|
else if (node.type === "tab")
|
|
714
905
|
text = "\t";
|
|
906
|
+
else if (node.type === "noteReference")
|
|
907
|
+
text = context.notes?.references?.get(String(attrs.story)) ?? "";
|
|
908
|
+
else if (node.type === "noteMark")
|
|
909
|
+
text = context.notes?.mark ?? "";
|
|
910
|
+
// A custom node without its extension here (ADR-0008): its `.docx` text, else its type.
|
|
911
|
+
else if (CUSTOM_TYPE.test(node.type))
|
|
912
|
+
text = foreignTextOf(node) ?? `[${node.type}]`;
|
|
715
913
|
else if (node.type === "opaqueInline") {
|
|
716
914
|
const xml = parseOpaque(String(attrs.xml ?? ""), doc);
|
|
915
|
+
const separator = separatorElement(xml, doc);
|
|
916
|
+
if (separator) {
|
|
917
|
+
parent.append(separator);
|
|
918
|
+
continue;
|
|
919
|
+
}
|
|
717
920
|
// A run's content (a drawing, a field character) shows as nothing; a content control or
|
|
718
921
|
// simple field as its text.
|
|
719
922
|
text = xml ? opaqueText(xml) : "";
|
|
@@ -730,6 +933,10 @@ function renderInline(parent, inline, paragraphRun, paragraph, context) {
|
|
|
730
933
|
let element = doc.createElement("span");
|
|
731
934
|
styleRun(element, run, lineHeight(paragraph, run, ratio(fontStack(latin(run)))));
|
|
732
935
|
element.textContent = text;
|
|
936
|
+
if (node.type === "noteReference")
|
|
937
|
+
element.setAttribute(NOTE_REF_ATTR, String(attrs.story));
|
|
938
|
+
if (node.type === "noteReference" || node.type === "noteMark")
|
|
939
|
+
element.setAttribute(NOTE_NUMBER_ATTR, "");
|
|
733
940
|
const link = marks.find((m) => m.type === "link");
|
|
734
941
|
if (link)
|
|
735
942
|
element.dataset.href = String(link.attrs?.href ?? `#${String(link.attrs?.anchor)}`);
|
|
@@ -756,10 +963,11 @@ function renderInline(parent, inline, paragraphRun, paragraph, context) {
|
|
|
756
963
|
* A block the model keeps opaque (tables until Phase 10, block content controls), shown from
|
|
757
964
|
* its XML: a table as a table of its cells' paragraphs, anything else as its paragraphs. Only
|
|
758
965
|
* text is read: no formatting beyond the paragraph styles.
|
|
966
|
+
* @internal
|
|
759
967
|
*/
|
|
760
968
|
export function opaqueBlock(node, index, resolver, doc, ratio, image,
|
|
761
969
|
/** The page floating pictures in its cells are placed against. */
|
|
762
|
-
geometry = floatGeometry(undefined)) {
|
|
970
|
+
geometry = floatGeometry(undefined), codes = false, notes) {
|
|
763
971
|
const attrs = (node.attrs ?? {});
|
|
764
972
|
const element = doc.createElement("div");
|
|
765
973
|
element.dataset.foliOpaque = String(attrs.label ?? "");
|
|
@@ -768,17 +976,19 @@ geometry = floatGeometry(undefined)) {
|
|
|
768
976
|
const xml = node.type === "table" ? null : parseOpaque(String(attrs.xml ?? ""), doc);
|
|
769
977
|
let after = 0;
|
|
770
978
|
if (node.type === "table")
|
|
771
|
-
element.append(modelTable(node, resolver, doc, ratio, image, geometry));
|
|
979
|
+
element.append(modelTable(node, resolver, doc, ratio, image, geometry, codes, notes));
|
|
772
980
|
else if (xml?.localName === "tbl")
|
|
773
981
|
element.append(opaqueTable(xml, resolver, doc, ratio));
|
|
774
982
|
else if (xml) {
|
|
775
983
|
const paragraphs = outermost(xml, PARAGRAPHS);
|
|
984
|
+
let above = 0;
|
|
776
985
|
paragraphs.forEach((p, i) => {
|
|
777
|
-
const rendered = opaqueParagraph(p, resolver, doc, ratio);
|
|
986
|
+
const rendered = opaqueParagraph(p, resolver, doc, ratio, above);
|
|
778
987
|
if (i < paragraphs.length - 1)
|
|
779
988
|
rendered.element.style.marginBottom = `${rendered.after}px`;
|
|
780
989
|
else
|
|
781
990
|
after = rendered.after;
|
|
991
|
+
above = rendered.after;
|
|
782
992
|
element.append(rendered.element);
|
|
783
993
|
});
|
|
784
994
|
}
|
|
@@ -816,7 +1026,9 @@ function child(element, name) {
|
|
|
816
1026
|
return undefined;
|
|
817
1027
|
}
|
|
818
1028
|
/** A paragraph of opaque XML: its style's formatting and its text. */
|
|
819
|
-
function opaqueParagraph(p, resolver, doc, ratio
|
|
1029
|
+
function opaqueParagraph(p, resolver, doc, ratio,
|
|
1030
|
+
/** The space after the paragraph before it in its box. */
|
|
1031
|
+
above = 0) {
|
|
820
1032
|
const pPr = child(p, "pPr");
|
|
821
1033
|
const style = childValue(pPr, "pStyle");
|
|
822
1034
|
const justification = childValue(pPr, "jc");
|
|
@@ -838,8 +1050,9 @@ function opaqueParagraph(p, resolver, doc, ratio) {
|
|
|
838
1050
|
}
|
|
839
1051
|
if (!element.textContent)
|
|
840
1052
|
element.append(doc.createElement("br"));
|
|
841
|
-
|
|
842
|
-
|
|
1053
|
+
const top = marginAbove(paragraph.spacing.before * PX_PER_TWIP, above, resolver);
|
|
1054
|
+
if (top)
|
|
1055
|
+
element.style.marginTop = `${top}px`;
|
|
843
1056
|
return { element, after: paragraph.spacing.after * PX_PER_TWIP };
|
|
844
1057
|
}
|
|
845
1058
|
/** The run properties of verbatim XML Foli reads for showing it (`w:rPr`) as marks. */
|
|
@@ -973,14 +1186,17 @@ function opaqueTable(tbl, resolver, doc, ratio) {
|
|
|
973
1186
|
row.append(cell);
|
|
974
1187
|
continue;
|
|
975
1188
|
}
|
|
1189
|
+
let above = 0;
|
|
976
1190
|
for (const p of outermost(tc, CELL_CONTENT)) {
|
|
977
1191
|
if (p.localName === "tbl") {
|
|
978
1192
|
cell.append(opaqueTable(p, resolver, doc, ratio));
|
|
1193
|
+
above = 0;
|
|
979
1194
|
continue;
|
|
980
1195
|
}
|
|
981
1196
|
// Word counts the space after a cell's last paragraph in the row's height too.
|
|
982
|
-
const rendered = opaqueParagraph(p, resolver, doc, ratio);
|
|
1197
|
+
const rendered = opaqueParagraph(p, resolver, doc, ratio, above);
|
|
983
1198
|
rendered.element.style.marginBottom = `${rendered.after}px`;
|
|
1199
|
+
above = rendered.after;
|
|
984
1200
|
cell.append(rendered.element);
|
|
985
1201
|
}
|
|
986
1202
|
row.append(cell);
|
|
@@ -990,7 +1206,10 @@ function opaqueTable(tbl, resolver, doc, ratio) {
|
|
|
990
1206
|
table.append(body);
|
|
991
1207
|
return table;
|
|
992
1208
|
}
|
|
993
|
-
/**
|
|
1209
|
+
/**
|
|
1210
|
+
* A border of the model (`BorderJSON`) as CSS; undefined when it says none.
|
|
1211
|
+
* @internal
|
|
1212
|
+
*/
|
|
994
1213
|
export function borderJsonCss(border) {
|
|
995
1214
|
if (!border)
|
|
996
1215
|
return undefined;
|
|
@@ -1004,10 +1223,11 @@ export function borderJsonCss(border) {
|
|
|
1004
1223
|
* A table of the model (schema v4), read-only, as opaque tables are shown: rows, cells with
|
|
1005
1224
|
* their table style's formatting (`Resolver.cell`), their paragraphs and nested tables.
|
|
1006
1225
|
*
|
|
1007
|
-
* shortcut:
|
|
1008
|
-
*
|
|
1226
|
+
* shortcut: `renderDocument`'s tables (the editing view draws its own, `table.ts`): one block,
|
|
1227
|
+
* split across pages as clips; a cell merged into the one above is shown empty, as opaque
|
|
1228
|
+
* tables are.
|
|
1009
1229
|
*/
|
|
1010
|
-
function modelTable(node, resolver, doc, ratio, image, geometry) {
|
|
1230
|
+
function modelTable(node, resolver, doc, ratio, image, geometry, codes, notes) {
|
|
1011
1231
|
const attrs = (node.attrs ?? {});
|
|
1012
1232
|
const table = doc.createElement("table");
|
|
1013
1233
|
table.style.borderCollapse = "collapse";
|
|
@@ -1034,6 +1254,7 @@ function modelTable(node, resolver, doc, ratio, image, geometry) {
|
|
|
1034
1254
|
image,
|
|
1035
1255
|
// shortcut: a floating picture in a cell is placed against the page, not its cell.
|
|
1036
1256
|
geometry,
|
|
1257
|
+
codes,
|
|
1037
1258
|
};
|
|
1038
1259
|
rows.forEach((rowNode, r) => {
|
|
1039
1260
|
const rowAttrs = (rowNode.attrs ?? {});
|
|
@@ -1069,13 +1290,18 @@ function modelTable(node, resolver, doc, ratio, image, geometry) {
|
|
|
1069
1290
|
row.append(cell);
|
|
1070
1291
|
if (cellAttrs.vMerge === "continue")
|
|
1071
1292
|
return;
|
|
1072
|
-
|
|
1293
|
+
let above = 0;
|
|
1294
|
+
const blocks = cellNode.content ?? [];
|
|
1295
|
+
const displays = fieldDisplay(blocks.map(inlineTypes), codes);
|
|
1296
|
+
for (const [b, block] of blocks.entries()) {
|
|
1073
1297
|
if (block.type === "table") {
|
|
1074
|
-
cell.append(modelTable(block, resolver, doc, ratio, image, geometry));
|
|
1298
|
+
cell.append(modelTable(block, resolver, doc, ratio, image, geometry, codes, notes));
|
|
1299
|
+
above = 0;
|
|
1075
1300
|
continue;
|
|
1076
1301
|
}
|
|
1077
1302
|
if (block.type !== "paragraph") {
|
|
1078
|
-
cell.append(opaqueBlock(block, 0, resolver, doc, ratio, image, geometry).element);
|
|
1303
|
+
cell.append(opaqueBlock(block, 0, resolver, doc, ratio, image, geometry, codes, notes).element);
|
|
1304
|
+
above = 0;
|
|
1079
1305
|
continue;
|
|
1080
1306
|
}
|
|
1081
1307
|
const { paragraph, run } = resolver.paragraph(block.attrs ?? {}, format);
|
|
@@ -1084,12 +1310,21 @@ function modelTable(node, resolver, doc, ratio, image, geometry) {
|
|
|
1084
1310
|
element.dataset.foliCell = "";
|
|
1085
1311
|
styleParagraph(element, paragraph);
|
|
1086
1312
|
styleRun(element, run, lineHeight(paragraph, run, ratio(fontStack(latin(run)))));
|
|
1087
|
-
|
|
1313
|
+
const display = displays[b];
|
|
1314
|
+
renderInline(element, block.content ?? [], run, paragraph, {
|
|
1315
|
+
...context,
|
|
1316
|
+
display,
|
|
1317
|
+
notes,
|
|
1318
|
+
});
|
|
1319
|
+
if (display.gone)
|
|
1320
|
+
element.style.display = "none";
|
|
1088
1321
|
if (!element.textContent)
|
|
1089
1322
|
element.append(doc.createElement("br"));
|
|
1090
|
-
|
|
1323
|
+
const before = paragraph.spacing.before * PX_PER_TWIP;
|
|
1324
|
+
element.style.marginTop = `${marginAbove(before, above, resolver)}px`;
|
|
1091
1325
|
// Word counts the space after a cell's last paragraph in the row's height too.
|
|
1092
|
-
|
|
1326
|
+
above = paragraph.spacing.after * PX_PER_TWIP;
|
|
1327
|
+
element.style.marginBottom = `${above}px`;
|
|
1093
1328
|
cell.append(element);
|
|
1094
1329
|
}
|
|
1095
1330
|
});
|