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.

util: Extract max_text_content_width for measuring text widths.

Aman Agrawal146f30d456f2
diff --git a/web/src/information_density.ts b/web/src/information_density.ts
index a4c9c67cebd..02ada6c58ee 100644
--- a/web/src/information_density.ts
+++ b/web/src/information_density.ts
@@ -7,6 +7,7 @@ import * as resize from "./resize.ts";
import * as stream_data from "./stream_data.ts";
import {stringify_time} from "./timerender.ts";
import {user_settings} from "./user_settings.ts";
+import * as util from "./util.ts";
// These are all relative-unit values for Source Sans Pro VF,
// as opened and inspected in FontForge.
@@ -128,43 +129,16 @@ export function set_base_typography_css_variables(): void {
}
export function calculate_timestamp_widths(): void {
- const base_font_size_px = user_settings.web_font_size_px;
- 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.
- // We set the base font-size ordinarily on body so that
- // the correct em-size timestamps can be calculated along
- // with all the other information density values.
- $temp_time_div.css({
- "font-size": `${base_font_size_px}px`,
- 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
+ // 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");
+ const candidate_strings = candidate_times.map((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
+ // arbitrary and only required for creating a Date object.
+ stringify_time(Date.parse(`1999-07-01T${time}`)),
+ );
+ const max_timestamp_width = util.max_text_content_width(candidate_strings, "message-time");
$(":root").css("--message-box-timestamp-column-width", `${max_timestamp_width}px`);
- // Clean up by removing the temporary <div> element
- $temp_time_div.remove();
}
function determine_container_query_support(): void {
diff --git a/web/src/stream_data.ts b/web/src/stream_data.ts
index f03d7d6bf5d..4d9e8749a60 100644
--- a/web/src/stream_data.ts
+++ b/web/src/stream_data.ts
@@ -1348,33 +1348,10 @@ export function get_streams_for_move_messages_widget(): (dropdown_widget.Option
}));
}
-function longest_subscribed_channel_name_width(): number {
- let longest_channel_name_width = 0;
- const $measure_div = $("<div>").css({
- position: "absolute",
- visibility: "hidden",
- whiteSpace: "nowrap",
- left: "-9999px",
- top: "0",
- });
- $("body").append($measure_div);
-
- for (const channel_name of subscribed_streams()) {
- $measure_div.text(channel_name);
- const width = $measure_div.get(0)!.getBoundingClientRect().width;
- if (width > longest_channel_name_width) {
- longest_channel_name_width = width;
- }
- }
-
- $measure_div.remove();
- return longest_channel_name_width;
-}
-
export let set_max_channel_width_css_variable = async (): Promise<void> => {
// Return a promise to avoid blocking main thread.
const promise = new Promise<void>((resolve) => {
- const length = longest_subscribed_channel_name_width();
+ const length = util.max_text_content_width([...subscribed_streams()]);
$(":root").css("--longest-subscribed-channel-name-width", `${length}px`);
resolve();
});
diff --git a/web/src/util.ts b/web/src/util.ts
index bc159f545f6..84e56bf2080 100644
--- a/web/src/util.ts
+++ b/web/src/util.ts
@@ -622,3 +622,39 @@ export function parse_youtube_start_time(url: string): number | undefined {
return undefined;
}
+
+// Measure the maximum rendered width of a set of candidate text
+// strings. This is used to set CSS variables for column widths
+// that need to fit their content tightly. All candidates are
+// inserted as block-level children of a single hidden container
+// sized to max-content, so only one reflow is needed.
+/* istanbul ignore next */
+export let max_text_content_width = (candidates: string[], css_class?: string): number => {
+ const container = document.createElement("div");
+ Object.assign(container.style, {
+ position: "absolute",
+ visibility: "hidden",
+ whiteSpace: "nowrap",
+ width: "max-content",
+ left: "-9999px",
+ top: "0",
+ });
+
+ for (const text of candidates) {
+ const child = document.createElement("div");
+ if (css_class !== undefined) {
+ child.className = css_class;
+ }
+ child.textContent = text;
+ container.append(child);
+ }
+
+ document.body.append(container);
+ const width = container.getBoundingClientRect().width;
+ container.remove();
+ return width;
+};
+
+export function rewire_max_text_content_width(value: typeof max_text_content_width): void {
+ max_text_content_width = value;
+}
diff --git a/web/tests/stream_data.test.cjs b/web/tests/stream_data.test.cjs
index 616e67cba97..7162e7d6e13 100644
--- a/web/tests/stream_data.test.cjs
+++ b/web/tests/stream_data.test.cjs
@@ -23,6 +23,7 @@ const stream_data = zrequire("stream_data");
const hash_util = zrequire("hash_util");
const {set_current_user, set_realm} = zrequire("state_data");
const stream_settings_data = zrequire("stream_settings_data");
+const util = zrequire("util");
const user_groups = zrequire("user_groups");
const {initialize_user_settings} = zrequire("user_settings");
@@ -2561,7 +2562,7 @@ run_test("can_use_empty_topic", ({override}) => {
assert.equal(stream_data.can_use_empty_topic(scotland.stream_id), false);
});
-test("set_max_channel_width_css_variable", async () => {
+test("set_max_channel_width_css_variable", async ({override_rewire}) => {
const stream = {
subscribed: true,
color: "blue",
@@ -2576,11 +2577,16 @@ test("set_max_channel_width_css_variable", async () => {
};
stream_data.add_sub_for_tests(stream);
- const $measure_div = $("<div>");
const $root = $(":root");
- $measure_div[0].getBoundingClientRect = () => ({width: $measure_div.text().length});
- $measure_div[0].remove = () => {};
+ override_rewire(util, "max_text_content_width", (candidates) => {
+ // Return the length of the longest candidate string.
+ let max_len = 0;
+ for (const s of candidates) {
+ max_len = Math.max(max_len, s.length);
+ }
+ return max_len;
+ });
await stream_data.set_max_channel_width_css_variable();