💄 cap preview code block height
This commit is contained in:
parent
0329b6b0f6
commit
1a4f2d97c5
4 changed files with 43 additions and 57 deletions
|
|
@ -21,7 +21,7 @@ const CodeBlockComponent = ({
|
|||
(state: RootStore) => state.state.contextItems
|
||||
);
|
||||
return (
|
||||
<NodeViewWrapper className="code-block-with-content" as="p">
|
||||
<NodeViewWrapper className="code-block-with-content" as="pre">
|
||||
<CodeSnippetPreview
|
||||
borderColor={
|
||||
item.id === contextItems[0]?.id
|
||||
|
|
|
|||
|
|
@ -1,14 +1,8 @@
|
|||
import {
|
||||
ChevronDownIcon,
|
||||
ChevronUpIcon,
|
||||
PaintBrushIcon,
|
||||
XMarkIcon,
|
||||
} from "@heroicons/react/24/outline";
|
||||
import { PaintBrushIcon, XMarkIcon } from "@heroicons/react/24/outline";
|
||||
import { ContextItemWithId } from "core";
|
||||
import { ExtensionIde } from "core/ide";
|
||||
import { getMarkdownLanguageTagForFile } from "core/util";
|
||||
import React from "react";
|
||||
import { useDispatch } from "react-redux";
|
||||
import styled from "styled-components";
|
||||
import { defaultBorderRadius, lightGray, vscEditorBackground } from "..";
|
||||
import { getFontSize } from "../../util";
|
||||
|
|
@ -17,10 +11,7 @@ import FileIcon from "../FileIcon";
|
|||
import HeaderButtonWithText from "../HeaderButtonWithText";
|
||||
import StyledMarkdownPreview from "./StyledMarkdownPreview";
|
||||
|
||||
const MAX_PREVIEW_HEIGHT = 160;
|
||||
|
||||
const PreviewMarkdownDiv = styled.div<{
|
||||
scroll: boolean;
|
||||
borderColor?: string;
|
||||
}>`
|
||||
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<HTMLPreElement>(null);
|
||||
const codeBlockRef = React.useRef<HTMLDivElement>(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 (
|
||||
<PreviewMarkdownDiv
|
||||
scroll={!scrollLocked}
|
||||
onMouseEnter={() => setHovered(true)}
|
||||
onMouseLeave={() => setHovered(false)}
|
||||
borderColor={props.borderColor}
|
||||
|
|
@ -144,17 +128,17 @@ function CodeSnippetPreview(props: CodeSnippetPreviewProps) {
|
|||
</HeaderButtonWithText>
|
||||
</div>
|
||||
</PreviewMarkdownHeader>
|
||||
<pre className="m-0" ref={codeBlockRef}>
|
||||
<div className="m-0" ref={codeBlockRef}>
|
||||
<StyledMarkdownPreview
|
||||
scrollLocked={scrollLocked}
|
||||
source={`${fence}${getMarkdownLanguageTagForFile(
|
||||
props.item.description
|
||||
)}\n${props.item.content}\n${fence}`}
|
||||
maxHeight={MAX_PREVIEW_HEIGHT}
|
||||
showCodeBorder={false}
|
||||
/>
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
{hovered && codeBlockRef.current?.scrollHeight > MAX_PREVIEW_HEIGHT && (
|
||||
{/* {hovered && codeBlockRef.current?.scrollHeight > MAX_PREVIEW_HEIGHT && (
|
||||
<HeaderButtonWithText
|
||||
className="bottom-1 right-1 absolute"
|
||||
text={scrollLocked ? "Scroll" : "Lock Scroll"}
|
||||
|
|
@ -173,7 +157,7 @@ function CodeSnippetPreview(props: CodeSnippetPreviewProps) {
|
|||
/>
|
||||
)}
|
||||
</HeaderButtonWithText>
|
||||
)}
|
||||
)} */}
|
||||
</PreviewMarkdownDiv>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<monaco.editor.IStandaloneCodeEditor | null>(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 (
|
||||
<Container showBorder={props.showBorder} {...props.preProps}>
|
||||
|
|
|
|||
|
|
@ -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 = (
|
||||
<MonacoCodeBlock
|
||||
maxHeight={props.showCodeBorder ? undefined : 160}
|
||||
scrollLocked={props.scrollLocked}
|
||||
showBorder={props.showCodeBorder}
|
||||
language={
|
||||
preProps.children?.[0]?.props?.className?.split("-")[1] ||
|
||||
|
|
@ -160,11 +153,7 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview(
|
|||
}, [props.source]);
|
||||
|
||||
return (
|
||||
<StyledMarkdown
|
||||
maxHeight={props.maxHeight}
|
||||
fontSize={getFontSize()}
|
||||
showBorder={props.showCodeBorder}
|
||||
>
|
||||
<StyledMarkdown fontSize={getFontSize()} showBorder={props.showCodeBorder}>
|
||||
{reactContent}
|
||||
</StyledMarkdown>
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue