@quillmark/svelte 0.35.1 → 0.36.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.
Files changed (60) hide show
  1. package/README.md +21 -13
  2. package/dist/core/address.d.ts +5 -0
  3. package/dist/core/address.js +14 -0
  4. package/dist/core/codec/field.d.ts +30 -7
  5. package/dist/core/codec/field.js +67 -4
  6. package/dist/core/codec/index.d.ts +6 -3
  7. package/dist/core/codec/index.js +2 -3
  8. package/dist/core/codec/prose.css +11 -77
  9. package/dist/core/codec/review.d.ts +27 -21
  10. package/dist/core/codec/review.js +87 -148
  11. package/dist/core/codec/shortcuts.d.ts +3 -3
  12. package/dist/core/codec/shortcuts.js +7 -7
  13. package/dist/core/codec/value-diff.d.ts +21 -0
  14. package/dist/core/codec/value-diff.js +83 -0
  15. package/dist/core/codec/value-plan.d.ts +29 -0
  16. package/dist/core/codec/value-plan.js +157 -0
  17. package/dist/core/theme.css +7 -2
  18. package/dist/visual/ArrayField.svelte +191 -17
  19. package/dist/visual/Card.svelte +12 -1
  20. package/dist/visual/Card.svelte.d.ts +3 -0
  21. package/dist/visual/EnumField.svelte +14 -4
  22. package/dist/visual/EnumField.svelte.d.ts +3 -0
  23. package/dist/visual/Field.svelte +56 -47
  24. package/dist/visual/FieldHint.svelte +1 -0
  25. package/dist/visual/FormatPopover.svelte +1 -0
  26. package/dist/visual/MatrixField.svelte +43 -5
  27. package/dist/visual/MatrixField.svelte.d.ts +5 -0
  28. package/dist/visual/NumberField.svelte +112 -3
  29. package/dist/visual/NumberField.svelte.d.ts +5 -0
  30. package/dist/visual/ObjectField.svelte +54 -16
  31. package/dist/visual/ProseField.svelte +16 -3
  32. package/dist/visual/ProseField.svelte.d.ts +3 -0
  33. package/dist/visual/ProseValue.svelte +38 -2
  34. package/dist/visual/ProseValue.svelte.d.ts +3 -0
  35. package/dist/visual/ReviewCard.svelte +347 -0
  36. package/dist/visual/ReviewCard.svelte.d.ts +19 -0
  37. package/dist/visual/ReviewProposal.svelte +104 -138
  38. package/dist/visual/ReviewProposal.svelte.d.ts +3 -6
  39. package/dist/visual/ReviewRows.svelte +6 -25
  40. package/dist/visual/ReviewRows.svelte.d.ts +1 -3
  41. package/dist/visual/SlashMenu.svelte +1 -0
  42. package/dist/visual/VariantField.svelte +44 -4
  43. package/dist/visual/VariantField.svelte.d.ts +5 -0
  44. package/dist/visual/VisualEditor.svelte +16 -7
  45. package/dist/visual/VisualEditor.svelte.d.ts +3 -0
  46. package/dist/visual/VisualEditorInner.svelte +840 -115
  47. package/dist/visual/VisualEditorInner.svelte.d.ts +3 -0
  48. package/dist/visual/index.d.ts +2 -2
  49. package/dist/visual/proposals.d.ts +13 -22
  50. package/dist/visual/proposals.js +69 -32
  51. package/dist/visual/props.d.ts +18 -18
  52. package/dist/visual/review.d.ts +68 -3
  53. package/dist/visual/review.js +36 -1
  54. package/dist/visual/review.svelte.d.ts +55 -15
  55. package/dist/visual/review.svelte.js +156 -42
  56. package/dist/visual/strings.d.ts +23 -6
  57. package/dist/visual/strings.js +10 -5
  58. package/dist/visual/undo.svelte.d.ts +3 -1
  59. package/dist/visual/undo.svelte.js +7 -3
  60. package/package.json +1 -1
@@ -17,22 +17,12 @@
17
17
  // transaction, one undo step, one commit.
18
18
  import { closeHistory, isHistoryTransaction, undo } from 'prosemirror-history';
