@jsb188/calendar 1.1.10 → 1.1.11

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.
@@ -1,4 +1,7 @@
1
- import { DEFAULT_TIMEZONE, isValidTimeZone } from '@jsb188/app/utils/timeZone.ts';
1
+ import {
2
+ DEFAULT_TIMEZONE,
3
+ isValidTimeZone,
4
+ } from '@jsb188/app/utils/timeZone.ts';
2
5
  import { DateTime } from 'luxon';
3
6
  import {
4
7
  CALENDAR_AGENDA_DAY_COUNT,
@@ -29,16 +32,34 @@ import type { CalendarDayObj, CalendarRangeObj } from '../types/calendar.d.ts';
29
32
  * because a setting is stale is worse.
30
33
  */
31
34
 
32
- function getCalendarZone(timeZone?: string | null): string {
35
+ export function getCalendarTimeZone(timeZone?: string | null): string {
33
36
  return timeZone && isValidTimeZone(timeZone) ? timeZone : DEFAULT_TIMEZONE;
34
37
  }
35
38
 
39
+ /**
40
+ * Normalize the app's Sunday-through-Saturday week-start convention.
41
+ */
42
+
43
+ export function normalizeCalendarWeekStartsOn(weekStartsOn?: number): number {
44
+ return typeof weekStartsOn === 'number' &&
45
+ Number.isInteger(weekStartsOn) &&
46
+ weekStartsOn >= 0 &&
47
+ weekStartsOn <= 6
48
+ ? weekStartsOn
49
+ : 0;
50
+ }
51
+
36
52
  /**
37
53
  * Return the local date key ("yyyy-MM-dd") one instant falls on, in a zone.
38
54
  */
39
55
 
40
- export function getCalendarDateKey(instant: Date, timeZone?: string | null): string {
41
- return DateTime.fromJSDate(instant, { zone: getCalendarZone(timeZone) }).toFormat(CALENDAR_DATE_PARAM_FORMAT);
56
+ export function getCalendarDateKey(
57
+ instant: Date,
58
+ timeZone?: string | null,
59
+ ): string {
60
+ return DateTime.fromJSDate(instant, {
61
+ zone: getCalendarTimeZone(timeZone),
62
+ }).toFormat(CALENDAR_DATE_PARAM_FORMAT);
42
63
  }
43
64
 
44
65
  /**
@@ -53,10 +74,12 @@ export function parseCalendarDateParam(
53
74
  now: Date,
54
75
  timeZone?: string | null,
55
76
  ): Date {
56
- const zone = getCalendarZone(timeZone);
77
+ const zone = getCalendarTimeZone(timeZone);
57
78
 
58
79
  if (value) {
59
- const parsed = DateTime.fromFormat(value, CALENDAR_DATE_PARAM_FORMAT, { zone });
80
+ const parsed = DateTime.fromFormat(value, CALENDAR_DATE_PARAM_FORMAT, {
81
+ zone,
82
+ });
60
83
 
61
84
  if (parsed.isValid) {
62
85
  return parsed.startOf('day').toJSDate();
@@ -130,7 +153,11 @@ function getWeekStart(day: DateTime, weekStartsOn: number): DateTime {
130
153
  * each navigation.
131
154
  */
132
155
 
133
- function buildMonthDays(anchor: DateTime, weekStartsOn: number, todayKey: string): CalendarDayObj[] {
156
+ function buildMonthDays(
157
+ anchor: DateTime,
158
+ weekStartsOn: number,
159
+ todayKey: string,
160
+ ): CalendarDayObj[] {
134
161
  const monthStart = anchor.startOf('month');
135
162
  const monthEnd = anchor.endOf('month');
136
163
  const gridStart = getWeekStart(monthStart, weekStartsOn);
@@ -139,11 +166,9 @@ function buildMonthDays(anchor: DateTime, weekStartsOn: number, todayKey: string
139
166
  for (let index = 0; index < 42; index++) {
140
167
  const day = gridStart.plus({ days: index });
141
168
 
142
- days.push(buildCalendarDay(
143
- day,
144
- todayKey,
145
- day >= monthStart && day <= monthEnd,
146
- ));
169
+ days.push(
170
+ buildCalendarDay(day, todayKey, day >= monthStart && day <= monthEnd),
171
+ );
147
172
  }
148
173
 
149
174
  return days;
@@ -184,10 +209,14 @@ export function getCalendarRange(p: {
184
209
  weekStartsOn?: number;
185
210
  }): CalendarRangeObj {
186
211
  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);
212
+ const timeZone = getCalendarTimeZone(p.timeZone);
213
+ const weekStartsOn = normalizeCalendarWeekStartsOn(p.weekStartsOn);
214
+ const anchor = DateTime.fromJSDate(anchorDate, { zone: timeZone }).startOf(
215
+ 'day',
216
+ );
217
+ const todayKey = DateTime.fromJSDate(now, { zone: timeZone }).toFormat(
218
+ CALENDAR_DATE_PARAM_FORMAT,
219
+ );
191
220
 
192
221
  const days = getCalendarViewDays({ view, anchor, weekStartsOn, todayKey });
193
222
 
@@ -215,7 +244,11 @@ function getCalendarViewDays(p: {
215
244
 
216
245
  switch (view) {
217
246
  case 'WEEK':
218
- return buildConsecutiveDays(getWeekStart(anchor, weekStartsOn), 7, todayKey);
247
+ return buildConsecutiveDays(
248
+ getWeekStart(anchor, weekStartsOn),
249
+ 7,
250
+ todayKey,
251
+ );
219
252
  case 'MONTH':
220
253
  return buildMonthDays(anchor, weekStartsOn, todayKey);
221
254
  case 'YEAR':
@@ -225,7 +258,11 @@ function getCalendarViewDays(p: {
225
258
  default:
226
259
  // DAY and FOUR_DAYS both run forward from the anchor, so clicking a
227
260
  // 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);
261
+ return buildConsecutiveDays(
262
+ anchor,
263
+ CALENDAR_VIEW_DAY_COUNTS[view] || 1,
264
+ todayKey,
265
+ );
229
266
  }
230
267
  }
231
268
 
@@ -244,7 +281,9 @@ export function getCalendarStepAnchor(p: {
244
281
  timeZone?: string | null;
245
282
  }): Date {
246
283
  const { view, anchorDate, direction } = p;
247
- const anchor = DateTime.fromJSDate(anchorDate, { zone: getCalendarZone(p.timeZone) }).startOf('day');
284
+ const anchor = DateTime.fromJSDate(anchorDate, {
285
+ zone: getCalendarTimeZone(p.timeZone),
286
+ }).startOf('day');
248
287
 
249
288
  switch (view) {
250
289
  case 'WEEK':
@@ -254,9 +293,13 @@ export function getCalendarStepAnchor(p: {
254
293
  case 'YEAR':
255
294
  return anchor.plus({ years: direction }).toJSDate();
256
295
  case 'AGENDA':
257
- return anchor.plus({ days: direction * CALENDAR_AGENDA_DAY_COUNT }).toJSDate();
296
+ return anchor
297
+ .plus({ days: direction * CALENDAR_AGENDA_DAY_COUNT })
298
+ .toJSDate();
258
299
  default:
259
- return anchor.plus({ days: direction * (CALENDAR_VIEW_DAY_COUNTS[view] || 1) }).toJSDate();
300
+ return anchor
301
+ .plus({ days: direction * (CALENDAR_VIEW_DAY_COUNTS[view] || 1) })
302
+ .toJSDate();
260
303
  }
261
304
  }
262
305
 
@@ -276,7 +319,10 @@ export function getMinutesFromDayStart(instant: Date, dayStart: Date): number {
276
319
  * Return the instant a number of minutes past a day's local start.
277
320
  */
278
321
 
279
- export function getInstantFromDayMinutes(dayStart: Date, minutes: number): Date {
322
+ export function getInstantFromDayMinutes(
323
+ dayStart: Date,
324
+ minutes: number,
325
+ ): Date {
280
326
  return new Date(dayStart.getTime() + minutes * 60_000);
281
327
  }
282
328
 
@@ -296,7 +342,11 @@ export function getDayLengthMinutes(day: CalendarDayObj): number {
296
342
  * Snap a minute offset to the nearest increment, clamped into the day.
297
343
  */
298
344
 
299
- export function snapMinutes(minutes: number, snapTo: number, dayLengthMinutes: number): number {
345
+ export function snapMinutes(
346
+ minutes: number,
347
+ snapTo: number,
348
+ dayLengthMinutes: number,
349
+ ): number {
300
350
  const snapped = Math.round(minutes / snapTo) * snapTo;
301
351
 
302
352
  return Math.max(0, Math.min(dayLengthMinutes, snapped));
@@ -311,17 +361,26 @@ export function snapMinutes(minutes: number, snapTo: number, dayLengthMinutes: n
311
361
  * where its days fall.
312
362
  */
313
363
 
314
- export function getCalendarRangeTitle(range: CalendarRangeObj, locale?: string | null): string {
364
+ export function getCalendarRangeTitle(
365
+ range: CalendarRangeObj,
366
+ locale?: string | null,
367
+ ): string {
315
368
  const zone = range.timeZone;
316
- const first = DateTime.fromJSDate(range.days[0].startAt, { zone }).setLocale(locale || 'en');
369
+ const first = DateTime.fromJSDate(range.days[0].startAt, { zone }).setLocale(
370
+ locale || 'en',
371
+ );
317
372
  const lastDay = range.days[range.days.length - 1];
318
- const last = DateTime.fromJSDate(new Date(lastDay.endAt.getTime() - 1), { zone }).setLocale(locale || 'en');
373
+ const last = DateTime.fromJSDate(new Date(lastDay.endAt.getTime() - 1), {
374
+ zone,
375
+ }).setLocale(locale || 'en');
319
376
 
320
377
  switch (range.view) {
321
378
  case 'YEAR':
322
379
  return first.toFormat('yyyy');
323
380
  case 'MONTH':
324
- return DateTime.fromJSDate(range.anchorDate, { zone }).setLocale(locale || 'en').toFormat('LLLL yyyy');
381
+ return DateTime.fromJSDate(range.anchorDate, { zone })
382
+ .setLocale(locale || 'en')
383
+ .toFormat('LLLL yyyy');
325
384
  case 'DAY':
326
385
  return first.toFormat('cccc, LLLL d, yyyy');
327
386
  default:
@@ -1,5 +1,8 @@
1
1
  import { atom } from 'jotai';
2
- import type { CalendarEventObj, CalendarSelectionObj } from '../types/calendar.d.ts';
2
+ import type {
3
+ CalendarEventObj,
4
+ CalendarSelectionObj,
5
+ } from '../types/calendar.d.ts';
3
6
 
4
7
  /**
5
8
  * Calendar app state.
@@ -76,7 +79,9 @@ export function createCalendarStateAtoms(scopeKey = '') {
76
79
  selectionAtom: atom<CalendarSelectionObj | null>(null),
77
80
 
78
81
  // Which overlaid calendars are drawn; absent means visible
79
- visibleCalendarIdsAtom: atom<Record<string, boolean>>(getInitialVisibleCalendarIds()),
82
+ visibleCalendarIdsAtom: atom<Record<string, boolean>>(
83
+ getInitialVisibleCalendarIds(),
84
+ ),
80
85
 
81
86
  // Optimistic edits applied over server data while a mutation is in
82
87
  // flight, keyed by event id. Cleared when authority arrives, so a
@@ -101,7 +106,9 @@ export type CalendarStateAtoms = ReturnType<typeof createCalendarStateAtoms>;
101
106
  * Return the stable scope key for one mounted calendar.
102
107
  */
103
108
 
104
- export function getCalendarStateScopeKey(organizationId?: string | null): string {
109
+ export function getCalendarStateScopeKey(
110
+ organizationId?: string | null,
111
+ ): string {
105
112
  return ['calendar', organizationId || 'pending'].join(':');
106
113
  }
107
114
 
@@ -4,7 +4,10 @@ import {
4
4
  CALENDAR_VIEW_SLUGS,
5
5
  type CalendarViewEnum,
6
6
  } from '../constants/calendar.ts';
7
- import { getCalendarDateKey, parseCalendarDateParam } from './calendar-range.ts';
7
+ import {
8
+ getCalendarDateKey,
9
+ parseCalendarDateParam,
10
+ } from './calendar-range.ts';
8
11
 
9
12
  /**
10
13
  * The calendar's URL state.
@@ -38,7 +41,10 @@ export interface CalendarUrlState {
38
41
  }
39
42
 
40
43
  const VIEW_BY_SLUG: Record<string, CalendarViewEnum> = Object.fromEntries(
41
- Object.entries(CALENDAR_VIEW_SLUGS).map(([view, slug]) => [slug, view as CalendarViewEnum]),
44
+ Object.entries(CALENDAR_VIEW_SLUGS).map(([view, slug]) => [
45
+ slug,
46
+ view as CalendarViewEnum,
47
+ ]),
42
48
  );
43
49
 
44
50
  /**
@@ -46,7 +52,9 @@ const VIEW_BY_SLUG: Record<string, CalendarViewEnum> = Object.fromEntries(
46
52
  */
47
53
 
48
54
  export function getCalendarViewSlug(view: CalendarViewEnum): string {
49
- return CALENDAR_VIEW_SLUGS[view] || CALENDAR_VIEW_SLUGS[CALENDAR_DEFAULT_VIEW];
55
+ return (
56
+ CALENDAR_VIEW_SLUGS[view] || CALENDAR_VIEW_SLUGS[CALENDAR_DEFAULT_VIEW]
57
+ );
50
58
  }
51
59
 
52
60
  /**
@@ -57,8 +65,12 @@ export function getCalendarViewSlug(view: CalendarViewEnum): string {
57
65
  * resetting to the week.
58
66
  */
59
67
 
60
- export function parseCalendarViewSlug(value: string | null | undefined): CalendarViewEnum {
61
- const raw = String(value || '').trim().toLowerCase();
68
+ export function parseCalendarViewSlug(
69
+ value: string | null | undefined,
70
+ ): CalendarViewEnum {
71
+ const raw = String(value || '')
72
+ .trim()
73
+ .toLowerCase();
62
74
 
63
75
  if (VIEW_BY_SLUG[raw]) {
64
76
  return VIEW_BY_SLUG[raw];
@@ -82,9 +94,18 @@ export function readCalendarUrlState(
82
94
 
83
95
  return {
84
96
  view: parseCalendarViewSlug(searchParams.get(CALENDAR_SEARCH_PARAMS.view)),
85
- anchorDate: parseCalendarDateParam(searchParams.get(CALENDAR_SEARCH_PARAMS.date), now, timeZone),
97
+ anchorDate: parseCalendarDateParam(
98
+ searchParams.get(CALENDAR_SEARCH_PARAMS.date),
99
+ now,
100
+ timeZone,
101
+ ),
86
102
  mineOnly: searchParams.get(CALENDAR_SEARCH_PARAMS.mine) === '1',
87
- hiddenCalendarIds: calendars ? calendars.split(',').map((id) => id.trim()).filter(Boolean) : [],
103
+ hiddenCalendarIds: calendars
104
+ ? calendars
105
+ .split(',')
106
+ .map((id) => id.trim())
107
+ .filter(Boolean)
108
+ : [],
88
109
  };
89
110
  }
90
111
 
@@ -115,7 +136,9 @@ export function writeCalendarUrlState(
115
136
  if (next.view !== undefined) {
116
137
  setOrDelete(
117
138
  CALENDAR_SEARCH_PARAMS.view,
118
- next.view === CALENDAR_DEFAULT_VIEW ? null : getCalendarViewSlug(next.view),
139
+ next.view === CALENDAR_DEFAULT_VIEW
140
+ ? null
141
+ : getCalendarViewSlug(next.view),
119
142
  );
120
143
  }
121
144
 
package/src/main.ts CHANGED
@@ -1,12 +1,15 @@
1
1
  export { Calendar } from './modules/Calendar.tsx';
2
+ export { MiniCalendar } from './modules/MiniCalendar.tsx';
2
3
  export { CalendarToolbarUI } from './ui/CalendarToolbarUI.tsx';
3
4
  export type { CalendarToolbarUIProps } from './ui/CalendarToolbarUI.tsx';
4
5
  export type { CalendarProps } from './modules/Calendar.tsx';
6
+ export type { MiniCalendarProps } from './modules/MiniCalendar.tsx';
5
7
  export {
6
8
  CALENDAR_SEARCH_PARAMS,
7
9
  CALENDAR_VIEW_ENUMS,
8
10
  type CalendarViewEnum,
9
11
  } from './constants/calendar.ts';
12
+ export { useCalendarNow } from './libs/calendar-now.ts';
10
13
  export {
11
14
  getCalendarDateKey,
12
15
  getCalendarRange,