Fix empty innerText

This commit is contained in:
Mimi 2024-05-13 13:46:20 +08:00
parent 783b6ffe8c
commit ddecbe3628
2 changed files with 7 additions and 4 deletions

View File

@ -14,7 +14,7 @@ document.addEventListener('page:loaded', () => {
newElement.className = 'mermaid'; newElement.className = 'mermaid';
box.appendChild(newElement); box.appendChild(newElement);
if (CONFIG.copycode.enable) { if (CONFIG.copycode.enable) {
NexT.utils.registerCopyButton(box, box, element.innerText); NexT.utils.registerCopyButton(box, box, element.textContent);
} }
const parent = element.parentNode; const parent = element.parentNode;
parent.parentNode.replaceChild(box, parent); parent.parentNode.replaceChild(box, parent);

View File

@ -39,11 +39,15 @@ NexT.utils = {
}); });
}, },
registerCopyButton(target, element, code) { registerCopyButton(target, element, code = '') {
// One-click copy code support. // One-click copy code support.
target.insertAdjacentHTML('beforeend', '<div class="copy-btn"><i class="fa fa-copy fa-fw"></i></div>'); target.insertAdjacentHTML('beforeend', '<div class="copy-btn"><i class="fa fa-copy fa-fw"></i></div>');
const button = target.querySelector('.copy-btn'); const button = target.querySelector('.copy-btn');
button.addEventListener('click', () => { button.addEventListener('click', () => {
if (!code) {
const lines = element.querySelector('.code') || element.querySelector('code');
code = lines.innerText;
}
if (navigator.clipboard) { if (navigator.clipboard) {
// https://caniuse.com/mdn-api_clipboard_writetext // https://caniuse.com/mdn-api_clipboard_writetext
navigator.clipboard.writeText(code).then(() => { navigator.clipboard.writeText(code).then(() => {
@ -131,8 +135,7 @@ NexT.utils = {
}); });
} }
if (!inited && CONFIG.copycode.enable) { if (!inited && CONFIG.copycode.enable) {
const lines = element.querySelector('.code') || element.querySelector('code'); this.registerCopyButton(target, element);
this.registerCopyButton(target, element, lines.innerText);
} }
}); });
}, },