File tree 4 files changed +76
-0
lines changed
4 files changed +76
-0
lines changed Original file line number Diff line number Diff line change @@ -156,6 +156,10 @@ ul code {
156
156
margin : 16px 0 ;
157
157
}
158
158
159
+ code {
160
+ position : relative;
161
+ }
162
+
159
163
code .inline {
160
164
display : inline-block;
161
165
vertical-align : middle;
@@ -165,6 +169,32 @@ code.inline {
165
169
margin : 0 ;
166
170
}
167
171
172
+ .copy-btn {
173
+ cursor : pointer;
174
+ position : absolute;
175
+ top : 16px ;
176
+ right : 16px ;
177
+ width : 24px ;
178
+ height : 24px ;
179
+ background-color : transparent;
180
+ background-image : url ('../files/ic_copy_grey_24dp.svg' );
181
+ background-size : contain;
182
+ background-position : center;
183
+ background-repeat : no-repeat;
184
+ opacity : 0.9 ;
185
+ border : none;
186
+ }
187
+
188
+ .copy-btn : hover {
189
+ opacity : 1 ;
190
+ }
191
+
192
+ .copy-btn .copied {
193
+ pointer-events : none;
194
+ opacity : 1 ;
195
+ background-image : url ('../files/ic_tick_green_24dp.svg' );
196
+ }
197
+
168
198
table {
169
199
width : 100% ;
170
200
border-collapse : collapse;
@@ -315,4 +345,10 @@ a.param:hover {
315
345
line-height : 28px ;
316
346
}
317
347
348
+ .copy-btn {
349
+ top : 12px ;
350
+ right : 8px ;
351
+ width : 20px ;
352
+ height : 20px ;
353
+ }
318
354
}
Original file line number Diff line number Diff line change @@ -125,6 +125,34 @@ function onDocumentLoad() {
125
125
126
126
}
127
127
128
+ // create copy button for copying code snippets
129
+
130
+ function addCopyButton ( element ) {
131
+
132
+ const copyButton = document . createElement ( 'button' ) ;
133
+ copyButton . className = 'copy-btn' ;
134
+
135
+ element . appendChild ( copyButton ) ;
136
+
137
+ copyButton . addEventListener ( 'click' , function ( ) {
138
+
139
+ const codeContent = element . textContent ;
140
+ navigator . clipboard . writeText ( codeContent ) . then ( ( ) => {
141
+
142
+ copyButton . classList . add ( 'copied' ) ;
143
+
144
+ setTimeout ( ( ) => {
145
+
146
+ copyButton . classList . remove ( 'copied' ) ;
147
+
148
+ } , 1000 ) ;
149
+
150
+ } ) ;
151
+
152
+ } ) ;
153
+
154
+ }
155
+
128
156
const elements = document . getElementsByTagName ( 'code' ) ;
129
157
130
158
for ( let i = 0 ; i < elements . length ; i ++ ) {
@@ -133,6 +161,12 @@ function onDocumentLoad() {
133
161
134
162
element . textContent = dedent ( element . textContent ) ;
135
163
164
+ if ( ! element . classList . contains ( 'inline' ) ) {
165
+
166
+ addCopyButton ( element ) ;
167
+
168
+ }
169
+
136
170
}
137
171
138
172
// Edit button
You can’t perform that action at this time.
0 commit comments