MCPcopy Create free account
hub / github.com/APIParkLab/APIPark / copyToClipboard

Function copyToClipboard

frontend/packages/common/src/hooks/copy.ts:9–48  ·  view source on GitHub ↗
(text: string)

Source from the content-addressed store, hash-verified

7 const [isCopied, setIsCopied] = useState(false)
8
9 const copyToClipboard = (text: string) => {
10 if (navigator.clipboard && window.isSecureContext) {
11 navigator.clipboard
12 .writeText(text)
13 ?.then(() => {
14 message.success($t(RESPONSE_TIPS.copySuccess))
15 setIsCopied(true)
16 })
17 .catch((error) => {
18 console.error('Failed to copy text to clipboard:', error)
19 })
20 } else {
21 // 创建text partition
22 const textArea = document.createElement('textarea')
23 textArea.value = text
24 // 使text area不在viewport,同时设置不可见
25 textArea.style.position = 'absolute'
26 textArea.style.opacity = 0 + ''
27 textArea.style.left = '-999999px'
28 textArea.style.top = '-999999px'
29 document.body.appendChild(textArea)
30 // textArea.focus();
31 textArea.select()
32 new Promise<void>((resolve, reject) => {
33 if (document.execCommand('copy')) {
34 message.success($t(RESPONSE_TIPS.copySuccess))
35 setIsCopied(true)
36 resolve()
37 } else {
38 reject('Failed to copy text to clipboard:')
39 }
40 })
41 .catch((error) => {
42 console.error('Failed to copy text to clipboard:', error)
43 })
44 .finally(() => {
45 textArea.remove()
46 })
47 }
48 }
49
50 useEffect(() => {
51 if (isCopied) {

Callers 6

handleCopyFunction · 0.85
handleCopyFunction · 0.85
handleCopyFunction · 0.85
renderAddressesFunction · 0.85
handleCopyFunction · 0.85
copyURLFunction · 0.85

Calls 2

$tFunction · 0.90
createElementMethod · 0.45

Tested by

no test coverage detected