Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
stream_list: Restore scroll position for user after update.
Aman Agrawal74276b05c48e
diff --git a/web/src/message_fetch.ts b/web/src/message_fetch.tsindex d0330bb9ed0..7f4507ae4c0 100644--- a/web/src/message_fetch.ts+++ b/web/src/message_fetch.ts@@ -178,7 +178,6 @@ function process_result(data: MessageFetchResponse, opts: MessageFetchOptions):direct_message_group_data.process_loaded_messages(messages);stream_list.update_streams_sidebar_for_messages(messages);- stream_list.maybe_scroll_narrow_into_view(!first_messages_fetch);if (message_lists.current !== undefined &&diff --git a/web/src/stream_list.ts b/web/src/stream_list.tsindex 22361b4da0c..d0135faea98 100644--- a/web/src/stream_list.ts+++ b/web/src/stream_list.ts@@ -22,6 +22,7 @@ import {$t} from "./i18n.ts";import * as keydown_util from "./keydown_util.ts";import * as left_sidebar_navigation_area from "./left_sidebar_navigation_area.ts";import {localstorage} from "./localstorage.ts";+import type {Message} from "./message_store.ts";import * as mouse_drag from "./mouse_drag.ts";import * as narrow_state from "./narrow_state.ts";import {page_params} from "./page_params.ts";@@ -54,7 +55,15 @@ export function set_update_inbox_channel_view_callback(value: (channel_id: numbeupdate_inbox_channel_view_callback = value;}-let has_scrolled = false;+let is_actively_scrolling = false;++const left_sidebar_scroll_state: {+ scroll_container_scroll_top: number | undefined;+ selection_offset_to_top: number | undefined;+} = {+ scroll_container_scroll_top: undefined,+ selection_offset_to_top: undefined,+};const collapsed_sections = new Set<string>();const sections_with_only_inactive_or_muted = new Set<string>();@@ -69,6 +78,59 @@ export function is_zoomed_in(): boolean {return zoomed_in;}+function get_selection_offset_from_top($target: JQuery, $scroll_element: JQuery): number {+ const container_top = $scroll_element.offset()?.top ?? 0;+ const target_top = $target.offset()?.top ?? 0;+ return target_top - container_top;+}++function capture_left_sidebar_selection_anchor(): void {+ const $scroll_element = scroll_util.get_left_sidebar_scroll_container();+ left_sidebar_scroll_state.scroll_container_scroll_top = $scroll_element.scrollTop();+ // .active-sub-filter: Selected topic or DM.+ // .active-filter: Selected channel.+ // We don't need to handle others since restoring `scroll_container_scroll_top` would+ // work for them.+ const $active_filter = $scroll_element.find(".active-sub-filter, .active-filter");+ if ($active_filter.length === 1) {+ left_sidebar_scroll_state.selection_offset_to_top = get_selection_offset_from_top(+ $active_filter,+ $scroll_element,+ );+ } else {+ left_sidebar_scroll_state.selection_offset_to_top = undefined;+ }+}++function restore_left_sidebar_scroll_state(): void {+ const $scroll_element = scroll_util.get_left_sidebar_scroll_container();+ if (left_sidebar_scroll_state.selection_offset_to_top !== undefined) {+ const $active_topic_or_channel = $scroll_element.find(".active-sub-filter, .active-filter");+ if ($active_topic_or_channel.length === 1) {+ const current_offset_to_top = get_selection_offset_from_top(+ $active_topic_or_channel,+ $scroll_element,+ );+ const offset_difference =+ current_offset_to_top - left_sidebar_scroll_state.selection_offset_to_top;+ $scroll_element.scrollTop(($scroll_element.scrollTop() ?? 0) + offset_difference);+ return;+ }+ }+ // Fallback: restore the raw scroll position captured before the update.+ if (left_sidebar_scroll_state.scroll_container_scroll_top !== undefined) {+ $scroll_element.scrollTop(left_sidebar_scroll_state.scroll_container_scroll_top);+ }+}++function update_streams_sidebar_and_restore_scroll_state(): void {+ requestAnimationFrame(() => {+ capture_left_sidebar_selection_anchor();+ update_streams_sidebar();+ restore_left_sidebar_scroll_state();+ });+}+export function update_streams_sidebar_for_messages(messages: Message[]): void {const channel_id = topic_list.active_stream_id();if (channel_id === undefined) {@@ -86,7 +148,11 @@ export function update_streams_sidebar_for_messages(messages: Message[]): void {return;}- update_streams_sidebar();+ if (is_actively_scrolling) {+ update_streams_sidebar();+ } else {+ update_streams_sidebar_and_restore_scroll_state();+ }}function zoom_in(): void {@@ -1530,9 +1596,15 @@ export function set_event_handlers({}}+ let mark_scroll_inactive_timeout: ReturnType<typeof setTimeout> | undefined;// check for user scrolls on streams list for first timescroll_util.get_scroll_element($("#left_sidebar_scroll_container")).on("scroll", () => {- has_scrolled = true;+ is_actively_scrolling = true;+ clearTimeout(mark_scroll_inactive_timeout);+ mark_scroll_inactive_timeout = setTimeout(() => {+ is_actively_scrolling = false;+ }, 200);+toggle_pm_header_icon();});@@ -1629,15 +1701,6 @@ export function rewire_scroll_stream_into_view(value: typeof scroll_stream_into_scroll_stream_into_view = value;}-export function maybe_scroll_narrow_into_view(first_messages_fetch_done: boolean): void {- // we don't want to interfere with user scrolling once the page loads- if (has_scrolled && first_messages_fetch_done) {- return;- }-- scroll_stream_into_view();-}-export function get_current_stream_li(): JQuery | undefined {const stream_id = topic_list.active_stream_id();diff --git a/web/tests/example5.test.cjs b/web/tests/example5.test.cjsindex c92edcc3b51..4fe55ea7f76 100644--- a/web/tests/example5.test.cjs+++ b/web/tests/example5.test.cjs@@ -114,7 +114,7 @@ run_test("insert_server_message", ({override}) => {helper.redirect(direct_message_group_data, "process_loaded_messages");helper.redirect(message_notifications, "received_messages");- helper.redirect(stream_list, "update_streams_sidebar");+ helper.redirect(stream_list, "update_streams_sidebar_for_messages");helper.redirect(unread_ui, "update_unread_counts");helper.redirect(activity, "set_received_new_messages");@@ -132,7 +132,7 @@ run_test("insert_server_message", ({override}) => {[unread_ui, "update_unread_counts"],[activity, "set_received_new_messages"],[message_notifications, "received_messages"],- [stream_list, "update_streams_sidebar"],+ [stream_list, "update_streams_sidebar_for_messages"],]);assert.ok(added_message);@@ -168,7 +168,7 @@ run_test("insert_local_message", ({override}) => {helper.redirect(direct_message_group_data, "process_loaded_messages");helper.redirect(message_notifications, "received_messages");- helper.redirect(stream_list, "update_streams_sidebar");+ helper.redirect(stream_list, "update_streams_sidebar_for_messages");helper.redirect(activity, "set_received_new_messages");message_events.insert_new_messages({@@ -184,7 +184,7 @@ run_test("insert_local_message", ({override}) => {[direct_message_group_data, "process_loaded_messages"],[activity, "set_received_new_messages"],[message_notifications, "received_messages"],- [stream_list, "update_streams_sidebar"],+ [stream_list, "update_streams_sidebar_for_messages"],]);assert.ok(added_message);