@@ -25,6 +25,15 @@ export default class extends React.PureComponent {
25
25
}
26
26
}
27
27
28
+ copyToClipboardFallback = ( textToCopy ) => {
29
+ const textArea = document . createElement ( 'textarea' ) ;
30
+ textArea . value = textToCopy ;
31
+ document . body . appendChild ( textArea ) ;
32
+ textArea . select ( ) ;
33
+ document . execCommand ( 'copy' ) ;
34
+ document . body . removeChild ( textArea ) ;
35
+ }
36
+
28
37
handleCopy = ( ) => {
29
38
const { clickCallback, src, namespace } = this . props ;
30
39
@@ -35,21 +44,13 @@ export default class extends React.PureComponent {
35
44
) ;
36
45
37
46
if ( navigator . clipboard ) {
38
- navigator . clipboard . writeText ( textToCopy ) . catch ( err => {
47
+ navigator . clipboard . writeText ( textToCopy ) . catch ( ( ) => {
39
48
// Fallback for non-secure contexts (i.e. http)
40
- const textArea = document . createElement ( 'textarea' ) ;
41
- textArea . value = textToCopy ;
42
- document . body . appendChild ( textArea ) ;
43
- textArea . select ( ) ;
44
- document . execCommand ( 'copy' ) ;
45
- document . body . removeChild ( textArea ) ;
49
+ copyToClipboardFallback ( textToCopy ) ;
46
50
} ) ;
47
51
} else {
48
- console . error (
49
- 'react-json-view error:' ,
50
- 'navigator.clipboard not supported by this browser'
51
- ) ;
52
- return ;
52
+ // Fallback for old browsers and test environments
53
+ copyToClipboardFallback ( textToCopy ) ;
53
54
} ;
54
55
55
56
this . copiedTimer = setTimeout ( ( ) => {
0 commit comments