js: Convert static/js/compose_actions.js to ES6 module.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2021-02-28 12:32:22 -08:00
committed by Tim Abbott
parent e74598da17
commit 0e90f3be6d
22 changed files with 111 additions and 97 deletions

View File

@@ -127,7 +127,6 @@
"globals": {
"$": false,
"blueslip": false,
"compose_actions": false,
"composebox_typeahead": false,
"csrf_token": false,
"current_msg_list": true,

View File

@@ -20,7 +20,7 @@ set_global("DOMParser", new JSDOM().window.DOMParser);
let compose_actions_start_checked;
let compose_actions_expected_opts;
set_global("compose_actions", {
rewiremock("../../static/js/compose_actions").with({
update_placeholder_text: noop,
start(msg_type, opts) {
assert.equal(msg_type, "stream");

View File

@@ -78,7 +78,7 @@ const compose_state = zrequire("compose_state");
const compose_actions = zrequire("compose_actions");
const stream_data = zrequire("stream_data");
compose_actions.update_placeholder_text = noop;
compose_actions.__Rewire__("update_placeholder_text", noop);
const start = compose_actions.start;
const cancel = compose_actions.cancel;
@@ -128,12 +128,12 @@ run_test("initial_state", () => {
});
run_test("start", (override) => {
compose_actions.autosize_message_content = noop;
compose_actions.expand_compose_box = noop;
compose_actions.set_focus = noop;
compose_actions.complete_starting_tasks = noop;
compose_actions.blur_compose_inputs = noop;
compose_actions.clear_textarea = noop;
compose_actions.__Rewire__("autosize_message_content", noop);
compose_actions.__Rewire__("expand_compose_box", noop);
compose_actions.__Rewire__("set_focus", noop);
compose_actions.__Rewire__("complete_starting_tasks", noop);
compose_actions.__Rewire__("blur_compose_inputs", noop);
compose_actions.__Rewire__("clear_textarea", noop);
let compose_defaults;
override(narrow_state, "set_compose_defaults", () => compose_defaults);
@@ -445,18 +445,18 @@ run_test("on_narrow", (override) => {
override(compose_state, "has_message_content", () => has_message_content);
let cancel_called = false;
compose_actions.cancel = () => {
compose_actions.__Rewire__("cancel", () => {
cancel_called = true;
};
});
compose_actions.on_narrow({
force_close: true,
});
assert(cancel_called);
let on_topic_narrow_called = false;
compose_actions.on_topic_narrow = () => {
compose_actions.__Rewire__("on_topic_narrow", () => {
on_topic_narrow_called = true;
};
});
narrowed_by_topic_reply = true;
compose_actions.on_narrow({
force_close: false,
@@ -476,9 +476,9 @@ run_test("on_narrow", (override) => {
has_message_content = false;
let start_called = false;
compose_actions.start = () => {
compose_actions.__Rewire__("start", () => {
start_called = true;
};
});
narrowed_by_pm_reply = true;
compose_actions.on_narrow({
force_close: false,

View File

@@ -2,15 +2,21 @@
const {strict: assert} = require("assert");
const {set_global, zrequire} = require("../zjsunit/namespace");
const rewiremock = require("rewiremock/node");
const {zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const $ = require("../zjsunit/zjquery");
rewiremock.enable();
const people = zrequire("people");
const compose_pm_pill = zrequire("compose_pm_pill");
const input_pill = zrequire("input_pill");
const compose_actions = set_global("compose_actions", {});
const compose_actions = {__esModule: true};
rewiremock("../../static/js/compose_actions").with(compose_actions);
let pills = {
pill: {},
@@ -193,3 +199,4 @@ run_test("has_unconverted_data", () => {
// we have some unconverted data.
assert.equal(compose_pm_pill.has_unconverted_data(), true);
});
rewiremock.disable();

View File

@@ -75,7 +75,8 @@ const page_params = set_global("page_params", {});
// jQuery stuff should go away if we make an initialize() method.
set_global("document", "document-stub");
const compose_actions = set_global("compose_actions", {});
const compose_actions = {__esModule: true};
rewiremock("../../static/js/compose_actions").with(compose_actions);
const condense = {__esModule: true};
rewiremock("../../static/js/condense").with(condense);
const drafts = {__esModule: true};

View File

@@ -15,7 +15,8 @@ const channel = {__esModule: true};
rewiremock("../../static/js/channel").with(channel);
const compose = {__esModule: true};
rewiremock("../../static/js/compose").with(compose);
const compose_actions = set_global("compose_actions", {});
const compose_actions = {__esModule: true};
rewiremock("../../static/js/compose_actions").with(compose_actions);
set_global("current_msg_list", {});
const hashchange = {__esModule: true};
rewiremock("../../static/js/hashchange").with(hashchange);

View File

@@ -16,7 +16,7 @@ rewiremock("../../static/js/top_left_corner").with({
rewiremock("../../static/js/stream_list").with({
handle_narrow_deactivated: noop,
});
set_global("compose_actions", {
rewiremock("../../static/js/compose_actions").with({
cancel: noop,
});
rewiremock("../../static/js/narrow").with({

View File

@@ -509,9 +509,9 @@ run_test("uppy_events", () => {
},
};
let compose_actions_start_called = false;
compose_actions.start = () => {
compose_actions.__Rewire__("start", () => {
compose_actions_start_called = true;
};
});
let compose_ui_replace_syntax_called = false;
compose_ui.__Rewire__("replace_syntax", (old_syntax, new_syntax, textarea) => {
compose_ui_replace_syntax_called = true;

View File

@@ -18,7 +18,6 @@ import "../fold_dict";
import "../setup";
import "../message_list";
import "../reload";
import "../compose_actions";
import "../subs";
import "../ui";
import "../composebox_typeahead";

View File

@@ -10,6 +10,7 @@ import * as activity from "./activity";
import * as buddy_data from "./buddy_data";
import * as channel from "./channel";
import * as compose from "./compose";
import * as compose_actions from "./compose_actions";
import * as compose_state from "./compose_state";
import * as emoji_picker from "./emoji_picker";
import * as hash_util from "./hash_util";

View File

@@ -9,6 +9,7 @@ import render_compose_private_stream_alert from "../templates/compose_private_st
import * as channel from "./channel";
import * as common from "./common";
import * as compose_actions from "./compose_actions";
import * as compose_fade from "./compose_fade";
import * as compose_pm_pill from "./compose_pm_pill";
import * as compose_state from "./compose_state";

View File

@@ -1,33 +1,31 @@
"use strict";
import autosize from "autosize";
const autosize = require("autosize");
import * as fenced_code from "../shared/js/fenced_code";
const fenced_code = require("../shared/js/fenced_code");
import * as channel from "./channel";
import * as common from "./common";
import * as compose from "./compose";
import * as compose_fade from "./compose_fade";
import * as compose_pm_pill from "./compose_pm_pill";
import * as compose_state from "./compose_state";
import * as compose_ui from "./compose_ui";
import * as drafts from "./drafts";
import * as hash_util from "./hash_util";
import * as message_viewport from "./message_viewport";
import * as narrow_state from "./narrow_state";
import * as notifications from "./notifications";
import * as people from "./people";
import * as reload_state from "./reload_state";
import * as stream_data from "./stream_data";
import * as ui_util from "./ui_util";
import * as unread_ops from "./unread_ops";
const channel = require("./channel");
const common = require("./common");
const compose = require("./compose");
const compose_fade = require("./compose_fade");
const compose_pm_pill = require("./compose_pm_pill");
const compose_state = require("./compose_state");
const compose_ui = require("./compose_ui");
const drafts = require("./drafts");
const hash_util = require("./hash_util");
const message_viewport = require("./message_viewport");
const narrow_state = require("./narrow_state");
const notifications = require("./notifications");
const people = require("./people");
const reload_state = require("./reload_state");
const stream_data = require("./stream_data");
const ui_util = require("./ui_util");
const unread_ops = require("./unread_ops");
exports.blur_compose_inputs = function () {
export function blur_compose_inputs() {
$(".message_comp").find("input, textarea, button, #private_message_recipient").trigger("blur");
};
}
function hide_box() {
exports.blur_compose_inputs();
blur_compose_inputs();
$("#stream-message").hide();
$("#private-message").hide();
$(".new_message_textarea").css("min-height", "");
@@ -56,10 +54,11 @@ function get_focus_area(msg_type, opts) {
}
return "#private_message_recipient";
}
// Export for testing
exports._get_focus_area = get_focus_area;
exports.set_focus = function (msg_type, opts) {
// Export for testing
export const _get_focus_area = get_focus_area;
export function set_focus(msg_type, opts) {
const focus_area = get_focus_area(msg_type, opts);
if (focus_area === undefined) {
return;
@@ -69,7 +68,7 @@ exports.set_focus = function (msg_type, opts) {
const elt = $(focus_area);
elt.trigger("focus").trigger("select");
}
};
}
// Show the compose box.
function show_box(msg_type, opts) {
@@ -89,12 +88,12 @@ function show_box(msg_type, opts) {
// When changing this, edit the 42px in _maybe_autoscroll
$(".new_message_textarea").css("min-height", "3em");
exports.set_focus(msg_type, opts);
set_focus(msg_type, opts);
}
exports.clear_textarea = function () {
export function clear_textarea() {
$("#compose").find("input[type=text], textarea").val("");
};
}
function clear_box() {
compose.clear_invites();
@@ -106,40 +105,40 @@ function clear_box() {
compose.reset_user_acknowledged_all_everyone_flag();
compose.reset_user_acknowledged_announce_flag();
exports.clear_textarea();
clear_textarea();
$("#compose-textarea").removeData("draft-id");
compose_ui.autosize_textarea($("#compose-textarea"));
$("#compose-send-status").hide(0);
}
exports.autosize_message_content = function () {
export function autosize_message_content() {
autosize($("#compose-textarea"), {
callback() {
exports.maybe_scroll_up_selected_message();
maybe_scroll_up_selected_message();
},
});
};
}
exports.expand_compose_box = function () {
export function expand_compose_box() {
$("#compose_close").show();
$("#compose_controls").hide();
$(".message_comp").show();
};
}
exports.complete_starting_tasks = function (msg_type, opts) {
export function complete_starting_tasks(msg_type, opts) {
// This is sort of a kitchen sink function, and it's called only
// by compose.start() for now. Having this as a separate function
// makes testing a bit easier.
exports.maybe_scroll_up_selected_message();
maybe_scroll_up_selected_message();
ui_util.change_tab_to("#message_feed_container");
compose_fade.start_compose(msg_type);
ui_util.decorate_stream_bar(opts.stream, $("#stream-message .message_header_stream"), true);
$(document).trigger(new $.Event("compose_started.zulip", opts));
exports.update_placeholder_text();
};
update_placeholder_text();
}
exports.maybe_scroll_up_selected_message = function () {
export function maybe_scroll_up_selected_message() {
// If the compose box is obscuring the currently selected message,
// scroll up until the message is no longer occluded.
if (current_msg_list.selected_id() === -1) {
@@ -161,7 +160,7 @@ exports.maybe_scroll_up_selected_message = function () {
if (cover > 0) {
message_viewport.user_initiated_animate_scroll(cover + 20);
}
};
}
function fill_in_opts_from_current_narrowed_view(msg_type, opts) {
return {
@@ -189,7 +188,7 @@ function same_recipient_as_before(msg_type, opts) {
);
}
exports.update_placeholder_text = function () {
export function update_placeholder_text() {
// Change compose placeholder text only if compose box is open.
if (!$("#compose-textarea").is(":visible")) {
return;
@@ -203,16 +202,16 @@ exports.update_placeholder_text = function () {
};
$("#compose-textarea").attr("placeholder", compose_ui.compute_placeholder_text(opts));
};
}
exports.start = function (msg_type, opts) {
exports.autosize_message_content();
export function start(msg_type, opts) {
autosize_message_content();
if (reload_state.is_in_progress()) {
return;
}
notifications.clear_compose_notifications();
exports.expand_compose_box();
expand_compose_box();
opts = fill_in_opts_from_current_narrowed_view(msg_type, opts);
// If we are invoked by a compose hotkey (c or x) or new topic
@@ -261,10 +260,10 @@ exports.start = function (msg_type, opts) {
// Show either stream/topic fields or "You and" field.
show_box(msg_type, opts);
exports.complete_starting_tasks(msg_type, opts);
};
complete_starting_tasks(msg_type, opts);
}
exports.cancel = function () {
export function cancel() {
$("#compose-textarea").height(40 + "px");
if (page_params.narrow !== undefined) {
@@ -287,9 +286,9 @@ exports.cancel = function () {
compose_state.set_message_type(false);
compose_pm_pill.clear();
$(document).trigger("compose_canceled.zulip");
};
}
exports.respond_to_message = function (opts) {
export function respond_to_message(opts) {
let msg_type;
// Before initiating a reply to a message, if there's an
// in-progress composition, snapshot it.
@@ -325,7 +324,7 @@ exports.respond_to_message = function (opts) {
}
const new_opts = fill_in_opts_from_current_narrowed_view(msg_type, opts);
exports.start(new_opts.message_type, new_opts);
start(new_opts.message_type, new_opts);
return;
}
@@ -360,22 +359,22 @@ exports.respond_to_message = function (opts) {
}
}
exports.start(msg_type, {
start(msg_type, {
stream,
topic,
private_message_recipient: pm_recipient,
trigger: opts.trigger,
});
};
}
exports.reply_with_mention = function (opts) {
exports.respond_to_message(opts);
export function reply_with_mention(opts) {
respond_to_message(opts);
const message = current_msg_list.selected_message();
const mention = people.get_mention_syntax(message.sender_full_name, message.sender_id);
compose_ui.insert_syntax_and_focus(mention);
};
}
exports.on_topic_narrow = function () {
export function on_topic_narrow() {
if (!compose_state.composing()) {
// If our compose box is closed, then just
// leave it closed, assuming that the user is
@@ -393,7 +392,7 @@ exports.on_topic_narrow = function () {
}
// Otherwise, avoid a mix.
exports.cancel();
cancel();
return;
}
@@ -418,9 +417,9 @@ exports.on_topic_narrow = function () {
compose_fade.set_focused_recipient("stream");
compose_fade.update_message_list();
$("#compose-textarea").trigger("focus").trigger("select");
};
}
exports.quote_and_reply = function (opts) {
export function quote_and_reply(opts) {
const textarea = $("#compose-textarea");
const message_id = current_msg_list.selected_id();
const message = current_msg_list.selected_message();
@@ -444,7 +443,7 @@ exports.quote_and_reply = function (opts) {
// smarter about newlines.
textarea.caret(0);
} else {
exports.respond_to_message(opts);
respond_to_message(opts);
}
compose_ui.insert_syntax_and_focus("[Quoting…]\n", textarea);
@@ -476,16 +475,16 @@ exports.quote_and_reply = function (opts) {
replace_content(message);
},
});
};
}
exports.on_narrow = function (opts) {
export function on_narrow(opts) {
// We use force_close when jumping between PM narrows with the "p" key,
// so that we don't have an open compose box that makes it difficult
// to cycle quickly through unread messages.
if (opts.force_close) {
// This closes the compose box if it was already open, and it is
// basically a noop otherwise.
exports.cancel();
cancel();
return;
}
@@ -495,7 +494,7 @@ exports.on_narrow = function (opts) {
}
if (narrow_state.narrowed_by_topic_reply()) {
exports.on_topic_narrow();
on_topic_narrow();
return;
}
@@ -511,14 +510,12 @@ exports.on_narrow = function (opts) {
if (opts.trigger === "search" && !opts.private_message_recipient) {
return;
}
exports.start("private");
start("private");
return;
}
// If we got this far, then we assume the user is now in "reading"
// mode, so we close the compose box to make it easier to use navigation
// hotkeys and to provide more screen real estate for messages.
exports.cancel();
};
window.compose_actions = exports;
cancel();
}

View File

@@ -1,3 +1,4 @@
import * as compose_actions from "./compose_actions";
import * as input_pill from "./input_pill";
import * as people from "./people";
import * as user_pill from "./user_pill";

View File

@@ -4,6 +4,7 @@ import Handlebars from "handlebars/runtime";
import render_draft_table_body from "../templates/draft_table_body.hbs";
import * as compose from "./compose";
import * as compose_actions from "./compose_actions";
import * as compose_fade from "./compose_fade";
import * as compose_state from "./compose_state";
import * as compose_ui from "./compose_ui";

View File

@@ -4,7 +4,6 @@
// to TS.
declare let blueslip: any;
declare let compose_actions: any;
declare let composebox_typeahead: any;
declare let csrf_token: any;
declare let current_msg_list: any;

View File

@@ -3,6 +3,7 @@ import * as emoji from "../shared/js/emoji";
import * as activity from "./activity";
import * as common from "./common";
import * as compose from "./compose";
import * as compose_actions from "./compose_actions";
import * as compose_state from "./compose_state";
import * as condense from "./condense";
import * as copy_and_paste from "./copy_and_paste";

View File

@@ -5,6 +5,7 @@ import render_topic_edit_form from "../templates/topic_edit_form.hbs";
import * as channel from "./channel";
import * as compose from "./compose";
import * as compose_actions from "./compose_actions";
import * as condense from "./condense";
import * as echo from "./echo";
import * as loading from "./loading";

View File

@@ -1,5 +1,6 @@
import * as channel from "./channel";
import * as compose from "./compose";
import * as compose_actions from "./compose_actions";
import * as compose_fade from "./compose_fade";
import * as compose_state from "./compose_state";
import * as condense from "./condense";

View File

@@ -15,6 +15,7 @@ import render_user_info_popover_title from "../templates/user_info_popover_title
import render_user_profile_modal from "../templates/user_profile_modal.hbs";
import * as buddy_data from "./buddy_data";
import * as compose_actions from "./compose_actions";
import * as compose_state from "./compose_state";
import * as compose_ui from "./compose_ui";
import * as condense from "./condense";

View File

@@ -2,6 +2,7 @@ import render_recent_topic_row from "../templates/recent_topic_row.hbs";
import render_recent_topics_filters from "../templates/recent_topics_filters.hbs";
import render_recent_topics_body from "../templates/recent_topics_table.hbs";
import * as compose_actions from "./compose_actions";
import * as drafts from "./drafts";
import * as hash_util from "./hash_util";
import * as ListWidget from "./list_widget";

View File

@@ -1,5 +1,6 @@
import * as activity from "./activity";
import * as compose from "./compose";
import * as compose_actions from "./compose_actions";
import * as compose_state from "./compose_state";
import * as hashchange from "./hashchange";
import {localstorage} from "./localstorage";

View File

@@ -3,6 +3,7 @@ import ProgressBar from "@uppy/progress-bar";
import XHRUpload from "@uppy/xhr-upload";
import * as compose from "./compose";
import * as compose_actions from "./compose_actions";
import * as compose_state from "./compose_state";
import * as compose_ui from "./compose_ui";