next-chessground 0.11.4 → 0.12.2

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/index.es.js CHANGED
@@ -3903,32 +3903,6 @@ const useChessground = () => {
3903
3903
  };
3904
3904
  };
3905
3905
 
3906
- /**
3907
- * Legal chess moves for chessground
3908
- * @param {*} chess
3909
- */
3910
- const toDests = chess => {
3911
- const dests = new Map();
3912
- chess.SQUARES.forEach(s => {
3913
- const ms = chess.moves({
3914
- square: s,
3915
- verbose: true
3916
- });
3917
-
3918
- if (ms.length) {
3919
- dests.set(s, ms.map(m => m.to));
3920
- }
3921
- });
3922
- const color = chess.turn() === 'w' ? 'white' : 'black';
3923
- return {
3924
- color,
3925
- // who's turn is it
3926
- dests,
3927
- // what to move
3928
- free: false
3929
- };
3930
- };
3931
-
3932
3906
  var chess = createCommonjsModule(function (module, exports) {
3933
3907
  /*
3934
3908
  * Copyright (c) 2021, Jeff Hlywa (jhlywa@gmail.com)
@@ -5948,6 +5922,51 @@ var Chess = function (fen) {
5948
5922
  exports.Chess = Chess;
5949
5923
  });
5950
5924
 
5925
+ const validateKings = fen => {
5926
+ if (!fen) {
5927
+ return false;
5928
+ }
5929
+
5930
+ const position = fen.split(' ')[0];
5931
+ return (position.match(/k/g) || []).length === 1 && (position.match(/K/g) || []).length === 1;
5932
+ };
5933
+
5934
+ const isValidFen = fen => {
5935
+ const chess$1 = new chess.Chess(fen);
5936
+ return chess$1.validate_fen(fen) && validateKings(fen);
5937
+ };
5938
+
5939
+ /**
5940
+ * Legal chess moves for chessground
5941
+ * @param {*} chess
5942
+ */
5943
+
5944
+ const toDests = chess => {
5945
+ if (!isValidFen(chess.fen())) {
5946
+ return;
5947
+ }
5948
+
5949
+ const dests = new Map();
5950
+ const color = chess.turn() === 'w' ? 'white' : 'black';
5951
+ chess.SQUARES.forEach(s => {
5952
+ const ms = chess.moves({
5953
+ square: s,
5954
+ verbose: true
5955
+ });
5956
+
5957
+ if (ms.length) {
5958
+ dests.set(s, ms.map(m => m.to));
5959
+ }
5960
+ });
5961
+ return {
5962
+ color,
5963
+ // who's turn is it
5964
+ dests,
5965
+ // what to move
5966
+ free: false
5967
+ };
5968
+ };
5969
+
5951
5970
  const fen = {
5952
5971
  initial: 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1',
5953
5972
  empty: '8/8/8/8/8/8/8/8 w - - 0 1'
@@ -5970,7 +5989,7 @@ const useChess = props => {
5970
5989
  setChess(new chess.Chess(props.fen));
5971
5990
  setLastMove([]);
5972
5991
  }
5973
- }, [props.fen]);
5992
+ }, [props.fen, props.reset]);
5974
5993
  const [lastMove, setLastMove] = useState([]);
5975
5994
  const promotion = props.lastMove && props.lastMove.promotion;
5976
5995
  const turnColor = chess$1.turn() === 'w' ? 'white' : 'black';
@@ -5987,13 +6006,8 @@ const useChess = props => {
5987
6006
  return move;
5988
6007
  };
5989
6008
 
