-
-
Notifications
You must be signed in to change notification settings - Fork 276
/
Copy pathapi.ts
187 lines (144 loc) · 4.99 KB
/
api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { State } from './state.js';
import * as board from './board.js';
import { write as fenWrite } from './fen.js';
import { Config, configure, applyAnimation } from './config.js';
import { anim, render } from './anim.js';
import { cancel as dragCancel, dragNewPiece } from './drag.js';
import { DrawShape } from './draw.js';
import { explosion } from './explosion.js';
import * as cg from './types.js';
export interface Api {
// reconfigure the instance. Accepts all config options, except for viewOnly & drawable.visible.
// board will be animated accordingly, if animations are enabled.
set(config: Config): void;
// read chessground state; write at your own risks.
state: State;
// get the position as a FEN string (only contains pieces, no flags)
// e.g. rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR
getFen(): cg.FEN;
// change the view angle
toggleOrientation(): void;
// perform a move programmatically
move(orig: cg.Key, dest: cg.Key): void;
// add and/or remove arbitrary pieces on the board
setPieces(pieces: cg.PiecesDiff): void;
// click a square programmatically
selectSquare(key: cg.Key | null, force?: boolean): void;
// put a new piece on the board
newPiece(piece: cg.Piece, key: cg.Key): void;
// play the current premove, if any; returns true if premove was played
playPremove(): boolean;
// cancel the current premove, if any
cancelPremove(): void;
// play the current predrop, if any; returns true if premove was played
playPredrop(validate: (drop: cg.Drop) => boolean): boolean;
// cancel the current predrop, if any
cancelPredrop(): void;
// cancel the current move being made
cancelMove(): void;
// cancel current move and prevent further ones
stop(): void;
// make squares explode (atomic chess)
explode(keys: cg.Key[]): void;
// programmatically draw user shapes
setShapes(shapes: DrawShape[]): void;
// programmatically draw auto shapes
setAutoShapes(shapes: DrawShape[]): void;
// square name at this DOM position (like "e4")
getKeyAtDomPos(pos: cg.NumberPair): cg.Key | undefined;
// only useful when CSS changes the board width/height ratio (for 3D)
redrawAll: cg.Redraw;
// for crazyhouse and board editors
dragNewPiece(piece: cg.Piece, event: cg.MouchEvent, force?: boolean): void;
// unbinds all events
// (important for document-wide events like scroll and mousemove)
destroy: cg.Unbind;
}
// see API types and documentations in dts/api.d.ts
export function start(state: State, redrawAll: cg.Redraw): Api {
function toggleOrientation(): void {
board.toggleOrientation(state);
redrawAll();
}
return {
set(config): void {
if (config.orientation && config.orientation !== state.orientation) toggleOrientation();
applyAnimation(state, config);
(config.fen ? anim : render)(state => configure(state, config), state);
},
state,
getFen: () => fenWrite(state.pieces),
toggleOrientation,
setPieces(pieces): void {
anim(state => board.setPieces(state, pieces), state);
},
selectSquare(key, force): void {
if (key) anim(state => board.selectSquare(state, key, force), state);
else if (state.selected) {
board.unselect(state);
state.dom.redraw();
}
},
move(orig, dest): void {
anim(state => board.baseMove(state, orig, dest), state);
},
newPiece(piece, key): void {
anim(state => board.baseNewPiece(state, piece, key), state);
},
playPremove(): boolean {
if (state.premovable.current) {
if (anim(board.playPremove, state)) return true;
// if the premove couldn't be played, redraw to clear it up
state.dom.redraw();
}
return false;
},
playPredrop(validate): boolean {
if (state.predroppable.current) {
const result = board.playPredrop(state, validate);
state.dom.redraw();
return result;
}
return false;
},
cancelPremove(): void {
render(board.unsetPremove, state);
},
cancelPredrop(): void {
render(board.unsetPredrop, state);
},
cancelMove(): void {
render(state => {
board.cancelMove(state);
dragCancel(state);
}, state);
},
stop(): void {
render(state => {
board.stop(state);
dragCancel(state);
}, state);
},
explode(keys: cg.Key[]): void {
explosion(state, keys);
},
setAutoShapes(shapes: DrawShape[]): void {
render(state => (state.drawable.autoShapes = shapes), state);
},
setShapes(shapes: DrawShape[]): void {
render(state => (state.drawable.shapes = shapes), state);
},
getKeyAtDomPos(pos): cg.Key | undefined {
return board.getKeyAtDomPos(pos, board.whitePov(state), state.dom.bounds());
},
redrawAll,
dragNewPiece(piece, event, force): void {
dragNewPiece(state, piece, event, force);
},
destroy(): void {
board.stop(state);
state.dom.unbind && state.dom.unbind();
state.dom.destroyed = true;
},
};
}