UNPKG

livecodes

Version:

A Code Playground That Just Works!

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