Extract calendar draft helpers
All checks were successful
Deploy Sokko G / deploy (push) Successful in 15s

This commit is contained in:
Shinuwa 2026-08-24 22:11:16 +02:00
parent d9bbf40fad
commit f2c88ee891
4 changed files with 110 additions and 65 deletions

View file

@ -0,0 +1,74 @@
// Rôle : regroupe les helpers purs de draft et de sélection du calendrier.
import {
CALENDAR_ALERT_OFFSETS,
CALENDAR_MONTHLY_REPEAT_BY,
WEEK_SLOT_MINUTES,
formatLocalDateKey,
getLocalDayIndex,
minutesToTimeValue,
parseLocalDateKey,
timeValueToMinutes
} from "./calendarUtils.js";
import { WEEK_SLOT_COUNT } from "./calendarLayout.js";
export function createDefaultDraft(date = new Date()) {
const dayIndex = getLocalDayIndex(date);
const dateKey = formatLocalDateKey(date);
const startMinutes = Math.min(23 * 60 + 30, Math.ceil((date.getHours() * 60 + date.getMinutes()) / WEEK_SLOT_MINUTES) * WEEK_SLOT_MINUTES);
return {
id: "",
title: "",
dayIndex,
endDayIndex: dayIndex,
startDate: dateKey,
endDate: dateKey,
monthlyRepeatBy: "monthDay",
repeat: "monthly",
startTime: minutesToTimeValue(startMinutes),
endTime: minutesToTimeValue(Math.min(23 * 60 + 59, startMinutes + WEEK_SLOT_MINUTES)),
repeatable: true,
alertMode: "off",
alertOffsetMinutes: [],
color: "violet"
};
}
export function getDraftSelection(draft) {
const dayIndex = Number(draft.dayIndex);
const startMinutes = timeValueToMinutes(draft.startTime);
const endMinutes = draft.endTime ? timeValueToMinutes(draft.endTime) : startMinutes + WEEK_SLOT_MINUTES;
if (dayIndex < 0 || dayIndex > 6 || startMinutes < 0 || endMinutes <= startMinutes) return null;
const startSlot = Math.floor(startMinutes / WEEK_SLOT_MINUTES);
const endSlot = Math.max(startSlot, Math.ceil(endMinutes / WEEK_SLOT_MINUTES) - 1);
return {
dayIndex,
startSlot: Math.max(0, Math.min(WEEK_SLOT_COUNT - 1, startSlot)),
endSlot: Math.max(0, Math.min(WEEK_SLOT_COUNT - 1, endSlot))
};
}
export function getCalendarRepeatMode() {
return "weekly";
}
export function getDateSpanDays(startDateKey, endDateKey) {
const start = parseLocalDateKey(startDateKey);
const end = parseLocalDateKey(endDateKey);
if (!start || !end || end < start) return 0;
return Math.round((end.getTime() - start.getTime()) / (24 * 60 * 60 * 1000));
}
export function normalizeDraftDateKey(value) {
const date = parseLocalDateKey(value);
return date ? formatLocalDateKey(date) : "";
}
export function normalizeDraftAlertOffsets(value) {
const rawOffsets = Array.isArray(value) ? value : [value];
const offsets = [...new Set(rawOffsets.map(Number).filter((offset) => CALENDAR_ALERT_OFFSETS.includes(offset)))];
return offsets.sort((a, b) => b - a);
}
export function normalizeDraftMonthlyRepeatBy(value) {
return CALENDAR_MONTHLY_REPEAT_BY.includes(value) ? value : "monthDay";
}