@moltenship/calendar-dnd 2.0.0-beta.1 → 2.0.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 +5 -5
- package/dist/index.d.mts +12 -13
- package/dist/index.mjs +595 -105
- package/package.json +4 -12
package/README.md
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
# @moltenship/calendar-dnd
|
|
2
2
|
|
|
3
3
|
The pointer layer of Moltenship Time: drag to move, resize and create events inside a view, on
|
|
4
|
-
native Pointer Events. Drags become change proposals for your app to save. `
|
|
5
|
-
does this
|
|
4
|
+
native Pointer Events. Drags become change proposals for your app to save. `attachCalendarPointer`
|
|
5
|
+
does this for a view's element. It's plain DOM, with no framework: `useCalendarPointer`, the React
|
|
6
|
+
binding, is in `@moltenship/calendar-react`.
|
|
6
7
|
|
|
7
8
|
It also takes drags across the calendar's edge: `startCalendarDrop` drops items dragged in from
|
|
8
9
|
the rest of the page, such as unscheduled tasks, into the view under them, and
|
|
@@ -15,10 +16,9 @@ It's a layer of `@moltenship/calendar`, which re-exports all of it. Most apps in
|
|
|
15
16
|
## Install
|
|
16
17
|
|
|
17
18
|
```bash
|
|
18
|
-
pnpm add @moltenship/calendar-dnd
|
|
19
|
+
pnpm add @moltenship/calendar-dnd
|
|
19
20
|
```
|
|
20
21
|
|
|
21
|
-
- React 19. React is a peer dependency.
|
|
22
22
|
- A runtime with `Temporal` (Node.js 26 and later include it), or a polyfill that defines
|
|
23
23
|
`globalThis.Temporal`, loaded first.
|
|
24
24
|
- TypeScript 6 or later with `"lib": ["DOM", "DOM.Iterable", "ESNext"]`. `ESNext` has the
|
|
@@ -26,7 +26,7 @@ pnpm add @moltenship/calendar-dnd react
|
|
|
26
26
|
|
|
27
27
|
## Documentation
|
|
28
28
|
|
|
29
|
-
- [Interactions](https://time.molten.sh/docs/interactions)
|
|
29
|
+
- [Interactions](https://time.molten.sh/docs/react/interactions)
|
|
30
30
|
- [Repository](https://github.com/Moltenship/time)
|
|
31
31
|
|
|
32
32
|
## License
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { CalendarAnnouncer, CalendarDraft, CalendarEventPlacement, CalendarEventProposal, CalendarPolicyResult, CalendarProposalDetail, CalendarTransactionResult, EventId, EventSnapshot, NormalizedCalendarEvent, ResourceId, TimeGridColumn } from "@moltenship/calendar-core";
|
|
2
2
|
import { WallTimeSnapOptions } from "@moltenship/calendar-temporal";
|
|
3
|
-
import { RefCallback } from "react";
|
|
4
3
|
interface CalendarPointerOptions<TEvent, TEventId extends EventId, TResourceId extends ResourceId> {
|
|
5
4
|
/** Events as rendered, pending changes included; resolves `data-event-id`. */
|
|
6
5
|
readonly events: EventSnapshot<TEvent, TEventId, TResourceId>;
|
|
@@ -36,7 +35,7 @@ interface CalendarPointerOptions<TEvent, TEventId extends EventId, TResourceId e
|
|
|
36
35
|
/**
|
|
37
36
|
* Turns gestures off. It's also read when the layer attaches: a region disabled then doesn't
|
|
38
37
|
* listen for touch moves, which a drag needs on iOS, so attach again when it changes, as
|
|
39
|
-
* `useCalendarPointer` does.
|
|
38
|
+
* the React integration's `useCalendarPointer` does.
|
|
40
39
|
*/
|
|
41
40
|
readonly disabled?: boolean;
|
|
42
41
|
/**
|
|
@@ -47,6 +46,15 @@ interface CalendarPointerOptions<TEvent, TEventId extends EventId, TResourceId e
|
|
|
47
46
|
readonly onPreview?: (draft: CalendarDraft<TEventId, TResourceId> | null) => void;
|
|
48
47
|
/** Extra classes for the preview segments. */
|
|
49
48
|
readonly ghostClassName?: string;
|
|
49
|
+
/**
|
|
50
|
+
* How the preview follows the pointer. `"snapped"`, the default, draws the event where a drop
|
|
51
|
+
* would land, so it steps from slot to slot. `"free"` moves a copy of a moved event with the
|
|
52
|
+
* pointer, pixel for pixel, over boxes where a drop would land
|
|
53
|
+
* (`[data-calendar-ghost="landing"]`), and slides it there on release; a resize or create on a
|
|
54
|
+
* time axis draws its edge under the pointer. Both propose, check, and announce the same snapped
|
|
55
|
+
* placement.
|
|
56
|
+
*/
|
|
57
|
+
readonly preview?: "free" | "snapped";
|
|
50
58
|
/**
|
|
51
59
|
* A ref to the element scrolled while dragging near its edges. Defaults to the nearest scrollable
|
|
52
60
|
* ancestor of where the drag starts.
|
|
@@ -226,7 +234,7 @@ interface CalendarDropSession<TEvent = unknown, TEventId extends EventId = Event
|
|
|
226
234
|
/**
|
|
227
235
|
* Follows a drag that started outside the calendar, such as a task dragged from a list, over the
|
|
228
236
|
* calendar views on the page, and drops it into one: the framework-agnostic half of external drag
|
|
229
|
-
* and drop, which a drag library's binding drives. Every view that uses `
|
|
237
|
+
* and drop, which a drag library's binding drives. Every view that uses `attachCalendarPointer` takes
|
|
230
238
|
* part, through the options it passes: the preview shows range boxes in its slots, its `check`
|
|
231
239
|
* marks them `data-invalid` or `data-warning`, its announcer speaks each step with `outside` set,
|
|
232
240
|
* and its `onPropose` gets the drop. Read-only views take no part.
|
|
@@ -257,7 +265,7 @@ export declare function eventIdOf(element: Element): EventId | null;
|
|
|
257
265
|
interface CalendarRegion {
|
|
258
266
|
/** Whether its gestures are off, as on a read-only root: then it takes no drops either. */
|
|
259
267
|
readonly disabled: boolean;
|
|
260
|
-
/** The element the view
|
|
268
|
+
/** The element the view attached with `attachCalendarPointer`, or a binding of it. */
|
|
261
269
|
readonly element: HTMLElement;
|
|
262
270
|
}
|
|
263
271
|
/**
|
|
@@ -280,13 +288,4 @@ export declare function resourceIdAttributes(id: ResourceId | null): {
|
|
|
280
288
|
* unassigned bucket, `undefined` outside any resource.
|
|
281
289
|
*/
|
|
282
290
|
export declare function resourceIdOf(element: Element): ResourceId | null | undefined;
|
|
283
|
-
/**
|
|
284
|
-
* Pointer move, resize, and create for a view region; see `attachCalendarPointer` for the data
|
|
285
|
-
* attributes the view renders. Returns a ref for the region element. Options are read when a
|
|
286
|
-
* gesture starts, and `columns` again as it moves, so changing them never re-attaches listeners,
|
|
287
|
-
* and dragging never re-renders. `disabled` is the exception: a disabled region doesn't listen for
|
|
288
|
-
* touch moves, so a read-only calendar's swipes never wait for its listener, and changing it
|
|
289
|
-
* attaches the listeners again.
|
|
290
|
-
*/
|
|
291
|
-
export declare function useCalendarPointer<TEvent, TEventId extends EventId, TResourceId extends ResourceId>(options: CalendarPointerOptions<TEvent, TEventId, TResourceId>): RefCallback<HTMLElement>;
|
|
292
291
|
export type { CalendarDragFollower, CalendarDragMonitor, CalendarDragPreview, CalendarDropOptions, CalendarDropSession, CalendarDropSource, CalendarEventDrag, CalendarPoint, CalendarPointerOptions, CalendarRect, CalendarRegion };
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
import { belongsInTimeGridAllDayRow, createPlacementBetween, eventPlacement, formatPlacementRange, movePlacementToTarget, resizePlacementToTarget, showsInResource, splitEventsByDay, timeGridDateTimeAt, timedFragmentBox, timelineExtent } from "@moltenship/calendar-core";
|
|
1
|
+
import { belongsInTimeGridAllDayRow, createPlacementBetween, eventPlacement, formatPlacementRange, movePlacementToTarget, resizePlacementToTarget, showsInResource, slotDuration, splitEventsByDay, timeGridDateTimeAt, timedFragmentBox, timelineExtent } from "@moltenship/calendar-core";
|
|
3
2
|
import { comparePlainDates, instantInterval, plainDateInterval } from "@moltenship/calendar-temporal";
|
|
4
|
-
import { useCallback, useInsertionEffect, useRef } from "react";
|
|
5
3
|
// src/auto-scroll.ts
|
|
6
4
|
const EDGE = 48;
|
|
7
5
|
const MAXIMUM_STEP = 18;
|
|
@@ -190,6 +188,42 @@ function createdPlacement(anchor, current, { snap }) {
|
|
|
190
188
|
};
|
|
191
189
|
return createPlacementBetween(interactionTarget(anchor, floor), interactionTarget(current, floor), { snap });
|
|
192
190
|
}
|
|
191
|
+
/** A free drag's drawn edge follows the pointer to the second; drops still land on slots. */
|
|
192
|
+
const FOLLOWING = {
|
|
193
|
+
increment: 1,
|
|
194
|
+
smallestUnit: "second"
|
|
195
|
+
};
|
|
196
|
+
/**
|
|
197
|
+
* Where a free resize (`preview: "free"`) draws the event: its dragged edge under the pointer
|
|
198
|
+
* rather than on a slot, never shorter than one. For drawing only; `null` across axes or
|
|
199
|
+
* resources, as `resizedPlacement` is.
|
|
200
|
+
*/
|
|
201
|
+
function followedResize(original, edge, grab, current, options) {
|
|
202
|
+
if (grab.kind !== "time" || current.kind !== "time") return null;
|
|
203
|
+
return resizePlacementToTarget(original, edge, interactionTarget(grab, FOLLOWING), interactionTarget(current, FOLLOWING), options);
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Where a free create draws its range: from the slot it started in to the pointer, at least that
|
|
207
|
+
* slot. For drawing only; `null` across axes or resources, as `createdPlacement` is.
|
|
208
|
+
*/
|
|
209
|
+
function followedCreate(anchor, current, { snap }) {
|
|
210
|
+
if (anchor.kind !== "time" || current.kind !== "time") return null;
|
|
211
|
+
if (anchor.resourceId !== current.resourceId) return null;
|
|
212
|
+
const start = timeGridDateTimeAt(anchor.column, anchor.y, { snap: {
|
|
213
|
+
...snap,
|
|
214
|
+
mode: "floor"
|
|
215
|
+
} });
|
|
216
|
+
const end = start.add(slotDuration(snap));
|
|
217
|
+
const at = timeGridDateTimeAt(current.column, current.y, { snap: FOLLOWING });
|
|
218
|
+
const resourceIds = anchor.resourceId == null ? [] : [anchor.resourceId];
|
|
219
|
+
const before = Temporal.ZonedDateTime.compare(at, start) < 0;
|
|
220
|
+
return {
|
|
221
|
+
end: (before || Temporal.ZonedDateTime.compare(at, end) < 0 ? end : at).toInstant(),
|
|
222
|
+
kind: "timed",
|
|
223
|
+
resourceIds,
|
|
224
|
+
start: (before ? at : start).toInstant()
|
|
225
|
+
};
|
|
226
|
+
}
|
|
193
227
|
/**
|
|
194
228
|
* Where an item dragged in from outside lands, starting at the slot or day under the pointer. A
|
|
195
229
|
* new event is `duration` long over a time column and `days` long over day cells, in the resource
|
|
@@ -356,6 +390,10 @@ function visibleBounds(surface, cell, rectOf) {
|
|
|
356
390
|
}
|
|
357
391
|
return bounds;
|
|
358
392
|
}
|
|
393
|
+
/** Where an element's content can paint, read now: for one box, outside any layout pass. */
|
|
394
|
+
function visibleOf(surface, element) {
|
|
395
|
+
return visibleBounds(surface, element, (ancestor) => ancestor.getBoundingClientRect());
|
|
396
|
+
}
|
|
359
397
|
function isVisible({ height, left, top, visible, width }) {
|
|
360
398
|
return left < visible.right && left + width > visible.left && top < visible.bottom && top + height > visible.top;
|
|
361
399
|
}
|
|
@@ -503,22 +541,106 @@ function ghostValidity(check) {
|
|
|
503
541
|
return check.change.warnings !== void 0 && check.change.warnings.length > 0 ? "warning" : null;
|
|
504
542
|
}
|
|
505
543
|
/**
|
|
544
|
+
* How long a free preview takes to settle where it lands, unless the computed
|
|
545
|
+
* `transition-duration` says otherwise; see `--calendar-ghost-settle-duration`.
|
|
546
|
+
*/
|
|
547
|
+
const SETTLE_MS = 150;
|
|
548
|
+
const SETTLE_TRANSITION = [
|
|
549
|
+
"transform",
|
|
550
|
+
"width",
|
|
551
|
+
"height",
|
|
552
|
+
"clip-path"
|
|
553
|
+
].map((property) => `${property} var(--calendar-ghost-settle-duration, ${SETTLE_MS}ms) var(--calendar-ghost-settle-easing, ease-out)`).join(", ");
|
|
554
|
+
function prefersReducedMotion() {
|
|
555
|
+
return typeof matchMedia === "function" && matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
556
|
+
}
|
|
557
|
+
/** The longest of an element's computed transition durations, in milliseconds. */
|
|
558
|
+
function transitionMs(element) {
|
|
559
|
+
const durations = getComputedStyle(element).transitionDuration.split(",").map((duration) => {
|
|
560
|
+
const value = Number.parseFloat(duration);
|
|
561
|
+
return duration.trim().endsWith("ms") ? value : value * 1e3;
|
|
562
|
+
}).filter(Number.isFinite);
|
|
563
|
+
return durations.length === 0 ? SETTLE_MS : Math.max(...durations);
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* Starts `segments` sliding to where `place` puts them, then removes them and `cleanup`'s parts.
|
|
567
|
+
* The current position is read first, so the change transitions from it.
|
|
568
|
+
*/
|
|
569
|
+
function settle(segments, place, cleanup) {
|
|
570
|
+
const remove = () => {
|
|
571
|
+
for (const segment of segments) segment.remove();
|
|
572
|
+
cleanup();
|
|
573
|
+
};
|
|
574
|
+
const [first] = segments;
|
|
575
|
+
if (first === void 0 || prefersReducedMotion()) {
|
|
576
|
+
remove();
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
for (const segment of segments) {
|
|
580
|
+
getComputedStyle(segment).transform;
|
|
581
|
+
segment.style.transition = SETTLE_TRANSITION;
|
|
582
|
+
segment.setAttribute("data-settling", "");
|
|
583
|
+
}
|
|
584
|
+
segments.forEach(place);
|
|
585
|
+
setTimeout(remove, transitionMs(first) + 20);
|
|
586
|
+
}
|
|
587
|
+
/**
|
|
588
|
+
* Finds where a fixed `left: 0; top: 0` box actually sits. It is the viewport origin unless an
|
|
589
|
+
* ancestor is transformed, which makes that ancestor the containing block of fixed elements.
|
|
590
|
+
*/
|
|
591
|
+
var OriginProbe = class {
|
|
592
|
+
#element = null;
|
|
593
|
+
#region;
|
|
594
|
+
constructor(region) {
|
|
595
|
+
this.#region = region;
|
|
596
|
+
}
|
|
597
|
+
measure() {
|
|
598
|
+
if (this.#element === null) {
|
|
599
|
+
this.#element = document.createElement("div");
|
|
600
|
+
this.#element.setAttribute("aria-hidden", "true");
|
|
601
|
+
this.#element.setAttribute("data-calendar-ghost-origin", "");
|
|
602
|
+
Object.assign(this.#element.style, {
|
|
603
|
+
height: "0",
|
|
604
|
+
left: "0",
|
|
605
|
+
pointerEvents: "none",
|
|
606
|
+
position: "fixed",
|
|
607
|
+
top: "0",
|
|
608
|
+
visibility: "hidden",
|
|
609
|
+
width: "0"
|
|
610
|
+
});
|
|
611
|
+
this.#region.append(this.#element);
|
|
612
|
+
}
|
|
613
|
+
return this.#element.getBoundingClientRect();
|
|
614
|
+
}
|
|
615
|
+
remove() {
|
|
616
|
+
this.#element?.remove();
|
|
617
|
+
this.#element = null;
|
|
618
|
+
}
|
|
619
|
+
};
|
|
620
|
+
/**
|
|
506
621
|
* Fixed-position preview segments, moved with transforms so a drag never re-renders the view, and
|
|
507
622
|
* clipped to their cells' scroll viewports. Segments are translucent clones of the dragged event,
|
|
508
623
|
* or range boxes for creates and day cells; style them through `[data-calendar-ghost]` or the
|
|
509
624
|
* `--calendar-ghost-*` custom properties. A clone's `[data-event-time]` shows `label`, and
|
|
510
625
|
* `data-invalid` or `data-warning` marks a drop the policies would reject or flag. A clone that
|
|
511
626
|
* follows the pointer outside the view, toward a drop zone of the app's, has `data-outside`.
|
|
627
|
+
*
|
|
628
|
+
* A `"landing"` layer draws the boxes where a free drag (`preview: "free"`) would land, as
|
|
629
|
+
* `[data-calendar-ghost="landing"]`, styled through `--calendar-ghost-landing-background` and
|
|
630
|
+
* `--calendar-ghost-landing-shadow`.
|
|
512
631
|
*/
|
|
513
632
|
var GhostLayer = class {
|
|
514
633
|
#className;
|
|
515
|
-
#
|
|
634
|
+
#kind;
|
|
516
635
|
#outside = false;
|
|
636
|
+
#origin;
|
|
517
637
|
#region;
|
|
518
638
|
#segments = [];
|
|
519
639
|
#template = null;
|
|
520
|
-
constructor(region, className) {
|
|
640
|
+
constructor(region, className, kind = "preview") {
|
|
521
641
|
this.#className = className;
|
|
642
|
+
this.#kind = kind;
|
|
643
|
+
this.#origin = new OriginProbe(region);
|
|
522
644
|
this.#region = region;
|
|
523
645
|
}
|
|
524
646
|
/**
|
|
@@ -534,21 +656,10 @@ var GhostLayer = class {
|
|
|
534
656
|
while (this.#segments.length < rects.length) this.#segments.push(this.#createSegment());
|
|
535
657
|
while (this.#segments.length > rects.length) this.#segments.pop().remove();
|
|
536
658
|
if (rects.length === 0) return;
|
|
537
|
-
const origin = this.#
|
|
659
|
+
const origin = this.#origin.measure();
|
|
538
660
|
rects.forEach((rect, index) => {
|
|
539
661
|
const segment = this.#segments[index];
|
|
540
|
-
|
|
541
|
-
const { height, left, top, visible, width } = rect;
|
|
542
|
-
const insets = [
|
|
543
|
-
visible.top - top,
|
|
544
|
-
left + width - visible.right,
|
|
545
|
-
top + height - visible.bottom,
|
|
546
|
-
visible.left - left
|
|
547
|
-
].map((inset) => Math.max(0, inset));
|
|
548
|
-
style.width = `${width}px`;
|
|
549
|
-
style.height = `${height}px`;
|
|
550
|
-
style.transform = `translate3d(${left - origin.left}px, ${top - origin.top}px, 0)`;
|
|
551
|
-
style.clipPath = insets.some((inset) => inset > 0) ? `inset(${insets.map((inset) => `${inset}px`).join(" ")})` : "";
|
|
662
|
+
this.#place(segment, rect, origin);
|
|
552
663
|
segment.toggleAttribute("data-invalid", validity === "invalid");
|
|
553
664
|
segment.toggleAttribute("data-warning", validity === "warning");
|
|
554
665
|
segment.toggleAttribute("data-outside", outside);
|
|
@@ -559,63 +670,255 @@ var GhostLayer = class {
|
|
|
559
670
|
clear() {
|
|
560
671
|
for (const segment of this.#segments) segment.remove();
|
|
561
672
|
this.#segments.length = 0;
|
|
562
|
-
this.#origin
|
|
563
|
-
this.#origin = null;
|
|
673
|
+
this.#origin.remove();
|
|
564
674
|
}
|
|
565
675
|
/**
|
|
566
|
-
*
|
|
567
|
-
*
|
|
676
|
+
* Slides the segments onto `rects`, one for one, and removes them once there; without as many
|
|
677
|
+
* rects, or under reduced motion, removes them at once. The layer is left empty for reuse.
|
|
568
678
|
*/
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
pointerEvents: "none",
|
|
578
|
-
position: "fixed",
|
|
579
|
-
top: "0",
|
|
580
|
-
visibility: "hidden",
|
|
581
|
-
width: "0"
|
|
582
|
-
});
|
|
583
|
-
this.#region.append(this.#origin);
|
|
679
|
+
settle(rects) {
|
|
680
|
+
const segments = this.#segments.splice(0);
|
|
681
|
+
const origin = this.#origin;
|
|
682
|
+
this.#origin = new OriginProbe(this.#region);
|
|
683
|
+
if (rects.length !== segments.length) {
|
|
684
|
+
for (const segment of segments) segment.remove();
|
|
685
|
+
origin.remove();
|
|
686
|
+
return;
|
|
584
687
|
}
|
|
585
|
-
|
|
688
|
+
const at = origin.measure();
|
|
689
|
+
settle(segments, (segment, index) => this.#place(segment, rects[index], at), () => origin.remove());
|
|
690
|
+
}
|
|
691
|
+
#place(segment, rect, origin) {
|
|
692
|
+
const { style } = segment;
|
|
693
|
+
const { height, left, top, visible, width } = rect;
|
|
694
|
+
const insets = [
|
|
695
|
+
visible.top - top,
|
|
696
|
+
left + width - visible.right,
|
|
697
|
+
top + height - visible.bottom,
|
|
698
|
+
visible.left - left
|
|
699
|
+
].map((inset) => Math.max(0, inset));
|
|
700
|
+
style.width = `${width}px`;
|
|
701
|
+
style.height = `${height}px`;
|
|
702
|
+
style.transform = `translate3d(${left - origin.left}px, ${top - origin.top}px, 0)`;
|
|
703
|
+
style.clipPath = insets.some((inset) => inset > 0) ? `inset(${insets.map((inset) => `${inset}px`).join(" ")})` : "";
|
|
586
704
|
}
|
|
587
705
|
#createSegment() {
|
|
588
706
|
const template = this.#template;
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
707
|
+
return ghostSegment(this.#region, template, this.#className, this.#kind === "landing" ? "landing" : template === null ? "range" : "event");
|
|
708
|
+
}
|
|
709
|
+
};
|
|
710
|
+
/**
|
|
711
|
+
* A see-through copy of `template`, or a range box without one, fixed over the view: the parts of
|
|
712
|
+
* every preview.
|
|
713
|
+
*/
|
|
714
|
+
function ghostSegment(region, template, className, kind) {
|
|
715
|
+
const segment = template === null ? document.createElement("div") : template.cloneNode(true);
|
|
716
|
+
for (const attribute of [
|
|
717
|
+
"data-dragging",
|
|
718
|
+
"data-event-id",
|
|
719
|
+
"data-event-id-type",
|
|
720
|
+
"id"
|
|
721
|
+
]) segment.removeAttribute(attribute);
|
|
722
|
+
segment.setAttribute("aria-hidden", "true");
|
|
723
|
+
segment.setAttribute("data-calendar-ghost", kind);
|
|
724
|
+
if (className !== void 0) segment.classList.add(...className.split(/\s+/).filter(Boolean));
|
|
725
|
+
Object.assign(segment.style, {
|
|
726
|
+
bottom: "auto",
|
|
727
|
+
boxSizing: "border-box",
|
|
728
|
+
left: "0",
|
|
729
|
+
margin: "0",
|
|
730
|
+
pointerEvents: "none",
|
|
731
|
+
position: "fixed",
|
|
732
|
+
right: "auto",
|
|
733
|
+
top: "0",
|
|
734
|
+
zIndex: kind === "landing" ? "calc(var(--calendar-ghost-z-index, 50) - 1)" : "var(--calendar-ghost-z-index, 50)"
|
|
735
|
+
});
|
|
736
|
+
Object.assign(segment.style, kind === "landing" ? {
|
|
737
|
+
background: "var(--calendar-ghost-landing-background, rgb(37 99 235 / 0.1))",
|
|
738
|
+
borderRadius: "var(--calendar-ghost-radius, 6px)",
|
|
739
|
+
boxShadow: "var(--calendar-ghost-landing-shadow, inset 0 0 0 1.5px rgb(37 99 235 / 0.45))"
|
|
740
|
+
} : template === null ? {
|
|
741
|
+
background: "var(--calendar-ghost-background, rgb(37 99 235 / 0.18))",
|
|
742
|
+
borderRadius: "var(--calendar-ghost-radius, 6px)"
|
|
743
|
+
} : {
|
|
744
|
+
boxShadow: "var(--calendar-ghost-shadow, 0 6px 16px rgb(0 0 0 / 0.18))",
|
|
745
|
+
opacity: "var(--calendar-ghost-opacity, 0.75)"
|
|
746
|
+
});
|
|
747
|
+
region.append(segment);
|
|
748
|
+
return segment;
|
|
749
|
+
}
|
|
750
|
+
/** The most of a long or wide event a copy that follows the pointer outside the view shows. */
|
|
751
|
+
const FLOATING_MAXIMUM = {
|
|
752
|
+
height: 120,
|
|
753
|
+
width: 320
|
|
754
|
+
};
|
|
755
|
+
function viewportBounds() {
|
|
756
|
+
return {
|
|
757
|
+
bottom: innerHeight,
|
|
758
|
+
left: 0,
|
|
759
|
+
right: innerWidth,
|
|
760
|
+
top: 0
|
|
761
|
+
};
|
|
762
|
+
}
|
|
763
|
+
/**
|
|
764
|
+
* A `clip-path` that hides what of `box` falls outside `bounds`. Insets are always given, and
|
|
765
|
+
* negative where nothing clips, so the clip transitions with the box and keeps the shadow.
|
|
766
|
+
*/
|
|
767
|
+
function clipTo({ height, left, top, width }, bounds) {
|
|
768
|
+
return `inset(${[
|
|
769
|
+
bounds.top - top,
|
|
770
|
+
left + width - bounds.right,
|
|
771
|
+
top + height - bounds.bottom,
|
|
772
|
+
bounds.left - left
|
|
773
|
+
].map((inset) => `${Math.max(-32, inset)}px`).join(" ")})`;
|
|
774
|
+
}
|
|
775
|
+
/**
|
|
776
|
+
* The copy of a moved event that a free drag (`preview: "free"`) moves with the pointer, pixel for
|
|
777
|
+
* pixel: the event's box at the press, carried by how far the pointer has gone since. Over the view
|
|
778
|
+
* it keeps the event's size and is clipped where the cell it would land in may paint, as scrolled
|
|
779
|
+
* content is; off the view it shrinks to at most `FLOATING_MAXIMUM` around the press, as the copy
|
|
780
|
+
* of a snapped drag does there. Its `[data-event-time]` shows the range a drop would propose;
|
|
781
|
+
* `data-outside` marks it where there's nothing to drop on.
|
|
782
|
+
*/
|
|
783
|
+
var GhostClone = class {
|
|
784
|
+
#bounds;
|
|
785
|
+
#box;
|
|
786
|
+
#element;
|
|
787
|
+
#floating;
|
|
788
|
+
#floats = false;
|
|
789
|
+
#home;
|
|
790
|
+
#label = null;
|
|
791
|
+
#origin;
|
|
792
|
+
#probe;
|
|
793
|
+
#press;
|
|
794
|
+
#point;
|
|
795
|
+
/** The time labels and what they said, restored where there's nothing to drop on. */
|
|
796
|
+
#times;
|
|
797
|
+
#validity = null;
|
|
798
|
+
#outside = false;
|
|
799
|
+
/** `visible` is where the event may paint at the press. */
|
|
800
|
+
constructor(region, template, className, press, visible) {
|
|
801
|
+
const rect = template.getBoundingClientRect();
|
|
802
|
+
this.#box = {
|
|
803
|
+
height: rect.height,
|
|
804
|
+
left: rect.left,
|
|
805
|
+
top: rect.top,
|
|
806
|
+
width: rect.width
|
|
807
|
+
};
|
|
808
|
+
const width = Math.min(rect.width, FLOATING_MAXIMUM.width);
|
|
809
|
+
const height = Math.min(rect.height, FLOATING_MAXIMUM.height);
|
|
810
|
+
this.#floating = {
|
|
811
|
+
height,
|
|
812
|
+
offset: {
|
|
813
|
+
x: Math.min(Math.max(press.x - rect.left, 0), width),
|
|
814
|
+
y: Math.min(Math.max(press.y - rect.top, 0), height)
|
|
815
|
+
},
|
|
816
|
+
width
|
|
817
|
+
};
|
|
818
|
+
this.#bounds = visible;
|
|
819
|
+
this.#home = visible;
|
|
820
|
+
this.#press = press;
|
|
821
|
+
this.#point = press;
|
|
822
|
+
this.#probe = new OriginProbe(region);
|
|
823
|
+
this.#origin = this.#probe.measure();
|
|
824
|
+
this.#element = ghostSegment(region, template, className, "event");
|
|
825
|
+
this.#resize(this.#box);
|
|
826
|
+
this.#element.style.clipPath = clipTo(this.#box, visible);
|
|
827
|
+
this.#times = [...this.#element.querySelectorAll("[data-event-time]")].map((time) => [time, time.textContent]);
|
|
828
|
+
}
|
|
829
|
+
/** The event's box at the press, and where it could paint then. */
|
|
830
|
+
get home() {
|
|
831
|
+
return {
|
|
832
|
+
...this.#box,
|
|
833
|
+
visible: this.#home
|
|
834
|
+
};
|
|
835
|
+
}
|
|
836
|
+
/** The copy's box now, in viewport coordinates. */
|
|
837
|
+
get rect() {
|
|
838
|
+
if (this.#floats) {
|
|
839
|
+
const { height, offset, width } = this.#floating;
|
|
840
|
+
return {
|
|
841
|
+
height,
|
|
842
|
+
left: this.#point.x - offset.x,
|
|
843
|
+
top: this.#point.y - offset.y,
|
|
844
|
+
width
|
|
845
|
+
};
|
|
846
|
+
}
|
|
847
|
+
return {
|
|
848
|
+
...this.#box,
|
|
849
|
+
left: this.#box.left + this.#point.x - this.#press.x,
|
|
850
|
+
top: this.#box.top + this.#point.y - this.#press.y
|
|
851
|
+
};
|
|
852
|
+
}
|
|
853
|
+
/** Clips the copy, from its next move, to where a cell's content may paint. */
|
|
854
|
+
clip(bounds) {
|
|
855
|
+
this.#bounds = bounds;
|
|
856
|
+
}
|
|
857
|
+
/**
|
|
858
|
+
* Shrinks the copy, from its next move, to at most `FLOATING_MAXIMUM` around the press, off the
|
|
859
|
+
* view or over a drop zone of the app's; `false` gives it the event's size again.
|
|
860
|
+
*/
|
|
861
|
+
float(floats) {
|
|
862
|
+
if (floats === this.#floats) return;
|
|
863
|
+
this.#floats = floats;
|
|
864
|
+
this.#resize(this.rect);
|
|
865
|
+
}
|
|
866
|
+
/** Puts the copy under the pointer: a transform and a clip. */
|
|
867
|
+
follow(point) {
|
|
868
|
+
this.#point = point;
|
|
869
|
+
const box = this.rect;
|
|
870
|
+
this.#element.style.transform = this.#translate(box);
|
|
871
|
+
this.#element.style.clipPath = clipTo(box, this.#floats ? viewportBounds() : this.#bounds);
|
|
872
|
+
}
|
|
873
|
+
/**
|
|
874
|
+
* Shows the range a drop would propose, or the event's own with `null`, and what the policies
|
|
875
|
+
* say; `outside` marks the copy where there's nothing to drop on.
|
|
876
|
+
*/
|
|
877
|
+
mark(label, validity, outside) {
|
|
878
|
+
if (label !== this.#label) {
|
|
879
|
+
this.#label = label;
|
|
880
|
+
for (const [time, text] of this.#times) time.textContent = label ?? text;
|
|
881
|
+
}
|
|
882
|
+
if (validity !== this.#validity) {
|
|
883
|
+
this.#validity = validity;
|
|
884
|
+
this.#element.toggleAttribute("data-invalid", validity === "invalid");
|
|
885
|
+
this.#element.toggleAttribute("data-warning", validity === "warning");
|
|
886
|
+
}
|
|
887
|
+
if (outside !== this.#outside) {
|
|
888
|
+
this.#outside = outside;
|
|
889
|
+
this.#element.toggleAttribute("data-outside", outside);
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
/** Finds the fixed-position origin again, after a scroll that may have carried it. */
|
|
893
|
+
measure() {
|
|
894
|
+
this.#origin = this.#probe.measure();
|
|
895
|
+
this.follow(this.#point);
|
|
896
|
+
}
|
|
897
|
+
/**
|
|
898
|
+
* Slides the copy onto `target`, its clip onto `target.visible`, and removes it there; at once
|
|
899
|
+
* without a target.
|
|
900
|
+
*/
|
|
901
|
+
settle(target) {
|
|
902
|
+
if (target === null) {
|
|
903
|
+
this.remove();
|
|
904
|
+
return;
|
|
905
|
+
}
|
|
906
|
+
settle([this.#element], ({ style }) => {
|
|
907
|
+
this.#resize(target);
|
|
908
|
+
style.transform = this.#translate(target);
|
|
909
|
+
style.clipPath = clipTo(target, target.visible);
|
|
910
|
+
}, () => this.#probe.remove());
|
|
911
|
+
}
|
|
912
|
+
remove() {
|
|
913
|
+
this.#element.remove();
|
|
914
|
+
this.#probe.remove();
|
|
915
|
+
}
|
|
916
|
+
#resize({ height, width }) {
|
|
917
|
+
this.#element.style.width = `${width}px`;
|
|
918
|
+
this.#element.style.height = `${height}px`;
|
|
919
|
+
}
|
|
920
|
+
#translate({ left, top }) {
|
|
921
|
+
return `translate3d(${left - this.#origin.left}px, ${top - this.#origin.top}px, 0)`;
|
|
619
922
|
}
|
|
620
923
|
};
|
|
621
924
|
// src/outcome.ts
|
|
@@ -722,11 +1025,6 @@ const DRAG_DISTANCE = 4;
|
|
|
722
1025
|
*/
|
|
723
1026
|
const LONG_PRESS_MS = 450;
|
|
724
1027
|
const TOUCH_TOLERANCE = 8;
|
|
725
|
-
/** The most of a long or wide event the copy that follows the pointer outside the view shows. */
|
|
726
|
-
const FLOATING_MAXIMUM = {
|
|
727
|
-
height: 120,
|
|
728
|
-
width: 320
|
|
729
|
-
};
|
|
730
1028
|
const noFollowers = Object.freeze([]);
|
|
731
1029
|
const pointerInput = Object.freeze({ input: "pointer" });
|
|
732
1030
|
function preventDefault(event) {
|
|
@@ -787,6 +1085,41 @@ function floatingRect({ height, offset, width }, point) {
|
|
|
787
1085
|
width
|
|
788
1086
|
};
|
|
789
1087
|
}
|
|
1088
|
+
function renderedBox(element) {
|
|
1089
|
+
const { height, left, top, width } = element.getBoundingClientRect();
|
|
1090
|
+
return {
|
|
1091
|
+
element,
|
|
1092
|
+
height,
|
|
1093
|
+
left,
|
|
1094
|
+
top,
|
|
1095
|
+
width
|
|
1096
|
+
};
|
|
1097
|
+
}
|
|
1098
|
+
/** Whether two boxes are the same to within half a pixel. */
|
|
1099
|
+
function sameBox(left, right) {
|
|
1100
|
+
return Math.abs(left.left - right.left) < .5 && Math.abs(left.top - right.top) < .5 && Math.abs(left.width - right.width) < .5 && Math.abs(left.height - right.height) < .5;
|
|
1101
|
+
}
|
|
1102
|
+
function center({ height, left, top, width }) {
|
|
1103
|
+
return {
|
|
1104
|
+
x: left + width / 2,
|
|
1105
|
+
y: top + height / 2
|
|
1106
|
+
};
|
|
1107
|
+
}
|
|
1108
|
+
/** The box nearest `near`, by their centres. */
|
|
1109
|
+
function nearest(boxes, near) {
|
|
1110
|
+
const { x, y } = center(near);
|
|
1111
|
+
let best = null;
|
|
1112
|
+
let distance = Infinity;
|
|
1113
|
+
for (const box of boxes) {
|
|
1114
|
+
const point = center(box);
|
|
1115
|
+
const next = Math.hypot(point.x - x, point.y - y);
|
|
1116
|
+
if (next < distance) {
|
|
1117
|
+
best = box;
|
|
1118
|
+
distance = next;
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
return best;
|
|
1122
|
+
}
|
|
790
1123
|
/**
|
|
791
1124
|
* The event an element renders. Without `data-event-id-type`, an ID that only exists as a number
|
|
792
1125
|
* still resolves, which keeps all-numeric calendars working with plain `data-event-id`.
|
|
@@ -823,6 +1156,20 @@ function resolveEvent(events, element) {
|
|
|
823
1156
|
*/
|
|
824
1157
|
function attachCalendarPointer(region, getOptions) {
|
|
825
1158
|
let session = null;
|
|
1159
|
+
/**
|
|
1160
|
+
* The last touch's pressed element, which keeps its `touchmove` listener until the finger
|
|
1161
|
+
* lifts. The gesture ends as the page starts scrolling under the finger, and in Chromium's touch
|
|
1162
|
+
* emulation, a change to the page's touch listeners then could stop that scroll after its first
|
|
1163
|
+
* step. So the region hears the lift through listeners it has from the start.
|
|
1164
|
+
*/
|
|
1165
|
+
let lastTouch = null;
|
|
1166
|
+
function releaseLastTouch() {
|
|
1167
|
+
lastTouch?.removeEventListener("touchmove", onTouchMove);
|
|
1168
|
+
lastTouch = null;
|
|
1169
|
+
}
|
|
1170
|
+
function onTouchLift(event) {
|
|
1171
|
+
if ((event.touches?.length ?? 0) === 0) releaseLastTouch();
|
|
1172
|
+
}
|
|
826
1173
|
function placementAt(current, target) {
|
|
827
1174
|
const { gesture, options } = current;
|
|
828
1175
|
if (gesture.kind === "create") return createdPlacement(gesture.anchor, target, options);
|
|
@@ -883,7 +1230,8 @@ function attachCalendarPointer(region, getOptions) {
|
|
|
883
1230
|
const changed = current.placement !== null;
|
|
884
1231
|
current.placement = null;
|
|
885
1232
|
current.check = null;
|
|
886
|
-
showOutside(current);
|
|
1233
|
+
if (current.free === void 0) showOutside(current);
|
|
1234
|
+
else followOutside(current, current.free);
|
|
887
1235
|
if (changed) {
|
|
888
1236
|
getOptions().onPreview?.(null);
|
|
889
1237
|
show(current, () => null);
|
|
@@ -891,7 +1239,10 @@ function attachCalendarPointer(region, getOptions) {
|
|
|
891
1239
|
return changed;
|
|
892
1240
|
}
|
|
893
1241
|
const changed = current.placement === null || !samePlacement(current.placement, placement);
|
|
894
|
-
if (!scrolled && !changed)
|
|
1242
|
+
if (!scrolled && !changed) {
|
|
1243
|
+
if (current.free !== void 0) drawFree(current, current.free, target, false);
|
|
1244
|
+
return false;
|
|
1245
|
+
}
|
|
895
1246
|
current.placement = placement;
|
|
896
1247
|
if (changed) {
|
|
897
1248
|
const proposed = proposal(current);
|
|
@@ -906,7 +1257,16 @@ function attachCalendarPointer(region, getOptions) {
|
|
|
906
1257
|
const template = layout.axis === "time" && gesture.kind !== "create" ? gesture.element : null;
|
|
907
1258
|
const label = template === null ? null : current.announcer?.formatRange(placement) ?? formatPlacementRange(placement, { timeZone: current.options.timeZone });
|
|
908
1259
|
const validity = ghostValidity(current.check);
|
|
909
|
-
current.ghost.render(layout.rects, template, label, validity);
|
|
1260
|
+
if (current.free === void 0) current.ghost.render(layout.rects, template, label, validity);
|
|
1261
|
+
else {
|
|
1262
|
+
Object.assign(current.free, {
|
|
1263
|
+
label,
|
|
1264
|
+
layout,
|
|
1265
|
+
validity
|
|
1266
|
+
});
|
|
1267
|
+
if (scrolled) current.free.clone?.measure();
|
|
1268
|
+
drawFree(current, current.free, target, true);
|
|
1269
|
+
}
|
|
910
1270
|
show(current, () => ({
|
|
911
1271
|
label,
|
|
912
1272
|
rects: layout.rects.map(viewportRect),
|
|
@@ -914,6 +1274,126 @@ function attachCalendarPointer(region, getOptions) {
|
|
|
914
1274
|
}));
|
|
915
1275
|
return changed;
|
|
916
1276
|
}
|
|
1277
|
+
/**
|
|
1278
|
+
* Draws a free drag (`preview: "free"`) over `target`: a moved event's copy under the pointer
|
|
1279
|
+
* over the landing boxes, or a resize's or create's edge under the pointer on a time axis.
|
|
1280
|
+
* `laidOut` says the snapped preview was laid out again; when it wasn't, a move only moves its
|
|
1281
|
+
* copy.
|
|
1282
|
+
*/
|
|
1283
|
+
function drawFree(current, free, target, laidOut) {
|
|
1284
|
+
const { gesture } = current;
|
|
1285
|
+
const { label, layout, validity } = free;
|
|
1286
|
+
if (layout === null) return;
|
|
1287
|
+
if (gesture.kind === "move") {
|
|
1288
|
+
const { clone } = free;
|
|
1289
|
+
if (clone === void 0) return;
|
|
1290
|
+
clone.float(false);
|
|
1291
|
+
if (laidOut) {
|
|
1292
|
+
free.marker.render(layout.rects, null, null, validity);
|
|
1293
|
+
clone.mark(label, validity, false);
|
|
1294
|
+
const landing = nearest(layout.rects, clone.rect);
|
|
1295
|
+
if (landing !== null) clone.clip(landing.visible);
|
|
1296
|
+
}
|
|
1297
|
+
clone.follow(current.point);
|
|
1298
|
+
return;
|
|
1299
|
+
}
|
|
1300
|
+
const template = layout.axis === "time" && gesture.kind === "resize" ? gesture.element : null;
|
|
1301
|
+
const followed = layout.axis !== "time" ? null : gesture.kind === "create" ? followedCreate(gesture.anchor, target, current.options) : followedResize(gesture.original, gesture.edge, gesture.grab, target, current.options);
|
|
1302
|
+
if (followed === null) {
|
|
1303
|
+
free.marker.clear();
|
|
1304
|
+
if (laidOut) current.ghost.render(layout.rects, template, label, validity);
|
|
1305
|
+
return;
|
|
1306
|
+
}
|
|
1307
|
+
if (laidOut) free.marker.render(layout.rects, null, null, validity);
|
|
1308
|
+
const drawn = ghostLayout(current.surface, followed, current.options.timeZone);
|
|
1309
|
+
current.ghost.render(drawn.rects, template, label, validity);
|
|
1310
|
+
}
|
|
1311
|
+
/**
|
|
1312
|
+
* Where a free drag has nothing to drop on: no landing, and a moved event's copy marked so. Off
|
|
1313
|
+
* the view, or over a drop zone of the app's, the copy shrinks as a snapped drag's does; over a
|
|
1314
|
+
* header of the view it keeps its size and its last cell's clip.
|
|
1315
|
+
*/
|
|
1316
|
+
function followOutside(current, free) {
|
|
1317
|
+
free.marker.clear();
|
|
1318
|
+
current.ghost.clear();
|
|
1319
|
+
const { clone } = free;
|
|
1320
|
+
if (clone === void 0) return;
|
|
1321
|
+
clone.mark(null, null, true);
|
|
1322
|
+
clone.float(current.claimed || !overView(current.point));
|
|
1323
|
+
clone.follow(current.point);
|
|
1324
|
+
}
|
|
1325
|
+
/** Where the view renders the event now: a box per segment it shows. */
|
|
1326
|
+
function renderedBoxes(id) {
|
|
1327
|
+
const boxes = [];
|
|
1328
|
+
for (const element of region.querySelectorAll("[data-event-id]")) {
|
|
1329
|
+
if (String(eventIdOf(element)) !== String(id)) continue;
|
|
1330
|
+
const box = renderedBox(element);
|
|
1331
|
+
if (box.width > 0 || box.height > 0) boxes.push(box);
|
|
1332
|
+
}
|
|
1333
|
+
return boxes;
|
|
1334
|
+
}
|
|
1335
|
+
/** A rendered box as a target to settle on, clipped where its element may paint. */
|
|
1336
|
+
function settleTarget(current, box) {
|
|
1337
|
+
if (box === null) return null;
|
|
1338
|
+
const { element, ...rest } = box;
|
|
1339
|
+
return {
|
|
1340
|
+
...rest,
|
|
1341
|
+
visible: visibleOf(current.surface, element)
|
|
1342
|
+
};
|
|
1343
|
+
}
|
|
1344
|
+
/**
|
|
1345
|
+
* Ends a free drag's preview: `"drop"` slides it to where the drop lands, `"home"` back to the
|
|
1346
|
+
* event, and `null` removes it, as for a drop zone of the app's. A drop waits a frame, for a
|
|
1347
|
+
* refusal the policies or the app give at once and for the view to render the change.
|
|
1348
|
+
*/
|
|
1349
|
+
function settleFree(current, free, to) {
|
|
1350
|
+
free.marker.clear();
|
|
1351
|
+
const { gesture } = current;
|
|
1352
|
+
const refused = () => free.refused || free.validity === "invalid";
|
|
1353
|
+
if (gesture.kind !== "move") {
|
|
1354
|
+
const { layout } = free;
|
|
1355
|
+
if (to !== "drop" || layout === null || refused()) {
|
|
1356
|
+
current.ghost.clear();
|
|
1357
|
+
return;
|
|
1358
|
+
}
|
|
1359
|
+
requestAnimationFrame(() => {
|
|
1360
|
+
if (refused()) current.ghost.clear();
|
|
1361
|
+
else current.ghost.settle(layout.rects);
|
|
1362
|
+
});
|
|
1363
|
+
return;
|
|
1364
|
+
}
|
|
1365
|
+
const { clone } = free;
|
|
1366
|
+
if (clone === void 0) return;
|
|
1367
|
+
if (to === null || prefersReducedMotion()) {
|
|
1368
|
+
clone.remove();
|
|
1369
|
+
return;
|
|
1370
|
+
}
|
|
1371
|
+
const from = clone.rect;
|
|
1372
|
+
const { id } = gesture.event;
|
|
1373
|
+
if (to === "home") {
|
|
1374
|
+
clone.settle(gesture.element.isConnected ? settleTarget(current, renderedBox(gesture.element)) : settleTarget(current, nearest(renderedBoxes(id), from)));
|
|
1375
|
+
return;
|
|
1376
|
+
}
|
|
1377
|
+
const { layout } = free;
|
|
1378
|
+
const landing = layout === null ? null : nearest(layout.rects, from);
|
|
1379
|
+
const fallback = landing === null ? null : {
|
|
1380
|
+
height: layout.axis === "time" ? landing.height : Math.min(from.height, landing.height),
|
|
1381
|
+
left: landing.left,
|
|
1382
|
+
top: landing.top,
|
|
1383
|
+
visible: landing.visible,
|
|
1384
|
+
width: Math.min(from.width, landing.width)
|
|
1385
|
+
};
|
|
1386
|
+
const before = renderedBoxes(id);
|
|
1387
|
+
requestAnimationFrame(() => {
|
|
1388
|
+
const after = renderedBoxes(id);
|
|
1389
|
+
if (refused()) {
|
|
1390
|
+
clone.settle(settleTarget(current, nearest(after, from)) ?? clone.home);
|
|
1391
|
+
return;
|
|
1392
|
+
}
|
|
1393
|
+
const moved = after.filter((box) => !before.some((old) => sameBox(old, box)));
|
|
1394
|
+
clone.settle(settleTarget(current, nearest(moved, from)) ?? fallback);
|
|
1395
|
+
});
|
|
1396
|
+
}
|
|
917
1397
|
/** Tells the followers what the preview shows now. */
|
|
918
1398
|
function show(current, shown) {
|
|
919
1399
|
if (current.followers.length === 0) return;
|
|
@@ -946,7 +1426,10 @@ function attachCalendarPointer(region, getOptions) {
|
|
|
946
1426
|
region.setAttribute("data-interaction", current.gesture.kind);
|
|
947
1427
|
if (current.gesture.kind !== "create") current.gesture.element.setAttribute("data-dragging", "");
|
|
948
1428
|
getSelection()?.removeAllRanges();
|
|
949
|
-
if (current.gesture.kind === "move")
|
|
1429
|
+
if (current.gesture.kind === "move") {
|
|
1430
|
+
follow(current, current.gesture);
|
|
1431
|
+
if (current.free !== void 0) current.free.clone = new GhostClone(region, current.gesture.element, current.free.className, current.origin, visibleOf(current.surface, current.gesture.element));
|
|
1432
|
+
}
|
|
950
1433
|
trackEdges(current);
|
|
951
1434
|
current.frame = requestAnimationFrame(scrollFrame);
|
|
952
1435
|
preview(current);
|
|
@@ -963,34 +1446,39 @@ function attachCalendarPointer(region, getOptions) {
|
|
|
963
1446
|
}
|
|
964
1447
|
});
|
|
965
1448
|
current.zones = current.followers.some((follower) => follower.claims !== void 0);
|
|
966
|
-
if (current.zones) current.floating = floatingOf(element, current.origin);
|
|
1449
|
+
if (current.zones && current.free === void 0) current.floating = floatingOf(element, current.origin);
|
|
967
1450
|
tellFollowers(current.followers, (follower) => follower.move?.(current.point));
|
|
968
1451
|
}
|
|
969
1452
|
/**
|
|
970
1453
|
* Ends the gesture; `dropped` tells its followers a drop zone of theirs took it, and `landing`
|
|
971
|
-
* what the preview showed where a release in the view proposed the move.
|
|
1454
|
+
* what the preview showed where a release in the view proposed the move. `settle` says where a
|
|
1455
|
+
* free drag's preview goes: to the drop it proposed, or back to the event.
|
|
972
1456
|
*/
|
|
973
|
-
function end(dropped = false, landing = null) {
|
|
1457
|
+
function end(dropped = false, landing = null, settle = "home") {
|
|
974
1458
|
const current = session;
|
|
975
1459
|
if (current === null) return;
|
|
976
1460
|
session = null;
|
|
977
1461
|
if (current.placement !== null) getOptions().onPreview?.(null);
|
|
978
1462
|
clearTimeout(current.longPress);
|
|
979
1463
|
if (current.frame !== void 0) cancelAnimationFrame(current.frame);
|
|
980
|
-
current.ghost.clear();
|
|
981
1464
|
region.removeAttribute("data-interaction");
|
|
982
1465
|
if (current.gesture.kind !== "create") current.gesture.element.removeAttribute("data-dragging");
|
|
983
1466
|
window.removeEventListener("pointermove", onPointerMove);
|
|
984
1467
|
window.removeEventListener("pointerup", onPointerUp);
|
|
985
1468
|
window.removeEventListener("pointercancel", onPointerCancel);
|
|
986
1469
|
window.removeEventListener("keydown", onKeyDown, true);
|
|
987
|
-
current.touchTarget
|
|
1470
|
+
lastTouch = current.touchTarget;
|
|
988
1471
|
for (const type of [
|
|
989
1472
|
"contextmenu",
|
|
990
1473
|
"dragstart",
|
|
991
1474
|
"selectstart"
|
|
992
1475
|
]) window.removeEventListener(type, preventDefault);
|
|
993
|
-
|
|
1476
|
+
try {
|
|
1477
|
+
if (current.free === void 0) current.ghost.clear();
|
|
1478
|
+
else settleFree(current, current.free, dropped ? null : settle);
|
|
1479
|
+
} finally {
|
|
1480
|
+
tellFollowers(current.followers, (follower) => follower.end?.(dropped, landing));
|
|
1481
|
+
}
|
|
994
1482
|
}
|
|
995
1483
|
/** Ends the gesture without proposing anything. */
|
|
996
1484
|
function cancel() {
|
|
@@ -1052,13 +1540,16 @@ function attachCalendarPointer(region, getOptions) {
|
|
|
1052
1540
|
return;
|
|
1053
1541
|
}
|
|
1054
1542
|
const proposed = proposal(current);
|
|
1055
|
-
end(false, proposed === null ? null : current.shown);
|
|
1543
|
+
end(false, proposed === null ? null : current.shown, proposed === null ? "home" : "drop");
|
|
1056
1544
|
suppressNextClick();
|
|
1057
1545
|
if (proposed === null) {
|
|
1058
1546
|
announce(current, { phase: "cancel" });
|
|
1059
1547
|
return;
|
|
1060
1548
|
}
|
|
1061
|
-
settleProposal(getOptions().onPropose(proposed, pointerInput), current.placement, (outcome) =>
|
|
1549
|
+
settleProposal(getOptions().onPropose(proposed, pointerInput), current.placement, (outcome) => {
|
|
1550
|
+
if (outcome.phase === "reject" && current.free !== void 0) current.free.refused = true;
|
|
1551
|
+
announce(current, outcome);
|
|
1552
|
+
});
|
|
1062
1553
|
}
|
|
1063
1554
|
function onKeyDown(event) {
|
|
1064
1555
|
if (event.key !== "Escape" || session === null) return;
|
|
@@ -1096,6 +1587,7 @@ function attachCalendarPointer(region, getOptions) {
|
|
|
1096
1587
|
}
|
|
1097
1588
|
function onPointerDown(event) {
|
|
1098
1589
|
if (session !== null || event.button !== 0 || !event.isPrimary) return;
|
|
1590
|
+
if (lastTouch instanceof Node && !region.contains(lastTouch)) releaseLastTouch();
|
|
1099
1591
|
const options = getOptions();
|
|
1100
1592
|
if (options.disabled === true || !(event.target instanceof Element)) return;
|
|
1101
1593
|
const surface = {
|
|
@@ -1114,6 +1606,14 @@ function attachCalendarPointer(region, getOptions) {
|
|
|
1114
1606
|
claimed: false,
|
|
1115
1607
|
edges: new EdgeArming(true),
|
|
1116
1608
|
followers: noFollowers,
|
|
1609
|
+
free: options.preview === "free" ? {
|
|
1610
|
+
className: options.ghostClassName,
|
|
1611
|
+
label: null,
|
|
1612
|
+
layout: null,
|
|
1613
|
+
marker: new GhostLayer(region, options.ghostClassName, "landing"),
|
|
1614
|
+
refused: false,
|
|
1615
|
+
validity: null
|
|
1616
|
+
} : void 0,
|
|
1117
1617
|
gesture,
|
|
1118
1618
|
ghost: new GhostLayer(region, options.ghostClassName),
|
|
1119
1619
|
options: {
|
|
@@ -1164,7 +1664,11 @@ function attachCalendarPointer(region, getOptions) {
|
|
|
1164
1664
|
if (session?.active === true) event.preventDefault();
|
|
1165
1665
|
}
|
|
1166
1666
|
region.addEventListener("pointerdown", onPointerDown);
|
|
1167
|
-
if (getOptions().disabled !== true)
|
|
1667
|
+
if (getOptions().disabled !== true) {
|
|
1668
|
+
region.addEventListener("touchmove", onTouchMove, { passive: false });
|
|
1669
|
+
region.addEventListener("touchend", onTouchLift, { passive: true });
|
|
1670
|
+
region.addEventListener("touchcancel", onTouchLift, { passive: true });
|
|
1671
|
+
}
|
|
1168
1672
|
const unregister = registerRegion({
|
|
1169
1673
|
element: region,
|
|
1170
1674
|
getOptions
|
|
@@ -1173,6 +1677,9 @@ function attachCalendarPointer(region, getOptions) {
|
|
|
1173
1677
|
cancel();
|
|
1174
1678
|
region.removeEventListener("pointerdown", onPointerDown);
|
|
1175
1679
|
region.removeEventListener("touchmove", onTouchMove);
|
|
1680
|
+
region.removeEventListener("touchend", onTouchLift);
|
|
1681
|
+
region.removeEventListener("touchcancel", onTouchLift);
|
|
1682
|
+
releaseLastTouch();
|
|
1176
1683
|
unregister();
|
|
1177
1684
|
};
|
|
1178
1685
|
}
|
|
@@ -1211,7 +1718,7 @@ function regionOver(region, hit, eventId) {
|
|
|
1211
1718
|
/**
|
|
1212
1719
|
* Follows a drag that started outside the calendar, such as a task dragged from a list, over the
|
|
1213
1720
|
* calendar views on the page, and drops it into one: the framework-agnostic half of external drag
|
|
1214
|
-
* and drop, which a drag library's binding drives. Every view that uses `
|
|
1721
|
+
* and drop, which a drag library's binding drives. Every view that uses `attachCalendarPointer` takes
|
|
1215
1722
|
* part, through the options it passes: the preview shows range boxes in its slots, its `check`
|
|
1216
1723
|
* marks them `data-invalid` or `data-warning`, its announcer speaks each step with `outside` set,
|
|
1217
1724
|
* and its `onPropose` gets the drop. Read-only views take no part.
|
|
@@ -1401,21 +1908,4 @@ function startCalendarDrop(source, { input = "pointer" } = {}) {
|
|
|
1401
1908
|
}
|
|
1402
1909
|
};
|
|
1403
1910
|
}
|
|
1404
|
-
|
|
1405
|
-
/**
|
|
1406
|
-
* Pointer move, resize, and create for a view region; see `attachCalendarPointer` for the data
|
|
1407
|
-
* attributes the view renders. Returns a ref for the region element. Options are read when a
|
|
1408
|
-
* gesture starts, and `columns` again as it moves, so changing them never re-attaches listeners,
|
|
1409
|
-
* and dragging never re-renders. `disabled` is the exception: a disabled region doesn't listen for
|
|
1410
|
-
* touch moves, so a read-only calendar's swipes never wait for its listener, and changing it
|
|
1411
|
-
* attaches the listeners again.
|
|
1412
|
-
*/
|
|
1413
|
-
function useCalendarPointer(options) {
|
|
1414
|
-
const latest = useRef(options);
|
|
1415
|
-
useInsertionEffect(() => {
|
|
1416
|
-
latest.current = options;
|
|
1417
|
-
});
|
|
1418
|
-
const disabled = options.disabled === true;
|
|
1419
|
-
return useCallback((element) => element === null ? void 0 : attachCalendarPointer(element, () => latest.current), [disabled]);
|
|
1420
|
-
}
|
|
1421
|
-
export { attachCalendarPointer, eventIdAttributes, eventIdOf, monitorCalendarDrags, observeCalendarRegions, resourceIdAttributes, resourceIdOf, startCalendarDrop, useCalendarPointer };
|
|
1911
|
+
export { attachCalendarPointer, eventIdAttributes, eventIdOf, monitorCalendarDrags, observeCalendarRegions, resourceIdAttributes, resourceIdOf, startCalendarDrop };
|
package/package.json
CHANGED
|
@@ -1,19 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moltenship/calendar-dnd",
|
|
3
|
-
"version": "2.0.0
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Pointer drag, resize, and create interactions for Moltenship Time views.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"calendar",
|
|
7
7
|
"drag-and-drop",
|
|
8
8
|
"event-calendar",
|
|
9
9
|
"pointer-events",
|
|
10
|
-
"react",
|
|
11
10
|
"resize",
|
|
12
11
|
"scheduler",
|
|
13
12
|
"temporal",
|
|
14
13
|
"typescript"
|
|
15
14
|
],
|
|
16
|
-
"homepage": "https://time.molten.sh/docs/interactions",
|
|
15
|
+
"homepage": "https://time.molten.sh/docs/react/interactions",
|
|
17
16
|
"bugs": {
|
|
18
17
|
"url": "https://github.com/Moltenship/time/issues"
|
|
19
18
|
},
|
|
@@ -39,22 +38,15 @@
|
|
|
39
38
|
"access": "public"
|
|
40
39
|
},
|
|
41
40
|
"dependencies": {
|
|
42
|
-
"@moltenship/calendar-core": "2.0.0
|
|
43
|
-
"@moltenship/calendar-temporal": "2.0.0
|
|
41
|
+
"@moltenship/calendar-core": "2.0.0",
|
|
42
|
+
"@moltenship/calendar-temporal": "2.0.0"
|
|
44
43
|
},
|
|
45
44
|
"devDependencies": {
|
|
46
|
-
"@types/react": "19.3.0",
|
|
47
|
-
"@types/react-dom": "19.3.0",
|
|
48
45
|
"jsdom": "30.1.3",
|
|
49
|
-
"react": "19.3.1",
|
|
50
|
-
"react-dom": "19.3.1",
|
|
51
46
|
"typescript": "^7.0.2",
|
|
52
47
|
"vite": "npm:@voidzero-dev/vite-plus-core@1.4.0",
|
|
53
48
|
"vite-plus": "1.4.0"
|
|
54
49
|
},
|
|
55
|
-
"peerDependencies": {
|
|
56
|
-
"react": "^19.0.0"
|
|
57
|
-
},
|
|
58
50
|
"scripts": {
|
|
59
51
|
"build": "vp pack",
|
|
60
52
|
"dev": "vp pack --watch",
|