cm-chessboard 6.0.2 → 6.0.3
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
|
@@ -8,7 +8,7 @@ in [chess-console](https://shaack.com/projekte/chess-console/examples/load-pgn.h
|
|
|
8
8
|
[cm-fen-editor](https://shaack.com/projekte/cm-fen-editor/). They are all nice written ES6 Modules to handle different
|
|
9
9
|
aspects of chess games.
|
|
10
10
|
|
|
11
|
-
> Note: With Version 6 **markers** have been
|
|
11
|
+
> Note: With Version 6 **markers** have been moved to an extension, see the [Input callbacks example](https://shaack.com/projekte/cm-chessboard/examples/input-callbacks.html).
|
|
12
12
|
|
|
13
13
|
## Features
|
|
14
14
|
|
|
@@ -24,7 +24,7 @@ aspects of chess games.
|
|
|
24
24
|
- [Arrows Extension](https://shaack.com/projekte/cm-chessboard/examples/extensions/chessboard-arrows-extension.html)
|
|
25
25
|
- [Markers Extension](https://shaack.com/projekte/cm-chessboard/examples/extensions/chessboard-markers-extension.html) 🆕
|
|
26
26
|
- [PromotionDialog Extension](https://shaack.com/projekte/cm-chessboard/examples/extensions/chessboard-promotion-dialog-extension.html)
|
|
27
|
-
- [RenderVideo](https://shaack.com/projekte/cm-chessboard/examples/extensions/chessboard-render-video-extension.html) 🆕
|
|
27
|
+
- [RenderVideo Extension](https://shaack.com/projekte/cm-chessboard/examples/extensions/chessboard-render-video-extension.html) 🆕
|
|
28
28
|
|
|
29
29
|
## Demo and repository
|
|
30
30
|
|
|
@@ -137,7 +137,7 @@ Returns the board position as `fen`.
|
|
|
137
137
|
|
|
138
138
|
### addMarker(type, square)
|
|
139
139
|
|
|
140
|
-
>
|
|
140
|
+
> Moved to the Markers extension with version 6
|
|
141
141
|
|
|
142
142
|
Adds a marker on a square.
|
|
143
143
|
|
|
@@ -156,7 +156,7 @@ below.
|
|
|
156
156
|
|
|
157
157
|
### getMarkers(type = undefined, square = undefined)
|
|
158
158
|
|
|
159
|
-
>
|
|
159
|
+
> Moved to the Markers extension with version 6
|
|
160
160
|
|
|
161
161
|
Returns the board's markers as an array.
|
|
162
162
|
|
|
@@ -166,7 +166,7 @@ Set `both` to `undefined` to get all markers on the board.
|
|
|
166
166
|
|
|
167
167
|
### removeMarkers(type = undefined, square = undefined)
|
|
168
168
|
|
|
169
|
-
>
|
|
169
|
+
> Moved to the Markers extension with version 6
|
|
170
170
|
|
|
171
171
|
Removes markers from the board.
|
|
172
172
|
|
package/package.json
CHANGED
|
@@ -1,132 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Authors and copyright: Laura Campo Alberca (@lauracampoalberca) and Stefan Haack (@shaack)
|
|
3
|
+
* Repository: https://github.com/shaack/cm-chessboard
|
|
4
|
+
* License: MIT, see file 'LICENSE'
|
|
5
|
+
*/
|
|
1
6
|
|
|
2
7
|
import {Extension, EXTENSION_POINT} from "../../model/Extension.js"
|
|
3
8
|
import {Svg} from "../../view/ChessboardView.js"
|
|
4
9
|
|
|
5
10
|
export const MARKER_TYPE = {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
11
|
+
square: {class: "marker-square", slice: "markerSquare"},
|
|
12
|
+
frame: {class: "marker-frame", slice: "markerFrame"},
|
|
13
|
+
dot: {class: "marker-dot", slice: "markerDot", position: 'above'},
|
|
14
|
+
circle: {class: "marker-circle", slice: "markerCircle"}
|
|
10
15
|
}
|
|
11
16
|
|
|
12
17
|
export class Markers extends Extension {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
18
|
+
constructor(chessboard, props) {
|
|
19
|
+
super(chessboard, props)
|
|
20
|
+
this.registerExtensionPoint(EXTENSION_POINT.redrawBoard, () => {
|
|
21
|
+
this.onRedrawBoard()
|
|
22
|
+
})
|
|
23
|
+
let defaultProps = {
|
|
24
|
+
style: {
|
|
25
|
+
moveFromMarker: MARKER_TYPE.frame, // the marker used to mark the start square
|
|
26
|
+
moveToMarker: MARKER_TYPE.frame, // the marker used to mark the square where the figure is moving to
|
|
27
|
+
},
|
|
28
|
+
sprite: chessboard.props.sprite
|
|
29
|
+
}
|
|
30
|
+
this.props = {}
|
|
31
|
+
Object.assign(this.props, defaultProps)
|
|
32
|
+
Object.assign(this.props, props)
|
|
33
|
+
this.props.sprite = defaultProps.sprite
|
|
34
|
+
if (props && props.sprite) {
|
|
35
|
+
Object.assign(this.props.sprite, props.sprite)
|
|
36
|
+
}
|
|
37
|
+
if (this.props.sprite.cache) {
|
|
38
|
+
this.chessboard.view.cacheSpriteToDiv("chessboardMarkerSpriteCache", this.props.sprite.url)
|
|
39
|
+
}
|
|
40
|
+
this.registerMethod("addMarker", this.addMarker)
|
|
41
|
+
this.registerMethod("getMarkers", this.getMarkers)
|
|
42
|
+
this.registerMethod("removeMarkers", this.removeMarkers)
|
|
43
|
+
this.markerGroupDown = Svg.addElement(chessboard.view.markersLayer, "g", {class: "markers"})
|
|
44
|
+
this.markerGroupUp = Svg.addElement(chessboard.view.markersTopLayer, "g", {class: "markers"})
|
|
45
|
+
this.markers = []
|
|
46
|
+
}
|
|
42
47
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
48
|
+
onRedrawBoard() {
|
|
49
|
+
while (this.markerGroupUp.firstChild) {
|
|
50
|
+
this.markerGroupUp.removeChild(this.markerGroupUp.firstChild)
|
|
51
|
+
}
|
|
52
|
+
while (this.markerGroupDown.firstChild) {
|
|
53
|
+
this.markerGroupDown.removeChild(this.markerGroupDown.firstChild)
|
|
54
|
+
}
|
|
55
|
+
this.markers.forEach((marker) => {
|
|
56
|
+
this.drawMarker(marker)
|
|
57
|
+
}
|
|
58
|
+
)
|
|
59
|
+
}
|
|
55
60
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
61
|
+
drawMarker(marker) {
|
|
62
|
+
let markerGroup
|
|
63
|
+
if (marker.type.position === 'above') {
|
|
64
|
+
markerGroup = Svg.addElement(this.markerGroupUp, "g")
|
|
65
|
+
} else {
|
|
66
|
+
markerGroup = Svg.addElement(this.markerGroupDown, "g")
|
|
67
|
+
}
|
|
68
|
+
markerGroup.setAttribute("data-square", marker.square)
|
|
69
|
+
const point = this.chessboard.view.squareToPoint(marker.square)
|
|
70
|
+
const transform = (this.chessboard.view.svg.createSVGTransform())
|
|
71
|
+
transform.setTranslate(point.x, point.y)
|
|
72
|
+
markerGroup.transform.baseVal.appendItem(transform)
|
|
73
|
+
const spriteUrl = this.chessboard.props.sprite.cache ? "" : this.chessboard.props.sprite.url
|
|
74
|
+
const markerUse = Svg.addElement(markerGroup, "use",
|
|
75
|
+
{href: `${spriteUrl}#${marker.type.slice}`, class: "marker " + marker.type.class})
|
|
76
|
+
const transformScale = (this.chessboard.view.svg.createSVGTransform())
|
|
77
|
+
transformScale.setScale(this.chessboard.view.scalingX, this.chessboard.view.scalingY)
|
|
78
|
+
markerUse.transform.baseVal.appendItem(transformScale)
|
|
79
|
+
return markerGroup
|
|
80
|
+
}
|
|
76
81
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
82
|
+
addMarker(type, square) {
|
|
83
|
+
if (typeof type === "string" || typeof square === "object") { // todo remove 2022-12-01
|
|
84
|
+
console.error("changed the signature of `addMarker` to `(type, square)` with v5.1.x")
|
|
85
|
+
return
|
|
86
|
+
}
|
|
87
|
+
this.markers.push(new Marker(square, type))
|
|
88
|
+
this.onRedrawBoard()
|
|
89
|
+
}
|
|
85
90
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
91
|
+
getMarkers(type = undefined, square = undefined) {
|
|
92
|
+
if (typeof type === "string" || typeof square === "object") { // todo remove 2022-12-01
|
|
93
|
+
console.error("changed the signature of `getMarkers` to `(type, square)` with v5.1.x")
|
|
94
|
+
return
|
|
95
|
+
}
|
|
96
|
+
let markersFound = []
|
|
97
|
+
this.markers.forEach((marker) => {
|
|
98
|
+
if (marker.matches(square, type)) {
|
|
99
|
+
markersFound.push(marker)
|
|
100
|
+
}
|
|
101
|
+
})
|
|
102
|
+
return markersFound
|
|
103
|
+
}
|
|
99
104
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
105
|
+
removeMarkers(type = undefined, square = undefined) {
|
|
106
|
+
if (typeof type === "string" || typeof square === "object") { // todo remove 2022-12-01
|
|
107
|
+
console.error("changed the signature of `removeMarkers` to `(type, square)` with v5.1.x")
|
|
108
|
+
return
|
|
109
|
+
}
|
|
110
|
+
this.markers = this.markers.filter((marker) => !marker.matches(square, type))
|
|
111
|
+
this.onRedrawBoard()
|
|
112
|
+
}
|
|
108
113
|
}
|
|
109
114
|
|
|
110
115
|
class Marker {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
116
|
+
constructor(square, type) {
|
|
117
|
+
this.square = square
|
|
118
|
+
this.type = type
|
|
119
|
+
}
|
|
115
120
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
121
|
+
matches(square = undefined, type = undefined) {
|
|
122
|
+
if (!type && !square) {
|
|
123
|
+
return true
|
|
124
|
+
} else if (!type) {
|
|
125
|
+
if (square === this.square) {
|
|
126
|
+
return true
|
|
127
|
+
}
|
|
128
|
+
} else if (!square) {
|
|
129
|
+
if (this.type === type) {
|
|
130
|
+
return true
|
|
131
|
+
}
|
|
132
|
+
} else if (this.type === type && square === this.square) {
|
|
133
|
+
return true
|
|
134
|
+
}
|
|
135
|
+
return false
|
|
136
|
+
}
|
|
132
137
|
}
|
|
@@ -168,6 +168,9 @@ export class PositionsAnimation {
|
|
|
168
168
|
|
|
169
169
|
animationStep(time) {
|
|
170
170
|
// console.log("animationStep", time)
|
|
171
|
+
if(!this.view || !this.view.chessboard.state) { // board was destroyed
|
|
172
|
+
return
|
|
173
|
+
}
|
|
171
174
|
if (!this.startTime) {
|
|
172
175
|
this.startTime = time
|
|
173
176
|
this.view.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.animation, {
|
package/test/TestMarkers.js
CHANGED
|
@@ -5,13 +5,15 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import {describe, it, assert} from "../node_modules/teevi/src/teevi.js"
|
|
8
|
-
import {Chessboard
|
|
8
|
+
import {Chessboard} from "../src/cm-chessboard/Chessboard.js"
|
|
9
|
+
import {MARKER_TYPE, Markers} from "../src/cm-chessboard/extensions/markers/Markers.js"
|
|
9
10
|
|
|
10
11
|
describe("TestMarkers", () => {
|
|
11
12
|
|
|
12
13
|
it("should set and get markers", () => {
|
|
13
14
|
const chessboard = new Chessboard(document.getElementById("TestMarkers"), {
|
|
14
|
-
sprite: {url: "../assets/images/chessboard-sprite.svg"}
|
|
15
|
+
sprite: {url: "../assets/images/chessboard-sprite.svg"},
|
|
16
|
+
extensions: [{class: Markers}]
|
|
15
17
|
})
|
|
16
18
|
chessboard.addMarker(MARKER_TYPE.square, "e5")
|
|
17
19
|
chessboard.addMarker(MARKER_TYPE.frame, "b6")
|