-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
159 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<style> | ||
.red { color: red; } | ||
.green { color: green; } | ||
</style> | ||
|
||
<body> | ||
<div id="app"></div> | ||
</body> | ||
|
||
<script> | ||
function h(tag, props, children) { | ||
return { | ||
tag, | ||
props, | ||
children, | ||
} | ||
} | ||
|
||
function mount(vnode, container) { | ||
const el = vnode.el = document.createElement(vnode.tag); | ||
vnode.parent = container | ||
// handle props | ||
if (vnode.props) { | ||
for (let key in vnode.props) { | ||
if (key.startsWith('on')) { // 事件绑定 | ||
const eventName = key.slice(2).toLowerCase(); | ||
el.addEventListener(eventName, vnode.props[key]); | ||
} else { | ||
el.setAttribute(key, vnode.props[key]); | ||
} | ||
} | ||
} | ||
// handle children | ||
if (vnode.children) { | ||
if (Array.isArray(vnode.children)) { | ||
vnode.children.forEach(child => { | ||
mount(child, el); | ||
}); | ||
} else { // text node | ||
el.textContent = vnode.children; | ||
} | ||
} | ||
|
||
container.appendChild(el); | ||
} | ||
|
||
const vdom = h('div', { class: 'red' }, [ | ||
h('span', null, 'hello') | ||
]) | ||
|
||
mount(vdom, document.getElementById('app')) | ||
|
||
function patch(n1, n2) { | ||
// there are lots of assumption and only handle simpler cases | ||
|
||
// patch tag | ||
if (n1.tag === n2.tag) { | ||
const el = n1.el | ||
// patch props | ||
const oldProps = n1.props || {} | ||
const nweProps = n2.props || {} | ||
for (const key in nweProps) { | ||
const oldValue = oldProps[key] | ||
const newValue = nweProps[key] | ||
if (oldValue !== newValue) { | ||
el.setAttribute(key, newValue) | ||
} | ||
} | ||
// patch children | ||
const oldChildren = n1.children | ||
const newChildren = n2.children | ||
if (typeof newChildren === 'string') { | ||
if (typeof oldChildren === 'string') { | ||
if (newChildren !== oldChildren) { | ||
el.textContent = newChildren | ||
} | ||
} else { | ||
// oldChildren is a array, but we can just override it | ||
el.textContent = newChildren | ||
} | ||
} else { | ||
if (typeof oldChildren === 'string') { | ||
// discard oldChildren | ||
el.innerHTML = '' | ||
// mount newChildren | ||
newChildren.forEach(child => { | ||
mount(child, el) | ||
}) | ||
} else { | ||
// both old and new is a array | ||
|
||
const commonLength = Math.min(oldChildren.length, newChildren.length) | ||
for (let i = 0; i < commonLength; i++) { | ||
patch(oldChildren[i], newChildren[i]) | ||
} | ||
if (newChildren.length > oldChildren.length) { | ||
// to new node, just mount it | ||
newChildren.slice(oldChildren.length).forEach(child => { | ||
mount(child, el) | ||
}) | ||
} else if (newChildren.length < oldChildren.length) { | ||
oldChildren.slice(newChildren.length).forEach(child => { | ||
// unmount old el | ||
el.removeChild(child.el) | ||
}) | ||
} | ||
} | ||
} | ||
} else { | ||
// ... replace | ||
|
||
// unmount old dom | ||
n1.parent.removeChild(n1.el) | ||
// mount new dom | ||
mount(n2, n1.parent) | ||
} | ||
} | ||
|
||
const vdom2 = h('div', { class: 'green' }, [ | ||
h('span', null, 'changed!') | ||
]) | ||
|
||
setTimeout(() => { | ||
patch(vdom, vdom2) | ||
}, 1000) | ||
|
||
</script> |
File renamed without changes.
File renamed without changes.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters