@antv/s2
Version:
effective spreadsheet render core lib
98 lines • 3.86 kB
JavaScript
import { groupBy, map, mapValues, reduce, uniqBy } from 'lodash';
import { TableSeriesNumberCell } from '../../cell';
import { CellType, InteractionKeyboardKey } from '../../common/constant';
import { getDataCellId } from '../cell/data-cell';
import { getActiveHoverHeaderCells } from './hover-event';
export const isMultiSelectionKey = (e) => [InteractionKeyboardKey.META, InteractionKeyboardKey.CONTROL].includes(e.key);
export const isMouseEventWithMeta = (e) => {
return e.ctrlKey || e.metaKey;
};
export const getCellMeta = (cell) => {
const meta = cell.getMeta();
const { id, colIndex, rowIndex, rowQuery } = meta || {};
return {
id,
colIndex,
rowIndex,
rowQuery,
type: cell instanceof TableSeriesNumberCell ? CellType.ROW_CELL : cell.cellType,
};
};
export 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,
},
};
}
export 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 = getDataCellId(String(meta.rowIndex), colId);
const result = [];
const rowCell = facet.getCellById(id);
if (rowCell && rowCell instanceof TableSeriesNumberCell) {
result.push(rowCell);
}
return result;
}
return getActiveHoverHeaderCells(meta.rowId, facet.getRowCells(), spreadsheet.isHierarchyTreeType());
}
export const getRowHeaderByCellId = (cellId, s2) => s2.facet.getRowNodes().filter((node) => cellId.includes(node.id));
export const getColHeaderByCellId = (cellId, s2) => s2.facet.getColNodes().filter((node) => cellId.includes(node.id));
export const getInteractionCells = (cell, s2) => {
const { colHeader, rowHeader } = s2.interaction.getSelectedCellHighlight();
const headerGetters = [
{
shouldGet: rowHeader,
getter: getRowHeaderByCellId,
},
{
shouldGet: colHeader,
getter: getColHeaderByCellId,
},
];
const selectedHeaderCells = headerGetters
.filter((item) => item.shouldGet)
.reduce((acc, i) => [...acc, ...i.getter(cell.id, s2)], [])
.filter((node) => !!node.belongsCell)
.map((node) => getCellMeta(node.belongsCell));
return [cell, ...selectedHeaderCells];
};
export const getInteractionCellsBySelectedCells = (selectedCells, s2) => {
const headerSelectedCell = reduce(selectedCells, (_cells, selectedCell) => [
..._cells,
...getInteractionCells(selectedCell, s2),
], []);
// headerSelectedCell 会有重复的 cell,在这里统一去重
return uniqBy([...selectedCells, ...headerSelectedCell], 'id');
};
export 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();
};
export const groupSelectedCells = (selectedCells) => {
return mapValues(groupBy(selectedCells, 'type'), (cells) => map(cells, 'id'));
};
//# sourceMappingURL=select-event.js.map