feat : use keyboard arrows to move between moves
This commit is contained in:
@@ -7,6 +7,7 @@ import FlipBoardButton from "./flipBoardButton";
|
|||||||
import NextMoveButton from "./nextMoveButton";
|
import NextMoveButton from "./nextMoveButton";
|
||||||
import GoToLastPositionButton from "./goToLastPositionButton";
|
import GoToLastPositionButton from "./goToLastPositionButton";
|
||||||
import SaveButton from "./saveButton";
|
import SaveButton from "./saveButton";
|
||||||
|
import { useEffect } from "react";
|
||||||
|
|
||||||
export default function ReviewPanelToolBar() {
|
export default function ReviewPanelToolBar() {
|
||||||
const board = useAtomValue(boardAtom);
|
const board = useAtomValue(boardAtom);
|
||||||
@@ -15,6 +16,21 @@ export default function ReviewPanelToolBar() {
|
|||||||
|
|
||||||
const boardHistory = board.history();
|
const boardHistory = board.history();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const onKeyDown = (e: KeyboardEvent) => {
|
||||||
|
if (boardHistory.length === 0) return;
|
||||||
|
if (e.key === "ArrowLeft") {
|
||||||
|
undoBoardMove();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener("keydown", onKeyDown);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener("keydown", onKeyDown);
|
||||||
|
};
|
||||||
|
}, [undoBoardMove, boardHistory]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Grid container item justifyContent="center" alignItems="center" xs={12}>
|
<Grid container item justifyContent="center" alignItems="center" xs={12}>
|
||||||
<FlipBoardButton />
|
<FlipBoardButton />
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { Grid, IconButton, Tooltip } from "@mui/material";
|
|||||||
import { useAtomValue } from "jotai";
|
import { useAtomValue } from "jotai";
|
||||||
import { boardAtom, gameAtom } from "../states";
|
import { boardAtom, gameAtom } from "../states";
|
||||||
import { useChessActions } from "@/hooks/useChessActions";
|
import { useChessActions } from "@/hooks/useChessActions";
|
||||||
|
import { useEffect } from "react";
|
||||||
|
|
||||||
export default function NextMoveButton() {
|
export default function NextMoveButton() {
|
||||||
const { makeMove: makeBoardMove } = useChessActions(boardAtom);
|
const { makeMove: makeBoardMove } = useChessActions(boardAtom);
|
||||||
@@ -31,6 +32,20 @@ export default function NextMoveButton() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const onKeyDown = (e: KeyboardEvent) => {
|
||||||
|
if (e.key === "ArrowRight") {
|
||||||
|
addNextGameMoveToBoard();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener("keydown", onKeyDown);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener("keydown", onKeyDown);
|
||||||
|
};
|
||||||
|
}, [addNextGameMoveToBoard]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Tooltip title="Go to next move">
|
<Tooltip title="Go to next move">
|
||||||
<Grid>
|
<Grid>
|
||||||
|
|||||||
Reference in New Issue
Block a user