1 | (function (global, factory) {
|
2 | typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('preact')) :
|
3 | typeof define === 'function' && define.amd ? define(['preact'], factory) :
|
4 | (factory(global.preact));
|
5 | }(this, (function (preact) { 'use strict';
|
6 |
|
7 |
|
8 |
|
9 | var ATTR_KEY = '__preactattr_';
|
10 |
|
11 |
|
12 |
|
13 | |
14 |
|
15 |
|
16 |
|
17 | function createReactElement(component) {
|
18 | return {
|
19 | type: component.constructor,
|
20 | key: component.key,
|
21 | ref: null,
|
22 | props: component.props
|
23 | };
|
24 | }
|
25 |
|
26 | |
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | function createReactDOMComponent(node) {
|
37 | var childNodes = node.nodeType === Node.ELEMENT_NODE ? Array.from(node.childNodes) : [];
|
38 |
|
39 | var isText = node.nodeType === Node.TEXT_NODE;
|
40 |
|
41 | return {
|
42 |
|
43 | _currentElement: isText ? node.textContent : {
|
44 | type: node.nodeName.toLowerCase(),
|
45 | props: node[ATTR_KEY]
|
46 | },
|
47 | _renderedChildren: childNodes.map(function (child) {
|
48 | if (child._component) {
|
49 | return updateReactComponent(child._component);
|
50 | }
|
51 | return updateReactComponent(child);
|
52 | }),
|
53 | _stringText: isText ? node.textContent : null,
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | _inDevTools: false,
|
62 | node: node
|
63 | };
|
64 | }
|
65 |
|
66 | |
67 |
|
68 |
|
69 |
|
70 |
|
71 | function typeName(element) {
|
72 | if (typeof element.type === 'function') {
|
73 | return element.type.displayName || element.type.name;
|
74 | }
|
75 | return element.type;
|
76 | }
|
77 |
|
78 | |
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | function createReactCompositeComponent(component) {
|
89 | var _currentElement = createReactElement(component);
|
90 | var node = component.base;
|
91 |
|
92 | var instance = {
|
93 |
|
94 | getName: function getName() {
|
95 | return typeName(_currentElement);
|
96 | },
|
97 |
|
98 | _currentElement: createReactElement(component),
|
99 | props: component.props,
|
100 | state: component.state,
|
101 | forceUpdate: component.forceUpdate && component.forceUpdate.bind(component),
|
102 | setState: component.setState && component.setState.bind(component),
|
103 |
|
104 |
|
105 | node: node
|
106 | };
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | instance._instance = component;
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | if (component._component) {
|
118 | instance._renderedComponent = updateReactComponent(component._component);
|
119 | } else {
|
120 |
|
121 |
|
122 | instance._renderedComponent = updateReactComponent(node);
|
123 | }
|
124 |
|
125 | return instance;
|
126 | }
|
127 |
|
128 | |
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | var instanceMap = typeof Map === 'function' && new Map();
|
136 |
|
137 | |
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | function updateReactComponent(componentOrNode) {
|
144 | var newInstance = componentOrNode instanceof Node ? createReactDOMComponent(componentOrNode) : createReactCompositeComponent(componentOrNode);
|
145 | if (instanceMap.has(componentOrNode)) {
|
146 | var inst = instanceMap.get(componentOrNode);
|
147 | Object.assign(inst, newInstance);
|
148 | return inst;
|
149 | }
|
150 | instanceMap.set(componentOrNode, newInstance);
|
151 | return newInstance;
|
152 | }
|
153 |
|
154 | function nextRootKey(roots) {
|
155 | return '.' + Object.keys(roots).length;
|
156 | }
|
157 |
|
158 | |
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 | function findRoots(node, roots) {
|
166 | Array.from(node.childNodes).forEach(function (child) {
|
167 | if (child._component) {
|
168 | roots[nextRootKey(roots)] = updateReactComponent(child._component);
|
169 | } else {
|
170 | findRoots(child, roots);
|
171 | }
|
172 | });
|
173 | }
|
174 |
|
175 | |
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 | function createDevToolsBridge() {
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 | var ComponentTree = {
|
196 | getNodeFromInstance: function getNodeFromInstance(instance) {
|
197 | return instance.node;
|
198 | },
|
199 | getClosestInstanceFromNode: function getClosestInstanceFromNode(node) {
|
200 | while (node && !node._component) {
|
201 | node = node.parentNode;
|
202 | }
|
203 | return node ? updateReactComponent(node._component) : null;
|
204 | }
|
205 | };
|
206 |
|
207 |
|
208 | var roots = {};
|
209 | findRoots(document.body, roots);
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 | var Mount = {
|
216 | _instancesByReactRootID: roots,
|
217 |
|
218 |
|
219 |
|
220 | _renderNewRootComponent: function _renderNewRootComponent() /* instance, ... */{}
|
221 | };
|
222 |
|
223 |
|
224 | var Reconciler = {
|
225 |
|
226 |
|
227 |
|
228 | mountComponent: function mountComponent() /* instance, ... */{},
|
229 | performUpdateIfNecessary: function performUpdateIfNecessary() /* instance, ... */{},
|
230 | receiveComponent: function receiveComponent() /* instance, ... */{},
|
231 | unmountComponent: function unmountComponent() /* instance, ... */{}
|
232 | };
|
233 |
|
234 |
|
235 | var componentAdded = function componentAdded(component) {
|
236 | var instance = updateReactComponent(component);
|
237 | if (isRootComponent(component)) {
|
238 | instance._rootID = nextRootKey(roots);
|
239 | roots[instance._rootID] = instance;
|
240 | Mount._renderNewRootComponent(instance);
|
241 | }
|
242 | visitNonCompositeChildren(instance, function (childInst) {
|
243 | childInst._inDevTools = true;
|
244 | Reconciler.mountComponent(childInst);
|
245 | });
|
246 | Reconciler.mountComponent(instance);
|
247 | };
|
248 |
|
249 |
|
250 | var componentUpdated = function componentUpdated(component) {
|
251 | var prevRenderedChildren = [];
|
252 | var instance = instanceMap.get(component);
|
253 | if (instance) {
|
254 | visitNonCompositeChildren(instance, function (childInst) {
|
255 | prevRenderedChildren.push(childInst);
|
256 | });
|
257 | }
|
258 |
|
259 |
|
260 | instance = updateReactComponent(component);
|
261 |
|
262 | Reconciler.receiveComponent(instance);
|
263 | visitNonCompositeChildren(instance, function (childInst) {
|
264 | if (!childInst._inDevTools) {
|
265 |
|
266 | childInst._inDevTools = true;
|
267 | Reconciler.mountComponent(childInst);
|
268 | } else {
|
269 |
|
270 | Reconciler.receiveComponent(childInst);
|
271 | }
|
272 | });
|
273 |
|
274 |
|
275 |
|
276 |
|
277 | prevRenderedChildren.forEach(function (childInst) {
|
278 | if (!document.body.contains(childInst.node)) {
|
279 | instanceMap.delete(childInst.node);
|
280 | Reconciler.unmountComponent(childInst);
|
281 | }
|
282 | });
|
283 | };
|
284 |
|
285 |
|
286 | var componentRemoved = function componentRemoved(component) {
|
287 | var instance = updateReactComponent(component);
|
288 | visitNonCompositeChildren(function (childInst) {
|
289 | instanceMap.delete(childInst.node);
|
290 | Reconciler.unmountComponent(childInst);
|
291 | });
|
292 | Reconciler.unmountComponent(instance);
|
293 | instanceMap.delete(component);
|
294 | if (instance._rootID) {
|
295 | delete roots[instance._rootID];
|
296 | }
|
297 | };
|
298 |
|
299 | return {
|
300 | componentAdded: componentAdded,
|
301 | componentUpdated: componentUpdated,
|
302 | componentRemoved: componentRemoved,
|
303 |
|
304 |
|
305 | ComponentTree: ComponentTree,
|
306 | Mount: Mount,
|
307 | Reconciler: Reconciler
|
308 | };
|
309 | }
|
310 |
|
311 | |
312 |
|
313 |
|
314 |
|
315 | function isRootComponent(component) {
|
316 |
|
317 | if (component._parentComponent || component.__u) {
|
318 |
|
319 | return false;
|
320 | }
|
321 | if (component.base.parentElement && component.base.parentElement[ATTR_KEY]) {
|
322 |
|
323 | return false;
|
324 | }
|
325 | return true;
|
326 | }
|
327 |
|
328 | |
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 | function visitNonCompositeChildren(component, visitor) {
|
336 | if (!component) return;
|
337 | if (component._renderedComponent) {
|
338 | if (!component._renderedComponent._component) {
|
339 | visitor(component._renderedComponent);
|
340 | visitNonCompositeChildren(component._renderedComponent, visitor);
|
341 | }
|
342 | } else if (component._renderedChildren) {
|
343 | component._renderedChildren.forEach(function (child) {
|
344 | visitor(child);
|
345 | if (!child._component) visitNonCompositeChildren(child, visitor);
|
346 | });
|
347 | }
|
348 | }
|
349 |
|
350 | |
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 | function initDevTools() {
|
364 | if (typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ === 'undefined') {
|
365 |
|
366 | return;
|
367 | }
|
368 |
|
369 |
|
370 | var bridge = createDevToolsBridge();
|
371 |
|
372 | var nextAfterMount = preact.options.afterMount;
|
373 | preact.options.afterMount = function (component) {
|
374 | bridge.componentAdded(component);
|
375 | if (nextAfterMount) nextAfterMount(component);
|
376 | };
|
377 |
|
378 | var nextAfterUpdate = preact.options.afterUpdate;
|
379 | preact.options.afterUpdate = function (component) {
|
380 | bridge.componentUpdated(component);
|
381 | if (nextAfterUpdate) nextAfterUpdate(component);
|
382 | };
|
383 |
|
384 | var nextBeforeUnmount = preact.options.beforeUnmount;
|
385 | preact.options.beforeUnmount = function (component) {
|
386 | bridge.componentRemoved(component);
|
387 | if (nextBeforeUnmount) nextBeforeUnmount(component);
|
388 | };
|
389 |
|
390 |
|
391 | __REACT_DEVTOOLS_GLOBAL_HOOK__.inject(bridge);
|
392 |
|
393 | return function () {
|
394 | preact.options.afterMount = nextAfterMount;
|
395 | preact.options.afterUpdate = nextAfterUpdate;
|
396 | preact.options.beforeUnmount = nextBeforeUnmount;
|
397 | };
|
398 | }
|
399 |
|
400 | initDevTools();
|
401 |
|
402 | })));
|
403 |
|