Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
js: Convert static/js/compose_actions.js to ES6 module.
Anders Kaseorg0e90f3be6dae
diff --git a/.eslintrc.json b/.eslintrc.jsonindex e95a37ee8c3..1da06d8096c 100644--- a/.eslintrc.json+++ b/.eslintrc.json@@ -127,7 +127,6 @@"globals": {"$": false,"blueslip": false,- "compose_actions": false,"composebox_typeahead": false,"csrf_token": false,"current_msg_list": true,diff --git a/frontend_tests/node_tests/compose.js b/frontend_tests/node_tests/compose.jsindex a53ff0013b9..c397eea1746 100644--- a/frontend_tests/node_tests/compose.js+++ b/frontend_tests/node_tests/compose.js@@ -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");diff --git a/frontend_tests/node_tests/compose_actions.js b/frontend_tests/node_tests/compose_actions.jsindex 0c2fcc86a73..c10754a86fe 100644--- a/frontend_tests/node_tests/compose_actions.js+++ b/frontend_tests/node_tests/compose_actions.js@@ -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,diff --git a/frontend_tests/node_tests/compose_pm_pill.js b/frontend_tests/node_tests/compose_pm_pill.jsindex 7e757b42482..0852c0f5dc5 100644--- a/frontend_tests/node_tests/compose_pm_pill.js+++ b/frontend_tests/node_tests/compose_pm_pill.js@@ -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();diff --git a/frontend_tests/node_tests/hotkey.js b/frontend_tests/node_tests/hotkey.jsindex 35e994984e4..9c41943d71d 100644--- a/frontend_tests/node_tests/hotkey.js+++ b/frontend_tests/node_tests/hotkey.js@@ -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};diff --git a/frontend_tests/node_tests/narrow_activate.js b/frontend_tests/node_tests/narrow_activate.jsindex 718b3a823fd..b3f57eab6f1 100644--- a/frontend_tests/node_tests/narrow_activate.js+++ b/frontend_tests/node_tests/narrow_activate.js@@ -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);diff --git a/frontend_tests/node_tests/recent_topics.js b/frontend_tests/node_tests/recent_topics.jsindex 36e27e49dec..b301a9e7a4e 100644--- a/frontend_tests/node_tests/recent_topics.js+++ b/frontend_tests/node_tests/recent_topics.js@@ -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({diff --git a/frontend_tests/node_tests/upload.js b/frontend_tests/node_tests/upload.jsindex 9aed9e45db0..9b001d527db 100644--- a/frontend_tests/node_tests/upload.js+++ b/frontend_tests/node_tests/upload.js@@ -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;diff --git a/static/js/bundles/app.js b/static/js/bundles/app.jsindex 6cdc0aa77b9..ee30894d223 100644--- a/static/js/bundles/app.js+++ b/static/js/bundles/app.js@@ -18,7 +18,6 @@ import "../fold_dict";import "../setup";import "../message_list";import "../reload";-import "../compose_actions";import "../subs";import "../ui";import "../composebox_typeahead";diff --git a/static/js/click_handlers.js b/static/js/click_handlers.jsindex d2904e5c8b3..e5d414698e9 100644--- a/static/js/click_handlers.js+++ b/static/js/click_handlers.js@@ -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";diff --git a/static/js/compose.js b/static/js/compose.jsindex 012386bb65c..fccc7fb6279 100644--- a/static/js/compose.js+++ b/static/js/compose.js@@ -9,6 +9,7 @@ import render_compose_private_stream_alert from "../templates/compose_private_stimport * 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";diff --git a/static/js/compose_actions.js b/static/js/compose_actions.jsindex 3108c69b0d1..976549496f4 100644--- a/static/js/compose_actions.js+++ b/static/js/compose_actions.js@@ -1,33 +1,31 @@-"use strict";--const autosize = require("autosize");--const fenced_code = require("../shared/js/fenced_code");--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 () {+import autosize from "autosize";++import * as fenced_code from "../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";++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;+export const _get_focus_area = get_focus_area;-exports.set_focus = function (msg_type, opts) {+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();+}diff --git a/static/js/compose_pm_pill.js b/static/js/compose_pm_pill.jsindex 16c2c3bdadd..d7017d79c24 100644--- a/static/js/compose_pm_pill.js+++ b/static/js/compose_pm_pill.js@@ -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";diff --git a/static/js/drafts.js b/static/js/drafts.jsindex 437b8392ccd..3079b263c78 100644--- a/static/js/drafts.js+++ b/static/js/drafts.js@@ -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";diff --git a/static/js/global.d.ts b/static/js/global.d.tsindex 2378627f884..fbb1f8a1838 100644--- a/static/js/global.d.ts+++ b/static/js/global.d.ts@@ -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;diff --git a/static/js/hotkey.js b/static/js/hotkey.jsindex 981bbf0b8ec..40dc5121eff 100644--- a/static/js/hotkey.js+++ b/static/js/hotkey.js@@ -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";diff --git a/static/js/message_edit.js b/static/js/message_edit.jsindex 2d2b1f4285e..8c1b567d0bb 100644--- a/static/js/message_edit.js+++ b/static/js/message_edit.js@@ -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";diff --git a/static/js/narrow.js b/static/js/narrow.jsindex 5bdd2c59280..1758b672ef0 100644--- a/static/js/narrow.js+++ b/static/js/narrow.js@@ -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";diff --git a/static/js/popovers.js b/static/js/popovers.jsindex 5ce737ac1f5..d7051d8910d 100644--- a/static/js/popovers.js+++ b/static/js/popovers.js@@ -15,6 +15,7 @@ import render_user_info_popover_title from "../templates/user_info_popover_titleimport 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";diff --git a/static/js/recent_topics.js b/static/js/recent_topics.jsindex 3d5943af877..18c692ed640 100644--- a/static/js/recent_topics.js+++ b/static/js/recent_topics.js@@ -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";diff --git a/static/js/reload.js b/static/js/reload.jsindex 3fb46eb420c..548d4643cef 100644--- a/static/js/reload.js+++ b/static/js/reload.js@@ -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";diff --git a/static/js/upload.js b/static/js/upload.jsindex 861ddf5617e..a52e8ac4dd9 100644--- a/static/js/upload.js+++ b/static/js/upload.js@@ -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";