@dxos/react-ui-gameboard 0.7.5-labs.e27f9b9 → 0.7.5-main.e9bb01b
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/browser/index.mjs +44 -147
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +43 -147
- package/dist/lib/node/index.cjs.map +3 -3
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +44 -147
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/Board/Board.d.ts.map +1 -1
- package/dist/types/src/Board/Piece.d.ts.map +1 -1
- package/dist/types/src/Board/context.d.ts +7 -7
- package/dist/types/src/Board/context.d.ts.map +1 -1
- package/dist/types/src/Board/index.d.ts +0 -1
- package/dist/types/src/Board/index.d.ts.map +1 -1
- package/dist/types/src/Board/types.d.ts +2 -6
- package/dist/types/src/Board/types.d.ts.map +1 -1
- package/dist/types/src/Chessboard/Chessboard.d.ts.map +1 -1
- package/dist/types/src/Chessboard/Chessboard.stories.d.ts +0 -2
- package/dist/types/src/Chessboard/Chessboard.stories.d.ts.map +1 -1
- package/dist/types/src/Chessboard/chess.d.ts +9 -5
- package/dist/types/src/Chessboard/chess.d.ts.map +1 -1
- package/package.json +11 -11
- package/src/Board/Board.tsx +6 -24
- package/src/Board/Piece.tsx +9 -10
- package/src/Board/Square.tsx +1 -1
- package/src/Board/context.ts +2 -9
- package/src/Board/index.ts +0 -1
- package/src/Board/types.ts +2 -6
- package/src/Chessboard/Chessboard.stories.tsx +3 -45
- package/src/Chessboard/Chessboard.tsx +9 -76
- package/src/Chessboard/chess.ts +10 -31
package/dist/lib/node/index.cjs
CHANGED
|
@@ -33,10 +33,8 @@ __export(node_exports, {
|
|
|
33
33
|
ChessModel: () => ChessModel,
|
|
34
34
|
ChessPieces: () => ChessPieces,
|
|
35
35
|
Chessboard: () => Chessboard,
|
|
36
|
-
Container: () => Container,
|
|
37
36
|
Piece: () => Piece,
|
|
38
37
|
Square: () => Square,
|
|
39
|
-
boardStyles: () => boardStyles,
|
|
40
38
|
getRelativeBounds: () => getRelativeBounds,
|
|
41
39
|
getSquareColor: () => getSquareColor,
|
|
42
40
|
isEqualLocation: () => isEqualLocation,
|
|
@@ -47,6 +45,7 @@ __export(node_exports, {
|
|
|
47
45
|
mapPieces: () => mapPieces,
|
|
48
46
|
posToLocation: () => posToLocation,
|
|
49
47
|
stringToLocation: () => stringToLocation,
|
|
48
|
+
styles: () => styles,
|
|
50
49
|
useBoardContext: () => useBoardContext
|
|
51
50
|
});
|
|
52
51
|
module.exports = __toCommonJS(node_exports);
|
|
@@ -90,8 +89,6 @@ var React16 = __toESM(require("react"));
|
|
|
90
89
|
var import_react6 = __toESM(require("react"));
|
|
91
90
|
var import_react_resize_detector = require("react-resize-detector");
|
|
92
91
|
var import_react_ui2 = require("@dxos/react-ui");
|
|
93
|
-
var import_react_ui_theme5 = require("@dxos/react-ui-theme");
|
|
94
|
-
var import_util = require("@dxos/util");
|
|
95
92
|
var __defProp2 = Object.defineProperty;
|
|
96
93
|
var __export2 = (target, all) => {
|
|
97
94
|
for (var name in all)
|
|
@@ -128,30 +125,14 @@ var Container = /* @__PURE__ */ (0, import_react3.forwardRef)(({ children, class
|
|
|
128
125
|
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/ui/react-ui-gameboard/src/Board/Board.tsx";
|
|
129
126
|
var Root = ({ children, classNames, model, onDrop }) => {
|
|
130
127
|
const [dragging, setDragging] = (0, import_react2.useState)(false);
|
|
131
|
-
const [promoting, setPromoting] = (0, import_react2.useState)();
|
|
132
|
-
const onPromotion = (0, import_react2.useCallback)((move) => {
|
|
133
|
-
(0, import_log.log)("onPromotion", {
|
|
134
|
-
move
|
|
135
|
-
}, {
|
|
136
|
-
F: __dxlog_file,
|
|
137
|
-
L: 32,
|
|
138
|
-
S: void 0,
|
|
139
|
-
C: (f, a) => f(...a)
|
|
140
|
-
});
|
|
141
|
-
setPromoting(void 0);
|
|
142
|
-
onDrop?.(move);
|
|
143
|
-
}, []);
|
|
144
128
|
(0, import_react2.useEffect)(() => {
|
|
145
|
-
if (!model) {
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
148
129
|
return (0, import_adapter.monitorForElements)({
|
|
149
130
|
onDragStart: ({ source }) => {
|
|
150
131
|
(0, import_log.log)("onDragStart", {
|
|
151
132
|
source
|
|
152
133
|
}, {
|
|
153
134
|
F: __dxlog_file,
|
|
154
|
-
L:
|
|
135
|
+
L: 33,
|
|
155
136
|
S: void 0,
|
|
156
137
|
C: (f, a) => f(...a)
|
|
157
138
|
});
|
|
@@ -163,7 +144,7 @@ var Root = ({ children, classNames, model, onDrop }) => {
|
|
|
163
144
|
location
|
|
164
145
|
}, {
|
|
165
146
|
F: __dxlog_file,
|
|
166
|
-
L:
|
|
147
|
+
L: 37,
|
|
167
148
|
S: void 0,
|
|
168
149
|
C: (f, a) => f(...a)
|
|
169
150
|
});
|
|
@@ -176,31 +157,19 @@ var Root = ({ children, classNames, model, onDrop }) => {
|
|
|
176
157
|
if (!isLocation(targetLocation) || !isPiece(piece)) {
|
|
177
158
|
return;
|
|
178
159
|
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
160
|
+
onDrop?.({
|
|
161
|
+
source: piece.location,
|
|
162
|
+
target: targetLocation,
|
|
182
163
|
piece: piece.type
|
|
183
|
-
};
|
|
184
|
-
if (model.canPromote?.(move)) {
|
|
185
|
-
setPromoting({
|
|
186
|
-
...piece,
|
|
187
|
-
location: targetLocation
|
|
188
|
-
});
|
|
189
|
-
} else {
|
|
190
|
-
onDrop?.(move);
|
|
191
|
-
}
|
|
164
|
+
});
|
|
192
165
|
setDragging(false);
|
|
193
166
|
}
|
|
194
167
|
});
|
|
195
|
-
}, [
|
|
196
|
-
model
|
|
197
|
-
]);
|
|
168
|
+
}, []);
|
|
198
169
|
return /* @__PURE__ */ import_react2.default.createElement(BoardContext.Provider, {
|
|
199
170
|
value: {
|
|
200
171
|
model,
|
|
201
|
-
dragging
|
|
202
|
-
promoting,
|
|
203
|
-
onPromotion
|
|
172
|
+
dragging
|
|
204
173
|
}
|
|
205
174
|
}, /* @__PURE__ */ import_react2.default.createElement(Container, {
|
|
206
175
|
classNames: (0, import_react_ui_theme.mx)("aspect-square", classNames)
|
|
@@ -256,8 +225,8 @@ var Square = /* @__PURE__ */ (0, import_react4.memo)(({ location, bounds, label,
|
|
|
256
225
|
return;
|
|
257
226
|
}
|
|
258
227
|
if (model?.isValidMove({
|
|
259
|
-
|
|
260
|
-
|
|
228
|
+
source: piece.location,
|
|
229
|
+
target: location,
|
|
261
230
|
piece: piece.type
|
|
262
231
|
})) {
|
|
263
232
|
setState("validMove");
|
|
@@ -292,8 +261,7 @@ var Piece = /* @__PURE__ */ (0, import_react5.memo)(({ classNames, piece, orient
|
|
|
292
261
|
Component
|
|
293
262
|
}, Piece.displayName, false);
|
|
294
263
|
const { model } = useBoardContext();
|
|
295
|
-
const { dragging: isDragging
|
|
296
|
-
const promotingRef = (0, import_react_ui.useDynamicRef)(promoting);
|
|
264
|
+
const { dragging: isDragging } = useBoardContext();
|
|
297
265
|
const [dragging, setDragging] = (0, import_react5.useState)(false);
|
|
298
266
|
const [preview, setPreview] = (0, import_react5.useState)();
|
|
299
267
|
const [current, setCurrent] = (0, import_react5.useState)({});
|
|
@@ -302,7 +270,7 @@ var Piece = /* @__PURE__ */ (0, import_react5.memo)(({ classNames, piece, orient
|
|
|
302
270
|
const el = ref.current;
|
|
303
271
|
(0, import_invariant2.invariant)(el, void 0, {
|
|
304
272
|
F: __dxlog_file3,
|
|
305
|
-
L:
|
|
273
|
+
L: 43,
|
|
306
274
|
S: void 0,
|
|
307
275
|
A: [
|
|
308
276
|
"el",
|
|
@@ -319,7 +287,7 @@ var Piece = /* @__PURE__ */ (0, import_react5.memo)(({ classNames, piece, orient
|
|
|
319
287
|
source: source.data
|
|
320
288
|
}, {
|
|
321
289
|
F: __dxlog_file3,
|
|
322
|
-
L:
|
|
290
|
+
L: 49,
|
|
323
291
|
S: void 0,
|
|
324
292
|
C: (f, a) => f(...a)
|
|
325
293
|
});
|
|
@@ -341,14 +309,15 @@ var Piece = /* @__PURE__ */ (0, import_react5.memo)(({ classNames, piece, orient
|
|
|
341
309
|
nativeSetDragImage
|
|
342
310
|
});
|
|
343
311
|
},
|
|
344
|
-
canDrag: () =>
|
|
312
|
+
canDrag: () => model?.turn === piece.side,
|
|
345
313
|
onDragStart: () => setDragging(true),
|
|
346
|
-
onDrop: ({ location
|
|
347
|
-
const
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
314
|
+
onDrop: ({ location }) => {
|
|
315
|
+
const drop = location.current.dropTargets[0].data;
|
|
316
|
+
const loc = drop.location;
|
|
317
|
+
if (isLocation(loc)) {
|
|
318
|
+
setCurrent((current2) => ({
|
|
319
|
+
...current2,
|
|
320
|
+
location: loc
|
|
352
321
|
}));
|
|
353
322
|
}
|
|
354
323
|
setDragging(false);
|
|
@@ -581,47 +550,33 @@ var locationToPos = ([row, col]) => {
|
|
|
581
550
|
};
|
|
582
551
|
var styles = {
|
|
583
552
|
neutral: {
|
|
584
|
-
black: "bg-neutral-50",
|
|
585
553
|
white: "bg-neutral-200",
|
|
586
|
-
|
|
587
|
-
},
|
|
588
|
-
original: {
|
|
589
|
-
black: "bg-[#6C95B9]",
|
|
590
|
-
white: "bg-[#CCD3DB]",
|
|
591
|
-
promotion: "duration-500 bg-[#CCD3DB] opacity-70 hover:opacity-100"
|
|
554
|
+
black: "bg-neutral-50"
|
|
592
555
|
},
|
|
593
556
|
blue: {
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
promotion: "duration-500 bg-[#CBDCEB] opacity-70 hover:opacity-100"
|
|
597
|
-
},
|
|
598
|
-
green: {
|
|
599
|
-
black: "bg-[#8EB486]",
|
|
600
|
-
white: "bg-[#FDF7F4]",
|
|
601
|
-
promotion: "duration-500 bg-[#FDF7F4] opacity-70 hover:opacity-100"
|
|
557
|
+
white: "bg-[#ccd3db]",
|
|
558
|
+
black: "bg-[#6c95b9]"
|
|
602
559
|
}
|
|
603
560
|
};
|
|
604
|
-
var boardStyles = styles.original;
|
|
605
561
|
var getSquareColor = ([row, col]) => {
|
|
606
|
-
return (col + row) % 2 === 0 ?
|
|
562
|
+
return (col + row) % 2 === 0 ? styles.blue.white : styles.blue.black;
|
|
607
563
|
};
|
|
608
|
-
var makeMove = (game,
|
|
609
|
-
const
|
|
610
|
-
const
|
|
564
|
+
var makeMove = (game, { source, target }) => {
|
|
565
|
+
const s = locationToPos(source);
|
|
566
|
+
const t = locationToPos(target);
|
|
611
567
|
try {
|
|
612
568
|
(0, import_log4.log)("makeMove", {
|
|
613
|
-
|
|
569
|
+
s,
|
|
570
|
+
t
|
|
614
571
|
}, {
|
|
615
572
|
F: __dxlog_file4,
|
|
616
|
-
L:
|
|
573
|
+
L: 58,
|
|
617
574
|
S: void 0,
|
|
618
575
|
C: (f, a) => f(...a)
|
|
619
576
|
});
|
|
620
|
-
const promotion = move.promotion ? move.promotion[1].toLowerCase() : "q";
|
|
621
577
|
game.move({
|
|
622
|
-
from,
|
|
623
|
-
to
|
|
624
|
-
promotion
|
|
578
|
+
from: s,
|
|
579
|
+
to: t
|
|
625
580
|
}, {
|
|
626
581
|
strict: false
|
|
627
582
|
});
|
|
@@ -655,11 +610,6 @@ var ChessModel = class {
|
|
|
655
610
|
isValidMove(move) {
|
|
656
611
|
return makeMove(new import_chess.Chess(this._game.fen()), move) !== null;
|
|
657
612
|
}
|
|
658
|
-
canPromote(move) {
|
|
659
|
-
const isPawnMove = move.piece === "BP" || move.piece === "WP";
|
|
660
|
-
const isToLastRank = move.to[0] === 0 || move.to[0] === 7;
|
|
661
|
-
return isPawnMove && isToLastRank;
|
|
662
|
-
}
|
|
663
613
|
makeMove(move) {
|
|
664
614
|
const game = makeMove(this._game, move);
|
|
665
615
|
if (!game) {
|
|
@@ -731,7 +681,7 @@ var mapPieces = (before, after) => {
|
|
|
731
681
|
added: Object.keys(difference.added).length
|
|
732
682
|
}, {
|
|
733
683
|
F: __dxlog_file4,
|
|
734
|
-
L:
|
|
684
|
+
L: 184,
|
|
735
685
|
S: void 0,
|
|
736
686
|
C: (f, a) => f(...a)
|
|
737
687
|
});
|
|
@@ -746,7 +696,7 @@ var Chessboard = /* @__PURE__ */ (0, import_react6.memo)(({ orientation, showLab
|
|
|
746
696
|
const { ref: containerRef, width, height } = (0, import_react_resize_detector.useResizeDetector)({
|
|
747
697
|
refreshRate: 200
|
|
748
698
|
});
|
|
749
|
-
const { model
|
|
699
|
+
const { model } = useBoardContext();
|
|
750
700
|
const locations = (0, import_react6.useMemo)(() => {
|
|
751
701
|
return Array.from({
|
|
752
702
|
length: rows
|
|
@@ -792,19 +742,15 @@ var Chessboard = /* @__PURE__ */ (0, import_react6.memo)(({ orientation, showLab
|
|
|
792
742
|
return [];
|
|
793
743
|
}
|
|
794
744
|
return Object.values(model?.pieces.value ?? {}).map((piece) => {
|
|
795
|
-
if (piece.id === promoting?.id) {
|
|
796
|
-
return null;
|
|
797
|
-
}
|
|
798
745
|
const bounds = grid[locationToString(piece.location)];
|
|
799
746
|
return {
|
|
800
|
-
|
|
801
|
-
|
|
747
|
+
bounds,
|
|
748
|
+
piece
|
|
802
749
|
};
|
|
803
|
-
})
|
|
750
|
+
});
|
|
804
751
|
}, [
|
|
805
752
|
grid,
|
|
806
|
-
model?.pieces.value
|
|
807
|
-
promoting
|
|
753
|
+
model?.pieces.value
|
|
808
754
|
]);
|
|
809
755
|
return /* @__PURE__ */ import_react6.default.createElement("div", {
|
|
810
756
|
ref: containerRef,
|
|
@@ -819,7 +765,7 @@ var Chessboard = /* @__PURE__ */ (0, import_react6.memo)(({ orientation, showLab
|
|
|
819
765
|
bounds: grid[locationToString(location)],
|
|
820
766
|
classNames: getSquareColor(location)
|
|
821
767
|
}))), /* @__PURE__ */ import_react6.default.createElement("div", {
|
|
822
|
-
className:
|
|
768
|
+
className: "grow"
|
|
823
769
|
}, positions.map(({ bounds, piece }) => /* @__PURE__ */ import_react6.default.createElement(Piece, {
|
|
824
770
|
key: piece.id,
|
|
825
771
|
piece,
|
|
@@ -827,61 +773,12 @@ var Chessboard = /* @__PURE__ */ (0, import_react6.memo)(({ orientation, showLab
|
|
|
827
773
|
label: debug ? piece.id : void 0,
|
|
828
774
|
orientation,
|
|
829
775
|
Component: ChessPieces[piece.type]
|
|
830
|
-
})))
|
|
831
|
-
grid,
|
|
832
|
-
piece: promoting,
|
|
833
|
-
onSelect: (piece) => {
|
|
834
|
-
onPromotion({
|
|
835
|
-
from: Object.values(model.pieces.value).find((p) => p.id === promoting.id).location,
|
|
836
|
-
to: piece.location,
|
|
837
|
-
piece: promoting.type,
|
|
838
|
-
promotion: piece.type
|
|
839
|
-
});
|
|
840
|
-
}
|
|
841
|
-
})));
|
|
776
|
+
}))));
|
|
842
777
|
});
|
|
843
778
|
Chessboard.displayName = "Chessboard";
|
|
844
779
|
var getSquareLocation = (container, location) => {
|
|
845
780
|
return container.querySelector(`[data-location="${locationToString(location)}"]`);
|
|
846
781
|
};
|
|
847
|
-
var PromotionSelector = ({ grid, piece, onSelect }) => {
|
|
848
|
-
const positions = [
|
|
849
|
-
"Q",
|
|
850
|
-
"N",
|
|
851
|
-
"R",
|
|
852
|
-
"B"
|
|
853
|
-
].map((pieceType, i) => {
|
|
854
|
-
const location = [
|
|
855
|
-
piece.location[0] + (piece.location[0] === 0 ? i : -i),
|
|
856
|
-
piece.location[1]
|
|
857
|
-
];
|
|
858
|
-
return {
|
|
859
|
-
piece: {
|
|
860
|
-
id: `promotion-${pieceType}`,
|
|
861
|
-
type: (piece.side === "black" ? "B" : "W") + pieceType,
|
|
862
|
-
side: piece.side,
|
|
863
|
-
location
|
|
864
|
-
},
|
|
865
|
-
bounds: grid[locationToString(location)]
|
|
866
|
-
};
|
|
867
|
-
});
|
|
868
|
-
const handleSelect = (selected) => {
|
|
869
|
-
onSelect({
|
|
870
|
-
...piece,
|
|
871
|
-
type: selected.type
|
|
872
|
-
});
|
|
873
|
-
};
|
|
874
|
-
return /* @__PURE__ */ import_react6.default.createElement("div", null, positions.map(({ piece: piece2, bounds }) => /* @__PURE__ */ import_react6.default.createElement("div", {
|
|
875
|
-
key: piece2.id,
|
|
876
|
-
style: bounds,
|
|
877
|
-
onClick: () => handleSelect(piece2)
|
|
878
|
-
}, /* @__PURE__ */ import_react6.default.createElement(Piece, {
|
|
879
|
-
piece: piece2,
|
|
880
|
-
bounds,
|
|
881
|
-
Component: ChessPieces[piece2.type],
|
|
882
|
-
classNames: (0, import_react_ui_theme5.mx)("border-2 border-neutral-700 rounded-full", boardStyles.promotion)
|
|
883
|
-
}))));
|
|
884
|
-
};
|
|
885
782
|
// Annotate the CommonJS export names for ESM import in node:
|
|
886
783
|
0 && (module.exports = {
|
|
887
784
|
Board,
|
|
@@ -889,10 +786,8 @@ var PromotionSelector = ({ grid, piece, onSelect }) => {
|
|
|
889
786
|
ChessModel,
|
|
890
787
|
ChessPieces,
|
|
891
788
|
Chessboard,
|
|
892
|
-
Container,
|
|
893
789
|
Piece,
|
|
894
790
|
Square,
|
|
895
|
-
boardStyles,
|
|
896
791
|
getRelativeBounds,
|
|
897
792
|
getSquareColor,
|
|
898
793
|
isEqualLocation,
|
|
@@ -903,6 +798,7 @@ var PromotionSelector = ({ grid, piece, onSelect }) => {
|
|
|
903
798
|
mapPieces,
|
|
904
799
|
posToLocation,
|
|
905
800
|
stringToLocation,
|
|
801
|
+
styles,
|
|
906
802
|
useBoardContext
|
|
907
803
|
});
|
|
908
804
|
//# sourceMappingURL=index.cjs.map
|