@moltenship/calendar-dnd-kit 0.0.0-stage → 1.0.0-rc.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/LICENSE +21 -0
- package/README.md +50 -2
- package/dist/index.d.mts +77 -0
- package/dist/index.mjs +399 -0
- package/package.json +54 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Timofey Maximov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,51 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @moltenship/calendar-dnd-kit
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
An optional [dnd-kit](https://dndkit.com) adapter for Moltenship Time. Apps that already use
|
|
4
|
+
dnd-kit drag their own items into the calendar, such as unscheduled tasks dropped on a time slot,
|
|
5
|
+
a day or a resource, and drag calendar events out to their own droppables, such as an
|
|
6
|
+
"unscheduled" list. Drops go through the calendar's hit-testing, snapping, policies and
|
|
7
|
+
`onEventChange`, like any drag. The calendar's own drags stay on native Pointer Events.
|
|
8
|
+
|
|
9
|
+
```tsx
|
|
10
|
+
import { DragDropProvider, useDraggable } from "@dnd-kit/react";
|
|
11
|
+
import { CalendarDndKit, calendarDropData } from "@moltenship/calendar-dnd-kit";
|
|
12
|
+
|
|
13
|
+
<DragDropProvider>
|
|
14
|
+
<CalendarDndKit />
|
|
15
|
+
<TaskList />
|
|
16
|
+
<Calendar.Root …>…</Calendar.Root>
|
|
17
|
+
</DragDropProvider>;
|
|
18
|
+
|
|
19
|
+
function TaskCard({ task }: { task: Task }) {
|
|
20
|
+
const { ref } = useDraggable({
|
|
21
|
+
id: task.id,
|
|
22
|
+
data: { task, ...calendarDropData({ event: toMeeting(task), duration: { minutes: 30 } }) },
|
|
23
|
+
});
|
|
24
|
+
return <li ref={ref}>{task.title}</li>;
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Install
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pnpm add @moltenship/calendar @moltenship/calendar-dnd-kit @dnd-kit/react @dnd-kit/dom react react-dom
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
- dnd-kit's `@dnd-kit/react` and `@dnd-kit/dom` 0.5. They are peer dependencies, and still pre-1.0,
|
|
35
|
+
so this package takes 0.5 only.
|
|
36
|
+
- React 19. React is a peer dependency.
|
|
37
|
+
- A runtime with `Temporal` (Node.js 26 and later include it), or a polyfill that defines
|
|
38
|
+
`globalThis.Temporal`, loaded first.
|
|
39
|
+
- TypeScript 6 or later with `"lib": ["DOM", "DOM.Iterable", "ESNext"]`.
|
|
40
|
+
|
|
41
|
+
`@moltenship/calendar` doesn't re-export this package, so the calendar's install and bundles carry
|
|
42
|
+
no dnd-kit.
|
|
43
|
+
|
|
44
|
+
## Documentation
|
|
45
|
+
|
|
46
|
+
- [dnd-kit adapter](https://time.molten.sh/docs/api/dnd-kit)
|
|
47
|
+
- [Repository](https://github.com/Moltenship/time)
|
|
48
|
+
|
|
49
|
+
## License
|
|
50
|
+
|
|
51
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { DragDropManager } from "@dnd-kit/dom";
|
|
2
|
+
import { CalendarEventPlacement, EventId, ResourceId } from "@moltenship/calendar-core";
|
|
3
|
+
import { CalendarDropSource } from "@moltenship/calendar-dnd";
|
|
4
|
+
//#region src/calendar-dnd-kit.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Connects the calendar views on the page to the dnd-kit `DragDropProvider` it's rendered in, so
|
|
7
|
+
* draggables carrying `calendarDropData` drop into them and calendar events drag out to your
|
|
8
|
+
* droppables. Render one inside the provider, anywhere; it renders nothing, and a second one
|
|
9
|
+
* shares the first's connection. Outside a provider, dnd-kit hands it a default manager that no
|
|
10
|
+
* droppable of yours is on. See `connectCalendarDragDrop`.
|
|
11
|
+
*/
|
|
12
|
+
export declare function CalendarDndKit(): null;
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/connect.d.ts
|
|
15
|
+
/**
|
|
16
|
+
* Connects the calendar views on the page to a dnd-kit manager, and returns a function that
|
|
17
|
+
* disconnects them. `CalendarDndKit` does this for the `DragDropProvider` it's in; call it
|
|
18
|
+
* yourself with a `DragDropManager` from `@dnd-kit/dom`. Connecting a manager again shares the
|
|
19
|
+
* first connection, which lasts until every call has disconnected.
|
|
20
|
+
*
|
|
21
|
+
* - Each view's region becomes a droppable, disabled while the view is read-only, that takes
|
|
22
|
+
* only draggables carrying `calendarDropData` and calendar events. Its ID is opaque: tell it by
|
|
23
|
+
* `isCalendarTarget`. It wins where the view is the topmost thing under the pointer, and a
|
|
24
|
+
* droppable of yours over the view wins there.
|
|
25
|
+
* - A draggable carrying `calendarDropData` previews in the view under it and proposes there when
|
|
26
|
+
* dropped, through the view's own policies, `onEventChange` and announcer. Over a droppable of
|
|
27
|
+
* yours it shows nothing, and dropped there it proposes nothing.
|
|
28
|
+
* - An event a view moves becomes a dnd-kit drag of type `calendarEventType` once the pointer
|
|
29
|
+
* leaves the view, so your droppables see it like any drag; moves that stay in the view never
|
|
30
|
+
* reach dnd-kit. Released over one of yours, the drag ends as a drop and the calendar proposes
|
|
31
|
+
* nothing; any other end, such as a drop back in the calendar or Escape, ends it as `canceled`.
|
|
32
|
+
* The drag needs dnd-kit's Feedback plugin, one of its defaults, which measures its shape.
|
|
33
|
+
* - dnd-kit's `AutoScroller` is off while a drag is over a view, which scrolls itself.
|
|
34
|
+
*/
|
|
35
|
+
export declare function connectCalendarDragDrop(manager: DragDropManager): () => void;
|
|
36
|
+
//#endregion
|
|
37
|
+
//#region src/data.d.ts
|
|
38
|
+
declare const dropKey = "@moltenship/calendar-drop";
|
|
39
|
+
/**
|
|
40
|
+
* The `type` of the draggable a calendar event becomes while it's dragged out of a view; list it
|
|
41
|
+
* in a droppable's `accept` to take calendar events.
|
|
42
|
+
*/
|
|
43
|
+
export declare const calendarEventType = "moltenship-calendar-event";
|
|
44
|
+
/** A dnd-kit entity, or anything else with `data`, such as `operation.source`. */
|
|
45
|
+
type WithData = {
|
|
46
|
+
readonly data?: unknown;
|
|
47
|
+
} | null | undefined;
|
|
48
|
+
/** What a calendar event carries while it's dragged out of a view. */
|
|
49
|
+
interface CalendarEventDragData<TEvent = unknown, TEventId extends EventId = EventId, TResourceId extends ResourceId = ResourceId> {
|
|
50
|
+
/** The application's record. */
|
|
51
|
+
readonly event: TEvent;
|
|
52
|
+
readonly eventId: TEventId;
|
|
53
|
+
/** Where the event is in the calendar. */
|
|
54
|
+
readonly placement: CalendarEventPlacement<TResourceId>;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Data that makes a draggable an item the calendar takes: spread it into `useDraggable`'s `data`,
|
|
58
|
+
* beside your own. Dropped on a view, it proposes a create carrying `source.event`, `duration` long
|
|
59
|
+
* over a time column or `days` long over day cells, or with `eventId` a move.
|
|
60
|
+
*/
|
|
61
|
+
export declare function calendarDropData<TEvent, TEventId extends EventId = EventId>(source: CalendarDropSource<TEvent, TEventId>): {
|
|
62
|
+
readonly [dropKey]: CalendarDropSource<TEvent, TEventId>;
|
|
63
|
+
};
|
|
64
|
+
/** The calendar drop a draggable carries (`calendarDropData`), or `null` without one. */
|
|
65
|
+
export declare function calendarDropOf<TEvent = unknown, TEventId extends EventId = EventId>(source: WithData): CalendarDropSource<TEvent, TEventId> | null;
|
|
66
|
+
/**
|
|
67
|
+
* The calendar event a draggable is, while one is dragged out of a view to a droppable of yours;
|
|
68
|
+
* `null` for any other draggable. Read it in `onDragEnd` from `operation.source`.
|
|
69
|
+
*/
|
|
70
|
+
export declare function calendarEventOf<TEvent = unknown, TEventId extends EventId = EventId, TResourceId extends ResourceId = ResourceId>(source: WithData): CalendarEventDragData<TEvent, TEventId, TResourceId> | null;
|
|
71
|
+
/**
|
|
72
|
+
* Whether a droppable is a calendar view's: the adapter handles drops there itself, so an
|
|
73
|
+
* `onDragEnd` of yours can leave `operation.target` alone when this says so.
|
|
74
|
+
*/
|
|
75
|
+
export declare function isCalendarTarget(target: WithData): boolean;
|
|
76
|
+
//#endregion
|
|
77
|
+
export type { CalendarEventDragData };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useDragDropManager } from "@dnd-kit/react";
|
|
3
|
+
import { useEffect } from "react";
|
|
4
|
+
import { AutoScroller, Draggable, Droppable, Feedback } from "@dnd-kit/dom";
|
|
5
|
+
import { eventPlacement } from "@moltenship/calendar-core";
|
|
6
|
+
import { monitorCalendarDrags, observeCalendarRegions, startCalendarDrop } from "@moltenship/calendar-dnd";
|
|
7
|
+
//#region src/data.ts
|
|
8
|
+
const dropKey = "@moltenship/calendar-drop";
|
|
9
|
+
const eventKey = "@moltenship/calendar-event";
|
|
10
|
+
const regionKey = "@moltenship/calendar-view";
|
|
11
|
+
/**
|
|
12
|
+
* The `type` of the draggable a calendar event becomes while it's dragged out of a view; list it
|
|
13
|
+
* in a droppable's `accept` to take calendar events.
|
|
14
|
+
*/
|
|
15
|
+
const calendarEventType = "moltenship-calendar-event";
|
|
16
|
+
function field(entity, key) {
|
|
17
|
+
const data = entity?.data;
|
|
18
|
+
return typeof data === "object" && data !== null ? data[key] : void 0;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Data that makes a draggable an item the calendar takes: spread it into `useDraggable`'s `data`,
|
|
22
|
+
* beside your own. Dropped on a view, it proposes a create carrying `source.event`, `duration` long
|
|
23
|
+
* over a time column or `days` long over day cells, or with `eventId` a move.
|
|
24
|
+
*/
|
|
25
|
+
function calendarDropData(source) {
|
|
26
|
+
return { [dropKey]: source };
|
|
27
|
+
}
|
|
28
|
+
/** The calendar drop a draggable carries (`calendarDropData`), or `null` without one. */
|
|
29
|
+
function calendarDropOf(source) {
|
|
30
|
+
const drop = field(source, dropKey);
|
|
31
|
+
return typeof drop === "object" && drop !== null ? drop : null;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The calendar event a draggable is, while one is dragged out of a view to a droppable of yours;
|
|
35
|
+
* `null` for any other draggable. Read it in `onDragEnd` from `operation.source`.
|
|
36
|
+
*/
|
|
37
|
+
function calendarEventOf(source) {
|
|
38
|
+
const event = field(source, eventKey);
|
|
39
|
+
return typeof event === "object" && event !== null ? event : null;
|
|
40
|
+
}
|
|
41
|
+
/** The data of the draggable a calendar event becomes while it's dragged out. */
|
|
42
|
+
function calendarEventData(data) {
|
|
43
|
+
return { [eventKey]: data };
|
|
44
|
+
}
|
|
45
|
+
/** The data of a calendar view's droppable. */
|
|
46
|
+
const calendarViewData = Object.freeze({ [regionKey]: true });
|
|
47
|
+
/**
|
|
48
|
+
* Whether a droppable is a calendar view's: the adapter handles drops there itself, so an
|
|
49
|
+
* `onDragEnd` of yours can leave `operation.target` alone when this says so.
|
|
50
|
+
*/
|
|
51
|
+
function isCalendarTarget(target) {
|
|
52
|
+
return field(target, regionKey) === true;
|
|
53
|
+
}
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region src/connect.ts
|
|
56
|
+
/** The most of a long or wide event the drag's stand-in covers, as the calendar's copy does. */
|
|
57
|
+
const PROXY_MAXIMUM = {
|
|
58
|
+
height: 120,
|
|
59
|
+
width: 320
|
|
60
|
+
};
|
|
61
|
+
/** The stand-in moves with the pointer and is dropped where it is, without an animation back. */
|
|
62
|
+
const proxyFeedback = [Feedback.configure({
|
|
63
|
+
dropAnimation: null,
|
|
64
|
+
feedback: "move"
|
|
65
|
+
})];
|
|
66
|
+
/**
|
|
67
|
+
* dnd-kit's `CollisionPriority.Highest`, which a view claims only where it's the topmost thing
|
|
68
|
+
* under the pointer: there it wins over a droppable of the app's around or under it, and a
|
|
69
|
+
* droppable of the app's over it wins, since the view isn't topmost there.
|
|
70
|
+
*/
|
|
71
|
+
const VIEW_PRIORITY = 4;
|
|
72
|
+
/** dnd-kit's `CollisionType.PointerIntersection`. */
|
|
73
|
+
const POINTER_INTERSECTION = 2;
|
|
74
|
+
/** Opaque IDs for the entities the adapter registers, unique on the page. */
|
|
75
|
+
let entities = 0;
|
|
76
|
+
const nextId = (kind) => `@moltenship/calendar-${kind}:${entities += 1}`;
|
|
77
|
+
/** One connection per manager, shared by every `connectCalendarDragDrop` call for it. */
|
|
78
|
+
const connections = /* @__PURE__ */ new WeakMap();
|
|
79
|
+
/** Reports an error without breaking dnd-kit's dispatch to its other listeners. */
|
|
80
|
+
function report(error) {
|
|
81
|
+
if (typeof reportError === "function") reportError(error);
|
|
82
|
+
else console.error(error);
|
|
83
|
+
}
|
|
84
|
+
/** Whether `element` holds the topmost thing at a point that the pointer can hit. */
|
|
85
|
+
function topmostIn(element, { x, y }) {
|
|
86
|
+
const [top] = document.elementsFromPoint(x, y);
|
|
87
|
+
return top !== void 0 && element.contains(top);
|
|
88
|
+
}
|
|
89
|
+
/** Whether a drag started from the keyboard, in any realm. */
|
|
90
|
+
function fromKeyboard(event) {
|
|
91
|
+
return event?.type.startsWith("key") === true;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* An invisible stand-in for a calendar event while dnd-kit follows its drag: dnd-kit measures the
|
|
95
|
+
* drag's shape, which collision detection needs, from its source's element, and moves that element
|
|
96
|
+
* with the pointer. The calendar's own element stays where React put it, and the calendar shows
|
|
97
|
+
* its own copy under the pointer. The stand-in has the event's size, clipped as that copy is, sits
|
|
98
|
+
* under the pointer as the event was grabbed, and hides from assistive technology: dnd-kit only
|
|
99
|
+
* adds the attributes it finds missing.
|
|
100
|
+
*/
|
|
101
|
+
function standIn(drag, grabbed, point) {
|
|
102
|
+
const width = Math.min(grabbed.width, PROXY_MAXIMUM.width);
|
|
103
|
+
const height = Math.min(grabbed.height, PROXY_MAXIMUM.height);
|
|
104
|
+
const offset = {
|
|
105
|
+
x: Math.min(Math.max(drag.origin.x - grabbed.left, 0), width),
|
|
106
|
+
y: Math.min(Math.max(drag.origin.y - grabbed.top, 0), height)
|
|
107
|
+
};
|
|
108
|
+
const proxy = document.createElement("div");
|
|
109
|
+
for (const [name, value] of [
|
|
110
|
+
["aria-describedby", ""],
|
|
111
|
+
["aria-hidden", "true"],
|
|
112
|
+
["aria-roledescription", ""],
|
|
113
|
+
["data-calendar-drag-proxy", ""],
|
|
114
|
+
["role", "presentation"],
|
|
115
|
+
["tabindex", "-1"]
|
|
116
|
+
]) proxy.setAttribute(name, value);
|
|
117
|
+
Object.assign(proxy.style, {
|
|
118
|
+
height: `${height}px`,
|
|
119
|
+
left: `${point.x - offset.x}px`,
|
|
120
|
+
margin: "0",
|
|
121
|
+
opacity: "0",
|
|
122
|
+
pointerEvents: "none",
|
|
123
|
+
position: "fixed",
|
|
124
|
+
top: `${point.y - offset.y}px`,
|
|
125
|
+
width: `${width}px`
|
|
126
|
+
});
|
|
127
|
+
document.body.append(proxy);
|
|
128
|
+
return proxy;
|
|
129
|
+
}
|
|
130
|
+
/** Whether a draggable is one the calendar takes: an item to drop, or one of its events. */
|
|
131
|
+
function takesDrop(source) {
|
|
132
|
+
return calendarDropOf(source) !== null || calendarEventOf(source) !== null;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Connects the calendar views on the page to a dnd-kit manager, and returns a function that
|
|
136
|
+
* disconnects them. `CalendarDndKit` does this for the `DragDropProvider` it's in; call it
|
|
137
|
+
* yourself with a `DragDropManager` from `@dnd-kit/dom`. Connecting a manager again shares the
|
|
138
|
+
* first connection, which lasts until every call has disconnected.
|
|
139
|
+
*
|
|
140
|
+
* - Each view's region becomes a droppable, disabled while the view is read-only, that takes
|
|
141
|
+
* only draggables carrying `calendarDropData` and calendar events. Its ID is opaque: tell it by
|
|
142
|
+
* `isCalendarTarget`. It wins where the view is the topmost thing under the pointer, and a
|
|
143
|
+
* droppable of yours over the view wins there.
|
|
144
|
+
* - A draggable carrying `calendarDropData` previews in the view under it and proposes there when
|
|
145
|
+
* dropped, through the view's own policies, `onEventChange` and announcer. Over a droppable of
|
|
146
|
+
* yours it shows nothing, and dropped there it proposes nothing.
|
|
147
|
+
* - An event a view moves becomes a dnd-kit drag of type `calendarEventType` once the pointer
|
|
148
|
+
* leaves the view, so your droppables see it like any drag; moves that stay in the view never
|
|
149
|
+
* reach dnd-kit. Released over one of yours, the drag ends as a drop and the calendar proposes
|
|
150
|
+
* nothing; any other end, such as a drop back in the calendar or Escape, ends it as `canceled`.
|
|
151
|
+
* The drag needs dnd-kit's Feedback plugin, one of its defaults, which measures its shape.
|
|
152
|
+
* - dnd-kit's `AutoScroller` is off while a drag is over a view, which scrolls itself.
|
|
153
|
+
*/
|
|
154
|
+
function connectCalendarDragDrop(manager) {
|
|
155
|
+
let connection = connections.get(manager);
|
|
156
|
+
if (connection === void 0) {
|
|
157
|
+
connection = {
|
|
158
|
+
count: 0,
|
|
159
|
+
disconnect: connect(manager)
|
|
160
|
+
};
|
|
161
|
+
connections.set(manager, connection);
|
|
162
|
+
}
|
|
163
|
+
const shared = connection;
|
|
164
|
+
shared.count += 1;
|
|
165
|
+
let released = false;
|
|
166
|
+
return () => {
|
|
167
|
+
if (released) return;
|
|
168
|
+
released = true;
|
|
169
|
+
shared.count -= 1;
|
|
170
|
+
if (shared.count > 0) return;
|
|
171
|
+
connections.delete(manager);
|
|
172
|
+
shared.disconnect();
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
function connect(manager) {
|
|
176
|
+
const { monitor } = manager;
|
|
177
|
+
const cleanups = [];
|
|
178
|
+
/** A droppable per view region, by its element. */
|
|
179
|
+
const droppables = /* @__PURE__ */ new Map();
|
|
180
|
+
/** Whether this connection turned the auto-scroller off, so it turns it on again. */
|
|
181
|
+
let pausedScroller = false;
|
|
182
|
+
/** Whether the target is a droppable of the app's rather than a view. */
|
|
183
|
+
function overOther() {
|
|
184
|
+
const { target } = manager.dragOperation;
|
|
185
|
+
return target != null && !isCalendarTarget(target);
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Whether a view that takes drops is the topmost thing at a point. Asked before dnd-kit applies a
|
|
189
|
+
* move, which is when its auto-scroller would act, rather than from the target it works out
|
|
190
|
+
* after.
|
|
191
|
+
*/
|
|
192
|
+
function overViewAt({ x, y }) {
|
|
193
|
+
const [top] = document.elementsFromPoint(x, y);
|
|
194
|
+
if (top === void 0) return false;
|
|
195
|
+
for (const [element, droppable] of droppables) if (!droppable.disabled && element.contains(top)) return true;
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
/** Lets the view scroll itself while the drag is over it, and dnd-kit scroll elsewhere. */
|
|
199
|
+
function pauseScroller(pause) {
|
|
200
|
+
const scroller = manager.registry.plugins.get(AutoScroller);
|
|
201
|
+
if (scroller === void 0) return;
|
|
202
|
+
if (pause && !scroller.isDisabled()) {
|
|
203
|
+
scroller.disable();
|
|
204
|
+
pausedScroller = true;
|
|
205
|
+
} else if (!pause && pausedScroller) {
|
|
206
|
+
scroller.enable();
|
|
207
|
+
pausedScroller = false;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
cleanups.push(() => pauseScroller(false));
|
|
211
|
+
cleanups.push(observeCalendarRegions((regions) => {
|
|
212
|
+
const present = /* @__PURE__ */ new Set();
|
|
213
|
+
for (const { disabled, element } of regions) {
|
|
214
|
+
present.add(element);
|
|
215
|
+
const existing = droppables.get(element);
|
|
216
|
+
if (existing !== void 0) {
|
|
217
|
+
existing.disabled = disabled;
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
const id = nextId("view");
|
|
221
|
+
const droppable = new Droppable({
|
|
222
|
+
accept: takesDrop,
|
|
223
|
+
collisionDetector: ({ dragOperation }) => topmostIn(element, dragOperation.position.current) ? {
|
|
224
|
+
id,
|
|
225
|
+
priority: VIEW_PRIORITY,
|
|
226
|
+
type: POINTER_INTERSECTION,
|
|
227
|
+
value: 1
|
|
228
|
+
} : null,
|
|
229
|
+
data: calendarViewData,
|
|
230
|
+
disabled,
|
|
231
|
+
element,
|
|
232
|
+
id,
|
|
233
|
+
register: false
|
|
234
|
+
}, manager);
|
|
235
|
+
droppable.register();
|
|
236
|
+
droppables.set(element, droppable);
|
|
237
|
+
}
|
|
238
|
+
for (const [element, droppable] of droppables) {
|
|
239
|
+
if (present.has(element)) continue;
|
|
240
|
+
droppable.destroy();
|
|
241
|
+
droppables.delete(element);
|
|
242
|
+
}
|
|
243
|
+
}));
|
|
244
|
+
cleanups.push(() => {
|
|
245
|
+
for (const droppable of droppables.values()) droppable.destroy();
|
|
246
|
+
droppables.clear();
|
|
247
|
+
});
|
|
248
|
+
let drop = null;
|
|
249
|
+
let point = null;
|
|
250
|
+
function follow() {
|
|
251
|
+
if (drop === null || point === null) return;
|
|
252
|
+
pauseScroller(overViewAt(point));
|
|
253
|
+
drop.move(overOther() ? null : point);
|
|
254
|
+
}
|
|
255
|
+
cleanups.push(monitor.addEventListener("dragstart", ({ operation }) => {
|
|
256
|
+
const source = calendarDropOf(operation.source);
|
|
257
|
+
if (source === null) return;
|
|
258
|
+
try {
|
|
259
|
+
drop = startCalendarDrop(source, { input: fromKeyboard(operation.activatorEvent) ? "keyboard" : "pointer" });
|
|
260
|
+
} catch (error) {
|
|
261
|
+
report(error);
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
point = operation.position.current;
|
|
265
|
+
follow();
|
|
266
|
+
}), monitor.addEventListener("dragmove", ({ by, operation, to }) => {
|
|
267
|
+
if (drop === null) return;
|
|
268
|
+
const { current } = operation.position;
|
|
269
|
+
point = to ?? {
|
|
270
|
+
x: current.x + (by?.x ?? 0),
|
|
271
|
+
y: current.y + (by?.y ?? 0)
|
|
272
|
+
};
|
|
273
|
+
follow();
|
|
274
|
+
}), monitor.addEventListener("dragover", () => {
|
|
275
|
+
if (drop === null) return;
|
|
276
|
+
point = manager.dragOperation.position.current;
|
|
277
|
+
follow();
|
|
278
|
+
}), monitor.addEventListener("dragend", ({ canceled, operation }) => {
|
|
279
|
+
const current = drop;
|
|
280
|
+
if (current === null) return;
|
|
281
|
+
drop = null;
|
|
282
|
+
point = null;
|
|
283
|
+
pauseScroller(false);
|
|
284
|
+
const elsewhere = operation.target != null && !isCalendarTarget(operation.target);
|
|
285
|
+
if (canceled || elsewhere) current.cancel();
|
|
286
|
+
else current.drop();
|
|
287
|
+
}));
|
|
288
|
+
cleanups.push(() => {
|
|
289
|
+
drop?.cancel();
|
|
290
|
+
drop = null;
|
|
291
|
+
});
|
|
292
|
+
cleanups.push(monitorCalendarDrags((drag) => {
|
|
293
|
+
const grabbed = drag.element.getBoundingClientRect();
|
|
294
|
+
/** The dnd-kit drag, once the pointer has left the view. */
|
|
295
|
+
let mirror = null;
|
|
296
|
+
/** dnd-kit refused the drag, or another was on: the calendar keeps it to itself. */
|
|
297
|
+
let refused = false;
|
|
298
|
+
let started = false;
|
|
299
|
+
let latest = drag.origin;
|
|
300
|
+
const isOurs = () => mirror !== null && manager.dragOperation.source === mirror.draggable;
|
|
301
|
+
function begin(at) {
|
|
302
|
+
if (!manager.dragOperation.status.idle) {
|
|
303
|
+
refused = true;
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
const proxy = standIn(drag, grabbed, at);
|
|
307
|
+
const draggable = new Draggable({
|
|
308
|
+
data: calendarEventData({
|
|
309
|
+
event: drag.event.event,
|
|
310
|
+
eventId: drag.event.id,
|
|
311
|
+
placement: eventPlacement(drag.event)
|
|
312
|
+
}),
|
|
313
|
+
element: proxy,
|
|
314
|
+
id: nextId("event"),
|
|
315
|
+
register: false,
|
|
316
|
+
sensors: [],
|
|
317
|
+
type: calendarEventType
|
|
318
|
+
}, manager);
|
|
319
|
+
draggable.register();
|
|
320
|
+
draggable.plugins = proxyFeedback;
|
|
321
|
+
const stops = [monitor.addEventListener("dragstart", () => {
|
|
322
|
+
if (!isOurs()) return;
|
|
323
|
+
started = true;
|
|
324
|
+
pauseScroller(overViewAt(latest));
|
|
325
|
+
manager.actions.move({ to: latest });
|
|
326
|
+
}), monitor.addEventListener("dragover", () => {
|
|
327
|
+
if (started && isOurs()) drag.refresh();
|
|
328
|
+
})];
|
|
329
|
+
const release = () => {
|
|
330
|
+
for (const stop of stops) stop();
|
|
331
|
+
pauseScroller(false);
|
|
332
|
+
const remove = () => {
|
|
333
|
+
if (!manager.dragOperation.status.idle && manager.dragOperation.source === draggable) {
|
|
334
|
+
requestAnimationFrame(remove);
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
draggable.destroy();
|
|
338
|
+
proxy.remove();
|
|
339
|
+
};
|
|
340
|
+
remove();
|
|
341
|
+
};
|
|
342
|
+
mirror = {
|
|
343
|
+
draggable,
|
|
344
|
+
release
|
|
345
|
+
};
|
|
346
|
+
try {
|
|
347
|
+
if (manager.actions.start({
|
|
348
|
+
coordinates: at,
|
|
349
|
+
source: draggable
|
|
350
|
+
}).signal.aborted) refused = true;
|
|
351
|
+
} catch (error) {
|
|
352
|
+
report(error);
|
|
353
|
+
refused = true;
|
|
354
|
+
}
|
|
355
|
+
if (refused) {
|
|
356
|
+
mirror = null;
|
|
357
|
+
release();
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return {
|
|
361
|
+
claims: () => started && isOurs() && overOther(),
|
|
362
|
+
end: (dropped) => {
|
|
363
|
+
if (mirror === null) return;
|
|
364
|
+
if (isOurs()) manager.actions.stop({ canceled: !dropped });
|
|
365
|
+
mirror.release();
|
|
366
|
+
mirror = null;
|
|
367
|
+
},
|
|
368
|
+
move: (next) => {
|
|
369
|
+
latest = next;
|
|
370
|
+
if (mirror === null) {
|
|
371
|
+
if (!refused && !overViewAt(next)) begin(next);
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
if (!started || !isOurs()) return;
|
|
375
|
+
pauseScroller(overViewAt(next));
|
|
376
|
+
manager.actions.move({ to: next });
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
}));
|
|
380
|
+
return () => {
|
|
381
|
+
for (const cleanup of cleanups.splice(0).reverse()) cleanup();
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
//#endregion
|
|
385
|
+
//#region src/calendar-dnd-kit.ts
|
|
386
|
+
/**
|
|
387
|
+
* Connects the calendar views on the page to the dnd-kit `DragDropProvider` it's rendered in, so
|
|
388
|
+
* draggables carrying `calendarDropData` drop into them and calendar events drag out to your
|
|
389
|
+
* droppables. Render one inside the provider, anywhere; it renders nothing, and a second one
|
|
390
|
+
* shares the first's connection. Outside a provider, dnd-kit hands it a default manager that no
|
|
391
|
+
* droppable of yours is on. See `connectCalendarDragDrop`.
|
|
392
|
+
*/
|
|
393
|
+
function CalendarDndKit() {
|
|
394
|
+
const manager = useDragDropManager();
|
|
395
|
+
useEffect(() => manager === null ? void 0 : connectCalendarDragDrop(manager), [manager]);
|
|
396
|
+
return null;
|
|
397
|
+
}
|
|
398
|
+
//#endregion
|
|
399
|
+
export { CalendarDndKit, calendarDropData, calendarDropOf, calendarEventOf, calendarEventType, connectCalendarDragDrop, isCalendarTarget };
|
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moltenship/calendar-dnd-kit",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.0-rc.1",
|
|
4
|
+
"description": "Optional dnd-kit adapter for Moltenship Time: drag your items into the calendar and events out.",
|
|
5
|
+
"homepage": "https://time.molten.sh/docs/api/dnd-kit",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/Moltenship/time/issues"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/Moltenship/time.git",
|
|
13
|
+
"directory": "packages/calendar-dnd-kit"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"type": "module",
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./dist/index.d.mts",
|
|
23
|
+
"import": "./dist/index.mjs"
|
|
24
|
+
},
|
|
25
|
+
"./package.json": "./package.json"
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@moltenship/calendar-core": "1.0.0-rc.1",
|
|
32
|
+
"@moltenship/calendar-dnd": "1.0.0-rc.1"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@dnd-kit/dom": "0.5.0",
|
|
36
|
+
"@dnd-kit/react": "0.5.0",
|
|
37
|
+
"@types/react": "19.3.0",
|
|
38
|
+
"@types/react-dom": "19.3.0",
|
|
39
|
+
"jsdom": "30.1.2",
|
|
40
|
+
"react": "19.3.0",
|
|
41
|
+
"react-dom": "19.3.0",
|
|
42
|
+
"typescript": "^7.0.2",
|
|
43
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@1.1.0",
|
|
44
|
+
"vite-plus": "1.1.0"
|
|
45
|
+
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"@dnd-kit/dom": "^0.5.0",
|
|
48
|
+
"@dnd-kit/react": "^0.5.0",
|
|
49
|
+
"react": "^19.0.0"
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"build": "vp pack",
|
|
53
|
+
"check": "vp check",
|
|
54
|
+
"dev": "vp pack --watch",
|
|
55
|
+
"test": "vp test --passWithNoTests"
|
|
56
|
+
}
|
|
6
57
|
}
|