@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.
@@ -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))
@@ -681,27 +678,9 @@ function listPlace($last, last, list, nest) {
681
678
  const item = items[level];
682
679
  return $last.node(item - 1).type === list.type ? { at: $last.after(item) } : undefined;
683
680
  }
684
- /** The item the caret is in: the one it was put into by its id while it holds the caret, else
685
- * the one that holds it closest, so of a proposal that rewrites a paragraph and one that adds
686
- * after it, the one that rewrites. */
687
- function spanAt(spans, pos, entered) {
688
- let best;
689
- for (const s of spans) {
690
- if (s.from > pos || pos > s.to)
691
- continue;
692
- if (s.id === entered)
693
- return s;
694
- const size = s.to - s.from;
695
- const held = best && best.to - best.from;
696
- if (!best || size < held || (size === held && best.adds && !s.adds))
697
- best = s;
698
- }
699
- return best;
700
- }
701
681
  /**
702
682
  * The leaf's review layer. The decorations are a function of the document and the channel's
703
- * items, kept until one of them moves. The caret's report holds off while the reader
704
- * 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.
705
684
  */
706
685
  export function reviewLayer(channel) {
707
686
  let memo;
@@ -775,30 +754,10 @@ export function reviewLayer(channel) {
775
754
  reported = joined;
776
755
  channel.drew(next);
777
756
  };
778
- let caret;
779
- /** The item the reader typed in, quiet until the caret has left it. */
780
- let muted;
781
- const follow = (view, typed) => {
782
- const { selection } = view.state;
783
- const at = view.hasFocus() && selection.empty
784
- ? spanAt(drawn(view.state).spans, selection.head, reviewKey.getState(view.state)?.entered)
785
- ?.id
786
- : undefined;
787
- if (at !== muted)
788
- muted = undefined;
789
- if (typed && at)
790
- muted = at;
791
- const next = at === muted ? undefined : at;
792
- if (next === caret)
793
- return;
794
- caret = next;
795
- channel.point(next);
796
- };
797
757
  const plugin = new Plugin({
798
758
  key: reviewKey,
799
759
  state: {
800
760
  init: () => ({
801
- edits: 0,
802
761
  answered: new Map(),
803
762
  entered: undefined,
804
763
  replayed: [],
@@ -807,21 +766,7 @@ export function reviewLayer(channel) {
807
766
  apply: applyReview
808
767
  },
809
768
  props: {
810
- decorations,
811
- handleDOMEvents: {
812
- focus: (view) => {
813
- follow(view, false);
814
- return false;
815
- },
816
- blur: (view) => {
817
- // The focus has not left yet when a blur fires.
818
- queueMicrotask(() => {
819
- if (!view.isDestroyed)
820
- follow(view, false);
821
- });
822
- return false;
823
- }
824
- }
769
+ decorations
825
770
  },
826
771
  view: (view) => {
827
772
  host = view;
@@ -833,11 +778,8 @@ export function reviewLayer(channel) {
833
778
  for (const { id, to } of review.replayed)
834
779
  channel.act(id, to ?? 'reopen');
835
780
  report(view.state);
836
- follow(view, reviewKey.getState(prev)?.edits !== reviewKey.getState(view.state)?.edits);
837
781
  },
838
782
  destroy() {
839
- if (caret !== undefined)
840
- channel.point(undefined);
841
783
  if (reported !== NOTHING)
842
784
  channel.drew({ ids: [], outdated: [], undrawn: [] });
843
785
  host = undefined;
@@ -878,6 +820,8 @@ export function reviewLayer(channel) {
878
820
  },
879
821
  undo: (view) => undo(view.state, view.dispatch),
880
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,
881
825
  enter(view, id) {
882
826
  const span = drawn(view.state).spans.find((s) => s.id === id);
883
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;
@@ -0,0 +1,108 @@
1
+ // How a landing frames its target: the target in the band its scroller shows, and the label of the
2
+ // field it sits in with it wherever the two fit together, since a box without its label is a box
3
+ // the reader has to scroll to name. Where they do not fit, the target alone is framed.
4
+ import { spacePx } from './rungs.js';
5
+ /**
6
+ * The scroll, in px down, that frames `target` in `band` with `gap` clear of each edge. `top` is
7
+ * where the field's label starts: kept in the band whenever it fits with the target, the target
8
+ * winning where it does not. Zero for a target already framed, so a landing on the field in view
9
+ * moves nothing.
10
+ */
11
+ export function frameDelta(band, target, top, gap, block) {
12
+ const room = band.bottom - band.top - 2 * gap;
13
+ const high = band.top + gap;
14
+ const low = band.bottom - gap;
15
+ const tall = target.bottom - target.top > room;
16
+ let delta = 0;
17
+ if (tall)
18
+ delta = target.top - high;
19
+ else if (block === 'center')
20
+ delta = (target.top + target.bottom) / 2 - (high + low) / 2;
21
+ else if (target.top < high)
22
+ delta = target.top - high;
23
+ else if (target.bottom > low)
24
+ delta = target.bottom - low;
25
+ if (top < target.top && target.bottom - top <= room && top - delta < high)
26
+ delta = top - high;
27
+ return delta;
28
+ }
29
+ /** The nearest ancestor of `el` that scrolls vertically, or `undefined` where the page does. */
30
+ function scrollerOf(el) {
31
+ for (let at = el.parentElement; at; at = at.parentElement) {
32
+ if (at === el.ownerDocument.documentElement || at === el.ownerDocument.body)
33
+ return undefined;
34
+ const { overflowY } = getComputedStyle(at);
35
+ if ((overflowY === 'auto' || overflowY === 'scroll') && at.scrollHeight > at.clientHeight)
36
+ return at;
37
+ }
38
+ return undefined;
39
+ }
40
+ function px(value) {
41
+ const n = Number.parseFloat(value);
42
+ return Number.isFinite(n) ? n : 0;
43
+ }
44
+ /** The band `scroller` shows, less its `scroll-padding` and at least `foot` at its foot. */
45
+ function bandOf(scroller, foot) {
46
+ const doc = document.documentElement;
47
+ const style = getComputedStyle(scroller ?? doc);
48
+ let top = 0;
49
+ let bottom = window.innerHeight;
50
+ if (scroller) {
51
+ const box = scroller.getBoundingClientRect();
52
+ top = Math.max(box.top + scroller.clientTop, 0);
53
+ bottom = Math.min(top + scroller.clientHeight, bottom);
54
+ }
55
+ return {
56
+ top: top + px(style.scrollPaddingTop),
57
+ bottom: bottom - Math.max(px(style.scrollPaddingBottom), foot)
58
+ };
59
+ }
60
+ /** Scroll the box `target` scrolls in sideways, a table's, the least that shows it. */
61
+ function frameInline(target, stop, behavior) {
62
+ for (let at = target.parentElement; at && at !== stop; at = at.parentElement) {
63
+ const { overflowX } = getComputedStyle(at);
64
+ if ((overflowX !== 'auto' && overflowX !== 'scroll') || at.scrollWidth <= at.clientWidth)
65
+ continue;
66
+ const port = at.getBoundingClientRect();
67
+ const style = getComputedStyle(at);
68
+ const left = port.left + at.clientLeft + px(style.scrollPaddingLeft);
69
+ const right = port.left + at.clientLeft + at.clientWidth - px(style.scrollPaddingRight);
70
+ const box = target.getBoundingClientRect();
71
+ const dx = box.left < left || box.right - box.left > right - left
72
+ ? box.left - left
73
+ : Math.max(box.right - right, 0);
74
+ if (Math.abs(dx) >= 1)
75
+ at.scrollBy({ left: dx, behavior });
76
+ return;
77
+ }
78
+ }
79
+ /**
80
+ * Scroll `target` into the band its scroller shows, with the label of the field it sits in where
81
+ * the two fit (`frameDelta`). One scroller moves, the nearest: a host page around the editor stays
82
+ * where it is.
83
+ */
84
+ export function frame(target, opts) {
85
+ const box = target.getBoundingClientRect();
86
+ if (!box.width && !box.height)
87
+ return;
88
+ const field = target.closest('.qm-field');
89
+ const top = Math.min(field?.getBoundingClientRect().top ?? box.top, box.top);
90
+ const scroller = scrollerOf(target);
91
+ frameInline(target, scroller, opts.behavior ?? 'instant');
92
+ const delta = frameDelta(bandOf(scroller, opts.foot ?? 0), box, top, spacePx(target, 3) ?? 0, opts.block);
93
+ if (Math.abs(delta) < 1)
94
+ return;
95
+ (scroller ?? window).scrollBy({ top: delta, behavior: opts.behavior ?? 'instant' });
96
+ }
97
+ /** The top a landing frames for `els`, its field's label included where it fits in `band`. */
98
+ export function framedTop(els, band) {
99
+ const rects = els.map((el) => el.getBoundingClientRect()).filter((r) => r.width || r.height);
100
+ if (!rects.length)
101
+ return undefined;
102
+ const top = Math.min(...rects.map((r) => r.top));
103
+ const bottom = Math.max(...rects.map((r) => r.bottom));
104
+ const label = els[0]?.closest('.qm-field')?.getBoundingClientRect().top;
105
+ if (label === undefined || label >= top)
106
+ return top;
107
+ return bottom - label <= band.bottom - band.top ? label : top;
108
+ }
@@ -539,6 +539,16 @@
539
539
  --_qm-ink-label: var(--_qm-ink);
540
540
  }
541
541
 
542
+ /* A card a proposal adds is filled with the review tint, which takes back that distance too, so
543
+ over it the rung steps to `default`, and over the lit tint to the ink: each holds 4.5:1 at
544
+ both poles, where the label rung over the dark pole's lit tint stands near 2.4:1. */
545
+ :where(.qm-review-card-add) {
546
+ --_qm-ink-label: var(--_qm-ink-default);
547
+ }
548
+ :where(.qm-review-card-add.qm-review-lit) {
549
+ --_qm-ink-label: var(--_qm-ink);
550
+ }
551
+
542
552
  /* More contrast steps the two rungs that identify a control rather than a list of
543
553
  selectors: the well's edge to 3:1 against the card at both poles (WCAG 1.4.11),
544
554
  which half the ink over the well clears, and the ring a pixel wider. */
@@ -382,7 +382,7 @@
382
382
  {@attach registerControl}
383
383
  onfocusin={(e) => {
384
384
  reportFocus?.(e);
385
- if (proposed) reviewing.point(proposed.id);
385
+ if (proposed && field.control !== 'prose') reviewing.point(proposed.id);
386
386
  }}
387
387
  >
388
388
  {#if field.control === 'prose'}
@@ -69,6 +69,8 @@
69
69
  offsetToPM,
70
70
  valueEqual,
71
71
  valueDiffPlugin,
72
+ valueSpans,
73
+ reviewCaretPlugin,
72
74
  type ValueProposal,
73
75
  type LeafViews,
74
76
  type TextCap
@@ -167,6 +169,13 @@
167
169
  drawnProposals = (proposed ?? []).map((p) => ({ ...p, lit: reviewing.lit === p.id }));
168
170
  redraw?.();
169
171
  });
172
+ /** The caret names a proposal it arrives at, as a field's own leaf's does (`review-caret.ts`). */
173
+ const reviewCaret = reviewCaretPlugin({
174
+ spans: (state) => valueSpans(state, drawnProposals),
175
+ entered: () => undefined,
176
+ litHere: () => drawnProposals.some((p) => p.lit),
177
+ point: (id) => reviewing.point(id)
178
+ });
170
179
  /** A proposal's plan goes when it leaves the list or the leaf goes, not when another in the
171
180
  * list is answered, which would leave its own Accept writing its value whole. */
172
181
  let plannedIds: readonly string[] = [];
@@ -303,7 +312,8 @@
303
312
  valueDiffPlugin(
304
313
  () => drawnProposals,
305
314
  (id, state) => reviewing.planned(id, state)
306
- )
315
+ ),
316
+ reviewCaret
307
317
  ]
308
318
  })
309
319
  });
@@ -9,16 +9,17 @@
9
9
  It sits under the block the change flows in, so it covers no line of its paragraph, or beside
10
10
  that block's last line where the line ends short of it, centred on a block of one line such as a
11
11
  one-line field's, right-aligned to the column the
12
- change stands in, and flips above that block where there is no room below, keeping clear of the
13
- host's chrome at the pane's foot (`inset`). The column is the card's body or its form whole,
14
- never the one field, so Accept stands at one edge from change to change and a pointer answering
15
- a run of them does not hunt for it. A field or a property sharing its row with others takes the
16
- card beside its line only where the card stands within its own slot, and under the row
17
- otherwise: beside the line of one that ends short of the column's edge the card covers the
18
- value of the next. Over a change taller than the pane it stays pinned
19
- at the foot while any of the change shows. A change out of the pane keeps its card where it
20
- would stand beside it, so the card scrolls out with the text and the pane's edge clips it as it
21
- clips the text.
12
+ change stands in, and never above it: a card that turns over the text as a scroll runs short
13
+ of room moves under the pointer about to answer. Its room under the change is what a step
14
+ scrolls for, and the host's chrome at the pane's foot (`inset`) is kept clear. The column is
15
+ the card's body or its form whole, never the one field, so Accept stands at one edge from
16
+ change to change and a pointer answering a run of them does not hunt for it. A field or a
17
+ property sharing its row with others takes the card beside its line only where the card
18
+ stands within its own slot, and under the row otherwise: beside the line of one that ends
19
+ short of the column's edge the card covers the value of the next. Over a change too tall for
20
+ the pane to show with its card it stays pinned at the foot while any of the change shows. A
21
+ change out of the pane keeps its card where it would stand beside it, so the card scrolls out
22
+ with the text and the pane's edge clips it as it clips the text.
22
23
  A change is several elements (struck runs, tinted widgets, added blocks, a row
23
24
  under a control, a card's mark), so the box is their union, read anew on each measure: a leaf
24
25
  redraws its decorations as it likes, and a box measured once parts from the text it stood on.
@@ -59,6 +60,8 @@
59
60
  let left = $state(0);
60
61
  /** Pinned at the pane's foot, where every scroll moves it, so it follows without a glide. */
61
62
  let pinned = $state(false);
63
+ /** The whole card stands in the pane, clear of the host's chrome at its foot. */
64
+ let stands = false;
62
65
  /** The change is drawn, so the card has somewhere to stand. */
63
66
  let placed = $state(false);
64
67
  /** Where the focus was when the keyboard entered the card, to hand it back. */
@@ -124,21 +127,12 @@
124
127
  const slot = last?.closest('.qm-object-prop, .qm-field')?.getBoundingClientRect();
125
128
  const beside = tuck && tuck.right + gap <= left && (!slot || left + w <= slot.right + 1);
126
129
  const below = beside ? tuck.top : lower + gap;
127
- const above = upper - gap - h;
128
- const fits = below + h <= floor || above >= ceil;
129
- const y =
130
- below + h <= floor
131
- ? below
132
- : above >= ceil
133
- ? above
134
- : !shows
135
- ? upper > floor
136
- ? above
137
- : below
138
- : Math.max(ceil, Math.min(floor - h, below));
130
+ const tall = lower - upper + gap + h > floor - ceil;
131
+ pinned = shows && tall && below + h > floor;
132
+ const y = pinned ? Math.max(ceil, floor - h) : below;
133
+ stands = shows && y >= ceil && y + h <= floor;
139
134
  top = y - at.top + (root?.scrollTop ?? 0);
140
135
  left = left - at.left + (root?.scrollLeft ?? 0);
141
- pinned = shows && !fits;
142
136
  placed = true;
143
137
  }
144
138
 
@@ -221,6 +215,14 @@
221
215
  attributeFilter: ['data-qm-review', 'class', 'open', 'hidden']
222
216
  };
