mirror of
https://github.com/oobabooga/text-generation-webui.git
synced 2026-02-20 14:44:58 +01:00
Update main.js
This commit is contained in:
parent
652d13c003
commit
acabe61532
56
js/main.js
56
js/main.js
|
|
@ -1105,3 +1105,59 @@ document.fonts.addEventListener("loadingdone", (event) => {
|
|||
// Initial call to set the margin based on current state
|
||||
updateMargin();
|
||||
})();
|
||||
|
||||
//------------------------------------------------
|
||||
// Fix for Copy Button (with HTTP Fallback)
|
||||
//------------------------------------------------
|
||||
document.addEventListener('click', function(e) {
|
||||
const copyBtn = e.target.closest('.hljs-copy-button');
|
||||
if (!copyBtn) return;
|
||||
|
||||
const pre = copyBtn.closest('pre');
|
||||
const code = pre ? pre.querySelector('code') : null;
|
||||
|
||||
if (code) {
|
||||
const text = code.innerText;
|
||||
|
||||
function copyToClipboard(content) {
|
||||
if (navigator.clipboard && window.isSecureContext) {
|
||||
return navigator.clipboard.writeText(content);
|
||||
} else {
|
||||
return new Promise((resolve, reject) => {
|
||||
const textArea = document.createElement("textarea");
|
||||
textArea.value = content;
|
||||
textArea.style.position = "fixed";
|
||||
textArea.style.left = "-9999px";
|
||||
textArea.style.top = "-9999px";
|
||||
document.body.appendChild(textArea);
|
||||
textArea.focus();
|
||||
textArea.select();
|
||||
try {
|
||||
const successful = document.execCommand('copy');
|
||||
document.body.removeChild(textArea);
|
||||
successful ? resolve() : reject();
|
||||
} catch (err) {
|
||||
document.body.removeChild(textArea);
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
copyToClipboard(text).then(() => {
|
||||
const originalText = copyBtn.innerText;
|
||||
// Update button state
|
||||
copyBtn.innerText = 'Copied!';
|
||||
copyBtn.setAttribute('data-copied', 'true');
|
||||
copyBtn.style.pointerEvents = 'none'; // Prevent double clicks
|
||||
|
||||
setTimeout(() => {
|
||||
copyBtn.innerText = 'Copy';
|
||||
copyBtn.setAttribute('data-copied', 'false');
|
||||
copyBtn.style.pointerEvents = 'auto';
|
||||
}, 2000);
|
||||
}).catch(err => {
|
||||
console.error('Copy failed:', err);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue