@portone/docx-editor 0.6.2 → 0.6.4
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 +91 -0
- package/dist/DocxEditor.js +2 -2
- package/dist/docx/comments/writing.d.ts +8 -0
- package/dist/docx/comments/writing.js +9 -2
- package/dist/docx/headersFooters.d.ts +7 -0
- package/dist/docx/headersFooters.js +8 -15
- package/dist/docx/identities.d.ts +9 -2
- package/dist/docx/identities.js +45 -12
- package/dist/docx/importDocx.js +2 -0
- package/dist/docx/importParagraph.d.ts +4 -0
- package/dist/docx/importParagraph.js +2 -0
- package/dist/docx/importPolicy.d.ts +2 -2
- package/dist/docx/importPolicy.js +12 -5
- package/dist/docx/importPreserved.d.ts +3 -3
- package/dist/docx/importSdtBlock.d.ts +23 -0
- package/dist/docx/importSdtBlock.js +33 -0
- package/dist/docx/importTable.js +35 -24
- package/dist/docx/invariants.js +60 -3
- package/dist/docx/notes/numbering.js +4 -4
- package/dist/docx/notes/references.d.ts +0 -2
- package/dist/docx/notes/references.js +2 -7
- package/dist/docx/numberingPlanner.d.ts +3 -0
- package/dist/docx/numberingPlanner.js +8 -2
- package/dist/docx/packageParts.d.ts +9 -0
- package/dist/docx/packageParts.js +16 -0
- package/dist/docx/packagePrefixes.d.ts +12 -0
- package/dist/docx/packagePrefixes.js +18 -0
- package/dist/docx/partPlan.js +3 -5
- package/dist/docx/sdt.d.ts +79 -9
- package/dist/docx/sdt.js +121 -26
- package/dist/docx/sections.d.ts +13 -13
- package/dist/docx/sections.js +34 -20
- package/dist/docx/serializeBlock.js +10 -1
- package/dist/docx/serializeParagraph.d.ts +2 -1
- package/dist/docx/serializeParagraph.js +6 -1
- package/dist/docx/serializeSdtBlock.d.ts +16 -0
- package/dist/docx/serializeSdtBlock.js +11 -0
- package/dist/docx/serializeTable.d.ts +8 -2
- package/dist/docx/serializeTable.js +14 -20
- package/dist/docx/story.d.ts +10 -4
- package/dist/docx/story.js +25 -20
- package/dist/docx/storyBlocks.d.ts +32 -0
- package/dist/docx/storyBlocks.js +65 -0
- package/dist/docx/storyParts.d.ts +3 -0
- package/dist/docx/storyParts.js +4 -1
- package/dist/docx/wrappers.d.ts +33 -15
- package/dist/docx/wrappers.js +45 -26
- package/dist/editor/blockControlEdits.d.ts +15 -0
- package/dist/editor/blockControlEdits.js +79 -0
- package/dist/editor/clipboard/normalizers.js +1 -1
- package/dist/editor/commands/lockCommands.d.ts +5 -1
- package/dist/editor/commands/lockCommands.js +51 -23
- package/dist/editor/createEditor.js +4 -0
- package/dist/editor/inlineControlEdits.d.ts +20 -0
- package/dist/editor/inlineControlEdits.js +69 -0
- package/dist/editor/notes/noteSurface.js +4 -3
- package/dist/editor/plugins/controlLifecycle.d.ts +18 -0
- package/dist/editor/plugins/controlLifecycle.js +130 -0
- package/dist/editor/plugins/keymap.js +34 -5
- package/dist/editor/plugins/rowResize.js +2 -4
- package/dist/editor/stories/storyState.d.ts +2 -0
- package/dist/editor/stories/storyState.js +2 -0
- package/dist/ooxml/element.js +3 -2
- package/dist/ooxml/errors.d.ts +11 -0
- package/dist/ooxml/fragment.js +3 -2
- package/dist/ooxml/names.d.ts +4 -0
- package/dist/ooxml/names.js +8 -2
- package/dist/ooxml/partPrefixes.d.ts +21 -0
- package/dist/ooxml/partPrefixes.js +164 -0
- package/dist/ooxml/partSplice.d.ts +6 -1
- package/dist/ooxml/partSplice.js +30 -10
- package/dist/ooxml/tagScan.d.ts +24 -2
- package/dist/ooxml/tagScan.js +26 -7
- package/dist/ooxml/xml.d.ts +8 -0
- package/dist/ooxml/xml.js +27 -5
- package/dist/page/blockKinds.d.ts +24 -2
- package/dist/page/blockKinds.js +6 -1
- package/dist/page/kinds/index.d.ts +5 -0
- package/dist/page/kinds/index.js +2 -0
- package/dist/page/kinds/sdtBlockKind.d.ts +14 -0
- package/dist/page/kinds/sdtBlockKind.js +143 -0
- package/dist/page/measureBlocks.js +4 -2
- package/dist/page/pageDecorations.js +3 -2
- package/dist/page/pageLayout.d.ts +16 -15
- package/dist/page/pageLayout.js +134 -67
- package/dist/page/usePageLayout.d.ts +2 -2
- package/dist/page/usePageLayout.js +5 -5
- package/dist/schema/attrRoles.js +56 -1
- package/dist/schema/clipboard.js +11 -0
- package/dist/schema/controlAttrs.d.ts +119 -0
- package/dist/schema/controlAttrs.js +137 -0
- package/dist/schema/controlEdges.d.ts +22 -0
- package/dist/schema/controlEdges.js +50 -0
- package/dist/schema/docxSchema.d.ts +1 -1
- package/dist/schema/docxSchema.js +235 -37
- package/dist/schema/editGuard.d.ts +1 -1
- package/dist/schema/guards.js +2 -0
- package/dist/schema/locks.d.ts +56 -8
- package/dist/schema/locks.js +115 -56
- package/dist/styles/classNames.d.ts +10 -0
- package/dist/styles/classNames.js +10 -0
- package/dist/styles.css +44 -2
- package/dist/table/merge.js +11 -3
- package/dist/table/rowResize.d.ts +9 -0
- package/dist/table/rowResize.js +9 -5
- package/dist/ui/TextMenu.js +2 -0
- package/package.json +3 -3
package/dist/docx/sdt.js
CHANGED
|
@@ -1,28 +1,50 @@
|
|
|
1
1
|
// src/docx/sdt.ts
|
|
2
2
|
import { elementXml, openTagXml } from "../ooxml/element.js";
|
|
3
|
-
import {
|
|
3
|
+
import { DocxExportError } from "../ooxml/errors.js";
|
|
4
|
+
import { NAMESPACES, wName } from "../ooxml/names.js";
|
|
4
5
|
import {
|
|
5
6
|
parseProps,
|
|
6
7
|
propsChild,
|
|
7
8
|
renderElement,
|
|
8
9
|
setChild
|
|
9
10
|
} from "../ooxml/props.js";
|
|
10
|
-
import { wAttr } from "../ooxml/units.js";
|
|
11
|
-
import { attrString, elementChildren, serializeXml } from "../ooxml/xml.js";
|
|
11
|
+
import { isOnElement, wAttr } from "../ooxml/units.js";
|
|
12
|
+
import { attrString, elementChildren, serializeXml, W_NS } from "../ooxml/xml.js";
|
|
12
13
|
var CONTENTS_LOCKED = ["contentLocked", "sdtContentLocked"];
|
|
13
14
|
var DELETION_LOCKED = ["sdtLocked", "sdtContentLocked"];
|
|
14
|
-
function
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
function qualifiedName(el) {
|
|
16
|
+
return `{${el.namespaceURI ?? ""}}${el.localName}`;
|
|
17
|
+
}
|
|
18
|
+
var GROUP = `{${W_NS}}group`;
|
|
19
|
+
var TEMPORARY = `{${W_NS}}temporary`;
|
|
20
|
+
var SHOWING_PLACEHOLDER = `{${W_NS}}showingPlcHdr`;
|
|
21
|
+
function states(children, qualified) {
|
|
22
|
+
return isOnElement(
|
|
23
|
+
children.find((child) => qualifiedName(child) === qualified) ?? null
|
|
17
24
|
);
|
|
18
|
-
|
|
25
|
+
}
|
|
26
|
+
function sdtFacts(sdtPr) {
|
|
27
|
+
const children = elementChildren(sdtPr);
|
|
28
|
+
const lock = children.find((child) => child.localName === "lock");
|
|
29
|
+
const val = lock ? wAttr(lock, "val") : null;
|
|
30
|
+
return {
|
|
31
|
+
contentsLocked: val !== null && CONTENTS_LOCKED.includes(val),
|
|
32
|
+
deletionLocked: val !== null && DELETION_LOCKED.includes(val),
|
|
33
|
+
group: children.some((child) => qualifiedName(child) === GROUP),
|
|
34
|
+
temporary: states(children, TEMPORARY),
|
|
35
|
+
showingPlaceholder: states(children, SHOWING_PLACEHOLDER)
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function isSdtContent(child) {
|
|
39
|
+
return child.localName === "sdtContent";
|
|
40
|
+
}
|
|
41
|
+
function sdtPrefix(el, head) {
|
|
42
|
+
return openTagXml(wName("sdt"), attrString(el)) + head.map(serializeXml).join("");
|
|
19
43
|
}
|
|
20
44
|
function readSdtWrapper(el) {
|
|
21
45
|
if (el.nodeName !== "w:sdt") return null;
|
|
22
46
|
const children = elementChildren(el);
|
|
23
|
-
const contentAt = children.findIndex(
|
|
24
|
-
(child) => child.localName === "sdtContent"
|
|
25
|
-
);
|
|
47
|
+
const contentAt = children.findIndex(isSdtContent);
|
|
26
48
|
const content = contentAt === -1 ? null : children[contentAt];
|
|
27
49
|
if (!content || content.nodeName !== "w:sdtContent") return null;
|
|
28
50
|
if (content.attributes.length > 0) return null;
|
|
@@ -30,15 +52,74 @@ function readSdtWrapper(el) {
|
|
|
30
52
|
const head = children.slice(0, contentAt);
|
|
31
53
|
const sdtPr = head.find((child) => child.localName === "sdtPr");
|
|
32
54
|
if (!sdtPr) return null;
|
|
33
|
-
const attrs = attrString(el);
|
|
34
|
-
const val = lockValue(sdtPr);
|
|
35
55
|
return {
|
|
36
|
-
prefix:
|
|
56
|
+
prefix: sdtPrefix(el, head),
|
|
37
57
|
content,
|
|
38
|
-
|
|
39
|
-
|
|
58
|
+
...sdtFacts(sdtPr)
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function readEmptySdt(el) {
|
|
62
|
+
if (el.nodeName !== "w:sdt") return null;
|
|
63
|
+
const children = elementChildren(el);
|
|
64
|
+
if (children.some(isSdtContent)) return null;
|
|
65
|
+
const sdtPr = children.find((child) => child.localName === "sdtPr");
|
|
66
|
+
if (!sdtPr) return null;
|
|
67
|
+
return {
|
|
68
|
+
prefix: sdtPrefix(el, children),
|
|
69
|
+
...sdtFacts(sdtPr)
|
|
40
70
|
};
|
|
41
71
|
}
|
|
72
|
+
function controlFactsFrom(wrapper) {
|
|
73
|
+
return {
|
|
74
|
+
prefix: wrapper.prefix,
|
|
75
|
+
contentsLocked: wrapper.contentsLocked,
|
|
76
|
+
deletionLocked: wrapper.deletionLocked,
|
|
77
|
+
group: wrapper.group,
|
|
78
|
+
temporary: wrapper.temporary,
|
|
79
|
+
showingPlaceholder: wrapper.showingPlaceholder
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function readSdtContents(el) {
|
|
83
|
+
const wrapper = readSdtWrapper(el);
|
|
84
|
+
if (!wrapper) {
|
|
85
|
+
const facts = readEmptySdt(el);
|
|
86
|
+
return facts === null ? null : { kind: "nothing", facts };
|
|
87
|
+
}
|
|
88
|
+
return elementChildren(wrapper.content).length === 0 ? { kind: "nothing", facts: controlFactsFrom(wrapper) } : { kind: "content", wrapper };
|
|
89
|
+
}
|
|
90
|
+
var RESTRAINED_TYPES = [
|
|
91
|
+
`{${W_NS}}text`,
|
|
92
|
+
`{${W_NS}}picture`,
|
|
93
|
+
`{${W_NS}}date`,
|
|
94
|
+
`{${W_NS}}comboBox`,
|
|
95
|
+
`{${W_NS}}dropDownList`,
|
|
96
|
+
`{${NAMESPACES.w14}}checkbox`
|
|
97
|
+
];
|
|
98
|
+
function modelsBlockContent(el) {
|
|
99
|
+
const sdtPr = elementChildren(el).find(
|
|
100
|
+
(child) => child.localName === "sdtPr"
|
|
101
|
+
);
|
|
102
|
+
if (!sdtPr) return true;
|
|
103
|
+
return !elementChildren(sdtPr).some(
|
|
104
|
+
(child) => RESTRAINED_TYPES.includes(qualifiedName(child))
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
var SDT_CLOSING_XML = "</w:sdtContent></w:sdt>";
|
|
108
|
+
function sdtOpeningXml(prefix) {
|
|
109
|
+
if (typeof prefix !== "string") {
|
|
110
|
+
throw new DocxExportError(
|
|
111
|
+
"lost-original",
|
|
112
|
+
"a content control has lost the opening XML it goes back out as"
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
return `${prefix}<w:sdtContent>`;
|
|
116
|
+
}
|
|
117
|
+
function sdtXml(prefix, content) {
|
|
118
|
+
return sdtOpeningXml(prefix) + content + SDT_CLOSING_XML;
|
|
119
|
+
}
|
|
120
|
+
function serializeEmptyControl(node) {
|
|
121
|
+
return sdtXml(node.attrs.sdtPrefix, "");
|
|
122
|
+
}
|
|
42
123
|
function newControlId() {
|
|
43
124
|
return Math.floor(Math.random() * 2147483647);
|
|
44
125
|
}
|
|
@@ -47,19 +128,25 @@ function renderPrefix(props) {
|
|
|
47
128
|
return openTagXml(props.tag, props.attrs) + inner + (props.tail ?? "");
|
|
48
129
|
}
|
|
49
130
|
var NAMES_NOTHING = ["id", "lock"];
|
|
50
|
-
function
|
|
131
|
+
function prefixProps(prefix) {
|
|
51
132
|
const sdt = parseProps(`${prefix}</w:sdt>`);
|
|
52
|
-
|
|
53
|
-
const sdtPr =
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
133
|
+
const child = sdt && propsChild(sdt.children, "sdtPr");
|
|
134
|
+
const sdtPr = child ? parseProps(child.xml) : null;
|
|
135
|
+
return sdt && sdtPr ? { sdt, sdtPr } : null;
|
|
136
|
+
}
|
|
137
|
+
function namesNothing(prefix) {
|
|
138
|
+
const props = prefixProps(prefix);
|
|
139
|
+
if (!props || props.sdt.attrs !== null || props.sdt.children.length !== 1) {
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
return props.sdtPr.children.every(
|
|
143
|
+
(child) => NAMES_NOTHING.includes(child.name)
|
|
144
|
+
);
|
|
57
145
|
}
|
|
58
146
|
function editSdtPrefix(prefix, edits) {
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
const
|
|
62
|
-
if (!sdt || !props) return null;
|
|
147
|
+
const read = prefixProps(prefix);
|
|
148
|
+
if (!read) return null;
|
|
149
|
+
const { sdt, sdtPr: props } = read;
|
|
63
150
|
const rendered = renderElement(
|
|
64
151
|
edits.reduce((kept, [name, xml]) => setChild(kept, name, xml), props)
|
|
65
152
|
);
|
|
@@ -73,9 +160,17 @@ function copiedControlPrefix(prefix, id) {
|
|
|
73
160
|
return copied ?? prefix;
|
|
74
161
|
}
|
|
75
162
|
export {
|
|
163
|
+
SDT_CLOSING_XML,
|
|
164
|
+
controlFactsFrom,
|
|
76
165
|
copiedControlPrefix,
|
|
77
166
|
editSdtPrefix,
|
|
167
|
+
modelsBlockContent,
|
|
78
168
|
namesNothing,
|
|
79
169
|
newControlId,
|
|
80
|
-
|
|
170
|
+
readEmptySdt,
|
|
171
|
+
readSdtContents,
|
|
172
|
+
readSdtWrapper,
|
|
173
|
+
sdtOpeningXml,
|
|
174
|
+
sdtXml,
|
|
175
|
+
serializeEmptyControl
|
|
81
176
|
};
|
package/dist/docx/sections.d.ts
CHANGED
|
@@ -43,7 +43,10 @@ export interface SectionProperties {
|
|
|
43
43
|
}
|
|
44
44
|
/** What a section the document does not spell out lays down: the paper every document used to be drawn on */
|
|
45
45
|
export declare const DEFAULT_SECTION: SectionProperties;
|
|
46
|
-
/**
|
|
46
|
+
/**
|
|
47
|
+
* Which `w:sectPr` closes a section: the paragraph carrying it, wherever in the document that
|
|
48
|
+
* paragraph stands, or the body itself.
|
|
49
|
+
*/
|
|
47
50
|
export type SectionAnchor = {
|
|
48
51
|
kind: "paragraph";
|
|
49
52
|
pos: number;
|
|
@@ -52,9 +55,9 @@ export type SectionAnchor = {
|
|
|
52
55
|
};
|
|
53
56
|
export interface DocumentSection {
|
|
54
57
|
index: number;
|
|
55
|
-
/**
|
|
56
|
-
|
|
57
|
-
|
|
58
|
+
/** Document positions this section covers, inclusive */
|
|
59
|
+
from: number;
|
|
60
|
+
to: number;
|
|
58
61
|
anchor: SectionAnchor;
|
|
59
62
|
props: SectionProperties;
|
|
60
63
|
}
|
|
@@ -116,16 +119,13 @@ export declare function setSectionChild(sectPrXml: string, name: string, childXm
|
|
|
116
119
|
* Every section of the document in order, the last of them the one the document node carries.
|
|
117
120
|
*
|
|
118
121
|
* A document naming no section at all still has one, drawn on the paper `DEFAULT_SECTION` lays
|
|
119
|
-
* down.
|
|
120
|
-
*
|
|
121
|
-
*/
|
|
122
|
-
export declare function sectionsOf(doc: PMNode): readonly DocumentSection[];
|
|
123
|
-
/**
|
|
124
|
-
* The same, against a table already read.
|
|
122
|
+
* down. Sections are keyed by document position rather than by the index of a top-level block,
|
|
123
|
+
* because the paragraph that ends one may stand inside a block-level content control.
|
|
125
124
|
*
|
|
126
|
-
*
|
|
127
|
-
*
|
|
125
|
+
* A section ends at the last position of the paragraph carrying its break (§17.6.17), wherever
|
|
126
|
+
* that paragraph stands, so a block holding several breaks ends several sections and a section
|
|
127
|
+
* whose break stands on the paragraph after another's covers that one paragraph alone.
|
|
128
128
|
*/
|
|
129
|
-
export declare function
|
|
129
|
+
export declare function sectionsOf(doc: PMNode): readonly DocumentSection[];
|
|
130
130
|
/** The section the block at this position belongs to */
|
|
131
131
|
export declare function sectionAt(doc: PMNode, pos: number): DocumentSection;
|
package/dist/docx/sections.js
CHANGED
|
@@ -20,6 +20,7 @@ import {
|
|
|
20
20
|
A4_PORTRAIT,
|
|
21
21
|
readPageGeometry
|
|
22
22
|
} from "./pageGeometry.js";
|
|
23
|
+
import { eachStoryParagraph } from "./storyBlocks.js";
|
|
23
24
|
var HEADER_FOOTER_VARIANTS = [
|
|
24
25
|
"default",
|
|
25
26
|
"first",
|
|
@@ -162,38 +163,52 @@ function setSectionChild(sectPrXml, name, childXml) {
|
|
|
162
163
|
const written = childXml !== null && REFERENCE_CHILDREN.includes(name) ? withReference(props, name, childXml) : setChild(props, name, childXml);
|
|
163
164
|
return gap + renderElement(written);
|
|
164
165
|
}
|
|
166
|
+
function sectionBreaksOf(block, pos) {
|
|
167
|
+
const found = [];
|
|
168
|
+
eachStoryParagraph(block, pos, (paragraph, paragraphPos) => {
|
|
169
|
+
const brk = sectionBreakOf(pPrText(paragraph));
|
|
170
|
+
if (brk !== null) {
|
|
171
|
+
found.push({
|
|
172
|
+
pos: paragraphPos,
|
|
173
|
+
to: paragraphPos + paragraph.nodeSize - 1,
|
|
174
|
+
xml: brk
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
return found;
|
|
179
|
+
}
|
|
165
180
|
function sectionsOf(doc) {
|
|
166
181
|
const sections = [];
|
|
167
|
-
let
|
|
168
|
-
doc.forEach((child, offset
|
|
169
|
-
const brk
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
182
|
+
let from = 0;
|
|
183
|
+
doc.forEach((child, offset) => {
|
|
184
|
+
for (const brk of sectionBreaksOf(child, offset)) {
|
|
185
|
+
sections.push({
|
|
186
|
+
index: sections.length,
|
|
187
|
+
from,
|
|
188
|
+
to: brk.to,
|
|
189
|
+
anchor: { kind: "paragraph", pos: brk.pos },
|
|
190
|
+
props: parseSectionProperties(brk.xml) ?? {
|
|
191
|
+
...DEFAULT_SECTION,
|
|
192
|
+
xml: brk.xml
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
from = brk.to + 1;
|
|
196
|
+
}
|
|
179
197
|
});
|
|
180
198
|
const body = doc.attrs.sectPr;
|
|
181
199
|
const xml = typeof body === "string" ? body : null;
|
|
182
200
|
sections.push({
|
|
183
201
|
index: sections.length,
|
|
184
|
-
|
|
185
|
-
|
|
202
|
+
from,
|
|
203
|
+
to: doc.content.size,
|
|
186
204
|
anchor: { kind: "body" },
|
|
187
205
|
props: xml === null ? DEFAULT_SECTION : parseSectionProperties(xml) ?? { ...DEFAULT_SECTION, xml }
|
|
188
206
|
});
|
|
189
207
|
return sections;
|
|
190
208
|
}
|
|
191
|
-
function sectionIn(sections, doc, pos) {
|
|
192
|
-
const index = doc.resolve(Math.min(Math.max(pos, 0), doc.content.size)).index(0);
|
|
193
|
-
return sections.find((section) => index <= section.lastBlock) ?? sections[sections.length - 1];
|
|
194
|
-
}
|
|
195
209
|
function sectionAt(doc, pos) {
|
|
196
|
-
|
|
210
|
+
const sections = sectionsOf(doc);
|
|
211
|
+
return sections.find((section) => pos <= section.to) ?? sections[sections.length - 1];
|
|
197
212
|
}
|
|
198
213
|
export {
|
|
199
214
|
DEFAULT_SECTION,
|
|
@@ -203,7 +218,6 @@ export {
|
|
|
203
218
|
readSectionProperties,
|
|
204
219
|
sectionAt,
|
|
205
220
|
sectionBreakOf,
|
|
206
|
-
sectionIn,
|
|
207
221
|
sectionsOf,
|
|
208
222
|
setSectionChild,
|
|
209
223
|
storyReferenceIds,
|
|
@@ -1,14 +1,23 @@
|
|
|
1
1
|
// src/docx/serializeBlock.ts
|
|
2
2
|
import { DocxExportError } from "../ooxml/errors.js";
|
|
3
|
+
import { isBlockControl, isEmptyBlockControl } from "../schema/controlAttrs.js";
|
|
3
4
|
import { sameSource } from "../schema/sourceEquality.js";
|
|
4
5
|
import { NO_EXPORT_REFS } from "./exportRefs.js";
|
|
6
|
+
import { serializeEmptyControl } from "./sdt.js";
|
|
5
7
|
import { serializeParagraph } from "./serializeParagraph.js";
|
|
6
8
|
import { serializePreservedBlock } from "./serializePreserved.js";
|
|
9
|
+
import { serializeSdtBlock } from "./serializeSdtBlock.js";
|
|
7
10
|
import { serializeTable } from "./serializeTable.js";
|
|
8
11
|
import { originalBlock } from "./session.js";
|
|
9
12
|
function serializeBlock(node, refs = NO_EXPORT_REFS) {
|
|
10
13
|
if (node.type.name === "paragraph") return serializeParagraph(node, refs);
|
|
11
|
-
if (node.type.name === "table")
|
|
14
|
+
if (node.type.name === "table") {
|
|
15
|
+
return serializeTable(node, refs, serializeBlock);
|
|
16
|
+
}
|
|
17
|
+
if (isBlockControl(node)) {
|
|
18
|
+
return serializeSdtBlock(node, refs, serializeBlock);
|
|
19
|
+
}
|
|
20
|
+
if (isEmptyBlockControl(node)) return serializeEmptyControl(node);
|
|
12
21
|
if (node.type.isInGroup("preserved")) {
|
|
13
22
|
return serializePreservedBlock(node, refs);
|
|
14
23
|
}
|
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
*
|
|
6
6
|
* A fragment the reader kept whole goes back where it stood: one kept inside a run is written as
|
|
7
7
|
* a piece of that run, and one kept beside the runs is written between them, which is what the
|
|
8
|
-
* two content models admit.
|
|
8
|
+
* two content models admit. A content control holding nothing is written between them for the
|
|
9
|
+
* same reason.
|
|
9
10
|
*
|
|
10
11
|
* The inlines are grouped from the outside in: neighbours sharing the outermost wrapper each stands
|
|
11
12
|
* inside go back into it, that grouping is made again one wrapper deeper, and at the bottom the
|
|
@@ -15,7 +15,7 @@ import { wName } from "../ooxml/names.js";
|
|
|
15
15
|
import { escapeXml } from "../ooxml/xml.js";
|
|
16
16
|
import { wrapperMarks } from "../schema/wrappers.js";
|
|
17
17
|
import { NO_EXPORT_REFS } from "./exportRefs.js";
|
|
18
|
-
import { wrapperKindOf } from "./wrappers.js";
|
|
18
|
+
import { emptyWrapperXml, wrapperKindOf } from "./wrappers.js";
|
|
19
19
|
function sameMark(a, b) {
|
|
20
20
|
if (a === null || b === null) return a === b;
|
|
21
21
|
return a.eq(b);
|
|
@@ -118,6 +118,11 @@ function addInline(parts, child, images) {
|
|
|
118
118
|
parts.push({ kind: "raw", xml: preservedXml(child) });
|
|
119
119
|
return;
|
|
120
120
|
}
|
|
121
|
+
const empty = emptyWrapperXml(child);
|
|
122
|
+
if (empty !== null) {
|
|
123
|
+
parts.push({ kind: "raw", xml: empty });
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
121
126
|
if (child.type.name === "commentStart" || child.type.name === "commentEnd") {
|
|
122
127
|
const original = child.attrs.xml;
|
|
123
128
|
if (typeof original === "string") {
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How a block-level content control goes back out: the opening tag it arrived with, and the blocks
|
|
3
|
+
* it holds inside the `w:sdtContent` this editor writes (`./sdt`).
|
|
4
|
+
*
|
|
5
|
+
* A control nobody edited never reaches here at all - `blockXml` hands back the bytes it arrived
|
|
6
|
+
* as, the way it does for a table (`./serializeBlock`).
|
|
7
|
+
*
|
|
8
|
+
* A control holds the same blocks as the level it stands in, and a control may stand inside a
|
|
9
|
+
* table as a table may stand inside a control, so the caller hands over the writer for its own
|
|
10
|
+
* level rather than this module reaching for one.
|
|
11
|
+
*/
|
|
12
|
+
import type { Node as PMNode } from "prosemirror-model";
|
|
13
|
+
import type { ExportRefs } from "./exportRefs";
|
|
14
|
+
/** How the level around a control writes one block of it */
|
|
15
|
+
export type BlockWriter = (block: PMNode, refs: ExportRefs) => string;
|
|
16
|
+
export declare function serializeSdtBlock(node: PMNode, refs: ExportRefs, writeBlock: BlockWriter): string;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// src/docx/serializeSdtBlock.ts
|
|
2
|
+
import { sdtXml } from "./sdt.js";
|
|
3
|
+
function serializeSdtBlock(node, refs, writeBlock) {
|
|
4
|
+
return sdtXml(
|
|
5
|
+
node.attrs.sdtPrefix,
|
|
6
|
+
node.children.map((block) => writeBlock(block, refs)).join("")
|
|
7
|
+
);
|
|
8
|
+
}
|
|
9
|
+
export {
|
|
10
|
+
serializeSdtBlock
|
|
11
|
+
};
|
|
@@ -8,5 +8,11 @@
|
|
|
8
8
|
* That way, when a horizontal merge shrinks from two cells to one, the `w:gridSpan` goes away with it.
|
|
9
9
|
*/
|
|
10
10
|
import type { Node as PMNode } from "prosemirror-model";
|
|
11
|
-
import {
|
|
12
|
-
|
|
11
|
+
import type { ExportRefs } from "./exportRefs";
|
|
12
|
+
import type { BlockWriter } from "./serializeSdtBlock";
|
|
13
|
+
/**
|
|
14
|
+
* A cell takes exactly the blocks the body takes, so the caller hands over the writer for its own
|
|
15
|
+
* level: a table may stand inside a content control as a control may stand inside a table, and
|
|
16
|
+
* neither writer may import the other.
|
|
17
|
+
*/
|
|
18
|
+
export declare function serializeTable(table: PMNode, refs: ExportRefs, writeBlock: BlockWriter): string;
|
|
@@ -16,9 +16,8 @@ import {
|
|
|
16
16
|
setChild
|
|
17
17
|
} from "../ooxml/props.js";
|
|
18
18
|
import { ST_MeasurementOrPercent, ST_TwipsMeasure } from "../ooxml/simpleTypes.js";
|
|
19
|
-
import {
|
|
20
|
-
import { rawAttrsOf
|
|
21
|
-
import { serializePreservedBlock } from "./serializePreserved.js";
|
|
19
|
+
import { sdtXml } from "./sdt.js";
|
|
20
|
+
import { rawAttrsOf } from "./serializeParagraph.js";
|
|
22
21
|
function propsOf(xml, tag) {
|
|
23
22
|
if (typeof xml !== "string") return { tag, attrs: null, children: [] };
|
|
24
23
|
const parsed = parseProps(xml);
|
|
@@ -94,24 +93,18 @@ function cellPropsXml(cell, role) {
|
|
|
94
93
|
const width = toTableWidth(cell.attrs.tcW);
|
|
95
94
|
return renderProps(withWidth(merged, "tcW", width));
|
|
96
95
|
}
|
|
97
|
-
function cellBlockXml(block, refs) {
|
|
98
|
-
if (block.type.name === "paragraph") return serializeParagraph(block, refs);
|
|
99
|
-
if (block.type.name === "table") return serializeTable(block, refs);
|
|
100
|
-
return serializePreservedBlock(block, refs);
|
|
101
|
-
}
|
|
102
96
|
function markerXml(node, attr) {
|
|
103
97
|
const xml = node.attrs[attr];
|
|
104
98
|
return typeof xml === "string" ? xml : "";
|
|
105
99
|
}
|
|
106
|
-
function wrapInSdt(xml,
|
|
107
|
-
const prefix =
|
|
108
|
-
|
|
109
|
-
return `${prefix}<w:sdtContent>${xml}</w:sdtContent></w:sdt>`;
|
|
100
|
+
function wrapInSdt(xml, node) {
|
|
101
|
+
const prefix = node.attrs.sdtPrefix;
|
|
102
|
+
return typeof prefix === "string" ? sdtXml(prefix, xml) : xml;
|
|
110
103
|
}
|
|
111
|
-
function cellXml(cell, role, refs) {
|
|
112
|
-
const body = role === "continue" ? elementXml(wName("p"), []) : cell.children.map((block) =>
|
|
104
|
+
function cellXml(cell, role, refs, writeBlock) {
|
|
105
|
+
const body = role === "continue" ? elementXml(wName("p"), []) : cell.children.map((block) => writeBlock(block, refs)).join("");
|
|
113
106
|
const xml = openTagXml(wName("tc"), rawAttrsOf(cell.attrs.tcAttrs)) + cellPropsXml(cell, role) + body + "</w:tc>";
|
|
114
|
-
return wrapInSdt(xml, cell
|
|
107
|
+
return role === "start" ? wrapInSdt(xml, cell) + markerXml(cell, "trailingXml") : xml;
|
|
115
108
|
}
|
|
116
109
|
function placeRow(row, covering) {
|
|
117
110
|
const placed = [];
|
|
@@ -140,15 +133,16 @@ function placeRow(row, covering) {
|
|
|
140
133
|
}
|
|
141
134
|
return placed;
|
|
142
135
|
}
|
|
143
|
-
function rowXml(row, covering, refs) {
|
|
144
|
-
const cells = placeRow(row, covering).map((placed) => cellXml(placed.cell, placed.role, refs)).join("");
|
|
136
|
+
function rowXml(row, covering, refs, writeBlock) {
|
|
137
|
+
const cells = placeRow(row, covering).map((placed) => cellXml(placed.cell, placed.role, refs, writeBlock)).join("");
|
|
145
138
|
const tblPrEx = row.attrs.tblPrEx;
|
|
146
139
|
const trPr = row.attrs.trPr;
|
|
147
|
-
|
|
140
|
+
const xml = openTagXml(wName("tr"), rawAttrsOf(row.attrs.trAttrs)) + (typeof tblPrEx === "string" ? tblPrEx : "") + (typeof trPr === "string" ? trPr : "") + markerXml(row, "leadingXml") + cells + "</w:tr>";
|
|
141
|
+
return wrapInSdt(xml, row) + markerXml(row, "trailingXml");
|
|
148
142
|
}
|
|
149
|
-
function serializeTable(table, refs
|
|
143
|
+
function serializeTable(table, refs, writeBlock) {
|
|
150
144
|
const covering = [];
|
|
151
|
-
const rows = table.children.map((row) => rowXml(row, covering, refs)).join("");
|
|
145
|
+
const rows = table.children.map((row) => rowXml(row, covering, refs, writeBlock)).join("");
|
|
152
146
|
if (covering.some((entry) => entry.rowsLeft > 0)) {
|
|
153
147
|
throw new DocxExportError(
|
|
154
148
|
"invalid-table",
|
package/dist/docx/story.d.ts
CHANGED
|
@@ -62,11 +62,14 @@ export interface StoryDeps {
|
|
|
62
62
|
/**
|
|
63
63
|
* Moves a single block into a node.
|
|
64
64
|
*
|
|
65
|
-
* A paragraph always opens editable (`./importParagraph`)
|
|
66
|
-
*
|
|
67
|
-
*
|
|
65
|
+
* A paragraph always opens editable (`./importParagraph`), and a content control opens as the
|
|
66
|
+
* container of the blocks it holds (`./importSdtBlock`), which are read at `sdtContent` and name
|
|
67
|
+
* no fragment of their own: an edit anywhere inside rewrites the control whole. What is left over
|
|
68
|
+
* is a table whose rows this reader could not take apart, a control it keeps whole, a range marker
|
|
69
|
+
* standing between blocks, and a block this reader has no reader for at all; each stands as one
|
|
70
|
+
* placeholder naming the original fragment.
|
|
68
71
|
*/
|
|
69
|
-
export declare function buildBlock(el: Element, srcId: string, sources: ImportSources, context: FormattingContext): PMNode;
|
|
72
|
+
export declare function buildBlock(el: Element, srcId: string | null, sources: ImportSources, context: FormattingContext, level?: "body" | "sdtContent"): PMNode;
|
|
70
73
|
/**
|
|
71
74
|
* Folds the style chain into the display values.
|
|
72
75
|
*
|
|
@@ -136,5 +139,8 @@ export declare function storyText(story: PMNode | null): string;
|
|
|
136
139
|
* because that text is what the writer puts back and a `w14:paraId` is not a name this schema
|
|
137
140
|
* holds. A paragraph already carrying one keeps it: a key already written is what the thread
|
|
138
141
|
* state elsewhere is keyed by, so re-pointing it would orphan that state.
|
|
142
|
+
*
|
|
143
|
+
* A story ending in a block-level content control keeps its last paragraph inside that control
|
|
144
|
+
* (`docx/storyBlocks`), and the key goes there rather than onto an earlier loose paragraph.
|
|
139
145
|
*/
|
|
140
146
|
export declare function withThreadKeyOn(story: PMNode, paraId: string): PMNode;
|
package/dist/docx/story.js
CHANGED
|
@@ -15,9 +15,11 @@ import {
|
|
|
15
15
|
import { styledParagraph } from "./formatting.js";
|
|
16
16
|
import { buildParagraph } from "./importParagraph.js";
|
|
17
17
|
import { buildPreservedBlock } from "./importPreserved.js";
|
|
18
|
+
import { buildSdtBlock } from "./importSdtBlock.js";
|
|
18
19
|
import { buildTable } from "./importTable.js";
|
|
19
20
|
import { scanBlocksIn } from "./scan.js";
|
|
20
21
|
import { blockKey } from "./session.js";
|
|
22
|
+
import { withLastStoryParagraph } from "./storyBlocks.js";
|
|
21
23
|
import {
|
|
22
24
|
asStoryKey,
|
|
23
25
|
sameStory,
|
|
@@ -26,13 +28,21 @@ import {
|
|
|
26
28
|
withoutCommentStories
|
|
27
29
|
} from "../schema/stories.js";
|
|
28
30
|
var storyOf = storyNodeOf;
|
|
29
|
-
function buildBlock(el, srcId, sources, context) {
|
|
31
|
+
function buildBlock(el, srcId, sources, context, level = "body") {
|
|
30
32
|
if (el.localName === "p") return buildParagraph(el, srcId, sources);
|
|
31
33
|
if (el.localName === "tbl") {
|
|
32
34
|
const table = buildTable(el, srcId, sources, context);
|
|
33
35
|
if (table) return table;
|
|
34
36
|
}
|
|
35
|
-
|
|
37
|
+
if (el.localName === "sdt") {
|
|
38
|
+
const control = buildSdtBlock(
|
|
39
|
+
el,
|
|
40
|
+
srcId,
|
|
41
|
+
(child) => buildBlock(child, null, sources, context, "sdtContent")
|
|
42
|
+
);
|
|
43
|
+
if (control) return control;
|
|
44
|
+
}
|
|
45
|
+
return buildPreservedBlock(el, srcId, level);
|
|
36
46
|
}
|
|
37
47
|
function withStyleFormats(node, context) {
|
|
38
48
|
if (node.type === docxSchema.nodes.paragraph) {
|
|
@@ -196,25 +206,20 @@ function storyText(story) {
|
|
|
196
206
|
return story.textBetween(0, story.content.size, "\n", storyLeafText);
|
|
197
207
|
}
|
|
198
208
|
function withThreadKeyOn(story, paraId) {
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
209
|
+
return withLastStoryParagraph(story, (paragraph) => {
|
|
210
|
+
const written = paragraph.attrs.pAttrs;
|
|
211
|
+
const pAttrs = typeof written === "string" ? written : "";
|
|
212
|
+
if (/\bw14:paraId\s*=/.test(pAttrs)) return null;
|
|
213
|
+
const declared = /\bxmlns:w14\s*=/.test(pAttrs) ? "" : `${xmlnsAttr("w14")[0]}="${NAMESPACES.w14}" `;
|
|
214
|
+
return paragraph.type.create(
|
|
215
|
+
{
|
|
216
|
+
...paragraph.attrs,
|
|
217
|
+
pAttrs: `${pAttrs === "" ? "" : `${pAttrs} `}${declared}${qualify("w14", "paraId")}="${paraId}"`
|
|
218
|
+
},
|
|
219
|
+
paragraph.content,
|
|
220
|
+
paragraph.marks
|
|
221
|
+
);
|
|
202
222
|
});
|
|
203
|
-
if (at === -1) return story;
|
|
204
|
-
const paragraph = story.child(at);
|
|
205
|
-
const written = paragraph.attrs.pAttrs;
|
|
206
|
-
const pAttrs = typeof written === "string" ? written : "";
|
|
207
|
-
if (/\bw14:paraId\s*=/.test(pAttrs)) return story;
|
|
208
|
-
const declared = /\bxmlns:w14\s*=/.test(pAttrs) ? "" : `${xmlnsAttr("w14")[0]}="${NAMESPACES.w14}" `;
|
|
209
|
-
const keyed = paragraph.type.create(
|
|
210
|
-
{
|
|
211
|
-
...paragraph.attrs,
|
|
212
|
-
pAttrs: `${pAttrs === "" ? "" : `${pAttrs} `}${declared}${qualify("w14", "paraId")}="${paraId}"`
|
|
213
|
-
},
|
|
214
|
-
paragraph.content,
|
|
215
|
-
paragraph.marks
|
|
216
|
-
);
|
|
217
|
-
return story.copy(story.content.replaceChild(at, keyed));
|
|
218
223
|
}
|
|
219
224
|
export {
|
|
220
225
|
asStoryKey,
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Walking the blocks of a story the way the sequence the document writes them in runs.
|
|
3
|
+
*
|
|
4
|
+
* `CT_SdtContentBlock` (§17.5.2.34) holds what `CT_Body` holds, so a paragraph inside a
|
|
5
|
+
* block-level content control is one of the story's own paragraphs and is reached here, at any
|
|
6
|
+
* depth of nesting. A table cell's paragraphs are not that sequence - a table is one block of the
|
|
7
|
+
* story and what stands in its cells belongs to the cells - so a table is not walked into.
|
|
8
|
+
*
|
|
9
|
+
* Everything that has to name the last, the first or every paragraph of a story asks here, so the
|
|
10
|
+
* readers cannot come to disagree about what a control is.
|
|
11
|
+
*/
|
|
12
|
+
import type { Node as PMNode } from "prosemirror-model";
|
|
13
|
+
/**
|
|
14
|
+
* Every paragraph one block of a story stands for, in document order: the block itself where it is
|
|
15
|
+
* a paragraph, and otherwise the paragraphs of the content control around them.
|
|
16
|
+
*
|
|
17
|
+
* `pos` is where the block itself stands, and each paragraph is visited with its own position.
|
|
18
|
+
*/
|
|
19
|
+
export declare function eachStoryParagraph(block: PMNode, pos: number, visit: (paragraph: PMNode, paragraphPos: number) => void): void;
|
|
20
|
+
/** The paragraphs a whole story draws, in order */
|
|
21
|
+
export declare function storyParagraphs(story: PMNode): readonly PMNode[];
|
|
22
|
+
/**
|
|
23
|
+
* The one paragraph a story holds and nothing besides, and null for every other story. A control
|
|
24
|
+
* around it is not content of its own, so a story holding one is as empty as its paragraph is.
|
|
25
|
+
*/
|
|
26
|
+
export declare function onlyStoryParagraph(container: PMNode): PMNode | null;
|
|
27
|
+
/**
|
|
28
|
+
* The story with its last paragraph rewritten, wherever the controls around it put it. The story
|
|
29
|
+
* itself comes back where it holds no paragraph, or where `rewrite` answers null for the one it
|
|
30
|
+
* found.
|
|
31
|
+
*/
|
|
32
|
+
export declare function withLastStoryParagraph(story: PMNode, rewrite: (paragraph: PMNode) => PMNode | null): PMNode;
|