grayling 0.0.0-stage → 0.1.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.
Files changed (55) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +268 -2
  3. package/dist/index.d.ts +290 -0
  4. package/dist/index.js +50 -0
  5. package/dist/node/archive.d.ts +4 -0
  6. package/dist/node/archive.js +115 -0
  7. package/dist/node/client.d.ts +188 -0
  8. package/dist/node/client.js +669 -0
  9. package/dist/node/connection.d.ts +56 -0
  10. package/dist/node/connection.js +240 -0
  11. package/dist/node/errors.d.ts +14 -0
  12. package/dist/node/errors.js +13 -0
  13. package/dist/node/handler.d.ts +56 -0
  14. package/dist/node/handler.js +208 -0
  15. package/dist/node/index.d.ts +15 -0
  16. package/dist/node/index.js +14 -0
  17. package/dist/node/paths.d.ts +51 -0
  18. package/dist/node/paths.js +115 -0
  19. package/dist/node/program.d.ts +74 -0
  20. package/dist/node/program.js +385 -0
  21. package/dist/node/state.d.ts +7 -0
  22. package/dist/node/state.js +12 -0
  23. package/dist/node/suggest.d.ts +13 -0
  24. package/dist/node/suggest.js +87 -0
  25. package/dist/node/types.d.ts +142 -0
  26. package/dist/node/types.js +13 -0
  27. package/dist/node/version.d.ts +10 -0
  28. package/dist/node/version.js +50 -0
  29. package/dist/node/wire.d.ts +32 -0
  30. package/dist/node/wire.js +44 -0
  31. package/dist/react/index.d.ts +121 -0
  32. package/dist/react/index.js +205 -0
  33. package/dist/web/announce.d.ts +51 -0
  34. package/dist/web/announce.js +141 -0
  35. package/dist/web/attach.d.ts +77 -0
  36. package/dist/web/attach.js +89 -0
  37. package/dist/web/box.d.ts +57 -0
  38. package/dist/web/box.js +150 -0
  39. package/dist/web/dom.d.ts +80 -0
  40. package/dist/web/dom.js +265 -0
  41. package/dist/web/index.d.ts +8 -0
  42. package/dist/web/index.js +8 -0
  43. package/dist/web/logic.d.ts +308 -0
  44. package/dist/web/logic.js +467 -0
  45. package/dist/web/overlay.d.ts +56 -0
  46. package/dist/web/overlay.js +211 -0
  47. package/dist/web/style.css +193 -0
  48. package/dist/web/suggester.d.ts +189 -0
  49. package/dist/web/suggester.js +696 -0
  50. package/dist/web/ui.d.ts +65 -0
  51. package/dist/web/ui.js +32 -0
  52. package/dist/web/usage.d.ts +39 -0
  53. package/dist/web/usage.js +84 -0
  54. package/package.json +89 -4
  55. package/program.json +35 -0
