@folijs/core 0.3.0 → 0.4.0
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 +22 -0
- package/dist/state/commands.d.ts.map +1 -1
- package/dist/state/commands.js +32 -4
- package/dist/state/commands.js.map +1 -1
- package/dist/state/comments.d.ts +47 -0
- package/dist/state/comments.d.ts.map +1 -0
- package/dist/state/comments.js +304 -0
- package/dist/state/comments.js.map +1 -0
- package/dist/state/editor.d.ts +42 -1
- package/dist/state/editor.d.ts.map +1 -1
- package/dist/state/editor.js +30 -2
- package/dist/state/editor.js.map +1 -1
- package/dist/state/history.d.ts +1 -1
- package/dist/state/history.d.ts.map +1 -1
- package/dist/state/history.js +3 -2
- package/dist/state/history.js.map +1 -1
- package/dist/state/index.d.ts +4 -1
- package/dist/state/index.d.ts.map +1 -1
- package/dist/state/index.js +4 -1
- package/dist/state/index.js.map +1 -1
- package/dist/state/mark.d.ts +5 -2
- package/dist/state/mark.d.ts.map +1 -1
- package/dist/state/mark.js +20 -3
- package/dist/state/mark.js.map +1 -1
- package/dist/state/review.d.ts +43 -0
- package/dist/state/review.d.ts.map +1 -0
- package/dist/state/review.js +282 -0
- package/dist/state/review.js.map +1 -0
- package/dist/state/state.d.ts +2 -0
- package/dist/state/state.d.ts.map +1 -1
- package/dist/state/state.js +8 -1
- package/dist/state/state.js.map +1 -1
- package/dist/state/track.d.ts +47 -0
- package/dist/state/track.d.ts.map +1 -0
- package/dist/state/track.js +534 -0
- package/dist/state/track.js.map +1 -0
- package/dist/state/word.d.ts +30 -2
- package/dist/state/word.d.ts.map +1 -1
- package/dist/state/word.js +82 -2
- package/dist/state/word.js.map +1 -1
- package/dist/v3/schema.d.ts +2 -0
- package/dist/v3/schema.d.ts.map +1 -1
- package/dist/v3/schema.js +2 -0
- package/dist/v3/schema.js.map +1 -1
- package/package.json +1 -1
- package/src/state/commands.ts +30 -4
- package/src/state/comments.ts +342 -0
- package/src/state/editor.ts +95 -1
- package/src/state/history.ts +3 -2
- package/src/state/index.ts +40 -1
- package/src/state/mark.ts +20 -3
- package/src/state/review.ts +330 -0
- package/src/state/state.ts +8 -1
- package/src/state/track.ts +602 -0
- package/src/state/word.ts +88 -3
- package/src/v3/schema.ts +3 -0
package/src/state/editor.ts
CHANGED
|
@@ -26,12 +26,40 @@ import {
|
|
|
26
26
|
splitBlock,
|
|
27
27
|
toggleMark,
|
|
28
28
|
} from "./commands.js";
|
|
29
|
+
import {
|
|
30
|
+
addComment,
|
|
31
|
+
type CommentContent,
|
|
32
|
+
type CommentOptions,
|
|
33
|
+
commentsPlugin,
|
|
34
|
+
deleteAllComments,
|
|
35
|
+
deleteComment,
|
|
36
|
+
editComment,
|
|
37
|
+
replyToComment,
|
|
38
|
+
resolveComment,
|
|
39
|
+
} from "./comments.js";
|
|
29
40
|
import { setAlign, setHighlight, setTextColor } from "./format.js";
|
|
30
41
|
import { history, redo, undo } from "./history.js";
|
|
31
42
|
import { removeLink, setLink } from "./links.js";
|
|
32
43
|
import { liftListItem, listKeymap, sinkListItem, toggleList, wrapInList } from "./lists.js";
|
|
44
|
+
import {
|
|
45
|
+
acceptAllChanges,
|
|
46
|
+
acceptChange,
|
|
47
|
+
nextChange,
|
|
48
|
+
previousChange,
|
|
49
|
+
rejectAllChanges,
|
|
50
|
+
rejectChange,
|
|
51
|
+
} from "./review.js";
|
|
33
52
|
import { TextSelection } from "./selection.js";
|
|
34
53
|
import { type Command, EditorState, type Plugin, type Transaction } from "./state.js";
|
|
54
|
+
import {
|
|
55
|
+
type ReviewView,
|
|
56
|
+
setReviewView,
|
|
57
|
+
setTrackChanges,
|
|
58
|
+
type TrackState,
|
|
59
|
+
trackPlugin,
|
|
60
|
+
trackState,
|
|
61
|
+
trackTransaction,
|
|
62
|
+
} from "./track.js";
|
|
35
63
|
import { type WordEnvelope, wordContext, wordPlugin } from "./word.js";
|
|
36
64
|
import type { ListKind } from "./wordformat.js";
|
|
37
65
|
|
|
@@ -46,6 +74,15 @@ export interface EditorOptions {
|
|
|
46
74
|
document?: EditorDocument;
|
|
47
75
|
/** Run before the built-in plugins, so their keymaps take precedence. */
|
|
48
76
|
plugins?: readonly Plugin[];
|
|
77
|
+
/** Who edits: tracked changes are made in this name. Default "Author". */
|
|
78
|
+
author?: string;
|
|
79
|
+
/**
|
|
80
|
+
* Record edits as tracked changes from the start (a `.docx`'s `w:trackRevisions`:
|
|
81
|
+
* `DocxImport.trackRevisions`). Schema v3 only. Default false.
|
|
82
|
+
*/
|
|
83
|
+
trackChanges?: boolean;
|
|
84
|
+
/** How the editor's views show tracked changes at first (default `markup`). */
|
|
85
|
+
reviewView?: ReviewView;
|
|
49
86
|
}
|
|
50
87
|
|
|
51
88
|
export type EditorListener = (state: EditorState, transactions: readonly Transaction[]) => void;
|
|
@@ -103,7 +140,37 @@ export interface Editor {
|
|
|
103
140
|
setTextColor(color: string | null): boolean;
|
|
104
141
|
/** Highlight (`#rrggbb`) of the selected or next typed text; null removes it. */
|
|
105
142
|
setHighlight(color: string | null): boolean;
|
|
143
|
+
/** Turns tracked changes on or off (`author` changes who makes them). */
|
|
144
|
+
setTrackChanges(on: boolean, author?: string): boolean;
|
|
145
|
+
/** How the editor's views show tracked changes. */
|
|
146
|
+
setReviewView(view: ReviewView): boolean;
|
|
147
|
+
/** Accepts the change at the caret, or the changes in the selection. */
|
|
148
|
+
acceptChange(): boolean;
|
|
149
|
+
/** Rejects the change at the caret, or the changes in the selection. */
|
|
150
|
+
rejectChange(): boolean;
|
|
151
|
+
/** Accepts every change, or every change of `author`. */
|
|
152
|
+
acceptAllChanges(author?: string): boolean;
|
|
153
|
+
/** Rejects every change, or every change of `author`. */
|
|
154
|
+
rejectAllChanges(author?: string): boolean;
|
|
155
|
+
/** Selects the next change after the selection. */
|
|
156
|
+
nextChange(): boolean;
|
|
157
|
+
/** Selects the previous change before the selection. */
|
|
158
|
+
previousChange(): boolean;
|
|
159
|
+
/** Comments the selection (at a caret, the point there). */
|
|
160
|
+
addComment(content: CommentContent, options?: CommentOptions): boolean;
|
|
161
|
+
/** Replies to a comment's thread. */
|
|
162
|
+
replyToComment(id: number, content: CommentContent, options?: CommentOptions): boolean;
|
|
163
|
+
/** Replaces a comment's content. */
|
|
164
|
+
editComment(id: number, content: CommentContent): boolean;
|
|
165
|
+
/** Resolves (`done` false: reopens) a comment's thread. */
|
|
166
|
+
resolveComment(id: number, done?: boolean): boolean;
|
|
167
|
+
/** Deletes a comment, its replies and their anchors. */
|
|
168
|
+
deleteComment(id: number): boolean;
|
|
169
|
+
/** Deletes every comment. */
|
|
170
|
+
deleteAllComments(): boolean;
|
|
106
171
|
};
|
|
172
|
+
/** Whether edits are recorded as tracked changes. */
|
|
173
|
+
readonly trackingChanges: boolean;
|
|
107
174
|
isMarkActive(type: string): boolean;
|
|
108
175
|
/** The current document (schema v3). */
|
|
109
176
|
getDocument(): FoliDocument;
|
|
@@ -147,12 +214,18 @@ function load(document: EditorDocument): Loaded {
|
|
|
147
214
|
export function createEditor({
|
|
148
215
|
document = createDocument(),
|
|
149
216
|
plugins = [],
|
|
217
|
+
author = "Author",
|
|
218
|
+
trackChanges = false,
|
|
219
|
+
reviewView = "markup",
|
|
150
220
|
}: EditorOptions = {}): Editor {
|
|
151
221
|
let loaded = load(document);
|
|
222
|
+
let track: TrackState = { on: trackChanges, author, view: reviewView };
|
|
152
223
|
const pluginsFor = ({ envelope }: Loaded) => [
|
|
153
224
|
...plugins,
|
|
154
225
|
history(),
|
|
155
226
|
wordPlugin(envelope),
|
|
227
|
+
trackPlugin(track),
|
|
228
|
+
commentsPlugin(),
|
|
156
229
|
listKeymap(),
|
|
157
230
|
baseKeymap(),
|
|
158
231
|
];
|
|
@@ -163,7 +236,10 @@ export function createEditor({
|
|
|
163
236
|
for (const listener of listeners) listener(state, transactions);
|
|
164
237
|
};
|
|
165
238
|
const dispatch = (tr: Transaction) => {
|
|
166
|
-
|
|
239
|
+
// With tracking on, the edit is rebuilt as tracked changes; one that cannot be is not made.
|
|
240
|
+
const tracked = trackTransaction(state, tr);
|
|
241
|
+
if (!tracked) return;
|
|
242
|
+
const result = state.applyTransaction(tracked);
|
|
167
243
|
state = result.state;
|
|
168
244
|
if (result.transactions.length) notify(result.transactions);
|
|
169
245
|
};
|
|
@@ -214,6 +290,23 @@ export function createEditor({
|
|
|
214
290
|
setAlign: (align) => run(setAlign(align)),
|
|
215
291
|
setTextColor: (color) => run(setTextColor(color)),
|
|
216
292
|
setHighlight: (color) => run(setHighlight(color)),
|
|
293
|
+
setTrackChanges: (on, name) => run(setTrackChanges(on, name)),
|
|
294
|
+
setReviewView: (view) => run(setReviewView(view)),
|
|
295
|
+
acceptChange: () => run(acceptChange),
|
|
296
|
+
rejectChange: () => run(rejectChange),
|
|
297
|
+
acceptAllChanges: (name) => run(acceptAllChanges(name)),
|
|
298
|
+
rejectAllChanges: (name) => run(rejectAllChanges(name)),
|
|
299
|
+
nextChange: () => run(nextChange),
|
|
300
|
+
previousChange: () => run(previousChange),
|
|
301
|
+
addComment: (content, options) => run(addComment(content, options)),
|
|
302
|
+
replyToComment: (id, content, options) => run(replyToComment(id, content, options)),
|
|
303
|
+
editComment: (id, content) => run(editComment(id, content)),
|
|
304
|
+
resolveComment: (id, done) => run(resolveComment(id, done)),
|
|
305
|
+
deleteComment: (id) => run(deleteComment(id)),
|
|
306
|
+
deleteAllComments: () => run(deleteAllComments),
|
|
307
|
+
},
|
|
308
|
+
get trackingChanges() {
|
|
309
|
+
return trackState(state)?.on ?? false;
|
|
217
310
|
},
|
|
218
311
|
isMarkActive: (type) => isMarkActive(state, type),
|
|
219
312
|
getDocument: () => ({
|
|
@@ -221,6 +314,7 @@ export function createEditor({
|
|
|
221
314
|
content: state.doc.toJSON(),
|
|
222
315
|
}),
|
|
223
316
|
setDocument(next) {
|
|
317
|
+
track = trackState(state) ?? track;
|
|
224
318
|
loaded = load(next);
|
|
225
319
|
state = EditorState.create({ doc: loaded.doc, plugins: pluginsFor(loaded) });
|
|
226
320
|
notify([]);
|
package/src/state/history.ts
CHANGED
|
@@ -33,7 +33,7 @@ const historyKey = new PluginKey<HistoryState>("history");
|
|
|
33
33
|
/** Meta set on undo/redo transactions: which operation produced them. */
|
|
34
34
|
const historyOp = "historyOp";
|
|
35
35
|
|
|
36
|
-
/** Set this meta on a transaction to
|
|
36
|
+
/** Set this meta on a transaction to make it an undo step of its own (no merging either side). */
|
|
37
37
|
export const closeHistory = "closeHistory";
|
|
38
38
|
|
|
39
39
|
const EMPTY: HistoryState = { done: [], undone: [], prevTime: 0, prevRanges: null };
|
|
@@ -99,7 +99,8 @@ export function history({ newGroupDelay = 500, depth = 100 }: HistoryOptions = {
|
|
|
99
99
|
return {
|
|
100
100
|
done: done.slice(-depth),
|
|
101
101
|
undone: [],
|
|
102
|
-
|
|
102
|
+
// A closed change is a step of its own on both sides: what follows does not join it.
|
|
103
|
+
prevTime: root ? hist.prevTime : tr.getMeta(closeHistory) ? 0 : tr.time,
|
|
103
104
|
prevRanges: root ? mapRanges(hist.prevRanges, tr.mapping) : changedRanges(tr),
|
|
104
105
|
};
|
|
105
106
|
},
|
package/src/state/index.ts
CHANGED
|
@@ -17,6 +17,17 @@ export {
|
|
|
17
17
|
splitBlock,
|
|
18
18
|
toggleMark,
|
|
19
19
|
} from "./commands.js";
|
|
20
|
+
export {
|
|
21
|
+
addComment,
|
|
22
|
+
type CommentContent,
|
|
23
|
+
type CommentOptions,
|
|
24
|
+
commentAnchors,
|
|
25
|
+
deleteAllComments,
|
|
26
|
+
deleteComment,
|
|
27
|
+
editComment,
|
|
28
|
+
replyToComment,
|
|
29
|
+
resolveComment,
|
|
30
|
+
} from "./comments.js";
|
|
20
31
|
export {
|
|
21
32
|
createEditor,
|
|
22
33
|
type Editor,
|
|
@@ -63,6 +74,18 @@ export {
|
|
|
63
74
|
wrapInList,
|
|
64
75
|
} from "./lists.js";
|
|
65
76
|
export { typedMarks } from "./mark.js";
|
|
77
|
+
export {
|
|
78
|
+
acceptAllChanges,
|
|
79
|
+
acceptChange,
|
|
80
|
+
type Change,
|
|
81
|
+
type ChangeFilter,
|
|
82
|
+
type ChangeKind,
|
|
83
|
+
nextChange,
|
|
84
|
+
previousChange,
|
|
85
|
+
rejectAllChanges,
|
|
86
|
+
rejectChange,
|
|
87
|
+
trackedChanges,
|
|
88
|
+
} from "./review.js";
|
|
66
89
|
export {
|
|
67
90
|
AllSelection,
|
|
68
91
|
marksAt,
|
|
@@ -81,7 +104,23 @@ export {
|
|
|
81
104
|
type PluginSpec,
|
|
82
105
|
Transaction,
|
|
83
106
|
} from "./state.js";
|
|
84
|
-
export {
|
|
107
|
+
export {
|
|
108
|
+
type ReviewView,
|
|
109
|
+
setReviewView,
|
|
110
|
+
setTrackChanges,
|
|
111
|
+
type TrackState,
|
|
112
|
+
trackState,
|
|
113
|
+
trackTransaction,
|
|
114
|
+
untracked,
|
|
115
|
+
} from "./track.js";
|
|
116
|
+
export {
|
|
117
|
+
CommentStep,
|
|
118
|
+
setComments,
|
|
119
|
+
type WordContext,
|
|
120
|
+
type WordEnvelope,
|
|
121
|
+
withEnvelope,
|
|
122
|
+
wordContext,
|
|
123
|
+
} from "./word.js";
|
|
85
124
|
export {
|
|
86
125
|
copiedDefinitions,
|
|
87
126
|
fitPaste,
|
package/src/state/mark.ts
CHANGED
|
@@ -13,14 +13,31 @@ import type { Transform } from "../transform/transform.js";
|
|
|
13
13
|
*/
|
|
14
14
|
export const MARK_ATTRS = ["section", "revision", "formatChange", "attributes"] as const;
|
|
15
15
|
|
|
16
|
+
/** Inline nodes that stand between runs: bookmarks and comment ranges. */
|
|
17
|
+
const MARKERS = new Set(["bookmarkStart", "bookmarkEnd", "commentStart", "commentEnd"]);
|
|
18
|
+
|
|
16
19
|
/**
|
|
17
20
|
* After a join, or a deletion across paragraphs, the merged paragraph at `pos` ends with the
|
|
18
21
|
* last paragraph's mark, as in Word: it takes that paragraph's mark attributes (deleting a
|
|
19
|
-
* section break merges its section into the next one).
|
|
22
|
+
* section break merges its section into the next one). When the first paragraph left nothing
|
|
23
|
+
* (it was empty, or deleted from its start), it is gone: the last one stays whole, as in Word.
|
|
24
|
+
* `tail` is where the content kept of the last one starts in it (a deletion into it). Nothing for
|
|
25
|
+
* a schema without them (v2).
|
|
20
26
|
*/
|
|
21
|
-
export function keepLastMark(tr: Transform, pos: number, last: Node): void {
|
|
27
|
+
export function keepLastMark(tr: Transform, pos: number, last: Node, tail = 0): void {
|
|
22
28
|
const merged = tr.doc.nodeAt(pos);
|
|
23
|
-
if (!merged?.isTextblock) return;
|
|
29
|
+
if (!merged?.isTextblock || !Object.hasOwn(merged.attrs, "revision")) return;
|
|
30
|
+
// What the first paragraph left, bookmarks and comment ranges aside (headings often start
|
|
31
|
+
// with a bookmark).
|
|
32
|
+
let left = false;
|
|
33
|
+
merged.content.cut(0, merged.content.size - (last.content.size - tail)).forEach((child) => {
|
|
34
|
+
if (!MARKERS.has(child.type.name)) left = true;
|
|
35
|
+
});
|
|
36
|
+
if (!left) {
|
|
37
|
+
if (merged.type !== last.type || JSON.stringify(merged.attrs) !== JSON.stringify(last.attrs))
|
|
38
|
+
tr.setNodeMarkup(pos, last.type, last.attrs);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
24
41
|
const attrs = { ...merged.attrs };
|
|
25
42
|
let changed = false;
|
|
26
43
|
for (const key of MARK_ATTRS) {
|
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Accepting and rejecting tracked changes (9.4), as Word does. A change is a run of content with
|
|
3
|
+
* one `insertion`, `deletion` or `formatChange` mark, a paragraph mark inserted or deleted
|
|
4
|
+
* (`revision`), or a paragraph's recorded formatting (`formatChange`). Accepting an insertion or
|
|
5
|
+
* a formatting change drops its record; accepting a deletion removes the content (a deleted
|
|
6
|
+
* paragraph mark joins the paragraph with the next, keeping the first one's properties, as an
|
|
7
|
+
* untracked join). Rejecting does the reverse: inserted content goes (an inserted paragraph mark
|
|
8
|
+
* joins the paragraph with the next, which keeps its own properties: the original mark's), a
|
|
9
|
+
* deletion's record is dropped, recorded formatting comes back. Both ends of a tracked move go
|
|
10
|
+
* together. These edits are not tracked themselves, and undo takes them back.
|
|
11
|
+
*/
|
|
12
|
+
import type { Attrs, Mark, Node } from "../model/index.js";
|
|
13
|
+
import { RUN_PROPERTY_MARKS } from "../v3/schema.js";
|
|
14
|
+
import { closeHistory } from "./history.js";
|
|
15
|
+
import { MARK_ATTRS } from "./mark.js";
|
|
16
|
+
import { TextSelection } from "./selection.js";
|
|
17
|
+
import type { Command, EditorState, Transaction } from "./state.js";
|
|
18
|
+
import { RECORDED, trackState, untracked } from "./track.js";
|
|
19
|
+
|
|
20
|
+
export type ChangeKind =
|
|
21
|
+
| "insertion"
|
|
22
|
+
| "deletion"
|
|
23
|
+
| "formatting"
|
|
24
|
+
| "paragraphInsertion"
|
|
25
|
+
| "paragraphDeletion"
|
|
26
|
+
| "paragraphFormatting";
|
|
27
|
+
|
|
28
|
+
/** One tracked change of the document, where it is. */
|
|
29
|
+
export interface Change {
|
|
30
|
+
readonly kind: ChangeKind;
|
|
31
|
+
readonly id: number;
|
|
32
|
+
readonly author: string;
|
|
33
|
+
readonly date: string | null;
|
|
34
|
+
/** The content it covers; for a paragraph mark, the paragraph's closing token. */
|
|
35
|
+
readonly from: number;
|
|
36
|
+
readonly to: number;
|
|
37
|
+
/** A tracked move's name, shared by both ends (`w:moveFrom`, `w:moveTo`). */
|
|
38
|
+
readonly move: string | null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface ChangeFilter {
|
|
42
|
+
/** Only this author's changes. */
|
|
43
|
+
author?: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const MARK_KINDS: Record<string, ChangeKind> = {
|
|
47
|
+
insertion: "insertion",
|
|
48
|
+
deletion: "deletion",
|
|
49
|
+
formatChange: "formatting",
|
|
50
|
+
};
|
|
51
|
+
const RUN_MARKS = new Set<string>(RUN_PROPERTY_MARKS);
|
|
52
|
+
|
|
53
|
+
interface Revision {
|
|
54
|
+
id: number;
|
|
55
|
+
author: string;
|
|
56
|
+
date?: string | null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Every tracked change of `doc`, in document order. */
|
|
60
|
+
export function trackedChanges(doc: Node): Change[] {
|
|
61
|
+
const changes: Change[] = [];
|
|
62
|
+
const flush = (run: { mark: Mark; from: number; to: number }) => {
|
|
63
|
+
const attrs = run.mark.attrs;
|
|
64
|
+
changes.push({
|
|
65
|
+
kind: MARK_KINDS[run.mark.type.name] as ChangeKind,
|
|
66
|
+
id: attrs.id as number,
|
|
67
|
+
author: attrs.author as string,
|
|
68
|
+
date: (attrs.date as string | null) ?? null,
|
|
69
|
+
from: run.from,
|
|
70
|
+
to: run.to,
|
|
71
|
+
move: (attrs.move as string | null | undefined) ?? null,
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
doc.forEach((block, offset) => {
|
|
75
|
+
if (!block.isTextblock) return;
|
|
76
|
+
const change = block.attrs.formatChange as Revision | null;
|
|
77
|
+
if (change) changes.push(paragraphChange("paragraphFormatting", change, offset, block));
|
|
78
|
+
// Runs of content with the same change mark, extended while the next node has it too.
|
|
79
|
+
let open: { mark: Mark; from: number; to: number }[] = [];
|
|
80
|
+
block.forEach((inline, at) => {
|
|
81
|
+
const pos = offset + 1 + at;
|
|
82
|
+
const next: typeof open = [];
|
|
83
|
+
for (const mark of inline.marks) {
|
|
84
|
+
if (!MARK_KINDS[mark.type.name]) continue;
|
|
85
|
+
const run = open.find((r) => r.to === pos && r.mark.eq(mark));
|
|
86
|
+
if (run) run.to = pos + inline.nodeSize;
|
|
87
|
+
next.push(run ?? { mark, from: pos, to: pos + inline.nodeSize });
|
|
88
|
+
}
|
|
89
|
+
for (const run of open) if (!next.includes(run)) flush(run);
|
|
90
|
+
open = next;
|
|
91
|
+
});
|
|
92
|
+
for (const run of open) flush(run);
|
|
93
|
+
const revision = block.attrs.revision as { insertion?: Revision; deletion?: Revision } | null;
|
|
94
|
+
if (revision?.insertion)
|
|
95
|
+
changes.push(paragraphChange("paragraphInsertion", revision.insertion, offset, block));
|
|
96
|
+
if (revision?.deletion)
|
|
97
|
+
changes.push(paragraphChange("paragraphDeletion", revision.deletion, offset, block));
|
|
98
|
+
});
|
|
99
|
+
return changes.sort((a, b) => a.from - b.from || a.to - b.to);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** A paragraph's change: its mark (the closing token), or its content for its formatting. */
|
|
103
|
+
function paragraphChange(
|
|
104
|
+
kind: ChangeKind,
|
|
105
|
+
revision: Revision,
|
|
106
|
+
offset: number,
|
|
107
|
+
block: Node,
|
|
108
|
+
): Change {
|
|
109
|
+
const end = offset + block.nodeSize;
|
|
110
|
+
return {
|
|
111
|
+
kind,
|
|
112
|
+
id: revision.id,
|
|
113
|
+
author: revision.author,
|
|
114
|
+
date: revision.date ?? null,
|
|
115
|
+
from: kind === "paragraphFormatting" ? offset + 1 : end - 1,
|
|
116
|
+
to: kind === "paragraphFormatting" ? end - 1 : end,
|
|
117
|
+
move: null,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
const INLINE: ReadonlySet<ChangeKind> = new Set(["insertion", "deletion", "formatting"]);
|
|
121
|
+
/** Inline nodes no change marks: they stay when the text around them goes. */
|
|
122
|
+
const MARKERS = new Set(["bookmarkStart", "bookmarkEnd", "commentStart", "commentEnd"]);
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The changes `from..to` touches, with their moves' other ends. At a caret: the ones around it,
|
|
126
|
+
* the paragraph's formatting only when nothing else is. In a selection, changes to content only
|
|
127
|
+
* as far as the selection goes (Word accepts the selected part).
|
|
128
|
+
*/
|
|
129
|
+
function changesIn(doc: Node, from: number, to: number, filter: ChangeFilter = {}): Change[] {
|
|
130
|
+
const all = trackedChanges(doc).filter((c) => !filter.author || c.author === filter.author);
|
|
131
|
+
let picked: Change[];
|
|
132
|
+
if (from === to) {
|
|
133
|
+
const around = all.filter((c) => c.from <= from && from <= c.to);
|
|
134
|
+
const own = around.filter((c) => c.kind !== "paragraphFormatting");
|
|
135
|
+
picked = own.length ? own : around;
|
|
136
|
+
} else
|
|
137
|
+
picked = all
|
|
138
|
+
.filter((c) => c.from < to && from < c.to)
|
|
139
|
+
.map((c) =>
|
|
140
|
+
INLINE.has(c.kind) ? { ...c, from: Math.max(c.from, from), to: Math.min(c.to, to) } : c,
|
|
141
|
+
);
|
|
142
|
+
const moves = new Set(picked.map((c) => c.move).filter((m) => m !== null));
|
|
143
|
+
const partners = all.filter(
|
|
144
|
+
(c) =>
|
|
145
|
+
c.move !== null &&
|
|
146
|
+
moves.has(c.move) &&
|
|
147
|
+
!picked.some((p) => p.id === c.id && p.kind === c.kind),
|
|
148
|
+
);
|
|
149
|
+
return [...picked, ...partners];
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Applies accepting (or rejecting) `changes` to `tr`. */
|
|
153
|
+
function resolve(tr: Transaction, changes: readonly Change[], accept: boolean): void {
|
|
154
|
+
const schema = tr.doc.type.schema;
|
|
155
|
+
const removed: [number, number][] = [];
|
|
156
|
+
const joins: { pos: number; keepFirst: boolean }[] = [];
|
|
157
|
+
for (const change of changes) {
|
|
158
|
+
const { kind, from, to } = change;
|
|
159
|
+
const markType = schema.marks[kind === "formatting" ? "formatChange" : kind];
|
|
160
|
+
switch (kind) {
|
|
161
|
+
case "insertion":
|
|
162
|
+
case "deletion": {
|
|
163
|
+
if (!markType) break;
|
|
164
|
+
if ((kind === "insertion") === accept) removeMarkOf(tr, from, to, markType.name, change.id);
|
|
165
|
+
else removed.push([from, to]);
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
case "formatting": {
|
|
169
|
+
tr.doc.nodesBetween(from, to, (node, pos) => {
|
|
170
|
+
if (!node.isInline) return true;
|
|
171
|
+
const record = node.marks.find((m) => m.type.name === "formatChange");
|
|
172
|
+
if (!record || record.attrs.id !== change.id) return false;
|
|
173
|
+
const s = Math.max(pos, from);
|
|
174
|
+
const e = Math.min(pos + node.nodeSize, to);
|
|
175
|
+
if (!accept) {
|
|
176
|
+
for (const mark of node.marks)
|
|
177
|
+
if (RUN_MARKS.has(mark.type.name)) tr.removeMark(s, e, mark);
|
|
178
|
+
for (const json of record.attrs.previous as { type: string; attrs?: Attrs }[])
|
|
179
|
+
tr.addMark(s, e, schema.mark(json.type, json.attrs ?? null));
|
|
180
|
+
}
|
|
181
|
+
tr.removeMark(s, e, record);
|
|
182
|
+
return false;
|
|
183
|
+
});
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
case "paragraphFormatting": {
|
|
187
|
+
const start = tr.doc.resolve(from).before();
|
|
188
|
+
const node = tr.doc.nodeAt(start);
|
|
189
|
+
if (!node?.isTextblock) break;
|
|
190
|
+
const attrs: Attrs = { ...node.attrs, formatChange: null };
|
|
191
|
+
if (!accept) {
|
|
192
|
+
const previous = (node.attrs.formatChange as { previous: Attrs }).previous;
|
|
193
|
+
for (const key of RECORDED)
|
|
194
|
+
(attrs as Record<string, unknown>)[key] = previous[key] ?? null;
|
|
195
|
+
}
|
|
196
|
+
tr.setNodeMarkup(start, node.type, attrs);
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
199
|
+
case "paragraphInsertion":
|
|
200
|
+
case "paragraphDeletion": {
|
|
201
|
+
const start = tr.doc.resolve(from).before();
|
|
202
|
+
const node = tr.doc.nodeAt(start);
|
|
203
|
+
if (!node?.isTextblock) break;
|
|
204
|
+
const revision = { ...(node.attrs.revision as Record<string, unknown>) };
|
|
205
|
+
const goes = (kind === "paragraphDeletion") === accept;
|
|
206
|
+
delete revision[kind === "paragraphInsertion" ? "insertion" : "deletion"];
|
|
207
|
+
const next = Object.keys(revision).length ? revision : null;
|
|
208
|
+
tr.setNodeMarkup(start, node.type, { ...node.attrs, revision: next });
|
|
209
|
+
// The mark goes: the paragraph joins the next one, when there is one to join.
|
|
210
|
+
const end = start + node.nodeSize;
|
|
211
|
+
// A paragraph whose text goes too is removed whole: the next one stays as it is.
|
|
212
|
+
const emptied = covered(node, start + 1, removed);
|
|
213
|
+
if (goes && tr.doc.resolve(end).nodeAfter?.isTextblock)
|
|
214
|
+
joins.push({ pos: end, keepFirst: accept && !emptied });
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
// Content removed and paragraphs joined, from the end so earlier positions stay.
|
|
220
|
+
const ops = [
|
|
221
|
+
...removed.map(([s, e]) => ({ at: s, run: () => tr.delete(s, e) })),
|
|
222
|
+
...joins.map(({ pos, keepFirst }) => ({ at: pos - 1, run: () => join(tr, pos, keepFirst) })),
|
|
223
|
+
].sort((a, b) => b.at - a.at);
|
|
224
|
+
for (const op of ops) op.run();
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** Whether every child of `node` (content at `start`) but markers lies in `removed`. */
|
|
228
|
+
function covered(node: Node, start: number, removed: readonly [number, number][]): boolean {
|
|
229
|
+
let all = true;
|
|
230
|
+
node.forEach((child, offset) => {
|
|
231
|
+
const pos = start + offset;
|
|
232
|
+
if (MARKERS.has(child.type.name)) return;
|
|
233
|
+
if (!removed.some(([s, e]) => s <= pos && pos + child.nodeSize <= e)) all = false;
|
|
234
|
+
});
|
|
235
|
+
return all;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** Removes `type`'s mark with `id` from `from..to`. */
|
|
239
|
+
function removeMarkOf(tr: Transaction, from: number, to: number, type: string, id: number): void {
|
|
240
|
+
tr.doc.nodesBetween(from, to, (node, pos) => {
|
|
241
|
+
if (!node.isInline) return true;
|
|
242
|
+
const mark = node.marks.find((m) => m.type.name === type && m.attrs.id === id);
|
|
243
|
+
if (mark) tr.removeMark(Math.max(pos, from), Math.min(pos + node.nodeSize, to), mark);
|
|
244
|
+
return false;
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Joins the paragraph ending at `pos` with the next one. Accepting a deleted mark keeps the first
|
|
250
|
+
* one's properties and the last one's mark (as an untracked join), unless the first one is left
|
|
251
|
+
* with nothing (then it is gone); rejecting an inserted mark keeps the last one whole: its mark
|
|
252
|
+
* is the original's.
|
|
253
|
+
*/
|
|
254
|
+
function join(tr: Transaction, pos: number, keepFirst: boolean): void {
|
|
255
|
+
const $pos = tr.doc.resolve(pos);
|
|
256
|
+
const first = $pos.nodeBefore;
|
|
257
|
+
const last = $pos.nodeAfter;
|
|
258
|
+
if (!first?.isTextblock || !last?.isTextblock) return;
|
|
259
|
+
const start = pos - first.nodeSize;
|
|
260
|
+
tr.join(pos);
|
|
261
|
+
const attrs: Attrs = keepFirst ? { ...first.attrs } : { ...last.attrs };
|
|
262
|
+
if (keepFirst)
|
|
263
|
+
for (const key of MARK_ATTRS) (attrs as Record<string, unknown>)[key] = last.attrs[key] ?? null;
|
|
264
|
+
tr.setNodeMarkup(start, first.type, attrs);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function command(
|
|
268
|
+
accept: boolean,
|
|
269
|
+
range: (state: EditorState) => [number, number] | null,
|
|
270
|
+
filter?: ChangeFilter,
|
|
271
|
+
): Command {
|
|
272
|
+
return (state, dispatch) => {
|
|
273
|
+
if (!state.schema.marks.insertion) return false;
|
|
274
|
+
const r = range(state);
|
|
275
|
+
if (!r) return false;
|
|
276
|
+
const changes = changesIn(state.doc, r[0], r[1], filter);
|
|
277
|
+
if (!changes.length) return false;
|
|
278
|
+
if (dispatch) {
|
|
279
|
+
// Its own undo step, never merged with the typing before it.
|
|
280
|
+
const tr = state.tr.setMeta(untracked, true).setMeta(closeHistory, true);
|
|
281
|
+
resolve(tr, changes, accept);
|
|
282
|
+
dispatch(tr);
|
|
283
|
+
}
|
|
284
|
+
return true;
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const selected = (state: EditorState): [number, number] => [
|
|
289
|
+
state.selection.from,
|
|
290
|
+
state.selection.to,
|
|
291
|
+
];
|
|
292
|
+
const everything = (state: EditorState): [number, number] => [0, state.doc.content.size];
|
|
293
|
+
|
|
294
|
+
/** Accepts the change at the caret, or the changes in the selection. */
|
|
295
|
+
export const acceptChange: Command = command(true, selected);
|
|
296
|
+
/** Rejects the change at the caret, or the changes in the selection. */
|
|
297
|
+
export const rejectChange: Command = command(false, selected);
|
|
298
|
+
/** Accepts every change (of `author`, if given). */
|
|
299
|
+
export const acceptAllChanges = (author?: string): Command =>
|
|
300
|
+
command(true, everything, author === undefined ? {} : { author });
|
|
301
|
+
/** Rejects every change (of `author`, if given). */
|
|
302
|
+
export const rejectAllChanges = (author?: string): Command =>
|
|
303
|
+
command(false, everything, author === undefined ? {} : { author });
|
|
304
|
+
|
|
305
|
+
/** Selects the next (or previous) change after (before) the selection. */
|
|
306
|
+
function goTo(forward: boolean): Command {
|
|
307
|
+
return (state, dispatch) => {
|
|
308
|
+
// Where the review view hides deleted content, only what it shows.
|
|
309
|
+
const hidden = (trackState(state)?.view ?? "markup") !== "markup";
|
|
310
|
+
const changes = trackedChanges(state.doc).filter(
|
|
311
|
+
(c) => !hidden || (c.kind !== "deletion" && c.kind !== "paragraphDeletion"),
|
|
312
|
+
);
|
|
313
|
+
const { from, to } = state.selection;
|
|
314
|
+
// In document order (by start, then end), the first one after (before) the selection.
|
|
315
|
+
const target = forward
|
|
316
|
+
? changes.find((c) => c.from > from || (c.from === from && c.to > to))
|
|
317
|
+
: [...changes].reverse().find((c) => c.from < from || (c.from === from && c.to < to));
|
|
318
|
+
if (!target) return false;
|
|
319
|
+
if (dispatch) {
|
|
320
|
+
// A paragraph mark: the caret at the end of its paragraph.
|
|
321
|
+
const mark = target.kind === "paragraphInsertion" || target.kind === "paragraphDeletion";
|
|
322
|
+
const head = mark ? target.from : target.to;
|
|
323
|
+
dispatch(state.tr.setSelection(TextSelection.create(state.doc, target.from, head)));
|
|
324
|
+
}
|
|
325
|
+
return true;
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
export const nextChange: Command = goTo(true);
|
|
330
|
+
export const previousChange: Command = goTo(false);
|
package/src/state/state.ts
CHANGED
|
@@ -111,6 +111,13 @@ export class Transaction extends Transform {
|
|
|
111
111
|
return this.meta.get(key);
|
|
112
112
|
}
|
|
113
113
|
|
|
114
|
+
/** @internal Takes `other`'s metadata and time: a transaction rebuilt from it (tracking). */
|
|
115
|
+
copyMeta(other: Transaction): this {
|
|
116
|
+
for (const [key, value] of other.meta) this.meta.set(key, value);
|
|
117
|
+
this.time = other.time;
|
|
118
|
+
return this;
|
|
119
|
+
}
|
|
120
|
+
|
|
114
121
|
get addToHistory(): boolean {
|
|
115
122
|
return this.meta.get("addToHistory") !== false;
|
|
116
123
|
}
|
|
@@ -148,7 +155,7 @@ export class Transaction extends Transform {
|
|
|
148
155
|
? $to.parent
|
|
149
156
|
: null;
|
|
150
157
|
this.delete(selection.from, selection.to);
|
|
151
|
-
if (last) keepLastMark(this, $from.before(), last);
|
|
158
|
+
if (last) keepLastMark(this, $from.before(), last, $to.parentOffset);
|
|
152
159
|
const caret = this.mapping.slice(start).map(selection.from, -1);
|
|
153
160
|
return this.setSelection(TextSelection.create(this.doc, caret));
|
|
154
161
|
}
|