diff --git a/components/text-selection-toolbar.tsx b/components/text-selection-toolbar.tsx index f6ba060b..9bcc76ca 100644 --- a/components/text-selection-toolbar.tsx +++ b/components/text-selection-toolbar.tsx @@ -283,12 +283,21 @@ export function TextSelectionToolbar() { e.preventDefault(); const formatted = formatCopyText(text); + // The native copy event is synchronous — we can't await the short-link + // fetch before setData, so this writes the long highlight URL. It's a + // valid fallback and stands if the async upgrade below can't run. e.clipboardData?.setData("text/plain", formatted); + // Upgrade the clipboard to the shortened /share/ URL: copy() hands + // the Clipboard API a *promise* of the shortened text, which keeps this + // gesture's activation alive while createShortLink resolves (the same + // trick the toolbar's Copy button uses). Only when there's a URL to + // shorten in the payload. + if (formatted.includes("?hl=")) void copy(formatted); } document.addEventListener("copy", handleCopy); return () => document.removeEventListener("copy", handleCopy); - }, []); + }, [copy]); // Close toolbar on mouse click outside (desktop only) useEffect(() => {