Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e1ebd7c

Browse files
Add Preview Function(#132)
1 parent dbeddab commit e1ebd7c

File tree

8 files changed

+186
-95
lines changed

8 files changed

+186
-95
lines changed

‎package.json

Lines changed: 90 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@
4141
],
4242
"main": "./out/src/extension",
4343
"contributes": {
44-
"commands": [
45-
{
44+
"commands": [{
4645
"command": "leetcode.deleteCache",
4746
"title": "Delete Cache",
4847
"category": "LeetCode"
@@ -94,6 +93,11 @@
9493
"title": "Show Problem",
9594
"category": "LeetCode"
9695
},
96+
{
97+
"command": "leetcode.previewProblem",
98+
"title": "Preview Problem",
99+
"category": "LeetCode"
100+
},
97101
{
98102
"command": "leetcode.searchProblem",
99103
"title": "Search Problem",
@@ -120,25 +124,20 @@
120124
}
121125
],
122126
"viewsContainers": {
123-
"activitybar": [
124-
{
125-
"id": "leetcode",
126-
"title": "LeetCode",
127-
"icon": "resources/LeetCode.svg"
128-
}
129-
]
127+
"activitybar": [{
128+
"id": "leetcode",
129+
"title": "LeetCode",
130+
"icon": "resources/LeetCode.svg"
131+
}]
130132
},
131133
"views": {
132-
"leetcode": [
133-
{
134-
"id": "leetCodeExplorer",
135-
"name": "Problems"
136-
}
137-
]
134+
"leetcode": [{
135+
"id": "leetCodeExplorer",
136+
"name": "Problems"
137+
}]
138138
},
139139
"menus": {
140-
"view/title": [
141-
{
140+
"view/title": [{
142141
"command": "leetcode.toggleLeetCodeCn",
143142
"when": "view == leetCodeExplorer",
144143
"group": "navigation@0"
@@ -159,21 +158,22 @@
159158
"group": "navigation@3"
160159
}
161160
],
162-
"view/item/context": [
163-
{
161+
"view/item/context": [{
164162
"command": "leetcode.showProblem",
165163
"when": "view == leetCodeExplorer && viewItem == problem",
166164
"group": "leetcode@1"
167-
}
168-
],
169-
"commandPalette": [
165+
},
170166
{
171-
"command": "leetcode.showProblem",
172-
"when": "never"
167+
"command": "leetcode.previewProblem",
168+
"when": "view == leetCodeExplorer && viewItem == problem",
169+
"group": "leetcode@1"
173170
}
174171
],
175-
"explorer/context": [
176-
{
172+
"commandPalette": [{
173+
"command": "leetcode.showProblem",
174+
"when": "never"
175+
}],
176+
"explorer/context": [{
177177
"command": "leetcode.testSolution",
178178
"when": "explorerResourceIsFolder == false",
179179
"group": "leetcode@1"
@@ -184,8 +184,7 @@
184184
"group": "leetcode@2"
185185
}
186186
],
187-
"editor/context": [
188-
{
187+
"editor/context": [{
189188
"command": "leetcode.testSolution",
190189
"group": "leetcode@1"
191190
},
@@ -195,73 +194,71 @@
195194
}
196195
]
197196
},
198-
"configuration": [
199-
{
200-
"title": "LeetCode",
201-
"properties": {
202-
"leetcode.hideSolved": {
203-
"type": "boolean",
204-
"default": false,
205-
"scope": "application",
206-
"description": "Hide solved problems."
207-
},
208-
"leetcode.showLocked": {
209-
"type": "boolean",
210-
"default": false,
211-
"scope": "application",
212-
"description": "Show locked problems."
213-
},
214-
"leetcode.defaultLanguage": {
215-
"type": "string",
216-
"enum": [
217-
"bash",
218-
"c",
219-
"cpp",
220-
"csharp",
221-
"golang",
222-
"java",
223-
"javascript",
224-
"kotlin",
225-
"mysql",
226-
"python",
227-
"python3",
228-
"ruby",
229-
"scala",
230-
"swift"
231-
],
232-
"scope": "application",
233-
"description": "Default language for solving the problems."
234-
},
235-
"leetcode.showSetDefaultLanguageHint": {
236-
"type": "boolean",
237-
"default": true,
238-
"scope": "application",
239-
"description": "Show a hint to set the default language."
240-
},
241-
"leetcode.useWsl": {
242-
"type": "boolean",
243-
"default": false,
244-
"scope": "application",
245-
"description": "Use Node.js inside the Windows Subsystem for Linux."
246-
},
247-
"leetcode.endpoint": {
248-
"type": "string",
249-
"default": "leetcode",
250-
"scope": "application",
251-
"enum": [
252-
"leetcode",
253-
"leetcode-cn"
254-
],
255-
"description": "Endpoint of the user account."
256-
},
257-
"leetcode.outputFolder": {
258-
"type": "string",
259-
"scope": "application",
260-
"description": "Specify the relative path to save the problem files."
261-
}
197+
"configuration": [{
198+
"title": "LeetCode",
199+
"properties": {
200+
"leetcode.hideSolved": {
201+
"type": "boolean",
202+
"default": false,
203+
"scope": "application",
204+
"description": "Hide solved problems."
205+
},
206+
"leetcode.showLocked": {
207+
"type": "boolean",
208+
"default": false,
209+
"scope": "application",
210+
"description": "Show locked problems."
211+
},
212+
"leetcode.defaultLanguage": {
213+
"type": "string",
214+
"enum": [
215+
"bash",
216+
"c",
217+
"cpp",
218+
"csharp",
219+
"golang",
220+
"java",
221+
"javascript",
222+
"kotlin",
223+
"mysql",
224+
"python",
225+
"python3",
226+
"ruby",
227+
"scala",
228+
"swift"
229+
],
230+
"scope": "application",
231+
"description": "Default language for solving the problems."
232+
},
233+
"leetcode.showSetDefaultLanguageHint": {
234+
"type": "boolean",
235+
"default": true,
236+
"scope": "application",
237+
"description": "Show a hint to set the default language."
238+
},
239+
"leetcode.useWsl": {
240+
"type": "boolean",
241+
"default": false,
242+
"scope": "application",
243+
"description": "Use Node.js inside the Windows Subsystem for Linux."
244+
},
245+
"leetcode.endpoint": {
246+
"type": "string",
247+
"default": "leetcode",
248+
"scope": "application",
249+
"enum": [
250+
"leetcode",
251+
"leetcode-cn"
252+
],
253+
"description": "Endpoint of the user account."
254+
},
255+
"leetcode.outputFolder": {
256+
"type": "string",
257+
"scope": "application",
258+
"description": "Specify the relative path to save the problem files."
262259
}
263260
}
264-
]
261+
}]
265262
},
266263
"scripts": {
267264
"vscode:prepublish": "npm run compile",

‎src/commands/show.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import { LeetCodeNode } from "../explorer/LeetCodeNode";
88
import { leetCodeChannel } from "../leetCodeChannel";
99
import { leetCodeExecutor } from "../leetCodeExecutor";
1010
import { leetCodeManager } from "../leetCodeManager";
11-
import { IProblem, IQuickItemEx, languages, ProblemState } from "../shared";
11+
import { Command,IProblem, IQuickItemEx,IWebViewMessage, languages, ProblemState } from "../shared";
1212
import { DialogOptions, DialogType, promptForOpenOutputChannel, promptForSignIn } from "../utils/uiUtils";
13+
import { renderHTML } from "../utils/webviewUtils";
1314
import { selectWorkspaceFolder } from "../utils/workspaceUtils";
1415
import * as wsl from "../utils/wslUtils";
1516
import * as list from "./list";
@@ -135,3 +136,21 @@ async function resolveRelativePath(value: string, node: IProblem, selectedLangua
135136
throw new Error(errorMsg);
136137
}
137138
}
139+
140+
export async function previewProblem(node: IProblem): Promise<void> {
141+
const panelType: string = "previewProblem";
142+
const panelTitle: string = node.name;
143+
const panel: vscode.WebviewPanel = vscode.window.createWebviewPanel(panelType, panelTitle, vscode.ViewColumn.Active, {
144+
enableScripts: true,
145+
enableCommandUris: true,
146+
});
147+
panel.webview.onDidReceiveMessage(async (message: IWebViewMessage) => {
148+
switch (message.command) {
149+
case Command.ShowProblem:
150+
await showProblemInternal(node);
151+
panel.dispose();
152+
return;
153+
}
154+
});
155+
panel.webview.html = await renderHTML(node);
156+
}

‎src/explorer/LeetCodeNode.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ export class LeetCodeNode {
5050
return this.parentNodeName;
5151
}
5252

53+
public get previewCommand(): Command {
54+
return {
55+
title: "Preview Problem",
56+
command: "leetcode.previewProblem",
57+
arguments: [this],
58+
};
59+
}
60+
5361
public get selectedCommand(): Command {
5462
return {
5563
title: "Open Problem",

‎src/explorer/LeetCodeTreeDataProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCod
5252
collapsibleState: element.isProblem ? vscode.TreeItemCollapsibleState.None : vscode.TreeItemCollapsibleState.Collapsed,
5353
contextValue: element.isProblem ? "problem" : element.id.toLowerCase(),
5454
iconPath: this.parseIconPathFromProblemState(element),
55-
command: element.isProblem ? element.selectedCommand : undefined,
55+
command: element.isProblem ? element.previewCommand : undefined,
5656
};
5757
}
5858

‎src/extension.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
4343
vscode.commands.registerCommand("leetcode.signout", () => leetCodeManager.signOut()),
4444
vscode.commands.registerCommand("leetcode.selectSessions", () => session.selectSession()),
4545
vscode.commands.registerCommand("leetcode.createSession", () => session.createSession()),
46+
vscode.commands.registerCommand("leetcode.previewProblem", (node: LeetCodeNode) => show.previewProblem(node)),
4647
vscode.commands.registerCommand("leetcode.showProblem", (node: LeetCodeNode) => show.showProblem(node)),
4748
vscode.commands.registerCommand("leetcode.searchProblem", () => show.searchProblem()),
4849
vscode.commands.registerCommand("leetcode.refreshExplorer", () => leetCodeTreeDataProvider.refresh()),

‎src/leetCodeExecutor.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ class LeetCodeExecutor {
8787
return filePath;
8888
}
8989

90+
public async getDescription(node: IProblem): Promise<string> {
91+
return await this.executeCommandWithProgressEx("Fetching problem description...", "node", [await this.getLeetCodeBinaryPath(), "show", node.id, "-x"]);
92+
}
93+
9094
public async listSessions(): Promise<string> {
9195
return await this.executeCommandEx("node", [await this.getLeetCodeBinaryPath(), "session"]);
9296
}

‎src/shared.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,11 @@ export enum Category {
9191
Company = "Company",
9292
Favorite = "Favorite",
9393
}
94+
95+
export enum Command {
96+
ShowProblem = "ShowProblem",
97+
}
98+
99+
export interface IWebViewMessage {
100+
command: string;
101+
}

‎src/utils/webviewUtils.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { leetCodeExecutor } from "../leetCodeExecutor";
2+
import { Command, IProblem } from "../shared";
3+
4+
export async function renderHTML(node: IProblem): Promise<string> {
5+
const description: string = await leetCodeExecutor.getDescription(node);
6+
const descriptionHTML: string = description.replace(/\n/g, "<br>");
7+
const htmlTemplate: string = `
8+
<!DOCTYPE html>
9+
<html>
10+
<head>
11+
<meta charset="UTF-8">
12+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
13+
<title>Preview Problem</title>
14+
</head>
15+
<style>
16+
#solve {
17+
position: fixed;
18+
bottom: 10px;
19+
right: 10px;
20+
border: 0;
21+
margin: 10px 0;
22+
padding: 2px 14px;
23+
color: white;
24+
background-color: rgb(14, 99, 156);
25+
}
26+
#solve:hover {
27+
background-color: rgb(17, 119, 187);
28+
}
29+
#solve:active {
30+
border: 0;
31+
}
32+
</style>
33+
<body>
34+
<div >
35+
${ descriptionHTML}
36+
</div>
37+
<button id="solve">Code Now</button>
38+
<script>
39+
(function() {
40+
const vscode = acquireVsCodeApi();
41+
let button = document.getElementById('solve');
42+
button.onclick = solveHandler;
43+
function solveHandler() {
44+
vscode.postMessage({
45+
command: '${Command.ShowProblem}',
46+
})
47+
}
48+
}())
49+
</script>
50+
</body>
51+
</html>
52+
`;
53+
return htmlTemplate;
54+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /