@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
@@ -0,0 +1,177 @@
1
+ // src/editor/editRefusal.ts
2
+ import {
3
+ AddMarkStep,
4
+ AddNodeMarkStep,
5
+ AttrStep,
6
+ RemoveMarkStep,
7
+ RemoveNodeMarkStep,
8
+ ReplaceAroundStep,
9
+ ReplaceStep
10
+ } from "prosemirror-transform";
11
+ import { childElement, parseProps, propsChild } from "../ooxml/props.js";
12
+ import { localPart } from "../ooxml/xml.js";
13
+ import {
14
+ controlAttrsOf,
15
+ controlFactsOf,
16
+ isEmptyBlockControl,
17
+ isEmptyInlineControl,
18
+ OWN_CONTROL_ATTRS
19
+ } from "../schema/controlAttrs.js";
20
+ import { transactionRefusal } from "../schema/guards.js";
21
+ import { controlSpans, emptyControlsIn } from "../schema/locks.js";
22
+ function reachesOf(step, doc) {
23
+ if (step instanceof ReplaceAroundStep) {
24
+ return [
25
+ { from: step.from, to: step.gapFrom },
26
+ { from: step.gapTo, to: step.to }
27
+ ];
28
+ }
29
+ if (step instanceof ReplaceStep || step instanceof AddMarkStep || step instanceof RemoveMarkStep) {
30
+ return [{ from: step.from, to: step.to }];
31
+ }
32
+ if (step instanceof AttrStep || step instanceof AddNodeMarkStep || step instanceof RemoveNodeMarkStep) {
33
+ const node = doc.nodeAt(step.pos);
34
+ return node ? [{ from: step.pos, to: step.pos + node.nodeSize }] : [];
35
+ }
36
+ return [];
37
+ }
38
+ function actionOf(tr) {
39
+ const replaced = tr.steps.filter((step) => step instanceof ReplaceStep);
40
+ const takes = replaced.some((step) => step.from < step.to);
41
+ const puts = replaced.some((step) => step.slice.size > 0);
42
+ if (takes && puts) return "replace";
43
+ if (takes) return "delete";
44
+ if (puts) return "insert";
45
+ return "format";
46
+ }
47
+ function lockOf(facts) {
48
+ if (facts.contentsLocked && facts.deletionLocked) return "sdtContentLocked";
49
+ if (facts.contentsLocked) return "contentLocked";
50
+ if (facts.deletionLocked) return "sdtLocked";
51
+ return "unlocked";
52
+ }
53
+ function refuses(facts) {
54
+ return facts.contentsLocked || facts.deletionLocked || facts.group;
55
+ }
56
+ function sdtPrValue(prefix, name) {
57
+ const sdt = parseProps(`${prefix}</w:sdt>`);
58
+ const child = sdt && propsChild(sdt.children, "sdtPr");
59
+ const sdtPr = child ? parseProps(child.xml) : null;
60
+ const element = sdtPr ? childElement(sdtPr, name) : null;
61
+ const val = element?.attrs.find(([attr]) => localPart(attr) === "val");
62
+ return val ? val[1] : null;
63
+ }
64
+ function idOf(prefix) {
65
+ const value = sdtPrValue(prefix, "id");
66
+ const id = value === null ? Number.NaN : Number.parseInt(value, 10);
67
+ return Number.isNaN(id) ? null : id;
68
+ }
69
+ function lockedControl(facts, level, pos) {
70
+ const prefix = facts.prefix ?? "";
71
+ return {
72
+ tag: sdtPrValue(prefix, "tag"),
73
+ alias: sdtPrValue(prefix, "alias"),
74
+ id: idOf(prefix),
75
+ lock: lockOf(facts),
76
+ group: facts.group,
77
+ level,
78
+ pos
79
+ };
80
+ }
81
+ function containerLevel(node) {
82
+ if (isEmptyInlineControl(node)) return "inline";
83
+ if (isEmptyBlockControl(node)) return "block";
84
+ const role = node.type.spec.tableRole;
85
+ if (role === "row") return "row";
86
+ if (role === "cell") return "cell";
87
+ return "block";
88
+ }
89
+ function containerFound(node, pos, depth) {
90
+ const names = controlAttrsOf(node);
91
+ if (!names) return null;
92
+ const facts = controlFactsOf(names, node.attrs);
93
+ if (facts.prefix === null || !refuses(facts)) return null;
94
+ return {
95
+ control: lockedControl(facts, containerLevel(node), pos),
96
+ depth
97
+ };
98
+ }
99
+ function markFacts(mark) {
100
+ return controlFactsOf(OWN_CONTROL_ATTRS, mark.attrs);
101
+ }
102
+ function meets(reach, span) {
103
+ return reach.from === reach.to ? span.from < reach.from && reach.from < span.to : reach.from < span.to && reach.to > span.from;
104
+ }
105
+ function inlineFound(block, start, depth, reach) {
106
+ const textblock = { node: block, start };
107
+ const marked = controlSpans(textblock).filter((span) => meets(reach, span)).filter((span) => refuses(markFacts(span.mark))).map((span) => ({
108
+ control: lockedControl(markFacts(span.mark), "inline", span.from),
109
+ depth: depth + 1 + Number(span.mark.attrs.depth ?? 0)
110
+ }));
111
+ const empty = emptyControlsIn(textblock).filter((control) => meets(reach, control.span)).map((control) => containerFound(control.node, control.span.from, depth)).filter((found) => found !== null);
112
+ return [...marked, ...empty];
113
+ }
114
+ function controlsReached(doc, reach) {
115
+ const found = [];
116
+ for (const at of [reach.from, reach.to]) {
117
+ const $pos = doc.resolve(at);
118
+ for (let depth = 1; depth <= $pos.depth; depth += 1) {
119
+ const container = containerFound(
120
+ $pos.node(depth),
121
+ $pos.before(depth),
122
+ depth
123
+ );
124
+ if (container) found.push(container);
125
+ }
126
+ if ($pos.parent.isTextblock) {
127
+ found.push(...inlineFound($pos.parent, $pos.start(), $pos.depth, reach));
128
+ }
129
+ }
130
+ doc.nodesBetween(reach.from, reach.to, (node, pos) => {
131
+ const depth = doc.resolve(pos).depth + 1;
132
+ const container = containerFound(node, pos, depth);
133
+ if (container) found.push(container);
134
+ if (!node.isTextblock) return true;
135
+ found.push(...inlineFound(node, pos + 1, depth, reach));
136
+ return false;
137
+ });
138
+ return found;
139
+ }
140
+ function sameControl(a, b) {
141
+ return a.pos === b.pos && a.level === b.level && a.id === b.id;
142
+ }
143
+ function controlsOf(tr, state) {
144
+ const found = tr.steps.flatMap((step, index) => {
145
+ const doc = tr.docs[index] ?? state.doc;
146
+ const back = tr.mapping.slice(0, index).invert();
147
+ return reachesOf(step, doc).flatMap((reach) => controlsReached(doc, reach)).map(({ control, depth }) => ({
148
+ control: { ...control, pos: back.map(control.pos, 1) },
149
+ depth
150
+ }));
151
+ });
152
+ const unique = [];
153
+ for (const entry of found) {
154
+ if (!unique.some((kept) => sameControl(kept.control, entry.control))) {
155
+ unique.push(entry);
156
+ }
157
+ }
158
+ return unique.sort((a, b) => b.depth - a.depth || a.control.pos - b.control.pos).map((entry) => entry.control);
159
+ }
160
+ function startOf(tr, state) {
161
+ const [first] = tr.steps;
162
+ const reach = first ? reachesOf(first, state.doc)[0] : void 0;
163
+ return reach?.from ?? state.selection.from;
164
+ }
165
+ function editRefusal(tr, state) {
166
+ const reason = transactionRefusal(tr, state);
167
+ if (reason === null) return null;
168
+ return {
169
+ reason,
170
+ action: actionOf(tr),
171
+ pos: startOf(tr, state),
172
+ controls: reason === "lock" ? controlsOf(tr, state) : []
173
+ };
174
+ }
175
+ export {
176
+ editRefusal
177
+ };
@@ -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;
@@ -0,0 +1,69 @@
1
+ // src/editor/inlineControlEdits.ts
2
+ import {
3
+ chainCommands,
4
+ joinBackward,
5
+ joinForward,
6
+ selectNodeBackward,
7
+ selectNodeForward
8
+ } from "prosemirror-commands";
9
+ import {
10
+ TextSelection
11
+ } from "prosemirror-state";
12
+ import { isEmptyInlineControl } from "../schema/controlAttrs.js";
13
+ import { guardedCommand } from "../schema/guards.js";
14
+ function beyondEmptyControls(doc, pos, dir) {
15
+ let at = pos;
16
+ for (; ; ) {
17
+ const $at = doc.resolve(at);
18
+ const sibling = dir < 0 ? $at.nodeBefore : $at.nodeAfter;
19
+ if (!sibling || !isEmptyInlineControl(sibling)) break;
20
+ at += dir * sibling.nodeSize;
21
+ }
22
+ return at === pos ? null : at;
23
+ }
24
+ function beyondRun(state, dir) {
25
+ const $cursor = state.selection instanceof TextSelection ? state.selection.$cursor : null;
26
+ return $cursor === null ? null : beyondEmptyControls(state.doc, $cursor.pos, dir);
27
+ }
28
+ function charSize(text, dir) {
29
+ const point = text.codePointAt(dir < 0 ? Math.max(text.length - 2, 0) : 0);
30
+ return point !== void 0 && point > 65535 ? 2 : 1;
31
+ }
32
+ function reachBeyond(doc, beyond, dir) {
33
+ const $beyond = doc.resolve(beyond);
34
+ const sibling = dir < 0 ? $beyond.nodeBefore : $beyond.nodeAfter;
35
+ if (!sibling) return null;
36
+ const size = sibling.isText ? charSize(sibling.text ?? "", dir) : sibling.nodeSize;
37
+ return dir < 0 ? { from: beyond - size, to: beyond } : { from: beyond, to: beyond + size };
38
+ }
39
+ function fromBlockEdge(state, beyond, atEdge) {
40
+ const moved = state.apply(
41
+ state.tr.setSelection(TextSelection.create(state.doc, beyond))
42
+ );
43
+ let built = null;
44
+ atEdge(moved, (tr) => {
45
+ built = tr;
46
+ });
47
+ return built;
48
+ }
49
+ function deleteBeyondEmptyControls(dir) {
50
+ const atEdge = dir < 0 ? chainCommands(joinBackward, selectNodeBackward) : chainCommands(joinForward, selectNodeForward);
51
+ const edit = guardedCommand((state) => {
52
+ const beyond = beyondRun(state, dir);
53
+ if (beyond === null) return null;
54
+ const reach = reachBeyond(state.doc, beyond, dir);
55
+ if (reach === null) return fromBlockEdge(state, beyond, atEdge);
56
+ return state.tr.delete(reach.from, reach.to).scrollIntoView();
57
+ });
58
+ return (state, dispatch, view) => (
59
+ // With nothing beyond the run to take, the key still may not take the control away, so it is
60
+ // reported handled and the document is left as it stood.
61
+ edit(state, dispatch, view) || beyondRun(state, dir) !== null
62
+ );
63
+ }
64
+ var deleteBeyondEmptyControlsBefore = deleteBeyondEmptyControls(-1);
65
+ var deleteBeyondEmptyControlsAfter = deleteBeyondEmptyControls(1);
66
+ export {
67
+ deleteBeyondEmptyControlsAfter,
68
+ deleteBeyondEmptyControlsBefore
69
+ };
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Keeps an inline content control standing through an edit of what it holds
3
+ * (`spec/notes/contentControls.md`).
4
+ *
5
+ * An inline control is a mark, and the mark is not inclusive, so ProseMirror carries it over a
6
+ * replacement only where it stands on both sides of it. Nothing of the control stands past its
7
+ * end, so typing, a composition or a paste over everything the control holds, or over its end,
8
+ * would write the new text beside the control, and a deletion of everything it holds would take
9
+ * the control away with its text.
10
+ *
11
+ * The lock guard reads such a replacement as an edit of the contents (`schema/locks`), and this
12
+ * appends what makes it one: the control laid back over what the edit wrote or, where the edit
13
+ * wrote nothing, the control left standing as one holding nothing (`docx/wrappers`).
14
+ *
15
+ * The caret such an edit leaves at the end of the control stays inside it, and so does the one
16
+ * left beside a control the edit emptied: what is typed, composed or pasted there next goes into
17
+ * the control rather than beside it, until the caret moves. A caret the user places against the
18
+ * edge of a control still stands outside it.
19
+ */
20
+ import { Plugin } from "prosemirror-state";
21
+ /** Where the caret keeps writing into the controls it wrote into, while it stays there */
22
+ type Continuing = number | null;
23
+ export declare function controlContents(): Plugin<Continuing>;
24
+ export {};
@@ -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
  }
