@quillmark/svelte 0.38.0 → 0.39.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 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
@@ -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 holding the caret, `undefined` once it has left. The report holds off
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
@@ -32,7 +32,8 @@ import { DEFAULT_LEAVE_STRINGS, leavePlugin } from './leave.js';
32
32
  import { FIELD_KEYS, markKeymap } from './shortcuts.js';
33
33
  import { remoteKey } from './remote.js';
34
34
  import { renameBlocks, reviewLayer } from './review.js';
35
- import { valueDiffPlugin } from './value-diff.js';
35
+ import { valueDiffPlugin, valueSpans } from './value-diff.js';
36
+ import { reviewCaretPlugin } from './review-caret.js';
36
37
  import { blockSchema, isBlockSchema, isInlineSchema, isParagraphsSchema, leafSchema } from './schema.js';
37
38
  import { linkPastePlugin } from './link-paste.js';
38
39
  import { dragMovePlugin } from './drag-move.js';
@@ -284,11 +285,17 @@ export function createField(opts) {
284
285
  },
285
286
  act: (id, action) => opts.onReview?.(id, action),
286
287
  writing: (on) => opts.onReviewWriting?.(on),
287
- point: (id) => opts.onReviewPoint?.(id),
288
288
  drew: (report) => opts.onReviewDrawn?.(report)
289
289
  });
290
290
  let valueProposals = [];
291
291
  const valueDiff = valueDiffPlugin(() => valueProposals, (id, state) => opts.onValuePlanned?.(id, state));
292
+ const reviewCaret = reviewCaretPlugin({
293
+ spans: (state) => [...(review?.spans(state) ?? []), ...valueSpans(state, valueProposals)],
294
+ entered: (state) => review?.entered(state),
295
+ litHere: () => valueProposals.some((p) => p.lit) ||
296
+ (reviewLit !== undefined && reviewItems.some((item) => item.id === reviewLit)),
297
+ point: (id) => opts.onReviewPoint?.(id)
298
+ });
292
299
  // The menu's report channel; the vocabulary is the codec's own constant (`slash.ts`).
293
300
  const slash = block ? opts.onSlash : undefined;
294
301
  // Judged of the value being mounted rather than read off a diagnostic: a routed set
@@ -414,7 +421,12 @@ export function createField(opts) {
414
421
  placeholder: () => placeholderText,
415
422
  placeholderUntilEdit: opts.placeholderUntilEdit,
416
423
  cap: opts.cap,
417
- afterHistory: [anchorPlugin(seededAnchors), ...(review ? [review.plugin] : []), valueDiff]
424
+ afterHistory: [
425
+ anchorPlugin(seededAnchors),
426
+ ...(review ? [review.plugin] : []),
427
+ valueDiff,
428
+ reviewCaret
429
+ ]
418
430
  })
419
431
  });
420
432
  return { state: built, index: seedIndex };
@@ -470,7 +482,7 @@ export function createField(opts) {
470
482
  if (change) {
471
483
  const asked = askCore(oldRt, change);
472
484
  doc.apply(at, change);
473
- if (asked.size)
485
+ if (asked.size || change.steps.some((step) => 'setBlock' in step))
474
486
  adopt();
475
487
  }
476
488
  }
@@ -553,8 +565,12 @@ export function createField(opts) {
553
565
  if (n.id === stored[i])
554
566
  return;
555
567
  tr.setNodeAttribute(n.pos, n.key, stored[i]);
556
- // A split's copy of an id the first half keeps is no rename.
557
- if (typeof n.id === 'string' && !stored.includes(n.id))
568
+ if (typeof n.id !== 'string')
569
+ return;
570
+ // A split's copy of an id the first half keeps is no rename. A block written above the
571
+ // first takes the first's id, which leaves the block that held it.
572
+ const kept = stored.indexOf(n.id);
573
+ if (kept < 0 || nodes[kept].id !== n.id)
558
574
  renamed.push([n.id, stored[i]]);
559
575
  });
560
576
  renameBlocks(tr, renamed);
@@ -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
  *
@@ -9,7 +10,8 @@ import { type EditorView } from 'prosemirror-view';
9
10
  * proposes their removal. Its blocks pair with the paragraphs in order, each drawn as an edit of the one
