@portone/docx-editor 0.6.4 → 0.6.6
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.
- package/CHANGELOG.md +47 -0
- package/CONTRIBUTING.md +4 -4
- package/dist/DocxEditor.d.ts +11 -0
- package/dist/DocxEditor.js +7 -3
- package/dist/core.d.ts +7 -0
- package/dist/core.js +2 -0
- package/dist/docx/commentOnlyChange.d.ts +4 -4
- package/dist/docx/comments/constants.d.ts +7 -0
- package/dist/docx/comments/constants.js +13 -1
- package/dist/docx/comments/dates.d.ts +22 -0
- package/dist/docx/comments/dates.js +102 -0
- package/dist/docx/comments/durable.d.ts +22 -0
- package/dist/docx/comments/durable.js +138 -0
- package/dist/docx/comments/grammar.d.ts +10 -1
- package/dist/docx/comments/grammar.js +49 -2
- package/dist/docx/comments/model.d.ts +10 -2
- package/dist/docx/comments/model.js +38 -8
- package/dist/docx/comments/parts.d.ts +53 -7
- package/dist/docx/comments/parts.js +163 -20
- package/dist/docx/comments/people.d.ts +1 -1
- package/dist/docx/comments/people.js +5 -15
- package/dist/docx/comments/reading.d.ts +26 -0
- package/dist/docx/comments/reading.js +109 -49
- package/dist/docx/comments/writing.d.ts +2 -7
- package/dist/docx/comments/writing.js +30 -43
- package/dist/docx/compareDocx.d.ts +154 -0
- package/dist/docx/compareDocx.js +218 -0
- package/dist/docx/importParagraph.js +5 -3
- package/dist/docx/invariants.js +19 -6
- package/dist/docx/packageParts.d.ts +2 -0
- package/dist/docx/packageParts.js +5 -1
- package/dist/docx/partPlan.d.ts +12 -0
- package/dist/docx/partPlan.js +19 -1
- package/dist/docx/protectionPolicy.d.ts +33 -5
- package/dist/docx/protectionPolicy.js +93 -47
- package/dist/docx/sequenceDiff.d.ts +56 -0
- package/dist/docx/sequenceDiff.js +252 -0
- package/dist/docx/storyProjection.d.ts +2 -0
- package/dist/docx/storyProjection.js +10 -9
- package/dist/docx/textDiff.d.ts +24 -0
- package/dist/docx/textDiff.js +38 -0
- package/dist/editor/commands/comments/editing.js +56 -20
- package/dist/editor/commands/comments/model.d.ts +3 -1
- package/dist/editor/createEditor.d.ts +4 -1
- package/dist/editor/createEditor.js +14 -2
- package/dist/editor/editRefusal.d.ts +68 -0
- package/dist/editor/editRefusal.js +177 -0
- package/dist/editor/editorDocument.d.ts +4 -0
- package/dist/editor/editorDocument.js +10 -0
- package/dist/editor/plugins/commentDecorations.js +6 -2
- package/dist/editor/plugins/controlContents.d.ts +24 -0
- package/dist/editor/plugins/controlContents.js +260 -0
- package/dist/editor/plugins/controlLifecycle.js +3 -5
- package/dist/editor/stories/storyState.js +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/ooxml/errors.d.ts +1 -1
- package/dist/ooxml/names.d.ts +2 -0
- package/dist/ooxml/names.js +2 -0
- package/dist/ooxml/partSplice.d.ts +13 -3
- package/dist/ooxml/partSplice.js +45 -18
- package/dist/schema/attrRoles.js +3 -1
- package/dist/schema/controlAttrs.d.ts +6 -0
- package/dist/schema/controlAttrs.js +4 -0
- package/dist/schema/docxSchema.js +10 -3
- package/dist/schema/guards.d.ts +8 -2
- package/dist/schema/guards.js +14 -8
- package/dist/schema/locks.d.ts +25 -2
- package/dist/schema/locks.js +67 -11
- package/dist/schema/protection.js +2 -1
- package/dist/styles/classNames.d.ts +2 -0
- package/dist/styles/classNames.js +2 -0
- package/dist/styles.css +65 -14
- package/dist/ui/CommentsPanel.js +8 -13
- package/dist/ui/MenuNote.d.ts +12 -0
- package/dist/ui/MenuNote.js +14 -0
- package/dist/ui/TableMenu.js +38 -19
- package/dist/ui/TextMenu.js +31 -25
- package/dist/ui/comments/commentDate.d.ts +7 -0
- package/dist/ui/comments/commentDate.js +17 -0
- package/package.json +7 -7
|
@@ -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;
|
|
@@ -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
|
+
};
|
|
@@ -43,6 +43,8 @@ export interface EditorDocument {
|
|
|
43
43
|
readonly reservedCommentIds: ReadonlySet<string>;
|
|
44
44
|
/** Every paragraph id present in the opened comment parts, including orphan extension entries */
|
|
45
45
|
readonly reservedCommentParaIds: ReadonlySet<string>;
|
|
46
|
+
/** Every durable id the opened comment parts name, including orphan entries */
|
|
47
|
+
readonly reservedCommentDurableIds: ReadonlySet<string>;
|
|
46
48
|
/** How the document counts its footnotes and endnotes, which the note labels are spelled in */
|
|
47
49
|
readonly noteNumbering: NoteNumbering;
|
|
48
50
|
/** The note entries that lay out the page rather than number a note: separators and the continuation notice */
|
|
@@ -79,3 +81,5 @@ export declare function storyDocument(document: EditorDocument, geometry: PageGe
|
|
|
79
81
|
export declare function reservedCommentIds(state: EditorState): ReadonlySet<string>;
|
|
80
82
|
/** The paragraph ids the opened comment parts already spent */
|
|
81
83
|
export declare function reservedCommentParaIds(state: EditorState): ReadonlySet<string>;
|
|
84
|
+
/** The durable ids the opened comment parts already spent */
|
|
85
|
+
export declare function reservedCommentDurableIds(state: EditorState): ReadonlySet<string>;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// src/editor/editorDocument.ts
|
|
2
2
|
import { Plugin, PluginKey } from "prosemirror-state";
|
|
3
|
+
import { spentDurableIds } from "../docx/comments/reading.js";
|
|
3
4
|
import { DEFAULT_TAB_STOP_PT } from "../docx/documentSettings.js";
|
|
4
5
|
import {
|
|
5
6
|
NO_DOCUMENT_DEFAULTS,
|
|
@@ -29,6 +30,7 @@ var NO_DOCUMENT = {
|
|
|
29
30
|
defaultTabStopPt: DEFAULT_TAB_STOP_PT,
|
|
30
31
|
reservedCommentIds: NO_IDS,
|
|
31
32
|
reservedCommentParaIds: NO_IDS,
|
|
33
|
+
reservedCommentDurableIds: NO_IDS,
|
|
32
34
|
noteNumbering: DEFAULT_NOTE_NUMBERING,
|
|
33
35
|
specialNotes: NO_NOTE_KEYS,
|
|
34
36
|
reservedNoteKeys: NO_NOTE_KEYS
|
|
@@ -41,6 +43,9 @@ function reservedParaIds(session) {
|
|
|
41
43
|
for (const extension of session.comments.extendedOrdered) {
|
|
42
44
|
paraIds.add(extension.paraId);
|
|
43
45
|
}
|
|
46
|
+
for (const entry of session.comments.ids.ordered) {
|
|
47
|
+
paraIds.add(entry.paraId);
|
|
48
|
+
}
|
|
44
49
|
return paraIds;
|
|
45
50
|
}
|
|
46
51
|
function formattingWithNewLists(formatting, doc) {
|
|
@@ -62,6 +67,7 @@ function editorDocumentOf(session, doc) {
|
|
|
62
67
|
defaultTabStopPt: session.defaultTabStopPt,
|
|
63
68
|
reservedCommentIds: new Set(session.comments.byId.keys()),
|
|
64
69
|
reservedCommentParaIds: reservedParaIds(session),
|
|
70
|
+
reservedCommentDurableIds: spentDurableIds(session.comments),
|
|
65
71
|
noteNumbering: session.noteNumbering,
|
|
66
72
|
specialNotes: session.specialNotes,
|
|
67
73
|
reservedNoteKeys: new Set(
|
|
@@ -105,11 +111,15 @@ function reservedCommentIds(state) {
|
|
|
105
111
|
function reservedCommentParaIds(state) {
|
|
106
112
|
return documentOf(state).reservedCommentParaIds;
|
|
107
113
|
}
|
|
114
|
+
function reservedCommentDurableIds(state) {
|
|
115
|
+
return documentOf(state).reservedCommentDurableIds;
|
|
116
|
+
}
|
|
108
117
|
export {
|
|
109
118
|
NO_DOCUMENT,
|
|
110
119
|
documentOf,
|
|
111
120
|
editorDocument,
|
|
112
121
|
editorDocumentOf,
|
|
122
|
+
reservedCommentDurableIds,
|
|
113
123
|
reservedCommentIds,
|
|
114
124
|
reservedCommentParaIds,
|
|
115
125
|
storyDocument
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// src/editor/plugins/commentDecorations.ts
|
|
2
2
|
import { Decoration, DecorationSet } from "prosemirror-view";
|
|
3
|
+
import { commentInstant } from "../../docx/comments/dates.js";
|
|
3
4
|
import { storyKey, storyOf, storyText } from "../../docx/story.js";
|
|
4
5
|
import { editorClassNames } from "../../styles/classNames.js";
|
|
5
6
|
import {
|
|
@@ -41,7 +42,10 @@ function commentsIn(doc) {
|
|
|
41
42
|
author: stringAttr(node.attrs.author),
|
|
42
43
|
authorId: stringAttr(node.attrs.authorId),
|
|
43
44
|
initials: stringAttr(node.attrs.initials),
|
|
44
|
-
date:
|
|
45
|
+
date: commentInstant(
|
|
46
|
+
stringAttr(node.attrs.date),
|
|
47
|
+
stringAttr(node.attrs.dateUtc)
|
|
48
|
+
),
|
|
45
49
|
text: bodyText(doc, id),
|
|
46
50
|
from: anchored ? start + 1 : point,
|
|
47
51
|
to: anchored ? end : point,
|
|
@@ -53,7 +57,7 @@ function commentsIn(doc) {
|
|
|
53
57
|
author: reply.author,
|
|
54
58
|
authorId: reply.authorId,
|
|
55
59
|
initials: reply.initials,
|
|
56
|
-
date: reply.date,
|
|
60
|
+
date: commentInstant(stringAttr(reply.date), stringAttr(reply.dateUtc)),
|
|
57
61
|
text: bodyText(doc, reply.id)
|
|
58
62
|
}))
|
|
59
63
|
};
|
|
@@ -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 (
|
|
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 (
|
|
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";
|