Skip to content

Commit 2e90926

Browse files
committed
优化剪贴板功能 #86
1 parent 16539a5 commit 2e90926

File tree

4 files changed

+50
-23
lines changed

4 files changed

+50
-23
lines changed

src/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const tool = [
3939
{'name': 'json', 'title': 'JSON工具', 'cat': ['conversion', 'serialize']},
4040
{'name': 'url', 'title': 'URL编码', 'cat': ['conversion']},
4141
{'name': 'timestamp', 'title': '时间戳', 'cat': ['conversion']},
42-
{'name': 'qrCode', 'title': '二维码', 'cat': ['other']},
42+
{'name': 'qrCode', 'title': '二维码', 'cat': ['generate']},
4343
{'name': 'pinyin', 'title': '汉字转拼音', 'cat': ['conversion']},
4444
{'name': 'ip', 'title': 'IP地址查询', 'cat': ['other']},
4545
{'name': 'code', 'title': '代码格式化', 'cat': ['other']},

src/tool/clipboard.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {isUtools} from '../helper'
2+
// 剪贴板操作
3+
export const copy = (data,successCallback)=>{
4+
document.querySelector(
5+
'#clipboard').innerHTML = '<textarea id="clipboard-text"></textarea>'
6+
document.querySelector('#clipboard-text').value = data
7+
document.querySelector('#clipboard-text').select()
8+
if (document.execCommand('copy')) {
9+
successCallback && successCallback()
10+
}
11+
document.querySelector('#clipboard').innerHTML = ''
12+
}
13+
14+
export const paste = ()=>{
15+
document.querySelector(
16+
'#clipboard').innerHTML = '<textarea id="clipboard-text"></textarea>'
17+
document.querySelector('#clipboard-text').select()
18+
document.execCommand('paste')
19+
let r = document.querySelector('#clipboard-text').value ||
20+
document.querySelector('#clipboard-text').innerHTML
21+
document.querySelector('#clipboard').innerHTML = ''
22+
return r ? r : ''
23+
}
24+
25+
export const copyImage = (imageBase64,successCallback = "")=>{
26+
if (isUtools && imageBase64){
27+
window.utools.copyImage(imageBase64)
28+
successCallback && successCallback()
29+
}
30+
}
31+
32+
export default {
33+
copy,
34+
paste,
35+
copyImage
36+
}

src/tool/model.js

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import config from './config'
2+
import clipboard from './clipboard'
23
import setting from './setting'
34
import cache from './cache'
45
import history from './history.js'
@@ -43,17 +44,6 @@ const model = {
4344
}
4445
}
4546

46-
const clipboardPaste = function () {
47-
document.querySelector(
48-
'#clipboard').innerHTML = '<textarea id="clipboard-text"></textarea>'
49-
document.querySelector('#clipboard-text').select()
50-
document.execCommand('paste')
51-
let r = document.querySelector('#clipboard-text').value ||
52-
document.querySelector('#clipboard-text').innerHTML
53-
document.querySelector('#clipboard').innerHTML = ''
54-
return r ? r : ''
55-
}
56-
5747
export const plugin = {
5848
install: function (Vue) {
5949
Vue.prototype.$getToolData = function (clipboardField = '') {
@@ -63,7 +53,7 @@ export const plugin = {
6353
data[clipboardField] = fixeInputData
6454
fixeInputData = ""
6555
} else if (setting.autoReadCopy()) {
66-
let paste = clipboardPaste()
56+
let paste = clipboard.paste()
6757
if (!data[clipboardField] && paste) {
6858
if (setting.autoReadCopyFilter()){
6959
paste = paste.trim()
@@ -79,16 +69,17 @@ export const plugin = {
7969
}
8070
Vue.prototype.$clipboardCopy = function (data) {
8171
if (!setting.autoSaveCopy() || !data) return
82-
document.querySelector(
83-
'#clipboard').innerHTML = '<textarea id="clipboard-text"></textarea>'
84-
document.querySelector('#clipboard-text').value = data
85-
document.querySelector('#clipboard-text').select()
86-
if (document.execCommand('copy')) {
72+
clipboard.copy(data,()=>{
8773
this.$Message.success('结果已复制 ^o^')
88-
}
89-
document.querySelector('#clipboard').innerHTML = ''
74+
})
75+
}
76+
Vue.prototype.$clipboardCopyImages = function (data) {
77+
if (!setting.autoSaveCopy() || !data) return
78+
clipboard.copyImage(data,()=>{
79+
this.$Message.success('图片已复制 ^o^')
80+
})
9081
}
9182
},
9283
}
9384

94-
export default model
85+
export default model

src/views/tool/qrCode.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
generateHandle (str) {
8888
generator.toDataURL(str, (error, url) => {
8989
if (error) return this.$Message.error('二维码生成错误:' + error)
90-
this.$Message.success('生成成功')
90+
this.$clipboardCopyImages(url)
9191
this.current.generateOutput = `<img style="width:300px" src="${url}" />`
9292
})
9393
},
@@ -118,4 +118,4 @@
118118
}
119119
},
120120
}
121-
</script>
121+
</script>

0 commit comments

Comments
 (0)