10
11
  * it pairs with while the two are one kind of block; past the first that is not, the paragraphs
11
12
  * left are struck whole and the blocks left are added after the outermost block holding the last
12
- * paragraph, or as the next items of its list. One that names where it adds, by `insert`, proposes new blocks the same way after the block it names, or before the leaf's
13
+ * paragraph, or, where they open a list, as items at the depth the store spells them: a block of a
14
+ * nested list indented two spaces for each list around its own. One that names where it adds, by `insert`, proposes new blocks the same way after the block it names, or before the leaf's
13
15
  * first; in a leaf that holds one empty paragraph it proposes that paragraph's text. A paragraph
14
16
  * takes one proposal that rewrites it and one that adds after it, and a later item of either kind
15
17
  * naming it draws nothing.
@@ -49,8 +51,6 @@ export interface ReviewChannel {
49
51
  act(id: string, action: ReviewAction): void;
50
52
  /** The leaf is writing a taken proposal, and is done. */
51
53
  writing?(on: boolean): void;
52
- /** The item the caret is in, `undefined` once it has left. */
53
- point(id: string | undefined): void;
54
54
  /** What the leaf draws and what it holds and does not draw, on each change of either. */
55
55
  drew(report: ReviewReport): void;
56
56
  }
@@ -98,6 +98,10 @@ export interface ReviewLayer {
98
98
  refresh(view: EditorView): void;
99
99
  /** The elements item `id` draws, in document order. */
100
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;
101
105
  /** Put the caret in item `id` and take the focus. False for an item the leaf does not
102
106
  * draw. */
103
107
  enter(view: EditorView, id: string): boolean;
@@ -115,7 +119,6 @@ export interface ReviewLayer {
115
119
  }
116
120
  /**
117
121
  * The leaf's review layer. The decorations are a function of the document and the channel's
118
- * items, kept until one of them moves. The caret's report holds off while the reader
119
- * 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.
120
123
  */
121
124
  export declare function reviewLayer(channel: ReviewChannel): ReviewLayer;
@@ -21,7 +21,6 @@ import { Plugin, PluginKey, Selection } from 'prosemirror-state';
21
21
  import { Mapping, Step, StepResult } from 'prosemirror-transform';
22
22
  import { Decoration, DecorationSet } from 'prosemirror-view';
23
23
  import { arriving, HARD_BREAK, ISLAND_SLOT } from './islands.js';
24
- import { remoteKey } from './remote.js';
25
24
  import { wordDiff } from './review-diff.js';
26
25
  const reviewKey = new PluginKey('quill-review');
27
26
  /** Tell the review layer the store took blocks under new ids, each `[was, now]`. */
@@ -78,8 +77,6 @@ function answering(state, id, to) {
78
77
  }
79
78
  function applyReview(tr, state) {
80
79
  let next = state.replayed.length ? { ...state, replayed: [] } : state;
81
- if (tr.docChanged && !tr.getMeta(remoteKey))
82
- next = { ...next, edits: next.edits + 1 };
83
80
  const history = isHistoryTransaction(tr);
84
81
  tr.steps.forEach((step) => {
85
82
  if (!(step instanceof ReviewAnswer))
@@ -423,6 +420,25 @@ function placeOf(item, leaves, now) {
423
420
  return { cover: [beside], beside, ahead: false, missing: false };
424
421
  return { cover: [], beside, ahead, missing: false };
425
422
  }
423
+ /** A list marker, after the spaces that open a line. */
424
+ const MARKER = /^ *(?:[-*+]|\d{1,9}[.)])(?: |$)/;
425
+ /**
426
+ * Markdown as the store spells a block of a nested list, read back: each line indented two
427
+ * spaces for every list around the block's own, which a parse alone reads as a sibling or as
428
+ * code. `markdown` with that indent taken off, and `nest`, the lists it stood in past the first.
429
+ */
430
+ function unnest(markdown) {
431
+ const first = markdown.match(/^[ \n]*/)[0];
432
+ const spaces = first.length - first.lastIndexOf('\n') - 1;
433
+ const nest = Math.floor(spaces / 2);
434
+ if (!nest || !MARKER.test(markdown.slice(first.length - spaces)))
435
+ return { markdown, nest: 0 };
436
+ const indent = ' '.repeat(2 * nest);
437
+ const lines = markdown
438
+ .split('\n')
439
+ .map((l) => (l.startsWith(indent) ? l.slice(indent.length) : l));
440
+ return { markdown: lines.join('\n'), nest };
441
+ }
426
442
  /** The blocks a proposal's writer read, by the id each was read under; `undefined` where
427
443
  * `before` does not read as one block for each id. */
428
444
  const reads = new WeakMap();
@@ -432,7 +448,8 @@ function readOf(ids, before, channel) {
432
448
  reads.set(ids, (row = new Map()));
433
449
  if (row.has(before))
434
450
  return row.get(before);
435
- const doc = before.trim() ? channel.parse(before) : undefined;
451
+ const read = unnest(before).markdown;
452
+ const doc = read.trim() ? channel.parse(read) : undefined;
436
453
  const blocks = doc ? leavesOf(doc) : [];
437
454
  const out = blocks.length === ids.length ? new Map(ids.map((id, k) => [id, blocks[k].node])) : undefined;
438
455
  row.set(before, out);
@@ -489,7 +506,8 @@ function draw(state, channel, bases) {
489
506
  undrawn.push({ id: item.id, reason: 'taken' });
490
507
  continue;
491
508
  }
492
- const proposed = item.after.trim() ? channel.parse(item.after) : undefined;
509
+ const { markdown: after, nest } = unnest(item.after);
510
+ const proposed = after.trim() ? channel.parse(after) : undefined;
493
511
  if (item.after.trim() && !proposed) {
494
512
  undrawn.push({ id: item.id, reason: 'parse' });
495
513
  continue;
@@ -592,16 +610,13 @@ function draw(state, channel, bases) {
592
610
  /** Past the outermost block holding the last one, or the head of the leaf. */
593
611
  const outer = ahead ? 0 : $last.depth ? $last.after(1) : last.pos + last.node.nodeSize;
594
612
  if (added.length) {
595
- // A proposal that continues the list its paragraph is in adds items to that list.
596
- const listed = !ahead &&
597
- $last.parent.type.name === 'list_item' &&
598
- added[0].type === $last.node($last.depth - 1).type;
599
613
  const pieces = [];
600
- if (listed) {
601
- const at = $last.after($last.depth);
602
- added[0].forEach((li) => pieces.push({ at, node: li }));
603
- }
604
- for (const node of listed ? added.slice(1) : added)
614
+ const into = ahead ? undefined : listPlace($last, last, added[0], nest);
615
+ if (into?.whole)
616
+ pieces.push({ at: into.at, node: added[0] });
617
+ else if (into)
618
+ added[0].forEach((li) => pieces.push({ at: into.at, node: li }));
619
+ for (const node of into ? added.slice(1) : added)
605
620
  pieces.push({ at: outer, node });
606
621
  for (const piece of pieces) {
607
622
  const held = plan.at(-1);
@@ -636,27 +651,36 @@ function draw(state, channel, bases) {
636
651
  }
637
652
  return { decos, lighting, spans, plans, outdated, undrawn };
638
653
  }
639
- /** The item the caret is in: the one it was put into by its id while it holds the caret, else
640
- * the one that holds it closest, so of a proposal that rewrites a paragraph and one that adds
641
- * after it, the one that rewrites. */
642
- function spanAt(spans, pos, entered) {
643
- let best;
644
- for (const s of spans) {
645
- if (s.from > pos || pos > s.to)
646
- continue;
647
- if (s.id === entered)
648
- return s;
649
- const size = s.to - s.from;
650
- const held = best && best.to - best.from;
651
- if (!best || size < held || (size === held && best.adds && !s.adds))
652
- best = s;
654
+ /**
655
+ * Where a list a proposal adds after `last` goes, at the depth it was written at: the list it
656
+ * opens with stood in `nest` lists past the first. At the depth of a list `last` is in, its items
657
+ * follow the item of that list holding `last`; one deeper, they open the list under `last` in its
658
+ * item, or are that list, `whole`, where the item holds none. `undefined` where the list is not
659
+ * the kind of the one it would join, or `last` stands in no list.
660
+ */
661
+ function listPlace($last, last, list, nest) {
662
+ const items = [];
663
+ for (let d = 1; d <= $last.depth; d++)
664
+ if ($last.node(d).type.name === 'list_item')
665
+ items.push(d);
666
+ if (!items.length || !/_list$/.test(list.type.name))
667
+ return undefined;
668
+ const level = Math.min(nest, items.length);
669
+ if (level === items.length) {
670
+ if ($last.parent.type.name !== 'list_item')
671
+ return undefined;
672
+ const end = last.pos + last.node.nodeSize;
673
+ const under = $last.doc.resolve(end).nodeAfter;
674
+ if (under?.type === list.type)
675
+ return { at: end + 1 };
676
+ return { at: end, whole: true };
653
677
  }
654
- return best;
678
+ const item = items[level];
679
+ return $last.node(item - 1).type === list.type ? { at: $last.after(item) } : undefined;
655
680
  }
656
681
  /**
657
682
  * The leaf's review layer. The decorations are a function of the document and the channel's
658
- * items, kept until one of them moves. The caret's report holds off while the reader
659
- * types inside an item: an item is news on arriving in it, and noise under every keystroke.
683
+ * items, kept until one of them moves.
660
684
  */
661
685
  export function reviewLayer(channel) {
662
686
  let memo;
@@ -730,30 +754,10 @@ export function reviewLayer(channel) {
730
754
  reported = joined;
731
755
  channel.drew(next);
732
756
  };
733
- let caret;
734
- /** The item the reader typed in, quiet until the caret has left it. */
735
- let muted;
736
- const follow = (view, typed) => {
737
- const { selection } = view.state;
738
- const at = view.hasFocus() && selection.empty
739
- ? spanAt(drawn(view.state).spans, selection.head, reviewKey.getState(view.state)?.entered)
740
- ?.id
741
- : undefined;
742
- if (at !== muted)
743
- muted = undefined;
744
- if (typed && at)
745
- muted = at;
746
- const next = at === muted ? undefined : at;
747
- if (next === caret)
748
- return;
749
- caret = next;
750
- channel.point(next);
751
- };
752
757
  const plugin = new Plugin({
753
758
  key: reviewKey,
754
759
  state: {
755
760
  init: () => ({
756
- edits: 0,
757
761
  answered: new Map(),
758
762
  entered: undefined,
759
763
  replayed: [],
@@ -762,21 +766,7 @@ export function reviewLayer(channel) {
762
766
  apply: applyReview
763
767
  },
764
768
  props: {
765
- decorations,
766
- handleDOMEvents: {
767
- focus: (view) => {
768
- follow(view, false);
769
- return false;
770
- },
771
- blur: (view) => {
772
- // The focus has not left yet when a blur fires.
773
- queueMicrotask(() => {
774
- if (!view.isDestroyed)
775
- follow(view, false);
776
- });
777
- return false;
778
- }
779
- }
769
+ decorations
780
770
  },
781
771
  view: (view) => {
782
772
  host = view;
@@ -788,11 +778,8 @@ export function reviewLayer(channel) {
788
778
  for (const { id, to } of review.replayed)
789
779
  channel.act(id, to ?? 'reopen');
790
780
  report(view.state);
791
- follow(view, reviewKey.getState(prev)?.edits !== reviewKey.getState(view.state)?.edits);
792
781
  },
793
782
  destroy() {
794
- if (caret !== undefined)
795
- channel.point(undefined);
796
783
  if (reported !== NOTHING)
797
784
  channel.drew({ ids: [], outdated: [], undrawn: [] });
798
785
  host = undefined;
@@ -833,6 +820,8 @@ export function reviewLayer(channel) {
833
820
  },
834
821
  undo: (view) => undo(view.state, view.dispatch),
835
822
  project: (state) => projecting(state, drawn(state), (at) => at === state ? drawn(state) : draw(at, channel, new Map(bases))),
823
+ spans: (state) => drawn(state).spans,
824
+ entered: (state) => reviewKey.getState(state)?.entered,
836
825
  enter(view, id) {
837
826
  const span = drawn(view.state).spans.find((s) => s.id === id);
838
827
  if (!span)
@@ -1,5 +1,6 @@
1
- import { Plugin, PluginKey } from 'prosemirror-state';
1
+ import { Plugin, PluginKey, type EditorState } from 'prosemirror-state';
2
2
  import type { Content } from '@quillmark/wasm';
3
+ import type { CaretSpan } from './review-caret.js';
3
4
  /** A field's proposed value as the leaf draws it. */
4
5
  export interface ValueProposal {
5
6
  id: string;
@@ -14,6 +15,8 @@ export interface ValueProposal {
14
15
  * reader's writing, or already read as proposed, so its Accept would write nothing. */
15
16
  export type ValueState = 'open' | 'outdated' | 'applied';
16
17
  export declare const valueDiffKey: PluginKey<void>;
18
+ /** What each proposal that changes the leaf stands on: from its first change to its last. */
19
+ export declare function valueSpans(state: EditorState, proposals: readonly ValueProposal[]): CaretSpan[];
17
20
  /**
18
21
  * The plugin, reading the proposals live: a caller that moves them redraws with `setProps`.
19
22
  * Each is a run of one answer or a whole value, drawn apart. `planned` hears, on each draw, how
@@ -38,6 +38,22 @@ function draw(state, proposals, planned) {
38
38
  const at = (offset) => offsetToPM(index, offset);
39
39
  return DecorationSet.create(state.doc, proposals.flatMap((p) => decorations(state, live, at, p, planned)));
40
40
  }
41
+ /** What each proposal that changes the leaf stands on: from its first change to its last. */
42
+ export function valueSpans(state, proposals) {
43
+ if (!proposals.length)
44
+ return [];
45
+ const scan = scanDoc(state.doc);
46
+ const live = scanContent(scan);
47
+ const index = lineIndexOf(scan);
48
+ return proposals.flatMap((p) => {
49
+ const changes = valueChanges(live, planValue(p.before, live, p.after).merged);
50
+ if (!changes.length)
51
+ return [];
52
+ const from = offsetToPM(index, Math.min(...changes.map((c) => c.at)));
53
+ const to = offsetToPM(index, Math.max(...changes.map((c) => c.end)));
54
+ return [{ id: p.id, from, to }];
55
+ });
56
+ }
41
57
  /** One proposal's marks over the leaf as it reads, `live`. */
42
58
  function decorations(state, live, at, p, planned) {
43
59
  const { merged, outdated } = planValue(p.before, live, p.after);
@@ -0,0 +1,27 @@
1
+ /** A vertical extent in viewport px. */
2
+ export interface Band {
3
+ top: number;
4
+ bottom: number;
5
+ }
6
+ export interface FrameOptions {
7
+ /** `nearest` moves the least that frames the target; `center` centres it where it fits. */
8
+ block: 'nearest' | 'center';
9
+ /** Room held clear at the band's foot, where it is more than the scroller's own `scroll-padding`. */
10
+ foot?: number;
11
+ behavior?: ScrollBehavior;
12
+ }
13
+ /**
14
+ * The scroll, in px down, that frames `target` in `band` with `gap` clear of each edge. `top` is
15
+ * where the field's label starts: kept in the band whenever it fits with the target, the target
16
+ * winning where it does not. Zero for a target already framed, so a landing on the field in view
17
+ * moves nothing.
18
+ */
19
+ export declare function frameDelta(band: Band, target: Band, top: number, gap: number, block: FrameOptions['block']): number;
20
+ /**
21
+ * Scroll `target` into the band its scroller shows, with the label of the field it sits in where
22
+ * the two fit (`frameDelta`). One scroller moves, the nearest: a host page around the editor stays
23
+ * where it is.
24
+ */
25
+ export declare function frame(target: HTMLElement, opts: FrameOptions): void;
26
+ /** The top a landing frames for `els`, its field's label included where it fits in `band`. */
27
+ export declare function framedTop(els: readonly HTMLElement[], band: Band): number | undefined;