@dlightjs/dlight
Advanced tools
Comparing version 1.0.0-beta.0 to 1.0.0-beta.2
@@ -1,2 +0,2 @@ | ||
import{Store as N}from"@dlightjs/store";"global"in N||(typeof window<"u"?N.global=window:typeof global<"u"?N.global=global:N.global={});"document"in N||typeof document<"u"&&(N.document=document);var r={...N,delegatedEvents:new Set};function B(i){r.global=i}function C(i){r.document=i}var c={Comp:0,For:1,Cond:2,Env:3,Exp:4,Subview:5},u=class{_$dlNodeType;constructor(t){this._$dlNodeType=t}get _$el(){return u.toEls(this._$nodes)}static toEls(t){let e=[];return this.loopShallowEls(t,n=>{e.push(n)}),e}static loopShallowEls(t,e){t.forEach(n=>{if(!("_$dlNodeType"in n))return e(n);n._$nodes&&u.loopShallowEls(n._$nodes,e)})}static addParentEl(t,e){t.forEach(n=>{"_$dlNodeType"in n&&(n._$parentEl=e,n._$nodes&&u.addParentEl(n._$nodes,e))})}static getFlowIndexFromNodes(t,e){let n=0,s=[...t].reverse();for(;s.length>0;){let o=s.pop();if(o===e)break;"_$dlNodeType"in o?o._$nodes&&s.push(...[...o._$nodes].reverse()):n++}return n}static appendNodesWithSibling(t,e,n){return n?this.insertNodesBefore(t,e,n):this.appendNodes(t,e)}static appendNodesWithIndex(t,e,n,s){return s=s??e.childNodes.length,s!==n?this.insertNodesBefore(t,e,e.childNodes[n]):this.appendNodes(t,e)}static insertNodesBefore(t,e,n){let s=0;return this.loopShallowEls(t,o=>{e.insertBefore(o,n),s++}),s}static appendNodes(t,e){let n=0;return this.loopShallowEls(t,s=>{e.appendChild(s),n++}),n}static addWillUnmount(t,e){let n=r.global.WillUnmountStore,s=n[n.length-1];s&&s.push(e.bind(null,t))}static addDidUnmount(t,e){let n=r.global.DidUnmountStore,s=n[n.length-1];s&&s.push(e.bind(null,t))}static addDidMount(t,e){r.global.DidMountStore||(r.global.DidMountStore=[]),r.global.DidMountStore.push(e.bind(null,t))}static runDidMount(){let t=r.global.DidMountStore;if(!(!t||t.length===0)){for(let e=t.length-1;e>=0;e--)t[e]();r.global.DidMountStore=[]}}};function I(i,t){Object.assign(i.style,t)}function P(i,t){Object.assign(i.dataset,t)}function W(i,t,e){let n=`$${t}`;n in i&&i[n]===e||(i[t]=e,i[n]=e)}function V(i,t){Object.entries(t).forEach(([e,n])=>{if(e==="style")return I(i,n);if(e==="dataset")return P(i,n);W(i,e,n)})}function T(i,t,e){let n=`$${t}`;n in i&&i[n]===e||(i.setAttribute(t,e),i[n]=e)}function z(i,t){Object.entries(t).forEach(([e,n])=>{T(i,e,n)})}function q(i,t,e){let n=i[`$on${t}`];n&&i.removeEventListener(t,n),i.addEventListener(t,e),i[`$on${t}`]=e}function G(i){let t=`$$${i.type}`;for(let e of i.composedPath())if(e[t]&&e[t](i),i.cancelBubble)return}function et(i,t,e){i[`$$${t}`]!==e&&(i[`$$${t}`]=e,r.delegatedEvents.has(t)||(r.delegatedEvents.add(t),r.document.addEventListener(t,G)))}function nt(i){return r.document.createElement(i)}function y(i,t,e){i._$nodes||(i._$nodes=Array.from(i.childNodes)),i._$nodes.splice(e,0,t);let n=u.getFlowIndexFromNodes(i._$nodes,t);u.appendNodesWithIndex([t],i,n),u.addParentEl([t],i)}function L(i,t,e){if(t==="style")return I(i,e);if(t==="dataset")return P(i,e);if(t!=="element"){if(t==="prop")return V(i,e);if(t==="attr")return z(i,e);if(t==="innerHTML")return W(i,"innerHTML",e);if(t!=="forwardProp"){if(t.startsWith("on"))return q(i,t.slice(2).toLowerCase(),e);T(i,t,e)}}}var _=class extends u{constructor(){super(c.Comp)}_$init(t,e,n,s){this._$notInitd=!0,s&&s._$addForwardProps(this),e!==null&&this._$setContent(e),t&&Object.entries(t).forEach(([o,a])=>{this._$setProp(o,a)}),n&&(this._$children=n),r.global.DLEnvStore&&Object.entries(r.global.DLEnvStore.envs).forEach(([o,[a,p]])=>{`$e$${o}`in this&&(p.addNode(this),this._$initEnv(o,a,p))}),this._$callUpdatesBeforeInit(),this.didMount&&u.addDidMount(this,this.didMount.bind(this)),this.willUnmount&&u.addWillUnmount(this,this.willUnmount.bind(this)),this.didUnmount&&u.addDidUnmount(this,this.didUnmount.bind(this)),this.willMount?.(),this._$nodes=this.View?.()??[]}_$callUpdatesBeforeInit(){let t=Object.getOwnPropertyNames(Object.getPrototypeOf(this)),e=Object.getOwnPropertyNames(this);[...t,...e].forEach(s=>{if(s.startsWith("$w$"))return this[s.slice(3)]();if(s.startsWith("$f$")){let o=s.slice(3);this[o]=this[s],this._$updateProp(o)}}),delete this._$notInitd}_$setForwardProp(t,e){if(t in this){this[t]=e;return}this._$forwardPropsId.push(t);let n=`$${t}`;this[n]=e,Object.defineProperty(this,t,{get(){return this[n]},set(s){this[n]===s&&!(s instanceof Object)||(this[n]=s,this._$forwardPropsSet?.forEach(o=>{o._$dlNodeType===c.Comp&&o._$setProp(t,s),o instanceof HTMLElement&&L(o,t,s)}))}})}_$addForwardProps(t){this._$forwardPropsSet.add(t),this._$forwardPropsId.forEach(e=>{let n=this[e];this._$forwardPropsSet?.forEach(s=>{s._$dlNodeType===c.Comp&&("_$forwardProps"in s&&s._$forwardPropsId.push(e),s._$setProp(e,n)),s instanceof HTMLElement&&L(s,e,n)})}),u.addWillUnmount(t,this._$forwardPropsSet.delete.bind(this._$forwardPropsSet,t))}_$setProp(t,e){"_$forwardProps"in this&&this._$setForwardProp(t,e),`$p$${t}`in this&&(this[t]=e)}_$initEnv(t,e,n){this[t]=e,this[`$en$${t}`]=n}_$updateEnv(t,e,n){`$e$${t}`in this&&n===this[`$en$${t}`]&&(this[t]===e&&!(e instanceof Object)||(this[t]=e))}_$setContent(t){let e=this._$contentKey;e&&(this[e]===t&&!(t instanceof Object)||(this[e]=t))}_$updateDerived(t){"_$notInitd"in this||(this[`$tv$${t}`]||(this[`$tv$${t}`]=!0),this[`$s$${t}`]?.forEach(e=>{`$w$${e}`in this?this[e](t):this[e]=this[`$f$${e}`]}))}_$updateView(t){if(!this[`$tv$${t}`])return;let e=this[`$$${t}`];e&&(this._$update?.(e,t),this[`$tv$${t}`]=!1)}_$updateProp(t){this._$updateDerived(t),this._$updateView(t)}},dt=_;function ut(i,t){i._$updateProp(t)}var F=class{constructor(){this.envs={},this.currentEnvNodes=[]}addEnvNode(t){this.currentEnvNodes.push(t),this.mergeEnvs()}replaceEnvNodes(t){this.currentEnvNodes=t,this.mergeEnvs()}removeEnvNode(){this.currentEnvNodes.pop(),this.mergeEnvs()}mergeEnvs(){this.envs={},this.currentEnvNodes.forEach(t=>{Object.entries(t.envs).forEach(([e,n])=>{this.envs[e]=[n,t]})})}},O=class extends u{constructor(t){super(c.Env),"DLEnvStore"in r.global||(r.global.DLEnvStore=new F),this.envs=t,this.updateNodes=new Set,r.global.DLEnvStore.addEnvNode(this)}updateEnv(t,e){this.envs[t]=e,r.global.DLEnvStore.currentEnvNodes.includes(this)&&r.global.DLEnvStore.mergeEnvs(),this.updateNodes.forEach(n=>{n._$updateEnv(t,e,this)})}addNode(t){this.updateNodes.add(t),u.addWillUnmount(t,this.updateNodes.delete.bind(this.updateNodes,t))}initNodes(t){this._$nodes=t,r.global.DLEnvStore.removeEnvNode()}};function ft(i){return r.document.createTextNode(i)}function mt(i,t){i.textContent!==t&&(i.textContent=t)}var j=class{propViewFunc;dlUpdateFunc=new Set;constructor(t){this.propViewFunc=t}build(){let t,e=s=>{t=s,this.dlUpdateFunc.add(s)},n=this.propViewFunc(e);return n.length===0?[]:(t&&u.addWillUnmount(n[0],this.dlUpdateFunc.delete.bind(this.dlUpdateFunc,t)),n)}update(...t){this.dlUpdateFunc.forEach(e=>{e(...t)})}};var H=class extends u{constructor(){super(c.Subview)}};var E=class extends u{constructor(t){super(t),r.global.DLEnvStore&&r.global.DLEnvStore.currentEnvNodes.length>0&&(this.savedEnvNodes=[...r.global.DLEnvStore.currentEnvNodes])}initNewNodes(t){u.addParentEl(t,this._$parentEl)}geneNewNodesInEnv(t){if(!this.savedEnvNodes){let s=t();return this.initNewNodes(s),s}let e=r.global.DLEnvStore.currentEnvNodes;r.global.DLEnvStore.replaceEnvNodes(this.savedEnvNodes);let n=t();return r.global.DLEnvStore.replaceEnvNodes(e),this.initNewNodes(n),n}initUnmountStore(){r.global.WillUnmountStore.push([]),r.global.DidUnmountStore.push([])}removeNodes(t){u.loopShallowEls(t,e=>{this._$parentEl.removeChild(e)})}};var l=class extends E{array;nodeFunc;depNum;nodesMap=new Map;updateArr=[];get _$nodes(){let t=[];for(let e=0;e<this.array.length;e++)t.push(...this.nodesMap.get(this.keys?.[e]??e));return t}constructor(t,e,n){super(c.For),this.array=[...t],this.keys=n,this.depNum=e}addNodeFunc(t){this.nodeFunc=t,this.array.forEach((e,n)=>{this.initUnmountStore();let s=this.keys?.[n]??n,o=t(e,this.updateArr,n);this.nodesMap.set(s,o),this.setUnmountMap(s)}),l.addWillUnmount(this,this.runAllWillUnmount.bind(this)),l.addDidUnmount(this,this.runAllDidUnmount.bind(this))}update(t,...e){if(!(t&this.depNum)){this.updateArgs=e;for(let n=0;n<this.array.length;n++)this.updateItem(n,this.array,t)}}updateItem(t,e,n){this.updateArr[t]?.(n??this.depNum,...this.updateArgs,e[t])}updateArray(t,e,n,s,o){if(this.updateArgs=[e,n,s],o){this.updateWithKey(t,o);return}this.updateWithOutKey(t)}getNewNodes(t,e,n,s){this.initUnmountStore();let o=this.geneNewNodesInEnv(()=>this.nodeFunc(n[t],s??this.updateArr,t));return this.setUnmountMap(e),this.nodesMap.set(e,o),o}setUnmountMap(t){let e=r.global.WillUnmountStore.pop();e&&e.length>0&&(this.willUnmountMap||(this.willUnmountMap=new Map),this.willUnmountMap.set(t,e));let n=r.global.DidUnmountStore.pop();n&&n.length>0&&(this.didUnmountMap||(this.didUnmountMap=new Map),this.didUnmountMap.set(t,n))}runAllWillUnmount(){!this.willUnmountMap||this.willUnmountMap.size===0||(this.willUnmountMap.forEach(t=>{for(let e=0;e<t.length;e++)t[e]?.()}),this.willUnmountMap.clear())}runAllDidUnmount(){!this.didUnmountMap||this.didUnmountMap.size===0||(this.didUnmountMap.forEach(t=>{for(let e=t.length-1;e>=0;e--)t[e]?.()}),this.didUnmountMap.clear())}runWillUnmount(t){if(!this.willUnmountMap||this.willUnmountMap.size===0)return;let e=this.willUnmountMap.get(t);if(e){for(let n=0;n<e.length;n++)e[n]?.();this.willUnmountMap.delete(t)}}runDidUnmount(t){if(!this.didUnmountMap||this.didUnmountMap.size===0)return;let e=this.didUnmountMap.get(t);if(e){for(let n=e.length-1;n>=0;n--)e[n]?.();this.didUnmountMap.delete(t)}}removeNodes(t,e){this.runWillUnmount(e),super.removeNodes(t),this.runDidUnmount(e),this.nodesMap.delete(e)}updateWithOutKey(t){let e=this.array.length,n=t.length;if(e===n){for(let o=0;o<this.array.length;o++)this.updateItem(o,t);this.array=[...t];return}let s=this._$parentEl;if(e<n){let o=l.getFlowIndexFromNodes(s._$nodes,this),a=s.childNodes.length;for(let p=0;p<n;p++){if(p<e){o+=l.getFlowIndexFromNodes(this.nodesMap.get(p)),this.updateItem(p,t);continue}let S=this.getNewNodes(p,p,t);l.appendNodesWithIndex(S,s,o,a)}l.runDidMount(),this.array=[...t];return}for(let o=0;o<n;o++)this.updateItem(o,t);for(let o=n;o<e;o++){let a=this.nodesMap.get(o);this.removeNodes(a,o)}this.updateArr.splice(n,e-n),this.array=[...t]}updateWithKey(t,e){if(e.length!==new Set(e).size)throw new Error("DLight: Duplicate keys in for loop are not allowed");let n=this.keys;if(this.keys=e,l.arrayEqual(n,this.keys)){for(let d=0;d<t.length;d++)this.updateItem(d,t);this.array=[...t];return}let s=this._$parentEl;if(this.keys.length===0){let d=s._$nodes??[];if(d.length===1&&d[0]===this)this.runAllWillUnmount(),s.innerHTML="",this.runAllDidUnmount();else for(let h=0;h<n.length;h++){let f=n[h];this.removeNodes(this.nodesMap.get(f),f)}this.nodesMap.clear(),this.updateArr=[],this.array=[];return}let o=l.getFlowIndexFromNodes(s._$nodes,this);if(n.length===0){let d=s.childNodes[o];for(let h=0;h<this.keys.length;h++){let f=this.getNewNodes(h,this.keys[h],t);l.appendNodesWithSibling(f,s,d)}l.runDidMount(),this.array=[...t];return}let a=[],p=[];for(let d=0;d<n.length;d++){let h=n[d];if(this.keys.includes(h)){a.push(h),p.push(this.updateArr[d]);continue}this.removeNodes(this.nodesMap.get(h),h)}let S=s.childNodes.length,$=o;for(let d=0;d<this.keys.length;d++){let h=this.keys[d];if(a.includes(h)){$+=l.getFlowIndexFromNodes(this.nodesMap.get(h)),this.updateItem(d,t);continue}p.splice(d,0,null);let f=this.getNewNodes(d,h,t,p);a.splice(d,0,h);let U=l.appendNodesWithIndex(f,s,$,S);$+=U,S+=U}if(l.runDidMount(),l.arrayEqual(this.keys,a)){this.array=[...t],this.updateArr=p;return}$=o;let D=new Map;for(let d=0;d<this.keys.length;d++){let h=this.keys[d],f=a.indexOf(h),U=D.get(h);if(U){let x=l.getFlowIndexFromNodes(U),v=l.toEls(U).pop(),w=s.childNodes[$+x];v!==w&&v.nextSibling!==w&&l.insertNodesBefore(U,s,w),$+=x,delete D[d]}else if(f===d){$+=l.getFlowIndexFromNodes(this.nodesMap.get(h));continue}else{let x=a[d];D.set(x,this.nodesMap.get(x));let v=this.nodesMap.get(h),w=l.toEls(v).pop(),M=s.childNodes[$];w!==M&&w.nextSibling!==M&&($+=l.insertNodesBefore(v,s,M))}let K=a[d];a[d]=a[f],a[f]=K;let A=p[d];p[d]=p[f],p[f]=A}this.array=[...t],this.updateArr=p}static arrayEqual(t,e){return t.length!==e.length?!1:t.every((n,s)=>n===e[s])}};var b=class extends E{willUnmountFuncs=[];didUnmountFuncs=[];setUnmountFuncs(){this.willUnmountFuncs=r.global.WillUnmountStore.pop(),this.didUnmountFuncs=r.global.DidUnmountStore.pop()}runWillUnmount(){for(let t=0;t<this.willUnmountFuncs.length;t++)this.willUnmountFuncs[t]()}runDidUnmount(){for(let t=this.didUnmountFuncs.length-1;t>=0;t--)this.didUnmountFuncs[t]()}removeNodes(t){this.runWillUnmount(),super.removeNodes(t),this.runDidUnmount()}geneNewNodesInEnv(t){this.initUnmountStore();let e=super.geneNewNodesInEnv(t);return this.setUnmountFuncs(),e}};var m=class extends b{nodesFunc;constructor(t){super(c.Exp),this.nodesFunc=t,this.initUnmountStore(),this._$nodes=m.formatNodes(t()),this.setUnmountFuncs(),m.addWillUnmount(this,this.runWillUnmount.bind(this)),m.addDidUnmount(this,this.runDidUnmount.bind(this))}update(){this.removeNodes(this._$nodes);let t=this.geneNewNodesInEnv(()=>m.formatNodes(this.nodesFunc()));if(t.length===0){this._$nodes=[];return}let e=this._$parentEl,n=m.getFlowIndexFromNodes(e._$nodes,this),s=e.childNodes[n];m.appendNodesWithSibling(t,e,s),m.runDidMount(),this._$nodes=t}static formatNodes(t){return Array.isArray(t)||(t=[t]),t.flat(1).filter(e=>e!=null&&typeof e!="boolean").map(e=>typeof e=="string"||typeof e=="number"||typeof e=="bigint"?r.document.createTextNode(`${e}`):"propViewFunc"in e?e.build():e).flat(1)}};var g=class extends b{condFunc;cond;depNum;constructor(t){super(c.Cond),this.depNum=t}addCondFunc(t){this.cond=-1,this.condFunc=t,this.initUnmountStore(),this._$nodes=this.condFunc(this),this.setUnmountFuncs(),g.addWillUnmount(this,this.runWillUnmount.bind(this)),g.addDidUnmount(this,this.runDidUnmount.bind(this))}updateCond(...t){let e=[this.willUnmountFuncs,this.didUnmountFuncs],n=this.geneNewNodesInEnv(()=>this.condFunc(this));if(this.didntChange)return[this.willUnmountFuncs,this.didUnmountFuncs]=e,this.didntChange=!1,this.updateFunc?.(this.depNum,...t),this._$nodes;let s=[this.willUnmountFuncs,this.didUnmountFuncs];if([this.willUnmountFuncs,this.didUnmountFuncs]=e,this._$nodes&&this._$nodes.length>0&&this.removeNodes(this._$nodes),[this.willUnmountFuncs,this.didUnmountFuncs]=s,n.length===0)return this._$nodes=[],this._$nodes;let o=this._$parentEl,a=g.getFlowIndexFromNodes(o._$nodes,this),p=o.childNodes[a];return g.appendNodesWithSibling(n,o,p),g.runDidMount(),this._$nodes=n,this._$nodes}update(t,...e){t&this.depNum||this.updateFunc?.(t,...e)}};function J(){r.global.WillUnmountStore=[],r.global.DidUnmountStore=[]}function Bt(i,t){let e=i;if(typeof i=="string"){let s=r.document.getElementById(i);if(s)e=s;else throw new Error(`DLight: Element with id ${i} not found`)}J(),e.innerHTML="";let n=new t;n._$init(),y(e,n,0),u.runDidMount()}function Ct(i,t){return i()}function Q(i){return i}var Vt=Q;export{Vt as $,_ as CompNode,g as CondNode,O as EnvNode,F as EnvStoreClass,m as ExpNode,l as ForNode,j as PropView,H as SubViewNode,dt as View,nt as createElement,ft as createTextNode,et as delegateEvent,Q as escape,L as forwardHTMLProp,y as insertNode,Ct as manual,Bt as render,P as setDataset,C as setDocument,q as setEvent,B as setGlobal,T as setHTMLAttr,z as setHTMLAttrs,W as setHTMLProp,V as setHTMLProps,I as setStyle,ut as update,mt as updateText}; | ||
import{Store as N}from"@dlightjs/store";"global"in N||(typeof window<"u"?N.global=window:typeof global<"u"?N.global=global:N.global={});"document"in N||typeof document<"u"&&(N.document=document);var r={...N,delegatedEvents:new Set};function C(s){r.global=s}function A(s){r.document=s}var c={Comp:0,For:1,Cond:2,Env:3,Exp:4,Subview:5},u=class{_$dlNodeType;constructor(t){this._$dlNodeType=t}get _$el(){return u.toEls(this._$nodes)}static toEls(t){let e=[];return this.loopShallowEls(t,n=>{e.push(n)}),e}static loopShallowEls(t,e){t.forEach(n=>{if(!("_$dlNodeType"in n))return e(n);n._$nodes&&u.loopShallowEls(n._$nodes,e)})}static addParentEl(t,e){t.forEach(n=>{"_$dlNodeType"in n&&(n._$parentEl=e,n._$nodes&&u.addParentEl(n._$nodes,e))})}static getFlowIndexFromNodes(t,e){let n=0,i=[...t].reverse();for(;i.length>0;){let o=i.pop();if(o===e)break;"_$dlNodeType"in o?o._$nodes&&i.push(...[...o._$nodes].reverse()):n++}return n}static appendNodesWithSibling(t,e,n){return n?this.insertNodesBefore(t,e,n):this.appendNodes(t,e)}static appendNodesWithIndex(t,e,n,i){return i=i??e.childNodes.length,i!==n?this.insertNodesBefore(t,e,e.childNodes[n]):this.appendNodes(t,e)}static insertNodesBefore(t,e,n){let i=0;return this.loopShallowEls(t,o=>{e.insertBefore(o,n),i++}),i}static appendNodes(t,e){let n=0;return this.loopShallowEls(t,i=>{e.appendChild(i),n++}),n}static addWillUnmount(t,e){let n=r.global.WillUnmountStore,i=n[n.length-1];i&&i.push(e.bind(null,t))}static addDidUnmount(t,e){let n=r.global.DidUnmountStore,i=n[n.length-1];i&&i.push(e.bind(null,t))}static addDidMount(t,e){r.global.DidMountStore||(r.global.DidMountStore=[]),r.global.DidMountStore.push(e.bind(null,t))}static runDidMount(){let t=r.global.DidMountStore;if(!(!t||t.length===0)){for(let e=t.length-1;e>=0;e--)t[e]();r.global.DidMountStore=[]}}};function I(s,t){Object.assign(s.style,t)}function P(s,t){Object.assign(s.dataset,t)}function W(s,t,e){let n=`$${t}`;n in s&&s[n]===e||(s[t]=e,s[n]=e)}function V(s,t){Object.entries(t).forEach(([e,n])=>{if(e==="style")return I(s,n);if(e==="dataset")return P(s,n);W(s,e,n)})}function T(s,t,e){let n=`$${t}`;n in s&&s[n]===e||(s.setAttribute(t,e),s[n]=e)}function z(s,t){Object.entries(t).forEach(([e,n])=>{T(s,e,n)})}function q(s,t,e){let n=s[`$on${t}`];n&&s.removeEventListener(t,n),s.addEventListener(t,e),s[`$on${t}`]=e}function G(s){let t=`$$${s.type}`;for(let e of s.composedPath())if(e[t]&&e[t](s),s.cancelBubble)return}function et(s,t,e){s[`$$${t}`]!==e&&(s[`$$${t}`]=e,r.delegatedEvents.has(t)||(r.delegatedEvents.add(t),r.document.addEventListener(t,G)))}function nt(s){return r.document.createElement(s)}function y(s,t,e){s._$nodes||(s._$nodes=Array.from(s.childNodes)),s._$nodes.splice(e,0,t);let n=u.getFlowIndexFromNodes(s._$nodes,t);u.appendNodesWithIndex([t],s,n),u.addParentEl([t],s)}function L(s,t,e){if(t==="style")return I(s,e);if(t==="dataset")return P(s,e);if(t!=="element"){if(t==="prop")return V(s,e);if(t==="attr")return z(s,e);if(t==="innerHTML")return W(s,"innerHTML",e);if(t!=="forwardProp"){if(t.startsWith("on"))return q(s,t.slice(2).toLowerCase(),e);T(s,t,e)}}}var _=class extends u{constructor(){super(c.Comp)}_$init(t,e,n,i){this._$notInitd=!0,i&&i._$addForwardProps(this),e!==null&&this._$setContent(e),t&&Object.entries(t).forEach(([o,a])=>{this._$setProp(o,a)}),n&&(this._$children=n),r.global.DLEnvStore&&Object.entries(r.global.DLEnvStore.envs).forEach(([o,[a,p]])=>{`$e$${o}`in this&&(p.addNode(this),this._$initEnv(o,a,p))}),this._$callUpdatesBeforeInit(),this.didMount&&u.addDidMount(this,this.didMount.bind(this)),this.willUnmount&&u.addWillUnmount(this,this.willUnmount.bind(this)),this.didUnmount&&u.addDidUnmount(this,this.didUnmount.bind(this)),this.willMount?.(),this._$nodes=this.View?.()??[]}_$callUpdatesBeforeInit(){let t=Object.getOwnPropertyNames(Object.getPrototypeOf(this)),e=Object.getOwnPropertyNames(this);[...t,...e].forEach(i=>{if(i.startsWith("$w$"))return this[i.slice(3)]();if(i.startsWith("$f$")){let o=i.slice(3);this[o]=this[i],this._$updateProp(o)}}),delete this._$notInitd}_$setForwardProp(t,e){if(t in this){this[t]=e;return}this._$forwardPropsId.push(t);let n=`$${t}`;this[n]=e,Object.defineProperty(this,t,{get(){return this[n]},set(i){this[n]===i&&!(i instanceof Object)||(this[n]=i,this._$forwardPropsSet?.forEach(o=>{o._$dlNodeType===c.Comp&&o._$setProp(t,i),o instanceof HTMLElement&&L(o,t,i)}))}})}_$addForwardProps(t){this._$forwardPropsSet.add(t),this._$forwardPropsId.forEach(e=>{let n=this[e];this._$forwardPropsSet?.forEach(i=>{i._$dlNodeType===c.Comp&&("_$forwardProps"in i&&i._$forwardPropsId.push(e),i._$setProp(e,n)),i instanceof HTMLElement&&L(i,e,n)})}),u.addWillUnmount(t,this._$forwardPropsSet.delete.bind(this._$forwardPropsSet,t))}_$setProp(t,e){"_$forwardProps"in this&&this._$setForwardProp(t,e),`$p$${t}`in this&&(this[t]=e)}_$initEnv(t,e,n){this[t]=e,this[`$en$${t}`]=n}_$updateEnv(t,e,n){`$e$${t}`in this&&n===this[`$en$${t}`]&&(this[t]===e&&!(e instanceof Object)||(this[t]=e))}_$setContent(t){let e=this._$contentKey;e&&(this[e]===t&&!(t instanceof Object)||(this[e]=t))}_$updateDerived(t){"_$notInitd"in this||(this[`$tv$${t}`]||(this[`$tv$${t}`]=!0),this[`$s$${t}`]?.forEach(e=>{`$w$${e}`in this?this[e](t):this[e]=this[`$f$${e}`]}))}_$updateView(t){if(!this[`$tv$${t}`])return;let e=this[`$$${t}`];e&&(this._$update?.(e,t),this[`$tv$${t}`]=!1)}_$updateProp(t){this._$updateDerived(t),this._$updateView(t)}},dt=_;function ut(s,t){s._$updateProp(t)}var F=class{constructor(){this.envs={},this.currentEnvNodes=[]}addEnvNode(t){this.currentEnvNodes.push(t),this.mergeEnvs()}replaceEnvNodes(t){this.currentEnvNodes=t,this.mergeEnvs()}removeEnvNode(){this.currentEnvNodes.pop(),this.mergeEnvs()}mergeEnvs(){this.envs={},this.currentEnvNodes.forEach(t=>{Object.entries(t.envs).forEach(([e,n])=>{this.envs[e]=[n,t]})})}},O=class extends u{constructor(t){super(c.Env),"DLEnvStore"in r.global||(r.global.DLEnvStore=new F),this.envs=t,this.updateNodes=new Set,r.global.DLEnvStore.addEnvNode(this)}updateEnv(t,e){this.envs[t]=e,r.global.DLEnvStore.currentEnvNodes.includes(this)&&r.global.DLEnvStore.mergeEnvs(),this.updateNodes.forEach(n=>{n._$updateEnv(t,e,this)})}addNode(t){this.updateNodes.add(t),u.addWillUnmount(t,this.updateNodes.delete.bind(this.updateNodes,t))}initNodes(t){this._$nodes=t,r.global.DLEnvStore.removeEnvNode()}};function ft(s){return r.document.createTextNode(s)}function mt(s,t){s.textContent!==t&&(s.textContent=t)}var K=class{propViewFunc;dlUpdateFunc=new Set;constructor(t){this.propViewFunc=t}build(){let t,e=i=>{t=i,this.dlUpdateFunc.add(i)},n=this.propViewFunc(e);return n.length===0?[]:(t&&u.addWillUnmount(n[0],this.dlUpdateFunc.delete.bind(this.dlUpdateFunc,t)),n)}update(...t){this.dlUpdateFunc.forEach(e=>{e(...t)})}};var j=class extends u{constructor(){super(c.Subview)}};var E=class extends u{constructor(t){super(t),r.global.DLEnvStore&&r.global.DLEnvStore.currentEnvNodes.length>0&&(this.savedEnvNodes=[...r.global.DLEnvStore.currentEnvNodes])}initNewNodes(t){u.addParentEl(t,this._$parentEl)}geneNewNodesInEnv(t){if(!this.savedEnvNodes){let i=t();return this.initNewNodes(i),i}let e=r.global.DLEnvStore.currentEnvNodes;r.global.DLEnvStore.replaceEnvNodes(this.savedEnvNodes);let n=t();return r.global.DLEnvStore.replaceEnvNodes(e),this.initNewNodes(n),n}initUnmountStore(){r.global.WillUnmountStore.push([]),r.global.DidUnmountStore.push([])}removeNodes(t){u.loopShallowEls(t,e=>{this._$parentEl.removeChild(e)})}};var l=class extends E{array;nodeFunc;depNum;nodesMap=new Map;updateArr=[];get _$nodes(){let t=[];for(let e=0;e<this.array.length;e++)t.push(...this.nodesMap.get(this.keys?.[e]??e));return t}constructor(t,e,n){super(c.For),this.array=[...t],this.keys=n,this.depNum=e}addNodeFunc(t){this.nodeFunc=t,this.array.forEach((e,n)=>{this.initUnmountStore();let i=this.keys?.[n]??n,o=t(e,this.updateArr,n);this.nodesMap.set(i,o),this.setUnmountMap(i)}),l.addWillUnmount(this,this.runAllWillUnmount.bind(this)),l.addDidUnmount(this,this.runAllDidUnmount.bind(this))}update(t,e){if(!(t&this.depNum)){this.updateKey=e;for(let n=0;n<this.array.length;n++)this.updateItem(n,this.array,t)}}updateItem(t,e,n){this.updateArr[t]?.(n??this.depNum,this.updateKey,e[t])}updateArray(t,e,n){if(this.updateKey=e,n){this.updateWithKey(t,n);return}this.updateWithOutKey(t)}getNewNodes(t,e,n,i){this.initUnmountStore();let o=this.geneNewNodesInEnv(()=>this.nodeFunc(n[t],i??this.updateArr,t));return this.setUnmountMap(e),this.nodesMap.set(e,o),o}setUnmountMap(t){let e=r.global.WillUnmountStore.pop();e&&e.length>0&&(this.willUnmountMap||(this.willUnmountMap=new Map),this.willUnmountMap.set(t,e));let n=r.global.DidUnmountStore.pop();n&&n.length>0&&(this.didUnmountMap||(this.didUnmountMap=new Map),this.didUnmountMap.set(t,n))}runAllWillUnmount(){!this.willUnmountMap||this.willUnmountMap.size===0||(this.willUnmountMap.forEach(t=>{for(let e=0;e<t.length;e++)t[e]?.()}),this.willUnmountMap.clear())}runAllDidUnmount(){!this.didUnmountMap||this.didUnmountMap.size===0||(this.didUnmountMap.forEach(t=>{for(let e=t.length-1;e>=0;e--)t[e]?.()}),this.didUnmountMap.clear())}runWillUnmount(t){if(!this.willUnmountMap||this.willUnmountMap.size===0)return;let e=this.willUnmountMap.get(t);if(e){for(let n=0;n<e.length;n++)e[n]?.();this.willUnmountMap.delete(t)}}runDidUnmount(t){if(!this.didUnmountMap||this.didUnmountMap.size===0)return;let e=this.didUnmountMap.get(t);if(e){for(let n=e.length-1;n>=0;n--)e[n]?.();this.didUnmountMap.delete(t)}}removeNodes(t,e){this.runWillUnmount(e),super.removeNodes(t),this.runDidUnmount(e),this.nodesMap.delete(e)}updateWithOutKey(t){let e=this.array.length,n=t.length;if(e===n){for(let o=0;o<this.array.length;o++)this.updateItem(o,t);this.array=[...t];return}let i=this._$parentEl;if(e<n){let o=l.getFlowIndexFromNodes(i._$nodes,this),a=i.childNodes.length;for(let p=0;p<n;p++){if(p<e){o+=l.getFlowIndexFromNodes(this.nodesMap.get(p)),this.updateItem(p,t);continue}let S=this.getNewNodes(p,p,t);l.appendNodesWithIndex(S,i,o,a)}l.runDidMount(),this.array=[...t];return}for(let o=0;o<n;o++)this.updateItem(o,t);for(let o=n;o<e;o++){let a=this.nodesMap.get(o);this.removeNodes(a,o)}this.updateArr.splice(n,e-n),this.array=[...t]}updateWithKey(t,e){if(console.log(e),e.length!==new Set(e).size)throw new Error("DLight: Duplicate keys in for loop are not allowed");let n=this.keys;if(this.keys=e,l.arrayEqual(n,this.keys)){for(let d=0;d<t.length;d++)this.updateItem(d,t);this.array=[...t];return}let i=this._$parentEl;if(this.keys.length===0){let d=i._$nodes??[];if(d.length===1&&d[0]===this)this.runAllWillUnmount(),i.innerHTML="",this.runAllDidUnmount();else for(let h=0;h<n.length;h++){let f=n[h];this.removeNodes(this.nodesMap.get(f),f)}this.nodesMap.clear(),this.updateArr=[],this.array=[];return}let o=l.getFlowIndexFromNodes(i._$nodes,this);if(n.length===0){let d=i.childNodes[o];for(let h=0;h<this.keys.length;h++){let f=this.getNewNodes(h,this.keys[h],t);l.appendNodesWithSibling(f,i,d)}l.runDidMount(),this.array=[...t];return}let a=[],p=[];for(let d=0;d<n.length;d++){let h=n[d];if(this.keys.includes(h)){a.push(h),p.push(this.updateArr[d]);continue}this.removeNodes(this.nodesMap.get(h),h)}let S=i.childNodes.length,$=o;for(let d=0;d<this.keys.length;d++){let h=this.keys[d];if(a.includes(h)){$+=l.getFlowIndexFromNodes(this.nodesMap.get(h)),this.updateItem(d,t);continue}p.splice(d,0,null);let f=this.getNewNodes(d,h,t,p);a.splice(d,0,h);let U=l.appendNodesWithIndex(f,i,$,S);$+=U,S+=U}if(l.runDidMount(),l.arrayEqual(this.keys,a)){this.array=[...t],this.updateArr=p;return}$=o;let D=new Map;for(let d=0;d<this.keys.length;d++){let h=this.keys[d],f=a.indexOf(h),U=D.get(h);if(U){let x=l.getFlowIndexFromNodes(U),v=l.toEls(U).pop(),w=i.childNodes[$+x];v!==w&&v.nextSibling!==w&&l.insertNodesBefore(U,i,w),$+=x,delete D[d]}else if(f===d){$+=l.getFlowIndexFromNodes(this.nodesMap.get(h));continue}else{let x=a[d];D.set(x,this.nodesMap.get(x));let v=this.nodesMap.get(h),w=l.toEls(v).pop(),M=i.childNodes[$];w!==M&&w.nextSibling!==M&&($+=l.insertNodesBefore(v,i,M))}let H=a[d];a[d]=a[f],a[f]=H;let B=p[d];p[d]=p[f],p[f]=B}this.array=[...t],this.updateArr=p}static arrayEqual(t,e){return t.length!==e.length?!1:t.every((n,i)=>n===e[i])}};var b=class extends E{willUnmountFuncs=[];didUnmountFuncs=[];setUnmountFuncs(){this.willUnmountFuncs=r.global.WillUnmountStore.pop(),this.didUnmountFuncs=r.global.DidUnmountStore.pop()}runWillUnmount(){for(let t=0;t<this.willUnmountFuncs.length;t++)this.willUnmountFuncs[t]()}runDidUnmount(){for(let t=this.didUnmountFuncs.length-1;t>=0;t--)this.didUnmountFuncs[t]()}removeNodes(t){this.runWillUnmount(),super.removeNodes(t),this.runDidUnmount()}geneNewNodesInEnv(t){this.initUnmountStore();let e=super.geneNewNodesInEnv(t);return this.setUnmountFuncs(),e}};var m=class extends b{nodesFunc;constructor(t){super(c.Exp),this.nodesFunc=t,this.initUnmountStore(),this._$nodes=m.formatNodes(t()),this.setUnmountFuncs(),m.addWillUnmount(this,this.runWillUnmount.bind(this)),m.addDidUnmount(this,this.runDidUnmount.bind(this))}update(){this.removeNodes(this._$nodes);let t=this.geneNewNodesInEnv(()=>m.formatNodes(this.nodesFunc()));if(t.length===0){this._$nodes=[];return}let e=this._$parentEl,n=m.getFlowIndexFromNodes(e._$nodes,this),i=e.childNodes[n];m.appendNodesWithSibling(t,e,i),m.runDidMount(),this._$nodes=t}static formatNodes(t){return Array.isArray(t)||(t=[t]),t.flat(1).filter(e=>e!=null&&typeof e!="boolean").map(e=>typeof e=="string"||typeof e=="number"||typeof e=="bigint"?r.document.createTextNode(`${e}`):"propViewFunc"in e?e.build():e).flat(1)}};var g=class extends b{condFunc;cond;depNum;constructor(t){super(c.Cond),this.depNum=t}addCondFunc(t){this.cond=-1,this.condFunc=t,this.initUnmountStore(),this._$nodes=this.condFunc(this),this.setUnmountFuncs(),g.addWillUnmount(this,this.runWillUnmount.bind(this)),g.addDidUnmount(this,this.runDidUnmount.bind(this))}updateCond(...t){let e=[this.willUnmountFuncs,this.didUnmountFuncs],n=this.geneNewNodesInEnv(()=>this.condFunc(this));if(this.didntChange)return[this.willUnmountFuncs,this.didUnmountFuncs]=e,this.didntChange=!1,this.updateFunc?.(this.depNum,...t),this._$nodes;let i=[this.willUnmountFuncs,this.didUnmountFuncs];if([this.willUnmountFuncs,this.didUnmountFuncs]=e,this._$nodes&&this._$nodes.length>0&&this.removeNodes(this._$nodes),[this.willUnmountFuncs,this.didUnmountFuncs]=i,n.length===0)return this._$nodes=[],this._$nodes;let o=this._$parentEl,a=g.getFlowIndexFromNodes(o._$nodes,this),p=o.childNodes[a];return g.appendNodesWithSibling(n,o,p),g.runDidMount(),this._$nodes=n,this._$nodes}update(t,...e){t&this.depNum||this.updateFunc?.(t,...e)}};function J(){r.global.WillUnmountStore=[],r.global.DidUnmountStore=[]}function Ct(s,t){let e=s;if(typeof s=="string"){let i=r.document.getElementById(s);if(i)e=i;else throw new Error(`DLight: Element with id ${s} not found`)}J(),e.innerHTML="";let n=new t;n._$init(),y(e,n,0),u.runDidMount()}function At(s,t){return s()}function Q(s){return s}var Vt=Q;export{Vt as $,_ as CompNode,g as CondNode,O as EnvNode,F as EnvStoreClass,m as ExpNode,l as ForNode,K as PropView,j as SubViewNode,dt as View,nt as createElement,ft as createTextNode,et as delegateEvent,Q as escape,L as forwardHTMLProp,y as insertNode,At as manual,Ct as render,P as setDataset,A as setDocument,q as setEvent,C as setGlobal,T as setHTMLAttr,z as setHTMLAttrs,W as setHTMLProp,V as setHTMLProps,I as setStyle,ut as update,mt as updateText}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@dlightjs/dlight", | ||
"version": "1.0.0-beta.0", | ||
"version": "1.0.0-beta.2", | ||
"description": "DX-first UI rendering library", | ||
@@ -19,3 +19,3 @@ "author": { | ||
"main": "dist/index.cjs", | ||
"module": "dist/index.js", | ||
"module": "src/index.js", | ||
"typings": "dist/index.d.ts", | ||
@@ -22,0 +22,0 @@ "dependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
168188