Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support folding code blocks in tabs #694

Merged
merged 1 commit into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions source/js/next-boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
45 changes: 26 additions & 19 deletions source/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,47 +39,54 @@ 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;
let target;
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', '<div class="fold-cover"></div><div class="expand-btn"><i class="fa fa-angle-down fa-fw"></i></div>');
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', '<div class="copy-btn"><i class="fa fa-copy fa-fw"></i></div>');
const button = target.querySelector('.copy-btn');
Expand Down
Loading