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

Commit dcf8ab1

Browse files
committed
Added delete command
1 parent d783361 commit dcf8ab1

File tree

5 files changed

+122
-22
lines changed

5 files changed

+122
-22
lines changed

keymaps/snippet-injector.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"atom-workspace": {
3-
"ctrl-alt-o": "snippet-injector:create"
3+
"ctrl-alt-o": "snippet-injector:create",
4+
"ctrl-alt-i": "snippet-injector:insert"
45
}
56
}

lib/snippet-injector.js

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export default {
1919
// Register command that toggles this view
2020
this.subscriptions.add(atom.commands.add('atom-workspace', {
2121
'snippet-injector:create': () => this.create(),
22-
'snippet-injector:insert': () => this.insert()
22+
'snippet-injector:insert': () => this.insert(),
23+
'snippet-injector:delete': () => this.delete()
2324
}));
2425
},
2526

@@ -41,15 +42,17 @@ export default {
4142
btnconfirm: 'Save snippet',
4243
btncancel: 'Cancel'
4344
},function(text){
44-
var result = storage.store(new Snippet({
45-
title: text,
46-
tags: new Array(),
47-
content: selection
48-
}));
49-
if(result) {
50-
atom.notifications.addSuccess('Snippet "'+text+'" was saved successfully.', null);
51-
} else {
52-
atom.notifications.addError('An Error occured while saving the snippet "'+text+'".', null);
45+
if(text !== null) {
46+
var result = storage.store(new Snippet({
47+
title: text,
48+
tags: new Array(),
49+
content: selection
50+
}));
51+
if(result) {
52+
atom.notifications.addSuccess('Snippet "'+text+'" was saved successfully.', null);
53+
} else {
54+
atom.notifications.addError('An Error occured while saving the snippet "'+text+'".', null);
55+
}
5356
}
5457
});
5558
},
@@ -74,18 +77,39 @@ export default {
7477
btncancel: 'Cancel',
7578
nothingfound: 'No Snippets found that match you search.'
7679
},function(element) {
77-
var snippet = new Snippet(JSON.parse(storage.retrieveFile(element)));
78-
atom.workspace.getActiveTextEditor().insertText(snippet.getContent(),{
79-
select: true,
80-
autoIndent: true,
81-
autoIndentNewline: true,
82-
autoDecreaseIndent: true,
83-
normalizeLineEndings: true
84-
});
85-
atom.notifications.addInfo('Snippet \''+element+'\' was successfully inserted.', null);
80+
if(element !== null) {
81+
var snippet = new Snippet(JSON.parse(storage.retrieveFile(element)));
82+
atom.workspace.getActiveTextEditor().insertText(snippet.getContent(),{
83+
select: true,
84+
autoIndent: true,
85+
autoIndentNewline: true,
86+
autoDecreaseIndent: true,
87+
normalizeLineEndings: true
88+
});
89+
atom.notifications.addInfo('Snippet \''+element+'\' was successfully inserted.', null);
90+
}
8691
});
8792
} else {
8893
atom.notifications.addWarning('No snippets found in the local storage directory.', null);
8994
}
95+
},
96+
97+
delete() {
98+
var storage = this.storage;
99+
const deletePrompt = Util.promptDelete({
100+
title: 'Delete a snippet',
101+
placeholder: 'Title of the snippet that shall be deleted',
102+
btnconfirm: 'Delete',
103+
btncancel: 'Cancel',
104+
suremsg: 'Sure?'
105+
},function(element) {
106+
if(element !== null) {
107+
if(storage.deleteFile(element)) {
108+
atom.notifications.addSuccess('Snippet \''+element+'\' was successfully deleted.', null);
109+
} else {
110+
atom.notifications.addWarning('Snippet \''+element+'\' could not be found. Is it spelled right?', null);
111+
}
112+
}
113+
});
90114
}
91115
};

