cm-chessboard 4.1.12 → 4.3.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
@@ -16,7 +16,8 @@ aspects of chess games.
16
16
  - [Can handle moves input via click or drag](https://shaack.com/projekte/cm-chessboard/examples/validate-moves.html)
17
17
  - [Styleable via css](https://shaack.com/projekte/cm-chessboard/examples/different-styles.html)
18
18
  - [Supports multiple piece sets](https://shaack.com/projekte/cm-chessboard/examples/different-styles.html)
19
- - [Supports an optimized usage for visually impaired people](https://shaack.com/projekte/cm-chessboard/examples/accessible-chessboard.html) 🆕
19
+ - [Supports an optimized usage for visually impaired people](https://shaack.com/projekte/cm-chessboard/examples/accessible-chessboard.html)
20
+ 🆕
20
21
  - Uses SVG for rendering
21
22
  - Vanilla JavaScript modules in ECMAScript 6 syntax
22
23
  - **No dependencies**
@@ -48,7 +49,7 @@ dependencies.
48
49
 
49
50
  > With the new version 4 of cm-chessboard I completely redesigned the animation of positions with
50
51
  > the use of promises.
51
-
52
+
52
53
  The pieces animations are now smoother and less error-prone for race conditions.
53
54
 
54
55
  As of version 4.x, the API functions `setPosition()`, `setPiece()` and `movePiece()` are **not animated** as default.
@@ -84,24 +85,24 @@ Below is the default configuration
84
85
 
85
86
  ```javascript
86
87
  let defaultProps = {
87
- position: "empty", // set as fen, "start" or "empty"
88
- orientation: COLOR.white, // white on bottom
89
- responsive: true, // resize the board automatically to the size of the context element
90
- animationDuration: 300, // pieces animation duration in milliseconds. Disable all animation with `0`.
91
- language: navigator.language.substring(0,2).toLowerCase(), // supports "de" and "en" for now, used for pieces naming
92
- style: {
93
- cssClass: "default", // set the css theme of the board, try "green", "blue" or "chess-club"
94
- showCoordinates: true, // show ranks and files
95
- borderType: BORDER_TYPE.none, // "thin" thin border, "frame" wide border with coordinates in it, "none" no border
96
- aspectRatio: 1, // height/width of the board
97
- moveFromMarker: MARKER_TYPE.frame, // the marker used to mark the start square
98
- moveToMarker: MARKER_TYPE.frame, // the marker used to mark the square where the figure is moving to
99
- },
100
- sprite: {
101
- url: "./assets/images/chessboard-sprite.svg", // pieces and markers are stored in a sprite file
102
- size: 40, // the sprite tiles size, defaults to 40x40px
103
- cache: true // cache the sprite
104
- }
88
+ position: "empty", // set as fen, "start" or "empty"
89
+ orientation: COLOR.white, // white on bottom
90
+ responsive: true, // resize the board automatically to the size of the context element
91
+ animationDuration: 300, // pieces animation duration in milliseconds. Disable all animation with `0`.
92
+ language: navigator.language.substring(0, 2).toLowerCase(), // supports "de" and "en" for now, used for pieces naming
93
+ style: {
94
+ cssClass: "default", // set the css theme of the board, try "green", "blue" or "chess-club"
95
+ showCoordinates: true, // show ranks and files
96
+ borderType: BORDER_TYPE.none, // "thin" thin border, "frame" wide border with coordinates in it, "none" no border
97
+ aspectRatio: 1, // height/width of the board
98
+ moveFromMarker: MARKER_TYPE.frame, // the marker used to mark the start square
99
+ moveToMarker: MARKER_TYPE.frame, // the marker used to mark the square where the figure is moving to
100
+ },
101
+ sprite: {
102
+ url: "./assets/images/chessboard-sprite.svg", // pieces and markers are stored in a sprite file
103
+ size: 40, // the sprite tiles size, defaults to 40x40px
104
+ cache: true // cache the sprite
105
+ }
105
106
  }
106
107
  ```
107
108
 
@@ -330,7 +331,64 @@ chessboard.removeMarkers(undefined, myMarkerType)
330
331
 
331
332
  ## Extensions
332
333
 
333
- cm-chessboard provides the ability to extend its functionality with extensions.
334
+ cm-chessboard provides the ability to extend its functionality with extensions. Extensions extend the class `Extension`
335
+ and have access to the chessboard and can register extension points.
336
+
337
+ ```js
338
+ this.registerExtensionPoint(EXTENSION_POINT.moveInputStateChanged, (data) => {
339
+ // do something on move [start | cancel | done]
340
+ console.log(data)
341
+ })
342
+ ```
343
+
344
+ Enable extension via the chessboard props.
345
+
346
+ ```js
347
+ const chessboard = new Chessboard(document.getElementById("board"), {
348
+ position: "start",
349
+ extensions: // list of used extensions
350
+ [{
351
+ class: ExtensionName, // this class of the extionsion
352
+ props: {
353
+ // configure your extions via its properties
354
+ }
355
+
356
+ }]
357
+ })
358
+ ```
359
+
360
+ ### Extension `Accessibility`
361
+
362
+ This extension ensures that visual impaired people can better use the chessboard. It displays the braille notation
363
+ of the current position in the alt tag of the board image and enables a form to move the pieces via text input. It
364
+ can also display the board as HTML table and the pieces as list.
365
+
366
+ See example [Accessibility extension](https://shaack.com/projekte/cm-chessboard/examples/accessible-chessboard.html)
367
+
368
+ #### Usage
369
+
370
+ ```js
371
+ const chessboard = new Chessboard(document.getElementById("board"), {
372
+ position: "start",
373
+ sprite: {url: "../assets/images/chessboard-sprite.svg"},
374
+ // animationDuration: 0, // optional, set to 0 to disable animations
375
+ style: {
376
+ cssClass: "default-contrast" // make the coordinates better visible with the "default-contrast" theme
377
+ },
378
+ extensions:
379
+ [{
380
+ class: Accessibility,
381
+ props: {
382
+ brailleNotationInAlt: true, // show the braille notation of the game in the alt attribute of the SVG
383
+ boardAsTable: true, // display the board additionally as HTML table
384
+ movePieceForm: true, // display a form to move a piece (from, to, move)
385
+ piecesAsList: true, // display the pieces additionally as List
386
+ visuallyHidden: false // hide all those extra outputs visually but keep them accessible for screen readers and braille displays
387
+ }
388
+
389
+ }]
390
+ })
391
+ ```
334
392
 
335
393
  ## Usage with React
336
394
 
@@ -9,13 +9,21 @@
9
9
  </head>
10
10
  <body>
11
11
  <h1><a href="../">cm-chessboard</a></h1>
12
- <h2>Example: Accessibility features enabled</h2>
12
+ <h2>Example: Accessibility extension</h2>
13
13
  <p>
14
- This example shows, how the accessibility features from cm-chessboard work.
15
- They allow a better usage of the board for visually impaired
16
- people. You can enable these features via configuration.
14
+ This example shows, how the accessibility extension from cm-chessboard works.
15
+ It allows a better usage of the board for visually impaired
16
+ people.
17
17
  </p>
18
-
18
+ <h3>Features of the Accessibility extension</h3>
19
+ <ul>
20
+ <li>Enable Braille notation in the alt text of the chessboard image</li>
21
+ <li>Display the chessboard additionally as HTML table</li>
22
+ <li>Display a form to move a piece (from, to, move)</li>
23
+ <li>Display the pieces additionally as List</li>
24
+ <li>Display these features visually hidden or not</li>
25
+ </ul>
26
+ <h3>The chessboard</h3>
19
27
  <div class="board green" id="board"></div>
20
28
  <button style="margin-bottom: 10px"
21
29
  onclick="window.setOrientation()">Switch Orientation
@@ -27,21 +35,26 @@
27
35
  const chessboard = new Chessboard(document.getElementById("board"), {
28
36
  position: "start",
29
37
  sprite: {url: "../assets/images/chessboard-sprite.svg"},
38
+ // animationDuration: 0, // optional, set to 0 to disable animations
30
39
  style: {
31
- cssClass: "default-contrast"
32
- }
40
+ cssClass: "default-contrast" // make the coordinates better visible with the "default-contrast" theme
41
+ },
42
+ extensions:
43
+ [{
44
+ class: Accessibility,
45
+ props: {
46
+ brailleNotationInAlt: true, // show the braille notation of the game in the alt attribute of the SVG
47
+ boardAsTable: true, // display the board additionally as HTML table
48
+ movePieceForm: true, // display a form to move a piece (from, to, move)
49
+ piecesAsList: true, // display the pieces additionally as List
50
+ visuallyHidden: false // hide all those extra outputs visually but keep them accessible for screen readers and braille displays
51
+ }
52
+
53
+ }]
33
54
  })
34
55
  window.setOrientation = function () {
35
56
  chessboard.setOrientation(chessboard.getOrientation() === 'w' ? 'b' : 'w')
36
57
  }
37
- // Extension
38
- new Accessibility(chessboard, {
39
- brailleNotationInAlt: true, // show the braille notation of the game in the alt attribute of the SVG
40
- boardAsTable: true, // display the board additionally as HTML table
41
- movePieceForm: true, // display a form to move a piece (from, to, move)
42
- piecesAsList: true, // display the pieces additionally as List
43
- visuallyHidden: false // hide all those extra outputs visually but keep them accessible for screen readers and braille displays
44
- })
45
58
 
46
59
  chessboard.enableMoveInput(inputHandler)
47
60
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cm-chessboard",
3
- "version": "4.1.12",
3
+ "version": "4.3.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",
@@ -53,7 +53,7 @@ export class Chessboard {
53
53
  orientation: COLOR.white, // white on bottom
54
54
  responsive: true, // resize the board automatically to the size of the context element
55
55
  animationDuration: 300, // pieces animation duration in milliseconds. Disable all animation with `0`.
56
- language: navigator.language.substring(0,2).toLowerCase(), // supports "de" and "en" for now, used for pieces naming
56
+ language: navigator.language.substring(0, 2).toLowerCase(), // supports "de" and "en" for now, used for pieces naming
57
57
  style: {
58
58
  cssClass: "default", // set the css theme of the board, try "green", "blue" or "chess-club"
59
59
  showCoordinates: true, // show ranks and files
@@ -67,27 +67,21 @@ export class Chessboard {
67
67
  size: 40, // the sprite tiles size, defaults to 40x40px
68
68
  cache: true // cache the sprite
69
69
  },
70
- accessibility: {
71
- movePieceForm: false, // display a form to move a piece (from, to, move)
72
- boardAsTable: false, // display the board additionally as HTML table
73
- piecesAsList: false, // display the pieces additionally as List
74
- visuallyHidden: true // hide all those extra outputs visually but keep them accessible for screen readers and braille displays
75
- }
70
+ extensions: [ /* {class: ExtensionClass, props: { ... }} */ ] // add extensions here like this
76
71
  }
77
72
  this.props = {}
78
73
  Object.assign(this.props, defaultProps)
79
74
  Object.assign(this.props, props)
80
75
  this.props.sprite = defaultProps.sprite
81
76
  this.props.style = defaultProps.style
82
- this.props.accessibility = defaultProps.accessibility
83
77
  if (props.sprite) {
84
78
  Object.assign(this.props.sprite, props.sprite)
85
79
  }
86
80
  if (props.style) {
87
81
  Object.assign(this.props.style, props.style)
88
82
  }
89
- if (props.accessibility) {
90
- Object.assign(this.props.accessibility, props.accessibility)
83
+ if (props.extensions) {
84
+ this.props.extensions = props.extensions
91
85
  }
92
86
  if (this.props.language !== "de" && this.props.language !== "en") {
93
87
  this.props.language = "en"
@@ -95,12 +89,15 @@ export class Chessboard {
95
89
 
96
90
  this.state = new ChessboardState()
97
91
  this.view = new ChessboardView(this)
98
- // callback Extension
99
92
  this.positionAnimationsQueue = new PositionAnimationsQueue(this)
100
93
  this.state.orientation = this.props.orientation
101
94
  this.view.redrawBoard()
102
95
  this.state.position = new Position(this.props.position)
103
96
  this.view.redrawPieces()
97
+ // instantiate extensions
98
+ for (const extensionData of this.props.extensions) {
99
+ new extensionData.class(this, extensionData.props)
100
+ }
104
101
  }
105
102
 
106
103
  // API //
@@ -128,7 +125,7 @@ export class Chessboard {
128
125
 
129
126
  async setOrientation(color, animated = false) {
130
127
  const position = this.state.position.clone()
131
- if(this.boardTurning) {
128
+ if (this.boardTurning) {
132
129
  console.log("setOrientation is only once in queue allowed")
133
130
  return
134
131
  }
@@ -98,7 +98,7 @@ export class Accessibility extends Extension {
98
98
  this.updateFormInputs()
99
99
  })
100
100
  this.registerExtensionPoint(EXTENSION_POINT.positionChanged, () => {
101
- if(!this.chessboard.state.destroyed) {
101
+ if(this.chessboard.state) { // not destroyed
102
102
  this.redrawPositionInAltAttribute()
103
103
  if (this.props.boardAsTable) {
104
104
  this.redrawBoardAsTable()
@@ -108,7 +108,7 @@ export class Accessibility extends Extension {
108
108
  }
109
109
  }
110
110
  })
111
- setTimeout(() => {
111
+ setTimeout(() => { // todo replace with something like this.chessboard.initialized.then(() => { ... })
112
112
  this.updateFormInputs()
113
113
  this.redrawPositionInAltAttribute()
114
114
  if (this.props.boardAsTable) {
@@ -66,12 +66,14 @@ export class ChessboardState {
66
66
  }
67
67
  }
68
68
 
69
- invokeExtensionPoints(name, data = undefined) {
69
+ invokeExtensionPoints(name, data = {}) {
70
70
  const extensionPoints = this.extensionPoints[name]
71
+ const dataCloned = Object.assign({}, data);
72
+ dataCloned.extensionPoint = name
71
73
  if(extensionPoints) {
72
74
  for (const extensionPoint of extensionPoints) {
73
75
  setTimeout(() => {
74
- extensionPoint(data)
76
+ extensionPoint(dataCloned)
75
77
  })
76
78
  }
77
79
  }