fix(editor): stable per-tab line restore and fold closing fences

This commit is contained in:
2026-11-06 10:12:18 +08:00
parent 9f12b91a14
commit a206ce8450
4 changed files with 38 additions and 19 deletions

View File

@@ -138,14 +138,19 @@
function cmTopLine() {
if (!cm) return 0;
var info = cm.getScrollInfo();
var line = cm.lineAtHeight(info.top, 'local');
var top = cm.heightAtLine(line, 'local');
while (line > 0 && top > info.top + 0.5) {
line -= 1;
top = cm.heightAtLine(line, 'local');
}
// +1px lands inside the first visible line instead of on its exact top
// boundary, where CM can report the previous line and drift one line per
// tab switch.
var line = cm.lineAtHeight(info.top + 1, 'local');
if (line > cm.lastLine()) line = cm.lastLine();
return line;
}
function cmScrollToLine(line) {
if (!cm) return;
if (line < 0) line = 0;
if (line > cm.lastLine()) line = cm.lastLine();
cm.scrollTo(null, Math.ceil(cm.heightAtLine(line, 'local')));
}
function cmRememberLine() {
if (!cm || !inEdit() || !activeTabId) return;
cmTabLines[activeTabId] = cmTopLine();
@@ -167,18 +172,20 @@
function restoreScrollProgress(progress, useRemembered) {
if (cm && inEdit()) {
pendingScrollRatio = progress;
var immediateLine = useRemembered ? cmTabLines[activeTabId] : undefined;
if (typeof immediateLine === 'number') cmScrollToLine(immediateLine);
var cmToken = ++scrollRestoreToken;
var applyCm = function() {
if (cmToken !== scrollRestoreToken) return;
var remembered = useRemembered ? cmTabLines[activeTabId] : undefined;
if (typeof remembered === 'number') {
cm.scrollTo(null, cm.heightAtLine(remembered, 'local'));
cmScrollToLine(remembered);
return;
}
var info = cm.getScrollInfo();
var cmMax = Math.max(info.height - info.clientHeight, 0);
cm.scrollTo(null, cmMax * progress);
cm.scrollTo(null, cm.heightAtLine(cmTopLine(), 'local'));
cmScrollToLine(cmTopLine());
};
requestAnimationFrame(function() {
if (cmToken !== scrollRestoreToken) return;
@@ -801,8 +808,8 @@
break;
}
}
if (fenceEnd > lineNo + 1) {
return { from: CodeMirror.Pos(lineNo + 1, 0), to: CodeMirror.Pos(fenceEnd - 1, instance.getLine(fenceEnd - 1).length) };
if (fenceEnd > lineNo) {
return { from: CodeMirror.Pos(lineNo + 1, 0), to: CodeMirror.Pos(fenceEnd, instance.getLine(fenceEnd).length) };
}
return null;
}
@@ -1282,6 +1289,7 @@
};
window.__setTabs = function(tabs) {
hideTabMenu();
var previousActiveTabId = activeTabId;
tabs = Array.isArray(tabs) ? tabs : [];
tabCount = tabs.length;
if (tabMenuNewWindow) tabMenuNewWindow.disabled = tabCount <= 1;
@@ -1321,6 +1329,10 @@
tabsEl.appendChild(item);
if (tab.active) requestAnimationFrame(function() { item.scrollIntoView({ block: 'nearest', inline: 'nearest' }); });
});
if (activeTabId && activeTabId !== previousActiveTabId && cm && inEdit() &&
typeof cmTabLines[activeTabId] === 'number') {
cmScrollToLine(cmTabLines[activeTabId]);
}
};
tabsEl.addEventListener('keydown', function(e) {
if (e.key !== 'Enter' && e.key !== ' ') return;
@@ -1329,8 +1341,10 @@
e.preventDefault();
requestTabAction('activate', tab.getAttribute('data-tab-id'));
});
window.__setContent = function(previewHtml, rawMd, baseHref, needsMath, needsMermaid, editing, scrollRatio) {
cmRememberLine();
window.__setContent = function(previewHtml, rawMd, baseHref, needsMath, needsMermaid, editing, scrollRatio, tabId) {
var previousTabId = activeTabId;
if (cm && inEdit() && previousTabId) cmTabLines[previousTabId] = cmTopLine();
if (typeof tabId === 'number' && tabId) activeTabId = tabId;
if (editFinding) closeEditFind();
document.body.classList.remove('home');
document.body.classList.remove('missing');