cm-chessboard 7.6.3 → 7.6.5

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
@@ -380,6 +380,8 @@ const chessboard = new Chessboard(document.getElementById("board"), {
380
380
 
381
381
  ### registerMethod(name, callback)
382
382
 
383
+ > Deprecated 2023-05-18, just add methods directly to the chessboard class instance.
384
+
383
385
  Add methods to the main chessboard from your extension with `this.registerMethod("name", callback)`
384
386
  like `addArrow(type, from, to)` in the
385
387
  [Arrows extension](https://shaack.com/projekte/cm-chessboard/examples/extensions/arrows-extension.html).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cm-chessboard",
3
- "version": "7.6.3",
3
+ "version": "7.6.5",
4
4
  "description": "A JavaScript chessboard which is lightweight, ES6 module based, responsive, SVG rendered and without dependencies.",
5
5
  "keywords": [
6
6
  "chess",
@@ -29,6 +29,6 @@
29
29
  },
30
30
  "homepage": "https://github.com/shaack/cm-chessboard#readme",
31
31
  "devDependencies": {
32
- "teevi": "^2.2.2"
32
+ "teevi": "^2.2.4"
33
33
  }
34
34
  }
@@ -14,6 +14,8 @@ export const ARROW_TYPE = {
14
14
  }
15
15
 
16
16
  export class Arrows extends Extension {
17
+
18
+ /** @constructor */
17
19
  constructor(chessboard, props = {}) {
18
20
  super(chessboard)
19
21
  this.registerExtensionPoint(EXTENSION_POINT.redrawBoard, () => {
@@ -27,9 +29,9 @@ export class Arrows extends Extension {
27
29
  this.chessboard.view.cacheSpriteToDiv("cm-chessboard-arrows",
28
30
  this.chessboard.props.assetsUrl + "extensions/arrows/" + this.props.sprite)
29
31
  }
30
- this.registerMethod("addArrow", this.addArrow)
31
- this.registerMethod("getArrows", this.getArrows)
32
- this.registerMethod("removeArrows", this.removeArrows)
32
+ chessboard.addArrow = this.addArrow.bind(this)
33
+ chessboard.getArrows = this.getArrows.bind(this)
34
+ chessboard.removeArrows = this.removeArrows.bind(this)
33
35
  this.arrowGroup = Svg.addElement(chessboard.view.markersTopLayer, "g", {class: "arrows"})
34
36
  this.arrows = []
35
37
  }
@@ -21,6 +21,8 @@ export const MARKER_TYPE = {
21
21
  }
22
22
 
23
23
  export class Markers extends Extension {
24
+
25
+ /** @constructor */
24
26
  constructor(chessboard, props = {}) {
25
27
  super(chessboard)
26
28
  this.registerExtensionPoint(EXTENSION_POINT.redrawBoard, () => {
@@ -35,9 +37,9 @@ export class Markers extends Extension {
35
37
  chessboard.view.cacheSpriteToDiv("cm-chessboard-markers", this.chessboard.props.assetsUrl +
36
38
  "extensions/markers/" + this.props.sprite)
37
39
  }
38
- this.registerMethod("addMarker", this.addMarker)
39
- this.registerMethod("getMarkers", this.getMarkers)
40
- this.registerMethod("removeMarkers", this.removeMarkers)
40
+ chessboard.addMarker = this.addMarker.bind(this)
41
+ chessboard.getMarkers = this.getMarkers.bind(this)
42
+ chessboard.removeMarkers = this.removeMarkers.bind(this)
41
43
  this.markerGroupDown = Svg.addElement(chessboard.view.markersLayer, "g", {class: "markers"})
42
44
  this.markerGroupUp = Svg.addElement(chessboard.view.markersTopLayer, "g", {class: "markers"})
43
45
  this.markers = []
@@ -17,11 +17,13 @@ const DISPLAY_STATE = {
17
17
 
18
18
  export class PromotionDialog extends Extension {
19
19
 
20
+ /** @constructor */
20
21
  constructor(chessboard) {
21
22
  super(chessboard)
22
23
  this.registerExtensionPoint(EXTENSION_POINT.redrawBoard, this.extensionPointRedrawBoard.bind(this))
23
24
  this.registerExtensionPoint(EXTENSION_POINT.animation, this.extensionPointAnimation.bind(this))
24
- this.registerMethod("showPromotionDialog", this.showPromotionDialog)
25
+ chessboard.showPromotionDialog = this.showPromotionDialog.bind(this)
26
+ chessboard.isPromotionDialogShown = this.isPromotionDialogShown.bind(this)
25
27
  this.promotionDialogGroup = Svg.addElement(chessboard.view.interactiveTopLayer, "g", {class: "promotion-dialog-group"})
26
28
  this.state = {
27
29
  displayState: DISPLAY_STATE.hidden,
@@ -41,6 +43,11 @@ export class PromotionDialog extends Extension {
41
43
  this.setDisplayState(DISPLAY_STATE.displayRequested)
42
44
  }
43
45
 
46
+ isPromotionDialogShown() {
47
+ return this.state.displayState === DISPLAY_STATE.shown ||
48
+ this.state.displayState === DISPLAY_STATE.displayRequested
49
+ }
50
+
44
51
  // private
45
52
  extensionPointRedrawBoard() {
46
53
  this.redrawDialog()
@@ -54,7 +61,6 @@ export class PromotionDialog extends Extension {
54
61
  }
55
62
  }
56
63
 
57
-
58
64
  drawPieceButton(piece, point) {
59
65
  const squareWidth = this.chessboard.view.squareWidth
60
66
  const squareHeight = this.chessboard.view.squareHeight
@@ -133,7 +139,7 @@ export class PromotionDialog extends Extension {
133
139
  }
134
140
 
135
141
  promotionDialogOnClickPiece(event) {
136
- if(event.button !== 2) {
142
+ if (event.button !== 2) {
137
143
  if (event.target.dataset.piece) {
138
144
  this.state.callback({square: this.state.dialogParams.square, piece: event.target.dataset.piece})
139
145
  this.setDisplayState(DISPLAY_STATE.hidden)
@@ -144,7 +150,7 @@ export class PromotionDialog extends Extension {
144
150
  }
145
151
 
146
152
  promotionDialogOnCancel(event) {
147
- if(this.state.displayState === DISPLAY_STATE.shown) {
153
+ if (this.state.displayState === DISPLAY_STATE.shown) {
148
154
  event.preventDefault()
149
155
  this.setDisplayState(DISPLAY_STATE.hidden)
150
156
  this.state.callback(null)
@@ -159,14 +165,14 @@ export class PromotionDialog extends Extension {
159
165
 
160
166
  setDisplayState(displayState) {
161
167
  this.state.displayState = displayState
162
- if(displayState === DISPLAY_STATE.shown) {
168
+ if (displayState === DISPLAY_STATE.shown) {
163
169
  this.clickDelegate = Utils.delegate(this.chessboard.view.svg,
164
170
  "mousedown",
165
171
  "*",
166
172
  this.promotionDialogOnClickPiece.bind(this))
167
173
  this.contextMenuListener = this.contextMenu.bind(this)
168
174
  this.chessboard.view.svg.addEventListener("contextmenu", this.contextMenuListener)
169
- } else if(displayState === DISPLAY_STATE.hidden) {
175
+ } else if (displayState === DISPLAY_STATE.hidden) {
170
176
  this.clickDelegate.remove()
171
177
  this.chessboard.view.svg.removeEventListener("contextmenu", this.contextMenuListener)
172
178
  }
@@ -9,6 +9,7 @@ import {ANIMATION_EVENT_TYPE} from "../../view/PositionAnimationsQueue.js"
9
9
 
10
10
  export class RenderVideo extends Extension {
11
11
 
12
+ /** @constructor */
12
13
  constructor(chessboard, props) {
13
14
  super(chessboard)
14
15
  this.props = {
@@ -38,7 +39,7 @@ export class RenderVideo extends Extension {
38
39
  }
39
40
  }
40
41
  })
41
- this.registerMethod("recorderInit", () => {
42
+ chessboard.recorderInit = () => {
42
43
  return new Promise((resolve) => {
43
44
  let all = document.querySelectorAll("svg *")
44
45
  for (let i = 0; i < all.length; i++) {
@@ -72,8 +73,8 @@ export class RenderVideo extends Extension {
72
73
  resolve()
73
74
  }, 100)
74
75
  })
75
- })
76
- this.registerMethod("recorderStart", () => {
76
+ }
77
+ chessboard.recorderStart = () => {
77
78
  return new Promise((resolve) => {
78
79
  if (this.recorder && this.recorder.state === "recording") {
79
80
  console.error("recorder is running")
@@ -84,11 +85,11 @@ export class RenderVideo extends Extension {
84
85
  console.log("recorder", this.recorder.state)
85
86
  resolve()
86
87
  })
87
- })
88
+ }
88
89
  /**
89
90
  * returns the url of the recorded media
90
91
  */
91
- this.registerMethod("recorderStop", () => {
92
+ chessboard.recorderStop = () => {
92
93
  return new Promise(async (resolve, reject) => {
93
94
  if (!this.recorder || this.recorder.state !== "recording") {
94
95
  reject("recorder is not recording")
@@ -100,8 +101,8 @@ export class RenderVideo extends Extension {
100
101
  resolve(URL.createObjectURL(new Blob(this.recordedData, {type: this.props.mediaType})))
101
102
  }, 200)
102
103
  })
103
- })
104
- this.registerMethod("recorderPause", (ms) => {
104
+ }
105
+ chessboard.recorderPause = (ms) => {
105
106
  return new Promise(async (resolve) => {
106
107
  await this.cloneImageAndRender()
107
108
  const interval = setInterval(async () => {
@@ -113,7 +114,7 @@ export class RenderVideo extends Extension {
113
114
  resolve()
114
115
  }, ms)
115
116
  })
116
- })
117
+ }
117
118
  }
118
119
 
119
120
  async cloneImageAndRender() {
@@ -27,7 +27,9 @@ export class Extension {
27
27
  this.chessboard.state.extensionPoints[name].push(callback)
28
28
  }
29
29
 
30
+ /** @deprecated 2023-05-18 */
30
31
  registerMethod(name, callback) {
32
+ console.warn("registerMethod is deprecated, just add methods directly to the chessboard instance")
31
33
  if (!this.chessboard[name]) {
32
34
  this.chessboard[name] = (...args) => {
33
35
  return callback.apply(this, args)