-
Notifications
You must be signed in to change notification settings - Fork 5
/
clientRootMixin.js
executable file
·54 lines (48 loc) · 1.31 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
47
48
49
50
51
52
53
54
/* 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 )
} )
parent.appendChild( copyElement )
parent.classList.add( 'codecopy-enabled' )
},
copyToClipboard: function( str ) {
const el = document.createElement( 'textarea' )
el.value = str
el.setAttribute( 'readonly', '' )
el.style.position = 'absolute'
el.style.left = '-9999px'
document.body.appendChild( el )
const selected =
document.getSelection().rangeCount > 0
? document.getSelection().getRangeAt( 0 )
: false
el.select()
document.execCommand( 'copy' )
document.body.removeChild( el )
if ( selected ) {
document.getSelection().removeAllRanges()
document.getSelection().addRange( selected )
}
}
}
}