From 8315eee04642c7147004e2841bf032dde6ffab2f Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Thu, 2 Apr 2020 17:39:10 +0000 Subject: [PATCH] message edit: Handle escape key more nicely. We now handle the esc key completely within the keydown handler that we already have for message editing. We allow escape to work no matter what the focused element is within an edited message, and we blur that element properly and end the edit. We remove all the strange, duplicated logic from hotkey.js. This should also fix a blueslip error where the hotkey code was passing message_edit a jQuery element with zero length. Fixes the traceback reported in #14151, though we should still look at the DOM cleanup discussed there. --- static/js/hotkey.js | 27 --------------------------- static/js/message_edit.js | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 27 deletions(-) diff --git a/static/js/hotkey.js b/static/js/hotkey.js index bc72cc1497..a3f135f108 100644 --- a/static/js/hotkey.js +++ b/static/js/hotkey.js @@ -178,8 +178,6 @@ exports.in_content_editable_widget = function (e) { // Returns true if we handled it, false if the browser should. exports.process_escape_key = function (e) { - let row; - if (exports.in_content_editable_widget(e)) { return false; } @@ -204,32 +202,7 @@ exports.process_escape_key = function (e) { return true; } - if (message_edit.is_editing(current_msg_list.selected_id())) { - // Using this definition of "row" instead of "current_msg_list.selected_row()" - // because it returns a more complete object. - // Necessary for refocusing on message list in Firefox. - const message_edit_inputs = $(".message_edit_content, .message_edit_topic"); - row = message_edit_inputs.filter(":focus").closest(".message_row"); - row.find('.message_edit_content').blur(); - message_edit.end(row); - return true; - } - if (exports.processing_text()) { - if ($(".message_edit_content").filter(":focus").length > 0) { - row = $(".message_edit_content").filter(":focus").closest(".message_row"); - row.find('.message_edit_content').blur(); - message_edit.end(row); - return true; - } - - if ($(".message_edit_topic").filter(":focus").length > 0) { - row = $(".message_edit_topic").filter(":focus").closest(".message_row"); - row.find('.message_edit_topic').blur(); - message_edit.end(row); - return true; - } - if (activity.searching()) { activity.escape_search(); return true; diff --git a/static/js/message_edit.js b/static/js/message_edit.js index 42b7896dd0..7a6d5d34b4 100644 --- a/static/js/message_edit.js +++ b/static/js/message_edit.js @@ -143,10 +143,28 @@ exports.show_topic_edit_spinner = function (row) { $(".topic_edit_cancel").hide(); }; +exports.end_if_focused = function () { + const focused_elem = $(".message_edit").find(':focus'); + + if (focused_elem.length === 1) { + focused_elem.blur(); + const row = focused_elem.closest('.message_row'); + exports.end(row); + } +}; + function handle_edit_keydown(from_topic_edited_only, e) { let row; const code = e.keyCode || e.which; + // Handle escape keys in the message_edit form. + if (code === 27) { + exports.end_if_focused(); + e.stopPropagation(); + e.preventDefault(); + return; + } + if ($(e.target).hasClass("message_edit_content") && code === 13) { // Pressing enter to save edits is coupled with enter to send if (composebox_typeahead.should_enter_send(e)) {