@super-calendar/core 2.14.0 → 2.14.1

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,18 +1,18 @@
1
- import { addDays, differenceInMinutes, max as maxDate, min as minDate, startOfDay } from "date-fns";
1
+ import { addDays, max as maxDate, min as minDate, startOfDay } from "date-fns";
2
2
  import type { BusinessHours, CalendarEvent } from "../types";
3
-
4
- const MINUTES_PER_HOUR = 60;
5
- // Minimum duration (in hours) a positioned event is given, so a zero/negative
6
- // span still occupies a sliver rather than collapsing to nothing.
7
- const MIN_DURATION_HOURS = 0.25;
3
+ import { MIN_DURATION_HOURS } from "./minDuration";
8
4
 
9
5
  /** An event placed on a single day's time grid by {@link layoutDayEvents}, with its vertical span and overlap column. */
10
6
  export type PositionedEvent<T> = {
11
7
  /** The source event for this segment. */
12
8
  event: CalendarEvent<T>;
13
- /** Hours from midnight to the event's segment start on this day (fractional). */
9
+ /**
10
+ * Clock time of the segment start on this day, in fractional hours: the
11
+ * time-grid row it starts in. On a DST change day this differs from the time
12
+ * elapsed since midnight (see {@link wallClockHours}).
13
+ */
14
14
  startHours: number;
15
- /** Segment duration in hours on this day (clamped to a small minimum). */
15
+ /** Clock hours the segment spans on this day (clamped to a small minimum). */
16
16
  durationHours: number;
17
17
  /** Zero-based column index within its overlap cluster. */
18
18
  column: number;
@@ -31,6 +31,66 @@ type Segment<T> = {
31
31
  continuesAfter: boolean;
32
32
  };
33
33
 
34
+ /**
35
+ * The fractional hour `date` shows on the clock, which is the time-grid row it
36
+ * belongs in, clamped to `[dayStart, nextDayStart]` so the midnight that ends
37
+ * the day reads 24. On a DST change day this differs from the time elapsed
38
+ * since midnight: after clocks go back, 10:00 is 11 hours in but reads 10.
39
+ * Worklet-safe, so a UI-thread preview places events the same way.
40
+ */
41
+ export function wallClockHours(date: Date, dayStart: Date, nextDayStart: Date): number {
42
+ "worklet";
43
+ if (date.getTime() >= nextDayStart.getTime()) return 24;
44
+ if (date.getTime() <= dayStart.getTime()) return 0;
45
+ return (
46
+ date.getHours() +
47
+ date.getMinutes() / 60 +
48
+ date.getSeconds() / 3600 +
49
+ date.getMilliseconds() / 3_600_000
50
+ );
51
+ }
52
+
53
+ /**
54
+ * The hours between `from` and `to` as the clock reads them: the elapsed hours
55
+ * plus any DST offset change between the two, so 01:30 to 03:30 is 2 even on a
56
+ * spring-forward day when only 1 hour passes. In the hour a fall-back change
57
+ * repeats the clock can read earlier at `to` than at `from` (01:30 EDT to 01:00
58
+ * EST); such a span keeps its elapsed length so it still has a size to draw.
59
+ * Worklet-safe, so a UI-thread drag preview sizes boxes the same way.
60
+ */
61
+ export function clockHoursBetween(from: Date, to: Date): number {
62
+ "worklet";
63
+ const elapsed = (to.getTime() - from.getTime()) / 3_600_000;
64
+ const clock = elapsed + (from.getTimezoneOffset() - to.getTimezoneOffset()) / 60;
65
+ return clock > 0 || elapsed <= 0 ? clock : elapsed;
66
+ }
67
+
68
+ /**
69
+ * The instant a time grid draws the bottom edge of `[start, end]` at. That is
70
+ * `end`, except in the hour a fall-back change repeats: a span whose clock runs
71
+ * backwards (01:30 EDT to 01:00 EST) is drawn its elapsed length from `start`
72
+ * (see {@link clockHoursBetween}), so its edge reads 02:00. Resizing from that
73
+ * instant keeps the edge under the pointer. Worklet-safe.
74
+ */
75
+ export function drawnEnd(start: Date, end: Date): Date {
76
+ "worklet";
77
+ const offsetChange = end.getTimezoneOffset() - start.getTimezoneOffset();
78
+ const elapsed = end.getTime() - start.getTime();
79
+ if (elapsed <= 0 || elapsed - offsetChange * 60_000 > 0) return end;
80
+ return new Date(end.getTime() + offsetChange * 60_000);
81
+ }
82
+
83
+ // The clock-hour rows `[from, to]` covers on a day.
84
+ const clockSpan = (
85
+ from: Date,
86
+ to: Date,
87
+ dayStart: Date,
88
+ nextDayStart: Date,
89
+ ): { start: number; end: number } => {
90
+ const start = wallClockHours(from, dayStart, nextDayStart);
91
+ return { start, end: Math.min(24, start + clockHoursBetween(from, to)) };
92
+ };
93
+
34
94
  /**
35
95
  * Lay out a single day's events: events that overlap in time are split into
36
96
  * side-by-side columns. Multi-day events are clipped to the portion that falls
@@ -52,8 +112,7 @@ export function layoutDayEvents<T>(events: CalendarEvent<T>[], day: Date): Posit
52
112
  const segEnd = minDate([event.end, nextDayStart]);
53
113
  return {
54
114
  event,
55
- start: differenceInMinutes(segStart, dayStart) / MINUTES_PER_HOUR,
56
- end: differenceInMinutes(segEnd, dayStart) / MINUTES_PER_HOUR,
115
+ ...clockSpan(segStart, segEnd, dayStart, nextDayStart),
57
116
  continuesBefore: event.start < dayStart,
58
117
  continuesAfter: event.end > nextDayStart,
59
118
  };
@@ -235,8 +294,7 @@ export function backgroundBandsForDay<T>(
235
294
  if (event.start >= nextDayStart || event.end <= dayStart) continue;
236
295
  const from = event.start > dayStart ? event.start : dayStart;
237
296
  const to = event.end < nextDayStart ? event.end : nextDayStart;
238
- const startHours = (from.getTime() - dayStart.getTime()) / 3_600_000;
239
- const endHours = (to.getTime() - dayStart.getTime()) / 3_600_000;
297
+ const { start: startHours, end: endHours } = clockSpan(from, to, dayStart, nextDayStart);
240
298
  if (endHours > startHours) out.push({ event, startHours, endHours });
241
299
  }
242
300
  return out;
@@ -0,0 +1,4 @@
1
+ // Minimum duration (in hours) a positioned event is given, so a zero/negative
2
+ // span still occupies a sliver rather than collapsing to nothing. Internal: the
3
+ // layout and the drag previews share it, and it isn't part of the public API.
4
+ export const MIN_DURATION_HOURS = 0.25;