11import type JsEnginePlugin from 'jsEngine/main' ;
22import type { App } from 'obsidian' ;
3- import { normalizePath , PluginSettingTab , Setting } from 'obsidian' ;
3+ import { normalizePath , PluginSettingTab , Setting , TFile , TFolder } from 'obsidian' ;
44
55export interface JsEnginePluginSettings {
66 startupScriptsDirectory : string | undefined ;
@@ -32,6 +32,11 @@ export class JsEnginePluginSettingTab extends PluginSettingTab {
3232 new Setting ( containerEl )
3333 . setName ( 'JS snippets (loaded on startup)' )
3434 . setHeading ( )
35+ . addExtraButton ( el => {
36+ el . setTooltip ( 'Reload snippets' )
37+ . setIcon ( 'refresh-cw' )
38+ . onClick ( ( ) => this . display ( ) ) ;
39+ } )
3540 . addExtraButton ( el => {
3641 el . setTooltip ( 'Open snippets folder' )
3742 . setIcon ( 'folder-open' )
@@ -49,6 +54,23 @@ export class JsEnginePluginSettingTab extends PluginSettingTab {
4954 await this . plugin . saveSettings ( ) ;
5055 } ) ;
5156 } ) ;
57+
58+ const startupScriptsDirectory = this . app . vault . getFolderByPath ( settings . startupScriptsDirectory ?? '/' ) ;
59+ let startupScripts : TFile [ ] = [ ] ;
60+ if ( startupScriptsDirectory != null ) {
61+ startupScripts = this . listJSfilesInDirectory ( startupScriptsDirectory ) ;
62+ }
63+ if ( startupScripts . length == 0 ) {
64+ new Setting ( containerEl ) . setName ( 'No JS snippets found' ) . setDesc ( `JS snippets are stored in "vault/${ settings . startupScriptsDirectory ?? '' } "` ) ;
65+ }
66+ for ( const file of startupScripts ) {
67+ new Setting ( containerEl )
68+ . setName ( file . basename )
69+ . setDesc ( `Apply JS snippet from "vault/${ file . path } "` )
70+ . addToggle ( el => {
71+ el . setValue ( settings . startupScripts . contains ( file . path ) ) . onChange ( async val => this . toggleStartupScript ( file , val ) ) ;
72+ } ) ;
73+ }
5274 }
5375
5476 async openStartupScriptsDirectory ( ) : Promise < void > {
@@ -59,4 +81,20 @@ export class JsEnginePluginSettingTab extends PluginSettingTab {
5981 }
6082 this . app . openWithDefaultApp ( directory ) ;
6183 }
84+
85+ listJSfilesInDirectory ( directory : TFolder ) : TFile [ ] {
86+ const files = directory . children . filter ( el => el instanceof TFile ) ;
87+ const folders = directory . children . filter ( el => el instanceof TFolder ) ;
88+ return files . filter ( f => f . extension == 'js' ) . concat ( folders . flatMap ( dir => this . listJSfilesInDirectory ( dir ) ) ) ;
89+ }
90+
91+ async toggleStartupScript ( file : TFile , enable : boolean ) : Promise < void > {
92+ const settings = this . plugin . settings ;
93+ if ( enable ) {
94+ settings . startupScripts . push ( file . path ) ;
95+ } else {
96+ settings . startupScripts . remove ( file . path ) ;
97+ }
98+ await this . plugin . saveSettings ( ) ;
99+ }
62100}
0 commit comments