Skip to content

Commit bfe44d8

Browse files
author
winjo
committed
feat: 增加行居中
1 parent 5d1305d commit bfe44d8

4 files changed

Lines changed: 110 additions & 22 deletions

File tree

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

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,17 +267,34 @@ class EditorSpecialContribution
267267
}
268268
})
269269
);
270+
271+
this.addDispose(
272+
this.editorService.onActiveResourceChange((resource) => {
273+
if (!resource) return;
274+
const documentModel = select((props) => props.documentModel);
275+
let rootPath = '';
276+
if (isCodeDocumentModel(documentModel)) {
277+
rootPath = path.join(this.appConfig.workspaceDir, documentModel.ref);
278+
} else {
279+
rootPath = this.appConfig.workspaceDir;
280+
}
281+
const relativePath = path.relative(rootPath, resource.uri.codeUri.path);
282+
select((props) => props.documentModel.onFilepathChange)?.(relativePath);
283+
})
284+
);
270285
}
271286

272-
private openEditor(relativePath: string) {
287+
private openEditor(relativePath?: string) {
273288
if (!relativePath) return;
274289
const uri = URI.file(path.join(this.appConfig.workspaceDir, relativePath));
275290
this.editorService.open(uri, {
276291
preview: true,
292+
// 始终只显示一个,目前切换编码,preview 会消失,可能得在 kaitian 中修复下
293+
replace: true,
277294
});
278295
}
279296

280-
private openCodeEditor(ref: string, filepath: string) {
297+
private openCodeEditor(ref?: string, filepath?: string) {
281298
if (!ref || !filepath) return;
282299
return this.openEditor(`${encodeURIComponent(ref)}/${filepath}`);
283300
}
@@ -340,7 +357,12 @@ class EditorSpecialContribution
340357
},
341358
},
342359
]);
360+
// 延迟高亮,否则不居中
361+
setTimeout(() => {
362+
editor.monacoEditor.revealLineInCenter(Number(lineNumber));
363+
}, 0);
343364
};
365+
344366
disposer.addDispose(
345367
editor.monacoEditor.onMouseDown((event) => {
346368
const type = event?.target?.type;
@@ -357,10 +379,16 @@ class EditorSpecialContribution
357379
}
358380
})
359381
);
360-
const initialLineNumber = select((props) => props.documentModel.lineNumber);
361-
if (initialLineNumber) {
362-
highlightLine(initialLineNumber);
363-
}
382+
383+
disposer.addDispose(
384+
editor.monacoEditor.onDidChangeModel(() => {
385+
const initialLineNumber = select((props) => props.documentModel.lineNumber);
386+
if (initialLineNumber) {
387+
highlightLine(initialLineNumber);
388+
}
389+
})
390+
);
391+
364392
disposer.addDispose(
365393
onSelect((props) => props.documentModel.lineNumber)((newLineNumber) => {
366394
if (newLineNumber) {

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
11
import { Thenable } from '../types';
22

33
export interface DocumentModel {
4-
filepath: string;
4+
/**
5+
* 打开的文件路径
6+
*/
7+
filepath?: string;
8+
/**
9+
* 文件路径变化事件,类型跳转时会从内部打开文件
10+
*/
11+
onFilepathChange?: (filepath: string) => void;
12+
/**
13+
* 文件读取接口
14+
* @param {string} filepath 文件路径
15+
* @returns 文件数据
16+
*/
517
readFile(filepath: string): Uint8Array | Thenable<Uint8Array>;
18+
/**
19+
* 文件编码
20+
*/
621
encoding?: 'gbk' | 'utf8';
22+
/**
23+
* 展示文件行号
24+
*/
725
lineNumber?: number;
26+
/**
27+
* 行号变更事件
28+
*/
829
onLineNumberChange?: (num: number) => void;
930
}
1031

@@ -13,9 +34,21 @@ export interface FSDocumentModel extends DocumentModel {
1334
}
1435

1536
export interface CodeDocumentModel extends DocumentModel {
37+
/**
38+
* 文档类型,code 代码平台仓库类型
39+
*/
1640
type: 'code';
41+
/**
42+
* tag or branch or commit
43+
*/
1744
ref: string;
45+
/**
46+
* 仓库 group 或用户
47+
*/
1848
owner: string;
49+
/**
50+
* 仓库名
51+
*/
1952
name: string;
2053
}
2154

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

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ const App = () => {
2424
const [encoding, setEncoding] = useState<'utf8' | 'gbk' | undefined>('utf8');
2525
const [lineNumber, setLineNumber] = useState<number | undefined>();
2626

27+
const setContent = () => {
28+
setRef('3977cf5ccb607beedce3824f6686fa97e2244506');
29+
setFilePath('src/main/java/com/alipay/languagebase/service/lsif/impl/CommitServiceImpl.java');
30+
setLineNumber(30);
31+
};
32+
2733
const readFile = async (filepath: string) => {
2834
const res = await fetch(
2935
`/code-test/api/v3/projects/${project}/repository/blobs/${encodeURIComponent(
@@ -39,14 +45,11 @@ const App = () => {
3945
return (
4046
<div style={{ width: '100%', height: '100%', padding: 8 }}>
4147
<div style={{ display: 'flex', marginBottom: 8 }}>
42-
<Button size="small" onClick={() => setKey((k) => k + 1)} style={{ marginRight: 8 }}>
43-
重置
44-
</Button>
4548
<Select
4649
value={ref || undefined}
4750
onChange={setRef}
4851
size="small"
49-
style={{ width: 200, marginRight: 8 }}
52+
style={{ width: 400, marginRight: 8 }}
5053
placeholder="选择分支"
5154
>
5255
{['3977cf5ccb607beedce3824f6686fa97e2244506'].map((path) => (
@@ -59,17 +62,26 @@ const App = () => {
5962
value={filepath || undefined}
6063
onChange={setFilePath}
6164
size="small"
62-
style={{ width: 300, marginRight: 8 }}
65+
style={{ width: 600, marginRight: 8 }}
6366
placeholder="选择文件"
6467
>
65-
{['src/main/java/com/alipay/languagebase/service/lsif/impl/CommitServiceImpl.java'].map(
66-
(path) => (
67-
<Select.Option key={path} value={path}>
68-
{path}
69-
</Select.Option>
70-
)
71-
)}
68+
{[
69+
'src/main/java/com/alipay/languagebase/service/lsif/impl/CommitServiceImpl.java',
70+
'src/main/java/com/alipay/languagebase/service/lsif/CommitService.java',
71+
].map((path) => (
72+
<Select.Option key={path} value={path}>
73+
{path}
74+
</Select.Option>
75+
))}
7276
</Select>
77+
</div>
78+
<div style={{ display: 'flex', marginBottom: 8 }}>
79+
<Button size="small" onClick={() => setKey((k) => k + 1)} style={{ marginRight: 8 }}>
80+
重置
81+
</Button>
82+
<Button size="small" onClick={() => setContent()} style={{ marginRight: 8 }}>
83+
一键设置文件
84+
</Button>
7385
<Select
7486
value={encoding}
7587
onChange={setEncoding}
@@ -90,9 +102,9 @@ const App = () => {
90102
style={{ width: 120, marginRight: 8 }}
91103
placeholder="更改选中行"
92104
>
93-
{['10', '20', '30'].map((encoding) => (
94-
<Select.Option key={encoding} value={encoding}>
95-
{encoding}
105+
{[10, 30, 100].map((line) => (
106+
<Select.Option key={line} value={line}>
107+
{line}
96108
</Select.Option>
97109
))}
98110
</Select>
@@ -138,6 +150,13 @@ const App = () => {
138150
owner,
139151
name,
140152
filepath,
153+
onFilepathChange(newFilepath) {
154+
setFilePath(newFilepath);
155+
// 切换文件时行号清楚掉
156+
if (newFilepath !== filepath) {
157+
setLineNumber(undefined);
158+
}
159+
},
141160
readFile,
142161
encoding,
143162
lineNumber,

scripts/bundle.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ export * from "../lib";
2626
`.trim() + '\n'
2727
);
2828

29+
// editor 类型文件
30+
await fse.writeFile(
31+
path.join(targetDir, 'alex.editor.d.ts'),
32+
`
33+
export * from "../lib/editor";
34+
`.trim() + '\n'
35+
);
36+
2937
signale.success('打包成功');
3038
} catch (err) {
3139
console.error(err);

0 commit comments

Comments
 (0)