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

Making board snapshot #343

Merged
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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@
"@tiptap/pm": "2.2.4",
"@tiptap/react": "2.2.4",
"@tiptap/starter-kit": "2.2.4",
"@types/dom-to-image": "^2.6.7",
"@vitejs/plugin-react-swc": "^3.6.0",
"chessground": "^9.0.4",
"chessops": "^0.14.0",
"clsx": "^2.0.0",
"dayjs": "^1.11.10",
"dom-to-image": "^2.6.0",
"fast-deep-equal": "^3.1.3",
"fuse.js": "^7.0.0",
"i18next": "^23.11.2",
Expand Down
21 changes: 21 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/chessground/Chessground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function Chessground(
} else {
const chessgroundApi = NativeChessground(ref.current, {
...props,
addDimensionsCssVarsTo: ref.current,
events: {
change: () => {
if (props.setBoardFen && chessgroundApi) {
Expand Down
42 changes: 42 additions & 0 deletions src/components/boards/Board.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
import { notifications } from "@mantine/notifications";
import {
IconArrowBack,
IconCamera,
IconChess,
IconChessFilled,
IconChevronRight,
Expand All @@ -55,6 +56,9 @@ import {
IconTarget,
IconZoomCheck,
} from "@tabler/icons-react";
import { save } from "@tauri-apps/api/dialog";
import { writeBinaryFile } from "@tauri-apps/api/fs";
import { documentDir } from "@tauri-apps/api/path";
import type { DrawShape } from "chessground/draw";
import {
type NormalMove,
Expand All @@ -65,6 +69,7 @@ import {
} from "chessops";
import { chessgroundDests, chessgroundMove } from "chessops/compat";
import { makeSan } from "chessops/san";
import domtoimage from "dom-to-image";
import { useAtom, useAtomValue } from "jotai";
import { memo, useContext, useMemo, useState } from "react";
import { Helmet } from "react-helmet";
Expand Down Expand Up @@ -185,6 +190,34 @@ function Board({
orientation: orientation === "black" ? "white" : "black",
});

const takeSnapshot = async () => {
const ref = boardRef?.current;
if (ref == null) return;

// We must get the first children three levels below, as it has the right dimensions.
const refChildNode = ref.children[0].children[0].children[0] as HTMLElement;
if (refChildNode == null) return;

domtoimage.toBlob(refChildNode).then(async (blob) => {
if (blob == null) return;
const documentsDirPath = await documentDir();

const filePath = await save({
title: "Save board snapshot",
defaultPath: documentsDirPath,
filters: [
{
name: "Png image",
extensions: ["png"],
},
],
});
const arrayBuffer = await blob.arrayBuffer();
if (filePath == null) return;
await writeBinaryFile(filePath, arrayBuffer);
});
};

const keyMap = useAtomValue(keyMapAtom);
useHotkeys(keyMap.SWAP_ORIENTATION.keys, () => toggleOrientation());
const [currentTab, setCurrentTab] = useAtom(currentTabAtom);
Expand Down Expand Up @@ -402,6 +435,15 @@ function Board({
<IconSwitchVertical size="1.3rem" />
</ActionIcon>
</Tooltip>
<Tooltip label={"Take snapshot"}>
<ActionIcon
variant="default"
size="lg"
onClick={() => takeSnapshot()}
>
<IconCamera size="1.3rem" />
</ActionIcon>
</Tooltip>
</ActionIcon.Group>
),
[
Expand Down
Loading