From 74607971a2de90618e2e9102cb723fa28fbda2a6 Mon Sep 17 00:00:00 2001 From: Sayo Date: Fri, 31 Oct 2025 07:45:59 +0800 Subject: [PATCH] feat: codeblock add copy button when hover. --- src/components/ChatInterface.jsx | 72 +++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 5 deletions(-) diff --git a/src/components/ChatInterface.jsx b/src/components/ChatInterface.jsx index 9ba9027..7e53797 100644 --- a/src/components/ChatInterface.jsx +++ b/src/components/ChatInterface.jsx @@ -213,12 +213,74 @@ const markdownComponents = { ); } + const [copied, setCopied] = React.useState(false); + const textToCopy = raw; + + const handleCopy = () => { + const doSet = () => { + setCopied(true); + setTimeout(() => setCopied(false), 1500); + }; + try { + if (navigator && navigator.clipboard && navigator.clipboard.writeText) { + navigator.clipboard.writeText(textToCopy).then(doSet).catch(() => { + // Fallback + const ta = document.createElement('textarea'); + ta.value = textToCopy; + ta.style.position = 'fixed'; + ta.style.opacity = '0'; + document.body.appendChild(ta); + ta.select(); + try { document.execCommand('copy'); } catch {} + document.body.removeChild(ta); + doSet(); + }); + } else { + const ta = document.createElement('textarea'); + ta.value = textToCopy; + ta.style.position = 'fixed'; + ta.style.opacity = '0'; + document.body.appendChild(ta); + ta.select(); + try { document.execCommand('copy'); } catch {} + document.body.removeChild(ta); + doSet(); + } + } catch {} + }; + return ( -
-        
-          {children}
-        
-      
+
+ +
+          
+            {children}
+          
+        
+
); }, blockquote: ({ children }) => (