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.

left_sidebar: Scroll user to top on search.

Aman Agrawal5b22d6e2b8df
diff --git a/web/src/pm_list.ts b/web/src/pm_list.ts
index a716245ecf2..33a9177e4a2 100644
--- a/web/src/pm_list.ts
+++ b/web/src/pm_list.ts
@@ -28,6 +28,10 @@ let private_messages_collapsed = false;
// This keeps track of if we're zoomed in or not.
let zoomed = false;
+// Scroll position before user started searching.
+let pre_search_scroll_position = 0;
+let previous_search_term = "";
+
export function is_zoomed_in(): boolean {
return zoomed;
}
@@ -239,6 +243,8 @@ export function toggle_private_messages_section(): void {
function zoom_in(): void {
zoomed = true;
+ previous_search_term = "";
+ pre_search_scroll_position = 0;
ui_util.disable_left_sidebar_search();
update_private_messages();
$(".direct-messages-container").removeClass("zoom-out").addClass("zoom-in");
@@ -276,7 +282,6 @@ export function initialize(): void {
expand();
}
- const throttled_update_private_message = _.throttle(update_private_messages, 50);
$(".direct-messages-container").on("click", "#show-more-direct-messages", (e) => {
e.stopPropagation();
e.preventDefault();
@@ -298,6 +303,32 @@ export function initialize(): void {
zoom_out();
});
+ const throttled_update_private_message = _.throttle(() => {
+ const $filter = $<HTMLInputElement>(".direct-messages-list-filter").expectOne();
+ const search_term = $filter.val()!;
+ const is_previous_search_term_empty = previous_search_term === "";
+ previous_search_term = search_term;
+
+ const left_sidebar_scroll_container = scroll_util.get_left_sidebar_scroll_container();
+ if (search_term === "") {
+ requestAnimationFrame(() => {
+ update_private_messages();
+ // Restore previous scroll position.
+ left_sidebar_scroll_container.scrollTop(pre_search_scroll_position);
+ });
+ } else {
+ if (is_previous_search_term_empty) {
+ // Store original scroll position to be restored later.
+ pre_search_scroll_position = left_sidebar_scroll_container.scrollTop()!;
+ }
+ requestAnimationFrame(() => {
+ update_private_messages();
+ // Always scroll to top when there is a search term present.
+ left_sidebar_scroll_container.scrollTop(0);
+ });
+ }
+ }, 50);
+
$(".direct-messages-container").on("input", ".direct-messages-list-filter", (e) => {
e.preventDefault();
diff --git a/web/src/scroll_util.ts b/web/src/scroll_util.ts
index c886b67776d..17df10f274a 100644
--- a/web/src/scroll_util.ts
+++ b/web/src/scroll_util.ts
@@ -102,3 +102,7 @@ export function scroll_element_into_container(
$container.scrollTop(($container.scrollTop() ?? 0) + delta);
}
+
+export function get_left_sidebar_scroll_container(): JQuery {
+ return get_scroll_element($("#left_sidebar_scroll_container"));
+}
diff --git a/web/src/sidebar_ui.ts b/web/src/sidebar_ui.ts
index 8613e2ccb7d..a3c49411952 100644
--- a/web/src/sidebar_ui.ts
+++ b/web/src/sidebar_ui.ts
@@ -21,6 +21,7 @@ import * as popover_menus from "./popover_menus.ts";
import * as popovers from "./popovers.ts";
import * as resize from "./resize.ts";
import * as scheduled_messages from "./scheduled_messages.ts";
+import * as scroll_util from "./scroll_util.ts";
import * as search_util from "./search_util.ts";
import * as settings_config from "./settings_config.ts";
import * as settings_data from "./settings_data.ts";
@@ -605,7 +606,34 @@ function actually_update_left_sidebar_for_search(): void {
);
}
-const update_left_sidebar_for_search = _.throttle(actually_update_left_sidebar_for_search, 50);
+// Scroll position before user started searching.
+let pre_search_scroll_position = 0;
+let previous_search_term = "";
+
+const update_left_sidebar_for_search = _.throttle(() => {
+ const search_term = ui_util.get_left_sidebar_search_term();
+ const is_previous_search_term_empty = previous_search_term === "";
+ previous_search_term = search_term;
+
+ const left_sidebar_scroll_container = scroll_util.get_left_sidebar_scroll_container();
+ if (search_term === "") {
+ requestAnimationFrame(() => {
+ actually_update_left_sidebar_for_search();
+ // Restore previous scroll position.
+ left_sidebar_scroll_container.scrollTop(pre_search_scroll_position);
+ });
+ } else {
+ if (is_previous_search_term_empty) {
+ // Store original scroll position to be restored later.
+ pre_search_scroll_position = left_sidebar_scroll_container.scrollTop()!;
+ }
+ requestAnimationFrame(() => {
+ actually_update_left_sidebar_for_search();
+ // Always scroll to top when there is a search term present.
+ left_sidebar_scroll_container.scrollTop(0);
+ });
+ }
+}, 50);
function focus_left_sidebar_filter(e: JQuery.ClickEvent): void {
left_sidebar_cursor.reset();
diff --git a/web/src/topic_list.ts b/web/src/topic_list.ts
index 8feff208382..cfd1e8bebd1 100644
--- a/web/src/topic_list.ts
+++ b/web/src/topic_list.ts
@@ -34,6 +34,10 @@ export let topic_state_typeahead: Typeahead<TopicFilterPill> | undefined;
// We know whether we're zoomed or not.
let zoomed = false;
+// Scroll position before user started searching.
+let pre_search_scroll_position = 0;
+let previous_search_term = "";
+
export function update(): void {
for (const widget of active_widgets.values()) {
widget.build();
@@ -435,6 +439,8 @@ export function left_sidebar_scroll_zoomed_in_topic_into_view(): void {
// handle hiding/showing the non-narrowed streams
export function zoom_in(): void {
zoomed = true;
+ previous_search_term = "";
+ pre_search_scroll_position = 0;
ui_util.disable_left_sidebar_search();
const stream_id = active_stream_id();
@@ -636,9 +642,20 @@ export function initialize({
$("body").on("input", "#left-sidebar-filter-topic-input", (): void => {
const stream_id = active_stream_id();
assert(stream_id !== undefined);
- active_widgets.get(stream_id)?.build();
- if (get_left_sidebar_topic_search_term() === "") {
+ const search_term = get_left_sidebar_topic_search_term();
+ const is_previous_search_term_empty = previous_search_term === "";
+ previous_search_term = search_term;
+
+ const widget = active_widgets.get(stream_id)!;
+ const left_sidebar_scroll_container = scroll_util.get_left_sidebar_scroll_container();
+ if (search_term === "") {
+ requestAnimationFrame(() => {
+ widget.build();
+ // Restore previous scroll position.
+ left_sidebar_scroll_container.scrollTop(pre_search_scroll_position);
+ });
+
// When the contenteditable div is empty, the browser
// adds a <br> element to it, which interferes with
// the ":empty" selector in the CSS. Hence, we detect
@@ -651,6 +668,16 @@ export function initialize({
// doesn't work in this particular case.
// See: https://stackoverflow.com/questions/14638887/br-is-inserted-into-contenteditable-html-element-if-left-empty
$("#topic_filter_query").empty();
+ } else {
+ if (is_previous_search_term_empty) {
+ // Store original scroll position to be restored later.
+ pre_search_scroll_position = left_sidebar_scroll_container.scrollTop()!;
+ }
+ requestAnimationFrame(() => {
+ widget.build();
+ // Always scroll to top when there is a search term present.
+ left_sidebar_scroll_container.scrollTop(0);
+ });
}
});
}