-
Notifications
You must be signed in to change notification settings - Fork 93
/
vue-clipboard.js
141 lines (116 loc) · 3.79 KB
/
vue-clipboard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copied from https://github.com/xiaokaike/vue-clipboard/blob/master/vue-clipboard.js
;(function() {
var vueClipboard = {}
vueClipboard.install = function(Vue) {
Vue.directive('clipboard', {
params: ['success', 'error'],
acceptStatement: true,
bind: function() {
//bind callback
this.arg = this.arg === 'cut' ? 'cut' : 'copy'
Vue.util.on(this.el, 'click', this.handler.bind(this))
},
update: function(data) {
this.text = data
},
unbind: function() {
Vue.util.off(this.el, 'click', this.handler)
this.removeFake()
},
handler: function() {
this.selectFake(this.text)
if (this.arg === 'cut') {
this.vm[this.expression] = ''
}
},
/**
* Creates a fake textarea element, sets its value from `text` property,
* and makes a selection on it.
*/
selectFake: function(text) {
var isRTL = document.documentElement.getAttribute('dir') == 'rtl'
this.removeFake()
this.fakeHandler = document.body.addEventListener('click', this.removeFake.bind(this))
this.fakeElem = document.createElement('textarea')
// Prevent zooming on iOS
this.fakeElem.style.fontSize = '12pt'
// Reset box model
this.fakeElem.style.border = '0'
this.fakeElem.style.padding = '0'
this.fakeElem.style.margin = '0'
// Move element out of screen horizontally
this.fakeElem.style.position = 'fixed'
this.fakeElem.style[isRTL ? 'right' : 'left'] = '-9999px'
// Move element to the same position vertically
this.fakeElem.style.top = (window.pageYOffset || document.documentElement.scrollTop) + 'px'
this.fakeElem.setAttribute('readonly', '')
this.fakeElem.value = text
document.body.appendChild(this.fakeElem)
this.selectedText = select(this.fakeElem)
this.copyText()
},
/**
* Only removes the fake element after another click event, that way
* a user can hit `Ctrl+C` to copy because selection still exists.
*/
removeFake: function() {
if (this.fakeHandler) {
document.body.removeEventListener('click')
this.fakeHandler = null
}
if (this.fakeElem) {
document.body.removeChild(this.fakeElem)
this.fakeElem = null
}
},
/**
* Executes the copy operation based on the current selection.
*/
copyText: function() {
var succeeded
try {
succeeded = document.execCommand('copy')
} catch (err) {
succeeded = false
}
this.handleResult(succeeded)
},
handleResult: function(succeeded) {
if (succeeded) {
this.params.success && this.params.success()
} else {
this.params.error && this.params.error()
}
}
})
}
function select(element) {
var selectedText
if (element.nodeName === 'INPUT' || element.nodeName === 'TEXTAREA') {
element.focus()
element.setSelectionRange(0, element.value.length)
selectedText = element.value
} else {
if (element.hasAttribute('contenteditable')) {
element.focus()
}
var selection = window.getSelection()
var range = document.createRange()
range.selectNodeContents(element)
selection.removeAllRanges()
selection.addRange(range)
selectedText = selection.toString()
}
return selectedText
}
if (typeof exports == "object") {
module.exports = vueClipboard
} else if (typeof define == "function" && define.amd) {
define([], function() {
return vueClipboard
})
} else if (window.Vue) {
window.vueClipboard = vueClipboard
Vue.use(vueClipboard)
}
})()