cm-chessboard 7.6.8 → 7.6.10

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cm-chessboard",
3
- "version": "7.6.8",
3
+ "version": "7.6.10",
4
4
  "description": "A JavaScript chessboard which is lightweight, ES6 module based, responsive, SVG rendered and without dependencies.",
5
5
  "keywords": [
6
6
  "chess",
@@ -7,7 +7,6 @@ import {Extension, EXTENSION_POINT} from "../../model/Extension.js"
7
7
  import {COLOR, PIECE} from "../../Chessboard.js"
8
8
  import {Svg} from "../../lib/Svg.js"
9
9
  import {Utils} from "../../lib/Utils.js"
10
- import {ANIMATION_EVENT_TYPE} from "../../view/PositionAnimationsQueue.js"
11
10
 
12
11
  const DISPLAY_STATE = {
13
12
  hidden: "hidden",
@@ -21,7 +20,6 @@ export class PromotionDialog extends Extension {
21
20
  constructor(chessboard) {
22
21
  super(chessboard)
23
22
  this.registerExtensionPoint(EXTENSION_POINT.redrawBoard, this.extensionPointRedrawBoard.bind(this))
24
- this.registerExtensionPoint(EXTENSION_POINT.animation, this.extensionPointAnimation.bind(this))
25
23
  chessboard.showPromotionDialog = this.showPromotionDialog.bind(this)
26
24
  chessboard.isPromotionDialogShown = this.isPromotionDialogShown.bind(this)
27
25
  this.promotionDialogGroup = Svg.addElement(chessboard.view.interactiveTopLayer, "g", {class: "promotion-dialog-group"})
@@ -35,14 +33,21 @@ export class PromotionDialog extends Extension {
35
33
  }
36
34
  }
37
35
 
38
- // public (registerMethod)
36
+ // public (chessboard.showPromotionDialog)
39
37
  showPromotionDialog(square, color, callback) {
40
38
  this.state.dialogParams.square = square
41
39
  this.state.dialogParams.color = color
42
40
  this.state.callback = callback
43
41
  this.setDisplayState(DISPLAY_STATE.displayRequested)
42
+ setTimeout(() => {
43
+ this.chessboard.view.positionsAnimationTask.then(() => {
44
+ this.setDisplayState(DISPLAY_STATE.shown)
45
+ })
46
+ }
47
+ )
44
48
  }
45
49
 
50
+ // public (chessboard.isPromotionDialogShown)
46
51
  isPromotionDialogShown() {
47
52
  return this.state.displayState === DISPLAY_STATE.shown ||
48
53
  this.state.displayState === DISPLAY_STATE.displayRequested
@@ -53,14 +58,6 @@ export class PromotionDialog extends Extension {
53
58
  this.redrawDialog()
54
59
  }
55
60
 
56
- extensionPointAnimation(event) {
57
- if (event.type === ANIMATION_EVENT_TYPE.end) {
58
- if (this.state.displayState === DISPLAY_STATE.displayRequested) {
59
- this.setDisplayState(DISPLAY_STATE.shown)
60
- }
61
- }
62
- }
63
-
64
61
  drawPieceButton(piece, point) {
65
62
  const squareWidth = this.chessboard.view.squareWidth
66
63
  const squareHeight = this.chessboard.view.squareHeight
package/src/lib/Svg.js CHANGED
@@ -28,10 +28,9 @@ export class Svg {
28
28
  * @param parent
29
29
  * @param name
30
30
  * @param attributes
31
- * @param sibling // TODO remove this parameter (2023-05-22)
32
31
  * @returns {Element}
33
32
  */
34
- static addElement(parent, name, attributes = {}, sibling = undefined) {
33
+ static addElement(parent, name, attributes = {}) {
35
34
  let element = document.createElementNS(SVG_NAMESPACE, name)
36
35
  if (name === "use") {
37
36
  attributes["xlink:href"] = attributes["href"] // fix for safari
@@ -46,11 +45,7 @@ export class Svg {
46
45
  }
47
46
  }
48
47
  }
49
- if (sibling !== undefined) {
50
- parent.appendChild(element)
51
- } else {
52
- parent.insertBefore(element, sibling)
53
- }
48
+ parent.appendChild(element)
54
49
  return element
55
50
  }
56
51
 
@@ -30,7 +30,7 @@ export class ChessboardView {
30
30
  window.addEventListener("resize", this.resizeListener)
31
31
  }
32
32
  }
33
-
33
+ this.positionsAnimationTask = Promise.resolve()
34
34
  this.pointerDownListener = this.pointerDownHandler.bind(this)
35
35
  this.pointerDownListener = this.pointerDownHandler.bind(this)
36
36
  this.context.addEventListener("mousedown", this.pointerDownListener)
@@ -6,6 +6,7 @@
6
6
  import {FEN, Position} from "../model/Position.js"
7
7
  import {Svg} from "../lib/Svg.js"
8
8
  import {EXTENSION_POINT} from "../model/Extension.js"
9
+ import {Utils} from "../lib/Utils.js"
9
10
 
10
11
  /*
11
12
  * Thanks to markosyan for the idea of the PromiseQueue
@@ -92,6 +93,10 @@ export class PositionsAnimation {
92
93
  } else {
93
94
  console.error("fromPosition", fromPosition, "toPosition", toPosition)
94
95
  }
96
+ this.view.positionsAnimationTask = Utils.createTask()
97
+ this.view.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.animation, {
98
+ type: ANIMATION_EVENT_TYPE.start
99
+ })
95
100
  }
96
101
 
97
102
  static seekChanges(fromSquares, toSquares) {
@@ -173,21 +178,18 @@ export class PositionsAnimation {
173
178
  }
174
179
  if (!this.startTime) {
175
180
  this.startTime = time
176
- this.view.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.animation, {
177
- type: ANIMATION_EVENT_TYPE.start
178
- })
179
181
  }
180
182
  const timeDiff = time - this.startTime
181
183
  if (timeDiff <= this.duration) {
182
184
  this.frameHandle = requestAnimationFrame(this.animationStep.bind(this))
183
185
  } else {
184
186
  cancelAnimationFrame(this.frameHandle)
185
- // console.log("ANIMATION FINISHED")
186
187
  this.animatedElements.forEach((animatedItem) => {
187
188
  if (animatedItem.type === CHANGE_TYPE.disappear) {
188
189
  Svg.removeElement(animatedItem.element)
189
190
  }
190
191
  })
192
+ this.view.positionsAnimationTask.resolve()
191
193
  this.view.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.animation, {
192
194
  type: ANIMATION_EVENT_TYPE.end
193
195
  })