Skip to content

Commit 5c15fbd

Browse files
committed
fix:修改教学页的配置存盘
1 parent 54fdc80 commit 5c15fbd

File tree

1 file changed

+61
-22
lines changed

1 file changed

+61
-22
lines changed

src/guide.ts

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@ export function activateGuide(context: vscode.ExtensionContext) {
99
}
1010

1111
class GuideViewProvider implements vscode.WebviewViewProvider {
12+
private webviewView?: vscode.WebviewView;
13+
1214
constructor(private context: vscode.ExtensionContext) {}
1315

1416
resolveWebviewView(webviewView: vscode.WebviewView) {
17+
this.webviewView = webviewView;
1518
webviewView.webview.options = {
1619
enableScripts: true,
1720
localResourceRoots: [this.context.extensionUri],
1821
};
1922
webviewView.webview.html = this.getWebviewContent(webviewView);
2023

21-
webviewView.webview?.onDidReceiveMessage((message) => {
24+
webviewView.webview?.onDidReceiveMessage(async (message) => {
2225
switch (message.command) {
23-
case 'saveApiKey':
24-
this.saveApiKey(message.apiKey);
26+
case 'saveAllConfig':
27+
await this.handleSaveAllConfig(message.data, webviewView);
2528
break;
2629
case 'updateModelConfig':
27-
const config = vscode.workspace.getConfiguration('codeReDesign');
28-
config.update('modelConfig', message.selectedModel, vscode.ConfigurationTarget.Global).then(() => {
29-
webviewView.webview.html = this.getWebviewContent(webviewView); // 更新内容
30-
});
30+
await this.updateModelConfig(message.selectedModel, webviewView);
3131
break;
3232
default:
3333
vscode.commands.executeCommand(message.command);
@@ -36,15 +36,38 @@ class GuideViewProvider implements vscode.WebviewViewProvider {
3636
});
3737
}
3838

39-
private saveApiKey(apiKey: string) {
39+
private async handleSaveAllConfig(data: any, webviewView: vscode.WebviewView) {
4040
const config = vscode.workspace.getConfiguration('codeReDesign');
41-
config.update('deepSeekApiKey', apiKey, vscode.ConfigurationTarget.Global)
42-
.then(() => {
43-
vscode.window.showInformationMessage('配置已更新');
44-
}, (err) => {
45-
vscode.window.showErrorMessage(`配置更新失败: ${err}`);
46-
});
41+
try {
42+
// 保存DeepSeek API Key
43+
await config.update('deepSeekApiKey', data.deepSeekApiKey, vscode.ConfigurationTarget.Global);
44+
45+
// 保存模型配置
46+
await config.update('modelConfig', data.selectedModel, vscode.ConfigurationTarget.Global);
47+
48+
// 如果选择的是自定义模型,保存自定义配置
49+
const customModelMatch = data.selectedModel.match(/^custom(\d+)$/);
50+
if (customModelMatch) {
51+
const customNumber = customModelMatch[1];
52+
await config.update(`custom${customNumber}BaseURL`, data.customBaseURL, vscode.ConfigurationTarget.Global);
53+
await config.update(`custom${customNumber}ModelName`, data.customModelName, vscode.ConfigurationTarget.Global);
54+
await config.update(`custom${customNumber}ModelNickname`, data.customModelNickname, vscode.ConfigurationTarget.Global);
55+
await config.update(`custom${customNumber}APIKey`, data.customAPIKey, vscode.ConfigurationTarget.Global);
56+
}
57+
58+
vscode.window.showInformationMessage('配置已保存');
59+
webviewView.webview.html = this.getWebviewContent(webviewView);
60+
} catch (err) {
61+
vscode.window.showErrorMessage(`保存配置失败: ${err}`);
62+
}
63+
}
64+
65+
private async updateModelConfig(selectedModel: string, webviewView: vscode.WebviewView) {
66+
const config = vscode.workspace.getConfiguration('codeReDesign');
67+
await config.update('modelConfig', selectedModel, vscode.ConfigurationTarget.Global);
68+
webviewView.webview.html = this.getWebviewContent(webviewView);
4769
}
70+
4871
private getWebviewContent(webviewView: vscode.WebviewView): string {
4972
const config = vscode.workspace.getConfiguration('codeReDesign');
5073
const apiKey = config.get('deepSeekApiKey') || '';
@@ -64,7 +87,7 @@ class GuideViewProvider implements vscode.WebviewViewProvider {
6487
baseURL: baseURL || '',
6588
modelName: modelName || '',
6689
modelNickname: modelNickname || `自定义模型 ${i}`,
67-
modelAPIKey : modelAPIKey || ''
90+
modelAPIKey: modelAPIKey || ''
6891
});
6992
}
7093

@@ -84,10 +107,10 @@ class GuideViewProvider implements vscode.WebviewViewProvider {
84107
const customModelName = selectedCustomConfig?.modelName || '';
85108
const customModelNickname = selectedCustomConfig?.modelNickname || '';
86109
const customAPIKey = selectedCustomConfig?.modelAPIKey || '';
87-
110+
88111
// Get path to resource on disk
89112
const onDiskPath = vscode.Uri.joinPath(this.context.extensionUri, 'images', 'guide', 'rightClick.png');
90-
113+
91114
// And get the special URI to use with the webview
92115
const imageUri = webviewView.webview.asWebviewUri(onDiskPath);
93116

@@ -155,7 +178,6 @@ class GuideViewProvider implements vscode.WebviewViewProvider {
155178
<summary>点击此处展开选择模型和设置APIKey</summary>
156179
<label for="apiKey">DeepSeek 官方 API Key:</label>
157180
<input type="text" id="apiKey" value="${apiKey}" placeholder="请输入您的 DeepSeek API 密钥" />
158-
<button id="saveApiKey">保存</button>
159181
<summary>自定义模型配置</summary>
160182
<label for="modelConfig">选择模型</label>
161183
<select id="modelConfig">
@@ -175,6 +197,7 @@ class GuideViewProvider implements vscode.WebviewViewProvider {
175197
<label for="customModelAPIKey">API Key:</label>
176198
<input type="text" id="customModelAPIKey" value="${customAPIKey}" placeholder="请输入自定义模型的 API Key" />
177199
</div>
200+
<button id="saveAllConfig">保存所有配置</button>
178201
</details>
179202
</div>
180203
@@ -211,9 +234,25 @@ class GuideViewProvider implements vscode.WebviewViewProvider {
211234
<script>
212235
const vscode = acquireVsCodeApi();
213236
214-
document.getElementById('saveApiKey').addEventListener('click', () => {
237+
document.getElementById('saveAllConfig').addEventListener('click', () => {
215238
const apiKey = document.getElementById('apiKey').value;
216-
vscode.postMessage({ command: 'saveApiKey', apiKey });
239+
const selectedModel = document.getElementById('modelConfig').value;
240+
const customBaseURL = document.getElementById('customBaseURL').value;
241+
const customModelName = document.getElementById('customModelName').value;
242+
const customModelNickname = document.getElementById('customModelNickname').value;
243+
const customAPIKey = document.getElementById('customModelAPIKey').value;
244+
245+
vscode.postMessage({
246+
command: 'saveAllConfig',
247+
data: {
248+
deepSeekApiKey: apiKey,
249+
selectedModel: selectedModel,
250+
customBaseURL: customBaseURL,
251+
customModelName: customModelName,
252+
customModelNickname: customModelNickname,
253+
customAPIKey: customAPIKey
254+
}
255+
});
217256
});
218257
219258
document.getElementById('modelConfig').addEventListener('change', (event) => {
@@ -239,5 +278,5 @@ class GuideViewProvider implements vscode.WebviewViewProvider {
239278
</body>
240279
</html>
241280
`;
242-
}
243-
}
281+
}
282+
}

0 commit comments

Comments
 (0)