Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

feat(sheets-ui): check before undo-redo rollback (#4784)

ZhangWeif66f9314c678
diff --git a/packages/core/src/services/undoredo/undoredo.service.ts b/packages/core/src/services/undoredo/undoredo.service.ts
index 83d7cbe1b4..0995a2e987 100644
--- a/packages/core/src/services/undoredo/undoredo.service.ts
+++ b/packages/core/src/services/undoredo/undoredo.service.ts
@@ -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 {
@@ -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;
@@ -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);
}
}
diff --git a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts
index 1f7d32c025..d5471ca8c7 100644
--- a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts
+++ b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts
@@ -35,6 +35,7 @@ import {
FOCUSING_EDITOR_INPUT_FORMULA,
FOCUSING_EDITOR_STANDALONE,
FOCUSING_FX_BAR_EDITOR,
+ generateRandomId,
ICommandService,
IContextService,
Inject,
@@ -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,
@@ -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;
}
}
diff --git a/packages/sheets/src/commands/commands/set-range-values.command.ts b/packages/sheets/src/commands/commands/set-range-values.command.ts
index 1c16fb6502..bdbb0a4f17 100644
--- a/packages/sheets/src/commands/commands/set-range-values.command.ts
+++ b/packages/sheets/src/commands/commands/set-range-values.command.ts
@@ -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;
}
/**
@@ -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;
@@ -123,6 +125,7 @@ export const SetRangeValuesCommand: ICommand = {
...redos,
Tools.deepClone(selectionOperation),
],
+ id: redoUndoId,
});
return true;