8000 fix: copy button position by Borber · Pull Request #110 · tomfran/typo · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: copy button position #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 1, 2025
Merged
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
23 changes: 15 additions & 8 deletions assets/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ li {
list-style-type: none;
}

& > input[type="checkbox"] {
&>input[type="checkbox"] {
width: var(--li-checkbox-size);
height: var(--li-checkbox-size);
margin: 0 0.2em 0.15em -1.25em;
Expand All @@ -86,17 +86,24 @@ input {

/* Code blocks */

.highlight {
position: relative;
}


.copy-code-container {
position: absolute;
top: 10px;
right: 10px;
z-index: 10;
}

.copy-code-button {
background-color: var(--background);
font-family: var(--font-mono);
padding: 3px 6px;
font-size: 0.8em;
font-size: .8em;
border-radius: var(--copy-code-button-border-radius);
position: absolute;
top: 10px;
right: 10px;
z-index: 1;
display: none;
border: 1px solid var(--code-border);
}

Expand Down Expand Up @@ -598,4 +605,4 @@ blockquote {
blockquote p {
margin-left: 1rem;
margin-right: 1rem;
}
}
35 changes: 19 additions & 16 deletions static/js/copy-code.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
document.addEventListener("DOMContentLoaded", function () {
const codeBlocks = document.querySelectorAll("pre");
const codeBlocks = document.querySelectorAll(".highlight")

codeBlocks.forEach((codeBlock) => {
if (codeBlock.className == "mermaid") return;
const copyButton = document.createElement("button");
copyButton.className = "copy-code-button";
copyButton.textContent = "copy";
if (codeBlock.className == "mermaid") return
const copyButton = document.createElement("button")
copyButton.className = "copy-code-button"
copyButton.textContent = "copy"
const copyButtonContainer = document.createElement("div")
copyButtonContainer.className = "copy-code-container"
copyButtonContainer.appendChild(copyButton)

// Insert the button inside the <pre> block
codeBlock.appendChild(copyButton);
codeBlock.appendChild(copyButton)

copyButton.addEventListener("click", function () {
const code = codeBlock.querySelector("code");
const code = codeBlock.querySelector("code")
// Get the code content
const textToCopy = code.textContent || code.innerText;
const textToCopy = code.textContent || code.innerText

// Use the Clipboard API to copy the text
navigator.clipboard
.writeText(textToCopy)
.then(() => {
// Change button text to "Copied"
copyButton.textContent = "copied";
copyButton.textContent = "copied"

setTimeout(() => {
copyButton.textContent = "copy";
}, 2000); // Reset the button text after 2 seconds
copyButton.textContent = "copy"
}, 2000) // Reset the button text after 2 seconds
})
.catch((err) => {
console.error("Unable to copy text:", err);
});
});
});
});
console.error("Unable to copy text:", err)
})
})
})
})
0