223
217
 
218
+ /** Whether the card stands whole in the pane where the change now is: an answer key answers
219
+ * only a change whose card the reader can see. A change with no box measures nothing, and
220
+ * counts as shown. */
221
+ export function standsInView(): boolean {
222
+ place();
223
+ return !placed || stands;
224
+ }
225
+
224
226
  /** Put the focus on Accept, handing it back to where it was on Escape or an answer. False
225
227
  * with no card on show. */
226
228
  export async function enter(): Promise<boolean> {
@@ -14,6 +14,7 @@ interface Props {
14
14
  onAnswer(action: 'accept' | 'dismiss'): void;
15
15
  }
16
16
  declare const ReviewCard: import("svelte").Component<Props, {
17
+ standsInView: () => boolean;
17
18
  enter: () => Promise<boolean>;
18
19
  }, "">;
19
20
  type ReviewCard = ReturnType<typeof ReviewCard>;
@@ -42,6 +42,7 @@
42
42
  } from '../core/address.js';
43
43
  import { errorMessage, reportError } from '../core/errors.js';
44
44
  import { bloom } from '../core/bloom.js';
45
+ import { frame, framedTop } from '../core/frame.js';
45
46
  import { createLifespan } from '../core/teardown.js';
46
47
  import { spacePx } from '../core/rungs.js';
