Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement clearing of drawables with mouse #321

Merged
merged 5 commits into from
Jun 29, 2024
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
25 changes: 13 additions & 12 deletions src/chessground/Chessground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,19 @@ export function Chessground(
const moveMethod = useAtomValue(moveMethodAtom);

useEffect(() => {
if (ref?.current && !api) {
if (ref?.current == null) return;
if (api) {
api?.set({
...props,
events: {
change: () => {
if (props.setBoardFen && api) {
props.setBoardFen(api.getFen());
}
},
},
});
} else {
Comment on lines -19 to +31
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixing a nit

const chessgroundApi = NativeChessground(ref.current, {
...props,
events: {
Expand All @@ -36,17 +48,6 @@ export function Chessground(
},
});
setApi(chessgroundApi);
} else if (ref?.current && api) {
api?.set({
...props,
events: {
change: () => {
if (props.setBoardFen && api) {
props.setBoardFen(api.getFen());
}
},
},
});
}
}, [api, props, ref]);

Expand Down
18 changes: 18 additions & 0 deletions src/components/boards/Board.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
currentTabAtom,
deckAtomFamily,
enableBoardScrollAtom,
eraseDrawablesOnClickAtom,
forcedEnPassantAtom,
moveInputAtom,
showArrowsAtom,
Expand Down Expand Up @@ -46,6 +47,7 @@ import {
IconDeviceFloppy,
IconEdit,
IconEditOff,
IconEraser,
IconPlus,
IconSwitchVertical,
IconTarget,
Expand Down Expand Up @@ -143,6 +145,7 @@ function Board({
const storeMakeMove = useStore(store, (s) => s.makeMove);
const setHeaders = useStore(store, (s) => s.setHeaders);
const deleteMove = useStore(store, (s) => s.deleteMove);
const clearShapes = useStore(store, (s) => s.clearShapes);
const setShapes = useStore(store, (s) => s.setShapes);
const setFen = useStore(store, (s) => s.setFen);

Expand All @@ -152,6 +155,7 @@ function Board({
const showDests = useAtomValue(showDestsAtom);
const showArrows = useAtomValue(showArrowsAtom);
const showConsecutiveArrows = useAtomValue(showConsecutiveArrowsAtom);
const eraseDrawablesOnClick = useAtomValue(eraseDrawablesOnClickAtom);
const autoPromote = useAtomValue(autoPromoteAtom);
const forcedEP = useAtomValue(forcedEnPassantAtom);
const showCoordinates = useAtomValue(showCoordinatesAtom);
Expand Down Expand Up @@ -324,6 +328,17 @@ function Board({
)}
</ActionIcon>
</Tooltip>
{!eraseDrawablesOnClick && (
<Tooltip label="Clear Drawings">
<ActionIcon
variant={editingMode ? "filled" : "default"}
size="lg"
onClick={() => clearShapes()}
>
<IconEraser size="1.3rem" />
</ActionIcon>
</Tooltip>
)}
{!disableVariations && (
<Tooltip label="Edit Position">
<ActionIcon
Expand Down Expand Up @@ -588,6 +603,9 @@ function Board({
}
className={chessboard}
ref={boardRef}
onClick={() => {
eraseDrawablesOnClick && clearShapes();
}}
onWheel={(e) => {
if (enableBoardScroll) {
if (e.deltaY > 0) {
Expand Down
16 changes: 15 additions & 1 deletion src/components/settings/SettingsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Dirs } from "@/App";
import {
autoPromoteAtom,
autoSaveAtom,
enableBoardScrollAtom,
eraseDrawablesOnClickAtom,
forcedEnPassantAtom,
minimumGamesAtom,
moveInputAtom,
Expand Down Expand Up @@ -222,6 +222,20 @@ export default function Page() {
</div>
<SettingsSwitch atom={showConsecutiveArrowsAtom} />
</Group>
<Group
justify="space-between"
wrap="nowrap"
gap="xl"
className={classes.item}
>
<div>
<Text>{t("Settings.EraseDrawablesOnClick")}</Text>
<Text size="xs" c="dimmed">
{t("Settings.EraseDrawablesOnClick.Desc")}
</Text>
</div>
<SettingsSwitch atom={eraseDrawablesOnClickAtom} />
</Group>
<Group
justify="space-between"
wrap="nowrap"
Expand Down
4 changes: 4 additions & 0 deletions src/state/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ export const showConsecutiveArrowsAtom = atomWithStorage<boolean>(
"show-consecutive-arrows",
false,
);
export const eraseDrawablesOnClickAtom = atomWithStorage<boolean>(
"erase-drawables-on-click",
false,
);
export const autoPromoteAtom = atomWithStorage<boolean>("auto-promote", true);
export const autoSaveAtom = atomWithStorage<boolean>("auto-save", true);
export const previewBoardOnHoverAtom = atomWithStorage<boolean>(
Expand Down
25 changes: 16 additions & 9 deletions src/state/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,9 @@ export const createTreeStore = (id?: string, initialTree?: TreeState) => {
clearShapes: () =>
set(
produce((state) => {
state.dirty = true;
const node = getNodeAtPath(state.root, state.position);
if (node) {
if (node && node.shapes.length > 0) {
state.dirty = true;
node.shapes = [];
}
}),
Expand Down Expand Up @@ -640,16 +640,23 @@ function promoteVariation(state: TreeState, path: number[]) {
function setShapes(state: TreeState, shapes: DrawShape[]) {
const node = getNodeAtPath(state.root, state.position);
if (!node) return state;
const shape = shapes[0];
const index = node.shapes.findIndex(
(s) => s.orig === shape.orig && s.dest === shape.dest,
);

if (index !== -1) {
node.shapes.splice(index, 1);
const [shape] = shapes;
if (shape) {
const index = node.shapes.findIndex(
(s) => s.orig === shape.orig && s.dest === shape.dest,
);

if (index !== -1) {
node.shapes.splice(index, 1);
} else {
node.shapes.push(shape);
}
} else {
node.shapes.push(shape);
node.shapes = [];
}

return state;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i assume this function is always supposed to return state, but this branch previously returned nothing.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reducer can either return a new state, or simply mutate it when it's inside produce from immer. Since the state is a tree, it can be annoying to ensure immutability, so a lot of these functions are inside produce which doesn't require a return.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall i change it back, or is a monomorphic return type acceptable here?

}

function addAnalysis(
Expand Down
3 changes: 3 additions & 0 deletions src/translation/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ export const en_US = {
"Settings.ConsecutiveArrows": "Consecutive Arrows",
"Settings.ConsecutiveArrows.Desc":
"Show multiple arrows for the best line, if it involves moving the same piece several times",
"Settings.EraseDrawablesOnClick": "Erase Drawables On Click",
"Settings.EraseDrawablesOnClick.Desc":
"Clear the board of drawn arrows & circles by left-clicking the mouse",
"Settings.AutoPromition": "Auto Promotion",
"Settings.AutoPromition.Desc":
"Automatically promote to a queen when a pawn reaches the last rank",
Expand Down
3 changes: 3 additions & 0 deletions src/translation/pt_PT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ export const pt_PT = {
"Settings.ConsecutiveArrows": "Setas consecutivas",
"Settings.ConsecutiveArrows.Desc":
"Mostra várias setas para a melhor linha, caso ela consista em mover a mesma peça várias vezes",
"Settings.EraseDrawablesOnClick": "Apagar desenhos ao clicar",
"Settings.EraseDrawablesOnClick.Desc":
"Limpar o tabuleiro das setas e círculos desenhados ao clicar com o botão esquerdo do rato",
"Settings.AutoPromition": "Auto Promoção",
"Settings.AutoPromition.Desc": "Automaticamente promover para rainha",
"Settings.Coordinates": "Coordenadas",
Expand Down
3 changes: 3 additions & 0 deletions src/translation/zh_CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@ export const zh_CN = {
"Settings.ConsecutiveArrows": "连续箭头",
"Settings.ConsecutiveArrows.Desc":
"如果最佳着法中会多次移动同一个棋子,显示多个箭头",
"Settings.EraseDrawablesOnClick": "单击时擦除可绘制对象",
"Settings.EraseDrawablesOnClick.Desc":
"通过单击鼠标左键清除棋盘上绘制的箭头和圆圈",
"Settings.AutoPromition": "自动升变",
"Settings.AutoPromition.Desc": "兵抵达底线时自动升变为后",
"Settings.Coordinates": "坐标",
Expand Down
Loading