diff --git a/dist/dde-with-observables.js b/dist/dde-with-observables.js index 3907924..72528c0 100644 --- a/dist/dde-with-observables.js +++ b/dist/dde-with-observables.js @@ -1,27 +1,30 @@ //deka-dom-el library is available via global namespace `dde` (()=> { -var A={isObservable(e){return!1},processReactiveAttribute(e,t,n,r){return n}};function T(e,t=!0){return t?Object.assign(A,e):(Object.setPrototypeOf(e,A),e)}function C(e){return A.isPrototypeOf(e)&&e!==A?e:A}function x(e){return typeof e>"u"}function B(e){let t=typeof e;return t!=="object"?t:e===null?"null":Object.prototype.toString.call(e)}function N(e,t){if(!e||!(e instanceof AbortSignal))return!0;if(!e.aborted)return e.addEventListener("abort",t),function(){e.removeEventListener("abort",t)}}var $={setDeleteAttr:ee,ssr:""};function ee(e,t,n){if(Reflect.set(e,t,n),!!x(n)){if(Reflect.deleteProperty(e,t),e instanceof HTMLElement&&e.getAttribute(t)==="undefined")return e.removeAttribute(t);if(Reflect.get(e,t)==="undefined")return Reflect.set(e,t,"")}}var E=[{scope:document.body,host:e=>e?e(document.body):document.body,custom_element:!1,prevent:!0}],b={get current(){return E[E.length-1]},get host(){return this.current.host},preventDefault(){let{current:e}=this;return e.prevent=!0,e},get state(){return[...E]},push(e={}){return E.push(Object.assign({},this.current,{prevent:!1},e))},pushRoot(){return E.push(E[0])},pop(){if(E.length!==1)return E.pop()}};function H(...e){return this.appendOriginal(...e),this}function te(e){return e.append===H||(e.appendOriginal=e.append,e.append=H),e}var j;function y(e,t,...n){let r=C(this),o=0,c,s;switch((Object(t)!==t||r.isObservable(t))&&(t={textContent:t}),!0){case typeof e=="function":{o=1,b.push({scope:e,host:(...l)=>l.length?(o===1?n.unshift(...l):l.forEach(v=>v(s)),void 0):s}),c=e(t||void 0);let d=c instanceof DocumentFragment;if(c.nodeName==="#comment")break;let u=y.mark({type:"component",name:e.name,host:d?"this":"parentElement"});c.prepend(u),d&&(s=u);break}case e==="#text":c=P.call(this,document.createTextNode(""),t);break;case(e==="<>"||!e):c=P.call(this,document.createDocumentFragment(),t);break;case!!j:c=P.call(this,document.createElementNS(j,e),t);break;case!c:c=P.call(this,document.createElement(e),t)}return te(c),s||(s=c),n.forEach(d=>d(s)),o&&b.pop(),o=2,c}function _e(e){let t=Symbol.for("default"),n=Array.from(e.querySelectorAll("slot")).reduce((o,c)=>Reflect.set(o,c.name||t,c)&&o,{}),r=Reflect.has(n,t);return e.append=new Proxy(e.append,{apply(o,c,s){if(!s.length)return e;let d=document.createDocumentFragment();for(let u of s){if(!u||!u.slot){r&&d.appendChild(u);continue}let l=u.slot,v=n[l];O(u,"remove","slot"),v&&(v.replaceWith(u),Reflect.deleteProperty(n,l))}return r&&(n[t].replaceWith(d),Reflect.deleteProperty(n,t)),Object.values(n).forEach(u=>u.replaceWith(y().append(...Array.from(u.childNodes)))),e}}),e}y.mark=function(e,t=!1){e=Object.entries(e).map(([o,c])=>o+`="${c}"`).join(" ");let n=t?"":"/",r=document.createComment(``);return t||(r.end=document.createComment("")),r};function Ee(e){let t=this;return function(...r){j=e;let o=y.call(t,...r);return j=void 0,o}}var{setDeleteAttr:I}=$,L=new WeakMap;function P(e,...t){if(!t.length)return e;L.set(e,G(e,this));for(let[n,r]of Object.entries(Object.assign({},...t)))Z.call(this,e,n,r);return L.delete(e),e}function Z(e,t,n){let{setRemoveAttr:r,s:o}=G(e,this),c=this;n=o.processReactiveAttribute(e,t,n,(d,u)=>Z.call(c,e,d,u));let[s]=t;if(s==="=")return r(t.slice(1),n);if(s===".")return J(e,t.slice(1),n);if(/(aria|data)([A-Z])/.test(t))return t=t.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),r(t,n);switch(t==="className"&&(t="class"),t){case"xlink:href":return r(t,n,"http://www.w3.org/1999/xlink");case"textContent":return I(e,t,n);case"style":if(typeof n!="object")break;case"dataset":return F(o,n,J.bind(null,e[t]));case"ariaset":return F(o,n,(d,u)=>r("aria-"+d,u));case"classList":return ne.call(c,e,n)}return re(e,t)?I(e,t,n):r(t,n)}function G(e,t){if(L.has(e))return L.get(e);let r=(e instanceof SVGElement?ce:oe).bind(null,e,"Attribute"),o=C(t);return{setRemoveAttr:r,s:o}}function ne(e,t){let n=C(this);return F(n,t,(r,o)=>e.classList.toggle(r,o===-1?void 0:!!o)),e}function xe(e){return Array.from(e.children).forEach(t=>t.remove()),e}function O(e,t,n,r){return e instanceof HTMLElement?e[t+"Attribute"](n,r):e[t+"AttributeNS"](null,n,r)}function re(e,t){if(!Reflect.has(e,t))return!1;let n=V(e,t);return!x(n.set)}function V(e,t){if(e=Object.getPrototypeOf(e),!e)return{};let n=Object.getOwnPropertyDescriptor(e,t);return n||V(e,t)}function F(e,t,n){if(!(typeof t!="object"||t===null))return Object.entries(t).forEach(function([o,c]){o&&(c=e.processReactiveAttribute(t,o,c,n),n(o,c))})}function K(e){return Array.isArray(e)?e.filter(Boolean).join(" "):e}function oe(e,t,n,r){return e[(x(r)?"remove":"set")+t](n,K(r))}function ce(e,t,n,r,o=null){return e[(x(r)?"remove":"set")+t+"NS"](o,n,K(r))}function J(e,t,n){if(Reflect.set(e,t,n),!!x(n))return Reflect.deleteProperty(e,t)}function we(e,t,n){return t||(t={}),function(o,...c){n&&(c.unshift(o),o=typeof n=="function"?n():n);let s=c.length?new CustomEvent(e,Object.assign({detail:c[0]},t)):new Event(e,t);return o.dispatchEvent(s)}}function _(e,t,n){return function(o){return o.addEventListener(e,t,n),o}}var D=ie(),se=new WeakSet;_.connected=function(e,t){let{custom_element:n}=b.current,r="connected";return typeof t!="object"&&(t={}),t.once=!0,function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,e,t),c.__dde_lifecycleToEvents?c:c.isConnected?(c.dispatchEvent(new Event(s)),c):(N(t.signal,()=>D.offConnected(c,e))&&D.onConnected(c,e),c)}};_.disconnected=function(e,t){let{custom_element:n}=b.current,r="disconnected";return typeof t!="object"&&(t={}),t.once=!0,function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,e,t),c.__dde_lifecycleToEvents||N(t.signal,()=>D.offDisconnected(c,e))&&D.onDisconnected(c,e),c}};var z=new WeakMap;_.disconnectedAsAbort=function(e){if(z.has(e))return z.get(e);let t=new AbortController;return z.set(e,t),e(_.disconnected(()=>t.abort())),t};_.attributeChanged=function(e,t){let n="attributeChanged";return typeof t!="object"&&(t={}),function(o){let c="dde:"+n;if(o.addEventListener(c,e,t),o.__dde_lifecycleToEvents||se.has(o))return o;let s=new MutationObserver(function(u){for(let{attributeName:l,target:v}of u)v.dispatchEvent(new CustomEvent(c,{detail:[l,v.getAttribute(l)]}))});return N(t.signal,()=>s.disconnect())&&s.observe(o,{attributes:!0}),o}};function ie(){let e=new Map,t=!1,n=new MutationObserver(function(i){for(let f of i)if(f.type==="childList"){if(l(f.addedNodes,!0)){s();continue}v(f.removedNodes,!0)&&s()}});return{onConnected(i,f){c();let a=o(i);a.connected.has(f)||(a.connected.add(f),a.length_c+=1)},offConnected(i,f){if(!e.has(i))return;let a=e.get(i);a.connected.has(f)&&(a.connected.delete(f),a.length_c-=1,r(i,a))},onDisconnected(i,f){c();let a=o(i);a.disconnected.has(f)||(a.disconnected.add(f),a.length_d+=1)},offDisconnected(i,f){if(!e.has(i))return;let a=e.get(i);a.disconnected.has(f)&&(a.disconnected.delete(f),a.length_d-=1,r(i,a))}};function r(i,f){f.length_c||f.length_d||(e.delete(i),s())}function o(i){if(e.has(i))return e.get(i);let f={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return e.set(i,f),f}function c(){t||(t=!0,n.observe(document.body,{childList:!0,subtree:!0}))}function s(){!t||e.size||(t=!1,n.disconnect())}function d(){return new Promise(function(i){(requestIdleCallback||requestAnimationFrame)(i)})}async function u(i){e.size>30&&await d();let f=[];if(!(i instanceof Node))return f;for(let a of e.keys())a===i||!(a instanceof Node)||i.contains(a)&&f.push(a);return f}function l(i,f){let a=!1;for(let g of i){if(f&&u(g).then(l),!e.has(g))continue;let w=e.get(g);w.length_c&&(g.dispatchEvent(new Event("dde:connected")),w.connected=new WeakSet,w.length_c=0,w.length_d||e.delete(g),a=!0)}return a}function v(i,f){let a=!1;for(let g of i)f&&u(g).then(v),!(!e.has(g)||!e.get(g).length_d)&&(g.dispatchEvent(new Event("dde:disconnected")),e.delete(g),a=!0);return a}}var p=Symbol.for("observable");function W(e){try{return Reflect.has(e,p)}catch{return!1}}var M=[],h=new WeakMap;function m(e,t){if(typeof e!="function")return Q(e,t);if(W(e))return e;let n=Q(),r=function(){let[o,...c]=h.get(r);if(h.set(r,new Set([o])),M.push(r),n(e()),M.pop(),!c.length)return;let s=h.get(r);for(let d of c)s.has(d)||S(d,r)};return h.set(n[p],r),h.set(r,new Set([n])),r(),n}m.action=function(e,t,...n){let r=e[p],{actions:o}=r;if(!o||!Reflect.has(o,t))throw new Error(`'${e}' has no action with name '${t}'!`);if(o[t].apply(r,n),r.skip)return Reflect.deleteProperty(r,"skip");r.listeners.forEach(c=>c(r.value))};m.on=function e(t,n,r={}){let{signal:o}=r;if(!(o&&o.aborted)){if(Array.isArray(t))return t.forEach(c=>e(c,n,r));U(t,n),o&&o.addEventListener("abort",()=>S(t,n))}};m.symbols={onclear:Symbol.for("Observable.onclear")};m.clear=function(...e){for(let n of e){Reflect.deleteProperty(n,"toJSON");let r=n[p];r.onclear.forEach(o=>o.call(r)),t(n,r),Reflect.deleteProperty(n,p)}function t(n,r){r.listeners.forEach(o=>{if(r.listeners.delete(o),!h.has(o))return;let c=h.get(o);c.delete(n),!(c.size>1)&&(n.clear(...c),h.delete(o))})}};var k="__dde_reactive";m.el=function(e,t){let n=y.mark({type:"reactive"},!1),r=n.end,o=document.createDocumentFragment();o.append(n,r);let{current:c}=b,s=d=>{if(!n.parentNode||!r.parentNode)return S(e,s);b.push(c);let u=t(d);b.pop(),Array.isArray(u)||(u=[u]);let l=n;for(;(l=n.nextSibling)!==r;)l.remove();n.after(...u)};return U(e,s),Y(e,s,n,t),s(e()),o};var R="__dde_attributes";m.attribute=function(e,t=null){let n=m(t),r;return b.host(o=>{if(r=o,O(r,"has",e)?n(O(r,"get",e)):t!==null&&O(r,"set",e,t),o[R]){o[R][e]=n;return}r[R]={[e]:n},_.attributeChanged(function({detail:s}){/*! This maps attributes to observables (`S.attribute`). -* Investigate `__dde_attributes` key of the element.*/let[d,u]=s,l=r[R][d];if(l)return l(u)})(r),_.disconnected(function(){/*! This removes all observables mapped to attributes (`S.attribute`). -* Investigate `__dde_attributes` key of the element.*/m.clear(...Object.values(r[R]))})(r)}),new Proxy(n,{apply(o,c,s){if(!s.length)return o();let d=s[0];return O(r,"set",e,d)}})};var X={isObservable:W,processReactiveAttribute(e,t,n,r){if(!W(n))return n;let o=c=>r(t,c);return U(n,o),Y(n,o,e,t),n()}};function Y(e,t,...n){let{current:r}=b;r.prevent||r.host(function(o){o[k]||(o[k]=[],_.disconnected(()=>o[k].forEach(([[c,s]])=>S(c,s,c[p]?.host()===o)))(o)),o[k].push([[e,t],...n])})}function Q(e,t){let n=(...r)=>r.length?le(n,...r):de(n);return ue(n,e,t)}var fe=Object.assign(Object.create(null),{stopPropagation(){this.skip=!0}}),q=class extends Error{constructor(){super();let[t,...n]=this.stack.split(` -`),r=t.slice(t.indexOf("@"),t.indexOf(".js:")+4);this.stack=n.find(o=>!o.includes(r))}};function ue(e,t,n){let r=[];B(n)!=="[object Object]"&&(n={});let{onclear:o}=m.symbols;n[o]&&(r.push(n[o]),Reflect.deleteProperty(n,o));let{host:c}=b;return Reflect.defineProperty(e,p,{value:{value:t,actions:n,onclear:r,host:c,listeners:new Set,defined:new q},enumerable:!1,writable:!1,configurable:!0}),e.toJSON=()=>e(),Object.setPrototypeOf(e[p],fe),e}function ae(){return M[M.length-1]}function de(e){if(!e[p])return;let{value:t,listeners:n}=e[p],r=ae();return r&&n.add(r),h.has(r)&&h.get(r).add(e),t}function le(e,t,n){if(!e[p])return;let r=e[p];if(!(!n&&r.value===t))return r.value=t,r.listeners.forEach(o=>o(t)),t}function U(e,t){if(e[p])return e[p].listeners.add(t)}function S(e,t,n){let r=e[p];if(!r)return;let o=r.listeners.delete(t);if(n&&!r.listeners.size){if(e.clear(e),!h.has(r))return o;let c=h.get(r);if(!h.has(c))return o;h.get(c).forEach(s=>S(s,c,!0))}return o}T(X); +var w={isObservable(e){return!1},processReactiveAttribute(e,t,n,r){return n}};function M(e,t=!0){return t?Object.assign(w,e):(Object.setPrototypeOf(e,w),e)}function N(e){return w.isPrototypeOf(e)&&e!==w?e:w}function x(e){return typeof e>"u"}function B(e){let t=typeof e;return t!=="object"?t:e===null?"null":Object.prototype.toString.call(e)}function P(e,t){if(!e||!(e instanceof AbortSignal))return!0;if(!e.aborted)return e.addEventListener("abort",t),function(){e.removeEventListener("abort",t)}}var $={setDeleteAttr:te,ssr:""};function te(e,t,n){if(Reflect.set(e,t,n),!!x(n)){if(Reflect.deleteProperty(e,t),e instanceof HTMLElement&&e.getAttribute(t)==="undefined")return e.removeAttribute(t);if(Reflect.get(e,t)==="undefined")return Reflect.set(e,t,"")}}var y=[{scope:document.body,host:e=>e?e(document.body):document.body,custom_element:!1,prevent:!0}],p={get current(){return y[y.length-1]},get host(){return this.current.host},preventDefault(){let{current:e}=this;return e.prevent=!0,e},get state(){return[...y]},push(e={}){return y.push(Object.assign({},this.current,{prevent:!1},e))},pushRoot(){return y.push(y[0])},pop(){if(y.length!==1)return y.pop()}};function H(...e){return this.appendOriginal(...e),this}function ne(e){return e.append===H||(e.appendOriginal=e.append,e.append=H),e}var L;function R(e,t,...n){let r=N(this),o=0,c,s;switch((Object(t)!==t||r.isObservable(t))&&(t={textContent:t}),!0){case typeof e=="function":{o=1,p.push({scope:e,host:(...l)=>l.length?(o===1?n.unshift(...l):l.forEach(b=>b(s)),void 0):s}),c=e(t||void 0);let u=c instanceof DocumentFragment;if(c.nodeName==="#comment")break;let d=R.mark({type:"component",name:e.name,host:u?"this":"parentElement"});c.prepend(d),u&&(s=d);break}case e==="#text":c=A.call(this,document.createTextNode(""),t);break;case(e==="<>"||!e):c=A.call(this,document.createDocumentFragment(),t);break;case!!L:c=A.call(this,document.createElementNS(L,e),t);break;case!c:c=A.call(this,document.createElement(e),t)}return ne(c),s||(s=c),n.forEach(u=>u(s)),o&&p.pop(),o=2,c}function xe(e,t=e,n=void 0){let r=Symbol.for("default"),o=Array.from(t.querySelectorAll("slot")).reduce((s,u)=>Reflect.set(s,u.name||r,u)&&s,{}),c=Reflect.has(o,r);if(e.append=new Proxy(e.append,{apply(s,u,d){if(!d.length)return e;let l=document.createDocumentFragment();for(let b of d){if(!b||!b.slot){c&&l.appendChild(b);continue}let i=b.slot,f=o[i];_(b,"remove","slot"),f&&(re(f,b,n),Reflect.deleteProperty(o,i))}return c&&(o[r].replaceWith(l),Reflect.deleteProperty(o,r)),e.append=s,e}}),e!==t){let s=Array.from(e.childNodes);s.forEach(u=>u.remove()),e.append(...s)}return t}function re(e,t,n){n&&n(e,t);try{e.replaceWith(A(t,{className:[t.className,e.className],dataset:{...e.dataset}}))}catch{e.replaceWith(t)}}R.mark=function(e,t=!1){e=Object.entries(e).map(([o,c])=>o+`="${c}"`).join(" ");let n=t?"":"/",r=document.createComment(``);return t||(r.end=document.createComment("")),r};function _e(e){let t=this;return function(...r){L=e;let o=R.call(t,...r);return L=void 0,o}}var{setDeleteAttr:I}=$,j=new WeakMap;function A(e,...t){if(!t.length)return e;j.set(e,G(e,this));for(let[n,r]of Object.entries(Object.assign({},...t)))Z.call(this,e,n,r);return j.delete(e),e}function Z(e,t,n){let{setRemoveAttr:r,s:o}=G(e,this),c=this;n=o.processReactiveAttribute(e,t,n,(u,d)=>Z.call(c,e,u,d));let[s]=t;if(s==="=")return r(t.slice(1),n);if(s===".")return J(e,t.slice(1),n);if(/(aria|data)([A-Z])/.test(t))return t=t.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),r(t,n);switch(t==="className"&&(t="class"),t){case"xlink:href":return r(t,n,"http://www.w3.org/1999/xlink");case"textContent":return I(e,t,n);case"style":if(typeof n!="object")break;case"dataset":return F(o,n,J.bind(null,e[t]));case"ariaset":return F(o,n,(u,d)=>r("aria-"+u,d));case"classList":return oe.call(c,e,n)}return ce(e,t)?I(e,t,n):r(t,n)}function G(e,t){if(j.has(e))return j.get(e);let r=(e instanceof SVGElement?ie:se).bind(null,e,"Attribute"),o=N(t);return{setRemoveAttr:r,s:o}}function oe(e,t){let n=N(this);return F(n,t,(r,o)=>e.classList.toggle(r,o===-1?void 0:!!o)),e}function Oe(e){return Array.from(e.children).forEach(t=>t.remove()),e}function _(e,t,n,r){return e instanceof HTMLElement?e[t+"Attribute"](n,r):e[t+"AttributeNS"](null,n,r)}function ce(e,t){if(!Reflect.has(e,t))return!1;let n=V(e,t);return!x(n.set)}function V(e,t){if(e=Object.getPrototypeOf(e),!e)return{};let n=Object.getOwnPropertyDescriptor(e,t);return n||V(e,t)}function F(e,t,n){if(!(typeof t!="object"||t===null))return Object.entries(t).forEach(function([o,c]){o&&(c=e.processReactiveAttribute(t,o,c,n),n(o,c))})}function K(e){return Array.isArray(e)?e.filter(Boolean).join(" "):e}function se(e,t,n,r){return e[(x(r)?"remove":"set")+t](n,K(r))}function ie(e,t,n,r,o=null){return e[(x(r)?"remove":"set")+t+"NS"](o,n,K(r))}function J(e,t,n){if(Reflect.set(e,t,n),!!x(n))return Reflect.deleteProperty(e,t)}function Se(e,t,n=e){p.push({scope:e,host:(...o)=>o.length?o.forEach(c=>c(e)):e,custom_element:e});let r=t.call(e,n);return p.pop(),r}function Ce(e){for(let n of["connected","disconnected"])Q(e.prototype,n+"Callback",function(r,o,c){r.apply(o,c),o.dispatchEvent(new Event("dde:"+n))});let t="attributeChanged";return Q(e.prototype,t+"Callback",function(n,r,o){let[c,,s]=o;r.dispatchEvent(new CustomEvent("dde:"+t,{detail:[c,s]})),n.apply(r,o)}),e.prototype.__dde_lifecycleToEvents=!0,e}function Q(e,t,n){e[t]=new Proxy(e[t]||(()=>{}),{apply:n})}function Pe(e,t,n){return t||(t={}),function(o,...c){n&&(c.unshift(o),o=typeof n=="function"?n():n);let s=c.length?new CustomEvent(e,Object.assign({detail:c[0]},t)):new Event(e,t);return o.dispatchEvent(s)}}function E(e,t,n){return function(o){return o.addEventListener(e,t,n),o}}var D=ue(),fe=new WeakSet;E.connected=function(e,t){let{custom_element:n}=p.current,r="connected";return typeof t!="object"&&(t={}),t.once=!0,function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,e,t),c.__dde_lifecycleToEvents?c:c.isConnected?(c.dispatchEvent(new Event(s)),c):(P(t.signal,()=>D.offConnected(c,e))&&D.onConnected(c,e),c)}};E.disconnected=function(e,t){let{custom_element:n}=p.current,r="disconnected";return typeof t!="object"&&(t={}),t.once=!0,function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,e,t),c.__dde_lifecycleToEvents||P(t.signal,()=>D.offDisconnected(c,e))&&D.onDisconnected(c,e),c}};var z=new WeakMap;E.disconnectedAsAbort=function(e){if(z.has(e))return z.get(e);let t=new AbortController;return z.set(e,t),e(E.disconnected(()=>t.abort())),t};E.attributeChanged=function(e,t){let n="attributeChanged";return typeof t!="object"&&(t={}),function(o){let c="dde:"+n;if(o.addEventListener(c,e,t),o.__dde_lifecycleToEvents||fe.has(o))return o;let s=new MutationObserver(function(d){for(let{attributeName:l,target:b}of d)b.dispatchEvent(new CustomEvent(c,{detail:[l,b.getAttribute(l)]}))});return P(t.signal,()=>s.disconnect())&&s.observe(o,{attributes:!0}),o}};function ue(){let e=new Map,t=!1,n=new MutationObserver(function(i){for(let f of i)if(f.type==="childList"){if(l(f.addedNodes,!0)){s();continue}b(f.removedNodes,!0)&&s()}});return{onConnected(i,f){c();let a=o(i);a.connected.has(f)||(a.connected.add(f),a.length_c+=1)},offConnected(i,f){if(!e.has(i))return;let a=e.get(i);a.connected.has(f)&&(a.connected.delete(f),a.length_c-=1,r(i,a))},onDisconnected(i,f){c();let a=o(i);a.disconnected.has(f)||(a.disconnected.add(f),a.length_d+=1)},offDisconnected(i,f){if(!e.has(i))return;let a=e.get(i);a.disconnected.has(f)&&(a.disconnected.delete(f),a.length_d-=1,r(i,a))}};function r(i,f){f.length_c||f.length_d||(e.delete(i),s())}function o(i){if(e.has(i))return e.get(i);let f={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return e.set(i,f),f}function c(){t||(t=!0,n.observe(document.body,{childList:!0,subtree:!0}))}function s(){!t||e.size||(t=!1,n.disconnect())}function u(){return new Promise(function(i){(requestIdleCallback||requestAnimationFrame)(i)})}async function d(i){e.size>30&&await u();let f=[];if(!(i instanceof Node))return f;for(let a of e.keys())a===i||!(a instanceof Node)||i.contains(a)&&f.push(a);return f}function l(i,f){let a=!1;for(let v of i){if(f&&d(v).then(l),!e.has(v))continue;let O=e.get(v);O.length_c&&(v.dispatchEvent(new Event("dde:connected")),O.connected=new WeakSet,O.length_c=0,O.length_d||e.delete(v),a=!0)}return a}function b(i,f){let a=!1;for(let v of i)f&&d(v).then(b),!(!e.has(v)||!e.get(v).length_d)&&(v.dispatchEvent(new Event("dde:disconnected")),e.delete(v),a=!0);return a}}var h=Symbol.for("observable");function W(e){try{return Reflect.has(e,h)}catch{return!1}}var T=[],g=new WeakMap;function m(e,t){if(typeof e!="function")return X(e,t);if(W(e))return e;let n=X(),r=function(){let[o,...c]=g.get(r);if(g.set(r,new Set([o])),T.push(r),n(e()),T.pop(),!c.length)return;let s=g.get(r);for(let u of c)s.has(u)||C(u,r)};return g.set(n[h],r),g.set(r,new Set([n])),r(),n}m.action=function(e,t,...n){let r=e[h],{actions:o}=r;if(!o||!Reflect.has(o,t))throw new Error(`'${e}' has no action with name '${t}'!`);if(o[t].apply(r,n),r.skip)return Reflect.deleteProperty(r,"skip");r.listeners.forEach(c=>c(r.value))};m.on=function e(t,n,r={}){let{signal:o}=r;if(!(o&&o.aborted)){if(Array.isArray(t))return t.forEach(c=>e(c,n,r));U(t,n),o&&o.addEventListener("abort",()=>C(t,n))}};m.symbols={onclear:Symbol.for("Observable.onclear")};m.clear=function(...e){for(let n of e){Reflect.deleteProperty(n,"toJSON");let r=n[h];r.onclear.forEach(o=>o.call(r)),t(n,r),Reflect.deleteProperty(n,h)}function t(n,r){r.listeners.forEach(o=>{if(r.listeners.delete(o),!g.has(o))return;let c=g.get(o);c.delete(n),!(c.size>1)&&(n.clear(...c),g.delete(o))})}};var k="__dde_reactive";m.el=function(e,t){let n=R.mark({type:"reactive"},!1),r=n.end,o=document.createDocumentFragment();o.append(n,r);let{current:c}=p,s=u=>{if(!n.parentNode||!r.parentNode)return C(e,s);p.push(c);let d=t(u);p.pop(),Array.isArray(d)||(d=[d]);let l=n;for(;(l=n.nextSibling)!==r;)l.remove();n.after(...d)};return U(e,s),ee(e,s,n,t),s(e()),o};var S="__dde_attributes";m.attribute=function(e,t=null){let n=m(t),r;return p.host(o=>{if(r=o,_(r,"has",e)?n(_(r,"get",e)):t!==null&&_(r,"set",e,t),o[S]){o[S][e]=n;return}r[S]={[e]:n},E.attributeChanged(function({detail:s}){/*! This maps attributes to observables (`S.attribute`). +* Investigate `__dde_attributes` key of the element.*/let[u,d]=s,l=r[S][u];if(l)return l(d)})(r),E.disconnected(function(){/*! This removes all observables mapped to attributes (`S.attribute`). +* Investigate `__dde_attributes` key of the element.*/m.clear(...Object.values(r[S]))})(r)}),new Proxy(n,{apply(o,c,s){if(!s.length)return o();let u=s[0];return _(r,"set",e,u)}})};var Y={isObservable:W,processReactiveAttribute(e,t,n,r){if(!W(n))return n;let o=c=>r(t,c);return U(n,o),ee(n,o,e,t),n()}};function ee(e,t,...n){let{current:r}=p;r.prevent||r.host(function(o){o[k]||(o[k]=[],E.disconnected(()=>o[k].forEach(([[c,s]])=>C(c,s,c[h]?.host()===o)))(o)),o[k].push([[e,t],...n])})}function X(e,t){let n=(...r)=>r.length?he(n,...r):pe(n);return de(n,e,t)}var ae=Object.assign(Object.create(null),{stopPropagation(){this.skip=!0}}),q=class extends Error{constructor(){super();let[t,...n]=this.stack.split(` +`),r=t.slice(t.indexOf("@"),t.indexOf(".js:")+4);this.stack=n.find(o=>!o.includes(r))}};function de(e,t,n){let r=[];B(n)!=="[object Object]"&&(n={});let{onclear:o}=m.symbols;n[o]&&(r.push(n[o]),Reflect.deleteProperty(n,o));let{host:c}=p;return Reflect.defineProperty(e,h,{value:{value:t,actions:n,onclear:r,host:c,listeners:new Set,defined:new q},enumerable:!1,writable:!1,configurable:!0}),e.toJSON=()=>e(),Object.setPrototypeOf(e[h],ae),e}function le(){return T[T.length-1]}function pe(e){if(!e[h])return;let{value:t,listeners:n}=e[h],r=le();return r&&n.add(r),g.has(r)&&g.get(r).add(e),t}function he(e,t,n){if(!e[h])return;let r=e[h];if(!(!n&&r.value===t))return r.value=t,r.listeners.forEach(o=>o(t)),t}function U(e,t){if(e[h])return e[h].listeners.add(t)}function C(e,t,n){let r=e[h];if(!r)return;let o=r.listeners.delete(t);if(n&&!r.listeners.size){if(e.clear(e),!g.has(r))return o;let c=g.get(r);if(!g.has(c))return o;g.get(c).forEach(s=>C(s,c,!0))}return o}M(Y); globalThis.dde= {O: m, -assign: P, +assign: A, assignAttribute: Z, -chainableAppend: te, -classListDeclarative: ne, -createElement: y, -createElementNS: Ee, -dispatchEvent: we, -el: y, -elNS: Ee, -elementAttribute: O, -empty: xe, +chainableAppend: ne, +classListDeclarative: oe, +createElement: R, +createElementNS: _e, +customElementRender: Se, +customElementWithDDE: Ce, +dispatchEvent: Pe, +el: R, +elNS: _e, +elementAttribute: _, +empty: Oe, isObservable: W, +lifecycleToEvents: Ce, observable: m, -on: _, -registerReactivity: T, -scope: b, -simulateSlots: _e +on: E, +registerReactivity: M, +scope: p, +simulateSlots: xe }; })(); \ No newline at end of file diff --git a/dist/dde.js b/dist/dde.js index 1286bc5..d1540de 100644 --- a/dist/dde.js +++ b/dist/dde.js @@ -1,21 +1,24 @@ //deka-dom-el library is available via global namespace `dde` (()=> { -var v={isObservable(t){return!1},processReactiveAttribute(t,e,n,c){return n}};function q(t,e=!0){return e?Object.assign(v,t):(Object.setPrototypeOf(t,v),t)}function x(t){return v.isPrototypeOf(t)&&t!==v?t:v}function g(t){return typeof t>"u"}function A(t,e){if(!t||!(t instanceof AbortSignal))return!0;if(!t.aborted)return t.addEventListener("abort",e),function(){t.removeEventListener("abort",e)}}var S={setDeleteAttr:F,ssr:""};function F(t,e,n){if(Reflect.set(t,e,n),!!g(n)){if(Reflect.deleteProperty(t,e),t instanceof HTMLElement&&t.getAttribute(e)==="undefined")return t.removeAttribute(e);if(Reflect.get(t,e)==="undefined")return Reflect.set(t,e,"")}}var b=[{scope:document.body,host:t=>t?t(document.body):document.body,custom_element:!1,prevent:!0}],E={get current(){return b[b.length-1]},get host(){return this.current.host},preventDefault(){let{current:t}=this;return t.prevent=!0,t},get state(){return[...b]},push(t={}){return b.push(Object.assign({},this.current,{prevent:!1},t))},pushRoot(){return b.push(b[0])},pop(){if(b.length!==1)return b.pop()}};function L(...t){return this.appendOriginal(...t),this}function U(t){return t.append===L||(t.appendOriginal=t.append,t.append=L),t}var O;function C(t,e,...n){let c=x(this),o=0,r,f;switch((Object(e)!==e||c.isObservable(e))&&(e={textContent:e}),!0){case typeof t=="function":{o=1,E.push({scope:t,host:(...l)=>l.length?(o===1?n.unshift(...l):l.forEach(h=>h(f)),void 0):f}),r=t(e||void 0);let d=r instanceof DocumentFragment;if(r.nodeName==="#comment")break;let a=C.mark({type:"component",name:t.name,host:d?"this":"parentElement"});r.prepend(a),d&&(f=a);break}case t==="#text":r=w.call(this,document.createTextNode(""),e);break;case(t==="<>"||!t):r=w.call(this,document.createDocumentFragment(),e);break;case!!O:r=w.call(this,document.createElementNS(O,t),e);break;case!r:r=w.call(this,document.createElement(t),e)}return U(r),f||(f=r),n.forEach(d=>d(f)),o&&E.pop(),o=2,r}function tt(t){let e=Symbol.for("default"),n=Array.from(t.querySelectorAll("slot")).reduce((o,r)=>Reflect.set(o,r.name||e,r)&&o,{}),c=Reflect.has(n,e);return t.append=new Proxy(t.append,{apply(o,r,f){if(!f.length)return t;let d=document.createDocumentFragment();for(let a of f){if(!a||!a.slot){c&&d.appendChild(a);continue}let l=a.slot,h=n[l];B(a,"remove","slot"),h&&(h.replaceWith(a),Reflect.deleteProperty(n,l))}return c&&(n[e].replaceWith(d),Reflect.deleteProperty(n,e)),Object.values(n).forEach(a=>a.replaceWith(C().append(...Array.from(a.childNodes)))),t}}),t}C.mark=function(t,e=!1){t=Object.entries(t).map(([o,r])=>o+`="${r}"`).join(" ");let n=e?"":"/",c=document.createComment(``);return e||(c.end=document.createComment("")),c};function et(t){let e=this;return function(...c){O=t;let o=C.call(e,...c);return O=void 0,o}}var{setDeleteAttr:j}=S,y=new WeakMap;function w(t,...e){if(!e.length)return t;y.set(t,M(t,this));for(let[n,c]of Object.entries(Object.assign({},...e)))W.call(this,t,n,c);return y.delete(t),t}function W(t,e,n){let{setRemoveAttr:c,s:o}=M(t,this),r=this;n=o.processReactiveAttribute(t,e,n,(d,a)=>W.call(r,t,d,a));let[f]=e;if(f==="=")return c(e.slice(1),n);if(f===".")return P(t,e.slice(1),n);if(/(aria|data)([A-Z])/.test(e))return e=e.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),c(e,n);switch(e==="className"&&(e="class"),e){case"xlink:href":return c(e,n,"http://www.w3.org/1999/xlink");case"textContent":return j(t,e,n);case"style":if(typeof n!="object")break;case"dataset":return N(o,n,P.bind(null,t[e]));case"ariaset":return N(o,n,(d,a)=>c("aria-"+d,a));case"classList":return z.call(r,t,n)}return k(t,e)?j(t,e,n):c(e,n)}function M(t,e){if(y.has(t))return y.get(t);let c=(t instanceof SVGElement?I:H).bind(null,t,"Attribute"),o=x(e);return{setRemoveAttr:c,s:o}}function z(t,e){let n=x(this);return N(n,e,(c,o)=>t.classList.toggle(c,o===-1?void 0:!!o)),t}function nt(t){return Array.from(t.children).forEach(e=>e.remove()),t}function B(t,e,n,c){return t instanceof HTMLElement?t[e+"Attribute"](n,c):t[e+"AttributeNS"](null,n,c)}function k(t,e){if(!Reflect.has(t,e))return!1;let n=T(t,e);return!g(n.set)}function T(t,e){if(t=Object.getPrototypeOf(t),!t)return{};let n=Object.getOwnPropertyDescriptor(t,e);return n||T(t,e)}function N(t,e,n){if(!(typeof e!="object"||e===null))return Object.entries(e).forEach(function([o,r]){o&&(r=t.processReactiveAttribute(e,o,r,n),n(o,r))})}function $(t){return Array.isArray(t)?t.filter(Boolean).join(" "):t}function H(t,e,n,c){return t[(g(c)?"remove":"set")+e](n,$(c))}function I(t,e,n,c,o=null){return t[(g(c)?"remove":"set")+e+"NS"](o,n,$(c))}function P(t,e,n){if(Reflect.set(t,e,n),!!g(n))return Reflect.deleteProperty(t,e)}function ct(t,e,n){return e||(e={}),function(o,...r){n&&(r.unshift(o),o=typeof n=="function"?n():n);let f=r.length?new CustomEvent(t,Object.assign({detail:r[0]},e)):new Event(t,e);return o.dispatchEvent(f)}}function _(t,e,n){return function(o){return o.addEventListener(t,e,n),o}}var R=G(),Z=new WeakSet;_.connected=function(t,e){let{custom_element:n}=E.current,c="connected";return typeof e!="object"&&(e={}),e.once=!0,function(r){n&&(r=n);let f="dde:"+c;return r.addEventListener(f,t,e),r.__dde_lifecycleToEvents?r:r.isConnected?(r.dispatchEvent(new Event(f)),r):(A(e.signal,()=>R.offConnected(r,t))&&R.onConnected(r,t),r)}};_.disconnected=function(t,e){let{custom_element:n}=E.current,c="disconnected";return typeof e!="object"&&(e={}),e.once=!0,function(r){n&&(r=n);let f="dde:"+c;return r.addEventListener(f,t,e),r.__dde_lifecycleToEvents||A(e.signal,()=>R.offDisconnected(r,t))&&R.onDisconnected(r,t),r}};var D=new WeakMap;_.disconnectedAsAbort=function(t){if(D.has(t))return D.get(t);let e=new AbortController;return D.set(t,e),t(_.disconnected(()=>e.abort())),e};_.attributeChanged=function(t,e){let n="attributeChanged";return typeof e!="object"&&(e={}),function(o){let r="dde:"+n;if(o.addEventListener(r,t,e),o.__dde_lifecycleToEvents||Z.has(o))return o;let f=new MutationObserver(function(a){for(let{attributeName:l,target:h}of a)h.dispatchEvent(new CustomEvent(r,{detail:[l,h.getAttribute(l)]}))});return A(e.signal,()=>f.disconnect())&&f.observe(o,{attributes:!0}),o}};function G(){let t=new Map,e=!1,n=new MutationObserver(function(s){for(let i of s)if(i.type==="childList"){if(l(i.addedNodes,!0)){f();continue}h(i.removedNodes,!0)&&f()}});return{onConnected(s,i){r();let u=o(s);u.connected.has(i)||(u.connected.add(i),u.length_c+=1)},offConnected(s,i){if(!t.has(s))return;let u=t.get(s);u.connected.has(i)&&(u.connected.delete(i),u.length_c-=1,c(s,u))},onDisconnected(s,i){r();let u=o(s);u.disconnected.has(i)||(u.disconnected.add(i),u.length_d+=1)},offDisconnected(s,i){if(!t.has(s))return;let u=t.get(s);u.disconnected.has(i)&&(u.disconnected.delete(i),u.length_d-=1,c(s,u))}};function c(s,i){i.length_c||i.length_d||(t.delete(s),f())}function o(s){if(t.has(s))return t.get(s);let i={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return t.set(s,i),i}function r(){e||(e=!0,n.observe(document.body,{childList:!0,subtree:!0}))}function f(){!e||t.size||(e=!1,n.disconnect())}function d(){return new Promise(function(s){(requestIdleCallback||requestAnimationFrame)(s)})}async function a(s){t.size>30&&await d();let i=[];if(!(s instanceof Node))return i;for(let u of t.keys())u===s||!(u instanceof Node)||s.contains(u)&&i.push(u);return i}function l(s,i){let u=!1;for(let p of s){if(i&&a(p).then(l),!t.has(p))continue;let m=t.get(p);m.length_c&&(p.dispatchEvent(new Event("dde:connected")),m.connected=new WeakSet,m.length_c=0,m.length_d||t.delete(p),u=!0)}return u}function h(s,i){let u=!1;for(let p of s)i&&a(p).then(h),!(!t.has(p)||!t.get(p).length_d)&&(p.dispatchEvent(new Event("dde:disconnected")),t.delete(p),u=!0);return u}} -globalThis.dde= {assign: w, -assignAttribute: W, +var m={isObservable(t){return!1},processReactiveAttribute(t,e,n,c){return n}};function q(t,e=!0){return e?Object.assign(m,t):(Object.setPrototypeOf(t,m),t)}function y(t){return m.isPrototypeOf(t)&&t!==m?t:m}function v(t){return typeof t>"u"}function w(t,e){if(!t||!(t instanceof AbortSignal))return!0;if(!t.aborted)return t.addEventListener("abort",e),function(){t.removeEventListener("abort",e)}}var R={setDeleteAttr:F,ssr:""};function F(t,e,n){if(Reflect.set(t,e,n),!!v(n)){if(Reflect.deleteProperty(t,e),t instanceof HTMLElement&&t.getAttribute(e)==="undefined")return t.removeAttribute(e);if(Reflect.get(t,e)==="undefined")return Reflect.set(t,e,"")}}var b=[{scope:document.body,host:t=>t?t(document.body):document.body,custom_element:!1,prevent:!0}],g={get current(){return b[b.length-1]},get host(){return this.current.host},preventDefault(){let{current:t}=this;return t.prevent=!0,t},get state(){return[...b]},push(t={}){return b.push(Object.assign({},this.current,{prevent:!1},t))},pushRoot(){return b.push(b[0])},pop(){if(b.length!==1)return b.pop()}};function L(...t){return this.appendOriginal(...t),this}function U(t){return t.append===L||(t.appendOriginal=t.append,t.append=L),t}var A;function N(t,e,...n){let c=y(this),o=0,r,s;switch((Object(e)!==e||c.isObservable(e))&&(e={textContent:e}),!0){case typeof t=="function":{o=1,g.push({scope:t,host:(...l)=>l.length?(o===1?n.unshift(...l):l.forEach(p=>p(s)),void 0):s}),r=t(e||void 0);let a=r instanceof DocumentFragment;if(r.nodeName==="#comment")break;let d=N.mark({type:"component",name:t.name,host:a?"this":"parentElement"});r.prepend(d),a&&(s=d);break}case t==="#text":r=x.call(this,document.createTextNode(""),e);break;case(t==="<>"||!t):r=x.call(this,document.createDocumentFragment(),e);break;case!!A:r=x.call(this,document.createElementNS(A,t),e);break;case!r:r=x.call(this,document.createElement(t),e)}return U(r),s||(s=r),n.forEach(a=>a(s)),o&&g.pop(),o=2,r}function nt(t,e=t,n=void 0){let c=Symbol.for("default"),o=Array.from(e.querySelectorAll("slot")).reduce((s,a)=>Reflect.set(s,a.name||c,a)&&s,{}),r=Reflect.has(o,c);if(t.append=new Proxy(t.append,{apply(s,a,d){if(!d.length)return t;let l=document.createDocumentFragment();for(let p of d){if(!p||!p.slot){r&&l.appendChild(p);continue}let i=p.slot,u=o[i];H(p,"remove","slot"),u&&(z(u,p,n),Reflect.deleteProperty(o,i))}return r&&(o[c].replaceWith(l),Reflect.deleteProperty(o,c)),t.append=s,t}}),t!==e){let s=Array.from(t.childNodes);s.forEach(a=>a.remove()),t.append(...s)}return e}function z(t,e,n){n&&n(t,e);try{t.replaceWith(x(e,{className:[e.className,t.className],dataset:{...t.dataset}}))}catch{t.replaceWith(e)}}N.mark=function(t,e=!1){t=Object.entries(t).map(([o,r])=>o+`="${r}"`).join(" ");let n=e?"":"/",c=document.createComment(``);return e||(c.end=document.createComment("")),c};function rt(t){let e=this;return function(...c){A=t;let o=N.call(e,...c);return A=void 0,o}}var{setDeleteAttr:P}=R,C=new WeakMap;function x(t,...e){if(!e.length)return t;C.set(t,T(t,this));for(let[n,c]of Object.entries(Object.assign({},...e)))j.call(this,t,n,c);return C.delete(t),t}function j(t,e,n){let{setRemoveAttr:c,s:o}=T(t,this),r=this;n=o.processReactiveAttribute(t,e,n,(a,d)=>j.call(r,t,a,d));let[s]=e;if(s==="=")return c(e.slice(1),n);if(s===".")return W(t,e.slice(1),n);if(/(aria|data)([A-Z])/.test(e))return e=e.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),c(e,n);switch(e==="className"&&(e="class"),e){case"xlink:href":return c(e,n,"http://www.w3.org/1999/xlink");case"textContent":return P(t,e,n);case"style":if(typeof n!="object")break;case"dataset":return S(o,n,W.bind(null,t[e]));case"ariaset":return S(o,n,(a,d)=>c("aria-"+a,d));case"classList":return B.call(r,t,n)}return I(t,e)?P(t,e,n):c(e,n)}function T(t,e){if(C.has(t))return C.get(t);let c=(t instanceof SVGElement?G:Z).bind(null,t,"Attribute"),o=y(e);return{setRemoveAttr:c,s:o}}function B(t,e){let n=y(this);return S(n,e,(c,o)=>t.classList.toggle(c,o===-1?void 0:!!o)),t}function ot(t){return Array.from(t.children).forEach(e=>e.remove()),t}function H(t,e,n,c){return t instanceof HTMLElement?t[e+"Attribute"](n,c):t[e+"AttributeNS"](null,n,c)}function I(t,e){if(!Reflect.has(t,e))return!1;let n=M(t,e);return!v(n.set)}function M(t,e){if(t=Object.getPrototypeOf(t),!t)return{};let n=Object.getOwnPropertyDescriptor(t,e);return n||M(t,e)}function S(t,e,n){if(!(typeof e!="object"||e===null))return Object.entries(e).forEach(function([o,r]){o&&(r=t.processReactiveAttribute(e,o,r,n),n(o,r))})}function $(t){return Array.isArray(t)?t.filter(Boolean).join(" "):t}function Z(t,e,n,c){return t[(v(c)?"remove":"set")+e](n,$(c))}function G(t,e,n,c,o=null){return t[(v(c)?"remove":"set")+e+"NS"](o,n,$(c))}function W(t,e,n){if(Reflect.set(t,e,n),!!v(n))return Reflect.deleteProperty(t,e)}function ut(t,e,n=t){g.push({scope:t,host:(...o)=>o.length?o.forEach(r=>r(t)):t,custom_element:t});let c=e.call(t,n);return g.pop(),c}function ft(t){for(let n of["connected","disconnected"])k(t.prototype,n+"Callback",function(c,o,r){c.apply(o,r),o.dispatchEvent(new Event("dde:"+n))});let e="attributeChanged";return k(t.prototype,e+"Callback",function(n,c,o){let[r,,s]=o;c.dispatchEvent(new CustomEvent("dde:"+e,{detail:[r,s]})),n.apply(c,o)}),t.prototype.__dde_lifecycleToEvents=!0,t}function k(t,e,n){t[e]=new Proxy(t[e]||(()=>{}),{apply:n})}function dt(t,e,n){return e||(e={}),function(o,...r){n&&(r.unshift(o),o=typeof n=="function"?n():n);let s=r.length?new CustomEvent(t,Object.assign({detail:r[0]},e)):new Event(t,e);return o.dispatchEvent(s)}}function _(t,e,n){return function(o){return o.addEventListener(t,e,n),o}}var O=J(),V=new WeakSet;_.connected=function(t,e){let{custom_element:n}=g.current,c="connected";return typeof e!="object"&&(e={}),e.once=!0,function(r){n&&(r=n);let s="dde:"+c;return r.addEventListener(s,t,e),r.__dde_lifecycleToEvents?r:r.isConnected?(r.dispatchEvent(new Event(s)),r):(w(e.signal,()=>O.offConnected(r,t))&&O.onConnected(r,t),r)}};_.disconnected=function(t,e){let{custom_element:n}=g.current,c="disconnected";return typeof e!="object"&&(e={}),e.once=!0,function(r){n&&(r=n);let s="dde:"+c;return r.addEventListener(s,t,e),r.__dde_lifecycleToEvents||w(e.signal,()=>O.offDisconnected(r,t))&&O.onDisconnected(r,t),r}};var D=new WeakMap;_.disconnectedAsAbort=function(t){if(D.has(t))return D.get(t);let e=new AbortController;return D.set(t,e),t(_.disconnected(()=>e.abort())),e};_.attributeChanged=function(t,e){let n="attributeChanged";return typeof e!="object"&&(e={}),function(o){let r="dde:"+n;if(o.addEventListener(r,t,e),o.__dde_lifecycleToEvents||V.has(o))return o;let s=new MutationObserver(function(d){for(let{attributeName:l,target:p}of d)p.dispatchEvent(new CustomEvent(r,{detail:[l,p.getAttribute(l)]}))});return w(e.signal,()=>s.disconnect())&&s.observe(o,{attributes:!0}),o}};function J(){let t=new Map,e=!1,n=new MutationObserver(function(i){for(let u of i)if(u.type==="childList"){if(l(u.addedNodes,!0)){s();continue}p(u.removedNodes,!0)&&s()}});return{onConnected(i,u){r();let f=o(i);f.connected.has(u)||(f.connected.add(u),f.length_c+=1)},offConnected(i,u){if(!t.has(i))return;let f=t.get(i);f.connected.has(u)&&(f.connected.delete(u),f.length_c-=1,c(i,f))},onDisconnected(i,u){r();let f=o(i);f.disconnected.has(u)||(f.disconnected.add(u),f.length_d+=1)},offDisconnected(i,u){if(!t.has(i))return;let f=t.get(i);f.disconnected.has(u)&&(f.disconnected.delete(u),f.length_d-=1,c(i,f))}};function c(i,u){u.length_c||u.length_d||(t.delete(i),s())}function o(i){if(t.has(i))return t.get(i);let u={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return t.set(i,u),u}function r(){e||(e=!0,n.observe(document.body,{childList:!0,subtree:!0}))}function s(){!e||t.size||(e=!1,n.disconnect())}function a(){return new Promise(function(i){(requestIdleCallback||requestAnimationFrame)(i)})}async function d(i){t.size>30&&await a();let u=[];if(!(i instanceof Node))return u;for(let f of t.keys())f===i||!(f instanceof Node)||i.contains(f)&&u.push(f);return u}function l(i,u){let f=!1;for(let h of i){if(u&&d(h).then(l),!t.has(h))continue;let E=t.get(h);E.length_c&&(h.dispatchEvent(new Event("dde:connected")),E.connected=new WeakSet,E.length_c=0,E.length_d||t.delete(h),f=!0)}return f}function p(i,u){let f=!1;for(let h of i)u&&d(h).then(p),!(!t.has(h)||!t.get(h).length_d)&&(h.dispatchEvent(new Event("dde:disconnected")),t.delete(h),f=!0);return f}} +globalThis.dde= {assign: x, +assignAttribute: j, chainableAppend: U, -classListDeclarative: z, -createElement: C, -createElementNS: et, -dispatchEvent: ct, -el: C, -elNS: et, -elementAttribute: B, -empty: nt, +classListDeclarative: B, +createElement: N, +createElementNS: rt, +customElementRender: ut, +customElementWithDDE: ft, +dispatchEvent: dt, +el: N, +elNS: rt, +elementAttribute: H, +empty: ot, +lifecycleToEvents: ft, on: _, registerReactivity: q, -scope: E, -simulateSlots: tt +scope: g, +simulateSlots: nt }; })(); \ No newline at end of file diff --git a/dist/esm-with-observables.d.ts b/dist/esm-with-observables.d.ts index e4c7aa2..debccbe 100644 --- a/dist/esm-with-observables.d.ts +++ b/dist/esm-with-observables.d.ts @@ -107,18 +107,19 @@ type _fromElsInterfaces= Omit= Partial<_fromElsInterfaces & { [K in keyof _fromElsInterfaces]: Observable<_fromElsInterfaces[K], any> } & AttrsModified>; +type ElementAttributes= Partial<_fromElsInterfaces & { [K in keyof _fromElsInterfaces]: Observable<_fromElsInterfaces[K], any> } & AttrsModified> & Record; export function classListDeclarative(element: El, classList: AttrsModified["classList"]): El export function assign(element: El, ...attrs_array: ElementAttributes[]): El export function assignAttribute>(element: El, attr: ATT, value: ElementAttributes[ATT]): ElementAttributes[ATT] type ExtendedHTMLElementTagNameMap= HTMLElementTagNameMap & CustomElementTagNameMap & ddePublicElementTagNameMap +type textContent= string | ( (set?: string)=> string ); // TODO: for some reason `Observable` leads to `attrs?: any` export function el< TAG extends keyof ExtendedHTMLElementTagNameMap & string, EL extends (TAG extends keyof ExtendedHTMLElementTagNameMap ? ExtendedHTMLElementTagNameMap[TAG] : HTMLElement) >( tag_name: TAG, - attrs?: string | Observable | ElementAttributes, + attrs?: ElementAttributes | textContent, ...addons: ddeElementAddon[] ): TAG extends keyof ddeHTMLElementTagNameMap ? ddeHTMLElementTagNameMap[TAG] : ddeHTMLElement export function el( @@ -126,34 +127,39 @@ export function el( ): ddeDocumentFragment export function el< - A extends ddeComponentAttributes, - C extends (attr: Partial)=> SupportedElement | DocumentFragment + C extends (attr: ddeComponentAttributes)=> SupportedElement | ddeDocumentFragment >( component: C, - attrs?: A | string, + attrs?: Parameters[0] | textContent, ...addons: ddeElementAddon>[] -): ReturnType +): ReturnType extends ddeHTMLElementTagNameMap[keyof ddeHTMLElementTagNameMap] ? ReturnType : ( ReturnType extends ddeDocumentFragment ? ReturnType : ddeHTMLElement ) export { el as createElement } export function elNS( namespace: "http://www.w3.org/2000/svg" -): ( +): < + TAG extends keyof SVGElementTagNameMap & string, + EL extends ( TAG extends keyof SVGElementTagNameMap ? SVGElementTagNameMap[TAG] : SVGElement ), +>( tag_name: TAG, - attrs?: string | Partial<{ [key in KEYS]: EL[key] | string | number | boolean }>, + attrs?: ElementAttributes | textContent, ...addons: ddeElementAddon[] -)=> EL +)=> TAG extends keyof ddeSVGElementTagNameMap ? ddeSVGElementTagNameMap[TAG] : ddeSVGElement export function elNS( namespace: "http://www.w3.org/1998/Math/MathML" -): ( +): < + TAG extends keyof MathMLElementTagNameMap & string, + EL extends ( TAG extends keyof MathMLElementTagNameMap ? MathMLElementTagNameMap[TAG] : MathMLElement ), +>( tag_name: TAG, - attrs?: string | Partial<{ [key in KEYS]: MathMLElementTagNameMap[TAG][key] | string | number | boolean }>, - ...addons: ddeElementAddon[] + attrs?: string | textContent | Partial<{ [key in keyof EL]: EL[key] | Observable | string | number | boolean }>, + ...addons: ddeElementAddon[] )=> ddeMathMLElement export function elNS( namespace: string ): ( tag_name: string, - attrs?: string | Record, + attrs?: string | textContent | Record, ...addons: ddeElementAddon[] )=> SupportedElement export { elNS as createElementNS } diff --git a/dist/esm-with-observables.js b/dist/esm-with-observables.js index 5274bff..6d845a0 100644 --- a/dist/esm-with-observables.js +++ b/dist/esm-with-observables.js @@ -1,4 +1,4 @@ -var A={isObservable(e){return!1},processReactiveAttribute(e,t,n,r){return n}};function T(e,t=!0){return t?Object.assign(A,e):(Object.setPrototypeOf(e,A),e)}function C(e){return A.isPrototypeOf(e)&&e!==A?e:A}function x(e){return typeof e>"u"}function B(e){let t=typeof e;return t!=="object"?t:e===null?"null":Object.prototype.toString.call(e)}function N(e,t){if(!e||!(e instanceof AbortSignal))return!0;if(!e.aborted)return e.addEventListener("abort",t),function(){e.removeEventListener("abort",t)}}var $={setDeleteAttr:ee,ssr:""};function ee(e,t,n){if(Reflect.set(e,t,n),!!x(n)){if(Reflect.deleteProperty(e,t),e instanceof HTMLElement&&e.getAttribute(t)==="undefined")return e.removeAttribute(t);if(Reflect.get(e,t)==="undefined")return Reflect.set(e,t,"")}}var E=[{scope:document.body,host:e=>e?e(document.body):document.body,custom_element:!1,prevent:!0}],b={get current(){return E[E.length-1]},get host(){return this.current.host},preventDefault(){let{current:e}=this;return e.prevent=!0,e},get state(){return[...E]},push(e={}){return E.push(Object.assign({},this.current,{prevent:!1},e))},pushRoot(){return E.push(E[0])},pop(){if(E.length!==1)return E.pop()}};function H(...e){return this.appendOriginal(...e),this}function te(e){return e.append===H||(e.appendOriginal=e.append,e.append=H),e}var j;function y(e,t,...n){let r=C(this),o=0,c,s;switch((Object(t)!==t||r.isObservable(t))&&(t={textContent:t}),!0){case typeof e=="function":{o=1,b.push({scope:e,host:(...l)=>l.length?(o===1?n.unshift(...l):l.forEach(v=>v(s)),void 0):s}),c=e(t||void 0);let d=c instanceof DocumentFragment;if(c.nodeName==="#comment")break;let u=y.mark({type:"component",name:e.name,host:d?"this":"parentElement"});c.prepend(u),d&&(s=u);break}case e==="#text":c=P.call(this,document.createTextNode(""),t);break;case(e==="<>"||!e):c=P.call(this,document.createDocumentFragment(),t);break;case!!j:c=P.call(this,document.createElementNS(j,e),t);break;case!c:c=P.call(this,document.createElement(e),t)}return te(c),s||(s=c),n.forEach(d=>d(s)),o&&b.pop(),o=2,c}function _e(e){let t=Symbol.for("default"),n=Array.from(e.querySelectorAll("slot")).reduce((o,c)=>Reflect.set(o,c.name||t,c)&&o,{}),r=Reflect.has(n,t);return e.append=new Proxy(e.append,{apply(o,c,s){if(!s.length)return e;let d=document.createDocumentFragment();for(let u of s){if(!u||!u.slot){r&&d.appendChild(u);continue}let l=u.slot,v=n[l];O(u,"remove","slot"),v&&(v.replaceWith(u),Reflect.deleteProperty(n,l))}return r&&(n[t].replaceWith(d),Reflect.deleteProperty(n,t)),Object.values(n).forEach(u=>u.replaceWith(y().append(...Array.from(u.childNodes)))),e}}),e}y.mark=function(e,t=!1){e=Object.entries(e).map(([o,c])=>o+`="${c}"`).join(" ");let n=t?"":"/",r=document.createComment(``);return t||(r.end=document.createComment("")),r};function Ee(e){let t=this;return function(...r){j=e;let o=y.call(t,...r);return j=void 0,o}}var{setDeleteAttr:I}=$,L=new WeakMap;function P(e,...t){if(!t.length)return e;L.set(e,G(e,this));for(let[n,r]of Object.entries(Object.assign({},...t)))Z.call(this,e,n,r);return L.delete(e),e}function Z(e,t,n){let{setRemoveAttr:r,s:o}=G(e,this),c=this;n=o.processReactiveAttribute(e,t,n,(d,u)=>Z.call(c,e,d,u));let[s]=t;if(s==="=")return r(t.slice(1),n);if(s===".")return J(e,t.slice(1),n);if(/(aria|data)([A-Z])/.test(t))return t=t.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),r(t,n);switch(t==="className"&&(t="class"),t){case"xlink:href":return r(t,n,"http://www.w3.org/1999/xlink");case"textContent":return I(e,t,n);case"style":if(typeof n!="object")break;case"dataset":return F(o,n,J.bind(null,e[t]));case"ariaset":return F(o,n,(d,u)=>r("aria-"+d,u));case"classList":return ne.call(c,e,n)}return re(e,t)?I(e,t,n):r(t,n)}function G(e,t){if(L.has(e))return L.get(e);let r=(e instanceof SVGElement?ce:oe).bind(null,e,"Attribute"),o=C(t);return{setRemoveAttr:r,s:o}}function ne(e,t){let n=C(this);return F(n,t,(r,o)=>e.classList.toggle(r,o===-1?void 0:!!o)),e}function xe(e){return Array.from(e.children).forEach(t=>t.remove()),e}function O(e,t,n,r){return e instanceof HTMLElement?e[t+"Attribute"](n,r):e[t+"AttributeNS"](null,n,r)}function re(e,t){if(!Reflect.has(e,t))return!1;let n=V(e,t);return!x(n.set)}function V(e,t){if(e=Object.getPrototypeOf(e),!e)return{};let n=Object.getOwnPropertyDescriptor(e,t);return n||V(e,t)}function F(e,t,n){if(!(typeof t!="object"||t===null))return Object.entries(t).forEach(function([o,c]){o&&(c=e.processReactiveAttribute(t,o,c,n),n(o,c))})}function K(e){return Array.isArray(e)?e.filter(Boolean).join(" "):e}function oe(e,t,n,r){return e[(x(r)?"remove":"set")+t](n,K(r))}function ce(e,t,n,r,o=null){return e[(x(r)?"remove":"set")+t+"NS"](o,n,K(r))}function J(e,t,n){if(Reflect.set(e,t,n),!!x(n))return Reflect.deleteProperty(e,t)}function we(e,t,n){return t||(t={}),function(o,...c){n&&(c.unshift(o),o=typeof n=="function"?n():n);let s=c.length?new CustomEvent(e,Object.assign({detail:c[0]},t)):new Event(e,t);return o.dispatchEvent(s)}}function _(e,t,n){return function(o){return o.addEventListener(e,t,n),o}}var D=ie(),se=new WeakSet;_.connected=function(e,t){let{custom_element:n}=b.current,r="connected";return typeof t!="object"&&(t={}),t.once=!0,function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,e,t),c.__dde_lifecycleToEvents?c:c.isConnected?(c.dispatchEvent(new Event(s)),c):(N(t.signal,()=>D.offConnected(c,e))&&D.onConnected(c,e),c)}};_.disconnected=function(e,t){let{custom_element:n}=b.current,r="disconnected";return typeof t!="object"&&(t={}),t.once=!0,function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,e,t),c.__dde_lifecycleToEvents||N(t.signal,()=>D.offDisconnected(c,e))&&D.onDisconnected(c,e),c}};var z=new WeakMap;_.disconnectedAsAbort=function(e){if(z.has(e))return z.get(e);let t=new AbortController;return z.set(e,t),e(_.disconnected(()=>t.abort())),t};_.attributeChanged=function(e,t){let n="attributeChanged";return typeof t!="object"&&(t={}),function(o){let c="dde:"+n;if(o.addEventListener(c,e,t),o.__dde_lifecycleToEvents||se.has(o))return o;let s=new MutationObserver(function(u){for(let{attributeName:l,target:v}of u)v.dispatchEvent(new CustomEvent(c,{detail:[l,v.getAttribute(l)]}))});return N(t.signal,()=>s.disconnect())&&s.observe(o,{attributes:!0}),o}};function ie(){let e=new Map,t=!1,n=new MutationObserver(function(i){for(let f of i)if(f.type==="childList"){if(l(f.addedNodes,!0)){s();continue}v(f.removedNodes,!0)&&s()}});return{onConnected(i,f){c();let a=o(i);a.connected.has(f)||(a.connected.add(f),a.length_c+=1)},offConnected(i,f){if(!e.has(i))return;let a=e.get(i);a.connected.has(f)&&(a.connected.delete(f),a.length_c-=1,r(i,a))},onDisconnected(i,f){c();let a=o(i);a.disconnected.has(f)||(a.disconnected.add(f),a.length_d+=1)},offDisconnected(i,f){if(!e.has(i))return;let a=e.get(i);a.disconnected.has(f)&&(a.disconnected.delete(f),a.length_d-=1,r(i,a))}};function r(i,f){f.length_c||f.length_d||(e.delete(i),s())}function o(i){if(e.has(i))return e.get(i);let f={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return e.set(i,f),f}function c(){t||(t=!0,n.observe(document.body,{childList:!0,subtree:!0}))}function s(){!t||e.size||(t=!1,n.disconnect())}function d(){return new Promise(function(i){(requestIdleCallback||requestAnimationFrame)(i)})}async function u(i){e.size>30&&await d();let f=[];if(!(i instanceof Node))return f;for(let a of e.keys())a===i||!(a instanceof Node)||i.contains(a)&&f.push(a);return f}function l(i,f){let a=!1;for(let g of i){if(f&&u(g).then(l),!e.has(g))continue;let w=e.get(g);w.length_c&&(g.dispatchEvent(new Event("dde:connected")),w.connected=new WeakSet,w.length_c=0,w.length_d||e.delete(g),a=!0)}return a}function v(i,f){let a=!1;for(let g of i)f&&u(g).then(v),!(!e.has(g)||!e.get(g).length_d)&&(g.dispatchEvent(new Event("dde:disconnected")),e.delete(g),a=!0);return a}}var p=Symbol.for("observable");function W(e){try{return Reflect.has(e,p)}catch{return!1}}var M=[],h=new WeakMap;function m(e,t){if(typeof e!="function")return Q(e,t);if(W(e))return e;let n=Q(),r=function(){let[o,...c]=h.get(r);if(h.set(r,new Set([o])),M.push(r),n(e()),M.pop(),!c.length)return;let s=h.get(r);for(let d of c)s.has(d)||S(d,r)};return h.set(n[p],r),h.set(r,new Set([n])),r(),n}m.action=function(e,t,...n){let r=e[p],{actions:o}=r;if(!o||!Reflect.has(o,t))throw new Error(`'${e}' has no action with name '${t}'!`);if(o[t].apply(r,n),r.skip)return Reflect.deleteProperty(r,"skip");r.listeners.forEach(c=>c(r.value))};m.on=function e(t,n,r={}){let{signal:o}=r;if(!(o&&o.aborted)){if(Array.isArray(t))return t.forEach(c=>e(c,n,r));U(t,n),o&&o.addEventListener("abort",()=>S(t,n))}};m.symbols={onclear:Symbol.for("Observable.onclear")};m.clear=function(...e){for(let n of e){Reflect.deleteProperty(n,"toJSON");let r=n[p];r.onclear.forEach(o=>o.call(r)),t(n,r),Reflect.deleteProperty(n,p)}function t(n,r){r.listeners.forEach(o=>{if(r.listeners.delete(o),!h.has(o))return;let c=h.get(o);c.delete(n),!(c.size>1)&&(n.clear(...c),h.delete(o))})}};var k="__dde_reactive";m.el=function(e,t){let n=y.mark({type:"reactive"},!1),r=n.end,o=document.createDocumentFragment();o.append(n,r);let{current:c}=b,s=d=>{if(!n.parentNode||!r.parentNode)return S(e,s);b.push(c);let u=t(d);b.pop(),Array.isArray(u)||(u=[u]);let l=n;for(;(l=n.nextSibling)!==r;)l.remove();n.after(...u)};return U(e,s),Y(e,s,n,t),s(e()),o};var R="__dde_attributes";m.attribute=function(e,t=null){let n=m(t),r;return b.host(o=>{if(r=o,O(r,"has",e)?n(O(r,"get",e)):t!==null&&O(r,"set",e,t),o[R]){o[R][e]=n;return}r[R]={[e]:n},_.attributeChanged(function({detail:s}){/*! This maps attributes to observables (`S.attribute`). -* Investigate `__dde_attributes` key of the element.*/let[d,u]=s,l=r[R][d];if(l)return l(u)})(r),_.disconnected(function(){/*! This removes all observables mapped to attributes (`S.attribute`). -* Investigate `__dde_attributes` key of the element.*/m.clear(...Object.values(r[R]))})(r)}),new Proxy(n,{apply(o,c,s){if(!s.length)return o();let d=s[0];return O(r,"set",e,d)}})};var X={isObservable:W,processReactiveAttribute(e,t,n,r){if(!W(n))return n;let o=c=>r(t,c);return U(n,o),Y(n,o,e,t),n()}};function Y(e,t,...n){let{current:r}=b;r.prevent||r.host(function(o){o[k]||(o[k]=[],_.disconnected(()=>o[k].forEach(([[c,s]])=>S(c,s,c[p]?.host()===o)))(o)),o[k].push([[e,t],...n])})}function Q(e,t){let n=(...r)=>r.length?le(n,...r):de(n);return ue(n,e,t)}var fe=Object.assign(Object.create(null),{stopPropagation(){this.skip=!0}}),q=class extends Error{constructor(){super();let[t,...n]=this.stack.split(` -`),r=t.slice(t.indexOf("@"),t.indexOf(".js:")+4);this.stack=n.find(o=>!o.includes(r))}};function ue(e,t,n){let r=[];B(n)!=="[object Object]"&&(n={});let{onclear:o}=m.symbols;n[o]&&(r.push(n[o]),Reflect.deleteProperty(n,o));let{host:c}=b;return Reflect.defineProperty(e,p,{value:{value:t,actions:n,onclear:r,host:c,listeners:new Set,defined:new q},enumerable:!1,writable:!1,configurable:!0}),e.toJSON=()=>e(),Object.setPrototypeOf(e[p],fe),e}function ae(){return M[M.length-1]}function de(e){if(!e[p])return;let{value:t,listeners:n}=e[p],r=ae();return r&&n.add(r),h.has(r)&&h.get(r).add(e),t}function le(e,t,n){if(!e[p])return;let r=e[p];if(!(!n&&r.value===t))return r.value=t,r.listeners.forEach(o=>o(t)),t}function U(e,t){if(e[p])return e[p].listeners.add(t)}function S(e,t,n){let r=e[p];if(!r)return;let o=r.listeners.delete(t);if(n&&!r.listeners.size){if(e.clear(e),!h.has(r))return o;let c=h.get(r);if(!h.has(c))return o;h.get(c).forEach(s=>S(s,c,!0))}return o}T(X);export{m as O,P as assign,Z as assignAttribute,te as chainableAppend,ne as classListDeclarative,y as createElement,Ee as createElementNS,we as dispatchEvent,y as el,Ee as elNS,O as elementAttribute,xe as empty,W as isObservable,m as observable,_ as on,T as registerReactivity,b as scope,_e as simulateSlots}; +var w={isObservable(e){return!1},processReactiveAttribute(e,t,n,r){return n}};function M(e,t=!0){return t?Object.assign(w,e):(Object.setPrototypeOf(e,w),e)}function N(e){return w.isPrototypeOf(e)&&e!==w?e:w}function x(e){return typeof e>"u"}function B(e){let t=typeof e;return t!=="object"?t:e===null?"null":Object.prototype.toString.call(e)}function P(e,t){if(!e||!(e instanceof AbortSignal))return!0;if(!e.aborted)return e.addEventListener("abort",t),function(){e.removeEventListener("abort",t)}}var $={setDeleteAttr:te,ssr:""};function te(e,t,n){if(Reflect.set(e,t,n),!!x(n)){if(Reflect.deleteProperty(e,t),e instanceof HTMLElement&&e.getAttribute(t)==="undefined")return e.removeAttribute(t);if(Reflect.get(e,t)==="undefined")return Reflect.set(e,t,"")}}var y=[{scope:document.body,host:e=>e?e(document.body):document.body,custom_element:!1,prevent:!0}],p={get current(){return y[y.length-1]},get host(){return this.current.host},preventDefault(){let{current:e}=this;return e.prevent=!0,e},get state(){return[...y]},push(e={}){return y.push(Object.assign({},this.current,{prevent:!1},e))},pushRoot(){return y.push(y[0])},pop(){if(y.length!==1)return y.pop()}};function H(...e){return this.appendOriginal(...e),this}function ne(e){return e.append===H||(e.appendOriginal=e.append,e.append=H),e}var L;function R(e,t,...n){let r=N(this),o=0,c,s;switch((Object(t)!==t||r.isObservable(t))&&(t={textContent:t}),!0){case typeof e=="function":{o=1,p.push({scope:e,host:(...l)=>l.length?(o===1?n.unshift(...l):l.forEach(b=>b(s)),void 0):s}),c=e(t||void 0);let u=c instanceof DocumentFragment;if(c.nodeName==="#comment")break;let d=R.mark({type:"component",name:e.name,host:u?"this":"parentElement"});c.prepend(d),u&&(s=d);break}case e==="#text":c=A.call(this,document.createTextNode(""),t);break;case(e==="<>"||!e):c=A.call(this,document.createDocumentFragment(),t);break;case!!L:c=A.call(this,document.createElementNS(L,e),t);break;case!c:c=A.call(this,document.createElement(e),t)}return ne(c),s||(s=c),n.forEach(u=>u(s)),o&&p.pop(),o=2,c}function xe(e,t=e,n=void 0){let r=Symbol.for("default"),o=Array.from(t.querySelectorAll("slot")).reduce((s,u)=>Reflect.set(s,u.name||r,u)&&s,{}),c=Reflect.has(o,r);if(e.append=new Proxy(e.append,{apply(s,u,d){if(!d.length)return e;let l=document.createDocumentFragment();for(let b of d){if(!b||!b.slot){c&&l.appendChild(b);continue}let i=b.slot,f=o[i];_(b,"remove","slot"),f&&(re(f,b,n),Reflect.deleteProperty(o,i))}return c&&(o[r].replaceWith(l),Reflect.deleteProperty(o,r)),e.append=s,e}}),e!==t){let s=Array.from(e.childNodes);s.forEach(u=>u.remove()),e.append(...s)}return t}function re(e,t,n){n&&n(e,t);try{e.replaceWith(A(t,{className:[t.className,e.className],dataset:{...e.dataset}}))}catch{e.replaceWith(t)}}R.mark=function(e,t=!1){e=Object.entries(e).map(([o,c])=>o+`="${c}"`).join(" ");let n=t?"":"/",r=document.createComment(``);return t||(r.end=document.createComment("")),r};function _e(e){let t=this;return function(...r){L=e;let o=R.call(t,...r);return L=void 0,o}}var{setDeleteAttr:I}=$,j=new WeakMap;function A(e,...t){if(!t.length)return e;j.set(e,G(e,this));for(let[n,r]of Object.entries(Object.assign({},...t)))Z.call(this,e,n,r);return j.delete(e),e}function Z(e,t,n){let{setRemoveAttr:r,s:o}=G(e,this),c=this;n=o.processReactiveAttribute(e,t,n,(u,d)=>Z.call(c,e,u,d));let[s]=t;if(s==="=")return r(t.slice(1),n);if(s===".")return J(e,t.slice(1),n);if(/(aria|data)([A-Z])/.test(t))return t=t.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),r(t,n);switch(t==="className"&&(t="class"),t){case"xlink:href":return r(t,n,"http://www.w3.org/1999/xlink");case"textContent":return I(e,t,n);case"style":if(typeof n!="object")break;case"dataset":return F(o,n,J.bind(null,e[t]));case"ariaset":return F(o,n,(u,d)=>r("aria-"+u,d));case"classList":return oe.call(c,e,n)}return ce(e,t)?I(e,t,n):r(t,n)}function G(e,t){if(j.has(e))return j.get(e);let r=(e instanceof SVGElement?ie:se).bind(null,e,"Attribute"),o=N(t);return{setRemoveAttr:r,s:o}}function oe(e,t){let n=N(this);return F(n,t,(r,o)=>e.classList.toggle(r,o===-1?void 0:!!o)),e}function Oe(e){return Array.from(e.children).forEach(t=>t.remove()),e}function _(e,t,n,r){return e instanceof HTMLElement?e[t+"Attribute"](n,r):e[t+"AttributeNS"](null,n,r)}function ce(e,t){if(!Reflect.has(e,t))return!1;let n=V(e,t);return!x(n.set)}function V(e,t){if(e=Object.getPrototypeOf(e),!e)return{};let n=Object.getOwnPropertyDescriptor(e,t);return n||V(e,t)}function F(e,t,n){if(!(typeof t!="object"||t===null))return Object.entries(t).forEach(function([o,c]){o&&(c=e.processReactiveAttribute(t,o,c,n),n(o,c))})}function K(e){return Array.isArray(e)?e.filter(Boolean).join(" "):e}function se(e,t,n,r){return e[(x(r)?"remove":"set")+t](n,K(r))}function ie(e,t,n,r,o=null){return e[(x(r)?"remove":"set")+t+"NS"](o,n,K(r))}function J(e,t,n){if(Reflect.set(e,t,n),!!x(n))return Reflect.deleteProperty(e,t)}function Se(e,t,n=e){p.push({scope:e,host:(...o)=>o.length?o.forEach(c=>c(e)):e,custom_element:e});let r=t.call(e,n);return p.pop(),r}function Ce(e){for(let n of["connected","disconnected"])Q(e.prototype,n+"Callback",function(r,o,c){r.apply(o,c),o.dispatchEvent(new Event("dde:"+n))});let t="attributeChanged";return Q(e.prototype,t+"Callback",function(n,r,o){let[c,,s]=o;r.dispatchEvent(new CustomEvent("dde:"+t,{detail:[c,s]})),n.apply(r,o)}),e.prototype.__dde_lifecycleToEvents=!0,e}function Q(e,t,n){e[t]=new Proxy(e[t]||(()=>{}),{apply:n})}function Pe(e,t,n){return t||(t={}),function(o,...c){n&&(c.unshift(o),o=typeof n=="function"?n():n);let s=c.length?new CustomEvent(e,Object.assign({detail:c[0]},t)):new Event(e,t);return o.dispatchEvent(s)}}function E(e,t,n){return function(o){return o.addEventListener(e,t,n),o}}var D=ue(),fe=new WeakSet;E.connected=function(e,t){let{custom_element:n}=p.current,r="connected";return typeof t!="object"&&(t={}),t.once=!0,function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,e,t),c.__dde_lifecycleToEvents?c:c.isConnected?(c.dispatchEvent(new Event(s)),c):(P(t.signal,()=>D.offConnected(c,e))&&D.onConnected(c,e),c)}};E.disconnected=function(e,t){let{custom_element:n}=p.current,r="disconnected";return typeof t!="object"&&(t={}),t.once=!0,function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,e,t),c.__dde_lifecycleToEvents||P(t.signal,()=>D.offDisconnected(c,e))&&D.onDisconnected(c,e),c}};var z=new WeakMap;E.disconnectedAsAbort=function(e){if(z.has(e))return z.get(e);let t=new AbortController;return z.set(e,t),e(E.disconnected(()=>t.abort())),t};E.attributeChanged=function(e,t){let n="attributeChanged";return typeof t!="object"&&(t={}),function(o){let c="dde:"+n;if(o.addEventListener(c,e,t),o.__dde_lifecycleToEvents||fe.has(o))return o;let s=new MutationObserver(function(d){for(let{attributeName:l,target:b}of d)b.dispatchEvent(new CustomEvent(c,{detail:[l,b.getAttribute(l)]}))});return P(t.signal,()=>s.disconnect())&&s.observe(o,{attributes:!0}),o}};function ue(){let e=new Map,t=!1,n=new MutationObserver(function(i){for(let f of i)if(f.type==="childList"){if(l(f.addedNodes,!0)){s();continue}b(f.removedNodes,!0)&&s()}});return{onConnected(i,f){c();let a=o(i);a.connected.has(f)||(a.connected.add(f),a.length_c+=1)},offConnected(i,f){if(!e.has(i))return;let a=e.get(i);a.connected.has(f)&&(a.connected.delete(f),a.length_c-=1,r(i,a))},onDisconnected(i,f){c();let a=o(i);a.disconnected.has(f)||(a.disconnected.add(f),a.length_d+=1)},offDisconnected(i,f){if(!e.has(i))return;let a=e.get(i);a.disconnected.has(f)&&(a.disconnected.delete(f),a.length_d-=1,r(i,a))}};function r(i,f){f.length_c||f.length_d||(e.delete(i),s())}function o(i){if(e.has(i))return e.get(i);let f={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return e.set(i,f),f}function c(){t||(t=!0,n.observe(document.body,{childList:!0,subtree:!0}))}function s(){!t||e.size||(t=!1,n.disconnect())}function u(){return new Promise(function(i){(requestIdleCallback||requestAnimationFrame)(i)})}async function d(i){e.size>30&&await u();let f=[];if(!(i instanceof Node))return f;for(let a of e.keys())a===i||!(a instanceof Node)||i.contains(a)&&f.push(a);return f}function l(i,f){let a=!1;for(let v of i){if(f&&d(v).then(l),!e.has(v))continue;let O=e.get(v);O.length_c&&(v.dispatchEvent(new Event("dde:connected")),O.connected=new WeakSet,O.length_c=0,O.length_d||e.delete(v),a=!0)}return a}function b(i,f){let a=!1;for(let v of i)f&&d(v).then(b),!(!e.has(v)||!e.get(v).length_d)&&(v.dispatchEvent(new Event("dde:disconnected")),e.delete(v),a=!0);return a}}var h=Symbol.for("observable");function W(e){try{return Reflect.has(e,h)}catch{return!1}}var T=[],g=new WeakMap;function m(e,t){if(typeof e!="function")return X(e,t);if(W(e))return e;let n=X(),r=function(){let[o,...c]=g.get(r);if(g.set(r,new Set([o])),T.push(r),n(e()),T.pop(),!c.length)return;let s=g.get(r);for(let u of c)s.has(u)||C(u,r)};return g.set(n[h],r),g.set(r,new Set([n])),r(),n}m.action=function(e,t,...n){let r=e[h],{actions:o}=r;if(!o||!Reflect.has(o,t))throw new Error(`'${e}' has no action with name '${t}'!`);if(o[t].apply(r,n),r.skip)return Reflect.deleteProperty(r,"skip");r.listeners.forEach(c=>c(r.value))};m.on=function e(t,n,r={}){let{signal:o}=r;if(!(o&&o.aborted)){if(Array.isArray(t))return t.forEach(c=>e(c,n,r));U(t,n),o&&o.addEventListener("abort",()=>C(t,n))}};m.symbols={onclear:Symbol.for("Observable.onclear")};m.clear=function(...e){for(let n of e){Reflect.deleteProperty(n,"toJSON");let r=n[h];r.onclear.forEach(o=>o.call(r)),t(n,r),Reflect.deleteProperty(n,h)}function t(n,r){r.listeners.forEach(o=>{if(r.listeners.delete(o),!g.has(o))return;let c=g.get(o);c.delete(n),!(c.size>1)&&(n.clear(...c),g.delete(o))})}};var k="__dde_reactive";m.el=function(e,t){let n=R.mark({type:"reactive"},!1),r=n.end,o=document.createDocumentFragment();o.append(n,r);let{current:c}=p,s=u=>{if(!n.parentNode||!r.parentNode)return C(e,s);p.push(c);let d=t(u);p.pop(),Array.isArray(d)||(d=[d]);let l=n;for(;(l=n.nextSibling)!==r;)l.remove();n.after(...d)};return U(e,s),ee(e,s,n,t),s(e()),o};var S="__dde_attributes";m.attribute=function(e,t=null){let n=m(t),r;return p.host(o=>{if(r=o,_(r,"has",e)?n(_(r,"get",e)):t!==null&&_(r,"set",e,t),o[S]){o[S][e]=n;return}r[S]={[e]:n},E.attributeChanged(function({detail:s}){/*! This maps attributes to observables (`S.attribute`). +* Investigate `__dde_attributes` key of the element.*/let[u,d]=s,l=r[S][u];if(l)return l(d)})(r),E.disconnected(function(){/*! This removes all observables mapped to attributes (`S.attribute`). +* Investigate `__dde_attributes` key of the element.*/m.clear(...Object.values(r[S]))})(r)}),new Proxy(n,{apply(o,c,s){if(!s.length)return o();let u=s[0];return _(r,"set",e,u)}})};var Y={isObservable:W,processReactiveAttribute(e,t,n,r){if(!W(n))return n;let o=c=>r(t,c);return U(n,o),ee(n,o,e,t),n()}};function ee(e,t,...n){let{current:r}=p;r.prevent||r.host(function(o){o[k]||(o[k]=[],E.disconnected(()=>o[k].forEach(([[c,s]])=>C(c,s,c[h]?.host()===o)))(o)),o[k].push([[e,t],...n])})}function X(e,t){let n=(...r)=>r.length?he(n,...r):pe(n);return de(n,e,t)}var ae=Object.assign(Object.create(null),{stopPropagation(){this.skip=!0}}),q=class extends Error{constructor(){super();let[t,...n]=this.stack.split(` +`),r=t.slice(t.indexOf("@"),t.indexOf(".js:")+4);this.stack=n.find(o=>!o.includes(r))}};function de(e,t,n){let r=[];B(n)!=="[object Object]"&&(n={});let{onclear:o}=m.symbols;n[o]&&(r.push(n[o]),Reflect.deleteProperty(n,o));let{host:c}=p;return Reflect.defineProperty(e,h,{value:{value:t,actions:n,onclear:r,host:c,listeners:new Set,defined:new q},enumerable:!1,writable:!1,configurable:!0}),e.toJSON=()=>e(),Object.setPrototypeOf(e[h],ae),e}function le(){return T[T.length-1]}function pe(e){if(!e[h])return;let{value:t,listeners:n}=e[h],r=le();return r&&n.add(r),g.has(r)&&g.get(r).add(e),t}function he(e,t,n){if(!e[h])return;let r=e[h];if(!(!n&&r.value===t))return r.value=t,r.listeners.forEach(o=>o(t)),t}function U(e,t){if(e[h])return e[h].listeners.add(t)}function C(e,t,n){let r=e[h];if(!r)return;let o=r.listeners.delete(t);if(n&&!r.listeners.size){if(e.clear(e),!g.has(r))return o;let c=g.get(r);if(!g.has(c))return o;g.get(c).forEach(s=>C(s,c,!0))}return o}M(Y);export{m as O,A as assign,Z as assignAttribute,ne as chainableAppend,oe as classListDeclarative,R as createElement,_e as createElementNS,Se as customElementRender,Ce as customElementWithDDE,Pe as dispatchEvent,R as el,_e as elNS,_ as elementAttribute,Oe as empty,W as isObservable,Ce as lifecycleToEvents,m as observable,E as on,M as registerReactivity,p as scope,xe as simulateSlots}; diff --git a/dist/esm.d.ts b/dist/esm.d.ts index e4c7aa2..debccbe 100644 --- a/dist/esm.d.ts +++ b/dist/esm.d.ts @@ -107,18 +107,19 @@ type _fromElsInterfaces= Omit= Partial<_fromElsInterfaces & { [K in keyof _fromElsInterfaces]: Observable<_fromElsInterfaces[K], any> } & AttrsModified>; +type ElementAttributes= Partial<_fromElsInterfaces & { [K in keyof _fromElsInterfaces]: Observable<_fromElsInterfaces[K], any> } & AttrsModified> & Record; export function classListDeclarative(element: El, classList: AttrsModified["classList"]): El export function assign(element: El, ...attrs_array: ElementAttributes[]): El export function assignAttribute>(element: El, attr: ATT, value: ElementAttributes[ATT]): ElementAttributes[ATT] type ExtendedHTMLElementTagNameMap= HTMLElementTagNameMap & CustomElementTagNameMap & ddePublicElementTagNameMap +type textContent= string | ( (set?: string)=> string ); // TODO: for some reason `Observable` leads to `attrs?: any` export function el< TAG extends keyof ExtendedHTMLElementTagNameMap & string, EL extends (TAG extends keyof ExtendedHTMLElementTagNameMap ? ExtendedHTMLElementTagNameMap[TAG] : HTMLElement) >( tag_name: TAG, - attrs?: string | Observable | ElementAttributes, + attrs?: ElementAttributes | textContent, ...addons: ddeElementAddon[] ): TAG extends keyof ddeHTMLElementTagNameMap ? ddeHTMLElementTagNameMap[TAG] : ddeHTMLElement export function el( @@ -126,34 +127,39 @@ export function el( ): ddeDocumentFragment export function el< - A extends ddeComponentAttributes, - C extends (attr: Partial)=> SupportedElement | DocumentFragment + C extends (attr: ddeComponentAttributes)=> SupportedElement | ddeDocumentFragment >( component: C, - attrs?: A | string, + attrs?: Parameters[0] | textContent, ...addons: ddeElementAddon>[] -): ReturnType +): ReturnType extends ddeHTMLElementTagNameMap[keyof ddeHTMLElementTagNameMap] ? ReturnType : ( ReturnType extends ddeDocumentFragment ? ReturnType : ddeHTMLElement ) export { el as createElement } export function elNS( namespace: "http://www.w3.org/2000/svg" -): ( +): < + TAG extends keyof SVGElementTagNameMap & string, + EL extends ( TAG extends keyof SVGElementTagNameMap ? SVGElementTagNameMap[TAG] : SVGElement ), +>( tag_name: TAG, - attrs?: string | Partial<{ [key in KEYS]: EL[key] | string | number | boolean }>, + attrs?: ElementAttributes | textContent, ...addons: ddeElementAddon[] -)=> EL +)=> TAG extends keyof ddeSVGElementTagNameMap ? ddeSVGElementTagNameMap[TAG] : ddeSVGElement export function elNS( namespace: "http://www.w3.org/1998/Math/MathML" -): ( +): < + TAG extends keyof MathMLElementTagNameMap & string, + EL extends ( TAG extends keyof MathMLElementTagNameMap ? MathMLElementTagNameMap[TAG] : MathMLElement ), +>( tag_name: TAG, - attrs?: string | Partial<{ [key in KEYS]: MathMLElementTagNameMap[TAG][key] | string | number | boolean }>, - ...addons: ddeElementAddon[] + attrs?: string | textContent | Partial<{ [key in keyof EL]: EL[key] | Observable | string | number | boolean }>, + ...addons: ddeElementAddon[] )=> ddeMathMLElement export function elNS( namespace: string ): ( tag_name: string, - attrs?: string | Record, + attrs?: string | textContent | Record, ...addons: ddeElementAddon[] )=> SupportedElement export { elNS as createElementNS } diff --git a/dist/esm.js b/dist/esm.js index f2cd11e..882b11e 100644 --- a/dist/esm.js +++ b/dist/esm.js @@ -1 +1 @@ -var v={isObservable(t){return!1},processReactiveAttribute(t,e,n,c){return n}};function q(t,e=!0){return e?Object.assign(v,t):(Object.setPrototypeOf(t,v),t)}function x(t){return v.isPrototypeOf(t)&&t!==v?t:v}function g(t){return typeof t>"u"}function A(t,e){if(!t||!(t instanceof AbortSignal))return!0;if(!t.aborted)return t.addEventListener("abort",e),function(){t.removeEventListener("abort",e)}}var S={setDeleteAttr:F,ssr:""};function F(t,e,n){if(Reflect.set(t,e,n),!!g(n)){if(Reflect.deleteProperty(t,e),t instanceof HTMLElement&&t.getAttribute(e)==="undefined")return t.removeAttribute(e);if(Reflect.get(t,e)==="undefined")return Reflect.set(t,e,"")}}var b=[{scope:document.body,host:t=>t?t(document.body):document.body,custom_element:!1,prevent:!0}],E={get current(){return b[b.length-1]},get host(){return this.current.host},preventDefault(){let{current:t}=this;return t.prevent=!0,t},get state(){return[...b]},push(t={}){return b.push(Object.assign({},this.current,{prevent:!1},t))},pushRoot(){return b.push(b[0])},pop(){if(b.length!==1)return b.pop()}};function L(...t){return this.appendOriginal(...t),this}function U(t){return t.append===L||(t.appendOriginal=t.append,t.append=L),t}var O;function C(t,e,...n){let c=x(this),o=0,r,f;switch((Object(e)!==e||c.isObservable(e))&&(e={textContent:e}),!0){case typeof t=="function":{o=1,E.push({scope:t,host:(...l)=>l.length?(o===1?n.unshift(...l):l.forEach(h=>h(f)),void 0):f}),r=t(e||void 0);let d=r instanceof DocumentFragment;if(r.nodeName==="#comment")break;let a=C.mark({type:"component",name:t.name,host:d?"this":"parentElement"});r.prepend(a),d&&(f=a);break}case t==="#text":r=w.call(this,document.createTextNode(""),e);break;case(t==="<>"||!t):r=w.call(this,document.createDocumentFragment(),e);break;case!!O:r=w.call(this,document.createElementNS(O,t),e);break;case!r:r=w.call(this,document.createElement(t),e)}return U(r),f||(f=r),n.forEach(d=>d(f)),o&&E.pop(),o=2,r}function tt(t){let e=Symbol.for("default"),n=Array.from(t.querySelectorAll("slot")).reduce((o,r)=>Reflect.set(o,r.name||e,r)&&o,{}),c=Reflect.has(n,e);return t.append=new Proxy(t.append,{apply(o,r,f){if(!f.length)return t;let d=document.createDocumentFragment();for(let a of f){if(!a||!a.slot){c&&d.appendChild(a);continue}let l=a.slot,h=n[l];B(a,"remove","slot"),h&&(h.replaceWith(a),Reflect.deleteProperty(n,l))}return c&&(n[e].replaceWith(d),Reflect.deleteProperty(n,e)),Object.values(n).forEach(a=>a.replaceWith(C().append(...Array.from(a.childNodes)))),t}}),t}C.mark=function(t,e=!1){t=Object.entries(t).map(([o,r])=>o+`="${r}"`).join(" ");let n=e?"":"/",c=document.createComment(``);return e||(c.end=document.createComment("")),c};function et(t){let e=this;return function(...c){O=t;let o=C.call(e,...c);return O=void 0,o}}var{setDeleteAttr:j}=S,y=new WeakMap;function w(t,...e){if(!e.length)return t;y.set(t,M(t,this));for(let[n,c]of Object.entries(Object.assign({},...e)))W.call(this,t,n,c);return y.delete(t),t}function W(t,e,n){let{setRemoveAttr:c,s:o}=M(t,this),r=this;n=o.processReactiveAttribute(t,e,n,(d,a)=>W.call(r,t,d,a));let[f]=e;if(f==="=")return c(e.slice(1),n);if(f===".")return P(t,e.slice(1),n);if(/(aria|data)([A-Z])/.test(e))return e=e.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),c(e,n);switch(e==="className"&&(e="class"),e){case"xlink:href":return c(e,n,"http://www.w3.org/1999/xlink");case"textContent":return j(t,e,n);case"style":if(typeof n!="object")break;case"dataset":return N(o,n,P.bind(null,t[e]));case"ariaset":return N(o,n,(d,a)=>c("aria-"+d,a));case"classList":return z.call(r,t,n)}return k(t,e)?j(t,e,n):c(e,n)}function M(t,e){if(y.has(t))return y.get(t);let c=(t instanceof SVGElement?I:H).bind(null,t,"Attribute"),o=x(e);return{setRemoveAttr:c,s:o}}function z(t,e){let n=x(this);return N(n,e,(c,o)=>t.classList.toggle(c,o===-1?void 0:!!o)),t}function nt(t){return Array.from(t.children).forEach(e=>e.remove()),t}function B(t,e,n,c){return t instanceof HTMLElement?t[e+"Attribute"](n,c):t[e+"AttributeNS"](null,n,c)}function k(t,e){if(!Reflect.has(t,e))return!1;let n=T(t,e);return!g(n.set)}function T(t,e){if(t=Object.getPrototypeOf(t),!t)return{};let n=Object.getOwnPropertyDescriptor(t,e);return n||T(t,e)}function N(t,e,n){if(!(typeof e!="object"||e===null))return Object.entries(e).forEach(function([o,r]){o&&(r=t.processReactiveAttribute(e,o,r,n),n(o,r))})}function $(t){return Array.isArray(t)?t.filter(Boolean).join(" "):t}function H(t,e,n,c){return t[(g(c)?"remove":"set")+e](n,$(c))}function I(t,e,n,c,o=null){return t[(g(c)?"remove":"set")+e+"NS"](o,n,$(c))}function P(t,e,n){if(Reflect.set(t,e,n),!!g(n))return Reflect.deleteProperty(t,e)}function ct(t,e,n){return e||(e={}),function(o,...r){n&&(r.unshift(o),o=typeof n=="function"?n():n);let f=r.length?new CustomEvent(t,Object.assign({detail:r[0]},e)):new Event(t,e);return o.dispatchEvent(f)}}function _(t,e,n){return function(o){return o.addEventListener(t,e,n),o}}var R=G(),Z=new WeakSet;_.connected=function(t,e){let{custom_element:n}=E.current,c="connected";return typeof e!="object"&&(e={}),e.once=!0,function(r){n&&(r=n);let f="dde:"+c;return r.addEventListener(f,t,e),r.__dde_lifecycleToEvents?r:r.isConnected?(r.dispatchEvent(new Event(f)),r):(A(e.signal,()=>R.offConnected(r,t))&&R.onConnected(r,t),r)}};_.disconnected=function(t,e){let{custom_element:n}=E.current,c="disconnected";return typeof e!="object"&&(e={}),e.once=!0,function(r){n&&(r=n);let f="dde:"+c;return r.addEventListener(f,t,e),r.__dde_lifecycleToEvents||A(e.signal,()=>R.offDisconnected(r,t))&&R.onDisconnected(r,t),r}};var D=new WeakMap;_.disconnectedAsAbort=function(t){if(D.has(t))return D.get(t);let e=new AbortController;return D.set(t,e),t(_.disconnected(()=>e.abort())),e};_.attributeChanged=function(t,e){let n="attributeChanged";return typeof e!="object"&&(e={}),function(o){let r="dde:"+n;if(o.addEventListener(r,t,e),o.__dde_lifecycleToEvents||Z.has(o))return o;let f=new MutationObserver(function(a){for(let{attributeName:l,target:h}of a)h.dispatchEvent(new CustomEvent(r,{detail:[l,h.getAttribute(l)]}))});return A(e.signal,()=>f.disconnect())&&f.observe(o,{attributes:!0}),o}};function G(){let t=new Map,e=!1,n=new MutationObserver(function(s){for(let i of s)if(i.type==="childList"){if(l(i.addedNodes,!0)){f();continue}h(i.removedNodes,!0)&&f()}});return{onConnected(s,i){r();let u=o(s);u.connected.has(i)||(u.connected.add(i),u.length_c+=1)},offConnected(s,i){if(!t.has(s))return;let u=t.get(s);u.connected.has(i)&&(u.connected.delete(i),u.length_c-=1,c(s,u))},onDisconnected(s,i){r();let u=o(s);u.disconnected.has(i)||(u.disconnected.add(i),u.length_d+=1)},offDisconnected(s,i){if(!t.has(s))return;let u=t.get(s);u.disconnected.has(i)&&(u.disconnected.delete(i),u.length_d-=1,c(s,u))}};function c(s,i){i.length_c||i.length_d||(t.delete(s),f())}function o(s){if(t.has(s))return t.get(s);let i={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return t.set(s,i),i}function r(){e||(e=!0,n.observe(document.body,{childList:!0,subtree:!0}))}function f(){!e||t.size||(e=!1,n.disconnect())}function d(){return new Promise(function(s){(requestIdleCallback||requestAnimationFrame)(s)})}async function a(s){t.size>30&&await d();let i=[];if(!(s instanceof Node))return i;for(let u of t.keys())u===s||!(u instanceof Node)||s.contains(u)&&i.push(u);return i}function l(s,i){let u=!1;for(let p of s){if(i&&a(p).then(l),!t.has(p))continue;let m=t.get(p);m.length_c&&(p.dispatchEvent(new Event("dde:connected")),m.connected=new WeakSet,m.length_c=0,m.length_d||t.delete(p),u=!0)}return u}function h(s,i){let u=!1;for(let p of s)i&&a(p).then(h),!(!t.has(p)||!t.get(p).length_d)&&(p.dispatchEvent(new Event("dde:disconnected")),t.delete(p),u=!0);return u}}export{w as assign,W as assignAttribute,U as chainableAppend,z as classListDeclarative,C as createElement,et as createElementNS,ct as dispatchEvent,C as el,et as elNS,B as elementAttribute,nt as empty,_ as on,q as registerReactivity,E as scope,tt as simulateSlots}; +var m={isObservable(t){return!1},processReactiveAttribute(t,e,n,c){return n}};function q(t,e=!0){return e?Object.assign(m,t):(Object.setPrototypeOf(t,m),t)}function y(t){return m.isPrototypeOf(t)&&t!==m?t:m}function v(t){return typeof t>"u"}function w(t,e){if(!t||!(t instanceof AbortSignal))return!0;if(!t.aborted)return t.addEventListener("abort",e),function(){t.removeEventListener("abort",e)}}var R={setDeleteAttr:F,ssr:""};function F(t,e,n){if(Reflect.set(t,e,n),!!v(n)){if(Reflect.deleteProperty(t,e),t instanceof HTMLElement&&t.getAttribute(e)==="undefined")return t.removeAttribute(e);if(Reflect.get(t,e)==="undefined")return Reflect.set(t,e,"")}}var b=[{scope:document.body,host:t=>t?t(document.body):document.body,custom_element:!1,prevent:!0}],g={get current(){return b[b.length-1]},get host(){return this.current.host},preventDefault(){let{current:t}=this;return t.prevent=!0,t},get state(){return[...b]},push(t={}){return b.push(Object.assign({},this.current,{prevent:!1},t))},pushRoot(){return b.push(b[0])},pop(){if(b.length!==1)return b.pop()}};function L(...t){return this.appendOriginal(...t),this}function U(t){return t.append===L||(t.appendOriginal=t.append,t.append=L),t}var A;function N(t,e,...n){let c=y(this),o=0,r,s;switch((Object(e)!==e||c.isObservable(e))&&(e={textContent:e}),!0){case typeof t=="function":{o=1,g.push({scope:t,host:(...l)=>l.length?(o===1?n.unshift(...l):l.forEach(p=>p(s)),void 0):s}),r=t(e||void 0);let a=r instanceof DocumentFragment;if(r.nodeName==="#comment")break;let d=N.mark({type:"component",name:t.name,host:a?"this":"parentElement"});r.prepend(d),a&&(s=d);break}case t==="#text":r=x.call(this,document.createTextNode(""),e);break;case(t==="<>"||!t):r=x.call(this,document.createDocumentFragment(),e);break;case!!A:r=x.call(this,document.createElementNS(A,t),e);break;case!r:r=x.call(this,document.createElement(t),e)}return U(r),s||(s=r),n.forEach(a=>a(s)),o&&g.pop(),o=2,r}function nt(t,e=t,n=void 0){let c=Symbol.for("default"),o=Array.from(e.querySelectorAll("slot")).reduce((s,a)=>Reflect.set(s,a.name||c,a)&&s,{}),r=Reflect.has(o,c);if(t.append=new Proxy(t.append,{apply(s,a,d){if(!d.length)return t;let l=document.createDocumentFragment();for(let p of d){if(!p||!p.slot){r&&l.appendChild(p);continue}let i=p.slot,u=o[i];H(p,"remove","slot"),u&&(z(u,p,n),Reflect.deleteProperty(o,i))}return r&&(o[c].replaceWith(l),Reflect.deleteProperty(o,c)),t.append=s,t}}),t!==e){let s=Array.from(t.childNodes);s.forEach(a=>a.remove()),t.append(...s)}return e}function z(t,e,n){n&&n(t,e);try{t.replaceWith(x(e,{className:[e.className,t.className],dataset:{...t.dataset}}))}catch{t.replaceWith(e)}}N.mark=function(t,e=!1){t=Object.entries(t).map(([o,r])=>o+`="${r}"`).join(" ");let n=e?"":"/",c=document.createComment(``);return e||(c.end=document.createComment("")),c};function rt(t){let e=this;return function(...c){A=t;let o=N.call(e,...c);return A=void 0,o}}var{setDeleteAttr:P}=R,C=new WeakMap;function x(t,...e){if(!e.length)return t;C.set(t,T(t,this));for(let[n,c]of Object.entries(Object.assign({},...e)))j.call(this,t,n,c);return C.delete(t),t}function j(t,e,n){let{setRemoveAttr:c,s:o}=T(t,this),r=this;n=o.processReactiveAttribute(t,e,n,(a,d)=>j.call(r,t,a,d));let[s]=e;if(s==="=")return c(e.slice(1),n);if(s===".")return W(t,e.slice(1),n);if(/(aria|data)([A-Z])/.test(e))return e=e.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),c(e,n);switch(e==="className"&&(e="class"),e){case"xlink:href":return c(e,n,"http://www.w3.org/1999/xlink");case"textContent":return P(t,e,n);case"style":if(typeof n!="object")break;case"dataset":return S(o,n,W.bind(null,t[e]));case"ariaset":return S(o,n,(a,d)=>c("aria-"+a,d));case"classList":return B.call(r,t,n)}return I(t,e)?P(t,e,n):c(e,n)}function T(t,e){if(C.has(t))return C.get(t);let c=(t instanceof SVGElement?G:Z).bind(null,t,"Attribute"),o=y(e);return{setRemoveAttr:c,s:o}}function B(t,e){let n=y(this);return S(n,e,(c,o)=>t.classList.toggle(c,o===-1?void 0:!!o)),t}function ot(t){return Array.from(t.children).forEach(e=>e.remove()),t}function H(t,e,n,c){return t instanceof HTMLElement?t[e+"Attribute"](n,c):t[e+"AttributeNS"](null,n,c)}function I(t,e){if(!Reflect.has(t,e))return!1;let n=M(t,e);return!v(n.set)}function M(t,e){if(t=Object.getPrototypeOf(t),!t)return{};let n=Object.getOwnPropertyDescriptor(t,e);return n||M(t,e)}function S(t,e,n){if(!(typeof e!="object"||e===null))return Object.entries(e).forEach(function([o,r]){o&&(r=t.processReactiveAttribute(e,o,r,n),n(o,r))})}function $(t){return Array.isArray(t)?t.filter(Boolean).join(" "):t}function Z(t,e,n,c){return t[(v(c)?"remove":"set")+e](n,$(c))}function G(t,e,n,c,o=null){return t[(v(c)?"remove":"set")+e+"NS"](o,n,$(c))}function W(t,e,n){if(Reflect.set(t,e,n),!!v(n))return Reflect.deleteProperty(t,e)}function ut(t,e,n=t){g.push({scope:t,host:(...o)=>o.length?o.forEach(r=>r(t)):t,custom_element:t});let c=e.call(t,n);return g.pop(),c}function ft(t){for(let n of["connected","disconnected"])k(t.prototype,n+"Callback",function(c,o,r){c.apply(o,r),o.dispatchEvent(new Event("dde:"+n))});let e="attributeChanged";return k(t.prototype,e+"Callback",function(n,c,o){let[r,,s]=o;c.dispatchEvent(new CustomEvent("dde:"+e,{detail:[r,s]})),n.apply(c,o)}),t.prototype.__dde_lifecycleToEvents=!0,t}function k(t,e,n){t[e]=new Proxy(t[e]||(()=>{}),{apply:n})}function dt(t,e,n){return e||(e={}),function(o,...r){n&&(r.unshift(o),o=typeof n=="function"?n():n);let s=r.length?new CustomEvent(t,Object.assign({detail:r[0]},e)):new Event(t,e);return o.dispatchEvent(s)}}function _(t,e,n){return function(o){return o.addEventListener(t,e,n),o}}var O=J(),V=new WeakSet;_.connected=function(t,e){let{custom_element:n}=g.current,c="connected";return typeof e!="object"&&(e={}),e.once=!0,function(r){n&&(r=n);let s="dde:"+c;return r.addEventListener(s,t,e),r.__dde_lifecycleToEvents?r:r.isConnected?(r.dispatchEvent(new Event(s)),r):(w(e.signal,()=>O.offConnected(r,t))&&O.onConnected(r,t),r)}};_.disconnected=function(t,e){let{custom_element:n}=g.current,c="disconnected";return typeof e!="object"&&(e={}),e.once=!0,function(r){n&&(r=n);let s="dde:"+c;return r.addEventListener(s,t,e),r.__dde_lifecycleToEvents||w(e.signal,()=>O.offDisconnected(r,t))&&O.onDisconnected(r,t),r}};var D=new WeakMap;_.disconnectedAsAbort=function(t){if(D.has(t))return D.get(t);let e=new AbortController;return D.set(t,e),t(_.disconnected(()=>e.abort())),e};_.attributeChanged=function(t,e){let n="attributeChanged";return typeof e!="object"&&(e={}),function(o){let r="dde:"+n;if(o.addEventListener(r,t,e),o.__dde_lifecycleToEvents||V.has(o))return o;let s=new MutationObserver(function(d){for(let{attributeName:l,target:p}of d)p.dispatchEvent(new CustomEvent(r,{detail:[l,p.getAttribute(l)]}))});return w(e.signal,()=>s.disconnect())&&s.observe(o,{attributes:!0}),o}};function J(){let t=new Map,e=!1,n=new MutationObserver(function(i){for(let u of i)if(u.type==="childList"){if(l(u.addedNodes,!0)){s();continue}p(u.removedNodes,!0)&&s()}});return{onConnected(i,u){r();let f=o(i);f.connected.has(u)||(f.connected.add(u),f.length_c+=1)},offConnected(i,u){if(!t.has(i))return;let f=t.get(i);f.connected.has(u)&&(f.connected.delete(u),f.length_c-=1,c(i,f))},onDisconnected(i,u){r();let f=o(i);f.disconnected.has(u)||(f.disconnected.add(u),f.length_d+=1)},offDisconnected(i,u){if(!t.has(i))return;let f=t.get(i);f.disconnected.has(u)&&(f.disconnected.delete(u),f.length_d-=1,c(i,f))}};function c(i,u){u.length_c||u.length_d||(t.delete(i),s())}function o(i){if(t.has(i))return t.get(i);let u={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return t.set(i,u),u}function r(){e||(e=!0,n.observe(document.body,{childList:!0,subtree:!0}))}function s(){!e||t.size||(e=!1,n.disconnect())}function a(){return new Promise(function(i){(requestIdleCallback||requestAnimationFrame)(i)})}async function d(i){t.size>30&&await a();let u=[];if(!(i instanceof Node))return u;for(let f of t.keys())f===i||!(f instanceof Node)||i.contains(f)&&u.push(f);return u}function l(i,u){let f=!1;for(let h of i){if(u&&d(h).then(l),!t.has(h))continue;let E=t.get(h);E.length_c&&(h.dispatchEvent(new Event("dde:connected")),E.connected=new WeakSet,E.length_c=0,E.length_d||t.delete(h),f=!0)}return f}function p(i,u){let f=!1;for(let h of i)u&&d(h).then(p),!(!t.has(h)||!t.get(h).length_d)&&(h.dispatchEvent(new Event("dde:disconnected")),t.delete(h),f=!0);return f}}export{x as assign,j as assignAttribute,U as chainableAppend,B as classListDeclarative,N as createElement,rt as createElementNS,ut as customElementRender,ft as customElementWithDDE,dt as dispatchEvent,N as el,rt as elNS,H as elementAttribute,ot as empty,ft as lifecycleToEvents,_ as on,q as registerReactivity,g as scope,nt as simulateSlots}; diff --git a/examples/components/fullNameComponent.js b/examples/components/fullNameComponent.js index ecbcfc2..479b65a 100644 --- a/examples/components/fullNameComponent.js +++ b/examples/components/fullNameComponent.js @@ -31,7 +31,7 @@ export function fullNameComponent(){ ), elSVG("svg", { viewBox: "0 0 240 80", style: { height: "80px", display: "block" } }).append( //elSVG("style", { }) - elSVG("text", { x: 20, y: 35, textContent: "Text" }) + elSVG("text", { x: 20, y: 35, textContent: "Text" }), ) ); } diff --git a/examples/components/webComponent.js b/examples/components/webComponent.js index 36a0163..00d2b8f 100644 --- a/examples/components/webComponent.js +++ b/examples/components/webComponent.js @@ -1,4 +1,4 @@ -import { el, on, scope } from "../../index.js"; +import { el, on, customElementRender, customElementWithDDE, scope, simulateSlots } from "../../index.js"; import { O } from "../../observables.js"; /** @@ -42,35 +42,24 @@ export class CustomHTMLTestElement extends HTMLElement{ get preName(){ return this.getAttribute("pre-name"); } set preName(value){ this.setAttribute("pre-name", value); } } -// https://gist.github.com/WebReflection/ec9f6687842aa385477c4afca625bbf4 -lifecycleToEvents(CustomHTMLTestElement) +customElementWithDDE(CustomHTMLTestElement); customElements.define(CustomHTMLTestElement.tagName, CustomHTMLTestElement); -function customElementRender(_this, render, props= _this){ - console.log(_this.shadowRoot, _this.childList); - scope.push({ scope: _this, host: (...c)=> c.length ? c.forEach(c=> c(_this)) : _this, custom_element: _this }); - if(typeof props==="function") props= props(_this); - const out= render(props); - scope.pop(); - return out; -} -function lifecycleToEvents(class_declaration){ - for (const name of [ "connected", "disconnected" ]) - wrapMethod(class_declaration.prototype, name+"Callback", function(target, thisArg, detail){ - target.apply(thisArg, detail); - thisArg.dispatchEvent(new Event("dde:"+name)); - }); - const name= "attributeChanged"; - wrapMethod(class_declaration.prototype, name+"Callback", function(target, thisArg, detail){ - const [ attribute, , value ]= detail; - thisArg.dispatchEvent(new CustomEvent("dde:"+name, { - detail: [ attribute, value ] - })); - target.apply(thisArg, detail); - }); - class_declaration.prototype.__dde_lifecycleToEvents= true; - return class_declaration; -} -function wrapMethod(obj, method, apply){ - obj[method]= new Proxy(obj[method] || (()=> {}), { apply }); +export class CustomSlottingHTMLElement extends HTMLElement{ + static tagName= "custom-slotting"; + render(){ + return simulateSlots(this, el().append( + el("p").append( + "Ahoj ", el("slot", { name: "name", className: "name", textContent: "World" }) + ), + el("p").append( + "BTW ", el("slot") + ) + )); + } + connectedCallback(){ + this.append(customElementRender(this, this.render)); + } } +customElementWithDDE(CustomSlottingHTMLElement); +customElements.define(CustomSlottingHTMLElement.tagName, CustomSlottingHTMLElement); diff --git a/examples/index.js b/examples/index.js index d80905c..1d0d24a 100644 --- a/examples/index.js +++ b/examples/index.js @@ -2,7 +2,7 @@ import { style, el } from './exports.js'; document.head.append(style.element); import { fullNameComponent } from './components/fullNameComponent.js'; import { todosComponent } from './components/todosComponent.js'; -import { CustomHTMLTestElement } from "./components/webComponent.js"; +import { CustomHTMLTestElement, CustomSlottingHTMLElement } from "./components/webComponent.js"; import { thirdParty } from "./components/3rd-party.js"; document.body.append( @@ -10,5 +10,9 @@ document.body.append( el(fullNameComponent), el(todosComponent), el(CustomHTMLTestElement.tagName, { name: "attr" }), - el(thirdParty) + el(thirdParty), + el(CustomSlottingHTMLElement.tagName).append( + el("strong", { slot: "name", textContent: "Honzo" }), + el("span", "…default slot") + ) ); diff --git a/index.d.ts b/index.d.ts index b152d18..a106eff 100644 --- a/index.d.ts +++ b/index.d.ts @@ -68,7 +68,7 @@ export function el< component: C, attrs?: Parameters[0] | textContent, ...addons: ddeElementAddon>[] -): ReturnType extends ddeHTMLElementTagNameMap[keyof ddeHTMLElementTagNameMap] ? ReturnType : ddeHTMLElement +): ReturnType extends ddeHTMLElementTagNameMap[keyof ddeHTMLElementTagNameMap] ? ReturnType : ( ReturnType extends ddeDocumentFragment ? ReturnType : ddeHTMLElement ) export { el as createElement } export function elNS( diff --git a/index.js b/index.js index 0ba4366..74ce727 100644 --- a/index.js +++ b/index.js @@ -1,2 +1,3 @@ export * from "./src/dom.js"; +export * from "./src/customElement.js"; export * from "./src/events.js"; diff --git a/package.json b/package.json index eb8f32a..0f285ee 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "deka-dom-el", - "version": "0.7.3", + "version": "0.7.5", "description": "A low-code library that simplifies the creation of native DOM elements/components using small wrappers and tweaks.", "author": "Jan Andrle ", "license": "MIT", @@ -58,7 +58,7 @@ "size-limit": [ { "path": "./index.js", - "limit": "9 kB", + "limit": "9.75 kB", "gzip": false, "brotli": false @@ -71,19 +71,7 @@ }, { - "path": "./jsdom.js", - "limit": "10 kB", - "gzip": false, - "brotli": false - }, - { - "path": "./examples/components/webComponent.js", - "limit": "13 kB", - "gzip": false, - "brotli": false - }, - { - "path": "./examples/components/webComponent.js", + "path": "./index-with-observables.js", "limit": "5 kB" } ], diff --git a/src/customElement.js b/src/customElement.js new file mode 100644 index 0000000..e0c117b --- /dev/null +++ b/src/customElement.js @@ -0,0 +1,33 @@ +import { scope } from "./dom.js"; +export function customElementRender(custom_element, render, props= custom_element){ + scope.push({ + scope: custom_element, + host: (...c)=> c.length ? c.forEach(c=> c(custom_element)) : custom_element, + custom_element + }); + const out= render.call(custom_element, props); + scope.pop(); + return out; +} +export function lifecycleToEvents(class_declaration){ + for (const name of [ "connected", "disconnected" ]) + wrapMethod(class_declaration.prototype, name+"Callback", function(target, thisArg, detail){ + target.apply(thisArg, detail); + thisArg.dispatchEvent(new Event("dde:"+name)); + }); + const name= "attributeChanged"; + wrapMethod(class_declaration.prototype, name+"Callback", function(target, thisArg, detail){ + const [ attribute, , value ]= detail; + thisArg.dispatchEvent(new CustomEvent("dde:"+name, { + detail: [ attribute, value ] + })); + target.apply(thisArg, detail); + }); + class_declaration.prototype.__dde_lifecycleToEvents= true; + return class_declaration; +} +// https://gist.github.com/WebReflection/ec9f6687842aa385477c4afca625bbf4 +export { lifecycleToEvents as customElementWithDDE }; +function wrapMethod(obj, method, apply){ + obj[method]= new Proxy(obj[method] || (()=> {}), { apply }); +} diff --git a/src/dom.js b/src/dom.js index 2048dac..9682d7b 100644 --- a/src/dom.js +++ b/src/dom.js @@ -66,14 +66,14 @@ export function createElement(tag, attributes, ...addons){ scoped= 2; return el; } -/** @param {HTMLElement} element */ -export function simulateSlots(element){ +/** @param {HTMLElement} element @param {HTMLElement} [root] */ +export function simulateSlots(element, root= element, mapper= undefined){ const _default= Symbol.for("default"); - const slots= Array.from(element.querySelectorAll("slot")) + const slots= Array.from(root.querySelectorAll("slot")) .reduce((out, curr)=> Reflect.set(out, curr.name || _default, curr) && out, {}); const has_d= Reflect.has(slots, _default); element.append= new Proxy(element.append, { - apply(_1, _2, els){ + apply(orig, _, els){ if(!els.length) return element; const d= document.createDocumentFragment(); @@ -83,19 +83,28 @@ export function simulateSlots(element){ const slot= slots[name]; elementAttribute(el, "remove", "slot"); if(!slot) continue; - slot.replaceWith(el); + simulateSlotReplace(slot, el, mapper); Reflect.deleteProperty(slots, name); } if(has_d){ slots[_default].replaceWith(d); Reflect.deleteProperty(slots, _default); } - Object.values(slots) - .forEach(slot=> slot.replaceWith(createElement().append(...Array.from(slot.childNodes)))); + element.append= orig; //TODO: better memory management, but non-native behavior! return element; } }); - return element; + if(element!==root){ + const els= Array.from(element.childNodes); + els.forEach(el=> el.remove()); + element.append(...els); + } + return root; +} +function simulateSlotReplace(slot, element, mapper){ + if(mapper) mapper(slot, element); + try{ slot.replaceWith(assign(element, { className: [ element.className, slot.className ], dataset: { ...slot.dataset } })); } + catch(_){ slot.replaceWith(element); } } /** * @param { { type: "component", name: string, host: "this" | "parentElement" } | { type: "reactive" | "later" } } attrs