UNPKG

@antv/s2

Version:

effective spreadsheet render core lib

231 lines 9.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SelectedCellMove = void 0; const common_1 = require("../common"); const constant_1 = require("../common/constant"); const utils_1 = require("../utils"); const select_event_1 = require("../utils/interaction/select-event"); const base_interaction_1 = require("./base-interaction"); const SelectedCellMoveMap = [ constant_1.InteractionKeyboardKey.ARROW_LEFT, constant_1.InteractionKeyboardKey.ARROW_RIGHT, constant_1.InteractionKeyboardKey.ARROW_UP, constant_1.InteractionKeyboardKey.ARROW_DOWN, ]; class SelectedCellMove extends base_interaction_1.BaseEvent { constructor(spreadsheet) { super(spreadsheet); } isCanvasEffect() { return this.spreadsheet.interaction.eventController.isCanvasEffect; } bindEvents() { this.spreadsheet.on(constant_1.S2Event.GLOBAL_KEYBOARD_DOWN, (event) => { if (!this.isCanvasEffect()) { return; } const isShift = event.shiftKey; const isMeta = event.metaKey; const hasDirection = SelectedCellMoveMap.includes(event.key); let changeStartCell = false; let isJumpMode = false; let isSingleSelection = false; if (hasDirection) { if (isMeta && isShift) { // META + SHIFT + Direction changeStartCell = false; isJumpMode = true; isSingleSelection = false; } else if (isMeta) { // META + Direction changeStartCell = true; isJumpMode = true; isSingleSelection = true; } else if (isShift) { // SHIFT + Direction changeStartCell = false; isJumpMode = false; isSingleSelection = false; } else { // Only Direction changeStartCell = true; isJumpMode = false; isSingleSelection = true; } this.handleMove({ event, changeStartCell, isJumpMode, isSingleSelection, }); } }); this.spreadsheet.on(constant_1.S2Event.DATA_CELL_CLICK, (event) => { const cell = this.spreadsheet.getCell(event.target); const cellMeta = cell === null || cell === void 0 ? void 0 : cell.getMeta(); if (cellMeta) { this.startCell = this.getCellMetaByViewMeta(cellMeta); this.endCell = this.startCell; } }); } getCellMetaByViewMeta(meta) { return { rowIndex: meta.rowIndex, colIndex: meta.colIndex, id: meta.id, type: common_1.CellType.DATA_CELL, }; } // core move function handleMove({ event, changeStartCell, isJumpMode, isSingleSelection, }) { const { spreadsheet, startCell, endCell } = this; const cell = changeStartCell ? startCell : endCell; const rowCol = this.getMoveInfo(event.key, cell, isJumpMode); if (!rowCol) { return; } const [rowIndex, colIndex] = [rowCol.row, rowCol.col]; this.scrollToActiveCell(spreadsheet, rowIndex, colIndex); const movedCell = this.generateCellMeta(spreadsheet, rowIndex, colIndex); const selectedCells = isSingleSelection ? [movedCell] : this.getRangeCells(spreadsheet, startCell, movedCell); if (changeStartCell) { this.startCell = movedCell; } this.endCell = movedCell; spreadsheet.interaction.changeState({ stateName: common_1.InteractionStateName.SELECTED, cells: selectedCells, }); spreadsheet.interaction.emitSelectEvent({ event, interactionName: common_1.InteractionName.SELECTED_CELL_MOVE, }); this.spreadsheet.emit(constant_1.S2Event.DATA_CELL_SELECT_MOVE, selectedCells); } generateCellMeta(spreadsheet, row, col) { const { isTableMode, facet } = spreadsheet; const rowLeafNodes = facet.getRowLeafNodes(); const colLeafNodes = facet.getColLeafNodes(); const rowId = isTableMode() ? String(row) : rowLeafNodes[row].id; const colId = colLeafNodes[col].id; return { rowIndex: row, colIndex: col, id: (0, utils_1.getDataCellId)(rowId, colId), type: common_1.CellType.DATA_CELL, }; } getRangeCells(spreadsheet, startCell, endCell) { const { start: { rowIndex: startRowIndex, colIndex: startColIndex }, end: { rowIndex: endRowIndex, colIndex: endColIndex }, } = (0, select_event_1.getRangeIndex)(startCell, endCell); const cells = []; for (let row = startRowIndex; row <= endRowIndex; row++) { for (let col = startColIndex; col <= endColIndex; col++) { cells.push(this.generateCellMeta(spreadsheet, row, col)); } } return cells; } getMoveInfo(code, cell, isJump) { const { spreadsheet } = this; const { rowCount, trailingRowCount, colCount, trailingColCount } = spreadsheet.facet.getFrozenOptions(); const { start, end } = spreadsheet.facet.getCellRange(); const colLeafNodes = spreadsheet.facet.getColLeafNodes(); const [minCol, maxCol] = [ 0 + colCount, colLeafNodes.length - trailingColCount - 1, ]; const [minRow, maxRow] = [start + rowCount, end - trailingRowCount]; if (!cell) { return; } switch (code) { case constant_1.InteractionKeyboardKey.ARROW_RIGHT: if (cell.colIndex + 1 > maxCol) { return; } return { row: cell.rowIndex, col: isJump ? maxCol : cell.colIndex + 1, }; case constant_1.InteractionKeyboardKey.ARROW_LEFT: if (cell.colIndex - 1 < minCol) { return; } return { row: cell.rowIndex, col: isJump ? minCol : cell.colIndex - 1, }; case constant_1.InteractionKeyboardKey.ARROW_UP: if (cell.rowIndex - 1 < minRow) { return; } return { row: isJump ? minRow : cell.rowIndex - 1, col: cell.colIndex, }; case constant_1.InteractionKeyboardKey.ARROW_DOWN: if (cell.rowIndex + 1 > maxRow) { return; } return { row: isJump ? maxRow : cell.rowIndex + 1, col: cell.colIndex, }; default: break; } } // 计算需要滚动的offset calculateOffset(spreadsheet, rowIndex, colIndex) { const facet = spreadsheet.facet; const { rowCount } = facet.getFrozenOptions(); const colLeafNodes = facet.getColLeafNodes(); const { viewportHeight: height, viewportWidth: width } = facet.panelBBox; const frozenGroupAreas = facet.frozenGroupAreas; const frozenColWidth = frozenGroupAreas[common_1.FrozenGroupArea.Col].width; const frozenTrailingColWidth = frozenGroupAreas[common_1.FrozenGroupArea.TrailingCol].width; const frozenTrailingRowHeight = frozenGroupAreas[common_1.FrozenGroupArea.TrailingRow].height; const indexes = facet.panelScrollGroupIndexes; const targetNode = colLeafNodes.find((node) => node.colIndex === colIndex); let offsetX = null; let offsetY = null; // offsetX if (colIndex <= indexes[0]) { // scroll left offsetX = (targetNode === null || targetNode === void 0 ? void 0 : targetNode.x) - frozenColWidth; } else if (colIndex >= indexes[1]) { // scroll right offsetX = (targetNode === null || targetNode === void 0 ? void 0 : targetNode.x) + (targetNode === null || targetNode === void 0 ? void 0 : targetNode.width) - width + frozenTrailingColWidth; } // offsetY if (rowIndex <= indexes[2]) { // scroll top offsetY = facet.viewCellHeights.getCellOffsetY(rowIndex - rowCount); } else if (rowIndex >= indexes[3]) { // scroll bottom const y = facet.viewCellHeights.getCellOffsetY(rowIndex + 1); offsetY = y - height + frozenTrailingRowHeight; } return { offsetX, offsetY }; } scrollToActiveCell(spreadsheet, rowIndex, colIndex) { const { offsetX, offsetY } = this.calculateOffset(spreadsheet, rowIndex, colIndex); const { facet } = spreadsheet; const { scrollX, scrollY } = spreadsheet.facet.getScrollOffset(); facet.scrollWithAnimation({ offsetX: { value: offsetX !== null && offsetX !== void 0 ? offsetX : scrollX }, offsetY: { value: offsetY !== null && offsetY !== void 0 ? offsetY : scrollY }, }); } } exports.SelectedCellMove = SelectedCellMove; //# sourceMappingURL=selected-cell-move.js.map