2354c04f208d7a1017bd1f3629ec90ff06851959.svn-base 1.6 KB

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