@dlightjs/dlight
Advanced tools
Comparing version 1.0.0-alpha.46 to 1.0.0-alpha.47
@@ -1,2 +0,2 @@ | ||
import{Store as g}from"@dlightjs/store";"global"in g||(typeof window<"u"?g.global=window:typeof global<"u"?g.global=global:g.global={});"document"in g||typeof document<"u"&&(g.document=document);var i=g;function C(o){i.global=o}function H(o){i.document=o}var f={Comp:0,For:1,Cond:2,Env:3,Exp:4,Subview:5},p=class{_$dlNodeType;constructor(t){this._$dlNodeType=t}get _$el(){return p.toEls(this._$nodes)}_$parentEl;_$nodes;static toEls(t){let e=[];return this.loopShallowEls(t,s=>{e.push(s)}),e}static loopShallowEls(t,e){t.forEach(s=>{if(!("_$dlNodeType"in s))return e(s);s._$nodes&&p.loopShallowEls(s._$nodes,e)})}static loopShallowDLNodes(t,e){t.forEach(s=>{"_$dlNodeType"in s&&(e(s),s._$nodes&&p.loopShallowDLNodes(s._$nodes,e))})}static addParentEl(t,e){this.loopShallowDLNodes(t,s=>{s._$parentEl=e})}static getFlowIndexFromNodes(t,e){let s=0,n=[...t];for(;n.length>0;){let d=n.shift();if(d===e)break;"_$dlNodeType"in d?d._$nodes&&n.unshift(...d._$nodes):s++}return s}static appendNodesWithSibling(t,e,s){return s?this.insertNodesBefore(t,e,s):this.appendNodes(t,e)}static appendNodesWithIndex(t,e,s,n){return n=n??e.childNodes.length,n!==s?this.insertNodesBefore(t,e,e.childNodes[s]):this.appendNodes(t,e)}static insertNodesBefore(t,e,s){let n=0;return this.loopShallowEls(t,d=>{e.insertBefore(d,s),n++}),n}static appendNodes(t,e){let s=0;return this.loopShallowEls(t,n=>{e.appendChild(n),s++}),s}static lifecycleFunc(t,e){t(),e?.()}static addWillUnmount(t,e){t.willUnmount=this.lifecycleFunc.bind(this,e,t.willUnmount?.bind(t))}static addDidMount(t){i.global.DidMountStore.push(t)}static runDidMount(){if(!("DidMountStore"in i.global))return;let t=i.global.DidMountStore;for(let e=t.length-1;e>=0;e--)t[e]();i.global.DidMountStore=[]}};function K(o,t){Object.assign(o.style,t)}function j(o,t){Object.assign(o.dataset,t)}function y(o,t,e){let s=`$${t}`;s in o&&o[s]===e||(o[t]=e,o[s]=e)}function B(o,t){Object.entries(t).forEach(([e,s])=>{y(o,e,s)})}function F(o,t,e){let s=`$${t}`;s in o&&o[s]===e||(o.setAttribute(t,e),o[s]=e)}function V(o,t){Object.entries(t).forEach(([e,s])=>{F(o,e,s)})}function q(o,t,e){let s=o[`$on${t}`];s&&o.removeEventListener(t,s),o.addEventListener(t,e),o[`$on${t}`]=e}function Z(o){let t=i.document.createElement("template");t.innerHTML=o;let e=t.content.firstChild;return()=>e.cloneNode(!0)}function k(o){return i.document.createElement(o)}function M(o,t,e){o._$nodes||(o._$nodes=Array.from(o.childNodes)),o._$nodes.splice(e,0,t);let s=p.getFlowIndexFromNodes(o._$nodes,t);p.appendNodesWithIndex([t],o,s),p.addParentEl([t],o)}function L(o,t,e){if(t==="style"){K(o,e);return}if(t==="dataset"){j(o,e);return}if(t!=="element"){if(t==="prop"){B(o,e);return}if(t==="attr"){V(o,e);return}if(t==="innerHTML"){y(o,"innerHTML",e);return}if(t!=="forwardProp"){if(t.startsWith("on")){q(o,t.slice(2).toLowerCase(),e);return}F(o,t,e)}}}var S=class extends p{constructor(){super(f.Comp)}_$init(t,e,s,n){this._$notInitd=!0,n&&n._$addForwardProps(this),e!==null&&this._$setContent(e),t&&Object.entries(t).forEach(([d,$])=>{this._$setProp(d,$)}),s&&(this._$children=s),i.global.DLEnvStore&&Object.entries(i.global.DLEnvStore.envs).forEach(([d,[$,a]])=>{a.addNode(this),this._$initEnv(d,$,a)}),this._$callUpdatesBeforeInit(),"DidMountStore"in i.global||(i.global.DidMountStore=[]),this.didMount&&i.global.DidMountStore.push(this.didMount.bind(this)),this.willMount?.(),this._$nodes=this.View?.()??[]}_$callUpdatesBeforeInit(){let t=Object.getOwnPropertyNames(Object.getPrototypeOf(this)),e=Object.getOwnPropertyNames(this);[...t,...e].forEach(n=>{if(n.startsWith("$w$"))return this[n.slice(3)]();n.startsWith("$f$")&&(this[`$${n.slice(3)}`]=this[n])}),delete this._$notInitd}_$setForwardProp(t,e){if(t in this){this[t]=e;return}this._$forwardPropsId.push(t);let s=`$${t}`;this[s]=e,Object.defineProperty(this,t,{get(){return this[s]},set(n){this[s]!==n&&(this[s]=n,this._$forwardPropsSet?.forEach(d=>{d._$dlNodeType===f.Comp&&d._$setProp(t,n),d instanceof HTMLElement&&L(d,t,n)}))}})}_$addForwardProps(t){this._$forwardPropsSet.add(t),this._$forwardPropsId.forEach(e=>{let s=this[e];this._$forwardPropsSet?.forEach(n=>{n._$dlNodeType===f.Comp&&("_$forwardProps"in n&&n._$forwardPropsId.push(e),n._$setProp(e,s)),n instanceof HTMLElement&&L(n,e,s)})}),p.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,s){`$e$${t}`in this&&(this[t]=e,this[`$en$${t}`]=s)}_$updateEnv(t,e,s){`$e$${t}`in this&&s===this[`$en$${t}`]&&this[t]!==e&&(this[t]=e)}_$setContent(t){let e=this._$contentKey;e&&this[e]!==t&&(this[e]=t)}_$updateProp(t,e){let s=`$${t}`;this[s]!==e&&(this[s]=e,this._$updateDerived(t),this._$updateView(t))}_$updateDerived(t){"_$notInitd"in this||this[`$s$${t}`]?.forEach(e=>{`$w$${e}`in this?this[e]():this[`$${e}`]=this[`$f$${e}`]})}_$updateView(t){let e=this[`$$${t}`];e&&this._$update?.(e)}},nt=S;function rt(o,t){o._$updateDerived(t),o._$updateView(t)}var I=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,s])=>{this.envs[e]=[s,t]})})}},P=class extends p{constructor(t){super(f.Env),"DLEnvStore"in i.global||(i.global.DLEnvStore=new I),this.envs=t,this.updateNodes=new Set,i.global.DLEnvStore.addEnvNode(this)}updateEnv(t,e){this.envs[t]=e,i.global.DLEnvStore.currentEnvNodes.includes(this)&&i.global.DLEnvStore.mergeEnvs(),this.updateNodes.forEach(s=>{s._$updateEnv(t,e,this)})}addNode(t){this.updateNodes.add(t),p.addWillUnmount(t,this.updateNodes.delete.bind(this.updateNodes,t))}initNodes(t){this._$nodes=t,i.global.DLEnvStore.removeEnvNode()}};function lt(o){return i.document.createTextNode(o)}function ut(o,t){o.textContent!==t&&(o.textContent=t)}var T=class{propViewFunc;dlUpdateFunc=new Set;constructor(t){this.propViewFunc=t}build(){let t,e=n=>{n.initd=!0,t=n,this.dlUpdateFunc.add(n)},s=this.propViewFunc(e);return s.length===0?[]:(t&&p.addWillUnmount(s[0],this.dlUpdateFunc.delete.bind(this.dlUpdateFunc,t)),s)}update(t){this.dlUpdateFunc.forEach(e=>{if(e.initd){delete e.initd;return}e(t)})}};var W=class extends p{constructor(){super(f.Subview)}};var h=class extends p{constructor(t){super(t),i.global.DLEnvStore&&i.global.DLEnvStore.currentEnvNodes.length>0&&(this.savedEnvNodes=[...i.global.DLEnvStore.currentEnvNodes])}initNewNodes(t){p.addParentEl(t,this._$parentEl)}geneNewNodesInEnv(t){if(!this.savedEnvNodes){let n=t();return this.initNewNodes(n),n}let e=i.global.DLEnvStore.currentEnvNodes;i.global.DLEnvStore.replaceEnvNodes(this.savedEnvNodes);let s=t();return i.global.DLEnvStore.replaceEnvNodes(e),this.initNewNodes(s),s}removeNodes(t,e=!0){t.forEach(s=>{s.willUnmount?.(),"_$dlNodeType"in s||(e&&this._$parentEl?.removeChild(s),e=!1);let n=s._$nodes??s.childNodes;this.removeNodes(n,e),s.didUnmount?.()})}};var w=class extends h{array;keys;nodeFunc;nodess;depNum;updateArr=[];constructor(t,e,s){super(f.For),this.array=[...t],this.keys=s,this.depNum=e}addNodeFunc(t){this.nodeFunc=t,this.nodess=this.array.map((e,s)=>t(e,this.updateArr,s)),this._$nodes=this.nodess.flat(1)}update(t){if(!(t&this.depNum))for(let e=0;e<this.array.length;e++)this.updateItem(e,t)}updateItem(t,e){this.updateArr[t]?.(e??this.depNum,this.array[t])}updateArray(t,e){if(e){this.updateWithKey(t,e);return}this.updateWithOutKey(t)}getNewNodes(t,e){return this.geneNewNodesInEnv(()=>this.nodeFunc(this.array[t],e??this.updateArr,t))}updateWithOutKey(t){let e=this.array.length,s=t.length;if(this.array=[...t],this.updateArr=this.updateArr.slice(0,s),e===s){for(let d=0;d<this.array.length;d++)this.updateItem(d);return}let n=this._$parentEl;if(e<s){let d=h.getFlowIndexFromNodes(n._$nodes,this),$=n.childNodes.length;for(let a=0;a<s;a++){if(a<e){d+=h.getFlowIndexFromNodes(this.nodess[a]),this.updateItem(a);continue}let l=this.getNewNodes(a);h.appendNodesWithIndex(l,n,d,$),h.runDidMount(),this.nodess.push(l)}this._$nodes=this.nodess.flat(1);return}for(let d=0;d<s;d++)this.updateItem(d);for(let d=s;d<e;d++)this.removeNodes(this.nodess[d]);this.nodess=this.nodess.slice(0,s),this._$nodes=this.nodess.flat(1)}updateWithKey(t,e){let s=this.keys;if(this.array=[...t],this.keys=e,w.arrayEqual(s,this.keys)){for(let r=0;r<this.array.length;r++)this.updateItem(r);return}let n=this._$parentEl,d=this.nodess;if(this.keys.length===0){let r=n._$nodes??[];if(r.length===1&&r[0]===this)n.innerHTML="";else for(let u=0;u<s.length;u++)this.removeNodes(d[u]);this.nodess=[],this._$nodes=[],this.updateArr=[];return}let $=h.getFlowIndexFromNodes(n._$nodes,this);if(s.length===0){let r=n.childNodes[$];for(let u=0;u<this.keys.length;u++){let c=this.getNewNodes(u);h.appendNodesWithSibling(c,n,r),h.runDidMount(),this.nodess.push(c)}this._$nodes=this.nodess.flat(1);return}let a=[],l=[],m=[];for(let r=0;r<s.length;r++){let u=s[r];if(this.keys.includes(u)){a.push(u),l.push(d[r]),m.push(this.updateArr[r]);continue}this.removeNodes(d[r])}let E=n.childNodes.length,N=$;for(let r=0;r<this.keys.length;r++){let u=this.keys[r],c=a.indexOf(u);if(c!==-1){N+=h.getFlowIndexFromNodes(l[c]),this.updateArr[c]?.(this.depNum,this.array[r]);continue}m.splice(r,0,null);let x=this.getNewNodes(r,m),b=h.appendNodesWithIndex(x,n,N,E);h.runDidMount(),N+=b,E+=b,l.splice(r,0,x),a.splice(r,0,u)}if(w.arrayEqual(this.keys,a)){this.nodess=l,this._$nodes=this.nodess.flat(1),this.updateArr=m;return}N=$;let D=[];for(let r=0;r<this.keys.length;r++){let u=this.keys[r],c=a.indexOf(u),x=D[r];if(x){let _=h.appendNodesWithIndex(x,n,N+h.getFlowIndexFromNodes(x),E);N+=_,E+=_,D[r]=void 0}else if(c===r){N+=h.getFlowIndexFromNodes(l[r]);continue}else{D[this.keys.indexOf(a[r])]=l[r];let _=h.appendNodesWithIndex(l[c],n,N,E);N+=_,E+=_}let b=l[r];l[r]=l[c],l[c]=b;let U=a[r];a[r]=a[c],a[c]=U;let O=m[r];m[r]=m[c],m[c]=O}this.nodess=l,this._$nodes=this.nodess.flat(1),this.updateArr=m}static arrayEqual(t,e){return t.length!==e.length?!1:t.every((s,n)=>s===e[n])}};var v=class extends h{nodesFunc;constructor(t){super(f.Exp),this.nodesFunc=t,this._$nodes=v.formatNodes(t())}update(){let t=this.geneNewNodesInEnv(()=>v.formatNodes(this.nodesFunc()));if(this.removeNodes(this._$nodes),t.length===0){this._$nodes=[];return}let e=this._$parentEl,s=h.getFlowIndexFromNodes(e._$nodes,this),n=e.childNodes[s];h.appendNodesWithSibling(t,e,n),h.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"?i.document.createTextNode(`${e}`):"propViewFunc"in e?e.build():e).flat(1)}};var A=class extends h{condFunc;cond;depNum;constructor(t){super(f.Cond),this.depNum=t}addCondFunc(t){this.cond=-1,this.condFunc=t,this._$nodes=this.condFunc(this)}updateCond(){let t=this.geneNewNodesInEnv(()=>this.condFunc(this));if(this.didntChange)return this.didntChange=!1,this.updateFunc?.(this.depNum),this._$nodes;if(this._$nodes&&this._$nodes.length>0&&this.removeNodes(this._$nodes),t.length===0)return this._$nodes=[],this._$nodes;let e=this._$parentEl,s=h.getFlowIndexFromNodes(e._$nodes,this),n=e.childNodes[s];return h.appendNodesWithSibling(t,e,n),h.runDidMount(),this._$nodes=t,this._$nodes}update(t){t&this.depNum||this.updateFunc?.(t)}};function Wt(o,t){let e=o;if(typeof o=="string"){let n=i.document.getElementById(o);if(n)e=n;else throw new Error(`DLight: Element with id ${o} not found`)}e.innerHTML="";let s=new t;s._$init(),M(e,s,0),p.runDidMount()}function At(o,t){return o()}function G(o){return o}var Ut=G;export{Ut as $,S as CompNode,A as CondNode,P as EnvNode,v as ExpNode,w as ForNode,T as PropView,W as SubViewNode,nt as View,k as createElement,Z as createTemplate,lt as createTextNode,G as escape,L as forwardHTMLProp,M as insertNode,At as manual,Wt as render,j as setDataset,H as setDocument,q as setEvent,C as setGlobal,F as setHTMLAttr,V as setHTMLAttrs,y as setHTMLProp,B as setHTMLProps,K as setStyle,rt as update,ut as updateText}; | ||
import{Store as g}from"@dlightjs/store";"global"in g||(typeof window<"u"?g.global=window:typeof global<"u"?g.global=global:g.global={});"document"in g||typeof document<"u"&&(g.document=document);var d=g;function C(n){d.global=n}function H(n){d.document=n}var f={Comp:0,For:1,Cond:2,Env:3,Exp:4,Subview:5},p=class{_$dlNodeType;constructor(t){this._$dlNodeType=t}get _$el(){return p.toEls(this._$nodes)}_$parentEl;_$nodes;static toEls(t){let e=[];return this.loopShallowEls(t,s=>{e.push(s)}),e}static loopShallowEls(t,e){t.forEach(s=>{if(!("_$dlNodeType"in s))return e(s);s._$nodes&&p.loopShallowEls(s._$nodes,e)})}static loopShallowDLNodes(t,e){t.forEach(s=>{"_$dlNodeType"in s&&(e(s),s._$nodes&&p.loopShallowDLNodes(s._$nodes,e))})}static addParentEl(t,e){this.loopShallowDLNodes(t,s=>{s._$parentEl=e})}static getFlowIndexFromNodes(t,e){let s=0,o=[...t];for(;o.length>0;){let r=o.shift();if(r===e)break;"_$dlNodeType"in r?r._$nodes&&o.unshift(...r._$nodes):s++}return s}static appendNodesWithSibling(t,e,s){return s?this.insertNodesBefore(t,e,s):this.appendNodes(t,e)}static appendNodesWithIndex(t,e,s,o){return o=o??e.childNodes.length,o!==s?this.insertNodesBefore(t,e,e.childNodes[s]):this.appendNodes(t,e)}static insertNodesBefore(t,e,s){let o=0;return this.loopShallowEls(t,r=>{e.insertBefore(r,s),o++}),o}static appendNodes(t,e){let s=0;return this.loopShallowEls(t,o=>{e.appendChild(o),s++}),s}static lifecycleFunc(t,e,s){t(s),e?.(s)}static addWillUnmount(t,e){t.willUnmount=this.lifecycleFunc.bind(this,e,t.willUnmount?.bind(t),t)}static addDidUnmount(t,e){t.willUnmount=this.lifecycleFunc.bind(this,e,t.willUnmount?.bind(t),t)}static addDidMount(t,e){d.global.DidMountStore.push(()=>e(t))}static runDidMount(){if(!("DidMountStore"in d.global))return;let t=d.global.DidMountStore;for(let e=t.length-1;e>=0;e--)t[e]();d.global.DidMountStore=[]}};function K(n,t){Object.assign(n.style,t)}function j(n,t){Object.assign(n.dataset,t)}function F(n,t,e){let s=`$${t}`;s in n&&n[s]===e||(n[t]=e,n[s]=e)}function V(n,t){Object.entries(t).forEach(([e,s])=>{F(n,e,s)})}function y(n,t,e){let s=`$${t}`;s in n&&n[s]===e||(n.setAttribute(t,e),n[s]=e)}function B(n,t){Object.entries(t).forEach(([e,s])=>{y(n,e,s)})}function q(n,t,e){let s=n[`$on${t}`];s&&n.removeEventListener(t,s),n.addEventListener(t,e),n[`$on${t}`]=e}function Z(n){let t=d.document.createElement("template");t.innerHTML=n;let e=t.content.firstChild;return()=>e.cloneNode(!0)}function k(n){return d.document.createElement(n)}function M(n,t,e){n._$nodes||(n._$nodes=Array.from(n.childNodes)),n._$nodes.splice(e,0,t);let s=p.getFlowIndexFromNodes(n._$nodes,t);p.appendNodesWithIndex([t],n,s),p.addParentEl([t],n)}function L(n,t,e){if(t==="style"){K(n,e);return}if(t==="dataset"){j(n,e);return}if(t!=="element"){if(t==="prop"){V(n,e);return}if(t==="attr"){B(n,e);return}if(t==="innerHTML"){F(n,"innerHTML",e);return}if(t!=="forwardProp"){if(t.startsWith("on")){q(n,t.slice(2).toLowerCase(),e);return}y(n,t,e)}}}var S=class extends p{constructor(){super(f.Comp)}_$init(t,e,s,o){this._$notInitd=!0,o&&o._$addForwardProps(this),e!==null&&this._$setContent(e),t&&Object.entries(t).forEach(([r,m])=>{this._$setProp(r,m)}),s&&(this._$children=s),d.global.DLEnvStore&&Object.entries(d.global.DLEnvStore.envs).forEach(([r,[m,a]])=>{a.addNode(this),this._$initEnv(r,m,a)}),this._$callUpdatesBeforeInit(),"DidMountStore"in d.global||(d.global.DidMountStore=[]),this.didMount&&d.global.DidMountStore.push(this.didMount.bind(this,this)),this.willMount?.(this),this._$nodes=this.View?.()??[]}_$callUpdatesBeforeInit(){let t=Object.getOwnPropertyNames(Object.getPrototypeOf(this)),e=Object.getOwnPropertyNames(this);[...t,...e].forEach(o=>{if(o.startsWith("$w$"))return this[o.slice(3)]();o.startsWith("$f$")&&(this[`$${o.slice(3)}`]=this[o])}),delete this._$notInitd}_$setForwardProp(t,e){if(t in this){this[t]=e;return}this._$forwardPropsId.push(t);let s=`$${t}`;this[s]=e,Object.defineProperty(this,t,{get(){return this[s]},set(o){this[s]!==o&&(this[s]=o,this._$forwardPropsSet?.forEach(r=>{r._$dlNodeType===f.Comp&&r._$setProp(t,o),r instanceof HTMLElement&&L(r,t,o)}))}})}_$addForwardProps(t){this._$forwardPropsSet.add(t),this._$forwardPropsId.forEach(e=>{let s=this[e];this._$forwardPropsSet?.forEach(o=>{o._$dlNodeType===f.Comp&&("_$forwardProps"in o&&o._$forwardPropsId.push(e),o._$setProp(e,s)),o instanceof HTMLElement&&L(o,e,s)})}),p.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,s){`$e$${t}`in this&&(this[t]=e,this[`$en$${t}`]=s)}_$updateEnv(t,e,s){`$e$${t}`in this&&s===this[`$en$${t}`]&&this[t]!==e&&(this[t]=e)}_$setContent(t){let e=this._$contentKey;e&&this[e]!==t&&(this[e]=t)}_$updateProp(t,e){let s=`$${t}`;if(this[s]===e)return;let o=this[s];this[s]=e,this._$updateDerived(t),this._$updateView(t,o,e)}_$updateDerived(t){"_$notInitd"in this||this[`$s$${t}`]?.forEach(e=>{`$w$${e}`in this?this[e]():this[`$${e}`]=this[`$f$${e}`]})}_$updateView(t,e,s){let o=this[`$$${t}`];o&&this._$update?.(o,t,e,s)}},nt=S;function it(n,t){n._$updateDerived(t),n._$updateView(t)}var I=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,s])=>{this.envs[e]=[s,t]})})}},P=class extends p{constructor(t){super(f.Env),"DLEnvStore"in d.global||(d.global.DLEnvStore=new I),this.envs=t,this.updateNodes=new Set,d.global.DLEnvStore.addEnvNode(this)}updateEnv(t,e){this.envs[t]=e,d.global.DLEnvStore.currentEnvNodes.includes(this)&&d.global.DLEnvStore.mergeEnvs(),this.updateNodes.forEach(s=>{s._$updateEnv(t,e,this)})}addNode(t){this.updateNodes.add(t),p.addWillUnmount(t,this.updateNodes.delete.bind(this.updateNodes,t))}initNodes(t){this._$nodes=t,d.global.DLEnvStore.removeEnvNode()}};function ut(n){return d.document.createTextNode(n)}function lt(n,t){n.textContent!==t&&(n.textContent=t)}var T=class{propViewFunc;dlUpdateFunc=new Set;constructor(t){this.propViewFunc=t}build(){let t,e=o=>{o.initd=!0,t=o,this.dlUpdateFunc.add(o)},s=this.propViewFunc(e);return s.length===0?[]:(t&&p.addWillUnmount(s[0],this.dlUpdateFunc.delete.bind(this.dlUpdateFunc,t)),s)}update(...t){this.dlUpdateFunc.forEach(e=>{if(e.initd){delete e.initd;return}e(...t)})}};var A=class extends p{constructor(){super(f.Subview)}};var h=class extends p{constructor(t){super(t),d.global.DLEnvStore&&d.global.DLEnvStore.currentEnvNodes.length>0&&(this.savedEnvNodes=[...d.global.DLEnvStore.currentEnvNodes])}initNewNodes(t){p.addParentEl(t,this._$parentEl)}geneNewNodesInEnv(t){if(!this.savedEnvNodes){let o=t();return this.initNewNodes(o),o}let e=d.global.DLEnvStore.currentEnvNodes;d.global.DLEnvStore.replaceEnvNodes(this.savedEnvNodes);let s=t();return d.global.DLEnvStore.replaceEnvNodes(e),this.initNewNodes(s),s}removeNodes(t,e=!0){t.forEach(s=>{s.willUnmount?.(s),"_$dlNodeType"in s||(e&&this._$parentEl?.removeChild(s),e=!1);let o=s._$nodes??s.childNodes;this.removeNodes(o,e),s.didUnmount?.(s)})}};var _=class extends h{array;keys;nodeFunc;nodess;depNum;updateArr=[];constructor(t,e,s){super(f.For),this.array=[...t],this.keys=s,this.depNum=e}addNodeFunc(t){this.nodeFunc=t,this.nodess=this.array.map((e,s)=>t(e,this.updateArr,s)),this._$nodes=this.nodess.flat(1)}update(t,...e){if(!(t&this.depNum)){this.updateArgs=e;for(let s=0;s<this.array.length;s++)this.updateItem(s,t)}}updateItem(t,e){this.updateArr[t]?.(e??this.depNum,...this.updateArgs,this.array[t])}updateArray(t,e,s,o,r){if(this.updateArgs=[e,s,o],r){this.updateWithKey(t,r);return}this.updateWithOutKey(t)}getNewNodes(t,e){return this.geneNewNodesInEnv(()=>this.nodeFunc(this.array[t],e??this.updateArr,t))}updateWithOutKey(t){let e=this.array.length,s=t.length;if(this.array=[...t],this.updateArr=this.updateArr.slice(0,s),e===s){for(let r=0;r<this.array.length;r++)this.updateItem(r);return}let o=this._$parentEl;if(e<s){let r=h.getFlowIndexFromNodes(o._$nodes,this),m=o.childNodes.length;for(let a=0;a<s;a++){if(a<e){r+=h.getFlowIndexFromNodes(this.nodess[a]),this.updateItem(a);continue}let u=this.getNewNodes(a);h.appendNodesWithIndex(u,o,r,m),h.runDidMount(),this.nodess.push(u)}this._$nodes=this.nodess.flat(1);return}for(let r=0;r<s;r++)this.updateItem(r);for(let r=s;r<e;r++)this.removeNodes(this.nodess[r]);this.nodess=this.nodess.slice(0,s),this._$nodes=this.nodess.flat(1)}updateWithKey(t,e){let s=this.keys;if(this.array=[...t],this.keys=e,_.arrayEqual(s,this.keys)){for(let i=0;i<this.array.length;i++)this.updateItem(i);return}let o=this._$parentEl,r=this.nodess;if(this.keys.length===0){let i=o._$nodes??[];if(i.length===1&&i[0]===this)o.innerHTML="";else for(let l=0;l<s.length;l++)this.removeNodes(r[l]);this.nodess=[],this._$nodes=[],this.updateArr=[];return}let m=h.getFlowIndexFromNodes(o._$nodes,this);if(s.length===0){let i=o.childNodes[m];for(let l=0;l<this.keys.length;l++){let c=this.getNewNodes(l);h.appendNodesWithSibling(c,o,i),h.runDidMount(),this.nodess.push(c)}this._$nodes=this.nodess.flat(1);return}let a=[],u=[],$=[];for(let i=0;i<s.length;i++){let l=s[i];if(this.keys.includes(l)){a.push(l),u.push(r[i]),$.push(this.updateArr[i]);continue}this.removeNodes(r[i])}let E=o.childNodes.length,N=m;for(let i=0;i<this.keys.length;i++){let l=this.keys[i],c=a.indexOf(l);if(c!==-1){N+=h.getFlowIndexFromNodes(u[c]),this.updateArr[c]?.(this.depNum,...this.updateArgs,this.array[i]);continue}$.splice(i,0,null);let w=this.getNewNodes(i,$),v=h.appendNodesWithIndex(w,o,N,E);h.runDidMount(),N+=v,E+=v,u.splice(i,0,w),a.splice(i,0,l)}if(_.arrayEqual(this.keys,a)){this.nodess=u,this._$nodes=this.nodess.flat(1),this.updateArr=$;return}N=m;let D=[];for(let i=0;i<this.keys.length;i++){let l=this.keys[i],c=a.indexOf(l),w=D[i];if(w){let x=h.appendNodesWithIndex(w,o,N+h.getFlowIndexFromNodes(w),E);N+=x,E+=x,D[i]=void 0}else if(c===i){N+=h.getFlowIndexFromNodes(u[i]);continue}else{D[this.keys.indexOf(a[i])]=u[i];let x=h.appendNodesWithIndex(u[c],o,N,E);N+=x,E+=x}let v=u[i];u[i]=u[c],u[c]=v;let W=a[i];a[i]=a[c],a[c]=W;let O=$[i];$[i]=$[c],$[c]=O}this.nodess=u,this._$nodes=this.nodess.flat(1),this.updateArr=$}static arrayEqual(t,e){return t.length!==e.length?!1:t.every((s,o)=>s===e[o])}};var b=class extends h{nodesFunc;constructor(t){super(f.Exp),this.nodesFunc=t,this._$nodes=b.formatNodes(t())}update(){let t=this.geneNewNodesInEnv(()=>b.formatNodes(this.nodesFunc()));if(this.removeNodes(this._$nodes),t.length===0){this._$nodes=[];return}let e=this._$parentEl,s=h.getFlowIndexFromNodes(e._$nodes,this),o=e.childNodes[s];h.appendNodesWithSibling(t,e,o),h.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"?d.document.createTextNode(`${e}`):"propViewFunc"in e?e.build():e).flat(1)}};var U=class extends h{condFunc;cond;depNum;constructor(t){super(f.Cond),this.depNum=t}addCondFunc(t){this.cond=-1,this.condFunc=t,this._$nodes=this.condFunc(this)}updateCond(...t){let e=this.geneNewNodesInEnv(()=>this.condFunc(this));if(this.didntChange)return this.didntChange=!1,this.updateFunc?.(this.depNum,...t),this._$nodes;if(this._$nodes&&this._$nodes.length>0&&this.removeNodes(this._$nodes),e.length===0)return this._$nodes=[],this._$nodes;let s=this._$parentEl,o=h.getFlowIndexFromNodes(s._$nodes,this),r=s.childNodes[o];return h.appendNodesWithSibling(e,s,r),h.runDidMount(),this._$nodes=e,this._$nodes}update(t,...e){t&this.depNum||this.updateFunc?.(t,...e)}};function At(n,t){let e=n;if(typeof n=="string"){let o=d.document.getElementById(n);if(o)e=o;else throw new Error(`DLight: Element with id ${n} not found`)}e.innerHTML="";let s=new t;s._$init(),M(e,s,0),p.runDidMount()}function Ut(n,t){return n()}function G(n){return n}var Wt=G;export{Wt as $,S as CompNode,U as CondNode,P as EnvNode,b as ExpNode,_ as ForNode,T as PropView,A as SubViewNode,nt as View,k as createElement,Z as createTemplate,ut as createTextNode,G as escape,L as forwardHTMLProp,M as insertNode,Ut as manual,At as render,j as setDataset,H as setDocument,q as setEvent,C as setGlobal,y as setHTMLAttr,B as setHTMLAttrs,F as setHTMLProp,V as setHTMLProps,K as setStyle,it as update,lt as updateText}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@dlightjs/dlight", | ||
"version": "1.0.0-alpha.46", | ||
"version": "1.0.0-alpha.47", | ||
"description": "DX-first UI rendering library", | ||
@@ -5,0 +5,0 @@ "author": { |
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
144538
107