Skip to content

Commit 5e2a48f

Browse files
authored
feat: Delete area with delete or backspace #312 (#313)
1 parent 4a52cd3 commit 5e2a48f

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/components/DrawArea.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ interface IDrawArea {
3434
setIgnoreAreas: (ignoreAreas: IgnoreArea[]) => void;
3535
selectedRectId: string | undefined;
3636
setSelectedRectId: (id: string) => void;
37+
deleteIgnoreArea?: (id:string)=>void;
3738
onStageClick: (event: Konva.KonvaEventObject<MouseEvent>) => void;
3839
stageOffsetState: [
3940
{ x: number; y: number },
@@ -64,6 +65,7 @@ export const DrawArea: FunctionComponent<IDrawArea> = ({
6465
setIgnoreAreas,
6566
selectedRectId,
6667
setSelectedRectId,
68+
deleteIgnoreArea,
6769
onStageClick,
6870
stageScaleState,
6971
stageOffsetState,
@@ -83,6 +85,27 @@ export const DrawArea: FunctionComponent<IDrawArea> = ({
8385
const [isDrawMode, setIsDrawMode] = drawModeState;
8486
const [isDrawing, setIsDrawing] = React.useState(isDrawMode);
8587
const [image, imageStatus] = imageState;
88+
const stageRef = React.useRef<Konva.Stage>(null);
89+
90+
React.useEffect(() => {
91+
if(stageRef.current){
92+
const container = stageRef.current.container();
93+
container.addEventListener('keydown', handleStageKeyDown);
94+
}
95+
},[]);
96+
97+
const isModifierKeyPressed = (e:any) => {
98+
return e.altKey || e.ctrlKey || e.shiftKey;
99+
};
100+
101+
const handleStageKeyDown = (e:any) => {
102+
if(!deleteIgnoreArea || isModifierKeyPressed(e)){
103+
return;
104+
}
105+
if(selectedRectId && (e.key==='Delete' || e.key==='Backspace')){
106+
deleteIgnoreArea(selectedRectId);
107+
}
108+
};
86109

87110
const scrollContainerRef = React.useRef<HTMLDivElement>(null);
88111

@@ -91,6 +114,11 @@ export const DrawArea: FunctionComponent<IDrawArea> = ({
91114
}, [stageScollPos]);
92115

93116
const handleContentMousedown = (e: any) => {
117+
if(stageRef.current){
118+
const container = stageRef.current.container();
119+
container.tabIndex=1;
120+
container.focus();
121+
}
94122
if (!isDrawMode) return;
95123

96124
const newArea: IgnoreArea = {
@@ -195,8 +223,10 @@ export const DrawArea: FunctionComponent<IDrawArea> = ({
195223
y: event.clientY - stageOffset.y,
196224
});
197225
}}
226+
onKeyDown={(e) => handleStageKeyDown(e)}
198227
>
199228
<Stage
229+
ref={stageRef}
200230
width={image && image.width}
201231
height={image && image.height}
202232
onMouseDown={onStageClick}

src/components/TestDetailsDialog/TestDetailsModal.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ const TestDetailsModal: React.FunctionComponent<{
466466
tempIgnoreAreas={JSON.parse(testRun.tempIgnoreAreas)}
467467
setIgnoreAreas={handleIgnoreAreaChange}
468468
selectedRectId={selectedRectId}
469+
deleteIgnoreArea={deleteIgnoreArea}
469470
setSelectedRectId={setSelectedRectId}
470471
onStageClick={removeSelection}
471472
stageScaleState={[stageScale, setStageScale]}
@@ -486,6 +487,7 @@ const TestDetailsModal: React.FunctionComponent<{
486487
setIgnoreAreas={handleIgnoreAreaChange}
487488
selectedRectId={selectedRectId}
488489
setSelectedRectId={setSelectedRectId}
490+
deleteIgnoreArea={deleteIgnoreArea}
489491
onStageClick={removeSelection}
490492
stageScaleState={[stageScale, setStageScale]}
491493
stagePosState={[stagePos, setStagePos]}

0 commit comments

Comments
 (0)