feat(sidebar): locate column in data grid on sidebar column click
This commit is contained in:
1 parent
9419e18b79
commit
f951983914
14 files changed
+549
-5
No files matched your search
@@ -605,6 +605,7 @@ interface DataGridProps {
|
||||
showCancel?: boolean;
|
||||
cancelling?: boolean;
|
||||
cancelDisabled?: boolean;
|
||||
revealColumnRequest?: { id: number; columnName: string };
|
||||
}
|
||||
|
||||
const props = withDefaults(defineProps<DataGridProps>(), {
|
||||
@@ -2522,6 +2523,52 @@ function scrollToColumnIndex(columnIndex: number) {
|
||||
});
|
||||
}
|
||||
|
||||
let lastHandledRevealColumnRequestId: number | null = null;
|
||||
|
||||
function handleRevealColumnRequest(request?: { id: number; columnName: string }) {
|
||||
if (!request?.columnName || request.id === lastHandledRevealColumnRequestId) return;
|
||||
if (!props.result.columns || props.result.columns.length === 0) return;
|
||||
const columnIndex = props.result.columns.findIndex((column, index) => matchesTableInfoColumn(column, props.sourceColumns?.[index], request.columnName));
|
||||
if (columnIndex < 0) return;
|
||||
lastHandledRevealColumnRequestId = request.id;
|
||||
scrollToColumnIndex(columnIndex);
|
||||
}
|
||||
|
||||
watch(
|
||||
() => props.revealColumnRequest,
|
||||
(request) => {
|
||||
handleRevealColumnRequest(request);
|
||||
},
|
||||
{ immediate: true, deep: true },
|
||||
);
|
||||
|
||||
watch(
|
||||
() => props.result.columns,
|
||||
() => {
|
||||
if (props.revealColumnRequest && props.revealColumnRequest.id !== lastHandledRevealColumnRequestId) {
|
||||
void nextTick(() => {
|
||||
handleRevealColumnRequest(props.revealColumnRequest);
|
||||
});
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
onMounted(() => {
|
||||
if (props.revealColumnRequest && props.revealColumnRequest.id !== lastHandledRevealColumnRequestId) {
|
||||
void nextTick(() => {
|
||||
handleRevealColumnRequest(props.revealColumnRequest);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
onActivated(() => {
|
||||
if (props.revealColumnRequest && props.revealColumnRequest.id !== lastHandledRevealColumnRequestId) {
|
||||
void nextTick(() => {
|
||||
handleRevealColumnRequest(props.revealColumnRequest);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// --- Column resize composable ---
|
||||
const columnWidthDensity = computed(() => settingsStore.editorSettings.columnWidthDensity);
|
||||
const columnWidthMode = computed(() => settingsStore.editorSettings.dataGridColumnWidthMode ?? "content");
|
||||
|
||||
@@ -0,0 +1,175 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import { createApp, defineComponent, h, markRaw, nextTick, ref, type App, type PropType } from "vue";
|
||||
import { createPinia, setActivePinia } from "pinia";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import i18n from "@/i18n";
|
||||
import type { QueryResult } from "@/types/database";
|
||||
import { TooltipProvider } from "@/components/ui/tooltip";
|
||||
|
||||
vi.mock("@/composables/useDataGridColumnResize", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("@/composables/useDataGridColumnResize")>();
|
||||
const { ref } = await import("vue");
|
||||
return {
|
||||
...actual,
|
||||
useDataGridColumnResize: () => ({
|
||||
initColumnWidths: vi.fn(),
|
||||
onResizeStart: vi.fn(),
|
||||
autoFitColumn: vi.fn(),
|
||||
renderedColumnWidths: ref([100, 100, 100, 100]),
|
||||
totalWidth: ref(400),
|
||||
columnVars: ref({ "--total-w": "400px" }),
|
||||
getIsResizing: () => false,
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
import DataGrid from "../DataGrid.vue";
|
||||
import { useSettingsStore } from "@/stores/settingsStore";
|
||||
|
||||
const mountedApps: Array<{ app: App; host: HTMLElement }> = [];
|
||||
|
||||
const RecycleScroller = defineComponent({
|
||||
props: {
|
||||
items: {
|
||||
type: Array as PropType<unknown[]>,
|
||||
default: () => [],
|
||||
},
|
||||
},
|
||||
setup(props, { attrs, slots }) {
|
||||
return () =>
|
||||
h(
|
||||
"div",
|
||||
attrs,
|
||||
props.items.map((item) => slots.default?.({ item })),
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
for (const { app, host } of mountedApps.splice(0)) {
|
||||
app.unmount();
|
||||
host.remove();
|
||||
}
|
||||
document.body.innerHTML = "";
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe("DataGrid revealColumnRequest", () => {
|
||||
it("reveals and highlights column matching revealColumnRequest", async () => {
|
||||
const pinia = createPinia();
|
||||
setActivePinia(pinia);
|
||||
const settingsStore = useSettingsStore();
|
||||
settingsStore.updateEditorSettings({
|
||||
dataGridRenderMode: "canvas",
|
||||
});
|
||||
|
||||
const columns = ["id", "username", "email", "created_at"];
|
||||
const result = markRaw<QueryResult>({
|
||||
columns,
|
||||
rows: [[1, "alice", "alice@example.com", "2026-01-01"]],
|
||||
affected_rows: 0,
|
||||
execution_time_ms: 0,
|
||||
});
|
||||
|
||||
const revealRequest = ref<{ id: number; columnName: string } | undefined>({
|
||||
id: 1,
|
||||
columnName: "email",
|
||||
});
|
||||
|
||||
const host = document.createElement("div");
|
||||
document.body.append(host);
|
||||
|
||||
const Root = defineComponent({
|
||||
setup() {
|
||||
return () =>
|
||||
h(
|
||||
TooltipProvider,
|
||||
{ delayDuration: 0 },
|
||||
{
|
||||
default: () =>
|
||||
h(DataGrid, {
|
||||
result,
|
||||
databaseType: "mysql",
|
||||
context: "table-data",
|
||||
revealColumnRequest: revealRequest.value,
|
||||
}),
|
||||
},
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
const app = createApp(Root);
|
||||
app.use(pinia);
|
||||
app.use(i18n);
|
||||
app.component("RecycleScroller", RecycleScroller);
|
||||
app.mount(host);
|
||||
mountedApps.push({ app, host });
|
||||
|
||||
await nextTick();
|
||||
await nextTick();
|
||||
|
||||
const emailHeader = host.querySelector('[data-grid-column-index="2"]');
|
||||
expect(emailHeader).toBeTruthy();
|
||||
expect(emailHeader?.classList.contains("data-grid-header-cell--selected")).toBe(true);
|
||||
});
|
||||
|
||||
it("updates reveal when revealColumnRequest prop changes with new id", async () => {
|
||||
const pinia = createPinia();
|
||||
setActivePinia(pinia);
|
||||
const settingsStore = useSettingsStore();
|
||||
settingsStore.updateEditorSettings({
|
||||
dataGridRenderMode: "canvas",
|
||||
});
|
||||
|
||||
const columns = ["id", "username", "email", "created_at"];
|
||||
const result = markRaw<QueryResult>({
|
||||
columns,
|
||||
rows: [[1, "alice", "alice@example.com", "2026-01-01"]],
|
||||
affected_rows: 0,
|
||||
execution_time_ms: 0,
|
||||
});
|
||||
|
||||
const revealRequest = ref<{ id: number; columnName: string } | undefined>(undefined);
|
||||
|
||||
const host = document.createElement("div");
|
||||
document.body.append(host);
|
||||
|
||||
const Root = defineComponent({
|
||||
setup() {
|
||||
return () =>
|
||||
h(
|
||||
TooltipProvider,
|
||||
{ delayDuration: 0 },
|
||||
{
|
||||
default: () =>
|
||||
h(DataGrid, {
|
||||
result,
|
||||
databaseType: "mysql",
|
||||
context: "table-data",
|
||||
revealColumnRequest: revealRequest.value,
|
||||
}),
|
||||
},
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
const app = createApp(Root);
|
||||
app.use(pinia);
|
||||
app.use(i18n);
|
||||
app.component("RecycleScroller", RecycleScroller);
|
||||
app.mount(host);
|
||||
mountedApps.push({ app, host });
|
||||
|
||||
await nextTick();
|
||||
|
||||
// Trigger reveal request
|
||||
revealRequest.value = { id: 2, columnName: "created_at" };
|
||||
await nextTick();
|
||||
await nextTick();
|
||||
|
||||
const createdAtHeader = host.querySelector('[data-grid-column-index="3"]');
|
||||
expect(createdAtHeader).toBeTruthy();
|
||||
expect(createdAtHeader?.classList.contains("data-grid-header-cell--selected")).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -2518,6 +2518,7 @@ defineExpose({
|
||||
:allow-insert-rows="activeTab.queryAnalysis?.allowInsert ?? activeTab.queryAnalysis?.allowInsertDelete !== false"
|
||||
:allow-delete-rows="activeTab.queryAnalysis?.allowDelete ?? activeTab.queryAnalysis?.allowInsertDelete !== false"
|
||||
context="results"
|
||||
:reveal-column-request="activeTab.gridRevealColumnRequest"
|
||||
:auto-transpose-single-row="settingsStore.editorSettings.dataGridAutoTransposeSingleRow"
|
||||
:database-type="activeEffectiveDatabaseType"
|
||||
:connection-id="activeResultConnectionId"
|
||||
@@ -3014,6 +3015,7 @@ defineExpose({
|
||||
:show-cancel="shouldShowCancelAction(activeTab)"
|
||||
:cancelling="activeTab.isCancelling"
|
||||
:cancel-disabled="!canCancelQueryExecution(activeTab)"
|
||||
:reveal-column-request="activeTab.gridRevealColumnRequest"
|
||||
@cancel="emit('cancel', activeTab.id)"
|
||||
@update:where-input="(v: string) => (activeTab.whereInput = v)"
|
||||
@update:order-by-input="(v: string) => (activeTab.orderByInput = v)"
|
||||
|
||||
@@ -1190,6 +1190,8 @@ function runRowClickAction(clickDetail: number) {
|
||||
const requestId = beginNavigationRequest();
|
||||
if (action === "open-data") {
|
||||
scheduleOpenData(node);
|
||||
} else if (action === "locate-column") {
|
||||
locateColumnInDataGrid(node);
|
||||
} else if (action === "open-object-browser") {
|
||||
void openObjectBrowser(false, false, true);
|
||||
} else if (action === "open-object-browser-and-expand") {
|
||||
@@ -1631,6 +1633,8 @@ function onDoubleClick(event: MouseEvent) {
|
||||
openDataImmediately(activeNode.value);
|
||||
} else if (action === "activate-data") {
|
||||
activateDataTableFromDoubleClick();
|
||||
} else if (action === "locate-column") {
|
||||
locateColumnInDataGrid(activeNode.value);
|
||||
} else if (action === "open-source") {
|
||||
openObjectSourceDialog(false);
|
||||
} else if (action === "open-extension-details") {
|
||||
@@ -1671,6 +1675,57 @@ function findExistingSameTableDataTab() {
|
||||
return queryStore.tabs.find((tab) => tab.mode === "data" && tab.connectionId === node.connectionId && tab.database === node.database && (tab.tableMeta?.catalog || "") === (node.catalog || "") && (tab.schema || "") === (tableSchema || "") && (tab.tableMeta?.tableName || tab.title) === node.label);
|
||||
}
|
||||
|
||||
function extractColumnNameFromTreeNode(node: TreeNode): string {
|
||||
if (node.meta && typeof node.meta === "object" && "name" in node.meta && typeof (node.meta as any).name === "string") {
|
||||
return (node.meta as any).name;
|
||||
}
|
||||
return node.label.replace(/\s+\(.+\)$/, "").trim();
|
||||
}
|
||||
|
||||
function findTableNodeForColumn(columnNode: TreeNode): TreeNode | undefined {
|
||||
if (!columnNode.tableName) return undefined;
|
||||
const match = (n: TreeNode): boolean => {
|
||||
return (
|
||||
(n.type === "table" || n.type === "view" || n.type === "materialized_view") &&
|
||||
n.connectionId === columnNode.connectionId &&
|
||||
n.database === columnNode.database &&
|
||||
(n.schema || "") === (columnNode.schema || "") &&
|
||||
(n.catalog || "") === (columnNode.catalog || "") &&
|
||||
(n.label === columnNode.tableName || n.tableName === columnNode.tableName)
|
||||
);
|
||||
};
|
||||
const search = (nodes: TreeNode[]): TreeNode | undefined => {
|
||||
for (const n of nodes) {
|
||||
if (match(n)) return n;
|
||||
if (n.children?.length) {
|
||||
const found = search(n.children);
|
||||
if (found) return found;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
return search(connectionStore.treeNodes);
|
||||
}
|
||||
|
||||
function locateColumnInDataGrid(columnNode: TreeNode) {
|
||||
if (!columnNode.tableName || !hasNodeDatabaseContext(columnNode)) return;
|
||||
const columnName = extractColumnNameFromTreeNode(columnNode);
|
||||
if (!columnName) return;
|
||||
|
||||
const tableNode = findTableNodeForColumn(columnNode) ?? {
|
||||
id: columnNode.schema ? `${columnNode.connectionId}:${columnNode.database}:${columnNode.schema}:${columnNode.tableName}` : `${columnNode.connectionId}:${columnNode.database}:${columnNode.tableName}`,
|
||||
label: columnNode.tableName,
|
||||
type: "table" as const,
|
||||
connectionId: columnNode.connectionId,
|
||||
database: columnNode.database,
|
||||
schema: columnNode.schema,
|
||||
catalog: columnNode.catalog,
|
||||
tableName: columnNode.tableName,
|
||||
};
|
||||
|
||||
emit("open-data", tableNode, false, "default", (target, request) => openData(target, request, "default", { revealColumn: columnName }));
|
||||
}
|
||||
|
||||
function openMongoTreeData(node: TreeNode) {
|
||||
if (!node.connectionId || !node.database) return;
|
||||
if (node.type === "mongo-gridfs") {
|
||||
|
||||
+172
@@ -0,0 +1,172 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import { createApp, defineComponent, h, nextTick, ref, type App } from "vue";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import i18n from "@/i18n";
|
||||
import type { TreeNode } from "@/types/database";
|
||||
import SidebarTreeRuntimeHost from "@/components/sidebar/SidebarTreeRuntimeHost.vue";
|
||||
|
||||
const connectionStore = {
|
||||
treeNodes: [] as TreeNode[],
|
||||
sidebarSearchQuery: "",
|
||||
activeConnectionId: null as string | null,
|
||||
connectedIds: new Set<string>(),
|
||||
selectedTreeNodeId: null as string | null,
|
||||
selectedTreeNodeIds: [] as string[],
|
||||
treeSelectionAnchorId: null as string | null,
|
||||
connectionMultiSelectActive: false,
|
||||
get selectedTreeNodeIdsSet(): Set<string> {
|
||||
return new Set(this.selectedTreeNodeIds);
|
||||
},
|
||||
canUseLoadedTreeNodeToggle: vi.fn(() => true),
|
||||
releaseCollapsedTreeNodeChildren: vi.fn(),
|
||||
getConfig: vi.fn(() => ({ db_type: "mysql", name: "connection" })),
|
||||
ensureConnected: vi.fn(async () => undefined),
|
||||
};
|
||||
|
||||
const queryStore = {
|
||||
tabs: [] as any[],
|
||||
switchTab: vi.fn(),
|
||||
createTab: vi.fn(),
|
||||
};
|
||||
|
||||
const settings = {
|
||||
sidebarActivation: "single",
|
||||
sidebarBrowseObjectsOnDatabaseActivation: false,
|
||||
dataTabReuseMode: "same-table",
|
||||
shortcuts: { openDataInNewTab: "" },
|
||||
};
|
||||
|
||||
const openDataMock = vi.fn();
|
||||
|
||||
vi.mock("@/stores/connectionStore", () => ({
|
||||
CONNECTION_ATTEMPT_CANCELLED_MESSAGE: "connection attempt cancelled",
|
||||
useConnectionStore: () => connectionStore,
|
||||
}));
|
||||
|
||||
vi.mock("@/stores/queryStore", () => ({ useQueryStore: () => queryStore }));
|
||||
vi.mock("@/stores/settingsStore", () => ({ useSettingsStore: () => ({ editorSettings: settings }) }));
|
||||
vi.mock("@/stores/savedSqlStore", () => ({ useSavedSqlStore: () => ({}) }));
|
||||
vi.mock("@/composables/useToast", () => ({ useToast: () => ({ toast: vi.fn() }) }));
|
||||
vi.mock("@/composables/useSqlHighlighter", () => ({ useSqlHighlighter: () => ({ highlight: vi.fn() }) }));
|
||||
vi.mock("@/composables/useSidebarDataOpenRuntime", () => ({ useSidebarDataOpenRuntime: () => ({ openData: openDataMock }) }));
|
||||
vi.mock("@/composables/useDatabaseOptions", () => ({ useDatabaseOptions: () => ({ getDatabaseOptions: vi.fn() }) }));
|
||||
vi.mock("@/composables/useSidebarConnectionMutationRuntime", () => ({ useSidebarConnectionMutationRuntime: () => ({}) }));
|
||||
vi.mock("@/composables/useSidebarDatabaseSpecificMutationRuntime", () => ({ useSidebarDatabaseSpecificMutationRuntime: () => ({}) }));
|
||||
vi.mock("@/composables/useSidebarTableMutationRuntime", () => ({ useSidebarTableMutationRuntime: () => ({}) }));
|
||||
vi.mock("@/composables/useSidebarTreeExportRuntime", () => ({ useSidebarTreeExportRuntime: () => ({}) }));
|
||||
vi.mock("@/composables/useSidebarTreeToolRuntime", () => ({ useSidebarTreeToolRuntime: () => ({}) }));
|
||||
|
||||
const mountedApps: App[] = [];
|
||||
|
||||
afterEach(() => {
|
||||
for (const app of mountedApps.splice(0)) app.unmount();
|
||||
document.body.innerHTML = "";
|
||||
connectionStore.treeNodes = [];
|
||||
openDataMock.mockReset();
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe("SidebarTreeRuntimeHost column locate", () => {
|
||||
it("locates column in data grid on single click in single-activation mode", async () => {
|
||||
settings.sidebarActivation = "single";
|
||||
|
||||
const tableNode: TreeNode = {
|
||||
id: "mysql:app:users",
|
||||
label: "users",
|
||||
type: "table",
|
||||
connectionId: "mysql",
|
||||
database: "app",
|
||||
isExpanded: true,
|
||||
children: [],
|
||||
};
|
||||
const columnNode: TreeNode = {
|
||||
id: "mysql:app:users:__columns:email",
|
||||
label: "email (varchar(255))",
|
||||
type: "column",
|
||||
connectionId: "mysql",
|
||||
database: "app",
|
||||
tableName: "users",
|
||||
meta: { name: "email", data_type: "varchar(255)" },
|
||||
};
|
||||
tableNode.children = [columnNode];
|
||||
connectionStore.treeNodes = [tableNode];
|
||||
|
||||
const onOpenData = vi.fn(async (target, requireSelection, openMode, runner) => {
|
||||
await runner(target, { isCurrent: () => true, signal: new AbortController().signal, registerCancel: vi.fn() });
|
||||
});
|
||||
|
||||
const host = ref<InstanceType<typeof SidebarTreeRuntimeHost> | null>(null);
|
||||
const container = document.createElement("div");
|
||||
document.body.appendChild(container);
|
||||
|
||||
const app = createApp(
|
||||
defineComponent({
|
||||
setup() {
|
||||
return () => h(SidebarTreeRuntimeHost, { ref: host, node: columnNode, depth: 1, onOpenData });
|
||||
},
|
||||
}),
|
||||
);
|
||||
app.use(i18n);
|
||||
mountedApps.push(app);
|
||||
app.mount(container);
|
||||
await nextTick();
|
||||
|
||||
host.value?.handleRowClick(columnNode, 1);
|
||||
await nextTick();
|
||||
|
||||
expect(onOpenData).toHaveBeenCalledWith(expect.objectContaining({ label: "users", connectionId: "mysql", database: "app" }), false, "default", expect.any(Function));
|
||||
expect(openDataMock).toHaveBeenCalledWith(expect.objectContaining({ label: "users" }), expect.anything(), "default", { revealColumn: "email" });
|
||||
});
|
||||
|
||||
it("locates column in data grid on double click in double-activation mode", async () => {
|
||||
settings.sidebarActivation = "double";
|
||||
|
||||
const tableNode: TreeNode = {
|
||||
id: "mysql:app:users",
|
||||
label: "users",
|
||||
type: "table",
|
||||
connectionId: "mysql",
|
||||
database: "app",
|
||||
isExpanded: true,
|
||||
children: [],
|
||||
};
|
||||
const columnNode: TreeNode = {
|
||||
id: "mysql:app:users:__columns:age",
|
||||
label: "age (int)",
|
||||
type: "column",
|
||||
connectionId: "mysql",
|
||||
database: "app",
|
||||
tableName: "users",
|
||||
meta: { name: "age", data_type: "int" },
|
||||
};
|
||||
tableNode.children = [columnNode];
|
||||
connectionStore.treeNodes = [tableNode];
|
||||
|
||||
const onOpenData = vi.fn(async (target, requireSelection, openMode, runner) => {
|
||||
await runner(target, { isCurrent: () => true, signal: new AbortController().signal, registerCancel: vi.fn() });
|
||||
});
|
||||
|
||||
const host = ref<InstanceType<typeof SidebarTreeRuntimeHost> | null>(null);
|
||||
const container = document.createElement("div");
|
||||
document.body.appendChild(container);
|
||||
|
||||
const app = createApp(
|
||||
defineComponent({
|
||||
setup() {
|
||||
return () => h(SidebarTreeRuntimeHost, { ref: host, node: columnNode, depth: 1, onOpenData });
|
||||
},
|
||||
}),
|
||||
);
|
||||
app.use(i18n);
|
||||
mountedApps.push(app);
|
||||
app.mount(container);
|
||||
await nextTick();
|
||||
|
||||
host.value?.handleRowDoubleClick(columnNode, new MouseEvent("dblclick"));
|
||||
await nextTick();
|
||||
|
||||
expect(onOpenData).toHaveBeenCalledWith(expect.objectContaining({ label: "users", connectionId: "mysql", database: "app" }), false, "default", expect.any(Function));
|
||||
expect(openDataMock).toHaveBeenCalledWith(expect.objectContaining({ label: "users" }), expect.anything(), "default", { revealColumn: "age" });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user