@antv/s2
Version:
effective spreadsheet render core lib
95 lines • 3.8 kB
JavaScript
import { isEmpty } from 'lodash';
import { CellType, InteractionName, InteractionStateName, InterceptType, S2Event, } from '../common/constant';
import { afterSelectDataCells, getCellMeta, isMouseEventWithMeta, isMultiSelectionKey, } from '../utils/interaction/select-event';
import { getCellsTooltipData } from '../utils/tooltip';
import { BaseEvent } from './base-interaction';
export class DataCellMultiSelection extends BaseEvent {
constructor() {
super(...arguments);
this.isMultiSelection = false;
}
bindEvents() {
this.bindKeyboardDown();
this.bindDataCellClick();
this.bindKeyboardUp();
this.bindMouseMove();
}
reset() {
this.isMultiSelection = false;
this.spreadsheet.interaction.removeIntercepts([InterceptType.CLICK]);
}
bindKeyboardDown() {
this.spreadsheet.on(S2Event.GLOBAL_KEYBOARD_DOWN, (event) => {
if (isMultiSelectionKey(event)) {
this.isMultiSelection = true;
this.spreadsheet.interaction.addIntercepts([InterceptType.CLICK]);
}
});
}
bindKeyboardUp() {
this.spreadsheet.on(S2Event.GLOBAL_KEYBOARD_UP, (event) => {
if (isMultiSelectionKey(event)) {
this.reset();
}
});
}
bindMouseMove() {
this.spreadsheet.on(S2Event.GLOBAL_MOUSE_MOVE, (event) => {
// 当快捷键被系统拦截后,按需补充调用一次 reset
if (this.isMultiSelection && !isMouseEventWithMeta(event)) {
this.reset();
}
});
}
getSelectedCells(cell) {
const id = cell.getMeta().id;
const { interaction } = this.spreadsheet;
let selectedCells = interaction.getCells([CellType.DATA_CELL]);
let cells = [];
if (interaction.getCurrentStateName() !== InteractionStateName.SELECTED) {
selectedCells = [];
}
if (selectedCells.find((meta) => meta.id === id)) {
cells = selectedCells.filter((item) => item.id !== id);
}
else {
cells = [...selectedCells, getCellMeta(cell)];
}
return cells;
}
bindDataCellClick() {
this.spreadsheet.on(S2Event.DATA_CELL_CLICK, (event) => {
event.stopPropagation();
const cell = this.spreadsheet.getCell(event.target);
const meta = cell.getMeta();
const { interaction } = this.spreadsheet;
if (this.isMultiSelection && meta) {
const selectedCells = this.getSelectedCells(cell);
if (isEmpty(selectedCells)) {
interaction.clearState();
this.spreadsheet.hideTooltip();
interaction.emitSelectEvent({
event,
targetCell: cell,
interactionName: InteractionName.DATA_CELL_MULTI_SELECTION,
});
return;
}
interaction.addIntercepts([InterceptType.CLICK, InterceptType.HOVER]);
this.spreadsheet.hideTooltip();
interaction.changeState({
cells: selectedCells,
stateName: InteractionStateName.SELECTED,
onUpdateCells: afterSelectDataCells,
});
interaction.emitSelectEvent({
event,
targetCell: cell,
interactionName: InteractionName.DATA_CELL_MULTI_SELECTION,
});
this.spreadsheet.showTooltipWithInfo(event, getCellsTooltipData(this.spreadsheet));
}
});
}
}
//# sourceMappingURL=data-cell-multi-selection.js.map