compose_typeahead: Cut dependency on `compose.js`.

This commit is contained in:
Lalit 2023-06-18 12:39:19 +05:30 committed by Tim Abbott
parent e1ef8c623c
commit 6ade7dcfb9
3 changed files with 18 additions and 12 deletions

View File

@ -4,7 +4,6 @@ import _ from "lodash";
import * as typeahead from "../shared/src/typeahead";
import render_topic_typeahead_hint from "../templates/topic_typeahead_hint.hbs";
import * as compose from "./compose";
import * as compose_pm_pill from "./compose_pm_pill";
import * as compose_state from "./compose_state";
import * as compose_ui from "./compose_ui";
@ -193,7 +192,7 @@ export function handle_enter($textarea, e) {
// has reliable information about whether it was a Tab or a Shift+Tab.
let $nextFocus = false;
function handle_keydown(e) {
function handle_keydown(e, {on_enter_send}) {
const key = e.key;
if (keydown_util.is_enter_event(e) || (key === "Tab" && !e.shiftKey)) {
@ -234,7 +233,7 @@ function handle_keydown(e) {
compose_validate.validate_message_length() &&
!$("#compose-send-button").prop("disabled")
) {
compose.finish();
on_enter_send();
}
return;
}
@ -1077,11 +1076,11 @@ export function initialize_compose_typeahead(selector) {
});
}
export function initialize() {
export function initialize({on_enter_send}) {
update_emoji_data();
// These handlers are at the "form" level so that they are called after typeahead
$("form#send_message_form").on("keydown", handle_keydown);
$("form#send_message_form").on("keydown", (e) => handle_keydown(e, {on_enter_send}));
$("form#send_message_form").on("keyup", handle_keyup);
$("#stream_message_recipient_topic").typeahead({

View File

@ -714,7 +714,10 @@ export function initialize_everything() {
linkifiers.initialize(page_params.realm_linkifiers);
realm_playground.initialize(page_params.realm_playgrounds, generated_pygments_data);
compose.initialize();
composebox_typeahead.initialize(); // Must happen after compose.initialize()
// Typeahead must be initialized after compose.initialize()
composebox_typeahead.initialize({
on_enter_send: compose.finish,
});
compose_textarea.initialize();
search.initialize();
tutorial.initialize();

View File

@ -1112,8 +1112,14 @@ test("initialize", ({override, override_rewire, mock_template}) => {
};
user_settings.enter_sends = false;
let compose_finish_called = false;
override_rewire(compose, "finish", () => {
compose_finish_called = true;
});
ct.initialize();
ct.initialize({
on_enter_send: compose.finish,
});
$("#private_message_recipient").val("othello@zulip.com, ");
$("#private_message_recipient").trigger("blur");
@ -1155,10 +1161,6 @@ test("initialize", ({override, override_rewire, mock_template}) => {
event.target.id = "compose-textarea";
user_settings.enter_sends = false;
event.metaKey = true;
let compose_finish_called = false;
override_rewire(compose, "finish", () => {
compose_finish_called = true;
});
$("form#send_message_form").trigger(event);
assert.ok(compose_finish_called);
@ -1211,7 +1213,9 @@ test("initialize", ({override, override_rewire, mock_template}) => {
$("form#send_message_form").off("keyup");
$("#private_message_recipient").off("blur");
$("#send_later").css = noop;
ct.initialize();
ct.initialize({
on_enter_send: compose.finish,
});
// Now let's make sure that all the stub functions have been called
// during the initialization.