@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
@@ -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;
@@ -0,0 +1,65 @@
1
+ // src/docx/storyBlocks.ts
2
+ import { docxSchema } from "../schema/index.js";
3
+ import { isBlockControl } from "../schema/controlAttrs.js";
4
+ function isParagraph(node) {
5
+ return node.type === docxSchema.nodes.paragraph;
6
+ }
7
+ function eachStoryParagraph(block, pos, visit) {
8
+ if (isParagraph(block)) {
9
+ visit(block, pos);
10
+ return;
11
+ }
12
+ if (!isBlockControl(block)) return;
13
+ block.forEach((child, offset) => {
14
+ eachStoryParagraph(child, pos + 1 + offset, visit);
15
+ });
16
+ }
17
+ function storyParagraphs(story) {
18
+ const found = [];
19
+ story.forEach((block, offset) => {
20
+ eachStoryParagraph(block, offset, (paragraph) => found.push(paragraph));
21
+ });
22
+ return found;
23
+ }
24
+ function onlyStoryParagraph(container) {
25
+ const only = container.childCount === 1 ? container.firstChild : null;
26
+ if (only === null) return null;
27
+ if (isParagraph(only)) return only;
28
+ return isBlockControl(only) ? onlyStoryParagraph(only) : null;
29
+ }
30
+ function lastParagraphPath(container) {
31
+ for (let index = container.childCount - 1; index >= 0; index -= 1) {
32
+ const child = container.child(index);
33
+ if (isParagraph(child)) return [index];
34
+ if (!isBlockControl(child)) continue;
35
+ const deeper = lastParagraphPath(child);
36
+ if (deeper !== null) return [index, ...deeper];
37
+ }
38
+ return null;
39
+ }
40
+ function replaceAtPath(container, path, written) {
41
+ const [index, ...rest] = path;
42
+ if (index === void 0) return written;
43
+ return container.copy(
44
+ container.content.replaceChild(
45
+ index,
46
+ rest.length === 0 ? written : replaceAtPath(container.child(index), rest, written)
47
+ )
48
+ );
49
+ }
50
+ function withLastStoryParagraph(story, rewrite) {
51
+ const path = lastParagraphPath(story);
52
+ if (path === null) return story;
53
+ const paragraph = path.reduce(
54
+ (node, index) => node.child(index),
55
+ story
56
+ );
57
+ const written = rewrite(paragraph);
58
+ return written === null ? story : replaceAtPath(story, path, written);
59
+ }
60
+ export {
61
+ eachStoryParagraph,
62
+ onlyStoryParagraph,
63
+ storyParagraphs,
64
+ withLastStoryParagraph
65
+ };
@@ -6,7 +6,7 @@
6
6
  * and the export invariants would have a third answer of their own.
7
7
  */
8
8
  import type { Node as PMNode } from "prosemirror-model";
9
- import type { DocxExportErrorCode } from "../ooxml/errors";
9
+ import type { DocxExportErrorCode, ExportPartName, ExportProblemReason } from "../ooxml/errors";
10
10
  import { type StoryKey, type StoryKind } from "../schema/stories";
11
11
  import { type StoryToSettle } from "./identities";
12
12
  import type { PartPlanner } from "./partPlan";
@@ -27,8 +27,6 @@ export type StoryChange = {
27
27
  readonly key: StoryKey;
28
28
  readonly current: PMNode;
29
29
  };
30
- /** The id a key of this kind names */
31
- export declare function storyIdOf(key: StoryKey, kind: StoryKind): string;
32
30
  /** Every story of this kind the package arrived with or the document now holds, in part order then id order */
33
31
  export declare function storyChangesOf(doc: PMNode, session: SessionStore, kind: StoryKind): readonly StoryChange[];
34
32
  export type StoryChanged = Exclude<StoryChange["change"], "kept">;
