cm-chessboard 4.3.5 → 4.3.9

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
@@ -2,8 +2,6 @@
2
2
 
3
3
  A JavaScript chessboard which is lightweight, ES6 module based, responsive, SVG rendered and **without dependencies**.
4
4
 
5
- It works on desktop (current versions of Chrome, Firefox, Safari, Edge), and mobile (Android and iOS).
6
-
7
5
  cm-chessboard is the main chessboard of
8
6
  [chessmail.eu](https://www.chessmail.eu) and [chessmail.de](https://www.chessmail.de). It is also used
9
7
  in [chess-console](https://shaack.com/projekte/chess-console/examples/load-pgn.html) and in
@@ -0,0 +1,21 @@
1
+ <!doctype html>
2
+ <html>
3
+ <head>
4
+ <link rel="stylesheet" href="../assets/styles/cm-chessboard.css">
5
+ <title>frame test</title>
6
+ </head>
7
+ <body>
8
+ Board:
9
+ <div id="board"></div>
10
+
11
+ <script type="module">
12
+ import { Chessboard, MARKER_TYPE } from '../src/cm-chessboard/Chessboard.js'
13
+
14
+ const board = new Chessboard(document.getElementById('board'), {
15
+ position: 'start',
16
+ sprite: {url: '../assets/images/chessboard-sprite.svg'},
17
+ style: {borderType: 'frame'}
18
+ })
19
+ </script>
20
+ </body>
21
+ </html>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cm-chessboard",
3
- "version": "4.3.5",
3
+ "version": "4.3.9",
4
4
  "description": "A JavaScript chessboard which is lightweight, ES6 module based, responsive, SVG rendered and without dependencies.",
5
5
  "keywords": [
6
6
  "chess",
@@ -87,6 +87,7 @@ export class Chessboard {
87
87
  this.props.language = "en"
88
88
  }
89
89
 
90
+ this.moveTask = Promise.resolve()
90
91
  this.state = new ChessboardState()
91
92
  this.view = new ChessboardView(this)
92
93
  this.positionAnimationsQueue = new PositionAnimationsQueue(this)
@@ -96,7 +97,7 @@ export class Chessboard {
96
97
  this.view.redrawPieces()
97
98
  // instantiate extensions
98
99
  for (const extensionData of this.props.extensions) {
99
- new extensionData.class(this, extensionData.props)
100
+ this.extensions.push(new extensionData.class(this, extensionData.props))
100
101
  }
101
102
  }
102
103
 
@@ -26,14 +26,42 @@ 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
+
29
40
  export class VisualMoveInput {
30
41
 
31
42
  constructor(view, moveStartCallback, moveDoneCallback, moveCanceledCallback) {
32
43
  this.view = view
33
44
  this.chessboard = view.chessboard
34
- this.moveStartCallback = moveStartCallback
35
- this.moveDoneCallback = moveDoneCallback
36
- this.moveCanceledCallback = moveCanceledCallback
45
+ this.moveStartCallback = (square) => {
46
+ const result = moveStartCallback(square)
47
+ if (result) {
48
+ this.chessboard.moveTask = createTask()
49
+ }
50
+ return result
51
+ }
52
+ 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
60
+ }
61
+ this.moveCanceledCallback = (reason, fromSquare, toSquare) => {
62
+ moveCanceledCallback(reason, fromSquare, toSquare)
63
+ this.chessboard.moveTask.reject()
64
+ }
37
65
  this.setMoveInputState(STATE.waitForInputStart)
38
66
  }
39
67
 
@@ -138,7 +166,7 @@ export class VisualMoveInput {
138
166
  }
139
167
  this.toSquare = params.square
140
168
  if (this.toSquare && this.moveDoneCallback(this.fromSquare, this.toSquare)) {
141
- if(prevState === STATE.clickTo) {
169
+ if (prevState === STATE.clickTo) {
142
170
  this.chessboard.movePiece(this.fromSquare, this.toSquare, true).then(() => {
143
171
  this.setMoveInputState(STATE.reset)
144
172
  })
@@ -211,18 +239,18 @@ export class VisualMoveInput {
211
239
  onPointerDown(e) {
212
240
  if (e.type === "mousedown" && e.button === 0 || e.type === "touchstart") {
213
241
  const square = e.target.getAttribute("data-square")
214
- const pieceName = this.chessboard.getPiece(square)
215
- // console.log("onPointerDown", square, pieceName)
216
- let color
217
- if (pieceName) {
218
- color = pieceName ? pieceName.substring(0, 1) : undefined
219
- // allow scrolling, if not pointed on draggable piece
220
- if (color === "w" && this.chessboard.state.inputWhiteEnabled ||
221
- color === "b" && this.chessboard.state.inputBlackEnabled) {
222
- e.preventDefault()
223
- }
224
- }
225
242
  if (square) { // pointer on square
243
+ const pieceName = this.chessboard.getPiece(square)
244
+ // console.log("onPointerDown", square, pieceName)
245
+ let color
246
+ if (pieceName) {
247
+ color = pieceName ? pieceName.substring(0, 1) : undefined
248
+ // allow scrolling, if not pointed on draggable piece
249
+ if (color === "w" && this.chessboard.state.inputWhiteEnabled ||
250
+ color === "b" && this.chessboard.state.inputBlackEnabled) {
251
+ e.preventDefault()
252
+ }
253
+ }
226
254
  if (this.moveInputState !== STATE.waitForInputStart ||
227
255
  this.chessboard.state.inputWhiteEnabled && color === "w" ||
228
256
  this.chessboard.state.inputBlackEnabled && color === "b") {
@@ -365,10 +393,10 @@ export class VisualMoveInput {
365
393
  }
366
394
 
367
395
  updateStartEndMarkers() {
368
- if(this.chessboard.props.style.moveFromMarker) {
396
+ if (this.chessboard.props.style.moveFromMarker) {
369
397
  this.chessboard.state.removeMarkers(undefined, this.chessboard.props.style.moveFromMarker)
370
398
  }
371
- if(this.chessboard.props.style.moveToMarker) {
399
+ if (this.chessboard.props.style.moveToMarker) {
372
400
  this.chessboard.state.removeMarkers(undefined, this.chessboard.props.style.moveToMarker)
373
401
  }
374
402
  if (this.chessboard.props.style.moveFromMarker) {