Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

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

Anders Kaseorg6477b7631c0e
diff --git a/.eslintrc.json b/.eslintrc.json
index b003eab8179..d107be83279 100644
--- a/.eslintrc.json
+++ b/.eslintrc.json
@@ -211,7 +211,6 @@
"reminder": false,
"resize": false,
"rows": false,
- "rtl": false,
"scroll_bar": false,
"scroll_util": false,
"search": false,
diff --git a/frontend_tests/node_tests/compose.js b/frontend_tests/node_tests/compose.js
index 46e1234324b..1248fbac6b7 100644
--- a/frontend_tests/node_tests/compose.js
+++ b/frontend_tests/node_tests/compose.js
@@ -495,10 +495,10 @@ test_ui("markdown_rtl", () => {
keyCode: 65, // A
};
- rtl.get_direction = (text) => {
+ rtl.__Rewire__("get_direction", (text) => {
assert.equal(text, " foo");
return "rtl";
- };
+ });
assert.equal(textarea.hasClass("rtl"), false);
@@ -510,7 +510,7 @@ test_ui("markdown_rtl", () => {
// This is important for subsequent tests--put
// us back to the "normal" ltr case.
-rtl.get_direction = () => "ltr";
+rtl.__Rewire__("get_direction", () => "ltr");
test_ui("markdown_ltr", () => {
const textarea = $("#compose-textarea");
diff --git a/frontend_tests/node_tests/rendered_markdown.js b/frontend_tests/node_tests/rendered_markdown.js
index 626a76666e5..c4a7f2b3b74 100644
--- a/frontend_tests/node_tests/rendered_markdown.js
+++ b/frontend_tests/node_tests/rendered_markdown.js
@@ -2,15 +2,19 @@
const {strict: assert} = require("assert");
+const rewiremock = require("rewiremock/node");
+
const {set_global, with_field, zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const $ = require("../zjsunit/zjquery");
-set_global("rtl", {
+rewiremock("../../static/js/rtl").with({
get_direction: () => "ltr",
});
const page_params = set_global("page_params", {emojiset: "apple"});
+rewiremock.enable();
+
const rm = zrequire("rendered_markdown");
const people = zrequire("people");
const user_groups = zrequire("user_groups");
@@ -270,3 +274,4 @@ run_test("spoiler-header-empty-fill", () => {
rm.update_elements($content);
assert.equal(toggle_button_html + "<p>translated: Spoiler</p>", $header.html());
});
+rewiremock.disable();
diff --git a/static/js/bundles/app.js b/static/js/bundles/app.js
index 1c57c727291..0bc558c09f3 100644
--- a/static/js/bundles/app.js
+++ b/static/js/bundles/app.js
@@ -14,7 +14,6 @@ import "flatpickr/dist/plugins/confirmDate/confirmDate";
// Import app JS
import "../i18n";
-import "../rtl";
import "../fold_dict";
import "../scroll_util";
import "../drafts";
diff --git a/static/js/compose.js b/static/js/compose.js
index 8cfd503f6b8..7a08a44bf78 100644
--- a/static/js/compose.js
+++ b/static/js/compose.js
@@ -15,6 +15,7 @@ const loading = require("./loading");
const peer_data = require("./peer_data");
const people = require("./people");
const rendered_markdown = require("./rendered_markdown");
+const rtl = require("./rtl");
const settings_config = require("./settings_config");
const util = require("./util");
const zcommand = require("./zcommand");
diff --git a/static/js/global.d.ts b/static/js/global.d.ts
index 5c22190c7e5..7bef826ce72 100644
--- a/static/js/global.d.ts
+++ b/static/js/global.d.ts
@@ -84,7 +84,6 @@ declare let reload_state: any;
declare let reminder: any;
declare let resize: any;
declare let rows: any;
-declare let rtl: any;
declare let scroll_bar: any;
declare let scroll_util: any;
declare let search: any;
diff --git a/static/js/rendered_markdown.js b/static/js/rendered_markdown.js
index 116827ad742..153d4a9ca51 100644
--- a/static/js/rendered_markdown.js
+++ b/static/js/rendered_markdown.js
@@ -4,6 +4,7 @@ import copy_code_button from "../templates/copy_code_button.hbs";
import view_code_in_playground from "../templates/view_code_in_playground.hbs";
import * as people from "./people";
+import * as rtl from "./rtl";
import * as settings_config from "./settings_config";
const {parseISO, isValid} = require("date-fns");
diff --git a/static/js/rtl.js b/static/js/rtl.js
index 642c9929855..35759398ff5 100644
--- a/static/js/rtl.js
+++ b/static/js/rtl.js
@@ -1,6 +1,5 @@
-"use strict";
+import * as util from "./util";
-const util = require("./util");
// How to determine the direction of a paragraph (P1-P3): https://www.unicode.org/reports/tr9/tr9-35.html#The_Paragraph_Level
// Embedding level: https://www.unicode.org/reports/tr9/tr9-35.html#BD2
// How to find the matching PDI for an isolation initiator: https://www.unicode.org/reports/tr9/tr9-35.html#BD9
@@ -108,7 +107,7 @@ function get_bidi_class(ch) {
* @param {string} str The string to get its direction.
* @returns {'ltr' | 'rtl'}
*/
-exports.get_direction = function (str) {
+export function get_direction(str) {
let isolations = 0;
for (let i = 0; i < str.length; i += 1) {
// Extracting high and low surrogates and putting them together.
@@ -148,19 +147,17 @@ exports.get_direction = function (str) {
}
}
return "ltr";
-};
+}
-exports.set_rtl_class_for_textarea = function (textarea) {
+export function set_rtl_class_for_textarea(textarea) {
// Set the rtl class if the text has an rtl direction, remove it otherwise
let text = textarea.val();
if (text.startsWith("```quote")) {
text = text.slice(8);
}
- if (exports.get_direction(text) === "rtl") {
+ if (get_direction(text) === "rtl") {
textarea.addClass("rtl");
} else {
textarea.removeClass("rtl");
}
-};
-
-window.rtl = exports;
+}