@super-calendar/core 2.13.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.
- package/README.md +6 -0
- package/dist/index.d.mts +50 -3
- package/dist/index.d.ts +50 -3
- package/dist/index.js +275 -180
- package/dist/index.mjs +273 -182
- package/package.json +1 -1
- package/src/utils/drag.ts +69 -6
- package/src/utils/layout.ts +70 -12
- package/src/utils/minDuration.ts +4 -0
package/README.md
CHANGED
|
@@ -53,6 +53,12 @@ import { clampMoveStartMinutes } from "@super-calendar/core";
|
|
|
53
53
|
const startMinutes = clampMoveStartMinutes(rawStartMinutes, minHour, maxHour, snapMinutes);
|
|
54
54
|
```
|
|
55
55
|
|
|
56
|
+
To position your own marks on a time grid, `wallClockHours(date, dayStart, nextDayStart)`
|
|
57
|
+
returns the fractional clock hour, clamped to the day. It matches `layoutDayEvents`,
|
|
58
|
+
which places events by clock time so they line up with the hour labels on a
|
|
59
|
+
daylight saving change day. `clockHoursBetween(from, to)` returns how many rows a
|
|
60
|
+
span covers.
|
|
61
|
+
|
|
56
62
|
Use `resolveDraggedBounds` to commit a move or resize. Equal start and end deltas
|
|
57
63
|
move the event while preserving elapsed duration, including across daylight
|
|
58
64
|
saving changes. For a resize, pass zero for the edge you want to keep fixed.
|
package/dist/index.d.mts
CHANGED
|
@@ -427,8 +427,26 @@ export declare function snapDeltaMinutes(translationPx: number, cellHeightPx: nu
|
|
|
427
427
|
* spans midnight. Runs on the UI thread inside the drag gesture.
|
|
428
428
|
*/
|
|
429
429
|
export declare function clampMoveStartMinutes(startMinutes: number, minHour: number, maxHour: number, snapMinutes: number): number;
|
|
430
|
-
/**
|
|
430
|
+
/**
|
|
431
|
+
* A copy of `date` shifted by `minutes` (may be negative) on the clock, so a
|
|
432
|
+
* shift across a DST change keeps the clock time. When the resulting clock time
|
|
433
|
+
* occurs twice (the hour a fall-back change repeats), the occurrence nearer to
|
|
434
|
+
* `minutes` of elapsed time wins.
|
|
435
|
+
*/
|
|
431
436
|
export declare function shiftMinutes(date: Date, minutes: number): Date;
|
|
437
|
+
/**
|
|
438
|
+
* Where moving `[start, end]` by `minutes` on the clock lands, in clock hours:
|
|
439
|
+
* how far the start moves, and how many rows the moved span covers. The span
|
|
440
|
+
* keeps its elapsed duration, as {@link resolveDraggedBounds} commits a move, so
|
|
441
|
+
* gaining or losing a DST change changes its row count, and a start that lands
|
|
442
|
+
* in the hour a spring-forward change skips moves on past it. The row count is
|
|
443
|
+
* at least the layout's minimum box, as the committed event will be drawn.
|
|
444
|
+
* Worklet-safe, for live drag previews.
|
|
445
|
+
*/
|
|
446
|
+
export declare function movedClockSpan(start: Date, end: Date, minutes: number): {
|
|
447
|
+
startHours: number;
|
|
448
|
+
hours: number;
|
|
449
|
+
};
|
|
432
450
|
/**
|
|
433
451
|
* Resolve a committed drag into the event's new bounds: `start` shifts by
|
|
434
452
|
* `deltaStartMinutes`, `end` by `deltaEndMinutes` (a move passes the same delta
|
|
@@ -793,9 +811,13 @@ export declare function useEventSource<T = unknown>(source: string | (() => Prom
|
|
|
793
811
|
export type PositionedEvent<T> = {
|
|
794
812
|
/** The source event for this segment. */
|
|
795
813
|
event: CalendarEvent<T>;
|
|
796
|
-
/**
|
|
814
|
+
/**
|
|
815
|
+
* Clock time of the segment start on this day, in fractional hours: the
|
|
816
|
+
* time-grid row it starts in. On a DST change day this differs from the time
|
|
817
|
+
* elapsed since midnight (see {@link wallClockHours}).
|
|
818
|
+
*/
|
|
797
819
|
startHours: number;
|
|
798
|
-
/**
|
|
820
|
+
/** Clock hours the segment spans on this day (clamped to a small minimum). */
|
|
799
821
|
durationHours: number;
|
|
800
822
|
/** Zero-based column index within its overlap cluster. */
|
|
801
823
|
column: number;
|
|
@@ -805,6 +827,31 @@ export type PositionedEvent<T> = {
|
|
|
805
827
|
continuesBefore: boolean;
|
|
806
828
|
continuesAfter: boolean;
|
|
807
829
|
};
|
|
830
|
+
/**
|
|
831
|
+
* The fractional hour `date` shows on the clock, which is the time-grid row it
|
|
832
|
+
* belongs in, clamped to `[dayStart, nextDayStart]` so the midnight that ends
|
|
833
|
+
* the day reads 24. On a DST change day this differs from the time elapsed
|
|
834
|
+
* since midnight: after clocks go back, 10:00 is 11 hours in but reads 10.
|
|
835
|
+
* Worklet-safe, so a UI-thread preview places events the same way.
|
|
836
|
+
*/
|
|
837
|
+
export declare function wallClockHours(date: Date, dayStart: Date, nextDayStart: Date): number;
|
|
838
|
+
/**
|
|
839
|
+
* The hours between `from` and `to` as the clock reads them: the elapsed hours
|
|
840
|
+
* plus any DST offset change between the two, so 01:30 to 03:30 is 2 even on a
|
|
841
|
+
* spring-forward day when only 1 hour passes. In the hour a fall-back change
|
|
842
|
+
* repeats the clock can read earlier at `to` than at `from` (01:30 EDT to 01:00
|
|
843
|
+
* EST); such a span keeps its elapsed length so it still has a size to draw.
|
|
844
|
+
* Worklet-safe, so a UI-thread drag preview sizes boxes the same way.
|
|
845
|
+
*/
|
|
846
|
+
export declare function clockHoursBetween(from: Date, to: Date): number;
|
|
847
|
+
/**
|
|
848
|
+
* The instant a time grid draws the bottom edge of `[start, end]` at. That is
|
|
849
|
+
* `end`, except in the hour a fall-back change repeats: a span whose clock runs
|
|
850
|
+
* backwards (01:30 EDT to 01:00 EST) is drawn its elapsed length from `start`
|
|
851
|
+
* (see {@link clockHoursBetween}), so its edge reads 02:00. Resizing from that
|
|
852
|
+
* instant keeps the edge under the pointer. Worklet-safe.
|
|
853
|
+
*/
|
|
854
|
+
export declare function drawnEnd(start: Date, end: Date): Date;
|
|
808
855
|
/**
|
|
809
856
|
* Lay out a single day's events: events that overlap in time are split into
|
|
810
857
|
* side-by-side columns. Multi-day events are clipped to the portion that falls
|
package/dist/index.d.ts
CHANGED
|
@@ -427,8 +427,26 @@ export declare function snapDeltaMinutes(translationPx: number, cellHeightPx: nu
|
|
|
427
427
|
* spans midnight. Runs on the UI thread inside the drag gesture.
|
|
428
428
|
*/
|
|
429
429
|
export declare function clampMoveStartMinutes(startMinutes: number, minHour: number, maxHour: number, snapMinutes: number): number;
|
|
430
|
-
/**
|
|
430
|
+
/**
|
|
431
|
+
* A copy of `date` shifted by `minutes` (may be negative) on the clock, so a
|
|
432
|
+
* shift across a DST change keeps the clock time. When the resulting clock time
|
|
433
|
+
* occurs twice (the hour a fall-back change repeats), the occurrence nearer to
|
|
434
|
+
* `minutes` of elapsed time wins.
|
|
435
|
+
*/
|
|
431
436
|
export declare function shiftMinutes(date: Date, minutes: number): Date;
|
|
437
|
+
/**
|
|
438
|
+
* Where moving `[start, end]` by `minutes` on the clock lands, in clock hours:
|
|
439
|
+
* how far the start moves, and how many rows the moved span covers. The span
|
|
440
|
+
* keeps its elapsed duration, as {@link resolveDraggedBounds} commits a move, so
|
|
441
|
+
* gaining or losing a DST change changes its row count, and a start that lands
|
|
442
|
+
* in the hour a spring-forward change skips moves on past it. The row count is
|
|
443
|
+
* at least the layout's minimum box, as the committed event will be drawn.
|
|
444
|
+
* Worklet-safe, for live drag previews.
|
|
445
|
+
*/
|
|
446
|
+
export declare function movedClockSpan(start: Date, end: Date, minutes: number): {
|
|
447
|
+
startHours: number;
|
|
448
|
+
hours: number;
|
|
449
|
+
};
|
|
432
450
|
/**
|
|
433
451
|
* Resolve a committed drag into the event's new bounds: `start` shifts by
|
|
434
452
|
* `deltaStartMinutes`, `end` by `deltaEndMinutes` (a move passes the same delta
|
|
@@ -793,9 +811,13 @@ export declare function useEventSource<T = unknown>(source: string | (() => Prom
|
|
|
793
811
|
export type PositionedEvent<T> = {
|
|
794
812
|
/** The source event for this segment. */
|
|
795
813
|
event: CalendarEvent<T>;
|
|
796
|
-
/**
|
|
814
|
+
/**
|
|
815
|
+
* Clock time of the segment start on this day, in fractional hours: the
|
|
816
|
+
* time-grid row it starts in. On a DST change day this differs from the time
|
|
817
|
+
* elapsed since midnight (see {@link wallClockHours}).
|
|
818
|
+
*/
|
|
797
819
|
startHours: number;
|
|
798
|
-
/**
|
|
820
|
+
/** Clock hours the segment spans on this day (clamped to a small minimum). */
|
|
799
821
|
durationHours: number;
|
|
800
822
|
/** Zero-based column index within its overlap cluster. */
|
|
801
823
|
column: number;
|
|
@@ -805,6 +827,31 @@ export type PositionedEvent<T> = {
|
|
|
805
827
|
continuesBefore: boolean;
|
|
806
828
|
continuesAfter: boolean;
|
|
807
829
|
};
|
|
830
|
+
/**
|
|
831
|
+
* The fractional hour `date` shows on the clock, which is the time-grid row it
|
|
832
|
+
* belongs in, clamped to `[dayStart, nextDayStart]` so the midnight that ends
|
|
833
|
+
* the day reads 24. On a DST change day this differs from the time elapsed
|
|
834
|
+
* since midnight: after clocks go back, 10:00 is 11 hours in but reads 10.
|
|
835
|
+
* Worklet-safe, so a UI-thread preview places events the same way.
|
|
836
|
+
*/
|
|
837
|
+
export declare function wallClockHours(date: Date, dayStart: Date, nextDayStart: Date): number;
|
|
838
|
+
/**
|
|
839
|
+
* The hours between `from` and `to` as the clock reads them: the elapsed hours
|
|
840
|
+
* plus any DST offset change between the two, so 01:30 to 03:30 is 2 even on a
|
|
841
|
+
* spring-forward day when only 1 hour passes. In the hour a fall-back change
|
|
842
|
+
* repeats the clock can read earlier at `to` than at `from` (01:30 EDT to 01:00
|
|
843
|
+
* EST); such a span keeps its elapsed length so it still has a size to draw.
|
|
844
|
+
* Worklet-safe, so a UI-thread drag preview sizes boxes the same way.
|
|
845
|
+
*/
|
|
846
|
+
export declare function clockHoursBetween(from: Date, to: Date): number;
|
|
847
|
+
/**
|
|
848
|
+
* The instant a time grid draws the bottom edge of `[start, end]` at. That is
|
|
849
|
+
* `end`, except in the hour a fall-back change repeats: a span whose clock runs
|
|
850
|
+
* backwards (01:30 EDT to 01:00 EST) is drawn its elapsed length from `start`
|
|
851
|
+
* (see {@link clockHoursBetween}), so its edge reads 02:00. Resizing from that
|
|
852
|
+
* instant keeps the edge under the pointer. Worklet-safe.
|
|
853
|
+
*/
|
|
854
|
+
export declare function drawnEnd(start: Date, end: Date): Date;
|
|
808
855
|
/**
|
|
809
856
|
* Lay out a single day's events: events that overlap in time are split into
|
|
810
857
|
* side-by-side columns. Multi-day events are clipped to the portion that falls
|
package/dist/index.js
CHANGED
|
@@ -277,6 +277,230 @@ function useDateRange(options = {}) {
|
|
|
277
277
|
};
|
|
278
278
|
}
|
|
279
279
|
//#endregion
|
|
280
|
+
//#region src/utils/minDuration.ts
|
|
281
|
+
const MIN_DURATION_HOURS = .25;
|
|
282
|
+
//#endregion
|
|
283
|
+
//#region src/utils/layout.ts
|
|
284
|
+
/**
|
|
285
|
+
* The fractional hour `date` shows on the clock, which is the time-grid row it
|
|
286
|
+
* belongs in, clamped to `[dayStart, nextDayStart]` so the midnight that ends
|
|
287
|
+
* the day reads 24. On a DST change day this differs from the time elapsed
|
|
288
|
+
* since midnight: after clocks go back, 10:00 is 11 hours in but reads 10.
|
|
289
|
+
* Worklet-safe, so a UI-thread preview places events the same way.
|
|
290
|
+
*/
|
|
291
|
+
function wallClockHours(date, dayStart, nextDayStart) {
|
|
292
|
+
"worklet";
|
|
293
|
+
if (date.getTime() >= nextDayStart.getTime()) return 24;
|
|
294
|
+
if (date.getTime() <= dayStart.getTime()) return 0;
|
|
295
|
+
return date.getHours() + date.getMinutes() / 60 + date.getSeconds() / 3600 + date.getMilliseconds() / 36e5;
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* The hours between `from` and `to` as the clock reads them: the elapsed hours
|
|
299
|
+
* plus any DST offset change between the two, so 01:30 to 03:30 is 2 even on a
|
|
300
|
+
* spring-forward day when only 1 hour passes. In the hour a fall-back change
|
|
301
|
+
* repeats the clock can read earlier at `to` than at `from` (01:30 EDT to 01:00
|
|
302
|
+
* EST); such a span keeps its elapsed length so it still has a size to draw.
|
|
303
|
+
* Worklet-safe, so a UI-thread drag preview sizes boxes the same way.
|
|
304
|
+
*/
|
|
305
|
+
function clockHoursBetween(from, to) {
|
|
306
|
+
"worklet";
|
|
307
|
+
const elapsed = (to.getTime() - from.getTime()) / 36e5;
|
|
308
|
+
const clock = elapsed + (from.getTimezoneOffset() - to.getTimezoneOffset()) / 60;
|
|
309
|
+
return clock > 0 || elapsed <= 0 ? clock : elapsed;
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* The instant a time grid draws the bottom edge of `[start, end]` at. That is
|
|
313
|
+
* `end`, except in the hour a fall-back change repeats: a span whose clock runs
|
|
314
|
+
* backwards (01:30 EDT to 01:00 EST) is drawn its elapsed length from `start`
|
|
315
|
+
* (see {@link clockHoursBetween}), so its edge reads 02:00. Resizing from that
|
|
316
|
+
* instant keeps the edge under the pointer. Worklet-safe.
|
|
317
|
+
*/
|
|
318
|
+
function drawnEnd(start, end) {
|
|
319
|
+
"worklet";
|
|
320
|
+
const offsetChange = end.getTimezoneOffset() - start.getTimezoneOffset();
|
|
321
|
+
const elapsed = end.getTime() - start.getTime();
|
|
322
|
+
if (elapsed <= 0 || elapsed - offsetChange * 6e4 > 0) return end;
|
|
323
|
+
return new Date(end.getTime() + offsetChange * 6e4);
|
|
324
|
+
}
|
|
325
|
+
const clockSpan = (from, to, dayStart, nextDayStart) => {
|
|
326
|
+
const start = wallClockHours(from, dayStart, nextDayStart);
|
|
327
|
+
return {
|
|
328
|
+
start,
|
|
329
|
+
end: Math.min(24, start + clockHoursBetween(from, to))
|
|
330
|
+
};
|
|
331
|
+
};
|
|
332
|
+
/**
|
|
333
|
+
* Lay out a single day's events: events that overlap in time are split into
|
|
334
|
+
* side-by-side columns. Multi-day events are clipped to the portion that falls
|
|
335
|
+
* on `day` (e.g. a 23:00→01:00 event renders 23:00–24:00 on the start day and
|
|
336
|
+
* 00:00–01:00 on the next). Pure — safe to call per render, never per frame.
|
|
337
|
+
*/
|
|
338
|
+
function layoutDayEvents(events, day) {
|
|
339
|
+
const dayStart = (0, date_fns.startOfDay)(day);
|
|
340
|
+
const nextDayStart = (0, date_fns.addDays)(dayStart, 1);
|
|
341
|
+
const segments = events.filter((event) => !isAllDayEvent(event) && !isBackgroundEvent(event)).filter((event) => event.start < nextDayStart && event.end > dayStart).map((event) => {
|
|
342
|
+
const segStart = (0, date_fns.max)([event.start, dayStart]);
|
|
343
|
+
const segEnd = (0, date_fns.min)([event.end, nextDayStart]);
|
|
344
|
+
return {
|
|
345
|
+
event,
|
|
346
|
+
...clockSpan(segStart, segEnd, dayStart, nextDayStart),
|
|
347
|
+
continuesBefore: event.start < dayStart,
|
|
348
|
+
continuesAfter: event.end > nextDayStart
|
|
349
|
+
};
|
|
350
|
+
}).sort((a, b) => a.start - b.start);
|
|
351
|
+
const positioned = [];
|
|
352
|
+
let cluster = [];
|
|
353
|
+
let clusterEnd = Number.NEGATIVE_INFINITY;
|
|
354
|
+
const flushCluster = () => {
|
|
355
|
+
const columnEnds = [];
|
|
356
|
+
const columnOf = /* @__PURE__ */ new Map();
|
|
357
|
+
for (const seg of cluster) {
|
|
358
|
+
let column = columnEnds.findIndex((end) => end <= seg.start);
|
|
359
|
+
if (column === -1) {
|
|
360
|
+
column = columnEnds.length;
|
|
361
|
+
columnEnds.push(seg.end);
|
|
362
|
+
} else columnEnds[column] = seg.end;
|
|
363
|
+
columnOf.set(seg, column);
|
|
364
|
+
}
|
|
365
|
+
for (const seg of cluster) positioned.push({
|
|
366
|
+
event: seg.event,
|
|
367
|
+
startHours: seg.start,
|
|
368
|
+
durationHours: Math.max(seg.end - seg.start, MIN_DURATION_HOURS),
|
|
369
|
+
column: columnOf.get(seg) ?? 0,
|
|
370
|
+
columns: columnEnds.length,
|
|
371
|
+
continuesBefore: seg.continuesBefore,
|
|
372
|
+
continuesAfter: seg.continuesAfter
|
|
373
|
+
});
|
|
374
|
+
cluster = [];
|
|
375
|
+
};
|
|
376
|
+
for (const seg of segments) {
|
|
377
|
+
if (cluster.length > 0 && seg.start >= clusterEnd) flushCluster();
|
|
378
|
+
cluster.push(seg);
|
|
379
|
+
clusterEnd = Math.max(clusterEnd, seg.end);
|
|
380
|
+
}
|
|
381
|
+
if (cluster.length > 0) flushCluster();
|
|
382
|
+
return positioned;
|
|
383
|
+
}
|
|
384
|
+
const atMidnight = (date) => date.getHours() === 0 && date.getMinutes() === 0 && date.getSeconds() === 0 && date.getMilliseconds() === 0;
|
|
385
|
+
/**
|
|
386
|
+
* Whether an event belongs in the all-day lane. An explicit `allDay` flag wins;
|
|
387
|
+
* otherwise it's inferred when the event spans whole days (both `start` and
|
|
388
|
+
* `end` land on midnight, e.g. an iCal-style all-day event). Pure.
|
|
389
|
+
*/
|
|
390
|
+
function isAllDayEvent(event) {
|
|
391
|
+
if (typeof event.allDay === "boolean") return event.allDay;
|
|
392
|
+
return event.end > event.start && atMidnight(event.start) && atMidnight(event.end);
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* The `startOfDay` ISO keys of every calendar day an event touches (inclusive).
|
|
396
|
+
* An event ending exactly at midnight does not count the following day. Used to
|
|
397
|
+
* index events by day for the month grid. Pure.
|
|
398
|
+
*/
|
|
399
|
+
function eventDayKeys(event) {
|
|
400
|
+
const first = (0, date_fns.startOfDay)(event.start);
|
|
401
|
+
const lastInstant = event.end > event.start ? /* @__PURE__ */ new Date(event.end.getTime() - 1) : event.start;
|
|
402
|
+
const last = (0, date_fns.startOfDay)(lastInstant);
|
|
403
|
+
const keys = [];
|
|
404
|
+
for (let cursor = first; cursor <= last; cursor = (0, date_fns.addDays)(cursor, 1)) keys.push(cursor.toISOString());
|
|
405
|
+
return keys;
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* Index events by the `startOfDay` ISO key of every day they touch (via
|
|
409
|
+
* {@link eventDayKeys}), so a month grid can look up a day's events with
|
|
410
|
+
* `startOfDay(date).toISOString()`. Built once and shared across month cells.
|
|
411
|
+
*/
|
|
412
|
+
function groupEventsByDay(events) {
|
|
413
|
+
const map = /* @__PURE__ */ new Map();
|
|
414
|
+
for (const event of events) for (const key of eventDayKeys(event)) {
|
|
415
|
+
const list = map.get(key);
|
|
416
|
+
if (list) list.push(event);
|
|
417
|
+
else map.set(key, [event]);
|
|
418
|
+
}
|
|
419
|
+
return map;
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* Order a day's events for the month and list views: all-day events come first
|
|
423
|
+
* (they head the day regardless of their start time), then timed events by start.
|
|
424
|
+
* Shared by both renderers so the order is identical. Use as an `Array.sort`
|
|
425
|
+
* comparator.
|
|
426
|
+
*/
|
|
427
|
+
function compareDayEvents(a, b) {
|
|
428
|
+
const aAllDay = isAllDayEvent(a);
|
|
429
|
+
if (aAllDay !== isAllDayEvent(b)) return aAllDay ? -1 : 1;
|
|
430
|
+
return a.start.getTime() - b.start.getTime();
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* The closed hour-spans of a day to shade on the time grid, given a
|
|
434
|
+
* `businessHours` callback and the visible `[minHour, maxHour]` window: the spans
|
|
435
|
+
* before open and after close (clamped to the window), the whole window when the
|
|
436
|
+
* day is closed (`null`) or the open hours are inverted/empty, or none when the
|
|
437
|
+
* callback returns `undefined`. Shared by both renderers so shading stays
|
|
438
|
+
* identical. Co-located with `groupEventsByDay`; both feed the grid layout.
|
|
439
|
+
*/
|
|
440
|
+
function closedHourBands(day, businessHours, minHour = 0, maxHour = 24) {
|
|
441
|
+
const open = businessHours?.(day);
|
|
442
|
+
if (open === void 0) return [];
|
|
443
|
+
if (open === null) return [{
|
|
444
|
+
start: minHour,
|
|
445
|
+
end: maxHour
|
|
446
|
+
}];
|
|
447
|
+
const windows = (Array.isArray(open) ? open : [open]).map((w) => ({
|
|
448
|
+
start: Math.max(minHour, Math.min(maxHour, w.start)),
|
|
449
|
+
end: Math.max(minHour, Math.min(maxHour, w.end))
|
|
450
|
+
})).filter((w) => w.end > w.start).sort((a, b) => a.start - b.start);
|
|
451
|
+
if (windows.length === 0) return [{
|
|
452
|
+
start: minHour,
|
|
453
|
+
end: maxHour
|
|
454
|
+
}];
|
|
455
|
+
const merged = [];
|
|
456
|
+
for (const w of windows) {
|
|
457
|
+
const last = merged[merged.length - 1];
|
|
458
|
+
if (last && w.start <= last.end) last.end = Math.max(last.end, w.end);
|
|
459
|
+
else merged.push({ ...w });
|
|
460
|
+
}
|
|
461
|
+
const bands = [];
|
|
462
|
+
let cursor = minHour;
|
|
463
|
+
for (const w of merged) {
|
|
464
|
+
if (w.start > cursor) bands.push({
|
|
465
|
+
start: cursor,
|
|
466
|
+
end: w.start
|
|
467
|
+
});
|
|
468
|
+
cursor = w.end;
|
|
469
|
+
}
|
|
470
|
+
if (cursor < maxHour) bands.push({
|
|
471
|
+
start: cursor,
|
|
472
|
+
end: maxHour
|
|
473
|
+
});
|
|
474
|
+
return bands;
|
|
475
|
+
}
|
|
476
|
+
/** True when the event paints as a shaded background band, not an event box. */
|
|
477
|
+
function isBackgroundEvent(event) {
|
|
478
|
+
return event.display === "background";
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* The background events of `events` sliced to `day`, as fractional-hour bands
|
|
482
|
+
* (an all-day or multi-day background covers the day's full window). Shared by
|
|
483
|
+
* both renderers so the shading can't disagree.
|
|
484
|
+
*/
|
|
485
|
+
function backgroundBandsForDay(events, day) {
|
|
486
|
+
const dayStart = (0, date_fns.startOfDay)(day);
|
|
487
|
+
const nextDayStart = (0, date_fns.addDays)(dayStart, 1);
|
|
488
|
+
const out = [];
|
|
489
|
+
for (const event of events) {
|
|
490
|
+
if (!isBackgroundEvent(event)) continue;
|
|
491
|
+
if (event.start >= nextDayStart || event.end <= dayStart) continue;
|
|
492
|
+
const from = event.start > dayStart ? event.start : dayStart;
|
|
493
|
+
const to = event.end < nextDayStart ? event.end : nextDayStart;
|
|
494
|
+
const { start: startHours, end: endHours } = clockSpan(from, to, dayStart, nextDayStart);
|
|
495
|
+
if (endHours > startHours) out.push({
|
|
496
|
+
event,
|
|
497
|
+
startHours,
|
|
498
|
+
endHours
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
return out;
|
|
502
|
+
}
|
|
503
|
+
//#endregion
|
|
280
504
|
//#region src/utils/drag.ts
|
|
281
505
|
/**
|
|
282
506
|
* True when two time ranges overlap. Edges touching (one ends exactly when the
|
|
@@ -329,12 +553,50 @@ function clampMoveStartMinutes(startMinutes, minHour, maxHour, snapMinutes) {
|
|
|
329
553
|
const upper = Math.max(lower, maxHour * 60 - snapMinutes);
|
|
330
554
|
return Math.min(Math.max(startMinutes, lower), upper);
|
|
331
555
|
}
|
|
332
|
-
/**
|
|
556
|
+
/**
|
|
557
|
+
* A copy of `date` shifted by `minutes` (may be negative) on the clock, so a
|
|
558
|
+
* shift across a DST change keeps the clock time. When the resulting clock time
|
|
559
|
+
* occurs twice (the hour a fall-back change repeats), the occurrence nearer to
|
|
560
|
+
* `minutes` of elapsed time wins.
|
|
561
|
+
*/
|
|
333
562
|
function shiftMinutes(date, minutes) {
|
|
334
563
|
"worklet";
|
|
335
564
|
const next = new Date(date);
|
|
336
565
|
next.setMinutes(next.getMinutes() + minutes);
|
|
337
|
-
return next;
|
|
566
|
+
return nearestOccurrence(next, date.getTime() + minutes * 6e4);
|
|
567
|
+
}
|
|
568
|
+
function nearestOccurrence(date, target) {
|
|
569
|
+
"worklet";
|
|
570
|
+
const offset = date.getTimezoneOffset();
|
|
571
|
+
const later = new Date(date.getTime() + 108e5).getTimezoneOffset() - offset;
|
|
572
|
+
const earlier = offset - (/* @__PURE__ */ new Date(date.getTime() - 108e5)).getTimezoneOffset();
|
|
573
|
+
let best = date;
|
|
574
|
+
for (const change of [later, -earlier]) {
|
|
575
|
+
if (change === 0) continue;
|
|
576
|
+
const other = new Date(date.getTime() + change * 6e4);
|
|
577
|
+
if (other.getHours() !== date.getHours() || other.getMinutes() !== date.getMinutes()) continue;
|
|
578
|
+
if (Math.abs(other.getTime() - target) < Math.abs(best.getTime() - target)) best = other;
|
|
579
|
+
}
|
|
580
|
+
return best;
|
|
581
|
+
}
|
|
582
|
+
const localTime = (date) => date.getTime() - date.getTimezoneOffset() * 6e4;
|
|
583
|
+
/**
|
|
584
|
+
* Where moving `[start, end]` by `minutes` on the clock lands, in clock hours:
|
|
585
|
+
* how far the start moves, and how many rows the moved span covers. The span
|
|
586
|
+
* keeps its elapsed duration, as {@link resolveDraggedBounds} commits a move, so
|
|
587
|
+
* gaining or losing a DST change changes its row count, and a start that lands
|
|
588
|
+
* in the hour a spring-forward change skips moves on past it. The row count is
|
|
589
|
+
* at least the layout's minimum box, as the committed event will be drawn.
|
|
590
|
+
* Worklet-safe, for live drag previews.
|
|
591
|
+
*/
|
|
592
|
+
function movedClockSpan(start, end, minutes) {
|
|
593
|
+
"worklet";
|
|
594
|
+
const nextStart = shiftMinutes(start, minutes);
|
|
595
|
+
const nextEnd = new Date(nextStart.getTime() + end.getTime() - start.getTime());
|
|
596
|
+
return {
|
|
597
|
+
startHours: (nextStart.getTime() - start.getTime()) / 36e5 + (start.getTimezoneOffset() - nextStart.getTimezoneOffset()) / 60,
|
|
598
|
+
hours: Math.max(clockHoursBetween(nextStart, nextEnd), MIN_DURATION_HOURS)
|
|
599
|
+
};
|
|
338
600
|
}
|
|
339
601
|
/**
|
|
340
602
|
* Resolve a committed drag into the event's new bounds: `start` shifts by
|
|
@@ -349,7 +611,13 @@ function shiftMinutes(date, minutes) {
|
|
|
349
611
|
function resolveDraggedBounds(start, end, deltaStartMinutes, deltaEndMinutes, snapMinutes) {
|
|
350
612
|
const nextStart = shiftMinutes(start, deltaStartMinutes);
|
|
351
613
|
const oldDuration = end.getTime() - start.getTime();
|
|
352
|
-
|
|
614
|
+
let nextEnd = end;
|
|
615
|
+
if (deltaStartMinutes === deltaEndMinutes) nextEnd = new Date(nextStart.getTime() + oldDuration);
|
|
616
|
+
else if (deltaEndMinutes !== 0) {
|
|
617
|
+
const target = shiftMinutes(drawnEnd(start, end), deltaEndMinutes);
|
|
618
|
+
nextEnd = nearestOccurrence(target, end.getTime() + deltaEndMinutes * 6e4);
|
|
619
|
+
if (localTime(drawnEnd(nextStart, nextEnd)) !== localTime(target)) return null;
|
|
620
|
+
}
|
|
353
621
|
const newDuration = nextEnd.getTime() - nextStart.getTime();
|
|
354
622
|
if (newDuration < snapMinutes * 6e4 && newDuration < oldDuration) return null;
|
|
355
623
|
return {
|
|
@@ -1065,183 +1333,6 @@ function useEventSource(source, { format, map, refetchIntervalMs } = {}) {
|
|
|
1065
1333
|
};
|
|
1066
1334
|
}
|
|
1067
1335
|
//#endregion
|
|
1068
|
-
//#region src/utils/layout.ts
|
|
1069
|
-
const MINUTES_PER_HOUR = 60;
|
|
1070
|
-
const MIN_DURATION_HOURS = .25;
|
|
1071
|
-
/**
|
|
1072
|
-
* Lay out a single day's events: events that overlap in time are split into
|
|
1073
|
-
* side-by-side columns. Multi-day events are clipped to the portion that falls
|
|
1074
|
-
* on `day` (e.g. a 23:00→01:00 event renders 23:00–24:00 on the start day and
|
|
1075
|
-
* 00:00–01:00 on the next). Pure — safe to call per render, never per frame.
|
|
1076
|
-
*/
|
|
1077
|
-
function layoutDayEvents(events, day) {
|
|
1078
|
-
const dayStart = (0, date_fns.startOfDay)(day);
|
|
1079
|
-
const nextDayStart = (0, date_fns.addDays)(dayStart, 1);
|
|
1080
|
-
const segments = events.filter((event) => !isAllDayEvent(event) && !isBackgroundEvent(event)).filter((event) => event.start < nextDayStart && event.end > dayStart).map((event) => {
|
|
1081
|
-
const segStart = (0, date_fns.max)([event.start, dayStart]);
|
|
1082
|
-
const segEnd = (0, date_fns.min)([event.end, nextDayStart]);
|
|
1083
|
-
return {
|
|
1084
|
-
event,
|
|
1085
|
-
start: (0, date_fns.differenceInMinutes)(segStart, dayStart) / MINUTES_PER_HOUR,
|
|
1086
|
-
end: (0, date_fns.differenceInMinutes)(segEnd, dayStart) / MINUTES_PER_HOUR,
|
|
1087
|
-
continuesBefore: event.start < dayStart,
|
|
1088
|
-
continuesAfter: event.end > nextDayStart
|
|
1089
|
-
};
|
|
1090
|
-
}).sort((a, b) => a.start - b.start);
|
|
1091
|
-
const positioned = [];
|
|
1092
|
-
let cluster = [];
|
|
1093
|
-
let clusterEnd = Number.NEGATIVE_INFINITY;
|
|
1094
|
-
const flushCluster = () => {
|
|
1095
|
-
const columnEnds = [];
|
|
1096
|
-
const columnOf = /* @__PURE__ */ new Map();
|
|
1097
|
-
for (const seg of cluster) {
|
|
1098
|
-
let column = columnEnds.findIndex((end) => end <= seg.start);
|
|
1099
|
-
if (column === -1) {
|
|
1100
|
-
column = columnEnds.length;
|
|
1101
|
-
columnEnds.push(seg.end);
|
|
1102
|
-
} else columnEnds[column] = seg.end;
|
|
1103
|
-
columnOf.set(seg, column);
|
|
1104
|
-
}
|
|
1105
|
-
for (const seg of cluster) positioned.push({
|
|
1106
|
-
event: seg.event,
|
|
1107
|
-
startHours: seg.start,
|
|
1108
|
-
durationHours: Math.max(seg.end - seg.start, MIN_DURATION_HOURS),
|
|
1109
|
-
column: columnOf.get(seg) ?? 0,
|
|
1110
|
-
columns: columnEnds.length,
|
|
1111
|
-
continuesBefore: seg.continuesBefore,
|
|
1112
|
-
continuesAfter: seg.continuesAfter
|
|
1113
|
-
});
|
|
1114
|
-
cluster = [];
|
|
1115
|
-
};
|
|
1116
|
-
for (const seg of segments) {
|
|
1117
|
-
if (cluster.length > 0 && seg.start >= clusterEnd) flushCluster();
|
|
1118
|
-
cluster.push(seg);
|
|
1119
|
-
clusterEnd = Math.max(clusterEnd, seg.end);
|
|
1120
|
-
}
|
|
1121
|
-
if (cluster.length > 0) flushCluster();
|
|
1122
|
-
return positioned;
|
|
1123
|
-
}
|
|
1124
|
-
const atMidnight = (date) => date.getHours() === 0 && date.getMinutes() === 0 && date.getSeconds() === 0 && date.getMilliseconds() === 0;
|
|
1125
|
-
/**
|
|
1126
|
-
* Whether an event belongs in the all-day lane. An explicit `allDay` flag wins;
|
|
1127
|
-
* otherwise it's inferred when the event spans whole days (both `start` and
|
|
1128
|
-
* `end` land on midnight, e.g. an iCal-style all-day event). Pure.
|
|
1129
|
-
*/
|
|
1130
|
-
function isAllDayEvent(event) {
|
|
1131
|
-
if (typeof event.allDay === "boolean") return event.allDay;
|
|
1132
|
-
return event.end > event.start && atMidnight(event.start) && atMidnight(event.end);
|
|
1133
|
-
}
|
|
1134
|
-
/**
|
|
1135
|
-
* The `startOfDay` ISO keys of every calendar day an event touches (inclusive).
|
|
1136
|
-
* An event ending exactly at midnight does not count the following day. Used to
|
|
1137
|
-
* index events by day for the month grid. Pure.
|
|
1138
|
-
*/
|
|
1139
|
-
function eventDayKeys(event) {
|
|
1140
|
-
const first = (0, date_fns.startOfDay)(event.start);
|
|
1141
|
-
const lastInstant = event.end > event.start ? /* @__PURE__ */ new Date(event.end.getTime() - 1) : event.start;
|
|
1142
|
-
const last = (0, date_fns.startOfDay)(lastInstant);
|
|
1143
|
-
const keys = [];
|
|
1144
|
-
for (let cursor = first; cursor <= last; cursor = (0, date_fns.addDays)(cursor, 1)) keys.push(cursor.toISOString());
|
|
1145
|
-
return keys;
|
|
1146
|
-
}
|
|
1147
|
-
/**
|
|
1148
|
-
* Index events by the `startOfDay` ISO key of every day they touch (via
|
|
1149
|
-
* {@link eventDayKeys}), so a month grid can look up a day's events with
|
|
1150
|
-
* `startOfDay(date).toISOString()`. Built once and shared across month cells.
|
|
1151
|
-
*/
|
|
1152
|
-
function groupEventsByDay(events) {
|
|
1153
|
-
const map = /* @__PURE__ */ new Map();
|
|
1154
|
-
for (const event of events) for (const key of eventDayKeys(event)) {
|
|
1155
|
-
const list = map.get(key);
|
|
1156
|
-
if (list) list.push(event);
|
|
1157
|
-
else map.set(key, [event]);
|
|
1158
|
-
}
|
|
1159
|
-
return map;
|
|
1160
|
-
}
|
|
1161
|
-
/**
|
|
1162
|
-
* Order a day's events for the month and list views: all-day events come first
|
|
1163
|
-
* (they head the day regardless of their start time), then timed events by start.
|
|
1164
|
-
* Shared by both renderers so the order is identical. Use as an `Array.sort`
|
|
1165
|
-
* comparator.
|
|
1166
|
-
*/
|
|
1167
|
-
function compareDayEvents(a, b) {
|
|
1168
|
-
const aAllDay = isAllDayEvent(a);
|
|
1169
|
-
if (aAllDay !== isAllDayEvent(b)) return aAllDay ? -1 : 1;
|
|
1170
|
-
return a.start.getTime() - b.start.getTime();
|
|
1171
|
-
}
|
|
1172
|
-
/**
|
|
1173
|
-
* The closed hour-spans of a day to shade on the time grid, given a
|
|
1174
|
-
* `businessHours` callback and the visible `[minHour, maxHour]` window: the spans
|
|
1175
|
-
* before open and after close (clamped to the window), the whole window when the
|
|
1176
|
-
* day is closed (`null`) or the open hours are inverted/empty, or none when the
|
|
1177
|
-
* callback returns `undefined`. Shared by both renderers so shading stays
|
|
1178
|
-
* identical. Co-located with `groupEventsByDay`; both feed the grid layout.
|
|
1179
|
-
*/
|
|
1180
|
-
function closedHourBands(day, businessHours, minHour = 0, maxHour = 24) {
|
|
1181
|
-
const open = businessHours?.(day);
|
|
1182
|
-
if (open === void 0) return [];
|
|
1183
|
-
if (open === null) return [{
|
|
1184
|
-
start: minHour,
|
|
1185
|
-
end: maxHour
|
|
1186
|
-
}];
|
|
1187
|
-
const windows = (Array.isArray(open) ? open : [open]).map((w) => ({
|
|
1188
|
-
start: Math.max(minHour, Math.min(maxHour, w.start)),
|
|
1189
|
-
end: Math.max(minHour, Math.min(maxHour, w.end))
|
|
1190
|
-
})).filter((w) => w.end > w.start).sort((a, b) => a.start - b.start);
|
|
1191
|
-
if (windows.length === 0) return [{
|
|
1192
|
-
start: minHour,
|
|
1193
|
-
end: maxHour
|
|
1194
|
-
}];
|
|
1195
|
-
const merged = [];
|
|
1196
|
-
for (const w of windows) {
|
|
1197
|
-
const last = merged[merged.length - 1];
|
|
1198
|
-
if (last && w.start <= last.end) last.end = Math.max(last.end, w.end);
|
|
1199
|
-
else merged.push({ ...w });
|
|
1200
|
-
}
|
|
1201
|
-
const bands = [];
|
|
1202
|
-
let cursor = minHour;
|
|
1203
|
-
for (const w of merged) {
|
|
1204
|
-
if (w.start > cursor) bands.push({
|
|
1205
|
-
start: cursor,
|
|
1206
|
-
end: w.start
|
|
1207
|
-
});
|
|
1208
|
-
cursor = w.end;
|
|
1209
|
-
}
|
|
1210
|
-
if (cursor < maxHour) bands.push({
|
|
1211
|
-
start: cursor,
|
|
1212
|
-
end: maxHour
|
|
1213
|
-
});
|
|
1214
|
-
return bands;
|
|
1215
|
-
}
|
|
1216
|
-
/** True when the event paints as a shaded background band, not an event box. */
|
|
1217
|
-
function isBackgroundEvent(event) {
|
|
1218
|
-
return event.display === "background";
|
|
1219
|
-
}
|
|
1220
|
-
/**
|
|
1221
|
-
* The background events of `events` sliced to `day`, as fractional-hour bands
|
|
1222
|
-
* (an all-day or multi-day background covers the day's full window). Shared by
|
|
1223
|
-
* both renderers so the shading can't disagree.
|
|
1224
|
-
*/
|
|
1225
|
-
function backgroundBandsForDay(events, day) {
|
|
1226
|
-
const dayStart = (0, date_fns.startOfDay)(day);
|
|
1227
|
-
const nextDayStart = (0, date_fns.addDays)(dayStart, 1);
|
|
1228
|
-
const out = [];
|
|
1229
|
-
for (const event of events) {
|
|
1230
|
-
if (!isBackgroundEvent(event)) continue;
|
|
1231
|
-
if (event.start >= nextDayStart || event.end <= dayStart) continue;
|
|
1232
|
-
const from = event.start > dayStart ? event.start : dayStart;
|
|
1233
|
-
const to = event.end < nextDayStart ? event.end : nextDayStart;
|
|
1234
|
-
const startHours = (from.getTime() - dayStart.getTime()) / 36e5;
|
|
1235
|
-
const endHours = (to.getTime() - dayStart.getTime()) / 36e5;
|
|
1236
|
-
if (endHours > startHours) out.push({
|
|
1237
|
-
event,
|
|
1238
|
-
startHours,
|
|
1239
|
-
endHours
|
|
1240
|
-
});
|
|
1241
|
-
}
|
|
1242
|
-
return out;
|
|
1243
|
-
}
|
|
1244
|
-
//#endregion
|
|
1245
1336
|
//#region src/utils/monthGrid.ts
|
|
1246
1337
|
/** The last calendar day an event visibly covers (a midnight end ends the day before). */
|
|
1247
1338
|
function lastCoveredDay(event) {
|
|
@@ -1580,6 +1671,7 @@ exports.buildMonthGrid = buildMonthGrid;
|
|
|
1580
1671
|
exports.buildMonthWeeks = buildMonthWeeks;
|
|
1581
1672
|
exports.cellRangeFromDrag = cellRangeFromDrag;
|
|
1582
1673
|
exports.clampMoveStartMinutes = clampMoveStartMinutes;
|
|
1674
|
+
exports.clockHoursBetween = clockHoursBetween;
|
|
1583
1675
|
exports.closedHourBands = closedHourBands;
|
|
1584
1676
|
exports.compareDayEvents = compareDayEvents;
|
|
1585
1677
|
exports.darkColors = darkColors;
|
|
@@ -1587,6 +1679,7 @@ exports.dayAccessibilityLabel = dayAccessibilityLabel;
|
|
|
1587
1679
|
exports.dayBadgeKind = dayBadgeKind;
|
|
1588
1680
|
exports.daySelectionState = daySelectionState;
|
|
1589
1681
|
exports.defaultCalendarLabels = defaultCalendarLabels;
|
|
1682
|
+
exports.drawnEnd = drawnEnd;
|
|
1590
1683
|
exports.eventAccessibilityLabel = eventAccessibilityLabel;
|
|
1591
1684
|
exports.eventChipLayout = eventChipLayout;
|
|
1592
1685
|
exports.eventDayKeys = eventDayKeys;
|
|
@@ -1617,6 +1710,7 @@ exports.monthCreateRange = monthCreateRange;
|
|
|
1617
1710
|
exports.monthDropBounds = monthDropBounds;
|
|
1618
1711
|
exports.monthEventCapacity = monthEventCapacity;
|
|
1619
1712
|
exports.monthVisibleCount = monthVisibleCount;
|
|
1713
|
+
exports.movedClockSpan = movedClockSpan;
|
|
1620
1714
|
exports.nextDateRange = nextDateRange;
|
|
1621
1715
|
exports.overlapsOtherEvents = overlapsOtherEvents;
|
|
1622
1716
|
exports.pageStepDays = pageStepDays;
|
|
@@ -1636,6 +1730,7 @@ exports.useEventSource = useEventSource;
|
|
|
1636
1730
|
exports.useMonthGrid = useMonthGrid;
|
|
1637
1731
|
exports.useNow = useNow;
|
|
1638
1732
|
exports.viewDayCount = viewDayCount;
|
|
1733
|
+
exports.wallClockHours = wallClockHours;
|
|
1639
1734
|
exports.weekDaysCount = weekDaysCount;
|
|
1640
1735
|
exports.weekdayFormatToken = weekdayFormatToken;
|
|
1641
1736
|
exports.zonedTimeToUtc = zonedTimeToUtc;
|