cm-chessboard 3.15.1 → 3.16.3

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,11 +18,23 @@
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"
25
+ value="e4"/>
26
+ <button onclick="movePiece()">Do move</button>
27
+ <pre>board.movePiece(squareFrom, squareTo)</pre>
28
+ <br/>
29
+ <button onclick="knightMove()">Knight dance</button>
30
+ <pre>
31
+ board.setPiece("e4", PIECE.wn)
32
+ await board.movePiece("e4", "c3")
33
+ await board.movePiece("c3", "d5")
34
+ await board.movePiece("d5", "f4")
35
+ </pre>
24
36
  <script type="module">
25
- import {Chessboard} from "../src/cm-chessboard/Chessboard.js"
37
+ import {Chessboard, PIECE} from "../src/cm-chessboard/Chessboard.js"
26
38
 
27
39
  const board = new Chessboard(document.getElementById("board"),
28
40
  {
@@ -40,6 +52,19 @@
40
52
  window.switchOrientation = () => {
41
53
  board.setOrientation(board.getOrientation() === 'w' ? 'b' : 'w')
42
54
  }
55
+ window.movePiece = () => {
56
+ const squareFrom = document.getElementById("moveFrom").value
57
+ const squareTo = document.getElementById("moveTo").value
58
+ console.log("movePiece", squareFrom, squareTo)
59
+ board.movePiece(squareFrom, squareTo)
60
+ }
61
+ window.knightMove = async () => {
62
+ await board.setPosition("empty")
63
+ board.setPiece("e4", PIECE.wn)
64
+ await board.movePiece("e4", "c3")
65
+ await board.movePiece("c3", "d5")
66
+ await board.movePiece("d5", "f4")
67
+ }
43
68
  </script>
44
69
  </body>
45
70
  </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.3",
4
4
  "description": "A JavaScript chessboard which is lightweight, ES6 module based, responsive, SVG rendered and without dependencies.",
5
5
  "keywords": [
6
6
  "chess",
@@ -40,7 +40,7 @@ export const FEN_EMPTY_POSITION = "8/8/8/8/8/8/8/8"
40
40
 
41
41
  export class Chessboard {
42
42
 
43
- constructor(element, props = {}) {
43
+ constructor(element, props = {}) { // TODO rename element to context
44
44
  if (!element) {
45
45
  throw new Error("container element is " + element)
46
46
  }
@@ -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
+ } else {
124
+ this.state.squares[this.state.squareToIndex(squareFrom)] = null
125
+ this.state.squares[this.state.squareToIndex(squareTo)] = pieceFrom
126
+ if (animated) {
127
+ this.view.animatePieces(prevSquares, this.state.squares, () => {
128
+ resolve()
129
+ })
130
+ } else {
131
+ this.view.drawPieces(this.state.squares)
132
+ resolve()
133
+ }
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()}`