Skip to content

Commit 62b8082

Browse files
committed
refactor(runtime-dom): 重构patchProps函数
1 parent ea956bc commit 62b8082

File tree

5 files changed

+44
-35
lines changed

5 files changed

+44
-35
lines changed

packages/runtime-dom/src/index.ts

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
import { isOn } from '@coderwei-mini-vue3/shared'
33
import { createRenderer } from '@coderwei-mini-vue3/runtime-core'
44

5+
import { patchEvent } from './modules/event'
6+
import { patchClass } from './modules/class'
7+
import { patchStyle } from './modules/style'
8+
59
function createElement(type) {
610
// console.log('create el 操作', type)
711
const element = document.createElement(type)
@@ -23,15 +27,6 @@ function setElementText(el, text) {
2327
el.textContent = text
2428
}
2529

26-
// 处理浏览器端 元素style样式
27-
function patchStyle(el, value) {
28-
console.log(value)
29-
const { style } = el
30-
for (const key in value) {
31-
style[key] = value[key]
32-
}
33-
}
34-
3530
function patchProp(el, key, preValue, nextValue) {
3631
// preValue 之前的值
3732
// 为了之后 update 做准备的值
@@ -40,35 +35,11 @@ function patchProp(el, key, preValue, nextValue) {
4035
// console.log(`key: ${key} 之前的值是:${preValue}`)
4136

4237
if (isOn(key)) {
43-
// 添加事件处理函数的时候需要注意一下
44-
// 1. 添加的和删除的必须是一个函数,不然的话 删除不掉
45-
// 那么就需要把之前 add 的函数给存起来,后面删除的时候需要用到
46-
// 2. nextValue 有可能是匿名函数,当对比发现不一样的时候也可以通过缓存的机制来避免注册多次
47-
// 存储所有的事件函数
48-
const invokers = el._vei || (el._vei = {})
49-
const existingInvoker = invokers[key]
50-
if (nextValue && existingInvoker) {
51-
// patch
52-
// 直接修改函数的值即可
53-
existingInvoker.value = nextValue
54-
} else {
55-
const eventName = key.slice(2).toLowerCase()
56-
if (nextValue) {
57-
const invoker = (invokers[key] = nextValue)
58-
el.addEventListener(eventName, invoker)
59-
} else {
60-
el.removeEventListener(eventName, existingInvoker)
61-
invokers[key] = undefined
62-
}
63-
}
38+
patchEvent(el, key, nextValue)
6439
} else if (key === 'style') {
6540
patchStyle(el, nextValue)
6641
} else {
67-
if (nextValue === null || nextValue === undefined) {
68-
el.removeAttribute(key)
69-
} else {
70-
el.setAttribute(key, nextValue)
71-
}
42+
patchClass(el, key, nextValue)
7243
}
7344
}
7445

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function patchClass(el, key, nextValue) {
2+
if (nextValue === null || nextValue === undefined) {
3+
el.removeAttribute(key)
4+
} else {
5+
el.setAttribute(key, nextValue)
6+
}
7+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// 添加事件处理函数的时候需要注意一下
2+
// 1. 添加的和删除的必须是一个函数,不然的话 删除不掉
3+
// 那么就需要把之前 add 的函数给存起来,后面删除的时候需要用到
4+
// 2. nextValue 有可能是匿名函数,当对比发现不一样的时候也可以通过缓存的机制来避免注册多次
5+
// 存储所有的事件函数
6+
export function patchEvent(el: any, key: any, nextValue: any) {
7+
const invokers = el._vei || (el._vei = {})
8+
const existingInvoker = invokers[key]
9+
if (nextValue && existingInvoker) {
10+
// patch
11+
// 直接修改函数的值即可
12+
existingInvoker.value = nextValue
13+
} else {
14+
const eventName = key.slice(2).toLowerCase()
15+
if (nextValue) {
16+
const invoker = (invokers[key] = nextValue)
17+
el.addEventListener(eventName, invoker)
18+
} else {
19+
el.removeEventListener(eventName, existingInvoker)
20+
invokers[key] = undefined
21+
}
22+
}
23+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// 处理浏览器端 元素style样式
2+
export function patchStyle(el, value) {
3+
console.log(value)
4+
const { style } = el
5+
for (const key in value) {
6+
style[key] = value[key]
7+
}
8+
}

packages/runtime-dom/src/patchProps.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)