mirror of
https://github.com/alex-shpak/hugo-book.git
synced 2024-11-28 14:10:14 +00:00
#281, Copy code block content on click
This commit is contained in:
parent
6a1932611c
commit
6720168dd5
21
assets/clipboard.js
Normal file
21
assets/clipboard.js
Normal file
@ -0,0 +1,21 @@
|
||||
(function () {
|
||||
function select(element) {
|
||||
const selection = window.getSelection();
|
||||
|
||||
const range = document.createRange();
|
||||
range.selectNodeContents(element);
|
||||
|
||||
selection.removeAllRanges();
|
||||
selection.addRange(range);
|
||||
}
|
||||
|
||||
document.querySelectorAll("pre code").forEach(code => {
|
||||
code.addEventListener("click", function (event) {
|
||||
select(code.parentElement);
|
||||
|
||||
if (navigator.clipboard) {
|
||||
navigator.clipboard.writeText(code.parentElement.textContent);
|
||||
}
|
||||
});
|
||||
});
|
||||
})();
|
@ -23,3 +23,8 @@
|
||||
</div>
|
||||
{{ end }}
|
||||
</div>
|
||||
|
||||
{{ $script := resources.Get "clipboard.js" | resources.Minify }}
|
||||
{{ with $script.Content }}
|
||||
<script>{{ . | safeJS }}</script>
|
||||
{{ end }}
|
||||
|
Loading…
Reference in New Issue
Block a user