mirror of
https://github.com/CaiJimmy/hugo-theme-stack.git
synced 2025-02-06 20:03:31 +08:00
de08e29b00
Now it can have i18n support
28 lines
1.0 KiB
TypeScript
28 lines
1.0 KiB
TypeScript
/**
|
|
* Copy button for code blocks
|
|
*/
|
|
export default () => {
|
|
const copyButtons = document.querySelectorAll('.codeblock-copy');
|
|
copyButtons.forEach(button => {
|
|
const codeblockID = button.getAttribute('data-id'),
|
|
copyText = button.textContent,
|
|
copiedText = button.getAttribute('data-copied-text');
|
|
if (!codeblockID) return;
|
|
button.addEventListener('click', (e) => {
|
|
e.preventDefault();
|
|
const codeblock = document.getElementById(codeblockID) as HTMLElement;
|
|
if (!codeblockID) return;
|
|
navigator.clipboard.writeText(codeblock.textContent)
|
|
.then(() => {
|
|
button.textContent = copiedText;
|
|
setTimeout(() => {
|
|
button.textContent = copyText;
|
|
}, 1000);
|
|
})
|
|
.catch(err => {
|
|
alert(err)
|
|
console.log('Something went wrong', err);
|
|
});
|
|
}, false);
|
|
});
|
|
} |