@@ -228,9 +228,10 @@ export interface IExplorerService extends Component<IExplorer> {
228
228
getRootFolderByRootId ( id : number ) : ITreeNodeItem | undefined ;
229
229
addRootFolder ( folder ?: ITreeNodeItem | ITreeNodeItem [ ] ) : void ;
230
230
removeRootFolder ( id : number ) : void ;
231
+ setActive ( id : number ) : void ;
231
232
updateFile ( file : ITreeNodeItem , callback ?: Function ) : void ;
232
233
newFile ( id ?: number , callback ?: Function ) : void ;
233
- newFolder ( id : number , callback ?: Function ) : void ;
234
+ newFolder ( id ? : number , callback ?: Function ) : void ;
234
235
rename ( id : number , callback ?: Function ) : void ;
235
236
delete ( id : number , callback ?: Function ) : void ;
236
237
onDropTree ( treeData : ITreeNodeItem [ ] ) : void ;
@@ -319,6 +320,23 @@ export class ExplorerService
319
320
} ;
320
321
}
321
322
323
+ private createTargetNodeById (
324
+ id : number ,
325
+ treeInstance ,
326
+ extra ?: ITreeNodeItem
327
+ ) {
328
+ const currentIndex = treeInstance . getIndex ( id ) ;
329
+ // If the node type of the current id is a file, insert it at the parent node above it
330
+ if ( currentIndex ?. node ?. fileType === FileTypes . file ) {
331
+ treeInstance . prepend (
332
+ new TreeNodeModel ( extra ) ,
333
+ currentIndex ?. parent
334
+ ) ;
335
+ } else {
336
+ treeInstance . append ( new TreeNodeModel ( extra ) , id ) ;
337
+ }
338
+ }
339
+
322
340
public getRootFolderIndexByRootId ( id : number ) : number | undefined {
323
341
return this . state . folderTree ?. data ! . findIndex (
324
342
( folder ) => folder . id === id
@@ -365,6 +383,16 @@ export class ExplorerService
365
383
} ) ;
366
384
}
367
385
386
+ public setActive ( id : number ) {
387
+ const { folderTree } = this . state ;
388
+ const { currentRootFolder } = this . getCurrentRootFolderAndIndex ( id ) ;
389
+ const tree = new TreeView ( currentRootFolder ) ;
390
+ const currentNode = tree . get ( id ) ;
391
+ this . setState ( {
392
+ folderTree : { ...folderTree , current : currentNode } ,
393
+ } ) ;
394
+ }
395
+
368
396
public updateFile ( file , callback ) {
369
397
const { folderTree } = this . state ;
370
398
const { id, name, fileType } = file ;
@@ -421,14 +449,14 @@ export class ExplorerService
421
449
if ( callback ) callback ( ) ;
422
450
}
423
451
424
- public newFile ( parentId : number , callback ?: Function ) {
452
+ public newFile ( id : number , callback ?: Function ) {
425
453
const { folderTree } = this . state ;
426
454
const cloneData : ITreeNodeItem [ ] = folderTree ?. data || [ ] ;
427
455
const { currentRootFolder, index } = this . getCurrentRootFolderAndIndex (
428
- parentId
456
+ id
429
457
) ;
430
458
const tree = new TreeView ( currentRootFolder ) ;
431
- if ( ! parentId ) {
459
+ if ( ! id ) {
432
460
const tabData = {
433
461
id : `${ Math . random ( ) * 10 + 1 } ` ,
434
462
name : `Untitled` ,
@@ -438,33 +466,27 @@ export class ExplorerService
438
466
} ;
439
467
editorService . open ( tabData ) ;
440
468
}
441
- tree . append (
442
- new TreeNodeModel ( {
443
- modify : true ,
444
- } ) ,
445
- parentId
446
- ) ;
469
+ this . createTargetNodeById ( id , tree , {
470
+ modify : true ,
471
+ } ) ;
447
472
if ( index > - 1 ) cloneData [ index ] = tree . obj ;
448
473
this . setState ( {
449
474
folderTree : { ...folderTree , data : cloneData } ,
450
475
} ) ;
451
476
if ( callback ) callback ( ) ;
452
477
}
453
478
454
- public newFolder ( parentId , callback : Function ) {
479
+ public newFolder ( id , callback : Function ) {
455
480
const { folderTree } = this . state ;
456
481
const cloneData : ITreeNodeItem [ ] = folderTree ?. data || [ ] ;
457
482
const { currentRootFolder, index } = this . getCurrentRootFolderAndIndex (
458
- parentId
483
+ id
459
484
) ;
460
485
const tree = new TreeView ( currentRootFolder ) ;
461
- tree . append (
462
- new TreeNodeModel ( {
463
- fileType : FileTypes . folder as FileType ,
464
- modify : true ,
465
- } ) ,
466
- parentId
467
- ) ;
486
+ this . createTargetNodeById ( id , tree , {
487
+ fileType : FileTypes . folder as FileType ,
488
+ modify : true ,
489
+ } ) ;
468
490
if ( index > - 1 ) cloneData [ index ] = tree . obj ;
469
491
this . setState ( {
470
492
folderTree : { ...folderTree , data : cloneData } ,
0 commit comments