@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
@@ -63,7 +63,7 @@ export { addComment, addCommentReply, canAddComment, canEditComment, documentCom
63
63
  * with the code `exportDocx` would throw it under. `canExport` is what an export control is
64
64
  * drawn from, and `downloadDocx` answers `blocked` with the same list.
65
65
  */
66
- export type { ExportProblem } from "./exportQueries";
66
+ export type { ExportPartName, ExportProblem, ExportProblemReason, ExportProblemStory, ExportStoryKind, } from "./exportQueries";
67
67
  export { canExport, documentExportProblems } from "./exportQueries";
68
68
  export type { FidelityCode, FidelityNote, FidelitySeverity, } from "./fidelityQueries";
69
69
  export { documentFidelity } from "./fidelityQueries";
@@ -14,8 +14,12 @@ import { type Command, type EditorState } from "prosemirror-state";
14
14
  * A control the document locked against deletion alone leaves its contents open, so it reads as
15
15
  * text to lock rather than as a lock in reach: lifting that one clause by itself is not a state
16
16
  * this answer can name. The guard still refuses taking such a control away.
17
+ *
18
+ * `shut` is what a `w:group` reads as: its contents refuse every edit on terms the editor cannot
19
+ * write or lift (`spec/notes/contentControls.md`), so there is neither text to lock nor a lock to
20
+ * take off. It is told apart from `none` so that a menu can say why it offers nothing.
17
21
  */
18
- export type SelectionLock = "none" | "lockable" | "locked" | "mixed";
22
+ export type SelectionLock = "none" | "shut" | "lockable" | "locked" | "mixed";
19
23
  /** What locking and unlocking would do where the selection stands */
20
24
  export declare function selectionLock(state: EditorState): SelectionLock;
21
25
  /**
@@ -5,11 +5,11 @@ import {
5
5
  import { namesNothing, newControlId } from "../../docx/sdt.js";
6
6
  import { lockedControlPrefix, withContentLock } from "../../docx/sdtProps.js";
7
7
  import { docxSchema } from "../../schema/index.js";
8
+ import { controlAttrsOf } from "../../schema/controlAttrs.js";
8
9
  import { guardedCommand, openStretches } from "../../schema/guards.js";
9
10
  import {
10
11
  carriesLock,
11
12
  controlSpans,
12
- isLockedCell,
13
13
  selectionShut,
14
14
  unlockAllowed
15
15
  } from "../../schema/locks.js";
@@ -78,6 +78,17 @@ function gapsIn(stretch, spans) {
78
78
  if (at < stretch.to) gaps.push({ from: at, to: stretch.to });
79
79
  return gaps;
80
80
  }
81
+ function unlockedContainerAttrs(node) {
82
+ const names = controlAttrsOf(node);
83
+ if (!names || node.attrs[names.contentsLocked] !== true) return null;
84
+ const prefix = prefixOf(node.attrs);
85
+ return {
86
+ ...node.attrs,
87
+ sdtPrefix: prefix === null ? null : withContentLock(prefix, false),
88
+ [names.contentsLocked]: false,
89
+ [names.deletionLocked]: false
90
+ };
91
+ }
81
92
  function takeover(block, met, stretch, open) {
82
93
  const only = met.length === 1 ? met[0] : null;
83
94
  const prefix = only && prefixOf(only.mark.attrs);
@@ -133,11 +144,12 @@ function selectionLockDetail(state) {
133
144
  const locking = !selection.empty && selection instanceof TextSelection;
134
145
  const edits = [];
135
146
  const spans = [];
136
- const cells = /* @__PURE__ */ new Map();
147
+ const containers = /* @__PURE__ */ new Map();
137
148
  for (const range of selection.ranges) {
138
149
  const reach = { from: range.$from.pos, to: range.$to.pos };
139
150
  state.doc.nodesBetween(reach.from, reach.to, (node, pos) => {
140
- if (isLockedCell(node)) cells.set(pos, node);
151
+ const opened = unlockedContainerAttrs(node);
152
+ if (opened) containers.set(pos, opened);
141
153
  if (!node.isTextblock) return true;
142
154
  const block = { node, start: pos + 1 };
143
155
  const controls = controlSpans(block);
@@ -157,11 +169,12 @@ function selectionLockDetail(state) {
157
169
  }
158
170
  const locked = {
159
171
  spans,
160
- cells: Array.from(cells, ([pos, node]) => ({ pos, node }))
172
+ containers: Array.from(containers, ([pos, attrs]) => ({ pos, attrs }))
161
173
  };
162
- const anyLocked = spans.length > 0 || locked.cells.length > 0;
174
+ const anyLocked = spans.length > 0 || locked.containers.length > 0;
163
175
  if (edits.length === 0) {
164
- return anyLocked ? { kind: "locked", ...locked } : { kind: "none" };
176
+ if (anyLocked) return { kind: "locked", ...locked };
177
+ return selectionShut(selection, state.doc) ? { kind: "shut" } : { kind: "none" };
165
178
  }
166
179
  return anyLocked ? { kind: "mixed", edits, ...locked } : { kind: "lockable", edits };
167
180
  }
@@ -174,17 +187,19 @@ function lockEditsOf(lock) {
174
187
  case "mixed":
175
188
  return lock.edits;
176
189
  case "none":
190
+ case "shut":
177
191
  case "locked":
178
192
  return [];
179
193
  }
180
194
  }
181
- var NOTHING_LOCKED = { spans: [], cells: [] };
195
+ var NOTHING_LOCKED = { spans: [], containers: [] };
182
196
  function lockedOf(lock) {
183
197
  switch (lock.kind) {
184
198
  case "locked":
185
199
  case "mixed":
186
200
  return lock;
187
201
  case "none":
202
+ case "shut":
188
203
  case "lockable":
189
204
  return NOTHING_LOCKED;
190
205
  }
@@ -199,25 +214,18 @@ var lockSelection = guardedCommand((state) => {
199
214
  }
200
215
  return tr;
201
216
  });
202
- function unlockCell(tr, cell) {
203
- const prefix = prefixOf(cell.node.attrs);
204
- tr.setNodeMarkup(cell.pos, null, {
205
- ...cell.node.attrs,
206
- sdtPrefix: prefix === null ? null : withContentLock(prefix, false),
207
- sdtContentsLocked: false,
208
- sdtDeletionLocked: false
209
- });
210
- }
211
217
  var unlockSelection = guardedCommand((state) => {
212
- const { spans, cells } = lockedOf(selectionLockDetail(state));
213
- if (spans.length === 0 && cells.length === 0) return null;
218
+ const { spans, containers } = lockedOf(selectionLockDetail(state));
219
+ if (spans.length === 0 && containers.length === 0) return null;
214
220
  const tr = state.tr.setMeta(unlockAllowed, true);
215
221
  for (const span of spans) {
216
222
  const opened = withLock(span.mark, false);
217
223
  tr.removeMark(span.from, span.to, span.mark);
218
224
  if (opened) tr.addMark(span.from, span.to, opened);
219
225
  }
220
- for (const cell of cells) unlockCell(tr, cell);
226
+ for (const container of containers) {
227
+ tr.setNodeMarkup(container.pos, null, container.attrs);
228
+ }
221
229
  return tr;
222
230
  });
223
231
  function selectionTouchesLocked(state) {
@@ -29,6 +29,7 @@ import { commentComposer } from "./plugins/commentComposer.js";
29
29
  import { commentDecorations } from "./plugins/commentDecorations.js";
30
30
  import { commentRestoration } from "./plugins/commentRestoration.js";
31
31
  import { compositionSelection } from "./plugins/compositionSelection.js";
32
+ import { controlLifecycle } from "./plugins/controlLifecycle.js";
32
33
  import {
33
34
  displayDerivation,
34
35
  withDerivedDisplay
@@ -117,6 +118,9 @@ function createEditorState(doc, options = {}) {
117
118
  // It stands ahead of the numbering, which then labels what it settled
118
119
  noteLifecycle(),
119
120
  noteNumbering(),
121
+ // A control that says it goes once its contents are edited goes here, after the edit that
122
+ // triggered it and in the same history entry (`plugins/controlLifecycle`)
123
+ controlLifecycle(),
120
124
  // What the notes under the page are, worked out from the document the same way
121
125
  noteProjection.plugin,
122
126
  noteNavigation(),
@@ -6,6 +6,7 @@ import {
6
6
  } from "prosemirror-model";
7
7
  import { Plugin, TextSelection } from "prosemirror-state";
8
8
  import { NOTE_NUMBER_ELEMENTS } from "../../docx/notes/newNote.js";
9
+ import { onlyStoryParagraph, storyParagraphs } from "../../docx/storyBlocks.js";
9
10
  import { docxSchema } from "../../schema/index.js";
10
11
  import { editShut, transactionAllowed } from "../../schema/guards.js";
11
12
  import { editsShut } from "../../schema/protectionState.js";
@@ -88,7 +89,7 @@ function returnToReference(main, key) {
88
89
  main.focus();
89
90
  }
90
91
  function leadingChipSize(story) {
91
- const first = story.firstChild?.firstChild ?? null;
92
+ const first = storyParagraphs(story).at(0)?.firstChild ?? null;
92
93
  return first !== null && isOwnMark(first) ? first.nodeSize : 0;
93
94
  }
94
95
  function ownMarkIn(story) {
@@ -175,8 +176,8 @@ function ownMarkFirst() {
175
176
  });
176
177
  }
177
178
  function holdsNothing(story) {
178
- const only = story.childCount === 1 ? story.firstChild : null;
179
- return only !== null && only.type === docxSchema.nodes.paragraph && only.content.size === leadingChipSize(story);
179
+ const only = onlyStoryParagraph(story);
180
+ return only !== null && only.content.size === leadingChipSize(story);
180
181
  }
181
182
  function deleteEmptyNote(main, key) {
182
183
  return (state, dispatch) => {
@@ -0,0 +1,18 @@
1
+ /**
2
+ * What a content control says about outliving an edit of its contents, appended to the edit that
3
+ * caused it (`spec/notes/contentControls.md`).
4
+ *
5
+ * Neither `w:temporary` nor `w:showingPlcHdr` can be answered by preserving the control's opening
6
+ * XML, so the first edit inside a control carrying one lifts the wrapper or rewrites that XML
7
+ * without the flag. Appending it is what puts it in the history beside the edit, so one undo takes
8
+ * the two back together.
9
+ */
10
+ import { Plugin } from "prosemirror-state";
11
+ /**
12
+ * Applies both properties after every change that reached inside a control carrying one.
13
+ *
14
+ * The appended transaction carries the pass that lets a wrapper be lifted at all
15
+ * (`schema/locks`): a control's edge and its lock both stand against the step that removes it,
16
+ * and this is the one change neither speaks to.
17
+ */
18
+ export declare function controlLifecycle(): Plugin;
@@ -0,0 +1,130 @@
1
+ // src/editor/plugins/controlLifecycle.ts
2
+ import { Slice } from "prosemirror-model";
3
+ import { Plugin } from "prosemirror-state";
4
+ import { Mapping, ReplaceAroundStep } from "prosemirror-transform";
5
+ import { editSdtPrefix } from "../../docx/sdt.js";
6
+ import {
7
+ controlAttrs,
8
+ controlAttrsOf,
9
+ controlFactsOf,
10
+ isBlockControl,
11
+ NO_CONTROL,
12
+ OWN_CONTROL_ATTRS
13
+ } from "../../schema/controlAttrs.js";
14
+ import { displayOnly } from "../../schema/displayDerivation.js";
15
+ import { controlLifted, controlSpans, historyReplay } from "../../schema/locks.js";
16
+ import { changesOnlyComments } from "../../schema/protection.js";
17
+ function rewritten(transactions) {
18
+ const whole = new Mapping();
19
+ for (const transaction of transactions)
20
+ whole.appendMapping(transaction.mapping);
21
+ const ranges = [];
22
+ let index = 0;
23
+ for (const transaction of transactions) {
24
+ for (const map of transaction.mapping.maps) {
25
+ const rest = whole.slice(index + 1);
26
+ map.forEach((_from, _to, start, end) => {
27
+ ranges.push({ from: rest.map(start, -1), to: rest.map(end, 1) });
28
+ });
29
+ index += 1;
30
+ }
31
+ }
32
+ return ranges;
33
+ }
34
+ function edited(ranges, from, to) {
35
+ return ranges.some((range) => range.from <= to && range.to >= from);
36
+ }
37
+ function withoutPlaceholderFlag(prefix) {
38
+ return prefix === null ? null : editSdtPrefix(prefix, [["showingPlcHdr", null]]);
39
+ }
40
+ function acts(control) {
41
+ return control.temporary || control.showingPlaceholder;
42
+ }
43
+ function lifts(control) {
44
+ return control.temporary && !control.deletionLocked;
45
+ }
46
+ function settleContainer(tr, node, pos, names, control) {
47
+ const at = tr.mapping.map(pos);
48
+ const end = tr.mapping.map(pos + node.nodeSize);
49
+ if (lifts(control)) {
50
+ if (isBlockControl(node)) {
51
+ tr.step(
52
+ new ReplaceAroundStep(at, end, at + 1, end - 1, Slice.empty, 0, true)
53
+ );
54
+ return;
55
+ }
56
+ tr.setNodeMarkup(at, void 0, {
57
+ ...node.attrs,
58
+ ...controlAttrs(names, NO_CONTROL)
59
+ });
60
+ return;
61
+ }
62
+ if (!control.showingPlaceholder) return;
63
+ const prefix = withoutPlaceholderFlag(control.prefix);
64
+ if (prefix === null) return;
65
+ tr.setNodeMarkup(at, void 0, {
66
+ ...node.attrs,
67
+ ...controlAttrs(names, { ...control, prefix, showingPlaceholder: false })
68
+ });
69
+ }
70
+ function settleMark(tr, span, control) {
71
+ const from = tr.mapping.map(span.from);
72
+ const to = tr.mapping.map(span.to);
73
+ if (lifts(control)) {
74
+ tr.removeMark(from, to, span.mark);
75
+ return;
76
+ }
77
+ if (!control.showingPlaceholder) return;
78
+ const prefix = withoutPlaceholderFlag(control.prefix);
79
+ if (prefix === null) return;
80
+ tr.removeMark(from, to, span.mark);
81
+ tr.addMark(
82
+ from,
83
+ to,
84
+ span.mark.type.create({
85
+ ...span.mark.attrs,
86
+ ...controlAttrs(OWN_CONTROL_ATTRS, {
87
+ ...control,
88
+ prefix,
89
+ showingPlaceholder: false
90
+ })
91
+ })
92
+ );
93
+ }
94
+ function settleDocument(tr, doc, ranges) {
95
+ doc.descendants((node, pos) => {
96
+ const names = node.content.size > 0 ? controlAttrsOf(node) : null;
97
+ if (names) {
98
+ const container = controlFactsOf(names, node.attrs);
99
+ if (acts(container) && edited(ranges, pos + 1, pos + node.nodeSize - 1)) {
100
+ settleContainer(tr, node, pos, names, container);
101
+ }
102
+ }
103
+ if (!node.isTextblock) return true;
104
+ for (const span of controlSpans({ node, start: pos + 1 })) {
105
+ const inline = controlFactsOf(OWN_CONTROL_ATTRS, span.mark.attrs);
106
+ if (!acts(inline)) continue;
107
+ if (edited(ranges, span.from, span.to)) settleMark(tr, span, inline);
108
+ }
109
+ return false;
110
+ });
111
+ }
112
+ var NOT_AN_EDIT = [historyReplay, displayOnly, controlLifted];
113
+ function anEdit(transaction) {
114
+ return transaction.docChanged && !NOT_AN_EDIT.some((pass) => transaction.getMeta(pass) === true);
115
+ }
116
+ function controlLifecycle() {
117
+ return new Plugin({
118
+ appendTransaction(transactions, oldState, newState) {
119
+ const changed = transactions.filter(anEdit);
120
+ if (changed.length === 0) return null;
121
+ const tr = newState.tr.setMeta(controlLifted, true);
122
+ settleDocument(tr, newState.doc, rewritten(changed));
123
+ if (!tr.docChanged) return null;
124
+ return changesOnlyComments(oldState.doc, newState.doc) ? null : tr;
125
+ }
126
+ });
127
+ }
128
+ export {
129
+ controlLifecycle
130
+ };
@@ -8,6 +8,12 @@ import { canSplit } from "prosemirror-transform";
8
8
  import { splitParagraphAttrs } from "../../docx/cloning.js";
9
9
  import { toParagraphFormat } from "../../model/format.js";
10
10
  import { docxSchema } from "../../schema/index.js";
11
+ import { isBlockControl } from "../../schema/controlAttrs.js";
12
+ import {
13
+ removeEmptyBlockControl,
14
+ skipEmptyControlAfter,
15
+ skipEmptyControlBefore
16
+ } from "../blockControlEdits.js";
11
17
  import { insertLineBreak, insertPageBreak } from "../commands/breakCommands.js";
12
18
  import {
13
19
  toggleBold,
@@ -67,11 +73,13 @@ function keptEveryAttr(paragraph, attrs) {
67
73
  }
68
74
  var preserveTableFollowingParagraph = (state) => {
69
75
  const { $from } = state.selection;
70
- if (!state.selection.empty || $from.depth !== 1 || $from.parent.type !== docxSchema.nodes.paragraph || $from.parent.content.size !== 0 || $from.parentOffset !== 0) {
76
+ if (!state.selection.empty || $from.parent.type !== docxSchema.nodes.paragraph || $from.parent.content.size !== 0 || $from.parentOffset !== 0) {
71
77
  return false;
72
78
  }
73
- const paragraphIndex = $from.index(0);
74
- return paragraphIndex > 0 && state.doc.child(paragraphIndex - 1).type.spec.tableRole === "table";
79
+ const container = $from.node(-1);
80
+ const holdsBlocks = container.type === docxSchema.nodes.doc || isBlockControl(container);
81
+ const paragraphIndex = $from.index(-1);
82
+ return holdsBlocks && paragraphIndex > 0 && container.child(paragraphIndex - 1).type.spec.tableRole === "table";
75
83
  };
76
84
  var historyKeymap = {
77
85
  "Mod-z": undo,
@@ -104,8 +112,18 @@ var docxKeymap = {
104
112
  "Shift-Enter": insertLineBreak,
105
113
  // Word and Google Docs both put a page break on this key, so it needs no learning
106
114
  "Mod-Enter": insertPageBreak,
107
- // The paragraph keeps adjacent tables separate without changing imported documents.
108
- Backspace: chainCommands(preserveTableFollowingParagraph, undoInputRule),
115
+ // `undoInputRule` comes first because it answers false unless the last transaction was an input
116
+ // rule, and the commands after it report the key handled wherever a control stands beside the
117
+ // caret. Then: a control holding nothing is passed over rather than taken away, a control holding
118
+ // one empty paragraph goes whole rather than being opened up, and the empty paragraph after a
119
+ // table keeps adjacent tables separate without changing imported documents.
120
+ Backspace: chainCommands(
121
+ undoInputRule,
122
+ skipEmptyControlBefore,
123
+ removeEmptyBlockControl,
124
+ preserveTableFollowingParagraph
125
+ ),
126
+ Delete: chainCommands(skipEmptyControlAfter, removeEmptyBlockControl),
109
127
  ...historyKeymap,
110
128
  "Mod-b": toggleBold,
111
129
  "Mod-i": toggleItalic,
@@ -2,11 +2,11 @@
2
2
  import { Plugin, PluginKey } from "prosemirror-state";
3
3
  import { TableMap } from "prosemirror-tables";
4
4
  import { Decoration, DecorationSet } from "prosemirror-view";
5
- import { isLockedCell } from "../../schema/locks.js";
6
5
  import { editorClassNames } from "../../styles/classNames.js";
7
6
  import { pageScaleAround } from "../../styles/visualScale.js";
8
7
  import {
9
8
  buildResizeRowTransaction,
9
+ isRowResizable,
10
10
  resizedRowHeight,
11
11
  rowPositionAt
12
12
  } from "../../table/rowResize.js";
@@ -45,9 +45,7 @@ function edgeUnder(view, event) {
45
45
  const spot = cellSpotAt(view, td);
46
46
  if (!spot) return null;
47
47
  const hit = rowEdgeAt(event.clientY, td.getBoundingClientRect(), spot);
48
- if (!hit || hit.row < 0 || hit.row >= spot.table.childCount) return null;
49
- const row = spot.table.child(hit.row);
50
- if (row.children.some(isLockedCell)) return null;
48
+ if (!hit || !isRowResizable(spot.table, hit.row)) return null;
51
49
  const rowPos = rowPositionAt(spot.tablePos, spot.table, hit.row);
52
50
  const rowDom = view.nodeDOM(rowPos);
53
51
  if (!(rowDom instanceof HTMLTableRowElement)) return null;
@@ -6,6 +6,8 @@
6
6
  * own, so the ones that answer for a whole document stay out - the history, which the main
7
7
  * document keeps for both (`./storyView`), the note and comment plugins, whose subjects a story
8
8
  * does not hold, and the page decorations, since a story is drawn wherever its host puts it.
9
+ * The control lifecycle is among the ones that stay: a story reaches the main state as a document
10
+ * attribute (`schema/stories`), so a control inside a footnote settles here or nowhere.
9
11
  *
10
12
  * The snapshot handed in is the main document's, which is what makes a run resolve here as it
11
13
  * would in the body. Its caller narrows the two values a story answers differently: a story has
@@ -7,6 +7,7 @@ import { tableEditing } from "prosemirror-tables";
7
7
  import { docxClipboard } from "../clipboard/plugin.js";
8
8
  import { editorDocument } from "../editorDocument.js";
9
9
  import { compositionSelection } from "../plugins/compositionSelection.js";
10
+ import { controlLifecycle } from "../plugins/controlLifecycle.js";
10
11
  import {
11
12
  displayDerivation,
12
13
  withDerivedDisplay
@@ -64,6 +65,7 @@ function storyEditorState({
64
65
  tabPointer(),
65
66
  tabLayout(),
66
67
  tabCaret(),
68
+ controlLifecycle(),
67
69
  displayDerivation(),
68
70
  numberingMarkers()
69
71
  ]
package/dist/index.d.ts CHANGED
@@ -16,12 +16,12 @@ export type { DocxEditorHandle, DocxEditorMode, DocxEditorProps, } from "./DocxE
16
16
  export { DocxEditor } from "./DocxEditor";
17
17
  export type { DocxBytes, DocxSource } from "./docx/importDocx";
18
18
  /** What the handle and a `blocked` download report: each reason the document cannot be written, under the code `DocxExportError` would throw it with */
19
- export type { ExportProblem } from "./docx/invariants";
19
+ export type { ExportPartName, ExportProblem, ExportProblemReason, ExportProblemStory, ExportStoryKind, } from "./docx/invariants";
20
20
  export type { DownloadDocxOptions, DownloadDocxResult, } from "./download";
21
21
  export { downloadDocx } from "./download";
22
22
  export type { CommentAuthor } from "./editor/commands/commentCommands";
23
23
  export type { CellFormat, CellVerticalAlign, DocumentDefaults, HighlightName, LineSpacing, NumberingRef, ParagraphAlign, ParagraphFormat, RowFormat, RowHeight, RunFormat, TableFormat, TableWidth, TableWidthType, UnderlineKind, VerticalAlign, } from "./model/format";
24
- export type { DocxExportErrorCode, DocxImportErrorCode, } from "./ooxml/errors";
24
+ export type { DocxExportErrorCode, DocxExportErrorOptions, DocxImportErrorCode, } from "./ooxml/errors";
25
25
  export { DocxExportError, DocxImportError } from "./ooxml/errors";
26
26
  export { docxSchema } from "./schema";
27
27
  /** Whose comments the panel offers to edit, which `DocxEditorMode` takes */
@@ -3,7 +3,9 @@
3
3
  *
4
4
  * Messages are English one-liners meant for developers. The `code` is the stable,
5
5
  * machine-readable part: consumers switch on it to show their own localized text,
6
- * so a code never changes meaning even when a message is reworded.
6
+ * so a code never changes meaning even when a message is reworded. An export refusal carries an
7
+ * `ExportProblemReason` besides, which tells the several situations one code covers apart and
8
+ * names what each is about, so a host can say which content to put back.
7
9
  */
8
10
  /**
9
11
  * Why a document could not be opened.
@@ -33,8 +35,139 @@ export declare class DocxImportError extends Error {
33
35
  * - `invalid-table`: the table grid is inconsistent, e.g. a vertical merge outliving its rows
34
36
  */
35
37
  export type DocxExportErrorCode = "missing-content-types" | "unsupported-content" | "lost-original" | "malformed-xml" | "invalid-table";
38
+ /**
39
+ * The kinds of side story a refusal can be about, which are the kinds a document holds beside its
40
+ * body (`schema/stories`). Spelled here because the reasons stand below the schema layer.
41
+ */
42
+ export type ExportStoryKind = "comment" | "footnote" | "endnote" | "header" | "footer";
43
+ /**
44
+ * One side story, as the document holds it.
45
+ *
46
+ * A reason names it wherever the content the refusal is about may stand outside the body, and the
47
+ * refusal is then about that story rather than the document: `story` is null for the body itself.
48
+ */
49
+ export interface ExportProblemStory {
50
+ readonly kind: ExportStoryKind;
51
+ /** A note's number, a comment's id, or the path of the part a header or footer stands in */
52
+ readonly id: string;
53
+ }
54
+ /** A part the export writes beside the body, named after what it holds */
55
+ export type ExportPartName = "media" | "numbering" | "comments" | "commentsExtended" | "people" | "footnotes" | "endnotes";
56
+ /**
57
+ * What a refusal is actually about.
58
+ *
59
+ * A `code` groups several situations - `unsupported-content` alone covers a list nothing defines,
60
+ * a block standing twice, and a change to a story no writer carries - and the `message` telling
61
+ * them apart is an English one-liner for a developer. This says which situation was met and names
62
+ * what it is about, so a host can write the sentence its own user reads. Switch on `kind`.
63
+ */
64
+ export type ExportProblemReason =
65
+ /** A preserved fragment holding bookmark markup could not be parsed (`malformed-xml`) */
66
+ {
67
+ readonly kind: "unreadable-preserved-xml";
68
+ readonly story: ExportProblemStory | null;
69
+ }
70
+ /** A bookmark marker carries no id to pair it by (`malformed-xml`) */
71
+ | {
72
+ readonly kind: "unnamed-bookmark";
73
+ readonly marker: "start" | "end";
74
+ readonly story: ExportProblemStory | null;
75
+ }
76
+ /**
77
+ * A bookmark marker whose partner is gone (`malformed-xml`): a `start` with no end after it, or
78
+ * an `end` with no start before it.
79
+ */
80
+ | {
81
+ readonly kind: "unmatched-bookmark";
82
+ readonly id: string;
83
+ readonly marker: "start" | "end";
84
+ readonly story: ExportProblemStory | null;
85
+ }
86
+ /** One bookmark id is started twice (`malformed-xml`) */
87
+ | {
88
+ readonly kind: "repeated-bookmark-start";
89
+ readonly id: string;
90
+ readonly story: ExportProblemStory | null;
91
+ }
92
+ /** A part cannot be rewritten around its root element (`malformed-xml`) */
93
+ | {
94
+ readonly kind: "unwritable-part-root";
95
+ readonly part: ExportPartName;
96
+ }
97
+ /** A cell's vertical merge covers rows its table does not have (`invalid-table`) */
98
+ | {
99
+ readonly kind: "vertical-merge-past-table";
100
+ readonly story: ExportProblemStory | null;
101
+ }
102
+ /** A node that is written from its original XML alone no longer holds it (`lost-original`) */
103
+ | {
104
+ readonly kind: "lost-preserved-xml";
105
+ readonly node: string;
106
+ readonly story: ExportProblemStory | null;
107
+ }
108
+ /** A placeholder pasted in from another opened document, whose XML this session never read (`lost-original`) */
109
+ | {
110
+ readonly kind: "preserved-from-another-document";
111
+ readonly node: string;
112
+ /** The session the placeholder was opened in */
113
+ readonly sessionId: string;
114
+ readonly story: ExportProblemStory | null;
115
+ }
116
+ /** One preserved block stands in two places, and it has one original XML to be written (`unsupported-content`) */
117
+ | {
118
+ readonly kind: "duplicate-preserved-block";
119
+ readonly node: string;
120
+ readonly story: ExportProblemStory | null;
121
+ }
122
+ /** A paragraph is in a list neither the file nor the editor's register defines (`unsupported-content`) */
123
+ | {
124
+ readonly kind: "undefined-list";
125
+ readonly numId: number;
126
+ readonly story: ExportProblemStory | null;
127
+ }
128
+ /** A story was changed and no part writer carries that change into the file (`unsupported-content`) */
129
+ | {
130
+ readonly kind: "unwritten-story-change";
131
+ readonly story: ExportProblemStory;
132
+ readonly change: "added" | "edited" | "removed";
133
+ }
134
+ /** A story was added under an id its part identifies entries by no whole number of (`unsupported-content`) */
135
+ | {
136
+ readonly kind: "story-id-not-a-number";
137
+ readonly story: ExportProblemStory;
138
+ }
139
+ /** A part the writer has to add cannot be declared, the package having no content types part (`missing-content-types`) */
140
+ | {
141
+ readonly kind: "missing-content-types";
142
+ readonly part: ExportPartName;
143
+ };
144
+ /** One reason the document cannot be written back, with the code `exportDocx` would throw it under */
145
+ export interface ExportProblem {
146
+ readonly code: DocxExportErrorCode;
147
+ /** An English one-liner for a developer; reworded freely. Read `code` and `reason` instead */
148
+ readonly message: string;
149
+ /** Which of the situations the code covers was met, and what it is about */
150
+ readonly reason: ExportProblemReason;
151
+ /**
152
+ * Where the problem stands in the document: the block or marker it is about, or, for a footnote
153
+ * or an endnote, the first reference to that note in the body, the note's own text standing
154
+ * nowhere there. Absent for a problem of the package, of the session, of a header, footer or
155
+ * comment story, and of a note the body refers to nowhere.
156
+ */
157
+ readonly pos?: number;
158
+ }
159
+ /** What a `DocxExportError` may be given beyond its code and message */
160
+ export interface DocxExportErrorOptions extends ErrorOptions {
161
+ /** The entry `exportProblems` reports for this refusal, where an invariant predicted it */
162
+ readonly problem?: ExportProblem;
163
+ }
36
164
  /** Thrown when an edited document cannot be written back out without losing or corrupting content */
37
165
  export declare class DocxExportError extends Error {
38
166
  readonly code: DocxExportErrorCode;
39
- constructor(code: DocxExportErrorCode, message: string, options?: ErrorOptions);
167
+ /**
168
+ * What `exportProblems` reports for this refusal, carrying its `reason` and `pos`. Absent for a
169
+ * refusal met while writing that no invariant predicted.
170
+ */
171
+ readonly problem?: ExportProblem;
172
+ constructor(code: DocxExportErrorCode, message: string, options?: DocxExportErrorOptions);
40
173
  }
@@ -9,10 +9,16 @@ var DocxImportError = class extends Error {
9
9
  };
10
10
  var DocxExportError = class extends Error {
11
11
  code;
12
+ /**
13
+ * What `exportProblems` reports for this refusal, carrying its `reason` and `pos`. Absent for a
14
+ * refusal met while writing that no invariant predicted.
15
+ */
16
+ problem;
12
17
  constructor(code, message, options) {
13
18
  super(message, options);
14
19
  this.name = "DocxExportError";
15
20
  this.code = code;
21
+ this.problem = options?.problem;
16
22
  }
17
23
  };
18
24
  export {