|
| 1 | +import { commands, Disposable, ExtensionContext, ViewColumn, WebviewPanel, window } from "vscode"; |
| 2 | +import { leetCodeExecutor } from "./leetCodeExecutor"; |
| 3 | +import { IProblem } from "./shared"; |
| 4 | +class LeetCodePreviewProvider implements Disposable { |
| 5 | + |
| 6 | +private context: ExtensionContext; |
| 7 | +private panel: WebviewPanel | undefined; |
| 8 | + |
| 9 | +public initialize(context: ExtensionContext): void { |
| 10 | +this.context = context; |
| 11 | +} |
| 12 | + |
| 13 | +public async preview(node: IProblem): Promise<void> { |
| 14 | +if (!this.panel) { |
| 15 | +const panelType: string = "previewProblem"; |
| 16 | +const panelTitle: string = node.name; |
| 17 | +this.panel = window.createWebviewPanel(panelType, panelTitle, ViewColumn.Active, { |
| 18 | +enableScripts: true, |
| 19 | +enableCommandUris: true, |
| 20 | +enableFindWidget: true, |
| 21 | +retainContextWhenHidden: true, |
| 22 | +}); |
| 23 | +} |
| 24 | + |
| 25 | +this.panel.onDidDispose(() => { |
| 26 | +this.panel = undefined; |
| 27 | +}, null, this.context.subscriptions); |
| 28 | + |
| 29 | +this.panel.webview.onDidReceiveMessage(async (message: IWebViewMessage) => { |
| 30 | +switch (message.command) { |
| 31 | +case "ShowProblem": |
| 32 | +await commands.executeCommand("leetcode.showProblem", node); |
| 33 | +this.dispose(); |
| 34 | +return; |
| 35 | +} |
| 36 | +}); |
| 37 | +this.panel.webview.html = await this.provideHtmlContent(node); |
| 38 | +} |
| 39 | + |
| 40 | +public dispose(): void { |
| 41 | +if (this.panel) { |
| 42 | +this.panel.dispose(); |
| 43 | +} |
| 44 | +} |
| 45 | + |
| 46 | +public async provideHtmlContent(node: IProblem): Promise<string> { |
| 47 | +return await this.renderHTML(node); |
| 48 | +} |
| 49 | + |
| 50 | +private async renderHTML(node: IProblem): Promise<string> { |
| 51 | +const description: string = await leetCodeExecutor.getDescription(node); |
| 52 | +const descriptionHTML: string = description.replace(/\n/g, "<br>"); |
| 53 | +const htmlTemplate: string = ` |
| 54 | +<!DOCTYPE html> |
| 55 | +<html> |
| 56 | +<head> |
| 57 | +<meta charset="UTF-8"> |
| 58 | +<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 59 | +<title>Preview Problem</title> |
| 60 | +</head> |
| 61 | +<style> |
| 62 | +#solve { |
| 63 | +position: fixed; |
| 64 | +bottom: 1rem; |
| 65 | +right: 1rem; |
| 66 | +border: 0; |
| 67 | +margin: 1rem 0; |
| 68 | +padding: 0.2rem 1rem; |
| 69 | +color: white; |
| 70 | +background-color: var(--vscode-button-background); |
| 71 | +} |
| 72 | +#solve:hover { |
| 73 | +background-color: var(--vscode-button-hoverBackground); |
| 74 | +} |
| 75 | +#solve:active { |
| 76 | +border: 0; |
| 77 | +} |
| 78 | +</style> |
| 79 | +<body> |
| 80 | +<div > |
| 81 | +${ descriptionHTML} |
| 82 | +</div> |
| 83 | +<button id="solve">Code Now</button> |
| 84 | +<script> |
| 85 | +(function() { |
| 86 | +const vscode = acquireVsCodeApi(); |
| 87 | +let button = document.getElementById('solve'); |
| 88 | +button.onclick = solveHandler; |
| 89 | +function solveHandler() { |
| 90 | +vscode.postMessage({ |
| 91 | +command: 'ShowProblem', |
| 92 | +}); |
| 93 | +} |
| 94 | +}()); |
| 95 | +</script> |
| 96 | +</body> |
| 97 | +</html> |
| 98 | +`; |
| 99 | +return htmlTemplate; |
| 100 | +} |
| 101 | + |
| 102 | +} |
| 103 | +export interface IWebViewMessage { |
| 104 | +command: string; |
| 105 | +} |
| 106 | + |
| 107 | +export const leetCodePreviewProvider: LeetCodePreviewProvider = new LeetCodePreviewProvider(); |
0 commit comments