UNPKG

chessground12

Version:
71 lines 2.98 kB
import { setCheck, setSelected } from './board'; import { setPredropDests, readPockets } from './pocket'; import { dimensions, } from './types'; export function applyAnimation(state, config) { if (config.animation) { deepMerge(state.animation, config.animation); // no need for such short animations if ((state.animation.duration || 0) < 70) state.animation.enabled = false; } } export function configure(state, config) { // don't merge destinations and autoShapes. Just override. if (config.movable?.dests) state.movable.dests = undefined; if (config.dropmode?.dropDests) state.dropmode.dropDests = undefined; if (config.drawable?.autoShapes) state.drawable.autoShapes = []; deepMerge(state, config); if (config.geometry) state.dimensions = dimensions[config.geometry]; // if a fen was provided, replace the pieces if (config.fen) { const pieces = new Map(); // prevent calling cancel() if piece drag is already started from pocket! const draggedPiece = state.pieces.get('a0'); if (draggedPiece !== undefined) pieces.set('a0', draggedPiece); state.pieces = pieces; state.drawable.shapes = []; if (state.pocketRoles) { state.pockets = readPockets(config.fen, state.pocketRoles); } } // apply config values that could be undefined yet meaningful if ('check' in config) setCheck(state, config.check || false); if ('lastMove' in config && !config.lastMove) state.lastMove = undefined; // in case of ZH drop last move, there's a single square. // if the previous last move had two squares, // the merge algorithm will incorrectly keep the second square. else if (config.lastMove) state.lastMove = config.lastMove; // fix move/premove dests if (state.selected) setSelected(state, state.selected); setPredropDests(state); // TODO: integrate pocket with the "selected" infrastructure and move this in setSelected() applyAnimation(state, config); if (!state.movable.rookCastle && state.movable.dests) { const rank = state.movable.color === 'white' ? '1' : '8', kingStartPos = ('e' + rank), dests = state.movable.dests.get(kingStartPos), king = state.pieces.get(kingStartPos); if (!dests || !king || king.role !== 'k-piece') return; state.movable.dests.set(kingStartPos, dests.filter(d => !(d === 'a' + rank && dests.includes(('c' + rank))) && !(d === 'h' + rank && dests.includes(('g' + rank))))); } } // eslint-disable-next-line function deepMerge(base, extend) { for (const key in extend) { if (isObject(base[key]) && isObject(extend[key])) deepMerge(base[key], extend[key]); else base[key] = extend[key]; } } function isObject(o) { return typeof o === 'object'; } //# sourceMappingURL=config.js.map