11// Copyright (c) jdneo. All rights reserved.
22// Licensed under the MIT license.
33
4- import { commands , Disposable , ExtensionContext , ViewColumn , WebviewPanel , window } from "vscode" ;
4+ import { commands , ViewColumn } from "vscode" ;
55import { leetCodeExecutor } from "../leetCodeExecutor" ;
66import { IProblem } from "../shared" ;
7+ import { ILeetCodeWebviewOption , LeetCodeWebview } from "./LeetCodeWebview" ;
78import { markdownEngine } from "./markdownEngine" ;
89
9- class LeetCodePreviewProvider implements Disposable {
10+ class LeetCodePreviewProvider extends LeetCodeWebview {
1011
11- private context : ExtensionContext ;
1212 private node : IProblem ;
13- private panel : WebviewPanel | undefined ;
14-
15- public initialize ( context : ExtensionContext ) : void {
16- this . context = context ;
17- }
13+ private description : IDescription ;
1814
1915 public async show ( node : IProblem ) : Promise < void > {
20- // Fetch problem first before creating webview panel
2116 const descString : string = await leetCodeExecutor . getDescription ( node ) ;
22-
2317 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+ }
3225
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 > => {
3431 switch ( message . command ) {
3532 case "ShowProblem" : {
3633 await commands . executeCommand ( "leetcode.showProblem" , this . node ) ;
3734 break ;
3835 }
3936 }
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+ } ,
8438 } ;
8539 }
8640
87- private getWebViewContent ( desc : IDescription ) : string {
41+ protected getWebviewContent ( ) : string {
8842 const mdStyles : string = markdownEngine . getStyles ( ) ;
8943 const buttonStyle : string = `
9044 <style>
@@ -106,7 +60,7 @@ class LeetCodePreviewProvider implements Disposable {
10660 }
10761 </style>
10862 ` ;
109- const { title, url, category, difficulty, likes, dislikes, body } = desc ;
63+ const { title, url, category, difficulty, likes, dislikes, body } = this . description ;
11064 const head : string = markdownEngine . render ( `# [${ title } ](${ url } )` ) ;
11165 const info : string = markdownEngine . render ( [
11266 `| Category | Difficulty | Likes | Dislikes |` ,
@@ -117,7 +71,7 @@ class LeetCodePreviewProvider implements Disposable {
11771 `<details>` ,
11872 `<summary><strong>Tags</strong></summary>` ,
11973 markdownEngine . render (
120- desc . tags
74+ this . description . tags
12175 . map ( ( t : string ) => `[\`${ t } \`](https://leetcode.com/tag/${ t } )` )
12276 . join ( " | " ) ,
12377 ) ,
@@ -127,7 +81,7 @@ class LeetCodePreviewProvider implements Disposable {
12781 `<details>` ,
12882 `<summary><strong>Companies</strong></summary>` ,
12983 markdownEngine . render (
130- desc . companies
84+ this . description . companies
13185 . map ( ( c : string ) => `\`${ c } \`` )
13286 . join ( " | " ) ,
13387 ) ,
@@ -159,6 +113,33 @@ class LeetCodePreviewProvider implements Disposable {
159113 ` ;
160114 }
161115
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+ }
162143}
163144
164145interface IDescription {
0 commit comments