diff --git a/web/src/compose_actions.js b/web/src/compose_actions.js index ae95c06271..badf4446b2 100644 --- a/web/src/compose_actions.js +++ b/web/src/compose_actions.js @@ -58,12 +58,15 @@ function hide_box() { $("#compose_controls").show(); } -function show_compose_box(msg_type, opts) { - compose_recipient.update_compose_for_message_type(msg_type, opts); +function show_compose_box(message_type, opts) { + compose_recipient.update_compose_for_message_type(message_type, opts); $("#compose").css({visibility: "visible"}); // When changing this, edit the 42px in _maybe_autoscroll $(".new_message_textarea").css("min-height", "3em"); - compose_ui.set_focus(msg_type, opts); + compose_ui.set_focus({ + ...opts, + message_type, + }); } export function clear_textarea() { diff --git a/web/src/compose_recipient.js b/web/src/compose_recipient.js index 1a35c76fc9..7b2080c66f 100644 --- a/web/src/compose_recipient.js +++ b/web/src/compose_recipient.js @@ -148,7 +148,7 @@ function switch_message_type(message_type) { }; update_compose_for_message_type(message_type, opts); update_placeholder_text(); - compose_ui.set_focus(message_type, opts); + compose_ui.set_focus(opts); } function update_recipient_label(stream_id) { diff --git a/web/src/compose_ui.ts b/web/src/compose_ui.ts index d008427c35..2a9e97de61 100644 --- a/web/src/compose_ui.ts +++ b/web/src/compose_ui.ts @@ -22,14 +22,20 @@ import * as stream_data from "./stream_data"; import * as user_status from "./user_status"; import * as util from "./util"; -// TODO: Refactor to push this into a field of ComposeTriggeredOptions. type MessageType = "stream" | "private"; type ComposeTriggeredOptions = { trigger: string; - private_message_recipient: string; - topic: string; - stream_id: number; -}; +} & ( + | { + message_type: "stream"; + topic: string; + stream_id: number; + } + | { + message_type: "private"; + private_message_recipient: string; + } +); type ComposePlaceholderOptions = { direct_message_user_ids: number[]; message_type: MessageType; @@ -87,14 +93,14 @@ export function insert_and_scroll_into_view( autosize_textarea($textarea); } -function get_focus_area(msg_type: MessageType, opts: ComposeTriggeredOptions): string { +function get_focus_area(opts: ComposeTriggeredOptions): string { // Set focus to "Topic" when narrowed to a stream+topic // and "Start new conversation" button clicked. - if (msg_type === "stream" && opts.stream_id && !opts.topic) { + if (opts.message_type === "stream" && opts.stream_id && !opts.topic) { return "input#stream_message_recipient_topic"; } else if ( - (msg_type === "stream" && opts.stream_id) || - (msg_type === "private" && opts.private_message_recipient) + (opts.message_type === "stream" && opts.stream_id) || + (opts.message_type === "private" && opts.private_message_recipient) ) { if (opts.trigger === "clear topic button") { return "input#stream_message_recipient_topic"; @@ -102,7 +108,7 @@ function get_focus_area(msg_type: MessageType, opts: ComposeTriggeredOptions): s return "textarea#compose-textarea"; } - if (msg_type === "stream") { + if (opts.message_type === "stream") { return "#compose_select_recipient_widget_wrapper"; } return "#private_message_recipient"; @@ -111,12 +117,12 @@ function get_focus_area(msg_type: MessageType, opts: ComposeTriggeredOptions): s // Export for testing export const _get_focus_area = get_focus_area; -export function set_focus(msg_type: MessageType, opts: ComposeTriggeredOptions): void { +export function set_focus(opts: ComposeTriggeredOptions): void { // Called mainly when opening the compose box or switching the // message type to set the focus in the first empty input in the // compose box. if (window.getSelection()!.toString() === "" || opts.trigger !== "message click") { - const focus_area = get_focus_area(msg_type, opts); + const focus_area = get_focus_area(opts); $(focus_area).trigger("focus"); } } diff --git a/web/tests/compose_ui.test.js b/web/tests/compose_ui.test.js index 5647809a3d..a9f20c9ddf 100644 --- a/web/tests/compose_ui.test.js +++ b/web/tests/compose_ui.test.js @@ -1250,24 +1250,29 @@ run_test("right-to-left", () => { const get_focus_area = compose_ui._get_focus_area; run_test("get_focus_area", () => { - assert.equal(get_focus_area("private", {}), "#private_message_recipient"); + assert.equal(get_focus_area({message_type: "private"}), "#private_message_recipient"); assert.equal( - get_focus_area("private", { + get_focus_area({ + message_type: "private", private_message_recipient: "bob@example.com", }), "textarea#compose-textarea", ); - assert.equal(get_focus_area("stream", {}), "#compose_select_recipient_widget_wrapper"); assert.equal( - get_focus_area("stream", {stream_name: "fun", stream_id: 4}), + get_focus_area({message_type: "stream"}), + "#compose_select_recipient_widget_wrapper", + ); + assert.equal( + get_focus_area({message_type: "stream", stream_name: "fun", stream_id: 4}), "input#stream_message_recipient_topic", ); assert.equal( - get_focus_area("stream", {stream_name: "fun", stream_id: 4, topic: "more"}), + get_focus_area({message_type: "stream", stream_name: "fun", stream_id: 4, topic: "more"}), "textarea#compose-textarea", ); assert.equal( - get_focus_area("stream", { + get_focus_area({ + message_type: "stream", stream_id: 4, topic: "more", trigger: "clear topic button",