Fix #3532: Make Enter key accept current tag during autocomplete.

Previously we patched the jqueryui-autocomplete library in order to
customize how the Tab and Enter keys behaved. Specifically, we wanted to
prevent the Tab key from moving the focus out of the tag input box, and
we wanted to prevent the Enter key from submitting the page when editing tags.

These things can achieved without patching the library by using
`event.preventDefault` and `event.stopImmediatePropagation` to prevent
other event handlers from running after these keys trigger the
`autocompleteselect` event.
This commit is contained in:
evazion
2018-02-06 19:11:01 -06:00
parent b03c8084a5
commit cf27de2ec9
6 changed files with 9 additions and 668 deletions

View File

@@ -3,7 +3,7 @@
//= require jquery-ui-1.11.2.min.js
//= require jquery.hotkeys.js
//= require jquery.timeout.js
//= require jquery-ui-autocomplete-custom.js
//= require jquery-ui-autocomplete-1.11.2.js
//= require jquery.storageapi.js
//= require jquery.dropdown.min.js
//= require jquery.hammer.js

View File

@@ -59,6 +59,11 @@
this.value += after_caret_text;
this.selectionStart = this.selectionEnd = original_start;
// Prevent the enter key from submitting the tag edit form (Danbooru.Upload.initialize_enter_on_tags).
event.stopImmediatePropagation();
// Prevent the tab key from moving the focus to the next element.
event.preventDefault();
return false;
},
source: function(req, resp) {
@@ -114,6 +119,8 @@
this.value += after_caret_text;
this.selectionStart = this.selectionEnd = original_start;
event.stopImmediatePropagation();
event.preventDefault();
return false;
},
source: function(req, resp) {
@@ -217,16 +224,6 @@
}
});
$fields_multiple.on("autocompleteselect", function() {
Danbooru.autocompleting = true;
});
$fields_multiple.on("autocompleteclose", function() {
// this is needed otherwise the var is disabled by the time the
// keydown is triggered
setTimeout(function() {Danbooru.autocompleting = false;}, 100);
});
$fields_single.autocomplete({
minLength: 1,
autoFocus: true,

View File

@@ -66,10 +66,7 @@
var $submit = $textarea.parents("form").find('input[type="submit"]');
$textarea.on("keydown.danbooru.submit", null, "return", function(e) {
if (!Danbooru.autocompleting) {
$submit.click();
}
$submit.click();
e.preventDefault();
});
}