forked from alex-shpak/hugo-book
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
alex-shpak#281, Copy code block content on click
- Loading branch information
1 parent
6a19326
commit 6720168
Showing
2 changed files
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
}); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters