Skip to content

Add Click the tree explorer should open the topic (#132) #186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 12, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@
"command": "leetcode.showProblem",
"title": "Show Problem",
"category": "LeetCode"
},
{
"command": "leetcode.previewProblem",
"title": "Preview Problem",
"category": "LeetCode"
},
{
"command": "leetcode.searchProblem",
Expand Down Expand Up @@ -164,6 +169,11 @@
"command": "leetcode.showProblem",
"when": "view == leetCodeExplorer && viewItem == problem",
"group": "leetcode@1"
},
{
"command": "leetcode.previewProblem",
"when": "view == leetCodeExplorer && viewItem == problem",
"group": "leetcode@1"
}
],
"commandPalette": [
Expand Down
21 changes: 20 additions & 1 deletion src/commands/show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import { LeetCodeNode } from "../explorer/LeetCodeNode";
import { leetCodeChannel } from "../leetCodeChannel";
import { leetCodeExecutor } from "../leetCodeExecutor";
import { leetCodeManager } from "../leetCodeManager";
import { IProblem, IQuickItemEx, languages, ProblemState } from "../shared";
import { Command, IProblem, IQuickItemEx, IWebViewMessage, languages, ProblemState } from "../shared";
import { DialogOptions, DialogType, promptForOpenOutputChannel, promptForSignIn } from "../utils/uiUtils";
import { renderHTML } from "../utils/webviewUtils";
import { selectWorkspaceFolder } from "../utils/workspaceUtils";
import * as wsl from "../utils/wslUtils";
import * as list from "./list";
Expand Down Expand Up @@ -135,3 +136,21 @@ async function resolveRelativePath(value: string, node: IProblem, selectedLangua
throw new Error(errorMsg);
}
}

export async function previewProblem(node: IProblem): Promise<void> {
const panelType: string = "previewProblem";
const panelTitle: string = node.name;
const panel: vscode.WebviewPanel = vscode.window.createWebviewPanel(panelType, panelTitle, vscode.ViewColumn.Active, {
enableScripts: true,
enableCommandUris: true,
});
panel.webview.onDidReceiveMessage(async (message: IWebViewMessage) => {
switch (message.command) {
case Command.ShowProblem:
await showProblemInternal(node);
panel.dispose();
return;
}
});
panel.webview.html = await renderHTML(node);
}
17 changes: 17 additions & 0 deletions src/explorer/LeetCodeNode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) jdneo. All rights reserved.
// Licensed under the MIT license.

import { Command } from "vscode";
import { IProblem, ProblemState } from "../shared";

export class LeetCodeNode {
Expand Down Expand Up @@ -48,4 +49,20 @@ export class LeetCodeNode {
public get parentName(): string {
return this.parentNodeName;
}

public get previewCommand(): Command {
return {
title: "Preview Problem",
command: "leetcode.previewProblem",
arguments: [this],
};
}

public get selectedCommand(): Command {
return {
title: "Open Problem",
command: "leetcode.showProblem",
arguments: [this],
};
}
}
1 change: 1 addition & 0 deletions src/explorer/LeetCodeTreeDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export class LeetCodeTreeDataProvider implements vscode.TreeDataProvider<LeetCod
collapsibleState: element.isProblem ? vscode.TreeItemCollapsibleState.None : vscode.TreeItemCollapsibleState.Collapsed,
contextValue: element.isProblem ? "problem" : element.id.toLowerCase(),
iconPath: this.parseIconPathFromProblemState(element),
command: element.isProblem ? element.previewCommand : undefined,
};
}

Expand Down
1 change: 1 addition & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
vscode.commands.registerCommand("leetcode.signout", () => leetCodeManager.signOut()),
vscode.commands.registerCommand("leetcode.selectSessions", () => session.selectSession()),
vscode.commands.registerCommand("leetcode.createSession", () => session.createSession()),
vscode.commands.registerCommand("leetcode.previewProblem", (node: LeetCodeNode) => show.previewProblem(node)),
vscode.commands.registerCommand("leetcode.showProblem", (node: LeetCodeNode) => show.showProblem(node)),
vscode.commands.registerCommand("leetcode.searchProblem", () => show.searchProblem()),
vscode.commands.registerCommand("leetcode.refreshExplorer", () => leetCodeTreeDataProvider.refresh()),
Expand Down
4 changes: 4 additions & 0 deletions src/leetCodeExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ class LeetCodeExecutor {
return filePath;
}

public async getDescription(node: IProblem): Promise<string> {
return await this.executeCommandWithProgressEx("Fetching problem description...", "node", [await this.getLeetCodeBinaryPath(), "show", node.id, "-x"]);
}

public async listSessions(): Promise<string> {
return await this.executeCommandEx("node", [await this.getLeetCodeBinaryPath(), "session"]);
}
Expand Down
8 changes: 8 additions & 0 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,11 @@ export enum Category {
Company = "Company",
Favorite = "Favorite",
}

export enum Command {
ShowProblem = "ShowProblem",
}

export interface IWebViewMessage {
command: string;
}
54 changes: 54 additions & 0 deletions src/utils/webviewUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { leetCodeExecutor } from "../leetCodeExecutor";
import { Command, IProblem } from "../shared";

export async function renderHTML(node: IProblem): Promise<string> {
const description: string = await leetCodeExecutor.getDescription(node);
const descriptionHTML: string = description.replace(/\n/g, "<br>");
const htmlTemplate: string = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Preview Problem</title>
</head>
<style>
#solve {
position: fixed;
bottom: 10px;
right: 10px;
border: 0;
margin: 10px 0;
padding: 2px 14px;
color: white;
background-color: rgb(14, 99, 156);
}
#solve:hover {
background-color: rgb(17, 119, 187);
}
#solve:active {
border: 0;
}
</style>
<body>
<div >
${ descriptionHTML}
</div>
<button id="solve">Code Now</button>
<script>
(function() {
const vscode = acquireVsCodeApi();
let button = document.getElementById('solve');
button.onclick = solveHandler;
function solveHandler() {
vscode.postMessage({
command: '${Command.ShowProblem}',
})
}
}())
</script>
</body>
</html>
`;
return htmlTemplate;
}