@quillmark/svelte 0.33.0 → 0.35.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 (98) hide show
  1. package/README.md +73 -1
  2. package/THEMING.md +11 -10
  3. package/dist/core/address.d.ts +6 -0
  4. package/dist/core/address.js +16 -0
  5. package/dist/core/codec/anchor.d.ts +12 -0
  6. package/dist/core/codec/anchor.js +39 -0
  7. package/dist/core/codec/drag-move.d.ts +11 -0
  8. package/dist/core/codec/drag-move.js +143 -0
  9. package/dist/core/codec/encode.js +14 -5
  10. package/dist/core/codec/field.d.ts +44 -1
  11. package/dist/core/codec/field.js +137 -38
  12. package/dist/core/codec/index.d.ts +8 -2
  13. package/dist/core/codec/index.js +9 -2
  14. package/dist/core/codec/islands.d.ts +4 -1
  15. package/dist/core/codec/islands.js +6 -0
  16. package/dist/core/codec/keymap.d.ts +3 -0
  17. package/dist/core/codec/keymap.js +13 -12
  18. package/dist/core/codec/leave.d.ts +17 -0
  19. package/dist/core/codec/leave.js +78 -0
  20. package/dist/core/codec/prose.css +172 -0
  21. package/dist/core/codec/review-diff.d.ts +12 -0
  22. package/dist/core/codec/review-diff.js +131 -0
  23. package/dist/core/codec/review.d.ts +116 -0
  24. package/dist/core/codec/review.js +920 -0
  25. package/dist/core/codec/shortcuts.d.ts +20 -4
  26. package/dist/core/codec/shortcuts.js +27 -7
  27. package/dist/core/codec/slash.d.ts +3 -0
  28. package/dist/core/codec/slash.js +2 -1
  29. package/dist/core/codec/table-view.js +163 -25
  30. package/dist/core/codec/undo.d.ts +6 -0
  31. package/dist/core/codec/undo.js +25 -2
  32. package/dist/core/dragfollow.d.ts +18 -0
  33. package/dist/core/dragfollow.js +63 -0
  34. package/dist/core/theme.css +34 -7
  35. package/dist/preview/controller.js +16 -0
  36. package/dist/preview/strings.d.ts +4 -0
  37. package/dist/preview/strings.js +3 -1
  38. package/dist/visual/ArrayField.svelte +363 -57
  39. package/dist/visual/ArrayField.svelte.d.ts +5 -0
  40. package/dist/visual/BooleanField.svelte +60 -5
  41. package/dist/visual/BooleanField.svelte.d.ts +2 -1
  42. package/dist/visual/Card.svelte +27 -17
  43. package/dist/visual/CardControls.svelte +17 -7
  44. package/dist/visual/CardControls.svelte.d.ts +2 -0
  45. package/dist/visual/DateField.svelte +90 -39
  46. package/dist/visual/DateField.svelte.d.ts +2 -2
  47. package/dist/visual/Field.svelte +98 -6
  48. package/dist/visual/FieldHint.svelte +24 -9
  49. package/dist/visual/FieldLabel.svelte +128 -22
  50. package/dist/visual/FieldLabel.svelte.d.ts +9 -0
  51. package/dist/visual/FormatPopover.svelte +125 -20
  52. package/dist/visual/MatrixAdd.svelte +13 -6
  53. package/dist/visual/MatrixAdd.svelte.d.ts +3 -2
  54. package/dist/visual/MatrixField.svelte +36 -8
  55. package/dist/visual/NumberField.svelte +27 -6
  56. package/dist/visual/NumberField.svelte.d.ts +3 -0
  57. package/dist/visual/ObjectField.svelte +25 -1
  58. package/dist/visual/ObjectField.svelte.d.ts +6 -0
  59. package/dist/visual/ProseField.svelte +21 -0
  60. package/dist/visual/ProseField.svelte.d.ts +3 -0
  61. package/dist/visual/ProseValue.svelte +107 -17
  62. package/dist/visual/ProseValue.svelte.d.ts +18 -4
  63. package/dist/visual/ReviewProposal.svelte +149 -0
  64. package/dist/visual/ReviewProposal.svelte.d.ts +15 -0
  65. package/dist/visual/ReviewRows.svelte +43 -0
  66. package/dist/visual/ReviewRows.svelte.d.ts +11 -0
  67. package/dist/visual/SlashMenu.svelte +13 -1
  68. package/dist/visual/SlashMenu.svelte.d.ts +2 -0
  69. package/dist/visual/VisualEditor.svelte +22 -0
  70. package/dist/visual/VisualEditor.svelte.d.ts +7 -0
  71. package/dist/visual/VisualEditorInner.svelte +591 -27
  72. package/dist/visual/VisualEditorInner.svelte.d.ts +7 -0
  73. package/dist/visual/controls.css +17 -2
  74. package/dist/visual/guidance.d.ts +12 -0
  75. package/dist/visual/guidance.js +37 -2
  76. package/dist/visual/icons/nodes.d.ts +14 -2
  77. package/dist/visual/icons/nodes.js +12 -1
  78. package/dist/visual/index.d.ts +6 -0
  79. package/dist/visual/index.js +2 -0
  80. package/dist/visual/listhistory.d.ts +36 -0
  81. package/dist/visual/listhistory.js +53 -0
  82. package/dist/visual/proposals.d.ts +40 -0
  83. package/dist/visual/proposals.js +219 -0
  84. package/dist/visual/props.d.ts +36 -0
  85. package/dist/visual/removed.d.ts +20 -0
  86. package/dist/visual/removed.js +30 -0
  87. package/dist/visual/review.d.ts +135 -0
  88. package/dist/visual/review.js +227 -0
  89. package/dist/visual/review.svelte.d.ts +79 -0
  90. package/dist/visual/review.svelte.js +226 -0
  91. package/dist/visual/rowdrag.svelte.js +44 -55
  92. package/dist/visual/strings.d.ts +36 -7
  93. package/dist/visual/strings.js +26 -7
  94. package/dist/visual/structure.d.ts +5 -0
  95. package/dist/visual/structure.js +1 -1
  96. package/dist/visual/undo.svelte.d.ts +40 -7
  97. package/dist/visual/undo.svelte.js +98 -25
  98. package/package.json +1 -1
