Merge branch 'trunk' into fen-supp

This commit is contained in:
Christoph Lindstädt 2024-02-04 18:06:29 +01:00 committed by GitHub
commit 131f26d7c4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 68 additions and 4 deletions

View file

@ -42,6 +42,7 @@ export interface GameState {
export type GameActions =
| { type: 'ADD_MOVE_TO_HISTORY'; move: Move }
| { type: 'REMOVE_LAST_MOVE_FROM_HISTORY' }
| { type: 'DISPLAY_NEXT_MOVE_IN_HISTORY' }
| { type: 'DISPLAY_PREVIOUS_MOVE_IN_HISTORY' }
| { type: 'DISPLAY_SELECTED_MOVE_IN_HISTORY'; moveId: string }
@ -56,6 +57,7 @@ export const ChessStudy = ({
}: AppProps) => {
// Parse Obsidian / Code Block Settings
const { boardColor, boardOrientation, fen, viewComments, chessStudyId } =
parseUserConfig(pluginSettings, source);
// Setup Chessground API
@ -85,7 +87,7 @@ export const ChessStudy = ({
(draft, action) => {
switch (action.type) {
case 'DISPLAY_NEXT_MOVE_IN_HISTORY': {
if (!chessView) return draft;
if (!chessView || !draft || draft.study.moves.length == 0) return draft;
displayMoveInHistory(draft, chessView, setChessLogic, {
offset: 1,
@ -95,7 +97,7 @@ export const ChessStudy = ({
return draft;
}
case 'DISPLAY_PREVIOUS_MOVE_IN_HISTORY': {
if (!chessView) return draft;
if (!chessView || !draft || draft.study.moves.length == 0) return draft;
displayMoveInHistory(draft, chessView, setChessLogic, {
offset: -1,
@ -104,8 +106,57 @@ export const ChessStudy = ({
return draft;
}
case 'REMOVE_LAST_MOVE_FROM_HISTORY': {
if (!chessView || !draft || draft.study.moves.length == 0) return draft;
let moves = draft.study.moves;
const currentMoveId = draft.currentMove?.moveId;
const { variant, moveIndex } = findMoveIndex(moves, currentMoveId);
if (variant) {
const parent = moves[variant.parentMoveIndex];
const variantMoves = parent.variants[variant.variantIndex].moves;
const isLastMove = moveIndex === variantMoves.length - 1;
if (isLastMove) {
displayMoveInHistory(draft, chessView, setChessLogic, {
offset: -1,
selectedMoveId: currentMoveId,
});
}
variantMoves.pop();
if (variantMoves.length == 0) {
parent.variants.splice(variant.variantIndex, 1);
}
if (isLastMove) {
draft.currentMove = (variantMoves.length > 0) ? variantMoves[variantMoves.length - 1] : moves[variant.parentMoveIndex];
}
} else {
const isLastMove = moveIndex === moves.length - 1;
if (isLastMove) {
displayMoveInHistory(draft, chessView, setChessLogic, {
offset: -1,
selectedMoveId: currentMoveId,
});
}
moves.pop();
if (isLastMove) {
draft.currentMove = (moves.length > 0) ? moves[moves.length - 1] : null;
}
}
return draft;
}
case 'DISPLAY_SELECTED_MOVE_IN_HISTORY': {
if (!chessView) return draft;
if (!chessView || !draft || draft.study.moves.length == 0) return draft;
const selectedMoveId = action.moveId;
@ -117,6 +168,8 @@ export const ChessStudy = ({
return draft;
}
case 'SYNC_SHAPES': {
if (!chessView || !draft || draft.study.moves.length == 0) return draft;
const move = getCurrentMove(draft);
move.shapes = action.shapes;
@ -125,6 +178,8 @@ export const ChessStudy = ({
return draft;
}
case 'SYNC_COMMENT': {
if (!chessView || !draft || draft.study.moves.length == 0) return draft;
const move = getCurrentMove(draft);
move.comment = action.comment;
@ -262,8 +317,12 @@ export const ChessStudy = ({
<PgnViewer
history={gameState.study.moves}
currentMoveId={gameState.currentMove?.moveId}
firstPlayer={firstPlayer}
initialMoveNumber={initialMoveNumber}
onUndoButtonClick={() =>
dispatch({ type: 'REMOVE_LAST_MOVE_FROM_HISTORY' })
}
onBackButtonClick={() =>
dispatch({ type: 'DISPLAY_PREVIOUS_MOVE_IN_HISTORY' })
}

View file

@ -1,4 +1,4 @@
import { ArrowLeft, ArrowRight, Copy, Save } from 'lucide-react';
import { ArrowLeft, ArrowRight, Save, Undo2 } from 'lucide-react';
import * as React from 'react';
import { ReactNode, useEffect, useMemo, useRef } from 'react';
import { ChessStudyMove } from 'src/lib/storage';
@ -110,6 +110,7 @@ export const PgnViewer = React.memo(
currentMoveId,
firstPlayer,
initialMoveNumber,
onUndoButtonClick,
onBackButtonClick,
onForwardButtonClick,
onMoveItemClick,
@ -120,6 +121,7 @@ export const PgnViewer = React.memo(
currentMoveId: string;
firstPlayer: string;
initialMoveNumber: number;
onUndoButtonClick: () => void;
onBackButtonClick: () => void;
onForwardButtonClick: () => void;
onMoveItemClick: (moveId: string) => void;
@ -295,6 +297,9 @@ export const PgnViewer = React.memo(
</div>
</div>
<div className="button-section">
<button onClick={() => onUndoButtonClick()}>
<Undo2 />
</button>
<button onClick={() => onBackButtonClick()}>
<ArrowLeft />
</button>