Add language and clipboard buttons to code blocks

This commit is contained in:
Jake Howard 2022-08-30 22:48:45 +01:00
parent da89510eee
commit f3b603a82a
Signed by: jake
GPG Key ID: 57AFB45680EDD477
5 changed files with 50 additions and 6 deletions

View File

@ -59,6 +59,24 @@ window.addEventListener("load", () => {
endAudio: elevatorButton.dataset.endAudio,
preloadAudio: false,
});
document.querySelectorAll(".block-code").forEach((codeBlock) => {
const clipboardIcon = codeBlock.querySelector(".code-copy");
clipboardIcon.addEventListener("click", (event) => {
event.preventDefault();
navigator.clipboard
.writeText(codeBlock.querySelector("pre").textContent)
.then(() => {
clipboardIcon.classList.remove("fa-regular", "fa-clipboard");
clipboardIcon.classList.add("fa-solid", "fa-check");
setTimeout(() => {
clipboardIcon.classList.add("fa-regular", "fa-clipboard");
clipboardIcon.classList.remove("fa-solid", "fa-check");
}, 3000);
});
});
});
});
window.addEventListener("DOMContentLoaded", () => {

View File

@ -54,11 +54,31 @@ div.block-mermaid {
}
}
div.block-code .highlight {
pre,
span,
& {
@include dark-mode;
div.block-code {
.filename {
font-family: $family-code;
background-color: $grey-lighter;
font-size: 1rem;
position: relative;
text-align: right;
transform: translateY(100%) translateX(-100%);
display: inline-block;
left: 100%;
padding: 0.25rem 1rem;
border-radius: 0 0 0 $radius-large;
box-shadow: $shadow;
@include dark-mode {
background-color: $grey-darker;
}
}
.highlight {
pre,
span,
& {
@include dark-mode;
}
}
}

View File

@ -20,3 +20,4 @@ $code-background: none;
$pre-background: none;
$code-padding: 0;
$code: inherit;
$content-pre-padding: 2.75rem 1.25rem 1.25rem 1.25rem;

View File

@ -2,7 +2,8 @@
// Ensure animations aren't for the initial transition
html.dark-mode-animate & {
transition-duration: 0.5s;
transition-property: color, background-color, border-color, filter;
transition-property: color, background-color, border-color, filter,
box-shadow;
}
html.dark-mode & {

View File

@ -1 +1,5 @@
<span class="filename">
{{ value.language }}
<i class="fa-regular fa-clipboard is-clickable code-copy" title="Copy to clipboard"></i>
</span>
{{ value.code }}