Skip to content

Commit 5ded2ac

Browse files
authored
fix: arrow bugs (#216)
- fixed issue where onSquareRightClick would fire upon drawing an arrow - fixed issue where right clicking, dragging into a piece, and releasing, all within the same square, would cause an arrow to show when right clicking on another square - fixed analysis board logic that would still try to find the best move in a draw
1 parent eb7d9bc commit 5ded2ac

File tree

4 files changed

+45
-12
lines changed

4 files changed

+45
-12
lines changed

commitlint.config.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
export default {
22
extends: ['@commitlint/config-conventional'],
3+
rules: {
4+
'body-max-line-length': [0, 'always', 100], // [enabled, condition, value]
5+
},
36
};

docs/stories/advanced-examples/AnalysisBoard.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const AnalysisBoard: Story = {
3535

3636
// when the chess game position changes, find the best move
3737
useEffect(() => {
38-
if (!chessGame.isGameOver() || chessGame.isDraw()) {
38+
if (!(chessGame.isGameOver() || chessGame.isDraw())) {
3939
findBestMove();
4040
}
4141
}, [chessGame.fen()]);

src/ChessboardProvider.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ type ContextType = {
114114
positionDifferences: ReturnType<typeof getPositionUpdates>;
115115
newArrowStartSquare: string | null;
116116
newArrowOverSquare: { square: string; color: string } | null;
117-
setNewArrowStartSquare: (square: string) => void;
117+
setNewArrowStartSquare: (square: string | null) => void;
118118
setNewArrowOverSquare: (
119-
square: string,
119+
square: string | null,
120120
modifiers?: { shiftKey: boolean; ctrlKey: boolean },
121121
) => void;
122122
internalArrows: Arrow[];
@@ -498,13 +498,20 @@ export function ChessboardProvider({
498498
}, [clearArrowsOnClick]);
499499

500500
const setNewArrowOverSquareWithModifiers = useCallback(
501-
(square: string, modifiers?: { shiftKey: boolean; ctrlKey: boolean }) => {
501+
(
502+
square: string | null,
503+
modifiers?: { shiftKey: boolean; ctrlKey: boolean },
504+
) => {
502505
const color = modifiers?.shiftKey
503506
? arrowOptions.secondaryColor
504507
: modifiers?.ctrlKey
505508
? arrowOptions.tertiaryColor
506509
: arrowOptions.color;
507-
setNewArrowOverSquare({ square, color });
510+
if (square) {
511+
setNewArrowOverSquare({ square, color });
512+
} else {
513+
setNewArrowOverSquare(null);
514+
}
508515
},
509516
[arrowOptions],
510517
);

src/Square.tsx

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { memo } from 'react';
1+
import { memo, useState } from 'react';
22

33
import { useChessboardContext } from './ChessboardProvider';
44
import {
@@ -27,6 +27,8 @@ export const Square = memo(function Square({
2727
isLightSquare,
2828
isOver,
2929
}: SquareProps) {
30+
// track if we are drawing an arrow so that onSquareRightClick is not fired when we finish drawing an arrow
31+
const [isDrawingArrow, setIsDrawingArrow] = useState(false);
3032
const {
3133
id,
3234
allowDrawingArrows,
@@ -93,10 +95,14 @@ export const Square = memo(function Square({
9395
}}
9496
onContextMenu={(e) => {
9597
e.preventDefault();
96-
onSquareRightClick?.({
97-
piece: currentPosition[squareId] ?? null,
98-
square: squareId,
99-
});
98+
99+
if (!isDrawingArrow) {
100+
onSquareRightClick?.({
101+
piece: currentPosition[squareId] ?? null,
102+
square: squareId,
103+
});
104+
}
105+
setIsDrawingArrow(false);
100106
}}
101107
onMouseDown={(e) => {
102108
if (e.button === 0) {
@@ -115,11 +121,20 @@ export const Square = memo(function Square({
115121
}}
116122
onMouseUp={(e) => {
117123
if (e.button === 2) {
118-
if (newArrowStartSquare) {
124+
if (
125+
allowDrawingArrows &&
126+
newArrowStartSquare &&
127+
newArrowStartSquare !== squareId
128+
) {
129+
setIsDrawingArrow(true);
119130
drawArrow(squareId, {
120131
shiftKey: e.shiftKey,
121132
ctrlKey: e.ctrlKey,
122133
});
134+
} else if (newArrowStartSquare === squareId) {
135+
// right clicked the same square - clear the arrow start square
136+
setNewArrowStartSquare(null);
137+
setNewArrowOverSquare(null);
123138
}
124139
}
125140
onSquareMouseUp?.(
@@ -132,11 +147,19 @@ export const Square = memo(function Square({
132147
}}
133148
onMouseOver={(e) => {
134149
// right mouse button is held down and we are drawing an arrow
135-
if (e.buttons === 2 && newArrowStartSquare) {
150+
if (
151+
e.buttons === 2 &&
152+
allowDrawingArrows &&
153+
newArrowStartSquare &&
154+
newArrowStartSquare !== squareId
155+
) {
136156
setNewArrowOverSquare(squareId, {
137157
shiftKey: e.shiftKey,
138158
ctrlKey: e.ctrlKey,
139159
});
160+
} else if (newArrowStartSquare === squareId) {
161+
// hovering back over the starting square - clear the over square
162+
setNewArrowOverSquare(null);
140163
}
141164
onMouseOverSquare?.({
142165
piece: currentPosition[squareId] ?? null,

0 commit comments

Comments
 (0)