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/copy_and_paste.js to ES6 module.

Anders Kaseorg118edf8982f2
diff --git a/.eslintrc.json b/.eslintrc.json
index 1e50348d573..f071afa59d5 100644
--- a/.eslintrc.json
+++ b/.eslintrc.json
@@ -161,7 +161,6 @@
"composebox_typeahead": false,
"condense": false,
"confirm_dialog": false,
- "copy_and_paste": false,
"csrf_token": false,
"current_msg_list": true,
"drafts": false,
diff --git a/frontend_tests/node_tests/ui_init.js b/frontend_tests/node_tests/ui_init.js
index d21101d6754..980dd69fb1e 100644
--- a/frontend_tests/node_tests/ui_init.js
+++ b/frontend_tests/node_tests/ui_init.js
@@ -45,7 +45,6 @@ const ignore_modules = [
"activity",
"click_handlers",
"compose_pm_pill",
- "copy_and_paste",
"drafts",
"emoji_picker",
"gear_menu",
diff --git a/static/js/bundles/app.js b/static/js/bundles/app.js
index ffac50e44af..599879d0754 100644
--- a/static/js/bundles/app.js
+++ b/static/js/bundles/app.js
@@ -88,7 +88,6 @@ import "../settings_panel_menu";
import "../settings_toggle";
import "../scroll_bar";
import "../gear_menu";
-import "../copy_and_paste";
import "../stream_popover";
import "../popovers";
import "../overlays";
diff --git a/static/js/copy_and_paste.js b/static/js/copy_and_paste.js
index 2c3e55f616d..f2b553bcc73 100644
--- a/static/js/copy_and_paste.js
+++ b/static/js/copy_and_paste.js
@@ -1,6 +1,4 @@
-"use strict";
-
-const TurndownService = require("turndown/lib/turndown.cjs");
+import TurndownService from "turndown/lib/turndown.cjs";
function find_boundary_tr(initial_tr, iterate_row) {
let j;
@@ -123,7 +121,7 @@ function remove_div(div, ranges, selection) {
}, 0);
}
-exports.copy_handler = function () {
+export function copy_handler() {
// This is the main handler for copying message content via
// `Ctrl+C` in Zulip (note that this is totally independent of the
// "select region" copy behavior on Linux; that is handled
@@ -140,7 +138,7 @@ exports.copy_handler = function () {
// were partially covered by the selection.
const selection = window.getSelection();
- const analysis = exports.analyze_selection(selection);
+ const analysis = analyze_selection(selection);
const ranges = analysis.ranges;
const start_id = analysis.start_id;
const end_id = analysis.end_id;
@@ -179,9 +177,9 @@ exports.copy_handler = function () {
select_div(div, selection);
document.execCommand("copy");
remove_div(div, ranges, selection);
-};
+}
-exports.analyze_selection = function (selection) {
+export function analyze_selection(selection) {
// Here we analyze our selection to determine if part of a message
// or multiple messages are selected.
//
@@ -264,9 +262,9 @@ exports.analyze_selection = function (selection) {
end_id,
skip_same_td_check,
};
-};
+}
-exports.paste_handler_converter = function (paste_html) {
+export function paste_handler_converter(paste_html) {
const turndownService = new TurndownService();
turndownService.addRule("headings", {
filter: ["h1", "h2", "h3", "h4", "h5", "h6"],
@@ -300,9 +298,9 @@ exports.paste_handler_converter = function (paste_html) {
// Removes newlines before the start of a list and between list elements.
markdown_text = markdown_text.replace(/\n+([*+-])/g, "\n$1");
return markdown_text;
-};
+}
-exports.paste_handler = function (event) {
+export function paste_handler(event) {
const clipboardData = event.originalEvent.clipboardData;
if (!clipboardData) {
// On IE11, ClipboardData isn't defined. One can instead
@@ -316,7 +314,7 @@ exports.paste_handler = function (event) {
if (clipboardData.getData) {
const paste_html = clipboardData.getData("text/html");
if (paste_html && page_params.development_environment) {
- const text = exports.paste_handler_converter(paste_html);
+ const text = paste_handler_converter(paste_html);
const mdImageRegex = /^!\[.*]\(.*\)$/;
if (mdImageRegex.test(text)) {
// This block catches cases where we are pasting an
@@ -328,11 +326,9 @@ exports.paste_handler = function (event) {
compose_ui.insert_syntax_and_focus(text);
}
}
-};
-
-exports.initialize = function () {
- $("#compose-textarea").on("paste", exports.paste_handler);
- $("body").on("paste", "#message_edit_form", exports.paste_handler);
-};
+}
-window.copy_and_paste = exports;
+export function initialize() {
+ $("#compose-textarea").on("paste", paste_handler);
+ $("body").on("paste", "#message_edit_form", paste_handler);
+}
diff --git a/static/js/global.d.ts b/static/js/global.d.ts
index eb61a916e2f..ae3b46925af 100644
--- a/static/js/global.d.ts
+++ b/static/js/global.d.ts
@@ -34,7 +34,6 @@ declare let compose_state: any;
declare let compose_ui: any;
declare let condense: any;
declare let confirm_dialog: any;
-declare let copy_and_paste: any;
declare let csrf_token: any;
declare let current_msg_list: any;
declare let drafts: any;
diff --git a/static/js/hotkey.js b/static/js/hotkey.js
index b4d99d95e35..a182ace71f2 100644
--- a/static/js/hotkey.js
+++ b/static/js/hotkey.js
@@ -2,6 +2,7 @@
const emoji = require("../shared/js/emoji");
+const copy_and_paste = require("./copy_and_paste");
const feedback_widget = require("./feedback_widget");
const topic_zoom = require("./topic_zoom");
diff --git a/static/js/ui_init.js b/static/js/ui_init.js
index 138bbfbd2f9..01af6d11096 100644
--- a/static/js/ui_init.js
+++ b/static/js/ui_init.js
@@ -8,6 +8,7 @@ const emoji = require("../shared/js/emoji");
const fenced_code = require("../shared/js/fenced_code");
const render_edit_content_button = require("../templates/edit_content_button.hbs");
+const copy_and_paste = require("./copy_and_paste");
const echo = require("./echo");
const emojisets = require("./emojisets");
const markdown_config = require("./markdown_config");