Skip to content

Commit f4571ae

Browse files
committed
add a Copy code to clipboard button
only for java for now. maybe more later. Signed-off-by: Christoph Rueger <[email protected]>
1 parent 5fd1964 commit f4571ae

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

_includes/footer.htm

+38
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,42 @@
2727
$( document ).ready(function() {
2828
$(document).foundation();
2929
});
30+
31+
// copy to clipboard buttons
32+
33+
const copyButtonLabel = "Copy Code";
34+
35+
// use a class selector if available
36+
// TODO only java for now. maybe more later
37+
let blocks = document.querySelectorAll(".language-java");
38+
39+
blocks.forEach((block) => {
40+
// only add button if browser supports Clipboard API
41+
if (navigator.clipboard) {
42+
let button = document.createElement("button");
43+
button.classList.add("copycodebtn");
44+
45+
button.innerText = copyButtonLabel;
46+
block.appendChild(button);
47+
48+
button.addEventListener("click", async () => {
49+
await copyCode(block, button);
50+
});
51+
52+
}
53+
});
54+
55+
async function copyCode(block, button) {
56+
let code = block.querySelector("code");
57+
let text = code.innerText;
58+
59+
await navigator.clipboard.writeText(text);
60+
61+
button.innerText = "Code Copied";
62+
63+
setTimeout(()=> {
64+
button.innerText = copyButtonLabel;
65+
},2000)
66+
}
67+
3068
</script>

css/style.scss

+17-1
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,20 @@ signature {
9191

9292
code {
9393
all: revert !important;
94-
}
94+
}
95+
96+
div[class*="language-"] {
97+
position: relative;
98+
margin: 5px 0 ;
99+
padding: 1.75rem 0 1.75rem 1rem;
100+
}
101+
102+
div[class*="language-"] button{
103+
position: absolute;
104+
top: 35px;
105+
right: 5px;
106+
border-radius: 8px;
107+
background-color: grey;
108+
padding: 5px;
109+
}
110+

0 commit comments

Comments
 (0)