From 2b817451775d0d1cd363f4bb50339c954b3bda37 Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Wed, 7 Feb 2024 16:31:49 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=84=20allow=20expand/collapse=20on=20m?= =?UTF-8?q?onaco=20code=20block?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../markdown/CodeSnippetPreview.tsx | 57 +++++++++++-------- .../components/markdown/MonacoCodeBlock.tsx | 25 +++----- .../components/markdown/PreWithToolbar.tsx | 24 +------- .../markdown/StyledMarkdownPreview.tsx | 12 ++-- 4 files changed, 49 insertions(+), 69 deletions(-) diff --git a/gui/src/components/markdown/CodeSnippetPreview.tsx b/gui/src/components/markdown/CodeSnippetPreview.tsx index 213f3e01e..403bc7b70 100644 --- a/gui/src/components/markdown/CodeSnippetPreview.tsx +++ b/gui/src/components/markdown/CodeSnippetPreview.tsx @@ -1,4 +1,9 @@ -import { PaintBrushIcon, XMarkIcon } from "@heroicons/react/24/outline"; +import { + ChevronDownIcon, + ChevronUpIcon, + PaintBrushIcon, + XMarkIcon, +} from "@heroicons/react/24/outline"; import { ContextItemWithId } from "core"; import { ExtensionIde } from "core/ide"; import { getMarkdownLanguageTagForFile } from "core/util"; @@ -9,7 +14,7 @@ import { getFontSize } from "../../util"; import { postToIde } from "../../util/ide"; import FileIcon from "../FileIcon"; import HeaderButtonWithText from "../HeaderButtonWithText"; -import StyledMarkdownPreview from "./StyledMarkdownPreview"; +import { MonacoCodeBlock } from "./MonacoCodeBlock"; const PreviewMarkdownDiv = styled.div<{ borderColor?: string; @@ -51,18 +56,13 @@ const StyledHeaderButtonWithText = styled(HeaderButtonWithText)<{ ${(props) => props.color && `background-color: ${props.color};`} `; -// Pre-compile the regular expression outside of the function -const backticksRegex = /`{3,}/gm; +const MAX_PREVIEW_HEIGHT = 300; function CodeSnippetPreview(props: CodeSnippetPreviewProps) { - const [scrollLocked, setScrollLocked] = React.useState(true); + const [collapsed, setCollapsed] = React.useState(true); const [hovered, setHovered] = React.useState(false); const codeBlockRef = React.useRef(null); - const fence = React.useMemo(() => { - const backticks = props.item.content.match(backticksRegex); - return backticks ? backticks.sort().at(-1) + "`" : "```"; - }, [props.item.content]); return ( -
- +
+
- {/* {hovered && codeBlockRef.current?.scrollHeight > MAX_PREVIEW_HEIGHT && ( + {codeBlockRef.current?.scrollHeight > MAX_PREVIEW_HEIGHT && ( - {scrollLocked ? ( + {collapsed ? ( setScrollLocked(false)} + onClick={() => setCollapsed(false)} /> ) : ( setScrollLocked(true)} + onClick={() => setCollapsed(true)} /> )} - )} */} + )} ); } diff --git a/gui/src/components/markdown/MonacoCodeBlock.tsx b/gui/src/components/markdown/MonacoCodeBlock.tsx index 92e70ff41..8d1555f3d 100644 --- a/gui/src/components/markdown/MonacoCodeBlock.tsx +++ b/gui/src/components/markdown/MonacoCodeBlock.tsx @@ -122,8 +122,6 @@ interface MonacoCodeBlockProps { preProps: any; language: string; showBorder: boolean; - scrollLocked?: boolean; - maxHeight?: number; } export const MonacoCodeBlock = (props: MonacoCodeBlockProps) => { @@ -159,14 +157,12 @@ export const MonacoCodeBlock = (props: MonacoCodeBlockProps) => { monacoRef.current.editor.setTheme("custom-theme"); }); - 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 }); + editorRef.current.onDidContentSizeChange(() => { + editorRef.current.layout({ + width: editorRef.current.getLayoutInfo().width, + height: editorRef.current.getContentHeight(), }); - } + }); }; useEffect(() => { @@ -247,15 +243,8 @@ 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), + automaticLayout: true, readOnly: true, minimap: { enabled: false }, scrollBeyondLastLine: false, @@ -281,7 +270,7 @@ export const MonacoCodeBlock = (props: MonacoCodeBlockProps) => { }} /> ); - }, [props.scrollLocked, props.maxHeight]); + }, []); return ( diff --git a/gui/src/components/markdown/PreWithToolbar.tsx b/gui/src/components/markdown/PreWithToolbar.tsx index afe411a98..1b2f926bc 100644 --- a/gui/src/components/markdown/PreWithToolbar.tsx +++ b/gui/src/components/markdown/PreWithToolbar.tsx @@ -1,25 +1,9 @@ import { useState } from "react"; import CodeBlockToolBar from "./CodeBlockToolbar"; -function PreWithToolbar(props: any) { +function PreWithToolbar(props: { copyvalue: string; children: any }) { const [hovering, setHovering] = useState(false); - function childToText(child: any) { - if (typeof child === "string") { - return child; - } else if (child?.props) { - return childToText(child.props?.children); - } else if (Array.isArray(child)) { - return childrenToText(child); - } else { - return ""; - } - } - - function childrenToText(child: any) { - return child.props.codeString || ""; - } - return (
 setHovering(true)}
       onMouseLeave={() => setHovering(false)}
     >
-      {hovering && (
-        
-      )}
+      {hovering && }
       {props.children}
     
); diff --git a/gui/src/components/markdown/StyledMarkdownPreview.tsx b/gui/src/components/markdown/StyledMarkdownPreview.tsx index 77bcfd028..b79bf4904 100644 --- a/gui/src/components/markdown/StyledMarkdownPreview.tsx +++ b/gui/src/components/markdown/StyledMarkdownPreview.tsx @@ -124,23 +124,23 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview( ); }, pre: ({ node, ...preProps }) => { + const codeString = + preProps.children?.[0]?.props?.children?.[0].trim() || ""; const monacoEditor = ( ); return props.showCodeBorder ? ( - {monacoEditor} + + {monacoEditor} + ) : ( monacoEditor );