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:
@@ -17,7 +17,7 @@
|
||||
| 2 | 移除 Open / Print 按钮 | 0.5 h | 低 |
|
||||
| 3 | 代码配色 VS 风格 | 1.5–2.5 h | 低 |
|
||||
| 4 | Ctrl + 滚轮缩放 | 0.5–1 h | 低 |
|
||||
| 5 | 标题折叠 | 6–10 h | 中高 |
|
||||
| 5 | 标题折叠(已完成) | 6–10 h | 中高 |
|
||||
| 6 | 回归(大文档、多标签、mermaid、打印) | 2–3 h | 中 |
|
||||
|
||||
轻量版约 1 天;稳妥版(折叠完整、含回归)约 2.5–3.5 天。
|
||||
@@ -69,26 +69,33 @@
|
||||
|
||||
验收:预览/编辑都能缩放;范围 50%–300%;Ctrl+0 复位;不触发浏览器缩放;不按 Ctrl 时滚动不受影响。
|
||||
|
||||
## 步骤 5:标题折叠
|
||||
## 步骤 5:标题折叠(已实现)
|
||||
|
||||
实现要点:
|
||||
已确认的决策:
|
||||
|
||||
1. 在 `preview-enhance.js` 里,把 `#preview` 的标题按层级包成嵌套 `<section class="mdp-fold" data-level="n">`。
|
||||
2. 标题左侧加三角按钮(`<button class="mdp-fold-toggle" aria-expanded>`),点击切换 `collapsed`。
|
||||
3. **状态持久化**:`localStorage` 按 `tabKey(文件路径)+ heading id` 存折叠集合;Rust 需把 tab 路径传给页面(`__setContent` 多一个参数,或复用 `__setTabs` 的 active path)。
|
||||
4. 每次 `__setPreview` 重建 DOM 后立即恢复折叠态(在 idle enhance 之前),避免闪烁。
|
||||
5. 查找:命中在折叠区时自动展开祖先(或先展开全部再恢复,二选一,需实测体验)。
|
||||
6. Mermaid:隐藏容器里尺寸为 0,展开后需要重新渲染/触发布局;建议记录源码,展开时重跑。
|
||||
7. 打印:`@media print` 强制展开,避免打印缺失内容。
|
||||
8. 可选:`Alt+点击` 折叠整棵子树;“全部折叠/展开”快捷键。
|
||||
- 三角按钮**只在标题 hover 时显示**(`opacity: 0` → `.6`)。
|
||||
- 点 H2 折到下一个 H2,点 H3 折到下一个 H3(实际是折到下一个同级或更高级标题,自然包含子级)。
|
||||
- **不持久化**:每次打开/重渲染默认全部展开。
|
||||
- 不做“全部折叠/展开”UI。
|
||||
- 查找命中折叠区域时自动展开(已实现,成本很低)。
|
||||
|
||||
验收:
|
||||
实现:
|
||||
|
||||
- 点击 H1/H2/H3 三角可折叠其正文与子标题;
|
||||
- 自动保存 700ms 后折叠状态不丢;
|
||||
- 切换标签后各自的折叠状态正确;
|
||||
- 查找、锚点跳转、打印、mermaid 均正常;
|
||||
- 大文档(>= 1MB)折叠后滚动与展开无明显卡顿。
|
||||
- `assets/enhance/preview-enhance.js` 的 `enhanceFolds()`:把 `#preview` 直接子节点按标题切分,包成嵌套
|
||||
`<section class="mdp-fold" data-level="n">` + `<div class="mdp-fold-body">`;无正文的标题不包。
|
||||
- 按钮 `<button class="mdp-fold-toggle" aria-expanded>` 插入标题内部,绝对定位到左侧 `-20px`。
|
||||
- 点击委托在 `document` 上,切换 `collapsed`;`aria-expanded` 同步。
|
||||
- `window.__mdPreviewExpandFold(el)` 被查找的 `selectFindHit` 调用,逐层展开祖先。
|
||||
- 打印:`@media print` 强制 `.mdp-fold-body { display: block }` 并隐藏按钮。
|
||||
- Mermaid/KaTeX 在折叠前已渲染,只是被 `display:none` 隐藏,展开无需重跑。
|
||||
|
||||
验收(CDP 实测):
|
||||
|
||||
- 9 个 section 层级正确(H1 > H2 > H3 > H4 嵌套);
|
||||
- 折叠 H2 A-1 后其 body `display:none`,包含 2 个嵌套 section;
|
||||
- 再次点击恢复,`aria-expanded` 同步;
|
||||
- 折叠 H2 B-1 后查找 `println`:折叠数 1 → 0,命中可见;
|
||||
- 标题 hover 时按钮 `opacity: 0.6`,静止时 `0`。
|
||||
|
||||
## 回归清单(每次发版)
|
||||
|
||||
@@ -99,7 +106,7 @@
|
||||
- [ ] 外部修改提示与保护
|
||||
- [ ] 查找(含中文输入法组合输入)
|
||||
- [ ] Ctrl+滚轮、Ctrl+0、50%–300%
|
||||
- [ ] 折叠状态与自动保存共存
|
||||
- [ ] 标题折叠:hover 显示三角、H2/H3 折叠范围、查找自动展开、打印全展开
|
||||
- [ ] 代码块始终深色(VS Dark),行内代码随主题
|
||||
- [ ] 打印预览完整
|
||||
- [ ] 单实例转发
|
||||
|
||||
BIN
agent/reports/feature4-fold.png
Normal file
BIN
agent/reports/feature4-fold.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 114 KiB |
@@ -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);
|
||||
});
|
||||
};
|
||||
})();
|
||||
|
||||
61
src/main.rs
61
src/main.rs
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user