feat(SPA): media field type
This commit is contained in:
1 parent
a2d596f4ca
commit
3e7fc9a4a5
57 files changed
+6109
-75
No files matched your search
@@ -0,0 +1,64 @@
|
||||
import { t } from '@/i18n'
|
||||
import type { MediaItem } from '@/composables/useMedia'
|
||||
import type { ContentEntry } from '@/lib/contentTypes'
|
||||
import { formatDimensions, formatSize } from '@/lib/mediaFormat'
|
||||
|
||||
const MEDIA_ID_MAX = 4294967295
|
||||
|
||||
export interface MediaImage {
|
||||
id: number
|
||||
filename: string
|
||||
width: number | null
|
||||
height: number | null
|
||||
size: number
|
||||
thumbnail_url: string
|
||||
}
|
||||
|
||||
export interface MissingImage {
|
||||
id: number
|
||||
missing: true
|
||||
}
|
||||
|
||||
export type MediaSummary = MediaImage | MissingImage
|
||||
|
||||
export type MediaMap = Record<string, MediaSummary>
|
||||
|
||||
export function isMissingImage(image: MediaSummary): image is MissingImage {
|
||||
return 'missing' in image
|
||||
}
|
||||
|
||||
export function isMediaId(value: unknown): value is number {
|
||||
return (
|
||||
typeof value === 'number' && Number.isInteger(value) && value >= 1 && value <= MEDIA_ID_MAX
|
||||
)
|
||||
}
|
||||
|
||||
export function mediaOf(
|
||||
entry: Pick<ContentEntry, 'media'> | null | undefined,
|
||||
key: string,
|
||||
): MediaSummary | null {
|
||||
return entry?.media?.[key] ?? null
|
||||
}
|
||||
|
||||
export function imageOfItem(item: MediaItem): MediaImage {
|
||||
return {
|
||||
id: item.id,
|
||||
filename: item.filename,
|
||||
width: item.width,
|
||||
height: item.height,
|
||||
size: item.size,
|
||||
thumbnail_url: item.url,
|
||||
}
|
||||
}
|
||||
|
||||
export function imageMeta(image: MediaImage) {
|
||||
return `${formatDimensions(image.width, image.height)} · ${formatSize(image.size)}`
|
||||
}
|
||||
|
||||
export function imageButtonName(image: MediaImage) {
|
||||
const dimensions =
|
||||
image.width === null || image.height === null
|
||||
? formatDimensions(null, null)
|
||||
: t('{0} by {1}', [String(image.width), String(image.height)])
|
||||
return t('Change image, {0}, {1}, {2}', [image.filename, dimensions, formatSize(image.size)])
|
||||
}
|
||||
Reference in new issue
Block a user