From f3dbe5a25116bc9487edd5165cf8cbe442655264 Mon Sep 17 00:00:00 2001 From: Yidadaa Date: Thu, 6 Apr 2023 23:18:51 +0800 Subject: [PATCH] fix: #513 show toast after copying --- app/utils.ts | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/app/utils.ts b/app/utils.ts index 9fcb1182..bb44e072 100644 --- a/app/utils.ts +++ b/app/utils.ts @@ -7,23 +7,21 @@ export function trimTopic(topic: 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'); + try { + await navigator.clipboard.writeText(text); + } catch (error) { + const textArea = document.createElement("textarea"); textArea.value = text; document.body.appendChild(textArea); textArea.focus(); textArea.select(); try { - document.execCommand('copy'); - console.log('Text copied to clipboard'); - } catch (err) { - console.error('Failed to copy: ', err); + document.execCommand("copy"); + } catch (error) { + showToast(Locale.Copy.Failed); } - document.body.removeChild(textArea); + } finally { + showToast(Locale.Copy.Success); } }