|
@@ -20,6 +20,9 @@ export const store = EditorModule.store({
|
|
currComp(state) {
|
|
currComp(state) {
|
|
return state.designData.compMap[state.currCompId];
|
|
return state.designData.compMap[state.currCompId];
|
|
},
|
|
},
|
|
|
|
+ pageCompIds(state): string[] {
|
|
|
|
+ return state.designData.compMap.root?.children.default || [];
|
|
|
|
+ },
|
|
},
|
|
},
|
|
actions: {
|
|
actions: {
|
|
setCompData(id: string, data: any) {
|
|
setCompData(id: string, data: any) {
|
|
@@ -32,10 +35,11 @@ export const store = EditorModule.store({
|
|
this.store.designData = new DesignTemp(data);
|
|
this.store.designData = new DesignTemp(data);
|
|
},
|
|
},
|
|
insertDesignContent(compKey: ICompKeys, index?: number) {
|
|
insertDesignContent(compKey: ICompKeys, index?: number) {
|
|
- index === undefined && (index = this.store.designData.content.length);
|
|
|
|
|
|
+ const { pageCompIds } = this.store;
|
|
|
|
+ index === undefined && (index = pageCompIds.length);
|
|
const compId = createCompId(compKey);
|
|
const compId = createCompId(compKey);
|
|
addCacheToMap(this.store.designData.compMap);
|
|
addCacheToMap(this.store.designData.compMap);
|
|
- this.store.designData.content.splice(index, 0, compId);
|
|
|
|
|
|
+ pageCompIds.splice(index, 0, compId);
|
|
return compId;
|
|
return compId;
|
|
},
|
|
},
|
|
insertCompContainer(compKey: ICompKeys, container: DesignComp) {
|
|
insertCompContainer(compKey: ICompKeys, container: DesignComp) {
|
|
@@ -49,46 +53,52 @@ export const store = EditorModule.store({
|
|
this.store.currCompId = compId;
|
|
this.store.currCompId = compId;
|
|
},
|
|
},
|
|
deleteComp(compId: string) {
|
|
deleteComp(compId: string) {
|
|
- const { content, compMap } = this.store.designData;
|
|
|
|
|
|
+ const { compMap } = this.store.designData;
|
|
const parentComp = this.helper.findParentComp(compId);
|
|
const parentComp = this.helper.findParentComp(compId);
|
|
let deleteOK = false;
|
|
let deleteOK = false;
|
|
if (parentComp) {
|
|
if (parentComp) {
|
|
- // 只能删除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;
|
|
|
|
|
|
+ const ids = parentComp.children.default;
|
|
|
|
+ // 只能删除children.default中的组件
|
|
|
|
+ if (ids?.includes(compId)) {
|
|
|
|
+ const index = ids.findIndex((id) => id === compId);
|
|
|
|
+ if (index >= 0) {
|
|
|
|
+ ids.splice(index, 1);
|
|
|
|
+ deleteOK = true;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
if (deleteOK) {
|
|
if (deleteOK) {
|
|
- const comp = this.helper.findComp(compId);
|
|
|
|
- const ids = flatCompChildIds(comp?.children || {});
|
|
|
|
|
|
+ const comp = this.helper.findComp(compId) as DesignComp;
|
|
|
|
+ const ids = this.helper.getCompChildIds(comp);
|
|
[compId, ...ids].forEach((id) => {
|
|
[compId, ...ids].forEach((id) => {
|
|
delete compMap[id];
|
|
delete compMap[id];
|
|
});
|
|
});
|
|
}
|
|
}
|
|
},
|
|
},
|
|
moveComp(selIndex: number, targetIndex: number) {
|
|
moveComp(selIndex: number, targetIndex: number) {
|
|
- const { content } = this.store.designData;
|
|
|
|
- const [selComp] = content.splice(selIndex, 1);
|
|
|
|
- content.splice(targetIndex, 0, selComp);
|
|
|
|
|
|
+ const { pageCompIds } = this.store;
|
|
|
|
+ const [selComp] = pageCompIds.splice(selIndex, 1);
|
|
|
|
+ pageCompIds.splice(targetIndex, 0, selComp);
|
|
|
|
+ },
|
|
|
|
+ clearUnusedComps() {
|
|
|
|
+ const used = new Set<string>();
|
|
|
|
+ const getUsedIds = (ids: string[]) => {
|
|
|
|
+ ids.forEach((id) => {
|
|
|
|
+ const comp = this.helper.findComp(id);
|
|
|
|
+ if (!comp) return;
|
|
|
|
+ used.add(id);
|
|
|
|
+ getUsedIds(this.helper.getCompChildIds(comp));
|
|
|
|
+ });
|
|
|
|
+ return used;
|
|
|
|
+ };
|
|
|
|
+ const rootId = (this.helper.findRootComp() as DesignComp).id;
|
|
|
|
+ getUsedIds([rootId]);
|
|
|
|
+ Object.keys(this.store.designData.compMap).forEach((compId) => {
|
|
|
|
+ if (!used.has(compId)) {
|
|
|
|
+ delete this.store.designData.compMap[compId];
|
|
|
|
+ }
|
|
|
|
+ });
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
});
|
|
-
|
|
|
|
-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;
|
|
|
|
-}
|
|
|