Skip to content
This repository was archived by the owner on Dec 12, 2021. It is now read-only.

Commit a796bc7

Browse files
committed
Added import to IMEX module
1 parent 299699a commit a796bc7

File tree

4 files changed

+131
-10
lines changed

4 files changed

+131
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
#### Features
88
- Snippet tag functionalities
99
- Edit option for snippets
10-
- IMEX module *(Export option)*
10+
- IMEX module *(Import & Export options)*
1111
#### Improvements
1212
- Quick Start Guide enhanced
1313
- `README.md`
14+
- IMEX module config
1415
#### Patches
1516
- Debug output
1617
- Available debug commands reduced

lib/db/imex.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
{
2-
"csv": {
1+
[
2+
{
3+
"name": "csv",
4+
"dialogOptions": {
5+
"name": "CSV File",
6+
"extensions": ["csv"]
7+
},
38
"labels": [
49
"Title",
510
"Author",
@@ -45,4 +50,4 @@
4550
"line": "\n"
4651
}
4752
}
48-
}
53+
]

lib/imex.js

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,45 @@ export default class IMEX {
4141
}
4242

4343
storage_import() {
44+
var filters = new Array();
45+
this.data.forEach(function(e) {
46+
filters.push({
47+
name: e.dialogOptions.name,
48+
extensions: e.dialogOptions.extensions
49+
});
50+
});
51+
filters.push({name: 'Any File', extensions: ['*']});
4452

53+
dialog.showOpenDialog({
54+
buttonLabel: 'Import',
55+
properties: ['openFile'],
56+
filters: filters
57+
},function(filename) {
58+
if(Util.isset(filename,'string')) {
59+
var temp = path.extname(filename).replace('.','').toLowerCase();
60+
switch(temp) {
61+
case 'csv':
62+
this._impcsv(filename);
63+
return true;
64+
break;
65+
default:
66+
return false;
67+
}
68+
}
69+
});
4570
}
4671

4772
// internal methods
4873

4974
_expcsv() {
5075
var storage = this.store;
51-
var config = this.data.csv;
76+
var config = this.data.filter(function(e) {
77+
if(e.name === 'csv') {
78+
return true;
79+
} else {
80+
return false;
81+
}
82+
});
5283
var files = storage.retrieveFiles();
5384
var csv_string = '';
5485
if(files.length > 0) {
@@ -89,13 +120,18 @@ export default class IMEX {
89120
});
90121
});
91122

123+
var filters = new Array();
124+
this.data.forEach(function(e) {
125+
filters.push({
126+
name: e.dialogOptions.name,
127+
extensions: e.dialogOptions.extensions
128+
});
129+
});
130+
filters.push({name: 'Any File', extensions: ['*']});
131+
92132
dialog.showSaveDialog({
93-
title: 'Choose the location for your export',
94133
buttonLabel: 'Export',
95-
filters: [
96-
{name: 'CSV File', extensions: ['csv']},
97-
{name: 'Any File', extensions: ['*']}
98-
]
134+
filters: filters
99135
},function(filename) {
100136
if(Util.isset(filename,'string')) {
101137
fs.writeFileSync(filename,csv_string);
@@ -113,4 +149,51 @@ export default class IMEX {
113149
return false;
114150
}
115151
}
152+
153+
_impcsv(filename) {
154+
var storage = this.store;
155+
var config = this.data.filter(function(e) {
156+
if(e.name === 'csv') {
157+
return true;
158+
} else {
159+
return false;
160+
}
161+
});
162+
163+
var count = 0;
164+
fs.readFileSync(filename).split(config.divider.line).forEach(function(e,i,a) {
165+
if(i > 0) {
166+
var snippet = new Snippet();
167+
e.split(config.divider.field).forEach(function(field,index) {
168+
var type = config.order[index];
169+
var insert = field;
170+
if(config.convert.length > 0) {
171+
config.convert.filter(function(item) {
172+
if(item.name === type) {
173+
return true;
174+
} else {
175+
return false;
176+
}
177+
}).forEach(function(conversion) {
178+
if(conversion.type === 'replace') {
179+
insert = insert.split(conversion.replace).join(conversion.search);
180+
} else if(conversion.type === 'json') {
181+
insert = JSON.parse(insert);
182+
}
183+
});
184+
}
185+
snippet._set(type,insert);
186+
});
187+
storage.store(snippet);
188+
count++;
189+
}
190+
});
191+
if(count < 1) {
192+
atom.notifications.addWarning('No snippets have been imported.', null)
193+
} else if(count == 1) {
194+
atom.notifications.addSuccess('One snippet has been imported.', null);
195+
} else {
196+
atom.notifications.addSuccess(count+' snippets have been imported.', null);
197+
}
198+
}
116199
}

lib/snippet.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,38 @@ export default class Snippet {
154154
}
155155
}
156156

157+
_set(attribute,value) {
158+
if(Util.isset(attribute,'string')) {
159+
switch (attribute.toLowerCase()) {
160+
case 'content':
161+
this.setContent(value);
162+
break;
163+
case 'title':
164+
this.setTitle(value);
165+
break;
166+
case 'tags':
167+
this.setTags(value);
168+
break;
169+
case 'lang':
170+
this.setLang(value);
171+
break;
172+
case 'uid':
173+
this.setUID(value);
174+
break;
175+
case 'version':
176+
this.setVersion(value);
177+
break;
178+
case 'author':
179+
this.setAuthor(value);
180+
break;
181+
default:
182+
return undefined;
183+
}
184+
} else {
185+
return undefined;
186+
}
187+
}
188+
157189
static getLastUpdate() {
158190
return lastUpdate;
159191
}

0 commit comments

Comments
 (0)