import { ICommand } from 'valor-unistore-undo'; import { IStoreState, ID, ICell, ICellNN } from '../index.data'; import { setCellData } from '../store/actions'; import { restoreSelection } from './helper'; import { SpreadSheetRuntime } from '../RuntimeContext'; class SetCellDataCommand extends ICommand< IStoreState, Partial, ICellNN, SpreadSheetRuntime > { id?: ID; execute = () => { const state = this.store.getState(); const { isActiving, selectionType, selectedCellRange } = state; let id: ID | null = null; if (!this.params!.id) { // 如果未指定id, 则选当前cellId // 1. 如果正在编辑, 不允许改内容 if (isActiving) return false; // 2. 非单选单元格, 不允许改内容 if (selectionType !== 'cell' || selectedCellRange[0] !== selectedCellRange[1]) return false; id = this.id || selectedCellRange[0]; } else { // 已指定id, 表示强制改 id = this.id || this.params!.id; } // 用于undo/redo this.id = id; this.undoContext = { ...state.cells[id]! }; this.do(); return true; }; undo = () => { const patch = setCellData( this.store.getState(), this.id!, this.undoContext, true, this.runtime!, ); this.store.setState(patch as any); restoreSelection(this.store, this.id!, this.runtime!); }; do = () => { const patch = setCellData(this.store.getState(), this.id!, this.params, false, this.runtime!); this.store.setState(patch as any); }; } export default SetCellDataCommand;