fix(webui): restore full width bottom bar spanning across carousel in file viewer

This commit is contained in:
yiling
2026-08-25 21:27:20 +08:00
parent 59e641ccb3
commit 49f3fc0be5
6 changed files with 205 additions and 151 deletions
+13 -3
View File
@@ -5,15 +5,25 @@ import { afterEach, describe, expect, it, vi } from 'vitest'
import { FileViewer } from './file-viewer'
import type { AssetInfo } from '@shumai/dtos'
const MockViewer = React.forwardRef<unknown, { file: AssetInfo; children?: React.ReactNode }>(
({ file, children }, ref) => {
React.useImperativeHandle(ref, () => ({}))
return (
<div>
<div data-testid={`viewer-${file.id}`}>{file.name}</div>
{children}
</div>
)
},
)
// Mock registry so getViewerForFile returns a simple test viewer
vi.mock('./viewers/registry', () => ({
getViewerForFile: () => ({
id: 'test-viewer',
name: 'Test Viewer',
match: () => true,
viewer: React.forwardRef<unknown, { file: AssetInfo }>(({ file }) => (
<div data-testid={`viewer-${file.id}`}>{file.name}</div>
)),
viewer: MockViewer,
}),
}))
+13 -16
View File
@@ -22,22 +22,19 @@ export function FileViewer({
const ViewerComponent = viewerDef.viewer
return (
<div className="flex flex-col-reverse md:flex-row flex-1 h-full min-h-0 relative">
<ViewerComponent
key={viewerDef.id}
ref={mediaControllerRef}
file={file}
onPlay={onPlay}
onPause={onPause}
onTimeUpdate={onTimeUpdate}
annotations={annotations}
startTime={startTime}
shareId={shareId}
allowDownload={allowDownload}
>
{children}
<div className="flex-1 min-w-0 h-full flex flex-col">
<ViewerComponent
key={file.id}
ref={mediaControllerRef}
file={file}
onPlay={onPlay}
onPause={onPause}
onTimeUpdate={onTimeUpdate}
annotations={annotations}
startTime={startTime}
shareId={shareId}
allowDownload={allowDownload}
/>
</div>
</div>
</ViewerComponent>
)
}
@@ -4,7 +4,7 @@ import React, { useImperativeHandle } from 'react'
import { FileViewerProps, MediaController } from '../types'
export const DefaultViewer = React.forwardRef<MediaController, FileViewerProps>(
({ file, shareId, allowDownload = true }, ref) => {
({ file, shareId, children, allowDownload = true }, ref) => {
// Implement no-op media controller
useImperativeHandle(ref, () => ({
play: () => {},
@@ -39,8 +39,11 @@ export const DefaultViewer = React.forwardRef<MediaController, FileViewerProps>(
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 items-center justify-center">
<p className="text-muted-foreground">Preview unavailable</p>
<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">
{allowDownload && (
@@ -10,7 +10,7 @@ import { centeredPan, fitScale, isZoomed, zoomAtPoint } from '../pan-zoom'
import { usePanZoomGestures } from '../use-pan-zoom'
export const ImageViewer = React.forwardRef<MediaController, FileViewerProps>(
({ file, annotations, shareId, allowDownload }, ref) => {
({ file, annotations, shareId, children, allowDownload }, ref) => {
// Implement no-op media controller since images are static
useImperativeHandle(ref, () => ({
play: () => {},
@@ -35,6 +35,11 @@ export const ImageViewer = React.forwardRef<MediaController, FileViewerProps>(
const containerRef = useRef<HTMLDivElement | null>(null)
useEffect(() => {
setHasManuallyZoomed(false)
setCopied(false)
}, [file.id])
useEffect(() => {
const node = containerRef.current
if (!node) return
@@ -173,31 +178,34 @@ export const ImageViewer = React.forwardRef<MediaController, FileViewerProps>(
return (
<div className="flex flex-col flex-1 h-full overflow-hidden bg-gray-100 dark:bg-gray-950 relative">
<div ref={containerRef} className="flex-1 relative overflow-hidden touch-none">
{bestUrl ? (
<DrawingCanvas
width={conW}
height={conH}
mediaDimensions={{
width: imgW,
height: imgH,
}}
imageUrl={bestUrl}
annotations={displayAnnotations}
scale={zoom}
offset={pan}
onPan={isZoomed(zoom, baseScale) ? setPan : undefined}
className="absolute inset-0 z-0"
isDrawing={isDrawing}
currentTool={currentTool}
currentColor={currentColor}
onAddAnnotation={addAnnotation}
/>
) : (
<div className="flex h-full w-full items-center justify-center">
<p className="text-muted-foreground">Preview unavailable</p>
</div>
)}
<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 touch-none">
{bestUrl ? (
<DrawingCanvas
width={conW}
height={conH}
mediaDimensions={{
width: imgW,
height: imgH,
}}
imageUrl={bestUrl}
annotations={displayAnnotations}
scale={zoom}
offset={pan}
onPan={isZoomed(zoom, baseScale) ? setPan : undefined}
className="absolute inset-0 z-0"
isDrawing={isDrawing}
currentTool={currentTool}
currentColor={currentColor}
onAddAnnotation={addAnnotation}
/>
) : (
<div className="flex h-full w-full items-center justify-center">
<p className="text-muted-foreground">Preview unavailable</p>
</div>
)}
</div>
</div>
<ImageControlBar
zoom={zoom}
@@ -31,7 +31,17 @@ if (typeof window !== 'undefined' && pdfjsLib.GlobalWorkerOptions) {
export const PdfViewer = React.forwardRef<MediaController, FileViewerProps>(
(
{ file, annotations, shareId, onPlay, onPause, onTimeUpdate, startTime, allowDownload },
{
file,
annotations,
shareId,
children,
onPlay,
onPause,
onTimeUpdate,
startTime,
allowDownload,
},
ref,
) => {
const {
@@ -127,6 +137,10 @@ export const PdfViewer = React.forwardRef<MediaController, FileViewerProps>(
const [pan, setPan] = useState({ x: 0, y: 0 })
const [hasManuallyZoomed, setHasManuallyZoomed] = useState(false)
useEffect(() => {
setHasManuallyZoomed(false)
}, [file.id])
const containerRef = useRef<HTMLDivElement | null>(null)
useEffect(() => {
@@ -382,53 +396,56 @@ export const PdfViewer = React.forwardRef<MediaController, FileViewerProps>(
return (
<div className="flex flex-col flex-1 h-full overflow-hidden bg-gray-100 dark:bg-gray-950 relative">
<div ref={containerRef} className="flex-1 relative overflow-hidden touch-none">
{loading ? (
<div className="absolute inset-0 flex items-center justify-center bg-black/40 z-20">
<div className="w-8 h-8 border-4 border-white/30 border-t-white rounded-full animate-spin" />
</div>
) : error ? (
<div className="absolute inset-0 flex items-center justify-center text-gray-500 text-sm">
{error}
</div>
) : (
<DrawingCanvas
width={conW}
height={conH}
mediaDimensions={pageDimensions}
canvasElement={pageCanvas}
annotations={displayAnnotations}
scale={zoom}
offset={pan}
className="absolute inset-0 z-0"
isDrawing={isDrawing}
currentTool={currentTool}
currentColor={currentColor}
onAddAnnotation={addAnnotation}
<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 touch-none">
{loading ? (
<div className="absolute inset-0 flex items-center justify-center bg-black/40 z-20">
<div className="w-8 h-8 border-4 border-white/30 border-t-white rounded-full animate-spin" />
</div>
) : error ? (
<div className="absolute inset-0 flex items-center justify-center text-gray-500 text-sm">
{error}
</div>
) : (
<DrawingCanvas
width={conW}
height={conH}
mediaDimensions={pageDimensions}
canvasElement={pageCanvas}
annotations={displayAnnotations}
scale={zoom}
offset={pan}
className="absolute inset-0 z-0"
isDrawing={isDrawing}
currentTool={currentTool}
currentColor={currentColor}
onAddAnnotation={addAnnotation}
/>
)}
{/* Invisible selectable text overlay, transformed to match the Konva
stage. It sits ON TOP of the page canvas so the native selection
highlight is visible; pointer events are enabled only when not
drawing, so drags reach the stage below in draw mode. */}
<div
ref={textLayerContainerRef}
className="pdf-text-layer"
onMouseDown={(e) => {
// The container is user-select:none, so Chromium no longer
// clears the selection when clicking empty space (only text
// spans move the caret). Restore the expected behavior: a
// press on the container/br (not a text span) deselects.
if (e.target instanceof HTMLElement && e.target.tagName !== 'SPAN') {
window.getSelection()?.removeAllRanges()
}
}}
style={{
transform: `translate(${pan.x}px, ${pan.y}px) scale(${zoom})`,
transformOrigin: '0 0',
pointerEvents: isDrawing ? 'none' : 'auto',
}}
/>
)}
{/* Invisible selectable text overlay, transformed to match the Konva
stage. It sits ON TOP of the page canvas so the native selection
highlight is visible; pointer events are enabled only when not
drawing, so drags reach the stage below in draw mode. */}
<div
ref={textLayerContainerRef}
className="pdf-text-layer"
onMouseDown={(e) => {
// The container is user-select:none, so Chromium no longer
// clears the selection when clicking empty space (only text
// spans move the caret). Restore the expected behavior: a
// press on the container/br (not a text span) deselects.
if (e.target instanceof HTMLElement && e.target.tagName !== 'SPAN') {
window.getSelection()?.removeAllRanges()
}
}}
style={{
transform: `translate(${pan.x}px, ${pan.y}px) scale(${zoom})`,
transformOrigin: '0 0',
pointerEvents: isDrawing ? 'none' : 'auto',
}}
/>
</div>
</div>
<PdfControlBar
currentPage={currentPage}
@@ -15,7 +15,17 @@ import { usePanZoomGestures } from '../use-pan-zoom'
const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
(
{ file: data, onPlay, onPause, onTimeUpdate, annotations, startTime, shareId, allowDownload },
{
file: data,
onPlay,
onPause,
onTimeUpdate,
annotations,
startTime,
shareId,
children,
allowDownload,
},
ref,
) => {
const localPlayerRef = useRef<Player | null>(null)
@@ -117,6 +127,10 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
const [hasManuallyZoomed, setHasManuallyZoomed] = useState(false)
const [containerSize, setContainerSize] = useState({ width: 0, height: 0 })
useEffect(() => {
setHasManuallyZoomed(false)
}, [data.id])
const vidW = data.media?.metadata?.originalWidth || 1920
const vidH = data.media?.metadata?.originalHeight || 1080
const baseScale = fitScale(containerSize.width, containerSize.height, vidW, vidH)
@@ -622,64 +636,69 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
onMouseMove={handleMouseMove}
onMouseLeave={handleMouseLeave}
>
{/* Video Area */}
<div
ref={containerRef}
className={cn(
'flex-1 bg-black cursor-pointer relative flex items-center justify-center overflow-hidden min-h-0 touch-none',
)}
onClick={togglePlay}
data-vjs-player
data-testid="video-area"
>
{/* Hidden VideoJS container */}
<div ref={videoContainerRef} className="absolute inset-0 z-[-1]" />
<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}
{isAudio ? (
<div className="flex flex-col items-center justify-center text-muted-foreground w-full h-full pointer-events-none select-none">
<AudioLines
className={cn(
'w-16 h-16 text-foreground/75 transition-transform duration-500',
state.isPlaying ? 'animate-pulse scale-110 text-primary' : '',
)}
/>
</div>
) : (
/* 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={pan}
className="absolute inset-0"
// Play/pause is handled by the video-area div's onClick; a
// Konva-level onClick would double-toggle once the overlay
// becomes interactive (zoomed).
// Drawing Props
isDrawing={isDrawing}
currentTool={currentTool}
currentColor={currentColor}
onAddAnnotation={addAnnotation}
/>
)
)}
{/* Video Area */}
<div
ref={containerRef}
className={cn(
'flex-1 bg-black cursor-pointer relative flex items-center justify-center overflow-hidden min-h-0 touch-none',
)}
onClick={togglePlay}
data-vjs-player
data-testid="video-area"
>
{/* Hidden VideoJS container */}
<div ref={videoContainerRef} className="absolute inset-0 z-[-1]" />
{/* 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" />
{isAudio ? (
<div className="flex flex-col items-center justify-center text-muted-foreground w-full h-full pointer-events-none select-none">
<AudioLines
className={cn(
'w-16 h-16 text-foreground/75 transition-transform duration-500',
state.isPlaying ? 'animate-pulse scale-110 text-primary' : '',
)}
/>
</div>
</div>
)}
) : (
/* 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={pan}
className="absolute inset-0"
// Play/pause is handled by the video-area div's onClick; a
// Konva-level onClick would double-toggle once the overlay
// becomes interactive (zoomed).
// 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