feat(calendars): reorder calendar components

This commit is contained in:
Ryan Mwangi 2024-11-19 15:24:16 +03:00
parent e5e9dbb680
commit ac26afe778
1 changed files with 25 additions and 25 deletions

View File

@ -30,9 +30,9 @@ export async function fetchCalendarData(calendar) {
export function createCalendarComponent(name) { export function createCalendarComponent(name) {
const calendarComponent = new ICAL.Component(['vcalendar', [], []]); const calendarComponent = new ICAL.Component(['vcalendar', [], []]);
calendarComponent.updatePropertyWithValue('name', name); calendarComponent.updatePropertyWithValue('name', name);
calendarComponent.updatePropertyWithValue('prodid', '-//CalMerge//Calendar Merger 1.0//EN');
calendarComponent.updatePropertyWithValue('version', '2.0'); calendarComponent.updatePropertyWithValue('version', '2.0');
calendarComponent.updatePropertyWithValue('calscale', 'GREGORIAN'); calendarComponent.updatePropertyWithValue('calscale', 'GREGORIAN');
calendarComponent.updatePropertyWithValue('prodid', '-//CalMerge//Calendar Merger 1.0//EN');
return calendarComponent; return calendarComponent;
} }
@ -56,7 +56,20 @@ export function addEventsToCalendar(calendarComponent, results, overrideFlag = f
const vevent = new ICAL.Event(event); const vevent = new ICAL.Event(event);
const newEvent = new ICAL.Component('vevent'); const newEvent = new ICAL.Component('vevent');
// 1. DTEND with time zone // 1. DTSTART with time zone
if (vevent.startDate) {
const startTime = vevent.startDate.toString(); // Format start date properly
const dtstartProp = new ICAL.Property('dtstart', newEvent);
dtstartProp.setValue(startTime);
// Add TZID parameter if zone is present
if (vevent.startDate.zone) {
dtstartProp.setParameter('TZID', vevent.startDate.zone.tzid);
}
newEvent.addProperty(dtstartProp);
}
// 2. DTEND with time zone
if (vevent.endDate) { if (vevent.endDate) {
const endTime = vevent.endDate.toString(); // Format end date properly const endTime = vevent.endDate.toString(); // Format end date properly
const dtendProp = new ICAL.Property('dtend', newEvent); const dtendProp = new ICAL.Property('dtend', newEvent);
@ -69,24 +82,14 @@ export function addEventsToCalendar(calendarComponent, results, overrideFlag = f
newEvent.addProperty(dtendProp); newEvent.addProperty(dtendProp);
} }
// 2. Copy DTSTAMP // 3. Copy DTSTAMP
const dtstamp = event.getFirstPropertyValue('dtstamp'); const dtstamp = event.getFirstPropertyValue('dtstamp');
if (dtstamp) newEvent.updatePropertyWithValue('dtstamp', dtstamp); if (dtstamp) newEvent.updatePropertyWithValue('dtstamp', dtstamp);
// 3. DTSTART with time zone // 4. Copy UID
if (vevent.startDate) { newEvent.updatePropertyWithValue('uid', vevent.uid);
const startTime = vevent.startDate.toString(); // Format start date properly
const dtstartProp = new ICAL.Property('dtstart', newEvent);
dtstartProp.setValue(startTime);
// Add TZID parameter if zone is present
if (vevent.startDate.zone) {
dtstartProp.setParameter('TZID', vevent.startDate.zone.tzid);
}
newEvent.addProperty(dtstartProp);
}
// Add LOCATION (conditionally included) // 5. Add LOCATION (conditionally included)
if (!overrideFlag && vevent.location) { if (!overrideFlag && vevent.location) {
newEvent.updatePropertyWithValue('location', vevent.location); newEvent.updatePropertyWithValue('location', vevent.location);
} else if (overrideFlag && vevent.location) { } else if (overrideFlag && vevent.location) {
@ -97,23 +100,20 @@ export function addEventsToCalendar(calendarComponent, results, overrideFlag = f
newEvent.updatePropertyWithValue('summary', vevent.summary.trim()); newEvent.updatePropertyWithValue('summary', vevent.summary.trim());
} }
// 5. Copy Recurrence Rules (RRULE) and Recurrence ID // 6. Copy Recurrence Rules (RRULE) and Recurrence ID
const rrule = event.getFirstPropertyValue('rrule'); const rrule = event.getFirstPropertyValue('rrule');
if (rrule) newEvent.updatePropertyWithValue('rrule', rrule); if (rrule) newEvent.updatePropertyWithValue('rrule', rrule);
const recurrenceId = event.getFirstPropertyValue('recurrence-id'); const recurrenceId = event.getFirstPropertyValue('recurrence-id');
if (recurrenceId) newEvent.updatePropertyWithValue('recurrence-id', recurrenceId); if (recurrenceId) newEvent.updatePropertyWithValue('recurrence-id', recurrenceId);
// 6. Add SEQUENCE (if available or default to 0)
const sequence = event.getFirstPropertyValue('sequence') || 0;
newEvent.updatePropertyWithValue('sequence', sequence);
// 7. Copy SUMMARY // 7. Copy SUMMARY
newEvent.updatePropertyWithValue('summary', vevent.summary.trim()); newEvent.updatePropertyWithValue('summary', vevent.summary.trim());
// 8. Copy UID // 8. Add SEQUENCE (if available or default to 0)
newEvent.updatePropertyWithValue('uid', vevent.uid); const sequence = event.getFirstPropertyValue('sequence') || 0;
newEvent.updatePropertyWithValue('sequence', sequence);
// Add the VEVENT to the calendar // Add the VEVENT to the calendar
calendarComponent.addSubcomponent(newEvent); calendarComponent.addSubcomponent(newEvent);
}); });