File tree Expand file tree Collapse file tree 3 files changed +39
-1
lines changed Expand file tree Collapse file tree 3 files changed +39
-1
lines changed Original file line number Diff line number Diff line change 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 ;
Original file line number Diff line number Diff line change 11import { ISTKProps } from "@/types" ;
2+ import { default as useDelete } from "./deletion" ;
23import { default as useDeselection } from "./deselection" ;
34import { default as useDuplicate } from "./duplication" ;
45import { default as usePolyline } from "./polyline" ;
@@ -7,6 +8,7 @@ import { default as useWorkspaceClick } from "./workspace-click";
78import { default as useWorkspaceLoad } from "./workspace-load" ;
89
910export const useDesignerEvents = ( props : ISTKProps ) => {
11+ useDelete ( ) ;
1012 useDeselection ( ) ;
1113 useDuplicate ( ) ;
1214 usePolyline ( ) ;
Original file line number Diff line number Diff 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
306319export const selectPolylineById = ( id : string ) =>
You can’t perform that action at this time.
0 commit comments