import { ICommand } from 'valor-unistore-undo'; import { IStoreState, IRow } from '../index.data'; import { pushTreeRow } from '../store/actions/row'; import { getCurrentRowIndex, restoreSelection } from './helper'; import * as R from 'rambda'; import { isTreeRow } from '../store/selectors'; import { IRowWithCell } from '../SpreadSheetProvider/index.data'; import { SpreadSheetRuntime } from '../RuntimeContext'; export interface PushRowCommandParams { type: 'left' | 'right'; helpers: { normalizeRow: (rows: IRow[], row: IRowWithCell) => IRowWithCell; }; } class PushRowCommand extends ICommand { // 操作的行号 i?: number; execute = () => { const { type } = this.params; const state = this.store.getState(); const i = R.is(Number, this.i) ? this.i! : getCurrentRowIndex(state); this.i = i; if (i < 0) return false; const row = state.rows[i]; if (row.sealed || !isTreeRow(row)) return false; const newState = pushTreeRow(state, type, i, this.params.helpers, this.runtime!); if (!newState) return false; // 无法升级 this.store.setState(newState); // 收尾 return true; }; undo = () => { const state = this.store.getState(); const newState = pushTreeRow( state, this.params.type === 'left' ? 'right' : 'left', this.i!, this.params.helpers, this.runtime!, )!; this.store.setState(newState); restoreSelection(this.store, newState['rows'][this.i!].cellIds![0]!, this.runtime!); }; } export default PushRowCommand;