-
Notifications
You must be signed in to change notification settings - Fork 12
/
index2.html
306 lines (279 loc) · 8.79 KB
/
index2.html
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Hello React!</title>
<!--官方提供的 CDN 地址:-->
<script src="./common/js/react.development.v16.12.0.js"></script>
<script src="./common/js/react-dom.development.v16.12.0.js"></script>
<script src="./common/js/browser-polyfill.5.8.38.js"></script>
<script src="./common/js/browser.5.8.38.js"></script>
</head>
<style>
* {
margin: 0;
padding: 0;
}
#text {
/* width: 500px; */
/* height: 300px; */
}
.left-box {
float: left;
width: 45%;
}
.right-box {
float: right;
width: 45%;
height: 700px;
/* background: red; */
}
textarea {
width: 100%;
height: 400px;
}
p {
margin: 20px;
}
button {
width: 80px;
padding: 10px;
}
</style>
<body>
<div class="box">
<div class='left-box'>
<p>代码编辑窗口</p>
<textarea id='text'></textarea>
<button id='but'>
执行Code
</button>
<div id='parser'>
</div>
</div>
<div class='right-box'>
<div>
<p>标准组件demo代码</p>
<textarea>
const H=()=>{
return (<h1> 这 个 是 标 题 1</h1>)
}
class Clock extends React.Component {
constructor (props) {
super(props);
this.state = { date:"2019-1-1", count: 10 };
}
addCount() {
const { count } = this.state;
this.setState({
count: count + 1,
})
}
render() {
const { date, count } = this.state;
return (
<div>
<h1>Hello, world!</h1>
<H/>
<h2>现在是 {date}</h2>
<button onClick={() => this.addCount()}>add1 count</button>
<button onClick={this.addCount.bind(this)}>add2 count</button>
<h2>count {count}</h2>
</div>
);
}
}
export default Clock;
</textarea>
<p>模板组件 demo 代码</p>
<textarea>
render(date,count) {
return (
<div>
<h1>Hello, world!</h1>
<h2>现在是 {date}</h2>
<h2>count {count}</h2>
</div>
);
}
</textarea>
</div>
</div>
</div>
<script type="text/babel">
var text = document.getElementById('text')
var but = document.getElementById('but')
var Tool = {
noop: () => { },
//把字符串 转成真正的js 并且以一个函数形式导出去
createFunction: (code, errors = []) => {
try {
return new Function(code).bind(this)
} catch (err) {
console.log('err', err)
errors.push({ err: err, code: code });
return noop
}
},
getExportDefaulComponentName: (code = '') => {
const exportDefaultComponentNameReg = /export\s+default\s+[a-zA-Z]+/g;
const exportDefault = /export\s+default\s+/g;
let exportDefaultComponentName = code.match(exportDefaultComponentNameReg);
if (exportDefaultComponentName && exportDefaultComponentName.length === 1) {
exportDefaultComponentName = exportDefaultComponentName[0].replace(exportDefault, '')
console.log('exportDefaultComponentName', exportDefaultComponentName)
return {
exportDefaultComponentName,
code: code.replace(exportDefaultComponentNameReg, '')
}
} else if (exportDefaultComponentName && exportDefaultComponentName.length >= 1) {
console.log('\x1B[31m%s\x1B[0m', 'This is export default Only one export is allowed')
// throw ('This is export default Only one export is allowed');
return {}
} else {
console.log('\x1B[31m%s\x1B[0m', 'It is an error not to export default Components')
// throw ('It is an error not to export default Components');
return {}
}
},
checkTemplate: ({
code, tagId, data = {}
}) => {
const classComponentReg = /class\s+[a-zA-Z]+\s+extends\s+React.Component/g;
const templateReg = /render\s*\(.*\)\s*{/g;
const renderHeadCode = code.match(templateReg)
if (classComponentReg.test(code)) {
Tool.componentReactDOMJoin({ code, tagId, data });
} else if (renderHeadCode && renderHeadCode.length >= 1) {
Tool.templateReactDOMJoin({ code, tagId, data, renderHeadCode: renderHeadCode[0] });
}
},
runScript: (newCode) => {
// 执行react 代码
transform.run(newCode)
},
componentReactDOMJoin: ({
code, tagId, data = {}
}) => {
// try {
let { code: newCode = '', exportDefaultComponentName = '' } = Tool.getExportDefaulComponentName(code);
if (!newCode || !exportDefaultComponentName) {
return null;
}
newCode = newCode + '; ReactDOM.render(<' + exportDefaultComponentName + '/>,document.getElementById("' + tagId + '"))'
Tool.runScript(newCode)
return newCode;
// }catch (err) {
// console.log('\x1B[31m%s\x1B[0m', err)
// }
},
getRenderParameterName({
code, tagId, data, renderHeadCode
}) {
const parameter = renderHeadCode.substring(renderHeadCode.indexOf("(") + 1, renderHeadCode.indexOf(")"))
if (parameter.trim() === '') {
console.log('\x1B[31m%s\x1B[0m', 'Please add Template render function parameter')
return {};
}
return {
parameter: parameter.split(','),
code: code.replace(parameter, ''),
renderHeadCode: renderHeadCode.replace(parameter, '')
}
},
checkStateKey: ({ parameter, data }) => {
const errorInfo = [];
const state = {};
let stateStrKey = `{`
parameter.map(key => {
let flag = null;
for (let _key in data) {
if (_key.trim() === key.trim()) {
flag = key;
}
}
if (!flag) {
console.log('\x1B[31m%s\x1B[0m', `error, The data is no ${key} parameter`)
errorInfo.push(`error, The data is no ${key} parameter`)
return false;
}
state[key.trim()] = data[key.trim()];
stateStrKey += `${key},`
})
return errorInfo.length >= 1 ? {} : {
stateStrKey: `${stateStrKey}}`,
state
};
},
templateReact: ({
code, tagId, data, renderHeadCode
}) => {
const { code: newCode, parameter, renderHeadCode: newRenderHeadCode } = Tool.getRenderParameterName({ code, tagId, data, renderHeadCode })
if (!parameter) {
return false;
}
let { state, stateStrKey } = Tool.checkStateKey({ parameter, data });
if (!state) {
return false;
}
state = JSON.stringify(state)
const returnLastStr = newCode.trim().substring(newRenderHeadCode.trim().length);
let renderStr = `
render() {
const ${stateStrKey} = this.state;
${returnLastStr}
`
const template = `
class App extends React.Component {
constructor (props) {
super(props);
this.state = ${
state
}
}
${renderStr}
}
export default App;
`
Tool.componentReactDOMJoin({ code: template, tagId, data });
},
templateReactDOMJoin: ({
code, tagId, data = {}, renderHeadCode
}) => {
Tool.templateReact({ code, tagId, data, renderHeadCode })
},
parseStyle: (code) => {
// const styleStartReg=//g;
// const styleEndReg='<style';
},
reactComponentParser({
code, tagId, data = {}
}) {
if (!code.trim()) {
console.log('\x1B[31m%s\x1B[0m', 'Please enter the react code')
return false;
}
if (!tagId.trim()) {
console.log('\x1B[31m%s\x1B[0m', 'Please write tagId')
return false;
}
// Tool.parseStyle(code);
Tool.checkTemplate({
code, tagId, data
});
}
}
but.addEventListener('click', () => {
Tool.reactComponentParser({
code: text.value,
// code: code2,
tagId: 'parser',
data: {
date: new Date(),
count: 100
}
});
})
</script>
</body>
</html>