@dxos/plugin-chess 0.8.4-main.f9ba587 → 0.8.4-main.fcfe5033a5

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.
Files changed (215) hide show
  1. package/dist/lib/browser/blueprints/index.mjs +14 -0
  2. package/dist/lib/browser/blueprints/index.mjs.map +7 -0
  3. package/dist/lib/browser/chunk-2TJN26DN.mjs +67 -0
  4. package/dist/lib/browser/chunk-2TJN26DN.mjs.map +7 -0
  5. package/dist/lib/browser/chunk-J5LGTIGS.mjs +10 -0
  6. package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +7 -0
  7. package/dist/lib/browser/chunk-JZMN24DY.mjs +28 -0
  8. package/dist/lib/browser/chunk-JZMN24DY.mjs.map +7 -0
  9. package/dist/lib/browser/chunk-MC7L6JNC.mjs +36 -0
  10. package/dist/lib/browser/chunk-MC7L6JNC.mjs.map +7 -0
  11. package/dist/lib/browser/chunk-XAJZK6Z2.mjs +110 -0
  12. package/dist/lib/browser/chunk-XAJZK6Z2.mjs.map +7 -0
  13. package/dist/lib/browser/cli/index.mjs +34 -0
  14. package/dist/lib/browser/cli/index.mjs.map +7 -0
  15. package/dist/lib/browser/create-AJHN4QT4.mjs +24 -0
  16. package/dist/lib/browser/create-AJHN4QT4.mjs.map +7 -0
  17. package/dist/lib/browser/index.mjs +367 -74
  18. package/dist/lib/browser/index.mjs.map +4 -4
  19. package/dist/lib/browser/meta.json +1 -1
  20. package/dist/lib/browser/move-T2NLRBR4.mjs +36 -0
  21. package/dist/lib/browser/move-T2NLRBR4.mjs.map +7 -0
  22. package/dist/lib/browser/operations/index.mjs +19 -0
  23. package/dist/lib/browser/operations/index.mjs.map +7 -0
  24. package/dist/lib/browser/play-7VTMD3SJ.mjs +45 -0
  25. package/dist/lib/browser/play-7VTMD3SJ.mjs.map +7 -0
  26. package/dist/lib/browser/print-EW7LPW4D.mjs +30 -0
  27. package/dist/lib/browser/print-EW7LPW4D.mjs.map +7 -0
  28. package/dist/lib/browser/types/index.mjs +4 -7
  29. package/dist/lib/node-esm/blueprints/index.mjs +15 -0
  30. package/dist/lib/node-esm/blueprints/index.mjs.map +7 -0
  31. package/dist/lib/node-esm/chunk-4HZWCKMR.mjs +38 -0
  32. package/dist/lib/node-esm/chunk-4HZWCKMR.mjs.map +7 -0
  33. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
  34. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +7 -0
  35. package/dist/lib/node-esm/chunk-LQEDIKCV.mjs +29 -0
  36. package/dist/lib/node-esm/chunk-LQEDIKCV.mjs.map +7 -0
  37. package/dist/lib/node-esm/chunk-NYU5JVXP.mjs +111 -0
  38. package/dist/lib/node-esm/chunk-NYU5JVXP.mjs.map +7 -0
  39. package/dist/lib/node-esm/chunk-OIRJVY2R.mjs +68 -0
  40. package/dist/lib/node-esm/chunk-OIRJVY2R.mjs.map +7 -0
  41. package/dist/lib/node-esm/cli/index.mjs +35 -0
  42. package/dist/lib/node-esm/cli/index.mjs.map +7 -0
  43. package/dist/lib/node-esm/create-QSETDT4N.mjs +25 -0
  44. package/dist/lib/node-esm/create-QSETDT4N.mjs.map +7 -0
  45. package/dist/lib/node-esm/index.mjs +367 -74
  46. package/dist/lib/node-esm/index.mjs.map +4 -4
  47. package/dist/lib/node-esm/meta.json +1 -1
  48. package/dist/lib/node-esm/move-5F2ANDTE.mjs +37 -0
  49. package/dist/lib/node-esm/move-5F2ANDTE.mjs.map +7 -0
  50. package/dist/lib/node-esm/operations/index.mjs +20 -0
  51. package/dist/lib/node-esm/operations/index.mjs.map +7 -0
  52. package/dist/lib/node-esm/play-W4DQCQRX.mjs +46 -0
  53. package/dist/lib/node-esm/play-W4DQCQRX.mjs.map +7 -0
  54. package/dist/lib/node-esm/print-UJHXAXMS.mjs +31 -0
  55. package/dist/lib/node-esm/print-UJHXAXMS.mjs.map +7 -0
  56. package/dist/lib/node-esm/types/index.mjs +4 -7
  57. package/dist/types/src/ChessPlugin.d.ts +2 -1
  58. package/dist/types/src/ChessPlugin.d.ts.map +1 -1
  59. package/dist/types/src/ChessPlugin.test.d.ts +2 -0
  60. package/dist/types/src/ChessPlugin.test.d.ts.map +1 -0
  61. package/dist/types/src/blueprints/chess-blueprint.d.ts +4 -0
  62. package/dist/types/src/blueprints/chess-blueprint.d.ts.map +1 -0
  63. package/dist/types/src/blueprints/index.d.ts +3 -0
  64. package/dist/types/src/blueprints/index.d.ts.map +1 -0
  65. package/dist/types/src/capabilities/artifact-definition.d.ts +3 -2
  66. package/dist/types/src/capabilities/artifact-definition.d.ts.map +1 -1
  67. package/dist/types/src/capabilities/blueprint-definition.d.ts +6 -0
  68. package/dist/types/src/capabilities/blueprint-definition.d.ts.map +1 -0
  69. package/dist/types/src/capabilities/index.d.ts +5 -3
  70. package/dist/types/src/capabilities/index.d.ts.map +1 -1
  71. package/dist/types/src/capabilities/operation-handler.d.ts +6 -0
  72. package/dist/types/src/capabilities/operation-handler.d.ts.map +1 -0
  73. package/dist/types/src/capabilities/react-surface.d.ts +3 -2
  74. package/dist/types/src/capabilities/react-surface.d.ts.map +1 -1
  75. package/dist/types/src/cli/index.d.ts +2 -0
  76. package/dist/types/src/cli/index.d.ts.map +1 -0
  77. package/dist/types/src/cli/plugin.d.ts +3 -0
  78. package/dist/types/src/cli/plugin.d.ts.map +1 -0
  79. package/dist/types/src/components/Chessboard/Chessboard.d.ts +34 -0
  80. package/dist/types/src/components/Chessboard/Chessboard.d.ts.map +1 -0
  81. package/dist/types/src/components/Chessboard/Chessboard.stories.d.ts +48 -0
  82. package/dist/types/src/components/Chessboard/Chessboard.stories.d.ts.map +1 -0
  83. package/dist/types/src/components/Chessboard/Info.d.ts +21 -0
  84. package/dist/types/src/components/Chessboard/Info.d.ts.map +1 -0
  85. package/dist/types/src/components/Chessboard/index.d.ts +2 -0
  86. package/dist/types/src/components/Chessboard/index.d.ts.map +1 -0
  87. package/dist/types/src/components/Chessboard/types.d.ts +8 -0
  88. package/dist/types/src/components/Chessboard/types.d.ts.map +1 -0
  89. package/dist/types/src/components/index.d.ts +1 -5
  90. package/dist/types/src/components/index.d.ts.map +1 -1
  91. package/dist/types/src/containers/ChessArticle/ChessArticle.d.ts +6 -0
  92. package/dist/types/src/containers/ChessArticle/ChessArticle.d.ts.map +1 -0
  93. package/dist/types/src/containers/ChessArticle/ChessArticle.stories.d.ts +47 -0
  94. package/dist/types/src/containers/ChessArticle/ChessArticle.stories.d.ts.map +1 -0
  95. package/dist/types/src/containers/ChessArticle/index.d.ts +2 -0
  96. package/dist/types/src/containers/ChessArticle/index.d.ts.map +1 -0
  97. package/dist/types/src/containers/ChessCard/ChessCard.d.ts +6 -0
  98. package/dist/types/src/containers/ChessCard/ChessCard.d.ts.map +1 -0
  99. package/dist/types/src/containers/ChessCard/ChessCard.stories.d.ts +15 -0
  100. package/dist/types/src/containers/ChessCard/ChessCard.stories.d.ts.map +1 -0
  101. package/dist/types/src/containers/ChessCard/index.d.ts +2 -0
  102. package/dist/types/src/containers/ChessCard/index.d.ts.map +1 -0
  103. package/dist/types/src/containers/index.d.ts +4 -0
  104. package/dist/types/src/containers/index.d.ts.map +1 -0
  105. package/dist/types/src/index.d.ts +3 -2
  106. package/dist/types/src/index.d.ts.map +1 -1
  107. package/dist/types/src/meta.d.ts +2 -3
  108. package/dist/types/src/meta.d.ts.map +1 -1
  109. package/dist/types/src/operations/create.d.ts +5 -0
  110. package/dist/types/src/operations/create.d.ts.map +1 -0
  111. package/dist/types/src/operations/definitions.d.ts +51 -0
  112. package/dist/types/src/operations/definitions.d.ts.map +1 -0
  113. package/dist/types/src/operations/index.d.ts +4 -0
  114. package/dist/types/src/operations/index.d.ts.map +1 -0
  115. package/dist/types/src/operations/move.d.ts +5 -0
  116. package/dist/types/src/operations/move.d.ts.map +1 -0
  117. package/dist/types/src/operations/play.d.ts +5 -0
  118. package/dist/types/src/operations/play.d.ts.map +1 -0
  119. package/dist/types/src/operations/print.d.ts +5 -0
  120. package/dist/types/src/operations/print.d.ts.map +1 -0
  121. package/dist/types/src/translations.d.ts +22 -10
  122. package/dist/types/src/translations.d.ts.map +1 -1
  123. package/dist/types/src/types/Chess.d.ts +27 -0
  124. package/dist/types/src/types/Chess.d.ts.map +1 -0
  125. package/dist/types/src/types/index.d.ts +1 -2
  126. package/dist/types/src/types/index.d.ts.map +1 -1
  127. package/dist/types/tsconfig.tsbuildinfo +1 -1
  128. package/package.json +77 -35
  129. package/src/ChessPlugin.test.ts +65 -0
  130. package/src/ChessPlugin.tsx +42 -53
  131. package/src/blueprints/chess-blueprint.ts +35 -0
  132. package/src/blueprints/index.ts +6 -0
  133. package/src/capabilities/artifact-definition.ts +108 -97
  134. package/src/capabilities/blueprint-definition.ts +17 -0
  135. package/src/capabilities/index.ts +8 -4
  136. package/src/capabilities/operation-handler.ts +14 -0
  137. package/src/capabilities/react-surface.tsx +27 -22
  138. package/src/cli/index.ts +5 -0
  139. package/src/cli/plugin.ts +37 -0
  140. package/src/components/Chessboard/Chessboard.stories.tsx +94 -0
  141. package/src/components/Chessboard/Chessboard.tsx +180 -0
  142. package/src/components/Chessboard/Info.tsx +253 -0
  143. package/src/components/Chessboard/index.ts +5 -0
  144. package/src/components/Chessboard/types.ts +18 -0
  145. package/src/components/index.ts +2 -6
  146. package/src/containers/ChessArticle/ChessArticle.stories.tsx +53 -0
  147. package/src/containers/ChessArticle/ChessArticle.tsx +73 -0
  148. package/src/containers/ChessArticle/index.ts +5 -0
  149. package/src/containers/ChessCard/ChessCard.stories.tsx +42 -0
  150. package/src/containers/ChessCard/ChessCard.tsx +22 -0
  151. package/src/containers/ChessCard/index.ts +5 -0
  152. package/src/containers/index.ts +8 -0
  153. package/src/index.ts +3 -2
  154. package/src/meta.ts +9 -7
  155. package/src/operations/create.ts +21 -0
  156. package/src/operations/definitions.ts +109 -0
  157. package/src/operations/index.ts +16 -0
  158. package/src/operations/move.ts +36 -0
  159. package/src/operations/play.ts +45 -0
  160. package/src/operations/print.ts +28 -0
  161. package/src/translations.ts +22 -10
  162. package/src/types/Chess.ts +62 -0
  163. package/src/types/index.ts +1 -2
  164. package/dist/lib/browser/ChessContainer-DL5J6VIK.mjs +0 -101
  165. package/dist/lib/browser/ChessContainer-DL5J6VIK.mjs.map +0 -7
  166. package/dist/lib/browser/artifact-definition-4AMZY5LQ.mjs +0 -182
  167. package/dist/lib/browser/artifact-definition-4AMZY5LQ.mjs.map +0 -7
  168. package/dist/lib/browser/chunk-2VF6U52K.mjs +0 -8
  169. package/dist/lib/browser/chunk-2VF6U52K.mjs.map +0 -7
  170. package/dist/lib/browser/chunk-A3WEUKPK.mjs +0 -61
  171. package/dist/lib/browser/chunk-A3WEUKPK.mjs.map +0 -7
  172. package/dist/lib/browser/chunk-AZSYKAFK.mjs +0 -61
  173. package/dist/lib/browser/chunk-AZSYKAFK.mjs.map +0 -7
  174. package/dist/lib/browser/intent-resolver-KQYYX44R.mjs +0 -28
  175. package/dist/lib/browser/intent-resolver-KQYYX44R.mjs.map +0 -7
  176. package/dist/lib/browser/react-surface-4TBVZUKU.mjs +0 -43
  177. package/dist/lib/browser/react-surface-4TBVZUKU.mjs.map +0 -7
  178. package/dist/lib/node-esm/ChessContainer-WVOL2KSE.mjs +0 -102
  179. package/dist/lib/node-esm/ChessContainer-WVOL2KSE.mjs.map +0 -7
  180. package/dist/lib/node-esm/artifact-definition-TT3747JB.mjs +0 -183
  181. package/dist/lib/node-esm/artifact-definition-TT3747JB.mjs.map +0 -7
  182. package/dist/lib/node-esm/chunk-2DRWEDVT.mjs +0 -63
  183. package/dist/lib/node-esm/chunk-2DRWEDVT.mjs.map +0 -7
  184. package/dist/lib/node-esm/chunk-IS3Y25PL.mjs +0 -63
  185. package/dist/lib/node-esm/chunk-IS3Y25PL.mjs.map +0 -7
  186. package/dist/lib/node-esm/chunk-TD2SWK2H.mjs +0 -10
  187. package/dist/lib/node-esm/chunk-TD2SWK2H.mjs.map +0 -7
  188. package/dist/lib/node-esm/intent-resolver-GXK2JROQ.mjs +0 -29
  189. package/dist/lib/node-esm/intent-resolver-GXK2JROQ.mjs.map +0 -7
  190. package/dist/lib/node-esm/react-surface-PAUEOIBL.mjs +0 -44
  191. package/dist/lib/node-esm/react-surface-PAUEOIBL.mjs.map +0 -7
  192. package/dist/types/src/capabilities/intent-resolver.d.ts +0 -4
  193. package/dist/types/src/capabilities/intent-resolver.d.ts.map +0 -1
  194. package/dist/types/src/components/ChessComponent.d.ts +0 -6
  195. package/dist/types/src/components/ChessComponent.d.ts.map +0 -1
  196. package/dist/types/src/components/ChessContainer.d.ts +0 -8
  197. package/dist/types/src/components/ChessContainer.d.ts.map +0 -1
  198. package/dist/types/src/components/ChessPanel.d.ts +0 -11
  199. package/dist/types/src/components/ChessPanel.d.ts.map +0 -1
  200. package/dist/types/src/components/Chessboard.stories.d.ts +0 -8
  201. package/dist/types/src/components/Chessboard.stories.d.ts.map +0 -1
  202. package/dist/types/src/components/PlayerSelector.d.ts +0 -8
  203. package/dist/types/src/components/PlayerSelector.d.ts.map +0 -1
  204. package/dist/types/src/types/schema.d.ts +0 -29
  205. package/dist/types/src/types/schema.d.ts.map +0 -1
  206. package/dist/types/src/types/types.d.ts +0 -32
  207. package/dist/types/src/types/types.d.ts.map +0 -1
  208. package/src/capabilities/intent-resolver.ts +0 -26
  209. package/src/components/ChessComponent.tsx +0 -54
  210. package/src/components/ChessContainer.tsx +0 -35
  211. package/src/components/ChessPanel.tsx +0 -67
  212. package/src/components/Chessboard.stories.tsx +0 -39
  213. package/src/components/PlayerSelector.tsx +0 -80
  214. package/src/types/schema.ts +0 -34
  215. package/src/types/types.ts +0 -20
