@super-calendar/core 2.17.0 → 2.18.0
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/README.md +8 -0
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +13 -10
- package/dist/index.mjs +9 -11
- package/dist/{recurrence-CvIJfSMl.mjs → recurrence-15B6fu6K.mjs} +306 -101
- package/dist/{recurrence-D_oJ7A9D.js → recurrence-M-rJQgqd.js} +334 -99
- package/dist/{timezone-vbd68uRS.d.mts → timezone-BY2vQwRx.d.mts} +109 -13
- package/dist/{timezone-vbd68uRS.d.ts → timezone-BY2vQwRx.d.ts} +109 -13
- package/dist/vanilla.d.mts +2 -2
- package/dist/vanilla.d.ts +2 -2
- package/dist/vanilla.js +6 -1
- package/dist/vanilla.mjs +1 -1
- package/package.json +1 -1
- package/src/types.ts +7 -0
- package/src/utils/dates.ts +7 -2
- package/src/utils/drag.ts +14 -6
- package/src/utils/eventSource.ts +2 -0
- package/src/utils/ical.ts +5 -0
- package/src/utils/layout.ts +19 -5
- package/src/utils/monthGrid.ts +7 -1
- package/src/utils/multiWeekGrid.ts +2 -1
- package/src/utils/recurrence.ts +203 -77
- package/src/utils/timezone.ts +151 -48
- package/src/utils/useMonthGrid.ts +5 -0
- package/src/utils/useNow.ts +4 -9
- package/src/utils/zoneFields.ts +71 -0
|
@@ -159,8 +159,12 @@ const isWeekend = (date) => {
|
|
|
159
159
|
const day = date.getDay();
|
|
160
160
|
return day === 0 || day === 6;
|
|
161
161
|
};
|
|
162
|
-
/**
|
|
163
|
-
|
|
162
|
+
/**
|
|
163
|
+
* True when `date` falls on the current calendar day. Pass `today` to compare
|
|
164
|
+
* against that day instead of the device clock, e.g. the result of
|
|
165
|
+
* `nowInTimeZone` for a calendar displayed in another time zone.
|
|
166
|
+
*/
|
|
167
|
+
const getIsToday = (date, today) => today ? (0, date_fns.isSameDay)(date, today) : (0, date_fns.isToday)(date);
|
|
164
168
|
/** True when `a` and `b` are the same calendar day (ignoring the time of day). */
|
|
165
169
|
const isSameCalendarDay = (a, b) => (0, date_fns.isSameDay)(a, b);
|
|
166
170
|
/** Minutes elapsed since midnight (0–1439). */
|
|
@@ -291,7 +295,7 @@ const clockSpan = (from, to, dayStart, nextDayStart) => {
|
|
|
291
295
|
function layoutDayEvents(events, day) {
|
|
292
296
|
const dayStart = (0, date_fns.startOfDay)(day);
|
|
293
297
|
const nextDayStart = (0, date_fns.addDays)(dayStart, 1);
|
|
294
|
-
const segments = events.filter((event) => !isAllDayEvent(event) && !isBackgroundEvent(event)).filter((event) => event.start < nextDayStart && event.end > dayStart).map((event) => {
|
|
298
|
+
const segments = events.filter((event) => !isAllDayEvent(event) && !isBackgroundEvent(event)).filter((event) => event.start < nextDayStart && (event.end > dayStart || event.end.getTime() === event.start.getTime() && event.start >= dayStart)).map((event) => {
|
|
295
299
|
const segStart = (0, date_fns.max)([event.start, dayStart]);
|
|
296
300
|
const segEnd = (0, date_fns.min)([event.end, nextDayStart]);
|
|
297
301
|
return {
|
|
@@ -304,6 +308,7 @@ function layoutDayEvents(events, day) {
|
|
|
304
308
|
const positioned = [];
|
|
305
309
|
let cluster = [];
|
|
306
310
|
let clusterEnd = Number.NEGATIVE_INFINITY;
|
|
311
|
+
const drawnEndOf = (seg) => Math.max(seg.end, seg.start + MIN_DURATION_HOURS);
|
|
307
312
|
const flushCluster = () => {
|
|
308
313
|
const columnEnds = [];
|
|
309
314
|
const columnOf = /* @__PURE__ */ new Map();
|
|
@@ -311,8 +316,8 @@ function layoutDayEvents(events, day) {
|
|
|
311
316
|
let column = columnEnds.findIndex((end) => end <= seg.start);
|
|
312
317
|
if (column === -1) {
|
|
313
318
|
column = columnEnds.length;
|
|
314
|
-
columnEnds.push(seg
|
|
315
|
-
} else columnEnds[column] = seg
|
|
319
|
+
columnEnds.push(drawnEndOf(seg));
|
|
320
|
+
} else columnEnds[column] = drawnEndOf(seg);
|
|
316
321
|
columnOf.set(seg, column);
|
|
317
322
|
}
|
|
318
323
|
for (const seg of cluster) positioned.push({
|
|
@@ -329,7 +334,7 @@ function layoutDayEvents(events, day) {
|
|
|
329
334
|
for (const seg of segments) {
|
|
330
335
|
if (cluster.length > 0 && seg.start >= clusterEnd) flushCluster();
|
|
331
336
|
cluster.push(seg);
|
|
332
|
-
clusterEnd = Math.max(clusterEnd, seg
|
|
337
|
+
clusterEnd = Math.max(clusterEnd, drawnEndOf(seg));
|
|
333
338
|
}
|
|
334
339
|
if (cluster.length > 0) flushCluster();
|
|
335
340
|
return positioned;
|
|
@@ -354,7 +359,7 @@ function eventDayKeys(event) {
|
|
|
354
359
|
const lastInstant = event.end > event.start ? /* @__PURE__ */ new Date(event.end.getTime() - 1) : event.start;
|
|
355
360
|
const last = (0, date_fns.startOfDay)(lastInstant);
|
|
356
361
|
const keys = [];
|
|
357
|
-
for (let cursor = first; cursor <= last; cursor = (0, date_fns.addDays)(cursor, 1)) keys.push(cursor.toISOString());
|
|
362
|
+
for (let cursor = first; cursor <= last; cursor = (0, date_fns.startOfDay)((0, date_fns.addDays)(cursor, 1))) keys.push(cursor.toISOString());
|
|
358
363
|
return keys;
|
|
359
364
|
}
|
|
360
365
|
/**
|
|
@@ -456,11 +461,19 @@ function backgroundBandsForDay(events, day) {
|
|
|
456
461
|
// src/utils/drag.ts
|
|
457
462
|
/**
|
|
458
463
|
* True when two time ranges overlap. Edges touching (one ends exactly when the
|
|
459
|
-
* next begins) do not count as an overlap.
|
|
460
|
-
*
|
|
464
|
+
* next begins) do not count as an overlap. A zero-length range occupies its own
|
|
465
|
+
* instant: it overlaps a range that contains that instant or starts at it, and
|
|
466
|
+
* another zero-length range at the same instant. Compares absolute instants, so
|
|
467
|
+
* events at the same wall-clock time on different days do not overlap.
|
|
461
468
|
*/
|
|
462
469
|
function eventsOverlap(aStart, aEnd, bStart, bEnd) {
|
|
463
|
-
|
|
470
|
+
const a0 = aStart.getTime();
|
|
471
|
+
const a1 = aEnd.getTime();
|
|
472
|
+
const b0 = bStart.getTime();
|
|
473
|
+
const b1 = bEnd.getTime();
|
|
474
|
+
if (a0 === a1) return b0 === b1 ? a0 === b0 : b0 <= a0 && a0 < b1;
|
|
475
|
+
if (b0 === b1) return a0 <= b0 && b0 < a1;
|
|
476
|
+
return a0 < b1 && b0 < a1;
|
|
464
477
|
}
|
|
465
478
|
/**
|
|
466
479
|
* True when the range `[start, end)` would overlap any event in `events` other
|
|
@@ -579,8 +592,8 @@ function resolveDraggedBounds(start, end, deltaStartMinutes, deltaEndMinutes, sn
|
|
|
579
592
|
}
|
|
580
593
|
/**
|
|
581
594
|
* The all-day range swept out between two day cells of the month grid, ordered
|
|
582
|
-
* whichever way the sweep ran: `start` is
|
|
583
|
-
*
|
|
595
|
+
* whichever way the sweep ran: `start` is the first local day's start and `end` is
|
|
596
|
+
* the next day's start after the last, so a one-day sweep yields a usable event. Shared
|
|
584
597
|
* by both renderers so a month drag-to-create means the same thing on each.
|
|
585
598
|
*/
|
|
586
599
|
function monthCreateRange(anchor, hover) {
|
|
@@ -590,7 +603,7 @@ function monthCreateRange(anchor, hover) {
|
|
|
590
603
|
const last = a.getTime() <= b.getTime() ? b : a;
|
|
591
604
|
return {
|
|
592
605
|
start: first,
|
|
593
|
-
end: (0, date_fns.addDays)(last, 1)
|
|
606
|
+
end: (0, date_fns.startOfDay)((0, date_fns.addDays)(last, 1))
|
|
594
607
|
};
|
|
595
608
|
}
|
|
596
609
|
/**
|
|
@@ -800,6 +813,56 @@ function monthVisibleCount(total, capacity) {
|
|
|
800
813
|
if (total <= capacity.full) return total;
|
|
801
814
|
return Math.max(1, capacity.withMore);
|
|
802
815
|
}
|
|
816
|
+
// src/utils/zoneFields.ts
|
|
817
|
+
const MAX_ZONE_FORMATTERS = 32;
|
|
818
|
+
const zoneFormatters = /* @__PURE__ */ new Map();
|
|
819
|
+
function zoneFormatter(timeZone) {
|
|
820
|
+
let formatter = zoneFormatters.get(timeZone);
|
|
821
|
+
if (!formatter) {
|
|
822
|
+
formatter = new Intl.DateTimeFormat("en-US", {
|
|
823
|
+
timeZone,
|
|
824
|
+
hourCycle: "h23",
|
|
825
|
+
year: "numeric",
|
|
826
|
+
month: "2-digit",
|
|
827
|
+
day: "2-digit",
|
|
828
|
+
hour: "2-digit",
|
|
829
|
+
minute: "2-digit",
|
|
830
|
+
second: "2-digit"
|
|
831
|
+
});
|
|
832
|
+
if (zoneFormatters.size >= MAX_ZONE_FORMATTERS) zoneFormatters.clear();
|
|
833
|
+
zoneFormatters.set(timeZone, formatter);
|
|
834
|
+
}
|
|
835
|
+
return formatter;
|
|
836
|
+
}
|
|
837
|
+
/**
|
|
838
|
+
* The wall-clock fields `date` reads in `timeZone`, as plain numbers. Unlike a
|
|
839
|
+
* device-local `Date` holding those fields, the result can't be bent by the
|
|
840
|
+
* device's own DST changes.
|
|
841
|
+
*/
|
|
842
|
+
function zoneFields(date, timeZone) {
|
|
843
|
+
const parts = zoneFormatter(timeZone).formatToParts(date);
|
|
844
|
+
const field = (type) => {
|
|
845
|
+
const part = parts.find((p) => p.type === type);
|
|
846
|
+
return part ? Number(part.value) : 0;
|
|
847
|
+
};
|
|
848
|
+
return [
|
|
849
|
+
field("year"),
|
|
850
|
+
field("month") - 1,
|
|
851
|
+
field("day"),
|
|
852
|
+
field("hour") % 24,
|
|
853
|
+
field("minute"),
|
|
854
|
+
field("second")
|
|
855
|
+
];
|
|
856
|
+
}
|
|
857
|
+
/**
|
|
858
|
+
* A calendar reading as a UTC time value. Fields may overflow and roll over.
|
|
859
|
+
* Setters, not `Date.UTC`, which reads a year below 100 as 19xx.
|
|
860
|
+
*/
|
|
861
|
+
function utcTime(year, month, day, hour = 0, minute = 0, second = 0, ms = 0) {
|
|
862
|
+
const date = /* @__PURE__ */ new Date(0);
|
|
863
|
+
date.setUTCFullYear(year, month, day);
|
|
864
|
+
return date.setUTCHours(hour, minute, second, ms);
|
|
865
|
+
}
|
|
803
866
|
// src/utils/timezone.ts
|
|
804
867
|
/**
|
|
805
868
|
* Reinterpret an instant as its wall-clock time in `timeZone`, returned as a
|
|
@@ -812,51 +875,125 @@ function monthVisibleCount(total, capacity) {
|
|
|
812
875
|
* original UTC instant, so don't round-trip it back to a real time.
|
|
813
876
|
*/
|
|
814
877
|
function toZonedTime(date, timeZone) {
|
|
815
|
-
const
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
const
|
|
826
|
-
|
|
827
|
-
return part ? Number(part.value) : 0;
|
|
828
|
-
};
|
|
829
|
-
const hour = field("hour") % 24;
|
|
830
|
-
return new Date(field("year"), field("month") - 1, field("day"), hour, field("minute"), field("second"), date.getMilliseconds());
|
|
878
|
+
const [year, month, day, hour, minute, second] = zoneFields(date, timeZone);
|
|
879
|
+
return new Date(year, month, day, hour, minute, second, date.getMilliseconds());
|
|
880
|
+
}
|
|
881
|
+
/**
|
|
882
|
+
* The current instant, or the fixed `now` when given, as the wall clock of
|
|
883
|
+
* `timeZone` (see {@link toZonedTime}). Without a `timeZone` it is the instant
|
|
884
|
+
* itself, read on the device clock. Compare calendar days against it to find
|
|
885
|
+
* "today" for a calendar displayed in another time zone.
|
|
886
|
+
*/
|
|
887
|
+
function nowInTimeZone(now, timeZone) {
|
|
888
|
+
const instant = now ?? /* @__PURE__ */ new Date();
|
|
889
|
+
return timeZone ? toZonedTime(instant, timeZone) : instant;
|
|
831
890
|
}
|
|
832
891
|
function zoneOffsetMs(instant, timeZone) {
|
|
833
|
-
const
|
|
834
|
-
return
|
|
892
|
+
const [year, month, day, hour, minute, second] = zoneFields(instant, timeZone);
|
|
893
|
+
return utcTime(year, month, day, hour, minute, second, instant.getUTCMilliseconds()) - instant.getTime();
|
|
835
894
|
}
|
|
836
895
|
/**
|
|
837
896
|
* The inverse of {@link toZonedTime}: given a wall-clock time in `timeZone`,
|
|
838
897
|
* return the absolute UTC instant. Pass the wall clock as a `Date` whose **UTC**
|
|
839
898
|
* fields hold the components (e.g. `new Date(Date.UTC(y, m, d, h, min))`). Used to
|
|
840
|
-
* resolve iCal `TZID` times; DST-correct via a two-pass offset
|
|
841
|
-
*
|
|
899
|
+
* resolve iCal `TZID` times; DST-correct via a two-pass offset. A time in the
|
|
900
|
+
* hour a spring-forward change skips names no instant; it resolves the same
|
|
901
|
+
* distance after the skipped hour (02:30 becomes 03:30), as a device clock does.
|
|
842
902
|
*/
|
|
843
903
|
function zonedTimeToUtc(wallClock, timeZone) {
|
|
844
904
|
const guess = wallClock.getTime();
|
|
845
905
|
const firstPass = zoneOffsetMs(new Date(guess), timeZone);
|
|
846
906
|
const secondPass = zoneOffsetMs(new Date(guess - firstPass), timeZone);
|
|
847
|
-
|
|
907
|
+
const instant = new Date(guess - secondPass);
|
|
908
|
+
if (firstPass !== secondPass && zoneOffsetMs(instant, timeZone) !== secondPass) return new Date(guess - Math.min(firstPass, secondPass));
|
|
909
|
+
return instant;
|
|
910
|
+
}
|
|
911
|
+
/**
|
|
912
|
+
* The inverse of {@link toZonedTime}: the real instant for a date whose
|
|
913
|
+
* device-local fields hold a wall-clock time in `timeZone`, such as a date a
|
|
914
|
+
* calendar displaying that zone reports. DST-correct via {@link zonedTimeToUtc}.
|
|
915
|
+
*/
|
|
916
|
+
function fromZonedTime(date, timeZone) {
|
|
917
|
+
return zonedTimeToUtc(new Date(utcTime(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds())), timeZone);
|
|
848
918
|
}
|
|
919
|
+
const zonedSources = /* @__PURE__ */ new WeakMap();
|
|
849
920
|
/**
|
|
850
|
-
*
|
|
851
|
-
* displays them in `timeZone`. Other fields are preserved.
|
|
852
|
-
* (
|
|
921
|
+
* Shift every timed event's `start`/`end` through {@link toZonedTime} so the
|
|
922
|
+
* calendar displays them in `timeZone`. Other fields are preserved. All-day
|
|
923
|
+
* events (see `isAllDayEvent`) name calendar days, not instants, so they are
|
|
924
|
+
* returned as they are and stay on their days. Memoize the result (e.g. with
|
|
925
|
+
* `useMemo`) since it allocates new dates.
|
|
853
926
|
*/
|
|
854
927
|
function eventsInTimeZone(events, timeZone) {
|
|
855
|
-
return events.map((event) =>
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
928
|
+
return events.map((event) => {
|
|
929
|
+
if (isAllDayEvent(event)) return event;
|
|
930
|
+
const zoned = {
|
|
931
|
+
...event,
|
|
932
|
+
start: toZonedTime(event.start, timeZone),
|
|
933
|
+
end: toZonedTime(event.end, timeZone)
|
|
934
|
+
};
|
|
935
|
+
zonedSources.set(zoned, {
|
|
936
|
+
event,
|
|
937
|
+
timeZone
|
|
938
|
+
});
|
|
939
|
+
return zoned;
|
|
940
|
+
});
|
|
941
|
+
}
|
|
942
|
+
/**
|
|
943
|
+
* The event a display copy made by {@link eventsInTimeZone} stands for, with its
|
|
944
|
+
* real `start` and `end`. Any other event is returned as it is. Use it in the
|
|
945
|
+
* callbacks of a view that displays zoned events, to get back the event you
|
|
946
|
+
* passed in.
|
|
947
|
+
*/
|
|
948
|
+
function zonedEventSource(event) {
|
|
949
|
+
return zonedSources.get(event)?.event ?? event;
|
|
950
|
+
}
|
|
951
|
+
/**
|
|
952
|
+
* Convert the bounds a view reports for a dragged event back to real instants.
|
|
953
|
+
* `event` is the displayed event the view passed to `onDragEvent`, and `start`
|
|
954
|
+
* / `end` the bounds it reported. For a display copy made by
|
|
955
|
+
* {@link eventsInTimeZone}, each bound is read as a wall-clock time in that
|
|
956
|
+
* zone, following the zone's own DST changes; an edge the drag left alone keeps
|
|
957
|
+
* the source event's exact instant. Bounds of any other event (an all-day
|
|
958
|
+
* event, or one that was never shifted) are returned unchanged.
|
|
959
|
+
*
|
|
960
|
+
* Returns `null` when the reported span doesn't survive the conversion: an edge
|
|
961
|
+
* dropped into the hour a spring-forward change skips moves to after it, which
|
|
962
|
+
* can leave the event ending at or before its start. Reject such a drop.
|
|
963
|
+
*/
|
|
964
|
+
function zonedDragBounds(event, start, end, { keepDuration = false } = {}) {
|
|
965
|
+
const zoned = zonedSources.get(event);
|
|
966
|
+
if (!zoned) return {
|
|
967
|
+
start,
|
|
968
|
+
end
|
|
969
|
+
};
|
|
970
|
+
const source = zoned.event;
|
|
971
|
+
const startMoved = start.getTime() !== event.start.getTime();
|
|
972
|
+
const endMoved = end.getTime() !== event.end.getTime();
|
|
973
|
+
const realStart = startMoved ? fromZonedTime(start, zoned.timeZone) : source.start;
|
|
974
|
+
if (keepDuration && startMoved && endMoved) {
|
|
975
|
+
const durationMs = source.end.getTime() - source.start.getTime();
|
|
976
|
+
return {
|
|
977
|
+
start: realStart,
|
|
978
|
+
end: new Date(realStart.getTime() + durationMs)
|
|
979
|
+
};
|
|
980
|
+
}
|
|
981
|
+
return orderedLike(start, end, realStart, endMoved ? fromZonedTime(end, zoned.timeZone) : source.end);
|
|
982
|
+
}
|
|
983
|
+
/**
|
|
984
|
+
* Convert a wall-clock range a view reports in `timeZone` (a created event, for
|
|
985
|
+
* example) to real instants. Returns `null` when the span doesn't survive the
|
|
986
|
+
* conversion: a time in the hour a spring-forward change skips moves to after
|
|
987
|
+
* it, which can leave the range ending at or before its start.
|
|
988
|
+
*/
|
|
989
|
+
function fromZonedRange(start, end, timeZone) {
|
|
990
|
+
return orderedLike(start, end, fromZonedTime(start, timeZone), fromZonedTime(end, timeZone));
|
|
991
|
+
}
|
|
992
|
+
function orderedLike(start, end, realStart, realEnd) {
|
|
993
|
+
return end.getTime() > start.getTime() && realEnd.getTime() <= realStart.getTime() ? null : {
|
|
994
|
+
start: realStart,
|
|
995
|
+
end: realEnd
|
|
996
|
+
};
|
|
860
997
|
}
|
|
861
998
|
// src/utils/ical.ts
|
|
862
999
|
const RRULE_FREQ = {
|
|
@@ -990,6 +1127,8 @@ function parseRRule(value) {
|
|
|
990
1127
|
}
|
|
991
1128
|
if (plain.length) rule.weekdays = plain;
|
|
992
1129
|
}
|
|
1130
|
+
const wkst = BYDAY.indexOf(parts.get("WKST")?.trim().toUpperCase());
|
|
1131
|
+
if (wkst >= 0) rule.weekStartsOn = wkst;
|
|
993
1132
|
const bymonthday = parts.get("BYMONTHDAY");
|
|
994
1133
|
if (bymonthday) {
|
|
995
1134
|
const days = bymonthday.split(",").map((d) => Number(d.trim())).filter((d) => Number.isInteger(d) && d !== 0);
|
|
@@ -1009,6 +1148,7 @@ function formatRRule(rule) {
|
|
|
1009
1148
|
if (rule.until) parts.push(`UNTIL=${formatUtc(rule.until)}`);
|
|
1010
1149
|
if (rule.nthWeekday) parts.push(`BYDAY=${rule.nthWeekday.week}${BYDAY[rule.nthWeekday.weekday]}`);
|
|
1011
1150
|
else if (rule.weekdays?.length) parts.push(`BYDAY=${rule.weekdays.map((d) => BYDAY[d]).join(",")}`);
|
|
1151
|
+
if (rule.weekStartsOn != null && BYDAY[rule.weekStartsOn]) parts.push(`WKST=${BYDAY[rule.weekStartsOn]}`);
|
|
1012
1152
|
if (rule.monthDays?.length) parts.push(`BYMONTHDAY=${rule.monthDays.join(",")}`);
|
|
1013
1153
|
if (rule.months?.length) parts.push(`BYMONTH=${rule.months.join(",")}`);
|
|
1014
1154
|
return parts.join(";");
|
|
@@ -1202,6 +1342,7 @@ function recurrenceFromJSON(value, field) {
|
|
|
1202
1342
|
if (input.until !== void 0) rule.until = dateFromJSON(input.until, `${field}.until`);
|
|
1203
1343
|
for (const key of ["exdates", "rdates"]) if (input[key] !== void 0) rule[key] = arrayFromJSON(input[key], `${field}.${key}`).map((date, index) => dateFromJSON(date, `${field}.${key}[${index}]`));
|
|
1204
1344
|
if (input.weekdays !== void 0) rule.weekdays = arrayFromJSON(input.weekdays, `${field}.weekdays`).map((day) => weekdayFromJSON(day, `${field}.weekdays`));
|
|
1345
|
+
if (input.weekStartsOn !== void 0) rule.weekStartsOn = weekdayFromJSON(input.weekStartsOn, `${field}.weekStartsOn`);
|
|
1205
1346
|
if (input.months !== void 0) rule.months = arrayFromJSON(input.months, `${field}.months`).map((month) => integerFromJSON(month, `${field}.months`, 1, 12));
|
|
1206
1347
|
if (input.monthDays !== void 0) rule.monthDays = arrayFromJSON(input.monthDays, `${field}.monthDays`).map((day) => {
|
|
1207
1348
|
const parsed = integerFromJSON(day, `${field}.monthDays`, -31, 31);
|
|
@@ -1337,7 +1478,7 @@ function weekdayFormatToken(format) {
|
|
|
1337
1478
|
* data outside React; inside a component prefer {@link useMonthGrid}.
|
|
1338
1479
|
*/
|
|
1339
1480
|
function buildMonthGrid(month, options = {}) {
|
|
1340
|
-
const { weekStartsOn = 0, weekdayFormat = "short", showSixWeeks = false, isRTL = false, hiddenDays, selectedDates, selectedRange, minDate, maxDate, isDateDisabled, locale } = options;
|
|
1481
|
+
const { weekStartsOn = 0, weekdayFormat = "short", showSixWeeks = false, isRTL = false, hiddenDays, selectedDates, selectedRange, minDate, maxDate, isDateDisabled, locale, today } = options;
|
|
1341
1482
|
const rows = buildMonthWeeks(month, weekStartsOn, {
|
|
1342
1483
|
showSixWeeks,
|
|
1343
1484
|
isRTL,
|
|
@@ -1350,7 +1491,7 @@ function buildMonthGrid(month, options = {}) {
|
|
|
1350
1491
|
id: (0, date_fns.format)(date, "yyyy-MM-dd"),
|
|
1351
1492
|
label: (0, date_fns.format)(date, "d"),
|
|
1352
1493
|
isCurrentMonth: (0, date_fns.isSameMonth)(date, month),
|
|
1353
|
-
isToday: getIsToday(date),
|
|
1494
|
+
isToday: getIsToday(date, today),
|
|
1354
1495
|
isWeekend: isWeekend(date),
|
|
1355
1496
|
...daySelectionState(date, {
|
|
1356
1497
|
selectedDates,
|
|
@@ -1388,7 +1529,7 @@ function buildMonthGrid(month, options = {}) {
|
|
|
1388
1529
|
* safe integer, or the last week falls outside the range `Date` can represent.
|
|
1389
1530
|
*/
|
|
1390
1531
|
function buildMultiWeekGrid(date, options = {}) {
|
|
1391
|
-
const { numberOfWeeks = 2, weekStartsOn = 0, weekdayFormat = "short", isRTL = false, hiddenDays, selectedDates, selectedRange, minDate, maxDate, isDateDisabled, locale } = options;
|
|
1532
|
+
const { numberOfWeeks = 2, weekStartsOn = 0, weekdayFormat = "short", isRTL = false, hiddenDays, selectedDates, selectedRange, minDate, maxDate, isDateDisabled, locale, today } = options;
|
|
1392
1533
|
if (!(date instanceof Date) || Number.isNaN(date.getTime())) throw new RangeError("buildMultiWeekGrid: date must be a valid Date.");
|
|
1393
1534
|
if (!Number.isSafeInteger(numberOfWeeks) || numberOfWeeks < 1) throw new RangeError(`buildMultiWeekGrid: numberOfWeeks must be a positive whole number, received ${String(numberOfWeeks)}.`);
|
|
1394
1535
|
const start = (0, date_fns.startOfWeek)(date, { weekStartsOn });
|
|
@@ -1421,7 +1562,7 @@ function buildMultiWeekGrid(date, options = {}) {
|
|
|
1421
1562
|
date: day,
|
|
1422
1563
|
id: (0, date_fns.format)(day, "yyyy-MM-dd"),
|
|
1423
1564
|
label: (0, date_fns.format)(day, "d"),
|
|
1424
|
-
isToday: getIsToday(day),
|
|
1565
|
+
isToday: getIsToday(day, today),
|
|
1425
1566
|
isWeekend: isWeekend(day),
|
|
1426
1567
|
...daySelectionState(day, selection, constraints)
|
|
1427
1568
|
}))
|
|
@@ -1446,12 +1587,6 @@ function buildMultiWeekGrid(date, options = {}) {
|
|
|
1446
1587
|
};
|
|
1447
1588
|
}
|
|
1448
1589
|
// src/utils/recurrence.ts
|
|
1449
|
-
const STEP = {
|
|
1450
|
-
daily: date_fns.addDays,
|
|
1451
|
-
weekly: date_fns.addWeeks,
|
|
1452
|
-
monthly: date_fns.addMonths,
|
|
1453
|
-
yearly: date_fns.addYears
|
|
1454
|
-
};
|
|
1455
1590
|
const MAX_OCCURRENCES = 5e3;
|
|
1456
1591
|
const APPROX_STEP_MS = {
|
|
1457
1592
|
daily: 864e5,
|
|
@@ -1459,23 +1594,58 @@ const APPROX_STEP_MS = {
|
|
|
1459
1594
|
monthly: 2629746e3,
|
|
1460
1595
|
yearly: 31556952e3
|
|
1461
1596
|
};
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1597
|
+
const WEEKDAYS = /* @__PURE__ */ new Set([
|
|
1598
|
+
0,
|
|
1599
|
+
1,
|
|
1600
|
+
2,
|
|
1601
|
+
3,
|
|
1602
|
+
4,
|
|
1603
|
+
5,
|
|
1604
|
+
6
|
|
1605
|
+
]);
|
|
1606
|
+
const DEVICE_CLOCK = {
|
|
1607
|
+
fields: (instant) => [
|
|
1608
|
+
instant.getFullYear(),
|
|
1609
|
+
instant.getMonth(),
|
|
1610
|
+
instant.getDate(),
|
|
1611
|
+
instant.getHours(),
|
|
1612
|
+
instant.getMinutes(),
|
|
1613
|
+
instant.getSeconds(),
|
|
1614
|
+
instant.getMilliseconds()
|
|
1615
|
+
],
|
|
1616
|
+
at(year, month, day, hour, minute, second, ms) {
|
|
1617
|
+
const date = new Date(2e3, 0, 1);
|
|
1618
|
+
date.setFullYear(year, month, day);
|
|
1619
|
+
date.setHours(hour, minute, second, ms);
|
|
1620
|
+
return date;
|
|
1621
|
+
}
|
|
1622
|
+
};
|
|
1623
|
+
function zoneClock(timeZone, start) {
|
|
1624
|
+
const fields = (instant) => [...zoneFields(instant, timeZone), instant.getUTCMilliseconds()];
|
|
1625
|
+
const origin = fields(start);
|
|
1626
|
+
return {
|
|
1627
|
+
fields,
|
|
1628
|
+
at(...reading) {
|
|
1629
|
+
if (reading.every((value, index) => value === origin[index])) return new Date(start);
|
|
1630
|
+
const wallClock = utcTime(...reading);
|
|
1631
|
+
if (!(Math.abs(wallClock) <= 86399999136e5)) return /* @__PURE__ */ new Date(NaN);
|
|
1632
|
+
return zonedTimeToUtc(new Date(wallClock), timeZone);
|
|
1633
|
+
}
|
|
1634
|
+
};
|
|
1466
1635
|
}
|
|
1636
|
+
const weekdayOf = (year, month, day) => new Date(utcTime(year, month, day)).getUTCDay();
|
|
1637
|
+
const daysInMonth = (year, month) => new Date(utcTime(year, month + 1, 0)).getUTCDate();
|
|
1467
1638
|
function nthWeekdayOfMonth(year, month, week, weekday) {
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
}
|
|
1473
|
-
const day = 1 + (weekday - new Date(year, month, 1).getDay() + 7) % 7 + (week - 1) * 7;
|
|
1474
|
-
const date = new Date(year, month, day);
|
|
1475
|
-
return date.getMonth() === month ? date : null;
|
|
1639
|
+
const lastDay = daysInMonth(year, month);
|
|
1640
|
+
if (week === -1) return lastDay - (weekdayOf(year, month, lastDay) - weekday + 7) % 7;
|
|
1641
|
+
const day = 1 + (weekday - weekdayOf(year, month, 1) + 7) % 7 + (week - 1) * 7;
|
|
1642
|
+
return day <= lastDay ? day : null;
|
|
1476
1643
|
}
|
|
1477
|
-
function* occurrenceStarts(start, rule, earliestStart, rangeEnd) {
|
|
1644
|
+
function* occurrenceStarts(start, rule, earliestStart, rangeEnd, clock) {
|
|
1478
1645
|
const interval = Math.max(1, Math.trunc(rule.interval ?? 1));
|
|
1646
|
+
const [startYear, startMonth, startDay, hour, minute, second, ms] = clock.fields(start);
|
|
1647
|
+
const onDay = (year, month, day) => clock.at(year, month, day, hour, minute, second, ms);
|
|
1648
|
+
const dayStart = (year, month, day) => clock.at(year, month, day, 0, 0, 0, 0);
|
|
1479
1649
|
let produced = 0;
|
|
1480
1650
|
let emitted = 0;
|
|
1481
1651
|
const consider = (date) => {
|
|
@@ -1490,29 +1660,29 @@ function* occurrenceStarts(start, rule, earliestStart, rangeEnd) {
|
|
|
1490
1660
|
return "emit";
|
|
1491
1661
|
};
|
|
1492
1662
|
if (rule.freq === "weekly" && rule.weekdays?.length) {
|
|
1493
|
-
const
|
|
1494
|
-
|
|
1663
|
+
const weekStartsOn = WEEKDAYS.has(rule.weekStartsOn) ? rule.weekStartsOn : 1;
|
|
1664
|
+
const offsets = [...new Set(rule.weekdays.map((day) => (day - weekStartsOn + 7) % 7))].sort((a, b) => a - b);
|
|
1665
|
+
let weekStart = startDay - (weekdayOf(startYear, startMonth, startDay) - weekStartsOn + 7) % 7;
|
|
1495
1666
|
while (true) {
|
|
1496
|
-
for (const
|
|
1497
|
-
const date =
|
|
1667
|
+
for (const offset of offsets) {
|
|
1668
|
+
const date = onDay(startYear, startMonth, weekStart + offset);
|
|
1498
1669
|
const verdict = consider(date);
|
|
1499
1670
|
if (verdict === "stop") return;
|
|
1500
1671
|
if (verdict === "skip") continue;
|
|
1501
1672
|
yield date;
|
|
1502
1673
|
}
|
|
1503
|
-
|
|
1504
|
-
if (!(
|
|
1505
|
-
weekStart = nextWeek;
|
|
1674
|
+
weekStart += 7 * interval;
|
|
1675
|
+
if (!(dayStart(startYear, startMonth, weekStart).getTime() <= rangeEnd.getTime())) return;
|
|
1506
1676
|
}
|
|
1507
1677
|
}
|
|
1508
1678
|
if (rule.freq === "monthly" && rule.monthDays?.length) {
|
|
1509
|
-
let year =
|
|
1510
|
-
let month =
|
|
1679
|
+
let year = startYear;
|
|
1680
|
+
let month = startMonth;
|
|
1511
1681
|
while (true) {
|
|
1512
|
-
const
|
|
1513
|
-
const days = [...new Set(rule.monthDays.map((d) => d < 0 ?
|
|
1682
|
+
const lastDay = daysInMonth(year, month);
|
|
1683
|
+
const days = [...new Set(rule.monthDays.map((d) => d < 0 ? lastDay + d + 1 : d))].filter((d) => d >= 1 && d <= lastDay).sort((a, b) => a - b);
|
|
1514
1684
|
for (const d of days) {
|
|
1515
|
-
const date =
|
|
1685
|
+
const date = onDay(year, month, d);
|
|
1516
1686
|
const verdict = consider(date);
|
|
1517
1687
|
if (verdict === "stop") return;
|
|
1518
1688
|
if (verdict === "skip") continue;
|
|
@@ -1521,18 +1691,18 @@ function* occurrenceStarts(start, rule, earliestStart, rangeEnd) {
|
|
|
1521
1691
|
month += interval;
|
|
1522
1692
|
year += Math.floor(month / 12);
|
|
1523
1693
|
month = (month % 12 + 12) % 12;
|
|
1524
|
-
if (!(
|
|
1694
|
+
if (!(dayStart(year, month, 1).getTime() <= rangeEnd.getTime())) return;
|
|
1525
1695
|
}
|
|
1526
1696
|
}
|
|
1527
1697
|
if ((rule.freq === "monthly" || rule.freq === "yearly") && rule.nthWeekday) {
|
|
1528
1698
|
const { week, weekday } = rule.nthWeekday;
|
|
1529
1699
|
const stepMonths = rule.freq === "monthly" ? interval : 12 * interval;
|
|
1530
|
-
let year =
|
|
1531
|
-
let month =
|
|
1700
|
+
let year = startYear;
|
|
1701
|
+
let month = startMonth;
|
|
1532
1702
|
while (true) {
|
|
1533
1703
|
const day = nthWeekdayOfMonth(year, month, week, weekday);
|
|
1534
|
-
if (day) {
|
|
1535
|
-
const date =
|
|
1704
|
+
if (day !== null) {
|
|
1705
|
+
const date = onDay(year, month, day);
|
|
1536
1706
|
const verdict = consider(date);
|
|
1537
1707
|
if (verdict === "stop") return;
|
|
1538
1708
|
if (verdict === "emit") yield date;
|
|
@@ -1540,28 +1710,34 @@ function* occurrenceStarts(start, rule, earliestStart, rangeEnd) {
|
|
|
1540
1710
|
month += stepMonths;
|
|
1541
1711
|
year += Math.floor(month / 12);
|
|
1542
1712
|
month = (month % 12 + 12) % 12;
|
|
1543
|
-
if (!(
|
|
1713
|
+
if (!(dayStart(year, month, 1).getTime() <= rangeEnd.getTime())) return;
|
|
1544
1714
|
}
|
|
1545
1715
|
}
|
|
1546
1716
|
if (rule.freq === "yearly" && rule.months?.length) {
|
|
1547
|
-
const day = start.getDate();
|
|
1548
1717
|
const months = [...new Set(rule.months)].filter((m) => m >= 1 && m <= 12).sort((a, b) => a - b);
|
|
1549
|
-
let year =
|
|
1718
|
+
let year = startYear;
|
|
1550
1719
|
while (true) {
|
|
1551
1720
|
for (const m of months) {
|
|
1552
1721
|
const month = m - 1;
|
|
1553
|
-
if (
|
|
1554
|
-
const date =
|
|
1722
|
+
if (startDay > daysInMonth(year, month)) continue;
|
|
1723
|
+
const date = onDay(year, month, startDay);
|
|
1555
1724
|
const verdict = consider(date);
|
|
1556
1725
|
if (verdict === "stop") return;
|
|
1557
1726
|
if (verdict === "skip") continue;
|
|
1558
1727
|
yield date;
|
|
1559
1728
|
}
|
|
1560
1729
|
year += interval;
|
|
1561
|
-
if (!(
|
|
1730
|
+
if (!(dayStart(year, 0, 1).getTime() <= rangeEnd.getTime())) return;
|
|
1562
1731
|
}
|
|
1563
1732
|
}
|
|
1564
|
-
const occAt = (n) =>
|
|
1733
|
+
const occAt = (n) => {
|
|
1734
|
+
if (n === 0) return new Date(start.getTime());
|
|
1735
|
+
const steps = n * interval;
|
|
1736
|
+
if (rule.freq === "daily") return onDay(startYear, startMonth, startDay + steps);
|
|
1737
|
+
if (rule.freq === "weekly") return onDay(startYear, startMonth, startDay + 7 * steps);
|
|
1738
|
+
const month = startMonth + (rule.freq === "monthly" ? steps : 12 * steps);
|
|
1739
|
+
return onDay(startYear, month, Math.min(startDay, daysInMonth(startYear, month)));
|
|
1740
|
+
};
|
|
1565
1741
|
const lowerBound = earliestStart.getTime();
|
|
1566
1742
|
let n = 0;
|
|
1567
1743
|
if (occAt(0).getTime() < lowerBound) {
|
|
@@ -1589,13 +1765,18 @@ function instanceAt(event, start, durationMs) {
|
|
|
1589
1765
|
delete instance.recurrence;
|
|
1590
1766
|
return instance;
|
|
1591
1767
|
}
|
|
1768
|
+
const zonedOrInvalid = (date, timeZone) => Number.isNaN(date.getTime()) ? date : toZonedTime(date, timeZone);
|
|
1769
|
+
const isMidnight = (fields) => fields[3] === 0 && fields[4] === 0 && fields[5] === 0 && fields[6] === 0;
|
|
1592
1770
|
/**
|
|
1593
1771
|
* Materialise recurring events into concrete occurrences overlapping
|
|
1594
1772
|
* `[rangeStart, rangeEnd]`. Non-recurring events pass through untouched, so the
|
|
1595
1773
|
* result is ready to hand to `<Calendar events={...} />`. Each occurrence keeps
|
|
1596
1774
|
* the original event's duration and fields (minus `recurrence`).
|
|
1775
|
+
*
|
|
1776
|
+
* Pass `timeZone` to repeat timed events on that zone's wall clock instead of
|
|
1777
|
+
* the device's. `rangeStart` and `rangeEnd` are real instants either way.
|
|
1597
1778
|
*/
|
|
1598
|
-
function expandRecurringEvents(events, rangeStart, rangeEnd) {
|
|
1779
|
+
function expandRecurringEvents(events, rangeStart, rangeEnd, { timeZone } = {}) {
|
|
1599
1780
|
const out = [];
|
|
1600
1781
|
for (const event of events) {
|
|
1601
1782
|
if (!event.recurrence) {
|
|
@@ -1603,19 +1784,43 @@ function expandRecurringEvents(events, rangeStart, rangeEnd) {
|
|
|
1603
1784
|
continue;
|
|
1604
1785
|
}
|
|
1605
1786
|
const durationMs = event.end.getTime() - event.start.getTime();
|
|
1606
|
-
const
|
|
1787
|
+
const zoned = timeZone != null && !isAllDayEvent(event) && !Number.isNaN(event.start.getTime());
|
|
1788
|
+
const clock = zoned ? zoneClock(timeZone, event.start) : DEVICE_CLOCK;
|
|
1789
|
+
const [from, to] = timeZone != null && isAllDayEvent(event) ? [zonedOrInvalid(rangeStart, timeZone), zonedOrInvalid(rangeEnd, timeZone)] : [rangeStart, rangeEnd];
|
|
1790
|
+
const starts = /* @__PURE__ */ new Map();
|
|
1791
|
+
const earliestStart = new Date(from.getTime() - durationMs);
|
|
1792
|
+
for (const start of occurrenceStarts(event.start, event.recurrence, earliestStart, to, clock)) starts.set(start.getTime(), start);
|
|
1793
|
+
for (const rdate of event.recurrence.rdates ?? []) if (rdate.getTime() <= to.getTime()) starts.set(rdate.getTime(), rdate);
|
|
1794
|
+
const dayKey = (fields) => `${fields[0]}-${fields[1]}-${fields[2]}`;
|
|
1607
1795
|
const excludedDays = /* @__PURE__ */ new Set();
|
|
1608
1796
|
const excludedInstants = /* @__PURE__ */ new Set();
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
const
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1797
|
+
const startFields = clock.fields(event.start);
|
|
1798
|
+
const rdateTimes = new Set((event.recurrence.rdates ?? []).map((rdate) => rdate.getTime()));
|
|
1799
|
+
for (const exdate of event.recurrence.exdates ?? []) {
|
|
1800
|
+
if (Number.isNaN(exdate.getTime())) continue;
|
|
1801
|
+
const onDevice = DEVICE_CLOCK.fields(exdate);
|
|
1802
|
+
if (!zoned) {
|
|
1803
|
+
if (isMidnight(onDevice)) excludedDays.add(dayKey(onDevice));
|
|
1804
|
+
else excludedInstants.add(exdate.getTime());
|
|
1805
|
+
continue;
|
|
1806
|
+
}
|
|
1807
|
+
const inZone = clock.fields(exdate);
|
|
1808
|
+
excludedInstants.add(exdate.getTime());
|
|
1809
|
+
if (rdateTimes.has(exdate.getTime())) continue;
|
|
1810
|
+
if ([
|
|
1811
|
+
3,
|
|
1812
|
+
4,
|
|
1813
|
+
5,
|
|
1814
|
+
6
|
|
1815
|
+
].every((index) => inZone[index] === startFields[index])) continue;
|
|
1816
|
+
if (isMidnight(onDevice)) excludedDays.add(dayKey(onDevice));
|
|
1817
|
+
else if (isMidnight(inZone)) excludedDays.add(dayKey(inZone));
|
|
1818
|
+
}
|
|
1615
1819
|
const ordered = [...starts.values()].sort((a, b) => a.getTime() - b.getTime());
|
|
1616
1820
|
for (const start of ordered) {
|
|
1617
|
-
if (start.getTime() + durationMs <
|
|
1618
|
-
if (
|
|
1821
|
+
if (start.getTime() + durationMs < from.getTime()) continue;
|
|
1822
|
+
if (excludedInstants.has(start.getTime())) continue;
|
|
1823
|
+
if (excludedDays.size > 0 && excludedDays.has(dayKey(clock.fields(start)))) continue;
|
|
1619
1824
|
const instance = instanceAt(event, start, durationMs);
|
|
1620
1825
|
if (Number.isFinite(instance.end.getTime())) out.push(instance);
|
|
1621
1826
|
}
|
|
@@ -1778,6 +1983,18 @@ Object.defineProperty(exports, "formatHour", {
|
|
|
1778
1983
|
return formatHour;
|
|
1779
1984
|
}
|
|
1780
1985
|
});
|
|
1986
|
+
Object.defineProperty(exports, "fromZonedRange", {
|
|
1987
|
+
enumerable: true,
|
|
1988
|
+
get: function() {
|
|
1989
|
+
return fromZonedRange;
|
|
1990
|
+
}
|
|
1991
|
+
});
|
|
1992
|
+
Object.defineProperty(exports, "fromZonedTime", {
|
|
1993
|
+
enumerable: true,
|
|
1994
|
+
get: function() {
|
|
1995
|
+
return fromZonedTime;
|
|
1996
|
+
}
|
|
1997
|
+
});
|
|
1781
1998
|
Object.defineProperty(exports, "getIsToday", {
|
|
1782
1999
|
enumerable: true,
|
|
1783
2000
|
get: function() {
|
|
@@ -1922,6 +2139,12 @@ Object.defineProperty(exports, "nextDateRange", {
|
|
|
1922
2139
|
return nextDateRange;
|
|
1923
2140
|
}
|
|
1924
2141
|
});
|
|
2142
|
+
Object.defineProperty(exports, "nowInTimeZone", {
|
|
2143
|
+
enumerable: true,
|
|
2144
|
+
get: function() {
|
|
2145
|
+
return nowInTimeZone;
|
|
2146
|
+
}
|
|
2147
|
+
});
|
|
1925
2148
|
Object.defineProperty(exports, "overlapsOtherEvents", {
|
|
1926
2149
|
enumerable: true,
|
|
1927
2150
|
get: function() {
|
|
@@ -2018,6 +2241,18 @@ Object.defineProperty(exports, "weekdayFormatToken", {
|
|
|
2018
2241
|
return weekdayFormatToken;
|
|
2019
2242
|
}
|
|
2020
2243
|
});
|
|
2244
|
+
Object.defineProperty(exports, "zonedDragBounds", {
|
|
2245
|
+
enumerable: true,
|
|
2246
|
+
get: function() {
|
|
2247
|
+
return zonedDragBounds;
|
|
2248
|
+
}
|
|
2249
|
+
});
|
|
2250
|
+
Object.defineProperty(exports, "zonedEventSource", {
|
|
2251
|
+
enumerable: true,
|
|
2252
|
+
get: function() {
|
|
2253
|
+
return zonedEventSource;
|
|
2254
|
+
}
|
|
2255
|
+
});
|
|
2021
2256
|
Object.defineProperty(exports, "zonedTimeToUtc", {
|
|
2022
2257
|
enumerable: true,
|
|
2023
2258
|
get: function() {
|