39 lines
2.1 KiB
JavaScript
39 lines
2.1 KiB
JavaScript
import './bootstrap';
|
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
const codeBlocks = document.querySelectorAll('pre');
|
|
|
|
codeBlocks.forEach((pre) => {
|
|
if (!pre.parentElement.classList.contains('code-block-wrapper')) {
|
|
const wrapper = document.createElement('div');
|
|
wrapper.className = 'code-block-wrapper';
|
|
pre.parentNode.insertBefore(wrapper, pre);
|
|
wrapper.appendChild(pre);
|
|
}
|
|
|
|
const wrapper = pre.parentElement;
|
|
const copyButton = document.createElement('button');
|
|
copyButton.className = 'copy-button';
|
|
copyButton.innerHTML = `<svg class="copy-icon" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"></path></svg> <span class="copy-text">Copy</span>`;
|
|
|
|
copyButton.addEventListener('click', () => {
|
|
const code = pre.querySelector('code');
|
|
const text = code ? code.textContent : pre.textContent;
|
|
|
|
navigator.clipboard.writeText(text).then(() => {
|
|
copyButton.classList.add('copied');
|
|
copyButton.innerHTML = `<svg fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path></svg> <span>Copied!</span>`;
|
|
|
|
setTimeout(() => {
|
|
copyButton.classList.remove('copied');
|
|
copyButton.innerHTML = `<svg class="copy-icon" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"></path></svg><span class="copy-text">Copy</span>`;
|
|
}, 2000);
|
|
}).catch((err) => {
|
|
console.error('Failed to copy:', err);
|
|
});
|
|
});
|
|
|
|
wrapper.appendChild(copyButton);
|
|
});
|
|
});
|