From 8a2ecb79448c6c9d35f7b47322d93ce4d1978f85 Mon Sep 17 00:00:00 2001 From: Francisco Salgueiro Date: Mon, 5 Feb 2024 00:28:25 +0000 Subject: [PATCH] delete piece on drop off --- src/chessground/Chessground.tsx | 19 +++++++++++++++++-- src/components/boards/Board.tsx | 31 +++++++++++++++++++------------ 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/chessground/Chessground.tsx b/src/chessground/Chessground.tsx index 4e2454e8..9e1a6549 100644 --- a/src/chessground/Chessground.tsx +++ b/src/chessground/Chessground.tsx @@ -1,16 +1,31 @@ import { Chessground as NativeChessground } from "chessground"; import { Api } from "chessground/api"; import { Config } from "chessground/config"; +import { makeFen, parseFen } from "chessops/fen"; import { useEffect, useRef, useState } from "react"; -export function Chessground(props: Config) { +export function Chessground( + props: Config & { setBoardFen?: (fen: string) => void }, +) { const [api, setApi] = useState(null); const ref = useRef(null); useEffect(() => { if (ref?.current && !api) { - const chessgroundApi = NativeChessground(ref.current, props); + const chessgroundApi = NativeChessground(ref.current, { + ...props, + events: { + change: () => { + if (props.setBoardFen && chessgroundApi) { + const fen = parseFen(chessgroundApi.getFen()); + if (fen.isOk) { + props.setBoardFen(makeFen(fen.value)); + } + } + }, + }, + }); setApi(chessgroundApi); } else if (ref?.current && api) { api.set(props); diff --git a/src/components/boards/Board.tsx b/src/components/boards/Board.tsx index 0c112fc4..be4b0dd8 100644 --- a/src/components/boards/Board.tsx +++ b/src/components/boards/Board.tsx @@ -55,10 +55,9 @@ import { DrawShape } from "chessground/draw"; import { Color } from "chessground/types"; import { NormalMove, Square, SquareName, parseSquare } from "chessops"; import { chessgroundDests } from "chessops/compat"; -import { makeFen, parseFen } from "chessops/fen"; import { makeSan } from "chessops/san"; import { useAtom, useAtomValue, useSetAtom } from "jotai"; -import { memo, useContext, useMemo, useState } from "react"; +import { memo, useContext, useEffect, useMemo, useState } from "react"; import { useHotkeys } from "react-hotkeys-hook"; import { match } from "ts-pattern"; import ShowMaterial from "../common/ShowMaterial"; @@ -344,6 +343,17 @@ function Board({ const bottomProgress = timeControl && bottomClock ? bottomClock / timeControl[0].seconds : 0; + const [boardFen, setBoardFen] = useState(null); + + useEffect(() => { + if (editingMode && boardFen && boardFen !== currentNode.fen) { + dispatch({ + type: "SET_FEN", + payload: boardFen, + }); + } + }, [boardFen, editingMode, dispatch]); + return ( <> @@ -394,8 +404,10 @@ function Board({ orientation={orientation} /> { - if (editingMode) { - const setup = parseFen(currentNode.fen).unwrap(); - const p = setup.board.take(parseSquare(orig)!)!; - setup.board.set(parseSquare(dest)!, p); - dispatch({ - type: "SET_FEN", - payload: makeFen(setup), - }); - } else { + if (!editingMode) { const from = parseSquare(orig)!; const to = parseSquare(dest)!; @@ -452,10 +456,13 @@ function Board({ }} turnColor={turn} check={pos?.isCheck()} - lastMove={moveToKey(currentNode.move)} + lastMove={editingMode ? undefined : moveToKey(currentNode.move)} premovable={{ enabled: false, }} + draggable={{ + deleteOnDropOff: editingMode, + }} drawable={{ enabled: true, visible: true,