Compare commits

..

3 Commits

Author SHA1 Message Date
5c74c2e667 Fix timezone parsing for calendars 2026-04-03 15:40:06 +02:00
1cfd935794 Add zed settings 2026-04-03 15:27:34 +02:00
8a36333883 Add Validation for time imputs 2026-04-03 15:27:26 +02:00
3 changed files with 114 additions and 5 deletions

5
.zed/settings.json Normal file
View File

@@ -0,0 +1,5 @@
{
"file_types": {
"LESS": ["style.css"],
},
}

View File

@@ -458,6 +458,39 @@
}); });
} }
function setupTimeInputNormalization(input) {
// Track digits typed into the hours sub-field so we can reconstruct
// "HH:00" when the user fills hours but leaves minutes as "--".
var typedDigits = "";
input.addEventListener("keydown", function (e) {
if (this.value) {
// Field already has a valid complete value — reset tracking.
typedDigits = "";
return;
}
if (/^\d$/.test(e.key)) {
typedDigits = (typedDigits + e.key).slice(-2);
} else if (e.key === "Backspace" || e.key === "Delete") {
typedDigits = "";
}
});
input.addEventListener("change", function () {
typedDigits = "";
});
input.addEventListener("blur", function () {
if (!this.value && this.validity && this.validity.badInput) {
var h = parseInt(typedDigits, 10);
if (!isNaN(h) && h >= 0 && h <= 23) {
this.value = pad2(h) + ":00";
}
typedDigits = "";
}
});
}
function renderForm(data, slots) { function renderForm(data, slots) {
var isEdit = data.mode === "edit"; var isEdit = data.mode === "edit";
var title = isEdit ? "Edit Event" : "Create Event"; var title = isEdit ? "Edit Event" : "Create Event";
@@ -565,6 +598,12 @@
timeFields.style.display = allDayCheckbox.checked ? "none" : ""; timeFields.style.display = allDayCheckbox.checked ? "none" : "";
}); });
} }
// Wire up time normalization: auto-substitute :00 for missing minutes
var startTimeInput = popup.querySelector(".luxtools-form-start-time");
var endTimeInput = popup.querySelector(".luxtools-form-end-time");
if (startTimeInput) setupTimeInputNormalization(startTimeInput);
if (endTimeInput) setupTimeInputNormalization(endTimeInput);
} }
function collectFormData() { function collectFormData() {
@@ -600,6 +639,22 @@
return; return;
} }
if (!formData.allDay) {
var popup = getDialog().getContainer();
var startInput = popup.querySelector(".luxtools-form-start-time");
var endInput = popup.querySelector(".luxtools-form-end-time");
if (
(startInput && startInput.validity && startInput.validity.badInput) ||
(endInput && endInput.validity && endInput.validity.badInput)
) {
showNotification(
"Please complete the time fields or clear them.",
"error",
);
return;
}
}
// For recurring event edits, ask about scope first // For recurring event edits, ask about scope first
if (mode === "edit") { if (mode === "edit") {
var recurrence = saveBtn.getAttribute("data-recurrence") || ""; var recurrence = saveBtn.getAttribute("data-recurrence") || "";

View File

@@ -138,12 +138,13 @@ class CalendarService
if ($calendar === null) continue; if ($calendar === null) continue;
try { try {
$uidTimezones = self::buildUidTimezoneMap($calendar);
$expanded = $calendar->expand($rangeStart, $rangeEnd); $expanded = $calendar->expand($rangeStart, $rangeEnd);
if (!($expanded instanceof VCalendar)) continue; if (!($expanded instanceof VCalendar)) continue;
for ($day = 1; $day <= $daysInMonth; $day++) { for ($day = 1; $day <= $daysInMonth; $day++) {
$dateIso = sprintf('%04d-%02d-%02d', $year, $month, $day); $dateIso = sprintf('%04d-%02d-%02d', $year, $month, $day);
$events = self::collectFromCalendar($expanded, $slot->getKey(), $dateIso); $events = self::collectFromCalendar($expanded, $slot->getKey(), $dateIso, $uidTimezones);
$cacheKey = $slot->getKey() . '|' . $dateIso; $cacheKey = $slot->getKey() . '|' . $dateIso;
self::$dayCache[$cacheKey] = $events; self::$dayCache[$cacheKey] = $events;
@@ -222,6 +223,28 @@ class CalendarService
} }
} }
/**
* Build a UID → TZID map from the original (non-expanded) calendar.
*
* VCalendar::expand() normalizes all timezone-aware datetimes to UTC, losing
* the original TZID. This map lets us restore the correct timezone afterwards.
*
* @param VCalendar $calendar
* @return array<string,string> uid => tzid
*/
protected static function buildUidTimezoneMap(VCalendar $calendar): array
{
$map = [];
foreach ($calendar->select('VEVENT') as $vevent) {
if (!isset($vevent->DTSTART)) continue;
$uid = trim((string)($vevent->UID ?? ''));
if ($uid === '' || isset($map[$uid])) continue;
$tzid = (string)($vevent->DTSTART['TZID'] ?? '');
if ($tzid !== '') $map[$uid] = $tzid;
}
return $map;
}
/** /**
* Parse events from a local ICS file for a specific date. * Parse events from a local ICS file for a specific date.
* *
@@ -236,6 +259,8 @@ class CalendarService
if ($calendar === null) return []; if ($calendar === null) return [];
try { try {
$uidTimezones = self::buildUidTimezoneMap($calendar);
$utc = new DateTimeZone('UTC'); $utc = new DateTimeZone('UTC');
$rangeStart = new DateTimeImmutable($dateIso . ' 00:00:00', $utc); $rangeStart = new DateTimeImmutable($dateIso . ' 00:00:00', $utc);
$rangeStart = $rangeStart->sub(new DateInterval('P1D')); $rangeStart = $rangeStart->sub(new DateInterval('P1D'));
@@ -244,7 +269,7 @@ class CalendarService
$expanded = $calendar->expand($rangeStart, $rangeEnd); $expanded = $calendar->expand($rangeStart, $rangeEnd);
if (!($expanded instanceof VCalendar)) return []; if (!($expanded instanceof VCalendar)) return [];
return self::collectFromCalendar($expanded, $slotKey, $dateIso); return self::collectFromCalendar($expanded, $slotKey, $dateIso, $uidTimezones);
} catch (Throwable $e) { } catch (Throwable $e) {
return []; return [];
} }
@@ -256,9 +281,10 @@ class CalendarService
* @param VCalendar $calendar * @param VCalendar $calendar
* @param string $slotKey * @param string $slotKey
* @param string $dateIso * @param string $dateIso
* @param array<string,string> $uidTimezones uid => tzid, for restoring timezone after expand()
* @return CalendarEvent[] * @return CalendarEvent[]
*/ */
protected static function collectFromCalendar(VCalendar $calendar, string $slotKey, string $dateIso): array protected static function collectFromCalendar(VCalendar $calendar, string $slotKey, string $dateIso, array $uidTimezones = []): array
{ {
$result = []; $result = [];
$seen = []; $seen = [];
@@ -266,7 +292,7 @@ class CalendarService
// VEVENTs // VEVENTs
foreach ($calendar->select('VEVENT') as $vevent) { foreach ($calendar->select('VEVENT') as $vevent) {
if (!($vevent instanceof VEvent)) continue; if (!($vevent instanceof VEvent)) continue;
$event = self::normalizeVEventForDay($vevent, $slotKey, $dateIso); $event = self::normalizeVEventForDay($vevent, $slotKey, $dateIso, $uidTimezones);
if ($event === null) continue; if ($event === null) continue;
$dedupeKey = $event->uid . '|' . $event->recurrenceId . '|' . $event->startIso . '|' . $event->summary; $dedupeKey = $event->uid . '|' . $event->recurrenceId . '|' . $event->startIso . '|' . $event->summary;
@@ -285,9 +311,10 @@ class CalendarService
* @param VEvent $vevent * @param VEvent $vevent
* @param string $slotKey * @param string $slotKey
* @param string $dateIso * @param string $dateIso
* @param array<string,string> $uidTimezones uid => tzid, for restoring timezone after expand()
* @return CalendarEvent|null * @return CalendarEvent|null
*/ */
protected static function normalizeVEventForDay(VEvent $vevent, string $slotKey, string $dateIso): ?CalendarEvent protected static function normalizeVEventForDay(VEvent $vevent, string $slotKey, string $dateIso, array $uidTimezones = []): ?CalendarEvent
{ {
if (!isset($vevent->DTSTART)) return null; if (!isset($vevent->DTSTART)) return null;
@@ -295,8 +322,30 @@ class CalendarService
$start = self::toImmutable($vevent->DTSTART->getDateTime()); $start = self::toImmutable($vevent->DTSTART->getDateTime());
if ($start === null) return null; if ($start === null) return null;
// VCalendar::expand() normalizes all timezone-aware datetimes to UTC, losing
// the original TZID. Restore it using the pre-expansion UID→TZID map so that
// display times and day-boundary checks use the event's original timezone.
$restoredTz = null;
if (!$isAllDay && $uidTimezones !== [] && $start->getTimezone()->getName() === 'UTC') {
$uid = trim((string)($vevent->UID ?? ''));
$tzid = $uidTimezones[$uid] ?? '';
if ($tzid !== '') {
try {
$restoredTz = new DateTimeZone($tzid);
$start = $start->setTimezone($restoredTz);
} catch (Throwable $e) {
$restoredTz = null;
}
}
}
$end = self::resolveEnd($vevent, $start, $isAllDay); $end = self::resolveEnd($vevent, $start, $isAllDay);
// Apply the same timezone restoration to the end datetime.
if ($restoredTz !== null && $end->getTimezone()->getName() === 'UTC') {
$end = $end->setTimezone($restoredTz);
}
if (!self::intersectsDay($start, $end, $isAllDay, $dateIso)) return null; if (!self::intersectsDay($start, $end, $isAllDay, $dateIso)) return null;
$event = new CalendarEvent(); $event = new CalendarEvent();