@@ -2,29 +2,34 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
+ import * as Effect from 'effect/Effect';
5
6
  import React from 'react';
6
7
 
7
- import { Capabilities, contributes, createSurface } from '@dxos/app-framework';
8
- import { Obj } from '@dxos/echo';
8
+ import { Capabilities, Capability } from '@dxos/app-framework';
9
+ import { Surface } from '@dxos/app-framework/ui';
10
+ import { AppSurface } from '@dxos/app-toolkit/ui';
9
11
 
10
- import { ChessContainer, ChessComponent } from '../components';
11
- import { meta } from '../meta';
12
- import { ChessType } from '../types';
12
+ import { ChessArticle, ChessCard } from '#containers';
13
+ import { Chess } from '#types';
13
14
 
14
- export default () =>
15
- contributes(Capabilities.ReactSurface, [
16
- createSurface({
17
- id: meta.id,
18
- role: ['article', 'section'],
19
- // TODO(burdon): Could this be standardized so that we don't require a subject property (like below)?
20
- filter: (data): data is { subject: ChessType } => Obj.instanceOf(ChessType, data.subject),
21
- component: ({ data, role }) => <ChessContainer game={data.subject} role={role} />,
22
- }),
23
- createSurface({
24
- id: 'plugin-chess',
25
- // TODO(burdon): Change role to card?
26
- role: 'canvas-node',
27
- filter: Obj.instanceOf(ChessType),
28
- component: ({ data }) => <ChessComponent game={data} />,
29
- }),
30
- ]);
15
+ export default Capability.makeModule(() =>
16
+ Effect.succeed(
17
+ Capability.contributes(Capabilities.ReactSurface, [
18
+ Surface.create({
19
+ id: 'game',
20
+ filter: AppSurface.oneOf(
21
+ AppSurface.object(AppSurface.Article, Chess.Game),
22
+ AppSurface.object(AppSurface.Section, Chess.Game),
23
+ ),
24
+ component: ({ data, role }) => (
25
+ <ChessArticle role={role} subject={data.subject} attendableId={data.attendableId} />
26
+ ),
27
+ }),
28
+ Surface.create({
29
+ id: 'game-card',
30
+ filter: AppSurface.object(AppSurface.Card, Chess.Game),
31
+ component: ({ data, role }) => <ChessCard role={role} subject={data.subject} />,
32
+ }),
33
+ ]),
34
+ ),
35
+ );
@@ -0,0 +1,5 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ export * from './plugin';
@@ -0,0 +1,37 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import * as Effect from 'effect/Effect';
6
+
7
+ import { Plugin } from '@dxos/app-framework';
8
+ import { AppPlugin } from '@dxos/app-toolkit';
9
+ import { Operation } from '@dxos/operation';
10
+ import { SpaceOperation } from '@dxos/plugin-space/operations';
11
+ import { type CreateObject } from '@dxos/plugin-space/types';
12
+
13
+ import { meta } from '#meta';
14
+ import { Chess } from '#types';
15
+
16
+ // TODO(wittjosiah): Factor out shared modules.
17
+ export const ChessPlugin = Plugin.define(meta).pipe(
18
+ AppPlugin.addMetadataModule({
19
+ metadata: {
20
+ id: Chess.Game.typename,
21
+ metadata: {
22
+ createObject: ((props, options) =>
23
+ Effect.gen(function* () {
24
+ const object = Chess.make(props);
25
+ return yield* Operation.invoke(SpaceOperation.AddObject, {
26
+ object,
27
+ target: options.target,
28
+ hidden: true,
29
+ targetNodeId: options.targetNodeId,
30
+ });
31
+ })) satisfies CreateObject,
32
+ },
33
+ },
34
+ }),
35
+ AppPlugin.addSchemaModule({ schema: [Chess.Game] }),
36
+ Plugin.make,
37
+ );
@@ -0,0 +1,94 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { type Meta, type StoryObj } from '@storybook/react-vite';
6
+ import React, { useCallback, useMemo, useRef, useState } from 'react';
7
+
8
+ import { withClientProvider } from '@dxos/react-client/testing';
9
+ import { type Player } from '@dxos/react-ui-gameboard';
10
+ import { withLayout, withTheme } from '@dxos/react-ui/testing';
11
+
12
+ import { Chess } from '#types';
13
+
14
+ import { translations } from '../../translations';
15
+ import { Chessboard, type ChessboardController, type ChessboardInfoProps } from './Chessboard';
16
+
17
+ const SAMPLE_PGN =
18
+ '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 *';
19
+
20
+ const PROMOTION_PGN =
21
+ '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 *';
22
+
23
+ type DefaultStoryProps = {
24
+ pgn?: string;
25
+ };
26
+
27
+ const DefaultStory = ({ pgn }: DefaultStoryProps) => {
28
+ const game = useMemo(() => Chess.make(pgn ? { pgn } : undefined), [pgn]);
29
+ const [orientation, setOrientation] = useState<Player>('white');
30
+ const [showInfo, setShowInfo] = useState(true);
31
+ const controller = useRef<ChessboardController>(null);
32
+
33
+ const handleSelect = useCallback<NonNullable<ChessboardInfoProps['onSelect']>>((index) => {
34
+ controller.current?.setMoveNumber(index);
35
+ }, []);
36
+
37
+ return (
38
+ <Chessboard.Root game={game} ref={controller}>
39
+ <div className='flex h-full w-full gap-4 p-4 overflow-hidden'>
40
+ <Chessboard.Content classNames='grow'>
41
+ <Chessboard.Board classNames='border rounded-xs' orientation={orientation} />
42
+ </Chessboard.Content>
43
+ {showInfo && (
44
+ <div className='flex flex-col justify-center items-center overflow-hidden'>
45
+ <Chessboard.Info
46
+ orientation={orientation}
47
+ min={8}
48
+ max={8}
49
+ onOrientationChange={setOrientation}
50
+ onClose={() => setShowInfo(false)}
51
+ onSelect={handleSelect}
52
+ />
53
+ </div>
54
+ )}
55
+ </div>
56
+ </Chessboard.Root>
57
+ );
58
+ };
59
+
60
+ const meta = {
61
+ title: 'plugins/plugin-chess/components/Chessboard',
62
+ component: DefaultStory,
63
+ decorators: [withTheme(), withLayout({ layout: 'fullscreen' }), withClientProvider({ createIdentity: true })],
64
+ parameters: {
65
+ layout: 'fullscreen',
66
+ translations,
67
+ },
68
+ } satisfies Meta<typeof DefaultStory>;
69
+
70
+ export default meta;
71
+
72
+ type Story = StoryObj<typeof meta>;
73
+
74
+ export const Default: Story = {
75
+ args: {},
76
+ };
77
+
78
+ export const WithPgn: Story = {
79
+ args: {
80
+ pgn: SAMPLE_PGN,
81
+ },
82
+ };
83
+
84
+ export const Promotion: Story = {
85
+ args: {
86
+ pgn: PROMOTION_PGN,
87
+ },
88
+ };
89
+
90
+ export const NewGame: Story = {
91
+ args: {
92
+ pgn: undefined,
93
+ },
94
+ };
@@ -0,0 +1,180 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { RegistryContext } from '@effect-atom/atom-react';
6
+ import React, {
7
+ type PropsWithChildren,
8
+ forwardRef,
9
+ useCallback,
10
+ useContext,
11
+ useEffect,
12
+ useImperativeHandle,
13
+ useMemo,
14
+ useRef,
15
+ } from 'react';
16
+
17
+ import { addEventListener } from '@dxos/async';
18
+ import { useObject } from '@dxos/echo-react';
19
+ import { type ThemedClassName } from '@dxos/react-ui';
20
+ import {
21
+ type ChessModel,
22
+ Gameboard,
23
+ type GameboardRootProps,
24
+ Chessboard as NaturalChessboard,
25
+ type ChessboardProps as NaturalChessboardProps,
26
+ getRawPgn,
27
+ useGameboardContext,
28
+ } from '@dxos/react-ui-gameboard';
29
+ import { useSoundEffect } from '@dxos/react-ui-sfx';
30
+
31
+ import { type Chess } from '#types';
32
+
33
+ import { Info, type InfoProps } from './Info';
34
+ import { ExtendedChessModel } from './types';
35
+
36
+ export interface ChessboardController {
37
+ setMoveNumber(index: number): void;
38
+ }
39
+
40
+ //
41
+ // Root
42
+ //
43
+
44
+ type RootProps = PropsWithChildren<{
45
+ game: Chess.Game;
46
+ }>;
47
+
48
+ const Root = forwardRef<ChessboardController, RootProps>(({ game, children }, forwardedRef) => {
49
+ const registry = useContext(RegistryContext);
50
+ const model = useMemo(() => new ExtendedChessModel(registry, game), [registry]);
51
+ const click = useSoundEffect('Click');
52
+ const [pgn, updatePgn] = useObject(game, 'pgn');
53
+
54
+ // Controller.
55
+ useImperativeHandle(forwardedRef, () => {
56
+ return {
57
+ setMoveNumber: (index) => model.setMoveIndex(index),
58
+ };
59
+ }, [model]);
60
+
61
+ // TODO(wittjosiah): Rather than useEffect to sync ECHO object to model, create an alternative model implementation.
62
+ // This model would take the ECHO object and derive game state directly from that.
63
+ // Mutation methods would mutate the ECHO object and other state would be derived from that.
64
+
65
+ // External change.
66
+ // NOTE: Warning if user has not interacted with the board.
67
+ useEffect(() => {
68
+ if (model.pgn !== getRawPgn(pgn ?? '')) {
69
+ const silent = model.pgn === '*';
70
+ model.update(pgn);
71
+ if (!silent) {
72
+ void click.play();
73
+ }
74
+ }
75
+ }, [pgn]);
76
+
77
+ // Move.
78
+ const handleDrop = useCallback<NonNullable<GameboardRootProps<ChessModel>['onDrop']>>(
79
+ (move) => {
80
+ if (!model.makeMove(move)) {
81
+ return false;
82
+ }
83
+
84
+ void click.play();
85
+ updatePgn(() => model.pgn);
86
+ return true;
87
+ },
88
+ [model],
89
+ );
90
+
91
+ return (
92
+ <Gameboard.Root model={model} onDrop={handleDrop}>
93
+ {children}
94
+ </Gameboard.Root>
95
+ );
96
+ });
97
+
98
+ //
99
+ // Content
100
+ //
101
+
102
+ type Role = 'card--content';
103
+
104
+ type ContentProps = ThemedClassName<PropsWithChildren<{ role?: Role }>>;
105
+
106
+ const Content = ({ classNames, children }: ContentProps) => {
107
+ return (
108
+ <Gameboard.Content grow contain classNames={classNames}>
109
+ {children}
110
+ </Gameboard.Content>
111
+ );
112
+ };
113
+
114
+ //
115
+ // Board
116
+ //
117
+
118
+ const BOARD_NAME = 'Chessboard.Board';
119
+
120
+ type BoardProps = NaturalChessboardProps;
121
+
122
+ const Board = (props: BoardProps) => {
123
+ const registry = useContext(RegistryContext);
124
+ const { model } = useGameboardContext<ChessModel>(BOARD_NAME);
125
+
126
+ // Keyboard navigation.
127
+ const ref = useRef<HTMLDivElement>(null);
128
+ useEffect(() => {
129
+ if (!ref.current) {
130
+ return;
131
+ }
132
+
133
+ // Participate in keyboard navigation (set tabIndex={0})
134
+ ref.current.setAttribute('data-arrow-keys', 'all');
135
+
136
+ return addEventListener(ref.current, 'keydown', (ev) => {
137
+ const moveIndex = registry.get(model.moveIndex);
138
+ switch (ev.key) {
139
+ case 'ArrowUp':
140
+ model.setMoveIndex(0);
141
+ break;
142
+ case 'ArrowDown':
143
+ model.setMoveIndex(model.game.history().length);
144
+ break;
145
+ case 'ArrowLeft':
146
+ if (moveIndex > 0) {
147
+ model.setMoveIndex(moveIndex - 1);
148
+ }
149
+ break;
150
+ case 'ArrowRight':
151
+ if (moveIndex < model.game.history().length) {
152
+ model.setMoveIndex(moveIndex + 1);
153
+ }
154
+ break;
155
+ }
156
+ });
157
+ }, [registry, model]);
158
+
159
+ return <NaturalChessboard {...props} ref={ref} />;
160
+ };
161
+
162
+ Board.displayName = BOARD_NAME;
163
+
164
+ //
165
+ // Chessboard
166
+ //
167
+
168
+ export const Chessboard = {
169
+ Root: Root,
170
+ Content: Content,
171
+ Board: Board,
172
+ Info: Info,
173
+ };
174
+
175
+ export type {
176
+ RootProps as ChessboardRootProps,
177
+ ContentProps as ChessboardContentProps,
178
+ BoardProps as ChessboardBoardProps,
179
+ InfoProps as ChessboardInfoProps,
180
+ };
@@ -0,0 +1,253 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import { useAtomValue } from '@effect-atom/atom-react';
6
+ import React, { type JSX, type PropsWithChildren, useEffect, useMemo, useRef } from 'react';
7
+
8
+ import { generateName } from '@dxos/display-name';
9
+ import { Obj } from '@dxos/echo';
10
+ import { type SpaceMember, useMembers } from '@dxos/react-client/echo';
11
+ import { Icon, IconButton, Select, type ThemedClassName, useTranslation } from '@dxos/react-ui';
12
+ import { type Player, useGameboardContext } from '@dxos/react-ui-gameboard';
13
+ import { mx } from '@dxos/ui-theme';
14
+
15
+ import { meta } from '#meta';
16
+
17
+ import { type ExtendedChessModel } from './types';
18
+
19
+ const INFO_NAME = 'Chessboard.Info';
20
+
21
+ export type InfoProps = ThemedClassName<
22
+ {
23
+ orientation?: Player;
24
+ onOrientationChange?: (orientation: Player) => void;
25
+ onClose?: () => void;
26
+ } & Pick<HistoryProps, 'min' | 'max' | 'onSelect'>
27
+ >;
28
+
29
+ export const Info = ({ classNames, orientation = 'white', onOrientationChange, onClose, ...props }: InfoProps) => {
30
+ const { t } = useTranslation(meta.id);
31
+ const { model } = useGameboardContext<ExtendedChessModel>(INFO_NAME);
32
+ const db = Obj.getDatabase(model.object);
33
+ const members = useMembers(db?.spaceId);
34
+
35
+ return (
36
+ <div
37
+ className={mx(
38
+ 'grid grid-rows-[min-content_1fr_min-content] w-full min-w-[18rem] p-2 overflow-hidden bg-group-surface rounded-xs',
39
+ classNames,
40
+ )}
41
+ >
42
+ <PlayerIndicator
43
+ model={model}
44
+ player={orientation === 'white' ? 'black' : 'white'}
45
+ icon={
46
+ onClose && (
47
+ <IconButton
48
+ variant='ghost'
49
+ icon='ph--x--regular'
50
+ iconOnly
51
+ label={t('close-info.button')}
52
+ size={4}
53
+ onClick={onClose}
54
+ />
55
+ )
56
+ }
57
+ >
58
+ <PlayerSelector
59
+ value={model.object.players?.[orientation === 'white' ? 'black' : 'white']}
60
+ onValueChange={(value) => {
61
+ Obj.change(model.object, (obj) => {
62
+ obj.players![orientation === 'white' ? 'black' : 'white'] = value;
63
+ });
64
+ }}
65
+ members={members}
66
+ />
67
+ </PlayerIndicator>
68
+
69
+ <History model={model} {...props} />
70
+
71
+ <PlayerIndicator
72
+ model={model}
73
+ player={orientation}
74
+ icon={
75
+ onOrientationChange && (
76
+ <IconButton
77
+ classNames={mx('transition duration-200 ease-linear', orientation === 'white' && 'rotate-180')}
78
+ icon='ph--arrows-clockwise--regular'
79
+ iconOnly
80
+ label={t('flip-board.button')}
81
+ size={4}
82
+ onClick={() => onOrientationChange(orientation === 'white' ? 'black' : 'white')}
83
+ />
84
+ )
85
+ }
86
+ >
87
+ <PlayerSelector
88
+ value={model.object.players?.[orientation]}
89
+ onValueChange={(value) => {
90
+ Obj.change(model.object, (obj) => {
91
+ obj.players![orientation] = value;
92
+ });
93
+ }}
94
+ members={members}
95
+ />
96
+ </PlayerIndicator>
97
+ </div>
98
+ );
99
+ };
100
+
101
+ Info.displayName = INFO_NAME;
102
+
103
+ //
104
+ // History
105
+ //
106
+
107
+ type HistoryProps = ThemedClassName<{
108
+ model: ExtendedChessModel;
109
+ min?: number;
110
+ max?: number;
111
+ onSelect?: (index: number) => void;
112
+ }>;
113
+
114
+ const History = ({ classNames, model, min, max, onSelect }: HistoryProps) => {
115
+ const { t } = useTranslation(meta.id);
116
+ const moveIndex = useAtomValue(model.moveIndex);
117
+ const label = model.game.isGameOver()
118
+ ? model.game.isCheckmate()
119
+ ? t('game.checkmate.label')
120
+ : model.game.isStalemate()
121
+ ? t('game.stalemate.label')
122
+ : t('game.draw.label')
123
+ : model.game.isCheck()
124
+ ? t('game.check.label')
125
+ : undefined;
126
+
127
+ const history = model.game.history();
128
+ const moves = useMemo(
129
+ () =>
130
+ history.reduce(
131
+ (acc, move, index) => {
132
+ if (index % 2 === 0) {
133
+ acc.push([{ index, move }]);
134
+ } else {
135
+ acc[acc.length - 1].push({ index, move });
136
+ }
137
+ return acc;
138
+ },
139
+ [] as { index: number; move: string }[][],
140
+ ),
141
+ [history.length],
142
+ );
143
+
144
+ const scrollerRef = useRef<HTMLDivElement>(null);
145
+ useEffect(() => {
146
+ scrollerRef.current?.scrollTo({ top: scrollerRef.current.scrollHeight });
147
+ }, [history.length]);
148
+
149
+ useEffect(() => {
150
+ const div = scrollerRef.current?.querySelector(`[data-index="${moveIndex - 1}"]`);
151
+ scrollerRef.current?.classList.add('scrollbar-none');
152
+ div?.scrollIntoView({ behavior: 'smooth' });
153
+ scrollerRef.current?.classList.remove('scrollbar-none');
154
+ }, [moveIndex]);
155
+
156
+ return (
157
+ <div
158
+ ref={scrollerRef}
159
+ className={mx('overflow-y-auto', classNames)}
160
+ style={{
161
+ minHeight: min === undefined ? 'auto' : `${min * 24}px`,
162
+ maxHeight: max === undefined ? 'auto' : `${max * 24}px`,
163
+ }}
164
+ >
165
+ {moves.map(([a, b], index) => (
166
+ <div key={index} className='grid grid-cols-[3rem_1fr_1fr_1rem] gap-2 ps-4'>
167
+ <div className='content-center text-xs text-subdued'>{index + 1}</div>
168
+ {a && (
169
+ <div
170
+ data-index={a.index}
171
+ className={mx('ps-2 text-sm cursor-pointer', a.index === moveIndex - 1 && 'bg-primary-500')}
172
+ onClick={() => onSelect?.(a.index + 1)}
173
+ >
174
+ {a.move}
175
+ </div>
176
+ )}
177
+ {b && (
178
+ <div
179
+ data-index={b.index}
180
+ className={mx('ps-2 text-sm cursor-pointer', b.index === moveIndex - 1 && 'bg-primary-500')}
181
+ onClick={() => onSelect?.(b.index + 1)}
182
+ >
183
+ {b.move}
184
+ </div>
185
+ )}
186
+ </div>
187
+ ))}
188
+ {label && <div className='text-center'>{label}</div>}
189
+ </div>
190
+ );
191
+ };
192
+
193
+ //
194
+ // PlayerIndicator
195
+ //
196
+
197
+ type PlayerIndicatorProps = PropsWithChildren<{
198
+ model: ExtendedChessModel;
199
+ player: Player;
200
+ icon?: JSX.Element;
201
+ }>;
202
+
203
+ const PlayerIndicator = ({ children, model, player, icon }: PlayerIndicatorProps) => {
204
+ const turn = player === (model.game.turn() === 'w' ? 'white' : 'black');
205
+ return (
206
+ <div className='grid grid-cols-[2rem_1fr_2rem] gap-2 h-(--dx-rail-size) px-1 flex items-center overflow-hidden'>
207
+ <div className='place-items-center'>
208
+ <Icon
209
+ icon={turn ? 'ph--circle--fill' : 'ph--circle--thin'}
210
+ size={6}
211
+ classNames={mx(turn && (model.game.isCheckmate() ? 'text-error-500' : 'text-success-text'))}
212
+ />
213
+ </div>
214
+ <div className='truncate overflow-hidden items-center'>{children}</div>
215
+ {icon}
216
+ </div>
217
+ );
218
+ };
219
+
220
+ //
221
+ // PlayerSelector
222
+ //
223
+
224
+ type PlayerSelectorProps = {
225
+ value?: string;
226
+ onValueChange: (player: string) => void;
227
+ members: SpaceMember[];
228
+ };
229
+
230
+ const PlayerSelector = ({ value, onValueChange, members }: PlayerSelectorProps) => {
231
+ const { t } = useTranslation(meta.id);
232
+ return (
233
+ <Select.Root value={value} onValueChange={onValueChange}>
234
+ <Select.TriggerButton placeholder={t('select-player.button')} />
235
+ <Select.Portal>
236
+ <Select.Content>
237
+ <Select.Viewport>
238
+ {members.map((member) => {
239
+ const memberKey = member.identity.identityKey.toHex();
240
+ const displayName = member.identity?.profile?.displayName || generateName(memberKey);
241
+ return (
242
+ <Select.Option key={memberKey} value={memberKey}>
243
+ {displayName}
244
+ </Select.Option>
245
+ );
246
+ })}
247
+ </Select.Viewport>
248
+ <Select.Arrow />
249
+ </Select.Content>
250
+ </Select.Portal>
251
+ </Select.Root>
252
+ );
253
+ };
@@ -0,0 +1,5 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ export * from './Chessboard';
@@ -0,0 +1,18 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { type Registry } from '@effect-atom/atom-react';
6
+
7
+ import { ChessModel } from '@dxos/react-ui-gameboard';
8
+
9
+ import { type Chess } from '#types';
10
+
11
+ export class ExtendedChessModel extends ChessModel {
12
+ constructor(
13
+ registry: Registry.Registry,
14
+ readonly object: Chess.Game,
15
+ ) {
16
+ super(registry);
17
+ }
18
+ }
@@ -1,9 +1,5 @@
1
1
  //
2
- // Copyright 2023 DXOS.org
2
+ // Copyright 2026 DXOS.org
3
3
  //
4
4
 
5
- import { lazy } from 'react';
6
-
7
- export * from './ChessComponent';
8
-
9
- export const ChessContainer = lazy(() => import('./ChessContainer'));
5
+ export * from './Chessboard';