1
- import { App , PluginSettingTab , Setting , Notice } from 'obsidian' ;
2
1
import { replaceDateInString } from '@utils/utils' ;
2
+ import { App , Notice , PluginSettingTab , Setting } from 'obsidian' ;
3
3
4
+ import { ServiceProvider } from '@src/constants' ;
5
+ import languages from '@utils/languages' ;
6
+ import { SettingServiceProviderModal } from '@views/setting_service_provider_modal' ;
4
7
import BookSearchPlugin from '../main' ;
5
8
import { FileNameFormatSuggest } from './suggesters/FileNameFormatSuggester' ;
6
- import { FolderSuggest } from './suggesters/FolderSuggester' ;
7
9
import { FileSuggest } from './suggesters/FileSuggester' ;
8
- import { ServiceProvider } from '@src/constants' ;
9
- import { SettingServiceProviderModal } from '@views/setting_service_provider_modal' ;
10
- import languages from '@utils/languages' ;
10
+ import { FolderSuggest } from './suggesters/FolderSuggester' ;
11
11
12
12
const docUrl = 'https://github.com/anpigon/obsidian-book-search-plugin' ;
13
13
@@ -59,28 +59,37 @@ export class BookSearchSettingTab extends PluginSettingTab {
59
59
super ( app , plugin ) ;
60
60
}
61
61
62
- get settings ( ) {
63
- return this . plugin . settings ;
62
+ private createGeneralSettings ( containerEl ) {
63
+ this . createHeader ( 'General Settings' , containerEl ) ;
64
+ this . createFileLocationSetting ( containerEl ) ;
65
+ this . createFileNameFormatSetting ( containerEl ) ;
64
66
}
65
67
66
- display ( ) : void {
67
- const { containerEl } = this ;
68
-
69
- containerEl . empty ( ) ;
70
-
71
- containerEl . classList . add ( 'book-search-plugin__settings' ) ;
68
+ private createHeader ( title , containerEl ) {
69
+ const header = document . createDocumentFragment ( ) ;
70
+ header . createEl ( 'h2' , { text : title } ) ;
71
+ return new Setting ( containerEl ) . setHeading ( ) . setName ( header ) ;
72
+ }
72
73
73
- createHeader ( containerEl , 'General Settings' ) ;
74
+ private createFoldingHeader ( containerEl : HTMLElement , title : string , formatterSettingsChildren : Setting [ ] ) {
75
+ return this . createHeader ( title , containerEl ) . addToggle ( toggle => {
76
+ toggle . onChange ( checked => {
77
+ formatterSettingsChildren . forEach ( ( { settingEl } ) => {
78
+ settingEl . toggleClass ( 'book-search-plugin__show' , checked ) ;
79
+ } ) ;
80
+ } ) ;
81
+ } ) ;
82
+ }
74
83
75
- // New file location
84
+ private createFileLocationSetting ( containerEl ) {
76
85
new Setting ( containerEl )
77
86
. setName ( 'New file location' )
78
87
. setDesc ( 'New book notes will be placed here.' )
79
88
. addSearch ( cb => {
80
89
try {
81
90
new FolderSuggest ( this . app , cb . inputEl ) ;
82
- } catch {
83
- // eslint-disable
91
+ } catch ( e ) {
92
+ console . error ( e ) ; // Improved error handling
84
93
}
85
94
cb . setPlaceholder ( 'Example: folder1/folder2' )
86
95
. setValue ( this . plugin . settings . folder )
@@ -89,8 +98,9 @@ export class BookSearchSettingTab extends PluginSettingTab {
89
98
this . plugin . saveSettings ( ) ;
90
99
} ) ;
91
100
} ) ;
101
+ }
92
102
93
- // New File Name
103
+ private createFileNameFormatSetting ( containerEl ) {
94
104
const newFileNameHint = document . createDocumentFragment ( ) . createEl ( 'code' , {
95
105
text : replaceDateInString ( this . plugin . settings . fileNameFormat ) || '{{title}} - {{author}}' ,
96
106
} ) ;
@@ -101,8 +111,8 @@ export class BookSearchSettingTab extends PluginSettingTab {
101
111
. addSearch ( cb => {
102
112
try {
103
113
new FileNameFormatSuggest ( this . app , cb . inputEl ) ;
104
- } catch {
105
- // eslint-disable
114
+ } catch ( e ) {
115
+ console . error ( e ) ; // Improved error handling
106
116
}
107
117
cb . setPlaceholder ( 'Example: {{title}} - {{author}}' )
108
118
. setValue ( this . plugin . settings . fileNameFormat )
@@ -118,8 +128,36 @@ export class BookSearchSettingTab extends PluginSettingTab {
118
128
cls : [ 'setting-item-description' , 'book-search-plugin__settings--new_file_name_hint' ] ,
119
129
} )
120
130
. append ( newFileNameHint ) ;
131
+ }
132
+
133
+ private createAPIKeySettings ( containerEl : HTMLElement ) {
134
+ const APISettingsChildren : Setting [ ] = [ ] ;
135
+ this . createFoldingHeader ( containerEl , 'Google API Settings' , APISettingsChildren ) ;
136
+ let tempKeyValue = '' ;
137
+ APISettingsChildren . push (
138
+ new Setting ( containerEl )
139
+ . setClass ( 'book-search-plugin__hide' )
140
+ . setName ( 'Google Book API Key' )
141
+ . setDesc (
142
+ 'Add your Books API key. **WARNING** please use this field after you must understand Google Cloud API, such as API key security.' ,
143
+ )
144
+ . addText ( text => {
145
+ text . inputEl . type = 'password' ;
146
+ text . setValue ( this . plugin . settings . apiKey ) . onChange ( async value => {
147
+ tempKeyValue = value ;
148
+ } ) ;
149
+ } )
150
+ . addButton ( button => {
151
+ button . setButtonText ( 'Save Key' ) . onClick ( async ( ) => {
152
+ this . plugin . settings . apiKey = tempKeyValue ;
153
+ await this . plugin . saveSettings ( ) ;
154
+ new Notice ( 'Apikey Saved' ) ;
155
+ } ) ;
156
+ } ) ,
157
+ ) ;
158
+ }
121
159
122
- // Template file
160
+ private createTemplateFileSetting ( containerEl : HTMLElement ) {
123
161
const templateFileDesc = document . createDocumentFragment ( ) ;
124
162
templateFileDesc . createDiv ( { text : 'Files will be available as templates.' } ) ;
125
163
templateFileDesc . createEl ( 'a' , {
@@ -142,6 +180,15 @@ export class BookSearchSettingTab extends PluginSettingTab {
142
180
this . plugin . saveSettings ( ) ;
143
181
} ) ;
144
182
} ) ;
183
+ }
184
+
185
+ display ( ) : void {
186
+ const { containerEl } = this ;
187
+ containerEl . empty ( ) ;
188
+ containerEl . classList . add ( 'book-search-plugin__settings' ) ;
189
+
190
+ this . createGeneralSettings ( containerEl ) ;
191
+ this . createTemplateFileSetting ( containerEl ) ;
145
192
146
193
// Service Provider
147
194
let serviceProviderExtraSettingButton : HTMLElement ;
@@ -163,7 +210,9 @@ export class BookSearchSettingTab extends PluginSettingTab {
163
210
preferredLocaleDropdownSetting . settingEl . removeClass ( 'book-search-plugin__hide' ) ;
164
211
}
165
212
} ;
166
- const toggleServiceProviderExtraSettings = ( serviceProvider : ServiceProvider = this . settings ?. serviceProvider ) => {
213
+ const toggleServiceProviderExtraSettings = (
214
+ serviceProvider : ServiceProvider = this . plugin . settings ?. serviceProvider ,
215
+ ) => {
167
216
if ( serviceProvider === ServiceProvider . naver ) {
168
217
showServiceProviderExtraSettingButton ( ) ;
169
218
hideServiceProviderExtraSettingDropdown ( ) ;
@@ -183,7 +232,7 @@ export class BookSearchSettingTab extends PluginSettingTab {
183
232
dropDown . onChange ( async value => {
184
233
const newValue = value as ServiceProvider ;
185
234
toggleServiceProviderExtraSettings ( newValue ) ;
186
- this . settings [ 'serviceProvider' ] = newValue ;
235
+ this . plugin . settings [ 'serviceProvider' ] = newValue ;
187
236
await this . plugin . saveSettings ( ) ;
188
237
} ) ;
189
238
} )
@@ -208,15 +257,15 @@ export class BookSearchSettingTab extends PluginSettingTab {
208
257
const localeName = languages [ locale ] ;
209
258
if ( localeName ) dropDown . addOption ( locale , localeName ) ;
210
259
} ) ;
211
- const setValue = this . settings . localePreference ;
260
+ const setValue = this . plugin . settings . localePreference ;
212
261
if ( setValue === 'default' ) {
213
262
dropDown . setValue ( defaultLocale ) ;
214
263
} else {
215
264
dropDown . setValue ( setValue ) ;
216
265
}
217
266
dropDown . onChange ( async value => {
218
267
const newValue = value ;
219
- this . settings . localePreference = newValue ;
268
+ this . plugin . settings . localePreference = newValue ;
220
269
await this . plugin . saveSettings ( ) ;
221
270
} ) ;
222
271
} ) ;
@@ -269,45 +318,6 @@ export class BookSearchSettingTab extends PluginSettingTab {
269
318
} ) ;
270
319
271
320
// API Settings
272
- const APISettingsChildren : Setting [ ] = [ ] ;
273
- createFoldingHeader ( containerEl , 'Google API Settings' , APISettingsChildren ) ;
274
- let tempKeyValue = '' ;
275
- APISettingsChildren . push (
276
- new Setting ( containerEl )
277
- . setClass ( 'book-search-plugin__hide' )
278
- . setName ( 'Google Book API Key' )
279
- . setDesc (
280
- 'Add your Books API key. **WARNING** please use this field after you must understand Google Cloud API, such as API key security.' ,
281
- )
282
- . addText ( text => {
283
- text . inputEl . type = 'password' ;
284
- text . setValue ( this . plugin . settings . apiKey ) . onChange ( async value => {
285
- tempKeyValue = value ;
286
- } ) ;
287
- } )
288
- . addButton ( button => {
289
- button . setButtonText ( 'Save Key' ) . onClick ( async ( ) => {
290
- this . plugin . settings . apiKey = tempKeyValue ;
291
- await this . plugin . saveSettings ( ) ;
292
- new Notice ( 'Apikey Saved' ) ;
293
- } ) ;
294
- } ) ,
295
- ) ;
321
+ this . createAPIKeySettings ( containerEl ) ;
296
322
}
297
323
}
298
-
299
- function createHeader ( containerEl : HTMLElement , title : string ) {
300
- const titleEl = document . createDocumentFragment ( ) ;
301
- titleEl . createEl ( 'h2' , { text : title } ) ;
302
- return new Setting ( containerEl ) . setHeading ( ) . setName ( titleEl ) ;
303
- }
304
-
305
- function createFoldingHeader ( containerEl : HTMLElement , title : string , formatterSettingsChildren : Setting [ ] ) {
306
- return createHeader ( containerEl , title ) . addToggle ( toggle => {
307
- toggle . onChange ( checked => {
308
- formatterSettingsChildren . forEach ( ( { settingEl } ) => {
309
- settingEl . toggleClass ( 'book-search-plugin__show' , checked ) ;
310
- } ) ;
311
- } ) ;
312
- } ) ;
313
- }
0 commit comments