import { ICommand } from 'valor-unistore-undo'; import { SpreadSheetRuntime } from '../RuntimeContext'; import { IStoreState } from '../index.data'; import { setSelection } from '../store/actions/selection'; import { ID, ISelection } from '..'; export interface SelectCellCommandParams { id: ID; } interface IUndoContext { lastSelection: ISelection | null; } class SelectCellCommand extends ICommand< IStoreState, SelectCellCommandParams, IUndoContext, SpreadSheetRuntime > { execute = () => { const state = this.store.getState(); this.undoContext.lastSelection = { selectionType: state.selectionType, selectedCellRange: state.selectedCellRange, selectedRowRange: state.selectedRowRange, selectedColumnRange: state.selectedColumnRange, }; const patchState = setSelection(this.store.getState(), 'cell', this.params.id); this.store.setState(patchState as IStoreState); return true; }; undo = () => { if (!this.undoContext.lastSelection) return; this.store.setState(this.undoContext.lastSelection as IStoreState); }; } export default SelectCellCommand;