@antv/s2
Version:
effective spreadsheet render core lib
112 lines • 4.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.groupSelectedCells = exports.afterSelectDataCells = exports.getInteractionCellsBySelectedCells = exports.getInteractionCells = exports.getColHeaderByCellId = exports.getRowHeaderByCellId = exports.getRowCellForSelectedCell = exports.getRangeIndex = exports.getCellMeta = exports.isMouseEventWithMeta = exports.isMultiSelectionKey = void 0;
const lodash_1 = require("lodash");
const cell_1 = require("../../cell");
const constant_1 = require("../../common/constant");
const data_cell_1 = require("../cell/data-cell");
const hover_event_1 = require("./hover-event");
const isMultiSelectionKey = (e) => [constant_1.InteractionKeyboardKey.META, constant_1.InteractionKeyboardKey.CONTROL].includes(e.key);
exports.isMultiSelectionKey = isMultiSelectionKey;
const isMouseEventWithMeta = (e) => {
return e.ctrlKey || e.metaKey;
};
exports.isMouseEventWithMeta = isMouseEventWithMeta;
const getCellMeta = (cell) => {
const meta = cell.getMeta();
const { id, colIndex, rowIndex, rowQuery } = meta || {};
return {
id,
colIndex,
rowIndex,
rowQuery,
type: cell instanceof cell_1.TableSeriesNumberCell ? constant_1.CellType.ROW_CELL : cell.cellType,
};
};
exports.getCellMeta = getCellMeta;
function getRangeIndex(start, end) {
const minRowIndex = Math.min(start.rowIndex, end.rowIndex);
const maxRowIndex = Math.max(start.rowIndex, end.rowIndex);
const minColIndex = Math.min(start.colIndex, end.colIndex);
const maxColIndex = Math.max(start.colIndex, end.colIndex);
return {
start: {
rowIndex: minRowIndex,
colIndex: minColIndex,
},
end: {
rowIndex: maxRowIndex,
colIndex: maxColIndex,
},
};
}
exports.getRangeIndex = getRangeIndex;
function getRowCellForSelectedCell(meta, spreadsheet) {
var _a;
const { facet, options } = spreadsheet;
if (spreadsheet.isTableMode()) {
if (!((_a = options.seriesNumber) === null || _a === void 0 ? void 0 : _a.enable)) {
return [];
}
const colId = facet.getColLeafNodes()[0].id;
const id = (0, data_cell_1.getDataCellId)(String(meta.rowIndex), colId);
const result = [];
const rowCell = facet.getCellById(id);
if (rowCell && rowCell instanceof cell_1.TableSeriesNumberCell) {
result.push(rowCell);
}
return result;
}
return (0, hover_event_1.getActiveHoverHeaderCells)(meta.rowId, facet.getRowCells(), spreadsheet.isHierarchyTreeType());
}
exports.getRowCellForSelectedCell = getRowCellForSelectedCell;
const getRowHeaderByCellId = (cellId, s2) => s2.facet.getRowNodes().filter((node) => cellId.includes(node.id));
exports.getRowHeaderByCellId = getRowHeaderByCellId;
const getColHeaderByCellId = (cellId, s2) => s2.facet.getColNodes().filter((node) => cellId.includes(node.id));
exports.getColHeaderByCellId = getColHeaderByCellId;
const getInteractionCells = (cell, s2) => {
const { colHeader, rowHeader } = s2.interaction.getSelectedCellHighlight();
const headerGetters = [
{
shouldGet: rowHeader,
getter: exports.getRowHeaderByCellId,
},
{
shouldGet: colHeader,
getter: exports.getColHeaderByCellId,
},
];
const selectedHeaderCells = headerGetters
.filter((item) => item.shouldGet)
.reduce((acc, i) => [...acc, ...i.getter(cell.id, s2)], [])
.filter((node) => !!node.belongsCell)
.map((node) => (0, exports.getCellMeta)(node.belongsCell));
return [cell, ...selectedHeaderCells];
};
exports.getInteractionCells = getInteractionCells;
const getInteractionCellsBySelectedCells = (selectedCells, s2) => {
const headerSelectedCell = (0, lodash_1.reduce)(selectedCells, (_cells, selectedCell) => [
..._cells,
...(0, exports.getInteractionCells)(selectedCell, s2),
], []);
// headerSelectedCell 会有重复的 cell,在这里统一去重
return (0, lodash_1.uniqBy)([...selectedCells, ...headerSelectedCell], 'id');
};
exports.getInteractionCellsBySelectedCells = getInteractionCellsBySelectedCells;
const afterSelectDataCells = (root, updateDataCells) => {
const { facet } = root.spreadsheet;
const { colHeader, rowHeader } = root.getSelectedCellHighlight();
if (colHeader) {
root.updateCells(facet.getColCells());
}
if (rowHeader) {
root.updateCells(facet.getRowCells());
}
updateDataCells();
};
exports.afterSelectDataCells = afterSelectDataCells;
const groupSelectedCells = (selectedCells) => {
return (0, lodash_1.mapValues)((0, lodash_1.groupBy)(selectedCells, 'type'), (cells) => (0, lodash_1.map)(cells, 'id'));
};
exports.groupSelectedCells = groupSelectedCells;
//# sourceMappingURL=select-event.js.map