diff --git a/src/components/DrawArea.tsx b/src/components/DrawArea.tsx index b212b9d4..7a5b056b 100644 --- a/src/components/DrawArea.tsx +++ b/src/components/DrawArea.tsx @@ -34,6 +34,7 @@ interface IDrawArea { setIgnoreAreas: (ignoreAreas: IgnoreArea[]) => void; selectedRectId: string | undefined; setSelectedRectId: (id: string) => void; + deleteIgnoreArea?: (id:string)=>void; onStageClick: (event: Konva.KonvaEventObject) => void; stageOffsetState: [ { x: number; y: number }, @@ -60,6 +61,7 @@ export const DrawArea: FunctionComponent = ({ setIgnoreAreas, selectedRectId, setSelectedRectId, + deleteIgnoreArea, onStageClick, stageScaleState, stageOffsetState, @@ -77,8 +79,34 @@ export const DrawArea: FunctionComponent = ({ const [isDrawMode, setIsDrawMode] = drawModeState; const [isDrawing, setIsDrawing] = React.useState(isDrawMode); const [image, imageStatus] = imageState; + const stageRef = React.useRef(null); + + React.useEffect(() => { + if(stageRef.current){ + const container = stageRef.current.container(); + container.addEventListener('keydown', handleStageKeyDown); + } + },[]); + + const isModifierKeyPressed = (e:any) => { + return e.altKey || e.ctrlKey || e.shiftKey; + }; + + const handleStageKeyDown = (e:any) => { + if(!deleteIgnoreArea || isModifierKeyPressed(e)){ + return; + } + if(selectedRectId && (e.key==='Delete' || e.key==='Backspace')){ + deleteIgnoreArea(selectedRectId); + } + }; const handleContentMousedown = (e: any) => { + if(stageRef.current){ + const container = stageRef.current.container(); + container.tabIndex=1; + container.focus(); + } if (!isDrawMode) return; const newArea: IgnoreArea = { @@ -178,8 +206,10 @@ export const DrawArea: FunctionComponent = ({ y: event.clientY - stageOffset.y, }); }} + onKeyDown={(e) => handleStageKeyDown(e)} >