tmfelwu_obsidian-inbox/main.ts

108 lines
3.1 KiB
TypeScript
Raw Normal View History

2023-04-11 07:43:14 +00:00
import { Plugin, Modal } from 'obsidian';
2023-04-11 12:02:22 +00:00
2023-04-11 07:43:14 +00:00
class TextInputModal extends Modal {
private titleInput: HTMLInputElement;
private textarea: HTMLTextAreaElement;
constructor(app, private onSubmit: (title: string, value: string) => void) {
super(app);
}
onOpen() {
// Title Input
this.titleInput = document.createElement('input');
this.titleInput.type = 'text';
this.titleInput.placeholder = 'Enter note title';
this.contentEl.appendChild(this.titleInput);
this.textarea = document.createElement('textarea');
this.textarea.rows = 5;
this.textarea.placeholder = 'Enter to capture';
this.textarea.addEventListener('keydown', (event) => {
if (event.key === 'Enter' && event.ctrlKey) {
this.onSubmit(this.titleInput.value, this.textarea.value);
this.close();
} else if (event.key === 'Escape') {
this.close();
}
});
this.contentEl.appendChild(this.textarea);
}
onClose() {
this.titleInput.remove();
this.textarea.remove();
2023-04-11 07:43:14 +00:00
}
}
2023-04-11 07:43:14 +00:00
export default class QuickCaptureToNotePlugin extends Plugin {
2023-04-11 11:53:51 +00:00
async onload() {
this.addCommand({
id: 'quick-capture-to-note',
name: 'Quick Capture to Note',
callback: () => this.openCaptureModal(),
});
// This is the global listeneer for obsidian url Eg. obsidian://quick-capture
this.registerObsidianProtocolHandler('quick-capture', () => {
this.openCaptureModal();
});
}
//
openCaptureModal() {
const modal = new TextInputModal(this.app, async (title, content) => {
if (title && content) {
await this.createNoteWithTitle(title, content);
2023-04-11 07:43:14 +00:00
}
});
modal.open();
}
2023-04-11 07:43:14 +00:00
//
async createNoteWithTitle(title, content) {
try {
const noteExists = this.app.vault.getAbstractFileByPath(`${title}.md`);
if (!noteExists) {
await this.app.vault.create(`${title}.md`, content);
} else {
console.error('Note with the same title already exists');
2023-04-11 07:43:14 +00:00
}
} catch (error) {
console.error('Error while creating a new note:', error);
}
}
// To append data to an existing note called Inbox
// openCaptureModal() {
// const targetNoteTitle = 'Inbox'; // You can change this to any note title you prefer
// const targetNote = this.app.vault.getAbstractFileByPath(`${targetNoteTitle}.md`);
// const modal = new TextInputModal(this.app, async (capturedText) => {
// if (capturedText) {
// await this.appendTextToNote(targetNote, capturedText);
// }
// });
// modal.open();
// }
2023-04-11 07:43:14 +00:00
async appendTextToNote(note, text) {
if (note) {
const currentContent = await this.app.vault.read(note);
const newContent = currentContent + '\n' + text;
await this.app.vault.modify(note, newContent);
2023-04-11 07:43:14 +00:00
// Open the note and set the cursor position to the end of the note
const leaf = this.app.workspace.activeLeaf;
const editor = await leaf.openFile(note, { state: { mode: 'source' } });
editor.setCursor(editor.lineCount(), 0);
} else {
console.error('Target note not found');
}
}
}