-
Notifications
You must be signed in to change notification settings - Fork 1
/
ToyReact.js
230 lines (227 loc) · 6.24 KB
/
ToyReact.js
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
let childrenSymbol = Symbol('children');
class ElementWrapper {
constructor(type) {
this.type = type;
this.props = Object.create(null);
this[childrenSymbol] = [];
this.children = [];
}
setAttribute(name, value) {
this.props[name] = value;
}
// get children() {
// return this.children.map(child => child.vdom);
// }
appendChild(vchild) {
this[childrenSymbol].push(vchild);
this.children.push(vchild.vdom)
}
get vdom() {
return this;
}
mountTo(range) {
this.range = range;
let placeholder = document.createComment("placeholder");
let endRange = document.createRange();
endRange.setStart(range.endContainer, range.endOffset);
endRange.setEnd(range.endContainer, range.endOffset);
endRange.insertNode(placeholder);
range.deleteContents();
let element = document.createElement(this.type);
for (let name in this.props) {
let value = this.props[name];
if (name.match(/^on([\s\S]+)$/)) {
let eventName = RegExp.$1.replace(/^[\s\S]/, s => s.toLowerCase());
element.addEventListener(eventName, value);
}
if (name === "className") {
element.setAttribute("class", value);
}
element.setAttribute(name, value);
}
for (let child of this.children) {
let range = document.createRange();
if (element.children.length) {
range.setStartAfter(element.lastChild);
range.setEndAfter(element.lastChild);
} else {
range.setStart(element, 0);
range.setEnd(element, 0);
}
child.mountTo(range);
}
range.insertNode(element);
}
}
class TextWrapper {
constructor(content) {
this.root = document.createTextNode(content);
this.type = '#text';
this.children = [];
this.props = Object.create(null);
}
mountTo(range) {
this.range = range;
range.deleteContents();
range.insertNode(this.root);
}
get vdom() {
return this;
}
}
export class Component {
constructor() {
this.children = [];
this.props = Object.create(null);
}
get type() {
return this.constructor.name;
}
setAttribute(name, value) {
this.props[name] = value;
this[name] = value;
}
mountTo(range) {
this.range = range;
this.update();
}
update() {
let vdom = this.vdom;
if (this.oldVdom) {
const isSameNode = (node1, node2) => {
if (node1.type !== node2.type) {
return false;
}
for (let name in node1.props) {
// if (typeof node1.props[name] === "function"
// && typeof node2.props[name] === "function"
// && node1.props[name].toString() === node2.props[name].toString()) {
// continue;
// }
if (typeof node1.props[name] === "object"
&& typeof node2.props[name] === "object"
&& JSON.stringify(node1.props[name]) === JSON.stringify(node2.props[name])) {
continue;
}
if (node1.props[name] !== node2.props[name]) {
return false;
}
}
if (Object.keys(node1.props).length !== Object.keys(node2.props).length) {
return false;
}
return true;
}
const isSameTree = (node1, node2) => {
if (!isSameNode(node1, node2)) {
return false;
}
if (node1.children.length !== node2.children.length) {
return false;
}
for (let i = 0; i < node1.children.length; i++) {
if (!isSameTree(node1.children[i], node2.children[i])) {
return false;
}
}
return true;
};
const replace = (newTree, oldTree, indent) => {
console.log(indent + 'new:', newTree);
console.log(indent + 'old:', oldTree);
if (isSameTree(newTree, oldTree)) {
console.log("all the same");
return;
}
if (!isSameNode(newTree, oldTree)) {
console.log("all different");
newTree.mountTo(oldTree.range);
} else {
for (let i = 0; i < newTree.children.length; i++) {
replace(newTree.children[i], oldTree.children[i], ' ' + indent);
}
}
}
replace(vdom, this.oldVdom, '');
} else {
vdom.mountTo(this.range);
}
this.oldVdom = vdom;
}
get vdom() {
return this.render().vdom;
}
appendChild(vchild) {
return this.children.push(vchild);
}
setState(state) {
const merge = (oldState, newState) => {
for (let p in newState) {
if (typeof newState[p] === "object" && newState[p] !== null) {
if (typeof oldState[p] !== "object") {
if (newState[p] instanceof Array) {
oldState[p] = [];
} else {
oldState[p] = {};
}
}
merge(oldState[p], newState[p]);
} else {
oldState[p] = newState[p];
}
}
};
if (!this.state && state) {
this.state = {};
}
merge(this.state, state);
this.update();
}
}
export const ToyReact = {
createElement(type, attributes, ...children) {
let element;
if (typeof type === 'string') {
element = new ElementWrapper(type);
} else {
element = new type();
}
for (let name in attributes) {
element.setAttribute(name, attributes[name]);
}
const insertChildren = (children) => {
for (let child of children) {
if (typeof child === "object" && child instanceof Array) {
insertChildren(child)
} else {
if (child === null || child === void 0) {
child = "";
}
if (!(child instanceof Component)
&& !(child instanceof ElementWrapper)
&& !(child instanceof TextWrapper)
) {
child = String(child);
}
if (typeof child === "string") {
child = new TextWrapper(child);
}
element.appendChild(child);
}
}
}
insertChildren(children);
return element;
},
render(vdom, element) {
let range = document.createRange();
if (element.children.length) {
range.setStartAfter(element.lastChild);
range.setEndAfter(element.lastChild);
} else {
range.setStart(element, 0);
range.setEnd(element, 0);
}
vdom.mountTo(range);
}
}