@portone/docx-editor 0.6.2 → 0.6.3

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.
Files changed (74) hide show
  1. package/CHANGELOG.md +62 -0
  2. package/dist/DocxEditor.js +2 -2
  3. package/dist/docx/headersFooters.js +4 -14
  4. package/dist/docx/identities.d.ts +8 -2
  5. package/dist/docx/identities.js +31 -11
  6. package/dist/docx/importPolicy.d.ts +2 -2
  7. package/dist/docx/importPolicy.js +12 -5
  8. package/dist/docx/importPreserved.d.ts +3 -3
  9. package/dist/docx/importSdtBlock.d.ts +24 -0
  10. package/dist/docx/importSdtBlock.js +42 -0
  11. package/dist/docx/importTable.js +35 -24
  12. package/dist/docx/notes/numbering.js +4 -4
  13. package/dist/docx/notes/references.d.ts +0 -2
  14. package/dist/docx/notes/references.js +2 -7
  15. package/dist/docx/sdt.d.ts +52 -9
  16. package/dist/docx/sdt.js +108 -26
  17. package/dist/docx/sections.d.ts +13 -13
  18. package/dist/docx/sections.js +34 -20
  19. package/dist/docx/serializeBlock.js +9 -1
  20. package/dist/docx/serializeSdtBlock.d.ts +24 -0
  21. package/dist/docx/serializeSdtBlock.js +15 -0
  22. package/dist/docx/serializeTable.d.ts +8 -2
  23. package/dist/docx/serializeTable.js +14 -20
  24. package/dist/docx/story.d.ts +10 -4
  25. package/dist/docx/story.js +25 -20
  26. package/dist/docx/storyBlocks.d.ts +32 -0
  27. package/dist/docx/storyBlocks.js +65 -0
  28. package/dist/docx/wrappers.d.ts +10 -7
  29. package/dist/docx/wrappers.js +15 -27
  30. package/dist/editor/blockControlEdits.d.ts +15 -0
  31. package/dist/editor/blockControlEdits.js +79 -0
  32. package/dist/editor/clipboard/normalizers.js +1 -1
  33. package/dist/editor/commands/lockCommands.d.ts +5 -1
  34. package/dist/editor/commands/lockCommands.js +27 -19
  35. package/dist/editor/createEditor.js +4 -0
  36. package/dist/editor/notes/noteSurface.js +4 -3
  37. package/dist/editor/plugins/controlLifecycle.d.ts +18 -0
  38. package/dist/editor/plugins/controlLifecycle.js +130 -0
  39. package/dist/editor/plugins/keymap.js +23 -5
  40. package/dist/editor/plugins/rowResize.js +2 -4
  41. package/dist/editor/stories/storyState.d.ts +2 -0
  42. package/dist/editor/stories/storyState.js +2 -0
  43. package/dist/page/blockKinds.d.ts +24 -2
  44. package/dist/page/blockKinds.js +6 -1
  45. package/dist/page/kinds/index.d.ts +5 -0
  46. package/dist/page/kinds/index.js +2 -0
  47. package/dist/page/kinds/sdtBlockKind.d.ts +14 -0
  48. package/dist/page/kinds/sdtBlockKind.js +143 -0
  49. package/dist/page/measureBlocks.js +4 -2
  50. package/dist/page/pageDecorations.js +3 -2
  51. package/dist/page/pageLayout.d.ts +16 -15
  52. package/dist/page/pageLayout.js +134 -67
  53. package/dist/page/usePageLayout.d.ts +2 -2
  54. package/dist/page/usePageLayout.js +5 -5
  55. package/dist/schema/attrRoles.js +45 -1
  56. package/dist/schema/clipboard.js +9 -0
  57. package/dist/schema/controlAttrs.d.ts +105 -0
  58. package/dist/schema/controlAttrs.js +129 -0
  59. package/dist/schema/controlEdges.d.ts +22 -0
  60. package/dist/schema/controlEdges.js +50 -0
  61. package/dist/schema/docxSchema.d.ts +1 -1
  62. package/dist/schema/docxSchema.js +180 -37
  63. package/dist/schema/editGuard.d.ts +1 -1
  64. package/dist/schema/guards.js +2 -0
  65. package/dist/schema/locks.d.ts +22 -6
  66. package/dist/schema/locks.js +88 -47
  67. package/dist/styles/classNames.d.ts +8 -0
  68. package/dist/styles/classNames.js +8 -0
  69. package/dist/styles.css +31 -2
  70. package/dist/table/merge.js +11 -3
  71. package/dist/table/rowResize.d.ts +9 -0
  72. package/dist/table/rowResize.js +9 -5
  73. package/dist/ui/TextMenu.js +2 -0
  74. 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 { wName } from "../ooxml/names.js";
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 lockValue(sdtPr) {
15
- const lock = elementChildren(sdtPr).find(
16
- (child) => child.localName === "lock"
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
- return lock ? wAttr(lock, "val") : null;
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,63 @@ 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: openTagXml(wName("sdt"), attrs) + head.map(serializeXml).join(""),
56
+ prefix: sdtPrefix(el, head),
37
57
  content,
38
- contentsLocked: val !== null && CONTENTS_LOCKED.includes(val),
39
- deletionLocked: val !== null && DELETION_LOCKED.includes(val)
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)
70
+ };
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
40
80
  };
