import { IStoreState, IRow, ICell, ID } from '../index.data'; import { CellProps, CellPropsNature } from './index.data'; import { TextRenderer, getRenderer } from '../cellTypes'; import ErrorRenderer from '../cellTypes/error/ErrorRenderer'; import { getCellType, isRowNoCell, getRowNoCellValue, getCellFormat, getRowByCell, getRow, isTreeRow, isCellSingleSelected, } from '../store/selectors'; import { Store } from 'unistore'; import { isAbstractFormula } from '../cellTypes/formula/FormulaEditor/NamedFormulaEditor/AbstractCellMapper/helper'; import { Props } from '../SpreadSheetProvider/index.data'; import { getValue } from '../utils'; // #region 单元格 export const getCellPropsNature = ( store: Store, row: IRow, cell: ICell, checkableConfig: Props['checkableConfig'], ): Omit, 'disableFormula'> | null => { const state = store.getState(); const { treeContext } = state; // 与rowIndicator一样, 这里可能先于父组件执行, 在删除时会报空指针异常 if (!cell) return null; const rowType = row.type; const level = isTreeRow(row) ? treeContext[row.id].level : -1; const isLeaf = isTreeRow(row) ? (treeContext[row.id].childrenIds || ([] as ID[])).length === 0 : false; const formulaIsAbstract = !cell.formulaDisabled && isAbstractFormula(cell.formula + ''); const Renderer = // rowType === 'header' || rowType === 'footer' // ? TextRenderer : formulaIsAbstract || cell.error ? ErrorRenderer : getRenderer(getCellType(state, cell.id)); const tdBackground = cell.style && cell.style.background ? cell.style.background : cell.locked ? '#e5e5e5' : undefined; const _value = isRowNoCell(state, cell.id) ? getRowNoCellValue(state, cell.id) : cell.value; const value = getValue(_value, cell!.dataType); return { id: cell.id, rowId: row.id, level, rowspan: cell.rowspan || 1, colspan: cell.colspan || 1, style: { ...cell.style, background: tdBackground, position: 'relative' }, format: getCellFormat(state, cell.id), value, error: cell.error as string, formulaIsAbstract, Renderer: Renderer as any, // isSelected: isCellSingleSelected(state, cell.id), // 是否被选中, 主要用于2-5单元格 checkable: !!checkableConfig && checkableConfig.checkable(cell, row), isChecked: !!checkableConfig && checkableConfig.isChecked(cell, row), rowAdditions: row.additions, locked: cell.locked, isCellSelected: id => { const state = store.getState(); return isCellSingleSelected(state, cell.id); }, getRow: rowId => { const state = store.getState(); return getRow(state, rowId); }, }; };