cm-chessboard 8.7.0 → 8.7.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/package.json +1 -1
- package/src/Chessboard.js +3 -0
- package/src/extensions/accessibility/Accessibility.js +3 -3
- package/src/model/Position.js +12 -4
- package/src/view/ChessboardView.js +0 -1
- package/test/TestChessboard.js +21 -2
- package/test/TestPosition.js +19 -1
- package/test/index.html +0 -1
- package/test/TestBoard.js +0 -32
package/package.json
CHANGED
package/src/Chessboard.js
CHANGED
|
@@ -15,6 +15,9 @@ export const PIECE = {
|
|
|
15
15
|
wp: "wp", wb: "wb", wn: "wn", wr: "wr", wq: "wq", wk: "wk",
|
|
16
16
|
bp: "bp", bb: "bb", bn: "bn", br: "br", bq: "bq", bk: "bk"
|
|
17
17
|
}
|
|
18
|
+
export const PIECE_TYPE = {
|
|
19
|
+
pawn: "p", knight: "n", bishop: "b", rook: "r", queen: "q", king: "k"
|
|
20
|
+
}
|
|
18
21
|
export const PIECES_FILE_TYPE = {
|
|
19
22
|
svgSprite: "svgSprite"
|
|
20
23
|
}
|
|
@@ -92,7 +92,7 @@ class BrailleNotationInAlt {
|
|
|
92
92
|
let listW = piecesTranslations[this.extension.lang].colors.w.toUpperCase() + ":"
|
|
93
93
|
let listB = piecesTranslations[this.extension.lang].colors.b.toUpperCase() + ":"
|
|
94
94
|
for (const piece of pieces) {
|
|
95
|
-
const pieceName = piece.
|
|
95
|
+
const pieceName = piece.type === "p" ? "" : piecesTranslations[this.extension.lang].pieces[piece.type].toUpperCase()
|
|
96
96
|
if (piece.color === "w") {
|
|
97
97
|
listW += " " + pieceName + piece.position
|
|
98
98
|
} else {
|
|
@@ -226,9 +226,9 @@ class PiecesAsList {
|
|
|
226
226
|
let listB = ""
|
|
227
227
|
for (const piece of pieces) {
|
|
228
228
|
if (piece.color === "w") {
|
|
229
|
-
listW += `<li class="list-inline-item">${renderPieceTitle(this.extension.lang, piece.
|
|
229
|
+
listW += `<li class="list-inline-item">${renderPieceTitle(this.extension.lang, piece.type)} ${piece.position}</li>`
|
|
230
230
|
} else {
|
|
231
|
-
listB += `<li class="list-inline-item">${renderPieceTitle(this.extension.lang, piece.
|
|
231
|
+
listB += `<li class="list-inline-item">${renderPieceTitle(this.extension.lang, piece.type)} ${piece.position}</li>`
|
|
232
232
|
}
|
|
233
233
|
}
|
|
234
234
|
this.piecesList.innerHTML = `
|
package/src/model/Position.js
CHANGED
|
@@ -71,7 +71,7 @@ export class Position {
|
|
|
71
71
|
return parts.join("/")
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
getPieces(sortBy = ['k', 'q', 'r', 'b', 'n', 'p']) {
|
|
74
|
+
getPieces(pieceType = undefined, pieceColor = undefined, sortBy = ['k', 'q', 'r', 'b', 'n', 'p']) {
|
|
75
75
|
const pieces = []
|
|
76
76
|
const sort = (a, b) => {
|
|
77
77
|
return sortBy.indexOf(a.name) - sortBy.indexOf(b.name)
|
|
@@ -79,10 +79,18 @@ export class Position {
|
|
|
79
79
|
for (let i = 0; i < 64; i++) {
|
|
80
80
|
const piece = this.squares[i]
|
|
81
81
|
if (piece) {
|
|
82
|
+
const type = piece.charAt(1)
|
|
83
|
+
const color = piece.charAt(0)
|
|
84
|
+
const square = Position.indexToSquare(i)
|
|
85
|
+
if(pieceType && pieceType !== type || pieceColor && pieceColor !== color) {
|
|
86
|
+
continue
|
|
87
|
+
}
|
|
82
88
|
pieces.push({
|
|
83
|
-
name:
|
|
84
|
-
|
|
85
|
-
|
|
89
|
+
name: type, // deprecated, use type
|
|
90
|
+
type: type,
|
|
91
|
+
color: color,
|
|
92
|
+
position: square, // deprecated, use square
|
|
93
|
+
square: square
|
|
86
94
|
})
|
|
87
95
|
}
|
|
88
96
|
}
|
|
@@ -61,7 +61,6 @@ export class ChessboardView {
|
|
|
61
61
|
}
|
|
62
62
|
this.positionsAnimationTask = Promise.resolve()
|
|
63
63
|
this.pointerDownListener = this.pointerDownHandler.bind(this)
|
|
64
|
-
this.pointerDownListener = this.pointerDownHandler.bind(this)
|
|
65
64
|
this.container.addEventListener("mousedown", this.pointerDownListener)
|
|
66
65
|
this.container.addEventListener("touchstart", this.pointerDownListener, {passive: false})
|
|
67
66
|
this.createSvgAndGroups()
|
package/test/TestChessboard.js
CHANGED
|
@@ -10,12 +10,31 @@ import {FEN} from "../src/model/Position.js"
|
|
|
10
10
|
|
|
11
11
|
describe("TestChessboard", () => {
|
|
12
12
|
|
|
13
|
+
// https://github.com/shaack/cm-chessboard/issues/47
|
|
14
|
+
it("should create and immediately destroy a board without failure", () => {
|
|
15
|
+
const chessboard = new Chessboard(document.getElementById("TestBoard"), {
|
|
16
|
+
assetsUrl: "../assets/",
|
|
17
|
+
position: FEN.start
|
|
18
|
+
})
|
|
19
|
+
chessboard.destroy()
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it("should create and destroy a board", () => {
|
|
23
|
+
const chessboard = new Chessboard(document.getElementById("TestBoard"), {
|
|
24
|
+
assetsUrl: "../assets/",
|
|
25
|
+
position: FEN.start
|
|
26
|
+
})
|
|
27
|
+
assert.equal(chessboard.view.container.childNodes.length, 1)
|
|
28
|
+
chessboard.destroy()
|
|
29
|
+
assert.equal(chessboard.state, undefined)
|
|
30
|
+
})
|
|
31
|
+
|
|
13
32
|
it("should create and destroy a chessboard", () => {
|
|
14
33
|
const chessboard = new Chessboard(document.getElementById("TestPosition"), {
|
|
15
34
|
assetsUrl: "../assets/",
|
|
16
35
|
position: FEN.start
|
|
17
36
|
})
|
|
18
|
-
assert.equal(chessboard.getPosition(), "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR")
|
|
37
|
+
assert.equal("" + chessboard.getPosition(), "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR")
|
|
19
38
|
chessboard.destroy()
|
|
20
39
|
})
|
|
21
40
|
|
|
@@ -23,7 +42,7 @@ describe("TestChessboard", () => {
|
|
|
23
42
|
const chessboard = new Chessboard(document.getElementById("TestPosition"),
|
|
24
43
|
{assetsUrl: "../assets/"})
|
|
25
44
|
chessboard.setPosition("rn2k1r1/ppp1pp1p/3p2p1/5bn1/P7/2N2B2/1PPPPP2/2BNK1RR w Gkq - 4 11", false).then(() => {
|
|
26
|
-
assert.equal(chessboard.getPosition(), "rn2k1r1/ppp1pp1p/3p2p1/5bn1/P7/2N2B2/1PPPPP2/2BNK1RR")
|
|
45
|
+
assert.equal("" + chessboard.getPosition(), "rn2k1r1/ppp1pp1p/3p2p1/5bn1/P7/2N2B2/1PPPPP2/2BNK1RR")
|
|
27
46
|
chessboard.destroy()
|
|
28
47
|
})
|
|
29
48
|
})
|
package/test/TestPosition.js
CHANGED
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import {describe, it, assert} from "../node_modules/teevi/src/teevi.js"
|
|
8
|
-
import {Position} from "../src/model/Position.js"
|
|
8
|
+
import {FEN, Position} from "../src/model/Position.js"
|
|
9
|
+
import {COLOR, PIECE_TYPE} from "../src/Chessboard.js"
|
|
9
10
|
|
|
10
11
|
describe("TestPosition", () => {
|
|
11
12
|
it("should convert square to index", () => {
|
|
@@ -22,4 +23,21 @@ describe("TestPosition", () => {
|
|
|
22
23
|
assert.equal(Position.indexToSquare(38), "g5")
|
|
23
24
|
assert.equal(Position.indexToSquare(63), "h8")
|
|
24
25
|
})
|
|
26
|
+
it("should return the correct FEN string", () => {
|
|
27
|
+
const position = new Position(FEN.start)
|
|
28
|
+
assert.equal(position.getFen(), "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR")
|
|
29
|
+
})
|
|
30
|
+
it("should find the correct pieces", () => {
|
|
31
|
+
const position = new Position("8/5P2/8/1P3r2/8/8/1P3P1P/8")
|
|
32
|
+
const blackPiece = position.getPieces(undefined, COLOR.black)
|
|
33
|
+
assert.equal(blackPiece.length, 1)
|
|
34
|
+
assert.equal(blackPiece[0].type, PIECE_TYPE.rook)
|
|
35
|
+
assert.equal(blackPiece[0].square, "f5")
|
|
36
|
+
const whitePieces = position.getPieces(undefined, COLOR.white)
|
|
37
|
+
assert.equal(whitePieces.length, 5)
|
|
38
|
+
const rooks = position.getPieces(PIECE_TYPE.rook)
|
|
39
|
+
assert.equal(rooks.length, 1)
|
|
40
|
+
assert.equal(rooks[0].square, "f5")
|
|
41
|
+
})
|
|
25
42
|
})
|
|
43
|
+
|
package/test/index.html
CHANGED
package/test/TestBoard.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Author and copyright: Stefan Haack (https://shaack.com)
|
|
3
|
-
* Repository: https://github.com/shaack/cm-chessboard
|
|
4
|
-
* License: MIT, see file 'LICENSE'
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import {describe, it, assert} from "../node_modules/teevi/src/teevi.js"
|
|
8
|
-
import {Chessboard} from "../src/Chessboard.js"
|
|
9
|
-
import {FEN} from "../src/model/Position.js"
|
|
10
|
-
|
|
11
|
-
describe("TestBoard", () => {
|
|
12
|
-
|
|
13
|
-
// https://github.com/shaack/cm-chessboard/issues/47
|
|
14
|
-
it("should create and immediately destroy a board without failure", () => {
|
|
15
|
-
const chessboard = new Chessboard(document.getElementById("TestBoard"), {
|
|
16
|
-
assetsUrl: "../assets/",
|
|
17
|
-
position: FEN.start
|
|
18
|
-
})
|
|
19
|
-
chessboard.destroy()
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
it("should create and destroy a board", () => {
|
|
23
|
-
const chessboard = new Chessboard(document.getElementById("TestBoard"), {
|
|
24
|
-
assetsUrl: "../assets/",
|
|
25
|
-
position: FEN.start
|
|
26
|
-
})
|
|
27
|
-
assert.equal(chessboard.view.container.childNodes.length, 1)
|
|
28
|
-
chessboard.destroy()
|
|
29
|
-
assert.equal(chessboard.state, undefined)
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
})
|