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