Skip to content

Commit

Permalink
Add copy button to codeblocks on install pages
Browse files Browse the repository at this point in the history
  • Loading branch information
hendriklammers authored and postmodern committed May 21, 2024
1 parent f875f6e commit 8a19b02
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions install/arch/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
layout: page
title: Install - Arch
js: ["copy-button.js"]
---

# Installing Ronin on Arch
Expand Down
1 change: 1 addition & 0 deletions install/fedora/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
layout: page
title: Install - Fedora
js: ["copy-button.js"]
---

# Installing Ronin on Fedora
Expand Down
1 change: 1 addition & 0 deletions install/freebsd/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
layout: page
title: Install - FreeBSD
js: ["copy-button.js"]
---

# Installing Ronin on FreeBSD
Expand Down
1 change: 1 addition & 0 deletions install/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
layout: page
title: Install
js: ["copy-button.js"]
---

# Install
Expand Down
1 change: 1 addition & 0 deletions install/macos/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
layout: page
title: Install - macOS
js: ["copy-button.js"]
---

# Installing Ronin on macOS
Expand Down
1 change: 1 addition & 0 deletions install/nix/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
layout: page
title: Install - Nix
js: ["copy-button.js"]
---

# Installing Ronin on Nix
Expand Down
1 change: 1 addition & 0 deletions install/opensuse/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
layout: page
title: Install - OpenSUSE
js: ["copy-button.js"]
---

# Installing Ronin on OpenSUSE
Expand Down
1 change: 1 addition & 0 deletions install/termux/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
layout: page
title: Install - Termux
js: ["copy-button.js"]
---

# Installing Ronin on Termux
Expand Down
1 change: 1 addition & 0 deletions install/ubuntu/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
layout: page
title: Install - Ubuntu
js: ["copy-button.js"]
---

# Installing Ronin on Ubuntu
Expand Down
53 changes: 53 additions & 0 deletions javascript/copy-button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"use strict";

function initCopyButtons() {
// Make sure browser supports clipboard functionality
if (!window.navigator?.clipboard) {
return;
}

const codeBlocks = document.querySelectorAll(".highlighter-rouge > .highlight");

codeBlocks.forEach((elem) => {
const code = elem.querySelector(".highlight > code");
if (!code) {
return;
}

let timeout;
const button = document.createElement("button");
button.type = "button";
button.textContent = "Copy to clipboard";
button.className = "copy-button";
button.addEventListener("click", async () => {
try {
await window.navigator.clipboard.writeText(code.textContent.trim());
button.classList.add("copied");

if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(() => {
button.classList.remove("copied");
}, 1500);
} catch (error) {
console.error("Failed to copy to clipboard:", error);
}
});

elem.insertBefore(button, elem.querySelector(":scope > .highlight"));
});
}

(() => {
function ready(callback) {
if (document.readyState != "loading") {
callback();
} else {
document.addEventListener("DOMContentLoaded", callback);
}
}

ready(initCopyButtons);
})();

35 changes: 35 additions & 0 deletions stylesheets/default.css
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,41 @@ button#dark-mode-toggle svg#dark-mode-moon {
background-color: #ffa8a8;
}

.content .highlighter-rouge > .highlight {
position: relative;
}

.content .copy-button + pre.highlight {
padding-right: 40px;
}

.content .copy-button {
position: absolute;
top: 0;
right: 0;
z-index: 1;
border: none;
width: 36px;
height: 36px;
background-image: url('data:image/svg+xml, <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="%23808080" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect width="14" height="14" x="8" y="8" rx="2" ry="2"></rect><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"></path></svg>');
background-color: #000;
background-repeat: no-repeat;
background-position: center;
text-indent: 100%;
white-space: nowrap;
color: transparent;
overflow: hidden;
cursor: pointer;
}

.content .copy-button:hover {
filter: brightness(2);
}

.content .copy-button.copied {
background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"></path></svg>');
}

/*
* Blog posts
*/
Expand Down

0 comments on commit 8a19b02

Please sign in to comment.