File tree 2 files changed +55
-1
lines changed
2 files changed +55
-1
lines changed Original file line number Diff line number Diff line change 27
27
$ ( document ) . ready ( function ( ) {
28
28
$ ( document ) . foundation ( ) ;
29
29
} ) ;
30
+
31
+ // copy to clipboard buttons
32
+
33
+ const copyButtonLabel = "Copy Code" ;
34
+
35
+ // use a class selector if available
36
+ // TODO only java for now. maybe more later
37
+ let blocks = document . querySelectorAll ( ".language-java" ) ;
38
+
39
+ blocks . forEach ( ( block ) => {
40
+ // only add button if browser supports Clipboard API
41
+ if ( navigator . clipboard ) {
42
+ let button = document . createElement ( "button" ) ;
43
+ button . classList . add ( "copycodebtn" ) ;
44
+
45
+ button . innerText = copyButtonLabel ;
46
+ block . appendChild ( button ) ;
47
+
48
+ button . addEventListener ( "click" , async ( ) => {
49
+ await copyCode ( block , button ) ;
50
+ } ) ;
51
+
52
+ }
53
+ } ) ;
54
+
55
+ async function copyCode ( block , button ) {
56
+ let code = block . querySelector ( "code" ) ;
57
+ let text = code . innerText ;
58
+
59
+ await navigator . clipboard . writeText ( text ) ;
60
+
61
+ button . innerText = "Code Copied" ;
62
+
63
+ setTimeout ( ( ) => {
64
+ button . innerText = copyButtonLabel ;
65
+ } , 2000 )
66
+ }
67
+
30
68
</ script >
Original file line number Diff line number Diff line change @@ -91,4 +91,20 @@ signature {
91
91
92
92
code {
93
93
all : revert !important ;
94
- }
94
+ }
95
+
96
+ div [class *= " language-" ] {
97
+ position : relative ;
98
+ margin : 5px 0 ;
99
+ padding : 1.75rem 0 1.75rem 1rem ;
100
+ }
101
+
102
+ div [class *= " language-" ] button {
103
+ position : absolute ;
104
+ top : 35px ;
105
+ right : 5px ;
106
+ border-radius : 8px ;
107
+ background-color : grey ;
108
+ padding : 5px ;
109
+ }
110
+
You can’t perform that action at this time.
0 commit comments