Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadContextDrag.ts

98 lines2.9 KB

1import type { EnvironmentId, ScopedThreadRef } from "@t3tools/contracts";
2import { type RefObject, useEffect, useEffectEvent, useSyncExternalStore } from "react";
3​
4export const THREAD_CONTEXT_DROP_EVENT = "t3-thread-context-drop";
5​
6export function useThreadContextDrop(
7 targetRef: RefObject<EventTarget | null>,
8 environmentId: EnvironmentId,
9 targetKey: string,
10 onDrop: (refs: ReadonlyArray<ScopedThreadRef>, isActive: () => boolean) => void,
11) {
12 const handleDrop = useEffectEvent(onDrop);
13 useEffect(() => {
14 const target = targetRef.current;
15 if (!target) return;
16 let active = true;
17 const listener = (event: Event) =>
18 handleDrop((event as CustomEvent<ReadonlyArray<ScopedThreadRef>>).detail, () => active);
19 target.addEventListener(THREAD_CONTEXT_DROP_EVENT, listener);
20 return () => {
21 active = false;
22 target.removeEventListener(THREAD_CONTEXT_DROP_EVENT, listener);
23 };
24 }, [environmentId, targetKey, targetRef]);
26​
27const DROP_TARGET_ATTRIBUTE = "data-thread-context-drop";
28const DROP_OVER_ATTRIBUTE = "data-thread-context-over";
29​
30export interface ThreadContextDragGhost {
31 readonly x: number;
32 readonly y: number;
33 readonly title: string;
34 readonly count: number;
36​
37let ghost: ThreadContextDragGhost | null = null;
38const listeners = new Set<() => void>();
39​
40function setGhost(next: ThreadContextDragGhost | null) {
41 if (ghost === next) return;
42 ghost = next;
43 for (const listener of listeners) listener();
45​
46export function useThreadContextDragGhost(): ThreadContextDragGhost | null {
47 return useSyncExternalStore(
48 (listener) => {
49 listeners.add(listener);
50 return () => listeners.delete(listener);
51 },
52 () => ghost,
53 () => null,
54 );
56​
57let overTarget: HTMLElement | null = null;
58​
59function findDropTarget(point: { x: number; y: number }): HTMLElement | null {
60 return (
61 document
62 .elementFromPoint(point.x, point.y)
63 ?.closest<HTMLElement>(`[${DROP_TARGET_ATTRIBUTE}]`) ?? null
64 );
66​
67export function moveThreadContextDrag(
68 point: { x: number; y: number },
69 label: { title: string; count: number },
70) {
71 const target = findDropTarget(point);
72 if (target !== overTarget) {
73 overTarget?.removeAttribute(DROP_OVER_ATTRIBUTE);
74 target?.setAttribute(DROP_OVER_ATTRIBUTE, "true");
75 overTarget = target;
76 }
77 setGhost({ x: point.x, y: point.y, ...label });
79​
80export function endThreadContextDrag() {
81 overTarget?.removeAttribute(DROP_OVER_ATTRIBUTE);
82 overTarget = null;
83 setGhost(null);
85​
86export function dropThreadContext(
87 point: { x: number; y: number },
88 threads: ReadonlyArray<ScopedThreadRef>,
89): boolean {
90 const target = findDropTarget(point);
91 if (!target || threads.length === 0) return false;
92 target.dispatchEvent(new CustomEvent(THREAD_CONTEXT_DROP_EVENT, { detail: threads }));
93 return true;
95​
96export function threadContextDropTargetProps() {
97 return { [DROP_TARGET_ATTRIBUTE]: "true" } as const;