Skip to content

Commit

Permalink
feat: insert metadata to the note
Browse files Browse the repository at this point in the history
fix #12
  • Loading branch information
heycalmdown committed Jun 4, 2022
1 parent 84b82df commit 64b814a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
4 changes: 3 additions & 1 deletion src/book_search_modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ export class BookSearchModal extends Modal {
okBtnRef: ButtonComponent;
onSubmit: (err: Error, result?: Book[]) => void;

constructor(app: App, onSubmit?: (err: Error, result?: Book[]) => void) {
constructor(app: App, query: string, onSubmit?: (err: Error, result?: Book[]) => void) {
super(app);
this.query = query
this.onSubmit = onSubmit;
}

Expand Down Expand Up @@ -47,6 +48,7 @@ export class BookSearchModal extends Modal {

const placeholder = 'Search by keyword or ISBN';
const textComponent = new TextComponent(contentEl);
textComponent.setValue(this.query)
textComponent.inputEl.style.width = '100%';
textComponent
.setPlaceholder(placeholder ?? '')
Expand Down
63 changes: 46 additions & 17 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Notice, Plugin } from 'obsidian';
import { MarkdownView, Notice, Plugin } from 'obsidian';
import { BookSearchModal } from './book_search_modal';
import { BookSuggestModal } from './book_suggest_modal';
import { CursorJumper } from './editor/cursor_jumper';
Expand All @@ -7,6 +7,8 @@ import { Book } from './models/book.model';
import { BookSearchSettingTab, BookSearchPluginSettings, DEFAULT_SETTINGS } from './settings/settings';
import { replaceVariableSyntax, makeFileName, makeFrontMater } from './utils/utils';

type MetadataWriter = (book: Book, metadata: string) => Promise<void>;

export default class BookSearchPlugin extends Plugin {
settings: BookSearchPluginSettings;

Expand All @@ -25,14 +27,20 @@ export default class BookSearchPlugin extends Plugin {
callback: () => this.createNewBookNote(),
});

this.addCommand({
id: 'open-book-search-modal-to-insert',
name: 'Insert the metadata',
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 createNewBookNote(): Promise<void> {
async searchBookMetadata(query: string, writer: MetadataWriter): Promise<void> {
try {
// open modal for book search
const book = await this.openBookSearchModal();
const book = await this.openBookSearchModal(query);

let frontMatter = replaceVariableSyntax(book, this.settings.frontmatter);
if (this.settings.useDefaultFrontmatter) {
Expand All @@ -43,18 +51,7 @@ export default class BookSearchPlugin extends Plugin {
const content = replaceVariableSyntax(book, this.settings.content);
const fileContent = frontMatter ? `---\n${frontMatter}\n---\n${content}` : content;

const fileName = makeFileName(book);
const filePath = `${this.settings.folder.replace(/\/$/, '')}/${fileName}.md`;
const targetFile = await this.app.vault.create(filePath, fileContent);

// open file
const activeLeaf = this.app.workspace.getLeaf();
if (!activeLeaf) {
console.warn('No active leaf');
return;
}
await activeLeaf.openFile(targetFile, { state: { mode: 'source' } });
activeLeaf.setEphemeralState({ rename: 'all' });
await writer(book, fileContent);

// cursor focus
await new CursorJumper(this.app).jumpToNextCursorLocation();
Expand All @@ -68,9 +65,41 @@ export default class BookSearchPlugin extends Plugin {
}
}

async openBookSearchModal(): Promise<Book> {
async insertMetadata(): Promise<void> {
const markdownView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!markdownView) {
console.warn('Can not find an active markdown view');
return;
}
await this.searchBookMetadata(markdownView.file.basename, async (_, metadata) => {
if (!markdownView.editor) {
console.warn('Can not find editor from the active markdown view');
return;
}
markdownView.editor.replaceRange(metadata, { line: 0, ch: 0 });
});
}

async createNewBookNote(): Promise<void> {
await this.searchBookMetadata('', async (book, metadata) => {
const fileName = makeFileName(book);
const filePath = `${this.settings.folder.replace(/\/$/, '')}/${fileName}.md`;
const targetFile = await this.app.vault.create(filePath, metadata);

// open file
const activeLeaf = this.app.workspace.getLeaf();
if (!activeLeaf) {
console.warn('No active leaf');
return;
}
await activeLeaf.openFile(targetFile, { state: { mode: 'source' } });
activeLeaf.setEphemeralState({ rename: 'all' });
});
}

async openBookSearchModal(query = ''): Promise<Book> {
return new Promise((resolve, reject) => {
new BookSearchModal(this.app, (error, results) => {
new BookSearchModal(this.app, query, (error, results) => {
if (error) return reject(error);
new BookSuggestModal(this.app, results, (error2, selectedBook) => {
if (error2) return reject(error2);
Expand Down

0 comments on commit 64b814a

Please sign in to comment.