Attachment Modal: Fix pasting from clipboard always inserted in the end (#1188)

This commit is contained in:
Alexander Zinchuk 2021-06-19 15:05:18 +03:00
parent 215b3d5603
commit 0b287b0f3d
3 changed files with 6 additions and 8 deletions

View File

@ -320,7 +320,7 @@ const Composer: FC<OwnProps & StateProps & DispatchProps> = ({
if (selection.rangeCount) {
const selectionRange = selection.getRangeAt(0);
if (isSelectionInsideInput(selectionRange)) {
if (isSelectionInsideInput(selectionRange, inputId)) {
if (IS_EMOJI_SUPPORTED) {
// Insertion will trigger `onChange` in MessageInput, so no need to setHtml in state
document.execCommand('insertText', false, text);
@ -347,7 +347,7 @@ const Composer: FC<OwnProps & StateProps & DispatchProps> = ({
if (selection.rangeCount) {
const selectionRange = selection.getRangeAt(0);
if (isSelectionInsideInput(selectionRange)) {
if (isSelectionInsideInput(selectionRange, EDITABLE_INPUT_ID)) {
document.execCommand('delete', false);
return;
}

View File

@ -149,7 +149,7 @@ const MessageInput: FC<OwnProps & StateProps & DispatchProps> = ({
const selectedText = selectionRange.toString().trim();
if (
shouldSupressTextFormatter
|| !isSelectionInsideInput(selectionRange)
|| !isSelectionInsideInput(selectionRange, editableInputId || EDITABLE_INPUT_ID)
|| !selectedText
|| parseEmojiOnlyString(selectedText)
|| !selectionRange.START_TO_END

View File

@ -1,15 +1,13 @@
import { EDITABLE_INPUT_ID } from '../../../../config';
const MAX_NESTING_PARENTS = 5;
export function isSelectionInsideInput(selectionRange: Range) {
export function isSelectionInsideInput(selectionRange: Range, inputId: string) {
const { commonAncestorContainer } = selectionRange;
let parentNode: HTMLElement | null = commonAncestorContainer as HTMLElement;
let iterations = 1;
while (parentNode && parentNode.id !== EDITABLE_INPUT_ID && iterations < MAX_NESTING_PARENTS) {
while (parentNode && parentNode.id !== inputId && iterations < MAX_NESTING_PARENTS) {
parentNode = parentNode.parentElement;
iterations++;
}
return Boolean(parentNode && parentNode.id === EDITABLE_INPUT_ID);
return Boolean(parentNode && parentNode.id === inputId);
}