-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
60 lines (52 loc) · 1.16 KB
/
index.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
import { createRenderer } from '../runtime-core'
import { isOn } from '../shared'
function createElement(type: string) {
return document.createElement(type)
}
function createTextNode(text: string) {
return document.createTextNode(text)
}
function setElementText(el: HTMLElement, text) {
el.textContent = text
}
function patchProp(el: HTMLElement, key: string, prevProp, nextProp) {
// 2.1 事件
if (isOn(key)) {
const event = key.slice(2).toLowerCase()
el.addEventListener(event, nextProp)
}
// 2.2 属性
else {
if (!nextProp) {
el.removeAttribute(key)
} else {
el.setAttribute(key, nextProp)
}
}
}
function insert(
child: HTMLElement,
parent: HTMLElement,
anchor: Node | null = null
) {
parent.insertBefore(child, anchor)
}
function remove(el: HTMLElement) {
const parent = el.parentNode
if (parent) {
parent.removeChild(el)
}
}
const renderer: any = createRenderer({
createElement,
setElementText,
createTextNode,
patchProp,
insert,
remove
})
export * from '../runtime-core'
// 暴露 `DOM` 的操作 `API` 的渲染器
export function createApp(...args) {
return renderer.createApp(...args)
}