feat: fold heading sections with hover toggles
- Wrap #preview children into nested section.mdp-fold/.mdp-fold-body by heading level - Chevron button appears on heading hover; H2 folds to next H2, H3 to next H3, nested sections included - No persistence: every render starts fully expanded - Find auto-expands collapsed ancestors before scrolling to a hit - Print expands all sections and hides the toggles
This commit is contained in:
@@ -406,6 +406,98 @@
|
||||
});
|
||||
}
|
||||
|
||||
function isHeadingNode(node) {
|
||||
return !!node && node.nodeType === 1 && /^H[1-6]$/.test(node.tagName);
|
||||
}
|
||||
|
||||
function headingLevel(node) {
|
||||
return Number(node.tagName.charAt(1));
|
||||
}
|
||||
|
||||
function makeFoldToggle() {
|
||||
var btn = document.createElement('button');
|
||||
btn.type = 'button';
|
||||
btn.className = 'mdp-fold-toggle';
|
||||
btn.setAttribute('aria-expanded', 'true');
|
||||
btn.title = window.__mdPreviewFoldLabel || 'Collapse / Expand';
|
||||
btn.setAttribute('aria-label', btn.title);
|
||||
btn.innerHTML = '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" ' +
|
||||
'stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">' +
|
||||
'<path d="m6 9 6 6 6-6"/></svg>';
|
||||
return btn;
|
||||
}
|
||||
|
||||
// Wrap a heading and its body (up to the next heading of the same or higher
|
||||
// level) in a section; nested headings are wrapped recursively.
|
||||
function sectionizeFolds(container) {
|
||||
var nodes = Array.prototype.slice.call(container.childNodes);
|
||||
for (var i = 0; i < nodes.length; i++) {
|
||||
var node = nodes[i];
|
||||
if (!isHeadingNode(node)) continue;
|
||||
var level = headingLevel(node);
|
||||
var end = i + 1;
|
||||
while (end < nodes.length) {
|
||||
var next = nodes[end];
|
||||
if (isHeadingNode(next) && headingLevel(next) <= level) break;
|
||||
end++;
|
||||
}
|
||||
if (end === i + 1) continue;
|
||||
var section = document.createElement('section');
|
||||
section.className = 'mdp-fold';
|
||||
section.setAttribute('data-level', String(level));
|
||||
container.insertBefore(section, node);
|
||||
section.appendChild(node);
|
||||
var body = document.createElement('div');
|
||||
body.className = 'mdp-fold-body';
|
||||
for (var k = i + 1; k < end; k++) body.appendChild(nodes[k]);
|
||||
sectionizeFolds(body);
|
||||
section.appendChild(body);
|
||||
node.insertBefore(makeFoldToggle(), node.firstChild);
|
||||
i = end - 1;
|
||||
}
|
||||
}
|
||||
|
||||
var foldClicksBound = false;
|
||||
|
||||
function bindFoldClicks() {
|
||||
if (foldClicksBound) return;
|
||||
foldClicksBound = true;
|
||||
document.addEventListener('click', function(event) {
|
||||
var btn = event.target && event.target.closest ?
|
||||
event.target.closest('.mdp-fold-toggle') :
|
||||
null;
|
||||
if (!btn) return;
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
var section = btn.closest('.mdp-fold');
|
||||
if (!section) return;
|
||||
var collapsed = section.classList.toggle('collapsed');
|
||||
btn.setAttribute('aria-expanded', collapsed ? 'false' : 'true');
|
||||
});
|
||||
}
|
||||
|
||||
function enhanceFolds(root) {
|
||||
if (!root || root.querySelector('.mdp-fold')) return;
|
||||
sectionizeFolds(root);
|
||||
bindFoldClicks();
|
||||
}
|
||||
|
||||
window.__mdPreviewExpandFold = function(el) {
|
||||
var section = el && el.closest ? el.closest('.mdp-fold') : null;
|
||||
while (section) {
|
||||
if (section.classList.contains('collapsed')) {
|
||||
section.classList.remove('collapsed');
|
||||
var heading = section.firstElementChild;
|
||||
var btn = heading && heading.querySelector ?
|
||||
heading.querySelector('.mdp-fold-toggle') :
|
||||
null;
|
||||
if (btn) btn.setAttribute('aria-expanded', 'true');
|
||||
}
|
||||
var parent = section.parentElement;
|
||||
section = parent && parent.closest ? parent.closest('.mdp-fold') : null;
|
||||
}
|
||||
};
|
||||
|
||||
window.__setFeatureFlags = setFlags;
|
||||
|
||||
window.__setKatexCss = function(css) {
|
||||
@@ -426,6 +518,7 @@
|
||||
enhanceMarks(root);
|
||||
enhanceMath(root);
|
||||
enhanceMermaid(root);
|
||||
enhanceFolds(root);
|
||||
});
|
||||
};
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user