fix(editor): stable per-tab line restore and fold closing fences
This commit is contained in:
@@ -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 { 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-row[hidden] { display: none; }
|
||||||
#edit-findbar #edit-replace-input { flex: 1 1 auto; width: auto; min-width: 0; }
|
#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 { 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: #e7e7e7; color: #111; }
|
#edit-findbar button.edit-replace-btn:hover { background: #e0e0e0; color: #111; }
|
||||||
#cm-editor .cm-edit-find-hit { background: rgba(255, 213, 0, .38); }
|
#cm-editor .cm-edit-find-hit { background: rgba(255, 213, 0, .38); }
|
||||||
|
|
||||||
/* Use the same thin scrollbar inside CodeMirror as the preview container. */
|
/* Use the same thin scrollbar inside CodeMirror as the preview container. */
|
||||||
|
|||||||
@@ -138,14 +138,19 @@
|
|||||||
function cmTopLine() {
|
function cmTopLine() {
|
||||||
if (!cm) return 0;
|
if (!cm) return 0;
|
||||||
var info = cm.getScrollInfo();
|
var info = cm.getScrollInfo();
|
||||||
var line = cm.lineAtHeight(info.top, 'local');
|
// +1px lands inside the first visible line instead of on its exact top
|
||||||
var top = cm.heightAtLine(line, 'local');
|
// boundary, where CM can report the previous line and drift one line per
|
||||||
while (line > 0 && top > info.top + 0.5) {
|
// tab switch.
|
||||||
line -= 1;
|
var line = cm.lineAtHeight(info.top + 1, 'local');
|
||||||
top = cm.heightAtLine(line, 'local');
|
if (line > cm.lastLine()) line = cm.lastLine();
|
||||||
}
|
|
||||||
return line;
|
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() {
|
function cmRememberLine() {
|
||||||
if (!cm || !inEdit() || !activeTabId) return;
|
if (!cm || !inEdit() || !activeTabId) return;
|
||||||
cmTabLines[activeTabId] = cmTopLine();
|
cmTabLines[activeTabId] = cmTopLine();
|
||||||
@@ -167,18 +172,20 @@
|
|||||||
function restoreScrollProgress(progress, useRemembered) {
|
function restoreScrollProgress(progress, useRemembered) {
|
||||||
if (cm && inEdit()) {
|
if (cm && inEdit()) {
|
||||||
pendingScrollRatio = progress;
|
pendingScrollRatio = progress;
|
||||||
|
var immediateLine = useRemembered ? cmTabLines[activeTabId] : undefined;
|
||||||
|
if (typeof immediateLine === 'number') cmScrollToLine(immediateLine);
|
||||||
var cmToken = ++scrollRestoreToken;
|
var cmToken = ++scrollRestoreToken;
|
||||||
var applyCm = function() {
|
var applyCm = function() {
|
||||||
if (cmToken !== scrollRestoreToken) return;
|
if (cmToken !== scrollRestoreToken) return;
|
||||||
var remembered = useRemembered ? cmTabLines[activeTabId] : undefined;
|
var remembered = useRemembered ? cmTabLines[activeTabId] : undefined;
|
||||||
if (typeof remembered === 'number') {
|
if (typeof remembered === 'number') {
|
||||||
cm.scrollTo(null, cm.heightAtLine(remembered, 'local'));
|
cmScrollToLine(remembered);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var info = cm.getScrollInfo();
|
var info = cm.getScrollInfo();
|
||||||
var cmMax = Math.max(info.height - info.clientHeight, 0);
|
var cmMax = Math.max(info.height - info.clientHeight, 0);
|
||||||
cm.scrollTo(null, cmMax * progress);
|
cm.scrollTo(null, cmMax * progress);
|
||||||
cm.scrollTo(null, cm.heightAtLine(cmTopLine(), 'local'));
|
cmScrollToLine(cmTopLine());
|
||||||
};
|
};
|
||||||
requestAnimationFrame(function() {
|
requestAnimationFrame(function() {
|
||||||
if (cmToken !== scrollRestoreToken) return;
|
if (cmToken !== scrollRestoreToken) return;
|
||||||
@@ -801,8 +808,8 @@
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (fenceEnd > lineNo + 1) {
|
if (fenceEnd > lineNo) {
|
||||||
return { from: CodeMirror.Pos(lineNo + 1, 0), to: CodeMirror.Pos(fenceEnd - 1, instance.getLine(fenceEnd - 1).length) };
|
return { from: CodeMirror.Pos(lineNo + 1, 0), to: CodeMirror.Pos(fenceEnd, instance.getLine(fenceEnd).length) };
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -1282,6 +1289,7 @@
|
|||||||
};
|
};
|
||||||
window.__setTabs = function(tabs) {
|
window.__setTabs = function(tabs) {
|
||||||
hideTabMenu();
|
hideTabMenu();
|
||||||
|
var previousActiveTabId = activeTabId;
|
||||||
tabs = Array.isArray(tabs) ? tabs : [];
|
tabs = Array.isArray(tabs) ? tabs : [];
|
||||||
tabCount = tabs.length;
|
tabCount = tabs.length;
|
||||||
if (tabMenuNewWindow) tabMenuNewWindow.disabled = tabCount <= 1;
|
if (tabMenuNewWindow) tabMenuNewWindow.disabled = tabCount <= 1;
|
||||||
@@ -1321,6 +1329,10 @@
|
|||||||
tabsEl.appendChild(item);
|
tabsEl.appendChild(item);
|
||||||
if (tab.active) requestAnimationFrame(function() { item.scrollIntoView({ block: 'nearest', inline: 'nearest' }); });
|
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) {
|
tabsEl.addEventListener('keydown', function(e) {
|
||||||
if (e.key !== 'Enter' && e.key !== ' ') return;
|
if (e.key !== 'Enter' && e.key !== ' ') return;
|
||||||
@@ -1329,8 +1341,10 @@
|
|||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
requestTabAction('activate', tab.getAttribute('data-tab-id'));
|
requestTabAction('activate', tab.getAttribute('data-tab-id'));
|
||||||
});
|
});
|
||||||
window.__setContent = function(previewHtml, rawMd, baseHref, needsMath, needsMermaid, editing, scrollRatio) {
|
window.__setContent = function(previewHtml, rawMd, baseHref, needsMath, needsMermaid, editing, scrollRatio, tabId) {
|
||||||
cmRememberLine();
|
var previousTabId = activeTabId;
|
||||||
|
if (cm && inEdit() && previousTabId) cmTabLines[previousTabId] = cmTopLine();
|
||||||
|
if (typeof tabId === 'number' && tabId) activeTabId = tabId;
|
||||||
if (editFinding) closeEditFind();
|
if (editFinding) closeEditFind();
|
||||||
document.body.classList.remove('home');
|
document.body.classList.remove('home');
|
||||||
document.body.classList.remove('missing');
|
document.body.classList.remove('missing');
|
||||||
|
|||||||
12
src/app.rs
12
src/app.rs
@@ -569,16 +569,18 @@ pub(crate) fn set_content_script(
|
|||||||
flags: EnhanceFlags,
|
flags: EnhanceFlags,
|
||||||
editing: bool,
|
editing: bool,
|
||||||
scroll_ratio: f32,
|
scroll_ratio: f32,
|
||||||
|
tab_id: u64,
|
||||||
) -> String {
|
) -> String {
|
||||||
format!(
|
format!(
|
||||||
"if(window.__setContent)window.__setContent('{}', '{}', '{}', {}, {}, {}, {});",
|
"if(window.__setContent)window.__setContent('{}', '{}', '{}', {}, {}, {}, {}, {});",
|
||||||
escape_js(html),
|
escape_js(html),
|
||||||
escape_js(raw),
|
escape_js(raw),
|
||||||
escape_js(base_href),
|
escape_js(base_href),
|
||||||
flags.math,
|
flags.math,
|
||||||
flags.mermaid,
|
flags.mermaid,
|
||||||
editing,
|
editing,
|
||||||
scroll_ratio
|
scroll_ratio,
|
||||||
|
tab_id
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -618,6 +620,7 @@ fn render_active_document(
|
|||||||
flags,
|
flags,
|
||||||
active.editing,
|
active.editing,
|
||||||
active.scroll_ratio,
|
active.scroll_ratio,
|
||||||
|
active.id,
|
||||||
));
|
));
|
||||||
for script in build_enhancer_bootstrap(flags, *loaded_enhancers) {
|
for script in build_enhancer_bootstrap(flags, *loaded_enhancers) {
|
||||||
let _ = webview.evaluate_script(&script);
|
let _ = webview.evaluate_script(&script);
|
||||||
@@ -1918,10 +1921,11 @@ mod tests {
|
|||||||
},
|
},
|
||||||
true,
|
true,
|
||||||
0.25,
|
0.25,
|
||||||
|
5,
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
script,
|
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
|
// The page must declare the same argument order; a mismatch renders a
|
||||||
@@ -1935,7 +1939,7 @@ mod tests {
|
|||||||
true,
|
true,
|
||||||
);
|
);
|
||||||
assert!(page.contains(
|
assert!(page.contains(
|
||||||
"window.__setContent = function(previewHtml, rawMd, baseHref, needsMath, needsMermaid, editing, scrollRatio)"
|
"window.__setContent = function(previewHtml, rawMd, baseHref, needsMath, needsMermaid, editing, scrollRatio, tabId)"
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -280,7 +280,8 @@ mod tests {
|
|||||||
assert!(page.contains("minFoldSize: 0"));
|
assert!(page.contains("minFoldSize: 0"));
|
||||||
assert!(page.contains("var lineNo = typeof line === 'number'"));
|
assert!(page.contains("var lineNo = typeof line === 'number'"));
|
||||||
assert!(page.contains("appEl.addEventListener('scroll', reportScroll"));
|
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("width: min(560px, calc(100vw - 32px))"));
|
||||||
assert!(page.contains("edit-find-row"));
|
assert!(page.contains("edit-find-row"));
|
||||||
assert!(page.contains("widget: '⋯'"));
|
assert!(page.contains("widget: '⋯'"));
|
||||||
|
|||||||
Reference in New Issue
Block a user