cm-chessboard 7.5.0 → 7.6.0
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 +1 -1
- package/examples/validate-moves.html +9 -4
- package/package.json +1 -1
- package/src/Chessboard.js +3 -2
- package/src/view/ChessboardView.js +15 -5
- package/src/view/VisualMoveInput.js +10 -5
package/README.md
CHANGED
|
@@ -91,7 +91,7 @@ this.props = {
|
|
|
91
91
|
borderType: BORDER_TYPE.none, // "thin" thin border, "frame" wide border with coordinates in it, "none" no border
|
|
92
92
|
aspectRatio: 1, // height/width of the board
|
|
93
93
|
pieces: {
|
|
94
|
-
type: PIECES_FILE_TYPE.svgSprite, // pieces are in
|
|
94
|
+
type: PIECES_FILE_TYPE.svgSprite, // pieces are in an SVG sprite, no other type supported for now
|
|
95
95
|
file: "standard.svg", // the filename of the sprite in `assets/pieces/`
|
|
96
96
|
tileSize: 40 // the tile size in the sprite
|
|
97
97
|
},
|
|
@@ -39,6 +39,8 @@
|
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
function inputHandler(event) {
|
|
42
|
+
console.log("inputHandler", event)
|
|
43
|
+
console.log("fen", event.chessboard.getPosition())
|
|
42
44
|
event.chessboard.removeMarkers(MARKER_TYPE.dot)
|
|
43
45
|
event.chessboard.removeMarkers(MARKER_TYPE.bevel)
|
|
44
46
|
if (event.type === INPUT_EVENT_TYPE.moveInputStarted) {
|
|
@@ -58,7 +60,6 @@
|
|
|
58
60
|
const move = {from: event.squareFrom, to: event.squareTo, promotion: event.promotion}
|
|
59
61
|
const result = chess.move(move)
|
|
60
62
|
if (result) {
|
|
61
|
-
event.chessboard.disableMoveInput()
|
|
62
63
|
this.chessboard.state.moveInputProcess.then(() => { // wait for the move input process has finished
|
|
63
64
|
this.chessboard.setPosition(chess.fen(), true).then(() => { // update position, maybe castled and wait for animation has finished
|
|
64
65
|
makeEngineMove(event.chessboard)
|
|
@@ -74,11 +75,11 @@
|
|
|
74
75
|
if (result) {
|
|
75
76
|
chess.move({from: event.squareFrom, to: event.squareTo, promotion: result.piece.charAt(1)})
|
|
76
77
|
event.chessboard.setPosition(chess.fen(), true)
|
|
77
|
-
event.chessboard.disableMoveInput()
|
|
78
78
|
makeEngineMove(event.chessboard)
|
|
79
79
|
} else {
|
|
80
|
-
|
|
80
|
+
// promotion canceled
|
|
81
81
|
event.chessboard.enableMoveInput(inputHandler, COLOR.white)
|
|
82
|
+
event.chessboard.setPosition(chess.fen(), true)
|
|
82
83
|
}
|
|
83
84
|
})
|
|
84
85
|
return true
|
|
@@ -86,13 +87,17 @@
|
|
|
86
87
|
}
|
|
87
88
|
}
|
|
88
89
|
return result
|
|
90
|
+
} else if (event.type === INPUT_EVENT_TYPE.moveInputFinished) {
|
|
91
|
+
if(event.legalMove) {
|
|
92
|
+
event.chessboard.disableMoveInput()
|
|
93
|
+
}
|
|
89
94
|
}
|
|
90
95
|
}
|
|
91
96
|
|
|
92
97
|
const board = new Chessboard(document.getElementById("board"), {
|
|
93
98
|
position: chess.fen(),
|
|
94
99
|
assetsUrl: "../assets/",
|
|
95
|
-
style: {borderType: BORDER_TYPE.none, pieces: {file: "staunty.svg"}},
|
|
100
|
+
style: {borderType: BORDER_TYPE.none, pieces: {file: "staunty.svg"}, animationDuration: 300},
|
|
96
101
|
orientation: COLOR.white,
|
|
97
102
|
extensions: [
|
|
98
103
|
{class: Markers, props: {autoMarkers: MARKER_TYPE.square}},
|
package/package.json
CHANGED
package/src/Chessboard.js
CHANGED
|
@@ -19,7 +19,8 @@ export const INPUT_EVENT_TYPE = {
|
|
|
19
19
|
moveInputStarted: "moveInputStarted",
|
|
20
20
|
movingOverSquare: "movingOverSquare", // while dragging or hover after click
|
|
21
21
|
validateMoveInput: "validateMoveInput",
|
|
22
|
-
moveInputCanceled: "moveInputCanceled"
|
|
22
|
+
moveInputCanceled: "moveInputCanceled",
|
|
23
|
+
moveInputFinished: "moveInputFinished"
|
|
23
24
|
}
|
|
24
25
|
export const SQUARE_SELECT_TYPE = {
|
|
25
26
|
primary: "primary",
|
|
@@ -63,7 +64,7 @@ export class Chessboard {
|
|
|
63
64
|
borderType: BORDER_TYPE.none, // "thin" thin border, "frame" wide border with coordinates in it, "none" no border
|
|
64
65
|
aspectRatio: 1, // height/width of the board
|
|
65
66
|
pieces: {
|
|
66
|
-
type: PIECES_FILE_TYPE.svgSprite, // pieces are in
|
|
67
|
+
type: PIECES_FILE_TYPE.svgSprite, // pieces are in an SVG sprite, no other type supported for now
|
|
67
68
|
file: "standard.svg", // the filename of the sprite in `assets/pieces/`
|
|
68
69
|
tileSize: 40 // the tile size in the sprite
|
|
69
70
|
},
|
|
@@ -13,11 +13,7 @@ import {Svg} from "../lib/Svg.js"
|
|
|
13
13
|
export class ChessboardView {
|
|
14
14
|
constructor(chessboard) {
|
|
15
15
|
this.chessboard = chessboard
|
|
16
|
-
this.visualMoveInput = new VisualMoveInput(this
|
|
17
|
-
this.moveInputStartedCallback.bind(this),
|
|
18
|
-
this.movingOverSquareCallback.bind(this),
|
|
19
|
-
this.validateMoveInputCallback.bind(this),
|
|
20
|
-
this.moveInputCanceledCallback.bind(this))
|
|
16
|
+
this.visualMoveInput = new VisualMoveInput(this)
|
|
21
17
|
if (chessboard.props.assetsCache) {
|
|
22
18
|
this.cacheSpriteToDiv("cm-chessboard-sprite", this.getSpriteUrl())
|
|
23
19
|
}
|
|
@@ -395,6 +391,20 @@ export class ChessboardView {
|
|
|
395
391
|
}
|
|
396
392
|
}
|
|
397
393
|
|
|
394
|
+
moveInputFinishedCallback(squareFrom, squareTo, legalMove) {
|
|
395
|
+
const data = {
|
|
396
|
+
chessboard: this.chessboard,
|
|
397
|
+
type: INPUT_EVENT_TYPE.moveInputFinished,
|
|
398
|
+
squareFrom: squareFrom,
|
|
399
|
+
squareTo: squareTo,
|
|
400
|
+
legalMove: legalMove
|
|
401
|
+
}
|
|
402
|
+
this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.moveInput, data)
|
|
403
|
+
if (this.moveInputCallback) {
|
|
404
|
+
this.moveInputCallback(data)
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
398
408
|
// Helpers //
|
|
399
409
|
|
|
400
410
|
visualizeInputState() {
|
|
@@ -30,27 +30,32 @@ const DRAG_THRESHOLD = 4
|
|
|
30
30
|
|
|
31
31
|
export class VisualMoveInput {
|
|
32
32
|
|
|
33
|
-
constructor(view
|
|
33
|
+
constructor(view) {
|
|
34
34
|
this.view = view
|
|
35
35
|
this.chessboard = view.chessboard
|
|
36
36
|
this.moveInputState = null
|
|
37
|
+
this.fromSquare = null
|
|
38
|
+
this.toSquare = null
|
|
37
39
|
this.moveInputStartedCallback = (square) => {
|
|
38
|
-
const result = moveInputStartedCallback(square)
|
|
40
|
+
const result = view.moveInputStartedCallback(square)
|
|
39
41
|
if (result) {
|
|
40
42
|
this.chessboard.state.moveInputProcess = Utils.createTask()
|
|
43
|
+
this.chessboard.state.moveInputProcess.then((result) => {
|
|
44
|
+
view.moveInputFinishedCallback(this.fromSquare, this.toSquare, result)
|
|
45
|
+
})
|
|
41
46
|
}
|
|
42
47
|
return result
|
|
43
48
|
}
|
|
44
49
|
this.movingOverSquareCallback = (fromSquare, toSquare) => {
|
|
45
|
-
movingOverSquareCallback(fromSquare, toSquare)
|
|
50
|
+
view.movingOverSquareCallback(fromSquare, toSquare)
|
|
46
51
|
}
|
|
47
52
|
this.validateMoveInputCallback = (fromSquare, toSquare) => {
|
|
48
|
-
const result = validateMoveInputCallback(fromSquare, toSquare)
|
|
53
|
+
const result = view.validateMoveInputCallback(fromSquare, toSquare)
|
|
49
54
|
this.chessboard.state.moveInputProcess.resolve(result)
|
|
50
55
|
return result
|
|
51
56
|
}
|
|
52
57
|
this.moveInputCanceledCallback = (fromSquare, toSquare, reason) => {
|
|
53
|
-
moveInputCanceledCallback(fromSquare, toSquare, reason)
|
|
58
|
+
view.moveInputCanceledCallback(fromSquare, toSquare, reason)
|
|
54
59
|
this.chessboard.state.moveInputProcess.resolve()
|
|
55
60
|
}
|
|
56
61
|
this.setMoveInputState(MOVE_INPUT_STATE.waitForInputStart)
|