@quillmark/svelte 0.38.1 → 0.39.1
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 +2 -0
- package/dist/core/codec/encode.d.ts +10 -0
- package/dist/core/codec/encode.js +101 -0
- package/dist/core/codec/field.d.ts +1 -2
- package/dist/core/codec/field.js +51 -10
- package/dist/core/codec/index.d.ts +2 -1
- package/dist/core/codec/index.js +2 -1
- package/dist/core/codec/review-caret.d.ts +30 -0
- package/dist/core/codec/review-caret.js +195 -0
- package/dist/core/codec/review.d.ts +6 -4
- package/dist/core/codec/review.js +4 -60
- package/dist/core/codec/value-diff.d.ts +4 -1
- package/dist/core/codec/value-diff.js +16 -0
- package/dist/core/frame.d.ts +27 -0
- package/dist/core/frame.js +108 -0
- package/dist/core/theme.css +10 -0
- package/dist/visual/Field.svelte +1 -1
- package/dist/visual/ProseValue.svelte +11 -1
- package/dist/visual/ReviewCard.svelte +25 -23
- package/dist/visual/ReviewCard.svelte.d.ts +1 -0
- package/dist/visual/VisualEditorInner.svelte +68 -19
- package/dist/visual/index.d.ts +1 -1
- package/dist/visual/index.js +1 -1
- package/dist/visual/proposals.d.ts +9 -0
- package/dist/visual/proposals.js +71 -17
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Editor + live-preview components for [Quillmark](https://github.com/borb-sh/quillmark) WASM consumers. A WYSIWYG **VisualEditor**, and a canvas **Preview** that paints the compiled document and round-trips clicks to the editor, over one `@quillmark/wasm` session. Vanilla-TS cores with thin Svelte 5 wrappers.
|
|
4
4
|
|
|
5
|
+
Quillmark is pre-1.0. Its [stability statement](https://github.com/borb-sh/quillmark#status) says what any release can change and what every release keeps.
|
|
6
|
+
|
|
5
7
|
## Install
|
|
6
8
|
|
|
7
9
|
```sh
|
|
@@ -41,3 +41,13 @@ export declare function pmToContent(doc: PMNode, anchors?: ContentAnchor[]): Con
|
|
|
41
41
|
* not hold for placeholders (`field.ts`).
|
|
42
42
|
*/
|
|
43
43
|
export declare function lower(oldRt: Content, newRt: Content): Change | null;
|
|
44
|
+
/**
|
|
45
|
+
* The one splice taking `oldRt` to `newRt` where the two differ only in the text of one block
|
|
46
|
+
* and the formatting of what it inserts, or `null`: built here rather than by the store's `diff`,
|
|
47
|
+
* which reads both values whole across the boundary. Exact or absent, never a guess: the value the
|
|
48
|
+
* splice leaves, the store's marks cut at the edit and the insert's own merged back, is checked
|
|
49
|
+
* equal to `newRt`, so a caller that holds `newRt` as the stored value after the write holds it.
|
|
50
|
+
*/
|
|
51
|
+
export declare function spliceOf(oldRt: Content, newRt: Content): Change | null;
|
|
52
|
+
/** Whether `receipt`'s normalized change is `change`, a {@link spliceOf}, as it was sent. */
|
|
53
|
+
export declare function landedAsSent(applied: Change, change: Change): boolean;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { core } from '../lifecycle.js';
|
|
2
2
|
import { HARD_BREAK, ISLAND_SLOT, islandEntryFromNode } from './islands.js';
|
|
3
3
|
import { contentDescriptorFromPM, markKey } from './marks.js';
|
|
4
|
+
import { valueEqual } from './reconcile.js';
|
|
4
5
|
function idsOf(doc) {
|
|
5
6
|
const next = new Map();
|
|
6
7
|
doc.descendants((node) => {
|
|
@@ -244,3 +245,103 @@ export function lower(oldRt, newRt) {
|
|
|
244
245
|
const change = core().diff(oldRt, newRt);
|
|
245
246
|
return change.steps.length ? change : null;
|
|
246
247
|
}
|
|
248
|
+
/**
|
|
249
|
+
* The one splice taking `oldRt` to `newRt` where the two differ only in the text of one block
|
|
250
|
+
* and the formatting of what it inserts, or `null`: built here rather than by the store's `diff`,
|
|
251
|
+
* which reads both values whole across the boundary. Exact or absent, never a guess: the value the
|
|
252
|
+
* splice leaves, the store's marks cut at the edit and the insert's own merged back, is checked
|
|
253
|
+
* equal to `newRt`, so a caller that holds `newRt` as the stored value after the write holds it.
|
|
254
|
+
*/
|
|
255
|
+
export function spliceOf(oldRt, newRt) {
|
|
256
|
+
const a = oldRt.text;
|
|
257
|
+
const b = newRt.text;
|
|
258
|
+
if (a === b)
|
|
259
|
+
return null;
|
|
260
|
+
if (oldRt.codec !== newRt.codec ||
|
|
261
|
+
!valueEqual(oldRt.blocks, newRt.blocks) ||
|
|
262
|
+
!valueEqual(oldRt.containers ?? [], newRt.containers ?? []) ||
|
|
263
|
+
!valueEqual(oldRt.islands ?? [], newRt.islands ?? []))
|
|
264
|
+
return null;
|
|
265
|
+
const short = Math.min(a.length, b.length);
|
|
266
|
+
let at = 0;
|
|
267
|
+
while (at < short && a.charCodeAt(at) === b.charCodeAt(at))
|
|
268
|
+
at++;
|
|
269
|
+
let tail = 0;
|
|
270
|
+
while (tail < short - at &&
|
|
271
|
+
a.charCodeAt(a.length - 1 - tail) === b.charCodeAt(b.length - 1 - tail))
|
|
272
|
+
tail++;
|
|
273
|
+
if (splitsPair(a, at) || splitsPair(a, a.length - tail))
|
|
274
|
+
return null;
|
|
275
|
+
const del = a.slice(at, a.length - tail);
|
|
276
|
+
const insert = b.slice(at, b.length - tail);
|
|
277
|
+
if (breaksText(del) || breaksText(insert))
|
|
278
|
+
return null;
|
|
279
|
+
const end = at + del.length;
|
|
280
|
+
const shift = insert.length - del.length;
|
|
281
|
+
const before = oldRt.marks ?? [];
|
|
282
|
+
const after = newRt.marks ?? [];
|
|
283
|
+
if (before.some(isExtMark) || after.some(isExtMark))
|
|
284
|
+
return null;
|
|
285
|
+
const carried = [];
|
|
286
|
+
for (const m of before) {
|
|
287
|
+
if (m.start < at)
|
|
288
|
+
carried.push({ ...m, end: Math.min(m.end, at) });
|
|
289
|
+
if (m.end > end)
|
|
290
|
+
carried.push({ ...m, start: Math.max(m.start, end) + shift, end: m.end + shift });
|
|
291
|
+
}
|
|
292
|
+
const own = [];
|
|
293
|
+
for (const m of after) {
|
|
294
|
+
const lo = Math.max(m.start, at);
|
|
295
|
+
const hi = Math.min(m.end, at + insert.length);
|
|
296
|
+
if (lo < hi)
|
|
297
|
+
own.push({ ...m, start: lo, end: hi });
|
|
298
|
+
}
|
|
299
|
+
if (!valueEqual(mergeMarks([...carried, ...own]), after))
|
|
300
|
+
return null;
|
|
301
|
+
const anchors = [];
|
|
302
|
+
for (const x of oldRt.anchors ?? []) {
|
|
303
|
+
if ((x.start >= at && x.start <= end) || (x.end >= at && x.end <= end))
|
|
304
|
+
return null;
|
|
305
|
+
anchors.push(x.start > end
|
|
306
|
+
? { ...x, start: x.start + shift, end: x.end + shift }
|
|
307
|
+
: x.end > end
|
|
308
|
+
? { ...x, end: x.end + shift }
|
|
309
|
+
: x);
|
|
310
|
+
}
|
|
311
|
+
if (!valueEqual(anchors, newRt.anchors ?? []))
|
|
312
|
+
return null;
|
|
313
|
+
const marks = own.map((m) => ({ ...m, start: m.start - at, end: m.end - at }));
|
|
314
|
+
const step = { at, insert };
|
|
315
|
+
if (del)
|
|
316
|
+
step.delete = del;
|
|
317
|
+
if (marks.length)
|
|
318
|
+
step.marks = marks;
|
|
319
|
+
return { steps: [{ splice: step }] };
|
|
320
|
+
}
|
|
321
|
+
/** Whether `receipt`'s normalized change is `change`, a {@link spliceOf}, as it was sent. */
|
|
322
|
+
export function landedAsSent(applied, change) {
|
|
323
|
+
const [sent] = change.steps;
|
|
324
|
+
const [got] = applied.steps;
|
|
325
|
+
if (applied.steps.length !== 1 || !('splice' in got) || !('splice' in sent))
|
|
326
|
+
return false;
|
|
327
|
+
const s = sent.splice;
|
|
328
|
+
const g = got.splice;
|
|
329
|
+
return (g.at === s.at &&
|
|
330
|
+
(g.delete ?? '') === (s.delete ?? '') &&
|
|
331
|
+
g.insert === s.insert &&
|
|
332
|
+
valueEqual(g.marks ?? [], s.marks ?? []));
|
|
333
|
+
}
|
|
334
|
+
/** Whether offset `i` of `s` falls between the halves of a surrogate pair. */
|
|
335
|
+
function splitsPair(s, i) {
|
|
336
|
+
if (i <= 0 || i >= s.length)
|
|
337
|
+
return false;
|
|
338
|
+
const lo = s.charCodeAt(i);
|
|
339
|
+
return lo >= 0xdc00 && lo <= 0xdfff;
|
|
340
|
+
}
|
|
341
|
+
/** Whether `s` holds a character that ends a block or a line, or stands for an island. */
|
|
342
|
+
function breaksText(s) {
|
|
343
|
+
return s.includes('\n') || s.includes(HARD_BREAK) || s.includes(ISLAND_SLOT);
|
|
344
|
+
}
|
|
345
|
+
function isExtMark(m) {
|
|
346
|
+
return m.type.startsWith('x-');
|
|
347
|
+
}
|
|
@@ -96,8 +96,7 @@ export interface CreateFieldOpts {
|
|
|
96
96
|
onReview?(id: string, action: ReviewAction): void;
|
|
97
97
|
/** The leaf is writing a taken proposal, and is done: the commit between is the answer's. */
|
|
98
98
|
onReviewWriting?(on: boolean): void;
|
|
99
|
-
/** The review item
|
|
100
|
-
* while the reader types inside the item. */
|
|
99
|
+
/** The review item the caret names, `undefined` once it has left one (`review-caret.ts`). */
|
|
101
100
|
onReviewPoint?(id: string | undefined): void;
|
|
102
101
|
/** What the leaf draws of the review items, on each change: the ids drawn, and the items
|
|
103
102
|
* whose anchor the leaf holds that it does not draw, each with why. An item whose anchor
|
package/dist/core/codec/field.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
// The prose leaf: `createField` (INVARIANTS §Surface). One content field, one PM view wired to
|
|
2
2
|
// the WASM edit surface.
|
|
3
3
|
//
|
|
4
|
-
// `dispatchTransaction` applies the transaction optimistically, lowers it to a `Change` (
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
//
|
|
4
|
+
// `dispatchTransaction` applies the transaction optimistically, lowers it to a `Change` (one
|
|
5
|
+
// splice against the last write's hash for a text edit inside one block, or `replace` for a field
|
|
6
|
+
// not yet at content rest, which has nothing to change), commits via `doc.apply` (a `text` leaf's
|
|
7
|
+
// whole text through the typed writer), then fires `onChange` if it committed and `onCaretMove`.
|
|
8
|
+
// A throw keeps the optimistic PM state and reports through `onError`.
|
|
8
9
|
// An external change re-hydrates through `applyExternal`, gated by `reconcile` except where the
|
|
9
10
|
// hold it re-evaluates flips.
|
|
10
11
|
import { baseKeymap } from 'prosemirror-commands';
|
|
@@ -20,7 +21,7 @@ import { tooltips } from '../tooltip.js';
|
|
|
20
21
|
import { reportError, errorMessage } from '../errors.js';
|
|
21
22
|
import { decode, fitsLeaf } from './decode.js';
|
|
22
23
|
import { offsetToPM, pmToOffset, buildLineIndex, lineIndexOf, mapThroughDelta } from './positions.js';
|
|
23
|
-
import { lower, pmToContent, scanContent, scanDoc } from './encode.js';
|
|
24
|
+
import { landedAsSent, lower, pmToContent, scanContent, scanDoc, spliceOf } from './encode.js';
|
|
24
25
|
import { capPlugin } from './cap.js';
|
|
25
26
|
import { islandPastePlugin } from './islands.js';
|
|
26
27
|
import { anchorsFromContent } from './marks.js';
|
|
@@ -32,7 +33,8 @@ import { DEFAULT_LEAVE_STRINGS, leavePlugin } from './leave.js';
|
|
|
32
33
|
import { FIELD_KEYS, markKeymap } from './shortcuts.js';
|
|
33
34
|
import { remoteKey } from './remote.js';
|
|
34
35
|
import { renameBlocks, reviewLayer } from './review.js';
|
|
35
|
-
import { valueDiffPlugin } from './value-diff.js';
|
|
36
|
+
import { valueDiffPlugin, valueSpans } from './value-diff.js';
|
|
37
|
+
import { reviewCaretPlugin } from './review-caret.js';
|
|
36
38
|
import { blockSchema, isBlockSchema, isInlineSchema, isParagraphsSchema, leafSchema } from './schema.js';
|
|
37
39
|
import { linkPastePlugin } from './link-paste.js';
|
|
38
40
|
import { dragMovePlugin } from './drag-move.js';
|
|
@@ -251,6 +253,9 @@ export function createField(opts) {
|
|
|
251
253
|
// `known` is the codec's view of what the leaf holds: kept in sync after every
|
|
252
254
|
// own-edit so `reconcile` can tell an external change from the field's own.
|
|
253
255
|
const reconciler = createReconciler(shown());
|
|
256
|
+
/** The hash the leaf's own last write left on the field, while `reconciler.last` is that value:
|
|
257
|
+
* the base a splice commits against. */
|
|
258
|
+
let base;
|
|
254
259
|
let index; // rebuilt on every structural change
|
|
255
260
|
let view;
|
|
256
261
|
// The ghost's live cell: the ghost plugin reads it per pass, so moving it is an
|
|
@@ -284,11 +289,17 @@ export function createField(opts) {
|
|
|
284
289
|
},
|
|
285
290
|
act: (id, action) => opts.onReview?.(id, action),
|
|
286
291
|
writing: (on) => opts.onReviewWriting?.(on),
|
|
287
|
-
point: (id) => opts.onReviewPoint?.(id),
|
|
288
292
|
drew: (report) => opts.onReviewDrawn?.(report)
|
|
289
293
|
});
|
|
290
294
|
let valueProposals = [];
|
|
291
295
|
const valueDiff = valueDiffPlugin(() => valueProposals, (id, state) => opts.onValuePlanned?.(id, state));
|
|
296
|
+
const reviewCaret = reviewCaretPlugin({
|
|
297
|
+
spans: (state) => [...(review?.spans(state) ?? []), ...valueSpans(state, valueProposals)],
|
|
298
|
+
entered: (state) => review?.entered(state),
|
|
299
|
+
litHere: () => valueProposals.some((p) => p.lit) ||
|
|
300
|
+
(reviewLit !== undefined && reviewItems.some((item) => item.id === reviewLit)),
|
|
301
|
+
point: (id) => opts.onReviewPoint?.(id)
|
|
302
|
+
});
|
|
292
303
|
// The menu's report channel; the vocabulary is the codec's own constant (`slash.ts`).
|
|
293
304
|
const slash = block ? opts.onSlash : undefined;
|
|
294
305
|
// Judged of the value being mounted rather than read off a diagnostic: a routed set
|
|
@@ -414,7 +425,12 @@ export function createField(opts) {
|
|
|
414
425
|
placeholder: () => placeholderText,
|
|
415
426
|
placeholderUntilEdit: opts.placeholderUntilEdit,
|
|
416
427
|
cap: opts.cap,
|
|
417
|
-
afterHistory: [
|
|
428
|
+
afterHistory: [
|
|
429
|
+
anchorPlugin(seededAnchors),
|
|
430
|
+
...(review ? [review.plugin] : []),
|
|
431
|
+
valueDiff,
|
|
432
|
+
reviewCaret
|
|
433
|
+
]
|
|
418
434
|
})
|
|
419
435
|
});
|
|
420
436
|
return { state: built, index: seedIndex };
|
|
@@ -460,16 +476,19 @@ export function createField(opts) {
|
|
|
460
476
|
return commitUnset();
|
|
461
477
|
if (plaintext)
|
|
462
478
|
return commitPlain(literalOf(newRt));
|
|
479
|
+
if (commitSplice(at, oldRt, newRt))
|
|
480
|
+
return true;
|
|
481
|
+
base = undefined;
|
|
463
482
|
try {
|
|
464
483
|
// Only a field not yet at content rest replaces by choice: neither an absent declared
|
|
465
484
|
// field nor an authored string has anchors to lose (`changeCommittable`).
|
|
466
485
|
if (!changeCommittable(doc, addr))
|
|
467
|
-
doc.replace(at, newRt);
|
|
486
|
+
base = doc.replace(at, newRt).hash;
|
|
468
487
|
else {
|
|
469
488
|
const change = lower(oldRt, newRt);
|
|
470
489
|
if (change) {
|
|
471
490
|
const asked = askCore(oldRt, change);
|
|
472
|
-
doc.apply(at, change);
|
|
491
|
+
base = doc.apply(at, change).hash;
|
|
473
492
|
if (asked.size || change.steps.some((step) => 'setBlock' in step))
|
|
474
493
|
adopt();
|
|
475
494
|
}
|
|
@@ -504,6 +523,28 @@ export function createField(opts) {
|
|
|
504
523
|
}
|
|
505
524
|
}
|
|
506
525
|
}
|
|
526
|
+
/**
|
|
527
|
+
* A text edit inside one block, committed as the one splice it is and against the hash of the
|
|
528
|
+
* value the reconciler holds, so neither side is read or sent whole. A store written since that
|
|
529
|
+
* hash refuses the base, and the commit takes the diff path. Whether it landed.
|
|
530
|
+
*/
|
|
531
|
+
function commitSplice(at, oldRt, newRt) {
|
|
532
|
+
if (base === undefined)
|
|
533
|
+
return false;
|
|
534
|
+
const change = spliceOf(oldRt, newRt);
|
|
535
|
+
if (!change)
|
|
536
|
+
return false;
|
|
537
|
+
let receipt;
|
|
538
|
+
try {
|
|
539
|
+
receipt = doc.apply(at, change, { base });
|
|
540
|
+
}
|
|
541
|
+
catch {
|
|
542
|
+
return false;
|
|
543
|
+
}
|
|
544
|
+
base = receipt.hash;
|
|
545
|
+
reconciler.commit(landedAsSent(receipt.applied, change) ? newRt : readLeaf(reader, addr));
|
|
546
|
+
return true;
|
|
547
|
+
}
|
|
507
548
|
/**
|
|
508
549
|
* Each block id the projection minted for a block the store does not hold, swapped in
|
|
509
550
|
* `change` for a placeholder the write fills, so the store is the one minter of block ids:
|
|
@@ -13,7 +13,8 @@ export { DEFAULT_LEAVE_STRINGS } from './leave.js';
|
|
|
13
13
|
export type { LeaveStrings } from './leave.js';
|
|
14
14
|
export type { BlockPlace, LeafUndrawn, ReviewAction, ReviewReport, TextReview } from './review.js';
|
|
15
15
|
export { wordDiff } from './review-diff.js';
|
|
16
|
-
export { valueDiffPlugin } from './value-diff.js';
|
|
16
|
+
export { valueDiffPlugin, valueSpans } from './value-diff.js';
|
|
17
|
+
export { reviewCaretPlugin } from './review-caret.js';
|
|
17
18
|
export type { ValueProposal, ValueState } from './value-diff.js';
|
|
18
19
|
export { asContent, planValue, valueChanges } from './value-plan.js';
|
|
19
20
|
export type { ValuePlan, ValueChange } from './value-plan.js';
|
package/dist/core/codec/index.js
CHANGED
|
@@ -26,7 +26,8 @@ export { DEFAULT_SLASH_STRINGS } from './slash.js';
|
|
|
26
26
|
// the visual tier's `strings` set its wording.
|
|
27
27
|
export { DEFAULT_LEAVE_STRINGS } from './leave.js';
|
|
28
28
|
export { wordDiff } from './review-diff.js';
|
|
29
|
-
export { valueDiffPlugin } from './value-diff.js';
|
|
29
|
+
export { valueDiffPlugin, valueSpans } from './value-diff.js';
|
|
30
|
+
export { reviewCaretPlugin } from './review-caret.js';
|
|
30
31
|
export { asContent, planValue, valueChanges } from './value-plan.js';
|
|
31
32
|
// The rich editor's shortcuts as a host lists them, read off the tables the keymaps bind from;
|
|
32
33
|
// the visual tier's `strings` set extends their wording, and the formatting popover binds its
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Plugin, type EditorState } from 'prosemirror-state';
|
|
2
|
+
import type { Node as PMNode } from 'prosemirror-model';
|
|
3
|
+
/** A change the leaf draws, by the stretch of the document the caret names it from. */
|
|
4
|
+
export interface CaretSpan {
|
|
5
|
+
id: string;
|
|
6
|
+
from: number;
|
|
7
|
+
to: number;
|
|
8
|
+
/** It only adds after what it covers: a rewrite of the same stretch is named first. */
|
|
9
|
+
adds?: boolean;
|
|
10
|
+
}
|
|
11
|
+
/** `spans` widened to the whole lines they touch, so a caret anywhere on a changed line is in
|
|
12
|
+
* its change. */
|
|
13
|
+
export declare function widened(doc: PMNode, spans: readonly CaretSpan[]): CaretSpan[];
|
|
14
|
+
/** The change holding `pos`: the one the caret was put into by id while it stays, else the
|
|
15
|
+
* narrowest, a rewrite before what only adds. */
|
|
16
|
+
export declare function changeAt(spans: readonly CaretSpan[], pos: number, entered?: string): CaretSpan | undefined;
|
|
17
|
+
/** The change nearest `pos`: the fewest lines away, then a rewrite before what only adds, then
|
|
18
|
+
* the fewest positions away, then the one above. */
|
|
19
|
+
export declare function nearestChange(doc: PMNode, spans: readonly CaretSpan[], pos: number): CaretSpan | undefined;
|
|
20
|
+
export interface ReviewCaret {
|
|
21
|
+
/** The changes the leaf draws, by the stretches they stand on. */
|
|
22
|
+
spans(state: EditorState): readonly CaretSpan[];
|
|
23
|
+
/** The item the caret was put into by its id, while it stays where it was put. */
|
|
24
|
+
entered(state: EditorState): string | undefined;
|
|
25
|
+
/** Whether the current item is one the leaf draws. */
|
|
26
|
+
litHere(): boolean;
|
|
27
|
+
/** The item the caret names, `undefined` once it has left one. */
|
|
28
|
+
point(id: string | undefined): void;
|
|
29
|
+
}
|
|
30
|
+
export declare function reviewCaretPlugin(caret: ReviewCaret): Plugin<number>;
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
// Which review item a leaf's caret names. The reader arriving in the leaf, by a press or by
|
|
2
|
+
// the focus coming from elsewhere on the page, names the change on the caret's line, else the
|
|
3
|
+
// nearest change in the leaf, unless the current item is one of the leaf's already; the caret
|
|
4
|
+
// moving within the leaf names a change only on arriving at one. Typing names nothing: the item
|
|
5
|
+
// typed in stays quiet until the caret has left it.
|
|
6
|
+
import { Plugin, PluginKey } from 'prosemirror-state';
|
|
7
|
+
import { remoteKey } from './remote.js';
|
|
8
|
+
/** The lines of `doc`, in order: each textblock, cut at its hard breaks. */
|
|
9
|
+
function linesOf(doc) {
|
|
10
|
+
const lines = [];
|
|
11
|
+
doc.descendants((node, pos) => {
|
|
12
|
+
if (!node.isTextblock)
|
|
13
|
+
return true;
|
|
14
|
+
let from = pos + 1;
|
|
15
|
+
node.forEach((child, offset) => {
|
|
16
|
+
if (child.type.name !== 'hard_break')
|
|
17
|
+
return;
|
|
18
|
+
lines.push({ from, to: pos + 1 + offset });
|
|
19
|
+
from = pos + 1 + offset + child.nodeSize;
|
|
20
|
+
});
|
|
21
|
+
lines.push({ from, to: pos + node.nodeSize - 1 });
|
|
22
|
+
return false;
|
|
23
|
+
});
|
|
24
|
+
return lines;
|
|
25
|
+
}
|
|
26
|
+
/** The index of the line holding `pos`, the nearest before it between lines. */
|
|
27
|
+
function lineAt(lines, pos) {
|
|
28
|
+
let at = 0;
|
|
29
|
+
while (at + 1 < lines.length && lines[at + 1].from <= pos)
|
|
30
|
+
at++;
|
|
31
|
+
return at;
|
|
32
|
+
}
|
|
33
|
+
/** `spans` widened to the whole lines they touch, so a caret anywhere on a changed line is in
|
|
34
|
+
* its change. */
|
|
35
|
+
export function widened(doc, spans) {
|
|
36
|
+
const lines = linesOf(doc);
|
|
37
|
+
if (!lines.length)
|
|
38
|
+
return [...spans];
|
|
39
|
+
return spans.map((s) => ({
|
|
40
|
+
...s,
|
|
41
|
+
from: Math.min(s.from, lines[lineAt(lines, s.from)].from),
|
|
42
|
+
to: Math.max(s.to, lines[lineAt(lines, s.to)].to)
|
|
43
|
+
}));
|
|
44
|
+
}
|
|
45
|
+
/** The change holding `pos`: the one the caret was put into by id while it stays, else the
|
|
46
|
+
* narrowest, a rewrite before what only adds. */
|
|
47
|
+
export function changeAt(spans, pos, entered) {
|
|
48
|
+
let best;
|
|
49
|
+
for (const s of spans) {
|
|
50
|
+
if (s.from > pos || pos > s.to)
|
|
51
|
+
continue;
|
|
52
|
+
if (s.id === entered)
|
|
53
|
+
return s;
|
|
54
|
+
const size = s.to - s.from;
|
|
55
|
+
const held = best && best.to - best.from;
|
|
56
|
+
if (!best || size < held || (size === held && best.adds && !s.adds))
|
|
57
|
+
best = s;
|
|
58
|
+
}
|
|
59
|
+
return best;
|
|
60
|
+
}
|
|
61
|
+
/** The change nearest `pos`: the fewest lines away, then a rewrite before what only adds, then
|
|
62
|
+
* the fewest positions away, then the one above. */
|
|
63
|
+
export function nearestChange(doc, spans, pos) {
|
|
64
|
+
const lines = linesOf(doc);
|
|
65
|
+
const line = lineAt(lines, pos);
|
|
66
|
+
const rank = (s) => {
|
|
67
|
+
const first = lineAt(lines, s.from);
|
|
68
|
+
const last = lineAt(lines, s.to);
|
|
69
|
+
const away = line < first ? first - line : line > last ? line - last : 0;
|
|
70
|
+
const chars = pos < s.from ? s.from - pos : pos > s.to ? pos - s.to : 0;
|
|
71
|
+
return [away, s.adds ? 1 : 0, chars, s.from];
|
|
72
|
+
};
|
|
73
|
+
let best;
|
|
74
|
+
let bestRank = [];
|
|
75
|
+
for (const s of spans) {
|
|
76
|
+
const r = rank(s);
|
|
77
|
+
const i = r.findIndex((v, n) => v !== bestRank[n]);
|
|
78
|
+
if (!best || (i >= 0 && r[i] < bestRank[i])) {
|
|
79
|
+
best = s;
|
|
80
|
+
bestRank = r;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return best;
|
|
84
|
+
}
|
|
85
|
+
const caretKey = new PluginKey('qm-review-caret');
|
|
86
|
+
/** Whether a focus coming from `from` is the reader arriving in the leaf: from elsewhere on the
|
|
87
|
+
* page, not handed back from a surface floating over the text. */
|
|
88
|
+
const arriving = (from) => from instanceof Element && !from.closest('.qm-popover-surface');
|
|
89
|
+
export function reviewCaretPlugin(caret) {
|
|
90
|
+
/** The item last named from inside a change, `undefined` outside one. */
|
|
91
|
+
let named;
|
|
92
|
+
/** The item the reader typed in, quiet until the caret has left it. */
|
|
93
|
+
let muted;
|
|
94
|
+
/** A press is down in the leaf: the caret it places names once it is up. */
|
|
95
|
+
let pressing = false;
|
|
96
|
+
const spansOf = (state) => widened(state.doc, caret.spans(state));
|
|
97
|
+
/** The caret moved, or the reader typed: an arrival at a change names it. */
|
|
98
|
+
const follow = (view, typed) => {
|
|
99
|
+
const { selection } = view.state;
|
|
100
|
+
const at = view.hasFocus() && selection.empty
|
|
101
|
+
? changeAt(spansOf(view.state), selection.head, caret.entered(view.state))?.id
|
|
102
|
+
: undefined;
|
|
103
|
+
if (at !== muted)
|
|
104
|
+
muted = undefined;
|
|
105
|
+
if (typed && at)
|
|
106
|
+
muted = at;
|
|
107
|
+
const next = at === muted ? undefined : at;
|
|
108
|
+
if (next === named)
|
|
109
|
+
return;
|
|
110
|
+
named = next;
|
|
111
|
+
caret.point(next);
|
|
112
|
+
};
|
|
113
|
+
/** The reader arrived in the leaf: the change on the caret's line, else, the current item
|
|
114
|
+
* being another leaf's, the nearest change here. */
|
|
115
|
+
const arrive = (view) => {
|
|
116
|
+
if (view.isDestroyed || !view.hasFocus())
|
|
117
|
+
return;
|
|
118
|
+
const { state } = view;
|
|
119
|
+
const spans = spansOf(state);
|
|
120
|
+
const head = state.selection.head;
|
|
121
|
+
const at = changeAt(spans, head, caret.entered(state));
|
|
122
|
+
muted = undefined;
|
|
123
|
+
if (at) {
|
|
124
|
+
named = at.id;
|
|
125
|
+
caret.point(at.id);
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
named = undefined;
|
|
129
|
+
if (caret.litHere())
|
|
130
|
+
return;
|
|
131
|
+
const near = nearestChange(state.doc, spans, head);
|
|
132
|
+
if (near)
|
|
133
|
+
caret.point(near.id);
|
|
134
|
+
};
|
|
135
|
+
/** Hold the caret's report until the press at `view` is up, then arrive, or not where it
|
|
136
|
+
* was cancelled or the press was on a change, which names that change itself. */
|
|
137
|
+
const press = (view, onChange) => {
|
|
138
|
+
pressing = true;
|
|
139
|
+
const end = (event) => {
|
|
140
|
+
window.removeEventListener('pointerup', end, true);
|
|
141
|
+
window.removeEventListener('pointercancel', end, true);
|
|
142
|
+
pressing = false;
|
|
143
|
+
if (event.type === 'pointerup' && !onChange)
|
|
144
|
+
setTimeout(() => arrive(view));
|
|
145
|
+
};
|
|
146
|
+
window.addEventListener('pointerup', end, true);
|
|
147
|
+
window.addEventListener('pointercancel', end, true);
|
|
148
|
+
};
|
|
149
|
+
return new Plugin({
|
|
150
|
+
key: caretKey,
|
|
151
|
+
state: {
|
|
152
|
+
init: () => 0,
|
|
153
|
+
apply: (tr, edits) => (tr.docChanged && !tr.getMeta(remoteKey) ? edits + 1 : edits)
|
|
154
|
+
},
|
|
155
|
+
props: {
|
|
156
|
+
handleDOMEvents: {
|
|
157
|
+
pointerdown: (view, event) => {
|
|
158
|
+
if (event.button !== 0 || pressing)
|
|
159
|
+
return false;
|
|
160
|
+
const target = event.target instanceof Element ? event.target : null;
|
|
161
|
+
press(view, !!target?.closest('[data-qm-review]'));
|
|
162
|
+
return false;
|
|
163
|
+
},
|
|
164
|
+
focus: (view, event) => {
|
|
165
|
+
if (pressing)
|
|
166
|
+
return false;
|
|
167
|
+
if (arriving(event.relatedTarget))
|
|
168
|
+
arrive(view);
|
|
169
|
+
else
|
|
170
|
+
follow(view, false);
|
|
171
|
+
return false;
|
|
172
|
+
},
|
|
173
|
+
blur: (view) => {
|
|
174
|
+
// The focus has not left yet when a blur fires.
|
|
175
|
+
queueMicrotask(() => {
|
|
176
|
+
if (!view.isDestroyed)
|
|
177
|
+
follow(view, false);
|
|
178
|
+
});
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
},
|
|
183
|
+
view: () => ({
|
|
184
|
+
update(view, prev) {
|
|
185
|
+
if (pressing)
|
|
186
|
+
return;
|
|
187
|
+
follow(view, caretKey.getState(prev) !== caretKey.getState(view.state));
|
|
188
|
+
},
|
|
189
|
+
destroy() {
|
|
190
|
+
if (named !== undefined)
|
|
191
|
+
caret.point(undefined);
|
|
192
|
+
}
|
|
193
|
+
})
|
|
194
|
+
});
|
|
195
|
+
}
|
|
@@ -2,6 +2,7 @@ import { type Node as PMNode } from 'prosemirror-model';
|
|
|
2
2
|
import { Plugin, type EditorState, type Transaction } from 'prosemirror-state';
|
|
3
3
|
import { Mapping } from 'prosemirror-transform';
|
|
4
4
|
import { type EditorView } from 'prosemirror-view';
|
|
5
|
+
import type { CaretSpan } from './review-caret.js';
|
|
5
6
|
/**
|
|
6
7
|
* One proposal over a prose leaf, by the blocks that place it.
|
|
7
8
|
*
|
|
@@ -50,8 +51,6 @@ export interface ReviewChannel {
|
|
|
50
51
|
act(id: string, action: ReviewAction): void;
|
|
51
52
|
/** The leaf is writing a taken proposal, and is done. */
|
|
52
53
|
writing?(on: boolean): void;
|
|
53
|
-
/** The item the caret is in, `undefined` once it has left. */
|
|
54
|
-
point(id: string | undefined): void;
|
|
55
54
|
/** What the leaf draws and what it holds and does not draw, on each change of either. */
|
|
56
55
|
drew(report: ReviewReport): void;
|
|
57
56
|
}
|
|
@@ -99,6 +98,10 @@ export interface ReviewLayer {
|
|
|
99
98
|
refresh(view: EditorView): void;
|
|
100
99
|
/** The elements item `id` draws, in document order. */
|
|
101
100
|
elements(view: EditorView, id: string): HTMLElement[];
|
|
101
|
+
/** What each item drawn stands on, where the caret names it from (`review-caret.ts`). */
|
|
102
|
+
spans(state: EditorState): readonly CaretSpan[];
|
|
103
|
+
/** The item the caret was put into by its id, while it stays where it was put. */
|
|
104
|
+
entered(state: EditorState): string | undefined;
|
|
102
105
|
/** Put the caret in item `id` and take the focus. False for an item the leaf does not
|
|
103
106
|
* draw. */
|
|
104
107
|
enter(view: EditorView, id: string): boolean;
|
|
@@ -116,7 +119,6 @@ export interface ReviewLayer {
|
|
|
116
119
|
}
|
|
117
120
|
/**
|
|
118
121
|
* The leaf's review layer. The decorations are a function of the document and the channel's
|
|
119
|
-
* items, kept until one of them moves.
|
|
120
|
-
* types inside an item: an item is news on arriving in it, and noise under every keystroke.
|
|
122
|
+
* items, kept until one of them moves.
|
|
121
123
|
*/
|
|
122
124
|
export declare function reviewLayer(channel: ReviewChannel): ReviewLayer;
|