19
19
  import { DOMSerializer, Fragment, Mark } from 'prosemirror-model';
20
- import { keydownHandler } from 'prosemirror-keymap';
21
20
  import { Plugin, PluginKey, Selection } from 'prosemirror-state';
22
- import { Step, StepResult } from 'prosemirror-transform';
21
+ import { Mapping, Step, StepResult } from 'prosemirror-transform';
23
22
  import { Decoration, DecorationSet } from 'prosemirror-view';
24
23
  import { arriving, HARD_BREAK, ISLAND_SLOT } from './islands.js';
25
24
  import { remoteKey } from './remote.js';
26
25
  import { wordDiff } from './review-diff.js';
27
- import { REVIEW_KEYS } from './shortcuts.js';
28
- export const DEFAULT_REVIEW_STRINGS = {
29
- reviewAccept: 'Accept',
30
- reviewDismiss: 'Dismiss',
31
- reviewAcceptTitle: 'Accept',
32
- reviewDismissTitle: 'Dismiss',
33
- reviewProposal: 'Proposed change',
34
- reviewOutdated: 'Edited since'
35
- };
36
26
  const reviewKey = new PluginKey('quill-review');
37
27
  /** Tell the review layer the store took blocks under new ids, each `[was, now]`. */
38
28
  export function renameBlocks(tr, renamed) {
@@ -182,22 +172,8 @@ function tail(doc, from) {
182
172
  function taking(state, plan) {
183
173
  const tr = state.tr;
184
174
  try {
185
- for (const piece of [...plan].sort((a, b) => b.from - a.from)) {
186
- let { from, to } = piece;
187
- if (piece.lift) {
188
- const $at = tr.doc.resolve(from);
189
- for (let d = $at.depth; d > 0 && $at.node(d).childCount === 1; d--) {
190
- from = $at.before(d);
191
- to = $at.after(d);
192
- }
193
- // A leaf holds a block at the least: its last one is emptied where it stands.
194
- if (from === 0 && to === tr.doc.content.size) {
195
- tr.replaceWith(from, to, tr.doc.type.createAndFill().content);
196
- continue;
197
- }
198
- }
199
- tr.replaceWith(from, to, arriving(tr.doc, piece.content));
200
- }
175
+ for (const piece of [...plan].sort((a, b) => b.from - a.from))
176
+ writePiece(tr, piece);
201
177
  tr.doc.check();
202
178
  }
203
179
  catch {
@@ -205,6 +181,78 @@ function taking(state, plan) {
205
181
  }
206
182
  return tr.docChanged ? closeHistory(tr) : undefined;
207
183
  }
184
+ /** One piece of a plan written into `tr`, at its place in the document the plan was read over. */
185
+ function writePiece(tr, piece) {
186
+ let { from, to } = piece;
187
+ if (piece.lift) {
188
+ const $at = tr.doc.resolve(from);
189
+ for (let d = $at.depth; d > 0 && $at.node(d).childCount === 1; d--) {
190
+ from = $at.before(d);
191
+ to = $at.after(d);
192
+ }
193
+ // A leaf holds a block at the least: its last one is emptied where it stands.
194
+ if (from === 0 && to === tr.doc.content.size) {
195
+ tr.replaceWith(from, to, tr.doc.type.createAndFill().content);
196
+ return;
197
+ }
198
+ }
199
+ tr.replaceWith(from, to, arriving(tr.doc, piece.content));
200
+ }
201
+ /**
202
+ * Every proposal the leaf draws, taken from the plans it draws them from. The pieces of every
203
+ * plan are written in one pass, last first, which is what taking them one at a time writes,
204
+ * since no two proposals the leaf draws share a stretch; where the pieces of two do not make a
205
+ * document together, each is planned again over what the one before it left, as a batch of
206
+ * accepts plans it.
207
+ */
208
+ function projecting(state, now, redraw) {
209
+ const ids = now.spans.map((span) => span.id);
210
+ if (!ids.length)
211
+ return undefined;
212
+ const pieces = ids.flatMap((id) => (now.plans.get(id) ?? []).map((piece) => ({ id, piece })));
213
+ pieces.sort((a, b) => b.piece.from - a.piece.from);
214
+ const tr = state.tr;
215
+ const adds = [];
216
+ try {
217
+ for (const { id, piece } of pieces) {
218
+ const step = tr.steps.length;
219
+ writePiece(tr, piece);
220
+ if (piece.from === piece.to && piece.content.firstChild?.isBlock)
221
+ adds.push({ id, at: piece.from, step });
222
+ }
223
+ tr.doc.check();
224
+ }
225
+ catch {
226
+ return oneByOne(state, ids, redraw);
227
+ }
228
+ const next = state.apply(tr);
229
+ if (next === state)
230
+ return undefined;
231
+ const added = adds.map(({ id, at, step }) => {
232
+ const after = tr.mapping.slice(step);
233
+ return { id, from: after.map(at, -1), to: after.map(at, 1) };
234
+ });
235
+ return { state: next, ids, mapping: tr.mapping, added };
236
+ }
237
+ /** {@link projecting}, each proposal planned over what the one before it left. */
238
+ function oneByOne(state, ids, redraw) {
239
+ const mapping = new Mapping();
240
+ const taken = [];
241
+ let at = state;
242
+ for (const id of ids) {
243
+ const plan = redraw(at).plans.get(id);
244
+ const tr = plan && taking(at, plan);
245
+ if (!tr)
246
+ continue;
247
+ const next = at.apply(tr);
248
+ if (next === at)
249
+ continue;
250
+ mapping.appendMapping(tr.mapping);
251
+ taken.push(id);
252
+ at = next;
253
+ }
254
+ return taken.length ? { state: at, ids: taken, mapping, added: [] } : undefined;
255
+ }
208
256
  /** The marks on each offset of a textblock's content. */
209
257
  function marksOf(block) {
210
258
  const out = [];
@@ -335,60 +383,6 @@ const rewritten = (() => {
335
383
  return out;
336
384
  };
337
385
  })();
338
- /** A chord as `aria-keyshortcuts` spells it, `Mod` as either platform's. */
339
- const ariaKeys = (chord) => ['Meta', 'Control'].map((mod) => chord.replace('Mod', mod).replaceAll('-', '+')).join(' ');
340
- function button(act, id, action, label, title, keys) {
341
- const el = document.createElement('button');
342
- el.type = 'button';
343
- el.tabIndex = -1;
344
- el.className = `qm-review-act qm-review-${action}`;
345
- el.textContent = label;
346
- el.title = title;
347
- el.setAttribute('aria-keyshortcuts', ariaKeys(keys));
348
- // The press keeps the caret where it is: the answer is about the text, not a move off it.
349
- el.addEventListener('mousedown', (e) => e.preventDefault());
350
- el.addEventListener('click', (e) => {
351
- e.preventDefault();
352
- act(id, action);
353
- });
354
- return el;
355
- }
356
- /** A proposal's two buttons: a run at the end of its last line, or a row of their own under a
357
- * block that has no line to end. */
358
- function chip(strings, act, id, row, outdated) {
359
- const el = document.createElement(row ? 'div' : 'span');
360
- el.className = ['qm-review-chip', row && 'qm-review-chip-row', outdated && 'qm-review-outdated']
361
- .filter(Boolean)
362
- .join(' ');
363
- el.contentEditable = 'false';
364
- el.setAttribute('data-qm-review', id);
365
- el.setAttribute('role', 'group');
366
- el.setAttribute('aria-label', strings.reviewProposal);
367
- const acts = document.createElement('span');
368
- acts.className = 'qm-review-acts';
369
- if (outdated) {
370
- const label = document.createElement('span');
371
- label.className = 'qm-review-outdated-label';
372
- label.textContent = strings.reviewOutdated;
373
- acts.append(label);
374
- }
375
- acts.append(button(act, id, 'accept', strings.reviewAccept, strings.reviewAcceptTitle, REVIEW_KEYS.accept), button(act, id, 'dismiss', strings.reviewDismiss, strings.reviewDismissTitle, REVIEW_KEYS.dismiss));
376
- // The space a run of answers stands off its line by, which a wrapped run leaves behind.
377
- el.append(row ? '' : ' ', acts);
378
- return el;
379
- }
380
- /** The last textblock's element under `el`, where a run can be appended to its last line. */
381
- function lastLine(el) {
382
- let at = el;
383
- for (;;) {
384
- if (/^(P|H[1-6]|CODE)$/.test(at.tagName))
385
- return at;
386
- const last = at.lastElementChild;
387
- if (!(last instanceof HTMLElement))
388
- return undefined;
389
- at = last;
390
- }
391
- }
392
386
  /** The id of the stored block a leaf decodes from: a block island's own, any other's `id`. */
393
387
  function blockOf(node) {
394
388
  return String((node.type.name === 'island_block' ? node.attrs.block : node.attrs.id) ?? '');
@@ -440,7 +434,7 @@ function readOf(ids, before, channel) {
440
434
  row.set(before, out);
441
435
  return out;
442
436
  }
443
- function draw(state, channel, act, bases) {
437
+ function draw(state, channel, bases) {
444
438
  const { answered, renamed } = reviewKey.getState(state);
445
439
  const now = (id) => nowOf(renamed, id);
446
440
  const listed = channel.items();
@@ -466,7 +460,6 @@ function draw(state, channel, act, bases) {
466
460
  if (!items.length)
467
461
  return { decos, lighting, spans: [], plans, outdated, undrawn };
468
462
  const { doc } = state;
469
- const strings = channel.strings();
470
463
  const serializer = DOMSerializer.fromSchema(state.schema);
471
464
  const lights = (id, make) => {
472
465
  lighting.add(id);
@@ -594,13 +587,6 @@ function draw(state, channel, act, bases) {
594
587
  const $last = doc.resolve(last.pos);
595
588
  /** Past the outermost block holding the last one, or the head of the leaf. */
596
589
  const outer = ahead ? 0 : $last.depth ? $last.after(1) : last.pos + last.node.nodeSize;
597
- const row = () => {
598
- decos.push(Decoration.widget(outer, () => chip(strings, act, item.id, true, stale), {
599
- side: ahead ? -1 : 9999,
600
- key: `chip:${item.id}:row:${stale}`,
601
- stopEvent: () => true
602
- }));
603
- };
604
590
  if (added.length) {
605
591
  // A proposal that continues the list its paragraph is in adds items to that list.
606
592
  const listed = !ahead &&
@@ -620,10 +606,7 @@ function draw(state, channel, act, bases) {
620
606
  else
621
607
  plan.push({ from: piece.at, to: piece.at, content: Fragment.from(piece.node) });
622
608
  }
623
- const closing = pieces.length - 1;
624
- const chipped = endsInLine(pieces[closing].node);
625
609
  pieces.forEach((piece, n) => {
626
- const holds = chipped && n === closing;
627
610
  lights(item.id, (on) => Decoration.widget(piece.at, () => {
628
611
  const el = serializer.serializeNode(piece.node);
629
612
  el.classList.add('qm-review-add');
@@ -631,27 +614,13 @@ function draw(state, channel, act, bases) {
631
614
  el.classList.add('qm-review-lit');
632
615
  el.contentEditable = 'false';
633
616
  el.setAttribute('data-qm-review', item.id);
634
- if (holds)
635
- lastLine(el)?.append(chip(strings, act, item.id, false, stale));
636
617
  return el;
637
618
  }, {
638
619
  side: ahead ? -1000 + n : 1 + n,
639
- key: `add:${item.id}:${n}:${on}:${piece.node.toString()}`,
640
- stopEvent: (e) => e.target?.closest?.('.qm-review-chip') != null
620
+ key: `add:${item.id}:${n}:${on}:${piece.node.toString()}`
641
621
  }));
642
622
  });
643
- if (!chipped)
644
- row();
645
- }
646
- else if (last.node.isTextblock) {
647
- decos.push(Decoration.widget(reach(last)[1], () => chip(strings, act, item.id, false, stale), {
648
- side: 1,
649
- key: `chip:${item.id}:${stale}`,
650
- stopEvent: () => true
651
- }));
652
623
  }
653
- else
654
- row();
655
624
  // What a proposal only adds to has the caret anywhere in the block it adds from.
656
625
  spans.push({
657
626
  id: item.id,
@@ -662,13 +631,6 @@ function draw(state, channel, act, bases) {
662
631
  }
663
632
  return { decos, lighting, spans, plans, outdated, undrawn };
664
633
  }
665
- /** Whether a block's last leaf is a textblock, so its last line can carry a run. */
666
- function endsInLine(node) {
667
- let at = node;
668
- while (!at.isTextblock && !at.isAtom && at.lastChild)
669
- at = at.lastChild;
670
- return at.isTextblock;
671
- }
672
634
  /** The item the caret is in: the one it was put into by its id while it holds the caret, else
673
635
  * the one that holds it closest, so of a proposal that rewrites a paragraph and one that adds
674
636
  * after it, the one that rewrites. */
@@ -699,10 +661,9 @@ export function reviewLayer(channel) {
699
661
  let reported = NOTHING;
700
662
  let host;
701
663
  const bases = new Map();
702
- /** Answer an item. Taking a proposal writes it first, and one that can no longer be written
703
- * stays as it is. A proposal taken by a press while the focus is elsewhere leaves the caret
704
- * after what landed, in a focused leaf: the edit is the reader's, and so is its undo. */
705
- const act = (id, action, how = {}) => {
664
+ /** Answer an item, moving neither the caret nor the focus. Taking a proposal writes it
665
+ * first, and one that can no longer be written stays as it is. */
666
+ const act = (id, action) => {
706
667
  const view = host;
707
668
  if (!view || !drawn(view.state).spans.some((span) => span.id === id))
708
669
  return false;
@@ -711,11 +672,6 @@ export function reviewLayer(channel) {
711
672
  const tr = plan && taking(view.state, plan);
712
673
  if (!tr || !plan)
713
674
  return false;
714
- const away = !how.quiet && !view.hasFocus();
715
- if (away) {
716
- const end = tr.mapping.map(Math.max(...plan.map((piece) => piece.to)), 1);
717
- tr.setSelection(Selection.near(tr.doc.resolve(Math.min(end, tr.doc.content.size)), -1));
718
- }
719
675
  channel.writing?.(true);
720
676
  try {
721
677
  view.dispatch(tr.step(new ReviewAnswer(id, 'accept')));
@@ -723,29 +679,22 @@ export function reviewLayer(channel) {
723
679
  finally {
724
680
  channel.writing?.(false);
725
681
  }
726
- if (away)
727
- view.focus();
728
682
  }
729
683
  else {
730
684
  view.dispatch(view.state.tr
731
685
  .setMeta(reviewKey, { answer: { id, to: 'dismiss' } })
732
686
  .setMeta('addToHistory', false));
733
687
  }
734
- channel.act(id, action, how.chord);
688
+ channel.act(id, action);
735
689
  return true;
736
690
  };
737
691
  const drawn = (state) => {
738
692
  const review = reviewKey.getState(state);
739
693
  const items = channel.items();
740
- const strings = channel.strings();
741
- if (memo &&
742
- memo.doc === state.doc &&
743
- memo.review === review &&
744
- memo.items === items &&
745
- memo.strings === strings)
694
+ if (memo && memo.doc === state.doc && memo.review === review && memo.items === items)
746
695
  return memo.drawn;
747
- const next = draw(state, channel, act, bases);
748
- memo = { doc: state.doc, review, items, strings, drawn: next };
696
+ const next = draw(state, channel, bases);
697
+ memo = { doc: state.doc, review, items, drawn: next };
749
698
  return next;
750
699
  };
751
700
  /** Whether the lit item moving to `lit` changes how anything here reads. */
@@ -801,13 +750,6 @@ export function reviewLayer(channel) {
801
750
  caret = next;
802
751
  channel.point(next, 'caret');
803
752
  };
804
- const answer = (action) => (state) => {
805
- const span = spanAt(drawn(state).spans, state.selection.head, reviewKey.getState(state)?.entered);
806
- if (!span)
807
- return false;
808
- act(span.id, action, { chord: true });
809
- return true;
810
- };
811
753
  const plugin = new Plugin({
812
754
  key: reviewKey,
813
755
  state: {
@@ -822,10 +764,6 @@ export function reviewLayer(channel) {
822
764
  },
823
765
  props: {
824
766
  decorations,
825
- handleKeyDown: keydownHandler({
826
- [REVIEW_KEYS.accept]: answer('accept'),
827
- [REVIEW_KEYS.dismiss]: answer('dismiss')
828
- }),
829
767
  handleDOMEvents: {
830
768
  mouseover: (_view, e) => {
831
769
  hover(idAt(e.target));
@@ -878,7 +816,7 @@ export function reviewLayer(channel) {
878
816
  refresh(view) {
879
817
  // Only the lit item moved, and nothing here reads it: the leaf is left alone.
880
818
  const lit = channel.lit();
881
- if (memo?.items === channel.items() && memo.strings === channel.strings() && !relit(lit)) {
819
+ if (memo?.items === channel.items() && !relit(lit)) {
882
820
  if (shown)
883
821
  shown.lit = lit;
884
822
  return;
@@ -896,7 +834,7 @@ export function reviewLayer(channel) {
896
834
  elements(view, id) {
897
835
  return [...view.dom.querySelectorAll('[data-qm-review]')].filter((el) => el.getAttribute('data-qm-review') === id);
898
836
  },
899
- answer: (id, action) => act(id, action, { quiet: true }),
837
+ answer: act,
900
838
  reopen(view, id) {
901
839
  if (reviewKey.getState(view.state)?.answered.get(id) === 'dismiss')
902
840
  view.dispatch(view.state.tr
@@ -905,6 +843,7 @@ export function reviewLayer(channel) {
905
843
  channel.act(id, 'reopen');
906
844
  },
907
845
  undo: (view) => undo(view.state, view.dispatch),
846
+ project: (state) => projecting(state, drawn(state), (at) => at === state ? drawn(state) : draw(at, channel, new Map(bases))),
908
847
  enter(view, id) {
909
848
  const span = drawn(view.state).spans.find((s) => s.id === id);
910
849
  if (!span)
@@ -45,9 +45,9 @@ export declare const TABLE_KEYS: {
45
45
  readonly erase: "Backspace";
46
46
  readonly eraseAlt: "Delete";
47
47
  };
48
- /** An item of review under the caret (`review.ts`), ahead of every other binding of its keys,
49
- * so `accept` takes a proposal over a task item's `LIST_KEYS.task`; `next` and `previous` step
50
- * through the items from anywhere in the editor. */
48
+ /** The review's keys, the editor's wherever the focus is while a review is open and ahead of
49
+ * every other binding of them, so `accept` takes the current change over a task item's
50
+ * `LIST_KEYS.task`; `next` and `previous` step through the changes. */
51
51
  export declare const REVIEW_KEYS: {
52
52
  readonly accept: "Mod-Enter";
53
53
  readonly dismiss: "Mod-.";
@@ -51,9 +51,9 @@ export const TABLE_KEYS = {
51
51
  erase: 'Backspace',
52
52
  eraseAlt: 'Delete'
53
53
  };
54
- /** An item of review under the caret (`review.ts`), ahead of every other binding of its keys,
55
- * so `accept` takes a proposal over a task item's `LIST_KEYS.task`; `next` and `previous` step
56
- * through the items from anywhere in the editor. */
54
+ /** The review's keys, the editor's wherever the focus is while a review is open and ahead of
55
+ * every other binding of them, so `accept` takes the current change over a task item's
56
+ * `LIST_KEYS.task`; `next` and `previous` step through the changes. */
57
57
  export const REVIEW_KEYS = {
58
58
  accept: 'Mod-Enter',
59
59
  dismiss: 'Mod-.',
@@ -91,7 +91,7 @@ export const DEFAULT_SHORTCUT_STRINGS = {
91
91
  shortcutBulletedList: 'Bulleted list',
92
92
  shortcutIndent: 'Nest under the item above',
93
93
  shortcutOutdent: 'Move back up a level',
94
- shortcutTask: 'Check or uncheck a task, outside a proposal',
94
+ shortcutTask: 'Check or uncheck a task, outside a review',
95
95
  shortcutMoveItem: 'Move the item up or down',
96
96
  shortcutNextCell: 'Next cell, adding a row past the last',
97
97
  shortcutPreviousCell: 'Previous cell',
@@ -102,9 +102,9 @@ export const DEFAULT_SHORTCUT_STRINGS = {
102
102
  shortcutAlignColumn: 'Align a selected column',
103
103
  shortcutDeleteCells: 'Delete the selected cells, rows or columns',
104
104
  shortcutReviewAccept: 'Accept the proposed change',
105
- shortcutReviewDismiss: 'Dismiss the proposed change, or undo the change made',
106
- shortcutReviewNext: 'Go to the next review item',
107
- shortcutReviewPrevious: 'Go to the previous review item'
105
+ shortcutReviewDismiss: 'Dismiss the proposed change',
106
+ shortcutReviewNext: 'Go to the next proposed change',
107
+ shortcutReviewPrevious: 'Go to the previous proposed change'
108
108
  };
109
109
  const F = FIELD_KEYS;
110
110
  const L = LIST_KEYS;
@@ -0,0 +1,21 @@
1
+ import { Plugin, PluginKey } from 'prosemirror-state';
2
+ import type { Content } from '@quillmark/wasm';
3
+ /** A field's proposed value as the leaf draws it. */
4
+ export interface ValueProposal {
5
+ id: string;
6
+ /** The value the field would hold. */
7
+ after: Content;
8
+ /** The value as the proposal's writer read it, where it says. */
9
+ before?: Content;
10
+ /** It is the current item, drawn a step stronger. */
11
+ lit: boolean;
12
+ }
13
+ /** How a proposal stands against what the leaf holds: drawn and open, drawn and met by the
14
+ * reader's writing, or already read as proposed, so its Accept would write nothing. */
15
+ export type ValueState = 'open' | 'outdated' | 'applied';
16
+ export declare const valueDiffKey: PluginKey<void>;
17
+ /**
18
+ * The plugin, reading the proposal live: a caller that moves it redraws with `setProps`.
19
+ * `planned` hears, on each draw, how the proposal stands against what the leaf holds.
20
+ */
21
+ export declare function valueDiffPlugin(read: () => ValueProposal | undefined, planned?: (id: string, state: ValueState) => void): Plugin;
@@ -0,0 +1,83 @@
1
+ // A field's whole proposed value, drawn as a word diff over a leaf of lines: the words Accept would
2
+ // strike struck where they stand, the words it would write tinted beside them in their own
3
+ // formatting, nothing written. What is drawn is the difference between the leaf as it reads and
4
+ // what Accept would write (`planValue`), so the reader's words beside a proposal stay unstruck.
5
+ // The leaf holds its own text throughout, so the caret, a selection and a copy read what the field
6
+ // holds; taking the proposal is the field's own write, not this plugin's.
7
+ import { Plugin, PluginKey } from 'prosemirror-state';
8
+ import { Decoration, DecorationSet } from 'prosemirror-view';
9
+ import { DOMSerializer } from 'prosemirror-model';
10
+ import { scanContent, scanDoc } from './encode.js';
11
+ import { lineIndexOf, offsetToPM } from './positions.js';
12
+ import { decode } from './decode.js';
13
+ import { hasMarks } from './schema.js';
14
+ import { planValue, valueChanges } from './value-plan.js';
15
+ export const valueDiffKey = new PluginKey('qm-value-diff');
16
+ /** `[from, to)` of `c` as a value of its own, one line. */
17
+ function slice(c, from, to) {
18
+ const marks = (c.marks ?? []).flatMap((m) => {
19
+ const start = Math.max(m.start, from);
20
+ const end = Math.min(m.end, to);
21
+ return end > start ? [{ ...m, start: start - from, end: end - from }] : [];
22
+ });
23
+ return { text: c.text.slice(from, to), blocks: [{ id: '', kind: 'para' }], marks };
24
+ }
25
+ /** The proposed words as they would read: in their own formatting where the leaf holds marks. */
26
+ function inserted(schema, c, from, to) {
27
+ const part = slice(c, from, to);
28
+ if (!hasMarks(schema) || !part.marks?.length || part.text.includes('\n'))
29
+ return document.createTextNode(part.text);
30
+ const doc = decode(part, schema);
31
+ const inline = doc.firstChild?.isTextblock ? doc.firstChild.content : doc.content;
32
+ return DOMSerializer.fromSchema(schema).serializeFragment(inline);
33
+ }
34
+ function draw(state, p, planned) {
35
+ const scan = scanDoc(state.doc);
36
+ const live = scanContent(scan);
37
+ const index = lineIndexOf(scan);
38
+ const at = (offset) => offsetToPM(index, offset);
39
+ const { merged, outdated } = planValue(p.before, live, p.after);
40
+ const changes = valueChanges(live, merged);
41
+ planned?.(p.id, !changes.length ? 'applied' : outdated ? 'outdated' : 'open');
42
+ const decos = [];
43
+ const name = { 'data-qm-review': p.id };
44
+ const lit = p.lit ? ' qm-review-lit' : '';
45
+ changes.forEach(({ at: from, end, from: start, to }, n) => {
46
+ if (end > from)
47
+ decos.push(Decoration.inline(at(from), at(end), {
48
+ nodeName: 'del',
49
+ class: `qm-review-del${lit}`,
50
+ ...name
51
+ }));
52
+ if (to <= start)
53
+ return;
54
+ const text = merged.text.slice(start, to);
55
+ decos.push(Decoration.widget(end > from ? at(end) : at(from), () => {
56
+ const el = document.createElement('ins');
57
+ el.className = `qm-review-ins${lit}`;
58
+ el.contentEditable = 'false';
59
+ el.setAttribute('data-qm-review', p.id);
60
+ el.append(inserted(state.schema, merged, start, to));
61
+ return el;
62
+ }, {
63
+ side: end > from ? -1 : 1,
64
+ key: `value:${p.id}:${n}:${start}:${JSON.stringify(slice(merged, start, to).marks)}:${text}:${p.lit}`
65
+ }));
66
+ });
67
+ return DecorationSet.create(state.doc, decos);
68
+ }
69
+ /**
70
+ * The plugin, reading the proposal live: a caller that moves it redraws with `setProps`.
71
+ * `planned` hears, on each draw, how the proposal stands against what the leaf holds.
72
+ */
73
+ export function valueDiffPlugin(read, planned) {
74
+ return new Plugin({
75
+ key: valueDiffKey,
76
+ props: {
77
+ decorations(state) {
78
+ const p = read();
79
+ return p ? draw(state, p, planned) : null;
80
+ }
81
+ }
82
+ });
83
+ }
@@ -0,0 +1,29 @@
1
+ import type { Content } from '@quillmark/wasm';
2
+ /** `[at, end)` of one value gives way to `[from, to)` of another. */
3
+ export interface ValueChange {
4
+ at: number;
5
+ end: number;
6
+ from: number;
7
+ to: number;
8
+ }
9
+ /** What Accept writes, and whether the reader's own change meets the proposal. */
10
+ export interface ValuePlan {
11
+ merged: Content;
12
+ outdated: boolean;
13
+ }
14
+ /** A field value of words as a `Content`: a string by its lines, a rich or plain text value as it
15
+ * is, nothing as the empty line. `undefined` for a value that is not words. */
16
+ export declare function asContent(value: unknown): Content | undefined;
17
+ /**
18
+ * The changes that take `a` to `b`, in order and apart: the words that differ, then, in the words
19
+ * both hold, each run whose formatting differs, which gives way to itself as `b` formats it.
20
+ */
21
+ export declare function valueChanges(a: Content, b: Content): ValueChange[];
22
+ /**
23
+ * What Accept writes over `live`, the value as it reads now: `after` where there is no `before`,
24
+ * or where the reader has changed nothing since; else each change from `before` to `after` carried
25
+ * onto `live`, the reader's own changes kept. Where a change of the reader's meets one of the
26
+ * proposal's, or the value holds more than lines of words, it is `after` whole and outdated, unless
27
+ * the value already reads as `after`.
28
+ */
29
+ export declare function planValue(before: Content | undefined, live: Content, after: Content): ValuePlan;