mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-22 11:47:19 +00:00
* feat(webui): redesign comment attachment display into compact rows * feat(webui): support pure frontend image extension matching and self-contained preview dialog * fix(webui): size image preview dialog to min(image size, 80% screen size) * fix(webui): prevent horizontal overflow of comment attachment rows in scroll areas * fix(webui): centralize attachment image preview ownership and localize download title
43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import type { ImageTranscode, VideoTranscode } from '@shumai/dtos'
|
|
|
|
type Transcode = ImageTranscode | VideoTranscode
|
|
|
|
const WEB_IMAGE_EXTENSION_REGEX = /\.(png|jpe?g|webp|gif|svg|avif|bmp|ico)$/i
|
|
|
|
export function isImageFileName(filename?: string | null): boolean {
|
|
if (!filename) return false
|
|
return WEB_IMAGE_EXTENSION_REGEX.test(filename)
|
|
}
|
|
|
|
export function getBestTranscode(
|
|
transcodes: Transcode[] | undefined,
|
|
screenWidth: number,
|
|
): Transcode | null {
|
|
if (!transcodes || transcodes.length === 0) {
|
|
return null
|
|
}
|
|
|
|
// Sort by width descending to easily find largest available
|
|
const sorted = [...transcodes].sort((a, b) => {
|
|
const wA = a.width ?? 0
|
|
const wB = b.width ?? 0
|
|
return wB - wA // Descending width
|
|
})
|
|
|
|
// Find smallest width that is >= screenWidth
|
|
const suitable = sorted.filter((t) => (t.width ?? 0) >= screenWidth)
|
|
|
|
if (suitable.length > 0) {
|
|
// Sort suitable by width ASCENDING to find "smallest fit".
|
|
suitable.sort((a, b) => {
|
|
const wA = a.width ?? 0
|
|
const wB = b.width ?? 0
|
|
return wA - wB // Ascending
|
|
})
|
|
return suitable[0]
|
|
}
|
|
|
|
// If no candidate >= screenWidth, simply return the largest available (first in original sorted)
|
|
return sorted[0]
|
|
}
|