summaryrefslogtreecommitdiff
path: root/apps/web/src/timeline/drag-source.ts
blob: 503c547b187952a79394470aab7c2962cc62a537 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import type { TimelineDragData } from "@/timeline/drag";

const TIMELINE_DRAG_MIME = "application/x-timeline-drag";

/**
 * Owns the state of an in-progress timeline drag session.
 *
 * Exists because browsers restrict `DataTransfer.getData()` to the `drop`
 * event for security — during `dragover`/`dragenter` only `types` is
 * readable. The drop target needs the payload (element type, target
 * element types, source duration) while the pointer is hovering, so we
 * keep a live copy here and hand it out via {@link getActive}.
 */
export class TimelineDragSource {
	private active: TimelineDragData | null = null;

	begin({
		dataTransfer,
		dragData,
	}: {
		dataTransfer: DataTransfer;
		dragData: TimelineDragData;
	}): void {
		dataTransfer.setData(TIMELINE_DRAG_MIME, JSON.stringify(dragData));
		dataTransfer.effectAllowed = "copy";
		this.active = dragData;
	}

	end(): void {
		this.active = null;
	}

	getActive(): TimelineDragData | null {
		return this.active;
	}

	isActive(): boolean {
		return this.active !== null;
	}
}