@publishvue/chessground
Version:
lichess.org chess ui
2 lines (1 loc) • 26.7 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).Chessground=t()}(this,(function(){"use strict";const e=["white","black"],t=["a","b","c","d","e","f","g","h"],o=["1","2","3","4","5","6","7","8"],n=[...o].reverse(),r=Array.prototype.concat(...t.map((e=>o.map((t=>e+t))))),i=e=>r[8*e[0]+e[1]],s=e=>[e.charCodeAt(0)-97,e.charCodeAt(1)-49],c=r.map(s);const a=()=>{let e;return{start(){e=performance.now()},cancel(){e=void 0},stop(){if(!e)return 0;const t=performance.now()-e;return e=void 0,t}}},l=e=>"white"===e?"black":"white",d=(e,t)=>{const o=e[0]-t[0],n=e[1]-t[1];return o*o+n*n},u=(e,t)=>e.role===t.role&&e.color===t.color,p=e=>(t,o)=>[(o?t[0]:7-t[0])*e.width/8,(o?7-t[1]:t[1])*e.height/8],f=(e,t)=>{e.style.transform=`translate(${t[0]}px,${t[1]}px)`},g=(e,t)=>{e.style.visibility=t?"visible":"hidden"},h=e=>{var t;return e.clientX||0===e.clientX?[e.clientX,e.clientY]:(null===(t=e.targetTouches)||void 0===t?void 0:t[0])?[e.targetTouches[0].clientX,e.targetTouches[0].clientY]:void 0},m=e=>2===e.buttons||2===e.button,v=(e,t)=>{const o=document.createElement(e);return t&&(o.className=t),o};function b(e,t,o){const n=s(e);return t||(n[0]=7-n[0],n[1]=7-n[1]),[o.left+o.width*n[0]/8+o.width/16,o.top+o.height*(7-n[1])/8+o.height/16]}function w(e,t){return Math.abs(e-t)}const y=(e,t,o,n)=>{const r=w(e,o),i=w(t,n);return 1===r&&2===i||2===r&&1===i},k=(e,t,o,n)=>w(e,o)===w(t,n),C=(e,t,o,n)=>e===o||t===n,M=(e,t,o,n)=>k(e,t,o,n)||C(e,t,o,n);function S(e,t,o){const n=e.get(t);if(!n)return[];const r=s(t),a=n.role,l="pawn"===a?(d=n.color,(e,t,o,n)=>w(e,o)<2&&("white"===d?n===t+1||t<=1&&n===t+2&&e===o:n===t-1||t>=6&&n===t-2&&e===o)):"knight"===a?y:"bishop"===a?k:"rook"===a?C:"queen"===a?M:function(e,t,o){return(n,r,i,s)=>w(n,i)<2&&w(r,s)<2||o&&r===s&&r===("white"===e?0:7)&&(4===n&&(2===i&&t.includes(0)||6===i&&t.includes(7))||t.includes(i))}(n.color,function(e,t){const o="white"===t?"1":"8",n=[];for(const[r,i]of e)r[1]===o&&i.color===t&&"rook"===i.role&&n.push(s(r)[0]);return n}(e,n.color),o);var d;return c.filter((e=>(r[0]!==e[0]||r[1]!==e[1])&&l(r[0],r[1],e[0],e[1]))).map(i)}function x(e,...t){e&&setTimeout((()=>e(...t)),1)}function P(e){e.premovable.current&&(e.premovable.current=void 0,x(e.premovable.events.unset))}function A(e){const t=e.predroppable;t.current&&(t.current=void 0,x(t.events.unset))}function K(e,t,o){const n=e.pieces.get(t),r=e.pieces.get(o);if(t===o||!n)return!1;const c=r&&r.color!==n.color?r:void 0;return o===e.selected&&$(e),x(e.events.move,t,o,c),function(e,t,o){if(!e.autoCastle)return!1;const n=e.pieces.get(t);if(!n||"king"!==n.role)return!1;const r=s(t),c=s(o);if(0!==r[1]&&7!==r[1]||r[1]!==c[1])return!1;4!==r[0]||e.pieces.has(o)||(6===c[0]?o=i([7,c[1]]):2===c[0]&&(o=i([0,c[1]])));const a=e.pieces.get(o);return!(!a||a.color!==n.color||"rook"!==a.role||(e.pieces.delete(t),e.pieces.delete(o),r[0]<c[0]?(e.pieces.set(i([6,c[1]]),n),e.pieces.set(i([5,c[1]]),a)):(e.pieces.set(i([2,c[1]]),n),e.pieces.set(i([3,c[1]]),a)),0))}(e,t,o)||(e.pieces.set(o,n),e.pieces.delete(t)),e.lastMove=[t,o],e.check=void 0,x(e.events.change),c||!0}function T(e,t,o,n){if(e.pieces.has(o)){if(!n)return!1;e.pieces.delete(o)}return x(e.events.dropNewPiece,t,o),e.pieces.set(o,t),e.lastMove=[o],e.check=void 0,x(e.events.change),e.movable.dests=void 0,e.turnColor=l(e.turnColor),!0}function q(e,t,o){const n=K(e,t,o);return n&&(e.movable.dests=void 0,e.turnColor=l(e.turnColor),e.animation.current=void 0),n}function L(e,t,o){if(W(e,t,o)){const n=q(e,t,o);if(n){const r=e.hold.stop();$(e);const i={premove:!1,ctrlKey:e.stats.ctrlKey,holdTime:r};return!0!==n&&(i.captured=n),x(e.movable.events.after,t,o,i),!0}}else if(function(e,t,o){return t!==o&&H(e,t)&&S(e.pieces,t,e.premovable.castle).includes(o)}(e,t,o))return function(e,t,o,n){A(e),e.premovable.current=[t,o],x(e.premovable.events.set,t,o,n)}(e,t,o,{ctrlKey:e.stats.ctrlKey}),$(e),!0;return $(e),!1}function N(e,t,o,n){const r=e.pieces.get(t);r&&(function(e,t,o){const n=e.pieces.get(t);return!(!n||t!==o&&e.pieces.has(o)||"both"!==e.movable.color&&(e.movable.color!==n.color||e.turnColor!==n.color))}(e,t,o)||n)?(e.pieces.delete(t),T(e,r,o,n),x(e.movable.events.afterNewPiece,r.role,o,{premove:!1,predrop:!1})):r&&function(e,t,o){const n=e.pieces.get(t),r=e.pieces.get(o);return!!n&&(!r||r.color!==e.movable.color)&&e.predroppable.enabled&&("pawn"!==n.role||"1"!==o[1]&&"8"!==o[1])&&e.movable.color===n.color&&e.turnColor!==n.color}(e,t,o)?function(e,t,o){P(e),e.predroppable.current={role:t,key:o},x(e.predroppable.events.set,t,o)}(e,r.role,o):(P(e),A(e)),e.pieces.delete(t),$(e)}function D(e,t,o){if(x(e.events.select,t),e.selected){if(e.selected===t&&!e.draggable.enabled)return $(e),void e.hold.cancel();if((e.selectable.enabled||o)&&e.selected!==t&&L(e,e.selected,t))return void(e.stats.dragged=!1)}(E(e,t)||H(e,t))&&(O(e,t),e.hold.start())}function O(e,t){e.selected=t,H(e,t)?e.premovable.dests=S(e.pieces,t,e.premovable.castle):e.premovable.dests=void 0}function $(e){e.selected=void 0,e.premovable.dests=void 0,e.hold.cancel()}function E(e,t){const o=e.pieces.get(t);return!!o&&("both"===e.movable.color||e.movable.color===o.color&&e.turnColor===o.color)}function W(e,t,o){var n,r;return t!==o&&E(e,t)&&(e.movable.free||!!(null===(r=null===(n=e.movable.dests)||void 0===n?void 0:n.get(t))||void 0===r?void 0:r.includes(o)))}function H(e,t){const o=e.pieces.get(t);return!!o&&e.premovable.enabled&&e.movable.color===o.color&&e.turnColor!==o.color}function R(e){const t=e.premovable.current;if(!t)return!1;const o=t[0],n=t[1];let r=!1;if(W(e,o,n)){const t=q(e,o,n);if(t){const i={premove:!0};!0!==t&&(i.captured=t),x(e.movable.events.after,o,n,i),r=!0}}return P(e),r}function B(e){P(e),A(e),$(e)}function F(e){e.movable.color=e.movable.dests=e.animation.current=void 0,B(e)}function z(e,t,o){let n=Math.floor(8*(e[0]-o.left)/o.width);t||(n=7-n);let r=7-Math.floor(8*(e[1]-o.top)/o.height);return t||(r=7-r),n>=0&&n<8&&r>=0&&r<8?i([n,r]):void 0}function I(e){return"white"===e.orientation}const j="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR",V={p:"pawn",r:"rook",n:"knight",b:"bishop",q:"queen",k:"king"},G={pawn:"p",rook:"r",knight:"n",bishop:"b",queen:"q",king:"k"};function U(e){"start"===e&&(e=j);const t=new Map;let o=7,n=0;for(const r of e)switch(r){case" ":return t;case"/":if(--o,o<0)return t;n=0;break;case"~":{const e=t.get(i([n,o]));e&&(e.promoted=!0);break}default:{const e=r.charCodeAt(0);if(e<57)n+=e-48;else{const e=r.toLowerCase();t.set(i([n,o]),{role:V[e],color:r===e?"black":"white"}),++n}}}return t}function X(e,t){t.animation&&(Z(e.animation,t.animation),(e.animation.duration||0)<70&&(e.animation.enabled=!1))}function Y(e,t){var o,n;if((null===(o=t.movable)||void 0===o?void 0:o.dests)&&(e.movable.dests=void 0),(null===(n=t.drawable)||void 0===n?void 0:n.autoShapes)&&(e.drawable.autoShapes=[]),Z(e,t),t.fen&&(e.pieces=U(t.fen),e.drawable.shapes=[]),"check"in t&&function(e,t){if(e.check=void 0,!0===t&&(t=e.turnColor),t)for(const[o,n]of e.pieces)"king"===n.role&&n.color===t&&(e.check=o)}(e,t.check||!1),"lastMove"in t&&!t.lastMove?e.lastMove=void 0:t.lastMove&&(e.lastMove=t.lastMove),e.selected&&O(e,e.selected),X(e,t),!e.movable.rookCastle&&e.movable.dests){const t="white"===e.movable.color?"1":"8",o="e"+t,n=e.movable.dests.get(o),r=e.pieces.get(o);if(!n||!r||"king"!==r.role)return;e.movable.dests.set(o,n.filter((e=>!(e==="a"+t&&n.includes("c"+t)||e==="h"+t&&n.includes("g"+t)))))}}function Z(e,t){for(const o in t)Q(e[o])&&Q(t[o])?Z(e[o],t[o]):e[o]=t[o]}function Q(e){return"object"==typeof e}function J(e,t){return t.animation.enabled?function(e,t){const o=new Map(t.pieces),n=e(t),i=function(e,t){const o=new Map,n=[],i=new Map,s=[],c=[],a=new Map;let l,d,p;for(const[t,o]of e)a.set(t,ee(t,o));for(const e of r)l=t.pieces.get(e),d=a.get(e),l?d?u(l,d.piece)||(s.push(d),c.push(ee(e,l))):c.push(ee(e,l)):d&&s.push(d);for(const e of c)d=te(e,s.filter((t=>u(e.piece,t.piece)))),d&&(p=[d.pos[0]-e.pos[0],d.pos[1]-e.pos[1]],o.set(e.key,p.concat(p)),n.push(d.key));for(const e of s)n.includes(e.key)||i.set(e.key,e.piece);return{anims:o,fadings:i}}(o,t);if(i.anims.size||i.fadings.size){const e=t.animation.current&&t.animation.current.start;t.animation.current={start:performance.now(),frequency:1/t.animation.duration,plan:i},e||oe(t,performance.now())}else t.dom.redraw();return n}(e,t):_(e,t)}function _(e,t){const o=e(t);return t.dom.redraw(),o}function ee(e,t){return{key:e,pos:s(e),piece:t}}function te(e,t){return t.sort(((t,o)=>d(e.pos,t.pos)-d(e.pos,o.pos)))[0]}function oe(e,t){const o=e.animation.current;if(void 0===o)return void(e.dom.destroyed||e.dom.redrawNow());const n=1-(t-o.start)*o.frequency;if(n<=0)e.animation.current=void 0,e.dom.redrawNow();else{const t=(r=n)<.5?4*r*r*r:(r-1)*(2*r-2)*(2*r-2)+1;for(const e of o.plan.anims.values())e[2]=e[0]*t,e[3]=e[1]*t;e.dom.redrawNow(!0),requestAnimationFrame(((t=performance.now())=>oe(e,t)))}var r}const ne=["green","red","blue","yellow"];function re(e,t){if(t.touches&&t.touches.length>1)return;t.stopPropagation(),t.preventDefault(),t.ctrlKey?$(e):B(e);const o=h(t),n=z(o,I(e),e.dom.bounds());n&&(e.drawable.current={orig:n,pos:o,brush:le(t),snapToValidMove:e.drawable.defaultSnapToValidMove},ie(e))}function ie(e){requestAnimationFrame((()=>{const t=e.drawable.current;if(t){const o=z(t.pos,I(e),e.dom.bounds());o||(t.snapToValidMove=!1);const n=t.snapToValidMove?function(e,t,o,n){const r=s(e),a=c.filter((e=>M(r[0],r[1],e[0],e[1])||y(r[0],r[1],e[0],e[1]))),l=a.map((e=>b(i(e),o,n))).map((e=>d(t,e))),[,u]=l.reduce(((e,t,o)=>e[0]<t?e:[t,o]),[l[0],0]);return i(a[u])}(t.orig,t.pos,I(e),e.dom.bounds()):o;n!==t.mouseSq&&(t.mouseSq=n,t.dest=n!==t.orig?n:void 0,e.dom.redrawNow()),ie(e)}}))}function se(e,t){e.drawable.current&&(e.drawable.current.pos=h(t))}function ce(e){const t=e.drawable.current;t&&(t.mouseSq&&function(e,t){const o=e=>e.orig===t.orig&&e.dest===t.dest,n=e.shapes.find(o);n&&(e.shapes=e.shapes.filter((e=>!o(e))));n&&n.brush===t.brush||e.shapes.push(t);de(e)}(e.drawable,t),ae(e))}function ae(e){e.drawable.current&&(e.drawable.current=void 0,e.dom.redraw())}function le(e){var t;const o=(e.shiftKey||e.ctrlKey)&&m(e),n=e.altKey||e.metaKey||(null===(t=e.getModifierState)||void 0===t?void 0:t.call(e,"AltGraph"));return ne[(o?1:0)+(n?2:0)]}function de(e){e.onChange&&e.onChange(e.shapes)}function ue(e,t){if(!t.isTrusted||void 0!==t.button&&0!==t.button)return;if(t.touches&&t.touches.length>1)return;const o=e.dom.bounds(),n=h(t),r=z(n,I(e),o);if(!r)return;const i=e.pieces.get(r),c=e.selected;var a;c||!e.drawable.enabled||!e.drawable.eraseOnClick&&i&&i.color===e.turnColor||(a=e).drawable.shapes.length&&(a.drawable.shapes=[],a.dom.redraw(),de(a.drawable)),!1!==t.cancelable&&(!t.touches||e.blockTouchScroll||i||c||function(e,t){const o=I(e),n=e.dom.bounds(),r=Math.pow(n.width/8,2);for(const i of e.pieces.keys()){const e=b(i,o,n);if(d(e,t)<=r)return!0}return!1}(e,n))&&t.preventDefault();const l=!!e.premovable.current,u=!!e.predroppable.current;e.stats.ctrlKey=t.ctrlKey,e.selected&&W(e,e.selected,r)?J((e=>D(e,r)),e):D(e,r);const m=e.selected===r,v=ve(e,r);if(i&&v&&m&&function(e,t){const o=e.pieces.get(t);return!!o&&e.draggable.enabled&&("both"===e.movable.color||e.movable.color===o.color&&(e.turnColor===o.color||e.premovable.enabled))}(e,r)){e.draggable.current={orig:r,piece:i,origPos:n,pos:n,started:e.draggable.autoDistance&&e.stats.dragged,element:v,previouslySelected:c,originTarget:t.target,keyHasChanged:!1},v.cgDragging=!0,v.classList.add("dragging");const a=e.dom.elements.ghost;a&&(a.className=`ghost ${i.color} ${i.role}`,f(a,p(o)(s(r),I(e))),g(a,!0)),pe(e)}else l&&P(e),u&&A(e);e.dom.redraw()}function pe(e){requestAnimationFrame((()=>{var t;const o=e.draggable.current;if(!o)return;(null===(t=e.animation.current)||void 0===t?void 0:t.plan.anims.has(o.orig))&&(e.animation.current=void 0);const n=e.pieces.get(o.orig);if(n&&u(n,o.piece)){if(!o.started&&d(o.pos,o.origPos)>=Math.pow(e.draggable.distance,2)&&(o.started=!0),o.started){if("function"==typeof o.element){const e=o.element();if(!e)return;e.cgDragging=!0,e.classList.add("dragging"),o.element=e}const t=e.dom.bounds();f(o.element,[o.pos[0]-t.left-t.width/16,o.pos[1]-t.top-t.height/16]),o.keyHasChanged||(o.keyHasChanged=o.orig!==z(o.pos,I(e),t))}}else he(e);pe(e)}))}function fe(e,t){e.draggable.current&&(!t.touches||t.touches.length<2)&&(e.draggable.current.pos=h(t))}function ge(e,t){const o=e.draggable.current;if(!o)return;if("touchend"===t.type&&!1!==t.cancelable&&t.preventDefault(),"touchend"===t.type&&o.originTarget!==t.target&&!o.newPiece)return void(e.draggable.current=void 0);P(e),A(e);const n=z(h(t)||o.pos,I(e),e.dom.bounds());n&&o.started&&o.orig!==n?o.newPiece?N(e,o.orig,n,o.force):(e.stats.ctrlKey=t.ctrlKey,L(e,o.orig,n)&&(e.stats.dragged=!0)):o.newPiece?e.pieces.delete(o.orig):e.draggable.deleteOnDropOff&&!n&&(e.pieces.delete(o.orig),x(e.events.change)),(o.orig!==o.previouslySelected&&!o.keyHasChanged||o.orig!==n&&n)&&e.selectable.enabled||$(e),me(e),e.draggable.current=void 0,e.dom.redraw()}function he(e){const t=e.draggable.current;t&&(t.newPiece&&e.pieces.delete(t.orig),e.draggable.current=void 0,$(e),me(e),e.dom.redraw())}function me(e){const t=e.dom.elements;t.ghost&&g(t.ghost,!1)}function ve(e,t){let o=e.dom.elements.board.firstChild;for(;o;){if(o.cgKey===t&&"PIECE"===o.tagName)return o;o=o.nextSibling}}function be(e,t){e.exploding&&(t?e.exploding.stage=t:e.exploding=void 0,e.dom.redraw())}function we(e,o){function r(){!function(e){e.orientation=l(e.orientation),e.animation.current=e.draggable.current=e.selected=void 0}(e),o()}return{set(t){t.orientation&&t.orientation!==e.orientation&&r(),X(e,t),(t.fen?J:_)((e=>Y(e,t)),e)},state:e,getFen:()=>{return o=e.pieces,n.map((e=>t.map((t=>{const n=o.get(t+e);if(n){const e=G[n.role];return"white"===n.color?e.toUpperCase():e}return"1"})).join(""))).join("/").replace(/1{2,}/g,(e=>e.length.toString()));var o},toggleOrientation:r,setPieces(t){J((e=>function(e,t){for(const[o,n]of t)n?e.pieces.set(o,n):e.pieces.delete(o)}(e,t)),e)},selectSquare(t,o){t?J((e=>D(e,t,o)),e):e.selected&&($(e),e.dom.redraw())},move(t,o){J((e=>K(e,t,o)),e)},newPiece(t,o){J((e=>T(e,t,o)),e)},playPremove(){if(e.premovable.current){if(J(R,e))return!0;e.dom.redraw()}return!1},playPredrop(t){if(e.predroppable.current){const o=function(e,t){const o=e.predroppable.current;let n=!1;if(!o)return!1;t(o)&&T(e,{role:o.role,color:e.movable.color},o.key)&&(x(e.movable.events.afterNewPiece,o.role,o.key,{premove:!1,predrop:!0}),n=!0);return A(e),n}(e,t);return e.dom.redraw(),o}return!1},cancelPremove(){_(P,e)},cancelPredrop(){_(A,e)},cancelMove(){_((e=>{B(e),he(e)}),e)},stop(){_((e=>{F(e),he(e)}),e)},explode(t){!function(e,t){e.exploding={stage:1,keys:t},e.dom.redraw(),setTimeout((()=>{be(e,2),setTimeout((()=>be(e,void 0)),120)}),120)}(e,t)},setAutoShapes(t){_((e=>e.drawable.autoShapes=t),e)},setShapes(t){_((e=>e.drawable.shapes=t),e)},getKeyAtDomPos:t=>z(t,I(e),e.dom.bounds()),redrawAll:o,dragNewPiece(t,o,n){!function(e,t,o,n){const r="a0";e.pieces.set(r,t),e.dom.redraw();const i=h(o);e.draggable.current={orig:r,piece:t,origPos:i,pos:i,started:!0,element:()=>ve(e,r),originTarget:o.target,newPiece:!0,force:!!n,keyHasChanged:!1},pe(e)}(e,t,o,n)},destroy(){F(e),e.dom.unbind&&e.dom.unbind(),e.dom.destroyed=!0}}}function ye(e){return document.createElementNS("http://www.w3.org/2000/svg",e)}function ke(e,t,o){const n=e.drawable,r=n.current,i=r&&r.mouseSq?r:void 0,s=new Map,c=e.dom.bounds();for(const e of n.shapes.concat(n.autoShapes).concat(i?[i]:[]))e.dest&&s.set(e.dest,(s.get(e.dest)||0)+1);const a=n.shapes.concat(n.autoShapes).map((e=>({shape:e,current:!1,hash:Me(e,s,!1,c)})));i&&a.push({shape:i,current:!0,hash:Me(i,s,!0,c)});const l=a.map((e=>e.hash)).join(";");if(l===e.drawable.prevSvgHash)return;e.drawable.prevSvgHash=l;const d=t.querySelector("defs"),u=t.querySelector("g"),p=o.querySelector("g");!function(e,t,o){const n=new Map;let r;for(const o of t)o.shape.dest&&(r=e.brushes[o.shape.brush],o.shape.modifiers&&(r=qe(r,o.shape.modifiers)),n.set(r.key,r));const i=new Set;let s=o.firstChild;for(;s;)i.add(s.getAttribute("cgKey")),s=s.nextSibling;for(const[e,t]of n.entries())i.has(e)||o.appendChild(Ae(t))}(n,a,d),Ce(e,a.filter((e=>!e.shape.customSvg)),n.brushes,s,u),Ce(e,a.filter((e=>e.shape.customSvg)),n.brushes,s,p)}function Ce(e,t,o,n,r){const i=e.dom.bounds(),s=new Map,c=[];for(const e of t)s.set(e.hash,!1);let a,l=r.firstChild;for(;l;)a=l.getAttribute("cgHash"),s.has(a)?s.set(a,!0):c.push(l),l=l.nextSibling;for(const e of c)r.removeChild(e);for(const c of t)s.get(c.hash)||r.appendChild(Pe(e,c,o,n,i))}function Me({orig:e,dest:t,brush:o,piece:n,modifiers:r,customSvg:i},s,c,a){return[a.width,a.height,c,e,t,o,t&&(s.get(t)||0)>1,n&&Se(n),r&&(l=r,""+(l.lineWidth||"")),i&&xe(i)].filter((e=>e)).join(",");var l}function Se(e){return[e.color,e.role,e.scale].filter((e=>e)).join(",")}function xe(e){let t=0;for(let o=0;o<e.length;o++)t=(t<<5)-t+e.charCodeAt(o)>>>0;return"custom-"+t.toString()}function Pe(e,{shape:t,current:o,hash:n},r,i,c){let a;if(t.customSvg){const o=Te(s(t.orig),e.orientation);a=function(e,t,o){const[n,r]=De(t,o),i=Ke(ye("g"),{transform:`translate(${n},${r})`}),s=Ke(ye("svg"),{width:1,height:1,viewBox:"0 0 100 100"});return i.appendChild(s),s.innerHTML=e,i}(t.customSvg,o,c)}else if(t.piece)a=function(e,t,o,n){const r=De(t,n),i=o.color[0]+("knight"===o.role?"n":o.role[0]).toUpperCase();return Ke(ye("image"),{className:`${o.role} ${o.color}`,x:r[0]-.5,y:r[1]-.5,width:1,height:1,href:e+i+".svg",transform:`scale(${o.scale||1})`,"transform-origin":`${r[0]} ${r[1]}`})}(e.drawable.pieces.baseUrl,Te(s(t.orig),e.orientation),t.piece,c);else{const n=Te(s(t.orig),e.orientation);if(t.dest){let l=r[t.brush];t.modifiers&&(l=qe(l,t.modifiers)),a=function(e,t,o,n,r,i){const s=function(e){return(e?20:10)/64}(r&&!n),c=De(t,i),a=De(o,i),l=a[0]-c[0],d=a[1]-c[1],u=Math.atan2(d,l),p=Math.cos(u)*s,f=Math.sin(u)*s;return Ke(ye("line"),{stroke:e.color,"stroke-width":Le(e,n),"stroke-linecap":"round","marker-end":"url(#arrowhead-"+e.key+")",opacity:Ne(e,n),x1:c[0],y1:c[1],x2:a[0]-p,y2:a[1]-f})}(l,n,Te(s(t.dest),e.orientation),o,(i.get(t.dest)||0)>1,c)}else a=function(e,t,o,n){const r=De(t,n),i=[3/64,4/64],s=(n.width+n.height)/(4*Math.max(n.width,n.height));return Ke(ye("circle"),{stroke:e.color,"stroke-width":i[o?0:1],fill:"none",opacity:Ne(e,o),cx:r[0],cy:r[1],r:s-i[1]/2})}(r[t.brush],n,o,c)}return a.setAttribute("cgHash",n),a}function Ae(e){const t=Ke(ye("marker"),{id:"arrowhead-"+e.key,orient:"auto",markerWidth:4,markerHeight:8,refX:2.05,refY:2.01});return t.appendChild(Ke(ye("path"),{d:"M0,0 V4 L3,2 Z",fill:e.color})),t.setAttribute("cgKey",e.key),t}function Ke(e,t){for(const o in t)e.setAttribute(o,t[o]);return e}function Te(e,t){return"white"===t?e:[7-e[0],7-e[1]]}function qe(e,t){return{color:e.color,opacity:Math.round(10*e.opacity)/10,lineWidth:Math.round(t.lineWidth||e.lineWidth),key:[e.key,t.lineWidth].filter((e=>e)).join("")}}function Le(e,t){return(e.lineWidth||10)*(t?.85:1)/64}function Ne(e,t){return(e.opacity||1)*(t?.9:1)}function De(e,t){const o=Math.min(1,t.width/t.height),n=Math.min(1,t.height/t.width);return[(e[0]-3.5)*o,(3.5-e[1])*n]}function Oe(e,t){const o=v("coords",t);let n;for(const t of e)n=v("coord"),n.textContent=t,o.appendChild(n);return o}function $e(e,t){const o=e.dom.elements.board;if("ResizeObserver"in window&&new ResizeObserver(t).observe(e.dom.elements.wrap),e.viewOnly)return;const n=function(e){return t=>{e.draggable.current?he(e):e.drawable.current?ae(e):t.shiftKey||m(t)?e.drawable.enabled&&re(e,t):e.viewOnly||(e.dropmode.active?function(e,t){if(!e.dropmode.active)return;P(e),A(e);const o=e.dropmode.piece;if(o){e.pieces.set("a0",o);const n=h(t),r=n&&z(n,I(e),e.dom.bounds());r&&N(e,"a0",r)}e.dom.redraw()}(e,t):ue(e,t))}}(e);o.addEventListener("touchstart",n,{passive:!1}),o.addEventListener("mousedown",n,{passive:!1}),(e.disableContextMenu||e.drawable.enabled)&&o.addEventListener("contextmenu",(e=>e.preventDefault()))}function Ee(e,t,o,n){return e.addEventListener(t,o,n),()=>e.removeEventListener(t,o,n)}function We(e,t,o){return n=>{e.drawable.current?e.drawable.enabled&&o(e,n):e.viewOnly||t(e,n)}}function He(e){const t=I(e),o=p(e.dom.bounds()),n=e.dom.elements.board,r=e.pieces,i=e.animation.current,c=i?i.plan.anims:new Map,a=i?i.plan.fadings:new Map,l=e.draggable.current,d=function(e){var t;const o=new Map;if(e.lastMove&&e.highlight.lastMove)for(const t of e.lastMove)Ve(o,t,"last-move");e.check&&e.highlight.check&&Ve(o,e.check,"check");if(e.selected&&(Ve(o,e.selected,"selected"),e.movable.showDests)){const n=null===(t=e.movable.dests)||void 0===t?void 0:t.get(e.selected);if(n)for(const t of n)Ve(o,t,"move-dest"+(e.pieces.has(t)?" oc":""));const r=e.premovable.dests;if(r)for(const t of r)Ve(o,t,"premove-dest"+(e.pieces.has(t)?" oc":""))}const n=e.premovable.current;if(n)for(const e of n)Ve(o,e,"current-premove");else e.predroppable.current&&Ve(o,e.predroppable.current.key,"current-premove");const r=e.exploding;if(r)for(const e of r.keys)Ve(o,e,"exploding"+r.stage);return o}(e),u=new Set,g=new Set,h=new Map,m=new Map;let b,w,y,k,C,M,S,x,P,A;for(w=n.firstChild;w;){if(b=w.cgKey,Be(w))if(y=r.get(b),C=c.get(b),M=a.get(b),k=w.cgPiece,!w.cgDragging||l&&l.orig===b||(w.classList.remove("dragging"),f(w,o(s(b),t)),w.cgDragging=!1),!M&&w.cgFading&&(w.cgFading=!1,w.classList.remove("fading")),y){if(C&&w.cgAnimating&&k===je(y)){const e=s(b);e[0]+=C[2],e[1]+=C[3],w.classList.add("anim"),f(w,o(e,t))}else w.cgAnimating&&(w.cgAnimating=!1,w.classList.remove("anim"),f(w,o(s(b),t)),e.addPieceZIndex&&(w.style.zIndex=Ie(s(b),t)));k!==je(y)||M&&w.cgFading?M&&k===je(M)?(w.classList.add("fading"),w.cgFading=!0):Ge(h,k,w):u.add(b)}else Ge(h,k,w);else if(Fe(w)){const e=w.className;d.get(b)===e?g.add(b):Ge(m,e,w)}w=w.nextSibling}for(const[e,r]of d)if(!g.has(e)){P=m.get(r),A=P&&P.pop();const i=o(s(e),t);if(A)A.cgKey=e,f(A,i);else{const t=v("square",r);t.cgKey=e,f(t,i),n.insertBefore(t,n.firstChild)}}for(const[i,a]of r)if(C=c.get(i),!u.has(i))if(S=h.get(je(a)),x=S&&S.pop(),x){x.cgKey=i,x.cgFading&&(x.classList.remove("fading"),x.cgFading=!1);const n=s(i);e.addPieceZIndex&&(x.style.zIndex=Ie(n,t)),C&&(x.cgAnimating=!0,x.classList.add("anim"),n[0]+=C[2],n[1]+=C[3]),f(x,o(n,t))}else{const r=je(a),c=v("piece",r),l=s(i);c.cgPiece=r,c.cgKey=i,C&&(c.cgAnimating=!0,l[0]+=C[2],l[1]+=C[3]),f(c,o(l,t)),e.addPieceZIndex&&(c.style.zIndex=Ie(l,t)),n.appendChild(c)}for(const t of h.values())ze(e,t);for(const t of m.values())ze(e,t)}function Re(e){const t=e.dom.elements.wrap.getBoundingClientRect(),o=e.dom.elements.container,n=t.height/t.width,r=8*Math.floor(t.width*window.devicePixelRatio/8)/window.devicePixelRatio,i=r*n;o.style.width=r+"px",o.style.height=i+"px",e.dom.bounds.clear(),e.addDimensionsCssVars&&(document.documentElement.style.setProperty("--cg-width",r+"px"),document.documentElement.style.setProperty("--cg-height",i+"px"))}function Be(e){return"PIECE"===e.tagName}function Fe(e){return"SQUARE"===e.tagName}function ze(e,t){for(const o of t)e.dom.elements.board.removeChild(o)}function Ie(e,t){const o=e[1];return`${t?10-o:3+o}`}function je(e){return`${e.color} ${e.role}`}function Ve(e,t,o){const n=e.get(t);n?e.set(t,`${n} ${o}`):e.set(t,o)}function Ge(e,t,o){const n=e.get(t);n?n.push(o):e.set(t,[o])}function Ue(e){let t=!1;return()=>{t||(t=!0,requestAnimationFrame((()=>{e(),t=!1})))}}return function(n,r){const i={pieces:U(j),orientation:"white",turnColor:"white",coordinates:!0,autoCastle:!0,viewOnly:!1,disableContextMenu:!1,addPieceZIndex:!1,addDimensionsCssVars:!1,blockTouchScroll:!1,pieceKey:!1,highlight:{lastMove:!0,check:!0},animation:{enabled:!0,duration:200},movable:{free:!0,color:"both",showDests:!0,events:{},rookCastle:!0},premovable:{enabled:!0,showDests:!0,castle:!0,events:{}},predroppable:{enabled:!1,events:{}},draggable:{enabled:!0,distance:3,autoDistance:!0,showGhost:!0,deleteOnDropOff:!1},dropmode:{active:!1},selectable:{enabled:!0},stats:{dragged:!("ontouchstart"in window)},events:{},drawable:{enabled:!0,visible:!0,defaultSnapToValidMove:!0,eraseOnClick:!0,shapes:[],autoShapes:[],brushes:{green:{key:"g",color:"#15781B",opacity:1,lineWidth:10},red:{key:"r",color:"#882020",opacity:1,lineWidth:10},blue:{key:"b",color:"#003088",opacity:1,lineWidth:10},yellow:{key:"y",color:"#e68f00",opacity:1,lineWidth:10},paleBlue:{key:"pb",color:"#003088",opacity:.4,lineWidth:15},paleGreen:{key:"pg",color:"#15781B",opacity:.4,lineWidth:15},paleRed:{key:"pr",color:"#882020",opacity:.4,lineWidth:15},paleGrey:{key:"pgr",color:"#4a4a4a",opacity:.35,lineWidth:15}},pieces:{baseUrl:"https://lichess1.org/assets/piece/cburnett/"},prevSvgHash:""},hold:a()};function c(){const r="dom"in i?i.dom.unbind:void 0,c=function(n,r){n.innerHTML="",n.classList.add("cg-wrap");for(const t of e)n.classList.toggle("orientation-"+t,r.orientation===t);n.classList.toggle("manipulable",!r.viewOnly);const i=v("cg-container");n.appendChild(i);const s=v("cg-board");let c,a,l;if(i.appendChild(s),r.drawable.visible&&(c=Ke(ye("svg"),{class:"cg-shapes",viewBox:"-4 -4 8 8",preserveAspectRatio:"xMidYMid slice"}),c.appendChild(ye("defs")),c.appendChild(ye("g")),a=Ke(ye("svg"),{class:"cg-custom-svgs",viewBox:"-3.5 -3.5 8 8",preserveAspectRatio:"xMidYMid slice"}),a.appendChild(ye("g")),i.appendChild(c),i.appendChild(a)),r.coordinates){const e="black"===r.orientation?" black":"";i.appendChild(Oe(o,"ranks"+e)),i.appendChild(Oe(t,"files"+e))}return r.draggable.showGhost&&(l=v("piece","ghost"),g(l,!1),i.appendChild(l)),{board:s,container:i,wrap:n,ghost:l,svg:c,customSvg:a}}(n,i),a=function(e){let t;const o=()=>(void 0===t&&(t=e()),t);return o.clear=()=>{t=void 0},o}((()=>c.board.getBoundingClientRect())),l=e=>{He(u),!e&&c.svg&&ke(u,c.svg,c.customSvg)},d=()=>{Re(u),function(e){const t=I(e),o=p(e.dom.bounds());let n=e.dom.elements.board.firstChild;for(;n;)(Be(n)&&!n.cgAnimating||Fe(n))&&f(n,o(s(n.cgKey),t)),n=n.nextSibling}(u)},u=i;return u.dom={elements:c,bounds:a,redraw:Ue(l),redrawNow:l,unbind:r},u.drawable.prevSvgHash="",Re(u),l(!1),$e(u,d),r||(u.dom.unbind=function(e,t){const o=[];if("ResizeObserver"in window||o.push(Ee(document.body,"chessground.resize",t)),!e.viewOnly){const t=We(e,fe,se),n=We(e,ge,ce);for(const e of["touchmove","mousemove"])o.push(Ee(document,e,t));for(const e of["touchend","mouseup"])o.push(Ee(document,e,n));const r=()=>e.dom.bounds.clear();o.push(Ee(document,"scroll",r,{capture:!0,passive:!0})),o.push(Ee(window,"resize",r,{passive:!0}))}return()=>o.forEach((e=>e()))}(u,d)),u.events.insert&&u.events.insert(c),u}return Y(i,r||{}),we(c(),c)}}));