cm-chessboard 3.17.9 → 3.18.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
|
@@ -33,6 +33,8 @@ aspects of chess games.
|
|
|
33
33
|
|
|
34
34
|
**Option 2:** Download the code from [GitHub](https://github.com/shaack/cm-chessboard).
|
|
35
35
|
|
|
36
|
+
**Option 3:** Use it via CDN https://cdn.jsdelivr.net/npm/cm-chessboard@3.18.0/src/cm-chessboard/Chessboard.js
|
|
37
|
+
|
|
36
38
|
After installation, copy the sprite in `cm-chessboard/assets/images/` to your projects `assets/images/`
|
|
37
39
|
folder. If you put the sprite somewhere else you have to configure the location with `{sprite.url: "./url/of/chessboard-sprite.svg"}`
|
|
38
40
|
(see section 'Configuration' below).
|
|
@@ -7,13 +7,25 @@
|
|
|
7
7
|
<link rel="stylesheet" href="styles/examples.css"/>
|
|
8
8
|
<link rel="stylesheet" href="../assets/styles/cm-chessboard.css"/>
|
|
9
9
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/chess.js/0.10.2/chess.js"></script>
|
|
10
|
+
<style>
|
|
11
|
+
.visually-hidden {
|
|
12
|
+
position: absolute;
|
|
13
|
+
left: -10000px;
|
|
14
|
+
top: auto;
|
|
15
|
+
width: 1px;
|
|
16
|
+
height: 1px;
|
|
17
|
+
overflow: hidden;
|
|
18
|
+
}
|
|
19
|
+
</style>
|
|
10
20
|
</head>
|
|
11
21
|
<body>
|
|
12
|
-
<h1
|
|
13
|
-
<
|
|
22
|
+
<h1>Example: Input enabled, move validation with chess.js</h1>
|
|
23
|
+
<a href="../">Back to the cm-chessboard overview</a>
|
|
14
24
|
<p>Input enabled for white. <a href="https://github.com/jhlywa/chess.js">chess.js</a> does the validation and answers
|
|
15
25
|
with random moves.</p>
|
|
26
|
+
<h2 class="visually-hidden">The Board</h2>
|
|
16
27
|
<div class="board" id="board"></div>
|
|
28
|
+
<h2 class="visually-hidden">The Sourcecode of the example</h2>
|
|
17
29
|
<pre>
|
|
18
30
|
import {INPUT_EVENT_TYPE, COLOR, Chessboard, MARKER_TYPE} from "../src/cm-chessboard/Chessboard.js"
|
|
19
31
|
|
|
@@ -105,6 +117,7 @@ board.enableMoveInput(inputHandler, COLOR.white)
|
|
|
105
117
|
|
|
106
118
|
const board = new Chessboard(document.getElementById("board"), {
|
|
107
119
|
position: chess.fen(),
|
|
120
|
+
accessible: true,
|
|
108
121
|
// animationDuration: 2000,
|
|
109
122
|
sprite: {url: "../assets/images/chessboard-sprite-staunty.svg"},
|
|
110
123
|
style: {moveFromMarker: undefined, moveToMarker: undefined}, // disable standard markers
|
package/package.json
CHANGED
|
@@ -45,7 +45,7 @@ export class Chessboard {
|
|
|
45
45
|
if (!context) {
|
|
46
46
|
throw new Error("container element is " + context)
|
|
47
47
|
}
|
|
48
|
-
this.
|
|
48
|
+
this.context = context
|
|
49
49
|
this.id = (Math.random() + 1).toString(36).substr(2, 6)
|
|
50
50
|
let defaultProps = {
|
|
51
51
|
position: "empty", // set as fen, "start" or "empty"
|
|
@@ -79,7 +79,7 @@ export class Chessboard {
|
|
|
79
79
|
Object.assign(this.props.style, props.style)
|
|
80
80
|
}
|
|
81
81
|
if (this.props.style.aspectRatio) {
|
|
82
|
-
this.
|
|
82
|
+
this.context.style.height = (this.context.offsetWidth * this.props.style.aspectRatio) + "px"
|
|
83
83
|
}
|
|
84
84
|
this.state = new ChessboardState()
|
|
85
85
|
if (this.props.position === "start") {
|
|
@@ -199,9 +199,9 @@ export class Chessboard {
|
|
|
199
199
|
this.view = undefined
|
|
200
200
|
this.state = undefined
|
|
201
201
|
if (this.squareSelectListener) {
|
|
202
|
-
this.
|
|
203
|
-
this.
|
|
204
|
-
this.
|
|
202
|
+
this.context.removeEventListener("contextmenu", this.squareSelectListener)
|
|
203
|
+
this.context.removeEventListener("mouseup", this.squareSelectListener)
|
|
204
|
+
this.context.removeEventListener("touchend", this.squareSelectListener)
|
|
205
205
|
}
|
|
206
206
|
}
|
|
207
207
|
|
|
@@ -248,17 +248,17 @@ export class Chessboard {
|
|
|
248
248
|
square: SQUARE_COORDINATES[index]
|
|
249
249
|
})
|
|
250
250
|
}
|
|
251
|
-
this.
|
|
252
|
-
this.
|
|
253
|
-
this.
|
|
251
|
+
this.context.addEventListener("contextmenu", this.squareSelectListener)
|
|
252
|
+
this.context.addEventListener("mouseup", this.squareSelectListener)
|
|
253
|
+
this.context.addEventListener("touchend", this.squareSelectListener)
|
|
254
254
|
this.state.squareSelectEnabled = true
|
|
255
255
|
this.view.visualizeInputState()
|
|
256
256
|
}
|
|
257
257
|
|
|
258
258
|
disableSquareSelect() {
|
|
259
|
-
this.
|
|
260
|
-
this.
|
|
261
|
-
this.
|
|
259
|
+
this.context.removeEventListener("contextmenu", this.squareSelectListener)
|
|
260
|
+
this.context.removeEventListener("mouseup", this.squareSelectListener)
|
|
261
|
+
this.context.removeEventListener("touchend", this.squareSelectListener)
|
|
262
262
|
this.squareSelectListener = undefined
|
|
263
263
|
this.state.squareSelectEnabled = false
|
|
264
264
|
this.view.visualizeInputState()
|
|
@@ -80,7 +80,7 @@ export class ChessboardView {
|
|
|
80
80
|
this.resizeObserver = new ResizeObserver(() => {
|
|
81
81
|
this.handleResize()
|
|
82
82
|
})
|
|
83
|
-
this.resizeObserver.observe(this.chessboard.
|
|
83
|
+
this.resizeObserver.observe(this.chessboard.context)
|
|
84
84
|
} else {
|
|
85
85
|
this.resizeListener = this.handleResize.bind(this)
|
|
86
86
|
window.addEventListener("resize", this.resizeListener)
|
|
@@ -88,8 +88,8 @@ export class ChessboardView {
|
|
|
88
88
|
}
|
|
89
89
|
|
|
90
90
|
this.pointerDownListener = this.pointerDownHandler.bind(this)
|
|
91
|
-
this.chessboard.
|
|
92
|
-
this.chessboard.
|
|
91
|
+
this.chessboard.context.addEventListener("mousedown", this.pointerDownListener)
|
|
92
|
+
this.chessboard.context.addEventListener("touchstart", this.pointerDownListener)
|
|
93
93
|
|
|
94
94
|
this.createSvgAndGroups()
|
|
95
95
|
this.updateMetrics()
|
|
@@ -106,13 +106,13 @@ export class ChessboardView {
|
|
|
106
106
|
destroy() {
|
|
107
107
|
this.moveInput.destroy()
|
|
108
108
|
if (this.resizeObserver) {
|
|
109
|
-
this.resizeObserver.unobserve(this.chessboard.
|
|
109
|
+
this.resizeObserver.unobserve(this.chessboard.context)
|
|
110
110
|
}
|
|
111
111
|
if (this.resizeListener) {
|
|
112
112
|
window.removeEventListener("resize", this.resizeListener)
|
|
113
113
|
}
|
|
114
|
-
this.chessboard.
|
|
115
|
-
this.chessboard.
|
|
114
|
+
this.chessboard.context.removeEventListener("mousedown", this.pointerDownListener)
|
|
115
|
+
this.chessboard.context.removeEventListener("touchstart", this.pointerDownListener)
|
|
116
116
|
Svg.removeElement(this.svg)
|
|
117
117
|
this.animationQueue = []
|
|
118
118
|
if (this.currentAnimation) {
|
|
@@ -142,7 +142,7 @@ export class ChessboardView {
|
|
|
142
142
|
if (this.svg) {
|
|
143
143
|
Svg.removeElement(this.svg)
|
|
144
144
|
}
|
|
145
|
-
this.svg = Svg.createSvg(this.chessboard.
|
|
145
|
+
this.svg = Svg.createSvg(this.chessboard.context)
|
|
146
146
|
let description = document.createElement("description")
|
|
147
147
|
description.innerText = "Chessboard"
|
|
148
148
|
description.id = "svg-description"
|
|
@@ -159,8 +159,8 @@ export class ChessboardView {
|
|
|
159
159
|
}
|
|
160
160
|
|
|
161
161
|
updateMetrics() {
|
|
162
|
-
this.width = this.chessboard.
|
|
163
|
-
this.height = this.chessboard.
|
|
162
|
+
this.width = this.chessboard.context.clientWidth
|
|
163
|
+
this.height = this.chessboard.context.clientHeight
|
|
164
164
|
if (this.chessboard.props.style.borderType === BORDER_TYPE.frame) {
|
|
165
165
|
this.borderSize = this.width / 25
|
|
166
166
|
} else if (this.chessboard.props.style.borderType === BORDER_TYPE.thin) {
|
|
@@ -179,10 +179,10 @@ export class ChessboardView {
|
|
|
179
179
|
|
|
180
180
|
handleResize() {
|
|
181
181
|
if (this.chessboard.props.style.aspectRatio) {
|
|
182
|
-
this.chessboard.
|
|
182
|
+
this.chessboard.context.style.height = (this.chessboard.context.clientWidth * this.chessboard.props.style.aspectRatio) + "px"
|
|
183
183
|
}
|
|
184
|
-
if (this.chessboard.
|
|
185
|
-
this.chessboard.
|
|
184
|
+
if (this.chessboard.context.clientWidth !== this.width ||
|
|
185
|
+
this.chessboard.context.clientHeight !== this.height) {
|
|
186
186
|
this.updateMetrics()
|
|
187
187
|
this.redraw()
|
|
188
188
|
}
|
|
@@ -59,14 +59,15 @@ export class ChessboardViewAccessible extends ChessboardView {
|
|
|
59
59
|
})
|
|
60
60
|
this.accessibleContainer.appendChild(this.movePieceFormContainer)
|
|
61
61
|
|
|
62
|
+
this.boardAsTableContainer = this.createElement(`<div><h3>${this.th.board_as_table}</h3><div class="table"></div></div>`)
|
|
63
|
+
this.boardAsTable = this.boardAsTableContainer.querySelector(".table")
|
|
64
|
+
this.accessibleContainer.appendChild(this.boardAsTableContainer)
|
|
65
|
+
|
|
62
66
|
this.piecesListContainer = this.createElement(`<div><h3>${this.th.pieces_lists}</h3><div class="list"></div></div>`)
|
|
63
67
|
this.piecesList = this.piecesListContainer.querySelector(".list")
|
|
64
68
|
this.accessibleContainer.appendChild(this.piecesListContainer)
|
|
65
69
|
|
|
66
|
-
this.
|
|
67
|
-
this.boardAsTable = this.boardAsTableContainer.querySelector(".table")
|
|
68
|
-
this.accessibleContainer.appendChild(this.boardAsTableContainer)
|
|
69
|
-
this.chessboard.element.appendChild(this.accessibleContainer)
|
|
70
|
+
this.chessboard.context.appendChild(this.accessibleContainer)
|
|
70
71
|
this.updateFormInputs()
|
|
71
72
|
}
|
|
72
73
|
|
|
@@ -92,8 +93,8 @@ export class ChessboardViewAccessible extends ChessboardView {
|
|
|
92
93
|
drawPieces(squares = this.chessboard.state.squares) {
|
|
93
94
|
super.drawPieces(squares)
|
|
94
95
|
setTimeout(() => {
|
|
95
|
-
this.redrawPiecesLists()
|
|
96
96
|
this.redrawBoardAsTable()
|
|
97
|
+
this.redrawPiecesLists()
|
|
97
98
|
})
|
|
98
99
|
}
|
|
99
100
|
|
package/test/TestBoard.js
CHANGED
|
@@ -23,7 +23,7 @@ describe("TestBoard", () => {
|
|
|
23
23
|
sprite: {url: "../assets/images/chessboard-sprite.svg"},
|
|
24
24
|
position: "start"
|
|
25
25
|
})
|
|
26
|
-
assert.equals(chessboard.
|
|
26
|
+
assert.equals(chessboard.context.childNodes.length, 1)
|
|
27
27
|
chessboard.destroy()
|
|
28
28
|
assert.equals(chessboard.state, undefined)
|
|
29
29
|
})
|