Skip to content

armgit / dbx

Public Imported from GitHub
0

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。

fix(sidebar): sync selection and reveal loaded ancestors when auto-selecting active tab (#13640) (#13706)

lxk955ecc6afea35f8
diff --git a/apps/desktop/src/components/sidebar/ConnectionTree.vue b/apps/desktop/src/components/sidebar/ConnectionTree.vue
index b5f799701a..b4058c61d3 100644
--- a/apps/desktop/src/components/sidebar/ConnectionTree.vue
+++ b/apps/desktop/src/components/sidebar/ConnectionTree.vue
@@ -1746,6 +1746,28 @@ async function startRenamingConnectionNode(connectionId: string) {
store.selectedTreeNodeId = connectionId;
}
+function expandSidebarNodePath(nodePath: TreeNode[]): void {
+ expandSidebarLocatePath(nodePath, store.canUseLoadedTreeNodeToggle);
+
+ // 表分组行同样是投影出的合成节点(不登记已加载子节点,上面的守卫会跳过),
+ // 折叠状态存在布局里,必须经布局 op 展开,否则下次投影又把它折叠回去。
+ for (const node of nodePath) {
+ if (node.type !== "table-vgroup" || !node.vgroupId) continue;
+ const group = store.tableVGroupLayoutFor(node)?.groups.find((current) => current.id === node.vgroupId);
+ if (group?.collapsed) store.toggleTableVGroupCollapsed(node, node.vgroupId);
+ }
+
+ // Connection groups never register loaded tree children, so the guard above
+ // skips them and a collapsed group keeps the target out of the visible flat
+ // tree. Reopen them through the layout op so the expansion is persisted and
+ // survives the next layout rebuild; flipping isExpanded directly would be
+ // reverted by that rebuild (issue #7387).
+ const collapsedGroupIds = nodePath.filter((node) => node.type === "connection-group" && !node.isExpanded).map((node) => node.id);
+ if (collapsedGroupIds.length > 0) {
+ store.expandConnectionGroups(collapsedGroupIds);
+ }
+}
+
async function locateActiveTabInSidebar() {
await locateTabInSidebar(activeTab.value, "smart");
}
@@ -1840,25 +1862,7 @@ async function locateTabInSidebarTarget(tab: QueryTab | undefined | null, align:
return;
}
- expandSidebarLocatePath(nodePath, store.canUseLoadedTreeNodeToggle);
-
- // 表分组行同样是投影出的合成节点(不登记已加载子节点,上面的守卫会跳过),
- // 折叠状态存在布局里,必须经布局 op 展开,否则下次投影又把它折叠回去。
- for (const node of nodePath) {
- if (node.type !== "table-vgroup" || !node.vgroupId) continue;
- const group = store.tableVGroupLayoutFor(node)?.groups.find((current) => current.id === node.vgroupId);
- if (group?.collapsed) store.toggleTableVGroupCollapsed(node, node.vgroupId);
- }
-
- // Connection groups never register loaded tree children, so the guard above
- // skips them and a collapsed group keeps the target out of the visible flat
- // tree. Reopen them through the layout op so the expansion is persisted and
- // survives the next layout rebuild; flipping isExpanded directly would be
- // reverted by that rebuild (issue #7387).
- const collapsedGroupIds = nodePath.filter((node) => node.type === "connection-group" && !node.isExpanded).map((node) => node.id);
- if (collapsedGroupIds.length > 0) {
- store.expandConnectionGroups(collapsedGroupIds);
- }
+ expandSidebarNodePath(nodePath);
await nextTick();
@@ -1868,6 +1872,7 @@ async function locateTabInSidebarTarget(tab: QueryTab | undefined | null, align:
store.selectedTreeNodeId = match.id;
store.selectedTreeNodeIds = [match.id];
store.treeSelectionAnchorId = match.id;
+ store.connectionMultiSelectActive = false;
await nextTick();
await scrollToSidebarNode(match.id, { align });
@@ -2506,10 +2511,29 @@ function currentTreeScroller(): HTMLElement | null {
async function selectActiveTabSidebarNode(options: { scroll: boolean }) {
if (!settingsStore.editorSettings.autoSelectActiveSidebarNode) return;
- const match = findSidebarNodeForActiveTab(activeTab.value, flatNodes.value);
+ const tab = activeTab.value;
+ if (!tab) return;
+
+ let match = findSidebarNodeForActiveTab(tab, flatNodes.value);
+ if (!match) {
+ const target = activeTabSidebarTarget(tab);
+ const nodePath = target ? findNodePathForTarget(target, store.treeNodes) : null;
+ if (nodePath && nodePath.length > 0) {
+ expandSidebarNodePath(nodePath);
+ await nextTick();
+ if (activeTab.value?.id !== tab.id) return;
+ match = findSidebarNodeForActiveTab(tab, flatNodes.value);
+ }
+ }
if (!match) return;
+ if (activeTab.value?.id !== tab.id) return;
- store.selectedTreeNodeId = match.id;
+ if (store.connectionMultiSelectActive || store.selectedTreeNodeId !== match.id || store.treeSelectionAnchorId !== match.id || store.selectedTreeNodeIds.length !== 1 || store.selectedTreeNodeIds[0] !== match.id) {
+ store.selectedTreeNodeId = match.id;
+ store.selectedTreeNodeIds = [match.id];
+ store.treeSelectionAnchorId = match.id;
+ store.connectionMultiSelectActive = false;
+ }
if (!options.scroll) return;
await nextTick();
diff --git a/packages/app-tests/sidebarActiveTabTarget.test.ts b/packages/app-tests/sidebarActiveTabTarget.test.ts
index 81f1ac58a4..44fee90e95 100644
--- a/packages/app-tests/sidebarActiveTabTarget.test.ts
+++ b/packages/app-tests/sidebarActiveTabTarget.test.ts
@@ -1,6 +1,6 @@
import { strict as assert } from "node:assert";
import { test } from "vitest";
-import { activeTabSidebarTarget, findNodePathForTarget, findSidebarConnectionNode, findSidebarNodeForActiveTab, findSidebarNodeForTarget, scrollTopForSidebarNode, shouldScrollActiveSidebarSelection } from "../../apps/desktop/src/lib/sidebar/sidebarActiveTabTarget.ts";
+import { activeTabSidebarTarget, expandSidebarLocatePath, findNodePathForTarget, findSidebarConnectionNode, findSidebarNodeForActiveTab, findSidebarNodeForTarget, scrollTopForSidebarNode, shouldScrollActiveSidebarSelection } from "../../apps/desktop/src/lib/sidebar/sidebarActiveTabTarget.ts";
import { buildTreeNodesFromLayout, expandGroups } from "../../apps/desktop/src/lib/sidebar/sidebarLayout.ts";
import { flattenTree } from "../../apps/desktop/src/composables/useFlatTree.ts";
import type { FlatTreeNode } from "../../apps/desktop/src/composables/useFlatTree.ts";
@@ -636,3 +636,108 @@ test("locate under collapsed connection groups resolves the path, expands the gr
const rebuiltTree = buildTreeNodesFromLayout(expandedLayout, connections, new Set());
assert.equal(findSidebarNodeForTarget(target, flattenTree(rebuiltTree))?.id, "conn-grouped");
});
+
+test("auto-select reveals target from collapsed loaded ancestors when tab switches", () => {
+ const tree: TreeNode[] = [
+ {
+ id: "conn-1",
+ label: "MySQL Local",
+ type: "connection",
+ connectionId: "conn-1",
+ isExpanded: false,
+ children: [
+ {
+ id: "conn-1:app",
+ label: "app",
+ type: "database",
+ connectionId: "conn-1",
+ database: "app",
+ isExpanded: false,
+ children: [
+ {
+ id: "conn-1:app:__tables",
+ label: "Tables",
+ type: "group-tables",
+ connectionId: "conn-1",
+ database: "app",
+ isExpanded: false,
+ children: [
+ {
+ id: "conn-1:app:__tables:users",
+ label: "users",
+ type: "table",
+ connectionId: "conn-1",
+ database: "app",
+ isExpanded: false,
+ children: [],
+ },
+ ],
+ },
+ ],
+ },
+ ],
+ },
+ ];
+
+ const tab: QueryTab = {
+ id: "tab-users",
+ title: "users",
+ mode: "data",
+ connectionId: "conn-1",
+ database: "app",
+ sql: "SELECT * FROM users",
+ };
+
+ // Initially, because ancestors are collapsed, flatNodes doesn't contain the table.
+ assert.equal(findSidebarNodeForActiveTab(tab, flattenTree(tree)), null);
+
+ // Resolving path and expanding loaded ancestors reveals it in the flat tree.
+ const target = activeTabSidebarTarget(tab);
+ assert.ok(target);
+ const nodePath = findNodePathForTarget(target, tree);
+ assert.ok(nodePath);
+ assert.deepEqual(
+ nodePath.map((n) => n.id),
+ ["conn-1", "conn-1:app", "conn-1:app:__tables", "conn-1:app:__tables:users"],
+ );
+
+ expandSidebarLocatePath(nodePath, () => true);
+
+ // Now flattened tree reveals the table node
+ const match = findSidebarNodeForActiveTab(tab, flattenTree(tree));
+ assert.ok(match);
+ assert.equal(match.id, "conn-1:app:__tables:users");
+});
+
+test("sidebar selection synchronization replaces previous selection set and multiSelect flag", () => {
+ // Simulates TreeItem rowSelected computation:
+ // rowSelected = selectedTreeNodeIds.length > 0 ? multiSelected : selected
+ const store = {
+ selectedTreeNodeId: "previous-db-id" as string | null,
+ selectedTreeNodeIds: ["previous-db-id"],
+ treeSelectionAnchorId: "previous-db-id" as string | null,
+ connectionMultiSelectActive: true,
+ };
+
+ const isRowSelected = (nodeId: string) => {
+ const selected = store.selectedTreeNodeId === nodeId;
+ const multiSelected = store.selectedTreeNodeIds.includes(nodeId);
+ return store.selectedTreeNodeIds.length > 0 ? multiSelected : selected;
+ };
+
+ // Before fix: only selectedTreeNodeId was set, selectedTreeNodeIds stayed ["previous-db-id"].
+ // As a result, isRowSelected("table-users") was FALSE, and "previous-db-id" was TRUE!
+ assert.equal(isRowSelected("previous-db-id"), true);
+ assert.equal(isRowSelected("conn-1:app:__tables:users"), false);
+
+ // With the fix: auto-select synchronizes selectedTreeNodeIds, treeSelectionAnchorId, and clears multiSelect.
+ const matchId = "conn-1:app:__tables:users";
+ store.selectedTreeNodeId = matchId;
+ store.selectedTreeNodeIds = [matchId];
+ store.treeSelectionAnchorId = matchId;
+ store.connectionMultiSelectActive = false;
+
+ assert.equal(isRowSelected("previous-db-id"), false);
+ assert.equal(isRowSelected("conn-1:app:__tables:users"), true);
+ assert.equal(store.connectionMultiSelectActive, false);
+});