Skip to content

Commit

Permalink
feat(sheets-ui): check before undo-redo rollback (#4784)
Browse files Browse the repository at this point in the history
  • Loading branch information
weird94 authored Mar 6, 2025
1 parent 2f3c477 commit f66f931
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
15 changes: 11 additions & 4 deletions packages/core/src/services/undoredo/undoredo.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ export interface IUndoRedoItem {

undoMutations: IMutationInfo[];
redoMutations: IMutationInfo[];

/**
* sometimes we need an id to mark the undo-redo item
*/
id?: string;
}

export interface IUndoRedoService {
Expand All @@ -49,7 +54,7 @@ export interface IUndoRedoService {
popUndoToRedo(): void;
popRedoToUndo(): void;

cancelLastRedo(unitId?: string): Nullable<IUndoRedoItem>;
rollback(id: string, unitId?: string): void;

clearUndoRedo(unitId: string): void;

Expand Down Expand Up @@ -273,11 +278,13 @@ export class LocalUndoRedoService extends Disposable implements IUndoRedoService
}
}

cancelLastRedo(unitID?: string): void {
rollback(id: string, unitID?: string): void {
const unitId = unitID || this._getFocusedUnitId();
const stack = this._getUndoStack(unitId);
const item = stack?.pop();
if (item) {
const item = stack?.[stack?.length - 1];
// if the last item is the one we want to rollback
if (item && item.id === id) {
stack.pop();
sequenceExecute(item.undoMutations, this._commandService);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
FOCUSING_EDITOR_INPUT_FORMULA,
FOCUSING_EDITOR_STANDALONE,
FOCUSING_FX_BAR_EDITOR,
generateRandomId,
ICommandService,
IContextService,
Inject,
Expand Down Expand Up @@ -610,6 +611,7 @@ export class EditingRenderController extends Disposable implements IRenderModule
return true;
}

const redoUndoId = generateRandomId(6);
const res = this._commandService.syncExecuteCommand(SetRangeValuesCommand.id, {
subUnitId: sheetId,
unitId,
Expand All @@ -620,12 +622,13 @@ export class EditingRenderController extends Disposable implements IRenderModule
endColumn: column,
},
value: cellData,
redoUndoId,
});

if (res) {
const isValid = await this._sheetInterceptorService.onValidateCell(workbook, worksheet, row, column);
if (isValid === false) {
this._undoRedoService.cancelLastRedo(unitId);
this._undoRedoService.rollback(redoUndoId, unitId);
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export interface ISetRangeValuesCommandParams extends Partial<ISheetCommandShare
* 3. IObjectMatrixPrimitiveType<ICellData>: Bring the row/column information MATRIX, indicating the data of multiple cells
*/
value: ICellData | ICellData[][] | IObjectMatrixPrimitiveType<ICellData>;

redoUndoId?: string;
}

/**
Expand All @@ -64,7 +66,7 @@ export const SetRangeValuesCommand: ICommand = {
if (!target) return false;

const { subUnitId, unitId, workbook, worksheet } = target;
const { value, range } = params;
const { value, range, redoUndoId } = params;
const currentSelections = range ? [range] : selectionManagerService.getCurrentSelections()?.map((s) => s.range);

if (!currentSelections || !currentSelections.length) return false;
Expand Down Expand Up @@ -123,6 +125,7 @@ export const SetRangeValuesCommand: ICommand = {
...redos,
Tools.deepClone(selectionOperation),
],
id: redoUndoId,
});

return true;
Expand Down

0 comments on commit f66f931

Please sign in to comment.