mirror of
https://github.com/fawney19/Aether.git
synced 2026-01-07 10:12:27 +08:00
44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
import { useToast } from './useToast'
|
|
|
|
export function useClipboard() {
|
|
const { success, error: showError } = useToast()
|
|
|
|
async function copyToClipboard(text: string): Promise<boolean> {
|
|
try {
|
|
if (navigator.clipboard && window.isSecureContext) {
|
|
await navigator.clipboard.writeText(text)
|
|
success('已复制到剪贴板')
|
|
return true
|
|
}
|
|
|
|
// Fallback for non-secure contexts
|
|
const textArea = document.createElement('textarea')
|
|
textArea.value = text
|
|
textArea.style.position = 'fixed'
|
|
textArea.style.left = '-999999px'
|
|
textArea.style.top = '-999999px'
|
|
document.body.appendChild(textArea)
|
|
textArea.focus()
|
|
textArea.select()
|
|
|
|
try {
|
|
const successful = document.execCommand('copy')
|
|
if (successful) {
|
|
success('已复制到剪贴板')
|
|
return true
|
|
}
|
|
showError('复制失败,请手动复制')
|
|
return false
|
|
} finally {
|
|
document.body.removeChild(textArea)
|
|
}
|
|
} catch (err) {
|
|
console.error('复制失败:', err)
|
|
showError('复制失败,请手动选择文本进行复制')
|
|
return false
|
|
}
|
|
}
|
|
|
|
return { copyToClipboard }
|
|
}
|