Skip to content

Commit

Permalink
feat: add confirm note creation setting
Browse files Browse the repository at this point in the history
  • Loading branch information
srg-kostyrko committed Sep 10, 2024
1 parent c299cba commit 792901f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 6 deletions.
24 changes: 24 additions & 0 deletions src/components/modals/ConfirmNoteCreation.modal.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<script setup lang="ts">
import ObsidianSetting from "../obsidian/ObsidianSetting.vue";
import ObsidianButton from "../obsidian/ObsidianButton.vue";
defineProps<{ noteName: string; journalName: string }>();
defineEmits<{
(event: "confirm", value: boolean): void;
(event: "close"): void;
}>();
</script>

<template>
<ObsidianSetting name="About to create new note"></ObsidianSetting>
<ObsidianSetting name="Journal">
{{ journalName }}
</ObsidianSetting>
<ObsidianSetting name="Note">
{{ noteName }}
</ObsidianSetting>
<ObsidianSetting>
<ObsidianButton @click="$emit('confirm', false)">Cancel</ObsidianButton>
<ObsidianButton cta @click="$emit('confirm', true)">Confirm</ObsidianButton>
</ObsidianSetting>
</template>
2 changes: 1 addition & 1 deletion src/components/modals/vue-modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ export class VueModal extends Modal {
});
}
this._vueApp = createApp(this.component, {
...this.componentProps,
onClose: () => {
this.close();
},
...this.componentProps,
});
this._vueApp.mount(this.contentEl);
}
Expand Down
42 changes: 37 additions & 5 deletions src/journals/journal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@ import {
} from "../constants";
import { FixedInterval } from "./fixed-interval";
import { date_from_string, today } from "../calendar";
import { VueModal } from "@/components/modals/vue-modal";
import ConfirmNoteCreationModal from "@/components/modals/ConfirmNoteCreation.modal.vue";

export class Journal {
readonly name$: ComputedRef<string>;
#config: ComputedRef<JournalSettings>;
#intervalResolver: IntervalResolver;

constructor(public readonly id: string) {
this.#config = computed(() => journals$.value[id]);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
this.name$ = computed(() => this.#config.value.name);
this.#intervalResolver = new FixedInterval(
id,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
computed(() => this.#config.value.write),
);
}
Expand Down Expand Up @@ -102,6 +106,7 @@ export class Journal {

async open(metadata: JournalMetadata): Promise<void> {
const file = await this.#ensureNote(metadata);
if (!file) return;
await this.#openFile(file);
}

Expand All @@ -111,15 +116,19 @@ export class Journal {
await leaf.openFile(file, { active: true });
}

async #ensureNote(metadata: JournalMetadata): Promise<TFile> {
async #ensureNote(metadata: JournalMetadata): Promise<TFile | null> {
const filePath = this.#getNotePath(metadata);
let file = app$.value.vault.getAbstractFileByPath(filePath);
if (!file) {
const templateContext = this.#getTemplateContext(metadata);
const noteName = replaceTemplateVariables(this.#config.value.nameTemplate, templateContext);
if (this.#config.value.confirmCreation && !(await this.#confirmNoteCreation(noteName))) {
return null;
}
await ensureFolderExists(app$.value, filePath);
file = await app$.value.vault.create(filePath, "");
if (!(file instanceof TFile)) throw new Error("File is not a TFile");
const templateContext = this.#getTemplateContext(metadata);
const noteName = replaceTemplateVariables(this.#config.value.nameTemplate, templateContext);

templateContext.note_name = { type: "string", value: noteName };
const content = await this.#getNoteContent(file, templateContext);
if (content) await app$.value.vault.modify(file, content);
Expand All @@ -129,6 +138,29 @@ export class Journal {
return file;
}

#confirmNoteCreation(noteName: string): Promise<boolean> {
return new Promise((resolve) => {
const modal = new VueModal(
"Confirm note creation",
ConfirmNoteCreationModal,
{
journalName: this.name$.value,
noteName,
onConfirm(confirm: boolean) {
modal.close();
resolve(confirm);
},
onClose() {
modal.close();
resolve(false);
},
},
400,
);
modal.open();
});
}

#getNotePath(metadata: JournalMetadata): string {
if (metadata.path) return metadata.path;
const templateContext = this.#getTemplateContext(metadata);
Expand Down
7 changes: 7 additions & 0 deletions src/settings/JournalSettingsEdit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ watch(
</ObsidianDropdown>
</ObsidianSetting>

<ObsidianSetting name="Confirm creating new note?">
<template #description>
When turned on will show confirmation dialog any time you try navigating to a date that does not have a note yet.
</template>
<ObsidianToggle v-model="journal.confirmCreation" />
</ObsidianSetting>

<ObsidianSetting name="Note name template">
<template #description>
Template used to generate new note name.<br />
Expand Down

0 comments on commit 792901f

Please sign in to comment.