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.
This commit is contained in:
Steve Howell 2020-04-02 17:39:10 +00:00 committed by Tim Abbott
parent 0f5dcd5d84
commit 8315eee046
2 changed files with 18 additions and 27 deletions

View File

@ -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;

View File

@ -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)) {