How to: insert string into field at cursor in editor

see the next post for a more detailed description.

Update: This problem only affects one of my computers. this happens with qt5 or qt6. It happens with the bundled versions from ankiweb as well as if I run from the sources. It’s debian linux 11.

I made a minimal add-on to illustrate the problem I have.

Up to 2.1.49 the string will always be inserted at the cursor.

In 2.1.50+ it’s different: If I call getOnlyText my string will be inserted at the beginning of the field. If I comment this out the string will be inserted at the cursor position.

I made this screencast - maybe it shows what I’m doing wrong. Maybe it’s just a quirk of my machine.

Affected add-ons are e.g. my add table or my add-on “pdf viewer” with this code.

import json
from anki.hooks import addHook
from aqt.utils import getOnlyText

def test_function(editor):
    text = ""
    text = getOnlyText("Enter some text to be inserted:")
    html = f"<b>aaa and {text}</b>"
    html = json.dumps(html)
    editor.web.eval(f"document.execCommand('insertHTML', false, {html});")
def setupEditorButtonsFilter(buttons, editor):
    b = editor.addButton(
    return buttons
addHook("setupEditorButtons", setupEditorButtonsFilter)

My guess is it’s related to a blur event firing or not firing by the time you eval the JS. @hengiesel If that’s the case, could we perhaps limit the scope of that code so that it only happens when the activeElement changes, and not when the entire webview loses foces? Wondering if that will help with the SIAC issue as well.