lib/storage.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ export default class Storage {
6363

6464
deleteFile(snippetTitle) {
6565
if(fs.existsSync(this.storageDir+this.dir+snippetTitle.split(' ').join('-')+'.snippet.json')) {
66-
return fs.unlinkSync(this.storageDir+this.dir+snippetTitle.split(' ').join('-')+'.snippet.json');
66+
fs.unlinkSync(this.storageDir+this.dir+snippetTitle.split(' ').join('-')+'.snippet.json');
67+
return true;
6768
} else {
6869
return false;
6970
}

lib/util.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export default class Util {
5959
});
6060
$('#prompt-cancel').on('click',function(e) {
6161
modal.destroy();
62+
callback(null);
6263
});
6364

6465
modal.show();
@@ -131,6 +132,65 @@ export default class Util {
131132
});
132133
$('#prompt-cancel').on('click',function(e) {
133134
modal.destroy();
135+
callback(null);
136+
});
137+
138+
modal.show();
139+
}
140+
}
141+
142+
static promptDelete(options,callback) {
143+
if(typeof options !== 'object') {
144+
throw TypeError('Param \'options\' has to be an object.');
145+
} else if(typeof callback !== 'function') {
146+
throw TypeError('Param \'callback\' has to be a function.');
147+
} else {
148+
const deletePrompt = document.createElement('div');
149+
deletePrompt.id = 'delete-prompt';
150+
151+
const modal = atom.workspace.addModalPanel({
152+
item: deletePrompt,
153+
visible: false
154+
});
155+
156+
if(options.title !== undefined && options.title !== null) {
157+
$('#delete-prompt').append('<h4 class="title">'+options.title+'</h4>');
158+
}
159+
var edit = atom.workspace.buildTextEditor({mini: true});
160+
edit.setPlaceholderText(''+options.placeholder);
161+
$('#delete-prompt').append(atom.views.getView(edit));
162+
$('#delete-prompt').find('atom-text-editor').attr('id','prompt-input');
163+
$('#delete-prompt').append('<button class="btn btn-error disabled" id="prompt-confirm">'+options.btnconfirm+'</button>\
164+
<button class="btn btn-default" id="prompt-cancel">'+options.btncancel+'</button>');
165+
edit.onDidChange(function() {
166+
if((edit.getText() === '' || edit.getText() === ' ' || edit.getText() === null || edit.getText() === undefined)) {
167+
if(!$('#prompt-confirm').hasClass('disabled')) {
168+
$('#prompt-confirm').addClass('disabled');
169+
}
170+
} else {
171+
if($('#prompt-confirm').hasClass('disabled')) {
172+
$('#prompt-confirm').removeClass('disabled');
173+
}
174+
}
175+
});
176+
$('#prompt-confirm').on('click',function(e) {
177+
if(!$(this).hasClass('disabled')) {
178+
var text = edit.getText();
179+
$('#delete-prompt').children().remove();
180+
$('#delete-prompt').append('<h4 class="title">'+options.suremsg+'</h4>');
181+
$('#delete-prompt').append('<button class="btn btn-error" id="prompt-confirm">'+options.btnconfirm+'</button>\
182+
<button class="btn btn-default" id="prompt-cancel">'+options.btncancel+'</button>');
183+
184+
$('#prompt-confirm').off('click');
185+
$('#prompt-confirm').on('click',function() {
186+
modal.destroy();
187+
callback(text);
188+
});
189+
}
190+
});
191+
$('#prompt-cancel').on('click',function(e) {
192+
modal.destroy();
193+
callback(null);
134194
});
135195

136196
modal.show();

menus/snippet-injector.json

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
{
22
"context-menu": {
33
"atom-text-editor": [
4+
{
5+
"type": "separator"
6+
},
47
{
58
"label": "Create Snippet from selection",
69
"command": "snippet-injector:create"
710
},
811
{
912
"label": "Insert Snippet",
1013
"command": "snippet-injector:insert"
14+
},
15+
{
16+
"label": "Delete Snippet",
17+
"command": "snippet-injector:delete"
18+
},
19+
{
20+
"type": "separator"
1121
}
1222
]
1323
},
@@ -16,7 +26,7 @@
1626
"label": "Packages",
1727
"submenu": [
1828
{
19-
"label": "snippet-injector",
29+
"label": "Snippet Injector",
2030
"submenu": [
2131
{
2232
"label": "Create Snippet from selection",
@@ -25,6 +35,10 @@
2535
{
2636
"label": "Insert Snippet",
2737
"command": "snippet-injector:insert"
38+
},
39+
{
40+
"label": "Delete Snippet",
41+
"command": "snippet-injector:delete"
2842
}
2943
]
3044
}

0 commit comments

Comments
 (0)