add board listeners

This commit is contained in:
2026-06-02 13:49:34 +02:00
parent 6b21f0bfac
commit 42ef8f20e0
3 changed files with 52 additions and 5 deletions
+3
View File
@@ -0,0 +1,3 @@
// ZOD
FEN string
Env Vars
+47 -2
View File
@@ -1,5 +1,4 @@
import LichessPgnViewer from "lichess-pgn-viewer"; import LichessPgnViewer from "lichess-pgn-viewer";
import type PgnViewerType from "lichess-pgn-viewer/pgnViewer";
import { useEffect, useRef } from "react"; import { useEffect, useRef } from "react";
interface IProps { interface IProps {
@@ -8,7 +7,7 @@ interface IProps {
const PgnViewer = ({ gamePgn }: IProps) => { const PgnViewer = ({ gamePgn }: IProps) => {
const containerRef = useRef<HTMLDivElement | null>(null); const containerRef = useRef<HTMLDivElement | null>(null);
const viewerRef = useRef<PgnViewerType | null>(null); const viewerRef = useRef(null);
useEffect(() => { useEffect(() => {
const element: HTMLElement | null = document.querySelector(".lpv-board"); const element: HTMLElement | null = document.querySelector(".lpv-board");
@@ -19,10 +18,56 @@ const PgnViewer = ({ gamePgn }: IProps) => {
scrollToMove: false, scrollToMove: false,
}); });
// BOARD EVENT LISTENERS - MOVE INTO HOOK?
const boardButtons = document.querySelectorAll(".lpv__controls");
const movesList = document.querySelectorAll(".lpv__moves");
if (!boardButtons || !movesList) return;
const clickHandlerDelay = () => {
setTimeout(() => handleClick(), 250);
};
const boardEventListener = () => {
const variationTags = document.querySelector("variation");
const moves = [...document.querySelectorAll("move")].filter((m) => {
if (!variationTags) return m.className !== "empty";
return (
m.parentNode?.querySelector("variation") && m.className !== "empty"
);
});
const ply = moves.findIndex((m) => m.classList.contains("current")) + 1;
return { moves, ply };
};
const handleClick = () => {
const { ply, moves } = boardEventListener();
console.log(ply, moves);
};
boardButtons.forEach((button) => {
button.addEventListener("click", handleClick);
button.addEventListener("touchstart", handleClick);
});
movesList.forEach((move) => {
move.addEventListener("click", clickHandlerDelay);
move.addEventListener("touchstart", clickHandlerDelay);
});
return () => { return () => {
if (containerRef.current) { if (containerRef.current) {
containerRef.current = null; containerRef.current = null;
} }
boardButtons.forEach((button) => {
button.removeEventListener("click", handleClick);
button.removeEventListener("touchstart", handleClick);
});
movesList.forEach((move) => {
move.removeEventListener("click", clickHandlerDelay);
move.removeEventListener("touchstart", clickHandlerDelay);
});
}; };
}, [gamePgn]); }, [gamePgn]);
+2 -3
View File
@@ -67,14 +67,13 @@ export const useChessGame = () => {
}; };
const handleToggleClock = () => { const handleToggleClock = () => {
console.log("Toggle clock");
gameDispatch({ gameDispatch({
type: "TOGGLE_DIAGRAM_CLOCK", type: "TOGGLE_DIAGRAM_CLOCK",
}); });
}; };
const handleToggleDiagram = () => { const handleToggleDiagram = ({ ply, fen }: { ply: number; fen: string }) => {
console.log("Toggle diagram"); console.log("Toggle diagram", ply, fen);
}; };
return { return {