From d15b71985e5248bbb681f38f8e9f4db2300d6dcb Mon Sep 17 00:00:00 2001 From: latenitecoding Date: Sat, 2 Mar 2024 18:08:48 -0600 Subject: [PATCH] feat: navigation can cycle through board --- src/lib/ui-state/index.ts | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/lib/ui-state/index.ts b/src/lib/ui-state/index.ts index 5cb622e..fae9503 100644 --- a/src/lib/ui-state/index.ts +++ b/src/lib/ui-state/index.ts @@ -65,17 +65,22 @@ export const displayMoveInHistory = ( if (typeof variantMoves[moveIndex + offset] !== 'undefined') { moveToDisplay = variantMoves[moveIndex + offset]; } + + if (typeof moveToDisplay === 'undefined') { + moveToDisplay = moves[variant.parentMoveIndex + offset]; + } } else { if (typeof moves[moveIndex + offset] !== 'undefined') { moveToDisplay = moves[moveIndex + offset]; } } + } else if (offset < 0) { + moveToDisplay = draft.study.moves[draft.study.moves.length - 1]; + } else if (offset > 0) { + moveToDisplay = draft.study.moves[0]; + } - if (!moveToDisplay) { - console.log(`No move to display found`); - return draft; - } - + if (moveToDisplay) { const chess = new Chess(moveToDisplay.after); chessView.set({ @@ -92,6 +97,26 @@ export const displayMoveInHistory = ( draft.currentMove = moveToDisplay; setChessLogic(chess); + } else if (offset !== 0){ + const chess = draft.study.root.fen ? new Chess(draft.study.root.fen) : new Chess(); + + chessView.set({ + fen: chess.fen(), + check: chess.isCheck(), + movable: { + free: false, + color: toColor(chess), + dests: toDests(chess), + }, + turnColor: toColor(chess), + }); + + draft.currentMove = null; + + setChessLogic(chess); + } else { + console.log(`No move to display found`); + return draft; } return draft;