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:
2026-10-21 16:53:03 +08:00
parent 99e1f0b6bf
commit f74a04bd13
4 changed files with 179 additions and 18 deletions

View File

@@ -144,6 +144,7 @@ struct Strings {
btn_preview: &'static str,
btn_new: &'static str,
new_filename: &'static str,
fold_toggle: &'static str,
btn_zoom: &'static str,
btn_zoom_out: &'static str,
btn_zoom_reset: &'static str,
@@ -169,6 +170,7 @@ impl Strings {
btn_preview: "预览 (Cmd/Ctrl+E)",
btn_new: "新建 Markdown (Cmd/Ctrl+N)",
new_filename: "新建.md",
fold_toggle: "折叠/展开",
btn_zoom: "正文缩放",
btn_zoom_out: "缩小正文 (Cmd/Ctrl+-)",
btn_zoom_reset: "重置正文缩放 (Cmd/Ctrl+0)",
@@ -190,6 +192,7 @@ impl Strings {
btn_preview: "Preview (Cmd/Ctrl+E)",
btn_new: "New Markdown (Cmd/Ctrl+N)",
new_filename: "Untitled.md",
fold_toggle: "Collapse / Expand",
btn_zoom: "Content zoom",
btn_zoom_out: "Zoom out (Cmd/Ctrl+-)",
btn_zoom_reset: "Reset zoom (Cmd/Ctrl+0)",
@@ -1061,6 +1064,37 @@ body.has-tabs {{ --chrome-top: 50px; }}
#preview h1,#preview h2,#preview h3,#preview h4 {{ margin-top: 1.4em; }}
#preview h1 {{ border-bottom: 1px solid #e1e4e8; padding-bottom: .3em; }}
#preview h2 {{ border-bottom: 1px solid #e1e4e8; padding-bottom: .2em; }}
/* Heading folding: triangle shows on heading hover; the body collapses down to
the next heading of the same or higher level. State is per-render only. */
#preview .mdp-fold > h1,
#preview .mdp-fold > h2,
#preview .mdp-fold > h3,
#preview .mdp-fold > h4,
#preview .mdp-fold > h5,
#preview .mdp-fold > h6 {{ position: relative; }}
#preview .mdp-fold-toggle {{
position: absolute; left: -20px; top: .3em;
width: 16px; height: 16px; padding: 0; border: 0; border-radius: 4px;
display: grid; place-items: center;
background: transparent; color: #999; cursor: pointer;
opacity: 0; transition: opacity .12s ease, background .12s ease;
}}
#preview h1:hover > .mdp-fold-toggle,
#preview h2:hover > .mdp-fold-toggle,
#preview h3:hover > .mdp-fold-toggle,
#preview h4:hover > .mdp-fold-toggle,
#preview h5:hover > .mdp-fold-toggle,
#preview h6:hover > .mdp-fold-toggle,
#preview .mdp-fold-toggle:focus-visible {{ opacity: .6; }}
#preview .mdp-fold-toggle:hover {{ opacity: 1; background: rgba(0,0,0,.07); }}
#preview .mdp-fold-toggle svg {{ width: 12px; height: 12px; transition: transform .15s ease; }}
#preview .mdp-fold.collapsed > h1 > .mdp-fold-toggle svg,
#preview .mdp-fold.collapsed > h2 > .mdp-fold-toggle svg,
#preview .mdp-fold.collapsed > h3 > .mdp-fold-toggle svg,
#preview .mdp-fold.collapsed > h4 > .mdp-fold-toggle svg,
#preview .mdp-fold.collapsed > h5 > .mdp-fold-toggle svg,
#preview .mdp-fold.collapsed > h6 > .mdp-fold-toggle svg {{ transform: rotate(-90deg); }}
#preview .mdp-fold.collapsed > .mdp-fold-body {{ display: none; }}
#preview code {{ background: #f0f0f0; padding: 2px 6px; border-radius: 4px; font-size: 90%; }}
/* Code blocks stay dark (VS Dark) even in the light app theme. */
#preview pre {{ background: #1e1e1e; color: #d4d4d4; padding: 16px; border-radius: 8px; overflow-x: auto; }}
@@ -1253,6 +1287,8 @@ body.empty .toolbar {{ display: none !important; }}
body {{ color: #d4d4d4; background: #1e1e1e; }}
#preview a {{ color: #6cb6ff; }}
#preview h1, #preview h2 {{ border-color: #333; }}
#preview .mdp-fold-toggle {{ color: #aaa; }}
#preview .mdp-fold-toggle:hover {{ background: rgba(255,255,255,.1); }}
#preview .front-matter {{ border-color: #333; color: #a9b1ba; }}
#preview .front-matter pre {{ background: transparent !important; }}
#preview code:not(pre code) {{ background: #2d2d2d; }}
@@ -1327,6 +1363,8 @@ body.editing #app {{ max-width: none; padding: 0; }}
#app {{ max-width: none; padding: 0; }}
#preview .mdp-table-wrap {{ width: auto; margin: 1em 0; transform: none; overflow: visible; }}
#preview pre {{ -webkit-print-color-adjust: exact; print-color-adjust: exact; }}
#preview .mdp-fold-body {{ display: block !important; }}
#preview .mdp-fold-toggle {{ display: none !important; }}
}}
</style></head><body class="{body_class}">
<div class="tabbar" id="tabbar"><div class="tabs" id="tabs"></div><div class="doc-stats" id="doc-stats" aria-live="polite"></div><button class="tab-open" id="tab-open" type="button" title="{btn_new}" aria-label="{btn_new}">+</button></div>
@@ -1361,6 +1399,7 @@ body.editing #app {{ max-width: none; padding: 0; }}
var ICON_DOWN = '<svg viewBox="0 0 24 24" width="17" height="17" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m6 9 6 6 6-6"/></svg>';
var ICON_CLOSE = '<svg viewBox="0 0 24 24" width="17" height="17" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M18 6 6 18"/><path d="m6 6 12 12"/></svg>';
var L_EDIT = '{btn_edit}', L_VIEW = '{btn_preview}';
window.__mdPreviewFoldLabel = '{fold_toggle}';
var btnToggle = document.getElementById('btn-toggle');
var btnZoom = document.getElementById('btn-zoom');
@@ -1562,6 +1601,7 @@ body.editing #app {{ max-width: none; padding: 0; }}
currentFindHit = (index + findHits.length) % findHits.length;
var hit = findHits[currentFindHit];
hit.classList.add('current');
if (window.__mdPreviewExpandFold) window.__mdPreviewExpandFold(hit);
hit.scrollIntoView({{ block: 'center', inline: 'nearest' }});
updateFindState();
}}
@@ -1962,6 +2002,7 @@ if(window.__enhancePreview)window.__enhancePreview();
btn_new = s.btn_new,
btn_edit = s.btn_edit,
btn_preview = s.btn_preview,
fold_toggle = s.fold_toggle,
btn_zoom = s.btn_zoom,
btn_zoom_out = s.btn_zoom_out,
btn_zoom_reset = s.btn_zoom_reset,
@@ -2365,6 +2406,26 @@ mod tests {
assert!(page.contains("window.addEventListener('wheel', onContentWheel, { passive: false })"));
}
#[test]
fn headings_get_hover_fold_toggles() {
let strings = Strings::for_lang(Lang::En);
let page = build_page(
&md_to_html("# A\n\n## B\n\ntext"),
"# A\n\n## B\n\ntext",
None,
EnhanceFlags::default(),
&strings,
false,
);
assert!(page.contains("mdp-fold-toggle"));
assert!(page.contains("mdp-fold-body"));
assert!(page.contains(".mdp-fold.collapsed > .mdp-fold-body { display: none; }"));
assert!(page.contains("window.__mdPreviewFoldLabel"));
assert!(page.contains("window.__mdPreviewExpandFold"));
assert!(page.contains("sectionizeFolds"));
}
#[test]
fn closing_the_last_editing_tab_restores_the_empty_preview() {
let strings = Strings::for_lang(Lang::En);