Skip to content

Commit 7f1f732

Browse files
committed
Make two event callback inheritable class method
1 parent 90c9480 commit 7f1f732

File tree

2 files changed

+42
-30
lines changed

2 files changed

+42
-30
lines changed

src/webview/LeetCodeWebview.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,8 @@ export abstract class LeetCodeWebview implements Disposable {
1111
private listener: Disposable;
1212

1313
public initialize(context: ExtensionContext): void {
14-
const { onDidChangeConfiguration } = this.getWebviewOption();
1514
this.context = context;
16-
if (onDidChangeConfiguration) {
17-
this.listener = workspace.onDidChangeConfiguration(onDidChangeConfiguration, this);
18-
} else {
19-
this.listener = workspace.onDidChangeConfiguration((event: ConfigurationChangeEvent) => {
20-
if (event.affectsConfiguration("markdown") && this.panel) {
21-
this.panel.webview.html = this.getWebviewContent();
22-
}
23-
}, this);
24-
}
15+
this.listener = workspace.onDidChangeConfiguration(this.onDidChangeConfiguration, this);
2516
}
2617

2718
public dispose(): void {
@@ -33,7 +24,7 @@ export abstract class LeetCodeWebview implements Disposable {
3324

3425
protected showWebviewInternal(): this is { panel: WebviewPanel } {
3526
if (!this.panel) {
36-
const { viewType, title, viewColumn, onDidReceiveMessage } = this.getWebviewOption();
27+
const { viewType, title, viewColumn } = this.getWebviewOption();
3728

3829
this.panel = window.createWebviewPanel(viewType, title, viewColumn || ViewColumn.One, {
3930
enableScripts: true,
@@ -47,15 +38,21 @@ export abstract class LeetCodeWebview implements Disposable {
4738
this.panel = undefined;
4839
}, null, this.context.subscriptions);
4940

50-
if (onDidReceiveMessage) {
51-
this.panel.webview.onDidReceiveMessage(onDidReceiveMessage, this, this.context.subscriptions);
52-
}
41+
this.panel.webview.onDidReceiveMessage(this.onDidReceiveMessage, this, this.context.subscriptions);
5342
}
5443

5544
this.panel.webview.html = this.getWebviewContent();
5645
return true;
5746
}
5847

48+
protected async onDidChangeConfiguration(event: ConfigurationChangeEvent): Promise<void> {
49+
if (this.panel && event.affectsConfiguration("markdown")) {
50+
this.panel.webview.html = this.getWebviewContent();
51+
}
52+
}
53+
54+
protected async onDidReceiveMessage(/* message */_: any): Promise<void> { /* no special rule */ }
55+
5956
protected abstract getWebviewOption(): ILeetCodeWebviewOption;
6057

6158
protected abstract getWebviewContent(): string;
@@ -65,6 +62,4 @@ export interface ILeetCodeWebviewOption {
6562
viewType: string;
6663
title: string;
6764
viewColumn?: ViewColumn;
68-
onDidReceiveMessage?: (message: any) => Promise<void>;
69-
onDidChangeConfiguration?: (event: ConfigurationChangeEvent) => Promise<void>;
7065
}

src/webview/leetCodePreviewProvider.ts

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,47 @@ class LeetCodePreviewProvider extends LeetCodeWebview {
1111

1212
private node: IProblem;
1313
private description: IDescription;
14+
private sideMode: boolean;
1415

15-
public async show(node: IProblem): Promise<void> {
16-
const descString: string = await leetCodeExecutor.getDescription(node);
16+
public isSideMode(): boolean {
17+
return this.sideMode;
18+
}
19+
20+
public async show(node: IProblem, isSideMode: boolean = false): Promise<void> {
21+
this.description = this.parseDescription(await leetCodeExecutor.getDescription(node), node);
1722
this.node = node;
18-
this.description = this.parseDescription(descString, node);
23+
this.sideMode = isSideMode;
1924
if (this.showWebviewInternal()) {
20-
this.panel.webview.html = this.getWebviewContent();
21-
this.panel.title = `${node.name}: Preview`;
22-
this.panel.reveal(ViewColumn.One);
25+
if (!this.sideMode) {
26+
this.panel.title = `${node.name}: Preview`;
27+
this.panel.reveal(ViewColumn.One);
28+
} else {
29+
this.panel.title = "Description";
30+
await Promise.all([
31+
commands.executeCommand("workbench.action.focusSideBar").then(() => {
32+
commands.executeCommand("workbench.action.toggleSidebarVisibility");
33+
}),
34+
commands.executeCommand("workbench.action.focusSecondEditorGroup").then(() => {
35+
this.panel!.reveal(ViewColumn.Two);
36+
}),
37+
]);
38+
}
39+
}
40+
}
41+
42+
protected async onDidReceiveMessage(message: IWebViewMessage): Promise<void> {
43+
switch (message.command) {
44+
case "ShowProblem": {
45+
await commands.executeCommand("leetcode.showProblem", this.node);
46+
break;
47+
}
2348
}
2449
}
2550

2651
protected getWebviewOption(): ILeetCodeWebviewOption {
2752
return {
2853
viewType: "leetcode.preview",
2954
title: "Preview Problem",
30-
onDidReceiveMessage: async (message: IWebViewMessage): Promise<void> => {
31-
switch (message.command) {
32-
case "ShowProblem": {
33-
await commands.executeCommand("leetcode.showProblem", this.node);
34-
break;
35-
}
36-
}
37-
},
3855
};
3956
}
4057

0 commit comments

Comments
 (0)