longyvfengyun
2024-09-02 2b7d8aa9b25adb6de0e58e6a26a48a46bce3be83
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Inspired by https://github.com/Inndy/vue-clipboard2
import Clipboard from 'clipboard';
 
if (!Clipboard) {
  throw new Error('you should npm install `clipboard` --save at first ');
}
 
export default {
  mounted(el, binding) {
    if (binding.arg === 'success') {
      el._v_clipboard_success = binding.value;
    } else if (binding.arg === 'error') {
      el._v_clipboard_error = binding.value;
    } else {
      const clipboard = new Clipboard(el, {
        text() { return binding.value; },
        action() { return binding.arg === 'cut' ? 'cut' : 'copy'; }
      });
      clipboard.on('success', e => {
        const callback = el._v_clipboard_success;
        callback && callback(e) // eslint-disable-line
      });
      clipboard.on('error', e => {
        const callback = el._v_clipboard_error;
        callback && callback(e) // eslint-disable-line
      });
      el._v_clipboard = clipboard;
    }
  },
  updated(el, binding) {
    if (binding.arg === 'success') {
      el._v_clipboard_success = binding.value;
    } else if (binding.arg === 'error') {
      el._v_clipboard_error = binding.value;
    } else {
      el._v_clipboard.text = function() { return binding.value; };
      el._v_clipboard.action = function() { return binding.arg === 'cut' ? 'cut' : 'copy'; };
    }
  },
  unmounted(el, binding) {
    if (binding.arg === 'success') {
      delete el._v_clipboard_success;
    } else if (binding.arg === 'error') {
      delete el._v_clipboard_error;
    } else {
      el._v_clipboard.destroy();
      delete el._v_clipboard;
    }
  }
};