47
48
  import { tooltips } from '../core/tooltip.js';
@@ -608,7 +609,10 @@
608
609
  const report = reviewing.report;
609
610
  drewOrder = report.open;
610
611
  drewOutdated = report.outdated;
611
- if (reviewing.lit === undefined && report.open.length) reviewing.reveal(report.open[0]);
612
+ if (reviewing.lit === undefined && report.open.length) {
613
+ reviewing.reveal(report.open[0]);
614
+ void reveal(report.open[0], false, true);
615
+ }
612
616
  const joined = JSON.stringify(report);
613
617
  if (joined === drewReport) return;
614
618
  drewReport = joined;
@@ -657,15 +661,15 @@
657
661
  const id = reviewing.lit;
658
662
  return id !== undefined && answerReview([id], action).length > 0;
659
663
  }
660
- /** An answer key: the card's, so it answers only a change the card stands on in view. A
661
- * press while the reader types in the editor is the leaf's own key, and any other press
662
- * with the change out of view or the card out of the way brings both back. */
664
+ /** An answer key: the card's, so it answers only a change whose card stands whole in view.
665
+ * A press while the reader types in the editor is the leaf's own key, and any other press
666
+ * with the change or its card out of view, or the card out of the way, brings both back. */
663
667
  function answerByKey(action: 'accept' | 'dismiss'): boolean {
664
668
  const id = reviewing.lit;
665
669
  if (id === undefined || (typing && inside)) return false;
666
- if (!typing && inView(id)) return answerCurrent(action);
670
+ if (!typing && inView(id) && (answerCard?.standsInView() ?? true)) return answerCurrent(action);
667
671
  typing = false;
668
- void reveal(id, false, true);
672
+ void reveal(id, false, false);
669
673
  return true;
670
674
  }
