Ajatt-Tools / PasteImagesAsWebP

🍙 An Anki add-on that makes your images small.
https://ankiweb.net/shared/info/1151815987
GNU Affero General Public License v3.0
22 stars 5 forks source link

Paste image at cursor #28

Closed shouya closed 7 months ago

shouya commented 7 months ago

First of all, thanks for this wonderful add-on!

A small annoyance I had when pasting image using this add-on while editing cards is that the inserted image comes into the beginning of the text input. I think it would be nice to have the image inserted at the cursor position instead of the beginning, since such behavior is consistent with the default image pasting ux.

Steps to reproduce:

  1. Enter Add Card UI
  2. In the text editor of any field (e.g. Front), enter "test"
  3. Copy an image from somewhere
  4. Switch to the UI, press Ctrl-V
  5. Press OK on the dialog

Expected outcome:

The text box should now contains "test\<image>|". Where | indicates the new cursor position.

Actual outcome:

The text box shows "\<image>test|".

tatsumoto-ren commented 7 months ago

When you drag and drop images, they're pasted at the cursor, unlike when you copy-paste them. So the issue probably can't be resolved in this add-on. This add-on takes mime data coming from Anki, modifies it and returns it back. It doesn't handle cursor position.

But if I'm wrong please correct me.

shouya commented 7 months ago

When you drag and drop images, they're pasted at the cursor

I tried dragging an image from browser, but the image was still inserted at the beginning. And the input box doesn't seem to support dragging image to move it around.

The only resort, which I currently rely on, is to shift-select the image in the text box, Ctrl-X, and then Ctrl-V at the desired location. Note that click-select on the image doesn't work with the cut/pasting, I have to select it by placing the cursor to the right of the image, press "Shift-\<Left>" to select it.

I'm not sure if this is an issue with my environment. Just to provide more information, I use Anki 23.12.1 under Debian running XMonad without a DE. The Anki's video driver is OpenGL. I couldn't find the exact version of AJT/PasteImageAsWebP I'm using, but I believe it's the latest version available on AnkiWeb (updated on 2024-02-11).

shouya commented 7 months ago

I did more experiments and found that if I turn off "Show this dialog" for all three options, the dialogue won't show up and the converted image is pasted at the expected place. From this evidence I suspect it's the modal dialogue that's stealing the focus on the text field, making it lose track of the cursor position.

For the time being, I'm satisfied with this solution. I rarely need to tweak the settings anyway. I would suggest leaving the dialogue disabled for copy-pasting by default.

BTW it seems to be a bug that even if "Show this dialog" is only enabled for "Toolbar and menus", the dialog still shows up for pasting.

tatsumoto-ren commented 7 months ago

I think Anki should remember cursor position before the process mime hook fires. Otherwise the position might shift during mime processing.

I tried dragging an image from browser, but the image was still inserted at the beginning.

This wasn't the case when I tested it for some reason.

BTW it seems to be a bug that even if "Show this dialog" is only enabled for "Toolbar and menus", the dialog still shows up for pasting.

"Toolbar and menus" includes copy-pasting, but doesn't include drag-and-dropping.

shouya commented 7 months ago

"Toolbar and menus" includes copy-pasting, but doesn't include drag-and-dropping.

I thought it was a mistake and was trying to send a PR (#29). If this is intended may I ask why is this designed so?

tatsumoto-ren commented 7 months ago

No, it is a feature. When you paste an image, you use a menu (right click > paste) so it is logical for this action to fall under the "toolbar and menus" category. When you use a keyboard shortcut, the shortcut is attached to the same action as well.

shouya commented 7 months ago

I see. Then how about we instead make a distinction between "toolbar" and "paste"? I think it's quite sensible for the toolbar button to show up a dialogue, probably a bit less so for pasting.

tatsumoto-ren commented 7 months ago

Then how about we instead make a distinction between "toolbar" and "paste"?

Yes, seems good.