@super-calendar/dom 2.14.2 → 2.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +48 -0
- package/dist/index.d.mts +47 -26
- package/dist/index.d.ts +47 -26
- package/dist/index.js +1264 -452
- package/dist/index.mjs +1265 -453
- package/package.json +2 -2
- package/src/Calendar.tsx +13 -0
- package/src/DateRangePicker.tsx +60 -6
- package/src/EventActions.tsx +470 -0
- package/src/InertDiv.ts +7 -0
- package/src/MonthList.tsx +64 -38
- package/src/MonthView.tsx +14 -2
- package/src/ResourceTimeline.tsx +438 -98
- package/src/TimeGrid.tsx +305 -35
- package/src/index.ts +1 -0
package/src/ResourceTimeline.tsx
CHANGED
|
@@ -5,12 +5,16 @@ import type {
|
|
|
5
5
|
ReactElement,
|
|
6
6
|
ReactNode,
|
|
7
7
|
} from "react";
|
|
8
|
-
import { useMemo, useRef, useState } from "react";
|
|
8
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
9
9
|
import {
|
|
10
10
|
backgroundBandsForDay,
|
|
11
11
|
type BusinessHoursBand,
|
|
12
12
|
type BusinessHoursValue,
|
|
13
13
|
type CalendarEvent,
|
|
14
|
+
type CalendarLabels,
|
|
15
|
+
type CreateGhostArgs,
|
|
16
|
+
eventAccessibilityLabel,
|
|
17
|
+
resolveCalendarLabels,
|
|
14
18
|
isSameCalendarDay,
|
|
15
19
|
useNow,
|
|
16
20
|
cellRangeFromDrag,
|
|
@@ -18,11 +22,22 @@ import {
|
|
|
18
22
|
eventTimeLabel,
|
|
19
23
|
formatHour,
|
|
20
24
|
layoutDayEvents,
|
|
25
|
+
isAllDayEvent,
|
|
26
|
+
isBackgroundEvent,
|
|
21
27
|
movedClockSpan,
|
|
22
28
|
type PositionedEvent,
|
|
23
29
|
resolveDraggedBounds,
|
|
24
30
|
} from "@super-calendar/core";
|
|
31
|
+
import {
|
|
32
|
+
EventActionsDialog,
|
|
33
|
+
describeEventDates,
|
|
34
|
+
type EventActionsSlot,
|
|
35
|
+
EventActionsMenu,
|
|
36
|
+
type EventEditAction,
|
|
37
|
+
useEventActions,
|
|
38
|
+
} from "./EventActions";
|
|
25
39
|
import { createSlots, dataState, type ResolvedSlot, type SlotStyleProps } from "./slots";
|
|
40
|
+
import { InertDiv } from "./InertDiv";
|
|
26
41
|
import { type DomCalendarTheme, mergeDomTheme } from "./theme";
|
|
27
42
|
|
|
28
43
|
/**
|
|
@@ -43,7 +58,8 @@ export type ResourceTimelineSlot =
|
|
|
43
58
|
| "event"
|
|
44
59
|
| "eventBox"
|
|
45
60
|
| "nowIndicator"
|
|
46
|
-
| "createGhost"
|
|
61
|
+
| "createGhost"
|
|
62
|
+
| EventActionsSlot;
|
|
47
63
|
|
|
48
64
|
/** A schedulable lane (room, person, machine) that events are grouped under. */
|
|
49
65
|
export interface Resource {
|
|
@@ -126,10 +142,23 @@ export interface ResourceTimelineProps<T = unknown> extends SlotStyleProps<Resou
|
|
|
126
142
|
end: Date,
|
|
127
143
|
resource: Resource,
|
|
128
144
|
) => void | boolean;
|
|
145
|
+
/** Stable, unique event identity for the web editing dialog across data refreshes. */
|
|
146
|
+
getEventId?: (event: CalendarEvent<T>) => string | number;
|
|
129
147
|
/** Tap empty lane space; called with the snapped time and the lane's resource. */
|
|
130
148
|
onPressCell?: (at: Date, resource: Resource) => void;
|
|
131
149
|
/** Drag empty lane space to create; called with the swept start/end and the lane's resource. */
|
|
132
150
|
onCreateEvent?: (start: Date, end: Date, resource: Resource) => void;
|
|
151
|
+
/**
|
|
152
|
+
* A component that draws content inside the drag-to-create preview, such as a
|
|
153
|
+
* draft title and the live time range. It receives the snapped `start` and
|
|
154
|
+
* `end` that `onCreateEvent` would get if the pointer were released now, plus
|
|
155
|
+
* the lane's resource, and re-renders as the sweep changes them. The board
|
|
156
|
+
* keeps positioning, sizing and styling the preview and clips the content to
|
|
157
|
+
* it; the content is non-interactive and hidden from assistive tech. Renders
|
|
158
|
+
* only while a sweep can create, so it needs `onCreateEvent` and does not
|
|
159
|
+
* enable creation itself. Applies to both orientations.
|
|
160
|
+
*/
|
|
161
|
+
renderCreateGhost?: ComponentType<CreateGhostArgs & { resource: Resource }>;
|
|
133
162
|
/**
|
|
134
163
|
* Shade the hours outside this window, per lane. Same shape as the Calendar's
|
|
135
164
|
* `businessHours` plus the lane's resource, so per-resource opening hours work
|
|
@@ -153,6 +182,11 @@ export interface ResourceTimelineProps<T = unknown> extends SlotStyleProps<Resou
|
|
|
153
182
|
renderResourceHeader?: (resource: Resource, index: number) => ReactNode;
|
|
154
183
|
/** Snap dragged events to this many minutes (default 15). */
|
|
155
184
|
dragStepMinutes?: number;
|
|
185
|
+
/**
|
|
186
|
+
* Words for the move and resize actions and the event labels. Keys you omit
|
|
187
|
+
* stay in English (`defaultCalendarLabels`).
|
|
188
|
+
*/
|
|
189
|
+
labels?: Partial<CalendarLabels>;
|
|
156
190
|
/** Show the current-time line when `date` is today (default true). */
|
|
157
191
|
showNowIndicator?: boolean;
|
|
158
192
|
/** Fixed "now" instant for the indicator (doesn't tick). Defaults to the device clock. */
|
|
@@ -170,10 +204,10 @@ const clamp = (v: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, v
|
|
|
170
204
|
// Hour-gutter width in the vertical orientation, matching the time grid's axis.
|
|
171
205
|
const GUTTER_WIDTH = 56;
|
|
172
206
|
|
|
173
|
-
type DragState = {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
207
|
+
type DragState<T> = {
|
|
208
|
+
pointerId: number;
|
|
209
|
+
event: CalendarEvent<T>;
|
|
210
|
+
resource: Resource;
|
|
177
211
|
kind: "move" | "resize";
|
|
178
212
|
startHours: number;
|
|
179
213
|
durationHours: number;
|
|
@@ -267,12 +301,15 @@ export function ResourceTimeline<T = unknown>({
|
|
|
267
301
|
renderEvent,
|
|
268
302
|
onPressEvent,
|
|
269
303
|
onDragEvent,
|
|
304
|
+
getEventId,
|
|
270
305
|
onPressCell,
|
|
271
306
|
onCreateEvent,
|
|
307
|
+
renderCreateGhost,
|
|
272
308
|
businessHours,
|
|
273
309
|
renderBusinessHours,
|
|
274
310
|
renderResourceHeader,
|
|
275
311
|
dragStepMinutes = 15,
|
|
312
|
+
labels: labelsProp,
|
|
276
313
|
showNowIndicator = true,
|
|
277
314
|
now: nowProp,
|
|
278
315
|
timeZone,
|
|
@@ -289,6 +326,7 @@ export function ResourceTimeline<T = unknown>({
|
|
|
289
326
|
return allResources.slice(page * resourcesPerPage, (page + 1) * resourcesPerPage);
|
|
290
327
|
}, [allResources, resourcesPerPage, resourcePage]);
|
|
291
328
|
const theme = useMemo(() => mergeDomTheme(themeOverrides), [themeOverrides]);
|
|
329
|
+
const labels = useMemo(() => resolveCalendarLabels(labelsProp), [labelsProp]);
|
|
292
330
|
const slot = createSlots<ResourceTimelineSlot>({ classNames, styles });
|
|
293
331
|
const Renderer = renderEvent;
|
|
294
332
|
const snapHours = dragStepMinutes / 60;
|
|
@@ -335,9 +373,11 @@ export function ResourceTimeline<T = unknown>({
|
|
|
335
373
|
|
|
336
374
|
// `drag` drives the visual; `dragRef` is the source of truth the pointer
|
|
337
375
|
// handlers read so they never see a stale closure between events.
|
|
338
|
-
const [drag, setDrag] = useState<DragState | null>(null);
|
|
339
|
-
const dragRef = useRef<DragState | null>(null);
|
|
376
|
+
const [drag, setDrag] = useState<DragState<T> | null>(null);
|
|
377
|
+
const dragRef = useRef<DragState<T> | null>(null);
|
|
378
|
+
const handledClick = useRef<HTMLButtonElement | null>(null);
|
|
340
379
|
const origin = useRef<{
|
|
380
|
+
element: HTMLButtonElement;
|
|
341
381
|
x: number;
|
|
342
382
|
cross: number;
|
|
343
383
|
startHours: number;
|
|
@@ -346,27 +386,49 @@ export function ResourceTimeline<T = unknown>({
|
|
|
346
386
|
// for a bar clipped to the day.
|
|
347
387
|
span: { start: Date; end: Date } | null;
|
|
348
388
|
} | null>(null);
|
|
349
|
-
const applyDrag = (next: DragState | null) => {
|
|
389
|
+
const applyDrag = (next: DragState<T> | null) => {
|
|
350
390
|
dragRef.current = next;
|
|
351
391
|
setDrag(next);
|
|
352
392
|
};
|
|
393
|
+
const clearDrag = () => {
|
|
394
|
+
const previous = dragRef.current;
|
|
395
|
+
const element = origin.current?.element;
|
|
396
|
+
applyDrag(null);
|
|
397
|
+
origin.current = null;
|
|
398
|
+
try {
|
|
399
|
+
if (previous) element?.releasePointerCapture?.(previous.pointerId);
|
|
400
|
+
} catch {
|
|
401
|
+
// The browser may have already released capture.
|
|
402
|
+
}
|
|
403
|
+
};
|
|
353
404
|
|
|
354
405
|
const beginDrag = (
|
|
355
406
|
e: ReactPointerEvent,
|
|
356
|
-
|
|
357
|
-
resourceId: string,
|
|
407
|
+
resource: Resource,
|
|
358
408
|
kind: "move" | "resize",
|
|
359
409
|
pe: PositionedEvent<T>,
|
|
360
410
|
) => {
|
|
361
411
|
const { startHours, durationHours } = pe;
|
|
362
|
-
if (!
|
|
412
|
+
if (e.button === 0 && e.isPrimary !== false && !dragRef.current) handledClick.current = null;
|
|
413
|
+
if (
|
|
414
|
+
!onDragEvent ||
|
|
415
|
+
pe.event.draggable === false ||
|
|
416
|
+
dragRef.current ||
|
|
417
|
+
createOrigin.current ||
|
|
418
|
+
e.isPrimary === false ||
|
|
419
|
+
e.button !== 0
|
|
420
|
+
)
|
|
421
|
+
return;
|
|
422
|
+
const element = e.currentTarget.closest("button");
|
|
423
|
+
if (!element) return;
|
|
363
424
|
e.stopPropagation();
|
|
364
425
|
try {
|
|
365
|
-
|
|
426
|
+
element.setPointerCapture?.(e.pointerId);
|
|
366
427
|
} catch {
|
|
367
428
|
// Pointer capture is best-effort.
|
|
368
429
|
}
|
|
369
430
|
origin.current = {
|
|
431
|
+
element,
|
|
370
432
|
x: vertical ? e.clientY : e.clientX,
|
|
371
433
|
cross: vertical ? e.clientX : e.clientY,
|
|
372
434
|
startHours,
|
|
@@ -376,11 +438,20 @@ export function ResourceTimeline<T = unknown>({
|
|
|
376
438
|
? null
|
|
377
439
|
: { start: pe.event.start, end: pe.event.end },
|
|
378
440
|
};
|
|
379
|
-
applyDrag({
|
|
441
|
+
applyDrag({
|
|
442
|
+
pointerId: e.pointerId,
|
|
443
|
+
event: pe.event,
|
|
444
|
+
resource,
|
|
445
|
+
kind,
|
|
446
|
+
startHours,
|
|
447
|
+
durationHours,
|
|
448
|
+
crossPx: 0,
|
|
449
|
+
moved: false,
|
|
450
|
+
});
|
|
380
451
|
};
|
|
381
452
|
const moveDrag = (e: ReactPointerEvent) => {
|
|
382
453
|
const d = dragRef.current;
|
|
383
|
-
if (!d || !origin.current) return;
|
|
454
|
+
if (!d || d.pointerId !== e.pointerId || !origin.current) return;
|
|
384
455
|
const dHours = ((vertical ? e.clientY : e.clientX) - origin.current.x) / hourSize;
|
|
385
456
|
const snap = (v: number) => Math.round(v / snapHours) * snapHours;
|
|
386
457
|
if (d.kind === "move") {
|
|
@@ -434,22 +505,18 @@ export function ResourceTimeline<T = unknown>({
|
|
|
434
505
|
const id = laneEl?.getAttribute("data-resource-id");
|
|
435
506
|
return (id && resources.find((r) => r.id === id)) || fallback;
|
|
436
507
|
};
|
|
437
|
-
const endDrag = (
|
|
438
|
-
e: ReactPointerEvent,
|
|
439
|
-
event: CalendarEvent<T>,
|
|
440
|
-
resource: Resource,
|
|
441
|
-
onPress: () => void,
|
|
442
|
-
) => {
|
|
508
|
+
const endDrag = (e: ReactPointerEvent) => {
|
|
443
509
|
const d = dragRef.current;
|
|
444
|
-
if (!d) return;
|
|
445
|
-
|
|
446
|
-
(e.target as HTMLElement).releasePointerCapture?.(e.pointerId);
|
|
447
|
-
} catch {
|
|
448
|
-
// Best-effort release.
|
|
449
|
-
}
|
|
510
|
+
if (!d || d.pointerId !== e.pointerId) return;
|
|
511
|
+
handledClick.current = origin.current?.element ?? null;
|
|
450
512
|
if (!d.moved) {
|
|
451
|
-
|
|
452
|
-
|
|
513
|
+
clearDrag();
|
|
514
|
+
onPressEvent?.(d.event);
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
const resource = resources.find((resource) => resource.id === d.resource.id);
|
|
518
|
+
if (!resource) {
|
|
519
|
+
clearDrag();
|
|
453
520
|
return;
|
|
454
521
|
}
|
|
455
522
|
const target = d.kind === "move" ? laneAt(e, resource) : resource;
|
|
@@ -462,19 +529,19 @@ export function ResourceTimeline<T = unknown>({
|
|
|
462
529
|
const endDelta =
|
|
463
530
|
d.kind === "move" ? startDelta : o ? Math.round((d.durationHours - o.durationHours) * 60) : 0;
|
|
464
531
|
const next = resolveDraggedBounds(
|
|
465
|
-
event.start,
|
|
466
|
-
event.end,
|
|
532
|
+
d.event.start,
|
|
533
|
+
d.event.end,
|
|
467
534
|
startDelta,
|
|
468
535
|
endDelta,
|
|
469
536
|
dragStepMinutes,
|
|
470
537
|
);
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
origin.current = null;
|
|
538
|
+
clearDrag();
|
|
539
|
+
if (next) onDragEvent?.(d.event, next.start, next.end, target);
|
|
474
540
|
};
|
|
475
|
-
const cancelDrag = () => {
|
|
476
|
-
|
|
477
|
-
|
|
541
|
+
const cancelDrag = (e: ReactPointerEvent) => {
|
|
542
|
+
if (dragRef.current?.pointerId !== e.pointerId) return;
|
|
543
|
+
handledClick.current = origin.current?.element ?? null;
|
|
544
|
+
clearDrag();
|
|
478
545
|
};
|
|
479
546
|
|
|
480
547
|
// Create-by-drag / tap on empty lane space, mirroring the time grid: mouse and
|
|
@@ -485,9 +552,51 @@ export function ResourceTimeline<T = unknown>({
|
|
|
485
552
|
startPx: number;
|
|
486
553
|
curPx: number;
|
|
487
554
|
} | null>(null);
|
|
488
|
-
const createOrigin = useRef<{
|
|
489
|
-
|
|
490
|
-
|
|
555
|
+
const createOrigin = useRef<{
|
|
556
|
+
pointerId: number;
|
|
557
|
+
clientX: number;
|
|
558
|
+
clientY: number;
|
|
559
|
+
el: HTMLElement;
|
|
560
|
+
resource: Resource;
|
|
561
|
+
startPx: number;
|
|
562
|
+
dayKey: string;
|
|
563
|
+
creating: boolean;
|
|
564
|
+
} | null>(null);
|
|
565
|
+
const createActive = createBox !== null;
|
|
566
|
+
useEffect(() => {
|
|
567
|
+
if (!createActive) return;
|
|
568
|
+
const onScroll = () => {
|
|
569
|
+
const origin = createOrigin.current;
|
|
570
|
+
if (!origin) return;
|
|
571
|
+
const rect = origin.el.getBoundingClientRect();
|
|
572
|
+
const curPx = vertical ? origin.clientY - rect.top : origin.clientX - rect.left;
|
|
573
|
+
setCreateBox((current) =>
|
|
574
|
+
current && current.curPx !== curPx ? { ...current, curPx } : current,
|
|
575
|
+
);
|
|
576
|
+
};
|
|
577
|
+
window.addEventListener("scroll", onScroll, true);
|
|
578
|
+
return () => window.removeEventListener("scroll", onScroll, true);
|
|
579
|
+
}, [createActive, vertical]);
|
|
580
|
+
const clearCreate = useCallback(() => {
|
|
581
|
+
const previous = createOrigin.current;
|
|
582
|
+
createOrigin.current = null;
|
|
583
|
+
setCreateBox(null);
|
|
584
|
+
try {
|
|
585
|
+
previous?.el.releasePointerCapture?.(previous.pointerId);
|
|
586
|
+
} catch {
|
|
587
|
+
// The browser may have already released capture.
|
|
588
|
+
}
|
|
589
|
+
}, []);
|
|
590
|
+
useEffect(() => {
|
|
591
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
592
|
+
if (event.key !== "Escape" || !createOrigin.current) return;
|
|
593
|
+
event.preventDefault();
|
|
594
|
+
event.stopPropagation();
|
|
595
|
+
clearCreate();
|
|
596
|
+
};
|
|
597
|
+
document.addEventListener("keydown", onKeyDown);
|
|
598
|
+
return () => document.removeEventListener("keydown", onKeyDown);
|
|
599
|
+
}, [clearCreate]);
|
|
491
600
|
const cellEnabled = !!onPressCell || !!onCreateEvent;
|
|
492
601
|
const pxAlongAxis = (el: HTMLElement, e: ReactPointerEvent) => {
|
|
493
602
|
const rect = el.getBoundingClientRect();
|
|
@@ -495,7 +604,14 @@ export function ResourceTimeline<T = unknown>({
|
|
|
495
604
|
};
|
|
496
605
|
const beginCreate = (e: ReactPointerEvent, resource: Resource) => {
|
|
497
606
|
// Only from the lane background, primary button, never on an event bar.
|
|
498
|
-
if (
|
|
607
|
+
if (
|
|
608
|
+
!cellEnabled ||
|
|
609
|
+
e.pointerType === "touch" ||
|
|
610
|
+
e.isPrimary === false ||
|
|
611
|
+
createOrigin.current ||
|
|
612
|
+
dragRef.current
|
|
613
|
+
)
|
|
614
|
+
return;
|
|
499
615
|
if (e.target !== e.currentTarget || e.button > 0) return;
|
|
500
616
|
const el = e.currentTarget as HTMLElement;
|
|
501
617
|
const startPx = pxAlongAxis(el, e);
|
|
@@ -504,29 +620,38 @@ export function ResourceTimeline<T = unknown>({
|
|
|
504
620
|
} catch {
|
|
505
621
|
// Best-effort capture.
|
|
506
622
|
}
|
|
507
|
-
createOrigin.current = {
|
|
623
|
+
createOrigin.current = {
|
|
624
|
+
pointerId: e.pointerId,
|
|
625
|
+
clientX: e.clientX,
|
|
626
|
+
clientY: e.clientY,
|
|
627
|
+
el,
|
|
628
|
+
resource,
|
|
629
|
+
startPx,
|
|
630
|
+
dayKey: date.toDateString(),
|
|
631
|
+
creating: !!onCreateEvent,
|
|
632
|
+
};
|
|
508
633
|
// Only preview a ghost when a create can happen; in press-only mode
|
|
509
634
|
// (onPressCell without onCreateEvent) the tap still commits via endCreate.
|
|
510
635
|
if (onCreateEvent) setCreateBox({ resourceKey: resource.id, startPx, curPx: startPx });
|
|
511
636
|
};
|
|
512
637
|
const moveCreate = (e: ReactPointerEvent) => {
|
|
513
638
|
const o = createOrigin.current;
|
|
514
|
-
if (!o || !onCreateEvent) return;
|
|
639
|
+
if (!o || o.pointerId !== e.pointerId || !onCreateEvent) return;
|
|
640
|
+
o.clientX = e.clientX;
|
|
641
|
+
o.clientY = e.clientY;
|
|
515
642
|
setCreateBox({ resourceKey: o.resource.id, startPx: o.startPx, curPx: pxAlongAxis(o.el, e) });
|
|
516
643
|
};
|
|
517
644
|
const endCreate = (e: ReactPointerEvent) => {
|
|
518
645
|
const o = createOrigin.current;
|
|
519
|
-
if (!o) return;
|
|
520
|
-
try {
|
|
521
|
-
o.el.releasePointerCapture?.(e.pointerId);
|
|
522
|
-
} catch {
|
|
523
|
-
// Best-effort release.
|
|
524
|
-
}
|
|
646
|
+
if (!o || o.pointerId !== e.pointerId) return;
|
|
525
647
|
const endPx = pxAlongAxis(o.el, e);
|
|
526
648
|
const moved = Math.abs(endPx - o.startPx) > 4;
|
|
649
|
+
clearCreate();
|
|
650
|
+
const resource = resources.find((resource) => resource.id === o.resource.id);
|
|
651
|
+
if (!resource) return;
|
|
527
652
|
if (moved && onCreateEvent) {
|
|
528
653
|
const range = cellRangeFromDrag(date, o.startPx, endPx, hourSize, startHour, dragStepMinutes);
|
|
529
|
-
if (range) onCreateEvent(range.start, range.end,
|
|
654
|
+
if (range) onCreateEvent(range.start, range.end, resource);
|
|
530
655
|
} else if (onPressCell) {
|
|
531
656
|
const at = cellRangeFromDrag(
|
|
532
657
|
date,
|
|
@@ -536,25 +661,180 @@ export function ResourceTimeline<T = unknown>({
|
|
|
536
661
|
startHour,
|
|
537
662
|
dragStepMinutes,
|
|
538
663
|
);
|
|
539
|
-
if (at) onPressCell(at.start,
|
|
664
|
+
if (at) onPressCell(at.start, resource);
|
|
540
665
|
}
|
|
541
|
-
createOrigin.current = null;
|
|
542
|
-
setCreateBox(null);
|
|
543
666
|
};
|
|
544
667
|
// A gesture the browser cancels (scroll takeover, etc.) must not commit.
|
|
545
|
-
const cancelCreate = () => {
|
|
546
|
-
createOrigin.current
|
|
547
|
-
|
|
668
|
+
const cancelCreate = (e: ReactPointerEvent) => {
|
|
669
|
+
if (createOrigin.current?.pointerId !== e.pointerId) return;
|
|
670
|
+
clearCreate();
|
|
548
671
|
};
|
|
549
|
-
const
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
672
|
+
const RenderCreateGhost = renderCreateGhost;
|
|
673
|
+
// What `onCreateEvent` would receive if the sweep ended here. The props keep
|
|
674
|
+
// the same `Date`s while that range holds, so content effects keyed on them
|
|
675
|
+
// re-run only when it changes.
|
|
676
|
+
const liveRange =
|
|
677
|
+
createBox && RenderCreateGhost && onCreateEvent
|
|
678
|
+
? cellRangeFromDrag(
|
|
679
|
+
date,
|
|
680
|
+
createBox.startPx,
|
|
681
|
+
createBox.curPx,
|
|
682
|
+
hourSize,
|
|
683
|
+
startHour,
|
|
684
|
+
dragStepMinutes,
|
|
685
|
+
)
|
|
557
686
|
: null;
|
|
687
|
+
const liveStart = liveRange?.start.getTime();
|
|
688
|
+
const liveEnd = liveRange?.end.getTime();
|
|
689
|
+
const createRange = useMemo(
|
|
690
|
+
() =>
|
|
691
|
+
liveStart === undefined || liveEnd === undefined
|
|
692
|
+
? null
|
|
693
|
+
: { start: new Date(liveStart), end: new Date(liveEnd) },
|
|
694
|
+
[liveStart, liveEnd],
|
|
695
|
+
);
|
|
696
|
+
const trackInteractionProps = (resource: Resource) => ({
|
|
697
|
+
onPointerDown: (e: ReactPointerEvent) => beginCreate(e, resource),
|
|
698
|
+
onPointerMove: moveCreate,
|
|
699
|
+
onPointerUp: endCreate,
|
|
700
|
+
onPointerCancel: cancelCreate,
|
|
701
|
+
onLostPointerCapture: cancelCreate,
|
|
702
|
+
});
|
|
703
|
+
useEffect(() => {
|
|
704
|
+
if (
|
|
705
|
+
dragRef.current &&
|
|
706
|
+
(!origin.current?.element.isConnected || !events.includes(dragRef.current.event))
|
|
707
|
+
)
|
|
708
|
+
clearDrag();
|
|
709
|
+
const creating = createOrigin.current;
|
|
710
|
+
if (
|
|
711
|
+
creating &&
|
|
712
|
+
(!creating.el.isConnected ||
|
|
713
|
+
!cellEnabled ||
|
|
714
|
+
(creating.creating && !onCreateEvent) ||
|
|
715
|
+
creating.dayKey !== date.toDateString())
|
|
716
|
+
)
|
|
717
|
+
clearCreate();
|
|
718
|
+
});
|
|
719
|
+
useEffect(
|
|
720
|
+
() => () => {
|
|
721
|
+
const captured = origin.current;
|
|
722
|
+
const currentDrag = dragRef.current;
|
|
723
|
+
const currentCreate = createOrigin.current;
|
|
724
|
+
origin.current = null;
|
|
725
|
+
dragRef.current = null;
|
|
726
|
+
createOrigin.current = null;
|
|
727
|
+
try {
|
|
728
|
+
if (currentDrag) captured?.element.releasePointerCapture?.(currentDrag.pointerId);
|
|
729
|
+
currentCreate?.el.releasePointerCapture?.(currentCreate.pointerId);
|
|
730
|
+
} catch {
|
|
731
|
+
// Removing a captured element may already have released it.
|
|
732
|
+
}
|
|
733
|
+
},
|
|
734
|
+
[],
|
|
735
|
+
);
|
|
736
|
+
const eventKeys = useRef(new WeakMap<CalendarEvent<T>, number>());
|
|
737
|
+
const nextEventKey = useRef(0);
|
|
738
|
+
const eventKey = (event: CalendarEvent<T>) => {
|
|
739
|
+
let key = eventKeys.current.get(event);
|
|
740
|
+
if (key === undefined) {
|
|
741
|
+
key = nextEventKey.current++;
|
|
742
|
+
eventKeys.current.set(event, key);
|
|
743
|
+
}
|
|
744
|
+
return key;
|
|
745
|
+
};
|
|
746
|
+
// The actions dialog's steps, matching the native screen-reader actions: one
|
|
747
|
+
// snap step along the axis, or one lane across, through the same commit as a
|
|
748
|
+
// drop. `draggable: false` locks a bar, as it does for a drag.
|
|
749
|
+
const laneOf = (event: CalendarEvent<T>) =>
|
|
750
|
+
resources.findIndex((resource) => resource.id === resourceId(event));
|
|
751
|
+
const eventDescription = (event: CalendarEvent<T>) =>
|
|
752
|
+
`${resources.find((resource) => resource.id === resourceId(event))?.title ?? ""}, ${describeEventDates(event)}, ${eventAccessibilityLabel({ title: event.title, isAllDay: false, start: event.start, end: event.end, ampm: ampm, labels: labels })}`;
|
|
753
|
+
const actionDelta = (id: string): [number, number, number] | null => {
|
|
754
|
+
switch (id) {
|
|
755
|
+
case "move-later":
|
|
756
|
+
return [dragStepMinutes, dragStepMinutes, 0];
|
|
757
|
+
case "move-earlier":
|
|
758
|
+
return [-dragStepMinutes, -dragStepMinutes, 0];
|
|
759
|
+
case "extend":
|
|
760
|
+
return [0, dragStepMinutes, 0];
|
|
761
|
+
case "shorten":
|
|
762
|
+
return [0, -dragStepMinutes, 0];
|
|
763
|
+
case "move-next-resource":
|
|
764
|
+
return [0, 0, 1];
|
|
765
|
+
case "move-previous-resource":
|
|
766
|
+
return [0, 0, -1];
|
|
767
|
+
default:
|
|
768
|
+
return null;
|
|
769
|
+
}
|
|
770
|
+
};
|
|
771
|
+
const editActions = (event: CalendarEvent<T>): EventEditAction[] => {
|
|
772
|
+
if (isAllDayEvent(event) || isBackgroundEvent(event)) return [];
|
|
773
|
+
const lane = laneOf(event);
|
|
774
|
+
if (!onDragEvent || event.draggable === false || lane < 0) return [];
|
|
775
|
+
const action = (id: string, label: string): EventEditAction => {
|
|
776
|
+
const [startDelta, endDelta] = actionDelta(id) ?? [0, 0];
|
|
777
|
+
const next = resolveDraggedBounds(
|
|
778
|
+
event.start,
|
|
779
|
+
event.end,
|
|
780
|
+
startDelta,
|
|
781
|
+
endDelta,
|
|
782
|
+
dragStepMinutes,
|
|
783
|
+
);
|
|
784
|
+
return { id, label, available: next !== null };
|
|
785
|
+
};
|
|
786
|
+
const laneTitle = (resource?: Resource) => resource && (resource.title || resource.id);
|
|
787
|
+
const nextLane = laneTitle(resources[lane + 1]);
|
|
788
|
+
const previousLane = laneTitle(resources[lane - 1]);
|
|
789
|
+
return [
|
|
790
|
+
action("move-later", labels.moveLater(dragStepMinutes)),
|
|
791
|
+
action("move-earlier", labels.moveEarlier(dragStepMinutes)),
|
|
792
|
+
action("extend", labels.extend(dragStepMinutes)),
|
|
793
|
+
action("shorten", labels.shorten(dragStepMinutes)),
|
|
794
|
+
...(nextLane ? [action("move-next-resource", labels.moveToResource(nextLane))] : []),
|
|
795
|
+
...(previousLane
|
|
796
|
+
? [action("move-previous-resource", labels.moveToResource(previousLane))]
|
|
797
|
+
: []),
|
|
798
|
+
];
|
|
799
|
+
};
|
|
800
|
+
const eventActions = useEventActions(
|
|
801
|
+
events,
|
|
802
|
+
editActions,
|
|
803
|
+
(event, id) => {
|
|
804
|
+
const delta = actionDelta(id);
|
|
805
|
+
const target = delta ? resources[laneOf(event) + delta[2]] : undefined;
|
|
806
|
+
const next =
|
|
807
|
+
delta &&
|
|
808
|
+
target &&
|
|
809
|
+
resolveDraggedBounds(event.start, event.end, delta[0], delta[1], dragStepMinutes);
|
|
810
|
+
if (!next || !target || onDragEvent?.(event, next.start, next.end, target) === false)
|
|
811
|
+
return null;
|
|
812
|
+
return (candidate) =>
|
|
813
|
+
candidate !== event &&
|
|
814
|
+
candidate.start.getTime() === next.start.getTime() &&
|
|
815
|
+
candidate.end.getTime() === next.end.getTime() &&
|
|
816
|
+
candidate.title === event.title &&
|
|
817
|
+
resourceId(candidate) === target.id;
|
|
818
|
+
},
|
|
819
|
+
getEventId,
|
|
820
|
+
);
|
|
821
|
+
const openActionsEvent = eventActions.openEvent;
|
|
822
|
+
const actionsDialog = openActionsEvent ? (
|
|
823
|
+
<EventActionsDialog
|
|
824
|
+
dialogRef={eventActions.dialogRef}
|
|
825
|
+
title={labels.eventActions(openActionsEvent.title ?? "")}
|
|
826
|
+
description={eventDescription(openActionsEvent)}
|
|
827
|
+
actions={editActions(openActionsEvent)}
|
|
828
|
+
closeLabel={labels.close}
|
|
829
|
+
rejectedLabel={labels.eventChangeRejected}
|
|
830
|
+
rejections={eventActions.rejections}
|
|
831
|
+
onAction={eventActions.run}
|
|
832
|
+
onClose={() => eventActions.close(true)}
|
|
833
|
+
slot={slot}
|
|
834
|
+
theme={theme}
|
|
835
|
+
/>
|
|
836
|
+
) : null;
|
|
837
|
+
|
|
558
838
|
// The closed-hours bands to shade in a lane, resolved per resource.
|
|
559
839
|
const bandsFor = (resource: Resource) =>
|
|
560
840
|
businessHours
|
|
@@ -591,7 +871,7 @@ export function ResourceTimeline<T = unknown>({
|
|
|
591
871
|
|
|
592
872
|
// The lane a move is dragging in, lifted above its siblings so the ghost
|
|
593
873
|
// stays visible as it crosses into another lane (a later DOM sibling).
|
|
594
|
-
const dragLaneId = drag?.kind === "move" && drag.moved ? drag.
|
|
874
|
+
const dragLaneId = drag?.kind === "move" && drag.moved ? drag.resource.id : null;
|
|
595
875
|
|
|
596
876
|
if (vertical) {
|
|
597
877
|
// Time flows down like the time grid: hour gutter on the left, one flexed
|
|
@@ -601,9 +881,23 @@ export function ResourceTimeline<T = unknown>({
|
|
|
601
881
|
const vGridLines = `repeating-linear-gradient(to bottom, transparent 0, transparent ${hourHeight - 1}px, ${theme.gridLine} ${hourHeight - 1}px, ${theme.gridLine} ${hourHeight}px)`;
|
|
602
882
|
return (
|
|
603
883
|
<div
|
|
884
|
+
ref={eventActions.rootRef}
|
|
885
|
+
tabIndex={-1}
|
|
604
886
|
className={className}
|
|
605
887
|
style={{ fontFamily: theme.fontFamily, color: theme.text, overflowY: "auto", ...style }}
|
|
606
888
|
>
|
|
889
|
+
<EventActionsMenu
|
|
890
|
+
events={[...new Set([...byResource.values()].flat().map((pe) => pe.event))].filter(
|
|
891
|
+
(event) => editActions(event).length > 0,
|
|
892
|
+
)}
|
|
893
|
+
label={labels.editEvents}
|
|
894
|
+
labelFor={(event) => labels.eventActions(event.title ?? "")}
|
|
895
|
+
descriptionFor={eventDescription}
|
|
896
|
+
onSelect={eventActions.select}
|
|
897
|
+
rootRef={eventActions.rootRef}
|
|
898
|
+
slot={slot}
|
|
899
|
+
theme={theme}
|
|
900
|
+
/>
|
|
607
901
|
{/* Header: corner above the hour gutter + one label per resource column */}
|
|
608
902
|
<div
|
|
609
903
|
{...slot("header", {
|
|
@@ -736,9 +1030,10 @@ export function ResourceTimeline<T = unknown>({
|
|
|
736
1030
|
themed: { backgroundImage: vGridLines },
|
|
737
1031
|
})}
|
|
738
1032
|
/>
|
|
739
|
-
{positioned.map((pe
|
|
740
|
-
const key =
|
|
741
|
-
const active =
|
|
1033
|
+
{positioned.map((pe) => {
|
|
1034
|
+
const key = eventKey(pe.event);
|
|
1035
|
+
const active =
|
|
1036
|
+
drag?.event === pe.event && drag.resource.id === resource.id ? drag : null;
|
|
742
1037
|
const startH = active ? active.startHours : pe.startHours;
|
|
743
1038
|
const durH = active ? active.durationHours : pe.durationHours;
|
|
744
1039
|
const top = clamp(startH - startHour, 0, endHour - startHour) * hourHeight;
|
|
@@ -758,19 +1053,22 @@ export function ResourceTimeline<T = unknown>({
|
|
|
758
1053
|
const draggable = !!onDragEvent && pe.event.draggable !== false;
|
|
759
1054
|
return (
|
|
760
1055
|
<button
|
|
761
|
-
key={
|
|
1056
|
+
key={key}
|
|
762
1057
|
type="button"
|
|
763
|
-
onClick={
|
|
1058
|
+
onClick={(e) => {
|
|
1059
|
+
if (e.detail !== 0 && handledClick.current === e.currentTarget) {
|
|
1060
|
+
handledClick.current = null;
|
|
1061
|
+
return;
|
|
1062
|
+
}
|
|
1063
|
+
if (!draggable || e.detail === 0) onPress();
|
|
1064
|
+
}}
|
|
764
1065
|
aria-label={pe.event.title}
|
|
765
1066
|
{...dataState({ "data-dragging": !!active })}
|
|
766
|
-
onPointerDown={
|
|
767
|
-
|
|
768
|
-
}
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
draggable ? (e) => endDrag(e, pe.event, resource, onPress) : undefined
|
|
772
|
-
}
|
|
773
|
-
onPointerCancel={draggable ? cancelDrag : undefined}
|
|
1067
|
+
onPointerDown={(e) => beginDrag(e, resource, "move", pe)}
|
|
1068
|
+
onPointerMove={moveDrag}
|
|
1069
|
+
onPointerUp={endDrag}
|
|
1070
|
+
onPointerCancel={cancelDrag}
|
|
1071
|
+
onLostPointerCapture={cancelDrag}
|
|
774
1072
|
{...slot("event", {
|
|
775
1073
|
base: {
|
|
776
1074
|
position: "absolute",
|
|
@@ -805,7 +1103,7 @@ export function ResourceTimeline<T = unknown>({
|
|
|
805
1103
|
aria-hidden
|
|
806
1104
|
onPointerDown={(e) => {
|
|
807
1105
|
e.stopPropagation();
|
|
808
|
-
beginDrag(e,
|
|
1106
|
+
beginDrag(e, resource, "resize", pe);
|
|
809
1107
|
}}
|
|
810
1108
|
style={{
|
|
811
1109
|
position: "absolute",
|
|
@@ -823,8 +1121,10 @@ export function ResourceTimeline<T = unknown>({
|
|
|
823
1121
|
})}
|
|
824
1122
|
{nowLine(resource.id)}
|
|
825
1123
|
{createBox?.resourceKey === resource.id ? (
|
|
826
|
-
<
|
|
1124
|
+
<InertDiv
|
|
827
1125
|
aria-hidden
|
|
1126
|
+
// Custom content stays out of the tab order and pointer input.
|
|
1127
|
+
inert={!!createRange}
|
|
828
1128
|
{...slot("createGhost", {
|
|
829
1129
|
base: {
|
|
830
1130
|
position: "absolute",
|
|
@@ -832,6 +1132,7 @@ export function ResourceTimeline<T = unknown>({
|
|
|
832
1132
|
right: 1,
|
|
833
1133
|
top: Math.min(createBox.startPx, createBox.curPx),
|
|
834
1134
|
height: Math.max(Math.abs(createBox.curPx - createBox.startPx), 2),
|
|
1135
|
+
...(createRange ? { overflow: "hidden" } : null),
|
|
835
1136
|
opacity: 0.7,
|
|
836
1137
|
pointerEvents: "none",
|
|
837
1138
|
zIndex: 2,
|
|
@@ -842,13 +1143,22 @@ export function ResourceTimeline<T = unknown>({
|
|
|
842
1143
|
borderRadius: 6,
|
|
843
1144
|
},
|
|
844
1145
|
})}
|
|
845
|
-
|
|
1146
|
+
>
|
|
1147
|
+
{createRange && RenderCreateGhost ? (
|
|
1148
|
+
<RenderCreateGhost
|
|
1149
|
+
start={createRange.start}
|
|
1150
|
+
end={createRange.end}
|
|
1151
|
+
resource={resource}
|
|
1152
|
+
/>
|
|
1153
|
+
) : null}
|
|
1154
|
+
</InertDiv>
|
|
846
1155
|
) : null}
|
|
847
1156
|
</div>
|
|
848
1157
|
</div>
|
|
849
1158
|
);
|
|
850
1159
|
})}
|
|
851
1160
|
</div>
|
|
1161
|
+
{actionsDialog}
|
|
852
1162
|
</div>
|
|
853
1163
|
);
|
|
854
1164
|
}
|
|
@@ -857,9 +1167,23 @@ export function ResourceTimeline<T = unknown>({
|
|
|
857
1167
|
|
|
858
1168
|
return (
|
|
859
1169
|
<div
|
|
1170
|
+
ref={eventActions.rootRef}
|
|
1171
|
+
tabIndex={-1}
|
|
860
1172
|
className={className}
|
|
861
1173
|
style={{ fontFamily: theme.fontFamily, color: theme.text, overflowX: "auto", ...style }}
|
|
862
1174
|
>
|
|
1175
|
+
<EventActionsMenu
|
|
1176
|
+
events={[...new Set([...byResource.values()].flat().map((pe) => pe.event))].filter(
|
|
1177
|
+
(event) => editActions(event).length > 0,
|
|
1178
|
+
)}
|
|
1179
|
+
label={labels.editEvents}
|
|
1180
|
+
labelFor={(event) => labels.eventActions(event.title ?? "")}
|
|
1181
|
+
descriptionFor={eventDescription}
|
|
1182
|
+
onSelect={eventActions.select}
|
|
1183
|
+
rootRef={eventActions.rootRef}
|
|
1184
|
+
slot={slot}
|
|
1185
|
+
theme={theme}
|
|
1186
|
+
/>
|
|
863
1187
|
<div style={{ minWidth: labelWidth + trackWidth }}>
|
|
864
1188
|
{/* Header: corner + hour axis */}
|
|
865
1189
|
<div
|
|
@@ -976,9 +1300,10 @@ export function ResourceTimeline<T = unknown>({
|
|
|
976
1300
|
themed: { backgroundImage: gridLines },
|
|
977
1301
|
})}
|
|
978
1302
|
/>
|
|
979
|
-
{positioned.map((pe
|
|
980
|
-
const key =
|
|
981
|
-
const active =
|
|
1303
|
+
{positioned.map((pe) => {
|
|
1304
|
+
const key = eventKey(pe.event);
|
|
1305
|
+
const active =
|
|
1306
|
+
drag?.event === pe.event && drag.resource.id === resource.id ? drag : null;
|
|
982
1307
|
const startH = active ? active.startHours : pe.startHours;
|
|
983
1308
|
const durH = active ? active.durationHours : pe.durationHours;
|
|
984
1309
|
const left = clamp(startH - startHour, 0, endHour - startHour) * hourWidth;
|
|
@@ -989,22 +1314,25 @@ export function ResourceTimeline<T = unknown>({
|
|
|
989
1314
|
const laneHeight = rowHeight / pe.columns;
|
|
990
1315
|
const onPress = () => onPressEvent?.(pe.event);
|
|
991
1316
|
const args: ResourceEventArgs<T> = { event: pe.event, width, onPress };
|
|
992
|
-
const draggable = !!onDragEvent;
|
|
1317
|
+
const draggable = !!onDragEvent && pe.event.draggable !== false;
|
|
993
1318
|
return (
|
|
994
1319
|
<button
|
|
995
|
-
key={
|
|
1320
|
+
key={key}
|
|
996
1321
|
type="button"
|
|
997
|
-
onClick={
|
|
1322
|
+
onClick={(e) => {
|
|
1323
|
+
if (e.detail !== 0 && handledClick.current === e.currentTarget) {
|
|
1324
|
+
handledClick.current = null;
|
|
1325
|
+
return;
|
|
1326
|
+
}
|
|
1327
|
+
if (!draggable || e.detail === 0) onPress();
|
|
1328
|
+
}}
|
|
998
1329
|
aria-label={pe.event.title}
|
|
999
1330
|
{...dataState({ "data-dragging": !!active })}
|
|
1000
|
-
onPointerDown={
|
|
1001
|
-
|
|
1002
|
-
}
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
draggable ? (e) => endDrag(e, pe.event, resource, onPress) : undefined
|
|
1006
|
-
}
|
|
1007
|
-
onPointerCancel={draggable ? cancelDrag : undefined}
|
|
1331
|
+
onPointerDown={(e) => beginDrag(e, resource, "move", pe)}
|
|
1332
|
+
onPointerMove={moveDrag}
|
|
1333
|
+
onPointerUp={endDrag}
|
|
1334
|
+
onPointerCancel={cancelDrag}
|
|
1335
|
+
onLostPointerCapture={cancelDrag}
|
|
1008
1336
|
{...slot("event", {
|
|
1009
1337
|
base: {
|
|
1010
1338
|
position: "absolute",
|
|
@@ -1037,7 +1365,7 @@ export function ResourceTimeline<T = unknown>({
|
|
|
1037
1365
|
aria-hidden
|
|
1038
1366
|
onPointerDown={(e) => {
|
|
1039
1367
|
e.stopPropagation();
|
|
1040
|
-
beginDrag(e,
|
|
1368
|
+
beginDrag(e, resource, "resize", pe);
|
|
1041
1369
|
}}
|
|
1042
1370
|
style={{
|
|
1043
1371
|
position: "absolute",
|
|
@@ -1055,8 +1383,10 @@ export function ResourceTimeline<T = unknown>({
|
|
|
1055
1383
|
})}
|
|
1056
1384
|
{nowLine(resource.id)}
|
|
1057
1385
|
{createBox?.resourceKey === resource.id ? (
|
|
1058
|
-
<
|
|
1386
|
+
<InertDiv
|
|
1059
1387
|
aria-hidden
|
|
1388
|
+
// Custom content stays out of the tab order and pointer input.
|
|
1389
|
+
inert={!!createRange}
|
|
1060
1390
|
{...slot("createGhost", {
|
|
1061
1391
|
base: {
|
|
1062
1392
|
position: "absolute",
|
|
@@ -1064,6 +1394,7 @@ export function ResourceTimeline<T = unknown>({
|
|
|
1064
1394
|
bottom: 1,
|
|
1065
1395
|
left: Math.min(createBox.startPx, createBox.curPx),
|
|
1066
1396
|
width: Math.max(Math.abs(createBox.curPx - createBox.startPx), 2),
|
|
1397
|
+
...(createRange ? { overflow: "hidden" } : null),
|
|
1067
1398
|
opacity: 0.7,
|
|
1068
1399
|
pointerEvents: "none",
|
|
1069
1400
|
zIndex: 2,
|
|
@@ -1074,13 +1405,22 @@ export function ResourceTimeline<T = unknown>({
|
|
|
1074
1405
|
borderRadius: 6,
|
|
1075
1406
|
},
|
|
1076
1407
|
})}
|
|
1077
|
-
|
|
1408
|
+
>
|
|
1409
|
+
{createRange && RenderCreateGhost ? (
|
|
1410
|
+
<RenderCreateGhost
|
|
1411
|
+
start={createRange.start}
|
|
1412
|
+
end={createRange.end}
|
|
1413
|
+
resource={resource}
|
|
1414
|
+
/>
|
|
1415
|
+
) : null}
|
|
1416
|
+
</InertDiv>
|
|
1078
1417
|
) : null}
|
|
1079
1418
|
</div>
|
|
1080
1419
|
</div>
|
|
1081
1420
|
);
|
|
1082
1421
|
})}
|
|
1083
1422
|
</div>
|
|
1423
|
+
{actionsDialog}
|
|
1084
1424
|
</div>
|
|
1085
1425
|
);
|
|
1086
1426
|
}
|