-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add code copy button to all code fields in the blog (#81)
- Loading branch information
1 parent
a9f2ddc
commit 16c9d78
Showing
10 changed files
with
110 additions
and
6 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
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,56 @@ | ||
/** | ||
* Utils | ||
*/ | ||
|
||
// Add code-copy buttons using progressive enhancement | ||
// © 2019. Tom Spencer | ||
// https://www.fiznool.com/blog/2018/09/14/adding-click-to-copy-buttons-to-a-hugo-powered-blog/ | ||
(function() { | ||
'use strict'; | ||
|
||
if(!document.queryCommandSupported('copy')) { | ||
return; | ||
} | ||
|
||
function flashCopyMessage(el, msg) { | ||
el.textContent = msg; | ||
setTimeout(function() { | ||
el.textContent = "Copy"; | ||
}, 1000); | ||
} | ||
|
||
function selectText(node) { | ||
var selection = window.getSelection(); | ||
var range = document.createRange(); | ||
range.selectNodeContents(node); | ||
selection.removeAllRanges(); | ||
selection.addRange(range); | ||
return selection; | ||
} | ||
|
||
function addCopyButton(containerEl) { | ||
var copyBtn = document.createElement("button"); | ||
copyBtn.className = "highlight-copy-btn"; | ||
copyBtn.textContent = "Copy"; | ||
|
||
var codeEl = containerEl.firstElementChild; | ||
copyBtn.addEventListener('click', function() { | ||
try { | ||
var selection = selectText(codeEl); | ||
document.execCommand('copy'); | ||
selection.removeAllRanges(); | ||
|
||
flashCopyMessage(copyBtn, 'Copied!') | ||
} catch(e) { | ||
console && console.log(e); | ||
flashCopyMessage(copyBtn, 'Failed :\'(') | ||
} | ||
}); | ||
|
||
containerEl.appendChild(copyBtn); | ||
} | ||
|
||
// Add copy button to code blocks | ||
var highlightBlocks = document.getElementsByClassName('highlight'); | ||
Array.prototype.forEach.call(highlightBlocks, addCopyButton); | ||
})(); |
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
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
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
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
1 change: 1 addition & 0 deletions
1
resources/_gen/assets/js/js/bundle.js_d11fe7b62c27961c87ecd0f2490357b9.json
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 @@ | ||
{"Target":"js/bundle.min.4a9a0ac3d2217822c7865b4161e6c2a71de1d70492264337755427898dd718f6.js","MediaType":"application/javascript","Data":{"Integrity":"sha256-SpoKw9IheCLHhltBYebCpx3h1wSSJkM3dVQniY3XGPY="}} |
1 change: 0 additions & 1 deletion
1
resources/_gen/assets/js/js/main.js_d11fe7b62c27961c87ecd0f2490357b9.json
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
This
.highlight pre
value is too large, causing issue #92