@folijs/core 0.5.0 → 0.6.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 +28 -0
- package/dist/state/commands.d.ts +2 -0
- package/dist/state/commands.d.ts.map +1 -1
- package/dist/state/commands.js +38 -6
- package/dist/state/commands.js.map +1 -1
- package/dist/state/comments.d.ts.map +1 -1
- package/dist/state/comments.js +12 -3
- package/dist/state/comments.js.map +1 -1
- package/dist/state/controls.d.ts +128 -0
- package/dist/state/controls.d.ts.map +1 -0
- package/dist/state/controls.js +1459 -0
- package/dist/state/controls.js.map +1 -0
- package/dist/state/editor.d.ts +86 -1
- package/dist/state/editor.d.ts.map +1 -1
- package/dist/state/editor.js +168 -42
- package/dist/state/editor.js.map +1 -1
- package/dist/state/fields.d.ts +79 -0
- package/dist/state/fields.d.ts.map +1 -0
- package/dist/state/fields.js +398 -0
- package/dist/state/fields.js.map +1 -0
- package/dist/state/fieldupdate.d.ts +178 -0
- package/dist/state/fieldupdate.d.ts.map +1 -0
- package/dist/state/fieldupdate.js +1574 -0
- package/dist/state/fieldupdate.js.map +1 -0
- package/dist/state/find.d.ts +68 -0
- package/dist/state/find.d.ts.map +1 -0
- package/dist/state/find.js +397 -0
- package/dist/state/find.js.map +1 -0
- package/dist/state/history.d.ts +12 -1
- package/dist/state/history.d.ts.map +1 -1
- package/dist/state/history.js +43 -7
- package/dist/state/history.js.map +1 -1
- package/dist/state/index.d.ts +12 -5
- package/dist/state/index.d.ts.map +1 -1
- package/dist/state/index.js +10 -3
- package/dist/state/index.js.map +1 -1
- package/dist/state/mark.d.ts.map +1 -1
- package/dist/state/mark.js +8 -1
- package/dist/state/mark.js.map +1 -1
- package/dist/state/navigation.d.ts +70 -0
- package/dist/state/navigation.d.ts.map +1 -0
- package/dist/state/navigation.js +226 -0
- package/dist/state/navigation.js.map +1 -0
- package/dist/state/notes.d.ts +94 -0
- package/dist/state/notes.d.ts.map +1 -0
- package/dist/state/notes.js +444 -0
- package/dist/state/notes.js.map +1 -0
- package/dist/state/review.d.ts +13 -4
- package/dist/state/review.d.ts.map +1 -1
- package/dist/state/review.js +133 -18
- package/dist/state/review.js.map +1 -1
- package/dist/state/sections.d.ts +101 -0
- package/dist/state/sections.d.ts.map +1 -0
- package/dist/state/sections.js +472 -0
- package/dist/state/sections.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 +4 -0
- package/dist/state/state.js.map +1 -1
- package/dist/state/stories.d.ts +41 -0
- package/dist/state/stories.d.ts.map +1 -0
- package/dist/state/stories.js +243 -0
- package/dist/state/stories.js.map +1 -0
- package/dist/state/tables.d.ts.map +1 -1
- package/dist/state/tables.js +2 -1
- package/dist/state/tables.js.map +1 -1
- package/dist/state/track.d.ts +23 -0
- package/dist/state/track.d.ts.map +1 -1
- package/dist/state/track.js +58 -9
- package/dist/state/track.js.map +1 -1
- package/dist/state/word.d.ts +78 -16
- package/dist/state/word.d.ts.map +1 -1
- package/dist/state/word.js +230 -10
- package/dist/state/word.js.map +1 -1
- package/dist/state/wordclip.d.ts +15 -6
- package/dist/state/wordclip.d.ts.map +1 -1
- package/dist/state/wordclip.js +192 -14
- package/dist/state/wordclip.js.map +1 -1
- package/dist/state/wordformat.d.ts +1 -1
- package/dist/state/wordformat.d.ts.map +1 -1
- package/dist/state/wordformat.js +60 -1
- package/dist/state/wordformat.js.map +1 -1
- package/dist/v3/document.d.ts +130 -7
- package/dist/v3/document.d.ts.map +1 -1
- package/dist/v3/document.js +172 -30
- package/dist/v3/document.js.map +1 -1
- package/dist/v3/index.d.ts +1 -1
- package/dist/v3/index.d.ts.map +1 -1
- package/dist/v3/index.js +1 -1
- package/dist/v3/index.js.map +1 -1
- package/dist/v3/resolve.d.ts +9 -3
- package/dist/v3/resolve.d.ts.map +1 -1
- package/dist/v3/resolve.js +22 -4
- package/dist/v3/resolve.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 +89 -3
- package/dist/v3/schema.js.map +1 -1
- package/package.json +1 -1
- package/src/state/commands.ts +41 -4
- package/src/state/comments.ts +11 -3
- package/src/state/controls.ts +1596 -0
- package/src/state/editor.ts +291 -47
- package/src/state/fields.ts +432 -0
- package/src/state/fieldupdate.ts +1770 -0
- package/src/state/find.ts +510 -0
- package/src/state/history.ts +51 -7
- package/src/state/index.ts +123 -0
- package/src/state/mark.ts +8 -1
- package/src/state/navigation.ts +305 -0
- package/src/state/notes.ts +519 -0
- package/src/state/numeric-pictures.word.json +172 -0
- package/src/state/review.ts +171 -19
- package/src/state/sections.ts +571 -0
- package/src/state/state.ts +5 -0
- package/src/state/stories.ts +285 -0
- package/src/state/tables.ts +2 -1
- package/src/state/track.ts +65 -10
- package/src/state/word.ts +275 -12
- package/src/state/wordclip.ts +211 -29
- package/src/state/wordformat.ts +61 -1
- package/src/v3/document.ts +326 -41
- package/src/v3/index.ts +7 -0
- package/src/v3/resolve.ts +33 -9
- package/src/v3/schema.ts +92 -3
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Editing a story (ADR-0007): a header, footer or note has an `EditorState` of its own (selection,
|
|
3
|
+
* stored marks, tracking) but no history. **The body goes first:** a story's transaction is
|
|
4
|
+
* computed without being kept, the body receives it as a `StoryStep`, and the story takes the new
|
|
5
|
+
* state only if the body then holds its tree. A `StoryStep` arriving from the body (undo, another
|
|
6
|
+
* editor of the story) replaces the state's document.
|
|
7
|
+
*/
|
|
8
|
+
import { FoliDocumentError, type Node } from "../model/index.js";
|
|
9
|
+
import type { StoryJSON } from "../v3/document.js";
|
|
10
|
+
import { storyRules } from "../v3/document.js";
|
|
11
|
+
import type { Editor } from "./editor.js";
|
|
12
|
+
import { closeHistory, joinHistory, type StoryEdit, storyEdit } from "./history.js";
|
|
13
|
+
import { Selection, type SelectionJSON } from "./selection.js";
|
|
14
|
+
import { EditorState, Plugin, type Transaction } from "./state.js";
|
|
15
|
+
import {
|
|
16
|
+
maxRevisionIn,
|
|
17
|
+
revisionFloor,
|
|
18
|
+
type TrackState,
|
|
19
|
+
trackState,
|
|
20
|
+
trackTransaction,
|
|
21
|
+
untracked,
|
|
22
|
+
withTrackState,
|
|
23
|
+
} from "./track.js";
|
|
24
|
+
import {
|
|
25
|
+
CommentStep,
|
|
26
|
+
EnvelopeStep,
|
|
27
|
+
envelopeChange,
|
|
28
|
+
type Story,
|
|
29
|
+
type StoryBody,
|
|
30
|
+
StoryStep,
|
|
31
|
+
storyBody,
|
|
32
|
+
storyBodyKey,
|
|
33
|
+
type WordContext,
|
|
34
|
+
withContext,
|
|
35
|
+
withEnvelope,
|
|
36
|
+
wordContext,
|
|
37
|
+
} from "./word.js";
|
|
38
|
+
|
|
39
|
+
/** What a story's editor needs of the body's. */
|
|
40
|
+
export interface StoryHost extends StoryBody {
|
|
41
|
+
/** The plugins of a story's state (keymaps, tracking, the word context). */
|
|
42
|
+
plugins(context: WordContext, track: TrackState | null): Plugin[];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Puts on `bodyTr` what `transactions` did to story `id`, from `before` to `next`. */
|
|
46
|
+
function carryStoryChange(
|
|
47
|
+
bodyTr: Transaction,
|
|
48
|
+
bodyContext: WordContext,
|
|
49
|
+
id: string,
|
|
50
|
+
before: EditorState,
|
|
51
|
+
next: EditorState,
|
|
52
|
+
transactions: readonly Transaction[],
|
|
53
|
+
): { after: Story; patched: boolean } {
|
|
54
|
+
const story = bodyContext.stories.get(id) as Story;
|
|
55
|
+
let after = story;
|
|
56
|
+
if (next.doc !== before.doc) {
|
|
57
|
+
after = { ...story, doc: next.doc };
|
|
58
|
+
bodyTr.step(
|
|
59
|
+
new StoryStep(id, story, after, before.selection.toJSON(), next.selection.toJSON()),
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
let patched = false;
|
|
63
|
+
for (const t of transactions) {
|
|
64
|
+
// What belongs to the document, not to the story: the body takes it.
|
|
65
|
+
for (const step of t.steps)
|
|
66
|
+
if (step instanceof CommentStep || step instanceof EnvelopeStep) bodyTr.step(step);
|
|
67
|
+
patched ||= envelopeChange(t) !== undefined;
|
|
68
|
+
}
|
|
69
|
+
// Only these: the story's copy of the rest of the envelope may be behind the body's.
|
|
70
|
+
if (patched) {
|
|
71
|
+
const { numbering, styles, resources, controls } = (wordContext(next) as WordContext).envelope;
|
|
72
|
+
const {
|
|
73
|
+
resources: _resources,
|
|
74
|
+
controls: _controls,
|
|
75
|
+
...rest
|
|
76
|
+
} = envelopeChange(bodyTr) ?? bodyContext.envelope;
|
|
77
|
+
withEnvelope(bodyTr, {
|
|
78
|
+
...rest,
|
|
79
|
+
numbering,
|
|
80
|
+
styles,
|
|
81
|
+
...(resources ? { resources } : {}),
|
|
82
|
+
...(controls ? { controls } : {}),
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
return { after, patched };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** What changes one tree of the document: its id (null: the body) and the edit. */
|
|
89
|
+
export type TreeEdits = ReadonlyMap<string | null, (tr: Transaction) => void>;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Edits several trees as one transaction of the body, so one undo step. `tracked`: as tracked
|
|
93
|
+
* changes when tracking is on. Returns the trees whose edit was refused; the others are made.
|
|
94
|
+
*/
|
|
95
|
+
export function editTrees(
|
|
96
|
+
editor: Editor,
|
|
97
|
+
edits: TreeEdits,
|
|
98
|
+
{ tracked }: { tracked: boolean },
|
|
99
|
+
): Set<string | null> {
|
|
100
|
+
const host = storyBody(editor.state) ?? editor;
|
|
101
|
+
const state = host.state;
|
|
102
|
+
const context = wordContext(state) as WordContext;
|
|
103
|
+
const failed = new Set<string | null>();
|
|
104
|
+
const keep = (tr: Transaction) => (tracked ? trackTransaction(state, tr) : tr);
|
|
105
|
+
let out = state.tr;
|
|
106
|
+
const bodyEdit = edits.get(null);
|
|
107
|
+
if (bodyEdit) {
|
|
108
|
+
bodyEdit(out);
|
|
109
|
+
const made = keep(out);
|
|
110
|
+
if (made) out = made;
|
|
111
|
+
else {
|
|
112
|
+
failed.add(null);
|
|
113
|
+
out = state.tr;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
let floor = tracked ? maxRevisionIn(out.doc) + 1 : 0;
|
|
117
|
+
for (const [id, edit] of edits) {
|
|
118
|
+
if (id === null) continue;
|
|
119
|
+
const story = editor.story(id);
|
|
120
|
+
if (!story) {
|
|
121
|
+
failed.add(id);
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
const before = story.state;
|
|
125
|
+
const tr = before.tr;
|
|
126
|
+
edit(tr);
|
|
127
|
+
if (!tr.docChanged) continue;
|
|
128
|
+
const made = tracked
|
|
129
|
+
? trackTransaction(before, tr.setMeta(revisionFloor, floor))
|
|
130
|
+
: tr.setMeta(untracked, true);
|
|
131
|
+
const result = made ? before.applyTransaction(made) : null;
|
|
132
|
+
if (!result?.transactions.length) {
|
|
133
|
+
failed.add(id);
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
carryStoryChange(out, context, id, before, result.state, result.transactions);
|
|
137
|
+
if (tracked) floor = Math.max(floor, maxRevisionIn(result.state.doc) + 1);
|
|
138
|
+
}
|
|
139
|
+
if (!out.docChanged) return failed;
|
|
140
|
+
host.dispatch(out.setMeta(untracked, true).setMeta(closeHistory, true));
|
|
141
|
+
// Refused by the body's filters: nothing happened, in any tree.
|
|
142
|
+
if (wordContext(host.state) === context && host.state.doc === state.doc)
|
|
143
|
+
for (const id of edits.keys()) failed.add(id);
|
|
144
|
+
return failed;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export type StoryListener = (state: EditorState, transactions: readonly Transaction[]) => void;
|
|
148
|
+
|
|
149
|
+
/** A story's editing state, kept in step with the body. */
|
|
150
|
+
export class StoryEditing {
|
|
151
|
+
state: EditorState;
|
|
152
|
+
/** Set while the story's own transaction goes through the body. */
|
|
153
|
+
private own = false;
|
|
154
|
+
/** Whether the story still exists: a removed one takes no more changes. */
|
|
155
|
+
alive = true;
|
|
156
|
+
readonly listeners = new Set<StoryListener>();
|
|
157
|
+
|
|
158
|
+
constructor(
|
|
159
|
+
readonly id: string,
|
|
160
|
+
private readonly host: StoryHost,
|
|
161
|
+
) {
|
|
162
|
+
const doc = this.tree()?.doc as Node;
|
|
163
|
+
this.state = this.create(doc);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
private tree(): Story | undefined {
|
|
167
|
+
return wordContext(this.host.state)?.stories.get(this.id);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
private create(doc: Node): EditorState {
|
|
171
|
+
const { host, id } = this;
|
|
172
|
+
const context = wordContext(host.state) as WordContext;
|
|
173
|
+
const kind = context.stories.get(id)?.kind as StoryJSON["kind"];
|
|
174
|
+
return EditorState.create({
|
|
175
|
+
doc,
|
|
176
|
+
selection: Selection.atStart(doc),
|
|
177
|
+
plugins: [
|
|
178
|
+
new Plugin<StoryBody>({
|
|
179
|
+
key: storyBodyKey,
|
|
180
|
+
state: { init: () => host, apply: () => host },
|
|
181
|
+
}),
|
|
182
|
+
// What a story may not hold (a section break, a comment) is never put in it.
|
|
183
|
+
new Plugin({
|
|
184
|
+
filterTransaction(tr) {
|
|
185
|
+
if (!tr.docChanged) return true;
|
|
186
|
+
try {
|
|
187
|
+
storyRules(tr.doc, kind, "story");
|
|
188
|
+
return true;
|
|
189
|
+
} catch (error) {
|
|
190
|
+
if (error instanceof FoliDocumentError) return false;
|
|
191
|
+
throw error;
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
}),
|
|
195
|
+
...host.plugins(context, trackState(host.state)),
|
|
196
|
+
],
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
private notify(transactions: readonly Transaction[]): void {
|
|
201
|
+
for (const listener of this.listeners) listener(this.state, transactions);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** `state` with the body's context and tracking; its plugins keep their state. */
|
|
205
|
+
private current(state: EditorState): EditorState {
|
|
206
|
+
const context = wordContext(this.host.state) as WordContext;
|
|
207
|
+
const track = trackState(this.host.state);
|
|
208
|
+
if (wordContext(state) === context && trackState(state) === track) return state;
|
|
209
|
+
const tr = withContext(state.tr, context);
|
|
210
|
+
if (track) withTrackState(tr, track);
|
|
211
|
+
return state.apply(tr);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
dispatch = (tr: Transaction): void => {
|
|
215
|
+
if (!this.alive) return;
|
|
216
|
+
const { host, id, state: before } = this;
|
|
217
|
+
const tracked = trackTransaction(before, tr);
|
|
218
|
+
if (!tracked) return;
|
|
219
|
+
const result = before.applyTransaction(tracked);
|
|
220
|
+
if (!result.transactions.length) return;
|
|
221
|
+
const next = result.state;
|
|
222
|
+
const bodyState = host.state;
|
|
223
|
+
const bodyContext = wordContext(bodyState) as WordContext;
|
|
224
|
+
const story = bodyContext.stories.get(id) as Story;
|
|
225
|
+
const bodyTr = bodyState.tr;
|
|
226
|
+
const { after, patched } = carryStoryChange(
|
|
227
|
+
bodyTr,
|
|
228
|
+
bodyContext,
|
|
229
|
+
id,
|
|
230
|
+
before,
|
|
231
|
+
next,
|
|
232
|
+
result.transactions,
|
|
233
|
+
);
|
|
234
|
+
const track = trackState(next);
|
|
235
|
+
const trackChanged = !!track && track !== trackState(before);
|
|
236
|
+
if (trackChanged) withTrackState(bodyTr, track);
|
|
237
|
+
if (bodyTr.steps.length || patched || trackChanged) {
|
|
238
|
+
bodyTr.setMeta(untracked, true);
|
|
239
|
+
bodyTr.setMeta(storyEdit, { id, tr: tracked } satisfies StoryEdit);
|
|
240
|
+
bodyTr.time = tracked.time;
|
|
241
|
+
if (!tracked.addToHistory) bodyTr.setMeta("addToHistory", false);
|
|
242
|
+
if (tracked.getMeta(closeHistory)) bodyTr.setMeta(closeHistory, true);
|
|
243
|
+
if (tracked.getMeta(joinHistory)) bodyTr.setMeta(joinHistory, true);
|
|
244
|
+
this.own = true;
|
|
245
|
+
try {
|
|
246
|
+
host.dispatch(bodyTr);
|
|
247
|
+
} finally {
|
|
248
|
+
this.own = false;
|
|
249
|
+
}
|
|
250
|
+
// Refused by the body: the story stays as it was.
|
|
251
|
+
if (after !== story && wordContext(host.state)?.stories.get(id) !== after) return;
|
|
252
|
+
}
|
|
253
|
+
this.state = this.current(next);
|
|
254
|
+
this.notify(result.transactions);
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
/** After a change of the body. */
|
|
258
|
+
sync(transactions: readonly Transaction[]): void {
|
|
259
|
+
if (this.own) return;
|
|
260
|
+
const tree = this.tree();
|
|
261
|
+
if (!tree) {
|
|
262
|
+
this.retire();
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
const { state } = this;
|
|
266
|
+
let selection: SelectionJSON | null = null;
|
|
267
|
+
for (const tr of transactions)
|
|
268
|
+
for (const step of tr.steps)
|
|
269
|
+
if (step instanceof StoryStep && step.id === this.id) selection = step.selectionAfter;
|
|
270
|
+
if (tree.doc !== state.doc) {
|
|
271
|
+
const json = selection ?? state.selection.toJSON();
|
|
272
|
+
this.state = this.current(state.withDocument(tree.doc, Selection.fromJSON(tree.doc, json)));
|
|
273
|
+
this.notify([]);
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
const current = this.current(state);
|
|
277
|
+
if (current === state) return;
|
|
278
|
+
this.state = current;
|
|
279
|
+
this.notify([]);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
retire(): void {
|
|
283
|
+
this.alive = false;
|
|
284
|
+
}
|
|
285
|
+
}
|
package/src/state/tables.ts
CHANGED
|
@@ -112,7 +112,8 @@ function deleteBetween(tr: Transaction, a: number, b: number): void {
|
|
|
112
112
|
* (paragraphs in a cell joined), so a tracked clear can be rejected; a cell holding other
|
|
113
113
|
* blocks (a nested table) gets one empty paragraph instead.
|
|
114
114
|
*
|
|
115
|
-
* shortcut: the cells in document order, not Word's rectangle (
|
|
115
|
+
* shortcut: the cells in document order between the range's ends, not Word's rectangle (a
|
|
116
|
+
* `CellSelection` deletes its own rectangle).
|
|
116
117
|
*/
|
|
117
118
|
function clearCells(tr: Transaction, from: number, to: number, start: number, end: number): void {
|
|
118
119
|
const cells: { pos: number; node: Node }[] = [];
|
package/src/state/track.ts
CHANGED
|
@@ -30,12 +30,12 @@ import {
|
|
|
30
30
|
type Step,
|
|
31
31
|
} from "../transform/steps.js";
|
|
32
32
|
import { TransformError } from "../transform/transform.js";
|
|
33
|
-
import type { CommentJSON } from "../v3/document.js";
|
|
33
|
+
import type { CommentJSON, SectionJSON } from "../v3/document.js";
|
|
34
34
|
import { PARAGRAPH_PROPERTY_KEYS, RUN_PROPERTY_MARKS } from "../v3/schema.js";
|
|
35
35
|
import { MARK_ATTRS } from "./mark.js";
|
|
36
36
|
import { NodeSelection, Selection, TextSelection } from "./selection.js";
|
|
37
37
|
import { type Command, type EditorState, Plugin, PluginKey, type Transaction } from "./state.js";
|
|
38
|
-
import { wordContext } from "./word.js";
|
|
38
|
+
import { otherTrees, wordContext } from "./word.js";
|
|
39
39
|
|
|
40
40
|
/**
|
|
41
41
|
* How tracked changes show: `markup` (inserted text underlined and deleted text struck through,
|
|
@@ -58,8 +58,24 @@ const key = new PluginKey<TrackState>("track");
|
|
|
58
58
|
/** Set this meta (true) on a transaction to keep it out of tracking. */
|
|
59
59
|
export const untracked = "untracked";
|
|
60
60
|
|
|
61
|
+
/**
|
|
62
|
+
* @internal On a transaction: the lowest id its tracked changes may take, for one operation over
|
|
63
|
+
* several trees (each is rebuilt before the body holds the others' new ids).
|
|
64
|
+
*/
|
|
65
|
+
export const revisionFloor = "revisionFloor";
|
|
66
|
+
|
|
67
|
+
/** On a rebuilt transaction: the ranges it records as deleted, as `[from, to]` of the document before. */
|
|
68
|
+
export const trackedDeletes = "trackedDeletes";
|
|
69
|
+
|
|
61
70
|
/** Inline nodes a tracked change does not mark: they stand between runs in Word. */
|
|
62
|
-
const MARKERS = new Set([
|
|
71
|
+
const MARKERS = new Set([
|
|
72
|
+
"bookmarkStart",
|
|
73
|
+
"bookmarkEnd",
|
|
74
|
+
"commentStart",
|
|
75
|
+
"commentEnd",
|
|
76
|
+
"controlStart",
|
|
77
|
+
"controlEnd",
|
|
78
|
+
]);
|
|
63
79
|
|
|
64
80
|
const RUN_MARKS = new Set<string>(RUN_PROPERTY_MARKS);
|
|
65
81
|
/**
|
|
@@ -102,6 +118,10 @@ export function trackState(state: EditorState): TrackState | null {
|
|
|
102
118
|
return key.getState(state) ?? null;
|
|
103
119
|
}
|
|
104
120
|
|
|
121
|
+
/** Makes `tr` set the tracking state (a story's editor passes its change to the body). */
|
|
122
|
+
export const withTrackState = (tr: Transaction, state: TrackState): Transaction =>
|
|
123
|
+
tr.setMeta(key, state);
|
|
124
|
+
|
|
105
125
|
/** Turns tracking on or off (and sets the author). Applies only to a schema v3 editor. */
|
|
106
126
|
export function setTrackChanges(on: boolean, author?: string): Command {
|
|
107
127
|
return (state, dispatch) => {
|
|
@@ -129,26 +149,43 @@ export function now(): string {
|
|
|
129
149
|
|
|
130
150
|
const REVIEW_MARKS = new Set(["insertion", "deletion", "formatChange"]);
|
|
131
151
|
|
|
132
|
-
/** The largest change id
|
|
133
|
-
function
|
|
152
|
+
/** @internal The largest change id on a tree's nodes, or -1. */
|
|
153
|
+
export function maxRevisionIn(doc: Node): number {
|
|
134
154
|
let max = -1;
|
|
135
155
|
const see = (value: unknown) => {
|
|
136
156
|
const id = (value as { id?: unknown } | null | undefined)?.id;
|
|
137
157
|
if (typeof id === "number" && id > max) max = id;
|
|
138
158
|
};
|
|
139
159
|
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
|
-
}
|
|
160
|
+
for (const mark of node.marks) if (REVIEW_MARKS.has(mark.type.name)) see(mark.attrs);
|
|
144
161
|
if (node.isTextblock) {
|
|
145
162
|
const revision = node.attrs.revision as ParagraphRevision | null | undefined;
|
|
146
163
|
see(revision?.insertion);
|
|
147
164
|
see(revision?.deletion);
|
|
148
165
|
see(node.attrs.formatChange);
|
|
166
|
+
see((node.attrs.section as SectionJSON | null | undefined)?.change);
|
|
149
167
|
}
|
|
150
168
|
return true;
|
|
151
169
|
});
|
|
170
|
+
return max;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* The largest change id in the document's trees (the body and every story: ids are allocated
|
|
175
|
+
* across them), the comments' content (marks, paragraph revisions) and the sections' tracked
|
|
176
|
+
* setups, or -1.
|
|
177
|
+
*/
|
|
178
|
+
function maxRevisionId(state: EditorState): number {
|
|
179
|
+
const envelope = wordContext(state)?.envelope;
|
|
180
|
+
const comments: readonly CommentJSON[] = envelope?.comments ?? [];
|
|
181
|
+
const last: SectionJSON | undefined = envelope?.section;
|
|
182
|
+
let max = -1;
|
|
183
|
+
const see = (value: unknown) => {
|
|
184
|
+
const id = (value as { id?: unknown } | null | undefined)?.id;
|
|
185
|
+
if (typeof id === "number" && id > max) max = id;
|
|
186
|
+
};
|
|
187
|
+
see(last?.change);
|
|
188
|
+
for (const doc of [state.doc, ...otherTrees(state)]) max = Math.max(max, maxRevisionIn(doc));
|
|
152
189
|
// Comments hold paragraphs too, which can carry changes (JSON: walked as it is).
|
|
153
190
|
const walk = (value: unknown): void => {
|
|
154
191
|
if (Array.isArray(value)) value.forEach(walk);
|
|
@@ -168,6 +205,18 @@ function maxRevisionId(doc: Node, comments: readonly CommentJSON[]): number {
|
|
|
168
205
|
return max;
|
|
169
206
|
}
|
|
170
207
|
|
|
208
|
+
/** @internal A new tracked change's id, author and date, when `state` is tracking; else null. */
|
|
209
|
+
export function newRevision(state: EditorState): Revision | null {
|
|
210
|
+
const track = key.getState(state);
|
|
211
|
+
if (!track?.on) return null;
|
|
212
|
+
return { id: nextRevisionId(state), author: track.author, date: now() };
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** @internal The id after every change of the document, in all its trees. */
|
|
216
|
+
export function nextRevisionId(state: EditorState): number {
|
|
217
|
+
return maxRevisionId(state) + 1;
|
|
218
|
+
}
|
|
219
|
+
|
|
171
220
|
/** A replaced range: `a..b` before the transaction, `c..d` after it. */
|
|
172
221
|
interface Range {
|
|
173
222
|
a: number;
|
|
@@ -252,11 +301,17 @@ function rebuild(
|
|
|
252
301
|
deletion: MarkType,
|
|
253
302
|
): Transaction {
|
|
254
303
|
const out = state.tr.copyMeta(tr);
|
|
304
|
+
out.setMeta(
|
|
305
|
+
trackedDeletes,
|
|
306
|
+
changedRanges(tr)
|
|
307
|
+
.filter((r) => r.b > r.a)
|
|
308
|
+
.map((r) => [r.a, r.b]),
|
|
309
|
+
);
|
|
255
310
|
const date = now();
|
|
256
311
|
let nextId = -1;
|
|
257
312
|
const newId = () => {
|
|
258
313
|
if (nextId < 0)
|
|
259
|
-
nextId = maxRevisionId(state
|
|
314
|
+
nextId = Math.max(maxRevisionId(state) + 1, (tr.getMeta(revisionFloor) as number) ?? 0);
|
|
260
315
|
return nextId++;
|
|
261
316
|
};
|
|
262
317
|
const at = (x: At, assoc: number) => out.mapping.slice(x.step).map(x.pos, assoc);
|