UNPKG

livecodes

Version:

A Code Playground That Just Works!

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