import * as React from 'react'; import type { SxProps } from '@mui/joy/styles/types'; import { Box, ColorPaletteProp, IconButton, Typography } from '@mui/joy'; import CodeIcon from '@mui/icons-material/Code'; import MoreVertIcon from '@mui/icons-material/MoreVert'; import type { ContentScaling } from '~/common/app.theme'; import { ExpanderControlledBox } from '~/common/components/ExpanderControlledBox'; import { TooltipOutlined } from '~/common/components/TooltipOutlined'; import { EnhancedRenderCodeMenu } from './EnhancedRenderCodeMenu'; import { RenderCodeMemo } from '../code/RenderCode'; import { enhancedCodePanelTitleTooltipSx, RenderCodePanelFrame } from '../code/RenderCodePanelFrame'; import { getCodeCollapseManager } from './codeCollapseManager'; import { useLiveFilePatch } from './livefile-patch/useLiveFilePatch'; export function EnhancedRenderCode(props: { semiStableId: string | undefined, title: string, code: string, isPartial: boolean, fitScreen: boolean, isMobile: boolean, initialShowHTML?: boolean, noCopyButton?: boolean, optimizeLightweight?: boolean, codeSx?: SxProps, language?: string, color?: ColorPaletteProp; contentScaling: ContentScaling; // onLiveFileCreate?: () => void, }) { // state const [contextMenuAnchor, setContextMenuAnchor] = React.useState(null); const [isCodeCollapsed, setIsCodeCollapsed] = React.useState(false); // LiveFile - patch state const { button: liveFileButton, actionBar: liveFileActionBar } = useLiveFilePatch( props.title, props.code, props.isPartial, props.isMobile, ); // handlers const handleCloseContextMenu = React.useCallback(() => setContextMenuAnchor(null), []); const handleToggleCodeCollapse = React.useCallback(() => { setIsCodeCollapsed(c => !c); handleCloseContextMenu(); }, [handleCloseContextMenu]); const handleToggleContextMenu = React.useCallback((event: React.MouseEvent) => { event.preventDefault(); // added for the Right mouse click (to prevent the menu) event.stopPropagation(); setContextMenuAnchor(anchor => anchor ? null : event.currentTarget); }, []); // effects React.useEffect(() => { return getCodeCollapseManager().addCollapseAllListener((collapseAll: boolean) => { setIsCodeCollapsed(collapseAll); handleCloseContextMenu(); }); }, [handleCloseContextMenu]); // components const headerTooltipContents = React.useMemo(() => ( {/* This is what we have */}
Code Block
Title
{props.title || '(empty)'}
{/*
Language
*/} {/*
{props.language}
*/}
Code Lines
{props.code.split('\n').length} lines
Code Length
{props.code.length} characters
semiStableId
{props.semiStableId || '(none)'}
{/* This is what attachments carry */} {/*
Attachment Title
*/} {/*
{fragment.title}
*/} {/*
Doc Title
*/} {/*
{fragmentDocPart.l1Title}
*/} {/*
Identifier
*/} {/*
{fragmentDocPart.ref}
*/} {/*
Render type
*/} {/*
{fragmentDocPart.vdt}
*/} {/*
Text Mime type
*/} {/*
{fragmentDocPart.data?.mimeType || '(unknown)'}
*/} {/*
Text Buffer Id
*/} {/*
{fragmentId}
*/}
), [props.code, props.semiStableId, props.title]); const headerRow = React.useMemo(() => <> {/* Icon and Title */} {props.title || 'Code'} {/* LiveFile - Select */} {liveFileButton} {/* Menu Options button */} , [handleToggleCodeCollapse, handleToggleContextMenu, headerTooltipContents, isCodeCollapsed, liveFileButton, props.title]); // const toolbarRow = React.useMemo(() => <> // {props.onLiveFileCreate && ( // // )} // {/* Add more toolbar items here */} // , [props.onLiveFileCreate]); // styles const patchCodeSx = React.useMemo(() => ({ ...props.codeSx, my: 0, borderTop: '1px solid', borderTopColor: `neutral.outlinedBorder`, borderTopLeftRadius: 0, borderTopRightRadius: 0, }), [props.codeSx]); return ( {/* Body of the message (it's a RenderCode with patched sx, for looks) */} {/* Context Menu */} {contextMenuAnchor && ( )} ); }