🐛 Fixed #155, the code block overflow in the markdown file.

This commit is contained in:
elkan1788
2025-01-21 20:44:34 +08:00
parent 23e220b5f3
commit 65a7a9c536
4 changed files with 208 additions and 81 deletions

View File

@@ -1,37 +1,35 @@
/* clipboard plugin */
NexT.plugins.others.clipboard = function() {
NexT.plugins.others.clipboard = function () {
const clipboard_js = NexT.utils.getCDNResource(NexT.CONFIG.page.clipboard.js);
NexT.utils.getScript(clipboard_js, function(){
NexT.utils.getScript(clipboard_js, function () {
let figure = document.querySelectorAll('.highlight pre');
if (figure.length === 0 || !NexT.CONFIG.copybtn) return;
/** Add copy button DOM. **/
figure.forEach(element => {
let cn = element.querySelector('code').className;
let cn = element.querySelector('code').className;
if (cn === '') return;
element.children[0].insertAdjacentHTML('beforebegin', '<div class="copy-btn" data-clipboard-snippe><i class="fa fa-copy fa-fw"></i></div>');
var clipboard = new ClipboardJS(element.children[0],
{
text: function(trigger) {
return trigger.nextElementSibling.textContent.trim();
}
});
clipboard.on('success', function (e) {
e.clearSelection();
button.querySelector('i').className = 'fa fa-check-circle fa-fw';
});
element.insertAdjacentHTML('beforeend', '<div class="copy-btn"><i class="fa fa-copy fa-fw"></i></div>');
});
clipboard.on('error', function (e) {
console.error('Copy failed:', e);
button.querySelector('i').className = 'fa fa-times-circle fa-fw';
});
/** Register the clipboard event. **/
var clipboard = new ClipboardJS('.copy-btn', {
text: function (trigger) {
// TODO: Why there clipboard default text content with enter?
return trigger.previousElementSibling.textContent.trim();
}
});
const button = element.querySelector('.copy-btn');
element.addEventListener('mouseleave', () => {
setTimeout(() => {
button.querySelector('i').className = 'fa fa-copy fa-fw';
}, 300);
});
clipboard.on('success', function (e) {
e.clearSelection();
e.trigger.querySelector('i').className = 'fa fa-fw fa-check-circle';
});
clipboard.on('error', function (e) {
console.error('Copy failed:', e);
e.trigger.querySelector('i').className = 'fa fa-fw fa-times-circle';
});
});
}