@portone/docx-editor 0.6.1 → 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 (92) hide show
  1. package/CHANGELOG.md +78 -0
  2. package/dist/DocxEditor.js +2 -2
  3. package/dist/core.d.ts +4 -3
  4. package/dist/docx/exportDocx.d.ts +1 -1
  5. package/dist/docx/exportDocx.js +3 -1
  6. package/dist/docx/headersFooters.js +4 -14
  7. package/dist/docx/identities.d.ts +10 -2
  8. package/dist/docx/identities.js +32 -11
  9. package/dist/docx/importPolicy.d.ts +2 -2
  10. package/dist/docx/importPolicy.js +12 -5
  11. package/dist/docx/importPreserved.d.ts +3 -3
  12. package/dist/docx/importSdtBlock.d.ts +24 -0
  13. package/dist/docx/importSdtBlock.js +42 -0
  14. package/dist/docx/importTable.js +35 -24
  15. package/dist/docx/invariants.d.ts +13 -10
  16. package/dist/docx/invariants.js +269 -126
  17. package/dist/docx/newLists.js +12 -3
  18. package/dist/docx/notes/numbering.js +5 -12
  19. package/dist/docx/notes/references.d.ts +31 -0
  20. package/dist/docx/notes/references.js +32 -0
  21. package/dist/docx/notes/writing.js +5 -6
  22. package/dist/docx/sdt.d.ts +52 -9
  23. package/dist/docx/sdt.js +108 -26
  24. package/dist/docx/sections.d.ts +13 -13
  25. package/dist/docx/sections.js +34 -20
  26. package/dist/docx/serializeBlock.js +9 -1
  27. package/dist/docx/serializePreserved.d.ts +5 -1
  28. package/dist/docx/serializePreserved.js +24 -7
  29. package/dist/docx/serializeSdtBlock.d.ts +24 -0
  30. package/dist/docx/serializeSdtBlock.js +15 -0
  31. package/dist/docx/serializeTable.d.ts +8 -2
  32. package/dist/docx/serializeTable.js +14 -20
  33. package/dist/docx/story.d.ts +10 -4
  34. package/dist/docx/story.js +25 -20
  35. package/dist/docx/storyBlocks.d.ts +32 -0
  36. package/dist/docx/storyBlocks.js +65 -0
  37. package/dist/docx/storyParts.d.ts +29 -8
  38. package/dist/docx/storyParts.js +40 -15
  39. package/dist/docx/wrappers.d.ts +10 -7
  40. package/dist/docx/wrappers.js +15 -27
  41. package/dist/editor/blockControlEdits.d.ts +15 -0
  42. package/dist/editor/blockControlEdits.js +79 -0
  43. package/dist/editor/clipboard/normalizers.js +1 -1
  44. package/dist/editor/commands/exportQueries.d.ts +4 -3
  45. package/dist/editor/commands/index.d.ts +1 -1
  46. package/dist/editor/commands/lockCommands.d.ts +5 -1
  47. package/dist/editor/commands/lockCommands.js +27 -19
  48. package/dist/editor/createEditor.js +4 -0
  49. package/dist/editor/notes/noteSurface.js +4 -3
  50. package/dist/editor/plugins/controlLifecycle.d.ts +18 -0
  51. package/dist/editor/plugins/controlLifecycle.js +130 -0
  52. package/dist/editor/plugins/keymap.js +23 -5
  53. package/dist/editor/plugins/rowResize.js +2 -4
  54. package/dist/editor/stories/storyState.d.ts +2 -0
  55. package/dist/editor/stories/storyState.js +2 -0
  56. package/dist/index.d.ts +2 -2
  57. package/dist/ooxml/errors.d.ts +135 -2
  58. package/dist/ooxml/errors.js +6 -0
  59. package/dist/page/blockKinds.d.ts +24 -2
  60. package/dist/page/blockKinds.js +6 -1
  61. package/dist/page/kinds/index.d.ts +5 -0
  62. package/dist/page/kinds/index.js +2 -0
  63. package/dist/page/kinds/sdtBlockKind.d.ts +14 -0
  64. package/dist/page/kinds/sdtBlockKind.js +143 -0
  65. package/dist/page/measureBlocks.js +4 -2
  66. package/dist/page/pageDecorations.js +3 -2
  67. package/dist/page/pageLayout.d.ts +16 -15
  68. package/dist/page/pageLayout.js +134 -67
  69. package/dist/page/usePageLayout.d.ts +2 -2
  70. package/dist/page/usePageLayout.js +5 -5
  71. package/dist/schema/attrRoles.js +45 -1
  72. package/dist/schema/clipboard.js +9 -0
  73. package/dist/schema/controlAttrs.d.ts +105 -0
  74. package/dist/schema/controlAttrs.js +129 -0
  75. package/dist/schema/controlEdges.d.ts +22 -0
  76. package/dist/schema/controlEdges.js +50 -0
  77. package/dist/schema/docxSchema.d.ts +1 -1
  78. package/dist/schema/docxSchema.js +180 -37
  79. package/dist/schema/editGuard.d.ts +1 -1
  80. package/dist/schema/guards.js +2 -0
  81. package/dist/schema/locks.d.ts +22 -6
  82. package/dist/schema/locks.js +88 -47
  83. package/dist/schema/stories.d.ts +11 -3
  84. package/dist/schema/stories.js +9 -3
  85. package/dist/styles/classNames.d.ts +8 -0
  86. package/dist/styles/classNames.js +8 -0
  87. package/dist/styles.css +31 -2
  88. package/dist/table/merge.js +11 -3
  89. package/dist/table/rowResize.d.ts +9 -0
  90. package/dist/table/rowResize.js +9 -5
  91. package/dist/ui/TextMenu.js +2 -0
  92. package/package.json +3 -3
