cm-chessboard 7.4.4 → 7.5.0
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.
|
@@ -74,6 +74,7 @@
|
|
|
74
74
|
if (result) {
|
|
75
75
|
chess.move({from: event.squareFrom, to: event.squareTo, promotion: result.piece.charAt(1)})
|
|
76
76
|
event.chessboard.setPosition(chess.fen(), true)
|
|
77
|
+
event.chessboard.disableMoveInput()
|
|
77
78
|
makeEngineMove(event.chessboard)
|
|
78
79
|
} else {
|
|
79
80
|
event.chessboard.setPosition(chess.fen(), true)
|
package/package.json
CHANGED
|
@@ -13,7 +13,7 @@ import {Svg} from "../lib/Svg.js"
|
|
|
13
13
|
export class ChessboardView {
|
|
14
14
|
constructor(chessboard) {
|
|
15
15
|
this.chessboard = chessboard
|
|
16
|
-
this.
|
|
16
|
+
this.visualMoveInput = new VisualMoveInput(this,
|
|
17
17
|
this.moveInputStartedCallback.bind(this),
|
|
18
18
|
this.movingOverSquareCallback.bind(this),
|
|
19
19
|
this.validateMoveInputCallback.bind(this),
|
|
@@ -47,11 +47,11 @@ export class ChessboardView {
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
pointerDownHandler(e) {
|
|
50
|
-
this.
|
|
50
|
+
this.visualMoveInput.onPointerDown(e)
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
destroy() {
|
|
54
|
-
this.
|
|
54
|
+
this.visualMoveInput.destroy()
|
|
55
55
|
if (this.resizeObserver) {
|
|
56
56
|
this.resizeObserver.unobserve(this.chessboard.context)
|
|
57
57
|
}
|
|
@@ -223,10 +223,12 @@ export class ChessboardView {
|
|
|
223
223
|
|
|
224
224
|
redrawPieces(squares = this.chessboard.state.position.squares) {
|
|
225
225
|
const childNodes = Array.from(this.piecesGroup.childNodes)
|
|
226
|
+
const isDragging = this.visualMoveInput.isDragging()
|
|
226
227
|
for (let i = 0; i < 64; i++) {
|
|
227
228
|
const pieceName = squares[i]
|
|
228
229
|
if (pieceName) {
|
|
229
|
-
|
|
230
|
+
const square = Position.indexToSquare(i)
|
|
231
|
+
this.drawPieceOnSquare(square, pieceName, isDragging && square === this.visualMoveInput.fromSquare)
|
|
230
232
|
}
|
|
231
233
|
}
|
|
232
234
|
for (const childNode of childNodes) {
|
|
@@ -250,10 +252,13 @@ export class ChessboardView {
|
|
|
250
252
|
return pieceGroup
|
|
251
253
|
}
|
|
252
254
|
|
|
253
|
-
drawPieceOnSquare(square, pieceName) {
|
|
255
|
+
drawPieceOnSquare(square, pieceName, hidden = false) {
|
|
254
256
|
const pieceGroup = Svg.addElement(this.piecesGroup, "g", {})
|
|
255
257
|
pieceGroup.setAttribute("data-piece", pieceName)
|
|
256
258
|
pieceGroup.setAttribute("data-square", square)
|
|
259
|
+
if(hidden) {
|
|
260
|
+
pieceGroup.setAttribute("visibility","hidden")
|
|
261
|
+
}
|
|
257
262
|
const point = this.squareToPoint(square)
|
|
258
263
|
const transform = (this.svg.createSVGTransform())
|
|
259
264
|
transform.setTranslate(point.x, point.y)
|
|
@@ -302,7 +307,7 @@ export class ChessboardView {
|
|
|
302
307
|
// enable and disable move input //
|
|
303
308
|
|
|
304
309
|
enableMoveInput(eventHandler, color = undefined) {
|
|
305
|
-
if(this.moveInputCallback) {
|
|
310
|
+
if (this.moveInputCallback) {
|
|
306
311
|
throw Error("moveInput already enabled")
|
|
307
312
|
}
|
|
308
313
|
if (color === COLOR.white) {
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
import {Svg} from "../lib/Svg.js"
|
|
8
8
|
import {Utils} from "../lib/Utils.js"
|
|
9
9
|
|
|
10
|
-
const
|
|
10
|
+
const MOVE_INPUT_STATE = {
|
|
11
11
|
waitForInputStart: 0,
|
|
12
12
|
pieceClickedThreshold: 1,
|
|
13
13
|
clickTo: 2,
|
|
@@ -33,6 +33,7 @@ export class VisualMoveInput {
|
|
|
33
33
|
constructor(view, moveInputStartedCallback, movingOverSquareCallback, validateMoveInputCallback, moveInputCanceledCallback) {
|
|
34
34
|
this.view = view
|
|
35
35
|
this.chessboard = view.chessboard
|
|
36
|
+
this.moveInputState = null
|
|
36
37
|
this.moveInputStartedCallback = (square) => {
|
|
37
38
|
const result = moveInputStartedCallback(square)
|
|
38
39
|
if (result) {
|
|
@@ -52,7 +53,7 @@ export class VisualMoveInput {
|
|
|
52
53
|
moveInputCanceledCallback(fromSquare, toSquare, reason)
|
|
53
54
|
this.chessboard.state.moveInputProcess.resolve()
|
|
54
55
|
}
|
|
55
|
-
this.setMoveInputState(
|
|
56
|
+
this.setMoveInputState(MOVE_INPUT_STATE.waitForInputStart)
|
|
56
57
|
}
|
|
57
58
|
|
|
58
59
|
setMoveInputState(newState, params = undefined) {
|
|
@@ -64,11 +65,11 @@ export class VisualMoveInput {
|
|
|
64
65
|
|
|
65
66
|
switch (newState) {
|
|
66
67
|
|
|
67
|
-
case
|
|
68
|
+
case MOVE_INPUT_STATE.waitForInputStart:
|
|
68
69
|
break
|
|
69
70
|
|
|
70
|
-
case
|
|
71
|
-
if (
|
|
71
|
+
case MOVE_INPUT_STATE.pieceClickedThreshold:
|
|
72
|
+
if (MOVE_INPUT_STATE.waitForInputStart !== prevState && MOVE_INPUT_STATE.clickTo !== prevState) {
|
|
72
73
|
throw new Error("moveInputState")
|
|
73
74
|
}
|
|
74
75
|
if (this.pointerMoveListener) {
|
|
@@ -110,25 +111,25 @@ export class VisualMoveInput {
|
|
|
110
111
|
}
|
|
111
112
|
break
|
|
112
113
|
|
|
113
|
-
case
|
|
114
|
+
case MOVE_INPUT_STATE.clickTo:
|
|
114
115
|
if (this.draggablePiece) {
|
|
115
116
|
Svg.removeElement(this.draggablePiece)
|
|
116
117
|
this.draggablePiece = undefined
|
|
117
118
|
}
|
|
118
|
-
if (prevState ===
|
|
119
|
+
if (prevState === MOVE_INPUT_STATE.dragTo) {
|
|
119
120
|
this.view.setPieceVisibility(params.square, true)
|
|
120
121
|
}
|
|
121
122
|
break
|
|
122
123
|
|
|
123
|
-
case
|
|
124
|
-
if (
|
|
124
|
+
case MOVE_INPUT_STATE.secondClickThreshold:
|
|
125
|
+
if (MOVE_INPUT_STATE.clickTo !== prevState) {
|
|
125
126
|
throw new Error("moveInputState")
|
|
126
127
|
}
|
|
127
128
|
this.startPoint = params.point
|
|
128
129
|
break
|
|
129
130
|
|
|
130
|
-
case
|
|
131
|
-
if (
|
|
131
|
+
case MOVE_INPUT_STATE.dragTo:
|
|
132
|
+
if (MOVE_INPUT_STATE.pieceClickedThreshold !== prevState) {
|
|
132
133
|
throw new Error("moveInputState")
|
|
133
134
|
}
|
|
134
135
|
if (this.view.chessboard.state.inputEnabled) {
|
|
@@ -137,8 +138,8 @@ export class VisualMoveInput {
|
|
|
137
138
|
}
|
|
138
139
|
break
|
|
139
140
|
|
|
140
|
-
case
|
|
141
|
-
if (
|
|
141
|
+
case MOVE_INPUT_STATE.clickDragTo:
|
|
142
|
+
if (MOVE_INPUT_STATE.secondClickThreshold !== prevState) {
|
|
142
143
|
throw new Error("moveInputState")
|
|
143
144
|
}
|
|
144
145
|
if (this.view.chessboard.state.inputEnabled) {
|
|
@@ -147,29 +148,29 @@ export class VisualMoveInput {
|
|
|
147
148
|
}
|
|
148
149
|
break
|
|
149
150
|
|
|
150
|
-
case
|
|
151
|
-
if ([
|
|
151
|
+
case MOVE_INPUT_STATE.moveDone:
|
|
152
|
+
if ([MOVE_INPUT_STATE.dragTo, MOVE_INPUT_STATE.clickTo, MOVE_INPUT_STATE.clickDragTo].indexOf(prevState) === -1) {
|
|
152
153
|
throw new Error("moveInputState")
|
|
153
154
|
}
|
|
154
155
|
this.toSquare = params.square
|
|
155
156
|
if (this.toSquare && this.validateMoveInputCallback(this.fromSquare, this.toSquare)) {
|
|
156
|
-
if (prevState ===
|
|
157
|
+
if (prevState === MOVE_INPUT_STATE.clickTo) {
|
|
157
158
|
this.chessboard.movePiece(this.fromSquare, this.toSquare, true).then(() => {
|
|
158
|
-
this.setMoveInputState(
|
|
159
|
+
this.setMoveInputState(MOVE_INPUT_STATE.reset)
|
|
159
160
|
})
|
|
160
161
|
} else {
|
|
161
162
|
this.chessboard.movePiece(this.fromSquare, this.toSquare, false).then(() => {
|
|
162
163
|
this.view.setPieceVisibility(this.toSquare, true)
|
|
163
|
-
this.setMoveInputState(
|
|
164
|
+
this.setMoveInputState(MOVE_INPUT_STATE.reset)
|
|
164
165
|
})
|
|
165
166
|
}
|
|
166
167
|
} else {
|
|
167
168
|
this.view.setPieceVisibility(this.fromSquare, true)
|
|
168
|
-
this.setMoveInputState(
|
|
169
|
+
this.setMoveInputState(MOVE_INPUT_STATE.reset)
|
|
169
170
|
}
|
|
170
171
|
break
|
|
171
172
|
|
|
172
|
-
case
|
|
173
|
+
case MOVE_INPUT_STATE.reset:
|
|
173
174
|
if (this.fromSquare && !this.toSquare && this.movedPiece) {
|
|
174
175
|
this.chessboard.state.position.setPiece(this.fromSquare, this.movedPiece)
|
|
175
176
|
}
|
|
@@ -192,7 +193,7 @@ export class VisualMoveInput {
|
|
|
192
193
|
removeEventListener("contextmenu", this.contextMenuListener)
|
|
193
194
|
this.contextMenuListener = undefined
|
|
194
195
|
}
|
|
195
|
-
this.setMoveInputState(
|
|
196
|
+
this.setMoveInputState(MOVE_INPUT_STATE.waitForInputStart)
|
|
196
197
|
break
|
|
197
198
|
|
|
198
199
|
default:
|
|
@@ -203,7 +204,7 @@ export class VisualMoveInput {
|
|
|
203
204
|
createDraggablePiece(pieceName) {
|
|
204
205
|
// maybe I should use the existing piece from the board and don't create a new one
|
|
205
206
|
if (this.draggablePiece) {
|
|
206
|
-
throw Error("
|
|
207
|
+
throw Error("draggablePiece already exists")
|
|
207
208
|
}
|
|
208
209
|
this.draggablePiece = Svg.createSvg(document.body)
|
|
209
210
|
this.draggablePiece.classList.add("cm-chessboard-draggable-piece")
|
|
@@ -241,7 +242,7 @@ export class VisualMoveInput {
|
|
|
241
242
|
e.preventDefault()
|
|
242
243
|
}
|
|
243
244
|
}
|
|
244
|
-
if (this.moveInputState !==
|
|
245
|
+
if (this.moveInputState !== MOVE_INPUT_STATE.waitForInputStart ||
|
|
245
246
|
this.chessboard.state.inputWhiteEnabled && color === "w" ||
|
|
246
247
|
this.chessboard.state.inputBlackEnabled && color === "b") {
|
|
247
248
|
let point
|
|
@@ -250,16 +251,16 @@ export class VisualMoveInput {
|
|
|
250
251
|
} else if (e.type === "touchstart") {
|
|
251
252
|
point = {x: e.touches[0].clientX, y: e.touches[0].clientY}
|
|
252
253
|
}
|
|
253
|
-
if (this.moveInputState ===
|
|
254
|
-
this.setMoveInputState(
|
|
254
|
+
if (this.moveInputState === MOVE_INPUT_STATE.waitForInputStart && pieceName && this.moveInputStartedCallback(square)) {
|
|
255
|
+
this.setMoveInputState(MOVE_INPUT_STATE.pieceClickedThreshold, {
|
|
255
256
|
square: square,
|
|
256
257
|
piece: pieceName,
|
|
257
258
|
point: point,
|
|
258
259
|
type: e.type
|
|
259
260
|
})
|
|
260
|
-
} else if (this.moveInputState ===
|
|
261
|
+
} else if (this.moveInputState === MOVE_INPUT_STATE.clickTo) {
|
|
261
262
|
if (square === this.fromSquare) {
|
|
262
|
-
this.setMoveInputState(
|
|
263
|
+
this.setMoveInputState(MOVE_INPUT_STATE.secondClickThreshold, {
|
|
263
264
|
square: square,
|
|
264
265
|
piece: pieceName,
|
|
265
266
|
point: point,
|
|
@@ -273,17 +274,17 @@ export class VisualMoveInput {
|
|
|
273
274
|
if (color && startPieceColor === pieceColor) {
|
|
274
275
|
this.moveInputCanceledCallback(this.fromSquare, square, MOVE_CANCELED_REASON.clickedAnotherPiece)
|
|
275
276
|
if (this.moveInputStartedCallback(square)) {
|
|
276
|
-
this.setMoveInputState(
|
|
277
|
+
this.setMoveInputState(MOVE_INPUT_STATE.pieceClickedThreshold, {
|
|
277
278
|
square: square,
|
|
278
279
|
piece: pieceName,
|
|
279
280
|
point: point,
|
|
280
281
|
type: e.type
|
|
281
282
|
})
|
|
282
283
|
} else {
|
|
283
|
-
this.setMoveInputState(
|
|
284
|
+
this.setMoveInputState(MOVE_INPUT_STATE.reset)
|
|
284
285
|
}
|
|
285
286
|
} else {
|
|
286
|
-
this.setMoveInputState(
|
|
287
|
+
this.setMoveInputState(MOVE_INPUT_STATE.moveDone, {square: square})
|
|
287
288
|
}
|
|
288
289
|
}
|
|
289
290
|
}
|
|
@@ -307,18 +308,18 @@ export class VisualMoveInput {
|
|
|
307
308
|
pageY = e.touches[0].pageY
|
|
308
309
|
target = document.elementFromPoint(clientX, clientY)
|
|
309
310
|
}
|
|
310
|
-
if (this.moveInputState ===
|
|
311
|
+
if (this.moveInputState === MOVE_INPUT_STATE.pieceClickedThreshold || this.moveInputState === MOVE_INPUT_STATE.secondClickThreshold) {
|
|
311
312
|
if (Math.abs(this.startPoint.x - clientX) > DRAG_THRESHOLD || Math.abs(this.startPoint.y - clientY) > DRAG_THRESHOLD) {
|
|
312
|
-
if (this.moveInputState ===
|
|
313
|
-
this.setMoveInputState(
|
|
313
|
+
if (this.moveInputState === MOVE_INPUT_STATE.secondClickThreshold) {
|
|
314
|
+
this.setMoveInputState(MOVE_INPUT_STATE.clickDragTo, {square: this.fromSquare, piece: this.movedPiece})
|
|
314
315
|
} else {
|
|
315
|
-
this.setMoveInputState(
|
|
316
|
+
this.setMoveInputState(MOVE_INPUT_STATE.dragTo, {square: this.fromSquare, piece: this.movedPiece})
|
|
316
317
|
}
|
|
317
318
|
if (this.view.chessboard.state.inputEnabled) {
|
|
318
319
|
this.moveDraggablePiece(pageX, pageY)
|
|
319
320
|
}
|
|
320
321
|
}
|
|
321
|
-
} else if (this.moveInputState ===
|
|
322
|
+
} else if (this.moveInputState === MOVE_INPUT_STATE.dragTo || this.moveInputState === MOVE_INPUT_STATE.clickDragTo || this.moveInputState === MOVE_INPUT_STATE.clickTo) {
|
|
322
323
|
if (target && target.getAttribute && target.parentElement === this.view.boardGroup) {
|
|
323
324
|
const square = target.getAttribute("data-square")
|
|
324
325
|
if (square !== this.fromSquare && square !== this.toSquare) {
|
|
@@ -334,7 +335,7 @@ export class VisualMoveInput {
|
|
|
334
335
|
this.movingOverSquareCallback(this.fromSquare, null)
|
|
335
336
|
}
|
|
336
337
|
}
|
|
337
|
-
if (this.view.chessboard.state.inputEnabled && (this.moveInputState ===
|
|
338
|
+
if (this.view.chessboard.state.inputEnabled && (this.moveInputState === MOVE_INPUT_STATE.dragTo || this.moveInputState === MOVE_INPUT_STATE.clickDragTo)) {
|
|
338
339
|
this.moveDraggablePiece(pageX, pageY)
|
|
339
340
|
}
|
|
340
341
|
}
|
|
@@ -351,46 +352,50 @@ export class VisualMoveInput {
|
|
|
351
352
|
const square = target.getAttribute("data-square")
|
|
352
353
|
|
|
353
354
|
if (square) {
|
|
354
|
-
if (this.moveInputState ===
|
|
355
|
+
if (this.moveInputState === MOVE_INPUT_STATE.dragTo || this.moveInputState === MOVE_INPUT_STATE.clickDragTo) {
|
|
355
356
|
if (this.fromSquare === square) {
|
|
356
|
-
if (this.moveInputState ===
|
|
357
|
+
if (this.moveInputState === MOVE_INPUT_STATE.clickDragTo) {
|
|
357
358
|
this.chessboard.state.position.setPiece(this.fromSquare, this.movedPiece)
|
|
358
359
|
this.view.setPieceVisibility(this.fromSquare)
|
|
359
360
|
this.moveInputCanceledCallback(square, null, MOVE_CANCELED_REASON.draggedBack)
|
|
360
|
-
this.setMoveInputState(
|
|
361
|
+
this.setMoveInputState(MOVE_INPUT_STATE.reset)
|
|
361
362
|
} else {
|
|
362
|
-
this.setMoveInputState(
|
|
363
|
+
this.setMoveInputState(MOVE_INPUT_STATE.clickTo, {square: square})
|
|
363
364
|
}
|
|
364
365
|
} else {
|
|
365
|
-
this.setMoveInputState(
|
|
366
|
+
this.setMoveInputState(MOVE_INPUT_STATE.moveDone, {square: square})
|
|
366
367
|
}
|
|
367
|
-
} else if (this.moveInputState ===
|
|
368
|
-
this.setMoveInputState(
|
|
369
|
-
} else if (this.moveInputState ===
|
|
370
|
-
this.setMoveInputState(
|
|
368
|
+
} else if (this.moveInputState === MOVE_INPUT_STATE.pieceClickedThreshold) {
|
|
369
|
+
this.setMoveInputState(MOVE_INPUT_STATE.clickTo, {square: square})
|
|
370
|
+
} else if (this.moveInputState === MOVE_INPUT_STATE.secondClickThreshold) {
|
|
371
|
+
this.setMoveInputState(MOVE_INPUT_STATE.reset)
|
|
371
372
|
this.moveInputCanceledCallback(square, null, MOVE_CANCELED_REASON.secondClick)
|
|
372
373
|
}
|
|
373
374
|
} else {
|
|
374
375
|
this.view.redrawPieces()
|
|
375
376
|
const moveStartSquare = this.fromSquare
|
|
376
|
-
this.setMoveInputState(
|
|
377
|
+
this.setMoveInputState(MOVE_INPUT_STATE.reset)
|
|
377
378
|
this.moveInputCanceledCallback(moveStartSquare, null, MOVE_CANCELED_REASON.movedOutOfBoard)
|
|
378
379
|
}
|
|
379
380
|
} else {
|
|
380
381
|
this.view.redrawPieces()
|
|
381
|
-
this.setMoveInputState(
|
|
382
|
+
this.setMoveInputState(MOVE_INPUT_STATE.reset)
|
|
382
383
|
}
|
|
383
384
|
}
|
|
384
385
|
|
|
385
386
|
onContextMenu(e) { // while moving
|
|
386
387
|
e.preventDefault()
|
|
387
388
|
this.view.redrawPieces()
|
|
388
|
-
this.setMoveInputState(
|
|
389
|
+
this.setMoveInputState(MOVE_INPUT_STATE.reset)
|
|
389
390
|
this.moveInputCanceledCallback(this.fromSquare, null, MOVE_CANCELED_REASON.secondaryClick)
|
|
390
391
|
}
|
|
391
392
|
|
|
393
|
+
isDragging() {
|
|
394
|
+
return this.moveInputState === MOVE_INPUT_STATE.dragTo || this.moveInputState === MOVE_INPUT_STATE.clickDragTo
|
|
395
|
+
}
|
|
396
|
+
|
|
392
397
|
destroy() {
|
|
393
|
-
this.setMoveInputState(
|
|
398
|
+
this.setMoveInputState(MOVE_INPUT_STATE.reset)
|
|
394
399
|
}
|
|
395
400
|
|
|
396
401
|
}
|