This commit is contained in:
Timothy Jaeryang Baek committed 2026-05-09 06:16:27 +09:00
1 parent af5628f8ef
commit f70b0da156
1 file changed
+16 -12
+16 -12
View File
@@ -511,28 +511,32 @@ export const copyToClipboard = async (text, html = null, formatted = false) => {
} else { } else {
let result = false; let result = false;
if (!navigator.clipboard) { if (!navigator.clipboard) {
const textArea = document.createElement('textarea'); const span = document.createElement('span');
textArea.value = text; span.textContent = text;
span.style.whiteSpace = 'pre';
span.style.position = 'fixed';
span.style.top = '0';
span.style.left = '0';
span.style.opacity = '0';
document.body.appendChild(span);
// Avoid scrolling to bottom const range = document.createRange();
textArea.style.top = '0'; range.selectNodeContents(span);
textArea.style.left = '0'; const selection = window.getSelection();
textArea.style.position = 'fixed'; selection?.removeAllRanges();
selection?.addRange(range);
document.body.appendChild(textArea);
textArea.focus({ preventScroll: true });
textArea.select();
try { try {
const successful = document.execCommand('copy'); const successful = document.execCommand('copy');
const msg = successful ? 'successful' : 'unsuccessful'; const msg = successful ? 'successful' : 'unsuccessful';
console.log('Fallback: Copying text command was ' + msg); console.log('Fallback: Copying text command was ' + msg);
result = true; result = successful;
} catch (err) { } catch (err) {
console.error('Fallback: Oops, unable to copy', err); console.error('Fallback: Oops, unable to copy', err);
} }
document.body.removeChild(textArea); selection?.removeAllRanges();
document.body.removeChild(span);
return result; return result;
} }