Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(calendar): draw a recurring whole-day event as whole-day
Elian Doran35485e6d31f4
diff --git a/apps/client/src/widgets/collections/calendar/event_builder.spec.ts b/apps/client/src/widgets/collections/calendar/event_builder.spec.tsindex 853fcba612..d477d8b6a1 100644--- a/apps/client/src/widgets/collections/calendar/event_builder.spec.ts+++ b/apps/client/src/widgets/collections/calendar/event_builder.spec.ts@@ -229,7 +229,7 @@ describe("Recurrence", () => {expect(events[0]).toMatchObject({title: "Recurring Event Spanning Day Boundary",start: "2025-05-05T23:00:00",- duration: "02:00"+ duration: { days: 0, hours: 2, minutes: 0 }});expect(events[0].rrule).toContain("DTSTART:20250505T230000");expect(events[0].end).toBeUndefined();@@ -251,7 +251,7 @@ describe("Recurrence", () => {expect(events[0]).toMatchObject({title: "Timed Recurring Event",start: "2025-05-05T13:00:00",- duration: "02:30"+ duration: { days: 0, hours: 2, minutes: 30 }});expect(events[0].rrule).toContain("DTSTART:20250505T130000");expect(events[0].end).toBeUndefined();@@ -273,6 +273,57 @@ describe("Recurrence", () => {expect(events[0].end).toBeUndefined();});+ it("keeps a recurring event whole-day where it has no times", async () => {+ const noteIds = buildNotes([+ {+ title: "Recurring Whole Day",+ "#startDate": "2025-05-05",+ "#recurrence": "FREQ=DAILY;COUNT=5"+ },+ {+ title: "Recurring Timed",+ "#startDate": "2025-05-05",+ "#startTime": "13:00",+ "#endTime": "15:30",+ "#recurrence": "FREQ=DAILY;COUNT=5"+ }+ ]);+ const events = await buildEvents(noteIds);++ expect(events).toHaveLength(2);+ // Said outright rather than left to FullCalendar to guess: the rrule plugin reads the whole+ // of the rule for a time, and a `UNTIL=…T235959Z` — which the recurrence editor writes —+ // makes it guess a timed event however the DTSTART is written.+ expect(events[0].allDay).toBe(true);+ expect(events[1].allDay).toBe(false);+ // And the DTSTART says the same, no midnight invented for a day that has no hours.+ expect(events[0].rrule).toContain("DTSTART:20250505\n");+ expect(events[1].rrule).toContain("DTSTART:20250505T130000\n");+ });++ it("carries the length of a recurring event that runs over days", async () => {+ const noteIds = buildNotes([+ {+ title: "Recurring Single Day",+ "#startDate": "2025-05-05",+ "#recurrence": "FREQ=WEEKLY;COUNT=3"+ },+ {+ title: "Recurring Three Days",+ "#startDate": "2025-05-05",+ "#endDate": "2025-05-07",+ "#recurrence": "FREQ=WEEKLY;COUNT=3"+ }+ ]);+ const events = await buildEvents(noteIds);++ expect(events).toHaveLength(2);+ // Days rather than hours: an `HH:mm` duration has nowhere to put them, so a whole-day+ // occurrence used to come out "00:00" — no length at all.+ expect(events[0].duration).toEqual({ days: 1, hours: 0, minutes: 0 });+ expect(events[1].duration).toEqual({ days: 3, hours: 0, minutes: 0 });+ });+it("writes to console on invalid recurrence rule", async () => {const noteIds = buildNotes([{diff --git a/apps/client/src/widgets/collections/calendar/event_builder.ts b/apps/client/src/widgets/collections/calendar/event_builder.tsindex a5174ace90..5ecdf6f6a3 100644--- a/apps/client/src/widgets/collections/calendar/event_builder.ts+++ b/apps/client/src/widgets/collections/calendar/event_builder.ts@@ -105,30 +105,41 @@ export async function buildEvent(note: FNote, { startDate, endDate, startTime, edisplayedAttributesData = await buildDisplayedAttributes(note, calendarDisplayedAttributes);}- for (const title of titles) {- if (startTime && endTime && !endDate) {- endDate = startDate;- }+ // An event with no start time takes the whole day, which is how the editor writes one too (see+ // EventDatesEditor). When the event repeats this has to be said outright rather than left to+ // FullCalendar's rrule plugin to infer from the rule: the plugin reads the whole of the rule+ // looking for a time, so an `UNTIL=…T235959Z` — which the recurrence editor writes for an end+ // date (see recurrence.ts) — makes it take the event for a timed one whatever its DTSTART says.+ const allDay = !startTime;++ // When the event happens is the same whatever the note is called, so it is worked out once for+ // all the titles rather than inside the loop, which used to append the time to the dates again+ // for every title past the first.+ if (startTime && endTime && !endDate) {+ endDate = startDate;+ }- startDate = (startTime ? `${startDate}T${startTime}:00` : startDate);- if (!startTime) {- if (endDate) {- endDate = dayjs(endDate).add(1, "day").format("YYYY-MM-DD");- } else if (startDate) {- endDate = dayjs(startDate).add(1, "day").format("YYYY-MM-DD");- }+ startDate = (startTime ? `${startDate}T${startTime}:00` : startDate);+ if (!startTime) {+ if (endDate) {+ endDate = dayjs(endDate).add(1, "day").format("YYYY-MM-DD");+ } else if (startDate) {+ endDate = dayjs(startDate).add(1, "day").format("YYYY-MM-DD");}+ }- endDate = (endTime ? `${endDate}T${endTime}:00` : endDate);- // If the end date is now before the start date, bump it a day forward to account for times spanning the day boundary- if (endDate && endTime && dayjs(endDate).isBefore(dayjs(startDate))) {- endDate = dayjs(endDate).add(1, "day").format("YYYY-MM-DDTHH:mm:ss");- }+ endDate = (endTime ? `${endDate}T${endTime}:00` : endDate);+ // If the end date is now before the start date, bump it a day forward to account for times spanning the day boundary+ if (endDate && endTime && dayjs(endDate).isBefore(dayjs(startDate))) {+ endDate = dayjs(endDate).add(1, "day").format("YYYY-MM-DDTHH:mm:ss");+ }+ for (const title of titles) {const eventData: EventInput = {id: note.noteId,title,start: startDate,+ allDay,url: `#${note.noteId}?popup`,noteId: note.noteId,iconClass: note.getLabelValue("iconClass"),@@ -140,8 +151,11 @@ export async function buildEvent(note: FNote, { startDate, endDate, startTime, e}if (recurrence) {- // Generate rrule string- const rruleString = `DTSTART:${dayjs(startDate).format("YYYYMMDD[T]HHmmss")}\n${recurrence}`;+ // Generate rrule string. A whole day is written as the bare date it is, no midnight+ // invented for hours the event does not have. `DTSTART;VALUE=DATE:` — the way iCalendar+ // spells the same thing — is not an option: the rrule library drops a DTSTART written+ // that way without a word and starts the series from today instead.+ const rruleString = `DTSTART:${dayjs(startDate).format(allDay ? "YYYYMMDD" : "YYYYMMDD[T]HHmmss")}\n${recurrence}`;// Validate rrule stringlet rruleValid = true;@@ -155,8 +169,7 @@ export async function buildEvent(note: FNote, { startDate, endDate, startTime, edelete eventData.end;eventData.rrule = rruleString;if (endDate){- const duration = dayjs.duration(dayjs(endDate).diff(dayjs(startDate)));- eventData.duration = duration.format("HH:mm");+ eventData.duration = buildOccurrenceDuration(startDate, endDate);}} else {throw new Error(`Note "${note.noteId} ${note.title}" has an invalid #recurrence string ${recurrence}. Excluding...`);@@ -167,6 +180,22 @@ export async function buildEvent(note: FNote, { startDate, endDate, startTime, ereturn events;}+/**+ * How long one occurrence of a recurring event lasts, as the duration object FullCalendar builds+ * its own from rather than as an `HH:mm` string. A string of hours and minutes has nowhere to put+ * days: a whole-day occurrence came out of it as `"00:00"` — no length at all — and a span of days+ * kept only the hours left over past the last of them.+ */+function buildOccurrenceDuration(startDate: string, endDate: string) {+ const minutes = dayjs(endDate).diff(dayjs(startDate), "minute");++ return {+ days: Math.floor(minutes / (24 * 60)),+ hours: Math.floor((minutes % (24 * 60)) / 60),+ minutes: minutes % 60+ };+}+async function parseCustomTitle(customTitlettributeName: string | null, note: FNote, allowRelations = true): Promise<string[]> {if (customTitlettributeName) {const labelValue = note.getAttributeValue("label", customTitlettributeName);