From 6027da4206e19990bfadf65b9ea2c16c95ea8ca2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=A4=E5=A4=A7=E7=BE=8A?= Date: Mon, 29 Apr 2024 14:56:19 +0800 Subject: [PATCH] fix: the clipboard.ts to address the issue of text copying failure when accessing it from the internet and when a modal dialog is open. --- frontend/src/utils/clipboard.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/frontend/src/utils/clipboard.ts b/frontend/src/utils/clipboard.ts index 51851007..1c8856c0 100644 --- a/frontend/src/utils/clipboard.ts +++ b/frontend/src/utils/clipboard.ts @@ -46,16 +46,25 @@ export function copy(text: string) { textarea.style.outline = "none"; textarea.style.boxShadow = "none"; textarea.style.background = "transparent"; - document.body.appendChild(textarea); + + let body: Element | null = null; + const focusedElement = document.activeElement; + if (focusedElement){ + body = focusedElement + }else{ + body = document.body + } + body.appendChild(textarea) + textarea.focus(); textarea.select(); try { document.execCommand("copy"); - document.body.removeChild(textarea); resolve(); } catch (e) { - document.body.removeChild(textarea); reject(e); + }finally{ + body.removeChild(textarea) } } else { reject(