sigment 1.1.2 → 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +4 -4
- package/package.json +1 -1
- package/types/index.d.ts +25 -0
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var
|
|
1
|
+
var I=Object.defineProperty;var c=(e,t)=>I(e,"name",{value:t,configurable:!0});var y={cleanHtml:B,cleanhtml:!0,setMaxCacheSize:U,maxCacheSize:10},g={funcname:[],dataMap:new Map},S=null,x={},M=new Map,v=new Map,J=null,E=null,$=new Map,w={innerHTMLElements:["div","p","span","h1","h2","h3","h4","h5","h6","a","ul","ol","li","table","tr","td","th","thead","tbody","tfoot","form","label","button","section","article","header","footer","nav","aside","main","figure","figcaption","blockquote","cite","code","pre","em","strong","i","b","u","s","small","mark","time","abbr","q","dl","dt","dd","fieldset","legend","textarea","output","progress","meter","fragment","details","summary","dialog","menu","slot","template"],valueElements:["input","textarea","select","button","option","progress","meter"],srcElements:["img","script","iframe","audio","video","source","track","embed","frame","input"],hrefElements:["a","link","area","base"],voidElements:["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr","frame"]};Object.values(w).flat().forEach(e=>{globalThis[e]=(...t)=>T(e,...t)});var G=new Set(w.valueElements),te=new Set(w.srcElements),ne=new Set(w.hrefElements),V=new Set(w.voidElements);function oe(e,t){x[e]||(x[e]=_(t))}c(oe,"createGlobalSignal");function re(e){return x[e]}c(re,"getGlobalSignal");function z(e,t){g.dataMap.has(e)&&g.dataMap.delete(e),g.dataMap.set(e,t)}c(z,"addEntry");function C(e){k(C.name);let t=g.dataMap.get(e);return t?t.value:null}c(C,"gve");function ie(e,t,n){let r;if(t instanceof HTMLElement?r=t.outerHTML:typeof t=="object"?r=JSON.stringify(t):r=t.toString(),r.length>4096){console.error("Cookie size exceeds 4KB limit. Data not stored.");return}document.cookie=e+"="+r+"; max-age="+n+" ; path=/ ;",setTimeout(()=>{document.cookie="testCookie=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/;"},n*1e3)}c(ie,"setCookie");function le(e){let n=`; ${document.cookie}`.split(`; ${e}=`);if(n.length===2){let r=n.pop().split(";").shift();try{let i=JSON.parse(r);return typeof i=="object"&&i!==null&&"nodeType"in i&&i.nodeType===1,i}catch{return r}}return null}c(le,"getCookie");function se(e,t){let n;t instanceof HTMLElement?n=t.outerHTML:typeof t=="object"?n=JSON.stringify(t):n=t.toString(),localStorage.setItem(e,n)}c(se,"setStorage");function ce(e){let t=localStorage.getItem(e);if(t!==null)try{let n=JSON.parse(t);return typeof n=="object"&&n!==null&&"nodeType"in n&&n.nodeType===1,n}catch{return t}}c(ce,"getStorage");function F(e,t=void 0){k(F.name),g.createInstance=F.name;let n="";t===void 0?n=e():n=e(t)}c(F,"createInstance");function H(e,t){k(H.name),g.rpc=H.name;let n={};for(let[r,i]of Object.entries(t))e[r]?n[r]=e[r].apply(null,i):(console.error(`Function ${r} not found!`),n[r]=null);return n}c(H,"rpc");function D(e,t=!1,n){k(D.name),g.gvec=D.name;let r=Object.keys(e),i=r[0],s=r[1],o=r[2];var f=e[i];f=f.name;var u=e[s];let d=e[s];var m=C(s)===null;if(m||t===!1){var h=""+s;g.dataMap.delete(h),F(u,e[o])}u=C(s);var l=C(i);if(l===null)throw new Error(` source id '${i} is not found in source component `);if(u===null)throw new Error(` target id '${s} is not found in target component `);if(l.replaceWith(u),n!==void 0)for(let b in d){let O=b.toString();if(typeof n=="object"){for(var a in n)if(O===a){var p=n[a];typeof p=="object"?d[b].apply(null,p):d[b].apply(null,[])}}else if(n.indexOf(",">-1)){let j=n.split(",");for(let L=0;L<j.length;L++)O===j[L]&&d[b].apply(null,[])}else O===n&&d[b].apply(null,[])}}c(D,"gvec");async function ae(e,t={}){let{passRestriction:n=[]}=t,r=window.location.pathname.replace(/^\/|\/$/g,"").toLowerCase(),i=e.replace(/^\/|\/$/g,"").toLowerCase();if(!new RegExp("^"+i.replace(/:[^\/]+/g,"[^/]+")+"$").test(r))return!1;let o=Array.isArray(n)?n:[n];for(let f of o)try{let u=typeof f=="function"?f():f;if(u instanceof Promise){if(!await u)return!1}else if(!u)return!1}catch(u){return console.error("Route check failed:",u),!1}return!0}c(ae,"Route");function fe(e){return e==null||e===""}c(fe,"IsEmpty");function K(e){window.history.pushState(e,e,e)}c(K,"Navigate");function U(e){y.maxCacheSize=e}c(U,"setMaxCacheSize");function B(e){y.cleanhtml=e}c(B,"cleanHtml");async function W(e,t){let n;t==="/"?n=t:n=t.replace(/\//g,"").toLowerCase();let r=Object.keys(e).find(i=>i.toLowerCase()===n.toLowerCase());if(!r){let i=Object.keys(e).join(", ");throw new Error(`Unknown component: "${t}".
|
|
2
2
|
Available components: ${i}
|
|
3
3
|
|
|
4
4
|
To add this component:
|
|
@@ -6,6 +6,6 @@ To add this component:
|
|
|
6
6
|
const loadAsyncComponents = {
|
|
7
7
|
...existingComponents,
|
|
8
8
|
"${t}": () => import('./${t}.js')
|
|
9
|
-
};`)}if(v.has(
|
|
10
|
-
`);for(let
|
|
11
|
-
`);if(
|
|
9
|
+
};`)}if(v.has(r))return v.get(r);try{let s=(await e[r]()).default;return v.set(r,s),s}catch(i){throw new Error(`Failed to load component "${r}": ${i.message}`)}}c(W,"loadFunc");async function ue(e,t,n,r){let i,s,o=Object.keys(t);if(typeof n=="string")i=n,s=!!r;else if(typeof n=="boolean"){if(o.length===1)i=o[0];else if(o.includes(e))i=e;else throw new Error(`Ambiguous component map: multiple components exist and no matching key for path '${e}' found. Provide component key explicitly.`);s=n}else{if(o.length===1)i=o[0];else if(o.includes(e))i=e;else throw new Error(`Ambiguous component map: multiple components exist and no matching key for path '${e}' found. Provide component key explicitly.`);s=!1}let f=await W(t,i),u=$.get(e);(!u||!s)&&(u=f(),$.set(e,u)),E&&E.isConnected?E.replaceWith(u):document.body.appendChild(u),E=u,J=e,K(e)}c(ue,"NavigateTo");function de(e){if(e){let t=e.toLowerCase(),n=!1;for(let r of v.keys())r.toLowerCase()===t&&(v.delete(r),n=!0);return n}else return v.clear(),!0}c(de,"clearComponentCache");function pe(e){var f,u;let n=window.location.pathname.split("/").filter(Boolean),r=n[0]||"home",i={},s=(u=(f=e[r])==null?void 0:f.urlParam)==null?void 0:u.split("/");return s&&s.length&&s.forEach((d,m)=>{d=d.replace(/[{}]/g,""),n[m+1]&&(i[d]=n[m+1])}),new URL(window.location.href).searchParams.forEach((d,m)=>{i[m]=d}),{componentName:r,params:i}}c(pe,"parsePath");function A(e){return new DOMParser().parseFromString(e,"text/html").body.firstChild}c(A,"parseHTML");async function me(e,t,n=null,r=null){let i=e[t.toLowerCase()];if(i||(typeof e.fallback=="string"?i=e[e.fallback]:i=e.fallback),!i)return A("<h1>404 - Component not found</h1>");let{loader:s,guard:o,cacheExpiration:f=null,...u}=i;if(r===null&&typeof i.logic=="function"&&(r=i.logic),typeof o=="function"&&!await o(n))return A("<h1>403 - Forbidden</h1><p>You do not have access to this page.</p>");for(let d in u){let m=u[d];if(typeof m=="function")try{await m()}catch(h){console.error(h)}}try{let d=Date.now(),m=v.get(t);m&&f!==null&&d-m.cachedAt>f&&(v.delete(t),m=null);let h;if(m)h=m.component,v.set(t,{component:h,cachedAt:d});else{if(h=(await s()).default,v.size>=y.maxCacheSize){let b=v.keys().next().value;v.delete(b)}v.set(t,{component:h,cachedAt:d})}if(typeof h!="function")return A("<h1>Invalid component</h1>");let l;Array.isArray(n)?l=h(...n):typeof n=="object"&&n!==null?l=h(n):l=h();let a="/";try{a=window.location.pathname+window.location.hash,a===""&&(a="/")}catch{a=t}return $.set(a,l),E=l,l}catch(d){return console.error(d),A("<h1>Error loading component</h1>")}}c(me,"loadRunFunc");function N(e=""){var i,s;k(N.name),g.buildRoadMap=N.name;var t="";let n=(i=new Error().stack)==null?void 0:i.toString();navigator.userAgent.toLowerCase().indexOf("firefox")>0?n=n.split("@"):n=n.split(`
|
|
10
|
+
`);for(let o=0;o<n.length;o++){var r="";if(navigator.userAgent.indexOf("Firefox")>0){let f=n[o].lastIndexOf(`
|
|
11
|
+
`);if(f===-1)continue;r=n[o].slice(f+1),r===""?(f=n[o].lastIndexOf("/")+1,f>-1&&(r=n[o].substr(f),r=r.substr(0,r.indexOf(".")))):r.indexOf("/")>-1&&(r=r.substr(0,r.indexOf("/")))}else r=(s=n[o])==null?void 0:s.trim().split(" ")[1];if(!g.funcname.includes(r)&&!(r===void 0||r==="")&&!(r.indexOf("http://")>-1||r.indexOf("https://")>-1)){if(g.gvec!==void 0&&r.indexOf(g.gvec)>-1)break;t.indexOf("|"+r)>-1||(t+="|"+r.replace(".","|"))}}return e+t}c(N,"buildRoadMap");function k(e){!g.funcname.includes(e)&&g.funcname.push(e)}c(k,"setFuncName");function _(e){let t=e,n=new Set;function r(){return S&&n.add(S),t}c(r,"get");function i(s,o=!1){(o||t!==s)&&(t=s,n.forEach(f=>f()))}return c(i,"set"),[r,i]}c(_,"createSignal");function P(e){function t(){S=t,e(),S=null}c(t,"wrapped"),t()}c(P,"createEffect");function q(e){e=e.toLowerCase(),w.innerHTMLElements.includes(e)||(w.innerHTMLElements.push(e),globalThis[e]=(...t)=>T(e,...t))}c(q,"createTypeElement");function Y(e){e=e.toLowerCase(),w.innerHTMLElements.includes(e)||(w.innerHTMLElements.push(e),globalThis[e]=(...t)=>T(e,...t))}c(Y,"createSigment");function he(e){Y(e)}c(he,"addSigment");function ye(e,...t){if(q(e),t.length!==0)return globalThis[e](...t)}c(ye,"createElement");function Q(e){let t=document.createTextNode("");return P(()=>{if(typeof x!="object"||x===null){t.textContent=e;return}let n=e.replace(/{{(.*?)}}/g,(r,i)=>{let s=i.trim();if(x.hasOwnProperty(s)){let o=x[s];if(o&&typeof o[0]=="function")return o[0]()??""}return""});t.textContent=n}),t}c(Q,"interpolateReactiveText");function ge(e,t=5e3,n={}){let r=JSON.stringify([e,n,t]);if(!M.has(r)){let i=Z(async()=>{let s=await fetch(e,n);if(!s.ok)throw new Error(`Fetch failed: ${s.status}`);return s.json()},t);M.set(r,i)}return M.get(r)()}c(ge,"fetchCache");function R(e){if(Array.isArray(e))return e.map(R);if(e&&typeof e=="object"){let t={};return Object.keys(e).sort().forEach(n=>{t[n]=R(e[n])}),t}return e}c(R,"sortObjectDeep");function X(e){if(e&&typeof e=="object"&&!Array.isArray(e)){let t={};return Object.keys(e).sort().forEach(n=>{t[n]=e[n]}),t}return e}c(X,"sortObjectShallow");function Z(e,t=5e3,n={}){let r=new Map,i=c((...s)=>{let{deepSort:o}=n,f;o===!0?f=s.map(l=>l&&typeof l=="object"?R(l):l):o===!1?f=s.map(l=>l&&typeof l=="object"?X(l):l):f=s;let u=JSON.stringify(f),d=Date.now(),m=r.get(u);if(m&&d-m.timestamp<t)return m.value;let h=e(...s);return r.set(u,{value:h,timestamp:d}),h instanceof Promise&&h.catch(()=>r.delete(u)),h},"wrapped");return i.clear=()=>r.clear(),i}c(Z,"memoizeFunc");function T(e,...t){let n={},r=[];k(T.name);var i="";i=N();var s=i.split("|");if(s.includes(g.rpc))return;t[0]&&typeof t[0]=="object"&&!Array.isArray(t[0])&&!(t[0]instanceof Node)?(n=t[0],r=t.slice(1)):r=t;let o=null,f=e.toLowerCase()==="fragment";f?o=document.createDocumentFragment():o=document.createElement(e);let u=V.has(e),d=G.has(e),m=e==="input"||e==="option",h=e==="option";if(!f)for(let l in n){let a=n[l];l.startsWith("on")&&typeof a=="function"?o.addEventListener(l.slice(2).toLowerCase(),a):typeof a=="function"&&l!=="children"?P(()=>{let p=a();p==null||p===!1?(o.removeAttribute(l),l==="value"&&d&&(o.value="",y.cleanhtml||o.removeAttribute("value")),l==="checked"&&m&&(o.checked=!1,y.cleanhtml||o.removeAttribute("checked")),l==="selected"&&h&&(o.selected=!1,y.cleanhtml||o.removeAttribute("selected")),l==="id"&&(o.iid="",y.cleanhtml||o.removeAttribute("id"))):l==="id"?(o.iid=p,y.cleanhtml?o.setAttribute("id",p):o.removeAttribute("id"),z(p,{name:p,caller:p+i,value:o})):l==="value"&&d?(o.value=p,y.cleanhtml?o.setAttribute("value",p):o.removeAttribute("value")):l==="checked"&&m?(o.checked=p,y.cleanhtml&&p?o.setAttribute("checked",""):o.removeAttribute("checked")):l==="selected"&&h?(o.selected=p,y.cleanhtml&&p?o.setAttribute("selected",""):o.removeAttribute("selected")):o.setAttribute(l,p)}):a!=null&&a!==!1&&(l==="id"?(o.iid=a,y.cleanhtml?o.setAttribute("id",a):o.removeAttribute("id"),z(a,{name:a,caller:a+i,value:o})):l==="value"&&d?(o.value=a,y.cleanhtml?o.setAttribute("value",a):o.removeAttribute("value")):l==="checked"&&m?(o.checked=a,y.cleanhtml?o.setAttribute("checked",""):o.removeAttribute("checked")):l==="selected"&&h?(o.selected=a,y.cleanhtml?o.setAttribute("selected",""):o.removeAttribute("selected")):o.setAttribute(l,a))}return u?r.length===1&&(typeof r[0]=="string"||typeof r[0]=="number")?d&&(o.value=r[0],y.cleanhtml?o.setAttribute("value",r[0]):o.removeAttribute("value")):r.length>0&&console.warn(`Void element <${e}> should not have children.`):r.flat().forEach(l=>{if(typeof l=="function"){let a=document.createTextNode("");o.appendChild(a),P(()=>{let p=l();if(p instanceof Node){let b=a.parentNode;if(!b)return;b.nodeType===Node.DOCUMENT_FRAGMENT_NODE?b.replaceChildren(p):b.replaceChild(p,a),a=p}else{let b=p==null?"":String(p);a.textContent!==b&&(a.textContent=b)}})}else if(typeof l=="string"&&l.includes("{{")&&!l.includes("function"))o.appendChild(Q(l));else if(typeof l=="string"||typeof l=="number"){if(l.includes("function get(")||l.includes("function (")||l.includes("[native code]"))throw new Error("Detected reactive getter function used inside a template string. Use a function instead like: div(() => `Your text is: ${someState()}`)");o.appendChild(document.createTextNode(l))}else(l instanceof DocumentFragment||l instanceof Node)&&o.appendChild(l)}),o}c(T,"h");export{fe as IsEmpty,y as MyApp,K as Navigate,ue as NavigateTo,ae as Route,he as addSigment,de as clearComponentCache,P as createEffect,ye as createElement,oe as createGlobalSignal,Y as createSigment,_ as createSignal,ge as fetchCache,le as getCookie,re as getGlobalSignal,ce as getStorage,C as getVirtualElementById,oe as globalSignal,C as gve,D as gvec,T as h,W as loadFunc,me as loadRunFunc,Z as memoizeFunc,pe as parsePath,H as rpc,ie as setCookie,se as setStorage,_ as signal,re as useGlobalSignal};
|
package/package.json
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -12,6 +12,31 @@ export declare const MyApp: {
|
|
|
12
12
|
|
|
13
13
|
|
|
14
14
|
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
type ComponentLoader = () => Promise<{ default: (...args: any[]) => HTMLElement }>;
|
|
18
|
+
|
|
19
|
+
type ComponentMap = {
|
|
20
|
+
[key: string]: ComponentLoader;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Navigate to a path and load/render a component.
|
|
25
|
+
*
|
|
26
|
+
* @param path - The URL path or route to navigate to.
|
|
27
|
+
* @param componentMap - Map of component keys to async loaders.
|
|
28
|
+
* @param componentKeyOrStateful - Either the key of the component to load, or a boolean indicating if stateful rendering is enabled.
|
|
29
|
+
* @param maybeStateful - Optional boolean, used if componentKey is provided explicitly as third param.
|
|
30
|
+
* @returns Promise<void>
|
|
31
|
+
*/
|
|
32
|
+
export function NavigateTo(
|
|
33
|
+
path: string,
|
|
34
|
+
componentMap: ComponentMap,
|
|
35
|
+
componentKeyOrStateful?: string | boolean,
|
|
36
|
+
maybeStateful?: boolean
|
|
37
|
+
): Promise<void>;
|
|
38
|
+
|
|
39
|
+
|
|
15
40
|
/**
|
|
16
41
|
* Creates a reactive effect that runs the given function and tracks dependencies.
|
|
17
42
|
*/
|