瀏覽代碼

fix deleting multi-point elem during edit (#1892)

David Luzar 5 年之前
父節點
當前提交
6e357c0291
共有 1 個文件被更改,包括 31 次插入35 次删除
  1. 31 35
      src/actions/actionDeleteSelected.tsx

+ 31 - 35
src/actions/actionDeleteSelected.tsx

@@ -52,49 +52,45 @@ function handleGroupEditingState(
 export const actionDeleteSelected = register({
   name: "deleteSelectedElements",
   perform: (elements, appState) => {
-    if (
-      appState.editingLinearElement?.activePointIndex != null &&
-      appState.editingLinearElement?.activePointIndex > -1
-    ) {
-      const { elementId } = appState.editingLinearElement;
+    if (appState.editingLinearElement) {
+      const { elementId, activePointIndex } = appState.editingLinearElement;
       const element = LinearElementEditor.getElement(elementId);
-      if (element) {
+      if (!element) {
+        return false;
+      }
+      if (
+        // case: no point selected → delete whole element
+        activePointIndex == null ||
+        activePointIndex === -1 ||
         // case: deleting last point
-        if (element.points.length < 2) {
-          const nextElements = elements.filter((el) => el.id !== element.id);
-          const nextAppState = handleGroupEditingState(appState, nextElements);
-
-          return {
-            elements: nextElements,
-            appState: {
-              ...nextAppState,
-              editingLinearElement: null,
-            },
-            commitToHistory: false,
-          };
-        }
-
-        LinearElementEditor.movePoint(
-          element,
-          appState.editingLinearElement.activePointIndex,
-          "delete",
-        );
+        element.points.length < 2
+      ) {
+        const nextElements = elements.filter((el) => el.id !== element.id);
+        const nextAppState = handleGroupEditingState(appState, nextElements);
 
         return {
-          elements: elements,
+          elements: nextElements,
           appState: {
-            ...appState,
-            editingLinearElement: {
-              ...appState.editingLinearElement,
-              activePointIndex:
-                appState.editingLinearElement.activePointIndex > 0
-                  ? appState.editingLinearElement.activePointIndex - 1
-                  : 0,
-            },
+            ...nextAppState,
+            editingLinearElement: null,
           },
-          commitToHistory: true,
+          commitToHistory: false,
         };
       }
+
+      LinearElementEditor.movePoint(element, activePointIndex, "delete");
+
+      return {
+        elements: elements,
+        appState: {
+          ...appState,
+          editingLinearElement: {
+            ...appState.editingLinearElement,
+            activePointIndex: activePointIndex > 0 ? activePointIndex - 1 : 0,
+          },
+        },
+        commitToHistory: true,
+      };
     }
 
     let {