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,696 @@
1
+ import { MARKS_MAX, TYPED_MAX, afterSpace, allowed, askedKey, backspaced, caretAtWord, charCount, consume, currentMark, findMark, fixOwnsTab, fixRequestOf, greyOf, guessOf, intoWord, judgeTyped, leftBehind, leftOf, midRequestOf, placeMarks, placeOf, punctAfterWord, requestOf, sameGrey, spaceAfterWord, spotOf, startsWord, takeAll, takeWord, takenCounts, textOf, toMid, typedBetween, typedChars, verdictOf, wordAround, wordEnded } from './logic.js';
2
+ export const DEBOUNCE_MS = 30;
3
+ /** The caret must rest this long in a word, which it came to by a caret move, before the word is checked. */
4
+ export const REST_MS = 500;
5
+ /** The words a caret rested at that are remembered, so that each is asked about once (and the places where a word still being typed was kept). */
6
+ const ASKED_MAX = 30;
7
+ /** The most fixes counted as shown that are remembered (the same word at the same place is counted once). */
8
+ const SHOWN_MAX = 200;
9
+ /** A request out this long is given up on at the next keystroke (a connection that hangs must not hold the box). */
10
+ export const MAX_FLIGHT_MS = 3000;
11
+ /** The most guesses at one time waiting for the word to be finished. */
12
+ const GUESSES_MAX = 24;
13
+ const same = (a, b) => !!a && !!b && a.before === b.before && a.after === b.after && (a.picked ?? '') === (b.picked ?? '') && a.id === b.id && !!a.selected === !!b.selected && !!a.composing === !!b.composing;
14
+ export class Suggester {
15
+ ad;
16
+ on;
17
+ out;
18
+ grey = null;
19
+ /** The grey word showing is a suggestion inside a text. */
20
+ mid = false;
21
+ /** What was last told (the view). */
22
+ view = { grey: null, mid: false, marks: [], fix: null };
23
+ /** The text its places are in: the box's when it was last read. */
24
+ text = '';
25
+ /** The words typed wrong that were found, in every box this one controls (an editor: every paragraph). */
26
+ marks = [];
27
+ /** Esc hid what shows: nothing shows until the next word begins. */
28
+ hidden = false;
29
+ last = null;
30
+ timer;
31
+ /** An ask for the word at the caret is waiting for its pause to end. */
32
+ waiting = false;
33
+ /** The pause before a fix of its own is asked for (punctuation typed, or the caret at rest). */
34
+ fixTimer;
35
+ /** That pause is for a word just ended. */
36
+ endedDue = false;
37
+ /** The box as it was when the word at the caret was last asked for (its answer may hold a fix: no other is needed there). */
38
+ allAt = null;
39
+ /** A fix of its own is wanted but a request is out: it is asked for when that answers. */
40
+ fixWait = null;
41
+ /** The request under way: what the box was when it was sent. */
42
+ flight = null;
43
+ /** The box as it was when it lost the focus. */
44
+ away = null;
45
+ inserting = false;
46
+ /** The grey word showing is a suggestion that has been counted as shown and has no outcome yet. */
47
+ live = false;
48
+ /** The person typed letters of what shows: an answer with nothing to show leaves it. */
49
+ confirmed = false;
50
+ /** The guesses waiting for the word to be finished, and the one of them whose letters show. */
51
+ guesses = [];
52
+ shownGuess = null;
53
+ /** The caret's place: whether a word is put in before another, and whether something was typed there. */
54
+ place = null;
55
+ /** The marks the caret has left (moved away from, or gone on by more than 6 words): their chip shows again only when the caret is in them. */
56
+ passed = new Set();
57
+ /** The words kept as typed in this box (lower case): never marked again. */
58
+ kept = new Set();
59
+ /** Where a word still being typed was kept: however it goes on there, it is not marked again. */
60
+ keptAt = [];
61
+ /** The fixes counted as shown (typed, word and what stood before it). */
62
+ shownFixes = [];
63
+ /** The words the caret rested at (the 16 characters before and the word) that were asked about. */
64
+ asked = [];
65
+ seq = 0;
66
+ told = '';
67
+ constructor(ad, on, out) {
68
+ this.ad = ad;
69
+ this.on = on;
70
+ this.out = out;
71
+ }
72
+ // ---- what is told: the grey word, the marks, and the chip
73
+ /** Which marks stand in the box as it is (`f`), with where. */
74
+ placed(f) {
75
+ return placeMarks(textOf(f), this.marks.filter((m) => m.id === f.id));
76
+ }
77
+ /** The mark whose chip shows now, if any. */
78
+ current(f) {
79
+ if (f.selected || f.composing || !this.fixOn())
80
+ return null;
81
+ return currentMark(this.placed(f), textOf(f), f.before.length, this.passed);
82
+ }
83
+ /** A fix's chip shows for the word the caret is in or right after: Tab is its, and no word is suggested inside a text there. */
84
+ fixHere(f) {
85
+ const c = this.current(f);
86
+ return !!c && caretAtWord(textOf(f), c.spot, f.before.length);
87
+ }
88
+ /** What the box shows now, told to whoever draws it when any of it changed. */
89
+ publish() {
90
+ const f = this.last;
91
+ const placed = f && this.fixOn() ? this.placed(f) : [];
92
+ const text = (this.text = f ? textOf(f) : '');
93
+ const cur = f && placed.length ? this.current(f) : null;
94
+ const v = {
95
+ grey: this.grey,
96
+ mid: !!this.grey && this.mid,
97
+ marks: placed.map((x) => x.spot).sort((a, b) => a.start - b.start),
98
+ fix: cur ? { start: cur.spot.start, end: cur.spot.end, typed: text.slice(cur.spot.start, cur.spot.end), word: cur.mark.word, tab: fixOwnsTab(text, cur.spot, f.before.length, !!this.grey) } : null,
99
+ };
100
+ const k = JSON.stringify(v);
101
+ if (k === this.told)
102
+ return;
103
+ this.told = k;
104
+ this.view = v;
105
+ this.on(v);
106
+ }
107
+ /** Count what suggestions did; a suggestion inside a text counts in the keys of its own. */
108
+ count(c) {
109
+ this.out.count?.(this.mid ? toMid(c) : c);
110
+ }
111
+ show(g, mid = false) {
112
+ if (g === this.grey || (g && this.grey && g.word === this.grey.word && g.next === this.grey.next)) {
113
+ if (!g || mid === this.mid)
114
+ return this.publish();
115
+ }
116
+ // Hidden with no verdict (Esc, a blur, a caret move, sending): neither right nor wrong.
117
+ if (!g)
118
+ (this.live = false), (this.shownGuess = null), (this.confirmed = false);
119
+ this.grey = g;
120
+ this.mid = !!g && mid;
121
+ this.publish();
122
+ }
123
+ /** A new suggestion is showing (inside a text, or at its end): counted as shown, and waiting for what the person does with it. */
124
+ began(mid = false) {
125
+ this.live = true;
126
+ this.shownGuess = null;
127
+ this.confirmed = false;
128
+ this.mid = mid;
129
+ this.count({ shown: 1 });
130
+ }
131
+ /** The suggestion showing has an outcome (taken, typed through, or something else typed): its guess is judged so. */
132
+ verdict(right) {
133
+ const g = this.shownGuess;
134
+ if (!g)
135
+ return;
136
+ this.shownGuess = null;
137
+ this.guesses = this.guesses.filter((x) => x !== g);
138
+ this.out.judge?.(g.plan, g.conf, right);
139
+ }
140
+ /** Keep the guess an answer about `asked` holds, to be judged (one per place in the word: a later one replaces it). */
141
+ note(asked, r) {
142
+ const g = this.out.judge ? guessOf(asked, r) : null;
143
+ if (!g)
144
+ return null;
145
+ this.guesses = this.guesses.filter((x) => x === this.shownGuess || x.base.before !== asked.before || x.base.after !== asked.after || x.base.id !== asked.id);
146
+ this.guesses.push(g);
147
+ while (this.guesses.length > GUESSES_MAX)
148
+ this.guesses.shift();
149
+ return g;
150
+ }
151
+ /** The letters showing are the rest of `g`'s word (`typed` of it typed already): their outcome judges it. */
152
+ link(g, typed) {
153
+ if (g && this.grey?.word && g.word.slice(typed) === this.grey.word && this.guesses.includes(g))
154
+ this.shownGuess = g;
155
+ }
156
+ /** Judge the guesses whose word the person has finished by now. */
157
+ judgeGuesses(now) {
158
+ this.guesses = this.guesses.filter((g) => {
159
+ // (What shows is judged by what is done with it.)
160
+ if (g === this.shownGuess)
161
+ return true;
162
+ const typed = typedBetween(g.base, now);
163
+ // A second space or a new line where the word would begin: no word was typed there.
164
+ if (typed === null || (/\s$/.test(g.base.before) && /^\s/.test(typed)))
165
+ return false;
166
+ const right = verdictOf(g.word, typed);
167
+ if (right === undefined)
168
+ return true;
169
+ this.out.judge?.(g.plan, g.conf, right);
170
+ return false;
171
+ });
172
+ }
173
+ /** No verdict for the guesses waiting (a deletion, a caret move, a paste, the box gone). */
174
+ forget() {
175
+ this.guesses = [];
176
+ this.shownGuess = null;
177
+ }
178
+ /** The request under way is no longer wanted: its answer is not waited for, and the engine stops working on it. */
179
+ cancel() {
180
+ const fl = this.flight;
181
+ if (!fl)
182
+ return;
183
+ this.flight = null;
184
+ fl.ctl.abort();
185
+ this.unasked(fl);
186
+ }
187
+ /** A request that had no answer: the word the caret rested at may be asked about again. */
188
+ unasked(fl) {
189
+ if (fl.key)
190
+ this.asked = this.asked.filter((k) => k !== fl.key);
191
+ }
192
+ /** Every mark goes (the switch was turned off). */
193
+ dropMarks() {
194
+ this.marks = [];
195
+ this.passed.clear();
196
+ }
197
+ /** Stop what is under way and show nothing (a blur, a switch turned off, the box gone). The marks stay: they show again when the box has the focus. */
198
+ reset() {
199
+ clearTimeout(this.timer);
200
+ clearTimeout(this.fixTimer);
201
+ this.waiting = false;
202
+ this.endedDue = false;
203
+ this.fixWait = null;
204
+ this.cancel();
205
+ if (this.last)
206
+ this.away = this.last;
207
+ this.last = null;
208
+ this.place = null;
209
+ this.allAt = null;
210
+ this.forget();
211
+ this.show(null);
212
+ }
213
+ /** The box may have changed: keep what shows if the person typed exactly its letters, else ask again. */
214
+ sync() {
215
+ if (this.inserting)
216
+ return;
217
+ const f = this.ad.read();
218
+ const gate = this.out.gate();
219
+ const ok = allowed(gate, this.ad.place);
220
+ if (!ok || !gate.fixTypos)
221
+ this.dropMarks();
222
+ if (!f || !ok)
223
+ return this.reset();
224
+ // Suggest inside a text turned off while one shows.
225
+ if (this.mid && this.grey && !gate.midText)
226
+ this.show(null);
227
+ const prev = this.last;
228
+ if (same(prev, f))
229
+ return this.publish();
230
+ this.last = f;
231
+ if (this.hidden && startsWord(f))
232
+ this.hidden = false;
233
+ // The request under way stays while the box only grows from what it was asked about.
234
+ if (this.flight && (f.selected || typedBetween(this.flight.field, f) === null || Date.now() - this.flight.at > MAX_FLIGHT_MS))
235
+ this.cancel();
236
+ const typed = typedBetween(prev, f);
237
+ // What was typed here (not what Tab inserted, which `take` has already read; not a paste or dictation, nor an input method's text).
238
+ const grown = prev?.composing || f.composing ? 0 : typedChars(typed);
239
+ if (grown)
240
+ this.out.count?.({ typedChars: grown });
241
+ this.place = placeOf(this.place, prev, f, { grown: grown > 0 });
242
+ this.marksMoved(prev ?? this.away, f);
243
+ this.away = null;
244
+ if (this.live && this.grey) {
245
+ if (typed) {
246
+ const v = judgeTyped(this.grey, typed);
247
+ if (v.through || v.wrong || v.fresh)
248
+ this.count({ typedThrough: v.through, wrong: v.wrong ? 1 : 0, shown: v.fresh ? 1 : 0 });
249
+ if (v.wrong)
250
+ this.live = false;
251
+ // Judged as a hidden guess is (the whole word, then what ends it), so both count alike: a word typed through and
252
+ // then made longer was not the word. Another letter is wrong at once.
253
+ if (v.wrong)
254
+ this.verdict(false);
255
+ else if (v.through && this.grey.word)
256
+ this.shownGuess = null;
257
+ }
258
+ else if (backspaced(prev, f)) {
259
+ this.count({ wrong: 1 });
260
+ this.live = false;
261
+ // No verdict, as for a hidden guess: Backspace often mends a typo, not the guess (the deletion forgets the guesses below).
262
+ }
263
+ }
264
+ // A deletion, a caret move, a paste or dictation: what the word was meant to be is no longer clear.
265
+ if (prev && (typed === null || charCount(typed) > TYPED_MAX))
266
+ this.forget();
267
+ this.judgeGuesses(f);
268
+ this.fixSchedule(prev, f, typed, grown, gate);
269
+ if (this.grey && typed) {
270
+ const rest = consume(this.grey, typed);
271
+ if (rest !== undefined) {
272
+ // Typed what shows: the rest stays, and no new request, until all of it is typed.
273
+ clearTimeout(this.timer);
274
+ this.waiting = false;
275
+ this.show(rest, this.mid);
276
+ if (rest) {
277
+ this.confirmed = true;
278
+ return;
279
+ }
280
+ }
281
+ }
282
+ this.ask(f, false);
283
+ }
284
+ /**
285
+ * After a change of the box: the marks that no longer stand in it are gone (a box that was emptied has none), and the ones the
286
+ * caret has left (moved away from, or gone on from by more than 6 words or a line) are passed: their chip shows again only when
287
+ * the caret is in them.
288
+ */
289
+ marksMoved(prev, f) {
290
+ const text = textOf(f);
291
+ this.marks = this.marks.filter((m) => m.id !== f.id || (!!text && !!findMark(text, m)));
292
+ const moved = !!prev && prev.id === f.id && textOf(prev) === text && prev.before.length !== f.before.length;
293
+ for (const x of this.placed(f)) {
294
+ if (x.spot.start <= f.before.length && f.before.length <= x.spot.end)
295
+ this.passed.delete(x.mark);
296
+ else if (moved || leftBehind(text, x.spot, f.before.length))
297
+ this.passed.add(x.mark);
298
+ }
299
+ for (const m of this.passed)
300
+ if (!this.marks.includes(m))
301
+ this.passed.delete(m);
302
+ }
303
+ /** Whether words typed wrong are looked for here now. */
304
+ fixOn() {
305
+ const g = this.out.gate();
306
+ return g.fixTypos && allowed(g, this.ad.place);
307
+ }
308
+ /**
309
+ * Ask for a fix of its own after punctuation was typed right after a word (at the usual pause; a space too, where the word
310
+ * at the caret is not asked for, whose answer would hold the fix), or once the caret has rested (REST_MS) in or at the end of a
311
+ * word that it came to by a caret move.
312
+ */
313
+ fixSchedule(prev, f, typed, grown, gate) {
314
+ clearTimeout(this.fixTimer);
315
+ // (More punctuation in the pause after the first: still to be asked for.)
316
+ const due = this.endedDue && wordEnded(f.before);
317
+ this.endedDue = false;
318
+ if (typed === null)
319
+ this.fixWait = null;
320
+ if (!gate.fixTypos || f.selected || f.composing)
321
+ return;
322
+ if (typed && grown && (due || punctAfterWord(f.before) || (spaceAfterWord(f.before) && !this.spot(f)))) {
323
+ this.endedDue = true;
324
+ this.fixTimer = setTimeout(() => this.fixFire('ended'), DEBOUNCE_MS);
325
+ return;
326
+ }
327
+ const arrived = !prev || prev.id !== f.id || (textOf(prev) === textOf(f) && (prev.before.length !== f.before.length || !!prev.selected));
328
+ if (arrived && wordAround(textOf(f), f.before.length))
329
+ this.fixTimer = setTimeout(() => this.fixFire('rest'), this.out.rest ?? REST_MS);
330
+ }
331
+ fixFire(why) {
332
+ this.endedDue = false;
333
+ const f = this.ad.read();
334
+ // (A change not read yet is asked about once it is.)
335
+ if (!f || f.selected || f.composing || !this.fixOn() || !same(this.last, f))
336
+ return;
337
+ let key;
338
+ if (why === 'ended') {
339
+ if (!wordEnded(f.before) && !spaceAfterWord(f.before))
340
+ return;
341
+ }
342
+ else {
343
+ // The word at the caret was asked for as it is: its answer has the fix too.
344
+ if (same(this.allAt, f))
345
+ return;
346
+ const text = textOf(f);
347
+ const w = wordAround(text, f.before.length);
348
+ if (!w)
349
+ return;
350
+ key = askedKey(text, w);
351
+ if (this.asked.includes(key))
352
+ return;
353
+ }
354
+ // Never two requests at once: it is asked for when the one out has answered.
355
+ if (this.flight || this.waiting)
356
+ return void (this.fixWait = why);
357
+ if (key) {
358
+ this.asked.push(key);
359
+ if (this.asked.length > ASKED_MAX)
360
+ this.asked.shift();
361
+ }
362
+ this.fly(f, 'fix', false, key);
363
+ }
364
+ /** A request has answered: a fix of its own that waited for it goes now, unless the word at the caret is asked for (its answer has the fix too). */
365
+ afterFlight() {
366
+ const why = this.fixWait;
367
+ if (!why || this.flight || this.waiting)
368
+ return;
369
+ this.fixWait = null;
370
+ this.fixFire(why);
371
+ }
372
+ /** Where a word may be suggested now: at the end of a text, inside it, or nowhere. */
373
+ spot(f) {
374
+ const where = this.hidden ? null : spotOf(f, { typed: !!this.place?.typed, seam: !!this.place?.seam });
375
+ return where === 'mid' && !this.out.gate().midText ? null : where;
376
+ }
377
+ /**
378
+ * Ask for a suggestion at the caret, after a pause of 30 ms. `keep`: what shows stays until the answer. While a
379
+ * request is under way none is sent: its answer asks about the box as it is then.
380
+ */
381
+ ask(f, keep) {
382
+ clearTimeout(this.timer);
383
+ this.waiting = false;
384
+ if (!keep)
385
+ this.show(null);
386
+ const where = this.spot(f);
387
+ if (!where)
388
+ return this.show(null);
389
+ if (this.flight)
390
+ return;
391
+ this.waiting = true;
392
+ this.timer = setTimeout(() => {
393
+ this.waiting = false;
394
+ this.fly(f, 'all', where === 'mid');
395
+ }, DEBOUNCE_MS);
396
+ }
397
+ /** Send a request for the box as it is: the word at the caret (and a fix), or a fix alone (`key`: of the word the caret rests at). */
398
+ fly(f, kind, mid, key) {
399
+ const flight = (this.flight = { field: f, asked: key ? intoWord(f) : f, ctl: new AbortController(), at: Date.now(), kind, mid, key });
400
+ if (kind === 'all') {
401
+ this.allAt = f;
402
+ this.fixWait = null;
403
+ }
404
+ new Promise((res) => {
405
+ const ctx = this.ad.context();
406
+ const req = kind === 'fix' ? fixRequestOf(this.ad.place, flight.asked, ctx, this.out.client) : mid ? midRequestOf(this.ad.place, f, ctx, this.out.client, !!this.place?.seam) : requestOf(this.ad.place, f, ctx, this.out.client);
407
+ res(this.out.send(req, flight.ctl.signal));
408
+ })
409
+ .catch(() => null)
410
+ .then((r) => {
411
+ if (this.flight !== flight)
412
+ return;
413
+ this.flight = null;
414
+ if (!r)
415
+ this.unasked(flight);
416
+ this.answered(flight.field, r, flight);
417
+ this.afterFlight();
418
+ });
419
+ }
420
+ /**
421
+ * An answer about the box as it was (`asked`): shown if the box is still so; what is left of it if the person has
422
+ * since typed letters of its grey word; else dropped. A box that has moved on is asked about again. A fix in it is taken
423
+ * when the word still stands where it was found.
424
+ */
425
+ answered(asked, r, fl) {
426
+ // A change not read yet (its event is still to be handled) is read first, so no letter is used up twice.
427
+ if (!same(this.last, this.ad.read()))
428
+ this.sync();
429
+ const now = this.ad.read();
430
+ if (!now || !allowed(this.out.gate(), this.ad.place))
431
+ return;
432
+ const typed = typedBetween(asked, now);
433
+ if (typed !== null && !now.selected && !now.composing) {
434
+ this.takeFix(fl.asked, now, r, typed === '');
435
+ if (fl.kind === 'all') {
436
+ // A guess inside a text is never judged: the thresholds are fitted to the words at the end of a text.
437
+ const guess = fl.mid ? null : this.note(asked, r);
438
+ let g = afterSpace(greyOf(r), asked.before);
439
+ // Inside a text one chip shows at a time, and a word typed wrong comes first.
440
+ if (fl.mid)
441
+ g = g?.word && !this.fixHere(now) ? { word: g.word, next: '' } : null;
442
+ if (!typed)
443
+ this.apply(g, guess, fl.mid);
444
+ else if (g && !this.grey && !this.hidden) {
445
+ const rest = consume(g, typed);
446
+ const left = rest ? afterSpace(rest, now.before) : null;
447
+ if (left) {
448
+ this.began(fl.mid);
449
+ this.show(left, fl.mid);
450
+ this.confirmed = true;
451
+ this.link(guess, typed.length);
452
+ }
453
+ }
454
+ }
455
+ if (this.mid && this.grey && this.fixHere(now))
456
+ this.show(null);
457
+ this.judgeGuesses(now);
458
+ }
459
+ if (!same(now, asked))
460
+ this.ask(now, !!this.grey);
461
+ this.publish();
462
+ }
463
+ /** An answer for the box as it is now. */
464
+ apply(g, guess, mid) {
465
+ if (!g) {
466
+ if (!this.confirmed)
467
+ this.show(null);
468
+ return;
469
+ }
470
+ if (sameGrey(g, this.grey) && mid === this.mid)
471
+ return;
472
+ // The same grey word with another next word is the same suggestion.
473
+ if (this.live && g.word && g.word === this.grey?.word && mid === this.mid)
474
+ return this.show(g, mid);
475
+ this.began(mid);
476
+ this.show(g, mid);
477
+ this.link(guess, 0);
478
+ }
479
+ // ---- words typed wrong
480
+ /**
481
+ * The fix an answer holds becomes a mark when, in the text as it was asked about, the word stands where it says and it still
482
+ * stands in the box now; an answer for the same place with no fix takes away the open mark there (a closed one stays). No answer
483
+ * at all (the models are loading, the request was dropped) changes nothing.
484
+ */
485
+ takeFix(asked, now, r, unchanged) {
486
+ if (!r || !this.fixOn())
487
+ return;
488
+ const text = asked.before + asked.after;
489
+ const caret = asked.before.length;
490
+ const fx = r.fix;
491
+ // An answer about the box as it is, with nothing about the word at the caret: the open mark there (found earlier in it) is no longer one.
492
+ if (unchanged) {
493
+ this.marks = this.marks.filter((m) => {
494
+ if (!m.open || m.id !== asked.id)
495
+ return true;
496
+ const s = findMark(text, m);
497
+ if (!s || !caretAtWord(text, s, caret))
498
+ return true;
499
+ return !!fx?.typed && !!fx.word && s.start < caret - fx.back + fx.typed.length && caret - fx.back < s.end;
500
+ });
501
+ }
502
+ if (!fx?.typed || !fx.word)
503
+ return;
504
+ const from = caret - fx.back;
505
+ if (from < 0 || text.slice(from, from + fx.typed.length) !== fx.typed || this.kept.has(fx.typed.toLowerCase()))
506
+ return;
507
+ const m = { typed: fx.typed, word: fx.word, conf: fx.conf, open: !!fx.open, left: leftOf(text, from), at: 0, id: asked.id };
508
+ if (this.keptAt.some((k) => k.id === m.id && k.left === m.left && m.typed.startsWith(k.typed)))
509
+ return;
510
+ const spot = findMark(textOf(now), m);
511
+ if (!spot)
512
+ return;
513
+ // The word before the one being typed is told again with each answer: a mark that is there already stays as it is.
514
+ if (!m.open && this.marks.some((x) => !x.open && x.id === m.id && x.left === m.left && x.typed === m.typed && x.word === m.word))
515
+ return;
516
+ // A mark at this place is the same word, judged again (an open one as it has gone on since): this one takes its place, and is not counted as another.
517
+ const n = this.marks.length;
518
+ this.marks = this.marks.filter((x) => {
519
+ if (x.id !== m.id)
520
+ return true;
521
+ if (x.left === m.left && x.typed === m.typed)
522
+ return false;
523
+ const s = x.open ? findMark(textOf(now), x) : null;
524
+ return !(s && s.start < spot.end && spot.start < s.end);
525
+ });
526
+ const again = this.marks.length < n;
527
+ m.at = ++this.seq;
528
+ this.marks.push(m);
529
+ while (this.marks.length > MARKS_MAX)
530
+ this.marks.shift();
531
+ const key = `${m.typed}\u0000${m.word}\u0000${m.left}`;
532
+ if (this.shownFixes.includes(key))
533
+ return;
534
+ this.shownFixes.push(key);
535
+ if (this.shownFixes.length > SHOWN_MAX)
536
+ this.shownFixes.shift();
537
+ if (!again)
538
+ this.out.count?.({ fixShown: 1 });
539
+ }
540
+ removeMark(m) {
541
+ this.marks = this.marks.filter((x) => x !== m);
542
+ this.passed.delete(m);
543
+ }
544
+ /** The right word goes in the place of the word at the caret (Tab, or a tap on its chip), and the box is read again. */
545
+ accept() {
546
+ const f = this.ad.read();
547
+ const cur = f && this.current(f);
548
+ if (!f || !cur)
549
+ return;
550
+ const { mark, spot } = cur;
551
+ const caret = f.before.length;
552
+ // An open mark's word was still being typed: it is replaced from its start to the caret.
553
+ const end = mark.open && caret >= spot.start && caret <= spot.end ? caret : spot.end;
554
+ clearTimeout(this.timer);
555
+ clearTimeout(this.fixTimer);
556
+ this.waiting = false;
557
+ this.cancel();
558
+ this.inserting = true;
559
+ let done;
560
+ try {
561
+ done = this.ad.replace(spot.start, end, mark.word);
562
+ }
563
+ finally {
564
+ this.inserting = false;
565
+ }
566
+ this.removeMark(mark);
567
+ const f2 = this.ad.read();
568
+ const seam = !!this.place?.seam && this.place.after === f2?.after && this.place.id === f2?.id;
569
+ this.last = f2;
570
+ if (done === false)
571
+ return void this.publish();
572
+ this.out.count?.({ fixTaken: 1 });
573
+ this.forget();
574
+ if (f2) {
575
+ // As after typing there: the word at the caret may be asked for again.
576
+ this.place = { before: f2.before, after: f2.after, id: f2.id, seam, typed: true };
577
+ this.marksMoved(null, f2);
578
+ }
579
+ this.show(null);
580
+ if (f2)
581
+ this.ask(f2, false);
582
+ this.publish();
583
+ }
584
+ /** The word is kept as it stands (Esc, or the chip's ×): its mark goes, and it is never marked again in this box, nor by the engine. */
585
+ keep() {
586
+ const f = this.ad.read() ?? this.last;
587
+ const cur = f && this.current(f);
588
+ if (!f || !cur)
589
+ return;
590
+ const { mark, spot } = cur;
591
+ const word = textOf(f).slice(spot.start, spot.end);
592
+ this.removeMark(mark);
593
+ this.kept.add(word.toLowerCase());
594
+ if (mark.open) {
595
+ this.keptAt.push({ id: mark.id, left: mark.left, typed: mark.typed });
596
+ if (this.keptAt.length > ASKED_MAX)
597
+ this.keptAt.shift();
598
+ }
599
+ this.out.keep?.(word);
600
+ this.out.count?.({ fixKept: 1 });
601
+ this.publish();
602
+ }
603
+ /** A key press, before the box's own handling: true when it was taken (Tab, → at the end, Esc). */
604
+ key(e) {
605
+ if (e.isComposing)
606
+ return false;
607
+ const plain = !e.shiftKey && !e.ctrlKey && !e.metaKey && !e.altKey;
608
+ // A word typed wrong, with Tab to put the right one in: the chip's keys, while its Tab is not the grey word's.
609
+ if (this.view.fix?.tab && plain && (e.key === 'Tab' || e.key === 'Escape')) {
610
+ const f = this.ad.read();
611
+ // (Not for a chip about to go: the box has changed since it was drawn.)
612
+ if (f && this.current(f)) {
613
+ e.preventDefault();
614
+ if (e.key === 'Tab')
615
+ this.accept();
616
+ else {
617
+ e.stopPropagation();
618
+ this.keep();
619
+ }
620
+ return true;
621
+ }
622
+ }
623
+ const g = this.grey;
624
+ if (!g)
625
+ return false;
626
+ if (e.key === 'Escape' && plain) {
627
+ e.preventDefault();
628
+ e.stopPropagation();
629
+ this.hidden = true;
630
+ this.cancel();
631
+ clearTimeout(this.timer);
632
+ this.waiting = false;
633
+ this.show(null);
634
+ return true;
635
+ }
636
+ if (!plain)
637
+ return false;
638
+ if (e.key === 'Tab') {
639
+ e.preventDefault();
640
+ this.tab();
641
+ return true;
642
+ }
643
+ if (e.key === 'ArrowRight') {
644
+ const f = this.ad.read();
645
+ // Inside a text → is the box's own: the caret moves.
646
+ if (!f || f.after.length)
647
+ return false;
648
+ e.preventDefault();
649
+ const insert = takeAll(g);
650
+ this.take({ insert, grey: null }, takenCounts(g, insert, true));
651
+ return true;
652
+ }
653
+ return false;
654
+ }
655
+ /** What Tab does with the grey word (also a tap on the chip of one inside a text): takes the word, and the next one becomes the one shown. */
656
+ tab() {
657
+ const g = this.grey;
658
+ if (!g)
659
+ return;
660
+ const t = takeWord(g);
661
+ this.take(t, takenCounts(g, t.insert, false));
662
+ }
663
+ /** Put what was taken in the box, and show what follows it. */
664
+ take(t, counts) {
665
+ // Right, and the keystrokes it saved; what it inserts is read as the box's state now, so it is never counted as typed.
666
+ this.count(counts);
667
+ if (this.grey?.word)
668
+ this.verdict(true);
669
+ this.live = false;
670
+ this.shownGuess = null;
671
+ this.confirmed = false;
672
+ this.cancel();
673
+ clearTimeout(this.fixTimer);
674
+ const prev = this.last;
675
+ this.inserting = true;
676
+ try {
677
+ this.ad.insert(t.insert);
678
+ }
679
+ finally {
680
+ this.inserting = false;
681
+ }
682
+ const f = this.ad.read();
683
+ this.last = f;
684
+ if (f) {
685
+ // Taking a suggestion is typing there: a word may be suggested inside a text from here on.
686
+ this.place = placeOf(this.place, prev, f, { grown: false, tab: true });
687
+ this.marksMoved(null, f);
688
+ }
689
+ // The next word is now the grey word: a new suggestion.
690
+ if (t.grey)
691
+ this.began();
692
+ this.show(t.grey);
693
+ if (f)
694
+ this.ask(f, !!t.grey);
695
+ }
696
+ }