qwerty084 / vue3-chessboard

Vue 3 chessboard component, built with lichess chessground and chess.js
https://qwerty084.github.io/vue3-chessboard-docs/
GNU General Public License v3.0
70 stars 18 forks source link

When using movable free, the internal board state is not kept up to date #231

Open jonatanpettersson opened 12 months ago

jonatanpettersson commented 12 months ago

Describe the bug When moving pieces freely on the board (using movable = {free: true}, the internal position is not kept up to date. This has the effect that if you then use api.putPiece(), the position will jump back to the last known position (before pieces were freely moved), before adding the piece. The combination of moving pieces freely and then using putPiece() is core for making a board editor, so not a far-fetched example I think.

Which version are you using 1.2.1

To Reproduce Steps to reproduce the behavior:

  1. Enable free moving with movable={free: true}
  2. Move a few pieces on the board
  3. Use api.putPiece(somePiece, someSquare).
  4. The position on the board jumps back to the start position, and then adds the piece.

Additional context It seems that for example getFen() returns the state of this.game.fen(), so without having looked deeper it seems that free-moving pieces are not reflected in the this.chess state, and only on this.board.

Workaround Since the board state is of course correct (it's what's actually showing in the UI), you can override the internal state in the change event like this:

boardConfig.events = {
      change: () => {
        api.setPosition(
          api.board.getFen() + " w - - 0 1" // Accessing the private board like this is a bit nasty of course
        );
      },
    };
qwerty084 commented 11 months ago

Thanks for the bug report. Has been fixed partially in v1.3.2. The position is no longer being reset, but there are still issues. For example setting a piece multiple times on the same square is not reflected in chess.js. This seems like an upstream issue.

Its really tough to get movable: free working with chess.js, since it allows no illegal moves/positions.