Verified Commit 2d1e32dc authored by Aral Balkan's avatar Aral Balkan
Browse files

Fix the click/tap-to-copy feature for code examples

parent dd752255
......@@ -22,12 +22,12 @@ const $$ = document.querySelectorAll.bind(document)
if (navigator.clipboard !== undefined) {
$('#copy-tip').className = 'tip shown'
const copyAlert = $('#copy-alert')
const codeSegments = $$('pre code')
const codeSegments = $$('pre')
const clickEventListener = event => {
function clickEventListener(event) {
// If there is a selection, copy that. If not, copy the whole code section.
const selection = document.getSelection()
const codeToCopy = selection.toString() === '' ? event.target.innerText : selection.toString();
const codeToCopy = selection.toString() === '' ? this.getElementsByTagName('code')[0].innerText : selection.toString();
navigator.clipboard.writeText(codeToCopy).then(() => {
copyAlert.className = 'show-copy-alert'
setTimeout(() => { copyAlert.className = 'hide-copy-alert' }, 1000)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment