Skip to content

Commit c884d77

Browse files
committed
feat: use webview
1 parent f9ea525 commit c884d77

File tree

5 files changed

+54
-25
lines changed

5 files changed

+54
-25
lines changed

package.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"activationEvents": [
1515
"onCustomEditor:customEditor.mrDetail",
1616
"onView:codingPlugin.treeMR",
17-
"onView:codingPlugin.treeDepot"
17+
"onView:codingPlugin.treeDepot",
18+
"onView:codingPlugin.webview"
1819
],
1920
"contributes": {
2021
"customEditors": [
@@ -61,6 +62,12 @@
6162
"id": "CODING-DEPOT",
6263
"title": "CODING 仓库"
6364
}
65+
],
66+
"rightside": [
67+
{
68+
"id": "WebviewContainerId",
69+
"title": "CODING 合并请求详情"
70+
}
6471
]
6572
},
6673
"views": {
@@ -75,6 +82,12 @@
7582
"id": "codingPlugin.treeDepot",
7683
"name": "CODING 仓库"
7784
}
85+
],
86+
"WebviewContainerId": [
87+
{
88+
"id": "codingPlugin.webview",
89+
"title": "CODING 合并请求详情"
90+
}
7891
]
7992
}
8093
},

src/extension.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import init from './init';
22
import CodingServer from './services/codingServer';
3+
import WebviewProvider from './webviews';
34
import ACTIONS, { dispatch } from './utils/actions';
45
import { proxyCtx } from './utils/proxy';
56
import toast from './utils/toast';
@@ -26,6 +27,7 @@ const user = {
2627

2728
async function activate(context: IContext) {
2829
// TODO: 认证,拿到用户信息
30+
const webviewProvider = new WebviewProvider();
2931

3032
const repoInfo = await CodingServer.getRepoParams();
3133
console.log('repoInfo ==> ', repoInfo);
@@ -43,6 +45,7 @@ async function activate(context: IContext) {
4345
dispatch(ACTIONS.SET_CTX, {
4446
context,
4547
value: {
48+
webviewProvider,
4649
codingServer,
4750
depots: [],
4851
selectedDepot: null,

src/init.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import MRCustomEditorProvider from './customEditors/mergeRequest';
55

66
import toast from './utils/toast';
77
import ACTIONS, { dispatch } from './utils/actions';
8-
import { openHosts } from './utils/mr';
98
import { IDepot, IMRItem } from './typings/common';
109

1110
const { registerCommand } = hx.commands;
@@ -15,11 +14,10 @@ export function registerCommands(context: IContext) {
1514

1615
context.subscriptions.push(
1716
registerCommand('codingPlugin.mrTreeItemClick', async function ([team, mrItem]: [string, IMRItem]) {
18-
openHosts();
1917
const matchRes = mrItem.path.match(/\/p\/([^/]+)\/d\/([^/]+)\/git\/merge\/([0-9]+)/);
2018
if (matchRes) {
2119
const [, project, repo, mergeRequestIId] = matchRes;
22-
context.mrCustomEditor.update({
20+
context.webviewProvider.update({
2321
session: codingServer.session,
2422
mergeRequestIId,
2523
repoInfo: {
@@ -47,6 +45,12 @@ export function registerCommands(context: IContext) {
4745
const depot = await hx.window.showInputBox({
4846
prompt: '请输入仓库名',
4947
});
48+
49+
if (!depot) {
50+
toast.warn('仓库名不能为空');
51+
return;
52+
}
53+
5054
const team = codingServer.session?.user?.team;
5155
await codingServer.createDepot(team, depot, depot);
5256
toast.info('仓库创建成功');
@@ -88,7 +92,6 @@ export function clear(context: IContext) {
8892

8993
export function registerCustomEditors(context: IContext) {
9094
const mrCustomEditor = new MRCustomEditorProvider(context);
91-
9295
hx.window.registerCustomEditorProvider('customEditor.mrDetail', mrCustomEditor);
9396

9497
dispatch(ACTIONS.SET_MR_CUSTOM_EDITOR, {
@@ -100,6 +103,5 @@ export function registerCustomEditors(context: IContext) {
100103
export default function init(context: IContext) {
101104
registerCommands(context);
102105
createTreeViews(context);
103-
registerCustomEditors(context);
104106
workspaceInit();
105107
}

src/customEditors/webview.ts renamed to src/webviews/index.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,29 @@ export default class WebviewProvider {
4444

4545
update(data: any) {
4646
const webview = this.panel.webView;
47-
const fileInfo = hx.Uri.file(path.resolve(__dirname, '../out/webviews/main.js'));
47+
const fileInfo = hx.Uri.file(path.resolve(__dirname, '../../out/webviews/main.js'));
48+
49+
const config = hx.workspace.getConfiguration();
50+
const colorScheme = config.get('editor.colorScheme');
51+
52+
const COLORS: Record<string, string> = {
53+
Monokai: 'themeDark',
54+
'Atom One Dark': 'themeDarkBlue',
55+
Default: 'themeLight',
56+
};
4857

4958
webview.html = `
50-
<body>
51-
<div>
52-
<div id='root'></div>
53-
</div>
54-
<script>
55-
window.__CODING__ = '${JSON.stringify(data)}'
56-
</script>
57-
<script src='${fileInfo}'></script>
58-
</body>
59+
<html>
60+
<body class='${COLORS[colorScheme]}'>
61+
<div>
62+
<div id='root'></div>
63+
</div>
64+
<script>
65+
window.__CODING__ = '${JSON.stringify(data)}'
66+
</script>
67+
<script src='${fileInfo}'></script>
68+
</body>
69+
</html>
5970
`;
6071
}
6172
}

webviews/App.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,6 @@ const App = () => {
180180
<div dangerouslySetInnerHTML={{ __html: body }} />
181181

182182
<div className={style.btnGroup}>
183-
{showMergeBtn && (
184-
<div
185-
className={cn(style.btn, style.btnPrimary, isMerging && style.disabled)}
186-
onClick={handleMerge}
187-
>
188-
{renderActionText(isMerging, '合并')}
189-
</div>
190-
)}
191-
192183
{showAllowMergeBtn && !isAgreed && (
193184
<div
194185
className={cn(style.btn, style.btnPrimary, isAllowing && style.disabled)}
@@ -207,6 +198,15 @@ const App = () => {
207198
</div>
208199
)}
209200

201+
{showMergeBtn && (
202+
<div
203+
className={cn(style.btn, style.btnPrimary, isMerging && style.disabled)}
204+
onClick={handleMerge}
205+
>
206+
{renderActionText(isMerging, '合并')}
207+
</div>
208+
)}
209+
210210
{showCloseBtn && (
211211
<div
212212
className={cn(style.btn, isClosing && style.disabled)}

0 commit comments

Comments
 (0)