@antv/s2
Version:
effective spreadsheet render core lib
117 lines • 5.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataCellBrushSelection = void 0;
const lodash_1 = require("lodash");
const constant_1 = require("../../common/constant");
const interaction_1 = require("../../common/constant/interaction");
const utils_1 = require("../../utils");
const select_event_1 = require("../../utils/interaction/select-event");
const base_brush_selection_1 = require("./base-brush-selection");
class DataCellBrushSelection extends base_brush_selection_1.BaseBrushSelection {
constructor() {
super(...arguments);
this.displayedCells = [];
this.brushRangeCells = [];
this.getSelectedCellMetas = (brushRange) => {
const { facet } = this.spreadsheet;
const metas = [];
const rowLeafNodes = facet.getRowLeafNodes();
const colLeafNodes = facet.getColLeafNodes();
const rowIndexRange = (0, lodash_1.range)(brushRange.start.rowIndex, brushRange.end.rowIndex + 1);
const colIndexRange = (0, lodash_1.range)(brushRange.start.colIndex, brushRange.end.colIndex + 1);
rowIndexRange.forEach((rowIndex) => {
colIndexRange.forEach((colIndex) => {
const colId = String(colLeafNodes[colIndex].id);
const rowId = (0, lodash_1.isEmpty)(rowLeafNodes)
? String(rowIndex)
: String(rowLeafNodes[rowIndex].id);
metas.push({
colIndex,
rowIndex,
id: (0, utils_1.getDataCellId)(rowId, colId),
type: interaction_1.CellType.DATA_CELL,
rowId,
colId,
spreadsheet: this.spreadsheet,
});
});
});
return metas;
};
}
bindMouseDown() {
this.spreadsheet.on(constant_1.S2Event.DATA_CELL_MOUSE_DOWN, (event) => {
if (!this.spreadsheet.interaction.getBrushSelection().dataCell) {
return;
}
super.mouseDown(event);
this.resetScrollDelta();
});
}
bindMouseMove() {
this.spreadsheet.on(constant_1.S2Event.GLOBAL_MOUSE_MOVE, (event) => {
if (this.brushSelectionStage === interaction_1.InteractionBrushSelectionStage.UN_DRAGGED) {
return;
}
this.setBrushSelectionStage(interaction_1.InteractionBrushSelectionStage.DRAGGED);
const pointInCanvas = this.spreadsheet.interaction.eventController.getViewportPoint(event);
if (this.autoBrushScroll(pointInCanvas)) {
return;
}
this.renderPrepareSelected(pointInCanvas);
});
}
isInBrushRange(meta) {
const { start, end } = this.getBrushRange();
const { rowIndex, colIndex } = meta;
return (rowIndex >= start.rowIndex &&
rowIndex <= end.rowIndex &&
colIndex >= start.colIndex &&
colIndex <= end.colIndex);
}
// 最终刷选的 cell
updateSelectedCells(event) {
const brushRange = this.getBrushRange();
const selectedCellMetas = this.getSelectedCellMetas(brushRange);
this.spreadsheet.interaction.changeState({
cells: selectedCellMetas,
stateName: interaction_1.InteractionStateName.DATA_CELL_BRUSH_SELECTED,
onUpdateCells: select_event_1.afterSelectDataCells,
});
const scrollBrushRangeCells = this.getScrollBrushRangeCells(selectedCellMetas);
this.emitBrushSelectionEvent(constant_1.S2Event.DATA_CELL_BRUSH_SELECTION, scrollBrushRangeCells, {
event,
targetCell: scrollBrushRangeCells[0],
interactionName: interaction_1.InteractionName.DATA_CELL_BRUSH_SELECTION,
});
}
/**
* @name 获取刷选 (含滚动后不再可视范围内) 的单元格
* @description DataCell 存在滚动刷选, 由于按需加载的特性, 非可视范围内的单元格已被注销
* 如果在可视范围, 直接返回 DataCell, 非可视范围, 由于实例已被销毁, 构造实例后返回
*/
getScrollBrushRangeCells(selectedCellMetas) {
return selectedCellMetas.map((meta) => {
const visibleCell = this.getVisibleBrushRangeCells(meta.id);
if (visibleCell) {
return visibleCell;
}
const viewMeta = this.spreadsheet.facet.getCellMeta(meta.rowIndex, meta.colIndex);
return this.spreadsheet.facet.createDataCell(viewMeta);
});
}
bindMouseUp() {
super.bindMouseUp(true);
}
getPrepareSelectMaskPosition(brushRange) {
const { minX, minY } = this.spreadsheet.facet.panelBBox;
const x = Math.max(brushRange.start.x, minX);
const y = Math.max(brushRange.start.y, minY);
return {
x,
y,
};
}
}
exports.DataCellBrushSelection = DataCellBrushSelection;
//# sourceMappingURL=data-cell-brush-selection.js.map