@folijs/core 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/dist/model/schema.d.ts +9 -1
  3. package/dist/model/schema.d.ts.map +1 -1
  4. package/dist/model/schema.js +13 -3
  5. package/dist/model/schema.js.map +1 -1
  6. package/dist/state/commands.d.ts +8 -1
  7. package/dist/state/commands.d.ts.map +1 -1
  8. package/dist/state/commands.js +119 -38
  9. package/dist/state/commands.js.map +1 -1
  10. package/dist/state/comments.d.ts +47 -0
  11. package/dist/state/comments.d.ts.map +1 -0
  12. package/dist/state/comments.js +304 -0
  13. package/dist/state/comments.js.map +1 -0
  14. package/dist/state/editor.d.ts +42 -1
  15. package/dist/state/editor.d.ts.map +1 -1
  16. package/dist/state/editor.js +32 -2
  17. package/dist/state/editor.js.map +1 -1
  18. package/dist/state/history.d.ts +1 -1
  19. package/dist/state/history.d.ts.map +1 -1
  20. package/dist/state/history.js +3 -2
  21. package/dist/state/history.js.map +1 -1
  22. package/dist/state/index.d.ts +8 -2
  23. package/dist/state/index.d.ts.map +1 -1
  24. package/dist/state/index.js +8 -2
  25. package/dist/state/index.js.map +1 -1
  26. package/dist/state/links.d.ts.map +1 -1
  27. package/dist/state/links.js +30 -19
  28. package/dist/state/links.js.map +1 -1
  29. package/dist/state/mark.d.ts +5 -2
  30. package/dist/state/mark.d.ts.map +1 -1
  31. package/dist/state/mark.js +20 -3
  32. package/dist/state/mark.js.map +1 -1
  33. package/dist/state/pictures.d.ts +51 -0
  34. package/dist/state/pictures.d.ts.map +1 -0
  35. package/dist/state/pictures.js +140 -0
  36. package/dist/state/pictures.js.map +1 -0
  37. package/dist/state/review.d.ts +43 -0
  38. package/dist/state/review.d.ts.map +1 -0
  39. package/dist/state/review.js +361 -0
  40. package/dist/state/review.js.map +1 -0
  41. package/dist/state/selection.d.ts +12 -1
  42. package/dist/state/selection.d.ts.map +1 -1
  43. package/dist/state/selection.js +12 -0
  44. package/dist/state/selection.js.map +1 -1
  45. package/dist/state/state.d.ts +2 -0
  46. package/dist/state/state.d.ts.map +1 -1
  47. package/dist/state/state.js +8 -1
  48. package/dist/state/state.js.map +1 -1
  49. package/dist/state/tablecommands.d.ts +77 -0
  50. package/dist/state/tablecommands.d.ts.map +1 -0
  51. package/dist/state/tablecommands.js +675 -0
  52. package/dist/state/tablecommands.js.map +1 -0
  53. package/dist/state/tables.d.ts +110 -0
  54. package/dist/state/tables.d.ts.map +1 -0
  55. package/dist/state/tables.js +729 -0
  56. package/dist/state/tables.js.map +1 -0
  57. package/dist/state/track.d.ts +47 -0
  58. package/dist/state/track.d.ts.map +1 -0
  59. package/dist/state/track.js +583 -0
  60. package/dist/state/track.js.map +1 -0
  61. package/dist/state/word.d.ts +30 -2
  62. package/dist/state/word.d.ts.map +1 -1
  63. package/dist/state/word.js +83 -3
  64. package/dist/state/word.js.map +1 -1
  65. package/dist/state/wordclip.d.ts.map +1 -1
  66. package/dist/state/wordclip.js +26 -9
  67. package/dist/state/wordclip.js.map +1 -1
  68. package/dist/state/wordformat.d.ts +9 -1
  69. package/dist/state/wordformat.d.ts.map +1 -1
  70. package/dist/state/wordformat.js +38 -16
  71. package/dist/state/wordformat.js.map +1 -1
  72. package/dist/v3/document.d.ts +114 -7
  73. package/dist/v3/document.d.ts.map +1 -1
  74. package/dist/v3/document.js +170 -13
  75. package/dist/v3/document.js.map +1 -1
  76. package/dist/v3/index.d.ts +2 -2
  77. package/dist/v3/index.d.ts.map +1 -1
  78. package/dist/v3/index.js +2 -2
  79. package/dist/v3/index.js.map +1 -1
  80. package/dist/v3/migrate.d.ts +2 -2
  81. package/dist/v3/migrate.d.ts.map +1 -1
  82. package/dist/v3/resolve.d.ts +50 -4
  83. package/dist/v3/resolve.d.ts.map +1 -1
  84. package/dist/v3/resolve.js +205 -15
  85. package/dist/v3/resolve.js.map +1 -1
  86. package/dist/v3/schema.d.ts +6 -0
  87. package/dist/v3/schema.d.ts.map +1 -1
  88. package/dist/v3/schema.js +168 -6
  89. package/dist/v3/schema.js.map +1 -1
  90. package/package.json +1 -1
  91. package/src/model/schema.ts +18 -2
  92. package/src/state/commands.ts +110 -32
  93. package/src/state/comments.ts +342 -0
  94. package/src/state/editor.ts +97 -1
  95. package/src/state/history.ts +3 -2
  96. package/src/state/index.ts +74 -1
  97. package/src/state/links.ts +27 -17
  98. package/src/state/mark.ts +20 -3
  99. package/src/state/pictures.ts +168 -0
  100. package/src/state/review.ts +412 -0
  101. package/src/state/selection.ts +15 -1
  102. package/src/state/state.ts +8 -1
  103. package/src/state/tablecommands.ts +741 -0
  104. package/src/state/tables.ts +760 -0
  105. package/src/state/track.ts +659 -0
  106. package/src/state/word.ts +89 -4
  107. package/src/state/wordclip.ts +28 -11
  108. package/src/state/wordformat.ts +35 -13
  109. package/src/v3/document.ts +285 -14
  110. package/src/v3/index.ts +14 -0
  111. package/src/v3/migrate.ts +2 -2
  112. package/src/v3/resolve.ts +259 -15
  113. package/src/v3/schema.ts +190 -7
