@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/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { addDays, addMonths, addWeeks, addYears, differenceInCalendarDays,
|
|
1
|
+
import { addDays, addMonths, addWeeks, addYears, differenceInCalendarDays, 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. */
|
|
@@ -276,6 +276,230 @@ function useDateRange(options = {}) {
|
|
|
276
276
|
};
|
|
277
277
|
}
|
|
278
278
|
//#endregion
|
|
279
|
+
//#region src/utils/minDuration.ts
|
|
280
|
+
const MIN_DURATION_HOURS = .25;
|
|
281
|
+
//#endregion
|
|
282
|
+
//#region src/utils/layout.ts
|
|
283
|
+
/**
|
|
284
|
+
* The fractional hour `date` shows on the clock, which is the time-grid row it
|
|
285
|
+
* belongs in, clamped to `[dayStart, nextDayStart]` so the midnight that ends
|
|
286
|
+
* the day reads 24. On a DST change day this differs from the time elapsed
|
|
287
|
+
* since midnight: after clocks go back, 10:00 is 11 hours in but reads 10.
|
|
288
|
+
* Worklet-safe, so a UI-thread preview places events the same way.
|
|
289
|
+
*/
|
|
290
|
+
function wallClockHours(date, dayStart, nextDayStart) {
|
|
291
|
+
"worklet";
|
|
292
|
+
if (date.getTime() >= nextDayStart.getTime()) return 24;
|
|
293
|
+
if (date.getTime() <= dayStart.getTime()) return 0;
|
|
294
|
+
return date.getHours() + date.getMinutes() / 60 + date.getSeconds() / 3600 + date.getMilliseconds() / 36e5;
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* The hours between `from` and `to` as the clock reads them: the elapsed hours
|
|
298
|
+
* plus any DST offset change between the two, so 01:30 to 03:30 is 2 even on a
|
|
299
|
+
* spring-forward day when only 1 hour passes. In the hour a fall-back change
|
|
300
|
+
* repeats the clock can read earlier at `to` than at `from` (01:30 EDT to 01:00
|
|
301
|
+
* EST); such a span keeps its elapsed length so it still has a size to draw.
|
|
302
|
+
* Worklet-safe, so a UI-thread drag preview sizes boxes the same way.
|
|
303
|
+
*/
|
|
304
|
+
function clockHoursBetween(from, to) {
|
|
305
|
+
"worklet";
|
|
306
|
+
const elapsed = (to.getTime() - from.getTime()) / 36e5;
|
|
307
|
+
const clock = elapsed + (from.getTimezoneOffset() - to.getTimezoneOffset()) / 60;
|
|
308
|
+
return clock > 0 || elapsed <= 0 ? clock : elapsed;
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* The instant a time grid draws the bottom edge of `[start, end]` at. That is
|
|
312
|
+
* `end`, except in the hour a fall-back change repeats: a span whose clock runs
|
|
313
|
+
* backwards (01:30 EDT to 01:00 EST) is drawn its elapsed length from `start`
|
|
314
|
+
* (see {@link clockHoursBetween}), so its edge reads 02:00. Resizing from that
|
|
315
|
+
* instant keeps the edge under the pointer. Worklet-safe.
|
|
316
|
+
*/
|
|
317
|
+
function drawnEnd(start, end) {
|
|
318
|
+
"worklet";
|
|
319
|
+
const offsetChange = end.getTimezoneOffset() - start.getTimezoneOffset();
|
|
320
|
+
const elapsed = end.getTime() - start.getTime();
|
|
321
|
+
if (elapsed <= 0 || elapsed - offsetChange * 6e4 > 0) return end;
|
|
322
|
+
return new Date(end.getTime() + offsetChange * 6e4);
|
|
323
|
+
}
|
|
324
|
+
const clockSpan = (from, to, dayStart, nextDayStart) => {
|
|
325
|
+
const start = wallClockHours(from, dayStart, nextDayStart);
|
|
326
|
+
return {
|
|
327
|
+
start,
|
|
328
|
+
end: Math.min(24, start + clockHoursBetween(from, to))
|
|
329
|
+
};
|
|
330
|
+
};
|
|
331
|
+
/**
|
|
332
|
+
* Lay out a single day's events: events that overlap in time are split into
|
|
333
|
+
* side-by-side columns. Multi-day events are clipped to the portion that falls
|
|
334
|
+
* on `day` (e.g. a 23:00→01:00 event renders 23:00–24:00 on the start day and
|
|
335
|
+
* 00:00–01:00 on the next). Pure — safe to call per render, never per frame.
|
|
336
|
+
*/
|
|
337
|
+
function layoutDayEvents(events, day) {
|
|
338
|
+
const dayStart = startOfDay(day);
|
|
339
|
+
const nextDayStart = addDays(dayStart, 1);
|
|
340
|
+
const segments = events.filter((event) => !isAllDayEvent(event) && !isBackgroundEvent(event)).filter((event) => event.start < nextDayStart && event.end > dayStart).map((event) => {
|
|
341
|
+
const segStart = max([event.start, dayStart]);
|
|
342
|
+
const segEnd = min([event.end, nextDayStart]);
|
|
343
|
+
return {
|
|
344
|
+
event,
|
|
345
|
+
...clockSpan(segStart, segEnd, dayStart, nextDayStart),
|
|
346
|
+
continuesBefore: event.start < dayStart,
|
|
347
|
+
continuesAfter: event.end > nextDayStart
|
|
348
|
+
};
|
|
349
|
+
}).sort((a, b) => a.start - b.start);
|
|
350
|
+
const positioned = [];
|
|
351
|
+
let cluster = [];
|
|
352
|
+
let clusterEnd = Number.NEGATIVE_INFINITY;
|
|
353
|
+
const flushCluster = () => {
|
|
354
|
+
const columnEnds = [];
|
|
355
|
+
const columnOf = /* @__PURE__ */ new Map();
|
|
356
|
+
for (const seg of cluster) {
|
|
357
|
+
let column = columnEnds.findIndex((end) => end <= seg.start);
|
|
358
|
+
if (column === -1) {
|
|
359
|
+
column = columnEnds.length;
|
|
360
|
+
columnEnds.push(seg.end);
|
|
361
|
+
} else columnEnds[column] = seg.end;
|
|
362
|
+
columnOf.set(seg, column);
|
|
363
|
+
}
|
|
364
|
+
for (const seg of cluster) positioned.push({
|
|
365
|
+
event: seg.event,
|
|
366
|
+
startHours: seg.start,
|
|
367
|
+
durationHours: Math.max(seg.end - seg.start, MIN_DURATION_HOURS),
|
|
368
|
+
column: columnOf.get(seg) ?? 0,
|
|
369
|
+
columns: columnEnds.length,
|
|
370
|
+
continuesBefore: seg.continuesBefore,
|
|
371
|
+
continuesAfter: seg.continuesAfter
|
|
372
|
+
});
|
|
373
|
+
cluster = [];
|
|
374
|
+
};
|
|
375
|
+
for (const seg of segments) {
|
|
376
|
+
if (cluster.length > 0 && seg.start >= clusterEnd) flushCluster();
|
|
377
|
+
cluster.push(seg);
|
|
378
|
+
clusterEnd = Math.max(clusterEnd, seg.end);
|
|
379
|
+
}
|
|
380
|
+
if (cluster.length > 0) flushCluster();
|
|
381
|
+
return positioned;
|
|
382
|
+
}
|
|
383
|
+
const atMidnight = (date) => date.getHours() === 0 && date.getMinutes() === 0 && date.getSeconds() === 0 && date.getMilliseconds() === 0;
|
|
384
|
+
/**
|
|
385
|
+
* Whether an event belongs in the all-day lane. An explicit `allDay` flag wins;
|
|
386
|
+
* otherwise it's inferred when the event spans whole days (both `start` and
|
|
387
|
+
* `end` land on midnight, e.g. an iCal-style all-day event). Pure.
|
|
388
|
+
*/
|
|
389
|
+
function isAllDayEvent(event) {
|
|
390
|
+
if (typeof event.allDay === "boolean") return event.allDay;
|
|
391
|
+
return event.end > event.start && atMidnight(event.start) && atMidnight(event.end);
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* The `startOfDay` ISO keys of every calendar day an event touches (inclusive).
|
|
395
|
+
* An event ending exactly at midnight does not count the following day. Used to
|
|
396
|
+
* index events by day for the month grid. Pure.
|
|
397
|
+
*/
|
|
398
|
+
function eventDayKeys(event) {
|
|
399
|
+
const first = startOfDay(event.start);
|
|
400
|
+
const lastInstant = event.end > event.start ? /* @__PURE__ */ new Date(event.end.getTime() - 1) : event.start;
|
|
401
|
+
const last = startOfDay(lastInstant);
|
|
402
|
+
const keys = [];
|
|
403
|
+
for (let cursor = first; cursor <= last; cursor = addDays(cursor, 1)) keys.push(cursor.toISOString());
|
|
404
|
+
return keys;
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Index events by the `startOfDay` ISO key of every day they touch (via
|
|
408
|
+
* {@link eventDayKeys}), so a month grid can look up a day's events with
|
|
409
|
+
* `startOfDay(date).toISOString()`. Built once and shared across month cells.
|
|
410
|
+
*/
|
|
411
|
+
function groupEventsByDay(events) {
|
|
412
|
+
const map = /* @__PURE__ */ new Map();
|
|
413
|
+
for (const event of events) for (const key of eventDayKeys(event)) {
|
|
414
|
+
const list = map.get(key);
|
|
415
|
+
if (list) list.push(event);
|
|
416
|
+
else map.set(key, [event]);
|
|
417
|
+
}
|
|
418
|
+
return map;
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Order a day's events for the month and list views: all-day events come first
|
|
422
|
+
* (they head the day regardless of their start time), then timed events by start.
|
|
423
|
+
* Shared by both renderers so the order is identical. Use as an `Array.sort`
|
|
424
|
+
* comparator.
|
|
425
|
+
*/
|
|
426
|
+
function compareDayEvents(a, b) {
|
|
427
|
+
const aAllDay = isAllDayEvent(a);
|
|
428
|
+
if (aAllDay !== isAllDayEvent(b)) return aAllDay ? -1 : 1;
|
|
429
|
+
return a.start.getTime() - b.start.getTime();
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* The closed hour-spans of a day to shade on the time grid, given a
|
|
433
|
+
* `businessHours` callback and the visible `[minHour, maxHour]` window: the spans
|
|
434
|
+
* before open and after close (clamped to the window), the whole window when the
|
|
435
|
+
* day is closed (`null`) or the open hours are inverted/empty, or none when the
|
|
436
|
+
* callback returns `undefined`. Shared by both renderers so shading stays
|
|
437
|
+
* identical. Co-located with `groupEventsByDay`; both feed the grid layout.
|
|
438
|
+
*/
|
|
439
|
+
function closedHourBands(day, businessHours, minHour = 0, maxHour = 24) {
|
|
440
|
+
const open = businessHours?.(day);
|
|
441
|
+
if (open === void 0) return [];
|
|
442
|
+
if (open === null) return [{
|
|
443
|
+
start: minHour,
|
|
444
|
+
end: maxHour
|
|
445
|
+
}];
|
|
446
|
+
const windows = (Array.isArray(open) ? open : [open]).map((w) => ({
|
|
447
|
+
start: Math.max(minHour, Math.min(maxHour, w.start)),
|
|
448
|
+
end: Math.max(minHour, Math.min(maxHour, w.end))
|
|
449
|
+
})).filter((w) => w.end > w.start).sort((a, b) => a.start - b.start);
|
|
450
|
+
if (windows.length === 0) return [{
|
|
451
|
+
start: minHour,
|
|
452
|
+
end: maxHour
|
|
453
|
+
}];
|
|
454
|
+
const merged = [];
|
|
455
|
+
for (const w of windows) {
|
|
456
|
+
const last = merged[merged.length - 1];
|
|
457
|
+
if (last && w.start <= last.end) last.end = Math.max(last.end, w.end);
|
|
458
|
+
else merged.push({ ...w });
|
|
459
|
+
}
|
|
460
|
+
const bands = [];
|
|
461
|
+
let cursor = minHour;
|
|
462
|
+
for (const w of merged) {
|
|
463
|
+
if (w.start > cursor) bands.push({
|
|
464
|
+
start: cursor,
|
|
465
|
+
end: w.start
|
|
466
|
+
});
|
|
467
|
+
cursor = w.end;
|
|
468
|
+
}
|
|
469
|
+
if (cursor < maxHour) bands.push({
|
|
470
|
+
start: cursor,
|
|
471
|
+
end: maxHour
|
|
472
|
+
});
|
|
473
|
+
return bands;
|
|
474
|
+
}
|
|
475
|
+
/** True when the event paints as a shaded background band, not an event box. */
|
|
476
|
+
function isBackgroundEvent(event) {
|
|
477
|
+
return event.display === "background";
|
|
478
|
+
}
|
|
479
|
+
/**
|
|
480
|
+
* The background events of `events` sliced to `day`, as fractional-hour bands
|
|
481
|
+
* (an all-day or multi-day background covers the day's full window). Shared by
|
|
482
|
+
* both renderers so the shading can't disagree.
|
|
483
|
+
*/
|
|
484
|
+
function backgroundBandsForDay(events, day) {
|
|
485
|
+
const dayStart = startOfDay(day);
|
|
486
|
+
const nextDayStart = addDays(dayStart, 1);
|
|
487
|
+
const out = [];
|
|
488
|
+
for (const event of events) {
|
|
489
|
+
if (!isBackgroundEvent(event)) continue;
|
|
490
|
+
if (event.start >= nextDayStart || event.end <= dayStart) continue;
|
|
491
|
+
const from = event.start > dayStart ? event.start : dayStart;
|
|
492
|
+
const to = event.end < nextDayStart ? event.end : nextDayStart;
|
|
493
|
+
const { start: startHours, end: endHours } = clockSpan(from, to, dayStart, nextDayStart);
|
|
494
|
+
if (endHours > startHours) out.push({
|
|
495
|
+
event,
|
|
496
|
+
startHours,
|
|
497
|
+
endHours
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
return out;
|
|
501
|
+
}
|
|
502
|
+
//#endregion
|
|
279
503
|
//#region src/utils/drag.ts
|
|
280
504
|
/**
|
|
281
505
|
* True when two time ranges overlap. Edges touching (one ends exactly when the
|
|
@@ -328,12 +552,50 @@ function clampMoveStartMinutes(startMinutes, minHour, maxHour, snapMinutes) {
|
|
|
328
552
|
const upper = Math.max(lower, maxHour * 60 - snapMinutes);
|
|
329
553
|
return Math.min(Math.max(startMinutes, lower), upper);
|
|
330
554
|
}
|
|
331
|
-
/**
|
|
555
|
+
/**
|
|
556
|
+
* A copy of `date` shifted by `minutes` (may be negative) on the clock, so a
|
|
557
|
+
* shift across a DST change keeps the clock time. When the resulting clock time
|
|
558
|
+
* occurs twice (the hour a fall-back change repeats), the occurrence nearer to
|
|
559
|
+
* `minutes` of elapsed time wins.
|
|
560
|
+
*/
|
|
332
561
|
function shiftMinutes(date, minutes) {
|
|
333
562
|
"worklet";
|
|
334
563
|
const next = new Date(date);
|
|
335
564
|
next.setMinutes(next.getMinutes() + minutes);
|
|
336
|
-
return next;
|
|
565
|
+
return nearestOccurrence(next, date.getTime() + minutes * 6e4);
|
|
566
|
+
}
|
|
567
|
+
function nearestOccurrence(date, target) {
|
|
568
|
+
"worklet";
|
|
569
|
+
const offset = date.getTimezoneOffset();
|
|
570
|
+
const later = new Date(date.getTime() + 108e5).getTimezoneOffset() - offset;
|
|
571
|
+
const earlier = offset - (/* @__PURE__ */ new Date(date.getTime() - 108e5)).getTimezoneOffset();
|
|
572
|
+
let best = date;
|
|
573
|
+
for (const change of [later, -earlier]) {
|
|
574
|
+
if (change === 0) continue;
|
|
575
|
+
const other = new Date(date.getTime() + change * 6e4);
|
|
576
|
+
if (other.getHours() !== date.getHours() || other.getMinutes() !== date.getMinutes()) continue;
|
|
577
|
+
if (Math.abs(other.getTime() - target) < Math.abs(best.getTime() - target)) best = other;
|
|
578
|
+
}
|
|
579
|
+
return best;
|
|
580
|
+
}
|
|
581
|
+
const localTime = (date) => date.getTime() - date.getTimezoneOffset() * 6e4;
|
|
582
|
+
/**
|
|
583
|
+
* Where moving `[start, end]` by `minutes` on the clock lands, in clock hours:
|
|
584
|
+
* how far the start moves, and how many rows the moved span covers. The span
|
|
585
|
+
* keeps its elapsed duration, as {@link resolveDraggedBounds} commits a move, so
|
|
586
|
+
* gaining or losing a DST change changes its row count, and a start that lands
|
|
587
|
+
* in the hour a spring-forward change skips moves on past it. The row count is
|
|
588
|
+
* at least the layout's minimum box, as the committed event will be drawn.
|
|
589
|
+
* Worklet-safe, for live drag previews.
|
|
590
|
+
*/
|
|
591
|
+
function movedClockSpan(start, end, minutes) {
|
|
592
|
+
"worklet";
|
|
593
|
+
const nextStart = shiftMinutes(start, minutes);
|
|
594
|
+
const nextEnd = new Date(nextStart.getTime() + end.getTime() - start.getTime());
|
|
595
|
+
return {
|
|
596
|
+
startHours: (nextStart.getTime() - start.getTime()) / 36e5 + (start.getTimezoneOffset() - nextStart.getTimezoneOffset()) / 60,
|
|
597
|
+
hours: Math.max(clockHoursBetween(nextStart, nextEnd), MIN_DURATION_HOURS)
|
|
598
|
+
};
|
|
337
599
|
}
|
|
338
600
|
/**
|
|
339
601
|
* Resolve a committed drag into the event's new bounds: `start` shifts by
|
|
@@ -348,7 +610,13 @@ function shiftMinutes(date, minutes) {
|
|
|
348
610
|
function resolveDraggedBounds(start, end, deltaStartMinutes, deltaEndMinutes, snapMinutes) {
|
|
349
611
|
const nextStart = shiftMinutes(start, deltaStartMinutes);
|
|
350
612
|
const oldDuration = end.getTime() - start.getTime();
|
|
351
|
-
|
|
613
|
+
let nextEnd = end;
|
|
614
|
+
if (deltaStartMinutes === deltaEndMinutes) nextEnd = new Date(nextStart.getTime() + oldDuration);
|
|
615
|
+
else if (deltaEndMinutes !== 0) {
|
|
616
|
+
const target = shiftMinutes(drawnEnd(start, end), deltaEndMinutes);
|
|
617
|
+
nextEnd = nearestOccurrence(target, end.getTime() + deltaEndMinutes * 6e4);
|
|
618
|
+
if (localTime(drawnEnd(nextStart, nextEnd)) !== localTime(target)) return null;
|
|
619
|
+
}
|
|
352
620
|
const newDuration = nextEnd.getTime() - nextStart.getTime();
|
|
353
621
|
if (newDuration < snapMinutes * 6e4 && newDuration < oldDuration) return null;
|
|
354
622
|
return {
|
|
@@ -1064,183 +1332,6 @@ function useEventSource(source, { format, map, refetchIntervalMs } = {}) {
|
|
|
1064
1332
|
};
|
|
1065
1333
|
}
|
|
1066
1334
|
//#endregion
|
|
1067
|
-
//#region src/utils/layout.ts
|
|
1068
|
-
const MINUTES_PER_HOUR = 60;
|
|
1069
|
-
const MIN_DURATION_HOURS = .25;
|
|
1070
|
-
/**
|
|
1071
|
-
* Lay out a single day's events: events that overlap in time are split into
|
|
1072
|
-
* side-by-side columns. Multi-day events are clipped to the portion that falls
|
|
1073
|
-
* on `day` (e.g. a 23:00→01:00 event renders 23:00–24:00 on the start day and
|
|
1074
|
-
* 00:00–01:00 on the next). Pure — safe to call per render, never per frame.
|
|
1075
|
-
*/
|
|
1076
|
-
function layoutDayEvents(events, day) {
|
|
1077
|
-
const dayStart = startOfDay(day);
|
|
1078
|
-
const nextDayStart = addDays(dayStart, 1);
|
|
1079
|
-
const segments = events.filter((event) => !isAllDayEvent(event) && !isBackgroundEvent(event)).filter((event) => event.start < nextDayStart && event.end > dayStart).map((event) => {
|
|
1080
|
-
const segStart = max([event.start, dayStart]);
|
|
1081
|
-
const segEnd = min([event.end, nextDayStart]);
|
|
1082
|
-
return {
|
|
1083
|
-
event,
|
|
1084
|
-
start: differenceInMinutes(segStart, dayStart) / MINUTES_PER_HOUR,
|
|
1085
|
-
end: differenceInMinutes(segEnd, dayStart) / MINUTES_PER_HOUR,
|
|
1086
|
-
continuesBefore: event.start < dayStart,
|
|
1087
|
-
continuesAfter: event.end > nextDayStart
|
|
1088
|
-
};
|
|
1089
|
-
}).sort((a, b) => a.start - b.start);
|
|
1090
|
-
const positioned = [];
|
|
1091
|
-
let cluster = [];
|
|
1092
|
-
let clusterEnd = Number.NEGATIVE_INFINITY;
|
|
1093
|
-
const flushCluster = () => {
|
|
1094
|
-
const columnEnds = [];
|
|
1095
|
-
const columnOf = /* @__PURE__ */ new Map();
|
|
1096
|
-
for (const seg of cluster) {
|
|
1097
|
-
let column = columnEnds.findIndex((end) => end <= seg.start);
|
|
1098
|
-
if (column === -1) {
|
|
1099
|
-
column = columnEnds.length;
|
|
1100
|
-
columnEnds.push(seg.end);
|
|
1101
|
-
} else columnEnds[column] = seg.end;
|
|
1102
|
-
columnOf.set(seg, column);
|
|
1103
|
-
}
|
|
1104
|
-
for (const seg of cluster) positioned.push({
|
|
1105
|
-
event: seg.event,
|
|
1106
|
-
startHours: seg.start,
|
|
1107
|
-
durationHours: Math.max(seg.end - seg.start, MIN_DURATION_HOURS),
|
|
1108
|
-
column: columnOf.get(seg) ?? 0,
|
|
1109
|
-
columns: columnEnds.length,
|
|
1110
|
-
continuesBefore: seg.continuesBefore,
|
|
1111
|
-
continuesAfter: seg.continuesAfter
|
|
1112
|
-
});
|
|
1113
|
-
cluster = [];
|
|
1114
|
-
};
|
|
1115
|
-
for (const seg of segments) {
|
|
1116
|
-
if (cluster.length > 0 && seg.start >= clusterEnd) flushCluster();
|
|
1117
|
-
cluster.push(seg);
|
|
1118
|
-
clusterEnd = Math.max(clusterEnd, seg.end);
|
|
1119
|
-
}
|
|
1120
|
-
if (cluster.length > 0) flushCluster();
|
|
1121
|
-
return positioned;
|
|
1122
|
-
}
|
|
1123
|
-
const atMidnight = (date) => date.getHours() === 0 && date.getMinutes() === 0 && date.getSeconds() === 0 && date.getMilliseconds() === 0;
|
|
1124
|
-
/**
|
|
1125
|
-
* Whether an event belongs in the all-day lane. An explicit `allDay` flag wins;
|
|
1126
|
-
* otherwise it's inferred when the event spans whole days (both `start` and
|
|
1127
|
-
* `end` land on midnight, e.g. an iCal-style all-day event). Pure.
|
|
1128
|
-
*/
|
|
1129
|
-
function isAllDayEvent(event) {
|
|
1130
|
-
if (typeof event.allDay === "boolean") return event.allDay;
|
|
1131
|
-
return event.end > event.start && atMidnight(event.start) && atMidnight(event.end);
|
|
1132
|
-
}
|
|
1133
|
-
/**
|
|
1134
|
-
* The `startOfDay` ISO keys of every calendar day an event touches (inclusive).
|
|
1135
|
-
* An event ending exactly at midnight does not count the following day. Used to
|
|
1136
|
-
* index events by day for the month grid. Pure.
|
|
1137
|
-
*/
|
|
1138
|
-
function eventDayKeys(event) {
|
|
1139
|
-
const first = startOfDay(event.start);
|
|
1140
|
-
const lastInstant = event.end > event.start ? /* @__PURE__ */ new Date(event.end.getTime() - 1) : event.start;
|
|
1141
|
-
const last = startOfDay(lastInstant);
|
|
1142
|
-
const keys = [];
|
|
1143
|
-
for (let cursor = first; cursor <= last; cursor = addDays(cursor, 1)) keys.push(cursor.toISOString());
|
|
1144
|
-
return keys;
|
|
1145
|
-
}
|
|
1146
|
-
/**
|
|
1147
|
-
* Index events by the `startOfDay` ISO key of every day they touch (via
|
|
1148
|
-
* {@link eventDayKeys}), so a month grid can look up a day's events with
|
|
1149
|
-
* `startOfDay(date).toISOString()`. Built once and shared across month cells.
|
|
1150
|
-
*/
|
|
1151
|
-
function groupEventsByDay(events) {
|
|
1152
|
-
const map = /* @__PURE__ */ new Map();
|
|
1153
|
-
for (const event of events) for (const key of eventDayKeys(event)) {
|
|
1154
|
-
const list = map.get(key);
|
|
1155
|
-
if (list) list.push(event);
|
|
1156
|
-
else map.set(key, [event]);
|
|
1157
|
-
}
|
|
1158
|
-
return map;
|
|
1159
|
-
}
|
|
1160
|
-
/**
|
|
1161
|
-
* Order a day's events for the month and list views: all-day events come first
|
|
1162
|
-
* (they head the day regardless of their start time), then timed events by start.
|
|
1163
|
-
* Shared by both renderers so the order is identical. Use as an `Array.sort`
|
|
1164
|
-
* comparator.
|
|
1165
|
-
*/
|
|
1166
|
-
function compareDayEvents(a, b) {
|
|
1167
|
-
const aAllDay = isAllDayEvent(a);
|
|
1168
|
-
if (aAllDay !== isAllDayEvent(b)) return aAllDay ? -1 : 1;
|
|
1169
|
-
return a.start.getTime() - b.start.getTime();
|
|
1170
|
-
}
|
|
1171
|
-
/**
|
|
1172
|
-
* The closed hour-spans of a day to shade on the time grid, given a
|
|
1173
|
-
* `businessHours` callback and the visible `[minHour, maxHour]` window: the spans
|
|
1174
|
-
* before open and after close (clamped to the window), the whole window when the
|
|
1175
|
-
* day is closed (`null`) or the open hours are inverted/empty, or none when the
|
|
1176
|
-
* callback returns `undefined`. Shared by both renderers so shading stays
|
|
1177
|
-
* identical. Co-located with `groupEventsByDay`; both feed the grid layout.
|
|
1178
|
-
*/
|
|
1179
|
-
function closedHourBands(day, businessHours, minHour = 0, maxHour = 24) {
|
|
1180
|
-
const open = businessHours?.(day);
|
|
1181
|
-
if (open === void 0) return [];
|
|
1182
|
-
if (open === null) return [{
|
|
1183
|
-
start: minHour,
|
|
1184
|
-
end: maxHour
|
|
1185
|
-
}];
|
|
1186
|
-
const windows = (Array.isArray(open) ? open : [open]).map((w) => ({
|
|
1187
|
-
start: Math.max(minHour, Math.min(maxHour, w.start)),
|
|
1188
|
-
end: Math.max(minHour, Math.min(maxHour, w.end))
|
|
1189
|
-
})).filter((w) => w.end > w.start).sort((a, b) => a.start - b.start);
|
|
1190
|
-
if (windows.length === 0) return [{
|
|
1191
|
-
start: minHour,
|
|
1192
|
-
end: maxHour
|
|
1193
|
-
}];
|
|
1194
|
-
const merged = [];
|
|
1195
|
-
for (const w of windows) {
|
|
1196
|
-
const last = merged[merged.length - 1];
|
|
1197
|
-
if (last && w.start <= last.end) last.end = Math.max(last.end, w.end);
|
|
1198
|
-
else merged.push({ ...w });
|
|
1199
|
-
}
|
|
1200
|
-
const bands = [];
|
|
1201
|
-
let cursor = minHour;
|
|
1202
|
-
for (const w of merged) {
|
|
1203
|
-
if (w.start > cursor) bands.push({
|
|
1204
|
-
start: cursor,
|
|
1205
|
-
end: w.start
|
|
1206
|
-
});
|
|
1207
|
-
cursor = w.end;
|
|
1208
|
-
}
|
|
1209
|
-
if (cursor < maxHour) bands.push({
|
|
1210
|
-
start: cursor,
|
|
1211
|
-
end: maxHour
|
|
1212
|
-
});
|
|
1213
|
-
return bands;
|
|
1214
|
-
}
|
|
1215
|
-
/** True when the event paints as a shaded background band, not an event box. */
|
|
1216
|
-
function isBackgroundEvent(event) {
|
|
1217
|
-
return event.display === "background";
|
|
1218
|
-
}
|
|
1219
|
-
/**
|
|
1220
|
-
* The background events of `events` sliced to `day`, as fractional-hour bands
|
|
1221
|
-
* (an all-day or multi-day background covers the day's full window). Shared by
|
|
1222
|
-
* both renderers so the shading can't disagree.
|
|
1223
|
-
*/
|
|
1224
|
-
function backgroundBandsForDay(events, day) {
|
|
1225
|
-
const dayStart = startOfDay(day);
|
|
1226
|
-
const nextDayStart = addDays(dayStart, 1);
|
|
1227
|
-
const out = [];
|
|
1228
|
-
for (const event of events) {
|
|
1229
|
-
if (!isBackgroundEvent(event)) continue;
|
|
1230
|
-
if (event.start >= nextDayStart || event.end <= dayStart) continue;
|
|
1231
|
-
const from = event.start > dayStart ? event.start : dayStart;
|
|
1232
|
-
const to = event.end < nextDayStart ? event.end : nextDayStart;
|
|
1233
|
-
const startHours = (from.getTime() - dayStart.getTime()) / 36e5;
|
|
1234
|
-
const endHours = (to.getTime() - dayStart.getTime()) / 36e5;
|
|
1235
|
-
if (endHours > startHours) out.push({
|
|
1236
|
-
event,
|
|
1237
|
-
startHours,
|
|
1238
|
-
endHours
|
|
1239
|
-
});
|
|
1240
|
-
}
|
|
1241
|
-
return out;
|
|
1242
|
-
}
|
|
1243
|
-
//#endregion
|
|
1244
1335
|
//#region src/utils/monthGrid.ts
|
|
1245
1336
|
/** The last calendar day an event visibly covers (a midnight end ends the day before). */
|
|
1246
1337
|
function lastCoveredDay(event) {
|
|
@@ -1571,4 +1662,4 @@ function expandRecurringEvents(events, rangeStart, rangeEnd) {
|
|
|
1571
1662
|
return out;
|
|
1572
1663
|
}
|
|
1573
1664
|
//#endregion
|
|
1574
|
-
export { CalendarSelectionProvider, MIN_BOX_HEIGHT_FOR_TIME, backgroundBandsForDay, bandRounding, buildMonthGrid, buildMonthWeeks, cellRangeFromDrag, clampMoveStartMinutes, closedHourBands, compareDayEvents, darkColors, dayAccessibilityLabel, dayBadgeKind, daySelectionState, defaultCalendarLabels, 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, resolveCalendarLabels, resolveDraggedBounds, shiftMinutes, snapDeltaMinutes, titleEllipsizeMode, titleNumberOfLines, toICalendar, toZonedTime, useCalendarSelection, useDateRange, useEventSource, useMonthGrid, useNow, viewDayCount, weekDaysCount, weekdayFormatToken, zonedTimeToUtc };
|
|
1665
|
+
export { CalendarSelectionProvider, MIN_BOX_HEIGHT_FOR_TIME, backgroundBandsForDay, bandRounding, buildMonthGrid, buildMonthWeeks, cellRangeFromDrag, clampMoveStartMinutes, clockHoursBetween, closedHourBands, compareDayEvents, darkColors, dayAccessibilityLabel, dayBadgeKind, daySelectionState, defaultCalendarLabels, drawnEnd, 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, movedClockSpan, nextDateRange, overlapsOtherEvents, pageStepDays, parseICalendar, rangeBandKind, resolveCalendarLabels, resolveDraggedBounds, shiftMinutes, snapDeltaMinutes, titleEllipsizeMode, titleNumberOfLines, toICalendar, toZonedTime, useCalendarSelection, useDateRange, useEventSource, useMonthGrid, useNow, viewDayCount, wallClockHours, 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.14.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,6 +1,8 @@
|
|
|
1
1
|
import { addDays, differenceInCalendarDays, startOfDay } from "date-fns";
|
|
2
2
|
import type { CalendarEvent, CalendarMode, WeekStartsOn } from "../types";
|
|
3
3
|
import { getViewDays } from "./dates";
|
|
4
|
+
import { clockHoursBetween, drawnEnd } from "./layout";
|
|
5
|
+
import { MIN_DURATION_HOURS } from "./minDuration";
|
|
4
6
|
|
|
5
7
|
/**
|
|
6
8
|
* True when two time ranges overlap. Edges touching (one ends exactly when the
|
|
@@ -77,12 +79,63 @@ export function clampMoveStartMinutes(
|
|
|
77
79
|
return Math.min(Math.max(startMinutes, lower), upper);
|
|
78
80
|
}
|
|
79
81
|
|
|
80
|
-
/**
|
|
82
|
+
/**
|
|
83
|
+
* A copy of `date` shifted by `minutes` (may be negative) on the clock, so a
|
|
84
|
+
* shift across a DST change keeps the clock time. When the resulting clock time
|
|
85
|
+
* occurs twice (the hour a fall-back change repeats), the occurrence nearer to
|
|
86
|
+
* `minutes` of elapsed time wins.
|
|
87
|
+
*/
|
|
81
88
|
export function shiftMinutes(date: Date, minutes: number): Date {
|
|
82
89
|
"worklet";
|
|
83
90
|
const next = new Date(date);
|
|
84
91
|
next.setMinutes(next.getMinutes() + minutes);
|
|
85
|
-
return next;
|
|
92
|
+
return nearestOccurrence(next, date.getTime() + minutes * 60_000);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// The instant reading the same clock time as `date` that is nearest `target`
|
|
96
|
+
// (ms). In the hour a fall-back change repeats a clock time names two instants,
|
|
97
|
+
// one offset change apart; elsewhere this is `date`.
|
|
98
|
+
function nearestOccurrence(date: Date, target: number): Date {
|
|
99
|
+
"worklet";
|
|
100
|
+
const offset = date.getTimezoneOffset();
|
|
101
|
+
const later = new Date(date.getTime() + 3 * 3_600_000).getTimezoneOffset() - offset;
|
|
102
|
+
const earlier = offset - new Date(date.getTime() - 3 * 3_600_000).getTimezoneOffset();
|
|
103
|
+
let best = date;
|
|
104
|
+
for (const change of [later, -earlier]) {
|
|
105
|
+
if (change === 0) continue;
|
|
106
|
+
const other = new Date(date.getTime() + change * 60_000);
|
|
107
|
+
if (other.getHours() !== date.getHours() || other.getMinutes() !== date.getMinutes()) continue;
|
|
108
|
+
if (Math.abs(other.getTime() - target) < Math.abs(best.getTime() - target)) best = other;
|
|
109
|
+
}
|
|
110
|
+
return best;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// `date`'s clock reading as milliseconds, equal for two instants that read the same.
|
|
114
|
+
const localTime = (date: Date): number => date.getTime() - date.getTimezoneOffset() * 60_000;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Where moving `[start, end]` by `minutes` on the clock lands, in clock hours:
|
|
118
|
+
* how far the start moves, and how many rows the moved span covers. The span
|
|
119
|
+
* keeps its elapsed duration, as {@link resolveDraggedBounds} commits a move, so
|
|
120
|
+
* gaining or losing a DST change changes its row count, and a start that lands
|
|
121
|
+
* in the hour a spring-forward change skips moves on past it. The row count is
|
|
122
|
+
* at least the layout's minimum box, as the committed event will be drawn.
|
|
123
|
+
* Worklet-safe, for live drag previews.
|
|
124
|
+
*/
|
|
125
|
+
export function movedClockSpan(
|
|
126
|
+
start: Date,
|
|
127
|
+
end: Date,
|
|
128
|
+
minutes: number,
|
|
129
|
+
): { startHours: number; hours: number } {
|
|
130
|
+
"worklet";
|
|
131
|
+
const nextStart = shiftMinutes(start, minutes);
|
|
132
|
+
const nextEnd = new Date(nextStart.getTime() + end.getTime() - start.getTime());
|
|
133
|
+
return {
|
|
134
|
+
startHours:
|
|
135
|
+
(nextStart.getTime() - start.getTime()) / 3_600_000 +
|
|
136
|
+
(start.getTimezoneOffset() - nextStart.getTimezoneOffset()) / 60,
|
|
137
|
+
hours: Math.max(clockHoursBetween(nextStart, nextEnd), MIN_DURATION_HOURS),
|
|
138
|
+
};
|
|
86
139
|
}
|
|
87
140
|
|
|
88
141
|
/**
|
|
@@ -104,10 +157,20 @@ export function resolveDraggedBounds(
|
|
|
104
157
|
): { start: Date; end: Date } | null {
|
|
105
158
|
const nextStart = shiftMinutes(start, deltaStartMinutes);
|
|
106
159
|
const oldDuration = end.getTime() - start.getTime();
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
160
|
+
let nextEnd = end;
|
|
161
|
+
if (deltaStartMinutes === deltaEndMinutes) {
|
|
162
|
+
nextEnd = new Date(nextStart.getTime() + oldDuration);
|
|
163
|
+
} else if (deltaEndMinutes !== 0) {
|
|
164
|
+
// A resize moves the end to the clock time its drawn edge is dragged to. That
|
|
165
|
+
// edge differs from `end` for a span whose clock runs backwards, and a clock
|
|
166
|
+
// time in the hour a fall-back change repeats resolves to the occurrence
|
|
167
|
+
// nearest `end` shifted by the same amount.
|
|
168
|
+
const target = shiftMinutes(drawnEnd(start, end), deltaEndMinutes);
|
|
169
|
+
nextEnd = nearestOccurrence(target, end.getTime() + deltaEndMinutes * 60_000);
|
|
170
|
+
// An end dragged above the start can still land after it in the repeated
|
|
171
|
+
// hour, where its edge would be drawn somewhere else; refuse it.
|
|
172
|
+
if (localTime(drawnEnd(nextStart, nextEnd)) !== localTime(target)) return null;
|
|
173
|
+
}
|
|
111
174
|
const newDuration = nextEnd.getTime() - nextStart.getTime();
|
|
112
175
|
// Reject only a shrink past one step — never a move, which preserves duration.
|
|
113
176
|
if (newDuration < snapMinutes * 60_000 && newDuration < oldDuration) return null;
|