1- import { App , PluginSettingTab , Setting , Notice } from 'obsidian' ;
21import { replaceDateInString } from '@utils/utils' ;
2+ import { App , Notice , PluginSettingTab , Setting } from 'obsidian' ;
33
4+ import { ServiceProvider } from '@src/constants' ;
5+ import languages from '@utils/languages' ;
6+ import { SettingServiceProviderModal } from '@views/setting_service_provider_modal' ;
47import BookSearchPlugin from '../main' ;
58import { FileNameFormatSuggest } from './suggesters/FileNameFormatSuggester' ;
6- import { FolderSuggest } from './suggesters/FolderSuggester' ;
79import { 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' ;
1111
1212const docUrl = 'https://github.com/anpigon/obsidian-book-search-plugin' ;
1313
@@ -59,28 +59,37 @@ export class BookSearchSettingTab extends PluginSettingTab {
5959 super ( app , plugin ) ;
6060 }
6161
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 ) ;
6466 }
6567
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+ }
7273
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+ }
7483
75- // New file location
84+ private createFileLocationSetting ( containerEl ) {
7685 new Setting ( containerEl )
7786 . setName ( 'New file location' )
7887 . setDesc ( 'New book notes will be placed here.' )
7988 . addSearch ( cb => {
8089 try {
8190 new FolderSuggest ( this . app , cb . inputEl ) ;
82- } catch {
83- // eslint-disable
91+ } catch ( e ) {
92+ console . error ( e ) ; // Improved error handling
8493 }
8594 cb . setPlaceholder ( 'Example: folder1/folder2' )
8695 . setValue ( this . plugin . settings . folder )
@@ -89,8 +98,9 @@ export class BookSearchSettingTab extends PluginSettingTab {
8998 this . plugin . saveSettings ( ) ;
9099 } ) ;
91100 } ) ;
101+ }
92102
93- // New File Name
103+ private createFileNameFormatSetting ( containerEl ) {
94104 const newFileNameHint = document . createDocumentFragment ( ) . createEl ( 'code' , {
95105 text : replaceDateInString ( this . plugin . settings . fileNameFormat ) || '{{title}} - {{author}}' ,
96106 } ) ;
@@ -101,8 +111,8 @@ export class BookSearchSettingTab extends PluginSettingTab {
101111 . addSearch ( cb => {
102112 try {
103113 new FileNameFormatSuggest ( this . app , cb . inputEl ) ;
104- } catch {
105- // eslint-disable
114+ } catch ( e ) {
115+ console . error ( e ) ; // Improved error handling
106116 }
107117 cb . setPlaceholder ( 'Example: {{title}} - {{author}}' )
108118 . setValue ( this . plugin . settings . fileNameFormat )
@@ -118,8 +128,36 @@ export class BookSearchSettingTab extends PluginSettingTab {
118128 cls : [ 'setting-item-description' , 'book-search-plugin__settings--new_file_name_hint' ] ,
119129 } )
120130 . 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+ }
121159
122- // Template file
160+ private createTemplateFileSetting ( containerEl : HTMLElement ) {
123161 const templateFileDesc = document . createDocumentFragment ( ) ;
124162 templateFileDesc . createDiv ( { text : 'Files will be available as templates.' } ) ;
125163 templateFileDesc . createEl ( 'a' , {
@@ -142,6 +180,15 @@ export class BookSearchSettingTab extends PluginSettingTab {
142180 this . plugin . saveSettings ( ) ;
143181 } ) ;
144182 } ) ;
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 ) ;
145192
146193 // Service Provider
147194 let serviceProviderExtraSettingButton : HTMLElement ;
@@ -163,7 +210,9 @@ export class BookSearchSettingTab extends PluginSettingTab {
163210 preferredLocaleDropdownSetting . settingEl . removeClass ( 'book-search-plugin__hide' ) ;
164211 }
165212 } ;
166- const toggleServiceProviderExtraSettings = ( serviceProvider : ServiceProvider = this . settings ?. serviceProvider ) => {
213+ const toggleServiceProviderExtraSettings = (
214+ serviceProvider : ServiceProvider = this . plugin . settings ?. serviceProvider ,
215+ ) => {
167216 if ( serviceProvider === ServiceProvider . naver ) {
168217 showServiceProviderExtraSettingButton ( ) ;
169218 hideServiceProviderExtraSettingDropdown ( ) ;
@@ -183,7 +232,7 @@ export class BookSearchSettingTab extends PluginSettingTab {
183232 dropDown . onChange ( async value => {
184233 const newValue = value as ServiceProvider ;
185234 toggleServiceProviderExtraSettings ( newValue ) ;
186- this . settings [ 'serviceProvider' ] = newValue ;
235+ this . plugin . settings [ 'serviceProvider' ] = newValue ;
187236 await this . plugin . saveSettings ( ) ;
188237 } ) ;
189238 } )
@@ -208,15 +257,15 @@ export class BookSearchSettingTab extends PluginSettingTab {
208257 const localeName = languages [ locale ] ;
209258 if ( localeName ) dropDown . addOption ( locale , localeName ) ;
210259 } ) ;
211- const setValue = this . settings . localePreference ;
260+ const setValue = this . plugin . settings . localePreference ;
212261 if ( setValue === 'default' ) {
213262 dropDown . setValue ( defaultLocale ) ;
214263 } else {
215264 dropDown . setValue ( setValue ) ;
216265 }
217266 dropDown . onChange ( async value => {
218267 const newValue = value ;
219- this . settings . localePreference = newValue ;
268+ this . plugin . settings . localePreference = newValue ;
220269 await this . plugin . saveSettings ( ) ;
221270 } ) ;
222271 } ) ;
@@ -269,45 +318,6 @@ export class BookSearchSettingTab extends PluginSettingTab {
269318 } ) ;
270319
271320 // 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 ) ;
296322 }
297323}
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