Skip to content

Commit 91baeb0

Browse files
committed
Paths are now parsed from the dgml file and substituted into the reference properties.
1 parent 584a447 commit 91baeb0

File tree

8 files changed

+99
-36
lines changed

8 files changed

+99
-36
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Change Log
22

3+
## Version 1.3.1
4+
5+
- Paths are now parsed from the dgml file and substituted into the reference properties.
6+
- The FilePath attribute on a node is now parsed and used as a reference property.
7+
38
## Version 1.3.0
49

510
- If nodes contain attributes that are defined as a reference property the node is now clickable and will open the referenced file in vscode.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"displayName": "DGMLViewer",
55
"description": "DGMLViewer is viewer for dgml (Directed Graph Markup Language) files",
66
"icon": "icon.png",
7-
"version": "1.3.0",
7+
"version": "1.3.1",
88
"repository": "https://github.com/CoderAllan/vscode-dgmlviewer",
99
"engines": {
1010
"vscode": "^1.55.0"

src/dgmlParser.ts

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import {
66
ICategory,
77
ICondition,
88
IDirectedGraph,
9-
Link,
10-
Node,
9+
IPath,
1110
IProperty,
1211
ISetter,
1312
IStyle,
14-
IXmlNode
13+
IXmlNode,
14+
Link,
15+
Node
1516
} from '@model';
1617

1718
export class DgmlParser {
@@ -47,6 +48,8 @@ export class DgmlParser {
4748
directedGraph.properties = this.convertXmlToProperties(xmlNode.children);
4849
} else if (xmlNode.name !== undefined && xmlNode.name.toLowerCase() === 'styles') {
4950
directedGraph.styles = this.convertXmlToStyles(xmlNode.children);
51+
} else if (xmlNode.name !== undefined && xmlNode.name.toLowerCase() === 'paths') {
52+
directedGraph.paths = this.convertXmlToPaths(xmlNode.children);
5053
}
5154
});
5255
this.addStylingToCategories(directedGraph);
@@ -99,6 +102,7 @@ export class DgmlParser {
99102
newNode.style = this.getAttributeValue(attributesCopy, 'style');
100103
newNode.horizontalAlignment = this.getAttributeValue(attributesCopy, 'horizontalalignment');
101104
newNode.verticalAlignment = this.getAttributeValue(attributesCopy, 'verticalalignment');
105+
newNode.filePath = this.getAttributeValue(attributesCopy, 'filepath');
102106
const minWidth = this.getAttributeValue(attributesCopy, 'minwidth');
103107
newNode.minWidth = minWidth !== undefined ? +minWidth : undefined;
104108
const maxWidth = this.getAttributeValue(attributesCopy, 'maxwidth');
@@ -116,10 +120,10 @@ export class DgmlParser {
116120
newNode.boundsWidth = +bounds[2];
117121
newNode.boundsHeight = +bounds[3];
118122
}
119-
const additionalProperties = Object.keys(attributesCopy);
120-
if (additionalProperties.length > 0) {
121-
additionalProperties.forEach(property => {
122-
newNode.properties.push({ id: property, value: attributesCopy[property] });
123+
const customProperties = Object.keys(attributesCopy);
124+
if (customProperties.length > 0) {
125+
customProperties.forEach(property => {
126+
newNode.customProperties.push({ id: property, value: attributesCopy[property] });
123127
});
124128
}
125129
if (nodes.filter(n => n.id === newNode.id).length === 0) {
@@ -275,6 +279,23 @@ export class DgmlParser {
275279
return styles;
276280
}
277281

282+
private convertXmlToPaths(xmlNodes: IXmlNode[]): IPath[] {
283+
const paths: IPath[] = [];
284+
if (xmlNodes.length > 0) {
285+
xmlNodes.forEach(xmlNode => {
286+
if (xmlNode.attributes !== undefined) {
287+
const attributesCopy: { [key: string]: string } = this.toLowercaseDictionary(xmlNode.attributes);
288+
const newProperty = {
289+
id: attributesCopy['id'],
290+
value: attributesCopy['value'],
291+
} as IPath;
292+
paths.push(newProperty);
293+
}
294+
});
295+
}
296+
return paths;
297+
}
298+
278299
private createCondition(xmlNode: IXmlNode): ICondition | undefined {
279300
let condition: ICondition | undefined = undefined;
280301
if (xmlNode.children !== undefined) {
@@ -409,15 +430,31 @@ export class DgmlParser {
409430
}
410431

411432
private enrichNodes(directedGraph: IDirectedGraph): void {
412-
directedGraph.properties.forEach(property => {
413-
directedGraph.nodes.forEach(node => {
414-
if (node.properties.length > 0) {
415-
const existingPropertyIdx = node.properties.findIndex(nodeProperty => nodeProperty.id.toLowerCase() === property.id.toLowerCase());
433+
directedGraph.nodes.forEach(node => {
434+
if (node.filePath !== undefined) {
435+
node.filePath = this.replacePaths(node.filePath, directedGraph);
436+
}
437+
if (node.customProperties !== undefined && node.customProperties.length > 0) {
438+
directedGraph.properties.forEach(property => {
439+
const existingPropertyIdx = node.customProperties.findIndex(nodeProperty => nodeProperty.id.toLowerCase() === property.id.toLowerCase());
416440
if (existingPropertyIdx !== -1) {
417-
Object.assign(node.properties[existingPropertyIdx], property);
441+
Object.assign(node.customProperties[existingPropertyIdx], property);
442+
if (node.customProperties[existingPropertyIdx].isReference) {
443+
node.customProperties[existingPropertyIdx].value = this.replacePaths(node.customProperties[existingPropertyIdx].value, directedGraph);
444+
}
418445
}
419-
}
420-
});
446+
});
447+
}
421448
});
422449
}
450+
451+
private replacePaths(filePath: string | undefined, directedGraph: IDirectedGraph): string | undefined {
452+
let fixedFilepath = filePath;
453+
if (fixedFilepath !== undefined && directedGraph.paths !== undefined && directedGraph.paths.length > 0) {
454+
directedGraph.paths.forEach(path => {
455+
fixedFilepath = fixedFilepath?.replace(`\$(${path.id})`, path.value);
456+
});
457+
}
458+
return fixedFilepath;
459+
}
423460
}

src/model/IDirectedGraph.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import {
22
ICategory,
3-
Link,
3+
IPath,
44
IProperty,
55
IStyle,
6+
Link,
67
Node
78
} from '@model';
89

@@ -22,4 +23,5 @@ export interface IDirectedGraph {
2223
categories: ICategory[];
2324
properties: IProperty[];
2425
styles: IStyle[];
26+
paths: IPath[];
2527
}

src/model/IPath.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface IPath {
2+
id: string;
3+
value: string;
4+
}

src/model/Node.ts

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ export class Node extends BaseElement {
3232
public minWidth: number | undefined;
3333
public maxWidth: number | undefined;
3434
public nodeRadius: number | undefined;
35-
// CodeSchemaAttributes - Not included
35+
// CodeSchemaAttributes - Not all included
36+
public filePath: string | undefined;
3637
// Visual Studio generated properties
3738
public boundsX: number | undefined;
3839
public boundsY: number | undefined;
@@ -44,7 +45,7 @@ export class Node extends BaseElement {
4445
this.categoryRef = categoryRef;
4546
}
4647

47-
public properties: IProperty[] = [];
48+
public customProperties: IProperty[] = [];
4849

4950
constructor(filename: string) {
5051
super();
@@ -113,24 +114,37 @@ export class Node extends BaseElement {
113114
if (this.boundsX !== undefined && this.boundsY !== undefined) { jsStringProperties.push(`x: ${this.boundsX}, y: ${this.boundsY}, fixed: { x: true, y: true}`); }
114115
if (this.boundsWidth !== undefined) { jsStringProperties.push(`widthConstraint: { minimum: ${this.boundsWidth} }`); }
115116
if (this.boundsHeight !== undefined) { jsStringProperties.push(`heightConstraint: { minimum: ${this.boundsHeight}, valign: top }`); }
116-
if (this.properties.length > 0) {
117-
const referenceProperties = this.properties.find(property => property.isReference === true);
118-
if (referenceProperties !== undefined && referenceProperties.value !== undefined) {
119-
const fsUtil = new FileSystemUtils();
120-
if (fsUtil.fileExists(referenceProperties.value)) {
121-
let referenceFilename = referenceProperties.value.split('\\').join('/');
122-
jsStringProperties.push(`filepath: "${referenceFilename}"`);
123-
} else {
124-
const currentDocumentFolder = path.dirname(this.filename);
125-
let referenceFilename = path.join(currentDocumentFolder, referenceProperties.value);
126-
if (fsUtil.fileExists(referenceFilename)) {
127-
referenceFilename = referenceFilename.split('\\').join('/');
128-
jsStringProperties.push(`filepath: "${referenceFilename}"`);
129-
}
130-
}
117+
let referencePropertyValue: string | undefined;
118+
if (this.filePath !== undefined) {
119+
referencePropertyValue = this.getReferenceFilename(this.filePath);
120+
}
121+
if (referencePropertyValue === undefined && this.customProperties.length > 0) {
122+
const firstReferenceProperty = this.customProperties.find(property => property.isReference === true);
123+
if (firstReferenceProperty !== undefined && firstReferenceProperty.value !== undefined) {
124+
referencePropertyValue = this.getReferenceFilename(firstReferenceProperty.value);
125+
}
126+
}
127+
if (referencePropertyValue !== undefined) {
128+
jsStringProperties.push(`filepath: "${referencePropertyValue}"`);
129+
}
130+
return `{${jsStringProperties.join(', ')}}`;
131+
}
132+
133+
private getReferenceFilename(propertyValue: string): string | undefined {
134+
let referenceFilename: string | undefined = undefined;
135+
const fsUtil = new FileSystemUtils();
136+
if (fsUtil.fileExists(propertyValue)) {
137+
referenceFilename = propertyValue.split('\\').join('/');
138+
} else {
139+
const currentDocumentFolder = path.dirname(this.filename);
140+
referenceFilename = path.join(currentDocumentFolder, propertyValue);
141+
if (fsUtil.fileExists(referenceFilename)) {
142+
referenceFilename = referenceFilename.split('\\').join('/');
143+
} else {
144+
referenceFilename = undefined;
131145
}
132146
}
133-
return `{${jsStringProperties.join(', ')}}`;
147+
return referenceFilename;
134148
}
135149

136150
private convertNewlines(text: string | undefined): string {

src/model/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export * from './ICategory';
22
export * from './ICondition';
33
export * from './IDirectedGraph';
44
export * from './Link';
5+
export * from './IPath';
56
export * from './IProperty';
67
export * from './ISetter';
78
export * from './IStyle';

0 commit comments

Comments
 (0)