Skip to content

Commit 736806b

Browse files
committed
Network seed is now persisted and reused when activating a command multiple times.
1 parent e550247 commit 736806b

File tree

7 files changed

+84
-11
lines changed

7 files changed

+84
-11
lines changed

src/commands/generateDependencyInjectionGraph.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ShowHierarchyBase } from './showHierarchyBase';
22
import { ModuleManager } from '@src';
3-
import { ArrowType, Component, Edge, Node, NodeType, Project } from '@model';
3+
import { ArrowType, Component, Edge, GraphState, Node, NodeType, Project } from '@model';
44
import * as fs from 'fs';
55
import * as path from 'path';
66
import * as vscode from 'vscode';
@@ -21,6 +21,12 @@ export class GenerateDependencyInjectionGraph extends ShowHierarchyBase {
2121
case 'saveAsDot':
2222
this.saveAsDot(this.config.dependencyInjectionDotGraphFilename, message.text, 'dependencyInjectionGraph', `'The components hierarchy has been analyzed and a GraphViz (dot) file '${this.config.dependencyInjectionDotGraphFilename}' has been created'`);
2323
return;
24+
case 'setGraphState':
25+
const newGraphState: GraphState = JSON.parse(message.text);
26+
this.graphState.networkSeed = newGraphState.networkSeed;
27+
this.graphState.nodePositions = newGraphState.nodePositions;
28+
this.setNewState(this.graphState);
29+
return;
2430
}
2531
},
2632
undefined,
@@ -112,6 +118,7 @@ export class GenerateDependencyInjectionGraph extends ShowHierarchyBase {
112118
jsContent = jsContent.replace('ctx.lineWidth = 1; // graph selection guideline width', `ctx.lineWidth = ${this.config.graphSelectionGuidelineWidth}; // graph selection guideline width`);
113119
jsContent = jsContent.replace('selectionCanvasContext.strokeStyle = \'red\';', `selectionCanvasContext.strokeStyle = '${this.config.graphSelectionColor}';`);
114120
jsContent = jsContent.replace('selectionCanvasContext.lineWidth = 2;', `selectionCanvasContext.lineWidth = ${this.config.graphSelectionWidth};`);
121+
jsContent = this.setGraphState(jsContent);
115122
return jsContent;
116123
}
117124
}

src/commands/showComponentHierarchy.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ShowHierarchyBase } from './showHierarchyBase';
22
import { Component, ComponentManager } from '@src';
3-
import { ArrowType, Edge, Node, NodeType } from '@model';
3+
import { ArrowType, Edge, GraphState, Node, NodeType } from '@model';
44
import * as fs from 'fs';
55
import * as path from 'path';
66
import * as vscode from 'vscode';
@@ -24,6 +24,12 @@ export class ShowComponentHierarchy extends ShowHierarchyBase {
2424
case 'saveAsDot':
2525
this.saveAsDot(this.config.componentHierarchyDotGraphFilename, message.text, 'componentHierarchy', `'The component hierarchy has been analyzed and a GraphViz (dot) file '${this.config.componentHierarchyDotGraphFilename}' has been created'`);
2626
return;
27+
case 'setGraphState':
28+
const newGraphState: GraphState = JSON.parse(message.text);
29+
this.graphState.networkSeed = newGraphState.networkSeed;
30+
this.graphState.nodePositions = newGraphState.nodePositions;
31+
this.setNewState(this.graphState);
32+
return;
2733
}
2834
},
2935
undefined,
@@ -112,6 +118,7 @@ export class ShowComponentHierarchy extends ShowHierarchyBase {
112118
jsContent = jsContent.replace('ctx.lineWidth = 1; // graph selection guideline width', `ctx.lineWidth = ${this.config.graphSelectionGuidelineWidth}; // graph selection guideline width`);
113119
jsContent = jsContent.replace('selectionCanvasContext.strokeStyle = \'red\';', `selectionCanvasContext.strokeStyle = '${this.config.graphSelectionColor}';`);
114120
jsContent = jsContent.replace('selectionCanvasContext.lineWidth = 2;', `selectionCanvasContext.lineWidth = ${this.config.graphSelectionWidth};`);
121+
jsContent = this.setGraphState(jsContent);
115122
return jsContent;
116123
}
117124
}

