From 36421119b7b97337902225279fd6b508c82094ca Mon Sep 17 00:00:00 2001 From: ajspig Date: Fri, 12 Dec 2025 10:54:56 -0500 Subject: [PATCH] fix: Improving copy. Tries the current clipboardAPI & then does textarea/execCommand method for mobile. --- quartz/components/scripts/copypage.inline.ts | 32 ++++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/quartz/components/scripts/copypage.inline.ts b/quartz/components/scripts/copypage.inline.ts index 7e57b8650..0003c6afb 100644 --- a/quartz/components/scripts/copypage.inline.ts +++ b/quartz/components/scripts/copypage.inline.ts @@ -3,20 +3,31 @@ const svgCopy = const svgCheck = '' +async function copyToClipboard(text: string): Promise { + try { + await navigator.clipboard.writeText(text) + return true + } catch { + // Fallback for mobile browsers + const textarea = Object.assign(document.createElement("textarea"), { + value: text, + style: "position:fixed;left:-9999px", + }) + document.body.appendChild(textarea) + textarea.select() + const ok = document.execCommand("copy") + document.body.removeChild(textarea) + return ok + } +} + async function copyPageMarkdown(slug: string): Promise { try { - // Use full blog content for home page, individual page content otherwise const llmsUrl = slug === "index" ? `/llms-full.txt` : `/${slug}/llms.txt` const response = await fetch(llmsUrl) - if (!response.ok) { - console.error("Failed to fetch markdown:", response.statusText) - return false - } - const markdown = await response.text() - await navigator.clipboard.writeText(markdown) - return true - } catch (error) { - console.error("Failed to copy page markdown:", error) + if (!response.ok) return false + return copyToClipboard(await response.text()) + } catch { return false } } @@ -112,3 +123,4 @@ document.addEventListener("nav", () => { }) +