compose_ui: Add message_type to ComposeTriggeredOptions.

This commit is contained in:
evykassirer
2024-04-01 15:09:37 -07:00
committed by Tim Abbott
parent 86979b98c7
commit 249a3935ac
4 changed files with 36 additions and 22 deletions

View File

@@ -58,12 +58,15 @@ function hide_box() {
$("#compose_controls").show(); $("#compose_controls").show();
} }
function show_compose_box(msg_type, opts) { function show_compose_box(message_type, opts) {
compose_recipient.update_compose_for_message_type(msg_type, opts); compose_recipient.update_compose_for_message_type(message_type, opts);
$("#compose").css({visibility: "visible"}); $("#compose").css({visibility: "visible"});
// When changing this, edit the 42px in _maybe_autoscroll // When changing this, edit the 42px in _maybe_autoscroll
$(".new_message_textarea").css("min-height", "3em"); $(".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() { export function clear_textarea() {

View File

@@ -148,7 +148,7 @@ function switch_message_type(message_type) {
}; };
update_compose_for_message_type(message_type, opts); update_compose_for_message_type(message_type, opts);
update_placeholder_text(); update_placeholder_text();
compose_ui.set_focus(message_type, opts); compose_ui.set_focus(opts);
} }
function update_recipient_label(stream_id) { function update_recipient_label(stream_id) {

View File

@@ -22,14 +22,20 @@ import * as stream_data from "./stream_data";
import * as user_status from "./user_status"; import * as user_status from "./user_status";
import * as util from "./util"; import * as util from "./util";
// TODO: Refactor to push this into a field of ComposeTriggeredOptions.
type MessageType = "stream" | "private"; type MessageType = "stream" | "private";
type ComposeTriggeredOptions = { type ComposeTriggeredOptions = {
trigger: string; trigger: string;
private_message_recipient: string; } & (
| {
message_type: "stream";
topic: string; topic: string;
stream_id: number; stream_id: number;
}; }
| {
message_type: "private";
private_message_recipient: string;
}
);
type ComposePlaceholderOptions = { type ComposePlaceholderOptions = {
direct_message_user_ids: number[]; direct_message_user_ids: number[];
message_type: MessageType; message_type: MessageType;
@@ -87,14 +93,14 @@ export function insert_and_scroll_into_view(
autosize_textarea($textarea); 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 // Set focus to "Topic" when narrowed to a stream+topic
// and "Start new conversation" button clicked. // 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"; return "input#stream_message_recipient_topic";
} else if ( } else if (
(msg_type === "stream" && opts.stream_id) || (opts.message_type === "stream" && opts.stream_id) ||
(msg_type === "private" && opts.private_message_recipient) (opts.message_type === "private" && opts.private_message_recipient)
) { ) {
if (opts.trigger === "clear topic button") { if (opts.trigger === "clear topic button") {
return "input#stream_message_recipient_topic"; return "input#stream_message_recipient_topic";
@@ -102,7 +108,7 @@ function get_focus_area(msg_type: MessageType, opts: ComposeTriggeredOptions): s
return "textarea#compose-textarea"; return "textarea#compose-textarea";
} }
if (msg_type === "stream") { if (opts.message_type === "stream") {
return "#compose_select_recipient_widget_wrapper"; return "#compose_select_recipient_widget_wrapper";
} }
return "#private_message_recipient"; return "#private_message_recipient";
@@ -111,12 +117,12 @@ function get_focus_area(msg_type: MessageType, opts: ComposeTriggeredOptions): s
// Export for testing // Export for testing
export const _get_focus_area = get_focus_area; 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 // Called mainly when opening the compose box or switching the
// message type to set the focus in the first empty input in the // message type to set the focus in the first empty input in the
// compose box. // compose box.
if (window.getSelection()!.toString() === "" || opts.trigger !== "message click") { 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"); $(focus_area).trigger("focus");
} }
} }

View File

@@ -1250,24 +1250,29 @@ run_test("right-to-left", () => {
const get_focus_area = compose_ui._get_focus_area; const get_focus_area = compose_ui._get_focus_area;
run_test("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( assert.equal(
get_focus_area("private", { get_focus_area({
message_type: "private",
private_message_recipient: "bob@example.com", private_message_recipient: "bob@example.com",
}), }),
"textarea#compose-textarea", "textarea#compose-textarea",
); );
assert.equal(get_focus_area("stream", {}), "#compose_select_recipient_widget_wrapper");
assert.equal( 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", "input#stream_message_recipient_topic",
); );
assert.equal( 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", "textarea#compose-textarea",
); );
assert.equal( assert.equal(
get_focus_area("stream", { get_focus_area({
message_type: "stream",
stream_id: 4, stream_id: 4,
topic: "more", topic: "more",
trigger: "clear topic button", trigger: "clear topic button",