Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
compose_paste: Show an alternative on pasting large text.
Kislay Verma4f26c6a6af2b
diff --git a/web/src/compose_banner.ts b/web/src/compose_banner.tsindex 8c244272b16..eeb87033c1b 100644--- a/web/src/compose_banner.ts+++ b/web/src/compose_banner.ts@@ -2,6 +2,7 @@ import $ from "jquery";import render_cannot_send_direct_message_error from "../templates/compose_banner/cannot_send_direct_message_error.hbs";import render_compose_banner from "../templates/compose_banner/compose_banner.hbs";+import render_long_paste_options from "../templates/compose_banner/long_paste_options.hbs";import render_stream_does_not_exist_error from "../templates/compose_banner/stream_does_not_exist_error.hbs";import render_topics_required_error_banner from "../templates/compose_banner/topics_required_error_banner.hbs";import render_unknown_zoom_user_error from "../templates/compose_banner/unknown_zoom_user_error.hbs";@@ -292,19 +293,25 @@ export function has_error(): boolean {return $("#compose_banners .error").length > 0;}-export function show_convert_pasted_text_to_file_banner(cb: () => void): JQuery {+export function show_convert_pasted_text_to_file_banner({+ show_paste_button,+ convert_to_file_cb,+ paste_to_compose_cb,+}: {+ show_paste_button: boolean;+ convert_to_file_cb: () => void;+ paste_to_compose_cb: () => void;+}): JQuery {$(`#compose_banners .${CSS.escape(CLASSNAMES.convert_pasted_text_to_file)}`).remove();const $new_row = $(- render_compose_banner({+ render_long_paste_options({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,+ show_paste_button,}),);- $new_row.on("click", ".main-view-banner-action-button", cb);+ $new_row.on("click", ".main-view-banner-action-button.convert-to-file", convert_to_file_cb);+ $new_row.on("click", ".main-view-banner-action-button.paste-to-compose", paste_to_compose_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 fc281cb1a04..22a8fc30a3d 100644--- a/web/src/compose_paste.ts+++ b/web/src/compose_paste.ts@@ -14,6 +14,7 @@ import * as topic_link_util from "./topic_link_util.ts";import * as util from "./util.ts";const MINIMUM_PASTE_SIZE_FOR_FILE_TREATMENT = 2000;+const MINIMUM_PASTE_SIZE_TO_AVOID_DIRECT_PASTE = 5000;declare global {// eslint-disable-next-line @typescript-eslint/consistent-type-definitions@@ -650,17 +651,20 @@ function do_paste_text(paste_text: string,$textarea: JQuery<HTMLTextAreaElement>,): void {- paste_html = maybe_transform_html(paste_html, paste_text);- const text = paste_handler_converter(paste_html, $textarea);- const trimmed_paste_text = paste_text.trim();- if (trimmed_paste_text !== text) {- // Pasting formatted text is a two-step process: First- // we paste unformatted text, then overwrite it with- // formatted text, so that undo restores the- // pre-formatting syntax.- add_text_and_select(trimmed_paste_text, $textarea);+ if (paste_html) {+ const text = paste_handler_converter(paste_html, $textarea);+ const trimmed_paste_text = paste_text.trim();+ if (trimmed_paste_text !== text) {+ // Pasting formatted text is a two-step process: First+ // we paste unformatted text, then overwrite it with+ // formatted text, so that undo restores the+ // pre-formatting syntax.+ add_text_and_select(trimmed_paste_text, $textarea);+ }+ compose_ui.insert_and_scroll_into_view(text, $textarea);+ } else {+ compose_ui.insert_and_scroll_into_view(paste_text, $textarea);}- compose_ui.insert_and_scroll_into_view(text, $textarea);}export function paste_handler(@@ -679,6 +683,7 @@ export function paste_handler(return;}+ let avoid_direct_paste = false;if (clipboardData.getData) {const $textarea = $(this);const existing_text = $textarea.val() ?? "";@@ -693,18 +698,24 @@ export function paste_handler(// putting the content into the compose box.const is_paste_large_enough_for_file =pasted_text_length >= MINIMUM_PASTE_SIZE_FOR_FILE_TREATMENT;-+ avoid_direct_paste = pasted_text_length >= MINIMUM_PASTE_SIZE_TO_AVOID_DIRECT_PASTE;// 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);+ const $banner = compose_banner.show_convert_pasted_text_to_file_banner({+ show_paste_button: avoid_direct_paste,+ convert_to_file_cb: () => {+ // 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);+ },+ paste_to_compose_cb() {+ do_paste_text(paste_html, paste_text, $textarea);+ },});setTimeout(() => {$("textarea#compose-textarea").one("input", () => {@@ -764,7 +775,14 @@ export function paste_handler(}event.preventDefault();event.stopPropagation();- do_paste_text(paste_html, paste_text, $textarea);+ if (!avoid_direct_paste) {+ paste_html = maybe_transform_html(paste_html, paste_text);+ do_paste_text(paste_html, paste_text, $textarea);+ }+ }+ if (avoid_direct_paste) {+ event.preventDefault();+ event.stopPropagation();}}}diff --git a/web/styles/compose.css b/web/styles/compose.cssindex 4d7068d0f29..51e3678be15 100644--- a/web/styles/compose.css+++ b/web/styles/compose.css@@ -1015,6 +1015,10 @@}}}++ .secondary-button {+ margin-left: 10px;+ }}.upload_banner {diff --git a/web/templates/compose_banner/long_paste_options.hbs b/web/templates/compose_banner/long_paste_options.hbsnew file mode 100644index 00000000000..f3656d75199--- /dev/null+++ b/web/templates/compose_banner/long_paste_options.hbs@@ -0,0 +1,14 @@+<div+ class="main-view-banner {{banner_type}} {{classname}}">+ <div class="main-view-banner-elements-wrapper">+ {{#if show_paste_button}}+ <div class="banner_content">{{t 'Do you want to paste directly into the compose box, or convert the pasted text into a file?'}}</div>+ <button class="main-view-banner-action-button paste-to-compose">{{t 'Paste'}}</button>+ <button class="main-view-banner-action-button convert-to-file secondary-button right_edge">{{t 'Convert'}}</button>+ {{else}}+ <div class="banner_content">{{t 'Do you want to convert the pasted text into a file?'}}</div>+ <button class="main-view-banner-action-button convert-to-file right_edge">{{t 'Yes, convert'}}</button>+ {{/if}}+ </div>+ <a role="button" class="zulip-icon zulip-icon-close main-view-banner-close-button"></a>+</div>