5990
- const onPromote = async promotion => {
6009
+ const onPromote = promotion => {
5991
6010
  const move = onMove(lastMove[0], lastMove[1], promotion);
5992
-
5993
- if (typeof props.onMove === 'function') {
5994
- await props.onMove(chess$1);
5995
- }
5996
-
5997
6011
  return move;
5998
6012
  };
5999
6013
 
@@ -6385,6 +6399,16 @@ const cgProps = props => {
6385
6399
  cgProps.drawable = {
6386
6400
  enabled: false
6387
6401
  };
6402
+ } // helper for Chessground editing mode
6403
+
6404
+
6405
+ if (props.editing) {
6406
+ cgProps.movable = {
6407
+ free: false
6408
+ };
6409
+ cgProps.drawable = {
6410
+ enabled: false
6411
+ };
6388
6412
  }
6389
6413
 
6390
6414
  return cgProps;
@@ -6414,7 +6438,24 @@ const Chessboard = (props, ref) => {
6414
6438
  const move = onMove(from, to, promotion);
6415
6439
 
6416
6440
  if (!move) {
6417
- show();
6441
+ return show();
6442
+ }
6443
+
6444
+ if (theme.playSounds) {
6445
+ audio(theme.sounds);
6446
+ } // pass the chess object to callback function
6447
+
6448
+
6449
+ if (typeof props.onMove === 'function') {
6450
+ await props.onMove(chess);
6451
+ }
6452
+ };
6453
+
6454
+ const handlePromotion = async promotion => {
6455
+ const move = onPromote(promotion);
6456
+
6457
+ if (!move) {
6458
+ return false;
6418
6459
  }
6419
6460
 
6420
6461
  if (theme.playSounds) {
@@ -6442,12 +6483,17 @@ const Chessboard = (props, ref) => {
6442
6483
  isOpen: isOpen,
6443
6484
  hide: hide,
6444
6485
  color: turnColor,
6445
- onPromote: onPromote
6486
+ onPromote: handlePromotion
6446
6487
  }));
6447
6488
  };
6448
6489
 
6449
6490
  var Chessboard$1 = /*#__PURE__*/forwardRef(Chessboard);
6450
6491
 
6492
+ const sideToMove = fen => {
6493
+ const fenOrientation = fen.split(' ')[1];
6494
+ return fenOrientation === 'w' ? 'white' : 'black';
6495
+ };
6496
+
6451
6497
  const getOrientation = props => {
6452
6498
  try {
6453
6499
  if (props.orientation) {
@@ -6455,8 +6501,7 @@ const getOrientation = props => {
6455
6501
  }
6456
6502
 
6457
6503
  if (props.fen) {
6458
- const fenOrientation = props.fen.split(' ')[1];
6459
- return fenOrientation === 'w' ? 'white' : 'black';
6504
+ return sideToMove(props.fen);
6460
6505
  }
6461
6506
 
6462
6507
  return 'white';
@@ -6474,6 +6519,9 @@ const useOrientation = props => {
6474
6519
  });
6475
6520
  };
6476
6521
 
6522
+ useEffect(() => {
6523
+ setOrientation(sideToMove(props.fen));
6524
+ }, [props.reset]);
6477
6525
  return [orientation, flip];
6478
6526
  };
6479
6527
 
