Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions commitlint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export default {
extends: ['@commitlint/config-conventional'],
rules: {
'body-max-line-length': [0, 'always', 100], // [enabled, condition, value]
},
};
2 changes: 1 addition & 1 deletion docs/stories/advanced-examples/AnalysisBoard.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const AnalysisBoard: Story = {

// when the chess game position changes, find the best move
useEffect(() => {
if (!chessGame.isGameOver() || chessGame.isDraw()) {
if (!(chessGame.isGameOver() || chessGame.isDraw())) {
findBestMove();
}
}, [chessGame.fen()]);
Expand Down
15 changes: 11 additions & 4 deletions src/ChessboardProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ type ContextType = {
positionDifferences: ReturnType<typeof getPositionUpdates>;
newArrowStartSquare: string | null;
newArrowOverSquare: { square: string; color: string } | null;
setNewArrowStartSquare: (square: string) => void;
setNewArrowStartSquare: (square: string | null) => void;
setNewArrowOverSquare: (
square: string,
square: string | null,
modifiers?: { shiftKey: boolean; ctrlKey: boolean },
) => void;
internalArrows: Arrow[];
Expand Down Expand Up @@ -498,13 +498,20 @@ export function ChessboardProvider({
}, [clearArrowsOnClick]);

const setNewArrowOverSquareWithModifiers = useCallback(
(square: string, modifiers?: { shiftKey: boolean; ctrlKey: boolean }) => {
(
square: string | null,
modifiers?: { shiftKey: boolean; ctrlKey: boolean },
) => {
const color = modifiers?.shiftKey
? arrowOptions.secondaryColor
: modifiers?.ctrlKey
? arrowOptions.tertiaryColor
: arrowOptions.color;
setNewArrowOverSquare({ square, color });
if (square) {
setNewArrowOverSquare({ square, color });
} else {
setNewArrowOverSquare(null);
}
},
[arrowOptions],
);
Expand Down
37 changes: 30 additions & 7 deletions src/Square.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { memo } from 'react';
import { memo, useState } from 'react';

import { useChessboardContext } from './ChessboardProvider';
import {
Expand Down Expand Up @@ -27,6 +27,8 @@ export const Square = memo(function Square({
isLightSquare,
isOver,
}: SquareProps) {
// track if we are drawing an arrow so that onSquareRightClick is not fired when we finish drawing an arrow
const [isDrawingArrow, setIsDrawingArrow] = useState(false);
const {
id,
allowDrawingArrows,
Expand Down Expand Up @@ -93,10 +95,14 @@ export const Square = memo(function Square({
}}
onContextMenu={(e) => {
e.preventDefault();
onSquareRightClick?.({
piece: currentPosition[squareId] ?? null,
square: squareId,
});

if (!isDrawingArrow) {
onSquareRightClick?.({
piece: currentPosition[squareId] ?? null,
square: squareId,
});
}
setIsDrawingArrow(false);
}}
onMouseDown={(e) => {
if (e.button === 0) {
Expand All @@ -115,11 +121,20 @@ export const Square = memo(function Square({
}}
onMouseUp={(e) => {
if (e.button === 2) {
if (newArrowStartSquare) {
if (
allowDrawingArrows &&
newArrowStartSquare &&
newArrowStartSquare !== squareId
) {
setIsDrawingArrow(true);
drawArrow(squareId, {
shiftKey: e.shiftKey,
ctrlKey: e.ctrlKey,
});
} else if (newArrowStartSquare === squareId) {
// right clicked the same square - clear the arrow start square
setNewArrowStartSquare(null);
setNewArrowOverSquare(null);
}
}
onSquareMouseUp?.(
Expand All @@ -132,11 +147,19 @@ export const Square = memo(function Square({
}}
onMouseOver={(e) => {
// right mouse button is held down and we are drawing an arrow
if (e.buttons === 2 && newArrowStartSquare) {
if (
e.buttons === 2 &&
allowDrawingArrows &&
newArrowStartSquare &&
newArrowStartSquare !== squareId
) {
setNewArrowOverSquare(squareId, {
shiftKey: e.shiftKey,
ctrlKey: e.ctrlKey,
});
} else if (newArrowStartSquare === squareId) {
// hovering back over the starting square - clear the over square
setNewArrowOverSquare(null);
}
onMouseOverSquare?.({
piece: currentPosition[squareId] ?? null,
Expand Down