Files
cms/ruoyi-ui/src/directive/module/clipboard.js
T

55 lines
1.4 KiB
JavaScript
Raw Normal View History

2021-12-09 09:56:11 +08:00
/**
* v-clipboard 文字复制剪贴
* Copyright (c) 2021 ruoyi
*/
import Clipboard from 'clipboard'
export default {
bind(el, binding, vnode) {
switch (binding.arg) {
case 'success':
2025-04-27 10:05:51 +08:00
el._vClipBoard_success = binding.value
break
2021-12-09 09:56:11 +08:00
case 'error':
2025-04-27 10:05:51 +08:00
el._vClipBoard_error = binding.value
break
2021-12-09 09:56:11 +08:00
default: {
const clipboard = new Clipboard(el, {
text: () => binding.value,
action: () => binding.arg === 'cut' ? 'cut' : 'copy'
2025-04-27 10:05:51 +08:00
})
2021-12-09 09:56:11 +08:00
clipboard.on('success', e => {
2025-04-27 10:05:51 +08:00
const callback = el._vClipBoard_success
callback && callback(e)
})
2021-12-09 09:56:11 +08:00
clipboard.on('error', e => {
2025-04-27 10:05:51 +08:00
const callback = el._vClipBoard_error
callback && callback(e)
})
el._vClipBoard = clipboard
2021-12-09 09:56:11 +08:00
}
}
},
update(el, binding) {
if (binding.arg === 'success') {
2025-04-27 10:05:51 +08:00
el._vClipBoard_success = binding.value
2021-12-09 09:56:11 +08:00
} else if (binding.arg === 'error') {
2025-04-27 10:05:51 +08:00
el._vClipBoard_error = binding.value
2021-12-09 09:56:11 +08:00
} else {
2025-04-27 10:05:51 +08:00
el._vClipBoard.text = function () { return binding.value }
el._vClipBoard.action = () => binding.arg === 'cut' ? 'cut' : 'copy'
2021-12-09 09:56:11 +08:00
}
},
unbind(el, binding) {
if (!el._vClipboard) return
if (binding.arg === 'success') {
2025-04-27 10:05:51 +08:00
delete el._vClipBoard_success
2021-12-09 09:56:11 +08:00
} else if (binding.arg === 'error') {
2025-04-27 10:05:51 +08:00
delete el._vClipBoard_error
2021-12-09 09:56:11 +08:00
} else {
2025-04-27 10:05:51 +08:00
el._vClipBoard.destroy()
delete el._vClipBoard
2021-12-09 09:56:11 +08:00
}
}
}