File tree Expand file tree Collapse file tree 2 files changed +29
-7
lines changed
src/client/components/Chat Expand file tree Collapse file tree 2 files changed +29
-7
lines changed Original file line number Diff line number Diff line change @@ -13,10 +13,12 @@ export const Response = ({
13
13
role,
14
14
content,
15
15
setMessage,
16
+ id,
16
17
} : {
17
18
role : Role
18
19
content : string
19
20
setMessage ?: any
21
+ id : string
20
22
} ) => {
21
23
const isUser = role === 'user'
22
24
@@ -27,19 +29,25 @@ export const Response = ({
27
29
< Box display = "flex" >
28
30
{ isUser ? (
29
31
< >
30
- { setMessage && < CopyToClipboardButton copied = { content } /> }
32
+ { setMessage && (
33
+ < CopyToClipboardButton copied = { content } id = { id } />
34
+ ) }
31
35
< Person sx = { { mx : 3 , my : 4 } } />
32
36
</ >
33
37
) : (
34
38
< >
35
- { setMessage && < CopyToClipboardButton copied = { content } /> }
39
+ { setMessage && (
40
+ < CopyToClipboardButton copied = { content } id = { id } />
41
+ ) }
36
42
< Assistant sx = { { mx : 3 , my : 4 } } />
37
43
</ >
38
44
) }
39
45
< 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 >
43
51
</ Box >
44
52
</ Box >
45
53
</ Paper >
@@ -70,6 +78,7 @@ const Conversation = ({
70
78
</ Box >
71
79
{ messages . map ( ( { role, content } , index ) => (
72
80
< Response
81
+ id = { `message-${ index } ` }
73
82
// eslint-disable-next-line react/no-array-index-key
74
83
key = { content + index }
75
84
role = { role }
@@ -91,7 +100,7 @@ const Conversation = ({
91
100
{ t ( 'chat:stop' ) }
92
101
</ Button >
93
102
</ Stack >
94
- < Response role = "assistant" content = { completion } />
103
+ < Response role = "assistant" content = { completion } id = "message" />
95
104
</ >
96
105
) }
97
106
</ Box >
Original file line number Diff line number Diff line change @@ -4,16 +4,29 @@ import ContentCopy from '@mui/icons-material/ContentCopy'
4
4
5
5
interface CopyToClipboardButtonProps {
6
6
copied : string
7
+ id : string
7
8
}
8
9
9
10
const CopyToClipboardButton : React . FC < CopyToClipboardButtonProps > = ( {
10
11
copied,
12
+ id,
11
13
} ) => {
12
14
const [ open , setOpen ] = useState ( false )
13
15
14
16
const handleClick = ( ) => {
15
17
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 )
17
30
}
18
31
19
32
return (
You can’t perform that action at this time.
0 commit comments