UNPKG

@lichess-org/chessground

Version:
238 lines 10.1 kB
import { key2pos, createEl, posToTranslate as posToTranslateFromBounds, translate, setVisible, } from './util.js'; import * as util from './util.js'; import { whitePov } from './board.js'; // ported from https://github.com/lichess-org/lichobile/blob/master/src/chessground/render.ts // in case of bugs, blame @veloce export function render(s) { const asWhite = whitePov(s), posToTranslate = posToTranslateFromBounds(s.dom.bounds()), boardEl = s.dom.elements.board, pieces = s.pieces, curAnim = s.animation.current, anims = curAnim ? curAnim.plan.anims : new Map(), fadings = curAnim ? curAnim.plan.fadings : new Map(), curDrag = s.draggable.current, samePieces = new Set(), movedPieces = new Map(), desiredSquares = computeSquareClasses(s), availableSquares = new Map(); // by class name let k, el, pieceAtKey, elPieceName, anim, fading, pMvdset, pMvd, sAvail; // walk over all board dom elements, apply animations and flag moved pieces el = boardEl.firstChild; while (el) { k = el.cgKey; if (isPieceNode(el)) { pieceAtKey = pieces.get(k); anim = anims.get(k); fading = fadings.get(k); elPieceName = el.cgPiece; // if piece not being dragged anymore, remove dragging style if (el.cgDragging && (!curDrag || curDrag.orig !== k)) { el.classList.remove('dragging'); translate(el, posToTranslate(key2pos(k), asWhite)); el.cgDragging = false; } // remove fading class if it still remains if (!fading && el.cgFading) { el.cgFading = false; el.classList.remove('fading'); } // there is now a piece at this dom key if (pieceAtKey) { // continue animation if already animating and same piece // (otherwise it could animate a captured piece) if (anim && el.cgAnimating && elPieceName === pieceNameOf(pieceAtKey)) { const pos = key2pos(k); pos[0] += anim[2]; pos[1] += anim[3]; el.classList.add('anim'); translate(el, posToTranslate(pos, asWhite)); } else if (el.cgAnimating) { el.cgAnimating = false; el.classList.remove('anim'); translate(el, posToTranslate(key2pos(k), asWhite)); if (s.addPieceZIndex) el.style.zIndex = posZIndex(key2pos(k), asWhite); } // same piece: flag as same if (elPieceName === pieceNameOf(pieceAtKey) && (!fading || !el.cgFading)) samePieces.add(k); // different piece: flag as moved unless it is a fading piece else if (fading && elPieceName === pieceNameOf(fading)) { el.classList.add('fading'); el.cgFading = true; } else appendValue(movedPieces, elPieceName, el); } // no piece: flag as moved else appendValue(movedPieces, elPieceName, el); } else if (isSquareNode(el)) { const cls = el.className; if (desiredSquares.get(k) === cls) { setVisible(el, true); desiredSquares.delete(k); } else appendValue(availableSquares, cls, el); } el = el.nextSibling; } // walk over all squares in current set, apply dom changes to moved squares // or append new squares for (const [sk, className] of desiredSquares) { sAvail = availableSquares.get(className)?.pop(); const translation = posToTranslate(key2pos(sk), asWhite); if (sAvail) { // repurpose an available square sAvail.cgKey = sk; if (s.jsHover) sAvail.dataset['key'] = sk; translate(sAvail, translation); setVisible(sAvail, true); } else { const squareNode = createEl('square', className); squareNode.cgKey = sk; if (s.jsHover) squareNode.dataset['key'] = sk; translate(squareNode, translation); boardEl.insertBefore(squareNode, boardEl.firstChild); } } // hide remaining available, therefore unused, squares for (const [, nodes] of availableSquares.entries()) { for (const node of nodes) setVisible(node, false); } // walk over all pieces in current set, apply dom changes to moved pieces // or append new pieces for (const [k, p] of pieces) { anim = anims.get(k); if (!samePieces.has(k)) { pMvdset = movedPieces.get(pieceNameOf(p)); pMvd = pMvdset && pMvdset.pop(); // a same piece was moved if (pMvd) { // apply dom changes pMvd.cgKey = k; if (pMvd.cgFading) { pMvd.classList.remove('fading'); pMvd.cgFading = false; } const pos = key2pos(k); if (s.addPieceZIndex) pMvd.style.zIndex = posZIndex(pos, asWhite); if (anim) { pMvd.cgAnimating = true; pMvd.classList.add('anim'); pos[0] += anim[2]; pos[1] += anim[3]; } translate(pMvd, posToTranslate(pos, asWhite)); } // no piece in moved obj: insert the new piece // assumes the new piece is not being dragged else { const pieceName = pieceNameOf(p), pieceNode = createEl('piece', pieceName), pos = key2pos(k); pieceNode.cgPiece = pieceName; pieceNode.cgKey = k; if (anim) { pieceNode.cgAnimating = true; pos[0] += anim[2]; pos[1] += anim[3]; } translate(pieceNode, posToTranslate(pos, asWhite)); if (s.addPieceZIndex) pieceNode.style.zIndex = posZIndex(pos, asWhite); boardEl.appendChild(pieceNode); } } } // remove any piece that remains in the moved sets for (const nodes of movedPieces.values()) removeNodes(s, nodes); } export function renderResized(s) { const asWhite = whitePov(s), posToTranslate = posToTranslateFromBounds(s.dom.bounds()); let el = s.dom.elements.board.firstChild; while (el) { if ((isPieceNode(el) && !el.cgAnimating) || isSquareNode(el)) { translate(el, posToTranslate(key2pos(el.cgKey), asWhite)); } el = el.nextSibling; } } export function updateBounds(s) { const bounds = s.dom.elements.wrap.getBoundingClientRect(); const container = s.dom.elements.container; const ratio = bounds.height / bounds.width; const width = (Math.floor((bounds.width * window.devicePixelRatio) / 8) * 8) / window.devicePixelRatio; const height = width * ratio; container.style.width = width + 'px'; container.style.height = height + 'px'; s.dom.bounds.clear(); s.addDimensionsCssVarsTo?.style.setProperty('---cg-width', width + 'px'); s.addDimensionsCssVarsTo?.style.setProperty('---cg-height', height + 'px'); } const isPieceNode = (el) => el.tagName === 'PIECE'; const isSquareNode = (el) => el.tagName === 'SQUARE'; function removeNodes(s, nodes) { for (const node of nodes) s.dom.elements.board.removeChild(node); } function posZIndex(pos, asWhite) { const minZ = 3; const rank = pos[1]; const z = asWhite ? minZ + 7 - rank : minZ + rank; return `${z}`; } const pieceNameOf = (piece) => `${piece.color} ${piece.role}`; const normalizeLastMoveStandardRookCastle = (s, k) => !!s.lastMove?.[1] && !s.pieces.has(s.lastMove[1]) && s.lastMove[0][0] === 'e' && ['h', 'a'].includes(s.lastMove[1][0]) && s.lastMove[0][1] === s.lastMove[1][1] && util.squaresBetween(...key2pos(s.lastMove[0]), ...key2pos(s.lastMove[1])).some(sq => s.pieces.has(sq)) ? ((k > s.lastMove[0] ? 'g' : 'c') + k[1]) : k; function computeSquareClasses(s) { const squares = new Map(); if (s.lastMove && s.highlight.lastMove) for (const [i, k] of s.lastMove.entries()) addSquare(squares, i === 1 ? normalizeLastMoveStandardRookCastle(s, k) : k, 'last-move'); if (s.check && s.highlight.check) addSquare(squares, s.check, 'check'); if (s.selected) { addSquare(squares, s.selected, 'selected'); if (s.movable.showDests) { for (const k of s.movable.dests?.get(s.selected) ?? []) addSquare(squares, k, 'move-dest' + (s.pieces.has(k) ? ' oc' : '')); for (const k of s.premovable.customDests?.get(s.selected) ?? s.premovable.dests ?? []) addSquare(squares, k, 'premove-dest' + (s.pieces.has(k) ? ' oc' : '')); } } const premove = s.premovable.current; if (premove) for (const k of premove) addSquare(squares, k, 'current-premove'); else if (s.predroppable.current) addSquare(squares, s.predroppable.current.key, 'current-premove'); const o = s.exploding; if (o) for (const k of o.keys) addSquare(squares, k, 'exploding' + o.stage); if (s.highlight.custom) { s.highlight.custom.forEach((v, k) => { addSquare(squares, k, v); }); } return squares; } function addSquare(squares, key, klass) { const classes = squares.get(key); if (classes) squares.set(key, `${classes} ${klass}`); else squares.set(key, klass); } function appendValue(map, key, value) { const arr = map.get(key); if (arr) arr.push(value); else map.set(key, [value]); } //# sourceMappingURL=render.js.map