@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,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 (a table): 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 (a table) stays, its anchors
|
|
269
|
+
// there cannot be removed; Phase 10 models tables.
|
|
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
|
+
}
|
package/src/state/editor.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
+
import type { DocumentSettings, FoliDocumentV2 } from "../document.js";
|
|
2
|
+
import type { Attrs, Node } from "../model/index.js";
|
|
3
|
+
import type { Alignment } from "../schema.js";
|
|
1
4
|
import {
|
|
2
5
|
createDocument,
|
|
3
|
-
type DocumentSettings,
|
|
4
6
|
type FoliDocument,
|
|
5
7
|
parseContent,
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
import
|
|
8
|
+
parseDocument,
|
|
9
|
+
type SectionJSON,
|
|
10
|
+
} from "../v3/document.js";
|
|
11
|
+
import { twipsToMm as mm } from "../v3/units.js";
|
|
10
12
|
import {
|
|
11
13
|
baseKeymap,
|
|
12
14
|
type DeleteUnit,
|
|
@@ -24,18 +26,63 @@ import {
|
|
|
24
26
|
splitBlock,
|
|
25
27
|
toggleMark,
|
|
26
28
|
} from "./commands.js";
|
|
27
|
-
import {
|
|
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";
|
|
40
|
+
import { setAlign, setHighlight, setTextColor } from "./format.js";
|
|
28
41
|
import { history, redo, undo } from "./history.js";
|
|
29
42
|
import { removeLink, setLink } from "./links.js";
|
|
30
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";
|
|
31
52
|
import { TextSelection } from "./selection.js";
|
|
32
53
|
import { type Command, EditorState, type Plugin, type Transaction } from "./state.js";
|
|
54
|
+
import {
|
|
55
|
+
type ReviewView,
|
|
56
|
+
setReviewView,
|
|
57
|
+
setTrackChanges,
|
|
58
|
+
type TrackState,
|
|
59
|
+
trackPlugin,
|
|
60
|
+
trackState,
|
|
61
|
+
trackTransaction,
|
|
62
|
+
} from "./track.js";
|
|
63
|
+
import { type WordEnvelope, wordContext, wordPlugin } from "./word.js";
|
|
64
|
+
import type { ListKind } from "./wordformat.js";
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* A document an editor loads: schema v3 (Word's model), or one saved by 0.1.0 or 0.2.0, migrated
|
|
68
|
+
* to v3 when loaded (`parseDocument`).
|
|
69
|
+
*/
|
|
70
|
+
export type EditorDocument = FoliDocument | FoliDocumentV2;
|
|
33
71
|
|
|
34
72
|
export interface EditorOptions {
|
|
35
|
-
/** Trusted or not, it is validated (`
|
|
36
|
-
document?:
|
|
73
|
+
/** Trusted or not, it is validated (and migrated: `parseDocument`). Default: an empty one. */
|
|
74
|
+
document?: EditorDocument;
|
|
37
75
|
/** Run before the built-in plugins, so their keymaps take precedence. */
|
|
38
76
|
plugins?: readonly Plugin[];
|
|
77
|
+
/** Who edits: tracked changes are made in this name. Default "Author". */
|
|
78
|
+
author?: string;
|
|
79
|
+
/**
|
|
80
|
+
* Record edits as tracked changes from the start (a `.docx`'s `w:trackRevisions`:
|
|
81
|
+
* `DocxImport.trackRevisions`). Schema v3 only. Default false.
|
|
82
|
+
*/
|
|
83
|
+
trackChanges?: boolean;
|
|
84
|
+
/** How the editor's views show tracked changes at first (default `markup`). */
|
|
85
|
+
reviewView?: ReviewView;
|
|
39
86
|
}
|
|
40
87
|
|
|
41
88
|
export type EditorListener = (state: EditorState, transactions: readonly Transaction[]) => void;
|
|
@@ -43,7 +90,10 @@ export type EditorListener = (state: EditorState, transactions: readonly Transac
|
|
|
43
90
|
/** A headless editor: state, commands, history and change notifications, no DOM. */
|
|
44
91
|
export interface Editor {
|
|
45
92
|
readonly state: EditorState;
|
|
46
|
-
/**
|
|
93
|
+
/**
|
|
94
|
+
* Page size and margins of the current document. For a schema v3 document, its last
|
|
95
|
+
* section's (the view lays each section out with its own).
|
|
96
|
+
*/
|
|
47
97
|
readonly settings: DocumentSettings;
|
|
48
98
|
dispatch(tr: Transaction): void;
|
|
49
99
|
/** Runs a command; returns whether it applied. */
|
|
@@ -69,13 +119,13 @@ export interface Editor {
|
|
|
69
119
|
selectAll(): boolean;
|
|
70
120
|
undo(): boolean;
|
|
71
121
|
redo(): boolean;
|
|
72
|
-
/**
|
|
73
|
-
wrapInList(
|
|
74
|
-
/** The list buttons: removes the
|
|
75
|
-
toggleList(
|
|
76
|
-
/** Tab in a list:
|
|
122
|
+
/** Numbers the selected paragraphs as a list ("bulletList" or "orderedList"). */
|
|
123
|
+
wrapInList(kind: ListKind): boolean;
|
|
124
|
+
/** The list buttons: removes the selection's numbering when it is a list of `kind`, else numbers it. */
|
|
125
|
+
toggleList(kind: ListKind): boolean;
|
|
126
|
+
/** Tab in a list: one level deeper. */
|
|
77
127
|
indentListItem(): boolean;
|
|
78
|
-
/** Shift+Tab in a list:
|
|
128
|
+
/** Shift+Tab in a list: one level up (out of the list at the top). */
|
|
79
129
|
outdentListItem(): boolean;
|
|
80
130
|
/**
|
|
81
131
|
* Links the selection to `href` (normalized, then validated: unsafe hrefs do not apply); at
|
|
@@ -90,31 +140,106 @@ export interface Editor {
|
|
|
90
140
|
setTextColor(color: string | null): boolean;
|
|
91
141
|
/** Highlight (`#rrggbb`) of the selected or next typed text; null removes it. */
|
|
92
142
|
setHighlight(color: string | null): boolean;
|
|
93
|
-
/**
|
|
94
|
-
|
|
143
|
+
/** Turns tracked changes on or off (`author` changes who makes them). */
|
|
144
|
+
setTrackChanges(on: boolean, author?: string): boolean;
|
|
145
|
+
/** How the editor's views show tracked changes. */
|
|
146
|
+
setReviewView(view: ReviewView): boolean;
|
|
147
|
+
/** Accepts the change at the caret, or the changes in the selection. */
|
|
148
|
+
acceptChange(): boolean;
|
|
149
|
+
/** Rejects the change at the caret, or the changes in the selection. */
|
|
150
|
+
rejectChange(): boolean;
|
|
151
|
+
/** Accepts every change, or every change of `author`. */
|
|
152
|
+
acceptAllChanges(author?: string): boolean;
|
|
153
|
+
/** Rejects every change, or every change of `author`. */
|
|
154
|
+
rejectAllChanges(author?: string): boolean;
|
|
155
|
+
/** Selects the next change after the selection. */
|
|
156
|
+
nextChange(): boolean;
|
|
157
|
+
/** Selects the previous change before the selection. */
|
|
158
|
+
previousChange(): boolean;
|
|
159
|
+
/** Comments the selection (at a caret, the point there). */
|
|
160
|
+
addComment(content: CommentContent, options?: CommentOptions): boolean;
|
|
161
|
+
/** Replies to a comment's thread. */
|
|
162
|
+
replyToComment(id: number, content: CommentContent, options?: CommentOptions): boolean;
|
|
163
|
+
/** Replaces a comment's content. */
|
|
164
|
+
editComment(id: number, content: CommentContent): boolean;
|
|
165
|
+
/** Resolves (`done` false: reopens) a comment's thread. */
|
|
166
|
+
resolveComment(id: number, done?: boolean): boolean;
|
|
167
|
+
/** Deletes a comment, its replies and their anchors. */
|
|
168
|
+
deleteComment(id: number): boolean;
|
|
169
|
+
/** Deletes every comment. */
|
|
170
|
+
deleteAllComments(): boolean;
|
|
95
171
|
};
|
|
172
|
+
/** Whether edits are recorded as tracked changes. */
|
|
173
|
+
readonly trackingChanges: boolean;
|
|
96
174
|
isMarkActive(type: string): boolean;
|
|
97
|
-
/** The current document
|
|
175
|
+
/** The current document (schema v3). */
|
|
98
176
|
getDocument(): FoliDocument;
|
|
99
|
-
/** Replaces the whole document and resets history. */
|
|
100
|
-
setDocument(document:
|
|
177
|
+
/** Replaces the whole document (an older one migrated) and resets history. */
|
|
178
|
+
setDocument(document: EditorDocument): void;
|
|
101
179
|
subscribe(listener: EditorListener): () => void;
|
|
102
180
|
}
|
|
103
181
|
|
|
182
|
+
/** Word's page when a section leaves it out: Letter, 1 inch margins. */
|
|
183
|
+
const LETTER = { width: 12240, height: 15840, margin: 1440 };
|
|
184
|
+
|
|
185
|
+
/** A section's page setup as page settings (mm; the gutter added to the left margin, as Word does). */
|
|
186
|
+
export function sectionSettings(section: SectionJSON): DocumentSettings {
|
|
187
|
+
const m = section.margins ?? {};
|
|
188
|
+
return {
|
|
189
|
+
pageSize: {
|
|
190
|
+
width: mm(section.pageSize?.width ?? LETTER.width),
|
|
191
|
+
height: mm(section.pageSize?.height ?? LETTER.height),
|
|
192
|
+
},
|
|
193
|
+
margins: {
|
|
194
|
+
top: mm(Math.abs(m.top ?? LETTER.margin)),
|
|
195
|
+
right: mm(m.right ?? LETTER.margin),
|
|
196
|
+
bottom: mm(Math.abs(m.bottom ?? LETTER.margin)),
|
|
197
|
+
left: mm((m.left ?? LETTER.margin) + (m.gutter ?? 0)),
|
|
198
|
+
},
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
interface Loaded {
|
|
203
|
+
doc: Node;
|
|
204
|
+
settings: DocumentSettings;
|
|
205
|
+
envelope: WordEnvelope;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** Validates (and migrates) a document and reads what the editor keeps of it. */
|
|
209
|
+
function load(document: EditorDocument): Loaded {
|
|
210
|
+
const { content, ...envelope } = parseDocument(document);
|
|
211
|
+
return { doc: parseContent(content), settings: sectionSettings(envelope.section), envelope };
|
|
212
|
+
}
|
|
213
|
+
|
|
104
214
|
export function createEditor({
|
|
105
215
|
document = createDocument(),
|
|
106
216
|
plugins = [],
|
|
217
|
+
author = "Author",
|
|
218
|
+
trackChanges = false,
|
|
219
|
+
reviewView = "markup",
|
|
107
220
|
}: EditorOptions = {}): Editor {
|
|
108
|
-
let
|
|
109
|
-
|
|
110
|
-
|
|
221
|
+
let loaded = load(document);
|
|
222
|
+
let track: TrackState = { on: trackChanges, author, view: reviewView };
|
|
223
|
+
const pluginsFor = ({ envelope }: Loaded) => [
|
|
224
|
+
...plugins,
|
|
225
|
+
history(),
|
|
226
|
+
wordPlugin(envelope),
|
|
227
|
+
trackPlugin(track),
|
|
228
|
+
commentsPlugin(),
|
|
229
|
+
listKeymap(),
|
|
230
|
+
baseKeymap(),
|
|
231
|
+
];
|
|
232
|
+
let state = EditorState.create({ doc: loaded.doc, plugins: pluginsFor(loaded) });
|
|
111
233
|
const listeners = new Set<EditorListener>();
|
|
112
234
|
|
|
113
235
|
const notify = (transactions: readonly Transaction[]) => {
|
|
114
236
|
for (const listener of listeners) listener(state, transactions);
|
|
115
237
|
};
|
|
116
238
|
const dispatch = (tr: Transaction) => {
|
|
117
|
-
|
|
239
|
+
// With tracking on, the edit is rebuilt as tracked changes; one that cannot be is not made.
|
|
240
|
+
const tracked = trackTransaction(state, tr);
|
|
241
|
+
if (!tracked) return;
|
|
242
|
+
const result = state.applyTransaction(tracked);
|
|
118
243
|
state = result.state;
|
|
119
244
|
if (result.transactions.length) notify(result.transactions);
|
|
120
245
|
};
|
|
@@ -122,7 +247,7 @@ export function createEditor({
|
|
|
122
247
|
|
|
123
248
|
return {
|
|
124
249
|
get settings() {
|
|
125
|
-
return settings;
|
|
250
|
+
return loaded.settings;
|
|
126
251
|
},
|
|
127
252
|
get state() {
|
|
128
253
|
return state;
|
|
@@ -156,27 +281,42 @@ export function createEditor({
|
|
|
156
281
|
selectAll: () => run(selectAll),
|
|
157
282
|
undo: () => run(undo),
|
|
158
283
|
redo: () => run(redo),
|
|
159
|
-
wrapInList: (
|
|
160
|
-
toggleList: (
|
|
161
|
-
indentListItem: () => run(sinkListItem
|
|
162
|
-
outdentListItem: () => run(liftListItem
|
|
284
|
+
wrapInList: (kind) => run(wrapInList(kind)),
|
|
285
|
+
toggleList: (kind) => run(toggleList(kind)),
|
|
286
|
+
indentListItem: () => run(sinkListItem),
|
|
287
|
+
outdentListItem: () => run(liftListItem),
|
|
163
288
|
setLink: (href) => run(setLink(href)),
|
|
164
289
|
removeLink: () => run(removeLink),
|
|
165
290
|
setAlign: (align) => run(setAlign(align)),
|
|
166
291
|
setTextColor: (color) => run(setTextColor(color)),
|
|
167
292
|
setHighlight: (color) => run(setHighlight(color)),
|
|
168
|
-
|
|
293
|
+
setTrackChanges: (on, name) => run(setTrackChanges(on, name)),
|
|
294
|
+
setReviewView: (view) => run(setReviewView(view)),
|
|
295
|
+
acceptChange: () => run(acceptChange),
|
|
296
|
+
rejectChange: () => run(rejectChange),
|
|
297
|
+
acceptAllChanges: (name) => run(acceptAllChanges(name)),
|
|
298
|
+
rejectAllChanges: (name) => run(rejectAllChanges(name)),
|
|
299
|
+
nextChange: () => run(nextChange),
|
|
300
|
+
previousChange: () => run(previousChange),
|
|
301
|
+
addComment: (content, options) => run(addComment(content, options)),
|
|
302
|
+
replyToComment: (id, content, options) => run(replyToComment(id, content, options)),
|
|
303
|
+
editComment: (id, content) => run(editComment(id, content)),
|
|
304
|
+
resolveComment: (id, done) => run(resolveComment(id, done)),
|
|
305
|
+
deleteComment: (id) => run(deleteComment(id)),
|
|
306
|
+
deleteAllComments: () => run(deleteAllComments),
|
|
307
|
+
},
|
|
308
|
+
get trackingChanges() {
|
|
309
|
+
return trackState(state)?.on ?? false;
|
|
169
310
|
},
|
|
170
311
|
isMarkActive: (type) => isMarkActive(state, type),
|
|
171
312
|
getDocument: () => ({
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
content: state.doc.toJSON() as DocJSON,
|
|
313
|
+
...structuredClone(wordContext(state)?.envelope ?? loaded.envelope),
|
|
314
|
+
content: state.doc.toJSON(),
|
|
175
315
|
}),
|
|
176
316
|
setDocument(next) {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
state = EditorState.create({ doc, plugins:
|
|
317
|
+
track = trackState(state) ?? track;
|
|
318
|
+
loaded = load(next);
|
|
319
|
+
state = EditorState.create({ doc: loaded.doc, plugins: pluginsFor(loaded) });
|
|
180
320
|
notify([]);
|
|
181
321
|
},
|
|
182
322
|
subscribe(listener) {
|