[GH-23555] Do not block global backspace key press for contenteditable's (#23626)

* [GH-23555] Backspace is forbidden globally

* [GH-23555] Backspace is forbidden globally (refactoring)

---------

Co-authored-by: Andrey Karavashkin <akaravashkin@stsoft.ru>
This commit is contained in:
Andrey K 2023-06-09 23:01:57 +03:00 committed by GitHub
parent 19f33f332c
commit c0ee02356e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,8 +15,8 @@ import WebSocketClient from 'client/web_websocket_client.jsx';
import BrowserStore from 'stores/browser_store';
import {UserProfile} from '@mattermost/types/users';
import {Channel} from '@mattermost/types/channels';
const BACKSPACE_CHAR = 8;
import {isKeyPressed} from 'utils/keyboard';
import Constants from 'utils/constants';
declare global {
interface Window {
@ -205,8 +205,20 @@ export default class LoggedIn extends React.PureComponent<Props> {
private handleBackSpace = (e: KeyboardEvent): void => {
const excludedElements = ['input', 'textarea'];
const targetElement = e.target as HTMLElement;
if (e.which === BACKSPACE_CHAR && !(excludedElements.includes((e.target as HTMLElement).tagName.toLowerCase()))) {
if (!targetElement) {
return;
}
const targetsTagName = targetElement.tagName.toLowerCase();
const isTargetNotContentEditable = targetElement.getAttribute?.('contenteditable') !== 'true';
if (
isKeyPressed(e, Constants.KeyCodes.BACKSPACE) &&
!(excludedElements.includes(targetsTagName)) &&
isTargetNotContentEditable
) {
e.preventDefault();
}
};