forked from facebook/react-devtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.js
97 lines (82 loc) · 2.46 KB
/
container.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
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/
'use strict';
var React = require('react');
var ReactDOM = require('react-dom');
var invariant = require('assert');
var nullthrows = require('nullthrows').default;
var installGlobalHook = require('../../backend/installGlobalHook');
window.React = React;
var Panel = require('../../frontend/Panel');
var target = document.getElementById('target');
invariant(target instanceof HTMLIFrameElement);
var appSrc = target.getAttribute('data-app-src') || '../../test/example/build/target.js';
var devtoolsSrc = target.getAttribute('data-devtools-src') || './build/backend.js';
var win = target.contentWindow;
installGlobalHook(win);
var iframeSrc = document.getElementById('iframe-src');
if (iframeSrc) {
win.document.documentElement.innerHTML = iframeSrc.textContent.replace(/>/g, '>');
}
window.addEventListener('keydown', function(e) {
const body = nullthrows(document.body);
if (e.altKey && e.keyCode === 68) { // Alt + D
if (body.className === 'devtools-bottom') {
body.className = 'devtools-right';
} else {
body.className = 'devtools-bottom';
}
}
});
var config = {
alreadyFoundReact: true,
showHiddenThemes: true,
inject(done) {
inject(devtoolsSrc, () => {
var wall = {
listen(fn) {
win.parent.addEventListener('message', evt => {
if (evt.source === win) {
fn(evt.data);
}
});
},
send(data) {
win.postMessage(data, '*');
},
};
done(wall);
});
},
};
function inject(src, done) {
if (!src || src === 'false') {
done();
return;
}
invariant(target instanceof HTMLIFrameElement);
var script = target.contentDocument.createElement('script');
script.src = src;
script.onload = done;
nullthrows(target.contentDocument.body).appendChild(script);
}
function injectMany(sources, done) {
if (sources.length === 1) {
inject(sources[0], done);
return;
}
inject(sources[0], () => injectMany(sources.slice(1), done));
}
var sources = appSrc.split('|');
injectMany(sources, () => {
var node = nullthrows(document.getElementById('container'));
ReactDOM.render(<Panel {...config} />, node);
});