|
@@ -67,8 +67,8 @@ export const store = EditorModule.store({
|
|
|
}
|
|
|
|
|
|
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) => {
|
|
|
delete compMap[id];
|
|
|
});
|
|
@@ -79,16 +79,23 @@ export const store = EditorModule.store({
|
|
|
const [selComp] = content.splice(selIndex, 1);
|
|
|
content.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;
|
|
|
+ };
|
|
|
+ getUsedIds(this.store.designData.content);
|
|
|
+ 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;
|
|
|
-}
|