| 1 |
import copy from 'copy-to-clipboard'; |
| 2 |
|
| 3 |
// Handle copy button |
| 4 |
addEventListener('DOMContentLoaded', () => { |
| 5 |
const buttons = Array.from( |
| 6 |
document.querySelectorAll('.code-block-pro-copy-button'), |
| 7 |
); |
| 8 |
buttons.forEach((button) => { |
| 9 |
// Setting it to block here lets users deactivate the plugin safely |
| 10 |
button.style.display = 'block'; |
| 11 |
button.addEventListener('click', (event) => { |
| 12 |
const b = event.target?.closest('span'); |
| 13 |
copy(b?.dataset?.code ?? '', { |
| 14 |
format: 'text/plain', |
| 15 |
}); |
| 16 |
b.classList.add('copying'); |
| 17 |
setTimeout(() => { |
| 18 |
b.classList.remove('copying'); |
| 19 |
}, 2000); |
| 20 |
}); |
| 21 |
}); |
| 22 |
}); |
| 23 |
|
| 24 |
// Handle font loading |
| 25 |
addEventListener('DOMContentLoaded', () => { |
| 26 |
if (!window.codeBlockPro?.pluginUrl) return; |
| 27 |
const fontsToLoad = new Set( |
| 28 |
Array.from( |
| 29 |
document.querySelectorAll('[data-code-block-pro-font-family]') ?? |
| 30 |
[], |
| 31 |
) |
| 32 |
.map((f) => f.dataset.codeBlockProFontFamily) |
| 33 |
.filter(Boolean), |
| 34 |
); |
| 35 |
[...fontsToLoad].forEach(async (fontName) => { |
| 36 |
const font = new FontFace( |
| 37 |
fontName, |
| 38 |
`url(${window.codeBlockPro.pluginUrl}/build/fonts/${fontName}.woff2)`, |
| 39 |
); |
| 40 |
await font.load(); |
| 41 |
document.fonts.add(font); |
| 42 |
}); |
| 43 |
}); |
| 44 |
|