From a35c8a08de214f411e397b89bb18e230f0c31be0 Mon Sep 17 00:00:00 2001 From: Aman Agrawal Date: Fri, 20 Sep 2024 06:21:22 +0000 Subject: [PATCH] message_edit: Attach keydown to $form instead of textarea. This allows any event handlers that are attached to textarea like typeahead to handle the keypress and stop propagation if they successfully handle it. This fixes a bug where message edit form gets saved on enter keypress instead of selecting the active item in the message edit typeahead. Introduced in 0696e234bd4ae937ffb9d3c1ace004b48f5414fd. --- web/src/composebox_typeahead.ts | 4 +++- web/src/message_edit.ts | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/web/src/composebox_typeahead.ts b/web/src/composebox_typeahead.ts index 1f790f580e..426c1e53aa 100644 --- a/web/src/composebox_typeahead.ts +++ b/web/src/composebox_typeahead.ts @@ -1305,7 +1305,9 @@ export function initialize({ }: { on_enter_send: (scheduling_message?: boolean) => boolean | undefined; }): void { - // These handlers are at the "form" level so that they are called after typeahead + // Attach event handlers to `form` instead of `textarea` to allow + // typeahead to call stopPropagation if it can handle the event + // and prevent the form from submitting. $("form#send_message_form").on("keydown", (e) => { handle_keydown(e, on_enter_send); }); diff --git a/web/src/message_edit.ts b/web/src/message_edit.ts index 50a9f21fa3..104d70c9c1 100644 --- a/web/src/message_edit.ts +++ b/web/src/message_edit.ts @@ -508,7 +508,10 @@ function edit_message($row: JQuery, raw_content: string): void { currently_editing_messages.set(message.id, $message_edit_content); message_lists.current.show_edit_message($row, $form); - $message_edit_content.on("keydown", (e) => { + // Attach event handlers to `form` instead of `textarea` to allow + // typeahead to call stopPropagation if it can handle the event + // and prevent the form from submitting. + $form.on("keydown", (e) => { if (keydown_util.is_enter_event(e)) { handle_message_edit_enter(e, $message_edit_content); }