Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
message_edit: Make message edit box resizable.
Pratik Chandabc1d378c6d41
diff --git a/web/src/compose_setup.ts b/web/src/compose_setup.tsindex 59cbfc9b790..d0b6747afce 100644--- a/web/src/compose_setup.ts+++ b/web/src/compose_setup.ts@@ -25,6 +25,7 @@ import * as flatpickr from "./flatpickr.ts";import {$t_html} from "./i18n.ts";import * as message_edit from "./message_edit.ts";import * as message_view from "./message_view.ts";+import * as message_viewport from "./message_viewport.ts";import * as narrow_state from "./narrow_state.ts";import * as onboarding_steps from "./onboarding_steps.ts";import {page_params} from "./page_params.ts";@@ -117,6 +118,7 @@ export function initialize(): void {});resize.watch_manual_resize("#compose-textarea");+ message_viewport.register_resize_handler(message_edit.maybe_autosize_message_edit_box);// Updates compose max-height and scroll to bottom button position when// there is a change in compose height like when a compose banner is displayed.diff --git a/web/src/message_edit.ts b/web/src/message_edit.tsindex 9d0e720365b..bb53bd5976f 100644--- a/web/src/message_edit.ts+++ b/web/src/message_edit.ts@@ -1,3 +1,4 @@+import autosize from "autosize";import ClipboardJS from "clipboard";import $ from "jquery";import _ from "lodash";@@ -65,6 +66,7 @@ import * as util from "./util.ts";// textarea element which has the modified content.// Storing textarea makes it easy to get the current content.export const currently_editing_messages = new Map<number, JQuery<HTMLTextAreaElement>>();+const resized_edit_box_height = new Map<number, number>();let currently_topic_editing_message_ids: number[] = [];const currently_echoing_messages = new Map<number, EchoedMessageData>();@@ -140,6 +142,17 @@ export function is_topic_editable(message: Message, edit_limit_seconds_buffer =);}+export function maybe_autosize_message_edit_box(): void {+ const message_ids = resized_edit_box_height.keys();+ for (const message_id of message_ids) {+ const $edit_container = currently_editing_messages.get(message_id);+ if ($edit_container) {+ autosize($edit_container);+ }+ }+ resized_edit_box_height.clear();+}+function is_widget_message(message: Message): boolean {if (message.submessages.length > 0) {return true;@@ -543,6 +556,14 @@ function create_copy_to_clipboard_handler(});}+// We store manually resized edit box height in case events rerender+// the message list and restore it.+function store_resized_height(message_id: number): (height: number) => void {+ return (height) => {+ resized_edit_box_height.set(message_id, height);+ };+}+function edit_message($row: JQuery, raw_content: string): void {// Open the message-edit UI for a given message.//@@ -589,7 +610,13 @@ function edit_message($row: JQuery, raw_content: string): void {const $message_edit_content = $form.find<HTMLTextAreaElement>("textarea.message_edit_content");assert($message_edit_content.length === 1);currently_editing_messages.set(message.id, $message_edit_content);- message_lists.current.show_edit_message($row, $form);+ const previous_height = resized_edit_box_height.get(message.id);+ const do_autosize = previous_height === undefined;+ message_lists.current.show_edit_message($row, $form, do_autosize);++ if (previous_height) {+ $(the($message_edit_content)).height(previous_height + "px");+ }// Attach event handlers to `form` instead of `textarea` to allow// typeahead to call stopPropagation if it can handle the event@@ -632,7 +659,10 @@ function edit_message($row: JQuery, raw_content: string): void {create_copy_to_clipboard_handler($row, the($copy_message), $message_edit_content);} else {$copy_message.remove();- resize.watch_manual_resize_for_element(the($message_edit_content));+ resize.watch_manual_resize_for_element(+ the($message_edit_content),+ store_resized_height(message.id),+ );composebox_typeahead.initialize_compose_typeahead($message_edit_content);compose_ui.handle_keyup(null, $message_edit_content);$message_edit_content.on("keydown", (event) => {@@ -1068,6 +1098,7 @@ export function end_message_row_edit($row: JQuery): void {if (message !== undefined && currently_editing_messages.has(message.id)) {typing.stop_message_edit_notifications(message.id);currently_editing_messages.delete(message.id);+ resized_edit_box_height.delete(message.id);message_lists.current.hide_edit_message($row);compose_call.abort_video_callbacks(message.id.toString());}diff --git a/web/src/message_list.ts b/web/src/message_list.tsindex f1c32c16029..3cf9ba5a8d1 100644--- a/web/src/message_list.ts+++ b/web/src/message_list.ts@@ -521,16 +521,18 @@ export class MessageList {}}- show_edit_message($row: JQuery, $form: JQuery): void {+ show_edit_message($row: JQuery, $form: JQuery, do_autosize: boolean): void {if ($row.find(".message_edit_form form").length > 0) {return;}$row.find(".messagebox-content").append($form);$row.find(".message_content, .status-message, .message_controls").hide();$row.find(".messagebox-content").addClass("content_edit_mode");- // autosize will not change the height of the textarea if the `$row` is not- // rendered in DOM yet. So, we call `autosize.update` post render.- autosize($row.find(".message_edit_content"));+ if (do_autosize) {+ // autosize will not change the height of the textarea if the `$row` is not+ // rendered in DOM yet. So, we call `autosize.update` post render.+ autosize($row.find(".message_edit_content"));+ }compose_ui.maybe_show_scrolling_formatting_buttons(".message-edit-feature-group");}diff --git a/web/src/message_viewport.ts b/web/src/message_viewport.tsindex 92e91c7fff0..ac8aa2e8a30 100644--- a/web/src/message_viewport.ts+++ b/web/src/message_viewport.ts@@ -16,6 +16,12 @@ export type MessageViewportInfo = {export const $scroll_container = $(":root");+let window_resize_handler: () => void;++export function register_resize_handler(handler: () => void): void {+ window_resize_handler = handler;+}+let in_stoppable_autoscroll = false;const cached_width = new util.CachedValue({compute_value: () => $scroll_container.width() ?? 0});@@ -557,6 +563,7 @@ export function initialize(): void {cached_height.reset();top_of_feed.reset();bottom_of_feed.reset();+ window_resize_handler?.();});$(document).on("compose_started compose_canceled compose_finished", () => {diff --git a/web/src/resize.ts b/web/src/resize.tsindex bb3cbb2457d..b740ac320ef 100644--- a/web/src/resize.ts+++ b/web/src/resize.ts@@ -56,7 +56,10 @@ export function watch_manual_resize(element: string): (() => void)[] | undefinedreturn watch_manual_resize_for_element(box);}-export function watch_manual_resize_for_element(box: Element): (() => void)[] {+export function watch_manual_resize_for_element(+ box: Element,+ resize_callback?: (height: number) => void,+): (() => void)[] {let height: number;let mousedown = false;@@ -75,6 +78,9 @@ export function watch_manual_resize_for_element(box: Element): (() => void)[] {if (height !== box.clientHeight) {height = box.clientHeight;autosize.destroy($(box)).height(height + "px");+ if (resize_callback) {+ resize_callback(height);+ }}}};diff --git a/web/styles/message_row.css b/web/styles/message_row.cssindex c28e23b4871..94ae96a21f4 100644--- a/web/styles/message_row.css+++ b/web/styles/message_row.css@@ -908,9 +908,6 @@min-width: 206px;min-height: 52px;box-sizing: border-box;- /* Setting resize as none hides the bottom right diagonal box- (which even has a background color of its own!). */- resize: none !important;color: var(--color-text-default);background-color: var(--background-color-textarea);/* Flatten the bottom edge of the textarea to@@ -933,6 +930,10 @@outline: 0;}+ &:read-write {+ resize: vertical !important;+ }+&:read-only,&:disabled {cursor: not-allowed;