mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-02 05:54:44 +00:00
Merge pull request #174 from shumaiOne/refactor/file-viewer-registry
refactor(webui): modularize file viewer components into registry-driven architecture
This commit is contained in:
@@ -17,7 +17,7 @@ import Markdown from 'react-markdown'
|
|||||||
import { Avatar, AvatarFallback, AvatarImage } from '../ui/avatar'
|
import { Avatar, AvatarFallback, AvatarImage } from '../ui/avatar'
|
||||||
import { DrawAnnotation } from '../ui/icons'
|
import { DrawAnnotation } from '../ui/icons'
|
||||||
import { Skeleton } from '../ui/skeleton'
|
import { Skeleton } from '../ui/skeleton'
|
||||||
import { formatTimecode } from '../viewers/utils'
|
import { formatTimecode } from '../viewers/video/utils'
|
||||||
import { m } from '@/ui/paraglide/messages.js'
|
import { m } from '@/ui/paraglide/messages.js'
|
||||||
import { useUiStore } from '@/ui/stores/ui'
|
import { useUiStore } from '@/ui/stores/ui'
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ import { Button } from '../ui/button'
|
|||||||
import { DrawAnnotation } from '../ui/icons'
|
import { DrawAnnotation } from '../ui/icons'
|
||||||
import { Popover, PopoverContent, PopoverTrigger } from '../ui/popover'
|
import { Popover, PopoverContent, PopoverTrigger } from '../ui/popover'
|
||||||
import { ProgressCircle } from '../ui/progress-circle'
|
import { ProgressCircle } from '../ui/progress-circle'
|
||||||
import { formatTimecode } from '../viewers/utils'
|
import { formatTimecode } from '../viewers/video/utils'
|
||||||
import { useUiStore } from '@/ui/stores/ui'
|
import { useUiStore } from '@/ui/stores/ui'
|
||||||
|
|
||||||
type UploadingFile = {
|
type UploadingFile = {
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import { ImageControlBar } from '@/ui/components/viewers/image-control-bar'
|
import { ImageControlBar } from '@/ui/components/viewers/image/image-control-bar'
|
||||||
import {
|
import {
|
||||||
VideoControlBar,
|
VideoControlBar,
|
||||||
type DisplayTranscode,
|
type DisplayTranscode,
|
||||||
type PlayerState,
|
type PlayerState,
|
||||||
} from '@/ui/components/viewers/video-control-bar'
|
} from '@/ui/components/viewers/video/video-control-bar'
|
||||||
import type { AssetInfo } from '@shumai/dtos'
|
import type { AssetInfo } from '@shumai/dtos'
|
||||||
import { Maximize, Minimize } from 'lucide-react'
|
import { Maximize, Minimize } from 'lucide-react'
|
||||||
import type { PaneReportedState } from './types'
|
import type { PaneReportedState } from './types'
|
||||||
@@ -85,10 +85,10 @@ export function CompareControlBar({
|
|||||||
toggleMute={onToggleMute}
|
toggleMute={onToggleMute}
|
||||||
handleVolumeChange={onVolumeChange}
|
handleVolumeChange={onVolumeChange}
|
||||||
changePlaybackRate={onChangePlaybackRate}
|
changePlaybackRate={onChangePlaybackRate}
|
||||||
changeResolution={(res) => onChangeResolution(res.resolution)}
|
changeResolution={(res: DisplayTranscode) => onChangeResolution(res.resolution)}
|
||||||
handleDownload={(key) => onDownload(key)}
|
handleDownload={(key?: string) => onDownload(key)}
|
||||||
toggleFullScreen={onToggleFullScreen}
|
toggleFullScreen={onToggleFullScreen}
|
||||||
onZoomChange={(nz) => (nz >= activeState.zoom ? onZoomIn() : onZoomOut())}
|
onZoomChange={(nz: number) => (nz >= activeState.zoom ? onZoomIn() : onZoomOut())}
|
||||||
onZoomReset={onFit}
|
onZoomReset={onFit}
|
||||||
frameRate={v.frameRate}
|
frameRate={v.frameRate}
|
||||||
totalFrames={v.totalFrames}
|
totalFrames={v.totalFrames}
|
||||||
|
|||||||
@@ -4,12 +4,10 @@ import type { AssetInfo } from '@shumai/dtos'
|
|||||||
import { useQuery } from '@tanstack/react-query'
|
import { useQuery } from '@tanstack/react-query'
|
||||||
import { Loader2 } from 'lucide-react'
|
import { Loader2 } from 'lucide-react'
|
||||||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
||||||
|
import { getViewerForFile } from '@/ui/components/viewers/registry'
|
||||||
import { CompareControlBar } from './compare-control-bar'
|
import { CompareControlBar } from './compare-control-bar'
|
||||||
import { CompareImagePane } from './compare-image-pane'
|
|
||||||
import { ComparePaneTopbar } from './compare-pane-topbar'
|
import { ComparePaneTopbar } from './compare-pane-topbar'
|
||||||
import { isSameType, kindOf } from './compare-utils'
|
import type { ComparePaneHandle, CompareSide, PaneReportedState } from './types'
|
||||||
import { CompareVideoPane } from './compare-video-pane'
|
|
||||||
import type { ComparePaneHandle, CompareSide, PaneKind, PaneReportedState } from './types'
|
|
||||||
|
|
||||||
interface VersionItem {
|
interface VersionItem {
|
||||||
id: string
|
id: string
|
||||||
@@ -89,9 +87,9 @@ export function CompareViewer({
|
|||||||
const { data: leftAsset, isLoading: leftLoading } = useCompareAsset(leftId, isPublic, shareId)
|
const { data: leftAsset, isLoading: leftLoading } = useCompareAsset(leftId, isPublic, shareId)
|
||||||
const { data: rightAsset, isLoading: rightLoading } = useCompareAsset(rightId, isPublic, shareId)
|
const { data: rightAsset, isLoading: rightLoading } = useCompareAsset(rightId, isPublic, shareId)
|
||||||
|
|
||||||
const leftKind = kindOf(leftAsset?.mediaType)
|
const leftDef = getViewerForFile(leftAsset)
|
||||||
const rightKind = kindOf(rightAsset?.mediaType)
|
const rightDef = getViewerForFile(rightAsset)
|
||||||
const sameType = isSameType(leftKind, rightKind)
|
const sameType = !!leftAsset && !!rightAsset && leftDef.id === rightDef.id
|
||||||
|
|
||||||
const activeAsset = activeSide === 'left' ? leftAsset : rightAsset
|
const activeAsset = activeSide === 'left' ? leftAsset : rightAsset
|
||||||
const activeState = activeSide === 'left' ? leftState : rightState
|
const activeState = activeSide === 'left' ? leftState : rightState
|
||||||
@@ -219,51 +217,41 @@ export function CompareViewer({
|
|||||||
[activeAsset?.media?.videoPreview?.url],
|
[activeAsset?.media?.videoPreview?.url],
|
||||||
)
|
)
|
||||||
|
|
||||||
const renderPane = (side: CompareSide, asset: AssetInfo, kind: PaneKind) => {
|
const renderPane = (side: CompareSide, asset: AssetInfo) => {
|
||||||
|
const def = getViewerForFile(asset)
|
||||||
|
const ComparePane = def?.comparePane
|
||||||
|
|
||||||
|
if (!ComparePane) {
|
||||||
|
return (
|
||||||
|
<div className="flex flex-1 items-center justify-center bg-gray-100 dark:bg-gray-950">
|
||||||
|
<p className="text-muted-foreground">Preview unavailable</p>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
const isActive = activeSide === side
|
const isActive = activeSide === side
|
||||||
const paneRef = side === 'left' ? leftRef : rightRef
|
const paneRef = side === 'left' ? leftRef : rightRef
|
||||||
const onStateChange = side === 'left' ? handleStateChangeLeft : handleStateChangeRight
|
const onStateChange = side === 'left' ? handleStateChangeLeft : handleStateChangeRight
|
||||||
const onUserPan = side === 'left' ? handleUserPanLeft : handleUserPanRight
|
const onUserPan = side === 'left' ? handleUserPanLeft : handleUserPanRight
|
||||||
const paneAnnotations = isActive ? annotations : []
|
const paneAnnotations = isActive ? annotations : []
|
||||||
|
|
||||||
if (kind === 'video') {
|
|
||||||
return (
|
|
||||||
<CompareVideoPane
|
|
||||||
key={asset.id}
|
|
||||||
ref={paneRef}
|
|
||||||
file={asset}
|
|
||||||
shareId={shareId}
|
|
||||||
isActive={isActive}
|
|
||||||
muted={isActive ? userMuted : true}
|
|
||||||
volume={userVolume}
|
|
||||||
annotations={paneAnnotations}
|
|
||||||
onStateChange={onStateChange}
|
|
||||||
onActivate={() => onActiveSideChange(side)}
|
|
||||||
onRequestTogglePlay={handleRequestTogglePlay}
|
|
||||||
onTimeUpdate={onTimeUpdate}
|
|
||||||
onPlay={onPlay}
|
|
||||||
/>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if (kind === 'image') {
|
|
||||||
return (
|
|
||||||
<CompareImagePane
|
|
||||||
key={asset.id}
|
|
||||||
ref={paneRef}
|
|
||||||
file={asset}
|
|
||||||
shareId={shareId}
|
|
||||||
isActive={isActive}
|
|
||||||
annotations={paneAnnotations}
|
|
||||||
onStateChange={onStateChange}
|
|
||||||
onUserPan={onUserPan}
|
|
||||||
onActivate={() => onActiveSideChange(side)}
|
|
||||||
/>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-1 items-center justify-center bg-gray-100 dark:bg-gray-950">
|
<ComparePane
|
||||||
<p className="text-muted-foreground">Preview unavailable</p>
|
key={asset.id}
|
||||||
</div>
|
ref={paneRef}
|
||||||
|
file={asset}
|
||||||
|
shareId={shareId}
|
||||||
|
isActive={isActive}
|
||||||
|
annotations={paneAnnotations}
|
||||||
|
onStateChange={onStateChange}
|
||||||
|
onUserPan={onUserPan}
|
||||||
|
onActivate={() => onActiveSideChange(side)}
|
||||||
|
muted={isActive ? userMuted : true}
|
||||||
|
volume={userVolume}
|
||||||
|
onPlay={onPlay}
|
||||||
|
onTimeUpdate={onTimeUpdate}
|
||||||
|
onRequestTogglePlay={handleRequestTogglePlay}
|
||||||
|
/>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -290,7 +278,7 @@ export function CompareViewer({
|
|||||||
)}
|
)}
|
||||||
<div className="relative flex min-h-0 flex-1">
|
<div className="relative flex min-h-0 flex-1">
|
||||||
{leftAsset ? (
|
{leftAsset ? (
|
||||||
renderPane('left', leftAsset, leftKind)
|
renderPane('left', leftAsset)
|
||||||
) : (
|
) : (
|
||||||
<div className="flex flex-1 items-center justify-center">
|
<div className="flex flex-1 items-center justify-center">
|
||||||
<Loader2 className="h-6 w-6 animate-spin text-foreground" />
|
<Loader2 className="h-6 w-6 animate-spin text-foreground" />
|
||||||
@@ -316,7 +304,7 @@ export function CompareViewer({
|
|||||||
)}
|
)}
|
||||||
<div className="relative flex min-h-0 flex-1">
|
<div className="relative flex min-h-0 flex-1">
|
||||||
{rightAsset ? (
|
{rightAsset ? (
|
||||||
renderPane('right', rightAsset, rightKind)
|
renderPane('right', rightAsset)
|
||||||
) : (
|
) : (
|
||||||
<div className="flex flex-1 items-center justify-center">
|
<div className="flex flex-1 items-center justify-center">
|
||||||
<Loader2 className="h-6 w-6 animate-spin text-foreground" />
|
<Loader2 className="h-6 w-6 animate-spin text-foreground" />
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { File, Folder } from 'lucide-react'
|
import { File, Folder } from 'lucide-react'
|
||||||
import { SpriteScrubber } from '../sprite-scrubber'
|
import { SpriteScrubber } from '../sprite-scrubber'
|
||||||
import { formatTime } from '../viewers/utils'
|
import { formatTime } from '../viewers/video/utils'
|
||||||
|
|
||||||
type FilePreviewItem = {
|
type FilePreviewItem = {
|
||||||
type?: string | null
|
type?: string | null
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import { ChatInput } from './chat/message-input'
|
|||||||
import FieldRenderer from './field-renderer'
|
import FieldRenderer from './field-renderer'
|
||||||
import { GuestIdentityPopup } from './guest-identity-popup'
|
import { GuestIdentityPopup } from './guest-identity-popup'
|
||||||
import { ScrollArea } from './ui/scroll-area'
|
import { ScrollArea } from './ui/scroll-area'
|
||||||
|
import { getViewerForFile } from '@/ui/components/viewers/registry'
|
||||||
|
|
||||||
interface FileViewerRightSidebarProps {
|
interface FileViewerRightSidebarProps {
|
||||||
teamId: string
|
teamId: string
|
||||||
@@ -88,7 +89,8 @@ export function FileViewerRightSidebar({
|
|||||||
enabled: !!projectId && !readOnly,
|
enabled: !!projectId && !readOnly,
|
||||||
})
|
})
|
||||||
|
|
||||||
const isAiEnabled = file?.mediaType?.startsWith('image/') || file?.mediaType?.startsWith('video/')
|
const viewerDef = getViewerForFile(file)
|
||||||
|
const isAiEnabled = !!viewerDef?.commentsConfig?.hasAiBots
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
const enabledBots = isAiEnabled ? (bots as any[]) || [] : []
|
const enabledBots = isAiEnabled ? (bots as any[]) || [] : []
|
||||||
@@ -355,12 +357,12 @@ export function FileViewerRightSidebar({
|
|||||||
onViewAttachment={setViewingAttachment}
|
onViewAttachment={setViewingAttachment}
|
||||||
hasReplies={!!comment.replies?.length}
|
hasReplies={!!comment.replies?.length}
|
||||||
frameRate={
|
frameRate={
|
||||||
file?.mediaType?.startsWith('video/')
|
viewerDef?.commentsConfig?.hasTimestamp
|
||||||
? file?.media?.metadata?.frameRate || 30
|
? file?.media?.metadata?.frameRate || 30
|
||||||
: undefined
|
: undefined
|
||||||
}
|
}
|
||||||
startTimecode={
|
startTimecode={
|
||||||
file?.mediaType?.startsWith('video/')
|
viewerDef?.commentsConfig?.hasTimestamp
|
||||||
? file?.media?.metadata?.startTimecode
|
? file?.media?.metadata?.startTimecode
|
||||||
: undefined
|
: undefined
|
||||||
}
|
}
|
||||||
@@ -381,12 +383,12 @@ export function FileViewerRightSidebar({
|
|||||||
hasReplies={false}
|
hasReplies={false}
|
||||||
isLastReply={index === (comment.replies?.length ?? 0) - 1}
|
isLastReply={index === (comment.replies?.length ?? 0) - 1}
|
||||||
frameRate={
|
frameRate={
|
||||||
file?.mediaType?.startsWith('video/')
|
viewerDef?.commentsConfig?.hasTimestamp
|
||||||
? file?.media?.metadata?.frameRate || 30
|
? file?.media?.metadata?.frameRate || 30
|
||||||
: undefined
|
: undefined
|
||||||
}
|
}
|
||||||
startTimecode={
|
startTimecode={
|
||||||
file?.mediaType?.startsWith('video/')
|
viewerDef?.commentsConfig?.hasTimestamp
|
||||||
? file?.media?.metadata?.startTimecode
|
? file?.media?.metadata?.startTimecode
|
||||||
: undefined
|
: undefined
|
||||||
}
|
}
|
||||||
@@ -417,14 +419,14 @@ export function FileViewerRightSidebar({
|
|||||||
bots={enabledBots}
|
bots={enabledBots}
|
||||||
hideAnnotationControl={hideAnnotationControl}
|
hideAnnotationControl={hideAnnotationControl}
|
||||||
disableMentions={isPublic}
|
disableMentions={isPublic}
|
||||||
currentTime={file?.mediaType?.startsWith('video/') ? currentTime : undefined}
|
currentTime={viewerDef?.commentsConfig?.hasTimestamp ? currentTime : undefined}
|
||||||
frameRate={
|
frameRate={
|
||||||
file?.mediaType?.startsWith('video/')
|
viewerDef?.commentsConfig?.hasTimestamp
|
||||||
? file?.media?.metadata?.frameRate || 30
|
? file?.media?.metadata?.frameRate || 30
|
||||||
: undefined
|
: undefined
|
||||||
}
|
}
|
||||||
startTimecode={
|
startTimecode={
|
||||||
file?.mediaType?.startsWith('video/')
|
viewerDef?.commentsConfig?.hasTimestamp
|
||||||
? file?.media?.metadata?.startTimecode
|
? file?.media?.metadata?.startTimecode
|
||||||
: undefined
|
: undefined
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,33 +1,14 @@
|
|||||||
import { useScreenSize } from '@/ui/hooks/useScreenSize'
|
import { getViewerForFile } from './viewers/registry'
|
||||||
import { client } from '@/ui/api/client'
|
import type { FileViewerProps, MediaController } from './viewers/types'
|
||||||
import { getBestTranscode } from '@/ui/lib/media'
|
import React, { RefObject } from 'react'
|
||||||
import type { AssetInfo } from '@shumai/dtos'
|
|
||||||
import { Download } from 'lucide-react'
|
|
||||||
import type { ReactNode, RefObject } from 'react'
|
|
||||||
import { useCallback, useEffect, useRef, useState } from 'react'
|
|
||||||
import type Player from 'video.js/dist/types/player'
|
|
||||||
import DrawingCanvas from './drawing-canvas'
|
|
||||||
import VideoPlayer from './viewers/video-player'
|
|
||||||
import { ImageControlBar } from './viewers/image-control-bar'
|
|
||||||
|
|
||||||
import { useAnnotationStore } from '@/ui/stores/annotation-store'
|
interface OuterFileViewerProps extends FileViewerProps {
|
||||||
import type { Annotation } from '@/ui/types'
|
mediaControllerRef?: RefObject<MediaController | null>
|
||||||
|
|
||||||
type FileViewerProps = {
|
|
||||||
file: AssetInfo
|
|
||||||
videoRef?: RefObject<Player | null>
|
|
||||||
onPlay?: () => void
|
|
||||||
onPause?: () => void
|
|
||||||
onTimeUpdate?: (time: number) => void
|
|
||||||
annotations?: Annotation[]
|
|
||||||
startTime?: number
|
|
||||||
shareId?: string
|
|
||||||
children?: ReactNode
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function FileViewer({
|
export function FileViewer({
|
||||||
file,
|
file,
|
||||||
videoRef,
|
mediaControllerRef,
|
||||||
onPlay,
|
onPlay,
|
||||||
onPause,
|
onPause,
|
||||||
onTimeUpdate,
|
onTimeUpdate,
|
||||||
@@ -35,271 +16,22 @@ export function FileViewer({
|
|||||||
startTime,
|
startTime,
|
||||||
shareId,
|
shareId,
|
||||||
children,
|
children,
|
||||||
}: FileViewerProps) {
|
}: OuterFileViewerProps) {
|
||||||
const { width: screenWidth } = useScreenSize()
|
const viewerDef = getViewerForFile(file)
|
||||||
const isImage = file.mediaType?.startsWith('image/')
|
const ViewerComponent = viewerDef.viewer
|
||||||
const isVideo = file.mediaType?.startsWith('video/')
|
|
||||||
|
|
||||||
// Annotation Store
|
|
||||||
const {
|
|
||||||
isDrawing,
|
|
||||||
currentTool,
|
|
||||||
currentColor,
|
|
||||||
addAnnotation,
|
|
||||||
annotations: draftAnnotations,
|
|
||||||
reset: resetAnnotations,
|
|
||||||
} = useAnnotationStore()
|
|
||||||
|
|
||||||
// Reset annotations when file changes
|
|
||||||
useEffect(() => {
|
|
||||||
resetAnnotations()
|
|
||||||
}, [file.id, resetAnnotations])
|
|
||||||
|
|
||||||
// State for Image Viewer
|
|
||||||
const [containerSize, setContainerSize] = useState({ width: 0, height: 0 })
|
|
||||||
const [zoom, setZoom] = useState(1)
|
|
||||||
const [pan, setPan] = useState({ x: 0, y: 0 })
|
|
||||||
const [copied, setCopied] = useState(false)
|
|
||||||
|
|
||||||
const observerRef = useRef<ResizeObserver | null>(null)
|
|
||||||
|
|
||||||
const containerRef = useCallback((node: HTMLDivElement | null) => {
|
|
||||||
if (observerRef.current) {
|
|
||||||
observerRef.current.disconnect()
|
|
||||||
observerRef.current = null
|
|
||||||
}
|
|
||||||
|
|
||||||
if (node !== null) {
|
|
||||||
const observer = new ResizeObserver((entries) => {
|
|
||||||
const entry = entries[0]
|
|
||||||
if (entry) {
|
|
||||||
setContainerSize({
|
|
||||||
width: entry.contentRect.width,
|
|
||||||
height: entry.contentRect.height,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
observer.observe(node)
|
|
||||||
observerRef.current = observer
|
|
||||||
}
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
const imgW = file.media?.metadata?.originalWidth ?? 1920
|
|
||||||
const imgH = file.media?.metadata?.originalHeight ?? 1080
|
|
||||||
const conW = containerSize.width
|
|
||||||
const conH = containerSize.height
|
|
||||||
|
|
||||||
let baseScale = 1
|
|
||||||
|
|
||||||
if (conW > 0 && conH > 0) {
|
|
||||||
baseScale = Math.min(conW / imgW, conH / imgH)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reset/Fit logic
|
|
||||||
useEffect(() => {
|
|
||||||
if (conW > 0 && conH > 0 && isImage) {
|
|
||||||
setZoom(baseScale)
|
|
||||||
// Center
|
|
||||||
const x = (conW - imgW * baseScale) / 2
|
|
||||||
const y = (conH - imgH * baseScale) / 2
|
|
||||||
setPan({ x, y })
|
|
||||||
}
|
|
||||||
}, [file.id, conW, conH, isImage, baseScale, imgW, imgH])
|
|
||||||
|
|
||||||
const handleZoom = (factor: number) => {
|
|
||||||
const cx = conW / 2
|
|
||||||
const cy = conH / 2
|
|
||||||
|
|
||||||
// Limit min/max zoom if needed?
|
|
||||||
const newZoom = zoom * factor
|
|
||||||
if (newZoom < 0.01 || newZoom > 50) return
|
|
||||||
|
|
||||||
const newPanX = cx - (cx - pan.x) * (newZoom / zoom)
|
|
||||||
const newPanY = cy - (cy - pan.y) * (newZoom / zoom)
|
|
||||||
|
|
||||||
setZoom(newZoom)
|
|
||||||
setPan({ x: newPanX, y: newPanY })
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleFit = () => {
|
|
||||||
setZoom(baseScale)
|
|
||||||
const x = (conW - imgW * baseScale) / 2
|
|
||||||
const y = (conH - imgH * baseScale) / 2
|
|
||||||
setPan({ x, y })
|
|
||||||
}
|
|
||||||
|
|
||||||
let bestUrl = file.media?.original?.downloadUrl
|
|
||||||
|
|
||||||
if (isImage) {
|
|
||||||
const bestTranscode = getBestTranscode(file.media?.imageTranscodes, screenWidth)
|
|
||||||
if (bestTranscode?.url) {
|
|
||||||
bestUrl = bestTranscode.url
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleDownload = async () => {
|
|
||||||
const key = file.media?.original?.key
|
|
||||||
if (!key || !file.id) return
|
|
||||||
try {
|
|
||||||
const res = shareId
|
|
||||||
? await client.api.shares[':shareId'].files[':fileId']['download-url'].$post({
|
|
||||||
param: { shareId, fileId: file.id },
|
|
||||||
json: { key },
|
|
||||||
})
|
|
||||||
: await client.api.files['download-url'].$post({
|
|
||||||
json: { key, assetId: file.id },
|
|
||||||
})
|
|
||||||
if (!res.ok) return
|
|
||||||
const { url } = await res.json()
|
|
||||||
const link = document.createElement('a')
|
|
||||||
link.href = url
|
|
||||||
link.download = ''
|
|
||||||
document.body.appendChild(link)
|
|
||||||
link.click()
|
|
||||||
document.body.removeChild(link)
|
|
||||||
} catch {
|
|
||||||
// silently fail
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleCopy = async () => {
|
|
||||||
const webpUrl = bestUrl
|
|
||||||
if (!webpUrl) return
|
|
||||||
try {
|
|
||||||
// To support Safari and iOS, we must invoke navigator.clipboard.write
|
|
||||||
// synchronously inside the click handler. We can pass a promise to ClipboardItem
|
|
||||||
// which resolves to the Blob asynchronously.
|
|
||||||
const copyPromise = (async () => {
|
|
||||||
const response = await fetch(webpUrl)
|
|
||||||
const blob = await response.blob()
|
|
||||||
|
|
||||||
const img = new Image()
|
|
||||||
img.crossOrigin = 'anonymous'
|
|
||||||
const objectUrl = URL.createObjectURL(blob)
|
|
||||||
img.src = objectUrl
|
|
||||||
|
|
||||||
try {
|
|
||||||
await new Promise((resolve, reject) => {
|
|
||||||
img.onload = resolve
|
|
||||||
img.onerror = (e) => reject(new Error('Failed to load image: ' + String(e)))
|
|
||||||
})
|
|
||||||
|
|
||||||
const canvas = document.createElement('canvas')
|
|
||||||
canvas.width = img.width
|
|
||||||
canvas.height = img.height
|
|
||||||
const ctx = canvas.getContext('2d')
|
|
||||||
if (!ctx) throw new Error('Could not get canvas context')
|
|
||||||
ctx.drawImage(img, 0, 0)
|
|
||||||
|
|
||||||
const pngBlob = await new Promise<Blob | null>((resolve) => {
|
|
||||||
canvas.toBlob(resolve, 'image/png')
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!pngBlob) throw new Error('Failed to convert image to PNG')
|
|
||||||
return pngBlob
|
|
||||||
} finally {
|
|
||||||
URL.revokeObjectURL(objectUrl)
|
|
||||||
}
|
|
||||||
})()
|
|
||||||
|
|
||||||
await navigator.clipboard.write([
|
|
||||||
new ClipboardItem({
|
|
||||||
'image/png': copyPromise,
|
|
||||||
}),
|
|
||||||
])
|
|
||||||
setCopied(true)
|
|
||||||
setTimeout(() => setCopied(false), 2000)
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Failed to copy image to clipboard:', err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const isUnsupported = !bestUrl || (!isImage && !isVideo)
|
|
||||||
|
|
||||||
if (isUnsupported) {
|
|
||||||
return (
|
|
||||||
<div className="flex flex-col flex-1 h-full overflow-hidden bg-gray-100 dark:bg-gray-950 relative">
|
|
||||||
<div className="flex-1 flex flex-col-reverse md:flex-row min-h-0 relative">
|
|
||||||
{children}
|
|
||||||
<div className="flex-1 flex items-center justify-center">
|
|
||||||
<p className="text-muted-foreground">Preview unavailable</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div className="relative px-4 py-3 bg-card border-t border-gray-200 dark:border-gray-700 z-10 flex items-center justify-end gap-2 transition-colors duration-200">
|
|
||||||
<button
|
|
||||||
onClick={handleDownload}
|
|
||||||
disabled={!file.media?.original?.key}
|
|
||||||
className="flex items-center gap-1.5 text-xs font-medium px-3 py-1.5 rounded bg-gray-200/50 dark:bg-white/10 hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-200 transition-colors border border-transparent disabled:opacity-50 animate-in fade-in zoom-in-95 duration-200"
|
|
||||||
title="Download original file"
|
|
||||||
>
|
|
||||||
<Download size={14} />
|
|
||||||
Download
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Combine saved annotations and draft annotations
|
|
||||||
const displayAnnotations = [...(annotations || []), ...draftAnnotations]
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-col flex-1 h-full overflow-hidden bg-gray-100 dark:bg-gray-950 relative">
|
<ViewerComponent
|
||||||
{isImage && (
|
ref={mediaControllerRef}
|
||||||
<>
|
file={file}
|
||||||
<div className="flex-1 flex flex-col-reverse md:flex-row min-h-0 relative">
|
onPlay={onPlay}
|
||||||
{children}
|
onPause={onPause}
|
||||||
<div ref={containerRef} className="flex-1 relative overflow-hidden">
|
onTimeUpdate={onTimeUpdate}
|
||||||
<DrawingCanvas
|
annotations={annotations}
|
||||||
width={conW}
|
startTime={startTime}
|
||||||
height={conH}
|
shareId={shareId}
|
||||||
mediaDimensions={{
|
>
|
||||||
width: imgW,
|
{children}
|
||||||
height: imgH,
|
</ViewerComponent>
|
||||||
}}
|
|
||||||
imageUrl={bestUrl}
|
|
||||||
annotations={displayAnnotations}
|
|
||||||
scale={zoom}
|
|
||||||
offset={pan}
|
|
||||||
onPan={setPan}
|
|
||||||
className="absolute inset-0 z-0"
|
|
||||||
// Drawing Props
|
|
||||||
isDrawing={isDrawing}
|
|
||||||
currentTool={currentTool}
|
|
||||||
currentColor={currentColor}
|
|
||||||
onAddAnnotation={addAnnotation}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
{/* Zoom Toolbar */}
|
|
||||||
<ImageControlBar
|
|
||||||
zoom={zoom}
|
|
||||||
onZoomIn={() => handleZoom(1.2)}
|
|
||||||
onZoomOut={() => handleZoom(0.8)}
|
|
||||||
onFit={handleFit}
|
|
||||||
onDownload={handleDownload}
|
|
||||||
canDownload={!!file.media?.original?.key}
|
|
||||||
onCopy={handleCopy}
|
|
||||||
copied={copied}
|
|
||||||
canCopy={!!bestUrl}
|
|
||||||
/>
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
{isVideo && (
|
|
||||||
<VideoPlayer
|
|
||||||
key={file.id}
|
|
||||||
data={file}
|
|
||||||
playerRef={videoRef}
|
|
||||||
onPlay={onPlay}
|
|
||||||
onPause={onPause}
|
|
||||||
onTimeUpdate={onTimeUpdate}
|
|
||||||
annotations={displayAnnotations}
|
|
||||||
startTime={startTime}
|
|
||||||
shareId={shareId}
|
|
||||||
>
|
|
||||||
{children}
|
|
||||||
</VideoPlayer>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ import { FieldInfo } from '@shumai/dtos'
|
|||||||
import { FileViewer } from './file-viewer'
|
import { FileViewer } from './file-viewer'
|
||||||
import { CompareViewer } from './compare/compare-viewer'
|
import { CompareViewer } from './compare/compare-viewer'
|
||||||
import { pickDefaultCompareVersions } from './compare/compare-utils'
|
import { pickDefaultCompareVersions } from './compare/compare-utils'
|
||||||
import type Player from 'video.js/dist/types/player'
|
import type { MediaController } from './viewers/types'
|
||||||
import type { Annotation } from '@/ui/types'
|
import type { Annotation } from '@/ui/types'
|
||||||
|
|
||||||
import { useNavigate } from '@tanstack/react-router'
|
import { useNavigate } from '@tanstack/react-router'
|
||||||
@@ -141,7 +141,7 @@ export function PublicShareManager({
|
|||||||
const [selectedIds, setSelectedIds] = useState<Set<string>>(new Set())
|
const [selectedIds, setSelectedIds] = useState<Set<string>>(new Set())
|
||||||
const [rightSidebarWidth, setRightSidebarWidth] = useState(360)
|
const [rightSidebarWidth, setRightSidebarWidth] = useState(360)
|
||||||
const [isRightSidebarCollapsed, setIsRightSidebarCollapsed] = useState(() => !initialFileId)
|
const [isRightSidebarCollapsed, setIsRightSidebarCollapsed] = useState(() => !initialFileId)
|
||||||
const videoRef = useRef<Player | null>(null)
|
const mediaControllerRef = useRef<MediaController | null>(null)
|
||||||
const [annotations, setAnnotations] = useState<Annotation[]>([])
|
const [annotations, setAnnotations] = useState<Annotation[]>([])
|
||||||
const [currentTime, setCurrentTime] = useState(0)
|
const [currentTime, setCurrentTime] = useState(0)
|
||||||
const [selectedCommentId, setSelectedCommentId] = useState<string | null>(null)
|
const [selectedCommentId, setSelectedCommentId] = useState<string | null>(null)
|
||||||
@@ -359,14 +359,10 @@ export function PublicShareManager({
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (comment.second !== null && comment.second !== undefined) {
|
if (comment.second !== null && comment.second !== undefined) {
|
||||||
if (videoRef.current) {
|
mediaControllerRef.current?.seekTo(comment.second)
|
||||||
videoRef.current.currentTime(comment.second)
|
mediaControllerRef.current?.pause()
|
||||||
videoRef.current.pause()
|
|
||||||
}
|
|
||||||
} else if (newAnnotations && newAnnotations.length > 0) {
|
} else if (newAnnotations && newAnnotations.length > 0) {
|
||||||
if (videoRef.current) {
|
mediaControllerRef.current?.pause()
|
||||||
videoRef.current.pause()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -580,7 +576,7 @@ export function PublicShareManager({
|
|||||||
viewingFileData && (
|
viewingFileData && (
|
||||||
<FileViewer
|
<FileViewer
|
||||||
file={viewingFileData}
|
file={viewingFileData}
|
||||||
videoRef={videoRef}
|
mediaControllerRef={mediaControllerRef}
|
||||||
onPlay={handlePlay}
|
onPlay={handlePlay}
|
||||||
onTimeUpdate={setCurrentTime}
|
onTimeUpdate={setCurrentTime}
|
||||||
annotations={annotations}
|
annotations={annotations}
|
||||||
@@ -647,9 +643,7 @@ export function PublicShareManager({
|
|||||||
shareId={shareId}
|
shareId={shareId}
|
||||||
currentTime={currentTime}
|
currentTime={currentTime}
|
||||||
onTyping={() => {
|
onTyping={() => {
|
||||||
if (videoRef.current) {
|
mediaControllerRef.current?.pause()
|
||||||
videoRef.current.pause()
|
|
||||||
}
|
|
||||||
}}
|
}}
|
||||||
selectedCommentId={selectedCommentId}
|
selectedCommentId={selectedCommentId}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
import { client } from '@/ui/api/client'
|
||||||
|
import { Download } from 'lucide-react'
|
||||||
|
import React, { useImperativeHandle } from 'react'
|
||||||
|
import { FileViewerProps, MediaController } from '../types'
|
||||||
|
|
||||||
|
export const DefaultViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||||
|
({ file, shareId, children }, ref) => {
|
||||||
|
// Implement no-op media controller
|
||||||
|
useImperativeHandle(ref, () => ({
|
||||||
|
play: () => {},
|
||||||
|
pause: () => {},
|
||||||
|
seekTo: () => {},
|
||||||
|
}))
|
||||||
|
|
||||||
|
const handleDownload = async () => {
|
||||||
|
const key = file.media?.original?.key
|
||||||
|
if (!key || !file.id) return
|
||||||
|
try {
|
||||||
|
const res = shareId
|
||||||
|
? await client.api.shares[':shareId'].files[':fileId']['download-url'].$post({
|
||||||
|
param: { shareId, fileId: file.id },
|
||||||
|
json: { key },
|
||||||
|
})
|
||||||
|
: await client.api.files['download-url'].$post({
|
||||||
|
json: { key, assetId: file.id },
|
||||||
|
})
|
||||||
|
if (!res.ok) return
|
||||||
|
const { url } = await res.json()
|
||||||
|
const link = document.createElement('a')
|
||||||
|
link.href = url
|
||||||
|
link.download = ''
|
||||||
|
document.body.appendChild(link)
|
||||||
|
link.click()
|
||||||
|
document.body.removeChild(link)
|
||||||
|
} catch {
|
||||||
|
// silently fail
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col flex-1 h-full overflow-hidden bg-gray-100 dark:bg-gray-950 relative">
|
||||||
|
<div className="flex-1 flex flex-col-reverse md:flex-row min-h-0 relative">
|
||||||
|
{children}
|
||||||
|
<div className="flex-1 flex items-center justify-center">
|
||||||
|
<p className="text-muted-foreground">Preview unavailable</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="relative px-4 py-3 bg-card border-t border-gray-200 dark:border-gray-700 z-10 flex items-center justify-end gap-2 transition-colors duration-200">
|
||||||
|
<button
|
||||||
|
onClick={handleDownload}
|
||||||
|
disabled={!file.media?.original?.key}
|
||||||
|
className="flex items-center gap-1.5 text-xs font-medium px-3 py-1.5 rounded bg-gray-200/50 dark:bg-white/10 hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-200 transition-colors border border-transparent disabled:opacity-50 animate-in fade-in zoom-in-95 duration-200"
|
||||||
|
title="Download original file"
|
||||||
|
>
|
||||||
|
<Download size={14} />
|
||||||
|
Download
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
export default DefaultViewer
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import { FileTypeDefinition } from '../types'
|
||||||
|
import DefaultViewer from './default-viewer'
|
||||||
|
|
||||||
|
export const defaultTypeDefinition: FileTypeDefinition = {
|
||||||
|
id: 'default',
|
||||||
|
name: 'Default',
|
||||||
|
match: () => true,
|
||||||
|
viewer: DefaultViewer,
|
||||||
|
commentsConfig: {
|
||||||
|
hasTimestamp: false,
|
||||||
|
hasAnnotations: false,
|
||||||
|
hasAiBots: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
+1
-1
@@ -6,7 +6,7 @@ import { useAnnotationStore } from '@/ui/stores/annotation-store'
|
|||||||
import type { Annotation } from '@/ui/types'
|
import type { Annotation } from '@/ui/types'
|
||||||
import type { AssetInfo } from '@shumai/dtos'
|
import type { AssetInfo } from '@shumai/dtos'
|
||||||
import { forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState } from 'react'
|
import { forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState } from 'react'
|
||||||
import type { ComparePaneHandle, PaneReportedState } from './types'
|
import type { ComparePaneHandle, PaneReportedState } from '../../compare/types'
|
||||||
|
|
||||||
interface CompareImagePaneProps {
|
interface CompareImagePaneProps {
|
||||||
file: AssetInfo
|
file: AssetInfo
|
||||||
@@ -0,0 +1,223 @@
|
|||||||
|
import { useScreenSize } from '@/ui/hooks/useScreenSize'
|
||||||
|
import { client } from '@/ui/api/client'
|
||||||
|
import { getBestTranscode } from '@/ui/lib/media'
|
||||||
|
import React, { useCallback, useEffect, useRef, useState, useImperativeHandle } from 'react'
|
||||||
|
import DrawingCanvas from '@/ui/components/drawing-canvas'
|
||||||
|
import { ImageControlBar } from './image-control-bar'
|
||||||
|
import { useAnnotationStore } from '@/ui/stores/annotation-store'
|
||||||
|
import { FileViewerProps, MediaController } from '../types'
|
||||||
|
|
||||||
|
export const ImageViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||||
|
({ file, annotations, shareId, children }, ref) => {
|
||||||
|
// Implement no-op media controller since images are static
|
||||||
|
useImperativeHandle(ref, () => ({
|
||||||
|
play: () => {},
|
||||||
|
pause: () => {},
|
||||||
|
seekTo: () => {},
|
||||||
|
}))
|
||||||
|
|
||||||
|
const { width: screenWidth } = useScreenSize()
|
||||||
|
const {
|
||||||
|
isDrawing,
|
||||||
|
currentTool,
|
||||||
|
currentColor,
|
||||||
|
addAnnotation,
|
||||||
|
annotations: draftAnnotations,
|
||||||
|
} = useAnnotationStore()
|
||||||
|
|
||||||
|
const [containerSize, setContainerSize] = useState({ width: 0, height: 0 })
|
||||||
|
const [zoom, setZoom] = useState(1)
|
||||||
|
const [pan, setPan] = useState({ x: 0, y: 0 })
|
||||||
|
const [copied, setCopied] = useState(false)
|
||||||
|
|
||||||
|
const observerRef = useRef<ResizeObserver | null>(null)
|
||||||
|
|
||||||
|
const containerRef = useCallback((node: HTMLDivElement | null) => {
|
||||||
|
if (observerRef.current) {
|
||||||
|
observerRef.current.disconnect()
|
||||||
|
observerRef.current = null
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node !== null) {
|
||||||
|
const observer = new ResizeObserver((entries) => {
|
||||||
|
const entry = entries[0]
|
||||||
|
if (entry) {
|
||||||
|
setContainerSize({
|
||||||
|
width: entry.contentRect.width,
|
||||||
|
height: entry.contentRect.height,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
observer.observe(node)
|
||||||
|
observerRef.current = observer
|
||||||
|
}
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
const imgW = file.media?.metadata?.originalWidth ?? 1920
|
||||||
|
const imgH = file.media?.metadata?.originalHeight ?? 1080
|
||||||
|
const conW = containerSize.width
|
||||||
|
const conH = containerSize.height
|
||||||
|
|
||||||
|
let baseScale = 1
|
||||||
|
|
||||||
|
if (conW > 0 && conH > 0) {
|
||||||
|
baseScale = Math.min(conW / imgW, conH / imgH)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset/Fit logic
|
||||||
|
useEffect(() => {
|
||||||
|
if (conW > 0 && conH > 0) {
|
||||||
|
setZoom(baseScale)
|
||||||
|
// Center
|
||||||
|
const x = (conW - imgW * baseScale) / 2
|
||||||
|
const y = (conH - imgH * baseScale) / 2
|
||||||
|
setPan({ x, y })
|
||||||
|
}
|
||||||
|
}, [file.id, conW, conH, baseScale, imgW, imgH])
|
||||||
|
|
||||||
|
const handleZoom = (factor: number) => {
|
||||||
|
const cx = conW / 2
|
||||||
|
const cy = conH / 2
|
||||||
|
|
||||||
|
const newZoom = zoom * factor
|
||||||
|
if (newZoom < 0.01 || newZoom > 50) return
|
||||||
|
|
||||||
|
const newPanX = cx - (cx - pan.x) * (newZoom / zoom)
|
||||||
|
const newPanY = cy - (cy - pan.y) * (newZoom / zoom)
|
||||||
|
|
||||||
|
setZoom(newZoom)
|
||||||
|
setPan({ x: newPanX, y: newPanY })
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleFit = () => {
|
||||||
|
setZoom(baseScale)
|
||||||
|
const x = (conW - imgW * baseScale) / 2
|
||||||
|
const y = (conH - imgH * baseScale) / 2
|
||||||
|
setPan({ x, y })
|
||||||
|
}
|
||||||
|
|
||||||
|
let bestUrl = file.media?.original?.downloadUrl
|
||||||
|
|
||||||
|
const bestTranscode = getBestTranscode(file.media?.imageTranscodes, screenWidth)
|
||||||
|
if (bestTranscode?.url) {
|
||||||
|
bestUrl = bestTranscode.url
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleDownload = async () => {
|
||||||
|
const key = file.media?.original?.key
|
||||||
|
if (!key || !file.id) return
|
||||||
|
try {
|
||||||
|
const res = shareId
|
||||||
|
? await client.api.shares[':shareId'].files[':fileId']['download-url'].$post({
|
||||||
|
param: { shareId, fileId: file.id },
|
||||||
|
json: { key },
|
||||||
|
})
|
||||||
|
: await client.api.files['download-url'].$post({
|
||||||
|
json: { key, assetId: file.id },
|
||||||
|
})
|
||||||
|
if (!res.ok) return
|
||||||
|
const { url } = await res.json()
|
||||||
|
const link = document.createElement('a')
|
||||||
|
link.href = url
|
||||||
|
link.download = ''
|
||||||
|
document.body.appendChild(link)
|
||||||
|
link.click()
|
||||||
|
document.body.removeChild(link)
|
||||||
|
} catch {
|
||||||
|
// silently fail
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleCopy = async () => {
|
||||||
|
const webpUrl = bestUrl
|
||||||
|
if (!webpUrl) return
|
||||||
|
try {
|
||||||
|
const copyPromise = (async () => {
|
||||||
|
const response = await fetch(webpUrl)
|
||||||
|
const blob = await response.blob()
|
||||||
|
|
||||||
|
const img = new Image()
|
||||||
|
img.crossOrigin = 'anonymous'
|
||||||
|
const objectUrl = URL.createObjectURL(blob)
|
||||||
|
img.src = objectUrl
|
||||||
|
|
||||||
|
try {
|
||||||
|
await new Promise((resolve, reject) => {
|
||||||
|
img.onload = resolve
|
||||||
|
img.onerror = (e) => reject(new Error('Failed to load image: ' + String(e)))
|
||||||
|
})
|
||||||
|
|
||||||
|
const canvas = document.createElement('canvas')
|
||||||
|
canvas.width = img.width
|
||||||
|
canvas.height = img.height
|
||||||
|
const ctx = canvas.getContext('2d')
|
||||||
|
if (!ctx) throw new Error('Could not get canvas context')
|
||||||
|
ctx.drawImage(img, 0, 0)
|
||||||
|
|
||||||
|
const pngBlob = await new Promise<Blob | null>((resolve) => {
|
||||||
|
canvas.toBlob(resolve, 'image/png')
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!pngBlob) throw new Error('Failed to convert image to PNG')
|
||||||
|
return pngBlob
|
||||||
|
} finally {
|
||||||
|
URL.revokeObjectURL(objectUrl)
|
||||||
|
}
|
||||||
|
})()
|
||||||
|
|
||||||
|
await navigator.clipboard.write([
|
||||||
|
new ClipboardItem({
|
||||||
|
'image/png': copyPromise,
|
||||||
|
}),
|
||||||
|
])
|
||||||
|
setCopied(true)
|
||||||
|
setTimeout(() => setCopied(false), 2000)
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to copy image to clipboard:', err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const displayAnnotations = [...(annotations || []), ...draftAnnotations]
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col flex-1 h-full overflow-hidden bg-gray-100 dark:bg-gray-950 relative">
|
||||||
|
<div className="flex-1 flex flex-col-reverse md:flex-row min-h-0 relative">
|
||||||
|
{children}
|
||||||
|
<div ref={containerRef} className="flex-1 relative overflow-hidden">
|
||||||
|
<DrawingCanvas
|
||||||
|
width={conW}
|
||||||
|
height={conH}
|
||||||
|
mediaDimensions={{
|
||||||
|
width: imgW,
|
||||||
|
height: imgH,
|
||||||
|
}}
|
||||||
|
imageUrl={bestUrl}
|
||||||
|
annotations={displayAnnotations}
|
||||||
|
scale={zoom}
|
||||||
|
offset={pan}
|
||||||
|
onPan={setPan}
|
||||||
|
className="absolute inset-0 z-0"
|
||||||
|
isDrawing={isDrawing}
|
||||||
|
currentTool={currentTool}
|
||||||
|
currentColor={currentColor}
|
||||||
|
onAddAnnotation={addAnnotation}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<ImageControlBar
|
||||||
|
zoom={zoom}
|
||||||
|
onZoomIn={() => handleZoom(1.2)}
|
||||||
|
onZoomOut={() => handleZoom(0.8)}
|
||||||
|
onFit={handleFit}
|
||||||
|
onDownload={handleDownload}
|
||||||
|
canDownload={!!file.media?.original?.key}
|
||||||
|
onCopy={handleCopy}
|
||||||
|
copied={copied}
|
||||||
|
canCopy={!!bestUrl}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
export default ImageViewer
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
import { FileTypeDefinition } from '../types'
|
||||||
|
import ImageViewer from './image-viewer'
|
||||||
|
import { CompareImagePane } from './compare-image-pane'
|
||||||
|
|
||||||
|
export const imageTypeDefinition: FileTypeDefinition = {
|
||||||
|
id: 'image',
|
||||||
|
name: 'Image',
|
||||||
|
match: (file) => !!file.mediaType?.startsWith('image/'),
|
||||||
|
viewer: ImageViewer,
|
||||||
|
comparePane: CompareImagePane,
|
||||||
|
commentsConfig: {
|
||||||
|
hasTimestamp: false,
|
||||||
|
hasAnnotations: true,
|
||||||
|
hasAiBots: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import type { AssetInfo } from '@shumai/dtos'
|
||||||
|
import { FileTypeDefinition } from './types'
|
||||||
|
import { videoTypeDefinition } from './video'
|
||||||
|
import { imageTypeDefinition } from './image'
|
||||||
|
import { defaultTypeDefinition } from './default'
|
||||||
|
|
||||||
|
const registry: FileTypeDefinition[] = [videoTypeDefinition, imageTypeDefinition]
|
||||||
|
|
||||||
|
export function getViewerForFile(file: AssetInfo | null | undefined): FileTypeDefinition {
|
||||||
|
if (!file) return defaultTypeDefinition
|
||||||
|
const match = registry.find((viewer) => viewer.match(file))
|
||||||
|
return match || defaultTypeDefinition
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getAllViewers(): FileTypeDefinition[] {
|
||||||
|
return registry
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
import type { AssetInfo } from '@shumai/dtos'
|
||||||
|
import type { Annotation } from '@/ui/types'
|
||||||
|
import type { ComparePaneHandle, PaneReportedState } from '@/ui/components/compare/types'
|
||||||
|
import type React from 'react'
|
||||||
|
|
||||||
|
export interface MediaController {
|
||||||
|
play: () => void
|
||||||
|
pause: () => void
|
||||||
|
seekTo: (second: number) => void
|
||||||
|
getCurrentTime?: () => number
|
||||||
|
getDuration?: () => number
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface FileViewerProps {
|
||||||
|
file: AssetInfo
|
||||||
|
annotations?: Annotation[]
|
||||||
|
onPlay?: () => void
|
||||||
|
onPause?: () => void
|
||||||
|
onTimeUpdate?: (time: number) => void
|
||||||
|
startTime?: number
|
||||||
|
shareId?: string
|
||||||
|
children?: React.ReactNode
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ComparePaneProps {
|
||||||
|
file: AssetInfo
|
||||||
|
isActive: boolean
|
||||||
|
annotations: Annotation[]
|
||||||
|
onStateChange: (state: PaneReportedState) => void
|
||||||
|
onActivate: () => void
|
||||||
|
onUserPan?: (dx: number, dy: number) => void
|
||||||
|
shareId?: string
|
||||||
|
muted?: boolean
|
||||||
|
volume?: number
|
||||||
|
onPlay?: () => void
|
||||||
|
onTimeUpdate?: (second: number) => void
|
||||||
|
onRequestTogglePlay?: () => void
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface FileTypeDefinition {
|
||||||
|
id: string
|
||||||
|
name: string
|
||||||
|
match: (file: AssetInfo) => boolean
|
||||||
|
viewer: React.ForwardRefExoticComponent<FileViewerProps & React.RefAttributes<MediaController>>
|
||||||
|
comparePane?: React.ForwardRefExoticComponent<
|
||||||
|
ComparePaneProps & React.RefAttributes<ComparePaneHandle>
|
||||||
|
>
|
||||||
|
commentsConfig?: {
|
||||||
|
hasTimestamp?: boolean
|
||||||
|
hasAnnotations?: boolean
|
||||||
|
hasAiBots?: boolean
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,644 +0,0 @@
|
|||||||
import type { AssetInfo } from '@shumai/dtos'
|
|
||||||
import { client } from '@/ui/api/client'
|
|
||||||
import { cn } from '@/ui/lib/utils'
|
|
||||||
import { Play } from 'lucide-react'
|
|
||||||
import React, { useCallback, useEffect, useRef, useState } from 'react'
|
|
||||||
import videojs from 'video.js'
|
|
||||||
import type Player from 'video.js/dist/types/player'
|
|
||||||
import { useFramePlayer } from './use-frame-player'
|
|
||||||
import { resolveTotalFrames } from './utils'
|
|
||||||
import { VideoControlBar, type PlayerState, type DisplayTranscode } from './video-control-bar'
|
|
||||||
|
|
||||||
import type { Annotation } from '@/ui/types'
|
|
||||||
import DrawingCanvas from '../drawing-canvas'
|
|
||||||
import { useAnnotationStore } from '@/ui/stores/annotation-store'
|
|
||||||
|
|
||||||
interface VideoPlayerProps {
|
|
||||||
data: AssetInfo
|
|
||||||
playerRef?: React.RefObject<Player | null>
|
|
||||||
onPlay?: () => void
|
|
||||||
onPause?: () => void
|
|
||||||
onTimeUpdate?: (time: number) => void
|
|
||||||
annotations?: Annotation[]
|
|
||||||
startTime?: number
|
|
||||||
shareId?: string
|
|
||||||
children?: React.ReactNode
|
|
||||||
}
|
|
||||||
|
|
||||||
const VideoPlayer: React.FC<VideoPlayerProps> = ({
|
|
||||||
data,
|
|
||||||
playerRef: passedPlayerRef,
|
|
||||||
onPlay,
|
|
||||||
onPause,
|
|
||||||
onTimeUpdate,
|
|
||||||
annotations,
|
|
||||||
startTime,
|
|
||||||
shareId,
|
|
||||||
children,
|
|
||||||
}) => {
|
|
||||||
const localPlayerRef = useRef<Player | null>(null)
|
|
||||||
const playerRef = passedPlayerRef || localPlayerRef
|
|
||||||
// We use a container ref to manually append the video element
|
|
||||||
const videoContainerRef = useRef<HTMLDivElement>(null)
|
|
||||||
const containerRef = useRef<HTMLDivElement>(null)
|
|
||||||
const rootRef = useRef<HTMLDivElement>(null)
|
|
||||||
const controlsTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null)
|
|
||||||
const isOverControlsRef = useRef(false)
|
|
||||||
|
|
||||||
const [videoHtmlEl, setVideoHtmlEl] = useState<HTMLVideoElement | undefined>(undefined)
|
|
||||||
const videoRef = useRef<HTMLVideoElement | null>(null)
|
|
||||||
const [zoom, setZoom] = useState(1)
|
|
||||||
const [hasManuallyZoomed, setHasManuallyZoomed] = useState(false)
|
|
||||||
const [containerSize, setContainerSize] = useState({ width: 0, height: 0 })
|
|
||||||
|
|
||||||
const handleZoomChange = (newZoom: number) => {
|
|
||||||
setZoom(newZoom)
|
|
||||||
setHasManuallyZoomed(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleZoomReset = () => {
|
|
||||||
setHasManuallyZoomed(false)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Store
|
|
||||||
const {
|
|
||||||
isDrawing,
|
|
||||||
currentTool,
|
|
||||||
currentColor,
|
|
||||||
addAnnotation,
|
|
||||||
annotations: draftAnnotations,
|
|
||||||
} = useAnnotationStore()
|
|
||||||
|
|
||||||
// Combine annotations
|
|
||||||
const displayAnnotations = [...(annotations || []), ...draftAnnotations]
|
|
||||||
|
|
||||||
// Resize Observer for container
|
|
||||||
useEffect(() => {
|
|
||||||
if (!containerRef.current) return
|
|
||||||
const observer = new ResizeObserver((entries) => {
|
|
||||||
const entry = entries[0]
|
|
||||||
if (entry) {
|
|
||||||
setContainerSize({
|
|
||||||
width: entry.contentRect.width,
|
|
||||||
height: entry.contentRect.height,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
observer.observe(containerRef.current)
|
|
||||||
return () => observer.disconnect()
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
// Fit to screen initial / responsive resize
|
|
||||||
useEffect(() => {
|
|
||||||
if (containerSize.width > 0 && containerSize.height > 0) {
|
|
||||||
const vidW = data.media?.metadata?.originalWidth ?? 1920
|
|
||||||
const vidH = data.media?.metadata?.originalHeight ?? 1080
|
|
||||||
const scale = Math.min(containerSize.width / vidW, containerSize.height / vidH)
|
|
||||||
if (!hasManuallyZoomed) {
|
|
||||||
setZoom(scale)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, [containerSize.width, containerSize.height, data.media?.metadata, hasManuallyZoomed])
|
|
||||||
|
|
||||||
// Cleanup ref when player is disposed
|
|
||||||
useEffect(() => {
|
|
||||||
return () => {
|
|
||||||
videoRef.current = null
|
|
||||||
}
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
if (!data.media?.original?.downloadUrl || !data.media.metadata) {
|
|
||||||
return (
|
|
||||||
<div className="flex h-full w-full items-center justify-center">
|
|
||||||
<p className="text-muted-foreground">Media is not available.</p>
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initial resolution setup
|
|
||||||
const originalRes: DisplayTranscode = {
|
|
||||||
id: 'original',
|
|
||||||
url: data.media.original.downloadUrl,
|
|
||||||
key: data.media.original.key ?? '',
|
|
||||||
width: data.media.metadata.originalWidth ?? 0,
|
|
||||||
height: data.media.metadata.originalHeight ?? 0,
|
|
||||||
size: 0,
|
|
||||||
isRaw: true,
|
|
||||||
resolution: 'Original',
|
|
||||||
}
|
|
||||||
|
|
||||||
const resolutions: DisplayTranscode[] = (data.media.videoTranscodes ?? []).map((t) => {
|
|
||||||
const longSide = Math.max(t.width, t.height)
|
|
||||||
let resolution = `${t.height}p`
|
|
||||||
if (longSide >= 3840) resolution = '2160p'
|
|
||||||
else if (longSide >= 1920) resolution = '1080p'
|
|
||||||
else if (longSide >= 1280) resolution = '720p'
|
|
||||||
else if (longSide >= 960) resolution = '540p'
|
|
||||||
else if (longSide >= 640) resolution = '360p'
|
|
||||||
else if (longSide >= 320) resolution = '180p'
|
|
||||||
|
|
||||||
return {
|
|
||||||
...t,
|
|
||||||
resolution: t.isRaw ? 'Original' : resolution,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
const previewResolutions = resolutions.filter((r) => !r.isRaw)
|
|
||||||
|
|
||||||
// Logic to select best resolution based on screen size
|
|
||||||
const getInitialResolution = (): DisplayTranscode => {
|
|
||||||
if (previewResolutions.length === 0) return originalRes
|
|
||||||
|
|
||||||
if (typeof window === 'undefined') return previewResolutions[0]
|
|
||||||
|
|
||||||
// Use device pixel ratio for high DPI screens
|
|
||||||
const screenWidth = window.innerWidth * (window.devicePixelRatio || 1)
|
|
||||||
|
|
||||||
// 1. Sort by width ascending
|
|
||||||
const sortedResolutions = [...previewResolutions].sort((a, b) => {
|
|
||||||
const wA = a.width ?? 0
|
|
||||||
const wB = b.width ?? 0
|
|
||||||
return wA - wB
|
|
||||||
})
|
|
||||||
|
|
||||||
// 2. Find first one >= screenWidth
|
|
||||||
const bestFit = sortedResolutions.find((r) => (r.width ?? 0) >= screenWidth)
|
|
||||||
|
|
||||||
// 3. If found, return it. If not (all are smaller), return the last one (largest).
|
|
||||||
return bestFit || sortedResolutions[sortedResolutions.length - 1]
|
|
||||||
}
|
|
||||||
|
|
||||||
const initialRes = getInitialResolution()
|
|
||||||
|
|
||||||
// State
|
|
||||||
const [state, setState] = useState<PlayerState>({
|
|
||||||
isPlaying: false,
|
|
||||||
progress: 0,
|
|
||||||
currentTime: 0,
|
|
||||||
duration: data.media.metadata.duration || 0,
|
|
||||||
volume: 1,
|
|
||||||
isMuted: false,
|
|
||||||
isLooping: false,
|
|
||||||
playbackRate: 1,
|
|
||||||
isFullScreen: false,
|
|
||||||
showFrames: false,
|
|
||||||
currentResolution: initialRes.resolution,
|
|
||||||
currentSrc: initialRes.url,
|
|
||||||
})
|
|
||||||
|
|
||||||
const [buffered, setBuffered] = useState(0)
|
|
||||||
const [isControlsVisible, setIsControlsVisible] = useState(true)
|
|
||||||
const [isPlayerReady, setIsPlayerReady] = useState(false)
|
|
||||||
const lastProcessedStartTimeRef = useRef<number | null>(null)
|
|
||||||
|
|
||||||
// Frame-accurate hook and derived state
|
|
||||||
const metadata = data.media.metadata
|
|
||||||
const frameRate = metadata.frameRate || 30
|
|
||||||
const dbTotalFrames = metadata.totalFrames || 0
|
|
||||||
const containerDuration = metadata.duration || 0
|
|
||||||
|
|
||||||
const totalFrames = resolveTotalFrames({ dbTotalFrames, containerDuration, frameRate })
|
|
||||||
const { currentFrame, seekToFrame } = useFramePlayer(videoRef, frameRate, totalFrames)
|
|
||||||
|
|
||||||
const currentTime = currentFrame / frameRate
|
|
||||||
const duration = totalFrames / frameRate
|
|
||||||
const progress = duration > 0 ? (currentTime / duration) * 100 : 0
|
|
||||||
|
|
||||||
const controlBarState: PlayerState = {
|
|
||||||
...state,
|
|
||||||
currentTime,
|
|
||||||
duration,
|
|
||||||
progress,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Trigger time update event reactively
|
|
||||||
useEffect(() => {
|
|
||||||
if (onTimeUpdate) {
|
|
||||||
onTimeUpdate(currentFrame / frameRate)
|
|
||||||
}
|
|
||||||
}, [currentFrame, frameRate, onTimeUpdate])
|
|
||||||
|
|
||||||
// Initialize Video.js
|
|
||||||
useEffect(() => {
|
|
||||||
if (!videoContainerRef.current) return
|
|
||||||
|
|
||||||
// 1. Create the video element manually
|
|
||||||
// This avoids conflicts with React Strict Mode where the DOM might not match React's virtual DOM expectations after Video.js modifies it.
|
|
||||||
const videoElement = document.createElement('video-js')
|
|
||||||
videoElement.classList.add('vjs-big-play-centered', '!h-full', '!w-full')
|
|
||||||
|
|
||||||
// Hide it, but keep it in DOM
|
|
||||||
videoElement.style.opacity = '0'
|
|
||||||
videoElement.style.pointerEvents = 'none'
|
|
||||||
|
|
||||||
// Append to our container
|
|
||||||
videoContainerRef.current.appendChild(videoElement)
|
|
||||||
|
|
||||||
// 2. Initialize the player
|
|
||||||
const player = (playerRef.current = videojs(videoElement, {
|
|
||||||
controls: false,
|
|
||||||
autoplay: false,
|
|
||||||
preload: 'auto',
|
|
||||||
playsinline: true,
|
|
||||||
sources: [
|
|
||||||
{
|
|
||||||
src: state.currentSrc,
|
|
||||||
type: 'video/mp4',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}))
|
|
||||||
|
|
||||||
// Extract video element for Konva
|
|
||||||
const htmlVid = videoElement.querySelector('video')
|
|
||||||
if (htmlVid) {
|
|
||||||
setVideoHtmlEl(htmlVid)
|
|
||||||
videoRef.current = htmlVid
|
|
||||||
} else {
|
|
||||||
// Fallback: try player.tech().el()
|
|
||||||
// Wait for ready?
|
|
||||||
player.ready(() => {
|
|
||||||
const techEl = player.tech({ iWillNotUseThisInPlugins: true })?.el() as HTMLVideoElement
|
|
||||||
if (techEl) {
|
|
||||||
setVideoHtmlEl(techEl)
|
|
||||||
videoRef.current = techEl
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
player.on('play', () => {
|
|
||||||
setState((p) => ({ ...p, isPlaying: true }))
|
|
||||||
onPlay?.()
|
|
||||||
})
|
|
||||||
player.on('pause', () => {
|
|
||||||
setState((p) => ({ ...p, isPlaying: false }))
|
|
||||||
onPause?.()
|
|
||||||
})
|
|
||||||
player.on('ended', () => setState((p) => ({ ...p, isPlaying: false })))
|
|
||||||
player.on('loadedmetadata', () => {
|
|
||||||
setIsPlayerReady(true)
|
|
||||||
})
|
|
||||||
player.on('loadstart', () => {
|
|
||||||
setIsPlayerReady(false)
|
|
||||||
})
|
|
||||||
|
|
||||||
player.on('timeupdate', () => {
|
|
||||||
const playerDuration = player.duration() || data.media?.metadata?.duration || 0
|
|
||||||
|
|
||||||
const bufferedEnd = player.bufferedEnd()
|
|
||||||
if (playerDuration > 0) {
|
|
||||||
setBuffered((bufferedEnd / playerDuration) * 100)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
player.on('volumechange', () => {
|
|
||||||
const vol = player.volume()
|
|
||||||
const muted = player.muted()
|
|
||||||
setState((prev) => ({
|
|
||||||
...prev,
|
|
||||||
volume: vol || 0,
|
|
||||||
isMuted: muted || false,
|
|
||||||
}))
|
|
||||||
})
|
|
||||||
|
|
||||||
player.on('ratechange', () => {
|
|
||||||
setState((prev) => ({
|
|
||||||
...prev,
|
|
||||||
playbackRate: player.playbackRate() || 1,
|
|
||||||
}))
|
|
||||||
})
|
|
||||||
|
|
||||||
player.on('error', () => {
|
|
||||||
console.error('VideoJS Error:', player.error())
|
|
||||||
})
|
|
||||||
|
|
||||||
// Set initial state
|
|
||||||
player.volume(state.volume)
|
|
||||||
|
|
||||||
// 4. Cleanup
|
|
||||||
return () => {
|
|
||||||
if (player && !player.isDisposed()) {
|
|
||||||
player.dispose()
|
|
||||||
playerRef.current = null
|
|
||||||
videoRef.current = null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, [data])
|
|
||||||
|
|
||||||
// Handle changes to startTime (e.g., clicking different chunks in search results)
|
|
||||||
useEffect(() => {
|
|
||||||
if (!isPlayerReady) return
|
|
||||||
|
|
||||||
if (startTime !== undefined && startTime !== null && startTime > 0) {
|
|
||||||
if (startTime !== lastProcessedStartTimeRef.current) {
|
|
||||||
lastProcessedStartTimeRef.current = startTime
|
|
||||||
const targetFrame = Math.floor(startTime * frameRate + 0.45)
|
|
||||||
seekToFrame(targetFrame)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, [startTime, frameRate, seekToFrame, isPlayerReady])
|
|
||||||
|
|
||||||
const scheduleHide = useCallback(() => {
|
|
||||||
if (controlsTimeoutRef.current) {
|
|
||||||
clearTimeout(controlsTimeoutRef.current)
|
|
||||||
}
|
|
||||||
// Never hide while the cursor rests over the control bar itself.
|
|
||||||
if (state.isFullScreen && !isOverControlsRef.current) {
|
|
||||||
controlsTimeoutRef.current = setTimeout(() => {
|
|
||||||
setIsControlsVisible(false)
|
|
||||||
}, 1200)
|
|
||||||
}
|
|
||||||
}, [state.isFullScreen])
|
|
||||||
|
|
||||||
const handleMouseMove = useCallback(() => {
|
|
||||||
// Always show controls on movement, then (re)start the idle hide timer.
|
|
||||||
setIsControlsVisible(true)
|
|
||||||
scheduleHide()
|
|
||||||
}, [scheduleHide])
|
|
||||||
|
|
||||||
const handleControlsMouseEnter = useCallback(() => {
|
|
||||||
isOverControlsRef.current = true
|
|
||||||
if (controlsTimeoutRef.current) clearTimeout(controlsTimeoutRef.current)
|
|
||||||
setIsControlsVisible(true)
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
const handleControlsMouseLeave = useCallback(() => {
|
|
||||||
isOverControlsRef.current = false
|
|
||||||
scheduleHide()
|
|
||||||
}, [scheduleHide])
|
|
||||||
|
|
||||||
const handleMouseLeave = useCallback(() => {
|
|
||||||
// If the cursor leaves the player while fullscreen, hide the controls.
|
|
||||||
if (state.isFullScreen) {
|
|
||||||
if (controlsTimeoutRef.current) clearTimeout(controlsTimeoutRef.current)
|
|
||||||
setIsControlsVisible(false)
|
|
||||||
}
|
|
||||||
}, [state.isFullScreen])
|
|
||||||
|
|
||||||
// Clean up timeout on unmount or state change
|
|
||||||
useEffect(() => {
|
|
||||||
return () => {
|
|
||||||
if (controlsTimeoutRef.current) clearTimeout(controlsTimeoutRef.current)
|
|
||||||
}
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
// Windowed mode: controls are always visible. Fullscreen: show now and let
|
|
||||||
// the idle timer hide them (re-runs on play/pause so pausing re-reveals them).
|
|
||||||
useEffect(() => {
|
|
||||||
if (!state.isFullScreen) {
|
|
||||||
setIsControlsVisible(true)
|
|
||||||
if (controlsTimeoutRef.current) clearTimeout(controlsTimeoutRef.current)
|
|
||||||
} else {
|
|
||||||
handleMouseMove()
|
|
||||||
}
|
|
||||||
}, [state.isPlaying, state.isFullScreen, handleMouseMove])
|
|
||||||
|
|
||||||
// -- Event Handlers --
|
|
||||||
|
|
||||||
const togglePlay = useCallback(() => {
|
|
||||||
// Disable click-to-play if drawing
|
|
||||||
if (useAnnotationStore.getState().isDrawing) return
|
|
||||||
|
|
||||||
const player = playerRef.current
|
|
||||||
if (!player) return
|
|
||||||
|
|
||||||
if (player.paused() || player.ended()) {
|
|
||||||
const playPromise = player.play()
|
|
||||||
if (playPromise !== undefined) {
|
|
||||||
playPromise.catch((error) => {
|
|
||||||
// Ignore AbortError which happens when pausing rapidly after playing
|
|
||||||
if (error.name === 'AbortError') return
|
|
||||||
console.error('Play failed:', error)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
player.pause()
|
|
||||||
}
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
const toggleLoop = () => {
|
|
||||||
const player = playerRef.current
|
|
||||||
if (!player) return
|
|
||||||
const newLoop = !state.isLooping
|
|
||||||
player.loop(newLoop)
|
|
||||||
setState((prev) => ({ ...prev, isLooping: newLoop }))
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleVolumeChange = (newVolume: number) => {
|
|
||||||
// Optimistic update to prevent slider jumping and ensure immediate UI feedback
|
|
||||||
setState((prev) => ({
|
|
||||||
...prev,
|
|
||||||
volume: newVolume,
|
|
||||||
isMuted: newVolume === 0, // If dragging to 0, consider it muted
|
|
||||||
}))
|
|
||||||
|
|
||||||
const player = playerRef.current
|
|
||||||
if (!player) return
|
|
||||||
|
|
||||||
// Set volume first so if we unmute, it is at the correct level
|
|
||||||
player.volume(newVolume)
|
|
||||||
|
|
||||||
// Manage mute state based on volume
|
|
||||||
if (newVolume > 0 && player.muted()) {
|
|
||||||
player.muted(false)
|
|
||||||
} else if (newVolume === 0 && !player.muted()) {
|
|
||||||
player.muted(true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const toggleMute = () => {
|
|
||||||
const player = playerRef.current
|
|
||||||
if (!player) return
|
|
||||||
|
|
||||||
const isMuted = player.muted()
|
|
||||||
if (isMuted) {
|
|
||||||
player.muted(false)
|
|
||||||
// If volume was 0 (e.g. user dragged to 0), restore to default 0.5 so they hear something
|
|
||||||
if (player.volume() === 0) {
|
|
||||||
player.volume(0.5)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
player.muted(true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const changePlaybackRate = (rate: number) => {
|
|
||||||
const player = playerRef.current
|
|
||||||
if (!player) return
|
|
||||||
player.playbackRate(rate)
|
|
||||||
}
|
|
||||||
|
|
||||||
const changeResolution = (res: DisplayTranscode) => {
|
|
||||||
const player = playerRef.current
|
|
||||||
if (!player) return
|
|
||||||
|
|
||||||
const wasPlaying = !player.paused()
|
|
||||||
const currentT = player.currentTime()
|
|
||||||
|
|
||||||
setState((prev) => ({
|
|
||||||
...prev,
|
|
||||||
currentResolution: res.resolution,
|
|
||||||
currentSrc: res.url,
|
|
||||||
}))
|
|
||||||
|
|
||||||
player.src({ type: 'video/mp4', src: res.url })
|
|
||||||
|
|
||||||
player.one('loadedmetadata', () => {
|
|
||||||
player.currentTime(currentT)
|
|
||||||
if (wasPlaying) {
|
|
||||||
const playPromise = player.play()
|
|
||||||
if (playPromise !== undefined) {
|
|
||||||
playPromise.catch((error) => {
|
|
||||||
if (error.name === 'AbortError') return
|
|
||||||
console.error('Play after seek failed:', error)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
player.playbackRate(state.playbackRate)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const toggleFullScreen = () => {
|
|
||||||
if (!rootRef.current) return
|
|
||||||
if (!document.fullscreenElement) {
|
|
||||||
rootRef.current.requestFullscreen()
|
|
||||||
setState((prev) => ({ ...prev, isFullScreen: true }))
|
|
||||||
} else {
|
|
||||||
document.exitFullscreen()
|
|
||||||
setState((prev) => ({ ...prev, isFullScreen: false }))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleDownload = async (key: string) => {
|
|
||||||
try {
|
|
||||||
const res = shareId
|
|
||||||
? await client.api.shares[':shareId'].files[':fileId']['download-url'].$post({
|
|
||||||
param: { shareId, fileId: data.id! },
|
|
||||||
json: { key },
|
|
||||||
})
|
|
||||||
: await client.api.files['download-url'].$post({
|
|
||||||
json: { key, assetId: data.id! },
|
|
||||||
})
|
|
||||||
if (!res.ok) return
|
|
||||||
const { url } = await res.json()
|
|
||||||
const link = document.createElement('a')
|
|
||||||
link.href = url
|
|
||||||
link.download = ''
|
|
||||||
document.body.appendChild(link)
|
|
||||||
link.click()
|
|
||||||
document.body.removeChild(link)
|
|
||||||
} catch {
|
|
||||||
// silently fail
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Listen for fullscreen change events (ESC key)
|
|
||||||
useEffect(() => {
|
|
||||||
const onFsChange = () => {
|
|
||||||
setState((prev) => ({
|
|
||||||
...prev,
|
|
||||||
isFullScreen: !!document.fullscreenElement,
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
document.addEventListener('fullscreenchange', onFsChange)
|
|
||||||
return () => document.removeEventListener('fullscreenchange', onFsChange)
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
// Calculate center pan
|
|
||||||
const vidW = data.media?.metadata?.originalWidth ?? 1920
|
|
||||||
const vidH = data.media?.metadata?.originalHeight ?? 1080
|
|
||||||
|
|
||||||
const scale = zoom
|
|
||||||
const panX = (containerSize.width - vidW * scale) / 2
|
|
||||||
const panY = (containerSize.height - vidH * scale) / 2
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
ref={rootRef}
|
|
||||||
className={cn(
|
|
||||||
'group shadow-2xl font-sans select-none flex flex-col mx-auto transition-all duration-300 relative',
|
|
||||||
state.isFullScreen ? 'h-full w-full rounded-none bg-black' : 'w-full h-full',
|
|
||||||
!isControlsVisible && state.isFullScreen ? 'cursor-none' : '',
|
|
||||||
)}
|
|
||||||
onMouseMove={handleMouseMove}
|
|
||||||
onMouseLeave={handleMouseLeave}
|
|
||||||
>
|
|
||||||
<div className="flex-1 flex flex-col-reverse md:flex-row min-h-0 relative">
|
|
||||||
{/* Render Carousel/Sidebar here if not fullscreen */}
|
|
||||||
{!state.isFullScreen && children}
|
|
||||||
|
|
||||||
{/* Video Area */}
|
|
||||||
<div
|
|
||||||
ref={containerRef}
|
|
||||||
className={cn(
|
|
||||||
'flex-1 bg-black cursor-pointer relative flex items-center justify-center overflow-hidden min-h-0',
|
|
||||||
)}
|
|
||||||
onClick={togglePlay}
|
|
||||||
data-vjs-player
|
|
||||||
data-testid="video-area"
|
|
||||||
>
|
|
||||||
{/* Hidden VideoJS container */}
|
|
||||||
<div ref={videoContainerRef} className="absolute inset-0 z-[-1]" />
|
|
||||||
|
|
||||||
{/* Drawing Canvas (Visible) */}
|
|
||||||
{videoHtmlEl && containerSize.width > 0 && (
|
|
||||||
<DrawingCanvas
|
|
||||||
width={containerSize.width}
|
|
||||||
height={containerSize.height}
|
|
||||||
mediaDimensions={{
|
|
||||||
width: vidW,
|
|
||||||
height: vidH,
|
|
||||||
}}
|
|
||||||
videoElement={videoHtmlEl}
|
|
||||||
annotations={displayAnnotations}
|
|
||||||
scale={scale}
|
|
||||||
offset={{ x: panX, y: panY }}
|
|
||||||
className="absolute inset-0"
|
|
||||||
onClick={togglePlay}
|
|
||||||
// Drawing Props
|
|
||||||
isDrawing={isDrawing}
|
|
||||||
currentTool={currentTool}
|
|
||||||
currentColor={currentColor}
|
|
||||||
onAddAnnotation={addAnnotation}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{/* Big Play Button Overlay (when paused) */}
|
|
||||||
{!state.isPlaying && !isDrawing && (
|
|
||||||
<div className="absolute inset-0 flex items-center justify-center bg-black/20 pointer-events-none z-10">
|
|
||||||
<div className="w-20 h-20 bg-white/10 backdrop-blur-sm rounded-full flex items-center justify-center border-2 border-white/30 animate-pulse">
|
|
||||||
<Play className="w-10 h-10 text-white ml-1 fill-white" />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<VideoControlBar
|
|
||||||
state={controlBarState}
|
|
||||||
zoom={zoom}
|
|
||||||
isControlsVisible={isControlsVisible}
|
|
||||||
buffered={buffered}
|
|
||||||
data={data}
|
|
||||||
resolutions={resolutions}
|
|
||||||
togglePlay={togglePlay}
|
|
||||||
toggleLoop={toggleLoop}
|
|
||||||
toggleMute={toggleMute}
|
|
||||||
handleVolumeChange={handleVolumeChange}
|
|
||||||
changePlaybackRate={changePlaybackRate}
|
|
||||||
changeResolution={changeResolution}
|
|
||||||
handleDownload={handleDownload}
|
|
||||||
toggleFullScreen={toggleFullScreen}
|
|
||||||
onZoomChange={handleZoomChange}
|
|
||||||
onZoomReset={handleZoomReset}
|
|
||||||
frameRate={frameRate}
|
|
||||||
totalFrames={totalFrames}
|
|
||||||
currentFrame={currentFrame}
|
|
||||||
seekToFrame={seekToFrame}
|
|
||||||
onMouseEnter={handleControlsMouseEnter}
|
|
||||||
onMouseLeave={handleControlsMouseLeave}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
export default VideoPlayer
|
|
||||||
+9
-9
@@ -8,21 +8,21 @@ import { Play } from 'lucide-react'
|
|||||||
import { forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState } from 'react'
|
import { forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState } from 'react'
|
||||||
import videojs from 'video.js'
|
import videojs from 'video.js'
|
||||||
import type Player from 'video.js/dist/types/player'
|
import type Player from 'video.js/dist/types/player'
|
||||||
import { useFramePlayer } from '@/ui/components/viewers/use-frame-player'
|
import { useFramePlayer } from './use-frame-player'
|
||||||
import { resolveTotalFrames } from '@/ui/components/viewers/utils'
|
import { resolveTotalFrames } from './utils'
|
||||||
import { clampFrame as clampFrameUtil } from './compare-utils'
|
import { clampFrame as clampFrameUtil } from '../../compare/compare-utils'
|
||||||
import type { ComparePaneHandle, DisplayTranscode, PaneReportedState } from './types'
|
import type { ComparePaneHandle, DisplayTranscode, PaneReportedState } from '../../compare/types'
|
||||||
|
|
||||||
interface CompareVideoPaneProps {
|
interface CompareVideoPaneProps {
|
||||||
file: AssetInfo
|
file: AssetInfo
|
||||||
shareId?: string
|
shareId?: string
|
||||||
isActive: boolean
|
isActive: boolean
|
||||||
/** Whether this pane's audio should be muted (only the active side plays audio). */
|
/** Whether this pane's audio should be muted (only the active side plays audio). */
|
||||||
muted: boolean
|
muted?: boolean
|
||||||
volume: number
|
volume?: number
|
||||||
annotations: Annotation[]
|
annotations: Annotation[]
|
||||||
onStateChange: (state: PaneReportedState) => void
|
onStateChange: (state: PaneReportedState) => void
|
||||||
onActivate?: () => void
|
onActivate: () => void
|
||||||
onRequestTogglePlay?: () => void
|
onRequestTogglePlay?: () => void
|
||||||
/** Called with the current playhead time in seconds (used only for the active pane). */
|
/** Called with the current playhead time in seconds (used only for the active pane). */
|
||||||
onTimeUpdate?: (second: number) => void
|
onTimeUpdate?: (second: number) => void
|
||||||
@@ -68,8 +68,8 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
|
|||||||
file,
|
file,
|
||||||
shareId,
|
shareId,
|
||||||
isActive,
|
isActive,
|
||||||
muted,
|
muted = true,
|
||||||
volume,
|
volume = 1,
|
||||||
annotations,
|
annotations,
|
||||||
onStateChange,
|
onStateChange,
|
||||||
onActivate,
|
onActivate,
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
import { FileTypeDefinition } from '../types'
|
||||||
|
import VideoViewer from './video-viewer'
|
||||||
|
import { CompareVideoPane } from './compare-video-pane'
|
||||||
|
|
||||||
|
export const videoTypeDefinition: FileTypeDefinition = {
|
||||||
|
id: 'video',
|
||||||
|
name: 'Video',
|
||||||
|
match: (file) => !!file.mediaType?.startsWith('video/'),
|
||||||
|
viewer: VideoViewer,
|
||||||
|
comparePane: CompareVideoPane,
|
||||||
|
commentsConfig: {
|
||||||
|
hasTimestamp: true,
|
||||||
|
hasAnnotations: true,
|
||||||
|
hasAiBots: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
+3
-3
@@ -23,8 +23,8 @@ import {
|
|||||||
DropdownMenuItem,
|
DropdownMenuItem,
|
||||||
DropdownMenuLabel,
|
DropdownMenuLabel,
|
||||||
DropdownMenuTrigger,
|
DropdownMenuTrigger,
|
||||||
} from '../ui/dropdown-menu'
|
} from '../../ui/dropdown-menu'
|
||||||
import { Slider } from '../ui/slider'
|
import { Slider } from '../../ui/slider'
|
||||||
import ProgressBar from './progress-bar'
|
import ProgressBar from './progress-bar'
|
||||||
import { formatTime, formatTimecode } from './utils'
|
import { formatTime, formatTimecode } from './utils'
|
||||||
|
|
||||||
@@ -196,7 +196,7 @@ export const VideoControlBar: React.FC<ControlBarProps> = ({
|
|||||||
<div className="w-24 px-1">
|
<div className="w-24 px-1">
|
||||||
<Slider
|
<Slider
|
||||||
value={state.isMuted ? [0] : [state.volume]}
|
value={state.isMuted ? [0] : [state.volume]}
|
||||||
onValueChange={(v) => handleVolumeChange(v[0])}
|
onValueChange={(v: number[]) => handleVolumeChange(v[0])}
|
||||||
max={1}
|
max={1}
|
||||||
step={0.05}
|
step={0.05}
|
||||||
/>
|
/>
|
||||||
@@ -0,0 +1,650 @@
|
|||||||
|
import { client } from '@/ui/api/client'
|
||||||
|
import { cn } from '@/ui/lib/utils'
|
||||||
|
import { Play } from 'lucide-react'
|
||||||
|
import React, { useCallback, useEffect, useRef, useState, useImperativeHandle } from 'react'
|
||||||
|
import videojs from 'video.js'
|
||||||
|
import type Player from 'video.js/dist/types/player'
|
||||||
|
import { useFramePlayer } from './use-frame-player'
|
||||||
|
import { resolveTotalFrames } from './utils'
|
||||||
|
import { VideoControlBar, type PlayerState, type DisplayTranscode } from './video-control-bar'
|
||||||
|
import DrawingCanvas from '@/ui/components/drawing-canvas'
|
||||||
|
import { useAnnotationStore } from '@/ui/stores/annotation-store'
|
||||||
|
import { FileViewerProps, MediaController } from '../types'
|
||||||
|
|
||||||
|
const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||||
|
(
|
||||||
|
{ file: data, onPlay, onPause, onTimeUpdate, annotations, startTime, shareId, children },
|
||||||
|
ref,
|
||||||
|
) => {
|
||||||
|
const localPlayerRef = useRef<Player | null>(null)
|
||||||
|
const playerRef = localPlayerRef
|
||||||
|
const hasMedia = !!data.media?.original?.downloadUrl && !!data.media?.metadata
|
||||||
|
// We use a container ref to manually append the video element
|
||||||
|
const videoContainerRef = useRef<HTMLDivElement>(null)
|
||||||
|
const containerRef = useRef<HTMLDivElement>(null)
|
||||||
|
const rootRef = useRef<HTMLDivElement>(null)
|
||||||
|
const controlsTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null)
|
||||||
|
|
||||||
|
useImperativeHandle(ref, () => ({
|
||||||
|
play: () => {
|
||||||
|
if (localPlayerRef.current) {
|
||||||
|
localPlayerRef.current.play()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
pause: () => {
|
||||||
|
if (localPlayerRef.current) {
|
||||||
|
localPlayerRef.current.pause()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
seekTo: (second: number) => {
|
||||||
|
if (localPlayerRef.current) {
|
||||||
|
localPlayerRef.current.currentTime(second)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
getCurrentTime: () => {
|
||||||
|
return (localPlayerRef.current ? localPlayerRef.current.currentTime() : 0) ?? 0
|
||||||
|
},
|
||||||
|
getDuration: () => {
|
||||||
|
return (localPlayerRef.current ? localPlayerRef.current.duration() : 0) ?? 0
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
const isOverControlsRef = useRef(false)
|
||||||
|
|
||||||
|
const [videoHtmlEl, setVideoHtmlEl] = useState<HTMLVideoElement | undefined>(undefined)
|
||||||
|
const videoRef = useRef<HTMLVideoElement | null>(null)
|
||||||
|
const [zoom, setZoom] = useState(1)
|
||||||
|
const [hasManuallyZoomed, setHasManuallyZoomed] = useState(false)
|
||||||
|
const [containerSize, setContainerSize] = useState({ width: 0, height: 0 })
|
||||||
|
|
||||||
|
const handleZoomChange = (newZoom: number) => {
|
||||||
|
setZoom(newZoom)
|
||||||
|
setHasManuallyZoomed(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleZoomReset = () => {
|
||||||
|
setHasManuallyZoomed(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store
|
||||||
|
const {
|
||||||
|
isDrawing,
|
||||||
|
currentTool,
|
||||||
|
currentColor,
|
||||||
|
addAnnotation,
|
||||||
|
annotations: draftAnnotations,
|
||||||
|
} = useAnnotationStore()
|
||||||
|
|
||||||
|
// Combine annotations
|
||||||
|
const displayAnnotations = [...(annotations || []), ...draftAnnotations]
|
||||||
|
|
||||||
|
// Resize Observer for container
|
||||||
|
useEffect(() => {
|
||||||
|
if (!containerRef.current) return
|
||||||
|
const observer = new ResizeObserver((entries) => {
|
||||||
|
const entry = entries[0]
|
||||||
|
if (entry) {
|
||||||
|
setContainerSize({
|
||||||
|
width: entry.contentRect.width,
|
||||||
|
height: entry.contentRect.height,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
observer.observe(containerRef.current)
|
||||||
|
return () => observer.disconnect()
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
// Fit to screen initial / responsive resize
|
||||||
|
useEffect(() => {
|
||||||
|
if (containerSize.width > 0 && containerSize.height > 0) {
|
||||||
|
const vidW = data.media?.metadata?.originalWidth ?? 1920
|
||||||
|
const vidH = data.media?.metadata?.originalHeight ?? 1080
|
||||||
|
const scale = Math.min(containerSize.width / vidW, containerSize.height / vidH)
|
||||||
|
if (!hasManuallyZoomed) {
|
||||||
|
setZoom(scale)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [containerSize.width, containerSize.height, data.media?.metadata, hasManuallyZoomed])
|
||||||
|
|
||||||
|
// Cleanup ref when player is disposed
|
||||||
|
useEffect(() => {
|
||||||
|
return () => {
|
||||||
|
videoRef.current = null
|
||||||
|
}
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
// Initial resolution setup
|
||||||
|
const originalRes: DisplayTranscode = {
|
||||||
|
id: 'original',
|
||||||
|
url: data.media?.original?.downloadUrl || '',
|
||||||
|
key: data.media?.original?.key ?? '',
|
||||||
|
width: data.media?.metadata?.originalWidth ?? 0,
|
||||||
|
height: data.media?.metadata?.originalHeight ?? 0,
|
||||||
|
size: 0,
|
||||||
|
isRaw: true,
|
||||||
|
resolution: 'Original',
|
||||||
|
}
|
||||||
|
|
||||||
|
const resolutions: DisplayTranscode[] = (data.media?.videoTranscodes ?? []).map((t) => {
|
||||||
|
const longSide = Math.max(t.width, t.height)
|
||||||
|
let resolution = `${t.height}p`
|
||||||
|
if (longSide >= 3840) resolution = '2160p'
|
||||||
|
else if (longSide >= 1920) resolution = '1080p'
|
||||||
|
else if (longSide >= 1280) resolution = '720p'
|
||||||
|
else if (longSide >= 960) resolution = '540p'
|
||||||
|
else if (longSide >= 640) resolution = '360p'
|
||||||
|
else if (longSide >= 320) resolution = '180p'
|
||||||
|
|
||||||
|
return {
|
||||||
|
...t,
|
||||||
|
resolution: t.isRaw ? 'Original' : resolution,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const previewResolutions = resolutions.filter((r) => !r.isRaw)
|
||||||
|
|
||||||
|
// Logic to select best resolution based on screen size
|
||||||
|
const getInitialResolution = (): DisplayTranscode => {
|
||||||
|
if (previewResolutions.length === 0) return originalRes
|
||||||
|
|
||||||
|
if (typeof window === 'undefined') return previewResolutions[0]
|
||||||
|
|
||||||
|
// Use device pixel ratio for high DPI screens
|
||||||
|
const screenWidth = window.innerWidth * (window.devicePixelRatio || 1)
|
||||||
|
|
||||||
|
// 1. Sort by width ascending
|
||||||
|
const sortedResolutions = [...previewResolutions].sort((a, b) => {
|
||||||
|
const wA = a.width ?? 0
|
||||||
|
const wB = b.width ?? 0
|
||||||
|
return wA - wB
|
||||||
|
})
|
||||||
|
|
||||||
|
// 2. Find first one >= screenWidth
|
||||||
|
const bestFit = sortedResolutions.find((r) => (r.width ?? 0) >= screenWidth)
|
||||||
|
|
||||||
|
// 3. If found, return it. If not (all are smaller), return the last one (largest).
|
||||||
|
return bestFit || sortedResolutions[sortedResolutions.length - 1]
|
||||||
|
}
|
||||||
|
|
||||||
|
const initialRes = getInitialResolution()
|
||||||
|
|
||||||
|
// State
|
||||||
|
const [state, setState] = useState<PlayerState>({
|
||||||
|
isPlaying: false,
|
||||||
|
progress: 0,
|
||||||
|
currentTime: 0,
|
||||||
|
duration: data.media?.metadata?.duration || 0,
|
||||||
|
volume: 1,
|
||||||
|
isMuted: false,
|
||||||
|
isLooping: false,
|
||||||
|
playbackRate: 1,
|
||||||
|
isFullScreen: false,
|
||||||
|
showFrames: false,
|
||||||
|
currentResolution: initialRes?.resolution || 'Original',
|
||||||
|
currentSrc: initialRes?.url || '',
|
||||||
|
})
|
||||||
|
|
||||||
|
const [buffered, setBuffered] = useState(0)
|
||||||
|
const [isControlsVisible, setIsControlsVisible] = useState(true)
|
||||||
|
const [isPlayerReady, setIsPlayerReady] = useState(false)
|
||||||
|
const lastProcessedStartTimeRef = useRef<number | null>(null)
|
||||||
|
|
||||||
|
// Frame-accurate hook and derived state
|
||||||
|
const metadata = data.media?.metadata
|
||||||
|
const frameRate = metadata?.frameRate || 30
|
||||||
|
const dbTotalFrames = metadata?.totalFrames || 0
|
||||||
|
const containerDuration = metadata?.duration || 0
|
||||||
|
|
||||||
|
const totalFrames = resolveTotalFrames({ dbTotalFrames, containerDuration, frameRate })
|
||||||
|
const { currentFrame, seekToFrame } = useFramePlayer(videoRef, frameRate, totalFrames)
|
||||||
|
|
||||||
|
const currentTime = currentFrame / frameRate
|
||||||
|
const duration = totalFrames / frameRate
|
||||||
|
const progress = duration > 0 ? (currentTime / duration) * 100 : 0
|
||||||
|
|
||||||
|
const controlBarState: PlayerState = {
|
||||||
|
...state,
|
||||||
|
currentTime,
|
||||||
|
duration,
|
||||||
|
progress,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Trigger time update event reactively
|
||||||
|
useEffect(() => {
|
||||||
|
if (onTimeUpdate) {
|
||||||
|
onTimeUpdate(currentFrame / frameRate)
|
||||||
|
}
|
||||||
|
}, [currentFrame, frameRate, onTimeUpdate])
|
||||||
|
|
||||||
|
// Initialize Video.js
|
||||||
|
useEffect(() => {
|
||||||
|
if (!videoContainerRef.current) return
|
||||||
|
|
||||||
|
// 1. Create the video element manually
|
||||||
|
// This avoids conflicts with React Strict Mode where the DOM might not match React's virtual DOM expectations after Video.js modifies it.
|
||||||
|
const videoElement = document.createElement('video-js')
|
||||||
|
videoElement.classList.add('vjs-big-play-centered', '!h-full', '!w-full')
|
||||||
|
|
||||||
|
// Hide it, but keep it in DOM
|
||||||
|
videoElement.style.opacity = '0'
|
||||||
|
videoElement.style.pointerEvents = 'none'
|
||||||
|
|
||||||
|
// Append to our container
|
||||||
|
videoContainerRef.current.appendChild(videoElement)
|
||||||
|
|
||||||
|
// 2. Initialize the player
|
||||||
|
const player = (playerRef.current = videojs(videoElement, {
|
||||||
|
controls: false,
|
||||||
|
autoplay: false,
|
||||||
|
preload: 'auto',
|
||||||
|
playsinline: true,
|
||||||
|
sources: [
|
||||||
|
{
|
||||||
|
src: state.currentSrc,
|
||||||
|
type: 'video/mp4',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}))
|
||||||
|
|
||||||
|
// Extract video element for Konva
|
||||||
|
const htmlVid = videoElement.querySelector('video')
|
||||||
|
if (htmlVid) {
|
||||||
|
setVideoHtmlEl(htmlVid)
|
||||||
|
videoRef.current = htmlVid
|
||||||
|
} else {
|
||||||
|
// Fallback: try player.tech().el()
|
||||||
|
// Wait for ready?
|
||||||
|
player.ready(() => {
|
||||||
|
const techEl = player.tech({ iWillNotUseThisInPlugins: true })?.el() as HTMLVideoElement
|
||||||
|
if (techEl) {
|
||||||
|
setVideoHtmlEl(techEl)
|
||||||
|
videoRef.current = techEl
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
player.on('play', () => {
|
||||||
|
setState((p) => ({ ...p, isPlaying: true }))
|
||||||
|
onPlay?.()
|
||||||
|
})
|
||||||
|
player.on('pause', () => {
|
||||||
|
setState((p) => ({ ...p, isPlaying: false }))
|
||||||
|
onPause?.()
|
||||||
|
})
|
||||||
|
player.on('ended', () => setState((p) => ({ ...p, isPlaying: false })))
|
||||||
|
player.on('loadedmetadata', () => {
|
||||||
|
setIsPlayerReady(true)
|
||||||
|
})
|
||||||
|
player.on('loadstart', () => {
|
||||||
|
setIsPlayerReady(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
player.on('timeupdate', () => {
|
||||||
|
const playerDuration = player.duration() || data.media?.metadata?.duration || 0
|
||||||
|
|
||||||
|
const bufferedEnd = player.bufferedEnd()
|
||||||
|
if (playerDuration > 0) {
|
||||||
|
setBuffered((bufferedEnd / playerDuration) * 100)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
player.on('volumechange', () => {
|
||||||
|
const vol = player.volume()
|
||||||
|
const muted = player.muted()
|
||||||
|
setState((prev) => ({
|
||||||
|
...prev,
|
||||||
|
volume: vol || 0,
|
||||||
|
isMuted: muted || false,
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
|
||||||
|
player.on('ratechange', () => {
|
||||||
|
setState((prev) => ({
|
||||||
|
...prev,
|
||||||
|
playbackRate: player.playbackRate() || 1,
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
|
||||||
|
player.on('error', () => {
|
||||||
|
console.error('VideoJS Error:', player.error())
|
||||||
|
})
|
||||||
|
|
||||||
|
// Set initial state
|
||||||
|
player.volume(state.volume)
|
||||||
|
|
||||||
|
// 4. Cleanup
|
||||||
|
return () => {
|
||||||
|
if (player && !player.isDisposed()) {
|
||||||
|
player.dispose()
|
||||||
|
playerRef.current = null
|
||||||
|
videoRef.current = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [data])
|
||||||
|
|
||||||
|
// Handle changes to startTime (e.g., clicking different chunks in search results)
|
||||||
|
useEffect(() => {
|
||||||
|
if (!isPlayerReady) return
|
||||||
|
|
||||||
|
if (startTime !== undefined && startTime !== null && startTime > 0) {
|
||||||
|
if (startTime !== lastProcessedStartTimeRef.current) {
|
||||||
|
lastProcessedStartTimeRef.current = startTime
|
||||||
|
const targetFrame = Math.floor(startTime * frameRate + 0.45)
|
||||||
|
seekToFrame(targetFrame)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [startTime, frameRate, seekToFrame, isPlayerReady])
|
||||||
|
|
||||||
|
const scheduleHide = useCallback(() => {
|
||||||
|
if (controlsTimeoutRef.current) {
|
||||||
|
clearTimeout(controlsTimeoutRef.current)
|
||||||
|
}
|
||||||
|
// Never hide while the cursor rests over the control bar itself.
|
||||||
|
if (state.isFullScreen && !isOverControlsRef.current) {
|
||||||
|
controlsTimeoutRef.current = setTimeout(() => {
|
||||||
|
setIsControlsVisible(false)
|
||||||
|
}, 1200)
|
||||||
|
}
|
||||||
|
}, [state.isFullScreen])
|
||||||
|
|
||||||
|
const handleMouseMove = useCallback(() => {
|
||||||
|
// Always show controls on movement, then (re)start the idle hide timer.
|
||||||
|
setIsControlsVisible(true)
|
||||||
|
scheduleHide()
|
||||||
|
}, [scheduleHide])
|
||||||
|
|
||||||
|
const handleControlsMouseEnter = useCallback(() => {
|
||||||
|
isOverControlsRef.current = true
|
||||||
|
if (controlsTimeoutRef.current) clearTimeout(controlsTimeoutRef.current)
|
||||||
|
setIsControlsVisible(true)
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
const handleControlsMouseLeave = useCallback(() => {
|
||||||
|
isOverControlsRef.current = false
|
||||||
|
scheduleHide()
|
||||||
|
}, [scheduleHide])
|
||||||
|
|
||||||
|
const handleMouseLeave = useCallback(() => {
|
||||||
|
// If the cursor leaves the player while fullscreen, hide the controls.
|
||||||
|
if (state.isFullScreen) {
|
||||||
|
if (controlsTimeoutRef.current) clearTimeout(controlsTimeoutRef.current)
|
||||||
|
setIsControlsVisible(false)
|
||||||
|
}
|
||||||
|
}, [state.isFullScreen])
|
||||||
|
|
||||||
|
// Clean up timeout on unmount or state change
|
||||||
|
useEffect(() => {
|
||||||
|
return () => {
|
||||||
|
if (controlsTimeoutRef.current) clearTimeout(controlsTimeoutRef.current)
|
||||||
|
}
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
// Windowed mode: controls are always visible. Fullscreen: show now and let
|
||||||
|
// the idle timer hide them (re-runs on play/pause so pausing re-reveals them).
|
||||||
|
useEffect(() => {
|
||||||
|
if (!state.isFullScreen) {
|
||||||
|
setIsControlsVisible(true)
|
||||||
|
if (controlsTimeoutRef.current) clearTimeout(controlsTimeoutRef.current)
|
||||||
|
} else {
|
||||||
|
handleMouseMove()
|
||||||
|
}
|
||||||
|
}, [state.isPlaying, state.isFullScreen, handleMouseMove])
|
||||||
|
|
||||||
|
// -- Event Handlers --
|
||||||
|
|
||||||
|
const togglePlay = useCallback(() => {
|
||||||
|
// Disable click-to-play if drawing
|
||||||
|
if (useAnnotationStore.getState().isDrawing) return
|
||||||
|
|
||||||
|
const player = playerRef.current
|
||||||
|
if (!player) return
|
||||||
|
|
||||||
|
if (player.paused() || player.ended()) {
|
||||||
|
const playPromise = player.play()
|
||||||
|
if (playPromise !== undefined) {
|
||||||
|
playPromise.catch((error) => {
|
||||||
|
// Ignore AbortError which happens when pausing rapidly after playing
|
||||||
|
if (error.name === 'AbortError') return
|
||||||
|
console.error('Play failed:', error)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
player.pause()
|
||||||
|
}
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
const toggleLoop = () => {
|
||||||
|
const player = playerRef.current
|
||||||
|
if (!player) return
|
||||||
|
const newLoop = !state.isLooping
|
||||||
|
player.loop(newLoop)
|
||||||
|
setState((prev) => ({ ...prev, isLooping: newLoop }))
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleVolumeChange = (newVolume: number) => {
|
||||||
|
// Optimistic update to prevent slider jumping and ensure immediate UI feedback
|
||||||
|
setState((prev) => ({
|
||||||
|
...prev,
|
||||||
|
volume: newVolume,
|
||||||
|
isMuted: newVolume === 0, // If dragging to 0, consider it muted
|
||||||
|
}))
|
||||||
|
|
||||||
|
const player = playerRef.current
|
||||||
|
if (!player) return
|
||||||
|
|
||||||
|
// Set volume first so if we unmute, it is at the correct level
|
||||||
|
player.volume(newVolume)
|
||||||
|
|
||||||
|
// Manage mute state based on volume
|
||||||
|
if (newVolume > 0 && player.muted()) {
|
||||||
|
player.muted(false)
|
||||||
|
} else if (newVolume === 0 && !player.muted()) {
|
||||||
|
player.muted(true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const toggleMute = () => {
|
||||||
|
const player = playerRef.current
|
||||||
|
if (!player) return
|
||||||
|
|
||||||
|
const isMuted = player.muted()
|
||||||
|
if (isMuted) {
|
||||||
|
player.muted(false)
|
||||||
|
// If volume was 0 (e.g. user dragged to 0), restore to default 0.5 so they hear something
|
||||||
|
if (player.volume() === 0) {
|
||||||
|
player.volume(0.5)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
player.muted(true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const changePlaybackRate = (rate: number) => {
|
||||||
|
const player = playerRef.current
|
||||||
|
if (!player) return
|
||||||
|
player.playbackRate(rate)
|
||||||
|
}
|
||||||
|
|
||||||
|
const changeResolution = (res: DisplayTranscode) => {
|
||||||
|
const player = playerRef.current
|
||||||
|
if (!player) return
|
||||||
|
|
||||||
|
const wasPlaying = !player.paused()
|
||||||
|
const currentT = player.currentTime()
|
||||||
|
|
||||||
|
setState((prev) => ({
|
||||||
|
...prev,
|
||||||
|
currentResolution: res.resolution,
|
||||||
|
currentSrc: res.url,
|
||||||
|
}))
|
||||||
|
|
||||||
|
player.src({ type: 'video/mp4', src: res.url })
|
||||||
|
|
||||||
|
player.one('loadedmetadata', () => {
|
||||||
|
player.currentTime(currentT)
|
||||||
|
if (wasPlaying) {
|
||||||
|
const playPromise = player.play()
|
||||||
|
if (playPromise !== undefined) {
|
||||||
|
playPromise.catch((error) => {
|
||||||
|
if (error.name === 'AbortError') return
|
||||||
|
console.error('Play after seek failed:', error)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
player.playbackRate(state.playbackRate)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const toggleFullScreen = () => {
|
||||||
|
if (!rootRef.current) return
|
||||||
|
if (!document.fullscreenElement) {
|
||||||
|
rootRef.current.requestFullscreen()
|
||||||
|
setState((prev) => ({ ...prev, isFullScreen: true }))
|
||||||
|
} else {
|
||||||
|
document.exitFullscreen()
|
||||||
|
setState((prev) => ({ ...prev, isFullScreen: false }))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleDownload = async (key: string) => {
|
||||||
|
try {
|
||||||
|
const res = shareId
|
||||||
|
? await client.api.shares[':shareId'].files[':fileId']['download-url'].$post({
|
||||||
|
param: { shareId, fileId: data.id! },
|
||||||
|
json: { key },
|
||||||
|
})
|
||||||
|
: await client.api.files['download-url'].$post({
|
||||||
|
json: { key, assetId: data.id! },
|
||||||
|
})
|
||||||
|
if (!res.ok) return
|
||||||
|
const { url } = await res.json()
|
||||||
|
const link = document.createElement('a')
|
||||||
|
link.href = url
|
||||||
|
link.download = ''
|
||||||
|
document.body.appendChild(link)
|
||||||
|
link.click()
|
||||||
|
document.body.removeChild(link)
|
||||||
|
} catch {
|
||||||
|
// silently fail
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Listen for fullscreen change events (ESC key)
|
||||||
|
useEffect(() => {
|
||||||
|
const onFsChange = () => {
|
||||||
|
setState((prev) => ({
|
||||||
|
...prev,
|
||||||
|
isFullScreen: !!document.fullscreenElement,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
document.addEventListener('fullscreenchange', onFsChange)
|
||||||
|
return () => document.removeEventListener('fullscreenchange', onFsChange)
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
// Calculate center pan
|
||||||
|
const vidW = data.media?.metadata?.originalWidth ?? 1920
|
||||||
|
const vidH = data.media?.metadata?.originalHeight ?? 1080
|
||||||
|
|
||||||
|
const scale = zoom
|
||||||
|
const panX = (containerSize.width - vidW * scale) / 2
|
||||||
|
const panY = (containerSize.height - vidH * scale) / 2
|
||||||
|
|
||||||
|
if (!hasMedia) {
|
||||||
|
return (
|
||||||
|
<div className="flex h-full w-full items-center justify-center">
|
||||||
|
<p className="text-muted-foreground">Media is not available.</p>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
ref={rootRef}
|
||||||
|
className={cn(
|
||||||
|
'group shadow-2xl font-sans select-none flex flex-col mx-auto transition-all duration-300 relative',
|
||||||
|
state.isFullScreen ? 'h-full w-full rounded-none bg-black' : 'w-full h-full',
|
||||||
|
!isControlsVisible && state.isFullScreen ? 'cursor-none' : '',
|
||||||
|
)}
|
||||||
|
onMouseMove={handleMouseMove}
|
||||||
|
onMouseLeave={handleMouseLeave}
|
||||||
|
>
|
||||||
|
<div className="flex-1 flex flex-col-reverse md:flex-row min-h-0 relative">
|
||||||
|
{/* Render Carousel/Sidebar here if not fullscreen */}
|
||||||
|
{!state.isFullScreen && children}
|
||||||
|
|
||||||
|
{/* Video Area */}
|
||||||
|
<div
|
||||||
|
ref={containerRef}
|
||||||
|
className={cn(
|
||||||
|
'flex-1 bg-black cursor-pointer relative flex items-center justify-center overflow-hidden min-h-0',
|
||||||
|
)}
|
||||||
|
onClick={togglePlay}
|
||||||
|
data-vjs-player
|
||||||
|
data-testid="video-area"
|
||||||
|
>
|
||||||
|
{/* Hidden VideoJS container */}
|
||||||
|
<div ref={videoContainerRef} className="absolute inset-0 z-[-1]" />
|
||||||
|
|
||||||
|
{/* Drawing Canvas (Visible) */}
|
||||||
|
{videoHtmlEl && containerSize.width > 0 && (
|
||||||
|
<DrawingCanvas
|
||||||
|
width={containerSize.width}
|
||||||
|
height={containerSize.height}
|
||||||
|
mediaDimensions={{
|
||||||
|
width: vidW,
|
||||||
|
height: vidH,
|
||||||
|
}}
|
||||||
|
videoElement={videoHtmlEl}
|
||||||
|
annotations={displayAnnotations}
|
||||||
|
scale={scale}
|
||||||
|
offset={{ x: panX, y: panY }}
|
||||||
|
className="absolute inset-0"
|
||||||
|
onClick={togglePlay}
|
||||||
|
// Drawing Props
|
||||||
|
isDrawing={isDrawing}
|
||||||
|
currentTool={currentTool}
|
||||||
|
currentColor={currentColor}
|
||||||
|
onAddAnnotation={addAnnotation}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Big Play Button Overlay (when paused) */}
|
||||||
|
{!state.isPlaying && !isDrawing && (
|
||||||
|
<div className="absolute inset-0 flex items-center justify-center bg-black/20 pointer-events-none z-10">
|
||||||
|
<div className="w-20 h-20 bg-white/10 backdrop-blur-sm rounded-full flex items-center justify-center border-2 border-white/30 animate-pulse">
|
||||||
|
<Play className="w-10 h-10 text-white ml-1 fill-white" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<VideoControlBar
|
||||||
|
state={controlBarState}
|
||||||
|
zoom={zoom}
|
||||||
|
isControlsVisible={isControlsVisible}
|
||||||
|
buffered={buffered}
|
||||||
|
data={data}
|
||||||
|
resolutions={resolutions}
|
||||||
|
togglePlay={togglePlay}
|
||||||
|
toggleLoop={toggleLoop}
|
||||||
|
toggleMute={toggleMute}
|
||||||
|
handleVolumeChange={handleVolumeChange}
|
||||||
|
changePlaybackRate={changePlaybackRate}
|
||||||
|
changeResolution={changeResolution}
|
||||||
|
handleDownload={handleDownload}
|
||||||
|
toggleFullScreen={toggleFullScreen}
|
||||||
|
onZoomChange={handleZoomChange}
|
||||||
|
onZoomReset={handleZoomReset}
|
||||||
|
frameRate={frameRate}
|
||||||
|
totalFrames={totalFrames}
|
||||||
|
currentFrame={currentFrame}
|
||||||
|
seekToFrame={seekToFrame}
|
||||||
|
onMouseEnter={handleControlsMouseEnter}
|
||||||
|
onMouseLeave={handleControlsMouseLeave}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
export default VideoViewer
|
||||||
@@ -0,0 +1,195 @@
|
|||||||
|
# File Viewer Registry System
|
||||||
|
|
||||||
|
The **File Viewer Registry** is a modular, type-safe system that makes it easy for developers to add support for new file types (e.g., audio, markdown, pdf, images, and videos) in Shumai's WebUI.
|
||||||
|
|
||||||
|
By registering a file type, the detail pages (`FileViewer`), split-screen comparison interfaces (`CompareViewer`), and sidebars automatically adapt to the content, control schemes, and comment features of that file type.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. Core Architecture
|
||||||
|
|
||||||
|
The system is located under `packages/webui/components/viewers/` and is divided into:
|
||||||
|
* **Types Definition (`types.ts`)**: Outlines the standard props and imperative controllers every viewer must share.
|
||||||
|
* **Central Registry (`registry.ts`)**: Matches files dynamically using file metadata (mime-types, extensions, etc.) to load the correct viewer configuration.
|
||||||
|
* **Viewer Modules**: Self-contained directories containing components, control bars, styles, and tests for each file type (e.g. `video/`, `image/`).
|
||||||
|
|
||||||
|
```text
|
||||||
|
packages/webui/components/viewers/
|
||||||
|
├── types.ts # Shared types (MediaController, FileViewerProps)
|
||||||
|
├── registry.ts # Registry definition & matcher helpers
|
||||||
|
├── video/ # Video viewer module
|
||||||
|
│ ├── index.tsx # Video type definition export
|
||||||
|
│ ├── video-viewer.tsx # Content viewer (forwardRef)
|
||||||
|
│ ├── video-control-bar.tsx
|
||||||
|
│ └── compare-video-pane.tsx # Comparison pane
|
||||||
|
├── image/ # Image viewer module
|
||||||
|
│ ├── index.tsx # Image type definition export
|
||||||
|
│ ├── image-viewer.tsx
|
||||||
|
│ └── compare-image-pane.tsx
|
||||||
|
└── default/ # Fallback for unsupported types
|
||||||
|
├── index.tsx
|
||||||
|
└── default-viewer.tsx
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. Shared Interfaces (`types.ts`)
|
||||||
|
|
||||||
|
To ensure type safety across all file types, the registry enforces strict TypeScript definitions.
|
||||||
|
|
||||||
|
### `MediaController`
|
||||||
|
Abstracts time-based media control (e.g., seeking, playing, pausing) from outer layout and comments components:
|
||||||
|
```typescript
|
||||||
|
export interface MediaController {
|
||||||
|
play: () => void
|
||||||
|
pause: () => void
|
||||||
|
seekTo: (second: number) => void
|
||||||
|
getCurrentTime?: () => number
|
||||||
|
getDuration?: () => number
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### `FileTypeDefinition`
|
||||||
|
The schema that every registered file type configuration must satisfy:
|
||||||
|
```typescript
|
||||||
|
export interface FileTypeDefinition {
|
||||||
|
id: string; // Unique ID (e.g., 'video', 'image')
|
||||||
|
name: string;
|
||||||
|
match: (file: AssetInfo) => boolean;
|
||||||
|
|
||||||
|
// Renders the main content & control bar, forwarding MediaController ref
|
||||||
|
viewer: React.ForwardRefExoticComponent<
|
||||||
|
FileViewerProps & React.RefAttributes<MediaController>
|
||||||
|
>;
|
||||||
|
|
||||||
|
// Optional split-screen comparison pane
|
||||||
|
comparePane?: React.ForwardRefExoticComponent<
|
||||||
|
ComparePaneProps & React.RefAttributes<ComparePaneHandle>
|
||||||
|
>;
|
||||||
|
|
||||||
|
// Configuration for comments and sidebar features
|
||||||
|
commentsConfig?: {
|
||||||
|
hasTimestamp?: boolean; // Enables timestamped comments
|
||||||
|
hasAnnotations?: boolean; // Enables annotation drawing canvas
|
||||||
|
hasAiBots?: boolean; // Enables metadata/autofill AI bots
|
||||||
|
};
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. How to Add a New File Type (Step-by-Step)
|
||||||
|
|
||||||
|
To add support for a new file type (e.g., **Audio**):
|
||||||
|
|
||||||
|
### Step 1: Create the Folder Structure
|
||||||
|
Create a new folder under `packages/webui/components/viewers/audio/`.
|
||||||
|
|
||||||
|
### Step 2: Implement the Viewer Component
|
||||||
|
Create `audio-viewer.tsx`. This component handles the content viewport (e.g. waveform display) and the play controls. It **must** forward its ref as a `MediaController`:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import React, { useImperativeHandle, useRef } from 'react'
|
||||||
|
import { FileViewerProps, MediaController } from '../types'
|
||||||
|
|
||||||
|
export const AudioViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||||
|
({ file, children, onPlay, onTimeUpdate }, ref) => {
|
||||||
|
const audioEl = useRef<HTMLAudioElement | null>(null)
|
||||||
|
|
||||||
|
// Expose control actions to the parent pages/comment areas
|
||||||
|
useImperativeHandle(ref, () => ({
|
||||||
|
play: () => audioEl.current?.play(),
|
||||||
|
pause: () => audioEl.current?.pause(),
|
||||||
|
seekTo: (second: number) => {
|
||||||
|
if (audioEl.current) audioEl.current.currentTime = second
|
||||||
|
},
|
||||||
|
getCurrentTime: () => audioEl.current?.currentTime || 0,
|
||||||
|
getDuration: () => audioEl.current?.duration || 0,
|
||||||
|
}))
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex flex-col flex-1 h-full bg-background relative">
|
||||||
|
{/* Render Carousel/LeftSidebar if present */}
|
||||||
|
{children}
|
||||||
|
|
||||||
|
<div className="flex-1 flex items-center justify-center">
|
||||||
|
<audio
|
||||||
|
ref={audioEl}
|
||||||
|
src={file.media?.original?.downloadUrl}
|
||||||
|
onPlay={onPlay}
|
||||||
|
onTimeUpdate={(e) => onTimeUpdate?.((e.target as HTMLAudioElement).currentTime)}
|
||||||
|
controls
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Step 3: (Optional) Implement the Compare Pane Component
|
||||||
|
If your file type supports comparison mode, create `compare-audio-pane.tsx`. It must forward a `ComparePaneHandle` ref:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import React, { forwardRef, useImperativeHandle, useRef } from 'react'
|
||||||
|
import { ComparePaneProps } from '../types'
|
||||||
|
import { ComparePaneHandle } from '../../compare/types'
|
||||||
|
|
||||||
|
export const CompareAudioPane = forwardRef<ComparePaneHandle, CompareAudioPaneProps>(
|
||||||
|
({ file, isActive, onStateChange, onActivate }, ref) => {
|
||||||
|
const audioEl = useRef<HTMLAudioElement | null>(null)
|
||||||
|
|
||||||
|
useImperativeHandle(ref, () => ({
|
||||||
|
getKind: () => 'audio',
|
||||||
|
play: () => audioEl.current?.play(),
|
||||||
|
pause: () => audioEl.current?.pause(),
|
||||||
|
// ... implement remaining ComparePaneHandle methods
|
||||||
|
}))
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div onClick={onActivate} className="flex-1 flex flex-col items-center justify-center">
|
||||||
|
<audio ref={audioEl} src={file.media?.original?.downloadUrl} />
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Step 4: Create the Module Entrypoint
|
||||||
|
Create `index.tsx` inside your folder and define the `FileTypeDefinition`:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { FileTypeDefinition } from '../types'
|
||||||
|
import { AudioViewer } from './audio-viewer'
|
||||||
|
import { CompareAudioPane } from './compare-audio-pane'
|
||||||
|
|
||||||
|
export const audioTypeDefinition: FileTypeDefinition = {
|
||||||
|
id: 'audio',
|
||||||
|
name: 'Audio',
|
||||||
|
match: (file) => !!file.mediaType?.startsWith('audio/'),
|
||||||
|
viewer: AudioViewer,
|
||||||
|
comparePane: CompareAudioPane,
|
||||||
|
commentsConfig: {
|
||||||
|
hasTimestamp: true,
|
||||||
|
hasAnnotations: false,
|
||||||
|
hasAiBots: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Step 5: Register the File Type
|
||||||
|
Add the definition import and reference to the `registry` array in `packages/webui/components/viewers/registry.ts`:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { videoTypeDefinition } from './video'
|
||||||
|
import { imageTypeDefinition } from './image'
|
||||||
|
import { audioTypeDefinition } from './audio' // 1. Import
|
||||||
|
|
||||||
|
const registry: FileTypeDefinition[] = [
|
||||||
|
videoTypeDefinition,
|
||||||
|
imageTypeDefinition,
|
||||||
|
audioTypeDefinition, // 2. Add to registry
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
|
That's it! The detail page, sidebars, and comparison layout will now dynamically resolve and support your new file type.
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
import { type ReactElement, useCallback, useRef, useState } from 'react'
|
import { type ReactElement, useCallback, useRef, useState } from 'react'
|
||||||
import ReactDOM from 'react-dom/client'
|
import ReactDOM from 'react-dom/client'
|
||||||
import type Player from 'video.js/dist/types/player'
|
import type { MediaController } from '@/ui/components/viewers/types'
|
||||||
import VideoPlayer from '@/ui/components/viewers/video-player'
|
import VideoPlayer from '@/ui/components/viewers/video/video-viewer'
|
||||||
import { calculateFrameCenterTime } from '@/ui/components/viewers/utils'
|
import { calculateFrameCenterTime } from '@/ui/components/viewers/video/utils'
|
||||||
import { useUiStore } from '@/ui/stores/ui'
|
import { useUiStore } from '@/ui/stores/ui'
|
||||||
import {
|
import {
|
||||||
SAMPLE_FRAME_RATE,
|
SAMPLE_FRAME_RATE,
|
||||||
@@ -70,7 +70,7 @@ interface HarnessComment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function Harness(): ReactElement {
|
function Harness(): ReactElement {
|
||||||
const playerRef = useRef<Player | null>(null)
|
const playerRef = useRef<MediaController | null>(null)
|
||||||
// Latest player time (seconds) as reported by the real onTimeUpdate prop.
|
// Latest player time (seconds) as reported by the real onTimeUpdate prop.
|
||||||
const currentTimeRef = useRef<number>(0)
|
const currentTimeRef = useRef<number>(0)
|
||||||
const seekInputRef = useRef<HTMLInputElement>(null)
|
const seekInputRef = useRef<HTMLInputElement>(null)
|
||||||
@@ -92,7 +92,7 @@ function Harness(): ReactElement {
|
|||||||
if (!player) return
|
if (!player) return
|
||||||
const frame = Number.parseInt(seekInputRef.current?.value ?? '', 10)
|
const frame = Number.parseInt(seekInputRef.current?.value ?? '', 10)
|
||||||
if (Number.isNaN(frame)) return
|
if (Number.isNaN(frame)) return
|
||||||
player.currentTime(calculateFrameCenterTime(frame, SAMPLE_FRAME_RATE))
|
player.seekTo(calculateFrameCenterTime(frame, SAMPLE_FRAME_RATE))
|
||||||
player.pause()
|
player.pause()
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
@@ -114,7 +114,7 @@ function Harness(): ReactElement {
|
|||||||
const handleCommentClick = useCallback((second: number) => {
|
const handleCommentClick = useCallback((second: number) => {
|
||||||
const player = playerRef.current
|
const player = playerRef.current
|
||||||
if (!player) return
|
if (!player) return
|
||||||
player.currentTime(second)
|
player.seekTo(second)
|
||||||
player.pause()
|
player.pause()
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
@@ -132,8 +132,8 @@ function Harness(): ReactElement {
|
|||||||
<div className="flex flex-col flex-1 h-full overflow-hidden relative">
|
<div className="flex flex-col flex-1 h-full overflow-hidden relative">
|
||||||
<VideoPlayer
|
<VideoPlayer
|
||||||
key={playerKey}
|
key={playerKey}
|
||||||
data={harnessAsset}
|
file={harnessAsset}
|
||||||
playerRef={playerRef}
|
ref={playerRef}
|
||||||
onTimeUpdate={handleTimeUpdate}
|
onTimeUpdate={handleTimeUpdate}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ import { keepPreviousData, useQuery, useQueryClient } from '@tanstack/react-quer
|
|||||||
import { createFileRoute, useNavigate } from '@tanstack/react-router'
|
import { createFileRoute, useNavigate } from '@tanstack/react-router'
|
||||||
import { Loader2 } from 'lucide-react'
|
import { Loader2 } from 'lucide-react'
|
||||||
import { useEffect, useRef, useState } from 'react'
|
import { useEffect, useRef, useState } from 'react'
|
||||||
import type Player from 'video.js/dist/types/player'
|
import type { MediaController } from '@/ui/components/viewers/types'
|
||||||
|
|
||||||
function FileViewPage() {
|
function FileViewPage() {
|
||||||
const { projectId, fileId } = Route.useParams()
|
const { projectId, fileId } = Route.useParams()
|
||||||
@@ -32,7 +32,7 @@ function FileViewPage() {
|
|||||||
const compareActiveSide = search.cmpActive ?? 'left'
|
const compareActiveSide = search.cmpActive ?? 'left'
|
||||||
const isCompareMode = !!search.compare && !!compareLeftId && !!compareRightId
|
const isCompareMode = !!search.compare && !!compareLeftId && !!compareRightId
|
||||||
|
|
||||||
const videoRef = useRef<Player | null>(null)
|
const mediaControllerRef = useRef<MediaController | null>(null)
|
||||||
const { teamId, ensureTeamIdForProject } = useTeamContextStore()
|
const { teamId, ensureTeamIdForProject } = useTeamContextStore()
|
||||||
const [rightSidebarWidth, setRightSidebarWidth] = useState(360)
|
const [rightSidebarWidth, setRightSidebarWidth] = useState(360)
|
||||||
const [annotations, setAnnotations] = useState<Annotation[]>([])
|
const [annotations, setAnnotations] = useState<Annotation[]>([])
|
||||||
@@ -368,14 +368,10 @@ function FileViewPage() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (comment.second !== null && comment.second !== undefined) {
|
if (comment.second !== null && comment.second !== undefined) {
|
||||||
if (videoRef.current) {
|
mediaControllerRef.current?.seekTo(comment.second)
|
||||||
videoRef.current.currentTime(comment.second)
|
mediaControllerRef.current?.pause()
|
||||||
videoRef.current.pause()
|
|
||||||
}
|
|
||||||
} else if (newAnnotations && newAnnotations.length > 0) {
|
} else if (newAnnotations && newAnnotations.length > 0) {
|
||||||
if (videoRef.current) {
|
mediaControllerRef.current?.pause()
|
||||||
videoRef.current.pause()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -415,7 +411,7 @@ function FileViewPage() {
|
|||||||
) : (
|
) : (
|
||||||
<FileViewer
|
<FileViewer
|
||||||
file={fileData as unknown as AssetInfo}
|
file={fileData as unknown as AssetInfo}
|
||||||
videoRef={videoRef}
|
mediaControllerRef={mediaControllerRef}
|
||||||
onPlay={handlePlay}
|
onPlay={handlePlay}
|
||||||
onTimeUpdate={setCurrentTime}
|
onTimeUpdate={setCurrentTime}
|
||||||
annotations={annotations}
|
annotations={annotations}
|
||||||
@@ -450,9 +446,7 @@ function FileViewPage() {
|
|||||||
currentTime={currentTime}
|
currentTime={currentTime}
|
||||||
onTyping={() => {
|
onTyping={() => {
|
||||||
if (isCompareMode) return
|
if (isCompareMode) return
|
||||||
if (videoRef.current) {
|
mediaControllerRef.current?.pause()
|
||||||
videoRef.current.pause()
|
|
||||||
}
|
|
||||||
}}
|
}}
|
||||||
selectedCommentId={selectedCommentId}
|
selectedCommentId={selectedCommentId}
|
||||||
/>
|
/>
|
||||||
|
|||||||
Reference in New Issue
Block a user