Add "copy to clipboard" button for code blocks (#2812)

* Add copy-to-clipboard button and JS

* Ignore line numbers if present

* Rewrite heading permalink code to use vanilla JS

* README: Add credits to zenorocha/clipboard.js (MIT License)

@iBug really wants a place here in the Credits section :P

* Add .no-copy for hiding the button, update docs

* Add td.rouge-code to selectors

* Fix navigator.clipboard branch

* Add screenreader text for copy button

* Restore focus to the button after copying

* Add site-wide enable switch
This commit is contained in:
iBug
2024-05-05 19:43:24 +08:00
committed by GitHub
parent c76813f6f5
commit 0527e17354
8 changed files with 199 additions and 26 deletions

View File

@ -591,3 +591,60 @@ a.reversefootnote {
position: static;
}
}
/*
Copy <pre> block to clipboard
========================================================================== */
// a <textarea> to hold text for document.execCommand("copy")
.clipboard-helper {
// Prevent zooming on iOS
font-size: 12pt !important;
border: 0 !important;
padding: 0 !important;
margin: 0 !important;
outline: none !important;
position: absolute;
}
pre {
.clipboard-copy-button {
display: block;
position: absolute;
top: 0.6em;
right: 0.5em;
z-index: 1;
background: none;
border: none;
outline: none;
border-radius: 0.1em;
padding: 0.2em 0.5em;
color: white;
opacity: 0.4;
transition: color 0.25s linear -0.25s, opacity 0.25s linear;
&:hover {
color: #ffffca;
}
&::before {
content: '';
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 2;
}
@at-root {
.no-copy & {
display: none;
}
}
}
&:hover .clipboard-copy-button {
opacity: 1;
}
}