@@ -2,7 +2,9 @@
2
2
  import { elementXml } from "../../ooxml/element.js";
3
3
  import { wName } from "../../ooxml/names.js";
4
4
  import { R_NS } from "../../ooxml/xml.js";
5
+ import { noteKeyOf } from "../../schema/stories.js";
5
6
  import { storyEntriesPlanner } from "../storyParts.js";
7
+ import { eachNoteReference } from "./references.js";
6
8
  var NOTE_CONTENT_TYPES = {
7
9
  footnote: "application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml",
8
10
  endnote: "application/vnd.openxmlformats-officedocument.wordprocessingml.endnotes+xml"
@@ -60,12 +62,9 @@ function frozenNotes(session, kind) {
60
62
  }
61
63
  function referenceIds(doc, kind) {
62
64
  const ids = /* @__PURE__ */ new Set();
63
- doc.descendants((node) => {
64
- const id = node.attrs.id;
65
- if (node.type.name === "noteReference" && node.attrs.kind === kind && typeof id === "string") {
66
- ids.add(id);
67
- }
68
- return true;
65
+ eachNoteReference(doc, ({ key }) => {
66
+ const note = key === null ? null : noteKeyOf(key);
67
+ if (note?.kind === kind) ids.add(note.id);
69
68
  });
70
69
  return ids;
71
70
  }
@@ -7,22 +7,65 @@
7
7
  * and stands last. Everything else is null, which leaves whatever held the control preserved
8
8
  * together with its original fragment.
9
9
  *
10
- * What sits inside the control is the caller's business. A whole cell (`docx/importTable`) and
11
- * a stretch of runs inside a paragraph (`docx/importParagraph`) each read the content their
12
- * own way, and the wrapper they put back on export is the same string in both cases.
10
+ * What sits inside the control is the caller's business. A whole cell or row
11
+ * (`docx/importTable`), the blocks under a body or a cell (`docx/importSdtBlock`) and a stretch of
12
+ * runs inside a paragraph (`docx/importParagraph`) each read the content their own way, and the
13
+ * wrapper they put back on export is the same string in all of them.
13
14
  */
14
- /** The opening of a content control taken apart, and the content it wraps */
15
- export interface SdtWrapper {
15
+ import type { ControlFacts, ControlFlags } from "../schema/controlAttrs";
16
+ /**
17
+ * What a control's `w:sdtPr` states, which is all the editor reads off a control besides the XML
18
+ * it goes back out as. The carriers write it under names of their own (`schema/controlAttrs`).
19
+ */
20
+ export type SdtFacts = ControlFlags;
21
+ /** The opening of a content control taken apart, the content it wraps, and what it says */
22
+ export interface SdtWrapper extends SdtFacts {
16
23
  /** The `<w:sdt>` opening tag followed by everything that stood ahead of `<w:sdtContent>` */
17
24
  prefix: string;
18
25
  content: Element;
19
- /** Whether the control says its contents may not be edited */
20
- contentsLocked: boolean;
21
- /** Whether the control says it may not be deleted, not even whole */
22
- deletionLocked: boolean;
23
26
  }
24
27
  /** Takes a `w:sdt` apart into the wrapper to put back on export and the content to read. null for a shape we do not write ourselves */
25
28
  export declare function readSdtWrapper(el: Element): SdtWrapper | null;
29
+ /**
30
+ * A `w:sdt` that carries no `w:sdtContent` at all, as the facts its carrier records. null for
31
+ * anything else, this editor's own shape of control included.
32
+ *
33
+ * `CT_Sdt` writes the content element `minOccurs="0"`, so a control stating that what it stood
34
+ * around is not there may leave it out entirely, and §17.5.2.34 makes that element a cache of
35
+ * contents rather than the statement itself. `readSdtWrapper` turns such a control down and stays
36
+ * that way: the inline mark and a wrapped cell or row all read the content it hands back, and
37
+ * there is none here for them to stand around. Everything the control carries, `w:sdtEndPr`
38
+ * included, rides in the prefix, exactly as it does when a content element follows it.
39
+ */
40
+ export declare function readEmptySdt(el: Element): ControlFacts | null;
41
+ /**
42
+ * What the carrier of this control records about it (`schema/controlAttrs`).
43
+ *
44
+ * Every carrier derives it from the wrapper here rather than from the element again, so the inline
45
+ * mark, the block container, the node a control holding nothing stands as, a wrapped cell and a
46
+ * wrapped row cannot come to disagree about what one and the same `w:sdtPr` says.
47
+ */
48
+ export declare function controlFactsFrom(wrapper: SdtWrapper): ControlFacts;
49
+ /**
50
+ * Whether this control's content may be read as a container of blocks, which is read off the
51
+ * `w:sdtPr` alone so that a control kept whole never has its prefix serialized.
52
+ *
53
+ * Every other type - `w:richText`, which is also what a control naming no type is, `w:group`, the
54
+ * gallery and field types, and the extension types a closed `CT_SdtPr` can only carry as ignorable
55
+ * markup - says what the control means rather than what it may hold.
56
+ */
57
+ export declare function modelsBlockContent(el: Element): boolean;
58
+ /**
59
+ * The `w:sdtContent` this editor writes itself, and the control it closes after it.
60
+ *
61
+ * `readSdtWrapper` takes back apart exactly this shape, so the two stand together: a control whose
62
+ * content tag carried an attribute, or did not stand last, is one this never writes.
63
+ */
64
+ export declare const SDT_CLOSING_XML = "</w:sdtContent></w:sdt>";
65
+ /** The control's opening tag as it arrived, up to the content tag. A wrapper that lost it cannot go out */
66
+ export declare function sdtOpeningXml(prefix: unknown): string;
67
+ /** One whole control: the opening it arrived with, around content this writer owns */
68
+ export declare function sdtXml(prefix: unknown, content: string): string;
26
69
  /**
27
70
  * The number Word writes on every control. Nothing reads it back and it only has to differ
28
71
  * from the other controls in the document, so a draw out of the whole 32 bit range is enough.
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
  }
@@ -7,8 +7,12 @@
7
7
  * the same way, from here.
8
8
  */
9
9
  import type { Node as PMNode } from "prosemirror-model";
10
+ import { type ExportProblemReason, type ExportProblemStory } from "../ooxml/errors";
10
11
  import type { ExportRefs } from "./exportRefs";
11
12
  import { type SessionStore } from "./session";
12
13
  /** Why there is no original to write, which a block that came in from another document answers differently */
13
- export declare function lostOriginal(node: PMNode, session: SessionStore | null): string;
14
+ export declare function lostOriginal(node: PMNode, session: SessionStore | null, story: ExportProblemStory | null): {
15
+ readonly message: string;
16
+ readonly reason: ExportProblemReason;
17
+ };
14
18
  export declare function serializePreservedBlock(node: PMNode, refs: ExportRefs): string;
@@ -1,20 +1,37 @@
1
1
  // src/docx/serializePreserved.ts
2
- import { DocxExportError } from "../ooxml/errors.js";
2
+ import {
3
+ DocxExportError
4
+ } from "../ooxml/errors.js";
3
5
  import { originalBlock, splitBlockKey } from "./session.js";
4
- function lostOriginal(node, session) {
6
+ function lostOriginal(node, session, story) {
5
7
  const srcId = node.attrs.srcId;
6
8
  const key = typeof srcId === "string" ? splitBlockKey(srcId) : null;
7
- return key === null || key.sessionId === session?.sessionId ? "a preserved block has lost its original XML" : `a preserved block comes from another document (${key.sessionId})`;
9
+ const name = node.type.name;
10
+ if (key === null || key.sessionId === session?.sessionId) {
11
+ return {
12
+ message: "a preserved block has lost its original XML",
13
+ reason: { kind: "lost-preserved-xml", node: name, story }
14
+ };
15
+ }
16
+ return {
17
+ message: `a preserved block comes from another document (${key.sessionId})`,
18
+ reason: {
19
+ kind: "preserved-from-another-document",
20
+ node: name,
21
+ sessionId: key.sessionId,
22
+ story
23
+ }
24
+ };
8
25
  }
9
26
  function serializePreservedBlock(node, refs) {
10
27
  const xml = node.attrs.xml;
11
28
  if (typeof xml === "string") return xml;
12
29
  const imported = refs.session ? originalBlock(node, refs.session) : void 0;
13
30
  if (!imported) {
14
- throw new DocxExportError(
15
- "lost-original",
16
- lostOriginal(node, refs.session)
17
- );
31
+ const { message, reason } = lostOriginal(node, refs.session, null);
32
+ throw new DocxExportError("lost-original", message, {
33
+ problem: { code: "lost-original", message, reason }
34
+ });
18
35
  }
19
36
  return imported.xml;
20
37
  }
@@ -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",