@portone/docx-editor 0.6.3 → 0.6.5

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 (79) hide show
  1. package/CHANGELOG.md +62 -0
  2. package/dist/DocxEditor.d.ts +11 -0
  3. package/dist/DocxEditor.js +7 -3
  4. package/dist/docx/comments/writing.d.ts +8 -0
  5. package/dist/docx/comments/writing.js +9 -2
  6. package/dist/docx/headersFooters.d.ts +7 -0
  7. package/dist/docx/headersFooters.js +4 -1
  8. package/dist/docx/identities.d.ts +4 -3
  9. package/dist/docx/identities.js +14 -1
  10. package/dist/docx/importDocx.js +2 -0
  11. package/dist/docx/importParagraph.d.ts +4 -0
  12. package/dist/docx/importParagraph.js +2 -0
  13. package/dist/docx/importSdtBlock.d.ts +2 -3
  14. package/dist/docx/importSdtBlock.js +7 -16
  15. package/dist/docx/invariants.js +60 -3
  16. package/dist/docx/numberingPlanner.d.ts +3 -0
  17. package/dist/docx/numberingPlanner.js +8 -2
  18. package/dist/docx/packageParts.d.ts +9 -0
  19. package/dist/docx/packageParts.js +16 -0
  20. package/dist/docx/packagePrefixes.d.ts +12 -0
  21. package/dist/docx/packagePrefixes.js +18 -0
  22. package/dist/docx/partPlan.js +3 -5
  23. package/dist/docx/sdt.d.ts +27 -0
  24. package/dist/docx/sdt.js +14 -1
  25. package/dist/docx/serializeBlock.js +4 -3
  26. package/dist/docx/serializeParagraph.d.ts +2 -1
  27. package/dist/docx/serializeParagraph.js +6 -1
  28. package/dist/docx/serializeSdtBlock.d.ts +0 -8
  29. package/dist/docx/serializeSdtBlock.js +1 -5
  30. package/dist/docx/storyParts.d.ts +3 -0
  31. package/dist/docx/storyParts.js +4 -1
  32. package/dist/docx/wrappers.d.ts +23 -8
  33. package/dist/docx/wrappers.js +33 -2
  34. package/dist/editor/blockControlEdits.d.ts +2 -2
  35. package/dist/editor/blockControlEdits.js +6 -6
  36. package/dist/editor/commands/lockCommands.js +25 -5
  37. package/dist/editor/createEditor.d.ts +4 -1
  38. package/dist/editor/createEditor.js +14 -2
  39. package/dist/editor/editRefusal.d.ts +68 -0
  40. package/dist/editor/editRefusal.js +177 -0
  41. package/dist/editor/inlineControlEdits.d.ts +20 -0
  42. package/dist/editor/inlineControlEdits.js +69 -0
  43. package/dist/editor/plugins/controlContents.d.ts +24 -0
  44. package/dist/editor/plugins/controlContents.js +260 -0
  45. package/dist/editor/plugins/controlLifecycle.js +3 -5
  46. package/dist/editor/plugins/keymap.js +18 -7
  47. package/dist/editor/stories/storyState.js +2 -0
  48. package/dist/index.d.ts +2 -0
  49. package/dist/ooxml/element.js +3 -2
  50. package/dist/ooxml/errors.d.ts +11 -0
  51. package/dist/ooxml/fragment.js +3 -2
  52. package/dist/ooxml/names.d.ts +4 -0
  53. package/dist/ooxml/names.js +8 -2
  54. package/dist/ooxml/partPrefixes.d.ts +21 -0
  55. package/dist/ooxml/partPrefixes.js +164 -0
  56. package/dist/ooxml/partSplice.d.ts +6 -1
  57. package/dist/ooxml/partSplice.js +30 -10
  58. package/dist/ooxml/tagScan.d.ts +24 -2
  59. package/dist/ooxml/tagScan.js +26 -7
  60. package/dist/ooxml/xml.d.ts +8 -0
  61. package/dist/ooxml/xml.js +27 -5
  62. package/dist/schema/attrRoles.js +11 -0
  63. package/dist/schema/clipboard.js +2 -0
  64. package/dist/schema/controlAttrs.d.ts +33 -13
  65. package/dist/schema/controlAttrs.js +18 -6
  66. package/dist/schema/docxSchema.d.ts +1 -1
  67. package/dist/schema/docxSchema.js +60 -5
  68. package/dist/schema/guards.d.ts +8 -2
  69. package/dist/schema/guards.js +14 -8
  70. package/dist/schema/locks.d.ts +62 -7
  71. package/dist/schema/locks.js +99 -25
  72. package/dist/styles/classNames.d.ts +4 -0
  73. package/dist/styles/classNames.js +4 -0
  74. package/dist/styles.css +78 -14
  75. package/dist/ui/MenuNote.d.ts +12 -0
  76. package/dist/ui/MenuNote.js +14 -0
  77. package/dist/ui/TableMenu.js +38 -19
  78. package/dist/ui/TextMenu.js +31 -25
  79. package/package.json +2 -2
