Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Enable Copy with Markdown Formatting #102

Merged
merged 3 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 6 additions & 0 deletions src/pages/content/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ window.addEventListener('message', async (event) => {
)
}

// ACTION: copy-to-clipboard =============================
if (action === 'copy-to-clipboard') {
const { content } = _payload as { content: string }
navigator.clipboard.writeText(content)
}

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