From f2febaf65267684fb09492be66491b34f9bbee57 Mon Sep 17 00:00:00 2001 From: Yiling-J Date: Wed, 25 Nov 2026 21:07:45 +0800 Subject: [PATCH] fix(comments): size printable comment thumbnails to match media aspect ratio (#551) --- .../comment-frame-thumbnail.tsx | 49 +++---- .../print-comments-view.test.tsx | 130 ++++++++++++++++++ .../print-comments/print-comments-view.tsx | 89 ++++++++++-- .../webui/components/print-comments/sizing.ts | 84 +++++++++++ 4 files changed, 306 insertions(+), 46 deletions(-) create mode 100644 packages/webui/components/print-comments/sizing.ts diff --git a/packages/webui/components/print-comments/comment-frame-thumbnail.tsx b/packages/webui/components/print-comments/comment-frame-thumbnail.tsx index 179c42df..b1f410ea 100644 --- a/packages/webui/components/print-comments/comment-frame-thumbnail.tsx +++ b/packages/webui/components/print-comments/comment-frame-thumbnail.tsx @@ -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 = ({ 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(initialDimensions) const imgRef = useRef(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) => { 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 = ({ 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 (
{status === 'loading' && ( @@ -84,32 +79,24 @@ export const CommentFrameThumbnail: React.FC = ({ {m.print_comments_frame_unavailable()}
) : ( -
+ <> {alt} {status === 'loaded' && annotations.length > 0 && ( )} -
+ )} ) diff --git a/packages/webui/components/print-comments/print-comments-view.test.tsx b/packages/webui/components/print-comments/print-comments-view.test.tsx index 553de2ca..981902f2 100644 --- a/packages/webui/components/print-comments/print-comments-view.test.tsx +++ b/packages/webui/components/print-comments/print-comments-view.test.tsx @@ -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 { 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( + , + ) + + // 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', + ) + }) }) diff --git a/packages/webui/components/print-comments/print-comments-view.tsx b/packages/webui/components/print-comments/print-comments-view.tsx index c9985be1..e98de465 100644 --- a/packages/webui/components/print-comments/print-comments-view.tsx +++ b/packages/webui/components/print-comments/print-comments-view.tsx @@ -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 = ({ url, proxyType, initialDimensions }) => { + const [naturalSize, setNaturalSize] = useState(initialDimensions) + const imgRef = useRef(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 ( +
+ {proxyType === 'audio' ? ( + + ) : proxyType === 'pdf' ? ( + + ) : ( + + )} +
+ ) + } + + const { width, height } = fitHeaderThumbnailBox(naturalSize) + + return ( +
+ { + const img = e.currentTarget + if (img.naturalWidth > 0 && img.naturalHeight > 0) { + setNaturalSize({ width: img.naturalWidth, height: img.naturalHeight }) + } + }} + className="block h-full w-full" + /> +
+ ) +} + export const PrintCommentsView: React.FC = ({ asset, breadcrumbs, @@ -255,6 +321,7 @@ export const PrintCommentsView: React.FC = ({ }, [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 = ({ 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 = ({
{/* Document Header */}
-
- {headerThumbUrl ? ( - - ) : proxyType === 'audio' ? ( - - ) : proxyType === 'pdf' ? ( - - ) : ( - - )} -
+
{/* Top row: Breadcrumbs + Shumai Logo */} diff --git a/packages/webui/components/print-comments/sizing.ts b/packages/webui/components/print-comments/sizing.ts new file mode 100644 index 00000000..d1cfc76d --- /dev/null +++ b/packages/webui/components/print-comments/sizing.ts @@ -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))), + } +}