Skip to content

Commit a71caca

Browse files
use Uri.file and open workspace file path to save exported files (#149)
plus white space cleanup/reformat
1 parent 55289fb commit a71caca

File tree

1 file changed

+47
-43
lines changed

1 file changed

+47
-43
lines changed

src/vega.preview.ts

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
'use strict';
2-
import {
3-
workspace,
4-
window,
5-
Disposable,
6-
Uri,
7-
ViewColumn,
8-
WorkspaceFolder,
2+
import {
3+
workspace,
4+
window,
5+
Disposable,
6+
Uri,
7+
ViewColumn,
8+
WorkspaceFolder,
99
Webview,
10-
WebviewPanel,
11-
WebviewPanelOnDidChangeViewStateEvent,
10+
WebviewPanel,
11+
WebviewPanelOnDidChangeViewStateEvent,
1212
WebviewPanelSerializer,
1313
commands
1414
} from 'vscode';
@@ -28,7 +28,7 @@ import {Template} from './template.manager';
2828
export class VegaPreviewSerializer implements WebviewPanelSerializer {
2929

3030
private _logger: Logger;
31-
31+
3232
/**
3333
* Creates new webview serializer.
3434
* @param viewType Web view type.
@@ -49,10 +49,10 @@ export class VegaPreviewSerializer implements WebviewPanelSerializer {
4949
previewManager.add(
5050
new VegaPreview(
5151
this.viewType,
52-
this.extensionPath,
52+
this.extensionPath,
5353
Uri.parse(state.uri),
54-
webviewPanel.viewColumn,
55-
this.template,
54+
webviewPanel.viewColumn,
55+
this.template,
5656
webviewPanel
5757
));
5858
}
@@ -62,7 +62,7 @@ export class VegaPreviewSerializer implements WebviewPanelSerializer {
6262
* Main vega preview webview implementation for this vscode extension.
6363
*/
6464
export class VegaPreview {
65-
65+
6666
protected _disposables: Disposable[] = [];
6767
private _extensionPath: string;
6868
private _uri: Uri;
@@ -87,10 +87,10 @@ export class VegaPreview {
8787
*/
8888
constructor(
8989
viewType: string,
90-
extensionPath: string,
91-
uri: Uri,
92-
viewColumn: ViewColumn,
93-
template: Template,
90+
extensionPath: string,
91+
uri: Uri,
92+
viewColumn: ViewColumn,
93+
template: Template,
9494
panel?: WebviewPanel) {
9595

9696
// save ext path, document uri, and create prview uri
@@ -112,7 +112,7 @@ export class VegaPreview {
112112
break;
113113
case 'vega.visual.vocabulary':
114114
this._title = 'Visual Vocabulary';
115-
break;
115+
break;
116116
default: // vega.help
117117
this._title = 'Vega Help';
118118
break;
@@ -141,7 +141,7 @@ export class VegaPreview {
141141
break;
142142
case 'vega.visual.vocabulary':
143143
panelIconPath = './images/visual-vocabulary.svg';
144-
break;
144+
break;
145145
default: // vega.help, etc.
146146
panelIconPath = './images/vega-viewer.svg';
147147
break;
@@ -154,14 +154,14 @@ export class VegaPreview {
154154
Uri.file(path.join(this._extensionPath, 'web/scripts'))).toString(true);
155155
const stylesPath: string = this.webview.asWebviewUri(
156156
Uri.file(path.join(this._extensionPath, 'web/styles'))).toString(true);
157-
157+
158158
this._html = template?.replace({
159159
scripts: scriptsPath,
160160
styles: stylesPath,
161161
fileName: this._fileName.replace('.json', '')
162162
});
163163

164-
// dispose preview panel
164+
// dispose preview panel
165165
this._panel.onDidDispose(() => {
166166
this.dispose();
167167
}, null, this._disposables);
@@ -191,7 +191,7 @@ export class VegaPreview {
191191
if (this._url.startsWith('https://')) {
192192
// open remote vega spec in browser
193193
commands.executeCommand('vscode.open', this._uri);
194-
}
194+
}
195195
else {
196196
// open vega spec json in text editor
197197
workspace.openTextDocument(this._uri).then(document => {
@@ -212,7 +212,7 @@ export class VegaPreview {
212212
case 'buyCoffee':
213213
const buyCoffeeUri: Uri = Uri.parse('https://ko-fi.com/datapixy');
214214
commands.executeCommand('vscode.open', buyCoffeeUri);
215-
break;
215+
break;
216216
}
217217
}, null, this._disposables);
218218
} // end of initWebview()
@@ -270,12 +270,12 @@ export class VegaPreview {
270270
if (dataUrl.startsWith('http://') || dataUrl.startsWith('https://')) {
271271
dataUri = Uri.parse(dataUrl);
272272
}
273-
else {
273+
else {
274274
// join with vega spec file path for reletive data file loading
275275
dataUri = Uri.file(path.join(path.dirname(this._uri.fsPath), dataUrl));
276276
}
277277
this._logger.info(`showData(): ${this.dataPreviewCommand}`, dataUri.toString(true));
278-
278+
279279
// execute requested data preview command
280280
let viewDataCommand: string = 'vscode.open'; // default
281281
commands.getCommands().then(availableCommands => {
@@ -334,7 +334,7 @@ export class VegaPreview {
334334
else if (description !== undefined) {
335335
// use truncated description for filename title
336336
this._fileName = `${description.substr(0, 100)}.${fileType}`;
337-
}
337+
}
338338
else {
339339
this._fileName = `Unititled.${fileType}`;
340340
}
@@ -405,7 +405,7 @@ export class VegaPreview {
405405
}
406406
};
407407
https.get(requestOptions, (response) => {
408-
response.setEncoding('utf8');
408+
response.setEncoding('utf8');
409409
let data: string = '';
410410
response.on('data', (chunk) => {
411411
data += chunk;
@@ -415,8 +415,8 @@ export class VegaPreview {
415415
const gist: any = JSON.parse(data);
416416

417417
// get vega files
418-
const vegaFiles: Array<any> = Object.keys(gist.files).filter(fileName =>
419-
(fileName.endsWith('.vg.json') || fileName.endsWith('.vl.json')));
418+
const vegaFiles: Array<any> = Object.keys(gist.files).filter(fileName =>
419+
(fileName.endsWith('.vg.json') || fileName.endsWith('.vl.json')));
420420
this._logger.debug('loadVegaGist(): Vega files', vegaFiles);
421421

422422
if (vegaFiles.length > 0) {
@@ -467,9 +467,9 @@ export class VegaPreview {
467467
});
468468
return dataFiles;
469469
}
470-
470+
471471
/**
472-
* Recursively extracts data urls from the specified vega json doc spec
472+
* Recursively extracts data urls from the specified vega json doc spec
473473
* or knowwn nested data elements for loading local data content.
474474
* @param spec Vega json doc spec root or nested data references to extract.
475475
*/
@@ -529,7 +529,7 @@ export class VegaPreview {
529529
}
530530

531531
/**
532-
* Encodes Vega spec and opens it in browser
532+
* Encodes Vega spec and opens it in browser
533533
* for preview and edits in Vega Editor online.
534534
*/
535535
private viewOnline(): void {
@@ -545,9 +545,9 @@ export class VegaPreview {
545545
* @param svg Svg document export to save.
546546
*/
547547
private async exportSvg(svg: string): Promise<void> {
548-
const svgFilePath: string = this.getFilePath().replace('.json', '');
548+
const svgFilePath: string = this.getSaveFilePath().replace('.json', '');
549549
const svgFileUri: Uri = await window.showSaveDialog({
550-
defaultUri: Uri.parse(svgFilePath).with({scheme: 'file'}),
550+
defaultUri: Uri.file(svgFilePath),
551551
filters: {'SVG': ['svg']}
552552
});
553553
if (svgFileUri) {
@@ -568,9 +568,9 @@ export class VegaPreview {
568568
*/
569569
private async exportPng(imageData: string): Promise<void> {
570570
const base64: string = imageData.replace('data:image/png;base64,', '');
571-
const pngFilePath: string = this.getFilePath().replace('.json', '');
571+
const pngFilePath: string = this.getSaveFilePath().replace('.json', '');
572572
const pngFileUri: Uri = await window.showSaveDialog({
573-
defaultUri: Uri.parse(pngFilePath).with({scheme: 'file'}),
573+
defaultUri: Uri.file(pngFilePath),
574574
filters: {'PNG': ['png']}
575575
});
576576
if (pngFileUri) {
@@ -590,9 +590,9 @@ export class VegaPreview {
590590
* @param vegaSpec Vega spec json to save.
591591
*/
592592
private async saveVegaSpec(vegaSpec: any): Promise<void> {
593-
const specFilePath: string = this.getFilePath();
593+
const specFilePath: string = this.getSaveFilePath();
594594
const specFileUri: Uri = await window.showSaveDialog({
595-
defaultUri: Uri.parse(specFilePath).with({scheme: 'file'}),
595+
defaultUri: Uri.file(specFilePath),
596596
filters: {'JSON': ['json']}
597597
});
598598
if (specFileUri) {
@@ -614,8 +614,12 @@ export class VegaPreview {
614614
/**
615615
* Creates local file path for saving json and image files.
616616
*/
617-
private getFilePath() {
617+
private getSaveFilePath() {
618618
let filePath: string = this._uri.fsPath;
619+
if (workspace.workspaceFolders && workspace.workspaceFolders.length >= 1) {
620+
const workspacePath: string = workspace.workspaceFolders[0].uri.fsPath;
621+
filePath = path.join(workspacePath, this._fileName);
622+
}
619623
if (this._url.startsWith('https://')) {
620624
filePath = this._fileName;
621625
}
@@ -649,7 +653,7 @@ export class VegaPreview {
649653
get webview(): Webview {
650654
return this._panel.webview;
651655
}
652-
656+
653657
/**
654658
* Gets the source vega spec json doc uri for this preview.
655659
*/
@@ -658,12 +662,12 @@ export class VegaPreview {
658662
}
659663

660664
/**
661-
* Gets the preview uri to load on commands triggers or vscode IDE reload.
665+
* Gets the preview uri to load on commands triggers or vscode IDE reload.
662666
*/
663667
get previewUri(): Uri {
664668
return this._previewUri;
665669
}
666-
670+
667671
/**
668672
* Gets the html content to load for this preview.
669673
*/

0 commit comments

Comments
 (0)