src/commands/showHierarchyBase.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export class ShowHierarchyBase extends CommandBase {
1515
protected config = new Config();
1616
protected extensionContext: vscode.ExtensionContext;
1717
protected graphState: GraphState;
18+
protected setNewState: (newGraphState: GraphState) => any;
1819
protected nodes: Node[] = [];
1920
protected edges: Edge[] = [];
2021
protected templateJsFilename: string = 'showHierarchy_Template.js';
@@ -24,9 +25,10 @@ export class ShowHierarchyBase extends CommandBase {
2425
protected showHierarchyCssFilename: string = 'showHierarchy.css';
2526
protected workspaceDirectory = this.fsUtils.getWorkspaceFolder();
2627

27-
constructor(context: vscode.ExtensionContext, graphState: GraphState) {
28+
constructor(context: vscode.ExtensionContext, graphState: GraphState, setNewState: (newGraphState: GraphState) => any) {
2829
super();
2930
this.extensionContext = context;
31+
this.setNewState = setNewState;
3032
this.graphState = graphState;
3133
}
3234
protected appendNodes = (nodeList: Node[]) => {
@@ -125,6 +127,13 @@ export class ShowHierarchyBase extends CommandBase {
125127
});
126128
}
127129

130+
protected setGraphState(jsContent: string): string {
131+
if (this.graphState.networkSeed !== undefined) {
132+
jsContent = jsContent.replace('var seed = network.getSeed();', `var seed = ${this.graphState.networkSeed};`);
133+
}
134+
return jsContent;
135+
}
136+
128137
protected generateHtmlContent(webview: vscode.Webview, outputJsFilename: string): string {
129138
let htmlContent = fs.readFileSync(this.extensionContext?.asAbsolutePath(path.join('templates', this.templateHtmlFilename)), 'utf8');
130139

src/commands/showModuleHierarchy.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ShowHierarchyBase } from './showHierarchyBase';
22
import { ModuleManager } from '@src';
3-
import { ArrowType, Edge, Node, NodeType, Project } from '@model';
3+
import { ArrowType, Edge, GraphState, Node, NodeType, Project } from '@model';
44
import * as fs from 'fs';
55
import * as path from 'path';
66
import * as vscode from 'vscode';
@@ -21,6 +21,12 @@ export class ShowModuleHierarchy extends ShowHierarchyBase {
2121
case 'saveAsDot':
2222
this.saveAsDot(this.config.moduleHierarchyDotGraphFilename, message.text, 'moduleHierarchy', `'The modules hierarchy has been analyzed and a GraphViz (dot) file '${this.config.moduleHierarchyDotGraphFilename}' has been created'`);
2323
return;
24+
case 'setGraphState':
25+
const newGraphState: GraphState = JSON.parse(message.text);
26+
this.graphState.networkSeed = newGraphState.networkSeed;
27+
this.graphState.nodePositions = newGraphState.nodePositions;
28+
this.setNewState(this.graphState);
29+
return;
2430
}
2531
},
2632
undefined,
@@ -104,6 +110,7 @@ export class ShowModuleHierarchy extends ShowHierarchyBase {
104110
jsContent = jsContent.replace('ctx.lineWidth = 1; // graph selection guideline width', `ctx.lineWidth = ${this.config.graphSelectionGuidelineWidth}; // graph selection guideline width`);
105111
jsContent = jsContent.replace('selectionCanvasContext.strokeStyle = \'red\';', `selectionCanvasContext.strokeStyle = '${this.config.graphSelectionColor}';`);
106112
jsContent = jsContent.replace('selectionCanvasContext.lineWidth = 2;', `selectionCanvasContext.lineWidth = ${this.config.graphSelectionWidth};`);
113+
jsContent = this.setGraphState(jsContent);
107114
return jsContent;
108115
}
109116
}

src/extension.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export function activate(context: vscode.ExtensionContext) {
4949
context.subscriptions.push(componentHierarchyMarkdownDisposable);
5050

5151
let componentHierarchyPanel: vscode.WebviewPanel | undefined = undefined;
52-
const componentHierarchyGraphState: GraphState = new GraphState();
52+
let componentHierarchyGraphState: GraphState = new GraphState();
5353
const showComponentHierarchyDisposable = vscode.commands.registerCommand(`${cmdPrefix}.${ShowComponentHierarchy.commandName}`, () => {
5454
if (componentHierarchyPanel !== undefined) {
5555
componentHierarchyPanel.reveal(vscode.ViewColumn.One);
@@ -66,13 +66,14 @@ export function activate(context: vscode.ExtensionContext) {
6666
}, null, context.subscriptions);
6767
}
6868
componentHierarchyPanel.onDidDispose(() => componentHierarchyPanel = undefined, undefined, context.subscriptions);
69-
const command = new ShowComponentHierarchy(context, componentHierarchyGraphState);
69+
const setNewState = (newGraphState: GraphState) => { componentHierarchyGraphState = newGraphState; };
70+
const command = new ShowComponentHierarchy(context, componentHierarchyGraphState, setNewState);
7071
command.execute(componentHierarchyPanel.webview);
7172
});
7273
context.subscriptions.push(showComponentHierarchyDisposable);
7374

