/** * Document display & preview helpers for edit pages. * * Extracted from pages/component/[id]/edit.vue, pieces/[id]/edit.vue, * product/[id]/edit.vue – each had an identical copy of these utilities. */ import { getFileIcon } from '~/utils/fileIcons' import { canPreviewDocument, isImageDocument, isPdfDocument } from '~/utils/documentPreview' export const PDF_PREVIEW_MAX_BYTES = 5 * 1024 * 1024 export const formatSize = (size: number | null | undefined): string => { if (size === null || size === undefined) return '—' if (size === 0) return '0 B' const units = ['B', 'KB', 'MB', 'GB'] const index = Math.min(units.length - 1, Math.floor(Math.log(size) / Math.log(1024))) const formatted = size / Math.pow(1024, index) return `${formatted.toFixed(1)} ${units[index]}` } export const shouldInlinePdf = (doc: any): boolean => { if (!doc || !isPdfDocument(doc) || !doc.path) return false if (typeof doc.size === 'number' && doc.size > PDF_PREVIEW_MAX_BYTES) return false return true } export const appendPdfViewerParams = (src: string): string => { if (!src || src.startsWith('data:')) return src || '' if (src.includes('#')) return `${src}&toolbar=0&navpanes=0` return `${src}#toolbar=0&navpanes=0` } export const documentPreviewSrc = (doc: any): string => { if (!doc?.path) return '' if (isPdfDocument(doc)) return appendPdfViewerParams(doc.path) return doc.path } export const documentThumbnailClass = (doc: any): string => { if (shouldInlinePdf(doc) || (isImageDocument(doc) && doc?.path)) return 'h-24 w-20' return 'h-16 w-16' } export interface FileIconResult { component: unknown colorClass: string label: string } export const documentIcon = (doc: any): FileIconResult => getFileIcon({ name: doc?.filename || doc?.name, mime: doc?.mimeType }) export const downloadDocument = (doc: any): void => { if (!doc?.path) return const target = String(doc.path) if (target.startsWith('data:')) { const link = document.createElement('a') link.href = target link.download = doc.filename || doc.name || 'document' link.click() return } window.open(target, '_blank') }