cm-chessboard 4.0.8 → 4.1.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.
@@ -4,6 +4,7 @@
4
4
  <meta charset="UTF-8">
5
5
  <title>cm-chessboard</title>
6
6
  <meta name="viewport" content="width=device-width, user-scalable=yes, initial-scale=1.0"/>
7
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/chess.js/0.10.2/chess.js"></script>
7
8
  <link rel="stylesheet" href="styles/examples.css"/>
8
9
  <link rel="stylesheet" href="../assets/styles/cm-chessboard.css"/>
9
10
  <!--suppress CssUnusedSymbol -->
@@ -29,32 +30,39 @@
29
30
  <h2>Stress Test: Create and destroy 5,000 responsive boards</h2>
30
31
  <div id="boards"></div>
31
32
  <script type="module">
32
- import {COLOR, Chessboard} from "../src/cm-chessboard/Chessboard.js"
33
+ import {Chessboard} from "../src/cm-chessboard/Chessboard.js"
33
34
  const boardsDiv = document.getElementById("boards")
34
35
  const boardDiv = document.createElement("div")
35
36
  boardDiv.setAttribute("class", "board1")
36
37
  boardsDiv.appendChild(boardDiv)
37
- new Chessboard(boardDiv, {
38
+ const largeBoard = new Chessboard(boardDiv, {
38
39
  position: "start",
39
40
  sprite: {url: "../assets/images/chessboard-sprite-staunty.svg"},
40
41
  })
41
-
42
+ const chess = new Chess()
42
43
  for (let i = 0; i < 5000; i++) {
43
44
  setTimeout(() => {
44
45
  const boardDiv = document.createElement("div")
45
46
  boardDiv.setAttribute("class", "board")
46
47
  boardsDiv.appendChild(boardDiv)
47
48
  const board = new Chessboard(boardDiv, {
48
- position: Math.round(Math.random()) ? "start" : "empty",
49
- orientation: Math.round(Math.random()) ? COLOR.white : COLOR.black,
49
+ position: chess.fen(),
50
50
  sprite: {url: "../assets/images/chessboard-sprite-staunty.svg"}
51
51
  })
52
+ makeRandomMove()
53
+ largeBoard.setPosition(chess.fen(), true)
52
54
  setTimeout(() => {
53
55
  board.destroy()
54
56
  boardsDiv.removeChild(boardDiv)
55
57
  }, 10000)
56
58
  }, i * 100)
57
59
  }
60
+ function makeRandomMove() {
61
+ const possibleMoves = chess.moves();
62
+ if (possibleMoves.length === 0) return;
63
+ const randomIndex = Math.floor(Math.random() * possibleMoves.length);
64
+ chess.move(possibleMoves[randomIndex]);
65
+ }
58
66
 
59
67
  </script>
60
68
  </body>
@@ -1,3 +1,3 @@
1
- body{color:#fff;background-color:#222;padding:20px;margin:0;font-family:"Helvetica Neue", sans-serif;line-height:1.3;font-size:17px}body *{font-size:17px}a{color:#516CD4}a:visited{color:#929292}a:hover{color:#E2FEDE}h1,h1 a{margin-top:0;font-size:34px;color:#CD7338}h1{margin-bottom:10px}h2{margin-bottom:15px;font-size:23.8px;color:#CD7338}pre{font-family:"courier new", monospace;font-size:90%;background-color:#111;color:#eee;padding:5px;overflow:auto}div.board{float:left;max-width:500px;width:calc(100vw - 40px);margin-right:20px;margin-bottom:20px}.clearfix::after{content:"";clear:both;display:table}button{padding:4px 20px;margin-right:10px;margin-bottom:10px;background-color:#999}button:hover{background-color:#777}
1
+ body{color:#fff;background-color:#222;padding:20px;margin:0;font-family:"Helvetica Neue", sans-serif;line-height:1.3;font-size:18px}body *{font-size:18px}a{color:#516CD4}a:visited{color:#929292}a:hover{color:#E2FEDE}h1,h1 a{margin-top:0;font-size:39.6px;color:#CD7338}h1{margin-bottom:10px}h2{margin-bottom:15px;font-size:25.2px;color:#CD7338}pre{font-family:"courier new", monospace;font-size:90%;background-color:#111;color:#eee;padding:5px;overflow:auto}div.board{float:left;max-width:500px;width:calc(100vw - 40px);margin-right:20px;margin-bottom:20px}.clearfix::after{content:"";clear:both;display:table}button{padding:4px 20px;margin-right:10px;margin-bottom:10px;background-color:#999}button:hover{background-color:#777}li{padding:0.05rem}
2
2
 
3
3
  /*# sourceMappingURL=examples.css.map */
@@ -5,5 +5,5 @@
5
5
  "examples.scss"
6
6
  ],
7
7
  "names": [],
8
- "mappings": "AASA,AAAA,IAAI,AAAC,CACH,KAAK,C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ACf,OAAO,CAAE,EAAE,CACX,KAAK,CAAE,IAAI,CACX,OAAO,CAAE,KAAK,CACf,AAED,AAAA,MAAM,AAAC,CACL,OAAO,CAAE,QAAQ,CACjB,YAAY,CAAE,IAAI,CAClB,aAAa,CAAE,IAAI,CACnB,gBAAgB,CAAE,IAAI,CAKvB,AATD,AAME,MANI,CAMF,KAAK,AAAC,CACN,gBAAgB,CAAE,IAAI,CACvB,AAGH,AAAA,EAAE,AAAC,CACD,OAAO,CAAE,OAAO,CAEjB"
9
9
  }
@@ -5,7 +5,7 @@ $default: #fff;
5
5
  $color2: #CD7338;
6
6
  $code-background: #111;
7
7
  $code: #eee;
8
- $font-size: 17px;
8
+ $font-size: 18px;
9
9
 
10
10
  body {
11
11
  color: $default;
@@ -15,6 +15,7 @@ body {
15
15
  font-family: "Helvetica Neue", sans-serif;
16
16
  line-height: 1.3;
17
17
  font-size: $font-size;
18
+
18
19
  * {
19
20
  font-size: $font-size;
20
21
  }
@@ -22,9 +23,11 @@ body {
22
23
 
23
24
  a {
24
25
  color: $link;
26
+
25
27
  &:visited {
26
28
  color: #929292;
27
29
  }
30
+
28
31
  &:hover {
29
32
  color: $link-hover;
30
33
  }
@@ -32,7 +35,7 @@ a {
32
35
 
33
36
  h1, h1 a {
34
37
  margin-top: 0;
35
- font-size: $font-size * 2;
38
+ font-size: $font-size * 2.2;
36
39
  color: $color2;
37
40
  }
38
41
 
@@ -74,7 +77,13 @@ button {
74
77
  margin-right: 10px;
75
78
  margin-bottom: 10px;
76
79
  background-color: #999;
80
+
77
81
  &:hover {
78
82
  background-color: #777;
79
83
  }
80
84
  }
85
+
86
+ li {
87
+ padding: 0.05rem;
88
+
89
+ }
@@ -19,8 +19,8 @@
19
19
  </style>
20
20
  </head>
21
21
  <body>
22
- <h1>Example: Input enabled, move validation with chess.js</h1>
23
- <a href="../">Back to the cm-chessboard overview</a>
22
+ <h1><a href="../">cm-chessboard</a></h1>
23
+ <h2>Example: Input enabled, move validation with chess.js</h2>
24
24
  <p>Input enabled for white. <a href="https://github.com/jhlywa/chess.js">chess.js</a> does the validation and answers
25
25
  with random moves.</p>
26
26
  <h2 class="visually-hidden">The Board</h2>
@@ -82,11 +82,9 @@ board.enableMoveInput(inputHandler, COLOR.white)
82
82
  function inputHandler(event) {
83
83
  console.log("event", event)
84
84
  event.chessboard.removeMarkers(undefined, MARKER_TYPE.dot)
85
- event.chessboard.removeMarkers(undefined, MARKER_TYPE.square)
86
85
  if (event.type === INPUT_EVENT_TYPE.moveStart) {
87
86
  const moves = chess.moves({square: event.square, verbose: true});
88
- event.chessboard.addMarker(event.square, MARKER_TYPE.square)
89
- for (const move of moves) { // draw dots on possible moves
87
+ for (const move of moves) { // draw dots on possible squares
90
88
  event.chessboard.addMarker(move.to, MARKER_TYPE.dot)
91
89
  }
92
90
  return moves.length > 0
@@ -115,9 +113,15 @@ board.enableMoveInput(inputHandler, COLOR.white)
115
113
  const board = new Chessboard(document.getElementById("board"), {
116
114
  position: chess.fen(),
117
115
  sprite: {url: "../assets/images/chessboard-sprite-staunty.svg"},
118
- style: {moveFromMarker: undefined, moveToMarker: undefined}, // disable standard markers
116
+ style: {moveFromMarker: MARKER_TYPE.square, moveToMarker: MARKER_TYPE.square},
119
117
  orientation: COLOR.white,
120
- animationDuration: 500
118
+ accessibility: {
119
+ brailleNotationInAlt: true, // show the braille notation of the game in the alt attribute of the svg
120
+ movePieceForm: true, // display a form to move a piece (from, to, move)
121
+ boardAsTable: true, // display the board additionally as HTML table
122
+ piecesAsList: true, // display the pieces additionally as List
123
+ visuallyHidden: true // hide all those extra outputs visually but keep them accessible for screen readers and braille displays
124
+ }
121
125
  })
122
126
  board.enableMoveInput(inputHandler, COLOR.white)
123
127
  </script>
package/favicon.ico CHANGED
Binary file
package/index.html CHANGED
@@ -6,36 +6,47 @@
6
6
  <link rel="stylesheet" href="assets/styles/cm-chessboard.css"/>
7
7
  <link rel="stylesheet" href="examples/styles/examples.css"/>
8
8
  <meta name="viewport" content="width=device-width, user-scalable=yes, initial-scale=1.0"/>
9
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/chess.js/0.10.2/chess.js"></script>
9
10
  </head>
10
11
  <body>
11
12
  <h1>cm-chessboard</h1>
12
- <p>A JavaScript chessboard which is lightweight, ES6 module based, responsive, SVG rendered and without dependencies.</p>
13
- <p>cm-chessboard is the chessboard of <a href="https://www.chessmail.de">chessmail.de</a> and <a
14
- href="https://www.chessmail.eu">chessmail.eu</a>.
15
- It is used every day by thousands of players. </p>
13
+ <p><i>A JavaScript chessboard which is lightweight, ES6 module based, responsive, SVG rendered and without dependencies.</i></p>
14
+ <p>cm-chessboard is the chessboard of <a href="https://www.chessmail.de">chessmail</a> and is used every day by thousands of players.
15
+ It works, it is cool and it is easy to use. 👍</p>
16
16
  <p><a href="https://github.com/shaack/cm-chessboard">Repository and documentation on GitHub</a></p>
17
- <h2>Examples</h2>
18
- <ul>
19
- <li><a href="examples/simple-boards.html">Simple chessboards, view only</a></li>
20
- <li><a href="examples/responsive-board.html">Responsive chessboard</a></li>
21
- <li><a href="examples/enable-input.html">Input enabled without validation</a></li>
22
- <li><a href="examples/validate-moves.html">Input enabled, move validation with chess.js</a></li>
23
- <li><a href="examples/pieces-animation.html">Set different positions, with animation</a></li>
24
- <li><a href="examples/input-callbacks.html">Context input, mark squares and detect clicks on fields</a></li>
25
- <li><a href="examples/different-styles.html">Different styles and piece sets</a></li>
26
- <li><a href="examples/many-boards.html">100 boards, in one page</a></li>
27
- <li><a href="examples/destroy-many-boards.html">Stress test, 5000 boards created and destroyed</a></li>
28
- </ul>
17
+ <div>
18
+ <h2>Examples</h2>
19
+ <ul>
20
+ <li><a href="examples/simple-boards.html">Simple chessboards, view only</a></li>
21
+ <li><a href="examples/responsive-board.html">Responsive chessboard</a></li>
22
+ <li><a href="examples/enable-input.html">Input enabled without validation</a></li>
23
+ <li><a href="examples/validate-moves.html">Input enabled, move validation with chess.js</a> 🥸</li>
24
+ <li><a href="examples/pieces-animation.html">Set different positions, with animation</a></li>
25
+ <li><a href="examples/input-callbacks.html">Context input, mark squares and detect clicks on fields</a></li>
26
+ <li><a href="examples/different-styles.html">Different styles and piece sets</a> 🎨</li>
27
+ <li><a href="examples/many-boards.html">100 boards, in one page</a></li>
28
+ <li><a href="examples/destroy-many-boards.html">Stress test, 5000 boards created and destroyed</a> 🤓 👍</li>
29
+ </ul>
30
+ </div>
29
31
  <h2>Chessboard</h2>
30
- <pre>const board = new Chessboard(document.getElementById("board"), {position: "start"})</pre>
31
32
  <div class="board" id="board"></div>
32
33
  <script type="module">
33
34
  import {Chessboard} from "./src/cm-chessboard/Chessboard.js"
34
35
 
36
+ const chess = new Chess()
35
37
  const board = new Chessboard(document.getElementById("board"), {
36
38
  position: "start"
37
39
  })
38
- board.enableMoveInput()
40
+ setInterval(() => {
41
+ makeRandomMove()
42
+ board.setPosition(chess.fen(), true)
43
+ }, 250)
44
+ function makeRandomMove() {
45
+ const possibleMoves = chess.moves();
46
+ if (possibleMoves.length === 0) return;
47
+ const randomIndex = Math.floor(Math.random() * possibleMoves.length);
48
+ chess.move(possibleMoves[randomIndex]);
49
+ }
39
50
  </script>
40
51
  <div class="clearfix"></div>
41
52
  <h2>Test</h2>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cm-chessboard",
3
- "version": "4.0.8",
3
+ "version": "4.1.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",
@@ -61,11 +61,9 @@ export class PromiseQueue {
61
61
  }
62
62
 
63
63
  destroy() {
64
- console.log("PAQ destroy")
65
64
  this.stop = true
66
65
  }
67
66
 
68
-
69
67
  }
70
68
 
71
69
 
@@ -45,41 +45,52 @@ export class ViewAccessible extends View {
45
45
  this.th = hlTranslations[this.lang]
46
46
 
47
47
  const accessibilityProps = chessboard.props.accessibility
48
- if(accessibilityProps.movePieceForm) {
48
+ if (accessibilityProps.movePieceForm) {
49
49
  this.movePieceFormContainer = this.createElement(`<div><h3>${this.th.move_piece}</h3><form>
50
50
  <label for="move_piece_input_from_${chessboard.id}">${this.th.from}</label><input class="input-from" type="text" size="2" id="move_piece_input_from_${chessboard.id}"/>
51
51
  <label for="move_piece_input_to_${chessboard.id}">${this.th.to}</label><input class="input-to" type="text" size="2" id="move_piece_input_to_${chessboard.id}"/>
52
- <button type="button" class="button-move">${this.th.move}</button>
52
+ <button type="submit" class="button-move">${this.th.move}</button>
53
53
  </form><div class="input-status" aria-live="polite"></div></div>`)
54
- this.inputFrom = this.movePieceFormContainer.querySelector(".input-from")
55
- this.inputTo = this.movePieceFormContainer.querySelector(".input-to")
56
- this.moveButton = this.movePieceFormContainer.querySelector(".button-move")
57
- if(accessibilityProps.visuallyHidden) {
54
+ this.form = this.movePieceFormContainer.querySelector("form")
55
+ this.inputFrom = this.form.querySelector(".input-from")
56
+ this.inputTo = this.form.querySelector(".input-to")
57
+ this.moveButton = this.form.querySelector(".button-move")
58
+ if (accessibilityProps.visuallyHidden) {
58
59
  this.movePieceFormContainer.classList.add("visually-hidden")
59
60
  }
60
- this.moveButton.addEventListener("click", () => {
61
+ this.form.addEventListener("submit", (evt) => {
62
+ evt.preventDefault()
61
63
  if (this.moveInputCallback({
62
64
  chessboard: this.chessboard,
63
65
  type: INPUT_EVENT_TYPE.moveDone,
64
66
  squareFrom: this.inputFrom.value,
65
67
  squareTo: this.inputTo.value
66
68
  })) {
67
- this.inputFrom.value = ""
68
- this.inputTo.value = ""
69
+ this.chessboard.movePiece(this.inputFrom.value, this.inputTo.value,
70
+ true).then(() => {
71
+ this.inputFrom.value = ""
72
+ this.inputTo.value = ""
73
+ })
69
74
  }
70
75
  })
71
76
  this.chessboard.context.appendChild(this.movePieceFormContainer)
72
77
  }
73
78
 
74
- if(accessibilityProps.boardAsTable) {
79
+ if (accessibilityProps.boardAsTable) {
75
80
  this.boardAsTableContainer = this.createElement(`<div><h3>${this.th.board_as_table}</h3><div class="table"></div></div>`)
76
81
  this.boardAsTable = this.boardAsTableContainer.querySelector(".table")
77
82
  this.chessboard.context.appendChild(this.boardAsTableContainer)
83
+ if (accessibilityProps.visuallyHidden) {
84
+ this.boardAsTableContainer.classList.add("visually-hidden")
85
+ }
78
86
  }
79
- if(accessibilityProps.piecesListContainer) {
87
+ if (accessibilityProps.piecesListContainer) {
80
88
  this.piecesListContainer = this.createElement(`<div><h3>${this.th.pieces_lists}</h3><div class="list"></div></div>`)
81
89
  this.piecesList = this.piecesListContainer.querySelector(".list")
82
90
  this.chessboard.context.appendChild(this.piecesListContainer)
91
+ if (accessibilityProps.visuallyHidden) {
92
+ this.piecesListContainer.classList.add("visually-hidden")
93
+ }
83
94
  }
84
95
  this.updateFormInputs()
85
96
  }
@@ -106,10 +117,10 @@ export class ViewAccessible extends View {
106
117
  redrawPieces(squares = this.chessboard.state.position.squares) {
107
118
  super.redrawPieces(squares)
108
119
  setTimeout(() => {
109
- if(this.chessboard.props.accessibility.boardAsTable) {
120
+ if (this.chessboard.props.accessibility.boardAsTable) {
110
121
  this.redrawBoardAsTable()
111
122
  }
112
- if(this.chessboard.props.accessibility.piecesAsList) {
123
+ if (this.chessboard.props.accessibility.piecesAsList) {
113
124
  this.redrawPiecesLists()
114
125
  }
115
126
  })