Support coping component to root level when no component is selected.

This commit is contained in:
Bowen Tan 2022-02-21 18:18:03 +08:00
parent 315f4945ad
commit 6b5c170691
3 changed files with 23 additions and 25 deletions

View File

@ -7,6 +7,7 @@ import { PasteManager } from '../operations/PasteManager';
import { EditorServices } from '../types';
import { AppModel } from '../AppModel/AppModel';
import { ComponentId } from '../AppModel/IAppModel';
import { RootId } from '../constants';
type Props = {
selectedComponentId: string;
@ -101,7 +102,7 @@ export const KeyboardEventWrapper: React.FC<Props> = ({
eventBus.send(
'operation',
genOperation(registry, 'pasteComponent', {
parentId: selectedComponentId,
parentId: selectedComponentId || RootId,
slot: 'content',
component: clonedComponent!,
copyTimes: pasteManager.current.copyTimes,

View File

@ -8,10 +8,9 @@ export const ignoreTraitsList = [
'core/v1/fetch',
];
export const hasSpecialFormTraitList = [
...ignoreTraitsList,
'core/v1/fetch',
];
export const hasSpecialFormTraitList = [...ignoreTraitsList, 'core/v1/fetch'];
export const RootId = '__root__';
export const EmptyAppSchema: Application = {
kind: 'Application',

View File

@ -1,6 +1,7 @@
import { AppModel } from '../../../AppModel/AppModel';
import { ComponentId, IComponentModel, SlotName } from '../../../AppModel/IAppModel';
import { BaseLeafOperation } from '../../type';
import { RootId } from '../../../constants';
export type PasteComponentLeafOperationContext = {
parentId: string;
@ -13,36 +14,33 @@ export class PasteComponentLeafOperation extends BaseLeafOperation<PasteComponen
private componentCopy!: IComponentModel;
do(prev: AppModel): AppModel {
const targetParent = prev.getComponentById(this.context.parentId as ComponentId);
if (!targetParent) {
return prev;
}
const component = this.context.component;
if (!component) {
return prev;
}
component.allComponents.forEach(c => {
this.context.component.allComponents.forEach(c => {
c.changeId(`${c.id}_copy${this.context.copyTimes}` as ComponentId);
});
targetParent.appendChild(component, this.context.slot as SlotName);
this.componentCopy = component;
return prev;
this.componentCopy = this.context.component;
return this.pasteComponent(prev);
}
redo(prev: AppModel): AppModel {
const targetParent = prev.getComponentById(this.context.parentId as ComponentId);
if (!targetParent) {
return prev;
}
targetParent.appendChild(this.componentCopy, this.context.slot as SlotName);
return prev;
return this.pasteComponent(prev);
}
undo(prev: AppModel): AppModel {
prev.removeComponent(this.componentCopy.id);
return prev;
}
private pasteComponent(prev: AppModel): AppModel {
if (this.context.parentId === RootId) {
prev.appendChild(this.componentCopy);
} else {
const targetParent = prev.getComponentById(this.context.parentId as ComponentId);
if (targetParent && targetParent.slots.includes(this.context.slot as SlotName)) {
targetParent.appendChild(this.componentCopy, this.context.slot as SlotName);
}
}
return prev;
}
}