@folijs/core 0.3.0 → 0.5.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 +43 -0
- package/dist/model/schema.d.ts +9 -1
- package/dist/model/schema.d.ts.map +1 -1
- package/dist/model/schema.js +13 -3
- package/dist/model/schema.js.map +1 -1
- package/dist/state/commands.d.ts +8 -1
- package/dist/state/commands.d.ts.map +1 -1
- package/dist/state/commands.js +119 -38
- 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 +32 -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 +8 -2
- package/dist/state/index.d.ts.map +1 -1
- package/dist/state/index.js +8 -2
- package/dist/state/index.js.map +1 -1
- package/dist/state/links.d.ts.map +1 -1
- package/dist/state/links.js +30 -19
- package/dist/state/links.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/pictures.d.ts +51 -0
- package/dist/state/pictures.d.ts.map +1 -0
- package/dist/state/pictures.js +140 -0
- package/dist/state/pictures.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 +361 -0
- package/dist/state/review.js.map +1 -0
- package/dist/state/selection.d.ts +12 -1
- package/dist/state/selection.d.ts.map +1 -1
- package/dist/state/selection.js +12 -0
- package/dist/state/selection.js.map +1 -1
- 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/tablecommands.d.ts +77 -0
- package/dist/state/tablecommands.d.ts.map +1 -0
- package/dist/state/tablecommands.js +675 -0
- package/dist/state/tablecommands.js.map +1 -0
- package/dist/state/tables.d.ts +110 -0
- package/dist/state/tables.d.ts.map +1 -0
- package/dist/state/tables.js +729 -0
- package/dist/state/tables.js.map +1 -0
- package/dist/state/track.d.ts +47 -0
- package/dist/state/track.d.ts.map +1 -0
- package/dist/state/track.js +583 -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 +83 -3
- package/dist/state/word.js.map +1 -1
- package/dist/state/wordclip.d.ts.map +1 -1
- package/dist/state/wordclip.js +26 -9
- package/dist/state/wordclip.js.map +1 -1
- package/dist/state/wordformat.d.ts +9 -1
- package/dist/state/wordformat.d.ts.map +1 -1
- package/dist/state/wordformat.js +38 -16
- package/dist/state/wordformat.js.map +1 -1
- package/dist/v3/document.d.ts +114 -7
- package/dist/v3/document.d.ts.map +1 -1
- package/dist/v3/document.js +170 -13
- package/dist/v3/document.js.map +1 -1
- package/dist/v3/index.d.ts +2 -2
- package/dist/v3/index.d.ts.map +1 -1
- package/dist/v3/index.js +2 -2
- package/dist/v3/index.js.map +1 -1
- package/dist/v3/migrate.d.ts +2 -2
- package/dist/v3/migrate.d.ts.map +1 -1
- package/dist/v3/resolve.d.ts +50 -4
- package/dist/v3/resolve.d.ts.map +1 -1
- package/dist/v3/resolve.js +205 -15
- package/dist/v3/resolve.js.map +1 -1
- package/dist/v3/schema.d.ts +6 -0
- package/dist/v3/schema.d.ts.map +1 -1
- package/dist/v3/schema.js +168 -6
- package/dist/v3/schema.js.map +1 -1
- package/package.json +1 -1
- package/src/model/schema.ts +18 -2
- package/src/state/commands.ts +110 -32
- package/src/state/comments.ts +342 -0
- package/src/state/editor.ts +97 -1
- package/src/state/history.ts +3 -2
- package/src/state/index.ts +74 -1
- package/src/state/links.ts +27 -17
- package/src/state/mark.ts +20 -3
- package/src/state/pictures.ts +168 -0
- package/src/state/review.ts +412 -0
- package/src/state/selection.ts +15 -1
- package/src/state/state.ts +8 -1
- package/src/state/tablecommands.ts +741 -0
- package/src/state/tables.ts +760 -0
- package/src/state/track.ts +659 -0
- package/src/state/word.ts +89 -4
- package/src/state/wordclip.ts +28 -11
- package/src/state/wordformat.ts +35 -13
- package/src/v3/document.ts +285 -14
- package/src/v3/index.ts +14 -0
- package/src/v3/migrate.ts +2 -2
- package/src/v3/resolve.ts +259 -15
- package/src/v3/schema.ts +190 -7
|
@@ -0,0 +1,412 @@
|
|
|
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 { TransformError } from "../transform/transform.js";
|
|
14
|
+
import { RUN_PROPERTY_MARKS } from "../v3/schema.js";
|
|
15
|
+
import { closeHistory } from "./history.js";
|
|
16
|
+
import { MARK_ATTRS } from "./mark.js";
|
|
17
|
+
import { TextSelection } from "./selection.js";
|
|
18
|
+
import type { Command, EditorState, Transaction } from "./state.js";
|
|
19
|
+
import { keepTablesApart } from "./tables.js";
|
|
20
|
+
import { RECORDED, trackState, untracked } from "./track.js";
|
|
21
|
+
|
|
22
|
+
export type ChangeKind =
|
|
23
|
+
| "insertion"
|
|
24
|
+
| "deletion"
|
|
25
|
+
| "formatting"
|
|
26
|
+
| "paragraphInsertion"
|
|
27
|
+
| "paragraphDeletion"
|
|
28
|
+
| "paragraphFormatting"
|
|
29
|
+
| "rowInsertion"
|
|
30
|
+
| "rowDeletion";
|
|
31
|
+
|
|
32
|
+
/** One tracked change of the document, where it is. */
|
|
33
|
+
export interface Change {
|
|
34
|
+
readonly kind: ChangeKind;
|
|
35
|
+
readonly id: number;
|
|
36
|
+
readonly author: string;
|
|
37
|
+
readonly date: string | null;
|
|
38
|
+
/** The content it covers; for a paragraph mark, the paragraph's closing token. */
|
|
39
|
+
readonly from: number;
|
|
40
|
+
readonly to: number;
|
|
41
|
+
/** A tracked move's name, shared by both ends (`w:moveFrom`, `w:moveTo`). */
|
|
42
|
+
readonly move: string | null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface ChangeFilter {
|
|
46
|
+
/** Only this author's changes. */
|
|
47
|
+
author?: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const MARK_KINDS: Record<string, ChangeKind> = {
|
|
51
|
+
insertion: "insertion",
|
|
52
|
+
deletion: "deletion",
|
|
53
|
+
formatChange: "formatting",
|
|
54
|
+
};
|
|
55
|
+
const RUN_MARKS = new Set<string>(RUN_PROPERTY_MARKS);
|
|
56
|
+
|
|
57
|
+
interface Revision {
|
|
58
|
+
id: number;
|
|
59
|
+
author: string;
|
|
60
|
+
date?: string | null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Every tracked change of `doc`, in document order. */
|
|
64
|
+
export function trackedChanges(doc: Node): Change[] {
|
|
65
|
+
const changes: Change[] = [];
|
|
66
|
+
const flush = (run: { mark: Mark; from: number; to: number }) => {
|
|
67
|
+
const attrs = run.mark.attrs;
|
|
68
|
+
changes.push({
|
|
69
|
+
kind: MARK_KINDS[run.mark.type.name] as ChangeKind,
|
|
70
|
+
id: attrs.id as number,
|
|
71
|
+
author: attrs.author as string,
|
|
72
|
+
date: (attrs.date as string | null) ?? null,
|
|
73
|
+
from: run.from,
|
|
74
|
+
to: run.to,
|
|
75
|
+
move: (attrs.move as string | null | undefined) ?? null,
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
// Every paragraph, those in table cells too; a table row's own insertion or deletion.
|
|
79
|
+
doc.descendants((block, offset) => {
|
|
80
|
+
if (block.type.name === "tableRow") {
|
|
81
|
+
const revision = block.attrs.revision as { insertion?: Revision; deletion?: Revision } | null;
|
|
82
|
+
for (const [kind, r] of [
|
|
83
|
+
["rowInsertion", revision?.insertion],
|
|
84
|
+
["rowDeletion", revision?.deletion],
|
|
85
|
+
] as const)
|
|
86
|
+
if (r)
|
|
87
|
+
changes.push({
|
|
88
|
+
kind,
|
|
89
|
+
id: r.id,
|
|
90
|
+
author: r.author,
|
|
91
|
+
date: r.date ?? null,
|
|
92
|
+
from: offset,
|
|
93
|
+
to: offset + block.nodeSize,
|
|
94
|
+
move: null,
|
|
95
|
+
});
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
if (!block.isTextblock) return true;
|
|
99
|
+
const change = block.attrs.formatChange as Revision | null;
|
|
100
|
+
if (change) changes.push(paragraphChange("paragraphFormatting", change, offset, block));
|
|
101
|
+
// Runs of content with the same change mark, extended while the next node has it too.
|
|
102
|
+
let open: { mark: Mark; from: number; to: number }[] = [];
|
|
103
|
+
block.forEach((inline, at) => {
|
|
104
|
+
const pos = offset + 1 + at;
|
|
105
|
+
const next: typeof open = [];
|
|
106
|
+
for (const mark of inline.marks) {
|
|
107
|
+
if (!MARK_KINDS[mark.type.name]) continue;
|
|
108
|
+
const run = open.find((r) => r.to === pos && r.mark.eq(mark));
|
|
109
|
+
if (run) run.to = pos + inline.nodeSize;
|
|
110
|
+
next.push(run ?? { mark, from: pos, to: pos + inline.nodeSize });
|
|
111
|
+
}
|
|
112
|
+
for (const run of open) if (!next.includes(run)) flush(run);
|
|
113
|
+
open = next;
|
|
114
|
+
});
|
|
115
|
+
for (const run of open) flush(run);
|
|
116
|
+
const revision = block.attrs.revision as { insertion?: Revision; deletion?: Revision } | null;
|
|
117
|
+
if (revision?.insertion)
|
|
118
|
+
changes.push(paragraphChange("paragraphInsertion", revision.insertion, offset, block));
|
|
119
|
+
if (revision?.deletion)
|
|
120
|
+
changes.push(paragraphChange("paragraphDeletion", revision.deletion, offset, block));
|
|
121
|
+
return false;
|
|
122
|
+
});
|
|
123
|
+
return changes.sort((a, b) => a.from - b.from || a.to - b.to);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** A paragraph's change: its mark (the closing token), or its content for its formatting. */
|
|
127
|
+
function paragraphChange(
|
|
128
|
+
kind: ChangeKind,
|
|
129
|
+
revision: Revision,
|
|
130
|
+
offset: number,
|
|
131
|
+
block: Node,
|
|
132
|
+
): Change {
|
|
133
|
+
const end = offset + block.nodeSize;
|
|
134
|
+
return {
|
|
135
|
+
kind,
|
|
136
|
+
id: revision.id,
|
|
137
|
+
author: revision.author,
|
|
138
|
+
date: revision.date ?? null,
|
|
139
|
+
from: kind === "paragraphFormatting" ? offset + 1 : end - 1,
|
|
140
|
+
to: kind === "paragraphFormatting" ? end - 1 : end,
|
|
141
|
+
move: null,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
const INLINE: ReadonlySet<ChangeKind> = new Set(["insertion", "deletion", "formatting"]);
|
|
145
|
+
/** Inline nodes no change marks: they stay when the text around them goes. */
|
|
146
|
+
const MARKERS = new Set(["bookmarkStart", "bookmarkEnd", "commentStart", "commentEnd"]);
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* The changes `from..to` touches, with their moves' other ends. At a caret: the ones around it,
|
|
150
|
+
* the paragraph's formatting only when nothing else is. In a selection, changes to content only
|
|
151
|
+
* as far as the selection goes (Word accepts the selected part).
|
|
152
|
+
*/
|
|
153
|
+
function changesIn(doc: Node, from: number, to: number, filter: ChangeFilter = {}): Change[] {
|
|
154
|
+
const all = trackedChanges(doc).filter((c) => !filter.author || c.author === filter.author);
|
|
155
|
+
let picked: Change[];
|
|
156
|
+
if (from === to) {
|
|
157
|
+
const around = all.filter((c) => c.from <= from && from <= c.to);
|
|
158
|
+
const own = around.filter((c) => c.kind !== "paragraphFormatting");
|
|
159
|
+
picked = own.length ? own : around;
|
|
160
|
+
} else
|
|
161
|
+
picked = all
|
|
162
|
+
.filter((c) => c.from < to && from < c.to)
|
|
163
|
+
.map((c) =>
|
|
164
|
+
INLINE.has(c.kind) ? { ...c, from: Math.max(c.from, from), to: Math.min(c.to, to) } : c,
|
|
165
|
+
);
|
|
166
|
+
const moves = new Set(picked.map((c) => c.move).filter((m) => m !== null));
|
|
167
|
+
const partners = all.filter(
|
|
168
|
+
(c) =>
|
|
169
|
+
c.move !== null &&
|
|
170
|
+
moves.has(c.move) &&
|
|
171
|
+
!picked.some((p) => p.id === c.id && p.kind === c.kind),
|
|
172
|
+
);
|
|
173
|
+
return [...picked, ...partners];
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Applies accepting (or rejecting) `changes` to `tr`. */
|
|
177
|
+
function resolve(tr: Transaction, changes: readonly Change[], accept: boolean): void {
|
|
178
|
+
const schema = tr.doc.type.schema;
|
|
179
|
+
const removed: [number, number][] = [];
|
|
180
|
+
const joins: { pos: number; keepFirst: boolean }[] = [];
|
|
181
|
+
/** Rows that go, by their table (its position): a table losing every row goes whole. */
|
|
182
|
+
const rowsGoing = new Map<number, [number, number][]>();
|
|
183
|
+
/** Whether whole rows or paragraphs go: what lies inside them must not be removed again. */
|
|
184
|
+
let wholes = false;
|
|
185
|
+
for (const change of changes) {
|
|
186
|
+
const { kind, from, to } = change;
|
|
187
|
+
const markType = schema.marks[kind === "formatting" ? "formatChange" : kind];
|
|
188
|
+
switch (kind) {
|
|
189
|
+
case "insertion":
|
|
190
|
+
case "deletion": {
|
|
191
|
+
if (!markType) break;
|
|
192
|
+
if ((kind === "insertion") === accept) removeMarkOf(tr, from, to, markType.name, change.id);
|
|
193
|
+
else removed.push([from, to]);
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
case "formatting": {
|
|
197
|
+
tr.doc.nodesBetween(from, to, (node, pos) => {
|
|
198
|
+
if (!node.isInline) return true;
|
|
199
|
+
const record = node.marks.find((m) => m.type.name === "formatChange");
|
|
200
|
+
if (!record || record.attrs.id !== change.id) return false;
|
|
201
|
+
const s = Math.max(pos, from);
|
|
202
|
+
const e = Math.min(pos + node.nodeSize, to);
|
|
203
|
+
if (!accept) {
|
|
204
|
+
for (const mark of node.marks)
|
|
205
|
+
if (RUN_MARKS.has(mark.type.name)) tr.removeMark(s, e, mark);
|
|
206
|
+
for (const json of record.attrs.previous as { type: string; attrs?: Attrs }[])
|
|
207
|
+
tr.addMark(s, e, schema.mark(json.type, json.attrs ?? null));
|
|
208
|
+
}
|
|
209
|
+
tr.removeMark(s, e, record);
|
|
210
|
+
return false;
|
|
211
|
+
});
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
case "paragraphFormatting": {
|
|
215
|
+
const start = tr.doc.resolve(from).before();
|
|
216
|
+
const node = tr.doc.nodeAt(start);
|
|
217
|
+
if (!node?.isTextblock) break;
|
|
218
|
+
const attrs: Attrs = { ...node.attrs, formatChange: null };
|
|
219
|
+
if (!accept) {
|
|
220
|
+
const previous = (node.attrs.formatChange as { previous: Attrs }).previous;
|
|
221
|
+
for (const key of RECORDED)
|
|
222
|
+
(attrs as Record<string, unknown>)[key] = previous[key] ?? null;
|
|
223
|
+
}
|
|
224
|
+
tr.setNodeMarkup(start, node.type, attrs);
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
case "rowInsertion":
|
|
228
|
+
case "rowDeletion": {
|
|
229
|
+
const row = tr.doc.nodeAt(from);
|
|
230
|
+
if (row?.type.name !== "tableRow") break;
|
|
231
|
+
if ((kind === "rowDeletion") === accept) {
|
|
232
|
+
const table = tr.doc.resolve(from).before();
|
|
233
|
+
rowsGoing.set(table, [...(rowsGoing.get(table) ?? []), [from, to]]);
|
|
234
|
+
} else {
|
|
235
|
+
const revision = { ...(row.attrs.revision as Record<string, unknown>) };
|
|
236
|
+
delete revision[kind === "rowInsertion" ? "insertion" : "deletion"];
|
|
237
|
+
const next = Object.keys(revision).length ? revision : null;
|
|
238
|
+
tr.setNodeMarkup(from, row.type, { ...row.attrs, revision: next });
|
|
239
|
+
}
|
|
240
|
+
break;
|
|
241
|
+
}
|
|
242
|
+
case "paragraphInsertion":
|
|
243
|
+
case "paragraphDeletion": {
|
|
244
|
+
const start = tr.doc.resolve(from).before();
|
|
245
|
+
const node = tr.doc.nodeAt(start);
|
|
246
|
+
if (!node?.isTextblock) break;
|
|
247
|
+
const revision = { ...(node.attrs.revision as Record<string, unknown>) };
|
|
248
|
+
const goes = (kind === "paragraphDeletion") === accept;
|
|
249
|
+
delete revision[kind === "paragraphInsertion" ? "insertion" : "deletion"];
|
|
250
|
+
const next = Object.keys(revision).length ? revision : null;
|
|
251
|
+
tr.setNodeMarkup(start, node.type, { ...node.attrs, revision: next });
|
|
252
|
+
// The mark goes: the paragraph joins the next one, when there is one to join.
|
|
253
|
+
const end = start + node.nodeSize;
|
|
254
|
+
// A paragraph whose text goes too is removed whole: the next one stays as it is.
|
|
255
|
+
const emptied = covered(node, start + 1, removed);
|
|
256
|
+
if (goes && tr.doc.resolve(end).nodeAfter?.isTextblock)
|
|
257
|
+
joins.push({ pos: end, keepFirst: accept && !emptied });
|
|
258
|
+
// Nothing to join with (a table follows, or a cell ends): an inserted paragraph left
|
|
259
|
+
// empty goes whole when rejected (Word's rules are restored after: `keepTablesApart`).
|
|
260
|
+
// (Never a cell's only paragraph: a cell ends with one.)
|
|
261
|
+
else if (
|
|
262
|
+
goes &&
|
|
263
|
+
!accept &&
|
|
264
|
+
(!node.content.size || emptied) &&
|
|
265
|
+
tr.doc.resolve(start).parent.childCount > 1
|
|
266
|
+
) {
|
|
267
|
+
removed.push([start, end]);
|
|
268
|
+
wholes = true;
|
|
269
|
+
}
|
|
270
|
+
break;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
// Rows that go, or their table when every row does; what lies inside them is not removed again.
|
|
275
|
+
for (const [table, rows] of rowsGoing) {
|
|
276
|
+
const node = tr.doc.nodeAt(table);
|
|
277
|
+
if (node && rows.length === node.childCount) removed.push([table, table + node.nodeSize]);
|
|
278
|
+
else removed.push(...rows);
|
|
279
|
+
}
|
|
280
|
+
if (rowsGoing.size || wholes) {
|
|
281
|
+
const outer = removed.filter(
|
|
282
|
+
([s, e]) => !removed.some(([s2, e2]) => (s2 < s && e <= e2) || (s2 <= s && e < e2)),
|
|
283
|
+
);
|
|
284
|
+
removed.length = 0;
|
|
285
|
+
removed.push(...outer);
|
|
286
|
+
}
|
|
287
|
+
// Content removed and paragraphs joined, from the end so earlier positions stay.
|
|
288
|
+
const ops = [
|
|
289
|
+
...removed.map(([s, e]) => ({ at: s, run: () => tr.delete(s, e) })),
|
|
290
|
+
...joins
|
|
291
|
+
.filter(
|
|
292
|
+
({ pos }) => !(rowsGoing.size || wholes) || !removed.some(([s, e]) => s < pos && pos < e),
|
|
293
|
+
)
|
|
294
|
+
.map(({ pos, keepFirst }) => ({ at: pos - 1, run: () => join(tr, pos, keepFirst) })),
|
|
295
|
+
].sort((a, b) => b.at - a.at);
|
|
296
|
+
for (const op of ops) op.run();
|
|
297
|
+
keepTablesApart(tr);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** Whether every child of `node` (content at `start`) but markers lies in `removed`. */
|
|
301
|
+
function covered(node: Node, start: number, removed: readonly [number, number][]): boolean {
|
|
302
|
+
let all = true;
|
|
303
|
+
node.forEach((child, offset) => {
|
|
304
|
+
const pos = start + offset;
|
|
305
|
+
if (MARKERS.has(child.type.name)) return;
|
|
306
|
+
if (!removed.some(([s, e]) => s <= pos && pos + child.nodeSize <= e)) all = false;
|
|
307
|
+
});
|
|
308
|
+
return all;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** Removes `type`'s mark with `id` from `from..to`. */
|
|
312
|
+
function removeMarkOf(tr: Transaction, from: number, to: number, type: string, id: number): void {
|
|
313
|
+
tr.doc.nodesBetween(from, to, (node, pos) => {
|
|
314
|
+
if (!node.isInline) return true;
|
|
315
|
+
const mark = node.marks.find((m) => m.type.name === type && m.attrs.id === id);
|
|
316
|
+
if (mark) tr.removeMark(Math.max(pos, from), Math.min(pos + node.nodeSize, to), mark);
|
|
317
|
+
return false;
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Joins the paragraph ending at `pos` with the next one. Accepting a deleted mark keeps the first
|
|
323
|
+
* one's properties and the last one's mark (as an untracked join), unless the first one is left
|
|
324
|
+
* with nothing (then it is gone); rejecting an inserted mark keeps the last one whole: its mark
|
|
325
|
+
* is the original's.
|
|
326
|
+
*/
|
|
327
|
+
function join(tr: Transaction, pos: number, keepFirst: boolean): void {
|
|
328
|
+
const $pos = tr.doc.resolve(pos);
|
|
329
|
+
const first = $pos.nodeBefore;
|
|
330
|
+
const last = $pos.nodeAfter;
|
|
331
|
+
if (!first?.isTextblock || !last?.isTextblock) return;
|
|
332
|
+
const start = pos - first.nodeSize;
|
|
333
|
+
tr.join(pos);
|
|
334
|
+
const attrs: Attrs = keepFirst ? { ...first.attrs } : { ...last.attrs };
|
|
335
|
+
if (keepFirst)
|
|
336
|
+
for (const key of MARK_ATTRS) (attrs as Record<string, unknown>)[key] = last.attrs[key] ?? null;
|
|
337
|
+
tr.setNodeMarkup(start, first.type, attrs);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function command(
|
|
341
|
+
accept: boolean,
|
|
342
|
+
range: (state: EditorState) => readonly { from: number; to: number }[],
|
|
343
|
+
filter?: ChangeFilter,
|
|
344
|
+
): Command {
|
|
345
|
+
return (state, dispatch) => {
|
|
346
|
+
if (!state.schema.marks.insertion) return false;
|
|
347
|
+
// A change around several selected cells (a row revision, a move's partner) counts once.
|
|
348
|
+
const seen = new Set<string>();
|
|
349
|
+
const changes = range(state)
|
|
350
|
+
.flatMap((r) => changesIn(state.doc, r.from, r.to, filter))
|
|
351
|
+
.filter((c) => {
|
|
352
|
+
const key = `${c.kind} ${c.id} ${c.from} ${c.to}`;
|
|
353
|
+
return !seen.has(key) && !!seen.add(key);
|
|
354
|
+
});
|
|
355
|
+
if (!changes.length) return false;
|
|
356
|
+
if (dispatch) {
|
|
357
|
+
// Its own undo step, never merged with the typing before it.
|
|
358
|
+
const tr = state.tr.setMeta(untracked, true).setMeta(closeHistory, true);
|
|
359
|
+
// A document the result would not fit (a structure Word allows and Foli cannot change
|
|
360
|
+
// yet): nothing is changed.
|
|
361
|
+
try {
|
|
362
|
+
resolve(tr, changes, accept);
|
|
363
|
+
} catch (error) {
|
|
364
|
+
if (error instanceof TransformError) return false;
|
|
365
|
+
throw error;
|
|
366
|
+
}
|
|
367
|
+
dispatch(tr);
|
|
368
|
+
}
|
|
369
|
+
return true;
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const selected = (state: EditorState) => state.selection.ranges;
|
|
374
|
+
const everything = (state: EditorState) => [{ from: 0, to: state.doc.content.size }];
|
|
375
|
+
|
|
376
|
+
/** Accepts the change at the caret, or the changes in the selection. */
|
|
377
|
+
export const acceptChange: Command = command(true, selected);
|
|
378
|
+
/** Rejects the change at the caret, or the changes in the selection. */
|
|
379
|
+
export const rejectChange: Command = command(false, selected);
|
|
380
|
+
/** Accepts every change (of `author`, if given). */
|
|
381
|
+
export const acceptAllChanges = (author?: string): Command =>
|
|
382
|
+
command(true, everything, author === undefined ? {} : { author });
|
|
383
|
+
/** Rejects every change (of `author`, if given). */
|
|
384
|
+
export const rejectAllChanges = (author?: string): Command =>
|
|
385
|
+
command(false, everything, author === undefined ? {} : { author });
|
|
386
|
+
|
|
387
|
+
/** Selects the next (or previous) change after (before) the selection. */
|
|
388
|
+
function goTo(forward: boolean): Command {
|
|
389
|
+
return (state, dispatch) => {
|
|
390
|
+
// Where the review view hides deleted content, only what it shows.
|
|
391
|
+
const hidden = (trackState(state)?.view ?? "markup") !== "markup";
|
|
392
|
+
const changes = trackedChanges(state.doc).filter(
|
|
393
|
+
(c) => !hidden || (c.kind !== "deletion" && c.kind !== "paragraphDeletion"),
|
|
394
|
+
);
|
|
395
|
+
const { from, to } = state.selection;
|
|
396
|
+
// In document order (by start, then end), the first one after (before) the selection.
|
|
397
|
+
const target = forward
|
|
398
|
+
? changes.find((c) => c.from > from || (c.from === from && c.to > to))
|
|
399
|
+
: [...changes].reverse().find((c) => c.from < from || (c.from === from && c.to < to));
|
|
400
|
+
if (!target) return false;
|
|
401
|
+
if (dispatch) {
|
|
402
|
+
// A paragraph mark: the caret at the end of its paragraph.
|
|
403
|
+
const mark = target.kind === "paragraphInsertion" || target.kind === "paragraphDeletion";
|
|
404
|
+
const head = mark ? target.from : target.to;
|
|
405
|
+
dispatch(state.tr.setSelection(TextSelection.create(state.doc, target.from, head)));
|
|
406
|
+
}
|
|
407
|
+
return true;
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
export const nextChange: Command = goTo(true);
|
|
412
|
+
export const previousChange: Command = goTo(false);
|
package/src/state/selection.ts
CHANGED
|
@@ -2,7 +2,8 @@ import { Mark, type Node, type ResolvedPos } from "../model/index.js";
|
|
|
2
2
|
import type { Mappable } from "../transform/stepmap.js";
|
|
3
3
|
|
|
4
4
|
export interface SelectionJSON {
|
|
5
|
-
|
|
5
|
+
/** `cell`: a `CellSelection` (anchor and head are the positions before two cells). */
|
|
6
|
+
type: "text" | "node" | "all" | "cell";
|
|
6
7
|
anchor: number;
|
|
7
8
|
head: number;
|
|
8
9
|
}
|
|
@@ -42,6 +43,14 @@ export abstract class Selection {
|
|
|
42
43
|
return this.from === this.to;
|
|
43
44
|
}
|
|
44
45
|
|
|
46
|
+
/**
|
|
47
|
+
* The ranges the selection covers, in document order: `from..to`, or one per selected cell
|
|
48
|
+
* for a cell selection (so commands that act on a range leave the cells outside it alone).
|
|
49
|
+
*/
|
|
50
|
+
get ranges(): readonly { from: number; to: number }[] {
|
|
51
|
+
return [{ from: this.from, to: this.to }];
|
|
52
|
+
}
|
|
53
|
+
|
|
45
54
|
/** This selection moved into `doc` through `mapping`. */
|
|
46
55
|
abstract map(doc: Node, mapping: Mappable): Selection;
|
|
47
56
|
|
|
@@ -74,9 +83,14 @@ export abstract class Selection {
|
|
|
74
83
|
return Selection.near(doc.resolve(doc.content.size), -1);
|
|
75
84
|
}
|
|
76
85
|
|
|
86
|
+
/** Selection kinds defined elsewhere (`cell`: tables), by their JSON `type`. */
|
|
87
|
+
static readonly kinds = new Map<string, (doc: Node, json: SelectionJSON) => Selection>();
|
|
88
|
+
|
|
77
89
|
/** Restores a selection, clamping positions into the document. */
|
|
78
90
|
static fromJSON(doc: Node, json: SelectionJSON): Selection {
|
|
79
91
|
if (json.type === "all") return new AllSelection(doc);
|
|
92
|
+
const kind = Selection.kinds.get(json.type);
|
|
93
|
+
if (kind) return kind(doc, json);
|
|
80
94
|
const clamp = (pos: number) => Math.max(0, Math.min(pos, doc.content.size));
|
|
81
95
|
if (json.type === "node") {
|
|
82
96
|
const $pos = doc.resolve(clamp(json.anchor));
|
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
|
}
|