fix(utils): 修复复制问题

This commit is contained in:
yiqiuzheng 2023-04-05 02:49:44 +08:00
parent bd7567fa85
commit 0af55366f1

View File

@ -6,17 +6,23 @@ export function trimTopic(topic: string) {
} }
export async function copyToClipboard(text: string) { export async function copyToClipboard(text: string) {
if (navigator.clipboard) {
navigator.clipboard.writeText(text).catch(err => {
console.error('Failed to copy: ', err);
});
} else {
const textArea = document.createElement('textarea');
textArea.value = text;
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try { try {
await navigator.clipboard.writeText(text); document.execCommand('copy');
} catch (error) { console.log('Text copied to clipboard');
const textarea = document.createElement("textarea"); } catch (err) {
textarea.value = text; console.error('Failed to copy: ', err);
document.body.appendChild(textarea); }
textarea.select(); document.body.removeChild(textArea);
document.execCommand("copy");
document.body.removeChild(textarea);
} finally {
showToast(Locale.Copy.Success);
} }
} }