@@ -12,6 +12,7 @@
12
12
  * runs inside a paragraph (`docx/importParagraph`) each read the content their own way, and the
13
13
  * wrapper they put back on export is the same string in all of them.
14
14
  */
15
+ import type { Node as PMNode } from "prosemirror-model";
15
16
  import type { ControlFacts, ControlFlags } from "../schema/controlAttrs";
16
17
  /**
17
18
  * What a control's `w:sdtPr` states, which is all the editor reads off a control besides the XML
@@ -46,6 +47,24 @@ export declare function readEmptySdt(el: Element): ControlFacts | null;
46
47
  * wrapped row cannot come to disagree about what one and the same `w:sdtPr` says.
47
48
  */
48
49
  export declare function controlFactsFrom(wrapper: SdtWrapper): ControlFacts;
50
+ /**
51
+ * What one `w:sdt` holds, which is the question the block reader and the inline reader must
52
+ * answer the same way.
53
+ *
54
+ * A control holding nothing takes two shapes - an empty `w:sdtContent`, and no content element at
55
+ * all, which §17.5.2.34 admits because that element is a cache of what stood inside rather than
56
+ * the statement itself - and the two state the same thing. They are told apart here rather than in
57
+ * each reader, so neither level can come to read one of them as content and the other as nothing.
58
+ */
59
+ export type SdtContents = {
60
+ kind: "nothing";
61
+ facts: ControlFacts;
62
+ } | {
63
+ kind: "content";
64
+ wrapper: SdtWrapper;
65
+ };
66
+ /** What this control holds, and null for a shape this editor does not write back */
67
+ export declare function readSdtContents(el: Element): SdtContents | null;
49
68
  /**
50
69
  * Whether this control's content may be read as a container of blocks, which is read off the
51
70
  * `w:sdtPr` alone so that a control kept whole never has its prefix serialized.
@@ -66,6 +85,14 @@ export declare const SDT_CLOSING_XML = "</w:sdtContent></w:sdt>";
66
85
  export declare function sdtOpeningXml(prefix: unknown): string;
67
86
  /** One whole control: the opening it arrived with, around content this writer owns */
68
87
  export declare function sdtXml(prefix: unknown, content: string): string;
