Browse Source

implement deletion of rountangles on alt + click

Jakob Pietron 3 years ago
parent
commit
335221c1f7

+ 2 - 0
src/frontend/rountangleEditor/RountangleActions.ts

@@ -1,8 +1,10 @@
 
 interface CreateRountangle {tag: 'createRountangle', id: string, name: string, posX: number, posY: number}
 interface MoveRountangle   {tag: 'moveRountangle',   id: string, newPosX: number, newPosY: number}
+interface DeleteRountangle {tag: 'deleteRountangle', id: string}
 
 export type RountangleAction =
     Readonly<CreateRountangle>
     | Readonly<MoveRountangle>
+    | Readonly<DeleteRountangle>
 ;

+ 9 - 3
src/frontend/rountangleEditor/RountangleComponent.tsx

@@ -59,9 +59,15 @@ export class RountangleComponent extends React.Component<RountangleProps, Rounta
         event.stopPropagation();
         event.preventDefault();
 
-        // only left mouse button
-        if (event.button !== 0) return;
-        this.setState({dragging: false, moved: false});
+        // if left button, complete dragging
+        if (event.button === 0) {
+            this.setState({dragging: false, moved: false});
+
+            // + alt key deletes block
+            if (event.altKey) {
+                this.props.dispatch({tag: 'deleteRountangle', id: this.props.id});
+            }
+        }
     }
 
     render() {

+ 8 - 0
src/frontend/rountangleEditor/RountangleStore.ts

@@ -1,4 +1,5 @@
 import {RountangleAction} from "./RountangleActions";
+import {assertNever} from "../../util/assert";
 
 interface Rountangle {
     name: string;
@@ -29,6 +30,13 @@ export class RountangleStore {
                     ...this.state,
                     [action.id]: {...this.state[action.id], posX: action.newPosX, posY: action.newPosY}
                 }
+                break;
+            case 'deleteRountangle':
+                this.state = {
+                    ...(delete this.state[action.id] && this.state)
+                }
+                break;
+            default: assertNever(action);
         }
         this.onDispatchListeners.forEach(listener => listener(action));
     }

+ 4 - 0
src/util/assert.ts

@@ -0,0 +1,4 @@
+// this function can be used to ensure there are no missing cases in the handling of a union type
+export function assertNever(x: never): never {
+    throw new Error(`Unexpected object: ${x}`);
+}