@@ -11,8 +11,8 @@ import { docxSchema } from "../../schema/index.js";
11
11
  import { isBlockControl } from "../../schema/controlAttrs.js";
12
12
  import {
13
13
  removeEmptyBlockControl,
14
- skipEmptyControlAfter,
15
- skipEmptyControlBefore
14
+ skipEmptyBlockControlAfter,
15
+ skipEmptyBlockControlBefore
16
16
  } from "../blockControlEdits.js";
17
17
  import { insertLineBreak, insertPageBreak } from "../commands/breakCommands.js";
18
18
  import {
@@ -30,6 +30,10 @@ import {
30
30
  } from "../commands/listCommands.js";
31
31
  import { insertEndnote, insertFootnote } from "../commands/noteCommands.js";
32
32
  import { insertTab, moveAcrossTab } from "../commands/tabCommands.js";
33
+ import {
34
+ deleteBeyondEmptyControlsAfter,
35
+ deleteBeyondEmptyControlsBefore
36
+ } from "../inlineControlEdits.js";
33
37
  import { openLinkPanel } from "./linkPanel.js";
34
38
  var insertTabFromKeyboard = (state, dispatch, view) => {
35
39
  if (!state.selection.empty && activeLinkSpan(state)) return false;
@@ -114,16 +118,23 @@ var docxKeymap = {
114
118
  "Mod-Enter": insertPageBreak,
115
119
  // `undoInputRule` comes first because it answers false unless the last transaction was an input
116
120
  // rule, and the commands after it report the key handled wherever a control stands beside the
117
- // caret. Then: a control holding nothing is passed over rather than taken away, a control holding
118
- // one empty paragraph goes whole rather than being opened up, and the empty paragraph after a
119
- // table keeps adjacent tables separate without changing imported documents.
121
+ // caret. Then: a control holding nothing is never taken away by the key - between blocks the
122
+ // caret passes to the line beyond it, inside a paragraph the key passes over it and takes what
123
+ // stands beyond instead - a control holding one empty paragraph goes whole rather than being
124
+ // opened up, and the empty paragraph after a table keeps adjacent tables separate without
125
+ // changing imported documents.
120
126
  Backspace: chainCommands(
121
127
  undoInputRule,
122
- skipEmptyControlBefore,
128
+ skipEmptyBlockControlBefore,
129
+ deleteBeyondEmptyControlsBefore,
123
130
  removeEmptyBlockControl,
124
131
  preserveTableFollowingParagraph
125
132
  ),
126
- Delete: chainCommands(skipEmptyControlAfter, removeEmptyBlockControl),
133
+ Delete: chainCommands(
134
+ skipEmptyBlockControlAfter,
135
+ deleteBeyondEmptyControlsAfter,
136
+ removeEmptyBlockControl
137
+ ),
127
138
  ...historyKeymap,
128
139
  "Mod-b": toggleBold,
129
140
  "Mod-i": toggleItalic,
@@ -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";
@@ -3,7 +3,8 @@ import {
3
3
  NAMESPACES,
4
4
  qualify,
5
5
  W_PREFIX,
6
- wName
6
+ wName,
7
+ xmlnsName
7
8
  } from "./names.js";
8
9
  import { escapeXml, localPart } from "./xml.js";
9
10
  function attrsText(attrs) {
@@ -16,7 +17,7 @@ function elementXml(name, attrs, children = []) {
16
17
  return `${open}>${children.join("")}</${name}>`;
17
18
  }
18
19
  function xmlnsAttr(prefix) {
19
- return [`xmlns:${prefix}`, NAMESPACES[prefix]];
20
+ return [xmlnsName(prefix), NAMESPACES[prefix]];
20
21
  }
21
22
  function localNameUnder(prefix, name) {
22
23
  if (!name.includes(":")) return name;