671
675
  /** Whether a key at `target` is the review's: anywhere but a text control outside the editor,
@@ -734,14 +738,60 @@
734
738
  function reviewIds(el: Element | null | undefined): string[] {
735
739
  return el?.getAttribute('data-qm-review')?.split(' ').filter(Boolean) ?? [];
736
740
  }
737
- /** A press on a change makes it current at once. */
741
+ /** A press on a change makes it current at once, and a press elsewhere in a field the
742
+ * nearest change in it, the current item being another field's. */
738
743
  function onPick(e: PointerEvent): void {
739
- const ids = reviewIds((e.target as Element | null)?.closest?.('[data-qm-review]'));
744
+ const target = e.target instanceof Element ? e.target : null;
745
+ const ids = reviewIds(target?.closest('[data-qm-review]'));
740
746
  const id = ids.includes(reviewing.lit ?? '') ? reviewing.lit : ids[0];
741
747
  if (id && reviewing.items.some((item) => item.id === id)) {
742
748
  typing = false;
743
749
  reviewing.reveal(id);
750
+ } else if (target) pickNear(target);
751
+ }
752
+ /** The reader arrived at `target` from outside its field, not handed back from a surface
753
+ * floating over it: the nearest change in the field becomes current. */
754
+ function onArrive(e: FocusEvent): void {
755
+ const target = e.target instanceof Element ? e.target : null;
756
+ const from = e.relatedTarget instanceof Element ? e.relatedTarget : null;
757
+ if (!target || !from || from.closest('.qm-popover-surface')) return;
758
+ const field = outerField(target);
759
+ if (field && !field.contains(from) && !target.closest('[data-qm-review]')) pickNear(target);
760
+ }
761
+ /** The outermost field holding `el`: the one a list's rows and a record's cells are of. */
762
+ function outerField(el: Element): Element | undefined {
763
+ let field: Element | undefined;
764
+ for (let at = el.closest('.qm-field'); at; at = at.parentElement?.closest('.qm-field') ?? null)
765
+ field = at;
766
+ return field;
767
+ }
768
+ /** Make the change in `target`'s field nearest it current, the fewest rows away and then the
769
+ * fewest pixels across, the current item being another field's. A leaf of words that draws
770
+ * changes of its own names them from its caret (`review-caret.ts`). */
771
+ function pickNear(target: Element): void {
772
+ if (target.closest('.ProseMirror')?.querySelector('[data-qm-review]')) return;
773
+ const field = outerField(target);
774
+ if (!field) return;
775
+ const open = new Set(reviewing.order);
776
+ const marks = [...field.querySelectorAll('[data-qm-review]')].filter((el) =>
777
+ reviewIds(el).some((id) => open.has(id))
778
+ );
779
+ if (!marks.length) return;
780
+ if (reviewing.lit !== undefined && marks.some((el) => reviewIds(el).includes(reviewing.lit!)))
781
+ return;
782
+ const at = target.getBoundingClientRect();
783
+ const gap = (a: number, b: number, c: number, d: number): number =>
784
+ b < c ? c - b : d < a ? a - d : 0;
785
+ let best: { el: Element; down: number; across: number } | undefined;
786
+ for (const el of marks) {
787
+ const r = el.getBoundingClientRect();
788
+ const down = gap(at.top, at.bottom, r.top, r.bottom);
789
+ const across = gap(at.left, at.right, r.left, r.right);
790
+ if (!best || down < best.down || (down === best.down && across < best.across))
791
+ best = { el, down, across };
744
792
  }
793
+ const id = best && reviewIds(best.el).find((id) => open.has(id));
794
+ if (id) reviewing.reveal(id);
745
795
  }
