/** * PreviewPane shows a file from the workspace: rendered Markdown or AsciiDoc * (with a Rendered/Source toggle), images at natural size, draw.io diagrams * through the diagrams.net viewer (with the XML as Source), and * syntax-highlighted read-only code for everything else. * * @example * */ import { useEffect, useState } from "react"; import { readFile } from "../api"; import { type DocumentKind, kindForPath, languageForPath } from "../lang"; import { openEditor } from "../workspace"; import { AsciiDocView } from "./AsciiDocView"; import { CodeView } from "./CodeView"; import { DrawioView } from "./DrawioView"; import { ImageView } from "./ImageView"; import { Markdown } from "./Markdown"; type PreviewMode = "rendered" | "source"; /** Kinds with both a rendered form and a source form. */ const toggleableKinds: ReadonlySet = new Set([ "markdown", "asciidoc", "drawio", ]); /** * usePreviewFile loads the file behind path (images excepted: the browser * fetches their bytes itself) and resets the view mode whenever the path * changes. `content` stays null while loading. */ function usePreviewFile(path: string | null) { const [content, setContent] = useState(null); const [error, setError] = useState(null); const [mode, setMode] = useState("rendered"); useEffect(() => { if (!path) { return; } setContent(null); setError(null); setMode("rendered"); if (kindForPath(path) === "image") { return; } readFile(path) .then((file) => setContent(file.content)) .catch((cause: Error) => setError(cause.message)); }, [path]); return { content, error, mode, setMode }; } export function PreviewPane({ path }: { path: string | null }) { const { content, error, mode, setMode } = usePreviewFile(path); const kind = path ? kindForPath(path) : "code"; if (!path) { return (

Select a file in the tree to preview it.

); } if (error) { return

{error}

; } if (kind !== "image" && content === null) { return

loading…

; } const toggleable = toggleableKinds.has(kind); return (
{path.split("/").pop()} {toggleable && (
)} {kind !== "image" && ( )}
); } function ModeButton({ current, mode, onSelect, }: { current: PreviewMode; mode: PreviewMode; onSelect: (mode: PreviewMode) => void; }) { return ( ); } function PreviewContent({ path, kind, content, mode, }: { path: string; kind: DocumentKind; content: string; mode: PreviewMode; }) { if (kind === "image") { return ; } if (mode === "source" || kind === "code") { return ; } if (kind === "drawio") { return ; } return (
{kind === "markdown" ? ( ) : ( )}
); }