cm-chessboard 4.3.9 → 4.4.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
@@ -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.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",
@@ -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 {
@@ -370,6 +370,7 @@ export class ChessboardView {
370
370
  }
371
371
  this.chessboard.state.inputEnabled = true
372
372
  this.moveInputCallback = eventHandler
373
+ this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.moveInputToggled, {enabled: true, color: color})
373
374
  this.visualizeInputState()
374
375
  }
375
376
 
@@ -378,6 +379,7 @@ export class ChessboardView {
378
379
  this.chessboard.state.inputBlackEnabled = false
379
380
  this.chessboard.state.inputEnabled = false
380
381
  this.moveInputCallback = undefined
382
+ this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.moveInputToggled, {enabled: false})
381
383
  this.visualizeInputState()
382
384
  }
383
385
 
@@ -389,7 +391,7 @@ export class ChessboardView {
389
391
  type: INPUT_EVENT_TYPE.moveStart,
390
392
  square: square
391
393
  }
392
- this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.moveInputStateChanged, data)
394
+ this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.moveInput, data)
393
395
  if (this.moveInputCallback) {
394
396
  return this.moveInputCallback(data)
395
397
  } else {
@@ -404,7 +406,7 @@ export class ChessboardView {
404
406
  squareFrom: squareFrom,
405
407
  squareTo: squareTo
406
408
  }
407
- this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.moveInputStateChanged, data)
409
+ this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.moveInput, data)
408
410
  if (this.moveInputCallback) {
409
411
  return this.moveInputCallback(data)
410
412
  } else {
@@ -420,7 +422,7 @@ export class ChessboardView {
420
422
  squareFrom: squareFrom,
421
423
  squareTo: squareTo
422
424
  }
423
- this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.moveInputStateChanged, data)
425
+ this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.moveInput, data)
424
426
  if (this.moveInputCallback) {
425
427
  this.moveInputCallback(data)
426
428
  }
@@ -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
  }