@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,602 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tracked changes (9.2): with tracking on, an edit is recorded as Word records it. Text and
|
|
3
|
+
* breaks an edit removes stay in the document with a `deletion` mark; what it adds gets an
|
|
4
|
+
* `insertion` mark; a paragraph mark it removes or adds is recorded on the paragraph
|
|
5
|
+
* (`revision`). Deleting one's own insertion removes it for real, as Word does.
|
|
6
|
+
*
|
|
7
|
+
* Commands do not know about tracking: the editor rebuilds each transaction it dispatches from
|
|
8
|
+
* what the transaction changed (`trackTransaction`). The ranges its steps replaced are compared
|
|
9
|
+
* before and after; each becomes a tracked deletion of the old content followed by a tracked
|
|
10
|
+
* insertion of the new, and the steps that move nothing (marks, attributes, comments) are
|
|
11
|
+
* replayed on top. Undo and redo, transactions kept out of history and those with the
|
|
12
|
+
* `untracked` meta (accepting and rejecting changes) are not tracked.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { deepEqual } from "../model/attrs.js";
|
|
16
|
+
import {
|
|
17
|
+
type Attrs,
|
|
18
|
+
Fragment,
|
|
19
|
+
type Mark,
|
|
20
|
+
type MarkType,
|
|
21
|
+
type Node,
|
|
22
|
+
Slice,
|
|
23
|
+
} from "../model/index.js";
|
|
24
|
+
import { type Mappable, Mapping, type MapResult } from "../transform/stepmap.js";
|
|
25
|
+
import {
|
|
26
|
+
AddMarkStep,
|
|
27
|
+
NodeMarkupBatchStep,
|
|
28
|
+
NodeMarkupStep,
|
|
29
|
+
RemoveMarkStep,
|
|
30
|
+
type Step,
|
|
31
|
+
} from "../transform/steps.js";
|
|
32
|
+
import { TransformError } from "../transform/transform.js";
|
|
33
|
+
import type { CommentJSON } from "../v3/document.js";
|
|
34
|
+
import { PARAGRAPH_PROPERTY_KEYS, RUN_PROPERTY_MARKS } from "../v3/schema.js";
|
|
35
|
+
import { MARK_ATTRS } from "./mark.js";
|
|
36
|
+
import { NodeSelection, Selection, TextSelection } from "./selection.js";
|
|
37
|
+
import { type Command, type EditorState, Plugin, PluginKey, type Transaction } from "./state.js";
|
|
38
|
+
import { wordContext } from "./word.js";
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* How tracked changes show: `markup` (inserted text underlined and deleted text struck through,
|
|
42
|
+
* in the author's color, with a bar beside changed paragraphs), `simple` (as if accepted, with
|
|
43
|
+
* the bar), `final` (as if accepted).
|
|
44
|
+
*/
|
|
45
|
+
export type ReviewView = "markup" | "simple" | "final";
|
|
46
|
+
|
|
47
|
+
export interface TrackState {
|
|
48
|
+
/** Whether edits are recorded as tracked changes. */
|
|
49
|
+
readonly on: boolean;
|
|
50
|
+
/** Who makes them. */
|
|
51
|
+
readonly author: string;
|
|
52
|
+
/** How changes show in a view of the editor (default `markup`). */
|
|
53
|
+
readonly view?: ReviewView;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const key = new PluginKey<TrackState>("track");
|
|
57
|
+
|
|
58
|
+
/** Set this meta (true) on a transaction to keep it out of tracking. */
|
|
59
|
+
export const untracked = "untracked";
|
|
60
|
+
|
|
61
|
+
/** Inline nodes a tracked change does not mark: they stand between runs in Word. */
|
|
62
|
+
const MARKERS = new Set(["bookmarkStart", "bookmarkEnd", "commentStart", "commentEnd"]);
|
|
63
|
+
|
|
64
|
+
const RUN_MARKS = new Set<string>(RUN_PROPERTY_MARKS);
|
|
65
|
+
/**
|
|
66
|
+
* Paragraph properties a formatting change does not record: the paragraph mark's look (Word
|
|
67
|
+
* records it on the mark) and the verbatim XML.
|
|
68
|
+
*/
|
|
69
|
+
const UNRECORDED = new Set(["markFormat", "ext"]);
|
|
70
|
+
|
|
71
|
+
/** @internal Paragraph properties a formatting change records (`previous`). */
|
|
72
|
+
export const RECORDED = PARAGRAPH_PROPERTY_KEYS.filter((k) => !UNRECORDED.has(k));
|
|
73
|
+
/** A paragraph's recorded properties (a formatting change's `previous`): those it has. */
|
|
74
|
+
const propertiesOf = (attrs: Attrs) =>
|
|
75
|
+
Object.fromEntries(RECORDED.filter((k) => attrs[k] != null).map((k) => [k, attrs[k]]));
|
|
76
|
+
const sameProperties = (a: Attrs, b: Attrs) =>
|
|
77
|
+
RECORDED.every((k) => deepEqual(a[k] ?? null, b[k] ?? null));
|
|
78
|
+
|
|
79
|
+
/** A tracked change's author and date, as the schema keeps them (`w:id`, `w:author`, `w:date`). */
|
|
80
|
+
interface Revision {
|
|
81
|
+
id: number;
|
|
82
|
+
author: string;
|
|
83
|
+
date: string;
|
|
84
|
+
}
|
|
85
|
+
interface ParagraphRevision {
|
|
86
|
+
insertion?: Revision;
|
|
87
|
+
deletion?: Revision;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function trackPlugin(state: TrackState): Plugin<TrackState> {
|
|
91
|
+
return new Plugin({
|
|
92
|
+
key,
|
|
93
|
+
state: {
|
|
94
|
+
init: () => state,
|
|
95
|
+
apply: (tr, value) => (tr.getMeta(key) as TrackState | undefined) ?? value,
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Whether `state` records edits as tracked changes, and in whose name. */
|
|
101
|
+
export function trackState(state: EditorState): TrackState | null {
|
|
102
|
+
return key.getState(state) ?? null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Turns tracking on or off (and sets the author). Applies only to a schema v3 editor. */
|
|
106
|
+
export function setTrackChanges(on: boolean, author?: string): Command {
|
|
107
|
+
return (state, dispatch) => {
|
|
108
|
+
const current = key.getState(state);
|
|
109
|
+
if (!current || !state.schema.marks.insertion) return false;
|
|
110
|
+
dispatch?.(state.tr.setMeta(key, { ...current, on, author: author ?? current.author }));
|
|
111
|
+
return true;
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Shows tracked changes as `view` in the editor's views. Applies only to a schema v3 editor. */
|
|
116
|
+
export function setReviewView(view: ReviewView): Command {
|
|
117
|
+
return (state, dispatch) => {
|
|
118
|
+
const current = key.getState(state);
|
|
119
|
+
if (!current || !state.schema.marks.insertion) return false;
|
|
120
|
+
dispatch?.(state.tr.setMeta(key, { ...current, view }));
|
|
121
|
+
return true;
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** @internal Word's date: UTC, to the second. */
|
|
126
|
+
export function now(): string {
|
|
127
|
+
return new Date().toISOString().replace(/\.\d+Z$/, "Z");
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const REVIEW_MARKS = new Set(["insertion", "deletion", "formatChange"]);
|
|
131
|
+
|
|
132
|
+
/** The largest change id in `doc` and the comments' content (marks, paragraph revisions), or -1. */
|
|
133
|
+
function maxRevisionId(doc: Node, comments: readonly CommentJSON[]): number {
|
|
134
|
+
let max = -1;
|
|
135
|
+
const see = (value: unknown) => {
|
|
136
|
+
const id = (value as { id?: unknown } | null | undefined)?.id;
|
|
137
|
+
if (typeof id === "number" && id > max) max = id;
|
|
138
|
+
};
|
|
139
|
+
doc.descendants((node) => {
|
|
140
|
+
for (const mark of node.marks) {
|
|
141
|
+
const name = mark.type.name;
|
|
142
|
+
if (REVIEW_MARKS.has(name)) see(mark.attrs);
|
|
143
|
+
}
|
|
144
|
+
if (node.isTextblock) {
|
|
145
|
+
const revision = node.attrs.revision as ParagraphRevision | null | undefined;
|
|
146
|
+
see(revision?.insertion);
|
|
147
|
+
see(revision?.deletion);
|
|
148
|
+
see(node.attrs.formatChange);
|
|
149
|
+
}
|
|
150
|
+
return true;
|
|
151
|
+
});
|
|
152
|
+
// Comments hold paragraphs too, which can carry changes (JSON: walked as it is).
|
|
153
|
+
const walk = (value: unknown): void => {
|
|
154
|
+
if (Array.isArray(value)) value.forEach(walk);
|
|
155
|
+
else if (value && typeof value === "object") {
|
|
156
|
+
const json = value as Record<string, unknown>;
|
|
157
|
+
if (REVIEW_MARKS.has(json.type as string)) see(json.attrs);
|
|
158
|
+
if (json.type === "paragraph") see((json.attrs as { formatChange?: unknown })?.formatChange);
|
|
159
|
+
const revision = json.revision as ParagraphRevision | null | undefined;
|
|
160
|
+
if (revision && typeof revision === "object") {
|
|
161
|
+
see(revision.insertion);
|
|
162
|
+
see(revision.deletion);
|
|
163
|
+
}
|
|
164
|
+
Object.values(json).forEach(walk);
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
walk(comments.map((c) => c.content));
|
|
168
|
+
return max;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** A replaced range: `a..b` before the transaction, `c..d` after it. */
|
|
172
|
+
interface Range {
|
|
173
|
+
a: number;
|
|
174
|
+
b: number;
|
|
175
|
+
c: number;
|
|
176
|
+
d: number;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** The ranges `tr`'s steps replaced, merged where they touch, in document order. */
|
|
180
|
+
function changedRanges(tr: Transaction): Range[] {
|
|
181
|
+
const maps = tr.mapping.maps;
|
|
182
|
+
const ranges: Range[] = [];
|
|
183
|
+
maps.forEach((map, k) => {
|
|
184
|
+
if (!map.ranges.length) return;
|
|
185
|
+
const back = new Mapping(
|
|
186
|
+
maps
|
|
187
|
+
.slice(0, k)
|
|
188
|
+
.map((m) => m.invert())
|
|
189
|
+
.reverse(),
|
|
190
|
+
);
|
|
191
|
+
const forward = tr.mapping.slice(k + 1);
|
|
192
|
+
map.forEach((oldStart, oldEnd, newStart, newEnd) => {
|
|
193
|
+
const a = back.map(oldStart, -1);
|
|
194
|
+
const c = forward.map(newStart, -1);
|
|
195
|
+
ranges.push({
|
|
196
|
+
a,
|
|
197
|
+
b: Math.max(a, back.map(oldEnd, 1)),
|
|
198
|
+
c,
|
|
199
|
+
d: Math.max(c, forward.map(newEnd, 1)),
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
ranges.sort((x, y) => x.a - y.a);
|
|
204
|
+
const merged: Range[] = [];
|
|
205
|
+
for (const r of ranges) {
|
|
206
|
+
const last = merged[merged.length - 1];
|
|
207
|
+
if (last && r.a <= last.b) {
|
|
208
|
+
last.b = Math.max(last.b, r.b);
|
|
209
|
+
last.c = Math.min(last.c, r.c);
|
|
210
|
+
last.d = Math.max(last.d, r.d);
|
|
211
|
+
} else merged.push({ ...r });
|
|
212
|
+
}
|
|
213
|
+
return merged;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** A position in the rebuilt transaction's document after its first `step` steps. */
|
|
217
|
+
interface At {
|
|
218
|
+
pos: number;
|
|
219
|
+
step: number;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* `tr` rebuilt as tracked changes when `state` tracks them (`tr` itself otherwise), or null when
|
|
224
|
+
* it cannot be recorded (the edit is then not made: nothing is changed untracked).
|
|
225
|
+
*/
|
|
226
|
+
export function trackTransaction(state: EditorState, tr: Transaction): Transaction | null {
|
|
227
|
+
const track = key.getState(state);
|
|
228
|
+
const { insertion, deletion } = state.schema.marks;
|
|
229
|
+
if (
|
|
230
|
+
!track?.on ||
|
|
231
|
+
!insertion ||
|
|
232
|
+
!deletion ||
|
|
233
|
+
!tr.docChanged ||
|
|
234
|
+
tr.getMeta(untracked) ||
|
|
235
|
+
tr.getMeta("historyOp") ||
|
|
236
|
+
!tr.addToHistory
|
|
237
|
+
)
|
|
238
|
+
return tr;
|
|
239
|
+
try {
|
|
240
|
+
return rebuild(state, tr, track.author, insertion, deletion);
|
|
241
|
+
} catch (error) {
|
|
242
|
+
if (error instanceof TransformError) return null;
|
|
243
|
+
throw error;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function rebuild(
|
|
248
|
+
state: EditorState,
|
|
249
|
+
tr: Transaction,
|
|
250
|
+
author: string,
|
|
251
|
+
insertion: MarkType,
|
|
252
|
+
deletion: MarkType,
|
|
253
|
+
): Transaction {
|
|
254
|
+
const out = state.tr.copyMeta(tr);
|
|
255
|
+
const date = now();
|
|
256
|
+
let nextId = -1;
|
|
257
|
+
const newId = () => {
|
|
258
|
+
if (nextId < 0)
|
|
259
|
+
nextId = maxRevisionId(state.doc, wordContext(state)?.envelope.comments ?? []) + 1;
|
|
260
|
+
return nextId++;
|
|
261
|
+
};
|
|
262
|
+
const at = (x: At, assoc: number) => out.mapping.slice(x.step).map(x.pos, assoc);
|
|
263
|
+
const here = (pos: number): At => ({ pos, step: out.steps.length });
|
|
264
|
+
const pending: { at: At; kind: "insertion" | "deletion"; revision: Revision }[] = [];
|
|
265
|
+
/** Paragraphs whose mark `tr` removed and the rebuilt document keeps (deleted). */
|
|
266
|
+
const apart: At[] = [];
|
|
267
|
+
/** Paragraphs joined for real (one's own inserted mark removed). */
|
|
268
|
+
const rejoined: At[] = [];
|
|
269
|
+
/** Paragraphs that keep a mark with another paragraph's properties, and that paragraph. */
|
|
270
|
+
const formats: { at: At; host: Node }[] = [];
|
|
271
|
+
|
|
272
|
+
/** The mark of `type` by `author` next to `from..to`, to extend instead of starting a change. */
|
|
273
|
+
const adjacent = (type: MarkType, from: number, to: number): Mark | undefined => {
|
|
274
|
+
const doc = out.doc;
|
|
275
|
+
for (const node of [doc.resolve(from).nodeBefore, doc.resolve(to).nodeAfter]) {
|
|
276
|
+
const mark = node && type.isInSet(node.marks);
|
|
277
|
+
const other = type === insertion ? deletion : insertion;
|
|
278
|
+
if (mark && mark.attrs.author === author && !other.isInSet(node.marks)) return mark;
|
|
279
|
+
}
|
|
280
|
+
return undefined;
|
|
281
|
+
};
|
|
282
|
+
const revisionOf = (mark: Mark): Revision => ({
|
|
283
|
+
id: mark.attrs.id as number,
|
|
284
|
+
author,
|
|
285
|
+
date: (mark.attrs.date as string | null) ?? date,
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
/** Records `from..to` (current positions) as deleted; returns where the kept content ends. */
|
|
289
|
+
const deleteRange = (from: number, to: number, inserting: boolean): { start: At; end: At } => {
|
|
290
|
+
const doc = out.doc;
|
|
291
|
+
const startStep = out.steps.length;
|
|
292
|
+
let mark: Mark | undefined;
|
|
293
|
+
const markOf = () =>
|
|
294
|
+
(mark ??= adjacent(deletion, from, to) ?? deletion.create({ id: newId(), author, date }));
|
|
295
|
+
const real: [number, number][] = [];
|
|
296
|
+
const marked: [number, number][] = [];
|
|
297
|
+
// Paragraphs joined for real (one's own inserted mark removed) become one, which starts
|
|
298
|
+
// where the first starts and ends with the last one's mark (`keepLastMark`), as an
|
|
299
|
+
// untracked join does.
|
|
300
|
+
let group: number | null = null;
|
|
301
|
+
/** Whether the group's first paragraph keeps content before the range (else it is gone). */
|
|
302
|
+
let groupLive = false;
|
|
303
|
+
const merged: { at: At; node: Node; live: boolean }[] = [];
|
|
304
|
+
doc.nodesBetween(from, to, (node, pos) => {
|
|
305
|
+
if (node.isTextblock) {
|
|
306
|
+
const end = pos + node.nodeSize;
|
|
307
|
+
const start = group ?? pos;
|
|
308
|
+
const revision = node.attrs.revision as ParagraphRevision | null;
|
|
309
|
+
// Its mark goes: the range runs into the next block, or covers this one whole.
|
|
310
|
+
const crossed =
|
|
311
|
+
(from < end && to > end) ||
|
|
312
|
+
(from <= pos && to >= end && (inserting || !!doc.resolve(end).nodeAfter));
|
|
313
|
+
if (
|
|
314
|
+
crossed &&
|
|
315
|
+
!revision?.deletion &&
|
|
316
|
+
revision?.insertion?.author === author &&
|
|
317
|
+
doc.resolve(end).nodeAfter?.isTextblock
|
|
318
|
+
) {
|
|
319
|
+
real.push([end - 1, end + 1]);
|
|
320
|
+
if (group === null) {
|
|
321
|
+
groupLive = false;
|
|
322
|
+
node.forEach((child, offset) => {
|
|
323
|
+
const live = !MARKERS.has(child.type.name) && !deletion.isInSet(child.marks);
|
|
324
|
+
if (live && pos + 1 + offset < from) groupLive = true;
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
group = start;
|
|
328
|
+
return true;
|
|
329
|
+
}
|
|
330
|
+
if (group !== null) merged.push({ at: here(group), node, live: groupLive });
|
|
331
|
+
group = null;
|
|
332
|
+
if (crossed) apart.push(here(start));
|
|
333
|
+
if (crossed && !revision?.deletion)
|
|
334
|
+
pending.push({ at: here(start), kind: "deletion", revision: revisionOf(markOf()) });
|
|
335
|
+
return true;
|
|
336
|
+
}
|
|
337
|
+
// shortcut: blocks Foli does not model (tables…) cannot be recorded as changed, so such an
|
|
338
|
+
// edit is not made while tracking; Phase 10 models tables and tracks them.
|
|
339
|
+
if (!node.isInline) throw new TransformError("A table cannot be deleted as a tracked change");
|
|
340
|
+
const s = Math.max(pos, from);
|
|
341
|
+
const e = Math.min(pos + node.nodeSize, to);
|
|
342
|
+
if (s >= e || MARKERS.has(node.type.name) || deletion.isInSet(node.marks)) return false;
|
|
343
|
+
const own = insertion.isInSet(node.marks);
|
|
344
|
+
(own && own.attrs.author === author ? real : marked).push([s, e]);
|
|
345
|
+
return false;
|
|
346
|
+
});
|
|
347
|
+
for (const [s, e] of joined(marked)) out.addMark(s, e, markOf());
|
|
348
|
+
for (const [s, e] of joined(real).reverse()) out.delete(s, e);
|
|
349
|
+
for (const { at: where, node, live } of merged) {
|
|
350
|
+
const pos = at(where, 1);
|
|
351
|
+
const current = out.doc.nodeAt(pos);
|
|
352
|
+
if (!current?.isTextblock) continue;
|
|
353
|
+
// The first paragraph left nothing (all of it deleted): it is gone, the last one stays
|
|
354
|
+
// whole (`keepLastMark`).
|
|
355
|
+
if (!live) {
|
|
356
|
+
out.step(new NodeMarkupStep(pos, node.type, node.attrs, current.marks));
|
|
357
|
+
rejoined.push(here(pos));
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
const attrs = { ...current.attrs };
|
|
361
|
+
for (const key of MARK_ATTRS) attrs[key] = node.attrs[key] ?? null;
|
|
362
|
+
// The mark kept is the last paragraph's: taking the first one's properties is a change of
|
|
363
|
+
// its formatting, recorded unless that mark is one's own insertion or has a record.
|
|
364
|
+
const revision = node.attrs.revision as ParagraphRevision | null;
|
|
365
|
+
if (
|
|
366
|
+
!attrs.formatChange &&
|
|
367
|
+
revision?.insertion?.author !== author &&
|
|
368
|
+
!sameProperties(attrs, node.attrs)
|
|
369
|
+
)
|
|
370
|
+
attrs.formatChange = { id: newId(), author, date, previous: propertiesOf(node.attrs) };
|
|
371
|
+
// One's own record of what the paragraph is again (Enter, then Backspace) goes.
|
|
372
|
+
const record = attrs.formatChange as { author: string; previous: Attrs } | null;
|
|
373
|
+
if (record?.author === author && sameProperties(attrs, record.previous))
|
|
374
|
+
attrs.formatChange = null;
|
|
375
|
+
out.step(new NodeMarkupStep(pos, current.type, attrs, current.marks));
|
|
376
|
+
rejoined.push(here(pos));
|
|
377
|
+
}
|
|
378
|
+
const mapping = out.mapping.slice(startStep);
|
|
379
|
+
return { start: here(mapping.map(from, -1)), end: here(mapping.map(to, 1)) };
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
const ranges = changedRanges(tr);
|
|
383
|
+
const records: { range: Range; start: At; end: At; inserted: At | null }[] = [];
|
|
384
|
+
for (const range of [...ranges].reverse()) {
|
|
385
|
+
const slice = tr.doc.slice(range.c, range.d);
|
|
386
|
+
const first = slice.content.firstChild;
|
|
387
|
+
// Deleting everything leaves one empty paragraph: here, the last one, its text deleted.
|
|
388
|
+
const emptyBlock =
|
|
389
|
+
range.a === 0 &&
|
|
390
|
+
range.b === state.doc.content.size &&
|
|
391
|
+
!slice.openStart &&
|
|
392
|
+
slice.content.childCount === 1 &&
|
|
393
|
+
first?.isTextblock &&
|
|
394
|
+
!first.content.size;
|
|
395
|
+
const { start, end } =
|
|
396
|
+
range.a < range.b
|
|
397
|
+
? deleteRange(range.a, range.b, slice.size > 0 && !emptyBlock)
|
|
398
|
+
: { start: here(range.a), end: here(range.a) };
|
|
399
|
+
let inserted: At | null = null;
|
|
400
|
+
const pos = end.pos;
|
|
401
|
+
if (slice.size && !emptyBlock) {
|
|
402
|
+
const host = out.doc.resolve(pos).parent;
|
|
403
|
+
const mark = adjacent(insertion, pos, pos) ?? insertion.create({ id: newId(), author, date });
|
|
404
|
+
const content = markFragment(slice.content, mark, insertion, deletion);
|
|
405
|
+
out.replace(pos, pos, new Slice(content, slice.openStart, slice.openEnd));
|
|
406
|
+
inserted = here(pos);
|
|
407
|
+
const size = slice.size;
|
|
408
|
+
out.doc.nodesBetween(pos, pos + size, (node, p) => {
|
|
409
|
+
if (!node.isTextblock) return !node.isInline;
|
|
410
|
+
const close = p + node.nodeSize - 1;
|
|
411
|
+
if (close >= pos && close < pos + size)
|
|
412
|
+
pending.push({ at: here(p), kind: "insertion", revision: revisionOf(mark) });
|
|
413
|
+
return false;
|
|
414
|
+
});
|
|
415
|
+
// An open end: the paragraph there keeps the original mark with the inserted content's
|
|
416
|
+
// properties (Enter at the end of a heading: the next style), a change of its formatting.
|
|
417
|
+
const $end = out.doc.resolve(pos + size);
|
|
418
|
+
if (slice.openEnd && host.isTextblock && $end.parent.isTextblock && $end.depth > 0)
|
|
419
|
+
formats.push({ at: here($end.before()), host });
|
|
420
|
+
}
|
|
421
|
+
records.unshift({ range, start, end, inserted });
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/** A position of `tr`'s final document in the rebuilt one. */
|
|
425
|
+
const toOut = (p: number, assoc: number): number => {
|
|
426
|
+
let delta = 0;
|
|
427
|
+
for (const { range, start, end, inserted } of records) {
|
|
428
|
+
const { a, b, c, d } = range;
|
|
429
|
+
if (p < c) break;
|
|
430
|
+
if (p <= d) {
|
|
431
|
+
if (inserted) return at(inserted, -1) + (p - c);
|
|
432
|
+
return c === d && assoc < 0 ? at(start, -1) : at(end, 1);
|
|
433
|
+
}
|
|
434
|
+
delta += d - c - (b - a);
|
|
435
|
+
}
|
|
436
|
+
return out.mapping.map(p - delta, assoc);
|
|
437
|
+
};
|
|
438
|
+
|
|
439
|
+
// Formatting changes (9.3): what the formatting was before the first tracked change, kept on
|
|
440
|
+
// the run (`formatChange` mark) or the paragraph (`formatChange` attribute). One's own
|
|
441
|
+
// insertions record none: they are new.
|
|
442
|
+
const formatChange = state.schema.marks.formatChange;
|
|
443
|
+
const ownInsertion = (node: Node) => insertion.isInSet(node.marks)?.attrs.author === author;
|
|
444
|
+
const recordRun = (step: AddMarkStep | RemoveMarkStep) => {
|
|
445
|
+
if (!formatChange || !RUN_MARKS.has(step.mark.type.name)) return;
|
|
446
|
+
const segments: { from: number; to: number; previous: string }[] = [];
|
|
447
|
+
out.doc.nodesBetween(step.from, step.to, (node, pos) => {
|
|
448
|
+
if (!node.isInline) return true;
|
|
449
|
+
if (MARKERS.has(node.type.name) || formatChange.isInSet(node.marks) || ownInsertion(node))
|
|
450
|
+
return false;
|
|
451
|
+
const present = step.mark.isInSet(node.marks);
|
|
452
|
+
if (step instanceof AddMarkStep ? present : !present) return false;
|
|
453
|
+
const previous = JSON.stringify(
|
|
454
|
+
node.marks.filter((m) => RUN_MARKS.has(m.type.name)).map((m) => m.toJSON()),
|
|
455
|
+
);
|
|
456
|
+
const from = Math.max(pos, step.from);
|
|
457
|
+
const to = Math.min(pos + node.nodeSize, step.to);
|
|
458
|
+
const last = segments[segments.length - 1];
|
|
459
|
+
if (last && last.to === from && last.previous === previous) last.to = to;
|
|
460
|
+
else segments.push({ from, to, previous });
|
|
461
|
+
return false;
|
|
462
|
+
});
|
|
463
|
+
for (const { from, to, previous } of segments) {
|
|
464
|
+
const attrs = { id: newId(), author, date, previous: JSON.parse(previous) };
|
|
465
|
+
out.addMark(from, to, formatChange.create(attrs));
|
|
466
|
+
}
|
|
467
|
+
};
|
|
468
|
+
const recordParagraph = (step: NodeMarkupStep): NodeMarkupStep => {
|
|
469
|
+
const node = out.doc.nodeAt(step.pos);
|
|
470
|
+
const attrs = step.attrs;
|
|
471
|
+
if (!node?.isTextblock || !attrs || node.type !== step.type) return step;
|
|
472
|
+
// Only the mark's attributes change (a split moves them to the second paragraph): as is.
|
|
473
|
+
if (sameProperties(node.attrs, attrs)) return step;
|
|
474
|
+
// A record stays (the properties before the first change), whatever the step sets.
|
|
475
|
+
if (node.attrs.formatChange) {
|
|
476
|
+
if (deepEqual(attrs.formatChange ?? null, node.attrs.formatChange)) return step;
|
|
477
|
+
const kept = { ...attrs, formatChange: node.attrs.formatChange };
|
|
478
|
+
return new NodeMarkupStep(step.pos, step.type, kept, step.marks);
|
|
479
|
+
}
|
|
480
|
+
const revision = node.attrs.revision as ParagraphRevision | null;
|
|
481
|
+
if (revision?.insertion?.author === author) return step;
|
|
482
|
+
const formatChange = { id: newId(), author, date, previous: propertiesOf(node.attrs) };
|
|
483
|
+
return new NodeMarkupStep(step.pos, step.type, { ...attrs, formatChange }, step.marks);
|
|
484
|
+
};
|
|
485
|
+
|
|
486
|
+
// Steps that move nothing (marks, attributes, comments), replayed on the rebuilt document.
|
|
487
|
+
// An attribute change of a paragraph whose mark is now a tracked deletion (the merged
|
|
488
|
+
// paragraph of a join) does not apply: the paragraphs stay apart.
|
|
489
|
+
tr.steps.forEach((step, k) => {
|
|
490
|
+
if (step.getMap().ranges.length) return;
|
|
491
|
+
const rest = tr.mapping.slice(k + 1);
|
|
492
|
+
const mapping: Mappable = {
|
|
493
|
+
map: (p, assoc = 1) => toOut(rest.map(p, assoc), assoc),
|
|
494
|
+
mapResult: (p, assoc = 1): MapResult => {
|
|
495
|
+
const result = rest.mapResult(p, assoc);
|
|
496
|
+
return { pos: toOut(result.pos, assoc), deleted: result.deleted, recover: null };
|
|
497
|
+
},
|
|
498
|
+
};
|
|
499
|
+
let mapped: Step | null = step.map(mapping);
|
|
500
|
+
// Paragraphs joined for real already have their attributes (`keepLastMark` and the record).
|
|
501
|
+
const joins = new Set([...apart, ...rejoined].map((x) => at(x, 1)));
|
|
502
|
+
if (mapped instanceof NodeMarkupStep && joins.has(mapped.pos)) mapped = null;
|
|
503
|
+
if (mapped instanceof NodeMarkupBatchStep) {
|
|
504
|
+
const kept = mapped.steps.filter((s) => !joins.has(s.pos));
|
|
505
|
+
mapped = kept.length ? new NodeMarkupBatchStep(kept) : null;
|
|
506
|
+
}
|
|
507
|
+
if (mapped instanceof AddMarkStep || mapped instanceof RemoveMarkStep) recordRun(mapped);
|
|
508
|
+
if (mapped instanceof NodeMarkupStep) mapped = recordParagraph(mapped);
|
|
509
|
+
if (mapped instanceof NodeMarkupBatchStep)
|
|
510
|
+
mapped = new NodeMarkupBatchStep(mapped.steps.map(recordParagraph));
|
|
511
|
+
if (mapped) out.step(mapped);
|
|
512
|
+
});
|
|
513
|
+
|
|
514
|
+
// Paragraphs keeping the original mark with an inserted paragraph's properties.
|
|
515
|
+
for (const { at: where, host } of formats) {
|
|
516
|
+
const pos = at(where, 1);
|
|
517
|
+
const node = out.doc.nodeAt(pos);
|
|
518
|
+
const revision = node?.attrs.revision as ParagraphRevision | null | undefined;
|
|
519
|
+
if (
|
|
520
|
+
!node?.isTextblock ||
|
|
521
|
+
node.attrs.formatChange ||
|
|
522
|
+
revision?.insertion?.author === author ||
|
|
523
|
+
sameProperties(node.attrs, host.attrs)
|
|
524
|
+
)
|
|
525
|
+
continue;
|
|
526
|
+
const formatChange = { id: newId(), author, date, previous: propertiesOf(host.attrs) };
|
|
527
|
+
out.step(new NodeMarkupStep(pos, node.type, { ...node.attrs, formatChange }, node.marks));
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
// Paragraph marks inserted or deleted, set last so attribute changes above do not undo them.
|
|
531
|
+
const done = new Set<number>();
|
|
532
|
+
for (const { at: where, kind, revision } of pending) {
|
|
533
|
+
const pos = at(where, 1);
|
|
534
|
+
const node = out.doc.nodeAt(pos);
|
|
535
|
+
if (!node?.isTextblock || done.has(pos * 2 + (kind === "insertion" ? 1 : 0))) continue;
|
|
536
|
+
done.add(pos * 2 + (kind === "insertion" ? 1 : 0));
|
|
537
|
+
const current = (node.attrs.revision as ParagraphRevision | null) ?? {};
|
|
538
|
+
const next =
|
|
539
|
+
kind === "insertion" ? { insertion: revision } : { ...current, deletion: revision };
|
|
540
|
+
out.step(new NodeMarkupStep(pos, node.type, { ...node.attrs, revision: next }));
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
// The selection: a caret left of what Backspace deleted, right of what Delete deleted.
|
|
544
|
+
const only = ranges.length === 1 ? ranges[0] : undefined;
|
|
545
|
+
const back =
|
|
546
|
+
state.selection.empty &&
|
|
547
|
+
only !== undefined &&
|
|
548
|
+
only.c === only.d &&
|
|
549
|
+
state.selection.head === only.b;
|
|
550
|
+
const assoc = back ? -1 : 1;
|
|
551
|
+
const selection = tr.selection;
|
|
552
|
+
const anchor = toOut(selection.anchor, assoc);
|
|
553
|
+
const head = toOut(selection.head, assoc);
|
|
554
|
+
if (selection instanceof NodeSelection && out.doc.nodeAt(anchor)?.type === selection.node.type)
|
|
555
|
+
out.setSelection(NodeSelection.create(out.doc, anchor));
|
|
556
|
+
else if (selection instanceof TextSelection)
|
|
557
|
+
out.setSelection(TextSelection.create(out.doc, anchor, head));
|
|
558
|
+
else out.setSelection(Selection.near(out.doc.resolve(head)));
|
|
559
|
+
if (tr.storedMarks)
|
|
560
|
+
out.setStoredMarks(tr.storedMarks.filter((m) => m.type !== insertion && m.type !== deletion));
|
|
561
|
+
return out;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/** Ranges with touching ones joined. */
|
|
565
|
+
function joined(ranges: readonly [number, number][]): [number, number][] {
|
|
566
|
+
const out: [number, number][] = [];
|
|
567
|
+
for (const [s, e] of [...ranges].sort((x, y) => x[0] - y[0])) {
|
|
568
|
+
const last = out[out.length - 1];
|
|
569
|
+
if (last && s <= last[1]) last[1] = Math.max(last[1], e);
|
|
570
|
+
else out.push([s, e]);
|
|
571
|
+
}
|
|
572
|
+
return out;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
/**
|
|
576
|
+
* `fragment` as inserted content: inline content marked with `mark` (other review marks
|
|
577
|
+
* dropped: the content is new, in one author's name).
|
|
578
|
+
*/
|
|
579
|
+
function markFragment(
|
|
580
|
+
fragment: Fragment,
|
|
581
|
+
mark: Mark,
|
|
582
|
+
insertion: MarkType,
|
|
583
|
+
deletion: MarkType,
|
|
584
|
+
): Fragment {
|
|
585
|
+
const nodes: Node[] = [];
|
|
586
|
+
fragment.forEach((node) => {
|
|
587
|
+
if (node.isInline) {
|
|
588
|
+
if (MARKERS.has(node.type.name)) nodes.push(node);
|
|
589
|
+
else {
|
|
590
|
+
const marks = node.marks.filter((m) => m.type !== insertion && m.type !== deletion);
|
|
591
|
+
nodes.push(node.mark(mark.addToSet(marks)));
|
|
592
|
+
}
|
|
593
|
+
return;
|
|
594
|
+
}
|
|
595
|
+
// A paragraph keeps its revision: an open end's is the original mark's, and the marks
|
|
596
|
+
// inserted get theirs afterwards.
|
|
597
|
+
if (!node.isTextblock)
|
|
598
|
+
throw new TransformError("A table cannot be inserted as a tracked change");
|
|
599
|
+
nodes.push(node.copy(markFragment(node.content, mark, insertion, deletion)));
|
|
600
|
+
});
|
|
601
|
+
return Fragment.fromArray(nodes);
|
|
602
|
+
}
|