Skip to content

Commit

Permalink
Add "copy" command to code blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
lah7 committed Oct 29, 2024
1 parent dacfc39 commit 2bed758
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
58 changes: 58 additions & 0 deletions _sass/buttons.scss
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,61 @@
filter: grayscale(0);
}
}

/* Copy button for code blocks */
pre {
position: relative;

&.copied {
animation: pre-block-copied 0.75s ease-in-out forwards;
border-image-slice: 1;

.copy-btn {
background: $bg-colour;
border-color: transparent;
}
}

&:hover .copy-btn,
&:focus .copy-btn {
opacity: 1;
}
}

.copy-btn {
appearance: none;
-webkit-appearance: none;
cursor: pointer;
position: absolute;
top: 0;
right: 0;
opacity: 0;
padding: 0.35em;
margin: 0;

&:hover,
&:focus {
background: $bg-button;
color: #fff;
}

img {
height: 1em;
width: 1em;
margin: auto;
}
}

@keyframes pre-block-copied {
0% { border-image: linear-gradient(315deg, $bg-colour, $bg-colour, $bg-colour, $bg-colour) 1; }
10% { border-image: linear-gradient(315deg, $secondary, $bg-colour, $bg-colour, $bg-colour) 1; }
20% { border-image: linear-gradient(315deg, $secondary, $secondary, $bg-colour, $bg-colour) 1; }
30% { border-image: linear-gradient(315deg, $secondary, $secondary, $secondary, $bg-colour) 1; }
40% { border-image: linear-gradient(315deg, $secondary, $secondary, $secondary, $secondary) 1; }
50% {
border-color: $secondary;
}
100% {
border-color: $bg-colour;
}
}
1 change: 1 addition & 0 deletions assets/img/copy-ok.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions assets/img/copy.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions assets/polychromatic.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ function pageEnter() {
// Downloads page only
if (window.location.pathname == "/download/")
downloadPage();

// Add copy button to code blocks
document.querySelectorAll("pre code").forEach(function(code) {
makeCopyButton(code);
});
}

function downloadPage() {
Expand Down Expand Up @@ -141,5 +146,33 @@ function loadDeviceList(backend_id, devices_url, version_url) {
requestVersion.send();
}

function makeCopyButton(code) {
const pre = code.parentNode;
const btn = document.createElement("button");
const img = document.createElement("img");

const iconCopy = "/assets/img/copy.svg";
const iconOK = "/assets/img/copy-ok.svg";

btn.className = "btn copy-btn";
btn.title = "Copy";
img.src = iconCopy;
btn.appendChild(img);
btn.addEventListener("click", function() {
navigator.clipboard.writeText(code.innerText).then(function() {
img.src = iconOK;
pre.classList.add("copied");
setTimeout(function() {
img.src = iconCopy;
pre.classList.remove("copied");
}, 2000);
}, function() {
window.alert("Unable to copy text to clipboard.");
});
});

pre.appendChild(btn);
}

// Initial site load
pageEnter();

0 comments on commit 2bed758

Please sign in to comment.