import { ICommand } from 'valor-unistore-undo'; import { IStoreState, ID, ICellNN } from '../index.data'; import { setCellStyle } from '../store/actions'; import { idMap } from 'valor-app-utils'; import { getSelectedCellIds, isCellSingleSelected } from '../store/selectors'; import { SpreadSheetRuntime } from '../RuntimeContext'; class SetCellStyleCommand extends ICommand< IStoreState, { style: React.CSSProperties }, Record, SpreadSheetRuntime > { // id?: ID; ids?: ID[]; execute = () => { const state = this.store.getState(); const { isActiving, selectionType, selectedCellRange } = state; // 如果正在编辑, 不允许改样式 if (isActiving) return false; // 非单选单元格, 不允许改样式 // if (selectionType !== 'cell' || selectedCellRange[0] !== selectedCellRange[1]) return false; // update: 支持多选单元格 if (selectionType !== 'cell') return false; // 用于undo / redo const ids: ID[] = getSelectedCellIds(state); this.ids = ids; this.undoContext = idMap(this.ids.map(id => ({ id, style: { ...state.cells[id]!.style } }))); this.do(); return true; }; undo = () => { const patch = setCellStyle( this.store.getState(), this.ids!, this.undoContext[this.ids![0]].style, true, ); this.store.setState(patch); this.restoreSelection(); }; do = () => { const patch = setCellStyle(this.store.getState(), this.ids!, this.params.style); this.store.setState(patch); }; restoreSelection = () => { // 如果当前单元格已被选中, 则直接返回 if (isCellSingleSelected(this.store.getState(), this.ids![0])) return; this.runtime!.fsmService.send({ type: 'MOUSE.DOWN', selectedCell: this.ids![0] }); }; } export default SetCellStyleCommand;