added cactus theme files
This commit is contained in:
36
hugo-content/themes/cactus/static/js/code-copy.js
Normal file
36
hugo-content/themes/cactus/static/js/code-copy.js
Normal file
@@ -0,0 +1,36 @@
|
||||
(() => {
|
||||
|
||||
function createCopyButton(codeNode) {
|
||||
const copyBtn = document.createElement('button')
|
||||
copyBtn.className = 'code-copy-btn'
|
||||
copyBtn.type = 'button'
|
||||
copyBtn.innerText = 'copy'
|
||||
|
||||
let resetTimer
|
||||
copyBtn.addEventListener('click', () => {
|
||||
navigator.clipboard.writeText(codeNode.innerText).then(() => {
|
||||
copyBtn.innerText = 'copied!'
|
||||
}).then(() => {
|
||||
clearTimeout(resetTimer)
|
||||
resetTimer = setTimeout(() => {
|
||||
copyBtn.innerText = 'copy'
|
||||
}, 1000)
|
||||
})
|
||||
})
|
||||
|
||||
return copyBtn
|
||||
}
|
||||
|
||||
document.querySelectorAll('pre > code')
|
||||
.forEach((codeNode) => {
|
||||
const copyBtn = createCopyButton(codeNode);
|
||||
const preNode = codeNode.parentNode
|
||||
codeNode.parentNode.insertBefore(copyBtn, codeNode)
|
||||
})
|
||||
|
||||
document.querySelectorAll('.highlight table > tbody > tr > td:first-child .code-copy-btn')
|
||||
.forEach((btn) => {
|
||||
btn.remove()
|
||||
})
|
||||
|
||||
})()
|
||||
Reference in New Issue
Block a user