-
Notifications
You must be signed in to change notification settings - Fork 10
/
dom-handlers.js
291 lines (275 loc) · 9.9 KB
/
dom-handlers.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
import { startRenderSubscription } from '.';
import {
findRenderableByVDOMPointer,
isPrimitiveElement,
} from './vdom-helpers';
import { diffType, propsDiffType, diffApplicationOrder } from './diff';
// map of eventHandlers that the ToDo app requires
const eventHandlersMap = {
onClick: 'click',
// for the `change` event to trigger, the user is required to leave the field and come back
// so it seems like React decided to use the `input` event under the hood
onChange: 'input',
onSubmit: 'submit',
};
const isEventHandlerProp = key => Object.keys(eventHandlersMap).includes(key);
const addEventHandler = (domElement, { key, value }) => {
domElement.addEventListener(eventHandlersMap[key], value);
};
const applyPropToHTMLElement = ({ key, value }, element) => {
if (isEventHandlerProp(key)) {
addEventHandler(element, { key, value });
return;
}
if (element[key] !== undefined) {
element[key] = value;
} else {
element.setAttribute(key, value);
}
};
// expects a JSX element and returns an HTML element
const renderTagElementToHtml = (
{ props: { children, ...props }, type },
renderedElementsMap,
) => {
const domElement = document.createElement(type);
Object.entries(props).forEach(([key, value]) => {
applyPropToHTMLElement({ key, value }, domElement);
});
if (children) {
const childrenAsDomElement = children.map(child =>
renderElementToHtml(child, renderedElementsMap),
);
childrenAsDomElement.forEach(childElement => {
if (childElement) {
domElement.appendChild(childElement);
}
});
}
return domElement;
};
const renderPrimitiveToHtml = ({ value }) => {
switch (typeof value) {
case 'string':
case 'number':
return document.createTextNode(value);
case 'undefined':
return;
case 'boolean':
return value ? renderPrimitiveToHtml('true') : undefined;
default:
throw new Error(`Type ${value} is not a known renderable type.`);
}
};
const renderElementToHtml = (element, renderedElementsMap) => {
const renderedElement = isPrimitiveElement(element)
? renderPrimitiveToHtml(element)
: renderTagElementToHtml(element, renderedElementsMap);
renderedElementsMap[element.VDOMPointer] = renderedElement;
return renderedElement;
};
/*
This is a helper function to retrieve the insertion point of an element within the
browser's DOM (which isn't trivial, as some elements such as `false` or `undefined` are part of the
renderable VDOM, but not part of the actual DOM).
Input
- {
renderedElementsMap: a map with keys VDOMPointer and values elements rendered in the browser
renderableVDOM: the renderableVDOM for the current update cycle
}
- VDOMPointer: the pointer to the element for which we want to find the next sibling
- parentPointer: the pointer to the parent of the VDOMPointer and the sibling
Output
- The next sibling of the VDOMPointer: a dom element currently rendered in the browser if found, undefined
if the node pointed by the VDOMPointer is the last rendered child of the parent
*/
const findNextSiblingOfVDOMPointer = (
{ renderedElementsMap, renderableVDOM },
VDOMPointer,
parentPointer,
) => {
/*
Given the following VDOM
<App>
<div>
<Component>
<div>
<Component>
<span>First child</span>
<span>Second child</span>
</Component>
<span>Hello world!</span>
</div>
</Component>
</div>
</App>
we get the following renderableVDOM (with VDOMPointer)
<div> [0]
<div> [0, 0, 0]
<span>First child</span> [0, 0, 0, 0, 0]
<span>Second child</span> [0, 0, 0, 0, 1]
<span>Hello world!</span> [0, 0, 0, 1]
</div>
</div>
So that the sibling of VDOMPointer [0, 0, 0, 1] in the renderableVDOM
is the span Second child with VDOMPointer [0, 0, 0, 0, 1]
*/
const parentElementFromRenderableVDOM = findRenderableByVDOMPointer(
renderableVDOM,
parentPointer,
);
const parentChildren = parentElementFromRenderableVDOM.props.children;
const childVDOMIndex = parentChildren.findIndex(
child => child.VDOMPointer === VDOMPointer,
);
let nextSiblingVDOMIndex = childVDOMIndex + 1;
let nextSibling;
// The next sibling could be a value that doesn't render to the DOM such as `false`
// or a sibling that has not yet been rendered to the DOM (if it was added in the same update cycle)
// so we need to continue searching for the first rendered one here
while (
nextSiblingVDOMIndex < parentChildren.length &&
nextSibling === undefined
) {
const nextSiblingFromVDOM =
parentElementFromRenderableVDOM.props.children[nextSiblingVDOMIndex];
if (nextSiblingFromVDOM) {
nextSibling = renderedElementsMap[nextSiblingFromVDOM.VDOMPointer];
}
nextSiblingVDOMIndex += 1;
}
return nextSibling;
};
/*
Function to apply to the browser's DOM a node added diff item
Input:
- {
renderedElementsMap: a map with keys VDOMPointer and values elements rendered in the browser
renderableVDOM: the renderableVDOM for the current update cycle
}
- VDOMPointer: the pointer to the position at which we want to insert the node
- node: the renderableVDOMElement we want to insert into the DOM
- parentPointer: the pointer to the parent of the VDOMPointer and the sibling
Side effects:
Updates the browser's DOM with the newly added node as well as the reference in the renderedElementsMap.
*/
const applyNodeAdded = (
{ renderedElementsMap, renderableVDOM },
{ VDOMPointer, payload: { node, parentPointer } },
) => {
// DON'T FORGET AFTER YOU HANDLED PROPS UPDATE
// Here we want to render the node from the payload to the DOM
// To do so, you will need to:
// 1. Create the right HTML element for that node
// 2. Find the right place to insert the node (ps: the provided `findNextSiblingOfVDOMPointer` function might come in handy)
// Careful, some primitive elements don't render anything to the DOM
// and don't forget to update the renderedElementsMap after the real DOM is updated.
};
/*
Function to apply to the browser's DOM a primitive node update diff item
Input:
- {
renderedElementsMap: a map with keys VDOMPointer and values elements rendered in the browser
}
- VDOMPointer: the pointer to the position at which we want to insert the node
- newElement: the renderableVDOMElement primitive element we want to update the browser's DOM with
Side effects:
Updates the browser's DOM
*/
const applyPrimitiveNodeUpdate = (
{ renderedElementsMap },
{ VDOMPointer, payload: { newElement } },
) => {
renderedElementsMap[VDOMPointer].nodeValue = newElement.value;
};
/*
Function to apply to the browser's DOM a props diff item
Input:
- {
renderedElementsMap: a map with keys VDOMPointer and values elements rendered in the browser
}
- VDOMPointer: the pointer to the element which props we want to update
- propsChanged: the list of prop diff item we want to apply
Side effects:
Updates the browser's DOM with the applied props
*/
const applyProps = (
{ renderedElementsMap },
{ VDOMPointer, payload: propsChanged },
) => {
// Loop through all prop diff items
Object.entries(propsChanged).forEach(
// Extracting for each the propDiffType (updated or removed)
// As well as the oldValue and the newValue for the given prop
// Example of what each of those values could be:
// [key = 'id', [propdDiffType = 'updated', { oldValue: 'main-title', newValue: 'sub-title' }]]
([key, [propDiffType, { oldValue, newValue }]]) => {
if (propDiffType === propsDiffType.updated) {
if (isEventHandlerProp(key)) {
// DON'T FORGET
// How to handle event listeners?
// What do we need to do when an event listener callback was updated?
}
// START HERE
// How to update the attribute on the element
// Here we are trying to update the attribute `key` of the element at VDOMPointer within the browser's DOM
// from it's old value `oldValue` to its new value `newValue`
}
},
);
};
// A map of applicators to simplify application
const diffApplicators = {
[diffType.nodeRemoved]: () => {},
[diffType.nodeAdded]: applyNodeAdded,
[diffType.nodeReplaced]: () => {},
[diffType.primitiveNodeUpdate]: applyPrimitiveNodeUpdate,
[diffType.props]: applyProps,
};
/*
Function to apply the diff to the browser's DOM
Input:
- diff: the diff we want to apply to the DOM
- renderedElementsMap: a map with keys VDOMPointer and values elements rendered in the browser
- renderableVDOM: the renderableVDOM for the current update cycle
Side effects:
Updates the browser's DOM as well as the references in the renderedElementsMap.
*/
const applyDiff = (diff, renderedElementsMap, renderableVDOM) => {
// Sort diff item so applying them is safe
const sortedDiffs = diff.sort(
(a, b) =>
diffApplicationOrder.indexOf(a.type) -
diffApplicationOrder.indexOf(b.type),
);
// Apply them all!
sortedDiffs.forEach(diffItem =>
diffApplicators[diffItem.type](
{ renderedElementsMap, renderableVDOM },
diffItem,
),
);
};
const createRoot = rootElement => ({
rootElement,
render: rootChild => {
let renderedElementsMap = {};
// subscribes to DOM changes which are driven by state changes
startRenderSubscription(rootChild, (renderableVDOM, diff) => {
if (Object.keys(renderedElementsMap).length === 0) {
const rootChildAsHTML = renderElementToHtml(
renderableVDOM,
renderedElementsMap,
);
rootElement.appendChild(rootChildAsHTML);
} else {
// Now on update instead of replacing the whole browser's DOM
// we will try to only do targeted updates of it
applyDiff(diff, renderedElementsMap, renderableVDOM);
}
});
},
});
export const DOMHandlers = {
createRoot,
};