@@ -0,0 +1,342 @@
1
+ /**
2
+ * Comments (9.6), as Word keeps them: a comment's range in the text (`commentStart`,
3
+ * `commentEnd`) and its reference mark right after the range (`commentReference`), its content
4
+ * and thread in the envelope (`comments`: replies point at their comment with `parent`,
5
+ * resolved ones are `done`). Comment changes are `CommentStep`s, so undo takes them back; they
6
+ * are not tracked changes. A reply has its own anchors, next to its comment's, as Word writes
7
+ * them. A comment whose reference mark goes (its range deleted with it) goes too, with its
8
+ * replies.
9
+ */
10
+ import type { Node, NodeJSON } from "../model/index.js";
11
+ import type { CommentJSON } from "../v3/document.js";
12
+ import { closeHistory } from "./history.js";
13
+ import { AllSelection, Selection, TextSelection } from "./selection.js";
14
+ import { type Command, type EditorState, Plugin, PluginKey, type Transaction } from "./state.js";
15
+ import { now, trackState, untracked } from "./track.js";
16
+ import { setComments, wordContext } from "./word.js";
17
+
18
+ const ANCHORS = new Set(["commentStart", "commentEnd", "commentReference"]);
19
+
20
+ export interface CommentOptions {
21
+ /** Default: the editor's author (`createEditor({ author })`). */
22
+ author?: string;
23
+ /** Word shows them on the comment (at most 9 characters). */
24
+ initials?: string;
25
+ }
26
+
27
+ /** A comment's content: paragraphs of plain text (one per line), or schema v3 blocks. */
28
+ export type CommentContent = string | readonly NodeJSON[];
29
+
30
+ function contentOf(content: CommentContent): NodeJSON[] {
31
+ if (typeof content !== "string") return structuredClone([...content]);
32
+ return content
33
+ .split(/\r\n|\r|\n/)
34
+ .map((line) =>
35
+ line ? { type: "paragraph", content: [{ type: "text", text: line }] } : { type: "paragraph" },
36
+ );
37
+ }
38
+
39
+ /** Word's limit on a comment's initials. */
40
+ const INITIALS = 9;
41
+ const initialsOf = (options: CommentOptions) =>
42
+ options.initials !== undefined ? { initials: options.initials.slice(0, INITIALS) } : {};
43
+
44
+ /** Comment anchors inside content Foli keeps verbatim (an opaque block): their ids. */
45
+ function opaqueCommentIds(doc: Node): Set<number> {
46
+ const ids = new Set<number>();
47
+ doc.descendants((node) => {
48
+ if (node.type.name === "opaqueBlock" || node.type.name === "opaqueInline")
49
+ for (const [, id] of String(node.attrs.xml).matchAll(
50
+ /<\w+:comment(?:RangeStart|RangeEnd|Reference)\b[^>]*?\b\w+:id="(\d+)"/g,
51
+ ))
52
+ ids.add(Number(id));
53
+ return node.isBlock;
54
+ });
55
+ return ids;
56
+ }
57
+
58
+ /** The next free comment id: above every comment and every comment anchor, verbatim ones too. */
59
+ function nextCommentId(doc: Node, comments: readonly CommentJSON[]): number {
60
+ let max = -1;
61
+ for (const c of comments) max = Math.max(max, c.id);
62
+ doc.descendants((node) => {
63
+ if (ANCHORS.has(node.type.name)) max = Math.max(max, node.attrs.id as number);
64
+ return node.isBlock;
65
+ });
66
+ for (const id of opaqueCommentIds(doc)) max = Math.max(max, id);
67
+ return max + 1;
68
+ }
69
+
70
+ /** The comment a thread starts with (`id`'s, following `parent`). */
71
+ function rootOf(comments: readonly CommentJSON[], id: number): CommentJSON | undefined {
72
+ let root = comments.find((c) => c.id === id);
73
+ const seen = new Set<number>();
74
+ while (root?.parent !== undefined && !seen.has(root.id)) {
75
+ seen.add(root.id);
76
+ const up = root.parent;
77
+ root = comments.find((c) => c.id === up);
78
+ }
79
+ return root;
80
+ }
81
+
82
+ /** Where each comment's anchors are: start, end (after which the reference goes) and reference. */
83
+ export function commentAnchors(
84
+ doc: Node,
85
+ ): Map<number, { start?: number; end?: number; reference?: number }> {
86
+ const anchors = new Map<number, { start?: number; end?: number; reference?: number }>();
87
+ doc.descendants((node, pos) => {
88
+ if (!ANCHORS.has(node.type.name)) return node.isBlock;
89
+ const id = node.attrs.id as number;
90
+ const entry = anchors.get(id) ?? {};
91
+ if (node.type.name === "commentStart") entry.start ??= pos;
92
+ else if (node.type.name === "commentEnd") entry.end ??= pos;
93
+ else entry.reference ??= pos;
94
+ anchors.set(id, entry);
95
+ return false;
96
+ });
97
+ return anchors;
98
+ }
99
+
100
+ /** A transaction for comment edits: not tracked, an undo step of its own. */
101
+ const commentTr = (state: EditorState) =>
102
+ state.tr.setMeta(untracked, true).setMeta(closeHistory, true);
103
+
104
+ const authorOf = (state: EditorState, options: CommentOptions) =>
105
+ options.author ?? trackState(state)?.author ?? "Author";
106
+
107
+ /**
108
+ * Comments the selection (at a caret, the point there): its range around the selection, the
109
+ * reference mark after it, the comment in the envelope. Schema v3 only.
110
+ */
111
+ export function addComment(content: CommentContent, options: CommentOptions = {}): Command {
112
+ return (state, dispatch) => {
113
+ const context = wordContext(state);
114
+ const { schema } = state;
115
+ const types = {
116
+ start: schema.nodes.commentStart,
117
+ end: schema.nodes.commentEnd,
118
+ reference: schema.nodes.commentReference,
119
+ };
120
+ if (!context || !types.start || !types.end || !types.reference) return false;
121
+ // Select-all comments the whole text: from the first textblock to the last.
122
+ const all = state.selection instanceof AllSelection;
123
+ const $from = all ? Selection.atStart(state.doc).$from : state.selection.$from;
124
+ const $to = all ? Selection.atEnd(state.doc).$to : state.selection.$to;
125
+ if (!$from.parent.inlineContent || !$to.parent.inlineContent) return false;
126
+ if (dispatch) {
127
+ const comments = context.envelope.comments;
128
+ const id = nextCommentId(state.doc, comments);
129
+ const tr = commentTr(state);
130
+ // From the end, so the start's position stays.
131
+ tr.insert($to.pos, [types.end.create({ id }), types.reference.create({ id })]);
132
+ tr.insert($from.pos, types.start.create({ id }));
133
+ // The selection stays on the commented text, not on the anchors after it: typing over
134
+ // it must not delete the comment's reference.
135
+ tr.setSelection(TextSelection.create(tr.doc, $from.pos + 1, $to.pos + 1));
136
+ const comment: CommentJSON = {
137
+ id,
138
+ author: authorOf(state, options),
139
+ ...initialsOf(options),
140
+ date: now(),
141
+ content: contentOf(content),
142
+ };
143
+ setComments(tr, comments, [...comments, comment]);
144
+ dispatch(tr);
145
+ }
146
+ return true;
147
+ };
148
+ }
149
+
150
+ /** Replies to comment `parent` (a reply to a reply answers its thread's comment, as Word). */
151
+ export function replyToComment(
152
+ parent: number,
153
+ content: CommentContent,
154
+ options: CommentOptions = {},
155
+ ): Command {
156
+ return (state, dispatch) => {
157
+ const context = wordContext(state);
158
+ const { schema } = state;
159
+ const comments = context?.envelope.comments ?? [];
160
+ const root = rootOf(comments, parent);
161
+ const all = commentAnchors(state.doc);
162
+ const anchors = root && all.get(root.id);
163
+ const { commentStart, commentEnd, commentReference } = schema.nodes;
164
+ if (!context || !root || !anchors || !commentStart || !commentEnd || !commentReference)
165
+ return false;
166
+ if (dispatch) {
167
+ const id = nextCommentId(state.doc, comments);
168
+ const tr = commentTr(state);
169
+ // As Word writes a thread: the starts together (this one after the thread's last), then
170
+ // each end with its reference (this pair after the thread's last reference).
171
+ const ids = thread(comments, root.id);
172
+ const last = (key: "start" | "reference") =>
173
+ Math.max(...[...ids].map((i) => all.get(i)?.[key] ?? -1));
174
+ const lastStart = last("start");
175
+ const lastReference = last("reference");
176
+ if (lastReference >= 0)
177
+ tr.insert(lastReference + 1, [commentEnd.create({ id }), commentReference.create({ id })]);
178
+ if (lastStart >= 0) tr.insert(lastStart + 1, commentStart.create({ id }));
179
+ const reply: CommentJSON = {
180
+ id,
181
+ author: authorOf(state, options),
182
+ ...initialsOf(options),
183
+ date: now(),
184
+ content: contentOf(content),
185
+ parent: root.id,
186
+ };
187
+ setComments(tr, comments, [...comments, reply]);
188
+ dispatch(tr);
189
+ }
190
+ return true;
191
+ };
192
+ }
193
+
194
+ /** Changes the comments with `change` applied to comment `id`; false when there is none. */
195
+ function changeComment(
196
+ id: number,
197
+ change: (comment: CommentJSON, comments: readonly CommentJSON[]) => CommentJSON[],
198
+ ): Command {
199
+ return (state, dispatch) => {
200
+ const comments = wordContext(state)?.envelope.comments;
201
+ const comment = comments?.find((c) => c.id === id);
202
+ if (!comments || !comment) return false;
203
+ if (dispatch) {
204
+ const next = comments.flatMap((c) => (c === comment ? change(c, comments) : [c]));
205
+ dispatch(setComments(commentTr(state), comments, next));
206
+ }
207
+ return true;
208
+ };
209
+ }
210
+
211
+ /** Replaces comment `id`'s content. */
212
+ export const editComment = (id: number, content: CommentContent): Command =>
213
+ changeComment(id, (c) => [{ ...c, content: contentOf(content) }]);
214
+
215
+ /** Resolves (or reopens) comment `id`'s thread: every comment of it, as Word marks them. */
216
+ export function resolveComment(id: number, done = true): Command {
217
+ return (state, dispatch) => {
218
+ const comments = wordContext(state)?.envelope.comments ?? [];
219
+ const root = rootOf(comments, id);
220
+ if (!root) return false;
221
+ if (dispatch) {
222
+ const ids = thread(comments, root.id);
223
+ const next = comments.map((c) => {
224
+ if (!ids.has(c.id)) return c;
225
+ const { done: _, ...rest } = c;
226
+ return done ? { ...rest, done: true } : rest;
227
+ });
228
+ dispatch(setComments(commentTr(state), comments, next));
229
+ }
230
+ return true;
231
+ };
232
+ }
233
+
234
+ /** The ids of `id` and every reply under it. */
235
+ function thread(comments: readonly CommentJSON[], id: number): Set<number> {
236
+ const ids = new Set([id]);
237
+ for (let grew = true; grew; ) {
238
+ grew = false;
239
+ for (const c of comments)
240
+ if (c.parent !== undefined && ids.has(c.parent) && !ids.has(c.id)) {
241
+ ids.add(c.id);
242
+ grew = true;
243
+ }
244
+ }
245
+ return ids;
246
+ }
247
+
248
+ /** Removes the comments `ids` and their anchors. */
249
+ function removeComments(tr: Transaction, comments: readonly CommentJSON[], ids: Set<number>): void {
250
+ const positions: number[] = [];
251
+ tr.doc.descendants((node, pos) => {
252
+ if (ANCHORS.has(node.type.name) && ids.has(node.attrs.id as number)) positions.push(pos);
253
+ return node.isBlock;
254
+ });
255
+ for (const pos of positions.reverse()) tr.delete(pos, pos + 1);
256
+ setComments(
257
+ tr,
258
+ comments,
259
+ comments.filter((c) => !ids.has(c.id)),
260
+ );
261
+ }
262
+
263
+ /** Deletes comment `id` with its replies and their anchors. */
264
+ export function deleteComment(id: number): Command {
265
+ return (state, dispatch) => {
266
+ const comments = wordContext(state)?.envelope.comments ?? [];
267
+ const ids = thread(comments, id);
268
+ // shortcut: a comment anchored in content Foli keeps verbatim (an opaque block) stays, its
269
+ // anchors there cannot be removed; model that content to remove them.
270
+ const verbatim = opaqueCommentIds(state.doc);
271
+ if (!comments.some((c) => c.id === id) || [...ids].some((i) => verbatim.has(i))) return false;
272
+ if (dispatch) {
273
+ const tr = commentTr(state);
274
+ removeComments(tr, comments, ids);
275
+ dispatch(tr);
276
+ }
277
+ return true;
278
+ };
279
+ }
280
+
281
+ /** Deletes every comment and its anchors (not the threads anchored in verbatim content). */
282
+ export const deleteAllComments: Command = (state, dispatch) => {
283
+ const comments = wordContext(state)?.envelope.comments ?? [];
284
+ const verbatim = opaqueCommentIds(state.doc);
285
+ const kept = new Set<number>();
286
+ for (const c of comments) {
287
+ const root = rootOf(comments, c.id);
288
+ if (root && verbatim.has(root.id)) for (const i of thread(comments, root.id)) kept.add(i);
289
+ if (verbatim.has(c.id)) kept.add(c.id);
290
+ }
291
+ const gone = new Set(comments.map((c) => c.id).filter((i) => !kept.has(i)));
292
+ if (!gone.size) return false;
293
+ if (dispatch) {
294
+ const tr = commentTr(state);
295
+ removeComments(tr, comments, gone);
296
+ dispatch(tr);
297
+ }
298
+ return true;
299
+ };
300
+
301
+ const key = new PluginKey("comments");
302
+
303
+ /** Whether one of `tr`'s steps replaced content holding a comment's reference mark. */
304
+ function removesReference(tr: Transaction): boolean {
305
+ return tr.steps.some((step, i) => {
306
+ let found = false;
307
+ step.getMap().forEach((from, to) => {
308
+ if (from < to)
309
+ (tr.docs[i] as Node).nodesBetween(from, to, (node) => {
310
+ if (node.type.name === "commentReference") found = true;
311
+ return !found;
312
+ });
313
+ });
314
+ return found;
315
+ });
316
+ }
317
+
318
+ /**
319
+ * Keeps comments with their text: a comment whose reference mark an edit removed (its range
320
+ * deleted) is removed with its replies and the rest of their anchors, in the same undo step.
321
+ * Only references the edit removed count: a comment without one from the start stays.
322
+ */
323
+ export function commentsPlugin(): Plugin {
324
+ return new Plugin({
325
+ key,
326
+ appendTransaction(transactions, oldState, newState) {
327
+ const comments = wordContext(newState)?.envelope.comments;
328
+ if (!comments?.length || !transactions.some(removesReference)) return null;
329
+ const before = commentAnchors(oldState.doc);
330
+ const after = commentAnchors(newState.doc);
331
+ const gone = new Set<number>();
332
+ for (const [id, anchors] of before)
333
+ if (anchors.reference !== undefined && after.get(id)?.reference === undefined)
334
+ for (const t of thread(comments, id)) gone.add(t);
335
+ const present = [...gone].filter((id) => comments.some((c) => c.id === id));
336
+ if (!present.length) return null;
337
+ const tr = newState.tr.setMeta(untracked, true);
338
+ removeComments(tr, comments, new Set(present));
339
+ return tr;
340
+ },
341
+ });
342
+ }
@@ -26,12 +26,41 @@ import {
26
26
  splitBlock,
27
27
  toggleMark,
28
28
  } from "./commands.js";
