2025-06-12 09:37:26 +08:00

2 lines
13 KiB
JavaScript

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