88
+ /**
89
+ * A control holding nothing, wherever it stood: the opening it arrived with around a content tag
90
+ * with nothing in it.
91
+ *
92
+ * A control that arrived writing no content element at all gains one here: §17.5.2.34 makes that
93
+ * element a cache of what stood inside, so the two shapes state the same thing.
94
+ */
95
+ export declare function serializeEmptyControl(node: PMNode): string;
69
96
  /**
70
97
  * The number Word writes on every control. Nothing reads it back and it only has to differ
71
98
  * from the other controls in the document, so a draw out of the whole 32 bit range is enough.
package/dist/docx/sdt.js CHANGED
@@ -79,6 +79,14 @@ function controlFactsFrom(wrapper) {
79
79
  showingPlaceholder: wrapper.showingPlaceholder
80
80
  };
81
81
  }
82
+ function readSdtContents(el) {
83
+ const wrapper = readSdtWrapper(el);
84
+ if (!wrapper) {
85
+ const facts = readEmptySdt(el);
86
+ return facts === null ? null : { kind: "nothing", facts };
87
+ }
88
+ return elementChildren(wrapper.content).length === 0 ? { kind: "nothing", facts: controlFactsFrom(wrapper) } : { kind: "content", wrapper };
89
+ }
82
90
  var RESTRAINED_TYPES = [
83
91
  `{${W_NS}}text`,
84
92
  `{${W_NS}}picture`,
@@ -109,6 +117,9 @@ function sdtOpeningXml(prefix) {
109
117
  function sdtXml(prefix, content) {
110
118
  return sdtOpeningXml(prefix) + content + SDT_CLOSING_XML;
111
119
  }
120
+ function serializeEmptyControl(node) {
121
+ return sdtXml(node.attrs.sdtPrefix, "");
122
+ }
112
123
  function newControlId() {
113
124
  return Math.floor(Math.random() * 2147483647);
114
125
  }
@@ -157,7 +168,9 @@ export {
157
168
  namesNothing,
158
169
  newControlId,
159
170
  readEmptySdt,
171
+ readSdtContents,
160
172
  readSdtWrapper,
161
173
  sdtOpeningXml,
162
- sdtXml
174
+ sdtXml,
175
+ serializeEmptyControl
163
176
  };
@@ -1,11 +1,12 @@
1
1
  // src/docx/serializeBlock.ts
2
2
  import { DocxExportError } from "../ooxml/errors.js";
3
- import { isBlockControl, isEmptyControl } from "../schema/controlAttrs.js";
3
+ import { isBlockControl, isEmptyBlockControl } from "../schema/controlAttrs.js";
4
4
  import { sameSource } from "../schema/sourceEquality.js";
5
5
  import { NO_EXPORT_REFS } from "./exportRefs.js";
6
+ import { serializeEmptyControl } from "./sdt.js";
6
7
  import { serializeParagraph } from "./serializeParagraph.js";
7
8
  import { serializePreservedBlock } from "./serializePreserved.js";
8
- import { serializeSdtBlock, serializeSdtEmpty } from "./serializeSdtBlock.js";
9
+ import { serializeSdtBlock } from "./serializeSdtBlock.js";
9
10
  import { serializeTable } from "./serializeTable.js";
10
11
  import { originalBlock } from "./session.js";
11
12
  function serializeBlock(node, refs = NO_EXPORT_REFS) {
@@ -16,7 +17,7 @@ function serializeBlock(node, refs = NO_EXPORT_REFS) {
16
17
  if (isBlockControl(node)) {
17
18
  return serializeSdtBlock(node, refs, serializeBlock);
18
19
  }
19
- if (isEmptyControl(node)) return serializeSdtEmpty(node);
20
+ if (isEmptyBlockControl(node)) return serializeEmptyControl(node);
20
21
  if (node.type.isInGroup("preserved")) {
21
22
  return serializePreservedBlock(node, refs);
22
23
  }
@@ -5,7 +5,8 @@
5
5
  *
6
6
  * A fragment the reader kept whole goes back where it stood: one kept inside a run is written as
7
7
  * a piece of that run, and one kept beside the runs is written between them, which is what the
8
- * two content models admit.
8
+ * two content models admit. A content control holding nothing is written between them for the
9
+ * same reason.
9
10
  *
10
11
  * The inlines are grouped from the outside in: neighbours sharing the outermost wrapper each stands
11
12
  * inside go back into it, that grouping is made again one wrapper deeper, and at the bottom the
@@ -15,7 +15,7 @@ import { wName } from "../ooxml/names.js";
15
15
  import { escapeXml } from "../ooxml/xml.js";
16
16
  import { wrapperMarks } from "../schema/wrappers.js";
17
17
  import { NO_EXPORT_REFS } from "./exportRefs.js";
18
- import { wrapperKindOf } from "./wrappers.js";
18
+ import { emptyWrapperXml, wrapperKindOf } from "./wrappers.js";
19
19
  function sameMark(a, b) {
20
20
  if (a === null || b === null) return a === b;
21
21
  return a.eq(b);
@@ -118,6 +118,11 @@ function addInline(parts, child, images) {
118
118
  parts.push({ kind: "raw", xml: preservedXml(child) });
119
119
  return;
120
120
  }
121
+ const empty = emptyWrapperXml(child);
122
+ if (empty !== null) {
123
+ parts.push({ kind: "raw", xml: empty });
124
+ return;
125
+ }
121
126
  if (child.type.name === "commentStart" || child.type.name === "commentEnd") {
122
127
  const original = child.attrs.xml;
123
128
  if (typeof original === "string") {
@@ -14,11 +14,3 @@ import type { ExportRefs } from "./exportRefs";
14
14
  /** How the level around a control writes one block of it */