7475
let moduleHierarchyPanel: vscode.WebviewPanel | undefined = undefined;
75-
const moduleHierarchyGraphState: GraphState = new GraphState();
76+
let moduleHierarchyGraphState: GraphState = new GraphState();
7677
const showModuleHierarchyDisposable = vscode.commands.registerCommand(`${cmdPrefix}.${ShowModuleHierarchy.commandName}`, () => {
7778
if (moduleHierarchyPanel !== undefined) {
7879
moduleHierarchyPanel.reveal(vscode.ViewColumn.One);
@@ -89,13 +90,14 @@ export function activate(context: vscode.ExtensionContext) {
8990
}, null, context.subscriptions);
9091
}
9192
moduleHierarchyPanel.onDidDispose(() => moduleHierarchyPanel = undefined, undefined, context.subscriptions);
92-
const command = new ShowModuleHierarchy(context, moduleHierarchyGraphState);
93+
const setNewState = (newGraphState: GraphState) => { moduleHierarchyGraphState = newGraphState; };
94+
const command = new ShowModuleHierarchy(context, moduleHierarchyGraphState, setNewState);
9395
command.execute(moduleHierarchyPanel.webview);
9496
});
9597
context.subscriptions.push(showModuleHierarchyDisposable);
9698

9799
let dependencyInjectionGraphPanel: vscode.WebviewPanel | undefined = undefined;
98-
const dependencyInjectionGraphState: GraphState = new GraphState();
100+
let dependencyInjectionGraphState: GraphState = new GraphState();
99101
const generateDependencyInjectionGraphDisposable = vscode.commands.registerCommand(`${cmdPrefix}.${GenerateDependencyInjectionGraph.commandName}`, () => {
100102
if (dependencyInjectionGraphPanel !== undefined) {
101103
dependencyInjectionGraphPanel.reveal(vscode.ViewColumn.One);
@@ -112,7 +114,8 @@ export function activate(context: vscode.ExtensionContext) {
112114
}, null, context.subscriptions);
113115
}
114116
dependencyInjectionGraphPanel.onDidDispose(() => dependencyInjectionGraphPanel = undefined, undefined, context.subscriptions);
115-
const command = new GenerateDependencyInjectionGraph(context, dependencyInjectionGraphState);
117+
const setNewState = (newGraphState: GraphState) => { dependencyInjectionGraphState = newGraphState; };
118+
const command = new GenerateDependencyInjectionGraph(context, dependencyInjectionGraphState, setNewState);
116119
command.execute(dependencyInjectionGraphPanel.webview);
117120
});
118121
context.subscriptions.push(generateDependencyInjectionGraphDisposable);

src/model/GraphState.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { Position } from "./Position";
22

33
export class GraphState {
4+
constructor() {
5+
this.nodePositions = {};
6+
}
47
public graphLayout: string | undefined;
8+
public graphDirection: string | undefined;
59
public networkSeed: number | undefined;
6-
public nodePositions: Position | undefined;
10+
public nodePositions: { [id: string]: Position };
711
}

templates/showHierarchy_Template.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
network.on("stabilizationIterationsDone", function () {
3737
network.setOptions( { physics: false } );
3838
});
39+
network.on('dragEnd', postGraphState);
3940

4041
const vscode = acquireVsCodeApi();
4142
const helpTextDiv = document.getElementById('helpText');
@@ -362,6 +363,41 @@
362363
network.on("stabilizationIterationsDone", function () {
363364
network.setOptions( { physics: false } );
364365
});
366+
network.on('dragEnd', postGraphState);
367+
vscode.postMessage({
368+
command: 'setGraphState',
369+
text: JSON.stringify({
370+
networkSeed: seed,
371+
graphDirection: hierarchicalOptionsDirectionSelect.value,
372+
graphLayout: hierarchicalOptionsSortMethodSelect.value,
373+
nodePositions: getNodePositions()
374+
})
375+
});
365376
}
366377

378+
function postGraphState() {
379+
const message = JSON.stringify({
380+
networkSeed: seed,
381+
// graphDirection: hierarchicalOptionsDirectionSelect.value,
382+
// graphLayout: hierarchicalOptionsSortMethodSelect.value,
383+
nodePositions: getNodePositions()
384+
});
385+
console.log('postGraphState', message);
386+
vscode.postMessage({
387+
command: 'setGraphState',
388+
text: message
389+
});
390+
}
391+
392+
function getNodePositions() {
393+
const nodePositions = {};
394+
nodes.forEach(node => {
395+
nodePositions[node.id] = {
396+
id: node.id,
397+
position: network.getPosition(node.id)
398+
};
399+
});
400+
return nodePositions;
401+
}
402+
367403
}());

0 commit comments

Comments
 (0)