💄 allow expand/collapse on monaco code block

This commit is contained in:
Nate Sesti 2024-02-07 16:31:49 -08:00
parent fc856da045
commit 2b81745177
4 changed files with 49 additions and 69 deletions

View file

@ -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<HTMLDivElement>(null);
const fence = React.useMemo(() => {
const backticks = props.item.content.match(backticksRegex);
return backticks ? backticks.sort().at(-1) + "`" : "```";
}, [props.item.content]);
return (
<PreviewMarkdownDiv
@ -128,36 +128,47 @@ function CodeSnippetPreview(props: CodeSnippetPreviewProps) {
</HeaderButtonWithText>
</div>
</PreviewMarkdownHeader>
<div className="m-0" ref={codeBlockRef}>
<StyledMarkdownPreview
scrollLocked={scrollLocked}
source={`${fence}${getMarkdownLanguageTagForFile(
props.item.description
)}\n${props.item.content}\n${fence}`}
showCodeBorder={false}
/>
<div
className="m-0"
ref={codeBlockRef}
style={{
height: collapsed
? `${Math.min(
MAX_PREVIEW_HEIGHT,
codeBlockRef.current?.scrollHeight
)}px`
: undefined,
overflow: collapsed ? "hidden" : "auto",
}}
>
<MonacoCodeBlock
codeString={props.item.content}
preProps={{}}
showBorder={false}
language={getMarkdownLanguageTagForFile(props.item.description)}
></MonacoCodeBlock>
</div>
{/* {hovered && codeBlockRef.current?.scrollHeight > MAX_PREVIEW_HEIGHT && (
{codeBlockRef.current?.scrollHeight > MAX_PREVIEW_HEIGHT && (
<HeaderButtonWithText
className="bottom-1 right-1 absolute"
text={scrollLocked ? "Scroll" : "Lock Scroll"}
text={collapsed ? "Expand" : "Collapse"}
>
{scrollLocked ? (
{collapsed ? (
<ChevronDownIcon
width="1.2em"
height="1.2em"
onClick={() => setScrollLocked(false)}
onClick={() => setCollapsed(false)}
/>
) : (
<ChevronUpIcon
width="1.2em"
height="1.2em"
onClick={() => setScrollLocked(true)}
onClick={() => setCollapsed(true)}
/>
)}
</HeaderButtonWithText>
)} */}
)}
</PreviewMarkdownDiv>
);
}

View file

@ -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 (
<Container showBorder={props.showBorder} {...props.preProps}>

View file

@ -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 (
<pre
{...props}
@ -28,11 +12,7 @@ function PreWithToolbar(props: any) {
onMouseEnter={() => setHovering(true)}
onMouseLeave={() => setHovering(false)}
>
{hovering && (
<CodeBlockToolBar
text={childrenToText(props.children)}
></CodeBlockToolBar>
)}
{hovering && <CodeBlockToolBar text={props.copyvalue}></CodeBlockToolBar>}
{props.children}
</pre>
);

View file

@ -124,23 +124,23 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview(
);
},
pre: ({ node, ...preProps }) => {
const codeString =
preProps.children?.[0]?.props?.children?.[0].trim() || "";
const monacoEditor = (
<MonacoCodeBlock
maxHeight={props.showCodeBorder ? undefined : 160}
scrollLocked={props.scrollLocked}
showBorder={props.showCodeBorder}
language={
preProps.children?.[0]?.props?.className?.split("-")[1] ||
"typescript"
}
preProps={preProps}
codeString={
preProps.children?.[0]?.props?.children?.[0].trim() || ""
}
codeString={codeString}
/>
);
return props.showCodeBorder ? (
<PreWithToolbar>{monacoEditor}</PreWithToolbar>
<PreWithToolbar copyvalue={codeString}>
{monacoEditor}
</PreWithToolbar>
) : (
monacoEditor
);