WIP memoria: copy the typeahead's answer — ⌘C/Ctrl+C and a Copy button
Paul, 2026-10-31. ⌘C copies the answer while the list is open unless the box has a selection (then it is the ordinary copy). The button sits outside the link and shows only with a script and a clipboard. Browser test, four mutations seen red.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+165
−3
Makefile+1 −1internal/memoria/static/memoria.css+16 −0internal/memoria/static/search.js+42 −0internal/memoria/templates/results.html+7 −2internal/memoriatest/copy_browser_test.go+99 −0
CI failed — run details
diff --git a/Makefile b/Makefile| index aadcf7b..d7bc7e7 100644 |
| --- a/Makefile |
| +++ b/Makefile |
| @@ -120,7 +120,7 @@ ci: vet fmt-check test migration-check |
| # Mac from the Playwright cache. |
| RASTRILLO_CHROME ?= $(firstword $(wildcard $(HOME)/Library/Caches/ms-playwright/chromium-*/chrome-mac*/Chromium.app/Contents/MacOS/Chromium)) |
| browser: |
| - RASTRILLO_CHROME="$(RASTRILLO_CHROME)" CGO_ENABLED=0 go test -tags browser -count=1 -run 'OnTap|ProductionCSP|NoSidewaysScroll|SettlesADeferredBox|WholeResultIsALink|StaleAIAnswer|AnswerSlotSettles|ThumbsUp' ./internal/memoriatest/ |
| + RASTRILLO_CHROME="$(RASTRILLO_CHROME)" CGO_ENABLED=0 go test -tags browser -count=1 -run 'OnTap|ProductionCSP|NoSidewaysScroll|SettlesADeferredBox|WholeResultIsALink|StaleAIAnswer|AnswerSlotSettles|ThumbsUp|TypeaheadAnswerCopies' ./internal/memoriatest/ |
| .PHONY: credits |
| credits: |
diff --git a/internal/memoria/static/memoria.css b/internal/memoria/static/memoria.css| index a1eddb2..20df9fc 100644 |
| --- a/internal/memoria/static/memoria.css |
| +++ b/internal/memoria/static/memoria.css |
| @@ -82,6 +82,22 @@ html { scrollbar-gutter: stable; } |
| [rst-suggest-title] { font-weight: 550; } |
| /* The answer row wraps rather than clip: on a phone the number and the |
| document's name do not fit on one line. */ |
| +[rst-suggest-fact-row] { align-items: center; display: flex; border-bottom: 1px solid var(--rst-line); } |
| +[rst-suggest-fact-row] [rst-suggest-item] { flex: 1; min-width: 0; border-bottom: 0; } |
| +[rst-suggest-copy] { |
| + background: var(--rst-surface); |
| + border: 1px solid var(--rst-line); |
| + border-radius: 999px; |
| + color: var(--rst-text-muted); |
| + cursor: pointer; |
| + flex: none; |
| + font: inherit; |
| + font-size: 0.8125rem; |
| + margin-right: 0.75rem; |
| + padding: 0.2rem 0.75rem; |
| +} |
| +[rst-suggest-copy]:hover { border-color: var(--rst-accent); color: var(--rst-accent-strong); } |
| +[rst-suggest-copy][hidden] { display: none; } |
| [rst-suggest-fact] { |
| border-bottom: 1px solid var(--rst-line); |
| padding-top: 0.65rem; |
diff --git a/internal/memoria/static/search.js b/internal/memoria/static/search.js| index 19bc1cc..a80ff98 100644 |
| --- a/internal/memoria/static/search.js |
| +++ b/internal/memoria/static/search.js |
| @@ -94,6 +94,10 @@ |
| box.innerHTML = html; |
| var list = box.querySelector("[rst-suggest-list]"); |
| if (list) list.id = "rst-suggestions"; |
| + // Copy needs a script, so it shows only once there is one. |
| + Array.prototype.forEach.call(box.querySelectorAll("[rst-suggest-copy]"), function (b) { |
| + b.hidden = !(navigator.clipboard && navigator.clipboard.writeText); |
| + }); |
| items().forEach(function (el, n) { |
| el.parentNode.id = "rst-suggestion-" + n; |
| el.parentNode.setAttribute("aria-selected", "false"); |
| @@ -153,6 +157,44 @@ |
| } |
| }); |
| + /* Copy the answer (Paul, 2026-10-31: "cmd+c to copy, and a little Copy |
| + button"). The value is the document's own span, carried on the |
| + button as text by the server. ⌘C/Ctrl+C copies it while the list is |
| + open, unless something in the box is selected: then the keystroke is |
| + the ordinary copy of that selection. */ |
| + function factValue() { |
| + var b = box.hidden ? null : box.querySelector("[rst-suggest-copy]"); |
| + return b ? b.getAttribute("rst-copy-value") : null; |
| + } |
| + function copied(button) { |
| + if (!button) return; |
| + button.textContent = "Copied"; |
| + setTimeout(function () { button.textContent = "Copy"; }, 1500); |
| + } |
| + function copy(text, button) { |
| + if (!navigator.clipboard || !navigator.clipboard.writeText) return false; |
| + navigator.clipboard.writeText(text).then(function () { copied(button); }, function () {}); |
| + return true; |
| + } |
| + box.addEventListener("mousedown", function (e) { |
| + // Keep the caret in the box, so the list does not close under the |
| + // pointer before the click lands. |
| + if (e.target.closest && e.target.closest("[rst-suggest-copy]")) e.preventDefault(); |
| + }); |
| + box.addEventListener("click", function (e) { |
| + var b = e.target.closest && e.target.closest("[rst-suggest-copy]"); |
| + if (!b) return; |
| + e.preventDefault(); |
| + copy(b.getAttribute("rst-copy-value"), b); |
| + }); |
| + input.addEventListener("keydown", function (e) { |
| + if (e.key !== "c" || !(e.metaKey || e.ctrlKey) || e.altKey || e.shiftKey) return; |
| + if (input.selectionStart !== input.selectionEnd) return; |
| + var v = factValue(); |
| + if (v === null) return; |
| + if (copy(v, box.querySelector("[rst-suggest-copy]"))) e.preventDefault(); |
| + }); |
| + |
| input.addEventListener("blur", function () { close(); }); |
| form.addEventListener("submit", function () { close(); }); |
| // Back to a page whose box still holds text: nothing is open. |
diff --git a/internal/memoria/templates/results.html b/internal/memoria/templates/results.html| index 730690a..64e847f 100644 |
| --- a/internal/memoria/templates/results.html |
| +++ b/internal/memoria/templates/results.html |
| @@ -215,11 +215,16 @@ |
| {{define "suggest"}} |
| <ul rst-suggest-list role="listbox" aria-label="Suggestions"> |
| {{with .Fact}} |
| - <li role="option"><a href="/documents/{{.DocumentID}}#p{{.Ordinal}}" rst-suggest-item rst-suggest-fact> |
| + {{/* The answer row, with Copy beside it (Paul, 2026-10-31). The button |
| + sits outside the link — a control inside a link is two controls in |
| + one — and is hidden until search.js wires it, because without a |
| + script it would do nothing. ⌘C/Ctrl+C copies the same value while |
| + the list is open and nothing in the box is selected. */}} |
| + <li role="option" rst-suggest-fact-row><a href="/documents/{{.DocumentID}}#p{{.Ordinal}}" rst-suggest-item rst-suggest-fact> |
| <span rst-suggest-fact-label>{{.Label}}</span> |
| <strong{{if or (eq .Kind "phone") (eq .Kind "eircode") (eq .Kind "vat")}} rst-nowrap{{end}}>{{.Span}}</strong> |
| <span rst-muted>{{.Title}}</span> |
| - </a></li> |
| + </a><button type="button" rst-suggest-copy rst-copy-value="{{.Span}}" hidden>Copy</button></li> |
| {{end}} |
| {{range .Suggestions}} |
| <li role="option"><a href="{{.Href}}" rst-suggest-item> |
diff --git a/internal/memoriatest/copy_browser_test.go b/internal/memoriatest/copy_browser_test.go| new file mode 100644 |
| index 0000000..f502035 |
| --- /dev/null |
| +++ b/internal/memoriatest/copy_browser_test.go |
| @@ -0,0 +1,99 @@ |
| +//go:build browser |
| + |
| +package memoriatest |
| + |
| +import ( |
| + "context" |
| + "io" |
| + "log/slog" |
| + "net/http" |
| + "path/filepath" |
| + "testing" |
| + "time" |
| + |
| + "amadan.net/rastrillo/rastrillo/db" |
| + "amadan.net/rastrillo/rastrillo/harness" |
| + cdpbrowser "github.com/chromedp/cdproto/browser" |
| + "github.com/chromedp/cdproto/input" |
| + "github.com/chromedp/cdproto/runtime" |
| + "github.com/chromedp/chromedp" |
| + |
| + memoria "amadan.net/oficina/memoria/internal/memoria" |
| +) |
| + |
| +// The typeahead's answer row copies its value (Paul, 2026-10-31: "cmd+c |
| +// to copy, and a little Copy button"): ⌘C/Ctrl+C with nothing selected |
| +// in the box, and the Copy button; and a selection in the box is still |
| +// the ordinary copy of that selection. |
| +// |
| +// MUTATION-TESTED 2026-10-31, each reverted: the keydown handler removed |
| +// → "⌘C"; the selection check removed → "a selection in the box"; the |
| +// button's click handler removed → "the button"; the button left hidden |
| +// → "no Copy button". |
| +func TestTheTypeaheadAnswerCopies(t *testing.T) { |
| + rig := harness.New(t, func(origin string) http.Handler { |
| + logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| + d, err := db.Open(filepath.Join(t.TempDir(), "app.db"), logger) |
| + if err != nil { |
| + t.Fatalf("db.Open: %v", err) |
| + } |
| + t.Cleanup(func() { d.Close() }) |
| + mux, a, err := memoria.AppForTest(d, origin, testDEK(t), logger) |
| + if err != nil { |
| + t.Fatalf("AppForTest: %v", err) |
| + } |
| + if _, err := a.Store().Ingest(context.Background(), memoria.IngestSpec{Source: "upload", Key: "o", Title: "Office", |
| + Body: "## Contact\n\nOffice phone: +353 (0)1 547 7854"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + return mux |
| + }) |
| + ctx, cancel := context.WithTimeout(rig.Context(), 30*time.Second) |
| + defer cancel() |
| + in := `input[type="search"]` |
| + var visible bool |
| + var afterKey, afterSelection, afterButton string |
| + mod := input.ModifierMeta |
| + err := chromedp.Run(ctx, |
| + cdpbrowser.GrantPermissions([]cdpbrowser.PermissionType{cdpbrowser.PermissionTypeClipboardReadWrite, cdpbrowser.PermissionTypeClipboardSanitizedWrite}).WithOrigin(rig.Origin), |
| + chromedp.Navigate(rig.Origin+"/"), |
| + chromedp.WaitVisible(in, chromedp.ByQuery), |
| + chromedp.SendKeys(in, "office phone", chromedp.ByQuery), |
| + chromedp.WaitVisible(`[rst-suggest-copy]`, chromedp.ByQuery), |
| + chromedp.Evaluate(`!document.querySelector("[rst-suggest-copy]").hidden`, &visible), |
| + chromedp.KeyEvent("c", chromedp.KeyModifiers(mod)), |
| + chromedp.KeyEvent("c", chromedp.KeyModifiers(input.ModifierCtrl)), |
| + chromedp.Sleep(300*time.Millisecond), |
| + evalAwait(`navigator.clipboard.readText()`, &afterKey), |
| + // A selection in the box: the keystroke copies that, not the value. |
| + chromedp.Evaluate(`navigator.clipboard.writeText("untouched"); var i = document.querySelector('input[type="search"]'); i.setSelectionRange(0, 6); true`, nil), |
| + chromedp.KeyEvent("c", chromedp.KeyModifiers(mod)), |
| + chromedp.KeyEvent("c", chromedp.KeyModifiers(input.ModifierCtrl)), |
| + chromedp.Sleep(300*time.Millisecond), |
| + evalAwait(`navigator.clipboard.readText()`, &afterSelection), |
| + chromedp.Evaluate(`navigator.clipboard.writeText("before the button"); var i = document.querySelector('input[type="search"]'); i.setSelectionRange(12, 12); true`, nil), |
| + chromedp.Click(`[rst-suggest-copy]`, chromedp.ByQuery), |
| + chromedp.Sleep(300*time.Millisecond), |
| + evalAwait(`navigator.clipboard.readText()`, &afterButton), |
| + ) |
| + if err != nil { |
| + t.Fatalf("copy: %v", err) |
| + } |
| + if !visible { |
| + t.Fatal("no Copy button on the answer row") |
| + } |
| + if afterKey != "+353 (0)1 547 7854" { |
| + t.Errorf("⌘C: the clipboard holds %q, want the number", afterKey) |
| + } |
| + if afterSelection == "+353 (0)1 547 7854" { |
| + t.Errorf("a selection in the box: ⌘C copied the answer over it") |
| + } |
| + if afterButton != "+353 (0)1 547 7854" { |
| + t.Errorf("the button: the clipboard holds %q, want the number", afterButton) |
| + } |
| +} |
| + |
| +// evalAwait evaluates a promise and waits for its value. |
| +func evalAwait(js string, out any) chromedp.Action { |
| + return chromedp.Evaluate(js, out, func(p *runtime.EvaluateParams) *runtime.EvaluateParams { return p.WithAwaitPromise(true) }) |
| +} |