62 lines
2.2 KiB
JavaScript
Raw Normal View History

2023-04-27 14:07:46 +08:00
// clipboard.js
function initClipboard(codeBlocks, onApplyButtonClick, onApplyCodeButtonClick, onApplyCodeFileButtonClick) {
codeBlocks.forEach(block => {
2023-04-27 14:07:46 +08:00
const contentSpan = document.createElement('span');
contentSpan.innerHTML = block.innerHTML;
block.innerHTML = '';
block.appendChild(contentSpan);
const copyButton = document.createElement('button');
copyButton.classList.add('copy-button');
copyButton.innerText = 'Copy';
block.appendChild(copyButton);
copyButton.addEventListener('click', () => {
// Copy the message text to the clipboard
2023-04-27 14:07:46 +08:00
navigator.clipboard.writeText(contentSpan.textContent);
// Change the button text temporarily to show that the text has been copied
copyButton.textContent = 'Copied!';
// Reset the button text after a short delay
setTimeout(() => {
copyButton.textContent = '';
const copyIcon = document.createElement('i');
copyIcon.classList.add('fas', 'fa-copy');
copyButton.appendChild(copyIcon);
}, 1000);
});
2023-04-27 14:07:46 +08:00
// Add 'Apply' button
const applyButton = document.createElement('button');
applyButton.classList.add('apply-button');
applyButton.innerText = 'Apply Patch';
block.appendChild(applyButton);
applyButton.addEventListener('click', () => {
onApplyButtonClick(contentSpan.textContent);
});
// Add 'Apply' button
const applyCodeButton = document.createElement('button');
applyCodeButton.classList.add('apply-button');
applyCodeButton.innerText = 'Insert Code';
block.appendChild(applyCodeButton);
applyCodeButton.addEventListener('click', () => {
onApplyCodeButtonClick(contentSpan.textContent);
});
// Add 'Apply' button
const applyCodeFileButton = document.createElement('button');
applyCodeFileButton.classList.add('apply-button');
applyCodeFileButton.innerText = 'Relace File';
block.appendChild(applyCodeFileButton);
applyCodeFileButton.addEventListener('click', () => {
onApplyCodeFileButtonClick(contentSpan.textContent);
});
});
}