2024-03-07 00:35:10 +03:00
|
|
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
|
|
|
import { isDesktop, isMobile } from "react-device-detect";
|
2024-03-03 19:32:35 +03:00
|
|
|
import scrollIntoView from "scroll-into-view-if-needed";
|
2024-03-18 23:58:54 +03:00
|
|
|
import { useTimelineUtils } from "./use-timeline-utils";
|
2024-02-21 02:22:59 +03:00
|
|
|
|
2024-03-18 23:58:54 +03:00
|
|
|
type DraggableElementProps = {
|
2024-02-21 02:22:59 +03:00
|
|
|
contentRef: React.RefObject<HTMLElement>;
|
|
|
|
|
timelineRef: React.RefObject<HTMLDivElement>;
|
2024-03-18 23:58:54 +03:00
|
|
|
draggableElementRef: React.RefObject<HTMLDivElement>;
|
2024-02-21 02:22:59 +03:00
|
|
|
segmentDuration: number;
|
2024-03-18 23:58:54 +03:00
|
|
|
showDraggableElement: boolean;
|
|
|
|
|
draggableElementTime?: number;
|
|
|
|
|
draggableElementEarliestTime?: number;
|
|
|
|
|
draggableElementLatestTime?: number;
|
|
|
|
|
setDraggableElementTime?: React.Dispatch<React.SetStateAction<number>>;
|
|
|
|
|
draggableElementTimeRef: React.MutableRefObject<HTMLDivElement | null>;
|
2024-02-21 02:22:59 +03:00
|
|
|
timelineDuration: number;
|
2024-03-09 01:49:10 +03:00
|
|
|
timelineStartAligned: number;
|
2024-02-21 02:22:59 +03:00
|
|
|
isDragging: boolean;
|
|
|
|
|
setIsDragging: React.Dispatch<React.SetStateAction<boolean>>;
|
2024-03-18 23:58:54 +03:00
|
|
|
setDraggableElementPosition?: React.Dispatch<React.SetStateAction<number>>;
|
2024-03-01 18:36:13 +03:00
|
|
|
};
|
2024-02-21 02:22:59 +03:00
|
|
|
|
2024-03-18 23:58:54 +03:00
|
|
|
function useDraggableElement({
|
2024-02-21 02:22:59 +03:00
|
|
|
contentRef,
|
|
|
|
|
timelineRef,
|
2024-03-18 23:58:54 +03:00
|
|
|
draggableElementRef,
|
2024-02-21 02:22:59 +03:00
|
|
|
segmentDuration,
|
2024-03-18 23:58:54 +03:00
|
|
|
showDraggableElement,
|
|
|
|
|
draggableElementTime,
|
|
|
|
|
draggableElementEarliestTime,
|
|
|
|
|
draggableElementLatestTime,
|
|
|
|
|
setDraggableElementTime,
|
|
|
|
|
draggableElementTimeRef,
|
2024-02-21 02:22:59 +03:00
|
|
|
timelineDuration,
|
2024-03-09 01:49:10 +03:00
|
|
|
timelineStartAligned,
|
2024-02-21 02:22:59 +03:00
|
|
|
isDragging,
|
|
|
|
|
setIsDragging,
|
2024-03-18 23:58:54 +03:00
|
|
|
setDraggableElementPosition,
|
|
|
|
|
}: DraggableElementProps) {
|
2024-03-07 00:35:10 +03:00
|
|
|
const [clientYPosition, setClientYPosition] = useState<number | null>(null);
|
2024-03-08 19:13:42 +03:00
|
|
|
const [initialClickAdjustment, setInitialClickAdjustment] = useState(0);
|
2024-03-21 20:49:04 +03:00
|
|
|
const { alignStartDateToTimeline, getCumulativeScrollTop } = useTimelineUtils(
|
|
|
|
|
{
|
|
|
|
|
segmentDuration: segmentDuration,
|
|
|
|
|
timelineDuration: timelineDuration,
|
|
|
|
|
timelineRef,
|
|
|
|
|
},
|
|
|
|
|
);
|
2024-03-07 00:35:10 +03:00
|
|
|
|
|
|
|
|
const draggingAtTopEdge = useMemo(() => {
|
|
|
|
|
if (clientYPosition && timelineRef.current) {
|
|
|
|
|
return (
|
|
|
|
|
clientYPosition - timelineRef.current.offsetTop <
|
|
|
|
|
timelineRef.current.clientHeight * 0.03 && isDragging
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}, [clientYPosition, timelineRef, isDragging]);
|
|
|
|
|
|
|
|
|
|
const draggingAtBottomEdge = useMemo(() => {
|
|
|
|
|
if (clientYPosition && timelineRef.current) {
|
|
|
|
|
return (
|
|
|
|
|
clientYPosition >
|
|
|
|
|
(timelineRef.current.clientHeight + timelineRef.current.offsetTop) *
|
|
|
|
|
0.97 && isDragging
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}, [clientYPosition, timelineRef, isDragging]);
|
|
|
|
|
|
|
|
|
|
const getClientYPosition = useCallback(
|
2024-03-21 17:00:04 +03:00
|
|
|
(e: MouseEvent | TouchEvent) => {
|
2024-03-07 00:35:10 +03:00
|
|
|
let clientY;
|
2024-03-21 17:00:04 +03:00
|
|
|
if (isMobile && e instanceof TouchEvent) {
|
|
|
|
|
clientY = e.touches[0].clientY;
|
|
|
|
|
} else if (e instanceof MouseEvent) {
|
|
|
|
|
clientY = e.clientY;
|
2024-03-07 00:35:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (clientY) {
|
|
|
|
|
setClientYPosition(clientY);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[setClientYPosition],
|
|
|
|
|
);
|
|
|
|
|
|
2024-02-21 02:22:59 +03:00
|
|
|
const handleMouseDown = useCallback(
|
2024-03-03 22:50:38 +03:00
|
|
|
(
|
|
|
|
|
e: React.MouseEvent<HTMLDivElement> | React.TouchEvent<HTMLDivElement>,
|
|
|
|
|
) => {
|
2024-03-18 23:58:54 +03:00
|
|
|
// prevent default only for mouse events
|
|
|
|
|
// to avoid chrome/android issues
|
|
|
|
|
if (e.nativeEvent instanceof MouseEvent) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
2024-02-21 02:22:59 +03:00
|
|
|
e.stopPropagation();
|
|
|
|
|
setIsDragging(true);
|
2024-03-08 19:13:42 +03:00
|
|
|
|
2024-03-18 23:58:54 +03:00
|
|
|
let clientY;
|
|
|
|
|
if (isMobile && e.nativeEvent instanceof TouchEvent) {
|
|
|
|
|
clientY = e.nativeEvent.touches[0].clientY;
|
|
|
|
|
} else if (e.nativeEvent instanceof MouseEvent) {
|
|
|
|
|
clientY = e.nativeEvent.clientY;
|
|
|
|
|
}
|
|
|
|
|
if (clientY && draggableElementRef.current && isDesktop) {
|
|
|
|
|
const draggableElementRect =
|
|
|
|
|
draggableElementRef.current.getBoundingClientRect();
|
|
|
|
|
if (!isDragging) {
|
|
|
|
|
setInitialClickAdjustment(clientY - draggableElementRect.top);
|
|
|
|
|
}
|
|
|
|
|
setClientYPosition(clientY);
|
2024-03-08 19:13:42 +03:00
|
|
|
}
|
2024-02-21 02:22:59 +03:00
|
|
|
},
|
2024-03-18 23:58:54 +03:00
|
|
|
// we know that these deps are correct
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
[setIsDragging, draggableElementRef],
|
2024-02-21 02:22:59 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const handleMouseUp = useCallback(
|
2024-03-21 17:00:04 +03:00
|
|
|
(e: MouseEvent | TouchEvent) => {
|
2024-02-21 02:22:59 +03:00
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
if (isDragging) {
|
|
|
|
|
setIsDragging(false);
|
2024-03-08 19:13:42 +03:00
|
|
|
setInitialClickAdjustment(0);
|
2024-02-21 02:22:59 +03:00
|
|
|
}
|
|
|
|
|
},
|
2024-02-29 01:23:56 +03:00
|
|
|
[isDragging, setIsDragging],
|
2024-02-21 02:22:59 +03:00
|
|
|
);
|
|
|
|
|
|
2024-03-18 23:58:54 +03:00
|
|
|
const timestampToPixels = useCallback(
|
|
|
|
|
(time: number) => {
|
|
|
|
|
const { scrollHeight: timelineHeight } =
|
|
|
|
|
timelineRef.current as HTMLDivElement;
|
|
|
|
|
|
|
|
|
|
const segmentHeight =
|
|
|
|
|
timelineHeight / (timelineDuration / segmentDuration);
|
|
|
|
|
|
|
|
|
|
return ((timelineStartAligned - time) / segmentDuration) * segmentHeight;
|
|
|
|
|
},
|
|
|
|
|
[segmentDuration, timelineRef, timelineStartAligned, timelineDuration],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const updateDraggableElementPosition = useCallback(
|
2024-03-04 03:17:24 +03:00
|
|
|
(
|
2024-03-18 23:58:54 +03:00
|
|
|
newElementPosition: number,
|
2024-03-04 03:17:24 +03:00
|
|
|
segmentStartTime: number,
|
2024-03-06 00:53:49 +03:00
|
|
|
scrollTimeline: boolean,
|
2024-03-04 03:17:24 +03:00
|
|
|
updateHandle: boolean,
|
|
|
|
|
) => {
|
2024-03-18 23:58:54 +03:00
|
|
|
const thumb = draggableElementRef.current;
|
2024-03-01 18:36:13 +03:00
|
|
|
if (thumb) {
|
|
|
|
|
requestAnimationFrame(() => {
|
2024-03-18 23:58:54 +03:00
|
|
|
thumb.style.top = `${newElementPosition}px`;
|
|
|
|
|
if (setDraggableElementPosition) {
|
|
|
|
|
setDraggableElementPosition(newElementPosition);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (draggableElementTimeRef.current) {
|
|
|
|
|
draggableElementTimeRef.current.textContent = new Date(
|
2024-03-01 18:36:13 +03:00
|
|
|
segmentStartTime * 1000,
|
|
|
|
|
).toLocaleTimeString([], {
|
|
|
|
|
hour: "2-digit",
|
|
|
|
|
minute: "2-digit",
|
2024-03-07 00:35:10 +03:00
|
|
|
...(segmentDuration < 60 && isDesktop && { second: "2-digit" }),
|
2024-03-01 18:36:13 +03:00
|
|
|
});
|
2024-03-06 00:53:49 +03:00
|
|
|
if (scrollTimeline) {
|
|
|
|
|
scrollIntoView(thumb, {
|
|
|
|
|
block: "center",
|
|
|
|
|
behavior: "smooth",
|
2024-03-08 07:02:29 +03:00
|
|
|
scrollMode: "if-needed",
|
2024-03-06 00:53:49 +03:00
|
|
|
});
|
|
|
|
|
}
|
2024-03-01 18:36:13 +03:00
|
|
|
}
|
|
|
|
|
});
|
2024-03-04 03:17:24 +03:00
|
|
|
|
2024-03-18 23:58:54 +03:00
|
|
|
if (setDraggableElementTime && updateHandle) {
|
|
|
|
|
setDraggableElementTime(segmentStartTime);
|
2024-03-01 18:36:13 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
2024-03-18 23:58:54 +03:00
|
|
|
[
|
|
|
|
|
segmentDuration,
|
|
|
|
|
draggableElementTimeRef,
|
|
|
|
|
draggableElementRef,
|
|
|
|
|
setDraggableElementTime,
|
|
|
|
|
setDraggableElementPosition,
|
|
|
|
|
],
|
2024-03-01 18:36:13 +03:00
|
|
|
);
|
|
|
|
|
|
2024-02-21 02:22:59 +03:00
|
|
|
const handleMouseMove = useCallback(
|
2024-03-21 17:00:04 +03:00
|
|
|
(e: MouseEvent | TouchEvent) => {
|
2024-02-21 20:58:41 +03:00
|
|
|
if (
|
|
|
|
|
!contentRef.current ||
|
|
|
|
|
!timelineRef.current ||
|
2024-03-18 23:58:54 +03:00
|
|
|
!draggableElementRef.current
|
2024-02-21 20:58:41 +03:00
|
|
|
) {
|
2024-02-21 02:22:59 +03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-07 00:35:10 +03:00
|
|
|
getClientYPosition(e);
|
|
|
|
|
},
|
2024-03-03 22:50:38 +03:00
|
|
|
|
2024-03-18 23:58:54 +03:00
|
|
|
[contentRef, draggableElementRef, timelineRef, getClientYPosition],
|
2024-03-07 00:35:10 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
let animationFrameId: number | null = null;
|
2024-02-21 02:22:59 +03:00
|
|
|
|
2024-03-07 00:35:10 +03:00
|
|
|
const handleScroll = () => {
|
|
|
|
|
if (
|
|
|
|
|
timelineRef.current &&
|
2024-03-18 23:58:54 +03:00
|
|
|
showDraggableElement &&
|
2024-03-07 00:35:10 +03:00
|
|
|
isDragging &&
|
|
|
|
|
clientYPosition
|
|
|
|
|
) {
|
2024-02-21 02:22:59 +03:00
|
|
|
const {
|
|
|
|
|
scrollHeight: timelineHeight,
|
|
|
|
|
scrollTop: scrolled,
|
|
|
|
|
offsetTop: timelineTop,
|
|
|
|
|
} = timelineRef.current;
|
|
|
|
|
|
|
|
|
|
const segmentHeight =
|
|
|
|
|
timelineHeight / (timelineDuration / segmentDuration);
|
|
|
|
|
|
|
|
|
|
const parentScrollTop = getCumulativeScrollTop(timelineRef.current);
|
|
|
|
|
|
2024-03-18 23:58:54 +03:00
|
|
|
// bottom of timeline
|
|
|
|
|
const elementEarliest = draggableElementEarliestTime
|
|
|
|
|
? timestampToPixels(draggableElementEarliestTime)
|
|
|
|
|
: segmentHeight * (timelineDuration / segmentDuration) -
|
|
|
|
|
segmentHeight * 3;
|
|
|
|
|
|
|
|
|
|
// top of timeline - default 2 segments added for draggableElement visibility
|
|
|
|
|
const elementLatest = draggableElementLatestTime
|
|
|
|
|
? timestampToPixels(draggableElementLatestTime)
|
|
|
|
|
: segmentHeight * 2 + scrolled;
|
|
|
|
|
|
|
|
|
|
const newElementPosition = Math.min(
|
|
|
|
|
elementEarliest,
|
2024-02-21 02:22:59 +03:00
|
|
|
Math.max(
|
2024-03-18 23:58:54 +03:00
|
|
|
elementLatest,
|
2024-03-08 19:13:42 +03:00
|
|
|
// current Y position
|
|
|
|
|
clientYPosition -
|
|
|
|
|
timelineTop +
|
|
|
|
|
parentScrollTop -
|
|
|
|
|
initialClickAdjustment,
|
2024-02-29 01:23:56 +03:00
|
|
|
),
|
2024-02-21 02:22:59 +03:00
|
|
|
);
|
|
|
|
|
|
2024-03-18 23:58:54 +03:00
|
|
|
const segmentIndex = Math.floor(newElementPosition / segmentHeight);
|
2024-02-28 16:18:08 +03:00
|
|
|
const segmentStartTime = alignStartDateToTimeline(
|
2024-03-09 01:49:10 +03:00
|
|
|
timelineStartAligned - segmentIndex * segmentDuration,
|
2024-02-21 02:22:59 +03:00
|
|
|
);
|
|
|
|
|
|
2024-03-07 00:35:10 +03:00
|
|
|
if (draggingAtTopEdge || draggingAtBottomEdge) {
|
|
|
|
|
let newPosition = clientYPosition;
|
|
|
|
|
|
|
|
|
|
if (draggingAtTopEdge) {
|
|
|
|
|
newPosition = scrolled - segmentHeight;
|
|
|
|
|
timelineRef.current.scrollTop = newPosition;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (draggingAtBottomEdge) {
|
|
|
|
|
newPosition = scrolled + segmentHeight;
|
|
|
|
|
timelineRef.current.scrollTop = newPosition;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-06 00:53:49 +03:00
|
|
|
|
2024-03-18 23:58:54 +03:00
|
|
|
updateDraggableElementPosition(
|
|
|
|
|
newElementPosition - segmentHeight,
|
2024-03-01 18:36:13 +03:00
|
|
|
segmentStartTime,
|
2024-03-07 00:35:10 +03:00
|
|
|
false,
|
2024-03-04 03:17:24 +03:00
|
|
|
false,
|
2024-03-01 18:36:13 +03:00
|
|
|
);
|
2024-03-04 03:17:24 +03:00
|
|
|
|
2024-03-18 23:58:54 +03:00
|
|
|
if (setDraggableElementTime) {
|
|
|
|
|
setDraggableElementTime(
|
2024-03-09 01:49:10 +03:00
|
|
|
timelineStartAligned -
|
2024-03-18 23:58:54 +03:00
|
|
|
((newElementPosition - segmentHeight / 2 - 2) / segmentHeight) *
|
2024-03-09 01:49:10 +03:00
|
|
|
segmentDuration,
|
2024-03-04 03:17:24 +03:00
|
|
|
);
|
|
|
|
|
}
|
2024-03-07 00:35:10 +03:00
|
|
|
|
|
|
|
|
if (draggingAtTopEdge || draggingAtBottomEdge) {
|
|
|
|
|
animationFrameId = requestAnimationFrame(handleScroll);
|
|
|
|
|
}
|
2024-02-21 02:22:59 +03:00
|
|
|
}
|
2024-03-07 00:35:10 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const startScroll = () => {
|
|
|
|
|
if (isDragging) {
|
|
|
|
|
handleScroll();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const stopScroll = () => {
|
|
|
|
|
if (animationFrameId !== null) {
|
|
|
|
|
cancelAnimationFrame(animationFrameId);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
startScroll();
|
|
|
|
|
|
|
|
|
|
return stopScroll;
|
2024-02-29 01:23:56 +03:00
|
|
|
// we know that these deps are correct
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2024-03-07 00:35:10 +03:00
|
|
|
}, [
|
|
|
|
|
clientYPosition,
|
|
|
|
|
segmentDuration,
|
2024-03-09 01:49:10 +03:00
|
|
|
timelineStartAligned,
|
2024-03-07 00:35:10 +03:00
|
|
|
timelineDuration,
|
|
|
|
|
timelineRef,
|
|
|
|
|
draggingAtTopEdge,
|
|
|
|
|
draggingAtBottomEdge,
|
2024-03-18 23:58:54 +03:00
|
|
|
showDraggableElement,
|
2024-03-07 00:35:10 +03:00
|
|
|
]);
|
2024-02-21 02:22:59 +03:00
|
|
|
|
2024-02-23 06:15:50 +03:00
|
|
|
useEffect(() => {
|
2024-03-01 18:36:13 +03:00
|
|
|
if (
|
|
|
|
|
timelineRef.current &&
|
2024-03-18 23:58:54 +03:00
|
|
|
draggableElementRef.current &&
|
|
|
|
|
showDraggableElement &&
|
|
|
|
|
draggableElementTime &&
|
2024-03-01 18:36:13 +03:00
|
|
|
!isDragging
|
|
|
|
|
) {
|
|
|
|
|
const { scrollHeight: timelineHeight, scrollTop: scrolled } =
|
|
|
|
|
timelineRef.current;
|
|
|
|
|
|
|
|
|
|
const segmentHeight =
|
|
|
|
|
timelineHeight / (timelineDuration / segmentDuration);
|
|
|
|
|
|
|
|
|
|
const parentScrollTop = getCumulativeScrollTop(timelineRef.current);
|
|
|
|
|
|
2024-03-18 23:58:54 +03:00
|
|
|
const newElementPosition =
|
|
|
|
|
((timelineStartAligned - draggableElementTime) / segmentDuration) *
|
2024-03-09 01:49:10 +03:00
|
|
|
segmentHeight +
|
2024-03-01 18:36:13 +03:00
|
|
|
parentScrollTop -
|
2024-03-09 01:49:10 +03:00
|
|
|
scrolled -
|
2024-03-18 23:58:54 +03:00
|
|
|
2; // height of draggableElement horizontal line
|
2024-03-01 18:36:13 +03:00
|
|
|
|
2024-03-18 23:58:54 +03:00
|
|
|
updateDraggableElementPosition(
|
|
|
|
|
newElementPosition,
|
|
|
|
|
draggableElementTime,
|
|
|
|
|
true,
|
|
|
|
|
true,
|
|
|
|
|
);
|
2024-02-23 06:15:50 +03:00
|
|
|
}
|
2024-03-01 18:36:13 +03:00
|
|
|
// we know that these deps are correct
|
2024-02-29 01:23:56 +03:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2024-03-18 23:58:54 +03:00
|
|
|
}, [
|
|
|
|
|
draggableElementTime,
|
|
|
|
|
showDraggableElement,
|
|
|
|
|
draggableElementRef,
|
|
|
|
|
timelineStartAligned,
|
|
|
|
|
]);
|
2024-02-23 06:15:50 +03:00
|
|
|
|
2024-02-21 02:22:59 +03:00
|
|
|
return { handleMouseDown, handleMouseUp, handleMouseMove };
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-18 23:58:54 +03:00
|
|
|
export default useDraggableElement;
|