29
+ import {
30
+ addComment,
31
+ type CommentContent,
32
+ type CommentOptions,
33
+ commentsPlugin,
34
+ deleteAllComments,
35
+ deleteComment,
36
+ editComment,
37
+ replyToComment,
38
+ resolveComment,
39
+ } from "./comments.js";
29
40
  import { setAlign, setHighlight, setTextColor } from "./format.js";
30
41
  import { history, redo, undo } from "./history.js";
31
42
  import { removeLink, setLink } from "./links.js";
32
43
  import { liftListItem, listKeymap, sinkListItem, toggleList, wrapInList } from "./lists.js";
44
+ import {
45
+ acceptAllChanges,
46
+ acceptChange,
47
+ nextChange,
48
+ previousChange,
49
+ rejectAllChanges,
50
+ rejectChange,
51
+ } from "./review.js";
33
52
  import { TextSelection } from "./selection.js";
34
53
  import { type Command, EditorState, type Plugin, type Transaction } from "./state.js";
54
+ import { tableKeymap } from "./tables.js";
55
+ import {
56
+ type ReviewView,
57
+ setReviewView,
58
+ setTrackChanges,
59
+ type TrackState,
60
+ trackPlugin,
61
+ trackState,
62
+ trackTransaction,
63
+ } from "./track.js";
35
64
  import { type WordEnvelope, wordContext, wordPlugin } from "./word.js";
