2022-09-28 23:53:03 +08:00
|
|
|
/* global KEEP */
|
|
|
|
|
|
|
|
KEEP.initCodeBlockTools = () => {
|
|
|
|
HTMLElement.prototype.wrap = function (wrapper) {
|
2022-09-30 11:40:30 +08:00
|
|
|
this.parentNode.insertBefore(wrapper, this)
|
|
|
|
this.parentNode.removeChild(this)
|
|
|
|
wrapper.appendChild(this)
|
|
|
|
}
|
2022-09-28 23:53:03 +08:00
|
|
|
|
2022-09-30 11:40:30 +08:00
|
|
|
const { style: codeCopyStyle } = KEEP.theme_config?.code_copy
|
2022-10-17 21:54:21 +08:00
|
|
|
const { style: codeBlockStyle } = KEEP.theme_config?.code_block
|
2022-09-30 11:40:30 +08:00
|
|
|
const isMac = (codeBlockStyle || codeCopyStyle || 'default') === 'mac'
|
|
|
|
const foldedIconClassName = isMac ? 'fas fa-chevron-left' : 'fas fa-chevron-right'
|
|
|
|
const {
|
|
|
|
copy: copyLang,
|
|
|
|
copied: copiedLang,
|
|
|
|
fold: foldLang,
|
|
|
|
folded: foldedLang
|
|
|
|
} = KEEP.language_code_block
|
2022-10-11 17:42:27 +08:00
|
|
|
const foldDom = `<span class="tool fold tooltip" data-content="${foldLang}" data-offset-y="-2px"><i class="fas fa-chevron-down"></i></span>`
|
2022-09-28 23:53:03 +08:00
|
|
|
|
2022-09-30 11:40:30 +08:00
|
|
|
document.querySelectorAll('figure.highlight').forEach((element) => {
|
|
|
|
let codeLang = element.classList.length ? element.classList[1].toUpperCase() : ''
|
|
|
|
if (codeLang === 'PLAINTEXT') {
|
|
|
|
codeLang = ''
|
|
|
|
}
|
|
|
|
const highlightContainer = document.createElement('div')
|
|
|
|
highlightContainer.classList.add('highlight-container')
|
|
|
|
element.wrap(highlightContainer)
|
2022-09-29 16:42:09 +08:00
|
|
|
|
2022-09-28 23:53:03 +08:00
|
|
|
const codeLangDom = `${codeLang ? '<span class="code-lang">' + codeLang + '</span>' : ''}`
|
|
|
|
|
|
|
|
highlightContainer.insertAdjacentHTML(
|
|
|
|
'afterbegin',
|
|
|
|
`<div class="code-tools-box">
|
|
|
|
${isMac ? foldDom + codeLangDom : '<span>' + foldDom + codeLangDom + '</span>'}
|
2022-10-11 17:42:27 +08:00
|
|
|
<span class="tool copy tooltip" data-content="${copyLang}" data-offset-y="-2px"><i class="fas fa-copy"></i></span>
|
2022-09-28 23:53:03 +08:00
|
|
|
</div>`
|
2022-09-30 11:40:30 +08:00
|
|
|
)
|
|
|
|
const codeToolsBox = element.parentNode.querySelector('.code-tools-box')
|
|
|
|
const copyDom = codeToolsBox.querySelector('.copy')
|
|
|
|
const targetFoldDom = codeToolsBox.querySelector('.fold')
|
2022-09-28 23:53:03 +08:00
|
|
|
|
2022-09-30 11:40:30 +08:00
|
|
|
copyDom.addEventListener('click', (event) => {
|
|
|
|
const target = event.currentTarget
|
|
|
|
const code = [...element.querySelectorAll('.code .line')]
|
|
|
|
.map((line) => line.innerText)
|
|
|
|
.join('\n')
|
|
|
|
const tta = document.createElement('textarea')
|
|
|
|
tta.style.top = window.scrollY + 'px'
|
|
|
|
tta.style.position = 'absolute'
|
|
|
|
tta.style.opacity = '0'
|
|
|
|
tta.readOnly = true
|
|
|
|
tta.value = code
|
|
|
|
document.body.append(tta)
|
|
|
|
const selection = document.getSelection()
|
|
|
|
const selected = selection.rangeCount > 0 ? selection.getRangeAt(0) : false
|
|
|
|
tta.select()
|
|
|
|
tta.setSelectionRange(0, code.length)
|
|
|
|
tta.readOnly = false
|
|
|
|
const result = document.execCommand('copy')
|
2022-09-29 16:42:09 +08:00
|
|
|
|
2022-09-30 11:40:30 +08:00
|
|
|
const copyIconDom = target.querySelector('i')
|
|
|
|
const copyTooltipDom = codeToolsBox.querySelector('.copy .tooltip-content')
|
2022-09-29 16:42:09 +08:00
|
|
|
|
|
|
|
if (result) {
|
2022-09-30 11:40:30 +08:00
|
|
|
copyIconDom.className = 'fas fa-check'
|
|
|
|
copyTooltipDom && (copyTooltipDom.innerHTML = copiedLang)
|
2022-09-29 16:42:09 +08:00
|
|
|
} else {
|
2022-09-30 11:40:30 +08:00
|
|
|
copyIconDom.className = 'fas fa-times'
|
2022-09-29 16:42:09 +08:00
|
|
|
}
|
|
|
|
|
2022-09-30 11:40:30 +08:00
|
|
|
tta.blur()
|
|
|
|
target.blur()
|
2022-09-28 23:53:03 +08:00
|
|
|
if (selected) {
|
2022-09-30 11:40:30 +08:00
|
|
|
selection.removeAllRanges()
|
|
|
|
selection.addRange(selected)
|
2022-09-28 23:53:03 +08:00
|
|
|
}
|
2022-09-30 11:40:30 +08:00
|
|
|
document.body.removeChild(tta)
|
|
|
|
})
|
2022-09-28 23:53:03 +08:00
|
|
|
|
2022-09-30 11:40:30 +08:00
|
|
|
copyDom.addEventListener('mouseleave', (event) => {
|
2022-09-28 23:53:03 +08:00
|
|
|
setTimeout(() => {
|
2022-09-30 11:40:30 +08:00
|
|
|
event.target.querySelector('i').className = 'fas fa-copy'
|
|
|
|
const copyTooltipDom = codeToolsBox.querySelector('.copy .tooltip-content')
|
|
|
|
copyTooltipDom && (copyTooltipDom.innerHTML = copyLang)
|
|
|
|
}, 500)
|
|
|
|
})
|
2022-09-28 23:53:03 +08:00
|
|
|
|
|
|
|
let isFold = false
|
2022-09-30 11:40:30 +08:00
|
|
|
targetFoldDom.addEventListener('click', (event) => {
|
|
|
|
const target = event.currentTarget
|
|
|
|
const icon = target.querySelector('i')
|
|
|
|
const foldTooltipDom = codeToolsBox.querySelector('.fold .tooltip-content')
|
|
|
|
isFold = !isFold
|
2022-09-28 23:53:03 +08:00
|
|
|
if (isFold) {
|
2022-09-30 11:40:30 +08:00
|
|
|
icon.className = foldedIconClassName
|
|
|
|
element.classList.add('folded')
|
|
|
|
codeToolsBox.classList.add('folded')
|
2022-09-29 16:42:09 +08:00
|
|
|
foldTooltipDom && (foldTooltipDom.innerHTML = foldedLang)
|
2022-09-28 23:53:03 +08:00
|
|
|
} else {
|
2022-09-30 11:40:30 +08:00
|
|
|
icon.className = 'fas fa-chevron-down'
|
|
|
|
element.classList.remove('folded')
|
|
|
|
codeToolsBox.classList.remove('folded')
|
2022-09-29 16:42:09 +08:00
|
|
|
foldTooltipDom && (foldTooltipDom.innerHTML = foldLang)
|
2022-09-28 23:53:03 +08:00
|
|
|
}
|
2022-09-30 11:40:30 +08:00
|
|
|
})
|
|
|
|
})
|
2022-09-28 23:53:03 +08:00
|
|
|
}
|