@alepot55/chessboardjs 2.2.0 → 2.2.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 +3 -3
- package/chessboard.bundle.js +86 -73
- package/chessboard.js +0 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
# Chessboard.js: Interactive and Customizable Chessboard Library
|
|
2
2
|
|
|
3
|
-
Chessboard.js is a lightweight and versatile NPM package that lets you easily integrate an interactive, customizable chessboard into your web applications. Use it for game displays, chess lessons, analysis tools, or any project that needs a visual chess interface.
|
|
3
|
+
[Chessboard.js](https://sites.google.com/view/chessboard-js/home) is a lightweight and versatile NPM package that lets you easily integrate an interactive, customizable chessboard into your web applications. Use it for game displays, chess lessons, analysis tools, or any project that needs a visual chess interface.
|
|
4
4
|
|
|
5
5
|
## Overview
|
|
6
|
-
Chessboard.js is designed with simplicity and flexibility in mind. Configure board appearance, piece sets, orientation, highlighting, animations, and more through a rich API. The board updates dynamically with user interactions and programmatic moves.
|
|
6
|
+
[Chessboard.js](https://sites.google.com/view/chessboard-js/home) is designed with simplicity and flexibility in mind. Configure board appearance, piece sets, orientation, highlighting, animations, and more through a rich API. The board updates dynamically with user interactions and programmatic moves.
|
|
7
7
|
|
|
8
8
|
## Installation
|
|
9
9
|
```bash
|
|
@@ -384,7 +384,7 @@ This document details the functions available to users for interacting with the
|
|
|
384
384
|
Places a piece on the board.
|
|
385
385
|
_Example:_
|
|
386
386
|
```js
|
|
387
|
-
board.put(
|
|
387
|
+
board.put('pw', 'd4');
|
|
388
388
|
```
|
|
389
389
|
|
|
390
390
|
- **remove(squareId, animation = true)**
|
package/chessboard.bundle.js
CHANGED
|
@@ -1997,7 +1997,8 @@ var Chessboard = (function () {
|
|
|
1997
1997
|
'linear': 'linear',
|
|
1998
1998
|
'ease-in': 'ease-in',
|
|
1999
1999
|
'ease-out': 'ease-out',
|
|
2000
|
-
'ease-in-out': 'ease-in-out'
|
|
2000
|
+
'ease-in-out': 'ease-in-out',
|
|
2001
|
+
'none': null
|
|
2001
2002
|
};
|
|
2002
2003
|
|
|
2003
2004
|
class ChessboardConfig {
|
|
@@ -2051,9 +2052,6 @@ var Chessboard = (function () {
|
|
|
2051
2052
|
this.dropOffBoard = config.dropOffBoard;
|
|
2052
2053
|
this.size = config.size;
|
|
2053
2054
|
this.movableColors = config.movableColors;
|
|
2054
|
-
this.moveAnimation = config.moveAnimation;
|
|
2055
|
-
this.snapbackAnimation = config.snapbackAnimation;
|
|
2056
|
-
this.fadeAnimation = config.fadeAnimation;
|
|
2057
2055
|
this.piecesPath = config.piecesPath;
|
|
2058
2056
|
this.onMove = config.onMove;
|
|
2059
2057
|
this.onMoveEnd = config.onMoveEnd;
|
|
@@ -2063,6 +2061,10 @@ var Chessboard = (function () {
|
|
|
2063
2061
|
this.onDrop = config.onDrop;
|
|
2064
2062
|
this.onSnapbackEnd = config.onSnapbackEnd;
|
|
2065
2063
|
|
|
2064
|
+
this.moveAnimation = this.setTransitionFunction(config.moveAnimation);
|
|
2065
|
+
this.snapbackAnimation = this.setTransitionFunction(config.snapbackAnimation);
|
|
2066
|
+
this.fadeAnimation = this.setTransitionFunction(config.fadeAnimation);
|
|
2067
|
+
|
|
2066
2068
|
this.hints = this.setBoolean(config.hints);
|
|
2067
2069
|
this.clickable = this.setBoolean(config.clickable);
|
|
2068
2070
|
this.draggable = this.setBoolean(config.draggable);
|
|
@@ -2115,7 +2117,7 @@ var Chessboard = (function () {
|
|
|
2115
2117
|
}
|
|
2116
2118
|
|
|
2117
2119
|
setTransitionFunction(value) {
|
|
2118
|
-
if (transitionFunctions
|
|
2120
|
+
if (Object.keys(transitionFunctions).indexOf(value) !== -1) return transitionFunctions[value];
|
|
2119
2121
|
throw new Error('Invalid transition function');
|
|
2120
2122
|
}
|
|
2121
2123
|
}
|
|
@@ -2168,7 +2170,7 @@ var Chessboard = (function () {
|
|
|
2168
2170
|
}
|
|
2169
2171
|
|
|
2170
2172
|
setDrag(f) {
|
|
2171
|
-
this.element.ondragstart = () =>
|
|
2173
|
+
this.element.ondragstart = (e) => { e.preventDefault(); };
|
|
2172
2174
|
this.element.onmousedown = f;
|
|
2173
2175
|
}
|
|
2174
2176
|
|
|
@@ -2423,27 +2425,15 @@ var Chessboard = (function () {
|
|
|
2423
2425
|
}
|
|
2424
2426
|
|
|
2425
2427
|
check() {
|
|
2426
|
-
if (this.piece === null)
|
|
2427
|
-
|
|
2428
|
-
|
|
2429
|
-
if (!(this.
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
if (
|
|
2433
|
-
|
|
2434
|
-
|
|
2435
|
-
if (!(this.from instanceof Square)) {
|
|
2436
|
-
throw new Error("Invalid move: from is not an instance of Square");
|
|
2437
|
-
}
|
|
2438
|
-
if (!(this.to instanceof Square)) {
|
|
2439
|
-
throw new Error("Invalid move: to is not an instance of Square");
|
|
2440
|
-
}
|
|
2441
|
-
if (!this.to) {
|
|
2442
|
-
throw new Error("Invalid move: to is null or undefined");
|
|
2443
|
-
}
|
|
2444
|
-
if (!this.from) {
|
|
2445
|
-
throw new Error("Invalid move: from is null or undefined");
|
|
2446
|
-
}
|
|
2428
|
+
if (this.piece === null) return false;
|
|
2429
|
+
if (!(this.piece instanceof Piece)) return false;
|
|
2430
|
+
if (['q', 'r', 'b', 'n', null].indexOf(this.promotion) === -1) return false;
|
|
2431
|
+
if (!(this.from instanceof Square)) return false;
|
|
2432
|
+
if (!(this.to instanceof Square)) return false;
|
|
2433
|
+
if (!this.to) return false;
|
|
2434
|
+
if (!this.from) return false;
|
|
2435
|
+
if (this.from === this.to) return false;
|
|
2436
|
+
return true;
|
|
2447
2437
|
}
|
|
2448
2438
|
|
|
2449
2439
|
isLegal(game) {
|
|
@@ -2515,7 +2505,7 @@ var Chessboard = (function () {
|
|
|
2515
2505
|
}
|
|
2516
2506
|
|
|
2517
2507
|
initParams() {
|
|
2518
|
-
this.
|
|
2508
|
+
this.element = null;
|
|
2519
2509
|
this.squares = {};
|
|
2520
2510
|
this.promoting = false;
|
|
2521
2511
|
this.clicked = null;
|
|
@@ -2527,12 +2517,12 @@ var Chessboard = (function () {
|
|
|
2527
2517
|
// Board Setup
|
|
2528
2518
|
// -------------------
|
|
2529
2519
|
buildBoard() {
|
|
2530
|
-
this.
|
|
2531
|
-
if (!this.
|
|
2520
|
+
this.element = document.getElementById(this.config.id_div);
|
|
2521
|
+
if (!this.element) {
|
|
2532
2522
|
throw new Error(this.error_messages['invalid_id_div'] + this.config.id_div);
|
|
2533
2523
|
}
|
|
2534
2524
|
this.resize(this.config.size);
|
|
2535
|
-
this.
|
|
2525
|
+
this.element.className = "board";
|
|
2536
2526
|
}
|
|
2537
2527
|
|
|
2538
2528
|
buildSquares() {
|
|
@@ -2544,19 +2534,19 @@ var Chessboard = (function () {
|
|
|
2544
2534
|
let square = new Square(square_row, square_col);
|
|
2545
2535
|
this.squares[square.getId()] = square;
|
|
2546
2536
|
|
|
2547
|
-
this.
|
|
2537
|
+
this.element.appendChild(square.element);
|
|
2548
2538
|
}
|
|
2549
2539
|
}
|
|
2550
2540
|
}
|
|
2551
2541
|
|
|
2552
2542
|
removeBoard() {
|
|
2553
2543
|
|
|
2554
|
-
this.
|
|
2544
|
+
this.element.innerHTML = '';
|
|
2555
2545
|
}
|
|
2556
2546
|
|
|
2557
2547
|
removeSquares() {
|
|
2558
2548
|
for (const square of Object.values(this.squares)) {
|
|
2559
|
-
this.
|
|
2549
|
+
this.element.removeChild(square.element);
|
|
2560
2550
|
square.destroy();
|
|
2561
2551
|
|
|
2562
2552
|
}
|
|
@@ -2566,12 +2556,12 @@ var Chessboard = (function () {
|
|
|
2566
2556
|
resize(value) {
|
|
2567
2557
|
if (value === 'auto') {
|
|
2568
2558
|
let size;
|
|
2569
|
-
if (this.
|
|
2570
|
-
size = this.
|
|
2571
|
-
} else if (this.
|
|
2572
|
-
size = this.
|
|
2559
|
+
if (this.element.offsetWidth === 0) {
|
|
2560
|
+
size = this.element.offsetHeight;
|
|
2561
|
+
} else if (this.element.offsetHeight === 0) {
|
|
2562
|
+
size = this.element.offsetWidth;
|
|
2573
2563
|
} else {
|
|
2574
|
-
size = Math.min(this.
|
|
2564
|
+
size = Math.min(this.element.offsetWidth, this.element.offsetHeight);
|
|
2575
2565
|
}
|
|
2576
2566
|
this.resize(size);
|
|
2577
2567
|
} else if (typeof value !== 'number') {
|
|
@@ -2587,7 +2577,7 @@ var Chessboard = (function () {
|
|
|
2587
2577
|
// -------------------
|
|
2588
2578
|
convertFen(position) {
|
|
2589
2579
|
if (typeof position === 'string') {
|
|
2590
|
-
|
|
2580
|
+
if (position == 'start') return 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1';
|
|
2591
2581
|
if (this.validateFen(position)) return position;
|
|
2592
2582
|
else if (this.standard_positions[position]) return this.standard_positions[position];
|
|
2593
2583
|
else throw new Error('Invalid position -' + position);
|
|
@@ -2614,17 +2604,15 @@ var Chessboard = (function () {
|
|
|
2614
2604
|
if (empty > 0) rowParts.push(empty);
|
|
2615
2605
|
parts.push(rowParts.join(''));
|
|
2616
2606
|
}
|
|
2617
|
-
return parts.join('/');
|
|
2618
|
-
} else {
|
|
2607
|
+
return parts.join('/') + ' w KQkq - 0 1'; } else {
|
|
2619
2608
|
throw new Error('Invalid position -' + position);
|
|
2620
2609
|
}
|
|
2621
2610
|
}
|
|
2622
2611
|
|
|
2623
|
-
setGame(position) {
|
|
2624
|
-
console.log(position);
|
|
2612
|
+
setGame(position, options = undefined) {
|
|
2625
2613
|
const fen = this.convertFen(position);
|
|
2626
|
-
|
|
2627
|
-
this.game = new Chess(fen
|
|
2614
|
+
if (this.game) this.game.load(fen, options);
|
|
2615
|
+
else this.game = new Chess(fen);
|
|
2628
2616
|
}
|
|
2629
2617
|
|
|
2630
2618
|
// -------------------
|
|
@@ -2704,7 +2692,7 @@ var Chessboard = (function () {
|
|
|
2704
2692
|
|
|
2705
2693
|
}
|
|
2706
2694
|
|
|
2707
|
-
snapbackPiece(square, animate) {
|
|
2695
|
+
snapbackPiece(square, animate = this.config.snapbackAnimation) {
|
|
2708
2696
|
let move = new Move(square, square);
|
|
2709
2697
|
this.translatePiece(move, false, animate);
|
|
2710
2698
|
}
|
|
@@ -2715,11 +2703,15 @@ var Chessboard = (function () {
|
|
|
2715
2703
|
updateBoardPieces(animation = false) {
|
|
2716
2704
|
let { updatedFlags, escapeFlags, movableFlags, pendingTranslations } = this.prepareBoardUpdateData();
|
|
2717
2705
|
|
|
2706
|
+
let change = Object.values(updatedFlags).some(flag => !flag);
|
|
2707
|
+
|
|
2718
2708
|
this.identifyPieceTranslations(updatedFlags, escapeFlags, movableFlags, pendingTranslations);
|
|
2719
2709
|
|
|
2720
2710
|
this.executePieceTranslations(pendingTranslations, escapeFlags, animation);
|
|
2721
2711
|
|
|
2722
2712
|
this.processRemainingPieceUpdates(updatedFlags, animation);
|
|
2713
|
+
|
|
2714
|
+
if (change) this.config.onChange(this.fen());
|
|
2723
2715
|
}
|
|
2724
2716
|
|
|
2725
2717
|
prepareBoardUpdateData() {
|
|
@@ -2847,7 +2839,6 @@ var Chessboard = (function () {
|
|
|
2847
2839
|
event.preventDefault();
|
|
2848
2840
|
|
|
2849
2841
|
if (!this.config.draggable || !piece) return;
|
|
2850
|
-
if (!this.config.onDragStart(square, piece)) return;
|
|
2851
2842
|
|
|
2852
2843
|
let prec;
|
|
2853
2844
|
let from = square;
|
|
@@ -2857,7 +2848,7 @@ var Chessboard = (function () {
|
|
|
2857
2848
|
|
|
2858
2849
|
if (!this.canMove(from)) return;
|
|
2859
2850
|
if (!this.config.clickable) this.clicked = null;
|
|
2860
|
-
if (this.onClick(from)) return;
|
|
2851
|
+
if (this.onClick(from, true, true)) return;
|
|
2861
2852
|
|
|
2862
2853
|
img.style.position = 'absolute';
|
|
2863
2854
|
img.style.zIndex = 100;
|
|
@@ -2871,10 +2862,11 @@ var Chessboard = (function () {
|
|
|
2871
2862
|
};
|
|
2872
2863
|
|
|
2873
2864
|
const onMouseMove = (event) => {
|
|
2865
|
+
if (!this.config.onDragStart(square, piece)) return;
|
|
2874
2866
|
if (!moveAt(event.pageX, event.pageY)) ;
|
|
2875
2867
|
|
|
2876
|
-
const boardRect = this.
|
|
2877
|
-
const { offsetWidth: boardWidth, offsetHeight: boardHeight } = this.
|
|
2868
|
+
const boardRect = this.element.getBoundingClientRect();
|
|
2869
|
+
const { offsetWidth: boardWidth, offsetHeight: boardHeight } = this.element;
|
|
2878
2870
|
const x = event.clientX - boardRect.left;
|
|
2879
2871
|
const y = event.clientY - boardRect.top;
|
|
2880
2872
|
|
|
@@ -2909,9 +2901,9 @@ var Chessboard = (function () {
|
|
|
2909
2901
|
this.allSquares('removeHint');
|
|
2910
2902
|
from.deselect();
|
|
2911
2903
|
this.remove(from);
|
|
2912
|
-
} else if (!to || !this.onClick(to, true)) {
|
|
2913
|
-
this.snapbackPiece(from
|
|
2914
|
-
this.config.onSnapbackEnd(from, piece);
|
|
2904
|
+
} else if (!to || !this.onClick(to, true, true)) {
|
|
2905
|
+
this.snapbackPiece(from);
|
|
2906
|
+
if (to !== from) this.config.onSnapbackEnd(from, piece);
|
|
2915
2907
|
}
|
|
2916
2908
|
};
|
|
2917
2909
|
|
|
@@ -2938,14 +2930,14 @@ var Chessboard = (function () {
|
|
|
2938
2930
|
if (this.config.clickable && (!piece || this.config.onlyLegalMoves)) this.onClick(square);
|
|
2939
2931
|
};
|
|
2940
2932
|
|
|
2941
|
-
square.element.addEventListener("
|
|
2933
|
+
square.element.addEventListener("mousedown", handleClick);
|
|
2942
2934
|
square.element.addEventListener("touch", handleClick);
|
|
2943
2935
|
}
|
|
2944
2936
|
}
|
|
2945
2937
|
|
|
2946
|
-
onClick(square, animation = this.config.moveAnimation) {
|
|
2947
|
-
|
|
2948
|
-
if (
|
|
2938
|
+
onClick(square, animation = this.config.moveAnimation, dragged = false) {
|
|
2939
|
+
|
|
2940
|
+
if (this.clicked === square) return false;
|
|
2949
2941
|
|
|
2950
2942
|
let from = this.clicked;
|
|
2951
2943
|
this.clicked = null;
|
|
@@ -2964,19 +2956,22 @@ var Chessboard = (function () {
|
|
|
2964
2956
|
if (!from) {
|
|
2965
2957
|
|
|
2966
2958
|
if (this.canMove(square)) {
|
|
2967
|
-
|
|
2968
|
-
|
|
2959
|
+
if (this.config.clickable) {
|
|
2960
|
+
square.select();
|
|
2961
|
+
this.hintMoves(square);
|
|
2962
|
+
}
|
|
2969
2963
|
this.clicked = square;
|
|
2970
2964
|
}
|
|
2971
2965
|
|
|
2972
2966
|
return false;
|
|
2973
2967
|
}
|
|
2974
2968
|
|
|
2975
|
-
if (!this.canMove(from)) return false;
|
|
2976
|
-
|
|
2977
2969
|
let move = new Move(from, square, promotion);
|
|
2978
2970
|
|
|
2979
2971
|
move.from.deselect();
|
|
2972
|
+
|
|
2973
|
+
if (!move.check()) return false;
|
|
2974
|
+
|
|
2980
2975
|
this.allSquares("removeHint");
|
|
2981
2976
|
|
|
2982
2977
|
if (this.config.onlyLegalMoves && !move.isLegal(this.game)) return false;
|
|
@@ -3031,7 +3026,7 @@ var Chessboard = (function () {
|
|
|
3031
3026
|
return this.game.moves({ verbose: verb });
|
|
3032
3027
|
}
|
|
3033
3028
|
|
|
3034
|
-
move(move, animation) {
|
|
3029
|
+
move(move, animation = true) {
|
|
3035
3030
|
move = this.convertMove(move);
|
|
3036
3031
|
move.check();
|
|
3037
3032
|
|
|
@@ -3202,14 +3197,27 @@ var Chessboard = (function () {
|
|
|
3202
3197
|
}
|
|
3203
3198
|
|
|
3204
3199
|
setOrientation(color, animation = true) {
|
|
3205
|
-
if (
|
|
3206
|
-
this.config.orientation
|
|
3207
|
-
|
|
3200
|
+
if (['w', 'b'].includes(color)) {
|
|
3201
|
+
if (color !== this.config.orientation) {
|
|
3202
|
+
this.flip(animation);
|
|
3203
|
+
}
|
|
3208
3204
|
} else {
|
|
3209
3205
|
throw new Error(this.error_messages['invalid_orientation'] + color);
|
|
3210
3206
|
}
|
|
3211
3207
|
}
|
|
3212
3208
|
|
|
3209
|
+
highlight(squareId) {
|
|
3210
|
+
let square = this.convertSquare(squareId);
|
|
3211
|
+
square.check();
|
|
3212
|
+
square.highlight();
|
|
3213
|
+
}
|
|
3214
|
+
|
|
3215
|
+
dehighlight(squareId) {
|
|
3216
|
+
let square = this.convertSquare(squareId);
|
|
3217
|
+
square.check();
|
|
3218
|
+
square.dehighlight();
|
|
3219
|
+
}
|
|
3220
|
+
|
|
3213
3221
|
lastMove() {
|
|
3214
3222
|
const moves = this.history({ verbose: true });
|
|
3215
3223
|
return moves[moves.length - 1];
|
|
@@ -3223,7 +3231,7 @@ var Chessboard = (function () {
|
|
|
3223
3231
|
}
|
|
3224
3232
|
|
|
3225
3233
|
build() {
|
|
3226
|
-
if (this.
|
|
3234
|
+
if (this.element) this.destroy();
|
|
3227
3235
|
this.init();
|
|
3228
3236
|
}
|
|
3229
3237
|
|
|
@@ -3237,7 +3245,12 @@ var Chessboard = (function () {
|
|
|
3237
3245
|
}
|
|
3238
3246
|
|
|
3239
3247
|
board() {
|
|
3240
|
-
|
|
3248
|
+
let dict = {};
|
|
3249
|
+
for (let squareId in this.squares) {
|
|
3250
|
+
let piece = this.getGamePieceId(squareId);
|
|
3251
|
+
if (piece) dict[squareId] = piece;
|
|
3252
|
+
}
|
|
3253
|
+
return dict;
|
|
3241
3254
|
}
|
|
3242
3255
|
|
|
3243
3256
|
clear(options = {}, animation = true) {
|
|
@@ -3252,7 +3265,7 @@ var Chessboard = (function () {
|
|
|
3252
3265
|
get(squareId) {
|
|
3253
3266
|
const square = this.convertSquare(squareId);
|
|
3254
3267
|
square.check();
|
|
3255
|
-
return square.piece;
|
|
3268
|
+
return square.piece;
|
|
3256
3269
|
}
|
|
3257
3270
|
|
|
3258
3271
|
getCastlingRights(color) {
|
|
@@ -3299,9 +3312,9 @@ var Chessboard = (function () {
|
|
|
3299
3312
|
return this.game.isThreefoldRepetition();
|
|
3300
3313
|
}
|
|
3301
3314
|
|
|
3302
|
-
load(
|
|
3315
|
+
load(position, options = {}, animation = true) {
|
|
3303
3316
|
this.clearSquares();
|
|
3304
|
-
this.
|
|
3317
|
+
this.setGame(position, options);
|
|
3305
3318
|
this.updateBoardPieces(animation);
|
|
3306
3319
|
}
|
|
3307
3320
|
|
|
@@ -3364,7 +3377,7 @@ var Chessboard = (function () {
|
|
|
3364
3377
|
setHeader(key, value) {
|
|
3365
3378
|
return this.game.setHeader(key, value);
|
|
3366
3379
|
}
|
|
3367
|
-
|
|
3380
|
+
|
|
3368
3381
|
squareColor(squareId) {
|
|
3369
3382
|
return this.game.squareColor(squareId);
|
|
3370
3383
|
}
|
|
@@ -3406,4 +3419,4 @@ var Chessboard = (function () {
|
|
|
3406
3419
|
|
|
3407
3420
|
})();
|
|
3408
3421
|
|
|
3409
|
-
window.Chessboard.Chessboard = Chessboard;
|
|
3422
|
+
window.Chessboard.Chessboard = Chessboard;
|
package/chessboard.js
CHANGED
|
@@ -172,7 +172,6 @@ class Chessboard {
|
|
|
172
172
|
|
|
173
173
|
setGame(position, options = undefined) {
|
|
174
174
|
const fen = this.convertFen(position);
|
|
175
|
-
console.log(fen);
|
|
176
175
|
if (this.game) this.game.load(fen, options);
|
|
177
176
|
else this.game = new Chess(fen);
|
|
178
177
|
}
|
|
@@ -786,7 +785,6 @@ class Chessboard {
|
|
|
786
785
|
}
|
|
787
786
|
|
|
788
787
|
flip() {
|
|
789
|
-
console.log(this.config.position);
|
|
790
788
|
this.config.orientation = this.config.orientation === 'w' ? 'b' : 'w';
|
|
791
789
|
this.destroy();
|
|
792
790
|
this.initParams();
|