-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShareButton.tsx
75 lines (65 loc) · 1.99 KB
/
ShareButton.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'use client'
import type * as monaco from 'monaco-editor'
import { Button } from '@/components/ui/button'
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'
import { ChevronDown, Hash, Link } from 'lucide-react'
import React, { useCallback, useState } from 'react'
import { toast } from 'sonner'
interface ShareButtonProps {
editor: monaco.editor.IStandaloneCodeEditor | undefined
}
const ShareButton: React.FC<ShareButtonProps> = React.memo(({ editor }) => {
const [isOpen, setIsOpen] = useState(false)
const handleShare = useCallback((type: 'url' | 'hash' | 'picture' | 'picture-compact') => {
if (!editor) {
return
}
if (editor.getValue().trim() === '') {
toast.warning('请先输入代码')
return
}
const action = editor.getAction(`cangjie.share.${type}`)
if (!action) {
console.error(`Action cangjie.share.${type} not found`)
return
}
action.run().then(() => {
setIsOpen(false)
}).catch((error) => {
console.error('Share failed:', error)
})
}, [editor])
return (
<Popover open={isOpen} onOpenChange={setIsOpen}>
<PopoverTrigger asChild>
<Button className="w-full sm:w-auto">
分享
{' '}
<ChevronDown className="ml-2 h-4 w-4" />
</Button>
</PopoverTrigger>
<PopoverContent className="w-56">
<div className="flex flex-col space-y-2">
<Button
variant="ghost"
className="justify-start"
onClick={() => handleShare('url')}
>
<Link className="mr-2 h-4 w-4" />
URL 方式
</Button>
<Button
variant="ghost"
className="justify-start"
onClick={() => handleShare('hash')}
>
<Hash className="mr-2 h-4 w-4" />
Hash 方式
</Button>
</div>
</PopoverContent>
</Popover>
)
})
ShareButton.displayName = 'ShareButton'
export default ShareButton