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

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2021-02-10 07:58:56 -08:00 committed by Tim Abbott
parent fc57f087b7
commit 118edf8982
7 changed files with 17 additions and 23 deletions

View File

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

View File

@ -45,7 +45,6 @@ const ignore_modules = [
"activity",
"click_handlers",
"compose_pm_pill",
"copy_and_paste",
"drafts",
"emoji_picker",
"gear_menu",

View File

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

View File

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

View File

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

View File

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

View File

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