-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeViewer.vue
102 lines (99 loc) · 2.36 KB
/
CodeViewer.vue
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
<template>
<div>
<pre class="sample-code"><code class="language-html">{{code}}</code></pre>
<button class="copyButton" @click="copy" :data-clipboard-text="code">
<div v-if="isCopied === true">
<p>🎉 Copied!</p>
</div>
<div v-else>
<p>✂️ Copy Source Code</p>
</div>
</button>
</div>
</template>
<script>
let code = ``
const data = {
isCopied: false
}
export default {
props: ['code'],
data: context => {
code = context.code
return data
},
methods: {
copy: () => {
data.isCopied = true
try {
new window.ClipboardJS('.copyButton')
} catch (e) {}
setTimeout(() => {
data.isCopied = false
}, 600)
}
},
mounted: () => {
const importScript = src => {
return new Promise((resolve, reject) => {
let s = document.createElement('script')
s.addEventListener('load', resolve)
s.type = 'text/javascript'
s.charset = 'utf-8'
s.src = src
document.body.appendChild(s)
})
}
const importStyle = href => {
return new Promise((resolve, reject) => {
let s = document.createElement('link')
s.addEventListener('load', resolve)
s.rel = 'stylesheet'
s.type = 'text/css'
s.href = href
document.body.appendChild(s)
})
}
const importManager = async () => {
await importScript(
`https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.2.0/highlight.min.js`
)
await importScript(
`https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js`
)
await importStyle(
`https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.2.0/styles/tomorrow.min.css`
)
if (window && window.hljs) {
document.querySelectorAll('pre code').forEach(block => {
window.hljs.highlightBlock(block)
})
}
}
importManager()
}
}
</script>
<style scoped>
.sample-code {
white-space: normal;
}
.copyButton {
width: 100%;
height: 45px;
box-shadow: rgba(0, 0, 0, 0.1) 0 1px 3px 0;
border: 1px solid rgba(0, 0, 0, 0.1);
border-radius: 4px;
padding: 10px;
text-align: center;
margin-top: 10px;
user-select: none;
outline: none;
font-weight: 600;
font-size: 15px;
color: #333333;
}
.copyButton:hover {
background: aliceblue;
}
</style>