Commit ca4290204c462c763f962060b36ce5c3eccb355e

Authored by YunaiV
1 parent fff6fedc

v3.8.0 自定义文字复制剪贴指令

yudao-ui-admin/src/directive/index.js
... ... @@ -3,10 +3,12 @@ import hasPermi from './permission/hasPermi'
3 3 import dialogDrag from './dialog/drag'
4 4 import dialogDragWidth from './dialog/dragWidth'
5 5 import dialogDragHeight from './dialog/dragHeight'
  6 +import clipboard from './module/clipboard'
6 7  
7 8 const install = function(Vue) {
8 9 Vue.directive('hasRole', hasRole)
9 10 Vue.directive('hasPermi', hasPermi)
  11 + Vue.directive('clipboard', clipboard)
10 12 Vue.directive('dialogDrag', dialogDrag)
11 13 Vue.directive('dialogDragWidth', dialogDragWidth)
12 14 Vue.directive('dialogDragHeight', dialogDragHeight)
... ...
yudao-ui-admin/src/directive/module/clipboard.js 0 → 100644
  1 +/**
  2 +* v-clipboard 文字复制剪贴
  3 +* Copyright (c) 2021 ruoyi
  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 +}
... ...