Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
compose_paste: Convert large pasted text into uploaded text file.
Kislay Vermae6dabb4ef459
diff --git a/tools/lib/capitalization.py b/tools/lib/capitalization.pyindex e8316011148..578734fc67d 100644--- a/tools/lib/capitalization.py+++ b/tools/lib/capitalization.py@@ -80,6 +80,8 @@ IGNORED_PHRASES = [r"email",r"enabled",r"signups",+ # Pasted text filename+ r"PastedText",# Placeholdersr"keyword",r"streamname",diff --git a/web/src/compose_banner.ts b/web/src/compose_banner.tsindex ad4cac93830..c0e809c8502 100644--- a/web/src/compose_banner.ts+++ b/web/src/compose_banner.ts@@ -39,6 +39,7 @@ export const CLASSNAMES = {non_interleaved_view_messages_fading: "non_interleaved_view_messages_fading",interleaved_view_messages_fading: "interleaved_view_messages_fading",topic_is_moved: "topic_is_moved",+ convert_pasted_text_to_file: "convert_pasted_text_to_file",// unmute topic notifications are styled like warnings but have distinct behaviourunmute_topic_notification: "unmute_topic_notification warning-style",// warnings@@ -290,3 +291,20 @@ export function show_unknown_zoom_user_error(email: string): void {export function has_error(): boolean {return $("#compose_banners .error").length > 0;}++export function show_convert_pasted_text_to_file_banner(cb: () => void): JQuery {+ $(`#compose_banners .${CSS.escape(CLASSNAMES.convert_pasted_text_to_file)}`).remove();+ const $new_row = $(+ render_compose_banner({+ banner_type: INFO,+ banner_text: $t({+ defaultMessage: "Do you want to convert the pasted text into a file?",+ }),+ button_text: $t({defaultMessage: "Yes, convert"}),+ classname: CLASSNAMES.convert_pasted_text_to_file,+ }),+ );+ $new_row.on("click", ".main-view-banner-action-button", cb);+ append_compose_banner_to_banner_list($new_row, $("#compose_banners"));+ return $new_row;+}diff --git a/web/src/compose_paste.ts b/web/src/compose_paste.tsindex 4568a523064..01ab52a4594 100644--- a/web/src/compose_paste.ts+++ b/web/src/compose_paste.ts@@ -5,12 +5,16 @@ import assert from "minimalistic-assert";import {insertTextIntoField} from "text-field-edit";import TurndownService from "turndown";+import * as compose_banner from "./compose_banner.ts";import * as compose_ui from "./compose_ui.ts";import * as hash_util from "./hash_util.ts";+import {$t} from "./i18n.ts";import * as stream_data from "./stream_data.ts";import * as topic_link_util from "./topic_link_util.ts";import * as util from "./util.ts";+const MINIMUM_PASTE_SIZE_FOR_FILE_TREATMENT = 2000;+declare global {// eslint-disable-next-line @typescript-eslint/consistent-type-definitionsinterface HTMLElementTagNameMap {@@ -593,7 +597,16 @@ export function try_stream_topic_syntax_text(text: string): string | null {return syntax_text;}-export function paste_handler(this: HTMLTextAreaElement, event: JQuery.TriggeredEvent): void {+function create_text_file(text: string, filename: string): File {+ const blob = new Blob([text], {type: "text/plain"});+ return new File([blob], filename, {type: "text/plain"});+}++export function paste_handler(+ this: HTMLTextAreaElement,+ event: JQuery.TriggeredEvent,+ upload_pasted_file: (textarea: HTMLTextAreaElement, pasted_file: File) => void,+): void {assert(event.originalEvent instanceof ClipboardEvent);const clipboardData = event.originalEvent.clipboardData;if (!clipboardData) {@@ -607,10 +620,40 @@ export function paste_handler(this: HTMLTextAreaElement, event: JQuery.Triggeredif (clipboardData.getData) {const $textarea = $(this);+ const existing_text = $textarea.val() ?? "";const paste_text = clipboardData.getData("text");let paste_html = clipboardData.getData("text/html");// Trim the paste_text to accommodate sloppy copyingconst trimmed_paste_text = paste_text.trim();+ const pasted_text_length = paste_text.length;++ // Idea: We could consider having two thresholds: Very large+ // files could prompt before running the paste handler and+ // putting the content into the compose box.+ const is_paste_large_enough_for_file =+ pasted_text_length >= MINIMUM_PASTE_SIZE_FOR_FILE_TREATMENT;++ // If the pasted or combined text is too large, present a+ // banner offering to upload as a file.+ if (is_paste_large_enough_for_file) {+ const filename = `${$t({defaultMessage: "PastedText"})}.txt`;+ const pasted_file = create_text_file(paste_text, filename);+ const $banner = compose_banner.show_convert_pasted_text_to_file_banner(() => {+ // Important: This undo mechanism is only correct if+ // the compose area hasn't changed since the banner+ // was created.+ $textarea.val(existing_text);+ upload_pasted_file(this, pasted_file);+ });+ setTimeout(() => {+ $("textarea#compose-textarea").one("input", () => {+ // The banner only displays until the user does+ // some further input. This is both reasonable UI+ // and also is required for undo, see above.+ $banner.remove();+ });+ }, 0);+ }// Only intervene to generate formatted links when dealing// with a URL and a URL-safe range selection.@@ -674,7 +717,23 @@ export function paste_handler(this: HTMLTextAreaElement, event: JQuery.Triggered}}-export function initialize(): void {- $<HTMLTextAreaElement>("textarea#compose-textarea").on("paste", paste_handler);- $("body").on("paste", "textarea.message_edit_content", paste_handler);+export function initialize({+ upload_pasted_file,+}: {+ upload_pasted_file: (textarea: HTMLTextAreaElement, pasted_file: File) => void;+}): void {+ $<HTMLTextAreaElement>("textarea#compose-textarea").on(+ "paste",+ function (this: HTMLTextAreaElement, event: JQuery.TriggeredEvent) {+ paste_handler.call(this, event, upload_pasted_file);+ },+ );++ $("body").on(+ "paste",+ "textarea.message_edit_content",+ function (this: HTMLTextAreaElement, event: JQuery.TriggeredEvent) {+ paste_handler.call(this, event, upload_pasted_file);+ },+ );}diff --git a/web/src/ui_init.js b/web/src/ui_init.jsindex ab3b27f3aa5..767cd1f20f1 100644--- a/web/src/ui_init.js+++ b/web/src/ui_init.js@@ -587,7 +587,9 @@ export async function initialize_everything(state_data) {add_stream_options_popover.initialize();click_handlers.initialize();scheduled_messages_overlay_ui.initialize();- compose_paste.initialize();+ compose_paste.initialize({+ upload_pasted_file: upload.upload_pasted_file,+ });overlays.initialize();invite.initialize();message_view_header.initialize();diff --git a/web/src/upload.ts b/web/src/upload.tsindex 744a93b7f62..cbc07334f6e 100644--- a/web/src/upload.ts+++ b/web/src/upload.ts@@ -279,6 +279,17 @@ export function rewire_upload_files(value: typeof upload_files): void {upload_files = value;}+export function upload_pasted_file(textarea: HTMLTextAreaElement, pasted_file: File): void {+ if (textarea.id === "compose-textarea") {+ upload_files(compose_upload_object, compose_config, [pasted_file]);+ return;+ }+ const row = rows.get_message_id(textarea);+ const edit_uploader = upload_objects_by_message_edit_row.get(row);+ assert(edit_uploader !== undefined);+ upload_files(edit_uploader, edit_config(row), [pasted_file]);+}+// Borrowed from tus-js-client code at// https://github.com/tus/tus-js-client/blob/ca63ba254ea8766438b9d422f6f94284911f1fa5/lib/index.d.ts#L79// The library does not export this type, hence requiring a copy here.