fix(editor): fold fence parity, search bar layout and per-tab scroll memory

This commit is contained in:
2026-11-06 10:03:23 +08:00
parent 2117551af7
commit 9f12b91a14
4 changed files with 65 additions and 24 deletions

View File

@@ -85,6 +85,7 @@
var cmDisabled = false;
var applyingRemote = false;
var pendingScrollRatio = 0;
var cmTabLines = {};
// All source-text access goes through this adapter. The CodeMirror
// implementation replaces the returned object; callers never touch ta.
var ed = createTextareaEditor(ta);
@@ -134,13 +135,27 @@
function changeZoom(delta) {
applyZoom(zoomPercent + delta, true);
}
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');
}
return line;
}
function cmRememberLine() {
if (!cm || !inEdit() || !activeTabId) return;
cmTabLines[activeTabId] = cmTopLine();
}
function currentScrollProgress() {
if (cm && inEdit()) {
var info = cm.getScrollInfo();
var cmMax = info.height - info.clientHeight;
if (cmMax <= 0) return 0;
var line = cm.lineAtHeight(info.top, 'local');
var lineTop = cm.heightAtLine(line, 'local');
var lineTop = cm.heightAtLine(cmTopLine(), 'local');
return Math.min(1, Math.max(0, lineTop / cmMax));
}
var max = Math.max(appEl.scrollHeight - appEl.clientHeight, 0);
@@ -149,18 +164,21 @@
}
// A newer restore supersedes any callback the previous one left behind.
var scrollRestoreToken = 0;
function restoreScrollProgress(progress) {
function restoreScrollProgress(progress, useRemembered) {
if (cm && inEdit()) {
pendingScrollRatio = progress;
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'));
return;
}
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'));
cm.scrollTo(null, cm.heightAtLine(cmTopLine(), 'local'));
};
requestAnimationFrame(function() {
if (cmToken !== scrollRestoreToken) return;
@@ -296,6 +314,7 @@
}
window.__mdPreviewSave = save;
function requestTabAction(action, id) {
cmRememberLine();
cancelPendingAutosave();
var message = 'tab-action:' + action + ':' + id + ':' + currentScrollProgress().toFixed(4);
if (dirty) {
@@ -678,7 +697,7 @@
lineWrapping: true,
foldGutter: { rangeFinder: markdownFoldRange, minFoldSize: 0 },
gutters: ['CodeMirror-linenumbers', 'CodeMirror-foldgutter'],
foldOptions: { rangeFinder: markdownFoldRange, minFoldSize: 0 },
foldOptions: { rangeFinder: markdownFoldRange, minFoldSize: 0, widget: '⋯' },
indentUnit: 2,
tabSize: 2,
indentWithTabs: false,
@@ -763,6 +782,16 @@
if (!text) return null;
var fence = /^\s*(```+|~~~+)/.exec(text);
if (fence) {
// Only an opening fence gets a fold marker. A closing fence is inside the
// block that started above it, so scanning up tells the two apart.
var openFence = null;
for (var b = 0; b < lineNo; b += 1) {
var priorFence = /^\s*(```+|~~~+)/.exec(instance.getLine(b) || '');
if (!priorFence) continue;
if (!openFence) openFence = priorFence[1].charAt(0);
else if (priorFence[1].charAt(0) === openFence) openFence = null;
}
if (openFence) return null;
var marker = fence[1];
var fenceEnd = -1;
for (var f = lineNo + 1; f <= instance.lastLine(); f += 1) {
@@ -960,6 +989,7 @@
if (window.ipc) window.ipc.postMessage('edit-mode:' + activeTabId + ':1');
}
function leaveEdit() {
cmRememberLine();
var progress = currentScrollProgress();
if (dirty) save();
setEditMode(false);
@@ -1300,6 +1330,7 @@
requestTabAction('activate', tab.getAttribute('data-tab-id'));
});
window.__setContent = function(previewHtml, rawMd, baseHref, needsMath, needsMermaid, editing, scrollRatio) {
cmRememberLine();
if (editFinding) closeEditFind();
document.body.classList.remove('home');
document.body.classList.remove('missing');
@@ -1313,7 +1344,7 @@
setDirty(false);
}
setEditMode(!!editing);
restoreScrollProgress(typeof scrollRatio === 'number' ? scrollRatio : 0);
restoreScrollProgress(typeof scrollRatio === 'number' ? scrollRatio : 0, true);
};
window.__setEmptyPreview = function(previewHtml) {
if (editFinding) closeEditFind();