@folijs/core 0.2.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 +45 -0
- package/README.md +1 -1
- package/dist/document.d.ts +10 -16
- package/dist/document.d.ts.map +1 -1
- package/dist/document.js +22 -22
- package/dist/document.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/model/fragment.d.ts +5 -2
- package/dist/model/fragment.d.ts.map +1 -1
- package/dist/model/fragment.js +5 -2
- package/dist/model/fragment.js.map +1 -1
- package/dist/model/node.d.ts +1 -1
- package/dist/model/node.d.ts.map +1 -1
- package/dist/model/node.js.map +1 -1
- package/dist/schema.d.ts +10 -1
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +11 -1
- package/dist/schema.js.map +1 -1
- package/dist/state/commands.d.ts +7 -6
- package/dist/state/commands.d.ts.map +1 -1
- package/dist/state/commands.js +162 -97
- 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 +67 -16
- package/dist/state/editor.d.ts.map +1 -1
- package/dist/state/editor.js +74 -18
- package/dist/state/editor.js.map +1 -1
- package/dist/state/format.d.ts +8 -22
- package/dist/state/format.d.ts.map +1 -1
- package/dist/state/format.js +16 -140
- package/dist/state/format.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 +11 -4
- package/dist/state/index.d.ts.map +1 -1
- package/dist/state/index.js +11 -4
- package/dist/state/index.js.map +1 -1
- package/dist/state/links.d.ts.map +1 -1
- package/dist/state/links.js +10 -2
- package/dist/state/links.js.map +1 -1
- package/dist/state/lists.d.ts +19 -32
- package/dist/state/lists.d.ts.map +1 -1
- package/dist/state/lists.js +23 -372
- package/dist/state/lists.js.map +1 -1
- package/dist/state/mark.d.ts +28 -0
- package/dist/state/mark.d.ts.map +1 -0
- package/dist/state/mark.js +57 -0
- package/dist/state/mark.js.map +1 -0
- 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 +18 -0
- 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 +100 -0
- package/dist/state/word.d.ts.map +1 -0
- package/dist/state/word.js +253 -0
- package/dist/state/word.js.map +1 -0
- package/dist/state/wordclip.d.ts +43 -0
- package/dist/state/wordclip.d.ts.map +1 -0
- package/dist/state/wordclip.js +412 -0
- package/dist/state/wordclip.js.map +1 -0
- package/dist/state/wordformat.d.ts +136 -0
- package/dist/state/wordformat.d.ts.map +1 -0
- package/dist/state/wordformat.js +846 -0
- package/dist/state/wordformat.js.map +1 -0
- package/dist/v3/check.d.ts +33 -0
- package/dist/v3/check.d.ts.map +1 -0
- package/dist/v3/check.js +90 -0
- package/dist/v3/check.js.map +1 -0
- package/dist/v3/document.d.ts +217 -0
- package/dist/v3/document.d.ts.map +1 -0
- package/dist/v3/document.js +225 -0
- package/dist/v3/document.js.map +1 -0
- package/dist/v3/index.d.ts +6 -0
- package/dist/v3/index.d.ts.map +1 -0
- package/dist/v3/index.js +6 -0
- package/dist/v3/index.js.map +1 -0
- package/dist/v3/migrate.d.ts +19 -0
- package/dist/v3/migrate.d.ts.map +1 -0
- package/dist/v3/migrate.js +227 -0
- package/dist/v3/migrate.js.map +1 -0
- package/dist/v3/resolve.d.ts +129 -0
- package/dist/v3/resolve.d.ts.map +1 -0
- package/dist/v3/resolve.js +534 -0
- package/dist/v3/resolve.js.map +1 -0
- package/dist/v3/schema.d.ts +29 -0
- package/dist/v3/schema.d.ts.map +1 -0
- package/dist/v3/schema.js +431 -0
- package/dist/v3/schema.js.map +1 -0
- package/dist/v3/units.d.ts +16 -0
- package/dist/v3/units.d.ts.map +1 -0
- package/dist/v3/units.js +28 -0
- package/dist/v3/units.js.map +1 -0
- package/package.json +1 -1
- package/src/document.ts +26 -26
- package/src/index.ts +3 -6
- package/src/model/fragment.ts +11 -3
- package/src/model/node.ts +6 -1
- package/src/schema.ts +13 -1
- package/src/state/commands.ts +173 -91
- package/src/state/comments.ts +342 -0
- package/src/state/editor.ts +176 -36
- package/src/state/format.ts +24 -122
- package/src/state/history.ts +3 -2
- package/src/state/index.ts +73 -3
- package/src/state/links.ts +10 -2
- package/src/state/lists.ts +31 -408
- package/src/state/mark.ts +64 -0
- package/src/state/review.ts +330 -0
- package/src/state/state.ts +18 -0
- package/src/state/track.ts +602 -0
- package/src/state/word.ts +330 -0
- package/src/state/wordclip.ts +445 -0
- package/src/state/wordformat.ts +982 -0
- package/src/v3/check.ts +119 -0
- package/src/v3/document.ts +456 -0
- package/src/v3/index.ts +33 -0
- package/src/v3/migrate.ts +267 -0
- package/src/v3/resolve.ts +646 -0
- package/src/v3/schema.ts +482 -0
- package/src/v3/units.ts +34 -0
|
@@ -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
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
} from "../model/index.js";
|
|
9
9
|
import type { Step } from "../transform/steps.js";
|
|
10
10
|
import { Transform, TransformError } from "../transform/transform.js";
|
|
11
|
+
import { keepLastMark } from "./mark.js";
|
|
11
12
|
import { AllSelection, NodeSelection, Selection, TextSelection } from "./selection.js";
|
|
12
13
|
|
|
13
14
|
/**
|
|
@@ -110,6 +111,13 @@ export class Transaction extends Transform {
|
|
|
110
111
|
return this.meta.get(key);
|
|
111
112
|
}
|
|
112
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
|
+
|
|
113
121
|
get addToHistory(): boolean {
|
|
114
122
|
return this.meta.get("addToHistory") !== false;
|
|
115
123
|
}
|
|
@@ -137,7 +145,17 @@ export class Transaction extends Transform {
|
|
|
137
145
|
this.replace(selection.from, selection.to, new Slice(Fragment.from(type.create()), 0, 0));
|
|
138
146
|
return this.setSelection(TextSelection.create(this.doc, selection.from + 1));
|
|
139
147
|
}
|
|
148
|
+
// Across paragraphs: the merged one ends with the last one's mark (schema v3: its section).
|
|
149
|
+
const { $to } = selection;
|
|
150
|
+
const last =
|
|
151
|
+
$from.depth > 0 &&
|
|
152
|
+
$from.parent !== $to.parent &&
|
|
153
|
+
$from.parent.isTextblock &&
|
|
154
|
+
$to.parent.isTextblock
|
|
155
|
+
? $to.parent
|
|
156
|
+
: null;
|
|
140
157
|
this.delete(selection.from, selection.to);
|
|
158
|
+
if (last) keepLastMark(this, $from.before(), last, $to.parentOffset);
|
|
141
159
|
const caret = this.mapping.slice(start).map(selection.from, -1);
|
|
142
160
|
return this.setSelection(TextSelection.create(this.doc, caret));
|
|
143
161
|
}
|