// Completing a field alias in a rule's condition (D9, ยง6.4 of DESIGN_M1.md). // // Tab has to decide at once whether to complete or to move focus, so the // server's answer is fetched ahead of it, whenever the text or the cursor may // have moved. An Offer is one such answer, kept with the text it was for. // // `field` below is anything with a textarea's value and selection, which in // the page is the textarea itself. // The text up to the cursor, which is what the server completes. export const beforeCursor = (field) => field.value.slice(0, field.selectionStart); export class Offer { #before; constructor(before, { candidates, insert }) { this.#before = before; this.candidates = candidates; this.insert = insert; } // Whether the offer is for the text now before the cursor. A later request // is on its way for text that has moved on since. isFor(field) { return beforeCursor(field) === this.#before; } // Whether Tab takes this offer rather than moving focus: a plain Tab with // nothing selected, while the offer is for the text before the cursor and // offers something. Otherwise Tab does what it always did. takesTab(event, field) { if (event.key !== "Tab" || event.shiftKey) return false; if (field.selectionStart !== field.selectionEnd) return false; return this.isFor(field) && this.candidates.length > 0; } // The line under the condition. get hint() { return this.candidates.join(" "); } }