fix(sidebar): locate active table reliably

This commit is contained in:
auth0 authored and GitHub committed 2026-07-08 20:21:13 +08:00
1 parent 33b4fc1dac
commit 07482557e6
3 files changed
+221 -14

No files matched your search

@@ -15,7 +15,7 @@ import { connectionPasteTargetGroupId, selectedConnectionClipboardNodes, selecte
import { isEditableSidebarTypeSearchTarget, sidebarTypeSearchNextQuery } from "@/lib/sidebar/sidebarTypeSearch"; import { isEditableSidebarTypeSearchTarget, sidebarTypeSearchNextQuery } from "@/lib/sidebar/sidebarTypeSearch";
import { usesTreeSchemaMode } from "@/lib/database/databaseFeatureSupport"; import { usesTreeSchemaMode } from "@/lib/database/databaseFeatureSupport";
import { connectionUsesDatabaseObjectTreeMode, effectiveDatabaseTypeForConnection } from "@/lib/database/jdbcDialect"; import { connectionUsesDatabaseObjectTreeMode, effectiveDatabaseTypeForConnection } from "@/lib/database/jdbcDialect";
import { activeTabSidebarTarget, findSidebarNodeForActiveTab, findSidebarNodeForTarget, findNodePathForTarget, scrollTopForSidebarNode, shouldScrollActiveSidebarSelection, type ActiveTabSidebarTarget } from "@/lib/sidebar/sidebarActiveTabTarget"; import { activeTabSidebarTarget, findSidebarNodeForActiveTab, findSidebarNodeForTarget, findNodePathForTarget, scrollTopForSidebarNode, shouldScrollActiveSidebarSelection, type ActiveTabSidebarTarget, type SidebarNodeScrollAlign } from "@/lib/sidebar/sidebarActiveTabTarget";
import { findLoadedTableTargetForCandidate, queryContextTargetFromCandidate, queryCursorTableCandidate, type QueryCursorTableCandidate } from "@/lib/sql/queryCursorTableTarget"; import { findLoadedTableTargetForCandidate, queryContextTargetFromCandidate, queryCursorTableCandidate, type QueryCursorTableCandidate } from "@/lib/sql/queryCursorTableTarget";
import { SIDEBAR_TREE_ROW_HEIGHT, SIDEBAR_TREE_PRERENDER_COUNT, SIDEBAR_TREE_SCROLL_BUFFER, flattenTree, shouldVirtualizeFlatTree, type FlatTreeNode } from "@/composables/useFlatTree"; import { SIDEBAR_TREE_ROW_HEIGHT, SIDEBAR_TREE_PRERENDER_COUNT, SIDEBAR_TREE_SCROLL_BUFFER, flattenTree, shouldVirtualizeFlatTree, type FlatTreeNode } from "@/composables/useFlatTree";
import { sidebarTreeContextKey } from "@/lib/sidebar/sidebarTreeContext"; import { sidebarTreeContextKey } from "@/lib/sidebar/sidebarTreeContext";
@@ -534,13 +534,33 @@ const pendingRenameGroupId = ref<string | null>(null);
const highlightedNodeId = ref<string | null>(null); const highlightedNodeId = ref<string | null>(null);
let highlightTimer: number | undefined; let highlightTimer: number | undefined;
// 等待虚拟列表渲染后再高亮。
function waitForSidebarRenderFrame(): Promise<void> {
return new Promise((resolve) => {
window.requestAnimationFrame(() => resolve());
});
}
// 重新触发定位高亮,支持连续定位同一节点。
async function flashSidebarNode(nodeId: string) {
window.clearTimeout(highlightTimer);
highlightedNodeId.value = null;
await nextTick();
await waitForSidebarRenderFrame();
highlightedNodeId.value = nodeId;
highlightTimer = window.setTimeout(() => {
if (highlightedNodeId.value === nodeId) highlightedNodeId.value = null;
}, 1800);
}
function topOcclusionHeightForSidebarNode(nodeId: string): number { function topOcclusionHeightForSidebarNode(nodeId: string): number {
const sticky = stickyNode.value; const sticky = stickyNode.value;
if (!useVirtualTree.value || !sticky || sticky.id === nodeId) return 0; if (!useVirtualTree.value || !sticky || sticky.id === nodeId) return 0;
return SIDEBAR_TREE_ROW_HEIGHT; return SIDEBAR_TREE_ROW_HEIGHT;
} }
async function scrollToSidebarNode(nodeId: string) { async function scrollToSidebarNode(nodeId: string, options?: { align?: SidebarNodeScrollAlign }) {
await nextTick(); await nextTick();
const index = flatNodes.value.findIndex((item) => item.id === nodeId); const index = flatNodes.value.findIndex((item) => item.id === nodeId);
@@ -552,6 +572,7 @@ async function scrollToSidebarNode(nodeId: string) {
currentScrollTop: scroller.scrollTop, currentScrollTop: scroller.scrollTop,
viewportHeight: scroller.clientHeight, viewportHeight: scroller.clientHeight,
topOcclusionHeight: topOcclusionHeightForSidebarNode(nodeId), topOcclusionHeight: topOcclusionHeightForSidebarNode(nodeId),
...(options?.align ? { align: options.align } : {}),
}); });
if (nextScrollTop !== scroller.scrollTop) { if (nextScrollTop !== scroller.scrollTop) {
scroller.scrollTop = nextScrollTop; scroller.scrollTop = nextScrollTop;
@@ -661,13 +682,8 @@ async function locateActiveTabInSidebar() {
store.treeSelectionAnchorId = match.id; store.treeSelectionAnchorId = match.id;
await nextTick(); await nextTick();
window.clearTimeout(highlightTimer); await scrollToSidebarNode(match.id, { align: "smart" });
highlightedNodeId.value = match.id; await flashSidebarNode(match.id);
highlightTimer = window.setTimeout(() => {
highlightedNodeId.value = null;
}, 1800);
await scrollToSidebarNode(match.id);
} }
function tableTargetFromCandidate(candidate: QueryCursorTableCandidate): ActiveTabSidebarTarget { function tableTargetFromCandidate(candidate: QueryCursorTableCandidate): ActiveTabSidebarTarget {
@@ -143,7 +143,13 @@ export function activeTabSidebarTarget(tab: QueryTab | undefined | null): Active
function schemaMatches(node: TreeNode, schema: string | undefined): boolean { function schemaMatches(node: TreeNode, schema: string | undefined): boolean {
if (!schema) return true; if (!schema) return true;
return (node.schema || "") === schema;
if ((node.schema || "") === schema) return true;
// Some database engines (for example MySQL in database-object tree mode) do
// not store a separate schema on table nodes. In that case the database name
// is the effective schema, so a target schema equal to the node database
// should still resolve to the loaded table node.
return !node.schema && (node.database || "") === schema;
} }
export function matchesTarget(node: TreeNode, target: ActiveTabSidebarTarget): boolean { export function matchesTarget(node: TreeNode, target: ActiveTabSidebarTarget): boolean {
@@ -207,16 +213,28 @@ export function shouldScrollActiveSidebarSelection(options: { activeTabId: strin
return options.activeTabId !== options.previousActiveTabId || (options.autoSelectEnabled && options.previousAutoSelectEnabled === false); return options.activeTabId !== options.previousActiveTabId || (options.autoSelectEnabled && options.previousAutoSelectEnabled === false);
} }
export function scrollTopForSidebarNode(options: { index: number; currentScrollTop: number; viewportHeight: number; rowHeight?: number; topOcclusionHeight?: number }): number { // nearest is used for passive auto-selection; smart keeps context for explicit locate actions.
export type SidebarNodeScrollAlign = "nearest" | "top" | "smart";
export function scrollTopForSidebarNode(options: { index: number; currentScrollTop: number; viewportHeight: number; rowHeight?: number; topOcclusionHeight?: number; align?: SidebarNodeScrollAlign }): number {
const rowHeight = options.rowHeight ?? SIDEBAR_TREE_ROW_HEIGHT; const rowHeight = options.rowHeight ?? SIDEBAR_TREE_ROW_HEIGHT;
if (options.index < 0 || options.viewportHeight <= 0) return options.currentScrollTop; if (options.index < 0 || options.viewportHeight <= 0) return options.currentScrollTop;
const rowTop = options.index * rowHeight; const rowTop = options.index * rowHeight;
const rowBottom = rowTop + rowHeight; const rowBottom = rowTop + rowHeight;
const viewportTop = options.currentScrollTop + (options.topOcclusionHeight ?? 0); const topOcclusionHeight = options.topOcclusionHeight ?? 0;
if (options.align === "top") return Math.max(0, rowTop - topOcclusionHeight);
if (options.align === "smart") {
// Similar to IDE Locate: place the target around the upper third of the viewport.
const availableViewportHeight = Math.max(rowHeight, options.viewportHeight - topOcclusionHeight);
const smartOffset = Math.max(0, (availableViewportHeight - rowHeight) / 3);
return Math.max(0, Math.round(rowTop - topOcclusionHeight - smartOffset));
}
const viewportTop = options.currentScrollTop + topOcclusionHeight;
const viewportBottom = options.currentScrollTop + options.viewportHeight; const viewportBottom = options.currentScrollTop + options.viewportHeight;
if (rowTop < viewportTop) return Math.max(0, rowTop - (options.topOcclusionHeight ?? 0)); if (rowTop < viewportTop) return Math.max(0, rowTop - topOcclusionHeight);
if (rowBottom > viewportBottom) return Math.max(0, rowBottom - options.viewportHeight); if (rowBottom > viewportBottom) return Math.max(0, rowBottom - options.viewportHeight);
return options.currentScrollTop; return options.currentScrollTop;
} }
@@ -1,6 +1,6 @@
import { strict as assert } from "node:assert"; import { strict as assert } from "node:assert";
import { test } from "vitest"; import { test } from "vitest";
import { activeTabSidebarTarget, findSidebarNodeForActiveTab, scrollTopForSidebarNode, shouldScrollActiveSidebarSelection } from "../../apps/desktop/src/lib/sidebar/sidebarActiveTabTarget.ts"; import { activeTabSidebarTarget, findNodePathForTarget, findSidebarNodeForActiveTab, scrollTopForSidebarNode, shouldScrollActiveSidebarSelection } from "../../apps/desktop/src/lib/sidebar/sidebarActiveTabTarget.ts";
import type { FlatTreeNode } from "../../apps/desktop/src/composables/useFlatTree.ts"; import type { FlatTreeNode } from "../../apps/desktop/src/composables/useFlatTree.ts";
import type { QueryTab, TreeNode } from "../../apps/desktop/src/types/database.ts"; import type { QueryTab, TreeNode } from "../../apps/desktop/src/types/database.ts";
@@ -8,6 +8,172 @@ function flat(node: TreeNode, depth = 0): FlatTreeNode {
return { id: node.id, node, depth, type: node.type }; return { id: node.id, node, depth, type: node.type };
} }
test("findNodePathForTarget handles loaded, unloaded, and MySQL schema fallback trees", () => {
const cases = [
{
name: "nested table path",
tree: [
{
id: "conn-1",
label: "mysql.example.test",
type: "connection",
connectionId: "conn-1",
isExpanded: true,
children: [
{
id: "conn-1:__user_admin",
label: "tree.userAdmin",
type: "user-admin",
connectionId: "conn-1",
database: "",
isExpanded: false,
},
{
id: "conn-1:app",
label: "app",
type: "database",
connectionId: "conn-1",
database: "app",
isExpanded: true,
children: [
{
id: "conn-1:app:__tables",
label: "tree.tables",
type: "group-tables",
connectionId: "conn-1",
database: "app",
isExpanded: true,
children: [
{
id: "conn-1:app:__tables:users",
label: "users",
type: "table",
connectionId: "conn-1",
database: "app",
isExpanded: false,
children: [],
pinned: false,
},
],
pinned: false,
},
],
pinned: false,
},
],
pinned: false,
},
],
target: {
type: "table",
connectionId: "conn-1",
database: "app",
tableName: "users",
},
expectedPath: ["conn-1", "conn-1:app", "conn-1:app:__tables", "conn-1:app:__tables:users"],
},
{
name: "unloaded connection tree",
tree: [
{
id: "mysql-conn-1",
label: "mysql.example.test",
type: "connection",
connectionId: "mysql-conn-1",
isExpanded: false,
children: [
{
id: "mysql-conn-1:__user_admin",
label: "tree.userAdmin",
type: "user-admin",
connectionId: "mysql-conn-1",
database: "",
isExpanded: false,
},
],
pinned: false,
},
],
target: {
type: "query-context",
connectionId: "mysql-conn-1",
database: "app_prd",
},
expectedPath: null,
},
{
name: "MySQL schema fallback to database",
tree: [
{
id: "mysql-conn-1",
label: "mysql.example.test",
type: "connection",
connectionId: "mysql-conn-1",
isExpanded: true,
children: [
{
id: "mysql-conn-1:app_dev",
label: "app_dev",
type: "database",
connectionId: "mysql-conn-1",
database: "app_dev",
isExpanded: true,
children: [
{
id: "mysql-conn-1:app_dev:__tables",
label: "tree.tables",
type: "group-tables",
connectionId: "mysql-conn-1",
database: "app_dev",
isExpanded: true,
children: [
{
id: "mysql-conn-1:app_dev:__tables:enum_info",
label: "enum_info",
type: "table",
tableType: "BASE TABLE",
connectionId: "mysql-conn-1",
database: "app_dev",
isExpanded: false,
children: [],
pinned: false,
},
],
pinned: false,
},
],
pinned: false,
},
],
},
],
target: {
type: "table",
connectionId: "mysql-conn-1",
database: "app_dev",
schema: "app_dev",
tableName: "enum_info",
},
expectedPath: [
"mysql-conn-1",
"mysql-conn-1:app_dev",
"mysql-conn-1:app_dev:__tables",
"mysql-conn-1:app_dev:__tables:enum_info",
],
},
] satisfies Array<{
name: string;
tree: TreeNode[];
target: Parameters<typeof findNodePathForTarget>[0];
expectedPath: string[] | null;
}>;
for (const item of cases) {
const path = findNodePathForTarget(item.target, item.tree);
assert.deepEqual(path?.map((node) => node.id) ?? null, item.expectedPath, item.name);
}
});
test("data tabs target the matching visible table or view node", () => { test("data tabs target the matching visible table or view node", () => {
const tab: QueryTab = { const tab: QueryTab = {
id: "tab-1", id: "tab-1",
@@ -260,6 +426,13 @@ test("sidebar node scrolling keeps visible rows in place and reveals hidden rows
assert.equal(scrollTopForSidebarNode({ index: 11, currentScrollTop: 300, viewportHeight: 140, topOcclusionHeight: 28 }), 280); assert.equal(scrollTopForSidebarNode({ index: 11, currentScrollTop: 300, viewportHeight: 140, topOcclusionHeight: 28 }), 280);
}); });
test("sidebar node scrolling supports top and smart locate alignment", () => {
assert.equal(scrollTopForSidebarNode({ index: 20, currentScrollTop: 0, viewportHeight: 140, align: "top" }), 560);
assert.equal(scrollTopForSidebarNode({ index: 20, currentScrollTop: 0, viewportHeight: 140, align: "smart" }), 523);
assert.equal(scrollTopForSidebarNode({ index: 11, currentScrollTop: 300, viewportHeight: 140, topOcclusionHeight: 28, align: "smart" }), 252);
assert.equal(scrollTopForSidebarNode({ index: 0, currentScrollTop: 300, viewportHeight: 140, topOcclusionHeight: 28, align: "smart" }), 0);
});
test("active sidebar selection only scrolls on tab or setting changes", () => { test("active sidebar selection only scrolls on tab or setting changes", () => {
assert.equal( assert.equal(
shouldScrollActiveSidebarSelection({ shouldScrollActiveSidebarSelection({