41
81
  }
82
+ var RESTRAINED_TYPES = [
83
+ `{${W_NS}}text`,
84
+ `{${W_NS}}picture`,
85
+ `{${W_NS}}date`,
86
+ `{${W_NS}}comboBox`,
87
+ `{${W_NS}}dropDownList`,
88
+ `{${NAMESPACES.w14}}checkbox`
89
+ ];
90
+ function modelsBlockContent(el) {
91
+ const sdtPr = elementChildren(el).find(
92
+ (child) => child.localName === "sdtPr"
93
+ );
94
+ if (!sdtPr) return true;
95
+ return !elementChildren(sdtPr).some(
96
+ (child) => RESTRAINED_TYPES.includes(qualifiedName(child))
97
+ );
98
+ }
99
+ var SDT_CLOSING_XML = "</w:sdtContent></w:sdt>";
100
+ function sdtOpeningXml(prefix) {
101
+ if (typeof prefix !== "string") {
102
+ throw new DocxExportError(
103
+ "lost-original",
104
+ "a content control has lost the opening XML it goes back out as"
105
+ );
106
+ }
107
+ return `${prefix}<w:sdtContent>`;
108
+ }
109
+ function sdtXml(prefix, content) {
110
+ return sdtOpeningXml(prefix) + content + SDT_CLOSING_XML;
111
+ }
42
112
  function newControlId() {
43
113
  return Math.floor(Math.random() * 2147483647);
44
114
  }
@@ -47,19 +117,25 @@ function renderPrefix(props) {
47
117
  return openTagXml(props.tag, props.attrs) + inner + (props.tail ?? "");
48
118
  }
49
119
  var NAMES_NOTHING = ["id", "lock"];
