@@ -13,6 +13,11 @@ const Terminal = require('../lib/terminal');
1313const Documents = require ( '../lib/documents' ) ;
1414const highlighter = require ( '../lib/highlighter' ) ;
1515
16+ const {
17+ NEW_FILE ,
18+ CHANGE_FILE
19+ } = require ( '../constants/queued-action-types' ) ;
20+
1621// TODO: move somewhere else?
1722const red = '#da2100' ;
1823const green = '#159600' ;
@@ -56,8 +61,21 @@ function handlers(app, opts, done){
5661 userConfig
5762 } = app ;
5863
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+
5977 function newFile ( ) {
60- const { cwd, directory } = workspace . getState ( ) ;
78+ const { cwd, directory, isNew , content } = workspace . getState ( ) ;
6179
6280 // TODO: utility function
6381 const untitledNums = _ . reduce ( directory , function ( untitled , dirfile ) {
@@ -74,6 +92,13 @@ function handlers(app, opts, done){
7492
7593 const builtName = `untitled${ untitledLast + 1 } ` ;
7694
95+ // TODO: DRY this up
96+ if ( isNew && _ . trim ( content ) . length ) {
97+ store . dispatch ( creators . queueNewFile ( ) ) ;
98+ showSaveOverlay ( ) ;
99+ return ;
100+ }
101+
77102 workspace . newFile ( builtName , '' )
78103 . then ( ( ) => userConfig . set ( 'last-file' , builtName ) )
79104 . then ( function ( ) {
@@ -100,30 +125,22 @@ function handlers(app, opts, done){
100125 return ;
101126 }
102127
103- const { nextFile } = store . getState ( ) ;
104128 const { cwd, content } = workspace . getState ( ) ;
105129
106130 workspace . updateFilename ( filename )
107- . then ( function ( ) {
108- return workspace . saveFile ( filename , content ) ;
109- } )
131+ . then ( ( ) => workspace . saveFile ( filename , content ) )
110132 . then ( function ( ) {
111133 documents . swap ( path . join ( cwd , filename ) ) ;
112- if ( nextFile ) {
113- changeFile ( nextFile ) ;
114- }
134+ handleActionQueue ( ) ;
115135 } ) ;
116136 }
117137
118138 function dontSaveFile ( ) {
119139 const { nextFile } = store . getState ( ) ;
120140
141+ // TODO: handle error
121142 workspace . resetFile ( )
122- . then ( function ( ) {
123- if ( nextFile ) {
124- changeFile ( nextFile ) ;
125- }
126- } ) ;
143+ . then ( handleActionQueue ) ;
127144 }
128145
129146 function deleteFile ( filename ) {
@@ -147,19 +164,20 @@ function handlers(app, opts, done){
147164 cwd
148165 } = workspace . getState ( ) ;
149166
150- store . dispatch ( creators . resetFileQueue ( ) ) ;
151-
167+ // TODO: DRY this up
152168 if ( isNew && _ . trim ( content ) . length ) {
153- store . dispatch ( creators . queueFileChange ( filename ) ) ;
169+ store . dispatch ( creators . queueChangeFile ( filename ) ) ;
154170 showSaveOverlay ( ) ;
155171 return ;
156172 }
157173
158174 const doc = documents . swap ( path . join ( cwd , filename ) ) ;
159175 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+ } ) ;
163181 return ;
164182 }
165183
0 commit comments