-
Notifications
You must be signed in to change notification settings - Fork 58
/
vdom-my.js
306 lines (306 loc) · 10.9 KB
/
vdom-my.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import directive from './directive';
export function Fragment(props, ...children) {
return collect(children);
}
const ATTR_PROPS = '_props';
function collect(children) {
const ch = [];
const push = (c) => {
if (c !== null && c !== undefined && c !== '' && c !== false) {
ch.push((typeof c === 'function' || typeof c === 'object') ? c : `${c}`);
}
};
children && children.forEach(c => {
if (Array.isArray(c)) {
c.forEach(i => push(i));
}
else {
push(c);
}
});
return ch;
}
export function createElement(tag, props, ...children) {
const ch = collect(children);
if (typeof tag === 'string')
return { tag, props, children: ch };
else if (Array.isArray(tag))
return tag; // JSX fragments - babel
else if (tag === undefined && children)
return ch; // JSX fragments - typescript
else if (Object.getPrototypeOf(tag).__isAppRunComponent)
return { tag, props, children: ch }; // createComponent(tag, { ...props, children });
else if (typeof tag === 'function')
return tag(props, ch);
else
throw new Error(`Unknown tag in vdom ${tag}`);
}
;
const keyCache = new WeakMap();
export const updateElement = (element, nodes, component = {}) => {
// tslint:disable-next-line
if (nodes == null || nodes === false)
return;
const el = (typeof element === 'string' && element) ?
document.getElementById(element) || document.querySelector(element) : element;
nodes = directive(nodes, component);
render(el, nodes, component);
};
function render(element, nodes, parent = {}) {
// tslint:disable-next-line
if (nodes == null || nodes === false)
return;
nodes = createComponent(nodes, parent);
if (!element)
return;
const isSvg = element.nodeName === "SVG";
if (Array.isArray(nodes)) {
updateChildren(element, nodes, isSvg);
}
else {
updateChildren(element, [nodes], isSvg);
}
}
function same(el, node) {
// if (!el || !node) return false;
const key1 = el.nodeName;
const key2 = `${node.tag || ''}`;
return key1.toUpperCase() === key2.toUpperCase();
}
function update(element, node, isSvg) {
if (node['_op'] === 3)
return;
// console.assert(!!element);
isSvg = isSvg || node.tag === "svg";
if (!same(element, node)) {
element.parentNode.replaceChild(create(node, isSvg), element);
return;
}
!(node['_op'] & 2) && updateChildren(element, node.children, isSvg);
!(node['_op'] & 1) && updateProps(element, node.props, isSvg);
}
function updateChildren(element, children, isSvg) {
var _a, _b;
const old_len = ((_a = element.childNodes) === null || _a === void 0 ? void 0 : _a.length) || 0;
const new_len = (children === null || children === void 0 ? void 0 : children.length) || 0;
const len = Math.min(old_len, new_len);
for (let i = 0; i < len; i++) {
const child = children[i];
if (child['_op'] === 3)
continue;
const el = element.childNodes[i];
if (typeof child === 'string') {
if (el.textContent !== child) {
if (el.nodeType === 3) {
el.nodeValue = child;
}
else {
element.replaceChild(createText(child), el);
}
}
}
else if (child instanceof HTMLElement || child instanceof SVGElement) {
element.insertBefore(child, el);
}
else {
const key = child.props && child.props['key'];
if (key) {
if (el.key === key) {
update(element.childNodes[i], child, isSvg);
}
else {
// console.log(el.key, key);
const old = keyCache[key];
if (old) {
const temp = old.nextSibling;
element.insertBefore(old, el);
temp ? element.insertBefore(el, temp) : element.appendChild(el);
update(element.childNodes[i], child, isSvg);
}
else {
element.replaceChild(create(child, isSvg), el);
}
}
}
else {
update(element.childNodes[i], child, isSvg);
}
}
}
let n = ((_b = element.childNodes) === null || _b === void 0 ? void 0 : _b.length) || 0;
while (n > len) {
element.removeChild(element.lastChild);
n--;
}
if (new_len > len) {
const d = document.createDocumentFragment();
for (let i = len; i < children.length; i++) {
d.appendChild(create(children[i], isSvg));
}
element.appendChild(d);
}
}
export const safeHTML = (html) => {
const div = document.createElement('section');
div.insertAdjacentHTML('afterbegin', html);
return Array.from(div.children);
};
function createText(node) {
if ((node === null || node === void 0 ? void 0 : node.indexOf('_html:')) === 0) { // ?
const div = document.createElement('div');
div.insertAdjacentHTML('afterbegin', node.substring(6));
return div;
}
else {
return document.createTextNode(node !== null && node !== void 0 ? node : '');
}
}
function create(node, isSvg) {
// console.assert(node !== null && node !== undefined);
if ((node instanceof HTMLElement) || (node instanceof SVGElement))
return node;
if (typeof node === "string")
return createText(node);
if (!node.tag || (typeof node.tag === 'function'))
return createText(JSON.stringify(node));
isSvg = isSvg || node.tag === "svg";
const element = isSvg
? document.createElementNS("http://www.w3.org/2000/svg", node.tag)
: document.createElement(node.tag);
updateProps(element, node.props, isSvg);
if (node.children)
node.children.forEach(child => element.appendChild(create(child, isSvg)));
return element;
}
function mergeProps(oldProps, newProps) {
newProps['class'] = newProps['class'] || newProps['className'];
delete newProps['className'];
const props = {};
if (oldProps)
Object.keys(oldProps).forEach(p => props[p] = null);
if (newProps)
Object.keys(newProps).forEach(p => props[p] = newProps[p]);
return props;
}
export function updateProps(element, props, isSvg) {
// console.assert(!!element);
const cached = element[ATTR_PROPS] || {};
props = mergeProps(cached, props || {});
element[ATTR_PROPS] = props;
for (const name in props) {
const value = props[name];
// if (cached[name] === value) continue;
// console.log('updateProps', name, value);
if (name.startsWith('data-')) {
const dname = name.substring(5);
const cname = dname.replace(/-(\w)/g, (match) => match[1].toUpperCase());
if (element.dataset[cname] !== value) {
if (value || value === "")
element.dataset[cname] = value;
else
delete element.dataset[cname];
}
}
else if (name === 'style') {
if (element.style.cssText)
element.style.cssText = '';
if (typeof value === 'string')
element.style.cssText = value;
else {
for (const s in value) {
if (element.style[s] !== value[s])
element.style[s] = value[s];
}
}
}
else if (name.startsWith('xlink')) {
const xname = name.replace('xlink', '').toLowerCase();
if (value == null || value === false) {
element.removeAttributeNS('http://www.w3.org/1999/xlink', xname);
}
else {
element.setAttributeNS('http://www.w3.org/1999/xlink', xname, value);
}
}
else if (name.startsWith('on')) {
if (!value || typeof value === 'function') {
element[name] = value;
}
else if (typeof value === 'string') {
if (value)
element.setAttribute(name, value);
else
element.removeAttribute(name);
}
}
else if (/^id$|^class$|^list$|^readonly$|^contenteditable$|^role|-|^for$/g.test(name) || isSvg) {
if (element.getAttribute(name) !== value) {
if (value)
element.setAttribute(name, value);
else
element.removeAttribute(name);
}
}
else if (element[name] !== value) {
element[name] = value;
}
if (name === 'key' && value)
keyCache[value] = element;
}
if (props && typeof props['ref'] === 'function') {
window.requestAnimationFrame(() => props['ref'](element));
}
}
function render_component(node, parent, idx) {
const { tag, props, children } = node;
let key = `_${idx}`;
let id = props && props['id'];
if (!id)
id = `_${idx}${Date.now()}`;
else
key = id;
let asTag = 'section';
if (props && props['as']) {
asTag = props['as'];
delete props['as'];
}
if (!parent.__componentCache)
parent.__componentCache = {};
let component = parent.__componentCache[key];
if (!component || !(component instanceof tag) || !component.element) {
const element = document.createElement(asTag);
component = parent.__componentCache[key] = new tag(Object.assign(Object.assign({}, props), { children })).mount(element, { render: true });
}
else {
component.renderState(component.state);
}
if (component.mounted) {
const new_state = component.mounted(props, children, component.state);
(typeof new_state !== 'undefined') && component.setState(new_state);
}
updateProps(component.element, props, false);
return component.element;
}
function createComponent(node, parent, idx = 0) {
var _a;
if (typeof node === 'string')
return node;
if (Array.isArray(node))
return node.map(child => createComponent(child, parent, idx++));
let vdom = node;
if (node && typeof node.tag === 'function' && Object.getPrototypeOf(node.tag).__isAppRunComponent) {
vdom = render_component(node, parent, idx);
}
if (vdom && Array.isArray(vdom.children)) {
const new_parent = (_a = vdom.props) === null || _a === void 0 ? void 0 : _a._component;
if (new_parent) {
let i = 0;
vdom.children = vdom.children.map(child => createComponent(child, new_parent, i++));
}
else {
vdom.children = vdom.children.map(child => createComponent(child, parent, idx++));
}
}
return vdom;
}
//# sourceMappingURL=vdom-my.js.map