tagu-tagu 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- var S=class{node2Data=new WeakMap;addCallbacks(t,n){if(!n)return;this.node2DescendantCallbacks.has(t)||this.node2DescendantCallbacks.set(t,{});let o=this.node2DescendantCallbacks.get(t);w(o,n);let i=this.node2Data.get(t);k(o,i)}setDataRecord(t,n){n&&this.node2Data.set(t,n)}resolveCallbacks(t,n){let o=(a,f)=>{if(k(f,this.node2Data.get(a)),!!Object.keys(f).length){if(!a.parentElement){this.node2DescendantCallbacks.has(a)||this.node2DescendantCallbacks.set(a,{});let m=this.node2DescendantCallbacks.get(a);m&&w(m,f);return}o(a.parentElement,f)}},i=this.node2DescendantCallbacks.get(n);i&&o(t,i)}node2DescendantCallbacks=new WeakMap};function k(e,t){for(let n in t)if(n in e){for(let o of e[n])o(t[n]);delete e[n]}}var s=new S;function b(e,t){s.addCallbacks(e,$(t)),s.setDataRecord(e,A(t))}function $(e){return H(e,t=>typeof t=="function",t=>[t])}function A(e){return H(e,t=>typeof t!="function")}function H(e,t,n=o=>o){if(!e)return;let o={};for(let i in e){let a=e[i];t(a)&&(o[i]=n(a))}if(Object.keys(o).length)return o}function w(e,t){for(let n in t)e[n]||(e[n]=[]),e[n].push(...t[n])}function D(e,t){if(!e)return;let n=s.node2Data.get(e);return n&&t in n?n[t]:D(e.parentElement,t)}function O(e,t){return new g(e,t)}var g=class{constructor(t,n){this.key=t;this.map=n}};var c=class{next=null;firstNode=null};var p=class{#e;constructor(t){this.#e=t}get=()=>this.#e;set(t){this.#e=t,this.#n("change")}#n(t){this.#t.dispatchEvent(new Event(t))}#t=new EventTarget;on(t,n){this.#t.addEventListener(t,n)}};function re(e){return new p(e)}function R(e,t){let n=new p(t()),o=()=>{n.set(t())};for(let i of e)i.on("change",o);return n}function z(e,t){let n=j(t);K(n);for(let o of n)V(e,o)}function V(e,t){t instanceof c?t.run(e):(s.resolveCallbacks(e,t),e.appendChild(t))}function j(e){return e.map(t=>{if(typeof t=="string"||t instanceof p){let n=document.createTextNode("");return y(t,o=>{n.textContent=o}),n}return t})}function K(e){for(let t=0;t<e.length;t++){let n=e[t];n instanceof c&&(n.next=e[t+1]??null)}}function B(e){let t=e.next;return t===null?null:t instanceof Node?t:t.firstNode?t.firstNode:B(t)}function T(e){let t=B(e);return t?.parentElement?t:null}function y(e,t){if(typeof e=="string")t(e);else{let n=()=>{t(e.get())};n(),e.on("change",n)}}function x(e,t,n){t instanceof g?b(e,{[t.key]:o=>{let a=o instanceof p?R([o],()=>t.map(o.get())):t.map(o);y(a,n)}}):y(t,n)}function q(e,t){t!==void 0&&x(e,t,n=>{e.innerHTML=n})}function F(e,t){t!==void 0&&x(e,t,n=>{e.textContent=n})}function P(e,t){let n=e.style;if(n instanceof CSSStyleDeclaration)for(let o in t){let i=t[o];x(e,i,a=>n.setProperty(o,a))}}function G(e,t){for(let n in t){let o=t[n];x(e,o,i=>{i?e.setAttribute(n,i):e.removeAttribute(n)})}}function U(e,t){for(let n in t){let o=t[n];x(e,o,i=>{e[n]=i})}}function W(e,t){for(let n in t){let o=e.querySelector(n);o&&C(o,t[n])}}function Q(e,t){for(let n in t){let o=e.querySelectorAll(n);for(let i of o)C(i,t[n])}}function J(e,t){for(let n in t){let i=t[n];i&&(typeof i=="function"?e.addEventListener(n,i):e.addEventListener(n,i.listener,i.options))}}function C(e,t){e&&(typeof t=="string"||t instanceof p?F(e,t):Array.isArray(t)?z(e,t):typeof t=="function"?t(e):(q(e,t.html),F(e,t.text),G(e,t.attr),U(e,t.prop),P(e,t.css),W(e,t.$),Q(e,t.$$),J(e,t.on),b(e,t.data)))}function v(e,...t){let n=typeof e=="string"?document.querySelector(e):e;for(let o of t)C(n,o);return n}function ye(e,...t){let n=document.createElementNS("http://www.w3.org/2000/svg",e);return v(n,...t)}function r(e,...t){let n=document.createElement(e);return v(n,...t),n}function ve(e,...t){let n=L({html:e}).children[0];return v(n,...t)}function be(...e){return r("h1",...e)}function Ce(...e){return r("h2",...e)}function Le(...e){return r("h3",...e)}function Me(...e){return r("h4",...e)}function Ie(...e){return r("h5",...e)}function Ne(...e){return r("h6",...e)}function ke(...e){return r("p",...e)}function we(...e){return r("section",...e)}function He(...e){return r("button",...e)}function De(...e){return r("span",...e)}function Re(e){function t(n,o){return`${n} {${Object.keys(o).map(i=>`${i}: ${o[i]};`).join("")}}`}return r("style",[Object.keys(e).map(n=>t(n,e[n])).join("")])}function L(...e){return r("div",...e)}function ze(...e){return L({css:{display:"flex"}},...e)}function Be(...e){return r("input",...e)}function Fe(...e){return r("textarea",...e)}function $e(...e){return r("select",...e)}function Ae(...e){return r("option",...e)}function Oe(...e){return r("br",...e)}function Ve(...e){return r("tr",...e)}function je(...e){return r("td",...e)}function Ke(...e){return r("b",...e)}function qe(...e){return r("label",...e)}function Pe(...e){return r("a",...e)}function Ge(...e){return r("blockquote",...e)}function Ue(...e){return r("li",...e)}function We(...e){return r("ol",...e)}function Qe(...e){return r("ul",...e)}function Je(...e){return r("audio",...e)}function Xe(...e){return r("video",...e)}function Ye(...e){return r("img",...e)}function Ze(...e){return r("canvas",...e)}function _e(...e){return r("iframe",...e)}function et(...e){return r("form",...e)}function tt(...e){return r("table",...e)}function nt(...e){return r("tbody",...e)}function ot(...e){return r("hr",...e)}function X(e,t){return new M(e,t)}var M=class extends c{constructor(n,o){super();this.list=n;this.map=o}run(n){let o=new Map,i=new Map,a=()=>{let f=[];for(let l of this.list.get())o.has(l)||f.push(l);let m=[],d=new Set(this.list.get());for(let l of i.keys()){let u=i.get(l);u&&!d.has(u)&&m.push(l)}for(let l of f){let u=this.map(l),h=typeof u=="string"?document.createTextNode(u):u;s.resolveCallbacks(n,h),i.set(h,l),o.set(l,h)}for(let l of m){l.parentNode?.removeChild(l);let u=i.get(l);i.delete(l),u&&o.delete(u)}for(let l of[...i.keys()])l.parentElement?.removeChild(l);let E=T(this);for(let l of this.list.get())n.insertBefore(o.get(l),E);this.firstNode=o.get(this.list.get()[0])??null};a(),this.list.on("change",()=>{a()})}};function Y(e,t,n){return new I(e,t,n)}var I=class extends c{#e;#n;#t;constructor(t,n,o){super(),this.#e=t,this.#n=n,this.#t=o}run(t){let n,o,i=()=>{let a=T(this);this.#e.get()?(n||(n=this.#n()),s.resolveCallbacks(t,n),this.firstNode=n,o?.remove(),t.insertBefore(n,a)):(o||(o=this.#t?.()),o&&s.resolveCallbacks(t,o),this.firstNode=o??null,n?.remove(),o&&t.insertBefore(o,a))};i(),this.#e.on("change",i)}};function Z(e,t,n){return new N(e,t,n)}var N=class extends c{#e;#n;#t;constructor(t,n,o){super(),this.#e=t,this.#n=n,this.#t=o}run(t){let n=new Map,o=new Map;for(let d of this.#n)o.set(d.case,d);let i,a,f=d=>{let E=o.get(d);if(E){if(!n.has(d)){let l=E.show();n.set(d,l)}return n.get(d)}return this.#t&&!a&&(a=this.#t()),a},m=()=>{let d=this.#e.get(),E=T(this),l=f(d);l&&s.resolveCallbacks(t,l),i?.remove(),l&&t.insertBefore(l,E),i=l};m(),this.#e.on("change",m)}};export{c as ControlFlow,ze as FlexDiv,X as For,R as FromStates,r as Html,Y as If,v as Modify,p as State,ye as Svg,Z as Switch,ve as Tag,Pe as a,y as applyStringOrState,Je as audio,Ke as b,Ge as blockquote,Oe as br,He as button,Ze as canvas,L as div,D as findData,et as form,be as h1,Ce as h2,Le as h3,Me as h4,Ie as h5,Ne as h6,ot as hr,_e as iframe,Ye as img,Be as input,qe as label,Ue as li,We as ol,Ae as option,ke as p,we as section,$e as select,De as span,Re as style,tt as table,nt as tbody,je as td,Fe as textarea,Ve as tr,Qe as ul,O as useBinding,re as useState,Xe as video};
1
+ var S=class{node2Data=new WeakMap;addCallbacks(t,n){if(!n)return;this.node2DescendantCallbacks.has(t)||this.node2DescendantCallbacks.set(t,{});let o=this.node2DescendantCallbacks.get(t);w(o,n);let i=this.node2Data.get(t);I(o,i)}setDataRecord(t,n){n&&this.node2Data.set(t,n)}resolveCallbacks(t,n){let o=(a,f)=>{if(I(f,this.node2Data.get(a)),!!Object.keys(f).length){if(!a.parentElement){this.node2DescendantCallbacks.has(a)||this.node2DescendantCallbacks.set(a,{});let m=this.node2DescendantCallbacks.get(a);m&&w(m,f);return}o(a.parentElement,f)}},i=this.node2DescendantCallbacks.get(n);i&&o(t,i)}node2DescendantCallbacks=new WeakMap};function I(e,t){for(let n in t)if(n in e){for(let o of e[n])o(t[n]);delete e[n]}}var s=new S;function H(e,t){s.setDataRecord(e,t)}function $(e){if(!e)return;let t={};for(let n in e){let o=e[n];t[n]=[o]}if(Object.keys(t).length)return t}function w(e,t){for(let n in t)e[n]||(e[n]=[]),e[n].push(...t[n])}function k(e,t){if(!e)return;let n=s.node2Data.get(e);return n&&t in n?n[t]:k(e.parentElement,t)}function z(e,t){s.addCallbacks(e,$(t))}function A(e,t){return new g(e,t)}var g=class{constructor(t,n){this.key=t;this.map=n}};var c=class{next=null;firstNode=null};var u=class{#e;constructor(t){this.#e=t}get=()=>this.#e;set(t){this.#e=t,this.#n("change")}#n(t){this.#t.dispatchEvent(new Event(t))}#t=new EventTarget;on(t,n){this.#t.addEventListener(t,n)}};function ie(e){return new u(e)}function D(e,t){let n=new u(t()),o=()=>{n.set(t())};for(let i of e)i.on("change",o);return n}function R(e,t){let n=j(t);K(n);for(let o of n)O(e,o)}function O(e,t){t instanceof c?t.run(e):(s.resolveCallbacks(e,t),e.appendChild(t))}function j(e){return e.map(t=>{if(typeof t=="string"||t instanceof u){let n=document.createTextNode("");return y(t,o=>{n.textContent=o}),n}return t})}function K(e){for(let t=0;t<e.length;t++){let n=e[t];n instanceof c&&(n.next=e[t+1]??null)}}function B(e){let t=e.next;return t===null?null:t instanceof Node?t:t.firstNode?t.firstNode:B(t)}function T(e){let t=B(e);return t?.parentElement?t:null}function y(e,t){if(typeof e=="string")t(e);else{let n=()=>{t(e.get())};n(),e.on("change",n)}}function x(e,t,n){t instanceof g?z(e,{[t.key]:o=>{let a=o instanceof u?D([o],()=>t.map(o.get())):t.map(o);y(a,n)}}):y(t,n)}function V(e,t){t!==void 0&&x(e,t,n=>{e.innerHTML=n})}function F(e,t){t!==void 0&&x(e,t,n=>{e.textContent=n})}function q(e,t){let n=e.style;if(n instanceof CSSStyleDeclaration)for(let o in t){let i=t[o];x(e,i,a=>n.setProperty(o,a))}}function P(e,t){for(let n in t){let o=t[n];x(e,o,i=>{i?e.setAttribute(n,i):e.removeAttribute(n)})}}function G(e,t){for(let n in t){let o=t[n];x(e,o,i=>{e[n]=i})}}function U(e,t){for(let n in t){let o=e.querySelector(n);o&&b(o,t[n])}}function W(e,t){for(let n in t){let o=e.querySelectorAll(n);for(let i of o)b(i,t[n])}}function Q(e,t){for(let n in t){let i=t[n];i&&(typeof i=="function"?e.addEventListener(n,i):e.addEventListener(n,i.listener,i.options))}}function b(e,t){e&&(typeof t=="string"||t instanceof u?F(e,t):Array.isArray(t)?R(e,t):typeof t=="function"?t(e):(V(e,t.html),F(e,t.text),P(e,t.attr),G(e,t.prop),q(e,t.css),U(e,t.$),W(e,t.$$),Q(e,t.on),H(e,t.data)))}function v(e,...t){let n=typeof e=="string"?document.querySelector(e):e;for(let o of t)b(n,o);return n}function xe(e,...t){let n=document.createElementNS("http://www.w3.org/2000/svg",e);return v(n,...t)}function r(e,...t){let n=document.createElement(e);return v(n,...t),n}function ye(e,...t){let n=C({html:e}).children[0];return v(n,...t)}function Se(...e){return r("h1",...e)}function be(...e){return r("h2",...e)}function Ce(...e){return r("h3",...e)}function Le(...e){return r("h4",...e)}function Me(...e){return r("h5",...e)}function Ne(...e){return r("h6",...e)}function Ie(...e){return r("p",...e)}function we(...e){return r("section",...e)}function He(...e){return r("button",...e)}function ke(...e){return r("span",...e)}function ze(e){function t(n,o){return`${n} {${Object.keys(o).map(i=>`${i}: ${o[i]};`).join("")}}`}return r("style",[Object.keys(e).map(n=>t(n,e[n])).join("")])}function C(...e){return r("div",...e)}function De(...e){return C({css:{display:"flex"}},...e)}function Re(...e){return r("input",...e)}function Be(...e){return r("textarea",...e)}function Fe(...e){return r("select",...e)}function $e(...e){return r("option",...e)}function Ae(...e){return r("br",...e)}function Oe(...e){return r("tr",...e)}function je(...e){return r("td",...e)}function Ke(...e){return r("b",...e)}function Ve(...e){return r("label",...e)}function qe(...e){return r("a",...e)}function Pe(...e){return r("blockquote",...e)}function Ge(...e){return r("li",...e)}function Ue(...e){return r("ol",...e)}function We(...e){return r("ul",...e)}function Qe(...e){return r("audio",...e)}function Je(...e){return r("video",...e)}function Xe(...e){return r("img",...e)}function Ye(...e){return r("canvas",...e)}function Ze(...e){return r("iframe",...e)}function _e(...e){return r("form",...e)}function et(...e){return r("table",...e)}function tt(...e){return r("tbody",...e)}function nt(...e){return r("hr",...e)}function J(e,t){return new L(e,t)}var L=class extends c{constructor(n,o){super();this.list=n;this.map=o}run(n){let o=new Map,i=new Map,a=()=>{let f=[];for(let l of this.list.get())o.has(l)||f.push(l);let m=[],d=new Set(this.list.get());for(let l of i.keys()){let p=i.get(l);p&&!d.has(p)&&m.push(l)}for(let l of f){let p=this.map(l),h=typeof p=="string"?document.createTextNode(p):p;s.resolveCallbacks(n,h),i.set(h,l),o.set(l,h)}for(let l of m){l.parentNode?.removeChild(l);let p=i.get(l);i.delete(l),p&&o.delete(p)}for(let l of[...i.keys()])l.parentElement?.removeChild(l);let E=T(this);for(let l of this.list.get())n.insertBefore(o.get(l),E);this.firstNode=o.get(this.list.get()[0])??null};a(),this.list.on("change",()=>{a()})}};function X(e,t,n){return new M(e,t,n)}var M=class extends c{#e;#n;#t;constructor(t,n,o){super(),this.#e=t,this.#n=n,this.#t=o}run(t){let n,o,i=()=>{let a=T(this);this.#e.get()?(n||(n=this.#n()),s.resolveCallbacks(t,n),this.firstNode=n,o?.remove(),t.insertBefore(n,a)):(o||(o=this.#t?.()),o&&s.resolveCallbacks(t,o),this.firstNode=o??null,n?.remove(),o&&t.insertBefore(o,a))};i(),this.#e.on("change",i)}};function Y(e,t,n){return new N(e,t,n)}var N=class extends c{#e;#n;#t;constructor(t,n,o){super(),this.#e=t,this.#n=n,this.#t=o}run(t){let n=new Map,o=new Map;for(let d of this.#n)o.set(d.case,d);let i,a,f=d=>{let E=o.get(d);if(E){if(!n.has(d)){let l=E.show();n.set(d,l)}return n.get(d)}return this.#t&&!a&&(a=this.#t()),a},m=()=>{let d=this.#e.get(),E=T(this),l=f(d);l&&s.resolveCallbacks(t,l),i?.remove(),l&&t.insertBefore(l,E),i=l};m(),this.#e.on("change",m)}};export{c as ControlFlow,De as FlexDiv,J as For,D as FromStates,r as Html,X as If,v as Modify,u as State,xe as Svg,Y as Switch,ye as Tag,qe as a,y as applyStringOrState,Qe as audio,Ke as b,Pe as blockquote,Ae as br,He as button,Ye as canvas,C as div,k as findData,_e as form,Se as h1,be as h2,Ce as h3,Le as h4,Me as h5,Ne as h6,nt as hr,Ze as iframe,Xe as img,Re as input,Ve as label,Ge as li,Ue as ol,$e as option,Ie as p,we as section,Fe as select,ke as span,ze as style,et as table,tt as tbody,je as td,Be as textarea,Oe as tr,We as ul,A as useBinding,ie as useState,Je as video};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tagu-tagu",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "A lightweight helper for vanilla `HTMLElement`, with reactivity.",
5
5
  "keywords": [
6
6
  "vanilla",
package/src/Modify.ts CHANGED
@@ -11,6 +11,7 @@ type EventListenerType<
11
11
  type EventListenerRecord<TEventType2Event> = {
12
12
  [TEventType in keyof TEventType2Event]?:
13
13
  | EventListenerType<TEventType2Event, TEventType>
14
+ | Binding
14
15
  | {
15
16
  listener: EventListenerType<TEventType2Event, TEventType>;
16
17
  options: boolean | AddEventListenerOptions;
@@ -170,6 +171,13 @@ function initializeEventListeners<TEventType2Event>(
170
171
  if (!listener) continue;
171
172
  if (typeof listener === "function") {
172
173
  element.addEventListener(eventName, listener as EventListener);
174
+ } else if (listener instanceof Binding) {
175
+ waitForData(element, {
176
+ [listener.key]: (data) => {
177
+ const func = listener.map(data);
178
+ element.addEventListener(eventName, func);
179
+ },
180
+ });
173
181
  } else {
174
182
  element.addEventListener(
175
183
  eventName,
@@ -1,10 +1,15 @@
1
- export function useBinding<T>(key: string, map: (value: T) => string) {
1
+ export function useBinding<T>(key: string): Binding<T, T>;
2
+ export function useBinding<T, TResult>(
3
+ key: string,
4
+ map: (value: T) => TResult,
5
+ ): Binding<T, TResult>;
6
+ export function useBinding<T>(key: string, map = (value: T) => value) {
2
7
  return new Binding(key, map);
3
8
  }
4
9
 
5
- export class Binding<T = any> {
10
+ export class Binding<T = any, TResult = any> {
6
11
  constructor(
7
12
  public key: string,
8
- public map: (value: T) => string,
13
+ public map: (value: T) => TResult,
9
14
  ) {}
10
15
  }