From 8d4734cb8a3f22f9530d195b6dbd4abc21a7abca Mon Sep 17 00:00:00 2001 From: Faisal Amir Date: Thu, 7 Nov 2024 16:08:56 +0700 Subject: [PATCH] fix: handle edge cases syntax highlight (#3969) --- .../LocalServerRightPanel/index.tsx | 2 +- .../ChatInput/RichTextEditor.tsx | 22 +++++++------------ web/styles/components/code-block.scss | 14 ++++++------ web/styles/main.scss | 16 ++++++++++++++ 4 files changed, 32 insertions(+), 22 deletions(-) diff --git a/web/screens/LocalServer/LocalServerRightPanel/index.tsx b/web/screens/LocalServer/LocalServerRightPanel/index.tsx index 8cb2af350..a59e83e7e 100644 --- a/web/screens/LocalServer/LocalServerRightPanel/index.tsx +++ b/web/screens/LocalServer/LocalServerRightPanel/index.tsx @@ -95,7 +95,7 @@ const LocalServerRightPanel = () => {
{ clipboard.copy(selectedModel?.id) diff --git a/web/screens/Thread/ThreadCenterPanel/ChatInput/RichTextEditor.tsx b/web/screens/Thread/ThreadCenterPanel/ChatInput/RichTextEditor.tsx index 87ea4e08f..096ef51e0 100644 --- a/web/screens/Thread/ThreadCenterPanel/ChatInput/RichTextEditor.tsx +++ b/web/screens/Thread/ThreadCenterPanel/ChatInput/RichTextEditor.tsx @@ -132,20 +132,6 @@ const RichTextEditor = ({ if (Editor.isBlock(editor, node) && node.type === 'paragraph') { node.children.forEach((child: { text: any }, childIndex: number) => { const text = child.text - const { selection } = editor - - if (selection) { - const selectedNode = Editor.node(editor, selection) - - if (Editor.isBlock(editor, selectedNode[0] as CustomElement)) { - const isNodeEmpty = Editor.string(editor, selectedNode[1]) === '' - - if (isNodeEmpty) { - // Reset language when a node is cleared - currentLanguage.current = 'plaintext' - } - } - } // Match code block start and end const startMatch = text.match(/^```(\w*)$/) @@ -346,6 +332,14 @@ const RichTextEditor = ({ .join('\n') setCurrentPrompt(combinedText) + if (combinedText.trim() === '') { + currentLanguage.current = 'plaintext' + } + const hasCodeBlockStart = combinedText.match(/^```(\w*)/m) + // Set language to plaintext if no code block with language identifier is found + if (!hasCodeBlockStart) { + currentLanguage.current = 'plaintext' + } }} >