cm-chessboard 5.0.2 → 5.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.
- package/README.md +19 -13
- package/examples/extensions/chessboard-arrows-extension.html +8 -8
- package/examples/input-callbacks.html +9 -9
- package/examples/validate-moves.html +6 -6
- package/package.json +1 -1
- package/src/cm-chessboard/Chessboard.js +15 -6
- package/src/cm-chessboard/extensions/arrows/Arrows.js +16 -4
- package/test/TestMarkers.js +18 -18
package/README.md
CHANGED
|
@@ -132,7 +132,9 @@ Sets the position as `fen`. Special values are "start", sets the chess start pos
|
|
|
132
132
|
|
|
133
133
|
Returns the board position as `fen`.
|
|
134
134
|
|
|
135
|
-
### addMarker(
|
|
135
|
+
### addMarker(type, square)
|
|
136
|
+
|
|
137
|
+
> signature changed with V5.1 from (square, type) to (type, square)
|
|
136
138
|
|
|
137
139
|
Adds a marker on a square.
|
|
138
140
|
|
|
@@ -149,19 +151,23 @@ below.
|
|
|
149
151
|
[Example for **addMarker**, **getMarkers** and
|
|
150
152
|
**removeMarkers**](https://shaack.com/projekte/cm-chessboard/examples/context-input.html)
|
|
151
153
|
|
|
152
|
-
### getMarkers(
|
|
154
|
+
### getMarkers(type = undefined, square = undefined)
|
|
155
|
+
|
|
156
|
+
> signature changed with V5.1 from (square, type) to (type, square)
|
|
153
157
|
|
|
154
158
|
Returns the board's markers as an array.
|
|
155
159
|
|
|
156
|
-
|
|
157
|
-
Set `both` to undefined to get all markers on the board.
|
|
160
|
+
Only set type, to get all markers of a type on the board. Set type to `undefined`, to get markers of all types on a square.
|
|
161
|
+
Set `both` to `undefined` to get all markers on the board.
|
|
162
|
+
|
|
163
|
+
### removeMarkers(type = undefined, square = undefined)
|
|
158
164
|
|
|
159
|
-
|
|
165
|
+
> signature changed with V5.1 from (square, type) to (type, square)
|
|
160
166
|
|
|
161
167
|
Removes markers from the board.
|
|
162
168
|
|
|
163
|
-
|
|
164
|
-
from a square.
|
|
169
|
+
Only set `type` to remove all markers of `type` from the board. Set `type` to `undefined`, to remove all types
|
|
170
|
+
of markers from a square. Call without parameters to remove all markers from the board.
|
|
165
171
|
|
|
166
172
|
### setOrientation(color)
|
|
167
173
|
|
|
@@ -277,7 +283,7 @@ Example: The markerCircle is defined in the SVG like this.
|
|
|
277
283
|
It's a circle with the radius 18 and its center at 20/20.
|
|
278
284
|
|
|
279
285
|
Important is the id "markerCircle". You can set the marker
|
|
280
|
-
with `board.addMarker(
|
|
286
|
+
with `board.addMarker({class: "markerSquare", slice: "markerSquare"}, "e4")`
|
|
281
287
|
"emphasize" is the css class, which defines the color and opacity of the marker. "slice" is the id of the marker in the
|
|
282
288
|
SVG. This is
|
|
283
289
|
also demonstrated in the [mark squares example](https://shaack.com/projekte/cm-chessboard/examples/input-callbacks.html)
|
|
@@ -304,18 +310,18 @@ marker.markerCircleRed {
|
|
|
304
310
|
So you can simply add a marker with the id `myMarkerIdInSvg` to the SVG, and add the class `myMarkerCssClass` to the
|
|
305
311
|
css. Then you can show it on the field "e4" with
|
|
306
312
|
|
|
307
|
-
`addMarker(
|
|
313
|
+
`addMarker({class: "myMarkerCssClass", slice: "myMarkerIdInSvg"}, "e4")`
|
|
308
314
|
|
|
309
315
|
To allow easy removing of the marker, you have to define the marker type in your code.
|
|
310
316
|
|
|
311
317
|
```js
|
|
312
318
|
const myMarkerType = {class: "myMarkerCssClass", slice: "myMarkerIdInSvg"}
|
|
313
319
|
// add
|
|
314
|
-
chessboard.addMarker("e4"
|
|
320
|
+
chessboard.addMarker(myMarkerType, "e4")
|
|
315
321
|
// remove
|
|
316
|
-
chessboard.removeMarkers("e4"
|
|
322
|
+
chessboard.removeMarkers(myMarkerType, "e4")
|
|
317
323
|
// remove all "myMarkerType"
|
|
318
|
-
chessboard.removeMarkers(
|
|
324
|
+
chessboard.removeMarkers(myMarkerType, undefined)
|
|
319
325
|
```
|
|
320
326
|
|
|
321
327
|
## Extensions
|
|
@@ -367,7 +373,7 @@ const chessboard = new Chessboard(document.getElementById("board"), {
|
|
|
367
373
|
### registerMethod(name, callback)
|
|
368
374
|
|
|
369
375
|
Add methods to the main chessboard from your extension with `this.registerMethod("name", callback)`
|
|
370
|
-
like `addArrow(
|
|
376
|
+
like `addArrow(ftype, rom, to)` in the
|
|
371
377
|
[Arrows extension](https://shaack.com/projekte/cm-chessboard/examples/extensions/chessboard-arrows-extension.html).
|
|
372
378
|
|
|
373
379
|
```js
|
|
@@ -31,10 +31,10 @@ const chessboard = new Chessboard(document.getElementById("board"), {
|
|
|
31
31
|
|
|
32
32
|
}]
|
|
33
33
|
})
|
|
34
|
-
chessboard.addArrow("f3", "d5"
|
|
35
|
-
chessboard.addArrow("b8", "c6"
|
|
36
|
-
chessboard.addArrow("d2", "d3"
|
|
37
|
-
chessboard.addArrow("g5", "e6"
|
|
34
|
+
chessboard.addArrow(ARROW_TYPE.default, "f3", "d5");
|
|
35
|
+
chessboard.addArrow(ARROW_TYPE.default, "b8", "c6");
|
|
36
|
+
chessboard.addArrow(ARROW_TYPE.pointy, "d2", "d3");
|
|
37
|
+
chessboard.addArrow(ARROW_TYPE.danger, "g5", "e6");
|
|
38
38
|
</pre>
|
|
39
39
|
|
|
40
40
|
<script type="module">
|
|
@@ -55,10 +55,10 @@ chessboard.addArrow("g5", "e6", ARROW_TYPE.danger);
|
|
|
55
55
|
|
|
56
56
|
}]
|
|
57
57
|
})
|
|
58
|
-
chessboard.addArrow("f3", "d5"
|
|
59
|
-
chessboard.addArrow("b8", "c6"
|
|
60
|
-
chessboard.addArrow("d2", "d3"
|
|
61
|
-
chessboard.addArrow("g5", "e6"
|
|
58
|
+
chessboard.addArrow(ARROW_TYPE.default, "f3", "d5");
|
|
59
|
+
chessboard.addArrow(ARROW_TYPE.default, "b8", "c6");
|
|
60
|
+
chessboard.addArrow(ARROW_TYPE.pointy, "d2", "d3");
|
|
61
|
+
chessboard.addArrow(ARROW_TYPE.danger, "g5", "e6");
|
|
62
62
|
</script>
|
|
63
63
|
</body>
|
|
64
64
|
</html>
|
|
@@ -22,11 +22,11 @@
|
|
|
22
22
|
} else {
|
|
23
23
|
markerType = MARKER_TYPE.circle
|
|
24
24
|
}
|
|
25
|
-
const markersOnSquare = board1.getMarkers(event.square
|
|
25
|
+
const markersOnSquare = board1.getMarkers(markerType, event.square)
|
|
26
26
|
if (markersOnSquare.length > 0) {
|
|
27
|
-
board1.removeMarkers(event.square
|
|
27
|
+
board1.removeMarkers(markerType, event.square)
|
|
28
28
|
} else {
|
|
29
|
-
board1.addMarker(event.square
|
|
29
|
+
board1.addMarker(markerType, event.square)
|
|
30
30
|
}
|
|
31
31
|
})
|
|
32
32
|
</pre>
|
|
@@ -45,11 +45,11 @@
|
|
|
45
45
|
} else {
|
|
46
46
|
markerType = MARKER_TYPE.circle
|
|
47
47
|
}
|
|
48
|
-
const markersOnSquare = board1.getMarkers(event.square
|
|
48
|
+
const markersOnSquare = board1.getMarkers(markerType, event.square)
|
|
49
49
|
if (markersOnSquare.length > 0) {
|
|
50
|
-
board1.removeMarkers(event.square
|
|
50
|
+
board1.removeMarkers(markerType, event.square)
|
|
51
51
|
} else {
|
|
52
|
-
board1.addMarker(event.square
|
|
52
|
+
board1.addMarker(markerType, event.square)
|
|
53
53
|
}
|
|
54
54
|
})
|
|
55
55
|
|
|
@@ -65,11 +65,11 @@
|
|
|
65
65
|
const marker = {class: "markerCircleRed", slice: "markerCircle"}
|
|
66
66
|
board2.enableSquareSelect((event) => {
|
|
67
67
|
if(event.type === SQUARE_SELECT_TYPE.secondary) {
|
|
68
|
-
const markersOnSquare = board2.getMarkers(event.square
|
|
68
|
+
const markersOnSquare = board2.getMarkers(marker, event.square)
|
|
69
69
|
if (markersOnSquare.length > 0) {
|
|
70
|
-
board2.removeMarkers(event.square
|
|
70
|
+
board2.removeMarkers(marker, event.square)
|
|
71
71
|
} else {
|
|
72
|
-
board2.addMarker(event.square
|
|
72
|
+
board2.addMarker(marker, event.square)
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
})
|
|
@@ -33,13 +33,13 @@ const chess = new Chess()
|
|
|
33
33
|
|
|
34
34
|
function inputHandler(event) {
|
|
35
35
|
console.log("event", event)
|
|
36
|
-
event.chessboard.removeMarkers(
|
|
37
|
-
event.chessboard.removeMarkers(
|
|
36
|
+
event.chessboard.removeMarkers(MARKER_TYPE.dot)
|
|
37
|
+
event.chessboard.removeMarkers(MARKER_TYPE.square)
|
|
38
38
|
if (event.type === INPUT_EVENT_TYPE.moveStart) {
|
|
39
39
|
const moves = chess.moves({square: event.square, verbose: true});
|
|
40
|
-
event.chessboard.addMarker(
|
|
40
|
+
event.chessboard.addMarker(MARKER_TYPE.square, event.square)
|
|
41
41
|
for (const move of moves) { // draw dots on possible moves
|
|
42
|
-
event.chessboard.addMarker(
|
|
42
|
+
event.chessboard.addMarker(MARKER_TYPE.dot, move.to)
|
|
43
43
|
}
|
|
44
44
|
return moves.length > 0
|
|
45
45
|
} else if (event.type === INPUT_EVENT_TYPE.moveDone) {
|
|
@@ -81,11 +81,11 @@ board.enableMoveInput(inputHandler, COLOR.white)
|
|
|
81
81
|
|
|
82
82
|
function inputHandler(event) {
|
|
83
83
|
console.log("event", event)
|
|
84
|
-
event.chessboard.removeMarkers(
|
|
84
|
+
event.chessboard.removeMarkers(MARKER_TYPE.dot)
|
|
85
85
|
if (event.type === INPUT_EVENT_TYPE.moveStart) {
|
|
86
86
|
const moves = chess.moves({square: event.square, verbose: true});
|
|
87
87
|
for (const move of moves) { // draw dots on possible squares
|
|
88
|
-
event.chessboard.addMarker(
|
|
88
|
+
event.chessboard.addMarker(MARKER_TYPE.dot, move.to)
|
|
89
89
|
}
|
|
90
90
|
return moves.length > 0
|
|
91
91
|
} else if (event.type === INPUT_EVENT_TYPE.moveDone) {
|
package/package.json
CHANGED
|
@@ -67,7 +67,7 @@ export class Chessboard {
|
|
|
67
67
|
size: 40, // the sprite tiles size, defaults to 40x40px
|
|
68
68
|
cache: true // cache the sprite
|
|
69
69
|
},
|
|
70
|
-
extensions: [ /* {class: ExtensionClass, props: { ... }} */
|
|
70
|
+
extensions: [ /* {class: ExtensionClass, props: { ... }} */] // add extensions here
|
|
71
71
|
}
|
|
72
72
|
this.props = {}
|
|
73
73
|
Object.assign(this.props, defaultProps)
|
|
@@ -149,15 +149,20 @@ export class Chessboard {
|
|
|
149
149
|
return this.state.orientation
|
|
150
150
|
}
|
|
151
151
|
|
|
152
|
-
addMarker(
|
|
153
|
-
if (
|
|
154
|
-
console.error("
|
|
152
|
+
addMarker(type, square) {
|
|
153
|
+
if (typeof type === "string" || typeof square === "object") { // todo remove 2022-12-01
|
|
154
|
+
console.error("changed the signature of `addMarker` to `(type, square)` with v5.1.x")
|
|
155
|
+
return
|
|
155
156
|
}
|
|
156
157
|
this.state.addMarker(square, type)
|
|
157
158
|
this.view.drawMarkers()
|
|
158
159
|
}
|
|
159
160
|
|
|
160
|
-
getMarkers(
|
|
161
|
+
getMarkers(type = undefined, square = undefined) {
|
|
162
|
+
if (typeof type === "string" || typeof square === "object") { // todo remove 2022-12-01
|
|
163
|
+
console.error("changed the signature of `getMarkers` to `(type, square)` with v5.1.x")
|
|
164
|
+
return
|
|
165
|
+
}
|
|
161
166
|
const markersFound = []
|
|
162
167
|
this.state.markers.forEach((marker) => {
|
|
163
168
|
const markerSquare = marker.square
|
|
@@ -170,7 +175,11 @@ export class Chessboard {
|
|
|
170
175
|
return markersFound
|
|
171
176
|
}
|
|
172
177
|
|
|
173
|
-
removeMarkers(
|
|
178
|
+
removeMarkers(type = undefined, square = undefined) {
|
|
179
|
+
if (typeof type === "string" || typeof square === "object") { // todo remove 2022-12-01
|
|
180
|
+
console.error("changed the signature of `removeMarkers` to `(type, square)` with v5.1.x")
|
|
181
|
+
return
|
|
182
|
+
}
|
|
174
183
|
this.state.removeMarkers(square, type)
|
|
175
184
|
this.view.drawMarkers()
|
|
176
185
|
}
|
|
@@ -97,12 +97,20 @@ export class Arrows extends Extension {
|
|
|
97
97
|
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
-
addArrow(from, to
|
|
100
|
+
addArrow(type, from, to) {
|
|
101
|
+
if (typeof type === "string" || typeof from === "object" || typeof to === "object") { // todo remove 2022-12-01
|
|
102
|
+
console.error("changed the signature of `addArrow` to `(type, from, to)` with v5.1.x")
|
|
103
|
+
return
|
|
104
|
+
}
|
|
101
105
|
this.arrows.push(new Arrow(from, to, type))
|
|
102
106
|
this.chessboard.view.redrawBoard()
|
|
103
107
|
}
|
|
104
108
|
|
|
105
|
-
getArrows(
|
|
109
|
+
getArrows(type = undefined, from = undefined, to = undefined) {
|
|
110
|
+
if (typeof type === "string" || typeof from === "object" || typeof to === "object") { // todo remove 2022-12-01
|
|
111
|
+
console.error("changed the signature of `getArrows` to `(type, from, to)` with v5.1.x")
|
|
112
|
+
return
|
|
113
|
+
}
|
|
106
114
|
let arrows = []
|
|
107
115
|
this.arrows.forEach((arrow) => {
|
|
108
116
|
if (arrow.matches(from, to, type)) {
|
|
@@ -112,7 +120,11 @@ export class Arrows extends Extension {
|
|
|
112
120
|
return arrows
|
|
113
121
|
}
|
|
114
122
|
|
|
115
|
-
removeArrows(
|
|
123
|
+
removeArrows(type = undefined, from = undefined, to = undefined) {
|
|
124
|
+
if (typeof type === "string" || typeof from === "object" || typeof to === "object") { // todo remove 2022-12-01
|
|
125
|
+
console.error("changed the signature of `removeArrows` to `(type, from, to)` with v5.1.x")
|
|
126
|
+
return
|
|
127
|
+
}
|
|
116
128
|
this.arrows = this.arrows.filter((arrow) => !arrow.matches(from, to, type))
|
|
117
129
|
this.chessboard.view.redrawBoard()
|
|
118
130
|
}
|
|
@@ -132,6 +144,6 @@ class Arrow {
|
|
|
132
144
|
if (to && to !== this.to) {
|
|
133
145
|
return false
|
|
134
146
|
}
|
|
135
|
-
return !(type && type !== this.type)
|
|
147
|
+
return !(type && type !== this.type)
|
|
136
148
|
}
|
|
137
149
|
}
|
package/test/TestMarkers.js
CHANGED
|
@@ -14,27 +14,27 @@ describe("TestMarkers", () => {
|
|
|
14
14
|
sprite: {url: "../assets/images/chessboard-sprite.svg"},
|
|
15
15
|
position: "empty"
|
|
16
16
|
})
|
|
17
|
-
chessboard.addMarker("e5"
|
|
18
|
-
chessboard.addMarker("b6"
|
|
19
|
-
chessboard.addMarker("h6"
|
|
20
|
-
assert.equal(chessboard.getMarkers().length,3)
|
|
21
|
-
const markersE5 = chessboard.getMarkers("e5")
|
|
17
|
+
chessboard.addMarker(MARKER_TYPE.square, "e5")
|
|
18
|
+
chessboard.addMarker(MARKER_TYPE.frame, "b6")
|
|
19
|
+
chessboard.addMarker(MARKER_TYPE.frame, "h6")
|
|
20
|
+
assert.equal(chessboard.getMarkers().length, 3)
|
|
21
|
+
const markersE5 = chessboard.getMarkers(undefined,"e5")
|
|
22
22
|
assert.equal(markersE5.length, 1)
|
|
23
23
|
assert.equal(markersE5[0].square, "e5")
|
|
24
24
|
assert.equal(markersE5[0].type.slice, "markerSquare")
|
|
25
|
-
assert.equal(chessboard.getMarkers(
|
|
26
|
-
assert.equal(chessboard.getMarkers("a4").length, 0)
|
|
27
|
-
assert.equal(chessboard.getMarkers(
|
|
28
|
-
assert.equal(chessboard.getMarkers("b6"
|
|
29
|
-
assert.equal(chessboard.getMarkers("h6"
|
|
30
|
-
chessboard.removeMarkers("h6")
|
|
31
|
-
assert.equal(chessboard.getMarkers("h6"
|
|
32
|
-
chessboard.addMarker("h6"
|
|
33
|
-
assert.equal(chessboard.getMarkers("h6"
|
|
34
|
-
chessboard.removeMarkers("h6"
|
|
35
|
-
assert.equal(chessboard.getMarkers("h6"
|
|
36
|
-
chessboard.removeMarkers("h6"
|
|
37
|
-
assert.equal(chessboard.getMarkers("h6"
|
|
25
|
+
assert.equal(chessboard.getMarkers(MARKER_TYPE.square).length, 1)
|
|
26
|
+
assert.equal(chessboard.getMarkers(undefined, "a4").length, 0)
|
|
27
|
+
assert.equal(chessboard.getMarkers(MARKER_TYPE.frame).length, 2)
|
|
28
|
+
assert.equal(chessboard.getMarkers(MARKER_TYPE.frame, "b6").length, 1)
|
|
29
|
+
assert.equal(chessboard.getMarkers(MARKER_TYPE.frame, "h6").length, 1)
|
|
30
|
+
chessboard.removeMarkers(undefined, "h6")
|
|
31
|
+
assert.equal(chessboard.getMarkers(MARKER_TYPE.frame, "h6").length, 0)
|
|
32
|
+
chessboard.addMarker(MARKER_TYPE.frame, "h6")
|
|
33
|
+
assert.equal(chessboard.getMarkers(MARKER_TYPE.frame, "h6").length, 1)
|
|
34
|
+
chessboard.removeMarkers(MARKER_TYPE.square, "h6")
|
|
35
|
+
assert.equal(chessboard.getMarkers(MARKER_TYPE.frame, "h6").length, 1)
|
|
36
|
+
chessboard.removeMarkers(MARKER_TYPE.frame, "h6")
|
|
37
|
+
assert.equal(chessboard.getMarkers(MARKER_TYPE.frame, "h6").length, 0)
|
|
38
38
|
chessboard.destroy()
|
|
39
39
|
})
|
|
40
40
|
|