Skip to content

Commit 5b3e12b

Browse files
author
winjo
authored
chore: add provider example (#178)
1 parent 9ab6c76 commit 5b3e12b

2 files changed

Lines changed: 72 additions & 69 deletions

File tree

Lines changed: 64 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,75 @@
11
import { AppProvider, CodeEditor, DiffEditor } from '@codeblitzjs/ide-core';
2-
import React from 'react';
2+
import React, { useEffect, useState } from 'react';
33
import { createRoot } from 'react-dom/client';
44
import '@codeblitzjs/ide-core/languages';
5-
import { SampleModule } from './module'
5+
import { SampleModule, diffsDeferred } from './module'
66
import '../index.css';
77
import './index.css'
88

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],
4336
}}
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',
5239
}}
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+
)
6069
}
61-
}}
62-
style={{ width: 1000, height: 300 }}
63-
/>
64-
</AppProvider>
65-
);
70+
})}
71+
</AppProvider>
72+
)
73+
};
6674

6775
createRoot(document.getElementById('main') as HTMLElement).render(<App />);

packages/startup/src/provider/module.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
import { Autowired, Injectable, Provider } from '@opensumi/di';
22
import { BrowserModule } from '@opensumi/ide-core-browser';
33
import { IEditorDocumentModelContentProvider, BrowserEditorContribution, IEditorDocumentModelContentRegistry } from '@opensumi/ide-editor/lib/browser'
4-
import { URI, Emitter, Event, Domain } from '@opensumi/ide-core-common'
4+
import { URI, Emitter, Event, Domain, Deferred } from '@opensumi/ide-core-common'
55

6-
const contentMap = {
7-
'a1.js': `const add = (x, y) => {
8-
return x + y
9-
}
10-
`,
11-
'a2.js': `const add = (x, y) => {
12-
return x + y + 1
13-
}
14-
`,
15-
}
6+
export const diffsDeferred = new Deferred<{filePath: string, oldFileContent: string | null, newFileContent: string }[]>()
167

178
@Injectable()
189
export class SampleSchemeDocumentProvider implements IEditorDocumentModelContentProvider {
@@ -21,11 +12,15 @@ export class SampleSchemeDocumentProvider implements IEditorDocumentModelContent
2112
}
2213

2314
async provideEditorDocumentModelContent(uri: URI): Promise<string> {
24-
return contentMap[uri.codeUri.path.slice(1)]
15+
const diffs = await diffsDeferred.promise
16+
const diff = diffs.find(item => item.filePath === uri.codeUri.path.slice(1))
17+
if (!diff) return ''
18+
if (uri.authority === 'new') return diff.newFileContent
19+
return diff.oldFileContent || ''
2520
}
2621

2722
isReadonly() {
28-
return true;
23+
return false;
2924
}
3025

3126
private _onDidChangeContent: Emitter<URI> = new Emitter();

0 commit comments

Comments
 (0)