mirror of
https://github.com/TheRealOwenRees/chess-scribe-website.git
synced 2026-07-23 01:46:57 +00:00
pass ply change to callback
This commit is contained in:
@@ -3,9 +3,10 @@ import { useEffect, useRef } from "react";
|
|||||||
|
|
||||||
interface IProps {
|
interface IProps {
|
||||||
gamePgn: string;
|
gamePgn: string;
|
||||||
|
handlePlyChange: ({ ply, fen }: { ply: number; fen: string }) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
const PgnViewer = ({ gamePgn }: IProps) => {
|
const PgnViewer = ({ gamePgn, handlePlyChange }: IProps) => {
|
||||||
const containerRef = useRef<HTMLDivElement | null>(null);
|
const containerRef = useRef<HTMLDivElement | null>(null);
|
||||||
const viewerRef = useRef(null);
|
const viewerRef = useRef(null);
|
||||||
|
|
||||||
@@ -18,7 +19,6 @@ const PgnViewer = ({ gamePgn }: IProps) => {
|
|||||||
scrollToMove: false,
|
scrollToMove: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
// BOARD EVENT LISTENERS - MOVE INTO HOOK?
|
|
||||||
const boardButtons = document.querySelectorAll(".lpv__controls");
|
const boardButtons = document.querySelectorAll(".lpv__controls");
|
||||||
const movesList = document.querySelectorAll(".lpv__moves");
|
const movesList = document.querySelectorAll(".lpv__moves");
|
||||||
if (!boardButtons || !movesList) return;
|
if (!boardButtons || !movesList) return;
|
||||||
@@ -27,6 +27,7 @@ const PgnViewer = ({ gamePgn }: IProps) => {
|
|||||||
setTimeout(() => handleClick(), 250);
|
setTimeout(() => handleClick(), 250);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Hack using DOM manipulation
|
||||||
const boardEventListener = () => {
|
const boardEventListener = () => {
|
||||||
const variationTags = document.querySelector("variation");
|
const variationTags = document.querySelector("variation");
|
||||||
const moves = [...document.querySelectorAll("move")].filter((m) => {
|
const moves = [...document.querySelectorAll("move")].filter((m) => {
|
||||||
@@ -40,8 +41,9 @@ const PgnViewer = ({ gamePgn }: IProps) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleClick = () => {
|
const handleClick = () => {
|
||||||
const { ply, moves } = boardEventListener();
|
const { ply } = boardEventListener();
|
||||||
console.log(ply, moves);
|
const fen = viewerRef.current?.curData().fen;
|
||||||
|
handlePlyChange({ ply, fen });
|
||||||
};
|
};
|
||||||
|
|
||||||
boardButtons.forEach((button) => {
|
boardButtons.forEach((button) => {
|
||||||
@@ -69,7 +71,7 @@ const PgnViewer = ({ gamePgn }: IProps) => {
|
|||||||
move.removeEventListener("touchstart", clickHandlerDelay);
|
move.removeEventListener("touchstart", clickHandlerDelay);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
}, [gamePgn]);
|
}, [gamePgn, handlePlyChange]);
|
||||||
|
|
||||||
return <div ref={containerRef} className="lpv-board" />;
|
return <div ref={containerRef} className="lpv-board" />;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -7,6 +7,11 @@ export const useChessGame = () => {
|
|||||||
const [gameState, gameDispatch] = useReducer(gameReducer, initialGameState);
|
const [gameState, gameDispatch] = useReducer(gameReducer, initialGameState);
|
||||||
const fileInputRef = useRef<HTMLInputElement | null>(null);
|
const fileInputRef = useRef<HTMLInputElement | null>(null);
|
||||||
|
|
||||||
|
const handlePlyChange = ({ ply, fen }: { ply: number; fen: string }) => {
|
||||||
|
console.log("Ply change", ply, fen);
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
const handleSavePgn = () => {
|
const handleSavePgn = () => {
|
||||||
const pgnString = buildPgnString(gameState);
|
const pgnString = buildPgnString(gameState);
|
||||||
downloadString(pgnString, "game.pgn");
|
downloadString(pgnString, "game.pgn");
|
||||||
@@ -84,6 +89,7 @@ export const useChessGame = () => {
|
|||||||
handleClearGame,
|
handleClearGame,
|
||||||
handleLoadPgn,
|
handleLoadPgn,
|
||||||
handleToggleClock,
|
handleToggleClock,
|
||||||
|
handlePlyChange,
|
||||||
handleToggleDiagram,
|
handleToggleDiagram,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ const ChessboardContainer = () => {
|
|||||||
handleSavePdf={chessGameProps.handleSavePdf}
|
handleSavePdf={chessGameProps.handleSavePdf}
|
||||||
handleToggleClock={chessGameProps.handleToggleClock}
|
handleToggleClock={chessGameProps.handleToggleClock}
|
||||||
handleToggleDiagram={chessGameProps.handleToggleDiagram}
|
handleToggleDiagram={chessGameProps.handleToggleDiagram}
|
||||||
|
handlePlyChange={chessGameProps.handlePlyChange}
|
||||||
fileInputRef={chessGameProps.fileInputRef}
|
fileInputRef={chessGameProps.fileInputRef}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ interface IProps {
|
|||||||
handleSavePdf: () => void;
|
handleSavePdf: () => void;
|
||||||
handleToggleClock: () => void;
|
handleToggleClock: () => void;
|
||||||
handleToggleDiagram: () => void;
|
handleToggleDiagram: () => void;
|
||||||
|
handlePlyChange: ({ ply, fen }: { ply: number; fen: string }) => void;
|
||||||
fileInputRef: RefObject<HTMLInputElement | null>;
|
fileInputRef: RefObject<HTMLInputElement | null>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,6 +24,7 @@ const Chessboard = ({
|
|||||||
handleSavePdf,
|
handleSavePdf,
|
||||||
handleToggleClock,
|
handleToggleClock,
|
||||||
handleToggleDiagram,
|
handleToggleDiagram,
|
||||||
|
handlePlyChange,
|
||||||
fileInputRef,
|
fileInputRef,
|
||||||
}: IProps) => {
|
}: IProps) => {
|
||||||
return (
|
return (
|
||||||
@@ -65,7 +67,10 @@ const Chessboard = ({
|
|||||||
Save PDF
|
Save PDF
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<PgnViewer gamePgn={gameState.pgn || ""} />
|
<PgnViewer
|
||||||
|
gamePgn={gameState.pgn || ""}
|
||||||
|
handlePlyChange={handlePlyChange}
|
||||||
|
/>
|
||||||
<div className="flex justify-between max-w-150 mt-2">
|
<div className="flex justify-between max-w-150 mt-2">
|
||||||
<div className="flex gap-2 items-center">
|
<div className="flex gap-2 items-center">
|
||||||
<label
|
<label
|
||||||
@@ -75,8 +80,8 @@ const Chessboard = ({
|
|||||||
Select Diagram
|
Select Diagram
|
||||||
</label>
|
</label>
|
||||||
<input
|
<input
|
||||||
id="diagram-add"
|
id="diagram-toggle"
|
||||||
name="diagram-add"
|
name="diagram-toggle"
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
className="checkbox-accent"
|
className="checkbox-accent"
|
||||||
onChange={handleToggleDiagram}
|
onChange={handleToggleDiagram}
|
||||||
|
|||||||
Reference in New Issue
Block a user