Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
recent_view: Restore scroll position when clearing search.
Aman Agrawal72e51cd0dcff
diff --git a/web/src/recent_view_ui.ts b/web/src/recent_view_ui.tsindex 8c0a1a9ea20..3905be3771d 100644--- a/web/src/recent_view_ui.ts+++ b/web/src/recent_view_ui.ts@@ -124,6 +124,13 @@ let is_waiting_for_revive_current_focus = true;let last_scroll_offset: number | undefined;let hide_other_views_callback: (() => void) | undefined;+// Row focus and viewport offset of the focused row before search,+// so we can restore both focus and scroll position when search is+// cleared.+let pre_search_row_focus: number | undefined;+let pre_search_topic_viewport_offset: number | undefined;+let previous_search_term = "";+export function set_hide_other_views(callback: () => void): void {hide_other_views_callback = callback;}@@ -2172,6 +2179,12 @@ export function initialize({change_focused_element($(e.target), "click");});+ $(window).on("resize", () => {+ // The saved viewport offset is no longer meaningful after a+ // resize, so discard it to avoid incorrect scroll restoration.+ pre_search_topic_viewport_offset = undefined;+ });+$("body").on("click", "#recent-view-content-table .on_hover_topic_read", (e) => {e.stopPropagation();assert(e.currentTarget instanceof HTMLElement);@@ -2290,10 +2303,48 @@ export function initialize({"#recent_view_search",_.debounce(() => {const search_term = $<HTMLInputElement>("#recent_view_search").val() ?? "";+ const is_previous_search_term_empty = previous_search_term === "";+ previous_search_term = search_term;++ if (search_term !== "" && is_previous_search_term_empty) {+ // Store the focused row index and its viewport offset+ // to restore both focus and scroll position when the+ // search is cleared.+ pre_search_row_focus = row_focus;+ const $topic_row = $("#recent-view-content-tbody tr").eq(row_focus);+ pre_search_topic_viewport_offset = $topic_row[0]?.getBoundingClientRect().top;+ }if (search_term === "") {- row_focus = 0;+ // Restore scroll position to the row that was focused+ // before search started.+ row_focus = pre_search_row_focus ?? 0;update_filters_view();+ if (topics_widget !== undefined) {+ // Clamp row_focus if the list shrank while+ // searching (e.g., messages were read with the+ // "unread" filter active).+ const list_length = topics_widget.get_current_list().length;+ if (list_length > 0) {+ row_focus = Math.min(row_focus, list_length - 1);+ } else {+ row_focus = 0;+ }+ // The initial render might not have rendered enough+ // rows for the target row_focus. Calling render()+ // will use get_min_load_count to extend rendering+ // if needed.+ topics_widget.render();+ }+ // Restore the row to the same viewport position it+ // was at before search started.+ const $topic_row = $("#recent-view-content-tbody tr").eq(row_focus);+ const current_top = $topic_row[0]?.getBoundingClientRect().top;+ if (current_top !== undefined && pre_search_topic_viewport_offset !== undefined) {+ window.scrollBy(0, current_top - pre_search_topic_viewport_offset);+ }+ pre_search_row_focus = undefined;+ pre_search_topic_viewport_offset = undefined;} else {// Reset focus to first row on new search.row_focus = 0;