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: sheet drawing commands support intercepting mutations (#3885)

YuHong7f1cf918cf35
diff --git a/packages/sheets-drawing-ui/src/commands/commands/insert-sheet-drawing.command.ts b/packages/sheets-drawing-ui/src/commands/commands/insert-sheet-drawing.command.ts
index 6ca8353b2e..0532defa11 100644
--- a/packages/sheets-drawing-ui/src/commands/commands/insert-sheet-drawing.command.ts
+++ b/packages/sheets-drawing-ui/src/commands/commands/insert-sheet-drawing.command.ts
@@ -15,16 +15,18 @@
*/
import type { IAccessor, ICommand } from '@univerjs/core';
+import type { IDrawingJsonUndo1 } from '@univerjs/drawing';
+
+import type { IInsertDrawingCommandParams } from './interfaces';
import {
CommandType,
ICommandService,
IUndoRedoService,
+ sequenceExecute,
} from '@univerjs/core';
-
+import { SheetInterceptorService } from '@univerjs/sheets';
import { DrawingApplyType, ISheetDrawingService, SetDrawingApplyMutation } from '@univerjs/sheets-drawing';
-import type { IDrawingJsonUndo1 } from '@univerjs/drawing';
import { ClearSheetDrawingTransformerOperation } from '../operations/clear-drawing-transformer.operation';
-import type { IInsertDrawingCommandParams } from './interfaces';
/**
* The command to insert new defined name
@@ -36,6 +38,7 @@ export const InsertSheetDrawingCommand: ICommand = {
const commandService = accessor.get(ICommandService);
const undoRedoService = accessor.get(IUndoRedoService);
const sheetDrawingService = accessor.get(ISheetDrawingService);
+ const sheetInterceptorService = accessor.get(SheetInterceptorService);
if (!params) return false;
@@ -51,17 +54,24 @@ export const InsertSheetDrawingCommand: ICommand = {
const { unitId, subUnitId, undo, redo, objects } = jsonOp;
- const result = commandService.syncExecuteCommand(SetDrawingApplyMutation.id, { op: redo, unitId, subUnitId, objects, type: DrawingApplyType.INSERT });
+ const intercepted = sheetInterceptorService.onCommandExecute({ id: InsertSheetDrawingCommand.id, params });
+
+ const insertMutation = { id: SetDrawingApplyMutation.id, params: { op: redo, unitId, subUnitId, objects, type: DrawingApplyType.INSERT } };
+ const undoInsertMutation = { id: SetDrawingApplyMutation.id, params: { op: undo, unitId, subUnitId, objects, type: DrawingApplyType.REMOVE } };
+
+ const result = sequenceExecute([insertMutation, ...intercepted.redos], commandService);
if (result) {
undoRedoService.pushUndoRedo({
unitID: unitId,
undoMutations: [
- { id: SetDrawingApplyMutation.id, params: { op: undo, unitId, subUnitId, objects, type: DrawingApplyType.REMOVE } },
+ ...intercepted.undos,
+ undoInsertMutation,
{ id: ClearSheetDrawingTransformerOperation.id, params: unitIds },
],
redoMutations: [
- { id: SetDrawingApplyMutation.id, params: { op: redo, unitId, subUnitId, objects, type: DrawingApplyType.INSERT } },
+ insertMutation,
+ ...intercepted.redos,
{ id: ClearSheetDrawingTransformerOperation.id, params: unitIds },
],
});
diff --git a/packages/sheets-drawing-ui/src/commands/commands/remove-sheet-drawing.command.ts b/packages/sheets-drawing-ui/src/commands/commands/remove-sheet-drawing.command.ts
index b0c0745600..75bd3a7845 100644
--- a/packages/sheets-drawing-ui/src/commands/commands/remove-sheet-drawing.command.ts
+++ b/packages/sheets-drawing-ui/src/commands/commands/remove-sheet-drawing.command.ts
@@ -15,15 +15,17 @@
*/
import type { IAccessor, ICommand } from '@univerjs/core';
+import type { IDrawingJsonUndo1 } from '@univerjs/drawing';
+import type { IDeleteDrawingCommandParams } from './interfaces';
import {
CommandType,
ICommandService,
IUndoRedoService,
+ sequenceExecute,
} from '@univerjs/core';
+import { SheetInterceptorService } from '@univerjs/sheets';
import { DrawingApplyType, ISheetDrawingService, SetDrawingApplyMutation } from '@univerjs/sheets-drawing';
-import type { IDrawingJsonUndo1 } from '@univerjs/drawing';
import { ClearSheetDrawingTransformerOperation } from '../operations/clear-drawing-transformer.operation';
-import type { IDeleteDrawingCommandParams } from './interfaces';
/**
* The command to remove new sheet image
@@ -34,6 +36,7 @@ export const RemoveSheetDrawingCommand: ICommand = {
handler: (accessor: IAccessor, params?: IDeleteDrawingCommandParams) => {
const commandService = accessor.get(ICommandService);
const undoRedoService = accessor.get(IUndoRedoService);
+ const sheetInterceptorService = accessor.get(SheetInterceptorService);
const sheetDrawingService = accessor.get(ISheetDrawingService);
@@ -52,18 +55,25 @@ export const RemoveSheetDrawingCommand: ICommand = {
const { unitId, subUnitId, undo, redo, objects } = jsonOp;
+ const intercepted = sheetInterceptorService.onCommandExecute({ id: RemoveSheetDrawingCommand.id, params });
+
// execute do mutations and add undo mutations to undo stack if completed
- const result = commandService.syncExecuteCommand(SetDrawingApplyMutation.id, { unitId, subUnitId, op: redo, objects, type: DrawingApplyType.REMOVE });
+ const removeMutation = { id: SetDrawingApplyMutation.id, params: { unitId, subUnitId, op: redo, objects, type: DrawingApplyType.REMOVE } };
+ const undoRemoveMutation = { id: SetDrawingApplyMutation.id, params: { unitId, subUnitId, op: undo, objects, type: DrawingApplyType.INSERT } };
+
+ const result = sequenceExecute([removeMutation, ...intercepted.redos], commandService);
if (result) {
undoRedoService.pushUndoRedo({
unitID: unitId,
undoMutations: [
- { id: SetDrawingApplyMutation.id, params: { unitId, subUnitId, op: undo, objects, type: DrawingApplyType.INSERT } },
+ ...intercepted.undos,
+ undoRemoveMutation,
{ id: ClearSheetDrawingTransformerOperation.id, params: unitIds },
],
redoMutations: [
- { id: SetDrawingApplyMutation.id, params: { unitId, subUnitId, op: redo, objects, type: DrawingApplyType.REMOVE } },
+ removeMutation,
+ ...intercepted.redos,
{ id: ClearSheetDrawingTransformerOperation.id, params: unitIds },
],
});
diff --git a/packages/sheets-drawing-ui/src/index.ts b/packages/sheets-drawing-ui/src/index.ts
index 9810d02bfe..89f1511718 100644
--- a/packages/sheets-drawing-ui/src/index.ts
+++ b/packages/sheets-drawing-ui/src/index.ts
@@ -34,4 +34,5 @@ export { EditSheetDrawingOperation } from './commands/operations/edit-sheet-draw
export { type IInsertImageCommandParams, InsertFloatImageCommand } from './commands/commands/insert-image.command';
export { SidebarSheetDrawingOperation } from './commands/operations/open-drawing-panel.operation';
+export type { IDeleteDrawingCommandParams, IInsertDrawingCommandParams } from './commands/commands/interfaces';
// #endregion