@@ -10,12 +10,12 @@ declare module 'obsidian' {
1010}
1111
1212export interface JsEnginePluginSettings {
13- startupScriptsDirectory : string | undefined ;
13+ startupScriptsDirectory : string ;
1414 enabledStartupScripts : string [ ] ;
1515}
1616
1717export const JS_ENGINE_DEFAULT_SETTINGS : JsEnginePluginSettings = {
18- startupScriptsDirectory : undefined ,
18+ startupScriptsDirectory : '' ,
1919 enabledStartupScripts : [ ] ,
2020} ;
2121
@@ -27,6 +27,11 @@ export class JsEnginePluginSettingTab extends PluginSettingTab {
2727 this . plugin = plugin ;
2828 }
2929
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+
3035 display ( ) : void {
3136 const { containerEl } = this ;
3237 const { settings } = this . plugin ;
@@ -50,14 +55,12 @@ export class JsEnginePluginSettingTab extends PluginSettingTab {
5055 . addExtraButton ( el => {
5156 el . setTooltip ( 'Reload snippets' )
5257 . setIcon ( 'refresh-cw' )
53- . onClick ( ( ) => {
54- /* TODO */
55- } ) ;
58+ . onClick ( ( ) => this . display ( ) ) ;
5659 } )
5760 . addExtraButton ( el => {
5861 el . setTooltip ( 'Open snippets folder' )
5962 . setIcon ( 'folder-open' )
60- . onClick ( ( ) => this . app . openWithDefaultApp ( settings . startupScriptsDirectory ?? '' ) ) ;
63+ . onClick ( ( ) => this . app . openWithDefaultApp ( settings . startupScriptsDirectory ) ) ;
6164 } ) ;
6265
6366 new Setting ( containerEl )
@@ -71,5 +74,25 @@ export class JsEnginePluginSettingTab extends PluginSettingTab {
7174 await this . plugin . saveSettings ( ) ;
7275 } ) ;
7376 } ) ;
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+ ) ;
7497 }
7598}
0 commit comments