-
Notifications
You must be signed in to change notification settings - Fork 726
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update code snippets to show button on hover (#3561)
Signed-off-by: Aaron Crawfis <[email protected]>
- Loading branch information
1 parent
4c82f65
commit 377d8a5
Showing
2 changed files
with
48 additions
and
82 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 |
---|---|---|
@@ -1,49 +1,35 @@ | ||
function addCopyButtons(clipboard) { | ||
document.querySelectorAll('pre > code').forEach(function(codeBlock) { | ||
var button = document.createElement('button'); | ||
button.className = 'copy-code-button'; | ||
button.type = 'button'; | ||
button.innerText = 'Copy'; | ||
const highlightClass = document.querySelectorAll('.highlight'); | ||
|
||
button.addEventListener('click', function() { | ||
clipboard.writeText(codeBlock.textContent).then( | ||
function() { | ||
button.blur(); | ||
highlightClass.forEach(element => { | ||
const copyIcon = document.createElement('i'); | ||
copyIcon.classList.add('fas', 'fa-copy', 'copy-icon'); | ||
copyIcon.style.color = 'white'; | ||
copyIcon.style.display = 'none'; | ||
element.appendChild(copyIcon); | ||
|
||
button.innerText = 'Copied!'; | ||
setTimeout(function() { | ||
button.innerText = 'Copy'; | ||
}, 2000); | ||
}, | ||
function(error) { | ||
button.innerText = 'Error'; | ||
console.error(error); | ||
} | ||
); | ||
}); | ||
element.addEventListener('mouseenter', () => { | ||
copyIcon.style.display = 'inline'; | ||
}); | ||
|
||
var pre = codeBlock.parentNode; | ||
if (pre.parentNode.classList.contains('highlight')) { | ||
var highlight = pre.parentNode; | ||
highlight.parentNode.insertBefore(button, highlight); | ||
} else { | ||
pre.parentNode.insertBefore(button, pre); | ||
} | ||
}); | ||
} | ||
element.addEventListener('mouseleave', () => { | ||
copyIcon.style.display = 'none'; | ||
copyIcon.classList.replace('fa-check', 'fa-copy'); | ||
}); | ||
|
||
if (navigator && navigator.clipboard) { | ||
addCopyButtons(navigator.clipboard); | ||
} else { | ||
var script = document.createElement('script'); | ||
script.src = | ||
'https://cdnjs.cloudflare.com/ajax/libs/clipboard-polyfill/2.7.0/clipboard-polyfill.promise.js'; | ||
script.integrity = 'sha256-waClS2re9NUbXRsryKoof+F9qc1gjjIhc2eT7ZbIv94='; | ||
script.crossOrigin = 'anonymous'; | ||
copyIcon.addEventListener('click', async () => { | ||
const selection = window.getSelection(); | ||
const range = document.createRange(); | ||
range.selectNodeContents(element); | ||
selection.removeAllRanges(); | ||
selection.addRange(range); | ||
|
||
script.onload = function() { | ||
addCopyButtons(clipboard); | ||
}; | ||
|
||
document.body.appendChild(script); | ||
} | ||
try { | ||
await navigator.clipboard.writeText(selection.toString()); | ||
console.log('Text copied to clipboard'); | ||
copyIcon.classList.replace('fa-copy', 'fa-check'); | ||
selection.removeAllRanges(); | ||
} catch (error) { | ||
console.error('Failed to copy: ', error); | ||
} | ||
}); | ||
}); |