0.3.6 修复shitf选择问题
This commit is contained in:
@@ -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 };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user