Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sheet drawing commands support intercepting mutations #3885

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

Expand All @@ -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 },
],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand All @@ -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 },
],
});
Expand Down
1 change: 1 addition & 0 deletions packages/sheets-drawing-ui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading