1
1
import { Path } from "src/utils/path" ;
2
2
import { InvioSettingTab } from "src/settings" ;
3
3
import { HeadingCache , TAbstractFile , TFile , TFolder } from "obsidian" ;
4
+ import { log } from '../moreOnLog' ;
4
5
5
6
export const enum TreeItemType
6
7
{
@@ -10,6 +11,9 @@ export const enum TreeItemType
10
11
None = "none"
11
12
}
12
13
14
+ export type TFileWithMeta = TFile & { sort ?: number } ;
15
+ export type TAbstractFileWithMeta = TAbstractFile & { sort ?: number } ;
16
+
13
17
function isHeadingCache ( obj : any ) : obj is HeadingCache
14
18
{
15
19
return obj != undefined && "heading" in obj && "level" in obj ;
@@ -26,6 +30,7 @@ export class LinkTree
26
30
public href : string | undefined = undefined ;
27
31
public root : LinkTree | undefined = undefined ;
28
32
public isRoot : boolean = false ;
33
+ public sort : number = 0 ;
29
34
30
35
/**
31
36
* Sets the source of this tree item. This also sets the type, title and href based on the source.
@@ -71,11 +76,12 @@ export class LinkTree
71
76
return this . #type;
72
77
}
73
78
74
- constructor ( source : TAbstractFile | HeadingCache | undefined , parent : LinkTree | undefined , depth : number , root : LinkTree | undefined = undefined )
79
+ constructor ( source : TAbstractFile | HeadingCache | undefined , parent : LinkTree | undefined , depth : number , root : LinkTree | undefined = undefined , sort : number | undefined = 0 )
75
80
{
76
81
this . source = source ;
77
82
this . parent = parent ;
78
83
this . depth = depth ;
84
+ this . sort = sort ;
79
85
80
86
if ( root == undefined ) this . root = this . findRoot ( ) ;
81
87
}
@@ -99,15 +105,15 @@ export class LinkTree
99
105
* Creates a tree from a list of files.
100
106
* @returns The root of the tree.
101
107
*/
102
- public static fromFiles ( files : TFile [ ] ) : LinkTree
108
+ public static fromFiles ( files : TFileWithMeta [ ] ) : LinkTree
103
109
{
104
110
let root = new LinkTree ( undefined , undefined , 0 ) ;
105
111
106
112
for ( let file of files )
107
113
{
108
- let pathSections : TAbstractFile [ ] = [ ] ;
114
+ let pathSections : TAbstractFileWithMeta [ ] = [ ] ;
109
115
110
- let parentFile : TAbstractFile = file ;
116
+ let parentFile : TAbstractFileWithMeta = file ;
111
117
while ( parentFile != undefined )
112
118
{
113
119
pathSections . push ( parentFile ) ;
@@ -124,7 +130,7 @@ export class LinkTree
124
130
let child = parent . children . find ( c => c . title == section . name && c . type == sectionType && c . depth == i ) ;
125
131
if ( child == undefined )
126
132
{
127
- child = new LinkTree ( section , parent , i , root ) ;
133
+ child = new LinkTree ( section , parent , i , root , section . sort ) ;
128
134
parent . children . push ( child ) ;
129
135
}
130
136
parent = child ;
@@ -194,12 +200,26 @@ export class LinkTree
194
200
return list ;
195
201
}
196
202
203
+ public sortMeta ( reverse : boolean = false )
204
+ {
205
+ this . children . sort ( ( a , b ) => {
206
+ const aSort = a . sort || Number . MAX_SAFE_INTEGER ;
207
+ const bSort = b . sort || Number . MAX_SAFE_INTEGER ;
208
+ return ( reverse ? ( bSort >= aSort ? 1 : - 1 ) : ( aSort >= bSort ? 1 : - 1 ) )
209
+ } ) ;
210
+ for ( let child of this . children )
211
+ {
212
+ child . sortMeta ( reverse ) ;
213
+ }
214
+ }
215
+
216
+
197
217
public sortAlphabetically ( reverse : boolean = false )
198
218
{
199
219
this . children . sort ( ( a , b ) => reverse ? b . title . localeCompare ( a . title ) : a . title . localeCompare ( b . title ) ) ;
200
220
for ( let child of this . children )
201
221
{
202
- child . sortAlphabetically ( ) ;
222
+ child . sortAlphabetically ( reverse ) ;
203
223
}
204
224
}
205
225
@@ -338,14 +358,15 @@ export class GlobalDataGenerator
338
358
// size is the depth of the file from the root
339
359
// the list will be sorted first by size, then by title
340
360
// the list will include folders and files
341
- public static getFileTree ( exportedFiles : TFile [ ] | undefined = undefined ) : LinkTree
361
+ public static getFileTree ( exportedFiles : TFileWithMeta [ ] | undefined = undefined ) : LinkTree
342
362
{
343
363
if ( this . fileTreeCache != undefined ) return this . fileTreeCache ;
344
364
if ( exportedFiles == undefined ) return new LinkTree ( undefined , undefined , 0 ) ;
345
365
346
366
let fileTree = LinkTree . fromFiles ( exportedFiles ) ;
347
367
fileTree . sortAlphabetically ( ) ;
348
368
fileTree . sortByIsFolder ( true ) ;
369
+ fileTree . sortMeta ( ) ;
349
370
if ( InvioSettingTab . settings . makeNamesWebStyle ) fileTree . makeLinksWebStyle ( ) ;
350
371
351
372
this . fileTreeCache = fileTree ;
0 commit comments