1
- import { CommandBase } from '@commands ' ;
2
- import { Component , ComponentManager , Config , FileSystemUtils } from '@src' ;
1
+ import { Node , Edge , ShowHierarchyBase } from './showHierarchyBase ' ;
2
+ import { Component , ComponentManager } from '@src' ;
3
3
import * as fs from 'fs' ;
4
- import { Base64 } from 'js-base64' ;
5
4
import * as path from 'path' ;
6
5
import * as vscode from 'vscode' ;
7
6
8
- class Node {
9
- constructor ( id : string , tsFilename : string , isRoot : boolean ) {
10
- this . id = id ;
11
- this . tsFilename = tsFilename ;
12
- this . isRoot = isRoot ;
13
- }
14
- public id : string ;
15
- public tsFilename : string ;
16
- public isRoot : boolean ;
17
-
18
- public toJsonString ( ) : string {
19
- return `{id: "${ this . id } ", label: "${ this . id } "}` ;
20
- }
21
- }
22
-
23
- class Edge {
24
- constructor ( id : string , source : string , target : string ) {
25
- this . id = id ;
26
- this . source = source ;
27
- this . target = target ;
28
- }
29
- public id : string ;
30
- public source : string ;
31
- public target : string ;
32
-
33
- public toJsonString ( ) : string {
34
- return `{from: "${ this . source } ", to: "${ this . target } ", arrows: arrowAttr }` ;
35
- }
36
- }
37
-
38
- export class ShowComponentHierarchy extends CommandBase {
39
- private config = new Config ( ) ;
40
- private extensionContext : vscode . ExtensionContext ;
41
- private fsUtils = new FileSystemUtils ( ) ;
7
+ export class ShowComponentHierarchy extends ShowHierarchyBase {
42
8
private static readonly Name = 'showComponentHierarchy' ;
43
- constructor ( context : vscode . ExtensionContext ) {
44
- super ( ) ;
45
- this . extensionContext = context ;
46
- }
47
9
public static get commandName ( ) : string { return ShowComponentHierarchy . Name ; }
48
10
49
11
public execute ( webview : vscode . Webview ) {
@@ -52,7 +14,7 @@ export class ShowComponentHierarchy extends CommandBase {
52
14
message => {
53
15
switch ( message . command ) {
54
16
case 'saveAsPng' :
55
- this . saveAsPng ( message . text ) ;
17
+ this . saveAsPng ( this . config . componentHierarchyFilename , message . text ) ;
56
18
return ;
57
19
}
58
20
} ,
@@ -63,32 +25,18 @@ export class ShowComponentHierarchy extends CommandBase {
63
25
var directoryPath : string = this . fsUtils . getWorkspaceFolder ( ) ;
64
26
const components = ComponentManager . findComponents ( directoryPath ) ;
65
27
66
- let nodes : Node [ ] = [ ] ;
67
- const appendNodes = ( nodeList : Node [ ] ) => {
68
- nodeList . forEach ( newNode => {
69
- if ( ! nodes . some ( node => node . id === newNode . id ) ) {
70
- nodes = nodes . concat ( newNode ) ;
71
- }
72
- } ) ;
73
- } ;
74
- let edges : Edge [ ] = [ ] ;
75
- const appendEdges = ( edgeList : Edge [ ] ) => {
76
- edgeList . forEach ( newEdge => {
77
- if ( ! edges . some ( edge => edge . source === newEdge . source && edge . target === newEdge . target ) ) {
78
- edges = edges . concat ( newEdge ) ;
79
- }
80
- } ) ;
81
- } ;
82
- this . addNodesAndEdges ( components , appendNodes , appendEdges ) ;
28
+ this . nodes = [ ] ;
29
+ this . edges = [ ] ;
30
+ this . addNodesAndEdges ( components , this . appendNodes , this . appendEdges ) ;
83
31
84
- const nodesJson = nodes
32
+ const nodesJson = this . nodes
85
33
. map ( ( node , index , arr ) => { return node . toJsonString ( ) ; } )
86
34
. join ( ',\n' ) ;
87
- const rootNodesJson = nodes
35
+ const rootNodesJson = this . nodes
88
36
. filter ( node => node . isRoot )
89
37
. map ( ( node , index , arr ) => { return '"' + node . id + '"' ; } )
90
38
. join ( ',\n' ) ;
91
- const edgesJson = edges
39
+ const edgesJson = this . edges
92
40
. map ( ( edge , index , arr ) => { return edge . toJsonString ( ) ; } )
93
41
. join ( ',\n' ) ;
94
42
@@ -143,15 +91,6 @@ export class ShowComponentHierarchy extends CommandBase {
143
91
}
144
92
}
145
93
146
- private getNonce ( ) {
147
- let text = '' ;
148
- const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' ;
149
- for ( let i = 0 ; i < 32 ; i ++ ) {
150
- text += possible . charAt ( Math . floor ( Math . random ( ) * possible . length ) ) ;
151
- }
152
- return text ;
153
- }
154
-
155
94
private generateJavascriptContent ( nodesJson : string , rootNodesJson : string , edgesJson : string ) : string {
156
95
const templateJsFilename = ShowComponentHierarchy . Name + '_Template.js' ;
157
96
let template = fs . readFileSync ( this . extensionContext ?. asAbsolutePath ( path . join ( 'templates' , templateJsFilename ) ) , 'utf8' ) ;
@@ -190,17 +129,4 @@ export class ShowComponentHierarchy extends CommandBase {
190
129
htmlContent = htmlContent . replace ( ShowComponentHierarchy . Name + '.js' , jsUri . toString ( ) ) ;
191
130
return htmlContent ;
192
131
}
193
-
194
- private saveAsPng ( messageText : string ) {
195
- const dataUrl = messageText . split ( ',' ) ;
196
- if ( dataUrl . length > 0 ) {
197
- const u8arr = Base64 . toUint8Array ( dataUrl [ 1 ] ) ;
198
-
199
- const workspaceDirectory = this . fsUtils . getWorkspaceFolder ( ) ;
200
- const newFilePath = path . join ( workspaceDirectory , this . config . componentHierarchyFilename ) ;
201
- this . fsUtils . writeFile ( newFilePath , u8arr , ( ) => { } ) ;
202
-
203
- vscode . window . showInformationMessage ( `The file ${ this . config . componentHierarchyFilename } has been created in the root of the workspace.` ) ;
204
- }
205
- }
206
132
}
0 commit comments