From d8c793f791818490f31e8db6a692fd65e1fb2bac Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Sun, 28 Feb 2021 12:33:26 -0800 Subject: [PATCH] js: Convert static/js/composebox_typeahead.js to ES6 module. Signed-off-by: Anders Kaseorg --- .eslintrc.json | 1 - .../node_tests/composebox_typeahead.js | 6 +- frontend_tests/node_tests/dispatch.js | 3 +- frontend_tests/node_tests/ui_init.js | 1 - static/js/bundles/app.js | 1 - static/js/composebox_typeahead.js | 216 +++++++++--------- static/js/global.d.ts | 1 - static/js/message_edit.js | 1 + static/js/server_events_dispatch.js | 1 + static/js/ui_init.js | 1 + 10 files changed, 114 insertions(+), 118 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index a049c64592..22adf4975d 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -127,7 +127,6 @@ "globals": { "$": false, "blueslip": false, - "composebox_typeahead": false, "csrf_token": false, "current_msg_list": true, "dropdown_list_widget": false, diff --git a/frontend_tests/node_tests/composebox_typeahead.js b/frontend_tests/node_tests/composebox_typeahead.js index ad1c8ee283..017a3e3908 100644 --- a/frontend_tests/node_tests/composebox_typeahead.js +++ b/frontend_tests/node_tests/composebox_typeahead.js @@ -70,7 +70,7 @@ const ct = composebox_typeahead; // Use a slightly larger value than what's user-facing // to facilitate testing different combinations of // broadcast-mentions/persons/groups. -ct.max_num_items = 15; +ct.__Rewire__("max_num_items", 15); const mention_all = ct.broadcast_mentions()[0]; assert.equal(mention_all.email, "all"); @@ -1146,7 +1146,7 @@ run_test("begins_typeahead", () => { function get_values(input, rest) { // Stub out split_at_cursor that uses $(':focus') - ct.split_at_cursor = () => [input, rest]; + ct.__Rewire__("split_at_cursor", () => [input, rest]); const values = ct.get_candidates.call(begin_typehead_this, input); return values; } @@ -1526,7 +1526,7 @@ run_test("message people", () => { let results; compose_state.__Rewire__("stream_name", () => undefined); - ct.max_num_items = 2; + ct.__Rewire__("max_num_items", 2); /* We will simulate that we talk to Hal and Harry, diff --git a/frontend_tests/node_tests/dispatch.js b/frontend_tests/node_tests/dispatch.js index 2ce0b07cf8..3aa313a5cf 100644 --- a/frontend_tests/node_tests/dispatch.js +++ b/frontend_tests/node_tests/dispatch.js @@ -29,7 +29,8 @@ rewiremock("../../static/js/attachments_ui").with(attachments_ui); const bot_data = {__esModule: true}; rewiremock("../../static/js/bot_data").with(bot_data); rewiremock("../../static/js/compose").with({}); -const composebox_typeahead = set_global("composebox_typeahead", {}); +const composebox_typeahead = {__esModule: true}; +rewiremock("../../static/js/composebox_typeahead").with(composebox_typeahead); set_global("current_msg_list", {}); const emoji_picker = {__esModule: true}; rewiremock("../../static/js/emoji_picker").with(emoji_picker); diff --git a/frontend_tests/node_tests/ui_init.js b/frontend_tests/node_tests/ui_init.js index 068242b976..2405321b88 100644 --- a/frontend_tests/node_tests/ui_init.js +++ b/frontend_tests/node_tests/ui_init.js @@ -117,7 +117,6 @@ const util = zrequire("util"); const upload = zrequire("upload"); const compose = zrequire("compose"); -zrequire("composebox_typeahead"); run_test("initialize_everything", () => { util.is_mobile = () => false; diff --git a/static/js/bundles/app.js b/static/js/bundles/app.js index 13f04f01f0..0f6699926d 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 "../composebox_typeahead"; import "../hotkey"; import "../notifications"; import "../message_events"; diff --git a/static/js/composebox_typeahead.js b/static/js/composebox_typeahead.js index 6d60db30a5..7b7665bcd0 100644 --- a/static/js/composebox_typeahead.js +++ b/static/js/composebox_typeahead.js @@ -1,29 +1,27 @@ -"use strict"; +import autosize from "autosize"; +import {formatISO} from "date-fns"; +import ConfirmDatePlugin from "flatpickr/dist/plugins/confirmDate/confirmDate"; +import _ from "lodash"; -const autosize = require("autosize"); -const {formatISO} = require("date-fns"); -const ConfirmDatePlugin = require("flatpickr/dist/plugins/confirmDate/confirmDate"); -const _ = require("lodash"); +import pygments_data from "../generated/pygments_data.json"; +import * as emoji from "../shared/js/emoji"; +import * as typeahead from "../shared/js/typeahead"; -const pygments_data = require("../generated/pygments_data.json"); -const emoji = require("../shared/js/emoji"); -const typeahead = require("../shared/js/typeahead"); - -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"); -const settings_data = require("./settings_data"); -const stream_data = require("./stream_data"); -const stream_topic_history = require("./stream_topic_history"); -const timerender = require("./timerender"); -const typeahead_helper = require("./typeahead_helper"); -const user_groups = require("./user_groups"); -const user_pill = require("./user_pill"); +import * as channel from "./channel"; +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"; +import * as message_store from "./message_store"; +import * as people from "./people"; +import * as rows from "./rows"; +import * as settings_data from "./settings_data"; +import * as stream_data from "./stream_data"; +import * as stream_topic_history from "./stream_topic_history"; +import * as timerender from "./timerender"; +import * as typeahead_helper from "./typeahead_helper"; +import * as user_groups from "./user_groups"; +import * as user_pill from "./user_pill"; //************************************ // AN IMPORTANT NOTE ABOUT TYPEAHEADS @@ -38,38 +36,38 @@ const user_pill = require("./user_pill"); // This is what we use for PM/compose typeaheads. // We export it to allow tests to mock it. -exports.max_num_items = 8; +export const max_num_items = 8; -exports.emoji_collection = []; +export let emoji_collection = []; -exports.update_emoji_data = function () { - exports.emoji_collection = []; +export function update_emoji_data() { + emoji_collection = []; for (const emoji_dict of emoji.emojis_by_name.values()) { if (emoji_dict.is_realm_emoji === true) { - exports.emoji_collection.push({ + emoji_collection.push({ emoji_name: emoji_dict.name, emoji_url: emoji_dict.url, is_realm_emoji: true, }); } else { for (const alias of emoji_dict.aliases) { - exports.emoji_collection.push({ + emoji_collection.push({ emoji_name: alias, emoji_code: emoji_dict.emoji_code, }); } } } -}; +} -exports.topics_seen_for = function (stream_name) { +export function topics_seen_for(stream_name) { const stream_id = stream_data.get_stream_id(stream_name); if (!stream_id) { return []; } const topic_names = stream_topic_history.get_recent_topic_names(stream_id); return topic_names; -}; +} function get_language_matcher(query) { query = query.toLowerCase(); @@ -78,7 +76,7 @@ function get_language_matcher(query) { }; } -exports.query_matches_person = function (query, person) { +export function query_matches_person(query, person) { if (!settings_data.show_email()) { return typeahead.query_matches_source_attrs(query, person, ["full_name"], " "); } @@ -87,7 +85,7 @@ exports.query_matches_person = function (query, person) { email_attr = "delivery_email"; } return typeahead.query_matches_source_attrs(query, person, ["full_name", email_attr], " "); -}; +} function query_matches_name_description(query, user_group_or_stream) { return typeahead.query_matches_source_attrs( @@ -127,7 +125,7 @@ function get_topic_matcher(query) { }; } -exports.should_enter_send = function (e) { +export function should_enter_send(e) { const has_non_shift_modifier_key = e.ctrlKey || e.metaKey || e.altKey; const has_modifier_key = e.shiftKey || has_non_shift_modifier_key; let this_enter_sends; @@ -147,9 +145,9 @@ exports.should_enter_send = function (e) { this_enter_sends = has_non_shift_modifier_key; } return this_enter_sends; -}; +} -exports.handle_enter = function (textarea, e) { +export function handle_enter(textarea, e) { // Used only if Enter doesn't send. // Since this Enter doesn't send, we just want to do @@ -180,7 +178,7 @@ exports.handle_enter = function (textarea, e) { return; } // Fall through to native browser behavior, otherwise. -}; +} let nextFocus = false; @@ -205,13 +203,13 @@ function handle_keydown(e) { // This if branch is only here to make Tab+Enter work on Safari, // which does not make