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