Skip to content

Commit

Permalink
alex-shpak#281, Copy code block content on click
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-shpak committed May 3, 2021
1 parent 6a19326 commit 6720168
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
21 changes: 21 additions & 0 deletions assets/clipboard.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
});
})();
5 changes: 5 additions & 0 deletions layouts/partials/docs/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@
</div>
{{ end }}
</div>

{{ $script := resources.Get "clipboard.js" | resources.Minify }}
{{ with $script.Content }}
<script>{{ . | safeJS }}</script>
{{ end }}

0 comments on commit 6720168

Please sign in to comment.