@@ -0,0 +1,135 @@
1
+ import type { PathStep } from '@quillmark/wasm';
2
+ import type { DocPath } from '../core/address.js';
3
+ import { type BlockPlace, type ReviewAction } from '../core/codec/index.js';
4
+ /**
5
+ * One proposal. `id` is the host's, unique in the list, and is what every event names.
6
+ *
7
+ * An item names its place one of two ways:
8
+ *
9
+ * - `field` with `replace` or `insert`: blocks of a rich text field or a body, by the ids the
10
+ * document gives them. A `proposed` item with `replace` reads the blocks named as rewritten to
11
+ * `after`, Markdown, drawn as a word-level edit of them; `''` proposes their removal. `before`
12
+ * is those blocks as the proposal's writer read them, so what the reader wrote in them since is
13
+ * theirs: kept unstruck, and kept by Accept. One with `insert` proposes new blocks after or
14
+ * before a block, or at the field's end. A paragraph takes one proposal that rewrites it and one
15
+ * that adds after it.
16
+ * - `field` alone: the path of a field. It sets `after` under the control, the value the
17
+ * field's writer would take, an absent one proposing the field cleared, and `before` the value
18
+ * its writer read. A path to a place inside a record or a
19
+ * list names that place: a cell, or a member by its index, which `after` absent takes out
20
+ * and an index past the last appends.
21
+ *
22
+ * An item whose place the editor does not hold draws nothing. An answered item is drawn no
23
+ * more: a dismissed one wrote nothing, and an accepted one is plain text in the document.
24
+ */
25
+ export type ReviewItem = FieldProposal | BlockProposal;
26
+ /** A proposal over blocks of a rich text field, by their ids. */
27
+ export type BlockProposal = {
28
+ id: string;
29
+ field: DocPath;
30
+ replace: readonly string[];
31
+ before?: string;
32
+ after: string;
33
+ } | {
34
+ id: string;
35
+ field: DocPath;
36
+ insert: BlockPlace;
37
+ after: string;
38
+ };
39
+ /** Whether an item names blocks of a rich text field, which its prose leaf draws. */
40
+ export declare const byBlocks: (item: object) => item is BlockProposal;
41
+ /** Whether an item is a field's own, drawn under its control. */
42
+ export declare const onField: (item: object) => item is FieldProposal;
43
+ /**
44
+ * A reader's answer to an item.
45
+ *
46
+ * - `accept`: a proposal taken. The editor has written it by now, as an edit of the reader's.
47
+ * - `dismiss`: a proposal turned down. Nothing is written.
48
+ * - `reopen`: an accept or a dismissal undone by ⌘Z. The editor has taken back what an accept
49
+ * wrote, and the item is open again.
50
+ */
51
+ export interface ReviewEvent {
52
+ id: string;
53
+ action: ReviewAction;
54
+ }
55
+ /**
56
+ * Why a listed item draws nothing:
57
+ *
58
+ * - `block`: no block it names stands in its field, or no leaf of the field is mounted.
59
+ * - `field`: its path names no field the editor holds, or a place inside one its schema does
60
+ * not declare.
61
+ * - `parse`: its `after` is not Markdown the leaf holding it reads.
62
+ * - `taken`: an earlier item has its paragraph, or its field.
63
+ * - `applied`: the text already reads as proposed, so it would write nothing.
64
+ */
65
+ export type ReviewUndrawn = 'block' | 'field' | 'parse' | 'taken' | 'applied';
66
+ /** What the editor draws of the host's list. */
67
+ export interface ReviewDrawn {
68
+ /** The proposals the editor draws, in document order, each waiting for an answer. A review
69
+ * is answered when it is empty. */
70
+ open: readonly string[];
71
+ /** Drawn proposals the reader's own writing meets: words both changed, a block one names
72
+ * gone or no longer what its writer read, or a field no longer holding its `before`. */
73
+ outdated: readonly string[];
74
+ /** Listed items the editor does not draw, each with why. */
75
+ undrawn: readonly {
76
+ id: string;
77
+ reason: ReviewUndrawn;
78
+ }[];
79
+ }
80
+ /** A proposal under a field's control. */
81
+ export type FieldProposal = {
82
+ id: string;
83
+ field: DocPath;
84
+ before?: unknown;
85
+ after?: unknown;
86
+ };
87
+ /** A field item routed past its field: the steps from the rung it is handed to down to the
88
+ * value it names, a member of a list by its index, a cell of a record by its key. */
89
+ export interface DeepReview {
90
+ steps: readonly PathStep[];
91
+ item: FieldProposal;
92
+ }
93
+ /** One rung of a nested item's walk: the items naming this rung's own value, and the rest by
94
+ * the step they take next, that step consumed. */
95
+ export declare function splitReview(list: readonly DeepReview[] | undefined): {
96
+ here: DeepReview[];
97
+ below: Map<PathStep, DeepReview[]>;
98
+ };
99
+ /** The value `steps` reach in `value`, `undefined` where they reach nothing. */
100
+ export declare function valueIn(value: unknown, steps: readonly PathStep[]): unknown;
101
+ /** `value` with what `steps` reach set to `to`: a list's member past its end appended, and one
102
+ * set to nothing taken out; a record's cell set to nothing left out. Copies on the way down. */
103
+ export declare function valueWith(value: unknown, steps: readonly PathStep[], to: unknown): unknown;
104
+ /**
105
+ * Where a field proposal's steps land in `value` as it stands. A list's member is found by its
106
+ * index where it holds `before`, else as the nearest member holding `before` that `claimed`
107
+ * does not already hold, so one member taken out does not move another's proposal onto the
108
+ * wrong row. `found` is false where the proposal has a `before` nothing holds.
109
+ */
110
+ export declare function locate(value: unknown, steps: readonly PathStep[], item: FieldProposal, claimed: Set<string>): {
111
+ steps: readonly PathStep[];
112
+ found: boolean;
113
+ };
114
+ /** Whether two values are one, whatever order their keys stand in; absent is `null`, and a
115
+ * plain text value rests as its words, as a writer takes it either way. */
116
+ export declare function sameValue(a: unknown, b: unknown): boolean;
117
+ /** A reviewed value as the words a reader is shown for it: a string as written, a rich text
118
+ * value by its text, a list by its members, a subform by its named cells. `''` for nothing. */
119
+ export declare function reviewText(value: unknown, words: {
120
+ on: string;
121
+ off: string;
122
+ }): string;
123
+ /** A stretch of a proposal as a reader is shown it: kept, struck, or tinted. */
124
+ export interface ReviewRun {
125
+ text: string;
126
+ kind?: 'del' | 'ins';
127
+ }
128
+ type Words = {
129
+ on: string;
130
+ off: string;
131
+ };
132
+ /** A proposal as runs, what it keeps, strikes and tints. Two lines of words differ by their words,
133
+ * two lists by their members, two records by their keys; any other pair differs whole. */
134
+ export declare function reviewRuns(before: unknown, after: unknown, words: Words): ReviewRun[];
135
+ export {};
@@ -0,0 +1,227 @@
1
+ import { wordDiff } from '../core/codec/index.js';
2
+ /** Whether an item names blocks of a rich text field, which its prose leaf draws. */
3
+ export const byBlocks = (item) => 'replace' in item || 'insert' in item;
4
+ /** Whether an item is a field's own, drawn under its control. */
5
+ export const onField = (item) => 'field' in item && !byBlocks(item);
6
+ /** One rung of a nested item's walk: the items naming this rung's own value, and the rest by
7
+ * the step they take next, that step consumed. */
8
+ export function splitReview(list) {
9
+ const here = [];
10
+ const below = new Map();
11
+ for (const entry of list ?? []) {
12
+ if (!entry.steps.length) {
13
+ here.push(entry);
14
+ continue;
15
+ }
16
+ const [step, ...steps] = entry.steps;
17
+ const bucket = below.get(step) ?? [];
18
+ bucket.push({ steps, item: entry.item });
19
+ below.set(step, bucket);
20
+ }
21
+ return { here, below };
22
+ }
23
+ /** The value `steps` reach in `value`, `undefined` where they reach nothing. */
24
+ export function valueIn(value, steps) {
25
+ let at = value;
26
+ for (const step of steps) {
27
+ if (at == null || typeof at !== 'object')
28
+ return undefined;
29
+ at = at[step];
30
+ }
31
+ return at;
32
+ }
33
+ /** `value` with what `steps` reach set to `to`: a list's member past its end appended, and one
34
+ * set to nothing taken out; a record's cell set to nothing left out. Copies on the way down. */
35
+ export function valueWith(value, steps, to) {
36
+ if (!steps.length)
37
+ return to;
38
+ const [step, ...rest] = steps;
39
+ if (typeof step === 'number') {
40
+ const list = Array.isArray(value) ? [...value] : [];
41
+ const at = Math.min(step, list.length);
42
+ const next = valueWith(list[at], rest, to);
43
+ if (!rest.length && to === undefined)
44
+ list.splice(at, 1);
45
+ else if (step >= list.length)
46
+ list.push(next);
47
+ else
48
+ list[at] = next;
49
+ return list;
50
+ }
51
+ const record = value && typeof value === 'object' && !Array.isArray(value) ? { ...value } : {};
52
+ const next = valueWith(record[step], rest, to);
53
+ if (next === undefined)
54
+ delete record[step];
55
+ else
56
+ record[step] = next;
57
+ return record;
58
+ }
59
+ /**
60
+ * Where a field proposal's steps land in `value` as it stands. A list's member is found by its
61
+ * index where it holds `before`, else as the nearest member holding `before` that `claimed`
62
+ * does not already hold, so one member taken out does not move another's proposal onto the
63
+ * wrong row. `found` is false where the proposal has a `before` nothing holds.
64
+ */
65
+ export function locate(value, steps, item, claimed) {
66
+ const read = 'before' in item;
67
+ const before = read ? item.before : undefined;
68
+ const at = steps.at(-1);
69
+ const head = steps.slice(0, -1);
70
+ const list = valueIn(value, head);
71
+ const claim = (out) => {
72
+ claimed.add(JSON.stringify(out));
73
+ return out;
74
+ };
75
+ if (!read || before === undefined || typeof at !== 'number' || !Array.isArray(list)) {
76
+ const found = !read || sameValue(valueIn(value, steps), before);
77
+ return { steps: claim(steps), found };
78
+ }
79
+ const free = (i) => sameValue(list[i], before) && !claimed.has(JSON.stringify([...head, i]));
80
+ if (free(at))
81
+ return { steps: claim(steps), found: true };
82
+ for (let d = 1; d < list.length; d++)
83
+ for (const i of [at - d, at + d])
84
+ if (i >= 0 && i < list.length && free(i))
85
+ return { steps: claim([...head, i]), found: true };
86
+ return { steps, found: false };
87
+ }
88
+ /** Whether two values are one, whatever order their keys stand in; absent is `null`, and a
89
+ * plain text value rests as its words, as a writer takes it either way. */
90
+ export function sameValue(a, b) {
91
+ const canonical = (value) => JSON.stringify(value ?? null, (_, v) => {
92
+ if (!v || typeof v !== 'object' || Array.isArray(v))
93
+ return v;
94
+ const { text, codec } = v;
95
+ if (codec === 'plain' && typeof text === 'string')
96
+ return text;
97
+ return Object.fromEntries(Object.entries(v).sort(([x], [y]) => (x < y ? -1 : x > y ? 1 : 0)));
98
+ });
99
+ return canonical(a) === canonical(b);
100
+ }
101
+ /** A reviewed value as the words a reader is shown for it: a string as written, a rich text
102
+ * value by its text, a list by its members, a subform by its named cells. `''` for nothing. */
103
+ export function reviewText(value, words) {
104
+ if (value == null)
105
+ return '';
106
+ if (typeof value === 'boolean')
107
+ return value ? words.on : words.off;
108
+ if (typeof value !== 'object')
109
+ return String(value);
110
+ if (Array.isArray(value))
111
+ return value
112
+ .map((v) => reviewText(v, words))
113
+ .filter(Boolean)
114
+ .join(', ');
115
+ const text = value.text;
116
+ if (typeof text === 'string' && Array.isArray(value.blocks))
117
+ return text;
118
+ return Object.entries(value)
119
+ .map(([key, v]) => [key, reviewText(v, words)])
120
+ .filter(([, v]) => v)
121
+ .map(([key, v]) => `${key}: ${v}`)
122
+ .join('; ');
123
+ }
124
+ const prose = (v) => typeof v === 'string' || (typeof v === 'object' && v !== null && 'text' in v);
125
+ const record = (v) => typeof v === 'object' && v !== null && !Array.isArray(v) && !prose(v);
126
+ const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
127
+ function whole(before, after) {
128
+ return [
129
+ ...(before ? [{ text: before, kind: 'del' }] : []),
130
+ ...(after ? [{ text: after, kind: 'ins' }] : [])
131
+ ];
132
+ }
133
+ function joined(parts, by) {
134
+ return parts.filter((p) => p.length).flatMap((p, i) => (i ? [{ text: by }, ...p] : p));
135
+ }
136
+ /** The keys of two records that differ, each as `key: ` with what goes struck and what comes
137
+ * tinted. */
138
+ function recordRuns(before, after, words) {
139
+ const keys = [...new Set([...Object.keys(after), ...Object.keys(before)])];
140
+ return joined(keys
141
+ .filter((key) => !same(before[key], after[key]))
142
+ .map((key) => {
143
+ const runs = whole(reviewText(before[key], words), reviewText(after[key], words));
144
+ return runs.length ? [{ text: `${key}: ` }, ...runs] : [];
145
+ }), '; ');
146
+ }
147
+ /** Two lists by their members, in order: a member both hold is kept, one only `before` holds is
148
+ * struck, one only `after` holds is tinted. In a list of records a kept record is left out, and a
149
+ * record struck where one is tinted reads as the keys the two differ in. */
150
+ function listRuns(before, after, words) {
151
+ const n = before.length;
152
+ const m = after.length;
153
+ const common = Array.from({ length: n + 1 }, () => new Array(m + 1).fill(0));
154
+ for (let i = n - 1; i >= 0; i--)
155
+ for (let j = m - 1; j >= 0; j--)
156
+ common[i][j] = same(before[i], after[j])
157
+ ? common[i + 1][j + 1] + 1
158
+ : Math.max(common[i + 1][j], common[i][j + 1]);
159
+ const records = [...before, ...after].some(record);
160
+ /** A record edited in place, named by its first value where that stays, so a reader can tell
161
+ * which of the list it is with the records kept left out. */
162
+ const edited = (was, now) => {
163
+ const first = Object.keys(now)[0];
164
+ const name = first !== undefined && same(was[first], now[first]) && reviewText(now[first], words);
165
+ const runs = recordRuns(was, now, words);
166
+ return name ? [{ text: `${name} (` }, ...runs, { text: ')' }] : runs;
167
+ };
168
+ const parts = [];
169
+ let gone = [];
170
+ let come = [];
171
+ const flush = () => {
172
+ const pairs = records ? Math.min(gone.length, come.length) : 0;
173
+ for (let k = 0; k < pairs; k++)
174
+ parts.push(record(gone[k]) && record(come[k])
175
+ ? edited(gone[k], come[k])
176
+ : whole(reviewText(gone[k], words), reviewText(come[k], words)));
177
+ for (const v of gone.slice(pairs))
178
+ parts.push(whole(reviewText(v, words), ''));
179
+ for (const v of come.slice(pairs))
180
+ parts.push(whole('', reviewText(v, words)));
181
+ gone = [];
182
+ come = [];
183
+ };
184
+ let i = 0;
185
+ let j = 0;
186
+ while (i < n || j < m) {
187
+ if (i < n && j < m && same(before[i], after[j])) {
188
+ flush();
189
+ if (!records)
190
+ parts.push([{ text: reviewText(before[i], words) }]);
191
+ i++;
192
+ j++;
193
+ }
194
+ else if (j < m && (i === n || common[i][j + 1] >= common[i + 1][j]))
195
+ come.push(after[j++]);
196
+ else
197
+ gone.push(before[i++]);
198
+ }
199
+ flush();
200
+ return joined(parts, ', ');
201
+ }
202
+ /** A proposal as runs, what it keeps, strikes and tints. Two lines of words differ by their words,
203
+ * two lists by their members, two records by their keys; any other pair differs whole. */
204
+ export function reviewRuns(before, after, words) {
205
+ if (Array.isArray(before) && Array.isArray(after) && before.length && after.length)
206
+ return listRuns(before, after, words);
207
+ if (record(before) && record(after))
208
+ return recordRuns(before, after, words);
209
+ const was = reviewText(before, words);
210
+ const now = reviewText(after, words);
211
+ if (!prose(before) || !prose(after) || !was || !now)
212
+ return whole(was, now);
213
+ const out = [];
214
+ let at = 0;
215
+ for (const hunk of wordDiff(was, now)) {
216
+ if (hunk.at > at)
217
+ out.push({ text: was.slice(at, hunk.at) });
218
+ if (hunk.end > hunk.at)
219
+ out.push({ text: was.slice(hunk.at, hunk.end), kind: 'del' });
220
+ if (hunk.insert)
221
+ out.push({ text: hunk.insert, kind: 'ins' });
222
+ at = hunk.end;
223
+ }
224
+ if (at < was.length)
225
+ out.push({ text: was.slice(at) });
226
+ return out;
227
+ }
@@ -0,0 +1,79 @@
1
+ import type { PathStep } from '@quillmark/wasm';
2
+ import type { DocPath } from '../core/address.js';
3
+ import type { ReviewAction, ReviewReport, ReviewVia, TextReview } from '../core/codec/index.js';
4
+ import { type DeepReview, type FieldProposal, type ReviewItem, type ReviewUndrawn } from './review.js';
5
+ /** How long a revealed item stays lit, in ms. */
6
+ export declare const REVEAL_MS = 2400;
7
+ export interface Review {
8
+ /** The host's list, less what was answered here. */
9
+ readonly items: readonly ReviewItem[];
10
+ /** The items the prose leaf at `key` draws, those placed by blocks of its own field. One
11
+ * identity until the list changes. */
12
+ textAt(key: string): readonly TextReview[];
13
+ /** Each mounted prose leaf's report of what it draws, by leaf key. */
14
+ readonly reports: ReadonlyMap<string, ReviewReport>;
15
+ /** The field items no control draws, each with why. */
16
+ readonly fieldUndrawn: readonly {
17
+ id: string;
18
+ reason: ReviewUndrawn;
19
+ }[];
20
+ /** The field proposals whose field no longer holds the value their `before` says was read. */
21
+ readonly fieldOutdated: ReadonlySet<string>;
22
+ /** The item under attention. */
23
+ readonly lit: string | undefined;
24
+ /** The items over the field at leaf `key`: its own, and those naming a place inside it,
25
+ * each with the steps to the value it names as the field holds it now. */
26
+ at(key: string): {
27
+ proposed?: FieldProposal;
28
+ nested?: readonly DeepReview[];
29
+ };
30
+ /** Whether anything draws at leaf `key`: a field's own item, or a text item in its leaf. */
31
+ marks(key: string): boolean;
32
+ /** The leaf drawing item `id`, where one does. */
33
+ keyOf(id: string): string | undefined;
34
+ /** A prose leaf's report of what it draws. */
35
+ drew(key: string, report: ReviewReport): void;
36
+ /** The item the pointer or the caret is in, from leaf `from`: a caret's leaving reported by
37
+ * a leaf the caret has already left for another is old news. */
38
+ point(id: string | undefined, via: ReviewVia, from?: string): void;
39
+ /** A reader's answer, `chord` where a key gave it. False where it could not be made. */
40
+ act(id: string, action: ReviewAction, chord?: boolean): boolean;
41
+ /** An answer leaf `key` has already given, the item listed or not: a reopen and a redo's
42
+ * accept arrive for items the host may have taken off or not yet put back. */
43
+ heard(id: string, action: ReviewAction, key: string, chord?: boolean): void;
44
+ /** A leaf is writing an answer, and is done: its edit is the answer's, not the reader's. */
45
+ writing(on: boolean): void;
46
+ reveal(id: string): void;
47
+ }
48
+ export interface ReviewSource {
49
+ items(): readonly ReviewItem[] | undefined;
50
+ /** The leaf key a field path names, `undefined` for one the editor does not hold. */
51
+ keyFor(path: DocPath): string | undefined;
52
+ /** Whether a path names a field itself, and not a place inside one. */
53
+ exact(path: DocPath): boolean;
54
+ /** A path naming a place inside a field: the field's leaf key, the steps from it, and the
55
+ * value it holds. `undefined` for a place its schema does not declare. */
56
+ nested(path: DocPath): {
57
+ key: string;
58
+ steps: readonly PathStep[];
59
+ value: unknown;
60
+ } | undefined;
61
+ /** The value the field at `path` holds, as its control is handed it. */
62
+ valueAt(path: DocPath): unknown;
63
+ /** The key of the prose leaf a rich text field's or a body's path names, `undefined` for
64
+ * one the editor does not hold. */
65
+ leafFor(path: DocPath): string | undefined;
66
+ /** A reader's answer, to make and to pass on: `item` where the list holds it, `key` the
67
+ * leaf that gave it where one did. `false` where it could not be made, and the item
68
+ * stands. */
69
+ answer(id: string, item: ReviewItem | undefined, action: ReviewAction, key?: string): boolean;
70
+ /** An answer a chord gave, once it is made. */
71
+ chorded?(id: string): void;
72
+ writing?(on: boolean): void;
73
+ }
74
+ export declare function createReview(source: ReviewSource): Review & {
75
+ dispose(): void;
76
+ };
77
+ export declare function setReview(review: Review): void;
78
+ /** The editor's review state, or {@link NO_REVIEW} off-tree. */
79
+ export declare function review(): Review;
@@ -0,0 +1,226 @@
1
+ // The editor's review state: the host's list split by where each item draws, and the one item
2
+ // under attention.
3
+ //
4
+ // Attention has three sources and one answer. The pointer over an item, the caret in one and a
5
+ // host's reveal each name an item; the lit one is the revealed, else the caret's, else the
6
+ // pointer's.
7
+ //
8
+ // An answered item is drawn no more, from the answer and not from the host's next list: the
9
+ // reader's press shows at once however long the host takes to hand over a list without it.
10
+ import { getContext, setContext } from 'svelte';
11
+ import { SvelteMap } from 'svelte/reactivity';
12
+ import { byBlocks, locate, onField, sameValue } from './review.js';
13
+ /** How long a revealed item stays lit, in ms. */
14
+ export const REVEAL_MS = 2400;
15
+ export function createReview(source) {
16
+ /** The ids answered, each until a list arrives without it. */
17
+ const answered = new Set();
18
+ let answers = $state(0);
19
+ const items = $derived.by(() => {
20
+ const listed = source.items() ?? [];
21
+ if (!answers)
22
+ return listed;
23
+ const ids = new Set(listed.map((item) => item.id));
24
+ for (const id of answered)
25
+ if (!ids.has(id))
26
+ answered.delete(id);
27
+ return answered.size ? listed.filter((item) => !answered.has(item.id)) : listed;
28
+ });
29
+ const drawn = new SvelteMap();
30
+ const byId = $derived(new Map(items.map((item) => [item.id, item])));
31
+ /** The items each prose leaf draws, by leaf key. */
32
+ const text = $derived.by(() => {
33
+ const own = new Map();
34
+ for (const item of items) {
35
+ if (!byBlocks(item))
36
+ continue;
37
+ const key = source.leafFor(item.field);
38
+ if (key === undefined)
39
+ continue;
40
+ let list = own.get(key);
41
+ if (!list)
42
+ own.set(key, (list = []));
43
+ list.push(item);
44
+ }
45
+ return own;
46
+ });
47
+ const placed = $derived.by(() => {
48
+ const out = new Map();
49
+ const undrawn = [];
50
+ /** The nested proposals whose `before` the field no longer holds. */
51
+ const moved = new Set();
52
+ const claims = new Map();
53
+ for (const item of items) {
54
+ if (byBlocks(item)) {
55
+ if (source.leafFor(item.field) === undefined)
56
+ undrawn.push({ id: item.id, reason: 'field' });
57
+ continue;
58
+ }
59
+ if (!onField(item))
60
+ continue;
61
+ if (!source.exact(item.field)) {
62
+ const deep = source.nested(item.field);
63
+ if (!deep) {
64
+ undrawn.push({ id: item.id, reason: 'field' });
65
+ continue;
66
+ }
67
+ const claimed = claims.get(deep.key) ?? new Set();
68
+ claims.set(deep.key, claimed);
69
+ const held = out.get(deep.key) ?? {};
70
+ const { steps, found } = locate(deep.value, deep.steps, item, claimed);
71
+ if ((held.nested ?? []).some((e) => sameValue(e.steps, steps))) {
72
+ undrawn.push({ id: item.id, reason: 'taken' });
73
+ continue;
74
+ }
75
+ if (!found)
76
+ moved.add(item.id);
77
+ held.nested = [...(held.nested ?? []), { steps, item }];
78
+ out.set(deep.key, held);
79
+ continue;
80
+ }
81
+ const key = source.keyFor(item.field);
82
+ if (key === undefined) {
83
+ undrawn.push({ id: item.id, reason: 'field' });
84
+ continue;
85
+ }
86
+ const held = out.get(key) ?? {};
87
+ if (held.proposed !== undefined) {
88
+ undrawn.push({ id: item.id, reason: 'taken' });
89
+ continue;
90
+ }
91
+ held.proposed = item;
92
+ out.set(key, held);
93
+ }
94
+ return { fields: out, undrawn, moved };
95
+ });
96
+ const fields = $derived(placed.fields);
97
+ // A proposal made from a value the field no longer holds would write over the reader's.
98
+ const outdated = $derived.by(() => {
99
+ const out = new Set(placed.moved);
100
+ for (const { proposed } of fields.values())
101
+ if (proposed &&
102
+ 'before' in proposed &&
103
+ !sameValue(source.valueAt(proposed.field), proposed.before))
104
+ out.add(proposed.id);
105
+ return out;
106
+ });
107
+ let pointer = $state();
108
+ let caret = $state();
109
+ let caretFrom;
110
+ let revealed = $state();
111
+ let fade;
112
+ const lit = $derived([revealed, caret, pointer].find((id) => id !== undefined && byId.has(id)));
113
+ const answering = (id, action, chord) => {
114
+ if (action === 'reopen')
115
+ answered.delete(id);
116
+ else
117
+ answered.add(id);
118
+ answers++;
119
+ if (chord)
120
+ source.chorded?.(id);
121
+ };
122
+ const self = {
123
+ get items() {
124
+ return items;
125
+ },
126
+ textAt: (key) => text.get(key) ?? NO_ITEMS,
127
+ get reports() {
128
+ return drawn;
129
+ },
130
+ get fieldUndrawn() {
131
+ return placed.undrawn;
132
+ },
133
+ get fieldOutdated() {
134
+ return outdated;
135
+ },
136
+ get lit() {
137
+ return lit;
138
+ },
139
+ at: (key) => fields.get(key) ?? {},
140
+ marks: (key) => fields.has(key) || !!drawn.get(key)?.ids.length,
141
+ keyOf(id) {
142
+ const item = byId.get(id);
143
+ if (!item)
144
+ return undefined;
145
+ if (byBlocks(item)) {
146
+ const key = source.leafFor(item.field);
147
+ return key !== undefined && drawn.get(key)?.ids.includes(id) ? key : undefined;
148
+ }
149
+ if ('field' in item)
150
+ return source.keyFor(item.field);
151
+ for (const [key, report] of drawn)
152
+ if (report.ids.includes(id))
153
+ return key;
154
+ return undefined;
155
+ },
156
+ drew(key, report) {
157
+ if (report.ids.length || report.undrawn.length)
158
+ drawn.set(key, report);
159
+ else
160
+ drawn.delete(key);
161
+ },
162
+ point(id, via, from) {
163
+ if (via === 'pointer') {
164
+ pointer = id;
165
+ return;
166
+ }
167
+ if (id === undefined && from !== undefined && caretFrom !== from)
168
+ return;
169
+ caretFrom = from;
170
+ caret = id;
171
+ if (id !== undefined)
172
+ revealed = undefined;
173
+ },
174
+ act(id, action, chord) {
175
+ const item = byId.get(id);
176
+ if (!item || !source.answer(id, item, action))
177
+ return false;
178
+ answering(id, action, chord);
179
+ return true;
180
+ },
181
+ writing: (on) => source.writing?.(on),
182
+ heard(id, action, key, chord) {
183
+ if (source.answer(id, byId.get(id), action, key))
184
+ answering(id, action, chord);
185
+ },
186
+ reveal(id) {
187
+ clearTimeout(fade);
188
+ revealed = id;
189
+ fade = setTimeout(() => {
190
+ if (revealed === id)
191
+ revealed = undefined;
192
+ }, REVEAL_MS);
193
+ },
194
+ dispose() {
195
+ clearTimeout(fade);
196
+ }
197
+ };
198
+ return self;
199
+ }
200
+ const KEY = Symbol('qm.review');
201
+ export function setReview(review) {
202
+ setContext(KEY, review);
203
+ }
204
+ const NO_ITEMS = [];
205
+ /** What a component rendered outside the editor gets: nothing to draw. */
206
+ const NO_REVIEW = {
207
+ items: [],
208
+ textAt: () => NO_ITEMS,
209
+ reports: new Map(),
210
+ fieldUndrawn: [],
211
+ fieldOutdated: new Set(),
212
+ lit: undefined,
213
+ at: () => ({}),
214
+ marks: () => false,
215
+ keyOf: () => undefined,
216
+ drew() { },
217
+ point() { },
218
+ act: () => false,
219
+ heard() { },
220
+ writing() { },
221
+ reveal() { }
222
+ };
223
+ /** The editor's review state, or {@link NO_REVIEW} off-tree. */
224
+ export function review() {
225
+ return getContext(KEY) ?? NO_REVIEW;
226
+ }