Skip to content

Commit b8f021d

Browse files
authored
feat: update filesystem sample (#8)
1 parent 6876db4 commit b8f021d

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

filesystem.tsx

+33-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import '@codeblitzjs/ide-core/bundle/codeblitz.css';
66
import '@codeblitzjs/ide-core/languages';
77
import Select from 'antd/lib/select';
88
import 'antd/lib/select/style/index.css';
9+
import { Button } from 'antd';
10+
import type { IFileServiceClient as TFileServiceClient } from '@opensumi/ide-file-service/lib/common';
11+
12+
const path = requireModule('path');
913

1014
const dirMap: Record<string, [string, FileType][]> = {
1115
'/': [
@@ -35,8 +39,10 @@ const zipDataPromise = (async () => {
3539
zipData = Buffer.from(new Uint8Array(buf));
3640
})();
3741

42+
const workspaceDir = 'filesystem';
43+
3844
const App = () => {
39-
const [fsType, setFsType] = useState<string>('OverlayFS');
45+
const [fsType, setFsType] = useState<string>('FileIndexSystem');
4046

4147
const filesystem = useMemo<
4248
NonNullable<IAppRendererProps['runtimeConfig']['workspace']>['filesystem'] | undefined
@@ -63,7 +69,8 @@ const App = () => {
6369
'main.html': '<div id="root"></div>',
6470
'main.css': 'body {}',
6571
'main.js': 'console.log("main")',
66-
'package.json': '{\n "name": "Riddle"\n}',
72+
'package.json': '{\n "name": "Codeblitz"\n}',
73+
'readme.md': '# Codeblitz',
6774
});
6875
},
6976
},
@@ -124,7 +131,7 @@ const App = () => {
124131
return (
125132
<div style={{ height: '100%' }}>
126133
<div style={{ height: 48, display: 'flex', alignItems: 'center' }}>
127-
<Select<string> onChange={(e) => setFsType(e)} style={{ width: 200 }}>
134+
<Select<string> onChange={(e) => setFsType(e)} style={{ width: 200 }} value={fsType}>
128135
<Select.Option value="IndexedDB">IndexedDB</Select.Option>
129136
<Select.Option value="InMemory">InMemory</Select.Option>
130137
<Select.Option value="FileIndexSystem">FileIndexSystem</Select.Option>
@@ -133,7 +140,29 @@ const App = () => {
133140
<Select.Option value="FolderAdapter">FolderAdapter</Select.Option>
134141
<Select.Option value="OverlayFS">OverlayFS</Select.Option>
135142
</Select>
143+
144+
{
145+
fsType === 'FileIndexSystem' && (
146+
<Button onClick={async () => {
147+
// All files in codeblitz are mounted to /workspace/${workspaceDir}
148+
const targetFile = path.join('/workspace', workspaceDir, 'readme.md');
149+
const fileService = window.app.injector.get(IFileServiceClient) as TFileServiceClient;
150+
const uri = Uri.file(targetFile).toString();
151+
const stat = await fileService.getFileStat(uri);
152+
if (stat) {
153+
const content = await fileService.readFile(uri);
154+
const text = content.content.toString();
155+
await fileService.setContent(stat, text +'\n\nThis is a demo file.', {
156+
encoding: 'utf8',
157+
});
158+
}
159+
}}>
160+
修改 readme.md
161+
</Button>
162+
)
163+
}
136164
</div>
165+
137166
<div style={{ height: 'calc(100% - 48px)' }}>
138167
<AppRenderer
139168
key={fsType}
@@ -165,7 +194,7 @@ const App = () => {
165194
});
166195
}}
167196
appConfig={{
168-
workspaceDir: 'filesystem',
197+
workspaceDir,
169198
defaultPreferences: {
170199
'general.theme': 'opensumi-light',
171200
},

0 commit comments

Comments
 (0)