diff --git a/copy.client.ts b/copy.client.ts index 22d99c5a0..608a540d4 100644 --- a/copy.client.ts +++ b/copy.client.ts @@ -1,10 +1,31 @@ -const copyBtns = document.querySelectorAll("button[data-copy]"); - -copyBtns.forEach((btn) => { - btn.addEventListener("click", () => { - let textToCopy = btn.getAttribute("data-copy") as string; - // CLEAN COMMANDS: Remove leading spaces, $, and > from each line - textToCopy = textToCopy.replace(/^[\$>\s]+/, ""); - navigator?.clipboard?.writeText(textToCopy); +document.addEventListener("click", (event) => { + const btn = (event.target as HTMLElement).closest("button[data-copy]"); + + if (!btn) { + return; + } + + let textToCopy = btn.getAttribute("data-copy") as string; + + // CLEAN COMMANDS: Remove leading spaces, $, and > from each line + textToCopy = textToCopy.replace(/^[\$>\s]+/, ""); + + navigator?.clipboard?.writeText(textToCopy).then(() => { + if (!btn) { + return; + } + + const copyIcon = btn.querySelector(".copy-icon"); + const checkIcon = btn.querySelector(".check-icon"); + + if (copyIcon && checkIcon) { + copyIcon.classList.add("hidden"); + checkIcon.classList.remove("hidden"); + + setTimeout(() => { + copyIcon.classList.remove("hidden"); + checkIcon.classList.add("hidden"); + }, 2000); + } }); }); diff --git a/markdown-it/codeblock-copy.ts b/markdown-it/codeblock-copy.ts index e65b3f7f5..52c3836b1 100644 --- a/markdown-it/codeblock-copy.ts +++ b/markdown-it/codeblock-copy.ts @@ -30,28 +30,6 @@ export default function codeblockCopyPlugin(md: any) { `; - const script = ` - - `; - - return `
${render}${buttonHtml}${script}
`; + return `
${render}${buttonHtml}
`; }; }