Skip to content

Commit

Permalink
Merge pull request #102 from Royal-lobster/feat/copy-answer
Browse files Browse the repository at this point in the history
  • Loading branch information
Royal-lobster authored Dec 29, 2024
2 parents 838475e + 1f182cc commit d9f4374
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions manifest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export default defineManifest(async (env) => ({
'contextMenus',
'tabs',
'activeTab',
'clipboardWrite',
],
background: {
service_worker: 'src/pages/background/index.ts',
Expand Down
32 changes: 30 additions & 2 deletions src/components/Sidebar/chat/ChatList.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { useEffect, useRef } from 'react'
import { RiCloseLine, RiErrorWarningLine, RiLoader4Line } from 'react-icons/ri'
import {
RiCloseLine,
RiErrorWarningLine,
RiLoader4Line,
RiFileCopyLine,
} from 'react-icons/ri'
import { ReactMarkdown } from 'react-markdown/lib/react-markdown'
import rehypeRaw from 'rehype-raw'
import remarkBreaks from 'remark-breaks'
Expand Down Expand Up @@ -33,6 +38,20 @@ const ChatList = ({

const filteredMsgs = messages.filter((msg) => msg.role !== ChatRole.SYSTEM)

const formatContent = (content: string) => {
return content.replace(/(?<=\n\n)(?![*-])\n/gi, '&nbsp;\n ')
}

const handleCopyMessage = (content: string) => {
window.parent.postMessage(
{
action: 'copy-to-clipboard',
_payload: { content },
},
'*',
)
}

return (
<div
ref={containerRef}
Expand Down Expand Up @@ -72,6 +91,15 @@ const ChatList = ({
<RiCloseLine />
</button>
)}
{msg.role === ChatRole.ASSISTANT && (
<button
type="button"
onClick={() => handleCopyMessage(formatContent(msg.content))}
className="cdx-absolute group-hover:cdx-visible cdx-invisible cdx-right-2 cdx-top-2 cdx-p-0.5 cdx-bg-black/20 cdx-rounded"
>
<RiFileCopyLine />
</button>
)}
<ReactMarkdown
remarkPlugins={[remarkGfm, remarkBreaks]}
rehypePlugins={[rehypeRaw]}
Expand All @@ -80,7 +108,7 @@ const ChatList = ({
table: Table,
}}
>
{msg.content.replace(/(?<=\n\n)(?![*-])\n/gi, '&nbsp;\n ')}
{formatContent(msg.content)}
</ReactMarkdown>
{msg.files && <FilePreviewBar files={msg.files} />}
</div>
Expand Down
8 changes: 8 additions & 0 deletions src/pages/content/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ window.addEventListener('message', async (event) => {
)
}

// ACTION: copy-to-clipboard =============================
if (action === 'copy-to-clipboard') {
const { content } = _payload as { content: string }
navigator.clipboard.writeText(content).catch((err) => {
console.error('Clipboard write failed', err)
})
}

// ACTION: get-screenshot-image ===========================
if (action === 'get-screenshot-image') {
iframe.style.width = '0px'
Expand Down

0 comments on commit d9f4374

Please sign in to comment.