diff --git a/source/js/next-boot.js b/source/js/next-boot.js index 2ed68a885..fceb80bb2 100644 --- a/source/js/next-boot.js +++ b/source/js/next-boot.js @@ -29,6 +29,10 @@ NexT.boot.registerEvents = function() { target && target.click(); } }); + + window.addEventListener('tabs:click', e => { + NexT.utils.registerCodeblock(e.target); + }); }; NexT.boot.refresh = function() { diff --git a/source/js/utils.js b/source/js/utils.js index a0416b33a..abc50ff78 100644 --- a/source/js/utils.js +++ b/source/js/utils.js @@ -39,24 +39,27 @@ NexT.utils = { }); }, - registerCodeblock: function() { - let figure = document.querySelectorAll('figure.highlight'); + registerCodeblock: function(element) { + const inited = !!element; + let figure = (inited ? element : document).querySelectorAll('figure.highlight'); let isHljsWithWrap = true; if (figure.length === 0) { figure = document.querySelectorAll('pre:not(.mermaid)'); isHljsWithWrap = false; } figure.forEach(element => { - let span = element.querySelectorAll('.code .line span'); - if (span.length === 0) { - // Hljs without line_number and wrap - span = element.querySelectorAll('code.highlight span'); - } - span.forEach(s => { - s.classList.forEach(name => { - s.classList.replace(name, `hljs-${name}`); + if (!inited) { + let span = element.querySelectorAll('.code .line span'); + if (span.length === 0) { + // Hljs without line_number and wrap + span = element.querySelectorAll('code.highlight span'); + } + span.forEach(s => { + s.classList.forEach(name => { + s.classList.replace(name, `hljs-${name}`); + }); }); - }); + } const height = parseInt(window.getComputedStyle(element).height.replace('px', ''), 10); const needFold = CONFIG.fold.enable && (height > CONFIG.fold.height); if (!needFold && !CONFIG.copycode.enable) return; @@ -64,22 +67,26 @@ NexT.utils = { if (isHljsWithWrap && CONFIG.copycode.style === 'mac') { target = element; } else { - // https://github.com/next-theme/hexo-theme-next/issues/98 - // https://github.com/next-theme/hexo-theme-next/pull/508 - const container = element.querySelector('.table-container') || element; - const box = document.createElement('div'); - box.className = 'code-container'; - container.wrap(box); + let box = element.querySelector('.code-container'); + if (!box) { + // https://github.com/next-theme/hexo-theme-next/issues/98 + // https://github.com/next-theme/hexo-theme-next/pull/508 + const container = element.querySelector('.table-container') || element; + box = document.createElement('div'); + box.className = 'code-container'; + container.wrap(box); + } target = box; } - if (needFold) { + if (needFold && !target.classList.contains('unfold')) { target.classList.add('highlight-fold'); target.insertAdjacentHTML('beforeend', '
'); target.querySelector('.expand-btn').addEventListener('click', () => { target.classList.remove('highlight-fold'); + target.classList.add('unfold'); }); } - if (!CONFIG.copycode.enable) return; + if (inited || !CONFIG.copycode.enable) return; // One-click copy code support. target.insertAdjacentHTML('beforeend', '
'); const button = target.querySelector('.copy-btn');