提交 | 用户 | 时间
|
e57a89
|
1 |
/** |
懒 |
2 |
* v-clipboard 文字复制剪贴 |
|
3 |
* Copyright (c) 2021 jcdm |
|
4 |
*/ |
|
5 |
|
|
6 |
import Clipboard from 'clipboard' |
|
7 |
export default { |
|
8 |
bind(el, binding, vnode) { |
|
9 |
switch (binding.arg) { |
|
10 |
case 'success': |
|
11 |
el._vClipBoard_success = binding.value; |
|
12 |
break; |
|
13 |
case 'error': |
|
14 |
el._vClipBoard_error = binding.value; |
|
15 |
break; |
|
16 |
default: { |
|
17 |
const clipboard = new Clipboard(el, { |
|
18 |
text: () => binding.value, |
|
19 |
action: () => binding.arg === 'cut' ? 'cut' : 'copy' |
|
20 |
}); |
|
21 |
clipboard.on('success', e => { |
|
22 |
const callback = el._vClipBoard_success; |
|
23 |
callback && callback(e); |
|
24 |
}); |
|
25 |
clipboard.on('error', e => { |
|
26 |
const callback = el._vClipBoard_error; |
|
27 |
callback && callback(e); |
|
28 |
}); |
|
29 |
el._vClipBoard = clipboard; |
|
30 |
} |
|
31 |
} |
|
32 |
}, |
|
33 |
update(el, binding) { |
|
34 |
if (binding.arg === 'success') { |
|
35 |
el._vClipBoard_success = binding.value; |
|
36 |
} else if (binding.arg === 'error') { |
|
37 |
el._vClipBoard_error = binding.value; |
|
38 |
} else { |
|
39 |
el._vClipBoard.text = function () { return binding.value; }; |
|
40 |
el._vClipBoard.action = () => binding.arg === 'cut' ? 'cut' : 'copy'; |
|
41 |
} |
|
42 |
}, |
|
43 |
unbind(el, binding) { |
|
44 |
if (!el._vClipboard) return |
|
45 |
if (binding.arg === 'success') { |
|
46 |
delete el._vClipBoard_success; |
|
47 |
} else if (binding.arg === 'error') { |
|
48 |
delete el._vClipBoard_error; |
|
49 |
} else { |
|
50 |
el._vClipBoard.destroy(); |
|
51 |
delete el._vClipBoard; |
|
52 |
} |
|
53 |
} |
|
54 |
} |