@folijs/core 0.4.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 +49 -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 +10 -1
- package/dist/state/commands.d.ts.map +1 -1
- package/dist/state/commands.js +125 -40
- package/dist/state/commands.js.map +1 -1
- package/dist/state/comments.d.ts.map +1 -1
- package/dist/state/comments.js +15 -6
- 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 +169 -41
- 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 +15 -5
- package/dist/state/index.d.ts.map +1 -1
- package/dist/state/index.js +13 -3
- 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.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/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 +13 -4
- package/dist/state/review.d.ts.map +1 -1
- package/dist/state/review.js +223 -29
- 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/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 +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/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 +730 -0
- package/dist/state/tables.js.map +1 -0
- package/dist/state/track.d.ts +23 -0
- package/dist/state/track.d.ts.map +1 -1
- package/dist/state/track.js +113 -15
- 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 +231 -11
- 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 +213 -18
- package/dist/state/wordclip.js.map +1 -1
- package/dist/state/wordformat.d.ts +10 -2
- package/dist/state/wordformat.d.ts.map +1 -1
- package/dist/state/wordformat.js +98 -17
- package/dist/state/wordformat.js.map +1 -1
- package/dist/v3/document.d.ts +237 -7
- package/dist/v3/document.d.ts.map +1 -1
- package/dist/v3/document.js +314 -15
- 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 +57 -5
- package/dist/v3/resolve.d.ts.map +1 -1
- package/dist/v3/resolve.js +224 -16
- 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 +254 -8
- 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 +121 -32
- package/src/state/comments.ts +14 -6
- package/src/state/controls.ts +1596 -0
- package/src/state/editor.ts +292 -46
- 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 +157 -0
- package/src/state/links.ts +27 -17
- 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/pictures.ts +168 -0
- package/src/state/review.ts +265 -31
- package/src/state/sections.ts +571 -0
- package/src/state/selection.ts +15 -1
- package/src/state/state.ts +5 -0
- package/src/state/stories.ts +285 -0
- package/src/state/tablecommands.ts +741 -0
- package/src/state/tables.ts +761 -0
- package/src/state/track.ts +128 -16
- package/src/state/word.ts +276 -13
- package/src/state/wordclip.ts +231 -32
- package/src/state/wordformat.ts +96 -14
- package/src/v3/document.ts +572 -16
- package/src/v3/index.ts +21 -0
- package/src/v3/migrate.ts +2 -2
- package/src/v3/resolve.ts +289 -21
- package/src/v3/schema.ts +278 -9
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);
|
|
@@ -294,6 +349,10 @@ function rebuild(
|
|
|
294
349
|
(mark ??= adjacent(deletion, from, to) ?? deletion.create({ id: newId(), author, date }));
|
|
295
350
|
const real: [number, number][] = [];
|
|
296
351
|
const marked: [number, number][] = [];
|
|
352
|
+
/** Whole rows deleted: recorded as the rows' deletion. */
|
|
353
|
+
const rows: { pos: number; node: Node }[] = [];
|
|
354
|
+
/** One's own inserted rows deleted, by table: they go (the table, when they are all its rows). */
|
|
355
|
+
const ownRows = new Map<number, [number, number][]>();
|
|
297
356
|
// Paragraphs joined for real (one's own inserted mark removed) become one, which starts
|
|
298
357
|
// where the first starts and ends with the last one's mark (`keepLastMark`), as an
|
|
299
358
|
// untracked join does.
|
|
@@ -334,9 +393,22 @@ function rebuild(
|
|
|
334
393
|
pending.push({ at: here(start), kind: "deletion", revision: revisionOf(markOf()) });
|
|
335
394
|
return true;
|
|
336
395
|
}
|
|
337
|
-
//
|
|
338
|
-
//
|
|
339
|
-
if (
|
|
396
|
+
// A whole row: its deletion is the row's (`w:trPr/w:del`), its content kept as Word keeps
|
|
397
|
+
// it; one's own inserted row goes for real.
|
|
398
|
+
if (node.type.name === "tableRow" && from <= pos && to >= pos + node.nodeSize) {
|
|
399
|
+
const revision = node.attrs.revision as ParagraphRevision | null;
|
|
400
|
+
if (revision?.insertion?.author === author && !revision.deletion) {
|
|
401
|
+
const table = doc.resolve(pos).before();
|
|
402
|
+
ownRows.set(table, [...(ownRows.get(table) ?? []), [pos, pos + node.nodeSize]]);
|
|
403
|
+
} else if (!revision?.deletion) rows.push({ pos, node });
|
|
404
|
+
return false;
|
|
405
|
+
}
|
|
406
|
+
// Tables, rows and cells partly covered: the text in them, cell by cell.
|
|
407
|
+
if (["table", "tableRow", "tableCell"].includes(node.type.name)) return true;
|
|
408
|
+
// shortcut: other blocks Foli does not model cannot be recorded as changed, so such an
|
|
409
|
+
// edit is not made while tracking.
|
|
410
|
+
if (!node.isInline)
|
|
411
|
+
throw new TransformError("This content cannot be deleted as a tracked change");
|
|
340
412
|
const s = Math.max(pos, from);
|
|
341
413
|
const e = Math.min(pos + node.nodeSize, to);
|
|
342
414
|
if (s >= e || MARKERS.has(node.type.name) || deletion.isInSet(node.marks)) return false;
|
|
@@ -344,7 +416,24 @@ function rebuild(
|
|
|
344
416
|
(own && own.attrs.author === author ? real : marked).push([s, e]);
|
|
345
417
|
return false;
|
|
346
418
|
});
|
|
419
|
+
for (const [table, list] of ownRows) {
|
|
420
|
+
const node = doc.nodeAt(table);
|
|
421
|
+
if (node && list.length === node.childCount) real.push([table, table + node.nodeSize]);
|
|
422
|
+
else real.push(...list);
|
|
423
|
+
}
|
|
347
424
|
for (const [s, e] of joined(marked)) out.addMark(s, e, markOf());
|
|
425
|
+
for (const { pos, node } of rows) {
|
|
426
|
+
const revision = (node.attrs.revision as ParagraphRevision | null) ?? {};
|
|
427
|
+
const deleted = revisionOf(markOf());
|
|
428
|
+
out.step(
|
|
429
|
+
new NodeMarkupStep(
|
|
430
|
+
pos,
|
|
431
|
+
node.type,
|
|
432
|
+
{ ...node.attrs, revision: { ...revision, deletion: deleted } },
|
|
433
|
+
node.marks,
|
|
434
|
+
),
|
|
435
|
+
);
|
|
436
|
+
}
|
|
348
437
|
for (const [s, e] of joined(real).reverse()) out.delete(s, e);
|
|
349
438
|
for (const { at: where, node, live } of merged) {
|
|
350
439
|
const pos = at(where, 1);
|
|
@@ -405,13 +494,31 @@ function rebuild(
|
|
|
405
494
|
out.replace(pos, pos, new Slice(content, slice.openStart, slice.openEnd));
|
|
406
495
|
inserted = here(pos);
|
|
407
496
|
const size = slice.size;
|
|
497
|
+
// Whole rows inserted: the rows' insertion (`w:trPr/w:ins`), beside their text's marks.
|
|
498
|
+
const newRows: number[] = [];
|
|
408
499
|
out.doc.nodesBetween(pos, pos + size, (node, p) => {
|
|
500
|
+
if (node.type.name === "tableRow" && p >= pos && p + node.nodeSize <= pos + size)
|
|
501
|
+
newRows.push(p);
|
|
409
502
|
if (!node.isTextblock) return !node.isInline;
|
|
410
503
|
const close = p + node.nodeSize - 1;
|
|
411
504
|
if (close >= pos && close < pos + size)
|
|
412
505
|
pending.push({ at: here(p), kind: "insertion", revision: revisionOf(mark) });
|
|
413
506
|
return false;
|
|
414
507
|
});
|
|
508
|
+
for (const p of newRows) {
|
|
509
|
+
const row = out.doc.nodeAt(p);
|
|
510
|
+
if (!row) continue;
|
|
511
|
+
const revision = (row.attrs.revision as ParagraphRevision | null) ?? {};
|
|
512
|
+
if (revision.insertion) continue;
|
|
513
|
+
out.step(
|
|
514
|
+
new NodeMarkupStep(
|
|
515
|
+
p,
|
|
516
|
+
row.type,
|
|
517
|
+
{ ...row.attrs, revision: { ...revision, insertion: revisionOf(mark) } },
|
|
518
|
+
row.marks,
|
|
519
|
+
),
|
|
520
|
+
);
|
|
521
|
+
}
|
|
415
522
|
// An open end: the paragraph there keeps the original mark with the inserted content's
|
|
416
523
|
// properties (Enter at the end of a heading: the next style), a change of its formatting.
|
|
417
524
|
const $end = out.doc.resolve(pos + size);
|
|
@@ -555,6 +662,9 @@ function rebuild(
|
|
|
555
662
|
out.setSelection(NodeSelection.create(out.doc, anchor));
|
|
556
663
|
else if (selection instanceof TextSelection)
|
|
557
664
|
out.setSelection(TextSelection.create(out.doc, anchor, head));
|
|
665
|
+
// Cells stay selected (`Selection.fromJSON` falls back to a caret when they are not cells).
|
|
666
|
+
else if (selection.toJSON().type === "cell")
|
|
667
|
+
out.setSelection(Selection.fromJSON(out.doc, { type: "cell", anchor, head }));
|
|
558
668
|
else out.setSelection(Selection.near(out.doc.resolve(head)));
|
|
559
669
|
if (tr.storedMarks)
|
|
560
670
|
out.setStoredMarks(tr.storedMarks.filter((m) => m.type !== insertion && m.type !== deletion));
|
|
@@ -593,9 +703,11 @@ function markFragment(
|
|
|
593
703
|
return;
|
|
594
704
|
}
|
|
595
705
|
// A paragraph keeps its revision: an open end's is the original mark's, and the marks
|
|
596
|
-
// inserted get theirs afterwards.
|
|
597
|
-
|
|
598
|
-
|
|
706
|
+
// inserted get theirs afterwards. Tables, rows and cells: their content marked (whole rows
|
|
707
|
+
// get their insertion afterwards too).
|
|
708
|
+
const container = ["table", "tableRow", "tableCell"].includes(node.type.name);
|
|
709
|
+
if (!node.isTextblock && !container)
|
|
710
|
+
throw new TransformError("This content cannot be inserted as a tracked change");
|
|
599
711
|
nodes.push(node.copy(markFragment(node.content, mark, insertion, deletion)));
|
|
600
712
|
});
|
|
601
713
|
return Fragment.fromArray(nodes);
|
package/src/state/word.ts
CHANGED
|
@@ -9,20 +9,29 @@
|
|
|
9
9
|
* shortcut: v2 and v3 editing live side by side until 8.7d makes v3 the only schema; then these
|
|
10
10
|
* become the commands themselves.
|
|
11
11
|
*/
|
|
12
|
+
import { deepEqual } from "../model/attrs.js";
|
|
12
13
|
import type { Attrs, Mark, MarkType, Node } from "../model/index.js";
|
|
13
14
|
import { Step, StepResult } from "../transform/steps.js";
|
|
14
|
-
import type { CommentJSON, FoliDocument } from "../v3/document.js";
|
|
15
|
+
import type { CommentJSON, FoliDocument, StoryJSON } from "../v3/document.js";
|
|
16
|
+
import { parseContent } from "../v3/document.js";
|
|
15
17
|
import { Resolver, type RunFormat } from "../v3/resolve.js";
|
|
16
18
|
import { MARK_ATTRS } from "./mark.js";
|
|
17
|
-
import { NodeSelection, TextSelection } from "./selection.js";
|
|
18
|
-
import { type EditorState, Plugin, PluginKey, type Transaction } from "./state.js";
|
|
19
|
+
import { NodeSelection, type SelectionJSON, TextSelection } from "./selection.js";
|
|
20
|
+
import { type Command, EditorState, Plugin, PluginKey, type Transaction } from "./state.js";
|
|
19
21
|
|
|
20
22
|
/** A v3 document without its content: what the editor keeps beside the edited tree. */
|
|
21
23
|
export type WordEnvelope = Omit<FoliDocument, "content">;
|
|
22
24
|
|
|
25
|
+
/** A story as the editor holds it: its tree, and what the envelope says beside it. */
|
|
26
|
+
export interface Story extends Omit<StoryJSON, "content"> {
|
|
27
|
+
readonly doc: Node;
|
|
28
|
+
}
|
|
29
|
+
|
|
23
30
|
export interface WordContext {
|
|
24
31
|
readonly envelope: WordEnvelope;
|
|
25
32
|
readonly resolver: Resolver;
|
|
33
|
+
/** The envelope's `stories` as trees: what `StoryStep`s hold and compare. */
|
|
34
|
+
readonly stories: ReadonlyMap<string, Story>;
|
|
26
35
|
/**
|
|
27
36
|
* Made when the document is loaded and kept while it is edited: a copy carries it, so a paste
|
|
28
37
|
* tells this document's lists from another's (Word's `w:nsid`; Foli makes equal definitions
|
|
@@ -108,27 +117,262 @@ export function setComments(
|
|
|
108
117
|
return tr;
|
|
109
118
|
}
|
|
110
119
|
|
|
120
|
+
/** What `EnvelopeStep` can change (ADR-0007). */
|
|
121
|
+
export type EnvelopeKey = "section" | "settings" | "controls";
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Changes one value of the envelope (the last section's setup, the document's settings) from
|
|
125
|
+
* `before` to `after`. A step, unlike `withEnvelope`, so undo and redo take it with the edit it
|
|
126
|
+
* belongs to. It leaves the tree as it is; history refuses an undo whose `before` no longer holds.
|
|
127
|
+
*/
|
|
128
|
+
export class EnvelopeStep<K extends EnvelopeKey = EnvelopeKey> extends Step {
|
|
129
|
+
constructor(
|
|
130
|
+
readonly key: K,
|
|
131
|
+
readonly before: WordEnvelope[K],
|
|
132
|
+
readonly after: WordEnvelope[K],
|
|
133
|
+
) {
|
|
134
|
+
super();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
apply(doc: Node): StepResult {
|
|
138
|
+
return StepResult.ok(doc);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
invert(): Step {
|
|
142
|
+
return new EnvelopeStep(this.key, this.after, this.before);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
map(): Step {
|
|
146
|
+
return this;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Changes story `id` from `before` to `after` (null: no story), a step of the body's transaction
|
|
152
|
+
* (ADR-0007): its inverse needs no document, it moves no position of the body. The selections are
|
|
153
|
+
* the story's around the change, so undo can give it back.
|
|
154
|
+
*/
|
|
155
|
+
export class StoryStep extends Step {
|
|
156
|
+
constructor(
|
|
157
|
+
readonly id: string,
|
|
158
|
+
readonly before: Story | null,
|
|
159
|
+
readonly after: Story | null,
|
|
160
|
+
readonly selectionBefore: SelectionJSON | null = null,
|
|
161
|
+
readonly selectionAfter: SelectionJSON | null = null,
|
|
162
|
+
) {
|
|
163
|
+
super();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
apply(doc: Node): StepResult {
|
|
167
|
+
return StepResult.ok(doc);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
invert(): Step {
|
|
171
|
+
return new StoryStep(
|
|
172
|
+
this.id,
|
|
173
|
+
this.after,
|
|
174
|
+
this.before,
|
|
175
|
+
this.selectionAfter,
|
|
176
|
+
this.selectionBefore,
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
map(): Step {
|
|
181
|
+
return this;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** This step followed by `next`, when `next` starts from what this one leaves. */
|
|
185
|
+
merge(next: StoryStep): StoryStep | null {
|
|
186
|
+
if (next.id !== this.id || !sameStory(this.after, next.before)) return null;
|
|
187
|
+
return new StoryStep(
|
|
188
|
+
this.id,
|
|
189
|
+
this.before,
|
|
190
|
+
next.after,
|
|
191
|
+
this.selectionBefore,
|
|
192
|
+
next.selectionAfter,
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** Whether two stories are the same: identical trees are the usual case. */
|
|
198
|
+
export function sameStory(a: Story | null | undefined, b: Story | null | undefined): boolean {
|
|
199
|
+
if (a === b || (!a && !b)) return true;
|
|
200
|
+
return (
|
|
201
|
+
!!a &&
|
|
202
|
+
!!b &&
|
|
203
|
+
a.kind === b.kind &&
|
|
204
|
+
a.noteType === b.noteType &&
|
|
205
|
+
a.ext === b.ext &&
|
|
206
|
+
(a.doc === b.doc || a.doc.eq(b.doc))
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** A story's JSON in the envelope. */
|
|
211
|
+
function storyJSON({ doc, ...rest }: Story): StoryJSON {
|
|
212
|
+
return { ...rest, content: doc.toJSON() };
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** The value of `key` as `steps` leave it, from `value`. */
|
|
216
|
+
function afterEnvelopeSteps<K extends EnvelopeKey>(
|
|
217
|
+
key: K,
|
|
218
|
+
value: WordEnvelope[K],
|
|
219
|
+
steps: readonly Step[],
|
|
220
|
+
): WordEnvelope[K] {
|
|
221
|
+
let current = value;
|
|
222
|
+
for (const step of steps)
|
|
223
|
+
if (step instanceof EnvelopeStep && step.key === key) current = step.after as WordEnvelope[K];
|
|
224
|
+
return current;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** Makes `tr` set the envelope's `key` to `value`, from what it holds after `tr` so far. */
|
|
228
|
+
export function setEnvelopeValue<K extends EnvelopeKey>(
|
|
229
|
+
state: EditorState,
|
|
230
|
+
tr: Transaction,
|
|
231
|
+
key: K,
|
|
232
|
+
value: WordEnvelope[K],
|
|
233
|
+
): Transaction {
|
|
234
|
+
const envelope = wordContext(state)?.envelope;
|
|
235
|
+
if (!envelope) return tr;
|
|
236
|
+
return tr.step(new EnvelopeStep(key, afterEnvelopeSteps(key, envelope[key], tr.steps), value));
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Whether stored `steps` can apply to `context`: every `EnvelopeStep` and `StoryStep` finds what
|
|
241
|
+
* it expects, given the ones before it. A change made outside history makes them stale.
|
|
242
|
+
*/
|
|
243
|
+
export function storedStepsApply(context: WordContext, steps: readonly Step[]): boolean {
|
|
244
|
+
const values = new Map<EnvelopeKey, unknown>();
|
|
245
|
+
const stories = new Map<string, Story | null>();
|
|
246
|
+
for (const step of steps) {
|
|
247
|
+
if (step instanceof EnvelopeStep) {
|
|
248
|
+
const key = step.key as EnvelopeKey;
|
|
249
|
+
const current = values.has(key) ? values.get(key) : context.envelope[key];
|
|
250
|
+
if (!deepEqual(current, step.before)) return false;
|
|
251
|
+
values.set(key, step.after);
|
|
252
|
+
} else if (step instanceof StoryStep) {
|
|
253
|
+
const current = stories.has(step.id)
|
|
254
|
+
? stories.get(step.id)
|
|
255
|
+
: (context.stories.get(step.id) ?? null);
|
|
256
|
+
if (!sameStory(current, step.before)) return false;
|
|
257
|
+
stories.set(step.id, step.after);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return true;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** The story as `tr` leaves it (`state`'s, after the story steps `tr` already has). */
|
|
264
|
+
export function storyAfter(state: EditorState, tr: Transaction, id: string): Story | null {
|
|
265
|
+
let story = wordContext(state)?.stories.get(id) ?? null;
|
|
266
|
+
for (const step of tr.steps) if (step instanceof StoryStep && step.id === id) story = step.after;
|
|
267
|
+
return story;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** What a story's editor knows of the body's: undo, and the trees it allocates ids across. */
|
|
271
|
+
export interface StoryBody {
|
|
272
|
+
readonly state: EditorState;
|
|
273
|
+
dispatch(tr: Transaction): void;
|
|
274
|
+
run(command: Command): boolean;
|
|
275
|
+
can(command: Command): boolean;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** On the state of a story's editor: the body it belongs to. */
|
|
279
|
+
export const storyBodyKey = new PluginKey<StoryBody>("storyBody");
|
|
280
|
+
|
|
281
|
+
/** The body of the story `state` edits; null when `state` is the body's. */
|
|
282
|
+
export const storyBody = (state: EditorState): StoryBody | null =>
|
|
283
|
+
storyBodyKey.getState(state) ?? null;
|
|
284
|
+
|
|
285
|
+
/** A state over a story's `doc` for working on it, with `state`'s word context (and `envelope`). */
|
|
286
|
+
export function storyState(state: EditorState, doc: Node, envelope?: WordEnvelope): EditorState {
|
|
287
|
+
const context = wordContext(state) as WordContext;
|
|
288
|
+
const given = envelope ? { ...context, envelope } : context;
|
|
289
|
+
return EditorState.create({ doc, plugins: [wordPlugin(given.envelope, given)] });
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/** Every tree of the document but `state`'s own: ids are allocated across them (ADR-0007). */
|
|
293
|
+
export function otherTrees(state: EditorState): Node[] {
|
|
294
|
+
const trees = [...(wordContext(state)?.stories.values() ?? [])].map((story) => story.doc);
|
|
295
|
+
const body = storyBody(state)?.state.doc;
|
|
296
|
+
return (body ? [body, ...trees] : trees).filter((tree) => tree !== state.doc);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** The trees of `envelope`'s stories. */
|
|
300
|
+
function storyTrees(envelope: WordEnvelope): Map<string, Story> {
|
|
301
|
+
return new Map(
|
|
302
|
+
Object.entries(envelope.stories ?? {}).map(([id, { content, ...rest }]) => [
|
|
303
|
+
id,
|
|
304
|
+
{ ...rest, doc: parseContent(content, `stories[${JSON.stringify(id)}].content`) },
|
|
305
|
+
]),
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
|
|
111
309
|
/**
|
|
112
310
|
* The plugin that gives commands a v3 document's envelope. A transaction changes it with
|
|
113
311
|
* `withEnvelope` (a new list's numbering definition); undo leaves such additions, as Word does.
|
|
114
|
-
* Comments change only through `CommentStep`s,
|
|
312
|
+
* Comments change only through `CommentStep`s, the last section, the settings and the controls'
|
|
313
|
+
* properties through `EnvelopeStep`s and stories through `StoryStep`s, which undo takes back (a
|
|
314
|
+
* paste adds the properties of the controls it copies with `withEnvelope`). A story's editor is
|
|
315
|
+
* given its body's `context` as it stands.
|
|
115
316
|
*/
|
|
116
|
-
export function wordPlugin(envelope: WordEnvelope): Plugin<WordContext> {
|
|
317
|
+
export function wordPlugin(envelope: WordEnvelope, context?: WordContext): Plugin<WordContext> {
|
|
117
318
|
// Not a secret: only tells documents apart.
|
|
118
319
|
const id = `${Date.now().toString(36)}${Math.random().toString(36).slice(2)}`;
|
|
119
|
-
const
|
|
320
|
+
const initial: WordContext = context ?? {
|
|
321
|
+
envelope,
|
|
322
|
+
resolver: new Resolver(envelope),
|
|
323
|
+
stories: storyTrees(envelope),
|
|
324
|
+
id,
|
|
325
|
+
};
|
|
120
326
|
return new Plugin({
|
|
121
327
|
key,
|
|
122
328
|
state: {
|
|
123
|
-
init: () =>
|
|
329
|
+
init: () => initial,
|
|
124
330
|
apply: (tr, value) => {
|
|
331
|
+
const adopted = tr.getMeta(ADOPT) as WordContext | undefined;
|
|
332
|
+
if (adopted) return adopted;
|
|
125
333
|
const next = tr.getMeta(key) as WordEnvelope | undefined;
|
|
126
334
|
const comments = applyCommentSteps(value.envelope.comments, tr.steps);
|
|
127
|
-
|
|
128
|
-
const
|
|
129
|
-
|
|
335
|
+
const base = next ?? value.envelope;
|
|
336
|
+
const section = afterEnvelopeSteps("section", base.section, tr.steps);
|
|
337
|
+
const settings = afterEnvelopeSteps("settings", value.envelope.settings, tr.steps);
|
|
338
|
+
const controls = afterEnvelopeSteps("controls", base.controls, tr.steps);
|
|
339
|
+
let { stories } = value;
|
|
340
|
+
let storiesJSON = value.envelope.stories;
|
|
341
|
+
for (const step of tr.steps) {
|
|
342
|
+
if (!(step instanceof StoryStep)) continue;
|
|
343
|
+
const trees = new Map(stories);
|
|
344
|
+
const json = { ...storiesJSON };
|
|
345
|
+
if (step.after) {
|
|
346
|
+
trees.set(step.id, step.after);
|
|
347
|
+
json[step.id] = storyJSON(step.after);
|
|
348
|
+
} else {
|
|
349
|
+
trees.delete(step.id);
|
|
350
|
+
delete json[step.id];
|
|
351
|
+
}
|
|
352
|
+
stories = trees;
|
|
353
|
+
storiesJSON = json;
|
|
354
|
+
}
|
|
355
|
+
if (
|
|
356
|
+
!next &&
|
|
357
|
+
comments === value.envelope.comments &&
|
|
358
|
+
section === base.section &&
|
|
359
|
+
settings === value.envelope.settings &&
|
|
360
|
+
controls === value.envelope.controls &&
|
|
361
|
+
stories === value.stories
|
|
362
|
+
)
|
|
363
|
+
return value;
|
|
364
|
+
const { stories: _stories, settings: _settings, controls: _controls, ...rest } = base;
|
|
365
|
+
const envelope: WordEnvelope = {
|
|
366
|
+
...rest,
|
|
367
|
+
section,
|
|
368
|
+
comments: comments as CommentJSON[],
|
|
369
|
+
...(storiesJSON && Object.keys(storiesJSON).length ? { stories: storiesJSON } : {}),
|
|
370
|
+
...(settings ? { settings } : {}),
|
|
371
|
+
...(controls ? { controls } : {}),
|
|
372
|
+
};
|
|
373
|
+
// Only comments, stories or steps changed: they do not affect formatting.
|
|
130
374
|
const resolver = next ? new Resolver(envelope) : value.resolver;
|
|
131
|
-
return { envelope, resolver, id: value.id };
|
|
375
|
+
return { envelope, resolver, stories, id: value.id };
|
|
132
376
|
},
|
|
133
377
|
},
|
|
134
378
|
});
|
|
@@ -142,6 +386,16 @@ export function withEnvelope(tr: Transaction, envelope: WordEnvelope): Transacti
|
|
|
142
386
|
return tr.setMeta(key, envelope);
|
|
143
387
|
}
|
|
144
388
|
|
|
389
|
+
const ADOPT = "adoptWordContext";
|
|
390
|
+
|
|
391
|
+
/** Makes `tr` give a story's state the body's context as it now is. */
|
|
392
|
+
export const withContext = (tr: Transaction, context: WordContext): Transaction =>
|
|
393
|
+
tr.setMeta(ADOPT, context);
|
|
394
|
+
|
|
395
|
+
/** The envelope `tr` gives the document through `withEnvelope`, if any. */
|
|
396
|
+
export const envelopeChange = (tr: Transaction): WordEnvelope | undefined =>
|
|
397
|
+
tr.getMeta(key) as WordEnvelope | undefined;
|
|
398
|
+
|
|
145
399
|
/** The envelope and resolver of the v3 document in `state`, or null for a v2 editor. */
|
|
146
400
|
export function wordContext(state: EditorState): WordContext | null {
|
|
147
401
|
return key.getState(state) ?? null;
|
|
@@ -181,7 +435,8 @@ export function splitAttrs(
|
|
|
181
435
|
const next = style?.next;
|
|
182
436
|
if (next === undefined || next === id) return { before, after: attrs };
|
|
183
437
|
const mark = Object.fromEntries(Object.keys(MARK_ONLY).map((k) => [k, attrs[k] ?? null]));
|
|
184
|
-
|
|
438
|
+
// The new paragraph is in the same content controls.
|
|
439
|
+
return { before, after: { ...mark, controls: attrs.controls, style: next } };
|
|
185
440
|
}
|
|
186
441
|
|
|
187
442
|
/** Run toggles: whether text shows the property, from its resolved formatting. */
|
|
@@ -252,7 +507,7 @@ export function wordToggleActive(
|
|
|
252
507
|
): boolean {
|
|
253
508
|
const { selection } = state;
|
|
254
509
|
if (caretMarks) return shows(context, selection.$from.parent, caretMarks, type);
|
|
255
|
-
const
|
|
510
|
+
const judged = selection.ranges.flatMap((r) => inlineIn(state, r.from, r.to, type).judged);
|
|
256
511
|
return judged.length > 0 && judged.every((f) => shows(context, f.parent, f.node.marks, type));
|
|
257
512
|
}
|
|
258
513
|
|
|
@@ -303,6 +558,14 @@ const MARKERS = new Set([
|
|
|
303
558
|
"bookmarkEnd",
|
|
304
559
|
"commentStart",
|
|
305
560
|
"commentEnd",
|
|
561
|
+
"controlStart",
|
|
562
|
+
"controlEnd",
|
|
563
|
+
// A field's parts and a note's own number: deleting one alone would break what it belongs to.
|
|
564
|
+
"fieldStart",
|
|
565
|
+
"fieldCode",
|
|
566
|
+
"fieldSeparator",
|
|
567
|
+
"fieldEnd",
|
|
568
|
+
"noteMark",
|
|
306
569
|
// A comment's reference mark: deleting it would take the comment's anchor.
|
|
307
570
|
"commentReference",
|
|
308
571
|
]);
|