From 1d39cb5dfaa34a284145cb1de5a01e1b3a292997 Mon Sep 17 00:00:00 2001 From: anpigon Date: Wed, 17 Aug 2022 22:30:03 +0900 Subject: [PATCH] refactor: main.ts --- src/main.ts | 60 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/src/main.ts b/src/main.ts index c04156c..b287839 100644 --- a/src/main.ts +++ b/src/main.ts @@ -24,34 +24,33 @@ export default class BookSearchPlugin extends Plugin { this.addCommand({ id: 'open-book-search-modal', name: 'Create new book note', - callback: () => this.createNewBookNote(), + callback: this.createNewBookNote, }); this.addCommand({ id: 'open-book-search-modal-to-insert', name: 'Insert the metadata', - callback: () => this.insertMetadata(), + callback: this.insertMetadata, }); // This adds a settings tab so the user can configure various aspects of the plugin this.addSettingTab(new BookSearchSettingTab(this.app, this)); } - async searchBookMetadata(query?: string): Promise { + showNotice(message: unknown) { try { - // open modal for book search - const searchedBooks = await this.openBookSearchModal(query); - return await this.openBookSuggestModal(searchedBooks); - } catch (err) { - console.warn(err); - try { - new Notice(err.toString()); - } catch { - // eslint-disable - } + new Notice(message?.toString()); + } catch { + // eslint-disable } } + // open modal for book search + async searchBookMetadata(query?: string): Promise { + const searchedBooks = await this.openBookSearchModal(query); + return await this.openBookSuggestModal(searchedBooks); + } + async getRenderedContents(book: Book) { const { templateFile, @@ -94,8 +93,13 @@ export default class BookSearchPlugin extends Plugin { return; } - const renderedContents = await this.getRenderedContents(book); - markdownView.editor.replaceRange(renderedContents, { line: 0, ch: 0 }); + try { + const renderedContents = await this.getRenderedContents(book); + markdownView.editor.replaceRange(renderedContents, { line: 0, ch: 0 }); + } catch (err) { + console.warn(err); + this.showNotice(err); + } } async createNewBookNote(): Promise { @@ -108,15 +112,23 @@ export default class BookSearchPlugin extends Plugin { return; } - const renderedContents = await this.getRenderedContents(book); - const fileName = makeFileName(book, this.settings.fileNameFormat); - const filePath = path.join(this.settings.folder, fileName); - const targetFile = await this.app.vault.create(filePath, renderedContents); - await activeLeaf.openFile(targetFile, { state: { mode: 'source' } }); - activeLeaf.setEphemeralState({ rename: 'all' }); - - // cursor focus - await new CursorJumper(this.app).jumpToNextCursorLocation(); + try { + const renderedContents = await this.getRenderedContents(book); + + // TODO: If the same file exists, it asks if you want to overwrite it. + // create new File + const fileName = makeFileName(book, this.settings.fileNameFormat); + const filePath = path.join(this.settings.folder, fileName); + const targetFile = await this.app.vault.create(filePath, renderedContents); + await activeLeaf.openFile(targetFile, { state: { mode: 'source' } }); + activeLeaf.setEphemeralState({ rename: 'all' }); + + // cursor focus + await new CursorJumper(this.app).jumpToNextCursorLocation(); + } catch (err) { + console.warn(err); + this.showNotice(err); + } } async openBookSearchModal(query = ''): Promise {