1
1
<template >
2
2
<div class =" code-block-wrapper" >
3
3
<div :class =" className" >
4
+ <button class =" copy" @click =" copyCode" >{{ copyText }}</button >
4
5
<pre
5
6
:class =" className"
6
7
><code :class =" className" v-html =" highlightCode" ></code ></pre >
@@ -20,7 +21,7 @@ import 'prismjs/components/prism-json'
20
21
import ' prismjs/components/prism-python'
21
22
import ' prismjs/components/prism-markdown'
22
23
import ' prismjs/themes/prism-tomorrow.css'
23
- import { computed , toRefs } from ' vue'
24
+ import { computed , ref , toRefs } from ' vue'
24
25
25
26
export default {
26
27
props: {
@@ -35,6 +36,7 @@ export default {
35
36
},
36
37
setup (props ) {
37
38
const { lang , code } = toRefs (props)
39
+ const copyText = ref (' Copy' )
38
40
39
41
// computed properties
40
42
const className = computed (() => {
@@ -52,8 +54,17 @@ export default {
52
54
const getLineNumbers = computed (() => {
53
55
return code .value .split (' \n ' ).length
54
56
})
57
+ const copyCode = async () => {
58
+ try {
59
+ await navigator .clipboard .writeText (code .value )
60
+ } catch (e) {
61
+ console .error (e)
62
+ }
63
+ copyText .value = ' Copied'
64
+ setTimeout (() => (copyText .value = ' Copy' ), 3000 )
65
+ }
55
66
56
- return { className, highlightCode, getLineNumbers }
67
+ return { className, highlightCode, getLineNumbers, copyCode, copyText }
57
68
}
58
69
}
59
70
</script >
@@ -67,7 +78,17 @@ div[class*='language-'] {
67
78
position : relative ;
68
79
}
69
80
70
- div [class *= ' language-' ]::before {
81
+ .copy {
82
+ right : 3em !important ;
83
+ background : transparent ;
84
+ padding-top : 0 ;
85
+ padding-bottom : 0 ;
86
+ line-height : inherit ;
87
+ cursor : pointer ;
88
+ }
89
+
90
+ div [class *= ' language-' ]::before ,
91
+ .copy {
71
92
position : absolute ;
72
93
color : var (--c-white-dark );
73
94
font-size : 0.75rem ;
0 commit comments