Skip to content

Commit c9929ab

Browse files
committed
add copy code button
1 parent 92f93b8 commit c9929ab

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
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: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pre.highlight {
2323
position: relative;
2424
}
2525

26+
li code.highlighter-rouge,
2627
p code.highlighter-rouge,
2728
p kbd {
2829
color: #cc6666;
@@ -47,7 +48,7 @@ div.highlighter-rouge::before {
4748
position: absolute;
4849
display: block;
4950
height: 1.2rem;
50-
right: 1rem;
51+
right: 7rem;
5152
top: 0.5rem;
5253
z-index: 5;
5354
}
@@ -100,6 +101,21 @@ div.highlight table {
100101
font-size: 1.3rem;
101102
}
102103

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+
103119
.highlight .hll { background-color: #373b41 }
104120
.highlight .c { color: #969896 } /* Comment */
105121
.highlight .err { color: #cc6666 } /* Error */
@@ -163,4 +179,4 @@ div.highlight table {
163179
.highlight .vc { color: #cc6666 } /* Name.Variable.Class */
164180
.highlight .vg { color: #cc6666 } /* Name.Variable.Global */
165181
.highlight .vi { color: #cc6666 } /* Name.Variable.Instance */
166-
.highlight .il { color: #de935f } /* Literal.Number.Integer.Long */
182+
.highlight .il { color: #de935f } /* Literal.Number.Integer.Long */

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)