@@ -238,6 +238,25 @@ const dummyFiles: TreeFile[] = [
238
238
}
239
239
] ;
240
240
241
+ const updateFile = ( file : File , change : FileVague ) : File => {
242
+ return Object . assign ( { } , file , change ) ;
243
+ }
244
+
245
+ const updateState = ( state : FileListState , change : FileListStateVague ) => {
246
+ return Object . assign ( { } , state , change ) ;
247
+ }
248
+
249
+ /**
250
+ * Returns new File object with sorted children inside
251
+ * 1. Directories go before files.
252
+ * 2. Alphabetic order.
253
+ */
254
+ const sortChildrenOfFile = ( state : FileListState , fileId : FileId ) : File => {
255
+ const file = state . files . get ( fileId ) ;
256
+ const children = state . files . get ( fileId ) . children . map ( fId => state . files . get ( fId ) ) . sort ( ( f1 , f2 ) => ( + ! ( f1 . isDirectory ) - + ! ( f2 . isDirectory ) ) || f1 . filename . localeCompare ( f2 . filename ) ) . map ( f => f . id ) ;
257
+ return updateFile ( file , { children } ) ;
258
+ }
259
+
241
260
let filesFiles = Map < FileId , File > ( ) . withMutations ( ( map ) => {
242
261
let nextId = 1 ;
243
262
@@ -247,6 +266,7 @@ let filesFiles = Map<FileId, File>().withMutations((map) => {
247
266
const children = treeFile . children ? treeFile . children . map ( mapTreeFileToFile ) : [ ] ;
248
267
const file : File = { id, isRoot : false , filename, isSelected, isDirectory, isExpanded, extension, content, children : treeFile . children ? treeFile . children . map ( mapTreeFileToFile ) : [ ] } ;
249
268
map . set ( id , file )
269
+ map . set ( id , sortChildrenOfFile ( { files : map , open : null , selected : null } , id ) ) // fixme
250
270
251
271
return id ;
252
272
}
@@ -262,19 +282,12 @@ let filesFiles = Map<FileId, File>().withMutations((map) => {
262
282
isExpanded : false ,
263
283
isSelected : false
264
284
} ) ;
285
+ map . set ( 0 , sortChildrenOfFile ( { files : map , open : null , selected : null } , 0 ) ) // fixme
265
286
} ) ;
266
287
267
288
const dummyState : FileListState = { files : filesFiles , open : null , selected : null } ;
268
289
( window as any ) . zzz = dummyState ;
269
290
270
- const updateFile = ( file : File , change : FileVague ) : File => {
271
- return Object . assign ( { } , file , change ) ;
272
- }
273
-
274
- const updateState = ( state : FileListState , change : FileListStateVague ) => {
275
- return Object . assign ( { } , state , change ) ;
276
- }
277
-
278
291
const reducer = ( state = dummyState , action : FileListActions | CodeChangedLocalAction | CodeChangedRemoteAction ) => {
279
292
console . log ( action ) ;
280
293
switch ( action . type ) {
0 commit comments