|
@@ -2,6 +2,8 @@ import {reactive, computed} from "vue"
|
|
|
import { DesignComp } from "../../defines/DesignTemp/DesignComp"
|
|
|
import { cloneDeep } from "lodash";
|
|
|
|
|
|
+type CreateFunc = (values:any, comm?: Partial<CommPropType>)=>DesignComp;
|
|
|
+
|
|
|
export class CompController{
|
|
|
state = reactive({
|
|
|
_id: "",
|
|
@@ -11,7 +13,7 @@ export class CompController{
|
|
|
})
|
|
|
//包括所有部件和所有列表的
|
|
|
designCompMap = new Map<string, DesignComp>();
|
|
|
- CompTypes = [] as {type:string, name:string, thumbnail:string, isBasic: boolean}[];
|
|
|
+ CompTypes = [] as {type:string, name:string, thumbnail:string, isBasic: boolean, createCompData: CreateFunc}[];
|
|
|
|
|
|
initPageData(designData:any) {
|
|
|
this.state._id = designData._id;
|
|
@@ -30,6 +32,17 @@ export class CompController{
|
|
|
this.designCompMap.set(data.id, data);
|
|
|
}
|
|
|
|
|
|
+ createCompData(compKey: string) {
|
|
|
+ const CompType = this.CompTypes.find(item=>item.type == compKey);
|
|
|
+ if (!CompType) return;
|
|
|
+
|
|
|
+ return CompType.createCompData({});
|
|
|
+ }
|
|
|
+
|
|
|
+ getCompType(compKey:string) {
|
|
|
+ return this.CompTypes.find(item=>item.type == compKey);
|
|
|
+ }
|
|
|
+
|
|
|
setCompState(compId: string , data:any) {
|
|
|
this.designCompMap.set(compId, data);
|
|
|
}
|
|
@@ -43,15 +56,15 @@ export const CompCtrl = new CompController();
|
|
|
type CommPropType = Pick<DesignComp, "compKey" | "background" | "layout" | "id">
|
|
|
export function RegCompType<T>(info: {type:string , name:string, thumbnail:string, isBasic?:boolean}, getDef:(Partial<CommPropType> & {value: T})|(()=>Partial<CommPropType> & {value: T})) {
|
|
|
info.isBasic = info.isBasic ? true : false;
|
|
|
- if (!CompCtrl.CompTypes.find(item=>item.type == info.type)) {
|
|
|
- CompCtrl.CompTypes.push(info as any);
|
|
|
- }
|
|
|
+
|
|
|
const createCompData = function(values: Partial<T>, comm?: Partial<CommPropType>) {
|
|
|
- const defvalues = typeof getDef == "function" ? getDef():getDef;
|
|
|
+ const defvalues = typeof getDef == "function" ? getDef(): cloneDeep(getDef);
|
|
|
defvalues.compKey = info.type as any
|
|
|
const c = Object.assign({}, defvalues, comm, {value: values}) as CommPropType &{value: T}
|
|
|
const ret = new DesignComp(c) as typeof c;
|
|
|
- CompCtrl.setCompState(ret.id, ret);
|
|
|
+
|
|
|
+ CompCtrl.setCompState(ret.id, reactive(ret));
|
|
|
+
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
@@ -61,5 +74,12 @@ export function RegCompType<T>(info: {type:string , name:string, thumbnail:strin
|
|
|
return CompCtrl.getCompState(compId) as ValueType;
|
|
|
}
|
|
|
|
|
|
+ //@ts-ignore
|
|
|
+ info.createCompData = createCompData;
|
|
|
+
|
|
|
+ if (!CompCtrl.CompTypes.find(item=>item.type == info.type)) {
|
|
|
+ CompCtrl.CompTypes.push(info as any);
|
|
|
+ }
|
|
|
+
|
|
|
return {useCompData, createCompData}
|
|
|
}
|