@@ -0,0 +1,467 @@
1
+ // Word suggestions, the rules without a screen: when to ask, what to send, what typing the grey letters does to what
2
+ // shows, what Tab and → take. No React and no DOM here, so each rule is a plain function a test can run;
3
+ // suggester.ts keeps the state of one text box and calls them, and an adapter (a textarea, an editor) only reads and
4
+ // writes its field.
5
+ import { SUGGEST_BINS, SUGGEST_COUNT_KEYS, SUGGEST_PLANS, SUGGEST_USAGE_CAP } from '../index.js';
6
+ /** What the engine is asked for in each place. */
7
+ export const surfaceOf = (p) => (p === 'chat' ? 'chat' : p === 'docs' ? 'doc' : 'text');
8
+ /** How much of each text goes to the engine (its last characters). */
9
+ export const MAX_CHARS = 2000;
10
+ export const tail = (s, n = MAX_CHARS) => (s && s.length > n ? s.slice(s.length - n) : s);
11
+ /** The whole text of a box: what is before the caret, selected, and after it. */
12
+ export const textOf = (f) => f.before + (f.picked ?? '') + f.after;
13
+ /** Whether suggestions show in `place` at all: on, set up (or loading), the place on in the settings, and this browser's switch on. */
14
+ export function allowed(g, place) {
15
+ if (!g.enabled || !g.browser)
16
+ return false;
17
+ if (g.state !== 'ready' && g.state !== 'starting')
18
+ return false;
19
+ return g.where[place];
20
+ }
21
+ /** A letter, digit or mark of any script, or an apostrophe inside a word. */
22
+ const WORD = /[\p{L}\p{N}\p{M}'’]/u;
23
+ const lastChar = (s) => (s ? Array.from(s.slice(-2)).pop() : '');
24
+ /** The rest of a line after the caret is only whitespace (or nothing): the grey text would sit on no other text. */
25
+ const lineEnds = (after) => !after || /^[ \t]*(\r?\n|$)/.test(after);
26
+ /** The first character (a whole code point) of a text, '' for none. */
27
+ const firstChar = (s) => (s ? String.fromCodePoint(s.codePointAt(0)) : '');
28
+ /**
29
+ * Whether the caret is where a word could be suggested, whatever stands after it: collapsed, not mid-composition, some text
30
+ * before it, right after a letter (the end of a word) or after whitespace (a new word), and not in an @file, a /command or
31
+ * an address.
32
+ */
33
+ function wordPlace(f) {
34
+ if (f.selected || f.composing || !/\S/.test(f.before))
35
+ return false;
36
+ const c = lastChar(f.before);
37
+ // A file or command being picked from the box's @ and / menus, or an address, is not a word to complete.
38
+ const token = /\S*$/.exec(f.before)[0];
39
+ if (token && (/^[@/]/.test(token) || token.includes('://')))
40
+ return false;
41
+ return /\s/.test(c) || WORD.test(c);
42
+ }
43
+ /**
44
+ * Whether the caret is where a suggestion makes sense: collapsed, not mid-composition, nothing more on its line
45
+ * (the grey text would sit on top of it), and right after a letter (the end of a word) or after whitespace (a new
46
+ * word). After punctuation or nothing at all there is no word to suggest, nor in an @file, a /command or an address.
47
+ */
48
+ export function atWordEnd(f) {
49
+ return wordPlace(f) && lineEnds(f.after);
50
+ }
51
+ /**
52
+ * Where a suggestion goes at the caret: `end` (grey text after it: nothing more on the line), `mid` (a chip over the caret: more
53
+ * text follows on the line), or null. Inside a text only after typing there (`typed`), and never before a letter of the same
54
+ * word, unless the caret was put right before that word to put a new one in (`seam`).
55
+ */
56
+ export function spotOf(f, o) {
57
+ if (atWordEnd(f))
58
+ return 'end';
59
+ if (!o.typed || lineEnds(f.after) || !wordPlace(f))
60
+ return null;
61
+ if (WORD.test(firstChar(f.after)) && !o.seam)
62
+ return null;
63
+ return 'mid';
64
+ }
65
+ /** Whether a new word is about to be put in before the word that follows the caret. */
66
+ export const seamOf = (f) => (!f.before || /\s$/.test(f.before)) && WORD.test(firstChar(f.after));
67
+ const placeAt = (f, typed = false) => ({ before: f.before, after: f.after, id: f.id, seam: seamOf(f), typed });
68
+ /**
69
+ * The caret's place after a change from `prev` to `cur`: the same while the text after the caret and the id stay, and the text
70
+ * before still begins as it did when the caret came there; typing there (`grown`: 1 to TYPED_MAX characters) or Tab (`tab`)
71
+ * makes it typed. Any other change (a caret move, Backspace, another box) starts over.
72
+ */
73
+ export function placeOf(p, prev, cur, o) {
74
+ if (!p || p.after !== cur.after || p.id !== cur.id || !cur.before.startsWith(p.before) || backspaced(prev, cur))
75
+ return placeAt(cur);
76
+ return p.typed || !(o.grown || o.tab) ? p : { ...p, typed: true };
77
+ }
78
+ /** What goes to the engine for a text box in `place` (chat: with the reply it answers and the earlier message). */
79
+ export function requestOf(place, f, ctx, client) {
80
+ const r = { surface: surfaceOf(place), text: tail(f.before), client };
81
+ if (place === 'chat') {
82
+ if (ctx.reply)
83
+ r.reply = tail(ctx.reply);
84
+ if (ctx.prev)
85
+ r.prev = tail(ctx.prev);
86
+ if (ctx.session)
87
+ r.session = ctx.session;
88
+ }
89
+ else if (place === 'docs' && ctx.doc)
90
+ r.doc = tail(ctx.doc);
91
+ return r;
92
+ }
93
+ /** The most characters after the caret that go to the engine. */
94
+ export const AFTER_MAX = 300;
95
+ /** What goes to the engine for a suggestion inside a text: the same, with the text after the caret (and `apart` when a new word is put in before the next one). */
96
+ export function midRequestOf(place, f, ctx, client, seam) {
97
+ const r = requestOf(place, f, ctx, client);
98
+ r.after = f.after.slice(0, AFTER_MAX);
99
+ if (seam)
100
+ r.apart = true;
101
+ return r;
102
+ }
103
+ /** What goes to the engine to check a word alone: the text before the caret, and what follows when a word goes on after it or more text stands on the line. */
104
+ export function fixRequestOf(place, f, ctx, client) {
105
+ const r = requestOf(place, f, ctx, client);
106
+ r.want = 'fix';
107
+ if (f.after && (WORD.test(firstChar(f.after)) || !lineEnds(f.after)))
108
+ r.after = f.after.slice(0, AFTER_MAX);
109
+ return r;
110
+ }
111
+ /** What an answer shows: nothing when it says not to (or has nothing), `word` when `show`, `next` also when `showNext`. */
112
+ export function greyOf(r) {
113
+ if (!r || !r.show)
114
+ return null;
115
+ const word = r.word ?? '';
116
+ const next = r.showNext ? (r.next ?? '') : '';
117
+ return word || next ? { word, next } : null;
118
+ }
119
+ /**
120
+ * What shows right after whitespace that was typed: never more whitespace (a second space). The grey text loses the
121
+ * whitespace it begins with, and when the word itself is empty the next word becomes the word.
122
+ */
123
+ export function afterSpace(g, before) {
124
+ if (!g || !/\s$/.test(before))
125
+ return g;
126
+ let word = g.word.replace(/^\s+/, '');
127
+ let next = g.next;
128
+ if (!word)
129
+ (word = next.replace(/^\s+/, '')), (next = '');
130
+ return word || next ? { word, next } : null;
131
+ }
132
+ /** Whether two things to show read the same (an answer that only repeats what is on screen changes nothing). */
133
+ export const sameGrey = (a, b) => (a?.word ?? '') + (a?.next ?? '') === (b?.word ?? '') + (b?.next ?? '');
134
+ /**
135
+ * What typing `typed` leaves of what shows: the letters it shows are used up, with no new request. undefined when
136
+ * the letters differ (a new request, then); null when all of it has been typed. Once the rest of the word is typed
137
+ * the next word shows as it is; once its first letters are typed it is the word being typed, in the word's grey.
138
+ */
139
+ export function consume(g, typed) {
140
+ if (!typed)
141
+ return g;
142
+ const all = g.word + g.next;
143
+ if (!all.startsWith(typed))
144
+ return undefined;
145
+ const rest = all.slice(typed.length);
146
+ if (!rest)
147
+ return null;
148
+ if (typed.length > g.word.length)
149
+ return { word: rest, next: '' };
150
+ const w = g.word.length - typed.length;
151
+ return { word: rest.slice(0, w), next: rest.slice(w) };
152
+ }
153
+ /** What Tab takes: the word, and the next word becomes the one shown (the word's grey); with no word left, the next word. */
154
+ export function takeWord(g) {
155
+ if (g.word)
156
+ return { insert: g.word, grey: g.next ? { word: g.next, next: '' } : null };
157
+ return { insert: g.next, grey: null };
158
+ }
159
+ /** What → at the end of the text takes: the word and the next one. */
160
+ export const takeAll = (g) => g.word + g.next;
161
+ /**
162
+ * What was typed between two states of a box, when the text after the caret did not change and the text before it
163
+ * only grew: that growth ('' when nothing changed). null when it is anything else (Backspace, a paste over a
164
+ * selection, a caret move, a different text).
165
+ */
166
+ export function typedBetween(prev, cur) {
167
+ if (!prev)
168
+ return null;
169
+ if (prev.after !== cur.after || prev.id !== cur.id)
170
+ return null;
171
+ if (!cur.before.startsWith(prev.before))
172
+ return null;
173
+ return cur.before.slice(prev.before.length);
174
+ }
175
+ /** Whether the caret is just after whitespace: a new word begins here (Esc's hiding lasts until it does). */
176
+ export const startsWord = (f) => /\s$/.test(f.before);
177
+ // ---- what suggestions did: counts only, never any text (shared/suggest.ts SuggestCounts)
178
+ /** No counts. */
179
+ export const noCounts = () => ({ shown: 0, taken: 0, typedThrough: 0, wrong: 0, saved: 0, typedChars: 0, midShown: 0, midTaken: 0, midThrough: 0, midWrong: 0, midSaved: 0, fixShown: 0, fixTaken: 0, fixKept: 0 });
180
+ /** The counts of a suggestion shown inside a text, in the keys of its own (the characters typed are the box's, not the suggestion's). */
181
+ export function toMid(c) {
182
+ const { shown, taken, typedThrough, wrong, saved, ...rest } = c;
183
+ const out = { ...rest };
184
+ if (shown)
185
+ out.midShown = (out.midShown ?? 0) + shown;
186
+ if (taken)
187
+ out.midTaken = (out.midTaken ?? 0) + taken;
188
+ if (typedThrough)
189
+ out.midThrough = (out.midThrough ?? 0) + typedThrough;
190
+ if (wrong)
191
+ out.midWrong = (out.midWrong ?? 0) + wrong;
192
+ if (saved)
193
+ out.midSaved = (out.midSaved ?? 0) + saved;
194
+ return out;
195
+ }
196
+ /** `a` and `b` added up. */
197
+ export function addCounts(a, b) {
198
+ const out = { ...a };
199
+ for (const k of SUGGEST_COUNT_KEYS)
200
+ out[k] += b[k] ?? 0;
201
+ return out;
202
+ }
203
+ /** Whether any count is more than 0. */
204
+ export const hasCounts = (c) => SUGGEST_COUNT_KEYS.some((k) => c[k] > 0);
205
+ /**
206
+ * `c` cut into pieces the engine accepts: no count above `cap` in any one (the rest goes in the next piece). Nothing
207
+ * for no counts.
208
+ */
209
+ export function splitCounts(c, cap = SUGGEST_USAGE_CAP) {
210
+ const out = [];
211
+ const left = { ...c };
212
+ while (hasCounts(left)) {
213
+ const piece = noCounts();
214
+ for (const k of SUGGEST_COUNT_KEYS) {
215
+ piece[k] = Math.min(left[k], cap);
216
+ left[k] -= piece[k];
217
+ }
218
+ out.push(piece);
219
+ }
220
+ return out;
221
+ }
222
+ /** The characters of a text, as a person counts them (an emoji or a letter with its accents is one). */
223
+ export const charCount = (s) => Array.from(s).length;
224
+ /** The most characters one change of a box may add and still be counted as typing (more is a paste or dictation). */
225
+ export const TYPED_MAX = 4;
226
+ /** What a change that grew the text before the caret by `typed` ('' or null: not a growth) adds to the characters typed: 1 to 4, else 0. */
227
+ export function typedChars(typed) {
228
+ if (!typed)
229
+ return 0;
230
+ const n = charCount(typed);
231
+ return n <= TYPED_MAX ? n : 0;
232
+ }
233
+ /** The keystrokes one Tab or → saved: the characters it inserted, minus the key pressed (never below 0). */
234
+ export const savedBy = (inserted) => Math.max(0, charCount(inserted) - 1);
235
+ /** What Tab or → did to the counts: the words taken (→ takes the word and the next: two), and the keystrokes saved by what it inserted. */
236
+ export function takenCounts(g, insert, all) {
237
+ const words = all && g.word && g.next ? 2 : 1;
238
+ // A second word taken without ever being the grey word was shown too (dimmer), so right is never more than shown.
239
+ return { ...noCounts(), taken: words, shown: words - 1, saved: savedBy(insert) };
240
+ }
241
+ /**
242
+ * The verdict on typing `typed` (a growth of the text before the caret) while `g` showed: the letters it shows used up
243
+ * are right once a whole word is typed (its last letter, or the space after it, typed in the same change); a letter
244
+ * that differs is wrong. The unit is the word, or the next word when the word is empty (it is all that shows).
245
+ */
246
+ export function judgeTyped(g, typed) {
247
+ const rest = consume(g, typed);
248
+ if (rest === undefined)
249
+ return { through: 0, fresh: false, wrong: true };
250
+ const n = charCount(typed);
251
+ const w = charCount(g.word);
252
+ const all = w + charCount(g.next);
253
+ if (!w)
254
+ return { through: n >= all ? 1 : 0, fresh: false, wrong: false };
255
+ if (n < w)
256
+ return { through: 0, fresh: false, wrong: false };
257
+ if (!g.next)
258
+ return { through: 1, fresh: false, wrong: false };
259
+ // The word is done and the next one is now the word being typed (or is typed too).
260
+ return { through: n >= all ? 2 : 1, fresh: true, wrong: false };
261
+ }
262
+ /** Whether `cur` is `prev` with some characters just before the caret deleted (Backspace, or Ctrl+Backspace), nothing else changed. */
263
+ export function backspaced(prev, cur) {
264
+ return !!prev && prev.after === cur.after && prev.id === cur.id && cur.before.length < prev.before.length && prev.before.startsWith(cur.before);
265
+ }
266
+ /**
267
+ * The guess an answer about `base` holds, shown or not: the letters of the word (without the space already typed) and
268
+ * how sure. null when it has no word, no plan or no confidence (there is nothing to judge).
269
+ */
270
+ export function guessOf(base, r) {
271
+ if (!r || !r.plan || !(r.conf > 0))
272
+ return null;
273
+ let word = r.word ?? '';
274
+ if (/\s$/.test(base.before))
275
+ word = word.replace(/^\s+/, '');
276
+ return word.trim() ? { base, word, plan: r.plan, conf: r.conf } : null;
277
+ }
278
+ /**
279
+ * Whether the guess `word` was right, from what was typed since it was asked (`typed`): true once the word is typed
280
+ * and something that ends it (a space, punctuation) follows; false once the person finished another word; undefined
281
+ * while they are still typing it.
282
+ */
283
+ export function verdictOf(word, typed) {
284
+ const ends = (s) => Array.from(s).some((c) => !WORD.test(c));
285
+ if (typed.startsWith(word)) {
286
+ const rest = typed.slice(word.length);
287
+ if (!rest)
288
+ return undefined;
289
+ // (A guess that ends in punctuation has ended the word itself.)
290
+ return !WORD.test(String.fromCodePoint(rest.codePointAt(0))) || !WORD.test(lastChar(word));
291
+ }
292
+ let i = 0;
293
+ while (i < typed.length && typed[i] === word[i])
294
+ i++;
295
+ if (i === typed.length)
296
+ return undefined;
297
+ return ends(typed.slice(i)) ? false : undefined;
298
+ }
299
+ /** The bin a confidence falls in (0..1 cut in SUGGEST_BINS equal parts). */
300
+ export const binOf = (conf) => Math.max(0, Math.min(SUGGEST_BINS - 1, Math.floor(conf * SUGGEST_BINS)));
301
+ const emptyBins = () => ({ right: new Array(SUGGEST_BINS).fill(0), total: new Array(SUGGEST_BINS).fill(0) });
302
+ /** One judged guess added to the counts (a bin never past what one request may send). */
303
+ export function addVerdict(j, plan, conf, right) {
304
+ const b = (j[plan] ??= emptyBins());
305
+ const i = binOf(conf);
306
+ if (b.total[i] >= SUGGEST_USAGE_CAP)
307
+ return;
308
+ b.total[i]++;
309
+ if (right)
310
+ b.right[i]++;
311
+ }
312
+ /** `add` put into `into` (a batch that could not be sent, joining the next). */
313
+ export function mergeJudged(into, add) {
314
+ for (const plan of SUGGEST_PLANS) {
315
+ const a = add[plan];
316
+ if (!a)
317
+ continue;
318
+ const b = (into[plan] ??= emptyBins());
319
+ for (let i = 0; i < SUGGEST_BINS; i++) {
320
+ const n = Math.min(a.total[i], SUGGEST_USAGE_CAP - b.total[i]);
321
+ b.total[i] += n;
322
+ b.right[i] += Math.min(a.right[i], n);
323
+ }
324
+ }
325
+ }
326
+ export const hasJudged = (j) => SUGGEST_PLANS.some((p) => j[p]?.total.some((n) => n > 0));
327
+ /** A share as a whole percent (0 when there is nothing to divide by). */
328
+ const percent = (part, whole) => (whole > 0 ? Math.round((100 * part) / whole) : 0);
329
+ /** The numbers a settings page can show for a set of counts: keystrokes saved and their share of the typing, suggestions right of those judged (at the end of a text and inside it together). */
330
+ export function summarise(c) {
331
+ const taken = c.taken + c.midTaken;
332
+ const right = taken + c.typedThrough + c.midThrough;
333
+ const judged = right + c.wrong + c.midWrong;
334
+ const saved = c.saved + c.midSaved;
335
+ return { saved, savedShare: percent(saved, c.typedChars + saved), right, judged, rightShare: percent(right, judged), taken };
336
+ }
337
+ /** The most marks a box keeps (the oldest go). */
338
+ export const MARKS_MAX = 20;
339
+ /** The characters kept before a mark's word. */
340
+ export const LEFT_MAX = 16;
341
+ /** The letters an open mark's word may have gone on by. */
342
+ export const OPEN_MORE = 3;
343
+ /** The most words typed after a mark for its chip to still show. */
344
+ export const CHIP_WORDS = 6;
345
+ const isWord = (c) => !!c && WORD.test(c);
346
+ /** The `left` a mark found at `start` keeps. */
347
+ export const leftOf = (text, start) => text.slice(Math.max(0, start - LEFT_MAX), start);
348
+ /**
349
+ * Where a mark stands in a text: the first place where its word stands as a whole word (no letter on either side), with
350
+ * the text before it ending as `left`; an open mark's word may go on by up to 3 letters. null when it stands nowhere
351
+ * now (the word was changed, or what was before it).
352
+ */
353
+ export function findMark(text, m) {
354
+ if (!m.typed)
355
+ return null;
356
+ for (let i = text.indexOf(m.typed); i >= 0; i = text.indexOf(m.typed, i + 1)) {
357
+ if (isWord(lastChar(text.slice(Math.max(0, i - 2), i))) || !text.slice(0, i).endsWith(m.left))
358
+ continue;
359
+ let end = i + m.typed.length;
360
+ if (m.open) {
361
+ for (let n = 0; n < OPEN_MORE && isWord(firstChar(text.slice(end))); n++)
362
+ end += firstChar(text.slice(end)).length;
363
+ }
364
+ if (!isWord(firstChar(text.slice(end))))
365
+ return { start: i, end };
366
+ }
367
+ return null;
368
+ }
369
+ /**
370
+ * Where a stretch of `from` stands in `to`, the same text a change later: before the change as it was, after it moved along, and
371
+ * nowhere (null) when the change touches it. What is drawn keeps to its words until the marks are looked for again.
372
+ */
373
+ export function moveSpot(s, from, to) {
374
+ if (from === to)
375
+ return s;
376
+ const most = Math.min(from.length, to.length);
377
+ let p = 0;
378
+ while (p < most && from[p] === to[p])
379
+ p++;
380
+ if (s.end <= p)
381
+ return s;
382
+ let q = 0;
383
+ while (q < most - p && from[from.length - 1 - q] === to[to.length - 1 - q])
384
+ q++;
385
+ const by = to.length - from.length;
386
+ return s.start >= from.length - q ? { start: s.start + by, end: s.end + by } : null;
387
+ }
388
+ /** The marks that stand in a text, with their places (the others are gone). */
389
+ export const placeMarks = (text, marks) => marks.flatMap((mark) => {
390
+ const spot = findMark(text, mark);
391
+ return spot ? [{ mark, spot }] : [];
392
+ });
393
+ const newest = (list) => list.reduce((a, b) => (!a || b.mark.at > a.mark.at ? b : a), null);
394
+ /** The words in a text. */
395
+ const wordsIn = (s) => (s.match(/[\p{L}\p{N}\p{M}'’]+/gu) ?? []).length;
396
+ /** Whether the caret has left a mark behind: a line break, or more than 6 words, stand between its end and the caret. */
397
+ export function leftBehind(text, spot, caret) {
398
+ if (spot.end >= caret)
399
+ return false;
400
+ const gap = text.slice(spot.end, caret);
401
+ return /[\r\n]/.test(gap) || wordsIn(gap) > CHIP_WORDS;
402
+ }
403
+ /**
404
+ * The mark whose chip shows: the one the caret is in or at the end of (the newest if several), else the newest one before the
405
+ * caret with at most 6 words after it and no line break between (`passed`: marks the caret has left, which show again only when
406
+ * the caret is in them).
407
+ */
408
+ export function currentMark(list, text, caret, passed) {
409
+ const inside = newest(list.filter((x) => x.spot.start <= caret && caret <= x.spot.end));
410
+ if (inside)
411
+ return inside;
412
+ return newest(list.filter((x) => x.spot.end < caret && !passed?.has(x.mark) && !leftBehind(text, x.spot, caret)));
413
+ }
414
+ /** Whether the caret is in a word or right after it: only characters that are no letters stand between the word's end and the caret. */
415
+ export const caretAtWord = (text, spot, caret) => caret >= spot.start && (caret <= spot.end || !Array.from(text.slice(spot.end, caret)).some(isWord));
416
+ /** Whether Tab is the chip's: the caret is in its word or right after it, or no grey word shows. */
417
+ export const fixOwnsTab = (text, spot, caret, greyShows) => !greyShows || caretAtWord(text, spot, caret);
418
+ /**
419
+ * The chips a box shows: one at a time, the one Tab is for, so that Tab never does what cannot be seen. A fix whose Tab is the
420
+ * grey word's keeps its chip beside grey text at the end of a text, but gives way to a word suggested inside it.
421
+ */
422
+ export function chipsOf(v) {
423
+ const mid = v.mid ? v.grey : null;
424
+ if (!v.fix || !mid)
425
+ return { fix: v.fix, mid };
426
+ return v.fix.tab ? { fix: v.fix, mid: null } : { fix: null, mid };
427
+ }
428
+ /** The word a caret is in or at the ends of (a letter right before it or right after it): where it stands, or null. */
429
+ export function wordAround(text, caret) {
430
+ let start = caret;
431
+ let end = caret;
432
+ while (start > 0 && isWord(lastChar(text.slice(Math.max(0, start - 2), start))))
433
+ start -= lastChar(text.slice(Math.max(0, start - 2), start)).length;
434
+ while (end < text.length && isWord(firstChar(text.slice(end))))
435
+ end += firstChar(text.slice(end)).length;
436
+ return start === end ? null : { start, end };
437
+ }
438
+ /** Whether a character that is no letter and no whitespace (punctuation, a closing bracket, a quote) was just typed right after a word. */
439
+ export function punctAfterWord(before) {
440
+ const c = lastChar(before);
441
+ if (!c || /\s/.test(c) || isWord(c))
442
+ return false;
443
+ return isWord(lastChar(before.slice(Math.max(0, before.length - c.length - 2), before.length - c.length)));
444
+ }
445
+ /** Whether a word has just ended before the caret: at most three characters that are no letters and no spaces stand after it. */
446
+ export const wordEnded = (before) => /[\p{L}\p{N}\p{M}'’][^\p{L}\p{N}\p{M}\s]{1,3}$/u.test(before);
447
+ /** Whether a word and one space after it are what stands right before the caret. */
448
+ export const spaceAfterWord = (before) => /[\p{L}\p{N}\p{M}'’]\s$/u.test(before);
449
+ /** A box as if the caret were one character into the word it stands at the beginning of (so that this word is the one at the caret, not the one before it). */
450
+ export function intoWord(f) {
451
+ const c = firstChar(f.after);
452
+ return isWord(c) && !isWord(lastChar(f.before)) ? { ...f, before: f.before + c, after: f.after.slice(c.length) } : f;
453
+ }
454
+ /** The words a caret rests at, remembered as the 16 characters before it and the word: the same word at the same place is asked about once. */
455
+ export const askedKey = (text, w) => `${leftOf(text, w.start)}\u0000${text.slice(w.start, w.end)}`;
456
+ /** The right word with the letters that differ from the typed word marked: outside the longest common prefix and suffix. */
457
+ export function diffLetters(typed, word) {
458
+ const a = Array.from(typed);
459
+ const b = Array.from(word);
460
+ let p = 0;
461
+ while (p < a.length && p < b.length && a[p] === b[p])
462
+ p++;
463
+ let q = 0;
464
+ while (q < a.length - p && q < b.length - p && a[a.length - 1 - q] === b[b.length - 1 - q])
465
+ q++;
466
+ return { pre: b.slice(0, p).join(''), mid: b.slice(p, b.length - q).join(''), post: b.slice(b.length - q).join('') };
467
+ }
@@ -0,0 +1,56 @@
1
+ import { type Attributes, type Classes, type Labels } from './ui.js';
2
+ import { type TextBox } from './dom.js';
3
+ import { type View } from './logic.js';
4
+ export interface OverlayOptions {
5
+ classes?: Partial<Classes>;
6
+ labels?: Partial<Labels>;
7
+ attributes?: Attributes;
8
+ /** The key drawn beside a word on a chip (default: a <kbd> with the label). */
9
+ kbd?: (label: string) => Node;
10
+ /** The person tapped the fix chip's word. */
11
+ onAccept: () => void;
12
+ /** The person tapped the fix chip's ×. */
13
+ onKeep: () => void;
14
+ /** The person tapped the chip over a text. */
15
+ onTake: () => void;
16
+ }
17
+ /** What the suggester told, and the text before the caret when it did. */
18
+ export interface Shown {
19
+ v: View;
20
+ before: string;
21
+ }
22
+ export declare class Overlay {
23
+ private ta;
24
+ private o;
25
+ private classes;
26
+ private labels;
27
+ private doc;
28
+ private grey;
29
+ private marks;
30
+ private markEl;
31
+ private caretEl;
32
+ private chip;
33
+ constructor(ta: TextBox, o: OverlayOptions);
34
+ /** A mirror laid over the box, right after it, and kept there. */
35
+ private mirror;
36
+ private drop;
37
+ /** The textarea's text, wrapped as it wraps it: its text up to the caret unseen, then the grey words. */
38
+ private drawGrey;
39
+ /** Exactly the box's text, its letters unseen, with each word typed wrong marked (so that nothing shifts), and an empty span at the caret where a chip points at. */
40
+ private drawMarks;
41
+ private kbd;
42
+ private part;
43
+ private fixContent;
44
+ private midContent;
45
+ /** A chip's fixed layer: one kind at a time (a new kind replaces the one before). */
46
+ private showChip;
47
+ private dropChip;
48
+ private static rectOf;
49
+ /** Draw what the suggester told, with the box's text as it is now. `known` is the text the places in `shown` are in. */
50
+ update(shown: Shown | null, known: string): void;
51
+ /** Nothing shows. */
52
+ clear(): void;
53
+ /** Put the layers where they belong again (the box moved or changed). */
54
+ place(): void;
55
+ destroy(): void;
56
+ }