Skip to content

Commit 9ea9c2e

Browse files
committed
Feat: added element deletion on key press
1 parent 4e39cb4 commit 9ea9c2e

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

src/hooks/events/deletion.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { useEffect } from "react";
2+
import { useSelector } from "react-redux";
3+
import { store } from "@/store";
4+
import { deleteElements } from "@/store/reducers/editor";
5+
6+
const useDelete = () => {
7+
const selectedElementIds = useSelector((state: any) => state.editor.selectedElementIds);
8+
9+
useEffect(() => {
10+
const handler = (e) => {
11+
if (e.key === "Backspace" || e.key === "Delete") {
12+
e.preventDefault();
13+
store.dispatch(deleteElements(selectedElementIds));
14+
}
15+
};
16+
window.addEventListener("keydown", handler);
17+
return () => {
18+
window.removeEventListener("keydown", handler);
19+
};
20+
}, [selectedElementIds]);
21+
};
22+
23+
export default useDelete;

src/hooks/events/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ISTKProps } from "@/types";
2+
import { default as useDelete } from "./deletion";
23
import { default as useDeselection } from "./deselection";
34
import { default as useDuplicate } from "./duplication";
45
import { default as usePolyline } from "./polyline";
@@ -7,6 +8,7 @@ import { default as useWorkspaceClick } from "./workspace-click";
78
import { default as useWorkspaceLoad } from "./workspace-load";
89

910
export const useDesignerEvents = (props: ISTKProps) => {
11+
useDelete();
1012
useDeselection();
1113
useDuplicate();
1214
usePolyline();

src/store/reducers/editor/index.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,18 @@ export const slice = createSlice({
254254
},
255255
setVisibilityOffset: (state, action) => {
256256
state.visibilityOffset = action.payload;
257+
},
258+
deleteElements: (state, action) => {
259+
const ids = action.payload;
260+
state.seats = state.seats.filter((seat) => !ids.includes(seat.id));
261+
state.booths = state.booths.filter((booth) => !ids.includes(booth.id));
262+
state.text = state.text.filter((text) => !ids.includes(text.id));
263+
state.shapes = state.shapes.filter((shape) => !ids.includes(shape.id));
264+
state.polylines = state.polylines.filter((polyline) => !ids.includes(polyline.id));
265+
state.images = state.images.filter((image) => !ids.includes(image.id));
266+
state.selectedElementIds = state.selectedElementIds.filter((id) => !ids.includes(id));
267+
state.selectedPolylineId =
268+
state.selectedPolylineId && !ids.includes(state.selectedPolylineId) ? null : state.selectedPolylineId;
257269
}
258270
}
259271
});
@@ -300,7 +312,8 @@ export const {
300312
setSelectedPolylineId,
301313
sync,
302314
setInitialViewBoxScale,
303-
setVisibilityOffset
315+
setVisibilityOffset,
316+
deleteElements
304317
} = slice.actions;
305318

306319
export const selectPolylineById = (id: string) =>

0 commit comments

Comments
 (0)