@@ -13,6 +13,11 @@ const Terminal = require('../lib/terminal');
13
13
const Documents = require ( '../lib/documents' ) ;
14
14
const highlighter = require ( '../lib/highlighter' ) ;
15
15
16
+ const {
17
+ NEW_FILE ,
18
+ CHANGE_FILE
19
+ } = require ( '../constants/queued-action-types' ) ;
20
+
16
21
// TODO: move somewhere else?
17
22
const red = '#da2100' ;
18
23
const green = '#159600' ;
@@ -56,8 +61,21 @@ function handlers(app, opts, done){
56
61
userConfig
57
62
} = app ;
58
63
64
+ function handleActionQueue ( ) {
65
+ const { nextAction, nextFile } = store . getState ( ) ;
66
+
67
+ store . dispatch ( creators . resetActionQueue ( ) ) ;
68
+
69
+ switch ( nextAction ) {
70
+ case NEW_FILE :
71
+ return newFile ( ) ;
72
+ case CHANGE_FILE :
73
+ return changeFile ( nextFile ) ;
74
+ }
75
+ }
76
+
59
77
function newFile ( ) {
60
- const { cwd, directory } = workspace . getState ( ) ;
78
+ const { cwd, directory, isNew , content } = workspace . getState ( ) ;
61
79
62
80
// TODO: utility function
63
81
const untitledNums = _ . reduce ( directory , function ( untitled , dirfile ) {
@@ -74,6 +92,13 @@ function handlers(app, opts, done){
74
92
75
93
const builtName = `untitled${ untitledLast + 1 } ` ;
76
94
95
+ // TODO: DRY this up
96
+ if ( isNew && _ . trim ( content ) . length ) {
97
+ store . dispatch ( creators . queueNewFile ( ) ) ;
98
+ showSaveOverlay ( ) ;
99
+ return ;
100
+ }
101
+
77
102
workspace . newFile ( builtName , '' )
78
103
. then ( ( ) => userConfig . set ( 'last-file' , builtName ) )
79
104
. then ( function ( ) {
@@ -100,30 +125,22 @@ function handlers(app, opts, done){
100
125
return ;
101
126
}
102
127
103
- const { nextFile } = store . getState ( ) ;
104
128
const { cwd, content } = workspace . getState ( ) ;
105
129
106
130
workspace . updateFilename ( filename )
107
- . then ( function ( ) {
108
- return workspace . saveFile ( filename , content ) ;
109
- } )
131
+ . then ( ( ) => workspace . saveFile ( filename , content ) )
110
132
. then ( function ( ) {
111
133
documents . swap ( path . join ( cwd , filename ) ) ;
112
- if ( nextFile ) {
113
- changeFile ( nextFile ) ;
114
- }
134
+ handleActionQueue ( ) ;
115
135
} ) ;
116
136
}
117
137
118
138
function dontSaveFile ( ) {
119
139
const { nextFile } = store . getState ( ) ;
120
140
141
+ // TODO: handle error
121
142
workspace . resetFile ( )
122
- . then ( function ( ) {
123
- if ( nextFile ) {
124
- changeFile ( nextFile ) ;
125
- }
126
- } ) ;
143
+ . then ( handleActionQueue ) ;
127
144
}
128
145
129
146
function deleteFile ( filename ) {
@@ -147,19 +164,20 @@ function handlers(app, opts, done){
147
164
cwd
148
165
} = workspace . getState ( ) ;
149
166
150
- store . dispatch ( creators . resetFileQueue ( ) ) ;
151
-
167
+ // TODO: DRY this up
152
168
if ( isNew && _ . trim ( content ) . length ) {
153
- store . dispatch ( creators . queueFileChange ( filename ) ) ;
169
+ store . dispatch ( creators . queueChangeFile ( filename ) ) ;
154
170
showSaveOverlay ( ) ;
155
171
return ;
156
172
}
157
173
158
174
const doc = documents . swap ( path . join ( cwd , filename ) ) ;
159
175
if ( doc ) {
160
- workspace . updateFilename ( filename ) ;
161
- workspace . updateContent ( doc . getValue ( ) ) ;
162
- documents . focus ( ) ;
176
+ workspace . changeFile ( filename )
177
+ . then ( ( ) => workspace . updateContent ( doc . getValue ( ) ) )
178
+ . then ( function ( ) {
179
+ documents . focus ( ) ;
180
+ } ) ;
163
181
return ;
164
182
}
165
183
0 commit comments