@portone/docx-editor 0.6.2 → 0.6.4

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 (107) hide show
  1. package/CHANGELOG.md +91 -0
  2. package/dist/DocxEditor.js +2 -2
  3. package/dist/docx/comments/writing.d.ts +8 -0
  4. package/dist/docx/comments/writing.js +9 -2
  5. package/dist/docx/headersFooters.d.ts +7 -0
  6. package/dist/docx/headersFooters.js +8 -15
  7. package/dist/docx/identities.d.ts +9 -2
  8. package/dist/docx/identities.js +45 -12
  9. package/dist/docx/importDocx.js +2 -0
  10. package/dist/docx/importParagraph.d.ts +4 -0
  11. package/dist/docx/importParagraph.js +2 -0
  12. package/dist/docx/importPolicy.d.ts +2 -2
  13. package/dist/docx/importPolicy.js +12 -5
  14. package/dist/docx/importPreserved.d.ts +3 -3
  15. package/dist/docx/importSdtBlock.d.ts +23 -0
  16. package/dist/docx/importSdtBlock.js +33 -0
  17. package/dist/docx/importTable.js +35 -24
  18. package/dist/docx/invariants.js +60 -3
  19. package/dist/docx/notes/numbering.js +4 -4
  20. package/dist/docx/notes/references.d.ts +0 -2
  21. package/dist/docx/notes/references.js +2 -7
  22. package/dist/docx/numberingPlanner.d.ts +3 -0
  23. package/dist/docx/numberingPlanner.js +8 -2
  24. package/dist/docx/packageParts.d.ts +9 -0
  25. package/dist/docx/packageParts.js +16 -0
  26. package/dist/docx/packagePrefixes.d.ts +12 -0
  27. package/dist/docx/packagePrefixes.js +18 -0
  28. package/dist/docx/partPlan.js +3 -5
  29. package/dist/docx/sdt.d.ts +79 -9
  30. package/dist/docx/sdt.js +121 -26
  31. package/dist/docx/sections.d.ts +13 -13
  32. package/dist/docx/sections.js +34 -20
  33. package/dist/docx/serializeBlock.js +10 -1
  34. package/dist/docx/serializeParagraph.d.ts +2 -1
  35. package/dist/docx/serializeParagraph.js +6 -1
  36. package/dist/docx/serializeSdtBlock.d.ts +16 -0
  37. package/dist/docx/serializeSdtBlock.js +11 -0
  38. package/dist/docx/serializeTable.d.ts +8 -2
  39. package/dist/docx/serializeTable.js +14 -20
  40. package/dist/docx/story.d.ts +10 -4
  41. package/dist/docx/story.js +25 -20
  42. package/dist/docx/storyBlocks.d.ts +32 -0
  43. package/dist/docx/storyBlocks.js +65 -0
  44. package/dist/docx/storyParts.d.ts +3 -0
  45. package/dist/docx/storyParts.js +4 -1
  46. package/dist/docx/wrappers.d.ts +33 -15
  47. package/dist/docx/wrappers.js +45 -26
  48. package/dist/editor/blockControlEdits.d.ts +15 -0
  49. package/dist/editor/blockControlEdits.js +79 -0
  50. package/dist/editor/clipboard/normalizers.js +1 -1
  51. package/dist/editor/commands/lockCommands.d.ts +5 -1
  52. package/dist/editor/commands/lockCommands.js +51 -23
  53. package/dist/editor/createEditor.js +4 -0
  54. package/dist/editor/inlineControlEdits.d.ts +20 -0
  55. package/dist/editor/inlineControlEdits.js +69 -0
  56. package/dist/editor/notes/noteSurface.js +4 -3
  57. package/dist/editor/plugins/controlLifecycle.d.ts +18 -0
  58. package/dist/editor/plugins/controlLifecycle.js +130 -0
  59. package/dist/editor/plugins/keymap.js +34 -5
  60. package/dist/editor/plugins/rowResize.js +2 -4
  61. package/dist/editor/stories/storyState.d.ts +2 -0
  62. package/dist/editor/stories/storyState.js +2 -0
  63. package/dist/ooxml/element.js +3 -2
  64. package/dist/ooxml/errors.d.ts +11 -0
  65. package/dist/ooxml/fragment.js +3 -2
  66. package/dist/ooxml/names.d.ts +4 -0
  67. package/dist/ooxml/names.js +8 -2
  68. package/dist/ooxml/partPrefixes.d.ts +21 -0
  69. package/dist/ooxml/partPrefixes.js +164 -0
  70. package/dist/ooxml/partSplice.d.ts +6 -1
  71. package/dist/ooxml/partSplice.js +30 -10
  72. package/dist/ooxml/tagScan.d.ts +24 -2
  73. package/dist/ooxml/tagScan.js +26 -7
  74. package/dist/ooxml/xml.d.ts +8 -0
  75. package/dist/ooxml/xml.js +27 -5
  76. package/dist/page/blockKinds.d.ts +24 -2
  77. package/dist/page/blockKinds.js +6 -1
  78. package/dist/page/kinds/index.d.ts +5 -0
  79. package/dist/page/kinds/index.js +2 -0
  80. package/dist/page/kinds/sdtBlockKind.d.ts +14 -0
  81. package/dist/page/kinds/sdtBlockKind.js +143 -0
  82. package/dist/page/measureBlocks.js +4 -2
  83. package/dist/page/pageDecorations.js +3 -2
  84. package/dist/page/pageLayout.d.ts +16 -15
  85. package/dist/page/pageLayout.js +134 -67
  86. package/dist/page/usePageLayout.d.ts +2 -2
  87. package/dist/page/usePageLayout.js +5 -5
  88. package/dist/schema/attrRoles.js +56 -1
  89. package/dist/schema/clipboard.js +11 -0
  90. package/dist/schema/controlAttrs.d.ts +119 -0
  91. package/dist/schema/controlAttrs.js +137 -0
  92. package/dist/schema/controlEdges.d.ts +22 -0
  93. package/dist/schema/controlEdges.js +50 -0
  94. package/dist/schema/docxSchema.d.ts +1 -1
  95. package/dist/schema/docxSchema.js +235 -37
  96. package/dist/schema/editGuard.d.ts +1 -1
  97. package/dist/schema/guards.js +2 -0
  98. package/dist/schema/locks.d.ts +56 -8
  99. package/dist/schema/locks.js +115 -56
  100. package/dist/styles/classNames.d.ts +10 -0
  101. package/dist/styles/classNames.js +10 -0
  102. package/dist/styles.css +44 -2
  103. package/dist/table/merge.js +11 -3
  104. package/dist/table/rowResize.d.ts +9 -0
  105. package/dist/table/rowResize.js +9 -5
  106. package/dist/ui/TextMenu.js +2 -0
  107. 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