50
- function namesNothing(prefix) {
120
+ function prefixProps(prefix) {
51
121
  const sdt = parseProps(`${prefix}</w:sdt>`);
52
- if (!sdt || sdt.attrs !== null) return false;
53
- const sdtPr = propsChild(sdt.children, "sdtPr");
54
- const props = sdtPr ? parseProps(sdtPr.xml) : null;
55
- if (!props || sdt.children.length !== 1) return false;
56
- return props.children.every((child) => NAMES_NOTHING.includes(child.name));
122
+ const child = sdt && propsChild(sdt.children, "sdtPr");
123
+ const sdtPr = child ? parseProps(child.xml) : null;
124
+ return sdt && sdtPr ? { sdt, sdtPr } : null;
125
+ }
126
+ function namesNothing(prefix) {
127
+ const props = prefixProps(prefix);
128
+ if (!props || props.sdt.attrs !== null || props.sdt.children.length !== 1) {
129
+ return false;
130
+ }
131
+ return props.sdtPr.children.every(
132
+ (child) => NAMES_NOTHING.includes(child.name)
133
+ );
57
134
  }
58
135
  function editSdtPrefix(prefix, edits) {
59
- const sdt = parseProps(`${prefix}</w:sdt>`);
60
- const sdtPr = sdt && propsChild(sdt.children, "sdtPr");
61
- const props = sdtPr ? parseProps(sdtPr.xml) : null;
62
- if (!sdt || !props) return null;
136
+ const read = prefixProps(prefix);
137
+ if (!read) return null;
138
+ const { sdt, sdtPr: props } = read;
63
139
  const rendered = renderElement(
64
140
  edits.reduce((kept, [name, xml]) => setChild(kept, name, xml), props)
65
141
  );
@@ -73,9 +149,15 @@ function copiedControlPrefix(prefix, id) {
73
149
  return copied ?? prefix;
74
150
  }
75
151
  export {
152
+ SDT_CLOSING_XML,
153
+ controlFactsFrom,
76
154
  copiedControlPrefix,
77
155
  editSdtPrefix,
156
+ modelsBlockContent,
78
157
  namesNothing,
79
158
  newControlId,
80
- readSdtWrapper
159
+ readEmptySdt,
160
+ readSdtWrapper,
161
+ sdtOpeningXml,
162
+ sdtXml
81
163
  };
@@ -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
- /** Which `w:sectPr` closes a section: the paragraph carrying it, or the body itself */
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
- /** Top-level block indexes this section covers, inclusive */
56
- firstBlock: number;
57
- lastBlock: number;
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. The last section covers no block where the final paragraph itself ends a section, which
120
- * leaves `firstBlock` past `lastBlock`.
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
- * Reading the table walks every block of the document, so anything asking about more than one
127
- * position - the pages of a preview, one per section - reads it once and asks here.
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 sectionIn(sections: readonly DocumentSection[], doc: PMNode, pos: number): DocumentSection;
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;
@@ -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 firstBlock = 0;
168
- doc.forEach((child, offset, index) => {
169
- const brk = sectionBreakOf(pPrText(child));
170
- if (brk === null) return;
171
- sections.push({
172
- index: sections.length,
173
- firstBlock,
174
- lastBlock: index,
175
- anchor: { kind: "paragraph", pos: offset },
176
- props: parseSectionProperties(brk) ?? { ...DEFAULT_SECTION, xml: brk }
177
- });
178
- firstBlock = index + 1;
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
- firstBlock,
185
- lastBlock: doc.childCount - 1,
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
- return sectionIn(sectionsOf(doc), doc, pos);
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,22 @@
1
1
  // src/docx/serializeBlock.ts
2
2
  import { DocxExportError } from "../ooxml/errors.js";
3
+ import { isBlockControl, isEmptyControl } from "../schema/controlAttrs.js";
3
4
  import { sameSource } from "../schema/sourceEquality.js";
4
5
  import { NO_EXPORT_REFS } from "./exportRefs.js";
5
6
  import { serializeParagraph } from "./serializeParagraph.js";
6
7
  import { serializePreservedBlock } from "./serializePreserved.js";
8
+ import { serializeSdtBlock, serializeSdtEmpty } from "./serializeSdtBlock.js";
7
9
  import { serializeTable } from "./serializeTable.js";
8
10
  import { originalBlock } from "./session.js";
9
11
  function serializeBlock(node, refs = NO_EXPORT_REFS) {
10
12
  if (node.type.name === "paragraph") return serializeParagraph(node, refs);
11
- if (node.type.name === "table") return serializeTable(node, refs);
13
+ if (node.type.name === "table") {
14
+ return serializeTable(node, refs, serializeBlock);
15
+ }
16
+ if (isBlockControl(node)) {
17
+ return serializeSdtBlock(node, refs, serializeBlock);
18
+ }
19
+ if (isEmptyControl(node)) return serializeSdtEmpty(node);
12
20
  if (node.type.isInGroup("preserved")) {
13
21
  return serializePreservedBlock(node, refs);
14
22
  }
@@ -0,0 +1,24 @@
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;
17
+ /**
18
+ * A control holding nothing, which is the same opening around a content tag with nothing in it.
19
+ *
20
+ * A control that arrived writing no content element at all gains one here. The two shapes state
21
+ * the same thing - §17.5.2.34 makes the element a cache of what stood inside - and only a control
22
+ * something rewrote reaches this at all; one nobody touched goes out as the bytes it arrived as.
23
+ */
24
+ export declare function serializeSdtEmpty(node: PMNode): string;
@@ -0,0 +1,15 @@
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
+ function serializeSdtEmpty(node) {
10
+ return sdtXml(node.attrs.sdtPrefix, "");
11
+ }
12
+ export {
13
+ serializeSdtBlock,
14
+ serializeSdtEmpty
15
+ };
@@ -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 { type ExportRefs } from "./exportRefs";
12
- export declare function serializeTable(table: PMNode, refs?: ExportRefs): string;
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 { NO_EXPORT_REFS } from "./exportRefs.js";
20
- import { rawAttrsOf, serializeParagraph } from "./serializeParagraph.js";
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, cell, role) {
107
- const prefix = cell.attrs.sdtPrefix;
108
- if (role !== "start" || typeof prefix !== "string") return xml;
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) => cellBlockXml(block, refs)).join("");
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, role) + (role === "start" ? markerXml(cell, "trailingXml") : "");
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
- return openTagXml(wName("tr"), rawAttrsOf(row.attrs.trAttrs)) + (typeof tblPrEx === "string" ? tblPrEx : "") + (typeof trPr === "string" ? trPr : "") + markerXml(row, "leadingXml") + cells + "</w:tr>" + markerXml(row, "trailingXml");
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 = NO_EXPORT_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",
@@ -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`). What is left over is a table whose rows
66
- * this reader could not take apart, a range marker standing between blocks, and a block this
67
- * reader has no reader for at all; each stands as one placeholder naming the original fragment.
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;
@@ -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
- return buildPreservedBlock(el, srcId, "body");
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
- let at = -1;
200
- story.forEach((block, _offset, index) => {
201
- if (block.type === docxSchema.nodes.paragraph) at = index;
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;