Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Pressing esc whilst editing a message closes the contextual bar #32861

Merged
merged 7 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/large-vans-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---

fixed the contextual bar closing when editing thread messages instead of cancelling the message edit
33 changes: 16 additions & 17 deletions apps/meteor/client/views/room/composer/messageBox/MessageBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,7 @@ import {
} from '@rocket.chat/ui-composer';
import { useTranslation, useUserPreference, useLayout, useSetting } from '@rocket.chat/ui-contexts';
import { useMutation } from '@tanstack/react-query';
import type {
ReactElement,
MouseEventHandler,
FormEvent,
KeyboardEventHandler,
KeyboardEvent,
ClipboardEventHandler,
MouseEvent,
} from 'react';
import type { ReactElement, MouseEventHandler, FormEvent, ClipboardEventHandler, MouseEvent } from 'react';
import React, { memo, useRef, useReducer, useCallback } from 'react';
import { Trans } from 'react-i18next';
import { useSubscription } from 'use-subscription';
Expand Down Expand Up @@ -60,11 +52,7 @@ const reducer = (_: unknown, event: FormEvent<HTMLInputElement>): boolean => {
return Boolean(target.value.trim());
};

const handleFormattingShortcut = (
event: KeyboardEvent<HTMLTextAreaElement>,
formattingButtons: FormattingButton[],
composer: ComposerAPI,
) => {
const handleFormattingShortcut = (event: KeyboardEvent, formattingButtons: FormattingButton[], composer: ComposerAPI) => {
const isMacOS = navigator.platform.indexOf('Mac') !== -1;
const isCmdOrCtrlPressed = (isMacOS && event.metaKey) || (!isMacOS && event.ctrlKey);

Expand Down Expand Up @@ -196,7 +184,7 @@ const MessageBox = ({
}
};

const handler: KeyboardEventHandler<HTMLTextAreaElement> = useMutableCallback((event) => {
const handler = useMutableCallback((event: KeyboardEvent) => {
const { which: keyCode } = event;

const input = event.target as HTMLTextAreaElement;
Expand Down Expand Up @@ -357,7 +345,19 @@ const MessageBox = ({
configurations: composerPopupConfig,
});

const mergedRefs = useMessageComposerMergedRefs(c, textareaRef, callbackRef, autofocusRef);
const keyDownHandlerCallbackRef = useCallback(
(node: HTMLTextAreaElement) => {
if (node === null) {
return;
}
node.addEventListener('keydown', (e: KeyboardEvent) => {
handler(e);
});
},
[handler],
);

const mergedRefs = useMessageComposerMergedRefs(c, textareaRef, callbackRef, autofocusRef, keyDownHandlerCallbackRef);

const shouldPopupPreview = useEnablePopupPreview(filter, popup);

Expand Down Expand Up @@ -411,7 +411,6 @@ const MessageBox = ({
onChange={setTyping}
style={textAreaStyle}
placeholder={composerPlaceholder}
onKeyDown={handler}
onPaste={handlePaste}
aria-activedescendant={ariaActiveDescendant}
/>
Expand Down
33 changes: 33 additions & 0 deletions apps/meteor/tests/e2e/threads.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,38 @@ test.describe.serial('Threads', () => {
await page.keyboard.press('Escape');
await expect(page).not.toHaveURL(/.*thread/);
});

test('expect reset the thread composer to original message if user presses escape', async ({ page }) => {
await expect(page).toHaveURL(/.*thread/);
await expect(page.getByRole('dialog').locator('[data-qa-type="message"]')).toBeVisible();

await expect(page.locator('[name="msg"]').last()).toBeFocused();
await page.locator('[name="msg"]').last().fill('message to be edited');
await page.keyboard.press('Enter');
await page.keyboard.press('ArrowUp');

await expect(page.locator('[name="msg"]').last()).toHaveValue('message to be edited');
await page.locator('[name="msg"]').last().fill('this message was edited');

await page.keyboard.press('Escape');
await expect(page.locator('[name="msg"]').last()).toHaveValue('message to be edited');
await expect(page).toHaveURL(/.*thread/);
});

test('expect clean composer and keep the thread open if user is editing message and presses escape', async ({ page }) => {
await expect(page).toHaveURL(/.*thread/);
await expect(page.getByRole('dialog').locator('[data-qa-type="message"]')).toBeVisible();
await expect(page.locator('[name="msg"]').last()).toBeFocused();

await page.locator('[name="msg"]').last().fill('message to be edited');
await page.keyboard.press('Enter');

await page.keyboard.press('ArrowUp');
await expect(page.locator('[name="msg"]').last()).toHaveValue('message to be edited');

await page.keyboard.press('Escape');
await expect(page.locator('[name="msg"]').last()).toHaveValue('');
await expect(page).toHaveURL(/.*thread/);
});
});
});
Loading