@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.
Files changed (67) hide show
  1. package/dist/lib/browser/index.mjs +733 -788
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node-esm/index.mjs +733 -788
  5. package/dist/lib/node-esm/index.mjs.map +4 -4
  6. package/dist/lib/node-esm/meta.json +1 -1
  7. package/dist/types/src/components/Chessboard/Chessboard.d.ts +20 -0
  8. package/dist/types/src/components/Chessboard/Chessboard.d.ts.map +1 -0
  9. package/dist/types/src/components/Chessboard/Chessboard.stories.d.ts +30 -0
  10. package/dist/types/src/components/Chessboard/Chessboard.stories.d.ts.map +1 -0
  11. package/dist/types/src/components/Chessboard/chess.d.ts +60 -0
  12. package/dist/types/src/components/Chessboard/chess.d.ts.map +1 -0
  13. package/dist/types/src/components/Chessboard/chess.test.d.ts +2 -0
  14. package/dist/types/src/components/Chessboard/chess.test.d.ts.map +1 -0
  15. package/dist/types/src/components/Chessboard/index.d.ts.map +1 -0
  16. package/dist/types/src/components/Gameboard/Gameboard.d.ts +38 -0
  17. package/dist/types/src/components/Gameboard/Gameboard.d.ts.map +1 -0
  18. package/dist/types/src/{Gameboard → components/Gameboard}/Piece.d.ts +3 -2
  19. package/dist/types/src/components/Gameboard/Piece.d.ts.map +1 -0
  20. package/dist/types/src/components/Gameboard/Square.d.ts.map +1 -0
  21. package/dist/types/src/components/Gameboard/index.d.ts +4 -0
  22. package/dist/types/src/components/Gameboard/index.d.ts.map +1 -0
  23. package/dist/types/src/{Gameboard → components/Gameboard}/types.d.ts +3 -2
  24. package/dist/types/src/components/Gameboard/types.d.ts.map +1 -0
  25. package/dist/types/src/components/Gameboard/util.d.ts.map +1 -0
  26. package/dist/types/src/components/index.d.ts +3 -0
  27. package/dist/types/src/components/index.d.ts.map +1 -0
  28. package/dist/types/src/index.d.ts +1 -2
  29. package/dist/types/src/index.d.ts.map +1 -1
  30. package/dist/types/tsconfig.tsbuildinfo +1 -1
  31. package/package.json +27 -22
  32. package/src/{Chessboard → components/Chessboard}/Chessboard.stories.tsx +33 -30
  33. package/src/{Chessboard → components/Chessboard}/Chessboard.tsx +67 -57
  34. package/src/components/Chessboard/chess.test.ts +19 -0
  35. package/src/components/Chessboard/chess.ts +325 -0
  36. package/src/components/Gameboard/Gameboard.tsx +145 -0
  37. package/src/{Gameboard → components/Gameboard}/Piece.tsx +25 -23
  38. package/src/{Gameboard → components/Gameboard}/Square.tsx +5 -5
  39. package/src/{Gameboard → components/Gameboard}/index.ts +0 -3
  40. package/src/{Gameboard → components/Gameboard}/types.ts +4 -2
  41. package/src/components/index.ts +6 -0
  42. package/src/index.ts +1 -2
  43. package/dist/types/src/Chessboard/Chessboard.d.ts +0 -15
  44. package/dist/types/src/Chessboard/Chessboard.d.ts.map +0 -1
  45. package/dist/types/src/Chessboard/Chessboard.stories.d.ts +0 -16
  46. package/dist/types/src/Chessboard/Chessboard.stories.d.ts.map +0 -1
  47. package/dist/types/src/Chessboard/chess.d.ts +0 -40
  48. package/dist/types/src/Chessboard/chess.d.ts.map +0 -1
  49. package/dist/types/src/Chessboard/index.d.ts.map +0 -1
  50. package/dist/types/src/Gameboard/Gameboard.d.ts +0 -23
  51. package/dist/types/src/Gameboard/Gameboard.d.ts.map +0 -1
  52. package/dist/types/src/Gameboard/Piece.d.ts.map +0 -1
  53. package/dist/types/src/Gameboard/Square.d.ts.map +0 -1
  54. package/dist/types/src/Gameboard/context.d.ts +0 -10
  55. package/dist/types/src/Gameboard/context.d.ts.map +0 -1
  56. package/dist/types/src/Gameboard/index.d.ts +0 -7
  57. package/dist/types/src/Gameboard/index.d.ts.map +0 -1
  58. package/dist/types/src/Gameboard/types.d.ts.map +0 -1
  59. package/dist/types/src/Gameboard/util.d.ts.map +0 -1
  60. package/src/Chessboard/chess.ts +0 -213
  61. package/src/Gameboard/Gameboard.tsx +0 -103
  62. package/src/Gameboard/context.ts +0 -22
  63. /package/dist/types/src/{Chessboard → components/Chessboard}/index.d.ts +0 -0
  64. /package/dist/types/src/{Gameboard → components/Gameboard}/Square.d.ts +0 -0
  65. /package/dist/types/src/{Gameboard → components/Gameboard}/util.d.ts +0 -0
  66. /package/src/{Chessboard → components/Chessboard}/index.ts +0 -0
  67. /package/src/{Gameboard → components/Gameboard}/util.ts +0 -0
@@ -5,22 +5,14 @@ var __export = (target, all) => {
5
5
  __defProp(target, name, { get: all[name], enumerable: true });
6
6
  };
7
7
 
8
- // src/Gameboard/context.ts
9
- import { createContext, useContext } from "react";
10
- import { raise } from "@dxos/debug";
11
- var GameboardContext = createContext(void 0);
12
- var useBoardContext = () => {
13
- return useContext(GameboardContext) ?? raise(new Error("Missing BoardContext"));
14
- };
15
-
16
- // src/Gameboard/types.ts
8
+ // src/components/Gameboard/types.ts
17
9
  var locationToString = (location) => location.join("-");
18
10
  var stringToLocation = (str) => str.split("-").map(Number);
19
11
  var isPiece = (piece) => piece != null && typeof piece === "object" && "id" in piece && "type" in piece && "location" in piece && isLocation(piece.location);
20
12
  var isLocation = (token) => Array.isArray(token) && token.length === 2 && token.every((val) => typeof val === "number");
21
13
  var isEqualLocation = (l1, l2) => l1[0] === l2[0] && l1[1] === l2[1];
22
14
 
23
- // src/Gameboard/util.ts
15
+ // src/components/Gameboard/util.ts
24
16
  var getRelativeBounds = (container, element) => {
25
17
  const containerRect = container.getBoundingClientRect();
26
18
  const elementRect = element.getBoundingClientRect();
@@ -32,70 +24,281 @@ var getRelativeBounds = (container, element) => {
32
24
  };
33
25
  };
34
26
 
35
- // src/Gameboard/Gameboard.tsx
36
- import { useSignals as _useSignals } from "@preact-signals/safe-react/tracking";
27
+ // src/components/Gameboard/Gameboard.tsx
37
28
  import { monitorForElements } from "@atlaskit/pragmatic-drag-and-drop/element/adapter";
38
- import React, { forwardRef, useCallback, useEffect, useState } from "react";
29
+ import { createContext } from "@radix-ui/react-context";
30
+ import React3, { forwardRef, useCallback, useEffect as useEffect3, useState as useState3 } from "react";
31
+ import { log as log3 } from "@dxos/log";
32
+ import { mx as mx3 } from "@dxos/ui-theme";
33
+
34
+ // src/components/Gameboard/Piece.tsx
35
+ import { draggable } from "@atlaskit/pragmatic-drag-and-drop/element/adapter";
36
+ import { centerUnderPointer } from "@atlaskit/pragmatic-drag-and-drop/element/center-under-pointer";
37
+ import { setCustomNativeDragPreview } from "@atlaskit/pragmatic-drag-and-drop/element/set-custom-native-drag-preview";
38
+ import React, { memo, useEffect, useRef, useState } from "react";
39
+ import { createPortal } from "react-dom";
40
+ import { invariant } from "@dxos/invariant";
39
41
  import { log } from "@dxos/log";
