feat: 쇼츠 페이지에서 마우스로 끌어 넘기기(한 화면 15% 넘게 끌면 다음·이전), 영상을 누르면 일시정지·재생
This commit is contained in:
1 parent
81dc101239
commit
0db4b130f0
1 file changed
+57
-2
@@ -1,5 +1,5 @@
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { type CSSProperties, useEffect, useEffectEvent, useLayoutEffect, useRef, useState } from 'react'
|
||||
import { type CSSProperties, type PointerEvent as ReactPointerEvent, useEffect, useEffectEvent, useLayoutEffect, useRef, useState } from 'react'
|
||||
import { Link, useNavigate, useParams } from 'react-router'
|
||||
import { listShortsOptions } from '../api/client/@tanstack/react-query.gen'
|
||||
import type { ShortItem } from '../api/client'
|
||||
@@ -140,6 +140,8 @@ function ShortsViewer({ items, start, onClose, onAt }: { items: ShortItem[]; sta
|
||||
const frames = useRef(new Map<number, HTMLIFrameElement>())
|
||||
const atRef = useRef(start)
|
||||
const soundRef = useRef(false)
|
||||
// 누르기로 멈춘 상태. 다른 영상으로 넘어가면 다시 재생 중으로
|
||||
const paused = useRef(false)
|
||||
|
||||
// 처음 한 번: 시작 영상으로 맞추고, 뒤 페이지 스크롤을 잠근다
|
||||
const startRef = useRef(start)
|
||||
@@ -201,6 +203,7 @@ function ShortsViewer({ items, start, onClose, onAt }: { items: ShortItem[]; sta
|
||||
useEffect(() => {
|
||||
if (atRef.current !== at) moved(items[at])
|
||||
atRef.current = at
|
||||
paused.current = false
|
||||
for (const [i, f] of frames.current) {
|
||||
if (i === at) {
|
||||
command(f, soundRef.current ? 'unMute' : 'mute')
|
||||
@@ -212,6 +215,46 @@ function ShortsViewer({ items, start, onClose, onAt }: { items: ShortItem[]; sta
|
||||
}
|
||||
}, [at, items])
|
||||
|
||||
// 마우스로도 끌어 넘긴다: 영상 위는 유튜브 iframe 이 마우스를 가져가서, 그 위에 투명한 판을 깔고 끈 만큼 목록을 옮긴다.
|
||||
// 손가락은 브라우저 스크롤에 맡기고, 끌지 않고 누르기만 하면 재생·일시정지
|
||||
const drag = useRef<{ y: number; top: number; moved: boolean } | null>(null)
|
||||
const lastPointer = useRef('')
|
||||
const [dragging, setDragging] = useState(false)
|
||||
const togglePause = () => {
|
||||
paused.current = !paused.current
|
||||
command(frames.current.get(atRef.current), paused.current ? 'pauseVideo' : 'playVideo')
|
||||
}
|
||||
const onPointerDown = (e: ReactPointerEvent<HTMLDivElement>) => {
|
||||
lastPointer.current = e.pointerType
|
||||
const box = list.current
|
||||
if (e.pointerType !== 'mouse' || !box) return
|
||||
e.currentTarget.setPointerCapture(e.pointerId)
|
||||
drag.current = { y: e.clientY, top: box.scrollTop, moved: false }
|
||||
setDragging(true)
|
||||
}
|
||||
const onPointerMove = (e: ReactPointerEvent<HTMLDivElement>) => {
|
||||
const d = drag.current
|
||||
const box = list.current
|
||||
if (!d || !box) return
|
||||
const dy = e.clientY - d.y
|
||||
if (Math.abs(dy) > 6) d.moved = true
|
||||
box.scrollTop = d.top - dy
|
||||
}
|
||||
const onPointerUp = (e: ReactPointerEvent<HTMLDivElement>) => {
|
||||
const d = drag.current
|
||||
const box = list.current
|
||||
drag.current = null
|
||||
setDragging(false)
|
||||
if (!d || !box) return
|
||||
if (!d.moved) return togglePause()
|
||||
// 한 화면의 15% 넘게 끌면 다음·이전 영상으로, 아니면 제자리로
|
||||
const h = box.clientHeight
|
||||
const dy = e.clientY - d.y
|
||||
const base = Math.round(d.top / h)
|
||||
const target = Math.min(items.length - 1, Math.max(0, base + (dy < -h * 0.15 ? 1 : dy > h * 0.15 ? -1 : 0)))
|
||||
box.scrollTo({ top: target * h, behavior: 'smooth' })
|
||||
}
|
||||
|
||||
const toggleSound = () => {
|
||||
const on = !sound
|
||||
soundRef.current = on
|
||||
@@ -244,7 +287,7 @@ function ShortsViewer({ items, start, onClose, onAt }: { items: ShortItem[]; sta
|
||||
{sound ? '소리 끄기' : '소리 켜기'}
|
||||
</button>
|
||||
</div>
|
||||
<div ref={list} className="relative min-h-0 flex-1 snap-y snap-mandatory overflow-y-auto [scrollbar-width:none]">
|
||||
<div ref={list} className={cx('relative min-h-0 flex-1 overflow-y-auto [scrollbar-width:none]', !dragging && 'snap-y snap-mandatory')}>
|
||||
{items.map((s, i) => (
|
||||
<div key={s.id} data-i={i} className="relative grid h-full snap-start place-items-center">
|
||||
<div className="relative" style={FIT}>
|
||||
@@ -272,6 +315,18 @@ function ShortsViewer({ items, start, onClose, onAt }: { items: ShortItem[]; sta
|
||||
className={cx('absolute inset-0 size-full border-0 transition-opacity duration-200', !ready.has(i) && 'opacity-0')}
|
||||
/>
|
||||
)}
|
||||
<div
|
||||
aria-hidden="true"
|
||||
onPointerDown={onPointerDown}
|
||||
onPointerMove={onPointerMove}
|
||||
onPointerUp={onPointerUp}
|
||||
onPointerCancel={() => {
|
||||
drag.current = null
|
||||
setDragging(false)
|
||||
}}
|
||||
onClick={() => lastPointer.current !== 'mouse' && togglePause()}
|
||||
className={cx('absolute inset-0 touch-pan-y select-none', dragging ? 'cursor-grabbing' : 'cursor-grab')}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
|
||||
Reference in new issue
Block a user