Skip to content

Commit

Permalink
#34 implemented .properties save/export data for property grid display
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomFractals committed Jun 17, 2019
1 parent 7446fcc commit ede6d15
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 12 deletions.
87 changes: 87 additions & 0 deletions data/yaml/demo.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
map.Block style.Clark=Evans
map.Block style.Ingy=d枚t Net
map.Block style.Oren=Ben-Kiki
map.Flow style.Clark=Evans
map.Flow style.Ingy=d枚t Net
map.Flow style.Oren=Ben-Kiki
omap.Bestiary.0.aardvark=African pig-like ant eater. Ugly.
omap.Bestiary.1.anteater=South-American ant eater. Two species.
omap.Bestiary.2.anaconda=South-American constrictor snake. Scaly.
omap.Numbers.0.one=1
omap.Numbers.1.two=2
omap.Numbers.2.three=3
pairs.Block tasks.0.0=meeting
pairs.Block tasks.0.1=with team.
pairs.Block tasks.1.0=meeting
pairs.Block tasks.1.1=with boss.
pairs.Block tasks.2.0=break
pairs.Block tasks.2.1=lunch.
pairs.Block tasks.3.0=meeting
pairs.Block tasks.3.1=with client.
pairs.Flow tasks.0.0=meeting
pairs.Flow tasks.0.1=with team
pairs.Flow tasks.1.0=meeting
pairs.Flow tasks.1.1=with boss
seq.Block style.0=Mercury
seq.Block style.1=Venus
seq.Block style.2=Earth
seq.Block style.3=Mars
seq.Block style.4=Jupiter
seq.Block style.5=Saturn
seq.Block style.6=Uranus
seq.Block style.7=Neptune
seq.Block style.8=Pluto
seq.Flow style.0=Mercury
seq.Flow style.1=Venus
seq.Flow style.2=Earth
seq.Flow style.3=Mars
seq.Flow style.4=Jupiter
seq.Flow style.5=Saturn
seq.Flow style.6=Uranus
seq.Flow style.7=Neptune
seq.Flow style.8=Pluto
bool.0=true
bool.1=true
bool.2=true
float.canonical=685230.15
float.exponentioal=685230.15
float.fixed=685230.15
float.sexagesimal=685230.15
float.negative infinity=-Infinity
int.canonical=685230
int.decimal=685230
int.octal=685230
int.hexadecimal=685230
int.binary=685230
int.sexagesimal=685230
merge.0.x=1
merge.0.y=2
merge.1.y=2
merge.2.r=10
merge.3.r=1
merge.4.x=1
merge.4.y=2
merge.4.r=10
merge.4.label=nothing
merge.5.x=1
merge.5.y=2
merge.5.r=10
merge.5.label=center
merge.6.x=1
merge.6.y=2
merge.6.r=10
merge.6.label=center/big
merge.7.r=10
merge.7.x=1
merge.7.y=2
merge.7.label=big/left/small
null.null=null key
null.sparse.1=2nd entry
null.sparse.3=4th entry
string=abcd
function=function anonymous(
) {

return 'Wow! JS-YAML Rocks!';

}
49 changes: 37 additions & 12 deletions src/data.preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -636,19 +636,44 @@ export class DataPreview {
});

if (dataFileUri) {
if (dataFilePath.endsWith('.config') || dataFilePath.endsWith('.json')) {
fileData = JSON.stringify(fileData, null, 2);
} else if (dataFilePath.endsWith('.yml')) {
// convert to yaml. see: https://github.com/nodeca/js-yaml#safedump-object---options-
fileData = yaml.dump(fileData, {skipInvalid: true});
const dataFileExtension = dataFilePath.substr(dataFilePath.lastIndexOf('.'));
switch (dataFileExtension) {
case '.config':
case '.json':
fileData = JSON.stringify(fileData, null, 2);
break;
case '.yml':
// convert to yaml. see: https://github.com/nodeca/js-yaml#safedump-object---options-
fileData = yaml.dump(fileData, {skipInvalid: true});
break;
case '.properties':
if (fileData.length > 0 &&
fileData[0].hasOwnProperty('key') && fileData[0].hasOwnProperty('value')) { // data in properties format
let propertiesString: string = '';
fileData = fileData.forEach(property => {
// convert it to properties string
propertiesString += `${property['key']}=${property['value']}\n`;
});
fileData = propertiesString;
} else {
// display not a properties collection warning
fileData = '';
window.showWarningMessage(`Preview data is not a Properties collection. Use Save JSON or YAML format to save it.`);
}
break;
}

if (fileData.length > 0) {
// save exported data
// TODO: change this to async later
fs.writeFile(dataFileUri.fsPath, fileData, (error) => {
if (error) {
const errorMessage: string = `Failed to save file: ${dataFileUri.fsPath}`;
this._logger.logMessage(LogLevel.Error, 'saveData():', errorMessage);
window.showErrorMessage(errorMessage);
}
});
}
fs.writeFile(dataFileUri.fsPath, fileData, (error) => {
if (error) {
const errorMessage: string = `Failed to save file: ${dataFileUri.fsPath}`;
this._logger.logMessage(LogLevel.Error, 'saveData():', errorMessage);
window.showErrorMessage(errorMessage);
}
});
}
} // end of saveData()

Expand Down
2 changes: 2 additions & 0 deletions templates/data.view.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<option value=".csv">csv</option>
<option value=".json">json</option>
<option value=".yml">yaml</option>
<option value=".properties">properties</option>
</select>
</div>
<div>
Expand Down Expand Up @@ -315,6 +316,7 @@
break;
case '.json':
case '.yml':
case '.properties':
viewer.view.to_json().then(json => postData(dataFileType, json));
break;
}
Expand Down

0 comments on commit ede6d15

Please sign in to comment.