diff --git a/gui/src/components/mainInput/CodeBlockExtension.tsx b/gui/src/components/mainInput/CodeBlockExtension.tsx index 15e21cce5..96e16c0d3 100644 --- a/gui/src/components/mainInput/CodeBlockExtension.tsx +++ b/gui/src/components/mainInput/CodeBlockExtension.tsx @@ -21,7 +21,7 @@ const CodeBlockComponent = ({ (state: RootStore) => state.state.contextItems ); return ( - + ` background-color: ${vscEditorBackground}; @@ -34,13 +25,9 @@ const PreviewMarkdownDiv = styled.div<{ & div { background-color: ${vscEditorBackground}; } - - & code { - overflow-y: ${(props) => (props.scroll ? "scroll" : "hidden")} !important; - } `; -const PreviewMarkdownHeader = styled.p` +const PreviewMarkdownHeader = styled.div` margin: 0; padding: 2px 6px; border-bottom: 0.5px solid ${lightGray}; @@ -68,12 +55,10 @@ const StyledHeaderButtonWithText = styled(HeaderButtonWithText)<{ const backticksRegex = /`{3,}/gm; function CodeSnippetPreview(props: CodeSnippetPreviewProps) { - const dispatch = useDispatch(); - const [scrollLocked, setScrollLocked] = React.useState(true); const [hovered, setHovered] = React.useState(false); - const codeBlockRef = React.useRef(null); + const codeBlockRef = React.useRef(null); const fence = React.useMemo(() => { const backticks = props.item.content.match(backticksRegex); return backticks ? backticks.sort().at(-1) + "`" : "```"; @@ -81,7 +66,6 @@ function CodeSnippetPreview(props: CodeSnippetPreviewProps) { return ( setHovered(true)} onMouseLeave={() => setHovered(false)} borderColor={props.borderColor} @@ -144,17 +128,17 @@ function CodeSnippetPreview(props: CodeSnippetPreviewProps) { -
+      
-
+ - {hovered && codeBlockRef.current?.scrollHeight > MAX_PREVIEW_HEIGHT && ( + {/* {hovered && codeBlockRef.current?.scrollHeight > MAX_PREVIEW_HEIGHT && ( )} - )} + )} */}
); } diff --git a/gui/src/components/markdown/MonacoCodeBlock.tsx b/gui/src/components/markdown/MonacoCodeBlock.tsx index 0225e9cff..e5fac44d0 100644 --- a/gui/src/components/markdown/MonacoCodeBlock.tsx +++ b/gui/src/components/markdown/MonacoCodeBlock.tsx @@ -118,6 +118,8 @@ interface MonacoCodeBlockProps { preProps: any; language: string; showBorder: boolean; + scrollLocked?: boolean; + maxHeight?: number; } export const MonacoCodeBlock = (props: MonacoCodeBlockProps) => { @@ -125,16 +127,18 @@ export const MonacoCodeBlock = (props: MonacoCodeBlockProps) => { const editorRef = useRef(null); const liftOff = async (monaco) => { - const grammars = new Map(); - - for (const [scopeName, [languageId, _]] of Object.entries( - supportedLanguages - )) { - grammars.set(languageId, scopeName); - monaco.languages.register({ id: languageId }); + try { + const grammars = new Map(); + for (const [scopeName, [languageId, _]] of Object.entries( + supportedLanguages + )) { + grammars.set(languageId, scopeName); + monaco.languages.register({ id: languageId }); + } + await wireTmGrammars(monaco, registry, grammars, editorRef.current); + } catch (e) { + console.error("Error wiring grammars", e); } - - await wireTmGrammars(monaco, registry, grammars, editorRef.current); }; const onEditorDidMount = (editor, monaco) => { @@ -146,12 +150,14 @@ export const MonacoCodeBlock = (props: MonacoCodeBlockProps) => { monacoRef.current.editor.setTheme("custom-theme"); }); - editorRef.current.onDidContentSizeChange(() => { - const contentHeight = editorRef.current.getContentHeight(); - const layoutInfo = editorRef.current.getLayoutInfo(); - const width = layoutInfo.width; - editorRef.current.layout({ height: contentHeight, width }); - }); + if (!(props.maxHeight && !props.scrollLocked)) { + editorRef.current.onDidContentSizeChange(() => { + const contentHeight = editorRef.current.getContentHeight(); + const layoutInfo = editorRef.current.getLayoutInfo(); + const width = layoutInfo.width; + editorRef.current.layout({ height: contentHeight, width }); + }); + } }; useEffect(() => { @@ -233,7 +239,15 @@ export const MonacoCodeBlock = (props: MonacoCodeBlockProps) => { : "typescript" } theme="vs-dark" + height={ + props.maxHeight && + props.scrollLocked && + props.codeString.split("\n").length * 18 > props.maxHeight + ? props.maxHeight + : null + } options={{ + automaticLayout: !(props.maxHeight && !props.scrollLocked), readOnly: true, minimap: { enabled: false }, scrollBeyondLastLine: false, @@ -251,7 +265,6 @@ export const MonacoCodeBlock = (props: MonacoCodeBlockProps) => { indentation: false, }, renderLineHighlight: "none", - automaticLayout: true, fontLigatures: true, fontVariations: true, padding: { top: 4, bottom: 4 }, @@ -260,7 +273,7 @@ export const MonacoCodeBlock = (props: MonacoCodeBlockProps) => { }} /> ); - }, []); + }, [props.scrollLocked, props.maxHeight]); return ( diff --git a/gui/src/components/markdown/StyledMarkdownPreview.tsx b/gui/src/components/markdown/StyledMarkdownPreview.tsx index ba6be618c..c3976cd7b 100644 --- a/gui/src/components/markdown/StyledMarkdownPreview.tsx +++ b/gui/src/components/markdown/StyledMarkdownPreview.tsx @@ -14,7 +14,6 @@ import "./markdown.css"; const StyledMarkdown = styled.div<{ fontSize?: number; - maxHeight?: number; showBorder?: boolean; }>` pre { @@ -34,14 +33,6 @@ const StyledMarkdown = styled.div<{ } }} padding: ${(props) => (props.showBorder ? "12px" : "0px 2px")}; - ${(props) => { - if (props.maxHeight) { - return ` - max-height: ${props.maxHeight}px; - overflow-y: auto; - `; - } - }} } code { @@ -89,9 +80,9 @@ const StyledMarkdown = styled.div<{ interface StyledMarkdownPreviewProps { source?: string; - maxHeight?: number; className?: string; showCodeBorder?: boolean; + scrollLocked?: boolean; } const FadeInWords: React.FC = (props: any) => { @@ -131,6 +122,8 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( pre: ({ node, ...preProps }) => { const monacoEditor = ( + {reactContent} );