@@ -6875,8 +6923,9 @@ const NextEditor = (props, ref) => {
6875
6923
  color: "black"
6876
6924
  }), /*#__PURE__*/React.createElement(Chessboard$1, _extends({}, props, {
6877
6925
  ref: ref,
6926
+ fen: fen,
6878
6927
  onSelect: onSelect,
6879
- fen: fen
6928
+ editing: true
6880
6929
  })), /*#__PURE__*/React.createElement(EditorPieces, {
6881
6930
  selected: selected,
6882
6931
  selectPiece: setSelected,
@@ -7183,4 +7232,4 @@ var css_248z$7 = ".flex {\n display: flex;\n}\n.flex-col {\n flex-direction: c
7183
7232
  styleInject(css_248z$7);
7184
7233
 
7185
7234
  export default NextChessground$1;
7186
- export { NextChessground$1 as NextChessground, NextEditor$1 as NextEditor, Stockfish, constants, useChess, useChessground };
7235
+ export { NextChessground$1 as NextChessground, NextEditor$1 as NextEditor, Stockfish, constants, isValidFen, useChess, useChessground };
package/dist/index.js CHANGED
@@ -3912,32 +3912,6 @@ const useChessground = () => {
3912
3912
  };
3913
3913
  };
3914
3914
 
3915
- /**
3916
- * Legal chess moves for chessground
3917
- * @param {*} chess
3918
- */
3919
- const toDests = chess => {
3920
- const dests = new Map();
3921
- chess.SQUARES.forEach(s => {
3922
- const ms = chess.moves({
3923
- square: s,
3924
- verbose: true
3925
- });
3926
-
3927
- if (ms.length) {
3928
- dests.set(s, ms.map(m => m.to));
3929
- }
3930
- });
3931
- const color = chess.turn() === 'w' ? 'white' : 'black';
3932
- return {
3933
- color,
3934
- // who's turn is it
3935
- dests,
3936
- // what to move
3937
- free: false
3938
- };
3939
- };
3940
-
3941
3915
  var chess = createCommonjsModule(function (module, exports) {
3942
3916
  /*
3943
3917
  * Copyright (c) 2021, Jeff Hlywa (jhlywa@gmail.com)
@@ -5957,6 +5931,51 @@ var Chess = function (fen) {
5957
5931
  exports.Chess = Chess;
5958
5932
  });
5959
5933
 
5934
+ const validateKings = fen => {
5935
+ if (!fen) {
5936
+ return false;
5937
+ }
5938
+
5939
+ const position = fen.split(' ')[0];
5940
+ return (position.match(/k/g) || []).length === 1 && (position.match(/K/g) || []).length === 1;
5941
+ };
5942
+
5943
+ const isValidFen = fen => {
5944
+ const chess$1 = new chess.Chess(fen);
5945
+ return chess$1.validate_fen(fen) && validateKings(fen);
5946
+ };
5947
+
5948
+ /**
5949
+ * Legal chess moves for chessground
5950
+ * @param {*} chess
5951
+ */
5952
+
5953
+ const toDests = chess => {
5954
+ if (!isValidFen(chess.fen())) {
5955
+ return;
5956
+ }
5957
+
5958
+ const dests = new Map();
5959
+ const color = chess.turn() === 'w' ? 'white' : 'black';
5960
+ chess.SQUARES.forEach(s => {
5961
+ const ms = chess.moves({
5962
+ square: s,
5963
+ verbose: true
5964
+ });
5965
+
5966
+ if (ms.length) {
5967
+ dests.set(s, ms.map(m => m.to));
5968
+ }
5969
+ });
5970
+ return {
5971
+ color,
5972
+ // who's turn is it
5973
+ dests,
5974
+ // what to move
5975
+ free: false
5976
+ };
5977
+ };
5978
+
5960
5979
  const fen = {
5961
5980
  initial: 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1',
5962
5981
  empty: '8/8/8/8/8/8/8/8 w - - 0 1'
@@ -5979,7 +5998,7 @@ const useChess = props => {
5979
5998
  setChess(new chess.Chess(props.fen));
5980
5999
  setLastMove([]);
5981
6000
  }
5982
- }, [props.fen]);
6001
+ }, [props.fen, props.reset]);
5983
6002
  const [lastMove, setLastMove] = React.useState([]);
5984
6003
  const promotion = props.lastMove && props.lastMove.promotion;
5985
6004
  const turnColor = chess$1.turn() === 'w' ? 'white' : 'black';
@@ -5996,13 +6015,8 @@ const useChess = props => {
5996
6015
  return move;
5997
6016
  };
5998
6017
 
