@@ -18,7 +18,8 @@ export const initialState = () => {
18
18
_id : r ,
19
19
children : [ b , a , c ] ,
20
20
fileType : 'folder' ,
21
- content : ''
21
+ content : '' ,
22
+ isFolderClosed : false //
22
23
} ,
23
24
{
24
25
name : 'sketch.js' ,
@@ -159,8 +160,22 @@ const files = (state, action) => {
159
160
return Object . assign ( { } , file , { blobURL : action . blobURL } ) ;
160
161
} ) ;
161
162
case ActionTypes . NEW_PROJECT :
163
+ action . files = action . files . map ( ( file ) => {
164
+ const corrospondingObj = state . find ( ( obj ) => obj . id === file . id ) ;
165
+ if ( corrospondingObj && corrospondingObj . fileType === 'folder' ) {
166
+ file . isFolderClosed = corrospondingObj . isFolderClosed ;
167
+ }
168
+ return file ;
169
+ } ) ;
162
170
return setFilePaths ( action . files ) ;
163
171
case ActionTypes . SET_PROJECT :
172
+ action . files = action . files . map ( ( file ) => {
173
+ const corrospondingObj = state . find ( ( obj ) => obj . id === file . id ) ;
174
+ if ( corrospondingObj && corrospondingObj . fileType === 'folder' ) {
175
+ file . isFolderClosed = corrospondingObj . isFolderClosed ;
176
+ }
177
+ return file ;
178
+ } ) ;
164
179
return setFilePaths ( action . files ) ;
165
180
case ActionTypes . RESET_PROJECT :
166
181
return initialState ( ) ;
@@ -175,19 +190,53 @@ const files = (state, action) => {
175
190
parentFile . name === 'root'
176
191
? ''
177
192
: `${ parentFile . filePath } /${ parentFile . name } ` ;
178
- const newState = [
179
- ...updateParent ( state , action ) ,
180
- {
181
- name : action . name ,
182
- id : action . id ,
183
- _id : action . _id ,
184
- content : action . content ,
185
- url : action . url ,
186
- children : action . children ,
187
- fileType : action . fileType || 'file' ,
188
- filePath
189
- }
190
- ] ;
193
+ // const newState = [
194
+ // ...updateParent(state, action),
195
+ // {
196
+ // name: action.name,
197
+ // id: action.id,
198
+ // _id: action._id,
199
+ // content: action.content,
200
+ // url: action.url,
201
+ // children: action.children,
202
+ // fileType: action.fileType || 'file',
203
+ // filePath,
204
+ // isExpanded: action.isExpanded
205
+ // }
206
+ // ];
207
+
208
+ let newState = null ;
209
+ if ( action . fileType === 'folder' ) {
210
+ newState = [
211
+ ...updateParent ( state , action ) ,
212
+ {
213
+ name : action . name ,
214
+ id : action . id ,
215
+ _id : action . _id ,
216
+ content : action . content ,
217
+ url : action . url ,
218
+ children : action . children ,
219
+ fileType : 'folder' ,
220
+ filePath,
221
+ isFolderClosed : false
222
+ }
223
+ ] ;
224
+ } else {
225
+ newState = [
226
+ ...updateParent ( state , action ) ,
227
+ {
228
+ name : action . name ,
229
+ id : action . id ,
230
+ _id : action . _id ,
231
+ content : action . content ,
232
+ url : action . url ,
233
+ children : action . children ,
234
+ fileType : 'file' ,
235
+ filePath
236
+ }
237
+ ] ;
238
+ }
239
+
191
240
return newState . map ( ( file ) => {
192
241
if ( file . id === action . parentId ) {
193
242
file . children = sortedChildrenId ( newState , file . children ) ;
@@ -236,14 +285,18 @@ const files = (state, action) => {
236
285
case ActionTypes . SHOW_FOLDER_CHILDREN :
237
286
return state . map ( ( file ) => {
238
287
if ( file . id === action . id ) {
239
- return Object . assign ( { } , file , { isFolderClosed : false } ) ;
288
+ return Object . assign ( { } , file , {
289
+ isFolderClosed : false
290
+ } ) ;
240
291
}
241
292
return file ;
242
293
} ) ;
243
294
case ActionTypes . HIDE_FOLDER_CHILDREN :
244
295
return state . map ( ( file ) => {
245
296
if ( file . id === action . id ) {
246
- return Object . assign ( { } , file , { isFolderClosed : true } ) ;
297
+ return Object . assign ( { } , file , {
298
+ isFolderClosed : true
299
+ } ) ;
247
300
}
248
301
return file ;
249
302
} ) ;
0 commit comments