Skip to content

Commit 5372a10

Browse files
committed
Implemented Save As... to enable export af Png, Jpg, Svg and Json
1 parent 2b7196e commit 5372a10

14 files changed

+164
-35
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ See the full list of settings below the screenshot.
4949
| dgmlViewer.graphSelectionGuidelineWidth | The width of the guide lines shown when selecting part of a directed graph |
5050
| dgmlViewer.graphSelectionColor | The color of the selection rectangle used when selecting part of a directed graph. The string should be in rgba format or standard css color names. |
5151
| dgmlViewer.graphSelectionWidth | The width of the selection rectangle shown when selecting part of a directed graph. |
52-
| dgmlViewer.pngFilename | The default name used when saving the directed graph to a Png file. |
52+
| dgmlViewer.saveAsFilename | The default name used when saving the directed graph as a Png, Jpg, Svg or Json file. |
5353
| dgmlViewer.showPopupsOverNodesAndLinks | When the setting is set to true a popup with various information from node or edge will be shown when the mouse pointer hovers over nodes and edges. |
5454

5555
## Known Issues

images/icons8-file-download-18.png

254 Bytes
Loading

images/icons8-file-download-32.png

264 Bytes
Loading

images/icons8-jpg-24.png

258 Bytes
Loading

images/icons8-json-download-24.png

377 Bytes
Loading

images/icons8-png-24.png

272 Bytes
Loading

images/icons8-xml-file-24.png

293 Bytes
Loading

javascript/cytoscape-svg.js

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

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,10 @@
112112
"default": "2",
113113
"description": "The width of the selection rectangle shown when selecting part of a directed graph"
114114
},
115-
"dgmlViewer.pngFilename": {
115+
"dgmlViewer.saveAsFilename": {
116116
"type": "string",
117-
"default": "DirectedGraph.png",
118-
"description": "The default name used when saving the directed graph to a Png file."
117+
"default": "DirectedGraph",
118+
"description": "The default name used when saving the directed graph as a Png, Jpg, Svg or Json file."
119119
},
120120
"dgmlViewer.showPopupsOverNodesAndLinks": {
121121
"type": "boolean",

src/commands/dgmlViewer.ts

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,16 @@ export class DgmlViewer {
2727
message => {
2828
switch (message.command) {
2929
case 'saveAsPng':
30-
this.saveAsPng(message.text);
30+
this.saveAs(message.text, `${this.config.dgmlViewerSaveAsFilename}.png`);
31+
return;
32+
case 'saveAsJpg':
33+
this.saveAs(message.text, `${this.config.dgmlViewerSaveAsFilename}.jpg`);
34+
return;
35+
case 'saveAsSvg':
36+
this.saveAs(message.text, `${this.config.dgmlViewerSaveAsFilename}.svg`, true);
37+
return;
38+
case 'saveAsJson':
39+
this.saveAs(message.text, `${this.config.dgmlViewerSaveAsFilename}.json`, true);
3140
return;
3241
case 'openFile':
3342
const filename = message.text;
@@ -113,10 +122,12 @@ export class DgmlViewer {
113122
const templateHtmlFilename = DgmlViewer._name + '_Template.html';
114123
let htmlContent = fs.readFileSync(this.extensionContext?.asAbsolutePath(path.join('templates', templateHtmlFilename)), 'utf8');
115124

116-
const cytoscapeMinJs = 'cytoscape.min.js';
117-
const cytoscapePath = vscode.Uri.joinPath(this.extensionContext.extensionUri, 'javascript', cytoscapeMinJs);
118-
const cytoscapeUri = webview.asWebviewUri(cytoscapePath);
119-
htmlContent = htmlContent.replace(cytoscapeMinJs, cytoscapeUri.toString());
125+
const javascriptIncludes = ['cytoscape.min.js', 'cytoscape-svg.js'];
126+
javascriptIncludes.forEach((includeFile) => {
127+
const includePath = vscode.Uri.joinPath(this.extensionContext.extensionUri, 'javascript', includeFile);
128+
const includeUri = webview.asWebviewUri(includePath);
129+
htmlContent = htmlContent.replace(includeFile, includeUri.toString());
130+
});
120131

121132
const cssPath = vscode.Uri.joinPath(this.extensionContext.extensionUri, 'stylesheets', DgmlViewer._name + '.css');
122133
const cssUri = webview.asWebviewUri(cssPath);
@@ -142,17 +153,24 @@ export class DgmlViewer {
142153
return text;
143154
}
144155

145-
private saveAsPng(messageText: string) {
156+
private saveAs(messageText: string, filename: string, asText: boolean = false) {
146157
const dataUrl = messageText.split(',');
147-
if (dataUrl.length > 0) {
148-
const u8arr = Base64.toUint8Array(dataUrl[1]);
158+
if (!asText && dataUrl.length === 0) {
159+
return;
160+
}
161+
var content;
162+
if (asText) {
163+
content = messageText;
164+
}
165+
else {
166+
content = Base64.toUint8Array(dataUrl[1]);
167+
}
149168

150-
const workspaceDirectory = this.fsUtils.getWorkspaceFolder();
151-
const newFilePath = path.join(workspaceDirectory, this.config.dgmlViewerPngFilename);
152-
this.fsUtils.writeFile(newFilePath, u8arr, () => { });
169+
const workspaceDirectory = this.fsUtils.getWorkspaceFolder();
170+
const newFilePath = path.join(workspaceDirectory, filename);
171+
this.fsUtils.writeFile(newFilePath, content, () => { });
153172

154-
vscode.window.showInformationMessage(`The file ${this.config.dgmlViewerPngFilename} has been created in the root of the workspace.`);
155-
}
173+
vscode.window.showInformationMessage(`The file ${filename} has been created in the root of the workspace.`);
156174
}
157-
175+
158176
}

0 commit comments

Comments
 (0)