Skip to content

Commit

Permalink
add saveData() hook and post to tableView.ts (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomFractals committed Dec 24, 2021
1 parent 1051441 commit 84f91a8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
14 changes: 13 additions & 1 deletion src/views/tableView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,12 @@ export class TableView {
const text = message.text;
switch (command) {
case 'refresh':
// relad data view and config
// reload data view and config
this.refresh();
break;
case 'saveData':
this.saveData(message.data, message.dataFileName, message.dataFileType);
break;
}
}, undefined, this._disposables);
}
Expand Down Expand Up @@ -187,6 +191,14 @@ export class TableView {
});
}

/**
* Saves table data in a new data file.
*/
public async saveData(data: any, dataFileName: string, dataFileType: string): Promise<void> {
console.log('tabView:saveData(): saving data:', dataFileName);
this.logTextData(data);
}

/**
* Logs truncated text data for debug.
*
Expand Down
18 changes: 13 additions & 5 deletions web/scripts/tableView.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const vscode = acquireVsCodeApi();
// data document vars
let documentUrl = '';
let fileName = '';
let saveDataFileName = '';

// table view vars
let tableContainer, table, progressRing, saveFileTypeSelector;
Expand Down Expand Up @@ -187,9 +188,15 @@ function loadData(tableData, documentUrl) {
downloadReady: function (fileContents, blob) {
// fileContents - unencoded contents of the file to save
// blob - blob object for data file download/save
console.log(fileContents);
// console.log(fileContents);

// TODO: save data via vscode workspace.fs api
// request data file save
vscode.postMessage({
command: 'saveData',
data: fileContents,
dataFileType: saveFileTypeSelector.value,
dataFileName: saveDataFileName
});

// Note: this must return a blob to proceed with the download in a browser,
// or false to abort download and handle it in table view extension with workspace.fs
Expand Down Expand Up @@ -240,13 +247,14 @@ function addData(table, tableData) {
}

/**
* Saves table data.
* Saves table data as CSV or JSON for now.
*/
function saveData() {
const dataFileType = saveFileTypeSelector.value;
const dataFileName = fileName.substring(0, fileName.lastIndexOf('.') + 1);
console.log('tabView:saveData(): saving data:', dataFileName + dataFileType);
table.download(dataFileType, dataFileName + dataFileType);
saveDataFileName = dataFileName + dataFileType;
console.log('tabView:saveData(): saving data:', saveDataFileName);
table.download(dataFileType, saveDataFileName);
}

/**
Expand Down

0 comments on commit 84f91a8

Please sign in to comment.