Skip to content

Commit df771d1

Browse files
authored
Merge pull request #6 from xwcoder/feature/copy
feat(reader): add copy url function
2 parents 232dc98 + 7b42cb4 commit df771d1

File tree

5 files changed

+88
-9
lines changed

5 files changed

+88
-9
lines changed

src/i18n/en-US.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
"markRead": "Mark as read",
1212
"markUnread": "Mark as unread",
1313
"refresh": "Refresh",
14-
"openExternal": "Open in browser"
14+
"openExternal": "Open in browser",
15+
"copy": {
16+
"title": "Copy url",
17+
"success": "Copied"
18+
}
1519
},
1620

1721
"form": {

src/i18n/zh-CN.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
"markRead": "标记为已读",
1212
"markUnread": "标记为未读",
1313
"refresh": "刷新",
14-
"openExternal": "在浏览器中打开"
14+
"openExternal": "在浏览器中打开",
15+
"copy": {
16+
"title": "复制链接",
17+
"success": "已复制到剪贴板"
18+
}
1519
},
1620

1721
"form": {

src/renderer/apps/reader/SidePanel/feed-item.tsx

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { observer } from 'mobx-react-lite'
44
import { makeStyles, tokens } from '@fluentui/react-components'
55
import { store } from '@/renderer/store'
6-
import { DeleteButton } from '@/renderer/apps/reader/components/toolbar'
6+
import { DeleteButton, CopyButton } from '@/renderer/apps/reader/components/toolbar'
77
import { Feed } from '@/types/reader'
88

99
const { readerStore } = store
@@ -29,7 +29,12 @@ const useStyles = makeStyles({
2929
})
3030

3131
function Item({ data }: Props) {
32-
const { id, title, unreadCount = 0 } = data
32+
const {
33+
id,
34+
title,
35+
unreadCount = 0,
36+
url,
37+
} = data
3338
const active = readerStore.tab === id
3439
const styles = useStyles()
3540

@@ -63,11 +68,18 @@ function Item({ data }: Props) {
6368
</div>
6469
<div className="absolute top-0 right-0 hidden group-hover:block">
6570
<div className={styles.toolbar}>
66-
<DeleteButton
67-
className="h-7"
68-
fontSize={16}
69-
onClick={deleteFeed}
70-
/>
71+
<div className="flex">
72+
<CopyButton
73+
className="h-7 w-8"
74+
fontSize={16}
75+
content={url}
76+
/>
77+
<DeleteButton
78+
className="h-7 w-8"
79+
fontSize={16}
80+
onClick={deleteFeed}
81+
/>
82+
</div>
7183
</div>
7284
</div>
7385
</div>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import intl from 'react-intl-universal'
2+
import { CopyRegular } from '@fluentui/react-icons'
3+
import {
4+
useId,
5+
useToastController,
6+
Toast,
7+
ToastIntent,
8+
ToastTitle,
9+
Toaster,
10+
} from '@fluentui/react-components'
11+
import Button from './button'
12+
13+
type Props = {
14+
className: string
15+
fontSize: number
16+
content: string
17+
}
18+
19+
export default function CopyButton(props: Props) {
20+
const { className, fontSize = 18, content } = props
21+
22+
const toasterId = useId('toaster')
23+
const { dispatchToast } = useToastController(toasterId)
24+
25+
const notify = (text: string, intent: ToastIntent) => dispatchToast(
26+
<Toast>
27+
<ToastTitle>
28+
{text}
29+
</ToastTitle>
30+
</Toast>,
31+
{ intent },
32+
)
33+
34+
const onClick = async () => {
35+
try {
36+
await navigator.clipboard.writeText(content)
37+
notify(intl.get('reader.action.copy.success'), 'success')
38+
} catch (e) {
39+
notify(`${(e as Error).name}: ${(e as Error).message}`, 'error')
40+
}
41+
}
42+
43+
return (
44+
<>
45+
<Button
46+
icon={CopyRegular}
47+
className={className}
48+
fontSize={fontSize}
49+
onClick={onClick}
50+
tip={intl.get('reader.action.copy.title')}
51+
/>
52+
<Toaster
53+
position="top-end"
54+
toasterId={toasterId}
55+
/>
56+
</>
57+
)
58+
}

src/renderer/apps/reader/components/toolbar/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export { default as RefreshButton } from './refresh-button'
33
export { default as DeleteButton } from './delete-button'
44
export { default as MarkReadButton } from './mark-read-button'
55
export { default as StarButton } from './star-button'
6+
export { default as CopyButton } from './copy-button'

0 commit comments

Comments
 (0)