0.3.6 修复shitf选择问题

This commit is contained in:
liaibo
2026-01-24 13:14:24 +08:00
parent 3c3f407f4b
commit a4183d86d8
4 changed files with 174 additions and 12 deletions
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { reduceFileTreeSelection } from "./selection";
import { normalizeFileTreeSelectionForVisibleRows, reduceFileTreeSelection } from "./selection";
describe("reduceFileTreeSelection", () => {
const visible = ["a", "b", "c", "d"];
@@ -56,5 +56,14 @@ describe("reduceFileTreeSelection", () => {
expect(next.anchorRowId).toBeNull();
expect(next.focusedRowId).toBeNull();
});
});
it("可见行变化时保留仍可见的选择", () => {
const next = normalizeFileTreeSelectionForVisibleRows(
{ selectedRowIds: new Set(["b", "x"]), anchorRowId: "b", focusedRowId: "x" },
["a", "b", "c"],
);
expect(Array.from(next.selectedRowIds)).toEqual(["b"]);
expect(next.anchorRowId).toBe("b");
expect(next.focusedRowId).toBe("b");
});
});
@@ -82,3 +82,51 @@ export function reduceFileTreeSelection(
}
}
export function normalizeFileTreeSelectionForVisibleRows(
prev: FileTreeSelectionState,
visibleRowIds: string[],
): FileTreeSelectionState {
const visibleSet = new Set(visibleRowIds);
let selectedRowIds = prev.selectedRowIds;
let removedAny = false;
const kept: string[] = [];
prev.selectedRowIds.forEach((id) => {
if (visibleSet.has(id)) {
kept.push(id);
return;
}
removedAny = true;
});
if (removedAny) {
selectedRowIds = new Set(kept);
}
let anchorRowId = prev.anchorRowId;
if (anchorRowId && !visibleSet.has(anchorRowId)) {
anchorRowId = null;
}
let focusedRowId = prev.focusedRowId;
if (focusedRowId && !visibleSet.has(focusedRowId)) {
focusedRowId = null;
}
if (!focusedRowId && selectedRowIds.size > 0) {
focusedRowId = visibleRowIds.find((id) => selectedRowIds.has(id)) ?? null;
}
if (!anchorRowId && focusedRowId) {
anchorRowId = focusedRowId;
}
if (
selectedRowIds === prev.selectedRowIds &&
anchorRowId === prev.anchorRowId &&
focusedRowId === prev.focusedRowId
) {
return prev;
}
return { selectedRowIds, anchorRowId, focusedRowId };
}