@alepot55/chessboardjs 2.2.1 → 2.3.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.
Files changed (74) hide show
  1. package/README.md +125 -401
  2. package/assets/themes/alepot/theme.json +42 -0
  3. package/assets/themes/default/theme.json +42 -0
  4. package/dist/chessboard.cjs.js +11785 -0
  5. package/dist/chessboard.css +243 -0
  6. package/dist/chessboard.esm.js +11716 -0
  7. package/dist/chessboard.iife.js +11791 -0
  8. package/dist/chessboard.umd.js +11791 -0
  9. package/package.json +33 -3
  10. package/{chessboard.move.js → src/components/Move.js} +3 -3
  11. package/src/components/Piece.js +771 -0
  12. package/{chessboard.square.js → src/components/Square.js} +61 -8
  13. package/src/constants/index.js +15 -0
  14. package/src/constants/positions.js +62 -0
  15. package/src/core/Chessboard.js +2346 -0
  16. package/src/core/ChessboardConfig.js +707 -0
  17. package/src/core/ChessboardFactory.js +385 -0
  18. package/src/core/index.js +141 -0
  19. package/src/errors/ChessboardError.js +133 -0
  20. package/src/errors/index.js +15 -0
  21. package/src/errors/messages.js +189 -0
  22. package/src/index.js +103 -0
  23. package/src/services/AnimationService.js +180 -0
  24. package/src/services/BoardService.js +156 -0
  25. package/src/services/CoordinateService.js +355 -0
  26. package/src/services/EventService.js +955 -0
  27. package/src/services/MoveService.js +567 -0
  28. package/src/services/PieceService.js +339 -0
  29. package/src/services/PositionService.js +237 -0
  30. package/src/services/ValidationService.js +673 -0
  31. package/src/services/index.js +14 -0
  32. package/src/styles/animations.css +46 -0
  33. package/{chessboard.css → src/styles/board.css} +30 -7
  34. package/src/styles/index.css +4 -0
  35. package/src/styles/pieces.css +70 -0
  36. package/src/utils/animations.js +37 -0
  37. package/{chess.js → src/utils/chess.js} +16 -16
  38. package/src/utils/coordinates.js +62 -0
  39. package/src/utils/cross-browser.js +150 -0
  40. package/src/utils/logger.js +422 -0
  41. package/src/utils/performance.js +311 -0
  42. package/src/utils/validation.js +458 -0
  43. package/.babelrc +0 -4
  44. package/chessboard.bundle.js +0 -3422
  45. package/chessboard.config.js +0 -147
  46. package/chessboard.js +0 -979
  47. package/chessboard.piece.js +0 -115
  48. package/jest.config.js +0 -7
  49. package/rollup.config.js +0 -11
  50. package/test/chessboard.test.js +0 -128
  51. /package/{alepot_theme → assets/themes/alepot}/bb.svg +0 -0
  52. /package/{alepot_theme → assets/themes/alepot}/bw.svg +0 -0
  53. /package/{alepot_theme → assets/themes/alepot}/kb.svg +0 -0
  54. /package/{alepot_theme → assets/themes/alepot}/kw.svg +0 -0
  55. /package/{alepot_theme → assets/themes/alepot}/nb.svg +0 -0
  56. /package/{alepot_theme → assets/themes/alepot}/nw.svg +0 -0
  57. /package/{alepot_theme → assets/themes/alepot}/pb.svg +0 -0
  58. /package/{alepot_theme → assets/themes/alepot}/pw.svg +0 -0
  59. /package/{alepot_theme → assets/themes/alepot}/qb.svg +0 -0
  60. /package/{alepot_theme → assets/themes/alepot}/qw.svg +0 -0
  61. /package/{alepot_theme → assets/themes/alepot}/rb.svg +0 -0
  62. /package/{alepot_theme → assets/themes/alepot}/rw.svg +0 -0
  63. /package/{default_pieces → assets/themes/default}/bb.svg +0 -0
  64. /package/{default_pieces → assets/themes/default}/bw.svg +0 -0
  65. /package/{default_pieces → assets/themes/default}/kb.svg +0 -0
  66. /package/{default_pieces → assets/themes/default}/kw.svg +0 -0
  67. /package/{default_pieces → assets/themes/default}/nb.svg +0 -0
  68. /package/{default_pieces → assets/themes/default}/nw.svg +0 -0
  69. /package/{default_pieces → assets/themes/default}/pb.svg +0 -0
  70. /package/{default_pieces → assets/themes/default}/pw.svg +0 -0
  71. /package/{default_pieces → assets/themes/default}/qb.svg +0 -0
  72. /package/{default_pieces → assets/themes/default}/qw.svg +0 -0
  73. /package/{default_pieces → assets/themes/default}/rb.svg +0 -0
  74. /package/{default_pieces → assets/themes/default}/rw.svg +0 -0
@@ -1,4 +1,4 @@
1
- import Piece from "./chessboard.piece.js";
1
+ import Piece from "./Piece.js";
2
2
 
