1
- import * as vscode from 'vscode' ;
2
1
import * as fs from 'fs' ;
3
- import * as path from 'path' ;
4
- import { FileSystemUtils } from "../filesystemUtils" ;
5
2
import { Base64 } from 'js-base64' ;
3
+ import * as path from 'path' ;
4
+ import * as vscode from 'vscode' ;
5
+
6
+ import { Config } from '../config' ;
7
+ import { FileSystemUtils } from '../filesystemUtils' ;
6
8
7
9
class Component {
8
10
@@ -53,10 +55,11 @@ export class ShowComponentHierarchy {
53
55
54
56
private extensionContext : vscode . ExtensionContext ;
55
57
private fsUtils = new FileSystemUtils ( ) ;
58
+ private static readonly Name = 'showComponentHierarchy' ;
56
59
constructor ( context : vscode . ExtensionContext ) {
57
60
this . extensionContext = context ;
58
61
}
59
- public static get commandName ( ) : string { return 'showComponentHierarchy' ; }
62
+ public static get commandName ( ) : string { return ShowComponentHierarchy . Name ; }
60
63
61
64
public execute ( webview : vscode . Webview ) {
62
65
@@ -73,8 +76,7 @@ export class ShowComponentHierarchy {
73
76
) ;
74
77
75
78
var directoryPath : string = this . fsUtils . getWorkspaceFolder ( ) ;
76
- const excludeDirectories = [ 'bin' , 'obj' , 'node_modules' , 'dist' , 'packages' , '.git' , '.vs' , '.github' ] ;
77
- const componentFilenames = this . fsUtils . listFiles ( directoryPath , excludeDirectories , this . isComponentFile ) ;
79
+ const componentFilenames = this . fsUtils . listFiles ( directoryPath , Config . excludeDirectories , this . isComponentFile ) ;
78
80
const components = this . findComponents ( componentFilenames ) ;
79
81
this . enrichComponentsFromComponentTemplates ( components ) ;
80
82
@@ -109,10 +111,10 @@ export class ShowComponentHierarchy {
109
111
110
112
try {
111
113
const jsContent = this . generateJavascriptContent ( nodesJson , rootNodesJson , edgesJson ) ;
112
- const outputJsFilename = 'showComponentHierarchy .js';
114
+ const outputJsFilename = ShowComponentHierarchy . Name + ' .js';
113
115
let htmlContent = this . generateHtmlContent ( webview , outputJsFilename ) ;
114
116
115
- this . fsUtils . writeFile ( this . extensionContext ?. asAbsolutePath ( path . join ( 'out' , 'showComponentHierarchy .html') ) , htmlContent , ( ) => { } ) ; // For debugging
117
+ this . fsUtils . writeFile ( this . extensionContext ?. asAbsolutePath ( path . join ( 'out' , ShowComponentHierarchy . Name + ' .html') ) , htmlContent , ( ) => { } ) ; // For debugging
116
118
this . fsUtils . writeFile (
117
119
this . extensionContext ?. asAbsolutePath ( path . join ( 'out' , outputJsFilename ) ) ,
118
120
jsContent ,
@@ -199,29 +201,29 @@ export class ShowComponentHierarchy {
199
201
for ( let selector in componentHash ) {
200
202
const component = componentHash [ selector ] ;
201
203
if ( component . isRoot ) {
202
- this . generateDirectedGraphNodesXml ( component . subComponents , component , true , appendNodes ) ;
203
- this . generateDirectedGraphLinksXml ( component . subComponents , selector , "" , appendLinks ) ;
204
+ this . generateDirectedGraphNodes ( component . subComponents , component , true , appendNodes ) ;
205
+ this . generateDirectedGraphEdges ( component . subComponents , selector , "" , appendLinks ) ;
204
206
}
205
207
}
206
208
}
207
209
208
- private generateDirectedGraphNodesXml ( components : Component [ ] , component : Component , isRoot : boolean , appendNodes : ( nodeList : Node [ ] ) => void ) {
210
+ private generateDirectedGraphNodes ( components : Component [ ] , component : Component , isRoot : boolean , appendNodes : ( nodeList : Node [ ] ) => void ) {
209
211
appendNodes ( [ new Node ( component . selector , component . templateFilename , isRoot ) ] ) ;
210
212
if ( components . length > 0 ) {
211
213
components . forEach ( ( subComponent ) => {
212
- this . generateDirectedGraphNodesXml ( subComponent . subComponents , subComponent , subComponent . isRoot , appendNodes ) ;
214
+ this . generateDirectedGraphNodes ( subComponent . subComponents , subComponent , subComponent . isRoot , appendNodes ) ;
213
215
} ) ;
214
216
}
215
217
}
216
218
217
- private generateDirectedGraphLinksXml ( subComponents : Component [ ] , selector : string , parentSelector : string , appendLinks : ( edgeList : Edge [ ] ) => void ) {
219
+ private generateDirectedGraphEdges ( subComponents : Component [ ] , selector : string , parentSelector : string , appendLinks : ( edgeList : Edge [ ] ) => void ) {
218
220
if ( parentSelector . length > 0 ) {
219
221
const id = Math . random ( ) * 100000 ;
220
222
appendLinks ( [ new Edge ( id . toString ( ) , parentSelector , selector ) ] ) ;
221
223
}
222
224
if ( subComponents . length > 0 ) {
223
225
subComponents . forEach ( ( subComponent ) => {
224
- this . generateDirectedGraphLinksXml ( subComponent . subComponents , subComponent . selector , selector , appendLinks ) ;
226
+ this . generateDirectedGraphEdges ( subComponent . subComponents , subComponent . selector , selector , appendLinks ) ;
225
227
} ) ;
226
228
}
227
229
}
@@ -236,25 +238,31 @@ export class ShowComponentHierarchy {
236
238
}
237
239
238
240
private generateJavascriptContent ( nodesJson : string , rootNodesJson : string , edgesJson : string ) : string {
239
- const templateJsFilename = 'showComponentHierarchy_Template .js';
241
+ const templateJsFilename = ShowComponentHierarchy . Name + '_Template .js';
240
242
let template = fs . readFileSync ( this . extensionContext ?. asAbsolutePath ( path . join ( 'templates' , templateJsFilename ) ) , 'utf8' ) ;
241
243
let jsContent = template . replace ( 'var nodes = new vis.DataSet([]);' , `var nodes = new vis.DataSet([${ nodesJson } ]);` ) ;
242
244
jsContent = jsContent . replace ( 'var rootNodes = [];' , `var rootNodes = [${ rootNodesJson } ];` ) ;
243
245
jsContent = jsContent . replace ( 'var edges = new vis.DataSet([]);' , `var edges = new vis.DataSet([${ edgesJson } ]);` ) ;
246
+ jsContent = jsContent . replace ( 'background: "#00FF00" // rootNode background color' , `background: "${ Config . visRootNodeBackgroundColor } " // rootNode background color` ) ;
247
+ jsContent = jsContent . replace ( 'type: "triangle" // edge arrow to type' , `type: "${ Config . visEdgeArrowToType } " // edge arrow to type` ) ;
248
+ jsContent = jsContent . replace ( 'ctx.strokeStyle = \'blue\'; // graph selection guideline color' , `ctx.strokeStyle = '${ Config . graphSelectionGuidelineColor } '; // graph selection guideline color` ) ;
249
+ jsContent = jsContent . replace ( 'ctx.lineWidth = 1; // graph selection guideline width' , `ctx.lineWidth = ${ Config . graphSelectionGuidelineWidth } ; // graph selection guideline width` ) ;
250
+ jsContent = jsContent . replace ( 'selectionCanvasContext.strokeStyle = \'red\';' , `selectionCanvasContext.strokeStyle = '${ Config . graphSelectionColor } ';` ) ;
251
+ jsContent = jsContent . replace ( 'selectionCanvasContext.lineWidth = 2;' , `selectionCanvasContext.lineWidth = ${ Config . graphSelectionWidth } ;` ) ;
244
252
return jsContent ;
245
253
}
246
254
247
255
private generateHtmlContent ( webview : vscode . Webview , outputJsFilename : string ) : string {
248
- const templateHtmlFilename = 'showComponentHierarchy_Template .html';
256
+ const templateHtmlFilename = ShowComponentHierarchy . Name + '_Template .html';
249
257
let htmlContent = fs . readFileSync ( this . extensionContext ?. asAbsolutePath ( path . join ( 'templates' , templateHtmlFilename ) ) , 'utf8' ) ;
250
258
251
259
const visPath = vscode . Uri . joinPath ( this . extensionContext . extensionUri , 'javascript' , 'vis-network.min.js' ) ;
252
260
const visUri = webview . asWebviewUri ( visPath ) ;
253
261
htmlContent = htmlContent . replace ( 'vis-network.min.js' , visUri . toString ( ) ) ;
254
262
255
- const cssPath = vscode . Uri . joinPath ( this . extensionContext . extensionUri , 'stylesheet' , 'showComponentHierarchy .css') ;
263
+ const cssPath = vscode . Uri . joinPath ( this . extensionContext . extensionUri , 'stylesheet' , ShowComponentHierarchy . Name + ' .css') ;
256
264
const cssUri = webview . asWebviewUri ( cssPath ) ;
257
- htmlContent = htmlContent . replace ( 'showComponentHierarchy .css', cssUri . toString ( ) ) ;
265
+ htmlContent = htmlContent . replace ( ShowComponentHierarchy . Name + ' .css', cssUri . toString ( ) ) ;
258
266
259
267
const nonce = this . getNonce ( ) ;
260
268
htmlContent = htmlContent . replace ( 'nonce-nonce' , `nonce-${ nonce } ` ) ;
@@ -263,7 +271,7 @@ export class ShowComponentHierarchy {
263
271
264
272
const jsPath = vscode . Uri . joinPath ( this . extensionContext . extensionUri , 'out' , outputJsFilename ) ;
265
273
const jsUri = webview . asWebviewUri ( jsPath ) ;
266
- htmlContent = htmlContent . replace ( 'showComponentHierarchy .js', jsUri . toString ( ) ) ;
274
+ htmlContent = htmlContent . replace ( ShowComponentHierarchy . Name + ' .js', jsUri . toString ( ) ) ;
267
275
return htmlContent ;
268
276
}
269
277
@@ -273,7 +281,7 @@ export class ShowComponentHierarchy {
273
281
const u8arr = Base64 . toUint8Array ( dataUrl [ 1 ] ) ;
274
282
275
283
const workspaceDirectory = this . fsUtils . getWorkspaceFolder ( ) ;
276
- const newFilePath = path . join ( workspaceDirectory , 'ComponentHierarchy.png' ) ;
284
+ const newFilePath = path . join ( workspaceDirectory , Config . componentHierarchyFilename ) ;
277
285
this . fsUtils . writeFile ( newFilePath , u8arr , ( ) => { } ) ;
278
286
279
287
vscode . window . showInformationMessage ( 'The file ComponentHierarchy.png has been created in the root of the workspace.' ) ;
0 commit comments