Browse Source

添加删除快捷键操作(设备,空间不支持删除)

yunxing 4 years ago
parent
commit
0690389b8c
1 changed files with 82 additions and 14 deletions
  1. 82 14
      src/lib/SPlanScene.ts

+ 82 - 14
src/lib/SPlanScene.ts

@@ -24,15 +24,15 @@
  * ****/
 
 import { SBaseEditScene, SBasePipe, SBaseEquipment } from "@persagy-web/big-edit";
-import { SGraphEdit, SGraphPropertyCommand, SBaseImageEdit } from "@persagy-web/edit";
+import { SGraphEdit, SGraphPropertyCommand, SBaseImageEdit, SGraphDeleteListCommand } from "@persagy-web/edit";
 import { SMouseEvent } from "@persagy-web/base";
-import { SGraphSelectContainer, SLineStyle } from "@persagy-web/graph";
+import { SGraphItem, SGraphSelectContainer, SLineStyle } from "@persagy-web/graph";
 import { SItemStatus, ItemColor } from "@persagy-web/big";
 import { rgbaNum } from "@persagy-web/big-edit/lib/until";
 // 引入命令
 import { SGraphAddCommand } from "@persagy-web/edit";
 import { SColor, SFont, SArrowStyleType } from "@persagy-web/draw";
-import { SPlanParser } from "./";
+import { SPlanEquipment, SPlanParser, SPlanZone } from "./";
 
 /**
  * 平面图场景类
@@ -43,8 +43,9 @@ export class SPlanScene extends SBaseEditScene {
     /** 图例数据 */
     legendObj: any = null;
     // 静态服务器路径
-    public imgServeUrl: string = '';
-    
+    public imgServeUrl: string = "";
+    /** 判断是否为苹果电脑 */
+    public isMac = /macintosh|mac os x/i.test(navigator.userAgent);
     constructor() {
         super();
         // 选择绑定选额item事件
@@ -133,11 +134,11 @@ export class SPlanScene extends SBaseEditScene {
 
     /**
      * 重载添加图片-可以根据底图缩放的图片
-     * @param event 
+     * @param event
      */
-    addImageItem(event: SMouseEvent):void {
+    addImageItem(event: SMouseEvent): void {
         const data = {
-            name: '基础图片',
+            name: "基础图片",
             num: 1,
             type: "Image",
             pos: { x: event.x, y: event.y },
@@ -147,9 +148,9 @@ export class SPlanScene extends SBaseEditScene {
             style: {
                 default: {
                     strokecolor: "#c0ccda",
-                    url: '',
-                }
-            }
+                    url: "",
+                },
+            },
         };
         const item = new SBaseImageEdit(null, data);
         item.isTransform = false;
@@ -216,8 +217,6 @@ export class SPlanScene extends SBaseEditScene {
         });
     }
 
-    
-
     /**
      * 修改指定设备得信息点
      *
@@ -341,7 +340,76 @@ export class SPlanScene extends SBaseEditScene {
             relations,
         };
     }
-
+    /**
+     * 键盘事件
+     *
+     * @param	event         事件参数
+     * @return	boolean
+     */
+    onKeyDown(event: KeyboardEvent): any {
+        console.log("onKeyDown:", event);
+        console.log(this);
+        // if (!this.isEditStatus) {
+        //     return true;
+        // }
+        if (this.grabItem) {
+            this.grabItem.onKeyDown(event);
+            if (event.code == "Escape") {
+                const item = this.grabItem;
+                this.grabItem = null;
+                this.removeItem(item);
+                if (this.view) {
+                    this.view.update();
+                }
+                // this.setCmd = 'choice'
+            }
+        }
+        // 删除键功能
+        if (event.code == "Delete") {
+            this.deleteItem();
+        }
+        // 删除键功能--兼容苹果mac
+        if (event.code == "Backspace" && this.isMac) {
+            this.deleteItem();
+        }
+        // 复制粘贴
+        if (event.ctrlKey && !event.repeat) {
+            if (event.code == "KeyC") {
+                console.log("ctrl c");
+                // this.copy()
+            } else if (event.code == "KeyV") {
+                console.log("ctrl v");
+                // this.paste()
+            }
+        }
+        return false;
+    }
+    /**
+     * 删除
+     *
+     * @return 删除的图元素
+     */
+    deleteItem(itemArr: SGraphItem[] = []): any {
+        let itemList = null;
+        if (itemArr && itemArr.length && itemArr[0]) {
+            itemList = [...itemArr];
+        } else {
+            if (this.selectContainer.count == 0) {
+                return [];
+            }
+            // 不是设备类,空间类, 加入删除的itemList
+            itemList = this.selectContainer.itemList.filter((element: any) => !(element instanceof SPlanEquipment || element instanceof SPlanZone));
+            if (itemList.length) this.undoStack.push(new SGraphDeleteListCommand(this, [...itemList]));
+        }
+        // 推入 undo/redo 栈
+        itemList.forEach((element: any) => {
+            this.removeItem(element);
+        });
+        if (this.view) {
+            this.view.update();
+        }
+        return itemList;
+    }
     /**
      * 粘贴
      */