cm-chessboard 8.12.14 → 8.12.16

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cm-chessboard",
3
- "version": "8.12.14",
3
+ "version": "8.12.16",
4
4
  "description": "A JavaScript chessboard which is lightweight, ES6 module based, responsive, SVG rendered and without dependencies.",
5
5
  "keywords": [
6
6
  "chess",
@@ -68,6 +68,10 @@ export class ChessboardView {
68
68
  this.pointerDownListener = this.pointerDownHandler.bind(this)
69
69
  this.container.addEventListener("mousedown", this.pointerDownListener)
70
70
  this.container.addEventListener("touchstart", this.pointerDownListener, {passive: false})
71
+ // Suppress the native context menu on the whole board
72
+ // VisualMoveInput triggers a move cancel with a transient listener on right click
73
+ this.contextMenuListener = (e) => e.preventDefault()
74
+ this.container.addEventListener("contextmenu", this.contextMenuListener)
71
75
  this.createSvgAndGroups()
72
76
  this.handleResize()
73
77
  }
@@ -91,8 +95,9 @@ export class ChessboardView {
91
95
  if (this.resizeListener) {
92
96
  window.removeEventListener("resize", this.resizeListener)
93
97
  }
94
- this.chessboard.context.removeEventListener("mousedown", this.pointerDownListener)
95
- this.chessboard.context.removeEventListener("touchstart", this.pointerDownListener)
98
+ this.container.removeEventListener("mousedown", this.pointerDownListener)
99
+ this.container.removeEventListener("touchstart", this.pointerDownListener)
100
+ this.container.removeEventListener("contextmenu", this.contextMenuListener)
96
101
  Svg.removeElement(this.svg)
97
102
  this.container.remove()
98
103
  }
@@ -23,7 +23,8 @@ export const MOVE_CANCELED_REASON = {
23
23
  secondaryClick: "secondaryClick", // right click while moving
24
24
  movedOutOfBoard: "movedOutOfBoard",
25
25
  draggedBack: "draggedBack", // dragged to the start square
26
- clickedAnotherPiece: "clickedAnotherPiece" // of the same color
26
+ clickedAnotherPiece: "clickedAnotherPiece", // of the same color
27
+ touchCanceled: "touchCanceled"
27
28
  }
28
29
 
29
30
  const DRAG_THRESHOLD = 4
@@ -90,6 +91,10 @@ export class VisualMoveInput {
90
91
  removeEventListener(this.pointerUpListener.type, this.pointerUpListener)
91
92
  this.pointerUpListener = null
92
93
  }
94
+ if (this.pointerCancelListener) {
95
+ removeEventListener(this.pointerCancelListener.type, this.pointerCancelListener)
96
+ this.pointerCancelListener = null
97
+ }
93
98
  this.fromSquare = params.square
94
99
  this.toSquare = null
95
100
  this.movedPiece = params.piece
@@ -109,6 +114,9 @@ export class VisualMoveInput {
109
114
  this.pointerUpListener = this.onPointerUp.bind(this)
110
115
  this.pointerUpListener.type = "touchend"
111
116
  addEventListener("touchend", this.pointerUpListener)
117
+ this.pointerCancelListener = this.onPointerCancel.bind(this)
118
+ this.pointerCancelListener.type = "touchcancel"
119
+ addEventListener("touchcancel", this.pointerCancelListener)
112
120
  } else {
113
121
  throw Error("4b74af")
114
122
  }
@@ -195,8 +203,12 @@ export class VisualMoveInput {
195
203
  removeEventListener(this.pointerUpListener.type, this.pointerUpListener)
196
204
  this.pointerUpListener = null
197
205
  }
206
+ if (this.pointerCancelListener) {
207
+ removeEventListener(this.pointerCancelListener.type, this.pointerCancelListener)
208
+ this.pointerCancelListener = null
209
+ }
198
210
  if (this.contextMenuListener) {
199
- removeEventListener("contextmenu", this.contextMenuListener)
211
+ this.chessboard.view.svg.removeEventListener("contextmenu", this.contextMenuListener)
200
212
  this.contextMenuListener = null
201
213
  }
202
214
  this.setMoveInputState(MOVE_INPUT_STATE.waitForInputStart)
@@ -401,6 +413,13 @@ export class VisualMoveInput {
401
413
  }
402
414
  }
403
415
 
416
+ onPointerCancel() {
417
+ this.view.redrawPieces()
418
+ const moveStartSquare = this.fromSquare
419
+ this.setMoveInputState(MOVE_INPUT_STATE.reset)
420
+ this.moveInputCanceledCallback(moveStartSquare, null, MOVE_CANCELED_REASON.touchCanceled)
421
+ }
422
+
404
423
  onContextMenu(e) { // while moving
405
424
  e.preventDefault()
406
425
  this.view.redrawPieces()
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Author and copyright: Stefan Haack (https://shaack.com)
3
+ * Repository: https://github.com/shaack/cm-chessboard
4
+ * License: MIT, see file 'LICENSE'
5
+ */
6
+
7
+ import {describe, it, assert} from "../node_modules/teevi/src/teevi.js"
8
+ import {Chessboard} from "../src/Chessboard.js"
9
+ import {FEN} from "../src/model/Position.js"
10
+
11
+ const STATE_WAIT_FOR_INPUT_START = "waitForInputStart"
12
+ const STATE_PIECE_CLICKED_THRESHOLD = "pieceClickedThreshold"
13
+
14
+ describe("TestVisualMoveInput", () => {
15
+
16
+ it("should reset move input state on touchcancel", async () => {
17
+ const chessboard = new Chessboard(document.getElementById("TestBoard"), {
18
+ assetsUrl: "../assets/",
19
+ position: FEN.start
20
+ })
21
+ chessboard.enableMoveInput(() => true)
22
+ const visualMoveInput = chessboard.view.visualMoveInput
23
+
24
+ visualMoveInput.onPointerDown({
25
+ type: "touchstart",
26
+ target: {getAttribute: (name) => (name === "data-square" ? "e2" : null)},
27
+ touches: [{clientX: 100, clientY: 100}],
28
+ preventDefault: () => {}
29
+ })
30
+ assert.equal(visualMoveInput.moveInputState, STATE_PIECE_CLICKED_THRESHOLD)
31
+
32
+ window.dispatchEvent(new Event("touchcancel"))
33
+
34
+ assert.equal(visualMoveInput.moveInputState, STATE_WAIT_FOR_INPUT_START)
35
+
36
+ await new Promise((resolve) => setTimeout(resolve))
37
+ chessboard.destroy()
38
+ })
39
+
40
+ })
package/test/index.html CHANGED
@@ -26,6 +26,7 @@
26
26
  import "./TestPiecesAnimation.js"
27
27
  import "./TestMarkers.js"
28
28
  import "./TestPosition.js"
29
+ import "./TestVisualMoveInput.js"
29
30
  teevi.run()
30
31
  </script>
31
32
  </body>