Skip to content

Commit

Permalink
feat: Support parameters for URL scheme (closes #7)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonknittel committed Jan 20, 2024
1 parent 4dd4bca commit eecdc55
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 28 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ Faster creation of tasks in Obsidian.
- Create a new task using `Ctrl + P`
- Create a new task using the ribbon menu
- Create a new task using the [URL scheme standard](https://help.obsidian.md/Extending+Obsidian/Obsidian+URI)
- [obsidian://create-task](obsidian://create-task)
- This will open Obsidian and open the Create Task modal: [obsidian://create-task](obsidian://create-task)
- You can use parameters to pre-fill the individual modal inputs
- This pre-fills the Target note input: `note-path=Unsorted%20TODOs.md`
- This pre-fills the Task description input: `task-description=Do%20stuff`
- This pre-fills the Due date input: `due-date=tomorrow`
- This will skip the modal and immediately create the task in the specified note: `create=true`

## Screenshots

Expand Down
30 changes: 25 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default class CreateTask extends Plugin {
// This creates an icon in the left ribbon.
this.addRibbonIcon("check-square", "Create Task", () => {
// Called when the user clicks the icon.
new CreateTaskModal(this.app, this).open();
new CreateTaskModal(this.app, this, undefined).open();
});

// This adds a complex command that can check whether the current state of the app allows execution of the command
Expand All @@ -32,15 +32,31 @@ export default class CreateTask extends Plugin {
// If checking is false, then we want to actually perform the operation.
if (checking) return true;

new CreateTaskModal(this.app, this).open();
new CreateTaskModal(this.app, this, undefined).open();
},
});

// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new CreateTaskSettingTab(this.app, this));

this.registerObsidianProtocolHandler("create-task", () => {
new CreateTaskModal(this.app, this).open();
this.registerObsidianProtocolHandler("create-task", (params) => {
if (
params["create"] === "true" &&
params["note-path"] &&
this.app.vault.getAbstractFileByPath(params["note-path"])
) {
this.createTask(
params["note-path"],
params["task-description"],
params["due-date"],
);
} else {
new CreateTaskModal(this.app, this, {
notePath: params["note-path"],
taskDescription: params["task-description"],
dueDate: params["due-date"],
}).open();
}
});
}

Expand All @@ -56,7 +72,7 @@ export default class CreateTask extends Plugin {
}

async createTask(
customNoteIndex: "default" | number,
customNoteIndex: "default" | string | number,
taskDescription: string,
dueDate: string,
) {
Expand All @@ -66,6 +82,10 @@ export default class CreateTask extends Plugin {
if (customNoteIndex === "default") {
path = this.settings.defaultNote;

str = this.compileLine(undefined, taskDescription, dueDate) + "\n";
} else if (typeof customNoteIndex === "string") {
path = customNoteIndex;

str = this.compileLine(undefined, taskDescription, dueDate) + "\n";
} else {
const customNote = this.settings.customNotes[customNoteIndex];
Expand Down
85 changes: 63 additions & 22 deletions src/modal.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,76 @@
import { App, Modal, Notice, Setting } from "obsidian";
import CreateTask from "./main";

type InitialValues =
| {
notePath: string | undefined;
taskDescription: string | undefined;
dueDate: string | undefined;
}
| undefined;

export class CreateTaskModal extends Modal {
plugin: CreateTask;

customNoteIndex: "default" | number;
notePath: string;
taskDescription: string;
dueDate: string;

previewElDescription: HTMLElement;
previewElLine: HTMLElement;

constructor(app: App, plugin: CreateTask) {
initialValues: InitialValues;

constructor(app: App, plugin: CreateTask, initialValues: InitialValues) {
super(app);
this.plugin = plugin;
this.customNoteIndex = "default";

if (initialValues?.notePath) {
this.notePath = initialValues.notePath;
} else {
this.customNoteIndex = "default";
}

this.taskDescription = initialValues?.taskDescription || "";
this.dueDate = initialValues?.dueDate || "";
}

onOpen() {
const { contentEl } = this;

contentEl.createEl("h1", { text: "Create Task" });
contentEl.createEl("h1", { text: "Create task" });

new Setting(contentEl)
.setName("📁 Target note")
.setDesc("Corresponds to the custom notes added in the settings")
.addDropdown((dropdown) => {
dropdown.addOption("default", "Default");
dropdown.setValue("default");

for (const [
index,
customNote,
] of this.plugin.settings.customNotes.entries()) {
dropdown.addOption(index.toString(), customNote.name);
}

dropdown.onChange((value) => {
this.customNoteIndex = value === "default" ? value : parseInt(value);
this.updatePreview();
if (this.notePath) {
new Setting(contentEl)
.setName("📁 Target note")
.setDesc("Corresponds to the custom notes added in the settings")
.addText((text) => {
text.setValue(this.notePath);
text.setDisabled(true);
});
});
} else {
new Setting(contentEl)
.setName("📁 Target note")
.setDesc("Corresponds to the custom notes added in the settings")
.addDropdown((dropdown) => {
dropdown.addOption("default", "Default");
dropdown.setValue("default");

for (const [
index,
customNote,
] of this.plugin.settings.customNotes.entries()) {
dropdown.addOption(index.toString(), customNote.name);
}

dropdown.onChange((value) => {
this.customNoteIndex =
value === "default" ? value : parseInt(value);
this.updatePreview();
});
});
}

new Setting(contentEl)
.setName("🖊️ Task description")
Expand All @@ -52,6 +82,7 @@ export class CreateTaskModal extends Modal {
});

text.setPlaceholder("My task");
text.setValue(this.taskDescription);
});

new Setting(contentEl)
Expand All @@ -62,6 +93,8 @@ export class CreateTaskModal extends Modal {
this.dueDate = value;
this.updatePreview();
});

text.setValue(this.dueDate);
});

new Setting(contentEl).addButton((btn) =>
Expand Down Expand Up @@ -96,7 +129,15 @@ export class CreateTaskModal extends Modal {
}

updatePreview() {
if (this.customNoteIndex === "default") {
if (this.notePath) {
this.previewElDescription.setText(
`The following line will get added to: ${this.notePath}`,
);

this.previewElLine.setText(
this.plugin.compileLine(undefined, this.taskDescription, this.dueDate),
);
} else if (this.customNoteIndex === "default") {
this.previewElDescription.setText(
`The following line will get added to: ${this.plugin.settings.defaultNote}`,
);
Expand Down

0 comments on commit eecdc55

Please sign in to comment.