forked from LeetCode-OpenSource/vscode-leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetCodePreviewProvider.ts
113 lines (101 loc) · 3.81 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
// Copyright (c) jdneo. All rights reserved.
// Licensed under the MIT license.
import { commands, Disposable, ExtensionContext, ViewColumn, WebviewPanel, window } from "vscode";
import { leetCodeExecutor } from "../leetCodeExecutor";
import { IProblem } from "../shared";
class LeetCodePreviewProvider implements Disposable {
private context: ExtensionContext;
private node: IProblem;
private panel: WebviewPanel | undefined;
public initialize(context: ExtensionContext): void {
this.context = context;
}
public async preview(node: IProblem): Promise<void> {
this.node = node;
if (!this.panel) {
this.panel = window.createWebviewPanel("leetcode.preview", "Preview Problem", ViewColumn.Active, {
enableScripts: true,
enableCommandUris: true,
enableFindWidget: true,
retainContextWhenHidden: true,
});
this.panel.webview.onDidReceiveMessage(async (message: IWebViewMessage) => {
switch (message.command) {
case "ShowProblem":
await commands.executeCommand("leetcode.showProblem", this.node);
this.dispose();
return;
}
}, this, this.context.subscriptions);
this.panel.onDidDispose(() => {
this.panel = undefined;
}, null, this.context.subscriptions);
}
this.panel.webview.html = await this.provideHtmlContent(node);
this.panel.title = `${node.name}: Preview`;
this.panel.reveal();
}
public dispose(): void {
if (this.panel) {
this.panel.dispose();
}
}
public async provideHtmlContent(node: IProblem): Promise<string> {
return await this.renderHTML(node);
}
private async 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: 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>
<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: 'ShowProblem',
});
}
}());
</script>
</body>
</html>
`;
return htmlTemplate;
}
}
export interface IWebViewMessage {
command: string;
}
export const leetCodePreviewProvider: LeetCodePreviewProvider = new LeetCodePreviewProvider();