cm-chessboard 4.0.10 → 4.1.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.
@@ -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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cm-chessboard",
3
- "version": "4.0.10",
3
+ "version": "4.1.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",
@@ -4,10 +4,10 @@
4
4
  * License: MIT, see file 'LICENSE'
5
5
  */
6
6
 
7
- import {State} from "./core/State.js"
8
- import {ViewAccessible} from "./core/ViewAccessible.js"
9
- import {PositionAnimationsQueue} from "./core/PositionAnimationsQueue.js"
10
- import {Position} from "./core/Position.js"
7
+ import {ChessboardState} from "./model/ChessboardState.js"
8
+ import {Position} from "./model/Position.js"
9
+ import {ChessboardViewAccessible} from "./view/ChessboardViewAccessible.js"
10
+ import {PositionAnimationsQueue} from "./view/PositionAnimationsQueue.js"
11
11
 
12
12
  export const COLOR = {
13
13
  white: "w",
@@ -88,8 +88,8 @@ export class Chessboard {
88
88
  Object.assign(this.props.accessibility, props.accessibility)
89
89
  }
90
90
 
91
- this.state = new State()
92
- this.view = new ViewAccessible(this)
91
+ this.state = new ChessboardState()
92
+ this.view = new ChessboardViewAccessible(this)
93
93
  this.positionAnimationsQueue = new PositionAnimationsQueue(this)
94
94
  this.state.orientation = this.props.orientation
95
95
  this.view.redraw()
@@ -5,7 +5,7 @@
5
5
  */
6
6
  import {Position} from "./Position.js"
7
7
 
8
- export class State {
8
+ export class ChessboardState {
9
9
 
10
10
  constructor() {
11
11
  this.position = new Position()
File without changes
@@ -4,9 +4,9 @@
4
4
  * License: MIT, see file 'LICENSE'
5
5
  */
6
6
 
7
- import {MoveInput} from "./MoveInput.js"
7
+ import {VisualMoveInput} from "./VisualMoveInput.js"
8
8
  import {COLOR, INPUT_EVENT_TYPE, BORDER_TYPE} from "../Chessboard.js"
9
- import {Position} from "./Position.js"
9
+ import {Position} from "../model/Position.js"
10
10
 
11
11
  export const piecesTranslations = {
12
12
  en: {
@@ -47,11 +47,11 @@ export function renderPieceTitle(lang, name, color = undefined) {
47
47
  return title
48
48
  }
49
49
 
50
- export class View {
50
+ export class ChessboardView {
51
51
 
52
52
  constructor(chessboard) {
53
53
  this.chessboard = chessboard
54
- this.moveInput = new MoveInput(this,
54
+ this.moveInput = new VisualMoveInput(this,
55
55
  this.moveStartCallback.bind(this),
56
56
  this.moveDoneCallback.bind(this),
57
57
  this.moveCanceledCallback.bind(this)
@@ -3,9 +3,9 @@
3
3
  * Repository: https://github.com/shaack/cm-chessboard
4
4
  * License: MIT, see file 'LICENSE'
5
5
  */
6
- import {View, renderPieceTitle} from "./View.js"
6
+ import {ChessboardView, renderPieceTitle} from "./ChessboardView.js"
7
7
  import {COLOR, INPUT_EVENT_TYPE} from "../Chessboard.js"
8
- import {piecesTranslations} from "./View.js"
8
+ import {piecesTranslations} from "./ChessboardView.js"
9
9
 
10
10
  const hlTranslations = {
11
11
  de: {
@@ -32,7 +32,7 @@ const hlTranslations = {
32
32
  }
33
33
  }
34
34
 
35
- export class ViewAccessible extends View {
35
+ export class ChessboardViewAccessible extends ChessboardView {
36
36
 
37
37
  constructor(chessboard, callbackAfterCreation) {
38
38
  super(chessboard, callbackAfterCreation)
@@ -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
  })
@@ -3,8 +3,8 @@
3
3
  * Repository: https://github.com/shaack/cm-chessboard
4
4
  * License: MIT, see file 'LICENSE'
5
5
  */
6
- import {FEN_EMPTY_POSITION, Position} from "./Position.js"
7
- import {Svg} from "./View.js"
6
+ import {FEN_EMPTY_POSITION, Position} from "../model/Position.js"
7
+ import {Svg} from "./ChessboardView.js"
8
8
 
9
9
  /*
10
10
  * Thanks to markosyan for the idea to the PromiseQueue
@@ -4,7 +4,7 @@
4
4
  * License: MIT, see file 'LICENSE'
5
5
  */
6
6
 
7
- import {Svg} from "./View.js"
7
+ import {Svg} from "./ChessboardView.js"
8
8
 
9
9
  const STATE = {
10
10
  waitForInputStart: 0,
@@ -26,7 +26,7 @@ export const MOVE_CANCELED_REASON = {
26
26
 
27
27
  const DRAG_THRESHOLD = 4
28
28
 
29
- export class MoveInput {
29
+ export class VisualMoveInput {
30
30
 
31
31
  constructor(view, moveStartCallback, moveDoneCallback, moveCanceledCallback) {
32
32
  this.view = view
@@ -5,8 +5,8 @@
5
5
  */
6
6
 
7
7
  import {describe, it, assert} from "../node_modules/teevi/src/teevi.js"
8
- import {State} from "../src/cm-chessboard/core/State.js"
9
- import {PositionsAnimation} from "../src/cm-chessboard/core/PositionAnimationsQueue.js"
8
+ import {ChessboardState} from "../src/cm-chessboard/model/ChessboardState.js"
9
+ import {PositionsAnimation} from "../src/cm-chessboard/view/PositionAnimationsQueue.js"
10
10
 
11
11
  describe("TestPiecesAnimation", () => {
12
12
  it("should calculate square distances", () => {
@@ -21,9 +21,9 @@ describe("TestPiecesAnimation", () => {
21
21
  })
22
22
 
23
23
  it("should seek changes", () => {
24
- const state1 = new State()
24
+ const state1 = new ChessboardState()
25
25
  state1.setPosition("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR")
26
- const state2 = new State()
26
+ const state2 = new ChessboardState()
27
27
  state2.setPosition("rn2k1r1/ppp1pp1p/3p2p1/5bn1/P7/2N2B2/1PPPPP2/2BNK1RR")
28
28
  const previousBoard1 = state1.position.squares
29
29
  const newBoard1 = state2.position.squares
@@ -5,7 +5,7 @@
5
5
  */
6
6
 
7
7
  import {describe, it, assert} from "../node_modules/teevi/src/teevi.js"
8
- import {Position} from "../src/cm-chessboard/core/Position.js"
8
+ import {Position} from "../src/cm-chessboard/model/Position.js"
9
9
 
10
10
  describe("TestPosition", () => {
11
11
  it("should convert square to index", () => {