@dxos/react-ui-gameboard 0.10.0 → 0.11.0

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.
@@ -0,0 +1,854 @@
1
+ import { createContext } from "@radix-ui/react-context";
2
+ import { draggable, dropTargetForElements, monitorForElements } from "@atlaskit/pragmatic-drag-and-drop/element/adapter";
3
+ import { forwardRef, memo, useCallback, useEffect, useMemo, useRef, useState } from "react";
4
+ import { log } from "@dxos/log";
5
+ import { mx } from "@dxos/ui-theme";
6
+ import { centerUnderPointer } from "@atlaskit/pragmatic-drag-and-drop/element/center-under-pointer";
7
+ import { setCustomNativeDragPreview } from "@atlaskit/pragmatic-drag-and-drop/element/set-custom-native-drag-preview";
8
+ import { createPortal } from "react-dom";
9
+ import { invariant } from "@dxos/invariant";
10
+ import { useDynamicRef, useForwardedRef } from "@dxos/react-ui";
11
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
12
+ import { Atom } from "@effect-atom/atom";
13
+ import { Chess } from "chess.js";
14
+ import { Atom as Atom$1, useAtomValue } from "@effect-atom/atom-react";
15
+ import { useResizeDetector } from "react-resize-detector";
16
+ import { isNonNullable } from "@dxos/util";
17
+ //#region \0rolldown/runtime.js
18
+ var __defProp = Object.defineProperty;
19
+ var __exportAll = (all, no_symbols) => {
20
+ let target = {};
21
+ for (var name in all) __defProp(target, name, {
22
+ get: all[name],
23
+ enumerable: true
24
+ });
25
+ if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
26
+ return target;
27
+ };
28
+ //#endregion
29
+ //#region src/components/Gameboard/types.ts
30
+ var locationToString = (location) => location.join("-");
31
+ var stringToLocation = (str) => str.split("-").map(Number);
32
+ var isPiece = (piece) => piece != null && typeof piece === "object" && "id" in piece && "type" in piece && "location" in piece && isLocation(piece.location);
33
+ var isLocation = (token) => Array.isArray(token) && token.length === 2 && token.every((val) => typeof val === "number");
34
+ var isEqualLocation = (l1, l2) => l1[0] === l2[0] && l1[1] === l2[1];
35
+ //#endregion
36
+ //#region src/components/Gameboard/util.ts
37
+ var getRelativeBounds = (container, element) => {
38
+ const containerRect = container.getBoundingClientRect();
39
+ const elementRect = element.getBoundingClientRect();
40
+ return {
41
+ top: elementRect.top - containerRect.top,
42
+ left: elementRect.left - containerRect.left,
43
+ width: elementRect.width,
44
+ height: elementRect.height
45
+ };
46
+ };
47
+ //#endregion
48
+ //#region src/components/Gameboard/GameboardContext.ts
49
+ var [GameboardContextProvider, useRadixGameboardContext] = createContext("Gameboard");
50
+ var useGameboardContext = (consumerName) => {
51
+ return useRadixGameboardContext(consumerName);
52
+ };
53
+ //#endregion
54
+ //#region src/components/Gameboard/Piece.tsx
55
+ var __dxlog_file$3 = "/__w/dxos/dxos/packages/ui/react-ui-gameboard/src/components/Gameboard/Piece.tsx";
56
+ var PIECE_NAME = "Piece";
57
+ var Piece = memo(({ classNames, Component, piece, bounds, label, onClick }) => {
58
+ const { model, dragging: isDragging, promoting } = useGameboardContext(PIECE_NAME);
59
+ const promotingRef = useDynamicRef(promoting);
60
+ const [dragging, setDragging] = useState(false);
61
+ const [preview, setPreview] = useState();
62
+ const [current, setCurrent] = useState({});
63
+ const ref = useRef(null);
64
+ useEffect(() => {
65
+ if (!model) return;
66
+ const el = ref.current;
67
+ invariant(el, void 0, {
68
+ "~LogMeta": "~LogMeta",
69
+ F: __dxlog_file$3,
70
+ L: 47,
71
+ S: void 0,
72
+ A: ["el", ""]
73
+ });
74
+ return draggable({
75
+ element: el,
76
+ getInitialData: () => ({ piece }),
77
+ onGenerateDragPreview: ({ nativeSetDragImage, source }) => {
78
+ log("onGenerateDragPreview", { source: source.data }, {
79
+ "~LogMeta": "~LogMeta",
80
+ F: __dxlog_file$3,
81
+ L: 53,
82
+ S: void 0
83
+ });
84
+ setCustomNativeDragPreview({
85
+ getOffset: centerUnderPointer,
86
+ render: ({ container }) => {
87
+ setPreview(container);
88
+ const { width, height } = el.getBoundingClientRect();
89
+ container.style.width = width + "px";
90
+ container.style.height = height + "px";
91
+ return () => {
92
+ setPreview(void 0);
93
+ };
94
+ },
95
+ nativeSetDragImage
96
+ });
97
+ },
98
+ canDrag: () => !promotingRef.current && !model.readonly && model.turn === piece.side,
99
+ onDragStart: () => setDragging(true),
100
+ onDrop: ({ location: { current } }) => {
101
+ try {
102
+ const location = current.dropTargets[0]?.data.location;
103
+ if (isLocation(location)) setCurrent((current) => ({
104
+ ...current,
105
+ location
106
+ }));
107
+ } catch {}
108
+ setDragging(false);
109
+ }
110
+ });
111
+ }, [model, piece]);
112
+ useEffect(() => {
113
+ requestAnimationFrame(() => {
114
+ if (!ref.current || !bounds) return;
115
+ if (!current.location || !isEqualLocation(current.location, piece.location)) {
116
+ ref.current.style.transition = "top 250ms ease-out, left 250ms ease-out";
117
+ ref.current.style.top = bounds.top + "px";
118
+ ref.current.style.left = bounds.left + "px";
119
+ setCurrent({
120
+ location: piece.location,
121
+ bounds
122
+ });
123
+ } else if (current.bounds !== bounds) {
124
+ ref.current.style.transition = "none";
125
+ ref.current.style.top = bounds.top + "px";
126
+ ref.current.style.left = bounds.left + "px";
127
+ setCurrent({
128
+ location: piece.location,
129
+ bounds
130
+ });
131
+ }
132
+ });
133
+ }, [
134
+ current,
135
+ piece.location,
136
+ bounds
137
+ ]);
138
+ return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs("div", {
139
+ ref,
140
+ style: {
141
+ width: bounds?.width,
142
+ height: bounds?.height
143
+ },
144
+ className: mx("absolute", classNames, dragging && "opacity-20", isDragging && "pointer-events-none"),
145
+ onClick,
146
+ children: [/* @__PURE__ */ jsx(Component, { className: "grow" }), label && /* @__PURE__ */ jsx("div", {
147
+ className: "absolute inset-1 text-xs text-black",
148
+ children: label
149
+ })]
150
+ }), preview && createPortal(/* @__PURE__ */ jsx("div", {
151
+ className: mx(classNames),
152
+ children: /* @__PURE__ */ jsx(Component, { className: "grow" })
153
+ }), preview)] });
154
+ });
155
+ Piece.displayName = PIECE_NAME;
156
+ //#endregion
157
+ //#region src/components/Gameboard/Square.tsx
158
+ var __dxlog_file$2 = "/__w/dxos/dxos/packages/ui/react-ui-gameboard/src/components/Gameboard/Square.tsx";
159
+ var SQUARE_NAME = "Square";
160
+ var Square = memo(({ location, bounds, label, classNames }) => {
161
+ const ref = useRef(null);
162
+ const [state, setState] = useState("idle");
163
+ const { model } = useGameboardContext(SQUARE_NAME);
164
+ useEffect(() => {
165
+ const el = ref.current;
166
+ invariant(el, void 0, {
167
+ "~LogMeta": "~LogMeta",
168
+ F: __dxlog_file$2,
169
+ L: 34,
170
+ S: void 0,
171
+ A: ["el", ""]
172
+ });
173
+ return dropTargetForElements({
174
+ element: el,
175
+ getData: () => ({ location }),
176
+ canDrop: ({ source }) => {
177
+ log("canDrop", { source: source.data }, {
178
+ "~LogMeta": "~LogMeta",
179
+ F: __dxlog_file$2,
180
+ L: 40,
181
+ S: void 0
182
+ });
183
+ return true;
184
+ },
185
+ onDragEnter: ({ source }) => {
186
+ log("onDragEnter", { source: source.data }, {
187
+ "~LogMeta": "~LogMeta",
188
+ F: __dxlog_file$2,
189
+ L: 44,
190
+ S: void 0
191
+ });
192
+ const piece = source.data.piece;
193
+ if (!isPiece(piece)) return;
194
+ if (model?.isValidMove({
195
+ from: piece.location,
196
+ to: location,
197
+ piece: piece.type
198
+ })) setState("validMove");
199
+ else setState("invalidMove");
200
+ },
201
+ onDragLeave: () => setState("idle"),
202
+ onDrop: () => setState("idle")
203
+ });
204
+ }, [model, location]);
205
+ return /* @__PURE__ */ jsx("div", {
206
+ ref,
207
+ style: bounds,
208
+ className: mx("absolute flex justify-center items-center border-2 box-border select-none", classNames, state === "validMove" ? "border-neutral-800" : "border-transparent"),
209
+ children: label && /* @__PURE__ */ jsx("div", {
210
+ className: mx("absolute bottom-1 left-1 text-xs text-neutral-500"),
211
+ children: label
212
+ })
213
+ });
214
+ });
215
+ Square.displayName = SQUARE_NAME;
216
+ //#endregion
217
+ //#region src/components/Gameboard/Gameboard.tsx
218
+ var __dxlog_file$1 = "/__w/dxos/dxos/packages/ui/react-ui-gameboard/src/components/Gameboard/Gameboard.tsx";
219
+ /**
220
+ * Generic board container.
221
+ */
222
+ var GameboardRoot = ({ children, model, onDrop }) => {
223
+ const [dragging, setDragging] = useState(false);
224
+ const [promoting, setPromoting] = useState();
225
+ const handlePromotion = useCallback((move) => {
226
+ log("onPromotion", { move }, {
227
+ "~LogMeta": "~LogMeta",
228
+ F: __dxlog_file$1,
229
+ L: 34,
230
+ S: void 0
231
+ });
232
+ setPromoting(void 0);
233
+ onDrop?.(move);
234
+ }, []);
235
+ useEffect(() => {
236
+ if (!model) return;
237
+ return monitorForElements({
238
+ onDragStart: ({ source }) => {
239
+ log("onDragStart", { source }, {
240
+ "~LogMeta": "~LogMeta",
241
+ F: __dxlog_file$1,
242
+ L: 47,
243
+ S: void 0
244
+ });
245
+ setDragging(true);
246
+ },
247
+ onDrop: ({ source, location }) => {
248
+ log("onDrop", {
249
+ source,
250
+ location
251
+ }, {
252
+ "~LogMeta": "~LogMeta",
253
+ F: __dxlog_file$1,
254
+ L: 51,
255
+ S: void 0
256
+ });
257
+ const target = location.current.dropTargets[0];
258
+ if (!target) return;
259
+ const targetLocation = target.data.location;
260
+ const piece = source.data.piece;
261
+ if (!isLocation(targetLocation) || !isPiece(piece)) return;
262
+ const move = {
263
+ from: piece.location,
264
+ to: targetLocation,
265
+ piece: piece.type
266
+ };
267
+ if (model.isValidMove(move)) if (model.canPromote?.(move)) setPromoting({
268
+ ...piece,
269
+ location: targetLocation
270
+ });
271
+ else onDrop?.(move);
272
+ setDragging(false);
273
+ }
274
+ });
275
+ }, [model]);
276
+ return /* @__PURE__ */ jsx(GameboardContextProvider, {
277
+ model,
278
+ dragging,
279
+ promoting,
280
+ onPromotion: handlePromotion,
281
+ children
282
+ });
283
+ };
284
+ GameboardRoot.displayName = "Gameboard.Root";
285
+ var GameboardContent = forwardRef(({ children, classNames, grow, contain }, forwardedRef) => {
286
+ return /* @__PURE__ */ jsx("div", {
287
+ className: mx(grow && "dx-size-container dx-expander grid place-content-center", classNames),
288
+ ref: forwardedRef,
289
+ children: contain ? /* @__PURE__ */ jsx("div", {
290
+ className: "w-[min(100cqw,100cqh)] h-[min(100cqw,100cqh)]",
291
+ children
292
+ }) : children
293
+ });
294
+ });
295
+ GameboardContent.displayName = "Gameboard.Content";
296
+ var Gameboard = {
297
+ Root: GameboardRoot,
298
+ Content: GameboardContent,
299
+ Piece,
300
+ Square
301
+ };
302
+ //#endregion
303
+ //#region src/gen/pieces/chess/alpha/bB.tsx
304
+ var SvgBB = (props) => /* @__PURE__ */ jsxs("svg", {
305
+ xmlns: "http://www.w3.org/2000/svg",
306
+ viewBox: "0 0 2048 2048",
307
+ ...props,
308
+ children: [/* @__PURE__ */ jsx("path", {
309
+ fill: "#f9f9f9",
310
+ d: "M732 1290 628 993l386-417 421 375-132 380 49 152-658-9z"
311
+ }), /* @__PURE__ */ jsx("path", {
312
+ fill: "#101010",
313
+ 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"
314
+ })]
315
+ });
316
+ //#endregion
317
+ //#region src/gen/pieces/chess/alpha/bK.tsx
318
+ var SvgBK = (props) => /* @__PURE__ */ jsxs("svg", {
319
+ xmlns: "http://www.w3.org/2000/svg",
320
+ viewBox: "0 0 2048 2048",
321
+ ...props,
322
+ children: [/* @__PURE__ */ jsx("path", {
323
+ fill: "#f9f9f9",
324
+ 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"
325
+ }), /* @__PURE__ */ jsx("path", {
326
+ fill: "#101010",
327
+ 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"
328
+ })]
329
+ });
330
+ //#endregion
331
+ //#region src/gen/pieces/chess/alpha/bN.tsx
332
+ var SvgBN = (props) => /* @__PURE__ */ jsxs("svg", {
333
+ xmlns: "http://www.w3.org/2000/svg",
334
+ viewBox: "0 0 2048 2048",
335
+ ...props,
336
+ children: [/* @__PURE__ */ jsx("path", {
337
+ fill: "#f9f9f9",
338
+ 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"
339
+ }), /* @__PURE__ */ jsx("path", {
340
+ fill: "#101010",
341
+ 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"
342
+ })]
343
+ });
344
+ //#endregion
345
+ //#region src/gen/pieces/chess/alpha/bP.tsx
346
+ var SvgBP = (props) => /* @__PURE__ */ jsx("svg", {
347
+ xmlns: "http://www.w3.org/2000/svg",
348
+ viewBox: "0 0 2048 2048",
349
+ ...props,
350
+ children: /* @__PURE__ */ jsx("path", {
351
+ fill: "#101010",
352
+ 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"
353
+ })
354
+ });
355
+ //#endregion
356
+ //#region src/gen/pieces/chess/alpha/bQ.tsx
357
+ var SvgBQ = (props) => /* @__PURE__ */ jsxs("svg", {
358
+ xmlns: "http://www.w3.org/2000/svg",
359
+ viewBox: "0 0 2048 2048",
360
+ ...props,
361
+ children: [/* @__PURE__ */ jsx("path", {
362
+ fill: "#f9f9f9",
363
+ d: "m520 1801.8 41.5-448.7 474-128.9 458 133.5 34.4 446.4z"
364
+ }), /* @__PURE__ */ jsx("path", {
365
+ fill: "#101010",
366
+ 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"
367
+ })]
368
+ });
369
+ //#endregion
370
+ //#region src/gen/pieces/chess/alpha/bR.tsx
371
+ var SvgBR = (props) => /* @__PURE__ */ jsxs("svg", {
372
+ xmlns: "http://www.w3.org/2000/svg",
373
+ viewBox: "0 0 2048 2048",
374
+ ...props,
375
+ children: [/* @__PURE__ */ jsx("path", {
376
+ fill: "#f9f9f9",
377
+ d: "m674 732-76 807 851 14-75-833z"
378
+ }), /* @__PURE__ */ jsx("path", {
379
+ fill: "#101010",
380
+ 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"
381
+ })]
382
+ });
383
+ //#endregion
384
+ //#region src/gen/pieces/chess/alpha/wB.tsx
385
+ var SvgWB = (props) => /* @__PURE__ */ jsxs("svg", {
386
+ xmlns: "http://www.w3.org/2000/svg",
387
+ viewBox: "0 0 2048 2048",
388
+ ...props,
389
+ children: [/* @__PURE__ */ jsx("path", {
390
+ fill: "#f9f9f9",
391
+ 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"
392
+ }), /* @__PURE__ */ jsx("path", {
393
+ fill: "#101010",
394
+ 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"
395
+ })]
396
+ });
397
+ //#endregion
398
+ //#region src/gen/pieces/chess/alpha/wK.tsx
399
+ var SvgWK = (props) => /* @__PURE__ */ jsxs("svg", {
400
+ xmlns: "http://www.w3.org/2000/svg",
401
+ viewBox: "0 0 2048 2048",
402
+ ...props,
403
+ children: [/* @__PURE__ */ jsx("path", {
404
+ fill: "#f9f9f9",
405
+ 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"
406
+ }), /* @__PURE__ */ jsx("path", {
407
+ fill: "#101010",
408
+ 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"
409
+ })]
410
+ });
411
+ //#endregion
412
+ //#region src/gen/pieces/chess/alpha/wN.tsx
413
+ var SvgWN = (props) => /* @__PURE__ */ jsxs("svg", {
414
+ xmlns: "http://www.w3.org/2000/svg",
415
+ viewBox: "0 0 2048 2048",
416
+ ...props,
417
+ children: [/* @__PURE__ */ jsx("path", {
418
+ fill: "#f9f9f9",
419
+ 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"
420
+ }), /* @__PURE__ */ jsx("path", {
421
+ fill: "#101010",
422
+ 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"
423
+ })]
424
+ });
425
+ //#endregion
426
+ //#region src/gen/pieces/chess/alpha/wP.tsx
427
+ var SvgWP = (props) => /* @__PURE__ */ jsxs("svg", {
428
+ xmlns: "http://www.w3.org/2000/svg",
429
+ viewBox: "0 0 2048 2048",
430
+ ...props,
431
+ children: [/* @__PURE__ */ jsx("path", {
432
+ fill: "#f9f9f9",
433
+ 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"
434
+ }), /* @__PURE__ */ jsx("path", {
435
+ fill: "#101010",
436
+ 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"
437
+ })]
438
+ });
439
+ //#endregion
440
+ //#region src/gen/pieces/chess/alpha/wQ.tsx
441
+ var SvgWQ = (props) => /* @__PURE__ */ jsxs("svg", {
442
+ xmlns: "http://www.w3.org/2000/svg",
443
+ viewBox: "0 0 2048 2048",
444
+ ...props,
445
+ children: [/* @__PURE__ */ jsx("path", {
446
+ fill: "#f9f9f9",
447
+ 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"
448
+ }), /* @__PURE__ */ jsx("path", {
449
+ fill: "#101010",
450
+ 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"
451
+ })]
452
+ });
453
+ //#endregion
454
+ //#region src/gen/pieces/chess/alpha/wR.tsx
455
+ var SvgWR = (props) => /* @__PURE__ */ jsxs("svg", {
456
+ xmlns: "http://www.w3.org/2000/svg",
457
+ viewBox: "0 0 2048 2048",
458
+ ...props,
459
+ children: [/* @__PURE__ */ jsx("path", {
460
+ fill: "#f9f9f9",
461
+ 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"
462
+ }), /* @__PURE__ */ jsx("path", {
463
+ fill: "#101010",
464
+ 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"
465
+ })]
466
+ });
467
+ //#endregion
468
+ //#region src/gen/pieces/chess/alpha/index.ts
469
+ var alpha_exports = /* @__PURE__ */ __exportAll({
470
+ BB: () => SvgBB,
471
+ BK: () => SvgBK,
472
+ BN: () => SvgBN,
473
+ BP: () => SvgBP,
474
+ BQ: () => SvgBQ,
475
+ BR: () => SvgBR,
476
+ WB: () => SvgWB,
477
+ WK: () => SvgWK,
478
+ WN: () => SvgWN,
479
+ WP: () => SvgWP,
480
+ WQ: () => SvgWQ,
481
+ WR: () => SvgWR
482
+ });
483
+ //#endregion
484
+ //#region src/components/Chessboard/chess.ts
485
+ var __dxlog_file = "/__w/dxos/dxos/packages/ui/react-ui-gameboard/src/components/Chessboard/chess.ts";
486
+ var ChessPieces = alpha_exports;
487
+ var posToLocation = (pos) => {
488
+ const col = pos.charCodeAt(0) - "a".charCodeAt(0);
489
+ return [parseInt(pos[1]) - 1, col];
490
+ };
491
+ var locationToPos = ([row, col]) => {
492
+ return String.fromCharCode(col + "a".charCodeAt(0)) + (row + 1);
493
+ };
494
+ var getRawPgn = (pgn) => {
495
+ return pgn.replace(/\[.*?\]/g, "").trim();
496
+ };
497
+ var boardStyles = {
498
+ neutral: {
499
+ black: "bg-neutral-50",
500
+ white: "bg-neutral-200",
501
+ promotion: "bg-neutral-200 hover:bg-neutral-300 opacity-70 hover:opacity-100"
502
+ },
503
+ original: {
504
+ black: "bg-[#6C95B9]",
505
+ white: "bg-[#CCD3DB]",
506
+ promotion: "duration-500 bg-[#CCD3DB] opacity-70 hover:opacity-100"
507
+ },
508
+ blue: {
509
+ black: "bg-[#608BC1]",
510
+ white: "bg-[#CBDCEB]",
511
+ promotion: "duration-500 bg-[#CBDCEB] opacity-70 hover:opacity-100"
512
+ },
513
+ green: {
514
+ black: "bg-[#8EB486]",
515
+ white: "bg-[#FDF7F4]",
516
+ promotion: "duration-500 bg-[#FDF7F4] opacity-70 hover:opacity-100"
517
+ }
518
+ }.original;
519
+ var getSquareColor = ([row, col]) => {
520
+ return (col + row) % 2 === 0 ? boardStyles.black : boardStyles.white;
521
+ };
522
+ var createChess = (pgn) => {
523
+ const chess = new Chess();
524
+ if (pgn) try {
525
+ chess.loadPgn(pgn);
526
+ } catch {
527
+ log.warn(pgn, void 0, {
528
+ "~LogMeta": "~LogMeta",
529
+ F: __dxlog_file,
530
+ L: 76,
531
+ S: void 0
532
+ });
533
+ }
534
+ return chess;
535
+ };
536
+ /**
537
+ * Chess model.
538
+ */
539
+ var ChessModel = class {
540
+ _registry;
541
+ _chess = new Chess();
542
+ _pieces = Atom.make({});
543
+ _moveIndex = Atom.make(0);
544
+ constructor(_registry, pgn) {
545
+ this._registry = _registry;
546
+ this.update(pgn);
547
+ }
548
+ get readonly() {
549
+ return this._registry.get(this._moveIndex) !== this._chess.history().length;
550
+ }
551
+ get turn() {
552
+ return this._chess.turn() === "w" ? "white" : "black";
553
+ }
554
+ get game() {
555
+ return this._chess;
556
+ }
557
+ get pieces() {
558
+ return this._pieces;
559
+ }
560
+ get moveIndex() {
561
+ return this._moveIndex;
562
+ }
563
+ get fen() {
564
+ return this._chess.fen();
565
+ }
566
+ /**
567
+ * PGN with headers.
568
+ *
569
+ * [Event "?"]
570
+ * [Site "?"]
571
+ * [Date "2025.08.05"]
572
+ * [Round "?"]
573
+ * [White "?"]
574
+ * [Black "?"]
575
+ * [Result "*"]
576
+ */
577
+ get pgn() {
578
+ return getRawPgn(this._chess.pgn());
579
+ }
580
+ setMoveIndex(index) {
581
+ const temp = new Chess();
582
+ const history = this._chess.history({ verbose: true });
583
+ for (let i = 0; i < index && i < history.length; i++) temp.move(history[i]);
584
+ this._updateBoard(temp);
585
+ }
586
+ update(pgn = "") {
587
+ const previous = this._chess.history();
588
+ try {
589
+ this._chess.loadPgn(pgn);
590
+ this._chess.setHeader("Date", createDate());
591
+ this._chess.setHeader("Site", "dxos.org");
592
+ } catch {}
593
+ if (!isValidNextMove(previous, this._chess.history())) this._registry.set(this._pieces, {});
594
+ this._updateBoard(this._chess);
595
+ }
596
+ isValidMove(move) {
597
+ return tryMove(new Chess(this._chess.fen()), move) !== null;
598
+ }
599
+ canPromote(move) {
600
+ const isPawnMove = move.piece === "BP" || move.piece === "WP";
601
+ const isToLastRank = move.to[0] === 0 || move.to[0] === 7;
602
+ return isPawnMove && isToLastRank;
603
+ }
604
+ makeMove(move) {
605
+ if (!tryMove(this._chess, move)) return false;
606
+ this._updateBoard(this._chess);
607
+ return true;
608
+ }
609
+ makeRandomMove() {
610
+ const moves = this._chess.moves();
611
+ if (!moves.length) return false;
612
+ const move = moves[Math.floor(Math.random() * moves.length)];
613
+ this._chess.move(move);
614
+ this._updateBoard(this._chess);
615
+ return true;
616
+ }
617
+ /**
618
+ * Update pieces preserving identity.
619
+ */
620
+ _updateBoard(chess) {
621
+ this._registry.set(this._pieces, createPieceMap(chess));
622
+ this._registry.set(this._moveIndex, chess.history().length);
623
+ }
624
+ };
625
+ /**
626
+ * Attempt move.
627
+ */
628
+ var tryMove = (chess, move) => {
629
+ const from = locationToPos(move.from);
630
+ const to = locationToPos(move.to);
631
+ try {
632
+ const promotion = move.promotion ? move.promotion[1].toLowerCase() : "q";
633
+ chess.move({
634
+ from,
635
+ to,
636
+ promotion
637
+ }, { strict: false });
638
+ return chess;
639
+ } catch {
640
+ return null;
641
+ }
642
+ };
643
+ var isValidNextMove = (previous, current) => {
644
+ if (current.length > previous.length + 1) return false;
645
+ for (let i = 0; i < previous.length; i++) if (previous[i] !== current[i]) return false;
646
+ return true;
647
+ };
648
+ /**
649
+ * Starting from a new game, assign piece IDs based on their starting position.
650
+ * Then iterate through the history of the provided game and update the piece map.
651
+ */
652
+ var createPieceMap = (chess) => {
653
+ const temp = new Chess();
654
+ let pieces = _createPieceMap(temp);
655
+ const history = chess.history({ verbose: true });
656
+ for (let i = 0; i < history.length; i++) {
657
+ const move = history[i];
658
+ temp.move(move);
659
+ pieces = _diffPieces(pieces, _createPieceMap(temp));
660
+ const test = /* @__PURE__ */ new Set();
661
+ Object.values(pieces).forEach((piece) => {
662
+ invariant(!test.has(piece.id), "Duplicate: " + piece.id, {
663
+ "~LogMeta": "~LogMeta",
664
+ F: __dxlog_file,
665
+ L: 255,
666
+ S: void 0,
667
+ A: ["!test.has(piece.id)", "'Duplicate: ' + piece.id"]
668
+ });
669
+ test.add(piece.id);
670
+ });
671
+ }
672
+ return pieces;
673
+ };
674
+ /**
675
+ * Create a map of pieces from the board positions; assign each piece the ID of the current square.
676
+ */
677
+ var _createPieceMap = (chess) => {
678
+ const pieces = {};
679
+ chess.board().flatMap((row) => row.forEach((record) => {
680
+ if (!record) return;
681
+ const { square, type, color } = record;
682
+ const pieceType = `${color.toUpperCase()}${type.toUpperCase()}`;
683
+ const location = posToLocation(square);
684
+ pieces[locationToString(location)] = {
685
+ id: `${square}-${pieceType}`,
686
+ type: pieceType,
687
+ side: color === "w" ? "white" : "black",
688
+ location
689
+ };
690
+ }));
691
+ return pieces;
692
+ };
693
+ /**
694
+ * Preserve the original piece objects (and IDs).
695
+ */
696
+ var _diffPieces = (before, after) => {
697
+ const difference = {
698
+ removed: {},
699
+ added: {}
700
+ };
701
+ Object.keys(before).forEach((square) => {
702
+ if (after[square]?.type !== before[square]?.type) difference.removed[square] = before[square];
703
+ });
704
+ Object.keys(after).forEach((square) => {
705
+ if (before[square]?.type !== after[square]?.type) difference.added[square] = after[square];
706
+ else after[square] = before[square];
707
+ });
708
+ for (const piece of Object.values(difference.added)) {
709
+ const previous = Object.values(difference.removed).find((p) => p.type === piece.type);
710
+ if (previous) piece.id = previous.id;
711
+ }
712
+ return after;
713
+ };
714
+ var createDate = (date = /* @__PURE__ */ new Date()) => date.toISOString().slice(0, 10).replace(/-/g, ".");
715
+ //#endregion
716
+ //#region src/components/Chessboard/Chessboard.tsx
717
+ /** Fallback atom for when model is undefined. */
718
+ var EMPTY_PIECES_ATOM = Atom$1.make({});
719
+ /**
720
+ * Chessboard layout.
721
+ */
722
+ var CHESSBOARD_NAME = "Chessboard";
723
+ var ChessboardComponent = forwardRef(({ classNames, orientation, showLabels, debug, rows = 8, cols = 8 }, forwardedRef) => {
724
+ const targetRef = useForwardedRef(forwardedRef);
725
+ const { width, height } = useResizeDetector({
726
+ targetRef,
727
+ refreshRate: 200
728
+ });
729
+ const { model, promoting, onPromotion } = useGameboardContext(CHESSBOARD_NAME);
730
+ const pieces = useAtomValue(model?.pieces ?? EMPTY_PIECES_ATOM);
731
+ const squares = useMemo(() => {
732
+ return Array.from({ length: rows }, (_, i) => orientation === "black" ? i : rows - 1 - i).flatMap((row) => Array.from({ length: cols }).map((_, col) => [row, col]));
733
+ }, [
734
+ orientation,
735
+ rows,
736
+ cols
737
+ ]);
738
+ const layout = useMemo(() => {
739
+ return squares.map((location) => {
740
+ return /* @__PURE__ */ jsx("div", { "data-location": locationToString(location) }, locationToString(location));
741
+ });
742
+ }, [squares]);
743
+ const [grid, setGrid] = useState({});
744
+ const gridRef = useRef(null);
745
+ useEffect(() => {
746
+ setGrid(squares.reduce((acc, location) => {
747
+ const square = getSquareLocation(gridRef.current, location);
748
+ const bounds = getRelativeBounds(gridRef.current, square);
749
+ return {
750
+ ...acc,
751
+ [locationToString(location)]: bounds
752
+ };
753
+ }, {}));
754
+ }, [
755
+ squares,
756
+ width,
757
+ height
758
+ ]);
759
+ const positions = useMemo(() => {
760
+ if (!gridRef.current) return [];
761
+ return Object.values(pieces).map((piece) => {
762
+ if (piece.id === promoting?.id) return null;
763
+ return {
764
+ piece,
765
+ bounds: grid[locationToString(piece.location)]
766
+ };
767
+ }).filter(isNonNullable);
768
+ }, [
769
+ grid,
770
+ pieces,
771
+ promoting
772
+ ]);
773
+ return /* @__PURE__ */ jsxs("div", {
774
+ ref: targetRef,
775
+ tabIndex: 0,
776
+ className: mx("dx-expander relative outline-hidden", classNames),
777
+ children: [
778
+ /* @__PURE__ */ jsx("div", {
779
+ ref: gridRef,
780
+ className: "grid grid-rows-8 grid-cols-8 aspect-square select-none",
781
+ children: layout
782
+ }),
783
+ /* @__PURE__ */ jsx("div", { children: squares.map((location) => /* @__PURE__ */ jsx(Gameboard.Square, {
784
+ location,
785
+ label: showLabels ? locationToPos(location) : void 0,
786
+ bounds: grid[locationToString(location)],
787
+ classNames: getSquareColor(location)
788
+ }, locationToString(location))) }),
789
+ /* @__PURE__ */ jsx("div", {
790
+ className: mx(promoting && "opacity-50"),
791
+ children: positions.map(({ bounds, piece }) => /* @__PURE__ */ jsx(Gameboard.Piece, {
792
+ piece,
793
+ bounds,
794
+ label: debug ? piece.id : void 0,
795
+ orientation,
796
+ Component: ChessPieces[piece.type]
797
+ }, piece.id))
798
+ }),
799
+ promoting && /* @__PURE__ */ jsx(PromotionSelector, {
800
+ grid,
801
+ piece: promoting,
802
+ onSelect: (piece) => {
803
+ onPromotion({
804
+ from: Object.values(pieces).find((p) => p.id === promoting.id).location,
805
+ to: piece.location,
806
+ piece: promoting.type,
807
+ promotion: piece.type
808
+ });
809
+ }
810
+ })
811
+ ]
812
+ });
813
+ });
814
+ ChessboardComponent.displayName = CHESSBOARD_NAME;
815
+ var Chessboard = memo(ChessboardComponent);
816
+ var PromotionSelector = ({ grid, piece, onSelect }) => {
817
+ const positions = [
818
+ "Q",
819
+ "N",
820
+ "R",
821
+ "B"
822
+ ].map((pieceType, i) => {
823
+ const location = [piece.location[0] + (piece.location[0] === 0 ? i : -i), piece.location[1]];
824
+ return {
825
+ piece: {
826
+ id: `promotion-${pieceType}`,
827
+ type: (piece.side === "black" ? "B" : "W") + pieceType,
828
+ side: piece.side,
829
+ location
830
+ },
831
+ bounds: grid[locationToString(location)]
832
+ };
833
+ });
834
+ const handleSelect = (selected) => {
835
+ onSelect({
836
+ ...piece,
837
+ type: selected.type
838
+ });
839
+ };
840
+ return /* @__PURE__ */ jsx(Fragment, { children: positions.map(({ piece, bounds }) => /* @__PURE__ */ jsx(Gameboard.Piece, {
841
+ classNames: mx("border-2 border-neutral-700 rounded-full", boardStyles.promotion),
842
+ piece,
843
+ bounds,
844
+ Component: ChessPieces[piece.type],
845
+ onClick: () => handleSelect(piece)
846
+ }, piece.id)) });
847
+ };
848
+ var getSquareLocation = (container, location) => {
849
+ return container.querySelector(`[data-location="${locationToString(location)}"]`);
850
+ };
851
+ //#endregion
852
+ export { ChessModel, ChessPieces, Chessboard, Gameboard, boardStyles, createChess, createPieceMap, getRawPgn, getRelativeBounds, getSquareColor, isEqualLocation, isLocation, isPiece, locationToPos, locationToString, posToLocation, stringToLocation, useGameboardContext };
853
+
854
+ //# sourceMappingURL=index.mjs.map