Skip to content

Commit 0f4a0eb

Browse files
use tableshema js lib for loading data files instead of d3-dsv (#49
- add tableshema js lib to dependencies in package.json - add ext. version to v1.3.0 - change tabeView.ts to read and log tabular data info via tableshema-js lib
1 parent c9d501c commit 0f4a0eb

File tree

3 files changed

+139
-49
lines changed

3 files changed

+139
-49
lines changed

package-lock.json

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

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "tabular-data-viewer",
33
"displayName": "Tabular Data Viewer 中",
44
"description": "Tabular Data Viewer 🀄 provides custom Table View for .csv, .tsv and .tab data files with Tabulator.",
5-
"version": "1.2.0",
5+
"version": "1.3.0",
66
"engines": {
77
"vscode": "^1.63.0"
88
},
@@ -193,6 +193,7 @@
193193
"dependencies": {
194194
"@vscode/webview-ui-toolkit": "^0.8.4",
195195
"d3-dsv": "^3.0.1",
196+
"tableschema": "^1.12.4",
196197
"tabulator-tables": "^5.0.8"
197198
}
198199
}

src/views/tableView.ts

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import * as fileUtils from '../utils/fileUtils';
2121
import { FileTypes } from './fileTypes';
2222
import { ViewTypes } from './viewTypes';
2323

24-
const d3 = require('d3-dsv');
24+
// eslint-disable-next-line @typescript-eslint/naming-convention
25+
const {Table} = require('tableschema');
2526

2627
/**
2728
* Defines Table view class for managing state and behaviour of Table webview panels.
@@ -57,9 +58,12 @@ export class TableView {
5758
* @param webviewPanel Optional webview panel instance.
5859
*/
5960
public static render(extensionUri: Uri, documentUri: Uri, webviewPanel?: WebviewPanel) {
61+
// create table view Uri
6062
const viewUri: Uri = documentUri.with({ scheme: 'tabular-data' });
61-
console.log('tabular.data.view:render(): loading table view:', viewUri.toString());
62-
console.log('\tdocumentUri:', documentUri);
63+
console.log('tabular.data.view:render(): loading table view:', viewUri.toString(true)); // skip encoding
64+
// console.log('\tdocumentUri:', documentUri);
65+
66+
// check for open table view
6367
const tableView: TableView | undefined = TableView._views.get(viewUri.toString());
6468
if (tableView) {
6569
// show loaded table webview panel in the active editor view column
@@ -190,31 +194,29 @@ export class TableView {
190194
this._currentDataPage = 0;
191195
this._tableData = [];
192196

193-
// load data
194-
workspace.fs.readFile(this._documentUri).then((binaryData: Uint8Array) => {
195-
const textData: string = new TextDecoder().decode(binaryData);
196-
// this.logTextData(textData);
197-
198-
// parse dsv data for now
199-
const dsvParser = d3.dsvFormat(this.delimiter);
200-
let tableData: any = dsvParser.parse(textData, d3.autoType);
201-
this.logTableData(tableData);
202-
203-
// save table data for data streaming later
204-
this._tableData = tableData;
205-
this._totalRows = this._tableData.length;
206-
207-
// send initial data rows to table view for display
208-
const initialDataRows: Array<any> = tableData.slice(0, Math.min(this._pageDataSize, this._totalRows));
209-
this.webviewPanel.webview.postMessage({
210-
command: 'refresh',
211-
fileName: this._fileName,
212-
documentUrl: this._documentUri.toString(),
213-
tableData: initialDataRows,
214-
totalRows: this._totalRows
215-
});
216-
}, reason => {
217-
window.showErrorMessage(`Could not load \`${this._documentUri}\` content. Reason: \n ${reason}`);
197+
// load tabular data file
198+
const table = await Table.load(this._documentUri.fsPath);
199+
200+
// infer table shema
201+
const tableInfo = await table.infer();
202+
console.log('tabular.data.view:tableInfo:', tableInfo);
203+
204+
// read all table data rows
205+
const tableData: [] = await table.read({keyed: true});
206+
this.logTableData(tableData, table.headers);
207+
208+
// save table data for incrementally loading into table view later
209+
this._tableData = tableData;
210+
this._totalRows = this._tableData.length;
211+
212+
// send initial set of data rows to table view for display
213+
const initialDataRows: Array<any> = tableData.slice(0, Math.min(this._pageDataSize, this._totalRows));
214+
this.webviewPanel.webview.postMessage({
215+
command: 'refresh',
216+
fileName: this._fileName,
217+
documentUrl: this._documentUri.toString(),
218+
tableData: initialDataRows,
219+
totalRows: this._totalRows
218220
});
219221
}
220222

@@ -288,10 +290,12 @@ export class TableView {
288290
*
289291
* @param tableData Parsed table data.
290292
*/
291-
private logTableData(tableData: any): void {
293+
private logTableData(tableData: any, columns?: []): void {
292294
console.log('tabular.data.view:rowCount:', tableData.length);
293-
console.log('\tcolumns:', tableData.columns);
294-
// console.log('1st 10 rows:', tableData.slice(0, 10));
295+
if (columns) {
296+
console.log('\tcolumns:', columns );
297+
}
298+
console.log('1st 10 rows:', tableData.slice(0, 10));
295299
}
296300

297301
/**

0 commit comments

Comments
 (0)