25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
fix(editor): isolate tab viewport and selection across switches
lxk955192eecb1ec3d
diff --git a/apps/desktop/src/components/editor/QueryEditor.vue b/apps/desktop/src/components/editor/QueryEditor.vueindex 4101a72328..c17310dc91 100644--- a/apps/desktop/src/components/editor/QueryEditor.vue+++ b/apps/desktop/src/components/editor/QueryEditor.vue@@ -2701,7 +2701,9 @@ function resumeQueryEditorBackgroundWork() {// the first Run does not pay pool creation or external-driver startup.warmActiveTabConnection();if (view.value) schedulePreviewContextRefresh(view.value);- restoreEditorSelection(undefined, !props.initialViewport);+ if (props.initialSelection !== undefined) {+ restoreEditorSelection(props.initialSelection, !props.initialViewport);+ }restoreEditorFocus();restoreEditorViewport();}diff --git a/apps/desktop/src/components/editor/__tests__/useQueryEditorDocumentState.spec.ts b/apps/desktop/src/components/editor/__tests__/useQueryEditorDocumentState.spec.tsindex c244b3fa29..513e82e415 100644--- a/apps/desktop/src/components/editor/__tests__/useQueryEditorDocumentState.spec.ts+++ b/apps/desktop/src/components/editor/__tests__/useQueryEditorDocumentState.spec.ts@@ -132,6 +132,28 @@ describe("QueryEditor document and viewport ownership", () => {expect(currentView.scrollDOM.scrollLeft).toBe(0);});+ it("does not synchronize selection or viewport between cached tabs when switching", async () => {+ const { currentView, activate } = createHarness();+ const docA = "SELECT 1;\n".repeat(100);+ const docB = "SELECT 2;\n".repeat(100);++ await activate("tab-a", docA);+ expect(currentView.state.selection.main.head).toBe(0);+ expect(currentView.scrollDOM.scrollTop).toBe(0);++ await activate("tab-b", docB);+ currentView.dispatch({ selection: { anchor: 400, head: 400 } });+ currentView.scrollDOM.scrollTop = 500;++ await activate("tab-a", docA);+ expect(currentView.state.selection.main.head).toBe(0);+ expect(currentView.scrollDOM.scrollTop).toBe(0);++ await activate("tab-b", docB);+ expect(currentView.state.selection.main.head).toBe(400);+ expect(currentView.scrollDOM.scrollTop).toBe(500);+ });+it("flushes a deferred viewport with its outgoing tab owner and deduplicates emissions", async () => {const { state, currentView, activate, emit } = createHarness();currentView.scrollDOM.scrollTop = 240;diff --git a/apps/desktop/src/components/editor/useQueryEditorDocumentState.ts b/apps/desktop/src/components/editor/useQueryEditorDocumentState.tsindex cc50e7ac4e..f5e287f025 100644--- a/apps/desktop/src/components/editor/useQueryEditorDocumentState.ts+++ b/apps/desktop/src/components/editor/useQueryEditorDocumentState.ts@@ -78,7 +78,11 @@ export function useQueryEditorDocumentState(options: QueryEditorDocumentStateOpt// transaction excluded from history, and the history extension is dropped and// re-added in two separate transactions (a compartment reconfigure alone keeps// the old field value) so the previous tab's edits cannot leak in.- const tabStateCache = new Map<string, import("@codemirror/state").EditorState>();+ interface CachedTabState {+ state: import("@codemirror/state").EditorState;+ viewport: { scrollTop: number; scrollLeft: number };+ }+ const tabStateCache = new Map<string, CachedTabState>();const MAX_CACHED_TAB_STATES = 16;function swapEditorDocument(doc: string) {@@ -102,13 +106,18 @@ export function useQueryEditorDocumentState(options: QueryEditorDocumentStateOpt// become the new tab's state. The event carries its original owner. A// before-tab-switch capture already flushed this editor while it was still// visible, so avoid reading the reset scroll position during the transition.- if (!tabSwitchStateCaptured) flushEditorViewport();+ if (!tabSwitchStateCaptured) {+ flushEditorViewport();+ flushEditorSelection();+ }viewportOwnerTabId = tabId;- latestViewport = props.initialViewport ?? { scrollTop: 0, scrollLeft: 0 };lastEmittedViewport = undefined;clearScheduledPreviewContextRefresh();if (prevTabId !== undefined) {- tabStateCache.set(prevTabId, currentView.state);+ tabStateCache.set(prevTabId, {+ state: currentView.state,+ viewport: readEditorViewport(currentView),+ });if (tabStateCache.size > MAX_CACHED_TAB_STATES) {const oldest = tabStateCache.keys().next();if (!oldest.done) tabStateCache.delete(oldest.value);@@ -123,8 +132,12 @@ export function useQueryEditorDocumentState(options: QueryEditorDocumentStateOpt// document keeps whatever scroll offset the dispatch left behind (#8374).// A brand-new tab has no saved state, so reset it instead of falling back// to the previous tab's latest position (#8378).- restoreEditorSelection(props.initialSelection ?? { anchor: 0, head: 0 }, !props.initialViewport);- restoreEditorViewport(props.initialViewport ?? { scrollTop: 0, scrollLeft: 0 });+ const initialSel = props.initialSelection ?? { anchor: 0, head: 0 };+ const initialVp = props.initialViewport ?? { scrollTop: 0, scrollLeft: 0 };+ latestSelection = initialSel;+ latestViewport = initialVp;+ restoreEditorSelection(initialSel, !props.initialViewport);+ restoreEditorViewport(initialVp);clearScheduledPreviewContextRefresh();syncContextMenuState(currentView);emit("previewChangesAvailable", !!previewContextSql.value);@@ -132,7 +145,7 @@ export function useQueryEditorDocumentState(options: QueryEditorDocumentStateOpt}// setState swaps doc, selection, undo history and all fields at once, but it// is not a transaction, so update-listener side effects are re-run manually.- currentView.setState(cached);+ currentView.setState(cached.state);// Compartments in the restored state may lag behind settings that changed// while another tab was active; re-sync them from current values.void applyEditorAppearance();@@ -148,8 +161,13 @@ export function useQueryEditorDocumentState(options: QueryEditorDocumentStateOpt}scheduleDocumentSearchUpdate();invalidateSemanticDiagnosticsForDocumentChange();- restoreEditorSelection(undefined, !props.initialViewport);- restoreEditorViewport();+ if (props.initialSelection !== undefined) {+ restoreEditorSelection(props.initialSelection, !props.initialViewport);+ }+ latestSelection = readEditorSelection(currentView);+ const targetViewport = props.initialViewport ?? cached.viewport;+ latestViewport = targetViewport;+ restoreEditorViewport(targetViewport);clearScheduledPreviewContextRefresh();syncContextMenuState(currentView);emit("previewChangesAvailable", !!previewContextSql.value);diff --git a/apps/desktop/src/components/layout/EditorGroupTabBar.vue b/apps/desktop/src/components/layout/EditorGroupTabBar.vueindex 6d3407e85a..514982348b 100644--- a/apps/desktop/src/components/layout/EditorGroupTabBar.vue+++ b/apps/desktop/src/components/layout/EditorGroupTabBar.vue@@ -1158,14 +1158,17 @@ function tabTitleStyle(tab: QueryTab): CSSProperties | undefined {return dirtyTabTitleStyle(isDirtyTab(tab));}+let lastDispatchedSwitchTabId: string | null = null;function dispatchBeforeTabSwitch(tabId: string) {- if (tabId === props.activeTabId) {+ if (tabId === props.activeTabId || tabId === lastDispatchedSwitchTabId) {return;}+ lastDispatchedSwitchTabId = tabId;window.dispatchEvent(new CustomEvent("dbx:before-tab-switch", { detail: { tabId, fromTabId: props.activeTabId } }));}function activateTab(tabId: string) {+ dispatchBeforeTabSwitch(tabId);emit("activate-tab", tabId);}@@ -1558,6 +1561,7 @@ const tabScrollBehavior = ref<ScrollBehavior>("smooth");watch(() => props.activeTabId,(tabId) => {+ lastDispatchedSwitchTabId = null;// A tab that just became active must be visible: reveal its group if collapsed.expandTabGroupForTab(tabId);nextTick(() => {