36
65
  import type { ListKind } from "./wordformat.js";
37
66
 
@@ -46,6 +75,15 @@ export interface EditorOptions {
46
75
  document?: EditorDocument;
47
76
  /** Run before the built-in plugins, so their keymaps take precedence. */
48
77
  plugins?: readonly Plugin[];
78
+ /** Who edits: tracked changes are made in this name. Default "Author". */
79
+ author?: string;
80
+ /**
81
+ * Record edits as tracked changes from the start (a `.docx`'s `w:trackRevisions`:
82
+ * `DocxImport.trackRevisions`). Schema v3 only. Default false.
83
+ */
84
+ trackChanges?: boolean;
85
+ /** How the editor's views show tracked changes at first (default `markup`). */
86
+ reviewView?: ReviewView;
49
87
  }
50
88
 
51
89
  export type EditorListener = (state: EditorState, transactions: readonly Transaction[]) => void;
@@ -103,7 +141,37 @@ export interface Editor {
103
141
  setTextColor(color: string | null): boolean;
104
142
  /** Highlight (`#rrggbb`) of the selected or next typed text; null removes it. */
105
143
  setHighlight(color: string | null): boolean;
144
+ /** Turns tracked changes on or off (`author` changes who makes them). */
145
+ setTrackChanges(on: boolean, author?: string): boolean;
146
+ /** How the editor's views show tracked changes. */
147
+ setReviewView(view: ReviewView): boolean;
148
+ /** Accepts the change at the caret, or the changes in the selection. */
149
+ acceptChange(): boolean;
150
+ /** Rejects the change at the caret, or the changes in the selection. */
151
+ rejectChange(): boolean;
152
+ /** Accepts every change, or every change of `author`. */
153
+ acceptAllChanges(author?: string): boolean;
154
+ /** Rejects every change, or every change of `author`. */
155
+ rejectAllChanges(author?: string): boolean;
156
+ /** Selects the next change after the selection. */
157
+ nextChange(): boolean;
158
+ /** Selects the previous change before the selection. */
159
+ previousChange(): boolean;
160
+ /** Comments the selection (at a caret, the point there). */
161
+ addComment(content: CommentContent, options?: CommentOptions): boolean;
162
+ /** Replies to a comment's thread. */
163
+ replyToComment(id: number, content: CommentContent, options?: CommentOptions): boolean;
164
+ /** Replaces a comment's content. */
165
+ editComment(id: number, content: CommentContent): boolean;
166
+ /** Resolves (`done` false: reopens) a comment's thread. */
167
+ resolveComment(id: number, done?: boolean): boolean;
168
+ /** Deletes a comment, its replies and their anchors. */
169
+ deleteComment(id: number): boolean;
170
+ /** Deletes every comment. */
171
+ deleteAllComments(): boolean;
106
172
  };
