@jsb188/calendar 1.1.2
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 +45 -0
- package/src/constants/calendar.ts +153 -0
- package/src/css.d.ts +7 -0
- package/src/libs/calendar-geometry.ts +500 -0
- package/src/libs/calendar-gesture.ts +341 -0
- package/src/libs/calendar-now.ts +39 -0
- package/src/libs/calendar-range.ts +401 -0
- package/src/libs/calendar-state.ts +26 -0
- package/src/libs/calendar-url.ts +191 -0
- package/src/libs/roster-layout.ts +388 -0
- package/src/main.ts +32 -0
- package/src/modules/Calendar.tsx +736 -0
- package/src/modules/MiniCalendar.tsx +376 -0
- package/src/types/calendar.d.ts +128 -0
- package/src/types/shift-form.d.ts +19 -0
- package/src/ui/CalendarAgendaUI.tsx +115 -0
- package/src/ui/CalendarMonthUI.tsx +142 -0
- package/src/ui/CalendarToolbarUI.tsx +133 -0
- package/src/ui/CalendarUI.tsx +401 -0
- package/src/ui/CalendarYearUI.tsx +67 -0
- package/src/ui/RosterUI.tsx +273 -0
- package/src/ui/ShiftFormUI.tsx +333 -0
- package/tests/calendar-geometry.test.ts +549 -0
- package/tests/calendar-gesture.test.ts +340 -0
- package/tests/calendar-range.test.ts +358 -0
- package/tests/calendar-url.test.ts +189 -0
- package/tests/roster-layout.test.ts +210 -0
- package/tsconfig.json +13 -0
- package/vitest.config.ts +11 -0
|
@@ -0,0 +1,736 @@
|
|
|
1
|
+
import { cn } from '@jsb188/app/utils/string.ts';
|
|
2
|
+
import { DateTime } from 'luxon';
|
|
3
|
+
import { useAtom } from 'jotai';
|
|
4
|
+
import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
5
|
+
import {
|
|
6
|
+
CALENDAR_GUTTER_WIDTH,
|
|
7
|
+
CALENDAR_HOUR_HEIGHT,
|
|
8
|
+
CALENDAR_MONTH_CELL_MAX_EVENTS,
|
|
9
|
+
CALENDAR_VIEW_DAY_COUNTS,
|
|
10
|
+
type CalendarViewEnum,
|
|
11
|
+
} from '../constants/calendar.ts';
|
|
12
|
+
import {
|
|
13
|
+
getCalendarAllDayLayout,
|
|
14
|
+
getCalendarEventsByDateKey,
|
|
15
|
+
getCalendarMonthCells,
|
|
16
|
+
getCalendarTimedLayout,
|
|
17
|
+
} from '../libs/calendar-geometry.ts';
|
|
18
|
+
import {
|
|
19
|
+
getCalendarCreateRange,
|
|
20
|
+
getCalendarMoveRange,
|
|
21
|
+
getCalendarPointerSlot,
|
|
22
|
+
getCalendarResizeRange,
|
|
23
|
+
startCalendarGesture,
|
|
24
|
+
type CalendarGestureRange,
|
|
25
|
+
} from '../libs/calendar-gesture.ts';
|
|
26
|
+
import {
|
|
27
|
+
getCalendarDateKey,
|
|
28
|
+
getCalendarRange,
|
|
29
|
+
getDayLengthMinutes,
|
|
30
|
+
getMinutesFromDayStart,
|
|
31
|
+
normalizeCalendarWeekStartsOn,
|
|
32
|
+
} from '../libs/calendar-range.ts';
|
|
33
|
+
import {
|
|
34
|
+
createCalendarStateAtoms,
|
|
35
|
+
type CalendarStateAtoms,
|
|
36
|
+
} from '../libs/calendar-state.ts';
|
|
37
|
+
import type { CalendarEventObj } from '../types/calendar.d.ts';
|
|
38
|
+
import {
|
|
39
|
+
CalendarAgendaUI,
|
|
40
|
+
type CalendarAgendaDayObj,
|
|
41
|
+
} from '../ui/CalendarAgendaUI.tsx';
|
|
42
|
+
import { CalendarMonthUI } from '../ui/CalendarMonthUI.tsx';
|
|
43
|
+
import {
|
|
44
|
+
CalendarAllDayBandUI,
|
|
45
|
+
CalendarDayHeaderUI,
|
|
46
|
+
CalendarDraftBlockUI,
|
|
47
|
+
CalendarEventUI,
|
|
48
|
+
CalendarHourLinesUI,
|
|
49
|
+
CalendarNowLineUI,
|
|
50
|
+
CalendarTimeGutterUI,
|
|
51
|
+
} from '../ui/CalendarUI.tsx';
|
|
52
|
+
import {
|
|
53
|
+
CalendarYearUI,
|
|
54
|
+
type CalendarYearMonthObj,
|
|
55
|
+
} from '../ui/CalendarYearUI.tsx';
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The calendar surface.
|
|
59
|
+
*
|
|
60
|
+
* This is the container: it owns the atom bag, derives the visible range, runs
|
|
61
|
+
* the layout pass, and hands finished geometry to the dumb components in
|
|
62
|
+
* /ui/. It does not fetch — the page above it does, and passes events down —
|
|
63
|
+
* so the same grid draws a daycare's shifts, an off-site, and a network event
|
|
64
|
+
* without knowing which it is looking at.
|
|
65
|
+
*
|
|
66
|
+
* View and anchor date arrive as props from the URL rather than living here,
|
|
67
|
+
* because the app shell remounts a route on every pathname change but not on a
|
|
68
|
+
* search change. Keeping them in the URL is what lets somebody page through
|
|
69
|
+
* months without the calendar being destroyed and rebuilt each time.
|
|
70
|
+
*/
|
|
71
|
+
|
|
72
|
+
export interface CalendarProps {
|
|
73
|
+
className?: string;
|
|
74
|
+
events: CalendarEventObj[];
|
|
75
|
+
view: CalendarViewEnum;
|
|
76
|
+
anchorDate: Date;
|
|
77
|
+
now: Date;
|
|
78
|
+
timeZone?: string | null;
|
|
79
|
+
weekStartsOn?: number;
|
|
80
|
+
locale?: string | null;
|
|
81
|
+
// Rendered as the first row, so the page chooses its own toolbar
|
|
82
|
+
children?: React.ReactNode;
|
|
83
|
+
// Display strings. The package holds no copy of its own: the app owns
|
|
84
|
+
// i18n, so every user-visible word is passed in rather than translated
|
|
85
|
+
// here, and this package stays usable by any surface.
|
|
86
|
+
allDayText?: string;
|
|
87
|
+
agendaEmptyText?: string;
|
|
88
|
+
moreLabel?: (count: number) => string;
|
|
89
|
+
// Without this the grid is read-only: no create, no move, no resize
|
|
90
|
+
editable?: boolean;
|
|
91
|
+
onSelectEvent?: (eventId: string) => void;
|
|
92
|
+
onSelectDay?: (dateKey: string) => void;
|
|
93
|
+
// A range the user drew; the page turns it into a create form
|
|
94
|
+
onCreateRange?: (range: { startAt: Date; endAt: Date }) => void;
|
|
95
|
+
// An existing event the user moved or resized
|
|
96
|
+
onMoveEvent?: (p: { eventId: string; startAt: Date; endAt: Date }) => void;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Render one Calendar inside an isolated local calendar state store.
|
|
101
|
+
*/
|
|
102
|
+
|
|
103
|
+
export function Calendar(p: CalendarProps) {
|
|
104
|
+
const stateAtomsRef = useRef<CalendarStateAtoms | null>(null);
|
|
105
|
+
|
|
106
|
+
// Created in a ref, never inline in the render body: creating atoms inline
|
|
107
|
+
// would hand every render a fresh bag and reset all state
|
|
108
|
+
if (!stateAtomsRef.current) {
|
|
109
|
+
stateAtomsRef.current = createCalendarStateAtoms();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return (
|
|
113
|
+
<CalendarContent
|
|
114
|
+
{...p}
|
|
115
|
+
stateAtoms={stateAtomsRef.current}
|
|
116
|
+
/>
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
type CalendarContentProps = CalendarProps & {
|
|
121
|
+
stateAtoms: CalendarStateAtoms;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
/*
|
|
125
|
+
* Render the default month overflow label when the caller supplies no formatter.
|
|
126
|
+
*/
|
|
127
|
+
|
|
128
|
+
function getDefaultCalendarMoreLabel(count: number): string {
|
|
129
|
+
return `+${count}`;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/*
|
|
133
|
+
* Format the hour labels once per locale rather than per hour per render.
|
|
134
|
+
*/
|
|
135
|
+
|
|
136
|
+
function useCalendarHourLabels(timeZone: string, locale?: string | null) {
|
|
137
|
+
return useMemo(() => {
|
|
138
|
+
const base = DateTime.fromObject(
|
|
139
|
+
{ year: 2026, month: 1, day: 1 },
|
|
140
|
+
{ zone: timeZone },
|
|
141
|
+
).setLocale(locale || 'en');
|
|
142
|
+
|
|
143
|
+
return Array.from({ length: 24 }, (_unused, hour) =>
|
|
144
|
+
base.set({ hour }).toFormat('h a'),
|
|
145
|
+
);
|
|
146
|
+
}, [timeZone, locale]);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/*
|
|
150
|
+
* Render the time grid: gutter, hour lines, day columns, and events.
|
|
151
|
+
*/
|
|
152
|
+
|
|
153
|
+
const CalendarContent = memo((p: CalendarContentProps) => {
|
|
154
|
+
const {
|
|
155
|
+
className,
|
|
156
|
+
events,
|
|
157
|
+
view,
|
|
158
|
+
anchorDate,
|
|
159
|
+
now,
|
|
160
|
+
timeZone,
|
|
161
|
+
weekStartsOn,
|
|
162
|
+
locale,
|
|
163
|
+
children,
|
|
164
|
+
allDayText,
|
|
165
|
+
agendaEmptyText,
|
|
166
|
+
moreLabel,
|
|
167
|
+
editable,
|
|
168
|
+
onSelectEvent,
|
|
169
|
+
onSelectDay,
|
|
170
|
+
onCreateRange,
|
|
171
|
+
onMoveEvent,
|
|
172
|
+
stateAtoms,
|
|
173
|
+
} = p;
|
|
174
|
+
|
|
175
|
+
const [selectedEventId, setSelectedEventId] = useAtom(
|
|
176
|
+
stateAtoms.selectedEventIdAtom,
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
// "now" ticks every minute to move the now line, but the range only uses it
|
|
180
|
+
// to decide which column is today — so the memo keys on the local DATE
|
|
181
|
+
// rather than the instant. Keying on the instant would rebuild the day
|
|
182
|
+
// objects every minute, and every layout pass hanging off range.days with
|
|
183
|
+
// them, to redraw a line that moved one pixel.
|
|
184
|
+
const nowDateKey = getCalendarDateKey(now, timeZone);
|
|
185
|
+
const range = useMemo(
|
|
186
|
+
// "now" is read inside but "nowDateKey" is the dependency: the range only
|
|
187
|
+
// changes when the local date does
|
|
188
|
+
() => getCalendarRange({ view, anchorDate, now, timeZone, weekStartsOn }),
|
|
189
|
+
[view, anchorDate, nowDateKey, timeZone, weekStartsOn],
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
// One pass over the week per data change, not per subscriber
|
|
193
|
+
const visibleEvents = events;
|
|
194
|
+
|
|
195
|
+
const isTimeGrid = !!CALENDAR_VIEW_DAY_COUNTS[view];
|
|
196
|
+
|
|
197
|
+
// Each view runs only its own layout pass. A year is 365 day objects, and
|
|
198
|
+
// running the overlap sweep over it to draw a grid that shows no events
|
|
199
|
+
// would be pure waste on every render.
|
|
200
|
+
const timedLayout = useMemo(
|
|
201
|
+
() =>
|
|
202
|
+
isTimeGrid
|
|
203
|
+
? getCalendarTimedLayout(
|
|
204
|
+
visibleEvents.filter((event) => !event.allDay),
|
|
205
|
+
range.days,
|
|
206
|
+
)
|
|
207
|
+
: [],
|
|
208
|
+
[isTimeGrid, visibleEvents, range.days],
|
|
209
|
+
);
|
|
210
|
+
const allDayBars = useMemo(
|
|
211
|
+
() =>
|
|
212
|
+
isTimeGrid
|
|
213
|
+
? getCalendarAllDayLayout(
|
|
214
|
+
visibleEvents.filter((event) => event.allDay),
|
|
215
|
+
range.days,
|
|
216
|
+
)
|
|
217
|
+
: [],
|
|
218
|
+
[isTimeGrid, visibleEvents, range.days],
|
|
219
|
+
);
|
|
220
|
+
const monthCells = useMemo(
|
|
221
|
+
() =>
|
|
222
|
+
view === 'MONTH'
|
|
223
|
+
? getCalendarMonthCells(
|
|
224
|
+
visibleEvents,
|
|
225
|
+
range.days,
|
|
226
|
+
CALENDAR_MONTH_CELL_MAX_EVENTS,
|
|
227
|
+
)
|
|
228
|
+
: [],
|
|
229
|
+
[view, visibleEvents, range.days],
|
|
230
|
+
);
|
|
231
|
+
|
|
232
|
+
const hourLabels = useCalendarHourLabels(range.timeZone, locale);
|
|
233
|
+
|
|
234
|
+
const dayLabels = useMemo(
|
|
235
|
+
() =>
|
|
236
|
+
range.days.map((day) => {
|
|
237
|
+
const local = DateTime.fromJSDate(day.startAt, {
|
|
238
|
+
zone: range.timeZone,
|
|
239
|
+
}).setLocale(locale || 'en');
|
|
240
|
+
|
|
241
|
+
return {
|
|
242
|
+
weekday: local.toFormat('ccc'),
|
|
243
|
+
dayNumber: local.toFormat('d'),
|
|
244
|
+
};
|
|
245
|
+
}),
|
|
246
|
+
[range.days, range.timeZone, locale],
|
|
247
|
+
);
|
|
248
|
+
|
|
249
|
+
/*
|
|
250
|
+
* Weekday column headings, built from a real week so they honour the
|
|
251
|
+
* locale and the configured week start rather than a hardcoded list.
|
|
252
|
+
*/
|
|
253
|
+
|
|
254
|
+
const weekdayLabels = useMemo(() => {
|
|
255
|
+
const weekStart = normalizeCalendarWeekStartsOn(weekStartsOn);
|
|
256
|
+
// Any Sunday; 2026-01-04 is one
|
|
257
|
+
const sunday = DateTime.fromObject(
|
|
258
|
+
{ year: 2026, month: 1, day: 4 },
|
|
259
|
+
{ zone: range.timeZone },
|
|
260
|
+
).setLocale(locale || 'en');
|
|
261
|
+
|
|
262
|
+
return Array.from({ length: 7 }, (_unused, index) =>
|
|
263
|
+
sunday.plus({ days: index + weekStart }).toFormat('ccc'),
|
|
264
|
+
);
|
|
265
|
+
}, [range.timeZone, locale, weekStartsOn]);
|
|
266
|
+
const dayNumberLabels = useMemo(
|
|
267
|
+
() => dayLabels.map((label) => label.dayNumber),
|
|
268
|
+
[dayLabels],
|
|
269
|
+
);
|
|
270
|
+
|
|
271
|
+
const timeLabelByEventId = useMemo(() => {
|
|
272
|
+
const labels = new Map<string, string>();
|
|
273
|
+
|
|
274
|
+
for (const entry of timedLayout) {
|
|
275
|
+
if (labels.has(entry.event.id)) {
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
labels.set(
|
|
280
|
+
entry.event.id,
|
|
281
|
+
DateTime.fromJSDate(entry.event.startAt, { zone: range.timeZone })
|
|
282
|
+
.setLocale(locale || 'en')
|
|
283
|
+
.toFormat('h:mm a'),
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
return labels;
|
|
288
|
+
}, [timedLayout, range.timeZone, locale]);
|
|
289
|
+
|
|
290
|
+
const handleSelectEvent = useCallback(
|
|
291
|
+
(eventId: string) => {
|
|
292
|
+
setSelectedEventId((current) => (current === eventId ? null : eventId));
|
|
293
|
+
|
|
294
|
+
onSelectEvent?.(eventId);
|
|
295
|
+
},
|
|
296
|
+
[onSelectEvent],
|
|
297
|
+
);
|
|
298
|
+
|
|
299
|
+
/*
|
|
300
|
+
* Forward day selection with one stable callback for every calendar view.
|
|
301
|
+
*/
|
|
302
|
+
const handleSelectDay = useCallback(
|
|
303
|
+
(dateKey: string) => {
|
|
304
|
+
onSelectDay?.(dateKey);
|
|
305
|
+
},
|
|
306
|
+
[onSelectDay],
|
|
307
|
+
);
|
|
308
|
+
|
|
309
|
+
const layoutByDayIndex = useMemo(() => {
|
|
310
|
+
const byDay: Array<typeof timedLayout> = range.days.map(() => []);
|
|
311
|
+
|
|
312
|
+
for (const entry of timedLayout) {
|
|
313
|
+
byDay[entry.dayIndex]?.push(entry);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
return byDay;
|
|
317
|
+
}, [timedLayout, range.days]);
|
|
318
|
+
|
|
319
|
+
// A DST day is 23 or 25 hours long; drawing 24 would put every event after
|
|
320
|
+
// the transition an hour out
|
|
321
|
+
const gridHeight = useMemo(
|
|
322
|
+
() =>
|
|
323
|
+
(Math.max(...range.days.map(getDayLengthMinutes)) / 60) *
|
|
324
|
+
CALENDAR_HOUR_HEIGHT,
|
|
325
|
+
[range.days],
|
|
326
|
+
);
|
|
327
|
+
const gridContentStyle = useMemo(
|
|
328
|
+
() => ({ height: gridHeight, touchAction: 'manipulation' as const }),
|
|
329
|
+
[gridHeight],
|
|
330
|
+
);
|
|
331
|
+
|
|
332
|
+
/*
|
|
333
|
+
* The twelve months the year view draws. Each mini grid builds its own days
|
|
334
|
+
* from the month it is given, so this only has to name them.
|
|
335
|
+
*/
|
|
336
|
+
|
|
337
|
+
const yearMonths = useMemo<CalendarYearMonthObj[]>(() => {
|
|
338
|
+
if (view !== 'YEAR') {
|
|
339
|
+
return [];
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
const yearStart = DateTime.fromJSDate(range.anchorDate, {
|
|
343
|
+
zone: range.timeZone,
|
|
344
|
+
})
|
|
345
|
+
.setLocale(locale || 'en')
|
|
346
|
+
.startOf('year');
|
|
347
|
+
|
|
348
|
+
return Array.from({ length: 12 }, (_unused, index) => {
|
|
349
|
+
const monthStart = yearStart.plus({ months: index });
|
|
350
|
+
|
|
351
|
+
return {
|
|
352
|
+
label: monthStart.toFormat('LLLL'),
|
|
353
|
+
monthDate: monthStart.toJSDate(),
|
|
354
|
+
};
|
|
355
|
+
});
|
|
356
|
+
}, [view, range.anchorDate, range.timeZone, locale]);
|
|
357
|
+
|
|
358
|
+
/*
|
|
359
|
+
* Build the agenda list, omitting days with nothing on them.
|
|
360
|
+
*
|
|
361
|
+
* A month of empty headings is scrolling, not information — but the list
|
|
362
|
+
* being wholly empty is surfaced by the component so it never reads as a
|
|
363
|
+
* failed load.
|
|
364
|
+
*/
|
|
365
|
+
|
|
366
|
+
const agendaDays = useMemo<CalendarAgendaDayObj[]>(() => {
|
|
367
|
+
if (view !== 'AGENDA') {
|
|
368
|
+
return [];
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
const byDateKey = getCalendarEventsByDateKey(visibleEvents, range.days);
|
|
372
|
+
|
|
373
|
+
return range.days
|
|
374
|
+
.filter((day) => (byDateKey.get(day.dateKey) || []).length > 0)
|
|
375
|
+
.map((day) => ({
|
|
376
|
+
dateKey: day.dateKey,
|
|
377
|
+
label: DateTime.fromJSDate(day.startAt, { zone: range.timeZone })
|
|
378
|
+
.setLocale(locale || 'en')
|
|
379
|
+
.toFormat('ccc, LLL d'),
|
|
380
|
+
isToday: day.isToday,
|
|
381
|
+
events: byDateKey.get(day.dateKey) || [],
|
|
382
|
+
}));
|
|
383
|
+
}, [view, visibleEvents, range.days, range.timeZone, locale]);
|
|
384
|
+
|
|
385
|
+
/*
|
|
386
|
+
* Time ranges for the agenda's left column. Built from the events rather
|
|
387
|
+
* than the grid layout, because the agenda also lists all-day entries the
|
|
388
|
+
* time grid excludes.
|
|
389
|
+
*/
|
|
390
|
+
|
|
391
|
+
const agendaTimeLabels = useMemo(() => {
|
|
392
|
+
const labels = new Map<string, string>();
|
|
393
|
+
|
|
394
|
+
for (const day of agendaDays) {
|
|
395
|
+
for (const event of day.events) {
|
|
396
|
+
if (labels.has(event.id)) {
|
|
397
|
+
continue;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
labels.set(
|
|
401
|
+
event.id,
|
|
402
|
+
event.allDay
|
|
403
|
+
? allDayText || ''
|
|
404
|
+
: DateTime.fromJSDate(event.startAt, { zone: range.timeZone })
|
|
405
|
+
.setLocale(locale || 'en')
|
|
406
|
+
.toFormat('h:mm a'),
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
return labels;
|
|
412
|
+
}, [agendaDays, range.timeZone, locale, allDayText]);
|
|
413
|
+
|
|
414
|
+
/*
|
|
415
|
+
* Pointer interaction.
|
|
416
|
+
*
|
|
417
|
+
* The in-flight gesture lives in a ref and the window listeners it
|
|
418
|
+
* installs; only the resulting ghost range reaches React state, and only
|
|
419
|
+
* when it actually changes. A pointermove that wrote state directly would
|
|
420
|
+
* re-render every block in the week at pointer frequency.
|
|
421
|
+
*/
|
|
422
|
+
|
|
423
|
+
const gridContentRef = useRef<HTMLDivElement | null>(null);
|
|
424
|
+
const cancelGestureRef = useRef<(() => void) | null>(null);
|
|
425
|
+
const [draftRange, setDraftRange] = useState<CalendarGestureRange | null>(
|
|
426
|
+
null,
|
|
427
|
+
);
|
|
428
|
+
|
|
429
|
+
// Everything the resolver needs, read at pointer time rather than captured
|
|
430
|
+
// at pointerdown, so a scroll mid-drag does not use a stale layout
|
|
431
|
+
const gestureContextRef = useRef({ range, timedLayout });
|
|
432
|
+
gestureContextRef.current = { range, timedLayout };
|
|
433
|
+
|
|
434
|
+
// A gesture in flight when the component unmounts would leave window
|
|
435
|
+
// listeners behind and keep the whole subtree alive
|
|
436
|
+
useEffect(() => () => cancelGestureRef.current?.(), []);
|
|
437
|
+
|
|
438
|
+
const applyDraft = useCallback((next: CalendarGestureRange | null) => {
|
|
439
|
+
setDraftRange((current) => {
|
|
440
|
+
if (current === next) {
|
|
441
|
+
return current;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
// Returning the same reference on an unchanged range is what keeps a
|
|
445
|
+
// slow drag from re-rendering the grid on every frame
|
|
446
|
+
if (
|
|
447
|
+
current &&
|
|
448
|
+
next &&
|
|
449
|
+
current.dayIndex === next.dayIndex &&
|
|
450
|
+
current.startAt.getTime() === next.startAt.getTime() &&
|
|
451
|
+
current.endAt.getTime() === next.endAt.getTime()
|
|
452
|
+
) {
|
|
453
|
+
return current;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
return next;
|
|
457
|
+
});
|
|
458
|
+
}, []);
|
|
459
|
+
|
|
460
|
+
const handleGridPointerDown = useCallback(
|
|
461
|
+
(event: React.PointerEvent<HTMLDivElement>) => {
|
|
462
|
+
// Only the primary button, and never a second finger mid-gesture
|
|
463
|
+
if (!editable || event.button !== 0 || cancelGestureRef.current) {
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
const content = gridContentRef.current;
|
|
468
|
+
if (!content) {
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
const target = event.target as HTMLElement;
|
|
473
|
+
const blockElement = target.closest(
|
|
474
|
+
'[data-calendar-event-id]',
|
|
475
|
+
) as HTMLElement | null;
|
|
476
|
+
const edge = (
|
|
477
|
+
target.closest('[data-calendar-edge]') as HTMLElement | null
|
|
478
|
+
)?.dataset.calendarEdge;
|
|
479
|
+
|
|
480
|
+
/*
|
|
481
|
+
* Translate a client position into the day column and snapped minute
|
|
482
|
+
* under it, measured against the CONTENT box so scrolling is accounted
|
|
483
|
+
* for without tracking scrollTop separately.
|
|
484
|
+
*/
|
|
485
|
+
|
|
486
|
+
const resolveSlot = (clientX: number, clientY: number) => {
|
|
487
|
+
const contentRect = content.getBoundingClientRect();
|
|
488
|
+
|
|
489
|
+
return getCalendarPointerSlot({
|
|
490
|
+
offsetX: clientX - contentRect.left - CALENDAR_GUTTER_WIDTH,
|
|
491
|
+
offsetY: clientY - contentRect.top,
|
|
492
|
+
contentWidth: contentRect.width - CALENDAR_GUTTER_WIDTH,
|
|
493
|
+
hourHeight: CALENDAR_HOUR_HEIGHT,
|
|
494
|
+
days: gestureContextRef.current.range.days,
|
|
495
|
+
});
|
|
496
|
+
};
|
|
497
|
+
|
|
498
|
+
const anchorSlot = resolveSlot(event.clientX, event.clientY);
|
|
499
|
+
const eventId = blockElement?.dataset.calendarEventId || null;
|
|
500
|
+
const grabbed = eventId
|
|
501
|
+
? gestureContextRef.current.timedLayout.find(
|
|
502
|
+
(entry) => entry.event.id === eventId,
|
|
503
|
+
)
|
|
504
|
+
: null;
|
|
505
|
+
|
|
506
|
+
let resolveRange: (
|
|
507
|
+
clientX: number,
|
|
508
|
+
clientY: number,
|
|
509
|
+
moved: boolean,
|
|
510
|
+
) => CalendarGestureRange | null;
|
|
511
|
+
|
|
512
|
+
if (grabbed && edge) {
|
|
513
|
+
resolveRange = (clientX, clientY) => {
|
|
514
|
+
const slot = resolveSlot(clientX, clientY);
|
|
515
|
+
|
|
516
|
+
return getCalendarResizeRange({
|
|
517
|
+
days: gestureContextRef.current.range.days,
|
|
518
|
+
dayIndex: grabbed.dayIndex,
|
|
519
|
+
startAt: grabbed.event.startAt,
|
|
520
|
+
endAt: grabbed.event.endAt,
|
|
521
|
+
edge: edge === 'START' ? 'START' : 'END',
|
|
522
|
+
currentMinute: slot.minute,
|
|
523
|
+
});
|
|
524
|
+
};
|
|
525
|
+
} else if (grabbed) {
|
|
526
|
+
// Where in the block it was grabbed, so it does not jump under the
|
|
527
|
+
// pointer on the first pixel of movement
|
|
528
|
+
const grabOffsetMinutes = anchorSlot.minute - grabbed.startMinute;
|
|
529
|
+
|
|
530
|
+
resolveRange = (clientX, clientY) => {
|
|
531
|
+
const slot = resolveSlot(clientX, clientY);
|
|
532
|
+
|
|
533
|
+
return getCalendarMoveRange({
|
|
534
|
+
days: gestureContextRef.current.range.days,
|
|
535
|
+
startAt: grabbed.event.startAt,
|
|
536
|
+
endAt: grabbed.event.endAt,
|
|
537
|
+
grabOffsetMinutes,
|
|
538
|
+
currentDayIndex: slot.dayIndex,
|
|
539
|
+
currentMinute: slot.minute,
|
|
540
|
+
});
|
|
541
|
+
};
|
|
542
|
+
} else {
|
|
543
|
+
resolveRange = (clientX, clientY, moved) => {
|
|
544
|
+
const slot = resolveSlot(clientX, clientY);
|
|
545
|
+
|
|
546
|
+
return getCalendarCreateRange({
|
|
547
|
+
days: gestureContextRef.current.range.days,
|
|
548
|
+
anchorDayIndex: anchorSlot.dayIndex,
|
|
549
|
+
anchorMinute: anchorSlot.minute,
|
|
550
|
+
currentDayIndex: slot.dayIndex,
|
|
551
|
+
currentMinute: slot.minute,
|
|
552
|
+
moved,
|
|
553
|
+
});
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
cancelGestureRef.current = startCalendarGesture({
|
|
558
|
+
pointerId: event.pointerId,
|
|
559
|
+
originClientX: event.clientX,
|
|
560
|
+
originClientY: event.clientY,
|
|
561
|
+
resolveRange,
|
|
562
|
+
onPreview: applyDraft,
|
|
563
|
+
onCommit: (committed, moved) => {
|
|
564
|
+
if (grabbed) {
|
|
565
|
+
// A press that never moved is somebody opening the block, not
|
|
566
|
+
// asking to move it by zero minutes
|
|
567
|
+
if (moved) {
|
|
568
|
+
onMoveEvent?.({
|
|
569
|
+
eventId: grabbed.event.id,
|
|
570
|
+
startAt: committed.startAt,
|
|
571
|
+
endAt: committed.endAt,
|
|
572
|
+
});
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
return;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
onCreateRange?.({
|
|
579
|
+
startAt: committed.startAt,
|
|
580
|
+
endAt: committed.endAt,
|
|
581
|
+
});
|
|
582
|
+
},
|
|
583
|
+
onFinish: () => {
|
|
584
|
+
cancelGestureRef.current = null;
|
|
585
|
+
},
|
|
586
|
+
});
|
|
587
|
+
},
|
|
588
|
+
[editable, applyDraft, onCreateRange, onMoveEvent],
|
|
589
|
+
);
|
|
590
|
+
|
|
591
|
+
const todayIndex = range.days.findIndex((day) => day.isToday);
|
|
592
|
+
const nowOffset =
|
|
593
|
+
todayIndex >= 0
|
|
594
|
+
? (getMinutesFromDayStart(now, range.days[todayIndex].startAt) / 60) *
|
|
595
|
+
CALENDAR_HOUR_HEIGHT
|
|
596
|
+
: -1;
|
|
597
|
+
|
|
598
|
+
// The ghost's own label, so a drag reads as a time range while it is being
|
|
599
|
+
// drawn rather than as an unlabelled rectangle
|
|
600
|
+
const draftTimeLabel = useMemo(() => {
|
|
601
|
+
if (!draftRange) {
|
|
602
|
+
return '';
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
const zone = range.timeZone;
|
|
606
|
+
const start = DateTime.fromJSDate(draftRange.startAt, { zone }).setLocale(
|
|
607
|
+
locale || 'en',
|
|
608
|
+
);
|
|
609
|
+
const end = DateTime.fromJSDate(draftRange.endAt, { zone }).setLocale(
|
|
610
|
+
locale || 'en',
|
|
611
|
+
);
|
|
612
|
+
|
|
613
|
+
return `${start.toFormat('h:mm a')} – ${end.toFormat('h:mm a')}`;
|
|
614
|
+
}, [draftRange, range.timeZone, locale]);
|
|
615
|
+
|
|
616
|
+
return (
|
|
617
|
+
<div className={cn('v_stretch h_f w_f rel bg', className)}>
|
|
618
|
+
{children}
|
|
619
|
+
|
|
620
|
+
{/* Only the time grid has day columns to label and an all-day band
|
|
621
|
+
to hang above them */}
|
|
622
|
+
{isTimeGrid ? (
|
|
623
|
+
<>
|
|
624
|
+
<CalendarDayHeaderUI
|
|
625
|
+
days={range.days}
|
|
626
|
+
dayLabels={dayLabels}
|
|
627
|
+
onSelectDay={onSelectDay}
|
|
628
|
+
/>
|
|
629
|
+
|
|
630
|
+
<CalendarAllDayBandUI
|
|
631
|
+
bars={allDayBars}
|
|
632
|
+
dayCount={range.days.length}
|
|
633
|
+
selectedEventId={selectedEventId}
|
|
634
|
+
onSelect={handleSelectEvent}
|
|
635
|
+
/>
|
|
636
|
+
</>
|
|
637
|
+
) : null}
|
|
638
|
+
|
|
639
|
+
{view === 'MONTH' ? (
|
|
640
|
+
<CalendarMonthUI
|
|
641
|
+
cells={monthCells}
|
|
642
|
+
weekdayLabels={weekdayLabels}
|
|
643
|
+
dayNumberLabels={dayNumberLabels}
|
|
644
|
+
selectedEventId={selectedEventId}
|
|
645
|
+
moreLabel={moreLabel || getDefaultCalendarMoreLabel}
|
|
646
|
+
onSelectEvent={handleSelectEvent}
|
|
647
|
+
onSelectDay={handleSelectDay}
|
|
648
|
+
onSelectOverflow={handleSelectDay}
|
|
649
|
+
/>
|
|
650
|
+
) : null}
|
|
651
|
+
|
|
652
|
+
{view === 'YEAR' ? (
|
|
653
|
+
<CalendarYearUI
|
|
654
|
+
months={yearMonths}
|
|
655
|
+
timeZone={range.timeZone}
|
|
656
|
+
locale={locale}
|
|
657
|
+
weekStartsOn={weekStartsOn}
|
|
658
|
+
todayDateKey={nowDateKey}
|
|
659
|
+
onSelectDay={handleSelectDay}
|
|
660
|
+
/>
|
|
661
|
+
) : null}
|
|
662
|
+
|
|
663
|
+
{view === 'AGENDA' ? (
|
|
664
|
+
<CalendarAgendaUI
|
|
665
|
+
days={agendaDays}
|
|
666
|
+
selectedEventId={selectedEventId}
|
|
667
|
+
timeLabelByEventId={agendaTimeLabels}
|
|
668
|
+
emptyText={agendaEmptyText || ''}
|
|
669
|
+
onSelectEvent={handleSelectEvent}
|
|
670
|
+
/>
|
|
671
|
+
) : null}
|
|
672
|
+
|
|
673
|
+
{isTimeGrid ? (
|
|
674
|
+
<div className="f h_0 app_scr auto_y rel">
|
|
675
|
+
<div
|
|
676
|
+
ref={gridContentRef}
|
|
677
|
+
className="h_stretch rel"
|
|
678
|
+
style={gridContentStyle}
|
|
679
|
+
onPointerDown={handleGridPointerDown}
|
|
680
|
+
>
|
|
681
|
+
<CalendarTimeGutterUI
|
|
682
|
+
hourHeight={CALENDAR_HOUR_HEIGHT}
|
|
683
|
+
hourLabels={hourLabels}
|
|
684
|
+
/>
|
|
685
|
+
|
|
686
|
+
{range.days.map((day, dayIndex) => (
|
|
687
|
+
<div
|
|
688
|
+
key={day.dateKey}
|
|
689
|
+
className={cn(
|
|
690
|
+
'f rel bd_l_1 bd_l_bd',
|
|
691
|
+
day.isWeekend ? 'bg_alt' : '',
|
|
692
|
+
)}
|
|
693
|
+
>
|
|
694
|
+
<CalendarHourLinesUI
|
|
695
|
+
hourCount={Math.ceil(getDayLengthMinutes(day) / 60)}
|
|
696
|
+
hourHeight={CALENDAR_HOUR_HEIGHT}
|
|
697
|
+
/>
|
|
698
|
+
|
|
699
|
+
{dayIndex === todayIndex && nowOffset >= 0 ? (
|
|
700
|
+
<CalendarNowLineUI topOffset={nowOffset} />
|
|
701
|
+
) : null}
|
|
702
|
+
|
|
703
|
+
{layoutByDayIndex[dayIndex]?.map((entry) => (
|
|
704
|
+
<CalendarEventUI
|
|
705
|
+
key={entry.event.id}
|
|
706
|
+
editable={!!editable}
|
|
707
|
+
layout={entry}
|
|
708
|
+
hourHeight={CALENDAR_HOUR_HEIGHT}
|
|
709
|
+
selected={selectedEventId === entry.event.id}
|
|
710
|
+
timeLabel={timeLabelByEventId.get(entry.event.id) || ''}
|
|
711
|
+
onSelect={handleSelectEvent}
|
|
712
|
+
/>
|
|
713
|
+
))}
|
|
714
|
+
|
|
715
|
+
{draftRange?.dayIndex === dayIndex ? (
|
|
716
|
+
<CalendarDraftBlockUI
|
|
717
|
+
startMinute={getMinutesFromDayStart(
|
|
718
|
+
draftRange.startAt,
|
|
719
|
+
day.startAt,
|
|
720
|
+
)}
|
|
721
|
+
endMinute={getMinutesFromDayStart(
|
|
722
|
+
draftRange.endAt,
|
|
723
|
+
day.startAt,
|
|
724
|
+
)}
|
|
725
|
+
hourHeight={CALENDAR_HOUR_HEIGHT}
|
|
726
|
+
label={draftTimeLabel}
|
|
727
|
+
/>
|
|
728
|
+
) : null}
|
|
729
|
+
</div>
|
|
730
|
+
))}
|
|
731
|
+
</div>
|
|
732
|
+
</div>
|
|
733
|
+
) : null}
|
|
734
|
+
</div>
|
|
735
|
+
);
|
|
736
|
+
});
|