cm-chessboard 5.2.1 → 5.2.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 +29 -32
- package/examples/destroy-many-boards.html +2 -1
- package/examples/different-styles.html +10 -9
- package/examples/enable-input.html +15 -14
- package/examples/input-callbacks.html +2 -1
- package/examples/pieces-animation.html +2 -1
- package/examples/responsive-board.html +4 -3
- package/examples/simple-boards.html +4 -3
- package/examples/validate-moves.html +3 -3
- package/package.json +1 -1
- package/src/cm-chessboard/Chessboard.js +7 -4
- package/src/cm-chessboard/extensions/accessibility/Accessibility.js +1 -1
- package/src/cm-chessboard/model/Extension.js +1 -1
- package/src/cm-chessboard/model/Position.js +3 -3
- package/src/cm-chessboard/view/ChessboardView.js +11 -11
- package/src/cm-chessboard/view/VisualMoveInput.js +14 -14
- package/test/TestBoard.js +3 -2
- package/test/TestChessboard.js +3 -3
- package/test/TestMarkers.js +1 -2
package/README.md
CHANGED
|
@@ -72,23 +72,23 @@ Below is the default configuration
|
|
|
72
72
|
|
|
73
73
|
```javascript
|
|
74
74
|
let defaultProps = {
|
|
75
|
-
position:
|
|
75
|
+
position: FEN.empty, // set as fen, can use FEN.start or FEN.empty
|
|
76
76
|
orientation: COLOR.white, // white on bottom
|
|
77
77
|
responsive: true, // resize the board automatically to the size of the context element
|
|
78
78
|
animationDuration: 300, // pieces animation duration in milliseconds. Disable all animation with `0`.
|
|
79
79
|
language: navigator.language.substring(0, 2).toLowerCase(), // supports "de" and "en" for now, used for pieces naming
|
|
80
80
|
style: {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
81
|
+
cssClass: "default", // set the css theme of the board, try "green", "blue" or "chess-club"
|
|
82
|
+
showCoordinates: true, // show ranks and files
|
|
83
|
+
borderType: BORDER_TYPE.none, // "thin" thin border, "frame" wide border with coordinates in it, "none" no border
|
|
84
|
+
aspectRatio: 1, // height/width of the board
|
|
85
|
+
moveFromMarker: MARKER_TYPE.frame, // the marker used to mark the start square
|
|
86
|
+
moveToMarker: MARKER_TYPE.frame, // the marker used to mark the square where the figure is moving to
|
|
87
87
|
},
|
|
88
88
|
sprite: {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
89
|
+
url: "./assets/images/chessboard-sprite.svg", // pieces and markers are stored in a sprite file
|
|
90
|
+
size: 40, // the sprite tiles size, defaults to 40x40px
|
|
91
|
+
cache: true // cache the sprite
|
|
92
92
|
},
|
|
93
93
|
extensions: [ /* {class: ExtensionClass, props: { ... }} */] // add extensions here
|
|
94
94
|
}
|
|
@@ -122,8 +122,7 @@ Move a piece from `squareFrom` to `squareTo`. Returns a **Promise**, which is re
|
|
|
122
122
|
|
|
123
123
|
### setPosition(fen, animated = false)
|
|
124
124
|
|
|
125
|
-
Sets the position as `fen`.
|
|
126
|
-
"empty", sets an empty board. Returns a **Promise**, which is resolved, after the animation finished.
|
|
125
|
+
Sets the position as `fen`. Returns a **Promise**, which is resolved, after the animation finished.
|
|
127
126
|
|
|
128
127
|
[Example for **setPosition**](https://shaack.com/projekte/cm-chessboard/examples/pieces-animation.html)
|
|
129
128
|
|
|
@@ -202,26 +201,24 @@ board.enableMoveInput((event) => {
|
|
|
202
201
|
|
|
203
202
|
The event has the following **`event.type`**:
|
|
204
203
|
|
|
205
|
-
- **`INPUT_EVENT_TYPE.
|
|
206
|
-
- **`INPUT_EVENT_TYPE.
|
|
207
|
-
|
|
208
|
-
- **`INPUT_EVENT_TYPE.moveCanceled`**: User canceled the move with clicking again on the start square or clicking
|
|
209
|
-
outside of the board
|
|
204
|
+
- **`INPUT_EVENT_TYPE.moveInputStarted`**: User started the move input, `event.square` contains the coordinates. Return true or false to validate the start square.
|
|
205
|
+
- **`INPUT_EVENT_TYPE.validateMoveInput`**: User finished the move input, `event.squareFrom` and `event.squareTo` contain the coordinates. Return true or false to validate the move input.
|
|
206
|
+
- **`INPUT_EVENT_TYPE.moveInputCanceled`**: User canceled the move with clicking again on the start square or clicking outside the board.
|
|
210
207
|
|
|
211
208
|
```javascript
|
|
212
209
|
chessboard.enableMoveInput((event) => {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
210
|
+
switch (event.type) {
|
|
211
|
+
case INPUT_EVENT_TYPE.moveInputStarted:
|
|
212
|
+
console.log(`moveInputStarted: ${event.square}`)
|
|
213
|
+
// return `true`, if input is accepted/valid, `false` aborts the interaction, the piece will not move
|
|
214
|
+
return true
|
|
215
|
+
case INPUT_EVENT_TYPE.validateMoveInput:
|
|
216
|
+
console.log(`validateMoveInput: ${event.squareFrom}-${event.squareTo}`)
|
|
217
|
+
// return true, if input is accepted/valid, `false` takes the move back
|
|
218
|
+
return true
|
|
219
|
+
case INPUT_EVENT_TYPE.moveInputCanceled:
|
|
220
|
+
console.log(`moveInputCanceled`)
|
|
221
|
+
}
|
|
225
222
|
}, COLOR.white)
|
|
226
223
|
```
|
|
227
224
|
|
|
@@ -350,7 +347,7 @@ export const EXTENSION_POINT = {
|
|
|
350
347
|
positionChanged: "positionChanged", // the positions of the pieces was changed
|
|
351
348
|
boardChanged: "boardChanged", // the board (orientation) was changed
|
|
352
349
|
moveInputToggled: "moveInputToggled", // move input was enabled or disabled
|
|
353
|
-
moveInput: "moveInput", // move started,
|
|
350
|
+
moveInput: "moveInput", // move started, canceled or validation // TODO validation in extensions is not awailable for now
|
|
354
351
|
destroy: "destroy" // called, before the board is destroyed
|
|
355
352
|
}
|
|
356
353
|
```
|
|
@@ -359,7 +356,7 @@ Enable extensions via the chessboard props.
|
|
|
359
356
|
|
|
360
357
|
```js
|
|
361
358
|
const chessboard = new Chessboard(document.getElementById("board"), {
|
|
362
|
-
position:
|
|
359
|
+
position: FEN.start,
|
|
363
360
|
extensions: // list of used extensions
|
|
364
361
|
[{
|
|
365
362
|
class: MyCoolChessboardExtension, // the class of the extension
|
|
@@ -397,7 +394,7 @@ example [Accessibility extension](https://shaack.com/projekte/cm-chessboard/exam
|
|
|
397
394
|
|
|
398
395
|
```js
|
|
399
396
|
const chessboard = new Chessboard(document.getElementById("board"), {
|
|
400
|
-
position:
|
|
397
|
+
position: FEN.start,
|
|
401
398
|
sprite: {url: "../assets/images/chessboard-sprite.svg"},
|
|
402
399
|
// animationDuration: 0, // optional, set to 0 to disable animations
|
|
403
400
|
style: {
|
|
@@ -31,12 +31,13 @@
|
|
|
31
31
|
<div id="boards"></div>
|
|
32
32
|
<script type="module">
|
|
33
33
|
import {Chessboard} from "../src/cm-chessboard/Chessboard.js"
|
|
34
|
+
import {FEN} from "../src/cm-chessboard/model/Position.js"
|
|
34
35
|
const boardsDiv = document.getElementById("boards")
|
|
35
36
|
const boardDiv = document.createElement("div")
|
|
36
37
|
boardDiv.setAttribute("class", "board1")
|
|
37
38
|
boardsDiv.appendChild(boardDiv)
|
|
38
39
|
const largeBoard = new Chessboard(boardDiv, {
|
|
39
|
-
position:
|
|
40
|
+
position: FEN.start,
|
|
40
41
|
sprite: {url: "../assets/images/chessboard-sprite-staunty.svg"},
|
|
41
42
|
})
|
|
42
43
|
const chess = new Chess()
|
|
@@ -21,9 +21,10 @@
|
|
|
21
21
|
<div class="board" id="board9"></div>
|
|
22
22
|
<script type="module">
|
|
23
23
|
import {Chessboard, BORDER_TYPE} from "../src/cm-chessboard/Chessboard.js"
|
|
24
|
+
import {FEN} from "../src/cm-chessboard/model/Position.js"
|
|
24
25
|
|
|
25
26
|
new Chessboard(document.getElementById("board1"), {
|
|
26
|
-
position:
|
|
27
|
+
position: FEN.start,
|
|
27
28
|
style: {
|
|
28
29
|
cssClass: "green",
|
|
29
30
|
borderType: BORDER_TYPE.frame
|
|
@@ -31,7 +32,7 @@
|
|
|
31
32
|
sprite: {url: "../assets/images/chessboard-sprite-staunty.svg"},
|
|
32
33
|
})
|
|
33
34
|
new Chessboard(document.getElementById("board2"), {
|
|
34
|
-
position:
|
|
35
|
+
position: FEN.start,
|
|
35
36
|
style: {
|
|
36
37
|
cssClass: "chessboard-js",
|
|
37
38
|
borderType: BORDER_TYPE.thin
|
|
@@ -39,7 +40,7 @@
|
|
|
39
40
|
sprite: {url: "../assets/images/chessboard-sprite.svg", cache: false}
|
|
40
41
|
})
|
|
41
42
|
new Chessboard(document.getElementById("board3"), {
|
|
42
|
-
position:
|
|
43
|
+
position: FEN.start,
|
|
43
44
|
style: {
|
|
44
45
|
cssClass: "chess-club",
|
|
45
46
|
borderType: BORDER_TYPE.frame
|
|
@@ -47,7 +48,7 @@
|
|
|
47
48
|
sprite: {url: "../assets/images/chessboard-sprite-staunty.svg"}
|
|
48
49
|
})
|
|
49
50
|
new Chessboard(document.getElementById("board4"), {
|
|
50
|
-
position:
|
|
51
|
+
position: FEN.start,
|
|
51
52
|
style: {
|
|
52
53
|
cssClass: "blue",
|
|
53
54
|
borderType: BORDER_TYPE.thin,
|
|
@@ -56,14 +57,14 @@
|
|
|
56
57
|
sprite: {url: "../assets/images/chessboard-sprite.svg", cache: false}
|
|
57
58
|
})
|
|
58
59
|
new Chessboard(document.getElementById("board5"), {
|
|
59
|
-
position:
|
|
60
|
+
position: FEN.start,
|
|
60
61
|
style: {
|
|
61
62
|
borderType: BORDER_TYPE.none
|
|
62
63
|
},
|
|
63
64
|
sprite: {url: "../assets/images/chessboard-sprite-staunty.svg"},
|
|
64
65
|
})
|
|
65
66
|
new Chessboard(document.getElementById("board6"), {
|
|
66
|
-
position:
|
|
67
|
+
position: FEN.start,
|
|
67
68
|
style: {
|
|
68
69
|
cssClass: "blue",
|
|
69
70
|
borderType: BORDER_TYPE.frame
|
|
@@ -71,21 +72,21 @@
|
|
|
71
72
|
sprite: {url: "../assets/images/chessboard-sprite-staunty.svg"},
|
|
72
73
|
})
|
|
73
74
|
new Chessboard(document.getElementById("board7"), {
|
|
74
|
-
position:
|
|
75
|
+
position: FEN.start,
|
|
75
76
|
style: {
|
|
76
77
|
cssClass: "green"
|
|
77
78
|
},
|
|
78
79
|
sprite: {url: "../assets/images/chessboard-sprite.svg", cache: false},
|
|
79
80
|
})
|
|
80
81
|
new Chessboard(document.getElementById("board8"), {
|
|
81
|
-
position:
|
|
82
|
+
position: FEN.start,
|
|
82
83
|
style: {
|
|
83
84
|
cssClass: "black-and-white"
|
|
84
85
|
},
|
|
85
86
|
sprite: {url: "../assets/images/chessboard-sprite.svg", cache: false},
|
|
86
87
|
})
|
|
87
88
|
new Chessboard(document.getElementById("board9"), {
|
|
88
|
-
position:
|
|
89
|
+
position: FEN.start,
|
|
89
90
|
style: {
|
|
90
91
|
cssClass: "chessboard-js",
|
|
91
92
|
borderType: BORDER_TYPE.frame
|
|
@@ -14,21 +14,21 @@
|
|
|
14
14
|
<div class="board" id="board"></div>
|
|
15
15
|
<pre>
|
|
16
16
|
const board = new Chessboard(document.getElementById("board"), {
|
|
17
|
-
position:
|
|
17
|
+
position: FEN.start,
|
|
18
18
|
sprite: {url: "../assets/images/chessboard-sprite-staunty.svg"}
|
|
19
19
|
})
|
|
20
20
|
|
|
21
21
|
board.enableMoveInput(inputHandler)
|
|
22
22
|
function inputHandler(event) {
|
|
23
23
|
switch (event.type) {
|
|
24
|
-
case INPUT_EVENT_TYPE.
|
|
25
|
-
log(`
|
|
24
|
+
case INPUT_EVENT_TYPE.moveInputStarted:
|
|
25
|
+
log(`moveInputStarted: ${event.square}`)
|
|
26
26
|
return true
|
|
27
|
-
case INPUT_EVENT_TYPE.
|
|
28
|
-
log(`
|
|
27
|
+
case INPUT_EVENT_TYPE.validateMoveInput:
|
|
28
|
+
log(`validateMoveInput: ${event.squareFrom}-${event.squareTo}`)
|
|
29
29
|
return true
|
|
30
|
-
case INPUT_EVENT_TYPE.
|
|
31
|
-
log(`
|
|
30
|
+
case INPUT_EVENT_TYPE.moveInputCanceled:
|
|
31
|
+
log(`moveInputCanceled`)
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
</pre>
|
|
@@ -38,9 +38,10 @@ function inputHandler(event) {
|
|
|
38
38
|
<div id="output" style="width: 100%; overflow: hidden"></div>
|
|
39
39
|
<script type="module">
|
|
40
40
|
import {INPUT_EVENT_TYPE, Chessboard} from "../src/cm-chessboard/Chessboard.js"
|
|
41
|
+
import {FEN} from "../src/cm-chessboard/model/Position.js"
|
|
41
42
|
|
|
42
43
|
window.board = new Chessboard(document.getElementById("board"), {
|
|
43
|
-
position:
|
|
44
|
+
position: FEN.start,
|
|
44
45
|
sprite: {url: "../assets/images/chessboard-sprite-staunty.svg"}
|
|
45
46
|
})
|
|
46
47
|
|
|
@@ -48,14 +49,14 @@ function inputHandler(event) {
|
|
|
48
49
|
function inputHandler(event) {
|
|
49
50
|
console.log(event)
|
|
50
51
|
switch (event.type) {
|
|
51
|
-
case INPUT_EVENT_TYPE.
|
|
52
|
-
log(`
|
|
52
|
+
case INPUT_EVENT_TYPE.moveInputStarted:
|
|
53
|
+
log(`moveInputStarted: ${event.square}`)
|
|
53
54
|
return true
|
|
54
|
-
case INPUT_EVENT_TYPE.
|
|
55
|
-
log(`
|
|
55
|
+
case INPUT_EVENT_TYPE.validateMoveInput:
|
|
56
|
+
log(`validateMoveInput: ${event.squareFrom}-${event.squareTo}`)
|
|
56
57
|
return true
|
|
57
|
-
case INPUT_EVENT_TYPE.
|
|
58
|
-
log(`
|
|
58
|
+
case INPUT_EVENT_TYPE.moveInputCanceled:
|
|
59
|
+
log(`moveInputCanceled`)
|
|
59
60
|
}
|
|
60
61
|
}
|
|
61
62
|
|
|
@@ -32,9 +32,10 @@
|
|
|
32
32
|
</pre>
|
|
33
33
|
<script type="module">
|
|
34
34
|
import {Chessboard, MARKER_TYPE, SQUARE_SELECT_TYPE} from "../src/cm-chessboard/Chessboard.js"
|
|
35
|
+
import {FEN} from "../src/cm-chessboard/model/Position.js"
|
|
35
36
|
|
|
36
37
|
const board1 = new Chessboard(document.getElementById("board1"), {
|
|
37
|
-
position:
|
|
38
|
+
position: FEN.start,
|
|
38
39
|
sprite: {url: "../assets/images/chessboard-sprite.svg", cache: false}
|
|
39
40
|
})
|
|
40
41
|
board1.enableSquareSelect((event) => {
|
|
@@ -45,10 +45,11 @@ await board.movePiece("d5", "f4")
|
|
|
45
45
|
</pre>
|
|
46
46
|
<script type="module">
|
|
47
47
|
import {Chessboard, PIECE} from "../src/cm-chessboard/Chessboard.js"
|
|
48
|
+
import {FEN} from "../src/cm-chessboard/model/Position.js"
|
|
48
49
|
|
|
49
50
|
const board = new Chessboard(document.getElementById("board"),
|
|
50
51
|
{
|
|
51
|
-
position:
|
|
52
|
+
position: FEN.start,
|
|
52
53
|
sprite: {url: "../assets/images/chessboard-sprite-staunty.svg"},
|
|
53
54
|
animationDuration: 500
|
|
54
55
|
})
|
|
@@ -13,15 +13,16 @@
|
|
|
13
13
|
<div id="board"></div>
|
|
14
14
|
<pre>
|
|
15
15
|
new Chessboard(document.getElementById("board"), {
|
|
16
|
-
position:
|
|
16
|
+
position: FEN.start,
|
|
17
17
|
style: {aspectRatio: 0.9}
|
|
18
18
|
})
|
|
19
19
|
</pre>
|
|
20
20
|
<script type="module">
|
|
21
21
|
import {Chessboard} from "../src/cm-chessboard/Chessboard.js"
|
|
22
|
+
import {FEN} from "../src/cm-chessboard/model/Position.js"
|
|
22
23
|
|
|
23
24
|
new Chessboard(document.getElementById("board"), {
|
|
24
|
-
position:
|
|
25
|
+
position: FEN.start,
|
|
25
26
|
sprite: {url: "../assets/images/chessboard-sprite-staunty.svg"},
|
|
26
27
|
style: {
|
|
27
28
|
aspectRatio: 0.9
|
|
@@ -29,4 +30,4 @@ new Chessboard(document.getElementById("board"), {
|
|
|
29
30
|
})
|
|
30
31
|
</script>
|
|
31
32
|
</body>
|
|
32
|
-
</html>
|
|
33
|
+
</html>
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
<br style="clear: both"/>
|
|
16
16
|
<pre>
|
|
17
17
|
new Chessboard(document.getElementById("board1"), {
|
|
18
|
-
position:
|
|
18
|
+
position: FEN.start
|
|
19
19
|
})
|
|
20
20
|
new Chessboard(document.getElementById("board2"), {
|
|
21
21
|
position: "rn2k1r1/ppp1pp1p/3p2p1/5bn1/P7/2N2B2/1PPPPP2/2BNK1RR w Gkq - 4 11",
|
|
@@ -24,9 +24,10 @@ new Chessboard(document.getElementById("board2"), {
|
|
|
24
24
|
</pre>
|
|
25
25
|
<script type="module">
|
|
26
26
|
import {COLOR, Chessboard} from "../src/cm-chessboard/Chessboard.js"
|
|
27
|
+
import {FEN} from "../src/cm-chessboard/model/Position.js"
|
|
27
28
|
|
|
28
29
|
new Chessboard(document.getElementById("board1"), {
|
|
29
|
-
position:
|
|
30
|
+
position: FEN.start,
|
|
30
31
|
sprite: {url: "../assets/images/chessboard-sprite-staunty.svg"},
|
|
31
32
|
})
|
|
32
33
|
new Chessboard(document.getElementById("board2"), {
|
|
@@ -36,4 +37,4 @@ new Chessboard(document.getElementById("board2"), {
|
|
|
36
37
|
})
|
|
37
38
|
</script>
|
|
38
39
|
</body>
|
|
39
|
-
</html>
|
|
40
|
+
</html>
|
|
@@ -30,7 +30,7 @@ function inputHandler(event) {
|
|
|
30
30
|
event.chessboard.addMarker(MARKER_TYPE.dot, move.to)
|
|
31
31
|
}
|
|
32
32
|
return moves.length > 0
|
|
33
|
-
} else if (event.type === INPUT_EVENT_TYPE.
|
|
33
|
+
} else if (event.type === INPUT_EVENT_TYPE.validateMoveInput) {
|
|
34
34
|
const move = {from: event.squareFrom, to: event.squareTo}
|
|
35
35
|
const result = chess.move(move)
|
|
36
36
|
if (result) {
|
|
@@ -74,13 +74,13 @@ board.enableMoveInput(inputHandler, COLOR.white)
|
|
|
74
74
|
function inputHandler(event) {
|
|
75
75
|
console.log("event", event)
|
|
76
76
|
event.chessboard.removeMarkers(MARKER_TYPE.dot)
|
|
77
|
-
if (event.type === INPUT_EVENT_TYPE.
|
|
77
|
+
if (event.type === INPUT_EVENT_TYPE.moveInputStarted) {
|
|
78
78
|
const moves = chess.moves({square: event.square, verbose: true});
|
|
79
79
|
for (const move of moves) { // draw dots on possible squares
|
|
80
80
|
event.chessboard.addMarker(MARKER_TYPE.dot, move.to)
|
|
81
81
|
}
|
|
82
82
|
return moves.length > 0
|
|
83
|
-
} else if (event.type === INPUT_EVENT_TYPE.
|
|
83
|
+
} else if (event.type === INPUT_EVENT_TYPE.validateMoveInput) {
|
|
84
84
|
const move = {from: event.squareFrom, to: event.squareTo}
|
|
85
85
|
const result = chess.move(move)
|
|
86
86
|
if (result) {
|
package/package.json
CHANGED
|
@@ -15,9 +15,12 @@ export const COLOR = {
|
|
|
15
15
|
black: "b"
|
|
16
16
|
}
|
|
17
17
|
export const INPUT_EVENT_TYPE = {
|
|
18
|
-
moveStart: "
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
moveStart: "moveInputStarted", // TODO deprecated 2022-08-24, use `moveInputStarted`
|
|
19
|
+
moveInputStarted: "moveInputStarted",
|
|
20
|
+
moveDone: "validateMoveInput", // TODO deprecated 2022-08-24, use `validateMoveInput` https://github.com/shaack/cm-chessboard/issues/83
|
|
21
|
+
validateMoveInput: "validateMoveInput",
|
|
22
|
+
moveCanceled: "moveInputCanceled", // TODO deprecated 2022-08-24, use `moveInputCanceled`
|
|
23
|
+
moveInputCanceled: "moveInputCanceled",
|
|
21
24
|
}
|
|
22
25
|
export const SQUARE_SELECT_TYPE = {
|
|
23
26
|
primary: "primary",
|
|
@@ -49,7 +52,7 @@ export class Chessboard {
|
|
|
49
52
|
this.id = (Math.random() + 1).toString(36).substring(2, 8)
|
|
50
53
|
this.extensions = []
|
|
51
54
|
let defaultProps = {
|
|
52
|
-
position: FEN.empty, // set as fen, can use FEN.start or FEN.empty
|
|
55
|
+
position: FEN.empty, // set as fen, can use FEN.start or FEN.empty
|
|
53
56
|
orientation: COLOR.white, // white on bottom
|
|
54
57
|
responsive: true, // resize the board automatically to the size of the context element
|
|
55
58
|
animationDuration: 300, // pieces animation duration in milliseconds. Disable all animation with `0`.
|
|
@@ -65,7 +65,7 @@ export class Accessibility extends Extension {
|
|
|
65
65
|
evt.preventDefault()
|
|
66
66
|
if (this.chessboard.view.moveInputCallback({
|
|
67
67
|
chessboard: this.chessboard,
|
|
68
|
-
type: INPUT_EVENT_TYPE.
|
|
68
|
+
type: INPUT_EVENT_TYPE.validateMoveInput,
|
|
69
69
|
squareFrom: this.inputFrom.value,
|
|
70
70
|
squareTo: this.inputTo.value
|
|
71
71
|
})) {
|
|
@@ -8,7 +8,7 @@ export const EXTENSION_POINT = {
|
|
|
8
8
|
positionChanged: "positionChanged", // the positions of the pieces was changed
|
|
9
9
|
boardChanged: "boardChanged", // the board (orientation) was changed
|
|
10
10
|
moveInputToggled: "moveInputToggled", // move input was enabled or disabled
|
|
11
|
-
moveInput: "moveInput", // move started,
|
|
11
|
+
moveInput: "moveInput", // move started, to validate or canceled // TODO validation not possible for now, see https://github.com/shaack/cm-chessboard/issues/82
|
|
12
12
|
moveInputStateChanged: "moveInput", // TODO deprecated, use `moveInput`
|
|
13
13
|
destroy: "destroy", // called, before the board is destroyed
|
|
14
14
|
redrawBoard: "redrawBoard" // called while redrawing the board
|
|
@@ -17,14 +17,14 @@ export class Position {
|
|
|
17
17
|
this.setFen(fen)
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
setFen(fen =
|
|
20
|
+
setFen(fen = FEN.empty) {
|
|
21
21
|
let fenNormalized
|
|
22
22
|
if (fen === "start") {
|
|
23
23
|
console.warn("setting the position with the strings 'start' or 'empty' is deprecated, use FEN.start or FEN.empty")
|
|
24
|
-
fenNormalized =
|
|
24
|
+
fenNormalized = FEN.start
|
|
25
25
|
} else if (fen === "empty" || fen === undefined) {
|
|
26
26
|
console.warn("setting the position with the strings 'start' or 'empty' is deprecated, use FEN.start or FEN.empty")
|
|
27
|
-
fenNormalized =
|
|
27
|
+
fenNormalized = FEN.empty
|
|
28
28
|
} else {
|
|
29
29
|
fenNormalized = fen
|
|
30
30
|
}
|
|
@@ -53,9 +53,9 @@ export class ChessboardView {
|
|
|
53
53
|
constructor(chessboard) {
|
|
54
54
|
this.chessboard = chessboard
|
|
55
55
|
this.moveInput = new VisualMoveInput(this,
|
|
56
|
-
this.
|
|
57
|
-
this.
|
|
58
|
-
this.
|
|
56
|
+
this.moveInputStartedCallback.bind(this),
|
|
57
|
+
this.validateMoveInputCallback.bind(this),
|
|
58
|
+
this.moveInputCanceledCallback.bind(this)
|
|
59
59
|
)
|
|
60
60
|
if (chessboard.props.sprite.cache) {
|
|
61
61
|
this.cacheSpriteToDiv("chessboardSpriteCache", this.chessboard.props.sprite.url)
|
|
@@ -391,13 +391,13 @@ export class ChessboardView {
|
|
|
391
391
|
|
|
392
392
|
// callbacks //
|
|
393
393
|
|
|
394
|
-
|
|
394
|
+
moveInputStartedCallback(square) {
|
|
395
395
|
const data = {
|
|
396
396
|
chessboard: this.chessboard,
|
|
397
|
-
type: INPUT_EVENT_TYPE.
|
|
397
|
+
type: INPUT_EVENT_TYPE.moveInputStarted,
|
|
398
398
|
square: square
|
|
399
399
|
}
|
|
400
|
-
this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.moveInput, data)
|
|
400
|
+
this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.moveInput, data) // TODO use the return value of this EP
|
|
401
401
|
if (this.moveInputCallback) {
|
|
402
402
|
return this.moveInputCallback(data)
|
|
403
403
|
} else {
|
|
@@ -405,14 +405,14 @@ export class ChessboardView {
|
|
|
405
405
|
}
|
|
406
406
|
}
|
|
407
407
|
|
|
408
|
-
|
|
408
|
+
validateMoveInputCallback(squareFrom, squareTo) {
|
|
409
409
|
const data = {
|
|
410
410
|
chessboard: this.chessboard,
|
|
411
|
-
type: INPUT_EVENT_TYPE.
|
|
411
|
+
type: INPUT_EVENT_TYPE.validateMoveInput,
|
|
412
412
|
squareFrom: squareFrom,
|
|
413
413
|
squareTo: squareTo
|
|
414
414
|
}
|
|
415
|
-
this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.moveInput, data)
|
|
415
|
+
this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.moveInput, data) // TODO use the return value of this EP
|
|
416
416
|
if (this.moveInputCallback) {
|
|
417
417
|
return this.moveInputCallback(data)
|
|
418
418
|
} else {
|
|
@@ -420,10 +420,10 @@ export class ChessboardView {
|
|
|
420
420
|
}
|
|
421
421
|
}
|
|
422
422
|
|
|
423
|
-
|
|
423
|
+
moveInputCanceledCallback(reason, squareFrom, squareTo) {
|
|
424
424
|
const data = {
|
|
425
425
|
chessboard: this.chessboard,
|
|
426
|
-
type: INPUT_EVENT_TYPE.
|
|
426
|
+
type: INPUT_EVENT_TYPE.moveInputCanceled,
|
|
427
427
|
reason: reason,
|
|
428
428
|
squareFrom: squareFrom,
|
|
429
429
|
squareTo: squareTo
|
|
@@ -29,23 +29,23 @@ const DRAG_THRESHOLD = 4
|
|
|
29
29
|
|
|
30
30
|
export class VisualMoveInput {
|
|
31
31
|
|
|
32
|
-
constructor(view,
|
|
32
|
+
constructor(view, moveInputStartedCallback, validateMoveInputCallback, moveInputCanceledCallback) {
|
|
33
33
|
this.view = view
|
|
34
34
|
this.chessboard = view.chessboard
|
|
35
|
-
this.
|
|
36
|
-
const result =
|
|
35
|
+
this.moveInputStartedCallback = (square) => {
|
|
36
|
+
const result = moveInputStartedCallback(square)
|
|
37
37
|
if(result) {
|
|
38
38
|
this.chessboard.state.moveInputProcess = createTask()
|
|
39
39
|
}
|
|
40
40
|
return result
|
|
41
41
|
}
|
|
42
|
-
this.
|
|
43
|
-
const result =
|
|
42
|
+
this.validateMoveInputCallback = (fromSquare, toSquare) => {
|
|
43
|
+
const result = validateMoveInputCallback(fromSquare, toSquare)
|
|
44
44
|
this.chessboard.state.moveInputProcess.resolve(result)
|
|
45
45
|
return result
|
|
46
46
|
}
|
|
47
|
-
this.
|
|
48
|
-
|
|
47
|
+
this.moveInputCanceledCallback = (reason, fromSquare, toSquare) => {
|
|
48
|
+
moveInputCanceledCallback(reason, fromSquare, toSquare)
|
|
49
49
|
this.chessboard.state.moveInputProcess.resolve()
|
|
50
50
|
}
|
|
51
51
|
this.setMoveInputState(STATE.waitForInputStart)
|
|
@@ -151,7 +151,7 @@ export class VisualMoveInput {
|
|
|
151
151
|
throw new Error("moveInputState")
|
|
152
152
|
}
|
|
153
153
|
this.toSquare = params.square
|
|
154
|
-
if (this.toSquare && this.
|
|
154
|
+
if (this.toSquare && this.validateMoveInputCallback(this.fromSquare, this.toSquare)) {
|
|
155
155
|
if (prevState === STATE.clickTo) {
|
|
156
156
|
this.chessboard.movePiece(this.fromSquare, this.toSquare, true).then(() => {
|
|
157
157
|
this.setMoveInputState(STATE.reset)
|
|
@@ -246,7 +246,7 @@ export class VisualMoveInput {
|
|
|
246
246
|
} else if (e.type === "touchstart") {
|
|
247
247
|
point = {x: e.touches[0].clientX, y: e.touches[0].clientY}
|
|
248
248
|
}
|
|
249
|
-
if (this.moveInputState === STATE.waitForInputStart && pieceName && this.
|
|
249
|
+
if (this.moveInputState === STATE.waitForInputStart && pieceName && this.moveInputStartedCallback(square)) {
|
|
250
250
|
this.setMoveInputState(STATE.pieceClickedThreshold, {
|
|
251
251
|
square: square,
|
|
252
252
|
piece: pieceName,
|
|
@@ -267,8 +267,8 @@ export class VisualMoveInput {
|
|
|
267
267
|
const startPieceName = this.chessboard.getPiece(this.fromSquare)
|
|
268
268
|
const startPieceColor = startPieceName ? startPieceName.substring(0, 1) : undefined
|
|
269
269
|
if (color && startPieceColor === pieceColor) {
|
|
270
|
-
this.
|
|
271
|
-
if (this.
|
|
270
|
+
this.moveInputCanceledCallback(MOVE_CANCELED_REASON.clickedAnotherPiece, this.fromSquare, square)
|
|
271
|
+
if (this.moveInputStartedCallback(square)) {
|
|
272
272
|
this.setMoveInputState(STATE.pieceClickedThreshold, {
|
|
273
273
|
square: square,
|
|
274
274
|
piece: pieceName,
|
|
@@ -352,7 +352,7 @@ export class VisualMoveInput {
|
|
|
352
352
|
if (this.moveInputState === STATE.clickDragTo) {
|
|
353
353
|
this.chessboard.state.position.setPiece(this.fromSquare, this.movedPiece)
|
|
354
354
|
this.view.setPieceVisibility(this.fromSquare)
|
|
355
|
-
this.
|
|
355
|
+
this.moveInputCanceledCallback(MOVE_CANCELED_REASON.draggedBack, square, square)
|
|
356
356
|
this.setMoveInputState(STATE.reset)
|
|
357
357
|
} else {
|
|
358
358
|
this.setMoveInputState(STATE.clickTo, {square: square})
|
|
@@ -364,13 +364,13 @@ export class VisualMoveInput {
|
|
|
364
364
|
this.setMoveInputState(STATE.clickTo, {square: square})
|
|
365
365
|
} else if (this.moveInputState === STATE.secondClickThreshold) {
|
|
366
366
|
this.setMoveInputState(STATE.reset)
|
|
367
|
-
this.
|
|
367
|
+
this.moveInputCanceledCallback(MOVE_CANCELED_REASON.secondClick, square, square)
|
|
368
368
|
}
|
|
369
369
|
} else {
|
|
370
370
|
this.view.redrawPieces()
|
|
371
371
|
const moveStartSquare = this.fromSquare
|
|
372
372
|
this.setMoveInputState(STATE.reset)
|
|
373
|
-
this.
|
|
373
|
+
this.moveInputCanceledCallback(MOVE_CANCELED_REASON.movedOutOfBoard, moveStartSquare, undefined)
|
|
374
374
|
}
|
|
375
375
|
} else {
|
|
376
376
|
this.view.redrawPieces()
|
package/test/TestBoard.js
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
import {describe, it, assert} from "../node_modules/teevi/src/teevi.js"
|
|
8
8
|
import {Chessboard} from "../src/cm-chessboard/Chessboard.js"
|
|
9
|
+
import {FEN} from "../src/cm-chessboard/model/Position.js"
|
|
9
10
|
|
|
10
11
|
describe("TestBoard", () => {
|
|
11
12
|
|
|
@@ -13,7 +14,7 @@ describe("TestBoard", () => {
|
|
|
13
14
|
it("should create and immediately destroy a board without failure", () => {
|
|
14
15
|
const chessboard = new Chessboard(document.getElementById("TestBoard"), {
|
|
15
16
|
sprite: {url: "../assets/images/chessboard-sprite.svg"},
|
|
16
|
-
position:
|
|
17
|
+
position: FEN.start
|
|
17
18
|
})
|
|
18
19
|
chessboard.destroy()
|
|
19
20
|
})
|
|
@@ -21,7 +22,7 @@ describe("TestBoard", () => {
|
|
|
21
22
|
it("should create and destroy a board", () => {
|
|
22
23
|
const chessboard = new Chessboard(document.getElementById("TestBoard"), {
|
|
23
24
|
sprite: {url: "../assets/images/chessboard-sprite.svg"},
|
|
24
|
-
position:
|
|
25
|
+
position: FEN.start
|
|
25
26
|
})
|
|
26
27
|
assert.equal(chessboard.view.context.childNodes.length, 1)
|
|
27
28
|
chessboard.destroy()
|
package/test/TestChessboard.js
CHANGED
|
@@ -6,13 +6,14 @@
|
|
|
6
6
|
|
|
7
7
|
import {describe, it, assert} from "../node_modules/teevi/src/teevi.js"
|
|
8
8
|
import {PIECE, Chessboard} from "../src/cm-chessboard/Chessboard.js"
|
|
9
|
+
import {FEN} from "../src/cm-chessboard/model/Position.js"
|
|
9
10
|
|
|
10
11
|
describe("TestChessboard", () => {
|
|
11
12
|
|
|
12
13
|
it("should create and destroy a chessboard", () => {
|
|
13
14
|
const chessboard = new Chessboard(document.getElementById("TestPosition"), {
|
|
14
15
|
sprite: {url: "../assets/images/chessboard-sprite.svg"},
|
|
15
|
-
position:
|
|
16
|
+
position: FEN.start
|
|
16
17
|
})
|
|
17
18
|
assert.equal(chessboard.getPosition(), "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR")
|
|
18
19
|
chessboard.destroy()
|
|
@@ -30,7 +31,7 @@ describe("TestChessboard", () => {
|
|
|
30
31
|
it("should get pieces on squares", () => {
|
|
31
32
|
const chessboard = new Chessboard(document.getElementById("TestPosition"), {
|
|
32
33
|
sprite: {url: "../assets/images/chessboard-sprite.svg"},
|
|
33
|
-
position:
|
|
34
|
+
position: FEN.start
|
|
34
35
|
})
|
|
35
36
|
assert.equal(chessboard.getPiece("d1"), "wq")
|
|
36
37
|
assert.equal(chessboard.getPiece("d8"), "bq")
|
|
@@ -40,7 +41,6 @@ describe("TestChessboard", () => {
|
|
|
40
41
|
|
|
41
42
|
it("should set pieces on squares", () => {
|
|
42
43
|
const chessboard = new Chessboard(document.getElementById("TestPosition"), {
|
|
43
|
-
position: "empty",
|
|
44
44
|
sprite: {url: "../assets/images/chessboard-sprite.svg"},
|
|
45
45
|
})
|
|
46
46
|
chessboard.setPiece("a1", PIECE.bk)
|
package/test/TestMarkers.js
CHANGED
|
@@ -11,8 +11,7 @@ describe("TestMarkers", () => {
|
|
|
11
11
|
|
|
12
12
|
it("should set and get markers", () => {
|
|
13
13
|
const chessboard = new Chessboard(document.getElementById("TestMarkers"), {
|
|
14
|
-
sprite: {url: "../assets/images/chessboard-sprite.svg"}
|
|
15
|
-
position: "empty"
|
|
14
|
+
sprite: {url: "../assets/images/chessboard-sprite.svg"}
|
|
16
15
|
})
|
|
17
16
|
chessboard.addMarker(MARKER_TYPE.square, "e5")
|
|
18
17
|
chessboard.addMarker(MARKER_TYPE.frame, "b6")
|