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.

inbox: Find the row at the center by position to fix scroll jumps.

Aman Agrawal4476b7f6da9b
diff --git a/web/src/inbox_ui.ts b/web/src/inbox_ui.ts
index bf08dc50377..7fe7094b84f 100644
--- a/web/src/inbox_ui.ts
+++ b/web/src/inbox_ui.ts
@@ -2361,28 +2361,10 @@ function move_focus_to_visible_area(): void {
const inbox_filters_props = util.the($("#inbox-filters")).getBoundingClientRect();
const compose_top = window.innerHeight - $("#compose").outerHeight(true)!;
- const inbox_center_x = (inbox_filters_props.left + inbox_filters_props.right) / 2;
const inbox_center_y = (compose_top + inbox_filters_props.bottom) / 2;
- const element_in_row = views_util.find_element_at_point(
- inbox_center_x,
- inbox_center_y,
- ".inbox-row, .inbox-header",
- );
- if (element_in_row === undefined) {
- // The table is too short for there to be a row element
- // at the center of the table region; in that case, we just
- // select the last element.
- row_focus = $all_rows.length - 1;
- } else {
- const $element_in_row = $(element_in_row);
-
- let $inbox_row = $element_in_row.closest(".inbox-row");
- if ($inbox_row.length === 0) {
- $inbox_row = $element_in_row.closest(".inbox-header");
- }
-
- row_focus = $all_rows.index($inbox_row.get(0));
- }
+ row_focus =
+ views_util.find_first_row_index_at_or_below($all_rows.toArray(), inbox_center_y) ??
+ $all_rows.length - 1;
revive_current_focus();
}
diff --git a/web/src/views_util.ts b/web/src/views_util.ts
index 61f2dedf3f7..ecfeb464c18 100644
--- a/web/src/views_util.ts
+++ b/web/src/views_util.ts
@@ -207,6 +207,15 @@ export function find_element_at_point(
return undefined;
}
+export function find_first_row_index_at_or_below(rows: Element[], y: number): number | undefined {
+ // Returns the index of the first row whose bottom edge is below
+ // the viewport coordinate `y` (the row containing `y`, or else the
+ // first row below it), or undefined if there is none. Assumes
+ // `rows` are in top-to-bottom order and don't overlap.
+ const index = rows.findIndex((row) => row.getBoundingClientRect().bottom > y);
+ return index === -1 ? undefined : index;
+}
+
export function is_scroll_position_for_render(): boolean {
const scroll_position = window.scrollY;
const window_height = window.innerHeight;
diff --git a/web/tests/views_util.test.cjs b/web/tests/views_util.test.cjs
new file mode 100644
index 00000000000..e24d23889b6
--- /dev/null
+++ b/web/tests/views_util.test.cjs
@@ -0,0 +1,38 @@
+"use strict";
+
+const assert = require("node:assert/strict");
+
+const {zrequire} = require("./lib/namespace.cjs");
+const {run_test} = require("./lib/test.cjs");
+
+const views_util = zrequire("views_util");
+
+function make_row(top, height = 30) {
+ return {
+ getBoundingClientRect: () => ({top, bottom: top + height}),
+ };
+}
+
+run_test("find_first_row_index_at_or_below", () => {
+ // Two adjacent rows, then a gap like the margin between two
+ // sections of Inbox, a row, and a smaller gap like the one below
+ // a folder header.
+ const rows = [make_row(100), make_row(130), make_row(168), make_row(199.5)];
+ const find_row_index = (y) => views_util.find_first_row_index_at_or_below(rows, y);
+
+ assert.equal(find_row_index(50), 0);
+ assert.equal(find_row_index(145), 1);
+ assert.equal(find_row_index(175), 2);
+ assert.equal(find_row_index(210), 3);
+ // A point on the edge between two adjacent rows selects the lower one.
+ assert.equal(find_row_index(130), 1);
+ // A point in a gap, including its top edge, selects the row just
+ // below the gap.
+ assert.equal(find_row_index(160), 2);
+ assert.equal(find_row_index(162), 2);
+ assert.equal(find_row_index(198.5), 3);
+ // A point at or below the bottom edge of the last row selects no row.
+ assert.equal(find_row_index(229.5), undefined);
+ assert.equal(find_row_index(500), undefined);
+ assert.equal(views_util.find_first_row_index_at_or_below([], 145), undefined);
+});