1
1
import { CommandBase } from '@commands' ;
2
2
import { Config , FileSystemUtils } from '@src' ;
3
3
import { Base64 } from 'js-base64' ;
4
+ import * as fs from 'fs' ;
4
5
import * as path from 'path' ;
5
6
import * as vscode from 'vscode' ;
6
7
8
+ export enum NodeType {
9
+ none ,
10
+ rootNode ,
11
+ component ,
12
+ module ,
13
+ pipe ,
14
+ directive
15
+ }
7
16
export class Node {
8
- constructor ( id : string , tsFilename : string , isRoot : boolean ) {
17
+ private config : Config = new Config ( ) ;
18
+ constructor ( id : string , name : string , isRoot : boolean , nodeType : NodeType = NodeType . none ) {
9
19
this . id = id ;
10
- this . tsFilename = tsFilename ;
20
+ this . name = name ;
11
21
this . isRoot = isRoot ;
22
+ this . nodeType = nodeType ;
12
23
}
13
24
public id : string ;
14
- public tsFilename : string ;
25
+ public name : string ;
15
26
public isRoot : boolean ;
27
+ public nodeType : NodeType ;
16
28
17
29
public toJsonString ( ) : string {
18
- return `{id: "${ this . id } ", label: "${ this . id } "}` ;
30
+ let nodeColorAttr = '' ;
31
+ switch ( this . nodeType ) {
32
+ case NodeType . rootNode :
33
+ nodeColorAttr = `, color: "${ this . config . rootNodeBackgroundColor } ", shape: "${ this . config . visNodeShape } "` ;
34
+ break ;
35
+ case NodeType . component :
36
+ nodeColorAttr = `, color: "${ this . config . componentNodeBackgroundColor } ", shape: "${ this . config . componentNodeShape } "` ;
37
+ break ;
38
+ case NodeType . module :
39
+ nodeColorAttr = `, color: "${ this . config . moduleNodeBackgroundColor } ", shape: "${ this . config . moduleNodeShape } "` ;
40
+ break ;
41
+ case NodeType . pipe :
42
+ nodeColorAttr = `, color: "${ this . config . pipeNodeBackgroundColor } ", shape: "${ this . config . pipeNodeShape } "` ;
43
+ break ;
44
+ case NodeType . directive :
45
+ nodeColorAttr = `, color: "${ this . config . directiveNodeBackgroundColor } ", shape: "${ this . config . directiveNodeShape } "` ;
46
+ break ;
47
+ default :
48
+ nodeColorAttr = '' ;
49
+ break ;
50
+ }
51
+ const label = this . name . length > this . config . maximumNodeLabelLength ? this . name . substr ( 0 , this . config . maximumNodeLabelLength ) + '...' : this . name ;
52
+ return `{id: "${ this . id } ", label: "${ label } " ${ nodeColorAttr } }` ;
19
53
}
20
54
}
21
55
56
+ export enum ArrowType {
57
+ none = 0 ,
58
+ import = 1 ,
59
+ export = 2
60
+ }
61
+
22
62
export class Edge {
23
- constructor ( id : string , source : string , target : string ) {
63
+ private config : Config = new Config ( ) ;
64
+ constructor ( id : string , source : string , target : string , arrowType : ArrowType = ArrowType . none ) {
24
65
this . id = id ;
25
66
this . source = source ;
26
67
this . target = target ;
68
+ this . arrowType = arrowType ;
27
69
}
28
70
public id : string ;
29
71
public source : string ;
30
72
public target : string ;
73
+ public arrowType : ArrowType ;
31
74
32
75
public toJsonString ( ) : string {
33
- return `{from: "${ this . source } ", to: "${ this . target } ", arrows: arrowAttr }` ;
76
+ let arrowColorAttr = '' ;
77
+ switch ( this . arrowType ) {
78
+ case ArrowType . import :
79
+ arrowColorAttr = `, color: "${ this . config . importEdgeColor } "` ;
80
+ break ;
81
+ case ArrowType . export :
82
+ arrowColorAttr = `, color: "${ this . config . exportEdgeColor } "` ;
83
+ break ;
84
+ default :
85
+ arrowColorAttr = '' ;
86
+ break ;
87
+ }
88
+ return `{from: "${ this . source } ", to: "${ this . target } ", arrows: arrowAttr ${ arrowColorAttr } }` ;
34
89
}
35
90
}
36
91
@@ -40,6 +95,11 @@ export class ShowHierarchyBase extends CommandBase {
40
95
protected extensionContext : vscode . ExtensionContext ;
41
96
protected nodes : Node [ ] = [ ] ;
42
97
protected edges : Edge [ ] = [ ] ;
98
+ protected templateJsFilename : string = 'showHierarchy_Template.js' ;
99
+ protected templateHtmlFilename : string = 'showHierarchy_Template.html' ;
100
+ protected showComponentHierarchyJsFilename : string = 'showComponentHierarchy.js' ;
101
+ protected showModuleHierarchyJsFilename : string = 'showModuleHierarchy.js' ;
102
+ protected showHierarchyCssFilename : string = 'showHierarchy.css' ;
43
103
44
104
constructor ( context : vscode . ExtensionContext ) {
45
105
super ( ) ;
@@ -76,10 +136,32 @@ export class ShowHierarchyBase extends CommandBase {
76
136
77
137
const workspaceDirectory = this . fsUtils . getWorkspaceFolder ( ) ;
78
138
const newFilePath = path . join ( workspaceDirectory , pngFilename ) ;
79
- this . fsUtils . writeFile ( newFilePath , u8arr , ( ) => { } ) ;
139
+ this . fsUtils . writeFile ( newFilePath , u8arr , ( ) => { } ) ;
80
140
81
141
vscode . window . showInformationMessage ( `The file ${ pngFilename } has been created in the root of the workspace.` ) ;
82
142
}
83
143
}
84
144
145
+
146
+ protected generateHtmlContent ( webview : vscode . Webview , outputJsFilename : string ) : string {
147
+ let htmlContent = fs . readFileSync ( this . extensionContext ?. asAbsolutePath ( path . join ( 'templates' , this . templateHtmlFilename ) ) , 'utf8' ) ;
148
+
149
+ const visPath = vscode . Uri . joinPath ( this . extensionContext . extensionUri , 'javascript' , 'vis-network.min.js' ) ;
150
+ const visUri = webview . asWebviewUri ( visPath ) ;
151
+ htmlContent = htmlContent . replace ( 'vis-network.min.js' , visUri . toString ( ) ) ;
152
+
153
+ const cssPath = vscode . Uri . joinPath ( this . extensionContext . extensionUri , 'stylesheet' , this . showHierarchyCssFilename ) ;
154
+ const cssUri = webview . asWebviewUri ( cssPath ) ;
155
+ htmlContent = htmlContent . replace ( this . showHierarchyCssFilename , cssUri . toString ( ) ) ;
156
+
157
+ const nonce = this . getNonce ( ) ;
158
+ htmlContent = htmlContent . replace ( 'nonce-nonce' , `nonce-${ nonce } ` ) ;
159
+ htmlContent = htmlContent . replace ( / < s c r i p t / g, `<script nonce="${ nonce } " ` ) ;
160
+ htmlContent = htmlContent . replace ( 'cspSource' , webview . cspSource ) ;
161
+
162
+ const jsPath = vscode . Uri . joinPath ( this . extensionContext . extensionUri , 'out' , outputJsFilename ) ;
163
+ const jsUri = webview . asWebviewUri ( jsPath ) ;
164
+ htmlContent = htmlContent . replace ( 'showHierarchy.js' , jsUri . toString ( ) ) ;
165
+ return htmlContent ;
166
+ }
85
167
}
0 commit comments