From 51ccd11f82a104774c8eef44dddcadd75b91406c Mon Sep 17 00:00:00 2001 From: Jan Andrle Date: Sun, 14 Jan 2024 13:48:18 +0100 Subject: [PATCH] :rocket: implementing #16 --- dist/dde-with-observables.js | 46 +++++++++++++++++----------------- dist/dde.js | 40 ++++++++++++++--------------- dist/esm-with-observables.d.ts | 26 ++++++++++++------- dist/esm-with-observables.js | 8 +++--- dist/esm.d.ts | 26 ++++++++++++------- dist/esm.js | 2 +- index.d.ts | 12 +++------ package.json | 12 ++++++--- src/customElement.js | 26 ++++++++++++------- src/dom-common.js | 2 ++ src/events.js | 40 ++++++++++++++++++----------- src/observables-lib.js | 6 ++++- 12 files changed, 145 insertions(+), 101 deletions(-) diff --git a/dist/dde-with-observables.js b/dist/dde-with-observables.js index 2c52026..e7970a1 100644 --- a/dist/dde-with-observables.js +++ b/dist/dde-with-observables.js @@ -1,31 +1,31 @@ //deka-dom-el library is available via global namespace `dde` (()=> { -var O={isObservable(t){return!1},processReactiveAttribute(t,e,n,r){return n}};function F(t,e=!0){return e?Object.assign(O,t):(Object.setPrototypeOf(t,O),t)}function S(t){return O.isPrototypeOf(t)&&t!==O?t:O}function _(t){return typeof t>"u"}function B(t){let e=typeof t;return e!=="object"?e:t===null?"null":Object.prototype.toString.call(t)}function D(t,e){if(!t||!(t instanceof AbortSignal))return!0;if(!t.aborted)return t.addEventListener("abort",e),function(){t.removeEventListener("abort",e)}}function N(t,e){let{observedAttributes:n=[]}=t.constructor;return n.reduce(function(r,o){return Reflect.set(r,ct(o),e(t,o)),r},{})}function ct(t){return t.replace(/-./g,e=>e[1].toUpperCase())}var d={setDeleteAttr:st,ssr:"",D:globalThis.document,F:globalThis.DocumentFragment,H:globalThis.HTMLElement,S:globalThis.SVGElement,M:globalThis.MutationObserver};function st(t,e,n){if(Reflect.set(t,e,n),!!_(n)){if(Reflect.deleteProperty(t,e),t instanceof d.H&&t.getAttribute(e)==="undefined")return t.removeAttribute(e);if(Reflect.get(t,e)==="undefined")return Reflect.set(t,e,"")}}var y=[{get scope(){return d.D.body},host:t=>t?t(d.D.body):d.D.body,custom_element:!1,prevent:!0}],v={get current(){return y[y.length-1]},get host(){return this.current.host},preventDefault(){let{current:t}=this;return t.prevent=!0,t},get state(){return[...y]},push(t={}){return y.push(Object.assign({},this.current,{prevent:!1},t))},pushRoot(){return y.push(y[0])},pop(){if(y.length!==1)return y.pop()}};function I(...t){return this.appendOriginal(...t),this}function it(t){return t.append===I||(t.appendOriginal=t.append,t.append=I),t}var P;function R(t,e,...n){let r=S(this),o=0,c,s;switch((Object(e)!==e||r.isObservable(e))&&(e={textContent:e}),!0){case typeof t=="function":{o=1,v.push({scope:t,host:(...h)=>h.length?(o===1?n.unshift(...h):h.forEach(g=>g(s)),void 0):s}),c=t(e||void 0);let u=c instanceof d.F;if(c.nodeName==="#comment")break;let l=R.mark({type:"component",name:t.name,host:u?"this":"parentElement"});c.prepend(l),u&&(s=l);break}case t==="#text":c=w.call(this,d.D.createTextNode(""),e);break;case(t==="<>"||!t):c=w.call(this,d.D.createDocumentFragment(),e);break;case!!P:c=w.call(this,d.D.createElementNS(P,t),e);break;case!c:c=w.call(this,d.D.createElement(t),e)}return it(c),s||(s=c),n.forEach(u=>u(s)),o&&v.pop(),o=2,c}function Ct(t,e=t,n=void 0){let r=Symbol.for("default"),o=Array.from(e.querySelectorAll("slot")).reduce((s,u)=>Reflect.set(s,u.name||r,u)&&s,{}),c=Reflect.has(o,r);if(t.append=new Proxy(t.append,{apply(s,u,l){if(!l.length)return t;let h=d.D.createDocumentFragment();for(let g of l){if(!g||!g.slot){c&&h.appendChild(g);continue}let i=g.slot,f=o[i];at(g,"remove","slot"),f&&(ft(f,g,n),Reflect.deleteProperty(o,i))}return c&&(o[r].replaceWith(h),Reflect.deleteProperty(o,r)),t.append=s,t}}),t!==e){let s=Array.from(t.childNodes);s.forEach(u=>u.remove()),t.append(...s)}return e}function ft(t,e,n){n&&n(t,e);try{t.replaceWith(w(e,{className:[e.className,t.className],dataset:{...t.dataset}}))}catch{t.replaceWith(e)}}R.mark=function(t,e=!1){t=Object.entries(t).map(([o,c])=>o+`="${c}"`).join(" ");let n=e?"":"/",r=d.D.createComment(``);return e&&(r.end=d.D.createComment("")),r};function St(t){let e=this;return function(...r){P=t;let o=R.call(e,...r);return P=void 0,o}}var j=new WeakMap,{setDeleteAttr:J}=d;function w(t,...e){if(!e.length)return t;j.set(t,V(t,this));for(let[n,r]of Object.entries(Object.assign({},...e)))G.call(this,t,n,r);return j.delete(t),t}function G(t,e,n){let{setRemoveAttr:r,s:o}=V(t,this),c=this;n=o.processReactiveAttribute(t,e,n,(u,l)=>G.call(c,t,u,l));let[s]=e;if(s==="=")return r(e.slice(1),n);if(s===".")return Z(t,e.slice(1),n);if(/(aria|data)([A-Z])/.test(e))return e=e.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),r(e,n);switch(e==="className"&&(e="class"),e){case"xlink:href":return r(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 $(o,n,Z.bind(null,t[e]));case"ariaset":return $(o,n,(u,l)=>r("aria-"+u,l));case"classList":return ut.call(c,t,n)}return dt(t,e)?J(t,e,n):r(e,n)}function V(t,e){if(j.has(t))return j.get(t);let r=(t instanceof d.S?pt:lt).bind(null,t,"Attribute"),o=S(e);return{setRemoveAttr:r,s:o}}function ut(t,e){let n=S(this);return $(n,e,(r,o)=>t.classList.toggle(r,o===-1?void 0:!!o)),t}function Dt(t){return Array.from(t.children).forEach(e=>e.remove()),t}function at(t,e,n,r){return t instanceof d.H?t[e+"Attribute"](n,r):t[e+"AttributeNS"](null,n,r)}function dt(t,e){if(!Reflect.has(t,e))return!1;let n=K(t,e);return!_(n.set)}function K(t,e){if(t=Object.getPrototypeOf(t),!t)return{};let n=Object.getOwnPropertyDescriptor(t,e);return n||K(t,e)}function $(t,e,n){if(!(typeof e!="object"||e===null))return Object.entries(e).forEach(function([o,c]){o&&(c=t.processReactiveAttribute(e,o,c,n),n(o,c))})}function Q(t){return Array.isArray(t)?t.filter(Boolean).join(" "):t}function lt(t,e,n,r){return t[(_(r)?"remove":"set")+e](n,Q(r))}function pt(t,e,n,r,o=null){return t[(_(r)?"remove":"set")+e+"NS"](o,n,Q(r))}function Z(t,e,n){if(Reflect.set(t,e,n),!!_(n))return Reflect.deleteProperty(t,e)}function kt(t,e,n=ht){v.push({scope:t,host:(...o)=>o.length?o.forEach(c=>c(t)):t,custom_element:t}),typeof n=="function"&&(n=n.call(t,t));let r=e.call(t,n);return v.pop(),r}function Lt(t){for(let n of["connected","disconnected"])X(t.prototype,n+"Callback",function(r,o,c){r.apply(o,c),o.dispatchEvent(new Event("dde:"+n))});let e="attributeChanged";return X(t.prototype,e+"Callback",function(n,r,o){let[c,,s]=o;r.dispatchEvent(new CustomEvent("dde:"+e,{detail:[c,s]})),n.apply(r,o)}),t.prototype.__dde_lifecycleToEvents=!0,t}function X(t,e,n){t[e]=new Proxy(t[e]||(()=>{}),{apply:n})}function ht(t){return N(t,(e,n)=>e.getAttribute(n))}function Ft(t,e,n){return e||(e={}),function(o,...c){n&&(c.unshift(o),o=typeof n=="function"?n():n);let s=c.length?new CustomEvent(t,Object.assign({detail:c[0]},e)):new Event(t,e);return o.dispatchEvent(s)}}function x(t,e,n){return function(o){return o.addEventListener(t,e,n),o}}var k=d.M?gt():new Proxy({},{get(){return()=>{}}}),bt=new WeakSet;x.connected=function(t,e){let{custom_element:n}=v.current,r="connected";return typeof e!="object"&&(e={}),e.once=!0,function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,t,e),c.__dde_lifecycleToEvents?c:c.isConnected?(c.dispatchEvent(new Event(s)),c):(D(e.signal,()=>k.offConnected(c,t))&&k.onConnected(c,t),c)}};x.disconnected=function(t,e){let{custom_element:n}=v.current,r="disconnected";return typeof e!="object"&&(e={}),e.once=!0,function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,t,e),c.__dde_lifecycleToEvents||D(e.signal,()=>k.offDisconnected(c,t))&&k.onDisconnected(c,t),c}};var z=new WeakMap;x.disconnectedAsAbort=function(t){if(z.has(t))return z.get(t);let e=new AbortController;return z.set(t,e),t(x.disconnected(()=>e.abort())),e};x.attributeChanged=function(t,e){let n="attributeChanged";return typeof e!="object"&&(e={}),function(o){let c="dde:"+n;if(o.addEventListener(c,t,e),o.__dde_lifecycleToEvents||bt.has(o)||!d.M)return o;let s=new d.M(function(l){for(let{attributeName:h,target:g}of l)g.dispatchEvent(new CustomEvent(c,{detail:[h,g.getAttribute(h)]}))});return D(e.signal,()=>s.disconnect())&&s.observe(o,{attributes:!0}),o}};function gt(){let t=new Map,e=!1,n=new d.M(function(i){for(let f of i)if(f.type==="childList"){if(h(f.addedNodes,!0)){s();continue}g(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(!t.has(i))return;let a=t.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(!t.has(i))return;let a=t.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||(t.delete(i),s())}function o(i){if(t.has(i))return t.get(i);let f={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return t.set(i,f),f}function c(){e||(e=!0,n.observe(d.D.body,{childList:!0,subtree:!0}))}function s(){!e||t.size||(e=!1,n.disconnect())}function u(){return new Promise(function(i){(requestIdleCallback||requestAnimationFrame)(i)})}async function l(i,f){t.size>30&&await u();let a=[];if(!(i instanceof Node))return a;for(let p of t.keys())p===i||!(p instanceof Node)||f(p)||i.contains(p)&&a.push(p);return a}function h(i,f){let a=!1;for(let p of i){if(f&&l(p,M=>!M.isConnectedd).then(h),!t.has(p))continue;let A=t.get(p);A.length_c&&(p.dispatchEvent(new Event("dde:connected")),A.connected=new WeakSet,A.length_c=0,A.length_d||t.delete(p),a=!0)}return a}function g(i,f){let a=!1;for(let p of i)f&&l(p,M=>M.isConnectedd).then(g),!(!t.has(p)||!t.get(p).length_d)&&(p.dispatchEvent(new Event("dde:disconnected")),t.delete(p),a=!0);return a}}var b=Symbol.for("observable");function T(t){try{return Reflect.has(t,b)}catch{return!1}}var W=[],m=new WeakMap;function E(t,e){if(typeof t!="function")return Y(!1,t,e);if(T(t))return t;let n=Y(!0),r=function(){let[o,...c]=m.get(r);if(m.set(r,new Set([o])),W.push(r),ot(n,t()),W.pop(),!c.length)return;let s=m.get(r);for(let u of c)s.has(u)||C(u,r)};return m.set(n[b],r),m.set(r,new Set([n])),r(),n}E.action=function(t,e,...n){let r=t[b],{actions:o}=r;if(!o||!Reflect.has(o,e))throw new Error(`'${t}' has no action with name '${e}'!`);if(o[e].apply(r,n),r.skip)return Reflect.deleteProperty(r,"skip");r.listeners.forEach(c=>c(r.value))};E.on=function t(e,n,r={}){let{signal:o}=r;if(!(o&&o.aborted)){if(Array.isArray(e))return e.forEach(c=>t(c,n,r));H(e,n),o&&o.addEventListener("abort",()=>C(e,n))}};E.symbols={onclear:Symbol.for("Observable.onclear")};E.clear=function(...t){for(let n of t){Reflect.deleteProperty(n,"toJSON");let r=n[b];r.onclear.forEach(o=>o.call(r)),e(n,r),Reflect.deleteProperty(n,b)}function e(n,r){r.listeners.forEach(o=>{if(r.listeners.delete(o),!m.has(o))return;let c=m.get(o);c.delete(n),!(c.size>1)&&(n.clear(...c),m.delete(o))})}};var L="__dde_reactive";E.el=function(t,e){let n=R.mark({type:"reactive"},!0),r=n.end,o=d.D.createDocumentFragment();o.append(n,r);let{current:c}=v,s=u=>{if(!n.parentNode||!r.parentNode)return C(t,s);v.push(c);let l=e(u);v.pop(),Array.isArray(l)||(l=[l]);let h=n;for(;(h=n.nextSibling)!==r;)h.remove();n.after(...l)};return H(t,s),nt(t,s,n,e),s(t()),o};var vt={_set(t){this.value=t}};function mt(t){return function(e,n){let r=(...c)=>c.length?e.setAttribute(n,...c):e.getAttribute(n),o=rt(r,r(),vt);return t[n]=o,o}}var U="__dde_attributes";E.observedAttributes=function(t){let e=t[U]={},n=N(t,mt(e));return x.attributeChanged(function({detail:o}){/*! This maps attributes to observables (`O.observedAttributes`). - * Investigate `__dde_attributes` key of the element.*/let[c,s]=o,u=this[U][c];if(u)return E.action(u,"_set",s)})(t),x.disconnected(function(){/*! This removes all observables mapped to attributes (`O.observedAttributes`). - * Investigate `__dde_attributes` key of the element.*/E.clear(...Object.values(this[U]))})(t),n};var et={isObservable:T,processReactiveAttribute(t,e,n,r){if(!T(n))return n;let o=c=>r(e,c);return H(n,o),nt(n,o,t,e),n()}};function nt(t,e,...n){let{current:r}=v;r.prevent||r.host(function(o){o[L]||(o[L]=[],x.disconnected(()=>o[L].forEach(([[c,s]])=>C(c,s,c[b]?.host()===o)))(o)),o[L].push([[t,e],...n])})}function Y(t,e,n){let r=t?()=>tt(r):(...o)=>o.length?ot(r,...o):tt(r);return rt(r,e,n)}var Et=Object.assign(Object.create(null),{stopPropagation(){this.skip=!0}}),q=class extends Error{constructor(){super();let[e,...n]=this.stack.split(` -`),r=e.slice(e.indexOf("@"),e.indexOf(".js:")+4);this.stack=n.find(o=>!o.includes(r))}};function rt(t,e,n){let r=[];B(n)!=="[object Object]"&&(n={});let{onclear:o}=E.symbols;n[o]&&(r.push(n[o]),Reflect.deleteProperty(n,o));let{host:c}=v;return Reflect.defineProperty(t,b,{value:{value:e,actions:n,onclear:r,host:c,listeners:new Set,defined:new q},enumerable:!1,writable:!1,configurable:!0}),t.toJSON=()=>t(),Object.setPrototypeOf(t[b],Et),t}function xt(){return W[W.length-1]}function tt(t){if(!t[b])return;let{value:e,listeners:n}=t[b],r=xt();return r&&n.add(r),m.has(r)&&m.get(r).add(t),e}function ot(t,e,n){if(!t[b])return;let r=t[b];if(!(!n&&r.value===e))return r.value=e,r.listeners.forEach(o=>o(e)),e}function H(t,e){if(t[b])return t[b].listeners.add(e)}function C(t,e,n){let r=t[b];if(!r)return;let o=r.listeners.delete(e);if(n&&!r.listeners.size){if(E.clear(t),!m.has(r))return o;let c=m.get(r);if(!m.has(c))return o;m.get(c).forEach(s=>C(s,c,!0))}return o}F(et); +var D={isObservable(t){return!1},processReactiveAttribute(t,e,n,r){return n}};function $(t,e=!0){return e?Object.assign(D,t):(Object.setPrototypeOf(t,D),t)}function P(t){return D.isPrototypeOf(t)&&t!==D?t:D}function O(t){return typeof t>"u"}function G(t){let e=typeof t;return e!=="object"?e:t===null?"null":Object.prototype.toString.call(t)}function L(t,e){if(!t||!(t instanceof AbortSignal))return!0;if(!t.aborted)return t.addEventListener("abort",e),function(){t.removeEventListener("abort",e)}}function T(t,e){let{observedAttributes:n=[]}=t.constructor;return n.reduce(function(r,o){return Reflect.set(r,ft(o),e(t,o)),r},{})}function ft(t){return t.replace(/-./g,e=>e[1].toUpperCase())}var d={setDeleteAttr:ut,ssr:"",D:globalThis.document,F:globalThis.DocumentFragment,H:globalThis.HTMLElement,S:globalThis.SVGElement,M:globalThis.MutationObserver};function ut(t,e,n){if(Reflect.set(t,e,n),!!O(n)){if(Reflect.deleteProperty(t,e),t instanceof d.H&&t.getAttribute(e)==="undefined")return t.removeAttribute(e);if(Reflect.get(t,e)==="undefined")return Reflect.set(t,e,"")}}var C="__dde_lifecycleToEvents",x="__dde_disconnect_mode";var A=[{get scope(){return d.D.body},host:t=>t?t(d.D.body):d.D.body,custom_element:!1,prevent:!0}],v={get current(){return A[A.length-1]},get host(){return this.current.host},preventDefault(){let{current:t}=this;return t.prevent=!0,t},get state(){return[...A]},push(t={}){return A.push(Object.assign({},this.current,{prevent:!1},t))},pushRoot(){return A.push(A[0])},pop(){if(A.length!==1)return A.pop()}};function V(...t){return this.appendOriginal(...t),this}function at(t){return t.append===V||(t.appendOriginal=t.append,t.append=V),t}var j;function k(t,e,...n){let r=P(this),o=0,c,s;switch((Object(e)!==e||r.isObservable(e))&&(e={textContent:e}),!0){case typeof t=="function":{o=1,v.push({scope:t,host:(...l)=>l.length?(o===1?n.unshift(...l):l.forEach(b=>b(s)),void 0):s}),c=t(e||void 0);let a=c instanceof d.F;if(c.nodeName==="#comment")break;let p=k.mark({type:"component",name:t.name,host:a?"this":"parentElement"});c.prepend(p),a&&(s=p);break}case t==="#text":c=S.call(this,d.D.createTextNode(""),e);break;case(t==="<>"||!t):c=S.call(this,d.D.createDocumentFragment(),e);break;case!!j:c=S.call(this,d.D.createElementNS(j,t),e);break;case!c:c=S.call(this,d.D.createElement(t),e)}return at(c),s||(s=c),n.forEach(a=>a(s)),o&&v.pop(),o=2,c}function Nt(t,e=t,n=void 0){let r=Symbol.for("default"),o=Array.from(e.querySelectorAll("slot")).reduce((s,a)=>Reflect.set(s,a.name||r,a)&&s,{}),c=Reflect.has(o,r);if(t.append=new Proxy(t.append,{apply(s,a,p){if(!p.length)return t;let l=d.D.createDocumentFragment();for(let b of p){if(!b||!b.slot){c&&l.appendChild(b);continue}let N=b.slot,i=o[N];lt(b,"remove","slot"),i&&(dt(i,b,n),Reflect.deleteProperty(o,N))}return c&&(o[r].replaceWith(l),Reflect.deleteProperty(o,r)),t.append=s,t}}),t!==e){let s=Array.from(t.childNodes);s.forEach(a=>a.remove()),t.append(...s)}return e}function dt(t,e,n){n&&n(t,e);try{t.replaceWith(S(e,{className:[e.className,t.className],dataset:{...t.dataset}}))}catch{t.replaceWith(e)}}k.mark=function(t,e=!1){t=Object.entries(t).map(([o,c])=>o+`="${c}"`).join(" ");let n=e?"":"/",r=d.D.createComment(``);return e&&(r.end=d.D.createComment("")),r};function Pt(t){let e=this;return function(...r){j=t;let o=k.call(e,...r);return j=void 0,o}}var M=new WeakMap,{setDeleteAttr:K}=d;function S(t,...e){if(!e.length)return t;M.set(t,Y(t,this));for(let[n,r]of Object.entries(Object.assign({},...e)))X.call(this,t,n,r);return M.delete(t),t}function X(t,e,n){let{setRemoveAttr:r,s:o}=Y(t,this),c=this;n=o.processReactiveAttribute(t,e,n,(a,p)=>X.call(c,t,a,p));let[s]=e;if(s==="=")return r(e.slice(1),n);if(s===".")return Q(t,e.slice(1),n);if(/(aria|data)([A-Z])/.test(e))return e=e.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),r(e,n);switch(e==="className"&&(e="class"),e){case"xlink:href":return r(e,n,"http://www.w3.org/1999/xlink");case"textContent":return K(t,e,n);case"style":if(typeof n!="object")break;case"dataset":return U(o,n,Q.bind(null,t[e]));case"ariaset":return U(o,n,(a,p)=>r("aria-"+a,p));case"classList":return pt.call(c,t,n)}return ht(t,e)?K(t,e,n):r(e,n)}function Y(t,e){if(M.has(t))return M.get(t);let r=(t instanceof d.S?vt:bt).bind(null,t,"Attribute"),o=P(e);return{setRemoveAttr:r,s:o}}function pt(t,e){let n=P(this);return U(n,e,(r,o)=>t.classList.toggle(r,o===-1?void 0:!!o)),t}function Lt(t){return Array.from(t.children).forEach(e=>e.remove()),t}function lt(t,e,n,r){return t instanceof d.H?t[e+"Attribute"](n,r):t[e+"AttributeNS"](null,n,r)}function ht(t,e){if(!Reflect.has(t,e))return!1;let n=tt(t,e);return!O(n.set)}function tt(t,e){if(t=Object.getPrototypeOf(t),!t)return{};let n=Object.getOwnPropertyDescriptor(t,e);return n||tt(t,e)}function U(t,e,n){if(!(typeof e!="object"||e===null))return Object.entries(e).forEach(function([o,c]){o&&(c=t.processReactiveAttribute(e,o,c,n),n(o,c))})}function et(t){return Array.isArray(t)?t.filter(Boolean).join(" "):t}function bt(t,e,n,r){return t[(O(r)?"remove":"set")+e](n,et(r))}function vt(t,e,n,r,o=null){return t[(O(r)?"remove":"set")+e+"NS"](o,n,et(r))}function Q(t,e,n){if(Reflect.set(t,e,n),!!O(n))return Reflect.deleteProperty(t,e)}function qt(t,e,n=gt){v.push({scope:t,host:(...o)=>o.length?o.forEach(c=>c(t)):t,custom_element:t}),typeof n=="function"&&(n=n.call(t,t));let r=e.call(t,n);return v.pop(),r}function Ft(t){return z(t.prototype,"connectedCallback",function(e,n,r){e.apply(n,r),n.dispatchEvent(new Event("dde:connected"))}),t.prototype[x]||(t.prototype[x]="dde"),z(t.prototype,"disconnectedCallback",function(e,n,r){e.apply(n,r);let o=()=>n.dispatchEvent(new Event("dde:disconnected"));if(n[x]!=="dde")return o();(queueMicrotask||setTimeout)(()=>!n.isConnected&&o())}),z(t.prototype,"attributeChangedCallback",function(e,n,r){let[o,,c]=r;n.dispatchEvent(new CustomEvent("dde:attributeChanged",{detail:[o,c]})),e.apply(n,r)}),t.prototype[C]=!0,t}function z(t,e,n){t[e]=new Proxy(t[e]||(()=>{}),{apply:n})}function gt(t){return T(t,(e,n)=>e.getAttribute(n))}function Ht(t,e,n){return e||(e={}),function(o,...c){n&&(c.unshift(o),o=typeof n=="function"?n():n);let s=c.length?new CustomEvent(t,Object.assign({detail:c[0]},e)):new Event(t,e);return o.dispatchEvent(s)}}function y(t,e,n){return function(o){return o.addEventListener(t,e,n),o}}var W=d.M?mt():new Proxy({},{get(){return()=>{}}}),Et=new WeakSet;y.connected=function(t,e){let{custom_element:n}=v.current,r="connected";return typeof e!="object"&&(e={}),typeof e.once!="boolean"&&(e.once=!0),function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,t,e),c[C]?c:c.isConnected?(c.dispatchEvent(new Event(s)),c):(L(e.signal,()=>W.offConnected(c,t))&&W.onConnected(c,t),c)}};y.disconnected=function(t,e){let{custom_element:n}=v.current,r="disconnected";return typeof e!="object"&&(e={}),typeof e.once!="boolean"&&(e.once=!0),function(c){n&&(c=n),c[x]||(c[x]="dde");let s="dde:"+r;return c.addEventListener(s,t,e),c[C]||L(e.signal,()=>W.offDisconnected(c,t))&&W.onDisconnected(c,t),c}};var H=new WeakMap;y.disconnectedAsAbort=function(t){if(H.has(t))return H.get(t);let e=new AbortController;return H.set(t,e),t(y.disconnected(()=>e.abort())),e};y.attributeChanged=function(t,e){let n="attributeChanged";return typeof e!="object"&&(e={}),function(o){let c="dde:"+n;if(o.addEventListener(c,t,e),o[C]||Et.has(o)||!d.M)return o;let s=new d.M(function(p){for(let{attributeName:l,target:b}of p)b.dispatchEvent(new CustomEvent(c,{detail:[l,b.getAttribute(l)]}))});return L(e.signal,()=>s.disconnect())&&s.observe(o,{attributes:!0}),o}};function mt(){let t=new Map,e=!1,n=new d.M(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 u=o(i);u.connected.has(f)||(u.connected.add(f),u.length_c+=1)},offConnected(i,f){if(!t.has(i))return;let u=t.get(i);u.connected.has(f)&&(u.connected.delete(f),u.length_c-=1,r(i,u))},onDisconnected(i,f){c();let u=o(i);u.disconnected.has(f)||(u.disconnected.add(f),u.length_d+=1)},offDisconnected(i,f){if(!t.has(i))return;let u=t.get(i);u.disconnected.has(f)&&(u.disconnected.delete(f),u.length_d-=1,r(i,u))}};function r(i,f){f.length_c||f.length_d||(t.delete(i),s())}function o(i){if(t.has(i))return t.get(i);let f={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return t.set(i,f),f}function c(){e||(e=!0,n.observe(d.D.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 p(i){t.size>30&&await a();let f=[];if(!(i instanceof Node))return f;for(let u of t.keys())u===i||!(u instanceof Node)||i.contains(u)&&f.push(u);return f}function l(i,f){let u=!1;for(let m of i){if(f&&p(m).then(l),!t.has(m))continue;let R=t.get(m);R.length_c&&(m.dispatchEvent(new Event("dde:connected")),R.connected=new WeakSet,R.length_c=0,R.length_d||t.delete(m),u=!0)}return u}function b(i,f){let u=!1;for(let m of i){if(f&&p(m).then(b),!t.has(m)||!t.get(m).length_d)continue;let Z=N(m);m[x]==="dde"?(queueMicrotask||setTimeout)(Z):Z(),u=!0}return u}function N(i){return()=>{i.isConnected||(i.dispatchEvent(new Event("dde:disconnected")),t.delete(i))}}}var h=Symbol.for("observable");function q(t){try{return Reflect.has(t,h)}catch{return!1}}var F=[],g=new WeakMap;function E(t,e){if(typeof t!="function")return nt(!1,t,e);if(q(t))return t;let n=nt(!0),r=function(){let[o,...c]=g.get(r);if(g.set(r,new Set([o])),F.push(r),it(n,t()),F.pop(),!c.length)return;let s=g.get(r);for(let a of c)s.has(a)||w(a,r)};return g.set(n[h],r),g.set(r,new Set([n])),r(),n}E.action=function(t,e,...n){let r=t[h],{actions:o}=r;if(!o||!Reflect.has(o,e))throw new Error(`'${t}' has no action with name '${e}'!`);if(o[e].apply(r,n),r.skip)return Reflect.deleteProperty(r,"skip");r.listeners.forEach(c=>c(r.value))};E.on=function t(e,n,r={}){let{signal:o}=r;if(!(o&&o.aborted)){if(Array.isArray(e))return e.forEach(c=>t(c,n,r));J(e,n),o&&o.addEventListener("abort",()=>w(e,n))}};E.symbols={onclear:Symbol.for("Observable.onclear")};E.clear=function(...t){for(let n of t){Reflect.deleteProperty(n,"toJSON");let r=n[h];r.onclear.forEach(o=>o.call(r)),e(n,r),Reflect.deleteProperty(n,h)}function e(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 _="__dde_reactive";E.el=function(t,e){let n=k.mark({type:"reactive"},!0),r=n.end,o=d.D.createDocumentFragment();o.append(n,r);let{current:c}=v,s=a=>{if(!n.parentNode||!r.parentNode)return w(t,s);v.push(c);let p=e(a);v.pop(),Array.isArray(p)||(p=[p]);let l=n;for(;(l=n.nextSibling)!==r;)l.remove();n.after(...p),n.isConnected&&yt(c.host())};return J(t,s),ct(t,s,n,e),s(t()),o};function yt(t){!t||!t[_]||(requestIdleCallback||setTimeout)(function(){t[_]=t[_].filter(([e,n])=>n.isConnected?!0:(w(...e),!1))})}var xt={_set(t){this.value=t}};function At(t){return function(e,n){let r=(...c)=>c.length?e.setAttribute(n,...c):e.getAttribute(n),o=st(r,r(),xt);return t[n]=o,o}}var B="__dde_attributes";E.observedAttributes=function(t){let e=t[B]={},n=T(t,At(e));return y.attributeChanged(function({detail:o}){/*! This maps attributes to observables (`O.observedAttributes`). + * Investigate `__dde_attributes` key of the element.*/let[c,s]=o,a=this[B][c];if(a)return E.action(a,"_set",s)})(t),y.disconnected(function(){/*! This removes all observables mapped to attributes (`O.observedAttributes`). + * Investigate `__dde_attributes` key of the element.*/E.clear(...Object.values(this[B]))})(t),n};var ot={isObservable:q,processReactiveAttribute(t,e,n,r){if(!q(n))return n;let o=c=>{if(!t.isConnected)return w(n,o);r(e,c)};return J(n,o),ct(n,o,t,e),n()}};function ct(t,e,...n){let{current:r}=v;r.prevent||r.host(function(o){o[_]||(o[_]=[],y.disconnected(()=>o[_].forEach(([[c,s]])=>w(c,s,c[h]?.host()===o)))(o)),o[_].push([[t,e],...n])})}function nt(t,e,n){let r=t?()=>rt(r):(...o)=>o.length?it(r,...o):rt(r);return st(r,e,n)}var Ot=Object.assign(Object.create(null),{stopPropagation(){this.skip=!0}}),I=class extends Error{constructor(){super();let[e,...n]=this.stack.split(` +`),r=e.slice(e.indexOf("@"),e.indexOf(".js:")+4);this.stack=n.find(o=>!o.includes(r))}};function st(t,e,n){let r=[];G(n)!=="[object Object]"&&(n={});let{onclear:o}=E.symbols;n[o]&&(r.push(n[o]),Reflect.deleteProperty(n,o));let{host:c}=v;return Reflect.defineProperty(t,h,{value:{value:e,actions:n,onclear:r,host:c,listeners:new Set,defined:new I},enumerable:!1,writable:!1,configurable:!0}),t.toJSON=()=>t(),Object.setPrototypeOf(t[h],Ot),t}function _t(){return F[F.length-1]}function rt(t){if(!t[h])return;let{value:e,listeners:n}=t[h],r=_t();return r&&n.add(r),g.has(r)&&g.get(r).add(t),e}function it(t,e,n){if(!t[h])return;let r=t[h];if(!(!n&&r.value===e))return r.value=e,r.listeners.forEach(o=>o(e)),e}function J(t,e){if(t[h])return t[h].listeners.add(e)}function w(t,e,n){let r=t[h];if(!r)return;let o=r.listeners.delete(e);if(n&&!r.listeners.size){if(E.clear(t),!g.has(r))return o;let c=g.get(r);if(!g.has(c))return o;g.get(c).forEach(s=>w(s,c,!0))}return o}$(ot); globalThis.dde= {O: E, -assign: w, -assignAttribute: G, -chainableAppend: it, -classListDeclarative: ut, -createElement: R, -createElementNS: St, -customElementRender: kt, -customElementWithDDE: Lt, -dispatchEvent: Ft, -el: R, -elNS: St, -elementAttribute: at, -empty: Dt, -isObservable: T, -lifecycleToEvents: Lt, +assign: S, +assignAttribute: X, +chainableAppend: at, +classListDeclarative: pt, +createElement: k, +createElementNS: Pt, +customElementRender: qt, +customElementWithDDE: Ft, +dispatchEvent: Ht, +el: k, +elNS: Pt, +elementAttribute: lt, +empty: Lt, +isObservable: q, +lifecycleToEvents: Ft, observable: E, -observedAttributes: ht, -on: x, -registerReactivity: F, +observedAttributes: gt, +on: y, +registerReactivity: $, scope: v, -simulateSlots: Ct +simulateSlots: Nt }; })(); \ No newline at end of file diff --git a/dist/dde.js b/dist/dde.js index 6f865ec..b2d06f7 100644 --- a/dist/dde.js +++ b/dist/dde.js @@ -1,25 +1,25 @@ //deka-dom-el library is available via global namespace `dde` (()=> { -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 _(t){return m.isPrototypeOf(t)&&t!==m?t:m}function E(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)}}function T(t,e){let{observedAttributes:n=[]}=t.constructor;return n.reduce(function(c,r){return Reflect.set(c,H(r),e(t,r)),c},{})}function H(t){return t.replace(/-./g,e=>e[1].toUpperCase())}var a={setDeleteAttr:z,ssr:"",D:globalThis.document,F:globalThis.DocumentFragment,H:globalThis.HTMLElement,S:globalThis.SVGElement,M:globalThis.MutationObserver};function z(t,e,n){if(Reflect.set(t,e,n),!!E(n)){if(Reflect.deleteProperty(t,e),t instanceof a.H&&t.getAttribute(e)==="undefined")return t.removeAttribute(e);if(Reflect.get(t,e)==="undefined")return Reflect.set(t,e,"")}}var g=[{get scope(){return a.D.body},host:t=>t?t(a.D.body):a.D.body,custom_element:!1,prevent:!0}],v={get current(){return g[g.length-1]},get host(){return this.current.host},preventDefault(){let{current:t}=this;return t.prevent=!0,t},get state(){return[...g]},push(t={}){return g.push(Object.assign({},this.current,{prevent:!1},t))},pushRoot(){return g.push(g[0])},pop(){if(g.length!==1)return g.pop()}};function L(...t){return this.appendOriginal(...t),this}function B(t){return t.append===L||(t.appendOriginal=t.append,t.append=L),t}var C;function N(t,e,...n){let c=_(this),r=0,o,s;switch((Object(e)!==e||c.isObservable(e))&&(e={textContent:e}),!0){case typeof t=="function":{r=1,v.push({scope:t,host:(...b)=>b.length?(r===1?n.unshift(...b):b.forEach(h=>h(s)),void 0):s}),o=t(e||void 0);let d=o instanceof a.F;if(o.nodeName==="#comment")break;let l=N.mark({type:"component",name:t.name,host:d?"this":"parentElement"});o.prepend(l),d&&(s=l);break}case t==="#text":o=A.call(this,a.D.createTextNode(""),e);break;case(t==="<>"||!t):o=A.call(this,a.D.createDocumentFragment(),e);break;case!!C:o=A.call(this,a.D.createElementNS(C,t),e);break;case!o:o=A.call(this,a.D.createElement(t),e)}return B(o),s||(s=o),n.forEach(d=>d(s)),r&&v.pop(),r=2,o}function st(t,e=t,n=void 0){let c=Symbol.for("default"),r=Array.from(e.querySelectorAll("slot")).reduce((s,d)=>Reflect.set(s,d.name||c,d)&&s,{}),o=Reflect.has(r,c);if(t.append=new Proxy(t.append,{apply(s,d,l){if(!l.length)return t;let b=a.D.createDocumentFragment();for(let h of l){if(!h||!h.slot){o&&b.appendChild(h);continue}let i=h.slot,f=r[i];G(h,"remove","slot"),f&&(I(f,h,n),Reflect.deleteProperty(r,i))}return o&&(r[c].replaceWith(b),Reflect.deleteProperty(r,c)),t.append=s,t}}),t!==e){let s=Array.from(t.childNodes);s.forEach(d=>d.remove()),t.append(...s)}return e}function I(t,e,n){n&&n(t,e);try{t.replaceWith(A(e,{className:[e.className,t.className],dataset:{...t.dataset}}))}catch{t.replaceWith(e)}}N.mark=function(t,e=!1){t=Object.entries(t).map(([r,o])=>r+`="${o}"`).join(" ");let n=e?"":"/",c=a.D.createComment(``);return e&&(c.end=a.D.createComment("")),c};function it(t){let e=this;return function(...c){C=t;let r=N.call(e,...c);return C=void 0,r}}var D=new WeakMap,{setDeleteAttr:M}=a;function A(t,...e){if(!e.length)return t;D.set(t,F(t,this));for(let[n,c]of Object.entries(Object.assign({},...e)))j.call(this,t,n,c);return D.delete(t),t}function j(t,e,n){let{setRemoveAttr:c,s:r}=F(t,this),o=this;n=r.processReactiveAttribute(t,e,n,(d,l)=>j.call(o,t,d,l));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 M(t,e,n);case"style":if(typeof n!="object")break;case"dataset":return S(r,n,W.bind(null,t[e]));case"ariaset":return S(r,n,(d,l)=>c("aria-"+d,l));case"classList":return Z.call(o,t,n)}return V(t,e)?M(t,e,n):c(e,n)}function F(t,e){if(D.has(t))return D.get(t);let c=(t instanceof a.S?K:J).bind(null,t,"Attribute"),r=_(e);return{setRemoveAttr:c,s:r}}function Z(t,e){let n=_(this);return S(n,e,(c,r)=>t.classList.toggle(c,r===-1?void 0:!!r)),t}function ft(t){return Array.from(t.children).forEach(e=>e.remove()),t}function G(t,e,n,c){return t instanceof a.H?t[e+"Attribute"](n,c):t[e+"AttributeNS"](null,n,c)}function V(t,e){if(!Reflect.has(t,e))return!1;let n=$(t,e);return!E(n.set)}function $(t,e){if(t=Object.getPrototypeOf(t),!t)return{};let n=Object.getOwnPropertyDescriptor(t,e);return n||$(t,e)}function S(t,e,n){if(!(typeof e!="object"||e===null))return Object.entries(e).forEach(function([r,o]){r&&(o=t.processReactiveAttribute(e,r,o,n),n(r,o))})}function k(t){return Array.isArray(t)?t.filter(Boolean).join(" "):t}function J(t,e,n,c){return t[(E(c)?"remove":"set")+e](n,k(c))}function K(t,e,n,c,r=null){return t[(E(c)?"remove":"set")+e+"NS"](r,n,k(c))}function W(t,e,n){if(Reflect.set(t,e,n),!!E(n))return Reflect.deleteProperty(t,e)}function pt(t,e,n=Q){v.push({scope:t,host:(...r)=>r.length?r.forEach(o=>o(t)):t,custom_element:t}),typeof n=="function"&&(n=n.call(t,t));let c=e.call(t,n);return v.pop(),c}function lt(t){for(let n of["connected","disconnected"])U(t.prototype,n+"Callback",function(c,r,o){c.apply(r,o),r.dispatchEvent(new Event("dde:"+n))});let e="attributeChanged";return U(t.prototype,e+"Callback",function(n,c,r){let[o,,s]=r;c.dispatchEvent(new CustomEvent("dde:"+e,{detail:[o,s]})),n.apply(c,r)}),t.prototype.__dde_lifecycleToEvents=!0,t}function U(t,e,n){t[e]=new Proxy(t[e]||(()=>{}),{apply:n})}function Q(t){return T(t,(e,n)=>e.getAttribute(n))}function vt(t,e,n){return e||(e={}),function(r,...o){n&&(o.unshift(r),r=typeof n=="function"?n():n);let s=o.length?new CustomEvent(t,Object.assign({detail:o[0]},e)):new Event(t,e);return r.dispatchEvent(s)}}function y(t,e,n){return function(r){return r.addEventListener(t,e,n),r}}var R=a.M?Y():new Proxy({},{get(){return()=>{}}}),X=new WeakSet;y.connected=function(t,e){let{custom_element:n}=v.current,c="connected";return typeof e!="object"&&(e={}),e.once=!0,function(o){n&&(o=n);let s="dde:"+c;return o.addEventListener(s,t,e),o.__dde_lifecycleToEvents?o:o.isConnected?(o.dispatchEvent(new Event(s)),o):(w(e.signal,()=>R.offConnected(o,t))&&R.onConnected(o,t),o)}};y.disconnected=function(t,e){let{custom_element:n}=v.current,c="disconnected";return typeof e!="object"&&(e={}),e.once=!0,function(o){n&&(o=n);let s="dde:"+c;return o.addEventListener(s,t,e),o.__dde_lifecycleToEvents||w(e.signal,()=>R.offDisconnected(o,t))&&R.onDisconnected(o,t),o}};var P=new WeakMap;y.disconnectedAsAbort=function(t){if(P.has(t))return P.get(t);let e=new AbortController;return P.set(t,e),t(y.disconnected(()=>e.abort())),e};y.attributeChanged=function(t,e){let n="attributeChanged";return typeof e!="object"&&(e={}),function(r){let o="dde:"+n;if(r.addEventListener(o,t,e),r.__dde_lifecycleToEvents||X.has(r)||!a.M)return r;let s=new a.M(function(l){for(let{attributeName:b,target:h}of l)h.dispatchEvent(new CustomEvent(o,{detail:[b,h.getAttribute(b)]}))});return w(e.signal,()=>s.disconnect())&&s.observe(r,{attributes:!0}),r}};function Y(){let t=new Map,e=!1,n=new a.M(function(i){for(let f of i)if(f.type==="childList"){if(b(f.addedNodes,!0)){s();continue}h(f.removedNodes,!0)&&s()}});return{onConnected(i,f){o();let u=r(i);u.connected.has(f)||(u.connected.add(f),u.length_c+=1)},offConnected(i,f){if(!t.has(i))return;let u=t.get(i);u.connected.has(f)&&(u.connected.delete(f),u.length_c-=1,c(i,u))},onDisconnected(i,f){o();let u=r(i);u.disconnected.has(f)||(u.disconnected.add(f),u.length_d+=1)},offDisconnected(i,f){if(!t.has(i))return;let u=t.get(i);u.disconnected.has(f)&&(u.disconnected.delete(f),u.length_d-=1,c(i,u))}};function c(i,f){f.length_c||f.length_d||(t.delete(i),s())}function r(i){if(t.has(i))return t.get(i);let f={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return t.set(i,f),f}function o(){e||(e=!0,n.observe(a.D.body,{childList:!0,subtree:!0}))}function s(){!e||t.size||(e=!1,n.disconnect())}function d(){return new Promise(function(i){(requestIdleCallback||requestAnimationFrame)(i)})}async function l(i,f){t.size>30&&await d();let u=[];if(!(i instanceof Node))return u;for(let p of t.keys())p===i||!(p instanceof Node)||f(p)||i.contains(p)&&u.push(p);return u}function b(i,f){let u=!1;for(let p of i){if(f&&l(p,O=>!O.isConnectedd).then(b),!t.has(p))continue;let x=t.get(p);x.length_c&&(p.dispatchEvent(new Event("dde:connected")),x.connected=new WeakSet,x.length_c=0,x.length_d||t.delete(p),u=!0)}return u}function h(i,f){let u=!1;for(let p of i)f&&l(p,O=>O.isConnectedd).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: A, -assignAttribute: j, -chainableAppend: B, -classListDeclarative: Z, -createElement: N, -createElementNS: it, -customElementRender: pt, -customElementWithDDE: lt, -dispatchEvent: vt, -el: N, -elNS: it, -elementAttribute: G, -empty: ft, -lifecycleToEvents: lt, -observedAttributes: Q, -on: y, -registerReactivity: q, -scope: v, -simulateSlots: st +var A={isObservable(t){return!1},processReactiveAttribute(t,e,n,c){return n}};function B(t,e=!0){return e?Object.assign(A,t):(Object.setPrototypeOf(t,A),t)}function R(t){return A.isPrototypeOf(t)&&t!==A?t:A}function x(t){return typeof t>"u"}function _(t,e){if(!t||!(t instanceof AbortSignal))return!0;if(!t.aborted)return t.addEventListener("abort",e),function(){t.removeEventListener("abort",e)}}function W(t,e){let{observedAttributes:n=[]}=t.constructor;return n.reduce(function(c,r){return Reflect.set(c,I(r),e(t,r)),c},{})}function I(t){return t.replace(/-./g,e=>e[1].toUpperCase())}var a={setDeleteAttr:Z,ssr:"",D:globalThis.document,F:globalThis.DocumentFragment,H:globalThis.HTMLElement,S:globalThis.SVGElement,M:globalThis.MutationObserver};function Z(t,e,n){if(Reflect.set(t,e,n),!!x(n)){if(Reflect.deleteProperty(t,e),t instanceof a.H&&t.getAttribute(e)==="undefined")return t.removeAttribute(e);if(Reflect.get(t,e)==="undefined")return Reflect.set(t,e,"")}}var y="__dde_lifecycleToEvents",g="__dde_disconnect_mode";var v=[{get scope(){return a.D.body},host:t=>t?t(a.D.body):a.D.body,custom_element:!1,prevent:!0}],E={get current(){return v[v.length-1]},get host(){return this.current.host},preventDefault(){let{current:t}=this;return t.prevent=!0,t},get state(){return[...v]},push(t={}){return v.push(Object.assign({},this.current,{prevent:!1},t))},pushRoot(){return v.push(v[0])},pop(){if(v.length!==1)return v.pop()}};function j(...t){return this.appendOriginal(...t),this}function G(t){return t.append===j||(t.appendOriginal=t.append,t.append=j),t}var O;function M(t,e,...n){let c=R(this),r=0,o,i;switch((Object(e)!==e||c.isObservable(e))&&(e={textContent:e}),!0){case typeof t=="function":{r=1,E.push({scope:t,host:(...h)=>h.length?(r===1?n.unshift(...h):h.forEach(l=>l(i)),void 0):i}),o=t(e||void 0);let d=o instanceof a.F;if(o.nodeName==="#comment")break;let p=M.mark({type:"component",name:t.name,host:d?"this":"parentElement"});o.prepend(p),d&&(i=p);break}case t==="#text":o=w.call(this,a.D.createTextNode(""),e);break;case(t==="<>"||!t):o=w.call(this,a.D.createDocumentFragment(),e);break;case!!O:o=w.call(this,a.D.createElementNS(O,t),e);break;case!o:o=w.call(this,a.D.createElement(t),e)}return G(o),i||(i=o),n.forEach(d=>d(i)),r&&E.pop(),r=2,o}function ft(t,e=t,n=void 0){let c=Symbol.for("default"),r=Array.from(e.querySelectorAll("slot")).reduce((i,d)=>Reflect.set(i,d.name||c,d)&&i,{}),o=Reflect.has(r,c);if(t.append=new Proxy(t.append,{apply(i,d,p){if(!p.length)return t;let h=a.D.createDocumentFragment();for(let l of p){if(!l||!l.slot){o&&h.appendChild(l);continue}let D=l.slot,s=r[D];K(l,"remove","slot"),s&&(V(s,l,n),Reflect.deleteProperty(r,D))}return o&&(r[c].replaceWith(h),Reflect.deleteProperty(r,c)),t.append=i,t}}),t!==e){let i=Array.from(t.childNodes);i.forEach(d=>d.remove()),t.append(...i)}return e}function V(t,e,n){n&&n(t,e);try{t.replaceWith(w(e,{className:[e.className,t.className],dataset:{...t.dataset}}))}catch{t.replaceWith(e)}}M.mark=function(t,e=!1){t=Object.entries(t).map(([r,o])=>r+`="${o}"`).join(" ");let n=e?"":"/",c=a.D.createComment(``);return e&&(c.end=a.D.createComment("")),c};function at(t){let e=this;return function(...c){O=t;let r=M.call(e,...c);return O=void 0,r}}var S=new WeakMap,{setDeleteAttr:q}=a;function w(t,...e){if(!e.length)return t;S.set(t,U(t,this));for(let[n,c]of Object.entries(Object.assign({},...e)))$.call(this,t,n,c);return S.delete(t),t}function $(t,e,n){let{setRemoveAttr:c,s:r}=U(t,this),o=this;n=r.processReactiveAttribute(t,e,n,(d,p)=>$.call(o,t,d,p));let[i]=e;if(i==="=")return c(e.slice(1),n);if(i===".")return F(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 q(t,e,n);case"style":if(typeof n!="object")break;case"dataset":return L(r,n,F.bind(null,t[e]));case"ariaset":return L(r,n,(d,p)=>c("aria-"+d,p));case"classList":return J.call(o,t,n)}return Q(t,e)?q(t,e,n):c(e,n)}function U(t,e){if(S.has(t))return S.get(t);let c=(t instanceof a.S?Y:X).bind(null,t,"Attribute"),r=R(e);return{setRemoveAttr:c,s:r}}function J(t,e){let n=R(this);return L(n,e,(c,r)=>t.classList.toggle(c,r===-1?void 0:!!r)),t}function dt(t){return Array.from(t.children).forEach(e=>e.remove()),t}function K(t,e,n,c){return t instanceof a.H?t[e+"Attribute"](n,c):t[e+"AttributeNS"](null,n,c)}function Q(t,e){if(!Reflect.has(t,e))return!1;let n=H(t,e);return!x(n.set)}function H(t,e){if(t=Object.getPrototypeOf(t),!t)return{};let n=Object.getOwnPropertyDescriptor(t,e);return n||H(t,e)}function L(t,e,n){if(!(typeof e!="object"||e===null))return Object.entries(e).forEach(function([r,o]){r&&(o=t.processReactiveAttribute(e,r,o,n),n(r,o))})}function z(t){return Array.isArray(t)?t.filter(Boolean).join(" "):t}function X(t,e,n,c){return t[(x(c)?"remove":"set")+e](n,z(c))}function Y(t,e,n,c,r=null){return t[(x(c)?"remove":"set")+e+"NS"](r,n,z(c))}function F(t,e,n){if(Reflect.set(t,e,n),!!x(n))return Reflect.deleteProperty(t,e)}function gt(t,e,n=tt){E.push({scope:t,host:(...r)=>r.length?r.forEach(o=>o(t)):t,custom_element:t}),typeof n=="function"&&(n=n.call(t,t));let c=e.call(t,n);return E.pop(),c}function vt(t){return T(t.prototype,"connectedCallback",function(e,n,c){e.apply(n,c),n.dispatchEvent(new Event("dde:connected"))}),t.prototype[g]||(t.prototype[g]="dde"),T(t.prototype,"disconnectedCallback",function(e,n,c){e.apply(n,c);let r=()=>n.dispatchEvent(new Event("dde:disconnected"));if(n[g]!=="dde")return r();(queueMicrotask||setTimeout)(()=>!n.isConnected&&r())}),T(t.prototype,"attributeChangedCallback",function(e,n,c){let[r,,o]=c;n.dispatchEvent(new CustomEvent("dde:attributeChanged",{detail:[r,o]})),e.apply(n,c)}),t.prototype[y]=!0,t}function T(t,e,n){t[e]=new Proxy(t[e]||(()=>{}),{apply:n})}function tt(t){return W(t,(e,n)=>e.getAttribute(n))}function mt(t,e,n){return e||(e={}),function(r,...o){n&&(o.unshift(r),r=typeof n=="function"?n():n);let i=o.length?new CustomEvent(t,Object.assign({detail:o[0]},e)):new Event(t,e);return r.dispatchEvent(i)}}function C(t,e,n){return function(r){return r.addEventListener(t,e,n),r}}var N=a.M?nt():new Proxy({},{get(){return()=>{}}}),et=new WeakSet;C.connected=function(t,e){let{custom_element:n}=E.current,c="connected";return typeof e!="object"&&(e={}),typeof e.once!="boolean"&&(e.once=!0),function(o){n&&(o=n);let i="dde:"+c;return o.addEventListener(i,t,e),o[y]?o:o.isConnected?(o.dispatchEvent(new Event(i)),o):(_(e.signal,()=>N.offConnected(o,t))&&N.onConnected(o,t),o)}};C.disconnected=function(t,e){let{custom_element:n}=E.current,c="disconnected";return typeof e!="object"&&(e={}),typeof e.once!="boolean"&&(e.once=!0),function(o){n&&(o=n),o[g]||(o[g]="dde");let i="dde:"+c;return o.addEventListener(i,t,e),o[y]||_(e.signal,()=>N.offDisconnected(o,t))&&N.onDisconnected(o,t),o}};var k=new WeakMap;C.disconnectedAsAbort=function(t){if(k.has(t))return k.get(t);let e=new AbortController;return k.set(t,e),t(C.disconnected(()=>e.abort())),e};C.attributeChanged=function(t,e){let n="attributeChanged";return typeof e!="object"&&(e={}),function(r){let o="dde:"+n;if(r.addEventListener(o,t,e),r[y]||et.has(r)||!a.M)return r;let i=new a.M(function(p){for(let{attributeName:h,target:l}of p)l.dispatchEvent(new CustomEvent(o,{detail:[h,l.getAttribute(h)]}))});return _(e.signal,()=>i.disconnect())&&i.observe(r,{attributes:!0}),r}};function nt(){let t=new Map,e=!1,n=new a.M(function(s){for(let u of s)if(u.type==="childList"){if(h(u.addedNodes,!0)){i();continue}l(u.removedNodes,!0)&&i()}});return{onConnected(s,u){o();let f=r(s);f.connected.has(u)||(f.connected.add(u),f.length_c+=1)},offConnected(s,u){if(!t.has(s))return;let f=t.get(s);f.connected.has(u)&&(f.connected.delete(u),f.length_c-=1,c(s,f))},onDisconnected(s,u){o();let f=r(s);f.disconnected.has(u)||(f.disconnected.add(u),f.length_d+=1)},offDisconnected(s,u){if(!t.has(s))return;let f=t.get(s);f.disconnected.has(u)&&(f.disconnected.delete(u),f.length_d-=1,c(s,f))}};function c(s,u){u.length_c||u.length_d||(t.delete(s),i())}function r(s){if(t.has(s))return t.get(s);let u={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return t.set(s,u),u}function o(){e||(e=!0,n.observe(a.D.body,{childList:!0,subtree:!0}))}function i(){!e||t.size||(e=!1,n.disconnect())}function d(){return new Promise(function(s){(requestIdleCallback||requestAnimationFrame)(s)})}async function p(s){t.size>30&&await d();let u=[];if(!(s instanceof Node))return u;for(let f of t.keys())f===s||!(f instanceof Node)||s.contains(f)&&u.push(f);return u}function h(s,u){let f=!1;for(let b of s){if(u&&p(b).then(h),!t.has(b))continue;let m=t.get(b);m.length_c&&(b.dispatchEvent(new Event("dde:connected")),m.connected=new WeakSet,m.length_c=0,m.length_d||t.delete(b),f=!0)}return f}function l(s,u){let f=!1;for(let b of s){if(u&&p(b).then(l),!t.has(b)||!t.get(b).length_d)continue;let P=D(b);b[g]==="dde"?(queueMicrotask||setTimeout)(P):P(),f=!0}return f}function D(s){return()=>{s.isConnected||(s.dispatchEvent(new Event("dde:disconnected")),t.delete(s))}}} +globalThis.dde= {assign: w, +assignAttribute: $, +chainableAppend: G, +classListDeclarative: J, +createElement: M, +createElementNS: at, +customElementRender: gt, +customElementWithDDE: vt, +dispatchEvent: mt, +el: M, +elNS: at, +elementAttribute: K, +empty: dt, +lifecycleToEvents: vt, +observedAttributes: tt, +on: C, +registerReactivity: B, +scope: E, +simulateSlots: ft }; })(); \ No newline at end of file diff --git a/dist/esm-with-observables.d.ts b/dist/esm-with-observables.d.ts index debccbe..8e755e1 100644 --- a/dist/esm-with-observables.d.ts +++ b/dist/esm-with-observables.d.ts @@ -54,7 +54,7 @@ interface observable{ * */ el(observable: Observable, el: (v: S)=> Element | Element[] | DocumentFragment): DocumentFragment; - attribute(name: string, initial?: string): Observable; + observedAttributes(custom_element: HTMLElement): Record>; } export const observable: observable; export const O: observable; @@ -64,16 +64,11 @@ declare global { type ddeActions= Actions } type CustomElementTagNameMap= { '#text': Text, '#comment': Comment } -declare global { - interface ddePublicElementTagNameMap{ - } -} type SupportedElement= HTMLElementTagNameMap[keyof HTMLElementTagNameMap] | SVGElementTagNameMap[keyof SVGElementTagNameMap] | MathMLElementTagNameMap[keyof MathMLElementTagNameMap] | CustomElementTagNameMap[keyof CustomElementTagNameMap] - | ddePublicElementTagNameMap[keyof ddePublicElementTagNameMap]; declare global { type ddeComponentAttributes= Record | undefined; type ddeElementAddon= (element: El)=> El | void; @@ -112,7 +107,7 @@ export function classListDeclarative(element: El, c 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 ExtendedHTMLElementTagNameMap= HTMLElementTagNameMap & CustomElementTagNameMap; type textContent= string | ( (set?: string)=> string ); // TODO: for some reason `Observable` leads to `attrs?: any` export function el< TAG extends keyof ExtendedHTMLElementTagNameMap & string, @@ -239,9 +234,22 @@ export const scope: { pop(): ReturnType["pop"]>, }; -/* TypeScript MEH // TODO for SVG */ -type ddeAppend= (...nodes: (Node | string)[])=> el; +export function customElementRender< + EL extends HTMLElement, + P extends any = Record +>( + custom_element: EL, + render: (props: P)=> SupportedElement, + props?: P | ((...args: any[])=> P) +): EL +export function customElementWithDDE(custom_element: EL): EL +export function lifecycleToEvents(custom_element: EL): EL +export function observedAttributes(custom_element: HTMLElement): Record + +/* TypeScript MEH */ declare global{ + type ddeAppend= (...nodes: (Node | string)[])=> el; + interface ddeDocumentFragment extends DocumentFragment{ append: ddeAppend; } interface ddeHTMLElement extends HTMLElement{ append: ddeAppend; } interface ddeSVGElement extends SVGElement{ append: ddeAppend; } diff --git a/dist/esm-with-observables.js b/dist/esm-with-observables.js index f25a554..d6d0b6f 100644 --- a/dist/esm-with-observables.js +++ b/dist/esm-with-observables.js @@ -1,4 +1,4 @@ -var O={isObservable(t){return!1},processReactiveAttribute(t,e,n,r){return n}};function F(t,e=!0){return e?Object.assign(O,t):(Object.setPrototypeOf(t,O),t)}function S(t){return O.isPrototypeOf(t)&&t!==O?t:O}function _(t){return typeof t>"u"}function B(t){let e=typeof t;return e!=="object"?e:t===null?"null":Object.prototype.toString.call(t)}function D(t,e){if(!t||!(t instanceof AbortSignal))return!0;if(!t.aborted)return t.addEventListener("abort",e),function(){t.removeEventListener("abort",e)}}function N(t,e){let{observedAttributes:n=[]}=t.constructor;return n.reduce(function(r,o){return Reflect.set(r,ct(o),e(t,o)),r},{})}function ct(t){return t.replace(/-./g,e=>e[1].toUpperCase())}var d={setDeleteAttr:st,ssr:"",D:globalThis.document,F:globalThis.DocumentFragment,H:globalThis.HTMLElement,S:globalThis.SVGElement,M:globalThis.MutationObserver};function st(t,e,n){if(Reflect.set(t,e,n),!!_(n)){if(Reflect.deleteProperty(t,e),t instanceof d.H&&t.getAttribute(e)==="undefined")return t.removeAttribute(e);if(Reflect.get(t,e)==="undefined")return Reflect.set(t,e,"")}}var y=[{get scope(){return d.D.body},host:t=>t?t(d.D.body):d.D.body,custom_element:!1,prevent:!0}],v={get current(){return y[y.length-1]},get host(){return this.current.host},preventDefault(){let{current:t}=this;return t.prevent=!0,t},get state(){return[...y]},push(t={}){return y.push(Object.assign({},this.current,{prevent:!1},t))},pushRoot(){return y.push(y[0])},pop(){if(y.length!==1)return y.pop()}};function I(...t){return this.appendOriginal(...t),this}function it(t){return t.append===I||(t.appendOriginal=t.append,t.append=I),t}var P;function R(t,e,...n){let r=S(this),o=0,c,s;switch((Object(e)!==e||r.isObservable(e))&&(e={textContent:e}),!0){case typeof t=="function":{o=1,v.push({scope:t,host:(...h)=>h.length?(o===1?n.unshift(...h):h.forEach(g=>g(s)),void 0):s}),c=t(e||void 0);let u=c instanceof d.F;if(c.nodeName==="#comment")break;let l=R.mark({type:"component",name:t.name,host:u?"this":"parentElement"});c.prepend(l),u&&(s=l);break}case t==="#text":c=w.call(this,d.D.createTextNode(""),e);break;case(t==="<>"||!t):c=w.call(this,d.D.createDocumentFragment(),e);break;case!!P:c=w.call(this,d.D.createElementNS(P,t),e);break;case!c:c=w.call(this,d.D.createElement(t),e)}return it(c),s||(s=c),n.forEach(u=>u(s)),o&&v.pop(),o=2,c}function Ct(t,e=t,n=void 0){let r=Symbol.for("default"),o=Array.from(e.querySelectorAll("slot")).reduce((s,u)=>Reflect.set(s,u.name||r,u)&&s,{}),c=Reflect.has(o,r);if(t.append=new Proxy(t.append,{apply(s,u,l){if(!l.length)return t;let h=d.D.createDocumentFragment();for(let g of l){if(!g||!g.slot){c&&h.appendChild(g);continue}let i=g.slot,f=o[i];at(g,"remove","slot"),f&&(ft(f,g,n),Reflect.deleteProperty(o,i))}return c&&(o[r].replaceWith(h),Reflect.deleteProperty(o,r)),t.append=s,t}}),t!==e){let s=Array.from(t.childNodes);s.forEach(u=>u.remove()),t.append(...s)}return e}function ft(t,e,n){n&&n(t,e);try{t.replaceWith(w(e,{className:[e.className,t.className],dataset:{...t.dataset}}))}catch{t.replaceWith(e)}}R.mark=function(t,e=!1){t=Object.entries(t).map(([o,c])=>o+`="${c}"`).join(" ");let n=e?"":"/",r=d.D.createComment(``);return e&&(r.end=d.D.createComment("")),r};function St(t){let e=this;return function(...r){P=t;let o=R.call(e,...r);return P=void 0,o}}var j=new WeakMap,{setDeleteAttr:J}=d;function w(t,...e){if(!e.length)return t;j.set(t,V(t,this));for(let[n,r]of Object.entries(Object.assign({},...e)))G.call(this,t,n,r);return j.delete(t),t}function G(t,e,n){let{setRemoveAttr:r,s:o}=V(t,this),c=this;n=o.processReactiveAttribute(t,e,n,(u,l)=>G.call(c,t,u,l));let[s]=e;if(s==="=")return r(e.slice(1),n);if(s===".")return Z(t,e.slice(1),n);if(/(aria|data)([A-Z])/.test(e))return e=e.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),r(e,n);switch(e==="className"&&(e="class"),e){case"xlink:href":return r(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 $(o,n,Z.bind(null,t[e]));case"ariaset":return $(o,n,(u,l)=>r("aria-"+u,l));case"classList":return ut.call(c,t,n)}return dt(t,e)?J(t,e,n):r(e,n)}function V(t,e){if(j.has(t))return j.get(t);let r=(t instanceof d.S?pt:lt).bind(null,t,"Attribute"),o=S(e);return{setRemoveAttr:r,s:o}}function ut(t,e){let n=S(this);return $(n,e,(r,o)=>t.classList.toggle(r,o===-1?void 0:!!o)),t}function Dt(t){return Array.from(t.children).forEach(e=>e.remove()),t}function at(t,e,n,r){return t instanceof d.H?t[e+"Attribute"](n,r):t[e+"AttributeNS"](null,n,r)}function dt(t,e){if(!Reflect.has(t,e))return!1;let n=K(t,e);return!_(n.set)}function K(t,e){if(t=Object.getPrototypeOf(t),!t)return{};let n=Object.getOwnPropertyDescriptor(t,e);return n||K(t,e)}function $(t,e,n){if(!(typeof e!="object"||e===null))return Object.entries(e).forEach(function([o,c]){o&&(c=t.processReactiveAttribute(e,o,c,n),n(o,c))})}function Q(t){return Array.isArray(t)?t.filter(Boolean).join(" "):t}function lt(t,e,n,r){return t[(_(r)?"remove":"set")+e](n,Q(r))}function pt(t,e,n,r,o=null){return t[(_(r)?"remove":"set")+e+"NS"](o,n,Q(r))}function Z(t,e,n){if(Reflect.set(t,e,n),!!_(n))return Reflect.deleteProperty(t,e)}function kt(t,e,n=ht){v.push({scope:t,host:(...o)=>o.length?o.forEach(c=>c(t)):t,custom_element:t}),typeof n=="function"&&(n=n.call(t,t));let r=e.call(t,n);return v.pop(),r}function Lt(t){for(let n of["connected","disconnected"])X(t.prototype,n+"Callback",function(r,o,c){r.apply(o,c),o.dispatchEvent(new Event("dde:"+n))});let e="attributeChanged";return X(t.prototype,e+"Callback",function(n,r,o){let[c,,s]=o;r.dispatchEvent(new CustomEvent("dde:"+e,{detail:[c,s]})),n.apply(r,o)}),t.prototype.__dde_lifecycleToEvents=!0,t}function X(t,e,n){t[e]=new Proxy(t[e]||(()=>{}),{apply:n})}function ht(t){return N(t,(e,n)=>e.getAttribute(n))}function Ft(t,e,n){return e||(e={}),function(o,...c){n&&(c.unshift(o),o=typeof n=="function"?n():n);let s=c.length?new CustomEvent(t,Object.assign({detail:c[0]},e)):new Event(t,e);return o.dispatchEvent(s)}}function x(t,e,n){return function(o){return o.addEventListener(t,e,n),o}}var k=d.M?gt():new Proxy({},{get(){return()=>{}}}),bt=new WeakSet;x.connected=function(t,e){let{custom_element:n}=v.current,r="connected";return typeof e!="object"&&(e={}),e.once=!0,function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,t,e),c.__dde_lifecycleToEvents?c:c.isConnected?(c.dispatchEvent(new Event(s)),c):(D(e.signal,()=>k.offConnected(c,t))&&k.onConnected(c,t),c)}};x.disconnected=function(t,e){let{custom_element:n}=v.current,r="disconnected";return typeof e!="object"&&(e={}),e.once=!0,function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,t,e),c.__dde_lifecycleToEvents||D(e.signal,()=>k.offDisconnected(c,t))&&k.onDisconnected(c,t),c}};var z=new WeakMap;x.disconnectedAsAbort=function(t){if(z.has(t))return z.get(t);let e=new AbortController;return z.set(t,e),t(x.disconnected(()=>e.abort())),e};x.attributeChanged=function(t,e){let n="attributeChanged";return typeof e!="object"&&(e={}),function(o){let c="dde:"+n;if(o.addEventListener(c,t,e),o.__dde_lifecycleToEvents||bt.has(o)||!d.M)return o;let s=new d.M(function(l){for(let{attributeName:h,target:g}of l)g.dispatchEvent(new CustomEvent(c,{detail:[h,g.getAttribute(h)]}))});return D(e.signal,()=>s.disconnect())&&s.observe(o,{attributes:!0}),o}};function gt(){let t=new Map,e=!1,n=new d.M(function(i){for(let f of i)if(f.type==="childList"){if(h(f.addedNodes,!0)){s();continue}g(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(!t.has(i))return;let a=t.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(!t.has(i))return;let a=t.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||(t.delete(i),s())}function o(i){if(t.has(i))return t.get(i);let f={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return t.set(i,f),f}function c(){e||(e=!0,n.observe(d.D.body,{childList:!0,subtree:!0}))}function s(){!e||t.size||(e=!1,n.disconnect())}function u(){return new Promise(function(i){(requestIdleCallback||requestAnimationFrame)(i)})}async function l(i,f){t.size>30&&await u();let a=[];if(!(i instanceof Node))return a;for(let p of t.keys())p===i||!(p instanceof Node)||f(p)||i.contains(p)&&a.push(p);return a}function h(i,f){let a=!1;for(let p of i){if(f&&l(p,M=>!M.isConnectedd).then(h),!t.has(p))continue;let A=t.get(p);A.length_c&&(p.dispatchEvent(new Event("dde:connected")),A.connected=new WeakSet,A.length_c=0,A.length_d||t.delete(p),a=!0)}return a}function g(i,f){let a=!1;for(let p of i)f&&l(p,M=>M.isConnectedd).then(g),!(!t.has(p)||!t.get(p).length_d)&&(p.dispatchEvent(new Event("dde:disconnected")),t.delete(p),a=!0);return a}}var b=Symbol.for("observable");function T(t){try{return Reflect.has(t,b)}catch{return!1}}var W=[],m=new WeakMap;function E(t,e){if(typeof t!="function")return Y(!1,t,e);if(T(t))return t;let n=Y(!0),r=function(){let[o,...c]=m.get(r);if(m.set(r,new Set([o])),W.push(r),ot(n,t()),W.pop(),!c.length)return;let s=m.get(r);for(let u of c)s.has(u)||C(u,r)};return m.set(n[b],r),m.set(r,new Set([n])),r(),n}E.action=function(t,e,...n){let r=t[b],{actions:o}=r;if(!o||!Reflect.has(o,e))throw new Error(`'${t}' has no action with name '${e}'!`);if(o[e].apply(r,n),r.skip)return Reflect.deleteProperty(r,"skip");r.listeners.forEach(c=>c(r.value))};E.on=function t(e,n,r={}){let{signal:o}=r;if(!(o&&o.aborted)){if(Array.isArray(e))return e.forEach(c=>t(c,n,r));H(e,n),o&&o.addEventListener("abort",()=>C(e,n))}};E.symbols={onclear:Symbol.for("Observable.onclear")};E.clear=function(...t){for(let n of t){Reflect.deleteProperty(n,"toJSON");let r=n[b];r.onclear.forEach(o=>o.call(r)),e(n,r),Reflect.deleteProperty(n,b)}function e(n,r){r.listeners.forEach(o=>{if(r.listeners.delete(o),!m.has(o))return;let c=m.get(o);c.delete(n),!(c.size>1)&&(n.clear(...c),m.delete(o))})}};var L="__dde_reactive";E.el=function(t,e){let n=R.mark({type:"reactive"},!0),r=n.end,o=d.D.createDocumentFragment();o.append(n,r);let{current:c}=v,s=u=>{if(!n.parentNode||!r.parentNode)return C(t,s);v.push(c);let l=e(u);v.pop(),Array.isArray(l)||(l=[l]);let h=n;for(;(h=n.nextSibling)!==r;)h.remove();n.after(...l)};return H(t,s),nt(t,s,n,e),s(t()),o};var vt={_set(t){this.value=t}};function mt(t){return function(e,n){let r=(...c)=>c.length?e.setAttribute(n,...c):e.getAttribute(n),o=rt(r,r(),vt);return t[n]=o,o}}var U="__dde_attributes";E.observedAttributes=function(t){let e=t[U]={},n=N(t,mt(e));return x.attributeChanged(function({detail:o}){/*! This maps attributes to observables (`O.observedAttributes`). - * Investigate `__dde_attributes` key of the element.*/let[c,s]=o,u=this[U][c];if(u)return E.action(u,"_set",s)})(t),x.disconnected(function(){/*! This removes all observables mapped to attributes (`O.observedAttributes`). - * Investigate `__dde_attributes` key of the element.*/E.clear(...Object.values(this[U]))})(t),n};var et={isObservable:T,processReactiveAttribute(t,e,n,r){if(!T(n))return n;let o=c=>r(e,c);return H(n,o),nt(n,o,t,e),n()}};function nt(t,e,...n){let{current:r}=v;r.prevent||r.host(function(o){o[L]||(o[L]=[],x.disconnected(()=>o[L].forEach(([[c,s]])=>C(c,s,c[b]?.host()===o)))(o)),o[L].push([[t,e],...n])})}function Y(t,e,n){let r=t?()=>tt(r):(...o)=>o.length?ot(r,...o):tt(r);return rt(r,e,n)}var Et=Object.assign(Object.create(null),{stopPropagation(){this.skip=!0}}),q=class extends Error{constructor(){super();let[e,...n]=this.stack.split(` -`),r=e.slice(e.indexOf("@"),e.indexOf(".js:")+4);this.stack=n.find(o=>!o.includes(r))}};function rt(t,e,n){let r=[];B(n)!=="[object Object]"&&(n={});let{onclear:o}=E.symbols;n[o]&&(r.push(n[o]),Reflect.deleteProperty(n,o));let{host:c}=v;return Reflect.defineProperty(t,b,{value:{value:e,actions:n,onclear:r,host:c,listeners:new Set,defined:new q},enumerable:!1,writable:!1,configurable:!0}),t.toJSON=()=>t(),Object.setPrototypeOf(t[b],Et),t}function xt(){return W[W.length-1]}function tt(t){if(!t[b])return;let{value:e,listeners:n}=t[b],r=xt();return r&&n.add(r),m.has(r)&&m.get(r).add(t),e}function ot(t,e,n){if(!t[b])return;let r=t[b];if(!(!n&&r.value===e))return r.value=e,r.listeners.forEach(o=>o(e)),e}function H(t,e){if(t[b])return t[b].listeners.add(e)}function C(t,e,n){let r=t[b];if(!r)return;let o=r.listeners.delete(e);if(n&&!r.listeners.size){if(E.clear(t),!m.has(r))return o;let c=m.get(r);if(!m.has(c))return o;m.get(c).forEach(s=>C(s,c,!0))}return o}F(et);export{E as O,w as assign,G as assignAttribute,it as chainableAppend,ut as classListDeclarative,R as createElement,St as createElementNS,kt as customElementRender,Lt as customElementWithDDE,Ft as dispatchEvent,R as el,St as elNS,at as elementAttribute,Dt as empty,T as isObservable,Lt as lifecycleToEvents,E as observable,ht as observedAttributes,x as on,F as registerReactivity,v as scope,Ct as simulateSlots}; +var D={isObservable(t){return!1},processReactiveAttribute(t,e,n,r){return n}};function $(t,e=!0){return e?Object.assign(D,t):(Object.setPrototypeOf(t,D),t)}function P(t){return D.isPrototypeOf(t)&&t!==D?t:D}function O(t){return typeof t>"u"}function G(t){let e=typeof t;return e!=="object"?e:t===null?"null":Object.prototype.toString.call(t)}function L(t,e){if(!t||!(t instanceof AbortSignal))return!0;if(!t.aborted)return t.addEventListener("abort",e),function(){t.removeEventListener("abort",e)}}function T(t,e){let{observedAttributes:n=[]}=t.constructor;return n.reduce(function(r,o){return Reflect.set(r,ft(o),e(t,o)),r},{})}function ft(t){return t.replace(/-./g,e=>e[1].toUpperCase())}var d={setDeleteAttr:ut,ssr:"",D:globalThis.document,F:globalThis.DocumentFragment,H:globalThis.HTMLElement,S:globalThis.SVGElement,M:globalThis.MutationObserver};function ut(t,e,n){if(Reflect.set(t,e,n),!!O(n)){if(Reflect.deleteProperty(t,e),t instanceof d.H&&t.getAttribute(e)==="undefined")return t.removeAttribute(e);if(Reflect.get(t,e)==="undefined")return Reflect.set(t,e,"")}}var C="__dde_lifecycleToEvents",x="__dde_disconnect_mode";var A=[{get scope(){return d.D.body},host:t=>t?t(d.D.body):d.D.body,custom_element:!1,prevent:!0}],v={get current(){return A[A.length-1]},get host(){return this.current.host},preventDefault(){let{current:t}=this;return t.prevent=!0,t},get state(){return[...A]},push(t={}){return A.push(Object.assign({},this.current,{prevent:!1},t))},pushRoot(){return A.push(A[0])},pop(){if(A.length!==1)return A.pop()}};function V(...t){return this.appendOriginal(...t),this}function at(t){return t.append===V||(t.appendOriginal=t.append,t.append=V),t}var j;function k(t,e,...n){let r=P(this),o=0,c,s;switch((Object(e)!==e||r.isObservable(e))&&(e={textContent:e}),!0){case typeof t=="function":{o=1,v.push({scope:t,host:(...l)=>l.length?(o===1?n.unshift(...l):l.forEach(b=>b(s)),void 0):s}),c=t(e||void 0);let a=c instanceof d.F;if(c.nodeName==="#comment")break;let p=k.mark({type:"component",name:t.name,host:a?"this":"parentElement"});c.prepend(p),a&&(s=p);break}case t==="#text":c=S.call(this,d.D.createTextNode(""),e);break;case(t==="<>"||!t):c=S.call(this,d.D.createDocumentFragment(),e);break;case!!j:c=S.call(this,d.D.createElementNS(j,t),e);break;case!c:c=S.call(this,d.D.createElement(t),e)}return at(c),s||(s=c),n.forEach(a=>a(s)),o&&v.pop(),o=2,c}function Nt(t,e=t,n=void 0){let r=Symbol.for("default"),o=Array.from(e.querySelectorAll("slot")).reduce((s,a)=>Reflect.set(s,a.name||r,a)&&s,{}),c=Reflect.has(o,r);if(t.append=new Proxy(t.append,{apply(s,a,p){if(!p.length)return t;let l=d.D.createDocumentFragment();for(let b of p){if(!b||!b.slot){c&&l.appendChild(b);continue}let N=b.slot,i=o[N];lt(b,"remove","slot"),i&&(dt(i,b,n),Reflect.deleteProperty(o,N))}return c&&(o[r].replaceWith(l),Reflect.deleteProperty(o,r)),t.append=s,t}}),t!==e){let s=Array.from(t.childNodes);s.forEach(a=>a.remove()),t.append(...s)}return e}function dt(t,e,n){n&&n(t,e);try{t.replaceWith(S(e,{className:[e.className,t.className],dataset:{...t.dataset}}))}catch{t.replaceWith(e)}}k.mark=function(t,e=!1){t=Object.entries(t).map(([o,c])=>o+`="${c}"`).join(" ");let n=e?"":"/",r=d.D.createComment(``);return e&&(r.end=d.D.createComment("")),r};function Pt(t){let e=this;return function(...r){j=t;let o=k.call(e,...r);return j=void 0,o}}var M=new WeakMap,{setDeleteAttr:K}=d;function S(t,...e){if(!e.length)return t;M.set(t,Y(t,this));for(let[n,r]of Object.entries(Object.assign({},...e)))X.call(this,t,n,r);return M.delete(t),t}function X(t,e,n){let{setRemoveAttr:r,s:o}=Y(t,this),c=this;n=o.processReactiveAttribute(t,e,n,(a,p)=>X.call(c,t,a,p));let[s]=e;if(s==="=")return r(e.slice(1),n);if(s===".")return Q(t,e.slice(1),n);if(/(aria|data)([A-Z])/.test(e))return e=e.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),r(e,n);switch(e==="className"&&(e="class"),e){case"xlink:href":return r(e,n,"http://www.w3.org/1999/xlink");case"textContent":return K(t,e,n);case"style":if(typeof n!="object")break;case"dataset":return U(o,n,Q.bind(null,t[e]));case"ariaset":return U(o,n,(a,p)=>r("aria-"+a,p));case"classList":return pt.call(c,t,n)}return ht(t,e)?K(t,e,n):r(e,n)}function Y(t,e){if(M.has(t))return M.get(t);let r=(t instanceof d.S?vt:bt).bind(null,t,"Attribute"),o=P(e);return{setRemoveAttr:r,s:o}}function pt(t,e){let n=P(this);return U(n,e,(r,o)=>t.classList.toggle(r,o===-1?void 0:!!o)),t}function Lt(t){return Array.from(t.children).forEach(e=>e.remove()),t}function lt(t,e,n,r){return t instanceof d.H?t[e+"Attribute"](n,r):t[e+"AttributeNS"](null,n,r)}function ht(t,e){if(!Reflect.has(t,e))return!1;let n=tt(t,e);return!O(n.set)}function tt(t,e){if(t=Object.getPrototypeOf(t),!t)return{};let n=Object.getOwnPropertyDescriptor(t,e);return n||tt(t,e)}function U(t,e,n){if(!(typeof e!="object"||e===null))return Object.entries(e).forEach(function([o,c]){o&&(c=t.processReactiveAttribute(e,o,c,n),n(o,c))})}function et(t){return Array.isArray(t)?t.filter(Boolean).join(" "):t}function bt(t,e,n,r){return t[(O(r)?"remove":"set")+e](n,et(r))}function vt(t,e,n,r,o=null){return t[(O(r)?"remove":"set")+e+"NS"](o,n,et(r))}function Q(t,e,n){if(Reflect.set(t,e,n),!!O(n))return Reflect.deleteProperty(t,e)}function qt(t,e,n=gt){v.push({scope:t,host:(...o)=>o.length?o.forEach(c=>c(t)):t,custom_element:t}),typeof n=="function"&&(n=n.call(t,t));let r=e.call(t,n);return v.pop(),r}function Ft(t){return z(t.prototype,"connectedCallback",function(e,n,r){e.apply(n,r),n.dispatchEvent(new Event("dde:connected"))}),t.prototype[x]||(t.prototype[x]="dde"),z(t.prototype,"disconnectedCallback",function(e,n,r){e.apply(n,r);let o=()=>n.dispatchEvent(new Event("dde:disconnected"));if(n[x]!=="dde")return o();(queueMicrotask||setTimeout)(()=>!n.isConnected&&o())}),z(t.prototype,"attributeChangedCallback",function(e,n,r){let[o,,c]=r;n.dispatchEvent(new CustomEvent("dde:attributeChanged",{detail:[o,c]})),e.apply(n,r)}),t.prototype[C]=!0,t}function z(t,e,n){t[e]=new Proxy(t[e]||(()=>{}),{apply:n})}function gt(t){return T(t,(e,n)=>e.getAttribute(n))}function Ht(t,e,n){return e||(e={}),function(o,...c){n&&(c.unshift(o),o=typeof n=="function"?n():n);let s=c.length?new CustomEvent(t,Object.assign({detail:c[0]},e)):new Event(t,e);return o.dispatchEvent(s)}}function y(t,e,n){return function(o){return o.addEventListener(t,e,n),o}}var W=d.M?mt():new Proxy({},{get(){return()=>{}}}),Et=new WeakSet;y.connected=function(t,e){let{custom_element:n}=v.current,r="connected";return typeof e!="object"&&(e={}),typeof e.once!="boolean"&&(e.once=!0),function(c){n&&(c=n);let s="dde:"+r;return c.addEventListener(s,t,e),c[C]?c:c.isConnected?(c.dispatchEvent(new Event(s)),c):(L(e.signal,()=>W.offConnected(c,t))&&W.onConnected(c,t),c)}};y.disconnected=function(t,e){let{custom_element:n}=v.current,r="disconnected";return typeof e!="object"&&(e={}),typeof e.once!="boolean"&&(e.once=!0),function(c){n&&(c=n),c[x]||(c[x]="dde");let s="dde:"+r;return c.addEventListener(s,t,e),c[C]||L(e.signal,()=>W.offDisconnected(c,t))&&W.onDisconnected(c,t),c}};var H=new WeakMap;y.disconnectedAsAbort=function(t){if(H.has(t))return H.get(t);let e=new AbortController;return H.set(t,e),t(y.disconnected(()=>e.abort())),e};y.attributeChanged=function(t,e){let n="attributeChanged";return typeof e!="object"&&(e={}),function(o){let c="dde:"+n;if(o.addEventListener(c,t,e),o[C]||Et.has(o)||!d.M)return o;let s=new d.M(function(p){for(let{attributeName:l,target:b}of p)b.dispatchEvent(new CustomEvent(c,{detail:[l,b.getAttribute(l)]}))});return L(e.signal,()=>s.disconnect())&&s.observe(o,{attributes:!0}),o}};function mt(){let t=new Map,e=!1,n=new d.M(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 u=o(i);u.connected.has(f)||(u.connected.add(f),u.length_c+=1)},offConnected(i,f){if(!t.has(i))return;let u=t.get(i);u.connected.has(f)&&(u.connected.delete(f),u.length_c-=1,r(i,u))},onDisconnected(i,f){c();let u=o(i);u.disconnected.has(f)||(u.disconnected.add(f),u.length_d+=1)},offDisconnected(i,f){if(!t.has(i))return;let u=t.get(i);u.disconnected.has(f)&&(u.disconnected.delete(f),u.length_d-=1,r(i,u))}};function r(i,f){f.length_c||f.length_d||(t.delete(i),s())}function o(i){if(t.has(i))return t.get(i);let f={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return t.set(i,f),f}function c(){e||(e=!0,n.observe(d.D.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 p(i){t.size>30&&await a();let f=[];if(!(i instanceof Node))return f;for(let u of t.keys())u===i||!(u instanceof Node)||i.contains(u)&&f.push(u);return f}function l(i,f){let u=!1;for(let m of i){if(f&&p(m).then(l),!t.has(m))continue;let R=t.get(m);R.length_c&&(m.dispatchEvent(new Event("dde:connected")),R.connected=new WeakSet,R.length_c=0,R.length_d||t.delete(m),u=!0)}return u}function b(i,f){let u=!1;for(let m of i){if(f&&p(m).then(b),!t.has(m)||!t.get(m).length_d)continue;let Z=N(m);m[x]==="dde"?(queueMicrotask||setTimeout)(Z):Z(),u=!0}return u}function N(i){return()=>{i.isConnected||(i.dispatchEvent(new Event("dde:disconnected")),t.delete(i))}}}var h=Symbol.for("observable");function q(t){try{return Reflect.has(t,h)}catch{return!1}}var F=[],g=new WeakMap;function E(t,e){if(typeof t!="function")return nt(!1,t,e);if(q(t))return t;let n=nt(!0),r=function(){let[o,...c]=g.get(r);if(g.set(r,new Set([o])),F.push(r),it(n,t()),F.pop(),!c.length)return;let s=g.get(r);for(let a of c)s.has(a)||w(a,r)};return g.set(n[h],r),g.set(r,new Set([n])),r(),n}E.action=function(t,e,...n){let r=t[h],{actions:o}=r;if(!o||!Reflect.has(o,e))throw new Error(`'${t}' has no action with name '${e}'!`);if(o[e].apply(r,n),r.skip)return Reflect.deleteProperty(r,"skip");r.listeners.forEach(c=>c(r.value))};E.on=function t(e,n,r={}){let{signal:o}=r;if(!(o&&o.aborted)){if(Array.isArray(e))return e.forEach(c=>t(c,n,r));J(e,n),o&&o.addEventListener("abort",()=>w(e,n))}};E.symbols={onclear:Symbol.for("Observable.onclear")};E.clear=function(...t){for(let n of t){Reflect.deleteProperty(n,"toJSON");let r=n[h];r.onclear.forEach(o=>o.call(r)),e(n,r),Reflect.deleteProperty(n,h)}function e(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 _="__dde_reactive";E.el=function(t,e){let n=k.mark({type:"reactive"},!0),r=n.end,o=d.D.createDocumentFragment();o.append(n,r);let{current:c}=v,s=a=>{if(!n.parentNode||!r.parentNode)return w(t,s);v.push(c);let p=e(a);v.pop(),Array.isArray(p)||(p=[p]);let l=n;for(;(l=n.nextSibling)!==r;)l.remove();n.after(...p),n.isConnected&&yt(c.host())};return J(t,s),ct(t,s,n,e),s(t()),o};function yt(t){!t||!t[_]||(requestIdleCallback||setTimeout)(function(){t[_]=t[_].filter(([e,n])=>n.isConnected?!0:(w(...e),!1))})}var xt={_set(t){this.value=t}};function At(t){return function(e,n){let r=(...c)=>c.length?e.setAttribute(n,...c):e.getAttribute(n),o=st(r,r(),xt);return t[n]=o,o}}var B="__dde_attributes";E.observedAttributes=function(t){let e=t[B]={},n=T(t,At(e));return y.attributeChanged(function({detail:o}){/*! This maps attributes to observables (`O.observedAttributes`). + * Investigate `__dde_attributes` key of the element.*/let[c,s]=o,a=this[B][c];if(a)return E.action(a,"_set",s)})(t),y.disconnected(function(){/*! This removes all observables mapped to attributes (`O.observedAttributes`). + * Investigate `__dde_attributes` key of the element.*/E.clear(...Object.values(this[B]))})(t),n};var ot={isObservable:q,processReactiveAttribute(t,e,n,r){if(!q(n))return n;let o=c=>{if(!t.isConnected)return w(n,o);r(e,c)};return J(n,o),ct(n,o,t,e),n()}};function ct(t,e,...n){let{current:r}=v;r.prevent||r.host(function(o){o[_]||(o[_]=[],y.disconnected(()=>o[_].forEach(([[c,s]])=>w(c,s,c[h]?.host()===o)))(o)),o[_].push([[t,e],...n])})}function nt(t,e,n){let r=t?()=>rt(r):(...o)=>o.length?it(r,...o):rt(r);return st(r,e,n)}var Ot=Object.assign(Object.create(null),{stopPropagation(){this.skip=!0}}),I=class extends Error{constructor(){super();let[e,...n]=this.stack.split(` +`),r=e.slice(e.indexOf("@"),e.indexOf(".js:")+4);this.stack=n.find(o=>!o.includes(r))}};function st(t,e,n){let r=[];G(n)!=="[object Object]"&&(n={});let{onclear:o}=E.symbols;n[o]&&(r.push(n[o]),Reflect.deleteProperty(n,o));let{host:c}=v;return Reflect.defineProperty(t,h,{value:{value:e,actions:n,onclear:r,host:c,listeners:new Set,defined:new I},enumerable:!1,writable:!1,configurable:!0}),t.toJSON=()=>t(),Object.setPrototypeOf(t[h],Ot),t}function _t(){return F[F.length-1]}function rt(t){if(!t[h])return;let{value:e,listeners:n}=t[h],r=_t();return r&&n.add(r),g.has(r)&&g.get(r).add(t),e}function it(t,e,n){if(!t[h])return;let r=t[h];if(!(!n&&r.value===e))return r.value=e,r.listeners.forEach(o=>o(e)),e}function J(t,e){if(t[h])return t[h].listeners.add(e)}function w(t,e,n){let r=t[h];if(!r)return;let o=r.listeners.delete(e);if(n&&!r.listeners.size){if(E.clear(t),!g.has(r))return o;let c=g.get(r);if(!g.has(c))return o;g.get(c).forEach(s=>w(s,c,!0))}return o}$(ot);export{E as O,S as assign,X as assignAttribute,at as chainableAppend,pt as classListDeclarative,k as createElement,Pt as createElementNS,qt as customElementRender,Ft as customElementWithDDE,Ht as dispatchEvent,k as el,Pt as elNS,lt as elementAttribute,Lt as empty,q as isObservable,Ft as lifecycleToEvents,E as observable,gt as observedAttributes,y as on,$ as registerReactivity,v as scope,Nt as simulateSlots}; diff --git a/dist/esm.d.ts b/dist/esm.d.ts index debccbe..8e755e1 100644 --- a/dist/esm.d.ts +++ b/dist/esm.d.ts @@ -54,7 +54,7 @@ interface observable{ * */ el(observable: Observable, el: (v: S)=> Element | Element[] | DocumentFragment): DocumentFragment; - attribute(name: string, initial?: string): Observable; + observedAttributes(custom_element: HTMLElement): Record>; } export const observable: observable; export const O: observable; @@ -64,16 +64,11 @@ declare global { type ddeActions= Actions } type CustomElementTagNameMap= { '#text': Text, '#comment': Comment } -declare global { - interface ddePublicElementTagNameMap{ - } -} type SupportedElement= HTMLElementTagNameMap[keyof HTMLElementTagNameMap] | SVGElementTagNameMap[keyof SVGElementTagNameMap] | MathMLElementTagNameMap[keyof MathMLElementTagNameMap] | CustomElementTagNameMap[keyof CustomElementTagNameMap] - | ddePublicElementTagNameMap[keyof ddePublicElementTagNameMap]; declare global { type ddeComponentAttributes= Record | undefined; type ddeElementAddon= (element: El)=> El | void; @@ -112,7 +107,7 @@ export function classListDeclarative(element: El, c 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 ExtendedHTMLElementTagNameMap= HTMLElementTagNameMap & CustomElementTagNameMap; type textContent= string | ( (set?: string)=> string ); // TODO: for some reason `Observable` leads to `attrs?: any` export function el< TAG extends keyof ExtendedHTMLElementTagNameMap & string, @@ -239,9 +234,22 @@ export const scope: { pop(): ReturnType["pop"]>, }; -/* TypeScript MEH // TODO for SVG */ -type ddeAppend= (...nodes: (Node | string)[])=> el; +export function customElementRender< + EL extends HTMLElement, + P extends any = Record +>( + custom_element: EL, + render: (props: P)=> SupportedElement, + props?: P | ((...args: any[])=> P) +): EL +export function customElementWithDDE(custom_element: EL): EL +export function lifecycleToEvents(custom_element: EL): EL +export function observedAttributes(custom_element: HTMLElement): Record + +/* TypeScript MEH */ declare global{ + type ddeAppend= (...nodes: (Node | string)[])=> el; + interface ddeDocumentFragment extends DocumentFragment{ append: ddeAppend; } interface ddeHTMLElement extends HTMLElement{ append: ddeAppend; } interface ddeSVGElement extends SVGElement{ append: ddeAppend; } diff --git a/dist/esm.js b/dist/esm.js index d5e0b15..15c75ca 100644 --- a/dist/esm.js +++ b/dist/esm.js @@ -1 +1 @@ -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 _(t){return m.isPrototypeOf(t)&&t!==m?t:m}function E(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)}}function T(t,e){let{observedAttributes:n=[]}=t.constructor;return n.reduce(function(c,r){return Reflect.set(c,H(r),e(t,r)),c},{})}function H(t){return t.replace(/-./g,e=>e[1].toUpperCase())}var a={setDeleteAttr:z,ssr:"",D:globalThis.document,F:globalThis.DocumentFragment,H:globalThis.HTMLElement,S:globalThis.SVGElement,M:globalThis.MutationObserver};function z(t,e,n){if(Reflect.set(t,e,n),!!E(n)){if(Reflect.deleteProperty(t,e),t instanceof a.H&&t.getAttribute(e)==="undefined")return t.removeAttribute(e);if(Reflect.get(t,e)==="undefined")return Reflect.set(t,e,"")}}var g=[{get scope(){return a.D.body},host:t=>t?t(a.D.body):a.D.body,custom_element:!1,prevent:!0}],v={get current(){return g[g.length-1]},get host(){return this.current.host},preventDefault(){let{current:t}=this;return t.prevent=!0,t},get state(){return[...g]},push(t={}){return g.push(Object.assign({},this.current,{prevent:!1},t))},pushRoot(){return g.push(g[0])},pop(){if(g.length!==1)return g.pop()}};function L(...t){return this.appendOriginal(...t),this}function B(t){return t.append===L||(t.appendOriginal=t.append,t.append=L),t}var C;function N(t,e,...n){let c=_(this),r=0,o,s;switch((Object(e)!==e||c.isObservable(e))&&(e={textContent:e}),!0){case typeof t=="function":{r=1,v.push({scope:t,host:(...b)=>b.length?(r===1?n.unshift(...b):b.forEach(h=>h(s)),void 0):s}),o=t(e||void 0);let d=o instanceof a.F;if(o.nodeName==="#comment")break;let l=N.mark({type:"component",name:t.name,host:d?"this":"parentElement"});o.prepend(l),d&&(s=l);break}case t==="#text":o=A.call(this,a.D.createTextNode(""),e);break;case(t==="<>"||!t):o=A.call(this,a.D.createDocumentFragment(),e);break;case!!C:o=A.call(this,a.D.createElementNS(C,t),e);break;case!o:o=A.call(this,a.D.createElement(t),e)}return B(o),s||(s=o),n.forEach(d=>d(s)),r&&v.pop(),r=2,o}function st(t,e=t,n=void 0){let c=Symbol.for("default"),r=Array.from(e.querySelectorAll("slot")).reduce((s,d)=>Reflect.set(s,d.name||c,d)&&s,{}),o=Reflect.has(r,c);if(t.append=new Proxy(t.append,{apply(s,d,l){if(!l.length)return t;let b=a.D.createDocumentFragment();for(let h of l){if(!h||!h.slot){o&&b.appendChild(h);continue}let i=h.slot,f=r[i];G(h,"remove","slot"),f&&(I(f,h,n),Reflect.deleteProperty(r,i))}return o&&(r[c].replaceWith(b),Reflect.deleteProperty(r,c)),t.append=s,t}}),t!==e){let s=Array.from(t.childNodes);s.forEach(d=>d.remove()),t.append(...s)}return e}function I(t,e,n){n&&n(t,e);try{t.replaceWith(A(e,{className:[e.className,t.className],dataset:{...t.dataset}}))}catch{t.replaceWith(e)}}N.mark=function(t,e=!1){t=Object.entries(t).map(([r,o])=>r+`="${o}"`).join(" ");let n=e?"":"/",c=a.D.createComment(``);return e&&(c.end=a.D.createComment("")),c};function it(t){let e=this;return function(...c){C=t;let r=N.call(e,...c);return C=void 0,r}}var D=new WeakMap,{setDeleteAttr:M}=a;function A(t,...e){if(!e.length)return t;D.set(t,F(t,this));for(let[n,c]of Object.entries(Object.assign({},...e)))j.call(this,t,n,c);return D.delete(t),t}function j(t,e,n){let{setRemoveAttr:c,s:r}=F(t,this),o=this;n=r.processReactiveAttribute(t,e,n,(d,l)=>j.call(o,t,d,l));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 M(t,e,n);case"style":if(typeof n!="object")break;case"dataset":return S(r,n,W.bind(null,t[e]));case"ariaset":return S(r,n,(d,l)=>c("aria-"+d,l));case"classList":return Z.call(o,t,n)}return V(t,e)?M(t,e,n):c(e,n)}function F(t,e){if(D.has(t))return D.get(t);let c=(t instanceof a.S?K:J).bind(null,t,"Attribute"),r=_(e);return{setRemoveAttr:c,s:r}}function Z(t,e){let n=_(this);return S(n,e,(c,r)=>t.classList.toggle(c,r===-1?void 0:!!r)),t}function ft(t){return Array.from(t.children).forEach(e=>e.remove()),t}function G(t,e,n,c){return t instanceof a.H?t[e+"Attribute"](n,c):t[e+"AttributeNS"](null,n,c)}function V(t,e){if(!Reflect.has(t,e))return!1;let n=$(t,e);return!E(n.set)}function $(t,e){if(t=Object.getPrototypeOf(t),!t)return{};let n=Object.getOwnPropertyDescriptor(t,e);return n||$(t,e)}function S(t,e,n){if(!(typeof e!="object"||e===null))return Object.entries(e).forEach(function([r,o]){r&&(o=t.processReactiveAttribute(e,r,o,n),n(r,o))})}function k(t){return Array.isArray(t)?t.filter(Boolean).join(" "):t}function J(t,e,n,c){return t[(E(c)?"remove":"set")+e](n,k(c))}function K(t,e,n,c,r=null){return t[(E(c)?"remove":"set")+e+"NS"](r,n,k(c))}function W(t,e,n){if(Reflect.set(t,e,n),!!E(n))return Reflect.deleteProperty(t,e)}function pt(t,e,n=Q){v.push({scope:t,host:(...r)=>r.length?r.forEach(o=>o(t)):t,custom_element:t}),typeof n=="function"&&(n=n.call(t,t));let c=e.call(t,n);return v.pop(),c}function lt(t){for(let n of["connected","disconnected"])U(t.prototype,n+"Callback",function(c,r,o){c.apply(r,o),r.dispatchEvent(new Event("dde:"+n))});let e="attributeChanged";return U(t.prototype,e+"Callback",function(n,c,r){let[o,,s]=r;c.dispatchEvent(new CustomEvent("dde:"+e,{detail:[o,s]})),n.apply(c,r)}),t.prototype.__dde_lifecycleToEvents=!0,t}function U(t,e,n){t[e]=new Proxy(t[e]||(()=>{}),{apply:n})}function Q(t){return T(t,(e,n)=>e.getAttribute(n))}function vt(t,e,n){return e||(e={}),function(r,...o){n&&(o.unshift(r),r=typeof n=="function"?n():n);let s=o.length?new CustomEvent(t,Object.assign({detail:o[0]},e)):new Event(t,e);return r.dispatchEvent(s)}}function y(t,e,n){return function(r){return r.addEventListener(t,e,n),r}}var R=a.M?Y():new Proxy({},{get(){return()=>{}}}),X=new WeakSet;y.connected=function(t,e){let{custom_element:n}=v.current,c="connected";return typeof e!="object"&&(e={}),e.once=!0,function(o){n&&(o=n);let s="dde:"+c;return o.addEventListener(s,t,e),o.__dde_lifecycleToEvents?o:o.isConnected?(o.dispatchEvent(new Event(s)),o):(w(e.signal,()=>R.offConnected(o,t))&&R.onConnected(o,t),o)}};y.disconnected=function(t,e){let{custom_element:n}=v.current,c="disconnected";return typeof e!="object"&&(e={}),e.once=!0,function(o){n&&(o=n);let s="dde:"+c;return o.addEventListener(s,t,e),o.__dde_lifecycleToEvents||w(e.signal,()=>R.offDisconnected(o,t))&&R.onDisconnected(o,t),o}};var P=new WeakMap;y.disconnectedAsAbort=function(t){if(P.has(t))return P.get(t);let e=new AbortController;return P.set(t,e),t(y.disconnected(()=>e.abort())),e};y.attributeChanged=function(t,e){let n="attributeChanged";return typeof e!="object"&&(e={}),function(r){let o="dde:"+n;if(r.addEventListener(o,t,e),r.__dde_lifecycleToEvents||X.has(r)||!a.M)return r;let s=new a.M(function(l){for(let{attributeName:b,target:h}of l)h.dispatchEvent(new CustomEvent(o,{detail:[b,h.getAttribute(b)]}))});return w(e.signal,()=>s.disconnect())&&s.observe(r,{attributes:!0}),r}};function Y(){let t=new Map,e=!1,n=new a.M(function(i){for(let f of i)if(f.type==="childList"){if(b(f.addedNodes,!0)){s();continue}h(f.removedNodes,!0)&&s()}});return{onConnected(i,f){o();let u=r(i);u.connected.has(f)||(u.connected.add(f),u.length_c+=1)},offConnected(i,f){if(!t.has(i))return;let u=t.get(i);u.connected.has(f)&&(u.connected.delete(f),u.length_c-=1,c(i,u))},onDisconnected(i,f){o();let u=r(i);u.disconnected.has(f)||(u.disconnected.add(f),u.length_d+=1)},offDisconnected(i,f){if(!t.has(i))return;let u=t.get(i);u.disconnected.has(f)&&(u.disconnected.delete(f),u.length_d-=1,c(i,u))}};function c(i,f){f.length_c||f.length_d||(t.delete(i),s())}function r(i){if(t.has(i))return t.get(i);let f={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return t.set(i,f),f}function o(){e||(e=!0,n.observe(a.D.body,{childList:!0,subtree:!0}))}function s(){!e||t.size||(e=!1,n.disconnect())}function d(){return new Promise(function(i){(requestIdleCallback||requestAnimationFrame)(i)})}async function l(i,f){t.size>30&&await d();let u=[];if(!(i instanceof Node))return u;for(let p of t.keys())p===i||!(p instanceof Node)||f(p)||i.contains(p)&&u.push(p);return u}function b(i,f){let u=!1;for(let p of i){if(f&&l(p,O=>!O.isConnectedd).then(b),!t.has(p))continue;let x=t.get(p);x.length_c&&(p.dispatchEvent(new Event("dde:connected")),x.connected=new WeakSet,x.length_c=0,x.length_d||t.delete(p),u=!0)}return u}function h(i,f){let u=!1;for(let p of i)f&&l(p,O=>O.isConnectedd).then(h),!(!t.has(p)||!t.get(p).length_d)&&(p.dispatchEvent(new Event("dde:disconnected")),t.delete(p),u=!0);return u}}export{A as assign,j as assignAttribute,B as chainableAppend,Z as classListDeclarative,N as createElement,it as createElementNS,pt as customElementRender,lt as customElementWithDDE,vt as dispatchEvent,N as el,it as elNS,G as elementAttribute,ft as empty,lt as lifecycleToEvents,Q as observedAttributes,y as on,q as registerReactivity,v as scope,st as simulateSlots}; +var A={isObservable(t){return!1},processReactiveAttribute(t,e,n,c){return n}};function B(t,e=!0){return e?Object.assign(A,t):(Object.setPrototypeOf(t,A),t)}function R(t){return A.isPrototypeOf(t)&&t!==A?t:A}function x(t){return typeof t>"u"}function _(t,e){if(!t||!(t instanceof AbortSignal))return!0;if(!t.aborted)return t.addEventListener("abort",e),function(){t.removeEventListener("abort",e)}}function W(t,e){let{observedAttributes:n=[]}=t.constructor;return n.reduce(function(c,r){return Reflect.set(c,I(r),e(t,r)),c},{})}function I(t){return t.replace(/-./g,e=>e[1].toUpperCase())}var a={setDeleteAttr:Z,ssr:"",D:globalThis.document,F:globalThis.DocumentFragment,H:globalThis.HTMLElement,S:globalThis.SVGElement,M:globalThis.MutationObserver};function Z(t,e,n){if(Reflect.set(t,e,n),!!x(n)){if(Reflect.deleteProperty(t,e),t instanceof a.H&&t.getAttribute(e)==="undefined")return t.removeAttribute(e);if(Reflect.get(t,e)==="undefined")return Reflect.set(t,e,"")}}var y="__dde_lifecycleToEvents",g="__dde_disconnect_mode";var v=[{get scope(){return a.D.body},host:t=>t?t(a.D.body):a.D.body,custom_element:!1,prevent:!0}],E={get current(){return v[v.length-1]},get host(){return this.current.host},preventDefault(){let{current:t}=this;return t.prevent=!0,t},get state(){return[...v]},push(t={}){return v.push(Object.assign({},this.current,{prevent:!1},t))},pushRoot(){return v.push(v[0])},pop(){if(v.length!==1)return v.pop()}};function j(...t){return this.appendOriginal(...t),this}function G(t){return t.append===j||(t.appendOriginal=t.append,t.append=j),t}var O;function M(t,e,...n){let c=R(this),r=0,o,i;switch((Object(e)!==e||c.isObservable(e))&&(e={textContent:e}),!0){case typeof t=="function":{r=1,E.push({scope:t,host:(...h)=>h.length?(r===1?n.unshift(...h):h.forEach(l=>l(i)),void 0):i}),o=t(e||void 0);let d=o instanceof a.F;if(o.nodeName==="#comment")break;let p=M.mark({type:"component",name:t.name,host:d?"this":"parentElement"});o.prepend(p),d&&(i=p);break}case t==="#text":o=w.call(this,a.D.createTextNode(""),e);break;case(t==="<>"||!t):o=w.call(this,a.D.createDocumentFragment(),e);break;case!!O:o=w.call(this,a.D.createElementNS(O,t),e);break;case!o:o=w.call(this,a.D.createElement(t),e)}return G(o),i||(i=o),n.forEach(d=>d(i)),r&&E.pop(),r=2,o}function ft(t,e=t,n=void 0){let c=Symbol.for("default"),r=Array.from(e.querySelectorAll("slot")).reduce((i,d)=>Reflect.set(i,d.name||c,d)&&i,{}),o=Reflect.has(r,c);if(t.append=new Proxy(t.append,{apply(i,d,p){if(!p.length)return t;let h=a.D.createDocumentFragment();for(let l of p){if(!l||!l.slot){o&&h.appendChild(l);continue}let D=l.slot,s=r[D];K(l,"remove","slot"),s&&(V(s,l,n),Reflect.deleteProperty(r,D))}return o&&(r[c].replaceWith(h),Reflect.deleteProperty(r,c)),t.append=i,t}}),t!==e){let i=Array.from(t.childNodes);i.forEach(d=>d.remove()),t.append(...i)}return e}function V(t,e,n){n&&n(t,e);try{t.replaceWith(w(e,{className:[e.className,t.className],dataset:{...t.dataset}}))}catch{t.replaceWith(e)}}M.mark=function(t,e=!1){t=Object.entries(t).map(([r,o])=>r+`="${o}"`).join(" ");let n=e?"":"/",c=a.D.createComment(``);return e&&(c.end=a.D.createComment("")),c};function at(t){let e=this;return function(...c){O=t;let r=M.call(e,...c);return O=void 0,r}}var S=new WeakMap,{setDeleteAttr:q}=a;function w(t,...e){if(!e.length)return t;S.set(t,U(t,this));for(let[n,c]of Object.entries(Object.assign({},...e)))$.call(this,t,n,c);return S.delete(t),t}function $(t,e,n){let{setRemoveAttr:c,s:r}=U(t,this),o=this;n=r.processReactiveAttribute(t,e,n,(d,p)=>$.call(o,t,d,p));let[i]=e;if(i==="=")return c(e.slice(1),n);if(i===".")return F(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 q(t,e,n);case"style":if(typeof n!="object")break;case"dataset":return L(r,n,F.bind(null,t[e]));case"ariaset":return L(r,n,(d,p)=>c("aria-"+d,p));case"classList":return J.call(o,t,n)}return Q(t,e)?q(t,e,n):c(e,n)}function U(t,e){if(S.has(t))return S.get(t);let c=(t instanceof a.S?Y:X).bind(null,t,"Attribute"),r=R(e);return{setRemoveAttr:c,s:r}}function J(t,e){let n=R(this);return L(n,e,(c,r)=>t.classList.toggle(c,r===-1?void 0:!!r)),t}function dt(t){return Array.from(t.children).forEach(e=>e.remove()),t}function K(t,e,n,c){return t instanceof a.H?t[e+"Attribute"](n,c):t[e+"AttributeNS"](null,n,c)}function Q(t,e){if(!Reflect.has(t,e))return!1;let n=H(t,e);return!x(n.set)}function H(t,e){if(t=Object.getPrototypeOf(t),!t)return{};let n=Object.getOwnPropertyDescriptor(t,e);return n||H(t,e)}function L(t,e,n){if(!(typeof e!="object"||e===null))return Object.entries(e).forEach(function([r,o]){r&&(o=t.processReactiveAttribute(e,r,o,n),n(r,o))})}function z(t){return Array.isArray(t)?t.filter(Boolean).join(" "):t}function X(t,e,n,c){return t[(x(c)?"remove":"set")+e](n,z(c))}function Y(t,e,n,c,r=null){return t[(x(c)?"remove":"set")+e+"NS"](r,n,z(c))}function F(t,e,n){if(Reflect.set(t,e,n),!!x(n))return Reflect.deleteProperty(t,e)}function gt(t,e,n=tt){E.push({scope:t,host:(...r)=>r.length?r.forEach(o=>o(t)):t,custom_element:t}),typeof n=="function"&&(n=n.call(t,t));let c=e.call(t,n);return E.pop(),c}function vt(t){return T(t.prototype,"connectedCallback",function(e,n,c){e.apply(n,c),n.dispatchEvent(new Event("dde:connected"))}),t.prototype[g]||(t.prototype[g]="dde"),T(t.prototype,"disconnectedCallback",function(e,n,c){e.apply(n,c);let r=()=>n.dispatchEvent(new Event("dde:disconnected"));if(n[g]!=="dde")return r();(queueMicrotask||setTimeout)(()=>!n.isConnected&&r())}),T(t.prototype,"attributeChangedCallback",function(e,n,c){let[r,,o]=c;n.dispatchEvent(new CustomEvent("dde:attributeChanged",{detail:[r,o]})),e.apply(n,c)}),t.prototype[y]=!0,t}function T(t,e,n){t[e]=new Proxy(t[e]||(()=>{}),{apply:n})}function tt(t){return W(t,(e,n)=>e.getAttribute(n))}function mt(t,e,n){return e||(e={}),function(r,...o){n&&(o.unshift(r),r=typeof n=="function"?n():n);let i=o.length?new CustomEvent(t,Object.assign({detail:o[0]},e)):new Event(t,e);return r.dispatchEvent(i)}}function C(t,e,n){return function(r){return r.addEventListener(t,e,n),r}}var N=a.M?nt():new Proxy({},{get(){return()=>{}}}),et=new WeakSet;C.connected=function(t,e){let{custom_element:n}=E.current,c="connected";return typeof e!="object"&&(e={}),typeof e.once!="boolean"&&(e.once=!0),function(o){n&&(o=n);let i="dde:"+c;return o.addEventListener(i,t,e),o[y]?o:o.isConnected?(o.dispatchEvent(new Event(i)),o):(_(e.signal,()=>N.offConnected(o,t))&&N.onConnected(o,t),o)}};C.disconnected=function(t,e){let{custom_element:n}=E.current,c="disconnected";return typeof e!="object"&&(e={}),typeof e.once!="boolean"&&(e.once=!0),function(o){n&&(o=n),o[g]||(o[g]="dde");let i="dde:"+c;return o.addEventListener(i,t,e),o[y]||_(e.signal,()=>N.offDisconnected(o,t))&&N.onDisconnected(o,t),o}};var k=new WeakMap;C.disconnectedAsAbort=function(t){if(k.has(t))return k.get(t);let e=new AbortController;return k.set(t,e),t(C.disconnected(()=>e.abort())),e};C.attributeChanged=function(t,e){let n="attributeChanged";return typeof e!="object"&&(e={}),function(r){let o="dde:"+n;if(r.addEventListener(o,t,e),r[y]||et.has(r)||!a.M)return r;let i=new a.M(function(p){for(let{attributeName:h,target:l}of p)l.dispatchEvent(new CustomEvent(o,{detail:[h,l.getAttribute(h)]}))});return _(e.signal,()=>i.disconnect())&&i.observe(r,{attributes:!0}),r}};function nt(){let t=new Map,e=!1,n=new a.M(function(s){for(let u of s)if(u.type==="childList"){if(h(u.addedNodes,!0)){i();continue}l(u.removedNodes,!0)&&i()}});return{onConnected(s,u){o();let f=r(s);f.connected.has(u)||(f.connected.add(u),f.length_c+=1)},offConnected(s,u){if(!t.has(s))return;let f=t.get(s);f.connected.has(u)&&(f.connected.delete(u),f.length_c-=1,c(s,f))},onDisconnected(s,u){o();let f=r(s);f.disconnected.has(u)||(f.disconnected.add(u),f.length_d+=1)},offDisconnected(s,u){if(!t.has(s))return;let f=t.get(s);f.disconnected.has(u)&&(f.disconnected.delete(u),f.length_d-=1,c(s,f))}};function c(s,u){u.length_c||u.length_d||(t.delete(s),i())}function r(s){if(t.has(s))return t.get(s);let u={connected:new WeakSet,length_c:0,disconnected:new WeakSet,length_d:0};return t.set(s,u),u}function o(){e||(e=!0,n.observe(a.D.body,{childList:!0,subtree:!0}))}function i(){!e||t.size||(e=!1,n.disconnect())}function d(){return new Promise(function(s){(requestIdleCallback||requestAnimationFrame)(s)})}async function p(s){t.size>30&&await d();let u=[];if(!(s instanceof Node))return u;for(let f of t.keys())f===s||!(f instanceof Node)||s.contains(f)&&u.push(f);return u}function h(s,u){let f=!1;for(let b of s){if(u&&p(b).then(h),!t.has(b))continue;let m=t.get(b);m.length_c&&(b.dispatchEvent(new Event("dde:connected")),m.connected=new WeakSet,m.length_c=0,m.length_d||t.delete(b),f=!0)}return f}function l(s,u){let f=!1;for(let b of s){if(u&&p(b).then(l),!t.has(b)||!t.get(b).length_d)continue;let P=D(b);b[g]==="dde"?(queueMicrotask||setTimeout)(P):P(),f=!0}return f}function D(s){return()=>{s.isConnected||(s.dispatchEvent(new Event("dde:disconnected")),t.delete(s))}}}export{w as assign,$ as assignAttribute,G as chainableAppend,J as classListDeclarative,M as createElement,at as createElementNS,gt as customElementRender,vt as customElementWithDDE,mt as dispatchEvent,M as el,at as elNS,K as elementAttribute,dt as empty,vt as lifecycleToEvents,tt as observedAttributes,C as on,B as registerReactivity,E as scope,ft as simulateSlots}; diff --git a/index.d.ts b/index.d.ts index e99ce12..51677c1 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,16 +1,11 @@ import { Observable } from "./observables"; type CustomElementTagNameMap= { '#text': Text, '#comment': Comment } -declare global { - interface ddePublicElementTagNameMap{ - } -} type SupportedElement= HTMLElementTagNameMap[keyof HTMLElementTagNameMap] | SVGElementTagNameMap[keyof SVGElementTagNameMap] | MathMLElementTagNameMap[keyof MathMLElementTagNameMap] | CustomElementTagNameMap[keyof CustomElementTagNameMap] - | ddePublicElementTagNameMap[keyof ddePublicElementTagNameMap]; declare global { type ddeComponentAttributes= Record | undefined; type ddeElementAddon= (element: El)=> El | void; @@ -48,7 +43,7 @@ export function classListDeclarative(element: El, c 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 ExtendedHTMLElementTagNameMap= HTMLElementTagNameMap & CustomElementTagNameMap; type textContent= string | ( (set?: string)=> string ); // TODO: for some reason `Observable` leads to `attrs?: any` export function el< TAG extends keyof ExtendedHTMLElementTagNameMap & string, @@ -187,9 +182,10 @@ export function customElementWithDDE(custom_element: EL) export function lifecycleToEvents(custom_element: EL): EL export function observedAttributes(custom_element: HTMLElement): Record -/* TypeScript MEH // TODO for SVG */ -type ddeAppend= (...nodes: (Node | string)[])=> el; +/* TypeScript MEH */ declare global{ + type ddeAppend= (...nodes: (Node | string)[])=> el; + interface ddeDocumentFragment extends DocumentFragment{ append: ddeAppend; } interface ddeHTMLElement extends HTMLElement{ append: ddeAppend; } interface ddeSVGElement extends SVGElement{ append: ddeAppend; } diff --git a/package.json b/package.json index 0ae939c..0cb953f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "deka-dom-el", - "version": "0.7.7", + "version": "0.7.8", "description": "A low-code library that simplifies the creation of native DOM elements/components using small wrappers and tweaks.", "author": "Jan Andrle ", "license": "MIT", @@ -59,18 +59,24 @@ "size-limit": [ { "path": "./index.js", - "limit": "10 kB", + "limit": "10.5 kB", "gzip": false, "brotli": false }, { "path": "./observables.js", - "limit": "11.5 kB", + "limit": "12 kB", "gzip": false, "brotli": false }, + { + "path": "./index-with-observables.js", + "limit": "15 kB", + "gzip": false, + "brotli": false + }, { "path": "./index-with-observables.js", "limit": "5 kB" diff --git a/src/customElement.js b/src/customElement.js index 1267c73..f5b303e 100644 --- a/src/customElement.js +++ b/src/customElement.js @@ -1,3 +1,4 @@ +import { keyDM, keyLTE } from "./dom-common.js"; import { scope } from "./dom.js"; export function customElementRender(custom_element, render, props= observedAttributes){ scope.push({ @@ -11,20 +12,27 @@ export function customElementRender(custom_element, render, props= observedAttri 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){ + wrapMethod(class_declaration.prototype, "connectedCallback", function(target, thisArg, detail){ + target.apply(thisArg, detail); + thisArg.dispatchEvent(new Event("dde:connected")); + }); + if(!class_declaration.prototype[keyDM]) + class_declaration.prototype[keyDM]= "dde"; + wrapMethod(class_declaration.prototype, "disconnectedCallback", function(target, thisArg, detail){ + target.apply(thisArg, detail); + const dispatch= ()=> thisArg.dispatchEvent(new Event("dde:disconnected")); + if(thisArg[keyDM]!=="dde") + return dispatch(); + (queueMicrotask || setTimeout)(()=> !thisArg.isConnected && dispatch()); + }); + wrapMethod(class_declaration.prototype, "attributeChangedCallback", function(target, thisArg, detail){ const [ attribute, , value ]= detail; - thisArg.dispatchEvent(new CustomEvent("dde:"+name, { + thisArg.dispatchEvent(new CustomEvent("dde:attributeChanged", { detail: [ attribute, value ] })); target.apply(thisArg, detail); }); - class_declaration.prototype.__dde_lifecycleToEvents= true; + class_declaration.prototype[keyLTE]= true; return class_declaration; } export { lifecycleToEvents as customElementWithDDE }; diff --git a/src/dom-common.js b/src/dom-common.js index fa7d2a0..16e08b4 100644 --- a/src/dom-common.js +++ b/src/dom-common.js @@ -26,3 +26,5 @@ function setDeleteAttr(obj, prop, val){ if(Reflect.get(obj, prop)==="undefined") return Reflect.set(obj, prop, ""); } +export const keyLTE= "__dde_lifecycleToEvents"; //boolean +export const keyDM= "__dde_disconnect_mode"; //native (unset) | dde | skip diff --git a/src/events.js b/src/events.js index 0d6cbd3..7e12965 100644 --- a/src/events.js +++ b/src/events.js @@ -1,5 +1,5 @@ export { registerReactivity } from './observables-common.js'; -import { enviroment as env } from './dom-common.js'; +import { enviroment as env, keyDM, keyLTE } from './dom-common.js'; export function dispatchEvent(name, options, host){ if(!options) options= {}; @@ -26,17 +26,19 @@ const els_attribute_store= new WeakSet(); import { scope } from "./dom.js"; import { onAbort } from './helpers.js'; //TODO: cleanUp when event before abort? +//TODO: docs (e.g.) https://nolanlawson.com/2024/01/13/web-component-gotcha-constructor-vs-connectedcallback/ on.connected= function(listener, options){ const { custom_element }= scope.current; const name= "connected"; if(typeof options !== "object") options= {}; - options.once= true; + if(typeof options.once !== "boolean") + options.once= true; return function registerElement(element){ if(custom_element) element= custom_element; const event= "dde:"+name; element.addEventListener(event, listener, options); - if(element.__dde_lifecycleToEvents) return element; + if(element[keyLTE]) return element; if(element.isConnected) return ( element.dispatchEvent(new Event(event)), element ); const c= onAbort(options.signal, ()=> c_ch_o.offConnected(element, listener)); @@ -49,12 +51,14 @@ on.disconnected= function(listener, options){ const name= "disconnected"; if(typeof options !== "object") options= {}; - options.once= true; + if(typeof options.once !== "boolean") + options.once= true; return function registerElement(element){ if(custom_element) element= custom_element; + if(!element[keyDM]) element[keyDM]= "dde"; const event= "dde:"+name; element.addEventListener(event, listener, options); - if(element.__dde_lifecycleToEvents) return element; + if(element[keyLTE]) return element; const c= onAbort(options.signal, ()=> c_ch_o.offDisconnected(element, listener)); if(c) c_ch_o.onDisconnected(element, listener); @@ -77,7 +81,7 @@ on.attributeChanged= function(listener, options){ return function registerElement(element){ const event= "dde:"+name; element.addEventListener(event, listener, options); - if(element.__dde_lifecycleToEvents || els_attribute_store.has(element)) + if(element[keyLTE] || els_attribute_store.has(element)) return element; if(!env.M) return element; @@ -171,13 +175,13 @@ function connectionsChangesObserverConstructor(){ function requestIdle(){ return new Promise(function(resolve){ (requestIdleCallback || requestAnimationFrame)(resolve); }); } - async function collectChildren(element, filter){ + async function collectChildren(element){ if(store.size > 30)//TODO limit? await requestIdle(); const out= []; if(!(element instanceof Node)) return out; for(const el of store.keys()){ - if(el===element || !(el instanceof Node) || filter(el)) continue; + if(el===element || !(el instanceof Node)) continue; if(element.contains(el)) out.push(el); } @@ -186,7 +190,7 @@ function connectionsChangesObserverConstructor(){ function observerAdded(addedNodes, is_root){ let out= false; for(const element of addedNodes){ - if(is_root) collectChildren(element, el=> !el.isConnectedd).then(observerAdded); + if(is_root) collectChildren(element).then(observerAdded); if(!store.has(element)) continue; const ls= store.get(element); @@ -203,17 +207,25 @@ function connectionsChangesObserverConstructor(){ function observerRemoved(removedNodes, is_root){ let out= false; for(const element of removedNodes){ - if(is_root) collectChildren(element, el=> el.isConnectedd).then(observerRemoved); + if(is_root) collectChildren(element).then(observerRemoved); if(!store.has(element)) continue; const ls= store.get(element); if(!ls.length_d) continue; - - element.dispatchEvent(new Event("dde:disconnected")); - - store.delete(element); + const dispatch= dispatchRemove(element); + if(element[keyDM]==="dde") + (queueMicrotask || setTimeout)(dispatch); + else + dispatch(); out= true; } return out; } + function dispatchRemove(element){ + return ()=> { + if(element.isConnected) return; + element.dispatchEvent(new Event("dde:disconnected")); + store.delete(element); + }; + } } diff --git a/src/observables-lib.js b/src/observables-lib.js index cc1d7fa..28b3618 100644 --- a/src/observables-lib.js +++ b/src/observables-lib.js @@ -161,7 +161,11 @@ export const observables_config= { isObservable, processReactiveAttribute(element, key, attrs, set){ if(!isObservable(attrs)) return attrs; - const l= attr=> set(key, attr); + const l= attr=> { + if(!element.isConnected) + return removeObservableListener(attrs, l); + set(key, attr); + }; addObservableListener(attrs, l); removeObservablesFromElements(attrs, l, element, key); return attrs();