Merge pull request #536 from smartxworks/feat/batch-operation

feat: add `doOperations`
This commit is contained in:
tanbowensg 2022-07-21 10:19:22 +08:00 committed by GitHub
commit 4d6b67cbfb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 69 additions and 3 deletions

View File

@ -1,6 +1,7 @@
import { ComponentSchema } from '@sunmao-ui/core';
import { RegistryInterface } from '@sunmao-ui/runtime';
import WidgetManager from '../models/WidgetManager';
import type { Operations } from '../types/operation';
export interface EditorServices {
registry: RegistryInterface;
@ -9,6 +10,7 @@ export interface EditorServices {
};
appModelManager: {
appModel: any;
doOperations: (operations: Operations) => void;
};
stateManager: {
store: Record<string, any>;

View File

@ -0,0 +1,20 @@
type OperationType =
| 'createComponent'
| 'removeComponent'
| 'modifyComponentProperty'
| 'modifyComponentId'
| 'adjustComponentOrder'
| 'createTrait'
| 'removeTrait'
| 'modifyTraitProperty'
| 'replaceApp'
| 'pasteComponent'
| 'moveComponent'
| 'createDataSource';
type Operation = {
type: OperationType;
props: Record<string, any>;
};
export type Operations = Operation[];

View File

@ -3,6 +3,7 @@ import { RegistryInterface } from '@sunmao-ui/runtime';
import { EventBusType } from '../services/eventBus';
import { AppModel } from '../AppModel/AppModel';
import { IUndoRedoManager, IOperation, OperationList } from './type';
import { BatchBranchOperation, type BatchBranchOperationContext } from './branch/batch';
export class AppModelManager implements IUndoRedoManager {
appModel: AppModel;
@ -78,4 +79,13 @@ export class AppModelManager implements IUndoRedoManager {
console.warn('cannot undo as cursor has no operation', this.operationStack);
}
}
doOperations(operations: BatchBranchOperationContext['operations']) {
const context = {
operations,
};
const operation = new BatchBranchOperation(this.registry, context);
this.do(operation);
}
}

View File

@ -0,0 +1,30 @@
import { AppModel } from '../../AppModel/AppModel';
import { BaseBranchOperation } from '../type';
import { OperationConstructors } from '../index';
type Operation = {
type: keyof typeof OperationConstructors;
props: any;
};
export type BatchBranchOperationContext = {
operations: Operation[];
};
export class BatchBranchOperation extends BaseBranchOperation<BatchBranchOperationContext> {
do(prev: AppModel): AppModel {
const { operations } = this.context;
operations.forEach(operation => {
this.operationStack.insert(
new OperationConstructors[operation.type](this.registry, operation.props)
);
});
// do the operation in order
return this.operationStack.reduce((prev, node) => {
prev = node.do(prev);
return prev;
}, prev);
}
}

View File

@ -29,7 +29,7 @@ import {
} from './leaf';
import { IOperation } from './type';
const OperationConstructors: Record<
export const OperationConstructors: Record<
OperationTypes,
OperationConfigMaps[OperationTypes]['constructor']
> = {
@ -55,7 +55,7 @@ type OperationConfigMap<TOperation, TContext> = {
registry: RegistryInterface;
};
type OperationConfigMaps = {
export type OperationConfigMaps = {
createComponent: OperationConfigMap<
CreateComponentBranchOperation,
CreateComponentBranchOperationContext

View File

@ -1,5 +1,9 @@
import { Application, Module } from '@sunmao-ui/core';
import { initSunmaoUI, RegistryInterface, StateManagerInterface } from '@sunmao-ui/runtime';
import {
initSunmaoUI,
RegistryInterface,
StateManagerInterface,
} from '@sunmao-ui/runtime';
import { WidgetManager } from '@sunmao-ui/editor-sdk';
import { EditorStore } from './services/EditorStore';
import { EventBusType } from './services/eventBus';