@super-calendar/core 2.8.2 → 2.9.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 +14 -0
- package/dist/index.d.mts +33 -2
- package/dist/index.d.ts +33 -2
- package/dist/index.js +50 -2
- package/dist/index.mjs +49 -4
- package/package.json +1 -1
- package/src/utils/drag.ts +59 -2
package/README.md
CHANGED
|
@@ -43,6 +43,20 @@ import { useDateRange } from "@super-calendar/core";
|
|
|
43
43
|
const range = useDateRange({ mode: "range" });
|
|
44
44
|
```
|
|
45
45
|
|
|
46
|
+
For a custom time-grid drag, `clampMoveStartMinutes` keeps the moved event's
|
|
47
|
+
start inside the visible hour window while allowing its unchanged duration to
|
|
48
|
+
carry the end past midnight:
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
import { clampMoveStartMinutes } from "@super-calendar/core";
|
|
52
|
+
|
|
53
|
+
const startMinutes = clampMoveStartMinutes(rawStartMinutes, minHour, maxHour, snapMinutes);
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Use `resolveDraggedBounds` to commit a move or resize. Equal start and end deltas
|
|
57
|
+
move the event while preserving elapsed duration, including across daylight
|
|
58
|
+
saving changes. For a resize, pass zero for the edge you want to keep fixed.
|
|
59
|
+
|
|
46
60
|
## Documentation
|
|
47
61
|
|
|
48
62
|
See the [full documentation](https://super-calendar.afonsojramos.me) and the [API reference](https://super-calendar.afonsojramos.me/reference/api).
|
package/dist/index.d.mts
CHANGED
|
@@ -401,12 +401,23 @@ declare function pageStepDays(mode: CalendarMode, date: Date, weekStartsOn: Week
|
|
|
401
401
|
* degenerate grid (non-positive height/step).
|
|
402
402
|
*/
|
|
403
403
|
declare function snapDeltaMinutes(translationPx: number, cellHeightPx: number, stepMinutes: number): number;
|
|
404
|
+
/**
|
|
405
|
+
* Where a moved event may start, in minutes from midnight, given the grid's
|
|
406
|
+
* visible hour window. A move keeps its duration, so only the start is held
|
|
407
|
+
* inside the window: it can reach one `snapMinutes` step before `maxHour`, and
|
|
408
|
+
* the end is free to run past the end of the day, continuing on the following
|
|
409
|
+
* day. That keeps the event starting in the column it was dropped on (a
|
|
410
|
+
* cross-day *move* is the horizontal drag) while still allowing a range that
|
|
411
|
+
* spans midnight. Runs on the UI thread inside the drag gesture.
|
|
412
|
+
*/
|
|
413
|
+
declare function clampMoveStartMinutes(startMinutes: number, minHour: number, maxHour: number, snapMinutes: number): number;
|
|
404
414
|
/** A copy of `date` shifted by `minutes` (may be negative). */
|
|
405
415
|
declare function shiftMinutes(date: Date, minutes: number): Date;
|
|
406
416
|
/**
|
|
407
417
|
* Resolve a committed drag into the event's new bounds: `start` shifts by
|
|
408
418
|
* `deltaStartMinutes`, `end` by `deltaEndMinutes` (a move passes the same delta
|
|
409
|
-
* to both
|
|
419
|
+
* to both and preserves elapsed duration, including across DST; a resize passes
|
|
420
|
+
* 0 for the unchanged edge). Returns `null` only when the change
|
|
410
421
|
* would *shrink* the event below one `snapMinutes` step, so a resize can't commit
|
|
411
422
|
* a degenerate duration; a pure move (both deltas equal) keeps its duration and is
|
|
412
423
|
* never rejected, even for an already sub-step event. Pure, so the commit path is
|
|
@@ -416,6 +427,26 @@ declare function resolveDraggedBounds(start: Date, end: Date, deltaStartMinutes:
|
|
|
416
427
|
start: Date;
|
|
417
428
|
end: Date;
|
|
418
429
|
} | null;
|
|
430
|
+
/**
|
|
431
|
+
* The all-day range swept out between two day cells of the month grid, ordered
|
|
432
|
+
* whichever way the sweep ran: `start` is midnight of the first day and `end` is
|
|
433
|
+
* midnight after the last, so a one-day sweep still yields a usable event. Shared
|
|
434
|
+
* by both renderers so a month drag-to-create means the same thing on each.
|
|
435
|
+
*/
|
|
436
|
+
declare function monthCreateRange(anchor: Date, hover: Date): {
|
|
437
|
+
start: Date;
|
|
438
|
+
end: Date;
|
|
439
|
+
};
|
|
440
|
+
/**
|
|
441
|
+
* The new bounds for an event picked up on `fromDay` and dropped on `toDay` in
|
|
442
|
+
* the month grid: both ends move by the same number of calendar days, so the
|
|
443
|
+
* event keeps its time of day and duration (DST included). Returns `null` when
|
|
444
|
+
* the drop lands on the day it started, so a stray drag commits nothing.
|
|
445
|
+
*/
|
|
446
|
+
declare function monthDropBounds<T>(event: CalendarEvent<T>, fromDay: Date, toDay: Date): {
|
|
447
|
+
start: Date;
|
|
448
|
+
end: Date;
|
|
449
|
+
} | null;
|
|
419
450
|
/**
|
|
420
451
|
* The start/end of a new event swept out on `day` by dragging from `startPx` to
|
|
421
452
|
* `endPx` (vertical pixels from the grid's top, i.e. the `minHour` line). Both
|
|
@@ -916,4 +947,4 @@ declare function zonedTimeToUtc(wallClock: Date, timeZone: string): Date;
|
|
|
916
947
|
*/
|
|
917
948
|
declare function eventsInTimeZone<T>(events: CalendarEvent<T>[], timeZone: string): CalendarEvent<T>[];
|
|
918
949
|
//#endregion
|
|
919
|
-
export { BusinessHours, BusinessHoursBand, BusinessHoursValue, CalendarColors, CalendarEvent, CalendarMode, CalendarSelection, CalendarSelectionProvider, DateRange, DateSelectionConstraints, DayBadgeKind, DaySelectionState, EventAccessibilityLabelContext, EventAccessibilityLabeler, EventChipLayout, EventKeyExtractor, EventSourceOptions, EventSourceState, ICalEvent, ICalendarEvent, MIN_BOX_HEIGHT_FOR_TIME, MonthEventCapacity, MonthEventSegment, MonthGrid, MonthGridDay, MonthGridWeek, MonthGridWeekday, MonthWeekEvents, PositionedEvent, RangeBandKind, RecurrenceFrequency, RecurrenceRule, TimeGridMode, ToICalendarOptions, UseDateRangeOptions, UseDateRangeResult, UseMonthGridOptions, UseNowOptions, WeekStartsOn, WeekdayFormat, backgroundBandsForDay, bandRounding, buildMonthGrid, buildMonthWeeks, cellRangeFromDrag, closedHourBands, compareDayEvents, darkColors, dayBadgeKind, daySelectionState, eventAccessibilityLabel, eventChipLayout, eventDayKeys, eventTimeLabel, eventsInTimeZone, eventsOverlap, expandRecurringEvents, filterHiddenDays, formatHour, getIsToday, getViewDays, getWeekDays, getYearMonths, groupEventsByDay, isAllDayEvent, isBackgroundEvent, isDateSelectable, isRangeEndpoint, isSameCalendarDay, isTimeVisibleAtHeight, isWeekend, isWithinDateRange, layoutDayEvents, layoutMonthWeek, lightColors, minutesIntoDay, monthEventCapacity, monthVisibleCount, nextDateRange, overlapsOtherEvents, pageStepDays, parseICalendar, rangeBandKind, resolveDraggedBounds, shiftMinutes, snapDeltaMinutes, titleEllipsizeMode, titleNumberOfLines, toICalendar, toZonedTime, useCalendarSelection, useDateRange, useEventSource, useMonthGrid, useNow, viewDayCount, weekDaysCount, weekdayFormatToken, zonedTimeToUtc };
|
|
950
|
+
export { BusinessHours, BusinessHoursBand, BusinessHoursValue, CalendarColors, CalendarEvent, CalendarMode, CalendarSelection, CalendarSelectionProvider, DateRange, DateSelectionConstraints, DayBadgeKind, DaySelectionState, EventAccessibilityLabelContext, EventAccessibilityLabeler, EventChipLayout, EventKeyExtractor, EventSourceOptions, EventSourceState, ICalEvent, ICalendarEvent, MIN_BOX_HEIGHT_FOR_TIME, MonthEventCapacity, MonthEventSegment, MonthGrid, MonthGridDay, MonthGridWeek, MonthGridWeekday, MonthWeekEvents, PositionedEvent, RangeBandKind, RecurrenceFrequency, RecurrenceRule, TimeGridMode, ToICalendarOptions, UseDateRangeOptions, UseDateRangeResult, UseMonthGridOptions, UseNowOptions, WeekStartsOn, WeekdayFormat, backgroundBandsForDay, bandRounding, buildMonthGrid, buildMonthWeeks, cellRangeFromDrag, clampMoveStartMinutes, closedHourBands, compareDayEvents, darkColors, dayBadgeKind, daySelectionState, eventAccessibilityLabel, eventChipLayout, eventDayKeys, eventTimeLabel, eventsInTimeZone, eventsOverlap, expandRecurringEvents, filterHiddenDays, formatHour, getIsToday, getViewDays, getWeekDays, getYearMonths, groupEventsByDay, isAllDayEvent, isBackgroundEvent, isDateSelectable, isRangeEndpoint, isSameCalendarDay, isTimeVisibleAtHeight, isWeekend, isWithinDateRange, layoutDayEvents, layoutMonthWeek, lightColors, minutesIntoDay, monthCreateRange, monthDropBounds, monthEventCapacity, monthVisibleCount, nextDateRange, overlapsOtherEvents, pageStepDays, parseICalendar, rangeBandKind, resolveDraggedBounds, shiftMinutes, snapDeltaMinutes, titleEllipsizeMode, titleNumberOfLines, toICalendar, toZonedTime, useCalendarSelection, useDateRange, useEventSource, useMonthGrid, useNow, viewDayCount, weekDaysCount, weekdayFormatToken, zonedTimeToUtc };
|
package/dist/index.d.ts
CHANGED
|
@@ -401,12 +401,23 @@ declare function pageStepDays(mode: CalendarMode, date: Date, weekStartsOn: Week
|
|
|
401
401
|
* degenerate grid (non-positive height/step).
|
|
402
402
|
*/
|
|
403
403
|
declare function snapDeltaMinutes(translationPx: number, cellHeightPx: number, stepMinutes: number): number;
|
|
404
|
+
/**
|
|
405
|
+
* Where a moved event may start, in minutes from midnight, given the grid's
|
|
406
|
+
* visible hour window. A move keeps its duration, so only the start is held
|
|
407
|
+
* inside the window: it can reach one `snapMinutes` step before `maxHour`, and
|
|
408
|
+
* the end is free to run past the end of the day, continuing on the following
|
|
409
|
+
* day. That keeps the event starting in the column it was dropped on (a
|
|
410
|
+
* cross-day *move* is the horizontal drag) while still allowing a range that
|
|
411
|
+
* spans midnight. Runs on the UI thread inside the drag gesture.
|
|
412
|
+
*/
|
|
413
|
+
declare function clampMoveStartMinutes(startMinutes: number, minHour: number, maxHour: number, snapMinutes: number): number;
|
|
404
414
|
/** A copy of `date` shifted by `minutes` (may be negative). */
|
|
405
415
|
declare function shiftMinutes(date: Date, minutes: number): Date;
|
|
406
416
|
/**
|
|
407
417
|
* Resolve a committed drag into the event's new bounds: `start` shifts by
|
|
408
418
|
* `deltaStartMinutes`, `end` by `deltaEndMinutes` (a move passes the same delta
|
|
409
|
-
* to both
|
|
419
|
+
* to both and preserves elapsed duration, including across DST; a resize passes
|
|
420
|
+
* 0 for the unchanged edge). Returns `null` only when the change
|
|
410
421
|
* would *shrink* the event below one `snapMinutes` step, so a resize can't commit
|
|
411
422
|
* a degenerate duration; a pure move (both deltas equal) keeps its duration and is
|
|
412
423
|
* never rejected, even for an already sub-step event. Pure, so the commit path is
|
|
@@ -416,6 +427,26 @@ declare function resolveDraggedBounds(start: Date, end: Date, deltaStartMinutes:
|
|
|
416
427
|
start: Date;
|
|
417
428
|
end: Date;
|
|
418
429
|
} | null;
|
|
430
|
+
/**
|
|
431
|
+
* The all-day range swept out between two day cells of the month grid, ordered
|
|
432
|
+
* whichever way the sweep ran: `start` is midnight of the first day and `end` is
|
|
433
|
+
* midnight after the last, so a one-day sweep still yields a usable event. Shared
|
|
434
|
+
* by both renderers so a month drag-to-create means the same thing on each.
|
|
435
|
+
*/
|
|
436
|
+
declare function monthCreateRange(anchor: Date, hover: Date): {
|
|
437
|
+
start: Date;
|
|
438
|
+
end: Date;
|
|
439
|
+
};
|
|
440
|
+
/**
|
|
441
|
+
* The new bounds for an event picked up on `fromDay` and dropped on `toDay` in
|
|
442
|
+
* the month grid: both ends move by the same number of calendar days, so the
|
|
443
|
+
* event keeps its time of day and duration (DST included). Returns `null` when
|
|
444
|
+
* the drop lands on the day it started, so a stray drag commits nothing.
|
|
445
|
+
*/
|
|
446
|
+
declare function monthDropBounds<T>(event: CalendarEvent<T>, fromDay: Date, toDay: Date): {
|
|
447
|
+
start: Date;
|
|
448
|
+
end: Date;
|
|
449
|
+
} | null;
|
|
419
450
|
/**
|
|
420
451
|
* The start/end of a new event swept out on `day` by dragging from `startPx` to
|
|
421
452
|
* `endPx` (vertical pixels from the grid's top, i.e. the `minHour` line). Both
|
|
@@ -916,4 +947,4 @@ declare function zonedTimeToUtc(wallClock: Date, timeZone: string): Date;
|
|
|
916
947
|
*/
|
|
917
948
|
declare function eventsInTimeZone<T>(events: CalendarEvent<T>[], timeZone: string): CalendarEvent<T>[];
|
|
918
949
|
//#endregion
|
|
919
|
-
export { BusinessHours, BusinessHoursBand, BusinessHoursValue, CalendarColors, CalendarEvent, CalendarMode, CalendarSelection, CalendarSelectionProvider, DateRange, DateSelectionConstraints, DayBadgeKind, DaySelectionState, EventAccessibilityLabelContext, EventAccessibilityLabeler, EventChipLayout, EventKeyExtractor, EventSourceOptions, EventSourceState, ICalEvent, ICalendarEvent, MIN_BOX_HEIGHT_FOR_TIME, MonthEventCapacity, MonthEventSegment, MonthGrid, MonthGridDay, MonthGridWeek, MonthGridWeekday, MonthWeekEvents, PositionedEvent, RangeBandKind, RecurrenceFrequency, RecurrenceRule, TimeGridMode, ToICalendarOptions, UseDateRangeOptions, UseDateRangeResult, UseMonthGridOptions, UseNowOptions, WeekStartsOn, WeekdayFormat, backgroundBandsForDay, bandRounding, buildMonthGrid, buildMonthWeeks, cellRangeFromDrag, closedHourBands, compareDayEvents, darkColors, dayBadgeKind, daySelectionState, eventAccessibilityLabel, eventChipLayout, eventDayKeys, eventTimeLabel, eventsInTimeZone, eventsOverlap, expandRecurringEvents, filterHiddenDays, formatHour, getIsToday, getViewDays, getWeekDays, getYearMonths, groupEventsByDay, isAllDayEvent, isBackgroundEvent, isDateSelectable, isRangeEndpoint, isSameCalendarDay, isTimeVisibleAtHeight, isWeekend, isWithinDateRange, layoutDayEvents, layoutMonthWeek, lightColors, minutesIntoDay, monthEventCapacity, monthVisibleCount, nextDateRange, overlapsOtherEvents, pageStepDays, parseICalendar, rangeBandKind, resolveDraggedBounds, shiftMinutes, snapDeltaMinutes, titleEllipsizeMode, titleNumberOfLines, toICalendar, toZonedTime, useCalendarSelection, useDateRange, useEventSource, useMonthGrid, useNow, viewDayCount, weekDaysCount, weekdayFormatToken, zonedTimeToUtc };
|
|
950
|
+
export { BusinessHours, BusinessHoursBand, BusinessHoursValue, CalendarColors, CalendarEvent, CalendarMode, CalendarSelection, CalendarSelectionProvider, DateRange, DateSelectionConstraints, DayBadgeKind, DaySelectionState, EventAccessibilityLabelContext, EventAccessibilityLabeler, EventChipLayout, EventKeyExtractor, EventSourceOptions, EventSourceState, ICalEvent, ICalendarEvent, MIN_BOX_HEIGHT_FOR_TIME, MonthEventCapacity, MonthEventSegment, MonthGrid, MonthGridDay, MonthGridWeek, MonthGridWeekday, MonthWeekEvents, PositionedEvent, RangeBandKind, RecurrenceFrequency, RecurrenceRule, TimeGridMode, ToICalendarOptions, UseDateRangeOptions, UseDateRangeResult, UseMonthGridOptions, UseNowOptions, WeekStartsOn, WeekdayFormat, backgroundBandsForDay, bandRounding, buildMonthGrid, buildMonthWeeks, cellRangeFromDrag, clampMoveStartMinutes, closedHourBands, compareDayEvents, darkColors, dayBadgeKind, daySelectionState, eventAccessibilityLabel, eventChipLayout, eventDayKeys, eventTimeLabel, eventsInTimeZone, eventsOverlap, expandRecurringEvents, filterHiddenDays, formatHour, getIsToday, getViewDays, getWeekDays, getYearMonths, groupEventsByDay, isAllDayEvent, isBackgroundEvent, isDateSelectable, isRangeEndpoint, isSameCalendarDay, isTimeVisibleAtHeight, isWeekend, isWithinDateRange, layoutDayEvents, layoutMonthWeek, lightColors, minutesIntoDay, monthCreateRange, monthDropBounds, monthEventCapacity, monthVisibleCount, nextDateRange, overlapsOtherEvents, pageStepDays, parseICalendar, rangeBandKind, resolveDraggedBounds, shiftMinutes, snapDeltaMinutes, titleEllipsizeMode, titleNumberOfLines, toICalendar, toZonedTime, useCalendarSelection, useDateRange, useEventSource, useMonthGrid, useNow, viewDayCount, weekDaysCount, weekdayFormatToken, zonedTimeToUtc };
|
package/dist/index.js
CHANGED
|
@@ -313,8 +313,24 @@ function snapDeltaMinutes(translationPx, cellHeightPx, stepMinutes) {
|
|
|
313
313
|
const rawMinutes = translationPx / cellHeightPx * 60;
|
|
314
314
|
return Math.round(rawMinutes / stepMinutes) * stepMinutes;
|
|
315
315
|
}
|
|
316
|
+
/**
|
|
317
|
+
* Where a moved event may start, in minutes from midnight, given the grid's
|
|
318
|
+
* visible hour window. A move keeps its duration, so only the start is held
|
|
319
|
+
* inside the window: it can reach one `snapMinutes` step before `maxHour`, and
|
|
320
|
+
* the end is free to run past the end of the day, continuing on the following
|
|
321
|
+
* day. That keeps the event starting in the column it was dropped on (a
|
|
322
|
+
* cross-day *move* is the horizontal drag) while still allowing a range that
|
|
323
|
+
* spans midnight. Runs on the UI thread inside the drag gesture.
|
|
324
|
+
*/
|
|
325
|
+
function clampMoveStartMinutes(startMinutes, minHour, maxHour, snapMinutes) {
|
|
326
|
+
"worklet";
|
|
327
|
+
const lower = minHour * 60;
|
|
328
|
+
const upper = Math.max(lower, maxHour * 60 - snapMinutes);
|
|
329
|
+
return Math.min(Math.max(startMinutes, lower), upper);
|
|
330
|
+
}
|
|
316
331
|
/** A copy of `date` shifted by `minutes` (may be negative). */
|
|
317
332
|
function shiftMinutes(date, minutes) {
|
|
333
|
+
"worklet";
|
|
318
334
|
const next = new Date(date);
|
|
319
335
|
next.setMinutes(next.getMinutes() + minutes);
|
|
320
336
|
return next;
|
|
@@ -322,7 +338,8 @@ function shiftMinutes(date, minutes) {
|
|
|
322
338
|
/**
|
|
323
339
|
* Resolve a committed drag into the event's new bounds: `start` shifts by
|
|
324
340
|
* `deltaStartMinutes`, `end` by `deltaEndMinutes` (a move passes the same delta
|
|
325
|
-
* to both
|
|
341
|
+
* to both and preserves elapsed duration, including across DST; a resize passes
|
|
342
|
+
* 0 for the unchanged edge). Returns `null` only when the change
|
|
326
343
|
* would *shrink* the event below one `snapMinutes` step, so a resize can't commit
|
|
327
344
|
* a degenerate duration; a pure move (both deltas equal) keeps its duration and is
|
|
328
345
|
* never rejected, even for an already sub-step event. Pure, so the commit path is
|
|
@@ -330,8 +347,8 @@ function shiftMinutes(date, minutes) {
|
|
|
330
347
|
*/
|
|
331
348
|
function resolveDraggedBounds(start, end, deltaStartMinutes, deltaEndMinutes, snapMinutes) {
|
|
332
349
|
const nextStart = shiftMinutes(start, deltaStartMinutes);
|
|
333
|
-
const nextEnd = shiftMinutes(end, deltaEndMinutes);
|
|
334
350
|
const oldDuration = end.getTime() - start.getTime();
|
|
351
|
+
const nextEnd = deltaStartMinutes === deltaEndMinutes ? new Date(nextStart.getTime() + oldDuration) : shiftMinutes(end, deltaEndMinutes);
|
|
335
352
|
const newDuration = nextEnd.getTime() - nextStart.getTime();
|
|
336
353
|
if (newDuration < snapMinutes * 6e4 && newDuration < oldDuration) return null;
|
|
337
354
|
return {
|
|
@@ -340,6 +357,34 @@ function resolveDraggedBounds(start, end, deltaStartMinutes, deltaEndMinutes, sn
|
|
|
340
357
|
};
|
|
341
358
|
}
|
|
342
359
|
/**
|
|
360
|
+
* The all-day range swept out between two day cells of the month grid, ordered
|
|
361
|
+
* whichever way the sweep ran: `start` is midnight of the first day and `end` is
|
|
362
|
+
* midnight after the last, so a one-day sweep still yields a usable event. Shared
|
|
363
|
+
* by both renderers so a month drag-to-create means the same thing on each.
|
|
364
|
+
*/
|
|
365
|
+
function monthCreateRange(anchor, hover) {
|
|
366
|
+
const a = (0, date_fns.startOfDay)(anchor);
|
|
367
|
+
const b = (0, date_fns.startOfDay)(hover);
|
|
368
|
+
return {
|
|
369
|
+
start: a.getTime() <= b.getTime() ? a : b,
|
|
370
|
+
end: (0, date_fns.addDays)(a.getTime() <= b.getTime() ? b : a, 1)
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* The new bounds for an event picked up on `fromDay` and dropped on `toDay` in
|
|
375
|
+
* the month grid: both ends move by the same number of calendar days, so the
|
|
376
|
+
* event keeps its time of day and duration (DST included). Returns `null` when
|
|
377
|
+
* the drop lands on the day it started, so a stray drag commits nothing.
|
|
378
|
+
*/
|
|
379
|
+
function monthDropBounds(event, fromDay, toDay) {
|
|
380
|
+
const delta = (0, date_fns.differenceInCalendarDays)((0, date_fns.startOfDay)(toDay), (0, date_fns.startOfDay)(fromDay));
|
|
381
|
+
if (delta === 0) return null;
|
|
382
|
+
return {
|
|
383
|
+
start: (0, date_fns.addDays)(event.start, delta),
|
|
384
|
+
end: (0, date_fns.addDays)(event.end, delta)
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
343
388
|
* The start/end of a new event swept out on `day` by dragging from `startPx` to
|
|
344
389
|
* `endPx` (vertical pixels from the grid's top, i.e. the `minHour` line). Both
|
|
345
390
|
* ends snap to `snapMinutes`; the range is ordered (drag up or down) and widened
|
|
@@ -1483,6 +1528,7 @@ exports.bandRounding = bandRounding;
|
|
|
1483
1528
|
exports.buildMonthGrid = buildMonthGrid;
|
|
1484
1529
|
exports.buildMonthWeeks = buildMonthWeeks;
|
|
1485
1530
|
exports.cellRangeFromDrag = cellRangeFromDrag;
|
|
1531
|
+
exports.clampMoveStartMinutes = clampMoveStartMinutes;
|
|
1486
1532
|
exports.closedHourBands = closedHourBands;
|
|
1487
1533
|
exports.compareDayEvents = compareDayEvents;
|
|
1488
1534
|
exports.darkColors = darkColors;
|
|
@@ -1514,6 +1560,8 @@ exports.layoutDayEvents = layoutDayEvents;
|
|
|
1514
1560
|
exports.layoutMonthWeek = layoutMonthWeek;
|
|
1515
1561
|
exports.lightColors = lightColors;
|
|
1516
1562
|
exports.minutesIntoDay = minutesIntoDay;
|
|
1563
|
+
exports.monthCreateRange = monthCreateRange;
|
|
1564
|
+
exports.monthDropBounds = monthDropBounds;
|
|
1517
1565
|
exports.monthEventCapacity = monthEventCapacity;
|
|
1518
1566
|
exports.monthVisibleCount = monthVisibleCount;
|
|
1519
1567
|
exports.nextDateRange = nextDateRange;
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { addDays, addMonths, addWeeks, addYears, differenceInMinutes, eachDayOfInterval, endOfMonth, endOfWeek, format, getHours, getMinutes, isAfter, isBefore, isSameDay, isSameMonth, isToday, max, min, startOfDay, startOfMonth, startOfWeek } from "date-fns";
|
|
1
|
+
import { addDays, addMonths, addWeeks, addYears, differenceInCalendarDays, differenceInMinutes, eachDayOfInterval, endOfMonth, endOfWeek, format, getHours, getMinutes, isAfter, isBefore, isSameDay, isSameMonth, isToday, max, min, startOfDay, startOfMonth, startOfWeek } from "date-fns";
|
|
2
2
|
import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
|
|
3
3
|
//#region src/tokens.ts
|
|
4
4
|
/** The default light-theme colour palette. */
|
|
@@ -312,8 +312,24 @@ function snapDeltaMinutes(translationPx, cellHeightPx, stepMinutes) {
|
|
|
312
312
|
const rawMinutes = translationPx / cellHeightPx * 60;
|
|
313
313
|
return Math.round(rawMinutes / stepMinutes) * stepMinutes;
|
|
314
314
|
}
|
|
315
|
+
/**
|
|
316
|
+
* Where a moved event may start, in minutes from midnight, given the grid's
|
|
317
|
+
* visible hour window. A move keeps its duration, so only the start is held
|
|
318
|
+
* inside the window: it can reach one `snapMinutes` step before `maxHour`, and
|
|
319
|
+
* the end is free to run past the end of the day, continuing on the following
|
|
320
|
+
* day. That keeps the event starting in the column it was dropped on (a
|
|
321
|
+
* cross-day *move* is the horizontal drag) while still allowing a range that
|
|
322
|
+
* spans midnight. Runs on the UI thread inside the drag gesture.
|
|
323
|
+
*/
|
|
324
|
+
function clampMoveStartMinutes(startMinutes, minHour, maxHour, snapMinutes) {
|
|
325
|
+
"worklet";
|
|
326
|
+
const lower = minHour * 60;
|
|
327
|
+
const upper = Math.max(lower, maxHour * 60 - snapMinutes);
|
|
328
|
+
return Math.min(Math.max(startMinutes, lower), upper);
|
|
329
|
+
}
|
|
315
330
|
/** A copy of `date` shifted by `minutes` (may be negative). */
|
|
316
331
|
function shiftMinutes(date, minutes) {
|
|
332
|
+
"worklet";
|
|
317
333
|
const next = new Date(date);
|
|
318
334
|
next.setMinutes(next.getMinutes() + minutes);
|
|
319
335
|
return next;
|
|
@@ -321,7 +337,8 @@ function shiftMinutes(date, minutes) {
|
|
|
321
337
|
/**
|
|
322
338
|
* Resolve a committed drag into the event's new bounds: `start` shifts by
|
|
323
339
|
* `deltaStartMinutes`, `end` by `deltaEndMinutes` (a move passes the same delta
|
|
324
|
-
* to both
|
|
340
|
+
* to both and preserves elapsed duration, including across DST; a resize passes
|
|
341
|
+
* 0 for the unchanged edge). Returns `null` only when the change
|
|
325
342
|
* would *shrink* the event below one `snapMinutes` step, so a resize can't commit
|
|
326
343
|
* a degenerate duration; a pure move (both deltas equal) keeps its duration and is
|
|
327
344
|
* never rejected, even for an already sub-step event. Pure, so the commit path is
|
|
@@ -329,8 +346,8 @@ function shiftMinutes(date, minutes) {
|
|
|
329
346
|
*/
|
|
330
347
|
function resolveDraggedBounds(start, end, deltaStartMinutes, deltaEndMinutes, snapMinutes) {
|
|
331
348
|
const nextStart = shiftMinutes(start, deltaStartMinutes);
|
|
332
|
-
const nextEnd = shiftMinutes(end, deltaEndMinutes);
|
|
333
349
|
const oldDuration = end.getTime() - start.getTime();
|
|
350
|
+
const nextEnd = deltaStartMinutes === deltaEndMinutes ? new Date(nextStart.getTime() + oldDuration) : shiftMinutes(end, deltaEndMinutes);
|
|
334
351
|
const newDuration = nextEnd.getTime() - nextStart.getTime();
|
|
335
352
|
if (newDuration < snapMinutes * 6e4 && newDuration < oldDuration) return null;
|
|
336
353
|
return {
|
|
@@ -339,6 +356,34 @@ function resolveDraggedBounds(start, end, deltaStartMinutes, deltaEndMinutes, sn
|
|
|
339
356
|
};
|
|
340
357
|
}
|
|
341
358
|
/**
|
|
359
|
+
* The all-day range swept out between two day cells of the month grid, ordered
|
|
360
|
+
* whichever way the sweep ran: `start` is midnight of the first day and `end` is
|
|
361
|
+
* midnight after the last, so a one-day sweep still yields a usable event. Shared
|
|
362
|
+
* by both renderers so a month drag-to-create means the same thing on each.
|
|
363
|
+
*/
|
|
364
|
+
function monthCreateRange(anchor, hover) {
|
|
365
|
+
const a = startOfDay(anchor);
|
|
366
|
+
const b = startOfDay(hover);
|
|
367
|
+
return {
|
|
368
|
+
start: a.getTime() <= b.getTime() ? a : b,
|
|
369
|
+
end: addDays(a.getTime() <= b.getTime() ? b : a, 1)
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* The new bounds for an event picked up on `fromDay` and dropped on `toDay` in
|
|
374
|
+
* the month grid: both ends move by the same number of calendar days, so the
|
|
375
|
+
* event keeps its time of day and duration (DST included). Returns `null` when
|
|
376
|
+
* the drop lands on the day it started, so a stray drag commits nothing.
|
|
377
|
+
*/
|
|
378
|
+
function monthDropBounds(event, fromDay, toDay) {
|
|
379
|
+
const delta = differenceInCalendarDays(startOfDay(toDay), startOfDay(fromDay));
|
|
380
|
+
if (delta === 0) return null;
|
|
381
|
+
return {
|
|
382
|
+
start: addDays(event.start, delta),
|
|
383
|
+
end: addDays(event.end, delta)
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
342
387
|
* The start/end of a new event swept out on `day` by dragging from `startPx` to
|
|
343
388
|
* `endPx` (vertical pixels from the grid's top, i.e. the `minHour` line). Both
|
|
344
389
|
* ends snap to `snapMinutes`; the range is ordered (drag up or down) and widened
|
|
@@ -1475,4 +1520,4 @@ function expandRecurringEvents(events, rangeStart, rangeEnd) {
|
|
|
1475
1520
|
return out;
|
|
1476
1521
|
}
|
|
1477
1522
|
//#endregion
|
|
1478
|
-
export { CalendarSelectionProvider, MIN_BOX_HEIGHT_FOR_TIME, backgroundBandsForDay, bandRounding, buildMonthGrid, buildMonthWeeks, cellRangeFromDrag, closedHourBands, compareDayEvents, darkColors, dayBadgeKind, daySelectionState, eventAccessibilityLabel, eventChipLayout, eventDayKeys, eventTimeLabel, eventsInTimeZone, eventsOverlap, expandRecurringEvents, filterHiddenDays, formatHour, getIsToday, getViewDays, getWeekDays, getYearMonths, groupEventsByDay, isAllDayEvent, isBackgroundEvent, isDateSelectable, isRangeEndpoint, isSameCalendarDay, isTimeVisibleAtHeight, isWeekend, isWithinDateRange, layoutDayEvents, layoutMonthWeek, lightColors, minutesIntoDay, monthEventCapacity, monthVisibleCount, nextDateRange, overlapsOtherEvents, pageStepDays, parseICalendar, rangeBandKind, resolveDraggedBounds, shiftMinutes, snapDeltaMinutes, titleEllipsizeMode, titleNumberOfLines, toICalendar, toZonedTime, useCalendarSelection, useDateRange, useEventSource, useMonthGrid, useNow, viewDayCount, weekDaysCount, weekdayFormatToken, zonedTimeToUtc };
|
|
1523
|
+
export { CalendarSelectionProvider, MIN_BOX_HEIGHT_FOR_TIME, backgroundBandsForDay, bandRounding, buildMonthGrid, buildMonthWeeks, cellRangeFromDrag, clampMoveStartMinutes, closedHourBands, compareDayEvents, darkColors, dayBadgeKind, daySelectionState, eventAccessibilityLabel, eventChipLayout, eventDayKeys, eventTimeLabel, eventsInTimeZone, eventsOverlap, expandRecurringEvents, filterHiddenDays, formatHour, getIsToday, getViewDays, getWeekDays, getYearMonths, groupEventsByDay, isAllDayEvent, isBackgroundEvent, isDateSelectable, isRangeEndpoint, isSameCalendarDay, isTimeVisibleAtHeight, isWeekend, isWithinDateRange, layoutDayEvents, layoutMonthWeek, lightColors, minutesIntoDay, monthCreateRange, monthDropBounds, monthEventCapacity, monthVisibleCount, nextDateRange, overlapsOtherEvents, pageStepDays, parseICalendar, rangeBandKind, resolveDraggedBounds, shiftMinutes, snapDeltaMinutes, titleEllipsizeMode, titleNumberOfLines, toICalendar, toZonedTime, useCalendarSelection, useDateRange, useEventSource, useMonthGrid, useNow, viewDayCount, weekDaysCount, weekdayFormatToken, zonedTimeToUtc };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@super-calendar/core",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.9.1",
|
|
4
4
|
"description": "Render-agnostic core for super-calendar: date math, selection model, event layout, the month-grid builder, headless hooks, and neutral theme tokens. No renderer.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"calendar",
|
package/src/utils/drag.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { addDays, differenceInCalendarDays, startOfDay } from "date-fns";
|
|
1
2
|
import type { CalendarEvent, CalendarMode, WeekStartsOn } from "../types";
|
|
2
3
|
import { getViewDays } from "./dates";
|
|
3
4
|
|
|
@@ -55,8 +56,30 @@ export function snapDeltaMinutes(
|
|
|
55
56
|
return Math.round(rawMinutes / stepMinutes) * stepMinutes;
|
|
56
57
|
}
|
|
57
58
|
|
|
59
|
+
/**
|
|
60
|
+
* Where a moved event may start, in minutes from midnight, given the grid's
|
|
61
|
+
* visible hour window. A move keeps its duration, so only the start is held
|
|
62
|
+
* inside the window: it can reach one `snapMinutes` step before `maxHour`, and
|
|
63
|
+
* the end is free to run past the end of the day, continuing on the following
|
|
64
|
+
* day. That keeps the event starting in the column it was dropped on (a
|
|
65
|
+
* cross-day *move* is the horizontal drag) while still allowing a range that
|
|
66
|
+
* spans midnight. Runs on the UI thread inside the drag gesture.
|
|
67
|
+
*/
|
|
68
|
+
export function clampMoveStartMinutes(
|
|
69
|
+
startMinutes: number,
|
|
70
|
+
minHour: number,
|
|
71
|
+
maxHour: number,
|
|
72
|
+
snapMinutes: number,
|
|
73
|
+
): number {
|
|
74
|
+
"worklet";
|
|
75
|
+
const lower = minHour * 60;
|
|
76
|
+
const upper = Math.max(lower, maxHour * 60 - snapMinutes);
|
|
77
|
+
return Math.min(Math.max(startMinutes, lower), upper);
|
|
78
|
+
}
|
|
79
|
+
|
|
58
80
|
/** A copy of `date` shifted by `minutes` (may be negative). */
|
|
59
81
|
export function shiftMinutes(date: Date, minutes: number): Date {
|
|
82
|
+
"worklet";
|
|
60
83
|
const next = new Date(date);
|
|
61
84
|
next.setMinutes(next.getMinutes() + minutes);
|
|
62
85
|
return next;
|
|
@@ -65,7 +88,8 @@ export function shiftMinutes(date: Date, minutes: number): Date {
|
|
|
65
88
|
/**
|
|
66
89
|
* Resolve a committed drag into the event's new bounds: `start` shifts by
|
|
67
90
|
* `deltaStartMinutes`, `end` by `deltaEndMinutes` (a move passes the same delta
|
|
68
|
-
* to both
|
|
91
|
+
* to both and preserves elapsed duration, including across DST; a resize passes
|
|
92
|
+
* 0 for the unchanged edge). Returns `null` only when the change
|
|
69
93
|
* would *shrink* the event below one `snapMinutes` step, so a resize can't commit
|
|
70
94
|
* a degenerate duration; a pure move (both deltas equal) keeps its duration and is
|
|
71
95
|
* never rejected, even for an already sub-step event. Pure, so the commit path is
|
|
@@ -79,14 +103,47 @@ export function resolveDraggedBounds(
|
|
|
79
103
|
snapMinutes: number,
|
|
80
104
|
): { start: Date; end: Date } | null {
|
|
81
105
|
const nextStart = shiftMinutes(start, deltaStartMinutes);
|
|
82
|
-
const nextEnd = shiftMinutes(end, deltaEndMinutes);
|
|
83
106
|
const oldDuration = end.getTime() - start.getTime();
|
|
107
|
+
const nextEnd =
|
|
108
|
+
deltaStartMinutes === deltaEndMinutes
|
|
109
|
+
? new Date(nextStart.getTime() + oldDuration)
|
|
110
|
+
: shiftMinutes(end, deltaEndMinutes);
|
|
84
111
|
const newDuration = nextEnd.getTime() - nextStart.getTime();
|
|
85
112
|
// Reject only a shrink past one step — never a move, which preserves duration.
|
|
86
113
|
if (newDuration < snapMinutes * 60_000 && newDuration < oldDuration) return null;
|
|
87
114
|
return { start: nextStart, end: nextEnd };
|
|
88
115
|
}
|
|
89
116
|
|
|
117
|
+
/**
|
|
118
|
+
* The all-day range swept out between two day cells of the month grid, ordered
|
|
119
|
+
* whichever way the sweep ran: `start` is midnight of the first day and `end` is
|
|
120
|
+
* midnight after the last, so a one-day sweep still yields a usable event. Shared
|
|
121
|
+
* by both renderers so a month drag-to-create means the same thing on each.
|
|
122
|
+
*/
|
|
123
|
+
export function monthCreateRange(anchor: Date, hover: Date): { start: Date; end: Date } {
|
|
124
|
+
const a = startOfDay(anchor);
|
|
125
|
+
const b = startOfDay(hover);
|
|
126
|
+
const first = a.getTime() <= b.getTime() ? a : b;
|
|
127
|
+
const last = a.getTime() <= b.getTime() ? b : a;
|
|
128
|
+
return { start: first, end: addDays(last, 1) };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* The new bounds for an event picked up on `fromDay` and dropped on `toDay` in
|
|
133
|
+
* the month grid: both ends move by the same number of calendar days, so the
|
|
134
|
+
* event keeps its time of day and duration (DST included). Returns `null` when
|
|
135
|
+
* the drop lands on the day it started, so a stray drag commits nothing.
|
|
136
|
+
*/
|
|
137
|
+
export function monthDropBounds<T>(
|
|
138
|
+
event: CalendarEvent<T>,
|
|
139
|
+
fromDay: Date,
|
|
140
|
+
toDay: Date,
|
|
141
|
+
): { start: Date; end: Date } | null {
|
|
142
|
+
const delta = differenceInCalendarDays(startOfDay(toDay), startOfDay(fromDay));
|
|
143
|
+
if (delta === 0) return null;
|
|
144
|
+
return { start: addDays(event.start, delta), end: addDays(event.end, delta) };
|
|
145
|
+
}
|
|
146
|
+
|
|
90
147
|
/**
|
|
91
148
|
* The start/end of a new event swept out on `day` by dragging from `startPx` to
|
|
92
149
|
* `endPx` (vertical pixels from the grid's top, i.e. the `minHour` line). Both
|