173
+ /** Whether edits are recorded as tracked changes. */
174
+ readonly trackingChanges: boolean;
107
175
  isMarkActive(type: string): boolean;
108
176
  /** The current document (schema v3). */
109
177
  getDocument(): FoliDocument;
@@ -147,12 +215,19 @@ function load(document: EditorDocument): Loaded {
147
215
  export function createEditor({
148
216
  document = createDocument(),
149
217
  plugins = [],
218
+ author = "Author",
219
+ trackChanges = false,
220
+ reviewView = "markup",
150
221
  }: EditorOptions = {}): Editor {
151
222
  let loaded = load(document);
223
+ let track: TrackState = { on: trackChanges, author, view: reviewView };
152
224
  const pluginsFor = ({ envelope }: Loaded) => [
153
225
  ...plugins,
154
226
  history(),
155
227
  wordPlugin(envelope),
228
+ trackPlugin(track),
229
+ commentsPlugin(),
230
+ tableKeymap(),
156
231
  listKeymap(),
157
232
  baseKeymap(),
158
233
  ];
@@ -163,7 +238,10 @@ export function createEditor({
163
238
  for (const listener of listeners) listener(state, transactions);
164
239
  };
165
240
  const dispatch = (tr: Transaction) => {
166
- const result = state.applyTransaction(tr);
241
+ // With tracking on, the edit is rebuilt as tracked changes; one that cannot be is not made.
242
+ const tracked = trackTransaction(state, tr);
243
+ if (!tracked) return;
244
+ const result = state.applyTransaction(tracked);
167
245
  state = result.state;
168
246
  if (result.transactions.length) notify(result.transactions);
169
247
  };
@@ -214,6 +292,23 @@ export function createEditor({
214
292
  setAlign: (align) => run(setAlign(align)),
215
293
  setTextColor: (color) => run(setTextColor(color)),
216
294
  setHighlight: (color) => run(setHighlight(color)),
295
+ setTrackChanges: (on, name) => run(setTrackChanges(on, name)),
296
+ setReviewView: (view) => run(setReviewView(view)),
297
+ acceptChange: () => run(acceptChange),
298
+ rejectChange: () => run(rejectChange),
299
+ acceptAllChanges: (name) => run(acceptAllChanges(name)),
300
+ rejectAllChanges: (name) => run(rejectAllChanges(name)),
301
+ nextChange: () => run(nextChange),
302
+ previousChange: () => run(previousChange),
303
+ addComment: (content, options) => run(addComment(content, options)),
304
+ replyToComment: (id, content, options) => run(replyToComment(id, content, options)),
305
+ editComment: (id, content) => run(editComment(id, content)),
306
+ resolveComment: (id, done) => run(resolveComment(id, done)),
307
+ deleteComment: (id) => run(deleteComment(id)),
308
+ deleteAllComments: () => run(deleteAllComments),
309
+ },
310
+ get trackingChanges() {
311
+ return trackState(state)?.on ?? false;
217
312
  },
218
313
  isMarkActive: (type) => isMarkActive(state, type),
219
314
  getDocument: () => ({
@@ -221,6 +316,7 @@ export function createEditor({
221
316
  content: state.doc.toJSON(),
222
317
  }),
223
318
  setDocument(next) {
319
+ track = trackState(state) ?? track;
224
320
  loaded = load(next);
225
321
  state = EditorState.create({ doc: loaded.doc, plugins: pluginsFor(loaded) });
226
322
  notify([]);
@@ -33,7 +33,7 @@ const historyKey = new PluginKey<HistoryState>("history");
33
33
  /** Meta set on undo/redo transactions: which operation produced them. */
34
34
  const historyOp = "historyOp";
35
35
 
36
- /** Set this meta on a transaction to stop it merging into the previous undo event. */
36
+ /** Set this meta on a transaction to make it an undo step of its own (no merging either side). */
37
37
  export const closeHistory = "closeHistory";
38
38
 
39
39
  const EMPTY: HistoryState = { done: [], undone: [], prevTime: 0, prevRanges: null };
@@ -99,7 +99,8 @@ export function history({ newGroupDelay = 500, depth = 100 }: HistoryOptions = {
99
99
  return {
100
100
  done: done.slice(-depth),
101
101
  undone: [],
102
- prevTime: root ? hist.prevTime : tr.time,
102
+ // A closed change is a step of its own on both sides: what follows does not join it.
103
+ prevTime: root ? hist.prevTime : tr.getMeta(closeHistory) ? 0 : tr.time,
103
104
  prevRanges: root ? mapRanges(hist.prevRanges, tr.mapping) : changedRanges(tr),
104
105
  };
105
106
  },
@@ -5,6 +5,7 @@ export {
5
5
  deleteBackward,
6
6
  deleteForward,
7
7
  deleteSelection,
8
+ deleteSelectionKeepingTables,
8
9
  insertHardBreak,
9
10
  insertPageBreak,
10
11
  insertTab,
@@ -17,6 +18,17 @@ export {
17
18
  splitBlock,
18
19
  toggleMark,
19
20
  } from "./commands.js";
21
+ export {
22
+ addComment,
23
+ type CommentContent,
24
+ type CommentOptions,
25
+ commentAnchors,
26
+ deleteAllComments,
27
+ deleteComment,
28
+ editComment,
29
+ replyToComment,
30
+ resolveComment,
31
+ } from "./comments.js";
20
32
  export {
21
33
  createEditor,
22
34
  type Editor,
@@ -63,6 +75,25 @@ export {
63
75
  wrapInList,
64
76
  } from "./lists.js";
65
77
  export { typedMarks } from "./mark.js";
78
+ export {
79
+ insertPicture,
80
+ type NewPicture,
81
+ selectedPicture,
82
+ setPictureAlt,
83
+ setPictureSize,
84
+ } from "./pictures.js";
85
+ export {
86
+ acceptAllChanges,
87
+ acceptChange,
88
+ type Change,
89
+ type ChangeFilter,
90
+ type ChangeKind,
91
+ nextChange,
92
+ previousChange,
93
+ rejectAllChanges,
94
+ rejectChange,
95
+ trackedChanges,
96
+ } from "./review.js";
66
97
  export {
67
98
  AllSelection,
68
99
  marksAt,
@@ -81,7 +112,49 @@ export {
81
112
  type PluginSpec,
82
113
  Transaction,
83
114
  } from "./state.js";
84
- export { type WordContext, type WordEnvelope, withEnvelope, wordContext } from "./word.js";
115
+ export {
116
+ deleteColumns,
117
+ deleteRows,
118
+ deleteTable,
119
+ insertColumn,
120
+ insertRow,
121
+ insertTable,
122
+ isHeaderRow,
123
+ mergeCells,
124
+ setCellShading,
125
+ setColumnWidth,
126
+ setTableStyle,
127
+ splitCell,
128
+ tableStyleAt,
129
+ tableStyles,
130
+ toggleHeaderRow,
131
+ } from "./tablecommands.js";
132
+ export {
133
+ CellSelection,
134
+ cellsAsTable,
135
+ extendCellSelection,
136
+ goToCell,
137
+ goToCellVertically,
138
+ keepTablesApart,
139
+ pasteCells,
140
+ } from "./tables.js";
141
+ export {
142
+ type ReviewView,
143
+ setReviewView,
144
+ setTrackChanges,
145
+ type TrackState,
146
+ trackState,
147
+ trackTransaction,
148
+ untracked,
149
+ } from "./track.js";
150
+ export {
151
+ CommentStep,
152
+ setComments,
153
+ type WordContext,
154
+ type WordEnvelope,
155
+ withEnvelope,
156
+ wordContext,
157
+ } from "./word.js";
85
158
  export {
86
159
  copiedDefinitions,
87
160
  fitPaste,