746
796
  /** What the change `id` is to: the field it is drawn in, the body, or the card it adds or
747
797
  * takes out. */
@@ -1477,7 +1527,9 @@
1477
1527
  */
1478
1528
  async function land(found: Landed, pos: number | undefined): Promise<HTMLElement> {
1479
1529
  if (found.steps && found.control.focusPath) {
1480
- return (await found.control.focusPath(found.steps, pos)) ?? found.control.el;
1530
+ const box = (await found.control.focusPath(found.steps, pos)) ?? found.control.el;
1531
+ if (pos == null) frame(box, { block: 'nearest' });
1532
+ return box;
1481
1533
  }
1482
1534
  if (pos != null) {
1483
1535
  const prose = leaves.prose(found.key);
@@ -1487,6 +1539,7 @@
1487
1539
  }
1488
1540
  }
1489
1541
  found.control.focus();
1542
+ frame(found.control.el, { block: 'nearest' });
1490
1543
  return found.control.el;
1491
1544
  }
1492
1545
 
@@ -1516,7 +1569,7 @@
1516
1569
  export async function revealField(field: DocPath): Promise<void> {
1517
1570
  const found = await revealLeaf(field);
1518
1571
  if (!found) return missed(`no mounted field at ${field}`, field);
1519
- found.control.el.scrollIntoView({ block: 'nearest', behavior: 'instant' });
1572
+ frame(found.control.el, { block: 'nearest' });
1520
1573
  bloom(found.control.el);
1521
1574
  }