3
3
  class Square {
4
4
 
@@ -54,11 +54,51 @@ class Square {
54
54
  return this.element.getBoundingClientRect();
55
55
  }
56
56
 
57
- removePiece() {
58
- this.element.removeChild(this.piece.element);
59
- const piece = this.piece;
57
+ removePiece(preserve = false) {
58
+ if (!this.piece) {
59
+ return null;
60
+ }
61
+ // Only destroy the piece if not preserving (i.e., not moving)
62
+ if (!preserve && typeof this.piece.destroy === 'function') {
63
+ this.piece.destroy();
64
+ }
60
65
  this.piece = null;
61
- return piece;
66
+ return null;
67
+ }
68
+
69
+ /**
70
+ * Forcefully removes all pieces from this square
71
+ */
72
+ forceRemoveAllPieces() {
73
+ // Best practice: destroy the piece object if present
74
+ if (this.piece && typeof this.piece.destroy === 'function') {
75
+ this.piece.destroy();
76
+ this.piece = null;
77
+ }
78
+ // Remove any orphaned img.piece elements from the DOM
79
+ const pieceElements = this.element.querySelectorAll('img.piece');
80
+ pieceElements.forEach(element => {
81
+ if (element.parentNode === this.element) {
82
+ this.element.removeChild(element);
83
+ }
84
+ });
85
+ }
86
+
87
+ /**
88
+ * Replaces the current piece with a new one efficiently
89
+ * @param {Piece} newPiece - The new piece to place
90
+ */
91
+ replacePiece(newPiece) {
92
+ // If there's an existing piece, remove it first
93
+ if (this.piece) {
94
+ this.removePiece();
95
+ }
96
+
97
+ // Add the new piece
98
+ this.putPiece(newPiece);
99
+
100
+ // Ensure the piece is properly displayed
101
+ newPiece.element.style.opacity = '1';
62
102
  }
63
103
 
64
104
  addEventListener(event, callback) {
@@ -66,8 +106,14 @@ class Square {
66
106
  }
67
107
 
68
108
  putPiece(piece) {
109
+ // If there's already a piece, destroy it to avoid orphaned DOM elements
110
+ if (this.piece) {
111
+ this.removePiece(false);
112
+ }
69
113
  this.piece = piece;
70
- this.element.appendChild(piece.element);
114
+ if (piece && piece.element) {
115
+ this.element.appendChild(piece.element);
116
+ }
71
117
  }
72
118
 
73
119
  putHint(catchable) {
@@ -150,6 +196,10 @@ class Square {
150
196
 
151
197
  }
152
198
 
199
+ hasPromotion() {
200
+ return this.element.querySelector('.choice') !== null;
201
+ }
202
+
153
203
  removePromotion() {
154
204
  let choice = this.element.querySelector('.choice');
155
205
  if (choice) {
@@ -159,7 +209,10 @@ class Square {
159
209
  }
160
210
 
161
211
  destroy() {
212
+ // Remove all piece DOM nodes and clear reference
213
+ this.forceRemoveAllPieces();
162
214
  this.element.remove();
215
+ this.piece = null;
163
216
  }
164
217
 
165
218
  hasPiece() {
@@ -167,7 +220,7 @@ class Square {
167
220
  }
168
221
 
169
222
  getColor() {
170
- return this.piece.getColor();
223
+ return this.piece ? this.piece.getColor() : null;
171
224
  }
172
225
 
173
226
  check() {
@@ -177,7 +230,7 @@ class Square {
177
230
  if (this.col < 1 || this.col > 8) {
178
231
  throw new Error("Invalid square: col is out of bounds");
179
232
  }
180
-
233
+
181
234
  }
182
235
  }
183
236
 
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Constants module exports
3
+ * @module constants
4
+ * @since 2.0.0
5
+ */
6
+
7
+ export {
8
+ STANDARD_POSITIONS,
9
+ DEFAULT_STARTING_POSITION,
10
+ PIECE_TYPES,
11
+ PIECE_COLORS,
12
+ PROMOTION_PIECES,
13
+ BOARD_LETTERS,
14
+ BOARD_SIZE
15
+ } from './positions.js';
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Standard chess positions and FEN constants
3
+ * @module constants/positions
4
+ * @since 2.0.0
5
+ */
6
+
7
+ /**
8
+ * Standard chess positions used throughout the application
9
+ * @type {Object.<string, string>}
10
+ * @readonly
11
+ */
12
+ export const STANDARD_POSITIONS = {
13
+ 'start': 'start',
14
+ 'default': 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1',
15
+ 'empty': '8/8/8/8/8/8/8/8 w - - 0 1'
16
+ };
17
+
18
+ /**
19
+ * Default starting position FEN string
20
+ * @type {string}
21
+ * @readonly
22
+ */
23
+ export const DEFAULT_STARTING_POSITION = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1';
24
+
25
+ /**
26
+ * Chess piece types
27
+ * @type {string[]}
28
+ * @readonly
29
+ */
30
+ export const PIECE_TYPES = ['p', 'r', 'n', 'b', 'q', 'k'];
31
+
32
+ /**
33
+ * Chess piece colors
34
+ * @type {string[]}
35
+ * @readonly
36
+ */
37
+ export const PIECE_COLORS = ['w', 'b'];
38
+
39
+ /**
40
+ * Valid promotion pieces
41
+ * @type {string[]}
42
+ * @readonly
43
+ */
44
+ export const PROMOTION_PIECES = ['q', 'r', 'b', 'n'];
45
+
46
+ /**
47
+ * Board coordinates letters
48
+ * @type {string}
49
+ * @readonly
50
+ */
51
+ export const BOARD_LETTERS = 'abcdefgh';
52
+
53
+ /**
54
+ * Board size constants
55
+ * @type {Object}
56
+ * @readonly
57
+ */
58
+ export const BOARD_SIZE = {
59
+ ROWS: 8,
60
+ COLS: 8,
61
+ TOTAL_SQUARES: 64
62
+ };