shogiground
Version:
lishogi.org shogi ui
70 lines • 3.18 kB
JavaScript
import { setChecks, setPreDests } from './board.js';
import { inferDimensions, sfenToBoard, sfenToHands } from './sfen.js';
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) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
// don't merge, just override.
if ((_a = config.movable) === null || _a === void 0 ? void 0 : _a.dests)
state.movable.dests = undefined;
if ((_b = config.droppable) === null || _b === void 0 ? void 0 : _b.dests)
state.droppable.dests = undefined;
if ((_c = config.drawable) === null || _c === void 0 ? void 0 : _c.shapes)
state.drawable.shapes = [];
if ((_d = config.drawable) === null || _d === void 0 ? void 0 : _d.autoShapes)
state.drawable.autoShapes = [];
if ((_e = config.drawable) === null || _e === void 0 ? void 0 : _e.squares)
state.drawable.squares = [];
if ((_f = config.hands) === null || _f === void 0 ? void 0 : _f.roles)
state.hands.roles = [];
deepMerge(state, config);
// if a sfen was provided, replace the pieces, except the currently dragged one
if ((_g = config.sfen) === null || _g === void 0 ? void 0 : _g.board) {
state.dimensions = inferDimensions(config.sfen.board);
state.pieces = sfenToBoard(config.sfen.board, state.dimensions, state.forsyth.fromForsyth);
state.drawable.shapes = ((_h = config.drawable) === null || _h === void 0 ? void 0 : _h.shapes) || [];
}
if ((_j = config.sfen) === null || _j === void 0 ? void 0 : _j.hands) {
state.hands.handMap = sfenToHands(config.sfen.hands, state.forsyth.fromForsyth);
}
// apply config values that could be undefined yet meaningful
if ('checks' in config)
setChecks(state, config.checks || false);
if ('lastPiece' in config && !config.lastPiece)
state.lastPiece = undefined;
// in case of 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.
if ('lastDests' in config && !config.lastDests)
state.lastDests = undefined;
else if (config.lastDests)
state.lastDests = config.lastDests;
// fix move/premove dests
setPreDests(state);
applyAnimation(state, config);
}
function deepMerge(base, extend) {
for (const key in extend) {
if (Object.prototype.hasOwnProperty.call(extend, key)) {
if (Object.prototype.hasOwnProperty.call(base, key) &&
isPlainObject(base[key]) &&
isPlainObject(extend[key]))
deepMerge(base[key], extend[key]);
else
base[key] = extend[key];
}
}
}
function isPlainObject(o) {
if (typeof o !== 'object' || o === null)
return false;
const proto = Object.getPrototypeOf(o);
return proto === Object.prototype || proto === null;
}
//# sourceMappingURL=config.js.map