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

[FIX] Added plain text pasting and fixed issues with image pasting. #169

Conversation

charlwillia6
Copy link

Description ✏️

Closes #144

Fixed issues with pasting links and meta data getting pasted instead of the raw link.
There was also issues with pasting screenshots as images. This fixes those issues.
Fixed this issue:
image

Moved logging [2024-11-15T16:40:45] [LOG] Pasting image so that this log only prints when actually pasting an image.

@Himanshu-Singh-Chauhan will need to test and verify.

Checklist ✅

  • I have added screenshots (if UI changes are present).
  • I have done a self-review of my code.
  • I have manually tested my code (if applicable).

@charlwillia6
Copy link
Author

@Himanshu-Singh-Chauhan Please test

@Himanshu-Singh-Chauhan
Copy link

tested, works. awesome work. 🔥


// Only log and process if we actually have an image
if (hasImageItem) {
console.log("Pasting image");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

console log!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was already in the code. I only moved it to inside this logic. @Fryingpannn do you want me to delete it?

@Fryingpannn
Copy link

Screen.Recording.2024-11-17.at.5.03.33.PM.mov

copy pasting over existing text not working

@Himanshu-Singh-Chauhan
Copy link

copy pasting over existing text not working should be fixed now

@charlwillia6
Copy link
Author

copy pasting over existing text not working should be fixed now

Works for me! Excellent job @Himanshu-Singh-Chauhan!

@charlwillia6
Copy link
Author

Screen.Recording.2024-11-17.at.5.03.33.PM.mov
copy pasting over existing text not working

This has been resolved by @Himanshu-Singh-Chauhan

@Fryingpannn
Copy link

I can't reproduce the error in the description ): are u able to repro it? if so what are the steps?

@charlwillia6
Copy link
Author

charlwillia6 commented Nov 23, 2024

I can't reproduce the error in the description ): are u able to repro it? if so what are the steps?

I don't think it is platform dependent. And it is an error that doesn't happen all the time. The change I made is for a more aggressive check for what the user is trying to paste. Before, the code was not properly checking whether what was being pasted was an image or text. Sometimes it would think it was an image when it was really text, and sometimes it didn't register screenshots properly as images, so it wouldn't even add them to the input box. If I imported them as a file from path, it would work fine.

I think two things are happening. Sometimes, when you add an image to the chat input box, after that it would think everything was an image that you were trying to paste. So, it would fail until you reloaded the webviews or sometimes it would reset if you created a new session. Second, it didn't always properly know if what you were pasting was an image or text.

My changes just do a better job if checking what is being pasted, and makes sure that doesn't treat it as an image if it isn't one.

@Fryingpannn
Copy link

Fryingpannn commented Nov 24, 2024

Hmm I don't think I can merge this until I can reproduce an error we currently have and see this fixes it. I'm not sure if fully necessary. Cause rn it works fine for me? Also now with this change we're gonna have 2 paste actions (paste and handlePaste), seems not clean to me.

Most important thing rn for this is to understand how to repro the error in the screenshot u sent. Then tackle that issue specifically.

@charlwillia6
Copy link
Author

charlwillia6 commented Nov 24, 2024

Hmm I don't think I can merge this until I can reproduce an error we currently have and see this fixes it. I'm not sure if fully necessary. Cause rn it works fine for me? Also now with this change we're gonna have 2 paste actions (paste and handlePaste), seems not clean to me.

Most important thing rn for this is to understand how to repro the error in the screenshot u sent. Then tackle that issue specifically.

I will try to find a way to reproduce. This is affecting a lot of users, so it is a necessary fix. Maybe it's not an issue on Mac. I have no idea. I will see if there is any cleanup I can do too.

Remember, we are also fixing two separate issues with this PR. Image pasting and plain text pasting.

@charlwillia6
Copy link
Author

charlwillia6 commented Nov 24, 2024

Hmm I don't think I can merge this until I can reproduce an error we currently have and see this fixes it. I'm not sure if fully necessary. Cause rn it works fine for me? Also now with this change we're gonna have 2 paste actions (paste and handlePaste), seems not clean to me.

Most important thing rn for this is to understand how to repro the error in the screenshot u sent. Then tackle that issue specifically.

So, the reason that there are two different "paste" handlers, is because one really just handles images, and the other handles general pasting, specifically for this PR, plain text pasting. They both do two different things. While we could combine them, that would not really be ideal. I also asked Claude:

Looking at both paste handlers, they serve different but complementary purposes:

  1. The first handler (lines 329-366) is specifically for handling image paste events. It's part of the Image extension configuration and only handles cases where an image is being pasted.

  2. The second handler (lines 505-532) is a general paste handler in editorProps that handles text pasting, with special handling for maintaining line breaks using hardBreak nodes.

They could be combined, but keeping them separate actually makes sense because:

  1. Separation of Concerns: The Image extension handler is specifically focused on image-related functionality, while the general handler deals with text formatting.

  2. Extension Architecture: The first handler is part of TipTap's extension system, while the second is part of the core editor properties.

  3. Control Flow: The current setup allows for clean handling - if an image is being pasted, the Image extension handler takes care of it. If it's text, the general handler handles it. Also, you can reproduce this error easily with Haiku buy adding an image to the chat input box. But, that is probably because Haiku doesn't support images? I am not sure.

That said, they could be combined into a single handler in editorProps like this:

handlePaste(view, event) {
  const items = event.clipboardData.items;
  const hasImageItem = Array.from(items).some(item => item.type.startsWith('image/'));
  
  if (hasImageItem) {
    for (const item of items) {
      if (!item.type.startsWith('image/')) continue;
      
      const file = item.getAsFile();
      if (!file) continue;

      if (modelSupportsImages(
        defaultModel.provider,
        defaultModel.model,
        defaultModel.title,
        defaultModel.capabilities,
      )) {
        event.preventDefault();
        
        handleImageFile(file).then((resp) => {
          if (!resp) return;
          
          const [img, dataUrl] = resp;
          const { schema } = view.state;
          const node = schema.nodes.image.create({
            src: dataUrl,
          });
          const tr = view.state.tr.insert(0, node);
          view.dispatch(tr);
        });
        return true;
      }
    }
  }

  // Handle text paste
  event.preventDefault();
  const text = event.clipboardData.getData('text/plain');
  const lines = text.split(/\r?\n/);
  const { tr } = view.state;
  const { schema } = view.state;
  let pos = view.state.selection.from;

  tr.delete(view.state.selection.from, view.state.selection.to);

  lines.forEach((line, index) => {
    if (index > 0) {
      tr.insert(pos++, schema.nodes.hardBreak.create());
    }
    tr.insertText(line, pos);
    pos += line.length;
  });

  view.dispatch(tr);
  return true;
}

However, keeping them separate is likely better for code organization and maintainability. The current structure makes it clearer which code handles which functionality and follows TipTap's extension-based architecture.

@Fryingpannn The one paste handle is extending the image handling of the editor. This is more for when you add an image using the file import, but also for pasting. It is also doing it by manipulating DOM events. The handlePaste function is for general pasting. Like for text. They do two separate things. There is just a check in the handlePaste function to make sure it isn't an image. It doesn't make sense to combine them.

Also, the image pasting part of this doesn't just fix the issue above in the screenshot (bad request), it also fixes the issue with pasting screenshots with CTRL+V. That doesn't work most of the time. If you need me to send you a video of that not working, then I will. Let me know if you can reproduce that issue.

Let me know what I can do to get this approved. Thanks.

@Fryingpannn
Copy link

Will get to your comment soon.

Adding another comment about something I remembered when testing earlier: when I copy pasted image from google docs it didn't work. Which works for me on my current version.

@charlwillia6
Copy link
Author

charlwillia6 commented Nov 24, 2024

Will get to your comment soon.

Adding another comment about something I remembered when testing earlier: when I copy pasted image from google docs it didn't work. Which works for me on my current version.

Hmm. Can you do a git pull and try again? I found an issue and fixed. There was a useEffect that was conflicting with the changes to the paste handlers.

Also, have @Himanshu-Singh-Chauhan test too with Windows please. Because on all versions that I have had of PearAI, copy and pasting almost never works for images. It is indeed an issue.

@charlwillia6
Copy link
Author

charlwillia6 commented Nov 24, 2024

Here is a recording showing that I took a screenshot and it is in my clipboard. I try to use CTRL+V and it doesn't work. CTRL+V for screenshots does not work 99% of the time.

recording-2024-11-24-05-50-11.webm

@charlwillia6
Copy link
Author

Here is a screen recording of it working with this PR:

Screen.recording.1.online-video-cutter.com.mp4

@charlwillia6
Copy link
Author

charlwillia6 commented Nov 24, 2024

@Fryingpannn I took your advice and created one paste handler so there was nothing conflicting. Also, I made sure there was better checking for the selected model. I think that one of the issues was that when the extension first loaded, the selected model wasn't known to the image paste handler, so pasting an image would fail due to a return false. I think that this PR is in a good spot and has better checking for text and images.

@charlwillia6
Copy link
Author

@Himanshu-Singh-Chauhan - This PR can be closed. It has been replaced by #259

@charlwillia6 charlwillia6 deleted the charlwillia6/fix-issues-with-pasting branch February 4, 2025 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

paste without formatting in chat box
3 participants