Skip to content

Commit 59a7207

Browse files
committed
update: 更新、替换 reload命令 (#568)
1 parent 7471196 commit 59a7207

File tree

3 files changed

+46
-51
lines changed

3 files changed

+46
-51
lines changed

src/background/Background.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -155,25 +155,20 @@ export class Background implements Disposable {
155155
if (!enabled) {
156156
if (hasInstalled) {
157157
await this.uninstall();
158-
vsHelp.showInfoReload(l10n.t('Background has been disabled! Please reload.'));
159-
}
160-
return;
161-
}
162158

163-
// 更新,需要二次确认
164-
const confirm = await vscode.window.showInformationMessage(
165-
l10n.t('Configuration has been changed, click to update.'),
166-
{
167-
title: l10n.t('Update and Reload')
159+
vsHelp.reload({
160+
message: l10n.t('Background has been disabled! Please reload.')
161+
});
168162
}
169-
);
170-
171-
if (!confirm) {
172163
return;
173164
}
174165

175-
await this.applyPatch();
176-
vscode.commands.executeCommand('workbench.action.reloadWindow');
166+
// 更新,需要二次确认
167+
vsHelp.reload({
168+
message: l10n.t('Configuration has been changed, click to update.'),
169+
btnReload: l10n.t('Update and Reload'),
170+
beforeReload: () => this.applyPatch()
171+
});
177172
}
178173

179174
public async applyPatch() {
@@ -207,8 +202,11 @@ export class Background implements Disposable {
207202
if (this.config.enabled) {
208203
// 此时一般为 「background更新」、「vscode更新」
209204
if ([EFilePatchType.Legacy, EFilePatchType.None].includes(patchType)) {
205+
// 提示: background 可更新
210206
if (await this.applyPatch()) {
211-
vsHelp.showInfoReload(l10n.t('Background has been changed! Please reload.'));
207+
vsHelp.reload({
208+
message: l10n.t('Background has been changed! Please reload.')
209+
});
212210
}
213211
}
214212
}

src/extension.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
4040
vscode.commands.registerCommand('extension.background.install', async () => {
4141
await background.config.update('enabled', true, true);
4242
await background.applyPatch();
43-
await vscode.commands.executeCommand('workbench.action.reloadWindow');
43+
await vsHelp.reload();
4444
})
4545
);
4646

4747
context.subscriptions.push(
4848
vscode.commands.registerCommand('extension.background.disable', async () => {
4949
await background.config.update('enabled', false, true);
5050
await background.uninstall();
51-
await vscode.commands.executeCommand('workbench.action.reloadWindow');
51+
await vsHelp.reload();
5252
})
5353
);
5454

@@ -58,7 +58,9 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
5858
// 当且仅当成功删除样式时才会卸载扩展
5959
// 否则可能导致没有成功删掉样式时扩展就被卸载掉
6060
await vscode.commands.executeCommand('workbench.extensions.uninstallExtension', EXTENSION_ID);
61-
await vsHelp.showInfoReload(l10n.t('Background extension has been uninstalled. See you next time!'));
61+
vsHelp.reload({
62+
message: l10n.t('Background extension has been uninstalled. See you next time!')
63+
});
6264
}
6365
})
6466
);

src/utils/vsHelp.ts

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,37 @@
11
import vscode, { l10n } from 'vscode';
22

3-
// class ReloadOptions {
4-
// message = '';
5-
// btnReload = l10n.t('Reload vscode');
6-
// beforeReload?: () => void | Promise<void>;
7-
// }
3+
class ReloadOptions {
4+
/** reload 提示内容 */
5+
message = '';
6+
/** reload 按钮文案 */
7+
btnReload = l10n.t('Reload vscode');
8+
/** reload 前置动作 */
9+
beforeReload?: () => any;
10+
}
811

912
export const vsHelp = {
10-
// async reload(options: Partial<ReloadOptions> = {}): Promise<void> {
11-
// // 填充默认值
12-
// options = {
13-
// ...new ReloadOptions(),
14-
// ...options
15-
// };
16-
// // 如果需要确认
17-
// if (options.message) {
18-
// const goon = await vscode.window.showInformationMessage(options.message, { title: options.btnReload! });
19-
// // 关闭了 提示框
20-
// if (!goon) {
21-
// return Promise.resolve();
22-
// }
23-
24-
// if (options.beforeReload) {
25-
// await options.beforeReload();
26-
// }
27-
// }
28-
// return vscode.commands.executeCommand('workbench.action.reloadWindow');
29-
// },
3013
/**
31-
* 提示内容并重载
14+
* 重新加载 vscode
3215
*
33-
* @param {string} message 提示内容
34-
* @returns {Thenable<void>}
16+
* @param {Partial<ReloadOptions>} [options={}] 定义重新加载的相关配置
17+
* @return {*} {Promise<void>}
3518
*/
36-
showInfoReload(message: string): Thenable<void> {
37-
return vscode.window.showInformationMessage(message, { title: l10n.t('Reload vscode') }).then(function (item) {
38-
if (!item) return;
39-
vscode.commands.executeCommand('workbench.action.reloadWindow');
40-
});
19+
async reload(options: Partial<ReloadOptions> = {}): Promise<void> {
20+
// 填充默认值
21+
options = {
22+
...new ReloadOptions(),
23+
...options
24+
};
25+
// 如果需要确认
26+
if (options.message) {
27+
const goon = await vscode.window.showInformationMessage(options.message, { title: options.btnReload! });
28+
// 关闭了 提示框
29+
if (!goon) {
30+
return Promise.resolve();
31+
}
32+
// 关闭前置动作
33+
await options.beforeReload?.();
34+
}
35+
return vscode.commands.executeCommand('workbench.action.reloadWindow');
4136
}
4237
};

0 commit comments

Comments
 (0)