Skip to content

Commit 120ddb6

Browse files
author
winjo
committed
feat: add code document model
1 parent 40d452b commit 120ddb6

4 files changed

Lines changed: 74 additions & 25 deletions

File tree

packages/alex/src/core/editor/editor.module.ts

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import { SCMService } from '@ali/ide-scm';
3939
import { DirtyDiffWidget } from '@ali/ide-scm/lib/browser/dirty-diff/dirty-diff-widget';
4040
import { IDETheme } from '../../core/extensions';
4141
import { select, onSelect } from './container';
42+
import { isCodeDocumentModel, CodeDocumentModel } from './types';
4243

4344
// TODO: 此处 diff 的 stage 和 revertChange 应该是 git 注册的,框架中直接添加了按钮,耦合,需要修复实现 scm/change/title
4445
// @ts-ignore
@@ -134,7 +135,10 @@ class EditorSpecialContribution
134135
} = BrowserFS;
135136
const [editorSystem, memSystem] = await Promise.all([
136137
createFileSystem(Editor, {
137-
readFile: select((props) => props.documentModel.readFile),
138+
readFile: (filepath: string) => {
139+
const slashIndex = filepath.indexOf('/');
140+
return select((props) => props.documentModel.readFile)(filepath.slice(slashIndex + 1));
141+
},
138142
}),
139143
createFileSystem(InMemory, {}),
140144
]);
@@ -185,15 +189,25 @@ class EditorSpecialContribution
185189
}
186190

187191
onDidRestoreState() {
188-
const filepath = select((props) => props.documentModel.filepath);
189-
if (filepath) {
190-
this.openEditor(filepath);
191-
}
192+
const documentModel = select((props) => props.documentModel);
192193

193-
// 监听 props 变化
194-
onSelect((props) => props.documentModel.filepath)((newFilepath) => {
195-
this.openEditor(newFilepath);
196-
});
194+
if (isCodeDocumentModel(documentModel)) {
195+
this.openCodeEditor(documentModel.ref, documentModel.filepath);
196+
// 监听 props 变化
197+
onSelect(
198+
(props) => props.documentModel,
199+
(newModel: CodeDocumentModel, oldModel: CodeDocumentModel) =>
200+
newModel.filepath === oldModel.filepath && newModel.ref === oldModel.ref
201+
)((newModel: CodeDocumentModel) => {
202+
this.openCodeEditor(newModel.ref, newModel.filepath);
203+
});
204+
} else {
205+
this.openEditor(documentModel.filepath);
206+
// 监听 props 变化
207+
onSelect((props) => props.documentModel.filepath)((newFilepath) => {
208+
this.openEditor(newFilepath);
209+
});
210+
}
197211

198212
onSelect((props) => props.documentModel.encoding)((encoding) => {
199213
if (encoding) {
@@ -208,12 +222,18 @@ class EditorSpecialContribution
208222
}
209223

210224
private openEditor(relativePath: string) {
225+
if (!relativePath) return;
211226
const uri = URI.file(path.join(this.appConfig.workspaceDir, relativePath));
212227
this.editorService.open(uri, {
213228
preview: true,
214229
});
215230
}
216231

232+
private openCodeEditor(ref: string, filepath: string) {
233+
if (!ref || !filepath) return;
234+
return this.openEditor(`${encodeURIComponent(ref)}/${filepath}`);
235+
}
236+
217237
dispose() {
218238
this.disposables.forEach((disposer) => disposer.dispose());
219239
}
Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
1+
import { Thenable } from '../types';
2+
13
export interface DocumentModel {
24
filepath: string;
3-
readFile(path: string): Uint8Array | Thenable<Uint8Array>;
5+
readFile(filepath: string): Uint8Array | Thenable<Uint8Array>;
46
encoding?: 'gbk' | 'utf8';
57
}
68

9+
export interface FSDocumentModel extends DocumentModel {
10+
type?: 'fs';
11+
}
12+
13+
export interface CodeDocumentModel extends DocumentModel {
14+
type: 'code';
15+
ref: string;
16+
owner: string;
17+
name: string;
18+
}
19+
720
export interface EditorProps {
8-
documentModel: DocumentModel;
21+
documentModel: FSDocumentModel | CodeDocumentModel;
922
}
23+
24+
export const isCodeDocumentModel = (
25+
model: EditorProps['documentModel']
26+
): model is CodeDocumentModel => {
27+
return 'type' in model && model.type === 'code';
28+
};

packages/alex/src/core/types.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,14 @@ export interface LandingProps {
1111
export interface RootProps extends LandingProps {
1212
Landing?: ComponentType<LandingProps>;
1313
}
14+
15+
export interface Thenable<T> {
16+
then<TResult>(
17+
onfulfilled?: (value: T) => TResult | Thenable<TResult>,
18+
onrejected?: (reason: any) => TResult | Thenable<TResult>
19+
): Thenable<TResult>;
20+
then<TResult>(
21+
onfulfilled?: (value: T) => TResult | Thenable<TResult>,
22+
onrejected?: (reason: any) => void
23+
): Thenable<TResult>;
24+
}

packages/alex/src/integration/editor/index.tsx

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,20 @@ import 'antd/lib/spin/style/index.css';
1313

1414
(window as any).alex = Alex;
1515

16-
const project = encodeURIComponent('ide-s/TypeScript-Node-Starter');
16+
const owner = 'ide-s';
17+
const name = 'TypeScript-Node-Starter';
18+
const project = encodeURIComponent(`${owner}/${name}`);
1719

1820
const App = () => {
1921
const [ref, setRef] = useState('');
2022
const [filepath, setFilePath] = useState('');
2123
const [encoding, setEncoding] = useState<'utf8' | 'gbk' | undefined>('utf8');
2224

23-
const path = useMemo(() => (ref && filepath ? `${encodeURIComponent(ref)}/${filepath}` : ''), [
24-
ref,
25-
filepath,
26-
]);
27-
28-
const readFile = async (path: string) => {
29-
const i = path.indexOf('/');
25+
const readFile = async (filepath: string) => {
3026
const res = await fetch(
31-
`/code-service/api/v3/projects/${project}/repository/blobs/${path.slice(
32-
0,
33-
i
34-
)}?filepath=${path.slice(i + 1)}`
27+
`/code-service/api/v3/projects/${project}/repository/blobs/${encodeURIComponent(
28+
ref
29+
)}?filepath=${filepath}`
3530
);
3631
if (res.status >= 200 && res.status < 300) {
3732
return res.arrayBuffer();
@@ -112,10 +107,14 @@ const App = () => {
112107
biz: 'editor',
113108
scenario: null,
114109
startupEditor: 'none',
115-
hideEditorTab: true,
110+
// hideEditorTab: true,
116111
}}
117112
documentModel={{
118-
filepath: path,
113+
type: 'code',
114+
ref,
115+
owner,
116+
name,
117+
filepath,
119118
readFile,
120119
encoding,
121120
}}

0 commit comments

Comments
 (0)