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.

search pills: Keep search terms as free text instead of pills.

evykassirerbe09d751fa16
diff --git a/web/src/search.ts b/web/src/search.ts
index 0e4f44e3284..085a2d60af7 100644
--- a/web/src/search.ts
+++ b/web/src/search.ts
@@ -47,12 +47,22 @@ function narrow_or_search_for_term({on_narrow_search}: {on_narrow_search: OnNarr
}
assert(search_pill_widget !== null);
- const search_query = search_pill.get_current_search_string_for_widget(search_pill_widget);
+ const search_query = [
+ search_pill.get_current_search_string_for_widget(search_pill_widget),
+ get_search_bar_text(),
+ ]
+ .join(" ")
+ .trim();
if (search_query === "") {
exit_search({keep_search_narrow_open: true});
return "";
}
const terms = Filter.parse(search_query);
+ // Reset the search bar to display as many pills as possible for `terms`.
+ // We do this in case some of these terms haven't been pillified yet
+ // because convert_to_pill_on_enter is false.
+ search_pill_widget.clear();
+ search_pill.set_search_bar_contents(terms, search_pill_widget, set_search_bar_text);
on_narrow_search(terms, {trigger: "search"});
// It's sort of annoying that this is not in a position to
@@ -130,6 +140,11 @@ export function initialize({on_narrow_search}: {on_narrow_search: OnNarrowSearch
shouldHighlightFirstResult(): boolean {
return get_search_bar_text() !== "";
},
+ hideAfterSelect(): boolean {
+ const search_bar_text = get_search_bar_text();
+ const text_terms = Filter.parse(search_bar_text);
+ return text_terms.at(-1)?.operator === "search";
+ },
matcher(): boolean {
return true;
},
@@ -155,9 +170,9 @@ export function initialize({on_narrow_search}: {on_narrow_search: OnNarrowSearch
return items;
},
// Turns off `stopPropagation` in the typeahead code for
- // backspace, arrow left, arrow right, so that we can
- // manage those events for input pills.
- advanceKeyCodes: [8, 37, 39],
+ // backspace, arrow left, arrow right, and enter so that
+ // we can manage those events for search pills.
+ advanceKeyCodes: [8, 13, 37, 39],
// Use our custom typeahead `on_escape` hook to exit
// the search bar as soon as the user hits Esc.
@@ -192,6 +207,7 @@ export function initialize({on_narrow_search}: {on_narrow_search: OnNarrowSearch
is_using_input_method = true;
});
+ let typeahead_was_open_on_enter = false;
$searchbox_form
.on("keydown", (e: JQuery.KeyDownEvent): void => {
if (keydown_util.is_enter_event(e) && $search_query_box.is(":focus")) {
@@ -200,6 +216,14 @@ export function initialize({on_narrow_search}: {on_narrow_search: OnNarrowSearch
// to be done will be handled in the keyup.
e.preventDefault();
}
+
+ // Record this on keydown before the typeahead code closes the
+ // typeahead, so we can use this information on keyup.
+ if (keydown_util.is_enter_event(e) && $("#searchbox_form .typeahead").is(":visible")) {
+ typeahead_was_open_on_enter = true;
+ } else {
+ typeahead_was_open_on_enter = false;
+ }
})
.on("keyup", (e: JQuery.KeyUpEvent): void => {
if (is_using_input_method) {
@@ -211,11 +235,22 @@ export function initialize({on_narrow_search}: {on_narrow_search: OnNarrowSearch
exit_search({keep_search_narrow_open: false});
} else if (keydown_util.is_enter_event(e) && $search_query_box.is(":focus")) {
const text_terms = Filter.parse(get_search_bar_text());
- if (text_terms.every((term) => Filter.is_valid_search_term(term))) {
- narrow_or_search_for_term({on_narrow_search});
- } else {
+ // If the typeahead was open, the action was this Enter was to select
+ // an item from the typeahead and create a pill, so we don't search.
+ // The user can press Enter again to trigger the search. The one
+ // exception to this is if the user added a search term, which stays
+ // as text and is not turned into a pill, so to still have Enter complete
+ // an action, we initiate a search.
+ if (typeahead_was_open_on_enter && text_terms.at(-1)?.operator !== "search") {
+ return;
+ }
+ // The shake animation will show if there is any invalid term in the,
+ // search bar, even if it's not what the user just typed or selected.
+ if (!text_terms.every((term) => Filter.is_valid_search_term(term))) {
$("#search_query").addClass("shake");
+ return;
}
+ narrow_or_search_for_term({on_narrow_search});
}
});
diff --git a/web/src/search_pill.ts b/web/src/search_pill.ts
index 63c6abadddc..a14fdea871e 100644
--- a/web/src/search_pill.ts
+++ b/web/src/search_pill.ts
@@ -63,6 +63,7 @@ export function create_pills($pill_container: JQuery): SearchPillWidget {
create_item_from_text: create_item_from_search_string,
get_text_from_item: get_search_string_from_item,
split_text_on_comma: false,
+ convert_to_pill_on_enter: false,
});
// We don't automatically create pills on paste. When the user
// presses enter, we validate the input then.
@@ -108,6 +109,7 @@ export function set_search_bar_contents(
pill_widget.clear();
let partial_pill = "";
const invalid_inputs = [];
+ const search_operator_strings = [];
for (const term of search_terms) {
const input = Filter.unparse([term]);
@@ -136,14 +138,15 @@ export function set_search_bar_contents(
return user;
});
append_user_pill(users, pill_widget, term.operator, term.negated ?? false);
- continue;
+ } else if (term.operator === "search") {
+ search_operator_strings.push(input);
+ } else {
+ pill_widget.appendValue(input);
}
-
- pill_widget.appendValue(input);
}
pill_widget.clear_text();
- const search_bar_text_strings = [...invalid_inputs];
+ const search_bar_text_strings = [...search_operator_strings, ...invalid_inputs];
if (partial_pill !== "") {
search_bar_text_strings.push(partial_pill);
}
diff --git a/web/tests/search.test.js b/web/tests/search.test.js
index 4348dc70a93..2dee9dad917 100644
--- a/web/tests/search.test.js
+++ b/web/tests/search.test.js
@@ -241,12 +241,12 @@ run_test("initialize", ({override, override_rewire, mock_template}) => {
type: "prefix_for_operator",
},
];
- expected_pill_display_value = "ver";
+ expected_pill_display_value = null;
_setup(terms);
input_pill_displayed = false;
mock_pill_removes(search.search_pill_widget);
- assert.equal(opts.updater("ver"), "");
- assert.ok(input_pill_displayed);
+ assert.equal(opts.updater("ver"), "ver");
+ assert.ok(!input_pill_displayed);
terms = [
{