Add different colors for recurring events on event calendar

This commit is contained in:
2025-12-16 17:03:23 +01:00
parent d3edcaff14
commit 749cd067da
2 changed files with 56 additions and 2 deletions

View File

@@ -1,6 +1,6 @@
import { makeUrl } from "#core:utils/api"; import { makeUrl } from "#core:utils/api";
import { inheritHtmlElement, registerComponent } from "#core:utils/web-components"; import { inheritHtmlElement, registerComponent } from "#core:utils/web-components";
import { Calendar, type EventClickArg } from "@fullcalendar/core"; import { Calendar, type EventClickArg, type EventContentArg } from "@fullcalendar/core";
import type { EventImpl } from "@fullcalendar/core/internal"; import type { EventImpl } from "@fullcalendar/core/internal";
import enLocale from "@fullcalendar/core/locales/en-gb"; import enLocale from "@fullcalendar/core/locales/en-gb";
import frLocale from "@fullcalendar/core/locales/fr"; import frLocale from "@fullcalendar/core/locales/fr";
@@ -25,6 +25,11 @@ export class IcsCalendar extends inheritHtmlElement("div") {
private canDelete = false; private canDelete = false;
private helpUrl = ""; private helpUrl = "";
// Hack variable to detect recurring events
// The underlying ics library doesn't include any info about rrules
// That's why we have to detect those events ourselves
private recurrenceMap: Map<string, EventImpl> = new Map();
attributeChangedCallback(name: string, _oldValue?: string, newValue?: string) { attributeChangedCallback(name: string, _oldValue?: string, newValue?: string) {
if (name === "locale") { if (name === "locale") {
this.locale = newValue; this.locale = newValue;
@@ -95,6 +100,7 @@ export class IcsCalendar extends inheritHtmlElement("div") {
refreshEvents() { refreshEvents() {
this.click(); // Remove focus from popup this.click(); // Remove focus from popup
this.recurrenceMap.clear(); // Avoid double detection of the same non recurring event
this.calendar.refetchEvents(); this.calendar.refetchEvents();
} }
@@ -153,12 +159,24 @@ export class IcsCalendar extends inheritHtmlElement("div") {
} }
async getEventSources() { async getEventSources() {
const tagRecurringEvents = (eventData: EventImpl) => {
// This functions tags events with a similar event url
// We rely on the fact that the event url is always the same
// for recurring events and always different for single events
const firstEvent = this.recurrenceMap.get(eventData.url);
if (firstEvent !== undefined) {
eventData.extendedProps.isRecurring = true;
firstEvent.extendedProps.isRecurring = true; // Don't forget the first event
}
this.recurrenceMap.set(eventData.url, eventData);
};
return [ return [
{ {
url: `${await makeUrl(calendarCalendarInternal)}`, url: `${await makeUrl(calendarCalendarInternal)}`,
format: "ics", format: "ics",
className: "internal", className: "internal",
cache: false, cache: false,
eventDataTransform: tagRecurringEvents,
}, },
{ {
url: `${await makeUrl(calendarCalendarUnpublished)}`, url: `${await makeUrl(calendarCalendarUnpublished)}`,
@@ -166,6 +184,7 @@ export class IcsCalendar extends inheritHtmlElement("div") {
color: "red", color: "red",
className: "unpublished", className: "unpublished",
cache: false, cache: false,
eventDataTransform: tagRecurringEvents,
}, },
]; ];
} }
@@ -361,6 +380,14 @@ export class IcsCalendar extends inheritHtmlElement("div") {
event.jsEvent.preventDefault(); event.jsEvent.preventDefault();
this.createEventDetailPopup(event); this.createEventDetailPopup(event);
}, },
eventClassNames: (classNamesEvent: EventContentArg) => {
const classes: string[] = [];
if (classNamesEvent.event.extendedProps?.isRecurring) {
classes.push("recurring");
}
return classes;
},
}); });
this.calendar.render(); this.calendar.render();

View File

@@ -18,6 +18,8 @@
--event-details-border-radius: 4px; --event-details-border-radius: 4px;
--event-details-box-shadow: 0px 6px 20px 4px rgb(0 0 0 / 16%); --event-details-box-shadow: 0px 6px 20px 4px rgb(0 0 0 / 16%);
--event-details-max-width: 600px; --event-details-max-width: 600px;
--event-recurring-internal-color: #6f69cd;
--event-recurring-unpublished-color: orange;
} }
ics-calendar { ics-calendar {
@@ -146,4 +148,29 @@ ics-calendar {
.tooltip.calendar-copy-tooltip.text-copied { .tooltip.calendar-copy-tooltip.text-copied {
opacity: 0; opacity: 0;
transition: opacity 500ms ease-out; transition: opacity 500ms ease-out;
} }
// We have to override the color set by the lib in the html
// Hence the !important tag everywhere
.internal.recurring {
.fc-daygrid-event-dot {
border-color: var(--event-recurring-internal-color) !important;
}
&.fc-daygrid-block-event {
background-color: var(--event-recurring-internal-color) !important;
border-color: var(--event-recurring-internal-color) !important;
}
}
.unpublished.recurring {
.fc-daygrid-event-dot {
border-color: var(--event-recurring-unpublished-color) !important;
}
&.fc-daygrid-block-event {
background-color: var(--event-recurring-unpublished-color) !important;
border-color: var(--event-recurring-unpublished-color) !important;
}
}