Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
message_scroll: Move scroll-to-bottom button code to its own module.
Aman Agrawal575b69c476fe
diff --git a/tools/test-js-with-node b/tools/test-js-with-nodeindex 918394c0f6d..56673bffa17 100755--- a/tools/test-js-with-node+++ b/tools/test-js-with-node@@ -220,6 +220,7 @@ EXEMPT_FILES = make_set("web/src/scheduled_messages_overlay_ui.ts","web/src/scheduled_messages_ui.ts","web/src/scroll_bar.ts",+ "web/src/scroll_to_bottom_button.ts","web/src/scroll_util.ts","web/src/search.ts","web/src/search_pill.ts",diff --git a/web/src/message_scroll.ts b/web/src/message_scroll.tsindex 17fa19d7918..026696c1d17 100644--- a/web/src/message_scroll.ts+++ b/web/src/message_scroll.ts@@ -1,7 +1,5 @@import {$} from "jquery";import _ from "lodash";-import assert from "minimalistic-assert";-import type * as tippy from "tippy.js";import * as compose_banner from "./compose_banner.ts";import * as message_fetch from "./message_fetch.ts";@@ -9,68 +7,14 @@ import * as message_lists from "./message_lists.ts";import * as message_scroll_state from "./message_scroll_state.ts";import * as message_viewport from "./message_viewport.ts";import * as narrow_state from "./narrow_state.ts";+import * as scroll_to_bottom_button from "./scroll_to_bottom_button.ts";import * as unread from "./unread.ts";import * as unread_ops from "./unread_ops.ts";import * as unread_ui from "./unread_ui.ts";-import {the} from "./util.ts";--let hide_scroll_to_bottom_timer: ReturnType<typeof setInterval> | undefined;-export function hide_scroll_to_bottom(): void {- const $show_scroll_to_bottom_button = $("#scroll-to-bottom-button-container");- if (message_lists.current === undefined) {- // Scroll to bottom button is not for non-message views.- $show_scroll_to_bottom_button.removeClass("show");- return;- }-- if (- message_viewport.bottom_rendered_message_visible() ||- message_lists.current.visibly_empty()- ) {- // If last message is visible, just hide the- // scroll to bottom button.- $show_scroll_to_bottom_button.removeClass("show");- return;- }-- hide_scroll_to_bottom_timer = setInterval(() => {- // Check if the user is hovered over the scroll-to-bottom- // button every 3 seconds to allow time for interaction.- // If the user is not hovered on the button, hide the button- // and clear the timer until the next scroll event triggers- // showing the button again.- if (!the($show_scroll_to_bottom_button).matches(":hover")) {- $show_scroll_to_bottom_button.removeClass("show");- clearInterval(hide_scroll_to_bottom_timer);- }- }, 3000);-}--export function show_scroll_to_bottom_button(): void {- if (message_viewport.bottom_rendered_message_visible()) {- // Only show scroll to bottom button when- // last message is not visible in the- // current scroll position.- return;- }-- clearInterval(hide_scroll_to_bottom_timer);- $("#scroll-to-bottom-button-container").addClass("show");-}--$(document).on("keydown", (e) => {- if (e.shiftKey || e.ctrlKey || e.metaKey) {- return;- }-- // Hide scroll to bottom button on any keypress.- // Keyboard users are very less likely to use this button.- $("#scroll-to-bottom-button-container").removeClass("show");-});export function scroll_finished(): void {message_scroll_state.set_actively_scrolling(false);- hide_scroll_to_bottom();+ scroll_to_bottom_button.hide_scroll_to_bottom();if (message_lists.current === undefined) {return;@@ -135,7 +79,7 @@ function scroll_finish(): void {// Don't present the "scroll to bottom" widget if the current// scroll was triggered by the keyboard.if (!message_scroll_state.keyboard_triggered_current_scroll) {- show_scroll_to_bottom_button();+ scroll_to_bottom_button.show_scroll_to_bottom_button();}message_scroll_state.set_keyboard_triggered_current_scroll(false);@@ -151,7 +95,7 @@ export function initialize(): void {// When in a non-message view, we don't need to process// message scroll events. We just hide the scroll-to-bottom// button instantly, if it is already visible.- hide_scroll_to_bottom();+ scroll_to_bottom_button.hide_scroll_to_bottom();return;}@@ -204,21 +148,4 @@ export function initialize(): void {}}});-- const $show_scroll_to_bottom_button = $("#scroll-to-bottom-button-container").expectOne();- // Delete the tippy tooltip whenever the fadeout animation for- // this button is finished. This is necessary because the fading animation- // confuses Tippy's built-in `data-reference-hidden` feature.- $show_scroll_to_bottom_button.on("transitionend", (e) => {- assert(e.originalEvent instanceof TransitionEvent);- if (e.originalEvent.propertyName === "visibility") {- const tooltip = the(- $<tippy.ReferenceElement>("#scroll-to-bottom-button-clickable-area"),- )._tippy;- // make sure the tooltip exists and the class is not currently showing- if (tooltip && !$show_scroll_to_bottom_button.hasClass("show")) {- tooltip.destroy();- }- }- });}diff --git a/web/src/scroll_to_bottom_button.ts b/web/src/scroll_to_bottom_button.tsnew file mode 100644index 00000000000..07c1735b570--- /dev/null+++ b/web/src/scroll_to_bottom_button.ts@@ -0,0 +1,80 @@+import {$} from "jquery";+import assert from "minimalistic-assert";+import type * as tippy from "tippy.js";++import * as message_lists from "./message_lists.ts";+import * as message_viewport from "./message_viewport.ts";+import {the} from "./util.ts";++let hide_scroll_to_bottom_timer: ReturnType<typeof setInterval> | undefined;+export function hide_scroll_to_bottom(): void {+ const $show_scroll_to_bottom_button = $("#scroll-to-bottom-button-container");+ if (message_lists.current === undefined) {+ // Scroll to bottom button is not for non-message views.+ $show_scroll_to_bottom_button.removeClass("show");+ return;+ }++ if (+ message_viewport.bottom_rendered_message_visible() ||+ message_lists.current.visibly_empty()+ ) {+ // If last message is visible, just hide the+ // scroll to bottom button.+ $show_scroll_to_bottom_button.removeClass("show");+ return;+ }++ hide_scroll_to_bottom_timer = setInterval(() => {+ // Check if the user is hovered over the scroll-to-bottom+ // button every 3 seconds to allow time for interaction.+ // If the user is not hovered on the button, hide the button+ // and clear the timer until the next scroll event triggers+ // showing the button again.+ if (!the($show_scroll_to_bottom_button).matches(":hover")) {+ $show_scroll_to_bottom_button.removeClass("show");+ clearInterval(hide_scroll_to_bottom_timer);+ }+ }, 3000);+}++export function show_scroll_to_bottom_button(): void {+ if (message_viewport.bottom_rendered_message_visible()) {+ // Only show scroll to bottom button when+ // last message is not visible in the+ // current scroll position.+ return;+ }++ clearInterval(hide_scroll_to_bottom_timer);+ $("#scroll-to-bottom-button-container").addClass("show");+}++export function initialize(): void {+ $(document).on("keydown", (e) => {+ if (e.shiftKey || e.ctrlKey || e.metaKey) {+ return;+ }++ // Hide scroll to bottom button on any keypress.+ // Keyboard users are very less likely to use this button.+ $("#scroll-to-bottom-button-container").removeClass("show");+ });++ const $show_scroll_to_bottom_button = $("#scroll-to-bottom-button-container").expectOne();+ // Delete the tippy tooltip whenever the fadeout animation for+ // this button is finished. This is necessary because the fading animation+ // confuses Tippy's built-in `data-reference-hidden` feature.+ $show_scroll_to_bottom_button.on("transitionend", (e) => {+ assert(e.originalEvent instanceof TransitionEvent);+ if (e.originalEvent.propertyName === "visibility") {+ const tooltip = the(+ $<tippy.ReferenceElement>("#scroll-to-bottom-button-clickable-area"),+ )._tippy;+ // make sure the tooltip exists and the class is not currently showing+ if (tooltip && !$show_scroll_to_bottom_button.hasClass("show")) {+ tooltip.destroy();+ }+ }+ });+}diff --git a/web/src/ui_init.js b/web/src/ui_init.jsindex f49c7a2c76f..a722dfb36b1 100644--- a/web/src/ui_init.js+++ b/web/src/ui_init.js@@ -118,6 +118,7 @@ import * as scheduled_messages from "./scheduled_messages.ts";import * as scheduled_messages_overlay_ui from "./scheduled_messages_overlay_ui.ts";import * as scheduled_messages_ui from "./scheduled_messages_ui.ts";import * as scroll_bar from "./scroll_bar.ts";+import * as scroll_to_bottom_button from "./scroll_to_bottom_button.ts";import * as scroll_util from "./scroll_util.ts";import * as search from "./search.ts";import {FETCH_EVENT_TYPES} from "./server_event_types.ts";@@ -678,6 +679,7 @@ export async function initialize_everything(state_data) {server_events.finished_initial_fetch();});message_scroll.initialize();+ scroll_to_bottom_button.initialize();markdown.initialize(markdown_config.get_helpers());linkifiers.initialize(realm.realm_linkifiers);realm_playground.initialize({