@dxos/react-ui-gameboard 0.7.5-labs.35b4b42 → 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
|
@@ -33,7 +33,7 @@ var getRelativeBounds = (container, element) => {
|
|
|
33
33
|
|
|
34
34
|
// packages/ui/react-ui-gameboard/src/Board/Board.tsx
|
|
35
35
|
import { monitorForElements } from "@atlaskit/pragmatic-drag-and-drop/element/adapter";
|
|
36
|
-
import React2, {
|
|
36
|
+
import React2, { useEffect, useState } from "react";
|
|
37
37
|
import { log } from "@dxos/log";
|
|
38
38
|
import { mx as mx2 } from "@dxos/react-ui-theme";
|
|
39
39
|
|
|
@@ -54,30 +54,14 @@ var Container = /* @__PURE__ */ forwardRef(({ children, classNames, style }, for
|
|
|
54
54
|
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/ui/react-ui-gameboard/src/Board/Board.tsx";
|
|
55
55
|
var Root = ({ children, classNames, model, onDrop }) => {
|
|
56
56
|
const [dragging, setDragging] = useState(false);
|
|
57
|
-
const [promoting, setPromoting] = useState();
|
|
58
|
-
const onPromotion = useCallback((move) => {
|
|
59
|
-
log("onPromotion", {
|
|
60
|
-
move
|
|
61
|
-
}, {
|
|
62
|
-
F: __dxlog_file,
|
|
63
|
-
L: 32,
|
|
64
|
-
S: void 0,
|
|
65
|
-
C: (f, a) => f(...a)
|
|
66
|
-
});
|
|
67
|
-
setPromoting(void 0);
|
|
68
|
-
onDrop?.(move);
|
|
69
|
-
}, []);
|
|
70
57
|
useEffect(() => {
|
|
71
|
-
if (!model) {
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
58
|
return monitorForElements({
|
|
75
59
|
onDragStart: ({ source }) => {
|
|
76
60
|
log("onDragStart", {
|
|
77
61
|
source
|
|
78
62
|
}, {
|
|
79
63
|
F: __dxlog_file,
|
|
80
|
-
L:
|
|
64
|
+
L: 33,
|
|
81
65
|
S: void 0,
|
|
82
66
|
C: (f, a) => f(...a)
|
|
83
67
|
});
|
|
@@ -89,7 +73,7 @@ var Root = ({ children, classNames, model, onDrop }) => {
|
|
|
89
73
|
location
|
|
90
74
|
}, {
|
|
91
75
|
F: __dxlog_file,
|
|
92
|
-
L:
|
|
76
|
+
L: 37,
|
|
93
77
|
S: void 0,
|
|
94
78
|
C: (f, a) => f(...a)
|
|
95
79
|
});
|
|
@@ -102,31 +86,19 @@ var Root = ({ children, classNames, model, onDrop }) => {
|
|
|
102
86
|
if (!isLocation(targetLocation) || !isPiece(piece)) {
|
|
103
87
|
return;
|
|
104
88
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
89
|
+
onDrop?.({
|
|
90
|
+
source: piece.location,
|
|
91
|
+
target: targetLocation,
|
|
108
92
|
piece: piece.type
|
|
109
|
-
};
|
|
110
|
-
if (model.canPromote?.(move)) {
|
|
111
|
-
setPromoting({
|
|
112
|
-
...piece,
|
|
113
|
-
location: targetLocation
|
|
114
|
-
});
|
|
115
|
-
} else {
|
|
116
|
-
onDrop?.(move);
|
|
117
|
-
}
|
|
93
|
+
});
|
|
118
94
|
setDragging(false);
|
|
119
95
|
}
|
|
120
96
|
});
|
|
121
|
-
}, [
|
|
122
|
-
model
|
|
123
|
-
]);
|
|
97
|
+
}, []);
|
|
124
98
|
return /* @__PURE__ */ React2.createElement(BoardContext.Provider, {
|
|
125
99
|
value: {
|
|
126
100
|
model,
|
|
127
|
-
dragging
|
|
128
|
-
promoting,
|
|
129
|
-
onPromotion
|
|
101
|
+
dragging
|
|
130
102
|
}
|
|
131
103
|
}, /* @__PURE__ */ React2.createElement(Container, {
|
|
132
104
|
classNames: mx2("aspect-square", classNames)
|
|
@@ -189,8 +161,8 @@ var Square = /* @__PURE__ */ memo(({ location, bounds, label, classNames }) => {
|
|
|
189
161
|
return;
|
|
190
162
|
}
|
|
191
163
|
if (model?.isValidMove({
|
|
192
|
-
|
|
193
|
-
|
|
164
|
+
source: piece.location,
|
|
165
|
+
target: location,
|
|
194
166
|
piece: piece.type
|
|
195
167
|
})) {
|
|
196
168
|
setState("validMove");
|
|
@@ -223,7 +195,7 @@ import React4, { useState as useState3, useRef as useRef2, useEffect as useEffec
|
|
|
223
195
|
import { createPortal } from "react-dom";
|
|
224
196
|
import { invariant as invariant2 } from "@dxos/invariant";
|
|
225
197
|
import { log as log3 } from "@dxos/log";
|
|
226
|
-
import {
|
|
198
|
+
import { useTrackProps } from "@dxos/react-ui";
|
|
227
199
|
import { mx as mx4 } from "@dxos/react-ui-theme";
|
|
228
200
|
var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/ui/react-ui-gameboard/src/Board/Piece.tsx";
|
|
229
201
|
var Piece = /* @__PURE__ */ memo2(({ classNames, piece, orientation, bounds, label, Component }) => {
|
|
@@ -236,8 +208,7 @@ var Piece = /* @__PURE__ */ memo2(({ classNames, piece, orientation, bounds, lab
|
|
|
236
208
|
Component
|
|
237
209
|
}, Piece.displayName, false);
|
|
238
210
|
const { model } = useBoardContext();
|
|
239
|
-
const { dragging: isDragging
|
|
240
|
-
const promotingRef = useDynamicRef(promoting);
|
|
211
|
+
const { dragging: isDragging } = useBoardContext();
|
|
241
212
|
const [dragging, setDragging] = useState3(false);
|
|
242
213
|
const [preview, setPreview] = useState3();
|
|
243
214
|
const [current, setCurrent] = useState3({});
|
|
@@ -246,7 +217,7 @@ var Piece = /* @__PURE__ */ memo2(({ classNames, piece, orientation, bounds, lab
|
|
|
246
217
|
const el = ref.current;
|
|
247
218
|
invariant2(el, void 0, {
|
|
248
219
|
F: __dxlog_file3,
|
|
249
|
-
L:
|
|
220
|
+
L: 43,
|
|
250
221
|
S: void 0,
|
|
251
222
|
A: [
|
|
252
223
|
"el",
|
|
@@ -263,7 +234,7 @@ var Piece = /* @__PURE__ */ memo2(({ classNames, piece, orientation, bounds, lab
|
|
|
263
234
|
source: source.data
|
|
264
235
|
}, {
|
|
265
236
|
F: __dxlog_file3,
|
|
266
|
-
L:
|
|
237
|
+
L: 49,
|
|
267
238
|
S: void 0,
|
|
268
239
|
C: (f, a) => f(...a)
|
|
269
240
|
});
|
|
@@ -285,14 +256,15 @@ var Piece = /* @__PURE__ */ memo2(({ classNames, piece, orientation, bounds, lab
|
|
|
285
256
|
nativeSetDragImage
|
|
286
257
|
});
|
|
287
258
|
},
|
|
288
|
-
canDrag: () =>
|
|
259
|
+
canDrag: () => model?.turn === piece.side,
|
|
289
260
|
onDragStart: () => setDragging(true),
|
|
290
|
-
onDrop: ({ location
|
|
291
|
-
const
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
261
|
+
onDrop: ({ location }) => {
|
|
262
|
+
const drop = location.current.dropTargets[0].data;
|
|
263
|
+
const loc = drop.location;
|
|
264
|
+
if (isLocation(loc)) {
|
|
265
|
+
setCurrent((current2) => ({
|
|
266
|
+
...current2,
|
|
267
|
+
location: loc
|
|
296
268
|
}));
|
|
297
269
|
}
|
|
298
270
|
setDragging(false);
|
|
@@ -570,47 +542,33 @@ var locationToPos = ([row, col]) => {
|
|
|
570
542
|
};
|
|
571
543
|
var styles = {
|
|
572
544
|
neutral: {
|
|
573
|
-
black: "bg-neutral-50",
|
|
574
545
|
white: "bg-neutral-200",
|
|
575
|
-
|
|
576
|
-
},
|
|
577
|
-
original: {
|
|
578
|
-
black: "bg-[#6C95B9]",
|
|
579
|
-
white: "bg-[#CCD3DB]",
|
|
580
|
-
promotion: "duration-500 bg-[#CCD3DB] opacity-70 hover:opacity-100"
|
|
546
|
+
black: "bg-neutral-50"
|
|
581
547
|
},
|
|
582
548
|
blue: {
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
promotion: "duration-500 bg-[#CBDCEB] opacity-70 hover:opacity-100"
|
|
586
|
-
},
|
|
587
|
-
green: {
|
|
588
|
-
black: "bg-[#8EB486]",
|
|
589
|
-
white: "bg-[#FDF7F4]",
|
|
590
|
-
promotion: "duration-500 bg-[#FDF7F4] opacity-70 hover:opacity-100"
|
|
549
|
+
white: "bg-[#ccd3db]",
|
|
550
|
+
black: "bg-[#6c95b9]"
|
|
591
551
|
}
|
|
592
552
|
};
|
|
593
|
-
var boardStyles = styles.original;
|
|
594
553
|
var getSquareColor = ([row, col]) => {
|
|
595
|
-
return (col + row) % 2 === 0 ?
|
|
554
|
+
return (col + row) % 2 === 0 ? styles.blue.white : styles.blue.black;
|
|
596
555
|
};
|
|
597
|
-
var makeMove = (game,
|
|
598
|
-
const
|
|
599
|
-
const
|
|
556
|
+
var makeMove = (game, { source, target }) => {
|
|
557
|
+
const s = locationToPos(source);
|
|
558
|
+
const t = locationToPos(target);
|
|
600
559
|
try {
|
|
601
560
|
log4("makeMove", {
|
|
602
|
-
|
|
561
|
+
s,
|
|
562
|
+
t
|
|
603
563
|
}, {
|
|
604
564
|
F: __dxlog_file4,
|
|
605
|
-
L:
|
|
565
|
+
L: 58,
|
|
606
566
|
S: void 0,
|
|
607
567
|
C: (f, a) => f(...a)
|
|
608
568
|
});
|
|
609
|
-
const promotion = move.promotion ? move.promotion[1].toLowerCase() : "q";
|
|
610
569
|
game.move({
|
|
611
|
-
from,
|
|
612
|
-
to
|
|
613
|
-
promotion
|
|
570
|
+
from: s,
|
|
571
|
+
to: t
|
|
614
572
|
}, {
|
|
615
573
|
strict: false
|
|
616
574
|
});
|
|
@@ -644,11 +602,6 @@ var ChessModel = class {
|
|
|
644
602
|
isValidMove(move) {
|
|
645
603
|
return makeMove(new Chess(this._game.fen()), move) !== null;
|
|
646
604
|
}
|
|
647
|
-
canPromote(move) {
|
|
648
|
-
const isPawnMove = move.piece === "BP" || move.piece === "WP";
|
|
649
|
-
const isToLastRank = move.to[0] === 0 || move.to[0] === 7;
|
|
650
|
-
return isPawnMove && isToLastRank;
|
|
651
|
-
}
|
|
652
605
|
makeMove(move) {
|
|
653
606
|
const game = makeMove(this._game, move);
|
|
654
607
|
if (!game) {
|
|
@@ -720,7 +673,7 @@ var mapPieces = (before, after) => {
|
|
|
720
673
|
added: Object.keys(difference.added).length
|
|
721
674
|
}, {
|
|
722
675
|
F: __dxlog_file4,
|
|
723
|
-
L:
|
|
676
|
+
L: 184,
|
|
724
677
|
S: void 0,
|
|
725
678
|
C: (f, a) => f(...a)
|
|
726
679
|
});
|
|
@@ -731,8 +684,6 @@ var mapPieces = (before, after) => {
|
|
|
731
684
|
import React17, { useRef as useRef3, useMemo, useEffect as useEffect4, useState as useState4, memo as memo3 } from "react";
|
|
732
685
|
import { useResizeDetector } from "react-resize-detector";
|
|
733
686
|
import { useTrackProps as useTrackProps2 } from "@dxos/react-ui";
|
|
734
|
-
import { mx as mx5 } from "@dxos/react-ui-theme";
|
|
735
|
-
import { isNotFalsy } from "@dxos/util";
|
|
736
687
|
var Chessboard = /* @__PURE__ */ memo3(({ orientation, showLabels, debug, rows = 8, cols = 8 }) => {
|
|
737
688
|
useTrackProps2({
|
|
738
689
|
orientation,
|
|
@@ -742,7 +693,7 @@ var Chessboard = /* @__PURE__ */ memo3(({ orientation, showLabels, debug, rows =
|
|
|
742
693
|
const { ref: containerRef, width, height } = useResizeDetector({
|
|
743
694
|
refreshRate: 200
|
|
744
695
|
});
|
|
745
|
-
const { model
|
|
696
|
+
const { model } = useBoardContext();
|
|
746
697
|
const locations = useMemo(() => {
|
|
747
698
|
return Array.from({
|
|
748
699
|
length: rows
|
|
@@ -788,19 +739,15 @@ var Chessboard = /* @__PURE__ */ memo3(({ orientation, showLabels, debug, rows =
|
|
|
788
739
|
return [];
|
|
789
740
|
}
|
|
790
741
|
return Object.values(model?.pieces.value ?? {}).map((piece) => {
|
|
791
|
-
if (piece.id === promoting?.id) {
|
|
792
|
-
return null;
|
|
793
|
-
}
|
|
794
742
|
const bounds = grid[locationToString(piece.location)];
|
|
795
743
|
return {
|
|
796
|
-
|
|
797
|
-
|
|
744
|
+
bounds,
|
|
745
|
+
piece
|
|
798
746
|
};
|
|
799
|
-
})
|
|
747
|
+
});
|
|
800
748
|
}, [
|
|
801
749
|
grid,
|
|
802
|
-
model?.pieces.value
|
|
803
|
-
promoting
|
|
750
|
+
model?.pieces.value
|
|
804
751
|
]);
|
|
805
752
|
return /* @__PURE__ */ React17.createElement("div", {
|
|
806
753
|
ref: containerRef,
|
|
@@ -815,7 +762,7 @@ var Chessboard = /* @__PURE__ */ memo3(({ orientation, showLabels, debug, rows =
|
|
|
815
762
|
bounds: grid[locationToString(location)],
|
|
816
763
|
classNames: getSquareColor(location)
|
|
817
764
|
}))), /* @__PURE__ */ React17.createElement("div", {
|
|
818
|
-
className:
|
|
765
|
+
className: "grow"
|
|
819
766
|
}, positions.map(({ bounds, piece }) => /* @__PURE__ */ React17.createElement(Piece, {
|
|
820
767
|
key: piece.id,
|
|
821
768
|
piece,
|
|
@@ -823,71 +770,20 @@ var Chessboard = /* @__PURE__ */ memo3(({ orientation, showLabels, debug, rows =
|
|
|
823
770
|
label: debug ? piece.id : void 0,
|
|
824
771
|
orientation,
|
|
825
772
|
Component: ChessPieces[piece.type]
|
|
826
|
-
})))
|
|
827
|
-
grid,
|
|
828
|
-
piece: promoting,
|
|
829
|
-
onSelect: (piece) => {
|
|
830
|
-
onPromotion({
|
|
831
|
-
from: Object.values(model.pieces.value).find((p) => p.id === promoting.id).location,
|
|
832
|
-
to: piece.location,
|
|
833
|
-
piece: promoting.type,
|
|
834
|
-
promotion: piece.type
|
|
835
|
-
});
|
|
836
|
-
}
|
|
837
|
-
})));
|
|
773
|
+
}))));
|
|
838
774
|
});
|
|
839
775
|
Chessboard.displayName = "Chessboard";
|
|
840
776
|
var getSquareLocation = (container, location) => {
|
|
841
777
|
return container.querySelector(`[data-location="${locationToString(location)}"]`);
|
|
842
778
|
};
|
|
843
|
-
var PromotionSelector = ({ grid, piece, onSelect }) => {
|
|
844
|
-
const positions = [
|
|
845
|
-
"Q",
|
|
846
|
-
"N",
|
|
847
|
-
"R",
|
|
848
|
-
"B"
|
|
849
|
-
].map((pieceType, i) => {
|
|
850
|
-
const location = [
|
|
851
|
-
piece.location[0] + (piece.location[0] === 0 ? i : -i),
|
|
852
|
-
piece.location[1]
|
|
853
|
-
];
|
|
854
|
-
return {
|
|
855
|
-
piece: {
|
|
856
|
-
id: `promotion-${pieceType}`,
|
|
857
|
-
type: (piece.side === "black" ? "B" : "W") + pieceType,
|
|
858
|
-
side: piece.side,
|
|
859
|
-
location
|
|
860
|
-
},
|
|
861
|
-
bounds: grid[locationToString(location)]
|
|
862
|
-
};
|
|
863
|
-
});
|
|
864
|
-
const handleSelect = (selected) => {
|
|
865
|
-
onSelect({
|
|
866
|
-
...piece,
|
|
867
|
-
type: selected.type
|
|
868
|
-
});
|
|
869
|
-
};
|
|
870
|
-
return /* @__PURE__ */ React17.createElement("div", null, positions.map(({ piece: piece2, bounds }) => /* @__PURE__ */ React17.createElement("div", {
|
|
871
|
-
key: piece2.id,
|
|
872
|
-
style: bounds,
|
|
873
|
-
onClick: () => handleSelect(piece2)
|
|
874
|
-
}, /* @__PURE__ */ React17.createElement(Piece, {
|
|
875
|
-
piece: piece2,
|
|
876
|
-
bounds,
|
|
877
|
-
Component: ChessPieces[piece2.type],
|
|
878
|
-
classNames: mx5("border-2 border-neutral-700 rounded-full", boardStyles.promotion)
|
|
879
|
-
}))));
|
|
880
|
-
};
|
|
881
779
|
export {
|
|
882
780
|
Board,
|
|
883
781
|
BoardContext,
|
|
884
782
|
ChessModel,
|
|
885
783
|
ChessPieces,
|
|
886
784
|
Chessboard,
|
|
887
|
-
Container,
|
|
888
785
|
Piece,
|
|
889
786
|
Square,
|
|
890
|
-
boardStyles,
|
|
891
787
|
getRelativeBounds,
|
|
892
788
|
getSquareColor,
|
|
893
789
|
isEqualLocation,
|
|
@@ -898,6 +794,7 @@ export {
|
|
|
898
794
|
mapPieces,
|
|
899
795
|
posToLocation,
|
|
900
796
|
stringToLocation,
|
|
797
|
+
styles,
|
|
901
798
|
useBoardContext
|
|
902
799
|
};
|
|
903
800
|
//# sourceMappingURL=index.mjs.map
|