mirror of
https://github.com/smartxworks/sunmao-ui.git
synced 2025-04-06 21:40:23 +08:00
Merge pull request #536 from smartxworks/feat/batch-operation
feat: add `doOperations`
This commit is contained in:
commit
4d6b67cbfb
@ -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>;
|
||||
|
20
packages/editor-sdk/src/types/operation.ts
Normal file
20
packages/editor-sdk/src/types/operation.ts
Normal 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[];
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
30
packages/editor/src/operations/branch/batch.ts
Normal file
30
packages/editor/src/operations/branch/batch.ts
Normal 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);
|
||||
}
|
||||
}
|
@ -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
|
||||
|
@ -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';
|
||||
|
Loading…
x
Reference in New Issue
Block a user