From 6fba9b81a00e8e4e349d8f67fd7b490512c1f7e5 Mon Sep 17 00:00:00 2001 From: Sczlog Date: Tue, 30 Nov 2021 19:27:30 +0800 Subject: [PATCH] Update createComponentLeafOperation.ts fix create component leaf operation --- .../component/createComponentLeafOperation.ts | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/editor/src/operations/leaf/component/createComponentLeafOperation.ts b/packages/editor/src/operations/leaf/component/createComponentLeafOperation.ts index 6a75fc5e..8b15c1f6 100644 --- a/packages/editor/src/operations/leaf/component/createComponentLeafOperation.ts +++ b/packages/editor/src/operations/leaf/component/createComponentLeafOperation.ts @@ -9,26 +9,26 @@ export type CreateComponentLeafOperationContext = { }; export class CreateComponentLeafOperation extends BaseLeafOperation { + private index!: number; + private component!: ApplicationComponent; do(prev: ApplicationComponent[]): ApplicationComponent[] { - const newComponent = genComponent( - this.context.componentType, - this.context.componentId - ); - this.context.componentId = newComponent.id; + this.component = genComponent(this.context.componentType, this.context.componentId); + this.context.componentId = this.component.id; return produce(prev, draft => { - draft.push(newComponent); + draft.push(this.component); + this.index = draft.length - 1; + }); + } + + redo(prev: ApplicationComponent[]): ApplicationComponent[] { + return produce(prev, draft => { + draft.push(this.component); }); } undo(prev: ApplicationComponent[]): ApplicationComponent[] { return produce(prev, draft => { - const remains = draft.filter( - c => c.id !== this.context.componentId - ); - if (remains.length === draft.length) { - console.warn('element not found'); - } - draft = remains; + draft.splice(this.index, 1); }); } }