-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoy-react.js
More file actions
279 lines (253 loc) · 6.66 KB
/
toy-react.js
File metadata and controls
279 lines (253 loc) · 6.66 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/**
* ElementWrapper 和 TextWrapper 是基本单位
* Component 是 ElementWrapper 和 TextWrapper 的集合
*/
const RENDER_TO_DOM = Symbol('render to dom');
/**
* 更新节点的内容,防止吞并
* @param {Range} range 要更新的range
* @param {HTMLElement} node 节点
*/
const replaceContent = (range, node) => {
// 先插入 (在 Range 的起点处插入一个节点)
range.insertNode(node);
// 删除新节点之后的内容
range.setStartAfter(node);
range.deleteContents();
// 修正range
range.setStartBefore(node);
range.setEndAfter(node);
}
export class Component {
constructor() {
this._root = null;
this.props = Object.create(null);
this.children = [];
this._range = null;
this.state = Object.create(null);
}
setAttribute(name, value) {
this.props[name] = value;
}
appendChild(component) {
this.children.push(component);
}
/**
* 设置(更新)state
* @param {object}} newState 新state
*/
setState(newState) {
// state 格式化
if (this.state === null && typeof this.state !== 'object') {
this.state = {};
this.update();
return;
}
// 合并 state
const merge = (oldState, newState, deep = true) => {
for (let p in newState) {
if (deep && oldState[p] !== null && typeof oldState[p] === 'object') {
// 递归
merge(oldState[p], newState[p]);
}
oldState[p] = newState[p];
}
};
merge(this.state, newState);
// 更新
this.update();
}
// 虚拟dom树
get vdom() {
// 递归
return this.render().vdom;
}
[RENDER_TO_DOM](range) {
// 下次更新的那一刻 _range, _vdom 都属于(充当)旧数据了
this._range = range;
this._vdom = this.vdom;
this._vdom[RENDER_TO_DOM](range);
}
/**
* 更新, diff
*/
update() {
/**
* 对比新旧节点是否一致 (diff算法)
* @param {ElementWrapper | TextWrapper} oldNode
* @param {ElementWrapper | TextWrapper} newNode
*/
const isSameNode = (oldNode, newNode) => {
// 类型不同
if (oldNode.type !== newNode.type) {
return false;
}
// 属性字段不同
if (
Object.keys(oldNode.props).length > Object.keys(newNode.props).length
) {
return false;
}
// 属性值不同
for (let name in newNode.props) {
if (oldNode.props[name] !== newNode.props[name]) {
return false;
}
}
// 如果是文本节点
if (newNode.type === '#text') {
// 内容不同
if (oldNode.content !== newNode.content) {
return false;
}
}
return true;
};
/**
* 更新节点
* @param {ElementWrapper | TextWrapper}} oldNode 旧的节点
* @param {ElementWrapper | TextWrapper} newNode 新的节点
*/
const update = (oldNode, newNode) => {
if (!isSameNode(oldNode, newNode)) {
newNode[RENDER_TO_DOM](oldNode._range);
return false;
}
newNode._range = oldNode._range;
const newVchildren = newNode.vchildren;
const oldVchildren = oldNode.vchildren;
if (!newVchildren || !newVchildren.length) {
return;
}
let tailRange = oldVchildren[oldVchildren.length - 1]._range;
// 循环&递归对比children
newVchildren.forEach((newVchild, i) => {
const oldVchild = oldVchildren[i];
if (i < oldVchildren.length) {
// 递归调用
update(oldVchild, newVchild);
} else {
const range = document.createRange();
range.setStart(tailRange.endContainer, tailRange.endOffset);
range.setEnd(tailRange.endContainer, tailRange.endOffset);
newVchild[RENDER_TO_DOM](range);
tailRange = range;
}
});
};
const vdom = this.vdom;
update(this._vdom, vdom);
this._vdom = vdom;
}
}
/**
* 非文本组件
*/
class ElementWrapper extends Component {
constructor(type) {
super();
this.type = type;
}
get vdom() {
this.vchildren = this.children.map((child) => child.vdom);
// this 上包含 type,props,children
return this;
}
[RENDER_TO_DOM](range) {
this._range = range;
const root = document.createElement(this.type);
for (let name in this.props) {
const value = this.props[name];
if (name.match(/^on([\s\S]+)$/)) {
let eventName = RegExp.$1;
eventName = eventName.replace(/^([\s\S])/, (c) => c.toLowerCase());
root.addEventListener(eventName, value);
} else if (name === 'className') {
root.setAttribute('class', value);
}
root.setAttribute(name, value);
}
if (!this.vchildren) {
this.vchildren = this.children.map((child) => child.vdom);
}
for (let child of this.vchildren) {
const childRange = document.createRange();
// 在 this._range 最后 append 新的 range (of component);
childRange.setStart(root, root.childNodes.length);
childRange.setEnd(root, root.childNodes.length);
// 添加真实的dom
child[RENDER_TO_DOM](childRange);
}
replaceContent(range, root);
}
}
/**
* 文本组件
*/
class TextWrapper extends Component {
constructor(content) {
super();
this.type = '#text';
this.content = content;
}
get vdom() {
// this 上包含 type 和 content
return this;
}
/**
* 渲染到dom
* @param {Range}} range new range
*/
[RENDER_TO_DOM](range) {
this._range = range;
const root = document.createTextNode(this.content);
replaceContent(range, root);
}
}
/**
*
* @param {String | Component} type 标签类型
* @param {Object} attrs 属性
* @param {...any} children 子组件
*/
export const createElement = (type, attrs, ...children) => {
let ele;
if (typeof type === 'string') {
ele = new ElementWrapper(type);
} else {
ele = new type();
}
for (let p in attrs) {
ele.setAttribute(p, attrs[p]);
}
const insertChildren = (children) => {
for (let child of children) {
if (child == null) {
continue;
}
if (typeof child === 'string') {
child = new TextWrapper(child);
}
// {this.children}
if (Array.isArray(child)) {
insertChildren(child);
} else {
ele.appendChild(child);
}
}
};
insertChildren(children);
return ele;
};
/**
* 渲染开始~
* @param {Component} component 组件
* @param {HtmlElement} parentNode 根节点
*/
export const render = (component, parentNode) => {
const range = document.createRange();
range.setStart(parentNode, 0);
range.setEnd(parentNode, parentNode.childNodes.length);
range.deleteContents();
component[RENDER_TO_DOM](range);
};