Skip to content

Commit

Permalink
Merge pull request #39177 from software-mansion-labs/@Skalakid/fix-pa…
Browse files Browse the repository at this point in the history
…sting-to-composer

Fix pasting text into main composer

(cherry picked from commit 93e9719)
  • Loading branch information
thienlnam authored and OSBotify committed Apr 4, 2024
1 parent 8a712b3 commit f9d52e3
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/hooks/useHtmlPaste/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,33 @@ import ExpensiMark from 'expensify-common/lib/ExpensiMark';
import {useCallback, useEffect} from 'react';
import type UseHtmlPaste from './types';

const insertByCommand = (text: string) => {
document.execCommand('insertText', false, text);
};

const insertAtCaret = (target: HTMLElement, text: string) => {
const selection = window.getSelection();
if (selection?.rangeCount) {
const range = selection.getRangeAt(0);
range.deleteContents();
const node = document.createTextNode(text);
range.insertNode(node);

// Move caret to the end of the newly inserted text node.
range.setStart(node, node.length);
range.setEnd(node, node.length);
selection.removeAllRanges();
selection.addRange(range);

// Dispatch paste event to simulate real browser behavior
target.dispatchEvent(new Event('paste', {bubbles: true}));
// Dispatch input event to trigger Markdown Input to parse the new text
target.dispatchEvent(new Event('input', {bubbles: true}));
} else {
insertByCommand(text);
}
};

const useHtmlPaste: UseHtmlPaste = (textInputRef, preHtmlPasteCallback, removeListenerOnScreenBlur = false) => {
const navigation = useNavigation();

Expand All @@ -12,7 +39,12 @@ const useHtmlPaste: UseHtmlPaste = (textInputRef, preHtmlPasteCallback, removeLi
*/
const paste = useCallback((text: string) => {
try {
document.execCommand('insertText', false, text);
const textInputHTMLElement = textInputRef.current as HTMLElement;
if (textInputHTMLElement?.hasAttribute('contenteditable')) {
insertAtCaret(textInputHTMLElement, text);
} else {
insertByCommand(text);
}

// Pointer will go out of sight when a large paragraph is pasted on the web. Refocusing the input keeps the cursor in view.
textInputRef.current?.blur();
Expand Down

0 comments on commit f9d52e3

Please sign in to comment.