Skip to content

Commit 32ed6da

Browse files
committed
better copy paste
1 parent 8db6535 commit 32ed6da

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

src/client/components/Chat/Conversation.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ export const Response = ({
1313
role,
1414
content,
1515
setMessage,
16+
id,
1617
}: {
1718
role: Role
1819
content: string
1920
setMessage?: any
21+
id: string
2022
}) => {
2123
const isUser = role === 'user'
2224

@@ -27,19 +29,25 @@ export const Response = ({
2729
<Box display="flex">
2830
{isUser ? (
2931
<>
30-
{setMessage && <CopyToClipboardButton copied={content} />}
32+
{setMessage && (
33+
<CopyToClipboardButton copied={content} id={id} />
34+
)}
3135
<Person sx={{ mx: 3, my: 4 }} />
3236
</>
3337
) : (
3438
<>
35-
{setMessage && <CopyToClipboardButton copied={content} />}
39+
{setMessage && (
40+
<CopyToClipboardButton copied={content} id={id} />
41+
)}
3642
<Assistant sx={{ mx: 3, my: 4 }} />
3743
</>
3844
)}
3945
<Box pr={7} py={2}>
40-
<ReactMarkdown remarkPlugins={[remarkGfm]}>
41-
{content}
42-
</ReactMarkdown>
46+
<div id={id}>
47+
<ReactMarkdown remarkPlugins={[remarkGfm]}>
48+
{content}
49+
</ReactMarkdown>
50+
</div>
4351
</Box>
4452
</Box>
4553
</Paper>
@@ -70,6 +78,7 @@ const Conversation = ({
7078
</Box>
7179
{messages.map(({ role, content }, index) => (
7280
<Response
81+
id={`message-${index}`}
7382
// eslint-disable-next-line react/no-array-index-key
7483
key={content + index}
7584
role={role}
@@ -91,7 +100,7 @@ const Conversation = ({
91100
{t('chat:stop')}
92101
</Button>
93102
</Stack>
94-
<Response role="assistant" content={completion} />
103+
<Response role="assistant" content={completion} id="message" />
95104
</>
96105
)}
97106
</Box>

src/client/components/Chat/CopyToClipboardButton.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,29 @@ import ContentCopy from '@mui/icons-material/ContentCopy'
44

55
interface CopyToClipboardButtonProps {
66
copied: string
7+
id: string
78
}
89

910
const CopyToClipboardButton: React.FC<CopyToClipboardButtonProps> = ({
1011
copied,
12+
id,
1113
}) => {
1214
const [open, setOpen] = useState(false)
1315

1416
const handleClick = () => {
1517
setOpen(true)
16-
navigator.clipboard.writeText(copied)
18+
19+
const innerHtml = document.getElementById(id).innerHTML
20+
const blobHtml = new Blob([innerHtml], { type: 'text/html' })
21+
const blobText = new Blob([copied], { type: 'text/plain' })
22+
const data = [
23+
new ClipboardItem({
24+
'text/plain': blobText,
25+
'text/html': blobHtml,
26+
}),
27+
]
28+
29+
navigator.clipboard.write(data)
1730
}
1831

1932
return (

0 commit comments

Comments
 (0)