-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchessground.ts
61 lines (57 loc) · 2.07 KB
/
chessground.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
import { Api, start } from './api';
import { Config, configure } from './config';
import { HeadlessState, State, defaults } from './state';
import { renderWrap } from './wrap';
import * as events from './events';
import { updateBounds } from './render';
import * as svg from './svg';
import * as util from './util';
export function Chessground(element: HTMLElement, config?: Config): Api {
const maybeState: State | HeadlessState = defaults();
configure(maybeState, config || {});
function redrawAll(): State {
const prevUnbind = 'dom' in maybeState ? maybeState.dom.unbind : undefined;
// compute bounds from existing board element if possible
// this allows non-square boards from CSS to be handled (for 3D)
const relative = maybeState.viewOnly && !maybeState.drawable.visible,
elements = renderWrap(element, maybeState, relative),
bounds = util.memo(() => elements.board.getBoundingClientRect()),
redrawNow = (skipSvg?: boolean): void => {
maybeState.render(state);
if (!skipSvg && elements.svg) svg.renderSvg(state, elements.svg, elements.customSvg!);
},
boundsUpdated = (): void => {
bounds.clear();
updateBounds(state);
if (elements.svg) svg.renderSvg(state, elements.svg, elements.customSvg!);
};
const state = maybeState as State;
state.dom = {
elements,
bounds,
redraw: debounceRedraw(redrawNow),
redrawNow,
unbind: prevUnbind,
relative,
};
state.drawable.prevSvgHash = '';
redrawNow(false);
events.bindBoard(state, boundsUpdated);
if (!prevUnbind) state.dom.unbind = events.bindDocument(state, boundsUpdated);
state.events.insert && state.events.insert(elements);
return state;
}
return start(redrawAll(), redrawAll);
}
function debounceRedraw(redrawNow: (skipSvg?: boolean) => void): () => void {
let redrawing = false;
return () => {
if (redrawing) return;
redrawing = true;
requestAnimationFrame(() => {
redrawNow();
redrawing = false;
});
};
}
(window as any).Chessground = Chessground; // esbuild