Split calendar module into form and views
All checks were successful
Deploy Sokko G / deploy (push) Successful in 15s

This commit is contained in:
Shinuwa 2026-08-24 20:29:57 +02:00
parent c81f66c747
commit 233f81541d
4 changed files with 774 additions and 619 deletions

View file

@ -0,0 +1,302 @@
// Rôle : rend les vues calendrier semaine et mois avec occurrences, sélection et ligne horaire courante.
import { Tooltip } from "../../../components/Tooltip.jsx";
import {
WEEK_SLOT_MINUTES,
addDays,
formatLocalDateKey,
minutesToTimeValue,
pad2
} from "./calendarUtils.js";
import {
HOUR_HALF_SLOT_COUNT,
WEEK_COMPACT_EVENT_HEIGHT,
WEEKDAY_LABELS,
getMonthOccurrenceSegments,
getWeekMinutePosition,
layoutConcurrentOccurrences
} from "./calendarLayout.js";
function formatDayLabel(day) {
return day.toLocaleDateString(undefined, { weekday: "long" });
}
function formatHourLabel(hourIndex) {
const minutes = hourIndex * 60;
return `${pad2(Math.floor(minutes / 60))}:${pad2(minutes % 60)}`;
}
function getSelectionRange(selection) {
if (!selection) return null;
const startSlot = Math.min(selection.startSlot, selection.endSlot);
const endSlot = Math.max(selection.startSlot, selection.endSlot);
return { dayIndex: selection.dayIndex, startSlot, endSlot };
}
function isSlotSelected(selection, dayIndex, slotIndex) {
const range = getSelectionRange(selection);
return Boolean(range && range.dayIndex === dayIndex && slotIndex >= range.startSlot && slotIndex <= range.endSlot);
}
function getMonthSelectionRange(selection) {
if (!selection) return null;
const startDate = selection.startDate <= selection.endDate ? selection.startDate : selection.endDate;
const endDate = selection.startDate <= selection.endDate ? selection.endDate : selection.startDate;
return { startDate, endDate };
}
function isMonthDateSelected(selection, dateKey) {
const range = getMonthSelectionRange(selection);
return Boolean(range && dateKey >= range.startDate && dateKey <= range.endDate);
}
function formatMonthTitle(dateLike) {
return new Date(dateLike).toLocaleDateString(undefined, { month: "long", year: "numeric" });
}
export function CalendarWeekGrid({
days,
occurrences,
textContent,
activeEventId,
formOpen,
draftSelection,
selection,
weekGridLayout,
nowDayIndex,
nowMinutes,
onSlotPointerDown,
onSlotPointerEnter,
onSlotPointerUp,
onEditEvent
}) {
return (
<section className="calendar-week-grid" aria-label={textContent.gridLabel || "Calendrier hebdomadaire"}>
<div className="calendar-time-header" />
{days.map((day) => <div key={day.toISOString()} className="calendar-day-header">{formatDayLabel(day)}</div>)}
<div className="calendar-time-column" style={{ gridTemplateRows: weekGridLayout.gridTemplateRows }}>
{weekGridLayout.rows.map((row) => (
row.type === "hidden"
? <span key={`hidden:${row.startHour}`} className="calendar-hidden-hours-label">...</span>
: <span key={row.hourIndex} className="calendar-time-row">{formatHourLabel(row.hourIndex)}</span>
))}
</div>
{days.map((day, dayIndex) => {
const dayOccurrences = layoutConcurrentOccurrences(occurrences.filter((occurrence) => occurrence.event.dayIndex === dayIndex));
const isToday = dayIndex === nowDayIndex;
return (
<div key={day.toISOString()} className={`calendar-day-column ${isToday ? "is-today" : ""}`} style={{ gridTemplateRows: weekGridLayout.gridTemplateRows }}>
{weekGridLayout.rows.map((row) => (
row.type === "hidden" ? (
<div key={`hidden:${row.startHour}`} className="calendar-hidden-hours-cell" aria-hidden="true">
{dayIndex === 3 && <span>{`${formatHourLabel(row.startHour)} - ${formatHourLabel(row.endHour + 1)}`}</span>}
</div>
) : (
<div key={row.hourIndex} className="calendar-hour-cell">
{Array.from({ length: HOUR_HALF_SLOT_COUNT }, (_, halfIndex) => {
const slotIndex = row.hourIndex * HOUR_HALF_SLOT_COUNT + halfIndex;
const slotLabel = minutesToTimeValue(slotIndex * WEEK_SLOT_MINUTES);
return (
<button
key={slotIndex}
type="button"
className={`calendar-slot ${formOpen && isSlotSelected(draftSelection, dayIndex, slotIndex) ? "is-form-selected" : ""} ${isSlotSelected(selection, dayIndex, slotIndex) ? "is-selected" : ""}`}
onPointerDown={() => onSlotPointerDown(dayIndex, slotIndex)}
onPointerEnter={(event) => onSlotPointerEnter(event, dayIndex, slotIndex)}
onPointerUp={onSlotPointerUp}
aria-label={`${formatDayLabel(day)} ${slotLabel}`}
/>
);
})}
</div>
)
))}
<div className="calendar-events-layer">
{dayOccurrences.map((occurrence) => (
<CalendarOccurrence
key={occurrence.id}
occurrence={occurrence}
layout={weekGridLayout}
active={activeEventId === occurrence.event.id}
onEdit={() => onEditEvent(occurrence.event)}
/>
))}
</div>
{isToday && (
<div className="calendar-now-line" style={{ top: `${getWeekMinutePosition(weekGridLayout, nowMinutes)}px` }}>
<span>{minutesToTimeValue(nowMinutes)}</span>
</div>
)}
</div>
);
})}
</section>
);
}
function CalendarOccurrence({ occurrence, layout, active, onEdit }) {
const durationMinutes = Math.max(WEEK_SLOT_MINUTES, (occurrence.endMs - occurrence.startMs) / 60000 || WEEK_SLOT_MINUTES);
const startMinutes = occurrence.event.startMinutes;
const timeLabel = `${minutesToTimeValue(occurrence.event.startMinutes)}${occurrence.event.endMinutes ? ` - ${minutesToTimeValue(occurrence.event.endMinutes)}` : ""}`;
const compact = durationMinutes < 120;
const top = getWeekMinutePosition(layout, startMinutes);
const bottom = getWeekMinutePosition(layout, Math.min(24 * 60, startMinutes + durationMinutes));
const height = compact ? WEEK_COMPACT_EVENT_HEIGHT : Math.max(WEEK_COMPACT_EVENT_HEIGHT, bottom - top);
return (
<article
className={`calendar-event calendar-event-${occurrence.event.color || "violet"} ${active ? "is-active" : ""} ${compact ? "is-compact" : ""}`}
style={{
top: `${top}px`,
height: `${height}px`,
"--calendar-event-lane": occurrence.lane || 0,
"--calendar-event-lane-count": occurrence.laneCount || 1
}}
>
<Tooltip
as="button"
type="button"
className="calendar-event-button"
tooltipClassName="calendar-event-tooltip"
content={<><strong>{occurrence.event.title}</strong><span>{timeLabel}</span></>}
onClick={onEdit}
>
<strong>{occurrence.event.title}</strong>
{!compact && <span>{timeLabel}</span>}
</Tooltip>
</article>
);
}
export function CalendarMonthView({
monthStart,
monthGridDays,
occurrences,
textContent,
activeEventId,
selectedRange,
dragSelection,
todayDate,
nowLabel,
onPreviousMonth,
onCurrentMonth,
onNextMonth,
onPointerDown,
onPointerEnter,
onPointerUp,
onEditEvent
}) {
return (
<>
<div className="calendar-month-toolbar">
<button type="button" onClick={onPreviousMonth} title={textContent.previousMonthTitle || "Mois précédent"} aria-label={textContent.previousMonthTitle || "Mois précédent"}>{textContent.previousShortLabel || "Prec."}</button>
<strong>{formatMonthTitle(monthStart)}</strong>
<button type="button" onClick={onCurrentMonth}>{textContent.currentMonthTitle || "Mois courant"}</button>
<button type="button" onClick={onNextMonth} title={textContent.nextMonthTitle || "Mois suivant"} aria-label={textContent.nextMonthTitle || "Mois suivant"}>{textContent.nextShortLabel || "Suiv."}</button>
</div>
<CalendarMonthGrid
days={monthGridDays}
occurrences={occurrences}
textContent={textContent}
activeEventId={activeEventId}
selectedRange={selectedRange}
dragSelection={dragSelection}
todayDate={todayDate}
nowLabel={nowLabel}
onPointerDown={onPointerDown}
onPointerEnter={onPointerEnter}
onPointerUp={onPointerUp}
onEditEvent={onEditEvent}
/>
</>
);
}
function CalendarMonthGrid({ days, occurrences, textContent, activeEventId, selectedRange, dragSelection, todayDate, nowLabel, onPointerDown, onPointerEnter, onPointerUp, onEditEvent }) {
const monthBars = getMonthOccurrenceSegments(days, occurrences);
const weekRowCount = Math.ceil(days.length / WEEKDAY_LABELS.length);
return (
<section className="calendar-month-grid" style={{ "--calendar-month-week-count": weekRowCount }} aria-label={textContent.monthGridLabel || "Calendrier mensuel"}>
{WEEKDAY_LABELS.map((day) => <div key={day} className="calendar-month-header">{day}</div>)}
{days.map((day) => {
const dateKey = formatLocalDateKey(day.date);
const isToday = todayDate === dateKey;
return (
<div
key={day.date.toISOString()}
className={`calendar-month-cell ${day.inMonth ? "" : "is-outside"} ${day.inMonth && isMonthDateSelected(selectedRange, dateKey) ? "is-form-selected" : ""} ${day.inMonth && isMonthDateSelected(dragSelection, dateKey) ? "is-selected" : ""} ${day.inMonth && isToday ? "is-today" : ""}`}
role={day.inMonth ? "button" : undefined}
tabIndex={day.inMonth ? 0 : undefined}
onPointerDown={() => day.inMonth && onPointerDown(dateKey)}
onPointerEnter={(event) => day.inMonth && onPointerEnter(event, dateKey)}
onPointerUp={() => day.inMonth && onPointerUp()}
onKeyDown={(event) => {
if (!day.inMonth || (event.key !== "Enter" && event.key !== " ")) return;
event.preventDefault();
onPointerDown(dateKey);
onPointerUp();
}}
aria-label={day.inMonth ? `${textContent.dayLabel || "Jour"} ${day.dayNumber}` : undefined}
>
{day.inMonth && (
<>
<span className="calendar-month-day-number">{day.dayNumber}</span>
{isToday && <span className="calendar-month-now-indicator">{nowLabel}</span>}
</>
)}
</div>
);
})}
<div className="calendar-month-bars-layer" aria-hidden={monthBars.length ? undefined : true}>
{monthBars.map((segment, index) => (
<CalendarMonthBar
key={`${segment.occurrence.id}:${index}`}
segment={segment}
active={activeEventId === segment.occurrence.event.id}
onEdit={(event) => {
event.stopPropagation();
onEditEvent(segment.occurrence.event, segment.occurrence);
}}
/>
))}
</div>
</section>
);
}
function CalendarMonthBar({ segment, active, onEdit }) {
const gridRow = Math.floor(segment.startIndex / WEEKDAY_LABELS.length) + 2;
const startColumn = segment.startIndex % WEEKDAY_LABELS.length + 1;
const endColumn = segment.endIndex % WEEKDAY_LABELS.length + 2;
const laneCount = Math.max(1, Math.min(3, segment.laneCount || 1));
const barHeight = laneCount === 1 ? 28 : laneCount === 2 ? 23 : 18;
const barGap = laneCount === 3 ? 3 : 4;
const startLabel = new Date(segment.occurrence.startMs).toLocaleDateString(undefined, { day: "2-digit", month: "short", year: "numeric" });
const endDate = addDays(new Date(segment.occurrence.endMs), -1);
const endLabel = endDate.toLocaleDateString(undefined, { day: "2-digit", month: "short", year: "numeric" });
const dateLabel = startLabel === endLabel ? startLabel : `${startLabel} - ${endLabel}`;
return (
<article
className={`calendar-month-bar calendar-event-${segment.occurrence.event.color || "violet"} ${active ? "is-active" : ""}`}
style={{
gridColumn: `${startColumn} / ${endColumn}`,
gridRow,
"--calendar-event-lane": segment.lane || 0,
"--calendar-month-bar-height": `${barHeight}px`,
"--calendar-month-bar-gap": `${barGap}px`
}}
>
<Tooltip
as="button"
type="button"
className="calendar-month-bar-button"
tooltipClassName="calendar-event-tooltip"
content={<><strong>{segment.occurrence.event.title}</strong><span>{dateLabel}</span></>}
onPointerDown={(event) => event.stopPropagation()}
onClick={onEdit}
>
<strong>{segment.occurrence.event.title}</strong>
</Tooltip>
</article>
);
}