cm-chessboard 8.0.2 → 8.1.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.
@@ -23,6 +23,7 @@
23
23
 
24
24
  const board1 = new Chessboard(document.getElementById("board1"), {
25
25
  position: FEN.start,
26
+ assetsCache: false,
26
27
  assetsUrl: "../../assets/",
27
28
  extensions: [{class: Markers}]
28
29
  })
@@ -50,7 +51,6 @@
50
51
  }
51
52
  const board2 = new Chessboard(document.getElementById("board2"), {
52
53
  position: "rn2k1r1/ppp1pp1p/3p2p1/5bn1/P7/2N2B2/1PPPPP2/2BNK1RR",
53
- assetsCache: false,
54
54
  assetsUrl: "../../assets/",
55
55
  style: {pieces: {file: "pieces/staunty.svg"}},
56
56
  extensions: [{class: Markers, autoMarkers: MARKER_TYPE.frame}]
@@ -25,10 +25,15 @@
25
25
 
26
26
  const chess = new Chess()
27
27
 
28
+ let seed = 71;
29
+ function random() {
30
+ const x = Math.sin(seed++) * 10000;
31
+ return x - Math.floor(x);
32
+ }
28
33
  function makeEngineMove(chessboard) {
29
34
  const possibleMoves = chess.moves({verbose: true})
30
35
  if (possibleMoves.length > 0) {
31
- const randomIndex = Math.floor(Math.random() * possibleMoves.length)
36
+ const randomIndex = Math.floor(random() * possibleMoves.length)
32
37
  const randomMove = possibleMoves[randomIndex]
33
38
  setTimeout(() => { // smoother with 500ms delay
34
39
  chess.move({from: randomMove.from, to: randomMove.to})
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cm-chessboard",
3
- "version": "8.0.2",
3
+ "version": "8.1.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",
package/src/Chessboard.js CHANGED
@@ -46,7 +46,6 @@ export {FEN}
46
46
  export class Chessboard {
47
47
 
48
48
  constructor(context, props = {}) {
49
- this.initialized = Promise.resolve()
50
49
  if (!context) {
51
50
  throw new Error("container element is " + context)
52
51
  }
@@ -19,7 +19,7 @@ export class Arrows extends Extension {
19
19
  /** @constructor */
20
20
  constructor(chessboard, props = {}) {
21
21
  super(chessboard)
22
- this.registerExtensionPoint(EXTENSION_POINT.redrawBoard, () => {
22
+ this.registerExtensionPoint(EXTENSION_POINT.afterRedrawBoard, () => {
23
23
  this.onRedrawBoard()
24
24
  })
25
25
  this.props = {
@@ -26,7 +26,7 @@ export class Markers extends Extension {
26
26
  /** @constructor */
27
27
  constructor(chessboard, props = {}) {
28
28
  super(chessboard)
29
- this.registerExtensionPoint(EXTENSION_POINT.redrawBoard, () => {
29
+ this.registerExtensionPoint(EXTENSION_POINT.afterRedrawBoard, () => {
30
30
  this.onRedrawBoard()
31
31
  })
32
32
  this.props = {
@@ -24,7 +24,7 @@ export class PromotionDialog extends Extension {
24
24
  /** @constructor */
25
25
  constructor(chessboard) {
26
26
  super(chessboard)
27
- this.registerExtensionPoint(EXTENSION_POINT.redrawBoard, this.extensionPointRedrawBoard.bind(this))
27
+ this.registerExtensionPoint(EXTENSION_POINT.afterRedrawBoard, this.extensionPointRedrawBoard.bind(this))
28
28
  chessboard.showPromotionDialog = this.showPromotionDialog.bind(this)
29
29
  chessboard.isPromotionDialogShown = this.isPromotionDialogShown.bind(this)
30
30
  this.promotionDialogGroup = Svg.addElement(chessboard.view.interactiveTopLayer, "g", {class: "promotion-dialog-group"})
@@ -9,7 +9,9 @@ export const EXTENSION_POINT = {
9
9
  boardChanged: "boardChanged", // the board (orientation) was changed
10
10
  moveInputToggled: "moveInputToggled", // move input was enabled or disabled
11
11
  moveInput: "moveInput", // move started, moving over a square, validating or canceled
12
- redrawBoard: "redrawBoard", // called after redrawing the board
12
+ beforeRedrawBoard: "beforeRedrawBoard", // called before redrawing the board
13
+ afterRedrawBoard: "afterRedrawBoard", // called after redrawing the board
14
+ redrawBoard: "redrawBoard", // called after redrawing the board, DEPRECATED, use afterRedrawBoard
13
15
  animation: "animation", // called on animation start, end and on every animation frame
14
16
  destroy: "destroy" // called, before the board is destroyed
15
17
  }
@@ -21,6 +23,10 @@ export class Extension {
21
23
  }
22
24
 
23
25
  registerExtensionPoint(name, callback) {
26
+ if(name === EXTENSION_POINT.redrawBoard) {
27
+ console.warn("EXTENSION_POINT.redrawBoard is deprecated, use EXTENSION_POINT.afterRedrawBoard")
28
+ name = EXTENSION_POINT.afterRedrawBoard
29
+ }
24
30
  if (!this.chessboard.state.extensionPoints[name]) {
25
31
  this.chessboard.state.extensionPoints[name] = []
26
32
  }
@@ -18,8 +18,8 @@ export class ChessboardView {
18
18
  if (chessboard.props.assetsCache) {
19
19
  this.cacheSpriteToDiv("cm-chessboard-sprite", this.getSpriteUrl())
20
20
  }
21
- this.context = document.createElement("div")
22
- this.chessboard.context.appendChild(this.context)
21
+ this.container = document.createElement("div")
22
+ this.chessboard.context.appendChild(this.container)
23
23
  if (chessboard.props.responsive) {
24
24
  if (typeof ResizeObserver !== "undefined") {
25
25
  this.resizeObserver = new ResizeObserver(() => {
@@ -34,8 +34,8 @@ export class ChessboardView {
34
34
  this.positionsAnimationTask = Promise.resolve()
35
35
  this.pointerDownListener = this.pointerDownHandler.bind(this)
36
36
  this.pointerDownListener = this.pointerDownHandler.bind(this)
37
- this.context.addEventListener("mousedown", this.pointerDownListener)
38
- this.context.addEventListener("touchstart", this.pointerDownListener, {passive: false})
37
+ this.container.addEventListener("mousedown", this.pointerDownListener)
38
+ this.container.addEventListener("touchstart", this.pointerDownListener, {passive: false})
39
39
 
40
40
  this.createSvgAndGroups()
41
41
  this.updateMetrics()
@@ -80,7 +80,7 @@ export class ChessboardView {
80
80
  }
81
81
 
82
82
  createSvgAndGroups() {
83
- this.svg = Svg.createSvg(this.context)
83
+ this.svg = Svg.createSvg(this.container)
84
84
  // let description = document.createElement("description")
85
85
  // description.innerText = "Chessboard"
86
86
  // description.id = "svg-description"
@@ -101,8 +101,8 @@ export class ChessboardView {
101
101
 
102
102
  updateMetrics() {
103
103
  const piecesTileSize = this.chessboard.props.style.pieces.tileSize
104
- this.width = this.context.clientWidth
105
- this.height = this.context.clientWidth * (this.chessboard.props.style.aspectRatio || 1)
104
+ this.width = this.container.clientWidth
105
+ this.height = this.container.clientWidth * (this.chessboard.props.style.aspectRatio || 1)
106
106
  if (this.chessboard.props.style.borderType === BORDER_TYPE.frame) {
107
107
  this.borderSize = this.width / 25
108
108
  } else if (this.chessboard.props.style.borderType === BORDER_TYPE.thin) {
@@ -120,9 +120,9 @@ export class ChessboardView {
120
120
  }
121
121
 
122
122
  handleResize() {
123
- this.context.style.width = this.chessboard.context.clientWidth + "px"
124
- this.context.style.height = (this.chessboard.context.clientWidth * this.chessboard.props.style.aspectRatio) + "px"
125
- if (this.context.clientWidth !== this.width || this.context.clientHeight !== this.height) {
123
+ this.container.style.width = this.chessboard.context.clientWidth + "px"
124
+ this.container.style.height = (this.chessboard.context.clientWidth * this.chessboard.props.style.aspectRatio) + "px"
125
+ if (this.container.clientWidth !== this.width || this.container.clientHeight !== this.height) {
126
126
  this.updateMetrics()
127
127
  this.redrawBoard()
128
128
  this.redrawPieces()
@@ -132,9 +132,10 @@ export class ChessboardView {
132
132
  }
133
133
 
134
134
  redrawBoard() {
135
+ this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.beforeRedrawBoard)
135
136
  this.redrawSquares()
136
137
  this.drawCoordinates()
137
- this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.redrawBoard)
138
+ this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.afterRedrawBoard)
138
139
  this.visualizeInputState()
139
140
  }
140
141
 
package/test/TestBoard.js CHANGED
@@ -24,7 +24,7 @@ describe("TestBoard", () => {
24
24
  assetsUrl: "../assets/",
25
25
  position: FEN.start
26
26
  })
27
- assert.equal(chessboard.view.context.childNodes.length, 1)
27
+ assert.equal(chessboard.view.container.childNodes.length, 1)
28
28
  chessboard.destroy()
29
29
  assert.equal(chessboard.state, undefined)
30
30
  })