tagu-tagu 1.0.6 → 2.0.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);k(o,n);let i=this.node2Data.get(t);N(o,i)}setDataRecord(t,n){n&&this.node2Data.set(t,n)}resolveCallbacks(t,n){let o=(l,f)=>{if(N(f,this.node2Data.get(l)),!!Object.keys(f).length){if(!l.parentElement){this.node2DescendantCallbacks.has(l)||this.node2DescendantCallbacks.set(l,{});let m=this.node2DescendantCallbacks.get(l);m&&k(m,f);return}o(l.parentElement,f)}},i=this.node2DescendantCallbacks.get(n);i&&o(t,i)}node2DescendantCallbacks=new WeakMap};function N(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 l=e[i];t(l)&&(o[i]=n(l))}if(Object.keys(o).length)return o}function k(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 V(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=K(t);q(n);for(let o of n)j(e,o)}function j(e,t){t instanceof c?t.run(e):(s.resolveCallbacks(e,t),e.appendChild(t))}function K(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 q(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 l=o instanceof p?R([o],()=>t.map(o.get())):t.map(o);y(l,n)}}):y(t,n)}function P(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 G(e,t){let n=e.style;if(n instanceof CSSStyleDeclaration)for(let o in t){let i=t[o];x(e,i,l=>n.setProperty(o,l))}}function U(e,t){for(let n in t){let o=t[n];x(e,o,i=>{i?e.setAttribute(n,i):e.removeAttribute(n)})}}function W(e,t){for(let n in t){let o=t[n];x(e,o,i=>{e[n]=i})}}function Q(e,t){for(let n in t){let o=e.querySelector(n);o&&C(o,t[n])}}function J(e,t){for(let n in t){let o=e.querySelectorAll(n);for(let i of o)C(i,t[n])}}function X(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):(P(e,t.html),F(e,t.text),U(e,t.attr),W(e,t.prop),G(e,t.css),Q(e,t.$),J(e,t.$$),X(e,t.on),b(e,t.data)))}function h(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 h(n,...t)}function r(e,...t){let n=document.createElement(e);return h(n,...t),n}function he(e,...t){let n=L({html:e}).children[0];return h(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 we(...e){return r("h5",...e)}function Ie(...e){return r("h6",...e)}function Ne(...e){return r("p",...e)}function ke(...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 Y(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,l=()=>{let f=[];for(let a of this.list.get())o.has(a)||f.push(a);let m=[],d=new Set(this.list.get());for(let a of i.keys()){let u=i.get(a);u&&!d.has(u)&&m.push(a)}for(let a of f){let u=this.map(a),v=typeof u=="string"?document.createTextNode(u):u;s.resolveCallbacks(n,v),i.set(v,a),o.set(a,v)}for(let a of m){a.parentNode?.removeChild(a);let u=i.get(a);i.delete(a),u&&o.delete(u)}for(let a of[...i.keys()])a.parentElement?.removeChild(a);let E=T(this);for(let a of this.list.get())n.insertBefore(o.get(a),E);this.firstNode=o.get(this.list.get()[0])??null};l(),this.list.on("change",()=>{l()})}};function Z(e,t,n){return new w(e,t,n)}var w=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 l=T(this);this.#e.get()?(n||(n=this.#n()),s.resolveCallbacks(t,n),this.firstNode=n,o?.remove(),t.insertBefore(n,l)):(o||(o=this.#t?.()),o&&s.resolveCallbacks(t,o),this.firstNode=o??null,n?.remove(),o&&t.insertBefore(o,l))};i(),this.#e.on("change",i)}};function _(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=new Map,o=new Map;for(let d of this.#n)o.set(d.case,d);let i,l,f=d=>{let E=o.get(d);if(E){if(!n.has(d)){let a=E.show();n.set(d,a)}return n.get(d)}return this.#t&&!l&&(l=this.#t()),l},m=()=>{let d=this.#e.get(),E=T(this),a=f(d);a&&s.resolveCallbacks(t,a),i?.remove(),a&&t.insertBefore(a,E),i=a};m(),this.#e.on("change",m)}};export{c as ControlFlow,ze as FlexDiv,Y as For,R as FromStates,r as Html,Z as If,h as Modify,p as State,ye as Svg,_ as Switch,he 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,we as h5,Ie 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,Ne as p,ke 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,V 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);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};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tagu-tagu",
3
- "version": "1.0.6",
3
+ "version": "2.0.0",
4
4
  "description": "A lightweight helper for vanilla `HTMLElement`, with reactivity.",
5
5
  "keywords": [
6
6
  "vanilla",
@@ -23,7 +23,8 @@
23
23
  ],
24
24
  "scripts": {
25
25
  "test:browser": "vitest",
26
- "build": "tsx scripts/build.ts"
26
+ "build": "tsx scripts/build.ts",
27
+ "typecheck:watch": "tsc -p tsconfig.check.json --watch"
27
28
  },
28
29
  "devDependencies": {
29
30
  "@biomejs/biome": "2.3.8",
package/src/Modify.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type DataRecord, initializeData } from "./data/data";
1
+ import { type DataRecord, initializeData, waitForData } from "./data/data";
2
2
  import { Binding } from "./data/useBinding";
3
3
  import { type ChildType, initializeChildBlock } from "./initializeChildBlock";
4
4
  import { FromStates, State } from "./State";
@@ -65,7 +65,7 @@ function applyStringOrStateOrBinding(
65
65
  initialize: (text: string) => void,
66
66
  ) {
67
67
  if (value instanceof Binding) {
68
- initializeData(element, {
68
+ waitForData(element, {
69
69
  [value.key]: (data: any) => {
70
70
  const isState = data instanceof State;
71
71
  const stringOrState = isState
package/src/data/data.ts CHANGED
@@ -81,45 +81,21 @@ function resolveCallbacksByData(
81
81
  }
82
82
  }
83
83
 
84
- export type DataRecord = Record<string, DataCallback | any>;
84
+ export type DataRecord = Record<string, any>;
85
85
  export const nodeData = new NodeData();
86
86
  export function initializeData(element: Node, data: DataRecord | undefined) {
87
- nodeData.addCallbacks(element, extractCallbackRecord(data));
88
- nodeData.setDataRecord(element, extractDataValueRecord(data));
87
+ nodeData.setDataRecord(element, data);
89
88
  }
90
89
 
91
90
  export function extractCallbackRecord(
92
- record: DataRecord | undefined,
91
+ record: Record<string, DataCallback> | undefined,
93
92
  ): Record<string, DataCallback[]> | undefined {
94
- return extractRecordFromDataRecord(
95
- record,
96
- (value) => typeof value === "function",
97
- (callback) => [callback],
98
- );
99
- }
100
-
101
- export function extractDataValueRecord(
102
- record: DataRecord | undefined,
103
- ): Record<string, any> | undefined {
104
- return extractRecordFromDataRecord(
105
- record,
106
- (value) => typeof value !== "function",
107
- );
108
- }
109
-
110
- function extractRecordFromDataRecord(
111
- record: DataRecord | undefined,
112
- predicate: (value: any) => boolean,
113
- map = (value: any) => value,
114
- ) {
115
93
  if (!record) return;
116
94
 
117
95
  const result = {} as Record<string, any>;
118
96
  for (const key in record) {
119
97
  const value = record[key];
120
- if (predicate(value)) {
121
- result[key] = map(value);
122
- }
98
+ result[key] = [value];
123
99
  }
124
100
 
125
101
  if (!Object.keys(result).length) return;
@@ -127,16 +103,6 @@ function extractRecordFromDataRecord(
127
103
  return result;
128
104
  }
129
105
 
130
- export function createDescendantCallbacks(
131
- record: Record<string, DataCallback> | undefined,
132
- ) {
133
- return extractRecordFromDataRecord(
134
- record,
135
- () => true,
136
- (value) => [value],
137
- );
138
- }
139
-
140
106
  export function appendCallbacksRecord(
141
107
  record1: Record<string, DataCallback[]>,
142
108
  record2: Record<string, DataCallback[]> | undefined,
@@ -158,3 +124,10 @@ export function findData(node: Node | null, key: string) {
158
124
  }
159
125
  return findData(node.parentElement, key);
160
126
  }
127
+
128
+ export function waitForData(
129
+ node: Node,
130
+ callbackRecord: Record<string, (data: any) => void>,
131
+ ) {
132
+ nodeData.addCallbacks(node, extractCallbackRecord(callbackRecord));
133
+ }
package/src/flow/index.ts CHANGED
@@ -2,4 +2,4 @@ export { ControlFlow } from "./ControlFlow";
2
2
  export { For } from "./For";
3
3
  export { If } from "./If";
4
4
  export { Switch } from "./Switch";
5
- export { SwitchSection } from "./SwitchBlockState";
5
+ export type { SwitchSection } from "./SwitchBlockState";