chat-ui-kit-react icon indicating copy to clipboard operation
chat-ui-kit-react copied to clipboard

Pasting from clipboard replaces text already present in MessageInput

Open yevhen-logosha opened this issue 3 years ago • 3 comments

Hey @supersnager

Got one more, pasting text into <MessageInput /> that already contains text, replaces it.. If you do concatenation magic via setState, then there is no way to paste in the middle of the string.

Steps:

  1. copy text from elsewhere
  2. type anything in MessageInput
  3. paste copied text Actual: written text gets replaced Desired: text from clipboard is added after cursor position.

Sandbox here https://codesandbox.io/s/unruffled-booth-de3wb.

Is there a way to have a paste like we get with plain html input ?

Thank you.

yevhen-logosha avatar Apr 21 '21 19:04 yevhen-logosha

@yevhen-logosha My example is of course very simple and it does not cover all the edge cases. If you want to paste text at the cursor position in the middle of the string, you need to deal manually with the window.getSelection and so on.... https://developer.mozilla.org/pl/docs/Web/API/Window/getSelection.

I remain this issue open, to see if I can do something to support such a case.

Note: This is about this "magic": https://github.com/chatscope/chat-ui-kit-react/issues/22#issuecomment-815653617

supersnager avatar Apr 26 '21 20:04 supersnager

I spent a while trying to get cursor/text manipulation with window.getSelection to work, but was unsuccessful. It worked for most cases, but when invoking the paste command multiple times in a row, the contents of the MessageInput seem to be invisible to window.getSelection until the cursor is moved/clicked away. (i.e. if I copy "dog" and paste 3 times, the result should be "dogdogdog" but is instead "dog")

This workaround has been working for me so far as an alternative, and also bypasses the need for useEffect setDisabled etc.: onPaste={(evt) => { evt.preventDefault(); document.execCommand('insertText', false, evt.clipboardData.getData("text")); }}

iocuydi avatar Dec 27 '22 20:12 iocuydi

document.execCommand is deprecated, Any other known way?

thelokeshgoel00 avatar Nov 30 '23 10:11 thelokeshgoel00