From 699343b2d66bacdf86cfba8f6238fb0c7fa4ddbe Mon Sep 17 00:00:00 2001 From: lijinke666 Date: Tue, 5 Dec 2023 14:36:28 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix(interaction):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=95=B0=E5=80=BC=E5=8D=95=E5=85=83=E6=A0=BC=E5=8F=96=E6=B6=88?= =?UTF-8?q?=E9=80=89=E4=B8=AD=20&=20=E7=82=B9=E5=87=BB=E7=A9=BA=E7=99=BD?= =?UTF-8?q?=E5=A4=84=E5=8F=96=E6=B6=88=E9=80=89=E4=B8=AD=E6=97=B6=E6=B2=A1?= =?UTF-8?q?=E8=A7=A6=E5=8F=91=20GLOBAL=5FSELECTED=20=E4=BA=8B=E4=BB=B6=20c?= =?UTF-8?q?lose=20#2447?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../click/data-cell-click-spec.ts | 21 +++++++++++++++++++ .../click/row-column-click-spec.ts | 6 ++---- .../unit/interaction/event-controller-spec.ts | 8 +++++++ .../__tests__/unit/interaction/root-spec.ts | 7 +++---- .../s2-core/src/facet/header/pivot-row.ts | 1 + .../base-interaction/click/data-cell-click.ts | 12 ++++++++--- .../interaction/data-cell-multi-selection.ts | 6 +++++- .../src/interaction/event-controller.ts | 8 +++++-- 8 files changed, 55 insertions(+), 14 deletions(-) diff --git a/packages/s2-core/__tests__/unit/interaction/base-interaction/click/data-cell-click-spec.ts b/packages/s2-core/__tests__/unit/interaction/base-interaction/click/data-cell-click-spec.ts index 65827b8434..4572641696 100644 --- a/packages/s2-core/__tests__/unit/interaction/base-interaction/click/data-cell-click-spec.ts +++ b/packages/s2-core/__tests__/unit/interaction/base-interaction/click/data-cell-click-spec.ts @@ -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, @@ -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(); diff --git a/packages/s2-core/__tests__/unit/interaction/base-interaction/click/row-column-click-spec.ts b/packages/s2-core/__tests__/unit/interaction/base-interaction/click/row-column-click-spec.ts index d4d1629deb..c71eff6e1e 100644 --- a/packages/s2-core/__tests__/unit/interaction/base-interaction/click/row-column-click-spec.ts +++ b/packages/s2-core/__tests__/unit/interaction/base-interaction/click/row-column-click-spec.ts @@ -132,9 +132,9 @@ describe('Interaction Row & Column Cell Click Tests', () => { const selected = jest.fn(); s2.on(S2Event.GLOBAL_SELECTED, selected); - const isSelectedCellSpy = jest + jest .spyOn(s2.interaction, 'isSelectedCell') - .mockImplementation(() => false); + .mockImplementationOnce(() => false); s2.emit(event, { stopPropagation() {}, @@ -148,8 +148,6 @@ describe('Interaction Row & Column Cell Click Tests', () => { expect(s2.showTooltipWithInfo).toHaveBeenCalled(); expect(selected).toHaveBeenCalled(); expect(s2.interaction.hasIntercepts([InterceptType.HOVER])).toBeTrue(); - - isSelectedCellSpy.mockRestore(); }, ); diff --git a/packages/s2-core/__tests__/unit/interaction/event-controller-spec.ts b/packages/s2-core/__tests__/unit/interaction/event-controller-spec.ts index 0659ebe1ee..31b382b261 100644 --- a/packages/s2-core/__tests__/unit/interaction/event-controller-spec.ts +++ b/packages/s2-core/__tests__/unit/interaction/event-controller-spec.ts @@ -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', { @@ -516,6 +519,7 @@ describe('Interaction Event Controller Tests', () => { } as MouseEventInit), ); + expect(selected).toHaveBeenCalledWith([]); expect(reset).toHaveBeenCalled(); expect(spreadsheet.interaction.reset).toHaveBeenCalled(); }); @@ -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(); }); diff --git a/packages/s2-core/__tests__/unit/interaction/root-spec.ts b/packages/s2-core/__tests__/unit/interaction/root-spec.ts index 047935b86d..8869724d41 100644 --- a/packages/s2-core/__tests__/unit/interaction/root-spec.ts +++ b/packages/s2-core/__tests__/unit/interaction/root-spec.ts @@ -26,6 +26,7 @@ import { DataCellBrushSelection, ColBrushSelection, RowBrushSelection, + S2Event, } from '@/index'; import { RootInteraction } from '@/interaction/root'; import { mergeCell, unmergeCell } from '@/utils/interaction/merge-cell'; @@ -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; @@ -204,8 +205,6 @@ describe('RootInteraction Tests', () => { // 取消选中 expect(rootInteraction.getState().cells).toEqual([getCellMeta(mockCellA)]); - - isEqualStateNameSpy.mockRestore(); }); test('should call merge cells', () => { diff --git a/packages/s2-core/src/facet/header/pivot-row.ts b/packages/s2-core/src/facet/header/pivot-row.ts index 493bd13772..ea8a23463b 100644 --- a/packages/s2-core/src/facet/header/pivot-row.ts +++ b/packages/s2-core/src/facet/header/pivot-row.ts @@ -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, diff --git a/packages/s2-core/src/interaction/base-interaction/click/data-cell-click.ts b/packages/s2-core/src/interaction/base-interaction/click/data-cell-click.ts index f1b351d15c..e74115150c 100644 --- a/packages/s2-core/src/interaction/base-interaction/click/data-cell-click.ts +++ b/packages/s2-core/src/interaction/base-interaction/click/data-cell-click.ts @@ -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; } @@ -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); }); } } diff --git a/packages/s2-core/src/interaction/data-cell-multi-selection.ts b/packages/s2-core/src/interaction/data-cell-multi-selection.ts index b1ae87c6c7..5b2249209a 100644 --- a/packages/s2-core/src/interaction/data-cell-multi-selection.ts +++ b/packages/s2-core/src/interaction/data-cell-multi-selection.ts @@ -86,7 +86,7 @@ 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); @@ -94,6 +94,10 @@ export class DataCellMultiSelection if (isEmpty(selectedCells)) { interaction.clearState(); this.spreadsheet.hideTooltip(); + this.spreadsheet.emit( + S2Event.GLOBAL_SELECTED, + interaction.getActiveCells(), + ); return; } diff --git a/packages/s2-core/src/interaction/event-controller.ts b/packages/s2-core/src/interaction/event-controller.ts index a1258710b1..1632255142 100644 --- a/packages/s2-core/src/interaction/event-controller.ts +++ b/packages/s2-core/src/interaction/event-controller.ts @@ -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 { @@ -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; From dd7fc5c453df21ff70f41927f29f186e3e0c6f3f Mon Sep 17 00:00:00 2001 From: lijinke666 Date: Tue, 5 Dec 2023 15:11:25 +0800 Subject: [PATCH 2/2] =?UTF-8?q?test:=20=E4=BF=AE=E5=A4=8D=E5=8D=95?= =?UTF-8?q?=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../base-interaction/click/row-column-click-spec.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/s2-core/__tests__/unit/interaction/base-interaction/click/row-column-click-spec.ts b/packages/s2-core/__tests__/unit/interaction/base-interaction/click/row-column-click-spec.ts index c71eff6e1e..d4d1629deb 100644 --- a/packages/s2-core/__tests__/unit/interaction/base-interaction/click/row-column-click-spec.ts +++ b/packages/s2-core/__tests__/unit/interaction/base-interaction/click/row-column-click-spec.ts @@ -132,9 +132,9 @@ describe('Interaction Row & Column Cell Click Tests', () => { const selected = jest.fn(); s2.on(S2Event.GLOBAL_SELECTED, selected); - jest + const isSelectedCellSpy = jest .spyOn(s2.interaction, 'isSelectedCell') - .mockImplementationOnce(() => false); + .mockImplementation(() => false); s2.emit(event, { stopPropagation() {}, @@ -148,6 +148,8 @@ describe('Interaction Row & Column Cell Click Tests', () => { expect(s2.showTooltipWithInfo).toHaveBeenCalled(); expect(selected).toHaveBeenCalled(); expect(s2.interaction.hasIntercepts([InterceptType.HOVER])).toBeTrue(); + + isSelectedCellSpy.mockRestore(); }, );