cm-chessboard 6.0.3 → 7.0.1
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 -27
- package/assets/{styles/cm-chessboard.css → chessboard/chessboard.css} +2 -30
- package/assets/chessboard/chessboard.css.map +10 -0
- package/assets/{styles/cm-chessboard.scss → chessboard/chessboard.scss} +2 -43
- package/assets/chessboard/pieces/standard.svg +192 -0
- package/assets/chessboard/pieces/staunty.svg +240 -0
- package/assets/extensions/arrows/arrows.svg +19 -0
- package/assets/extensions/markers/markers.css +26 -0
- package/assets/extensions/markers/markers.css.map +9 -0
- package/assets/extensions/markers/markers.scss +40 -0
- package/assets/extensions/markers/markers.svg +27 -0
- package/{src/cm-chessboard/extensions/promotion-dialog/assets → assets/extensions/promotion-dialog}/promotion-dialog.css +1 -1
- package/{src/cm-chessboard/extensions/promotion-dialog/assets → assets/extensions/promotion-dialog}/promotion-dialog.css.map +1 -1
- package/{src/cm-chessboard/extensions/promotion-dialog/assets → assets/extensions/promotion-dialog}/promotion-dialog.scss +1 -1
- package/examples/destroy-many-boards.html +5 -3
- package/examples/different-styles.html +34 -23
- package/examples/enable-input.html +14 -7
- package/examples/extensions/{chessboard-accessibility-extension.html → accessibility-extension.html} +6 -4
- package/examples/extensions/{chessboard-arrows-extension.html → arrows-extension.html} +12 -30
- package/examples/{input-callbacks.html → extensions/markers-extension.html} +17 -25
- package/examples/extensions/{chessboard-promotion-dialog-extension.html → promotion-dialog-extension.html} +8 -7
- package/examples/extensions/{chessboard-render-video-extension.html → render-video-extension.html} +5 -9
- package/examples/many-boards.html +5 -5
- package/examples/pieces-animation.html +3 -3
- package/examples/responsive-board.html +3 -5
- package/examples/simple-boards.html +8 -4
- package/examples/test-getPiece.html +3 -3
- package/examples/validate-moves.html +7 -7
- package/index.html +7 -7
- package/package.json +2 -2
- package/src/cm-chessboard/Chessboard.js +19 -25
- package/src/cm-chessboard/extensions/accessibility/Accessibility.js +1 -1
- package/src/cm-chessboard/extensions/arrows/Arrows.js +9 -18
- package/src/cm-chessboard/extensions/markers/Markers.js +28 -17
- package/src/cm-chessboard/extensions/promotion-dialog/PromotionDialog.js +4 -6
- package/src/cm-chessboard/extensions/render-video/RenderVideo.js +5 -2
- package/src/cm-chessboard/lib/I18n.js +44 -0
- package/src/cm-chessboard/lib/Svg.js +69 -0
- package/src/cm-chessboard/lib/Utils.js +41 -0
- package/src/cm-chessboard/model/Extension.js +2 -2
- package/src/cm-chessboard/view/ChessboardView.js +44 -172
- package/src/cm-chessboard/view/PositionAnimationsQueue.js +3 -3
- package/src/cm-chessboard/view/VisualMoveInput.js +13 -7
- package/test/TestBoard.js +2 -2
- package/test/TestChessboard.js +4 -4
- package/test/TestMarkers.js +1 -1
- package/test/index.html +1 -1
- package/assets/images/chessboard-sprite-staunty.svg +0 -145
- package/assets/images/chessboard-sprite.svg +0 -144
- package/assets/styles/cm-chessboard.css.map +0 -10
- package/examples/extensions/chessboard-markers-extension.html +0 -52
- package/src/cm-chessboard/extensions/arrows/assets/arrows.svg +0 -31
- /package/assets/{styles/_cm-chessboard-theme.scss → chessboard/_chessboard-theme.scss} +0 -0
- /package/{src/cm-chessboard/extensions/arrows/assets → assets/extensions/arrows}/arrows.css +0 -0
- /package/{src/cm-chessboard/extensions/arrows/assets → assets/extensions/arrows}/arrows.css.map +0 -0
- /package/{src/cm-chessboard/extensions/arrows/assets → assets/extensions/arrows}/arrows.scss +0 -0
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import {Extension, EXTENSION_POINT} from "../../model/Extension.js"
|
|
8
|
-
import {Svg} from "../../
|
|
8
|
+
import {Svg} from "../../lib/Svg.js"
|
|
9
9
|
|
|
10
10
|
export const ARROW_TYPE = {
|
|
11
11
|
default: {class: "arrow-default", slice: "arrowDefault", headSize: 7},
|
|
@@ -14,27 +14,18 @@ export const ARROW_TYPE = {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
export class Arrows extends Extension {
|
|
17
|
-
constructor(chessboard, props) {
|
|
17
|
+
constructor(chessboard, props = {}) {
|
|
18
18
|
super(chessboard, props)
|
|
19
19
|
this.registerExtensionPoint(EXTENSION_POINT.redrawBoard, () => {
|
|
20
20
|
this.onRedrawBoard()
|
|
21
21
|
})
|
|
22
|
-
|
|
23
|
-
sprite:
|
|
24
|
-
url: "./assets/images/chessboard-arrows.svg",
|
|
25
|
-
size: 40,
|
|
26
|
-
cache: true
|
|
27
|
-
},
|
|
22
|
+
this.props = {
|
|
23
|
+
sprite: "arrows.svg"
|
|
28
24
|
}
|
|
29
|
-
this.props = {}
|
|
30
|
-
Object.assign(this.props, defaultProps)
|
|
31
25
|
Object.assign(this.props, props)
|
|
32
|
-
this.props.
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
if (this.props.sprite.cache) {
|
|
37
|
-
this.chessboard.view.cacheSpriteToDiv("chessboardArrowSpriteCache", this.props.sprite.url)
|
|
26
|
+
if (this.chessboard.props.assetsCache) {
|
|
27
|
+
this.chessboard.view.cacheSpriteToDiv("cm-chessboard-arrows",
|
|
28
|
+
this.chessboard.props.assetsUrl + "extensions/arrows/" + this.props.sprite)
|
|
38
29
|
}
|
|
39
30
|
this.registerMethod("addArrow", this.addArrow)
|
|
40
31
|
this.registerMethod("getArrows", this.getArrows)
|
|
@@ -56,11 +47,11 @@ export class Arrows extends Extension {
|
|
|
56
47
|
const arrowsGroup = Svg.addElement(this.arrowGroup, "g")
|
|
57
48
|
arrowsGroup.setAttribute("data-arrow", arrow.from + arrow.to)
|
|
58
49
|
arrowsGroup.setAttribute("class", "arrow " + arrow.type.class)
|
|
59
|
-
|
|
60
50
|
const view = this.chessboard.view
|
|
61
51
|
const sqfrom = document.querySelectorAll('[data-square="' + arrow.from + '"]')[0]
|
|
62
52
|
const sqto = document.querySelectorAll('[data-square="' + arrow.to + '"]')[0]
|
|
63
|
-
const spriteUrl = this.chessboard.props.
|
|
53
|
+
const spriteUrl = this.chessboard.props.assetsCache ? "" : this.chessboard.props.assetsUrl +
|
|
54
|
+
"extensions/arrows/" + this.props.sprite
|
|
64
55
|
const defs = Svg.addElement(arrowsGroup, "defs")
|
|
65
56
|
const id = "arrow-" + arrow.from + arrow.to
|
|
66
57
|
const marker = Svg.addElement(defs, "marker", {
|
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import {Extension, EXTENSION_POINT} from "../../model/Extension.js"
|
|
8
|
-
import {Svg} from "../../
|
|
8
|
+
import {Svg} from "../../lib/Svg.js"
|
|
9
|
+
import {INPUT_EVENT_TYPE} from "../../Chessboard.js"
|
|
9
10
|
|
|
10
11
|
export const MARKER_TYPE = {
|
|
11
12
|
square: {class: "marker-square", slice: "markerSquare"},
|
|
@@ -15,27 +16,20 @@ export const MARKER_TYPE = {
|
|
|
15
16
|
}
|
|
16
17
|
|
|
17
18
|
export class Markers extends Extension {
|
|
18
|
-
constructor(chessboard, props) {
|
|
19
|
+
constructor(chessboard, props = {}) {
|
|
19
20
|
super(chessboard, props)
|
|
20
21
|
this.registerExtensionPoint(EXTENSION_POINT.redrawBoard, () => {
|
|
21
22
|
this.onRedrawBoard()
|
|
22
23
|
})
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
moveToMarker: MARKER_TYPE.frame, // the marker used to mark the square where the figure is moving to
|
|
27
|
-
},
|
|
28
|
-
sprite: chessboard.props.sprite
|
|
24
|
+
this.props = {
|
|
25
|
+
autoMarkers: MARKER_TYPE.frame, // set to `null` to disable autoMarkers
|
|
26
|
+
sprite: "markers.svg" // the sprite file of the markers
|
|
29
27
|
}
|
|
30
|
-
this.props = {}
|
|
31
|
-
Object.assign(this.props, defaultProps)
|
|
32
28
|
Object.assign(this.props, props)
|
|
33
|
-
this.
|
|
34
|
-
if (props
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (this.props.sprite.cache) {
|
|
38
|
-
this.chessboard.view.cacheSpriteToDiv("chessboardMarkerSpriteCache", this.props.sprite.url)
|
|
29
|
+
this.autoMarker = {}
|
|
30
|
+
if (chessboard.props.assetsCache) {
|
|
31
|
+
chessboard.view.cacheSpriteToDiv("cm-chessboard-markers", this.chessboard.props.assetsUrl +
|
|
32
|
+
"extensions/markers/" + this.props.sprite)
|
|
39
33
|
}
|
|
40
34
|
this.registerMethod("addMarker", this.addMarker)
|
|
41
35
|
this.registerMethod("getMarkers", this.getMarkers)
|
|
@@ -43,6 +37,22 @@ export class Markers extends Extension {
|
|
|
43
37
|
this.markerGroupDown = Svg.addElement(chessboard.view.markersLayer, "g", {class: "markers"})
|
|
44
38
|
this.markerGroupUp = Svg.addElement(chessboard.view.markersTopLayer, "g", {class: "markers"})
|
|
45
39
|
this.markers = []
|
|
40
|
+
if (this.props.autoMarkers) {
|
|
41
|
+
Object.assign(this.autoMarker, this.props.autoMarkers)
|
|
42
|
+
this.registerExtensionPoint(EXTENSION_POINT.moveInput, (event) => {
|
|
43
|
+
this.removeMarkers(this.autoMarker)
|
|
44
|
+
if (event.type === INPUT_EVENT_TYPE.moveInputStarted ||
|
|
45
|
+
event.type === INPUT_EVENT_TYPE.movingOverSquare ||
|
|
46
|
+
event.type === INPUT_EVENT_TYPE.validateMoveInput) {
|
|
47
|
+
if(event.squareFrom) {
|
|
48
|
+
this.addMarker(this.autoMarker, event.squareFrom)
|
|
49
|
+
}
|
|
50
|
+
if(event.squareTo) {
|
|
51
|
+
this.addMarker(this.autoMarker, event.squareTo)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
})
|
|
55
|
+
}
|
|
46
56
|
}
|
|
47
57
|
|
|
48
58
|
onRedrawBoard() {
|
|
@@ -70,7 +80,8 @@ export class Markers extends Extension {
|
|
|
70
80
|
const transform = (this.chessboard.view.svg.createSVGTransform())
|
|
71
81
|
transform.setTranslate(point.x, point.y)
|
|
72
82
|
markerGroup.transform.baseVal.appendItem(transform)
|
|
73
|
-
const spriteUrl = this.chessboard.props.
|
|
83
|
+
const spriteUrl = this.chessboard.props.assetsCache ? "" : this.chessboard.props.assetsUrl +
|
|
84
|
+
"extensions/markers/" + this.props.sprite
|
|
74
85
|
const markerUse = Svg.addElement(markerGroup, "use",
|
|
75
86
|
{href: `${spriteUrl}#${marker.type.slice}`, class: "marker " + marker.type.class})
|
|
76
87
|
const transformScale = (this.chessboard.view.svg.createSVGTransform())
|
|
@@ -5,17 +5,17 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import {Extension, EXTENSION_POINT} from "../../model/Extension.js"
|
|
7
7
|
import {COLOR, PIECE} from "../../Chessboard.js"
|
|
8
|
-
import {
|
|
8
|
+
import {Svg} from "../../lib/Svg.js"
|
|
9
|
+
import {Utils} from "../../lib/Utils.js"
|
|
9
10
|
|
|
10
11
|
export class PromotionDialog extends Extension {
|
|
11
12
|
|
|
12
|
-
constructor(chessboard, props) {
|
|
13
|
+
constructor(chessboard, props = {}) {
|
|
13
14
|
super(chessboard, props)
|
|
14
|
-
|
|
15
15
|
this.registerExtensionPoint(EXTENSION_POINT.redrawBoard, this.redrawDialog.bind(this))
|
|
16
16
|
this.registerMethod("showPromotionDialog", this.showPromotionDialog)
|
|
17
17
|
this.promotionDialogGroup = Svg.addElement(chessboard.view.markersTopLayer, "g", {class: "promotion-dialog-group"})
|
|
18
|
-
|
|
18
|
+
Utils.delegate(this.promotionDialogGroup, "click", ".promotion-dialog-button",
|
|
19
19
|
this.promotionDialogOnClickPiece.bind(this))
|
|
20
20
|
this.state = {
|
|
21
21
|
isShown: false,
|
|
@@ -23,7 +23,6 @@ export class PromotionDialog extends Extension {
|
|
|
23
23
|
square: undefined,
|
|
24
24
|
callback: undefined
|
|
25
25
|
}
|
|
26
|
-
|
|
27
26
|
}
|
|
28
27
|
|
|
29
28
|
drawPieceButton(piece, point) {
|
|
@@ -121,7 +120,6 @@ export class PromotionDialog extends Extension {
|
|
|
121
120
|
this.state.color = color
|
|
122
121
|
this.state.callback = callback
|
|
123
122
|
const mousedownListener = (event) => {
|
|
124
|
-
// console.log("98c265", event)
|
|
125
123
|
if (!event.target.dataset.piece && this.state.isShown) {
|
|
126
124
|
this.state.isShown = false
|
|
127
125
|
this.state.callback({square: this.state.square, piece: null})
|
|
@@ -146,10 +146,13 @@ export class RenderVideo extends Extension {
|
|
|
146
146
|
document.body.appendChild(wrapper)
|
|
147
147
|
const svg = this.chessboard.view.svg
|
|
148
148
|
const xhr = new XMLHttpRequest()
|
|
149
|
-
xhr.open("GET", this.chessboard.
|
|
149
|
+
xhr.open("GET", this.chessboard.view.getSpriteUrl(), true)
|
|
150
150
|
xhr.onload = function () {
|
|
151
151
|
const doc = new DOMParser().parseFromString(xhr.response, "text/xml")
|
|
152
|
-
|
|
152
|
+
const pieces = doc.querySelectorAll("svg>g")
|
|
153
|
+
for (const piece of pieces) {
|
|
154
|
+
svg.prepend(piece)
|
|
155
|
+
}
|
|
153
156
|
}
|
|
154
157
|
xhr.send()
|
|
155
158
|
}
|
|
@@ -0,0 +1,44 @@
|
|
|
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
|
+
export const piecesTranslations = {
|
|
8
|
+
en: {
|
|
9
|
+
colors: {
|
|
10
|
+
w: "w", b: "b"
|
|
11
|
+
},
|
|
12
|
+
colors_long: {
|
|
13
|
+
w: "White", b: "Black"
|
|
14
|
+
},
|
|
15
|
+
pieces: {
|
|
16
|
+
p: "p", n: "n", b: "b", r: "r", q: "q", k: "k"
|
|
17
|
+
},
|
|
18
|
+
pieces_long: {
|
|
19
|
+
p: "Pawn", n: "Knight", b: "Bishop", r: "Rook", q: "Queen", k: "King"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
de: {
|
|
23
|
+
colors: {
|
|
24
|
+
w: "w", b: "s"
|
|
25
|
+
},
|
|
26
|
+
colors_long: {
|
|
27
|
+
w: "Weiß", b: "Schwarz"
|
|
28
|
+
},
|
|
29
|
+
pieces: {
|
|
30
|
+
p: "b", n: "s", b: "l", r: "t", q: "d", k: "k"
|
|
31
|
+
},
|
|
32
|
+
pieces_long: {
|
|
33
|
+
p: "Bauer", n: "Springer", b: "Läufer", r: "Turm", q: "Dame", k: "König"
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function renderPieceTitle(lang, name, color = undefined) {
|
|
39
|
+
let title = piecesTranslations[lang].pieces_long[name]
|
|
40
|
+
if (color) {
|
|
41
|
+
title += " " + piecesTranslations[lang].colors_long[color]
|
|
42
|
+
}
|
|
43
|
+
return title
|
|
44
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
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
|
+
const SVG_NAMESPACE = "http://www.w3.org/2000/svg"
|
|
8
|
+
|
|
9
|
+
export class Svg {
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* create the Svg in the HTML DOM
|
|
13
|
+
* @param containerElement
|
|
14
|
+
* @returns {Element}
|
|
15
|
+
*/
|
|
16
|
+
static createSvg(containerElement = undefined) {
|
|
17
|
+
let svg = document.createElementNS(SVG_NAMESPACE, "svg")
|
|
18
|
+
if (containerElement) {
|
|
19
|
+
svg.setAttribute("width", "100%")
|
|
20
|
+
svg.setAttribute("height", "100%")
|
|
21
|
+
containerElement.appendChild(svg)
|
|
22
|
+
}
|
|
23
|
+
return svg
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Add an Element to a SVG DOM
|
|
28
|
+
* @param parent
|
|
29
|
+
* @param name
|
|
30
|
+
* @param attributes
|
|
31
|
+
* @param sibling
|
|
32
|
+
* @returns {Element}
|
|
33
|
+
*/
|
|
34
|
+
static addElement(parent, name, attributes, sibling = undefined) {
|
|
35
|
+
let element = document.createElementNS(SVG_NAMESPACE, name)
|
|
36
|
+
if (name === "use") {
|
|
37
|
+
attributes["xlink:href"] = attributes["href"] // fix for safari
|
|
38
|
+
}
|
|
39
|
+
for (let attribute in attributes) {
|
|
40
|
+
if (attributes.hasOwnProperty(attribute)) {
|
|
41
|
+
if (attribute.indexOf(":") !== -1) {
|
|
42
|
+
const value = attribute.split(":")
|
|
43
|
+
element.setAttributeNS("http://www.w3.org/1999/" + value[0], value[1], attributes[attribute])
|
|
44
|
+
} else {
|
|
45
|
+
element.setAttribute(attribute, attributes[attribute])
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (sibling !== undefined) {
|
|
50
|
+
parent.appendChild(element)
|
|
51
|
+
} else {
|
|
52
|
+
parent.insertBefore(element, sibling)
|
|
53
|
+
}
|
|
54
|
+
return element
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Remove an Element from a SVG DOM
|
|
59
|
+
* @param element
|
|
60
|
+
*/
|
|
61
|
+
static removeElement(element) {
|
|
62
|
+
if (element.parentNode) {
|
|
63
|
+
element.parentNode.removeChild(element)
|
|
64
|
+
} else {
|
|
65
|
+
console.warn(element, "without parentNode")
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
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
|
+
export class Utils {
|
|
8
|
+
|
|
9
|
+
static delegate(element, eventName, selector, handler) {
|
|
10
|
+
const eventListener = function (event) {
|
|
11
|
+
let target = event.target
|
|
12
|
+
while (target && target !== this) {
|
|
13
|
+
if (target.matches(selector)) {
|
|
14
|
+
handler.call(target, event)
|
|
15
|
+
}
|
|
16
|
+
target = target.parentNode
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
element.addEventListener(eventName, eventListener)
|
|
20
|
+
return {
|
|
21
|
+
remove: function () {
|
|
22
|
+
element.removeEventListener(eventName, eventListener)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
static mergeObjects(target, source) {
|
|
28
|
+
const isObject = (obj) => obj && typeof obj === 'object'
|
|
29
|
+
if (!isObject(target) || !isObject(source)) {
|
|
30
|
+
return source
|
|
31
|
+
}
|
|
32
|
+
for (const key of Object.keys(source)) {
|
|
33
|
+
if (source[key] instanceof Object) {
|
|
34
|
+
Object.assign(source[key], Utils.mergeObjects(target[key], source[key]))
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
Object.assign(target || {}, source)
|
|
38
|
+
return target
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
}
|
|
@@ -8,9 +8,9 @@ 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, moving over a square, validating or canceled
|
|
12
12
|
redrawBoard: "redrawBoard", // called after redrawing the board
|
|
13
|
-
animation: "animation", // called on every animation frame
|
|
13
|
+
animation: "animation", // called on animation start, end and on every animation frame
|
|
14
14
|
destroy: "destroy" // called, before the board is destroyed
|
|
15
15
|
}
|
|
16
16
|
|