parent
eb43f3449e
commit
b47588fc5c
2 changed files with 51 additions and 5 deletions
|
@ -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(
|
||||
|
@ -28,12 +34,12 @@ export function useInputEventProcessor(onSend: () => void): (event: WysiwygEvent
|
|||
}
|
||||
|
||||
const isKeyboardEvent = event instanceof KeyboardEvent;
|
||||
const isEnterPress =
|
||||
!isCtrlEnter && (isKeyboardEvent ? event.key === "Enter" : event.inputType === "insertParagraph");
|
||||
// sendMessage is sent when ctrl+enter is pressed
|
||||
const isSendMessage = !isKeyboardEvent && event.inputType === "sendMessage";
|
||||
const isEnterPress = !isCtrlEnter && isKeyboardEvent && isEnterPressed(event);
|
||||
const isInsertParagraph = !isCtrlEnter && !isKeyboardEvent && event.inputType === "insertParagraph";
|
||||
// sendMessage is sent when cmd+enter is pressed
|
||||
const isSendMessage = isCtrlEnter && !isKeyboardEvent && event.inputType === "sendMessage";
|
||||
|
||||
if (isEnterPress || isSendMessage) {
|
||||
if (isEnterPress || isInsertParagraph || isSendMessage) {
|
||||
event.stopPropagation?.();
|
||||
event.preventDefault?.();
|
||||
onSend();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue