Add copy code button (#963)
* Add copy code button * External package not needed * Improve alignement * Update button style + add feedback on click Signed-off-by: hossainemruz <hossainemruz@gmail.com> * Update dependencies Signed-off-by: hossainemruz <hossainemruz@gmail.com> --------- Signed-off-by: hossainemruz <hossainemruz@gmail.com> Co-authored-by: Emruz Hossain <hossainemruz@gmail.com>
This commit is contained in:
parent
9da3e3420b
commit
0d4d8a8e3c
8 changed files with 582 additions and 99 deletions
22
assets/scripts/features/copyCode/copyCode.js
Normal file
22
assets/scripts/features/copyCode/copyCode.js
Normal file
|
@ -0,0 +1,22 @@
|
|||
addCopyButtons(navigator.clipboard)
|
||||
|
||||
function addCopyButtons(clipboard) {
|
||||
document.querySelectorAll('pre > code').forEach(function (codeBlock) {
|
||||
const button = document.createElement('button')
|
||||
button.title = "Copy"
|
||||
button.className = 'copy-code-button btn btn-sm'
|
||||
button.innerHTML = "<i class='fa-regular fa-copy'></i>"
|
||||
|
||||
button.addEventListener('click', function () {
|
||||
clipboard.writeText(codeBlock.innerText)
|
||||
})
|
||||
|
||||
const pre = codeBlock.parentNode
|
||||
if (pre.parentNode.classList.contains('highlight')) {
|
||||
const highlight = pre.parentNode
|
||||
highlight.parentNode.insertBefore(button, highlight)
|
||||
} else {
|
||||
pre.parentNode.insertBefore(button, pre)
|
||||
}
|
||||
})
|
||||
}
|
1
assets/scripts/features/copyCode/index.js
Normal file
1
assets/scripts/features/copyCode/index.js
Normal file
|
@ -0,0 +1 @@
|
|||
import './copyCode'
|
|
@ -29,3 +29,7 @@ if (process.env.FEATURE_MATH === '1') {
|
|||
if (process.env.FEATURE_EMBEDPDF === '1') {
|
||||
import('./embedpdf')
|
||||
}
|
||||
|
||||
if (process.env.FEATURE_COPYCODEBUTTON === '1') {
|
||||
import('./copyCode')
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue