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

@@ -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.