From a4fb301e6b78cb073fee86ab5f3b5beae2c1325a Mon Sep 17 00:00:00 2001 From: tolluset Date: Sun, 12 Jan 2025 14:39:26 +0900 Subject: [PATCH] fix: restore copy button in tabs --- copy.client.ts | 37 +++++++++++++++++++++++++++-------- markdown-it/codeblock-copy.ts | 24 +---------------------- 2 files changed, 30 insertions(+), 31 deletions(-) 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}
`; }; }