fix(grid): 修复 Ctrl 点选单元格变成区间选择

This commit is contained in:
zipg authored and GitHub committed 2026-06-28 10:33:25 +08:00
1 parent be996a4352
commit 9a730ce188
2 files changed
+163 -3

No files matched your search

@@ -31,6 +31,7 @@ export function useDataGridSelection(options: UseDataGridSelectionOptions) {
const isSelectingCells = ref(false);
const isSelectingAll = ref(false);
const selectedCellKeys = ref<Set<string>>(new Set());
const selectedRowIds = ref<Set<number>>(new Set());
const selectedColumnIndexes = ref<Set<number>>(new Set());
const lastClickedRowIndex = ref<number | null>(null);
@@ -50,17 +51,21 @@ export function useDataGridSelection(options: UseDataGridSelectionOptions) {
if (hasColumnSelection.value) {
return extractColumnsSelection(columns.value, visibleSelectionRows.value, selectedColumnIndexes.value);
}
if (selectedCellKeys.value.size > 0) {
return extractSelectedCellKeys(columns.value, visibleSelectionRows.value, selectedCellKeys.value);
}
const range = selectedRange.value;
if (!range) return { columns: [], rows: [] };
return extractSelection(columns.value, visibleSelectionRows.value, range);
});
const selectedCellCount = computed(() => selectedCells.value.columns.length * selectedCells.value.rows.length);
const selectedCellCount = computed(() => (selectedCellKeys.value.size > 0 ? selectedCellKeys.value.size : selectedCells.value.columns.length * selectedCells.value.rows.length));
const hasCellSelection = computed(() => selectedCellCount.value > 0);
function clearCellSelection() {
selectionAnchor.value = null;
selectionFocus.value = null;
selectedCellKeys.value = new Set();
selectedColumnIndexes.value = new Set();
isSelectingCells.value = false;
isSelectingAll.value = false;
@@ -74,6 +79,7 @@ export function useDataGridSelection(options: UseDataGridSelectionOptions) {
function selectSingleCell(rowIndex: number, colIndex: number) {
const cell = { rowIndex, colIndex };
selectedCellKeys.value = new Set();
selectionAnchor.value = cell;
selectionFocus.value = cell;
}
@@ -81,6 +87,7 @@ export function useDataGridSelection(options: UseDataGridSelectionOptions) {
function selectRow(rowIndex: number) {
const range = rowSelectionRange(rowIndex, columns.value.length);
if (!range) return;
selectedCellKeys.value = new Set();
selectionAnchor.value = { rowIndex: range.startRow, colIndex: range.startCol };
selectionFocus.value = { rowIndex: range.endRow, colIndex: range.endCol };
}
@@ -88,6 +95,7 @@ export function useDataGridSelection(options: UseDataGridSelectionOptions) {
function selectRows(startRow: number, endRow: number) {
const range = rowSelectionRange(startRow, columns.value.length, endRow);
if (!range) return;
selectedCellKeys.value = new Set();
selectionAnchor.value = { rowIndex: range.startRow, colIndex: range.startCol };
selectionFocus.value = { rowIndex: range.endRow, colIndex: range.endCol };
}
@@ -96,6 +104,7 @@ export function useDataGridSelection(options: UseDataGridSelectionOptions) {
const normalizedColumns = normalizeSelectedColumnIndexes(Array.from({ length: Math.abs(endCol - startCol) + 1 }, (_, index) => Math.min(startCol, endCol) + index));
if (normalizedColumns.length === 0 || displayItems.value.length <= 0) return;
clearRowSelection();
selectedCellKeys.value = new Set();
selectionAnchor.value = null;
selectionFocus.value = null;
const next = options?.merge ? new Set(selectedColumnIndexes.value) : new Set<number>();
@@ -113,6 +122,7 @@ export function useDataGridSelection(options: UseDataGridSelectionOptions) {
clearRowSelection();
selectionAnchor.value = null;
selectionFocus.value = null;
selectedCellKeys.value = new Set();
const next = new Set(selectedColumnIndexes.value);
if (next.has(colIndex)) {
next.delete(colIndex);
@@ -132,6 +142,7 @@ export function useDataGridSelection(options: UseDataGridSelectionOptions) {
if (!range) return;
clearRowSelection();
selectedColumnIndexes.value = new Set();
selectedCellKeys.value = new Set();
lastClickedColumnIndex.value = null;
selectionAnchor.value = { rowIndex: range.startRow, colIndex: range.startCol };
selectionFocus.value = { rowIndex: range.endRow, colIndex: range.endCol };
@@ -140,6 +151,7 @@ export function useDataGridSelection(options: UseDataGridSelectionOptions) {
}
function extendCellSelectionTo(rowIndex: number, colIndex: number) {
selectedCellKeys.value = new Set();
if (!selectionAnchor.value) {
selectSingleCell(rowIndex, colIndex);
return;
@@ -147,6 +159,29 @@ export function useDataGridSelection(options: UseDataGridSelectionOptions) {
selectionFocus.value = { rowIndex, colIndex };
}
function toggleCellSelection(rowIndex: number, colIndex: number) {
const key = cellKey(rowIndex, colIndex);
const next = selectedCellKeys.value.size > 0 ? new Set(selectedCellKeys.value) : selectedRangeToCellKeys(selectedRange.value);
if (next.has(key)) {
next.delete(key);
} else {
next.add(key);
}
selectedCellKeys.value = next;
selectedColumnIndexes.value = new Set();
if (next.size > 0) {
selectionAnchor.value = { rowIndex, colIndex };
selectionFocus.value = null;
} else {
selectionAnchor.value = null;
selectionFocus.value = null;
}
isSelectingCells.value = false;
isSelectingAll.value = false;
lastClickedColumnIndex.value = colIndex;
if (showTranspose.value) transposeRowIndex.value = rowIndex;
}
function handleRowClick(rowIndex: number, rowId: number, event: MouseEvent) {
const isMeta = event.metaKey || event.ctrlKey;
const isShift = event.shiftKey;
@@ -213,11 +248,18 @@ export function useDataGridSelection(options: UseDataGridSelectionOptions) {
const isMeta = event.metaKey || event.ctrlKey;
const isShift = event.shiftKey;
if (isMeta || isShift) {
if (isMeta) {
event.preventDefault();
focusGridWithoutScrolling();
clearRowSelection();
toggleCellSelection(rowIndex, colIndex);
return;
}
if (isShift) {
event.preventDefault();
focusGridWithoutScrolling();
clearRowSelection();
if (hasColumnSelection.value) clearCellSelection();
extendCellSelectionTo(rowIndex, colIndex);
return;
}
@@ -231,6 +273,7 @@ export function useDataGridSelection(options: UseDataGridSelectionOptions) {
}
function cellIsSelected(rowIndex: number, colIndex: number): boolean {
if (selectedCellKeys.value.size > 0) return selectedCellKeys.value.has(cellKey(rowIndex, colIndex));
if (hasColumnSelection.value) return rowIndex >= 0 && rowIndex < displayItems.value.length && selectedColumnIndexes.value.has(colIndex);
return isCellInSelection(rowIndex, colIndex, selectedRange.value);
}
@@ -257,6 +300,7 @@ export function useDataGridSelection(options: UseDataGridSelectionOptions) {
selectedCellCount,
hasCellSelection,
isSelectingAll,
selectedCellKeys,
clearCellSelection,
selectSingleCell,
selectRow,
@@ -282,3 +326,48 @@ export function useDataGridSelection(options: UseDataGridSelectionOptions) {
isRowSelected,
};
}
function cellKey(rowIndex: number, colIndex: number): string {
return `${rowIndex}:${colIndex}`;
}
function parseCellKey(key: string): CellPosition | null {
const [row, col] = key.split(":");
const rowIndex = Number(row);
const colIndex = Number(col);
if (!Number.isInteger(rowIndex) || !Number.isInteger(colIndex) || rowIndex < 0 || colIndex < 0) return null;
return { rowIndex, colIndex };
}
function selectedRangeToCellKeys(range: CellSelectionRange | null): Set<string> {
const keys = new Set<string>();
if (!range) return keys;
for (let rowIndex = range.startRow; rowIndex <= range.endRow; rowIndex++) {
for (let colIndex = range.startCol; colIndex <= range.endCol; colIndex++) {
keys.add(cellKey(rowIndex, colIndex));
}
}
return keys;
}
function extractSelectedCellKeys(columns: readonly string[], rows: readonly CellValue[][], keys: Iterable<string>): SelectionData {
const positions = [...keys]
.map(parseCellKey)
.filter((position): position is CellPosition => !!position && position.rowIndex < rows.length && position.colIndex < columns.length)
.sort((a, b) => a.rowIndex - b.rowIndex || a.colIndex - b.colIndex);
const selectedColumnIndexes = normalizeSelectedColumnIndexes(positions.map((position) => position.colIndex)).filter((index) => index < columns.length);
const rowsByIndex = new Map<number, CellValue[]>();
for (const position of positions) {
const row = rows[position.rowIndex];
if (!row) continue;
const values = rowsByIndex.get(position.rowIndex) ?? [];
values.push(row[position.colIndex] ?? null);
rowsByIndex.set(position.rowIndex, values);
}
return {
columns: selectedColumnIndexes.map((index) => columns[index]),
rows: [...rowsByIndex.values()],
};
}