@@ -13,11 +13,9 @@ export default {
1313
1414 activate ( state ) {
1515 this . storage = new Storage ( { dir : '/storage/snippet-injector/snippets/' } ) ;
16- console . log ( this . storage ) ;
16+ this . storage . migrate ( ) ;
1717
18- // Events subscribed to in atom's system can be easily cleaned up with a CompositeDisposable
1918 this . subscriptions = new CompositeDisposable ( ) ;
20- // Register command that toggles this view
2119 this . subscriptions . add ( atom . commands . add ( 'atom-workspace' , {
2220 'snippet-injector:create' : ( ) => this . create ( ) ,
2321 'snippet-injector:insert' : ( ) => this . insert ( ) ,
@@ -39,17 +37,26 @@ export default {
3937 var selection = atom . workspace . getActiveTextEditor ( ) . getSelectedText ( ) ;
4038 var grammar = atom . workspace . getActiveTextEditor ( ) . getGrammar ( ) . name ;
4139 var storage = this . storage ;
40+ var uid = Util . generateUID ( {
41+ unique : true ,
42+ tester : storage . testFile ,
43+ timeout : 100 ,
44+ length : 20 ,
45+ prefix : 'sn' ,
46+ insertstring : 'SNIPPET'
47+ } ) ;
4248 const inputPrompt = Util . promptUser ( {
4349 placeholder : 'Enter snippet title' ,
4450 btnconfirm : 'Save snippet' ,
4551 btncancel : 'Cancel'
4652 } , function ( text ) {
47- if ( text !== null ) {
53+ if ( Util . isset ( text , 'string' ) ) {
4854 var result = storage . store ( new Snippet ( {
4955 title : text ,
5056 tags : new Array ( ) ,
5157 content : selection ,
52- lang : grammar
58+ lang : grammar ,
59+ uid : uid
5360 } ) ) ;
5461 if ( result ) {
5562 atom . notifications . addSuccess ( 'Snippet "' + text + '" was saved successfully.' , null ) ;
@@ -64,13 +71,11 @@ export default {
6471 var storage = this . storage ;
6572 var filenames = storage . retrieveFiles ( ) ;
6673 if ( filenames . length > 0 ) {
67- var snippets = new Array ( ) ;
6874 var listitems = new Array ( ) ;
6975 var icons = new Array ( ) ;
7076 filenames . forEach ( function ( currentValue ) {
71- var elem = new Snippet ( JSON . parse ( storage . retrieveFile ( currentValue . replace ( '.snippet.json' , '' ) ) ) ) ;
72- snippets . push ( elem ) ;
73- listitems . push ( elem . getTitle ( ) ) ;
77+ var elem = new Snippet ( JSON . parse ( storage . retrieveFile ( currentValue . replace ( '.json' , '' ) ) ) ) ;
78+ listitems . push ( elem . getUID ( ) ) ;
7479 icons . push ( elem . getLang ( ) ) ;
7580 } ) ;
7681
@@ -82,7 +87,7 @@ export default {
8287 nothingfound : 'No Snippets found that match you search.' ,
8388 icons : icons
8489 } , function ( element ) {
85- if ( element !== null ) {
90+ if ( Util . isset ( element , 'string' ) ) {
8691 var snippet = new Snippet ( JSON . parse ( storage . retrieveFile ( element ) ) ) ;
8792 atom . workspace . getActiveTextEditor ( ) . insertText ( snippet . getContent ( ) , {
8893 select : true ,
@@ -91,7 +96,7 @@ export default {
9196 autoDecreaseIndent : true ,
9297 normalizeLineEndings : true
9398 } ) ;
94- atom . notifications . addInfo ( 'Snippet \'' + element + '\' was successfully inserted.' , null ) ;
99+ atom . notifications . addSuccess ( 'Snippet \'' + snippet . getTitle ( ) + '\' was successfully inserted.' , null ) ;
95100 }
96101 } ) ;
97102 } else {
@@ -101,20 +106,35 @@ export default {
101106
102107 delete ( ) {
103108 var storage = this . storage ;
104- const deletePrompt = Util . promptDelete ( {
105- title : 'Delete a snippet' ,
106- placeholder : 'Title of the snippet that shall be deleted' ,
107- btnconfirm : 'Delete' ,
108- btncancel : 'Cancel' ,
109- suremsg : 'Sure?'
110- } , function ( element ) {
111- if ( element !== null ) {
112- if ( storage . deleteFile ( element ) ) {
113- atom . notifications . addSuccess ( 'Snippet \'' + element + '\' was successfully deleted.' , null ) ;
114- } else {
115- atom . notifications . addWarning ( 'Snippet \'' + element + '\' could not be found. Is it spelled right?' , null ) ;
109+ var fileNames = storage . retrieveFiles ( ) ;
110+ if ( fileNames . length > 0 ) {
111+ var items = new Array ( ) ;
112+ fileNames . forEach ( function ( current ) {
113+ var snippet = new Snippet ( JSON . parse ( storage . retrieveFile ( current . replace ( '.json' , '' ) ) ) ) ;
114+ items . push ( {
115+ value : snippet . getUID ( ) ,
116+ title : snippet . getTitle ( )
117+ } ) ;
118+ } ) ;
119+ const deletePrompt = Util . promptDelete ( {
120+ title : 'Delete a snippet' ,
121+ placeholder : 'Please choose a snippet...' ,
122+ btnconfirm : 'Delete' ,
123+ btncancel : 'Cancel' ,
124+ suremsg : 'Are you sure?' ,
125+ items : items
126+ } , function ( element ) {
127+ if ( Util . isset ( element , 'string' ) ) {
128+ var snippet = new Snippet ( JSON . parse ( storage . retrieveFile ( element ) ) ) ;
129+ if ( storage . deleteFile ( snippet . getUID ( ) ) ) {
130+ atom . notifications . addSuccess ( 'Snippet \'' + snippet . getTitle ( ) + '\' was successfully deleted.' , null ) ;
131+ } else {
132+ atom . notifications . addWarning ( 'Snippet \'' + snippet . getTitle ( ) + '\' could not be found in the local storage.' , null ) ;
133+ }
116134 }
117- }
118- } ) ;
135+ } ) ;
136+ } else {
137+ atom . notifications . addWarning ( 'No snippets found in the local storage directory.' , null ) ;
138+ }
119139 }
120140} ;
0 commit comments