mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-26 10:55:27 +00:00
fix(comments): size printable comment thumbnails to match media aspect ratio (#551)
This commit is contained in:
@@ -3,13 +3,12 @@ import type { Annotation } from '@/ui/types'
|
||||
import { ImageOff, Loader2 } from 'lucide-react'
|
||||
import React, { useEffect, useRef, useState } from 'react'
|
||||
import { AnnotationOverlaySvg } from './annotation-overlay-svg'
|
||||
|
||||
const BOX_WIDTH = 200
|
||||
const BOX_HEIGHT = 112
|
||||
import { computeCommentFrameBox, ERROR_MIN_HEIGHT, type Dimensions } from './sizing'
|
||||
|
||||
interface CommentFrameThumbnailProps {
|
||||
src: string
|
||||
annotations: Annotation[]
|
||||
initialDimensions?: Dimensions | null
|
||||
alt?: string
|
||||
onStatusChange?: (status: 'loaded' | 'error') => void
|
||||
}
|
||||
@@ -17,33 +16,32 @@ interface CommentFrameThumbnailProps {
|
||||
export const CommentFrameThumbnail: React.FC<CommentFrameThumbnailProps> = ({
|
||||
src,
|
||||
annotations,
|
||||
initialDimensions = null,
|
||||
alt = '',
|
||||
onStatusChange,
|
||||
}) => {
|
||||
const [status, setStatus] = useState<'loading' | 'loaded' | 'error'>('loading')
|
||||
const [naturalSize, setNaturalSize] = useState<{ width: number; height: number }>({
|
||||
width: BOX_WIDTH,
|
||||
height: BOX_HEIGHT,
|
||||
})
|
||||
const [naturalSize, setNaturalSize] = useState<Dimensions | null>(initialDimensions)
|
||||
const imgRef = useRef<HTMLImageElement>(null)
|
||||
const onStatusChangeRef = useRef(onStatusChange)
|
||||
onStatusChangeRef.current = onStatusChange
|
||||
|
||||
useEffect(() => {
|
||||
setStatus('loading')
|
||||
setNaturalSize(initialDimensions)
|
||||
const img = imgRef.current
|
||||
if (img && img.complete && img.naturalWidth > 0) {
|
||||
if (img && img.complete && img.naturalWidth > 0 && img.naturalHeight > 0) {
|
||||
setNaturalSize({ width: img.naturalWidth, height: img.naturalHeight })
|
||||
setStatus('loaded')
|
||||
onStatusChangeRef.current?.('loaded')
|
||||
}
|
||||
}, [src])
|
||||
}, [src, initialDimensions])
|
||||
|
||||
const handleLoad = (e: React.SyntheticEvent<HTMLImageElement>) => {
|
||||
const img = e.currentTarget
|
||||
const nw = img.naturalWidth || BOX_WIDTH
|
||||
const nh = img.naturalHeight || BOX_HEIGHT
|
||||
setNaturalSize({ width: nw, height: nh })
|
||||
if (img.naturalWidth > 0 && img.naturalHeight > 0) {
|
||||
setNaturalSize({ width: img.naturalWidth, height: img.naturalHeight })
|
||||
}
|
||||
setStatus('loaded')
|
||||
onStatusChangeRef.current?.('loaded')
|
||||
}
|
||||
@@ -53,16 +51,13 @@ export const CommentFrameThumbnail: React.FC<CommentFrameThumbnailProps> = ({
|
||||
onStatusChangeRef.current?.('error')
|
||||
}
|
||||
|
||||
// Fit the actual image rect inside 200x112 while preserving aspect ratio so the
|
||||
// SVG markup overlay aligns accurately for portrait, square, PDF, and ultrawide media.
|
||||
const scale = Math.min(BOX_WIDTH / naturalSize.width, BOX_HEIGHT / naturalSize.height)
|
||||
const fittedWidth = Math.max(1, Math.round(naturalSize.width * scale))
|
||||
const fittedHeight = Math.max(1, Math.round(naturalSize.height * scale))
|
||||
const { width: boxWidth, height: boxHeight } = computeCommentFrameBox(naturalSize)
|
||||
const containerHeight = status === 'error' ? Math.max(boxHeight, ERROR_MIN_HEIGHT) : boxHeight
|
||||
|
||||
return (
|
||||
<div
|
||||
className="relative flex items-center justify-center overflow-hidden rounded-sm border border-border transparency-checkerboard shrink-0"
|
||||
style={{ width: BOX_WIDTH, height: BOX_HEIGHT }}
|
||||
style={{ width: boxWidth, height: containerHeight }}
|
||||
data-testid="print-comment-frame"
|
||||
>
|
||||
{status === 'loading' && (
|
||||
@@ -84,32 +79,24 @@ export const CommentFrameThumbnail: React.FC<CommentFrameThumbnailProps> = ({
|
||||
<span className="text-[11px] leading-tight">{m.print_comments_frame_unavailable()}</span>
|
||||
</div>
|
||||
) : (
|
||||
<div
|
||||
className="relative"
|
||||
style={{
|
||||
width: status === 'loaded' ? fittedWidth : BOX_WIDTH,
|
||||
height: status === 'loaded' ? fittedHeight : BOX_HEIGHT,
|
||||
}}
|
||||
>
|
||||
<>
|
||||
<img
|
||||
ref={imgRef}
|
||||
src={src}
|
||||
alt={alt}
|
||||
onLoad={handleLoad}
|
||||
onError={handleError}
|
||||
className={`block h-full w-full object-contain ${
|
||||
status === 'loaded' ? 'opacity-100' : 'opacity-0'
|
||||
}`}
|
||||
className={`block h-full w-full ${status === 'loaded' ? 'opacity-100' : 'opacity-0'}`}
|
||||
/>
|
||||
{status === 'loaded' && annotations.length > 0 && (
|
||||
<AnnotationOverlaySvg
|
||||
annotations={annotations}
|
||||
width={fittedWidth}
|
||||
height={fittedHeight}
|
||||
width={boxWidth}
|
||||
height={boxHeight}
|
||||
className="pointer-events-none absolute inset-0 h-full w-full z-10"
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
|
||||
@@ -4,6 +4,11 @@ import React from 'react'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import type { AssetInfo, CommentInfo } from '@shumai/dtos'
|
||||
import { PrintCommentsView } from './print-comments-view'
|
||||
import {
|
||||
computeCommentFrameBox,
|
||||
fitHeaderThumbnailBox,
|
||||
resolveAssetDisplayDimensions,
|
||||
} from './sizing'
|
||||
|
||||
function makeComment(overrides: Partial<CommentInfo> = {}): CommentInfo {
|
||||
return {
|
||||
@@ -368,4 +373,129 @@ describe('PrintCommentsView', () => {
|
||||
expect(screen.getByTestId('print-frame-error').textContent).toContain('Frame unavailable')
|
||||
expect(screen.queryByTestId('print-frames-progress')).toBeNull()
|
||||
})
|
||||
|
||||
it('sizes header and comment frame containers tightly with zero padding across aspect ratios', () => {
|
||||
const cases: Array<{
|
||||
label: string
|
||||
w: number
|
||||
h: number
|
||||
headerW: number
|
||||
headerH: number
|
||||
frameW: number
|
||||
frameH: number
|
||||
}> = [
|
||||
{ label: '16:9', w: 1920, h: 1080, headerW: 96, headerH: 54, frameW: 200, frameH: 113 },
|
||||
{ label: '9:16', w: 1080, h: 1920, headerW: 30, headerH: 54, frameW: 200, frameH: 356 },
|
||||
{
|
||||
label: '9:16 (704x1280)',
|
||||
w: 704,
|
||||
h: 1280,
|
||||
headerW: 30,
|
||||
headerH: 54,
|
||||
frameW: 200,
|
||||
frameH: 364,
|
||||
},
|
||||
{ label: '4:3', w: 1440, h: 1080, headerW: 72, headerH: 54, frameW: 200, frameH: 150 },
|
||||
{ label: '3:4', w: 1080, h: 1440, headerW: 41, headerH: 54, frameW: 200, frameH: 267 },
|
||||
{ label: '1:1', w: 1080, h: 1080, headerW: 54, headerH: 54, frameW: 200, frameH: 200 },
|
||||
{ label: '21:9', w: 2560, h: 1080, headerW: 96, headerH: 41, frameW: 200, frameH: 84 },
|
||||
]
|
||||
|
||||
for (const c of cases) {
|
||||
expect(fitHeaderThumbnailBox({ width: c.w, height: c.h })).toEqual({
|
||||
width: c.headerW,
|
||||
height: c.headerH,
|
||||
})
|
||||
expect(computeCommentFrameBox({ width: c.w, height: c.h })).toEqual({
|
||||
width: c.frameW,
|
||||
height: c.frameH,
|
||||
})
|
||||
}
|
||||
|
||||
// Rotation metadata swaps raw dimensions when transcodes are absent
|
||||
const rotatedAsset: AssetInfo = {
|
||||
...videoAsset,
|
||||
preview: {
|
||||
thumbnailUrl: 'https://example.com/thumb.jpg',
|
||||
originalWidth: 1920,
|
||||
originalHeight: 1080,
|
||||
rotation: 90,
|
||||
},
|
||||
media: {
|
||||
proxyType: 'video',
|
||||
imageTranscodes: [],
|
||||
videoTranscodes: [],
|
||||
},
|
||||
}
|
||||
expect(resolveAssetDisplayDimensions(rotatedAsset)).toEqual({ width: 1080, height: 1920 })
|
||||
|
||||
// Verify DOM container styles & SVG viewBox on a 9:16 portrait video
|
||||
const portraitAsset: AssetInfo = {
|
||||
...videoAsset,
|
||||
preview: {
|
||||
thumbnailUrl: 'https://example.com/portrait-thumb.jpg',
|
||||
originalWidth: 704,
|
||||
originalHeight: 1280,
|
||||
},
|
||||
media: {
|
||||
proxyType: 'video',
|
||||
imageTranscodes: [],
|
||||
videoTranscodes: [
|
||||
{
|
||||
id: 'v1',
|
||||
key: 'k1',
|
||||
url: 'https://example.com/v.mp4',
|
||||
width: 704,
|
||||
height: 1280,
|
||||
size: 1000,
|
||||
},
|
||||
],
|
||||
metadata: { frameRate: 24 },
|
||||
},
|
||||
}
|
||||
|
||||
const { container } = render(
|
||||
<PrintCommentsView
|
||||
asset={portraitAsset}
|
||||
breadcrumbs={['Project']}
|
||||
comments={[
|
||||
makeComment({
|
||||
id: 'c-portrait',
|
||||
second: 1.2,
|
||||
annotations: [
|
||||
{
|
||||
type: 'box',
|
||||
color: '#ef4444',
|
||||
points: [
|
||||
[0.1, 0.1],
|
||||
[0.9, 0.9],
|
||||
],
|
||||
},
|
||||
],
|
||||
}),
|
||||
]}
|
||||
/>,
|
||||
)
|
||||
|
||||
// Before image loads, containers already size to metadata aspect ratio (no layout shift)
|
||||
const headerBox = screen.getByTestId('print-header-thumbnail')
|
||||
expect(headerBox.style.width).toBe('30px')
|
||||
expect(headerBox.style.height).toBe('54px')
|
||||
|
||||
const frameBox = screen.getByTestId('print-comment-frame')
|
||||
expect(frameBox.style.width).toBe('200px')
|
||||
expect(frameBox.style.height).toBe('364px')
|
||||
|
||||
// When a frame loads with different actual dimensions (e.g. a 3:4 page), container & SVG update
|
||||
const frameImg = container.querySelector('[data-testid="print-comment-frame"] img')!
|
||||
Object.defineProperty(frameImg, 'naturalWidth', { value: 300, configurable: true })
|
||||
Object.defineProperty(frameImg, 'naturalHeight', { value: 400, configurable: true })
|
||||
fireEvent.load(frameImg)
|
||||
|
||||
expect(frameBox.style.width).toBe('200px')
|
||||
expect(frameBox.style.height).toBe('267px')
|
||||
expect(screen.getByTestId('print-annotation-overlay').getAttribute('viewBox')).toBe(
|
||||
'0 0 200 267',
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -16,10 +16,16 @@ import {
|
||||
Loader2,
|
||||
Printer,
|
||||
} from 'lucide-react'
|
||||
import React, { useCallback, useMemo, useState } from 'react'
|
||||
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
||||
import Markdown from 'react-markdown'
|
||||
import { parseAnnotations } from './annotation-overlay-svg'
|
||||
import { CommentFrameThumbnail } from './comment-frame-thumbnail'
|
||||
import {
|
||||
fitHeaderThumbnailBox,
|
||||
HEADER_MAX_HEIGHT,
|
||||
resolveAssetDisplayDimensions,
|
||||
type Dimensions,
|
||||
} from './sizing'
|
||||
|
||||
export type PrintSortMode = 'position' | 'oldest' | 'newest'
|
||||
|
||||
@@ -132,6 +138,66 @@ function renderPlainCommentText(comment: CommentInfo): React.ReactNode {
|
||||
})
|
||||
}
|
||||
|
||||
interface HeaderThumbnailProps {
|
||||
url: string | null
|
||||
proxyType: string | null
|
||||
initialDimensions: Dimensions | null
|
||||
}
|
||||
|
||||
const HeaderThumbnail: React.FC<HeaderThumbnailProps> = ({ url, proxyType, initialDimensions }) => {
|
||||
const [naturalSize, setNaturalSize] = useState<Dimensions | null>(initialDimensions)
|
||||
const imgRef = useRef<HTMLImageElement>(null)
|
||||
|
||||
useEffect(() => {
|
||||
setNaturalSize(initialDimensions)
|
||||
const img = imgRef.current
|
||||
if (img && img.complete && img.naturalWidth > 0 && img.naturalHeight > 0) {
|
||||
setNaturalSize({ width: img.naturalWidth, height: img.naturalHeight })
|
||||
}
|
||||
}, [url, initialDimensions])
|
||||
|
||||
if (!url) {
|
||||
return (
|
||||
<div
|
||||
className="flex shrink-0 items-center justify-center overflow-hidden rounded-sm border border-border bg-muted/40"
|
||||
style={{ width: HEADER_MAX_HEIGHT, height: HEADER_MAX_HEIGHT }}
|
||||
data-testid="print-header-thumbnail"
|
||||
>
|
||||
{proxyType === 'audio' ? (
|
||||
<FileAudio className="h-5 w-5 text-muted-foreground" />
|
||||
) : proxyType === 'pdf' ? (
|
||||
<FileText className="h-5 w-5 text-muted-foreground" />
|
||||
) : (
|
||||
<FileIcon className="h-5 w-5 text-muted-foreground" />
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const { width, height } = fitHeaderThumbnailBox(naturalSize)
|
||||
|
||||
return (
|
||||
<div
|
||||
className="flex shrink-0 items-center justify-center overflow-hidden rounded-sm border border-border transparency-checkerboard"
|
||||
style={{ width, height }}
|
||||
data-testid="print-header-thumbnail"
|
||||
>
|
||||
<img
|
||||
ref={imgRef}
|
||||
src={url}
|
||||
alt=""
|
||||
onLoad={(e) => {
|
||||
const img = e.currentTarget
|
||||
if (img.naturalWidth > 0 && img.naturalHeight > 0) {
|
||||
setNaturalSize({ width: img.naturalWidth, height: img.naturalHeight })
|
||||
}
|
||||
}}
|
||||
className="block h-full w-full"
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export const PrintCommentsView: React.FC<PrintCommentsViewProps> = ({
|
||||
asset,
|
||||
breadcrumbs,
|
||||
@@ -255,6 +321,7 @@ export const PrintCommentsView: React.FC<PrintCommentsViewProps> = ({
|
||||
}, [proxyType])
|
||||
|
||||
const headerThumbUrl = useMemo(() => getHeaderThumbnailUrl(asset), [asset])
|
||||
const assetDimensions = useMemo(() => resolveAssetDisplayDimensions(asset), [asset])
|
||||
const { head: nameHead, tail: nameTail } = splitMiddleTruncate(asset.name || '')
|
||||
const creatorAndDate = [asset.creator?.name, formatShortDate(asset.createdAt)]
|
||||
.filter(Boolean)
|
||||
@@ -274,6 +341,7 @@ export const PrintCommentsView: React.FC<PrintCommentsViewProps> = ({
|
||||
key={url}
|
||||
src={url}
|
||||
annotations={annotations}
|
||||
initialDimensions={assetDimensions}
|
||||
onStatusChange={() =>
|
||||
setSettledFrameUrls((prev) =>
|
||||
prev[commentId] === url ? prev : { ...prev, [commentId]: url },
|
||||
@@ -524,20 +592,11 @@ export const PrintCommentsView: React.FC<PrintCommentsViewProps> = ({
|
||||
<div className="mx-auto max-w-[860px] px-6 py-8 print:max-w-none print:px-0 print:py-0">
|
||||
{/* Document Header */}
|
||||
<header className="flex items-center gap-4 pb-5">
|
||||
<div
|
||||
className="flex shrink-0 items-center justify-center overflow-hidden rounded-sm border border-border transparency-checkerboard"
|
||||
style={{ width: 96, height: 54 }}
|
||||
>
|
||||
{headerThumbUrl ? (
|
||||
<img src={headerThumbUrl} alt="" className="h-full w-full object-contain" />
|
||||
) : proxyType === 'audio' ? (
|
||||
<FileAudio className="h-5 w-5 text-muted-foreground" />
|
||||
) : proxyType === 'pdf' ? (
|
||||
<FileText className="h-5 w-5 text-muted-foreground" />
|
||||
) : (
|
||||
<FileIcon className="h-5 w-5 text-muted-foreground" />
|
||||
)}
|
||||
</div>
|
||||
<HeaderThumbnail
|
||||
url={headerThumbUrl}
|
||||
proxyType={proxyType}
|
||||
initialDimensions={assetDimensions}
|
||||
/>
|
||||
|
||||
<div className="flex min-w-0 flex-1 flex-col justify-between gap-1.5">
|
||||
{/* Top row: Breadcrumbs + Shumai Logo */}
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
import type { AssetInfo } from '@shumai/dtos'
|
||||
|
||||
export const HEADER_MAX_WIDTH = 96
|
||||
export const HEADER_MAX_HEIGHT = 54
|
||||
export const FRAME_FIXED_WIDTH = 200
|
||||
export const DEFAULT_FRAME_HEIGHT = 112
|
||||
export const ERROR_MIN_HEIGHT = 112
|
||||
|
||||
export interface Dimensions {
|
||||
width: number
|
||||
height: number
|
||||
}
|
||||
|
||||
function isValidDimensions(w?: number | null, h?: number | null): boolean {
|
||||
return (
|
||||
typeof w === 'number' &&
|
||||
typeof h === 'number' &&
|
||||
Number.isFinite(w) &&
|
||||
Number.isFinite(h) &&
|
||||
w > 0 &&
|
||||
h > 0
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the asset's display dimensions from transcode/preview/metadata info so
|
||||
* thumbnail containers can size accurately before images finish loading.
|
||||
*/
|
||||
export function resolveAssetDisplayDimensions(asset: AssetInfo): Dimensions | null {
|
||||
const videoTranscode = asset.media?.videoTranscodes?.find((t) =>
|
||||
isValidDimensions(t.width, t.height),
|
||||
)
|
||||
if (videoTranscode) {
|
||||
return { width: videoTranscode.width, height: videoTranscode.height }
|
||||
}
|
||||
|
||||
const imageTranscode = asset.media?.imageTranscodes?.find((t) =>
|
||||
isValidDimensions(t.width, t.height),
|
||||
)
|
||||
if (imageTranscode) {
|
||||
return { width: imageTranscode.width, height: imageTranscode.height }
|
||||
}
|
||||
|
||||
const rawWidth = asset.preview?.originalWidth ?? asset.media?.metadata?.originalWidth
|
||||
const rawHeight = asset.preview?.originalHeight ?? asset.media?.metadata?.originalHeight
|
||||
if (isValidDimensions(rawWidth, rawHeight)) {
|
||||
const rotation = asset.preview?.rotation ?? asset.media?.metadata?.rotation ?? 0
|
||||
const isQuarterTurn = Math.abs(Math.round(rotation)) % 180 === 90
|
||||
return isQuarterTurn
|
||||
? { width: rawHeight!, height: rawWidth! }
|
||||
: { width: rawWidth!, height: rawHeight! }
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Fits the header thumbnail inside a 96x54 bounding box and shrink-wraps to the
|
||||
* media's exact aspect ratio so there is zero empty padding.
|
||||
*/
|
||||
export function fitHeaderThumbnailBox(dimensions: Dimensions | null): Dimensions {
|
||||
if (!dimensions || !isValidDimensions(dimensions.width, dimensions.height)) {
|
||||
return { width: HEADER_MAX_WIDTH, height: HEADER_MAX_HEIGHT }
|
||||
}
|
||||
const scale = Math.min(HEADER_MAX_WIDTH / dimensions.width, HEADER_MAX_HEIGHT / dimensions.height)
|
||||
return {
|
||||
width: Math.max(1, Math.round(dimensions.width * scale)),
|
||||
height: Math.max(1, Math.round(dimensions.height * scale)),
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the comment screenshot container with fixed 200px width and dynamic
|
||||
* height matching the media's exact aspect ratio (zero letterboxing/pillarboxing).
|
||||
*/
|
||||
export function computeCommentFrameBox(dimensions: Dimensions | null): Dimensions {
|
||||
if (!dimensions || !isValidDimensions(dimensions.width, dimensions.height)) {
|
||||
return { width: FRAME_FIXED_WIDTH, height: DEFAULT_FRAME_HEIGHT }
|
||||
}
|
||||
return {
|
||||
width: FRAME_FIXED_WIDTH,
|
||||
height: Math.max(1, Math.round(FRAME_FIXED_WIDTH * (dimensions.height / dimensions.width))),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user