From e1fa2b84fb8d11214d7052f57604b5bc8f227982 Mon Sep 17 00:00:00 2001 From: Sayan4444 <112304873+Sayan4444@users.noreply.github.com> Date: Wed, 17 Jan 2024 02:48:47 +0530 Subject: [PATCH] feat: Add support to cancel editing message action (#31347) Co-authored-by: Hugo Costa <20212776+hugocostadev@users.noreply.github.com> Co-authored-by: Douglas Fabris <27704687+dougfabris@users.noreply.github.com> --- .changeset/message-composer-hint.md | 6 ++ .../room/composer/messageBox/MessageBox.tsx | 85 ++++++++++++------- .../rocketchat-i18n/i18n/en.i18n.json | 2 + .../MessageComposer/MessageComposerButton.tsx | 6 ++ .../MessageComposer/MessageComposerHint.tsx | 21 +++++ .../ui-composer/src/MessageComposer/index.ts | 4 + 6 files changed, 92 insertions(+), 32 deletions(-) create mode 100644 .changeset/message-composer-hint.md create mode 100644 packages/ui-composer/src/MessageComposer/MessageComposerButton.tsx create mode 100644 packages/ui-composer/src/MessageComposer/MessageComposerHint.tsx diff --git a/.changeset/message-composer-hint.md b/.changeset/message-composer-hint.md new file mode 100644 index 000000000000..1df080174924 --- /dev/null +++ b/.changeset/message-composer-hint.md @@ -0,0 +1,6 @@ +--- +"@rocket.chat/meteor": minor +"@rocket.chat/ui-composer": minor +--- + +New feature to support cancel message editing message and hints for shortcuts. diff --git a/apps/meteor/client/views/room/composer/messageBox/MessageBox.tsx b/apps/meteor/client/views/room/composer/messageBox/MessageBox.tsx index 1f183ddfd893..45aabfe9c949 100644 --- a/apps/meteor/client/views/room/composer/messageBox/MessageBox.tsx +++ b/apps/meteor/client/views/room/composer/messageBox/MessageBox.tsx @@ -1,6 +1,5 @@ /* eslint-disable complexity */ import type { IMessage, ISubscription } from '@rocket.chat/core-typings'; -import { Button, Tag, Box } from '@rocket.chat/fuselage'; import { useContentBoxSize, useMutableCallback } from '@rocket.chat/fuselage-hooks'; import { MessageComposerAction, @@ -10,11 +9,22 @@ import { MessageComposerToolbar, MessageComposerActionsDivider, MessageComposerToolbarSubmit, + MessageComposerHint, + MessageComposerButton, } from '@rocket.chat/ui-composer'; import { useTranslation, useUserPreference, useLayout } from '@rocket.chat/ui-contexts'; import { useMutation } from '@tanstack/react-query'; -import type { ReactElement, MouseEventHandler, FormEvent, KeyboardEventHandler, KeyboardEvent, ClipboardEventHandler } from 'react'; +import type { + ReactElement, + MouseEventHandler, + FormEvent, + KeyboardEventHandler, + KeyboardEvent, + ClipboardEventHandler, + MouseEvent, +} from 'react'; import React, { memo, useRef, useReducer, useCallback } from 'react'; +import { Trans } from 'react-i18next'; import { useSubscription } from 'use-subscription'; import { createComposerAPI } from '../../../../../app/ui-message/client/messageBox/createComposerAPI'; @@ -169,6 +179,19 @@ const MessageBox = ({ }); }); + const closeEditing = (event: KeyboardEvent | MouseEvent) => { + if (chat.currentEditing) { + event.preventDefault(); + event.stopPropagation(); + + chat.currentEditing.reset().then((reset) => { + if (!reset) { + chat.currentEditing?.cancel(); + } + }); + } + }; + const handler: KeyboardEventHandler = useMutableCallback((event) => { const { which: keyCode } = event; @@ -199,19 +222,7 @@ const MessageBox = ({ switch (event.key) { case 'Escape': { - if (chat.currentEditing) { - event.preventDefault(); - event.stopPropagation(); - - chat.currentEditing.reset().then((reset) => { - if (!reset) { - chat.currentEditing?.cancel(); - } - }); - - return; - } - + closeEditing(event); if (!input.value.trim()) onEscape?.(); return; } @@ -349,7 +360,6 @@ const MessageBox = ({ return ( <> {chat.composer?.quotedMessages && } - {shouldPopupPreview && popup && ( )} @@ -371,13 +381,21 @@ const MessageBox = ({ suspended={suspended} /> )} - - {readOnly && ( - - {t('This_room_is_read_only')} - + {isEditing && ( + + esc to cancel · enter to save + + ) : undefined + } + > + {t('Editing_message')} + )} - + {readOnly && !isEditing && {t('This_room_is_read_only')}} {isRecordingVideo && } {isRecordingAudio && } @@ -423,19 +441,22 @@ const MessageBox = ({ {!canSend && ( - + )} {canSend && ( - + <> + {isEditing && {t('Cancel')}} + + )} diff --git a/apps/meteor/packages/rocketchat-i18n/i18n/en.i18n.json b/apps/meteor/packages/rocketchat-i18n/i18n/en.i18n.json index 628434d61c65..4eff46efc579 100644 --- a/apps/meteor/packages/rocketchat-i18n/i18n/en.i18n.json +++ b/apps/meteor/packages/rocketchat-i18n/i18n/en.i18n.json @@ -1831,6 +1831,8 @@ "edit-omnichannel-contact_description": "Permission to edit Omnichannel Contact", "Edit_Contact_Profile": "Edit Contact Profile", "edited": "edited", + "Editing_message": "Editing message", + "Editing_message_hint": "esc to cancel · enter to save", "Editing_room": "Editing room", "Editing_user": "Editing user", "Editor": "Editor", diff --git a/packages/ui-composer/src/MessageComposer/MessageComposerButton.tsx b/packages/ui-composer/src/MessageComposer/MessageComposerButton.tsx new file mode 100644 index 000000000000..51316bff7b08 --- /dev/null +++ b/packages/ui-composer/src/MessageComposer/MessageComposerButton.tsx @@ -0,0 +1,6 @@ +import { Button } from '@rocket.chat/fuselage'; +import type { ComponentProps, ReactElement } from 'react'; + +const MessageComposerButton = (props: ComponentProps): ReactElement =>