diff --git a/.eslintrc.json b/.eslintrc.json index 30fc5066bd..b5c3d86eea 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -127,7 +127,6 @@ "$": false, "blueslip": false, "compose_actions": false, - "compose_ui": 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.js index 24257f2fe8..4e70487289 100644 --- a/frontend_tests/node_tests/compose.js +++ b/frontend_tests/node_tests/compose.js @@ -1567,9 +1567,9 @@ test_ui("on_events", () => { }, }; - compose_ui.insert_syntax_and_focus = () => { + compose_ui.__Rewire__("insert_syntax_and_focus", () => { called = true; - }; + }); const handler = $("body").get_on_handler("click", ".video_link"); $("#compose-textarea").val(""); @@ -1591,10 +1591,10 @@ test_ui("on_events", () => { }, }; - compose_ui.insert_syntax_and_focus = (syntax) => { + compose_ui.__Rewire__("insert_syntax_and_focus", (syntax) => { syntax_to_insert = syntax; called = true; - }; + }); const handler = $("body").get_on_handler("click", ".video_link"); $("#compose-textarea").val(""); @@ -1627,10 +1627,10 @@ test_ui("on_events", () => { }, }; - compose_ui.insert_syntax_and_focus = (syntax) => { + compose_ui.__Rewire__("insert_syntax_and_focus", (syntax) => { syntax_to_insert = syntax; called = true; - }; + }); const handler = $("body").get_on_handler("click", ".video_link"); $("#compose-textarea").val(""); @@ -1672,10 +1672,10 @@ test_ui("on_events", () => { }, }; - compose_ui.insert_syntax_and_focus = (syntax) => { + compose_ui.__Rewire__("insert_syntax_and_focus", (syntax) => { syntax_to_insert = syntax; called = true; - }; + }); const handler = $("body").get_on_handler("click", ".video_link"); $("#compose-textarea").val(""); diff --git a/frontend_tests/node_tests/compose_actions.js b/frontend_tests/node_tests/compose_actions.js index 64e359b29c..bd7c24b3b3 100644 --- a/frontend_tests/node_tests/compose_actions.js +++ b/frontend_tests/node_tests/compose_actions.js @@ -343,9 +343,9 @@ run_test("quote_and_reply", (override) => { current_msg_list.selected_id = () => 100; - compose_ui.insert_syntax_and_focus = (syntax) => { + compose_ui.__Rewire__("insert_syntax_and_focus", (syntax) => { assert.equal(syntax, "[Quoting…]\n"); - }; + }); const opts = { reply_type: "personal", diff --git a/frontend_tests/node_tests/composebox_typeahead.js b/frontend_tests/node_tests/composebox_typeahead.js index 1acf04216a..ad1c8ee283 100644 --- a/frontend_tests/node_tests/composebox_typeahead.js +++ b/frontend_tests/node_tests/composebox_typeahead.js @@ -32,7 +32,7 @@ const compose = { rewiremock("../../static/js/compose").with(compose); let autosize_called; -set_global("compose_ui", { +rewiremock("../../static/js/compose_ui").with({ autosize_textarea() { autosize_called = true; }, diff --git a/frontend_tests/node_tests/copy_and_paste.js b/frontend_tests/node_tests/copy_and_paste.js index c8523431de..f8ab47325b 100644 --- a/frontend_tests/node_tests/copy_and_paste.js +++ b/frontend_tests/node_tests/copy_and_paste.js @@ -4,6 +4,7 @@ const {strict: assert} = require("assert"); const jquery = require("jquery"); const {JSDOM} = require("jsdom"); +const rewiremock = require("rewiremock/node"); const {set_global, zrequire} = require("../zjsunit/namespace"); const {run_test} = require("../zjsunit/test"); @@ -11,7 +12,9 @@ const {run_test} = require("../zjsunit/test"); set_global("page_params", { development_environment: true, }); -const compose_ui = set_global("compose_ui", {}); +const compose_ui = {__esModule: true}; + +rewiremock("../../static/js/compose_ui").with(compose_ui); const {window} = new JSDOM("

Hello world

"); const {DOMParser, document} = window; @@ -20,6 +23,8 @@ const $ = set_global("$", jquery(window)); set_global("DOMParser", DOMParser); set_global("document", document); +rewiremock.enable(); + const copy_and_paste = zrequire("copy_and_paste"); // Super stripped down version of the code in the drag-mock library @@ -111,3 +116,4 @@ run_test("paste_handler", () => { copy_and_paste.paste_handler(event); assert(!insert_syntax_and_focus_called); }); +rewiremock.disable(); diff --git a/frontend_tests/node_tests/upload.js b/frontend_tests/node_tests/upload.js index 85fe358f89..9aed9e45db 100644 --- a/frontend_tests/node_tests/upload.js +++ b/frontend_tests/node_tests/upload.js @@ -234,15 +234,15 @@ run_test("upload_files", () => { on_click_close_button_callback = callback; }; let compose_ui_insert_syntax_and_focus_called = false; - compose_ui.insert_syntax_and_focus = (syntax, textarea) => { + compose_ui.__Rewire__("insert_syntax_and_focus", (syntax, textarea) => { assert.equal(syntax, "[translated: Uploading budapest.png…]()"); assert.equal(textarea, $("#compose-textarea")); compose_ui_insert_syntax_and_focus_called = true; - }; + }); let compose_ui_autosize_textarea_called = false; - compose_ui.autosize_textarea = () => { + compose_ui.__Rewire__("autosize_textarea", () => { compose_ui_autosize_textarea_called = true; - }; + }); let markdown_preview_hide_button_clicked = false; $("#undo_markdown_preview").on("click", () => { markdown_preview_hide_button_clicked = true; @@ -291,12 +291,12 @@ run_test("upload_files", () => { type: "image/png", }, ]; - compose_ui.replace_syntax = (old_syntax, new_syntax, textarea) => { + compose_ui.__Rewire__("replace_syntax", (old_syntax, new_syntax, textarea) => { compose_ui_replace_syntax_called = true; assert.equal(old_syntax, "[translated: Uploading budapest.png…]()"); assert.equal(new_syntax, ""); assert.equal(textarea, $("#compose-textarea")); - }; + }); on_click_close_button_callback(); assert(uppy_cancel_all_called); assert(hide_upload_status_called); @@ -513,7 +513,7 @@ run_test("uppy_events", () => { compose_actions_start_called = true; }; let compose_ui_replace_syntax_called = false; - compose_ui.replace_syntax = (old_syntax, new_syntax, textarea) => { + compose_ui.__Rewire__("replace_syntax", (old_syntax, new_syntax, textarea) => { compose_ui_replace_syntax_called = true; assert.equal(old_syntax, "[translated: Uploading copenhagen.png…]()"); assert.equal( @@ -521,11 +521,11 @@ run_test("uppy_events", () => { "[copenhagen.png](https://foo.com/user_uploads/4/cb/rue1c-MlMUjDAUdkRrEM4BTJ/copenhagen.png)", ); assert.equal(textarea, $("#compose-textarea")); - }; + }); let compose_ui_autosize_textarea_called = false; - compose_ui.autosize_textarea = () => { + compose_ui.__Rewire__("autosize_textarea", () => { compose_ui_autosize_textarea_called = true; - }; + }); on_upload_success_callback(file, response); assert(compose_actions_start_called); assert(compose_ui_replace_syntax_called); @@ -614,12 +614,12 @@ run_test("uppy_events", () => { on_info_visible_callback(); assert(uppy_cancel_all_called); assert(show_error_message_called); - compose_ui.replace_syntax = (old_syntax, new_syntax, textarea) => { + compose_ui.__Rewire__("replace_syntax", (old_syntax, new_syntax, textarea) => { compose_ui_replace_syntax_called = true; assert.equal(old_syntax, "[translated: Uploading copenhagen.png…]()"); assert.equal(new_syntax, ""); assert.equal(textarea, $("#compose-textarea")); - }; + }); on_restriction_failed_callback(file, null, null); assert(compose_ui_replace_syntax_called); compose_ui_replace_syntax_called = false; diff --git a/static/js/bundles/app.js b/static/js/bundles/app.js index bd93574f31..2d52d77aa6 100644 --- a/static/js/bundles/app.js +++ b/static/js/bundles/app.js @@ -38,7 +38,6 @@ import "../dropdown_list_widget"; import "../settings_profile_fields"; import "../settings"; import "../ui_init"; -import "../compose_ui"; import "../panels"; import "../recent_topics"; import "../search_pill_widget"; diff --git a/static/js/compose.js b/static/js/compose.js index 8bef289b27..012386bb65 100644 --- a/static/js/compose.js +++ b/static/js/compose.js @@ -12,6 +12,7 @@ import * as common from "./common"; 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 echo from "./echo"; import * as loading from "./loading"; diff --git a/static/js/compose_actions.js b/static/js/compose_actions.js index 79fd5ed5e5..1624acb34f 100644 --- a/static/js/compose_actions.js +++ b/static/js/compose_actions.js @@ -10,6 +10,7 @@ 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"); diff --git a/static/js/compose_ui.js b/static/js/compose_ui.js index aeb549deba..4e917bae5c 100644 --- a/static/js/compose_ui.js +++ b/static/js/compose_ui.js @@ -1,17 +1,15 @@ -"use strict"; +import autosize from "autosize"; -const autosize = require("autosize"); +import * as people from "./people"; +import * as user_status from "./user_status"; -const people = require("./people"); -const user_status = require("./user_status"); - -exports.autosize_textarea = function (textarea) { +export function autosize_textarea(textarea) { // Since this supports both compose and file upload, one must pass // in the text area to autosize. autosize.update(textarea); -}; +} -exports.smart_insert = function (textarea, syntax) { +export function smart_insert(textarea, syntax) { function is_space(c) { return c === " " || c === "\t" || c === "\n"; } @@ -54,19 +52,19 @@ exports.smart_insert = function (textarea, syntax) { // This should just call exports.autosize_textarea, but it's a bit // annoying for the unit tests, so we don't do that. autosize.update(textarea); -}; +} -exports.insert_syntax_and_focus = function (syntax, textarea) { +export function insert_syntax_and_focus(syntax, textarea) { // Generic helper for inserting syntax into the main compose box // where the cursor was and focusing the area. Mostly a thin // wrapper around smart_insert. if (textarea === undefined) { textarea = $("#compose-textarea"); } - exports.smart_insert(textarea, syntax); -}; + smart_insert(textarea, syntax); +} -exports.replace_syntax = function (old_syntax, new_syntax, textarea) { +export function replace_syntax(old_syntax, new_syntax, textarea) { // Replaces `old_syntax` with `new_syntax` text in the compose box. Due to // the way that JavaScript handles string replacements, if `old_syntax` is // a string it will only replace the first instance. If `old_syntax` is @@ -87,9 +85,9 @@ exports.replace_syntax = function (old_syntax, new_syntax, textarea) { new_syntax, ), ); -}; +} -exports.compute_placeholder_text = function (opts) { +export function compute_placeholder_text(opts) { // Computes clear placeholder text for the compose box, depending // on what heading values have already been filled out. // @@ -131,6 +129,4 @@ exports.compute_placeholder_text = function (opts) { return i18n.t("Message __- recipient_names__", {recipient_names}); } return i18n.t("Compose your message here"); -}; - -window.compose_ui = exports; +} diff --git a/static/js/composebox_typeahead.js b/static/js/composebox_typeahead.js index 06149816b1..6d60db30a5 100644 --- a/static/js/composebox_typeahead.js +++ b/static/js/composebox_typeahead.js @@ -13,6 +13,7 @@ const channel = require("./channel"); const compose = require("./compose"); const compose_pm_pill = require("./compose_pm_pill"); const compose_state = require("./compose_state"); +const compose_ui = require("./compose_ui"); const message_store = require("./message_store"); const people = require("./people"); const rows = require("./rows"); diff --git a/static/js/copy_and_paste.js b/static/js/copy_and_paste.js index d23b6380ad..f3aef00046 100644 --- a/static/js/copy_and_paste.js +++ b/static/js/copy_and_paste.js @@ -1,5 +1,6 @@ import TurndownService from "turndown/lib/turndown.cjs"; +import * as compose_ui from "./compose_ui"; import * as rows from "./rows"; function find_boundary_tr(initial_tr, iterate_row) { diff --git a/static/js/drafts.js b/static/js/drafts.js index d5c58ab3c9..c8f5164cd3 100644 --- a/static/js/drafts.js +++ b/static/js/drafts.js @@ -6,6 +6,7 @@ import render_draft_table_body from "../templates/draft_table_body.hbs"; import * as compose from "./compose"; import * as compose_fade from "./compose_fade"; import * as compose_state from "./compose_state"; +import * as compose_ui from "./compose_ui"; import * as hashchange from "./hashchange"; import {localstorage} from "./localstorage"; import * as markdown from "./markdown"; diff --git a/static/js/emoji_picker.js b/static/js/emoji_picker.js index 5e72069949..2676c741fb 100644 --- a/static/js/emoji_picker.js +++ b/static/js/emoji_picker.js @@ -6,6 +6,7 @@ import render_emoji_popover_content from "../templates/emoji_popover_content.hbs import render_emoji_popover_search_results from "../templates/emoji_popover_search_results.hbs"; import render_emoji_showcase from "../templates/emoji_showcase.hbs"; +import * as compose_ui from "./compose_ui"; import * as message_store from "./message_store"; import * as popovers from "./popovers"; import * as reactions from "./reactions"; diff --git a/static/js/global.d.ts b/static/js/global.d.ts index 9035e0bea0..6e9189a484 100644 --- a/static/js/global.d.ts +++ b/static/js/global.d.ts @@ -6,7 +6,6 @@ declare let blueslip: any; declare let compose_actions: any; declare let composebox_typeahead: any; -declare let compose_ui: any; declare let csrf_token: any; declare let current_msg_list: any; declare let emoji: any; diff --git a/static/js/popovers.js b/static/js/popovers.js index 56591f11f9..b6db3025f0 100644 --- a/static/js/popovers.js +++ b/static/js/popovers.js @@ -15,6 +15,7 @@ import render_user_profile_modal from "../templates/user_profile_modal.hbs"; import * as buddy_data from "./buddy_data"; import * as compose_state from "./compose_state"; +import * as compose_ui from "./compose_ui"; import * as condense from "./condense"; import * as emoji_picker from "./emoji_picker"; import * as feature_flags from "./feature_flags"; diff --git a/static/js/upload.js b/static/js/upload.js index 513b575e90..861ddf5617 100644 --- a/static/js/upload.js +++ b/static/js/upload.js @@ -4,6 +4,7 @@ import XHRUpload from "@uppy/xhr-upload"; import * as compose from "./compose"; import * as compose_state from "./compose_state"; +import * as compose_ui from "./compose_ui"; export function make_upload_absolute(uri) { if (uri.startsWith(compose.uploads_path)) {