diff --git a/src/apis/base_api.ts b/src/apis/base_api.ts index df3ffc7..4438ef5 100644 --- a/src/apis/base_api.ts +++ b/src/apis/base_api.ts @@ -6,7 +6,7 @@ import { GoogleBooksApi } from './google_books_api'; import { NaverBooksApi } from './naver_books_api'; export interface BaseBooksApiImpl { - getByQuery(query: string, options?: any): Promise; + getByQuery(query: string, options?: Record): Promise; } class ConfigurationError extends Error { diff --git a/src/apis/google_books_api.ts b/src/apis/google_books_api.ts index e8115bf..8ef5f7e 100644 --- a/src/apis/google_books_api.ts +++ b/src/apis/google_books_api.ts @@ -12,7 +12,7 @@ export class GoogleBooksApi implements BaseBooksApiImpl { return this.localePreference === 'default' ? window.moment.locale() : this.localePreference; } - private buildSearchParams(query: string, options?: any): Record { + private buildSearchParams(query: string, options?: Record): Record { const params: Record = { q: query, maxResults: GoogleBooksApi.MAX_RESULTS, @@ -27,7 +27,7 @@ export class GoogleBooksApi implements BaseBooksApiImpl { return params; } - async getByQuery(query: string, options?: any): Promise { + async getByQuery(query: string, options?: Record): Promise { try { const params = this.buildSearchParams(query, options); const searchResults = await apiGet('https://www.googleapis.com/books/v1/volumes', params); diff --git a/src/settings/settings.ts b/src/settings/settings.ts index 0916f06..0f49ddb 100644 --- a/src/settings/settings.ts +++ b/src/settings/settings.ts @@ -1,13 +1,13 @@ -import { App, PluginSettingTab, Setting, Notice } from 'obsidian'; import { replaceDateInString } from '@utils/utils'; +import { App, Notice, PluginSettingTab, Setting } from 'obsidian'; +import { ServiceProvider } from '@src/constants'; +import languages from '@utils/languages'; +import { SettingServiceProviderModal } from '@views/setting_service_provider_modal'; import BookSearchPlugin from '../main'; import { FileNameFormatSuggest } from './suggesters/FileNameFormatSuggester'; -import { FolderSuggest } from './suggesters/FolderSuggester'; import { FileSuggest } from './suggesters/FileSuggester'; -import { ServiceProvider } from '@src/constants'; -import { SettingServiceProviderModal } from '@views/setting_service_provider_modal'; -import languages from '@utils/languages'; +import { FolderSuggest } from './suggesters/FolderSuggester'; const docUrl = 'https://github.com/anpigon/obsidian-book-search-plugin'; @@ -59,28 +59,37 @@ export class BookSearchSettingTab extends PluginSettingTab { super(app, plugin); } - get settings() { - return this.plugin.settings; + private createGeneralSettings(containerEl) { + this.createHeader('General Settings', containerEl); + this.createFileLocationSetting(containerEl); + this.createFileNameFormatSetting(containerEl); } - display(): void { - const { containerEl } = this; - - containerEl.empty(); - - containerEl.classList.add('book-search-plugin__settings'); + private createHeader(title, containerEl) { + const header = document.createDocumentFragment(); + header.createEl('h2', { text: title }); + return new Setting(containerEl).setHeading().setName(header); + } - createHeader(containerEl, 'General Settings'); + private createFoldingHeader(containerEl: HTMLElement, title: string, formatterSettingsChildren: Setting[]) { + return this.createHeader(title, containerEl).addToggle(toggle => { + toggle.onChange(checked => { + formatterSettingsChildren.forEach(({ settingEl }) => { + settingEl.toggleClass('book-search-plugin__show', checked); + }); + }); + }); + } - // New file location + private createFileLocationSetting(containerEl) { new Setting(containerEl) .setName('New file location') .setDesc('New book notes will be placed here.') .addSearch(cb => { try { new FolderSuggest(this.app, cb.inputEl); - } catch { - // eslint-disable + } catch (e) { + console.error(e); // Improved error handling } cb.setPlaceholder('Example: folder1/folder2') .setValue(this.plugin.settings.folder) @@ -89,8 +98,9 @@ export class BookSearchSettingTab extends PluginSettingTab { this.plugin.saveSettings(); }); }); + } - // New File Name + private createFileNameFormatSetting(containerEl) { const newFileNameHint = document.createDocumentFragment().createEl('code', { text: replaceDateInString(this.plugin.settings.fileNameFormat) || '{{title}} - {{author}}', }); @@ -101,8 +111,8 @@ export class BookSearchSettingTab extends PluginSettingTab { .addSearch(cb => { try { new FileNameFormatSuggest(this.app, cb.inputEl); - } catch { - // eslint-disable + } catch (e) { + console.error(e); // Improved error handling } cb.setPlaceholder('Example: {{title}} - {{author}}') .setValue(this.plugin.settings.fileNameFormat) @@ -118,8 +128,36 @@ export class BookSearchSettingTab extends PluginSettingTab { cls: ['setting-item-description', 'book-search-plugin__settings--new_file_name_hint'], }) .append(newFileNameHint); + } + + private createAPIKeySettings(containerEl: HTMLElement) { + const APISettingsChildren: Setting[] = []; + this.createFoldingHeader(containerEl, 'Google API Settings', APISettingsChildren); + let tempKeyValue = ''; + APISettingsChildren.push( + new Setting(containerEl) + .setClass('book-search-plugin__hide') + .setName('Google Book API Key') + .setDesc( + 'Add your Books API key. **WARNING** please use this field after you must understand Google Cloud API, such as API key security.', + ) + .addText(text => { + text.inputEl.type = 'password'; + text.setValue(this.plugin.settings.apiKey).onChange(async value => { + tempKeyValue = value; + }); + }) + .addButton(button => { + button.setButtonText('Save Key').onClick(async () => { + this.plugin.settings.apiKey = tempKeyValue; + await this.plugin.saveSettings(); + new Notice('Apikey Saved'); + }); + }), + ); + } - // Template file + private createTemplateFileSetting(containerEl: HTMLElement) { const templateFileDesc = document.createDocumentFragment(); templateFileDesc.createDiv({ text: 'Files will be available as templates.' }); templateFileDesc.createEl('a', { @@ -142,6 +180,15 @@ export class BookSearchSettingTab extends PluginSettingTab { this.plugin.saveSettings(); }); }); + } + + display(): void { + const { containerEl } = this; + containerEl.empty(); + containerEl.classList.add('book-search-plugin__settings'); + + this.createGeneralSettings(containerEl); + this.createTemplateFileSetting(containerEl); // Service Provider let serviceProviderExtraSettingButton: HTMLElement; @@ -163,7 +210,9 @@ export class BookSearchSettingTab extends PluginSettingTab { preferredLocaleDropdownSetting.settingEl.removeClass('book-search-plugin__hide'); } }; - const toggleServiceProviderExtraSettings = (serviceProvider: ServiceProvider = this.settings?.serviceProvider) => { + const toggleServiceProviderExtraSettings = ( + serviceProvider: ServiceProvider = this.plugin.settings?.serviceProvider, + ) => { if (serviceProvider === ServiceProvider.naver) { showServiceProviderExtraSettingButton(); hideServiceProviderExtraSettingDropdown(); @@ -183,7 +232,7 @@ export class BookSearchSettingTab extends PluginSettingTab { dropDown.onChange(async value => { const newValue = value as ServiceProvider; toggleServiceProviderExtraSettings(newValue); - this.settings['serviceProvider'] = newValue; + this.plugin.settings['serviceProvider'] = newValue; await this.plugin.saveSettings(); }); }) @@ -208,7 +257,7 @@ export class BookSearchSettingTab extends PluginSettingTab { const localeName = languages[locale]; if (localeName) dropDown.addOption(locale, localeName); }); - const setValue = this.settings.localePreference; + const setValue = this.plugin.settings.localePreference; if (setValue === 'default') { dropDown.setValue(defaultLocale); } else { @@ -216,7 +265,7 @@ export class BookSearchSettingTab extends PluginSettingTab { } dropDown.onChange(async value => { const newValue = value; - this.settings.localePreference = newValue; + this.plugin.settings.localePreference = newValue; await this.plugin.saveSettings(); }); }); @@ -269,45 +318,6 @@ export class BookSearchSettingTab extends PluginSettingTab { }); // API Settings - const APISettingsChildren: Setting[] = []; - createFoldingHeader(containerEl, 'Google API Settings', APISettingsChildren); - let tempKeyValue = ''; - APISettingsChildren.push( - new Setting(containerEl) - .setClass('book-search-plugin__hide') - .setName('Google Book API Key') - .setDesc( - 'Add your Books API key. **WARNING** please use this field after you must understand Google Cloud API, such as API key security.', - ) - .addText(text => { - text.inputEl.type = 'password'; - text.setValue(this.plugin.settings.apiKey).onChange(async value => { - tempKeyValue = value; - }); - }) - .addButton(button => { - button.setButtonText('Save Key').onClick(async () => { - this.plugin.settings.apiKey = tempKeyValue; - await this.plugin.saveSettings(); - new Notice('Apikey Saved'); - }); - }), - ); + this.createAPIKeySettings(containerEl); } } - -function createHeader(containerEl: HTMLElement, title: string) { - const titleEl = document.createDocumentFragment(); - titleEl.createEl('h2', { text: title }); - return new Setting(containerEl).setHeading().setName(titleEl); -} - -function createFoldingHeader(containerEl: HTMLElement, title: string, formatterSettingsChildren: Setting[]) { - return createHeader(containerEl, title).addToggle(toggle => { - toggle.onChange(checked => { - formatterSettingsChildren.forEach(({ settingEl }) => { - settingEl.toggleClass('book-search-plugin__show', checked); - }); - }); - }); -}