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

@@ -448,8 +448,8 @@ body.edit-finding #edit-findbar { display: flex; flex-direction: column; align-i
#edit-findbar .edit-replace-row { display: flex; align-items: center; gap: 6px; width: 100%; border-top: 1px solid rgba(0,0,0,.08); padding-top: 6px; }
#edit-findbar .edit-replace-row[hidden] { display: none; }
#edit-findbar #edit-replace-input { flex: 1 1 auto; width: auto; min-width: 0; }
#edit-findbar button.edit-replace-btn { width: auto; height: 28px; min-width: 64px; padding: 0 10px; border: 1px solid #e2e2e2; border-radius: 7px; background: #f2f2f2; color: #333; font-size: 12px; }
#edit-findbar button.edit-replace-btn:hover { background: #e7e7e7; color: #111; }
#edit-findbar button.edit-replace-btn { width: auto; height: 28px; min-width: 64px; padding: 0 10px; border: 1px solid #e6e6e6; border-radius: 7px; background: #ebebeb; color: #333; font-size: 12px; }
#edit-findbar button.edit-replace-btn:hover { background: #e0e0e0; color: #111; }
#cm-editor .cm-edit-find-hit { background: rgba(255, 213, 0, .38); }
/* Use the same thin scrollbar inside CodeMirror as the preview container. */

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');

View File

@@ -569,16 +569,18 @@ pub(crate) fn set_content_script(
flags: EnhanceFlags,
editing: bool,
scroll_ratio: f32,
tab_id: u64,
) -> String {
format!(
"if(window.__setContent)window.__setContent('{}', '{}', '{}', {}, {}, {}, {});",
"if(window.__setContent)window.__setContent('{}', '{}', '{}', {}, {}, {}, {}, {});",
escape_js(html),
escape_js(raw),
escape_js(base_href),
flags.math,
flags.mermaid,
editing,
scroll_ratio
scroll_ratio,
tab_id
)
}
@@ -618,6 +620,7 @@ fn render_active_document(
flags,
active.editing,
active.scroll_ratio,
active.id,
));
for script in build_enhancer_bootstrap(flags, *loaded_enhancers) {
let _ = webview.evaluate_script(&script);
@@ -1918,10 +1921,11 @@ mod tests {
},
true,
0.25,
5,
);
assert_eq!(
script,
"if(window.__setContent)window.__setContent('<p>x</p>', '# x', 'file:///D:/tmp/', true, false, true, 0.25);"
"if(window.__setContent)window.__setContent('<p>x</p>', '# x', 'file:///D:/tmp/', true, false, true, 0.25, 5);"
);
// The page must declare the same argument order; a mismatch renders a
@@ -1935,7 +1939,7 @@ mod tests {
true,
);
assert!(page.contains(
"window.__setContent = function(previewHtml, rawMd, baseHref, needsMath, needsMermaid, editing, scrollRatio)"
"window.__setContent = function(previewHtml, rawMd, baseHref, needsMath, needsMermaid, editing, scrollRatio, tabId)"
));
}

View File

@@ -280,7 +280,8 @@ mod tests {
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("cm.lineAtHeight(info.top + 1, 'local')"));
assert!(page.contains("function cmScrollToLine(line)"));
assert!(page.contains("width: min(560px, calc(100vw - 32px))"));
assert!(page.contains("edit-find-row"));
assert!(page.contains("widget: '⋯'"));