| 1 |
import copy from 'copy-to-clipboard'; |
| 2 |
|
| 3 |
const handleCopyButton = () => { |
| 4 |
const buttons = Array.from( |
| 5 |
document.querySelectorAll( |
| 6 |
'.code-block-pro-copy-button:not(.cbp-cb-loaded)', |
| 7 |
), |
| 8 |
); |
| 9 |
buttons.forEach((button) => { |
| 10 |
button.classList.add('cbp-cb-loaded'); |
| 11 |
// Setting it to block here lets users deactivate the plugin safely |
| 12 |
button.style.display = 'block'; |
| 13 |
button.addEventListener('click', (event) => { |
| 14 |
const b = event.target?.closest('span'); |
| 15 |
copy(b?.dataset?.code ?? '', { |
| 16 |
format: 'text/plain', |
| 17 |
}); |
| 18 |
b.classList.add('copying'); |
| 19 |
setTimeout(() => { |
| 20 |
b.classList.remove('copying'); |
| 21 |
}, 2000); |
| 22 |
}); |
| 23 |
}); |
| 24 |
}; |
| 25 |
|
| 26 |
const handleHighlighter = () => { |
| 27 |
const codeBlocks = Array.from( |
| 28 |
document.querySelectorAll( |
| 29 |
'.wp-block-kevinbatdorf-code-block-pro pre:not(.cbp-hl-loaded)', |
| 30 |
), |
| 31 |
); |
| 32 |
|
| 33 |
codeBlocks.forEach((codeBlock) => { |
| 34 |
codeBlock.classList.add('cbp-hl-loaded'); |
| 35 |
// Search for highlights |
| 36 |
const highlighters = codeBlock.querySelectorAll('.cbp-line-highlight'); |
| 37 |
if (!highlighters.length) return; |
| 38 |
|
| 39 |
// We need to track the block width so we can adjust the |
| 40 |
// highlighter width in case of overflow |
| 41 |
const resizeObserver = new ResizeObserver((entries) => { |
| 42 |
entries.forEach((entry) => { |
| 43 |
// get width of inner code block |
| 44 |
codeBlock.style.setProperty('--cbp-block-width', '0'); |
| 45 |
const codeWidth = |
| 46 |
entry.target.querySelector('code').offsetWidth; |
| 47 |
const computed = |
| 48 |
codeWidth + 16 === entry.target.scrollWidth |
| 49 |
? codeWidth + 16 |
| 50 |
: entry.target.scrollWidth + 16; |
| 51 |
codeBlock.style.setProperty( |
| 52 |
'--cbp-block-width', |
| 53 |
`${computed}px`, |
| 54 |
); |
| 55 |
}); |
| 56 |
}); |
| 57 |
resizeObserver.observe(codeBlock); |
| 58 |
|
| 59 |
// add the highlighter |
| 60 |
highlighters.forEach((highlighter) => { |
| 61 |
highlighter.insertAdjacentHTML( |
| 62 |
'beforeend', |
| 63 |
'<span aria-hidden="true" class="cbp-line-highlighter"></span>', |
| 64 |
); |
| 65 |
}); |
| 66 |
}); |
| 67 |
}; |
| 68 |
|
| 69 |
const handleFontLoading = () => { |
| 70 |
if (!window.codeBlockPro?.pluginUrl) return; |
| 71 |
const elements = Array.from( |
| 72 |
document.querySelectorAll( |
| 73 |
'[data-code-block-pro-font-family]:not(.cbp-ff-loaded)', |
| 74 |
) || [], |
| 75 |
); |
| 76 |
elements.forEach((e) => e.classList.add('cbp-ff-loaded')); |
| 77 |
const fontsToLoad = new Set( |
| 78 |
elements.map((f) => f.dataset.codeBlockProFontFamily).filter(Boolean), |
| 79 |
); |
| 80 |
[...fontsToLoad].forEach(async (fontName) => { |
| 81 |
const font = new FontFace( |
| 82 |
fontName, |
| 83 |
`url(${window.codeBlockPro.pluginUrl}/build/fonts/${fontName}.woff2)`, |
| 84 |
); |
| 85 |
await font.load(); |
| 86 |
document.fonts.add(font); |
| 87 |
}); |
| 88 |
}; |
| 89 |
|
| 90 |
const runAll = () => { |
| 91 |
handleCopyButton(); |
| 92 |
handleHighlighter(); |
| 93 |
handleFontLoading(); |
| 94 |
}; |
| 95 |
|
| 96 |
// Functions are idempotent, so we can run them on load and DOMContentLoaded |
| 97 |
runAll(); |
| 98 |
window.addEventListener('DOMContentLoaded', runAll); |
| 99 |
window.addEventListener('load', runAll); |
| 100 |
|