ensojs 1.0.0-rc.8 → 1.0.0-rc.9
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/ensojs.es.js +1 -1
- package/package.json +1 -1
package/dist/ensojs.es.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const t=Symbol("enso.internal"),e=Symbol("enso.scheduleUpdate"),n=Symbol("enso.scheduleEffect"),s=Symbol("enso.attachTemplates"),r=Symbol("enso.markChanged"),o=Symbol("enso.addChild"),i=Symbol("enso.update"),c=Symbol("enso.getBinding"),a=Symbol("enso.setBinding"),h=Symbol("enso.taskList"),u=Symbol("enso.bindings"),d=Symbol("enso.children"),l=Symbol("enso.anchor"),p=Symbol("enso.nodes"),f=Symbol("enso.root"),m=Symbol("enso.env"),b="ENSO-FRAGMENT",g="data-enso-node",y="data-enso-root",w="data-enso-fragment",E=Object.freeze({Window:{},Document:{},eval:null,Function:null,setTimeout:null}),v=Object.freeze({parse:(t,...e)=>{let n=!1;const s=t.reduce((t,s,r)=>{const o=e[r];return!0===o&&(n=!0),t+s+(!0!==(i=o)&&!1!==i&&null!=i?o:"");var i},"");return!(!n||s)||s},...E}),A=(t={},e=v)=>Object.freeze(Object.assign(Object.create(e),t)),N=t=>`(()=>parse\`${t.replaceAll("{{","${").replaceAll("}}","}").trim()}\`)`;class S{#t;#e;constructor(t,e,n){this.#t=e;try{this.#e=n.createFunc(t)}catch(s){console.error("[Enso] Error instantiating effect:\n",s,n),this.#e=()=>{}}}get element(){return this.#t}get action(){return this.#e}run(){try{return this.#e?.()}catch(t){return void console.error("[Enso] Error running effect:\n",t,this.#e.toString())}}}class _{#n;#s;#r;#o;constructor(t,e={},n=S){this.#s=t,this.#n=n,this.#r=e,this.#o=(t=>{const e=(t=>`with (env) {\n return (() => {\n "use strict";\n try { return ${t}; } catch(e) {\n console.error('[Enso] Runtime error in effect:', e);\n return undefined;\n }\n })();\n }`)(t);try{return new Function("env",e)}catch(n){return console.error("[Enso] Error in effect: ",n,"\n",e),()=>()=>{}}})(t)}get data(){return this.#r}get code(){return this.#s}createEffect(t,e){return new this.#n(t,e,this)}createFunc(t){const{component:e,[m]:n}=t,s=this.#o.call(e,n);if("function"!=typeof s)throw new TypeError("[Enso] Expected function but got "+typeof s);return s}}const O=(e,n)=>{const s=class extends e{constructor(){super(t)}};if(!n)return s;const r=typeof n;if("object"!==r)throw new Error(`Component expected object litteral but got ${r}`);const o=Object.getOwnPropertyDescriptors(n);for(const t in o)Object.defineProperty(s.prototype,t,o[t]);return s},T=(t=Object)=>class extends t{#i;#c=/* @__PURE__ */new Set;#a=[];constructor(){super(),this[i]=this[i].bind(this),this[r]=this[r].bind(this)}get[u](){return this.#i}set[u](t){this.#i=t}get[h](){return this.#c}get[d](){return this.#a}[o](t){this.#a.push(t)}[c](t){return this.#i.get(t)}[a](t,e){const n=this[c](t);n&&(n.effects.push(e),n.changed=!0)}[n](t){this.#c.add(t)}[r](t){const s=this.#i.get(t);if(s&&!s.changed){s.changed=!0;for(const t of s.effects)this[n](t);this[e]()}for(const e of this.#a)e[r](t)}[i](){for(const e of this.#c)e.run();this.#c.clear();for(const e of this.#i.values())e.changed=!1;const t=[...this.#a];for(const e of t)e[i]()}},j=Object.freeze({mount:"lifecycle:mount",update:"lifecycle:update",unmount:"lifecycle:unmount"}),P=Object.values(j);class C extends(T(HTMLElement)){#h=!1;#u=null;#d=!1;#l;#p=/* @__PURE__ */Object.create(null);#f=A(this.expose);#m(){return this.shadowRoot??this.attachShadow({mode:this.settings.shadowMode})}constructor(e){if(super(),e!==t)throw new Error("Direct subclassing of Enso is not supported.\nUse Enso.component() instead.");this.#l=new this.constructor.WatchedClass(this),this[u]=this.#l[u],this.#u=this.settings.useShadow?this.#m():this}get refs(){return this.#p}get component(){return this}get isAttached(){return this.#h}get watched(){return this.#l}get[f](){return this.#u}get[m](){return this.#f}connectedCallback(){if(this.#h)return;this.#h=!0;const t=this.observedAttributes;for(const e of t)this.watched._defs[e].attribute.force&&this.reflectAttribute(e);this[s](this.template.process(this,this.template)),this.styles&&((t,e)=>{const n=t instanceof ShadowRoot?t:document;n.adoptedStyleSheets=[...n.adoptedStyleSheets,...e]})(this[f],this.styles),this[i]()}disconnectedCallback(){this.#l._notify(j.unmount)}attributeChangedCallback(t,e,n){if(e===n)return;const s=this.watched._defs[t].attribute.toProp(n);this.watched[t]!==s&&(this.watched[t]=s)}[s](t){const e=Array.from(t.firstElementChild.childNodes);this[f].append(...e),this.#l._notify(j.mount)}reflectAttribute(t){const e=this.#l._defs[t],n=e.attribute.toAttr(this.watched[t]);n!==this.getAttribute(e.name)&&(null===n?this.removeAttribute(e.name):this.setAttribute(e.name,n))}[e](){this.#d||(this.#d=!0,requestAnimationFrame(this[i]))}[i](){this.#d=!1,super[i](),this.#l._notify(j.update)}}const M=/* @__PURE__ */new WeakMap,k=/* @__PURE__ */new WeakMap,R=(t,e)=>({get(n,s,r){const o=Reflect.get(n,s,r);return"object"==typeof o&&null!==o&&o?D(o,e,t):o},set(n,s,r,o){const i=n[s],c=Reflect.set(n,s,r,o);return i!==r&&t(e),c},deleteProperty(n,s){const r=Object.prototype.hasOwnProperty.call(n,s),o=Reflect.deleteProperty(n,s);return r&&o&&t(e),o}}),L=["values","entries","keys",Symbol.iterator],x=(t,e,n)=>{const s=R(t,e);return{...s,get(r,o,i){const c=Reflect.get(r,o,i);return"function"==typeof c&&n.has(o)?(...n)=>{const s=c.apply(r,n);return t(e),s}:((t,e)=>"function"==typeof t[e]&&L.includes(e))(r,o)?r[o].bind(r):s.get(r,o,i)}}},W={Array:/* @__PURE__ */new Set(["push","pop","shift","unshift","splice","sort","reverse"]),Set:/* @__PURE__ */new Set(["add","delete","clear"]),Map:/* @__PURE__ */new Set(["set","delete","clear"])},D=(t,e,n)=>{if("object"!=typeof t||null===t)return t;if(M.has(t))return M.get(t);const s=((t,e,n)=>Array.isArray(t)?x(n,e,W.Array):t instanceof Set?x(n,e,W.Set):t instanceof Map?x(n,e,W.Map):R(n,e))(t,e,n),r=new Proxy(t,s);return M.set(t,r),k.set(r,!0),r},$=Object.freeze([Boolean,Number,String]),F=/* @__PURE__ */new Map([[Boolean,{toProp:t=>"false"!==t&&null!==t,toAttr:t=>t?"":null}],[Number,{toProp:t=>Number(t),toAttr:t=>null!==t?String(t):null}],[String,{toProp:t=>t,toAttr:t=>t}]]);function z(t){return"function"==typeof t?t:null===t||"object"!=typeof t?()=>t:()=>structuredClone(t)}const B=t=>Object.defineProperty(t,"_prop",{value:!0,writable:!1,configurable:!1,enumerable:!1}),H=(t=null,e=!1)=>(e=e&&null!==t&&"object"==typeof t,B({value:z(t),deep:e,attribute:!1})),I=(t=null,e=String)=>{const n=null!=t;if(n&&(e=t.constructor),!$.includes(e)||null!==t&&"object"==typeof t)throw new Error(`Unsupported attribute type '${e}'. Allowed: ${$.map(t=>t.name).join(", ")}`);const{toProp:s,toAttr:r}=F.get(e);return B({value:z(t),deep:!1,attribute:{type:e,force:n,toProp:s,toAttr:r}})};function q(t){return t.watched[V]}function X(t,e){t.watched._update(e)}function U(t,e,n=!1){return"function"==typeof t&&(t.__watches={props:e,keep:n}),t}const V=Symbol("enso.watched.values");function J(t){const e=/* @__PURE__ */Object.create(null);if(!t)return e;for(const[s,r]of(n=t,Object.entries(Object.getOwnPropertyDescriptors(n)))){const n=r.value;if(n?.__watches){for(const t of n.__watches.props)(e[t]||=[]).push(n);n.__watches.keep||delete t[s]}}var n;return e}const G=t=>{if(t.startsWith("_"))throw new Error("[Enso] - Watched property names must not start with '_'.")},Z=(t,e,n=[])=>{const s=e?._prop?e:H(e);return Object.assign({},s,{name:t,watchers:n})};class K{static define(t,e={}){const n=class extends K{};n.attr=[],n.defs=/* @__PURE__ */Object.create(null);for(const[s,r]of Object.entries(t)){G(s);const t=Z(s,r,e[s]);Object.defineProperty(n.prototype,t.name,{configurable:!0,enumerable:!0,get(){return this._getProp(t)},set(e){this._setProp(t,e)}}),n.defs[t.name]=t,t.attribute&&n.attr.push(t.name)}for(const s of P)n.defs[s]={name:s,value:()=>!1,attribute:!1,watchers:e[s]??[]};return Object.freeze(n.defs),Object.freeze(n.attr),n}#b;#g=/* @__PURE__ */new Map;#i=/* @__PURE__ */new Map;constructor(t){this.#b=t;for(const e in this._defs){const n=this._defs[e];let s=n.value();n.deep&&"object"==typeof s&&null!==s&&(s=D(s,n.name,t[r])),this.#g.set(e,s),this.#i.set(e,{changed:!1,watchers:n.watchers,effects:[]})}}get[u](){return this.#i}get[V](){return Object.fromEntries(this.#g)}get _defs(){return this.constructor.defs}_notify(t){if(this.#i.has(t)){const{watchers:e}=this.#i.get(t),n=this.#g.get(t);for(const s of e)s.call(this.#b,t,n)}}_getProp(t){return this.#g.get(t.name)}_setProp(t,e){t.deep&&"object"==typeof e&&null!==e&&(e=D(e,t.name,this.#b[r])),this.#g.set(t.name,e),this.#b[r](t.name),t.attribute&&this.#b.reflectAttribute(t.name),this._notify(t.name)}_update(t){for(const e in t)t[e]!==this.#g.get(e)&&this._setProp(this._defs[e],t[e])}}const Q=t=>{if(t instanceof HTMLTemplateElement)return t;"string"==typeof t&&(t=t.replace(/<script\b[^>]*>.*?<\/script>/gis,""));const e=document.createElement("template");return e.content.appendChild("string"==typeof t?(t=>document.createRange().createContextualFragment(t))(t):t),e},Y="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict",tt=(t=6)=>{let e="",n=Math.max(0,Math.trunc(t));for(;n--;)e+=Y[Math.trunc(64*Math.random())];return e},et=t=>t.nodeType===Node.TEXT_NODE?t.parentElement:t;class nt{#y;#w;#E;#v;#A=[];#N=[];#S=[];#_;#O=/* @__PURE__ */new Set;constructor(t,e,n){this.#y=t,this.#w=n,this.#E=e}get id(){return this.#y}get node(){return this.#E}get map(){return this.#w}get ref(){return this.#v}set ref(t){this.#v=t,this.markWatched()}addEvent(t,e){this.#N.push({name:t,action:e}),this.markWatched()}get events(){return this.#N}addAttribute(t,e,n){this.#A.push({name:t,action:e,binds:n}),this.markWatched()}get attributes(){return this.#A}addContent(t,e,n,s){this.#S.push({parent:t,index:e,action:n,binds:s}),this.markWatched()}get content(){return this.#S}setDirective({type:t=this.#_?.type,template:e=this.#_?.template,action:n=this.#_?.effect,binds:s=this.#_?.binds}={}){this.#_={type:t,template:e,action:n,binds:s}}get directive(){return this.#_}attachParser(t){this.#O.add(t)}get parsers(){return this.#O}markRoot(t=!1){this.#E?.setAttribute(y,t?this.#y:"")}unRoot(){this.#E?.removeAttribute(y)}isRoot(){return this.#E?.hasAttribute(y)}markWatched(t=!0){const e=et(this.#E);e.hasAttribute(g)||e.setAttribute(g,this.#y),t&&this.#w.add(this)}unWatch(){const t=et(this.#E);t?.removeAttribute(g)}isWatched(){return this.#E?.hasAttribute(g)}replaceNode(t){const e=this.#E;return this.#E.replaceWith(t),this.#E=t,this.markWatched(!1),e}}class st{#w=/* @__PURE__ */new Map;get size(){return this.#w.size}add(t){this.#w.has(t.id)||this.#w.set(t.id,t)}get(t){return this.#w.has(t)?this.#w.get(t):null}getByNode(t){if(!t?.hasAttribute(g))return null;const e=t.getAttribute(g);return this.#w.get(e)}getByRoot(t){if(!t?.hasAttribute(y))return null;const e=t.getAttribute(y);return this.#w.get(e)}create(t){const e=et(t).getAttribute(g);return this.#w.has(e)?this.#w.get(e):new nt(tt(),t,this)}createRoot(t){const e=new nt(tt(),t,this);return e.markRoot(!0),this.add(e),e}[Symbol.iterator](){return this.#w.values()}}const rt=(()=>{const t=[],e=[];return Object.freeze({registerNode(e){t.push(e)},registerAttr(t){e.push(t)},getNodeParser(e){for(const n of t)if(n.match(e))return n;return null},getAttrParser(t,n){for(const s of e)if(s.match(t,n))return s;return null},markRoot(t){t.setAttribute(y,"")},getRoot:t=>t.querySelector(`[${y}]:not([${y}="COMPONENT"])`),isParsed:t=>t.hasAttribute(w),getWatched:t=>t.querySelector(`[${g}]`),preprocess(t,e){let n=!1;const s=this.getNodeParser(e);if(s&&(n=s.preprocess(t,e)),e.attributes?.length){const s=[...e.attributes];for(const r of s){const s=this.getAttrParser(e,r);s&&(n=s.preprocess(t,e,r)||n)}}return n},process(t,e,n){for(const s of t.parsers)s.process(t,e,n);n.removeAttribute(g)}})})(),ot=(t,e=1)=>{const n=t.name;if(n.startsWith("enso-")){const t=n.indexOf(":");if(t>-1)return n.slice(t+1).toLowerCase()}return n.slice(e).toLowerCase()},it=(t,e,n=null)=>{if(!t?.name)return!1;const s=t.name;return!!s.startsWith(e)||!!n&&s.startsWith(`enso-${n}:`)},ct=/(?:this\.watched\.|watched:|@:)([A-Za-z_$][\w$]*)/g,at=(t,e=null)=>{const n=t.replace(ct,(t,n)=>(e&&e.add(n),`this.watched.${n}`));return e&&0===e.size&&e.add("lifecycle:mount"),n},ht=(t,e,s)=>{t[a](e,s),t[n](s)},ut=()=>document.createElement("template"),dt=(t,e,n)=>{if(!t)return null;const s=t.getAttributeNode(e)??t.getAttributeNode(n);return s&&t.removeAttribute(s.name),s?.value??null},lt=/({{(.|\n)*}})/;class pt extends S{run(){const t=super.run();this.element&&t&&(this.element.textContent=t)}}rt.registerNode({type:"text",match:t=>t.nodeType===Node.TEXT_NODE&<.test(t.nodeValue),preprocess(t,e){const n=/* @__PURE__ */new Set,s=N(at(e.nodeValue,n));return t.addContent(e.parentNode,((t,e)=>Array.prototype.indexOf.call(t.childNodes,e))(e.parentNode,e),new _(s,{},pt),n),t.attachParser(this),!0},process(t,e,n){if(t.content)for(const s of t.content){const t=n.childNodes[s.index],r=s.action.createEffect(e,t);for(const n of s.binds)ht(e,n,r)}}});class ft extends S{#T;constructor(t,e,n){super(t,e,n),this.#T=n.data.name}run(){const t=super.run(),e=this.element;t?e.setAttribute(this.#T,!0===t?"":t):e.removeAttribute(this.#T)}}function mt(t,e,n){const{name:s,action:r}=n;try{const n=r.createFunc(t);e.addEventListener(s.toLowerCase(),n.bind(t.component))}catch(o){console.error("[Enso] - ",o),e.addEventListener(s.toLowerCase(),()=>console.warn(`[Enso] Invalid handler for event '${s}'`))}}if(rt.registerAttr({type:"attr",match:(t,e)=>t.nodeType===Node.ELEMENT_NODE&&it(e,":","attr"),preprocess(t,e,n){const s=ot(n),r=/* @__PURE__ */new Set,o=N(at(n.value,r));return t.addAttribute(s,new _(o,{name:s},ft),r),e.removeAttribute(n.name),t.attachParser(this),!0},process(t,e,n){if(t.attributes)for(const s of t.attributes){const t=s.action.createEffect(e,n);for(const n of s.binds)ht(e,n,t)}}}),rt.registerAttr({type:"event",match:(t,e)=>t.nodeType===Node.ELEMENT_NODE&&it(e,"@","evt"),preprocess(t,e,n){const s=at(n.value);return t.addEvent(ot(n),new _(s)),e.removeAttribute(n.name),t.attachParser(this),!0},process(t,e,n){if(t.events?.length)for(const s of t.events)mt(e,n,s)}}),rt.registerAttr({type:"ref",match:(t,e)=>t.nodeType===Node.ELEMENT_NODE&&("#ref"===e.name||"enso-ref"===e.name),preprocess(t,e,n){return t.ref=n.value,e.removeAttribute(n.name),t.attachParser(this),!0},process(t,e,n){t.ref&&Object.defineProperty(e.component.refs,t.ref,{value:n,writable:!1,configurable:!1})}}),!customElements.get(b.toLowerCase())){class t extends HTMLElement{}customElements.define(b.toLowerCase(),t)}class bt extends(T()){#b;#j;#P;#C=null;#u=null;#f;#M=!1;constructor(t,e,n){super(),this.#b=t.component,this.#j=t,this.#f=t[m],n&&(this.#P=document.createComment(this.tag),n.replaceWith(this.#P)),t[o](this);const s=/* @__PURE__ */new Map;for(const[r,o]of this.#b[u])s.set(r,{changed:!1,watchers:o.watchers,effects:[]});this[u]=s,this._processTemplate(e)}_processTemplate(t){t&&(this.#u=t.process(this))}_getChildren(){this.#C=Array.from(this.#u.childNodes)}get tag(){return"enso:fragment"}get component(){return this.#b}get isAttached(){return this.#M}set isAttached(t){this.#M=t}get[l](){return this.#P}get[p](){return this.#C}get[m](){return this.#f}set[m](t){this.#f=A(t,this.#j[m])}get#k(){return this.#j.isAttached}[e](){this.#b[e]()}mount(){!this.#M&&this.#k&&(this._getChildren(),this.#P.after(...this.#C),this.#M=!0,this[i]())}[i](){this.#M&&super[i]()}unmount(){this.#M&&(this.#u.append(...this.#C),this.#C=null,this.#M=!1)}}class gt extends bt{#R;constructor(t,e,n,s){super(t,e,n),this.#R=s.createEffect(t,null)}get tag(){return"enso:if"}run(){this.#R.run()?this.mount():this.unmount()}}rt.registerNode({type:"if",match:t=>t.nodeType===Node.ELEMENT_NODE&&(t.hasAttribute("*if")||t.hasAttribute("enso-if")),preprocess(t,e){if(t.directive)return!1;let n=dt(e,"*if","enso-if");const s=/* @__PURE__ */new Set;n=at(n,s);const r=new _(N(n)),o=t.map.createRoot(e);return o.setDirective({type:"if",action:r,binds:s}),o.attachParser(this),!0},process(t,e,n){if("if"===t?.directive?.type){const s=new gt(e,t.directive.template,n,t.directive.action);for(const n of t.directive.binds)ht(e,n,s)}}});const yt=t=>t.includes(":")?yt(t.split(":")[1]):t.includes("=")?yt(t.split("=")[0]):t.trim(),wt=t=>{const[e]=t.split(/\b(?:of|in)\b(?!.*\b(of|in)\b)/).filter(Boolean),n=(t=>{const e={"{":"}","[":"]","}":"{","]":"["},n=[{token:"",capturing:!0}];let s=!0;const r=t=>{const r=n.at(-1).capturing&&s;n.push({token:e[t],capturing:r}),s=!0},o=t=>{if(t!==n.pop().token)throw new Error("mismatched brackets");s=!1},i={",":()=>{s=!0},"{":r,"[":r,"}":o,"]":o},c=t.split(/([{}[\],])/).map(t=>{if(t?.trim())return i[t]?i[t](t):n.at(-1).capturing?(s=!1,":"===t.trim().at(-1)&&(s=!0),yt(t)):void 0}).filter(Boolean);if(n.length>1)throw new Error("mismatched brackets");return c})(e);return n};class Et extends bt{constructor(t,e,n){super(t),this[m]=n,this._processTemplate(e)}mount(){return this.isAttached=!0,this[i](),this._getChildren(),this[p]}}class vt extends bt{#R;#L;constructor(t,e,n,s){super(t,null,n),this.#R=s.createEffect(t,null),this.#L=e}get tag(){return"enso:for"}run(){this.mount()}mount(){this.unmount();const t=[],e=this.#R.run();for(const n of e){const e=new Et(this,this.#L,n);t.push(...e.mount())}this[l].after(...t),this.isAttached=!0,this[i]()}unmount(){for(const t of this[d])t.unmount();this[d].length=0,this.isAttached=!1}}rt.registerNode({type:"for",match:t=>t.nodeType===Node.ELEMENT_NODE&&(t.hasAttribute("*for")||t.hasAttribute("enso-for")),preprocess(t,e){if(t.directive)return!1;const n=/* @__PURE__ */new Set,s=at(dt(e,"*for","enso-for"),n),r=wt(s),o=new _(`(function* () {\n try {\n for (const ${s}) {\n yield { ${r.join(", ")} };\n }\n } catch(e) {\n console.error('Runtime error in for loop:', e);\n }\n }).bind(this);/*js*/`);const i=t.map.createRoot(e);return i.setDirective({type:"for",action:o,binds:n}),i.attachParser(this),!0},process(t,e,n){if("for"===t?.directive?.type){const s=new vt(e,t.directive.template,n,t.directive.action);for(const n of t.directive.binds)ht(e,n,s)}}});const At=/{{[^]*}}/,Nt=/* @__PURE__ */new Set(["SCRIPT","PRE","CODE"]),St=t=>(t=>t.nodeType===Node.ELEMENT_NODE&&t.hasAttribute("enso:ignore"))(t)||(t=>t.parentNode?.hasAttribute("enso:ignore-children"))(t)||(t=>t.nodeType===Node.ELEMENT_NODE&&Nt.has(t.tagName))(t)?NodeFilter.FILTER_REJECT:t.nodeType===Node.ELEMENT_NODE||t.nodeType===Node.TEXT_NODE&&At.test(t.nodeValue)?NodeFilter.FILTER_ACCEPT:NodeFilter.FILTER_REJECT,_t=NodeFilter.SHOW_ELEMENT|NodeFilter.SHOW_TEXT;class Ot{#L=null;#l;constructor(t,e=new st){const n=Q(t);this.#l=e,this.#L=this.#x(n),this.#W()}#x(t){if(rt.isParsed(t))return t;const e=(t=>{const e=document.createElement(b);return e.append(t),e})(t.content);e.setAttribute(y,"COMPONENT"),t.content.append(e);const n=(t=>document.createTreeWalker(t,_t,{acceptNode:St}))(e);let s;for(;s=n.nextNode();){const t=this.#l.create(s);rt.preprocess(t,s)}return t.setAttribute(w,""),this.#l,t}#W(){const t=this.#L.content;let e;for((t=>{const e=t.querySelectorAll(`${b}:not([${y}])`);for(const n of e){const t=[...n.childNodes];0!==t.length?n.replaceWith(...t):n.remove()}})(t);e=rt.getRoot(t);){const t=this.#l.getByRoot(e);t.unRoot(),t.replaceNode(ut());const n=Q(e);n.setAttribute(w,""),t.directive.template=new Ot(n,this.#l,!0)}}process(t){const e=this.#L.content.cloneNode(!0);let n;for(;n=rt.getWatched(e);){const e=this.#l.getByNode(n);rt.process(e,t,n)}return e}clone(){const t=this.#L.cloneNode(!0);return new Ot(t,this.#l)}get watchedNodes(){return this.#l}get template(){return this.#L}}const Tt=(t,...e)=>"string"==typeof t?t:t.reduce((t,n,s)=>t+n+(e[s]||""),""),jt=(t,...e)=>(t=>{const e=new CSSStyleSheet;return e.replaceSync(t),e})(Tt(t,...e)),Pt=(t,...e)=>new Ot(Tt(t,...e)),Ct=Object.seal({version:"1.0.0-rc.8",component(t,{template:e,styles:n=null,expose:s={},watched:r={},script:o=null,settings:i={}}){if(i=((t={})=>({useShadow:!0,shadowMode:"open",...t}))(i),customElements.get(t))throw new Error(`[Enso] Component "${t}" is already defined. \n Did you load the component twice?`);const c=J(o),a=O(C,o),h=K.define(r,c);return n&&!Array.isArray(n)&&(n=[n]),Object.defineProperties(a,{observedAttributes:{get:()=>h.attr},WatchedClass:{get:()=>h}}),Object.defineProperties(a.prototype,{observedAttributes:{get:()=>h.attr},settings:{get:()=>i},template:{get:()=>e},styles:{get:()=>n},expose:{get:()=>s}}),customElements.define(t,a),a}});export{I as attr,jt as css,Ct as default,q as getWatched,Pt as html,j as lifecycle,H as prop,X as setWatched,U as watches};
|
|
1
|
+
const t=Symbol("enso.internal"),e=Symbol("enso.scheduleUpdate"),n=Symbol("enso.scheduleEffect"),r=Symbol("enso.attachTemplates"),s=Symbol("enso.markChanged"),o=Symbol("enso.addChild"),i=Symbol("enso.update"),c=Symbol("enso.getBinding"),a=Symbol("enso.setBinding"),h=Symbol("enso.taskList"),u=Symbol("enso.bindings"),d=Symbol("enso.children"),l=Symbol("enso.anchor"),p=Symbol("enso.nodes"),f=Symbol("enso.root"),m=Symbol("enso.env"),b="ENSO-FRAGMENT",g="data-enso-node",y="data-enso-root",w="data-enso-fragment",E=Object.freeze({Window:{},Document:{},eval:null,Function:null,setTimeout:null}),v=Object.freeze({parse:(t,...e)=>{let n=!1;const r=t.reduce((t,r,s)=>{const o=e[s];return!0===o&&(n=!0),t+r+(!0!==(i=o)&&!1!==i&&null!=i?o:"");var i},"");return!(!n||r)||r},...E}),A=(t={},e=v)=>Object.freeze(Object.assign(Object.create(e),t)),N=t=>`(()=>parse\`${t.replaceAll("{{","${").replaceAll("}}","}").trim()}\`)`;class S{#t;#e;constructor(t,e,n){this.#t=e;try{this.#e=n.createFunc(t)}catch(r){console.error("[Enso] Error instantiating effect:\n",r,n),this.#e=()=>{}}}get element(){return this.#t}get action(){return this.#e}run(){try{return this.#e?.()}catch(t){return void console.error("[Enso] Error running effect:\n",t,this.#e.toString())}}}class O{#n;#r;#s;#o;constructor(t,e={},n=S){this.#r=t,this.#n=n,this.#s=e,this.#o=(t=>{const e=(t=>`with (env) {\n return (() => {\n "use strict";\n try { return ${t}; } catch(e) {\n console.error('[Enso] Runtime error in effect:', e);\n return undefined;\n }\n })();\n }`)(t);try{return new Function("env",e)}catch(n){return console.error("[Enso] Error in effect: ",n,"\n",e),()=>()=>{}}})(t)}get data(){return this.#s}get code(){return this.#r}createEffect(t,e){return new this.#n(t,e,this)}createFunc(t){const{component:e,[m]:n}=t,r=this.#o.call(e,n);if("function"!=typeof r)throw new TypeError("[Enso] Expected function but got "+typeof r);return r}}const _=(e,n)=>{const r=class extends e{constructor(){super(t)}};if(!n)return r;const s=typeof n;if("object"!==s)throw new Error(`Component expected object litteral but got ${s}`);const o=Object.getOwnPropertyDescriptors(n);for(const t in o)Object.defineProperty(r.prototype,t,o[t]);return r},T=(t=Object)=>class extends t{#i;#c=/* @__PURE__ */new Set;#a=[];constructor(){super(),this[i]=this[i].bind(this),this[s]=this[s].bind(this)}get[u](){return this.#i}set[u](t){this.#i=t}get[h](){return this.#c}get[d](){return this.#a}[o](t){this.#a.push(t)}[c](t){return this.#i.get(t)}[a](t,e){const n=this[c](t);n&&(n.effects.push(e),n.changed=!0)}[n](t){this.#c.add(t)}[s](t){const r=this.#i.get(t);if(r&&!r.changed){r.changed=!0;for(const t of r.effects)this[n](t);this[e]()}for(const e of this.#a)e[s](t)}[i](){for(const e of this.#c)e.run();this.#c.clear();for(const e of this.#i.values())e.changed=!1;const t=[...this.#a];for(const e of t)e[i]()}},j=Object.freeze({mount:"lifecycle:mount",update:"lifecycle:update",unmount:"lifecycle:unmount"}),P=Object.values(j);class C extends(T(HTMLElement)){#h=!1;#u=null;#d=!1;#l;#p=/* @__PURE__ */Object.create(null);#f=A(this.expose);#m(){return this.shadowRoot??this.attachShadow({mode:this.settings.shadowMode})}constructor(e){if(super(),e!==t)throw new Error("Direct subclassing of Enso is not supported.\nUse Enso.component() instead.");this.#l=new this.constructor.WatchedClass(this),this[u]=this.#l[u],this.#u=this.settings.useShadow?this.#m():this}get refs(){return this.#p}get component(){return this}get isAttached(){return this.#h}get watched(){return this.#l}get[f](){return this.#u}get[m](){return this.#f}connectedCallback(){if(this.#h)return;this.#h=!0;const t=this.observedAttributes;for(const e of t)this.watched._defs[e].attribute.force&&this.reflectAttribute(e);this[r](this.template.process(this,this.template)),this.styles&&((t,e)=>{const n=t instanceof ShadowRoot?t:t.getRootNode();(n instanceof Document||n instanceof ShadowRoot)&&(n.adoptedStyleSheets=[...n.adoptedStyleSheets,...e])})(this[f],this.styles),this[i]()}disconnectedCallback(){this.#l._notify(j.unmount)}attributeChangedCallback(t,e,n){if(e===n)return;const r=this.watched._defs[t].attribute.toProp(n);this.watched[t]!==r&&(this.watched[t]=r)}[r](t){const e=Array.from(t.firstElementChild.childNodes);this[f].append(...e),this.#l._notify(j.mount)}reflectAttribute(t){const e=this.#l._defs[t],n=e.attribute.toAttr(this.watched[t]);n!==this.getAttribute(e.name)&&(null===n?this.removeAttribute(e.name):this.setAttribute(e.name,n))}[e](){this.#d||(this.#d=!0,requestAnimationFrame(this[i]))}[i](){this.#d=!1,super[i](),this.#l._notify(j.update)}}const R=t=>{if(t instanceof HTMLTemplateElement)return t;"string"==typeof t&&(t=t.replace(/<script\b[^>]*>.*?<\/script>/gis,""));const e=document.createElement("template");return e.content.appendChild("string"==typeof t?(t=>document.createRange().createContextualFragment(t))(t):t),e},M="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict",k=(t=6)=>{let e="",n=Math.max(0,Math.trunc(t));for(;n--;)e+=M[Math.trunc(64*Math.random())];return e},L=t=>t.nodeType===Node.TEXT_NODE?t.parentElement:t;class ${#b;#g;#y;#w;#E=[];#v=[];#A=[];#N;#S=/* @__PURE__ */new Set;constructor(t,e,n){this.#b=t,this.#g=n,this.#y=e}get id(){return this.#b}get node(){return this.#y}get map(){return this.#g}get ref(){return this.#w}set ref(t){this.#w=t,this.markWatched()}addEvent(t,e){this.#v.push({name:t,action:e}),this.markWatched()}get events(){return this.#v}addAttribute(t,e,n){this.#E.push({name:t,action:e,binds:n}),this.markWatched()}get attributes(){return this.#E}addContent(t,e,n,r){this.#A.push({parent:t,index:e,action:n,binds:r}),this.markWatched()}get content(){return this.#A}setDirective({type:t=this.#N?.type,template:e=this.#N?.template,action:n=this.#N?.effect,binds:r=this.#N?.binds}={}){this.#N={type:t,template:e,action:n,binds:r}}get directive(){return this.#N}attachParser(t){this.#S.add(t)}get parsers(){return this.#S}markRoot(t=!1){this.#y?.setAttribute(y,t?this.#b:"")}unRoot(){this.#y?.removeAttribute(y)}isRoot(){return this.#y?.hasAttribute(y)}markWatched(t=!0){const e=L(this.#y);e.hasAttribute(g)||e.setAttribute(g,this.#b),t&&this.#g.add(this)}unWatch(){const t=L(this.#y);t?.removeAttribute(g)}isWatched(){return this.#y?.hasAttribute(g)}replaceNode(t){const e=this.#y;return this.#y.replaceWith(t),this.#y=t,this.markWatched(!1),e}}class x{#g=/* @__PURE__ */new Map;get size(){return this.#g.size}add(t){this.#g.has(t.id)||this.#g.set(t.id,t)}get(t){return this.#g.has(t)?this.#g.get(t):null}getByNode(t){if(!t?.hasAttribute(g))return null;const e=t.getAttribute(g);return this.#g.get(e)}getByRoot(t){if(!t?.hasAttribute(y))return null;const e=t.getAttribute(y);return this.#g.get(e)}create(t){const e=L(t).getAttribute(g);return this.#g.has(e)?this.#g.get(e):new $(k(),t,this)}createRoot(t){const e=new $(k(),t,this);return e.markRoot(!0),this.add(e),e}[Symbol.iterator](){return this.#g.values()}}const W=(()=>{const t=[],e=[];return Object.freeze({registerNode(e){t.push(e)},registerAttr(t){e.push(t)},getNodeParser(e){for(const n of t)if(n.match(e))return n;return null},getAttrParser(t,n){for(const r of e)if(r.match(t,n))return r;return null},markRoot(t){t.setAttribute(y,"")},getRoot:t=>t.querySelector(`[${y}]:not([${y}="COMPONENT"])`),isParsed:t=>t.hasAttribute(w),getWatched:t=>t.querySelector(`[${g}]`),preprocess(t,e){let n=!1;const r=this.getNodeParser(e);if(r&&(n=r.preprocess(t,e)),e.attributes?.length){const r=[...e.attributes];for(const s of r){const r=this.getAttrParser(e,s);r&&(n=r.preprocess(t,e,s)||n)}}return n},process(t,e,n){for(const r of t.parsers)r.process(t,e,n);n.removeAttribute(g)}})})(),D=(t,e=1)=>{const n=t.name;if(n.startsWith("enso-")){const t=n.indexOf(":");if(t>-1)return n.slice(t+1).toLowerCase()}return n.slice(e).toLowerCase()},F=(t,e,n=null)=>{if(!t?.name)return!1;const r=t.name;return!!r.startsWith(e)||!!n&&r.startsWith(`enso-${n}:`)},z=/(?:this\.watched\.|watched:|@:)([A-Za-z_$][\w$]*)/g,B=(t,e=null)=>{const n=t.replace(z,(t,n)=>(e&&e.add(n),`this.watched.${n}`));return e&&0===e.size&&e.add("lifecycle:mount"),n},H=(t,e,r)=>{t[a](e,r),t[n](r)},I=()=>document.createElement("template"),q=(t,e,n)=>{if(!t)return null;const r=t.getAttributeNode(e)??t.getAttributeNode(n);return r&&t.removeAttribute(r.name),r?.value??null},X=/({{(.|\n)*}})/;class U extends S{run(){const t=super.run();this.element&&t&&(this.element.textContent=t)}}W.registerNode({type:"text",match:t=>t.nodeType===Node.TEXT_NODE&&X.test(t.nodeValue),preprocess(t,e){const n=/* @__PURE__ */new Set,r=N(B(e.nodeValue,n));return t.addContent(e.parentNode,((t,e)=>Array.prototype.indexOf.call(t.childNodes,e))(e.parentNode,e),new O(r,{},U),n),t.attachParser(this),!0},process(t,e,n){if(t.content)for(const r of t.content){const t=n.childNodes[r.index],s=r.action.createEffect(e,t);for(const n of r.binds)H(e,n,s)}}});class V extends S{#O;constructor(t,e,n){super(t,e,n),this.#O=n.data.name}run(){const t=super.run(),e=this.element;t?e.setAttribute(this.#O,!0===t?"":t):e.removeAttribute(this.#O)}}function J(t,e,n){const{name:r,action:s}=n;try{const n=s.createFunc(t);e.addEventListener(r.toLowerCase(),n.bind(t.component))}catch(o){console.error("[Enso] - ",o),e.addEventListener(r.toLowerCase(),()=>console.warn(`[Enso] Invalid handler for event '${r}'`))}}if(W.registerAttr({type:"attr",match:(t,e)=>t.nodeType===Node.ELEMENT_NODE&&F(e,":","attr"),preprocess(t,e,n){const r=D(n),s=/* @__PURE__ */new Set,o=N(B(n.value,s));return t.addAttribute(r,new O(o,{name:r},V),s),e.removeAttribute(n.name),t.attachParser(this),!0},process(t,e,n){if(t.attributes)for(const r of t.attributes){const t=r.action.createEffect(e,n);for(const n of r.binds)H(e,n,t)}}}),W.registerAttr({type:"event",match:(t,e)=>t.nodeType===Node.ELEMENT_NODE&&F(e,"@","evt"),preprocess(t,e,n){const r=B(n.value);return t.addEvent(D(n),new O(r)),e.removeAttribute(n.name),t.attachParser(this),!0},process(t,e,n){if(t.events?.length)for(const r of t.events)J(e,n,r)}}),W.registerAttr({type:"ref",match:(t,e)=>t.nodeType===Node.ELEMENT_NODE&&("#ref"===e.name||"enso-ref"===e.name),preprocess(t,e,n){return t.ref=n.value,e.removeAttribute(n.name),t.attachParser(this),!0},process(t,e,n){t.ref&&Object.defineProperty(e.component.refs,t.ref,{value:n,writable:!1,configurable:!1})}}),!customElements.get(b.toLowerCase())){class t extends HTMLElement{}customElements.define(b.toLowerCase(),t)}class G extends(T()){#_;#T;#j;#P=null;#u=null;#f;#C=!1;constructor(t,e,n){super(),this.#_=t.component,this.#T=t,this.#f=t[m],n&&(this.#j=document.createComment(this.tag),n.replaceWith(this.#j)),t[o](this);const r=/* @__PURE__ */new Map;for(const[s,o]of this.#_[u])r.set(s,{changed:!1,watchers:o.watchers,effects:[]});this[u]=r,this._processTemplate(e)}_processTemplate(t){t&&(this.#u=t.process(this))}_getChildren(){this.#P=Array.from(this.#u.childNodes)}get tag(){return"enso:fragment"}get component(){return this.#_}get isAttached(){return this.#C}set isAttached(t){this.#C=t}get[l](){return this.#j}get[p](){return this.#P}get[m](){return this.#f}set[m](t){this.#f=A(t,this.#T[m])}get#R(){return this.#T.isAttached}[e](){this.#_[e]()}mount(){!this.#C&&this.#R&&(this._getChildren(),this.#j.after(...this.#P),this.#C=!0,this[i]())}[i](){this.#C&&super[i]()}unmount(){this.#C&&(this.#u.append(...this.#P),this.#P=null,this.#C=!1)}}class Z extends G{#M;constructor(t,e,n,r){super(t,e,n),this.#M=r.createEffect(t,null)}get tag(){return"enso:if"}run(){this.#M.run()?this.mount():this.unmount()}}W.registerNode({type:"if",match:t=>t.nodeType===Node.ELEMENT_NODE&&(t.hasAttribute("*if")||t.hasAttribute("enso-if")),preprocess(t,e){if(t.directive)return!1;let n=q(e,"*if","enso-if");const r=/* @__PURE__ */new Set;n=B(n,r);const s=new O(N(n)),o=t.map.createRoot(e);return o.setDirective({type:"if",action:s,binds:r}),o.attachParser(this),!0},process(t,e,n){if("if"===t?.directive?.type){const r=new Z(e,t.directive.template,n,t.directive.action);for(const n of t.directive.binds)H(e,n,r)}}});const K=t=>t.includes(":")?K(t.split(":")[1]):t.includes("=")?K(t.split("=")[0]):t.trim(),Q=t=>{const[e]=t.split(/\b(?:of|in)\b(?!.*\b(of|in)\b)/).filter(Boolean),n=(t=>{const e={"{":"}","[":"]","}":"{","]":"["},n=[{token:"",capturing:!0}];let r=!0;const s=t=>{const s=n.at(-1).capturing&&r;n.push({token:e[t],capturing:s}),r=!0},o=t=>{if(t!==n.pop().token)throw new Error("mismatched brackets");r=!1},i={",":()=>{r=!0},"{":s,"[":s,"}":o,"]":o},c=t.split(/([{}[\],])/).map(t=>{if(t?.trim())return i[t]?i[t](t):n.at(-1).capturing?(r=!1,":"===t.trim().at(-1)&&(r=!0),K(t)):void 0}).filter(Boolean);if(n.length>1)throw new Error("mismatched brackets");return c})(e);return n};class Y extends G{constructor(t,e,n){super(t),this[m]=n,this._processTemplate(e)}mount(){return this.isAttached=!0,this[i](),this._getChildren(),this[p]}}class tt extends G{#M;#k;constructor(t,e,n,r){super(t,null,n),this.#M=r.createEffect(t,null),this.#k=e}get tag(){return"enso:for"}run(){this.mount()}mount(){this.unmount();const t=[],e=this.#M.run();for(const n of e){const e=new Y(this,this.#k,n);t.push(...e.mount())}this[l].after(...t),this.isAttached=!0,this[i]()}unmount(){for(const t of this[d])t.unmount();this[d].length=0,this.isAttached=!1}}W.registerNode({type:"for",match:t=>t.nodeType===Node.ELEMENT_NODE&&(t.hasAttribute("*for")||t.hasAttribute("enso-for")),preprocess(t,e){if(t.directive)return!1;const n=/* @__PURE__ */new Set,r=B(q(e,"*for","enso-for"),n),s=Q(r),o=new O(`(function* () {\n try {\n for (const ${r}) {\n yield { ${s.join(", ")} };\n }\n } catch(e) {\n console.error('Runtime error in for loop:', e);\n }\n }).bind(this);/*js*/`);const i=t.map.createRoot(e);return i.setDirective({type:"for",action:o,binds:n}),i.attachParser(this),!0},process(t,e,n){if("for"===t?.directive?.type){const r=new tt(e,t.directive.template,n,t.directive.action);for(const n of t.directive.binds)H(e,n,r)}}});const et=/{{[^]*}}/,nt=/* @__PURE__ */new Set(["SCRIPT","PRE","CODE"]),rt=t=>(t=>t.nodeType===Node.ELEMENT_NODE&&t.hasAttribute("enso:ignore"))(t)||(t=>t.parentNode?.hasAttribute("enso:ignore-children"))(t)||(t=>t.nodeType===Node.ELEMENT_NODE&&nt.has(t.tagName))(t)?NodeFilter.FILTER_REJECT:t.nodeType===Node.ELEMENT_NODE||t.nodeType===Node.TEXT_NODE&&et.test(t.nodeValue)?NodeFilter.FILTER_ACCEPT:NodeFilter.FILTER_REJECT,st=NodeFilter.SHOW_ELEMENT|NodeFilter.SHOW_TEXT;class ot{#k=null;#l;constructor(t,e=new x){const n=R(t);this.#l=e,this.#k=this.#L(n),this.#$()}#L(t){if(W.isParsed(t))return t;const e=(t=>{const e=document.createElement(b);return e.append(t),e})(t.content);e.setAttribute(y,"COMPONENT"),t.content.append(e);const n=(t=>document.createTreeWalker(t,st,{acceptNode:rt}))(e);let r;for(;r=n.nextNode();){const t=this.#l.create(r);W.preprocess(t,r)}return t.setAttribute(w,""),this.#l,t}#$(){const t=this.#k.content;let e;for((t=>{const e=t.querySelectorAll(`${b}:not([${y}])`);for(const n of e){const t=[...n.childNodes];0!==t.length?n.replaceWith(...t):n.remove()}})(t);e=W.getRoot(t);){const t=this.#l.getByRoot(e);t.unRoot(),t.replaceNode(I());const n=R(e);n.setAttribute(w,""),t.directive.template=new ot(n,this.#l,!0)}}process(t){const e=this.#k.content.cloneNode(!0);let n;for(;n=W.getWatched(e);){const e=this.#l.getByNode(n);W.process(e,t,n)}return e}clone(){const t=this.#k.cloneNode(!0);return new ot(t,this.#l)}get watchedNodes(){return this.#l}get template(){return this.#k}}const it=(t,...e)=>"string"==typeof t?t:t.reduce((t,n,r)=>t+n+(e[r]||""),""),ct=(t,...e)=>(t=>{const e=new CSSStyleSheet;return e.replaceSync(t),e})(it(t,...e)),at=(t,...e)=>new ot(it(t,...e)),ht=(t,e,n=null)=>{const r=(e="")=>{const r=(t=>{if(!t)return"";let e="";for(const[n,r]of Object.entries(t))if(!0===r)e+=` ${n}`;else{if(!1===r||null==r)continue;e+=` ${n}="${String(r)}"`}return e})(n);return e?`<${t}${r}>${e}</${t}>`:`<${t}${r}></${t}>`};function s(s,...o){if(!Array.isArray(s)||!s.raw){const r={...n||{},...s};return ht(t,e,r)}const i=String.raw(s,...o);return r(i)}return Object.defineProperties(s,{toString:{value:()=>r(),enumerable:!1},[Symbol.toPrimitive]:{value:()=>r(),enumerable:!1},Class:{get:()=>e,enumerable:!1},tag:{value:t,enumerable:!0},attrs:{value:n,enumerable:!1}}),Object.freeze(s)},ut=/* @__PURE__ */new WeakMap,dt=/* @__PURE__ */new WeakMap,lt=(t,e)=>({get(n,r,s){const o=Reflect.get(n,r,s);return"object"==typeof o&&null!==o&&o?bt(o,e,t):o},set(n,r,s,o){const i=n[r],c=Reflect.set(n,r,s,o);return i!==s&&t(e),c},deleteProperty(n,r){const s=Object.prototype.hasOwnProperty.call(n,r),o=Reflect.deleteProperty(n,r);return s&&o&&t(e),o}}),pt=["values","entries","keys",Symbol.iterator],ft=(t,e,n)=>{const r=lt(t,e);return{...r,get(s,o,i){const c=Reflect.get(s,o,i);return"function"==typeof c&&n.has(o)?(...n)=>{const r=c.apply(s,n);return t(e),r}:((t,e)=>"function"==typeof t[e]&&pt.includes(e))(s,o)?s[o].bind(s):r.get(s,o,i)}}},mt={Array:/* @__PURE__ */new Set(["push","pop","shift","unshift","splice","sort","reverse"]),Set:/* @__PURE__ */new Set(["add","delete","clear"]),Map:/* @__PURE__ */new Set(["set","delete","clear"])},bt=(t,e,n)=>{if("object"!=typeof t||null===t)return t;if(ut.has(t))return ut.get(t);const r=((t,e,n)=>Array.isArray(t)?ft(n,e,mt.Array):t instanceof Set?ft(n,e,mt.Set):t instanceof Map?ft(n,e,mt.Map):lt(n,e))(t,e,n),s=new Proxy(t,r);return ut.set(t,s),dt.set(s,!0),s},gt=Object.freeze([Boolean,Number,String]),yt=/* @__PURE__ */new Map([[Boolean,{toProp:t=>"false"!==t&&null!==t,toAttr:t=>t?"":null}],[Number,{toProp:t=>Number(t),toAttr:t=>null!==t?String(t):null}],[String,{toProp:t=>t,toAttr:t=>t}]]);function wt(t){return"function"==typeof t?t:null===t||"object"!=typeof t?()=>t:()=>structuredClone(t)}const Et=t=>Object.defineProperty(t,"_prop",{value:!0,writable:!1,configurable:!1,enumerable:!1}),vt=(t=null,e=!1)=>(e=e&&null!==t&&"object"==typeof t,Et({value:wt(t),deep:e,attribute:!1})),At=(t=null,e=String)=>{const n=null!=t;if(n&&(e=t.constructor),!gt.includes(e)||null!==t&&"object"==typeof t)throw new Error(`Unsupported attribute type '${e}'. Allowed: ${gt.map(t=>t.name).join(", ")}`);const{toProp:r,toAttr:s}=yt.get(e);return Et({value:wt(t),deep:!1,attribute:{type:e,force:n,toProp:r,toAttr:s}})};function Nt(t){return t.watched[_t]}function St(t,e){t.watched._update(e)}function Ot(t,e=[],n=!1){if(!(t=>"function"==typeof t&&void 0!==t.prototype)(t))throw new Error("[Enso] - Watches can only be applied to functions.");return t.__watches={props:e,keep:n},t}const _t=Symbol("enso.watched.values");function Tt(t){const e=/* @__PURE__ */Object.create(null);if(!t)return e;for(const[r,s]of(n=t,Object.entries(Object.getOwnPropertyDescriptors(n)))){const n=s.value;if(n?.__watches){for(const t of n.__watches.props)(e[t]||=[]).push(n);n.__watches.keep||delete t[r]}}var n;return e}const jt=t=>{if(t.startsWith("_"))throw new Error("[Enso] - Watched property names must not start with '_'.")},Pt=(t,e,n=[])=>{const r=e?._prop?e:vt(e);return Object.assign({},r,{name:t,watchers:n})};class Ct{static define(t,e={}){const n=class extends Ct{};n.attr=[],n.defs=/* @__PURE__ */Object.create(null);for(const[r,s]of Object.entries(t)){jt(r);const t=Pt(r,s,e[r]);Object.defineProperty(n.prototype,t.name,{configurable:!0,enumerable:!0,get(){return this._getProp(t)},set(e){this._setProp(t,e)}}),n.defs[t.name]=t,t.attribute&&n.attr.push(t.name)}for(const r of P)n.defs[r]={name:r,value:()=>!1,attribute:!1,watchers:e[r]??[]};return Object.freeze(n.defs),Object.freeze(n.attr),n}#_;#x=/* @__PURE__ */new Map;#i=/* @__PURE__ */new Map;constructor(t){this.#_=t;for(const e in this._defs){const n=this._defs[e];let r=n.value();n.deep&&"object"==typeof r&&null!==r&&(r=bt(r,n.name,t[s])),this.#x.set(e,r),this.#i.set(e,{changed:!1,watchers:n.watchers,effects:[]})}}get[u](){return this.#i}get[_t](){return Object.fromEntries(this.#x)}get _defs(){return this.constructor.defs}_notify(t){if(this.#i.has(t)){const{watchers:e}=this.#i.get(t),n=this.#x.get(t);for(const r of e)r.call(this.#_,t,n)}}_getProp(t){return this.#x.get(t.name)}_setProp(t,e){t.deep&&"object"==typeof e&&null!==e&&(e=bt(e,t.name,this.#_[s])),this.#x.set(t.name,e),this.#_[s](t.name),t.attribute&&this.#_.reflectAttribute(t.name),this._notify(t.name)}_update(t){for(const e in t)t[e]!==this.#x.get(e)&&this._setProp(this._defs[e],t[e])}}const Rt=Object.seal({version:"1.0.0-rc.9",component(t,{template:e,styles:n=null,expose:r={},watched:s={},script:o=null,settings:i={}}){if(i=((t={})=>({useShadow:!0,shadowMode:"open",...t}))(i),customElements.get(t))throw new Error(`[Enso] Component "${t}" is already defined. \n Did you load the component twice?`);const c=Tt(o),a=_(C,o),h=Ct.define(s,c);return n&&!Array.isArray(n)&&(n=[n]),Object.defineProperties(a,{observedAttributes:{get:()=>h.attr},WatchedClass:{get:()=>h}}),Object.defineProperties(a.prototype,{observedAttributes:{get:()=>h.attr},settings:{get:()=>i},template:{get:()=>e},styles:{get:()=>n},expose:{get:()=>r}}),customElements.define(t,a),ht(t,a)}});export{At as attr,ct as css,Rt as default,Nt as getWatched,at as html,j as lifecycle,vt as prop,St as setWatched,Ot as watches};
|
package/package.json
CHANGED