forked from dovy/vuepress-plugin-clipboard-copy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclientRootMixin.js
executable file
·46 lines (40 loc) · 1.04 KB
/
clientRootMixin.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
/* global SELECTOR */
import './style.css'
export default {
data: () => ({zoom: null}),
mounted() {
this.updateCopy()
},
updated() {
this.updateCopy()
},
methods: {
updateCopy() {
setTimeout( () => {
document.querySelectorAll( COPY_SELECTOR ).forEach( this.generateCopyButton )
}, 1000 )
},
generateCopyButton: function( parent ) {
if ( parent.classList.contains( 'codecopy-enabled' ) ) return
const copyElement = document.createElement( 'span' )
copyElement.className = 'code-copy'
copyElement.title = 'Click to Copy to Clipboard'
copyElement.addEventListener( 'click', () => {
this.copyToClipboard( parent.innerText, copyElement )
} )
parent.appendChild( copyElement )
parent.classList.add( 'codecopy-enabled' )
},
copyToClipboard: function( str, copyElement ) {
navigator.clipboard.writeText(str)
.then(() => {
copyElement.classList.add( '_suc' );
console.log('success');
})
.catch(() => {
copyElement.classList.add( '_err' );
console.log('error');
});
}
}
}