|
| 1 | +// Copyright (c) jdneo. All rights reserved. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +import { Disposable, ExtensionContext, ViewColumn, WebviewPanel, window } from "vscode"; |
| 5 | + |
| 6 | +class LeetCodeResultProvider implements Disposable { |
| 7 | + |
| 8 | + private context: ExtensionContext; |
| 9 | + private panel: WebviewPanel | undefined; |
| 10 | + |
| 11 | + public initialize(context: ExtensionContext): void { |
| 12 | + this.context = context; |
| 13 | + } |
| 14 | + |
| 15 | + public async show(result: string): Promise<void> { |
| 16 | + if (!this.panel) { |
| 17 | + this.panel = window.createWebviewPanel("leetCode", "LeetCode Results", ViewColumn.Active, { |
| 18 | + retainContextWhenHidden: true, |
| 19 | + enableFindWidget: true, |
| 20 | + }); |
| 21 | + |
| 22 | + this.panel.onDidDispose(() => { |
| 23 | + this.panel = undefined; |
| 24 | + }, null, this.context.subscriptions); |
| 25 | + } |
| 26 | + |
| 27 | + this.panel.webview.html = await this.provideHtmlContent(result); |
| 28 | + this.panel.reveal(ViewColumn.Active); |
| 29 | + } |
| 30 | + |
| 31 | + public dispose(): void { |
| 32 | + if (this.panel) { |
| 33 | + this.panel.dispose(); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private async provideHtmlContent(result: string): Promise<string> { |
| 38 | + return `<!DOCTYPE html> |
| 39 | + <html lang="en"> |
| 40 | + <head> |
| 41 | + <meta charset="UTF-8"> |
| 42 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 43 | + <title>LeetCode Results</title> |
| 44 | + </head> |
| 45 | + <body> |
| 46 | + <pre>${result.trim()}</pre> |
| 47 | + </body> |
| 48 | + </html>`; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +export const leetCodeResultProvider: LeetCodeResultProvider = new LeetCodeResultProvider(); |
0 commit comments