From f6b55320f3ee91a180631f2316334140f0f1cf93 Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Wed, 18 Jan 2023 18:09:16 +0100 Subject: [PATCH] Make isEnterPress more human-readable --- .../hooks/useInputEventProcessor.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/components/views/rooms/wysiwyg_composer/hooks/useInputEventProcessor.ts b/src/components/views/rooms/wysiwyg_composer/hooks/useInputEventProcessor.ts index 8f9c00ce11a..4192c4c060a 100644 --- a/src/components/views/rooms/wysiwyg_composer/hooks/useInputEventProcessor.ts +++ b/src/components/views/rooms/wysiwyg_composer/hooks/useInputEventProcessor.ts @@ -19,6 +19,12 @@ import { useCallback } from "react"; import { useSettingValue } from "../../../../../hooks/useSettings"; +function isEnterPressed(event: KeyboardEvent): boolean { + // Ugly but here we need to send the message only if Enter is pressed + // And we need to stop the event propagation on enter to avoid the composer to grow + return event.key === "Enter" && !event.shiftKey && !event.ctrlKey && !event.metaKey && !event.altKey; +} + export function useInputEventProcessor(onSend: () => void): (event: WysiwygEvent) => WysiwygEvent | null { const isCtrlEnter = useSettingValue("MessageComposerInput.ctrlEnterToSend"); return useCallback( @@ -28,15 +34,9 @@ export function useInputEventProcessor(onSend: () => void): (event: WysiwygEvent } const isKeyboardEvent = event instanceof KeyboardEvent; - const isEnterPress = - !isCtrlEnter && - (isKeyboardEvent - ? // Ugly but here we need to send the message only if Enter is pressed - // And we need to stop the event propagation on enter to avoid the composer to grow - event.key === "Enter" && !event.shiftKey && !event.ctrlKey && !event.metaKey && !event.altKey - : event.inputType === "insertParagraph"); - // sendMessage is sent when ctrl+enter is pressed - const isSendMessage = !isKeyboardEvent && isCtrlEnter && event.inputType === "sendMessage"; + const isEnterPress = !isCtrlEnter && isKeyboardEvent && isEnterPressed(event); + // sendMessage is sent when cmd+enter is pressed + const isSendMessage = isCtrlEnter && !isKeyboardEvent && event.inputType === "sendMessage"; if (isEnterPress || isSendMessage) { event.stopPropagation?.();