@@ -3,7 +3,6 @@ import styled from '@emotion/styled'
3
3
import { Button , Popover } from 'antd'
4
4
import { getFileApiURL , replaceWithProvidedAppName } from '../../utils'
5
5
import { CopyOutlined } from '@ant-design/icons'
6
- import copy from 'copy-to-clipboard'
7
6
8
7
const popoverContentOpts = {
9
8
default : 'Copy raw contents' ,
@@ -16,6 +15,29 @@ const CopyFileButton = styled(
16
15
popoverContentOpts . default
17
16
)
18
17
18
+ const fetchContent = ( ) =>
19
+ fetch ( getFileApiURL ( { packageName, version, path } ) )
20
+ . then ( ( response ) => response . json ( ) )
21
+ . then ( ( json ) => window . atob ( json . content ) )
22
+ . then ( ( content ) => replaceWithProvidedAppName ( content , appName ) )
23
+
24
+ const copyContent = ( ) => {
25
+ // From https://wolfgangrittner.dev/how-to-use-clipboard-api-in-firefox/
26
+ if ( typeof ClipboardItem && navigator . clipboard . write ) {
27
+ const item = new ClipboardItem ( {
28
+ 'text/plain' : fetchContent ( ) . then (
29
+ ( content ) => new Blob ( [ content ] , { type : 'text/plain' } )
30
+ ) ,
31
+ } )
32
+
33
+ return navigator . clipboard . write ( [ item ] )
34
+ } else {
35
+ return fetchContent ( ) . then ( ( content ) =>
36
+ navigator . clipboard . writeText ( content )
37
+ )
38
+ }
39
+ }
40
+
19
41
if ( ! open ) {
20
42
return null
21
43
}
@@ -30,12 +52,9 @@ const CopyFileButton = styled(
30
52
setPopoverContent ( popoverContentOpts . default )
31
53
} }
32
54
onClick = { ( ) => {
33
- fetch ( getFileApiURL ( { packageName, version, path } ) )
34
- . then ( ( response ) => response . json ( ) )
35
- . then ( ( json ) => window . atob ( json . content ) )
36
- . then ( ( content ) => replaceWithProvidedAppName ( content , appName ) )
37
- . then ( ( content ) => copy ( content ) )
38
- . then ( ( ) => setPopoverContent ( popoverContentOpts . copied ) )
55
+ copyContent ( ) . then ( ( ) =>
56
+ setPopoverContent ( popoverContentOpts . copied )
57
+ )
39
58
} }
40
59
/>
41
60
</ Popover >
0 commit comments