From f710f5935b82df738401d925fd8dec4e645b3ace Mon Sep 17 00:00:00 2001 From: xiaoxia Date: Thu, 30 Jul 2026 09:58:35 +0800 Subject: [PATCH] fix: prettier format --- .../timeline/hooks/usePlayheadDrag.ts | 74 +++++++++---------- 1 file changed, 35 insertions(+), 39 deletions(-) diff --git a/apps/web/src/pages/editing-planner/components/timeline/hooks/usePlayheadDrag.ts b/apps/web/src/pages/editing-planner/components/timeline/hooks/usePlayheadDrag.ts index cb3a0c60d..fa189d090 100644 --- a/apps/web/src/pages/editing-planner/components/timeline/hooks/usePlayheadDrag.ts +++ b/apps/web/src/pages/editing-planner/components/timeline/hooks/usePlayheadDrag.ts @@ -1,75 +1,71 @@ -import { useState, useCallback, useEffect, useRef } from "react"; +import { useState, useCallback, useEffect, useRef } from "react" interface UsePlayheadDragOptions { /** 当前播放时间(秒) */ - currentTime: number; + currentTime: number /** 缩放:每秒像素数 */ - pps: number; + pps: number /** 总时长(秒) */ - totalDuration: number; + totalDuration: number /** 播放头跳转回调 */ - onSeek?: (time: number) => void; + onSeek?: (time: number) => void } /** * 播放头拖拽 Hook * 封装播放头的 mousedown/mousemove/mouseup 拖拽逻辑 */ -export const usePlayheadDrag = ({ - pps, - totalDuration, - onSeek, -}: UsePlayheadDragOptions) => { - const [playheadDragging, setPlayheadDragging] = useState(false); - const trackRef = useRef(null); +export const usePlayheadDrag = ({ pps, totalDuration, onSeek }: UsePlayheadDragOptions) => { + const [playheadDragging, setPlayheadDragging] = useState(false) + const trackRef = useRef(null) /* ── 播放头拖拽全局 mousemove/mouseup ── */ useEffect(() => { - if (!playheadDragging) return; + if (!playheadDragging) return const handleMouseMove = (e: MouseEvent) => { - const trackEl = trackRef.current; - if (!trackEl) return; - const rect = trackEl.getBoundingClientRect(); - const x = e.clientX - rect.left + trackEl.scrollLeft; - const time = Math.max(0, Math.min(x / pps, totalDuration)); - onSeek?.(Math.round(time * 10) / 10); - }; + const trackEl = trackRef.current + if (!trackEl) return + const rect = trackEl.getBoundingClientRect() + const x = e.clientX - rect.left + trackEl.scrollLeft + const time = Math.max(0, Math.min(x / pps, totalDuration)) + onSeek?.(Math.round(time * 10) / 10) + } const handleMouseUp = () => { - setPlayheadDragging(false); - }; + setPlayheadDragging(false) + } - document.addEventListener("mousemove", handleMouseMove); - document.addEventListener("mouseup", handleMouseUp); + document.addEventListener("mousemove", handleMouseMove) + document.addEventListener("mouseup", handleMouseUp) return () => { - document.removeEventListener("mousemove", handleMouseMove); - document.removeEventListener("mouseup", handleMouseUp); - }; - }, [playheadDragging, pps, totalDuration, onSeek]); + document.removeEventListener("mousemove", handleMouseMove) + document.removeEventListener("mouseup", handleMouseUp) + } + }, [playheadDragging, pps, totalDuration, onSeek]) /* ── 播放头拖拽开始 ── */ const handlePlayheadMouseDown = useCallback((e: React.MouseEvent) => { - e.preventDefault(); - e.stopPropagation(); - setPlayheadDragging(true); - }, []); + e.preventDefault() + e.stopPropagation() + setPlayheadDragging(true) + }, []) /* ── 标尺点击跳转播放头 ── */ const handleRulerClick = useCallback( (e: React.MouseEvent) => { - const rect = e.currentTarget.getBoundingClientRect(); - const x = e.clientX - rect.left; - const time = Math.max(0, Math.min(x / pps, totalDuration)); - onSeek?.(Math.round(time * 10) / 10); + const rect = e.currentTarget.getBoundingClientRect() + const x = e.clientX - rect.left + const time = Math.max(0, Math.min(x / pps, totalDuration)) + onSeek?.(Math.round(time * 10) / 10) }, [pps, totalDuration, onSeek], - ); + ) return { trackRef, playheadDragging, handlePlayheadMouseDown, handleRulerClick, - }; -}; \ No newline at end of file + } +}