Spaces:
Sleeping
Sleeping
File size: 1,089 Bytes
01d5a5d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
const manualCopyFallback = (textToCopy: string): void => {
const textArea = document.createElement('textarea');
textArea.value = textToCopy;
textArea.style.position = 'absolute';
textArea.style.left = '-999999px';
document.body.prepend(textArea);
textArea.select();
try {
document.execCommand('copy');
} catch (error) {
console.error(error);
} finally {
textArea.remove();
}
};
export const copyToClipboard = (textToCopy: string) => {
return new Promise<void>((resolve, reject) => {
if (navigator.clipboard && window.isSecureContext) {
setTimeout(() => {
navigator.clipboard
.writeText(textToCopy)
.then(() => {
resolve();
})
.catch(() => {
try {
manualCopyFallback(textToCopy);
resolve();
} catch (manualError) {
reject(manualError);
}
});
});
} else {
try {
manualCopyFallback(textToCopy);
resolve();
} catch {
reject();
}
}
});
};
|