|
@@ -1,5 +1,8 @@
|
|
|
import { EditorModule } from "..";
|
|
|
-import { CompUI } from "../components/CompUI";
|
|
|
+import {
|
|
|
+ addCacheToMap,
|
|
|
+ createCompId,
|
|
|
+} from "../components/CompUI/defines/createCompId";
|
|
|
import { DesignTemp } from "../defines/DesignTemp";
|
|
|
import { DesignComp } from "../defines/DesignTemp/DesignComp";
|
|
|
import { EditorMode, ICompKeys } from "../typings";
|
|
@@ -9,87 +12,67 @@ export const store = EditorModule.store({
|
|
|
mode: "edit" as EditorMode,
|
|
|
currCompId: "",
|
|
|
designData: new DesignTemp(),
|
|
|
- designCompMap: new Map<string, DesignComp>(),
|
|
|
}),
|
|
|
getters: {
|
|
|
isEditMode(state) {
|
|
|
return state.mode === "edit";
|
|
|
},
|
|
|
currComp(state) {
|
|
|
- return state.designCompMap.get(state.currCompId);
|
|
|
+ return state.designData.compMap[state.currCompId];
|
|
|
},
|
|
|
},
|
|
|
actions: {
|
|
|
- setCompData(id: string, data:any) {
|
|
|
- this.store.designCompMap.set(id, data)
|
|
|
+ setCompData(id: string, data: any) {
|
|
|
+ this.store.designData.compMap[id] = data;
|
|
|
},
|
|
|
setMode(v: EditorMode) {
|
|
|
this.store.mode = v;
|
|
|
},
|
|
|
- initDesignCompMap() {
|
|
|
- const { designData, designCompMap } = this.store;
|
|
|
- const arr = [...designData.content];
|
|
|
- designCompMap.clear();
|
|
|
- while (arr.length) {
|
|
|
- const item = arr[0];
|
|
|
- designCompMap.set(item.id, item);
|
|
|
- arr.shift();
|
|
|
- if (item.children) {
|
|
|
- arr.unshift(...Object.values(item.children));
|
|
|
- }
|
|
|
- }
|
|
|
- },
|
|
|
initDesignData(data: Partial<DesignTemp>) {
|
|
|
this.store.designData = new DesignTemp(data);
|
|
|
- this.store.initDesignCompMap();
|
|
|
},
|
|
|
insertDesignContent(compKey: ICompKeys, index?: number) {
|
|
|
index === undefined && (index = this.store.designData.content.length);
|
|
|
- const options = CompUI[compKey].options;
|
|
|
- const comp = new DesignComp({
|
|
|
- compKey,
|
|
|
- value: options?.value,
|
|
|
- layout: options?.layout,
|
|
|
- background: options?.background,
|
|
|
- children: options?.children as any,
|
|
|
- });
|
|
|
- this.store.designData.content.splice(index, 0, comp);
|
|
|
- this.store.initDesignCompMap();
|
|
|
- return comp;
|
|
|
+ const compId = createCompId(compKey);
|
|
|
+ addCacheToMap(this.store.designData.compMap);
|
|
|
+ this.store.designData.content.splice(index, 0, compId);
|
|
|
+ return compId;
|
|
|
},
|
|
|
insertCompContainer(compKey: ICompKeys, container: DesignComp) {
|
|
|
- const options = CompUI[compKey].options;
|
|
|
- const comp = new DesignComp({
|
|
|
- compKey,
|
|
|
- value: options?.value,
|
|
|
- layout: options?.layout,
|
|
|
- background: options?.background,
|
|
|
- children: options?.children as any,
|
|
|
- });
|
|
|
+ const compId = createCompId(compKey);
|
|
|
+ addCacheToMap(this.store.designData.compMap);
|
|
|
container.children || (container.children = {});
|
|
|
- container.children[comp.id] = comp;
|
|
|
- this.store.initDesignCompMap();
|
|
|
- return comp;
|
|
|
+ container.children[compId] = compId;
|
|
|
+ return compId;
|
|
|
},
|
|
|
setCurrComp(compId: string) {
|
|
|
this.store.currCompId = compId;
|
|
|
},
|
|
|
deleteComp(compId: string) {
|
|
|
+ const { content, compMap } = this.store.designData;
|
|
|
const parentComp = this.helper.findParentComp(compId);
|
|
|
+ let deleteOK = false;
|
|
|
if (parentComp) {
|
|
|
- console.log("find parent comp=>", parentComp);
|
|
|
- compId = parentComp?.id;
|
|
|
- // delete parentComp.children[compId];
|
|
|
+ // 只能删除children中以compId作为key的组件
|
|
|
+ if (parentComp.children?.[compId]) {
|
|
|
+ delete parentComp.children[compId];
|
|
|
+ deleteOK = true;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ const index = content.findIndex((id) => id === compId);
|
|
|
+ if (index >= 0) {
|
|
|
+ content.splice(index, 1);
|
|
|
+ deleteOK = true;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- // else {
|
|
|
- const index = this.store.designData.content.findIndex(
|
|
|
- (d) => d.id === compId
|
|
|
- );
|
|
|
- if (index !== -1) {
|
|
|
- this.store.designData.content.splice(index, 1);
|
|
|
+ if (deleteOK) {
|
|
|
+ const comp = this.helper.findComp(compId);
|
|
|
+ const ids = flatCompChildIds(comp?.children || {});
|
|
|
+ [compId, ...ids].forEach((id) => {
|
|
|
+ delete compMap[id];
|
|
|
+ });
|
|
|
}
|
|
|
- // }
|
|
|
},
|
|
|
moveComp(selIndex: number, targetIndex: number) {
|
|
|
const { content } = this.store.designData;
|
|
@@ -98,3 +81,14 @@ export const store = EditorModule.store({
|
|
|
},
|
|
|
},
|
|
|
});
|
|
|
+
|
|
|
+function flatCompChildIds(obj: object, ids: string[] = []) {
|
|
|
+ Object.values(obj).forEach((item) => {
|
|
|
+ if (item instanceof Object) {
|
|
|
+ flatCompChildIds(item, ids);
|
|
|
+ } else if (typeof item === "string") {
|
|
|
+ ids.push(item);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return ids;
|
|
|
+}
|