cm-chessboard 7.11.0 → 8.0.1

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
@@ -88,26 +88,25 @@ Below is the default configuration
88
88
 
89
89
  ```javascript
90
90
  this.props = {
91
- position: FEN.empty, // set position as fen, use FEN.start or FEN.empty as shortcuts
92
- orientation: COLOR.white, // white on bottom
93
- responsive: true, // resize the board automatically to the size of the context element
94
- language: navigator.language.substring(0, 2).toLowerCase(), // supports "de" and "en" for now, used for pieces naming
95
- assetsUrl: "./assets/", // put all css and sprites in this folder, will be ignored for absolute urls of assets files
96
- assetsCache: true, // cache sprites
97
- style: {
98
- cssClass: "default", // set the css theme of the board, try "green", "blue" or "chess-club"
99
- showCoordinates: true, // show ranks and files
100
- borderType: BORDER_TYPE.none, // "thin" thin border, "frame" wide border with coordinates in it, "none" no border
101
- aspectRatio: 1, // height/width of the board
102
- pieces: {
103
- type: PIECES_FILE_TYPE.svgSprite, // pieces are in an SVG sprite, no other type supported for now
104
- file: "pieces/standard.svg", // the filename of the sprite in `assets/pieces/` or an absolute url like `https://…` or `/…`
105
- tileSize: 40 // the tile size in the sprite
91
+ position: FEN.empty, // set position as fen, use FEN.start or FEN.empty as shortcuts
92
+ orientation: COLOR.white, // white on bottom
93
+ responsive: true, // resize the board automatically to the size of the context element
94
+ language: navigator.language.substring(0, 2).toLowerCase(), // supports "de" and "en" for now, used for pieces naming
95
+ assetsUrl: "./assets/", // put all css and sprites in this folder, will be ignored for absolute urls of assets files
96
+ assetsCache: true, // cache sprites
97
+ style: {
98
+ cssClass: "default", // set the css theme of the board, try "green", "blue" or "chess-club"
99
+ showCoordinates: true, // show ranks and files
100
+ borderType: BORDER_TYPE.none, // "thin" thin border, "frame" wide border with coordinates in it, "none" no border
101
+ aspectRatio: 1, // height/width of the board
102
+ pieces: {
103
+ type: PIECES_FILE_TYPE.svgSprite, // pieces are in an SVG sprite, no other type supported for now
104
+ file: "pieces/standard.svg", // the filename of the sprite in `assets/pieces/` or an absolute url like `https://…` or `/…`
105
+ tileSize: 40 // the tile size in the sprite
106
+ },
107
+ animationDuration: 300 // pieces animation duration in milliseconds. Disable all animations with `0`
106
108
  },
107
- // pieces animation duration in milliseconds. Disable all animations with `0`. Respects the operating system settings for reduced motion.
108
- animationDuration: window.matchMedia("(prefers-reduced-motion: reduce)").matches ? 0 : 300
109
- },
110
- extensions: [ /* {class: ExtensionClass, props: { ... }} */] // add extensions here
109
+ extensions: [ /* {class: ExtensionClass, props: { ... }} */] // add extensions here
111
110
  }
112
111
  ```
113
112
 
@@ -246,32 +245,6 @@ chessboard.enableMoveInput((event) => {
246
245
 
247
246
  Disables moves via user input.
248
247
 
249
- ### enableSquareSelect(eventHandler)
250
-
251
- > `enableSquareSelect` is deprecated and will be removed in future versions, because you can directly add events to the `chessboard.context` and then read the square from `event.target.getAttribute("data-square")`.
252
-
253
- Enables primary and secondary pointer events on squares.
254
- On desktop devices this means left and right click on squares.
255
-
256
- ```javascript
257
- board.enableSquareSelect((event) => {
258
- switch (event.type) {
259
- case SQUARE_SELECT_TYPE.primary:
260
- // left click
261
- case SQUARE_SELECT_TYPE.secondary:
262
- // right click
263
- }
264
- })
265
- ```
266
-
267
- [Example for pointer events handling](https://shaack.com/projekte/cm-chessboard/examples/extensions/markers-extension.html)
268
-
269
- **`event.square`** contains the coordinates of the user input.
270
-
271
- ### disableSquareSelect()
272
-
273
- Disables the square select.
274
-
275
248
  ## Piece sets
276
249
 
277
250
  cm-chessboard supports alternative piece sets. A piece set is defined in an SVG sprite. cm-chessboard is shipped with
@@ -61,8 +61,8 @@
61
61
  const move = {from: event.squareFrom, to: event.squareTo, promotion: event.promotion}
62
62
  const result = chess.move(move)
63
63
  if (result) {
64
- this.chessboard.state.moveInputProcess.then(() => { // wait for the move input process has finished
65
- this.chessboard.setPosition(chess.fen(), true).then(() => { // update position, maybe castled and wait for animation has finished
64
+ event.chessboard.state.moveInputProcess.then(() => { // wait for the move input process has finished
65
+ event.chessboard.setPosition(chess.fen(), true).then(() => { // update position, maybe castled and wait for animation has finished
66
66
  makeEngineMove(event.chessboard)
67
67
  })
68
68
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cm-chessboard",
3
- "version": "7.11.0",
3
+ "version": "8.0.1",
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
@@ -70,8 +70,7 @@ export class Chessboard {
70
70
  file: "pieces/standard.svg", // the filename of the sprite in `assets/pieces/` or an absolute url like `https://…` or `/…`
