forked from LeetCode-OpenSource/vscode-leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetCodePreviewProvider.ts
192 lines (178 loc) · 6.27 KB
/
leetCodePreviewProvider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright (c) jdneo. All rights reserved.
// Licensed under the MIT license.
import { commands, ViewColumn } from "vscode";
import { IProblem } from "../shared";
import { ILeetCodeWebviewOption, LeetCodeWebview } from "./LeetCodeWebview";
import { markdownEngine } from "./markdownEngine";
class LeetCodePreviewProvider extends LeetCodeWebview {
protected readonly viewType: string = "leetcode.preview";
private node: IProblem;
private description: IDescription;
private sideMode: boolean = false;
public isSideMode(): boolean {
return this.sideMode;
}
public show(descString: string, node: IProblem, isSideMode: boolean = false): void {
this.description = this.parseDescription(descString, node);
this.node = node;
this.sideMode = isSideMode;
this.showWebviewInternal();
// Comment out this operation since it sometimes may cause the webview become empty.
// Waiting for the progress of the VS Code side issue: https://github.com/microsoft/vscode/issues/3742
// if (this.sideMode) {
// this.hideSideBar(); // For better view area
// }
}
protected getWebviewOption(): ILeetCodeWebviewOption {
if (!this.sideMode) {
return {
title: `${this.node.name}: Preview`,
viewColumn: ViewColumn.One,
};
} else {
return {
title: "Description",
viewColumn: ViewColumn.Two,
preserveFocus: true,
};
}
}
protected getWebviewContent(): string {
const button: { element: string, script: string, style: string } = {
element: `<button id="solve">Code Now</button>`,
script: `const button = document.getElementById('solve');
button.onclick = () => vscode.postMessage({
command: 'ShowProblem',
});`,
style: `<style>
#solve {
position: fixed;
bottom: 1rem;
right: 1rem;
border: 0;
margin: 1rem 0;
padding: 0.2rem 1rem;
color: white;
background-color: var(--vscode-button-background);
}
#solve:hover {
background-color: var(--vscode-button-hoverBackground);
}
#solve:active {
border: 0;
}
</style>`,
};
const { title, url, category, difficulty, likes, dislikes, body } = this.description;
const head: string = markdownEngine.render(`# [${title}](${url})`);
const info: string = markdownEngine.render([
`| Category | Difficulty | Likes | Dislikes |`,
`| :------: | :--------: | :---: | :------: |`,
`| ${category} | ${difficulty} | ${likes} | ${dislikes} |`,
].join("\n"));
const tags: string = [
`<details>`,
`<summary><strong>Tags</strong></summary>`,
markdownEngine.render(
this.description.tags
.map((t: string) => `[\`${t}\`](https://leetcode.com/tag/${t})`)
.join(" | "),
),
`</details>`,
].join("\n");
const companies: string = [
`<details>`,
`<summary><strong>Companies</strong></summary>`,
markdownEngine.render(
this.description.companies
.map((c: string) => `\`${c}\``)
.join(" | "),
),
`</details>`,
].join("\n");
return `
<!DOCTYPE html>
<html>
<head>
${markdownEngine.getStyles()}
${!this.sideMode ? button.style : ""}
<style>
code { white-space: pre-wrap; }
</style>
</head>
<body>
${head}
${info}
${tags}
${companies}
${body}
${!this.sideMode ? button.element : ""}
<script>
const vscode = acquireVsCodeApi();
${!this.sideMode ? button.script : ""}
</script>
</body>
</html>
`;
}
protected onDidDisposeWebview(): void {
super.onDidDisposeWebview();
delete this.node;
delete this.description;
this.sideMode = false;
}
protected async onDidReceiveMessage(message: IWebViewMessage): Promise<void> {
switch (message.command) {
case "ShowProblem": {
await commands.executeCommand("leetcode.showProblem", this.node);
break;
}
}
}
// private async hideSideBar(): Promise<void> {
// await commands.executeCommand("workbench.action.focusSideBar");
// await commands.executeCommand("workbench.action.toggleSidebarVisibility");
// }
private parseDescription(descString: string, problem: IProblem): IDescription {
const [
/* title */, ,
url, ,
/* tags */, ,
/* langs */, ,
category,
difficulty,
likes,
dislikes,
/* accepted */,
/* submissions */,
/* testcase */, ,
...body
] = descString.split("\n");
return {
title: problem.name,
url,
tags: problem.tags,
companies: problem.companies,
category: category.slice(2),
difficulty: difficulty.slice(2),
likes: likes.split(": ")[1].trim(),
dislikes: dislikes.split(": ")[1].trim(),
body: body.join("\n").replace(/<pre>[\r\n]*([^]+?)[\r\n]*<\/pre>/g, "<pre><code>$1</code></pre>"),
};
}
}
interface IDescription {
title: string;
url: string;
tags: string[];
companies: string[];
category: string;
difficulty: string;
likes: string;
dislikes: string;
body: string;
}
interface IWebViewMessage {
command: string;
}
export const leetCodePreviewProvider: LeetCodePreviewProvider = new LeetCodePreviewProvider();