cm-chessboard 3.15.1 → 3.16.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/README.md CHANGED
@@ -106,12 +106,18 @@ Sets a piece on a square. Example: `board.setPiece("e4", PIECE.blackKnight)` or
106
106
 
107
107
  Returns the piece on a square or `undefined` if the square is empty.
108
108
 
109
+ ### movePiece(squareFrom, squareTo, animated = true)
110
+
111
+ Move a piece from `squareFrom` to `squareTo`. Returns a **Promise**, which is resolved, when the animation finished.
112
+
113
+ [Example for **movePiece**](https://shaack.com/projekte/cm-chessboard/examples/pieces-animation.html)
114
+
109
115
  ### setPosition(fen, animated = true)
110
116
 
111
117
  Sets the position as `fen`. Special values are `"start"`, sets the chess start position and
112
118
  `"empty"`, sets an empty board. When `animated` is set `false`, the new position will be shown instant.
113
119
 
114
- Returns a **Promise** which will be resolved, after the Animation has finished.
120
+ Returns a **Promise**, which is resolved, when the animation finished.
115
121
 
116
122
  [Example for **setPosition**](https://shaack.com/projekte/cm-chessboard/examples/pieces-animation.html)
117
123
 
@@ -18,9 +18,12 @@
18
18
  <button onclick="setPosition('rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR')">Position 1</button>
19
19
  <button onclick="setPosition('rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R')">Position 2</button>
20
20
  <button onclick="setPosition('rn2k1r1/ppp1pp1p/3p2p1/5bn1/P7/2N2B2/1PPPPP2/2BNK1RR')">Position 3</button>
21
- <button onclick="switchOrientation()">Switch Orientation
22
- </button>
21
+ <button onclick="switchOrientation()">Switch Orientation</button>
23
22
  <pre>&lt;button onclick="window.board.setPosition('rn2k1r1/ppp1pp1p/3p2p1/5bn1/P7/2N2B2/1PPPPP2/2BNK1RR')"&gt;Position 3&lt;/button&gt;</pre>
23
+ <br/>
24
+ Move piece from: <input id="moveFrom" type="text" size="2" value="e2"/> to: <input id="moveTo" type="text" size="2" value="e4"/>
25
+ <button onclick="movePiece()">Do move</button>
26
+ <pre>board.movePiece(squareFrom, squareTo)</pre>
24
27
  <script type="module">
25
28
  import {Chessboard} from "../src/cm-chessboard/Chessboard.js"
26
29
 
@@ -40,6 +43,12 @@
40
43
  window.switchOrientation = () => {
41
44
  board.setOrientation(board.getOrientation() === 'w' ? 'b' : 'w')
42
45
  }
46
+ window.movePiece = () => {
47
+ const squareFrom = document.getElementById("moveFrom").value
48
+ const squareTo = document.getElementById("moveTo").value
49
+ console.log("movePiece", squareFrom, squareTo)
50
+ board.movePiece(squareFrom, squareTo)
51
+ }
43
52
  </script>
44
53
  </body>
45
54
  </html>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cm-chessboard",
3
- "version": "3.15.1",
3
+ "version": "3.16.0",
4
4
  "description": "A JavaScript chessboard which is lightweight, ES6 module based, responsive, SVG rendered and without dependencies.",
5
5
  "keywords": [
6
6
  "chess",
@@ -114,6 +114,27 @@ export class Chessboard {
114
114
  return this.state.squares[this.state.squareToIndex(square)]
115
115
  }
116
116
 
117
+ movePiece(squareFrom, squareTo, animated = true) {
118
+ return new Promise((resolve, reject) => {
119
+ const prevSquares = this.state.squares.slice(0) // clone
120
+ const pieceFrom = this.getPiece(squareFrom)
121
+ if(!pieceFrom) {
122
+ reject("no piece on square " + squareFrom)
123
+ return
124
+ }
125
+ this.state.squares[this.state.squareToIndex(squareFrom)] = undefined
126
+ this.state.squares[this.state.squareToIndex(squareTo)] = pieceFrom
127
+ if (animated) {
128
+ this.view.animatePieces(prevSquares, this.state.squares, () => {
129
+ resolve()
130
+ })
131
+ } else {
132
+ this.view.drawPieces(this.state.squares)
133
+ resolve()
134
+ }
135
+ })
136
+ }
137
+
117
138
  setPosition(fen, animated = true) {
118
139
  return new Promise((resolve) => {
119
140
  if (fen === "start") {
@@ -7,7 +7,7 @@
7
7
  export class ChessboardState {
8
8
 
9
9
  constructor() {
10
- this.squares = new Array(64).fill(undefined)
10
+ this.squares = new Array(64).fill(null)
11
11
  this.orientation = undefined
12
12
  this.markers = []
13
13
  this.inputWhiteEnabled = false
@@ -59,7 +59,7 @@ export class ChessboardState {
59
59
  })
60
60
  for (let c = 0; c < 8; c++) {
61
61
  const char = row.substr(c, 1)
62
- let piece = undefined
62
+ let piece = null
63
63
  if (char !== '-') {
64
64
  if (char.toUpperCase() === char) {
65
65
  piece = `w${char.toLowerCase()}`