5999
- const onPromote = async promotion => {
6018
+ const onPromote = promotion => {
6000
6019
  const move = onMove(lastMove[0], lastMove[1], promotion);
6001
-
6002
- if (typeof props.onMove === 'function') {
6003
- await props.onMove(chess$1);
6004
- }
6005
-
6006
6020
  return move;
6007
6021
  };
6008
6022
 
@@ -6394,6 +6408,16 @@ const cgProps = props => {
6394
6408
  cgProps.drawable = {
6395
6409
  enabled: false
6396
6410
  };
6411
+ } // helper for Chessground editing mode
6412
+
6413
+
6414
+ if (props.editing) {
6415
+ cgProps.movable = {
6416
+ free: false
6417
+ };
6418
+ cgProps.drawable = {
6419
+ enabled: false
6420
+ };
6397
6421
  }
6398
6422
 
6399
6423
  return cgProps;
@@ -6423,7 +6447,24 @@ const Chessboard = (props, ref) => {
6423
6447
  const move = onMove(from, to, promotion);
6424
6448
 
6425
6449
  if (!move) {
6426
- show();
6450
+ return show();
6451
+ }
6452
+
6453
+ if (theme.playSounds) {
6454
+ audio(theme.sounds);
6455
+ } // pass the chess object to callback function
6456
+
6457
+
6458
+ if (typeof props.onMove === 'function') {
6459
+ await props.onMove(chess);
6460
+ }
6461
+ };
6462
+
6463
+ const handlePromotion = async promotion => {
6464
+ const move = onPromote(promotion);
6465
+
6466
+ if (!move) {
6467
+ return false;
6427
6468
  }
6428
6469
 
6429
6470
  if (theme.playSounds) {
@@ -6451,12 +6492,17 @@ const Chessboard = (props, ref) => {
6451
6492
  isOpen: isOpen,
6452
6493
  hide: hide,
6453
6494
  color: turnColor,
6454
- onPromote: onPromote
6495
+ onPromote: handlePromotion
6455
6496
  }));
6456
6497
  };
6457
6498
 
6458
6499
  var Chessboard$1 = /*#__PURE__*/React.forwardRef(Chessboard);
6459
6500
 
6501
+ const sideToMove = fen => {
6502
+ const fenOrientation = fen.split(' ')[1];
6503
+ return fenOrientation === 'w' ? 'white' : 'black';
6504
+ };
6505
+
6460
6506
  const getOrientation = props => {
6461
6507
  try {
6462
6508
  if (props.orientation) {
@@ -6464,8 +6510,7 @@ const getOrientation = props => {
6464
6510
  }
6465
6511
 
6466
6512
  if (props.fen) {
6467
- const fenOrientation = props.fen.split(' ')[1];
6468
- return fenOrientation === 'w' ? 'white' : 'black';
6513
+ return sideToMove(props.fen);
6469
6514
  }
6470
6515
 
6471
6516
  return 'white';
@@ -6483,6 +6528,9 @@ const useOrientation = props => {
6483
6528
  });
6484
6529
  };
6485
6530
 
6531
+ React.useEffect(() => {
6532
+ setOrientation(sideToMove(props.fen));
6533
+ }, [props.reset]);
6486
6534
  return [orientation, flip];
6487
6535
  };
6488
6536
 
@@ -6884,8 +6932,9 @@ const NextEditor = (props, ref) => {
6884
6932
  color: "black"
6885
6933
  }), /*#__PURE__*/React__default['default'].createElement(Chessboard$1, _extends({}, props, {
6886
6934
  ref: ref,
6935
+ fen: fen,
6887
6936
  onSelect: onSelect,
6888
- fen: fen
6937
+ editing: true
6889
6938
  })), /*#__PURE__*/React__default['default'].createElement(EditorPieces, {
6890
6939
  selected: selected,
6891
6940
  selectPiece: setSelected,
@@ -7196,5 +7245,6 @@ exports.NextEditor = NextEditor$1;
7196
7245
  exports.Stockfish = Stockfish;
7197
7246
  exports.constants = constants;
7198
7247
  exports.default = NextChessground$1;
7248
+ exports.isValidFen = isValidFen;
7199
7249
  exports.useChess = useChess;
7200
7250
  exports.useChessground = useChessground;
package/index.js CHANGED
@@ -4,6 +4,7 @@ import useChess from './hooks/use-chess';
4
4
  import useChessground from './hooks/use-chessground';
5
5
  import constants from './utils/constants';
6
6
  import Stockfish from './utils/stockfish';
7
+ import isValidFen from './utils/is-valid-fen';
7
8
 
8
9
  import './assets/css/board.css';
9
10
  import './assets/css/chess.css';
@@ -20,6 +21,7 @@ export {
20
21
  NextChessground,
21
22
  NextEditor,
22
23
  Stockfish,
24
+ isValidFen,
23
25
  useChess,
24
26
  useChessground,
25
27
  constants,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-chessground",
3
- "version": "0.11.4",
3
+ "version": "0.12.2",
4
4
  "description": "React and Next wrapper for Chessground with chessboard and pieces out of the box",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.es.js",