Skip to content

Commit

Permalink
feat: add API for calling new date modal
Browse files Browse the repository at this point in the history
  • Loading branch information
AB1908 committed Apr 22, 2023
1 parent 35fa0d7 commit 5a568e2
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/core/functions/internal_functions/date/InternalModuleDate.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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<void> {}
Expand Down Expand Up @@ -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<string> {
return async (
title: string,
format = "YYYY-MM-DD",
throw_on_cancel = false
): Promise<string> => {
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;
}
};
}
}

0 comments on commit 5a568e2

Please sign in to comment.