mirror of
https://github.com/jaandrle/deka-dom-el
synced 2024-11-22 07:49:38 +01:00
5 lines
11 KiB
JavaScript
5 lines
11 KiB
JavaScript
var y={isSignal(t){return!1},processReactiveAttribute(t,e,n,r){return n}};function $(t,e=!0){return e?Object.assign(y,t):(Object.setPrototypeOf(t,y),t)}function R(t){return y.isPrototypeOf(t)&&t!==y?t:y}function x(t){return typeof t>"u"}function q(t){let e=typeof t;return e!=="object"?e:t===null?"null":Object.prototype.toString.call(t)}function C(t,e){if(!t||!(t instanceof AbortSignal))return!0;if(!t.aborted)return t.addEventListener("abort",e),function(){t.removeEventListener("abort",e)}}var M={setDeleteAttr:Y,ssr:""};function Y(t,e,n){if(Reflect.set(t,e,n),!!x(n)){if(Reflect.deleteProperty(t,e),t instanceof HTMLElement&&t.getAttribute(e)==="undefined")return t.removeAttribute(e);if(Reflect.get(t,e)==="undefined")return Reflect.set(t,e,"")}}var E=[{scope:document.body,host:t=>t?t(document.body):document.body,custom_element:!1,prevent:!0}],m={get current(){return E[E.length-1]},get host(){return this.current.host},preventDefault(){let{current:t}=this;return t.prevent=!0,t},get state(){return[...E]},push(t={}){return E.push(Object.assign({},this.current,{prevent:!1},t))},pushRoot(){return E.push(E[0])},pop(){if(E.length!==1)return E.pop()}};function B(...t){return this.appendOriginal(...t),this}function tt(t){return t.append===B||(t.appendOriginal=t.append,t.append=B),t}var N;function S(t,e,...n){let r=R(this),o=0,c,s;switch((Object(e)!==e||r.isSignal(e))&&(e={textContent:e}),!0){case typeof t=="function":{o=1,m.push({scope:t,host:(...p)=>p.length?(o===1?n.unshift(...p):p.forEach(v=>v(s)),void 0):s}),c=t(e||void 0);let a=c instanceof DocumentFragment;if(c.nodeName==="#comment")break;let d=S.mark({type:"component",name:t.name,host:a?"this":"parentElement"});c.prepend(d),a&&(s=d);break}case t==="#text":c=j.call(this,document.createTextNode(""),e);break;case(t==="<>"||!t):c=j.call(this,document.createDocumentFragment(),e);break;case!!N:c=j.call(this,document.createElementNS(N,t),e);break;case!c:c=j.call(this,document.createElement(t),e)}return tt(c),s||(s=c),n.forEach(a=>a(s)),o&&m.pop(),o=2,c}S.mark=function(t,e=!1){t=Object.entries(t).map(([o,c])=>o+`="${c}"`).join(" ");let n=e?"":"/",r=document.createComment(`<dde:mark ${t}${M.ssr}${n}>`);return e||(r.end=document.createComment("</dde:mark>")),r};function _t(t){let e=this;return function(...r){N=t;let o=S.call(e,...r);return N=void 0,o}}var{setDeleteAttr:I}=M,L=new WeakMap;function j(t,...e){if(!e.length)return t;L.set(t,G(t,this));for(let[n,r]of Object.entries(Object.assign({},...e)))Z.call(this,t,n,r);return L.delete(t),t}function Z(t,e,n){let{setRemoveAttr:r,s:o}=G(t,this),c=this;n=o.processReactiveAttribute(t,e,n,(a,d)=>Z.call(c,t,a,d));let[s]=e;if(s==="=")return r(e.slice(1),n);if(s===".")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 I(t,e,n);case"style":if(typeof n!="object")break;case"dataset":return T(o,n,J.bind(null,t[e]));case"ariaset":return T(o,n,(a,d)=>r("aria-"+a,d));case"classList":return et.call(c,t,n)}return nt(t,e)?I(t,e,n):r(e,n)}function G(t,e){if(L.has(t))return L.get(t);let r=(t instanceof SVGElement?ot:rt).bind(null,t,"Attribute"),o=R(e);return{setRemoveAttr:r,s:o}}function et(t,e){let n=R(this);return T(n,e,(r,o)=>t.classList.toggle(r,o===-1?void 0:!!o)),t}function Et(t){return Array.from(t.children).forEach(e=>e.remove()),t}function nt(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 T(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 V(t){return Array.isArray(t)?t.filter(Boolean).join(" "):t}function rt(t,e,n,r){return t[(x(r)?"remove":"set")+e](n,V(r))}function ot(t,e,n,r,o=null){return t[(x(r)?"remove":"set")+e+"NS"](o,n,V(r))}function J(t,e,n){if(Reflect.set(t,e,n),!!x(n))return Reflect.deleteProperty(t,e)}function wt(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 _(t,e,n){return function(o){return o.addEventListener(t,e,n),o}}var P=st(),ct=new WeakSet;_.connected=function(t,e){let{custom_element:n}=m.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):(C(e.signal,()=>P.offConnected(c,t))&&P.onConnected(c,t),c)}};_.disconnected=function(t,e){let{custom_element:n}=m.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||C(e.signal,()=>P.offDisconnected(c,t))&&P.onDisconnected(c,t),c}};var z=new WeakMap;_.disconnectedAsAbort=function(t){if(z.has(t))return z.get(t);let e=new AbortController;return z.set(t,e),t(_.disconnected(()=>e.abort())),e};_.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||ct.has(o))return o;let s=new MutationObserver(function(d){for(let{attributeName:p,target:v}of d)v.dispatchEvent(new CustomEvent(c,{detail:[p,v.getAttribute(p)]}))});return C(e.signal,()=>s.disconnect())&&s.observe(o,{attributes:!0}),o}};function st(){let t=new Map,e=!1,n=new MutationObserver(function(i){for(let f of i)if(f.type==="childList"){if(p(f.addedNodes,!0)){s();continue}v(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(document.body,{childList:!0,subtree:!0}))}function s(){!e||t.size||(e=!1,n.disconnect())}function a(){return new Promise(function(i){(requestIdleCallback||requestAnimationFrame)(i)})}async function d(i){t.size>30&&await a();let 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 p(i,f){let u=!1;for(let b of i){if(f&&d(b).then(p),!t.has(b))continue;let w=t.get(b);w.length_c&&(b.dispatchEvent(new Event("dde:connected")),w.connected=new WeakSet,w.length_c=0,w.length_d||t.delete(b),u=!0)}return u}function v(i,f){let u=!1;for(let b of i)f&&d(b).then(v),!(!t.has(b)||!t.get(b).length_d)&&(b.dispatchEvent(new Event("dde:disconnected")),t.delete(b),u=!0);return u}}var l=Symbol.for("Signal");function D(t){try{return Reflect.has(t,l)}catch{return!1}}var W=[],h=new WeakMap;function g(t,e){if(typeof t!="function")return K(t,e);if(D(t))return t;let n=K(),r=function(){let[o,...c]=h.get(r);if(h.set(r,new Set([o])),W.push(r),n(t()),W.pop(),!c.length)return;let s=h.get(r);for(let a of c)s.has(a)||O(a,r)};return h.set(n[l],r),h.set(r,new Set([n])),r(),n}g.action=function(t,e,...n){let r=t[l],{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))};g.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));U(e,n),o&&o.addEventListener("abort",()=>O(e,n))}};g.symbols={signal:l,onclear:Symbol.for("Signal.onclear")};g.clear=function(...t){for(let n of t){Reflect.deleteProperty(n,"toJSON");let r=n[l];r.onclear.forEach(o=>o.call(r)),e(n,r),Reflect.deleteProperty(n,l)}function e(n,r){r.listeners.forEach(o=>{if(r.listeners.delete(o),!h.has(o))return;let c=h.get(o);c.delete(n),!(c.size>1)&&(g.clear(...c),h.delete(o))})}};var k="__dde_reactive";g.el=function(t,e){let n=S.mark({type:"reactive"},!1),r=n.end,o=document.createDocumentFragment();o.append(n,r);let{current:c}=m,s=a=>{if(!n.parentNode||!r.parentNode)return O(t,s);m.push(c);let d=e(a);m.pop(),Array.isArray(d)||(d=[d]);let p=n;for(;(p=n.nextSibling)!==r;)p.remove();n.after(...d)};return U(t,s),X(t,s,n,e),s(t()),o};var A="__dde_attributes";g.fromAttribute=function(t,e,n){t[A]||(t[A]={},_.attributeChanged(function({detail:s}){/*! This maps attributes to signals (`S.attribute`).
|
|
* Investigate `__dde_attributes` key of the element.*/let[a,d]=s,p=t[A][a];if(p)return p(d)})(t),_.disconnected(function(){/*! This removes all signals mapped to attributes (`S.attribute`).
|
|
* Investigate `__dde_attributes` key of the element.*/g.clear(...Object.values(t[A]))})(t));let r=t[A],o=Reflect.has(r,e)?Reflect.get(r,e):r[e]=g(n);return new Proxy(o,{apply(c,s,a){if(!a.length)return c();let d=a[0];return t.setAttribute(e,d)}})};var Q={isSignal:D,processReactiveAttribute(t,e,n,r){if(!D(n))return n;let o=c=>r(e,c);return U(n,o),X(n,o,t,e),n()}};function X(t,e,...n){let{current:r}=m;r.prevent||r.host(function(o){o[k]||(o[k]=[],_.disconnected(()=>o[k].forEach(([[c,s]])=>O(c,s,c[l]?.host()===o)))(o)),o[k].push([[t,e],...n])})}function K(t,e){let n=(...r)=>r.length?dt(n,...r):at(n);return ft(n,t,e)}var it=Object.assign(Object.create(null),{stopPropagation(){this.skip=!0}}),F=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 ft(t,e,n){let r=[];q(n)!=="[object Object]"&&(n={});let{onclear:o}=g.symbols;n[o]&&(r.push(n[o]),Reflect.deleteProperty(n,o));let{host:c}=m;return Reflect.defineProperty(t,l,{value:{value:e,actions:n,onclear:r,host:c,listeners:new Set,defined:new F},enumerable:!1,writable:!1,configurable:!0}),t.toJSON=()=>t(),Object.setPrototypeOf(t[l],it),t}function ut(){return W[W.length-1]}function at(t){if(!t[l])return;let{value:e,listeners:n}=t[l],r=ut();return r&&n.add(r),h.has(r)&&h.get(r).add(t),e}function dt(t,e,n){if(!t[l])return;let r=t[l];if(!(!n&&r.value===e))return r.value=e,r.listeners.forEach(o=>o(e)),e}function U(t,e){if(t[l])return t[l].listeners.add(e)}function O(t,e,n){let r=t[l];if(!r)return;let o=r.listeners.delete(e);if(n&&!r.listeners.size){if(g.clear(t),!h.has(r))return o;let c=h.get(r);if(!h.has(c))return o;h.get(c).forEach(s=>O(s,c,!0))}return o}$(Q);export{g as S,j as assign,Z as assignAttribute,tt as chainableAppend,et as classListDeclarative,S as createElement,_t as createElementNS,wt as dispatchEvent,S as el,_t as elNS,Et as empty,D as isSignal,_ as on,$ as registerReactivity,m as scope};
|