Skip to content

Commit b01634f

Browse files
committed
Refactoring: Moved common hierarchy method to baseclass.
1 parent fc712c3 commit b01634f

File tree

2 files changed

+95
-84
lines changed

2 files changed

+95
-84
lines changed
Lines changed: 10 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,11 @@
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';
33
import * as fs from 'fs';
4-
import { Base64 } from 'js-base64';
54
import * as path from 'path';
65
import * as vscode from 'vscode';
76

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 {
428
private static readonly Name = 'showComponentHierarchy';
43-
constructor(context: vscode.ExtensionContext) {
44-
super();
45-
this.extensionContext = context;
46-
}
479
public static get commandName(): string { return ShowComponentHierarchy.Name; }
4810

4911
public execute(webview: vscode.Webview) {
@@ -52,7 +14,7 @@ export class ShowComponentHierarchy extends CommandBase {
5214
message => {
5315
switch (message.command) {
5416
case 'saveAsPng':
55-
this.saveAsPng(message.text);
17+
this.saveAsPng(this.config.componentHierarchyFilename, message.text);
5618
return;
5719
}
5820
},
@@ -63,32 +25,18 @@ export class ShowComponentHierarchy extends CommandBase {
6325
var directoryPath: string = this.fsUtils.getWorkspaceFolder();
6426
const components = ComponentManager.findComponents(directoryPath);
6527

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);
8331

84-
const nodesJson = nodes
32+
const nodesJson = this.nodes
8533
.map((node, index, arr) => { return node.toJsonString(); })
8634
.join(',\n');
87-
const rootNodesJson = nodes
35+
const rootNodesJson = this.nodes
8836
.filter(node => node.isRoot)
8937
.map((node, index, arr) => { return '"' + node.id + '"'; })
9038
.join(',\n');
91-
const edgesJson = edges
39+
const edgesJson = this.edges
9240
.map((edge, index, arr) => { return edge.toJsonString(); })
9341
.join(',\n');
9442

@@ -143,15 +91,6 @@ export class ShowComponentHierarchy extends CommandBase {
14391
}
14492
}
14593

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-
15594
private generateJavascriptContent(nodesJson: string, rootNodesJson: string, edgesJson: string): string {
15695
const templateJsFilename = ShowComponentHierarchy.Name + '_Template.js';
15796
let template = fs.readFileSync(this.extensionContext?.asAbsolutePath(path.join('templates', templateJsFilename)), 'utf8');
@@ -190,17 +129,4 @@ export class ShowComponentHierarchy extends CommandBase {
190129
htmlContent = htmlContent.replace(ShowComponentHierarchy.Name + '.js', jsUri.toString());
191130
return htmlContent;
192131
}
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-
}
206132
}

src/commands/showHierarchyBase.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { CommandBase } from '@commands';
2+
import { Config, FileSystemUtils } from '@src';
3+
import { Base64 } from 'js-base64';
4+
import * as path from 'path';
5+
import * as vscode from 'vscode';
6+
7+
export class Node {
8+
constructor(id: string, tsFilename: string, isRoot: boolean) {
9+
this.id = id;
10+
this.tsFilename = tsFilename;
11+
this.isRoot = isRoot;
12+
}
13+
public id: string;
14+
public tsFilename: string;
15+
public isRoot: boolean;
16+
17+
public toJsonString(): string {
18+
return `{id: "${this.id}", label: "${this.id}"}`;
19+
}
20+
}
21+
22+
export class Edge {
23+
constructor(id: string, source: string, target: string) {
24+
this.id = id;
25+
this.source = source;
26+
this.target = target;
27+
}
28+
public id: string;
29+
public source: string;
30+
public target: string;
31+
32+
public toJsonString(): string {
33+
return `{from: "${this.source}", to: "${this.target}", arrows: arrowAttr }`;
34+
}
35+
}
36+
37+
export class ShowHierarchyBase extends CommandBase {
38+
protected fsUtils: FileSystemUtils = new FileSystemUtils();
39+
protected config = new Config();
40+
protected extensionContext: vscode.ExtensionContext;
41+
protected nodes: Node[] = [];
42+
protected edges: Edge[] = [];
43+
44+
constructor(context: vscode.ExtensionContext) {
45+
super();
46+
this.extensionContext = context;
47+
}
48+
protected appendNodes = (nodeList: Node[]) => {
49+
nodeList.forEach(newNode => {
50+
if (!this.nodes.some(node => node.id === newNode.id)) {
51+
this.nodes = this.nodes.concat(newNode);
52+
}
53+
});
54+
};
55+
protected appendEdges = (edgeList: Edge[]) => {
56+
edgeList.forEach(newEdge => {
57+
if (!this.edges.some(edge => edge.source === newEdge.source && edge.target === newEdge.target)) {
58+
this.edges = this.edges.concat(newEdge);
59+
}
60+
});
61+
};
62+
63+
protected getNonce() {
64+
let text = '';
65+
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
66+
for (let i = 0; i < 32; i++) {
67+
text += possible.charAt(Math.floor(Math.random() * possible.length));
68+
}
69+
return text;
70+
}
71+
72+
protected saveAsPng(pngFilename: string, messageText: string) {
73+
const dataUrl = messageText.split(',');
74+
if (dataUrl.length > 0) {
75+
const u8arr = Base64.toUint8Array(dataUrl[1]);
76+
77+
const workspaceDirectory = this.fsUtils.getWorkspaceFolder();
78+
const newFilePath = path.join(workspaceDirectory, pngFilename);
79+
this.fsUtils.writeFile(newFilePath, u8arr, () => {});
80+
81+
vscode.window.showInformationMessage(`The file ${pngFilename} has been created in the root of the workspace.`);
82+
}
83+
}
84+
85+
}

0 commit comments

Comments
 (0)