+ };
@@ -7,6 +7,7 @@
7
7
  */
8
8
  import type { Node as PMNode } from "prosemirror-model";
9
9
  import type { DocxExportErrorCode, ExportPartName, ExportProblemReason } from "../ooxml/errors";
10
+ import { type RootDeclarations } from "../ooxml/partSplice";
10
11
  import { type StoryKey, type StoryKind } from "../schema/stories";
11
12
  import { type StoryToSettle } from "./identities";
12
13
  import type { PartPlanner } from "./partPlan";
@@ -121,6 +122,8 @@ export interface StoryPartProblem {
121
122
  * the content types part, which the export does not write from nothing.
122
123
  */
123
124
  export declare function storyEntriesProblems(part: StoryEntriesPart, doc: PMNode, session: SessionStore): readonly StoryPartProblem[];
125
+ /** What a rewritten or created part declares: every entry this writer puts out is spelled under `w` */
126
+ export declare const ENTRY_MARKUP: RootDeclarations;
124
127
  /**
125
128
  * A planner for a part holding one entry per story.
126
129
  *
@@ -170,7 +170,9 @@ function storyEntriesProblems(part, doc, session) {
170
170
  }
171
171
  ];
172
172
  }
173
- var ENTRY_MARKUP = { namespaces: { w: NAMESPACES.w } };
173
+ var ENTRY_MARKUP = {
174
+ namespaces: { w: NAMESPACES.w }
175
+ };
174
176
  var XML_DECLARATION = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
175
177
  function containerOf(part, id) {
176
178
  const name = wName(part.entry);
@@ -302,6 +304,7 @@ function storyEntriesPlanner(part) {
302
304
  };
303
305
  }
304
306
  export {
307
+ ENTRY_MARKUP,
305
308
  EVERY_STORY_CHANGE,
306
309
  rewrittenStories,
307
310
  storyChangesOf,
@@ -2,23 +2,25 @@
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).
9
9
  *
10
- * A kind knows three things and nothing about either walk: what a mark reads out of one element,
11
- * what opens it again, and what closes it. Tracked changes (`w:ins`, `w:del`) and simple fields
12
- * (`w:fldSimple`) are wrappers of the same shape. Registering one is an entry here and a mark spec
13
- * in `schema/docxSchema` carrying `WRAPPER_ATTRS`; its attrs also want a row in
14
- * `schema/attrRoles`, and two test lists name the kinds outright (`WRAPPERS_READS` in
15
- * `schema/attrClasses.test.ts`, the kind list in `schema/wrappers.test.ts`). Neither paragraph walk
16
- * and neither policy table has to be touched.
10
+ * A kind knows four things and nothing about either walk: what a mark reads out of one element,
11
+ * what one holding nothing opens as instead, what opens the element again, and what closes it. A
12
+ * wrapper holding nothing leaves a mark nothing to sit on, so the kind says whether it has a node
13
+ * of its own for that shape. Tracked changes (`w:ins`, `w:del`) and simple fields (`w:fldSimple`)
14
+ * are wrappers of the same shape. Registering one is an entry here and a mark spec in
15
+ * `schema/docxSchema` carrying `WRAPPER_ATTRS`; its attrs also want a row in `schema/attrRoles`,
16
+ * and two test lists name the kinds outright (`WRAPPERS_READS` in `schema/attrClasses.test.ts`,
17
+ * the kind list in `schema/wrappers.test.ts`). Neither paragraph walk and neither policy table has
18
+ * to be touched.
17
19
  *
18
20
  * The opening tag is the verbatim string the file wrote wherever there is one, so a wrapper nobody
19
21
  * edited goes back out byte for byte; only the closing tag is written from scratch.
20
22
  */
21
- import type { Mark } from "prosemirror-model";
23
+ import type { Mark, Node as PMNode } from "prosemirror-model";
22
24
  import type { ExportRefs } from "./exportRefs";
23
25
  import type { ImportSources } from "./importParagraph";
24
26
  /** A wrapper read off the file: the mark its content wears, and the element that content stands in */
@@ -37,16 +39,27 @@ export interface WrapperKind {
37
39
  * not write back ourselves, which leaves the element preserved whole where it stood.
38
40
  */
39
41
  read(el: Element, depth: number, sources: ImportSources): WrapperReading | null;
42
+ /**
43
+ * One of these the file wrote with nothing inside it as the inline node it stands as, wearing
44
+ * the `wrappers` it stands inside. null for an element holding something, and for a kind with no
45
+ * node of that shape, which leaves the wrapper preserved whole where it stood.
46
+ */
47
+ readEmpty(el: Element, wrappers: readonly Mark[]): PMNode | null;
48
+ /** That node back out as the wrapper it came from, and null for a node of another kind */
49
+ writeEmpty(node: PMNode): string | null;
40
50
  /** The opening XML this mark goes back out as */
41
51
  open(mark: Mark, refs: ExportRefs): string;
42
52
  /** What closes it again */
43
53
  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
54
  }
55
+ /**
56
+ * The next number for one kind of wrapper read out of this document.
57
+ *
58
+ * A block content control (`docx/importSdtBlock`) is not a mark, but it is a control, so it draws
59
+ * from the control count as the inline mark does: two of them then never answer `docx/identities`
60
+ * with one name.
61
+ */
62
+ export declare function nextKey(kind: string, el: Element): number;
50
63
  /**
51
64
  * Every wrapper the editor models, in the order the schema declares their marks: what stands
52
65
  * first here stands outside the others where two of them are written at one depth.
@@ -54,13 +67,18 @@ export interface WrapperKind {
54
67
  export declare const WRAPPER_KINDS: readonly WrapperKind[];
55
68
  /** The kind this element is one of, undefined for an element no kind reads */
56
69
  export declare function wrapperKindFor(el: Element): WrapperKind | undefined;
70
+ /**
71
+ * One inline node a wrapper holding nothing opened as, back out as that wrapper. null for every
72
+ * other inline node, which is what the paragraph writer goes on to render as a run.
73
+ */
74
+ export declare function emptyWrapperXml(node: PMNode): string | null;
57
75
  /**
58
76
  * Whether a wrapper of this kind may be read inside the wrappers already around it.
59
77
  *
60
78
  * A kind whose mark excludes its own type records one of itself at a time, so the inner of two is
61
79
  * left whole where it stood rather than pushing the outer one off the content it wraps: that is a
62
80
  * `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).
81
+ * excludes nothing nests as deeply as the file does (`w:sdt`, §17.5.2.31).
64
82
  */
65
83
  export declare function wrapperFits(kind: WrapperKind, wrappers: readonly Mark[]): boolean;
66
84
  /** The kind this mark stands for. A wrapper mark nobody registered has no way back out */
@@ -1,14 +1,26 @@
1
1
  // src/docx/wrappers.ts
2
2
  import { DocxExportError } from "../ooxml/errors.js";
3
3
  import { docxSchema } from "../schema/index.js";
4
+ import {
5
+ controlAttrs,
6
+ isEmptyInlineControl,
7
+ OWN_CONTROL_ATTRS
8
+ } from "../schema/controlAttrs.js";
4
9
  import { readHyperlinkWrapper, relIdIn, withRelId } from "./hyperlink.js";
5
- import { copiedControlPrefix, newControlId, readSdtWrapper } from "./sdt.js";
10
+ import {
11
+ controlFactsFrom,
12
+ readSdtContents,
13
+ readSdtWrapper,
14
+ SDT_CLOSING_XML,
15
+ sdtOpeningXml,
16
+ serializeEmptyControl
17
+ } from "./sdt.js";
6
18
  var counts = /* @__PURE__ */ new WeakMap();
7
19
  function nextKey(kind, el) {
8
20
  const perDocument = counts.get(el.ownerDocument) ?? /* @__PURE__ */ new Map();
9
21
  counts.set(el.ownerDocument, perDocument);
10
- const key = perDocument.get(kind.mark) ?? 0;
11
- perDocument.set(kind.mark, key + 1);
22
+ const key = perDocument.get(kind) ?? 0;
23
+ perDocument.set(kind, key + 1);
12
24
  return key;
13
25
  }
14
26
  var SDT = {
@@ -19,34 +31,28 @@ var SDT = {
19
31
  if (!wrapper) return null;
20
32
  return {
21
33
  mark: docxSchema.marks.sdt.create({
22
- sdtPrefix: wrapper.prefix,
23
34
  depth,
24
- key: nextKey(SDT, el),
25
- contentsLocked: wrapper.contentsLocked,
26
- deletionLocked: wrapper.deletionLocked
35
+ key: nextKey(SDT.mark, el),
36
+ ...controlAttrs(OWN_CONTROL_ATTRS, controlFactsFrom(wrapper))
27
37
  }),
28
38
  content: wrapper.content
29
39
  };
30
40
  },
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>`;
41
+ readEmpty(el, wrappers) {
42
+ const read = readSdtContents(el);
43
+ if (read?.kind !== "nothing") return null;
44
+ return docxSchema.nodes.sdtEmptyInline.create(
45
+ {
46
+ key: nextKey(SDT.mark, el),
47
+ ...controlAttrs(OWN_CONTROL_ATTRS, read.facts)
48
+ },
49
+ null,
50
+ wrappers
51
+ );
40
52
  },
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
- }
53
+ writeEmpty: (node) => isEmptyInlineControl(node) ? serializeEmptyControl(node) : null,
54
+ open: (mark) => sdtOpeningXml(mark.attrs.sdtPrefix),
55
+ close: () => SDT_CLOSING_XML
50
56
  };
51
57
  var LINK = {
52
58
  mark: "link",
@@ -59,11 +65,15 @@ var LINK = {
59
65
  linkPrefix: wrapper.prefix,
60
66
  href: wrapper.relId === null ? null : sources.links.get(wrapper.relId) ?? null,
61
67
  depth,
62
- key: nextKey(LINK, el)
68
+ key: nextKey(LINK.mark, el)
63
69
  }),
64
70
  content: el
65
71
  };
66
72
  },
73
+ // A link holding nothing is a link to nowhere the user could stand, and there is no node for it
74
+ // to open as: it stays the preserved element it always was, wearing the wrappers around it
75
+ readEmpty: () => null,
76
+ writeEmpty: () => null,
67
77
  open(mark, refs) {
68
78
  const prefix = mark.attrs.linkPrefix;
69
79
  const original = typeof prefix === "string" ? prefix : null;
@@ -94,6 +104,13 @@ var WRAPPER_KINDS = [SDT, LINK];
94
104
  function wrapperKindFor(el) {
95
105
  return WRAPPER_KINDS.find((kind) => kind.element === el.localName);
96
106
  }
107
+ function emptyWrapperXml(node) {
108
+ for (const kind of WRAPPER_KINDS) {
109
+ const xml = kind.writeEmpty(node);
110
+ if (xml !== null) return xml;
111
+ }
112
+ return null;
113
+ }
97
114
  function wrapperFits(kind, wrappers) {
98
115
  const type = docxSchema.marks[kind.mark];
99
116
  if (!type.excludes(type)) return true;
@@ -111,6 +128,8 @@ function wrapperKindOf(mark) {
111
128
  }
112
129
  export {
113
130
  WRAPPER_KINDS,
131
+ emptyWrapperXml,
132
+ nextKey,
114
133
  wrapperFits,
115
134
  wrapperKindFor,
116
135
  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 skipEmptyBlockControlBefore: Command;
14
+ /** Delete passing over the controls holding nothing that stand after the caret */
15
+ export declare const skipEmptyBlockControlAfter: 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, isEmptyBlockControl } 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 && isEmptyBlockControl(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 skipEmptyBlockControlBefore = skipEmptyControls(-1);
74
+ var skipEmptyBlockControlAfter = skipEmptyControls(1);
75
+ export {
76
+ removeEmptyBlockControl,
77
+ skipEmptyBlockControlAfter,
78
+ skipEmptyBlockControlBefore
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,16 @@ 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 {
9
+ controlAttrsOf,
10
+ isEmptyInlineControl
11
+ } from "../../schema/controlAttrs.js";
8
12
  import { guardedCommand, openStretches } from "../../schema/guards.js";
9
13
  import {
10
14
  carriesLock,
11
15
  controlSpans,
12
- isLockedCell,
16
+ coversWhole,
17
+ emptyControlsIn,
13
18
  selectionShut,
14
19
  unlockAllowed
15
20
  } from "../../schema/locks.js";
@@ -56,6 +61,11 @@ function stretchIn(block, reach) {
56
61
  return from < to ? { from, to } : null;
57
62
  }
58
63
  function wrapperDepthOver(block, stretch, replaced = null) {
64
+ const shared = sharedWrappers(childrenUnder(block, stretch));
65
+ const at = replaced === null ? -1 : shared.findIndex((wrapper) => wrapper.eq(replaced));
66
+ return innermostDepth(at === -1 ? shared : shared.slice(0, at)) + 1;
67
+ }
68
+ function childrenUnder(block, stretch) {
59
69
  const covered = [];
60
70
  block.node.forEach((child, offset) => {
61
71
  const from = block.start + offset;
@@ -63,9 +73,11 @@ function wrapperDepthOver(block, stretch, replaced = null) {
63
73
  covered.push(child);
64
74
  }
65
75
  });
66
- const shared = sharedWrappers(covered);
67
- const at = replaced === null ? -1 : shared.findIndex((wrapper) => wrapper.eq(replaced));
68
- return innermostDepth(at === -1 ? shared : shared.slice(0, at)) + 1;
76
+ return covered;
77
+ }
78
+ function nothingToLock(block, stretch) {
79
+ const covered = childrenUnder(block, stretch);
80
+ return covered.length > 0 && covered.every(isEmptyInlineControl);
69
81
  }
70
82
  function gapsIn(stretch, spans) {
71
83
  const gaps = [];
@@ -78,6 +90,17 @@ function gapsIn(stretch, spans) {
78
90
  if (at < stretch.to) gaps.push({ from: at, to: stretch.to });
79
91
  return gaps;
80
92
  }
93
+ function unlockedContainerAttrs(node) {
94
+ const names = controlAttrsOf(node);
95
+ if (!names || node.attrs[names.contentsLocked] !== true) return null;
96
+ const prefix = prefixOf(node.attrs);
97
+ return {
98
+ ...node.attrs,
99
+ sdtPrefix: prefix === null ? null : withContentLock(prefix, false),
100
+ [names.contentsLocked]: false,
101
+ [names.deletionLocked]: false
102
+ };
103
+ }
81
104
  function takeover(block, met, stretch, open) {
82
105
  const only = met.length === 1 ? met[0] : null;
83
106
  const prefix = only && prefixOf(only.mark.attrs);
@@ -117,7 +140,9 @@ function blockLockEdits(state, block, spans, reach) {
117
140
  return mark ? [{ from: span.from, to: span.to, mark, replaces: span.mark }] : [];
118
141
  }
119
142
  );
120
- const fresh = open(gapsIn(stretch, spans)).map((gap) => ({
143
+ const fresh = open(
144
+ gapsIn(stretch, spans).filter((gap) => !nothingToLock(block, gap))
145
+ ).map((gap) => ({
121
146
  ...gap,
122
147
  mark: lockedControlMark(newControlId(), wrapperDepthOver(block, gap))
123
148
  }));
@@ -133,13 +158,20 @@ function selectionLockDetail(state) {
133
158
  const locking = !selection.empty && selection instanceof TextSelection;
134
159
  const edits = [];
135
160
  const spans = [];
136
- const cells = /* @__PURE__ */ new Map();
161
+ const containers = /* @__PURE__ */ new Map();
137
162
  for (const range of selection.ranges) {
138
163
  const reach = { from: range.$from.pos, to: range.$to.pos };
139
164
  state.doc.nodesBetween(reach.from, reach.to, (node, pos) => {
140
- if (isLockedCell(node)) cells.set(pos, node);
165
+ const opened = unlockedContainerAttrs(node);
166
+ if (opened) containers.set(pos, opened);
141
167
  if (!node.isTextblock) return true;
142
168
  const block = { node, start: pos + 1 };
169
+ for (const control of emptyControlsIn(block)) {
170
+ const lifted = unlockedContainerAttrs(control.node);
171
+ if (lifted && coversWhole(reach, control.span)) {
172
+ containers.set(control.span.from, lifted);
173
+ }
174
+ }
143
175
  const controls = controlSpans(block);
144
176
  spans.push(
145
177
  ...liftable(
@@ -157,11 +189,12 @@ function selectionLockDetail(state) {
157
189
  }
158
190
  const locked = {
159
191
  spans,
160
- cells: Array.from(cells, ([pos, node]) => ({ pos, node }))
192
+ containers: Array.from(containers, ([pos, attrs]) => ({ pos, attrs }))
161
193
  };
162
- const anyLocked = spans.length > 0 || locked.cells.length > 0;
194
+ const anyLocked = spans.length > 0 || locked.containers.length > 0;
163
195
  if (edits.length === 0) {
164
- return anyLocked ? { kind: "locked", ...locked } : { kind: "none" };
196
+ if (anyLocked) return { kind: "locked", ...locked };
197
+ return selectionShut(selection, state.doc) ? { kind: "shut" } : { kind: "none" };
165
198
  }
166
199
  return anyLocked ? { kind: "mixed", edits, ...locked } : { kind: "lockable", edits };
167
200
  }
@@ -174,17 +207,19 @@ function lockEditsOf(lock) {
174
207
  case "mixed":
175
208
  return lock.edits;
176
209
  case "none":
210
+ case "shut":
177
211
  case "locked":
178
212
  return [];
179
213
  }
180
214
  }
181
- var NOTHING_LOCKED = { spans: [], cells: [] };
215
+ var NOTHING_LOCKED = { spans: [], containers: [] };
182
216
  function lockedOf(lock) {
183
217
  switch (lock.kind) {
184
218
  case "locked":
185
219
  case "mixed":
186
220
  return lock;
187
221
  case "none":
222
+ case "shut":
188
223
  case "lockable":
189
224
  return NOTHING_LOCKED;
190
225
  }
@@ -199,25 +234,18 @@ var lockSelection = guardedCommand((state) => {
199
234
  }
200
235
  return tr;
201
236
  });
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
237
  var unlockSelection = guardedCommand((state) => {
212
- const { spans, cells } = lockedOf(selectionLockDetail(state));
213
- if (spans.length === 0 && cells.length === 0) return null;
238
+ const { spans, containers } = lockedOf(selectionLockDetail(state));
239
+ if (spans.length === 0 && containers.length === 0) return null;
214
240
  const tr = state.tr.setMeta(unlockAllowed, true);
215
241
  for (const span of spans) {
216
242
  const opened = withLock(span.mark, false);
217
243
  tr.removeMark(span.from, span.to, span.mark);
218
244
  if (opened) tr.addMark(span.from, span.to, opened);
219
245
  }
220
- for (const cell of cells) unlockCell(tr, cell);
246
+ for (const container of containers) {
247
+ tr.setNodeMarkup(container.pos, null, container.attrs);
248
+ }
221
249
  return tr;
222
250
  });
223
251
  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(),
@@ -0,0 +1,20 @@
1
+ /**
2
+ * What the editor does beside a content control the file wrote inside a paragraph with nothing in
3
+ * it (`docx/wrappers`): Backspace and Delete take away what stands on the far side of it, as if the
4
+ * control were not there (`spec/notes/contentControls.md`).
5
+ *
6
+ * Such a control draws nothing and takes no width, so a key that took it away would take a phrase
7
+ * the file keeps with nothing on screen to show what went, and a key that merely passed the caret
8
+ * over it would be a keystroke the user sees nothing at all come of. So the two are one keystroke:
9
+ * the caret passes the whole run of them and the same key takes the character beyond. The control
10
+ * itself goes when a stretch covering it is taken away, which is the deletion clause's question
11
+ * (`schema/locks`).
12
+ *
13
+ * Between blocks the rule is `./blockControlEdits`'s, where passing the caret to another line is
14
+ * something the user can see and the key stops there.
15
+ */
16
+ import { type Command } from "prosemirror-state";
17
+ /** Backspace beside the controls holding nothing that stand before the caret */
18
+ export declare const deleteBeyondEmptyControlsBefore: Command;
19
+ /** Delete beside the controls holding nothing that stand after the caret */
20
+ export declare const deleteBeyondEmptyControlsAfter: Command;