Skip to content

Commit

Permalink
added shortcut keys for tools and image naviagtion
Browse files Browse the repository at this point in the history
  • Loading branch information
sumn2u committed Oct 24, 2024
1 parent 346480e commit 40f71c1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
39 changes: 38 additions & 1 deletion client/src/FilesListMenu/index.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react"
import React, { useState, useEffect } from "react";
import { styled } from "@mui/material/styles"
import { createTheme, ThemeProvider } from "@mui/material/styles"
import Box from "@mui/material/Box"
Expand Down Expand Up @@ -67,6 +67,43 @@ export const FilesListMenu = ({
onClick,
}) => {
const { t } = useTranslation()

// Track the index of the selected image
const [selectedIndex, setSelectedIndex] = useState(
allImages.findIndex((img) => img.name === selectedImage) || 0
);

// Handle ArrowUp and ArrowDown key presses for navigation
const handleKeyDown = (event) => {
if (event.key === "ArrowUp") {
setSelectedIndex((prevIndex) =>
prevIndex > 0 ? prevIndex - 1 : allImages.length - 1
);
} else if (event.key === "ArrowDown") {
setSelectedIndex((prevIndex) =>
prevIndex < allImages.length - 1 ? prevIndex + 1 : 0
);
}
};

// Add keydown event listener when the component mounts
useEffect(() => {
window.addEventListener("keydown", handleKeyDown);

// Cleanup the event listener when the component unmounts
return () => {
window.removeEventListener("keydown", handleKeyDown);
};
}, []);

// Update selected image when selectedIndex changes
useEffect(() => {
if (allImages[selectedIndex]) {
onSelectJump(allImages[selectedIndex].name);
}
}, [selectedIndex]);


const handleClickLabel = (label) => {
onClick(getActiveImage(state))
onSelectJump(label)
Expand Down
22 changes: 21 additions & 1 deletion client/src/MainLayout/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,27 @@ export const MainLayout = ({
nextImage = state.images[currentImageIndex + 1]
}

useKey("Escape", () => dispatch({ type: "CANCEL" }))

const handleKey = (key, actionType, selectedTool = null) => {
useKey(
(event) => event.ctrlKey && event.shiftKey && event.key.toLowerCase() === key,
() => {
const action = { type: actionType };
if (selectedTool) {
action.selectedTool = selectedTool;
}
dispatch(action);
},
{ event: 'keydown' }
);
};

handleKey('escape', 'CANCEL');
handleKey('b', 'SELECT_TOOL', 'create-box');
handleKey('z', 'SELECT_TOOL', 'zoom');
handleKey('p', 'SELECT_TOOL', 'create-polygon');
handleKey('c', 'SELECT_TOOL', 'create-circle');


const innerContainerRef = useRef()
const hotkeyHandlers = useDispatchHotkeyHandlers({ dispatch })
Expand Down

0 comments on commit 40f71c1

Please sign in to comment.