@alepot55/chessboardjs 2.3.6 → 2.3.8

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.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Service for managing chess pieces and their operations
2
+ * PieceService - Handles chess piece management and operations
3
3
  * @module services/PieceService
4
4
  * @since 2.0.0
5
5
  */
@@ -11,11 +11,11 @@ import { PIECE_TYPES, PIECE_COLORS } from '../constants/positions.js';
11
11
 
12
12
  /**
13
13
  * Service responsible for piece management and operations
14
- * @class
14
+ * @class PieceService
15
15
  */
16
16
  export class PieceService {
17
17
  /**
18
- * Creates a new PieceService instance
18
+ * Create a new PieceService instance
19
19
  * @param {ChessboardConfig} config - Board configuration
20
20
  */
21
21
  constructor(config) {
@@ -23,14 +23,13 @@ export class PieceService {
23
23
  }
24
24
 
25
25
  /**
26
- * Gets the path to a piece asset
26
+ * Get the path to a piece asset
27
27
  * @param {string} piece - Piece identifier (e.g., 'wK', 'bP')
28
28
  * @returns {string} Path to piece asset
29
29
  * @throws {ValidationError} When piecesPath configuration is invalid
30
30
  */
31
31
  getPiecePath(piece) {
32
32
  const { piecesPath } = this.config;
33
-
34
33
  if (typeof piecesPath === 'string') {
35
34
  return `${piecesPath}/${piece}.svg`;
36
35
  } else if (typeof piecesPath === 'object' && piecesPath !== null) {
@@ -43,7 +42,7 @@ export class PieceService {
43
42
  }
44
43
 
45
44
  /**
46
- * Converts various piece formats to a Piece instance
45
+ * Convert various piece formats to a Piece instance
47
46
  * @param {string|Piece} piece - Piece in various formats
48
47
  * @returns {Piece} Piece instance
49
48
  * @throws {PieceError} When piece format is invalid
@@ -52,54 +51,41 @@ export class PieceService {
52
51
  if (piece instanceof Piece) {
53
52
  return piece;
54
53
  }
55
-
56
54
  if (typeof piece === 'string' && piece.length === 2) {
57
55
  const [first, second] = piece.split('');
58
56
  let type, color;
59
-
60
- // Check format: [type][color] (e.g., 'pw')
61
57
  if (PIECE_TYPES.includes(first.toLowerCase()) && PIECE_COLORS.includes(second)) {
62
58
  type = first.toLowerCase();
63
59
  color = second;
64
- }
65
- // Check format: [color][type] (e.g., 'wP')
66
- else if (PIECE_COLORS.includes(first) && PIECE_TYPES.includes(second.toLowerCase())) {
60
+ } else if (PIECE_COLORS.includes(first) && PIECE_TYPES.includes(second.toLowerCase())) {
67
61
  color = first;
68
62
  type = second.toLowerCase();
69
63
  } else {
70
64
  throw new PieceError(ERROR_MESSAGES.invalid_piece + piece, piece);
71
65
  }
72
-
73
66
  const piecePath = this.getPiecePath(type + color);
74
67
  return new Piece(color, type, piecePath);
75
68
  }
76
-
77
69
  throw new PieceError(ERROR_MESSAGES.invalid_piece + piece, piece);
78
70
  }
79
71
 
80
72
  /**
81
- * Adds a piece to a square with optional fade-in animation
82
- * @param {Square} square - Target square (oggetto)
83
- * @param {Piece} piece - Piece to add (oggetto)
73
+ * Add a piece to a square with optional fade-in animation
74
+ * @param {Square} square - Target square
75
+ * @param {Piece} piece - Piece to add
84
76
  * @param {boolean} [fade=true] - Whether to fade in the piece
85
77
  * @param {Function} dragFunction - Function to handle drag events
86
78
  * @param {Function} [callback] - Callback when animation completes
87
79
  */
88
80
  addPieceOnSquare(square, piece, fade = true, dragFunction, callback) {
89
- if (!square || !piece) throw new Error('addPieceOnSquare richiede oggetti Square e Piece');
90
- console.debug(`[PieceService] addPieceOnSquare: ${piece.id} to ${square.id}`);
81
+ if (!square || !piece) throw new Error('addPieceOnSquare requires Square and Piece objects');
91
82
  square.putPiece(piece);
92
-
93
- // Imposta sempre il drag (touch e mouse)
94
83
  if (dragFunction) {
95
84
  piece.setDrag(dragFunction(square, piece));
96
85
  }
97
- // Forza il drag touch se manca (debug/robustezza)
98
86
  if (!piece.element.ontouchstart) {
99
87
  piece.element.ontouchstart = dragFunction ? dragFunction(square, piece) : () => { };
100
- console.debug(`[PieceService] Forzato ontouchstart su ${piece.id}`);
101
88
  }
102
-
103
89
  if (fade && this.config.fadeTime > 0) {
104
90
  piece.fadeIn(
105
91
  this.config.fadeTime,
@@ -110,28 +96,24 @@ export class PieceService {
110
96
  } else {
111
97
  if (callback) callback();
112
98
  }
113
-
114
99
  piece.visible();
115
100
  }
116
101
 
117
102
  /**
118
- * Rimuove un pezzo da una casella
119
- * @param {Square} square - Oggetto Square
103
+ * Remove a piece from a square
104
+ * @param {Square} square - Square object
120
105
  * @param {boolean} [fade=true]
121
106
  * @param {Function} [callback]
122
- * @returns {Piece} Il pezzo rimosso
107
+ * @returns {Piece} The removed piece
123
108
  */
124
109
  removePieceFromSquare(square, fade = true, callback) {
125
- if (!square) throw new Error('removePieceFromSquare richiede oggetto Square');
126
- console.debug(`[PieceService] removePieceFromSquare: ${square.id}`);
110
+ if (!square) throw new Error('removePieceFromSquare requires a Square object');
127
111
  square.check();
128
-
129
112
  const piece = square.piece;
130
113
  if (!piece) {
131
114
  if (callback) callback();
132
115
  throw new PieceError(ERROR_MESSAGES.square_no_piece, null, square.getId());
133
116
  }
134
-
135
117
  if (fade && this.config.fadeTime > 0) {
136
118
  piece.fadeOut(
137
119
  this.config.fadeTime,
@@ -142,26 +124,22 @@ export class PieceService {
142
124
  } else {
143
125
  if (callback) callback();
144
126
  }
145
-
146
127
  square.removePiece();
147
128
  return piece;
148
129
  }
149
130
 
150
131
  /**
151
- * Moves a piece to a new position with animation
132
+ * Move a piece to a new position with animation
152
133
  * @param {Piece} piece - Piece to move
153
134
  * @param {Square} targetSquare - Target square
154
135
  * @param {number} duration - Animation duration
155
136
  * @param {Function} [callback] - Callback function when animation completes
156
137
  */
157
138
  movePiece(piece, targetSquare, duration, callback) {
158
- console.debug(`[PieceService] movePiece: ${piece.id} to ${targetSquare.id}`);
159
139
  if (!piece || !piece.element) {
160
- console.warn(`[PieceService] movePiece: piece or element is null, skipping animation`);
161
140
  if (callback) callback();
162
141
  return;
163
142
  }
164
-
165
143
  piece.translate(
166
144
  targetSquare,
167
145
  duration,
@@ -172,7 +150,7 @@ export class PieceService {
172
150
  }
173
151
 
174
152
  /**
175
- * Handles piece translation with optional capture
153
+ * Handle piece translation with optional capture
176
154
  * @param {Move} move - Move object containing from/to squares and piece
177
155
  * @param {boolean} removeTarget - Whether to remove piece from target square
178
156
  * @param {boolean} animate - Whether to animate the move
@@ -180,54 +158,37 @@ export class PieceService {
180
158
  * @param {Function} [callback] - Callback function when complete
181
159
  */
182
160
  translatePiece(move, removeTarget, animate, dragFunction = null, callback = null) {
183
- console.debug(`[PieceService] translatePiece: ${move.piece.id} from ${move.from.id} to ${move.to.id}`);
184
161
  if (!move.piece) {
185
- console.warn('PieceService.translatePiece: move.piece is null, skipping translation');
186
162
  if (callback) callback();
187
163
  return;
188
164
  }
189
-
190
165
  if (removeTarget) {
191
- // Deselect the captured piece before removing it
192
166
  move.to.deselect();
193
167
  this.removePieceFromSquare(move.to, false);
194
168
  }
195
-
196
169
  const changeSquareCallback = () => {
197
- // Check if piece still exists and is on the source square
198
170
  if (move.from.piece === move.piece) {
199
- move.from.removePiece(true); // Preserve the piece when moving
171
+ move.from.removePiece(true);
200
172
  }
201
-
202
- // Only put piece if destination square doesn't already have it
203
173
  if (move.to.piece !== move.piece) {
204
174
  move.to.putPiece(move.piece);
205
-
206
- // Re-attach drag handler if provided
207
175
  if (dragFunction && this.config.draggable && move.piece.element) {
208
176
  move.piece.setDrag(dragFunction(move.to, move.piece));
209
177
  }
210
178
  }
211
-
212
179
  if (callback) callback();
213
180
  };
214
-
215
- // Check if piece is currently being dragged
216
181
  const isDragging = move.piece.element && move.piece.element.classList.contains('dragging');
217
-
218
182
  if (isDragging) {
219
- // If piece is being dragged, don't animate - just move it immediately
220
- // The piece is already visually in the correct position from the drag
221
183
  changeSquareCallback();
222
184
  } else {
223
- // Normal animation
224
185
  const duration = animate ? this.config.moveTime : 0;
225
186
  this.movePiece(move.piece, move.to, duration, changeSquareCallback);
226
187
  }
227
188
  }
228
189
 
229
190
  /**
230
- * Snaps a piece back to its original position
191
+ * Snap a piece back to its original position
231
192
  * @param {Square} square - Square containing the piece
232
193
  * @param {boolean} [animate=true] - Whether to animate the snapback
233
194
  */
@@ -237,7 +198,6 @@ export class PieceService {
237
198
  }
238
199
  const piece = square.piece;
239
200
  const duration = animate ? this.config.snapbackTime : 0;
240
- console.debug(`[PieceService] snapbackPiece: ${piece.id} on ${square.id}`);
241
201
  piece.translate(
242
202
  square,
243
203
  duration,
@@ -247,7 +207,7 @@ export class PieceService {
247
207
  }
248
208
 
249
209
  /**
250
- * Centers a piece in its square with animation (after successful drop)
210
+ * Center a piece in its square with animation (after successful drop)
251
211
  * @param {Square} square - Square containing the piece to center
252
212
  * @param {boolean} animate - Whether to animate the centering
253
213
  */
@@ -257,14 +217,12 @@ export class PieceService {
257
217
  }
258
218
  const piece = square.piece;
259
219
  const duration = animate ? this.config.dropCenterTime : 0;
260
- console.debug(`[PieceService] centerPiece: ${piece.id} on ${square.id}`);
261
220
  piece.translate(
262
221
  square,
263
222
  duration,
264
223
  this._getTransitionTimingFunction(),
265
224
  this.config.dropCenterAnimation,
266
225
  () => {
267
- // After animation, reset all drag-related styles
268
226
  if (!piece.element) return;
269
227
  piece.element.style.position = '';
270
228
  piece.element.style.left = '';
@@ -276,14 +234,13 @@ export class PieceService {
276
234
  }
277
235
 
278
236
  /**
279
- * Gets the transition timing function for animations
237
+ * Get the transition timing function for animations
280
238
  * @private
281
239
  * @returns {Function} Timing function
282
240
  */
283
241
  _getTransitionTimingFunction() {
284
242
  return (elapsed, duration, type = 'ease') => {
285
243
  const x = elapsed / duration;
286
-
287
244
  switch (type) {
288
245
  case 'linear':
289
246
  return x;
@@ -302,7 +259,7 @@ export class PieceService {
302
259
  }
303
260
 
304
261
  /**
305
- * Cleans up resources
262
+ * Clean up resources
306
263
  */
307
264
  destroy() {
308
265
  // Cleanup any cached pieces or references
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Service for managing chess positions and FEN conversion
2
+ * PositionService - Handles chess position management and FEN conversion
3
3
  * @module services/PositionService
4
4
  * @since 2.0.0
5
5
  */
@@ -11,11 +11,11 @@ import { ERROR_MESSAGES } from '../errors/messages.js';
11
11
 
12
12
  /**
13
13
  * Service responsible for position management and FEN operations
14
- * @class
14
+ * @class PositionService
15
15
  */
16
16
  export class PositionService {
17
17
  /**
18
- * Creates a new PositionService instance
18
+ * Create a new PositionService instance
19
19
  * @param {ChessboardConfig} config - Board configuration
20
20
  */
21
21
  constructor(config) {
@@ -24,7 +24,7 @@ export class PositionService {
24
24
  }
25
25
 
26
26
  /**
27
- * Converts various position formats to FEN string
27
+ * Convert various position formats to FEN string
28
28
  * @param {string|Object} position - Position in various formats
29
29
  * @returns {string} FEN string representation
30
30
  * @throws {ValidationError} When position format is invalid
@@ -40,7 +40,7 @@ export class PositionService {
40
40
  }
41
41
 
42
42
  /**
43
- * Converts string position to FEN
43
+ * Convert string position to FEN
44
44
  * @private
45
45
  * @param {string} position - String position
46
46
  * @returns {string} FEN string
@@ -49,35 +49,29 @@ export class PositionService {
49
49
  if (position === 'start') {
50
50
  return DEFAULT_STARTING_POSITION;
51
51
  }
52
-
53
52
  if (this.validateFen(position)) {
54
53
  return position;
55
54
  }
56
-
57
55
  if (STANDARD_POSITIONS[position]) {
58
56
  return STANDARD_POSITIONS[position];
59
57
  }
60
-
61
58
  throw new ValidationError(ERROR_MESSAGES.invalid_position + position, 'position', position);
62
59
  }
63
60
 
64
61
  /**
65
- * Converts object position to FEN
62
+ * Convert object position to FEN
66
63
  * @private
67
64
  * @param {Object} position - Object with square->piece mapping
68
65
  * @returns {string} FEN string
69
66
  */
70
67
  _convertObjectPosition(position) {
71
68
  const parts = [];
72
-
73
69
  for (let row = 0; row < 8; row++) {
74
70
  const rowParts = [];
75
71
  let empty = 0;
76
-
77
72
  for (let col = 0; col < 8; col++) {
78
73
  const square = this._getSquareID(row, col);
79
74
  const piece = position[square];
80
-
81
75
  if (piece) {
82
76
  if (empty > 0) {
83
77
  rowParts.push(empty);
@@ -90,25 +84,21 @@ export class PositionService {
90
84
  empty++;
91
85
  }
92
86
  }
93
-
94
87
  if (empty > 0) {
95
88
  rowParts.push(empty);
96
89
  }
97
-
98
90
  parts.push(rowParts.join(''));
99
91
  }
100
-
101
92
  return parts.join('/') + ' w KQkq - 0 1';
102
93
  }
103
94
 
104
95
  /**
105
- * Sets up the game with the given position
96
+ * Set up the game with the given position
106
97
  * @param {string|Object} position - Position to set
107
98
  * @param {Object} [options] - Additional options for game setup
108
99
  */
109
100
  setGame(position, options = {}) {
110
101
  const fen = this.convertFen(position);
111
-
112
102
  if (this.game) {
113
103
  this.game.load(fen, options);
114
104
  } else {
@@ -117,7 +107,7 @@ export class PositionService {
117
107
  }
118
108
 
119
109
  /**
120
- * Gets the current game instance
110
+ * Get the current game instance
121
111
  * @returns {Chess} Current chess.js game instance
122
112
  */
123
113
  getGame() {
@@ -125,7 +115,7 @@ export class PositionService {
125
115
  }
126
116
 
127
117
  /**
128
- * Validates a FEN string
118
+ * Validate a FEN string
129
119
  * @param {string} fen - FEN string to validate
130
120
  * @returns {boolean} True if valid, false otherwise
131
121
  */
@@ -134,7 +124,7 @@ export class PositionService {
134
124
  }
135
125
 
136
126
  /**
137
- * Gets piece information for a specific square
127
+ * Get piece information for a specific square
138
128
  * @param {string} squareId - Square identifier
139
129
  * @returns {string|null} Piece ID or null if no piece
140
130
  */
@@ -145,7 +135,7 @@ export class PositionService {
145
135
  }
146
136
 
147
137
  /**
148
- * Checks if a specific piece is on a specific square
138
+ * Check if a specific piece is on a specific square
149
139
  * @param {string} piece - Piece ID to check
150
140
  * @param {string} square - Square to check
151
141
  * @returns {boolean} True if piece is on square
@@ -155,7 +145,7 @@ export class PositionService {
155
145
  }
156
146
 
157
147
  /**
158
- * Converts board coordinates to square ID
148
+ * Convert board coordinates to square ID
159
149
  * @private
160
150
  * @param {number} row - Row index (0-7)
161
151
  * @param {number} col - Column index (0-7)
@@ -164,7 +154,6 @@ export class PositionService {
164
154
  _getSquareID(row, col) {
165
155
  row = parseInt(row);
166
156
  col = parseInt(col);
167
-
168
157
  if (this.config.orientation === 'w') {
169
158
  row = 8 - row;
170
159
  col = col + 1;
@@ -172,13 +161,12 @@ export class PositionService {
172
161
  row = row + 1;
173
162
  col = 8 - col;
174
163
  }
175
-
176
164
  const letter = BOARD_LETTERS[col - 1];
177
165
  return letter + row;
178
166
  }
179
167
 
180
168
  /**
181
- * Changes the turn in a FEN string
169
+ * Change the turn in a FEN string
182
170
  * @param {string} fen - Original FEN string
183
171
  * @param {string} color - New turn color ('w' or 'b')
184
172
  * @returns {string} Modified FEN string
@@ -190,35 +178,33 @@ export class PositionService {
190
178
  }
191
179
 
192
180
  /**
193
- * Gets the current position as an object
181
+ * Get the current position as an object
194
182
  * @returns {Object} Position object with piece placements
195
183
  */
196
184
  getPosition() {
197
185
  const position = {};
198
186
  const game = this.getGame();
199
-
200
- // Convert chess.js board to position object
201
- const squares = ['a1', 'b1', 'c1', 'd1', 'e1', 'f1', 'g1', 'h1',
202
- 'a2', 'b2', 'c2', 'd2', 'e2', 'f2', 'g2', 'h2',
203
- 'a3', 'b3', 'c3', 'd3', 'e3', 'f3', 'g3', 'h3',
204
- 'a4', 'b4', 'c4', 'd4', 'e4', 'f4', 'g4', 'h4',
205
- 'a5', 'b5', 'c5', 'd5', 'e5', 'f5', 'g5', 'h5',
206
- 'a6', 'b6', 'c6', 'd6', 'e6', 'f6', 'g6', 'h6',
207
- 'a7', 'b7', 'c7', 'd7', 'e7', 'f7', 'g7', 'h7',
208
- 'a8', 'b8', 'c8', 'd8', 'e8', 'f8', 'g8', 'h8'];
209
-
187
+ const squares = [
188
+ 'a1', 'b1', 'c1', 'd1', 'e1', 'f1', 'g1', 'h1',
189
+ 'a2', 'b2', 'c2', 'd2', 'e2', 'f2', 'g2', 'h2',
190
+ 'a3', 'b3', 'c3', 'd3', 'e3', 'f3', 'g3', 'h3',
191
+ 'a4', 'b4', 'c4', 'd4', 'e4', 'f4', 'g4', 'h4',
192
+ 'a5', 'b5', 'c5', 'd5', 'e5', 'f5', 'g5', 'h5',
193
+ 'a6', 'b6', 'c6', 'd6', 'e6', 'f6', 'g6', 'h6',
194
+ 'a7', 'b7', 'c7', 'd7', 'e7', 'f7', 'g7', 'h7',
195
+ 'a8', 'b8', 'c8', 'd8', 'e8', 'f8', 'g8', 'h8'
196
+ ];
210
197
  for (const square of squares) {
211
198
  const piece = game.get(square);
212
199
  if (piece) {
213
200
  position[square] = piece.type + piece.color;
214
201
  }
215
202
  }
216
-
217
203
  return position;
218
204
  }
219
205
 
220
206
  /**
221
- * Toggles the turn in a FEN string
207
+ * Toggle the turn in a FEN string
222
208
  * @param {string} fen - Original FEN string
223
209
  * @returns {string} Modified FEN string
224
210
  */
@@ -229,7 +215,7 @@ export class PositionService {
229
215
  }
230
216
 
231
217
  /**
232
- * Cleans up resources
218
+ * Clean up resources
233
219
  */
234
220
  destroy() {
235
221
  this.game = null;