1522
1575
  /** Play the arrival wash over the field at `field` and touch nothing else: no focus,
@@ -1624,7 +1677,7 @@
1624
1677
  (key !== undefined ? leaves.control(key)?.el : undefined);
1625
1678
  if (!at || (stepping && comfortable(els.length ? els : [at]))) return true;
1626
1679
  const reduce = window.matchMedia?.('(prefers-reduced-motion: reduce)').matches;
1627
- at.scrollIntoView({ behavior: reduce ? 'auto' : 'smooth', block: 'center' });
1680
+ frame(at, { block: 'center', foot: reviewInset, behavior: reduce ? 'instant' : 'smooth' });
1628
1681
  return true;
1629
1682
  }
1630
1683
  /** Whether the open card item `id` is drawn. */
@@ -1659,9 +1712,9 @@
1659
1712
  if (!rects.length) return false;
1660
1713
  const { top, bottom } = pane();
1661
1714
  const room = (spacePx(rootEl, 1) ?? 0) * CARD_ROOM;
1715
+ const band = { top, bottom: bottom - reviewInset - room };
1662
1716
  return (
1663
- Math.min(...rects.map((r) => r.top)) >= top &&
1664
- Math.max(...rects.map((r) => r.bottom)) <= bottom - reviewInset - room
1717
+ (framedTop(els, band) ?? top) >= top && Math.max(...rects.map((r) => r.bottom)) <= band.bottom
1665
1718
  );
