@portone/docx-editor 0.6.2 → 0.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/CHANGELOG.md +62 -0
  2. package/dist/DocxEditor.js +2 -2
  3. package/dist/docx/headersFooters.js +4 -14
  4. package/dist/docx/identities.d.ts +8 -2
  5. package/dist/docx/identities.js +31 -11
  6. package/dist/docx/importPolicy.d.ts +2 -2
  7. package/dist/docx/importPolicy.js +12 -5
  8. package/dist/docx/importPreserved.d.ts +3 -3
  9. package/dist/docx/importSdtBlock.d.ts +24 -0
  10. package/dist/docx/importSdtBlock.js +42 -0
  11. package/dist/docx/importTable.js +35 -24
  12. package/dist/docx/notes/numbering.js +4 -4
  13. package/dist/docx/notes/references.d.ts +0 -2
  14. package/dist/docx/notes/references.js +2 -7
  15. package/dist/docx/sdt.d.ts +52 -9
  16. package/dist/docx/sdt.js +108 -26
  17. package/dist/docx/sections.d.ts +13 -13
  18. package/dist/docx/sections.js +34 -20
  19. package/dist/docx/serializeBlock.js +9 -1
  20. package/dist/docx/serializeSdtBlock.d.ts +24 -0
  21. package/dist/docx/serializeSdtBlock.js +15 -0
  22. package/dist/docx/serializeTable.d.ts +8 -2
  23. package/dist/docx/serializeTable.js +14 -20
  24. package/dist/docx/story.d.ts +10 -4
  25. package/dist/docx/story.js +25 -20
  26. package/dist/docx/storyBlocks.d.ts +32 -0
  27. package/dist/docx/storyBlocks.js +65 -0
  28. package/dist/docx/wrappers.d.ts +10 -7
  29. package/dist/docx/wrappers.js +15 -27
  30. package/dist/editor/blockControlEdits.d.ts +15 -0
  31. package/dist/editor/blockControlEdits.js +79 -0
  32. package/dist/editor/clipboard/normalizers.js +1 -1
  33. package/dist/editor/commands/lockCommands.d.ts +5 -1
  34. package/dist/editor/commands/lockCommands.js +27 -19
  35. package/dist/editor/createEditor.js +4 -0
  36. package/dist/editor/notes/noteSurface.js +4 -3
  37. package/dist/editor/plugins/controlLifecycle.d.ts +18 -0
  38. package/dist/editor/plugins/controlLifecycle.js +130 -0
  39. package/dist/editor/plugins/keymap.js +23 -5
  40. package/dist/editor/plugins/rowResize.js +2 -4
  41. package/dist/editor/stories/storyState.d.ts +2 -0
  42. package/dist/editor/stories/storyState.js +2 -0
  43. package/dist/page/blockKinds.d.ts +24 -2
  44. package/dist/page/blockKinds.js +6 -1
  45. package/dist/page/kinds/index.d.ts +5 -0
  46. package/dist/page/kinds/index.js +2 -0
  47. package/dist/page/kinds/sdtBlockKind.d.ts +14 -0
  48. package/dist/page/kinds/sdtBlockKind.js +143 -0
  49. package/dist/page/measureBlocks.js +4 -2
  50. package/dist/page/pageDecorations.js +3 -2
  51. package/dist/page/pageLayout.d.ts +16 -15
  52. package/dist/page/pageLayout.js +134 -67
  53. package/dist/page/usePageLayout.d.ts +2 -2
  54. package/dist/page/usePageLayout.js +5 -5
  55. package/dist/schema/attrRoles.js +45 -1
  56. package/dist/schema/clipboard.js +9 -0
  57. package/dist/schema/controlAttrs.d.ts +105 -0
  58. package/dist/schema/controlAttrs.js +129 -0
  59. package/dist/schema/controlEdges.d.ts +22 -0
  60. package/dist/schema/controlEdges.js +50 -0
  61. package/dist/schema/docxSchema.d.ts +1 -1
  62. package/dist/schema/docxSchema.js +180 -37
  63. package/dist/schema/editGuard.d.ts +1 -1
  64. package/dist/schema/guards.js +2 -0
  65. package/dist/schema/locks.d.ts +22 -6
  66. package/dist/schema/locks.js +88 -47
  67. package/dist/styles/classNames.d.ts +8 -0
  68. package/dist/styles/classNames.js +8 -0
  69. package/dist/styles.css +31 -2
  70. package/dist/table/merge.js +11 -3
  71. package/dist/table/rowResize.d.ts +9 -0
  72. package/dist/table/rowResize.js +9 -5
  73. package/dist/ui/TextMenu.js +2 -0
  74. package/package.json +3 -3
@@ -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
+ };
@@ -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 });
@@ -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,