@portone/docx-editor 0.6.4 → 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.
@@ -0,0 +1,260 @@
1
+ // src/editor/plugins/controlContents.ts
2
+ import { Mark } from "prosemirror-model";
3
+ import {
4
+ Plugin,
5
+ PluginKey,
6
+ TextSelection
7
+ } from "prosemirror-state";
8
+ import { Mapping, ReplaceStep } from "prosemirror-transform";
9
+ import { docxSchema } from "../../schema/index.js";
10
+ import {
11
+ controlAttrs,
12
+ controlFactsOf,
13
+ goesWithEdit,
14
+ isEmptyInlineControl,
15
+ OWN_CONTROL_ATTRS
16
+ } from "../../schema/controlAttrs.js";
17
+ import { displayOnly } from "../../schema/displayDerivation.js";
18
+ import {
19
+ controlKept,
20
+ controlLifted,
21
+ controlsWrittenInto,
22
+ historyReplay
23
+ } from "../../schema/locks.js";
24
+ import {
25
+ innermostDepth,
26
+ isWrapperType,
27
+ wrapperMarks,
28
+ wrappersOf
29
+ } from "../../schema/wrappers.js";
30
+ var controlContentsKey = new PluginKey(
31
+ "docxEditorControlContents"
32
+ );
33
+ var NOT_AN_EDIT = [historyReplay, displayOnly, controlLifted, controlKept];
34
+ function userEdit(tr) {
35
+ return tr.docChanged && tr.getMeta("appendedTransaction") === void 0 && !NOT_AN_EDIT.some((pass) => tr.getMeta(pass) === true);
36
+ }
37
+ function controlsOf(node) {
38
+ return node ? wrappersOf(node, docxSchema.marks.sdt) : [];
39
+ }
40
+ function controlOfEmpty(node) {
41
+ return docxSchema.marks.sdt.create({
42
+ depth: innermostDepth(node.marks) + 1,
43
+ key: node.attrs.key,
44
+ ...controlAttrs(
45
+ OWN_CONTROL_ATTRS,
46
+ controlFactsOf(OWN_CONTROL_ATTRS, node.attrs)
47
+ )
48
+ });
49
+ }
50
+ function emptyOf(control, wearing) {
51
+ return docxSchema.nodes.sdtEmptyInline.create(
52
+ {
53
+ key: control.attrs.key,
54
+ ...controlAttrs(
55
+ OWN_CONTROL_ATTRS,
56
+ controlFactsOf(OWN_CONTROL_ATTRS, control.attrs)
57
+ )
58
+ },
59
+ null,
60
+ wearing
61
+ );
62
+ }
63
+ function sameControl(a, b) {
64
+ return a.attrs.key === b.attrs.key && a.attrs.sdtPrefix === b.attrs.sdtPrefix;
65
+ }
66
+ function wearsControl(node, control) {
67
+ return controlsOf(node).some((worn) => sameControl(worn, control));
68
+ }
69
+ function kept(control) {
70
+ return !goesWithEdit(controlFactsOf(OWN_CONTROL_ATTRS, control.attrs));
71
+ }
72
+ function controlsContinued(doc, pos) {
73
+ const $pos = doc.resolve(pos);
74
+ const before = $pos.nodeBefore;
75
+ if (!before) return [];
76
+ if (isEmptyInlineControl(before)) {
77
+ const control = controlOfEmpty(before);
78
+ return kept(control) ? [control] : [];
79
+ }
80
+ const after = controlsOf($pos.nodeAfter);
81
+ return controlsOf(before).filter(
82
+ (control) => kept(control) && !after.some((other) => other.eq(control))
83
+ );
84
+ }
85
+ function writtenStretches(transactions, continuing) {
86
+ const maps = transactions.flatMap((tr) => tr.mapping.maps);
87
+ const written = [];
88
+ let index = 0;
89
+ for (const tr of transactions) {
90
+ const edit = userEdit(tr);
91
+ tr.steps.forEach((step, stepIndex) => {
92
+ const at = index;
93
+ index += 1;
94
+ if (!edit || !(step instanceof ReplaceStep)) return;
95
+ const doc = tr.docs[stepIndex] ?? tr.doc;
96
+ const found = stepControls(doc, step, continuing, maps.slice(0, at));
97
+ if (found === null) return;
98
+ const rest = new Mapping(maps.slice(at + 1));
99
+ const from = rest.map(step.from, 1);
100
+ written.push({
101
+ ...found,
102
+ from,
103
+ to: Math.max(from, rest.map(step.from + step.slice.size, -1))
104
+ });
105
+ });
106
+ }
107
+ return written;
108
+ }
109
+ function stepControls(doc, step, continuing, before) {
110
+ if (step.from < step.to) {
111
+ const spans = controlsWrittenInto(doc, step.from, step.to);
112
+ const controls2 = spans.map((span) => span.mark).filter(kept);
113
+ if (controls2.length === 0) return null;
114
+ const innermost = Math.max(...spans.map((span) => span.from));
115
+ return { controls: controls2, first: doc.nodeAt(innermost) };
116
+ }
117
+ if (continuing === null) return null;
118
+ if (new Mapping(before).map(continuing) !== step.from) return null;
119
+ const controls = controlsContinued(doc, step.from);
120
+ if (controls.length === 0) return null;
121
+ return { controls, first: doc.resolve(step.from).nodeBefore };
122
+ }
123
+ function carries(node, wrapper) {
124
+ if (wrapper.type !== docxSchema.marks.sdt) return wrapper.isInSet(node.marks);
125
+ return isEmptyInlineControl(node) ? sameControl(node, wrapper) : wearsControl(node, wrapper);
126
+ }
127
+ function stands(doc, wrapper) {
128
+ let found = false;
129
+ doc.descendants((node) => {
130
+ if (found) return false;
131
+ found = carries(node, wrapper);
132
+ return !found;
133
+ });
134
+ return found;
135
+ }
136
+ function marksWith(worn, controls) {
137
+ let set = Mark.none;
138
+ for (const mark of [...worn, ...controls]) set = mark.addToSet(set);
139
+ return set;
140
+ }
141
+ function keepControls(tr, written) {
142
+ let stored = null;
143
+ for (const stretch of written) {
144
+ const from = tr.mapping.map(stretch.from, 1);
145
+ const $from = tr.doc.resolve(from);
146
+ if (!$from.parent.inlineContent) continue;
147
+ const to = Math.min(tr.mapping.map(stretch.to, -1), $from.end());
148
+ if (to > from) {
149
+ for (const control of stretch.controls) {
150
+ if (!wearsControl(tr.doc.nodeAt(from), control)) {
151
+ tr.addMark(from, to, control);
152
+ }
153
+ }
154
+ }
155
+ const gone = stretch.controls.filter((control) => !stands(tr.doc, control));
156
+ const innermost = gone.at(-1);
157
+ if (innermost === void 0) continue;
158
+ const node = emptyOf(innermost, wrappersAround(tr.doc, stretch, gone));
159
+ tr.insert(from, node);
160
+ const { selection } = tr;
161
+ if (selection.empty && selection.head === from + node.nodeSize) {
162
+ stored = marksWith(
163
+ (stretch.first?.marks ?? []).filter(
164
+ (mark) => !isWrapperType(mark.type)
165
+ ),
166
+ [...node.marks, innermost]
167
+ );
168
+ }
169
+ }
170
+ return stored;
171
+ }
172
+ function wrappersAround(doc, stretch, gone) {
173
+ const ordered = stretch.first ? wrapperMarks(stretch.first) : [];
174
+ const innermost = gone.at(-1);
175
+ const inside = ordered.findIndex(
176
+ (mark) => innermost !== void 0 && mark.type === innermost.type && sameControl(mark, innermost)
177
+ );
178
+ return ordered.slice(0, inside < 0 ? 0 : inside).filter(
179
+ (mark) => gone.some(
180
+ (control) => mark.type === control.type && sameControl(control, mark)
181
+ ) || stands(doc, mark)
182
+ );
183
+ }
184
+ function dropRefilled(tr, written) {
185
+ const blocks = /* @__PURE__ */ new Set();
186
+ for (const stretch of written) {
187
+ const $at = tr.doc.resolve(tr.mapping.map(stretch.from, 1));
188
+ if ($at.parent.inlineContent) blocks.add($at.before());
189
+ }
190
+ const refilled = [];
191
+ for (const before of blocks) {
192
+ const block = tr.doc.nodeAt(before);
193
+ if (!block) continue;
194
+ block.forEach((child, offset, index) => {
195
+ if (!isEmptyInlineControl(child)) return;
196
+ const beside = [block.maybeChild(index - 1), block.maybeChild(index + 1)];
197
+ const refills = beside.some(
198
+ (node) => controlsOf(node).some((control) => sameControl(child, control))
199
+ );
200
+ const from = before + 1 + offset;
201
+ if (refills) refilled.push({ from, to: from + child.nodeSize });
202
+ });
203
+ }
204
+ for (const { from, to } of refilled.reverse()) tr.delete(from, to);
205
+ }
206
+ function continuingAfter(tr, written, previous) {
207
+ const caret = tr.selection instanceof TextSelection ? tr.selection.$cursor : null;
208
+ if (!caret || controlsContinued(tr.doc, caret.pos).length === 0) {
209
+ return null;
210
+ }
211
+ const left = written.some((stretch) => tr.mapping.map(stretch.to, 1) === caret.pos) || previous !== null && tr.mapping.map(previous) === caret.pos;
212
+ return left ? caret.pos : null;
213
+ }
214
+ function controlContents() {
215
+ let live = null;
216
+ return new Plugin({
217
+ key: controlContentsKey,
218
+ state: {
219
+ init: () => null,
220
+ apply(tr, value, oldState, newState) {
221
+ const meta = tr.getMeta(controlContentsKey);
222
+ if (meta !== void 0) return meta.continuing;
223
+ if (value === null || tr.getMeta(historyReplay) === true) return null;
224
+ if (tr.docChanged) return tr.mapping.map(value);
225
+ return newState.selection.eq(oldState.selection) ? value : null;
226
+ }
227
+ },
228
+ view(view) {
229
+ live = view;
230
+ return {
231
+ destroy() {
232
+ live = null;
233
+ }
234
+ };
235
+ },
236
+ appendTransaction(transactions, oldState, newState) {
237
+ if (!transactions.some(userEdit)) return null;
238
+ const written = writtenStretches(
239
+ transactions,
240
+ controlContentsKey.getState(oldState) ?? null
241
+ );
242
+ const previous = controlContentsKey.getState(newState) ?? null;
243
+ const tr = newState.tr.setMeta(controlKept, true);
244
+ const stored = keepControls(tr, written);
245
+ dropRefilled(tr, written);
246
+ if (stored !== null && live?.composing !== true) {
247
+ tr.setStoredMarks(stored);
248
+ }
249
+ const continuing = continuingAfter(tr, written, previous);
250
+ if (!tr.docChanged && !tr.storedMarksSet && continuing === previous) {
251
+ return null;
252
+ }
253
+ const meta = { continuing };
254
+ return tr.setMeta(controlContentsKey, meta);
255
+ }
256
+ });
257
+ }
258
+ export {
259
+ controlContents
260
+ };
@@ -7,6 +7,7 @@ import {
7
7
  controlAttrs,
8
8
  controlAttrsOf,
9
9
  controlFactsOf,
10
+ goesWithEdit,
10
11
  isBlockControl,
11
12
  NO_CONTROL,
12
13
  OWN_CONTROL_ATTRS
@@ -40,13 +41,10 @@ function withoutPlaceholderFlag(prefix) {
40
41
  function acts(control) {
41
42
  return control.temporary || control.showingPlaceholder;
42
43
  }
43
- function lifts(control) {
44
- return control.temporary && !control.deletionLocked;
45
- }
46
44
  function settleContainer(tr, node, pos, names, control) {
47
45
  const at = tr.mapping.map(pos);
48
46
  const end = tr.mapping.map(pos + node.nodeSize);
49
- if (lifts(control)) {
47
+ if (goesWithEdit(control)) {
50
48
  if (isBlockControl(node)) {
51
49
  tr.step(
52
50
  new ReplaceAroundStep(at, end, at + 1, end - 1, Slice.empty, 0, true)
@@ -70,7 +68,7 @@ function settleContainer(tr, node, pos, names, control) {
70
68
  function settleMark(tr, span, control) {
71
69
  const from = tr.mapping.map(span.from);
72
70
  const to = tr.mapping.map(span.to);
73
- if (lifts(control)) {
71
+ if (goesWithEdit(control)) {
74
72
  tr.removeMark(from, to, span.mark);
75
73
  return;
76
74
  }
@@ -7,6 +7,7 @@ import { tableEditing } from "prosemirror-tables";
7
7
  import { docxClipboard } from "../clipboard/plugin.js";
8
8
  import { editorDocument } from "../editorDocument.js";
9
9
  import { compositionSelection } from "../plugins/compositionSelection.js";
10
+ import { controlContents } from "../plugins/controlContents.js";
10
11
  import { controlLifecycle } from "../plugins/controlLifecycle.js";
11
12
  import {
12
13
  displayDerivation,
@@ -51,6 +52,7 @@ function storyEditorState({
51
52
  lockedContent(),
52
53
  documentProtection({ protection, author: null, editableComments: "own" }),
53
54
  compositionSelection(),
55
+ controlContents(),
54
56
  // A kind's own key rule stands ahead of the keymaps below: the base keymap answers
55
57
  // Backspace and Enter for every document, so a rule about an empty story would never be
56
58
  // asked behind it
package/dist/index.d.ts CHANGED
@@ -20,6 +20,8 @@ export type { ExportPartName, ExportProblem, ExportProblemReason, ExportProblemS
20
20
  export type { DownloadDocxOptions, DownloadDocxResult, } from "./download";
21
21
  export { downloadDocx } from "./download";
22
22
  export type { CommentAuthor } from "./editor/commands/commentCommands";
23
+ /** What `onEditRefused` is handed when the editor turns an edit down */
24
+ export type { ControlLevel, ControlLock, EditRefusal, EditRefusalAction, EditRefusalReason, LockedControl, } from "./editor/editRefusal";
23
25
  export type { CellFormat, CellVerticalAlign, DocumentDefaults, HighlightName, LineSpacing, NumberingRef, ParagraphAlign, ParagraphFormat, RowFormat, RowHeight, RunFormat, TableFormat, TableWidth, TableWidthType, UnderlineKind, VerticalAlign, } from "./model/format";
24
26
  export type { DocxExportErrorCode, DocxExportErrorOptions, DocxImportErrorCode, } from "./ooxml/errors";
25
27
  export { DocxExportError, DocxImportError } from "./ooxml/errors";
@@ -43,6 +43,12 @@ export interface ControlFacts {
43
43
  /** Whether `w:showingPlcHdr` says what stands inside is placeholder text (§17.5.2.39) */
44
44
  showingPlaceholder: boolean;
45
45
  }
46
+ /**
47
+ * Whether the first edit of its contents takes the control away. `w:temporary` (§17.5.2.43) says
48
+ * it goes and a lock against deletion (§17.5.2.23) that it may not, and the lock wins
49
+ * (`spec/notes/contentControls.md`).
50
+ */
51
+ export declare function goesWithEdit(control: ControlFacts): boolean;
46
52
  /** Everything a control states other than the opening XML the carrier preserves */
47
53
  export type ControlFlags = Omit<ControlFacts, "prefix">;
48
54
  /** The control's own names, which the mark and the block-level container both carry */
@@ -2,6 +2,9 @@
2
2
  var SDT_BLOCK_NODE = "sdtBlock";
3
3
  var SDT_EMPTY_BLOCK_NODE = "sdtEmpty";
4
4
  var SDT_EMPTY_INLINE_NODE = "sdtEmptyInline";
5
+ function goesWithEdit(control) {
6
+ return control.temporary && !control.deletionLocked;
7
+ }
5
8
  var OWN_CONTROL_ATTRS = {
6
9
  prefix: "sdtPrefix",
7
10
  contentsLocked: "contentsLocked",
@@ -131,6 +134,7 @@ export {
131
134
  controlClassName,
132
135
  controlFactsOf,
133
136
  controlToDom,
137
+ goesWithEdit,
134
138
  isBlockControl,
135
139
  isEmptyBlockControl,
136
140
  isEmptyInlineControl
@@ -22,7 +22,7 @@
22
22
  * is open at all, which is a question about the editor rather than about an edit.
23
23
  */
24
24
  import type { Command, EditorState, Selection, Transaction } from "prosemirror-state";
25
- import { type EditGuard, type EditIntent } from "./editGuard";
25
+ import { type EditGuard, type EditGuardName, type EditIntent } from "./editGuard";
26
26
  import { type ProtectionState } from "./protection";
27
27
  export type { EditGuard, EditGuardName, EditIntent } from "./editGuard";
28
28
  export { stepReaches } from "./editGuard";
@@ -42,7 +42,8 @@ export declare function transactionTouchesComments(tr: Transaction): boolean;
42
42
  export declare function protectionAllowsTransaction(tr: Transaction, rules: ProtectionState): boolean;
43
43
  export declare const EDIT_GUARDS: readonly EditGuard[];
44
44
  /**
45
- * Whether every guard would let this transaction through, decided and nothing else.
45
+ * The first guard that would turn this transaction down, decided and nothing else. Null when every
46
+ * guard lets it through.
46
47
  *
47
48
  * The refusal a guard itself answers with carries a side effect - the composition it ends
48
49
  * (`editor/plugins/lockedContent`) - which a query about a button's state may not set off, so the
@@ -53,7 +54,12 @@ export declare const EDIT_GUARDS: readonly EditGuard[];
53
54
  * whole-change judgements then come first and take no pass, since a pass lifts one guard's
54
55
  * reading of a step rather than another guard's reading of the change. What is left is judged
55
56
  * step by step, each step over the document it was built against.
57
+ *
58
+ * Which guard answers is what a refusal is reported under (`editor/editRefusal`), so the list is
59
+ * asked in its own order and the first refusal is the one named.
56
60
  */
61
+ export declare function transactionRefusal(tr: Transaction, state: EditorState): EditGuardName | null;
62
+ /** Whether every guard would let this transaction through (`transactionRefusal`) */
57
63
  export declare function transactionAllowed(tr: Transaction, state: EditorState): boolean;
58
64
  /**
59
65
  * Whether any guard shuts this intent where it stands.
@@ -52,16 +52,17 @@ function displayOnlyTransaction(tr, state) {
52
52
  (step, index) => changesOnlyDisplayAttrs(step, tr.docs[index] ?? state.doc)
53
53
  );
54
54
  }
55
- function transactionAllowed(tr, state) {
56
- if (!tr.docChanged) return true;
57
- if (displayOnlyTransaction(tr, state)) return true;
58
- if (EDIT_GUARDS.some((guard) => guard.change?.(tr, state) === false)) {
59
- return false;
60
- }
61
- return EDIT_GUARDS.filter(judgesSteps).filter((guard) => !lifted(guard, tr)).every(
55
+ function transactionRefusal(tr, state) {
56
+ if (!tr.docChanged) return null;
57
+ if (displayOnlyTransaction(tr, state)) return null;
58
+ const changeRefusal = EDIT_GUARDS.find(
59
+ (guard) => guard.change?.(tr, state) === false
60
+ );
61
+ if (changeRefusal) return changeRefusal.name;
62
+ const stepRefusal = EDIT_GUARDS.filter(judgesSteps).filter((guard) => !lifted(guard, tr)).find(
62
63
  (guard) => (
63
64
  // Each step counts positions in the document it was built against, which `docs` holds
64
- tr.steps.every(
65
+ !tr.steps.every(
65
66
  (step, index) => guard.step(
66
67
  step,
67
68
  tr.docs[index] ?? state.doc,
@@ -71,6 +72,10 @@ function transactionAllowed(tr, state) {
71
72
  )
72
73
  )
73
74
  );
75
+ return stepRefusal?.name ?? null;
76
+ }
77
+ function transactionAllowed(tr, state) {
78
+ return transactionRefusal(tr, state) === null;
74
79
  }
75
80
  function editShut(state, intent) {
76
81
  return EDIT_GUARDS.some((guard) => guard.shuts(intent, state));
@@ -105,5 +110,6 @@ export {
105
110
  selectionIntents,
106
111
  stepReaches,
107
112
  transactionAllowed,
113
+ transactionRefusal,
108
114
  transactionTouchesComments
109
115
  };
@@ -37,6 +37,18 @@ export declare const historyReplay: PluginKey<boolean>;
37
37
  * (`spec/notes/contentControls.md`).
38
38
  */
39
39
  export declare const controlLifted: PluginKey<boolean>;
40
+ /**
41
+ * The pass the transaction that keeps an inline control through an edit of its contents carries
42
+ * (`editor/plugins/controlContents`).
43
+ *
44
+ * Writing over everything a control holds is an edit of its contents rather than its deletion
45
+ * (`controlsWrittenInto`), and that transaction is what keeps the control standing: it lays the
46
+ * control back over what was written, or leaves the control standing as one holding nothing. When
47
+ * the caret then writes into a control it emptied, the node it stood as goes, since the control
48
+ * stands as the new text instead. That removal is the one step here a lock against deletion would
49
+ * refuse, and it removes no control.
50
+ */
51
+ export declare const controlKept: PluginKey<boolean>;
40
52
  /** The stretch a judgement or an edit runs over */
41
53
  export interface StepRange {
42
54
  from: number;
@@ -91,6 +103,16 @@ export declare function controlSpans(block: Textblock): ControlSpan[];
91
103
  * (`editor/commands/lockCommands`).
92
104
  */
93
105
  export declare function coversWhole(range: StepRange, span: StepRange): boolean;
106
+ /**
107
+ * The inline controls replacing this stretch writes into rather than takes away, each as the
108
+ * whole stretch it covers, outermost first.
109
+ *
110
+ * These are the controls the stretch stands within (`standsWithin`) whose contents no lock of
111
+ * their own shuts, which is what the lock guard reads as an edit of the contents rather than a
112
+ * deletion. What takes the stretch's place belongs inside each of them, and a control the
113
+ * replacement leaves holding nothing still stands (`editor/plugins/controlContents`).
114
+ */
115
+ export declare function controlsWrittenInto(doc: PMNode, from: number, to: number): ControlSpan[];
94
116
  /** One content control holding nothing, standing as a node of its own, as the one spot it covers */
95
117
  export interface EmptyControl {
96
118
  node: PMNode;
@@ -125,7 +147,8 @@ export declare function selectionShut(selection: Selection, doc: PMNode): boolea
125
147
  /**
126
148
  * The locks the document carries, as `./guards` registers them.
127
149
  *
128
- * Both passes lift it: unlocking is the one edit that may reach into a lock, and every step the
129
- * history replays is the reverse of a step that passed the guard when it was made.
150
+ * Every pass lifts it: unlocking is the one edit that may reach into a lock, every step the
151
+ * history replays is the reverse of a step that passed the guard when it was made, and the other
152
+ * two carry what an edit that passed does to a control (`controlLifted`, `controlKept`).
130
153
  */
131
154
  export declare const lockGuard: StepGuard;
@@ -19,6 +19,7 @@ import { wrappersOf } from "./wrappers.js";
19
19
  var unlockAllowed = new PluginKey("docxEditorUnlockAllowed");
20
20
  var historyReplay = new PluginKey("docxEditorHistoryReplay");
21
21
  var controlLifted = new PluginKey("docxEditorControlLifted");
22
+ var controlKept = new PluginKey("docxEditorControlKept");
22
23
  var OPEN = { contents: false, deletion: false, group: false };
23
24
  function shutsContents(locks, inner) {
24
25
  return locks.contents || locks.group && !inner;
@@ -126,8 +127,68 @@ function coversWhole(range, span) {
126
127
  function overlaps(range, span) {
127
128
  return range.from < span.to && range.to > span.from;
128
129
  }
129
- function shuts(locks, range, span, inner) {
130
- return range.takesAway && coversWhole(range, span) ? locks.deletion : shutsContents(locks, inner) && overlaps(range, span);
130
+ function drawsNothing(node) {
131
+ switch (node.type.name) {
132
+ case "commentStart":
133
+ case "commentEnd":
134
+ return true;
135
+ case "rawInline":
136
+ case "rawRunContent":
137
+ return node.attrs.display === "hidden";
138
+ default:
139
+ return isEmptyInlineControl(node);
140
+ }
141
+ }
142
+ function visibleStretch(block, range, span) {
143
+ let { from, to } = range;
144
+ const children = [];
145
+ block.node.forEach((node, offset) => {
146
+ children.push({ node, at: block.start + offset });
147
+ });
148
+ for (const { node, at } of children) {
149
+ if (at === from && from < span.from && drawsNothing(node)) {
150
+ from = at + node.nodeSize;
151
+ }
152
+ }
153
+ for (const { node, at } of children.reverse()) {
154
+ if (at + node.nodeSize === to && to > span.to && drawsNothing(node)) {
155
+ to = at;
156
+ }
157
+ }
158
+ return { from, to };
159
+ }
160
+ function standsWithin(block, range, span) {
161
+ if (range.from >= range.to) return false;
162
+ const end = block.start + block.node.content.size;
163
+ if (range.from < block.start || range.to > end) return false;
164
+ const visible = visibleStretch(block, range, span);
165
+ return visible.from >= span.from && visible.to <= span.to;
166
+ }
167
+ function shuts(locks, range, span, inner, within) {
168
+ const open = !shutsContents(locks, inner);
169
+ return range.takesAway && coversWhole(range, span) && !(within && open) ? locks.deletion : !open && overlaps(range, span);
170
+ }
171
+ function spanShut(block, spans, span, range) {
172
+ return shuts(
173
+ markLocks(span.mark),
174
+ range,
175
+ span,
176
+ supersededSpan(spans, span, range),
177
+ standsWithin(block, range, span)
178
+ );
179
+ }
180
+ function controlsWrittenInto(doc, from, to) {
181
+ if (from >= to) return [];
182
+ const $from = doc.resolve(from);
183
+ if (!$from.parent.isTextblock || !$from.sameParent(doc.resolve(to))) {
184
+ return [];
185
+ }
186
+ const block = { node: $from.parent, start: $from.start() };
187
+ const range = { from, to };
188
+ const spans = controlSpans(block);
189
+ return spans.filter(
190
+ (span) => standsWithin(block, range, span) && !shutsContents(markLocks(span.mark), supersededSpan(spans, span, range))
191
+ );
131
192
  }
132
193
  function supersededSpan(spans, span, range) {
133
194
  return spans.some(
@@ -162,14 +223,7 @@ function rangeShut(doc, range) {
162
223
  if (!node.isTextblock) return true;
163
224
  const block = { node, start: pos + 1 };
164
225
  const spans = controlSpans(block);
165
- shut ||= spans.some(
166
- (span) => shuts(
167
- markLocks(span.mark),
168
- range,
169
- span,
170
- supersededSpan(spans, span, range)
171
- )
172
- );
226
+ shut ||= spans.some((span) => spanShut(block, spans, span, range));
173
227
  shut ||= emptyControlsIn(block).some(
174
228
  (control) => containerShut(control.node, control.span, range)
175
229
  );
@@ -275,14 +329,16 @@ function intentShut(doc, intent) {
275
329
  }
276
330
  var lockGuard = {
277
331
  name: "lock",
278
- liftedBy: [unlockAllowed, historyReplay, controlLifted],
332
+ liftedBy: [unlockAllowed, historyReplay, controlLifted, controlKept],
279
333
  step: (step, before) => stepAllowed(step, before),
280
334
  shuts: (intent, state) => intentShut(state.doc, intent)
281
335
  };
282
336
  export {
283
337
  carriesLock,
338
+ controlKept,
284
339
  controlLifted,
285
340
  controlSpans,
341
+ controlsWrittenInto,
286
342
  coversWhole,
287
343
  emptyControlsIn,
288
344
  historyReplay,
@@ -122,6 +122,8 @@ export declare const editorClassNames: {
122
122
  /** The keyboard shortcut written on the right of a menu row */
123
123
  readonly menuHint: "docx-editor-menu-hint";
124
124
  readonly menuSeparator: "docx-editor-menu-sep";
125
+ /** The line at the head of a menu saying why its editing entries stand disabled */
126
+ readonly menuNote: "docx-editor-menu-note";
125
127
  /** The panel that reads and writes a link's address, floated over the selection */
126
128
  readonly linkPanel: "docx-editor-link-panel";
127
129
  /** The field the address is typed into */
@@ -118,6 +118,8 @@ var editorClassNames = {
118
118
  /** The keyboard shortcut written on the right of a menu row */
119
119
  menuHint: `${PREFIX}-menu-hint`,
120
120
  menuSeparator: `${PREFIX}-menu-sep`,
121
+ /** The line at the head of a menu saying why its editing entries stand disabled */
122
+ menuNote: `${PREFIX}-menu-note`,
121
123
  /** The panel that reads and writes a link's address, floated over the selection */
122
124
  linkPanel: `${PREFIX}-link-panel`,
123
125
  /** The field the address is typed into */