Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions _site/scripts/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ function updateCode() {
if (!e.dataset['codesrc']) {
for (const code of e.querySelectorAll('code')) {
addCommentStyle(code);
addCopyButton(e);
}
continue;
}
Expand Down Expand Up @@ -90,6 +91,7 @@ function updateCode() {
code.textContent = text;
addCommentStyle(code);
e.appendChild(code);
addCopyButton(e);
});
})(e);
}
Expand All @@ -116,6 +118,23 @@ function addCommentStyle(code) {
}
}

function addCopyButton(pre) {
pre.style.position = "relative";

const btn = document.createElement("button");
btn.classList.add("copy-btn");
btn.textContent = "Copy";

btn.addEventListener("click", () => {
navigator.clipboard.writeText(pre.querySelector("code").textContent.trim()).then(() => {
btn.textContent = "Copied";
setTimeout(() => btn.textContent = "Copy", 700);
});
});

pre.insertBefore(btn, pre.firstChild);
}

let tocLevel = 4;

function disableTOC() {
Expand Down
18 changes: 18 additions & 0 deletions _site/styles/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,24 @@ ul.toplinks li img {
opacity: 0.7;
}

.copy-btn {
position: absolute;
top: 8px;
right: 8px;
border-radius: 8px;
padding: 2px 6px;
background: rgba(0,0,0,0.8);
color: #fff;
border: none;
cursor: pointer;
opacity: 0.7;
transition: opacity 0.2s ease, background 0.2s ease;
}

.copy-btn:hover {
opacity: 1;
}

/* test image */

/*body > nav, main {
Expand Down