@jsb188/calendar 1.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +44 -0
- package/src/constants/calendar.ts +139 -0
- package/src/css.d.ts +7 -0
- package/src/libs/calendar-geometry.ts +426 -0
- package/src/libs/calendar-gesture.ts +298 -0
- package/src/libs/calendar-range.ts +340 -0
- package/src/libs/calendar-state.ts +161 -0
- package/src/libs/calendar-url.ts +144 -0
- package/src/main.ts +29 -0
- package/src/modules/Calendar.tsx +648 -0
- package/src/types/calendar.d.ts +122 -0
- package/src/ui/CalendarAgendaUI.tsx +86 -0
- package/src/ui/CalendarCoverageUI.tsx +199 -0
- package/src/ui/CalendarEventDetailUI.tsx +260 -0
- package/src/ui/CalendarEventFormUI.tsx +239 -0
- package/src/ui/CalendarMonthUI.tsx +130 -0
- package/src/ui/CalendarRailUI.tsx +282 -0
- package/src/ui/CalendarRequestUI.tsx +537 -0
- package/src/ui/CalendarToolbarUI.tsx +110 -0
- package/src/ui/CalendarUI.tsx +347 -0
- package/src/ui/CalendarViolationUI.tsx +207 -0
- package/src/ui/CalendarYearUI.tsx +81 -0
- package/tests/calendar-geometry.test.ts +403 -0
- package/tests/calendar-gesture.test.ts +309 -0
- package/tests/calendar-range.test.ts +232 -0
- package/tests/calendar-url.test.ts +124 -0
- package/tsconfig.json +13 -0
- package/vitest.config.ts +11 -0
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CALENDAR_DEFAULT_NEW_EVENT_MINUTES,
|
|
3
|
+
CALENDAR_DRAG_THRESHOLD_PX,
|
|
4
|
+
CALENDAR_MIN_EVENT_MINUTES,
|
|
5
|
+
CALENDAR_SNAP_MINUTES,
|
|
6
|
+
} from '../constants/calendar.ts';
|
|
7
|
+
import type { CalendarDayObj } from '../types/calendar.d.ts';
|
|
8
|
+
import { getDayLengthMinutes, getInstantFromDayMinutes, snapMinutes } from './calendar-range.ts';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The calendar's pointer gesture engine.
|
|
12
|
+
*
|
|
13
|
+
* Shaped after the Sheet's grid-selection-move: the gesture is a closure with
|
|
14
|
+
* window-level listeners, filtered by pointerId, torn down exactly once
|
|
15
|
+
* whether it was released or cancelled. Nothing here touches React — the
|
|
16
|
+
* in-flight gesture is a closure and a ref, never state, because a pointermove
|
|
17
|
+
* handler that set state would re-render every event in the week at pointer
|
|
18
|
+
* frequency.
|
|
19
|
+
*
|
|
20
|
+
* The time math is separated out into pure functions above the gesture so it
|
|
21
|
+
* can be tested without a DOM.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
export type CalendarGestureKind = 'CREATE' | 'MOVE' | 'RESIZE_START' | 'RESIZE_END';
|
|
25
|
+
|
|
26
|
+
export interface CalendarGestureRange {
|
|
27
|
+
startAt: Date;
|
|
28
|
+
endAt: Date;
|
|
29
|
+
dayIndex: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Convert a pointer's position over the grid into a day index and a snapped
|
|
34
|
+
* minute offset within that day.
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
export function getCalendarPointerSlot(p: {
|
|
38
|
+
// Pointer position relative to the scrolling grid's content box
|
|
39
|
+
offsetX: number;
|
|
40
|
+
offsetY: number;
|
|
41
|
+
contentWidth: number;
|
|
42
|
+
hourHeight: number;
|
|
43
|
+
days: CalendarDayObj[];
|
|
44
|
+
}): { dayIndex: number; minute: number } {
|
|
45
|
+
const { offsetX, offsetY, contentWidth, hourHeight, days } = p;
|
|
46
|
+
|
|
47
|
+
const columnWidth = contentWidth / Math.max(1, days.length);
|
|
48
|
+
const dayIndex = Math.max(0, Math.min(days.length - 1, Math.floor(offsetX / Math.max(1, columnWidth))));
|
|
49
|
+
const dayLengthMinutes = getDayLengthMinutes(days[dayIndex]);
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
dayIndex,
|
|
53
|
+
minute: snapMinutes((offsetY / hourHeight) * 60, CALENDAR_SNAP_MINUTES, dayLengthMinutes),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Build the time range a CREATE gesture describes.
|
|
59
|
+
*
|
|
60
|
+
* A press that never moved is a click, and a click means "an event of the
|
|
61
|
+
* default length starting here" — not a zero-length event, which is what a
|
|
62
|
+
* naive drag implementation produces because no hand is perfectly still.
|
|
63
|
+
*/
|
|
64
|
+
|
|
65
|
+
export function getCalendarCreateRange(p: {
|
|
66
|
+
days: CalendarDayObj[];
|
|
67
|
+
anchorDayIndex: number;
|
|
68
|
+
anchorMinute: number;
|
|
69
|
+
currentDayIndex: number;
|
|
70
|
+
currentMinute: number;
|
|
71
|
+
moved: boolean;
|
|
72
|
+
}): CalendarGestureRange {
|
|
73
|
+
const { days, anchorDayIndex, anchorMinute, currentMinute, moved } = p;
|
|
74
|
+
|
|
75
|
+
// Creation stays in the column it began in: dragging sideways while drawing
|
|
76
|
+
// a block is almost always a wobble, not a request for a multi-day event
|
|
77
|
+
const day = days[anchorDayIndex];
|
|
78
|
+
const dayLengthMinutes = getDayLengthMinutes(day);
|
|
79
|
+
|
|
80
|
+
if (!moved) {
|
|
81
|
+
const startMinute = Math.min(anchorMinute, dayLengthMinutes - CALENDAR_DEFAULT_NEW_EVENT_MINUTES);
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
dayIndex: anchorDayIndex,
|
|
85
|
+
startAt: getInstantFromDayMinutes(day.startAt, Math.max(0, startMinute)),
|
|
86
|
+
endAt: getInstantFromDayMinutes(
|
|
87
|
+
day.startAt,
|
|
88
|
+
Math.min(dayLengthMinutes, Math.max(0, startMinute) + CALENDAR_DEFAULT_NEW_EVENT_MINUTES),
|
|
89
|
+
),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Dragging upward is a legitimate way to draw a block, so the anchor is
|
|
94
|
+
// whichever end is earlier rather than whichever came first
|
|
95
|
+
const startMinute = Math.min(anchorMinute, currentMinute);
|
|
96
|
+
const endMinute = Math.max(anchorMinute, currentMinute);
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
dayIndex: anchorDayIndex,
|
|
100
|
+
startAt: getInstantFromDayMinutes(day.startAt, startMinute),
|
|
101
|
+
endAt: getInstantFromDayMinutes(
|
|
102
|
+
day.startAt,
|
|
103
|
+
Math.min(dayLengthMinutes, Math.max(endMinute, startMinute + CALENDAR_MIN_EVENT_MINUTES)),
|
|
104
|
+
),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Build the time range a MOVE gesture describes.
|
|
110
|
+
*
|
|
111
|
+
* The event keeps its duration and is offset by how far the pointer travelled
|
|
112
|
+
* from where it grabbed the block — not snapped to the pointer itself, which
|
|
113
|
+
* would make the block jump under the finger on the first pixel of movement.
|
|
114
|
+
*/
|
|
115
|
+
|
|
116
|
+
export function getCalendarMoveRange(p: {
|
|
117
|
+
days: CalendarDayObj[];
|
|
118
|
+
startAt: Date;
|
|
119
|
+
endAt: Date;
|
|
120
|
+
// Minutes between the grab point and the event's start
|
|
121
|
+
grabOffsetMinutes: number;
|
|
122
|
+
currentDayIndex: number;
|
|
123
|
+
currentMinute: number;
|
|
124
|
+
}): CalendarGestureRange {
|
|
125
|
+
const { days, startAt, endAt, grabOffsetMinutes, currentDayIndex, currentMinute } = p;
|
|
126
|
+
|
|
127
|
+
const day = days[currentDayIndex];
|
|
128
|
+
const dayLengthMinutes = getDayLengthMinutes(day);
|
|
129
|
+
const durationMs = endAt.getTime() - startAt.getTime();
|
|
130
|
+
const durationMinutes = durationMs / 60_000;
|
|
131
|
+
|
|
132
|
+
// Clamped so a drag near the bottom of the column cannot push the event
|
|
133
|
+
// past midnight and silently turn it into an overnight shift
|
|
134
|
+
const rawStart = currentMinute - grabOffsetMinutes;
|
|
135
|
+
const startMinute = snapMinutes(
|
|
136
|
+
Math.max(0, Math.min(rawStart, dayLengthMinutes - durationMinutes)),
|
|
137
|
+
CALENDAR_SNAP_MINUTES,
|
|
138
|
+
dayLengthMinutes,
|
|
139
|
+
);
|
|
140
|
+
const nextStart = getInstantFromDayMinutes(day.startAt, startMinute);
|
|
141
|
+
|
|
142
|
+
return {
|
|
143
|
+
dayIndex: currentDayIndex,
|
|
144
|
+
startAt: nextStart,
|
|
145
|
+
// Duration is preserved in ABSOLUTE time, so moving a shift onto a DST
|
|
146
|
+
// day keeps it the same number of hours long rather than the same
|
|
147
|
+
// wall-clock span
|
|
148
|
+
endAt: new Date(nextStart.getTime() + durationMs),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Build the time range a RESIZE gesture describes.
|
|
154
|
+
*
|
|
155
|
+
* The edge being dragged moves; the other stays put. A resize that would
|
|
156
|
+
* invert or collapse the event is clamped to the minimum length instead,
|
|
157
|
+
* because an event whose end precedes its start is not something the user
|
|
158
|
+
* meant to ask for.
|
|
159
|
+
*/
|
|
160
|
+
|
|
161
|
+
export function getCalendarResizeRange(p: {
|
|
162
|
+
days: CalendarDayObj[];
|
|
163
|
+
dayIndex: number;
|
|
164
|
+
startAt: Date;
|
|
165
|
+
endAt: Date;
|
|
166
|
+
edge: 'START' | 'END';
|
|
167
|
+
currentMinute: number;
|
|
168
|
+
}): CalendarGestureRange {
|
|
169
|
+
const { days, dayIndex, startAt, endAt, edge, currentMinute } = p;
|
|
170
|
+
|
|
171
|
+
const day = days[dayIndex];
|
|
172
|
+
const dayLengthMinutes = getDayLengthMinutes(day);
|
|
173
|
+
const dayStartMs = day.startAt.getTime();
|
|
174
|
+
|
|
175
|
+
if (edge === 'START') {
|
|
176
|
+
const endMinute = (endAt.getTime() - dayStartMs) / 60_000;
|
|
177
|
+
const startMinute = Math.min(currentMinute, endMinute - CALENDAR_MIN_EVENT_MINUTES);
|
|
178
|
+
|
|
179
|
+
return {
|
|
180
|
+
dayIndex,
|
|
181
|
+
startAt: getInstantFromDayMinutes(day.startAt, Math.max(0, startMinute)),
|
|
182
|
+
endAt,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const startMinute = (startAt.getTime() - dayStartMs) / 60_000;
|
|
187
|
+
const endMinute = Math.max(currentMinute, startMinute + CALENDAR_MIN_EVENT_MINUTES);
|
|
188
|
+
|
|
189
|
+
return {
|
|
190
|
+
dayIndex,
|
|
191
|
+
startAt,
|
|
192
|
+
endAt: getInstantFromDayMinutes(day.startAt, Math.min(dayLengthMinutes, endMinute)),
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export interface CalendarGestureParams {
|
|
197
|
+
kind: CalendarGestureKind;
|
|
198
|
+
pointerId: number;
|
|
199
|
+
// Client coordinates where the gesture began, for the movement threshold
|
|
200
|
+
originClientX: number;
|
|
201
|
+
originClientY: number;
|
|
202
|
+
// Called on every movement past the threshold, and once on release
|
|
203
|
+
onPreview: (range: CalendarGestureRange | null) => void;
|
|
204
|
+
// Called on release only when the gesture was not cancelled
|
|
205
|
+
onCommit: (range: CalendarGestureRange, moved: boolean) => void;
|
|
206
|
+
// Resolve the range for the pointer's current client position
|
|
207
|
+
resolveRange: (clientX: number, clientY: number, moved: boolean) => CalendarGestureRange | null;
|
|
208
|
+
onFinish?: () => void;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Start one calendar pointer gesture.
|
|
213
|
+
*
|
|
214
|
+
* Returns a cleanup function that is safe to call more than once; the gesture
|
|
215
|
+
* also calls it itself on release or cancel. Listeners live on the window so a
|
|
216
|
+
* drag that leaves the grid — which every drag does at the edges — keeps
|
|
217
|
+
* tracking rather than freezing at the boundary.
|
|
218
|
+
*/
|
|
219
|
+
|
|
220
|
+
export function startCalendarGesture(params: CalendarGestureParams): () => void {
|
|
221
|
+
let finished = false;
|
|
222
|
+
let moved = false;
|
|
223
|
+
|
|
224
|
+
/*
|
|
225
|
+
* Remove every global side effect exactly once, whether the gesture was
|
|
226
|
+
* released or cancelled.
|
|
227
|
+
*/
|
|
228
|
+
|
|
229
|
+
function cleanup() {
|
|
230
|
+
if (finished) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
finished = true;
|
|
235
|
+
params.onPreview(null);
|
|
236
|
+
globalThis.removeEventListener('pointermove', onPointerMove);
|
|
237
|
+
globalThis.removeEventListener('pointerup', onPointerUp);
|
|
238
|
+
globalThis.removeEventListener('pointercancel', onPointerCancel);
|
|
239
|
+
params.onFinish?.();
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function onPointerMove(event: PointerEvent) {
|
|
243
|
+
if (event.pointerId !== params.pointerId) {
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (!moved) {
|
|
248
|
+
const travelled = Math.hypot(
|
|
249
|
+
event.clientX - params.originClientX,
|
|
250
|
+
event.clientY - params.originClientY,
|
|
251
|
+
);
|
|
252
|
+
|
|
253
|
+
if (travelled < CALENDAR_DRAG_THRESHOLD_PX) {
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
moved = true;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
params.onPreview(params.resolveRange(event.clientX, event.clientY, true));
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function finish(event: PointerEvent, commit: boolean) {
|
|
264
|
+
if (event.pointerId !== params.pointerId) {
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (commit) {
|
|
269
|
+
const range = params.resolveRange(event.clientX, event.clientY, moved);
|
|
270
|
+
|
|
271
|
+
if (range) {
|
|
272
|
+
// Committed BEFORE cleanup, so the commit handler can still read
|
|
273
|
+
// whatever the gesture was showing
|
|
274
|
+
params.onCommit(range, moved);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
cleanup();
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function onPointerUp(event: PointerEvent) {
|
|
282
|
+
finish(event, true);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/*
|
|
286
|
+
* Discard one browser-cancelled pointer stream.
|
|
287
|
+
*/
|
|
288
|
+
|
|
289
|
+
function onPointerCancel(event: PointerEvent) {
|
|
290
|
+
finish(event, false);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
globalThis.addEventListener('pointermove', onPointerMove);
|
|
294
|
+
globalThis.addEventListener('pointerup', onPointerUp);
|
|
295
|
+
globalThis.addEventListener('pointercancel', onPointerCancel);
|
|
296
|
+
|
|
297
|
+
return cleanup;
|
|
298
|
+
}
|
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
import { DEFAULT_TIMEZONE, isValidTimeZone } from '@jsb188/app/utils/timeZone.ts';
|
|
2
|
+
import { DateTime } from 'luxon';
|
|
3
|
+
import {
|
|
4
|
+
CALENDAR_AGENDA_DAY_COUNT,
|
|
5
|
+
CALENDAR_DATE_PARAM_FORMAT,
|
|
6
|
+
CALENDAR_VIEW_DAY_COUNTS,
|
|
7
|
+
type CalendarViewEnum,
|
|
8
|
+
} from '../constants/calendar.ts';
|
|
9
|
+
import type { CalendarDayObj, CalendarRangeObj } from '../types/calendar.d.ts';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Calendar range math.
|
|
13
|
+
*
|
|
14
|
+
* Everything here is pure and takes its zone explicitly. A calendar is the one
|
|
15
|
+
* surface where "today" is genuinely ambiguous — the organization's day, the
|
|
16
|
+
* viewer's day, and UTC's day are three different things, and a shift that
|
|
17
|
+
* starts at 11pm belongs to whichever of them the calendar is drawn in. So
|
|
18
|
+
* nothing in this file reads a system zone or a system clock; both arrive as
|
|
19
|
+
* arguments and every date is built through luxon in the given zone.
|
|
20
|
+
*
|
|
21
|
+
* Days are half-open [start, end). A day boundary is not 23:59:59.999 — that
|
|
22
|
+
* loses the last millisecond, and over a DST fall-back day it loses an entire
|
|
23
|
+
* repeated hour.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/*
|
|
27
|
+
* Resolve a usable IANA zone, falling back rather than throwing: a calendar
|
|
28
|
+
* that renders in the wrong zone is a bug, but one that renders nothing
|
|
29
|
+
* because a setting is stale is worse.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
function getCalendarZone(timeZone?: string | null): string {
|
|
33
|
+
return timeZone && isValidTimeZone(timeZone) ? timeZone : DEFAULT_TIMEZONE;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Return the local date key ("yyyy-MM-dd") one instant falls on, in a zone.
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
export function getCalendarDateKey(instant: Date, timeZone?: string | null): string {
|
|
41
|
+
return DateTime.fromJSDate(instant, { zone: getCalendarZone(timeZone) }).toFormat(CALENDAR_DATE_PARAM_FORMAT);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Parse a "yyyy-MM-dd" URL parameter into the local start of that day.
|
|
46
|
+
*
|
|
47
|
+
* Anything unparseable falls back to the supplied "now" rather than to the
|
|
48
|
+
* epoch: a mistyped URL should land the user on today, not in 1970.
|
|
49
|
+
*/
|
|
50
|
+
|
|
51
|
+
export function parseCalendarDateParam(
|
|
52
|
+
value: string | null | undefined,
|
|
53
|
+
now: Date,
|
|
54
|
+
timeZone?: string | null,
|
|
55
|
+
): Date {
|
|
56
|
+
const zone = getCalendarZone(timeZone);
|
|
57
|
+
|
|
58
|
+
if (value) {
|
|
59
|
+
const parsed = DateTime.fromFormat(value, CALENDAR_DATE_PARAM_FORMAT, { zone });
|
|
60
|
+
|
|
61
|
+
if (parsed.isValid) {
|
|
62
|
+
return parsed.startOf('day').toJSDate();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return DateTime.fromJSDate(now, { zone }).startOf('day').toJSDate();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/*
|
|
70
|
+
* Build one day descriptor.
|
|
71
|
+
*/
|
|
72
|
+
|
|
73
|
+
function buildCalendarDay(
|
|
74
|
+
dayStart: DateTime,
|
|
75
|
+
todayKey: string,
|
|
76
|
+
isInPrimaryRange: boolean,
|
|
77
|
+
): CalendarDayObj {
|
|
78
|
+
return {
|
|
79
|
+
dateKey: dayStart.toFormat(CALENDAR_DATE_PARAM_FORMAT),
|
|
80
|
+
startAt: dayStart.toJSDate(),
|
|
81
|
+
// Adding a calendar day rather than 24 hours: across a DST boundary the
|
|
82
|
+
// day is 23 or 25 hours long, and the grid has to agree with the clock
|
|
83
|
+
endAt: dayStart.plus({ days: 1 }).toJSDate(),
|
|
84
|
+
isToday: dayStart.toFormat(CALENDAR_DATE_PARAM_FORMAT) === todayKey,
|
|
85
|
+
// Luxon weekdays are 1 (Monday) through 7 (Sunday)
|
|
86
|
+
isWeekend: dayStart.weekday >= 6,
|
|
87
|
+
isInPrimaryRange,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/*
|
|
92
|
+
* Build a consecutive run of days starting at one local midnight.
|
|
93
|
+
*/
|
|
94
|
+
|
|
95
|
+
function buildConsecutiveDays(
|
|
96
|
+
firstDay: DateTime,
|
|
97
|
+
dayCount: number,
|
|
98
|
+
todayKey: string,
|
|
99
|
+
): CalendarDayObj[] {
|
|
100
|
+
const days: CalendarDayObj[] = [];
|
|
101
|
+
|
|
102
|
+
for (let index = 0; index < dayCount; index++) {
|
|
103
|
+
days.push(buildCalendarDay(firstDay.plus({ days: index }), todayKey, true));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return days;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/*
|
|
110
|
+
* Return the first day of the week containing an instant.
|
|
111
|
+
*
|
|
112
|
+
* weekStartsOn is 0 for Sunday through 6 for Saturday, matching the rest of
|
|
113
|
+
* the app's day-of-week convention rather than luxon's 1-7.
|
|
114
|
+
*/
|
|
115
|
+
|
|
116
|
+
function getWeekStart(day: DateTime, weekStartsOn: number): DateTime {
|
|
117
|
+
// luxon: 1 = Monday .. 7 = Sunday; normalize to 0 = Sunday .. 6 = Saturday
|
|
118
|
+
const currentDayOfWeek = day.weekday % 7;
|
|
119
|
+
const offset = (currentDayOfWeek - weekStartsOn + 7) % 7;
|
|
120
|
+
|
|
121
|
+
return day.minus({ days: offset });
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/*
|
|
125
|
+
* Build the six-week grid a month view draws, including the leading and
|
|
126
|
+
* trailing days of the neighbouring months.
|
|
127
|
+
*
|
|
128
|
+
* Always six weeks, never five: a grid that changes height between months
|
|
129
|
+
* makes every event jump, and the row heights would have to be recomputed on
|
|
130
|
+
* each navigation.
|
|
131
|
+
*/
|
|
132
|
+
|
|
133
|
+
function buildMonthDays(anchor: DateTime, weekStartsOn: number, todayKey: string): CalendarDayObj[] {
|
|
134
|
+
const monthStart = anchor.startOf('month');
|
|
135
|
+
const monthEnd = anchor.endOf('month');
|
|
136
|
+
const gridStart = getWeekStart(monthStart, weekStartsOn);
|
|
137
|
+
const days: CalendarDayObj[] = [];
|
|
138
|
+
|
|
139
|
+
for (let index = 0; index < 42; index++) {
|
|
140
|
+
const day = gridStart.plus({ days: index });
|
|
141
|
+
|
|
142
|
+
days.push(buildCalendarDay(
|
|
143
|
+
day,
|
|
144
|
+
todayKey,
|
|
145
|
+
day >= monthStart && day <= monthEnd,
|
|
146
|
+
));
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return days;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/*
|
|
153
|
+
* Build every day of one year.
|
|
154
|
+
*
|
|
155
|
+
* The year view is a NAVIGATION surface, not a display one: twelve mini month
|
|
156
|
+
* grids you click to jump into. It deliberately does not draw events, so this
|
|
157
|
+
* only has to produce the days — which is also why the page skips the
|
|
158
|
+
* occurrence query entirely for this view rather than asking for a year of
|
|
159
|
+
* shifts and silently truncating them at the window limit.
|
|
160
|
+
*/
|
|
161
|
+
|
|
162
|
+
function buildYearDays(anchor: DateTime, todayKey: string): CalendarDayObj[] {
|
|
163
|
+
const yearStart = anchor.startOf('year');
|
|
164
|
+
const dayCount = yearStart.isInLeapYear ? 366 : 365;
|
|
165
|
+
|
|
166
|
+
return buildConsecutiveDays(yearStart, dayCount, todayKey);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Build the range one view is showing.
|
|
171
|
+
*
|
|
172
|
+
* The returned "from" and "to" are what the window query asks for, so they
|
|
173
|
+
* cover every day the view DRAWS — a month grid queries its leading and
|
|
174
|
+
* trailing days too, or the first row would render empty until the user
|
|
175
|
+
* navigated to the previous month.
|
|
176
|
+
*/
|
|
177
|
+
|
|
178
|
+
export function getCalendarRange(p: {
|
|
179
|
+
view: CalendarViewEnum;
|
|
180
|
+
anchorDate: Date;
|
|
181
|
+
now: Date;
|
|
182
|
+
timeZone?: string | null;
|
|
183
|
+
// 0 = Sunday .. 6 = Saturday
|
|
184
|
+
weekStartsOn?: number;
|
|
185
|
+
}): CalendarRangeObj {
|
|
186
|
+
const { view, anchorDate, now } = p;
|
|
187
|
+
const timeZone = getCalendarZone(p.timeZone);
|
|
188
|
+
const weekStartsOn = Number.isInteger(p.weekStartsOn) ? Number(p.weekStartsOn) : 0;
|
|
189
|
+
const anchor = DateTime.fromJSDate(anchorDate, { zone: timeZone }).startOf('day');
|
|
190
|
+
const todayKey = DateTime.fromJSDate(now, { zone: timeZone }).toFormat(CALENDAR_DATE_PARAM_FORMAT);
|
|
191
|
+
|
|
192
|
+
const days = getCalendarViewDays({ view, anchor, weekStartsOn, todayKey });
|
|
193
|
+
|
|
194
|
+
return {
|
|
195
|
+
view,
|
|
196
|
+
anchorDate: anchor.toJSDate(),
|
|
197
|
+
from: days[0].startAt,
|
|
198
|
+
to: days[days.length - 1].endAt,
|
|
199
|
+
days,
|
|
200
|
+
timeZone,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/*
|
|
205
|
+
* Return the days one view draws.
|
|
206
|
+
*/
|
|
207
|
+
|
|
208
|
+
function getCalendarViewDays(p: {
|
|
209
|
+
view: CalendarViewEnum;
|
|
210
|
+
anchor: DateTime;
|
|
211
|
+
weekStartsOn: number;
|
|
212
|
+
todayKey: string;
|
|
213
|
+
}): CalendarDayObj[] {
|
|
214
|
+
const { view, anchor, weekStartsOn, todayKey } = p;
|
|
215
|
+
|
|
216
|
+
switch (view) {
|
|
217
|
+
case 'WEEK':
|
|
218
|
+
return buildConsecutiveDays(getWeekStart(anchor, weekStartsOn), 7, todayKey);
|
|
219
|
+
case 'MONTH':
|
|
220
|
+
return buildMonthDays(anchor, weekStartsOn, todayKey);
|
|
221
|
+
case 'YEAR':
|
|
222
|
+
return buildYearDays(anchor, todayKey);
|
|
223
|
+
case 'AGENDA':
|
|
224
|
+
return buildConsecutiveDays(anchor, CALENDAR_AGENDA_DAY_COUNT, todayKey);
|
|
225
|
+
default:
|
|
226
|
+
// DAY and FOUR_DAYS both run forward from the anchor, so clicking a
|
|
227
|
+
// day in the month view lands on that day rather than on its week
|
|
228
|
+
return buildConsecutiveDays(anchor, CALENDAR_VIEW_DAY_COUNTS[view] || 1, todayKey);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Return the anchor date one step forward or backward in a view.
|
|
234
|
+
*
|
|
235
|
+
* A month step is a calendar month, not 30 days: stepping from the 31st of
|
|
236
|
+
* March has to land in April, and luxon already clamps that to the 30th rather
|
|
237
|
+
* than overflowing into May.
|
|
238
|
+
*/
|
|
239
|
+
|
|
240
|
+
export function getCalendarStepAnchor(p: {
|
|
241
|
+
view: CalendarViewEnum;
|
|
242
|
+
anchorDate: Date;
|
|
243
|
+
direction: 1 | -1;
|
|
244
|
+
timeZone?: string | null;
|
|
245
|
+
}): Date {
|
|
246
|
+
const { view, anchorDate, direction } = p;
|
|
247
|
+
const anchor = DateTime.fromJSDate(anchorDate, { zone: getCalendarZone(p.timeZone) }).startOf('day');
|
|
248
|
+
|
|
249
|
+
switch (view) {
|
|
250
|
+
case 'WEEK':
|
|
251
|
+
return anchor.plus({ weeks: direction }).toJSDate();
|
|
252
|
+
case 'MONTH':
|
|
253
|
+
return anchor.plus({ months: direction }).toJSDate();
|
|
254
|
+
case 'YEAR':
|
|
255
|
+
return anchor.plus({ years: direction }).toJSDate();
|
|
256
|
+
case 'AGENDA':
|
|
257
|
+
return anchor.plus({ days: direction * CALENDAR_AGENDA_DAY_COUNT }).toJSDate();
|
|
258
|
+
default:
|
|
259
|
+
return anchor.plus({ days: direction * (CALENDAR_VIEW_DAY_COUNTS[view] || 1) }).toJSDate();
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Return the minutes from local midnight one instant falls at, on a given day.
|
|
265
|
+
*
|
|
266
|
+
* Measured against the day's own start rather than by reading the clock, so a
|
|
267
|
+
* DST spring-forward day — where 2am never happens — still places events at
|
|
268
|
+
* the height the grid drew the hour lines at.
|
|
269
|
+
*/
|
|
270
|
+
|
|
271
|
+
export function getMinutesFromDayStart(instant: Date, dayStart: Date): number {
|
|
272
|
+
return (instant.getTime() - dayStart.getTime()) / 60_000;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Return the instant a number of minutes past a day's local start.
|
|
277
|
+
*/
|
|
278
|
+
|
|
279
|
+
export function getInstantFromDayMinutes(dayStart: Date, minutes: number): Date {
|
|
280
|
+
return new Date(dayStart.getTime() + minutes * 60_000);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Return how many minutes long a day is, in its own zone.
|
|
285
|
+
*
|
|
286
|
+
* Almost always 1440 — but 1380 or 1500 on the two DST days a year, and the
|
|
287
|
+
* grid has to draw those days at the right height or every event after the
|
|
288
|
+
* transition sits an hour off.
|
|
289
|
+
*/
|
|
290
|
+
|
|
291
|
+
export function getDayLengthMinutes(day: CalendarDayObj): number {
|
|
292
|
+
return (day.endAt.getTime() - day.startAt.getTime()) / 60_000;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Snap a minute offset to the nearest increment, clamped into the day.
|
|
297
|
+
*/
|
|
298
|
+
|
|
299
|
+
export function snapMinutes(minutes: number, snapTo: number, dayLengthMinutes: number): number {
|
|
300
|
+
const snapped = Math.round(minutes / snapTo) * snapTo;
|
|
301
|
+
|
|
302
|
+
return Math.max(0, Math.min(dayLengthMinutes, snapped));
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Format the heading one view shows for its range ("July 2026", "Jul 20 – 26,
|
|
307
|
+
* 2026", "Monday, July 20").
|
|
308
|
+
*
|
|
309
|
+
* Kept here rather than in the UI because the answer depends on the range
|
|
310
|
+
* math: whether a week spans two months or two years is the same question as
|
|
311
|
+
* where its days fall.
|
|
312
|
+
*/
|
|
313
|
+
|
|
314
|
+
export function getCalendarRangeTitle(range: CalendarRangeObj, locale?: string | null): string {
|
|
315
|
+
const zone = range.timeZone;
|
|
316
|
+
const first = DateTime.fromJSDate(range.days[0].startAt, { zone }).setLocale(locale || 'en');
|
|
317
|
+
const lastDay = range.days[range.days.length - 1];
|
|
318
|
+
const last = DateTime.fromJSDate(new Date(lastDay.endAt.getTime() - 1), { zone }).setLocale(locale || 'en');
|
|
319
|
+
|
|
320
|
+
switch (range.view) {
|
|
321
|
+
case 'YEAR':
|
|
322
|
+
return first.toFormat('yyyy');
|
|
323
|
+
case 'MONTH':
|
|
324
|
+
return DateTime.fromJSDate(range.anchorDate, { zone }).setLocale(locale || 'en').toFormat('LLLL yyyy');
|
|
325
|
+
case 'DAY':
|
|
326
|
+
return first.toFormat('cccc, LLLL d, yyyy');
|
|
327
|
+
default:
|
|
328
|
+
break;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
if (first.year !== last.year) {
|
|
332
|
+
return `${first.toFormat('LLL d, yyyy')} – ${last.toFormat('LLL d, yyyy')}`;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
if (first.month !== last.month) {
|
|
336
|
+
return `${first.toFormat('LLL d')} – ${last.toFormat('LLL d, yyyy')}`;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
return `${first.toFormat('LLL d')} – ${last.toFormat('d, yyyy')}`;
|
|
340
|
+
}
|