@@ -52,6 +52,17 @@ class AppProject extends Tonic {
52
52
mouseMoveThreshold = 0
53
53
timeoutMouseMove = 0
54
54
55
+ constructor ( ) {
56
+ super ( )
57
+
58
+ window . addEventListener ( 'keyup' , e => {
59
+ if ( this . mouseIsDragging && e . key === 'Escape' ) {
60
+ this . resetMouse ( )
61
+ this . load ( )
62
+ }
63
+ } )
64
+ }
65
+
55
66
/**
56
67
* auto-sort="false"
57
68
*/
@@ -136,6 +147,8 @@ class AppProject extends Tonic {
136
147
if ( ! node ) this . getNodeFromElement ( el . parentElement )
137
148
if ( ! node ) return
138
149
150
+ if ( node . nonMovable ) return
151
+
139
152
this . removeAttribute ( 'dragging' )
140
153
this . mouseMoveThreshold = 0
141
154
@@ -211,6 +224,10 @@ class AppProject extends Tonic {
211
224
}
212
225
213
226
mousemove ( e ) {
227
+ if ( Tonic . match ( e . target , '[data-event="rename"]' ) ) {
228
+ return // renaming, can't drag
229
+ }
230
+
214
231
if ( this . mouseIsDown ) {
215
232
++ this . mouseMoveThreshold
216
233
@@ -278,13 +295,26 @@ class AppProject extends Tonic {
278
295
// Rename a node in the tree
279
296
//
280
297
if ( Tonic . match ( e . target , '[data-event="rename"]' ) ) {
298
+ if ( e . key === 'Escape' ) {
299
+ this . load ( )
300
+ }
301
+
281
302
if ( e . key === 'Enter' ) {
282
303
const value = e . target . value . trim ( )
283
304
if ( this . getNodeByProperty ( 'id' , value ) ) return
284
305
285
- const newId = path . join ( path . dirname ( node . id ) , value )
306
+ const dirname = path . dirname ( node . id ) . replace ( / % 2 0 / g, ' ' )
307
+ const newId = path . join ( dirname , value )
286
308
await fs . promises . rename ( node . id , newId )
309
+
310
+ const coTabs = document . querySelector ( 'editor-tabs' )
311
+ if ( coTabs && coTabs . tab . id === node . id ) {
312
+ coTabs . rename ( { oldId : coTabs . tab . id , newId, label : value } )
313
+ }
314
+
287
315
node . label = value
316
+ node . id = newId
317
+
288
318
this . load ( )
289
319
}
290
320
}
@@ -293,6 +323,10 @@ class AppProject extends Tonic {
293
323
async dblclick ( e ) {
294
324
this . resetMouse ( )
295
325
326
+ if ( Tonic . match ( e . target , '[data-event="rename"]' ) ) {
327
+ return // already renaming
328
+ }
329
+
296
330
const el = Tonic . match ( e . target , '[data-path]' )
297
331
if ( ! el ) return
298
332
@@ -475,6 +509,8 @@ class AppProject extends Tonic {
475
509
476
510
this . state . currentProject = projectNode . id
477
511
512
+ if ( node . isDirectory ) return
513
+
478
514
const coImagePreview = document . querySelector ( 'view-image-preview' )
479
515
const coHome = document . querySelector ( 'view-home' )
480
516
@@ -578,8 +614,9 @@ class AppProject extends Tonic {
578
614
}
579
615
580
616
async load ( ) {
581
- const oldState = this . state . tree
582
- const oldChild = this . getNodeByProperty ( 'id' , 'home' , oldState )
617
+ const app = this . props . parent
618
+ let oldState = this . state . tree
619
+ let oldChild = this . getNodeByProperty ( 'id' , 'home' , oldState )
583
620
584
621
const tree = {
585
622
id : 'root' ,
@@ -594,6 +631,7 @@ class AppProject extends Tonic {
594
631
isDirectory : false ,
595
632
icon : 'home-icon' ,
596
633
label : 'Home' ,
634
+ nonMovable : true ,
597
635
children : [ ]
598
636
} )
599
637
@@ -619,7 +657,6 @@ class AppProject extends Tonic {
619
657
state : oldChild ?. state ?? 0 ,
620
658
isDirectory : entry . isDirectory ( ) ,
621
659
label : entry . name ,
622
- data : { } ,
623
660
mime : await lookup ( path . extname ( entry . name ) ) ,
624
661
children : [ ]
625
662
}
@@ -650,12 +687,49 @@ class AppProject extends Tonic {
650
687
}
651
688
}
652
689
653
- try {
654
- await readDir ( path . join ( path . DATA , 'projects' ) , tree )
655
- this . state . tree = tree
656
- } catch ( err ) {
657
- console . error ( 'Error initiating read directory operation:' , err )
658
- return
690
+ const settingsId = path . join ( path . DATA , 'settings.json' )
691
+ oldChild = this . getNodeByProperty ( 'id' , settingsId , oldState )
692
+
693
+ tree . children . push ( {
694
+ id : settingsId ,
695
+ parent : tree ,
696
+ selected : oldChild ?. selected ?? 0 ,
697
+ state : oldChild ?. state ?? 0 ,
698
+ isDirectory : false ,
699
+ icon : 'settings-icon' ,
700
+ label : 'Settings' ,
701
+ nonMovable : true ,
702
+ children : [ ]
703
+ } )
704
+
705
+ const { data : dataProjects } = await app . db . projects . readAll ( )
706
+
707
+ for ( const [ projectId , project ] of dataProjects . entries ( ) ) {
708
+ const oldChild = this . getNodeByProperty ( 'id' , project . path , oldState )
709
+
710
+ const node = {
711
+ parent : tree ,
712
+ selected : oldChild ?. selected ?? 0 ,
713
+ state : oldChild ?. state ?? 0 ,
714
+ id : project . path ,
715
+ label : project . label ,
716
+ isDirectory : true ,
717
+ nonMovable : true ,
718
+ icon : 'package' ,
719
+ children : [ ]
720
+ }
721
+
722
+ tree . children . push ( node )
723
+
724
+ if ( project . path ) {
725
+ try {
726
+ await readDir ( project . path , node )
727
+ this . state . tree = tree
728
+ } catch ( err ) {
729
+ console . error ( 'Error initiating read directory operation:' , err )
730
+ return
731
+ }
732
+ }
659
733
}
660
734
661
735
this . reRender ( )
0 commit comments