Fix: Use robust copy fallback in code blocks

Co-authored-by: gemini-3.7-flash <noreply@google.com>
This commit is contained in:
2026-08-29 20:47:22 -07:00
parent 66a9921d86
commit 2b9a7b4c25

View File

@@ -1,4 +1,5 @@
import mathjax3 from 'https://esm.sh/markdown-it-mathjax3'; import mathjax3 from 'https://esm.sh/markdown-it-mathjax3';
import { copyToClipboard } from './utils.js';
export const md = window.md = window.markdownit({ html: false, linkify: true, typographer: true, breaks: true }).use(mathjax3); export const md = window.md = window.markdownit({ html: false, linkify: true, typographer: true, breaks: true }).use(mathjax3);
@@ -10,11 +11,10 @@ export function enhanceCodeBlocks(root, doHL = true) {
const len = code.textContent.length, countText = len >= 1e3 ? (len / 1e3).toFixed(1) + 'K' : len; const len = code.textContent.length, countText = len >= 1e3 ? (len / 1e3).toFixed(1) + 'K' : len;
const $btn = window.$('<button class="bg-slate-900 text-white rounded-lg py-1 px-2 text-xs opacity-85">Copy</button>').on('click', async e => { const $btn = window.$('<button class="bg-slate-900 text-white rounded-lg py-1 px-2 text-xs opacity-85">Copy</button>').on('click', async e => {
e.stopPropagation(); e.stopPropagation();
try { if (await copyToClipboard(code.innerText)) {
await navigator.clipboard.writeText(code.innerText);
$btn.text('Copied'); $btn.text('Copied');
setTimeout(() => $btn.text('Copy'), 1200); setTimeout(() => $btn.text('Copy'), 1200);
} catch { } }
}); });
const $container = window.$('<div class="code-actions absolute top-2 right-2 flex items-center gap-2"></div>'); const $container = window.$('<div class="code-actions absolute top-2 right-2 flex items-center gap-2"></div>');
$container.append(window.$(`<span class="text-xs text-gray-500">${countText} chars</span>`), $btn); $container.append(window.$(`<span class="text-xs text-gray-500">${countText} chars</span>`), $btn);