Skip to content

Commit

Permalink
Merge pull request #21 from cloud-atlas-ai:default-new-task-to-inbox
Browse files Browse the repository at this point in the history
Improve dialog behavior when no category is selected
  • Loading branch information
muness authored Dec 26, 2023
2 parents 6928ecb + 18b50ab commit 776cb08
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 32 deletions.
78 changes: 49 additions & 29 deletions src/addTaskModal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
import { App, Modal, Setting, DropdownComponent, TextAreaComponent, FuzzySuggestModal, FuzzyMatch } from "obsidian";
import { App, Modal, Setting, DropdownComponent, TextAreaComponent, FuzzySuggestModal, FuzzyMatch, Notice } from "obsidian";
import { Category } from "./interfaces";

const inboxCategory: Category = {
_id: "__inbox-faux__", // Arbitrary unique ID for the Inbox faux category
title: "Inbox",
type: "faux",
updatedAt: 0,
parentId: "root",
startDate: "",
endDate: "",
note: "",
isRecurring: false,
priority: "",
deepLink: "",
dueDate: "",
done: false,
};

function getTitleWithParent(category: Category, categories: Category[]): string {
let parent = category.parentId;

Expand All @@ -22,23 +38,10 @@ function getTitleWithParent(category: Category, categories: Category[]): string

// Suggester Modal Class for Category Selection
class CategorySuggesterModal extends FuzzySuggestModal<Category> {
categories: Category[];

onChooseItem: (item: Category, _evt: MouseEvent | KeyboardEvent) => void;
getItems(): Category[] {
// Prepend a faux 'Inbox' category for matching purposes
const inboxCategory: Category = {
_id: "__inbox-faux__", // Arbitrary unique ID for the Inbox faux category
title: "Inbox",
type: "faux",
updatedAt: 0,
parentId: "root",
startDate: "",
endDate: "",
note: "",
isRecurring: false,
priority: "",
deepLink: "",
dueDate: "",
done: false,
};

// Include the Inbox at the beginning of the categories list
return [inboxCategory, ...this.categories];
Expand All @@ -49,14 +52,11 @@ class CategorySuggesterModal extends FuzzySuggestModal<Category> {
}
return getTitleWithParent(category, this.categories);
}
categories: Category[];
onChooseItem: (item: Category, _evt: MouseEvent | KeyboardEvent) => void;

constructor(app: App, categories: Category[], onChooseItem: (item: Category, _evt: MouseEvent | KeyboardEvent) => void) {
super(app);
this.categories = categories;
this.onChooseItem = onChooseItem;
this.setPlaceholder("Type to search for a Category");
}

onChooseSuggestion(item: FuzzyMatch<Category>, _evt: MouseEvent | KeyboardEvent): void {
Expand All @@ -75,7 +75,7 @@ export class AddTaskModal extends Modal {
this.categories = categories.sort((a, b) => {
return this.getFullPathToCategoryTitle(a, categories).localeCompare(this.getFullPathToCategoryTitle(b, categories));
});
this.result = { catId: '', task: '' }; // initialize result
this.result = { catId: inboxCategory._id, task: '' };
}

onOpen() {
Expand All @@ -88,8 +88,9 @@ export class AddTaskModal extends Modal {
.setName("Category")
.addText(text => {
categoryInput = text.inputEl;
text.setValue(inboxCategory.title);
this.result.catId = inboxCategory._id;
text.onChange(value => {
// Simulate a dropdown by creating a suggester modal
const suggester = new CategorySuggesterModal(this.app, this.categories, (item: Category) => {
categoryInput.value = item.title;
this.result.catId = item._id;
Expand All @@ -104,7 +105,9 @@ export class AddTaskModal extends Modal {

new Setting(contentEl)
.addTextArea((textArea: TextAreaComponent) => {
textArea.inputEl.style.minHeight = "5em"; // Increase the size of the text area
textArea.inputEl.style.minHeight = "5em";
textArea.inputEl.style.minWidth = "100%";

textArea.onChange((value: string) => {
this.result.task = value;
});
Expand All @@ -118,20 +121,37 @@ export class AddTaskModal extends Modal {
new Setting(contentEl)
.setDesc(shortcutsDesc);

// Submit Button
new Setting(contentEl)
.addButton((btn) =>
btn
.setButtonText("Submit")
.setButtonText("Add")
.setCta()
.onClick(() => {
this.close();
if (this.onSubmit && this.result.catId && this.result.task) {
this.onSubmit(this.result);
}
this.addTask();
}));

this.modalEl.addEventListener("keydown", (e: KeyboardEvent) => {
if (e.key === "Enter" && e.ctrlKey) {
e.preventDefault();
this.addTask();
}
});

}

private addTask() {
if (!this.result.task.trim()) {
new Notice('Please enter a task description.', 4000);
return;
}
this.close();
if (this.onSubmit && this.result.task) {
this.onSubmit(this.result);
}
}



private getShortcutsLink(): HTMLAnchorElement {
const a = document.createElement('a');
a.href = 'https://help.amazingmarvin.com/en/articles/1949399-using-shortcuts-while-creating-a-task';
Expand Down
4 changes: 1 addition & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,10 @@ export default class AmazingMarvinPlugin extends Plugin {
// Fetch categories first and make sure they are loaded
try {
const categories = await this.fetchTasksAndCategories(CONSTANTS.categoriesEndpoint);
console.log('Categories:', categories); // For debug purposes
// Ensure categories are fetched before initializing the modal
if (categories.length > 0) {
new AddTaskModal(this.app, categories, async (taskDetails: { catId: string, task: string }) => {
console.log('Task details:', taskDetails);

console.debug(`TaskModal result: ${taskDetails.catId} - ${taskDetails.task}}`);
this.addMarvinTask(taskDetails.catId, taskDetails.task)
.then(task => {
editor.replaceRange(`- [${task.done ? 'x' : ' '}] [⚓](${task.deepLink}) ${this.formatTaskDetails(task as Task, '')} ${task.title}`, editor.getCursor());
Expand Down

0 comments on commit 776cb08

Please sign in to comment.