Skip to content

Commit 978e9b4

Browse files
authored
Merge pull request #20 from KratosMultiphysics/add-copy-code-button
Add a copy to clipboard button to code blocks
2 parents b23d0d6 + c9929ab commit 978e9b4

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

Page_files/_includes/head.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<script src="https://cdnjs.cloudflare.com/ajax/libs/mermaid/11.3.0/mermaid.min.js"></script>
3838

3939
<script src="{{ "/js/toc.js" | prepend: site.baseurl | preprend: site.url }}"></script>
40+
<script src="{{ "/js/copyCode.js" | prepend: site.baseurl | preprend: site.url }}"></script>
4041
<script src="{{ "/js/customscripts.js" | prepend: site.baseurl | preprend: site.url }}"></script>
4142

4243
<link rel="shortcut icon" href="{{ "/images/favicon.ico" | prepend: site.baseurl | preprend: site.url }}">

Page_files/css/syntax.scss

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ div.highlighter-rouge::before {
4848
position: absolute;
4949
display: block;
5050
height: 1.2rem;
51-
right: 1rem;
51+
right: 7rem;
5252
top: 0.5rem;
5353
z-index: 5;
5454
}
@@ -101,6 +101,21 @@ div.highlight table {
101101
font-size: 1.3rem;
102102
}
103103

104+
div.highlight {
105+
position: relative;
106+
}
107+
108+
div.highlight button {
109+
position: absolute;
110+
top: 2px;
111+
right: 5px;
112+
z-index: 10;
113+
background-color: #2e3e4e;
114+
border: none;
115+
cursor: pointer;
116+
font-size: 1.2rem;
117+
}
118+
104119
.highlight .hll { background-color: #373b41 }
105120
.highlight .c { color: #969896 } /* Comment */
106121
.highlight .err { color: #cc6666 } /* Error */

Page_files/js/copyCode.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
layout: null
3+
---
4+
(function (jtd, undefined) {
5+
6+
// From https://github.com/just-the-docs/just-the-docs/blob/main/assets/js/just-the-docs.js
7+
// Event handling
8+
9+
jtd.addEvent = function(el, type, handler) {
10+
if (el.attachEvent) el.attachEvent('on'+type, handler); else el.addEventListener(type, handler);
11+
}
12+
jtd.removeEvent = function(el, type, handler) {
13+
if (el.detachEvent) el.detachEvent('on'+type, handler); else el.removeEventListener(type, handler);
14+
}
15+
jtd.onReady = function(ready) {
16+
// in case the document is already rendered
17+
if (document.readyState!='loading') ready();
18+
// modern browsers
19+
else if (document.addEventListener) document.addEventListener('DOMContentLoaded', ready);
20+
// IE <= 8
21+
else document.attachEvent('onreadystatechange', function(){
22+
if (document.readyState=='complete') ready();
23+
});
24+
}
25+
26+
jtd.onReady(function(){
27+
28+
if (!window.isSecureContext) {
29+
console.log('Window does not have a secure context, therefore code clipboard copy functionality will not be available. For more details see https://web.dev/async-clipboard/#security-and-permissions');
30+
return;
31+
}
32+
33+
var codeBlocks = document.querySelectorAll('div.highlight');
34+
35+
var svgCopied = '<i class="fa fa-check" aria-hidden="true"></i>&nbsp;Copied';
36+
var svgCopy = '<i class="fa fa-clipboard" aria-hidden="true"></i>&nbsp;Copy';
37+
38+
codeBlocks.forEach(codeBlock => {
39+
var copyButton = document.createElement('button');
40+
var timeout = null;
41+
copyButton.type = 'button';
42+
copyButton.ariaLabel = 'Copy code to clipboard';
43+
copyButton.innerHTML = svgCopy;
44+
codeBlock.append(copyButton);
45+
46+
copyButton.addEventListener('click', function () {
47+
if(timeout === null) {
48+
var code = (codeBlock.querySelector('pre:not(.lineno, .highlight)') || codeBlock.querySelector('code')).innerText;
49+
window.navigator.clipboard.writeText(code);
50+
51+
copyButton.innerHTML = svgCopied;
52+
53+
var timeoutSetting = 4000;
54+
55+
timeout = setTimeout(function () {
56+
copyButton.innerHTML = svgCopy;
57+
timeout = null;
58+
}, timeoutSetting);
59+
}
60+
});
61+
});
62+
63+
});
64+
65+
})(window.jtd = window.jtd || {});

0 commit comments

Comments
 (0)