Skip to content

Commit eb27578

Browse files
committed
Fix(clipboard): remove duplicated success callback function for clipboard directive
1 parent 6205e38 commit eb27578

File tree

4 files changed

+760
-631
lines changed

4 files changed

+760
-631
lines changed

.circleci/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ version: 2
77
jobs:
88
build:
99
docker:
10-
- image: circleci/node:11
10+
- image: circleci/node:12
1111

1212
working_directory: ~/vue-typescript-admin-template
1313

package.json

+25-25
Original file line numberDiff line numberDiff line change
@@ -17,93 +17,93 @@
1717
"@tinymce/tinymce-vue": "^2.1.0",
1818
"axios": "^0.19.0",
1919
"clipboard": "^2.0.4",
20-
"codemirror": "^5.48.0",
20+
"codemirror": "^5.48.2",
2121
"cors": "^2.8.5",
2222
"driver.js": "^0.9.7",
2323
"echarts": "^4.2.1",
24-
"element-ui": "^2.10.1",
24+
"element-ui": "^2.11.1",
2525
"faker": "^4.1.0",
2626
"file-saver": "^2.0.2",
2727
"fuse.js": "^3.4.5",
28-
"js-cookie": "^2.2.0",
28+
"js-cookie": "^2.2.1",
2929
"jsonlint": "^1.6.3",
3030
"jszip": "^3.2.2",
31-
"lodash": "^4.17.14",
31+
"lodash": "^4.17.15",
3232
"morgan": "^1.9.1",
3333
"normalize.css": "^8.0.1",
3434
"nprogress": "^0.2.0",
3535
"path-to-regexp": "^3.0.0",
3636
"register-service-worker": "^1.6.2",
37-
"screenfull": "^4.2.0",
37+
"screenfull": "^4.2.1",
3838
"script-loader": "^0.7.2",
3939
"sortablejs": "^1.9.0",
40-
"tinymce": "^5.0.12",
41-
"tui-editor": "^1.4.3",
40+
"tinymce": "^5.0.13",
41+
"tui-editor": "^1.4.5",
4242
"vue": "^2.6.10",
4343
"vue-class-component": "^7.1.0",
4444
"vue-count-to": "^1.0.13",
4545
"vue-i18n": "^8.12.0",
4646
"vue-image-crop-upload": "^2.5.0",
4747
"vue-property-decorator": "^8.2.1",
48-
"vue-router": "^3.0.7",
48+
"vue-router": "^3.1.1",
4949
"vue-splitpane": "^1.0.4",
5050
"vue-svgicon": "^3.2.6",
5151
"vue2-dropzone": "^3.6.0",
5252
"vuedraggable": "^2.23.0",
5353
"vuex": "^3.1.1",
5454
"vuex-class": "^0.3.2",
5555
"vuex-module-decorators": "^0.9.9",
56-
"xlsx": "^0.14.3",
56+
"xlsx": "^0.15.0",
5757
"yamljs": "^0.3.0"
5858
},
5959
"devDependencies": {
6060
"@types/clipboard": "^2.0.1",
6161
"@types/codemirror": "^0.0.76",
62-
"@types/compression": "^0.0.36",
62+
"@types/compression": "^1.0.0",
6363
"@types/cors": "^2.8.5",
64-
"@types/echarts": "^4.1.9",
64+
"@types/echarts": "^4.1.10",
6565
"@types/express": "^4.17.0",
6666
"@types/faker": "^4.1.5",
6767
"@types/file-saver": "^2.0.1",
68-
"@types/jest": "^24.0.15",
68+
"@types/jest": "^24.0.17",
6969
"@types/js-cookie": "^2.2.2",
7070
"@types/jszip": "^3.1.6",
7171
"@types/lodash": "^4.14.136",
72-
"@types/morgan": "^1.7.35",
72+
"@types/morgan": "^1.7.36",
7373
"@types/nprogress": "^0.2.0",
7474
"@types/sortablejs": "^1.7.2",
7575
"@types/tinymce": "^4.5.22",
7676
"@types/webpack-env": "^1.14.0",
7777
"@types/yamljs": "^0.2.30",
78-
"@vue/cli-plugin-babel": "^3.9.2",
79-
"@vue/cli-plugin-e2e-cypress": "^3.9.0",
80-
"@vue/cli-plugin-eslint": "^3.9.2",
81-
"@vue/cli-plugin-pwa": "^3.9.0",
82-
"@vue/cli-plugin-typescript": "^3.9.0",
83-
"@vue/cli-plugin-unit-jest": "^3.9.0",
84-
"@vue/cli-service": "^3.9.3",
78+
"@vue/cli-plugin-babel": "^3.10.0",
79+
"@vue/cli-plugin-e2e-cypress": "^3.10.0",
80+
"@vue/cli-plugin-eslint": "^3.10.0",
81+
"@vue/cli-plugin-pwa": "^3.10.0",
82+
"@vue/cli-plugin-typescript": "^3.10.0",
83+
"@vue/cli-plugin-unit-jest": "^3.10.0",
84+
"@vue/cli-service": "^3.10.0",
8585
"@vue/eslint-config-standard": "^4.0.0",
8686
"@vue/eslint-config-typescript": "^4.0.0",
8787
"@vue/test-utils": "^1.0.0-beta.29",
8888
"babel-core": "^7.0.0-bridge.0",
8989
"babel-eslint": "^10.0.2",
9090
"concurrently": "^4.1.1",
91-
"eslint": "^6.0.1",
91+
"eslint": "^6.1.0",
9292
"eslint-plugin-vue": "^5.2.3",
9393
"fibers": "^4.0.1",
9494
"jest": "^24.8.0",
95-
"lint-staged": "^9.2.0",
96-
"sass": "^1.22.6",
95+
"lint-staged": "^9.2.1",
96+
"sass": "^1.22.9",
9797
"sass-loader": "^7.1.0",
9898
"style-resources-loader": "^1.2.1",
99-
"swagger-routes-express": "^3.0.2",
99+
"swagger-routes-express": "^3.0.3",
100100
"ts-jest": "^24.0.2",
101101
"ts-node-dev": "^1.0.0-pre.40",
102102
"typescript": "3.5.3",
103103
"vue-cli-plugin-element": "^1.0.1",
104104
"vue-cli-plugin-style-resources-loader": "^0.1.3",
105105
"vue-template-compiler": "^2.6.10",
106-
"webpack": "^4.36.1"
106+
"webpack": "^4.39.1"
107107
},
108108
"bugs": {
109109
"url": "https://github.com/armour/vue-typescript-admin-template/issues"

src/directives/clipboard/index.ts

+38-31
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,60 @@
11
// Inspired by https://github.com/Inndy/vue-clipboard2
22
import Clipboard from 'clipboard'
33
import { DirectiveOptions } from 'vue'
4-
import { DirectiveBinding } from 'vue/types/options'
54

65
if (!Clipboard) {
76
throw new Error('you should npm install `clipboard` --save at first ')
87
}
98

10-
let successCallback: Function
11-
let errorCallback: Function
12-
let clipboardInstance: Clipboard
13-
14-
const updateClipboard = (el: HTMLElement, binding: DirectiveBinding) => {
15-
if (binding.arg === 'success') {
16-
successCallback = binding.value
17-
} else if (binding.arg === 'error') {
18-
errorCallback = binding.value
19-
} else {
20-
clipboardInstance = new Clipboard(el, {
21-
text() { return binding.value },
22-
action() { return binding.arg === 'cut' ? 'cut' : 'copy' }
23-
})
24-
clipboardInstance.on('success', e => {
25-
const callback = successCallback
26-
callback && callback(e)
27-
})
28-
clipboardInstance.on('error', e => {
29-
const callback = errorCallback
30-
callback && callback(e)
31-
})
32-
}
33-
}
9+
let successCallback: Function | null
10+
let errorCallback: Function | null
11+
let clipboardInstance: Clipboard | null
3412

3513
export const clipboard: DirectiveOptions = {
3614
bind(el, binding) {
37-
updateClipboard(el, binding)
15+
if (binding.arg === 'success') {
16+
successCallback = binding.value
17+
} else if (binding.arg === 'error') {
18+
errorCallback = binding.value
19+
} else {
20+
clipboardInstance = new Clipboard(el, {
21+
text() { return binding.value },
22+
action() { return binding.arg === 'cut' ? 'cut' : 'copy' }
23+
})
24+
clipboardInstance.on('success', e => {
25+
const callback = successCallback
26+
callback && callback(e)
27+
})
28+
clipboardInstance.on('error', e => {
29+
const callback = errorCallback
30+
callback && callback(e)
31+
})
32+
}
3833
},
3934

4035
update(el, binding) {
41-
updateClipboard(el, binding)
36+
if (binding.arg === 'success') {
37+
successCallback = binding.value
38+
} else if (binding.arg === 'error') {
39+
errorCallback = binding.value
40+
} else {
41+
clipboardInstance = new Clipboard(el, {
42+
text() { return binding.value },
43+
action() { return binding.arg === 'cut' ? 'cut' : 'copy' }
44+
})
45+
}
4246
},
4347

44-
unbind(el, binding) {
48+
unbind(_, binding) {
4549
if (binding.arg === 'success') {
46-
// ...
50+
successCallback = null
4751
} else if (binding.arg === 'error') {
48-
// ...
52+
errorCallback = null
4953
} else {
50-
clipboardInstance.destroy()
54+
if (clipboardInstance) {
55+
clipboardInstance.destroy()
56+
}
57+
clipboardInstance = null
5158
}
5259
}
5360
}

0 commit comments

Comments
 (0)