@alepot55/chessboardjs 2.2.0 → 2.2.2
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/.eslintrc.json +227 -0
- package/README.md +127 -403
- package/assets/themes/alepot/theme.json +42 -0
- package/assets/themes/default/theme.json +42 -0
- package/chessboard.bundle.js +782 -119
- package/config/jest.config.js +15 -0
- package/config/rollup.config.js +36 -0
- package/dist/chessboard.cjs.js +10690 -0
- package/dist/chessboard.css +228 -0
- package/dist/chessboard.esm.js +10621 -0
- package/dist/chessboard.iife.js +10696 -0
- package/dist/chessboard.umd.js +10696 -0
- package/jest.config.js +2 -7
- package/package.json +18 -3
- package/rollup.config.js +2 -11
- package/{chessboard.move.js → src/components/Move.js} +3 -3
- package/src/components/Piece.js +288 -0
- package/{chessboard.square.js → src/components/Square.js} +60 -7
- package/src/constants/index.js +15 -0
- package/src/constants/positions.js +62 -0
- package/src/core/Chessboard.js +1939 -0
- package/src/core/ChessboardConfig.js +458 -0
- package/src/core/ChessboardFactory.js +385 -0
- package/src/core/index.js +141 -0
- package/src/errors/ChessboardError.js +133 -0
- package/src/errors/index.js +15 -0
- package/src/errors/messages.js +189 -0
- package/src/index.js +103 -0
- package/src/services/AnimationService.js +180 -0
- package/src/services/BoardService.js +156 -0
- package/src/services/CoordinateService.js +355 -0
- package/src/services/EventService.js +955 -0
- package/src/services/MoveService.js +629 -0
- package/src/services/PieceService.js +312 -0
- package/src/services/PositionService.js +237 -0
- package/src/services/ValidationService.js +673 -0
- package/src/services/index.js +14 -0
- package/src/styles/animations.css +46 -0
- package/{chessboard.css → src/styles/board.css} +8 -4
- package/src/styles/index.css +4 -0
- package/src/styles/pieces.css +70 -0
- package/src/utils/animations.js +37 -0
- package/{chess.js → src/utils/chess.js} +16 -16
- package/src/utils/coordinates.js +62 -0
- package/src/utils/cross-browser.js +150 -0
- package/src/utils/logger.js +422 -0
- package/src/utils/performance.js +311 -0
- package/src/utils/validation.js +458 -0
- package/tests/unit/chessboard-config-animations.test.js +106 -0
- package/tests/unit/chessboard-robust.test.js +163 -0
- package/tests/unit/chessboard.test.js +183 -0
- package/chessboard.config.js +0 -147
- package/chessboard.js +0 -981
- package/chessboard.piece.js +0 -115
- package/test/chessboard.test.js +0 -128
- /package/{alepot_theme → assets/themes/alepot}/bb.svg +0 -0
- /package/{alepot_theme → assets/themes/alepot}/bw.svg +0 -0
- /package/{alepot_theme → assets/themes/alepot}/kb.svg +0 -0
- /package/{alepot_theme → assets/themes/alepot}/kw.svg +0 -0
- /package/{alepot_theme → assets/themes/alepot}/nb.svg +0 -0
- /package/{alepot_theme → assets/themes/alepot}/nw.svg +0 -0
- /package/{alepot_theme → assets/themes/alepot}/pb.svg +0 -0
- /package/{alepot_theme → assets/themes/alepot}/pw.svg +0 -0
- /package/{alepot_theme → assets/themes/alepot}/qb.svg +0 -0
- /package/{alepot_theme → assets/themes/alepot}/qw.svg +0 -0
- /package/{alepot_theme → assets/themes/alepot}/rb.svg +0 -0
- /package/{alepot_theme → assets/themes/alepot}/rw.svg +0 -0
- /package/{default_pieces → assets/themes/default}/bb.svg +0 -0
- /package/{default_pieces → assets/themes/default}/bw.svg +0 -0
- /package/{default_pieces → assets/themes/default}/kb.svg +0 -0
- /package/{default_pieces → assets/themes/default}/kw.svg +0 -0
- /package/{default_pieces → assets/themes/default}/nb.svg +0 -0
- /package/{default_pieces → assets/themes/default}/nw.svg +0 -0
- /package/{default_pieces → assets/themes/default}/pb.svg +0 -0
- /package/{default_pieces → assets/themes/default}/pw.svg +0 -0
- /package/{default_pieces → assets/themes/default}/qb.svg +0 -0
- /package/{default_pieces → assets/themes/default}/qw.svg +0 -0
- /package/{default_pieces → assets/themes/default}/rb.svg +0 -0
- /package/{default_pieces → assets/themes/default}/rw.svg +0 -0
- /package/{.babelrc → config/.babelrc} +0 -0
package/chessboard.piece.js
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
class Piece {
|
|
2
|
-
constructor(color, type, src, opacity = 1) {
|
|
3
|
-
this.color = color;
|
|
4
|
-
this.type = type;
|
|
5
|
-
this.id = this.getId();
|
|
6
|
-
this.src = src;
|
|
7
|
-
this.element = this.createElement(src, opacity);
|
|
8
|
-
|
|
9
|
-
this.check();
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
getId() { return this.type + this.color }
|
|
13
|
-
|
|
14
|
-
createElement(opacity) {
|
|
15
|
-
let element = document.createElement("img");
|
|
16
|
-
element.classList.add("piece");
|
|
17
|
-
element.id = this.id;
|
|
18
|
-
element.src = this.src;
|
|
19
|
-
element.style.opacity = opacity;
|
|
20
|
-
return element;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
visible() { this.element.style.opacity = 1 }
|
|
24
|
-
|
|
25
|
-
invisible() { this.element.style.opacity = 0 }
|
|
26
|
-
|
|
27
|
-
fadeIn(duration, speed, transition_f) {
|
|
28
|
-
let start = performance.now();
|
|
29
|
-
let opacity = 0;
|
|
30
|
-
let piece = this;
|
|
31
|
-
let fade = function () {
|
|
32
|
-
let elapsed = performance.now() - start;
|
|
33
|
-
opacity = transition_f(elapsed, duration, speed);
|
|
34
|
-
piece.element.style.opacity = opacity;
|
|
35
|
-
if (elapsed < duration) {
|
|
36
|
-
requestAnimationFrame(fade);
|
|
37
|
-
} else {
|
|
38
|
-
piece.element.style.opacity = 1;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
fade();
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
fadeOut(duration, speed, transition_f) {
|
|
45
|
-
let start = performance.now();
|
|
46
|
-
let opacity = 1;
|
|
47
|
-
let piece = this;
|
|
48
|
-
let fade = function () {
|
|
49
|
-
let elapsed = performance.now() - start;
|
|
50
|
-
opacity = 1 - transition_f(elapsed, duration, speed);
|
|
51
|
-
piece.element.style.opacity = opacity;
|
|
52
|
-
if (elapsed < duration) {
|
|
53
|
-
requestAnimationFrame(fade);
|
|
54
|
-
} else {
|
|
55
|
-
piece.element.style.opacity = 0;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
setDrag(f) {
|
|
61
|
-
this.element.ondragstart = (e) => { e.preventDefault() };
|
|
62
|
-
this.element.onmousedown = f;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
destroy() {
|
|
66
|
-
this.element.remove();
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
translate(to, duration, transition_f, speed, callback = null) {
|
|
70
|
-
|
|
71
|
-
let sourceRect = this.element.getBoundingClientRect();
|
|
72
|
-
let targetRect = to.getBoundingClientRect();
|
|
73
|
-
let x_start = sourceRect.left + sourceRect.width / 2;
|
|
74
|
-
let y_start = sourceRect.top + sourceRect.height / 2;
|
|
75
|
-
let x_end = targetRect.left + targetRect.width / 2;
|
|
76
|
-
let y_end = targetRect.top + targetRect.height / 2;
|
|
77
|
-
let dx = x_end - x_start;
|
|
78
|
-
let dy = y_end - y_start;
|
|
79
|
-
|
|
80
|
-
let keyframes = [
|
|
81
|
-
{ transform: 'translate(0, 0)' },
|
|
82
|
-
{ transform: `translate(${dx}px, ${dy}px)` }
|
|
83
|
-
];
|
|
84
|
-
|
|
85
|
-
if (this.element.animate) {
|
|
86
|
-
let animation = this.element.animate(keyframes, {
|
|
87
|
-
duration: duration,
|
|
88
|
-
easing: 'ease',
|
|
89
|
-
fill: 'none'
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
animation.onfinish = () => {
|
|
93
|
-
if (callback) callback();
|
|
94
|
-
this.element.style = '';
|
|
95
|
-
};
|
|
96
|
-
} else {
|
|
97
|
-
this.element.style.transition = `transform ${duration}ms ease`;
|
|
98
|
-
this.element.style.transform = `translate(${dx}px, ${dy}px)`;
|
|
99
|
-
if (callback) callback();
|
|
100
|
-
this.element.style = '';
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
check() {
|
|
105
|
-
if (['p', 'r', 'n', 'b', 'q', 'k'].indexOf(this.type) === -1) {
|
|
106
|
-
throw new Error("Invalid piece type");
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
if (['w', 'b'].indexOf(this.color) === -1) {
|
|
110
|
-
throw new Error("Invalid piece color");
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
export default Piece;
|
package/test/chessboard.test.js
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import Chessboard from '../chessboard.js';
|
|
2
|
-
|
|
3
|
-
describe('Chessboard User Functions', () => {
|
|
4
|
-
let chessboard;
|
|
5
|
-
let config = { id_div: 'board', size: 400, position: 'start', orientation: 'w' };
|
|
6
|
-
|
|
7
|
-
beforeEach(() => {
|
|
8
|
-
document.body.innerHTML = '<div id="board"></div>';
|
|
9
|
-
chessboard = new Chessboard(config);
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
test('turn() should return the current turn', () => {
|
|
13
|
-
expect(chessboard.turn()).toBe('w');
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
test('getOrientation() should return the current orientation', () => {
|
|
17
|
-
expect(chessboard.getOrientation()).toBe('w');
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
test('fen() should return the current FEN string', () => {
|
|
21
|
-
expect(chessboard.fen()).toBe('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1');
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
test('lastMove() should return the last move made', () => {
|
|
25
|
-
chessboard.move('e2e4');
|
|
26
|
-
expect(chessboard.lastMove().san).toBe('e4');
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
test('history() should return the history of moves', () => {
|
|
30
|
-
chessboard.move('e2e4');
|
|
31
|
-
chessboard.move('e7e5');
|
|
32
|
-
expect(chessboard.history().length).toBe(2);
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
test('get() should return the piece on the given square', () => {
|
|
36
|
-
expect(chessboard.get('e2')).toBe('pw');
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test('position() should set the board to the given position', () => {
|
|
40
|
-
chessboard.position('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1');
|
|
41
|
-
expect(chessboard.fen()).toBe('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1');
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
test('flip() should flip the board orientation', () => {
|
|
45
|
-
chessboard.flip();
|
|
46
|
-
expect(chessboard.getOrientation()).toBe('b');
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
test('build() should rebuild the board', () => {
|
|
50
|
-
chessboard.build();
|
|
51
|
-
expect(chessboard.fen()).toBe('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1');
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
test('clear() should clear the board', () => {
|
|
55
|
-
chessboard.clear();
|
|
56
|
-
expect(chessboard.fen()).toBe('8/8/8/8/8/8/8/8 w - - 0 1');
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
test('insert() should insert a piece on the given square', () => {
|
|
60
|
-
chessboard.insert('e4', 'qw');
|
|
61
|
-
expect(chessboard.get('e4')).toBe('qw');
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
test('isGameOver() should return the game over status', () => {
|
|
65
|
-
chessboard.position('default');
|
|
66
|
-
chessboard.move('e2e4');
|
|
67
|
-
chessboard.move('e7e5');
|
|
68
|
-
chessboard.move('d1h5');
|
|
69
|
-
chessboard.move('b8c6');
|
|
70
|
-
chessboard.move('f1c4');
|
|
71
|
-
chessboard.move('g8f6');
|
|
72
|
-
chessboard.move('h5f7');
|
|
73
|
-
expect(chessboard.isGameOver()).toBe('w');
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
test('orientation() should set the board orientation', () => {
|
|
77
|
-
chessboard.orientation('b');
|
|
78
|
-
expect(chessboard.getOrientation()).toBe('b');
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
test('resize() should resize the board', () => {
|
|
82
|
-
chessboard.resize(500);
|
|
83
|
-
expect(document.documentElement.style.getPropertyValue('--dimBoard')).toBe('500px');
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
test('destroy() should destroy the board', () => {
|
|
87
|
-
chessboard.destroy();
|
|
88
|
-
expect(chessboard.board).toBeNull();
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
test('remove() should remove a piece from the given square', () => {
|
|
92
|
-
chessboard.remove('e2');
|
|
93
|
-
expect(chessboard.get('e2')).toBeNull();
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
test('piece() should return the piece on the given square', () => {
|
|
97
|
-
expect(chessboard.piece('e2')).toBe('pw');
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
test('highlight() should highlight the given square', () => {
|
|
101
|
-
chessboard.highlight('e2');
|
|
102
|
-
expect(chessboard.squares['e2'].element.classList.contains('highlighted')).toBe(true);
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
test('dehighlight() should dehighlight the given square', () => {
|
|
106
|
-
chessboard.highlight('e2');
|
|
107
|
-
chessboard.dehighlight('e2');
|
|
108
|
-
expect(chessboard.squares['e2'].element.classList.contains('highlighted')).toBe(false);
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
test('playerTurn() should return true if it is the player turn', () => {
|
|
112
|
-
expect(chessboard.playerTurn()).toBe(true);
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
test('isWhiteOriented() should return true if the board is oriented for white', () => {
|
|
116
|
-
expect(chessboard.isWhiteOriented()).toBe(true);
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
test('getSquareID() should return the correct square ID', () => {
|
|
120
|
-
expect(chessboard.getSquareID(0, 0)).toBe('a8');
|
|
121
|
-
expect(chessboard.getSquareID(7, 7)).toBe('h1');
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
test('removeSquares() should remove all squares from the board', () => {
|
|
125
|
-
chessboard.removeSquares();
|
|
126
|
-
expect(Object.keys(chessboard.squares).length).toBe(0);
|
|
127
|
-
});
|
|
128
|
-
});
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|