diff --git a/src/components/code-editor/hooks/useCodeEditorDocument.ts b/src/components/code-editor/hooks/useCodeEditorDocument.ts index b2b7acd2..74f928d6 100644 --- a/src/components/code-editor/hooks/useCodeEditorDocument.ts +++ b/src/components/code-editor/hooks/useCodeEditorDocument.ts @@ -2,6 +2,7 @@ import { useCallback, useEffect, useState } from 'react'; import { api } from '../../../utils/api'; import type { CodeEditorFile } from '../types/types'; import { isBinaryFile } from '../utils/binaryFile'; +import { getPreviewKind } from '../utils/previewableFile'; type UseCodeEditorDocumentParams = { file: CodeEditorFile; @@ -23,6 +24,9 @@ export const useCodeEditorDocument = ({ file, projectPath }: UseCodeEditorDocume const [saveSuccess, setSaveSuccess] = useState(false); const [saveError, setSaveError] = useState(null); const [isBinary, setIsBinary] = useState(false); + // Some binaries (images, PDFs, audio, video) can be rendered natively, so the + // editor shows an inline preview instead of the generic binary placeholder. + const previewKind = getPreviewKind(file.name); // `fileProjectId` is the DB primary key passed down from the editor sidebar; // the fallback to `projectPath` preserves older callers that didn't yet // propagate the identifier. @@ -38,6 +42,13 @@ export const useCodeEditorDocument = ({ file, projectPath }: UseCodeEditorDocume setLoading(true); setIsBinary(false); + // Natively previewable media (image/pdf/audio/video) is rendered by + // CodeEditorMediaPreview, so there is nothing to read as text here. + if (getPreviewKind(file.name)) { + setLoading(false); + return; + } + // Check if file is binary by extension if (isBinaryFile(file.name)) { setIsBinary(true); @@ -134,6 +145,8 @@ export const useCodeEditorDocument = ({ file, projectPath }: UseCodeEditorDocume saveSuccess, saveError, isBinary, + previewKind, + fileProjectId, handleSave, handleDownload, }; diff --git a/src/components/code-editor/utils/previewableFile.ts b/src/components/code-editor/utils/previewableFile.ts new file mode 100644 index 00000000..464cad81 --- /dev/null +++ b/src/components/code-editor/utils/previewableFile.ts @@ -0,0 +1,31 @@ +// Some binary files can't be edited as text, but the browser can still render +// them natively (images, PDFs, audio, video). For those we show an inline +// preview instead of the generic "binary file" placeholder. Anything not listed +// here (zip, exe, avi, mkv, fonts, ...) falls through to the binary message. + +export type PreviewKind = 'image' | 'pdf' | 'video' | 'audio'; + +// Formats browsers can decode in . +const IMAGE_EXTENSIONS = new Set([ + 'png', 'jpg', 'jpeg', 'gif', 'svg', 'webp', 'ico', 'bmp', 'avif', 'apng', +]); + +const PDF_EXTENSIONS = new Set(['pdf']); + +// Container/codec combos broadly playable in