Browse Source

修改折线 多边形 直线 undo/redo命令问题

haojianlong 5 years ago
parent
commit
57616c536e

+ 1 - 1
saga-web-big/package.json

@@ -1,6 +1,6 @@
 {
     "name": "@saga-web/big",
-    "version": "1.0.64",
+    "version": "1.0.66",
     "description": "上格云建筑信息化库",
     "main": "lib/index.js",
     "types": "lib/index.d.js",

+ 7 - 12
saga-web-big/src/items/SLineItem.ts

@@ -42,6 +42,7 @@ export class SLineItem extends SGraphItem {
     }
     set status(v: SItemStatus) {
         this._status = v;
+        this.undoStack.clear();
         this.update();
     }
 
@@ -108,7 +109,7 @@ export class SLineItem extends SGraphItem {
     private curPoint: SPoint | null = null;
 
     /** undo/redo堆栈 */
-    private undoStack: SUndoStack | null = new SUndoStack();
+    private undoStack: SUndoStack = new SUndoStack();
 
     /**
      * 构造函数
@@ -281,7 +282,7 @@ export class SLineItem extends SGraphItem {
             if (dis < len) {
                 len = dis;
                 this.curIndex = i;
-                this.curPoint = this.line[this.curIndex];
+                this.curPoint = new SPoint(this.line[this.curIndex]);
             }
         }
     } // Function findNearestPoint()
@@ -294,10 +295,8 @@ export class SLineItem extends SGraphItem {
      */
     protected recordAction(SGraphCommand: any, any: any[]): void {
         // 记录相关命令并推入堆栈中
-        const command = new SGraphCommand(this, ...any);
-        if (this.undoStack) {
-            this.undoStack.push(command);
-        }
+        const command = new SGraphCommand(this.scene, this, ...any);
+        this.undoStack.push(command);
     } // Function recordAction()
 
     /**
@@ -343,9 +342,7 @@ export class SLineItem extends SGraphItem {
      */
     undo(): void {
         if (this.status != SItemStatus.Normal) {
-            if (this.undoStack) {
-                this.undoStack.undo();
-            }
+            this.undoStack.undo();
         }
     } // Function undo()
 
@@ -355,9 +352,7 @@ export class SLineItem extends SGraphItem {
      */
     redo(): void {
         if (this.status != SItemStatus.Normal) {
-            if (this.undoStack) {
-                this.undoStack.redo();
-            }
+            this.undoStack.redo();
         }
     } // Function redo()
 

+ 34 - 51
saga-web-big/src/items/SPolygonItem.ts

@@ -53,12 +53,7 @@ export class SPolygonItem extends SGraphItem {
     // 编辑当前状态
     set status(value: SItemStatus) {
         this._status = value;
-        // 如果状态为show则需清栈
-        if (value == SItemStatus.Normal) {
-            if (this.undoStack) {
-                this.undoStack.clear();
-            }
-        }
+        this.undoStack.clear();
         this.update();
     }
     data: any | null = null;
@@ -102,7 +97,7 @@ export class SPolygonItem extends SGraphItem {
     /** 场景像素  */
     private isAlt: boolean = false;
     /** undoredo堆栈 */
-    protected undoStack: SUndoStack | null = new SUndoStack();
+    protected undoStack: SUndoStack = new SUndoStack();
 
     /**
      * 构造函数
@@ -250,26 +245,28 @@ export class SPolygonItem extends SGraphItem {
             // 绘制顶点块
             painter.pen.color = SColor.Black;
             painter.brush.color = SColor.White;
-            pointList.forEach((item, index) => {
+            pointList.forEach(item => {
                 painter.drawCircle(item.x, item.y, painter.toPx(this.len / 2));
             });
             // 如果最后一个点在第一个点的灵敏度范围内,第一个点填充变红
-            if (
-                SMathUtil.pointDistance(
-                    this.lastPoint.x,
-                    this.lastPoint.y,
-                    this.pointList[0].x,
-                    this.pointList[0].y
-                ) < this.scenceLen
-            ) {
-                // 绘制第一个点的顶点块
-                painter.pen.color = SColor.Black;
-                painter.brush.color = SColor.Red;
-                painter.drawCircle(
-                    this.pointList[0].x,
-                    this.pointList[0].y,
-                    painter.toPx(this.len / 2)
-                );
+            if (this.pointList.length) {
+                if (
+                    SMathUtil.pointDistance(
+                        this.lastPoint.x,
+                        this.lastPoint.y,
+                        this.pointList[0].x,
+                        this.pointList[0].y
+                    ) < this.scenceLen
+                ) {
+                    // 绘制第一个点的顶点块
+                    painter.pen.color = SColor.Black;
+                    painter.brush.color = SColor.Red;
+                    painter.drawCircle(
+                        this.pointList[0].x,
+                        this.pointList[0].y,
+                        painter.toPx(this.len / 2)
+                    );
+                }
             }
         } else {
             painter.drawPolygon(pointList);
@@ -427,9 +424,7 @@ export class SPolygonItem extends SGraphItem {
     protected recordAction(SGraphCommand: any, any: any[]): void {
         // 记录相关命令并推入堆栈中
         const sgraphcommand = new SGraphCommand(this.scene, this, ...any);
-        if (this.undoStack) {
-            this.undoStack.push(sgraphcommand);
-        }
+        this.undoStack.push(sgraphcommand);
     }
 
     /**
@@ -439,9 +434,7 @@ export class SPolygonItem extends SGraphItem {
         if (this.status == SItemStatus.Normal) {
             return;
         }
-        if (this.undoStack) {
-            this.undoStack.undo();
-        }
+        this.undoStack.undo();
     }
 
     /**
@@ -451,9 +444,7 @@ export class SPolygonItem extends SGraphItem {
         if (this.status == SItemStatus.Normal) {
             return;
         }
-        if (this.undoStack) {
-            this.undoStack.redo();
-        }
+        this.undoStack.redo();
     }
 
     ///////////////////////////////
@@ -473,9 +464,6 @@ export class SPolygonItem extends SGraphItem {
         } else if (SItemStatus.Edit == this.status) {
             this.status = SItemStatus.Normal;
             this.releaseItem();
-            if (this.undoStack) {
-                this.undoStack.clear();
-            }
         }
         this.update();
         return true;
@@ -500,10 +488,6 @@ export class SPolygonItem extends SGraphItem {
                     this.$emit("finishCreated");
                     //1 grabItem 置为null
                     this.releaseItem();
-                    //2 清空undo
-                    if (this.undoStack) {
-                        this.undoStack.clear();
-                    }
                 }
             }
         } else if (this.status == SItemStatus.Edit) {
@@ -545,22 +529,21 @@ export class SPolygonItem extends SGraphItem {
         // 如果状态为编辑状态则添加点
         if (this.status == SItemStatus.Create) {
             // 新增顶点
-            let len = SMathUtil.pointDistance(
-                event.x,
-                event.y,
-                this.pointList[0].x,
-                this.pointList[0].y
-            );
-            if (this.pointList.length > 2 && len < this.scenceLen) {
+            let len = -1;
+            if (this.pointList.length) {
+                len = SMathUtil.pointDistance(
+                    event.x,
+                    event.y,
+                    this.pointList[0].x,
+                    this.pointList[0].y
+                );
+            }
+            if (this.pointList.length > 2 && len > 0 && len < this.scenceLen) {
                 this.status = SItemStatus.Normal;
                 //3 传递完成事件状态
                 this.$emit("finishCreated");
                 //1 grabItem 置为null
                 this.releaseItem();
-                //2 清空undo
-                if (this.undoStack) {
-                    this.undoStack.clear();
-                }
             } else {
                 this.insertPoint(event.x, event.y);
                 // 记录新增顶点操作记录压入堆栈

+ 5 - 10
saga-web-big/src/items/SPolylineItem.ts

@@ -32,6 +32,7 @@ export class SPolylineItem extends SGraphItem {
     }
     set status(v: SItemStatus) {
         this._status = v;
+        this.undoStack.clear();
         this.update();
     }
     /** 鼠标移动时的点 */
@@ -72,7 +73,7 @@ export class SPolylineItem extends SGraphItem {
     /** 当前点索引   */
     private curPoint: SPoint | null = null;
     /** undo/redo堆栈 */
-    private undoStack: SUndoStack | null = new SUndoStack();
+    private undoStack: SUndoStack = new SUndoStack();
 
     /**
      * 构造函数
@@ -372,9 +373,7 @@ export class SPolylineItem extends SGraphItem {
     protected recordAction(SGraphCommand: any, any: any[]): void {
         // 记录相关命令并推入堆栈中
         const command = new SGraphCommand(this.scene, this, ...any);
-        if (this.undoStack) {
-            this.undoStack.push(command);
-        }
+        this.undoStack.push(command);
     } // Function recordAction()
 
     /**
@@ -445,9 +444,7 @@ export class SPolylineItem extends SGraphItem {
      */
     undo(): void {
         if (this._status != SItemStatus.Normal) {
-            if (this.undoStack) {
-                this.undoStack.undo();
-            }
+            this.undoStack.undo();
         }
     } // Function undo()
 
@@ -457,9 +454,7 @@ export class SPolylineItem extends SGraphItem {
      */
     redo(): void {
         if (this._status != SItemStatus.Normal) {
-            if (this.undoStack) {
-                this.undoStack.redo();
-            }
+            this.undoStack.redo();
         }
     } // Function redo()