-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtiny.js
35 lines (34 loc) · 1.99 KB
/
tiny.js
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
(() => {
const assignDeep = (elm, props) => Object.entries(props).forEach(([key, value]) =>
typeof value === 'object' ? assignDeep(elm[key], value) : Object.assign(elm, {[key]: value}))
const tagNames = [
'a', 'abbr', 'address', 'area', 'article', 'aside', 'audio', 'b', 'base',
'bdi', 'bdo', 'blockquote', 'body', 'br', 'button', 'canvas', 'caption',
'cite', 'code', 'col', 'colgroup', 'data', 'datalist', 'dd', 'del',
'details', 'dfn', 'dialog', 'div', 'dl', 'dt', 'em', 'embed', 'fieldset',
'figcaption', 'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5',
'h6', 'head', 'header', 'hr', 'html', 'i', 'iframe', 'img', 'input', 'ins',
'kbd', 'label', 'legend', 'li', 'link', 'main', 'map', 'mark', 'meta',
'meter', 'nav', 'noscript', 'object', 'ol', 'optgroup', 'option', 'output',
'p', 'param', 'picture', 'pre', 'progress', 'q', 'rp', 'rt', 'ruby', 's',
'samp', 'script', 'section', 'select', 'small', 'source', 'span', 'strong',
'style', 'sub', 'summary', 'sup', 'table', 'tbody', 'td', 'template',
'textarea', 'tfoot', 'th', 'thead', 'time', 'title', 'tr', 'track', 'u',
'ul', 'var', 'video', 'wbr'
].forEach(tag => window[tag] = function(...args) {
const props = typeof args[0] == 'object' && !(args[0] instanceof HTMLElement) ? args.shift() : null
const elm = document.createElement(tag)
props && assignDeep(elm, props)
elm.append(...args.map(a => typeof a == 'string' ? document.createTextNode(a) : a))
return elm
})
window['$'] = selector => document.querySelector(selector)
window['$$'] = selector => Array.from(document.querySelectorAll(selector))
window['createState'] = state => {
const appState = { ...state }
appState._updates = Object.fromEntries(Object.keys(state).map(s => [s, []]))
appState._update = s => appState._updates[s].forEach(u => u())
appState.addUpdate = (s, u) => appState._updates[s].push(u)
return new Proxy(appState, {set(o, p, v) {o[p] = v; o._update(p); return true}})
}
})()