@folijs/view 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +33 -0
- package/dist/clipboard.d.ts +3 -1
- package/dist/clipboard.d.ts.map +1 -1
- package/dist/clipboard.js.map +1 -1
- package/dist/layout/flow.d.ts +193 -20
- package/dist/layout/flow.d.ts.map +1 -1
- package/dist/layout/flow.js +789 -101
- package/dist/layout/flow.js.map +1 -1
- package/dist/print.d.ts +22 -0
- package/dist/print.d.ts.map +1 -0
- package/dist/print.js +31 -0
- package/dist/print.js.map +1 -0
- package/dist/view.d.ts +220 -7
- package/dist/view.d.ts.map +1 -1
- package/dist/view.js +1753 -208
- package/dist/view.js.map +1 -1
- package/dist/word/blocks.d.ts +30 -1
- package/dist/word/blocks.d.ts.map +1 -1
- package/dist/word/blocks.js +193 -40
- package/dist/word/blocks.js.map +1 -1
- package/dist/word/columns.d.ts +23 -0
- package/dist/word/columns.d.ts.map +1 -0
- package/dist/word/columns.js +51 -0
- package/dist/word/columns.js.map +1 -0
- package/dist/word/controls.d.ts +63 -0
- package/dist/word/controls.d.ts.map +1 -0
- package/dist/word/controls.js +411 -0
- package/dist/word/controls.js.map +1 -0
- package/dist/word/edit.d.ts +17 -5
- package/dist/word/edit.d.ts.map +1 -1
- package/dist/word/edit.js +157 -26
- package/dist/word/edit.js.map +1 -1
- package/dist/word/matches.d.ts +23 -0
- package/dist/word/matches.d.ts.map +1 -0
- package/dist/word/matches.js +97 -0
- package/dist/word/matches.js.map +1 -0
- package/dist/word/notes.d.ts +105 -0
- package/dist/word/notes.d.ts.map +1 -0
- package/dist/word/notes.js +392 -0
- package/dist/word/notes.js.map +1 -0
- package/dist/word/pages.d.ts +18 -13
- package/dist/word/pages.d.ts.map +1 -1
- package/dist/word/pages.js +420 -229
- package/dist/word/pages.js.map +1 -1
- package/dist/word/regions.d.ts +113 -0
- package/dist/word/regions.d.ts.map +1 -0
- package/dist/word/regions.js +374 -0
- package/dist/word/regions.js.map +1 -0
- package/package.json +2 -2
- package/src/clipboard.ts +3 -0
- package/src/layout/flow.ts +945 -110
- package/src/print.ts +48 -0
- package/src/view.ts +1957 -229
- package/src/word/blocks.ts +219 -36
- package/src/word/columns.ts +66 -0
- package/src/word/controls.ts +447 -0
- package/src/word/edit.ts +175 -23
- package/src/word/matches.ts +99 -0
- package/src/word/notes.ts +451 -0
- package/src/word/pages.ts +501 -220
- package/src/word/regions.ts +469 -0
package/src/word/blocks.ts
CHANGED
|
@@ -5,7 +5,9 @@
|
|
|
5
5
|
* createElement/textContent only (never innerHTML); the page layout is in `pages.ts`.
|
|
6
6
|
*/
|
|
7
7
|
import {
|
|
8
|
+
type FieldDisplay,
|
|
8
9
|
type FoliDocument,
|
|
10
|
+
fieldDisplay,
|
|
9
11
|
type ListMarker,
|
|
10
12
|
type MarkJSON,
|
|
11
13
|
type NodeJSON,
|
|
@@ -18,6 +20,8 @@ import {
|
|
|
18
20
|
|
|
19
21
|
/** Marks the <br> of an explicit page break (the page flow ends the page after its line). */
|
|
20
22
|
export const WORD_PAGE_BREAK_ATTR = "data-page-break";
|
|
23
|
+
/** Marks the <br> of an explicit column break (the flow ends the column after its line). */
|
|
24
|
+
export const WORD_COLUMN_BREAK_ATTR = "data-column-break";
|
|
21
25
|
|
|
22
26
|
/** CSS px per twip (1/1440 inch; CSS: 96 px per inch). */
|
|
23
27
|
export const PX_PER_TWIP = 96 / 1440;
|
|
@@ -328,6 +332,8 @@ export function changeElement(
|
|
|
328
332
|
export interface BlockOptions {
|
|
329
333
|
/** `markup` (the default): insertions underlined, deletions struck through; `simple`, `final`: as if accepted. */
|
|
330
334
|
review?: ReviewView;
|
|
335
|
+
/** Fields show their codes instead of their results (Word's Alt+F9). */
|
|
336
|
+
fieldCodes?: boolean;
|
|
331
337
|
/**
|
|
332
338
|
* The ratio of a font's normal line height to its size (`fontFamily`: a CSS font-family
|
|
333
339
|
* list). Word's single line spacing is the font's own line height; multiples scale it.
|
|
@@ -340,6 +346,37 @@ export interface BlockOptions {
|
|
|
340
346
|
* resource's own image `data:` URL or web URL (fetched as it is shown).
|
|
341
347
|
*/
|
|
342
348
|
resource?: (key: string, resource: ResourceJSON) => string | null | undefined;
|
|
349
|
+
/** The document's resolver, when the caller has one (a story of it, drawn per page). */
|
|
350
|
+
resolver?: Resolver;
|
|
351
|
+
/** What note references show, by story, and what a note's own mark shows (never stored). */
|
|
352
|
+
notes?: { references?: ReadonlyMap<string, string>; mark?: string };
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/** On the element showing a note reference's number: the note's story. */
|
|
356
|
+
export const NOTE_REF_ATTR = "data-foli-note-ref";
|
|
357
|
+
/** On a number drawn for a note, at its reference or its mark: not text of the document. */
|
|
358
|
+
export const NOTE_NUMBER_ATTR = "data-foli-note-number";
|
|
359
|
+
|
|
360
|
+
/** Word's footnote separator is two inches long; its continuation line here is the same. */
|
|
361
|
+
const SEPARATOR_WIDTH = 192;
|
|
362
|
+
|
|
363
|
+
/** The line a separator run draws, when `xml` is one (`w:separator`, `w:continuationSeparator`). */
|
|
364
|
+
export function separatorElement(xml: Element | null, doc: Document): HTMLElement | null {
|
|
365
|
+
if (!xml) return null;
|
|
366
|
+
const found = [xml, ...xml.getElementsByTagName("*")].some(
|
|
367
|
+
(e) => e.localName === "separator" || e.localName === "continuationSeparator",
|
|
368
|
+
);
|
|
369
|
+
if (!found) return null;
|
|
370
|
+
const line = doc.createElement("span");
|
|
371
|
+
line.dataset.foliSeparator = "";
|
|
372
|
+
const { style } = line;
|
|
373
|
+
style.display = "inline-block";
|
|
374
|
+
style.width = `${SEPARATOR_WIDTH}px`;
|
|
375
|
+
style.maxWidth = "100%";
|
|
376
|
+
style.height = "0";
|
|
377
|
+
style.borderTop = "1px solid #000";
|
|
378
|
+
style.verticalAlign = "middle";
|
|
379
|
+
return line;
|
|
343
380
|
}
|
|
344
381
|
|
|
345
382
|
/** The source for a document's pictures: the app's URL, else the resource's own. */
|
|
@@ -348,7 +385,7 @@ export function imageSource(
|
|
|
348
385
|
resolve?: (key: string, resource: ResourceJSON) => string | null | undefined,
|
|
349
386
|
): ImageSource {
|
|
350
387
|
return (key) => {
|
|
351
|
-
const resource = resources
|
|
388
|
+
const resource = resources && Object.hasOwn(resources, key) ? resources[key] : undefined;
|
|
352
389
|
if (!resource) return undefined;
|
|
353
390
|
const url = resolve?.(key, resource);
|
|
354
391
|
// The app's answer stands, a refusal (null, "") included.
|
|
@@ -597,6 +634,7 @@ export function renderBlocks(
|
|
|
597
634
|
options: BlockOptions = {},
|
|
598
635
|
): WordBlock[] {
|
|
599
636
|
const review = options.review ?? "markup";
|
|
637
|
+
const codes = options.fieldCodes === true;
|
|
600
638
|
const ratios = new Map<string, number>();
|
|
601
639
|
const ratio = (family: string) => {
|
|
602
640
|
let value = ratios.get(family);
|
|
@@ -606,7 +644,7 @@ export function renderBlocks(
|
|
|
606
644
|
}
|
|
607
645
|
return value;
|
|
608
646
|
};
|
|
609
|
-
const resolver = new Resolver(document);
|
|
647
|
+
const resolver = options.resolver ?? new Resolver(document);
|
|
610
648
|
const image = imageSource(document.resources, options.resource);
|
|
611
649
|
// shortcut: floating pictures are placed against the last section's page.
|
|
612
650
|
const geometry = floatGeometry(document.section);
|
|
@@ -631,33 +669,38 @@ export function renderBlocks(
|
|
|
631
669
|
),
|
|
632
670
|
);
|
|
633
671
|
const blocks: WordBlock[] = [];
|
|
634
|
-
|
|
672
|
+
const displays = fieldDisplay(content.map(inlineTypes), codes);
|
|
673
|
+
let carried: { nodes: number[]; inline: NodeJSON[]; display: FieldDisplay } | null = null;
|
|
635
674
|
/** Comment ranges open at this point of the document. */
|
|
636
675
|
const comments = new Set<number>();
|
|
637
676
|
|
|
638
677
|
content.forEach((node, index) => {
|
|
639
678
|
if (node.type !== "paragraph") {
|
|
640
|
-
blocks.push(
|
|
679
|
+
blocks.push(
|
|
680
|
+
opaqueBlock(node, index, resolver, doc, ratio, image, geometry, codes, options.notes),
|
|
681
|
+
);
|
|
641
682
|
return;
|
|
642
683
|
}
|
|
643
684
|
const attrs = attrsOf(node);
|
|
685
|
+
const own = displays[index] as FieldDisplay;
|
|
644
686
|
if (joins(index)) {
|
|
645
|
-
const previous
|
|
646
|
-
nodes: [],
|
|
647
|
-
inline: [],
|
|
648
|
-
};
|
|
687
|
+
const previous = carried ?? { nodes: [], inline: [], display: NO_DISPLAY };
|
|
649
688
|
carried = {
|
|
650
689
|
nodes: [...previous.nodes, index],
|
|
651
690
|
inline: [...previous.inline, ...(node.content ?? [])],
|
|
691
|
+
display: joinDisplays(previous.display, previous.inline.length, own),
|
|
652
692
|
};
|
|
653
693
|
return;
|
|
654
694
|
}
|
|
655
|
-
const merged
|
|
695
|
+
const merged = carried ?? { nodes: [], inline: [] as NodeJSON[], display: NO_DISPLAY };
|
|
656
696
|
carried = null;
|
|
697
|
+
const display = joinDisplays(merged.display, merged.inline.length, own);
|
|
657
698
|
const { paragraph, run } = resolver.paragraph(attrs);
|
|
658
699
|
const element = doc.createElement(tagOf(paragraph));
|
|
659
700
|
styleParagraph(element, paragraph);
|
|
660
|
-
|
|
701
|
+
// The line's own height is the paragraph mark's (Word has none but its runs' and the mark's).
|
|
702
|
+
const mark = resolver.run(run, (attrs.markFormat ?? []) as MarkJSON[]);
|
|
703
|
+
styleRun(element, mark, lineHeight(paragraph, mark, ratio(fontStack(latin(mark)))));
|
|
661
704
|
const marker = markers[index];
|
|
662
705
|
const firstMerged = merged.nodes[0];
|
|
663
706
|
const shownMarker = firstMerged !== undefined ? (markers[firstMerged] ?? null) : marker;
|
|
@@ -671,7 +714,20 @@ export function renderBlocks(
|
|
|
671
714
|
ratio,
|
|
672
715
|
image,
|
|
673
716
|
geometry,
|
|
717
|
+
codes,
|
|
718
|
+
display,
|
|
719
|
+
notes: options.notes,
|
|
674
720
|
});
|
|
721
|
+
if (display.gone) element.style.display = "none";
|
|
722
|
+
if (
|
|
723
|
+
onlyPictures(
|
|
724
|
+
inline,
|
|
725
|
+
(node) => node.type,
|
|
726
|
+
(node) => !!node.attrs?.float,
|
|
727
|
+
)
|
|
728
|
+
)
|
|
729
|
+
for (const picture of element.querySelectorAll<HTMLElement>(`[${PICTURE_ATTR}]`))
|
|
730
|
+
picture.style.verticalAlign = "bottom";
|
|
675
731
|
// An empty paragraph, or one ending in a line break, shows its last (empty) line; after a
|
|
676
732
|
// page break the paragraph mark stays on the break's line, as in Word.
|
|
677
733
|
const last = element.lastChild as Element | null;
|
|
@@ -689,8 +745,8 @@ export function renderBlocks(
|
|
|
689
745
|
blocks.push({
|
|
690
746
|
element,
|
|
691
747
|
nodes: [...merged.nodes, index],
|
|
692
|
-
before: pt(spaceBefore),
|
|
693
|
-
after: pt(spaceAfter),
|
|
748
|
+
before: display.gone ? 0 : pt(spaceBefore),
|
|
749
|
+
after: display.gone ? 0 : pt(spaceAfter),
|
|
694
750
|
keepNext: paragraph.keepNext,
|
|
695
751
|
keepLines: paragraph.keepLines,
|
|
696
752
|
pageBreakBefore: paragraph.pageBreakBefore,
|
|
@@ -699,25 +755,91 @@ export function renderBlocks(
|
|
|
699
755
|
element.dataset.style = paragraph.style ?? "";
|
|
700
756
|
if (paragraph.contextualSpacing) element.dataset.contextual = "";
|
|
701
757
|
});
|
|
702
|
-
applyContextualSpacing(blocks);
|
|
758
|
+
applyContextualSpacing(blocks, resolver.addsParagraphSpacing);
|
|
703
759
|
return blocks;
|
|
704
760
|
}
|
|
705
761
|
|
|
762
|
+
const NO_DISPLAY = fieldDisplay([[]], false)[0] as FieldDisplay;
|
|
763
|
+
const inlineTypes = (node: NodeJSON) =>
|
|
764
|
+
node.type === "paragraph" ? (node.content ?? []).map((child) => child.type) : [];
|
|
765
|
+
|
|
766
|
+
/** `after`, whose nodes follow `length` others, added to `before` (paragraphs drawn as one). */
|
|
767
|
+
function joinDisplays(before: FieldDisplay, length: number, after: FieldDisplay): FieldDisplay {
|
|
768
|
+
if (!length) return after;
|
|
769
|
+
return {
|
|
770
|
+
hidden: new Set([...before.hidden, ...[...after.hidden].map((i) => i + length)]),
|
|
771
|
+
braces: new Map([
|
|
772
|
+
...before.braces,
|
|
773
|
+
...[...after.braces].map(([i, b]) => [i + length, b] as const),
|
|
774
|
+
]),
|
|
775
|
+
gone: before.gone && after.gone,
|
|
776
|
+
};
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
/**
|
|
780
|
+
* Whether a paragraph's line holds nothing but inline pictures: Word's line then ends at the
|
|
781
|
+
* picture's foot (its PDFs of the corpus: no room below it for the text it does not have).
|
|
782
|
+
*/
|
|
783
|
+
export function onlyPictures<T>(
|
|
784
|
+
inline: Iterable<T>,
|
|
785
|
+
type: (node: T) => string,
|
|
786
|
+
floats: (node: T) => boolean,
|
|
787
|
+
): boolean {
|
|
788
|
+
let found = false;
|
|
789
|
+
for (const node of inline) {
|
|
790
|
+
const name = type(node);
|
|
791
|
+
if (name === "picture") found ||= !floats(node);
|
|
792
|
+
else if (!INVISIBLE.has(name)) return false;
|
|
793
|
+
}
|
|
794
|
+
return found;
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
const INVISIBLE = new Set([
|
|
798
|
+
"bookmarkStart",
|
|
799
|
+
"bookmarkEnd",
|
|
800
|
+
"commentStart",
|
|
801
|
+
"commentEnd",
|
|
802
|
+
"commentReference",
|
|
803
|
+
"controlStart",
|
|
804
|
+
"controlEnd",
|
|
805
|
+
"fieldStart",
|
|
806
|
+
"fieldCode",
|
|
807
|
+
"fieldSeparator",
|
|
808
|
+
"fieldEnd",
|
|
809
|
+
]);
|
|
810
|
+
|
|
811
|
+
/**
|
|
812
|
+
* The top margin of a paragraph after another (whose space after is `above`) in a box the page
|
|
813
|
+
* flow does not lay out (a table cell). Adjacent margins collapse to the larger: Word's rule. In
|
|
814
|
+
* a document where the two spaces add up this margin holds both, so the collapse gives the sum
|
|
815
|
+
* and no paragraph's shading or border grows.
|
|
816
|
+
*/
|
|
817
|
+
export function marginAbove(before: number, above: number, resolver: Resolver): number {
|
|
818
|
+
return resolver.addsParagraphSpacing ? before + above : before;
|
|
819
|
+
}
|
|
820
|
+
|
|
706
821
|
/** A paragraph's element: a heading of its outline level (1-6), else a paragraph. */
|
|
707
822
|
export function tagOf(paragraph: ParagraphFormat): string {
|
|
708
823
|
const level = paragraph.outlineLevel;
|
|
709
824
|
return level !== null && level >= 0 && level < 6 ? `h${level + 1}` : "p";
|
|
710
825
|
}
|
|
711
826
|
|
|
712
|
-
/**
|
|
713
|
-
|
|
827
|
+
/**
|
|
828
|
+
* Word ignores the space between paragraphs of the same style that ask for it. When only the
|
|
829
|
+
* first asks, its space after still eats into the second's space before, unless the document
|
|
830
|
+
* adds the two (`adds`).
|
|
831
|
+
*/
|
|
832
|
+
function applyContextualSpacing(blocks: WordBlock[], adds: boolean): void {
|
|
714
833
|
for (let i = 1; i < blocks.length; i++) {
|
|
715
834
|
const a = blocks[i - 1] as WordBlock;
|
|
716
835
|
const b = blocks[i] as WordBlock;
|
|
717
836
|
const style = a.element.dataset.style;
|
|
718
837
|
if (style === undefined || style !== b.element.dataset.style) continue;
|
|
719
|
-
|
|
720
|
-
|
|
838
|
+
const first = "contextual" in a.element.dataset;
|
|
839
|
+
const second = "contextual" in b.element.dataset;
|
|
840
|
+
if (second) b.before = 0;
|
|
841
|
+
else if (first && !adds) b.before = Math.max(0, b.before - a.after);
|
|
842
|
+
if (first) a.after = 0;
|
|
721
843
|
}
|
|
722
844
|
}
|
|
723
845
|
|
|
@@ -781,6 +903,11 @@ interface InlineContext {
|
|
|
781
903
|
image?: ImageSource | undefined;
|
|
782
904
|
/** The page floating pictures are positioned against. */
|
|
783
905
|
geometry: FloatGeometry;
|
|
906
|
+
/** Whether fields show their codes. */
|
|
907
|
+
codes: boolean;
|
|
908
|
+
/** What these nodes show of their fields. */
|
|
909
|
+
display: FieldDisplay;
|
|
910
|
+
notes: BlockOptions["notes"] | undefined;
|
|
784
911
|
}
|
|
785
912
|
|
|
786
913
|
/** A paragraph's inline content: runs as spans, breaks, comment anchors, opaque inline content. */
|
|
@@ -791,9 +918,10 @@ function renderInline(
|
|
|
791
918
|
paragraph: ParagraphFormat,
|
|
792
919
|
context: InlineContext,
|
|
793
920
|
): void {
|
|
794
|
-
const { resolver, doc, review, comments, ratio, image } = context;
|
|
921
|
+
const { resolver, doc, review, comments, ratio, image, codes, display } = context;
|
|
795
922
|
const floats: HTMLElement[] = [];
|
|
796
|
-
for (const node of inline) {
|
|
923
|
+
for (const [index, node] of inline.entries()) {
|
|
924
|
+
if (display.hidden.has(index)) continue;
|
|
797
925
|
const marks = (node.marks ?? []) as MarkJSON[];
|
|
798
926
|
const inserted = marks.some((m) => m.type === "insertion");
|
|
799
927
|
const deleted = marks.some((m) => m.type === "deletion");
|
|
@@ -812,18 +940,31 @@ function renderInline(
|
|
|
812
940
|
parent.append(anchor);
|
|
813
941
|
continue;
|
|
814
942
|
}
|
|
815
|
-
case "bookmarkStart":
|
|
943
|
+
case "bookmarkStart": {
|
|
944
|
+
// Where a link to it goes (`@folijs/pdf`); out of the flow, so no line changes.
|
|
945
|
+
const mark = doc.createElement("span");
|
|
946
|
+
mark.dataset.foliBookmark = String(attrs.name ?? "");
|
|
947
|
+
mark.style.position = "absolute";
|
|
948
|
+
parent.append(mark);
|
|
949
|
+
continue;
|
|
950
|
+
}
|
|
816
951
|
case "bookmarkEnd":
|
|
952
|
+
case "controlStart":
|
|
953
|
+
case "controlEnd":
|
|
817
954
|
continue;
|
|
818
955
|
case "picture":
|
|
819
956
|
if (attrs.float) floats.push(...floatElements(attrs, doc, image, context.geometry).nodes);
|
|
820
957
|
else parent.append(pictureElement(attrs, doc, image));
|
|
821
958
|
continue;
|
|
822
959
|
case "hardBreak":
|
|
823
|
-
case "columnBreak":
|
|
824
|
-
// shortcut: a column break ends the line; Foli lays out one column.
|
|
825
960
|
parent.append(doc.createElement("br"));
|
|
826
961
|
continue;
|
|
962
|
+
case "columnBreak": {
|
|
963
|
+
const br = doc.createElement("br");
|
|
964
|
+
br.setAttribute(WORD_COLUMN_BREAK_ATTR, "");
|
|
965
|
+
parent.append(br);
|
|
966
|
+
continue;
|
|
967
|
+
}
|
|
827
968
|
case "pageBreak": {
|
|
828
969
|
const br = doc.createElement("br");
|
|
829
970
|
br.setAttribute(WORD_PAGE_BREAK_ATTR, "");
|
|
@@ -832,11 +973,22 @@ function renderInline(
|
|
|
832
973
|
}
|
|
833
974
|
default:
|
|
834
975
|
}
|
|
976
|
+
const code =
|
|
977
|
+
display.braces.get(index) ?? (codes && node.type === "fieldCode" ? String(attrs.text) : "");
|
|
835
978
|
let text: string;
|
|
836
|
-
if (
|
|
979
|
+
if (code) text = code;
|
|
980
|
+
else if (node.type === "text") text = node.text ?? "";
|
|
837
981
|
else if (node.type === "tab") text = "\t";
|
|
982
|
+
else if (node.type === "noteReference")
|
|
983
|
+
text = context.notes?.references?.get(String(attrs.story)) ?? "";
|
|
984
|
+
else if (node.type === "noteMark") text = context.notes?.mark ?? "";
|
|
838
985
|
else if (node.type === "opaqueInline") {
|
|
839
986
|
const xml = parseOpaque(String(attrs.xml ?? ""), doc);
|
|
987
|
+
const separator = separatorElement(xml, doc);
|
|
988
|
+
if (separator) {
|
|
989
|
+
parent.append(separator);
|
|
990
|
+
continue;
|
|
991
|
+
}
|
|
840
992
|
// A run's content (a drawing, a field character) shows as nothing; a content control or
|
|
841
993
|
// simple field as its text.
|
|
842
994
|
text = xml ? opaqueText(xml) : "";
|
|
@@ -851,6 +1003,9 @@ function renderInline(
|
|
|
851
1003
|
let element: HTMLElement = doc.createElement("span");
|
|
852
1004
|
styleRun(element, run, lineHeight(paragraph, run, ratio(fontStack(latin(run)))));
|
|
853
1005
|
element.textContent = text;
|
|
1006
|
+
if (node.type === "noteReference") element.setAttribute(NOTE_REF_ATTR, String(attrs.story));
|
|
1007
|
+
if (node.type === "noteReference" || node.type === "noteMark")
|
|
1008
|
+
element.setAttribute(NOTE_NUMBER_ATTR, "");
|
|
854
1009
|
const link = marks.find((m) => m.type === "link");
|
|
855
1010
|
if (link) element.dataset.href = String(link.attrs?.href ?? `#${String(link.attrs?.anchor)}`);
|
|
856
1011
|
if (comments.size) {
|
|
@@ -887,6 +1042,8 @@ export function opaqueBlock(
|
|
|
887
1042
|
image?: ImageSource,
|
|
888
1043
|
/** The page floating pictures in its cells are placed against. */
|
|
889
1044
|
geometry: FloatGeometry = floatGeometry(undefined),
|
|
1045
|
+
codes = false,
|
|
1046
|
+
notes?: BlockOptions["notes"],
|
|
890
1047
|
): WordBlock {
|
|
891
1048
|
const attrs = (node.attrs ?? {}) as Record<string, unknown>;
|
|
892
1049
|
const element = doc.createElement("div");
|
|
@@ -896,14 +1053,16 @@ export function opaqueBlock(
|
|
|
896
1053
|
const xml = node.type === "table" ? null : parseOpaque(String(attrs.xml ?? ""), doc);
|
|
897
1054
|
let after = 0;
|
|
898
1055
|
if (node.type === "table")
|
|
899
|
-
element.append(modelTable(node, resolver, doc, ratio, image, geometry));
|
|
1056
|
+
element.append(modelTable(node, resolver, doc, ratio, image, geometry, codes, notes));
|
|
900
1057
|
else if (xml?.localName === "tbl") element.append(opaqueTable(xml, resolver, doc, ratio));
|
|
901
1058
|
else if (xml) {
|
|
902
1059
|
const paragraphs = outermost(xml, PARAGRAPHS);
|
|
1060
|
+
let above = 0;
|
|
903
1061
|
paragraphs.forEach((p, i) => {
|
|
904
|
-
const rendered = opaqueParagraph(p, resolver, doc, ratio);
|
|
1062
|
+
const rendered = opaqueParagraph(p, resolver, doc, ratio, above);
|
|
905
1063
|
if (i < paragraphs.length - 1) rendered.element.style.marginBottom = `${rendered.after}px`;
|
|
906
1064
|
else after = rendered.after;
|
|
1065
|
+
above = rendered.after;
|
|
907
1066
|
element.append(rendered.element);
|
|
908
1067
|
});
|
|
909
1068
|
}
|
|
@@ -943,6 +1102,8 @@ function opaqueParagraph(
|
|
|
943
1102
|
resolver: Resolver,
|
|
944
1103
|
doc: Document,
|
|
945
1104
|
ratio: (family: string) => number,
|
|
1105
|
+
/** The space after the paragraph before it in its box. */
|
|
1106
|
+
above = 0,
|
|
946
1107
|
): { element: HTMLElement; after: number } {
|
|
947
1108
|
const pPr = child(p, "pPr");
|
|
948
1109
|
const style = childValue(pPr, "pStyle");
|
|
@@ -964,8 +1125,8 @@ function opaqueParagraph(
|
|
|
964
1125
|
element.append(span);
|
|
965
1126
|
}
|
|
966
1127
|
if (!element.textContent) element.append(doc.createElement("br"));
|
|
967
|
-
|
|
968
|
-
|
|
1128
|
+
const top = marginAbove(paragraph.spacing.before * PX_PER_TWIP, above, resolver);
|
|
1129
|
+
if (top) element.style.marginTop = `${top}px`;
|
|
969
1130
|
return { element, after: paragraph.spacing.after * PX_PER_TWIP };
|
|
970
1131
|
}
|
|
971
1132
|
|
|
@@ -1093,14 +1254,17 @@ function opaqueTable(
|
|
|
1093
1254
|
row.append(cell);
|
|
1094
1255
|
continue;
|
|
1095
1256
|
}
|
|
1257
|
+
let above = 0;
|
|
1096
1258
|
for (const p of outermost(tc, CELL_CONTENT)) {
|
|
1097
1259
|
if (p.localName === "tbl") {
|
|
1098
1260
|
cell.append(opaqueTable(p, resolver, doc, ratio));
|
|
1261
|
+
above = 0;
|
|
1099
1262
|
continue;
|
|
1100
1263
|
}
|
|
1101
1264
|
// Word counts the space after a cell's last paragraph in the row's height too.
|
|
1102
|
-
const rendered = opaqueParagraph(p, resolver, doc, ratio);
|
|
1265
|
+
const rendered = opaqueParagraph(p, resolver, doc, ratio, above);
|
|
1103
1266
|
rendered.element.style.marginBottom = `${rendered.after}px`;
|
|
1267
|
+
above = rendered.after;
|
|
1104
1268
|
cell.append(rendered.element);
|
|
1105
1269
|
}
|
|
1106
1270
|
row.append(cell);
|
|
@@ -1126,8 +1290,9 @@ export function borderJsonCss(
|
|
|
1126
1290
|
* A table of the model (schema v4), read-only, as opaque tables are shown: rows, cells with
|
|
1127
1291
|
* their table style's formatting (`Resolver.cell`), their paragraphs and nested tables.
|
|
1128
1292
|
*
|
|
1129
|
-
* shortcut:
|
|
1130
|
-
*
|
|
1293
|
+
* shortcut: `renderDocument`'s tables (the editing view draws its own, `table.ts`): one block,
|
|
1294
|
+
* split across pages as clips; a cell merged into the one above is shown empty, as opaque
|
|
1295
|
+
* tables are.
|
|
1131
1296
|
*/
|
|
1132
1297
|
function modelTable(
|
|
1133
1298
|
node: NodeJSON,
|
|
@@ -1136,6 +1301,8 @@ function modelTable(
|
|
|
1136
1301
|
ratio: (family: string) => number,
|
|
1137
1302
|
image: ImageSource | undefined,
|
|
1138
1303
|
geometry: FloatGeometry,
|
|
1304
|
+
codes: boolean,
|
|
1305
|
+
notes: BlockOptions["notes"],
|
|
1139
1306
|
): HTMLElement {
|
|
1140
1307
|
const attrs = (node.attrs ?? {}) as Record<string, unknown>;
|
|
1141
1308
|
const table = doc.createElement("table");
|
|
@@ -1162,6 +1329,7 @@ function modelTable(
|
|
|
1162
1329
|
image,
|
|
1163
1330
|
// shortcut: a floating picture in a cell is placed against the page, not its cell.
|
|
1164
1331
|
geometry,
|
|
1332
|
+
codes,
|
|
1165
1333
|
};
|
|
1166
1334
|
rows.forEach((rowNode, r) => {
|
|
1167
1335
|
const rowAttrs = (rowNode.attrs ?? {}) as Record<string, unknown>;
|
|
@@ -1191,13 +1359,20 @@ function modelTable(
|
|
|
1191
1359
|
}
|
|
1192
1360
|
row.append(cell);
|
|
1193
1361
|
if (cellAttrs.vMerge === "continue") return;
|
|
1194
|
-
|
|
1362
|
+
let above = 0;
|
|
1363
|
+
const blocks = cellNode.content ?? [];
|
|
1364
|
+
const displays = fieldDisplay(blocks.map(inlineTypes), codes);
|
|
1365
|
+
for (const [b, block] of blocks.entries()) {
|
|
1195
1366
|
if (block.type === "table") {
|
|
1196
|
-
cell.append(modelTable(block, resolver, doc, ratio, image, geometry));
|
|
1367
|
+
cell.append(modelTable(block, resolver, doc, ratio, image, geometry, codes, notes));
|
|
1368
|
+
above = 0;
|
|
1197
1369
|
continue;
|
|
1198
1370
|
}
|
|
1199
1371
|
if (block.type !== "paragraph") {
|
|
1200
|
-
cell.append(
|
|
1372
|
+
cell.append(
|
|
1373
|
+
opaqueBlock(block, 0, resolver, doc, ratio, image, geometry, codes, notes).element,
|
|
1374
|
+
);
|
|
1375
|
+
above = 0;
|
|
1201
1376
|
continue;
|
|
1202
1377
|
}
|
|
1203
1378
|
const { paragraph, run } = resolver.paragraph(block.attrs ?? {}, format);
|
|
@@ -1206,11 +1381,19 @@ function modelTable(
|
|
|
1206
1381
|
element.dataset.foliCell = "";
|
|
1207
1382
|
styleParagraph(element, paragraph);
|
|
1208
1383
|
styleRun(element, run, lineHeight(paragraph, run, ratio(fontStack(latin(run)))));
|
|
1209
|
-
|
|
1384
|
+
const display = displays[b] as FieldDisplay;
|
|
1385
|
+
renderInline(element, block.content ?? [], run, paragraph, {
|
|
1386
|
+
...context,
|
|
1387
|
+
display,
|
|
1388
|
+
notes,
|
|
1389
|
+
});
|
|
1390
|
+
if (display.gone) element.style.display = "none";
|
|
1210
1391
|
if (!element.textContent) element.append(doc.createElement("br"));
|
|
1211
|
-
|
|
1392
|
+
const before = paragraph.spacing.before * PX_PER_TWIP;
|
|
1393
|
+
element.style.marginTop = `${marginAbove(before, above, resolver)}px`;
|
|
1212
1394
|
// Word counts the space after a cell's last paragraph in the row's height too.
|
|
1213
|
-
|
|
1395
|
+
above = paragraph.spacing.after * PX_PER_TWIP;
|
|
1396
|
+
element.style.marginBottom = `${above}px`;
|
|
1214
1397
|
cell.append(element);
|
|
1215
1398
|
}
|
|
1216
1399
|
});
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A band of columns on a page (11.6, ADR-0007): one per section on the page, for both renderers.
|
|
3
|
+
*/
|
|
4
|
+
import { type SectionJSON, sectionColumns } from "@folijs/core";
|
|
5
|
+
import { PX_PER_TWIP } from "./blocks.js";
|
|
6
|
+
|
|
7
|
+
export const BAND_CLASS = "foli-band";
|
|
8
|
+
export const COLUMN_CLASS = "foli-column";
|
|
9
|
+
|
|
10
|
+
/** A section's columns in CSS px. */
|
|
11
|
+
export interface ColumnsPx {
|
|
12
|
+
widths: number[];
|
|
13
|
+
gaps: number[];
|
|
14
|
+
separator: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function columnsPx(section: SectionJSON): ColumnsPx {
|
|
18
|
+
const { widths, gaps, separator } = sectionColumns(section);
|
|
19
|
+
return {
|
|
20
|
+
widths: widths.map((width) => width * PX_PER_TWIP),
|
|
21
|
+
gaps: gaps.map((gap) => gap * PX_PER_TWIP),
|
|
22
|
+
separator,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** What identifies a band's look: the same key, the same elements can stay. */
|
|
27
|
+
export const columnsKey = ({ widths, gaps, separator }: ColumnsPx) =>
|
|
28
|
+
`${widths.join(" ")}|${gaps.join(" ")}|${separator}`;
|
|
29
|
+
|
|
30
|
+
/** Gives `band` and its `columns` their widths, spaces and the lines between them. */
|
|
31
|
+
export function styleBand(band: HTMLElement, columns: readonly HTMLElement[], c: ColumnsPx): void {
|
|
32
|
+
band.className = BAND_CLASS;
|
|
33
|
+
band.style.display = "flex";
|
|
34
|
+
// The lines are backgrounds, as tall as the tallest column: printed like text.
|
|
35
|
+
if (c.separator && c.gaps.length) {
|
|
36
|
+
let x = 0;
|
|
37
|
+
const at = c.gaps.map((gap, i) => {
|
|
38
|
+
x += (c.widths[i] as number) + gap;
|
|
39
|
+
return x - gap / 2;
|
|
40
|
+
});
|
|
41
|
+
band.style.backgroundImage = at.map(() => "linear-gradient(#000,#000)").join(",");
|
|
42
|
+
band.style.backgroundRepeat = "no-repeat";
|
|
43
|
+
band.style.backgroundSize = at.map(() => "1px 100%").join(",");
|
|
44
|
+
band.style.backgroundPosition = at.map((p) => `${p - 0.5}px 0`).join(",");
|
|
45
|
+
band.style.printColorAdjust = "exact";
|
|
46
|
+
}
|
|
47
|
+
columns.forEach((column, i) => {
|
|
48
|
+
column.className = COLUMN_CLASS;
|
|
49
|
+
column.style.flex = "none";
|
|
50
|
+
column.style.display = "flow-root";
|
|
51
|
+
column.style.width = `${c.widths[i]}px`;
|
|
52
|
+
column.style.marginRight = `${c.gaps[i] ?? 0}px`;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** A new band of `c`'s columns. */
|
|
57
|
+
export function bandElement(
|
|
58
|
+
doc: Document,
|
|
59
|
+
c: ColumnsPx,
|
|
60
|
+
): { band: HTMLElement; columns: HTMLElement[] } {
|
|
61
|
+
const band = doc.createElement("div");
|
|
62
|
+
const columns = c.widths.map(() => doc.createElement("div"));
|
|
63
|
+
styleBand(band, columns, c);
|
|
64
|
+
band.append(...columns);
|
|
65
|
+
return { band, columns };
|
|
66
|
+
}
|