1
1
import type JsEnginePlugin from 'jsEngine/main' ;
2
2
import { StartupScriptsModal } from 'jsEngine/settings/StartupScriptModal' ;
3
3
import type { App } from 'obsidian' ;
4
- import { normalizePath , PluginSettingTab , Setting } from 'obsidian' ;
4
+ import { normalizePath , PluginSettingTab , Setting , TFolder , TFile } from 'obsidian' ;
5
5
6
6
export interface JsEnginePluginSettings {
7
7
startupScriptsDirectory : string | undefined ;
@@ -41,6 +41,11 @@ export class JsEnginePluginSettingTab extends PluginSettingTab {
41
41
new Setting ( containerEl )
42
42
. setName ( 'JS snippets (loaded on startup)' )
43
43
. setHeading ( )
44
+ . addExtraButton ( el => {
45
+ el . setTooltip ( 'Reload snippets' )
46
+ . setIcon ( 'refresh-cw' )
47
+ . onClick ( ( ) => this . display ( ) ) ;
48
+ } )
44
49
. addExtraButton ( el => {
45
50
el . setTooltip ( 'Open snippets folder' )
46
51
. setIcon ( 'folder-open' )
@@ -62,12 +67,34 @@ export class JsEnginePluginSettingTab extends PluginSettingTab {
62
67
await this . plugin . saveSettings ( ) ;
63
68
} ) ;
64
69
} ) ;
70
+
71
+ const startupScriptsDirectory = this . app . vault . getFolderByPath ( this . getStartupScriptsDirectory ( ) ) ;
72
+ let startupScripts : TFile [ ] = [ ] ;
73
+ if ( startupScriptsDirectory != null ) {
74
+ startupScripts = this . listJSfilesInDirectory ( startupScriptsDirectory ) ;
75
+ }
76
+ if ( startupScriptsDirectory == null || startupScripts . length == 0 ) {
77
+ new Setting ( containerEl ) . setName ( 'No JS snippets found' ) . setDesc ( `JS snippets are stored in "vault/${ this . getStartupScriptsDirectory ( ) } "` ) ;
78
+ } else {
79
+ for ( const file of startupScripts ) {
80
+ new Setting ( containerEl )
81
+ . setName ( file . basename )
82
+ . setDesc ( `Apply JS snippet from "vault/${ file . path } "` )
83
+ . addToggle ( el => {
84
+ el . setValue ( settings . startupScripts . contains ( file . path ) ) . onChange ( val => this . toggleStartupScript ( file , val ) ) ;
85
+ } ) ;
86
+ }
87
+ }
65
88
}
66
89
67
90
getDefaultStartupScriptsDirectory ( ) : string {
68
91
return normalizePath ( '' . concat ( this . app . vault . configDir , '/scripts' ) ) ;
69
92
}
70
93
94
+ getStartupScriptsDirectory ( ) : string {
95
+ return this . plugin . settings . startupScriptsDirectory ?? this . getDefaultStartupScriptsDirectory ( ) ;
96
+ }
97
+
71
98
async openStartupScriptsDirectory ( ) : Promise < void > {
72
99
const vault = this . app . vault ;
73
100
const directory = this . plugin . settings . startupScriptsDirectory ?? this . getDefaultStartupScriptsDirectory ( ) ;
@@ -76,4 +103,19 @@ export class JsEnginePluginSettingTab extends PluginSettingTab {
76
103
}
77
104
this . app . openWithDefaultApp ( directory ) ;
78
105
}
106
+
107
+ listJSfilesInDirectory ( directory : TFolder ) : TFile [ ] {
108
+ const files = directory . children . filter ( el => el instanceof TFile ) ;
109
+ const folders = directory . children . filter ( el => el instanceof TFolder ) ;
110
+ return files ; // recurse the folders? .concat(folders.flatMap(dir => this.listJSfilesInDirectory(dir)))
111
+ }
112
+
113
+ toggleStartupScript ( file : TFile , enable : boolean ) : void {
114
+ const settings = this . plugin . settings ;
115
+ if ( enable ) {
116
+ settings . startupScripts . push ( file . path ) ;
117
+ } else {
118
+ settings . startupScripts . remove ( file . path ) ;
119
+ }
120
+ }
79
121
}
0 commit comments