@dxos/react-ui-gameboard 0.8.4-main.b97322e → 0.8.4-main.bc674ce
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/browser/index.mjs +733 -788
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +733 -788
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/components/Chessboard/Chessboard.d.ts +20 -0
- package/dist/types/src/components/Chessboard/Chessboard.d.ts.map +1 -0
- package/dist/types/src/components/Chessboard/Chessboard.stories.d.ts +30 -0
- package/dist/types/src/components/Chessboard/Chessboard.stories.d.ts.map +1 -0
- package/dist/types/src/components/Chessboard/chess.d.ts +60 -0
- package/dist/types/src/components/Chessboard/chess.d.ts.map +1 -0
- package/dist/types/src/components/Chessboard/chess.test.d.ts +2 -0
- package/dist/types/src/components/Chessboard/chess.test.d.ts.map +1 -0
- package/dist/types/src/components/Chessboard/index.d.ts.map +1 -0
- package/dist/types/src/components/Gameboard/Gameboard.d.ts +38 -0
- package/dist/types/src/components/Gameboard/Gameboard.d.ts.map +1 -0
- package/dist/types/src/{Gameboard → components/Gameboard}/Piece.d.ts +3 -2
- package/dist/types/src/components/Gameboard/Piece.d.ts.map +1 -0
- package/dist/types/src/components/Gameboard/Square.d.ts.map +1 -0
- package/dist/types/src/components/Gameboard/index.d.ts +4 -0
- package/dist/types/src/components/Gameboard/index.d.ts.map +1 -0
- package/dist/types/src/{Gameboard → components/Gameboard}/types.d.ts +3 -2
- package/dist/types/src/components/Gameboard/types.d.ts.map +1 -0
- package/dist/types/src/components/Gameboard/util.d.ts.map +1 -0
- package/dist/types/src/components/index.d.ts +3 -0
- package/dist/types/src/components/index.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +1 -2
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +27 -22
- package/src/{Chessboard → components/Chessboard}/Chessboard.stories.tsx +33 -30
- package/src/{Chessboard → components/Chessboard}/Chessboard.tsx +67 -57
- package/src/components/Chessboard/chess.test.ts +19 -0
- package/src/components/Chessboard/chess.ts +325 -0
- package/src/components/Gameboard/Gameboard.tsx +145 -0
- package/src/{Gameboard → components/Gameboard}/Piece.tsx +25 -23
- package/src/{Gameboard → components/Gameboard}/Square.tsx +5 -5
- package/src/{Gameboard → components/Gameboard}/index.ts +0 -3
- package/src/{Gameboard → components/Gameboard}/types.ts +4 -2
- package/src/components/index.ts +6 -0
- package/src/index.ts +1 -2
- package/dist/types/src/Chessboard/Chessboard.d.ts +0 -15
- package/dist/types/src/Chessboard/Chessboard.d.ts.map +0 -1
- package/dist/types/src/Chessboard/Chessboard.stories.d.ts +0 -16
- package/dist/types/src/Chessboard/Chessboard.stories.d.ts.map +0 -1
- package/dist/types/src/Chessboard/chess.d.ts +0 -40
- package/dist/types/src/Chessboard/chess.d.ts.map +0 -1
- package/dist/types/src/Chessboard/index.d.ts.map +0 -1
- package/dist/types/src/Gameboard/Gameboard.d.ts +0 -23
- package/dist/types/src/Gameboard/Gameboard.d.ts.map +0 -1
- package/dist/types/src/Gameboard/Piece.d.ts.map +0 -1
- package/dist/types/src/Gameboard/Square.d.ts.map +0 -1
- package/dist/types/src/Gameboard/context.d.ts +0 -10
- package/dist/types/src/Gameboard/context.d.ts.map +0 -1
- package/dist/types/src/Gameboard/index.d.ts +0 -7
- package/dist/types/src/Gameboard/index.d.ts.map +0 -1
- package/dist/types/src/Gameboard/types.d.ts.map +0 -1
- package/dist/types/src/Gameboard/util.d.ts.map +0 -1
- package/src/Chessboard/chess.ts +0 -213
- package/src/Gameboard/Gameboard.tsx +0 -103
- package/src/Gameboard/context.ts +0 -22
- /package/dist/types/src/{Chessboard → components/Chessboard}/index.d.ts +0 -0
- /package/dist/types/src/{Gameboard → components/Gameboard}/Square.d.ts +0 -0
- /package/dist/types/src/{Gameboard → components/Gameboard}/util.d.ts +0 -0
- /package/src/{Chessboard → components/Chessboard}/index.ts +0 -0
- /package/src/{Gameboard → components/Gameboard}/util.ts +0 -0
package/package.json
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/react-ui-gameboard",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.bc674ce",
|
|
4
4
|
"description": "Game board.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/dxos/dxos"
|
|
10
|
+
},
|
|
7
11
|
"license": "MIT",
|
|
8
12
|
"author": "DXOS.org",
|
|
9
13
|
"type": "module",
|
|
10
14
|
"exports": {
|
|
11
15
|
".": {
|
|
16
|
+
"source": "./src/index.ts",
|
|
12
17
|
"types": "./dist/types/src/index.d.ts",
|
|
13
18
|
"browser": "./dist/lib/browser/index.mjs",
|
|
14
19
|
"node": "./dist/lib/node-esm/index.mjs"
|
|
@@ -23,36 +28,36 @@
|
|
|
23
28
|
"src"
|
|
24
29
|
],
|
|
25
30
|
"dependencies": {
|
|
26
|
-
"@atlaskit/pragmatic-drag-and-drop": "
|
|
27
|
-
"@atlaskit/pragmatic-drag-and-drop-hitbox": "
|
|
28
|
-
"@
|
|
29
|
-
"@
|
|
31
|
+
"@atlaskit/pragmatic-drag-and-drop": "1.7.7",
|
|
32
|
+
"@atlaskit/pragmatic-drag-and-drop-hitbox": "1.1.0",
|
|
33
|
+
"@effect-atom/atom-react": "^0.4.6",
|
|
34
|
+
"@radix-ui/react-context": "1.1.1",
|
|
30
35
|
"chess.js": "^1.0.0",
|
|
31
36
|
"react-resize-detector": "^11.0.1",
|
|
32
|
-
"@dxos/debug": "0.8.4-main.
|
|
33
|
-
"@dxos/invariant": "0.8.4-main.
|
|
34
|
-
"@dxos/node-std": "0.8.4-main.
|
|
35
|
-
"@dxos/log": "0.8.4-main.
|
|
36
|
-
"@dxos/util": "0.8.4-main.
|
|
37
|
+
"@dxos/debug": "0.8.4-main.bc674ce",
|
|
38
|
+
"@dxos/invariant": "0.8.4-main.bc674ce",
|
|
39
|
+
"@dxos/node-std": "0.8.4-main.bc674ce",
|
|
40
|
+
"@dxos/log": "0.8.4-main.bc674ce",
|
|
41
|
+
"@dxos/util": "0.8.4-main.bc674ce"
|
|
37
42
|
},
|
|
38
43
|
"devDependencies": {
|
|
39
44
|
"@svgr/cli": "^8.1.0",
|
|
40
45
|
"@types/lodash.defaultsdeep": "^4.6.6",
|
|
41
|
-
"@types/react": "~
|
|
42
|
-
"@types/react-dom": "~
|
|
46
|
+
"@types/react": "~19.2.7",
|
|
47
|
+
"@types/react-dom": "~19.2.3",
|
|
43
48
|
"lodash.defaultsdeep": "^4.6.1",
|
|
44
|
-
"react": "~
|
|
45
|
-
"react-dom": "~
|
|
46
|
-
"vite": "
|
|
47
|
-
"@dxos/react-ui
|
|
48
|
-
"@dxos/
|
|
49
|
-
"@dxos/
|
|
49
|
+
"react": "~19.2.3",
|
|
50
|
+
"react-dom": "~19.2.3",
|
|
51
|
+
"vite": "7.1.9",
|
|
52
|
+
"@dxos/react-ui": "0.8.4-main.bc674ce",
|
|
53
|
+
"@dxos/storybook-utils": "0.8.4-main.bc674ce",
|
|
54
|
+
"@dxos/ui-theme": "0.8.4-main.bc674ce"
|
|
50
55
|
},
|
|
51
56
|
"peerDependencies": {
|
|
52
|
-
"react": "~
|
|
53
|
-
"react-dom": "~
|
|
54
|
-
"@dxos/react-ui": "0.8.4-main.
|
|
55
|
-
"@dxos/
|
|
57
|
+
"react": "~19.2.3",
|
|
58
|
+
"react-dom": "~19.2.3",
|
|
59
|
+
"@dxos/react-ui": "0.8.4-main.bc674ce",
|
|
60
|
+
"@dxos/ui-theme": "0.8.4-main.bc674ce"
|
|
56
61
|
},
|
|
57
62
|
"publishConfig": {
|
|
58
63
|
"access": "public"
|
|
@@ -2,31 +2,33 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import '@
|
|
5
|
+
import { RegistryContext } from '@effect-atom/atom-react';
|
|
6
|
+
import { type Meta, type StoryObj } from '@storybook/react-vite';
|
|
7
|
+
import React, { useCallback, useContext, useEffect, useMemo, useState } from 'react';
|
|
6
8
|
|
|
7
|
-
import type { Meta, StoryObj } from '@storybook/react-vite';
|
|
8
|
-
import React, { useCallback, useEffect, useMemo, useState } from 'react';
|
|
9
|
-
|
|
10
|
-
import { log } from '@dxos/log';
|
|
11
9
|
import { Button, Toolbar } from '@dxos/react-ui';
|
|
12
|
-
import { withLayout, withTheme } from '@dxos/
|
|
10
|
+
import { withLayout, withTheme } from '@dxos/react-ui/testing';
|
|
11
|
+
import { withRegistry } from '@dxos/storybook-utils';
|
|
12
|
+
|
|
13
|
+
import { Gameboard, type GameboardRootProps, type Move, type Player } from '../Gameboard';
|
|
13
14
|
|
|
14
|
-
import { Chessboard, type ChessboardProps } from './Chessboard';
|
|
15
15
|
import { ChessModel } from './chess';
|
|
16
|
-
import {
|
|
16
|
+
import { Chessboard, type ChessboardProps } from './Chessboard';
|
|
17
17
|
|
|
18
|
-
type
|
|
19
|
-
|
|
18
|
+
type DefaultStoryProps = Pick<ChessboardProps, 'orientation' | 'showLabels' | 'debug'> & {
|
|
19
|
+
pgn?: string;
|
|
20
20
|
};
|
|
21
21
|
|
|
22
|
-
const DefaultStory = ({
|
|
23
|
-
const
|
|
22
|
+
const DefaultStory = ({ orientation: _orientation, pgn, ...props }: DefaultStoryProps) => {
|
|
23
|
+
const registry = useContext(RegistryContext);
|
|
24
|
+
const model = useMemo(() => new ChessModel(registry, pgn), [registry, pgn]);
|
|
24
25
|
const [orientation, setOrientation] = useState<Player | undefined>(_orientation);
|
|
25
26
|
|
|
26
|
-
const handleDrop = useCallback<NonNullable<GameboardRootProps['onDrop']>>(
|
|
27
|
+
const handleDrop = useCallback<NonNullable<GameboardRootProps<ChessModel>['onDrop']>>(
|
|
27
28
|
(move: Move) => {
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
const result = model.makeMove(move);
|
|
30
|
+
console.log(model.pgn);
|
|
31
|
+
return result;
|
|
30
32
|
},
|
|
31
33
|
[model],
|
|
32
34
|
);
|
|
@@ -34,7 +36,7 @@ const DefaultStory = ({ fen, orientation: _orientation, ...props }: RenderProps)
|
|
|
34
36
|
return (
|
|
35
37
|
<div className='flex flex-col grow gap-2 overflow-hidden'>
|
|
36
38
|
<Toolbar.Root>
|
|
37
|
-
<Button onClick={() => model.
|
|
39
|
+
<Button onClick={() => model.update()}>Reset</Button>
|
|
38
40
|
<Button onClick={() => model.makeRandomMove()}>Move</Button>
|
|
39
41
|
<div className='grow'></div>
|
|
40
42
|
<Button
|
|
@@ -44,7 +46,7 @@ const DefaultStory = ({ fen, orientation: _orientation, ...props }: RenderProps)
|
|
|
44
46
|
</Button>
|
|
45
47
|
</Toolbar.Root>
|
|
46
48
|
<Gameboard.Root model={model} onDrop={handleDrop}>
|
|
47
|
-
<Gameboard.Content>
|
|
49
|
+
<Gameboard.Content grow contain>
|
|
48
50
|
<Chessboard orientation={orientation} {...props} />
|
|
49
51
|
</Gameboard.Content>
|
|
50
52
|
</Gameboard.Root>
|
|
@@ -52,8 +54,9 @@ const DefaultStory = ({ fen, orientation: _orientation, ...props }: RenderProps)
|
|
|
52
54
|
);
|
|
53
55
|
};
|
|
54
56
|
|
|
55
|
-
const
|
|
56
|
-
const
|
|
57
|
+
const GridStory = () => {
|
|
58
|
+
const registry = useContext(RegistryContext);
|
|
59
|
+
const models = useMemo(() => Array.from({ length: 9 }).map(() => new ChessModel(registry)), [registry]);
|
|
57
60
|
useEffect(() => {
|
|
58
61
|
const i = setInterval(() => {
|
|
59
62
|
const model = models[Math.floor(Math.random() * models.length)];
|
|
@@ -63,7 +66,7 @@ const Grid = (props: RenderProps) => {
|
|
|
63
66
|
}, []);
|
|
64
67
|
|
|
65
68
|
return (
|
|
66
|
-
<div className='
|
|
69
|
+
<div className='bs-full aspect-square mx-auto'>
|
|
67
70
|
<div className='grid grid-cols-3 gap-2'>
|
|
68
71
|
{models.map((model, i) => (
|
|
69
72
|
<div key={i} className='aspect-square'>
|
|
@@ -77,34 +80,34 @@ const Grid = (props: RenderProps) => {
|
|
|
77
80
|
);
|
|
78
81
|
};
|
|
79
82
|
|
|
80
|
-
const meta
|
|
83
|
+
const meta = {
|
|
81
84
|
title: 'ui/react-ui-gameboard/Chessboard',
|
|
82
85
|
component: Chessboard,
|
|
83
86
|
render: DefaultStory,
|
|
84
|
-
decorators: [withTheme, withLayout({
|
|
85
|
-
}
|
|
87
|
+
decorators: [withRegistry, withTheme, withLayout({ layout: 'column' })],
|
|
88
|
+
} satisfies Meta<typeof Chessboard>;
|
|
86
89
|
|
|
87
90
|
export default meta;
|
|
88
91
|
|
|
89
|
-
type Story = StoryObj<typeof
|
|
92
|
+
type Story = StoryObj<typeof meta>;
|
|
90
93
|
|
|
91
94
|
export const Default: Story = {};
|
|
92
95
|
|
|
93
96
|
export const Promotion: Story = {
|
|
94
97
|
args: {
|
|
95
|
-
|
|
98
|
+
pgn: '1. e4 e5 2. Nf3 Nc6 3. Bc4 Bc5 4. c3 Nf6 5. d4 exd4 6. cxd4 Bb4+ 7. Nc3 d5 8. exd5 Nxd5 9. O-O Be6 10. Qb3 Na5 11. Qa4+ c6 12. Bxd5 Bxc3 13. Bxe6 fxe6 14. d5 Qg5 15. dxe6 Kf8 16. e7+ Kg8 *',
|
|
96
99
|
},
|
|
97
100
|
};
|
|
98
101
|
|
|
99
102
|
export const Debug: Story = {
|
|
100
103
|
args: {
|
|
101
|
-
|
|
102
|
-
showLabels: true,
|
|
104
|
+
pgn: '1. e4 e5 2. Nf3 Nc6 3. Bc4 Bc5 4. c3 Nf6 5. d4 exd4 6. cxd4 Bb4+ 7. Nc3 d5 8. exd5 Nxd5 9. O-O Be6 10. Qb3 Na5 11. Qa4+ c6 12. Bxd5 Bxc3 13. Bxe6 fxe6 *',
|
|
103
105
|
orientation: 'black',
|
|
104
|
-
|
|
106
|
+
showLabels: true,
|
|
107
|
+
debug: true,
|
|
105
108
|
},
|
|
106
109
|
};
|
|
107
110
|
|
|
108
|
-
export const
|
|
109
|
-
render:
|
|
111
|
+
export const Grid = {
|
|
112
|
+
render: GridStory,
|
|
110
113
|
};
|
|
@@ -2,26 +2,31 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import { Atom, useAtomValue } from '@effect-atom/atom-react';
|
|
6
|
+
import React, { type PropsWithChildren, forwardRef, memo, useEffect, useMemo, useRef, useState } from 'react';
|
|
6
7
|
import { useResizeDetector } from 'react-resize-detector';
|
|
7
8
|
|
|
8
|
-
import { type ThemedClassName,
|
|
9
|
-
import { mx } from '@dxos/
|
|
10
|
-
import {
|
|
9
|
+
import { type ThemedClassName, useForwardedRef } from '@dxos/react-ui';
|
|
10
|
+
import { mx } from '@dxos/ui-theme';
|
|
11
|
+
import { isNonNullable } from '@dxos/util';
|
|
11
12
|
|
|
12
|
-
import { boardStyles, type ChessPiece, ChessPieces, getSquareColor, locationToPos } from './chess';
|
|
13
13
|
import {
|
|
14
14
|
type DOMRectBounds,
|
|
15
|
+
Gameboard,
|
|
15
16
|
type Location,
|
|
17
|
+
type PieceMap,
|
|
16
18
|
type PieceRecord,
|
|
17
19
|
type Player,
|
|
18
|
-
Piece,
|
|
19
|
-
Square,
|
|
20
20
|
getRelativeBounds,
|
|
21
21
|
locationToString,
|
|
22
|
-
|
|
22
|
+
useGameboardContext,
|
|
23
23
|
} from '../Gameboard';
|
|
24
24
|
|
|
25
|
+
import { type ChessModel, type ChessPiece, ChessPieces, boardStyles, getSquareColor, locationToPos } from './chess';
|
|
26
|
+
|
|
27
|
+
/** Fallback atom for when model is undefined. */
|
|
28
|
+
const EMPTY_PIECES_ATOM = Atom.make<PieceMap<ChessPiece>>({});
|
|
29
|
+
|
|
25
30
|
export type ChessboardProps = ThemedClassName<
|
|
26
31
|
PropsWithChildren<{
|
|
27
32
|
orientation?: Player;
|
|
@@ -35,13 +40,15 @@ export type ChessboardProps = ThemedClassName<
|
|
|
35
40
|
/**
|
|
36
41
|
* Chessboard layout.
|
|
37
42
|
*/
|
|
38
|
-
|
|
39
|
-
({ orientation, showLabels, debug, rows = 8, cols = 8
|
|
40
|
-
|
|
41
|
-
const {
|
|
42
|
-
const { model, promoting, onPromotion } =
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
const ChessboardComponent = forwardRef<HTMLDivElement, ChessboardProps>(
|
|
44
|
+
({ classNames, orientation, showLabels, debug, rows = 8, cols = 8 }, forwardedRef) => {
|
|
45
|
+
const targetRef = useForwardedRef(forwardedRef);
|
|
46
|
+
const { width, height } = useResizeDetector({ targetRef, refreshRate: 200 });
|
|
47
|
+
const { model, promoting, onPromotion } = useGameboardContext<ChessModel>(Chessboard.displayName!);
|
|
48
|
+
const pieces = useAtomValue(model?.pieces ?? EMPTY_PIECES_ATOM);
|
|
49
|
+
|
|
50
|
+
// Board squares.
|
|
51
|
+
const squares = useMemo<Location[]>(() => {
|
|
45
52
|
return Array.from({ length: rows }, (_, i) => (orientation === 'black' ? i : rows - 1 - i)).flatMap((row) =>
|
|
46
53
|
Array.from({ length: cols }).map((_, col) => [row, col] as Location),
|
|
47
54
|
);
|
|
@@ -49,7 +56,7 @@ export const Chessboard = memo(
|
|
|
49
56
|
|
|
50
57
|
// Use DOM grid layout to position squares.
|
|
51
58
|
const layout = useMemo(() => {
|
|
52
|
-
return
|
|
59
|
+
return squares.map((location) => {
|
|
53
60
|
return (
|
|
54
61
|
<div
|
|
55
62
|
key={locationToString(location)}
|
|
@@ -59,14 +66,14 @@ export const Chessboard = memo(
|
|
|
59
66
|
/>
|
|
60
67
|
);
|
|
61
68
|
});
|
|
62
|
-
}, [
|
|
69
|
+
}, [squares]);
|
|
63
70
|
|
|
64
71
|
// Build map of square locations to bounds.
|
|
65
72
|
const [grid, setGrid] = useState<Record<string, DOMRectBounds>>({});
|
|
66
73
|
const gridRef = useRef<HTMLDivElement>(null);
|
|
67
74
|
useEffect(() => {
|
|
68
75
|
setGrid(
|
|
69
|
-
|
|
76
|
+
squares.reduce(
|
|
70
77
|
(acc, location) => {
|
|
71
78
|
const square = getSquareLocation(gridRef.current!, location)!;
|
|
72
79
|
const bounds = getRelativeBounds(gridRef.current!, square);
|
|
@@ -75,7 +82,7 @@ export const Chessboard = memo(
|
|
|
75
82
|
{} as Record<string, DOMRectBounds>,
|
|
76
83
|
),
|
|
77
84
|
);
|
|
78
|
-
}, [
|
|
85
|
+
}, [squares, width, height]);
|
|
79
86
|
|
|
80
87
|
// Get the bounds of each square and piece.
|
|
81
88
|
const positions = useMemo<{ piece: PieceRecord; bounds: DOMRectBounds }[]>(() => {
|
|
@@ -83,7 +90,7 @@ export const Chessboard = memo(
|
|
|
83
90
|
return [];
|
|
84
91
|
}
|
|
85
92
|
|
|
86
|
-
return Object.values(
|
|
93
|
+
return Object.values(pieces)
|
|
87
94
|
.map((piece) => {
|
|
88
95
|
if (piece.id === promoting?.id) {
|
|
89
96
|
return null;
|
|
@@ -92,17 +99,19 @@ export const Chessboard = memo(
|
|
|
92
99
|
const bounds = grid[locationToString(piece.location)];
|
|
93
100
|
return { piece, bounds };
|
|
94
101
|
})
|
|
95
|
-
.filter(
|
|
96
|
-
}, [grid,
|
|
102
|
+
.filter(isNonNullable);
|
|
103
|
+
}, [grid, pieces, promoting]);
|
|
97
104
|
|
|
98
105
|
return (
|
|
99
|
-
<div ref={
|
|
106
|
+
<div ref={targetRef} tabIndex={0} className={mx('relative outline-none', classNames)}>
|
|
107
|
+
{/* DOM Layout. */}
|
|
100
108
|
<div ref={gridRef} className='grid grid-rows-8 grid-cols-8 aspect-square select-none'>
|
|
101
109
|
{layout}
|
|
102
110
|
</div>
|
|
111
|
+
{/* Squares. */}
|
|
103
112
|
<div>
|
|
104
|
-
{
|
|
105
|
-
<Square
|
|
113
|
+
{squares.map((location) => (
|
|
114
|
+
<Gameboard.Square
|
|
106
115
|
key={locationToString(location)}
|
|
107
116
|
location={location}
|
|
108
117
|
label={showLabels ? locationToPos(location) : undefined}
|
|
@@ -111,9 +120,10 @@ export const Chessboard = memo(
|
|
|
111
120
|
/>
|
|
112
121
|
))}
|
|
113
122
|
</div>
|
|
123
|
+
{/* Pieces. */}
|
|
114
124
|
<div className={mx(promoting && 'opacity-50')}>
|
|
115
125
|
{positions.map(({ bounds, piece }) => (
|
|
116
|
-
<Piece
|
|
126
|
+
<Gameboard.Piece
|
|
117
127
|
key={piece.id}
|
|
118
128
|
piece={piece}
|
|
119
129
|
bounds={bounds}
|
|
@@ -123,32 +133,29 @@ export const Chessboard = memo(
|
|
|
123
133
|
/>
|
|
124
134
|
))}
|
|
125
135
|
</div>
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
</div>
|
|
136
|
+
{/* Promotion selector. */}
|
|
137
|
+
{promoting && (
|
|
138
|
+
<PromotionSelector
|
|
139
|
+
grid={grid}
|
|
140
|
+
piece={promoting}
|
|
141
|
+
onSelect={(piece) => {
|
|
142
|
+
onPromotion({
|
|
143
|
+
from: Object.values(pieces).find((p) => p.id === promoting.id)!.location,
|
|
144
|
+
to: piece.location,
|
|
145
|
+
piece: promoting.type,
|
|
146
|
+
promotion: piece.type,
|
|
147
|
+
});
|
|
148
|
+
}}
|
|
149
|
+
/>
|
|
150
|
+
)}
|
|
142
151
|
</div>
|
|
143
152
|
);
|
|
144
153
|
},
|
|
145
154
|
);
|
|
146
155
|
|
|
147
|
-
|
|
156
|
+
ChessboardComponent.displayName = 'Chessboard';
|
|
148
157
|
|
|
149
|
-
const
|
|
150
|
-
return container.querySelector(`[data-location="${locationToString(location)}"]`);
|
|
151
|
-
};
|
|
158
|
+
export const Chessboard = memo(ChessboardComponent);
|
|
152
159
|
|
|
153
160
|
const PromotionSelector = ({
|
|
154
161
|
grid,
|
|
@@ -176,19 +183,22 @@ const PromotionSelector = ({
|
|
|
176
183
|
onSelect({ ...piece, type: selected.type });
|
|
177
184
|
};
|
|
178
185
|
|
|
179
|
-
// TODO(burdon): Circle.
|
|
180
186
|
return (
|
|
181
|
-
|
|
187
|
+
<>
|
|
182
188
|
{positions.map(({ piece, bounds }) => (
|
|
183
|
-
<
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
189
|
+
<Gameboard.Piece
|
|
190
|
+
key={piece.id}
|
|
191
|
+
classNames={mx('border-2 border-neutral-700 rounded-full', boardStyles.promotion)}
|
|
192
|
+
piece={piece}
|
|
193
|
+
bounds={bounds}
|
|
194
|
+
Component={ChessPieces[piece.type as ChessPiece]}
|
|
195
|
+
onClick={() => handleSelect(piece)}
|
|
196
|
+
/>
|
|
191
197
|
))}
|
|
192
|
-
|
|
198
|
+
</>
|
|
193
199
|
);
|
|
194
200
|
};
|
|
201
|
+
|
|
202
|
+
const getSquareLocation = (container: HTMLElement, location: Location): HTMLElement | null => {
|
|
203
|
+
return container.querySelector(`[data-location="${locationToString(location)}"]`);
|
|
204
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Chess as ChessJS } from 'chess.js';
|
|
6
|
+
import { describe, it } from 'vitest';
|
|
7
|
+
|
|
8
|
+
import { createPieceMap } from './chess';
|
|
9
|
+
|
|
10
|
+
describe('ChessModel', () => {
|
|
11
|
+
it('should update pieces', ({ expect }) => {
|
|
12
|
+
const chess = new ChessJS();
|
|
13
|
+
chess.loadPgn(
|
|
14
|
+
'1. e4 e5 2. Nf3 Nc6 3. Bc4 Bc5 4. c3 Nf6 5. d4 exd4 6. cxd4 Bb4+ 7. Nc3 d5 8. exd5 Nxd5 9. O-O Be6 10. Qb3 Na5 11. Qa4+ c6 12. Bxd5 Bxc3 13. Bxe6 fxe6 *',
|
|
15
|
+
);
|
|
16
|
+
const pieces = createPieceMap(chess);
|
|
17
|
+
expect(pieces).to.exist;
|
|
18
|
+
});
|
|
19
|
+
});
|