fix(editor): fold ranges, stacked replace bar and container scrolling

This commit is contained in:
2026-11-06 09:14:20 +08:00
parent a58a94b05f
commit 2117551af7
3 changed files with 62 additions and 31 deletions

View File

@@ -1,15 +1,20 @@
:root { color-scheme: light dark; --chrome-top: 10px; --content-scale: 1; }
/* Reserve scrollbar space permanently so the fixed toolbar doesn't shift
between modes (one with scrollbar, one without). */
html { overflow-y: scroll; scrollbar-gutter: stable; }
/* The document scrolls inside #app, below the tab strip. Preview and edit mode
therefore show the same scrollbar position and size. */
html { overflow: hidden; }
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
margin: 0; padding: 0;
line-height: 1.6; font-size: 15px;
color: #1a1a1a; background: #fff;
height: 100vh; display: flex; flex-direction: column; overflow: hidden;
}
body.has-tabs { --chrome-top: 50px; }
#app { width: 100%; box-sizing: border-box; margin: 0 auto; padding: 24px 48px; }
#app { flex: 1 1 auto; min-height: 0; width: 100%; box-sizing: border-box; margin: 0 auto; padding: 24px 48px; overflow-y: auto; overflow-x: hidden; scrollbar-gutter: stable; }
#app::-webkit-scrollbar { width: 10px; }
#app::-webkit-scrollbar-track { background: transparent; }
#app::-webkit-scrollbar-thumb { background: rgba(128,128,128,.45); border-radius: 6px; }
#app::-webkit-scrollbar-thumb:hover { background: rgba(128,128,128,.65); }
#preview { font-size: calc(15px * var(--content-scale)); }
#preview .front-matter {
margin: 0 0 1.5em;
@@ -163,7 +168,7 @@ body.has-tabs { --chrome-top: 50px; }
.recent-name { color: #555; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.recent-path { color: #aaa; font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tabbar {
display: none; position: sticky; top: 0; z-index: 110; height: 40px;
display: none; flex: none; position: sticky; top: 0; z-index: 110; height: 40px;
box-sizing: border-box; align-items: stretch; gap: 4px; padding: 4px 8px;
border-bottom: 1px solid #e6e6e6; background: rgba(248,248,248,0.96);
backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
@@ -430,14 +435,29 @@ body.editing.cm-active #cm-editor { display: block; height: 100%; padding: 10px
@media print {
#cm-editor { display: none !important; }
html, body { height: auto !important; overflow: visible !important; display: block !important; }
#app { flex: none !important; height: auto !important; min-height: 0 !important; overflow: visible !important; scrollbar-gutter: auto; }
}
/* Edit-mode find/replace bar: same look as the preview findbar. */
html.cm-editing { overflow: hidden; scrollbar-gutter: auto; }
#edit-findbar { top: calc(var(--chrome-top) + 8px); z-index: 130; }
#edit-findbar { top: calc(var(--chrome-top) + 8px); z-index: 130; flex-wrap: wrap; }
body.edit-finding #edit-findbar { display: flex; }
#edit-findbar .edit-replace-group { display: flex; align-items: center; gap: 6px; }
#edit-findbar .edit-replace-group { display: flex; flex: 1 0 100%; align-items: center; gap: 6px; justify-content: flex-start; border-top: 1px solid rgba(0,0,0,.08); padding-top: 6px; margin-top: 2px; }
#edit-findbar .edit-replace-group[hidden] { display: none; }
#edit-findbar #edit-replace-input { width: 160px; }
#edit-findbar button.edit-replace-btn { width: auto; min-width: 54px; padding: 0 8px; font-size: 12px; }
#cm-editor .cm-edit-find-hit { background: rgba(255, 213, 0, .38); }
/* Use the same thin scrollbar inside CodeMirror as the preview container. */
#cm-editor .CodeMirror-vscrollbar::-webkit-scrollbar { width: 10px; }
#cm-editor .CodeMirror-vscrollbar::-webkit-scrollbar-track { background: transparent; }
#cm-editor .CodeMirror-vscrollbar::-webkit-scrollbar-thumb { background: rgba(128,128,128,.45); border-radius: 6px; }
#cm-editor .CodeMirror-vscrollbar::-webkit-scrollbar-thumb:hover { background: rgba(128,128,128,.65); }
@media (prefers-color-scheme: dark) {
#app::-webkit-scrollbar-thumb { background: rgba(200,200,200,.35); }
#app::-webkit-scrollbar-thumb:hover { background: rgba(200,200,200,.55); }
#cm-editor .CodeMirror-vscrollbar::-webkit-scrollbar-thumb { background: rgba(200,200,200,.35); }
#cm-editor .CodeMirror-vscrollbar::-webkit-scrollbar-thumb:hover { background: rgba(200,200,200,.55); }
#edit-findbar .edit-replace-group { border-top-color: rgba(255,255,255,.12); }
}

View File

@@ -41,6 +41,7 @@
var tabDrag = null;
var suppressTabClickUntil = 0;
var ta = document.getElementById('editor');
var appEl = document.getElementById('app');
var dirty = false;
var activeTabId = 0;
var tabCount = 0;
@@ -137,11 +138,13 @@
if (cm && inEdit()) {
var info = cm.getScrollInfo();
var cmMax = info.height - info.clientHeight;
return cmMax > 0 ? Math.min(1, Math.max(0, info.top / cmMax)) : 0;
if (cmMax <= 0) return 0;
var line = cm.lineAtHeight(info.top, 'local');
var lineTop = cm.heightAtLine(line, 'local');
return Math.min(1, Math.max(0, lineTop / cmMax));
}
var root = document.documentElement;
var max = Math.max(root.scrollHeight - window.innerHeight, 0);
var y = window.scrollY || root.scrollTop || 0;
var max = Math.max(appEl.scrollHeight - appEl.clientHeight, 0);
var y = appEl.scrollTop || 0;
return max > 0 ? Math.min(1, Math.max(0, y / max)) : 0;
}
// A newer restore supersedes any callback the previous one left behind.
@@ -155,6 +158,9 @@
var info = cm.getScrollInfo();
var cmMax = Math.max(info.height - info.clientHeight, 0);
cm.scrollTo(null, cmMax * progress);
// Land on a line boundary so repeated switches do not drift.
var topLine = cm.lineAtHeight(cm.getScrollInfo().top, 'local');
cm.scrollTo(null, cm.heightAtLine(topLine, 'local'));
};
requestAnimationFrame(function() {
if (cmToken !== scrollRestoreToken) return;
@@ -169,8 +175,8 @@
var token = ++scrollRestoreToken;
function restore() {
if (token !== scrollRestoreToken) return;
var max = Math.max(document.documentElement.scrollHeight - window.innerHeight, 0);
window.scrollTo(window.scrollX || 0, max * progress);
var max = Math.max(appEl.scrollHeight - appEl.clientHeight, 0);
appEl.scrollTop = max * progress;
}
restore();
requestAnimationFrame(function() {
@@ -193,7 +199,7 @@
}
}, 200);
}
window.addEventListener('scroll', reportScroll, { passive: true });
appEl.addEventListener('scroll', reportScroll, { passive: true });
applyZoom(100, false);
window.__mdPreviewSetZoom = function(percent) { applyZoom(percent, false); };
updateDocumentStats(ed.value());
@@ -492,12 +498,11 @@
// scrollbar; avoids the double-scrollbar you see if textarea keeps its
// own internal scroll.
function autoResize() {
var x = window.scrollX || document.documentElement.scrollLeft || 0;
var y = window.scrollY || document.documentElement.scrollTop || 0;
var y = appEl.scrollTop;
ta.style.height = 'auto';
var h = Math.max(ta.scrollHeight, window.innerHeight);
var h = Math.max(ta.scrollHeight, appEl.clientHeight);
ta.style.height = h + 'px';
window.scrollTo(x, y);
appEl.scrollTop = y;
}
// ---- Source editor commands -------------------------------------------------
function editorLineStart(value, pos) {
@@ -752,35 +757,37 @@
};
}
function markdownFoldRange(instance, line) {
var text = instance.getLine(line);
// CodeMirror passes a Pos here; accept a plain line number too.
var lineNo = typeof line === 'number' ? line : line.line;
var text = instance.getLine(lineNo);
if (!text) return null;
var fence = /^\s*(```+|~~~+)/.exec(text);
if (fence) {
var marker = fence[1];
var fenceEnd = -1;
for (var f = line + 1; f <= instance.lastLine(); f += 1) {
for (var f = lineNo + 1; f <= instance.lastLine(); f += 1) {
var fenceLine = instance.getLine(f) || '';
if (/^\s*(```+|~~~+)\s*$/.test(fenceLine) && fenceLine.indexOf(marker.charAt(0)) >= 0) {
fenceEnd = f;
break;
}
}
if (fenceEnd > line + 1) {
return { from: CodeMirror.Pos(line + 1, 0), to: CodeMirror.Pos(fenceEnd - 1, instance.getLine(fenceEnd - 1).length) };
if (fenceEnd > lineNo + 1) {
return { from: CodeMirror.Pos(lineNo + 1, 0), to: CodeMirror.Pos(fenceEnd - 1, instance.getLine(fenceEnd - 1).length) };
}
return null;
}
var heading = /^(#{1,6})\s/.exec(text);
var level = heading ? heading[1].length : 0;
if (!heading) {
var underline = instance.getLine(line + 1) || '';
var underline = instance.getLine(lineNo + 1) || '';
if (text.trim() && /^(=+|-+)\s*$/.test(underline)) {
level = underline.charAt(0) === '=' ? 1 : 2;
}
}
if (!level) return null;
var end = line;
for (var i = line + 1; i <= instance.lastLine(); i += 1) {
var endLine = lineNo;
for (var i = lineNo + 1; i <= instance.lastLine(); i += 1) {
var bodyLine = instance.getLine(i) || '';
var nextHeading = /^(#{1,6})\s/.exec(bodyLine);
var nextLevel = nextHeading ? nextHeading[1].length : 0;
@@ -791,10 +798,10 @@
}
}
if (nextLevel && nextLevel <= level) break;
if (bodyLine.trim()) end = i;
if (bodyLine.trim()) endLine = i;
}
if (end <= line) return null;
return { from: CodeMirror.Pos(line + 1, 0), to: CodeMirror.Pos(end, instance.getLine(end).length) };
if (endLine <= lineNo) return null;
return { from: CodeMirror.Pos(lineNo + 1, 0), to: CodeMirror.Pos(endLine, instance.getLine(endLine).length) };
}
// ---- Edit-mode find/replace ----------------------------------------------
function editFindOverlayFor(query) {
@@ -1020,7 +1027,7 @@
drag.element.classList.remove('dragging');
});
window.addEventListener('resize', hideTabMenu);
window.addEventListener('scroll', hideTabMenu, true);
appEl.addEventListener('scroll', hideTabMenu, true);
window.addEventListener('blur', hideTabMenu);
document.addEventListener('click', function(e) {
if (Date.now() < suppressTabClickUntil) {
@@ -1321,7 +1328,7 @@
ed.setValue('');
updateDocumentStats('');
setDirty(false);
window.scrollTo(0, 0);
appEl.scrollTop = 0;
};
window.__setMissing = function(previewHtml) {
if (editFinding) closeEditFind();
@@ -1336,7 +1343,7 @@
ed.setValue('');
updateDocumentStats('');
setDirty(false);
window.scrollTo(0, 0);
appEl.scrollTop = 0;
};
// Defer hljs parse + initial highlight to idle time.

View File

@@ -278,6 +278,10 @@ mod tests {
assert!(page.contains("cm.getSearchCursor(query"));
assert!(page.contains("document.documentElement.classList.toggle('cm-editing'"));
assert!(page.contains("minFoldSize: 0"));
assert!(page.contains("var lineNo = typeof line === 'number'"));
assert!(page.contains("appEl.addEventListener('scroll', reportScroll"));
assert!(page.contains("cm.lineAtHeight(info.top, 'local')"));
assert!(page.contains("flex: 1 0 100%"));
// Escape closes the find bar; it must not leave edit mode anymore.
assert!(!page.contains("if (e.key === 'Escape' && inEdit()) { leaveEdit(); }"));
assert!(!page.contains("'Esc': function() { leaveEdit(); }"));