From 85c461bbfa7c01ad21e38f6c8147c1cb72974762 Mon Sep 17 00:00:00 2001 From: samanhappy Date: Fri, 18 Apr 2025 15:01:24 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20enhance=20clipboard=20copy=20functionali?= =?UTF-8?q?ty=20with=20fallback=20for=20unsupport=E2=80=A6=20(#15)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/components/GroupCard.tsx | 48 +++++++++++++++++++-------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/frontend/src/components/GroupCard.tsx b/frontend/src/components/GroupCard.tsx index 2a4f07d..c5c36dc 100644 --- a/frontend/src/components/GroupCard.tsx +++ b/frontend/src/components/GroupCard.tsx @@ -11,10 +11,10 @@ interface GroupCardProps { onDelete: (groupId: string) => void } -const GroupCard = ({ - group, - servers, - onEdit, +const GroupCard = ({ + group, + servers, + onEdit, onDelete }: GroupCardProps) => { const { t } = useTranslation() @@ -35,10 +35,31 @@ const GroupCard = ({ } const copyToClipboard = () => { - navigator.clipboard.writeText(group.id).then(() => { - setCopied(true) - setTimeout(() => setCopied(false), 2000) - }) + if (navigator.clipboard && window.isSecureContext) { + navigator.clipboard.writeText(group.id).then(() => { + setCopied(true) + setTimeout(() => setCopied(false), 2000) + }) + } else { + // Fallback for HTTP or unsupported clipboard API + const textArea = document.createElement('textarea') + textArea.value = group.id + // Avoid scrolling to bottom + textArea.style.position = 'fixed' + textArea.style.left = '-9999px' + document.body.appendChild(textArea) + textArea.focus() + textArea.select() + try { + document.execCommand('copy') + setCopied(true) + setTimeout(() => setCopied(false), 2000) + } catch (err) { + alert(t('common.copyFailed') || 'Copy failed') + console.error('Copy to clipboard failed:', err) + } + document.body.removeChild(textArea) + } } // Get servers that belong to this group @@ -52,7 +73,7 @@ const GroupCard = ({

{group.name}

{group.id} -