Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Make isEnterPress more human-readable
Browse files Browse the repository at this point in the history
  • Loading branch information
florianduros committed Jan 18, 2023
1 parent 65b7a67 commit f6b5532
Showing 1 changed file with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>("MessageComposerInput.ctrlEnterToSend");
return useCallback(
Expand All @@ -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?.();
Expand Down

0 comments on commit f6b5532

Please sign in to comment.