Files
DeepTutor/web/lib/clipboard.ts
Bingxi Zhao (Frank) 895d2cce25 fix(web): stop the copy button reporting a success that never happened
The button inferred success from its handler's promise *resolving*, and the
handler it was given could not fail: `copyAssistantMessage` awaited
`navigator.clipboard.writeText` inside a try/catch that logged and swallowed.
So the only failure signal was destroyed one frame before the only code that
could have used it — and every clipboard failure rendered 已复制 with a
checkmark. Because the label is also the `aria-label` on an `aria-live`
element, the false success was announced to screen readers.

The failure is not exotic. `launcher.py` binds the web app to 0.0.0.0 while
printing a localhost URL, so opening the same instance from another machine
on the LAN is an ordinary way to run DeepTutor — and on that plain-http
origin `navigator.clipboard` does not exist, so reading `.writeText` throws
synchronously. `FilePreviewDrawer` already carried a comment naming this
exact cause; the handling was local instead of shared.

So: one `copyText` helper that rejects when the text did not reach the
clipboard, with a selection-based fallback for insecure origins (probed
before any await, since `execCommand` needs the click's user activation);
`CopyActionButton` narrowed to a `Promise<void>` handler and given a real
failure state, so the contract cannot be reintroduced by the next consumer;
and the three consumers plus the two other optimistic sites moved onto it.

`Promise.resolve().then(() => onCopy(...))` replaces
`Promise.resolve(onCopy(...))` because the latter runs the handler outside
the chain, which is where the synchronous TypeError was escaping.

Also adds the `attachments` and `references` locale keys. Both call sites
pass a lowercase noun for inline reading, neither key existed, and i18next is
configured to return a missing key verbatim — which is why a Chinese UI
rendered "5 attachments" in English next to this very button.
2026-09-08 00:22:08 +08:00

96 lines
3.6 KiB
TypeScript

/**
* One way to put text on the clipboard, and one way to learn that it failed.
*
* Two facts make this worth its own module.
*
* The async Clipboard API does not exist outside a secure context, and a
* plain-http origin is an ordinary way to run DeepTutor rather than an exotic
* one: the launcher binds the web app to `0.0.0.0` while printing a
* `localhost` URL, so anyone opening the same instance from another machine
* on the LAN gets `http://<lan-ip>:3782` — where `navigator.clipboard` is
* `undefined` and touching `.writeText` throws a `TypeError` synchronously.
* `document.execCommand("copy")` still works there, which is the whole reason
* to keep a deprecated call around.
*
* And `writeText` rejects for reasons that have nothing to do with the caller
* — the document lost focus, the permission was denied. Callers kept
* swallowing those rejections next to a checkmark that said 已复制.
*
* So the contract here is: **this rejects when the text did not reach the
* clipboard.** A caller that wants to stay quiet can catch; a caller that
* tells the user it worked cannot do so without it having worked.
*/
/**
* The pre-Clipboard-API path: put the text in an off-screen field, select it,
* and let the browser copy the selection.
*
* It must stay synchronous. `execCommand` is only permitted while the user
* activation from the click is still live, and an `await` before it can spend
* that activation — which is why {@link copyText} decides whether to try the
* async API *before* awaiting anything.
*/
function copyViaSelection(text: string): boolean {
if (typeof document === "undefined" || !document.body) return false;
const field = document.createElement("textarea");
field.value = text;
field.setAttribute("readonly", "");
// Off-screen, but still laid out: an element that is `display:none` or
// `visibility:hidden` cannot hold a selection, so the copy would quietly
// succeed at copying nothing.
field.style.position = "fixed";
field.style.top = "0";
field.style.left = "-9999px";
field.style.opacity = "0";
field.style.pointerEvents = "none";
const previouslyFocused = document.activeElement;
document.body.appendChild(field);
try {
field.select();
field.setSelectionRange(0, text.length);
return document.execCommand("copy");
} catch {
return false;
} finally {
field.remove();
// Duck-typed rather than `instanceof HTMLElement`: the check runs inside a
// `finally`, and referencing a global that may not exist would replace the
// real outcome with a ReferenceError.
const focus = (previouslyFocused as { focus?: () => void } | null)?.focus;
if (typeof focus === "function") focus.call(previouslyFocused);
}
}
/**
* Copy `text`, or reject saying why.
*
* Call it from inside the event handler for the user's click — both paths
* depend on that click's user activation.
*/
export async function copyText(text: string): Promise<void> {
if (!text.trim()) throw new Error("There is nothing to copy.");
// Decided before the first `await`, deliberately — see `copyViaSelection`.
const asyncApiAvailable =
typeof navigator !== "undefined" &&
Boolean(navigator.clipboard) &&
typeof window !== "undefined" &&
window.isSecureContext;
if (asyncApiAvailable) {
try {
await navigator.clipboard.writeText(text);
return;
} catch {
// Focus lost, permission denied, or a policy block. The selection path
// sometimes still works, so it is worth one try before giving up.
}
}
if (!copyViaSelection(text)) {
throw new Error("The clipboard is not available in this browser.");
}
}