Skip to content

Commit 5b03204

Browse files
committed
Fix copy on Safari
1 parent f1c4e02 commit 5b03204

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

src/components/common/CopyFileButton.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import styled from '@emotion/styled'
33
import { Button, Popover } from 'antd'
44
import { getFileApiURL, replaceWithProvidedAppName } from '../../utils'
55
import { CopyOutlined } from '@ant-design/icons'
6-
import copy from 'copy-to-clipboard'
76

87
const popoverContentOpts = {
98
default: 'Copy raw contents',
@@ -16,6 +15,29 @@ const CopyFileButton = styled(
1615
popoverContentOpts.default
1716
)
1817

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+
1941
if (!open) {
2042
return null
2143
}
@@ -30,12 +52,9 @@ const CopyFileButton = styled(
3052
setPopoverContent(popoverContentOpts.default)
3153
}}
3254
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+
)
3958
}}
4059
/>
4160
</Popover>

0 commit comments

Comments
 (0)