Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

typeahead: Allow automated selection for `contenteditable` elements.

Ryan Rehman531a803bfc96
diff --git a/frontend_tests/node_tests/search.js b/frontend_tests/node_tests/search.js
index 7637c3ae186..cb9acc73ff2 100644
--- a/frontend_tests/node_tests/search.js
+++ b/frontend_tests/node_tests/search.js
@@ -16,6 +16,7 @@ set_global('narrow_state', {filter: return_false});
set_global('search_suggestion', {});
set_global('ui_util', {
change_tab_to: noop,
+ place_caret_at_end: noop,
});
set_global('narrow', {});
@@ -94,6 +95,7 @@ run_test('initialize', () => {
assert.equal(opts.naturalSearch, true);
assert.equal(opts.helpOnEmptyStrings, true);
assert.equal(opts.matcher(), true);
+ assert.equal(opts.on_move(), true);
{
const search_suggestions = {
diff --git a/static/js/search.js b/static/js/search.js
index df500386bcd..40096388d61 100644
--- a/static/js/search.js
+++ b/static/js/search.js
@@ -103,6 +103,12 @@ exports.initialize = function () {
stopAdvance: page_params.search_pills_enabled,
advanceKeyCodes: [8],
+ on_move: function () {
+ if (page_params.search_pills_enabled) {
+ ui_util.place_caret_at_end(search_query_box[0]);
+ return true;
+ }
+ },
// Use our custom typeahead `on_escape` hook to exit
// the search bar as soon as the user hits Esc.
on_escape: tab_bar.exit_search,
@@ -195,6 +201,7 @@ exports.initiate_search = function () {
$('#search_query').typeahead('lookup').select();
if (page_params.search_pills_enabled) {
$('#search_query').focus();
+ ui_util.place_caret_at_end($('#search_query')[0]);
}
};
diff --git a/static/third/bootstrap-typeahead/typeahead.js b/static/third/bootstrap-typeahead/typeahead.js
index 5a8e6a2e553..90f0c2907f1 100644
--- a/static/third/bootstrap-typeahead/typeahead.js
+++ b/static/third/bootstrap-typeahead/typeahead.js
@@ -28,6 +28,7 @@
* choice.
*
* Our custom changes include all mentions of this.automated.
+ * And also includes the blocks containing the is contenteditable condition.
*
* 2. Custom selection triggers:
*
@@ -81,6 +82,7 @@
this.fixed = this.options.fixed || false;
this.automated = this.options.automated || this.automated;
this.trigger_selection = this.options.trigger_selection || this.trigger_selection;
+ this.on_move = this.options.on_move;
this.on_escape = this.options.on_escape;
this.header = this.options.header || this.header;
@@ -99,15 +101,25 @@
, select: function (e) {
var val = this.$menu.find('.active').data('typeahead-value')
- this.$element
- .val(this.updater(val, e))
- .change()
+ if (this.$element.is("[contenteditable]")) {
+ this.$element.html(this.updater(val, e)).change();
+ // Empty textContent after the change event handler
+ // converts the input text to html elements.
+ this.$element.html('');
+ } else {
+ this.$element.val(this.updater(val, e)).change();
+ }
+
return this.hide()
}
, set_value: function () {
var val = this.$menu.find('.active').data('typeahead-value')
- this.$element.val(val)
+ this.$element.is("[contenteditable]") ? this.$element.html(val) : this.$element.val(val);
+
+ if (this.on_move) {
+ this.on_move();
+ }
}
, updater: function (item) {