Skip to content

Commit 2bed758

Browse files
committed
Add "copy" command to code blocks
1 parent dacfc39 commit 2bed758

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

_sass/buttons.scss

+58
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,61 @@
7979
filter: grayscale(0);
8080
}
8181
}
82+
83+
/* Copy button for code blocks */
84+
pre {
85+
position: relative;
86+
87+
&.copied {
88+
animation: pre-block-copied 0.75s ease-in-out forwards;
89+
border-image-slice: 1;
90+
91+
.copy-btn {
92+
background: $bg-colour;
93+
border-color: transparent;
94+
}
95+
}
96+
97+
&:hover .copy-btn,
98+
&:focus .copy-btn {
99+
opacity: 1;
100+
}
101+
}
102+
103+
.copy-btn {
104+
appearance: none;
105+
-webkit-appearance: none;
106+
cursor: pointer;
107+
position: absolute;
108+
top: 0;
109+
right: 0;
110+
opacity: 0;
111+
padding: 0.35em;
112+
margin: 0;
113+
114+
&:hover,
115+
&:focus {
116+
background: $bg-button;
117+
color: #fff;
118+
}
119+
120+
img {
121+
height: 1em;
122+
width: 1em;
123+
margin: auto;
124+
}
125+
}
126+
127+
@keyframes pre-block-copied {
128+
0% { border-image: linear-gradient(315deg, $bg-colour, $bg-colour, $bg-colour, $bg-colour) 1; }
129+
10% { border-image: linear-gradient(315deg, $secondary, $bg-colour, $bg-colour, $bg-colour) 1; }
130+
20% { border-image: linear-gradient(315deg, $secondary, $secondary, $bg-colour, $bg-colour) 1; }
131+
30% { border-image: linear-gradient(315deg, $secondary, $secondary, $secondary, $bg-colour) 1; }
132+
40% { border-image: linear-gradient(315deg, $secondary, $secondary, $secondary, $secondary) 1; }
133+
50% {
134+
border-color: $secondary;
135+
}
136+
100% {
137+
border-color: $bg-colour;
138+
}
139+
}

assets/img/copy-ok.svg

+1
Loading

assets/img/copy.svg

+1
Loading

assets/polychromatic.js

+33
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ function pageEnter() {
4848
// Downloads page only
4949
if (window.location.pathname == "/download/")
5050
downloadPage();
51+
52+
// Add copy button to code blocks
53+
document.querySelectorAll("pre code").forEach(function(code) {
54+
makeCopyButton(code);
55+
});
5156
}
5257

5358
function downloadPage() {
@@ -141,5 +146,33 @@ function loadDeviceList(backend_id, devices_url, version_url) {
141146
requestVersion.send();
142147
}
143148

149+
function makeCopyButton(code) {
150+
const pre = code.parentNode;
151+
const btn = document.createElement("button");
152+
const img = document.createElement("img");
153+
154+
const iconCopy = "/assets/img/copy.svg";
155+
const iconOK = "/assets/img/copy-ok.svg";
156+
157+
btn.className = "btn copy-btn";
158+
btn.title = "Copy";
159+
img.src = iconCopy;
160+
btn.appendChild(img);
161+
btn.addEventListener("click", function() {
162+
navigator.clipboard.writeText(code.innerText).then(function() {
163+
img.src = iconOK;
164+
pre.classList.add("copied");
165+
setTimeout(function() {
166+
img.src = iconCopy;
167+
pre.classList.remove("copied");
168+
}, 2000);
169+
}, function() {
170+
window.alert("Unable to copy text to clipboard.");
171+
});
172+
});
173+
174+
pre.appendChild(btn);
175+
}
176+
144177
// Initial site load
145178
pageEnter();

0 commit comments

Comments
 (0)