Skip to content

Commit

Permalink
fix: 系统拦截快捷键后多选交互异常 (#2191)
Browse files Browse the repository at this point in the history
* fix: 系统拦截快捷键后多选交互异常

* test: 增加测试用例

---------

Co-authored-by: 沫君 <[email protected]>
  • Loading branch information
lcx-seima and 沫君 authored May 12, 2023
1 parent ce9dcab commit 6a8b85a
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import type {
ViewMeta,
} from '@/common/interface';
import type { SpreadSheet } from '@/sheet-type';
import { InteractionStateName, S2Event } from '@/common/constant';
import {
InteractionKeyboardKey,
InteractionStateName,
InterceptType,
S2Event,
} from '@/common/constant';
import type { Node } from '@/facet/layout/node';

jest.mock('@/interaction/event-controller');
Expand Down Expand Up @@ -84,6 +89,42 @@ describe('Interaction Row & Column Cell Click Tests', () => {
expect(rowColumnClick.bindEvents).toBeDefined();
});

test.each([InteractionKeyboardKey.META, InteractionKeyboardKey.CONTROL])(
'should add click intercept when %s keydown',
(key) => {
s2.emit(S2Event.GLOBAL_KEYBOARD_DOWN, {
key,
} as KeyboardEvent);

expect(s2.interaction.hasIntercepts([InterceptType.CLICK])).toBeTruthy();
},
);

test.each([InteractionKeyboardKey.META, InteractionKeyboardKey.CONTROL])(
'should remove click intercept when %s keyup',
(key) => {
s2.interaction.addIntercepts([InterceptType.CLICK]);
s2.emit(S2Event.GLOBAL_KEYBOARD_UP, {
key,
} as KeyboardEvent);

expect(s2.interaction.hasIntercepts([InterceptType.CLICK])).toBeFalsy();
},
);

test.each([InteractionKeyboardKey.META, InteractionKeyboardKey.CONTROL])(
'should remove click intercept when %s released',
() => {
Object.defineProperty(rowColumnClick, 'isMultiSelection', {
value: true,
});
s2.interaction.addIntercepts([InterceptType.CLICK]);
s2.emit(S2Event.GLOBAL_MOUSE_MOVE, {} as MouseEvent);

expect(s2.interaction.hasIntercepts([InterceptType.CLICK])).toBeFalsy();
},
);

// https://github.com/antvis/S2/issues/1243
test.each([S2Event.ROW_CELL_CLICK, S2Event.COL_CELL_CLICK])(
'should selected cell when %s cell clicked',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('Interaction Data Cell Multi Selection Tests', () => {
});

test.each([InteractionKeyboardKey.META, InteractionKeyboardKey.CONTROL])(
'should add click intercept when keydown',
'should add click intercept when %s keydown',
(key) => {
s2.emit(S2Event.GLOBAL_KEYBOARD_DOWN, {
key,
Expand All @@ -58,8 +58,9 @@ describe('Interaction Data Cell Multi Selection Tests', () => {
);

test.each([InteractionKeyboardKey.META, InteractionKeyboardKey.CONTROL])(
'should remove click intercept when keyup',
'should remove click intercept when %s keyup',
(key) => {
s2.interaction.addIntercepts([InterceptType.CLICK]);
s2.emit(S2Event.GLOBAL_KEYBOARD_UP, {
key,
} as KeyboardEvent);
Expand All @@ -68,6 +69,19 @@ describe('Interaction Data Cell Multi Selection Tests', () => {
},
);

test.each([InteractionKeyboardKey.META, InteractionKeyboardKey.CONTROL])(
'should remove click intercept when %s released',
() => {
Object.defineProperty(dataCellMultiSelection, 'isMultiSelection', {
value: true,
});
s2.interaction.addIntercepts([InterceptType.CLICK]);
s2.emit(S2Event.GLOBAL_MOUSE_MOVE, {} as MouseEvent);

expect(s2.interaction.hasIntercepts([InterceptType.CLICK])).toBeFalsy();
},
);

test.each([InteractionKeyboardKey.META, InteractionKeyboardKey.CONTROL])(
'should select multiple data cell',
(key) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,24 @@ describe('Interaction Range Selection Tests', () => {
});

test('should remove click intercept when shift keyup', () => {
s2.interaction.addIntercepts([InterceptType.CLICK]);
s2.emit(S2Event.GLOBAL_KEYBOARD_UP, {
key: InteractionKeyboardKey.SHIFT,
} as KeyboardEvent);

expect(s2.interaction.hasIntercepts([InterceptType.CLICK])).toBeFalsy();
});

test('should remove click intercept when shift released', () => {
Object.defineProperty(rangeSelection, 'isRangeSelection', {
value: true,
});
s2.interaction.addIntercepts([InterceptType.CLICK]);
s2.emit(S2Event.GLOBAL_MOUSE_MOVE, {} as MouseEvent);

expect(s2.interaction.hasIntercepts([InterceptType.CLICK])).toBeFalsy();
});

test('should set last clicked cell', () => {
s2.interaction.changeState({
cells: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import {
hideColumnsByThunkGroup,
isEqualDisplaySiblingNodeId,
} from '../../../utils/hide-columns';
import { isMultiSelectionKey } from '../../../utils/interaction/select-event';
import {
isMouseEventWithMeta,
isMultiSelectionKey,
} from '../../../utils/interaction/select-event';
import {
getTooltipOptions,
getTooltipVisibleOperator,
Expand All @@ -36,6 +39,12 @@ export class RowColumnClick extends BaseEvent implements BaseEventImplement {
this.bindColCellClick();
this.bindRowCellClick();
this.bindTableColExpand();
this.bindMouseMove();
}

public reset() {
this.isMultiSelection = false;
this.spreadsheet.interaction.removeIntercepts([InterceptType.CLICK]);
}

private bindKeyboardDown() {
Expand All @@ -52,8 +61,16 @@ export class RowColumnClick extends BaseEvent implements BaseEventImplement {
private bindKeyboardUp() {
this.spreadsheet.on(S2Event.GLOBAL_KEYBOARD_UP, (event: KeyboardEvent) => {
if (isMultiSelectionKey(event)) {
this.isMultiSelection = false;
this.spreadsheet.interaction.removeIntercepts([InterceptType.CLICK]);
this.reset();
}
});
}

private bindMouseMove() {
this.spreadsheet.on(S2Event.GLOBAL_MOUSE_MOVE, (event) => {
// 当快捷键被系统拦截后,按需补充调用一次 reset
if (this.isMultiSelection && !isMouseEventWithMeta(event)) {
this.reset();
}
});
}
Expand Down
12 changes: 11 additions & 1 deletion packages/s2-core/src/interaction/data-cell-multi-selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { CellMeta, S2CellType, ViewMeta } from '../common/interface';
import {
getCellMeta,
isMultiSelectionKey,
getInteractionCellsBySelectedCells,
isMouseEventWithMeta,
} from '../utils/interaction/select-event';
import { getCellsTooltipData } from '../utils/tooltip';
import { afterSelectDataCells } from '../utils/interaction/select-event';
Expand All @@ -27,6 +27,7 @@ export class DataCellMultiSelection
this.bindKeyboardDown();
this.bindDataCellClick();
this.bindKeyboardUp();
this.bindMouseMove();
}

public reset() {
Expand Down Expand Up @@ -54,6 +55,15 @@ export class DataCellMultiSelection
});
}

private bindMouseMove() {
this.spreadsheet.on(S2Event.GLOBAL_MOUSE_MOVE, (event) => {
// 当快捷键被系统拦截后,按需补充调用一次 reset
if (this.isMultiSelection && !isMouseEventWithMeta(event)) {
this.reset();
}
});
}

private getSelectedCells(cell: S2CellType<ViewMeta>) {
const id = cell.getMeta().id;
const { interaction } = this.spreadsheet;
Expand Down
10 changes: 10 additions & 0 deletions packages/s2-core/src/interaction/range-selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class RangeSelection extends BaseEvent implements BaseEventImplement {
this.bindDataCellClick();
this.bindColCellClick();
this.bindKeyboardUp();
this.bindMouseMove();
}

public reset() {
Expand Down Expand Up @@ -49,6 +50,15 @@ export class RangeSelection extends BaseEvent implements BaseEventImplement {
});
}

private bindMouseMove() {
// 当快捷键被系统拦截后,按需补充调用一次 reset
this.spreadsheet.on(S2Event.GLOBAL_MOUSE_MOVE, (event) => {
if (this.isRangeSelection && !event.shiftKey) {
this.reset();
}
});
}

private bindColCellClick() {
if (this.spreadsheet.isTableMode()) {
// series-number click
Expand Down
4 changes: 4 additions & 0 deletions packages/s2-core/src/utils/interaction/select-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export const isMultiSelectionKey = (e: KeyboardEvent) => {
);
};

export const isMouseEventWithMeta = (e: MouseEvent) => {
return e.ctrlKey || e.metaKey;
};

export const getCellMeta = (cell: S2CellType): CellMeta => {
const meta = cell.getMeta();
const { id, colIndex, rowIndex, rowQuery } = meta || {};
Expand Down

1 comment on commit 6a8b85a

@vercel
Copy link

@vercel vercel bot commented on 6a8b85a May 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

antvis-s2 – ./

antvis-s2-antv-s2.vercel.app
antvis-s2-git-master-antv-s2.vercel.app
antvis-s2.vercel.app

Please sign in to comment.