Skip to content

Commit 34df00f

Browse files
committed
Add copy code block
1 parent fe06dd1 commit 34df00f

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

website/siteConfig.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,12 @@ const siteConfig = {
7575
},
7676

7777
// Add custom scripts here that would be placed in <script> tags
78-
scripts: ["https://buttons.github.io/buttons.js"],
78+
scripts: [
79+
'https://buttons.github.io/buttons.js',
80+
'https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.0/clipboard.min.js',
81+
'/js/code-block-buttons.js',
82+
],
83+
stylesheets: ['/css/code-block-buttons.css'],
7984

8085
/* On page navigation for the current documentation page */
8186
onPageNav: "separate",
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* "Copy" code block button */
2+
pre {
3+
position: relative;
4+
}
5+
6+
pre .btnIcon {
7+
position: absolute;
8+
top: 4px;
9+
z-index: 2;
10+
cursor: pointer;
11+
border: 1px solid transparent;
12+
padding: 0;
13+
color: #000;
14+
background-color: transparent;
15+
height: 30px;
16+
transition: all .25s ease-out;
17+
}
18+
19+
pre .btnIcon:hover {
20+
text-decoration: none;
21+
}
22+
23+
.btnIcon__body {
24+
align-items: center;
25+
display: flex;
26+
}
27+
28+
.btnIcon svg {
29+
fill: currentColor;
30+
margin-right: .4em;
31+
}
32+
33+
.btnIcon__label {
34+
font-size: 11px;
35+
}
36+
37+
.btnClipboard {
38+
right: 10px;
39+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Turn off ESLint for this file because it's sent down to users as-is.
2+
/* eslint-disable */
3+
window.addEventListener('load', function() {
4+
function button(label, ariaLabel, icon, className) {
5+
const btn = document.createElement('button');
6+
btn.classList.add('btnIcon', className);
7+
btn.setAttribute('type', 'button');
8+
btn.setAttribute('aria-label', ariaLabel);
9+
btn.innerHTML =
10+
'<div class="btnIcon__body">' +
11+
icon +
12+
'<strong class="btnIcon__label">' +
13+
label +
14+
'</strong>' +
15+
'</div>';
16+
return btn;
17+
}
18+
19+
function addButtons(codeBlockSelector, btn) {
20+
document.querySelectorAll(codeBlockSelector).forEach(function(code) {
21+
code.parentNode.appendChild(btn.cloneNode(true));
22+
});
23+
}
24+
25+
const copyIcon =
26+
'<svg width="12" height="12" viewBox="340 364 14 15" xmlns="http://www.w3.org/2000/svg"><path fill="currentColor" d="M342 375.974h4v.998h-4v-.998zm5-5.987h-5v.998h5v-.998zm2 2.994v-1.995l-3 2.993 3 2.994v-1.996h5v-1.995h-5zm-4.5-.997H342v.998h2.5v-.997zm-2.5 2.993h2.5v-.998H342v.998zm9 .998h1v1.996c-.016.28-.11.514-.297.702-.187.187-.422.28-.703.296h-10c-.547 0-1-.452-1-.998v-10.976c0-.546.453-.998 1-.998h3c0-1.107.89-1.996 2-1.996 1.11 0 2 .89 2 1.996h3c.547 0 1 .452 1 .998v4.99h-1v-2.995h-10v8.98h10v-1.996zm-9-7.983h8c0-.544-.453-.996-1-.996h-1c-.547 0-1-.453-1-.998 0-.546-.453-.998-1-.998-.547 0-1 .452-1 .998 0 .545-.453.998-1 .998h-1c-.547 0-1 .452-1 .997z" fill-rule="evenodd"/></svg>';
27+
28+
addButtons(
29+
'.hljs',
30+
button('Copy', 'Copy code to clipboard', copyIcon, 'btnClipboard'),
31+
);
32+
33+
const clipboard = new ClipboardJS('.btnClipboard', {
34+
target: function(trigger) {
35+
return trigger.parentNode.querySelector('code');
36+
},
37+
});
38+
39+
clipboard.on('success', function(event) {
40+
event.clearSelection();
41+
const textEl = event.trigger.querySelector('.btnIcon__label');
42+
textEl.textContent = 'Copied';
43+
setTimeout(function() {
44+
textEl.textContent = 'Copy';
45+
}, 2000);
46+
});
47+
});

0 commit comments

Comments
 (0)