@antv/s2
Version:
effective spreadsheet render core lib
98 lines • 4.11 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ColCellBrushSelection = void 0;
const lodash_1 = require("lodash");
const constant_1 = require("../../common/constant");
const interaction_1 = require("../../common/constant/interaction");
const select_event_1 = require("../../utils/interaction/select-event");
const base_brush_selection_1 = require("./base-brush-selection");
class ColCellBrushSelection extends base_brush_selection_1.BaseBrushSelection {
constructor() {
super(...arguments);
this.displayedCells = [];
this.brushRangeCells = [];
this.onUpdateCells = (root) => root.updateCells(this.spreadsheet.facet.getColCells());
}
bindEvents() {
this.bindMouseDown();
this.bindMouseMove();
this.bindMouseUp();
}
bindMouseDown() {
this.spreadsheet.on(constant_1.S2Event.COL_CELL_MOUSE_DOWN, (event) => {
if (!this.spreadsheet.interaction.getBrushSelection().colCell) {
return;
}
super.mouseDown(event);
});
}
isPointInCanvas(point) {
// 获取列头的区域范围
const { width: maxX } = this.spreadsheet.facet.getCanvasSize();
const { width: minX, minY, maxY } = this.spreadsheet.facet.cornerBBox;
return (point.x >= minX && point.x <= maxX && point.y >= minY && point.y <= maxY);
}
bindMouseMove() {
this.spreadsheet.on(constant_1.S2Event.COL_CELL_MOUSE_MOVE, (event) => {
var _a;
if (this.brushSelectionStage === interaction_1.InteractionBrushSelectionStage.UN_DRAGGED) {
return;
}
this.setBrushSelectionStage(interaction_1.InteractionBrushSelectionStage.DRAGGED);
const pointInCanvas = (_a = this.spreadsheet.interaction.eventController) === null || _a === void 0 ? void 0 : _a.getViewportPoint(event);
if (!this.isPointInCanvas(pointInCanvas)) {
return;
}
this.renderPrepareSelected(pointInCanvas);
});
}
setDisplayedCells() {
this.displayedCells = this.spreadsheet.facet.getColCells();
}
/**
* 用户判断 colCell 是否在当前刷选的范围内
* @param meta colCell 位置等属性存储的对象
* @returns boolean
*/
isInBrushRange(meta) {
const { start, end } = this.getBrushRange();
const { scrollX = 0 } = this.spreadsheet.facet.getScrollOffset();
const cornerBBox = this.spreadsheet.facet.cornerBBox;
const { x = 0, y = 0, width = 0, height = 0 } = meta;
return this.rectanglesIntersect({
// 由于刷选的时候,是以列头的左上角为起点,所以需要减去角头的宽度,在滚动后需要加上滚动条的偏移量
minX: start.x - cornerBBox.width + scrollX,
minY: start.y,
maxX: end.x - cornerBBox.width + scrollX,
maxY: end.y,
}, {
minX: x,
maxX: x + width,
minY: y,
maxY: y + height,
});
}
// 最终刷选的 cell
updateSelectedCells(event) {
const { interaction, facet } = this.spreadsheet;
interaction.changeState({
cells: (0, lodash_1.map)(this.brushRangeCells, select_event_1.getCellMeta),
onUpdateCells: (root) => {
root.updateCells(facet.getColCells());
},
stateName: interaction_1.InteractionStateName.COL_CELL_BRUSH_SELECTED,
});
this.emitBrushSelectionEvent(constant_1.S2Event.COL_CELL_BRUSH_SELECTION, this.brushRangeCells, {
event,
targetCell: this.brushRangeCells[0],
interactionName: interaction_1.InteractionName.COL_CELL_BRUSH_SELECTION,
});
}
addBrushIntercepts() {
this.spreadsheet.interaction.addIntercepts([
constant_1.InterceptType.COL_CELL_BRUSH_SELECTION,
]);
}
}
exports.ColCellBrushSelection = ColCellBrushSelection;
//# sourceMappingURL=col-brush-selection.js.map