@@ -10,12 +10,12 @@ declare module 'obsidian' {
10
10
}
11
11
12
12
export interface JsEnginePluginSettings {
13
- startupScriptsDirectory : string | undefined ;
13
+ startupScriptsDirectory : string ;
14
14
enabledStartupScripts : string [ ] ;
15
15
}
16
16
17
17
export const JS_ENGINE_DEFAULT_SETTINGS : JsEnginePluginSettings = {
18
- startupScriptsDirectory : undefined ,
18
+ startupScriptsDirectory : '' ,
19
19
enabledStartupScripts : [ ] ,
20
20
} ;
21
21
@@ -27,6 +27,11 @@ export class JsEnginePluginSettingTab extends PluginSettingTab {
27
27
this . plugin = plugin ;
28
28
}
29
29
30
+ private async listJSfilesInDirectory ( directory : string ) : Promise < string [ ] > {
31
+ const { adapter } = this . app . vault ;
32
+ return ( await adapter . list ( directory ) ) . files . filter ( file => file . endsWith ( '.js' ) ) ;
33
+ }
34
+
30
35
display ( ) : void {
31
36
const { containerEl } = this ;
32
37
const { settings } = this . plugin ;
@@ -50,14 +55,12 @@ export class JsEnginePluginSettingTab extends PluginSettingTab {
50
55
. addExtraButton ( el => {
51
56
el . setTooltip ( 'Reload snippets' )
52
57
. setIcon ( 'refresh-cw' )
53
- . onClick ( ( ) => {
54
- /* TODO */
55
- } ) ;
58
+ . onClick ( ( ) => this . display ( ) ) ;
56
59
} )
57
60
. addExtraButton ( el => {
58
61
el . setTooltip ( 'Open snippets folder' )
59
62
. setIcon ( 'folder-open' )
60
- . onClick ( ( ) => this . app . openWithDefaultApp ( settings . startupScriptsDirectory ?? '' ) ) ;
63
+ . onClick ( ( ) => this . app . openWithDefaultApp ( settings . startupScriptsDirectory ) ) ;
61
64
} ) ;
62
65
63
66
new Setting ( containerEl )
@@ -71,5 +74,25 @@ export class JsEnginePluginSettingTab extends PluginSettingTab {
71
74
await this . plugin . saveSettings ( ) ;
72
75
} ) ;
73
76
} ) ;
77
+
78
+ void this . listJSfilesInDirectory ( settings . startupScriptsDirectory ) . then ( fileList =>
79
+ fileList . forEach ( file => {
80
+ const fileName = file . split ( '/' ) . last ( ) ! ;
81
+ new Setting ( containerEl )
82
+ . setName ( fileName )
83
+ . setDesc ( `Apply JS snippet from "vault/${ file } "` )
84
+ . addToggle ( el => {
85
+ el . setValue ( settings . enabledStartupScripts . includes ( fileName ) )
86
+ . onChange ( async ( val : boolean ) => {
87
+ if ( val ) {
88
+ settings . enabledStartupScripts . push ( fileName ) ;
89
+ } else {
90
+ settings . enabledStartupScripts . remove ( fileName ) ;
91
+ }
92
+ await this . plugin . saveSettings ( ) ;
93
+ } ) ;
94
+ } ) ;
95
+ } ) ,
96
+ ) ;
74
97
}
75
98
}
0 commit comments