mirror of
https://github.com/jaandrle/deka-dom-el
synced 2024-11-23 00:59:38 +01:00
2 lines
9.1 KiB
JavaScript
2 lines
9.1 KiB
JavaScript
var y={isObservable(t){return!1},processReactiveAttribute(t,e,n,r){return n}};function H(t,e=!0){return e?Object.assign(y,t):(Object.setPrototypeOf(t,y),t)}function D(t){return y.isPrototypeOf(t)&&t!==y?t:y}function E(t){return typeof t>"u"}function R(t,e){if(!t||!(t instanceof AbortSignal))return!0;if(!t.aborted)return t.addEventListener("abort",e),function(){t.removeEventListener("abort",e)}}function M(t,e){let{observedAttributes:n=[]}=t.constructor;return n.reduce(function(r,o){return Reflect.set(r,z(o),e(t,o)),r},{})}function z(t){return t.replace(/-./g,e=>e[1].toUpperCase())}var a={setDeleteAttr:B,ssr:"",D:globalThis.document,F:globalThis.DocumentFragment,H:globalThis.HTMLElement,S:globalThis.SVGElement,M:globalThis.MutationObserver};function B(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 x="__dde_lifecycleToEvents";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 k(...t){return this.appendOriginal(...t),this}function I(t){return t.append===k||(t.appendOriginal=t.append,t.append=k),t}var O;function L(t,e,...n){let r=D(this),o=0,c,i;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(l=>l(i)),void 0):i}),c=t(e||void 0);let d=c instanceof a.F;if(c.nodeName==="#comment")break;let p=L.mark({type:"component",name:t.name,host:d?"this":"parentElement"});c.prepend(p),d&&(i=p);break}case t==="#text":c=A.call(this,a.D.createTextNode(""),e);break;case(t==="<>"||!t):c=A.call(this,a.D.createDocumentFragment(),e);break;case!!O:c=A.call(this,a.D.createElementNS(O,t),e);break;case!c:c=A.call(this,a.D.createElement(t),e)}return I(c),i||(i=c),n.forEach(d=>d(i)),o&&v.pop(),o=2,c}function it(t,e=t,n=void 0){let r=Symbol.for("default"),o=Array.from(e.querySelectorAll("slot")).reduce((i,d)=>Reflect.set(i,d.name||r,d)&&i,{}),c=Reflect.has(o,r);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){c&&h.appendChild(l);continue}let C=l.slot,s=o[C];V(l,"remove","slot"),s&&(Z(s,l,n),Reflect.deleteProperty(o,C))}return c&&(o[r].replaceWith(h),Reflect.deleteProperty(o,r)),t.append=i,t}}),t!==e){let i=Array.from(t.childNodes);i.forEach(d=>d.remove()),t.append(...i)}return e}function Z(t,e,n){n&&n(t,e);try{t.replaceWith(A(e,{className:[e.className,t.className],dataset:{...t.dataset}}))}catch{t.replaceWith(e)}}L.mark=function(t,e=!1){t=Object.entries(t).map(([o,c])=>o+`="${c}"`).join(" ");let n=e?"":"/",r=a.D.createComment(`<dde:mark ${t}${a.ssr}${n}>`);return e&&(r.end=a.D.createComment("</dde:mark>")),r};function ut(t){let e=this;return function(...r){O=t;let o=L.call(e,...r);return O=void 0,o}}var _=new WeakMap,{setDeleteAttr:W}=a;function A(t,...e){if(!e.length)return t;_.set(t,F(t,this));for(let[n,r]of Object.entries(Object.assign({},...e)))q.call(this,t,n,r);return _.delete(t),t}function q(t,e,n){let{setRemoveAttr:r,s:o}=F(t,this),c=this;n=o.processReactiveAttribute(t,e,n,(d,p)=>q.call(c,t,d,p));let[i]=e;if(i==="=")return r(e.slice(1),n);if(i===".")return j(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 W(t,e,n);case"style":if(typeof n!="object")break;case"dataset":return N(o,n,j.bind(null,t[e]));case"ariaset":return N(o,n,(d,p)=>r("aria-"+d,p));case"classList":return G.call(c,t,n)}return J(t,e)?W(t,e,n):r(e,n)}function F(t,e){if(_.has(t))return _.get(t);let r=(t instanceof a.S?Q:K).bind(null,t,"Attribute"),o=D(e);return{setRemoveAttr:r,s:o}}function G(t,e){let n=D(this);return N(n,e,(r,o)=>t.classList.toggle(r,o===-1?void 0:!!o)),t}function ft(t){return Array.from(t.children).forEach(e=>e.remove()),t}function V(t,e,n,r){return t instanceof a.H?t[e+"Attribute"](n,r):t[e+"AttributeNS"](null,n,r)}function J(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 N(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 U(t){return Array.isArray(t)?t.filter(Boolean).join(" "):t}function K(t,e,n,r){return t[(E(r)?"remove":"set")+e](n,U(r))}function Q(t,e,n,r,o=null){return t[(E(r)?"remove":"set")+e+"NS"](o,n,U(r))}function j(t,e,n){if(Reflect.set(t,e,n),!!E(n))return Reflect.deleteProperty(t,e)}function ht(t,e,n=X){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 bt(t){return T(t.prototype,"connectedCallback",function(e,n,r){e.apply(n,r),n.dispatchEvent(new Event("dde:connected"))}),T(t.prototype,"disconnectedCallback",function(e,n,r){e.apply(n,r),(queueMicrotask||setTimeout)(()=>!n.isConnected&&n.dispatchEvent(new Event("dde:disconnected")))}),T(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[x]=!0,t}function T(t,e,n){t[e]=new Proxy(t[e]||(()=>{}),{apply:n})}function X(t){return M(t,(e,n)=>e.getAttribute(n))}function xt(t,e,n){return e||(e={}),function(o,...c){n&&(c.unshift(o),o=typeof n=="function"?n():n);let i=c.length?new CustomEvent(t,Object.assign({detail:c[0]},e)):new Event(t,e);return o.dispatchEvent(i)}}function w(t,e,n){return function(o){return o.addEventListener(t,e,n),o}}var S=a.M?tt():new Proxy({},{get(){return()=>{}}}),Y=new WeakSet;w.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 i="dde:"+r;return c.addEventListener(i,t,e),c[x]?c:c.isConnected?(c.dispatchEvent(new Event(i)),c):(R(e.signal,()=>S.offConnected(c,t))&&S.onConnected(c,t),c)}};w.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 i="dde:"+r;return c.addEventListener(i,t,e),c[x]||R(e.signal,()=>S.offDisconnected(c,t))&&S.onDisconnected(c,t),c}};var P=new WeakMap;w.disconnectedAsAbort=function(t){if(P.has(t))return P.get(t);let e=new AbortController;return P.set(t,e),t(w.disconnected(()=>e.abort())),e};w.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[x]||Y.has(o)||!a.M)return o;let i=new a.M(function(p){for(let{attributeName:h,target:l}of p)l.dispatchEvent(new CustomEvent(c,{detail:[h,l.getAttribute(h)]}))});return R(e.signal,()=>i.disconnect())&&i.observe(o,{attributes:!0}),o}};function tt(){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){c();let f=o(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,r(s,f))},onDisconnected(s,u){c();let f=o(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,r(s,f))}};function r(s,u){u.length_c||u.length_d||(t.delete(s),i())}function o(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 c(){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)u&&p(b).then(l),!(!t.has(b)||!t.get(b).length_d)&&((queueMicrotask||setTimeout)(C(b)),f=!0);return f}function C(s){return()=>{s.isConnected||(s.dispatchEvent(new Event("dde:disconnected")),t.delete(s))}}}export{A as assign,q as assignAttribute,I as chainableAppend,G as classListDeclarative,L as createElement,ut as createElementNS,ht as customElementRender,bt as customElementWithDDE,xt as dispatchEvent,L as el,ut as elNS,V as elementAttribute,ft as empty,bt as lifecycleToEvents,X as observedAttributes,w as on,H as registerReactivity,v as scope,it as simulateSlots};
|