tagu-tagu 2.1.1 → 3.2.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.
- package/README.md +5 -5
- package/dist/bundle.min.js +1 -1
- package/package.json +3 -2
- package/src/Modify.ts +2 -2
- package/src/State.ts +9 -3
- package/src/data/data.ts +16 -2
- package/src/flow/Switch.ts +27 -1
package/README.md
CHANGED
|
@@ -97,11 +97,11 @@ function SwitchExample() {
|
|
|
97
97
|
button("Pentagon", { on: { click: () => state.set("pentagon") } }),
|
|
98
98
|
Switch(
|
|
99
99
|
state,
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
100
|
+
{
|
|
101
|
+
triangle: () => div("▲"),
|
|
102
|
+
rectangle: () => div("■"),
|
|
103
|
+
circle: () => div("●"),
|
|
104
|
+
},
|
|
105
105
|
() => div("?"),
|
|
106
106
|
),
|
|
107
107
|
]);
|
package/dist/bundle.min.js
CHANGED
|
@@ -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);
|
|
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);H(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=(l,d)=>{if(k(d,this.node2Data.get(l)),!!Object.keys(d).length){if(!l.parentElement){this.node2DescendantCallbacks.has(l)||this.node2DescendantCallbacks.set(l,{});let m=this.node2DescendantCallbacks.get(l);m&&H(m,d);return}o(l.parentElement,d)}},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 R(e,t){s.setDataRecord(e,t)}function A(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 H(e,t){for(let n in t)e[n]||(e[n]=[]),e[n].push(...t[n])}function b(e,t){if(!e)return;let n=s.node2Data.get(e);return n&&t in n?n[t]:b(e.parentElement,t)}function C(e,t){if(typeof t=="string")return P(e,t);s.addCallbacks(e,A(t))}function P(e,t){return new Promise(n=>{let o=b(e,t);o!==void 0?n(o):s.addCallbacks(e,{[t]:[n]})})}function K(e,t=n=>n){return new T(e,t)}var T=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 D(e,t){return typeof t=="function"?O(e,t):new p(e)}function O(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=V(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 V(e){return e.map(t=>{if(typeof t=="string"||t instanceof p){let n=document.createTextNode("");return x(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 g(e){let t=B(e);return t?.parentElement?t:null}function x(e,t){if(typeof e=="string")t(e);else{let n=()=>{t(e.get())};n(),e.on("change",n)}}function y(e,t,n){t instanceof T?C(e,{[t.key]:o=>{let l=o instanceof p?D([o],()=>t.map(o.get())):t.map(o);x(l,n)}}):x(t,n)}function G(e,t){t!==void 0&&y(e,t,n=>{e.innerHTML=n})}function F(e,t){t!==void 0&&y(e,t,n=>{e.textContent=n})}function U(e,t){let n=e.style;if(n instanceof CSSStyleDeclaration)for(let o in t){let i=t[o];y(e,i,l=>n.setProperty(o,l))}}function W(e,t){for(let n in t){let o=t[n];y(e,o,i=>{i?e.setAttribute(n,i):e.removeAttribute(n)})}}function Q(e,t){for(let n in t){let o=t[n];y(e,o,i=>{e[n]=i})}}function _(e,t){for(let n in t){let o=e.querySelector(n);o&&w(o,t[n])}}function J(e,t){for(let n in t){let o=e.querySelectorAll(n);for(let i of o)w(i,t[n])}}function X(e,t){for(let n in t){let i=t[n];i&&(typeof i=="function"?e.addEventListener(n,i):i instanceof T?C(e,{[i.key]:l=>{let d=i.map(l);e.addEventListener(n,d)}}):e.addEventListener(n,i.listener,i.options))}}function w(e,t){e&&(typeof t=="string"||t instanceof p?F(e,t):Array.isArray(t)?z(e,t):typeof t=="function"?t(e):(G(e,t.html),F(e,t.text),W(e,t.attr),Q(e,t.prop),U(e,t.css),_(e,t.$),J(e,t.$$),X(e,t.on),R(e,t.data)))}function h(e,...t){let n=typeof e=="string"?document.querySelector(e):e;for(let o of t)w(n,o);return n}function he(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 ve(e,...t){let n=L({html:e}).children[0];return h(n,...t)}function Ce(...e){return r("h1",...e)}function we(...e){return r("h2",...e)}function Le(...e){return r("h3",...e)}function Ne(...e){return r("h4",...e)}function Me(...e){return r("h5",...e)}function Ie(...e){return r("h6",...e)}function ke(...e){return r("p",...e)}function He(...e){return r("section",...e)}function Re(...e){return r("button",...e)}function De(...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 L(...e){return r("div",...e)}function Be(...e){return L({css:{display:"flex"}},...e)}function Fe(...e){return r("input",...e)}function $e(...e){return r("textarea",...e)}function Ae(...e){return r("select",...e)}function Pe(...e){return r("option",...e)}function Ke(...e){return r("br",...e)}function Oe(...e){return r("tr",...e)}function je(...e){return r("td",...e)}function Ve(...e){return r("b",...e)}function qe(...e){return r("label",...e)}function Ge(...e){return r("a",...e)}function Ue(...e){return r("blockquote",...e)}function We(...e){return r("li",...e)}function Qe(...e){return r("ol",...e)}function _e(...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 et(...e){return r("iframe",...e)}function tt(...e){return r("form",...e)}function nt(...e){return r("table",...e)}function ot(...e){return r("tbody",...e)}function it(...e){return r("hr",...e)}function Y(e,t){return new N(e,t)}var N=class extends c{constructor(n,o){super();this.list=n;this.map=o}run(n){let o=new Map,i=new Map,l=()=>{let d=[];for(let a of this.list.get())o.has(a)||d.push(a);let m=[],f=new Set(this.list.get());for(let a of i.keys()){let u=i.get(a);u&&!f.has(u)&&m.push(a)}for(let a of d){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=g(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 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 l=g(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 Array.isArray(t)?new I(e,t,n):$(e,ee(t).map(o=>({case:o,show:t[o]})),n)}function ee(e){let t=[];for(let n in e)t.push(n);return t}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 f of this.#n)o.set(f.case,f);let i,l,d=f=>{let E=o.get(f);if(E){if(!n.has(f)){let a=E.show();n.set(f,a)}return n.get(f)}return this.#t&&!l&&(l=this.#t()),l},m=()=>{let f=this.#e.get(),E=g(this),a=d(f);a&&s.resolveCallbacks(t,a),i?.remove(),a&&t.insertBefore(a,E),i=a};m(),this.#e.on("change",m)}};export{c as ControlFlow,Be as FlexDiv,Y as For,r as Html,Z as If,h as Modify,p as State,he as Svg,$ as Switch,ve as Tag,Ge as a,x as applyStringOrState,Je as audio,Ve as b,Ue as blockquote,Ke as br,Re as button,Ze as canvas,L as div,b as findData,tt as form,Ce as h1,we as h2,Le as h3,Ne as h4,Me as h5,Ie as h6,it as hr,et as iframe,Ye as img,Fe as input,qe as label,We as li,Qe as ol,Pe as option,ke as p,He as section,Ae as select,De as span,ze as style,nt as table,ot as tbody,je as td,$e as textarea,Oe as tr,_e as ul,K as useBinding,D as useState,Xe as video};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tagu-tagu",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "3.2.0",
|
|
4
4
|
"description": "A lightweight helper for vanilla `HTMLElement`, with reactivity.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"vanilla",
|
|
@@ -24,7 +24,8 @@
|
|
|
24
24
|
"scripts": {
|
|
25
25
|
"test:browser": "vitest",
|
|
26
26
|
"build": "tsx scripts/build.ts",
|
|
27
|
-
"typecheck:watch": "tsc -p tsconfig.check.json --watch"
|
|
27
|
+
"typecheck:watch": "tsc -p tsconfig.check.json --watch",
|
|
28
|
+
"watch": "tsx --watch scripts/watch.ts"
|
|
28
29
|
},
|
|
29
30
|
"devDependencies": {
|
|
30
31
|
"@biomejs/biome": "2.3.8",
|
package/src/Modify.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
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
|
-
import {
|
|
4
|
+
import { State, useState } from "./State";
|
|
5
5
|
|
|
6
6
|
type EventListenerType<
|
|
7
7
|
TEventType2Event,
|
|
@@ -70,7 +70,7 @@ function applyStringOrStateOrBinding(
|
|
|
70
70
|
[value.key]: (data: any) => {
|
|
71
71
|
const isState = data instanceof State;
|
|
72
72
|
const stringOrState = isState
|
|
73
|
-
?
|
|
73
|
+
? useState([data], () => value.map(data.get()))
|
|
74
74
|
: value.map(data);
|
|
75
75
|
|
|
76
76
|
applyStringOrState(stringOrState, initialize);
|
package/src/State.ts
CHANGED
|
@@ -22,11 +22,17 @@ export class State<T = any> {
|
|
|
22
22
|
|
|
23
23
|
type StateEventType = "change";
|
|
24
24
|
|
|
25
|
-
export function useState<T>(
|
|
26
|
-
|
|
25
|
+
export function useState<T>(states: State[], map: () => T): State<T>;
|
|
26
|
+
export function useState<T>(value: T): State<T>;
|
|
27
|
+
export function useState<T>(value: any, map?: any) {
|
|
28
|
+
if (typeof map === "function") {
|
|
29
|
+
return fromStates(value, map);
|
|
30
|
+
} else {
|
|
31
|
+
return new State<T>(value);
|
|
32
|
+
}
|
|
27
33
|
}
|
|
28
34
|
|
|
29
|
-
|
|
35
|
+
function fromStates<T>(states: State[], createValue: () => T) {
|
|
30
36
|
const result = new State<T>(createValue());
|
|
31
37
|
const update = () => {
|
|
32
38
|
result.set(createValue());
|
package/src/data/data.ts
CHANGED
|
@@ -125,9 +125,23 @@ export function findData(node: Node | null, key: string) {
|
|
|
125
125
|
return findData(node.parentElement, key);
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
+
export function waitForData<T = any>(node: Node, key: string): Promise<T>;
|
|
128
129
|
export function waitForData(
|
|
129
130
|
node: Node,
|
|
130
131
|
callbackRecord: Record<string, (data: any) => void>,
|
|
131
|
-
)
|
|
132
|
-
|
|
132
|
+
): void;
|
|
133
|
+
export function waitForData(node: Node, callbacksOrKey: any) {
|
|
134
|
+
if (typeof callbacksOrKey === "string") {
|
|
135
|
+
return waitForData_Promise(node, callbacksOrKey);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
nodeData.addCallbacks(node, extractCallbackRecord(callbacksOrKey));
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function waitForData_Promise<T = any>(node: Node, key: string): Promise<T> {
|
|
142
|
+
return new Promise<any>((resolve) => {
|
|
143
|
+
const found = findData(node, key);
|
|
144
|
+
if (found !== undefined) resolve(found);
|
|
145
|
+
else nodeData.addCallbacks(node, { [key]: [resolve] });
|
|
146
|
+
});
|
|
133
147
|
}
|
package/src/flow/Switch.ts
CHANGED
|
@@ -8,9 +8,35 @@ export function Switch<T>(
|
|
|
8
8
|
value: State<T>,
|
|
9
9
|
sections: SwitchSection<T>[],
|
|
10
10
|
createDefault?: () => Element,
|
|
11
|
+
): ControlFlow;
|
|
12
|
+
export function Switch(
|
|
13
|
+
value: State<string>,
|
|
14
|
+
sections: Record<string, () => Element>,
|
|
15
|
+
createDefault?: () => Element,
|
|
16
|
+
): ControlFlow;
|
|
17
|
+
export function Switch<T>(
|
|
18
|
+
value: State,
|
|
19
|
+
sections: SwitchSection<T>[] | Record<string, () => Element>,
|
|
20
|
+
createDefault?: () => Element,
|
|
11
21
|
): ControlFlow {
|
|
12
|
-
|
|
22
|
+
if (Array.isArray(sections)) {
|
|
23
|
+
return new SwitchFlow(value, sections, createDefault);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return Switch(
|
|
27
|
+
value,
|
|
28
|
+
getKeys(sections).map((key) => ({ case: key, show: sections[key] })),
|
|
29
|
+
createDefault,
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
function getKeys<K extends keyof any>(record: Record<K, any>) {
|
|
33
|
+
const result = [] as K[];
|
|
34
|
+
for (const key in record) {
|
|
35
|
+
result.push(key);
|
|
36
|
+
}
|
|
37
|
+
return result;
|
|
13
38
|
}
|
|
39
|
+
|
|
14
40
|
export class SwitchFlow<T> extends ControlFlow {
|
|
15
41
|
#value: State<T>;
|
|
16
42
|
#sections: SwitchSection<T>[];
|