Skip to content

Instantly share code, notes, and snippets.

@moxak
Last active June 5, 2022 11:18
Show Gist options
  • Save moxak/4e115f199827620ce06cbea1a33141b1 to your computer and use it in GitHub Desktop.
Save moxak/4e115f199827620ce06cbea1a33141b1 to your computer and use it in GitHub Desktop.
<style>
.codeblock--name {
width: fit-content;
/* margin: 1rem 0 0; */
font-size: 0.9rem;
color: white;
/* background-color: #272822; */
background-color: #4f4f4f;
padding: 0.3rem 0.8rem !important;
/* font-weight: 400; */
border-radius: 0.3rem;
/* margin-top: -20px !important; */
margin-bottom: -0.3rem;
}
.codeblock--contet {
margin-top: 0;
}
pre {
margin-top: 0rem !important;
}
.highlight-wrapper {
display: block;
}
.highlight {
position: relative;
z-index: 0;
padding: 0;
margin: 0;
border-radius: 4px;
}
.highlight > .chroma {
color: #d0d0d0;
background-color: #212121;
position: static;
z-index: 1;
border-radius: 4px;
padding: 10px;
}
.chroma .lntd:first-child {
padding: 7px 7px 7px 10px;
margin: 0;
}
.chroma .lntd:last-child {
padding: 7px 10px 7px 7px;
margin: 0;
}
.copy-code-button {
position: absolute;
z-index: 2;
right: 0;
top: 0;
/* font-size: 1.5em; */
font-weight: 700;
line-height: 14px;
letter-spacing: 0.5px;
height: 2rem;
width: 65px;
color: #232326;
background-color: #7f7f7f;
border: 1.25px solid #232326;
border-top-left-radius: 0;
border-top-right-radius: 3px;
border-bottom-right-radius: 0;
border-bottom-left-radius: 4px;
white-space: nowrap;
padding: 4px 4px 5px 4px;
margin: 0 0 0 1px;
cursor: pointer;
opacity: 0.6;
}
.copy-code-button:hover,
.copy-code-button:focus,
.copy-code-button:active,
.copy-code-button:active:hover {
color: #222225;
background-color: #b3b3b3;
opacity: 0.8;
}
.copyable-text-area {
position: absolute;
height: 0;
z-index: -1;
opacity: .01;
}
</style>
<script>
const copyIcon = '<i class="fa-solid fa-clone fa-lg"></i>';
function createCopyButton(highlightDiv) {
const button = document.createElement("button");
button.innerHTML = copyIcon;
button.className = "copy-code-button";
button.type = "button";
button.addEventListener("click", () => copyCodeToClipboard(button, highlightDiv));
addCopyButtonToDom(button, highlightDiv);
}
async function copyCodeToClipboard(button, highlightDiv) {
const codeElement = highlightDiv.querySelector("code");
let codeToCopy = "";
if (codeElement) {
codeToCopy = codeElement.innerText;
} else {
const codeLines = highlightDiv.querySelectorAll("td");
codeLines.forEach((codeLine, index) => {
if ( (index+1) % 2 === 0) {
codeToCopy += codeLine.innerText + "\n";
}
});
}
try {
result = await navigator.permissions.query({ name: "clipboard-write" });
if (result.state == "granted" || result.state == "prompt") {
await navigator.clipboard.writeText(codeToCopy);
} else {
copyCodeBlockExecCommand(codeToCopy, highlightDiv);
}
} catch (_) {
copyCodeBlockExecCommand(codeToCopy, highlightDiv);
}
finally {
codeWasCopied(button);
}
}
function copyCodeBlockExecCommand(codeToCopy, highlightDiv) {
const textArea = document.createElement("textArea");
textArea.contentEditable = 'true'
textArea.readOnly = 'false'
textArea.className = "copyable-text-area";
textArea.value = codeToCopy;
highlightDiv.insertBefore(textArea, highlightDiv.firstChild);
const range = document.createRange()
range.selectNodeContents(textArea)
const sel = window.getSelection()
sel.removeAllRanges()
sel.addRange(range)
textArea.setSelectionRange(0, 999999)
document.execCommand("copy");
highlightDiv.removeChild(textArea);
}
function codeWasCopied(button) {
button.blur();
button.innerText = "Copied";
setTimeout(function() {
button.innerHTML = copyIcon;
}, 2000);
}
function addCopyButtonToDom(button, highlightDiv) {
highlightDiv.insertBefore(button, highlightDiv.firstChild);
const wrapper = document.createElement("div");
wrapper.className = "highlight-wrapper";
highlightDiv.parentNode.insertBefore(wrapper, highlightDiv);
wrapper.appendChild(highlightDiv);
}
document.querySelectorAll(".highlight").forEach(highlightDiv => createCopyButton(highlightDiv));
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment