#281, Copy code block content on click

This commit is contained in:
Alex Shpak 2021-05-03 23:52:00 +02:00
parent 6a1932611c
commit 6720168dd5
2 changed files with 26 additions and 0 deletions

21
assets/clipboard.js Normal file
View 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);
}
});
});
})();

View file

@ -23,3 +23,8 @@
</div>
{{ end }}
</div>
{{ $script := resources.Get "clipboard.js" | resources.Minify }}
{{ with $script.Content }}
<script>{{ . | safeJS }}</script>
{{ end }}