Skip to content

Commit 866d3a7

Browse files
committed
* dtable: add selectOnClickCell option to control cell selection on click
- Add `selectOnClickCell` option (default `true` in datagrid plugin) to skip selection when the pointer barely moved between mousedown and mouseup. - Thread the originating `MouseEvent` through `DTableCellPos` / `getMousePos` so the selectable plugin can compare drag distance.
1 parent d5db132 commit 866d3a7

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

lib/dtable/src/plugins/datagrid/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ export const datagridPlugin: DTablePlugin<DTableDatagridTypes, DTableDatagridDep
301301
hotkeys: {},
302302
copyHeader: false,
303303
autoExpandGrid: true,
304+
selectOnClickCell: true,
304305
},
305306
options(options) {
306307
const {datagridHotkeys, datasource, hotkeys, editable: editableOption, selectable: selectableOption, beforeSelectCells, showRowIndex, colResize, onPasteToCell, afterStageDraft} = options;

lib/dtable/src/plugins/selectable/index.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export type DTableCellSelection = `${DTableColSelection}${DTableRowSelection}`;
1717
export type DTableSelection = DTableColSelection | DTableRowSelection | DTableCellSelection;
1818
export type DTableRangeSelection = `${DTableSelection}:${DTableSelection}`;
1919
export type DTableSelections = (DTableSelection | DTableRangeSelection)[];
20-
export type DTableCellPos = {col: DTableColIndex; row: DTableRowIndex};
20+
export type DTableCellPos = {col: DTableColIndex; row: DTableRowIndex; event?: MouseEvent};
2121

2222
export type DTableCellPosMap = Map<DTableColIndex, Set<DTableRowIndex>>;
2323

@@ -29,6 +29,7 @@ export interface DTableSelectableTypes {
2929
ignoreDeselectOn: string;
3030
markSelectRange: boolean;
3131
copyHeader?: boolean;
32+
selectOnClickCell?: boolean;
3233
selectableHotkeys?: {
3334
selectAll?: boolean | string;
3435
copy?: boolean | string;
@@ -433,7 +434,7 @@ function getSelectedCellsSize(this: DTableSelectable): number {
433434
return size;
434435
}
435436

436-
export function getMousePos(table: DTable, event: Event, options?: {ignoreHeaderCell?: boolean}): DTableCellPos | undefined {
437+
export function getMousePos(table: DTable, event: MouseEvent, options?: {ignoreHeaderCell?: boolean}): DTableCellPos | undefined {
437438
const pointerInfo = table.getPointerInfo(event);
438439
if (!pointerInfo || pointerInfo.target.closest('input,textarea,[contenteditable]')) {
439440
return;
@@ -448,7 +449,7 @@ export function getMousePos(table: DTable, event: Event, options?: {ignoreHeader
448449
return;
449450
}
450451
const rowIndex = isHeaderRow ? (-1) : table.getRowInfo(rowID)?.index ?? -1;
451-
return {col: colIndex, row: rowIndex};
452+
return {col: colIndex, row: rowIndex, event};
452453
}
453454

454455
function handleSelectNextCell(table: DTableSelectable, event: KeyboardEvent, direction?: 'right' | 'down' | 'left' | 'up') {
@@ -652,6 +653,16 @@ const selectablePlugin: DTablePlugin<DTableSelectableTypes, [DTableHotkeyTypes,
652653
this.data.selectingStart = undefined;
653654
const pos = getMousePos(this, event);
654655
if (pos) {
656+
// 如果鼠标只是点击没有移动(从按下到谈起的移动距离小于4px),并且没有启用 selectOnClickCell 则不进行选择
657+
const startEvent = selectingStart.event;
658+
if (startEvent && !this.options.selectOnClickCell) {
659+
const distance = Math.sqrt(Math.pow(event.clientX - startEvent.clientX, 2) + Math.pow(event.clientY - startEvent.clientY, 2));
660+
if (distance < 4) {
661+
return;
662+
}
663+
return;
664+
}
665+
655666
const selection = stringifySelection(selectingStart, pos);
656667
if (selection) {
657668
requestAnimationFrame(() => this.selectCells(selection));

0 commit comments

Comments
 (0)