UNPKG

caissify_chessground

Version:

Vue 3 wrapper for chessground - A modern chess board component

2 lines (1 loc) 6.74 kB
(function(w,u){typeof exports=="object"&&typeof module<"u"?u(exports,require("vue"),require("chessground")):typeof define=="function"&&define.amd?define(["exports","vue","chessground"],u):(w=typeof globalThis<"u"?globalThis:w||self,u(w.CaissifyChessground={},w.Vue,w.Chessground))})(this,function(w,u,Q){"use strict";function _(q,k){const e=u.ref(),t=()=>{if(q.value)try{e.value=Q.Chessground(q.value,k.value)}catch(s){console.error("Failed to initialize chessground:",s)}},b=()=>{e.value&&(e.value.destroy(),e.value=void 0)},D=s=>{e.value&&e.value.set(s)};u.watch(k,s=>{D(s)},{deep:!0}),u.watch(q,s=>{s&&(b(),t())}),u.onMounted(()=>{t()}),u.onUnmounted(()=>{b()});const C=s=>{const d=new Map,p=s.split(" ")[0].split("/");for(let c=0;c<8;c++){let m=0;for(const a of p[c])if(a>="1"&&a<="8")m+=parseInt(a);else{const S=a===a.toUpperCase()?"white":"black",E=a.toLowerCase(),A={p:"pawn",r:"rook",n:"knight",b:"bishop",q:"queen",k:"king"},O=String.fromCharCode(97+m)+(8-c);d.set(O,{role:A[E],color:S}),m++}}return d};return{chessground:e,set:s=>{if(e.value)if(s.fen){const d=C(s.fen),p={...s};delete p.fen,e.value.setPieces(d);const c={...k.value,...p,lastMove:p.lastMove||void 0,selected:void 0,check:p.check||void 0};e.value.set(c),e.value.redrawAll()}else e.value.set(s)},toggleOrientation:()=>{e.value&&e.value.toggleOrientation()},setPieces:s=>{if(e.value){let d=s;s instanceof Map||(d=new Map,s&&typeof s=="object"&&Object.entries(s).forEach(([p,c])=>{d.set(p,c)})),e.value.setPieces(d),e.value.redrawAll()}},selectSquare:s=>{e.value&&e.value.selectSquare(s)},move:(s,d)=>{e.value&&e.value.move(s,d)},newPiece:(s,d)=>{e.value&&e.value.newPiece(s,d)},playPremove:()=>{var s;return((s=e.value)==null?void 0:s.playPremove())??!1},cancelPremove:()=>{e.value&&e.value.cancelPremove()},cancelMove:()=>{e.value&&e.value.cancelMove()},stop:()=>{e.value&&e.value.stop()},explode:s=>{e.value&&e.value.explode(s)},setAutoShapes:s=>{e.value&&e.value.setAutoShapes(s)},setShapes:s=>{e.value&&e.value.setShapes(s)},getKeyAtDomPos:s=>{var d;return(d=e.value)==null?void 0:d.getKeyAtDomPos(s)},redrawAll:()=>{e.value&&e.value.redrawAll()},dragNewPiece:(s,d,p)=>{e.value&&e.value.dragNewPiece(s,d,p)}}}const x=u.defineComponent({__name:"ChessBoard",props:{fen:{},orientation:{default:"white"},turnColor:{},check:{type:Boolean},lastMove:{},coordinates:{type:Boolean,default:!0},coordinatesPosition:{default:"inside"},coordinatesOnSquares:{type:Boolean,default:!1},autoCastle:{type:Boolean,default:!0},viewOnly:{type:Boolean,default:!1},disableContextMenu:{type:Boolean,default:!0},movable:{},premovable:{},animation:{},highlight:{},drawable:{},selectable:{},onMove:{},onDropNewPiece:{},onSelect:{},onInsert:{},onChange:{}},emits:["move","dropNewPiece","select","insert","change"],setup(q,{expose:k,emit:e}){const t=q,b=e,D=u.ref(),C=u.ref(null),R=()=>{const c=new Map,m=(o,n)=>o>=0&&o<8&&n>=1&&n<=8,a=(o,n)=>String.fromCharCode(97+o)+n;for(let o=0;o<8;o++){const n=a(o,2),f=[a(o,3),a(o,4)];c.set(n,f)}for(let o=0;o<8;o++){const n=a(o,7),f=[a(o,6),a(o,5)];c.set(n,f)}const S=[[-2,-1],[-2,1],[-1,-2],[-1,2],[1,-2],[1,2],[2,-1],[2,1]];return[[1,1],[6,1]].forEach(([o,n])=>{const f=a(o,n),i=[];S.forEach(([r,l])=>{const g=o+r,v=n+l;m(g,v)&&i.push(a(g,v))}),c.set(f,i)}),[[1,8],[6,8]].forEach(([o,n])=>{const f=a(o,n),i=[];S.forEach(([r,l])=>{const g=o+r,v=n+l;m(g,v)&&i.push(a(g,v))}),c.set(f,i)}),[[0,1],[7,1],[0,8],[7,8]].forEach(([o,n])=>{const f=a(o,n),i=[];for(let r=0;r<8;r++)r!==o&&i.push(a(r,n));for(let r=1;r<=8;r++)r!==n&&i.push(a(o,r));c.set(f,i)}),[[2,1],[5,1],[2,8],[5,8]].forEach(([o,n])=>{const f=a(o,n),i=[];[[-1,-1],[-1,1],[1,-1],[1,1]].forEach(([l,g])=>{for(let v=1;v<8;v++){const M=o+l*v,B=n+g*v;if(m(M,B))i.push(a(M,B));else break}}),c.set(f,i)}),[[3,1],[3,8]].forEach(([o,n])=>{const f=a(o,n),i=[];for(let l=0;l<8;l++)l!==o&&i.push(a(l,n));for(let l=1;l<=8;l++)l!==n&&i.push(a(o,l));[[-1,-1],[-1,1],[1,-1],[1,1]].forEach(([l,g])=>{for(let v=1;v<8;v++){const M=o+l*v,B=n+g*v;if(m(M,B))i.push(a(M,B));else break}}),c.set(f,i)}),[[4,1],[4,8]].forEach(([o,n])=>{const f=a(o,n),i=[];[[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]].forEach(([l,g])=>{const v=o+l,M=n+g;m(v,M)&&i.push(a(v,M))}),c.set(f,i)}),c},F=u.computed(()=>{var m,a,S,E,A,O,N;return{orientation:t.orientation,turnColor:t.turnColor,check:t.check,lastMove:t.lastMove,coordinates:t.coordinates,coordinatesOnSquares:t.coordinatesOnSquares,autoCastle:t.autoCastle,viewOnly:t.viewOnly,disableContextMenu:t.disableContextMenu,movable:{free:((m=t.movable)==null?void 0:m.free)||!1,color:((a=t.movable)==null?void 0:a.color)||"white",rookCastle:((S=t.movable)==null?void 0:S.rookCastle)!==!1,showDests:((E=t.movable)==null?void 0:E.showDests)!==!1,dests:((A=t.movable)==null?void 0:A.dests)||((O=t.movable)!=null&&O.free?void 0:R()),events:{after:(h,P,o)=>{var n;(n=t.onMove)==null||n.call(t,h,P,o.captured),b("move",h,P,o.captured)}}},draggable:{enabled:!0,showGhost:!0},premovable:t.premovable||{enabled:!0},animation:t.animation||{enabled:!0,duration:200},highlight:t.highlight||{lastMove:!0,check:!0},drawable:t.drawable||{enabled:!1,visible:!0},selectable:{enabled:!t.viewOnly&&((N=t.selectable)==null?void 0:N.enabled)!==!1},events:{select:h=>{var P,o,n;if(C.value&&C.value!==h){const f=(P=F.value.movable)==null?void 0:P.dests,i=f==null?void 0:f.get(C.value);if(i&&i.includes(h)){if(y.value){const r=C.value,l=h,g=y.value.state.pieces.get(l);y.value.move(r,l),(o=t.onMove)==null||o.call(t,r,l,g),b("move",r,l,g)}C.value=null;return}}C.value=h,(n=t.onSelect)==null||n.call(t,h),b("select",h)},insert:h=>{var P;(P=t.onInsert)==null||P.call(t,h),b("insert",h)},change:()=>{var h;(h=t.onChange)==null||h.call(t),b("change")}}}}),{chessground:y,set:K,toggleOrientation:T,setPieces:j,selectSquare:I,move:z,newPiece:U,playPremove:V,cancelPremove:G,cancelMove:L,stop:W,explode:$,setAutoShapes:H,setShapes:J,getKeyAtDomPos:s,redrawAll:d,dragNewPiece:p}=_(D,F);return u.watch(()=>t.fen,c=>{c&&y.value&&(K({fen:c}),b("change"))}),u.onMounted(()=>{u.nextTick(()=>{t.fen&&y.value&&(K({fen:t.fen}),b("change"))})}),k({set:K,toggleOrientation:T,setPieces:j,selectSquare:I,move:z,newPiece:U,playPremove:V,cancelPremove:G,cancelMove:L,stop:W,explode:$,setAutoShapes:H,setShapes:J,getKeyAtDomPos:s,redrawAll:d,dragNewPiece:p,chessground:y.value}),(c,m)=>(u.openBlock(),u.createElementBlock("div",{ref_key:"boardElement",ref:D,class:u.normalizeClass(["chessground",{"cg-coordinates":c.coordinates,"coords-outside":c.coordinates&&c.coordinatesPosition==="outside"}])},null,2))}}),X={install(q){q.component("ChessBoard",x)}};w.CaissifyChessgroundPlugin=X,w.ChessBoard=x,w.default=x,w.useChessground=_,Object.defineProperties(w,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});