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