Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions example/components/chakra-markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@ function calculateChildrenHeight(parentDiv) {
return childrenHeight;
}

const ChakraMarkdown = React.memo<any>(({ content }) => {
interface ChakraMarkdownProps {
content: string;
}

const ChakraMarkdown = React.memo<ChakraMarkdownProps>(({ content }) => {
const renderers = {
h1: ({ children }) => <Heading as="h1" size="xl" mb={2}>{children}</Heading>,
h2: ({ children }) => <Heading as="h2" size="lg" mb={2}>{children}</Heading>,
h3: ({ children }) => <Heading as="h3" size="md" mb={2}>{children}</Heading>,
a: ({ href, children }) => (
<ChakraLink href={href} color="blue.500" isExternal>
h1: ({ children }: any) => <Heading as="h1" size="xl" mb={2}>{children}</Heading>,
h2: ({ children }: any) => <Heading as="h2" size="lg" mb={2}>{children}</Heading>,
h3: ({ children }: any) => <Heading as="h3" size="md" mb={2}>{children}</Heading>,
a: ({ href, children, ...props }: any) => (
<ChakraLink href={href} color="blue.500" isExternal {...props}>
{children}
</ChakraLink>
),
code: ({ inline, children, className }) => {
code: ({ inline, children, className }: any) => {
const { colorMode } = useColorMode();
const monacoTheme = colorMode === 'dark' ? 'vs-dark' : 'vs-light';

Expand Down
37 changes: 8 additions & 29 deletions example/components/chat-with-markdown.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React, { useState, useRef, useEffect } from 'react';
import { ChakraProvider, Box, Textarea, Button, VStack, HStack, Avatar, Text, useColorModeValue } from '@chakra-ui/react';
import React, { useState } from 'react';
import { ChakraProvider, Box, Button, VStack, HStack, Avatar, Text, useColorModeValue } from '@chakra-ui/react';
import ChakraMarkdown from './chakra-markdown'; // Import the renamed component
import MonacoMarkdownEditor from './monaco-markdown-editor';

function ChatApp() {
const [messages, setMessages] = useState([]);
const [input, setInput] = useState("");
const textareaRef = useRef(null);

const bgColor = useColorModeValue("gray.100", "gray.900");
const chatBgColor = useColorModeValue("white", "gray.800");
Expand All @@ -21,26 +21,9 @@ function ChatApp() {
};
setMessages([...messages, newMessage]);
setInput("");
if (textareaRef.current) {
textareaRef.current.style.height = "auto"; // Reset textarea height after sending
}
}
};

// Function to adjust the height of the textarea
const autoResizeTextarea = () => {
const maxTextareaHeight = window.innerHeight * 0.3; // 30% of screen height
if (textareaRef.current) {
textareaRef.current.style.height = "auto"; // Reset the height
textareaRef.current.style.height = `${Math.min(textareaRef.current.scrollHeight, maxTextareaHeight)}px`; // Adjust the height, limiting to 30%
}
};

useEffect(() => {
// Run the resize function when the input value changes
autoResizeTextarea();
}, [input]);

return (
<ChakraProvider>
<Box bg={bgColor} h="100vh" p={4} display="flex" flexDirection="column">
Expand Down Expand Up @@ -74,17 +57,13 @@ function ChatApp() {

{/* Input Section */}
<HStack mt={4} spacing={2} align="end">
<Textarea
ref={textareaRef}
<MonacoMarkdownEditor
value={input}
onChange={(e) => setInput(e.target.value)}
onInput={autoResizeTextarea} // Call onInput to trigger auto-resize
onChange={setInput}
onEnter={sendMessage}
placeholder="Type a message with Markdown..."
bg={chatBgColor}
resize="none"
overflow="hidden"
flex="1"
maxHeight={`30vh`} // Max height: 30% of screen height
maxHeight="30vh"
minHeight="60px"
/>
<Button colorScheme="blue" onClick={sendMessage}>
Send
Expand Down
140 changes: 140 additions & 0 deletions example/components/monaco-markdown-editor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import React, { useRef, useState, useEffect } from 'react';
import { Box, useColorMode } from '@chakra-ui/react';
import MonacoEditor from '@monaco-editor/react';

interface MonacoMarkdownEditorProps {
value: string;
onChange: (value: string) => void;
onEnter?: () => void;
placeholder?: string;
maxHeight?: string;
minHeight?: string;
}

const MonacoMarkdownEditor: React.FC<MonacoMarkdownEditorProps> = ({
value,
onChange,
onEnter,
placeholder = "Type your message with Markdown...",

Check notice on line 18 in example/components/monaco-markdown-editor.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/monaco-markdown-editor.tsx#L18

" should be '. (quotemark)
maxHeight = "30vh",

Check notice on line 19 in example/components/monaco-markdown-editor.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/monaco-markdown-editor.tsx#L19

" should be '. (quotemark)
minHeight = "100px"

Check notice on line 20 in example/components/monaco-markdown-editor.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/monaco-markdown-editor.tsx#L20

" should be '. (quotemark)

Check notice on line 20 in example/components/monaco-markdown-editor.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/monaco-markdown-editor.tsx#L20

Missing trailing comma. (trailing-comma)
}) => {
const { colorMode } = useColorMode();
const editorRef = useRef(null);
const [editorHeight, setEditorHeight] = useState(minHeight);

Check notice on line 25 in example/components/monaco-markdown-editor.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/monaco-markdown-editor.tsx#L25

Trailing whitespace. (no-trailing-whitespace)
const monacoTheme = colorMode === 'dark' ? 'vs-dark' : 'vs-light';

// Auto-resize editor based on content
const updateEditorHeight = () => {
if (editorRef.current) {
const editor = editorRef.current;
const contentHeight = editor.getContentHeight();
const maxHeightPx = parseInt(maxHeight.replace('vh', '')) * window.innerHeight / 100;

Check notice on line 33 in example/components/monaco-markdown-editor.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/monaco-markdown-editor.tsx#L33

Missing radix parameter.
const minHeightPx = parseInt(minHeight.replace('px', ''));

Check notice on line 34 in example/components/monaco-markdown-editor.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/monaco-markdown-editor.tsx#L34

Missing radix parameter.

Check notice on line 35 in example/components/monaco-markdown-editor.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/monaco-markdown-editor.tsx#L35

Trailing whitespace. (no-trailing-whitespace)
const newHeight = Math.min(Math.max(contentHeight, minHeightPx), maxHeightPx);
setEditorHeight(`${newHeight}px`);
}
};

const handleEditorDidMount = (editor, monaco) => {
editorRef.current = editor;

Check notice on line 43 in example/components/monaco-markdown-editor.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/monaco-markdown-editor.tsx#L43

Trailing whitespace. (no-trailing-whitespace)
// Set up auto-resize
editor.onDidContentSizeChange(updateEditorHeight);

Check notice on line 46 in example/components/monaco-markdown-editor.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/monaco-markdown-editor.tsx#L46

Trailing whitespace. (no-trailing-whitespace)
// Initial height calculation
updateEditorHeight();

Check notice on line 49 in example/components/monaco-markdown-editor.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/monaco-markdown-editor.tsx#L49

Trailing whitespace. (no-trailing-whitespace)
// Configure editor for better markdown editing experience
editor.updateOptions({
automaticLayout: true,
scrollBeyondLastLine: false,
wordWrap: 'on',
minimap: { enabled: false },
lineNumbers: 'off',
folding: false,
selectOnLineNumbers: false,
overviewRulerLanes: 0,
hideCursorInOverviewRuler: true,
renderLineHighlight: 'none',
scrollbar: {
vertical: 'auto',
horizontal: 'auto',
verticalScrollbarSize: 8,
horizontalScrollbarSize: 8

Check notice on line 66 in example/components/monaco-markdown-editor.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/monaco-markdown-editor.tsx#L66

Missing trailing comma. (trailing-comma)
}

Check notice on line 67 in example/components/monaco-markdown-editor.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/monaco-markdown-editor.tsx#L67

Missing trailing comma. (trailing-comma)
});

// Show placeholder when empty
if (!value) {
editor.setValue('');
}

// Add keyboard shortcut for Enter to send (Ctrl+Enter for new line)
if (onEnter) {
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.Enter, () => {
// Ctrl+Enter adds new line (default behavior)
editor.trigger('keyboard', 'type', { text: '\n' });
});

Check notice on line 81 in example/components/monaco-markdown-editor.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/monaco-markdown-editor.tsx#L81

Trailing whitespace. (no-trailing-whitespace)
editor.addCommand(monaco.KeyCode.Enter, () => {
// Enter sends message
onEnter();
});
}
};

const handleEditorChange = (newValue) => {
onChange(newValue || '');
// Trigger height update after content change
setTimeout(updateEditorHeight, 0);
};

useEffect(() => {
updateEditorHeight();
}, [value]);

Check notice on line 97 in example/components/monaco-markdown-editor.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/monaco-markdown-editor.tsx#L97

Arguments are not aligned.

return (
<Box
border="1px solid"
borderColor={colorMode === 'dark' ? 'gray.600' : 'gray.200'}
borderRadius="md"
overflow="hidden"
flex="1"
>
<MonacoEditor
height={editorHeight}
language="markdown"
theme={monacoTheme}
value={value}
onChange={handleEditorChange}
onMount={handleEditorDidMount}
options={{
placeholder: placeholder,

Check notice on line 115 in example/components/monaco-markdown-editor.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/monaco-markdown-editor.tsx#L115

Expected property shorthand in object literal ('{placeholder}'). (object-literal-shorthand)
automaticLayout: true,
scrollBeyondLastLine: false,
wordWrap: 'on',
minimap: { enabled: false },
lineNumbers: 'off',
folding: false,
selectOnLineNumbers: false,
overviewRulerLanes: 0,
hideCursorInOverviewRuler: true,
renderLineHighlight: 'none',
scrollbar: {
vertical: 'auto',
horizontal: 'auto',
verticalScrollbarSize: 8,
horizontalScrollbarSize: 8

Check notice on line 130 in example/components/monaco-markdown-editor.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/monaco-markdown-editor.tsx#L130

Missing trailing comma. (trailing-comma)
},
fontSize: 14,
fontFamily: 'ui-monospace, SFMono-Regular, "Roboto Mono", monospace'

Check notice on line 133 in example/components/monaco-markdown-editor.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/monaco-markdown-editor.tsx#L133

Missing trailing comma. (trailing-comma)
}}
/>
</Box>
);
};

export default MonacoMarkdownEditor;

Check notice on line 140 in example/components/monaco-markdown-editor.tsx

View check run for this annotation

codefactor.io / CodeFactor

example/components/monaco-markdown-editor.tsx#L140

File should end with a newline. (eofline)
Loading