15
15
  export type BlockWriter = (block: PMNode, refs: ExportRefs) => string;
16
16
  export declare function serializeSdtBlock(node: PMNode, refs: ExportRefs, writeBlock: BlockWriter): string;
17
- /**
18
- * A control holding nothing, which is the same opening around a content tag with nothing in it.
19
- *
20
- * A control that arrived writing no content element at all gains one here. The two shapes state
21
- * the same thing - §17.5.2.34 makes the element a cache of what stood inside - and only a control
22
- * something rewrote reaches this at all; one nobody touched goes out as the bytes it arrived as.
23
- */
24
- export declare function serializeSdtEmpty(node: PMNode): string;
@@ -6,10 +6,6 @@ function serializeSdtBlock(node, refs, writeBlock) {
6
6
  node.children.map((block) => writeBlock(block, refs)).join("")
7
7
  );
8
8
  }
9
- function serializeSdtEmpty(node) {
10
- return sdtXml(node.attrs.sdtPrefix, "");
11
- }
12
9
  export {
13
- serializeSdtBlock,
14
- serializeSdtEmpty
10
+ serializeSdtBlock
15
11
  };
@@ -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,
@@ -7,18 +7,20 @@
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,6 +39,14 @@ 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 */
@@ -57,6 +67,11 @@ export declare function nextKey(kind: string, el: Element): number;
57
67
  export declare const WRAPPER_KINDS: readonly WrapperKind[];
58
68
  /** The kind this element is one of, undefined for an element no kind reads */
59
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;
60
75
  /**
61
76
  * Whether a wrapper of this kind may be read inside the wrappers already around it.
62
77
  *
@@ -1,13 +1,19 @@
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
+ import {
5
+ controlAttrs,
6
+ isEmptyInlineControl,
7
+ OWN_CONTROL_ATTRS
8
+ } from "../schema/controlAttrs.js";
5
9
  import { readHyperlinkWrapper, relIdIn, withRelId } from "./hyperlink.js";
6
10
  import {
7
11
  controlFactsFrom,
12
+ readSdtContents,
8
13
  readSdtWrapper,
9
14
  SDT_CLOSING_XML,
10
- sdtOpeningXml
15
+ sdtOpeningXml,
16
+ serializeEmptyControl
11
17
  } from "./sdt.js";
12
18
  var counts = /* @__PURE__ */ new WeakMap();
13
19
  function nextKey(kind, el) {
@@ -32,6 +38,19 @@ var SDT = {
32
38
  content: wrapper.content
33
39
  };
34
40
  },
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
+ );
52
+ },
53
+ writeEmpty: (node) => isEmptyInlineControl(node) ? serializeEmptyControl(node) : null,
35
54
  open: (mark) => sdtOpeningXml(mark.attrs.sdtPrefix),
36
55
  close: () => SDT_CLOSING_XML
37
56
  };
@@ -51,6 +70,10 @@ var LINK = {
51
70
  content: el
52
71
  };
