fix(objects): support modifier-click table selection

This commit is contained in:
Melody Ruch authored and GitHub committed 2026-08-14 02:06:05 +08:00
1 parent f9f21cbd88
commit 10b1ba4578
3 files changed
+99

No files matched your search

@@ -0,0 +1,52 @@
import { describe, expect, it } from "vitest";
import { objectBrowserTableSelectionAnchor, objectBrowserTableSelectionRange } from "@/lib/table/objectBrowserSelection";
import type { ObjectBrowserRow } from "@/lib/table/objectBrowserRows";
function row(type: ObjectBrowserRow["type"], name: string): ObjectBrowserRow {
return { id: `${type}-${name}`, name, displayName: name, type };
}
describe("objectBrowserTableSelectionRange", () => {
it("selects every table between anchor and current row, inclusive", () => {
const rows = [row("TABLE", "a"), row("TABLE", "b"), row("TABLE", "c"), row("TABLE", "d")];
expect(objectBrowserTableSelectionRange(rows, "TABLE-a", "TABLE-c")).toEqual(["TABLE-a", "TABLE-b", "TABLE-c"]);
});
it("supports selecting upward when the current row precedes the anchor", () => {
const rows = [row("TABLE", "a"), row("TABLE", "b"), row("TABLE", "c"), row("TABLE", "d")];
expect(objectBrowserTableSelectionRange(rows, "TABLE-d", "TABLE-b")).toEqual(["TABLE-b", "TABLE-c", "TABLE-d"]);
});
it("drops non-table rows inside the span, since only tables are selectable", () => {
const rows = [row("TABLE", "a"), row("VIEW", "v1"), row("PROCEDURE", "p1"), row("TABLE", "b")];
expect(objectBrowserTableSelectionRange(rows, "TABLE-a", "TABLE-b")).toEqual(["TABLE-a", "TABLE-b"]);
});
it("returns just the current row when the anchor is not selectable and current is a table", () => {
const rows = [row("TABLE", "a"), row("TABLE", "b")];
expect(objectBrowserTableSelectionRange(rows, "missing-anchor", "TABLE-b")).toEqual(["TABLE-b"]);
});
it("returns an empty range when the anchor is missing and current row is not a table", () => {
const rows = [row("VIEW", "v1"), row("TABLE", "b")];
expect(objectBrowserTableSelectionRange(rows, "missing-anchor", "VIEW-v1")).toEqual([]);
});
it("collapses to a single row when anchor and current are the same table", () => {
const rows = [row("TABLE", "a"), row("TABLE", "b")];
expect(objectBrowserTableSelectionRange(rows, "TABLE-a", "TABLE-a")).toEqual(["TABLE-a"]);
});
});
describe("objectBrowserTableSelectionAnchor", () => {
it("keeps a visible table anchor", () => {
const rows = [row("TABLE", "a"), row("TABLE", "b")];
expect(objectBrowserTableSelectionAnchor(rows, "TABLE-a", "TABLE-b")).toBe("TABLE-a");
});
it("uses the current table when the anchor is missing or no longer visible", () => {
const rows = [row("TABLE", "a"), row("TABLE", "b")];
expect(objectBrowserTableSelectionAnchor(rows, null, "TABLE-b")).toBe("TABLE-b");
expect(objectBrowserTableSelectionAnchor(rows, "TABLE-filtered-out", "TABLE-b")).toBe("TABLE-b");
});
});
@@ -0,0 +1,25 @@
import type { ObjectBrowserRow } from "@/lib/table/objectBrowserRows";
export function objectBrowserTableSelectionAnchor(rows: readonly ObjectBrowserRow[], anchorId: string | null, currentId: string): string {
if (anchorId && rows.some((row) => row.id === anchorId && row.type === "TABLE")) return anchorId;
return currentId;
}
/**
* Compute the contiguous table-id range for a shift-click in the object
* browser, mirroring the sidebar tree's range-select behavior
* (sidebarTreeSelection.ts): the range spans every row between the anchor
* and the clicked row in visible order, but only TABLE rows are selectable
* so non-table rows inside the span are dropped from the result.
*/
export function objectBrowserTableSelectionRange(rows: readonly ObjectBrowserRow[], anchorId: string, currentId: string): string[] {
const anchorIndex = rows.findIndex((row) => row.id === anchorId);
const currentIndex = rows.findIndex((row) => row.id === currentId);
if (anchorIndex < 0 || currentIndex < 0) return rows.some((row) => row.id === currentId && row.type === "TABLE") ? [currentId] : [];
const start = Math.min(anchorIndex, currentIndex);
const end = Math.max(anchorIndex, currentIndex);
return rows
.slice(start, end + 1)
.filter((row) => row.type === "TABLE")
.map((row) => row.id);
}