40
- import { mx } from "@dxos/react-ui-theme";
41
- var __dxlog_file = "/__w/dxos/dxos/packages/ui/react-ui-gameboard/src/Gameboard/Gameboard.tsx";
42
- var GameboardRoot = ({ children, model, onDrop }) => {
43
- var _effect = _useSignals();
44
- try {
45
- const [dragging, setDragging] = useState(false);
46
- const [promoting, setPromoting] = useState();
47
- const onPromotion = useCallback((move) => {
48
- log("onPromotion", {
49
- move
50
- }, {
51
- F: __dxlog_file,
52
- L: 29,
53
- S: void 0,
54
- C: (f, a) => f(...a)
55
- });
56
- setPromoting(void 0);
57
- onDrop?.(move);
58
- }, []);
59
- useEffect(() => {
60
- if (!model) {
42
+ import { useDynamicRef } from "@dxos/react-ui";
43
+ import { mx } from "@dxos/ui-theme";
44
+ var __dxlog_file = "/__w/dxos/dxos/packages/ui/react-ui-gameboard/src/components/Gameboard/Piece.tsx";
45
+ var Piece = /* @__PURE__ */ memo(({ classNames, Component, piece, bounds, label, onClick }) => {
46
+ const { model, dragging: isDragging, promoting } = useGameboardContext(Piece.displayName);
47
+ const promotingRef = useDynamicRef(promoting);
48
+ const [dragging, setDragging] = useState(false);
49
+ const [preview, setPreview] = useState();
50
+ const [current, setCurrent] = useState({});
51
+ const ref = useRef(null);
52
+ useEffect(() => {
53
+ if (!model) {
54
+ return;
55
+ }
56
+ const el = ref.current;
57
+ invariant(el, void 0, {
58
+ F: __dxlog_file,
59
+ L: 45,
60
+ S: void 0,
61
+ A: [
62
+ "el",
63
+ ""
64
+ ]
65
+ });
66
+ return draggable({
67
+ element: el,
68
+ getInitialData: () => ({
69
+ piece
70
+ }),
71
+ onGenerateDragPreview: ({ nativeSetDragImage, source }) => {
72
+ log("onGenerateDragPreview", {
73
+ source: source.data
74
+ }, {
75
+ F: __dxlog_file,
76
+ L: 51,
77
+ S: void 0,
78
+ C: (f, a) => f(...a)
79
+ });
80
+ setCustomNativeDragPreview({
81
+ getOffset: centerUnderPointer,
82
+ render: ({ container }) => {
83
+ setPreview(container);
84
+ const { width, height } = el.getBoundingClientRect();
85
+ container.style.width = width + "px";
86
+ container.style.height = height + "px";
87
+ return () => {
88
+ setPreview(void 0);
89
+ };
90
+ },
91
+ nativeSetDragImage
92
+ });
93
+ },
94
+ canDrag: () => !promotingRef.current && !model.readonly && model.turn === piece.side,
95
+ onDragStart: () => setDragging(true),
96
+ onDrop: ({ location: { current: current2 } }) => {
97
+ try {
98
+ const location = current2.dropTargets[0]?.data.location;
99
+ if (isLocation(location)) {
100
+ setCurrent((current3) => ({
101
+ ...current3,
102
+ location
103
+ }));
104
+ }
105
+ } catch {
106
+ }
107
+ setDragging(false);
108
+ }
109
+ });
110
+ }, [
111
+ model,
112
+ piece
113
+ ]);
114
+ useEffect(() => {
115
+ requestAnimationFrame(() => {
116
+ if (!ref.current || !bounds) {
61
117
  return;
62
118
  }
63
- return monitorForElements({
64
- onDragStart: ({ source }) => {
65
- log("onDragStart", {
66
- source
67
- }, {
68
- F: __dxlog_file,
69
- L: 42,
70
- S: void 0,
71
- C: (f, a) => f(...a)
72
- });
73
- setDragging(true);
74
- },
75
- onDrop: ({ source, location }) => {
76
- log("onDrop", {
77
- source,
78
- location
79
- }, {
80
- F: __dxlog_file,
81
- L: 46,
82
- S: void 0,
83
- C: (f, a) => f(...a)
84
- });
85
- const target = location.current.dropTargets[0];
86
- if (!target) {
87
- return;
88
- }
89
- const targetLocation = target.data.location;
90
- const piece = source.data.piece;
91
- if (!isLocation(targetLocation) || !isPiece(piece)) {
92
- return;
93
- }
94
- const move = {
95
- from: piece.location,
96
- to: targetLocation,
97
- piece: piece.type
98
- };
119
+ if (!current.location || !isEqualLocation(current.location, piece.location)) {
120
+ ref.current.style.transition = "top 250ms ease-out, left 250ms ease-out";
121
+ ref.current.style.top = bounds.top + "px";
122
+ ref.current.style.left = bounds.left + "px";
123
+ setCurrent({
124
+ location: piece.location,
125
+ bounds
126
+ });
127
+ } else if (current.bounds !== bounds) {
128
+ ref.current.style.transition = "none";
129
+ ref.current.style.top = bounds.top + "px";
130
+ ref.current.style.left = bounds.left + "px";
131
+ setCurrent({
132
+ location: piece.location,
133
+ bounds
134
+ });
135
+ }
136
+ });
137
+ }, [
138
+ current,
139
+ piece.location,
140
+ bounds
141
+ ]);
142
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("div", {
143
+ ref,
144
+ style: {
145
+ width: bounds?.width,
146
+ height: bounds?.height
147
+ },
148
+ className: mx("absolute", classNames, dragging && "opacity-20", isDragging && "pointer-events-none"),
149
+ onClick
150
+ }, /* @__PURE__ */ React.createElement(Component, {
151
+ className: "grow"
152
+ }), label && /* @__PURE__ */ React.createElement("div", {
153
+ className: "absolute inset-1 text-xs text-black"
154
+ }, label)), preview && /* @__PURE__ */ createPortal(/* @__PURE__ */ React.createElement("div", {
155
+ className: mx(classNames)
156
+ }, /* @__PURE__ */ React.createElement(Component, {
157
+ className: "grow"
158
+ })), preview));
159
+ });
160
+ Piece.displayName = "Piece";
161
+
162
+ // src/components/Gameboard/Square.tsx
163
+ import { dropTargetForElements } from "@atlaskit/pragmatic-drag-and-drop/element/adapter";
164
+ import React2, { memo as memo2, useEffect as useEffect2, useRef as useRef2, useState as useState2 } from "react";
165
+ import { invariant as invariant2 } from "@dxos/invariant";
166
+ import { log as log2 } from "@dxos/log";
167
+ import { mx as mx2 } from "@dxos/ui-theme";
168
+ var __dxlog_file2 = "/__w/dxos/dxos/packages/ui/react-ui-gameboard/src/components/Gameboard/Square.tsx";
169
+ var Square = /* @__PURE__ */ memo2(({ location, bounds, label, classNames }) => {
170
+ const ref = useRef2(null);
171
+ const [state, setState] = useState2("idle");
172
+ const { model } = useGameboardContext(Square.displayName);
173
+ useEffect2(() => {
174
+ const el = ref.current;
175
+ invariant2(el, void 0, {
176
+ F: __dxlog_file2,
177
+ L: 32,
178
+ S: void 0,
179
+ A: [
180
+ "el",
181
+ ""
182
+ ]
183
+ });
184
+ return dropTargetForElements({
185
+ element: el,
186
+ getData: () => ({
187
+ location
188
+ }),
189
+ canDrop: ({ source }) => {
190
+ log2("canDrop", {
191
+ source: source.data
192
+ }, {
193
+ F: __dxlog_file2,
194
+ L: 38,
195
+ S: void 0,
196
+ C: (f, a) => f(...a)
197
+ });
198
+ return true;
199
+ },
200
+ onDragEnter: ({ source }) => {
201
+ log2("onDragEnter", {
202
+ source: source.data
203
+ }, {
204
+ F: __dxlog_file2,
205
+ L: 42,
206
+ S: void 0,
207
+ C: (f, a) => f(...a)
208
+ });
209
+ const piece = source.data.piece;
210
+ if (!isPiece(piece)) {
211
+ return;
212
+ }
213
+ if (model?.isValidMove({
214
+ from: piece.location,
215
+ to: location,
216
+ piece: piece.type
217
+ })) {
218
+ setState("validMove");
219
+ } else {
220
+ setState("invalidMove");
221
+ }
222
+ },
223
+ onDragLeave: () => setState("idle"),
224
+ onDrop: () => setState("idle")
225
+ });
226
+ }, [
227
+ model,
228
+ location
229
+ ]);
230
+ return /* @__PURE__ */ React2.createElement("div", {
231
+ ref,
232
+ style: bounds,
233
+ className: mx2("absolute flex justify-center items-center border-2 box-border select-none", classNames, state === "validMove" ? "border-neutral-800" : "border-transparent")
234
+ }, label && /* @__PURE__ */ React2.createElement("div", {
235
+ className: mx2("absolute bottom-1 left-1 text-xs text-neutral-500")
236
+ }, label));
237
+ });
238
+ Square.displayName = "Square";
239
+
240
+ // src/components/Gameboard/Gameboard.tsx
241
+ var __dxlog_file3 = "/__w/dxos/dxos/packages/ui/react-ui-gameboard/src/components/Gameboard/Gameboard.tsx";
242
+ var [GameboardContextProvider, useRadixGameboardContext] = createContext("Gameboard");
243
+ var useGameboardContext = (consumerName) => {
244
+ return useRadixGameboardContext(consumerName);
245
+ };
246
+ var GameboardRoot = ({ children, model, moveNumber, onDrop }) => {
247
+ const [dragging, setDragging] = useState3(false);
248
+ const [promoting, setPromoting] = useState3();
249
+ const handlePromotion = useCallback((move) => {
250
+ log3("onPromotion", {
251
+ move
252
+ }, {
253
+ F: __dxlog_file3,
254
+ L: 53,
255
+ S: void 0,
256
+ C: (f, a) => f(...a)
257
+ });
258
+ setPromoting(void 0);
259
+ onDrop?.(move);
260
+ }, []);
261
+ useEffect3(() => {
262
+ if (!model) {
263
+ return;
264
+ }
265
+ return monitorForElements({
266
+ onDragStart: ({ source }) => {
267
+ log3("onDragStart", {
268
+ source
269
+ }, {
270
+ F: __dxlog_file3,
271
+ L: 66,
272
+ S: void 0,
273
+ C: (f, a) => f(...a)
274
+ });
275
+ setDragging(true);
276
+ },
277
+ onDrop: ({ source, location }) => {
278
+ log3("onDrop", {
279
+ source,
280
+ location
281
+ }, {
282
+ F: __dxlog_file3,
283
+ L: 70,
284
+ S: void 0,
285
+ C: (f, a) => f(...a)
286
+ });
287
+ const target = location.current.dropTargets[0];
288
+ if (!target) {
289
+ return;
290
+ }
291
+ const targetLocation = target.data.location;
292
+ const piece = source.data.piece;
293
+ if (!isLocation(targetLocation) || !isPiece(piece)) {
294
+ return;
295
+ }
296
+ const move = {
297
+ from: piece.location,
298
+ to: targetLocation,
299
+ piece: piece.type
300
+ };
301
+ if (model.isValidMove(move)) {
99
302
  if (model.canPromote?.(move)) {
100
303
  setPromoting({
101
304
  ...piece,
@@ -104,277 +307,42 @@ var GameboardRoot = ({ children, model, onDrop }) => {
104
307
  } else {
105
308
  onDrop?.(move);
106
309
  }
107
- setDragging(false);
108
310
  }
109
- });
110
- }, [
111
- model
112
- ]);
113
- return /* @__PURE__ */ React.createElement(GameboardContext.Provider, {
114
- value: {
115
- model,
116
- dragging,
117
- promoting,
118
- onPromotion
311
+ setDragging(false);
119
312
  }
120
- }, children);
121
- } finally {
122
- _effect.f();
123
- }
313
+ });
314
+ }, [
315
+ model
316
+ ]);
317
+ return /* @__PURE__ */ React3.createElement(GameboardContextProvider, {
318
+ model,
319
+ dragging,
320
+ promoting,
321
+ onPromotion: handlePromotion
322
+ }, children);
124
323
  };
125
324
  GameboardRoot.displayName = "Gameboard.Root";
126
- var GameboardContent = /* @__PURE__ */ forwardRef(({ children, classNames, style }, forwardedRef) => {
127
- var _effect = _useSignals();
128
- try {
129
- return /* @__PURE__ */ React.createElement("div", {
130
- ref: forwardedRef,
131
- style,
132
- className: "flex w-full h-full justify-center overflow-hidden"
133
- }, /* @__PURE__ */ React.createElement("div", {
134
- className: mx("max-w-full max-h-full content-center aspect-square", classNames)
135
- }, children));
136
- } finally {
137
- _effect.f();
138
- }
325
+ var GameboardContent = /* @__PURE__ */ forwardRef(({ children, classNames, grow, contain }, forwardedRef) => {
326
+ return /* @__PURE__ */ React3.createElement("div", {
327
+ role: "none",
328
+ className: mx3(grow && "grid is-full bs-full size-container place-content-center", classNames),
329
+ ref: forwardedRef
330
+ }, contain ? /* @__PURE__ */ React3.createElement("div", {
331
+ className: "is-[min(100cqw,100cqh)] bs-[min(100cqw,100cqh)]"
332
+ }, children) : children);
139
333
  });
334
+ GameboardContent.displayName = "Gameboard.Content";
140
335
  var Gameboard = {
141
336
  Root: GameboardRoot,
142
- Content: GameboardContent
337
+ Content: GameboardContent,
338
+ Piece,
339
+ Square
143
340
  };
144
341
 
145
- // src/Gameboard/Square.tsx
146
- import { useSignals as _useSignals2 } from "@preact-signals/safe-react/tracking";
147
- import { dropTargetForElements } from "@atlaskit/pragmatic-drag-and-drop/element/adapter";
148
- import React2, { useRef, useState as useState2, useEffect as useEffect2, memo } from "react";
149
- import { invariant } from "@dxos/invariant";
150
- import { log as log2 } from "@dxos/log";
151
- import { mx as mx2 } from "@dxos/react-ui-theme";
152
- var __dxlog_file2 = "/__w/dxos/dxos/packages/ui/react-ui-gameboard/src/Gameboard/Square.tsx";
153
- var Square = /* @__PURE__ */ memo(({ location, bounds, label, classNames }) => {
154
- var _effect = _useSignals2();
155
- try {
156
- const ref = useRef(null);
157
- const [state, setState] = useState2("idle");
158
- const { model } = useBoardContext();
159
- useEffect2(() => {
160
- const el = ref.current;
161
- invariant(el, void 0, {
162
- F: __dxlog_file2,
163
- L: 32,
164
- S: void 0,
165
- A: [
166
- "el",
167
- ""
168
- ]
169
- });
170
- return dropTargetForElements({
171
- element: el,
172
- getData: () => ({
173
- location
174
- }),
175
- canDrop: ({ source }) => {
176
- log2("canDrop", {
177
- source: source.data
178
- }, {
179
- F: __dxlog_file2,
180
- L: 38,
181
- S: void 0,
182
- C: (f, a) => f(...a)
183
- });
184
- return true;
185
- },
186
- onDragEnter: ({ source }) => {
187
- log2("onDragEnter", {
188
- source: source.data
189
- }, {
190
- F: __dxlog_file2,
191
- L: 42,
192
- S: void 0,
193
- C: (f, a) => f(...a)
194
- });
195
- const piece = source.data.piece;
196
- if (!isPiece(piece)) {
197
- return;
198
- }
199
- if (model?.isValidMove({
200
- from: piece.location,
201
- to: location,
202
- piece: piece.type
203
- })) {
204
- setState("validMove");
205
- } else {
206
- setState("invalidMove");
207
- }
208
- },
209
- onDragLeave: () => setState("idle"),
210
- onDrop: () => setState("idle")
211
- });
212
- }, [
213
- model,
214
- location
215
- ]);
216
- return /* @__PURE__ */ React2.createElement("div", {
217
- ref,
218
- style: bounds,
219
- className: mx2("absolute flex justify-center items-center border-2 box-border select-none", classNames, state === "validMove" ? "border-neutral-800" : "border-transparent")
220
- }, label && /* @__PURE__ */ React2.createElement("div", {
221
- className: mx2("absolute bottom-1 left-1 text-xs text-neutral-500")
222
- }, label));
223
- } finally {
224
- _effect.f();
225
- }
226
- });
227
- Square.displayName = "Square";
228
-
229
- // src/Gameboard/Piece.tsx
230
- import { useSignals as _useSignals3 } from "@preact-signals/safe-react/tracking";
231
- import { draggable } from "@atlaskit/pragmatic-drag-and-drop/element/adapter";
232
- import { centerUnderPointer } from "@atlaskit/pragmatic-drag-and-drop/element/center-under-pointer";
233
- import { setCustomNativeDragPreview } from "@atlaskit/pragmatic-drag-and-drop/element/set-custom-native-drag-preview";
234
- import React3, { useState as useState3, useRef as useRef2, useEffect as useEffect3, memo as memo2 } from "react";
235
- import { createPortal } from "react-dom";
236
- import { invariant as invariant2 } from "@dxos/invariant";
237
- import { log as log3 } from "@dxos/log";
238
- import { useDynamicRef, useTrackProps } from "@dxos/react-ui";
239
- import { mx as mx3 } from "@dxos/react-ui-theme";
240
- var __dxlog_file3 = "/__w/dxos/dxos/packages/ui/react-ui-gameboard/src/Gameboard/Piece.tsx";
241
- var Piece = /* @__PURE__ */ memo2(({ classNames, piece, orientation, bounds, label, Component }) => {
242
- var _effect = _useSignals3();
243
- try {
244
- useTrackProps({
245
- classNames,
246
- piece,
247
- orientation,
248
- bounds,
249
- label,
250
- Component
251
- }, Piece.displayName, false);
252
- const { model } = useBoardContext();
253
- const { dragging: isDragging, promoting } = useBoardContext();
254
- const promotingRef = useDynamicRef(promoting);
255
- const [dragging, setDragging] = useState3(false);
256
- const [preview, setPreview] = useState3();
257
- const [current, setCurrent] = useState3({});
258
- const ref = useRef2(null);
259
- useEffect3(() => {
260
- const el = ref.current;
261
- invariant2(el, void 0, {
262
- F: __dxlog_file3,
263
- L: 44,
264
- S: void 0,
265
- A: [
266
- "el",
267
- ""
268
- ]
269
- });
270
- return draggable({
271
- element: el,
272
- getInitialData: () => ({
273
- piece
274
- }),
275
- onGenerateDragPreview: ({ nativeSetDragImage, location, source }) => {
276
- log3("onGenerateDragPreview", {
277
- source: source.data
278
- }, {
279
- F: __dxlog_file3,
280
- L: 50,
281
- S: void 0,
282
- C: (f, a) => f(...a)
283
- });
284
- setCustomNativeDragPreview({
285
- getOffset: centerUnderPointer,
286
- // getOffset: preserveOffsetOnSource({
287
- // element: source.element,
288
- // input: location.current.input,
289
- // }),
290
- render: ({ container }) => {
291
- setPreview(container);
292
- const { width, height } = el.getBoundingClientRect();
293
- container.style.width = width + "px";
294
- container.style.height = height + "px";
295
- return () => {
296
- setPreview(void 0);
297
- };
298
- },
299
- nativeSetDragImage
300
- });
301
- },
302
- canDrag: () => !promotingRef.current && model?.turn === piece.side,
303
- onDragStart: () => setDragging(true),
304
- onDrop: ({ location: { current: current2 } }) => {
305
- const location = current2.dropTargets[0].data.location;
306
- if (isLocation(location)) {
307
- setCurrent((current3) => ({
308
- ...current3,
309
- location
310
- }));
311
- }
312
- setDragging(false);
313
- }
314
- });
315
- }, [
316
- model,
317
- piece
318
- ]);
319
- useEffect3(() => {
320
- requestAnimationFrame(() => {
321
- if (!ref.current || !bounds) {
322
- return;
323
- }
324
- if (!current.location || !isEqualLocation(current.location, piece.location)) {
325
- ref.current.style.transition = "top 400ms ease-out, left 400ms ease-out";
326
- ref.current.style.top = bounds.top + "px";
327
- ref.current.style.left = bounds.left + "px";
328
- setCurrent({
329
- location: piece.location,
330
- bounds
331
- });
332
- } else if (current.bounds !== bounds) {
333
- ref.current.style.transition = "none";
334
- ref.current.style.top = bounds.top + "px";
335
- ref.current.style.left = bounds.left + "px";
336
- setCurrent({
337
- location: piece.location,
338
- bounds
339
- });
340
- }
341
- });
342
- }, [
343
- current,
344
- piece.location,
345
- bounds
346
- ]);
347
- return /* @__PURE__ */ React3.createElement(React3.Fragment, null, /* @__PURE__ */ React3.createElement("div", {
348
- ref,
349
- style: {
350
- width: bounds?.width,
351
- height: bounds?.height
352
- },
353
- className: mx3(
354
- "absolute",
355
- classNames,
356
- // orientation === 'black' && '_rotate-180',
357
- dragging && "opacity-20",
358
- isDragging && "pointer-events-none"
359
- )
360
- }, /* @__PURE__ */ React3.createElement(Component, {
361
- className: "grow"
362
- }), label && /* @__PURE__ */ React3.createElement("div", {
363
- className: "absolute inset-1 text-xs text-black"
364
- }, label)), preview && /* @__PURE__ */ createPortal(/* @__PURE__ */ React3.createElement("div", {
365
- className: mx3(classNames)
366
- }, /* @__PURE__ */ React3.createElement(Component, {
367
- className: "grow"
368
- })), preview));
369
- } finally {
370
- _effect.f();
371
- }
372
- });
373
- Piece.displayName = "Piece";
374
-
375
- // src/Chessboard/chess.ts
376
- import { signal } from "@preact/signals-core";
377
- import { Chess, validateFen } from "chess.js";
342
+ // src/components/Chessboard/chess.ts
343
+ import { Atom } from "@effect-atom/atom-react";
344
+ import { Chess as ChessJS } from "chess.js";
345
+ import { invariant as invariant3 } from "@dxos/invariant";
378
346
  import { log as log4 } from "@dxos/log";
379
347
 
380
348
  // src/gen/pieces/chess/alpha/index.ts
@@ -395,280 +363,184 @@ __export(alpha_exports, {
395
363
  });
396
364
 
397
365
  // src/gen/pieces/chess/alpha/bB.tsx
398
- import { useSignals as _useSignals4 } from "@preact-signals/safe-react/tracking";
399
366
  import * as React4 from "react";
400
- var SvgBB = (props) => {
401
- var _effect = _useSignals4();
402
- try {
403
- return /* @__PURE__ */ React4.createElement("svg", {
404
- xmlns: "http://www.w3.org/2000/svg",
405
- viewBox: "0 0 2048 2048",
406
- ...props
407
- }, /* @__PURE__ */ React4.createElement("path", {
408
- fill: "#f9f9f9",
409
- d: "M732 1290 628 993l386-417 421 375-132 380 49 152-658-9z"
410
- }), /* @__PURE__ */ React4.createElement("path", {
411
- fill: "#101010",
412
- d: "M768 1365q-5 39-26 82h564q-18-36-26-82zm495-73 46-73q-142-49-285-47-144-2-285 47l46 73q118-40 239-38 120-2 239 38m-432 227H624q67-116 72-229-114-119-162-223t-6-224q33-96 118-189t312-247q-17-11-46-36t-29-79q0-58 41-96t100-38q58 0 100 38t41 96q0 54-29 79t-46 36q226 153 311 247t119 189q42 119-6 224t-162 223q4 113 72 229h-207l10 16q33 53 70 60t89 7h250q76 0 142 62t65 179h-495q-123 0-223-84t-101-199q0 114-101 199t-223 84H205q0-117 65-179t142-62h250q51 0 88-7t71-60zm146-701h-95v89h95v165h94V907h95v-89h-95V714h-94z"
413
- }));
414
- } finally {
415
- _effect.f();
416
- }
417
- };
367
+ var SvgBB = (props) => /* @__PURE__ */ React4.createElement("svg", {
368
+ xmlns: "http://www.w3.org/2000/svg",
369
+ viewBox: "0 0 2048 2048",
370
+ ...props
371
+ }, /* @__PURE__ */ React4.createElement("path", {
372
+ fill: "#f9f9f9",
373
+ d: "M732 1290 628 993l386-417 421 375-132 380 49 152-658-9z"
374
+ }), /* @__PURE__ */ React4.createElement("path", {
375
+ fill: "#101010",
376
+ d: "M768 1365q-5 39-26 82h564q-18-36-26-82zm495-73 46-73q-142-49-285-47-144-2-285 47l46 73q118-40 239-38 120-2 239 38m-432 227H624q67-116 72-229-114-119-162-223t-6-224q33-96 118-189t312-247q-17-11-46-36t-29-79q0-58 41-96t100-38q58 0 100 38t41 96q0 54-29 79t-46 36q226 153 311 247t119 189q42 119-6 224t-162 223q4 113 72 229h-207l10 16q33 53 70 60t89 7h250q76 0 142 62t65 179h-495q-123 0-223-84t-101-199q0 114-101 199t-223 84H205q0-117 65-179t142-62h250q51 0 88-7t71-60zm146-701h-95v89h95v165h94V907h95v-89h-95V714h-94z"
377
+ }));
418
378
  var bB_default = SvgBB;
419
379
 
420
380
  // src/gen/pieces/chess/alpha/bK.tsx
421
- import { useSignals as _useSignals5 } from "@preact-signals/safe-react/tracking";
422
381
  import * as React5 from "react";
423
- var SvgBK = (props) => {
424
- var _effect = _useSignals5();
425
- try {
426
- return /* @__PURE__ */ React5.createElement("svg", {
427
- xmlns: "http://www.w3.org/2000/svg",
428
- viewBox: "0 0 2048 2048",
429
- ...props
430
- }, /* @__PURE__ */ React5.createElement("path", {
431
- fill: "#f9f9f9",
432
- d: "m553 1485-55 320 1048 5-48-335s324-313 330-467c7-153-35-331-241-406-183-67-372 121-372 121l-182-161-190 160S658 586 587 592a373 373 0 0 0-362 333c10 331 328 560 328 560"
433
- }), /* @__PURE__ */ React5.createElement("path", {
434
- fill: "#101010",
435
- d: "M1024 1769h489l-12-73H547l-12 73zm0-921q-25-60-62-111 31-48 62-65 30 17 62 65-38 51-62 111m-97 454q-154 11-303 58-123-108-200-213t-77-202q0-89 74-159t148-70q67 0 135 63t102 130q30 54 75 175t46 218m-350 217-26 156 145-84zm447-907q-47 0-136 121-31-36-50-55 93-140 186-140 92 0 186 140-20 19-50 55-90-121-136-121m0 775q-1-126-42-267t-84-227l-14-27-12-23q-28-43-48-69-51-63-120-105t-134-42q-103 0-208 93T257 949q0 120 99 255t249 259q201-74 419-76m0 456H448l61-365q-325-280-326-535-1-159 125-274t267-116q78 0 159 47t142 119q61 74 99 165t49 150q12-60 49-150t99-165q61-72 142-119t159-47q140 0 266 116t126 274q-2 255-326 535l61 365zm97-541q0-97 45-218t76-175q34-68 102-130t135-63q74 0 148 70t74 159q0 96-77 202t-200 213q-150-47-303-58m350 217-119 72 145 84zm-447-132q217 2 419 76 150-125 249-259t99-255q0-136-105-229t-208-93q-66 0-135 42t-119 105q-21 26-48 69l-12 23-14 27q-44 85-85 227t-41 267m-139 159 139 86 139-84-139-86zm92-1248v-95h94v95h107v95h-107v153q-48-16-94 0V393H870v-95z"
436
- }));
437
- } finally {
438
- _effect.f();
439
- }
440
- };
382
+ var SvgBK = (props) => /* @__PURE__ */ React5.createElement("svg", {
383
+ xmlns: "http://www.w3.org/2000/svg",
384
+ viewBox: "0 0 2048 2048",
385
+ ...props
386
+ }, /* @__PURE__ */ React5.createElement("path", {
387
+ fill: "#f9f9f9",
388
+ d: "m553 1485-55 320 1048 5-48-335s324-313 330-467c7-153-35-331-241-406-183-67-372 121-372 121l-182-161-190 160S658 586 587 592a373 373 0 0 0-362 333c10 331 328 560 328 560"
389
+ }), /* @__PURE__ */ React5.createElement("path", {
390
+ fill: "#101010",
391
+ d: "M1024 1769h489l-12-73H547l-12 73zm0-921q-25-60-62-111 31-48 62-65 30 17 62 65-38 51-62 111m-97 454q-154 11-303 58-123-108-200-213t-77-202q0-89 74-159t148-70q67 0 135 63t102 130q30 54 75 175t46 218m-350 217-26 156 145-84zm447-907q-47 0-136 121-31-36-50-55 93-140 186-140 92 0 186 140-20 19-50 55-90-121-136-121m0 775q-1-126-42-267t-84-227l-14-27-12-23q-28-43-48-69-51-63-120-105t-134-42q-103 0-208 93T257 949q0 120 99 255t249 259q201-74 419-76m0 456H448l61-365q-325-280-326-535-1-159 125-274t267-116q78 0 159 47t142 119q61 74 99 165t49 150q12-60 49-150t99-165q61-72 142-119t159-47q140 0 266 116t126 274q-2 255-326 535l61 365zm97-541q0-97 45-218t76-175q34-68 102-130t135-63q74 0 148 70t74 159q0 96-77 202t-200 213q-150-47-303-58m350 217-119 72 145 84zm-447-132q217 2 419 76 150-125 249-259t99-255q0-136-105-229t-208-93q-66 0-135 42t-119 105q-21 26-48 69l-12 23-14 27q-44 85-85 227t-41 267m-139 159 139 86 139-84-139-86zm92-1248v-95h94v95h107v95h-107v153q-48-16-94 0V393H870v-95z"
392
+ }));
441
393
  var bK_default = SvgBK;
442
394
 
443
395
  // src/gen/pieces/chess/alpha/bN.tsx
444
- import { useSignals as _useSignals6 } from "@preact-signals/safe-react/tracking";
445
396
  import * as React6 from "react";
446
- var SvgBN = (props) => {
447
- var _effect = _useSignals6();
448
- try {
449
- return /* @__PURE__ */ React6.createElement("svg", {
450
- xmlns: "http://www.w3.org/2000/svg",
451
- viewBox: "0 0 2048 2048",
452
- ...props
453
- }, /* @__PURE__ */ React6.createElement("path", {
454
- fill: "#f9f9f9",
455
- d: "M1658 1806c-408-301-71-920-452-1343l60-16s296 167 320 257c182 468 41 691 135 1077zM972 523l120 176-312-3zM498 981l-93-41 42-93 127 28z"
456
- }), /* @__PURE__ */ React6.createElement("path", {
457
- fill: "#101010",
458
- d: "m502 868-52 1-26 64 69 21 46-55zm536-187q34 1-16-68t-80-42L826 680zm-338-98q6-39 116-107t220-144l115-154 96 217q342 172 433 418t47 603q-18 128 5 236t57 190l-1242 1q-9-178 39-301t183-238q50-11 83-39t53-59l63-1 138-29 139-97 66-207q0-17-8-34t-12-37q-62 228-161 289t-191 58q-236-42-292 60l-56 102-217-121 115-82-51-50-122 86-12-297zm981 1192q-102-130-85-308t27-363-50-351-316-276q220 164 253 342t16 351-12 329 167 276"
459
- }));
460
- } finally {
461
- _effect.f();
462
- }
463
- };
397
+ var SvgBN = (props) => /* @__PURE__ */ React6.createElement("svg", {
398
+ xmlns: "http://www.w3.org/2000/svg",
399
+ viewBox: "0 0 2048 2048",
400
+ ...props
401
+ }, /* @__PURE__ */ React6.createElement("path", {
402
+ fill: "#f9f9f9",
403
+ d: "M1658 1806c-408-301-71-920-452-1343l60-16s296 167 320 257c182 468 41 691 135 1077zM972 523l120 176-312-3zM498 981l-93-41 42-93 127 28z"
404
+ }), /* @__PURE__ */ React6.createElement("path", {
405
+ fill: "#101010",
406
+ d: "m502 868-52 1-26 64 69 21 46-55zm536-187q34 1-16-68t-80-42L826 680zm-338-98q6-39 116-107t220-144l115-154 96 217q342 172 433 418t47 603q-18 128 5 236t57 190l-1242 1q-9-178 39-301t183-238q50-11 83-39t53-59l63-1 138-29 139-97 66-207q0-17-8-34t-12-37q-62 228-161 289t-191 58q-236-42-292 60l-56 102-217-121 115-82-51-50-122 86-12-297zm981 1192q-102-130-85-308t27-363-50-351-316-276q220 164 253 342t16 351-12 329 167 276"
407
+ }));
464
408
  var bN_default = SvgBN;
465
409
 
466
410
  // src/gen/pieces/chess/alpha/bP.tsx
467
- import { useSignals as _useSignals7 } from "@preact-signals/safe-react/tracking";
468
411
  import * as React7 from "react";
469
- var SvgBP = (props) => {
470
- var _effect = _useSignals7();
471
- try {
472
- return /* @__PURE__ */ React7.createElement("svg", {
473
- xmlns: "http://www.w3.org/2000/svg",
474
- viewBox: "0 0 2048 2048",
475
- ...props
476
- }, /* @__PURE__ */ React7.createElement("path", {
477
- fill: "#101010",
478
- d: "M1024 1843H446v-74q-4-80 42-137t125-108q117-91 172-217t78-268H576l284-239q-86-74-86-188 0-103 73-177t177-74q103 0 177 74t73 177q0 114-86 188l284 239h-287q23 141 78 268t172 217q79 51 125 108t42 137v74z"
479
- }));
480
- } finally {
481
- _effect.f();
482
- }
483
- };
412
+ var SvgBP = (props) => /* @__PURE__ */ React7.createElement("svg", {
413
+ xmlns: "http://www.w3.org/2000/svg",
414
+ viewBox: "0 0 2048 2048",
415
+ ...props
416
+ }, /* @__PURE__ */ React7.createElement("path", {
417
+ fill: "#101010",
418
+ d: "M1024 1843H446v-74q-4-80 42-137t125-108q117-91 172-217t78-268H576l284-239q-86-74-86-188 0-103 73-177t177-74q103 0 177 74t73 177q0 114-86 188l284 239h-287q23 141 78 268t172 217q79 51 125 108t42 137v74z"
419
+ }));
484
420
  var bP_default = SvgBP;
485
421
 
486
422
  // src/gen/pieces/chess/alpha/bQ.tsx
487
- import { useSignals as _useSignals8 } from "@preact-signals/safe-react/tracking";
488
423
  import * as React8 from "react";
489
- var SvgBQ = (props) => {
490
- var _effect = _useSignals8();
491
- try {
492
- return /* @__PURE__ */ React8.createElement("svg", {
493
- xmlns: "http://www.w3.org/2000/svg",
494
- viewBox: "0 0 2048 2048",
495
- ...props
496
- }, /* @__PURE__ */ React8.createElement("path", {
497
- fill: "#f9f9f9",
498
- d: "m520 1801.8 41.5-448.7 474-128.9 458 133.5 34.4 446.4z"
499
- }), /* @__PURE__ */ React8.createElement("path", {
500
- fill: "#101010",
501
- d: "M590 1519q4 72-15 158l134-86zm434 324H441q114-231 57.5-456.5T296 937q-12 2-19 2-54 0-92.5-38.5T146 808t38.5-92.5T277 677t92.5 38.5T408 808q0 20-6 38-4 14-15 33l196 139 100-486q-64-31-72-103-5-44 29-91t88-53q54-5 96 29t48 88q7 68-46 114l198 412 198-412q-54-46-46-114 6-54 48-88t96-29q54 6 87.5 53t29.5 91q-9 72-72 103l100 486 196-139q-12-19-15-33-6-18-6-38 0-54 38.5-92.5T1771 677t92.5 38.5T1902 808t-38.5 92.5T1771 939q-7 0-19-2-147 224-203 449.5t58 456.5zm0-450q109 0 222 28.5t213 67.5q2-41 11-89-108-42-221.5-68t-224.5-26-225 26-221 68q8 48 11 89 99-39 212-67.5t223-28.5m0 376h478q-15-34-24-73H570q-10 39-24 73zm434-250-119 72 134 86q-20-86-15-158m-573 47 139 87 139-84-139-86z"
502
- }));
503
- } finally {
504
- _effect.f();
505
- }
506
- };
424
+ var SvgBQ = (props) => /* @__PURE__ */ React8.createElement("svg", {
425
+ xmlns: "http://www.w3.org/2000/svg",
426
+ viewBox: "0 0 2048 2048",
427
+ ...props
428
+ }, /* @__PURE__ */ React8.createElement("path", {
429
+ fill: "#f9f9f9",
430
+ d: "m520 1801.8 41.5-448.7 474-128.9 458 133.5 34.4 446.4z"
431
+ }), /* @__PURE__ */ React8.createElement("path", {
432
+ fill: "#101010",
433
+ d: "M590 1519q4 72-15 158l134-86zm434 324H441q114-231 57.5-456.5T296 937q-12 2-19 2-54 0-92.5-38.5T146 808t38.5-92.5T277 677t92.5 38.5T408 808q0 20-6 38-4 14-15 33l196 139 100-486q-64-31-72-103-5-44 29-91t88-53q54-5 96 29t48 88q7 68-46 114l198 412 198-412q-54-46-46-114 6-54 48-88t96-29q54 6 87.5 53t29.5 91q-9 72-72 103l100 486 196-139q-12-19-15-33-6-18-6-38 0-54 38.5-92.5T1771 677t92.5 38.5T1902 808t-38.5 92.5T1771 939q-7 0-19-2-147 224-203 449.5t58 456.5zm0-450q109 0 222 28.5t213 67.5q2-41 11-89-108-42-221.5-68t-224.5-26-225 26-221 68q8 48 11 89 99-39 212-67.5t223-28.5m0 376h478q-15-34-24-73H570q-10 39-24 73zm434-250-119 72 134 86q-20-86-15-158m-573 47 139 87 139-84-139-86z"
434
+ }));
507
435
  var bQ_default = SvgBQ;
508
436
 
509
437
  // src/gen/pieces/chess/alpha/bR.tsx
510
- import { useSignals as _useSignals9 } from "@preact-signals/safe-react/tracking";
511
438
  import * as React9 from "react";
512
- var SvgBR = (props) => {
513
- var _effect = _useSignals9();
514
- try {
515
- return /* @__PURE__ */ React9.createElement("svg", {
516
- xmlns: "http://www.w3.org/2000/svg",
517
- viewBox: "0 0 2048 2048",
518
- ...props
519
- }, /* @__PURE__ */ React9.createElement("path", {
520
- fill: "#f9f9f9",
521
- d: "m674 732-76 807 851 14-75-833z"
522
- }), /* @__PURE__ */ React9.createElement("path", {
523
- fill: "#101010",
524
- d: "M1024 1843H383l29-264 159-118 50-659-149-107-17-341h289v147h137V354h286v147h137V354h289l-17 341-149 107 50 659 159 118 29 264zm0-989h333l-6-88H697l-6 88zm0 647h381l-6-87H649l-6 87z"
525
- }));
526
- } finally {
527
- _effect.f();
528
- }
529
- };
439
+ var SvgBR = (props) => /* @__PURE__ */ React9.createElement("svg", {
440
+ xmlns: "http://www.w3.org/2000/svg",
441
+ viewBox: "0 0 2048 2048",
442
+ ...props
443
+ }, /* @__PURE__ */ React9.createElement("path", {
444
+ fill: "#f9f9f9",
445
+ d: "m674 732-76 807 851 14-75-833z"
446
+ }), /* @__PURE__ */ React9.createElement("path", {
447
+ fill: "#101010",
448
+ d: "M1024 1843H383l29-264 159-118 50-659-149-107-17-341h289v147h137V354h286v147h137V354h289l-17 341-149 107 50 659 159 118 29 264zm0-989h333l-6-88H697l-6 88zm0 647h381l-6-87H649l-6 87z"
449
+ }));
530
450
  var bR_default = SvgBR;
531
451
 
532
452
  // src/gen/pieces/chess/alpha/wB.tsx
533
- import { useSignals as _useSignals10 } from "@preact-signals/safe-react/tracking";
534
453
  import * as React10 from "react";
535
- var SvgWB = (props) => {
536
- var _effect = _useSignals10();
537
- try {
538
- return /* @__PURE__ */ React10.createElement("svg", {
539
- xmlns: "http://www.w3.org/2000/svg",
540
- viewBox: "0 0 2048 2048",
541
- ...props
542
- }, /* @__PURE__ */ React10.createElement("path", {
543
- fill: "#f9f9f9",
544
- d: "m948 366 1-139 148-7 1 147zM564 860c114-267 456-443 456-443s392 176 476 502c-9 209-183 332-183 332l27 221-653 6 46-233s-230-171-169-385m-101 790c175 6 355 23 425-142h92s0 190-88 246c-163 103-625 38-625 38s-15-146 196-142m631 37-36-185 102 5s22 153 315 131c381-17 318 153 318 153l-483 5z"
545
- }), /* @__PURE__ */ React10.createElement("path", {
546
- fill: "#101010",
547
- d: "M1024 356q66 0 64-66 1-55-64-55-66 0-64 55-3 66 64 66m0 1204q0 114-101 199t-223 84H205q0-117 65-179t142-62h250q51 0 88-7t71-60l10-16h76q-7 21-3 13-45 105-109 125t-146 19H409q-52 0-86 40t-34 53h424q66 0 159-65t93-185H624q67-116 72-229-114-119-162-223t-6-224q33-96 118-189t312-247q-17-11-46-36t-29-79q0-58 41-96t100-38q58 0 100 38t41 96q0 54-29 79t-46 36q226 153 311 247t119 189q42 119-6 224t-162 223q4 113 72 229h-341q0 120 93 185t159 65h424q0-13-34-53t-86-40h-240q-83 0-146-19t-109-125q4 8-3-13h76l10 16q33 53 70 60t89 7h250q76 0 142 62t65 179h-495q-123 0-223-84t-101-199m0-114h283q-28-84-29-154-120-41-254-38-135-3-254 38-2 70-29 154zm0-267q159-1 285 42 189-180 142-346-60-193-427-431-368 238-427 431-48 166 142 346 125-43 285-42m-47-361V714h94v104h95v89h-95v165h-94V907h-95v-89z"
548
- }));
549
- } finally {
550
- _effect.f();
551
- }
552
- };
454
+ var SvgWB = (props) => /* @__PURE__ */ React10.createElement("svg", {
455
+ xmlns: "http://www.w3.org/2000/svg",
456
+ viewBox: "0 0 2048 2048",
457
+ ...props
458
+ }, /* @__PURE__ */ React10.createElement("path", {
459
+ fill: "#f9f9f9",
460
+ d: "m948 366 1-139 148-7 1 147zM564 860c114-267 456-443 456-443s392 176 476 502c-9 209-183 332-183 332l27 221-653 6 46-233s-230-171-169-385m-101 790c175 6 355 23 425-142h92s0 190-88 246c-163 103-625 38-625 38s-15-146 196-142m631 37-36-185 102 5s22 153 315 131c381-17 318 153 318 153l-483 5z"
461
+ }), /* @__PURE__ */ React10.createElement("path", {
462
+ fill: "#101010",
463
+ d: "M1024 356q66 0 64-66 1-55-64-55-66 0-64 55-3 66 64 66m0 1204q0 114-101 199t-223 84H205q0-117 65-179t142-62h250q51 0 88-7t71-60l10-16h76q-7 21-3 13-45 105-109 125t-146 19H409q-52 0-86 40t-34 53h424q66 0 159-65t93-185H624q67-116 72-229-114-119-162-223t-6-224q33-96 118-189t312-247q-17-11-46-36t-29-79q0-58 41-96t100-38q58 0 100 38t41 96q0 54-29 79t-46 36q226 153 311 247t119 189q42 119-6 224t-162 223q4 113 72 229h-341q0 120 93 185t159 65h424q0-13-34-53t-86-40h-240q-83 0-146-19t-109-125q4 8-3-13h76l10 16q33 53 70 60t89 7h250q76 0 142 62t65 179h-495q-123 0-223-84t-101-199m0-114h283q-28-84-29-154-120-41-254-38-135-3-254 38-2 70-29 154zm0-267q159-1 285 42 189-180 142-346-60-193-427-431-368 238-427 431-48 166 142 346 125-43 285-42m-47-361V714h94v104h95v89h-95v165h-94V907h-95v-89z"
464
+ }));
553
465
  var wB_default = SvgWB;
554
466
 
555
467
  // src/gen/pieces/chess/alpha/wK.tsx
556
- import { useSignals as _useSignals11 } from "@preact-signals/safe-react/tracking";
557
468
  import * as React11 from "react";
558
- var SvgWK = (props) => {
559
- var _effect = _useSignals11();
560
- try {
561
- return /* @__PURE__ */ React11.createElement("svg", {
562
- xmlns: "http://www.w3.org/2000/svg",
563
- viewBox: "0 0 2048 2048",
564
- ...props
565
- }, /* @__PURE__ */ React11.createElement("path", {
566
- fill: "#f9f9f9",
567
- d: "m501.6 1811 48.4-354.4-260-269.2s-166.4-288.2 29.9-481C582.2 448.7 826 727.2 826 727.2l195.6-165.7 184 165.7s216.4-232.5 430.4-76 255.4 317.6 117.4 531.6c-138.1 214-250.9 280.7-250.9 280.7L1558 1811z"
568
- }), /* @__PURE__ */ React11.createElement("path", {
569
- fill: "#101010",
570
- d: "M977 298v-95h94v95h107v95h-107v153q-48-16-94 0V393H870v-95zm47 314q-47 0-136 121-31-36-50-55 93-140 186-140 92 0 186 140-20 19-50 55-90-121-136-121m-447 907-26 156 145-84zm410-206q-1-147-36.5-274.5T870 845q-45-88-131.5-153T570 627q-103 0-208 93T257 949q0 109 86.5 236T546 1408q212-88 441-95m37 530H448l61-365q-325-280-326-535-1-159 125-274.5T575 553q78 0 158.5 47T876 719q61 74 98.5 164.5T1024 1034q12-60 49-150.5t99-164.5q61-72 142-119t159-47q140 0 266 115.5T1865 943q-2 255-326 535l61 365zm0-74h489l-50-298q-216-84-439-84t-439 84l-50 298zm447-250 26 156-145-84zm-410-206q229 7 441 95 115-96 202-223t87-236q0-136-105.5-229T1478 627q-83 0-169.5 65T1178 845q-46 66-81.5 193.5T1061 1313m-176 233 141-84 137 86-141 84z"
571
- }));
572
- } finally {
573
- _effect.f();
574
- }
575
- };
469
+ var SvgWK = (props) => /* @__PURE__ */ React11.createElement("svg", {
470
+ xmlns: "http://www.w3.org/2000/svg",
471
+ viewBox: "0 0 2048 2048",
472
+ ...props
473
+ }, /* @__PURE__ */ React11.createElement("path", {
474
+ fill: "#f9f9f9",
475
+ d: "m501.6 1811 48.4-354.4-260-269.2s-166.4-288.2 29.9-481C582.2 448.7 826 727.2 826 727.2l195.6-165.7 184 165.7s216.4-232.5 430.4-76 255.4 317.6 117.4 531.6c-138.1 214-250.9 280.7-250.9 280.7L1558 1811z"
476
+ }), /* @__PURE__ */ React11.createElement("path", {
477
+ fill: "#101010",
478
+ d: "M977 298v-95h94v95h107v95h-107v153q-48-16-94 0V393H870v-95zm47 314q-47 0-136 121-31-36-50-55 93-140 186-140 92 0 186 140-20 19-50 55-90-121-136-121m-447 907-26 156 145-84zm410-206q-1-147-36.5-274.5T870 845q-45-88-131.5-153T570 627q-103 0-208 93T257 949q0 109 86.5 236T546 1408q212-88 441-95m37 530H448l61-365q-325-280-326-535-1-159 125-274.5T575 553q78 0 158.5 47T876 719q61 74 98.5 164.5T1024 1034q12-60 49-150.5t99-164.5q61-72 142-119t159-47q140 0 266 115.5T1865 943q-2 255-326 535l61 365zm0-74h489l-50-298q-216-84-439-84t-439 84l-50 298zm447-250 26 156-145-84zm-410-206q229 7 441 95 115-96 202-223t87-236q0-136-105.5-229T1478 627q-83 0-169.5 65T1178 845q-46 66-81.5 193.5T1061 1313m-176 233 141-84 137 86-141 84z"
479
+ }));
576
480
  var wK_default = SvgWK;
577
481
 
578
482
  // src/gen/pieces/chess/alpha/wN.tsx
579
- import { useSignals as _useSignals12 } from "@preact-signals/safe-react/tracking";
580
483
  import * as React12 from "react";
581
- var SvgWN = (props) => {
582
- var _effect = _useSignals12();
583
- try {
584
- return /* @__PURE__ */ React12.createElement("svg", {
585
- xmlns: "http://www.w3.org/2000/svg",
586
- viewBox: "0 0 2048 2048",
587
- ...props
588
- }, /* @__PURE__ */ React12.createElement("path", {
589
- fill: "#f9f9f9",
590
- d: "m352 861 787-569 94 148s336 103 398 388c63 286 51 974 51 974l-1088 9s-37-290 184-460c221-171 221-212 221-212s-226-71-295-16-117 138-117 138l-129-67 74-85-88-97-94 56z"
591
- }), /* @__PURE__ */ React12.createElement("path", {
592
- fill: "#101010",
593
- d: "m1151 178-115 154c-74 50-147 98-220 144-73 45-112 81-116 107L304 846l12 297 122-86 51 50-115 82 217 121 56-102c37-68 135-88 292-60l-55 85c-25 37-63 60-115 71a608 608 0 0 0-183 238c-32 82-45 182-39 301h1242c-23-55-42-118-57-190-15-73-17-152-5-237 29-239 13-440-47-603-61-164-205-303-433-418zm-17 145 59 133a664 664 0 0 1 262 188c55 72 100 150 134 234 27 97 40 181 41 253 0 71-3 140-9 205-7 65-11 131-13 199-2 67 9 145 32 234H621c-4-84 12-158 48-223s85-124 146-177c78-22 129-56 152-102s53-90 90-131c13-10 27-15 38-15 10-1 21 0 33-2 52-7 95-36 129-85 33-49 51-104 52-165l-19-67c-37 159-99 245-188 257l-45 6c-16 1-33 10-52 26-41-25-87-35-138-31q-111 9-165 27l-108 73-39 45-47-28 78-65-138-144-64 41-4-125 366-241c15-34 58-74 131-120l208-131zM960 564c-6 0-12 2-18 7L826 671l212 2c23 0 17-21-16-63-24-31-44-46-62-46M502 868l-33 4-33 56 57 26 46-55z"
594
- }));
595
- } finally {
596
- _effect.f();
597
- }
598
- };
484
+ var SvgWN = (props) => /* @__PURE__ */ React12.createElement("svg", {
485
+ xmlns: "http://www.w3.org/2000/svg",
486
+ viewBox: "0 0 2048 2048",
487
+ ...props
488
+ }, /* @__PURE__ */ React12.createElement("path", {
489
+ fill: "#f9f9f9",
490
+ d: "m352 861 787-569 94 148s336 103 398 388c63 286 51 974 51 974l-1088 9s-37-290 184-460c221-171 221-212 221-212s-226-71-295-16-117 138-117 138l-129-67 74-85-88-97-94 56z"
491
+ }), /* @__PURE__ */ React12.createElement("path", {
492
+ fill: "#101010",
493
+ d: "m1151 178-115 154c-74 50-147 98-220 144-73 45-112 81-116 107L304 846l12 297 122-86 51 50-115 82 217 121 56-102c37-68 135-88 292-60l-55 85c-25 37-63 60-115 71a608 608 0 0 0-183 238c-32 82-45 182-39 301h1242c-23-55-42-118-57-190-15-73-17-152-5-237 29-239 13-440-47-603-61-164-205-303-433-418zm-17 145 59 133a664 664 0 0 1 262 188c55 72 100 150 134 234 27 97 40 181 41 253 0 71-3 140-9 205-7 65-11 131-13 199-2 67 9 145 32 234H621c-4-84 12-158 48-223s85-124 146-177c78-22 129-56 152-102s53-90 90-131c13-10 27-15 38-15 10-1 21 0 33-2 52-7 95-36 129-85 33-49 51-104 52-165l-19-67c-37 159-99 245-188 257l-45 6c-16 1-33 10-52 26-41-25-87-35-138-31q-111 9-165 27l-108 73-39 45-47-28 78-65-138-144-64 41-4-125 366-241c15-34 58-74 131-120l208-131zM960 564c-6 0-12 2-18 7L826 671l212 2c23 0 17-21-16-63-24-31-44-46-62-46M502 868l-33 4-33 56 57 26 46-55z"
494
+ }));
599
495
  var wN_default = SvgWN;
600
496
 
601
497
  // src/gen/pieces/chess/alpha/wP.tsx
602
- import { useSignals as _useSignals13 } from "@preact-signals/safe-react/tracking";
603
498
  import * as React13 from "react";
604
- var SvgWP = (props) => {
605
- var _effect = _useSignals13();
606
- try {
607
- return /* @__PURE__ */ React13.createElement("svg", {
608
- xmlns: "http://www.w3.org/2000/svg",
609
- viewBox: "0 0 2048 2048",
610
- ...props
611
- }, /* @__PURE__ */ React13.createElement("path", {
612
- fill: "#f9f9f9",
613
- d: "m734 981 196-193s-189-82-79-288c79-149 303-114 361 50 63 179-113 240-113 240l226 197Zm-235 799s-8-107 50-154c196-173 338-386 371-599l210 2c33 206 182 447 321 561 101 59 99 199 99 199z"
614
- }), /* @__PURE__ */ React13.createElement("path", {
615
- fill: "#101010",
616
- d: "M520 1769h1008q8-97-132-182-132-101-196-239t-80-309H928q-15 170-79 309t-197 239q-141 85-132 182m504 74H446v-74q-4-80 42-137t125-108q117-91 172-217t78-268H576l284-239q-86-74-86-188 0-103 73-177t177-74q103 0 177 74t73 177q0 114-86 188l284 239h-287q23 141 78 268t172 217q79 51 125 108t42 137v74zM756 974h536l-225-191q134-31 134-171 0-76-52-126t-125-51q-73 0-125 51t-52 126q0 140 134 171z"
617
- }));
618
- } finally {
619
- _effect.f();
620
- }
621
- };
499
+ var SvgWP = (props) => /* @__PURE__ */ React13.createElement("svg", {
500
+ xmlns: "http://www.w3.org/2000/svg",
501
+ viewBox: "0 0 2048 2048",
502
+ ...props
503
+ }, /* @__PURE__ */ React13.createElement("path", {
504
+ fill: "#f9f9f9",
505
+ d: "m734 981 196-193s-189-82-79-288c79-149 303-114 361 50 63 179-113 240-113 240l226 197Zm-235 799s-8-107 50-154c196-173 338-386 371-599l210 2c33 206 182 447 321 561 101 59 99 199 99 199z"
506
+ }), /* @__PURE__ */ React13.createElement("path", {
507
+ fill: "#101010",
508
+ d: "M520 1769h1008q8-97-132-182-132-101-196-239t-80-309H928q-15 170-79 309t-197 239q-141 85-132 182m504 74H446v-74q-4-80 42-137t125-108q117-91 172-217t78-268H576l284-239q-86-74-86-188 0-103 73-177t177-74q103 0 177 74t73 177q0 114-86 188l284 239h-287q23 141 78 268t172 217q79 51 125 108t42 137v74zM756 974h536l-225-191q134-31 134-171 0-76-52-126t-125-51q-73 0-125 51t-52 126q0 140 134 171z"
509
+ }));
622
510
  var wP_default = SvgWP;
623
511
 
624
512
  // src/gen/pieces/chess/alpha/wQ.tsx
625
- import { useSignals as _useSignals14 } from "@preact-signals/safe-react/tracking";
626
513
  import * as React14 from "react";
627
- var SvgWQ = (props) => {
628
- var _effect = _useSignals14();
629
- try {
630
- return /* @__PURE__ */ React14.createElement("svg", {
631
- xmlns: "http://www.w3.org/2000/svg",
632
- viewBox: "0 0 2048 2048",
633
- ...props
634
- }, /* @__PURE__ */ React14.createElement("path", {
635
- fill: "#f9f9f9",
636
- d: "m508.5 1815.6 48.4-356.7-216.3-554.6-135.8-20.7-16.1-126.5 112.7-43.8 78.3 73.7-18.4 99 246.2 197.8 112.8-568.3L635 428l78.3-108 112.8 43.7-23 161 223.2 474 244-490-66.8-105.9 92-92 105.9 73.6L1337 534l103.5 529.2 260-161-16-142.7 131-46 57.6 131.1-207 103.6-175 529.2 48.4 308.4z"
637
- }), /* @__PURE__ */ React14.createElement("path", {
638
- fill: "#101010",
639
- d: "M1024 1769h478q-53-130-43-280-100-39-213-67.5t-222-28.5q-110 0-223 28.5T589 1489q9 150-43 280zm0-450q111 0 223.5 26.5T1468 1413q17-105 60.5-212.5T1634 988l-220 155-123-601-267 555-267-555-123 601-220-155q61 105 104.5 212.5T580 1413q108-41 220.5-67.5T1024 1319m0 524H441q114-231 57.5-456.5T296 937q-12 2-19 2-54 0-92.5-38.5T146 808t38.5-92.5T277 677t92.5 38.5T408 808q0 20-6 38-4 14-15 33l196 139 100-486q-64-31-72-103-5-44 29-91t88-53q54-5 96 29t48 88q7 68-46 114l198 412 198-412q-54-46-46-114 6-54 48-88t96-29q54 6 87.5 53t29.5 91q-9 72-72 103l100 486 196-139q-12-19-15-33-6-18-6-38 0-54 38.5-92.5T1771 677t92.5 38.5T1902 808t-38.5 92.5T1771 939q-7 0-19-2-147 224-203 449.5t58 456.5zM276 746q-62 0-62 62t62 62q63 0 63-62t-63-62m466-394q-62 0-62 62t62 62 62-62-62-62M590 1519l119 72-134 86q19-86 15-158m1182-773q-63 0-63 62t63 62q62 0 62-62t-62-62m-466-394q-62 0-62 62t62 62 62-62-62-62m152 1167-119 72 134 86q-20-86-15-158m-573 47 139-83 139 86-139 84z"
640
- }));
641
- } finally {
642
- _effect.f();
643
- }
644
- };
514
+ var SvgWQ = (props) => /* @__PURE__ */ React14.createElement("svg", {
515
+ xmlns: "http://www.w3.org/2000/svg",
516
+ viewBox: "0 0 2048 2048",
517
+ ...props
518
+ }, /* @__PURE__ */ React14.createElement("path", {
519
+ fill: "#f9f9f9",
520
+ d: "m508.5 1815.6 48.4-356.7-216.3-554.6-135.8-20.7-16.1-126.5 112.7-43.8 78.3 73.7-18.4 99 246.2 197.8 112.8-568.3L635 428l78.3-108 112.8 43.7-23 161 223.2 474 244-490-66.8-105.9 92-92 105.9 73.6L1337 534l103.5 529.2 260-161-16-142.7 131-46 57.6 131.1-207 103.6-175 529.2 48.4 308.4z"
521
+ }), /* @__PURE__ */ React14.createElement("path", {
522
+ fill: "#101010",
523
+ d: "M1024 1769h478q-53-130-43-280-100-39-213-67.5t-222-28.5q-110 0-223 28.5T589 1489q9 150-43 280zm0-450q111 0 223.5 26.5T1468 1413q17-105 60.5-212.5T1634 988l-220 155-123-601-267 555-267-555-123 601-220-155q61 105 104.5 212.5T580 1413q108-41 220.5-67.5T1024 1319m0 524H441q114-231 57.5-456.5T296 937q-12 2-19 2-54 0-92.5-38.5T146 808t38.5-92.5T277 677t92.5 38.5T408 808q0 20-6 38-4 14-15 33l196 139 100-486q-64-31-72-103-5-44 29-91t88-53q54-5 96 29t48 88q7 68-46 114l198 412 198-412q-54-46-46-114 6-54 48-88t96-29q54 6 87.5 53t29.5 91q-9 72-72 103l100 486 196-139q-12-19-15-33-6-18-6-38 0-54 38.5-92.5T1771 677t92.5 38.5T1902 808t-38.5 92.5T1771 939q-7 0-19-2-147 224-203 449.5t58 456.5zM276 746q-62 0-62 62t62 62q63 0 63-62t-63-62m466-394q-62 0-62 62t62 62 62-62-62-62M590 1519l119 72-134 86q19-86 15-158m1182-773q-63 0-63 62t63 62q62 0 62-62t-62-62m-466-394q-62 0-62 62t62 62 62-62-62-62m152 1167-119 72 134 86q-20-86-15-158m-573 47 139-83 139 86-139 84z"
524
+ }));
645
525
  var wQ_default = SvgWQ;
646
526
 
647
527
  // src/gen/pieces/chess/alpha/wR.tsx
648
- import { useSignals as _useSignals15 } from "@preact-signals/safe-react/tracking";
649
528
  import * as React15 from "react";
650
- var SvgWR = (props) => {
651
- var _effect = _useSignals15();
652
- try {
653
- return /* @__PURE__ */ React15.createElement("svg", {
654
- xmlns: "http://www.w3.org/2000/svg",
655
- viewBox: "0 0 2048 2048",
656
- ...props
657
- }, /* @__PURE__ */ React15.createElement("path", {
658
- fill: "#f9f9f9",
659
- d: "m435 1804 16-212 152-115 51-688-148-115-7-276 210-2 4 138 198 2 7-140 212-3 14 145 193-4 5-138h204l-7 285-145 106 42 693 172 124 19 207z"
660
- }), /* @__PURE__ */ React15.createElement("path", {
661
- fill: "#101010",
662
- d: "M1024 1501H643l5-74h752l5 74zm0-661H692l5-74h654l5 74zm0 1003H383l29-264 159-118 50-659-149-107-17-341h289v147h137V354h286v147h137V354h289l-17 341-149 107 50 659 159 118 29 264zm0-74h557l-15-149-161-119-54-735 152-109 13-230h-138v148h-285V427H955v148H670V427H532l13 230 152 109-54 735-161 119-15 149z"
663
- }));
664
- } finally {
665
- _effect.f();
666
- }
667
- };
529
+ var SvgWR = (props) => /* @__PURE__ */ React15.createElement("svg", {
530
+ xmlns: "http://www.w3.org/2000/svg",
531
+ viewBox: "0 0 2048 2048",
532
+ ...props
533
+ }, /* @__PURE__ */ React15.createElement("path", {
534
+ fill: "#f9f9f9",
535
+ d: "m435 1804 16-212 152-115 51-688-148-115-7-276 210-2 4 138 198 2 7-140 212-3 14 145 193-4 5-138h204l-7 285-145 106 42 693 172 124 19 207z"
536
+ }), /* @__PURE__ */ React15.createElement("path", {
537
+ fill: "#101010",
538
+ d: "M1024 1501H643l5-74h752l5 74zm0-661H692l5-74h654l5 74zm0 1003H383l29-264 159-118 50-659-149-107-17-341h289v147h137V354h286v147h137V354h289l-17 341-149 107 50 659 159 118 29 264zm0-74h557l-15-149-161-119-54-735 152-109 13-230h-138v148h-285V427H955v148H670V427H532l13 230 152 109-54 735-161 119-15 149z"
539
+ }));
668
540
  var wR_default = SvgWR;
669
541
 
670
- // src/Chessboard/chess.ts
671
- var __dxlog_file4 = "/__w/dxos/dxos/packages/ui/react-ui-gameboard/src/Chessboard/chess.ts";
542
+ // src/components/Chessboard/chess.ts
543
+ var __dxlog_file4 = "/__w/dxos/dxos/packages/ui/react-ui-gameboard/src/components/Chessboard/chess.ts";
672
544
  var ChessPieces = alpha_exports;
673
545
  var posToLocation = (pos) => {
674
546
  const col = pos.charCodeAt(0) - "a".charCodeAt(0);
@@ -681,6 +553,9 @@ var posToLocation = (pos) => {
681
553
  var locationToPos = ([row, col]) => {
682
554
  return String.fromCharCode(col + "a".charCodeAt(0)) + (row + 1);
683
555
  };
556
+ var getRawPgn = (pgn) => {
557
+ return pgn.replace(/\[.*?\]/g, "").trim();
558
+ };
684
559
  var styles = {
685
560
  neutral: {
686
561
  black: "bg-neutral-50",
@@ -707,55 +582,90 @@ var boardStyles = styles.original;
707
582
  var getSquareColor = ([row, col]) => {
708
583
  return (col + row) % 2 === 0 ? boardStyles.black : boardStyles.white;
709
584
  };
710
- var makeMove = (game, move) => {
711
- const from = locationToPos(move.from);
712
- const to = locationToPos(move.to);
713
- try {
714
- log4("makeMove", {
715
- move
716
- }, {
717
- F: __dxlog_file4,
718
- L: 72,
719
- S: void 0,
720
- C: (f, a) => f(...a)
721
- });
722
- const promotion = move.promotion ? move.promotion[1].toLowerCase() : "q";
723
- game.move({
724
- from,
725
- to,
726
- promotion
727
- }, {
728
- strict: false
729
- });
730
- return game;
731
- } catch (err) {
732
- return null;
585
+ var createChess = (pgn) => {
586
+ const chess = new ChessJS();
587
+ if (pgn) {
588
+ try {
589
+ chess.loadPgn(pgn);
590
+ } catch {
591
+ log4.warn(pgn, void 0, {
592
+ F: __dxlog_file4,
593
+ L: 76,
594
+ S: void 0,
595
+ C: (f, a) => f(...a)
596
+ });
597
+ }
733
598
  }
599
+ return chess;
734
600
  };
735
601
  var ChessModel = class {
736
- constructor(fen) {
737
- this._pieces = signal({});
738
- this.initialize(fen);
602
+ _registry;
603
+ _chess = new ChessJS();
604
+ _pieces = Atom.make({});
605
+ _moveIndex = Atom.make(0);
606
+ constructor(_registry, pgn) {
607
+ this._registry = _registry;
608
+ this.update(pgn);
609
+ }
610
+ get readonly() {
611
+ return this._registry.get(this._moveIndex) !== this._chess.history().length;
739
612
  }
740
613
  get turn() {
741
- return this._game.turn() === "w" ? "white" : "black";
614
+ return this._chess.turn() === "w" ? "white" : "black";
615
+ }
616
+ get game() {
617
+ return this._chess;
742
618
  }
743
619
  get pieces() {
744
620
  return this._pieces;
745
621
  }
746
- get game() {
747
- return this._game;
622
+ get moveIndex() {
623
+ return this._moveIndex;
748
624
  }
749
625
  get fen() {
750
- return this._game.fen();
626
+ return this._chess.fen();
751
627
  }
752
- initialize(fen) {
753
- this._pieces.value = {};
754
- this._game = new Chess(fen ? validateFen(fen).ok ? fen : void 0 : void 0);
755
- this._update();
628
+ /**
629
+ * PGN with headers.
630
+ *
631
+ * [Event "?"]
632
+ * [Site "?"]
633
+ * [Date "2025.08.05"]
634
+ * [Round "?"]
635
+ * [White "?"]
636
+ * [Black "?"]
637
+ * [Result "*"]
638
+ */
639
+ // TODO(burdon): Update headers.
640
+ get pgn() {
641
+ return getRawPgn(this._chess.pgn());
642
+ }
643
+ setMoveIndex(index) {
644
+ const temp = new ChessJS();
645
+ const history = this._chess.history({
646
+ verbose: true
647
+ });
648
+ for (let i = 0; i < index && i < history.length; i++) {
649
+ temp.move(history[i]);
650
+ }
651
+ this._updateBoard(temp);
652
+ }
653
+ update(pgn = "") {
654
+ const previous = this._chess.history();
655
+ try {
656
+ this._chess.loadPgn(pgn);
657
+ this._chess.setHeader("Date", createDate());
658
+ this._chess.setHeader("Site", "dxos.org");
659
+ } catch {
660
+ }
661
+ const current = this._chess.history();
662
+ if (!isValidNextMove(previous, current)) {
663
+ this._registry.set(this._pieces, {});
664
+ }
665
+ this._updateBoard(this._chess);
756
666
  }
757
667
  isValidMove(move) {
758
- return makeMove(new Chess(this._game.fen()), move) !== null;
668
+ return tryMove(new ChessJS(this._chess.fen()), move) !== null;
759
669
  }
760
670
  canPromote(move) {
761
671
  const isPawnMove = move.piece === "BP" || move.piece === "WP";
@@ -763,47 +673,104 @@ var ChessModel = class {
763
673
  return isPawnMove && isToLastRank;
764
674
  }
765
675
  makeMove(move) {
766
- const game = makeMove(this._game, move);
676
+ const game = tryMove(this._chess, move);
767
677
  if (!game) {
768
678
  return false;
769
679
  }
770
- this._game = game;
771
- this._update();
680
+ this._updateBoard(this._chess);
772
681
  return true;
773
682
  }
774
683
  makeRandomMove() {
775
- const moves = this._game.moves();
684
+ const moves = this._chess.moves();
776
685
  if (!moves.length) {
777
686
  return false;
778
687
  }
779
688
  const move = moves[Math.floor(Math.random() * moves.length)];
780
- this._game.move(move);
781
- this._update();
689
+ this._chess.move(move);
690
+ this._updateBoard(this._chess);
782
691
  return true;
783
692
  }
784
693
  /**
785
694
  * Update pieces preserving identity.
786
695
  */
787
- _update() {
788
- const pieces = {};
789
- this._game.board().flatMap((row) => row.forEach((record) => {
790
- if (!record) {
791
- return;
792
- }
793
- const { square, type, color } = record;
794
- const pieceType = `${color.toUpperCase()}${type.toUpperCase()}`;
795
- const location = posToLocation(square);
796
- pieces[locationToString(location)] = {
797
- id: `${square}-${pieceType}`,
798
- type: pieceType,
799
- side: color === "w" ? "white" : "black",
800
- location
801
- };
802
- }));
803
- this._pieces.value = mapPieces(this._pieces.value, pieces);
696
+ _updateBoard(chess) {
697
+ this._registry.set(this._pieces, createPieceMap(chess));
698
+ this._registry.set(this._moveIndex, chess.history().length);
804
699
  }
805
700
  };
806
- var mapPieces = (before, after) => {
701
+ var tryMove = (chess, move) => {
702
+ const from = locationToPos(move.from);
703
+ const to = locationToPos(move.to);
704
+ try {
705
+ const promotion = move.promotion ? move.promotion[1].toLowerCase() : "q";
706
+ chess.move({
707
+ from,
708
+ to,
709
+ promotion
710
+ }, {
711
+ strict: false
712
+ });
713
+ return chess;
714
+ } catch {
715
+ return null;
716
+ }
717
+ };
718
+ var isValidNextMove = (previous, current) => {
719
+ if (current.length > previous.length + 1) {
720
+ return false;
721
+ }
722
+ for (let i = 0; i < previous.length; i++) {
723
+ if (previous[i] !== current[i]) {
724
+ return false;
725
+ }
726
+ }
727
+ return true;
728
+ };
729
+ var createPieceMap = (chess) => {
730
+ const temp = new ChessJS();
731
+ let pieces = _createPieceMap(temp);
732
+ const history = chess.history({
733
+ verbose: true
734
+ });
735
+ for (let i = 0; i < history.length; i++) {
736
+ const move = history[i];
737
+ temp.move(move);
738
+ pieces = _diffPieces(pieces, _createPieceMap(temp));
739
+ const test = /* @__PURE__ */ new Set();
740
+ Object.values(pieces).forEach((piece) => {
741
+ invariant3(!test.has(piece.id), "Duplicate: " + piece.id, {
742
+ F: __dxlog_file4,
743
+ L: 255,
744
+ S: void 0,
745
+ A: [
746
+ "!test.has(piece.id)",
747
+ "'Duplicate: ' + piece.id"
748
+ ]
749
+ });
750
+ test.add(piece.id);
751
+ });
752
+ }
753
+ return pieces;
754
+ };
755
+ var _createPieceMap = (chess) => {
756
+ const pieces = {};
757
+ chess.board().flatMap((row) => row.forEach((record) => {
758
+ if (!record) {
759
+ return;
760
+ }
761
+ const { square, type, color } = record;
762
+ const pieceType = `${color.toUpperCase()}${type.toUpperCase()}`;
763
+ const location = posToLocation(square);
764
+ pieces[locationToString(location)] = {
765
+ id: `${square}-${pieceType}`,
766
+ type: pieceType,
767
+ side: color === "w" ? "white" : "black",
768
+ location
769
+ };
770
+ }));
771
+ return pieces;
772
+ };
773
+ var _diffPieces = (before, after) => {
807
774
  const difference = {
808
775
  removed: {},
809
776
  added: {}
@@ -826,191 +793,170 @@ var mapPieces = (before, after) => {
826
793
  piece.id = previous.id;
827
794
  }
828
795
  }
829
- log4("delta", {
830
- before: Object.keys(before).length,
831
- after: Object.keys(after).length,
832
- removed: Object.keys(difference.removed).length,
833
- added: Object.keys(difference.added).length
834
- }, {
835
- F: __dxlog_file4,
836
- L: 205,
837
- S: void 0,
838
- C: (f, a) => f(...a)
839
- });
840
796
  return after;
841
797
  };
798
+ var createDate = (date = /* @__PURE__ */ new Date()) => date.toISOString().slice(0, 10).replace(/-/g, ".");
842
799
 
843
- // src/Chessboard/Chessboard.tsx
844
- import { useSignals as _useSignals16 } from "@preact-signals/safe-react/tracking";
845
- import React16, { useRef as useRef3, useMemo, useEffect as useEffect4, useState as useState4, memo as memo3 } from "react";
800
+ // src/components/Chessboard/Chessboard.tsx
801
+ import { Atom as Atom2, useAtomValue } from "@effect-atom/atom-react";
802
+ import React16, { forwardRef as forwardRef2, memo as memo3, useEffect as useEffect4, useMemo, useRef as useRef3, useState as useState4 } from "react";
846
803
  import { useResizeDetector } from "react-resize-detector";
847
- import { useTrackProps as useTrackProps2 } from "@dxos/react-ui";
848
- import { mx as mx4 } from "@dxos/react-ui-theme";
849
- import { isNotFalsy } from "@dxos/util";
850
- var Chessboard = /* @__PURE__ */ memo3(({ orientation, showLabels, debug, rows = 8, cols = 8, classNames }) => {
851
- var _effect = _useSignals16();
852
- try {
853
- useTrackProps2({
854
- orientation,
855
- showLabels,
856
- debug
857
- }, Chessboard.displayName, false);
858
- const { ref: containerRef, width, height } = useResizeDetector({
859
- refreshRate: 200
860
- });
861
- const { model, promoting, onPromotion } = useBoardContext();
862
- const locations = useMemo(() => {
863
- return Array.from({
864
- length: rows
865
- }, (_, i) => orientation === "black" ? i : rows - 1 - i).flatMap((row) => Array.from({
866
- length: cols
867
- }).map((_, col) => [
868
- row,
869
- col
870
- ]));
871
- }, [
872
- orientation,
873
- rows,
874
- cols
875
- ]);
876
- const layout = useMemo(() => {
877
- return locations.map((location) => {
878
- return /* @__PURE__ */ React16.createElement("div", {
879
- key: locationToString(location),
880
- ["data-location"]: locationToString(location)
881
- });
804
+ import { useForwardedRef } from "@dxos/react-ui";
805
+ import { mx as mx4 } from "@dxos/ui-theme";
806
+ import { isNonNullable } from "@dxos/util";
807
+ var EMPTY_PIECES_ATOM = Atom2.make({});
808
+ var ChessboardComponent = /* @__PURE__ */ forwardRef2(({ classNames, orientation, showLabels, debug, rows = 8, cols = 8 }, forwardedRef) => {
809
+ const targetRef = useForwardedRef(forwardedRef);
810
+ const { width, height } = useResizeDetector({
811
+ targetRef,
812
+ refreshRate: 200
813
+ });
814
+ const { model, promoting, onPromotion } = useGameboardContext(Chessboard.displayName);
815
+ const pieces = useAtomValue(model?.pieces ?? EMPTY_PIECES_ATOM);
816
+ const squares = useMemo(() => {
817
+ return Array.from({
818
+ length: rows
819
+ }, (_, i) => orientation === "black" ? i : rows - 1 - i).flatMap((row) => Array.from({
820
+ length: cols
821
+ }).map((_, col) => [
822
+ row,
823
+ col
824
+ ]));
825
+ }, [
826
+ orientation,
827
+ rows,
828
+ cols
829
+ ]);
830
+ const layout = useMemo(() => {
831
+ return squares.map((location) => {
832
+ return /* @__PURE__ */ React16.createElement("div", {
833
+ key: locationToString(location),
834
+ ["data-location"]: locationToString(location)
882
835
  });
883
- }, [
884
- locations
885
- ]);
886
- const [grid, setGrid] = useState4({});
887
- const gridRef = useRef3(null);
888
- useEffect4(() => {
889
- setGrid(locations.reduce((acc, location) => {
890
- const square = getSquareLocation(gridRef.current, location);
891
- const bounds = getRelativeBounds(gridRef.current, square);
892
- return {
893
- ...acc,
894
- [locationToString(location)]: bounds
895
- };
896
- }, {}));
897
- }, [
898
- locations,
899
- width,
900
- height
901
- ]);
902
- const positions = useMemo(() => {
903
- if (!gridRef.current) {
904
- return [];
905
- }
906
- return Object.values(model?.pieces.value ?? {}).map((piece) => {
907
- if (piece.id === promoting?.id) {
908
- return null;
909
- }
910
- const bounds = grid[locationToString(piece.location)];
911
- return {
912
- piece,
913
- bounds
914
- };
915
- }).filter(isNotFalsy);
916
- }, [
917
- grid,
918
- model?.pieces.value,
919
- promoting
920
- ]);
921
- return /* @__PURE__ */ React16.createElement("div", {
922
- ref: containerRef,
923
- className: mx4("relative", classNames)
924
- }, /* @__PURE__ */ React16.createElement("div", {
925
- ref: gridRef,
926
- className: "grid grid-rows-8 grid-cols-8 aspect-square select-none"
927
- }, layout), /* @__PURE__ */ React16.createElement("div", null, locations.map((location) => /* @__PURE__ */ React16.createElement(Square, {
928
- key: locationToString(location),
929
- location,
930
- label: showLabels ? locationToPos(location) : void 0,
931
- bounds: grid[locationToString(location)],
932
- classNames: getSquareColor(location)
933
- }))), /* @__PURE__ */ React16.createElement("div", {
934
- className: mx4(promoting && "opacity-50")
935
- }, positions.map(({ bounds, piece }) => /* @__PURE__ */ React16.createElement(Piece, {
936
- key: piece.id,
937
- piece,
938
- bounds,
939
- label: debug ? piece.id : void 0,
940
- orientation,
941
- Component: ChessPieces[piece.type]
942
- }))), /* @__PURE__ */ React16.createElement("div", null, promoting && /* @__PURE__ */ React16.createElement(PromotionSelector, {
943
- grid,
944
- piece: promoting,
945
- onSelect: (piece) => {
946
- onPromotion({
947
- from: Object.values(model.pieces.value).find((p) => p.id === promoting.id).location,
948
- to: piece.location,
949
- piece: promoting.type,
950
- promotion: piece.type
951
- });
836
+ });
837
+ }, [
838
+ squares
839
+ ]);
840
+ const [grid, setGrid] = useState4({});
841
+ const gridRef = useRef3(null);
842
+ useEffect4(() => {
843
+ setGrid(squares.reduce((acc, location) => {
844
+ const square = getSquareLocation(gridRef.current, location);
845
+ const bounds = getRelativeBounds(gridRef.current, square);
846
+ return {
847
+ ...acc,
848
+ [locationToString(location)]: bounds
849
+ };
850
+ }, {}));
851
+ }, [
852
+ squares,
853
+ width,
854
+ height
855
+ ]);
856
+ const positions = useMemo(() => {
857
+ if (!gridRef.current) {
858
+ return [];
859
+ }
860
+ return Object.values(pieces).map((piece) => {
861
+ if (piece.id === promoting?.id) {
862
+ return null;
952
863
  }
953
- })));
954
- } finally {
955
- _effect.f();
956
- }
957
- });
958
- Chessboard.displayName = "Chessboard";
959
- var getSquareLocation = (container, location) => {
960
- return container.querySelector(`[data-location="${locationToString(location)}"]`);
961
- };
962
- var PromotionSelector = ({ grid, piece, onSelect }) => {
963
- var _effect = _useSignals16();
964
- try {
965
- const positions = [
966
- "Q",
967
- "N",
968
- "R",
969
- "B"
970
- ].map((pieceType, i) => {
971
- const location = [
972
- piece.location[0] + (piece.location[0] === 0 ? i : -i),
973
- piece.location[1]
974
- ];
864
+ const bounds = grid[locationToString(piece.location)];
975
865
  return {
976
- piece: {
977
- id: `promotion-${pieceType}`,
978
- type: (piece.side === "black" ? "B" : "W") + pieceType,
979
- side: piece.side,
980
- location
981
- },
982
- bounds: grid[locationToString(location)]
866
+ piece,
867
+ bounds
983
868
  };
984
- });
985
- const handleSelect = (selected) => {
986
- onSelect({
987
- ...piece,
988
- type: selected.type
869
+ }).filter(isNonNullable);
870
+ }, [
871
+ grid,
872
+ pieces,
873
+ promoting
874
+ ]);
875
+ return /* @__PURE__ */ React16.createElement("div", {
876
+ ref: targetRef,
877
+ tabIndex: 0,
878
+ className: mx4("relative outline-none", classNames)
879
+ }, /* @__PURE__ */ React16.createElement("div", {
880
+ ref: gridRef,
881
+ className: "grid grid-rows-8 grid-cols-8 aspect-square select-none"
882
+ }, layout), /* @__PURE__ */ React16.createElement("div", null, squares.map((location) => /* @__PURE__ */ React16.createElement(Gameboard.Square, {
883
+ key: locationToString(location),
884
+ location,
885
+ label: showLabels ? locationToPos(location) : void 0,
886
+ bounds: grid[locationToString(location)],
887
+ classNames: getSquareColor(location)
888
+ }))), /* @__PURE__ */ React16.createElement("div", {
889
+ className: mx4(promoting && "opacity-50")
890
+ }, positions.map(({ bounds, piece }) => /* @__PURE__ */ React16.createElement(Gameboard.Piece, {
891
+ key: piece.id,
892
+ piece,
893
+ bounds,
894
+ label: debug ? piece.id : void 0,
895
+ orientation,
896
+ Component: ChessPieces[piece.type]
897
+ }))), promoting && /* @__PURE__ */ React16.createElement(PromotionSelector, {
898
+ grid,
899
+ piece: promoting,
900
+ onSelect: (piece) => {
901
+ onPromotion({
902
+ from: Object.values(pieces).find((p) => p.id === promoting.id).location,
903
+ to: piece.location,
904
+ piece: promoting.type,
905
+ promotion: piece.type
989
906
  });
907
+ }
908
+ }));
909
+ });
910
+ ChessboardComponent.displayName = "Chessboard";
911
+ var Chessboard = /* @__PURE__ */ memo3(ChessboardComponent);
912
+ var PromotionSelector = ({ grid, piece, onSelect }) => {
913
+ const positions = [
914
+ "Q",
915
+ "N",
916
+ "R",
917
+ "B"
918
+ ].map((pieceType, i) => {
919
+ const location = [
920
+ piece.location[0] + (piece.location[0] === 0 ? i : -i),
921
+ piece.location[1]
922
+ ];
923
+ return {
924
+ piece: {
925
+ id: `promotion-${pieceType}`,
926
+ type: (piece.side === "black" ? "B" : "W") + pieceType,
927
+ side: piece.side,
928
+ location
929
+ },
930
+ bounds: grid[locationToString(location)]
990
931
  };
991
- return /* @__PURE__ */ React16.createElement("div", null, positions.map(({ piece: piece2, bounds }) => /* @__PURE__ */ React16.createElement("div", {
992
- key: piece2.id,
993
- style: bounds,
994
- onClick: () => handleSelect(piece2)
995
- }, /* @__PURE__ */ React16.createElement(Piece, {
996
- piece: piece2,
997
- bounds,
998
- Component: ChessPieces[piece2.type],
999
- classNames: mx4("border-2 border-neutral-700 rounded-full", boardStyles.promotion)
1000
- }))));
1001
- } finally {
1002
- _effect.f();
1003
- }
932
+ });
933
+ const handleSelect = (selected) => {
934
+ onSelect({
935
+ ...piece,
936
+ type: selected.type
937
+ });
938
+ };
939
+ return /* @__PURE__ */ React16.createElement(React16.Fragment, null, positions.map(({ piece: piece2, bounds }) => /* @__PURE__ */ React16.createElement(Gameboard.Piece, {
940
+ key: piece2.id,
941
+ classNames: mx4("border-2 border-neutral-700 rounded-full", boardStyles.promotion),
942
+ piece: piece2,
943
+ bounds,
944
+ Component: ChessPieces[piece2.type],
945
+ onClick: () => handleSelect(piece2)
946
+ })));
947
+ };
948
+ var getSquareLocation = (container, location) => {
949
+ return container.querySelector(`[data-location="${locationToString(location)}"]`);
1004
950
  };
1005
951
  export {
1006
952
  ChessModel,
1007
953
  ChessPieces,
1008
954
  Chessboard,
1009
955
  Gameboard,
1010
- GameboardContext,
1011
- Piece,
1012
- Square,
1013
956
  boardStyles,
957
+ createChess,
958
+ createPieceMap,
959
+ getRawPgn,
1014
960
  getRelativeBounds,
1015
961
  getSquareColor,
1016
962
  isEqualLocation,
@@ -1018,9 +964,8 @@ export {
1018
964
  isPiece,
1019
965
  locationToPos,
1020
966
  locationToString,
1021
- mapPieces,
1022
967
  posToLocation,
1023
968
  stringToLocation,
1024
- useBoardContext
969
+ useGameboardContext
1025
970
  };
1026
971
  //# sourceMappingURL=index.mjs.map