diff --git a/packages/s2-core/src/interaction/root.ts b/packages/s2-core/src/interaction/root.ts index 301901326a..73d9a73c46 100644 --- a/packages/s2-core/src/interaction/root.ts +++ b/packages/s2-core/src/interaction/root.ts @@ -445,8 +445,9 @@ export class RootInteraction { } public clearState() { - clearState(this.spreadsheet); - this.draw(); + if (clearState(this.spreadsheet)) { + this.draw(); + } } public changeState(interactionStateInfo: InteractionStateInfo) { diff --git a/packages/s2-core/src/utils/interaction/state-controller.ts b/packages/s2-core/src/utils/interaction/state-controller.ts index 822198918f..0365fdaf52 100644 --- a/packages/s2-core/src/utils/interaction/state-controller.ts +++ b/packages/s2-core/src/utils/interaction/state-controller.ts @@ -7,31 +7,37 @@ import type { SpreadSheet } from '../../sheet-type'; * @desc clear the interaction state information * @param spreadsheet sheet instance */ -export const clearState = (spreadsheet: SpreadSheet) => { +export const clearState = (spreadsheet: SpreadSheet): boolean => { const activeIcons = spreadsheet.store.get('visibleActionIcons'); + const allInteractedCells = spreadsheet.interaction.getInteractedCells(); + const cellMetas = spreadsheet.interaction.getState().cells; + // 如果都处于初始化状态 不需要clear + if ( + isEmpty(allInteractedCells) && + isEmpty(cellMetas) && + isEmpty(activeIcons) + ) { + return false; + } forEach(activeIcons, (icon) => { icon.set('visible', false); }); spreadsheet.store.set('visibleActionIcons', []); - const allInteractedCells = spreadsheet.interaction.getInteractedCells(); - const cellMetas = spreadsheet.interaction.getState().cells; - - if (!isEmpty(allInteractedCells) || !isEmpty(cellMetas)) { - forEach(allInteractedCells, (cell: S2CellType) => { - cell.hideInteractionShape(); - }); + forEach(allInteractedCells, (cell: S2CellType) => { + cell.hideInteractionShape(); + }); - spreadsheet.interaction.resetState(); - if (spreadsheet.options.interaction.selectedCellsSpotlight) { - const unSelectedCells = - spreadsheet.interaction.getPanelGroupAllUnSelectedDataCells() || []; + spreadsheet.interaction.resetState(); + if (spreadsheet.options.interaction.selectedCellsSpotlight) { + const unSelectedCells = + spreadsheet.interaction.getPanelGroupAllUnSelectedDataCells() || []; - forEach(unSelectedCells, (cell) => { - cell.clearUnselectedState(); - }); - } + forEach(unSelectedCells, (cell) => { + cell.clearUnselectedState(); + }); } + return true; }; /**