| 1 | import { lazy, Suspense, type CSSProperties } from "react"; |
| 2 | |
| 3 | export type CodeScrollMode = "expand" | "bounded"; |
| 4 | |
| 5 | export interface EditorProps { |
| 6 | value: string; |
| 7 | /** Complete source when the displayed code is a folded preview. */ |
| 8 | copyValue?: string; |
| 9 | language?: string; |
| 10 | readOnly?: boolean; |
| 11 | scrollMode?: CodeScrollMode; |
| 12 | maxHeight?: CSSProperties["maxHeight"]; |
| 13 | /** Original source size in bytes when the caller already has it. */ |
| 14 | sourceSize?: number; |
| 15 | /** Opt in to the workspace-oriented viewer with line numbers and search. */ |
| 16 | showLineNumbers?: boolean; |
| 17 | /** Request that the workspace-oriented viewer opens search after mounting. */ |
| 18 | searchRequestPending?: boolean; |
| 19 | /** Called once the viewer has consumed a pending search request. */ |
| 20 | onSearchRequestConsumed?: () => void; |
| 21 | } |
| 22 | |
| 23 | // ── EDITOR SEAM (code) ─────────────────────────────────────────────────────── |
| 24 | // Keep the established highlighted viewer for existing chat, diff, and tool |
| 25 | // surfaces. Workspace previews explicitly opt into the heavier searchable |
| 26 | // viewer, so this feature cannot silently change every code block in the app. |
| 27 | const HljsImpl = lazy(() => import("./editors/HljsCode")); |
| 28 | const LineNumberImpl = lazy(() => import("./editors/LineNumberCode")); |
| 29 | |
| 30 | export function CodeViewer(props: EditorProps) { |
| 31 | const Impl = props.showLineNumbers ? LineNumberImpl : HljsImpl; |
| 32 | const bounded = props.scrollMode === "bounded" || (props.scrollMode !== "expand" && props.maxHeight != null); |
| 33 | return ( |
| 34 | <div className="code-block"> |
| 35 | <Suspense |
| 36 | fallback={ |
| 37 | <pre className={`code code--loading${bounded ? " code--scroll-y" : ""}`} data-nested-scroll={bounded ? "" : undefined}> |
| 38 | <code>{props.value}</code> |
| 39 | </pre> |
| 40 | } |
| 41 | > |
| 42 | <Impl {...props} /> |
| 43 | </Suspense> |
| 44 | </div> |
| 45 | ); |
| 46 | } |
| 47 |