71
71
  tileSize: 40 // the tile size in the sprite
72
72
  },
73
- // pieces animation duration in milliseconds. Disable all animations with `0`. Respects the operating system settings for reduced motion.
74
- animationDuration: window.matchMedia("(prefers-reduced-motion: reduce)").matches ? 0 : 300
73
+ animationDuration: 300 // pieces animation duration in milliseconds. Disable all animations with `0`
75
74
  },
76
75
  extensions: [ /* {class: ExtensionClass, props: { ... }} */] // add extensions here
77
76
  }
@@ -153,54 +152,6 @@ export class Chessboard {
153
152
  this.view.disableMoveInput()
154
153
  }
155
154
 
156
- /**
157
- * This will be removed in the future, because you can directly assign events
158
- * to the `chessboard.context` and then read the square from the `event.target`
159
- * @deprecated
160
- */
161
- enableSquareSelect(eventHandler) {
162
- console.warn("chessboard.enableSquareSelect() is deprecated will be removed in future versions");
163
- if (this.squareSelectListener) {
164
- console.warn("squareSelectListener already existing")
165
- return
166
- }
167
- this.squareSelectListener = function (e) {
168
- const square = e.target.getAttribute("data-square")
169
- if (e.type === "contextmenu") {
170
- // disable context menu
171
- e.preventDefault()
172
- return
173
- }
174
- eventHandler({
175
- mouseEvent: e,
176
- chessboard: this,
177
- type: e.button === 2 ? SQUARE_SELECT_TYPE.secondary : SQUARE_SELECT_TYPE.primary,
178
- square: square
179
- })
180
- }
181
- this.context.addEventListener("contextmenu", this.squareSelectListener)
182
- this.context.addEventListener("mousedown", this.squareSelectListener)
183
- this.context.addEventListener("mouseup", this.squareSelectListener)
184
- this.context.addEventListener("touchstart", this.squareSelectListener, {passive: false})
185
- this.context.addEventListener("touchend", this.squareSelectListener)
186
- this.state.squareSelectEnabled = true
187
- this.view.visualizeInputState()
188
- }
189
-
190
- /**
191
- * @deprecated
192
- */
193
- disableSquareSelect() {
194
- this.context.removeEventListener("contextmenu", this.squareSelectListener)
195
- this.context.removeEventListener("mousedown", this.squareSelectListener)
196
- this.context.removeEventListener("mouseup", this.squareSelectListener)
197
- this.context.removeEventListener("touchstart", this.squareSelectListener)
198
- this.context.removeEventListener("touchend", this.squareSelectListener)
199
- this.squareSelectListener = undefined
200
- this.state.squareSelectEnabled = false
201
- this.view.visualizeInputState()
202
- }
203
-
204
155
  addExtension(extensionClass, props) {
205
156
  if(this.getExtension(extensionClass)) {
206
157
  throw Error("extension \"" + extensionClass.name + "\" already added")
@@ -219,9 +170,6 @@ export class Chessboard {
219
170
 
220
171
  destroy() {
221
172
  this.state.invokeExtensionPoints(EXTENSION_POINT.destroy)
222
- if (this.state.squareSelectEnabled) {
223
- this.disableSquareSelect()
224
- }
225
173
  this.positionAnimationsQueue.destroy()
226
174
  this.view.destroy()
227
175
  this.view = undefined
@@ -121,7 +121,7 @@ class MovePieceForm {
121
121
  this.moveButton = this.form.querySelector(".button-move")
122
122
  this.form.addEventListener("submit", (evt) => {
123
123
  evt.preventDefault()
124
- if (this.chessboard.view.moveInputCallback({
124
+ if (this.chessboard.state.moveInputCallback({
125
125
  chessboard: this.chessboard,
126
126
  type: INPUT_EVENT_TYPE.validateMoveInput,
127
127
  squareFrom: this.inputFrom.value,
@@ -12,12 +12,15 @@ export class ChessboardState {
12
12
  this.orientation = undefined
13
13
  this.inputWhiteEnabled = false
14
14
  this.inputBlackEnabled = false
15
- this.inputEnabled = false
16
- this.squareSelectEnabled = false
15
+ this.moveInputCallback = null
17
16
  this.extensionPoints = {}
18
17
  this.moveInputProcess = Promise.resolve()
19
18
  }
20
19
 
20
+ inputEnabled() {
21
+ return this.inputWhiteEnabled || this.inputBlackEnabled
22
+ }
23
+
21
24
  invokeExtensionPoints(name, data = {}) {
22
25
  const extensionPoints = this.extensionPoints[name]
23
26
  const dataCloned = Object.assign({}, data)
@@ -127,7 +127,7 @@ export class ChessboardView {
127
127
  this.redrawBoard()
128
128
  this.redrawPieces()
129
129
  }
130
- this.svg.setAttribute("width", "100%") // safari bugfix
130
+ this.svg.setAttribute("width", "100%")
131
131
  this.svg.setAttribute("height", "100%")
132
132
  }
133
133
 
@@ -306,7 +306,7 @@ export class ChessboardView {
306
306
  // enable and disable move input //
307
307
 
308
308
  enableMoveInput(eventHandler, color = null) {
309
- if (this.moveInputCallback) {
309
+ if (this.chessboard.state.moveInputCallback) {
310
310
  throw Error("moveInput already enabled")
311
311
  }
312
312
  if (color === COLOR.white) {
@@ -317,8 +317,7 @@ export class ChessboardView {
317
317
  this.chessboard.state.inputWhiteEnabled = true
318
318
  this.chessboard.state.inputBlackEnabled = true
319
319
  }
320
- this.chessboard.state.inputEnabled = true
321
- this.moveInputCallback = eventHandler
320
+ this.chessboard.state.moveInputCallback = eventHandler
322
321
  this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.moveInputToggled, {enabled: true, color: color})
323
322
  this.visualizeInputState()
324
323
  }
@@ -326,8 +325,7 @@ export class ChessboardView {
326
325
  disableMoveInput() {
327
326
  this.chessboard.state.inputWhiteEnabled = false
328
327
  this.chessboard.state.inputBlackEnabled = false
329
- this.chessboard.state.inputEnabled = false
330
- this.moveInputCallback = null
328
+ this.chessboard.state.moveInputCallback = null
331
329
  this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.moveInputToggled, {enabled: false})
332
330
  this.visualizeInputState()
333
331
  }
@@ -342,8 +340,8 @@ export class ChessboardView {
342
340
  squareFrom: square,
343
341
  piece: this.chessboard.getPiece(square)
344
342
  }
345
- if (this.moveInputCallback) {
346
- data.moveInputCallbackResult = this.moveInputCallback(data)
343
+ if (this.chessboard.state.moveInputCallback) {
344
+ data.moveInputCallbackResult = this.chessboard.state.moveInputCallback(data)
347
345
  }
348
346
  this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.moveInput, data)
349
347
  return data.moveInputCallbackResult
@@ -368,8 +366,8 @@ export class ChessboardView {
368
366
  squareTo: squareTo,
369
367
  piece: this.chessboard.getPiece(squareFrom)
370
368
  }
371
- if (this.moveInputCallback) {
372
- data.moveInputCallbackResult = this.moveInputCallback(data)
369
+ if (this.chessboard.state.moveInputCallback) {
370
+ data.moveInputCallbackResult = this.chessboard.state.moveInputCallback(data)
373
371
  }
374
372
  this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.moveInput, data)
375
373
  return data.moveInputCallbackResult
@@ -383,8 +381,8 @@ export class ChessboardView {
383
381
  squareFrom: squareFrom,
384
382
  squareTo: squareTo
385
383
  }
386
- if (this.moveInputCallback) {
387
- this.moveInputCallback(data)
384
+ if (this.chessboard.state.moveInputCallback) {
385
+ this.chessboard.state.moveInputCallback(data)
388
386
  }
389
387
  this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.moveInput, data)
390
388
  }
@@ -397,8 +395,8 @@ export class ChessboardView {
397
395
  squareTo: squareTo,
398
396
  legalMove: legalMove
399
397
  }
400
- if (this.moveInputCallback) {
401
- this.moveInputCallback(data)
398
+ if (this.chessboard.state.moveInputCallback) {
399
+ this.chessboard.state.moveInputCallback(data)
402
400
  }
403
401
  this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.moveInput, data)
404
402
  }
@@ -407,7 +405,7 @@ export class ChessboardView {
407
405
 
408
406
  visualizeInputState() {
409
407
  if (this.chessboard.state) { // fix https://github.com/shaack/cm-chessboard/issues/47
410
- if (this.chessboard.state.inputWhiteEnabled || this.chessboard.state.inputBlackEnabled || this.chessboard.state.squareSelectEnabled) {
408
+ if (this.chessboard.state.inputWhiteEnabled || this.chessboard.state.inputBlackEnabled) {
411
409
  this.boardGroup.setAttribute("class", "board input-enabled")
412
410
  } else {
413
411
  this.boardGroup.setAttribute("class", "board")
@@ -142,7 +142,7 @@ export class VisualMoveInput {
142
142
  if (MOVE_INPUT_STATE.pieceClickedThreshold !== prevState) {
143
143
  throw new Error("moveInputState")
144
144
  }
145
- if (this.view.chessboard.state.inputEnabled) {
145
+ if (this.view.chessboard.state.inputEnabled()) {
146
146
  this.view.setPieceVisibility(params.square, false)
147
147
  this.createDraggablePiece(params.piece)
148
148
  }
@@ -152,7 +152,7 @@ export class VisualMoveInput {
152
152
  if (MOVE_INPUT_STATE.secondClickThreshold !== prevState) {
153
153
  throw new Error("moveInputState")
154
154
  }
155
- if (this.view.chessboard.state.inputEnabled) {
155
+ if (this.view.chessboard.state.inputEnabled()) {
156
156
  this.view.setPieceVisibility(params.square, false)
157
157
  this.createDraggablePiece(params.piece)
158
158
  }
@@ -244,6 +244,7 @@ export class VisualMoveInput {
244
244
  }
245
245
  if(e.type === "mousedown") { // another try to prevent strange safari desktop select all bug
246
246
  e.preventDefault()
247
+ e.stopPropagation()
247
248
  }
248
249
  const square = e.target.getAttribute("data-square")
249
250
  if (!square) { // pointer on square
@@ -317,6 +318,7 @@ export class VisualMoveInput {
317
318
  pageY = e.pageY
318
319
  target = e.target
319
320
  e.preventDefault() // trying to prevent strange safari desktop bug, selecting the whole page on drag
321
+ e.stopPropagation()
320
322
  } else if (e.type === "touchmove") {
321
323
  clientX = e.touches[0].clientX
322
324
  clientY = e.touches[0].clientY
@@ -334,7 +336,7 @@ export class VisualMoveInput {
334
336
  } else {
335
337
  this.setMoveInputState(MOVE_INPUT_STATE.dragTo, {square: this.fromSquare, piece: this.movedPiece})
336
338
  }
337
- if (this.view.chessboard.state.inputEnabled) {
339
+ if (this.view.chessboard.state.inputEnabled()) {
338
340
  this.moveDraggablePiece(pageX, pageY)
339
341
  }
340
342
  }
@@ -353,7 +355,7 @@ export class VisualMoveInput {
353
355
  this.movingOverSquareCallback(this.fromSquare, null)
354
356
  }
355
357
 
356
- if (this.view.chessboard.state.inputEnabled && (this.moveInputState === MOVE_INPUT_STATE.dragTo || this.moveInputState === MOVE_INPUT_STATE.clickDragTo)) {
358
+ if (this.view.chessboard.state.inputEnabled() && (this.moveInputState === MOVE_INPUT_STATE.dragTo || this.moveInputState === MOVE_INPUT_STATE.clickDragTo)) {
357
359
  this.moveDraggablePiece(pageX, pageY)
358
360
  }
359
361
  }