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; + } + }; + } }