import { IStoreState, ID, IRow } from '../index.data'; import { ICommand } from 'valor-unistore-undo'; import { getRow } from '../store/selectors'; import { setRowData } from '../store/actions/row'; import { SpreadSheetRuntime } from '../RuntimeContext'; /** * patch某个row * 暂时不允许修改row.cells * 目前仅有的需求: 修改additions */ class SetRowDataCommand extends ICommand, IRow, SpreadSheetRuntime> { id?: ID; execute = () => { const state = this.store.getState(); const { isActiving, selectionType, selectedRowRange } = state; let id: ID | null = null; if (!this.params!.id) { // 若未指定id, 则选当前rowId // 1. 若正在编辑, 则放弃修改 if (isActiving) return false; // 2. 非单选行, 不允许改 if (selectionType !== 'row' || selectedRowRange[0] !== selectedRowRange[1]) return false; id = this.id || selectedRowRange[0]; } else { // 已指定id , 表示强制改 id = this.id || this.params.id; } // 用于undo/redo this.id = id; this.undoContext = { ...getRow(state, id) }; this.do(); return true; }; undo = () => { const patch = setRowData(this.store.getState(), this.id!, this.undoContext, true); this.store.setState(patch as any); }; do = () => { const patch = setRowData(this.store.getState(), this.id!, this.params, false); this.store.setState(patch as any); }; } export default SetRowDataCommand;