Skip to content

Commit 9c0bd76

Browse files
authored
feat: add copy button to code blocks (#20)
* feat: add copy button to code blocks * fix: css update
1 parent d8fbd2d commit 9c0bd76

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

assets/css/f5-hugo.css

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,37 @@
77
padding-left: 8px;
88
}
99

10+
.code-copy {
11+
color: #272822;
12+
background-color: #fff;
13+
border-color: #272822;
14+
border: 1px solid;
15+
border-radius: 4px 4px 0px 0px;
16+
display: block;
17+
margin-left: auto;
18+
margin-right: 0;
19+
margin-bottom: -12px;
20+
padding: 3px 8px;
21+
font-size: 0.8em;
22+
}
23+
24+
.code-copy:hover {
25+
cursor: pointer;
26+
background-color: #009639;
27+
color: #ffffff;
28+
border-color: #272822!important;
29+
border: 1px solid;
30+
}
31+
32+
.code-copy:focus {
33+
outline: 0px solid transparent;
34+
background-color: #e6e6e6;
35+
}
36+
37+
.code-copy:active {
38+
background-color: #d9d9d9;
39+
}
40+
1041
.button {
1142
text-transform: uppercase;
1243
font-size: 14px;
@@ -742,6 +773,10 @@ pre {
742773
border-radius: 4px;
743774
}
744775

776+
pre.chroma {
777+
border-radius: 4px 0px 4px 4px;
778+
}
779+
745780
code {
746781
font-family: Courier;
747782
color: #343434;
@@ -1152,4 +1187,7 @@ ol.breadcrumb > li:first-child {
11521187

11531188
.fa, .far, .fas {
11541189
padding-right: 4px;
1155-
}
1190+
}
1191+
1192+
1193+

assets/js/code-copy.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function CopyCode(clipboard) {
2+
document.querySelectorAll('pre > code').forEach(function(codeBlock) {
3+
var button = document.createElement('button');
4+
button.className = 'code-copy';
5+
button.type = 'button';
6+
button.innerHTML = '<i class="fas fa-copy"></i> Copy';
7+
8+
button.addEventListener('click', function() {
9+
clipboard.writeText(codeBlock.textContent).then(
10+
function() {
11+
button.blur(); /* Chrome fix */
12+
button.innerHTML = '<i class="fas fa-check"></i> Copied!';
13+
setTimeout(function() {
14+
button.innerHTML = '<i class="fas fa-copy"></i> Copy';
15+
}, 2000);
16+
},
17+
function(error) {
18+
button.innerHTML = '<i class="fas fa-exclamation"></i> Error';
19+
console.error(error);
20+
}
21+
);
22+
});
23+
24+
var pre = codeBlock.parentNode;
25+
if (pre.parentNode.classList.contains('highlight')) {
26+
var highlight = pre.parentNode;
27+
highlight.parentNode.insertBefore(button, highlight);
28+
} else {
29+
pre.parentNode.insertBefore(button, pre);
30+
}
31+
});
32+
}
33+
34+
CopyCode(navigator.clipboard);

layouts/partials/scripts.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
{{ $jsBootstrap := resources.Get "js/bootstrap.bundle.min.js" | fingerprint "sha512" }}
1111
<script src="{{ $jsBootstrap.RelPermalink }}" type="text/javascript" integrity="{{ $jsBootstrap.Data.Integrity }}"></script>
1212

13+
<!-- load code copy js -->
14+
{{ $codecopy := resources.Get "/js/code-copy.js" | fingerprint "sha512" }}
15+
<script src="{{ $codecopy.RelPermalink }}" type="text/javascript" ></script>
1316

1417

1518
<!-- START COVEO -->

0 commit comments

Comments
 (0)