import { Directive } from 'vue'
import Clipboard from 'clipboard'

/**
 * 复制文本到剪贴板
 */
export const copy: Directive = {
    mounted(el, binding) {
        const fn = binding.value
        el.clipboard = new Clipboard(el)
        el.clipboard.on('success', () => {
            if (fn instanceof Function) {
                fn(true)
            }
        })
        el.clipboard.on('error', () => {
            if (fn instanceof Function) {
                fn(false)
            }
        })
    },
    unmounted(el) {
        el.clipboard.destroy()
    }
}