28 lines
953 B
TypeScript
28 lines
953 B
TypeScript
export function parseAspectRatio(value?: string | null): number {
|
|
if (!value) return 16 / 9
|
|
const [widthRaw, heightRaw] = value.split(':')
|
|
const width = Number(widthRaw)
|
|
const height = Number(heightRaw)
|
|
if (!Number.isFinite(width) || !Number.isFinite(height) || width <= 0 || height <= 0) {
|
|
return 16 / 9
|
|
}
|
|
return width / height
|
|
}
|
|
|
|
export function cssAspectRatio(value?: string | null): string {
|
|
if (!value?.includes(':')) return '16 / 9'
|
|
return value.replace(':', ' / ')
|
|
}
|
|
|
|
/** Preview box that keeps the slot ratio: landscape is wide, portrait is tall. */
|
|
export function slotPreviewSize(value?: string | null): { width: number; height: number } {
|
|
const aspect = parseAspectRatio(value)
|
|
const long = 220
|
|
if (aspect >= 1) {
|
|
const width = Math.min(360, Math.round(long * aspect))
|
|
return { width, height: Math.round(width / aspect) }
|
|
}
|
|
const height = long
|
|
return { width: Math.round(height * aspect), height }
|
|
}
|