compose: Fix recent autocomplete regression.

As part of giving the stream/topic fields in the
compose box longer ids, I broke the autocomplete
code that handles re-focusing the cursor after
a user hits enter.  The worst symptom of this was
that we tried to send a message before compose
finished (although it wouldn't fully deliver the
message).

The new code should be a bit easier to grep for
if we rename these fields again, as we explicitly
use selector syntax.
This commit is contained in:
Steve Howell 2018-12-22 15:35:32 +00:00 committed by showell
parent ca22302763
commit 72576b3a77
3 changed files with 27 additions and 26 deletions

View File

@ -828,7 +828,7 @@ run_test('initialize', () => {
var event = {
keyCode: 13,
target: {
id: 'stream',
id: 'stream_message_recipient_stream',
},
preventDefault: noop,
};
@ -872,7 +872,7 @@ run_test('initialize', () => {
$('#compose-textarea').caret = noop;
event.keyCode = 13;
event.target.id = 'subject';
event.target.id = 'stream_message_recipient_topic';
$('form#send_message_form').keydown(event);
event.target.id = 'compose-textarea';
page_params.enter_sends = false;
@ -909,7 +909,7 @@ run_test('initialize', () => {
event = {
keyCode: 13,
target: {
id: 'stream',
id: 'stream_message_recipient_stream',
},
preventDefault: noop,
};

View File

@ -115,31 +115,42 @@ function handle_keydown(e) {
var code = e.keyCode || e.which;
if (code === 13 || code === 9 && !e.shiftKey) { // Enter key or tab key
if (e.target.id === "stream" || e.target.id === "subject" || e.target.id === "private_message_recipient") {
var target_sel;
if (e.target.id) {
target_sel = '#' + e.target.id;
}
var on_stream = target_sel === "#stream_message_recipient_stream";
var on_topic = target_sel === "#stream_message_recipient_topic";
var on_pm = target_sel === "#private_message_recipient";
var on_compose = target_sel === '#compose-textarea';
if (on_stream || on_topic || on_pm) {
// For enter, prevent the form from submitting
// For tab, prevent the focus from changing again
e.preventDefault();
}
// In the compose_textarea box, preventDefault() for tab but not for enter
if (e.target.id === 'compose-textarea' && code !== 13) {
if (on_compose && code !== 13) {
e.preventDefault();
}
if (e.target.id === "stream") {
nextFocus = "subject";
} else if (e.target.id === "subject") {
if (on_stream) {
nextFocus = "#stream_message_recipient_topic";
} else if (on_topic) {
if (code === 13) {
e.preventDefault();
}
nextFocus = 'compose-textarea';
} else if (e.target.id === "private_message_recipient") {
nextFocus = 'compose-textarea';
} else if (e.target.id === 'compose-textarea') {
nextFocus = '#compose-textarea';
} else if (on_pm) {
nextFocus = '#compose-textarea';
} else if (on_compose) {
if (code === 13) {
nextFocus = false;
} else {
nextFocus = "compose-send-button";
nextFocus = "#compose-send-button";
}
} else {
nextFocus = false;
@ -159,11 +170,11 @@ function handle_keydown(e) {
// takes the typeaheads a little time to open after the user finishes typing, which
// can lead to the focus moving without the autocomplete having a chance to happen.
if (nextFocus) {
ui_util.focus_on(nextFocus);
$(nextFocus).focus();
nextFocus = false;
}
if (e.target.id === 'compose-textarea' && code === 13) {
if (on_compose && code === 13) {
var has_non_shift_modifier_key = e.ctrlKey || e.metaKey || e.altKey;
var has_modifier_key = e.shiftKey || has_non_shift_modifier_key;
var this_enter_sends;
@ -230,7 +241,7 @@ function handle_keyup(e) {
var code = e.keyCode || e.which;
if (code === 13 || code === 9 && !e.shiftKey) { // Enter key or tab key
if (nextFocus) {
ui_util.focus_on(nextFocus);
$(nextFocus).focus();
nextFocus = false;
}
}

View File

@ -9,16 +9,6 @@ exports.change_tab_to = function (tabname) {
$('#gear-menu a[href="' + tabname + '"]').tab('show');
};
exports.focus_on = function (field_id) {
// Call after autocompleting on a field, to advance the focus to
// the next input field.
// Bootstrap's typeahead does not expose a callback for when an
// autocomplete selection has been made, so we have to do this
// manually.
$("#" + field_id).focus();
};
// http://stackoverflow.com/questions/4233265/contenteditable-set-caret-at-the-end-of-the-text-cross-browser
exports.place_caret_at_end = function (el) {
el.focus();