#281, Copy code block content on click
This commit is contained in:
parent
6a1932611c
commit
6720168dd5
2 changed files with 26 additions and 0 deletions
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 a new issue