cm-chessboard 4.3.9 → 4.4.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/README.md CHANGED
@@ -33,7 +33,7 @@ aspects of chess games.
33
33
 
34
34
  **Option 2:** Download the code from [GitHub](https://github.com/shaack/cm-chessboard).
35
35
 
36
- **Option 3:** Use it via CDN https://cdn.jsdelivr.net/npm/cm-chessboard@4.1/src/cm-chessboard/Chessboard.js
36
+ **Option 3:** Use it via CDN https://cdn.jsdelivr.net/npm/cm-chessboard@4/src/cm-chessboard/Chessboard.js
37
37
 
38
38
  After installation, copy the sprite in `cm-chessboard/assets/images/` to your projects `assets/images/`
39
39
  folder. If you put the sprite somewhere else you have to configure the location
@@ -337,7 +337,7 @@ and have access to the chessboard and can register extension points.
337
337
  class MyCoolChessboardExtension extends Extension {
338
338
  constructor(chessboard, props) {
339
339
  super(chessboard, props)
340
- this.registerExtensionPoint(EXTENSION_POINT.moveInputStateChanged, (data) => {
340
+ this.registerExtensionPoint(EXTENSION_POINT.moveInput, (data) => {
341
341
  // do something on move [start | cancel | done]
342
342
  console.log(data)
343
343
  })
@@ -349,10 +349,11 @@ Currently possible extension points are defined in `Extension.js`.
349
349
 
350
350
  ```js
351
351
  export const EXTENSION_POINT = {
352
- positionChanged: "positionChanged",
353
- boardChanged: "boardChanged",
354
- moveInputStateChanged: "moveInputStateChanged",
355
- destroy: "destroy"
352
+ positionChanged: "positionChanged", // the positions of the pieces was changed
353
+ boardChanged: "boardChanged", // the board (orientation) was changed
354
+ moveInputToggled: "moveInputToggled", // move input was enabled or disabled
355
+ moveInput: "moveInput", // move started, cancelled or done
356
+ destroy: "destroy" // called, before the board is destroyed
356
357
  }
357
358
  ```
358
359
 
@@ -33,7 +33,7 @@
33
33
  import {Accessibility} from "../src/cm-chessboard/extensions/Accessibility.js"
34
34
 
35
35
  const chessboard = new Chessboard(document.getElementById("board"), {
36
- position: "start",
36
+ position: "rn2k1r1/ppp1pp1p/3p2p1/5bn1/P7/2N2B2/1PPPPP2/2BNK1RR w Gkq - 4 11",
37
37
  sprite: {url: "../assets/images/chessboard-sprite.svg"},
38
38
  // animationDuration: 0, // optional, set to 0 to disable animations
39
39
  style: {
package/index.html CHANGED
@@ -19,7 +19,6 @@ It works, it is cool and it is easy to use. 👍</p>
19
19
  <ul>
20
20
  <li><a href="examples/simple-boards.html">Simple chessboards, view only</a></li>
21
21
  <li><a href="examples/responsive-board.html">Responsive chessboard</a></li>
22
- <li><a href="examples/accessible-chessboard.html">All accessibility features turned on</a> 🆕</li>
23
22
  <li><a href="examples/enable-input.html">Input enabled without validation</a></li>
24
23
  <li><a href="examples/validate-moves.html">Input enabled, move validation with chess.js</a> 🥸</li>
25
24
  <li><a href="examples/pieces-animation.html">Set different positions, with animation</a></li>
@@ -28,6 +27,10 @@ It works, it is cool and it is easy to use. 👍</p>
28
27
  <li><a href="examples/many-boards.html">100 boards, in one page</a></li>
29
28
  <li><a href="examples/destroy-many-boards.html">Stress test, 5000 boards created and destroyed</a> 🤓 👍</li>
30
29
  </ul>
30
+ <h3>Examples of extensions</h3>
31
+ <ul>
32
+ <li><a href="examples/accessible-chessboard.html">Accessibility extension</a> 🆕</li>
33
+ </ul>
31
34
  </div>
32
35
  <h2>Chessboard</h2>
33
36
  <div class="board" id="board"></div>
@@ -53,7 +56,7 @@ It works, it is cool and it is easy to use. 👍</p>
53
56
  }
54
57
  </script>
55
58
  <div class="clearfix"></div>
56
- <h2>Test</h2>
59
+ <h2>Tests</h2>
57
60
  <ul>
58
61
  <li><a href="test/">Run the unit tests</a></li>
59
62
  </ul>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cm-chessboard",
3
- "version": "4.3.9",
3
+ "version": "4.4.2",
4
4
  "description": "A JavaScript chessboard which is lightweight, ES6 module based, responsive, SVG rendered and without dependencies.",
5
5
  "keywords": [
6
6
  "chess",
@@ -131,9 +131,9 @@ export class Chessboard {
131
131
  return
132
132
  }
133
133
  this.boardTurning = true
134
- this.state.invokeExtensionPoints(EXTENSION_POINT.boardChanged)
135
134
  return this.positionAnimationsQueue.enqueueTurnBoard(position, color, animated).then(() => {
136
135
  this.boardTurning = false
136
+ this.state.invokeExtensionPoints(EXTENSION_POINT.boardChanged)
137
137
  })
138
138
  }
139
139
 
@@ -94,7 +94,10 @@ export class Accessibility extends Extension {
94
94
  this.piecesListContainer.classList.add("visually-hidden")
95
95
  }
96
96
  }
97
- this.registerExtensionPoint(EXTENSION_POINT.moveInputStateChanged, () => {
97
+ this.registerExtensionPoint(EXTENSION_POINT.moveInput, () => {
98
+ this.updateFormInputs()
99
+ })
100
+ this.registerExtensionPoint(EXTENSION_POINT.moveInputToggled, () => {
98
101
  this.updateFormInputs()
99
102
  })
100
103
  this.registerExtensionPoint(EXTENSION_POINT.positionChanged, () => {
@@ -108,17 +111,19 @@ export class Accessibility extends Extension {
108
111
  }
109
112
  }
110
113
  })
111
- setTimeout(() => { // todo replace with something like this.chessboard.initialized.then(() => { ... })
112
- this.updateFormInputs()
113
- this.redrawPositionInAltAttribute()
114
+ this.registerExtensionPoint(EXTENSION_POINT.boardChanged, () => {
115
+ console.log("EXTENSION_POINT.boardChanged")
114
116
  if (this.props.boardAsTable) {
115
117
  this.redrawBoardAsTable()
116
118
  }
117
- if (this.props.piecesAsList) {
118
- this.redrawPiecesLists()
119
- }
120
-
121
119
  })
120
+ this.redrawPositionInAltAttribute()
121
+ if (this.props.boardAsTable) {
122
+ this.redrawBoardAsTable()
123
+ }
124
+ if (this.props.piecesAsList) {
125
+ this.redrawPiecesLists()
126
+ }
122
127
  }
123
128
 
124
129
  updateFormInputs() {
@@ -173,7 +178,7 @@ ${listB}`
173
178
  redrawBoardAsTable() {
174
179
  const squares = this.chessboard.state.position.squares.slice()
175
180
  const ranks = ["a", "b", "c", "d", "e", "f", "g", "h"]
176
- const files = ["1", "2", "3", "4", "5", "6", "7", "8"]
181
+ const files = ["8", "7", "6", "5", "4", "3", "2", "1"]
177
182
  if (this.chessboard.state.orientation === COLOR.black) {
178
183
  ranks.reverse()
179
184
  files.reverse()
@@ -5,10 +5,12 @@
5
5
  */
6
6
 
7
7
  export const EXTENSION_POINT = {
8
- positionChanged: "positionChanged",
9
- boardChanged: "boardChanged",
10
- moveInputStateChanged: "moveInputStateChanged",
11
- destroy: "destroy"
8
+ positionChanged: "positionChanged", // the positions of the pieces was changed
9
+ boardChanged: "boardChanged", // the board (orientation) was changed
10
+ moveInputToggled: "moveInputToggled", // move input was enabled or disabled
11
+ moveInput: "moveInput", // move started, cancelled or done
12
+ moveInputStateChanged: "moveInput", // TODO deprecated, use `moveInput`
13
+ destroy: "destroy" // called, before the board is destroyed
12
14
  }
13
15
 
14
16
  export class Extension {
@@ -100,7 +100,7 @@ export class Position {
100
100
 
101
101
  movePiece(squareFrom, squareTo) {
102
102
  if(!this.squares[Position.squareToIndex(squareFrom)]) {
103
- console.error("movePiece, no piece on square", squareFrom)
103
+ console.warn("no piece on", squareFrom)
104
104
  return
105
105
  }
106
106
  this.squares[Position.squareToIndex(squareTo)] = this.squares[Position.squareToIndex(squareFrom)]
@@ -310,10 +310,14 @@ export class ChessboardView {
310
310
 
311
311
  setPieceVisibility(square, visible = true) {
312
312
  const piece = this.getPieceElement(square)
313
- if (visible) {
314
- piece.setAttribute("visibility", "visible")
313
+ if(piece) {
314
+ if (visible) {
315
+ piece.setAttribute("visibility", "visible")
316
+ } else {
317
+ piece.setAttribute("visibility", "hidden")
318
+ }
315
319
  } else {
316
- piece.setAttribute("visibility", "hidden")
320
+ console.warn("no piece on", square)
317
321
  }
318
322
  }
319
323
 
@@ -323,7 +327,7 @@ export class ChessboardView {
323
327
  }
324
328
  const piece = this.piecesGroup.querySelector(`g[data-square='${square}']`)
325
329
  if (!piece) {
326
- console.error("no piece found on", square)
330
+ console.warn("no piece on", square)
327
331
  }
328
332
  return piece
329
333
  }
@@ -370,6 +374,7 @@ export class ChessboardView {
370
374
  }
371
375
  this.chessboard.state.inputEnabled = true
372
376
  this.moveInputCallback = eventHandler
377
+ this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.moveInputToggled, {enabled: true, color: color})
373
378
  this.visualizeInputState()
374
379
  }
375
380
 
@@ -378,6 +383,7 @@ export class ChessboardView {
378
383
  this.chessboard.state.inputBlackEnabled = false
379
384
  this.chessboard.state.inputEnabled = false
380
385
  this.moveInputCallback = undefined
386
+ this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.moveInputToggled, {enabled: false})
381
387
  this.visualizeInputState()
382
388
  }
383
389
 
@@ -389,7 +395,7 @@ export class ChessboardView {
389
395
  type: INPUT_EVENT_TYPE.moveStart,
390
396
  square: square
391
397
  }
392
- this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.moveInputStateChanged, data)
398
+ this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.moveInput, data)
393
399
  if (this.moveInputCallback) {
394
400
  return this.moveInputCallback(data)
395
401
  } else {
@@ -404,7 +410,7 @@ export class ChessboardView {
404
410
  squareFrom: squareFrom,
405
411
  squareTo: squareTo
406
412
  }
407
- this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.moveInputStateChanged, data)
413
+ this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.moveInput, data)
408
414
  if (this.moveInputCallback) {
409
415
  return this.moveInputCallback(data)
410
416
  } else {
@@ -420,7 +426,7 @@ export class ChessboardView {
420
426
  squareFrom: squareFrom,
421
427
  squareTo: squareTo
422
428
  }
423
- this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.moveInputStateChanged, data)
429
+ this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.moveInput, data)
424
430
  if (this.moveInputCallback) {
425
431
  this.moveInputCallback(data)
426
432
  }
@@ -26,41 +26,19 @@ export const MOVE_CANCELED_REASON = {
26
26
 
27
27
  const DRAG_THRESHOLD = 4
28
28
 
29
- function createTask() {
30
- let resolve, reject
31
- const promise = new Promise(function (_resolve, _reject) {
32
- resolve = _resolve
33
- reject = _reject
34
- })
35
- promise.resolve = resolve
36
- promise.reject = reject
37
- return promise
38
- }
39
-
40
29
  export class VisualMoveInput {
41
30
 
42
31
  constructor(view, moveStartCallback, moveDoneCallback, moveCanceledCallback) {
43
32
  this.view = view
44
33
  this.chessboard = view.chessboard
45
34
  this.moveStartCallback = (square) => {
46
- const result = moveStartCallback(square)
47
- if (result) {
48
- this.chessboard.moveTask = createTask()
49
- }
50
- return result
35
+ return moveStartCallback(square)
51
36
  }
52
37
  this.moveDoneCallback = (fromSquare, toSquare) => {
53
- const result = moveDoneCallback(fromSquare, toSquare)
54
- if (result) {
55
- this.chessboard.moveTask.resolve()
56
- } else {
57
- this.chessboard.moveTask.reject()
58
- }
59
- return result
38
+ return moveDoneCallback(fromSquare, toSquare)
60
39
  }
61
40
  this.moveCanceledCallback = (reason, fromSquare, toSquare) => {
62
41
  moveCanceledCallback(reason, fromSquare, toSquare)
63
- this.chessboard.moveTask.reject()
64
42
  }
65
43
  this.setMoveInputState(STATE.waitForInputStart)
66
44
  }