tag
、属性props
和子元素对象children
等构成的js对象。var vdom= {
tag: 'ol', // 标签名
props: { // 属性用对象存储键值对
id: 'ol-list'
},
children: [ // 子节点
{tag: 'li', props: {class: 'item'}, children: ["Item 1"]},
{tag: 'li', props: {class: 'item'}, children: ["Item 2"]},
{tag: 'li', props: {class: 'item'}, children: ["Item 3"]},
]
}
//对应HTML写法为:
<ol id='ol-list'>
<li class='item'>Item 1</li>
<li class='item'>Item 2</li>
<li class='item'>Item 3</li>
</ol>
// 1. 定义h函数,构造virtual dom
function h(tag, props, children) {
return {
tag,
props: props || {},
children: children
};
}
// 2. 遍历virtual dom对象,生成真实dom
function createElement(vdom) {
// 如果vdom是字符串或者数字类型,则创建文本节点,比如“Hello World”
if (typeof vdom === 'string' || typeof vdom === 'number') {
return doc.createTextNode(vdom);
}
const {tag, props, children} = vdom;
// 创建元素
const element = doc.createElement(tag);
// 属性赋值
setProps(element, props);
// 创建子元素
children.forEach(child => {
let dom = createElement(child)
element.appendChild(dom)
})
return element;
}
new virtualDom
与old virtualDom
的异同patch
{
type, //对应vdom元素的变化类型:新建、删除、替换和更新
vdom,
props: [{
type, //对应每个属性变化类型:更新和删除
key,
value
}],
children
}
//type对应的枚举值如下:
const nodePatchTypes = {
CREATE: 'create node',
REMOVE: 'remove node',
REPLACE: 'replace node',
UPDATE: 'update node'
}
const propPatchTypes = {
REMOVE: 'remove prop',
UPDATE: 'update prop'
}
let preVdom, state={num: 5};
function render(element) {
// 初始化的virtual dom
const vdom = view();
preVDom = vdom;
const dom = createElement(vdom);
element.appendChild(dom);
//state变化,更新dom
state.num += 1;
reRender(element);
}
function reRender(element){
const newVdom = view();
// 生成差异对象
const patchObj = diff(preVDom, newVDom);
preVDom = newVDom;
//给DOM打个补丁
patch(element, patchObj);
}
function diff(oldVDom, newVDom) {
// 新建 node
if (oldVDom == undefined) {
return {
type: nodePatchTypes.CREATE,
vdom: newVDom
}
}
// 删除 node
if (newVDom == undefined) {
return {
type: nodePatchTypes.REMOVE
}
}
// 替换 node
if (typeof oldVDom !== typeof newVDom || ((typeof oldVDom === 'string' || typeof oldVDom === 'number') && oldVDom !== newVDom) || oldVDom.tag !== newVDom.tag) {
return {
type: nodePatchTypes.REPLACE,
vdom: newVDom
}
}
// 更新 node
if (oldVDom.tag) {
// 比较 props 的变化
const propsDiff = diffProps(oldVDom, newVDom);
// 比较 children 的变化
const childrenDiff = diffChildren(oldVDom, newVDom);
// 如果 props 或者 children 有变化,才需要更新
if (propsDiff.length > 0 || childrenDiff.some( patchObj => (patchObj !== undefined) )) {
return {
type: nodePatchTypes.UPDATE,
props: propsDiff,
children: childrenDiff
}
}
}
}
// 比较 props 的变化
function diffProps(oldVDom, newVDom) {
const patches = [];
const allProps = {...oldVDom.props, ...newVDom.props};
// 获取新旧所有属性名后,再逐一判断新旧属性值
Object.keys(allProps).forEach((key) => {
const oldValue = oldVDom.props[key];
const newValue = newVDom.props[key];
// 删除属性
if (newValue == undefined) {
patches.push({
type: propPatchTypes.REMOVE,
key
});
}
// 更新属性
else if (oldValue == undefined || oldValue !== newValue) {
patches.push({
type: propPatchTypes.UPDATE,
key,
value: newValue
});
}
}
)
return patches;
}
// 比较 children 的变化
function diffChildren(oldVDom, newVDom) {
const patches = [];
// 获取子元素最大长度
const childLength = Math.max(oldVDom.children.length, newVDom.children.length);
// 遍历并diff子元素
for (let i = 0; i < childLength; i++) {
patches.push(diff(oldVDom.children[i], newVDom.children[i]));
}
return patches;
}
// 给 DOM 打个补丁
function patch(parent, patchObj, index=0) {
if (!patchObj) {
return;
}
// 新建元素
if (patchObj.type === nodePatchTypes.CREATE) {
return parent.appendChild(createElement(patchObj.vdom));
}
const element = parent.childNodes[index];
// 删除元素
if (patchObj.type === nodePatchTypes.REMOVE) {
return parent.removeChild(element);
}
// 替换元素
if (patchObj.type === nodePatchTypes.REPLACE) {
return parent.replaceChild(createElement(patchObj.vdom), element);
}
// 更新元素
if (patchObj.type === nodePatchTypes.UPDATE) {
const {props, children} = patchObj;
// 更新属性
patchProps(element, props);
// 更新子元素
children.forEach( (patchObj, i) => {
// 更新子元素时,需要将子元素的序号传入
patch(element, patchObj, i)
});
}
}
// 更新属性
function patchProps(element, props) {
if (!props) {
return;
}
props.forEach( patchObj => {
// 删除属性
if (patchObj.type === propPatchTypes.REMOVE) {
element.removeAttribute(patchObj.key);
}
// 更新或新建属性
else if (patchObj.type === propPatchTypes.UPDATE) {
element.setAttribute(patchObj.key, patchObj.value);
}
})
}
未完待续…