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

@ -6,9 +6,7 @@ import { CalendarMonthView, CalendarWeekGrid } from "./CalendarViews.jsx";
import { TextExchangeActions } from "./TextExchangeActions.jsx";
import {
CALENDAR_ALERT_MODES,
CALENDAR_ALERT_OFFSETS,
CALENDAR_COLORS,
CALENDAR_MONTHLY_REPEAT_BY,
WEEK_SLOT_MINUTES,
addDays,
addMonths,
@ -24,68 +22,16 @@ import {
parseLocalDateKey,
timeValueToMinutes
} from "./calendarUtils.js";
import { WEEK_SLOT_COUNT, getWeekGridLayout } from "./calendarLayout.js";
import {
WEEK_SLOT_COUNT,
getWeekGridLayout
} from "./calendarLayout.js";
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"
};
}
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))
};
}
function getCalendarRepeatMode() {
return "weekly";
}
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));
}
function normalizeDraftDateKey(value) {
const date = parseLocalDateKey(value);
return date ? formatLocalDateKey(date) : "";
}
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);
}
createDefaultDraft,
getCalendarRepeatMode,
getDateSpanDays,
getDraftSelection,
normalizeDraftAlertOffsets,
normalizeDraftDateKey,
normalizeDraftMonthlyRepeatBy
} from "./calendarDraft.js";
export function CalendarModule({ toolboxId, moduleId, context, editing = false, setEditing, textExchangeOpen }) {
const data = context.normalizeCalendarData(context.getModuleData(toolboxId, moduleId, { initialized: false, viewMode: "week", events: [] }));
@ -330,7 +276,7 @@ export function CalendarModule({ toolboxId, moduleId, context, editing = false,
const endDate = normalizeDraftDateKey(draft.endDate || draft.startDate);
const startMinutes = data.viewMode === "month" ? 0 : timeValueToMinutes(draft.startTime);
const endMinutes = data.viewMode === "month" ? -1 : draft.endTime ? timeValueToMinutes(draft.endTime) : -1;
const monthlyRepeatBy = CALENDAR_MONTHLY_REPEAT_BY.includes(draft.monthlyRepeatBy) ? draft.monthlyRepeatBy : "monthDay";
const monthlyRepeatBy = normalizeDraftMonthlyRepeatBy(draft.monthlyRepeatBy);
const monthRepeat = monthlyRepeatBy === "monthDay" && draft.repeat === "weekly" ? "none" : ["none", "weekly", "monthly"].includes(draft.repeat) ? draft.repeat : "monthly";
const validWeekTarget = data.viewMode === "week" && dayIndex >= 0 && dayIndex <= 6;
const validMonthTarget = data.viewMode === "month" && startDate && endDate && endDate >= startDate;