fix(editor): isolate new query tab position

This commit is contained in:
Eve authored and GitHub committed 2026-09-08 16:48:47 +08:00
1 parent 5c400a4498
commit e8b8e20eaa
2 files changed
+17 -10

No files matched your search

@@ -6709,8 +6709,10 @@ function activateTabDocument(prevTabId: string | undefined, tabId: string | unde
// beyond MAX_CACHED_TAB_STATES): restore the tab's saved cursor and scroll // beyond MAX_CACHED_TAB_STATES): restore the tab's saved cursor and scroll
// position exactly like the cached-state branch, otherwise the swapped-in // position exactly like the cached-state branch, otherwise the swapped-in
// document keeps whatever scroll offset the dispatch left behind (#8374). // document keeps whatever scroll offset the dispatch left behind (#8374).
restoreEditorSelection(); // A brand-new tab has no saved state, so reset it instead of falling back
restoreEditorViewport(); // to the previous tab's latest position (#8378).
restoreEditorSelection(props.initialSelection ?? { anchor: 0, head: 0 });
restoreEditorViewport(props.initialViewport ?? { scrollTop: 0, scrollLeft: 0 });
return; return;
} }
// setState swaps doc, selection, undo history and all fields at once, but it // setState swaps doc, selection, undo history and all fields at once, but it
@@ -7066,10 +7068,10 @@ function flushEditorSelection() {
if (latestSelection) emitEditorSelection(latestSelection); if (latestSelection) emitEditorSelection(latestSelection);
} }
function restoreEditorSelection() { function restoreEditorSelection(selection = props.initialSelection ?? latestSelection) {
const selection = normalizedEditorSelection(props.initialSelection ?? latestSelection, props.modelValue.length); const normalizedSelection = normalizedEditorSelection(selection, props.modelValue.length);
if (!view.value || !selection) return; if (!view.value || !normalizedSelection) return;
view.value.dispatch({ selection }); view.value.dispatch({ selection: normalizedSelection });
} }
function restoreEditorFocus() { function restoreEditorFocus() {
@@ -7108,8 +7110,7 @@ function flushEditorViewport() {
if (latestViewport) emitEditorViewport(latestViewport); if (latestViewport) emitEditorViewport(latestViewport);
} }
function restoreEditorViewport() { function restoreEditorViewport(viewport = props.initialViewport ?? latestViewport) {
const viewport = props.initialViewport ?? latestViewport;
if (!view.value || !viewport) return; if (!view.value || !viewport) return;
const restoreScroll = () => { const restoreScroll = () => {
if (!view.value) return; if (!view.value) return;
+8 -2
View File
@@ -23,7 +23,13 @@ function activateTabDocumentSource(): string {
test("uncached tab activation restores the saved cursor and scroll position", () => { test("uncached tab activation restores the saved cursor and scroll position", () => {
const source = activateTabDocumentSource(); const source = activateTabDocumentSource();
assert.match(source, /if \(!cached\) \{[\s\S]*?swapEditorDocument\(doc\);[\s\S]*?restoreEditorSelection\(\);[\s\S]*?restoreEditorViewport\(\);[\s\S]*?return;/, "the swapEditorDocument branch must restore selection and viewport before returning"); assert.match(source, /if \(!cached\) \{[\s\S]*?swapEditorDocument\(doc\);[\s\S]*?restoreEditorSelection\([^;]*\);[\s\S]*?restoreEditorViewport\([^;]*\);[\s\S]*?return;/, "the swapEditorDocument branch must restore selection and viewport before returning");
});
test("uncached tabs without saved state do not inherit the previous tab position", () => {
const source = activateTabDocumentSource();
assert.match(source, /restoreEditorSelection\(props\.initialSelection \?\? \{ anchor: 0, head: 0 \}\);/);
assert.match(source, /restoreEditorViewport\(props\.initialViewport \?\? \{ scrollTop: 0, scrollLeft: 0 \}\);/);
}); });
test("cached tab activation keeps restoring selection and viewport", () => { test("cached tab activation keeps restoring selection and viewport", () => {
@@ -34,5 +40,5 @@ test("cached tab activation keeps restoring selection and viewport", () => {
test("viewport restore prefers the per-tab saved viewport", () => { test("viewport restore prefers the per-tab saved viewport", () => {
const source = readFileSync(path.resolve("apps/desktop/src/components/editor/QueryEditor.vue"), "utf8"); const source = readFileSync(path.resolve("apps/desktop/src/components/editor/QueryEditor.vue"), "utf8");
assert.match(source, /function restoreEditorViewport\(\) \{[\s\S]*?const viewport = props\.initialViewport \?\? latestViewport;/); assert.match(source, /function restoreEditorViewport\(viewport = props\.initialViewport \?\? latestViewport\) \{/);
}); });