Skip to content

Commit 40f71c1

Browse files
committed
added shortcut keys for tools and image naviagtion
1 parent 346480e commit 40f71c1

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

client/src/FilesListMenu/index.jsx

+38-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from "react"
1+
import React, { useState, useEffect } from "react";
22
import { styled } from "@mui/material/styles"
33
import { createTheme, ThemeProvider } from "@mui/material/styles"
44
import Box from "@mui/material/Box"
@@ -67,6 +67,43 @@ export const FilesListMenu = ({
6767
onClick,
6868
}) => {
6969
const { t } = useTranslation()
70+
71+
// Track the index of the selected image
72+
const [selectedIndex, setSelectedIndex] = useState(
73+
allImages.findIndex((img) => img.name === selectedImage) || 0
74+
);
75+
76+
// Handle ArrowUp and ArrowDown key presses for navigation
77+
const handleKeyDown = (event) => {
78+
if (event.key === "ArrowUp") {
79+
setSelectedIndex((prevIndex) =>
80+
prevIndex > 0 ? prevIndex - 1 : allImages.length - 1
81+
);
82+
} else if (event.key === "ArrowDown") {
83+
setSelectedIndex((prevIndex) =>
84+
prevIndex < allImages.length - 1 ? prevIndex + 1 : 0
85+
);
86+
}
87+
};
88+
89+
// Add keydown event listener when the component mounts
90+
useEffect(() => {
91+
window.addEventListener("keydown", handleKeyDown);
92+
93+
// Cleanup the event listener when the component unmounts
94+
return () => {
95+
window.removeEventListener("keydown", handleKeyDown);
96+
};
97+
}, []);
98+
99+
// Update selected image when selectedIndex changes
100+
useEffect(() => {
101+
if (allImages[selectedIndex]) {
102+
onSelectJump(allImages[selectedIndex].name);
103+
}
104+
}, [selectedIndex]);
105+
106+
70107
const handleClickLabel = (label) => {
71108
onClick(getActiveImage(state))
72109
onSelectJump(label)

client/src/MainLayout/index.jsx

+21-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,27 @@ export const MainLayout = ({
8080
nextImage = state.images[currentImageIndex + 1]
8181
}
8282

83-
useKey("Escape", () => dispatch({ type: "CANCEL" }))
83+
84+
const handleKey = (key, actionType, selectedTool = null) => {
85+
useKey(
86+
(event) => event.ctrlKey && event.shiftKey && event.key.toLowerCase() === key,
87+
() => {
88+
const action = { type: actionType };
89+
if (selectedTool) {
90+
action.selectedTool = selectedTool;
91+
}
92+
dispatch(action);
93+
},
94+
{ event: 'keydown' }
95+
);
96+
};
97+
98+
handleKey('escape', 'CANCEL');
99+
handleKey('b', 'SELECT_TOOL', 'create-box');
100+
handleKey('z', 'SELECT_TOOL', 'zoom');
101+
handleKey('p', 'SELECT_TOOL', 'create-polygon');
102+
handleKey('c', 'SELECT_TOOL', 'create-circle');
103+
84104

85105
const innerContainerRef = useRef()
86106
const hotkeyHandlers = useDispatchHotkeyHandlers({ dispatch })

0 commit comments

Comments
 (0)