cm-chessboard 5.0.0 → 5.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.
package/README.md CHANGED
@@ -10,15 +10,17 @@ aspects of chess games.
10
10
 
11
11
  ## Features
12
12
 
13
- - [Mobile friendly and responsive](https://shaack.com/projekte/cm-chessboard/examples/responsive-board.html)
13
+ - **No dependencies**, just clean ES6
14
14
  - [Can handle moves input via click or drag](https://shaack.com/projekte/cm-chessboard/examples/validate-moves.html)
15
- - [Styleable via css](https://shaack.com/projekte/cm-chessboard/examples/different-styles.html)
16
- - [Supports multiple piece sets](https://shaack.com/projekte/cm-chessboard/examples/different-styles.html)
17
- - [Supports an optimized usage for visually impaired people](https://shaack.com/projekte/cm-chessboard/examples/extensions/chessboard-accessibility-extension.html)
18
- 🆕
15
+ - [Styleable via css and supports multiple piece sets](https://shaack.com/projekte/cm-chessboard/examples/different-styles.html)
19
16
  - Uses SVG for rendering
20
- - Vanilla JavaScript modules in ECMAScript 6 syntax
21
- - **No dependencies**
17
+ - [Allows adding **
18
+ extensions** to extend the functionality](https://shaack.com/projekte/cm-chessboard/examples/extensions/chessboard-arrows-extension.html)
19
+
20
+ ## Extensions 🆕
21
+
22
+ - [Accessibility Extension](https://shaack.com/projekte/cm-chessboard/examples/extensions/chessboard-accessibility-extension.html)
23
+ - [Arrows Extension](https://shaack.com/projekte/cm-chessboard/examples/extensions/chessboard-arrows-extension.html)
22
24
 
23
25
  ## Demo and repository
24
26
 
@@ -43,18 +45,6 @@ with `{sprite.url: "./url/of/chessboard-sprite.svg"}`
43
45
  To run the unit tests in `/test` you first have to `npm install` the dev dependencies. Without tests there are no
44
46
  dependencies.
45
47
 
46
- ## Version 4.x has many changes
47
-
48
- > With the new version 4 of cm-chessboard I completely redesigned the animation of positions with
49
- > the use of promises.
50
-
51
- The pieces animations are now smoother and less error-prone for race conditions.
52
-
53
- As of version 4.x, the API functions `setPosition()`, `setPiece()` and `movePiece()` are **not animated** as default.
54
- If you want some pieces animations you have to give the parameter `animated = true`.
55
-
56
- And with 4.x the chessboard has become more accessible for visually impaired people.
57
-
58
48
  ## Usage
59
49
 
60
50
  Preconditions for using cm-chessboard in a web page:
@@ -142,7 +132,9 @@ Sets the position as `fen`. Special values are "start", sets the chess start pos
142
132
 
143
133
  Returns the board position as `fen`.
144
134
 
145
- ### addMarker(square, type)
135
+ ### addMarker(type, square)
136
+
137
+ > signature changed with V5.1 from (square, type) to (type, square)
146
138
 
147
139
  Adds a marker on a square.
148
140
 
@@ -159,19 +151,23 @@ below.
159
151
  [Example for **addMarker**, **getMarkers** and
160
152
  **removeMarkers**](https://shaack.com/projekte/cm-chessboard/examples/context-input.html)
161
153
 
162
- ### getMarkers(square = undefined, type = undefined)
154
+ ### getMarkers(type = undefined, square = undefined)
155
+
156
+ > signature changed with V5.1 from (square, type) to (type, square)
163
157
 
164
158
  Returns the board's markers as an array.
165
159
 
166
- Set square to `undefined`, to get all markers of a type on the board. Set type to `undefined`, to get all types.
167
- Set `both` to undefined to get all markers on the board.
160
+ Only set type, to get all markers of a type on the board. Set type to `undefined`, to get markers of all types on a square.
161
+ Set `both` to `undefined` to get all markers on the board.
168
162
 
169
- ### removeMarkers(square = undefined, type = undefined)
163
+ ### removeMarkers(type = undefined, square = undefined)
164
+
165
+ > signature changed with V5.1 from (square, type) to (type, square)
170
166
 
171
167
  Removes markers from the board.
172
168
 
173
- Set `square` to `undefined` to remove markers of `type` from all squares. Set `type` to `undefined`, to remove all types
174
- from a square. Set both to `undefined` to remove all markers from the board.
169
+ Only set `type` to remove all markers of `type` from the board. Set `type` to `undefined`, to remove all types
170
+ of markers from a square. Call without parameters to remove all markers from the board.
175
171
 
176
172
  ### setOrientation(color)
177
173
 
@@ -333,6 +329,8 @@ chessboard.removeMarkers(undefined, myMarkerType)
333
329
  cm-chessboard provides the ability to extend its functionality with extensions. Extensions extend the class `Extension`
334
330
  and have access to the chessboard and can register extension points.
335
331
 
332
+ ### registerExtensionPoint(name, callback)
333
+
336
334
  ```js
337
335
  class MyCoolChessboardExtension extends Extension {
338
336
  constructor(chessboard, props) {
@@ -349,11 +347,11 @@ Currently possible extension points are defined in `Extension.js`.
349
347
 
350
348
  ```js
351
349
  export const EXTENSION_POINT = {
352
- positionChanged: "positionChanged", // the positions of the pieces was changed
353
- boardChanged: "boardChanged", // the board (orientation) was changed
354
- moveInputToggled: "moveInputToggled", // move input was enabled or disabled
355
- moveInput: "moveInput", // move started, cancelled or done
356
- destroy: "destroy" // called, before the board is destroyed
350
+ positionChanged: "positionChanged", // the positions of the pieces was changed
351
+ boardChanged: "boardChanged", // the board (orientation) was changed
352
+ moveInputToggled: "moveInputToggled", // move input was enabled or disabled
353
+ moveInput: "moveInput", // move started, cancelled or done
354
+ destroy: "destroy" // called, before the board is destroyed
357
355
  }
358
356
  ```
359
357
 
@@ -372,15 +370,30 @@ const chessboard = new Chessboard(document.getElementById("board"), {
372
370
  })
373
371
  ```
374
372
 
375
- ### `Accessibility` extension
373
+ ### registerMethod(name, callback)
374
+
375
+ Add methods to the main chessboard from your extension with `this.registerMethod("name", callback)`
376
+ like `addArrow(from, to, type)` in the
377
+ [Arrows extension](https://shaack.com/projekte/cm-chessboard/examples/extensions/chessboard-arrows-extension.html).
378
+
379
+ ```js
380
+ this.registerMethod("addArrow", this.addArrow)
381
+ ```
382
+
383
+ ### Existing extensions
384
+
385
+ cm-chessboard is shipped with these extensions.
386
+
387
+ #### Accessibility Extension
376
388
 
377
389
  This extension ensures that visual impaired people can better use the chessboard. It displays the braille notation
378
390
  of the current position in the alt tag of the board image and enables a form to move the pieces via text input. It
379
391
  can also display the board as HTML table and the pieces as list.
380
392
 
381
- See example [Accessibility extension](https://shaack.com/projekte/cm-chessboard/examples/extensions/chessboard-accessibility-extension.html)
393
+ See
394
+ example [Accessibility extension](https://shaack.com/projekte/cm-chessboard/examples/extensions/chessboard-accessibility-extension.html)
382
395
 
383
- #### Usage
396
+ ##### Usage
384
397
 
385
398
  ```js
386
399
  const chessboard = new Chessboard(document.getElementById("board"), {
@@ -405,11 +418,12 @@ const chessboard = new Chessboard(document.getElementById("board"), {
405
418
  })
406
419
  ```
407
420
 
408
- ### `Arrows` extension
421
+ #### Arrows extension
409
422
 
410
423
  To draw arrows on the board.
411
424
 
412
- See example [Arrows extension](https://shaack.com/projekte/cm-chessboard/examples/extensions/chessboard-arrows-extension.html)
425
+ See
426
+ example [Arrows extension](https://shaack.com/projekte/cm-chessboard/examples/extensions/chessboard-arrows-extension.html)
413
427
 
414
428
  ## Usage with React
415
429
 
@@ -31,10 +31,10 @@ const chessboard = new Chessboard(document.getElementById("board"), {
31
31
 
32
32
  }]
33
33
  })
34
- chessboard.addArrow("f3", "d5", ARROW_TYPE.default);
35
- chessboard.addArrow("b8", "c6", ARROW_TYPE.default);
36
- chessboard.addArrow("d2", "d3", ARROW_TYPE.pointy);
37
- chessboard.addArrow("g5", "e6", ARROW_TYPE.danger);
34
+ chessboard.addArrow(ARROW_TYPE.default, "f3", "d5");
35
+ chessboard.addArrow(ARROW_TYPE.default, "b8", "c6");
36
+ chessboard.addArrow(ARROW_TYPE.pointy, "d2", "d3");
37
+ chessboard.addArrow(ARROW_TYPE.danger, "g5", "e6");
38
38
  </pre>
39
39
 
40
40
  <script type="module">
@@ -55,10 +55,10 @@ chessboard.addArrow("g5", "e6", ARROW_TYPE.danger);
55
55
 
56
56
  }]
57
57
  })
58
- chessboard.addArrow("f3", "d5", ARROW_TYPE.default);
59
- chessboard.addArrow("b8", "c6", ARROW_TYPE.default);
60
- chessboard.addArrow("d2", "d3", ARROW_TYPE.pointy);
61
- chessboard.addArrow("g5", "e6", ARROW_TYPE.danger);
58
+ chessboard.addArrow(ARROW_TYPE.default, "f3", "d5");
59
+ chessboard.addArrow(ARROW_TYPE.default, "b8", "c6");
60
+ chessboard.addArrow(ARROW_TYPE.pointy, "d2", "d3");
61
+ chessboard.addArrow(ARROW_TYPE.danger, "g5", "e6");
62
62
  </script>
63
63
  </body>
64
64
  </html>
@@ -22,11 +22,11 @@
22
22
  } else {
23
23
  markerType = MARKER_TYPE.circle
24
24
  }
25
- const markersOnSquare = board1.getMarkers(event.square, markerType)
25
+ const markersOnSquare = board1.getMarkers(markerType, event.square)
26
26
  if (markersOnSquare.length > 0) {
27
- board1.removeMarkers(event.square, markerType)
27
+ board1.removeMarkers(markerType, event.square)
28
28
  } else {
29
- board1.addMarker(event.square, markerType)
29
+ board1.addMarker(markerType, event.square)
30
30
  }
31
31
  })
32
32
  </pre>
@@ -45,11 +45,11 @@
45
45
  } else {
46
46
  markerType = MARKER_TYPE.circle
47
47
  }
48
- const markersOnSquare = board1.getMarkers(event.square, markerType)
48
+ const markersOnSquare = board1.getMarkers(markerType, event.square)
49
49
  if (markersOnSquare.length > 0) {
50
- board1.removeMarkers(event.square, markerType)
50
+ board1.removeMarkers(markerType, event.square)
51
51
  } else {
52
- board1.addMarker(event.square, markerType)
52
+ board1.addMarker(markerType, event.square)
53
53
  }
54
54
  })
55
55
 
@@ -65,11 +65,11 @@
65
65
  const marker = {class: "markerCircleRed", slice: "markerCircle"}
66
66
  board2.enableSquareSelect((event) => {
67
67
  if(event.type === SQUARE_SELECT_TYPE.secondary) {
68
- const markersOnSquare = board2.getMarkers(event.square, marker)
68
+ const markersOnSquare = board2.getMarkers(marker, event.square)
69
69
  if (markersOnSquare.length > 0) {
70
- board2.removeMarkers(event.square, marker)
70
+ board2.removeMarkers(marker, event.square)
71
71
  } else {
72
- board2.addMarker(event.square, marker)
72
+ board2.addMarker(marker, event.square)
73
73
  }
74
74
  }
75
75
  })
@@ -33,13 +33,13 @@ const chess = new Chess()
33
33
 
34
34
  function inputHandler(event) {
35
35
  console.log("event", event)
36
- event.chessboard.removeMarkers(undefined, MARKER_TYPE.dot)
37
- event.chessboard.removeMarkers(undefined, MARKER_TYPE.square)
36
+ event.chessboard.removeMarkers(MARKER_TYPE.dot)
37
+ event.chessboard.removeMarkers(MARKER_TYPE.square)
38
38
  if (event.type === INPUT_EVENT_TYPE.moveStart) {
39
39
  const moves = chess.moves({square: event.square, verbose: true});
40
- event.chessboard.addMarker(event.square, MARKER_TYPE.square)
40
+ event.chessboard.addMarker(MARKER_TYPE.square, event.square)
41
41
  for (const move of moves) { // draw dots on possible moves
42
- event.chessboard.addMarker(move.to, MARKER_TYPE.dot)
42
+ event.chessboard.addMarker(MARKER_TYPE.dot, move.to)
43
43
  }
44
44
  return moves.length > 0
45
45
  } else if (event.type === INPUT_EVENT_TYPE.moveDone) {
@@ -81,11 +81,11 @@ board.enableMoveInput(inputHandler, COLOR.white)
81
81
 
82
82
  function inputHandler(event) {
83
83
  console.log("event", event)
84
- event.chessboard.removeMarkers(undefined, MARKER_TYPE.dot)
84
+ event.chessboard.removeMarkers(MARKER_TYPE.dot)
85
85
  if (event.type === INPUT_EVENT_TYPE.moveStart) {
86
86
  const moves = chess.moves({square: event.square, verbose: true});
87
87
  for (const move of moves) { // draw dots on possible squares
88
- event.chessboard.addMarker(move.to, MARKER_TYPE.dot)
88
+ event.chessboard.addMarker(MARKER_TYPE.dot, move.to)
89
89
  }
90
90
  return moves.length > 0
91
91
  } else if (event.type === INPUT_EVENT_TYPE.moveDone) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cm-chessboard",
3
- "version": "5.0.0",
3
+ "version": "5.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",
@@ -12,6 +12,7 @@
12
12
  "module",
13
13
  "widget"
14
14
  ],
15
+ "type": "module",
15
16
  "browser": "./src/cm-chessboard/Chessboard.js",
16
17
  "scripts": {
17
18
  "test": "tput setaf 4;echo 'Run test/index.html in your browser for unit testing.'; echo ''; exit 0"
@@ -67,7 +67,7 @@ export class Chessboard {
67
67
  size: 40, // the sprite tiles size, defaults to 40x40px
68
68
  cache: true // cache the sprite
69
69
  },
70
- extensions: [ /* {class: ExtensionClass, props: { ... }} */ ] // add extensions here
70
+ extensions: [ /* {class: ExtensionClass, props: { ... }} */] // add extensions here
71
71
  }
72
72
  this.props = {}
73
73
  Object.assign(this.props, defaultProps)
@@ -149,15 +149,20 @@ export class Chessboard {
149
149
  return this.state.orientation
150
150
  }
151
151
 
152
- addMarker(square, type) {
153
- if (!type) {
154
- console.error("Error addMarker(), type is " + type)
152
+ addMarker(type, square) {
153
+ if (typeof type === "string" || typeof square === "object") { // todo remove 2022-12-01
154
+ console.error("changed the signature of `addMarker` to `(type, square)` with v5.1.x")
155
+ return
155
156
  }
156
157
  this.state.addMarker(square, type)
157
158
  this.view.drawMarkers()
158
159
  }
159
160
 
160
- getMarkers(square = undefined, type = undefined) {
161
+ getMarkers(type = undefined, square = undefined) {
162
+ if (typeof type === "string" || typeof square === "object") { // todo remove 2022-12-01
163
+ console.error("changed the signature of `getMarkers` to `(type, square)` with v5.1.x")
164
+ return
165
+ }
161
166
  const markersFound = []
162
167
  this.state.markers.forEach((marker) => {
163
168
  const markerSquare = marker.square
@@ -170,7 +175,11 @@ export class Chessboard {
170
175
  return markersFound
171
176
  }
172
177
 
173
- removeMarkers(square = undefined, type = undefined) {
178
+ removeMarkers(type = undefined, square = undefined) {
179
+ if (typeof type === "string" || typeof square === "object") { // todo remove 2022-12-01
180
+ console.error("changed the signature of `removeMarkers` to `(type, square)` with v5.1.x")
181
+ return
182
+ }
174
183
  this.state.removeMarkers(square, type)
175
184
  this.view.drawMarkers()
176
185
  }
@@ -97,12 +97,20 @@ export class Arrows extends Extension {
97
97
 
98
98
  }
99
99
 
100
- addArrow(from, to, type) {
100
+ addArrow(type, from, to) {
101
+ if (typeof type === "string" || typeof from === "object" || typeof to === "object") { // todo remove 2022-12-01
102
+ console.error("changed the signature of `addArrow` to `(type, from, to)` with v5.1.x")
103
+ return
104
+ }
101
105
  this.arrows.push(new Arrow(from, to, type))
102
106
  this.chessboard.view.redrawBoard()
103
107
  }
104
108
 
105
- getArrows(from = undefined, to = undefined, type = undefined) {
109
+ getArrows(type = undefined, from = undefined, to = undefined) {
110
+ if (typeof type === "string" || typeof from === "object" || typeof to === "object") { // todo remove 2022-12-01
111
+ console.error("changed the signature of `getArrows` to `(type, from, to)` with v5.1.x")
112
+ return
113
+ }
106
114
  let arrows = []
107
115
  this.arrows.forEach((arrow) => {
108
116
  if (arrow.matches(from, to, type)) {
@@ -112,7 +120,11 @@ export class Arrows extends Extension {
112
120
  return arrows
113
121
  }
114
122
 
115
- removeArrows(from = undefined, to = undefined, type = undefined) {
123
+ removeArrows(type = undefined, from = undefined, to = undefined) {
124
+ if (typeof type === "string" || typeof from === "object" || typeof to === "object") { // todo remove 2022-12-01
125
+ console.error("changed the signature of `removeArrows` to `(type, from, to)` with v5.1.x")
126
+ return
127
+ }
116
128
  this.arrows = this.arrows.filter((arrow) => !arrow.matches(from, to, type))
117
129
  this.chessboard.view.redrawBoard()
118
130
  }
@@ -132,6 +144,6 @@ class Arrow {
132
144
  if (to && to !== this.to) {
133
145
  return false
134
146
  }
135
- return !(type && type !== this.type);
147
+ return !(type && type !== this.type)
136
148
  }
137
149
  }
@@ -14,27 +14,27 @@ describe("TestMarkers", () => {
14
14
  sprite: {url: "../assets/images/chessboard-sprite.svg"},
15
15
  position: "empty"
16
16
  })
17
- chessboard.addMarker("e5", MARKER_TYPE.square)
18
- chessboard.addMarker("b6", MARKER_TYPE.frame)
19
- chessboard.addMarker("h6", MARKER_TYPE.frame)
20
- assert.equal(chessboard.getMarkers().length,3)
21
- const markersE5 = chessboard.getMarkers("e5")
17
+ chessboard.addMarker(MARKER_TYPE.square, "e5")
18
+ chessboard.addMarker(MARKER_TYPE.frame, "b6")
19
+ chessboard.addMarker(MARKER_TYPE.frame, "h6")
20
+ assert.equal(chessboard.getMarkers().length, 3)
21
+ const markersE5 = chessboard.getMarkers(undefined,"e5")
22
22
  assert.equal(markersE5.length, 1)
23
23
  assert.equal(markersE5[0].square, "e5")
24
24
  assert.equal(markersE5[0].type.slice, "markerSquare")
25
- assert.equal(chessboard.getMarkers(undefined, MARKER_TYPE.square).length, 1)
26
- assert.equal(chessboard.getMarkers("a4").length, 0)
27
- assert.equal(chessboard.getMarkers(undefined, MARKER_TYPE.frame).length, 2)
28
- assert.equal(chessboard.getMarkers("b6", MARKER_TYPE.frame).length, 1)
29
- assert.equal(chessboard.getMarkers("h6", MARKER_TYPE.frame).length, 1)
30
- chessboard.removeMarkers("h6")
31
- assert.equal(chessboard.getMarkers("h6", MARKER_TYPE.frame).length, 0)
32
- chessboard.addMarker("h6", MARKER_TYPE.frame)
33
- assert.equal(chessboard.getMarkers("h6", MARKER_TYPE.frame).length, 1)
34
- chessboard.removeMarkers("h6", MARKER_TYPE.square)
35
- assert.equal(chessboard.getMarkers("h6", MARKER_TYPE.frame).length, 1)
36
- chessboard.removeMarkers("h6", MARKER_TYPE.frame)
37
- assert.equal(chessboard.getMarkers("h6", MARKER_TYPE.frame).length, 0)
25
+ assert.equal(chessboard.getMarkers(MARKER_TYPE.square).length, 1)
26
+ assert.equal(chessboard.getMarkers(undefined, "a4").length, 0)
27
+ assert.equal(chessboard.getMarkers(MARKER_TYPE.frame).length, 2)
28
+ assert.equal(chessboard.getMarkers(MARKER_TYPE.frame, "b6").length, 1)
29
+ assert.equal(chessboard.getMarkers(MARKER_TYPE.frame, "h6").length, 1)
30
+ chessboard.removeMarkers(undefined, "h6")
31
+ assert.equal(chessboard.getMarkers(MARKER_TYPE.frame, "h6").length, 0)
32
+ chessboard.addMarker(MARKER_TYPE.frame, "h6")
33
+ assert.equal(chessboard.getMarkers(MARKER_TYPE.frame, "h6").length, 1)
34
+ chessboard.removeMarkers(MARKER_TYPE.square, "h6")
35
+ assert.equal(chessboard.getMarkers(MARKER_TYPE.frame, "h6").length, 1)
36
+ chessboard.removeMarkers(MARKER_TYPE.frame, "h6")
37
+ assert.equal(chessboard.getMarkers(MARKER_TYPE.frame, "h6").length, 0)
38
38
  chessboard.destroy()
39
39
  })
40
40