UNPKG

shogiground

Version:
74 lines 3.29 kB
import { start } from './api.js'; import { configure } from './config.js'; import { defaults } from './state.js'; import * as util from './util.js'; import { redrawAll } from './dom.js'; import { bindDocument } from './events.js'; import { redrawNow, redrawShapesNow } from './redraw.js'; export function Shogiground(config, wrapElements) { const state = defaults(); configure(state, config || {}); const redrawStateNow = (skipShapes) => { redrawNow(state, skipShapes); }; state.dom = { wrapElements: wrapElements || {}, elements: {}, bounds: { board: { bounds: util.memo(() => { var _a; return (_a = state.dom.elements.board) === null || _a === void 0 ? void 0 : _a.pieces.getBoundingClientRect(); }), }, hands: { bounds: util.memo(() => { const handsRects = new Map(), handEls = state.dom.elements.hands; if (handEls === null || handEls === void 0 ? void 0 : handEls.top) handsRects.set('top', handEls.top.getBoundingClientRect()); if (handEls === null || handEls === void 0 ? void 0 : handEls.bottom) handsRects.set('bottom', handEls.bottom.getBoundingClientRect()); return handsRects; }), pieceBounds: util.memo(() => { const handPiecesRects = new Map(), handEls = state.dom.elements.hands; if (handEls === null || handEls === void 0 ? void 0 : handEls.top) { let wrapEl = handEls.top.firstElementChild; while (wrapEl) { const pieceEl = wrapEl.firstElementChild, piece = { role: pieceEl.sgRole, color: pieceEl.sgColor }; handPiecesRects.set(util.pieceNameOf(piece), pieceEl.getBoundingClientRect()); wrapEl = wrapEl.nextElementSibling; } } if (handEls === null || handEls === void 0 ? void 0 : handEls.bottom) { let wrapEl = handEls.bottom.firstElementChild; while (wrapEl) { const pieceEl = wrapEl.firstElementChild, piece = { role: pieceEl.sgRole, color: pieceEl.sgColor }; handPiecesRects.set(util.pieceNameOf(piece), pieceEl.getBoundingClientRect()); wrapEl = wrapEl.nextElementSibling; } } return handPiecesRects; }), }, }, redrawNow: redrawStateNow, redraw: debounceRedraw(redrawStateNow), redrawShapes: debounceRedraw(() => redrawShapesNow(state)), unbind: bindDocument(state), destroyed: false, }; if (wrapElements) redrawAll(wrapElements, state); return start(state); } function debounceRedraw(f) { let redrawing = false; return (...args) => { if (redrawing) return; redrawing = true; requestAnimationFrame(() => { f(...args); redrawing = false; }); }; } //# sourceMappingURL=shogiground.js.map