From 35fa0d7b16ba33aec5f0bad496fab6c0acb0f733 Mon Sep 17 00:00:00 2001 From: AB1908 <14124383+AB1908@users.noreply.github.com> Date: Sat, 22 Apr 2023 02:43:53 +0530 Subject: [PATCH 1/3] feat: add modal to pick date --- .../date/DatePickerModal.ts | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/core/functions/internal_functions/date/DatePickerModal.ts diff --git a/src/core/functions/internal_functions/date/DatePickerModal.ts b/src/core/functions/internal_functions/date/DatePickerModal.ts new file mode 100644 index 00000000..66f8c52d --- /dev/null +++ b/src/core/functions/internal_functions/date/DatePickerModal.ts @@ -0,0 +1,71 @@ +import { TemplaterError } from "utils/Error"; +import { + ButtonComponent, + Modal, + moment +} from "obsidian"; + +export class DateModal extends Modal { + private resolve: (value: string) => void; + private reject: (reason?: TemplaterError) => void; + private submitted = false; + private value: string; + + constructor( + private title: string, + private format: string, + ) { + super(app); + this.value = moment().format(format); + } + + onOpen(): void { + this.titleEl.setText(this.title); + this.createForm(); + } + + onClose(): void { + this.contentEl.empty(); + if (!this.submitted) { + // TOFIX: for some reason throwing TemplaterError on iOS causes the app to freeze. + // this.reject(new TemplaterError("Cancelled prompt")); + this.reject(); + } + } + + createForm(): void { + const div = this.contentEl.createDiv(); + div.addClass("templater-date-div"); + const datePicker = this.contentEl.createEl("input"); + datePicker.type = "date"; + const buttonDiv = this.contentEl.createDiv(); + buttonDiv.addClass("templater-button-div"); + const submitButton = new ButtonComponent(buttonDiv); + submitButton.buttonEl.addClass("mod-cta"); + submitButton.setButtonText("Submit").onClick((evt: Event) => { + this.resolveAndClose(evt); + }); + + datePicker.addClass("templater-prompt-input"); + datePicker.value = this.value; + datePicker.addEventListener("input", (event) => { + this.value = moment((event.target as HTMLInputElement)?.valueAsDate).format(this.format) || moment().format(this.format); + }); + } + + private resolveAndClose(evt: Event | KeyboardEvent) { + this.submitted = true; + evt.preventDefault(); + this.resolve(this.value); + this.close(); + } + + async openAndGetValue( + resolve: (value: string) => void, + reject: (reason?: TemplaterError) => void + ): Promise { + this.resolve = resolve; + this.reject = reject; + this.open(); + } +} From 5a568e24744c1ce3583a28197fb8f3a85c3549cb Mon Sep 17 00:00:00 2001 From: AB1908 <14124383+AB1908@users.noreply.github.com> Date: Sat, 22 Apr 2023 02:44:34 +0530 Subject: [PATCH 2/3] feat: add API for calling new date modal --- .../date/InternalModuleDate.ts | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/core/functions/internal_functions/date/InternalModuleDate.ts b/src/core/functions/internal_functions/date/InternalModuleDate.ts index fceeae95..11ec9ad9 100644 --- a/src/core/functions/internal_functions/date/InternalModuleDate.ts +++ b/src/core/functions/internal_functions/date/InternalModuleDate.ts @@ -1,6 +1,7 @@ import { TemplaterError } from "utils/Error"; import { InternalModule } from "../InternalModule"; import { ModuleName } from "editor/TpDocumentation"; +import { DateModal } from "./DatePickerModal"; export class InternalModuleDate extends InternalModule { public name: ModuleName = "date"; @@ -10,6 +11,7 @@ export class InternalModuleDate extends InternalModule { this.static_functions.set("tomorrow", this.generate_tomorrow()); this.static_functions.set("weekday", this.generate_weekday()); this.static_functions.set("yesterday", this.generate_yesterday()); + this.static_functions.set("date_picker", this.generate_date_picker()); } async create_dynamic_templates(): Promise {} @@ -86,4 +88,35 @@ export class InternalModuleDate extends InternalModule { return window.moment().add(-1, "days").format(format); }; } + + generate_date_picker(): ( + title: string, + placeholder: string, + throw_on_cancel: boolean, + ) => Promise { + return async ( + title: string, + format = "YYYY-MM-DD", + throw_on_cancel = false + ): Promise => { + const datePicker = new DateModal( + title, + format + ); + const promise = new Promise( + ( + resolve: (value: any) => void, + reject: (reason?: TemplaterError) => void + ) => datePicker.openAndGetValue(resolve, reject) + ); + try { + return await promise; + } catch (error) { + if (throw_on_cancel) { + throw error; + } + return null as any; + } + }; + } } From b6099a7fec26240d602f228251d8d9e2d27090ed Mon Sep 17 00:00:00 2001 From: AB1908 <14124383+AB1908@users.noreply.github.com> Date: Sat, 22 Apr 2023 21:55:43 +0530 Subject: [PATCH 3/3] fix: update argument name --- .../functions/internal_functions/date/InternalModuleDate.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/functions/internal_functions/date/InternalModuleDate.ts b/src/core/functions/internal_functions/date/InternalModuleDate.ts index 11ec9ad9..7ef30d79 100644 --- a/src/core/functions/internal_functions/date/InternalModuleDate.ts +++ b/src/core/functions/internal_functions/date/InternalModuleDate.ts @@ -91,7 +91,7 @@ export class InternalModuleDate extends InternalModule { generate_date_picker(): ( title: string, - placeholder: string, + format: string, throw_on_cancel: boolean, ) => Promise { return async (