| index 6ebff38..42eee95 100644 |
| --- a/internal/memoria/static/search.js |
| +++ b/internal/memoria/static/search.js |
| @@ -1,440 +1,204 @@ |
| -/* search.js — live search, keyboard navigation, and Enter. |
| - * |
| - * App-owned, not vendored: rastrillo.js comes from estilo and is pinned |
| - * byte-identical by a gate; this file and drop.js are memoria's. |
| - * |
| - * Same house rules as its neighbours: first-party, dependency-free, and |
| - * an enhancement rather than a mechanism. Delete this file and the |
| - * search form is still a GET form to "/" that still returns results — |
| - * what is lost is that the results arrive as you type and that Enter can |
| - * take you straight into the first one. |
| - * |
| - * Three behaviours, in the order they matter: |
| - * |
| - * Live. Debounced fetch of /search?q=…, whose response is the same |
| - * results template the page renders. The server sends markup, |
| - * not JSON, so a snippet is escaped once, on the server, by |
| - * the code that already does it — the browser never builds |
| - * HTML out of a document's text. |
| - * |
| - * Enter. Goes to the primed result, if there is one. WHICH result is |
| - * primed, and whether anything is primed at all, is the |
| - * server's decision (internal/memoria/query): a query that |
| - * reads as a question about the corpus primes nothing, because |
| - * navigating away from a page someone is still reading is a |
| - * worse failure than making them press Enter twice. This file |
| - * reads an attribute. It does not re-derive the rule. |
| - * |
| - * Arrows. Move real DOM focus onto the result links, rather than |
| - * mirroring a selection with aria-activedescendant. A focused |
| - * link needs no ARIA to be announced, gets the browser's own |
| - * focus ring, and Enter on it is just a link being followed. |
| - * |
| - * THE REGION. Results go into [rst-refresh="results"]. This file never |
| - * holds a reference to it — every read re-queries — so anything that |
| - * replaces the node cannot leave it writing to a detached one. It keeps |
| - * location.search in step with the box, so a reload or a bookmark |
| - * carries the query the screen is showing. |
| - * |
| - * WHAT IS ON SCREEN. The server stamps the query it rendered onto the |
| - * region as rst-search-results-for, and that attribute — never the input |
| - * — is the answer to "what do these results describe". The box and the |
| - * screen disagree more often than is comfortable: during a debounce, |
| - * while a fetch is in the air, and after a Back, where the browser |
| - * restores the input's value on a schedule of its own that no script |
| - * should be relying on. Every Enter checks the two against each other, |
| - * and where they differ it runs the search first and acts on the answer, |
| - * so a fast typist gets the same document as a slow one. */ |
| +/* search.js — the typeahead, Enter, and the results page's AI answer. |
| + |
| + App-owned, not vendored: rastrillo.js comes from estilo and is pinned |
| + byte-identical by a gate; this file and drop.js are memoria's. |
| + |
| + Same house rules as its neighbours: first-party, dependency-free, and |
| + an enhancement rather than a mechanism. Delete this file and the |
| + search form is still a GET form to "/" that returns the results page, |
| + answer and all; what is lost is the typeahead and the AI answer |
| + arriving on its own (the page then offers a link that asks for it). |
| + |
| + Paul, 2026-10-29: "Google-style typeahead and then press enter to jump |
| + to the search page where it gives an answer." So typing shows |
| + suggestions under the box — the box's one checked answer, if there is |
| + exactly one, and the documents the search finds — and never changes |
| + the page under it. Enter submits the form, and the results page is |
| + where the answer is. Arrow keys move a highlight over the suggestions |
| + while the caret stays in the box; Enter on a highlighted one opens it. |
| + |
| + The suggestions are the server's markup (/suggest), escaped there: this |
| + file never builds HTML out of a document's text. */ |
| (function () { |
| "use strict"; |
| |
| - // 40ms, down from 120: the results felt like they trailed the typing |
| - // (Paul, 2026-10-26). In-flight requests for an older query are |
| - // aborted, so a shorter wait costs requests, not correctness. |
| - var DEBOUNCE_MS = 40; |
| - // How long a live zero-result line waits for the person to stop typing. |
| - // The word being typed matches as a prefix now, but a misspelling still |
| - // finds nothing, and "No matches" on every keystroke is noise (polish |
| - // audit §B1). A submitted search is a page render and shows it at once. |
| - var PAUSE_MS = 900; |
| - // How long after the last keystroke a deferred box is completed. A |
| - // keystroke's request skips §8.9's model step (it can take 800ms and |
| - // sends passages out); once typing settles, one full request fills it. |
| - var SETTLE_MS = 450; |
| - var settleTimer = null; |
| + // How long after a keystroke the suggestions are asked for. In-flight |
| + // requests for an older prefix are aborted, so a short wait costs |
| + // requests, not correctness. |
| + var DEBOUNCE_MS = 60; |
| |
| var form = document.querySelector("form[rst-search]"); |
| var input = form && form.querySelector('input[type="search"]'); |
| - var status = document.querySelector("[rst-search-status]"); |
| - if (!form || !input) return; |
| + var box = document.querySelector("[rst-suggest-box]"); |
| + if (!form || !input || !box) return; |
| if (!window.fetch || !window.AbortController) return; |
| - if (!region()) return; |
| |
| var timer = null; |
| - var pauseTimer = null; |
| var inflight = null; |
| - var inflightFor = null; |
| + var active = -1; |
| |
| - // Re-queried, never cached: drop.js replaces this node outright when a |
| - // drop settles, and a held reference would have this file writing into |
| - // a detached element for the rest of the session. |
| - function region() { |
| - return document.querySelector('[rst-refresh="results"]'); |
| - } |
| - |
| - /* rendered is the query the markup on screen was produced from, as the |
| - SERVER reported it, or null when that cannot be determined — after a |
| - failed fetch, or after drop.js swapped in a region this file has not |
| - read. Null is not "no query": it means Enter must not act on what is |
| - on screen, and falls through to a real form submit. */ |
| - function rendered() { |
| - var el = region(); |
| - if (!el || !el.hasAttribute("rst-search-results-for")) return null; |
| - return el.getAttribute("rst-search-results-for"); |
| - } |
| - |
| - function fresh() { |
| - var r = rendered(); |
| - return r !== null && r === input.value.trim(); |
| - } |
| - |
| - function primedLink() { |
| - var el = region(); |
| - var li = el && el.querySelector("[rst-result-primed]"); |
| - return li && li.querySelector("[rst-result-title]"); |
| - } |
| - |
| - function links() { |
| - var el = region(); |
| - if (!el) return []; |
| - return Array.prototype.slice.call( |
| - el.querySelectorAll("[rst-result-title]") |
| - ); |
| - } |
| - |
| - /* search fetches the results region for q and swaps it in, resolving |
| - once the swap has happened so Enter can wait on it. |
| - |
| - It rejects when the answer never arrived or is no longer wanted, and |
| - the two are not the same thing: a REJECTION IS NOT A FAILURE. An |
| - aborted or superseded fetch was replaced by a newer one that is |
| - still coming, and treating it as a failure is how repeatedly |
| - pressing Enter used to end up on the search page instead of in the |
| - document. Callers check err.stale. */ |
| - // §8.8's ?explain=facts survives typing: the owner asked to see the |
| - // box's account of itself, and editing the query should not take it |
| - // away. Read once, from the URL the page was loaded with. |
| - var explain = /(?:^|[?&])explain=facts(?:&|$)/.test(location.search) ? "&explain=facts" : ""; |
| - |
| - function search(q, typing) { |
| - if (inflight) inflight.abort(); |
| - var ctl = new AbortController(); |
| - inflight = ctl; |
| - inflightFor = q; |
| - return fetch("/search?q=" + encodeURIComponent(q) + explain + (typing ? "&typing=1" : ""), { |
| - signal: ctl.signal, |
| - credentials: "same-origin", |
| - headers: { Accept: "text/html" }, |
| - }) |
| - .then(function (res) { |
| - if (ctl !== inflight) throw stale(); |
| - if (!res.ok) throw new Error("search " + res.status); |
| - // A session that ran out answers a GET with a 303 to /signin, |
| - // which fetch follows and reports as a perfectly good 200. Left |
| - // unchecked, the sign-in page goes into the results region — a |
| - // whole document nested inside a list, with the stamp below |
| - // vouching for it as the answer to what is in the box. drop.js |
| - // refuses the same page for the same reason; this is that guard. |
| - if (res.redirected || new URL(res.url).pathname !== "/search") { |
| - throw new Error("not the results"); |
| - } |
| - return res.text(); |
| - }) |
| - .then(function (html) { |
| - // A response that lost the race must not overwrite a newer one. |
| - // The abort above usually prevents this; this is the check that |
| - // holds when it does not. |
| - if (ctl !== inflight) throw stale(); |
| - inflight = inflightFor = null; |
| - put(html, q); |
| - if (typing) settle(q); |
| - }) |
| - .catch(function (err) { |
| - if (err && err.stale) throw err; |
| - if (err && err.name === "AbortError") throw stale(); |
| - if (ctl === inflight) { |
| - inflight = inflightFor = null; |
| - // Not "no results". The markup on screen stays — it is the |
| - // last true thing this page had — but it is no longer allowed |
| - // to speak for the box, so the stamp comes off and Enter falls |
| - // through to a real submit rather than following a stale link. |
| - // And the page SAYS so: results that silently describe an |
| - // older query are the confident-zero failure wearing a hat. |
| - var el = region(); |
| - if (el) { |
| - el.removeAttribute("rst-search-results-for"); |
| - // Said out loud AND shown. The status region is sr-only, so |
| - // announcing alone leaves a sighted reader looking at older |
| - // results with nothing on screen admitting it — which is the |
| - // confident-zero failure with better manners. The sentence |
| - // itself stays in the template; CSS renders the attribute. |
| - el.setAttribute("rst-search-stale", ""); |
| - } |
| - announce(el && el.getAttribute("rst-search-stale-note")); |
| - } |
| - throw err; |
| - }); |
| - } |
| - |
| - function stale() { |
| - var e = new Error("superseded"); |
| - e.stale = true; |
| - return e; |
| - } |
| - |
| - /* put swaps the markup in and restores the keyboard. |
| - |
| - Replacing innerHTML under a focused link moves focus to the body, |
| - which on a keyboard-navigation feature means the arrows and Enter |
| - both stop working with nothing on screen saying why. So where focus |
| - was inside the region, it is put back on the result at the same |
| - index — or on the input when the list has grown shorter than the |
| - place the person was standing in. */ |
| - function put(html, q) { |
| - var el = region(); |
| - if (!el) return; |
| - var held = links().indexOf(document.activeElement); |
| - el.innerHTML = html; |
| - el.setAttribute("rst-search-results-for", q); |
| - el.removeAttribute("rst-search-stale"); |
| - var waiting = holdEmpty(el); |
| - if (held >= 0) { |
| - var all = links(); |
| - (all[held] || all[all.length - 1] || input).focus(); |
| - } |
| - // location.search is kept in step so that drop.js's re-fetch of |
| - // pathname+search re-renders THIS search, and so that a reload or a |
| - // bookmark carries the query the screen is showing. replaceState, |
| - // not pushState: a debounce is not a navigation, and forty history |
| - // entries for one typed word is a Back button nobody can use. |
| - if (window.history && history.replaceState) { |
| - var url = q === "" ? location.pathname : location.pathname + "?q=" + encodeURIComponent(q) + explain; |
| - history.replaceState(null, "", url); |
| - } |
| - if (!waiting) announce(); |
| - } |
| + input.setAttribute("role", "combobox"); |
| + input.setAttribute("aria-autocomplete", "list"); |
| + input.setAttribute("aria-expanded", "false"); |
| + input.setAttribute("aria-controls", "rst-suggestions"); |
| + input.setAttribute("autocomplete", "off"); |
| |
| - /* holdEmpty keeps a live zero-result line out of sight until the person |
| - pauses. The line is the server's markup and stays in the region — |
| - [rst-search-pending] only hides it (memoria.css) — so the stamp above |
| - still vouches for what the region holds, and a pause has nothing to |
| - fetch: it lifts the attribute and says the line. Anything else in the |
| - region (results, the search-failed callout) shows at once. */ |
| - function holdEmpty(el) { |
| - if (pauseTimer) { |
| - clearTimeout(pauseTimer); |
| - pauseTimer = null; |
| - } |
| - if (!el.querySelector("[rst-search-empty]")) { |
| - el.removeAttribute("rst-search-pending"); |
| - return false; |
| - } |
| - el.setAttribute("rst-search-pending", ""); |
| - pauseTimer = setTimeout(function () { |
| - pauseTimer = null; |
| - var now = region(); |
| - if (!now) return; |
| - now.removeAttribute("rst-search-pending"); |
| - announce(); |
| - }, PAUSE_MS); |
| - return true; |
| + function items() { |
| + return Array.prototype.slice.call(box.querySelectorAll("[rst-suggest-item]")); |
| } |
| |
| - /* announce repeats the heading the server just sent into the live |
| - region, so a screen reader hears the count change. Reusing the |
| - server's sentence rather than composing one here keeps every string |
| - a person hears in a template, where it is written and reviewed. |
| - |
| - A fragment with no heading — the search-failed callout — falls back |
| - to that callout's own words, so silence never stands in for a |
| - failure. */ |
| - function announce(override) { |
| - if (!status) return; |
| - if (override) { |
| - status.textContent = override; |
| - return; |
| - } |
| - var el = region(); |
| - var said = el && el.querySelector("h2, [rst-callout-body], [rst-search-empty]"); |
| - status.textContent = said ? said.textContent.trim() : ""; |
| + function close() { |
| + box.hidden = true; |
| + box.innerHTML = ""; |
| + active = -1; |
| + input.setAttribute("aria-expanded", "false"); |
| + input.removeAttribute("aria-activedescendant"); |
| } |
| |
| - /* settle runs once typing pauses. It completes a box a keystroke's |
| - request deferred (§8.9) by asking once more without typing=1, and |
| - then fills §8.11's AI answer slot if the page has one. Neither runs |
| - on a keystroke, and neither runs if the box has moved on. */ |
| - function settle(q) { |
| - var el = region(); |
| - if (!el) return; |
| - var deferred = el.querySelector("[rst-facts-deferred]"); |
| - if (!deferred && !el.querySelector("[rst-answer-pending]")) return; |
| - settleTimer = setTimeout(function () { |
| - settleTimer = null; |
| - if (input.value.trim() !== q) return; |
| - if (deferred) { |
| - search(q, false).then(function () { answer(q); }, function () {}); |
| + function highlight(i) { |
| + var all = items(); |
| + active = i; |
| + all.forEach(function (el, n) { |
| + if (n === i) { |
| + el.setAttribute("rst-suggest-active", ""); |
| + el.parentNode.setAttribute("aria-selected", "true"); |
| + input.setAttribute("aria-activedescendant", el.parentNode.id); |
| } else { |
| - answer(q); |
| + el.removeAttribute("rst-suggest-active"); |
| + el.parentNode.setAttribute("aria-selected", "false"); |
| } |
| - }, SETTLE_MS); |
| - } |
| - |
| - /* answer fills §8.11's slot from GET /answer, which runs its own ruled |
| - search and takes only the query. The page never waits on it: the slot |
| - says the AI is being asked. |
| - |
| - An answer for a query the box has moved on from is never painted |
| - where anyone can see it, by construction: it goes into the slot it |
| - was asked for, held here rather than looked up again, and put() |
| - replaces the region's markup, so by the time newer results show, that |
| - slot is detached. Besides, every keystroke abandons it (schedule → |
| - stopAnswer), so it costs nothing either. There is deliberately no |
| - check at paint time. One was here — the region's stamp against q, the |
| - slot still in the region, ctl still current — and removing it left |
| - every test green (2026-10-27): nothing could reach it. Nor does |
| - answer() call stopAnswer itself: schedule already has, every time. */ |
| - var answering = null; |
| - var answeringSlot = null; |
| - |
| - /* stopAnswer abandons an answer in flight and puts its slot back to |
| - pending, so the next settle asks again. Without the second half, a |
| - space typed while the AI was being asked left "Asking AI…" on |
| - screen for good: the query on screen had not changed, so no new |
| - results came, and settle found no pending slot to fill (Astra, |
| - 2026-10-27). */ |
| - function stopAnswer() { |
| - if (!answering) return; |
| - answering.abort(); |
| - answeringSlot.textContent = ""; |
| - answeringSlot.setAttribute("rst-answer-pending", ""); |
| - answering = answeringSlot = null; |
| + }); |
| } |
| |
| - function answer(q) { |
| - var el = region(); |
| - var slot = el && el.querySelector("[rst-answer-pending]"); |
| - if (!slot) return; |
| + function suggest(q) { |
| + if (inflight) inflight.abort(); |
| var ctl = new AbortController(); |
| - answering = ctl; |
| - answeringSlot = slot; |
| - slot.removeAttribute("rst-answer-pending"); |
| - slot.textContent = "🤖 Asking AI…"; |
| - fetch("/answer?q=" + encodeURIComponent(q), { signal: ctl.signal, credentials: "same-origin" }) |
| + inflight = ctl; |
| + fetch("/suggest?q=" + encodeURIComponent(q), { signal: ctl.signal, credentials: "same-origin" }) |
| .then(function (res) { |
| - if (res.status === 204) return null; |
| - if (!res.ok || res.redirected) throw new Error("answer " + res.status); |
| + if (res.status === 204) return ""; |
| + // A session that ran out answers with a redirect to sign-in, |
| + // which fetch follows; that page is not suggestions. |
| + if (!res.ok || res.redirected) throw new Error("suggest " + res.status); |
| return res.text(); |
| }) |
| .then(function (html) { |
| - answering = answeringSlot = null; |
| - // 204: /answer's own ruled search no longer wants an answer — a |
| - // card answers it now, or nothing came back. The slot goes. |
| - if (html === null) { |
| - slot.remove(); |
| + if (ctl !== inflight) return; |
| + inflight = null; |
| + // Suggestions for a prefix the box has moved on from are not |
| + // shown: the abort usually stops them, and this holds when not. |
| + if (input.value.trim() !== q || !html) { |
| + if (!html) close(); |
| return; |
| } |
| - slot.innerHTML = html; |
| + box.innerHTML = html; |
| + var list = box.querySelector("[rst-suggest-list]"); |
| + if (list) list.id = "rst-suggestions"; |
| + items().forEach(function (el, n) { |
| + el.parentNode.id = "rst-suggestion-" + n; |
| + el.parentNode.setAttribute("aria-selected", "false"); |
| + // Clicking a suggestion is a link being followed; keeping the |
| + // focus in the box until then stops the blur closing the list |
| + // under the pointer. |
| + el.addEventListener("mousedown", function (e) { e.preventDefault(); }); |
| + }); |
| + active = -1; |
| + box.hidden = false; |
| + input.setAttribute("aria-expanded", "true"); |
| }) |
| - .catch(function (err) { |
| - // An abort is stopAnswer's, which has already reset the slot. |
| - if (err && err.name === "AbortError") return; |
| - answering = answeringSlot = null; |
| - slot.textContent = "🤖 The AI answer failed."; |
| + .catch(function () { |
| + // A failed suggestion is no suggestion; Enter still searches. |
| + if (ctl === inflight) { |
| + inflight = null; |
| + close(); |
| + } |
| }); |
| } |
| |
| - function schedule() { |
| + input.addEventListener("input", function () { |
| if (timer) clearTimeout(timer); |
| - // An AI answer in flight is for the query being edited away. If the |
| - // edit turns out to change nothing on screen, settle asks again. |
| - stopAnswer(); |
| - if (settleTimer) { |
| - clearTimeout(settleTimer); |
| - settleTimer = null; |
| - } |
| var q = input.value.trim(); |
| - // An in-flight fetch for some OTHER query is now answering a |
| - // question nobody is asking. Dropping it here rather than leaving it |
| - // to land is the difference between a list that matches the box and |
| - // one that quietly describes a keystroke that was taken back. |
| - if (inflight && inflightFor !== q) { |
| - inflight.abort(); |
| - inflight = inflightFor = null; |
| - } |
| - // Same query as on screen — a space added, or a letter typed and |
| - // taken back. No new search, but a deferred box still needs its |
| - // settled request, and the timer for it was just cleared above. |
| - if (rendered() === q) { |
| - settle(q); |
| + if (q === "") { |
| + if (inflight) inflight.abort(); |
| + inflight = null; |
| + close(); |
| return; |
| } |
| timer = setTimeout(function () { |
| timer = null; |
| - search(input.value.trim(), true).catch(function () {}); |
| + suggest(input.value.trim()); |
| }, DEBOUNCE_MS); |
| - } |
| - |
| - /* submit is the fallback for everything this file cannot do |
| - confidently: nothing primed, a failed fetch, a state it cannot |
| - vouch for. form.submit bypasses the submit event, so it cannot |
| - re-enter this handler. */ |
| - function submit() { |
| - form.submit(); |
| - } |
| - |
| - function follow() { |
| - var link = primedLink(); |
| - if (!link) return submit(); |
| - window.location.assign(link.href); |
| - } |
| + }); |
| |
| - /* Enter is handled on the input's keydown rather than on the form's |
| - submit, because submit cannot tell Enter from a click on the Search |
| - button and the two mean different things: Enter means "take me |
| - there", a click on a button labelled Search means "search". Reading |
| - e.submitter does not separate them — it is the same default button |
| - either way. */ |
| input.addEventListener("keydown", function (e) { |
| - if (e.key !== "Enter" || e.isComposing || e.keyCode === 229) return; |
| - if (e.metaKey || e.ctrlKey || e.altKey || e.shiftKey) return; |
| - var q = input.value.trim(); |
| - if (q === "") return; // the empty search behaves as it always did |
| - |
| - if (fresh()) { |
| - if (!primedLink()) return; // a question, or nothing found: submit |
| + if (e.isComposing || e.keyCode === 229) return; |
| + var all = items(); |
| + if (e.key === "ArrowDown" || e.key === "ArrowUp") { |
| + if (box.hidden || !all.length) return; |
| e.preventDefault(); |
| - follow(); |
| + var n = all.length; |
| + highlight(e.key === "ArrowDown" ? (active + 1) % n : (active <= 0 ? n - 1 : active - 1)); |
| return; |
| } |
| - // What is on screen is older than what is in the box. Find out |
| - // before deciding, so a fast typist gets the same answer as a slow |
| - // one rather than the previous query's first result. |
| - e.preventDefault(); |
| - if (timer) { |
| - clearTimeout(timer); |
| - timer = null; |
| + if (e.key === "Escape" && !box.hidden) { |
| + e.preventDefault(); |
| + close(); |
| + return; |
| + } |
| + if (e.key === "Enter" && !box.hidden && active >= 0 && all[active]) { |
| + // A highlighted suggestion is where Enter goes. With none, Enter is |
| + // the form's own submit: the results page. |
| + e.preventDefault(); |
| + window.location.assign(all[active].href); |
| } |
| - search(q).then(follow, function (err) { |
| - // A superseded search means a newer one is already on its way and |
| - // will do this; only a real failure falls back to the form. |
| - if (!err || !err.stale) submit(); |
| - }); |
| }); |
| |
| - /* 👍 on an AI answer saves in place (§8.11, Paul 2026-10-27). Delegated, |
| - because the answer arrives after load. 👎 is left to the form: it |
| - goes to the page that asks what was wrong. A failed save falls back |
| - to the form too, so the rating is never silently lost. */ |
| + input.addEventListener("blur", function () { close(); }); |
| + form.addEventListener("submit", function () { close(); }); |
| + // Back to a page whose box still holds text: nothing is open. |
| + window.addEventListener("pageshow", function () { close(); }); |
| +})(); |
| + |
| +/* The results page's AI answer (§8.11). The page never waits on the |
| + model: a page with an answer slot asks GET /answer once it has loaded, |
| + and the slot says the AI is being asked. GET /answer runs its own ruled |
| + search and takes only the query. A 204 means it no longer wants to |
| + answer (a card answers it now, or nothing came back), and the slot |
| + goes. */ |
| +(function () { |
| + "use strict"; |
| + if (!window.fetch) return; |
| + var region = document.querySelector('[rst-refresh="results"]'); |
| + var q = region && region.getAttribute("rst-search-results-for"); |
| + var slot = region && region.querySelector("[rst-answer-pending]"); |
| + if (!q || !slot) return; |
| + slot.removeAttribute("rst-answer-pending"); |
| + slot.textContent = "🤖 Asking AI…"; |
| + fetch("/answer?q=" + encodeURIComponent(q), { credentials: "same-origin" }) |
| + .then(function (res) { |
| + if (res.status === 204) return null; |
| + if (!res.ok || res.redirected) throw new Error("answer " + res.status); |
| + return res.text(); |
| + }) |
| + .then(function (html) { |
| + if (html === null) { |
| + slot.remove(); |
| + return; |
| + } |
| + slot.innerHTML = html; |
| + }) |
| + .catch(function () { |
| + slot.textContent = "🤖 The AI answer failed."; |
| + }); |
| +})(); |
| + |
| +/* 👍 on an AI answer saves in place (§8.11, Paul 2026-10-27). Delegated, |
| + because the answer arrives after load. 👎 is left to the form: it goes |
| + to the page that asks what was wrong. A failed save falls back to the |
| + form too, so the rating is never silently lost. */ |
| +(function () { |
| + "use strict"; |
| + if (!window.fetch || !window.URLSearchParams) return; |
| document.addEventListener("submit", function (e) { |
| var f = e.target; |
| if (!f.matches || !f.matches("[rst-ai-feedback]")) return; |
| @@ -459,61 +223,6 @@ |
| f.submit(); |
| }); |
| }); |
| - |
| - input.addEventListener("input", schedule); |
| - // A page loaded with a search on it (a submit, a reload, a link) asks |
| - // for its AI answer at once: there is no typing to wait for. |
| - (function () { |
| - var el = region(); |
| - var q = el && el.getAttribute("rst-search-results-for"); |
| - if (q && el.querySelector("[rst-answer-pending]")) answer(q); |
| - })(); |
| - |
| - /* A Back that restores the box without re-running the script leaves |
| - the input saying one thing and the screen another. The server's |
| - stamp is what settles it: where they differ, ask. pageshow rather |
| - than load, because it fires for a bfcache restore too, which is the |
| - case that has no load at all. */ |
| - window.addEventListener("pageshow", function () { |
| - if (!fresh() && input.value.trim() !== "") schedule(); |
| - }); |
| - |
| - input.addEventListener("keydown", function (e) { |
| - if (e.key !== "ArrowDown" && e.key !== "ArrowUp") return; |
| - var all = links(); |
| - if (!all.length) return; |
| - e.preventDefault(); |
| - (e.key === "ArrowDown" ? all[0] : all[all.length - 1]).focus(); |
| - }); |
| - |
| - /* Keyboard handling for the list is delegated to the document, not |
| - bound to the region: the links inside it are replaced on every |
| - keystroke, and the region ITSELF is replaced whenever a drop |
| - settles, so a listener on either would not survive. */ |
| - document.addEventListener("keydown", function (e) { |
| - var link = e.target.closest && e.target.closest("[rst-result-title]"); |
| - if (!link || !region() || !region().contains(link)) return; |
| - |
| - if (e.key === "Escape") { |
| - e.preventDefault(); |
| - input.focus(); |
| - return; |
| - } |
| - if (e.key === "ArrowDown" || e.key === "ArrowUp") { |
| - var all = links(); |
| - var i = all.indexOf(link); |
| - e.preventDefault(); |
| - if (e.key === "ArrowUp" && i === 0) return input.focus(); |
| - var next = all[e.key === "ArrowDown" ? i + 1 : i - 1]; |
| - if (next) next.focus(); |
| - return; |
| - } |
| - // Typing while a result is focused means the person has gone back to |
| - // searching. Move the caret without swallowing the character. |
| - if (e.key.length === 1 && !e.metaKey && !e.ctrlKey && !e.altKey) { |
| - input.focus(); |
| - } |
| - }); |
| })(); |
| |
| /* The whole result opens it (Paul, 2026-10-26: "Can the whole segment be |