From cae6abc0bf557619d933e599e7fbf1b078a536c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?nicole=20miko=C5=82ajczyk?= Date: Sun, 24 Aug 2025 20:53:52 +0200 Subject: [PATCH] pl-fe: nobody wants enter in compose form to create a new paragraph MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: nicole mikołajczyk --- .../compose/editor/plugins/state-plugin.tsx | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/pl-fe/src/features/compose/editor/plugins/state-plugin.tsx b/packages/pl-fe/src/features/compose/editor/plugins/state-plugin.tsx index b74a57f84..35674e7a3 100644 --- a/packages/pl-fe/src/features/compose/editor/plugins/state-plugin.tsx +++ b/packages/pl-fe/src/features/compose/editor/plugins/state-plugin.tsx @@ -2,7 +2,8 @@ import { HashtagNode } from '@lexical/hashtag'; import { AutoLinkNode, LinkNode } from '@lexical/link'; import { $convertToMarkdownString } from '@lexical/markdown'; import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'; -import { $nodesOfType, $getRoot, type EditorState, $getNodeByKey } from 'lexical'; +import { CAN_USE_BEFORE_INPUT, IS_APPLE_WEBKIT, IS_IOS, IS_SAFARI } from '@lexical/utils'; +import { $nodesOfType, $getRoot, type EditorState, $getNodeByKey, KEY_ENTER_COMMAND, $getSelection, $isRangeSelection, INSERT_PARAGRAPH_COMMAND, INSERT_LINE_BREAK_COMMAND, COMMAND_PRIORITY_CRITICAL } from 'lexical'; import debounce from 'lodash/debounce'; import { useCallback, useEffect } from 'react'; import { useIntl } from 'react-intl'; @@ -163,6 +164,32 @@ const StatePlugin: React.FC = ({ composeId, isWysiwyg }) => { }, 750), []); useEffect(() => { + // Adapted from https://github.com/facebook/lexical/blob/main/packages/lexical-rich-text/src/index.ts#L929 + editor.registerCommand( + KEY_ENTER_COMMAND, + (event) => { + const selection = $getSelection(); + if (!$isRangeSelection(selection)) { + return false; + } + + if (event !== null) { + if ( + (IS_IOS || IS_SAFARI || IS_APPLE_WEBKIT) && + CAN_USE_BEFORE_INPUT + ) { + return false; + } + event.preventDefault(); + if (event.ctrlKey && event.shiftKey) { + return editor.dispatchCommand(INSERT_PARAGRAPH_COMMAND, undefined); + } + } + return editor.dispatchCommand(INSERT_LINE_BREAK_COMMAND, false); + }, + COMMAND_PRIORITY_CRITICAL, + ); + return editor.registerUpdateListener(({ editorState }) => { const plainText = editorState.read(() => $getRoot().getTextContent()); editor.update(() => {