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

[Bug]: pasting into edit message breaks ctrl+z #3959

Closed
1 task done
adoxentor opened this issue Sep 9, 2024 · 3 comments · Fixed by #3974
Closed
1 task done

[Bug]: pasting into edit message breaks ctrl+z #3959

adoxentor opened this issue Sep 9, 2024 · 3 comments · Fixed by #3974
Labels
bug Something isn't working

Comments

@adoxentor
Copy link

adoxentor commented Sep 9, 2024

What happened?

When editing a past message after pasting test I'm unable to ctrl+z.
This is because of #1942

Steps to Reproduce

  1. send a message to the bot
  2. edit the message and type something
  3. copy some text
  4. paste it into the textbox
  5. try to undo using ctrl+z

What browsers are you seeing the problem on?

No response

Relevant log output

No response

Screenshots

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@adoxentor adoxentor added the bug Something isn't working label Sep 9, 2024
@adoxentor
Copy link
Author

I have a fix, but going down the rabbit hole of clipboard api I wrote code that should work only on browsers in the IE 11 to the next few years. I could not find the browser support target of the project... If needed, we can easily add support for IE 7+ (by just trying multiple old apis).
In a web spec only browser, the only way I could find to achieve this is to use a custom textarea with history. that's because document.execCommand is being deprecated and is the only way to affect the default textarea history.

export function handlePasteAsPlainText(
  event: ExtendedClipboardEvent,
  textAreaRef: React.RefObject<HTMLTextAreaElement>,
  setEditedText: React.Dispatch<React.SetStateAction<string>>,
) {
  event.preventDefault();
  console.log('Paste as plain text event triggered');

  let text = '';

  // Get pasted text from various possible sources
  if (event.clipboardData || event.originalEvent?.clipboardData) {
    text = (event.originalEvent?.clipboardData || event.clipboardData).getData('text/plain');
    console.log('Plain text obtained from clipboardData');
  } else if (window.clipboardData !== undefined) {
    text = window.clipboardData.getData('Text');
    console.log('Plain text obtained from window.clipboardData');
  } else {
    console.log('Unable to obtain pasted plain text from clipboard');
  }

  console.log('Pasted plain text:', text);

  const textArea = textAreaRef.current;
  if (!textArea) {
    console.log('TextArea ref is null');
    return;
  }

  // Check if document.queryCommandSupported exists
  const supportsQueryCommand = typeof document.queryCommandSupported === 'function';

  // Try to use execCommand for better undo/redo support
  if (supportsQueryCommand && document.queryCommandSupported('insertText')) {
    console.log('Using insertText command for plain text');
    document.execCommand('insertText', false, text);
    console.log('Plain text inserted using insertText command');
  } else if (supportsQueryCommand && document.queryCommandSupported('paste')) {
    console.log('Using paste command for plain text');
    document.execCommand('paste', false, text);
    console.log('Plain text inserted using paste command');
  } else {
    console.log('ExecCommand not supported, using fallback method for plain text');
    // Fall back to the original implementation if execCommand is not supported
    const start = textArea.selectionStart;
    const end = textArea.selectionEnd;
    const newValue = textArea.value.substring(0, start) + text + textArea.value.substring(end);
    setEditedText(newValue);
    console.log('Plain text inserted using fallback method');
  }

  console.log('Final textarea value after plain text paste:', textArea.value);
}

@danny-avila
Copy link
Owner

This issue was solved for the main chat input textarea, the same fix will need to be applied. I will try to address it tonight, since there is another issue there I need to address.

@danny-avila
Copy link
Owner

the specific fix for this was in the following commit: 46c47e7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants