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

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2021-02-27 16:26:48 -08:00 committed by Tim Abbott
parent 8191915e57
commit dc95f06b62
17 changed files with 52 additions and 45 deletions

View File

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

View File

@ -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("");

View File

@ -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",

View File

@ -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;
},

View File

@ -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("<!DOCTYPE html><p>Hello world</p>");
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();

View File

@ -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;

View File

@ -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";

View File

@ -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";

View File

@ -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");

View File

@ -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;
}

View File

@ -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");

View File

@ -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) {

View File

@ -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";

View File

@ -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";

View File

@ -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;

View File

@ -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";

View File

@ -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)) {