@docx4j/core-ts 0.5.0 → 0.7.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/NOTICE +4 -0
- package/README.md +92 -4
- package/dist/html/convert.mjs +2 -1
- package/dist/html/elements.mjs +2 -1
- package/dist/model/color/ColorMath.d.mts +29 -0
- package/dist/model/color/ColorMath.mjs +146 -0
- package/dist/model/color/ColorTables.d.mts +6 -0
- package/dist/model/color/ColorTables.mjs +83 -0
- package/dist/model/color/DrawingMLColors.d.mts +52 -0
- package/dist/model/color/DrawingMLColors.mjs +374 -0
- package/dist/model/color/ResolvedColor.d.mts +36 -0
- package/dist/model/color/ResolvedColor.mjs +65 -0
- package/dist/model/color/SpreadsheetColors.d.mts +36 -0
- package/dist/model/color/SpreadsheetColors.mjs +84 -0
- package/dist/model/color/StyleFill.d.mts +28 -0
- package/dist/model/color/StyleFill.mjs +21 -0
- package/dist/model/color/ThemeColors.d.mts +86 -0
- package/dist/model/color/ThemeColors.mjs +304 -0
- package/dist/model/color/WordColors.d.mts +71 -0
- package/dist/model/color/WordColors.mjs +121 -0
- package/dist/model/color/index.d.mts +6 -0
- package/dist/model/color/index.mjs +30 -0
- package/dist/model/content/Body.d.mts +145 -23
- package/dist/model/content/Body.mjs +507 -150
- package/dist/model/content/Comment.d.mts +23 -5
- package/dist/model/content/Comment.mjs +207 -26
- package/dist/model/content/ContentControl.d.mts +198 -19
- package/dist/model/content/ContentControl.mjs +672 -99
- package/dist/model/content/Font.d.mts +70 -17
- package/dist/model/content/Font.mjs +119 -36
- package/dist/model/content/InlinePicture.d.mts +11 -0
- package/dist/model/content/InlinePicture.mjs +21 -1
- package/dist/model/content/List.d.mts +6 -3
- package/dist/model/content/List.mjs +18 -8
- package/dist/model/content/Paragraph.d.mts +200 -14
- package/dist/model/content/Paragraph.mjs +898 -82
- package/dist/model/content/Range.d.mts +227 -12
- package/dist/model/content/Range.mjs +785 -59
- package/dist/model/content/ResolvedView.d.mts +104 -0
- package/dist/model/content/ResolvedView.mjs +596 -0
- package/dist/model/content/Table.d.mts +29 -3
- package/dist/model/content/Table.mjs +135 -49
- package/dist/model/content/TrackedChange.d.mts +174 -15
- package/dist/model/content/TrackedChange.mjs +734 -170
- package/dist/model/content/comments.d.mts +20 -6
- package/dist/model/content/comments.mjs +67 -57
- package/dist/model/content/generalException.d.mts +18 -0
- package/dist/model/content/generalException.mjs +24 -0
- package/dist/model/content/index.d.mts +10 -3
- package/dist/model/content/index.mjs +10 -2
- package/dist/model/content/mathText.d.mts +21 -0
- package/dist/model/content/mathText.mjs +267 -0
- package/dist/model/content/officeText.d.mts +27 -0
- package/dist/model/content/officeText.mjs +74 -0
- package/dist/model/content/ooxml.mjs +12 -1
- package/dist/model/content/outline.d.mts +5 -0
- package/dist/model/content/outline.mjs +13 -0
- package/dist/model/content/paraIds.d.mts +5 -0
- package/dist/model/content/paraIds.mjs +54 -0
- package/dist/model/content/placeholders.d.mts +110 -0
- package/dist/model/content/placeholders.mjs +253 -0
- package/dist/model/content/search.d.mts +74 -0
- package/dist/model/content/search.mjs +255 -9
- package/dist/model/content/spanEdits.d.mts +159 -0
- package/dist/model/content/spanEdits.mjs +728 -0
- package/dist/model/content/story.d.mts +47 -0
- package/dist/model/content/story.mjs +95 -0
- package/dist/model/content/tableColumns.d.mts +1 -1
- package/dist/model/content/tableColumns.mjs +108 -80
- package/dist/model/content/tableRevisions.d.mts +11 -3
- package/dist/model/content/tableRevisions.mjs +72 -42
- package/dist/model/content/tableRules.d.mts +30 -0
- package/dist/model/content/tableRules.mjs +102 -0
- package/dist/model/content/textBoxes.d.mts +18 -0
- package/dist/model/content/textBoxes.mjs +105 -0
- package/dist/model/content/trackedChangeFilter.d.mts +39 -0
- package/dist/model/content/trackedChangeFilter.mjs +81 -0
- package/dist/model/content/tracking.d.mts +63 -6
- package/dist/model/content/tracking.mjs +242 -22
- package/dist/model/content/tree.d.mts +172 -24
- package/dist/model/content/tree.mjs +328 -54
- package/dist/model/content/wordRange.d.mts +48 -0
- package/dist/model/content/wordRange.mjs +97 -0
- package/dist/model/customxml/CustomXmlPartCollection.d.mts +30 -1
- package/dist/model/customxml/CustomXmlPartCollection.mjs +81 -12
- package/dist/model/customxml/XmlMapping.d.mts +32 -8
- package/dist/model/customxml/XmlMapping.mjs +64 -10
- package/dist/model/customxml/bindings.d.mts +33 -4
- package/dist/model/customxml/bindings.mjs +171 -45
- package/dist/model/customxml/index.d.mts +1 -0
- package/dist/model/customxml/index.mjs +1 -0
- package/dist/model/customxml/kinds.d.mts +8 -3
- package/dist/model/customxml/kinds.mjs +26 -5
- package/dist/model/customxml/opendope.d.mts +4 -1
- package/dist/model/customxml/opendope.mjs +9 -6
- package/dist/model/customxml/picturePlaceholder.d.mts +17 -0
- package/dist/model/customxml/picturePlaceholder.mjs +32 -0
- package/dist/model/customxml/pictures.d.mts +4 -3
- package/dist/model/customxml/pictures.mjs +22 -8
- package/dist/model/customxml/storeItems.d.mts +57 -0
- package/dist/model/customxml/storeItems.mjs +122 -0
- package/dist/model/customxml/xhtml.mjs +23 -22
- package/dist/model/dateZone.d.mts +25 -0
- package/dist/model/dateZone.mjs +57 -0
- package/dist/model/index.d.mts +3 -0
- package/dist/model/index.mjs +5 -0
- package/dist/model/listnumbering/Emulator.d.mts +37 -5
- package/dist/model/listnumbering/Emulator.mjs +63 -9
- package/dist/model/properties/PropertyResolver.d.mts +74 -2
- package/dist/model/properties/PropertyResolver.mjs +133 -5
- package/dist/model/properties/catalogue.d.mts +19 -5
- package/dist/model/properties/catalogue.mjs +41 -15
- package/dist/model/properties/index.d.mts +1 -1
- package/dist/model/properties/log.d.mts +1 -1
- package/dist/model/properties/log.mjs +5 -5
- package/dist/model/properties/styleUtil.d.mts +3 -1
- package/dist/model/properties/styleUtil.mjs +20 -24
- package/dist/model/properties/table.mjs +4 -2
- package/dist/model/text/CaseMapping.d.mts +38 -0
- package/dist/model/text/CaseMapping.mjs +86 -0
- package/dist/model/text/Extractor.d.mts +349 -0
- package/dist/model/text/Extractor.mjs +2406 -0
- package/dist/model/text/LinkTargets.d.mts +45 -0
- package/dist/model/text/LinkTargets.mjs +170 -0
- package/dist/model/text/MarkdownExporter.d.mts +140 -0
- package/dist/model/text/MarkdownExporter.mjs +255 -0
- package/dist/model/text/PlainText.d.mts +29 -0
- package/dist/model/text/PlainText.mjs +542 -0
- package/dist/model/text/TextContext.d.mts +256 -0
- package/dist/model/text/TextContext.mjs +600 -0
- package/dist/model/text/TextOptions.d.mts +233 -0
- package/dist/model/text/TextOptions.mjs +419 -0
- package/dist/model/text/TextResult.d.mts +621 -0
- package/dist/model/text/TextResult.mjs +629 -0
- package/dist/model/text/Texts.d.mts +99 -0
- package/dist/model/text/Texts.mjs +187 -0
- package/dist/model/text/WmlToMarkdown.d.mts +120 -0
- package/dist/model/text/WmlToMarkdown.mjs +936 -0
- package/dist/model/text/caseMapping.generated.d.mts +7 -0
- package/dist/model/text/caseMapping.generated.mjs +214 -0
- package/dist/model/text/commonmark.d.mts +121 -0
- package/dist/model/text/commonmark.mjs +655 -0
- package/dist/model/text/index.d.mts +8 -0
- package/dist/model/text/index.mjs +9 -0
- package/dist/model/text/normalize.d.mts +8 -0
- package/dist/model/text/normalize.mjs +13 -0
- package/dist/model/text/outline.d.mts +4 -0
- package/dist/model/text/outline.mjs +71 -0
- package/dist/model/wordIntegers.d.mts +29 -0
- package/dist/model/wordIntegers.mjs +78 -0
- package/dist/office-js/document.mjs +15 -1
- package/dist/office-js/fragment.d.mts +7 -5
- package/dist/office-js/fragment.mjs +63 -14
- package/dist/office-js/proxy.d.mts +8 -0
- package/dist/office-js/proxy.mjs +29 -0
- package/dist/office-js/run.mjs +2 -0
- package/dist/office-js/supported.generated.mjs +16 -0
- package/dist/office-js/toApiScript.mjs +29 -12
- package/dist/opc/DirectoryPartStore.d.mts +6 -2
- package/dist/opc/DirectoryPartStore.mjs +11 -4
- package/dist/opc/FlatOpcPartStore.d.mts +4 -2
- package/dist/opc/FlatOpcPartStore.mjs +11 -8
- package/dist/opc/Load.d.mts +32 -0
- package/dist/opc/Load.mjs +115 -5
- package/dist/opc/PartStore.d.mts +34 -1
- package/dist/opc/PartStore.mjs +71 -8
- package/dist/opc/Save.d.mts +32 -0
- package/dist/opc/Save.mjs +110 -4
- package/dist/opc/ZipPartStore.d.mts +4 -2
- package/dist/opc/ZipPartStore.mjs +12 -7
- package/dist/opc/exceptions.d.mts +140 -0
- package/dist/opc/exceptions.mjs +108 -0
- package/dist/opc/index.d.mts +8 -5
- package/dist/opc/index.mjs +5 -3
- package/dist/opc/mce/preprocessor.d.mts +141 -5
- package/dist/opc/mce/preprocessor.mjs +119 -18
- package/dist/opc/mce/understood.d.mts +3 -11
- package/dist/opc/mce/understood.mjs +27 -23
- package/dist/opc/repair/breaks.d.mts +23 -0
- package/dist/opc/repair/breaks.mjs +288 -0
- package/dist/opc/repair/hosts.d.mts +12 -0
- package/dist/opc/repair/hosts.mjs +33 -0
- package/dist/opc/repair/mainRoot.d.mts +19 -0
- package/dist/opc/repair/mainRoot.mjs +168 -0
- package/dist/opc/repair/unknownElements.d.mts +8 -0
- package/dist/opc/repair/unknownElements.mjs +180 -0
- package/dist/opc/repair/wmlNames.generated.d.mts +2 -0
- package/dist/opc/repair/wmlNames.generated.mjs +80 -0
- package/dist/opc/skipped.d.mts +50 -0
- package/dist/opc/skipped.mjs +140 -0
- package/dist/packages/OpcPackage.d.mts +110 -4
- package/dist/packages/OpcPackage.mjs +123 -7
- package/dist/packages/WordprocessingMLPackage.d.mts +45 -8
- package/dist/packages/WordprocessingMLPackage.mjs +158 -21
- package/dist/packages/index.d.mts +1 -1
- package/dist/parts/DefaultXmlPart.mjs +2 -2
- package/dist/parts/Part.d.mts +14 -2
- package/dist/parts/RelationshipsPart.d.mts +15 -0
- package/dist/parts/RelationshipsPart.mjs +66 -1
- package/dist/parts/XmlPart.d.mts +63 -32
- package/dist/parts/XmlPart.mjs +240 -96
- package/dist/parts/wml/comments.mjs +30 -0
- package/dist/parts/wml/index.d.mts +12 -1
- package/dist/parts/wml/index.mjs +37 -1
- package/dist/xml/dom.d.mts +7 -0
- package/dist/xml/dom.mjs +18 -0
- package/package.json +2 -2
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type * as wml from '@docx4j/generated-objects-ts/modules/org_docx4j_wml';
|
|
2
|
+
import { type Element } from './tree.mjs';
|
|
3
|
+
/** One paragraph of a story: its element, the array holding it, and its text's place in the story's text. */
|
|
4
|
+
export interface StoryParagraph {
|
|
5
|
+
element: Element<wml.P>;
|
|
6
|
+
container: Element[];
|
|
7
|
+
/** Where its text starts and ends in `StoryLayout.text`. */
|
|
8
|
+
start: number;
|
|
9
|
+
end: number;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* A story's text in Word's default view (CR-012 phase 4; checks 30, 31, 48 and 51): each paragraph's text
|
|
13
|
+
* followed by its mark, `\r`, but a mark tracked as deleted (CR-012 section 19, check 58); in a table a row's cells a `\t` apart, each cell's paragraphs a mark apart
|
|
14
|
+
* (its last mark not written), and the row ended by `\r\n`. Unlike `Body.text` the story's last mark is
|
|
15
|
+
* kept, so that every paragraph's text is followed by what Word puts after it.
|
|
16
|
+
*/
|
|
17
|
+
export interface StoryLayout {
|
|
18
|
+
text: string;
|
|
19
|
+
/** The paragraphs a reader takes, in document order (tables and controls descended into). */
|
|
20
|
+
paragraphs: StoryParagraph[];
|
|
21
|
+
/** The index in `paragraphs` of a `w:p`, or -1 where the walk didn't meet it. */
|
|
22
|
+
indexOf(p: wml.P): number;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Whether a paragraph's mark is written in Word's default view: not where it is tracked as deleted, which reads as
|
|
26
|
+
* nothing there, the paragraphs staying apart (check 58 cases 6 to 8, CR-012 section 19), nor where it is tracked as
|
|
27
|
+
* moved from, as the paragraph's runs are (CR-002 section 64). A cell's last paragraph is ended by the cell's `\t`
|
|
28
|
+
* whatever its mark (cases 9 and 10).
|
|
29
|
+
*/
|
|
30
|
+
export declare const shownInDefaultView: (p: wml.P) => boolean;
|
|
31
|
+
/** Whether a paragraph's mark is written in the original view, the marks Reject All keeps: not one tracked as inserted, nor as moved to. */
|
|
32
|
+
export declare const shownInOriginalView: (p: wml.P) => boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Lays out the blocks `items`, each paragraph's text read by `textOf` (its `Paragraph.text`, or another view's), its
|
|
35
|
+
* mark written where `markShown` says (Word's default view's rule unless another view's is given).
|
|
36
|
+
*/
|
|
37
|
+
export declare function storyLayout(items: Element[], textOf: (element: Element<wml.P>, container: Element[]) => string, markShown?: (p: wml.P) => boolean): StoryLayout;
|
|
38
|
+
/**
|
|
39
|
+
* The story a paragraph stands in: the outermost array of block content above it, and the object holding
|
|
40
|
+
* that array. A text box's paragraphs are a story of their own (the array in its `w:txbxContent`), since a
|
|
41
|
+
* range never crosses a text box's edge; so is a branch of a kept `mc:AlternateContent` that Word doesn't
|
|
42
|
+
* read (CR-013's twin stories), where the selected branch is part of the story around it.
|
|
43
|
+
*/
|
|
44
|
+
export declare function storyOf(p: wml.P): {
|
|
45
|
+
items: Element[];
|
|
46
|
+
owner: object;
|
|
47
|
+
};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { Docx4JException } from '../../opc/exceptions.mjs';
|
|
2
|
+
import { typeNameOf, childrenOf, rowsOf, cellsOf, selectedBranchOf, BRANCH_TYPES } from './tree.mjs';
|
|
3
|
+
/**
|
|
4
|
+
* Whether a paragraph's mark is written in Word's default view: not where it is tracked as deleted, which reads as
|
|
5
|
+
* nothing there, the paragraphs staying apart (check 58 cases 6 to 8, CR-012 section 19), nor where it is tracked as
|
|
6
|
+
* moved from, as the paragraph's runs are (CR-002 section 64). A cell's last paragraph is ended by the cell's `\t`
|
|
7
|
+
* whatever its mark (cases 9 and 10).
|
|
8
|
+
*/
|
|
9
|
+
export const shownInDefaultView = (p) => { var _a, _b, _c, _d; return ((_b = (_a = p.pPr) === null || _a === void 0 ? void 0 : _a.rPr) === null || _b === void 0 ? void 0 : _b.del) === undefined && ((_d = (_c = p.pPr) === null || _c === void 0 ? void 0 : _c.rPr) === null || _d === void 0 ? void 0 : _d.moveFrom) === undefined; };
|
|
10
|
+
/** Whether a paragraph's mark is written in the original view, the marks Reject All keeps: not one tracked as inserted, nor as moved to. */
|
|
11
|
+
export const shownInOriginalView = (p) => { var _a, _b, _c, _d; return ((_b = (_a = p.pPr) === null || _a === void 0 ? void 0 : _a.rPr) === null || _b === void 0 ? void 0 : _b.ins) === undefined && ((_d = (_c = p.pPr) === null || _c === void 0 ? void 0 : _c.rPr) === null || _d === void 0 ? void 0 : _d.moveTo) === undefined; };
|
|
12
|
+
/**
|
|
13
|
+
* Lays out the blocks `items`, each paragraph's text read by `textOf` (its `Paragraph.text`, or another view's), its
|
|
14
|
+
* mark written where `markShown` says (Word's default view's rule unless another view's is given).
|
|
15
|
+
*/
|
|
16
|
+
export function storyLayout(items, textOf, markShown = shownInDefaultView) {
|
|
17
|
+
const pieces = [];
|
|
18
|
+
let length = 0;
|
|
19
|
+
const push = (piece) => { pieces.push(piece); length += piece.length; };
|
|
20
|
+
const pop = () => { length -= pieces.pop().length; };
|
|
21
|
+
const paragraphs = [];
|
|
22
|
+
// Word's default view: the cells a tab apart, the row ended by \r\n (checks 30, 31 and 51; check 48's \t after
|
|
23
|
+
// every cell and row was All Markup's, with hidden text shown)
|
|
24
|
+
const visitRow = (row) => {
|
|
25
|
+
cellsOf(row, 'read').forEach((cell, i) => {
|
|
26
|
+
var _a;
|
|
27
|
+
if (i > 0)
|
|
28
|
+
push('\t');
|
|
29
|
+
visit((_a = childrenOf(cell.element.value)) !== null && _a !== void 0 ? _a : []);
|
|
30
|
+
if (pieces[pieces.length - 1] === '\r')
|
|
31
|
+
pop();
|
|
32
|
+
});
|
|
33
|
+
push('\r\n');
|
|
34
|
+
};
|
|
35
|
+
const visit = (blocks) => {
|
|
36
|
+
for (const el of blocks) {
|
|
37
|
+
const tn = typeNameOf(el);
|
|
38
|
+
if (tn === 'org_docx4j_wml.P') {
|
|
39
|
+
const text = textOf(el, blocks);
|
|
40
|
+
paragraphs.push({ element: el, container: blocks, start: length, end: length + text.length });
|
|
41
|
+
push(text);
|
|
42
|
+
if (markShown(el.value))
|
|
43
|
+
push('\r');
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
const v = el.value;
|
|
47
|
+
if (typeof v !== 'object' || v === null)
|
|
48
|
+
continue;
|
|
49
|
+
if (tn === 'org_docx4j_wml.Tbl') {
|
|
50
|
+
for (const row of rowsOf(v, 'read'))
|
|
51
|
+
visitRow(row.element.value);
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
// a row-level content control's content: its rows (check 53: its text is its cells a \t apart)
|
|
55
|
+
if (tn === 'org_docx4j_wml.Tr') {
|
|
56
|
+
visitRow(v);
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
const children = childrenOf(v, 'read');
|
|
60
|
+
if (children)
|
|
61
|
+
visit(children);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
visit(items);
|
|
65
|
+
const index = new Map(paragraphs.map((p, i) => [p.element.value, i]));
|
|
66
|
+
return { text: pieces.join(''), paragraphs, indexOf: (p) => { var _a; return (_a = index.get(p)) !== null && _a !== void 0 ? _a : -1; } };
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* The story a paragraph stands in: the outermost array of block content above it, and the object holding
|
|
70
|
+
* that array. A text box's paragraphs are a story of their own (the array in its `w:txbxContent`), since a
|
|
71
|
+
* range never crosses a text box's edge; so is a branch of a kept `mc:AlternateContent` that Word doesn't
|
|
72
|
+
* read (CR-013's twin stories), where the selected branch is part of the story around it.
|
|
73
|
+
*/
|
|
74
|
+
export function storyOf(p) {
|
|
75
|
+
let story;
|
|
76
|
+
for (let o = p.PARENT; o !== undefined; o = o.PARENT) {
|
|
77
|
+
const tn = o.TYPE_NAME;
|
|
78
|
+
// a paragraph above this one: p is in a text box, whose content is its story
|
|
79
|
+
if (tn === 'org_docx4j_wml.P')
|
|
80
|
+
break;
|
|
81
|
+
if (tn !== undefined && BRANCH_TYPES.has(tn)) {
|
|
82
|
+
const ac = o.PARENT;
|
|
83
|
+
const items = o.any;
|
|
84
|
+
if (ac && items && selectedBranchOf(ac) !== items)
|
|
85
|
+
return { items, owner: o };
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const content = o.content;
|
|
89
|
+
if (Array.isArray(content))
|
|
90
|
+
story = { items: content, owner: o };
|
|
91
|
+
}
|
|
92
|
+
if (!story)
|
|
93
|
+
throw new Docx4JException('This paragraph is in no story: it is not in a body, a cell, a note or a comment');
|
|
94
|
+
return story;
|
|
95
|
+
}
|
|
@@ -10,7 +10,7 @@ export declare function layoutModeOf(tbl: wml.Tbl): TableLayoutMode;
|
|
|
10
10
|
export declare function textWidthTwips(container: {
|
|
11
11
|
sectPr?: wml.SectPr;
|
|
12
12
|
} | undefined): number;
|
|
13
|
-
/** The grid's column widths in twips; a table without a `w:tblGrid` gets one from its first row (equal columns over the text width when the cells state none). */
|
|
13
|
+
/** The grid's column widths in twips; a table without a `w:tblGrid` gets one from its first row as Word reads it (equal columns over the text width when the cells state none). */
|
|
14
14
|
export declare function gridWidthsOf(tbl: wml.Tbl, textWidth: number): number[];
|
|
15
15
|
/** The form a cell's width takes: its own `w:tcW` type, else the table's `w:tblW` type, else twips (section 42.2 item 4). */
|
|
16
16
|
export declare function widthTypeOf(tc: wml.Tc | undefined, tbl: wml.Tbl): string;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { deepCopy } from '@docx4j/generated-objects-ts';
|
|
2
2
|
import { tc as cellElementOf } from '@docx4j/generated-objects-ts/builders/wml';
|
|
3
3
|
import { Docx4JException } from '../../opc/exceptions.mjs';
|
|
4
|
-
import { rowsOf, cellsOf, childrenOf, linkParents, paragraphOf } from './tree.mjs';
|
|
4
|
+
import { rowsOf, cellsOf, cellLayoutsOf, childrenOf, linkParents, paragraphOf } from './tree.mjs';
|
|
5
5
|
import { wrapInsertedRuns } from './tracking.mjs';
|
|
6
6
|
/** The default text width in twips (A4 with 2.54 cm margins), as the objects package's `tbl` builder uses. */
|
|
7
7
|
export const DEFAULT_TEXT_WIDTH = 9026;
|
|
@@ -42,14 +42,14 @@ function setSpan(tc, count) {
|
|
|
42
42
|
else
|
|
43
43
|
delete tcPr.gridSpan;
|
|
44
44
|
}
|
|
45
|
-
/** The grid's column widths in twips; a table without a `w:tblGrid` gets one from its first row (equal columns over the text width when the cells state none). */
|
|
45
|
+
/** The grid's column widths in twips; a table without a `w:tblGrid` gets one from its first row as Word reads it (equal columns over the text width when the cells state none). */
|
|
46
46
|
export function gridWidthsOf(tbl, textWidth) {
|
|
47
47
|
var _a, _b, _c, _d;
|
|
48
48
|
const grid = (_a = tbl.tblGrid) === null || _a === void 0 ? void 0 : _a.gridCol;
|
|
49
49
|
if (grid && grid.length > 0)
|
|
50
50
|
return grid.map((col) => { var _a; return Number((_a = col.w) !== null && _a !== void 0 ? _a : 0); });
|
|
51
|
-
const first = rowsOf(tbl)[0];
|
|
52
|
-
const cells = first ? cellsOf(first.element.value) : [];
|
|
51
|
+
const first = rowsOf(tbl, 'read')[0];
|
|
52
|
+
const cells = first ? cellsOf(first.element.value, 'read') : [];
|
|
53
53
|
const columns = Math.max(1, cells.reduce((n, cell) => n + spanOf(cell.element.value), 0));
|
|
54
54
|
const each = Math.floor(textWidth / columns);
|
|
55
55
|
const widths = [];
|
|
@@ -158,6 +158,33 @@ function paragraphPropertiesFrom(template) {
|
|
|
158
158
|
delete rPr[key];
|
|
159
159
|
return Object.keys(out).length > 0 ? out : undefined;
|
|
160
160
|
}
|
|
161
|
+
/**
|
|
162
|
+
* The rows the column passes walk: every branch's of a kept row-level `mc:AlternateContent`, since the
|
|
163
|
+
* grid is every branch's; a row's cells along each of its layouts (`cellLayoutsOf`), so a cell-level
|
|
164
|
+
* element's every branch gains and loses the columns its selected branch does (CR-013 phase 3).
|
|
165
|
+
*/
|
|
166
|
+
function everyRow(tbl) {
|
|
167
|
+
return rowsOf(tbl, 'every').map((row) => row.element.value);
|
|
168
|
+
}
|
|
169
|
+
/** Each cell of a row once, with the grid columns it covers, along the row's layouts. */
|
|
170
|
+
function cellExtentsOf(tr) {
|
|
171
|
+
var _a, _b;
|
|
172
|
+
const out = [];
|
|
173
|
+
const seen = new Set();
|
|
174
|
+
for (const layout of cellLayoutsOf(tr)) {
|
|
175
|
+
let at = Number((_b = (_a = rowMember(tr, 'gridBefore')) === null || _a === void 0 ? void 0 : _a.value.val) !== null && _b !== void 0 ? _b : 0);
|
|
176
|
+
for (const cell of layout) {
|
|
177
|
+
const tc = cell.element.value;
|
|
178
|
+
const end = at + spanOf(tc);
|
|
179
|
+
if (!seen.has(tc)) {
|
|
180
|
+
seen.add(tc);
|
|
181
|
+
out.push({ cell, start: at, end });
|
|
182
|
+
}
|
|
183
|
+
at = end;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return out;
|
|
187
|
+
}
|
|
161
188
|
/** The new cells of one row, each a `w:tc` of one paragraph with the template's shading and paragraph properties. */
|
|
162
189
|
function newCells(count, values, template) {
|
|
163
190
|
var _a, _b;
|
|
@@ -204,49 +231,73 @@ export function insertGridColumns(tbl, textWidth, gridIndex, count, values, trac
|
|
|
204
231
|
const copied = old[gridIndex < old.length ? gridIndex : old.length - 1];
|
|
205
232
|
const unscaled = [...old.slice(0, gridIndex), ...Array.from({ length: count }, () => copied), ...old.slice(gridIndex)];
|
|
206
233
|
const widths = mode === 'window' ? rescaled(unscaled, old.reduce((a, b) => a + b, 0)) : unscaled;
|
|
207
|
-
|
|
234
|
+
// the rows a reader sees, which `values` and the result are by; then every other branch's (CR-013 phase 3)
|
|
235
|
+
const read = rowsOf(tbl, 'read').map((row) => row.element.value);
|
|
236
|
+
const rows = [...read, ...everyRow(tbl).filter((tr) => !read.includes(tr))];
|
|
237
|
+
// a row's plan per layout: a cell-level element's branches each get the new cells where the boundary falls in them
|
|
208
238
|
const plans = rows.map((tr) => planRow(tr, gridIndex));
|
|
209
239
|
// an AutoFit-to-contents add changes no width, and Office JS records nothing for it (check 41c)
|
|
210
240
|
const record = tracker && mode !== 'contents' ? recordTable(tbl, rows, tracker) : undefined;
|
|
211
241
|
const result = { cells: [] };
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
const
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
if (tracker)
|
|
242
|
+
const added = [];
|
|
243
|
+
plans.forEach((rowPlans, rowIndex) => {
|
|
244
|
+
const shown = [];
|
|
245
|
+
rowPlans.forEach((plan, layout) => {
|
|
246
|
+
var _a, _b;
|
|
247
|
+
const tr = plan.tr;
|
|
248
|
+
if (plan.skipped) {
|
|
249
|
+
plan.skipped.value.val = Number((_a = plan.skipped.value.val) !== null && _a !== void 0 ? _a : 0) + count;
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
const cells = newCells(count, rowIndex < read.length ? values === null || values === void 0 ? void 0 : values[rowIndex] : undefined, plan.template);
|
|
253
|
+
plan.container.splice(plan.at, 0, ...cells);
|
|
254
|
+
const owner = ownerOfCells(tr, plan.container);
|
|
226
255
|
for (const cell of cells)
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
256
|
+
linkParents(cell, owner);
|
|
257
|
+
if (tracker)
|
|
258
|
+
for (const cell of cells)
|
|
259
|
+
for (const block of (_b = childrenOf(cell.value)) !== null && _b !== void 0 ? _b : [])
|
|
260
|
+
wrapInsertedRuns(tracker, childrenOf(block.value), block.value);
|
|
261
|
+
added.push(...cells);
|
|
262
|
+
if (layout === 0)
|
|
263
|
+
shown.push(...cells);
|
|
264
|
+
});
|
|
265
|
+
if (rowIndex < read.length)
|
|
266
|
+
result.cells.push(shown);
|
|
230
267
|
});
|
|
231
268
|
// the grid, the table's width, and the cells' widths in their forms
|
|
232
269
|
writeGrid(tbl, widths);
|
|
233
270
|
const tblW = (_a = tbl.tblPr) === null || _a === void 0 ? void 0 : _a.tblW;
|
|
234
271
|
if (mode === 'fixed' && (tblW === null || tblW === void 0 ? void 0 : tblW.type) === 'dxa')
|
|
235
272
|
tblW.w = Number((_b = tblW.w) !== null && _b !== void 0 ? _b : 0) + copied * count;
|
|
236
|
-
writeCellWidths(tbl, widths, mode === 'window' ? 'all' :
|
|
273
|
+
writeCellWidths(tbl, widths, mode === 'window' ? 'all' : added.map((c) => c.value));
|
|
237
274
|
if (record)
|
|
238
|
-
record(
|
|
275
|
+
record(added.map((c) => c.value), { unscaled, scaled: widths });
|
|
239
276
|
return result;
|
|
240
277
|
}
|
|
241
|
-
/**
|
|
278
|
+
/**
|
|
279
|
+
* Where the boundary falls in a row, along each of its layouts (`cellLayoutsOf`): at or inside a cell
|
|
280
|
+
* (the new cells go before it), in its skipped columns, or after its last cell. A place two layouts
|
|
281
|
+
* share (outside a cell-level element) is planned once, so each list gains the new cells once.
|
|
282
|
+
*/
|
|
242
283
|
function planRow(tr, gridIndex) {
|
|
243
|
-
var _a
|
|
284
|
+
var _a;
|
|
244
285
|
const before = rowMember(tr, 'gridBefore');
|
|
245
286
|
const after = rowMember(tr, 'gridAfter');
|
|
246
287
|
const skippedBefore = Number((_a = before === null || before === void 0 ? void 0 : before.value.val) !== null && _a !== void 0 ? _a : 0);
|
|
247
288
|
if (before && gridIndex < skippedBefore)
|
|
248
|
-
return { tr, skipped: before };
|
|
249
|
-
const
|
|
289
|
+
return [{ tr, skipped: before }];
|
|
290
|
+
const plans = [];
|
|
291
|
+
const planned = (plan) => plans.some((p) => (p.skipped !== undefined && p.skipped === plan.skipped) || (p.container !== undefined && p.container === plan.container && p.at === plan.at));
|
|
292
|
+
for (const cells of cellLayoutsOf(tr)) {
|
|
293
|
+
const plan = planLayout(tr, cells, gridIndex, skippedBefore, after);
|
|
294
|
+
if (!planned(plan))
|
|
295
|
+
plans.push(plan);
|
|
296
|
+
}
|
|
297
|
+
return plans;
|
|
298
|
+
}
|
|
299
|
+
function planLayout(tr, cells, gridIndex, skippedBefore, after) {
|
|
300
|
+
var _a, _b;
|
|
250
301
|
let at = skippedBefore;
|
|
251
302
|
for (const cell of cells) {
|
|
252
303
|
const tc = cell.element.value;
|
|
@@ -256,26 +307,21 @@ function planRow(tr, gridIndex) {
|
|
|
256
307
|
return { tr, container: cell.container, at: cell.container.indexOf(cell.element), template: tc };
|
|
257
308
|
at = end;
|
|
258
309
|
}
|
|
259
|
-
const skippedAfter = Number((
|
|
310
|
+
const skippedAfter = Number((_a = after === null || after === void 0 ? void 0 : after.value.val) !== null && _a !== void 0 ? _a : 0);
|
|
260
311
|
if (after && gridIndex < at + skippedAfter)
|
|
261
312
|
return { tr, skipped: after };
|
|
262
313
|
const last = cells[cells.length - 1];
|
|
263
314
|
if (!last)
|
|
264
|
-
return { tr, container: ((
|
|
315
|
+
return { tr, container: ((_b = tr.content) !== null && _b !== void 0 ? _b : (tr.content = [])), at: tr.content.length, template: undefined };
|
|
265
316
|
return { tr, container: last.container, at: last.container.indexOf(last.element) + 1, template: last.element.value };
|
|
266
317
|
}
|
|
267
|
-
/** Every cell's `w:tcW` from the grid (`all`), or only the given cells', each in the form it takes. */
|
|
318
|
+
/** Every cell's `w:tcW` from the grid (`all`), or only the given cells', each in the form it takes, in every branch. */
|
|
268
319
|
function writeCellWidths(tbl, widths, which) {
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
const tr = row.element.value;
|
|
272
|
-
let at = Number((_b = (_a = rowMember(tr, 'gridBefore')) === null || _a === void 0 ? void 0 : _a.value.val) !== null && _b !== void 0 ? _b : 0);
|
|
273
|
-
for (const cell of cellsOf(tr)) {
|
|
320
|
+
for (const tr of everyRow(tbl)) {
|
|
321
|
+
for (const { cell, start, end } of cellExtentsOf(tr)) {
|
|
274
322
|
const tc = cell.element.value;
|
|
275
|
-
const end = at + spanOf(tc);
|
|
276
323
|
if (which === 'all' || which.includes(tc))
|
|
277
|
-
writeCellWidth(tc, tbl, widths,
|
|
278
|
-
at = end;
|
|
324
|
+
writeCellWidth(tc, tbl, widths, start, end);
|
|
279
325
|
}
|
|
280
326
|
}
|
|
281
327
|
}
|
|
@@ -299,7 +345,7 @@ function recordTable(tbl, rows, tracker) {
|
|
|
299
345
|
const recordGrid = !((_a = tbl.tblGrid) === null || _a === void 0 ? void 0 : _a.tblGridChange);
|
|
300
346
|
const oldCells = [];
|
|
301
347
|
for (const tr of rows)
|
|
302
|
-
for (const cell of cellsOf(tr)) {
|
|
348
|
+
for (const cell of cellsOf(tr, 'every')) {
|
|
303
349
|
const tc = cell.element.value;
|
|
304
350
|
if (!((_b = tc.tcPr) === null || _b === void 0 ? void 0 : _b.tcPrChange))
|
|
305
351
|
oldCells.push([tc, recordedCellProperties(tc)]);
|
|
@@ -361,18 +407,10 @@ function mergedEdges(unscaled, scaled) {
|
|
|
361
407
|
}
|
|
362
408
|
/** Each cell's column extent in the grid as it is now, through the rows' spans and skipped columns. */
|
|
363
409
|
function cellExtents(tbl) {
|
|
364
|
-
var _a, _b;
|
|
365
410
|
const out = new Map();
|
|
366
|
-
for (const
|
|
367
|
-
const
|
|
368
|
-
|
|
369
|
-
for (const cell of cellsOf(tr)) {
|
|
370
|
-
const tc = cell.element.value;
|
|
371
|
-
const end = at + spanOf(tc);
|
|
372
|
-
out.set(tc, { start: at, end });
|
|
373
|
-
at = end;
|
|
374
|
-
}
|
|
375
|
-
}
|
|
411
|
+
for (const tr of everyRow(tbl))
|
|
412
|
+
for (const { cell, start, end } of cellExtentsOf(tr))
|
|
413
|
+
out.set(cell.element.value, { start, end });
|
|
376
414
|
return out;
|
|
377
415
|
}
|
|
378
416
|
/** A cell's properties as a record holds them: everything but the markers and an earlier record. */
|
|
@@ -381,7 +419,7 @@ function recordedCellProperties(tc) {
|
|
|
381
419
|
}
|
|
382
420
|
/** Whether a cell is still in the table (a deleted column's cells are not). */
|
|
383
421
|
function isInTable(tc, tbl) {
|
|
384
|
-
return
|
|
422
|
+
return everyRow(tbl).some((tr) => cellsOf(tr, 'every').some((cell) => cell.element.value === tc));
|
|
385
423
|
}
|
|
386
424
|
function without(value, keys) {
|
|
387
425
|
const out = {};
|
|
@@ -407,22 +445,23 @@ export function deleteGridColumns(tbl, textWidth, columnIndex, count, tracker) {
|
|
|
407
445
|
if (columnIndex < 0 || columnIndex + count > old.length)
|
|
408
446
|
throw new Docx4JException(`No columns ${columnIndex} to ${columnIndex + count - 1} in a grid of ${old.length} columns`);
|
|
409
447
|
const mode = layoutModeOf(tbl);
|
|
410
|
-
const record = tracker && mode !== 'contents' ? recordTable(tbl,
|
|
448
|
+
const record = tracker && mode !== 'contents' ? recordTable(tbl, everyRow(tbl), tracker) : undefined;
|
|
411
449
|
const from = columnIndex;
|
|
412
450
|
const to = columnIndex + count;
|
|
413
451
|
const overlap = (start, end) => Math.max(0, Math.min(end, to) - Math.max(start, from));
|
|
414
|
-
|
|
415
|
-
|
|
452
|
+
// every branch's rows, and every branch's cells at their grid columns (CR-013 phase 3)
|
|
453
|
+
for (const tr of everyRow(tbl)) {
|
|
454
|
+
const extents = cellExtentsOf(tr);
|
|
416
455
|
const before = rowMember(tr, 'gridBefore');
|
|
417
456
|
const skippedBefore = Number((_a = before === null || before === void 0 ? void 0 : before.value.val) !== null && _a !== void 0 ? _a : 0);
|
|
457
|
+
// the row's last cell edge, before anything shrinks: every layout ends there
|
|
458
|
+
const lastEdge = extents.reduce((edge, extent) => Math.max(edge, extent.end), skippedBefore);
|
|
418
459
|
if (before)
|
|
419
460
|
before.value.val = skippedBefore - overlap(0, skippedBefore);
|
|
420
|
-
|
|
421
|
-
for (const cell of cellsOf(tr)) {
|
|
461
|
+
for (const { cell, start, end } of extents) {
|
|
422
462
|
const tc = cell.element.value;
|
|
423
|
-
const span =
|
|
424
|
-
const
|
|
425
|
-
const covered = overlap(at, end);
|
|
463
|
+
const span = end - start;
|
|
464
|
+
const covered = overlap(start, end);
|
|
426
465
|
if (covered === span) {
|
|
427
466
|
const i = cell.container.indexOf(cell.element);
|
|
428
467
|
if (i >= 0)
|
|
@@ -431,12 +470,11 @@ export function deleteGridColumns(tbl, textWidth, columnIndex, count, tracker) {
|
|
|
431
470
|
else if (covered > 0) {
|
|
432
471
|
setSpan(tc, span - covered);
|
|
433
472
|
}
|
|
434
|
-
at = end;
|
|
435
473
|
}
|
|
436
474
|
const after = rowMember(tr, 'gridAfter');
|
|
437
475
|
if (after) {
|
|
438
476
|
const skippedAfter = Number((_b = after.value.val) !== null && _b !== void 0 ? _b : 0);
|
|
439
|
-
after.value.val = skippedAfter - overlap(
|
|
477
|
+
after.value.val = skippedAfter - overlap(lastEdge, lastEdge + skippedAfter);
|
|
440
478
|
}
|
|
441
479
|
}
|
|
442
480
|
const removed = old.slice(from, to).reduce((a, b) => a + b, 0);
|
|
@@ -453,32 +491,22 @@ export function deleteGridColumns(tbl, textWidth, columnIndex, count, tracker) {
|
|
|
453
491
|
}
|
|
454
492
|
/** The cells whose span no longer matches their stated twips width: those that shrank. */
|
|
455
493
|
function shrunkCells(tbl) {
|
|
456
|
-
var _a, _b, _c, _d, _e
|
|
494
|
+
var _a, _b, _c, _d, _e;
|
|
457
495
|
const out = [];
|
|
458
496
|
const widths = (_c = (_b = (_a = tbl.tblGrid) === null || _a === void 0 ? void 0 : _a.gridCol) === null || _b === void 0 ? void 0 : _b.map((col) => { var _a; return Number((_a = col.w) !== null && _a !== void 0 ? _a : 0); })) !== null && _c !== void 0 ? _c : [];
|
|
459
|
-
for (const
|
|
460
|
-
const tr
|
|
461
|
-
let at = Number((_e = (_d = rowMember(tr, 'gridBefore')) === null || _d === void 0 ? void 0 : _d.value.val) !== null && _e !== void 0 ? _e : 0);
|
|
462
|
-
for (const cell of cellsOf(tr)) {
|
|
497
|
+
for (const tr of everyRow(tbl)) {
|
|
498
|
+
for (const { cell, start, end } of cellExtentsOf(tr)) {
|
|
463
499
|
const tc = cell.element.value;
|
|
464
|
-
const
|
|
465
|
-
|
|
466
|
-
if ((tcW === null || tcW === void 0 ? void 0 : tcW.type) === 'dxa' && Number((_g = tcW.w) !== null && _g !== void 0 ? _g : 0) !== widths.slice(at, end).reduce((a, b) => a + b, 0))
|
|
500
|
+
const tcW = (_d = tc.tcPr) === null || _d === void 0 ? void 0 : _d.tcW;
|
|
501
|
+
if ((tcW === null || tcW === void 0 ? void 0 : tcW.type) === 'dxa' && Number((_e = tcW.w) !== null && _e !== void 0 ? _e : 0) !== widths.slice(start, end).reduce((a, b) => a + b, 0))
|
|
467
502
|
out.push(tc);
|
|
468
|
-
at = end;
|
|
469
503
|
}
|
|
470
504
|
}
|
|
471
505
|
return out;
|
|
472
506
|
}
|
|
473
507
|
/** The grid columns a cell covers: its start and the one past its end. */
|
|
474
508
|
export function gridRangeOf(tr, tc) {
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
const end = at + spanOf(cell.element.value);
|
|
479
|
-
if (cell.element.value === tc)
|
|
480
|
-
return { start: at, end };
|
|
481
|
-
at = end;
|
|
482
|
-
}
|
|
483
|
-
return undefined;
|
|
509
|
+
// along the row's layouts, so a cell after a cell-level mc:AlternateContent is placed past its columns
|
|
510
|
+
const extent = cellExtentsOf(tr).find(({ cell }) => cell.element.value === tc);
|
|
511
|
+
return extent ? { start: extent.start, end: extent.end } : undefined;
|
|
484
512
|
}
|
|
@@ -9,15 +9,20 @@ export declare function tableRecordsOf(tbl: wml.Tbl): TableRecords | undefined;
|
|
|
9
9
|
export declare function hasTableRecords(tbl: wml.Tbl): boolean;
|
|
10
10
|
/** A row's text as Office JS reports it: its cells with a tab between them, then `\r\n` (checks 27 and 28). */
|
|
11
11
|
export declare function rowText(tr: wml.Tr): string;
|
|
12
|
-
/**
|
|
12
|
+
/**
|
|
13
|
+
* The text of a table's change: the table's where its own properties or grid changed, else the changed
|
|
14
|
+
* rows', read in the selected branch, what Word shows: so a change whose records are all in other
|
|
15
|
+
* branches has no text (docx4j's `Tables.changeText`, its `aTableChangesTextReadsTheBranchWordReads`).
|
|
16
|
+
*/
|
|
13
17
|
export declare function tableChangeText(tbl: wml.Tbl): string;
|
|
14
|
-
/** Accepting keeps the properties as they are and drops every record; a row's `w:trPr` left empty goes. */
|
|
18
|
+
/** Accepting keeps the properties as they are and drops every record, in every branch; a row's `w:trPr` left empty goes. */
|
|
15
19
|
export declare function acceptTable(tbl: wml.Tbl): void;
|
|
16
20
|
/**
|
|
17
21
|
* Rejecting puts the recorded table back: its properties, its grid, and each row and cell in the
|
|
18
22
|
* change as recorded - a row in the change without a `w:trPrChange` cleared, having had no properties
|
|
19
23
|
* (check 28, from a saved file) - then collapses a merged grid (check 27: a width change records the
|
|
20
24
|
* old cells over a grid of the old and new column edges together). Rows outside the change are left.
|
|
25
|
+
* Every branch of a kept row- or cell-level `mc:AlternateContent` is restored (CR-013 phase 3).
|
|
21
26
|
*/
|
|
22
27
|
export declare function rejectTable(tbl: wml.Tbl): void;
|
|
23
28
|
/** A row's `w:trPr` with nothing left in it goes, as Word writes none. */
|
|
@@ -25,6 +30,9 @@ export declare function pruneRow(tr: wml.Tr): void;
|
|
|
25
30
|
/**
|
|
26
31
|
* Grid columns that no row keeps apart become one: the edges any row's cells (or its `w:gridBefore`
|
|
27
32
|
* and `w:gridAfter`) fall on are kept, the others merged, and each cell's `w:gridSpan` and each row's
|
|
28
|
-
* skipped columns counted again over the kept columns.
|
|
33
|
+
* skipped columns counted again over the kept columns. The grid is every branch's: every row-level
|
|
34
|
+
* branch's rows are counted, and a row's cells along each of its layouts (`cellLayoutsOf`, one per
|
|
35
|
+
* reading of a cell-level `mc:AlternateContent`), every branch spanning the same columns (CR-013 phase 3,
|
|
36
|
+
* docx4j's `Lister.cellLayoutsOf`; phase 2 counted every branch's cells along one row).
|
|
29
37
|
*/
|
|
30
38
|
export declare function collapseGrid(tbl: wml.Tbl): void;
|