53
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,
54
77
  open(mark, refs) {
55
78
  const prefix = mark.attrs.linkPrefix;
56
79
  const original = typeof prefix === "string" ? prefix : null;
@@ -81,6 +104,13 @@ var WRAPPER_KINDS = [SDT, LINK];
81
104
  function wrapperKindFor(el) {
82
105
  return WRAPPER_KINDS.find((kind) => kind.element === el.localName);
83
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
+ }
84
114
  function wrapperFits(kind, wrappers) {
85
115
  const type = docxSchema.marks[kind.mark];
86
116
  if (!type.excludes(type)) return true;
@@ -98,6 +128,7 @@ function wrapperKindOf(mark) {
98
128
  }
99
129
  export {
100
130
  WRAPPER_KINDS,
131
+ emptyWrapperXml,
101
132
  nextKey,
102
133
  wrapperFits,
103
134
  wrapperKindFor,
@@ -10,6 +10,6 @@
10
10
  import { type Command } from "prosemirror-state";
11
11
  export declare const removeEmptyBlockControl: Command;
12
12
  /** Backspace passing over the controls holding nothing that stand before the caret */
13
- export declare const skipEmptyControlBefore: Command;
13
+ export declare const skipEmptyBlockControlBefore: Command;
14
14
  /** Delete passing over the controls holding nothing that stand after the caret */
15
- export declare const skipEmptyControlAfter: Command;
15
+ export declare const skipEmptyBlockControlAfter: Command;
@@ -1,7 +1,7 @@
1
1
  // src/editor/blockControlEdits.ts
2
2
  import { Selection, TextSelection } from "prosemirror-state";
3
3
  import { docxSchema } from "../schema/index.js";
4
- import { isBlockControl, isEmptyControl } from "../schema/controlAttrs.js";
4
+ import { isBlockControl, isEmptyBlockControl } from "../schema/controlAttrs.js";
5
5
  import { guardedCommand } from "../schema/guards.js";
6
6
  function soleEmptyControl($cursor) {
7
7
  if ($cursor.parent.content.size > 0) return null;
@@ -42,7 +42,7 @@ function emptyRunBeside(selection, dir) {
42
42
  let pos = dir < 0 ? $cursor.before(depth) : $cursor.after(depth);
43
43
  let index = first;
44
44
  let sibling = container.maybeChild(index);
45
- while (sibling && isEmptyControl(sibling)) {
45
+ while (sibling && isEmptyBlockControl(sibling)) {
46
46
  pos += dir * sibling.nodeSize;
47
47
  index += dir;
48
48
  sibling = container.maybeChild(index);
@@ -70,10 +70,10 @@ function skipEmptyControls(dir) {
70
70
  move(state, dispatch, view) || emptyRunBeside(state.selection, dir) !== null
71
71
  );
72
72
  }
73
- var skipEmptyControlBefore = skipEmptyControls(-1);
74
- var skipEmptyControlAfter = skipEmptyControls(1);
73
+ var skipEmptyBlockControlBefore = skipEmptyControls(-1);
74
+ var skipEmptyBlockControlAfter = skipEmptyControls(1);
75
75
  export {
76
76
  removeEmptyBlockControl,
77
- skipEmptyControlAfter,
78
- skipEmptyControlBefore
77
+ skipEmptyBlockControlAfter,
78
+ skipEmptyBlockControlBefore
79
79
  };
@@ -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 { controlAttrsOf } from "../../schema/controlAttrs.js";
8
+ import {
9
+ controlAttrsOf,
10
+ isEmptyInlineControl
11
+ } from "../../schema/controlAttrs.js";
9
12
  import { guardedCommand, openStretches } from "../../schema/guards.js";
10
13
  import {
11
14
  carriesLock,
12
15
  controlSpans,
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 = [];
@@ -128,7 +140,9 @@ function blockLockEdits(state, block, spans, reach) {
128
140
  return mark ? [{ from: span.from, to: span.to, mark, replaces: span.mark }] : [];
129
141
  }
130
142
  );
131
- const fresh = open(gapsIn(stretch, spans)).map((gap) => ({
143
+ const fresh = open(
144
+ gapsIn(stretch, spans).filter((gap) => !nothingToLock(block, gap))
145
+ ).map((gap) => ({
132
146
  ...gap,
133
147
  mark: lockedControlMark(newControlId(), wrapperDepthOver(block, gap))
134
148
  }));
@@ -152,6 +166,12 @@ function selectionLockDetail(state) {
152
166
  if (opened) containers.set(pos, opened);
153
167
  if (!node.isTextblock) return true;
154
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
+ }
155
175
  const controls = controlSpans(block);
156
176
  spans.push(
157
177
  ...liftable(
@@ -9,6 +9,7 @@ import type { EditableComments, EditingProtection } from "../schema/protection";
9
9
  import { type FontFallbacks } from "../styles/fontStack";
10
10
  import type { CommentAuthor } from "./commands/comments/model";
11
11
  import { type EditorDocument } from "./editorDocument";
12
+ import { type EditRefusal } from "./editRefusal";
12
13
  export interface EditorStateOptions {
13
14
  /**
14
15
  * What the opened document laid down: its styles, its list definitions, the paper it is
@@ -53,5 +54,7 @@ export interface EditorOptions {
53
54
  /** The fonts stood in for the ones the document declares. The built-in set when none is given */
54
55
  fontFallbacks?: FontFallbacks;
55
56
  onStateChange: (state: EditorState) => void;
57
+ /** Told about every edit the guards turn down (`./editRefusal`) */
58
+ onEditRefused?: (refusal: EditRefusal) => void;
56
59
  }
57
- export declare function createEditorView({ mount, state, fontFallbacks, onStateChange, }: EditorOptions): EditorView;
60
+ export declare function createEditorView({ mount, state, fontFallbacks, onStateChange, onEditRefused, }: EditorOptions): EditorView;
@@ -23,12 +23,14 @@ import {
23
23
  editorDocumentOf,
24
24
  NO_DOCUMENT
25
25
  } from "./editorDocument.js";
26
+ import { editRefusal } from "./editRefusal.js";
26
27
  import { imageFiles } from "./imageFiles.js";
27
28
  import { columnResize } from "./plugins/columnResize.js";
28
29
  import { commentComposer } from "./plugins/commentComposer.js";
29
30
  import { commentDecorations } from "./plugins/commentDecorations.js";
30
31
  import { commentRestoration } from "./plugins/commentRestoration.js";
31
32
  import { compositionSelection } from "./plugins/compositionSelection.js";
33
+ import { controlContents } from "./plugins/controlContents.js";
32
34
  import { controlLifecycle } from "./plugins/controlLifecycle.js";
33
35
  import {
34
36
  displayDerivation,
@@ -87,6 +89,10 @@ function createEditorState(doc, options = {}) {
87
89
  // Ahead of the keys and the clipboard: the plugin registered first sees a DOM event first,
88
90
  // and this one hands every composition on
89
91
  compositionSelection(),
92
+ // Keeps an inline control through an edit of everything it holds. It stands ahead of every
93
+ // plugin appending to an edit, which then sees the control it kept, and behind the one above,
94
+ // whose deletion it keeps the control through before a composition opens
95
+ controlContents(),
90
96
  history(),
91
97
  keymap(docxKeymap),
92
98
  historyKeys(),
@@ -163,7 +169,8 @@ function createEditorView({
163
169
  mount,
164
170
  state,
165
171
  fontFallbacks = DEFAULT_FONT_FALLBACKS,
166
- onStateChange
172
+ onStateChange,
173
+ onEditRefused
167
174
  }) {
168
175
  const view = new EditorView(mount, {
169
176
  state,
@@ -190,9 +197,14 @@ function createEditorView({
190
197
  image: (node, imageView, getPos) => new ImageNodeView(node, imageView, getPos)
191
198
  },
192
199
  dispatchTransaction(transaction) {
193
- const next = view.state.apply(transaction);
200
+ const before = view.state;
201
+ const next = before.apply(transaction);
194
202
  view.updateState(next);
195
203
  onStateChange(next);
204
+ if (next === before && onEditRefused) {
205
+ const refusal = editRefusal(transaction, before);
206
+ if (refusal) onEditRefused(refusal);
207
+ }
196
208
  }
197
209
  });
198
210
  return view;
@@ -0,0 +1,68 @@
1
+ /**
2
+ * What an application is told when the editor turns an edit down, so that it can say why.
3
+ *
4
+ * The guards (`schema/guards`) decide; this only reads a refused transaction back into terms a
5
+ * host can act on: which rule refused it, what the edit would have done, and, for a lock, which
6
+ * controls it reached, named by what the template wrote on them.
7
+ *
8
+ * The controls are read off the stretches the refused steps rewrite, which is the same reach the
9
+ * lock guard judges by, but it is a report rather than a second judgement: every control carrying a
10
+ * lock that the edit met is named, whichever of them the guard found first.
11
+ */
12
+ import type { EditorState, Transaction } from "prosemirror-state";
13
+ /**
14
+ * The rule that turned an edit down.
15
+ *
16
+ * - `protection`: the editor's `mode` takes no such edit, such as a body edit in a comment editor.
17
+ * - `lock`: the edit reaches a content control that the document locked, or a group control.
18
+ * - `controlEdge`: a keystroke would join text across the edge of a content control around blocks.
19
+ * - `preserved`: the edit would take away or reorder content the editor keeps without editing it,
20
+ * such as a bookmark's ends or the pieces of a field.
21
+ * - `section`: the edit would take a section break away.
22
+ *
23
+ * It is written out rather than read off the guard list, so that a guard added there has to be
24
+ * named here before it can be reported.
25
+ */
26
+ export type EditRefusalReason = "protection" | "lock" | "controlEdge" | "preserved" | "section";
27
+ /**
28
+ * What the refused edit would have done: put content in, take it away, both at once, or change
29
+ * formatting or structure while leaving the content where it stands.
30
+ */
31
+ export type EditRefusalAction = "insert" | "delete" | "replace" | "format";
32
+ /** The `w:lock` value a control carries (§17.18.49), `unlocked` when it carries none */
33
+ export type ControlLock = "unlocked" | "sdtLocked" | "contentLocked" | "sdtContentLocked";
34
+ /** What a content control stands around: text in a paragraph, whole blocks, a cell, or a row */
35
+ export type ControlLevel = "inline" | "block" | "cell" | "row";
36
+ /** A content control that refuses edits to its contents or its own removal */
37
+ export interface LockedControl {
38
+ /** The control's `w:tag`, which is how a template names it for software */
39
+ tag: string | null;
40
+ /** The control's `w:alias`, which is the name Word shows a reader */
41
+ alias: string | null;
42
+ /** The control's `w:id` */
43
+ id: number | null;
44
+ /** What its `w:lock` says about editing and removing it */
45
+ lock: ControlLock;
46
+ /** Whether it is a group control, which shuts its contents without a lock */
47
+ group: boolean;
48
+ /** What it stands around */
49
+ level: ControlLevel;
50
+ /** Where the control starts in the editor's document */
51
+ pos: number;
52
+ }
53
+ /** An edit the editor turned down, as `onEditRefused` is handed it */
54
+ export interface EditRefusal {
55
+ /** The rule that refused it */
56
+ reason: EditRefusalReason;
57
+ /** What it would have done */
58
+ action: EditRefusalAction;
59
+ /** Where the refused edit starts in the editor's document */
60
+ pos: number;
61
+ /** The locked controls the edit reached, innermost first. Empty unless `reason` is `lock` */
62
+ controls: readonly LockedControl[];
63
+ }
64
+ /**
65
+ * What the guards say about a transaction they turn down, or null when they let it through, which
66
+ * also leaves out a transaction a plugin of the application's own filtered.
67
+ */
68
+ export declare function editRefusal(tr: Transaction, state: EditorState): EditRefusal | null;