Skip to content

Commit e1f8a47

Browse files
committed
Add copy to clipboard for code snippets
1 parent 4b1ff1c commit e1f8a47

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

assets/css/index.css

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,4 +1073,25 @@ a[href*="#no-click"], img[src*="#no-click"] {
10731073

10741074
.dd-item .highlight:hover {
10751075
color: #5961ff;
1076+
}
1077+
1078+
.copy-button {
1079+
background-color: #eee;
1080+
border: none;
1081+
padding: 4px;
1082+
cursor: pointer;
1083+
border-radius: 4px;
1084+
display: flex;
1085+
align-items: center;
1086+
justify-content: center;
1087+
}
1088+
1089+
.copy-button svg {
1090+
width: 16px;
1091+
height: 16px;
1092+
fill: #333;
1093+
}
1094+
1095+
.copy-button:hover {
1096+
background-color: #ddd;
10761097
}

layouts/partials/scripts.html

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,53 @@
1-
<!-- Code for version selectors -->
1+
<!-- Code for copy to clipboard button -->
2+
<script>
3+
document.addEventListener("DOMContentLoaded", function () {
4+
document.querySelectorAll('div.highlight').forEach(block => {
5+
// Create a wrapper for the button and message
6+
const wrapper = document.createElement('div');
7+
wrapper.style = 'position:absolute;top:10px;right:10px;z-index:1;display:flex;align-items:center;gap:6px;';
8+
9+
// Create the copy button
10+
const button = document.createElement('button');
11+
button.className = 'copy-button';
12+
button.innerHTML = `
13+
<svg viewBox="0 0 24 24" width="18" height="18" fill="none" xmlns="http://www.w3.org/2000/svg">
14+
<path fill-rule="evenodd" clip-rule="evenodd" d="M21 8C21 6.34315 19.6569 5 18 5H10C8.34315 5 7 6.34315 7 8V20C7 21.6569 8.34315 23 10 23H18C19.6569 23 21 21.6569 21 20V8ZM19 8C19 7.44772 18.5523 7 18 7H10C9.44772 7 9 7.44772 9 8V20C9 20.5523 9.44772 21 10 21H18C18.5523 21 19 20.5523 19 20V8Z" fill="#0F0F0F"/>
15+
<path d="M6 3H16C16.5523 3 17 2.55228 17 2C17 1.44772 16.5523 1 16 1H6C4.34315 1 3 2.34315 3 4V18C3 18.5523 3.44772 19 4 19C4.55228 19 5 18.5523 5 18V4C5 3.44772 5.44772 3 6 3Z" fill="#0F0F0F"/>
16+
</svg>
17+
`;
18+
19+
// Create the "Copied!" message span
20+
const message = document.createElement('span');
21+
message.className = 'copied-message';
22+
message.innerText = 'Copied to clipboard!';
23+
message.style = 'opacity:0;transition:opacity 0.3s ease;font-size:0.75em;color:white;';
24+
25+
// Handle copy logic
26+
button.addEventListener('click', () => {
27+
const lines = block.querySelectorAll('span.cl');
28+
const text = Array.from(lines).map(line => line.textContent).join('');
229

30+
navigator.clipboard.writeText(text).then(() => {
31+
message.style.opacity = '1';
32+
setTimeout(() => {
33+
message.style.opacity = '0';
34+
}, 2000);
35+
}).catch(err => {
36+
console.error("Copy failed", err);
37+
});
38+
});
39+
40+
// Append button and message
41+
wrapper.appendChild(message);
42+
wrapper.appendChild(button);
43+
block.style.position = 'relative';
44+
block.appendChild(wrapper);
45+
});
46+
});
47+
</script>
48+
49+
50+
<!-- Code for version selectors -->
351
<script>
452
window.onload = function(){
553
var currentUrl = window.location.href

0 commit comments

Comments
 (0)