@@ -47,6 +45,11 @@ export interface StoryWriting {
47
45
  export declare function storyEntriesWriting(part: StoryEntriesPart): StoryWriting;
48
46
  /** A writer that takes these changes to every story of the kind, freezing none of them */
49
47
  export declare function storyWriting(kind: StoryKind, changes: readonly StoryChanged[]): StoryWriting;
48
+ /** A change to a side story that no part writer carries into the file */
49
+ export interface UnwrittenStoryChange {
50
+ readonly key: StoryKey;
51
+ readonly change: StoryChanged;
52
+ }
50
53
  /**
51
54
  * Every change the document made to a side story that no part writer carries into the file, as the
52
55
  * key it stands under and what was done to it.
@@ -56,13 +59,26 @@ export declare function storyWriting(kind: StoryKind, changes: readonly StoryCha
56
59
  * writer does not write - and a change to an entry written as it arrived are all reported here
57
60
  * rather than dropped from the file without a word.
58
61
  */
59
- export declare function unwrittenStoryChanges(writings: readonly StoryWriting[], doc: PMNode, session: SessionStore): readonly {
62
+ export declare function unwrittenStoryChanges(writings: readonly StoryWriting[], doc: PMNode, session: SessionStore): readonly UnwrittenStoryChange[];
63
+ /** One side story the export writes block by block, under the key it stands at */
64
+ export interface RewrittenStory {
60
65
  readonly key: StoryKey;
61
- readonly change: StoryChanged;
62
- }[];
66
+ readonly story: PMNode;
67
+ }
68
+ /**
69
+ * Every side story the export writes block by block, which is every one a part writer carries an
70
+ * addition or an edit of.
71
+ *
72
+ * A story the writer hands back as the bytes it arrived as - one nobody touched, one its part
73
+ * freezes, one whose change no writer carries, which `unwrittenStoryChanges` refuses instead -
74
+ * goes out holding exactly what it came in holding, so nothing about it can newly be refused. The
75
+ * same "changed and written" answer decides both, so a writer and the invariants cannot disagree
76
+ * about which story is written from a node.
77
+ */
78
+ export declare function rewrittenStories(writings: readonly StoryWriting[], doc: PMNode, session: SessionStore): readonly RewrittenStory[];
63
79
  /** A part holding one entry per story, e.g. `w:footnotes` holding a `w:footnote` apiece */
64
80
  export interface StoryEntriesPart {
65
- readonly name: string;
81
+ readonly name: ExportPartName;
66
82
  readonly kind: StoryKind;
67
83
  readonly relType: string;
68
84
  readonly contentType: string;
@@ -83,16 +99,21 @@ export interface StoryEntriesPart {
83
99
  /** The entries a part this planner creates opens with, before any story */
84
100
  prelude(taken: ReadonlySet<string>): string;
85
101
  }
102
+ /** One story a part writes, beside the key it stands under */
103
+ export interface StoryEntry extends StoryToSettle {
104
+ readonly key: StoryKey;
105
+ }
86
106
  /**
87
107
  * The stories a part is written with, in the order it writes them, and none where no story of its
88
108
  * kind changed and the part is not written at all. The identity pass runs over this list as one
89
109
  * part, and the export invariants ask the same list.
90
110
  */
91
- export declare function storyEntriesOf(part: StoryEntriesPart, doc: PMNode, session: SessionStore): readonly StoryToSettle[];
111
+ export declare function storyEntriesOf(part: StoryEntriesPart, doc: PMNode, session: SessionStore): readonly StoryEntry[];
92
112
  /** One reason the part cannot take the changes its stories went through */
93
113
  export interface StoryPartProblem {
94
114
  readonly code: DocxExportErrorCode;
95
115
  readonly message: string;
116
+ readonly reason: ExportProblemReason;
96
117
  }
97
118
  /**
98
119
  * Why the part cannot take what the document did to its stories, or none when it can. The part the
@@ -19,6 +19,7 @@ import { sameSource } from "../schema/sourceEquality.js";
19
19
  import {
20
20
  asStoryKey,
21
21
  STORY_KINDS,
22
+ splitStoryKey,
22
23
  storiesOf,
23
24
  storyKey,
24
25
  storyNodeOf
@@ -40,9 +41,6 @@ function byId(a, b) {
40
41
  if (a === b) return 0;
41
42
  return a < b ? -1 : 1;
42
43
  }
43
- function storyIdOf(key, kind) {
44
- return key.slice(kind.length + 1);
45
- }
46
44
  function arrivedChange(doc, imported) {
47
45
  const current = storyNodeOf(doc, imported.key);
48
46
  if (current === null) return { change: "removed", imported };
@@ -53,7 +51,7 @@ function storyChangesOf(doc, session, kind) {
53
51
  const added = Object.keys(storiesOf(doc)).flatMap((text) => {
54
52
  const key = asStoryKey(text);
55
53
  return key === null || !key.startsWith(`${kind}:`) || session.stories.has(key) ? [] : [key];
56
- }).sort((a, b) => byId(storyIdOf(a, kind), storyIdOf(b, kind))).flatMap((key) => {
54
+ }).sort((a, b) => byId(splitStoryKey(a).id, splitStoryKey(b).id)).flatMap((key) => {
57
55
  const current = storyNodeOf(doc, key);
58
56
  return current === null ? [] : [{ change: "added", key, current }];
59
57
  });
@@ -91,6 +89,21 @@ function unwrittenStoryChanges(writings, doc, session) {
91
89
  });
92
90
  });
93
91
  }
92
+ function rewrittenStories(writings, doc, session) {
93
+ const byKind = new Map(writings.map((writing) => [writing.kind, writing]));
94
+ return STORY_KINDS.flatMap((kind) => {
95
+ const writing = byKind.get(kind);
96
+ if (writing === void 0) return [];
97
+ const frozen = writing.frozenEntries(session);
98
+ return storyChangesOf(doc, session, kind).flatMap(
99
+ (entry) => {
100
+ if (entry.change !== "added" && entry.change !== "edited") return [];
101
+ const key = entry.change === "added" ? entry.key : entry.imported.key;
102
+ return writing.changes.includes(entry.change) && !frozen.has(key) ? [{ key, story: entry.current }] : [];
103
+ }
104
+ );
105
+ });
106
+ }
94
107
  function writtenStoryOf(change, frozen) {
95
108
  if (change.change === "added") return change.current;
96
109
  if (change.change === "kept" || frozen.has(change.imported.key)) {
@@ -102,11 +115,13 @@ function writtenStories(changes, frozen) {
102
115
  return changes.flatMap((change) => {
103
116
  const story = writtenStoryOf(change, frozen);
104
117
  if (story === null) return [];
118
+ const key = change.change === "added" ? change.key : change.imported.key;
105
119
  return [
106
120
  {
107
121
  change,
122
+ key,
108
123
  story,
109
- frozen: change.change !== "added" && frozen.has(change.imported.key)
124
+ frozen: change.change !== "added" && frozen.has(key)
110
125
  }
111
126
  ];
112
127
  });
@@ -116,14 +131,17 @@ function storyEntriesOf(part, doc, session) {
116
131
  return changed(changes) ? writtenStories(changes, part.frozenEntries(session)) : [];
117
132
  }
118
133
  function addedIdProblems(part, changes) {
119
- return changes.flatMap(
120
- (change) => change.change === "added" && ST_DecimalNumber.parse(storyIdOf(change.key, part.kind)) === null ? [
134
+ return changes.flatMap((change) => {
135
+ if (change.change !== "added") return [];
136
+ const story = splitStoryKey(change.key);
137
+ return ST_DecimalNumber.parse(story.id) === null ? [
121
138
  {
122
139
  code: "unsupported-content",
123
- message: `the ${change.key} story is named by no whole number, and a ${wName(part.entry)} is identified by one`
140
+ message: `the ${change.key} story is named by no whole number, and a ${wName(part.entry)} is identified by one`,
141
+ reason: { kind: "story-id-not-a-number", story }
124
142
  }
125
- ] : []
126
- );
143
+ ] : [];
144
+ });
127
145
  }
128
146
  function storyEntriesProblems(part, doc, session) {
129
147
  const changes = storyChangesOf(doc, session, part.kind);
@@ -136,12 +154,19 @@ function storyEntriesProblems(part, doc, session) {
136
154
  );
137
155
  if (xml !== null) {
138
156
  const problem = partRootProblem(xml, part.root);
139
- return problem === null ? [] : [{ code: "malformed-xml", message: problem }];
157
+ return problem === null ? [] : [
158
+ {
159
+ code: "malformed-xml",
160
+ message: problem,
161
+ reason: { kind: "unwritable-part-root", part: part.name }
162
+ }
163
+ ];
140
164
  }
141
165
  return session.parts.has(CONTENT_TYPES_PATH) ? [] : [
142
166
  {
143
167
  code: "missing-content-types",
144
- message: `cannot add a part to a package that has no ${CONTENT_TYPES_PATH}`
168
+ message: `cannot add a part to a package that has no ${CONTENT_TYPES_PATH}`,
169
+ reason: { kind: "missing-content-types", part: part.name }
145
170
  }
146
171
  ];
147
172
  }
@@ -159,7 +184,7 @@ function entryXml(part, { change, frozen }, settled, refs) {
159
184
  return serializeStory(
160
185
  settled,
161
186
  null,
162
- containerOf(part, storyIdOf(change.key, part.kind)),
187
+ containerOf(part, splitStoryKey(change.key).id),
163
188
  refs
164
189
  );
165
190
  }
@@ -256,7 +281,7 @@ function planEntries(part, doc, session, context) {
256
281
  context.contentTypes.addOverride(path, part.contentType);
257
282
  const taken = /* @__PURE__ */ new Set([
258
283
  ...changes.map(
259
- (change) => change.change === "added" ? storyIdOf(change.key, part.kind) : change.imported.id
284
+ (change) => change.change === "added" ? splitStoryKey(change.key).id : change.imported.id
260
285
  ),
261
286
  ...part.referencedIds(doc)
262
287
  ]);
@@ -278,12 +303,12 @@ function storyEntriesPlanner(part) {
278
303
  }
279
304
  export {
280
305
  EVERY_STORY_CHANGE,
306
+ rewrittenStories,
281
307
  storyChangesOf,
282
308
  storyEntriesOf,
283
309
  storyEntriesPlanner,
284
310
  storyEntriesProblems,
285
311
  storyEntriesWriting,
286
- storyIdOf,
287
312
  storyWriting,
288
313
  unwrittenStoryChanges
289
314
  };
@@ -2,7 +2,7 @@
2
2
  * The inline wrappers this editor takes apart and puts back together, one entry each.
3
3
  *
4
4
  * A wrapper is an element holding inline content that goes back out around the same content: a
5
- * content control (`w:sdt`, §17.5.2.17), a hyperlink (`w:hyperlink`, §17.16.22). `EG_PContent`
5
+ * content control (`w:sdt`, §17.5.2.31), a hyperlink (`w:hyperlink`, §17.16.22). `EG_PContent`
6
6
  * lets them nest in any order and any depth, so import reads the nesting as the file wrote it and
7
7
  * export rebuilds it from the marks (`schema/wrappers` holds the order, `docx/importParagraph`
8
8
  * reads, `docx/serializeParagraph` writes).
@@ -41,12 +41,15 @@ export interface WrapperKind {
41
41
  open(mark: Mark, refs: ExportRefs): string;
42
42
  /** What closes it again */
43
43
  close(mark: Mark): string;
44
- /**
45
- * The same wrapper under a name of its own, for a second stretch claiming one name
46
- * (`docx/identities`). Absent for a kind that names nothing a document must keep unique.
47
- */
48
- copy?(mark: Mark): Mark;
49
44
  }
45
+ /**
46
+ * The next number for one kind of wrapper read out of this document.
47
+ *
48
+ * A block content control (`docx/importSdtBlock`) is not a mark, but it is a control, so it draws
49
+ * from the control count as the inline mark does: two of them then never answer `docx/identities`
50
+ * with one name.
51
+ */
52
+ export declare function nextKey(kind: string, el: Element): number;
50
53
  /**
51
54
  * Every wrapper the editor models, in the order the schema declares their marks: what stands
52
55
  * first here stands outside the others where two of them are written at one depth.
@@ -60,7 +63,7 @@ export declare function wrapperKindFor(el: Element): WrapperKind | undefined;
60
63
  * A kind whose mark excludes its own type records one of itself at a time, so the inner of two is
61
64
  * left whole where it stood rather than pushing the outer one off the content it wraps: that is a
62
65
  * `w:hyperlink` inside a `w:hyperlink`, which OOXML admits and Word does not write. A kind that
63
- * excludes nothing nests as deeply as the file does (`w:sdt`, §17.5.2.17).
66
+ * excludes nothing nests as deeply as the file does (`w:sdt`, §17.5.2.31).
64
67
  */
65
68
  export declare function wrapperFits(kind: WrapperKind, wrappers: readonly Mark[]): boolean;
66
69
  /** The kind this mark stands for. A wrapper mark nobody registered has no way back out */
@@ -1,14 +1,20 @@
1
1
  // src/docx/wrappers.ts
2
2
  import { DocxExportError } from "../ooxml/errors.js";
3
3
  import { docxSchema } from "../schema/index.js";
4
+ import { controlAttrs, OWN_CONTROL_ATTRS } from "../schema/controlAttrs.js";
4
5
  import { readHyperlinkWrapper, relIdIn, withRelId } from "./hyperlink.js";
5
- import { copiedControlPrefix, newControlId, readSdtWrapper } from "./sdt.js";
6
+ import {
7
+ controlFactsFrom,
8
+ readSdtWrapper,
9
+ SDT_CLOSING_XML,
10
+ sdtOpeningXml
11
+ } from "./sdt.js";
6
12
  var counts = /* @__PURE__ */ new WeakMap();
7
13
  function nextKey(kind, el) {
8
14
  const perDocument = counts.get(el.ownerDocument) ?? /* @__PURE__ */ new Map();
9
15
  counts.set(el.ownerDocument, perDocument);
10
- const key = perDocument.get(kind.mark) ?? 0;
11
- perDocument.set(kind.mark, key + 1);
16
+ const key = perDocument.get(kind) ?? 0;
17
+ perDocument.set(kind, key + 1);
12
18
  return key;
13
19
  }
14
20
  var SDT = {
@@ -19,34 +25,15 @@ var SDT = {
19
25
  if (!wrapper) return null;
20
26
  return {
21
27
  mark: docxSchema.marks.sdt.create({
22
- sdtPrefix: wrapper.prefix,
23
28
  depth,
24
- key: nextKey(SDT, el),
25
- contentsLocked: wrapper.contentsLocked,
26
- deletionLocked: wrapper.deletionLocked
29
+ key: nextKey(SDT.mark, el),
30
+ ...controlAttrs(OWN_CONTROL_ATTRS, controlFactsFrom(wrapper))
27
31
  }),
28
32
  content: wrapper.content
29
33
  };
30
34
  },
31
- open(mark) {
32
- const prefix = mark.attrs.sdtPrefix;
33
- if (typeof prefix !== "string") {
34
- throw new DocxExportError(
35
- "lost-original",
36
- "a content control has lost the opening XML it goes back out as"
37
- );
38
- }
39
- return `${prefix}<w:sdtContent>`;
40
- },
41
- close: () => "</w:sdtContent></w:sdt>",
42
- copy(mark) {
43
- const prefix = mark.attrs.sdtPrefix;
44
- if (typeof prefix !== "string") return mark;
45
- return mark.type.create({
46
- ...mark.attrs,
47
- sdtPrefix: copiedControlPrefix(prefix, newControlId())
48
- });
49
- }
35
+ open: (mark) => sdtOpeningXml(mark.attrs.sdtPrefix),
36
+ close: () => SDT_CLOSING_XML
50
37
  };
51
38
  var LINK = {
52
39
  mark: "link",
@@ -59,7 +46,7 @@ var LINK = {
59
46
  linkPrefix: wrapper.prefix,
60
47
  href: wrapper.relId === null ? null : sources.links.get(wrapper.relId) ?? null,
61
48
  depth,
62
- key: nextKey(LINK, el)
49
+ key: nextKey(LINK.mark, el)
63
50
  }),
64
51
  content: el
65
52
  };
@@ -111,6 +98,7 @@ function wrapperKindOf(mark) {
111
98
  }
112
99
  export {
113
100
  WRAPPER_KINDS,
101
+ nextKey,
114
102
  wrapperFits,
115
103
  wrapperKindFor,
116
104
  wrapperKindOf
@@ -0,0 +1,15 @@
1
+ /**
2
+ * What the editor does at the edge of a block-level content control, beyond the join the edge
3
+ * itself refuses (`schema/controlEdges`): a control left holding a single empty paragraph is
4
+ * removed whole, and a control holding nothing is passed over rather than taken away, the caret's
5
+ * own empty paragraph going as it would anywhere else (`spec/notes/contentControls.md`).
6
+ *
7
+ * The removal is one step covering the control from end to end, which is what `schema/locks` reads
8
+ * as the deletion clause's question, so a control locked against deletion refuses it.
9
+ */
10
+ import { type Command } from "prosemirror-state";
11
+ export declare const removeEmptyBlockControl: Command;
12
+ /** Backspace passing over the controls holding nothing that stand before the caret */
13
+ export declare const skipEmptyControlBefore: Command;
14
+ /** Delete passing over the controls holding nothing that stand after the caret */
15
+ export declare const skipEmptyControlAfter: Command;
@@ -0,0 +1,79 @@
1
+ // src/editor/blockControlEdits.ts
2
+ import { Selection, TextSelection } from "prosemirror-state";
3
+ import { docxSchema } from "../schema/index.js";
4
+ import { isBlockControl, isEmptyControl } from "../schema/controlAttrs.js";
5
+ import { guardedCommand } from "../schema/guards.js";
6
+ function soleEmptyControl($cursor) {
7
+ if ($cursor.parent.content.size > 0) return null;
8
+ let found = null;
9
+ for (let depth = $cursor.depth - 1; depth > 0; depth -= 1) {
10
+ const node = $cursor.node(depth);
11
+ if (node.childCount !== 1 || !isBlockControl(node)) break;
12
+ found = depth;
13
+ }
14
+ return found;
15
+ }
16
+ var removeEmptyBlockControl = guardedCommand((state) => {
17
+ const { selection } = state;
18
+ const $cursor = selection instanceof TextSelection ? selection.$cursor : null;
19
+ if (!$cursor) return null;
20
+ const depth = soleEmptyControl($cursor);
21
+ if (depth === null) return null;
22
+ const from = $cursor.before(depth);
23
+ const tr = state.tr.delete(from, $cursor.after(depth));
24
+ if (!tr.docChanged) return null;
25
+ return tr.setSelection(Selection.near(tr.doc.resolve(from), -1)).scrollIntoView();
26
+ });
27
+ function isEmptyParagraph(node) {
28
+ return node.type === docxSchema.nodes.paragraph && node.content.size === 0;
29
+ }
30
+ function caretAtEdge(selection, dir) {
31
+ const $cursor = selection instanceof TextSelection ? selection.$cursor : null;
32
+ if (!$cursor || $cursor.depth === 0) return null;
33
+ const at = dir < 0 ? 0 : $cursor.parent.content.size;
34
+ return $cursor.parentOffset === at ? $cursor : null;
35
+ }
36
+ function emptyRunBeside(selection, dir) {
37
+ const $cursor = caretAtEdge(selection, dir);
38
+ if (!$cursor) return null;
39
+ const depth = $cursor.depth;
40
+ const container = $cursor.node(depth - 1);
41
+ const first = $cursor.index(depth - 1) + (dir < 0 ? -1 : 1);
42
+ let pos = dir < 0 ? $cursor.before(depth) : $cursor.after(depth);
43
+ let index = first;
44
+ let sibling = container.maybeChild(index);
45
+ while (sibling && isEmptyControl(sibling)) {
46
+ pos += dir * sibling.nodeSize;
47
+ index += dir;
48
+ sibling = container.maybeChild(index);
49
+ }
50
+ if (index === first) return null;
51
+ return { beyond: sibling ? pos : null };
52
+ }
53
+ function skipEmptyControls(dir) {
54
+ const move = guardedCommand((state) => {
55
+ const beyond = emptyRunBeside(state.selection, dir)?.beyond;
56
+ if (beyond === null || beyond === void 0) return null;
57
+ const $cursor = state.selection instanceof TextSelection ? state.selection.$cursor : null;
58
+ if ($cursor && isEmptyParagraph($cursor.parent)) {
59
+ const tr = state.tr.delete($cursor.before(), $cursor.after());
60
+ if (tr.docChanged) {
61
+ const at = tr.doc.resolve(tr.mapping.map(beyond));
62
+ return tr.setSelection(Selection.near(at, dir)).scrollIntoView();
63
+ }
64
+ }
65
+ return state.tr.setSelection(Selection.near(state.doc.resolve(beyond), dir)).scrollIntoView();
66
+ });
67
+ return (state, dispatch, view) => (
68
+ // With nothing beyond the run there is nowhere to pass to, and the key still may not take the
69
+ // control away, so it is reported handled and the document is left as it stood.
70
+ move(state, dispatch, view) || emptyRunBeside(state.selection, dir) !== null
71
+ );
72
+ }
73
+ var skipEmptyControlBefore = skipEmptyControls(-1);
74
+ var skipEmptyControlAfter = skipEmptyControls(1);
75
+ export {
76
+ removeEmptyBlockControl,
77
+ skipEmptyControlAfter,
78
+ skipEmptyControlBefore
79
+ };
@@ -80,7 +80,7 @@ var dropSourceIdentity = (slice, { move }) => move ? slice : mapSliceNodes(slice
80
80
  if (node.type === docxSchema.nodes.rawBlock) {
81
81
  return node.attrs.xml === null ? null : withAttrs(node, { ...node.attrs, srcId: null });
82
82
  }
83
- if (node.type !== docxSchema.nodes.paragraph && node.type !== docxSchema.nodes.table) {
83
+ if (node.type !== docxSchema.nodes.paragraph && node.type !== docxSchema.nodes.table && node.type !== docxSchema.nodes.sdtBlock && node.type !== docxSchema.nodes.sdtEmpty) {
84
84
  return node;
85
85
  }
86
86
  return node.attrs.srcId === null ? node : withAttrs(node, { ...node.attrs, srcId: null });
@@ -1,11 +1,12 @@
1
1
  /** Document-level readers for whether the document as it stands can be written back. */
2
2
  import type { EditorState } from "prosemirror-state";
3
3
  import { type ExportProblem } from "../../docx/invariants";
4
- export type { ExportProblem } from "../../docx/invariants";
4
+ export type { ExportPartName, ExportProblem, ExportProblemReason, ExportProblemStory, ExportStoryKind, } from "../../docx/invariants";
5
5
  /**
6
6
  * Every reason writing the document back would be refused, in the order `exportDocx` would raise
7
- * them, each with the code and message the refusal would carry and the position in the document
8
- * where there is one. Empty when the file would be written.
7
+ * them, each with the code and message the refusal would carry, the `reason` naming what it is
8
+ * about, and the position in the document where there is one. Empty when the file would be
9
+ * written.
9
10
  *
10
11
  * A state built without an opened document has no file to write back into, so nothing about it
11
12
  * can be refused and it reports no problem.