Browse Source

ctrl+c, v实现

liwei 1 year ago
parent
commit
33946c53f3

+ 12 - 0
src/modules/editor/controllers/HotKeyCtrl/index.ts

@@ -24,6 +24,18 @@ export class HotKeyCtrl extends ModuleControl<EditorModule> {
         this.actions.pickParentComp(this.store.currCompId);
       },
     },
+    {
+      hotKey: "ctrl+c",
+      action() {
+        this.actions.ctrlc();
+      },
+    },
+    {
+      hotKey: "ctrl+v",
+      action() {
+        this.actions.ctrlv();
+      },
+    },
     // 删除当前组件
     {
       hotKey: "Backspace,del",

+ 61 - 1
src/modules/editor/module/actions/edit.tsx

@@ -9,6 +9,11 @@ import CompSave from "../../components/CompSave";
 import { ObjsContainer } from "../../controllers/SelectCtrl/ObjsContainer";
 import { getKeyThenIncreaseKey } from "ant-design-vue/lib/message";
 import { Matrix } from "../../controllers/SelectCtrl/matrix";
+import { nanoid } from "nanoid";
+import { string } from "vue-types";
+
+
+let ctrlcselected:string[] = [];
 
 export const editActions = EditorModule.action({
   pickComp(compId: string, selected = true) {
@@ -305,6 +310,62 @@ export const editActions = EditorModule.action({
     parentComp && this.store.setCurrComp(parentComp.id);
   },
 
+  ctrlc() {
+    //  console.log("contrc ", this.store.selected);
+     ctrlcselected = this.store.selected.slice(0);
+  },
+
+  ctrlv() {
+    console.log("ctrlv ", this.store.selected);
+    if (ctrlcselected.length < 1) return;
+    
+    const news :string[] = [];
+
+     const deepCopy = (c:DesignComp) => {
+        const childs = c.children.default || [];
+        if (childs.length > 0 ) {
+          childs.forEach((id, index)=>{
+            const cp = this.helper.findComp(id) as DesignComp;
+            const cp1 = cloneDeep(cp);
+            cp1.id = nanoid();
+            this.store.compMap[cp1.id] = cp1;
+            this.store.setCompPid(cp1.id, c.id);
+            childs[index] = cp1.id;
+            
+            deepCopy(cp);
+          })
+        } 
+     }
+
+
+    ctrlcselected.forEach(c=>{
+       const cp = this.helper.findComp(c) as DesignComp;
+       const cp1 = cloneDeep(cp);
+       cp1.id = nanoid();
+       news.push(cp1.id);
+
+       this.store.compMap[cp1.id] = cp1;
+       this.store.setCompPid(cp1.id, this.store.currStreamCardId);
+
+       deepCopy(cp);
+    })
+
+    this.actions.addComps(news);
+
+    this.actions.selectObjs(news);
+
+    setTimeout(() => {
+        this.controls.selectCtrl.translate(20, 20);
+    }, 100);
+
+  },
+
+  addComps(ids:string[]) {
+    const childs = this.store.currStreamCard.children.default?.slice(0) || [];
+    childs.push(...ids);
+    this.store.currStreamCard.children.default = childs;
+  },
+
   // 删除组件
   removeSelectComps() {
     const selected = this.store.selected.slice(0);
@@ -327,7 +388,6 @@ export const editActions = EditorModule.action({
         this.actions.removeStreamCard(compId);
         return;
       }
-      
       const cardid = this.store.currStreamCardId;
       if (compId === this.store.currCompId) {
         this.store.setCurrComp(this.store.currStreamCardId);