@@ -47,9 +47,23 @@ export default class Clipboard extends React.Component {
47
47
return '{' + parts . join ( ',' ) + '}' ;
48
48
}
49
49
50
- async copySuccess ( content ) {
50
+ async copySuccess ( content , htmlContent ) {
51
51
const showCopiedIcon = 1000 ;
52
- await clipboardAPI . writeText ( content ) ;
52
+ if ( htmlContent ) {
53
+ const blobHtml = new Blob ( [ htmlContent ] , { type : 'text/html' } ) ;
54
+ const blobText = new Blob ( [ content ?? htmlContent ] , {
55
+ type : 'text/plain' ,
56
+ } ) ;
57
+ const data = [
58
+ new ClipboardItem ( {
59
+ [ 'text/plain' ] : blobText ,
60
+ [ 'text/html' ] : blobHtml ,
61
+ } ) ,
62
+ ] ;
63
+ await navigator . clipboard . write ( data ) ;
64
+ } else {
65
+ await clipboardAPI . writeText ( content ) ;
66
+ }
53
67
this . setState ( { copied : true } ) ;
54
68
await wait ( showCopiedIcon ) ;
55
69
this . setState ( { copied : false } ) ;
@@ -85,15 +99,17 @@ export default class Clipboard extends React.Component {
85
99
} ) ;
86
100
87
101
let content ;
102
+ let htmlContent ;
88
103
if ( this . props . target_id ) {
89
104
content = this . getTargetText ( ) ;
90
105
} else {
91
106
await wait ( 100 ) ; // gives time for callback to start
92
107
await this . loading ( ) ;
93
108
content = this . props . content ;
109
+ htmlContent = this . props . html_content ;
94
110
}
95
- if ( content ) {
96
- this . copySuccess ( content ) ;
111
+ if ( content || htmlContent ) {
112
+ this . copySuccess ( content , htmlContent ) ;
97
113
}
98
114
}
99
115
@@ -128,6 +144,7 @@ export default class Clipboard extends React.Component {
128
144
129
145
Clipboard . defaultProps = {
130
146
content : null ,
147
+ html_content : null ,
131
148
target_id : null ,
132
149
n_clicks : 0 ,
133
150
} ;
@@ -146,7 +163,7 @@ Clipboard.propTypes = {
146
163
target_id : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . object ] ) ,
147
164
148
165
/**
149
- * The text to be copied to the clipboard if the `target_id` is None.
166
+ * The text to be copied to the clipboard if the `target_id` is None.
150
167
*/
151
168
content : PropTypes . string ,
152
169
@@ -155,6 +172,11 @@ Clipboard.propTypes = {
155
172
*/
156
173
n_clicks : PropTypes . number ,
157
174
175
+ /**
176
+ * The clipboard html text be copied to the clipboard if the `target_id` is None.
177
+ */
178
+ html_content : PropTypes . string ,
179
+
158
180
/**
159
181
* The text shown as a tooltip when hovering over the copy icon.
160
182
*/
0 commit comments