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.

timestamps: Calculate the maximum timestamp width in use.

Karl Stolley6d5243148a54
diff --git a/web/src/message_lists.ts b/web/src/message_lists.ts
index 002ee28dd43..f0c274d0232 100644
--- a/web/src/message_lists.ts
+++ b/web/src/message_lists.ts
@@ -5,6 +5,7 @@ import * as blueslip from "./blueslip";
import * as inbox_util from "./inbox_util";
import type {MessageListData} from "./message_list_data";
import type {Message} from "./message_store";
+import {stringify_time} from "./timerender";
import * as ui_util from "./ui_util";
// TODO(typescript): Move this to message_list_view when it's
@@ -130,7 +131,44 @@ export function save_pre_narrow_offset_for_reload(): void {
}
}
+export function calculate_timestamp_widths(): void {
+ const $temp_time_div = $("<div>");
+ $temp_time_div.attr("id", "calculated-timestamp-widths");
+ // Size the div to the width of the largest timestamp,
+ // but the div out of the document flow with absolute positioning.
+ $temp_time_div.css({
+ width: "max-content",
+ visibility: "hidden",
+ position: "absolute",
+ top: "-100vh",
+ });
+ // We should get a reasonable max-width by looking only at
+ // the first and last minutes of AM and PM
+ const candidate_times = ["00:00", "11:59", "12:00", "23:59"];
+
+ for (const time of candidate_times) {
+ const $temp_time_element = $("<a>");
+ $temp_time_element.attr("class", "message-time");
+ // stringify_time only returns the time, so the date here is
+ // arbitrary and only required for creating a Date object
+ const candidate_timestamp = stringify_time(Date.parse(`1999-07-01T${time}`));
+ $temp_time_element.text(candidate_timestamp);
+ $temp_time_div.append($temp_time_element);
+ }
+
+ // Append the <div> element to calculate the maximum rendered width
+ $("body").append($temp_time_div);
+ const max_timestamp_width = $temp_time_div.width();
+ // Set the width as a CSS variable
+ $(":root").css("--message-box-timestamp-column-width", `${max_timestamp_width}px`);
+ // Clean up by removing the temporary <div> element
+ $temp_time_div.remove();
+}
+
export function initialize(): void {
+ // We calculate the widths of a candidate set of timestamps,
+ // and use the largest to set `--message-box-timestamp-column-width`
+ calculate_timestamp_widths();
// For users with automatic color scheme, we need to detect change
// in `prefers-color-scheme` as it changes based on time.
ui_util.listener_for_preferred_color_scheme_change(update_recipient_bar_background_color);
diff --git a/web/src/server_events_dispatch.js b/web/src/server_events_dispatch.js
index 2924a4ec0d5..487501424e2 100644
--- a/web/src/server_events_dispatch.js
+++ b/web/src/server_events_dispatch.js
@@ -746,6 +746,8 @@ export function dispatch_normal_event(event) {
}
}
if (event.property === "twenty_four_hour_time") {
+ // Recalculate timestamp column width
+ message_lists.calculate_timestamp_widths();
// Rerender the whole message list UI
for (const msg_list of message_lists.all_rendered_message_lists()) {
msg_list.rerender();
diff --git a/web/styles/app_variables.css b/web/styles/app_variables.css
index a5fa575754b..5a37420faa8 100644
--- a/web/styles/app_variables.css
+++ b/web/styles/app_variables.css
@@ -71,7 +71,7 @@
--message-box-icon-height: 25px;
/* This width is updated with an exact pixel
width upon UI initialization. */
- --message-box-timestamp-column-width: 42px;
+ --message-box-timestamp-column-width: 0;
/*
Reaction container UI scaling.
diff --git a/web/tests/dispatch.test.js b/web/tests/dispatch.test.js
index e8c1cffd574..2febf4a42e7 100644
--- a/web/tests/dispatch.test.js
+++ b/web/tests/dispatch.test.js
@@ -872,6 +872,7 @@ run_test("user_settings", ({override}) => {
let event = event_fixtures.user_settings__default_language;
user_settings.default_language = "en";
override(settings_preferences, "update_page", noop);
+ override(message_lists, "calculate_timestamp_widths", noop);
override(overlays, "settings_open", () => true);
dispatch(event);
assert_same(user_settings.default_language, "fr");
diff --git a/web/tests/lib/zjquery.js b/web/tests/lib/zjquery.js
index b80045018a6..1503443ad18 100644
--- a/web/tests/lib/zjquery.js
+++ b/web/tests/lib/zjquery.js
@@ -18,6 +18,7 @@ function verify_selector_for_zulip(selector) {
selector === "document-stub" ||
selector === "body" ||
selector === "html" ||
+ selector === ":root" ||
selector.location ||
selector.includes("#") ||
selector.includes(".") ||
diff --git a/web/tests/popover_menus_data.test.js b/web/tests/popover_menus_data.test.js
index 9694ff3cb32..86f10245885 100644
--- a/web/tests/popover_menus_data.test.js
+++ b/web/tests/popover_menus_data.test.js
@@ -4,6 +4,7 @@ const {strict: assert} = require("assert");
const {mock_esm, zrequire} = require("./lib/namespace");
const {run_test} = require("./lib/test");
+const $ = require("./lib/zjquery");
const {page_params, realm} = require("./lib/zpage_params");
const {Filter} = zrequire("filter");
@@ -121,6 +122,12 @@ function set_page_params_no_edit_restrictions() {
// Test init function
function test(label, f) {
run_test(label, (helpers) => {
+ // Stubs for calculate_timestamp_widths()
+ $("<div>").css = noop;
+ $(":root").css = noop;
+ $("<div>").width = noop;
+ $("<div>").remove = noop;
+
// Clear stuff for testing environment
add_initialize_users();
message_lists.initialize();