From 680729de63cc14f47bdd2f11815bd654e94ed0b5 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sun, 16 Mar 2025 20:09:21 +0200 Subject: [PATCH] feat(calendar): allow dragging to set the time --- .../app/widgets/view_widgets/calendar_view.ts | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/public/app/widgets/view_widgets/calendar_view.ts b/src/public/app/widgets/view_widgets/calendar_view.ts index 36e4a31ef..2403523c5 100644 --- a/src/public/app/widgets/view_widgets/calendar_view.ts +++ b/src/public/app/widgets/view_widgets/calendar_view.ts @@ -189,27 +189,45 @@ export default class CalendarView extends ViewMode { } async #onCalendarSelection(e: DateSelectArg) { + // Handle start and end date const startDate = CalendarView.#formatDateToLocalISO(e.start); if (!startDate) { return; } - const endDate = CalendarView.#formatDateToLocalISO(CalendarView.#offsetDate(e.end, -1)); + // Handle start and end time. + let startTime = null; + let endTime = null; + if (!e.allDay) { + startTime = CalendarView.#formatTimeToLocalISO(e.start); + endTime = CalendarView.#formatTimeToLocalISO(e.end); + } + + // Ask for the title const title = await dialogService.prompt({ message: t("relation_map.enter_title_of_new_note"), defaultValue: t("relation_map.default_new_note_title") }); if (!title?.trim()) { return; } + // Create the note. const { note } = await server.post(`notes/${this.parentNote.noteId}/children?target=into`, { title, content: "", type: "text" }); + + // Set the attributes. attributes.setLabel(note.noteId, "startDate", startDate); if (endDate) { attributes.setLabel(note.noteId, "endDate", endDate); } + if (startTime) { + attributes.setLabel(note.noteId, "startTime", startTime); + } + if (endTime) { + attributes.setLabel(note.noteId, "endTime", endTime); + } } async #onEventMoved(e: EventChangeArg) { @@ -445,6 +463,18 @@ export default class CalendarView extends ViewMode { return localDate.toISOString().split("T")[0]; } + static #formatTimeToLocalISO(date: Date | null | undefined) { + if (!date) { + return undefined; + } + + const offset = date.getTimezoneOffset(); + const localDate = new Date(date.getTime() - offset * 60 * 1000); + return localDate.toISOString() + .split("T")[1] + .substring(0, 5); + } + static #offsetDate(date: Date | string | null | undefined, offset: number) { if (!date) { return undefined;