Skip to content

Commit

Permalink
fix(interaction): 修复数值单元格取消选中 & 点击空白处取消选中时没触发 GLOBAL_SELECTED 事件 (#2449)
Browse files Browse the repository at this point in the history
* fix(interaction): 修复数值单元格取消选中 & 点击空白处取消选中时没触发 GLOBAL_SELECTED 事件 close #2447

* test: 修复单测
  • Loading branch information
lijinke666 authored Dec 5, 2023
1 parent a06bb99 commit 07f7201
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ describe('Interaction Data Cell Click Tests', () => {
s2.emit(S2Event.DATA_CELL_CLICK, {
stopPropagation() {},
} as unknown as GEvent);

expect(s2.interaction.getState()).toEqual({
cells: [mockCellInfo.mockCellMeta],
stateName: InteractionStateName.SELECTED,
Expand Down Expand Up @@ -82,9 +83,29 @@ describe('Interaction Data Cell Click Tests', () => {
s2.emit(S2Event.DATA_CELL_CLICK, {
stopPropagation() {},
} as unknown as GEvent);

expect(selected).toHaveBeenCalledWith([mockCellInfo.mockCell]);
});

// https://github.com/antvis/S2/issues/2447
test('should emit cell selected event when cell unselected', () => {
jest
.spyOn(s2.interaction, 'isSelectedCell')
.mockImplementationOnce(() => true);

const selected = jest.fn();
s2.on(S2Event.GLOBAL_SELECTED, selected);

s2.emit(S2Event.DATA_CELL_CLICK, {
stopPropagation() {},
originalEvent: {
detail: 1,
},
} as unknown as GEvent);

expect(selected).toHaveBeenCalledWith([]);
});

test('should emit link field jump event when link field text click and not show tooltip', () => {
const linkFieldJump = jest.fn();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,11 @@ describe('Interaction Event Controller Tests', () => {
maxY: 100,
} as BBox,
} as BaseFacet;

const selected = jest.fn();
const reset = jest.fn();
spreadsheet.on(S2Event.GLOBAL_RESET, reset);
spreadsheet.on(S2Event.GLOBAL_SELECTED, selected);

window.dispatchEvent(
new MouseEvent('click', {
Expand All @@ -516,6 +519,7 @@ describe('Interaction Event Controller Tests', () => {
} as MouseEventInit),
);

expect(selected).toHaveBeenCalledWith([]);
expect(reset).toHaveBeenCalled();
expect(spreadsheet.interaction.reset).toHaveBeenCalled();
});
Expand All @@ -527,13 +531,17 @@ describe('Interaction Event Controller Tests', () => {
maxY: 100,
} as BBox,
} as BaseFacet;

const selected = jest.fn();
const reset = jest.fn();
spreadsheet.on(S2Event.GLOBAL_RESET, reset);
spreadsheet.on(S2Event.GLOBAL_SELECTED, selected);

window.dispatchEvent(
new KeyboardEvent('keydown', { key: InteractionKeyboardKey.ESC }),
);

expect(selected).toHaveBeenCalledWith([]);
expect(reset).toHaveBeenCalled();
expect(spreadsheet.interaction.reset).toHaveBeenCalled();
});
Expand Down
7 changes: 3 additions & 4 deletions packages/s2-core/__tests__/unit/interaction/root-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
DataCellBrushSelection,
ColBrushSelection,
RowBrushSelection,
S2Event,
} from '@/index';
import { RootInteraction } from '@/interaction/root';
import { mergeCell, unmergeCell } from '@/utils/interaction/merge-cell';
Expand Down Expand Up @@ -170,9 +171,9 @@ describe('RootInteraction Tests', () => {

// https://github.com/antvis/S2/issues/1243
test('should multi selected header cells', () => {
const isEqualStateNameSpy = jest
jest
.spyOn(rootInteraction, 'isEqualStateName')
.mockImplementation(() => false);
.mockImplementationOnce(() => false);

const mockCellA = createMockCellInfo('test-A').mockCell;
const mockCellB = createMockCellInfo('test-B').mockCell;
Expand Down Expand Up @@ -204,8 +205,6 @@ describe('RootInteraction Tests', () => {

// 取消选中
expect(rootInteraction.getState().cells).toEqual([getCellMeta(mockCellA)]);

isEqualStateNameSpy.mockRestore();
});

test('should call merge cells', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/s2-core/src/facet/header/pivot-row.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export class PivotRowHeader extends BaseFrozenRowHeader {
protected createCellInstance(item: Node): RowCell {
const { spreadsheet, scrollY } = this.headerConfig;
const frozenRow = this.isFrozenRow(item);

return new FrozenRowCell(
item,
spreadsheet,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,15 @@ export class DataCellClick extends BaseEvent implements BaseEventImplement {
interaction.addIntercepts([InterceptType.HOVER]);

if (interaction.isSelectedCell(cell)) {
// https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/detail,使用 detail属性来判断是否是双击,双击时不触发选择态reset
// https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/detail,使用 detail 属性来判断是否是双击,双击时不触发选择态 reset
if ((event.originalEvent as UIEvent)?.detail === 1) {
interaction.reset();

// https://github.com/antvis/S2/issues/2447
this.spreadsheet.emit(
S2Event.GLOBAL_SELECTED,
interaction.getActiveCells(),
);
}
return;
}
Expand Down Expand Up @@ -87,8 +93,8 @@ export class DataCellClick extends BaseEvent implements BaseEventImplement {
meta,
spreadsheet,
);
forEach(allRowHeaderCells, (cell: RowCell) => {
cell.updateByState(InteractionStateName.SELECTED);
forEach(allRowHeaderCells, (rowCell) => {
rowCell.updateByState(InteractionStateName.SELECTED);
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,18 @@ export class DataCellMultiSelection
event.stopPropagation();
const cell: DataCell = this.spreadsheet.getCell(event.target);
const meta = cell.getMeta();
const { interaction, options } = this.spreadsheet;
const { interaction } = this.spreadsheet;

if (this.isMultiSelection && meta) {
const selectedCells = this.getSelectedCells(cell);

if (isEmpty(selectedCells)) {
interaction.clearState();
this.spreadsheet.hideTooltip();
this.spreadsheet.emit(
S2Event.GLOBAL_SELECTED,
interaction.getActiveCells(),
);
return;
}

Expand Down
8 changes: 6 additions & 2 deletions packages/s2-core/src/interaction/event-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,12 @@ export class EventController {
return;
}

this.spreadsheet.emit(S2Event.GLOBAL_RESET, event);
interaction.reset();
this.spreadsheet.emit(S2Event.GLOBAL_RESET, event);
this.spreadsheet.emit(
S2Event.GLOBAL_SELECTED,
interaction.getActiveCells(),
);
}

private isMouseEvent(event: Event): event is MouseEvent {
Expand Down Expand Up @@ -299,7 +303,7 @@ export class EventController {
if (this.isResizeArea(event)) {
this.spreadsheet.emit(S2Event.LAYOUT_RESIZE_MOUSE_DOWN, event);

// 仅捕获在canvas之外触发的事件 https://github.com/antvis/S2/issues/1592
// 仅捕获在 canvas 之外触发的事件 https://github.com/antvis/S2/issues/1592
const resizeMouseMoveCapture = (mouseEvent: MouseEvent) => {
if (!this.spreadsheet.getCanvasElement()) {
return false;
Expand Down

1 comment on commit 07f7201

@vercel
Copy link

@vercel vercel bot commented on 07f7201 Dec 5, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.