diff --git a/packages/fakeyou.com/src/components/common/VideoQuickTrim/TrimScrubber.tsx b/packages/fakeyou.com/src/components/common/VideoQuickTrim/TrimScrubber.tsx index b5fb2672af..78c93d6d92 100644 --- a/packages/fakeyou.com/src/components/common/VideoQuickTrim/TrimScrubber.tsx +++ b/packages/fakeyou.com/src/components/common/VideoQuickTrim/TrimScrubber.tsx @@ -1,101 +1,36 @@ -import React, { - useRef, - useState, - PointerEvent -} from 'react'; +import React from 'react'; import { faGripDots } from "@fortawesome/pro-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { QuickTrimData } from './types'; +import {withScrubbing, withScrubbingPropsI} from './withScrubbing'; -export default function TrimScrubber({ - trimStart, - trimDuration, - duration, - width, - boundingWidth, - onChange -}:{ +interface TrimScrubberPropsI extends withScrubbingPropsI { trimStart:number; trimDuration: number; duration: number; - boundingWidth: number; - width:number; onChange:(val:QuickTrimData)=>void -}){ - const [{ - left, - leftOffset, - pointerStart - }, setState] = useState<{ - left: number, - leftOffset:number - pointerStart:number - }>({ - left: trimStart/duration*boundingWidth, - leftOffset: trimStart/duration*boundingWidth, - pointerStart: -1 - }); - const ref = useRef(null); - - if(ref.current){ - ref.current.style.cursor = pointerStart >= 0 - ? 'grabbing' : 'grab'; - } - - function handleScrubStart(e: PointerEvent){ - e.persist(); - // console.log(`start: ${pointerStart}` ); - setState((curr)=>({ - ...curr, - pointerStart: e.clientX - })); - } - function handleScrubEnd(e: PointerEvent){ - e.persist(); - // console.log(`end` ); - setState((curr)=>({ - ...curr, - pointerStart: -1, - leftOffset: left, - })); - console.log(`${duration} * ${left} / ${boundingWidth} = ${duration * left / boundingWidth}`) - onChange(({ - trimStartSeconds: duration * left / boundingWidth, - trimEndSeconds: duration * left / boundingWidth + trimDuration, - })) - } - function handleScrubMove(e: PointerEvent){ - e.persist(); - if(pointerStart >= 0 && pointerStart!==null && ref.current){ - // console.log(`${e.clientX} - ${pointerStart} = ${e.clientX-pointerStart}`) - let newLeft = leftOffset + e.clientX - pointerStart; - // console.log(`${newLeft} + ${boundingWidth * widthPercent/100} > ${boundingWidth}`) - if (newLeft + width > boundingWidth) { - newLeft = boundingWidth - width; - }else if(newLeft < 0) { - newLeft = 0; - } - setState((curr)=>({ - ...curr, - left: newLeft - })); - } - } - return( -
- -
- ); } +export const TrimScrubber = (props:TrimScrubberPropsI)=>{ + + const TrimScrubberWithScrubbing = + withScrubbing(() => { + return( +
+ +
+ ); + }); + + return ( + + ) +} + + diff --git a/packages/fakeyou.com/src/components/common/VideoQuickTrim/VideoQuickTrim.tsx b/packages/fakeyou.com/src/components/common/VideoQuickTrim/VideoQuickTrim.tsx index 6f62433ba3..1658d91220 100644 --- a/packages/fakeyou.com/src/components/common/VideoQuickTrim/VideoQuickTrim.tsx +++ b/packages/fakeyou.com/src/components/common/VideoQuickTrim/VideoQuickTrim.tsx @@ -18,7 +18,7 @@ import { VideoFakeyouProps } from "../VideoFakeyou/VideoFakeyou"; import { QuickTrimData, TrimStates, PlaybarStates } from "./types"; import { formatSecondsToHHMMSSCS } from "./helpers"; -import TrimScrbber from "./TrimScrubber"; +import {TrimScrubber} from "./TrimScrubber"; import './styles.scss' interface VideoQuickTrimProps extends VideoFakeyouProps { @@ -225,10 +225,10 @@ export default memo(function VideoQuickTrim({ {trimScrubberWidth > 0 && playbarWidth > 0 && maxDuration > 0 && - number; //return scrubber location as % + onScrubChanges?: ()=>number; //return scrubber location as % +} +type withSrcubbingStates = { + currLeftOffset: number, + prevLeftOffset: number; + pointerStartPos: number; +} + +export const withScrubbing =

(Component: React.ComponentType

) => ({ + boundingWidth, + scrubberWidth, + styleOverride = {}, + onScrubEnds, + onScrubChanges, + ...rest +}: withScrubbingPropsI) => { + // return (props: Omit)=>{ + const ref = useRef(null) + const [{ + currLeftOffset, prevLeftOffset, pointerStartPos + }, setStates] = useState({ + currLeftOffset: 0, // in pixels + prevLeftOffset: 0, //in pixels + pointerStartPos: -1 // negative denotes pointer not engaged + }); + + function handleScrubStart(e: PointerEvent){ + e.persist(); + console.log(`start: ${pointerStartPos} -> ${e.clientX}` ); + setStates((curr)=>({ + ...curr, + pointerStartPos: e.clientX + })); + } + function handleScrubEnd(e: PointerEvent){ + e.persist(); + console.log(`end` ); + setStates((curr)=>({ + ...curr, + pointerStartPos: -1, + prevLeftOffset: currLeftOffset, + })); + } + function handleScrubMove(e: PointerEvent){ + e.persist(); + if(pointerStartPos >= 0 && pointerStartPos!==null){ + let newLeftOffset = prevLeftOffset + e.clientX - pointerStartPos; + if (newLeftOffset + scrubberWidth > boundingWidth) { + newLeftOffset = boundingWidth - scrubberWidth; + }else if(newLeftOffset < 0) { + newLeftOffset = 0; + } + setStates((curr)=>({ + ...curr, + currLeftOffset: newLeftOffset + })); + } + } + return( +

=0 ? 'grabbing': 'grab', + ...styleOverride + }} + onPointerDown={handleScrubStart} + onPointerUp={handleScrubEnd} + onPointerLeave={handleScrubEnd} + onPointerMove={handleScrubMove} + > + +
+ ); +// } +};