1666
1719
  }
1667
1720
  /** Whether any of the item `id` shows in that view, which is where its answer card stands.
@@ -1979,6 +2032,7 @@
1979
2032
  {@attach tooltips}
1980
2033
  onkeydowncapture={onTyped}
1981
2034
  onpointerdowncapture={onPick}
2035
+ onfocusin={onArrive}
1982
2036
  onkeydown={raiseGuidance}
1983
2037
  ondragover={refuseFiles}
1984
2038
  ondrop={refuseFiles}
@@ -2533,13 +2587,8 @@
2533
2587
  }
2534
2588
  .qm-review-card-add-text {
2535
2589
  margin: 0;
2536
- color: var(--_qm-ink-label);
2537
2590
  white-space: pre-line;
2538
- display: -webkit-box;
2539
- -webkit-line-clamp: 3;
2540
- line-clamp: 3;
2541
- -webkit-box-orient: vertical;
2542
- overflow: hidden;
2591
+ overflow-wrap: anywhere;
2543
2592
  }
2544
2593
  @media (forced-colors: active) {
2545
2594
  .qm-review-card-add {
@@ -9,7 +9,7 @@ export { editorShortcuts } from '../core/codec/index.js';
9
9
  export type { EditorShortcut, ShortcutScope } from '../core/codec/index.js';
10
10
  export type { BlockProposal, CardProposal, FieldProposal, ReviewDrawn, ReviewEvent, ReviewItem, ReviewLabel, ReviewProjection, ReviewUndrawn } from './review.js';
11
11
  export type { BlockPlace } from '../core/codec/index.js';
12
- export { proposalsBetween } from './proposals.js';
12
+ export { proposalsBetween, reviewRevised } from './proposals.js';
13
13
  export type { Proposed, ProposalsOpts } from './proposals.js';
14
14
  export type { ReviewAction } from '../core/codec/index.js';
15
15
  export { REVIEW_KEYS } from '../core/codec/index.js';
@@ -25,7 +25,7 @@ export { DEFAULT_VISUAL_STRINGS } from './strings.js';
25
25
  // The prose leaf's shortcuts, for a host's shortcut list: read off the keymaps' own tables and
26
26
  // worded by the same `strings` (pass the editor's, or none for the package's English).
27
27
  export { editorShortcuts } from '../core/codec/index.js';
28
- export { proposalsBetween } from './proposals.js';
28
+ export { proposalsBetween, reviewRevised } from './proposals.js';
29
29
  export { REVIEW_KEYS } from '../core/codec/index.js';
30
30
  // The `$ext.editor` write unit. A consumer seeding editor-side chrome
31
31
  // state (a card title, the tips channel) goes through this rather than `storeExt`,
@@ -42,3 +42,12 @@ export interface ProposalsOpts {
42
42
  mint?: () => string;
43
43
  label?: (item: Readonly<ReviewItem>) => ReviewLabel | undefined;
44
44
  }
45
+ /**
46
+ * `items` naming the blocks of `now`, a whole-document revise of `was`, such as a return from
47
+ * editing the document as Markdown. A revise keeps a field's first id on whichever block it puts
48
+ * first, so a block written above the first takes its id and the first block moves under a new
49
+ * one. An item naming that id, to rewrite the block or to add beside it, names the new one, where
50
+ * the first block stands as {@link proposalsBetween} reads a block put above it. Every other item
51
+ * is as it was, and so is every id a revise keeps on its block.
52
+ */
53
+ export declare function reviewRevised(items: readonly ReviewItem[], was: Document, now: Document): ReviewItem[];