From 42dd55df42d6352e713bf94208cc54c4d9ad190f Mon Sep 17 00:00:00 2001 From: Temidayo32 Date: Thu, 7 Mar 2024 20:46:14 +0000 Subject: [PATCH] condense: Convert module to Typescript. --- tools/test-js-with-node | 2 +- web/src/{condense.js => condense.ts} | 41 +++++++++++++++------------- web/src/message_store.ts | 1 + 3 files changed, 24 insertions(+), 20 deletions(-) rename web/src/{condense.js => condense.ts} (88%) diff --git a/tools/test-js-with-node b/tools/test-js-with-node index f14e35dc2e..3dcef1058d 100755 --- a/tools/test-js-with-node +++ b/tools/test-js-with-node @@ -81,7 +81,7 @@ EXEMPT_FILES = make_set( "web/src/compose_ui.ts", "web/src/compose_validate.js", "web/src/composebox_typeahead.js", - "web/src/condense.js", + "web/src/condense.ts", "web/src/confirm_dialog.ts", "web/src/copied_tooltip.ts", "web/src/copy_and_paste.js", diff --git a/web/src/condense.js b/web/src/condense.ts similarity index 88% rename from web/src/condense.js rename to web/src/condense.ts index 0a53c99621..4856d08b6e 100644 --- a/web/src/condense.js +++ b/web/src/condense.ts @@ -3,6 +3,7 @@ import assert from "minimalistic-assert"; import * as message_flags from "./message_flags"; import * as message_lists from "./message_lists"; +import type {Message} from "./message_store"; import * as message_viewport from "./message_viewport"; import * as rows from "./rows"; @@ -18,35 +19,35 @@ This library implements two related, similar concepts: */ -function show_more_link($row) { +function show_more_link($row: JQuery): void { $row.find(".message_condenser").hide(); $row.find(".message_expander").show(); } -function show_condense_link($row) { +function show_condense_link($row: JQuery): void { $row.find(".message_expander").hide(); $row.find(".message_condenser").show(); } -function condense_row($row) { +function condense_row($row: JQuery): void { const $content = $row.find(".message_content"); $content.addClass("condensed"); show_more_link($row); } -function uncondense_row($row) { +function uncondense_row($row: JQuery): void { const $content = $row.find(".message_content"); $content.removeClass("condensed"); show_condense_link($row); } -export function uncollapse(message) { +export function uncollapse(message: Message): void { // Uncollapse a message, restoring the condensed message "Show more" or // "Show less" button if necessary. message.collapsed = false; message_flags.save_uncollapsed(message); - const process_row = function process_row($row) { + const process_row = function process_row($row: JQuery): void { const $content = $row.find(".message_content"); $content.removeClass("collapsed"); @@ -75,7 +76,7 @@ export function uncollapse(message) { } } -export function collapse(message) { +export function collapse(message: Message): void { message.collapsed = true; if (message.locally_echoed) { @@ -88,7 +89,7 @@ export function collapse(message) { message_flags.save_collapsed(message); - const process_row = function process_row($row) { + const process_row = function process_row($row: JQuery): void { $row.find(".message_content").addClass("collapsed"); show_more_link($row); }; @@ -101,7 +102,7 @@ export function collapse(message) { } } -export function toggle_collapse(message) { +export function toggle_collapse(message: Message): void { if (message.is_me_message) { // Disabled temporarily because /me messages don't have a // styling for collapsing /me messages (they only recently @@ -146,38 +147,38 @@ export function toggle_collapse(message) { } } -function get_message_height(elem) { +function get_message_height(elem: HTMLElement): number { // This needs to be very fast. This function runs hundreds of times // when displaying a message feed view that has hundreds of message // history, which ideally should render in <100ms. return $(elem).find(".message_content")[0].scrollHeight; } -export function hide_message_expander($row) { +export function hide_message_expander($row: JQuery): void { if ($row.find(".could-be-condensed").length !== 0) { $row.find(".message_expander").hide(); } } -export function hide_message_condenser($row) { +export function hide_message_condenser($row: JQuery): void { if ($row.find(".could-be-condensed").length !== 0) { $row.find(".message_condenser").hide(); } } -export function show_message_expander($row) { +export function show_message_expander($row: JQuery): void { if ($row.find(".could-be-condensed").length !== 0) { $row.find(".message_expander").show(); } } -export function show_message_condenser($row) { +export function show_message_condenser($row: JQuery): void { if ($row.find(".could-be-condensed").length !== 0) { $row.find(".message_condenser").show(); } } -export function condense_and_collapse(elems) { +export function condense_and_collapse(elems: JQuery): void { if (message_lists.current === undefined) { return; } @@ -205,7 +206,7 @@ export function condense_and_collapse(elems) { continue; } - const message_height = get_message_height(elem, message.id); + const message_height = get_message_height(elem); rows_to_resize.push({ elem, @@ -257,7 +258,7 @@ export function condense_and_collapse(elems) { } } -export function initialize() { +export function initialize(): void { $("#message_feed_container").on("click", ".message_expander", function (e) { // Expanding a message can mean either uncollapsing or // uncondensing it. @@ -265,6 +266,7 @@ export function initialize() { const id = rows.id($row); assert(message_lists.current !== undefined); const message = message_lists.current.get(id); + assert(message !== undefined); // Focus on the expanded message. message_lists.current.select_id(id); const $content = $row.find(".message_content"); @@ -288,8 +290,9 @@ export function initialize() { // Focus on the condensed message. assert(message_lists.current !== undefined); message_lists.current.select_id(id); - message_lists.current.get(id).condensed = true; - condense_row($row); + const message = message_lists.current.get(id); + assert(message !== undefined); + message.condensed = true; e.stopPropagation(); e.preventDefault(); }); diff --git a/web/src/message_store.ts b/web/src/message_store.ts index ad371c4ada..c4a0858518 100644 --- a/web/src/message_store.ts +++ b/web/src/message_store.ts @@ -86,6 +86,7 @@ export type MessageWithBooleans = ( stream_wildcard_mentioned: boolean; topic_wildcard_mentioned: boolean; collapsed: boolean; + condensed?: boolean; alerted: boolean; };