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