UNPKG

livecodes

Version:

A Code Playground That Just Works!

2 lines (1 loc) 14.4 kB
var re=Object.create;var V=Object.defineProperty;var ie=Object.getOwnPropertyDescriptor;var se=Object.getOwnPropertyNames;var ae=Object.getPrototypeOf,le=Object.prototype.hasOwnProperty;var ce=(c,m)=>()=>(m||c((m={exports:{}}).exports,m),m.exports);var de=(c,m,C,f)=>{if(m&&typeof m=="object"||typeof m=="function")for(let x of se(m))!le.call(c,x)&&x!==C&&V(c,x,{get:()=>m[x],enumerable:!(f=ie(m,x))||f.enumerable});return c};var fe=(c,m,C)=>(C=c!=null?re(ae(c)):{},de(m||!c||!c.__esModule?V(C,"default",{value:c,enumerable:!0}):C,c));var Y=ce((Ce,F)=>{var ue=function(){var c=String.fromCharCode,m="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",C="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$",f={};function x(o,r){if(!f[o]){f[o]={};for(var t=0;t<o.length;t++)f[o][o.charAt(t)]=t}return f[o][r]}var P={compressToBase64:function(o){if(o==null)return"";var r=P._compress(o,6,function(t){return m.charAt(t)});switch(r.length%4){default:case 0:return r;case 1:return r+"===";case 2:return r+"==";case 3:return r+"="}},decompressFromBase64:function(o){return o==null?"":o==""?null:P._decompress(o.length,32,function(r){return x(m,o.charAt(r))})},compressToUTF16:function(o){return o==null?"":P._compress(o,15,function(r){return c(r+32)})+" "},decompressFromUTF16:function(o){return o==null?"":o==""?null:P._decompress(o.length,16384,function(r){return o.charCodeAt(r)-32})},compressToUint8Array:function(o){for(var r=P.compress(o),t=new Uint8Array(r.length*2),n=0,l=r.length;n<l;n++){var p=r.charCodeAt(n);t[n*2]=p>>>8,t[n*2+1]=p%256}return t},decompressFromUint8Array:function(o){if(o==null)return P.decompress(o);for(var r=new Array(o.length/2),t=0,n=r.length;t<n;t++)r[t]=o[t*2]*256+o[t*2+1];var l=[];return r.forEach(function(p){l.push(c(p))}),P.decompress(l.join(""))},compressToEncodedURIComponent:function(o){return o==null?"":P._compress(o,6,function(r){return C.charAt(r)})},decompressFromEncodedURIComponent:function(o){return o==null?"":o==""?null:(o=o.replace(/ /g,"+"),P._decompress(o.length,32,function(r){return x(C,o.charAt(r))}))},compress:function(o){return P._compress(o,16,function(r){return c(r)})},_compress:function(o,r,t){if(o==null)return"";var n,l,p={},v={},u="",E="",w="",b=2,A=3,d=2,y=[],e=0,i=0,O;for(O=0;O<o.length;O+=1)if(u=o.charAt(O),Object.prototype.hasOwnProperty.call(p,u)||(p[u]=A++,v[u]=!0),E=w+u,Object.prototype.hasOwnProperty.call(p,E))w=E;else{if(Object.prototype.hasOwnProperty.call(v,w)){if(w.charCodeAt(0)<256){for(n=0;n<d;n++)e=e<<1,i==r-1?(i=0,y.push(t(e)),e=0):i++;for(l=w.charCodeAt(0),n=0;n<8;n++)e=e<<1|l&1,i==r-1?(i=0,y.push(t(e)),e=0):i++,l=l>>1}else{for(l=1,n=0;n<d;n++)e=e<<1|l,i==r-1?(i=0,y.push(t(e)),e=0):i++,l=0;for(l=w.charCodeAt(0),n=0;n<16;n++)e=e<<1|l&1,i==r-1?(i=0,y.push(t(e)),e=0):i++,l=l>>1}b--,b==0&&(b=Math.pow(2,d),d++),delete v[w]}else for(l=p[w],n=0;n<d;n++)e=e<<1|l&1,i==r-1?(i=0,y.push(t(e)),e=0):i++,l=l>>1;b--,b==0&&(b=Math.pow(2,d),d++),p[E]=A++,w=String(u)}if(w!==""){if(Object.prototype.hasOwnProperty.call(v,w)){if(w.charCodeAt(0)<256){for(n=0;n<d;n++)e=e<<1,i==r-1?(i=0,y.push(t(e)),e=0):i++;for(l=w.charCodeAt(0),n=0;n<8;n++)e=e<<1|l&1,i==r-1?(i=0,y.push(t(e)),e=0):i++,l=l>>1}else{for(l=1,n=0;n<d;n++)e=e<<1|l,i==r-1?(i=0,y.push(t(e)),e=0):i++,l=0;for(l=w.charCodeAt(0),n=0;n<16;n++)e=e<<1|l&1,i==r-1?(i=0,y.push(t(e)),e=0):i++,l=l>>1}b--,b==0&&(b=Math.pow(2,d),d++),delete v[w]}else for(l=p[w],n=0;n<d;n++)e=e<<1|l&1,i==r-1?(i=0,y.push(t(e)),e=0):i++,l=l>>1;b--,b==0&&(b=Math.pow(2,d),d++)}for(l=2,n=0;n<d;n++)e=e<<1|l&1,i==r-1?(i=0,y.push(t(e)),e=0):i++,l=l>>1;for(;;)if(e=e<<1,i==r-1){y.push(t(e));break}else i++;return y.join("")},decompress:function(o){return o==null?"":o==""?null:P._decompress(o.length,32768,function(r){return o.charCodeAt(r)})},_decompress:function(o,r,t){var n=[],l,p=4,v=4,u=3,E="",w=[],b,A,d,y,e,i,O,a={val:t(0),position:r,index:1};for(b=0;b<3;b+=1)n[b]=b;for(d=0,e=Math.pow(2,2),i=1;i!=e;)y=a.val&a.position,a.position>>=1,a.position==0&&(a.position=r,a.val=t(a.index++)),d|=(y>0?1:0)*i,i<<=1;switch(l=d){case 0:for(d=0,e=Math.pow(2,8),i=1;i!=e;)y=a.val&a.position,a.position>>=1,a.position==0&&(a.position=r,a.val=t(a.index++)),d|=(y>0?1:0)*i,i<<=1;O=c(d);break;case 1:for(d=0,e=Math.pow(2,16),i=1;i!=e;)y=a.val&a.position,a.position>>=1,a.position==0&&(a.position=r,a.val=t(a.index++)),d|=(y>0?1:0)*i,i<<=1;O=c(d);break;case 2:return""}for(n[3]=O,A=O,w.push(O);;){if(a.index>o)return"";for(d=0,e=Math.pow(2,u),i=1;i!=e;)y=a.val&a.position,a.position>>=1,a.position==0&&(a.position=r,a.val=t(a.index++)),d|=(y>0?1:0)*i,i<<=1;switch(O=d){case 0:for(d=0,e=Math.pow(2,8),i=1;i!=e;)y=a.val&a.position,a.position>>=1,a.position==0&&(a.position=r,a.val=t(a.index++)),d|=(y>0?1:0)*i,i<<=1;n[v++]=c(d),O=v-1,p--;break;case 1:for(d=0,e=Math.pow(2,16),i=1;i!=e;)y=a.val&a.position,a.position>>=1,a.position==0&&(a.position=r,a.val=t(a.index++)),d|=(y>0?1:0)*i,i<<=1;n[v++]=c(d),O=v-1,p--;break;case 2:return w.join("")}if(p==0&&(p=Math.pow(2,u),u++),n[O])E=n[O];else if(O===v)E=A+A.charAt(0);else return null;w.push(E),n[v++]=A+E.charAt(0),p--,A=E,p==0&&(p=Math.pow(2,u),u++)}}};return P}();typeof F!="undefined"&&F!=null&&(F.exports=ue)});import{h as ye,onMounted as he,onUnmounted as ve,ref as B,watch as we}from"vue";var K=fe(Y());var pe={chrome:["accelerometer","bluetooth","camera","clipboard-read","clipboard-write","display-capture","encrypted-media","geolocation","gyroscope","language-detector","language-model","local-network-access","microphone","midi","proofreader","rewriter","serial","summarizer","translator","web-share","writer","window-placement","xr-spatial-tracking"],firefox:["camera","display-capture","geolocation","microphone","web-share"],default:["accelerometer","ambient-light-sensor","camera","display-capture","encrypted-media","geolocation","gyroscope","microphone","midi","payment","serial","vr","web-share","xr-spatial-tracking"]},ge=()=>{if(typeof navigator=="undefined")return"default";let c=navigator.userAgent;return/Firefox\//i.test(c)?"firefox":/Chrome\//i.test(c)?"chrome":"default"},X=()=>pe[ge()].filter(c=>{var C,f,x;let m=(x=(f=(C=globalThis.document)==null?void 0:C.featurePolicy)==null?void 0:f.features)==null?void 0:x.call(f);return m?m.includes(c):!0}).join("; ");async function z(c,m={}){typeof c=="object"&&!(c instanceof HTMLElement)&&(c.headless||c.view==="headless")&&(m=c,c=null);let C=6e4,{config:f={},headless:x,loading:P="lazy",view:o}=m,r=x||o==="headless",t=null,n=null,l=s=>{s.style.position="absolute",s.style.top="0",s.style.visibility="hidden",s.style.opacity="0"};if(typeof c=="string")t=document.querySelector(c);else if(c instanceof HTMLElement)t=c;else if(!(r&&typeof c=="object"))throw new Error("A valid container element is required.");if(!t)if(r)t=document.createElement("div"),l(t),document.body.appendChild(t);else throw new Error(`Cannot find element: "${c}"`);let p=new URL(me(m)),v=p.origin;p.searchParams.set("embed","true"),p.searchParams.set("loading",r?"eager":P),p.searchParams.set("sdkVersion","0.14.1"),typeof f=="object"&&Object.keys(f).length>0&&p.searchParams.set("config","sdk");let u=m.params;typeof u=="object"&&Object.keys(u).length>0&&JSON.stringify(u).length<1800&&Object.keys(u).forEach(s=>{p.searchParams.set(s,encodeURIComponent(String(u[s])))});let E=!1,w="Cannot call API methods after calling `destroy()`.",b=[],A=(s,g="message")=>{addEventListener(g,s),b.push(s)},d=(s,g="message")=>{removeEventListener(g,s);let S=b.indexOf(s);S>-1&&b.splice(S,1)},e=await new Promise(s=>{var R,U,M,I,_,j,$,G,Q;if(!t)return;let g=t.dataset.height||t.style.height;if(g&&!r){let N=isNaN(Number(g))?g:g+"px";t.style.height=N}t.dataset.defaultStyles!=="false"&&!r&&((R=t.style).backgroundColor||(R.backgroundColor="#fff"),(U=t.style).border||(U.border="1px solid black"),(M=t.style).borderRadius||(M.borderRadius="8px"),(I=t.style).boxSizing||(I.boxSizing="border-box"),(_=t.style).padding||(_.padding="0"),(j=t.style).width||(j.width="100%"),($=t.style).height||($.height=t.style.height||"300px"),t.style.minHeight="200px",t.style.flexGrow="1",(G=t.style).overflow||(G.overflow="hidden"),(Q=t.style).resize||(Q.resize="vertical"),getComputedStyle(t).getPropertyValue("display")==="inline"&&(t.style.display="block"));let S="livecodes",L=t.querySelector(`iframe.${S}`),h=L||document.createElement("iframe");h.classList.add(S),h.setAttribute("allow",X()),h.setAttribute("allowtransparency","true"),h.setAttribute("allowpaymentrequest","true"),h.setAttribute("allowfullscreen","true"),h.setAttribute("sandbox","allow-same-origin allow-downloads allow-forms allow-modals allow-orientation-lock allow-pointer-lock allow-popups allow-presentation allow-scripts");let k=P==="eager"?"eager":"lazy";h.setAttribute("loading",k),r?l(h):(h.style.height="100%",h.style.minHeight="200px",h.style.width="100%",h.style.margin="0",h.style.border="0",h.style.borderRadius=t.style.borderRadius),A(function N(H){var D;H.source!==h.contentWindow||H.origin!==v||((D=H.data)==null?void 0:D.type)!=="livecodes-init"||(d(N),n=Number(H.data.payload.appVersion.replace(/^v/,"")))}),(!n||n<46)&&A(function N(H){var D,Z;H.source!==h.contentWindow||H.origin!==v||((D=H.data)==null?void 0:D.type)!=="livecodes-get-config"||(d(N),(Z=h.contentWindow)==null||Z.postMessage({type:"livecodes-config",payload:f},v))}),h.onload=()=>{s(h)},h.src=p.href,L||t.appendChild(h)}),i=new Promise(s=>{A(function g(S){var L;S.source!==e.contentWindow||S.origin!==v||((L=S.data)==null?void 0:L.type)!=="livecodes-ready"||(d(g),s(),i.settled=!0)})}),O=()=>E?Promise.reject(w):new Promise(async s=>{var S;i.settled&&s();let g={type:"livecodes-load"};(S=e.contentWindow)==null||S.postMessage(g,v),await i,s()}),a=(s,g)=>new Promise(async(S,L)=>{var U;if(E)return L(w);await O();let h=oe(),k=setTimeout(()=>{d(R),L(new Error(`SDK call "${s}" timed out after ${C}ms.`))},C);function R(M){var I,_;if(!(M.source!==e.contentWindow||M.origin!==v||((I=M.data)==null?void 0:I.type)!=="livecodes-api-response"||((_=M.data)==null?void 0:_.id)!==h)&&M.data.method===s){clearTimeout(k),d(R);let j=M.data.payload;j!=null&&j.error?L(j.error):S(j)}}A(R),(U=e.contentWindow)==null||U.postMessage({method:s,id:h,args:g},v)}),T={},ee=["load","ready","code","console","tests","destroy"],q=(s,g)=>{var S;if(E)throw new Error(w);return ee.includes(s)?(a("watch",[s]),T[s]||(T[s]=[]),(S=T[s])==null||S.push(g),{remove:()=>{var L,h;T[s]=(L=T[s])==null?void 0:L.filter(k=>k!==g),((h=T[s])==null?void 0:h.length)===0&&a("watch",[s,"unsubscribe"])}}):{remove:()=>{}}},te=s=>({"livecodes-app-loaded":"load","livecodes-ready":"ready","livecodes-change":"code","livecodes-console":"console","livecodes-test-results":"tests","livecodes-destroy":"destroy"})[s];A(async function(g){var h,k,R,U;let S=te((k=(h=g.data)==null?void 0:h.type)!=null?k:"");if(g.source!==e.contentWindow||g.origin!==v||!S||!T[S])return;let L=(R=g.data)==null?void 0:R.payload;(U=T[S])==null||U.forEach(M=>{M(L)})});let ne=()=>{var s;(s=e==null?void 0:e.remove)==null||s.call(e),Object.values(T).forEach(g=>{g.length=0}),b.forEach(g=>removeEventListener("message",g)),b.length=0,J&&t&&J.unobserve(t),E=!0},J;P==="lazy"&&"IntersectionObserver"in window&&(J=new IntersectionObserver((s,g)=>{s.forEach(async S=>{S.isIntersecting&&(await O(),g.unobserve(t))})},{rootMargin:"150px"}),J.observe(t));let oe=()=>(String(Math.random())+Date.now().toFixed()).replace("0.","");return{load:()=>O(),run:()=>a("run"),format:s=>a("format",[s]),getShareUrl:s=>a("getShareUrl",[s]),getConfig:s=>a("getConfig",[s]),setConfig:s=>a("setConfig",[s]),getCode:()=>a("getCode"),show:(s,g)=>a("show",[s,g]),runTests:()=>a("runTests"),onChange:s=>q("code",s),watch:q,exec:(s,...g)=>a("exec",[s,...g]),destroy:()=>E?Promise.reject(w):(ne(),Promise.resolve())}}function me(c={}){let{appUrl:m="https://livecodes.io",params:C={},config:f={},headless:x,import:P,lite:o,view:r,...t}=c,n;try{n=new URL(m)}catch(v){throw new Error(`${m} is not a valid URL.`)}let l=new URLSearchParams;Object.entries(t).forEach(([v,u])=>{u!==void 0&&n.searchParams.set(v,String(u))});let p=c.view==="headless"||x;if(o&&(console.warn(`Deprecation notice: "lite" option is deprecated. Use "config: { mode: 'lite' }" instead.`),typeof f=="object"&&f.mode==null?f.mode="lite":n.searchParams.set("lite","true")),r&&(console.warn('Deprecation notice: The "view" option has been moved to "config.view". For headless mode use "headless: true".'),typeof f=="object"&&f.view==null&&r!=="headless"?f.view=r:n.searchParams.set("view",r)),typeof f=="string")try{new URL(f),n.searchParams.set("config",encodeURIComponent(f))}catch(v){throw new Error('"config" is not a valid URL or configuration object.')}else f&&typeof f=="object"&&Object.keys(f).length>0&&(f.title&&f.title!=="Untitled Project"&&n.searchParams.set("title",f.title),f.description&&f.description.length>0&&n.searchParams.set("description",f.description),l.set("config","code/"+(0,K.compressToEncodedURIComponent)(JSON.stringify(f))));if(C&&typeof C=="object"&&Object.keys(C).length>0)try{l.set("params",(0,K.compressToEncodedURIComponent)(JSON.stringify(C)))}catch(v){Object.keys(C).forEach(u=>{n.searchParams.set(u,encodeURIComponent(String(C[u])))})}return P&&n.searchParams.set("x",encodeURIComponent(P)),p&&n.searchParams.set("headless","true"),l.toString().length>0&&(n.hash=l.toString()),n.href}var Ee={appUrl:String,config:[Object,String],headless:Boolean,import:String,lite:Boolean,loading:String,params:Object,template:String,view:String,height:String},W=c=>JSON.parse(JSON.stringify(c)),be={props:Ee,emits:["sdkReady"],setup(c,m){let{height:C,...f}=c,x=B(),P=B(C||""),o=B(),{config:r,...t}=f,n=JSON.stringify(r),l=JSON.stringify(t),p=0,v=u=>u!==p;return he(()=>{if(!x.value)return;let u=++p;z(x.value,W(f)).then(E=>{if(v(u)){E.destroy();return}o.value=E,m.emit("sdkReady",E)})}),we(c,async u=>{var y;if(!x.value)return;let{height:E,...w}=u,b=++p;E&&(P.value=E);let{config:A,...d}=w;!o.value||JSON.stringify(d)!==l?(l=JSON.stringify(d),n=JSON.stringify(A),(y=o.value)==null||y.destroy(),o.value=void 0,z(x.value,W(w)).then(e=>{if(v(b)){e.destroy();return}o.value=e,m.emit("sdkReady",e)})):JSON.stringify(A)!==n&&(n=JSON.stringify(A),A&&o.value.setConfig(W(A)||{}))}),ve(()=>{var u;++p,(u=o.value)==null||u.destroy()}),()=>{var u,E;return ye("div",{ref:x,style:P.value?{height:Number(P.value)?`${P.value}px`:P.value}:void 0},((E=(u=m.slots).default)==null?void 0:E.call(u))||"")}}},Me=be;export{Me as default};