liwei 1 year ago
parent
commit
dd9abb52e2

+ 4 - 4
src/modules/editor/controllers/ScreenCtrl/index.ts

@@ -65,6 +65,8 @@ export class ScreenCtrl extends ModuleControl<EditorModule> {
 
    updateAdapterState() {
       if (!this.store.rootPage) return;
+      
+      this.restorScreenPage();
 
       this.store.streamCardIds.forEach(c=>{
          const card = this.helper.findComp(c) as DesignComp;
@@ -76,9 +78,7 @@ export class ScreenCtrl extends ModuleControl<EditorModule> {
 
       this.controls.editorCtrl.state.setPage({w, h});
       this.store.rootPage.layout.size[0] = this.getCurrScreenWidth();
-      this.store.rootPage.layout.size[1] = this.getCurrScreenHeight();
-
-      this.restorScreenPage();
+      this.store.rootPage.layout.size[1] = this.getCurrScreenHeight(); 
    }
 
    restorScreenPage() {
@@ -87,7 +87,7 @@ export class ScreenCtrl extends ModuleControl<EditorModule> {
       //获取当前screen的配置
       const screenId = this.state.screen.useFor + this.state.screen.pageMode + this.state.screen.pageSizeType;
       const screenCards = this.store.designData.compScreenMap[screenId] || [];
-      
+
       //刷新当前card的配置
       this.store.streamCardIds.forEach(c=>{
          const card = this.helper.findComp(c) as DesignComp;

+ 20 - 5
src/modules/editor/module/actions/edit.tsx

@@ -326,8 +326,14 @@ export const editActions = EditorModule.action({
   },
 
   ctrlc() {
-    //  console.log("contrc ", this.store.selected);
-    ctrlState.selected = this.store.selected.slice(0);
+    ctrlState.selected = []; 
+    const children = this.store.currStreamCard.children.default || [];
+    children.forEach(c=>{
+      if (this.store.selected.indexOf(c) > -1) {
+        ctrlState.selected.push(c);
+      }
+    })
+
     ctrlState.cardId = this.store.currStreamCardId;
     ctrlState.type = 1;
     const objc = this.controls.selectCtrl.objContainer as ObjsContainer;
@@ -390,7 +396,16 @@ export const editActions = EditorModule.action({
   ctrlx() {
     //console.log("ctrlv ", this.store.selected);
     //console.log("ctrlv ", this.store.selected);
-    ctrlState.selected = this.store.selected.slice(0);
+
+    //保持图层顺序
+    ctrlState.selected = []; 
+    const children = this.store.currStreamCard.children.default || [];
+    children.forEach(c=>{
+      if (this.store.selected.indexOf(c) > -1) {
+        ctrlState.selected.push(c);
+      }
+    })
+
     ctrlState.cardId = this.store.currStreamCardId;
     ctrlState.type = 2;
     
@@ -435,8 +450,8 @@ export const editActions = EditorModule.action({
 
       deepCopy(cp);
     });
-
     this.actions.addComps(news);
+
     this.actions.selectObjs(news);
 
     setTimeout(() => {
@@ -606,7 +621,7 @@ export const editActions = EditorModule.action({
   async saveDesign() {
     try {
       // 清除无用组件
-      this.helper.clearUnusedComps(this.store.designData.compMap);
+      this.helper.clearProjectUnusedComps(this.store.designData.compMap);
       const c = this.controls.screenCtrl;
       c.saveScreenPage();
       const root = this.helper.findRootComp() as DesignComp;

+ 37 - 0
src/modules/editor/module/helpers/index.ts

@@ -147,12 +147,49 @@ export const helpers = EditorModule.helper({
       return used;
     };
     getUsedIds([rootId]);
+
+    Object.keys(compMap).forEach((compId) => {
+      if (!used.has(compId)) {
+        delete compMap[compId];
+      }
+    });
+  },
+
+  clearProjectUnusedComps(compMap: Record<string, DesignComp>) {
+    const used = new Set<string>();
+    const getUsedIds = (ids: string[]) => {
+      ids.forEach((id) => {
+        const comp = compMap[id];
+        if (!comp) return;
+        used.add(id);
+        getUsedIds(comp.getChildIds());
+      });
+      return used;
+    };
+    getUsedIds(["root"]);
+
+    const compScreenMap = this.store.designData.compScreenMap
+    const keys = Object.keys(compScreenMap) 
+    keys.forEach(k=>{
+        const card = compScreenMap[k];
+        card.forEach(c=>{
+          c.children.forEach(item=>{
+            const comp = compMap[item.id];
+            if (!comp) return;
+            used.add(item.id);
+            getUsedIds([item.id])
+          })
+        })
+    })
+    
     Object.keys(compMap).forEach((compId) => {
       if (!used.has(compId)) {
         delete compMap[compId];
       }
     });
   },
+
+
   getPointOffsetWith(e: MouseEvent, dom: HTMLElement) {
     const domRect = dom.getBoundingClientRect();
     return {