forked from rohanchandra/javascript-terminal
-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
242 lines (239 loc) · 5.58 KB
/
index.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
import React from 'react';
import {
TouchableOpacity,
StyleSheet,
Alert,
WebView,
ActivityIndicator,
View,
Platform,
} from 'react-native';
import PropTypes from 'prop-types';
const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
android:
'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
});
const styles = StyleSheet.create({
container: {
flex: 1,
},
terminal: {
flex: 1,
},
loading: {
position: 'absolute',
top: 0,
bottom: 0,
left: 0,
right: 0,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#222222CC',
},
});
export default class Terminal extends React.Component {
static propTypes = {
commandMapping: PropTypes.shape({}).isRequired,
onReady: PropTypes.func,
style: PropTypes.shape({}),
fontSize: PropTypes.number,
}
static defaultProps = {
commandMapping: {
print: {
optDef: {},
function: (Terminal, opts) => {
return Terminal.OutputFactory.makeTextOutput(
opts
.join(' '),
);
},
},
alert: {
function: (Terminal, opts) => {
Alert.alert(
opts
.join(' '),
);
},
optDef: {},
},
},
onReady: (Terminal) => {
Terminal.OutputFactory.makeTextOutput(
'⚛️ Welcome to React Native!',
);
Terminal.OutputFactory.makeTextOutput('');
Terminal.OutputFactory.makeTextOutput(
'✍️ To get started, edit App.js',
);
Terminal.OutputFactory.makeTextOutput('');
Terminal.OutputFactory.makeTextOutput(
Platform.OS === 'ios' ? 'Press Cmd+R to reload, Cmd+D or shake for dev menu' : 'Shake or press menu button for dev menu',
);
Terminal.OutputFactory.makeTextOutput('');
// Terminal.OutputFactory.makeErrorOutput({
// source: 'hello',
// type: 'world',
// });
},
style: styles.container,
fontSize: 14,
}
constructor(nextProps) {
super(nextProps);
this.__onMessage = this.__onMessage.bind(this);
this.__makeTextOutput = this.__makeTextOutput.bind(this);
this.state = {
ready: false,
TerminalInterface: {
OutputFactory: {
makeTextOutput: str => this.__makeTextOutput(str),
makeErrorOutput: err => this.__makeErrorOutput(err),
},
},
};
}
// TODO: de-promisify
__handleAction(type, data) {
const {
commandMapping,
onReady,
} = this.props;
const {
// XXX: This naming convention exagerrates the point that
// we're making a bridge between the native environment
// and the WebView.
TerminalInterface: Terminal,
ready,
} = this.state;
if (type === 'ACTION_TYPE_READY') {
if (!ready) {
return new Promise(resolve => this.setState(
{
ready: true,
},
resolve,
))
.then(() => onReady(Terminal));
}
return Promise.reject(
new Error(
'Terminal is already initialized!',
),
);
}
if (!ready) {
return Promise.reject(
new Error(
`Received an action before the Terminal was initialized!`,
),
);
} else if (type === 'ACTION_TYPE_COMMAND') {
const {
key,
opts,
} = data;
const {
function: nativeFunction,
} = (commandMapping[key] || {});
if (nativeFunction) {
return Promise.resolve(
nativeFunction(
Terminal,
opts,
),
);
}
return Promise.reject(
new Error(
`Failed to resolve native handler for "${key}"!`,
),
);
}
return Promise.reject(
new Error(
`Unrecognized action "${type}"!`,
),
);
}
__makeTextOutput(str = '') {
return this.refs.terminal.injectJavaScript(
`window.addOutput("${str}");`,
);
}
__makeErrorOutput(err) {
return this.refs.terminal.injectJavaScript(
`window.addErrorOutput(${JSON.stringify(err)});`,
);
}
__onMessage(e) {
const {
type,
data,
} = JSON.parse(e.nativeEvent.data);
if (!type) {
return Promise.reject(
new Error(
`Received an invalid action. Exepcted truthy type, received "${type}".`,
),
);
}
return this.__handleAction(type, data);
}
__serializeCommandMapping(commandMapping = {}) {
return Object.entries(commandMapping)
.reduce(
(obj, [key, { optDef }]) => {
return {
...obj,
[key]: {
optDef,
},
};
},
{},
);
}
render() {
const {
style,
commandMapping,
fontSize,
} = this.props;
const {
ready,
} = this.state;
return (
<View
style={style || styles.container}
>
<WebView
ref="terminal"
style={styles.container}
source={{
html: require('./demo-rn/terminal.min.js')(
// TODO: requires serialiation functions
this.__serializeCommandMapping(
commandMapping,
),
`${fontSize}pt`,
),
}}
originWhitelist={['*']}
onMessage={this.__onMessage}
/>
{(!ready) && (
<View
style={styles.loading}
>
<ActivityIndicator
/>
</View>
)}
</View>
);
}
}