|
1 | 1 | import { AppProvider, CodeEditor, DiffEditor } from '@codeblitzjs/ide-core'; |
2 | | -import React from 'react'; |
| 2 | +import React, { useEffect, useState } from 'react'; |
3 | 3 | import { createRoot } from 'react-dom/client'; |
4 | 4 | import '@codeblitzjs/ide-core/languages'; |
5 | | -import { SampleModule } from './module' |
| 5 | +import { SampleModule, diffsDeferred } from './module' |
6 | 6 | import '../index.css'; |
7 | 7 | import './index.css' |
8 | 8 |
|
9 | | -const App = () => ( |
10 | | - <AppProvider |
11 | | - appConfig={{ |
12 | | - workspaceDir: 'my-workspace', |
13 | | - layoutConfig: {}, |
14 | | - modules: [SampleModule], |
15 | | - }} |
16 | | - runtimeConfig={{ |
17 | | - biz: 'startup', |
18 | | - workspace: { |
19 | | - filesystem: { |
20 | | - fs: 'FileIndexSystem', |
21 | | - options: { |
22 | | - // 初始全量文件索引 |
23 | | - requestFileIndex() { |
24 | | - return Promise.resolve({ |
25 | | - 'main.html': '<div id="root"></div>', |
26 | | - 'main.css': 'body {}', |
27 | | - 'main.js': 'console.log("main")', |
28 | | - 'package.json': '{\n "name": "startup"\n}', |
29 | | - }); |
30 | | - }, |
31 | | - }, |
32 | | - } |
33 | | - }, |
34 | | - }} |
35 | | - > |
36 | | - <CodeEditor |
37 | | - uri="main.js" |
38 | | - style={{ width: 1000, height: 300, marginBottom: 16 }} |
39 | | - editorOptions={{ |
40 | | - scrollbar: { |
41 | | - alwaysConsumeMouseWheel: false |
42 | | - } |
| 9 | +const App = () => { |
| 10 | + const [diffs, setDiffs] = useState<{filePath: string, oldFileContent: string | null, newFileContent: string | null }[]>([]) |
| 11 | + |
| 12 | + useEffect(() => { |
| 13 | + setTimeout(() => { |
| 14 | + const diffs = [{ |
| 15 | + filePath: 'a.js', |
| 16 | + oldFileContent: null, |
| 17 | + newFileContent: 'console.log(123)' |
| 18 | + }, { |
| 19 | + filePath: 'a1.js', |
| 20 | + oldFileContent: 'const add = (x, y) => {\n return x + y\n}', |
| 21 | + newFileContent: 'const add = (x, y) => {\n return x + y + 1\n}' |
| 22 | + }] |
| 23 | + setDiffs(diffs) |
| 24 | + diffsDeferred.resolve(diffs) |
| 25 | + }, 1000) |
| 26 | + }, []) |
| 27 | + |
| 28 | + if (!diffs) return null |
| 29 | + |
| 30 | + return ( |
| 31 | + <AppProvider |
| 32 | + appConfig={{ |
| 33 | + workspaceDir: 'my-workspace', |
| 34 | + layoutConfig: {}, |
| 35 | + modules: [SampleModule], |
43 | 36 | }} |
44 | | - /> |
45 | | - <CodeEditor |
46 | | - uri="main.css" |
47 | | - style={{ width: 1000, height: 300 }} |
48 | | - editorOptions={{ |
49 | | - scrollbar: { |
50 | | - alwaysConsumeMouseWheel: false |
51 | | - } |
| 37 | + runtimeConfig={{ |
| 38 | + biz: 'startup', |
52 | 39 | }} |
53 | | - /> |
54 | | - <DiffEditor |
55 | | - originalUri="sample:/a1.js" |
56 | | - modifiedUri="sample:/a2.js" |
57 | | - editorOptions={{ |
58 | | - scrollbar: { |
59 | | - alwaysConsumeMouseWheel: false |
| 40 | + > |
| 41 | + {diffs.map(({ filePath, oldFileContent, newFileContent }) => { |
| 42 | + if (!oldFileContent) { |
| 43 | + return ( |
| 44 | + <CodeEditor |
| 45 | + key={filePath} |
| 46 | + uri={`sample://new/${filePath}`} |
| 47 | + style={{ width: 1000, height: 300, marginBottom: 16 }} |
| 48 | + editorOptions={{ |
| 49 | + scrollbar: { |
| 50 | + alwaysConsumeMouseWheel: false |
| 51 | + } |
| 52 | + }} |
| 53 | + /> |
| 54 | + ) |
| 55 | + } else { |
| 56 | + return ( |
| 57 | + <DiffEditor |
| 58 | + key={filePath} |
| 59 | + originalUri={`sample://old/${filePath}`} |
| 60 | + modifiedUri={`sample://new/${filePath}`} |
| 61 | + editorOptions={{ |
| 62 | + scrollbar: { |
| 63 | + alwaysConsumeMouseWheel: false |
| 64 | + } |
| 65 | + }} |
| 66 | + style={{ width: 1000, height: 300 }} |
| 67 | + /> |
| 68 | + ) |
60 | 69 | } |
61 | | - }} |
62 | | - style={{ width: 1000, height: 300 }} |
63 | | - /> |
64 | | - </AppProvider> |
65 | | -); |
| 70 | + })} |
| 71 | + </AppProvider> |
| 72 | + ) |
| 73 | +}; |
66 | 74 |
|
67 | 75 | createRoot(document.getElementById('main') as HTMLElement).render(<App />); |
0 commit comments