mirror of
https://gitee.com/wanwujie/sub2api
synced 2026-04-17 13:24:45 +08:00
41 lines
978 B
TypeScript
41 lines
978 B
TypeScript
|
|
import { ref } from 'vue'
|
||
|
|
import { useAppStore } from '@/stores/app'
|
||
|
|
|
||
|
|
export function useClipboard() {
|
||
|
|
const appStore = useAppStore()
|
||
|
|
const copied = ref(false)
|
||
|
|
|
||
|
|
const copyToClipboard = async (text: string, successMessage = 'Copied to clipboard') => {
|
||
|
|
if (!text) return false
|
||
|
|
|
||
|
|
try {
|
||
|
|
await navigator.clipboard.writeText(text)
|
||
|
|
copied.value = true
|
||
|
|
appStore.showSuccess(successMessage)
|
||
|
|
setTimeout(() => {
|
||
|
|
copied.value = false
|
||
|
|
}, 2000)
|
||
|
|
return true
|
||
|
|
} catch {
|
||
|
|
// Fallback for older browsers
|
||
|
|
const input = document.createElement('input')
|
||
|
|
input.value = text
|
||
|
|
document.body.appendChild(input)
|
||
|
|
input.select()
|
||
|
|
document.execCommand('copy')
|
||
|
|
document.body.removeChild(input)
|
||
|
|
copied.value = true
|
||
|
|
appStore.showSuccess(successMessage)
|
||
|
|
setTimeout(() => {
|
||
|
|
copied.value = false
|
||
|
|
}, 2000)
|
||
|
|
return true
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return {
|
||
|
|
copied,
|
||
|
|
copyToClipboard
|
||
|
|
}
|
||
|
|
}
|