shogiground
Version:
lishogi.org shogi ui
144 lines • 5.72 kB
JavaScript
import * as board from './board.js';
import { addToHand, removeFromHand } from './hands.js';
import { inferDimensions, boardToSfen, handsToSfen } from './sfen.js';
import { applyAnimation, configure } from './config.js';
import { anim, render } from './anim.js';
import { cancel as dragCancel, dragNewPiece } from './drag.js';
import { detachElements, redrawAll } from './dom.js';
// see API types and documentations in api.d.ts
export function start(state) {
return {
attach(wrapElements) {
redrawAll(wrapElements, state);
},
detach(wrapElementsBoolean) {
detachElements(wrapElementsBoolean, state);
},
set(config, skipAnimation) {
var _a, _b, _c, _d;
function getByPath(path, obj) {
const properties = path.split('.');
return properties.reduce((prev, curr) => prev && prev[curr], obj);
}
const forceRedrawProps = [
'orientation',
'viewOnly',
'coordinates.enabled',
'coordinates.notation',
'drawable.visible',
'hands.inlined',
];
const newDims = ((_a = config.sfen) === null || _a === void 0 ? void 0 : _a.board) && inferDimensions(config.sfen.board);
const toRedraw = forceRedrawProps.some((p) => {
const cRes = getByPath(p, config);
return cRes && cRes !== getByPath(p, state);
}) ||
!!(newDims &&
(newDims.files !== state.dimensions.files || newDims.ranks !== state.dimensions.ranks)) ||
!!((_c = (_b = config.hands) === null || _b === void 0 ? void 0 : _b.roles) === null || _c === void 0 ? void 0 : _c.every((r, i) => r === state.hands.roles[i]));
if (toRedraw) {
board.reset(state);
configure(state, config);
redrawAll(state.dom.wrapElements, state);
}
else {
applyAnimation(state, config);
(((_d = config.sfen) === null || _d === void 0 ? void 0 : _d.board) && !skipAnimation ? anim : render)((state) => configure(state, config), state);
}
},
state,
getBoardSfen: () => boardToSfen(state.pieces, state.dimensions, state.forsyth.toForsyth),
getHandsSfen: () => handsToSfen(state.hands.handMap, state.hands.roles, state.forsyth.toForsyth),
toggleOrientation() {
board.toggleOrientation(state);
redrawAll(state.dom.wrapElements, state);
},
move(orig, dest, prom) {
anim((state) => board.baseMove(state, orig, dest, prom || state.promotion.forceMovePromotion(orig, dest)), state);
},
drop(piece, key, prom, spare) {
anim((state) => {
state.droppable.spare = !!spare;
board.baseDrop(state, piece, key, prom || state.promotion.forceDropPromotion(piece, key));
}, state);
},
setPieces(pieces) {
anim((state) => board.setPieces(state, pieces), state);
},
addToHand(piece, count) {
render((state) => addToHand(state, piece, count), state);
},
removeFromHand(piece, count) {
render((state) => removeFromHand(state, piece, count), state);
},
selectSquare(key, prom, force) {
if (key)
anim((state) => board.selectSquare(state, key, prom, force), state);
else if (state.selected) {
board.unselect(state);
state.dom.redraw();
}
},
selectPiece(piece, spare, force) {
if (piece)
render((state) => board.selectPiece(state, piece, spare, force, true), state);
else if (state.selectedPiece) {
board.unselect(state);
state.dom.redraw();
}
},
playPremove() {
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() {
if (state.predroppable.current) {
if (anim(board.playPredrop, state))
return true;
// if the predrop couldn't be played, redraw to clear it up
state.dom.redraw();
}
return false;
},
cancelPremove() {
render(board.unsetPremove, state);
},
cancelPredrop() {
render(board.unsetPredrop, state);
},
cancelMoveOrDrop() {
render((state) => {
board.cancelMoveOrDrop(state);
dragCancel(state);
}, state);
},
stop() {
render((state) => {
board.stop(state);
}, state);
},
setAutoShapes(shapes) {
render((state) => (state.drawable.autoShapes = shapes), state);
},
setShapes(shapes) {
render((state) => (state.drawable.shapes = shapes), state);
},
setSquareHighlights(squares) {
render((state) => (state.drawable.squares = squares), state);
},
dragNewPiece(piece, event, spare) {
dragNewPiece(state, piece, event, spare);
},
destroy() {
board.stop(state);
state.dom.unbind();
state.dom.destroyed = true;
},
};
}
//# sourceMappingURL=api.js.map