-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathartifacts_react_test2.html
More file actions
391 lines (357 loc) · 14.9 KB
/
artifacts_react_test2.html
File metadata and controls
391 lines (357 loc) · 14.9 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>React 实时编译渲染 - 支持第三方库</title>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<style>
.container { display: flex; height: 100vh; }
.editor { width: 50%; padding: 20px; }
.preview { width: 50%; padding: 20px; border-left: 1px solid #ccc; }
textarea { width: 100%; height: 500px; font-family: monospace; font-size: 14px; }
button { padding: 8px 16px; margin: 5px; cursor: pointer; }
</style>
</head>
<body>
<div class="container">
<div class="editor">
<h3>React 代码编辑器</h3>
<select onchange="loadExample(this.value)">
<option value="">选择示例</option>
<option value="card">卡片组件示例</option>
<option value="antd">Ant Design 示例</option>
</select>
<textarea id="codeInput" placeholder="在这里输入 React 代码...">
import React, { useState } from 'react';
import { Card, CardContent, CardHeader } from '@/components/ui/card';
function App() {
const [count, setCount] = useState(0);
return (
<div>
<h1>带卡片的计数器</h1>
<Card>
<CardHeader>
计数器应用
</CardHeader>
<CardContent>
<h2>当前计数: {count}</h2>
<div style={{ marginTop: '10px' }}>
<button onClick={() => setCount(count + 1)} style={{ marginRight: '10px' }}>
增加
</button>
<button onClick={() => setCount(count - 1)} style={{ marginRight: '10px' }}>
减少
</button>
<button onClick={() => setCount(0)}>
重置
</button>
</div>
</CardContent>
</Card>
</div>
);
}
</textarea>
<br>
<button onclick="renderCode()">渲染代码</button>
</div>
<div class="preview">
<h3>预览</h3>
<div id="preview"></div>
</div>
</div>
<script>
// 预置库映射
const presetLibraries = {
'react': {
default: React,
useState: React.useState,
useEffect: React.useEffect,
useCallback: React.useCallback,
useMemo: React.useMemo,
useReducer: React.useReducer,
useContext: React.useContext,
useRef: React.useRef
},
'@/components/ui/card': {
Card: ({ children, className = '', ...props }) =>
React.createElement('div', {
className: `card ${className}`,
style: {
border: '1px solid #e2e8f0',
borderRadius: '8px',
backgroundColor: 'white',
boxShadow: '0 1px 3px rgba(0,0,0,0.1)',
margin: '10px 0'
},
...props
}, children),
CardHeader: ({ children, className = '', ...props }) =>
React.createElement('div', {
className: `card-header ${className}`,
style: {
padding: '16px 20px 0 20px',
fontWeight: '600',
fontSize: '18px'
},
...props
}, children),
CardContent: ({ children, className = '', ...props }) =>
React.createElement('div', {
className: `card-content ${className}`,
style: {
padding: '16px 20px 20px 20px'
},
...props
}, children)
},
'antd': {
Button: ({ children, type = 'default', onClick, ...props }) =>
React.createElement('button', {
onClick,
style: {
padding: '8px 16px',
border: type === 'primary' ? 'none' : '1px solid #d9d9d9',
backgroundColor: type === 'primary' ? '#1890ff' : 'white',
color: type === 'primary' ? 'white' : '#000',
borderRadius: '4px',
cursor: 'pointer',
marginRight: '8px'
},
...props
}, children),
Card: ({ title, children, ...props }) =>
React.createElement('div', {
style: {
border: '1px solid #f0f0f0',
borderRadius: '8px',
overflow: 'hidden',
margin: '10px 0'
},
...props
}, [
title && React.createElement('div', {
key: 'title',
style: {
padding: '16px',
borderBottom: '1px solid #f0f0f0',
fontWeight: '500'
}
}, title),
React.createElement('div', {
key: 'content',
style: { padding: '16px' }
}, children)
])
}
};
// 解析导入语句
function parseImports(code) {
const imports = {};
// 更精确的 import 正则表达式
const patterns = [
// import React, { useState } from 'react';
/import\s+(\w+)\s*,\s*{\s*([^}]+)\s*}\s+from\s+['"]([^'"]+)['"];?\s*/g,
// import { Component1, Component2 } from 'module';
/import\s*{\s*([^}]+)\s*}\s+from\s+['"]([^'"]+)['"];?\s*/g,
// import * as Module from 'module';
/import\s+\*\s+as\s+(\w+)\s+from\s+['"]([^'"]+)['"];?\s*/g,
// import Module from 'module';
/import\s+(\w+)\s+from\s+['"]([^'"]+)['"];?\s*/g
];
patterns.forEach((pattern, index) => {
let match;
// 重置正则表达式的 lastIndex
pattern.lastIndex = 0;
while ((match = pattern.exec(code)) !== null) {
if (index === 0) {
// import React, { useState } from 'react';
const [, defaultImport, namedImports, modulePath] = match;
if (presetLibraries[modulePath]) {
// 默认导入
if (presetLibraries[modulePath].default) {
imports[defaultImport] = presetLibraries[modulePath].default;
}
// 命名导入
const names = namedImports.split(',').map(name => name.trim());
names.forEach(name => {
if (presetLibraries[modulePath][name]) {
imports[name] = presetLibraries[modulePath][name];
}
});
}
} else if (index === 1) {
// import { Component1, Component2 } from 'module';
const [, namedImports, modulePath] = match;
if (presetLibraries[modulePath]) {
const names = namedImports.split(',').map(name => name.trim());
names.forEach(name => {
if (presetLibraries[modulePath][name]) {
imports[name] = presetLibraries[modulePath][name];
}
});
}
} else if (index === 2) {
// import * as Module from 'module';
const [, namespaceImport, modulePath] = match;
if (presetLibraries[modulePath]) {
imports[namespaceImport] = presetLibraries[modulePath];
}
} else if (index === 3) {
// import Module from 'module';
const [, defaultImport, modulePath] = match;
if (presetLibraries[modulePath]) {
imports[defaultImport] = presetLibraries[modulePath].default || presetLibraries[modulePath];
}
}
}
});
return imports;
}
// 移除所有 import 和 export 语句
function removeImportsAndExports(code) {
return code
// 移除所有类型的 import 语句
.replace(/import\s+(\w+)\s*,\s*{\s*[^}]+\s*}\s+from\s+['"][^'"]+['"];?\s*/g, '')
.replace(/import\s*{\s*[^}]+\s*}\s+from\s+['"][^'"]+['"];?\s*/g, '')
.replace(/import\s+\*\s+as\s+\w+\s+from\s+['"][^'"]+['"];?\s*/g, '')
.replace(/import\s+\w+\s+from\s+['"][^'"]+['"];?\s*/g, '')
// 移除 export 语句
.replace(/export\s+default\s+\w+;?\s*$/gm, '')
.replace(/export\s*{\s*[^}]+\s*};?\s*/g, '')
// 清理多余的空行
.replace(/\n\s*\n\s*\n/g, '\n\n');
}
// 主渲染函数
function rendering(root_element, react_code) {
try {
console.log('原始代码:', react_code);
// 1. 解析导入的库
const importedLibraries = parseImports(react_code);
console.log('解析的导入:', importedLibraries);
// 2. 移除所有 import 和 export 语句
const cleanedCode = removeImportsAndExports(react_code);
console.log('清理后的代码:', cleanedCode);
// 3. 使用 Babel 转换 JSX
const transformedCode = Babel.transform(cleanedCode, {
presets: [['react', { runtime: 'classic' }]],
plugins: []
}).code;
console.log('转换后的代码:', transformedCode);
// 4. 准备执行环境的参数 - 避免重复参数名
const baseParams = {
'React': React,
'ReactDOM': ReactDOM
};
// 合并导入的库,避免重复
const allParams = { ...baseParams, ...importedLibraries };
// 去重参数名
const paramNames = Object.keys(allParams);
const paramValues = Object.values(allParams);
console.log('参数名:', paramNames);
console.log('参数值:', paramValues);
// 5. 创建并执行函数
const executeCode = new Function(
...paramNames,
`
"use strict";
${transformedCode}
return App;
`
);
const AppComponent = executeCode(...paramValues);
console.log('获取的组件:', AppComponent);
// 6. 渲染组件
if (window.currentRoot) {
window.currentRoot.unmount();
}
window.currentRoot = ReactDOM.createRoot(root_element);
window.currentRoot.render(React.createElement(AppComponent));
} catch (error) {
console.error('渲染失败:', error);
root_element.innerHTML = `
<div style="color: red; padding: 20px; border: 1px solid red; border-radius: 4px;">
<h4>渲染错误:</h4>
<pre>${error.message}</pre>
<details>
<summary>错误详情</summary>
<pre>${error.stack}</pre>
</details>
</div>
`;
}
}
function renderCode() {
const code = document.getElementById('codeInput').value;
const preview = document.getElementById('preview');
rendering(preview, code);
}
// 示例代码
const examples = {
card: `import React, { useState } from 'react';
import { Card, CardContent, CardHeader } from '@/components/ui/card';
function App() {
const [count, setCount] = useState(0);
return (
<div>
<h1>带卡片的计数器</h1>
<Card>
<CardHeader>
计数器应用
</CardHeader>
<CardContent>
<h2>当前计数: {count}</h2>
<div style={{ marginTop: '10px' }}>
<button onClick={() => setCount(count + 1)} style={{ marginRight: '10px' }}>
增加
</button>
<button onClick={() => setCount(count - 1)} style={{ marginRight: '10px' }}>
减少
</button>
<button onClick={() => setCount(0)}>
重置
</button>
</div>
</CardContent>
</Card>
</div>
);
}`,
antd: `import React, { useState } from 'react';
import { Button, Card } from 'antd';
function App() {
const [count, setCount] = useState(0);
return (
<div style={{ padding: '20px' }}>
<h1>Ant Design 示例</h1>
<Card title="计数器卡片">
<p>当前计数: <strong>{count}</strong></p>
<Button type="primary" onClick={() => setCount(count + 1)}>
增加
</Button>
<Button onClick={() => setCount(count - 1)}>
减少
</Button>
<Button onClick={() => setCount(0)}>
重置
</Button>
</Card>
</div>
);
}`
};
function loadExample(type) {
if (examples[type]) {
document.getElementById('codeInput').value = examples[type];
renderCode();
}
}
// 页面加载时自动渲染
window.onload = function() {
renderCode();
};
</script>
</body>
</html>