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.

info_density: Move timestamp calculations to better location.

Karl Stolley7dc20a56f2d8
diff --git a/web/src/information_density.ts b/web/src/information_density.ts
index 2fa279b9381..821d4e69c12 100644
--- a/web/src/information_density.ts
+++ b/web/src/information_density.ts
@@ -1,5 +1,6 @@
import $ from "jquery";
+import {stringify_time} from "./timerender";
import {user_settings} from "./user_settings";
// These are all relative-unit values for Source Sans Pro VF,
@@ -95,6 +96,43 @@ export function set_base_typography_css_variables(): void {
set_vertical_alignment_values(line_height_unitless);
}
+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 {
set_base_typography_css_variables();
+ // We calculate the widths of a candidate set of timestamps,
+ // and use the largest to set `--message-box-timestamp-column-width`
+ calculate_timestamp_widths();
}
diff --git a/web/src/message_lists.ts b/web/src/message_lists.ts
index 621da349965..3a2b416ce1d 100644
--- a/web/src/message_lists.ts
+++ b/web/src/message_lists.ts
@@ -3,7 +3,6 @@ import $ from "jquery";
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
@@ -147,44 +146,7 @@ export function update_recipient_bar_background_color(): void {
inbox_util.update_stream_colors();
}
-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 a4f2e4b1c43..cf0df040df3 100644
--- a/web/src/server_events_dispatch.js
+++ b/web/src/server_events_dispatch.js
@@ -751,7 +751,7 @@ export function dispatch_normal_event(event) {
}
if (event.property === "twenty_four_hour_time") {
// Recalculate timestamp column width
- message_lists.calculate_timestamp_widths();
+ information_density.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/tests/dispatch.test.js b/web/tests/dispatch.test.js
index 5a51635580e..479dec5c206 100644
--- a/web/tests/dispatch.test.js
+++ b/web/tests/dispatch.test.js
@@ -889,7 +889,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(information_density, "calculate_timestamp_widths", noop);
override(overlays, "settings_open", () => true);
dispatch(event);
assert_same(user_settings.default_language, "fr");