fix the bug that a component cannot be copied multiple times

This commit is contained in:
Bowen Tan 2022-02-21 17:54:25 +08:00
parent 2459bd6c34
commit abeccd45b4
3 changed files with 20 additions and 9 deletions

View File

@ -66,7 +66,7 @@ export const KeyboardEventWrapper: React.FC<Props> = ({
selectedComponentId as ComponentId
);
if (copiedComponent) {
pasteManager.current.setPasteComponents(selectedComponentId, copiedComponent);
pasteManager.current.setPasteComponents(copiedComponent);
}
}
break;
@ -77,7 +77,7 @@ export const KeyboardEventWrapper: React.FC<Props> = ({
selectedComponentId as ComponentId
);
if (copiedComponent) {
pasteManager.current.setPasteComponents(selectedComponentId, copiedComponent);
pasteManager.current.setPasteComponents(copiedComponent);
}
eventBus.send(
'operation',
@ -90,13 +90,20 @@ export const KeyboardEventWrapper: React.FC<Props> = ({
case 'v':
if (e.metaKey || e.ctrlKey) {
if (pasteManager.current.componentCache) {
// Clone the component which is going to copy, otherwise the new component will have the same reference.
const copiedComponentId = pasteManager.current.componentCache.id;
const copiedComponentsSchema =
pasteManager.current.componentCache.allComponents.map(c => c.toSchema());
const clonedComponent = new AppModel(
copiedComponentsSchema,
registry
).getComponentById(copiedComponentId);
eventBus.send(
'operation',
genOperation(registry, 'pasteComponent', {
parentId: selectedComponentId,
slot: 'content',
rootComponentId: pasteManager.current.rootComponentId,
component: pasteManager.current.componentCache,
component: clonedComponent!,
copyTimes: pasteManager.current.copyTimes,
})
);

View File

@ -1,12 +1,10 @@
import { IComponentModel } from '../AppModel/IAppModel';
export class PasteManager {
rootComponentId = ''
componentCache: IComponentModel | undefined;
copyTimes = 0;
setPasteComponents(componentId: string, component: IComponentModel) {
this.rootComponentId = componentId;
setPasteComponents(component: IComponentModel) {
this.componentCache = component;
}
}

View File

@ -5,7 +5,6 @@ import { BaseLeafOperation } from '../../type';
export type PasteComponentLeafOperationContext = {
parentId: string;
slot: string;
rootComponentId: string;
component: IComponentModel;
copyTimes: number;
};
@ -22,6 +21,7 @@ export class PasteComponentLeafOperation extends BaseLeafOperation<PasteComponen
if (!component) {
return prev;
}
component.allComponents.forEach(c => {
c.changeId(`${c.id}_copy${this.context.copyTimes}` as ComponentId);
});
@ -32,7 +32,13 @@ export class PasteComponentLeafOperation extends BaseLeafOperation<PasteComponen
}
redo(prev: AppModel): AppModel {
return this.do(prev);
const targetParent = prev.getComponentById(this.context.parentId as ComponentId);
if (!targetParent) {
return prev;
}
targetParent.appendChild(this.componentCopy, this.context.slot as SlotName);
return prev;
}
undo(prev: AppModel): AppModel {