@quillmark/svelte 0.33.0 → 0.34.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/README.md +73 -1
- package/THEMING.md +11 -10
- package/dist/core/codec/anchor.d.ts +12 -0
- package/dist/core/codec/anchor.js +39 -0
- package/dist/core/codec/drag-move.d.ts +11 -0
- package/dist/core/codec/drag-move.js +143 -0
- package/dist/core/codec/encode.js +14 -5
- package/dist/core/codec/field.d.ts +47 -1
- package/dist/core/codec/field.js +147 -38
- package/dist/core/codec/index.d.ts +8 -2
- package/dist/core/codec/index.js +9 -2
- package/dist/core/codec/islands.d.ts +4 -1
- package/dist/core/codec/islands.js +6 -0
- package/dist/core/codec/keymap.d.ts +3 -0
- package/dist/core/codec/keymap.js +13 -12
- package/dist/core/codec/leave.d.ts +17 -0
- package/dist/core/codec/leave.js +78 -0
- package/dist/core/codec/prose.css +217 -0
- package/dist/core/codec/review-diff.d.ts +12 -0
- package/dist/core/codec/review-diff.js +131 -0
- package/dist/core/codec/review.d.ts +131 -0
- package/dist/core/codec/review.js +1077 -0
- package/dist/core/codec/shortcuts.d.ts +20 -4
- package/dist/core/codec/shortcuts.js +27 -7
- package/dist/core/codec/slash.d.ts +3 -0
- package/dist/core/codec/slash.js +2 -1
- package/dist/core/codec/table-view.js +163 -25
- package/dist/core/codec/undo.d.ts +6 -0
- package/dist/core/codec/undo.js +25 -2
- package/dist/core/dragfollow.d.ts +18 -0
- package/dist/core/dragfollow.js +63 -0
- package/dist/core/theme.css +36 -7
- package/dist/preview/controller.js +16 -0
- package/dist/preview/strings.d.ts +4 -0
- package/dist/preview/strings.js +3 -1
- package/dist/visual/ArrayField.svelte +363 -57
- package/dist/visual/ArrayField.svelte.d.ts +5 -0
- package/dist/visual/BooleanField.svelte +60 -5
- package/dist/visual/BooleanField.svelte.d.ts +2 -1
- package/dist/visual/Card.svelte +27 -17
- package/dist/visual/CardControls.svelte +17 -7
- package/dist/visual/CardControls.svelte.d.ts +2 -0
- package/dist/visual/DateField.svelte +90 -39
- package/dist/visual/DateField.svelte.d.ts +2 -2
- package/dist/visual/Field.svelte +126 -6
- package/dist/visual/FieldHint.svelte +24 -9
- package/dist/visual/FieldLabel.svelte +128 -22
- package/dist/visual/FieldLabel.svelte.d.ts +9 -0
- package/dist/visual/FormatPopover.svelte +125 -20
- package/dist/visual/MatrixAdd.svelte +13 -6
- package/dist/visual/MatrixAdd.svelte.d.ts +3 -2
- package/dist/visual/MatrixField.svelte +36 -8
- package/dist/visual/NumberField.svelte +27 -6
- package/dist/visual/NumberField.svelte.d.ts +3 -0
- package/dist/visual/ObjectField.svelte +25 -1
- package/dist/visual/ObjectField.svelte.d.ts +6 -0
- package/dist/visual/ProseField.svelte +23 -0
- package/dist/visual/ProseField.svelte.d.ts +3 -0
- package/dist/visual/ProseValue.svelte +107 -17
- package/dist/visual/ProseValue.svelte.d.ts +18 -4
- package/dist/visual/ReviewPopover.svelte +179 -0
- package/dist/visual/ReviewPopover.svelte.d.ts +16 -0
- package/dist/visual/ReviewProposal.svelte +149 -0
- package/dist/visual/ReviewProposal.svelte.d.ts +15 -0
- package/dist/visual/ReviewRows.svelte +68 -0
- package/dist/visual/ReviewRows.svelte.d.ts +11 -0
- package/dist/visual/SlashMenu.svelte +13 -1
- package/dist/visual/SlashMenu.svelte.d.ts +2 -0
- package/dist/visual/VisualEditor.svelte +23 -0
- package/dist/visual/VisualEditor.svelte.d.ts +7 -0
- package/dist/visual/VisualEditorInner.svelte +671 -22
- package/dist/visual/VisualEditorInner.svelte.d.ts +7 -0
- package/dist/visual/controls.css +17 -2
- package/dist/visual/guidance.d.ts +12 -0
- package/dist/visual/guidance.js +37 -2
- package/dist/visual/icons/nodes.d.ts +14 -2
- package/dist/visual/icons/nodes.js +12 -1
- package/dist/visual/index.d.ts +6 -0
- package/dist/visual/index.js +2 -0
- package/dist/visual/listhistory.d.ts +36 -0
- package/dist/visual/listhistory.js +53 -0
- package/dist/visual/proposals.d.ts +40 -0
- package/dist/visual/proposals.js +219 -0
- package/dist/visual/props.d.ts +35 -0
- package/dist/visual/removed.d.ts +20 -0
- package/dist/visual/removed.js +30 -0
- package/dist/visual/review.d.ts +170 -0
- package/dist/visual/review.js +269 -0
- package/dist/visual/review.svelte.d.ts +106 -0
- package/dist/visual/review.svelte.js +356 -0
- package/dist/visual/rowdrag.svelte.js +44 -55
- package/dist/visual/strings.d.ts +48 -7
- package/dist/visual/strings.js +32 -7
- package/dist/visual/structure.d.ts +5 -0
- package/dist/visual/structure.js +1 -1
- package/dist/visual/undo.svelte.d.ts +40 -7
- package/dist/visual/undo.svelte.js +98 -25
- package/package.json +1 -1
|
@@ -0,0 +1,1077 @@
|
|
|
1
|
+
// Review, drawn in the leaf's own flow: what a writer other than the reader proposes to change
|
|
2
|
+
// here, and what of it the reader took. An item names blocks by the ids the leaf's nodes carry, so
|
|
3
|
+
// its place is the document's and rides every edit; nothing is measured from outside the view.
|
|
4
|
+
//
|
|
5
|
+
// A proposal is the paragraph it would rewrite, read as an edit of itself: the words that would go
|
|
6
|
+
// struck, the words that would come tinted in their place, and what it adds beyond the paragraph
|
|
7
|
+
// set in flow under it. None of it enters the document while it is proposed: the struck words are
|
|
8
|
+
// the leaf's own text under a decoration, and every added word is a widget. A proposal taken is a
|
|
9
|
+
// mark over the text it wrote, or a wedge where it only took text out.
|
|
10
|
+
//
|
|
11
|
+
// A proposal is an edit of the paragraph as its writer read it, or as it stood when it arrived. What the reader
|
|
12
|
+
// writes in the paragraph since is theirs: the proposal's stretches ride past it, and only the
|
|
13
|
+
// words it would rewrite are struck, as the reader has them now.
|
|
14
|
+
//
|
|
15
|
+
// Taking a proposal is the leaf's own edit, made from the plan the drawing was made from, so the
|
|
16
|
+
// words that land are the words that were drawn. It goes down the lane a keystroke takes: one
|
|
17
|
+
// transaction, one undo step, one commit.
|
|
18
|
+
import { closeHistory, isHistoryTransaction, undo } from 'prosemirror-history';
|
|
19
|
+
import { DOMSerializer, Fragment, Mark, Slice } from 'prosemirror-model';
|
|
20
|
+
import { keydownHandler } from 'prosemirror-keymap';
|
|
21
|
+
import { Plugin, PluginKey, Selection } from 'prosemirror-state';
|
|
22
|
+
import { Step, StepResult } from 'prosemirror-transform';
|
|
23
|
+
import { Decoration, DecorationSet } from 'prosemirror-view';
|
|
24
|
+
import { arriving, HARD_BREAK, ISLAND_SLOT } from './islands.js';
|
|
25
|
+
import { remoteKey } from './remote.js';
|
|
26
|
+
import { wordDiff } from './review-diff.js';
|
|
27
|
+
import { REVIEW_KEYS } from './shortcuts.js';
|
|
28
|
+
export const DEFAULT_REVIEW_STRINGS = {
|
|
29
|
+
reviewAccept: 'Accept',
|
|
30
|
+
reviewDismiss: 'Dismiss',
|
|
31
|
+
reviewAcceptTitle: 'Accept',
|
|
32
|
+
reviewDismissTitle: 'Dismiss',
|
|
33
|
+
reviewProposal: 'Proposed change',
|
|
34
|
+
reviewGone: 'Text removed here',
|
|
35
|
+
reviewOutdated: 'Edited since'
|
|
36
|
+
};
|
|
37
|
+
/** Each stretch through `mapping`. Words typed at a stretch's edge stay outside it, and one inside
|
|
38
|
+
* it grows it; `fresh` stretches were just written, so they reach over what the mapping wrote.
|
|
39
|
+
* `undefined` where nothing the proposal wrote or took out is left. */
|
|
40
|
+
function mapWritten(written, mapping, fresh = false) {
|
|
41
|
+
const out = [];
|
|
42
|
+
for (const w of written) {
|
|
43
|
+
const a = mapping.mapResult(w.from, fresh ? -1 : 1);
|
|
44
|
+
const b = mapping.mapResult(w.to, fresh ? 1 : -1);
|
|
45
|
+
if (w.from === w.to ? a.deleted : b.pos <= a.pos)
|
|
46
|
+
continue;
|
|
47
|
+
out.push({ ...w, from: a.pos, to: Math.max(a.pos, b.pos) });
|
|
48
|
+
}
|
|
49
|
+
return out.length ? out : undefined;
|
|
50
|
+
}
|
|
51
|
+
const reviewKey = new PluginKey('quill-review');
|
|
52
|
+
/** Tell the review layer the store took blocks under new ids, each `[was, now]`. */
|
|
53
|
+
export function renameBlocks(tr, renamed) {
|
|
54
|
+
if (renamed.length)
|
|
55
|
+
tr.setMeta(reviewKey, { renamed });
|
|
56
|
+
}
|
|
57
|
+
/** The id the block once named `id` stands under now. */
|
|
58
|
+
function nowOf(renamed, id) {
|
|
59
|
+
for (let hops = 0; hops < renamed.size && renamed.has(id); hops++)
|
|
60
|
+
id = renamed.get(id);
|
|
61
|
+
return id;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Taking a proposal, as a step of the transaction that writes it. It changes no content, and it
|
|
65
|
+
* is a step and not a transaction's meta so that the leaf's history carries the answer with the
|
|
66
|
+
* words: undoing the accept inverts it and the proposal draws again, and a redo, an undo event
|
|
67
|
+
* grown by typing, the depth cap and a rebase over a remote edit each move it with its words.
|
|
68
|
+
*/
|
|
69
|
+
class ReviewAnswer extends Step {
|
|
70
|
+
id;
|
|
71
|
+
to;
|
|
72
|
+
written;
|
|
73
|
+
/** `written` stands in the document as it is after the steps that write it: an accept's
|
|
74
|
+
* stretches, and on an undo of an accept the stretches it puts back, which an undo of that
|
|
75
|
+
* undo writes over again. */
|
|
76
|
+
constructor(id, to, written = []) {
|
|
77
|
+
super();
|
|
78
|
+
this.id = id;
|
|
79
|
+
this.to = to;
|
|
80
|
+
this.written = written;
|
|
81
|
+
}
|
|
82
|
+
apply(doc) {
|
|
83
|
+
return StepResult.ok(doc);
|
|
84
|
+
}
|
|
85
|
+
invert() {
|
|
86
|
+
return new ReviewAnswer(this.id, this.to ? undefined : 'accept', this.written);
|
|
87
|
+
}
|
|
88
|
+
map(mapping) {
|
|
89
|
+
return new ReviewAnswer(this.id, this.to, mapWritten(this.written, mapping) ?? []);
|
|
90
|
+
}
|
|
91
|
+
toJSON() {
|
|
92
|
+
return {
|
|
93
|
+
stepType: 'quillReviewAnswer',
|
|
94
|
+
id: this.id,
|
|
95
|
+
to: this.to ?? null,
|
|
96
|
+
written: this.written.map((w) => ({ ...w, before: w.before.toJSON() }))
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
static fromJSON(schema, json) {
|
|
100
|
+
return new ReviewAnswer(json.id, json.to ?? undefined, (json.written ?? []).map((w) => ({ ...w, before: Slice.fromJSON(schema, w.before) })));
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
Step.jsonID('quillReviewAnswer', ReviewAnswer);
|
|
104
|
+
function answering(state, id, to, written) {
|
|
105
|
+
let next = state;
|
|
106
|
+
if (state.written.has(id) || written) {
|
|
107
|
+
const all = new Map(state.written);
|
|
108
|
+
if (written)
|
|
109
|
+
all.set(id, written);
|
|
110
|
+
else
|
|
111
|
+
all.delete(id);
|
|
112
|
+
next = { ...next, written: all };
|
|
113
|
+
}
|
|
114
|
+
if (state.answered.get(id) === to)
|
|
115
|
+
return next;
|
|
116
|
+
const answered = new Map(state.answered);
|
|
117
|
+
if (to)
|
|
118
|
+
answered.set(id, to);
|
|
119
|
+
else
|
|
120
|
+
answered.delete(id);
|
|
121
|
+
return { ...next, answered };
|
|
122
|
+
}
|
|
123
|
+
function applyReview(tr, state) {
|
|
124
|
+
let next = state.replayed.length ? { ...state, replayed: [] } : state;
|
|
125
|
+
if (tr.docChanged && !tr.getMeta(remoteKey))
|
|
126
|
+
next = { ...next, edits: next.edits + 1 };
|
|
127
|
+
if (tr.docChanged && next.written.size) {
|
|
128
|
+
const all = new Map();
|
|
129
|
+
for (const [id, written] of next.written) {
|
|
130
|
+
const moved = mapWritten(written, tr.mapping);
|
|
131
|
+
if (moved)
|
|
132
|
+
all.set(id, moved);
|
|
133
|
+
}
|
|
134
|
+
next = { ...next, written: all };
|
|
135
|
+
}
|
|
136
|
+
const history = isHistoryTransaction(tr);
|
|
137
|
+
tr.steps.forEach((step, i) => {
|
|
138
|
+
if (!(step instanceof ReviewAnswer))
|
|
139
|
+
return;
|
|
140
|
+
const written = step.to && mapWritten(step.written, tr.mapping.slice(i + 1), true);
|
|
141
|
+
next = answering(next, step.id, step.to, written || undefined);
|
|
142
|
+
// Reported whether or not the answer is still held: the host's list may have dropped it.
|
|
143
|
+
if (history)
|
|
144
|
+
next = { ...next, replayed: [...next.replayed, { id: step.id, to: step.to }] };
|
|
145
|
+
});
|
|
146
|
+
const meta = tr.getMeta(reviewKey);
|
|
147
|
+
if (meta?.answer)
|
|
148
|
+
next = answering(next, meta.answer.id, meta.answer.to);
|
|
149
|
+
for (const id of meta?.prune ?? [])
|
|
150
|
+
next = answering(next, id, undefined);
|
|
151
|
+
if (meta?.renamed)
|
|
152
|
+
next = { ...next, renamed: new Map([...next.renamed, ...meta.renamed]) };
|
|
153
|
+
if (meta?.entered !== undefined)
|
|
154
|
+
next = { ...next, entered: meta.entered };
|
|
155
|
+
else if (next.entered !== undefined && (tr.docChanged || tr.selectionSet))
|
|
156
|
+
next = { ...next, entered: undefined };
|
|
157
|
+
return next;
|
|
158
|
+
}
|
|
159
|
+
function leavesOf(doc) {
|
|
160
|
+
const out = [];
|
|
161
|
+
const walk = (node, start, chain) => {
|
|
162
|
+
node.forEach((child, offset) => {
|
|
163
|
+
const pos = start + offset;
|
|
164
|
+
if (child.isTextblock || child.isAtom)
|
|
165
|
+
out.push({ node: child, pos, chain });
|
|
166
|
+
else
|
|
167
|
+
walk(child, pos + 1, `${chain}/${child.type.name}`);
|
|
168
|
+
});
|
|
169
|
+
};
|
|
170
|
+
walk(doc, 0, '');
|
|
171
|
+
return out;
|
|
172
|
+
}
|
|
173
|
+
/** A textblock's text with one character a leaf node, so an offset in it is a position in it. */
|
|
174
|
+
function textOf(block) {
|
|
175
|
+
return block.textBetween(0, block.content.size, undefined, (leaf) => leaf.type.name === 'hard_break' ? HARD_BREAK : ISLAND_SLOT);
|
|
176
|
+
}
|
|
177
|
+
/** Whether `b` is `a` rewritten in place: one kind of block, in the nesting `a` stands in as far
|
|
178
|
+
* as `b` spells one, so their difference is their words'. A block proposed bare keeps the
|
|
179
|
+
* containers it is in. Two atoms are in place only where they are equal. */
|
|
180
|
+
function inPlace(a, b) {
|
|
181
|
+
if (a.node.type !== b.node.type || !a.chain.endsWith(b.chain))
|
|
182
|
+
return false;
|
|
183
|
+
const { id: _a, block: _ab, ...x } = a.node.attrs;
|
|
184
|
+
const { id: _b, block: _bb, ...y } = b.node.attrs;
|
|
185
|
+
return JSON.stringify(x) === JSON.stringify(y);
|
|
186
|
+
}
|
|
187
|
+
/** The span of `leaf` a caret can stand in: a textblock's content, an atom's two sides. */
|
|
188
|
+
function reach(leaf) {
|
|
189
|
+
return leaf.node.isTextblock
|
|
190
|
+
? [leaf.pos + 1, leaf.pos + leaf.node.nodeSize - 1]
|
|
191
|
+
: [leaf.pos, leaf.pos + leaf.node.nodeSize];
|
|
192
|
+
}
|
|
193
|
+
/** `doc`'s blocks from its `from`th leaf on, in the containers they sat in. A numbered list
|
|
194
|
+
* that lost its head keeps counting from where the kept items stood. */
|
|
195
|
+
function tail(doc, from) {
|
|
196
|
+
let seen = 0;
|
|
197
|
+
const keep = (node) => {
|
|
198
|
+
if (node.isTextblock || node.isAtom)
|
|
199
|
+
return seen++ >= from ? node : undefined;
|
|
200
|
+
const kids = [];
|
|
201
|
+
let dropped = 0;
|
|
202
|
+
node.forEach((child) => {
|
|
203
|
+
const kept = keep(child);
|
|
204
|
+
if (kept)
|
|
205
|
+
kids.push(kept);
|
|
206
|
+
else if (!kids.length)
|
|
207
|
+
dropped++;
|
|
208
|
+
});
|
|
209
|
+
if (!kids.length)
|
|
210
|
+
return undefined;
|
|
211
|
+
const attrs = node.type.name === 'ordered_list'
|
|
212
|
+
? { ...node.attrs, start: node.attrs.start + dropped }
|
|
213
|
+
: node.attrs;
|
|
214
|
+
return node.type.create(attrs, kids);
|
|
215
|
+
};
|
|
216
|
+
const out = [];
|
|
217
|
+
doc.forEach((child) => {
|
|
218
|
+
const kept = keep(child);
|
|
219
|
+
if (kept)
|
|
220
|
+
out.push(kept);
|
|
221
|
+
});
|
|
222
|
+
return out;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* The edit that takes a proposal: its pieces, last first, so each still stands where it was
|
|
226
|
+
* read, and the stretches it wrote. `undefined` where the pieces no longer make a document the
|
|
227
|
+
* schema holds.
|
|
228
|
+
*/
|
|
229
|
+
function taking(state, plan, anchors) {
|
|
230
|
+
const tr = state.tr;
|
|
231
|
+
const made = [];
|
|
232
|
+
try {
|
|
233
|
+
for (const piece of [...plan].sort((a, b) => b.from - a.from)) {
|
|
234
|
+
let { from, to } = piece;
|
|
235
|
+
if (piece.lift) {
|
|
236
|
+
const $at = tr.doc.resolve(from);
|
|
237
|
+
for (let d = $at.depth; d > 0 && $at.node(d).childCount === 1; d--) {
|
|
238
|
+
from = $at.before(d);
|
|
239
|
+
to = $at.after(d);
|
|
240
|
+
}
|
|
241
|
+
// A leaf holds a block at the least: its last one is emptied where it stands.
|
|
242
|
+
if (from === 0 && to === tr.doc.content.size) {
|
|
243
|
+
made.push({ from, to, before: tr.doc.slice(from, to), step: tr.steps.length });
|
|
244
|
+
tr.replaceWith(from, to, tr.doc.type.createAndFill().content);
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
made.push({ from, to, before: tr.doc.slice(from, to), step: tr.steps.length });
|
|
249
|
+
tr.replaceWith(from, to, arriving(tr.doc, piece.content));
|
|
250
|
+
}
|
|
251
|
+
tr.doc.check();
|
|
252
|
+
}
|
|
253
|
+
catch {
|
|
254
|
+
return undefined;
|
|
255
|
+
}
|
|
256
|
+
if (!tr.docChanged)
|
|
257
|
+
return undefined;
|
|
258
|
+
const written = made.map(({ from, to, before, step }) => {
|
|
259
|
+
const after = tr.mapping.slice(step);
|
|
260
|
+
const swallowed = anchors
|
|
261
|
+
.filter((a) => a.end > a.start && from <= a.start && a.end <= to)
|
|
262
|
+
.map((a) => ({ id: a.id, start: a.start - from, end: a.end - from }));
|
|
263
|
+
return { from: after.map(from, -1), to: after.map(to, 1), before, anchors: swallowed };
|
|
264
|
+
});
|
|
265
|
+
return { tr: closeHistory(tr), written };
|
|
266
|
+
}
|
|
267
|
+
/** The reader's edit that takes back what proposal `id` wrote, wherever the stretches have
|
|
268
|
+
* moved to and whatever was typed inside them, so the proposal is open again. */
|
|
269
|
+
function putBack(state, id, written, channel) {
|
|
270
|
+
const tr = state.tr;
|
|
271
|
+
const made = [];
|
|
272
|
+
for (const w of [...written].sort((a, b) => b.from - a.from)) {
|
|
273
|
+
made.push({ w, step: tr.steps.length });
|
|
274
|
+
tr.replace(w.from, w.to, w.before);
|
|
275
|
+
}
|
|
276
|
+
const back = made.map(({ w, step }) => {
|
|
277
|
+
const after = tr.mapping.slice(step);
|
|
278
|
+
return { ...w, from: after.map(w.from, -1), to: after.map(w.to, 1) };
|
|
279
|
+
});
|
|
280
|
+
const anchors = back.flatMap((w) => (w.anchors ?? []).map((a) => ({ id: a.id, start: w.from + a.start, end: w.from + a.end })));
|
|
281
|
+
if (anchors.length)
|
|
282
|
+
channel.restore?.(tr, anchors);
|
|
283
|
+
return closeHistory(tr.step(new ReviewAnswer(id, undefined, back)));
|
|
284
|
+
}
|
|
285
|
+
/** The marks on each offset of a textblock's content. */
|
|
286
|
+
function marksOf(block) {
|
|
287
|
+
const out = [];
|
|
288
|
+
block.forEach((child) => {
|
|
289
|
+
for (let k = 0; k < child.nodeSize; k++)
|
|
290
|
+
out.push(child.marks);
|
|
291
|
+
});
|
|
292
|
+
return out;
|
|
293
|
+
}
|
|
294
|
+
/** The runs of `[at, end)` in `a` whose marks differ from the same words in `b`, which stand
|
|
295
|
+
* `shift` further on. */
|
|
296
|
+
function formatRuns(a, b, at, end, shift, after) {
|
|
297
|
+
const out = [];
|
|
298
|
+
let run = -1;
|
|
299
|
+
for (let i = at; i <= end; i++) {
|
|
300
|
+
const differs = i < end && !Mark.sameSet(a[i], b[i + shift]);
|
|
301
|
+
if (differs && run < 0)
|
|
302
|
+
run = i;
|
|
303
|
+
else if (!differs && run >= 0) {
|
|
304
|
+
out.push({ at: run, end: i, insert: after.slice(run + shift, i + shift), format: true });
|
|
305
|
+
run = -1;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
return out;
|
|
309
|
+
}
|
|
310
|
+
/** A value of two nodes, kept while both are: a node an edit leaves alone is the same object
|
|
311
|
+
* after it, so a paragraph the reader did not touch is not diffed again. */
|
|
312
|
+
function byPair(of) {
|
|
313
|
+
const kept = new WeakMap();
|
|
314
|
+
return (a, b) => {
|
|
315
|
+
let row = kept.get(a);
|
|
316
|
+
if (!row)
|
|
317
|
+
kept.set(a, (row = new WeakMap()));
|
|
318
|
+
if (row.has(b))
|
|
319
|
+
return row.get(b);
|
|
320
|
+
const value = of(a, b);
|
|
321
|
+
row.set(b, value);
|
|
322
|
+
return value;
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
/** The hunks between two paired textblocks: their words', and inside the words they keep,
|
|
326
|
+
* each run whose formatting differs. */
|
|
327
|
+
const hunksOf = byPair((a, b) => {
|
|
328
|
+
const before = textOf(a);
|
|
329
|
+
const after = textOf(b);
|
|
330
|
+
const words = before === after ? [] : wordDiff(before, after);
|
|
331
|
+
const ma = marksOf(a);
|
|
332
|
+
const mb = marksOf(b);
|
|
333
|
+
const out = [];
|
|
334
|
+
let kept = 0;
|
|
335
|
+
let shift = 0;
|
|
336
|
+
for (const hunk of words) {
|
|
337
|
+
out.push(...formatRuns(ma, mb, kept, hunk.at, shift, after), hunk);
|
|
338
|
+
shift += hunk.insert.length - (hunk.end - hunk.at);
|
|
339
|
+
kept = hunk.end;
|
|
340
|
+
}
|
|
341
|
+
out.push(...formatRuns(ma, mb, kept, before.length, shift, after));
|
|
342
|
+
return out;
|
|
343
|
+
});
|
|
344
|
+
/** What the reader wrote in a block since a proposal arrived over it. */
|
|
345
|
+
const sinceOf = byPair((was, now) => was === now ? [] : wordDiff(textOf(was), textOf(now)));
|
|
346
|
+
/**
|
|
347
|
+
* What `to` rewrites of a block that read `was` when the proposal was made and reads `now`. Each
|
|
348
|
+
* stretch the proposal changes in `was` is carried through what was written since: words typed
|
|
349
|
+
* at its edge stay outside it, and where a word it would rewrite was itself rewritten, the stretch
|
|
350
|
+
* covers the rewriting. A stretch that already reads as proposed is left out.
|
|
351
|
+
*
|
|
352
|
+
* The block is `outdated` where the reader's writing meets a stretch the proposal changes, since
|
|
353
|
+
* taking it would write over words the reader wrote. Where that writing reaches past the stretch,
|
|
354
|
+
* the stretch carried onto words it was never computed against would write a sentence nobody
|
|
355
|
+
* proposed, so the whole block as it now reads gives way to the whole proposed one.
|
|
356
|
+
*/
|
|
357
|
+
function rewrites(was, now, to) {
|
|
358
|
+
const since = sinceOf(was, now);
|
|
359
|
+
const hunks = hunksOf(was, to);
|
|
360
|
+
const meets = (s, h) => s.at === s.end
|
|
361
|
+
? h.at < s.at && s.at < h.end
|
|
362
|
+
: h.at === h.end
|
|
363
|
+
? s.at < h.at && h.at < s.end
|
|
364
|
+
: s.at < h.end && h.at < s.end;
|
|
365
|
+
const past = since.some((s) => hunks.some((h) => s.at < h.end && h.at < s.end && !(h.at <= s.at && s.end <= h.end)));
|
|
366
|
+
if (past)
|
|
367
|
+
return {
|
|
368
|
+
outdated: true,
|
|
369
|
+
stretches: [{ at: 0, end: now.content.size, content: to.content }]
|
|
370
|
+
};
|
|
371
|
+
const outdated = since.some((s) => hunks.some((h) => meets(s, h)));
|
|
372
|
+
const text = since.length ? textOf(now) : '';
|
|
373
|
+
const nowMarks = since.length ? marksOf(now) : [];
|
|
374
|
+
const toMarks = since.length ? marksOf(to) : [];
|
|
375
|
+
const formatted = (at, end, start) => nowMarks.slice(at, end).every((marks, i) => Mark.sameSet(marks, toMarks[start + i]));
|
|
376
|
+
const through = (pos, edge) => {
|
|
377
|
+
let moved = pos;
|
|
378
|
+
for (const hunk of since) {
|
|
379
|
+
const grew = hunk.insert.length - (hunk.end - hunk.at);
|
|
380
|
+
if (hunk.at === hunk.end ? (edge === 'start' ? hunk.at <= pos : hunk.at < pos) : hunk.end <= pos)
|
|
381
|
+
moved += grew;
|
|
382
|
+
else if (hunk.at < pos && pos < hunk.end)
|
|
383
|
+
return hunk.at + (moved - pos) + (edge === 'end' ? hunk.insert.length : 0);
|
|
384
|
+
}
|
|
385
|
+
return moved;
|
|
386
|
+
};
|
|
387
|
+
const out = [];
|
|
388
|
+
// Where each insert starts in the proposed block: its hunk's start in the block it was
|
|
389
|
+
// made on, moved by what the hunks before it added and took.
|
|
390
|
+
let shift = 0;
|
|
391
|
+
for (const hunk of hunks) {
|
|
392
|
+
const start = hunk.at + shift;
|
|
393
|
+
shift += hunk.insert.length - (hunk.end - hunk.at);
|
|
394
|
+
const at = through(hunk.at, 'start');
|
|
395
|
+
const end = Math.max(at, through(hunk.end, 'end'));
|
|
396
|
+
if (since.length &&
|
|
397
|
+
text.slice(at, end) === hunk.insert &&
|
|
398
|
+
(!hunk.format || formatted(at, end, start)))
|
|
399
|
+
continue;
|
|
400
|
+
out.push({ at, end, content: to.content.cut(start, start + hunk.insert.length) });
|
|
401
|
+
}
|
|
402
|
+
return { outdated, stretches: out };
|
|
403
|
+
}
|
|
404
|
+
/** {@link rewrites}, kept while its three blocks are. */
|
|
405
|
+
const rewritten = (() => {
|
|
406
|
+
const kept = byPair(() => new WeakMap());
|
|
407
|
+
return (was, now, to) => {
|
|
408
|
+
const row = kept(was, now);
|
|
409
|
+
let out = row.get(to);
|
|
410
|
+
if (!out)
|
|
411
|
+
row.set(to, (out = rewrites(was, now, to)));
|
|
412
|
+
return out;
|
|
413
|
+
};
|
|
414
|
+
})();
|
|
415
|
+
/** A chord as `aria-keyshortcuts` spells it, `Mod` as either platform's. */
|
|
416
|
+
const ariaKeys = (chord) => ['Meta', 'Control'].map((mod) => chord.replace('Mod', mod).replaceAll('-', '+')).join(' ');
|
|
417
|
+
function button(act, id, action, label, title, keys) {
|
|
418
|
+
const el = document.createElement('button');
|
|
419
|
+
el.type = 'button';
|
|
420
|
+
el.tabIndex = -1;
|
|
421
|
+
el.className = `qm-review-act qm-review-${action}`;
|
|
422
|
+
el.textContent = label;
|
|
423
|
+
el.title = title;
|
|
424
|
+
el.setAttribute('aria-keyshortcuts', ariaKeys(keys));
|
|
425
|
+
// The press keeps the caret where it is: the answer is about the text, not a move off it.
|
|
426
|
+
el.addEventListener('mousedown', (e) => e.preventDefault());
|
|
427
|
+
el.addEventListener('click', (e) => {
|
|
428
|
+
e.preventDefault();
|
|
429
|
+
act(id, action);
|
|
430
|
+
});
|
|
431
|
+
return el;
|
|
432
|
+
}
|
|
433
|
+
/** A proposal's two buttons: a run at the end of its last line, or a row of their own under a
|
|
434
|
+
* block that has no line to end. */
|
|
435
|
+
function chip(strings, act, id, row, outdated) {
|
|
436
|
+
const el = document.createElement(row ? 'div' : 'span');
|
|
437
|
+
el.className = ['qm-review-chip', row && 'qm-review-chip-row', outdated && 'qm-review-outdated']
|
|
438
|
+
.filter(Boolean)
|
|
439
|
+
.join(' ');
|
|
440
|
+
el.contentEditable = 'false';
|
|
441
|
+
el.setAttribute('data-qm-review', id);
|
|
442
|
+
el.setAttribute('role', 'group');
|
|
443
|
+
el.setAttribute('aria-label', strings.reviewProposal);
|
|
444
|
+
const acts = document.createElement('span');
|
|
445
|
+
acts.className = 'qm-review-acts';
|
|
446
|
+
if (outdated) {
|
|
447
|
+
const label = document.createElement('span');
|
|
448
|
+
label.className = 'qm-review-outdated-label';
|
|
449
|
+
label.textContent = strings.reviewOutdated;
|
|
450
|
+
acts.append(label);
|
|
451
|
+
}
|
|
452
|
+
acts.append(button(act, id, 'accept', strings.reviewAccept, strings.reviewAcceptTitle, REVIEW_KEYS.accept), button(act, id, 'dismiss', strings.reviewDismiss, strings.reviewDismissTitle, REVIEW_KEYS.dismiss));
|
|
453
|
+
// The space a run of answers stands off its line by, which a wrapped run leaves behind.
|
|
454
|
+
el.append(row ? '' : ' ', acts);
|
|
455
|
+
return el;
|
|
456
|
+
}
|
|
457
|
+
/** The last textblock's element under `el`, where a run can be appended to its last line. */
|
|
458
|
+
function lastLine(el) {
|
|
459
|
+
let at = el;
|
|
460
|
+
for (;;) {
|
|
461
|
+
if (/^(P|H[1-6]|CODE)$/.test(at.tagName))
|
|
462
|
+
return at;
|
|
463
|
+
const last = at.lastElementChild;
|
|
464
|
+
if (!(last instanceof HTMLElement))
|
|
465
|
+
return undefined;
|
|
466
|
+
at = last;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
/** The id of the stored block a leaf decodes from: a block island's own, any other's `id`. */
|
|
470
|
+
function blockOf(node) {
|
|
471
|
+
return String((node.type.name === 'island_block' ? node.attrs.block : node.attrs.id) ?? '');
|
|
472
|
+
}
|
|
473
|
+
function placeOf(item, leaves, now) {
|
|
474
|
+
const index = (id) => leaves.findIndex((leaf) => blockOf(leaf.node) === now(id));
|
|
475
|
+
if ('replace' in item) {
|
|
476
|
+
const named = new Set(item.replace.map(now));
|
|
477
|
+
const cover = leaves.flatMap((leaf, i) => (named.has(blockOf(leaf.node)) ? [i] : []));
|
|
478
|
+
if (!cover.length)
|
|
479
|
+
return 'gone';
|
|
480
|
+
const found = new Set(cover.map((i) => blockOf(leaves[i].node)));
|
|
481
|
+
return { cover, beside: -1, ahead: false, missing: found.size < named.size };
|
|
482
|
+
}
|
|
483
|
+
const at = item.insert;
|
|
484
|
+
let beside = leaves.length - 1;
|
|
485
|
+
let ahead = false;
|
|
486
|
+
if (at !== 'end') {
|
|
487
|
+
const i = index('after' in at ? at.after : at.before);
|
|
488
|
+
if (i < 0)
|
|
489
|
+
return 'gone';
|
|
490
|
+
if ('after' in at)
|
|
491
|
+
beside = i;
|
|
492
|
+
else if (i === 0)
|
|
493
|
+
[beside, ahead] = [0, true];
|
|
494
|
+
else
|
|
495
|
+
beside = i - 1;
|
|
496
|
+
}
|
|
497
|
+
if (beside < 0)
|
|
498
|
+
return 'gone';
|
|
499
|
+
// A leaf of one empty paragraph is filled by what is added to it.
|
|
500
|
+
const leaf = leaves[beside];
|
|
501
|
+
if (leaves.length === 1 && leaf.node.isTextblock && !leaf.node.content.size)
|
|
502
|
+
return { cover: [beside], beside, ahead: false, missing: false };
|
|
503
|
+
return { cover: [], beside, ahead, missing: false };
|
|
504
|
+
}
|
|
505
|
+
/** The blocks a proposal's writer read, by the id each was read under; `undefined` where
|
|
506
|
+
* `before` does not read as one block for each id. */
|
|
507
|
+
const reads = new WeakMap();
|
|
508
|
+
function readOf(ids, before, channel) {
|
|
509
|
+
let row = reads.get(ids);
|
|
510
|
+
if (!row)
|
|
511
|
+
reads.set(ids, (row = new Map()));
|
|
512
|
+
if (row.has(before))
|
|
513
|
+
return row.get(before);
|
|
514
|
+
const doc = before.trim() ? channel.parse(before) : undefined;
|
|
515
|
+
const blocks = doc ? leavesOf(doc) : [];
|
|
516
|
+
const out = blocks.length === ids.length ? new Map(ids.map((id, k) => [id, blocks[k].node])) : undefined;
|
|
517
|
+
row.set(before, out);
|
|
518
|
+
return out;
|
|
519
|
+
}
|
|
520
|
+
function draw(state, channel, act, bases) {
|
|
521
|
+
const { answered, written, renamed } = reviewKey.getState(state);
|
|
522
|
+
const now = (id) => nowOf(renamed, id);
|
|
523
|
+
const listed = channel.items();
|
|
524
|
+
// A base outlives its item's answer and its leaving the list while a block it covered
|
|
525
|
+
// stands, so a proposal brought back by an undo draws against the paragraph it first drew over.
|
|
526
|
+
if (bases.size) {
|
|
527
|
+
const ids = new Set(listed.map((item) => item.id));
|
|
528
|
+
let blocks;
|
|
529
|
+
const stands = (place) => {
|
|
530
|
+
blocks ??= new Set(leavesOf(state.doc).map((leaf) => blockOf(leaf.node)));
|
|
531
|
+
return place.some((id) => blocks.has(id));
|
|
532
|
+
};
|
|
533
|
+
for (const [id, base] of bases)
|
|
534
|
+
if (!ids.has(id) && !stands(base.place))
|
|
535
|
+
bases.delete(id);
|
|
536
|
+
}
|
|
537
|
+
const items = answered.size
|
|
538
|
+
? listed.filter((item) => !answered.has(item.id) || written.has(item.id))
|
|
539
|
+
: listed;
|
|
540
|
+
const plans = new Map();
|
|
541
|
+
const undrawn = [];
|
|
542
|
+
const outdated = [];
|
|
543
|
+
const accepted = [];
|
|
544
|
+
const decos = [];
|
|
545
|
+
const lighting = new Set();
|
|
546
|
+
if (!items.length)
|
|
547
|
+
return { decos, lighting, spans: [], plans, outdated, undrawn, accepted };
|
|
548
|
+
const { doc } = state;
|
|
549
|
+
const strings = channel.strings();
|
|
550
|
+
const serializer = DOMSerializer.fromSchema(state.schema);
|
|
551
|
+
const lights = (id, make) => {
|
|
552
|
+
lighting.add(id);
|
|
553
|
+
decos.push({ id, make });
|
|
554
|
+
};
|
|
555
|
+
const spans = [];
|
|
556
|
+
let leaves;
|
|
557
|
+
/** The paragraphs a proposal rewrites, and the places a proposal that only adds adds at:
|
|
558
|
+
* a paragraph's end, or `-1` for the head of the leaf. */
|
|
559
|
+
const taken = new Set();
|
|
560
|
+
const adding = new Set();
|
|
561
|
+
/** A taken proposal's mark over `[from, to)`, or its wedge where text was only taken out. */
|
|
562
|
+
const changed = (id, from, to) => {
|
|
563
|
+
if (to > from) {
|
|
564
|
+
lights(id, (on) => Decoration.inline(from, to, {
|
|
565
|
+
class: on ? 'qm-review-changed qm-review-lit' : 'qm-review-changed',
|
|
566
|
+
'data-qm-review': id
|
|
567
|
+
}));
|
|
568
|
+
return;
|
|
569
|
+
}
|
|
570
|
+
lights(id, (on) => Decoration.widget(from, () => {
|
|
571
|
+
const el = document.createElement('span');
|
|
572
|
+
el.className = on ? 'qm-review-gone qm-review-lit' : 'qm-review-gone';
|
|
573
|
+
el.contentEditable = 'false';
|
|
574
|
+
el.setAttribute('data-qm-review', id);
|
|
575
|
+
el.setAttribute('role', 'img');
|
|
576
|
+
el.setAttribute('aria-label', strings.reviewGone);
|
|
577
|
+
return el;
|
|
578
|
+
}, { side: -1, key: `gone:${id}:${on}` }));
|
|
579
|
+
};
|
|
580
|
+
for (const item of items) {
|
|
581
|
+
// A proposal taken here is drawn as the change it made, while the host lists it.
|
|
582
|
+
const wrote = written.get(item.id);
|
|
583
|
+
if (wrote) {
|
|
584
|
+
for (const w of wrote) {
|
|
585
|
+
// A wedge stands in a line: one where whole blocks went stands at the end of the
|
|
586
|
+
// line before, or the start of the one after.
|
|
587
|
+
const $at = doc.resolve(w.from);
|
|
588
|
+
if (w.to > w.from || $at.parent.inlineContent)
|
|
589
|
+
changed(item.id, w.from, w.to);
|
|
590
|
+
else {
|
|
591
|
+
const at = Selection.findFrom($at, -1, true) ?? Selection.findFrom($at, 1, true);
|
|
592
|
+
if (at)
|
|
593
|
+
changed(item.id, at.from, at.from);
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
spans.push({
|
|
597
|
+
id: item.id,
|
|
598
|
+
proposed: false,
|
|
599
|
+
from: Math.min(...wrote.map((w) => w.from)),
|
|
600
|
+
to: Math.max(...wrote.map((w) => w.to))
|
|
601
|
+
});
|
|
602
|
+
accepted.push({
|
|
603
|
+
id: item.id,
|
|
604
|
+
before: wrote
|
|
605
|
+
.map((w) => w.before.content.textBetween(0, w.before.content.size, '\n'))
|
|
606
|
+
.filter(Boolean)
|
|
607
|
+
.join('\n')
|
|
608
|
+
});
|
|
609
|
+
continue;
|
|
610
|
+
}
|
|
611
|
+
const name = { 'data-qm-review': item.id };
|
|
612
|
+
leaves ??= leavesOf(doc);
|
|
613
|
+
const at = placeOf(item, leaves, now);
|
|
614
|
+
if (at === 'gone') {
|
|
615
|
+
undrawn.push({ id: item.id, reason: 'block' });
|
|
616
|
+
continue;
|
|
617
|
+
}
|
|
618
|
+
const { cover, beside, ahead } = at;
|
|
619
|
+
const place = ahead ? -1 : beside;
|
|
620
|
+
if (cover.length ? cover.some((i) => taken.has(i)) : adding.has(place)) {
|
|
621
|
+
undrawn.push({ id: item.id, reason: 'taken' });
|
|
622
|
+
continue;
|
|
623
|
+
}
|
|
624
|
+
const proposed = item.after.trim() ? channel.parse(item.after) : undefined;
|
|
625
|
+
if (item.after.trim() && !proposed) {
|
|
626
|
+
undrawn.push({ id: item.id, reason: 'parse' });
|
|
627
|
+
continue;
|
|
628
|
+
}
|
|
629
|
+
const next = proposed ? leavesOf(proposed) : [];
|
|
630
|
+
const covered = cover.map((i) => leaves[i]);
|
|
631
|
+
let paired = 0;
|
|
632
|
+
while (paired < covered.length &&
|
|
633
|
+
paired < next.length &&
|
|
634
|
+
inPlace(covered[paired], next[paired]))
|
|
635
|
+
paired++;
|
|
636
|
+
const added = proposed && paired < next.length ? tail(proposed, paired) : [];
|
|
637
|
+
if (!covered.length && !added.length)
|
|
638
|
+
continue;
|
|
639
|
+
const plan = [];
|
|
640
|
+
const mark = decos.length;
|
|
641
|
+
let stale = at.missing;
|
|
642
|
+
let base;
|
|
643
|
+
if ('replace' in item && item.before !== undefined) {
|
|
644
|
+
const read = readOf(item.replace, item.before, channel);
|
|
645
|
+
const named = new Map(item.replace.map((id) => [now(id), id]));
|
|
646
|
+
const was = covered.map((leaf) => read?.get(named.get(blockOf(leaf.node)) ?? ''));
|
|
647
|
+
stale ||= was.some((node) => !node);
|
|
648
|
+
base = was.map((node, k) => node ?? covered[k].node);
|
|
649
|
+
}
|
|
650
|
+
else {
|
|
651
|
+
const seen = bases.get(item.id);
|
|
652
|
+
base =
|
|
653
|
+
seen?.after === item.after && seen.blocks.length === covered.length
|
|
654
|
+
? seen.blocks
|
|
655
|
+
: covered.map((leaf) => leaf.node);
|
|
656
|
+
bases.set(item.id, {
|
|
657
|
+
after: item.after,
|
|
658
|
+
place: covered.map((l) => blockOf(l.node)),
|
|
659
|
+
blocks: base
|
|
660
|
+
});
|
|
661
|
+
}
|
|
662
|
+
covered.forEach((leaf, k) => {
|
|
663
|
+
const [lo, hi] = reach(leaf);
|
|
664
|
+
const whole = k >= paired;
|
|
665
|
+
lights(item.id, (on) => Decoration.node(leaf.pos, leaf.pos + leaf.node.nodeSize, {
|
|
666
|
+
class: ['qm-review-held', whole && 'qm-review-out', on && 'qm-review-lit']
|
|
667
|
+
.filter(Boolean)
|
|
668
|
+
.join(' ')
|
|
669
|
+
}));
|
|
670
|
+
if (whole) {
|
|
671
|
+
// Taking it removes what the reader wrote in the block since it was read.
|
|
672
|
+
stale ||= !base[k].content.eq(leaf.node.content);
|
|
673
|
+
plan.push({
|
|
674
|
+
from: leaf.pos,
|
|
675
|
+
to: leaf.pos + leaf.node.nodeSize,
|
|
676
|
+
content: Fragment.empty,
|
|
677
|
+
lift: true
|
|
678
|
+
});
|
|
679
|
+
}
|
|
680
|
+
if (!leaf.node.isTextblock)
|
|
681
|
+
return;
|
|
682
|
+
const strike = (from, to) => {
|
|
683
|
+
if (to > from)
|
|
684
|
+
decos.push(Decoration.inline(from, to, { nodeName: 'del', class: 'qm-review-del', ...name }));
|
|
685
|
+
};
|
|
686
|
+
if (whole)
|
|
687
|
+
return strike(lo, hi);
|
|
688
|
+
const to = next[k].node;
|
|
689
|
+
const read = base[k].type === leaf.node.type;
|
|
690
|
+
stale ||= !read;
|
|
691
|
+
const { outdated: past, stretches } = rewritten(read ? base[k] : leaf.node, leaf.node, to);
|
|
692
|
+
stale ||= past;
|
|
693
|
+
stretches.forEach(({ at, end, content }, n) => {
|
|
694
|
+
strike(lo + at, lo + end);
|
|
695
|
+
plan.push({ from: lo + at, to: lo + end, content });
|
|
696
|
+
if (!content.size)
|
|
697
|
+
return;
|
|
698
|
+
decos.push(Decoration.widget(lo + end, () => {
|
|
699
|
+
const el = document.createElement('ins');
|
|
700
|
+
el.className = 'qm-review-ins';
|
|
701
|
+
el.contentEditable = 'false';
|
|
702
|
+
el.setAttribute('data-qm-review', item.id);
|
|
703
|
+
el.append(serializer.serializeFragment(content));
|
|
704
|
+
return el;
|
|
705
|
+
}, { side: -1, key: `ins:${item.id}:${n}:${content.toString()}` }));
|
|
706
|
+
});
|
|
707
|
+
});
|
|
708
|
+
if (!plan.length && !added.length) {
|
|
709
|
+
decos.length = mark;
|
|
710
|
+
lighting.delete(item.id);
|
|
711
|
+
undrawn.push({ id: item.id, reason: 'applied' });
|
|
712
|
+
continue;
|
|
713
|
+
}
|
|
714
|
+
if (cover.length)
|
|
715
|
+
for (const i of cover)
|
|
716
|
+
taken.add(i);
|
|
717
|
+
else
|
|
718
|
+
adding.add(place);
|
|
719
|
+
plans.set(item.id, plan);
|
|
720
|
+
if (stale)
|
|
721
|
+
outdated.push(item.id);
|
|
722
|
+
const last = covered.at(-1) ?? leaves[beside];
|
|
723
|
+
const $last = doc.resolve(last.pos);
|
|
724
|
+
/** Past the outermost block holding the last one, or the head of the leaf. */
|
|
725
|
+
const outer = ahead ? 0 : $last.depth ? $last.after(1) : last.pos + last.node.nodeSize;
|
|
726
|
+
const row = () => {
|
|
727
|
+
decos.push(Decoration.widget(outer, () => chip(strings, act, item.id, true, stale), {
|
|
728
|
+
side: ahead ? -1 : 9999,
|
|
729
|
+
key: `chip:${item.id}:row:${stale}`,
|
|
730
|
+
stopEvent: () => true
|
|
731
|
+
}));
|
|
732
|
+
};
|
|
733
|
+
if (added.length) {
|
|
734
|
+
// A proposal that continues the list its paragraph is in adds items to that list.
|
|
735
|
+
const listed = !ahead &&
|
|
736
|
+
$last.parent.type.name === 'list_item' &&
|
|
737
|
+
added[0].type === $last.node($last.depth - 1).type;
|
|
738
|
+
const pieces = [];
|
|
739
|
+
if (listed) {
|
|
740
|
+
const at = $last.after($last.depth);
|
|
741
|
+
added[0].forEach((li) => pieces.push({ at, node: li }));
|
|
742
|
+
}
|
|
743
|
+
for (const node of listed ? added.slice(1) : added)
|
|
744
|
+
pieces.push({ at: outer, node });
|
|
745
|
+
for (const piece of pieces) {
|
|
746
|
+
const held = plan.at(-1);
|
|
747
|
+
if (held && !held.lift && held.from === piece.at && held.to === piece.at)
|
|
748
|
+
held.content = held.content.addToEnd(piece.node);
|
|
749
|
+
else
|
|
750
|
+
plan.push({ from: piece.at, to: piece.at, content: Fragment.from(piece.node) });
|
|
751
|
+
}
|
|
752
|
+
const closing = pieces.length - 1;
|
|
753
|
+
const chipped = endsInLine(pieces[closing].node);
|
|
754
|
+
pieces.forEach((piece, n) => {
|
|
755
|
+
const holds = chipped && n === closing;
|
|
756
|
+
lights(item.id, (on) => Decoration.widget(piece.at, () => {
|
|
757
|
+
const el = serializer.serializeNode(piece.node);
|
|
758
|
+
el.classList.add('qm-review-add');
|
|
759
|
+
if (on)
|
|
760
|
+
el.classList.add('qm-review-lit');
|
|
761
|
+
el.contentEditable = 'false';
|
|
762
|
+
el.setAttribute('data-qm-review', item.id);
|
|
763
|
+
if (holds)
|
|
764
|
+
lastLine(el)?.append(chip(strings, act, item.id, false, stale));
|
|
765
|
+
return el;
|
|
766
|
+
}, {
|
|
767
|
+
side: ahead ? -1000 + n : 1 + n,
|
|
768
|
+
key: `add:${item.id}:${n}:${on}:${piece.node.toString()}`,
|
|
769
|
+
stopEvent: (e) => e.target?.closest?.('.qm-review-chip') != null
|
|
770
|
+
}));
|
|
771
|
+
});
|
|
772
|
+
if (!chipped)
|
|
773
|
+
row();
|
|
774
|
+
}
|
|
775
|
+
else if (last.node.isTextblock) {
|
|
776
|
+
decos.push(Decoration.widget(reach(last)[1], () => chip(strings, act, item.id, false, stale), {
|
|
777
|
+
side: 1,
|
|
778
|
+
key: `chip:${item.id}:${stale}`,
|
|
779
|
+
stopEvent: () => true
|
|
780
|
+
}));
|
|
781
|
+
}
|
|
782
|
+
else
|
|
783
|
+
row();
|
|
784
|
+
// What a proposal only adds to has the caret anywhere in the block it adds from.
|
|
785
|
+
spans.push({
|
|
786
|
+
id: item.id,
|
|
787
|
+
proposed: true,
|
|
788
|
+
adds: !covered.length,
|
|
789
|
+
from: reach(covered[0] ?? last)[0],
|
|
790
|
+
to: reach(last)[1]
|
|
791
|
+
});
|
|
792
|
+
}
|
|
793
|
+
return { decos, lighting, spans, plans, outdated, undrawn, accepted };
|
|
794
|
+
}
|
|
795
|
+
/** Whether a block's last leaf is a textblock, so its last line can carry a run. */
|
|
796
|
+
function endsInLine(node) {
|
|
797
|
+
let at = node;
|
|
798
|
+
while (!at.isTextblock && !at.isAtom && at.lastChild)
|
|
799
|
+
at = at.lastChild;
|
|
800
|
+
return at.isTextblock;
|
|
801
|
+
}
|
|
802
|
+
/** The item the caret is in: the one it was put into by its id while it holds the caret, else
|
|
803
|
+
* the one that holds it closest, so a change inside a paragraph a proposal covers is the change
|
|
804
|
+
* while the caret is in its words, and of a proposal that rewrites a paragraph and one that adds
|
|
805
|
+
* after it, the one that rewrites. `proposed` asks for a proposal alone. */
|
|
806
|
+
function spanAt(spans, pos, entered, proposed = false) {
|
|
807
|
+
let best;
|
|
808
|
+
for (const s of spans) {
|
|
809
|
+
if (s.from > pos || pos > s.to || (proposed && !s.proposed))
|
|
810
|
+
continue;
|
|
811
|
+
if (s.id === entered)
|
|
812
|
+
return s;
|
|
813
|
+
const size = s.to - s.from;
|
|
814
|
+
const held = best && best.to - best.from;
|
|
815
|
+
if (!best || size < held || (size === held && best.adds && !s.adds))
|
|
816
|
+
best = s;
|
|
817
|
+
}
|
|
818
|
+
return best;
|
|
819
|
+
}
|
|
820
|
+
/**
|
|
821
|
+
* The leaf's review layer. The decorations are a function of the document, the anchors and the
|
|
822
|
+
* channel's items, kept until one of them moves. The caret's report holds off while the reader
|
|
823
|
+
* types inside an item: an item is news on arriving in it, and noise under every keystroke.
|
|
824
|
+
*/
|
|
825
|
+
export function reviewLayer(channel) {
|
|
826
|
+
let memo;
|
|
827
|
+
/** The decorations of what is drawn, with the lit item they were made for. */
|
|
828
|
+
let shown;
|
|
829
|
+
const NOTHING = JSON.stringify({
|
|
830
|
+
ids: [],
|
|
831
|
+
outdated: [],
|
|
832
|
+
undrawn: [],
|
|
833
|
+
accepted: []
|
|
834
|
+
});
|
|
835
|
+
let reported = NOTHING;
|
|
836
|
+
let host;
|
|
837
|
+
const bases = new Map();
|
|
838
|
+
/** Answer an item. Taking a proposal writes it first, and one that can no longer be written
|
|
839
|
+
* stays as it is. A proposal taken by a press while the focus is elsewhere leaves the caret
|
|
840
|
+
* after what landed, in a focused leaf: the edit is the reader's, and so is its undo. */
|
|
841
|
+
const act = (id, action, how = {}) => {
|
|
842
|
+
const view = host;
|
|
843
|
+
if (!view || !drawn(view.state).spans.some((span) => span.id === id))
|
|
844
|
+
return false;
|
|
845
|
+
const wrote = reviewKey.getState(view.state).written.get(id);
|
|
846
|
+
if (wrote) {
|
|
847
|
+
if (action === 'accept' || action === 'dismiss')
|
|
848
|
+
return false;
|
|
849
|
+
view.dispatch(putBack(view.state, id, wrote, channel));
|
|
850
|
+
channel.act(id, 'reopen', how.chord);
|
|
851
|
+
return true;
|
|
852
|
+
}
|
|
853
|
+
if (action === 'accept') {
|
|
854
|
+
const plan = drawn(view.state).plans.get(id);
|
|
855
|
+
const took = plan && taking(view.state, plan, channel.anchors(view.state));
|
|
856
|
+
if (!took || !plan)
|
|
857
|
+
return false;
|
|
858
|
+
const { tr } = took;
|
|
859
|
+
const away = !how.quiet && !view.hasFocus();
|
|
860
|
+
if (away) {
|
|
861
|
+
const end = tr.mapping.map(Math.max(...plan.map((piece) => piece.to)), 1);
|
|
862
|
+
tr.setSelection(Selection.near(tr.doc.resolve(Math.min(end, tr.doc.content.size)), -1));
|
|
863
|
+
}
|
|
864
|
+
channel.writing?.(true);
|
|
865
|
+
try {
|
|
866
|
+
view.dispatch(tr.step(new ReviewAnswer(id, 'accept', took.written)));
|
|
867
|
+
}
|
|
868
|
+
finally {
|
|
869
|
+
channel.writing?.(false);
|
|
870
|
+
}
|
|
871
|
+
if (away)
|
|
872
|
+
view.focus();
|
|
873
|
+
}
|
|
874
|
+
else {
|
|
875
|
+
view.dispatch(view.state.tr
|
|
876
|
+
.setMeta(reviewKey, { answer: { id, to: 'dismiss' } })
|
|
877
|
+
.setMeta('addToHistory', false));
|
|
878
|
+
}
|
|
879
|
+
channel.act(id, action, how.chord);
|
|
880
|
+
return true;
|
|
881
|
+
};
|
|
882
|
+
const drawn = (state) => {
|
|
883
|
+
const review = reviewKey.getState(state);
|
|
884
|
+
const anchors = channel.anchors(state);
|
|
885
|
+
const items = channel.items();
|
|
886
|
+
const strings = channel.strings();
|
|
887
|
+
if (memo &&
|
|
888
|
+
memo.doc === state.doc &&
|
|
889
|
+
memo.review === review &&
|
|
890
|
+
memo.anchors === anchors &&
|
|
891
|
+
memo.items === items &&
|
|
892
|
+
memo.strings === strings)
|
|
893
|
+
return memo.drawn;
|
|
894
|
+
const next = draw(state, channel, act, bases);
|
|
895
|
+
memo = { doc: state.doc, review, anchors, items, strings, drawn: next };
|
|
896
|
+
return next;
|
|
897
|
+
};
|
|
898
|
+
/** Whether the lit item moving to `lit` changes how anything here reads. */
|
|
899
|
+
const relit = (lit) => !shown ||
|
|
900
|
+
(shown.lit !== lit &&
|
|
901
|
+
((shown.lit !== undefined && shown.drawn.lighting.has(shown.lit)) ||
|
|
902
|
+
(lit !== undefined && shown.drawn.lighting.has(lit))));
|
|
903
|
+
const decorations = (state) => {
|
|
904
|
+
const now = drawn(state);
|
|
905
|
+
const lit = channel.lit();
|
|
906
|
+
if (shown?.drawn === now && !relit(lit)) {
|
|
907
|
+
shown.lit = lit;
|
|
908
|
+
return shown.set;
|
|
909
|
+
}
|
|
910
|
+
const set = DecorationSet.create(state.doc, now.decos.map((d) => ('make' in d ? d.make(d.id === lit) : d)));
|
|
911
|
+
shown = { drawn: now, lit, set };
|
|
912
|
+
return set;
|
|
913
|
+
};
|
|
914
|
+
/** Tell the channel what the leaf draws, where that moved. */
|
|
915
|
+
const report = (state) => {
|
|
916
|
+
const { spans, outdated, undrawn, accepted } = drawn(state);
|
|
917
|
+
const next = { ids: spans.map((s) => s.id), outdated, undrawn, accepted };
|
|
918
|
+
const joined = JSON.stringify(next);
|
|
919
|
+
if (joined === reported)
|
|
920
|
+
return;
|
|
921
|
+
reported = joined;
|
|
922
|
+
channel.drew(next);
|
|
923
|
+
};
|
|
924
|
+
let hovered;
|
|
925
|
+
const hover = (id) => {
|
|
926
|
+
if (id === hovered)
|
|
927
|
+
return;
|
|
928
|
+
hovered = id;
|
|
929
|
+
channel.point(id, 'pointer');
|
|
930
|
+
};
|
|
931
|
+
const idAt = (target) => (target instanceof Element ? target.closest('[data-qm-review]') : null)?.getAttribute('data-qm-review') ?? undefined;
|
|
932
|
+
let caret;
|
|
933
|
+
/** The item the reader typed in, quiet until the caret has left it. */
|
|
934
|
+
let muted;
|
|
935
|
+
const follow = (view, typed) => {
|
|
936
|
+
const { selection } = view.state;
|
|
937
|
+
const at = view.hasFocus() && selection.empty
|
|
938
|
+
? spanAt(drawn(view.state).spans, selection.head, reviewKey.getState(view.state)?.entered)
|
|
939
|
+
?.id
|
|
940
|
+
: undefined;
|
|
941
|
+
if (at !== muted)
|
|
942
|
+
muted = undefined;
|
|
943
|
+
if (typed && at)
|
|
944
|
+
muted = at;
|
|
945
|
+
const next = at === muted ? undefined : at;
|
|
946
|
+
if (next === caret)
|
|
947
|
+
return;
|
|
948
|
+
caret = next;
|
|
949
|
+
channel.point(next, 'caret');
|
|
950
|
+
};
|
|
951
|
+
const answer = (action) => (state) => {
|
|
952
|
+
const span = spanAt(drawn(state).spans, state.selection.head, reviewKey.getState(state)?.entered, action === 'accept');
|
|
953
|
+
if (!span)
|
|
954
|
+
return false;
|
|
955
|
+
act(span.id, span.proposed ? action : 'reopen', { chord: true });
|
|
956
|
+
return true;
|
|
957
|
+
};
|
|
958
|
+
/** Escape lowers the card over the item the caret is in, before it is anything else's. */
|
|
959
|
+
const lower = (state) => {
|
|
960
|
+
const id = channel.shown();
|
|
961
|
+
if (id === undefined || !drawn(state).spans.some((span) => span.id === id))
|
|
962
|
+
return false;
|
|
963
|
+
channel.dismiss();
|
|
964
|
+
return true;
|
|
965
|
+
};
|
|
966
|
+
const plugin = new Plugin({
|
|
967
|
+
key: reviewKey,
|
|
968
|
+
state: {
|
|
969
|
+
init: () => ({
|
|
970
|
+
edits: 0,
|
|
971
|
+
answered: new Map(),
|
|
972
|
+
written: new Map(),
|
|
973
|
+
entered: undefined,
|
|
974
|
+
replayed: [],
|
|
975
|
+
renamed: new Map()
|
|
976
|
+
}),
|
|
977
|
+
apply: applyReview
|
|
978
|
+
},
|
|
979
|
+
props: {
|
|
980
|
+
decorations,
|
|
981
|
+
handleKeyDown: keydownHandler({
|
|
982
|
+
[REVIEW_KEYS.accept]: answer('accept'),
|
|
983
|
+
[REVIEW_KEYS.dismiss]: answer('dismiss'),
|
|
984
|
+
Escape: lower
|
|
985
|
+
}),
|
|
986
|
+
handleDOMEvents: {
|
|
987
|
+
mouseover: (_view, e) => {
|
|
988
|
+
hover(idAt(e.target));
|
|
989
|
+
return false;
|
|
990
|
+
},
|
|
991
|
+
mouseleave: () => {
|
|
992
|
+
hover(undefined);
|
|
993
|
+
return false;
|
|
994
|
+
},
|
|
995
|
+
focus: (view) => {
|
|
996
|
+
follow(view, false);
|
|
997
|
+
return false;
|
|
998
|
+
},
|
|
999
|
+
blur: (view) => {
|
|
1000
|
+
// The focus has not left yet when a blur fires.
|
|
1001
|
+
queueMicrotask(() => {
|
|
1002
|
+
if (!view.isDestroyed)
|
|
1003
|
+
follow(view, false);
|
|
1004
|
+
});
|
|
1005
|
+
return false;
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
},
|
|
1009
|
+
view: (view) => {
|
|
1010
|
+
host = view;
|
|
1011
|
+
report(view.state);
|
|
1012
|
+
return {
|
|
1013
|
+
update(view, prev) {
|
|
1014
|
+
const review = reviewKey.getState(view.state);
|
|
1015
|
+
if (review && review !== reviewKey.getState(prev))
|
|
1016
|
+
for (const { id, to } of review.replayed)
|
|
1017
|
+
channel.act(id, to ?? 'reopen');
|
|
1018
|
+
report(view.state);
|
|
1019
|
+
follow(view, reviewKey.getState(prev)?.edits !== reviewKey.getState(view.state)?.edits);
|
|
1020
|
+
},
|
|
1021
|
+
destroy() {
|
|
1022
|
+
if (hovered !== undefined)
|
|
1023
|
+
channel.point(undefined, 'pointer');
|
|
1024
|
+
if (caret !== undefined)
|
|
1025
|
+
channel.point(undefined, 'caret');
|
|
1026
|
+
if (reported !== NOTHING)
|
|
1027
|
+
channel.drew({ ids: [], outdated: [], undrawn: [], accepted: [] });
|
|
1028
|
+
host = undefined;
|
|
1029
|
+
}
|
|
1030
|
+
};
|
|
1031
|
+
}
|
|
1032
|
+
});
|
|
1033
|
+
return {
|
|
1034
|
+
plugin,
|
|
1035
|
+
refresh(view) {
|
|
1036
|
+
// Only the lit item moved, and nothing here reads it: the leaf is left alone.
|
|
1037
|
+
const lit = channel.lit();
|
|
1038
|
+
if (memo?.items === channel.items() && memo.strings === channel.strings() && !relit(lit)) {
|
|
1039
|
+
if (shown)
|
|
1040
|
+
shown.lit = lit;
|
|
1041
|
+
return;
|
|
1042
|
+
}
|
|
1043
|
+
// An answer is held until a list arrives without its item.
|
|
1044
|
+
const answered = reviewKey.getState(view.state)?.answered;
|
|
1045
|
+
const listed = new Set(channel.items().map((item) => item.id));
|
|
1046
|
+
const prune = [...(answered?.keys() ?? [])].filter((id) => !listed.has(id));
|
|
1047
|
+
if (!prune.length)
|
|
1048
|
+
return view.setProps({});
|
|
1049
|
+
view.dispatch(view.state.tr
|
|
1050
|
+
.setMeta(reviewKey, { prune })
|
|
1051
|
+
.setMeta('addToHistory', false));
|
|
1052
|
+
},
|
|
1053
|
+
elements(view, id) {
|
|
1054
|
+
return [...view.dom.querySelectorAll('[data-qm-review]')].filter((el) => el.getAttribute('data-qm-review') === id);
|
|
1055
|
+
},
|
|
1056
|
+
answer: (id, action) => act(id, action, { quiet: true }),
|
|
1057
|
+
reopen(view, id) {
|
|
1058
|
+
if (reviewKey.getState(view.state)?.answered.get(id) === 'dismiss')
|
|
1059
|
+
view.dispatch(view.state.tr
|
|
1060
|
+
.setMeta(reviewKey, { answer: { id, to: undefined } })
|
|
1061
|
+
.setMeta('addToHistory', false));
|
|
1062
|
+
channel.act(id, 'reopen');
|
|
1063
|
+
},
|
|
1064
|
+
undo: (view) => undo(view.state, view.dispatch),
|
|
1065
|
+
enter(view, id) {
|
|
1066
|
+
const span = drawn(view.state).spans.find((s) => s.id === id);
|
|
1067
|
+
if (!span)
|
|
1068
|
+
return false;
|
|
1069
|
+
const { doc, tr } = view.state;
|
|
1070
|
+
view.dispatch(tr
|
|
1071
|
+
.setSelection(Selection.near(doc.resolve(span.to), -1))
|
|
1072
|
+
.setMeta(reviewKey, { entered: id }));
|
|
1073
|
+
view.focus();
|
|
1074
|
+
return true;
|
|
1075
|
+
}
|
|
1076
|
+
};
|
|
1077
|
+
}
|