1
1
// Copyright (c) jdneo. All rights reserved.
2
2
// Licensed under the MIT license.
3
3
4
- import { Disposable , ExtensionContext , ViewColumn , WebviewPanel , window } from "vscode" ;
4
+ import { ViewColumn } from "vscode" ;
5
5
import { IProblem } from "../shared" ;
6
+ import { ILeetCodeWebviewOption , LeetCodeWebview } from "./LeetCodeWebview" ;
6
7
import { markdownEngine } from "./markdownEngine" ;
7
8
8
- class LeetCodeSolutionProvider implements Disposable {
9
+ class LeetCodeSolutionProvider extends LeetCodeWebview {
9
10
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 ;
16
12
17
13
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 ) ;
28
18
}
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 ) ;
34
19
}
35
20
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
+ } ;
40
26
}
41
27
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 {
56
29
const styles : string = markdownEngine . getStyles ( ) ;
57
- const { title, url, lang, author, votes } = solution ;
30
+ const { title, url, lang, author, votes } = this . solution ;
58
31
const head : string = markdownEngine . render ( `# [${ title } ](${ url } )` ) ;
59
32
const auth : string = `[${ author } ](https://leetcode.com/${ author } /)` ;
60
33
const info : string = markdownEngine . render ( [
61
34
`| Language | Author | Votes |` ,
62
35
`| :------: | :------: | :------: |` ,
63
36
`| ${ lang } | ${ auth } | ${ votes } |` ,
64
37
] . 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 ,
67
40
host : "https://discuss.leetcode.com/" ,
68
41
} ) ;
69
42
return `
@@ -80,6 +53,19 @@ class LeetCodeSolutionProvider implements Disposable {
80
53
</html>
81
54
` ;
82
55
}
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
+ }
83
69
}
84
70
85
71
// tslint:disable-next-line:max-classes-per-file
0 commit comments