refac
This commit is contained in:
1 parent
af5628f8ef
commit
f70b0da156
1 file changed
+16
-12
+16
-12
@@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user