feat(sidebar): navigate the connection tree with arrow keys
This commit is contained in:
1 parent
a196a68d38
commit
0b8c7d700a
8 files changed
+573
No files matched your search
@@ -0,0 +1,112 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { flattenTree } from "@/composables/useFlatTree";
|
||||
import type { TreeNode, TreeNodeType } from "@/types/database";
|
||||
import { sidebarTreeArrowAction } from "@/lib/sidebar/sidebarTreeArrowNavigation";
|
||||
|
||||
function node(id: string, type: TreeNodeType, children: TreeNode[] = [], isExpanded = false): TreeNode {
|
||||
return { id, label: id, type, children, isExpanded };
|
||||
}
|
||||
|
||||
function buildRows(): ReturnType<typeof flattenTree> {
|
||||
const tree = [node("conn1", "connection"), node("conn2", "connection", [node("db1", "database", [node("group-tables", "group-tables", [node("t1", "table"), node("t2", "table")], true), node("group-views", "group-views")], true), node("db2", "database")], true), node("conn3", "connection")];
|
||||
return flattenTree(tree);
|
||||
}
|
||||
|
||||
// Visible order: conn1, conn2, db1, group-tables, t1, t2, group-views, db2, conn3.
|
||||
describe("sidebar tree arrow navigation", () => {
|
||||
it("ArrowDown selects the next visible row", () => {
|
||||
const rows = buildRows();
|
||||
expect(sidebarTreeArrowAction(rows, "conn1", "ArrowDown")).toEqual({ kind: "select", nodeId: "conn2" });
|
||||
expect(sidebarTreeArrowAction(rows, "t1", "ArrowDown")).toEqual({ kind: "select", nodeId: "t2" });
|
||||
});
|
||||
|
||||
it("ArrowUp selects the previous visible row", () => {
|
||||
const rows = buildRows();
|
||||
expect(sidebarTreeArrowAction(rows, "t2", "ArrowUp")).toEqual({ kind: "select", nodeId: "t1" });
|
||||
expect(sidebarTreeArrowAction(rows, "group-views", "ArrowUp")).toEqual({ kind: "select", nodeId: "t2" });
|
||||
});
|
||||
|
||||
it("ArrowUp on the first row and ArrowDown on the last row do nothing", () => {
|
||||
const rows = buildRows();
|
||||
expect(sidebarTreeArrowAction(rows, "conn1", "ArrowUp")).toEqual({ kind: "none" });
|
||||
expect(sidebarTreeArrowAction(rows, "conn3", "ArrowDown")).toEqual({ kind: "none" });
|
||||
});
|
||||
|
||||
it("ArrowRight expands a collapsed expandable node", () => {
|
||||
const rows = buildRows();
|
||||
expect(sidebarTreeArrowAction(rows, "conn1", "ArrowRight")).toEqual({ kind: "toggle", nodeId: "conn1", expanded: true });
|
||||
});
|
||||
|
||||
it("ArrowRight expands a collapsed container whose children are not loaded yet", () => {
|
||||
const rows = buildRows();
|
||||
expect(sidebarTreeArrowAction(rows, "db2", "ArrowRight")).toEqual({ kind: "toggle", nodeId: "db2", expanded: true });
|
||||
});
|
||||
|
||||
it("ArrowRight on an expanded node selects its first child", () => {
|
||||
const rows = buildRows();
|
||||
expect(sidebarTreeArrowAction(rows, "group-tables", "ArrowRight")).toEqual({ kind: "select", nodeId: "t1" });
|
||||
});
|
||||
|
||||
it("ArrowRight on a collapsed table expands it (column/index groups load on demand)", () => {
|
||||
const rows = buildRows();
|
||||
expect(sidebarTreeArrowAction(rows, "t1", "ArrowRight")).toEqual({ kind: "toggle", nodeId: "t1", expanded: true });
|
||||
});
|
||||
|
||||
it("ArrowRight on a leaf does nothing", () => {
|
||||
const rows = flattenTree([node("tbl", "table", [node("col1", "column")], true)]);
|
||||
expect(sidebarTreeArrowAction(rows, "col1", "ArrowRight")).toEqual({ kind: "none" });
|
||||
});
|
||||
|
||||
it("ArrowLeft collapses an expanded node", () => {
|
||||
const rows = buildRows();
|
||||
expect(sidebarTreeArrowAction(rows, "group-tables", "ArrowLeft")).toEqual({ kind: "toggle", nodeId: "group-tables", expanded: false });
|
||||
expect(sidebarTreeArrowAction(rows, "conn2", "ArrowLeft")).toEqual({ kind: "toggle", nodeId: "conn2", expanded: false });
|
||||
});
|
||||
|
||||
it("ArrowLeft on a leaf selects its parent", () => {
|
||||
const rows = buildRows();
|
||||
expect(sidebarTreeArrowAction(rows, "t1", "ArrowLeft")).toEqual({ kind: "select", nodeId: "group-tables" });
|
||||
});
|
||||
|
||||
it("ArrowLeft on a collapsed container selects its parent", () => {
|
||||
const rows = buildRows();
|
||||
expect(sidebarTreeArrowAction(rows, "group-views", "ArrowLeft")).toEqual({ kind: "select", nodeId: "db1" });
|
||||
});
|
||||
|
||||
it("ArrowLeft on a collapsed root does nothing", () => {
|
||||
const rows = buildRows();
|
||||
expect(sidebarTreeArrowAction(rows, "conn1", "ArrowLeft")).toEqual({ kind: "none" });
|
||||
});
|
||||
|
||||
it("ignores unknown keys and unknown node ids", () => {
|
||||
const rows = buildRows();
|
||||
expect(sidebarTreeArrowAction(rows, "t1", "PageDown")).toEqual({ kind: "none" });
|
||||
expect(sidebarTreeArrowAction(rows, "missing", "ArrowDown")).toEqual({ kind: "none" });
|
||||
});
|
||||
|
||||
it("ArrowRight does not expand a PostgreSQL-family custom type without members", () => {
|
||||
const typeNode: TreeNode = { ...node("t1", "type", [node("m1", "type-attributes")]), hasMembers: false };
|
||||
const rows = flattenTree([typeNode]);
|
||||
expect(sidebarTreeArrowAction(rows, "t1", "ArrowRight", { databaseType: "postgres" })).toEqual({ kind: "none" });
|
||||
});
|
||||
|
||||
it("ArrowRight expands a custom type that has members", () => {
|
||||
const typeNode: TreeNode = { ...node("t1", "type", [node("m1", "type-attributes")]), hasMembers: true };
|
||||
const rows = flattenTree([typeNode]);
|
||||
expect(sidebarTreeArrowAction(rows, "t1", "ArrowRight", { databaseType: "postgres" })).toEqual({ kind: "toggle", nodeId: "t1", expanded: true });
|
||||
});
|
||||
|
||||
it("the member gate only applies to custom-type databases", () => {
|
||||
const typeNode: TreeNode = { ...node("t1", "type", [node("m1", "type-attributes")]), hasMembers: false };
|
||||
const rows = flattenTree([typeNode]);
|
||||
expect(sidebarTreeArrowAction(rows, "t1", "ArrowRight", { databaseType: "mysql" })).toEqual({ kind: "toggle", nodeId: "t1", expanded: true });
|
||||
const plainRows = buildRows();
|
||||
expect(sidebarTreeArrowAction(plainRows, "conn1", "ArrowRight", { databaseType: "postgres" })).toEqual({ kind: "toggle", nodeId: "conn1", expanded: true });
|
||||
});
|
||||
|
||||
it("ArrowLeft still collapses an expanded custom type without members", () => {
|
||||
const typeNode: TreeNode = { ...node("t1", "type", [node("m1", "type-attributes")], true), hasMembers: false };
|
||||
const rows = flattenTree([typeNode]);
|
||||
expect(sidebarTreeArrowAction(rows, "t1", "ArrowLeft", { databaseType: "postgres" })).toEqual({ kind: "toggle", nodeId: "t1", expanded: false });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,56 @@
|
||||
import type { FlatTreeNode } from "@/composables/useFlatTree";
|
||||
import type { DatabaseType, TreeNode } from "@/types/database";
|
||||
import { canTreeNodeShowExpander } from "@/lib/sidebar/sidebarTreeItemLayout";
|
||||
import { customTypeCapabilities } from "@/lib/database/databaseObjectCapabilities";
|
||||
|
||||
export type SidebarTreeArrowAction = { kind: "select"; nodeId: string } | { kind: "toggle"; nodeId: string; expanded: boolean } | { kind: "none" };
|
||||
|
||||
/**
|
||||
* Resolves the standard tree-navigation action (VS Code / file explorer
|
||||
* semantics) for an arrow key pressed on the row identified by
|
||||
* `currentNodeId`. Callers pass the visible rows they render, excluding
|
||||
* non-selectable pseudo rows (e.g. per-database table search controls), so
|
||||
* vertical movement naturally skips them. `databaseType` is the connection's
|
||||
* dialect of the active row and mirrors the chevron expandability gates.
|
||||
*/
|
||||
export function sidebarTreeArrowAction(rows: readonly FlatTreeNode[], currentNodeId: string, key: string, options?: { databaseType?: DatabaseType }): SidebarTreeArrowAction {
|
||||
const currentIndex = rows.findIndex((row) => row.id === currentNodeId);
|
||||
if (currentIndex < 0) return { kind: "none" };
|
||||
|
||||
const current = rows[currentIndex]!;
|
||||
const node = current.node;
|
||||
|
||||
if (key === "ArrowUp") {
|
||||
return currentIndex > 0 ? { kind: "select", nodeId: rows[currentIndex - 1]!.id } : { kind: "none" };
|
||||
}
|
||||
if (key === "ArrowDown") {
|
||||
return currentIndex < rows.length - 1 ? { kind: "select", nodeId: rows[currentIndex + 1]!.id } : { kind: "none" };
|
||||
}
|
||||
if (key === "ArrowRight") {
|
||||
const next = rows[currentIndex + 1];
|
||||
if (node.isExpanded && next && next.depth === current.depth + 1) return { kind: "select", nodeId: next.id };
|
||||
if (!node.isExpanded && isArrowExpandable(node, options?.databaseType)) return { kind: "toggle", nodeId: node.id, expanded: true };
|
||||
return { kind: "none" };
|
||||
}
|
||||
if (key === "ArrowLeft") {
|
||||
if (node.isExpanded && node.children?.length) return { kind: "toggle", nodeId: node.id, expanded: false };
|
||||
for (let index = currentIndex - 1; index >= 0; index -= 1) {
|
||||
if (rows[index]!.depth < current.depth) return { kind: "select", nodeId: rows[index]!.id };
|
||||
}
|
||||
return { kind: "none" };
|
||||
}
|
||||
return { kind: "none" };
|
||||
}
|
||||
|
||||
function isArrowExpandable(node: TreeNode, databaseType?: DatabaseType): boolean {
|
||||
// Match the chevron gate (TreeItem/SidebarTreeRuntimeHost canExpand):
|
||||
// PostgreSQL-family custom types only expand when the catalog marked them
|
||||
// with loadable members; member-less kinds (enum/domain/range/base) stay
|
||||
// collapsed even when child metadata rows exist.
|
||||
if (node.type === "type" && customTypeCapabilities(databaseType).details) return node.hasMembers === true;
|
||||
return canTreeNodeShowExpander({
|
||||
type: node.type,
|
||||
childCount: node.children?.length ?? 0,
|
||||
explicitContainer: (node.type === "package" && node.children !== undefined) || node.xuguTypeMembersExpandable === true,
|
||||
});
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { InjectionKey } from "vue";
|
||||
import type { FlatTreeNode } from "@/composables/useFlatTree";
|
||||
import type { TreeNode } from "@/types/database";
|
||||
|
||||
export interface SidebarTreeContext {
|
||||
@@ -10,6 +11,11 @@ export interface SidebarTreeContext {
|
||||
setTableSearchQuery?: (parentNodeId: string, query: string, local: boolean) => void;
|
||||
refreshTableSearchIndex?: (parentNodeId: string) => void;
|
||||
registerPasteHandler?: (nodeId: string, callback: () => void) => () => void;
|
||||
/** Selectable visible rows (pseudo rows like table search controls are already
|
||||
* filtered out) driving keyboard arrow navigation. */
|
||||
getVisibleFlatNodes?: () => readonly FlatTreeNode[];
|
||||
/** Moves DOM focus to a rendered row after keyboard navigation changed the selection. */
|
||||
focusTreeNode?: (nodeId: string) => void;
|
||||
}
|
||||
|
||||
export const sidebarTreeContextKey: InjectionKey<SidebarTreeContext> = Symbol("sidebar-tree-context");
|
||||
Reference in new issue
Block a user