livecode-static/sdk/livecodes.cjs
2025-06-11 22:23:49 +08:00

2 lines
12 KiB
JavaScript

"use strict";var V=Object.create;var D=Object.defineProperty;var ee=Object.getOwnPropertyDescriptor;var te=Object.getOwnPropertyNames;var ne=Object.getPrototypeOf,oe=Object.prototype.hasOwnProperty;var se=(c,h)=>()=>(h||c((h={exports:{}}).exports,h),h.exports),re=(c,h)=>{for(var E in h)D(c,E,{get:h[E],enumerable:!0})},G=(c,h,E,p)=>{if(h&&typeof h=="object"||typeof h=="function")for(let M of te(h))!oe.call(c,M)&&M!==E&&D(c,M,{get:()=>h[M],enumerable:!(p=ee(h,M))||p.enumerable});return c};var ie=(c,h,E)=>(E=c!=null?V(ne(c)):{},G(h||!c||!c.__esModule?D(E,"default",{value:c,enumerable:!0}):E,c)),ae=c=>G(D({},"__esModule",{value:!0}),c);var X=se((ue,K)=>{var le=function(){var c=String.fromCharCode,h="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",E="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 h.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(h,o.charAt(e))})},compressToUTF16:function(o){return o==null?"":x._compress(o,15,function(e){return c(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 m=e.charCodeAt(t);l[t*2]=m>>>8,l[t*2+1]=m%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(m){r.push(c(m))}),x.decompress(r.join(""))},compressToEncodedURIComponent:function(o){return o==null?"":x._compress(o,6,function(e){return E.charAt(e)})},decompressFromEncodedURIComponent:function(o){return o==null?"":o==""?null:(o=o.replace(/ /g,"+"),x._decompress(o.length,32,function(e){return M(E,o.charAt(e))}))},compress:function(o){return x._compress(o,16,function(e){return c(e)})},_compress:function(o,e,l){if(o==null)return"";var t,r,m={},w={},v="",L="",b="",y=2,O=3,f=2,d=[],n=0,s=0,S;for(S=0;S<o.length;S+=1)if(v=o.charAt(S),Object.prototype.hasOwnProperty.call(m,v)||(m[v]=O++,w[v]=!0),L=b+v,Object.prototype.hasOwnProperty.call(m,L))b=L;else{if(Object.prototype.hasOwnProperty.call(w,b)){if(b.charCodeAt(0)<256){for(t=0;t<f;t++)n=n<<1,s==e-1?(s=0,d.push(l(n)),n=0):s++;for(r=b.charCodeAt(0),t=0;t<8;t++)n=n<<1|r&1,s==e-1?(s=0,d.push(l(n)),n=0):s++,r=r>>1}else{for(r=1,t=0;t<f;t++)n=n<<1|r,s==e-1?(s=0,d.push(l(n)),n=0):s++,r=0;for(r=b.charCodeAt(0),t=0;t<16;t++)n=n<<1|r&1,s==e-1?(s=0,d.push(l(n)),n=0):s++,r=r>>1}y--,y==0&&(y=Math.pow(2,f),f++),delete w[b]}else for(r=m[b],t=0;t<f;t++)n=n<<1|r&1,s==e-1?(s=0,d.push(l(n)),n=0):s++,r=r>>1;y--,y==0&&(y=Math.pow(2,f),f++),m[L]=O++,b=String(v)}if(b!==""){if(Object.prototype.hasOwnProperty.call(w,b)){if(b.charCodeAt(0)<256){for(t=0;t<f;t++)n=n<<1,s==e-1?(s=0,d.push(l(n)),n=0):s++;for(r=b.charCodeAt(0),t=0;t<8;t++)n=n<<1|r&1,s==e-1?(s=0,d.push(l(n)),n=0):s++,r=r>>1}else{for(r=1,t=0;t<f;t++)n=n<<1|r,s==e-1?(s=0,d.push(l(n)),n=0):s++,r=0;for(r=b.charCodeAt(0),t=0;t<16;t++)n=n<<1|r&1,s==e-1?(s=0,d.push(l(n)),n=0):s++,r=r>>1}y--,y==0&&(y=Math.pow(2,f),f++),delete w[b]}else for(r=m[b],t=0;t<f;t++)n=n<<1|r&1,s==e-1?(s=0,d.push(l(n)),n=0):s++,r=r>>1;y--,y==0&&(y=Math.pow(2,f),f++)}for(r=2,t=0;t<f;t++)n=n<<1|r&1,s==e-1?(s=0,d.push(l(n)),n=0):s++,r=r>>1;for(;;)if(n=n<<1,s==e-1){d.push(l(n));break}else s++;return d.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,m=4,w=4,v=3,L="",b=[],y,O,f,d,n,s,S,a={val:l(0),position:e,index:1};for(y=0;y<3;y+=1)t[y]=y;for(f=0,n=Math.pow(2,2),s=1;s!=n;)d=a.val&a.position,a.position>>=1,a.position==0&&(a.position=e,a.val=l(a.index++)),f|=(d>0?1:0)*s,s<<=1;switch(r=f){case 0:for(f=0,n=Math.pow(2,8),s=1;s!=n;)d=a.val&a.position,a.position>>=1,a.position==0&&(a.position=e,a.val=l(a.index++)),f|=(d>0?1:0)*s,s<<=1;S=c(f);break;case 1:for(f=0,n=Math.pow(2,16),s=1;s!=n;)d=a.val&a.position,a.position>>=1,a.position==0&&(a.position=e,a.val=l(a.index++)),f|=(d>0?1:0)*s,s<<=1;S=c(f);break;case 2:return""}for(t[3]=S,O=S,b.push(S);;){if(a.index>o)return"";for(f=0,n=Math.pow(2,v),s=1;s!=n;)d=a.val&a.position,a.position>>=1,a.position==0&&(a.position=e,a.val=l(a.index++)),f|=(d>0?1:0)*s,s<<=1;switch(S=f){case 0:for(f=0,n=Math.pow(2,8),s=1;s!=n;)d=a.val&a.position,a.position>>=1,a.position==0&&(a.position=e,a.val=l(a.index++)),f|=(d>0?1:0)*s,s<<=1;t[w++]=c(f),S=w-1,m--;break;case 1:for(f=0,n=Math.pow(2,16),s=1;s!=n;)d=a.val&a.position,a.position>>=1,a.position==0&&(a.position=e,a.val=l(a.index++)),f|=(d>0?1:0)*s,s<<=1;t[w++]=c(f),S=w-1,m--;break;case 2:return b.join("")}if(m==0&&(m=Math.pow(2,v),v++),t[S])L=t[S];else if(S===w)L=O+O.charAt(0);else return null;b.push(L),t[w++]=O+L.charAt(0),m--,O=L,m==0&&(m=Math.pow(2,v),v++)}}};return x}();typeof K!="undefined"&&K!=null&&(K.exports=le)});var de={};re(de,{createPlayground:()=>ce,getPlaygroundUrl:()=>Y});module.exports=ae(de);var W=ie(X());async function ce(c,h={}){var q;typeof c=="object"&&!(c instanceof HTMLElement)&&(c.headless||c.view==="headless")&&(h=c,c=null);let{config:E={},headless:p,loading:M="lazy",view:x}=h,o=p||x==="headless",e=null,l=null;if(typeof c=="string")e=document.querySelector(c);else if(c instanceof HTMLElement)e=c;else if(!(o&&typeof c=="object"))throw new Error("A valid container element is required.");if(!e)if(o)e=document.createElement("div"),B(e),document.body.appendChild(e);else throw new Error(`Cannot find element: "${c}"`);let t=new URL(Y(h)),r=t.origin;t.searchParams.set("embed","true"),t.searchParams.set("loading",o?"eager":M);let m=globalThis.process;t.searchParams.set("sdkVersion",((q=m==null?void 0:m.env)==null?void 0:q.SDK_VERSION)||"latest"),typeof E=="object"&&Object.keys(E).length>0&&t.searchParams.set("config","sdk");let w=h.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,J,N,Q,Z;if(!e)return;let g=e.dataset.height||e.style.height;if(g&&!o){let _=isNaN(Number(g))?g:g+"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"),(J=e.style).width||(J.width="100%"),(N=e.style).height||(N.height=e.style.height||"300px"),e.style.minHeight="200px",e.style.flexGrow="1",(Q=e.style).overflow||(Q.overflow="hidden"),(Z=e.style).resize||(Z.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?B(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,$;C.source!==u.contentWindow||C.origin!==r||((I=C.data)==null?void 0:I.type)!=="livecodes-get-config"||(removeEventListener("message",_),($=u.contentWindow)==null||$.postMessage({type:"livecodes-config",payload:E},r))}),u.onload=()=>{i(u)},u.src=t.href,A||e.appendChild(u)}),O=new Promise(i=>{addEventListener("message",function g(P){var A;P.source!==y.contentWindow||P.origin!==r||((A=P.data)==null?void 0:A.type)!=="livecodes-ready"||(removeEventListener("message",g),i(),O.settled=!0)})}),f=()=>v?Promise.reject(L):new Promise(async i=>{var P;O.settled&&i();let g={type:"livecodes-load"};(P=y.contentWindow)==null||P.postMessage(g,r),await O,i()}),d=(i,g)=>new Promise(async(P,A)=>{var j;if(v)return A(L);await f();let u=F();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:g},r)}),n={},s=["load","ready","code","console","tests","destroy"],S=(i,g)=>{var P;if(v)throw new Error(L);return s.includes(i)?(d("watch",[i]),n[i]||(n[i]=[]),(P=n[i])==null||P.push(g),{remove:()=>{var A,u;n[i]=(A=n[i])==null?void 0:A.filter(j=>j!==g),((u=n[i])==null?void 0:u.length)===0&&d("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 g=a((u=(A=i.data)==null?void 0:A.type)!=null?u:"");if(i.source!==y.contentWindow||i.origin!==r||!g||!n[g])return;let P=(j=i.data)==null?void 0:j.payload;(R=n[g])==null||R.forEach(U=>{U(P)})});let z=()=>{var i;Object.values(n).forEach(g=>{g.length=0}),(i=y==null?void 0:y.remove)==null||i.call(y),v=!0};M==="lazy"&&"IntersectionObserver"in window&&new IntersectionObserver((g,P)=>{g.forEach(async A=>{A.isIntersecting&&(await f(),P.unobserve(e))})},{rootMargin:"150px"}).observe(e);function B(i){i.style.position="absolute",i.style.top="0",i.style.visibility="hidden",i.style.opacity="0"}let F=()=>(String(Math.random())+Date.now().toFixed()).replace("0.","");return{load:()=>f(),run:()=>d("run"),format:i=>d("format",[i]),getShareUrl:i=>d("getShareUrl",[i]),getConfig:i=>d("getConfig",[i]),setConfig:i=>d("setConfig",[i]),getCode:()=>d("getCode"),show:(i,g)=>d("show",[i,g]),runTests:()=>d("runTests"),onChange:i=>S("code",i),watch:S,exec:(i,...g)=>d("exec",[i,...g]),destroy:()=>O.settled?d("destroy").then(z):v?Promise.reject(L):(z(),Promise.resolve())}}function Y(c={}){let{appUrl:h="https://livecodes.io",params:E={},config:p={},headless:M,import:x,lite:o,view:e,...l}=c,t;try{t=new URL(h)}catch(w){throw new Error(`${h} 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 m=c.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,W.compressToEncodedURIComponent)(JSON.stringify(p))));if(E&&typeof E=="object"&&Object.keys(E).length>0)try{r.set("params",(0,W.compressToEncodedURIComponent)(JSON.stringify(E)))}catch(w){Object.keys(E).forEach(v=>{t.searchParams.set(v,encodeURIComponent(String(E[v])))})}return x&&t.searchParams.set("x",encodeURIComponent(x)),m&&t.searchParams.set("headless","true"),t.hash=r.toString(),t.href}