1
1
// Copyright (c) jdneo. All rights reserved.
2
2
// Licensed under the MIT license.
3
3
4
- import { commands , Disposable , ExtensionContext , ViewColumn , WebviewPanel , window } from "vscode" ;
4
+ import { commands , ViewColumn } from "vscode" ;
5
5
import { leetCodeExecutor } from "../leetCodeExecutor" ;
6
6
import { IProblem } from "../shared" ;
7
+ import { ILeetCodeWebviewOption , LeetCodeWebview } from "./LeetCodeWebview" ;
7
8
import { markdownEngine } from "./markdownEngine" ;
8
9
9
- class LeetCodePreviewProvider implements Disposable {
10
+ class LeetCodePreviewProvider extends LeetCodeWebview {
10
11
11
- private context : ExtensionContext ;
12
12
private node : IProblem ;
13
- private panel : WebviewPanel | undefined ;
14
-
15
- public initialize ( context : ExtensionContext ) : void {
16
- this . context = context ;
17
- }
13
+ private description : IDescription ;
18
14
19
15
public async show ( node : IProblem ) : Promise < void > {
20
- // Fetch problem first before creating webview panel
21
16
const descString : string = await leetCodeExecutor . getDescription ( node ) ;
22
-
23
17
this . node = node ;
24
- if ( ! this . panel ) {
25
- this . panel = window . createWebviewPanel ( "leetcode.preview" , "Preview Problem" , ViewColumn . One , {
26
- enableScripts : true ,
27
- enableCommandUris : true ,
28
- enableFindWidget : true ,
29
- retainContextWhenHidden : true ,
30
- localResourceRoots : markdownEngine . localResourceRoots ,
31
- } ) ;
18
+ this . description = this . parseDescription ( descString , node ) ;
19
+ if ( this . showWebviewInternal ( ) ) {
20
+ this . panel . webview . html = this . getWebviewContent ( ) ;
21
+ this . panel . title = `${ node . name } : Preview` ;
22
+ this . panel . reveal ( ViewColumn . One ) ;
23
+ }
24
+ }
32
25
33
- this . panel . webview . onDidReceiveMessage ( async ( message : IWebViewMessage ) => {
26
+ protected getWebviewOption ( ) : ILeetCodeWebviewOption {
27
+ return {
28
+ viewType : "leetcode.preview" ,
29
+ title : "Preview Problem" ,
30
+ onDidReceiveMessage : async ( message : IWebViewMessage ) : Promise < void > => {
34
31
switch ( message . command ) {
35
32
case "ShowProblem" : {
36
33
await commands . executeCommand ( "leetcode.showProblem" , this . node ) ;
37
34
break ;
38
35
}
39
36
}
40
- } , this , this . context . subscriptions ) ;
41
-
42
- this . panel . onDidDispose ( ( ) => {
43
- this . panel = undefined ;
44
- } , null , this . context . subscriptions ) ;
45
- }
46
-
47
- const description : IDescription = this . parseDescription ( descString , node ) ;
48
- this . panel . webview . html = this . getWebViewContent ( description ) ;
49
- this . panel . title = `${ node . name } : Preview` ;
50
- this . panel . reveal ( ViewColumn . One ) ;
51
- }
52
-
53
- public dispose ( ) : void {
54
- if ( this . panel ) {
55
- this . panel . dispose ( ) ;
56
- }
57
- }
58
-
59
- private parseDescription ( descString : string , problem : IProblem ) : IDescription {
60
- const [
61
- /* title */ , ,
62
- url , ,
63
- /* tags */ , ,
64
- /* langs */ , ,
65
- category ,
66
- difficulty ,
67
- likes ,
68
- dislikes ,
69
- /* accepted */ ,
70
- /* submissions */ ,
71
- /* testcase */ , ,
72
- ...body
73
- ] = descString . split ( "\n" ) ;
74
- return {
75
- title : problem . name ,
76
- url,
77
- tags : problem . tags ,
78
- companies : problem . companies ,
79
- category : category . slice ( 2 ) ,
80
- difficulty : difficulty . slice ( 2 ) ,
81
- likes : likes . split ( ": " ) [ 1 ] . trim ( ) ,
82
- dislikes : dislikes . split ( ": " ) [ 1 ] . trim ( ) ,
83
- body : body . join ( "\n" ) . replace ( / < p r e > \s * ( [ ^ ] + ?) \s * < \/ p r e > / g, "<pre><code>$1</code></pre>" ) ,
37
+ } ,
84
38
} ;
85
39
}
86
40
87
- private getWebViewContent ( desc : IDescription ) : string {
41
+ protected getWebviewContent ( ) : string {
88
42
const mdStyles : string = markdownEngine . getStyles ( ) ;
89
43
const buttonStyle : string = `
90
44
<style>
@@ -106,7 +60,7 @@ class LeetCodePreviewProvider implements Disposable {
106
60
}
107
61
</style>
108
62
` ;
109
- const { title, url, category, difficulty, likes, dislikes, body } = desc ;
63
+ const { title, url, category, difficulty, likes, dislikes, body } = this . description ;
110
64
const head : string = markdownEngine . render ( `# [${ title } ](${ url } )` ) ;
111
65
const info : string = markdownEngine . render ( [
112
66
`| Category | Difficulty | Likes | Dislikes |` ,
@@ -117,7 +71,7 @@ class LeetCodePreviewProvider implements Disposable {
117
71
`<details>` ,
118
72
`<summary><strong>Tags</strong></summary>` ,
119
73
markdownEngine . render (
120
- desc . tags
74
+ this . description . tags
121
75
. map ( ( t : string ) => `[\`${ t } \`](https://leetcode.com/tag/${ t } )` )
122
76
. join ( " | " ) ,
123
77
) ,
@@ -127,7 +81,7 @@ class LeetCodePreviewProvider implements Disposable {
127
81
`<details>` ,
128
82
`<summary><strong>Companies</strong></summary>` ,
129
83
markdownEngine . render (
130
- desc . companies
84
+ this . description . companies
131
85
. map ( ( c : string ) => `\`${ c } \`` )
132
86
. join ( " | " ) ,
133
87
) ,
@@ -159,6 +113,33 @@ class LeetCodePreviewProvider implements Disposable {
159
113
` ;
160
114
}
161
115
116
+ private parseDescription ( descString : string , problem : IProblem ) : IDescription {
117
+ const [
118
+ /* title */ , ,
119
+ url , ,
120
+ /* tags */ , ,
121
+ /* langs */ , ,
122
+ category ,
123
+ difficulty ,
124
+ likes ,
125
+ dislikes ,
126
+ /* accepted */ ,
127
+ /* submissions */ ,
128
+ /* testcase */ , ,
129
+ ...body
130
+ ] = descString . split ( "\n" ) ;
131
+ return {
132
+ title : problem . name ,
133
+ url,
134
+ tags : problem . tags ,
135
+ companies : problem . companies ,
136
+ category : category . slice ( 2 ) ,
137
+ difficulty : difficulty . slice ( 2 ) ,
138
+ likes : likes . split ( ": " ) [ 1 ] . trim ( ) ,
139
+ dislikes : dislikes . split ( ": " ) [ 1 ] . trim ( ) ,
140
+ body : body . join ( "\n" ) . replace ( / < p r e > \s * ( [ ^ ] + ?) \s * < \/ p r e > / g, "<pre><code>$1</code></pre>" ) ,
141
+ } ;
142
+ }
162
143
}
163
144
164
145
interface IDescription {
0 commit comments