11// Copyright (c) jdneo. All rights reserved.
22// Licensed under the MIT license.
33
4- import { Disposable , ExtensionContext , ViewColumn , WebviewPanel , window } from "vscode" ;
4+ import { ViewColumn } from "vscode" ;
55import { IProblem } from "../shared" ;
6+ import { ILeetCodeWebviewOption , LeetCodeWebview } from "./LeetCodeWebview" ;
67import { markdownEngine } from "./markdownEngine" ;
78
8- class LeetCodeSolutionProvider implements Disposable {
9+ class LeetCodeSolutionProvider extends LeetCodeWebview {
910
10- private context : ExtensionContext ;
11- private panel : WebviewPanel | undefined ;
12-
13- public initialize ( context : ExtensionContext ) : void {
14- this . context = context ;
15- }
11+ private solution : Solution ;
1612
1713 public async show ( solutionString : string , problem : IProblem ) : Promise < void > {
18- if ( ! this . panel ) {
19- this . panel = window . createWebviewPanel ( "leetCode.solution" , "Top Voted Solution" , ViewColumn . Active , {
20- retainContextWhenHidden : true ,
21- enableFindWidget : true ,
22- localResourceRoots : markdownEngine . localResourceRoots ,
23- } ) ;
24-
25- this . panel . onDidDispose ( ( ) => {
26- this . panel = undefined ;
27- } , null , this . context . subscriptions ) ;
14+ this . solution = this . parseSolution ( solutionString ) ;
15+ if ( this . showWebviewInternal ( ) ) {
16+ this . panel . title = `${ problem . name } : Solution` ;
17+ this . panel . reveal ( ViewColumn . Active ) ;
2818 }
29-
30- const solution : Solution = this . parseSolution ( solutionString ) ;
31- this . panel . title = `${ problem . name } : Solution` ;
32- this . panel . webview . html = this . getWebViewContent ( solution ) ;
33- this . panel . reveal ( ViewColumn . Active ) ;
3419 }
3520
36- public dispose ( ) : void {
37- if ( this . panel ) {
38- this . panel . dispose ( ) ;
39- }
21+ protected getWebviewOption ( ) : ILeetCodeWebviewOption {
22+ return {
23+ viewType : "leetcode.solution" ,
24+ title : "Top Voted Solution" ,
25+ } ;
4026 }
4127
42- private parseSolution ( raw : string ) : Solution {
43- const solution : Solution = new Solution ( ) ;
44- // [^] matches everything including \n, yet can be replaced by . in ES2018's `m` flag
45- raw = raw . slice ( 1 ) ; // skip first empty line
46- [ solution . title , raw ] = raw . split ( / \n \n ( [ ^ ] + ) / ) ; // parse title and skip one line
47- [ solution . url , raw ] = raw . split ( / \n \n ( [ ^ ] + ) / ) ; // parse url and skip one line
48- [ solution . lang , raw ] = raw . match ( / \* L a n g : \s + ( .+ ) \n ( [ ^ ] + ) / ) ! . slice ( 1 ) ;
49- [ solution . author , raw ] = raw . match ( / \* A u t h o r : \s + ( .+ ) \n ( [ ^ ] + ) / ) ! . slice ( 1 ) ;
50- [ solution . votes , raw ] = raw . match ( / \* V o t e s : \s + ( \d + ) \n \n ( [ ^ ] + ) / ) ! . slice ( 1 ) ;
51- solution . body = raw ;
52- return solution ;
53- }
54-
55- private getWebViewContent ( solution : Solution ) : string {
28+ protected getWebviewContent ( ) : string {
5629 const styles : string = markdownEngine . getStyles ( ) ;
57- const { title, url, lang, author, votes } = solution ;
30+ const { title, url, lang, author, votes } = this . solution ;
5831 const head : string = markdownEngine . render ( `# [${ title } ](${ url } )` ) ;
5932 const auth : string = `[${ author } ](https://leetcode.com/${ author } /)` ;
6033 const info : string = markdownEngine . render ( [
6134 `| Language | Author | Votes |` ,
6235 `| :------: | :------: | :------: |` ,
6336 `| ${ lang } | ${ auth } | ${ votes } |` ,
6437 ] . join ( "\n" ) ) ;
65- const body : string = markdownEngine . render ( solution . body , {
66- lang : solution . lang ,
38+ const body : string = markdownEngine . render ( this . solution . body , {
39+ lang : this . solution . lang ,
6740 host : "https://discuss.leetcode.com/" ,
6841 } ) ;
6942 return `
@@ -80,6 +53,19 @@ class LeetCodeSolutionProvider implements Disposable {
8053 </html>
8154 ` ;
8255 }
56+
57+ private parseSolution ( raw : string ) : Solution {
58+ const solution : Solution = new Solution ( ) ;
59+ // [^] matches everything including \n, yet can be replaced by . in ES2018's `m` flag
60+ raw = raw . slice ( 1 ) ; // skip first empty line
61+ [ solution . title , raw ] = raw . split ( / \n \n ( [ ^ ] + ) / ) ; // parse title and skip one line
62+ [ solution . url , raw ] = raw . split ( / \n \n ( [ ^ ] + ) / ) ; // parse url and skip one line
63+ [ solution . lang , raw ] = raw . match ( / \* L a n g : \s + ( .+ ) \n ( [ ^ ] + ) / ) ! . slice ( 1 ) ;
64+ [ solution . author , raw ] = raw . match ( / \* A u t h o r : \s + ( .+ ) \n ( [ ^ ] + ) / ) ! . slice ( 1 ) ;
65+ [ solution . votes , raw ] = raw . match ( / \* V o t e s : \s + ( \d + ) \n \n ( [ ^ ] + ) / ) ! . slice ( 1 ) ;
66+ solution . body = raw ;
67+ return solution ;
68+ }
8369}
8470
8571// tslint:disable-next-line:max-classes-per-file
0 commit comments