next-chessground 0.11.5 → 0.13.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.
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'
@@ -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,33 @@ const Chessboard = (props, ref) => {
6414
6438
  const move = onMove(from, to, promotion);
6415
6439
 
6416
6440
  if (!move) {
6441
+ if (typeof props.setPromoting === 'function') {
6442
+ await props.setPromoting(true);
6443
+ }
6444
+
6417
6445
  show();
6446
+ return false;
6447
+ }
6448
+
6449
+ if (theme.playSounds) {
6450
+ audio(theme.sounds);
6451
+ } // pass the chess object to callback function
6452
+
6453
+
6454
+ if (typeof props.onMove === 'function') {
6455
+ await props.onMove(chess);
6456
+ }
6457
+ };
6458
+
6459
+ const handlePromotion = async promotion => {
6460
+ if (typeof props.setPromoting === 'function') {
6461
+ await props.setPromoting(false);
6462
+ }
6463
+
6464
+ const move = onPromote(promotion);
6465
+
6466
+ if (!move) {
6467
+ return false;
6418
6468
  }
6419
6469
 
6420
6470
  if (theme.playSounds) {
@@ -6442,12 +6492,17 @@ const Chessboard = (props, ref) => {
6442
6492
  isOpen: isOpen,
6443
6493
  hide: hide,
6444
6494
  color: turnColor,
6445
- onPromote: onPromote
6495
+ onPromote: handlePromotion
6446
6496
  }));
6447
6497
  };
6448
6498
 
6449
6499
  var Chessboard$1 = /*#__PURE__*/forwardRef(Chessboard);
6450
6500
 
6501
+ const sideToMove = fen => {
6502
+ const fenOrientation = fen.split(' ')[1];
6503
+ return fenOrientation === 'w' ? 'white' : 'black';
6504
+ };
6505
+
6451
6506
  const getOrientation = props => {
6452
6507
  try {
6453
6508
  if (props.orientation) {
@@ -6455,8 +6510,7 @@ const getOrientation = props => {
6455
6510
  }
6456
6511
 
6457
6512
  if (props.fen) {
6458
- const fenOrientation = props.fen.split(' ')[1];
6459
- return fenOrientation === 'w' ? 'white' : 'black';
6513
+ return sideToMove(props.fen);
6460
6514
  }
6461
6515
 
6462
6516
  return 'white';
@@ -6474,6 +6528,9 @@ const useOrientation = props => {
6474
6528
  });
6475
6529
  };
6476
6530
 
6531
+ useEffect(() => {
6532
+ setOrientation(sideToMove(props.fen));
6533
+ }, [props.reset]);
6477
6534
  return [orientation, flip];
6478
6535
  };
6479
6536
 
@@ -6875,8 +6932,9 @@ const NextEditor = (props, ref) => {
6875
6932
  color: "black"
6876
6933
  }), /*#__PURE__*/React.createElement(Chessboard$1, _extends({}, props, {
6877
6934
  ref: ref,
6935
+ fen: fen,
6878
6936
  onSelect: onSelect,
6879
- fen: fen
6937
+ editing: true
6880
6938
  })), /*#__PURE__*/React.createElement(EditorPieces, {
6881
6939
  selected: selected,
6882
6940
  selectPiece: setSelected,
@@ -7183,4 +7241,4 @@ var css_248z$7 = ".flex {\n display: flex;\n}\n.flex-col {\n flex-direction: c
7183
7241
  styleInject(css_248z$7);
7184
7242
 
7185
7243
  export default NextChessground$1;
7186
- export { NextChessground$1 as NextChessground, NextEditor$1 as NextEditor, Stockfish, constants, useChess, useChessground };
7244
+ 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'
@@ -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,33 @@ const Chessboard = (props, ref) => {
6423
6447
  const move = onMove(from, to, promotion);
6424
6448
 
6425
6449
  if (!move) {
6450
+ if (typeof props.setPromoting === 'function') {
6451
+ await props.setPromoting(true);
6452
+ }
6453
+
6426
6454
  show();
6455
+ return false;
6456
+ }
6457
+
6458
+ if (theme.playSounds) {
6459
+ audio(theme.sounds);
6460
+ } // pass the chess object to callback function
6461
+
6462
+
6463
+ if (typeof props.onMove === 'function') {
6464
+ await props.onMove(chess);
6465
+ }
6466
+ };
6467
+
6468
+ const handlePromotion = async promotion => {
6469
+ if (typeof props.setPromoting === 'function') {
6470
+ await props.setPromoting(false);
6471
+ }
6472
+
6473
+ const move = onPromote(promotion);
6474
+
6475
+ if (!move) {
6476
+ return false;
6427
6477
  }
6428
6478
 
6429
6479
  if (theme.playSounds) {
@@ -6451,12 +6501,17 @@ const Chessboard = (props, ref) => {
6451
6501
  isOpen: isOpen,
6452
6502
  hide: hide,
6453
6503
  color: turnColor,
6454
- onPromote: onPromote
6504
+ onPromote: handlePromotion
6455
6505
  }));
6456
6506
  };
6457
6507
 
6458
6508
  var Chessboard$1 = /*#__PURE__*/React.forwardRef(Chessboard);
6459
6509
 
6510
+ const sideToMove = fen => {
6511
+ const fenOrientation = fen.split(' ')[1];
6512
+ return fenOrientation === 'w' ? 'white' : 'black';
6513
+ };
6514
+
6460
6515
  const getOrientation = props => {
6461
6516
  try {
6462
6517
  if (props.orientation) {
@@ -6464,8 +6519,7 @@ const getOrientation = props => {
6464
6519
  }
6465
6520
 
6466
6521
  if (props.fen) {
6467
- const fenOrientation = props.fen.split(' ')[1];
6468
- return fenOrientation === 'w' ? 'white' : 'black';
6522
+ return sideToMove(props.fen);
6469
6523
  }
6470
6524
 
6471
6525
  return 'white';
@@ -6483,6 +6537,9 @@ const useOrientation = props => {
6483
6537
  });
6484
6538
  };
6485
6539
 
6540
+ React.useEffect(() => {
6541
+ setOrientation(sideToMove(props.fen));
6542
+ }, [props.reset]);
6486
6543
  return [orientation, flip];
6487
6544
  };
6488
6545
 
@@ -6884,8 +6941,9 @@ const NextEditor = (props, ref) => {
6884
6941
  color: "black"
6885
6942
  }), /*#__PURE__*/React__default['default'].createElement(Chessboard$1, _extends({}, props, {
6886
6943
  ref: ref,
6944
+ fen: fen,
6887
6945
  onSelect: onSelect,
6888
- fen: fen
6946
+ editing: true
6889
6947
  })), /*#__PURE__*/React__default['default'].createElement(EditorPieces, {
6890
6948
  selected: selected,
6891
6949
  selectPiece: setSelected,
@@ -7196,5 +7254,6 @@ exports.NextEditor = NextEditor$1;
7196
7254
  exports.Stockfish = Stockfish;
7197
7255
  exports.constants = constants;
7198
7256
  exports.default = NextChessground$1;
7257
+ exports.isValidFen = isValidFen;
7199
7258
  exports.useChess = useChess;
7200
7259
  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.5",
3
+ "version": "0.13.0",
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",