cm-chessboard 4.1.13 → 4.3.2

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.
@@ -88,7 +89,7 @@ let defaultProps = {
88
89
  orientation: COLOR.white, // white on bottom
89
90
  responsive: true, // resize the board automatically to the size of the context element
90
91
  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
+ language: navigator.language.substring(0, 2).toLowerCase(), // supports "de" and "en" for now, used for pieces naming
92
93
  style: {
93
94
  cssClass: "default", // set the css theme of the board, try "green", "blue" or "chess-club"
94
95
  showCoordinates: true, // show ranks and files
@@ -101,7 +102,8 @@ let defaultProps = {
101
102
  url: "./assets/images/chessboard-sprite.svg", // pieces and markers are stored in a sprite file
102
103
  size: 40, // the sprite tiles size, defaults to 40x40px
103
104
  cache: true // cache the sprite
104
- }
105
+ },
106
+ extensions: [ /* {class: ExtensionClass, props: { ... }} */ ] // add extensions here
105
107
  }
106
108
  ```
107
109
 
@@ -330,7 +332,64 @@ chessboard.removeMarkers(undefined, myMarkerType)
330
332
 
331
333
  ## Extensions
332
334
 
333
- cm-chessboard provides the ability to extend its functionality with extensions.
335
+ cm-chessboard provides the ability to extend its functionality with extensions. Extensions extend the class `Extension`
336
+ and have access to the chessboard and can register extension points.
337
+
338
+ ```js
339
+ this.registerExtensionPoint(EXTENSION_POINT.moveInputStateChanged, (data) => {
340
+ // do something on move [start | cancel | done]
341
+ console.log(data)
342
+ })
343
+ ```
344
+
345
+ Enable extension via the chessboard props.
346
+
347
+ ```js
348
+ const chessboard = new Chessboard(document.getElementById("board"), {
349
+ position: "start",
350
+ extensions: // list of used extensions
351
+ [{
352
+ class: ExtensionName, // this class of the extionsion
353
+ props: {
354
+ // configure your extions via its properties
355
+ }
356
+
357
+ }]
358
+ })
359
+ ```
360
+
361
+ ### Extension `Accessibility`
362
+
363
+ This extension ensures that visual impaired people can better use the chessboard. It displays the braille notation
364
+ of the current position in the alt tag of the board image and enables a form to move the pieces via text input. It
365
+ can also display the board as HTML table and the pieces as list.
366
+
367
+ See example [Accessibility extension](https://shaack.com/projekte/cm-chessboard/examples/accessible-chessboard.html)
368
+
369
+ #### Usage
370
+
371
+ ```js
372
+ const chessboard = new Chessboard(document.getElementById("board"), {
373
+ position: "start",
374
+ sprite: {url: "../assets/images/chessboard-sprite.svg"},
375
+ // animationDuration: 0, // optional, set to 0 to disable animations
376
+ style: {
377
+ cssClass: "default-contrast" // make the coordinates better visible with the "default-contrast" theme
378
+ },
379
+ extensions:
380
+ [{
381
+ class: Accessibility,
382
+ props: {
383
+ brailleNotationInAlt: true, // show the braille notation of the game in the alt attribute of the SVG
384
+ boardAsTable: true, // display the board additionally as HTML table
385
+ movePieceForm: true, // display a form to move a piece (from, to, move)
386
+ piecesAsList: true, // display the pieces additionally as List
387
+ visuallyHidden: false // hide all those extra outputs visually but keep them accessible for screen readers and braille displays
388
+ }
389
+
390
+ }]
391
+ })
392
+ ```
334
393
 
335
394
  ## Usage with React
336
395
 
@@ -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.13",
3
+ "version": "4.3.2",
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
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
  }