cm-chessboard 7.11.0 → 8.0.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
@@ -246,32 +246,6 @@ chessboard.enableMoveInput((event) => {
246
246
 
247
247
  Disables moves via user input.
248
248
 
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
249
  ## Piece sets
276
250
 
277
251
  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.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
@@ -153,54 +153,6 @@ export class Chessboard {
153
153
  this.view.disableMoveInput()
154
154
  }
155
155
 
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
156
  addExtension(extensionClass, props) {
205
157
  if(this.getExtension(extensionClass)) {
206
158
  throw Error("extension \"" + extensionClass.name + "\" already added")
@@ -219,9 +171,6 @@ export class Chessboard {
219
171
 
220
172
  destroy() {
221
173
  this.state.invokeExtensionPoints(EXTENSION_POINT.destroy)
222
- if (this.state.squareSelectEnabled) {
223
- this.disableSquareSelect()
224
- }
225
174
  this.positionAnimationsQueue.destroy()
226
175
  this.view.destroy()
227
176
  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)
@@ -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
  }
@@ -334,7 +334,7 @@ export class VisualMoveInput {
334
334
  } else {
335
335
  this.setMoveInputState(MOVE_INPUT_STATE.dragTo, {square: this.fromSquare, piece: this.movedPiece})
336
336
  }
337
- if (this.view.chessboard.state.inputEnabled) {
337
+ if (this.view.chessboard.state.inputEnabled()) {
338
338
  this.moveDraggablePiece(pageX, pageY)
339
339
  }
340
340
  }
@@ -353,7 +353,7 @@ export class VisualMoveInput {
353
353
  this.movingOverSquareCallback(this.fromSquare, null)
354
354
  }
355
355
 
356
- if (this.view.chessboard.state.inputEnabled && (this.moveInputState === MOVE_INPUT_STATE.dragTo || this.moveInputState === MOVE_INPUT_STATE.clickDragTo)) {
356
+ if (this.view.chessboard.state.inputEnabled() && (this.moveInputState === MOVE_INPUT_STATE.dragTo || this.moveInputState === MOVE_INPUT_STATE.clickDragTo)) {
357
357
  this.moveDraggablePiece(pageX, pageY)
358
358
  }
359
359
  }