mirror of
https://github.com/jaandrle/deka-dom-el
synced 2026-10-24 01:51:48 +02:00
🚧 mainly wc/ce
This commit is contained in:
7 files changed
+132
-82
No files matched your search
Vendored
+15
-15
@@ -1,24 +1,24 @@
|
|||||||
//deka-dom-el library is available via global namespace `dde`
|
//deka-dom-el library is available via global namespace `dde`
|
||||||
(()=> {
|
(()=> {
|
||||||
var w={isSignal(t){return!1},processReactiveAttribute(t,e,n,o){return n}};function T(t,e=!0){return e?Object.assign(w,t):(Object.setPrototypeOf(t,w),t)}function R(t){return w.isPrototypeOf(t)&&t!==w?t:w}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 W={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}],g={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 L;function y(t,e,...n){let o=R(this),r=0,c,s;switch((Object(e)!==e||o.isSignal(e))&&(e={textContent:e}),!0){case typeof t=="function":{r=1,g.push({scope:t,host:(...p)=>p.length?(r===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=y.mark({type:"component",name:t.name,host:a?"this":"parentElement"});c.prepend(d),a&&(s=d);break}case t==="#text":c=N.call(this,document.createTextNode(""),e);break;case(t==="<>"||!t):c=N.call(this,document.createDocumentFragment(),e);break;case!!L:c=N.call(this,document.createElementNS(L,t),e);break;case!c:c=N.call(this,document.createElement(t),e)}return tt(c),s||(s=c),n.forEach(a=>a(s)),r&&g.pop(),r=2,c}y.mark=function(t,e=!1){t=Object.entries(t).map(([r,c])=>r+`="${c}"`).join(" ");let n=e?"":"/",o=document.createComment(`<dde:mark ${t}${W.ssr}${n}>`);return e||(o.end=document.createComment("</dde:mark>")),o};function mt(t){let e=this;return function(...o){L=t;let r=y.call(e,...o);return L=void 0,r}}var{setDeleteAttr:H}=W,j=new WeakMap;function N(t,...e){if(!e.length)return t;j.set(t,Z(t,this));for(let[n,o]of Object.entries(Object.assign({},...e)))J.call(this,t,n,o);return j.delete(t),t}function J(t,e,n){let{setRemoveAttr:o,s:r}=Z(t,this),c=this;n=r.processReactiveAttribute(t,e,n,(a,d)=>J.call(c,t,a,d));let[s]=e;if(s==="=")return o(e.slice(1),n);if(s===".")return I(t,e.slice(1),n);if(/(aria|data)([A-Z])/.test(e))return e=e.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),o(e,n);switch(e==="className"&&(e="class"),e){case"xlink:href":return o(e,n,"http://www.w3.org/1999/xlink");case"textContent":return H(t,e,n);case"style":if(typeof n!="object")break;case"dataset":return $(r,n,I.bind(null,t[e]));case"ariaset":return $(r,n,(a,d)=>o("aria-"+a,d));case"classList":return et.call(c,t,n)}return nt(t,e)?H(t,e,n):o(e,n)}function Z(t,e){if(j.has(t))return j.get(t);let o=(t instanceof SVGElement?ot:rt).bind(null,t,"Attribute"),r=R(e);return{setRemoveAttr:o,s:r}}function et(t,e){let n=R(this);return $(n,e,(o,r)=>t.classList.toggle(o,r===-1?void 0:!!r)),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=G(t,e);return!x(n.set)}function G(t,e){if(t=Object.getPrototypeOf(t),!t)return{};let n=Object.getOwnPropertyDescriptor(t,e);return n||G(t,e)}function $(t,e,n){if(!(typeof e!="object"||e===null))return Object.entries(e).forEach(function([r,c]){r&&(c=t.processReactiveAttribute(e,r,c,n),n(r,c))})}function V(t){return Array.isArray(t)?t.filter(Boolean).join(" "):t}function rt(t,e,n,o){return t[(x(o)?"remove":"set")+e](n,V(o))}function ot(t,e,n,o,r=null){return t[(x(o)?"remove":"set")+e+"NS"](r,n,V(o))}function I(t,e,n){if(Reflect.set(t,e,n),!!x(n))return Reflect.deleteProperty(t,e)}function St(t,e,n){return e||(e={}),function(r,...c){n&&(c.unshift(r),r=typeof n=="function"?n():n);let s=c.length?new CustomEvent(t,Object.assign({detail:c[0]},e)):new Event(t,e);return r.dispatchEvent(s)}}function m(t,e,n){return function(r){return r.addEventListener(t,e,n),r}}var P=st(),ct=new WeakSet;m.connected=function(t,e){let{custom_element:n}=g.current,o="connected";return typeof e!="object"&&(e={}),e.once=!0,function(c){n&&(c=n);let s="dde:"+o;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)}};m.disconnected=function(t,e){let{custom_element:n}=g.current,o="disconnected";return typeof e!="object"&&(e={}),e.once=!0,function(c){n&&(c=n);let s="dde:"+o;return c.addEventListener(s,t,e),c.__dde_lifecycleToEvents||C(e.signal,()=>P.offDisconnected(c,t))&&P.onDisconnecLine truncated
|
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.onDisconnecLine truncated
|
||||||
* Investigate `__dde_attributes` key of the element.*/let[a,d]=s,p=r[A][a];if(p)return p(d)})(r),m.disconnected(function(){/*! This removes all signals mapped to attributes (`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.*/b.clear(...Object.values(r[A])),o=null})(r),r}),new Proxy(n,{apply(r,c,s){if(!s.length)return r();if(!o)return;let a=s[0];return o instanceof HTMLElement?o.setAttribute(t,a):o.setAttributeNS(null,t,a)}})};b.clear=function(...t){for(let n of t){Reflect.deleteProperty(n,"toJSON");let o=n[l];o.onclear.forEach(r=>r.call(o)),e(n,o),Reflect.deleteProperty(n,l)}function e(n,o){o.listeners.forEach(r=>{if(o.listeners.delete(r),!h.has(r))return;let c=h.get(r);c.delete(n),!(c.size>1)&&(b.clear(...c),h.delete(r))})}};var k="__dde_reactive";b.el=function(t,e){let n=y.mark({type:"reactive"},!1),o=n.end,r=document.createDocumentFragment();r.append(n,o);let{current:c}=g,s=a=>{if(!n.parentNode||!o.parentNode)return O(t,s);g.push(c);let d=e(a);g.pop(),Array.isArray(d)||(d=[d]);let p=n;for(;(p=n.nextSibling)!==o;)p.remove();n.after(...d)};return U(t,s),X(t,s,n,e),s(t()),r};var Q={isSignal:D,processReactiveAttribute(t,e,n,o){if(!D(n))return n;let r=c=>o(e,c);return U(n,r),X(n,r,t,e),n()}};function X(t,e,...n){let{current:o}=g;o.prevent||o.host(function(r){r[k]||(r[k]=[],m.disconnected(()=>r[k].forEach(([[c,s]])=>O(c,s,c[l]?.host()===r)))(r)),r[k].push([[t,e],...n])})}function K(t,e){let n=(...o)=>o.length?dt(n,...o):at(n);return ut(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(`
|
* 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(`
|
||||||
`),o=e.slice(e.indexOf("@"),e.indexOf(".js:")+4);this.stack=n.find(r=>!r.includes(o))}};function ut(t,e,n){let o=[];q(n)!=="[object Object]"&&(n={});let{onclear:r}=b.symbols;n[r]&&(o.push(n[r]),Reflect.deleteProperty(n,r));let{host:c}=g;return Reflect.defineProperty(t,l,{value:{value:e,actions:n,onclear:o,host:c,listeners:new Set,defined:new F},enumerable:!1,writable:!1,configurable:!0}),t.toJSON=()=>t(),Object.setPrototypeOf(t[l],it),t}function ft(){return M[M.length-1]}function at(t){if(!t[l])return;let{value:e,listeners:n}=t[l],o=ft();return o&&n.add(o),h.has(o)&&h.get(o).add(t),e}function dt(t,e,n){if(!t[l])return;let o=t[l];if(!(!n&&o.value===e))return o.value=e,o.listeners.forEach(r=>r(e)),e}function U(t,e){if(t[l])return t[l].listeners.add(e)}function O(t,e,n){let o=t[l];if(!o)return;let r=o.listeners.delete(e);if(n&&!o.listeners.size){if(b.clear(t),!h.has(o))return r;let c=h.get(o);if(!h.has(c))return r;h.get(c).forEach(s=>O(s,c,!0))}return r}T(Q);
|
`),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);
|
||||||
globalThis.dde= {S: b,
|
globalThis.dde= {S: g,
|
||||||
assign: N,
|
assign: j,
|
||||||
assignAttribute: J,
|
assignAttribute: Z,
|
||||||
chainableAppend: tt,
|
chainableAppend: tt,
|
||||||
classListDeclarative: et,
|
classListDeclarative: et,
|
||||||
createElement: y,
|
createElement: S,
|
||||||
createElementNS: mt,
|
createElementNS: _t,
|
||||||
dispatchEvent: St,
|
dispatchEvent: wt,
|
||||||
el: y,
|
el: S,
|
||||||
elNS: mt,
|
elNS: _t,
|
||||||
empty: Et,
|
empty: Et,
|
||||||
isSignal: D,
|
isSignal: D,
|
||||||
on: m,
|
on: _,
|
||||||
registerReactivity: T,
|
registerReactivity: $,
|
||||||
scope: g
|
scope: m
|
||||||
};
|
};
|
||||||
|
|
||||||
})();
|
})();
|
||||||
Vendored
+29
-2
@@ -166,6 +166,34 @@ export const scope: {
|
|||||||
pop(): ReturnType<Array<Scope>["pop"]>,
|
pop(): ReturnType<Array<Scope>["pop"]>,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* TODO TypeScript HACK (better way?)
|
||||||
|
* this doesnt works
|
||||||
|
* ```ts
|
||||||
|
* interface element<el> extends Node{
|
||||||
|
* prototype: el;
|
||||||
|
* append(...els: (SupportedElement | DocumentFragment | string | element<SupportedElement | DocumentFragment>)[]): el
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
export function el<T>(
|
||||||
|
* tag_name?: "<>",
|
||||||
|
* ): element<DocumentFragment>
|
||||||
|
* ```
|
||||||
|
* …as its complains here
|
||||||
|
* ```
|
||||||
|
ts
|
||||||
|
*
|
||||||
|
const d= el("div");
|
||||||
|
*
|
||||||
|
const f= (a: HTMLDivElement)=> a;
|
||||||
|
* f(d);
|
||||||
|
//←
|
||||||
|
* document.head.append( //←
|
||||||
|
* el("script", { src: "https://flems.io/flems.html", type: "text/javascript", charset: "utf-8" }),
|
||||||
|
* );
|
||||||
|
* ```
|
||||||
|
* TODO for SVG
|
||||||
|
* */
|
||||||
type ddeAppend<el>= (...nodes: (Node | string)[])=> el;
|
type ddeAppend<el>= (...nodes: (Node | string)[])=> el;
|
||||||
declare global{
|
declare global{
|
||||||
interface HTMLAnchorElement{
|
interface HTMLAnchorElement{
|
||||||
@@ -493,8 +521,7 @@ interface S {
|
|||||||
* */
|
* */
|
||||||
el<S extends any>(signal: Signal<S, any>, el: (v: S)=> Element | Element[]): DocumentFragment;
|
el<S extends any>(signal: Signal<S, any>, el: (v: S)=> Element | Element[]): DocumentFragment;
|
||||||
|
|
||||||
/** Mirrors element attributes for current host (both way). */
|
fromAttribute<T>(element: HTMLElement, name: string, value?: T): Signal<T, {}>;
|
||||||
attribute<T>(name: string, initial?: T): Signal<T, {}>
|
|
||||||
}
|
}
|
||||||
export const S: S;
|
export const S: S;
|
||||||
declare global {
|
declare global {
|
||||||
|
|||||||
Vendored
+4
-4
@@ -1,4 +1,4 @@
|
|||||||
var w={isSignal(t){return!1},processReactiveAttribute(t,e,n,o){return n}};function T(t,e=!0){return e?Object.assign(w,t):(Object.setPrototypeOf(t,w),t)}function R(t){return w.isPrototypeOf(t)&&t!==w?t:w}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 W={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}],g={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 L;function y(t,e,...n){let o=R(this),r=0,c,s;switch((Object(e)!==e||o.isSignal(e))&&(e={textContent:e}),!0){case typeof t=="function":{r=1,g.push({scope:t,host:(...p)=>p.length?(r===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=y.mark({type:"component",name:t.name,host:a?"this":"parentElement"});c.prepend(d),a&&(s=d);break}case t==="#text":c=N.call(this,document.createTextNode(""),e);break;case(t==="<>"||!t):c=N.call(this,document.createDocumentFragment(),e);break;case!!L:c=N.call(this,document.createElementNS(L,t),e);break;case!c:c=N.call(this,document.createElement(t),e)}return tt(c),s||(s=c),n.forEach(a=>a(s)),r&&g.pop(),r=2,c}y.mark=function(t,e=!1){t=Object.entries(t).map(([r,c])=>r+`="${c}"`).join(" ");let n=e?"":"/",o=document.createComment(`<dde:mark ${t}${W.ssr}${n}>`);return e||(o.end=document.createComment("</dde:mark>")),o};function mt(t){let e=this;return function(...o){L=t;let r=y.call(e,...o);return L=void 0,r}}var{setDeleteAttr:H}=W,j=new WeakMap;function N(t,...e){if(!e.length)return t;j.set(t,Z(t,this));for(let[n,o]of Object.entries(Object.assign({},...e)))J.call(this,t,n,o);return j.delete(t),t}function J(t,e,n){let{setRemoveAttr:o,s:r}=Z(t,this),c=this;n=r.processReactiveAttribute(t,e,n,(a,d)=>J.call(c,t,a,d));let[s]=e;if(s==="=")return o(e.slice(1),n);if(s===".")return I(t,e.slice(1),n);if(/(aria|data)([A-Z])/.test(e))return e=e.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),o(e,n);switch(e==="className"&&(e="class"),e){case"xlink:href":return o(e,n,"http://www.w3.org/1999/xlink");case"textContent":return H(t,e,n);case"style":if(typeof n!="object")break;case"dataset":return $(r,n,I.bind(null,t[e]));case"ariaset":return $(r,n,(a,d)=>o("aria-"+a,d));case"classList":return et.call(c,t,n)}return nt(t,e)?H(t,e,n):o(e,n)}function Z(t,e){if(j.has(t))return j.get(t);let o=(t instanceof SVGElement?ot:rt).bind(null,t,"Attribute"),r=R(e);return{setRemoveAttr:o,s:r}}function et(t,e){let n=R(this);return $(n,e,(o,r)=>t.classList.toggle(o,r===-1?void 0:!!r)),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=G(t,e);return!x(n.set)}function G(t,e){if(t=Object.getPrototypeOf(t),!t)return{};let n=Object.getOwnPropertyDescriptor(t,e);return n||G(t,e)}function $(t,e,n){if(!(typeof e!="object"||e===null))return Object.entries(e).forEach(function([r,c]){r&&(c=t.processReactiveAttribute(e,r,c,n),n(r,c))})}function V(t){return Array.isArray(t)?t.filter(Boolean).join(" "):t}function rt(t,e,n,o){return t[(x(o)?"remove":"set")+e](n,V(o))}function ot(t,e,n,o,r=null){return t[(x(o)?"remove":"set")+e+"NS"](r,n,V(o))}function I(t,e,n){if(Reflect.set(t,e,n),!!x(n))return Reflect.deleteProperty(t,e)}function St(t,e,n){return e||(e={}),function(r,...c){n&&(c.unshift(r),r=typeof n=="function"?n():n);let s=c.length?new CustomEvent(t,Object.assign({detail:c[0]},e)):new Event(t,e);return r.dispatchEvent(s)}}function m(t,e,n){return function(r){return r.addEventListener(t,e,n),r}}var P=st(),ct=new WeakSet;m.connected=function(t,e){let{custom_element:n}=g.current,o="connected";return typeof e!="object"&&(e={}),e.once=!0,function(c){n&&(c=n);let s="dde:"+o;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)}};m.disconnected=function(t,e){let{custom_element:n}=g.current,o="disconnected";return typeof e!="object"&&(e={}),e.once=!0,function(c){n&&(c=n);let s="dde:"+o;return c.addEventListener(s,t,e),c.__dde_lifecycleToEvents||C(e.signal,()=>P.offDisconnected(c,t))&&P.onDisconnecLine truncated
|
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.onDisconnecLine truncated
|
||||||
* Investigate `__dde_attributes` key of the element.*/let[a,d]=s,p=r[A][a];if(p)return p(d)})(r),m.disconnected(function(){/*! This removes all signals mapped to attributes (`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.*/b.clear(...Object.values(r[A])),o=null})(r),r}),new Proxy(n,{apply(r,c,s){if(!s.length)return r();if(!o)return;let a=s[0];return o instanceof HTMLElement?o.setAttribute(t,a):o.setAttributeNS(null,t,a)}})};b.clear=function(...t){for(let n of t){Reflect.deleteProperty(n,"toJSON");let o=n[l];o.onclear.forEach(r=>r.call(o)),e(n,o),Reflect.deleteProperty(n,l)}function e(n,o){o.listeners.forEach(r=>{if(o.listeners.delete(r),!h.has(r))return;let c=h.get(r);c.delete(n),!(c.size>1)&&(b.clear(...c),h.delete(r))})}};var k="__dde_reactive";b.el=function(t,e){let n=y.mark({type:"reactive"},!1),o=n.end,r=document.createDocumentFragment();r.append(n,o);let{current:c}=g,s=a=>{if(!n.parentNode||!o.parentNode)return O(t,s);g.push(c);let d=e(a);g.pop(),Array.isArray(d)||(d=[d]);let p=n;for(;(p=n.nextSibling)!==o;)p.remove();n.after(...d)};return U(t,s),X(t,s,n,e),s(t()),r};var Q={isSignal:D,processReactiveAttribute(t,e,n,o){if(!D(n))return n;let r=c=>o(e,c);return U(n,r),X(n,r,t,e),n()}};function X(t,e,...n){let{current:o}=g;o.prevent||o.host(function(r){r[k]||(r[k]=[],m.disconnected(()=>r[k].forEach(([[c,s]])=>O(c,s,c[l]?.host()===r)))(r)),r[k].push([[t,e],...n])})}function K(t,e){let n=(...o)=>o.length?dt(n,...o):at(n);return ut(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(`
|
* 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(`
|
||||||
`),o=e.slice(e.indexOf("@"),e.indexOf(".js:")+4);this.stack=n.find(r=>!r.includes(o))}};function ut(t,e,n){let o=[];q(n)!=="[object Object]"&&(n={});let{onclear:r}=b.symbols;n[r]&&(o.push(n[r]),Reflect.deleteProperty(n,r));let{host:c}=g;return Reflect.defineProperty(t,l,{value:{value:e,actions:n,onclear:o,host:c,listeners:new Set,defined:new F},enumerable:!1,writable:!1,configurable:!0}),t.toJSON=()=>t(),Object.setPrototypeOf(t[l],it),t}function ft(){return M[M.length-1]}function at(t){if(!t[l])return;let{value:e,listeners:n}=t[l],o=ft();return o&&n.add(o),h.has(o)&&h.get(o).add(t),e}function dt(t,e,n){if(!t[l])return;let o=t[l];if(!(!n&&o.value===e))return o.value=e,o.listeners.forEach(r=>r(e)),e}function U(t,e){if(t[l])return t[l].listeners.add(e)}function O(t,e,n){let o=t[l];if(!o)return;let r=o.listeners.delete(e);if(n&&!o.listeners.size){if(b.clear(t),!h.has(o))return r;let c=h.get(o);if(!h.has(c))return r;h.get(c).forEach(s=>O(s,c,!0))}return r}T(Q);export{b as S,N as assign,J as assignAttribute,tt as chainableAppend,et as classListDeclarative,y as createElement,mt as createElementNS,St as dispatchEvent,y as el,mt as elNS,Et as empty,D as isSignal,m as on,T as registerReactivity,g as scope};
|
`),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};
|
||||||
Vendored
+28
@@ -166,6 +166,34 @@ export const scope: {
|
|||||||
pop(): ReturnType<Array<Scope>["pop"]>,
|
pop(): ReturnType<Array<Scope>["pop"]>,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* TODO TypeScript HACK (better way?)
|
||||||
|
* this doesnt works
|
||||||
|
* ```ts
|
||||||
|
* interface element<el> extends Node{
|
||||||
|
* prototype: el;
|
||||||
|
* append(...els: (SupportedElement | DocumentFragment | string | element<SupportedElement | DocumentFragment>)[]): el
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
export function el<T>(
|
||||||
|
* tag_name?: "<>",
|
||||||
|
* ): element<DocumentFragment>
|
||||||
|
* ```
|
||||||
|
* …as its complains here
|
||||||
|
* ```
|
||||||
|
ts
|
||||||
|
*
|
||||||
|
const d= el("div");
|
||||||
|
*
|
||||||
|
const f= (a: HTMLDivElement)=> a;
|
||||||
|
* f(d);
|
||||||
|
//←
|
||||||
|
* document.head.append( //←
|
||||||
|
* el("script", { src: "https://flems.io/flems.html", type: "text/javascript", charset: "utf-8" }),
|
||||||
|
* );
|
||||||
|
* ```
|
||||||
|
* TODO for SVG
|
||||||
|
* */
|
||||||
type ddeAppend<el>= (...nodes: (Node | string)[])=> el;
|
type ddeAppend<el>= (...nodes: (Node | string)[])=> el;
|
||||||
declare global{
|
declare global{
|
||||||
interface HTMLAnchorElement{
|
interface HTMLAnchorElement{
|
||||||
|
|||||||
@@ -1,14 +1,9 @@
|
|||||||
import { el, on, scope } from "../../index.js";
|
import { el, on, scope } from "../../index.js";
|
||||||
import { S } from "../../signals.js";
|
import { S } from "../../signals.js";
|
||||||
const { hasOwnProperty }= Object.prototype;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef CustomHTMLTestElementInterface
|
* Compatible with `npx -y web-component-analyzer examples/components/webComponent.js`
|
||||||
* @type {object}
|
* @element custom-test
|
||||||
* @property {string} name
|
|
||||||
* */
|
|
||||||
/**
|
|
||||||
* Compatible with `npx-wca test/components/webComponent.js`
|
|
||||||
* */
|
* */
|
||||||
export class CustomHTMLTestElement extends HTMLElement{
|
export class CustomHTMLTestElement extends HTMLElement{
|
||||||
static tagName= "custom-test";
|
static tagName= "custom-test";
|
||||||
@@ -16,32 +11,34 @@ export class CustomHTMLTestElement extends HTMLElement{
|
|||||||
return [ "name", "pre-name" ];
|
return [ "name", "pre-name" ];
|
||||||
}
|
}
|
||||||
connectedCallback(){
|
connectedCallback(){
|
||||||
|
if(!this.hasAttribute("pre-name")) this.setAttribute("pre-name", "default");
|
||||||
this.attachShadow({ mode: "open" }).append(
|
this.attachShadow({ mode: "open" }).append(
|
||||||
customElementRender(this, this.render)
|
customElementRender(this, this.render, observedAttributes(S))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
render({ test }){
|
render({ name, preName }){
|
||||||
|
const { host }= scope;
|
||||||
|
const { test }= host();
|
||||||
console.log(scope.state);
|
console.log(scope.state);
|
||||||
scope.host(
|
host(
|
||||||
on.connected(()=> console.log(CustomHTMLTestElement)),
|
on.connected(()=> console.log(CustomHTMLTestElement)),
|
||||||
on.attributeChanged(e=> console.log(e)),
|
on.attributeChanged(e=> console.log(e)),
|
||||||
on.disconnected(()=> console.log(CustomHTMLTestElement))
|
on.disconnected(()=> console.log(CustomHTMLTestElement))
|
||||||
);
|
);
|
||||||
|
|
||||||
const name= S.attribute("name");
|
|
||||||
const preName= S.attribute("pre-name");
|
|
||||||
console.log({ name, test, preName});
|
console.log({ name, test, preName});
|
||||||
return el("p").append(
|
return el("p").append(
|
||||||
el("#text", name),
|
el("#text", name),
|
||||||
el("#text", test),
|
|
||||||
el("#text", preName),
|
el("#text", preName),
|
||||||
el("button", { type: "button", textContent: "pre-name", onclick: ()=> preName("Ahoj") })
|
el("button", { type: "button", textContent: "pre-name", onclick: ()=> preName("Ahoj") })
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
test= "A";
|
||||||
|
|
||||||
get name(){ return this.getAttribute("name"); }
|
get name(){ return this.getAttribute("name"); }
|
||||||
set name(value){ this.setAttribute("name", value); }
|
set name(value){ this.setAttribute("name", value); }
|
||||||
|
/** @attr pre-name */
|
||||||
get preName(){ return this.getAttribute("pre-name"); }
|
get preName(){ return this.getAttribute("pre-name"); }
|
||||||
set preName(value){ this.setAttribute("pre-name", value); }
|
set preName(value){ this.setAttribute("pre-name", value); }
|
||||||
}
|
}
|
||||||
@@ -49,12 +46,25 @@ export class CustomHTMLTestElement extends HTMLElement{
|
|||||||
lifecycleToEvents(CustomHTMLTestElement)
|
lifecycleToEvents(CustomHTMLTestElement)
|
||||||
customElements.define(CustomHTMLTestElement.tagName, CustomHTMLTestElement);
|
customElements.define(CustomHTMLTestElement.tagName, CustomHTMLTestElement);
|
||||||
|
|
||||||
function customElementRender(_this, render){
|
function customElementRender(_this, render, props){
|
||||||
|
console.log(_this.shadowRoot, _this.childList);
|
||||||
scope.push({ scope: _this, host: (...c)=> c.length ? c.forEach(c=> c(_this)) : _this, custom_element: _this });
|
scope.push({ scope: _this, host: (...c)=> c.length ? c.forEach(c=> c(_this)) : _this, custom_element: _this });
|
||||||
const out= render(_this);
|
if(typeof props==="function") props= props(_this);
|
||||||
|
const out= render(props);
|
||||||
scope.pop();
|
scope.pop();
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
function observedAttributes(options, element){
|
||||||
|
const fromAttribute= typeof options==="undefined" ? (_1, _2, v)=> v : ( options.fromAttribute || options );
|
||||||
|
if(!element) return observedAttributes.bind(null, fromAttribute)
|
||||||
|
return Object.fromEntries(
|
||||||
|
element.constructor.observedAttributes
|
||||||
|
.map(name=> [
|
||||||
|
name.replace(/-./g, m=> m.slice(1).toUpperCase()),
|
||||||
|
fromAttribute(element, name, element.getAttribute(name))
|
||||||
|
])
|
||||||
|
);
|
||||||
|
}
|
||||||
function lifecycleToEvents(class_declaration){
|
function lifecycleToEvents(class_declaration){
|
||||||
for (const name of [ "connected", "disconnected" ])
|
for (const name of [ "connected", "disconnected" ])
|
||||||
wrapMethod(class_declaration.prototype, name+"Callback", function(target, thisArg, detail){
|
wrapMethod(class_declaration.prototype, name+"Callback", function(target, thisArg, detail){
|
||||||
@@ -70,6 +80,7 @@ function lifecycleToEvents(class_declaration){
|
|||||||
target.apply(thisArg, detail);
|
target.apply(thisArg, detail);
|
||||||
});
|
});
|
||||||
class_declaration.prototype.__dde_lifecycleToEvents= true;
|
class_declaration.prototype.__dde_lifecycleToEvents= true;
|
||||||
|
return class_declaration;
|
||||||
}
|
}
|
||||||
function wrapMethod(obj, method, apply){
|
function wrapMethod(obj, method, apply){
|
||||||
obj[method]= new Proxy(obj[method] || (()=> {}), { apply });
|
obj[method]= new Proxy(obj[method] || (()=> {}), { apply });
|
||||||
|
|||||||
Vendored
+1
-2
@@ -54,8 +54,7 @@ interface S {
|
|||||||
* */
|
* */
|
||||||
el<S extends any>(signal: Signal<S, any>, el: (v: S)=> Element | Element[]): DocumentFragment;
|
el<S extends any>(signal: Signal<S, any>, el: (v: S)=> Element | Element[]): DocumentFragment;
|
||||||
|
|
||||||
/** Mirrors element attributes for current host (both way). */
|
fromAttribute<T>(element: HTMLElement, name: string, value?: T): Signal<T, {}>;
|
||||||
attribute<T>(name: string, initial?: T): Signal<T, {}>
|
|
||||||
}
|
}
|
||||||
export const S: S;
|
export const S: S;
|
||||||
declare global {
|
declare global {
|
||||||
|
|||||||
+29
-44
@@ -62,50 +62,6 @@ S.symbols= {
|
|||||||
signal: mark,
|
signal: mark,
|
||||||
onclear: Symbol.for("Signal.onclear")
|
onclear: Symbol.for("Signal.onclear")
|
||||||
};
|
};
|
||||||
import { on } from "./events.js";
|
|
||||||
import { scope } from "./dom.js";
|
|
||||||
const key_attributes= "__dde_attributes";
|
|
||||||
S.attribute= function(name, initial= undefined){
|
|
||||||
const out= S(initial);
|
|
||||||
let host;
|
|
||||||
scope.host(element=> {
|
|
||||||
host= element;
|
|
||||||
if(element instanceof HTMLElement){
|
|
||||||
if(element.hasAttribute(name)) out(element.getAttribute(name));
|
|
||||||
} else {
|
|
||||||
if(element.hasAttributeNS(null, name)) out(element.getAttributeNS(null, name));
|
|
||||||
}
|
|
||||||
if(element[key_attributes]){
|
|
||||||
element[key_attributes][name]= out;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
element[key_attributes]= { [name]: out };
|
|
||||||
on.attributeChanged(function attributeChangeToSignal({ detail }){
|
|
||||||
/*! This maps attributes to signals (`S.attribute`).
|
|
||||||
* Investigate `__dde_attributes` key of the element.*/
|
|
||||||
const [ name, value ]= detail;
|
|
||||||
const curr= element[key_attributes][name];
|
|
||||||
if(curr) return curr(value);
|
|
||||||
})(element);
|
|
||||||
on.disconnected(function(){
|
|
||||||
/*! This removes all signals mapped to attributes (`S.attribute`).
|
|
||||||
* Investigate `__dde_attributes` key of the element.*/
|
|
||||||
S.clear(...Object.values(element[key_attributes]));
|
|
||||||
host= null;
|
|
||||||
})(element);
|
|
||||||
return element;
|
|
||||||
});
|
|
||||||
return new Proxy(out, {
|
|
||||||
apply(target, _, args){
|
|
||||||
if(!args.length) return target();
|
|
||||||
if(!host) return;
|
|
||||||
const value= args[0];
|
|
||||||
if(host instanceof HTMLElement)
|
|
||||||
return host.setAttribute(name, value);
|
|
||||||
return host.setAttributeNS(null, name, value);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
S.clear= function(...signals){
|
S.clear= function(...signals){
|
||||||
for(const signal of signals){
|
for(const signal of signals){
|
||||||
Reflect.deleteProperty(signal, "toJSON");
|
Reflect.deleteProperty(signal, "toJSON");
|
||||||
@@ -130,6 +86,7 @@ S.clear= function(...signals){
|
|||||||
};
|
};
|
||||||
const key_reactive= "__dde_reactive";
|
const key_reactive= "__dde_reactive";
|
||||||
import { el } from "./dom.js";
|
import { el } from "./dom.js";
|
||||||
|
import { scope } from "./dom.js";
|
||||||
S.el= function(signal, map){
|
S.el= function(signal, map){
|
||||||
const mark_start= el.mark({ type: "reactive" }, false);
|
const mark_start= el.mark({ type: "reactive" }, false);
|
||||||
const mark_end= mark_start.end;
|
const mark_end= mark_start.end;
|
||||||
@@ -154,6 +111,34 @@ S.el= function(signal, map){
|
|||||||
reRenderReactiveElement(signal());
|
reRenderReactiveElement(signal());
|
||||||
return out;
|
return out;
|
||||||
};
|
};
|
||||||
|
import { on } from "./events.js";
|
||||||
|
const key_attributes= "__dde_attributes";
|
||||||
|
S.fromAttribute= function(element, name, value){
|
||||||
|
if(!element[key_attributes]){ // needs registration
|
||||||
|
element[key_attributes]= {};
|
||||||
|
on.attributeChanged(function attributeChangeToSignal({ detail }){
|
||||||
|
/*! This maps attributes to signals (`S.attribute`).
|
||||||
|
* Investigate `__dde_attributes` key of the element.*/
|
||||||
|
const [ name, value ]= detail;
|
||||||
|
const curr= element[key_attributes][name];
|
||||||
|
if(curr) return curr(value);
|
||||||
|
})(element);
|
||||||
|
on.disconnected(function(){
|
||||||
|
/*! This removes all signals mapped to attributes (`S.attribute`).
|
||||||
|
* Investigate `__dde_attributes` key of the element.*/
|
||||||
|
S.clear(...Object.values(element[key_attributes]));
|
||||||
|
})(element);
|
||||||
|
}
|
||||||
|
const store= element[key_attributes];
|
||||||
|
const out= Reflect.has(store, name) ? Reflect.get(store, name) : (store[name]= S(value));
|
||||||
|
return new Proxy(out, {
|
||||||
|
apply(target, _, args){
|
||||||
|
if(!args.length) return target();
|
||||||
|
const value= args[0];
|
||||||
|
return element.setAttribute(name, value);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
import { typeOf } from './helpers.js';
|
import { typeOf } from './helpers.js';
|
||||||
export const signals_config= {
|
export const signals_config= {
|
||||||
|
|||||||
Reference in new issue
Block a user