Skip to content

Commit

Permalink
fix: restore copy button in tabs (#1326)
Browse files Browse the repository at this point in the history
  • Loading branch information
tolluset authored Jan 21, 2025
1 parent f8d233e commit fd20f71
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 31 deletions.
37 changes: 29 additions & 8 deletions copy.client.ts
Original file line number Diff line number Diff line change
@@ -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);
}
});
});
24 changes: 1 addition & 23 deletions markdown-it/codeblock-copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,6 @@ export default function codeblockCopyPlugin(md: any) {
</button>
`;

const script = `
<script>
(function() {
const button = document.getElementById('${uniqueId}');
button.addEventListener('click', function() {
let textToCopy = this.getAttribute('data-copy');
// CLEAN COMMANDS: Remove leading spaces, $, and > from each line
textToCopy = textToCopy.replace(/^[\$>\s]+/, '');
navigator.clipboard.writeText(textToCopy).then(() => {
this.querySelector('.copy-icon').classList.add('hidden');
this.querySelector('.check-icon').classList.remove('hidden');
setTimeout(() => {
this.querySelector('.copy-icon').classList.remove('hidden');
this.querySelector('.check-icon').classList.add('hidden');
}, 2000);
});
});
})();
</script>
`;

return `<div class="relative">${render}${buttonHtml}${script}</div>`;
return `<div class="relative">${render}${buttonHtml}</div>`;
};
}

0 comments on commit fd20f71

Please sign in to comment.