-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "copy to clipboard" button to code blocks
- Loading branch information
Showing
3 changed files
with
98 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
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,59 @@ | ||
$(document).ready(function() { | ||
const copyText = function(text) { | ||
const isRTL = document.documentElement.getAttribute('dir') == 'rtl'; | ||
|
||
var textarea = document.createElement('textarea'); | ||
// Prevent zooming on iOS | ||
textarea.style.fontSize = '12pt'; | ||
// Reset box model | ||
textarea.style.border = '0'; | ||
textarea.style.padding = '0'; | ||
textarea.style.margin = '0'; | ||
// Move element out of screen horizontally | ||
textarea.style.position = 'absolute'; | ||
textarea.style[isRTL ? 'right' : 'left'] = '-9999px'; | ||
// Move element to the same position vertically | ||
let yPosition = window.pageYOffset || document.documentElement.scrollTop; | ||
textarea.style.top = yPosition + "px"; | ||
|
||
textarea.setAttribute('readonly', ''); | ||
textarea.value = text; | ||
document.body.appendChild(textarea); | ||
|
||
let success = true; | ||
try { | ||
textarea.select(); | ||
success = document.execCommand("copy"); | ||
} catch { | ||
success = false; | ||
} | ||
textarea.parentNode.removeChild(textarea); | ||
return success; | ||
}; | ||
|
||
const copyButtonEventListener = function(event) { | ||
const thisButton = event.target; | ||
|
||
// Locate the <code> element | ||
let codeBlock = thisButton.nextElementSibling; | ||
while (codeBlock && codeBlock.tagName.toLowerCase() !== 'code') { | ||
codeBlock = codeBlock.nextElementSibling; | ||
} | ||
if (!codeBlock) { | ||
// No <code> found - wtf? | ||
throw new Error("No code block found beside this button. This is unexpected."); | ||
} | ||
return copyText(codeBlock.innerText); | ||
}; | ||
|
||
$(".page__content pre > code").each(function() { | ||
// Locate the <pre> element | ||
const container = $(this).parent(); | ||
var copyButton = document.createElement("button"); | ||
copyButton.title = "Copy to clipboard"; | ||
copyButton.className = "copy-button"; | ||
copyButton.innerHTML = '<i class="far fa-copy"></i>'; | ||
copyButton.addEventListener("click", copyButtonEventListener); | ||
container.prepend(copyButton); | ||
}); | ||
}); |