-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcore.ts
198 lines (179 loc) · 5.16 KB
/
core.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
export {
createText as t,
createText as text,
createHTMLElement as h,
createHTMLElement as html,
createSVGElement as s,
createSVGElement as svg,
}
export function fragment(nodes: NodeChild[]) {
const fragment = document.createDocumentFragment()
for (const node of nodes) {
appendChild(fragment, node)
}
return fragment
}
/** @alias t, text */
export function createText(value: string | number = '') {
const node = document.createTextNode(value as string)
return createProxy(node)
}
let watchFn: (() => void) | null = null
/** @description run once immediately, auto track dependency and re-run */
export function watch(fn: () => void) {
watchFn = fn
fn()
watchFn = null
}
export type CreateProxyOptions = {
listen?: 'change' | 'input' | false // default 'input'
}
export type ProxyNode<E> = E & { node: E }
let resetTimer: ReturnType<typeof setTimeout> | null = null
const resetFns = new Set<() => void>()
function resetTimeout() {
for (let fn of resetFns) {
try {
fn()
} catch (error) {
console.error(error)
}
}
resetFns.clear()
resetTimer = null
}
export function createProxy<E extends Node>(
node: E,
options?: CreateProxyOptions,
): ProxyNode<E> {
const deps = new Map<string, Set<() => void>>()
const listenEventType = options?.listen ?? 'input'
const proxy = new Proxy(node, {
get(target, p, receiver) {
if (listenEventType && watchFn && typeof p === 'string') {
const key =
p === 'value' || p === 'valueAsNumber' || p === 'valueAsDate'
? 'value'
: p
const fn = watchFn
if (key === 'value') {
target.addEventListener(
listenEventType,
// wrap the function to avoid the default behavior be cancelled
// if the inline-function returns false
() => fn(),
)
;(target as Node as HTMLInputElement).form?.addEventListener(
'reset',
() => {
resetFns.add(fn)
if (resetTimer) return
resetTimer = setTimeout(resetTimeout)
},
)
}
let fns = deps.get(key)
if (!fns) {
fns = new Set()
deps.set(key, fns)
}
fns.add(fn)
}
const value = target[p as keyof E]
if (typeof value === 'function') {
return value.bind(target)
}
return value
},
set(target, p, value, receiver) {
target[p as keyof E] = value
if (typeof p === 'string') {
const fns = deps.get(p)
if (fns) {
for (const fn of fns) {
fn()
}
}
}
return true
},
})
return Object.assign(proxy, { node })
}
export type NodeChild = Node | { node: Node } | string | number
export type Properties<E extends Node> = Partial<{
[P in keyof E]?: E[P] extends object ? Partial<E[P]> : E[P]
}>
export type CreateElementOptions<E extends Node> = Properties<E> &
CreateProxyOptions
export type PartialCreateElement<E extends Node> = (
props?: CreateElementOptions<E>,
children?: NodeChild[],
) => ProxyNode<E>
/** @description higher-function, partially applied createHTMLElement */
export function genCreateHTMLElement<K extends keyof HTMLElementTagNameMap>(
tagName: K,
): PartialCreateElement<HTMLElementTagNameMap[K]> {
return (props, children) => createHTMLElement(tagName, props, children)
}
/** @description higher-function, partially applied createSVGElement */
export function genCreateSVGElement<K extends keyof SVGElementTagNameMap>(
tagName: K,
): PartialCreateElement<SVGElementTagNameMap[K]> {
return (props, children) => createSVGElement(tagName, props, children)
}
/** @alias h, html */
export function createHTMLElement<K extends keyof HTMLElementTagNameMap>(
tagName: K,
props?: CreateElementOptions<HTMLElementTagNameMap[K]>,
children?: NodeChild[],
) {
const node = document.createElement<K>(tagName)
applyAttrs(node, props, children)
return createProxy(node, props)
}
/** @alias s, svg */
export function createSVGElement<K extends keyof SVGElementTagNameMap>(
tagName: K,
props?: CreateElementOptions<SVGElementTagNameMap[K]>,
children?: NodeChild[],
) {
const node = document.createElementNS('http://www.w3.org/2000/svg', tagName)
applyAttrs(node, props, children)
return createProxy(node, props)
}
function applyAttrs<E extends ParentNode>(
node: E,
props?: Properties<E>,
children?: NodeChild[],
) {
if (props) {
for (let p in props) {
let value = props[p]
if (value !== null && typeof value === 'object' && p in node) {
Object.assign((node as any)[p], value)
} else {
;(node as any)[p] = value
}
}
}
if (children) {
for (const child of children) {
appendChild(node, child)
}
}
}
export function appendChild(
parent: ParentNode,
child: Node | { node: Node } | string | number,
) {
if (typeof child == 'string') {
parent.appendChild(document.createTextNode(child))
} else if (typeof child == 'number') {
parent.appendChild(document.createTextNode(String(child)))
} else if ('node' in child) {
parent.appendChild(child.node)
} else {
parent.appendChild(child)
}
}