Skip to content

Commit 18f04d0

Browse files
committed
import, export commands in extension
1 parent b1f5ebd commit 18f04d0

File tree

4 files changed

+92
-10
lines changed

4 files changed

+92
-10
lines changed

media/scripts/uiLogic-main.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
const vscode = acquireVsCodeApi();
22
let navChart = undefined;
33
let infoChart = undefined;
4+
let Vizzu = undefined;
45

56
(function () {
67
window.addEventListener('message', async event => {
78
const message = event.data;
89
switch (message.command) {
910
case 'clear-data-table':
1011
await resetVizzuCharts();
11-
break;
12+
break;
1213
case 'refresh-data-table':
1314
await initializingVizzuCharts(message.dataTable);
1415
performInitAnimation(message.dataSummary);
@@ -25,10 +26,9 @@ function importVizzuLibAndCreateCharts() {
2526
navChart = undefined;
2627
infoChart = undefined;
2728
let promise = import('https://cdn.jsdelivr.net/npm/vizzu@~0.4.0/dist/vizzu.min.js');
28-
promise.then( (Vizzu) => {
29+
promise.then( (lib) => {
2930
try {
30-
navChart = new Vizzu.default('navVizzu');
31-
infoChart = new Vizzu.default('infoVizzu');
31+
Vizzu = lib;
3232
vscode.postMessage({ command: 'vizzu-ready' });
3333
}
3434
catch (e) {
@@ -43,6 +43,8 @@ function importVizzuLibAndCreateCharts() {
4343
}
4444

4545
async function initializingVizzuCharts(data) {
46+
navChart = new Vizzu.default('navVizzu');
47+
infoChart = new Vizzu.default('infoVizzu');
4648
await infoChart
4749
.initializing
4850
.then(infoChart => infoChart.animate({data: data}));
@@ -58,4 +60,13 @@ async function resetVizzuCharts() {
5860
await navChart.animate({data: {}});
5961
navChart.off('click');
6062
navChart.off('plot-axis-label-draw');
63+
let element1 = document.getElementById("navVizzu");
64+
while (element1.firstChild) {
65+
element1.removeChild(element1.firstChild);
66+
}
67+
let element2 = document.getElementById("infoVizzu");
68+
while (element2.firstChild) {
69+
element2.removeChild(element2.firstChild);
70+
}
71+
vscode.postMessage({ command: 'vizzu-ready' });
6172
}

package.json

+10-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
"report"
4545
],
4646
"activationEvents": [
47-
"onCommand:CodeViz.show"
47+
"onCommand:CodeViz.show",
48+
"onCommand:CodeViz.import"
4849
],
4950
"extensionDependencies": [
5051
"uctakeoff.vscode-counter"
@@ -63,6 +64,14 @@
6364
{
6465
"command": "CodeViz.show",
6566
"title": "CodeViz: Show statistics"
67+
},
68+
{
69+
"command": "CodeViz.import",
70+
"title": "CodeViz: Import statistics"
71+
},
72+
{
73+
"command": "CodeViz.export",
74+
"title": "CodeViz: Export statistics"
6675
}
6776
]
6877
},

src/extension.ts

+29
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,33 @@ import { VSCCDataPrep } from "./data/vscc_dataprep";
66
import { Summary } from "./data/vscc_result";
77

88
export function activate(context: ExtensionContext) {
9+
const importCommand = commands.registerCommand("CodeViz.import", async () => {
10+
let wsPath: Uri;
11+
workspace.workspaceFolders?.map((folder) => {
12+
if (wsPath == undefined)
13+
wsPath = folder.uri;
14+
});
15+
const folderPath = await window.showInputBox({
16+
placeHolder: "import path",
17+
prompt: "Enter import data path",
18+
value: ""
19+
});
20+
if (folderPath) {
21+
CCVizzuPanel.render(context.extensionUri).then(() => {
22+
CCVizzuPanel.import(folderPath);
23+
});
24+
}
25+
});
26+
const exportCommand = commands.registerCommand("CodeViz.export", async () => {
27+
const folderPath = await window.showInputBox({
28+
placeHolder: "export path",
29+
prompt: "Enter export data path",
30+
value: ""
31+
});
32+
if (folderPath) {
33+
CCVizzuPanel.export(folderPath);
34+
}
35+
});
936
const showCommand = commands.registerCommand("CodeViz.show", () => {
1037
let wsPath: Uri;
1138
workspace.workspaceFolders?.map((folder) => {
@@ -29,4 +56,6 @@ export function activate(context: ExtensionContext) {
2956
});
3057
});
3158
context.subscriptions.push(showCommand);
59+
context.subscriptions.push(importCommand);
60+
context.subscriptions.push(exportCommand);
3261
}

src/panels/ccvizzupanel.ts

+38-5
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,29 @@ export class CCVizzuPanel {
1818
this._pageGen = new PageGenerator(panel, extensionUri);
1919
}
2020

21+
public static export(folderPath: string) {
22+
if (CCVizzuPanel.currentPanel && this.currentPanel) {
23+
if (this.currentPanel._dataTable != undefined)
24+
CCVizzuPanel.currentPanel._jsonDataExport(folderPath);
25+
else
26+
window.showErrorMessage("No data to export");
27+
}
28+
}
29+
30+
public static import(folderPath: string) {
31+
if (CCVizzuPanel.currentPanel) {
32+
CCVizzuPanel.currentPanel._jsonDataImport(folderPath);
33+
if (this.currentPanel != undefined) {
34+
let panel = this.currentPanel._panel;
35+
panel.webview.postMessage({
36+
command: 'clear-data-table'
37+
});
38+
}
39+
}
40+
}
41+
2142
public static refresh(data: Object, summ: Summary) {
2243
if (this.currentPanel != undefined) {
23-
let refreshReq = this.currentPanel._dataTable != undefined;
2444
this.currentPanel._dataTable = data;
2545
this.currentPanel._dataSummary = summ;
2646
let panel = this.currentPanel._panel;
@@ -51,7 +71,7 @@ export class CCVizzuPanel {
5171
while (this._disposables.length) {
5272
const disposable = this._disposables.pop();
5373
if (disposable) {
54-
disposable.dispose();
74+
disposable.dispose();
5575
}
5676
}
5777
}
@@ -63,7 +83,6 @@ export class CCVizzuPanel {
6383
const text = message.text;
6484
switch (command) {
6585
case "vizzu-ready":
66-
//this._jsonDataExport('/home/...your home directory name comes here.../');
6786
this._panel.webview.postMessage({
6887
command: 'refresh-data-table',
6988
dataTable: this._dataTable,
@@ -96,16 +115,30 @@ export class CCVizzuPanel {
96115
);
97116
}
98117

118+
private _jsonDataImport(rootDir: string) {
119+
if (rootDir.endsWith('/'))
120+
rootDir = rootDir.substring(0, rootDir.length - 1);
121+
this._dataTable = {};
122+
this._dataSummary = {};
123+
const fs = require('fs');
124+
const text1 = fs.readFileSync(rootDir + '/data.json', {encoding:'utf8', flag:'r'});
125+
this._dataTable = JSON.parse(text1);
126+
const text2 = fs.readFileSync(rootDir + '/datasum.json', {encoding:'utf8', flag:'r'});
127+
this._dataSummary = JSON.parse(text2);
128+
}
129+
99130
private _jsonDataExport(rootDir: string) {
131+
if (rootDir.endsWith('/'))
132+
rootDir = rootDir.substring(0, rootDir.length - 1);
100133
var fs = require('fs');
101-
fs.writeFile(rootDir + 'data.json',
134+
fs.writeFile(rootDir + '/data.json',
102135
JSON.stringify(this._dataTable), function(err: Object) {
103136
if (err)
104137
console.log(err);
105138
}
106139
);
107140
var fs = require('fs');
108-
fs.writeFile(rootDir + 'datasum.json',
141+
fs.writeFile(rootDir + '/datasum.json',
109142
JSON.stringify(this._dataSummary), function(err: Object) {
110143
if (err)
111144
console.log(err);

0 commit comments

Comments
 (0)