-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.html
More file actions
112 lines (104 loc) · 2.7 KB
/
index.html
File metadata and controls
112 lines (104 loc) · 2.7 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>diff-patch demo</title>
<style>
.red {
color: red
}
.black {
color: black;
}
</style>
<script src="./element.js"></script>
<script src="./diff.js"></script>
<script src="./patch.js"></script>
</head>
<body>
<div id="app"></div>
<div id="diff"></div>
<button onclick="Mount()">挂载</button>
<button onclick="Diff()">diff</button>
<button onclick="Patch()">patch</button>
<pre>
<code>
// 1、初次渲染
var vdom1 = createVdom('h1', {
class: 'red'
}, [createVdom('p', {
class: 'red'
}, ['test']), createVdom('p', {
class: 'red',
style: 'color:green'
}, ['test2']), createVdom('p', {
class: 'red',
style: 'color:green',
onClick: function () {
console.log('clicked')
},
name: 'test'
}, ['test3'])])
const el = vdom2Element(vdom1)
mount(el, document.querySelector('#app'))
</code>
</pre>
<pre>
<code>
// 2、diff-patch
var vdom2 = createVdom('h1', {
class: 'black'
}, [createVdom('p', {}, ['testtest']), createVdom('p', {}, ['test2test2']), createVdom('p', {}, ['test2'])])
const patches = diff(vdom1, vdom2)
console.log('patches', patches)
patch(el, patches)
</code>
</pre>
<script>
// 1、初次渲染
var vdom1 = createVdom('h1', {
class: 'red'
}, [createVdom('p', {
class: 'red'
}, ['test']), createVdom('p', {
class: 'red',
style: 'color:green'
}, ['test2']), createVdom('p', {
class: 'red',
style: 'color:green',
onClick: function () {
console.log('clicked')
},
name: 'test'
}, ['test3'])])
// const el = vdom2Element(vdom1)
// mount(el, document.querySelector('#app'))
// 2、diff-patch
var vdom2 = createVdom('h1', {
class: 'black'
}, [createVdom('p', {}, ['testtest']), createVdom('p', {}, ['test2test2']), createVdom('p', {}, [
'test3test3'
])])
// const patches = diff(vdom1, vdom2)
// console.log('patches', patches)
// patch(el, patches)
</script>
<script>
var el, patches
function Mount() {
el = vdom2Element(vdom1)
mount(el, document.querySelector('#app'))
}
function Diff() {
patches = diff(vdom1, vdom2)
document.querySelector('#diff').innerHTML = JSON.stringify(patches, '', 4)
console.dir(patches)
}
function Patch() {
patch(el, patches)
}
</script>
</body>
</html>