ijgnd / anki21__editor_add_hyperlink

5 stars 7 forks source link

Inserting a hyperlink into italic text in effect removes the formatting #7

Open ijgnd opened 4 years ago

ijgnd commented 4 years ago

tldr: Fixing this is on the todo list but it's low priority since it took about two years until I got the first complaint and it's likely not a quick fix (but I probably need to switch to js).

 

In Anki 2.0 if you had

<i>a b c</i>

and then select the b and set "test" as the hyperlink target you get:

<i>a&nbsp;<a href="test">b</a>&nbsp;c</i>

 

In Anki 2.1.0 (or .29) if you had

<i>a b c</i>

and then select the b and set "test" as the hyperlink target you get:

<i>a&nbsp;</i><a href="test">b</a><i>&nbsp;c</i>

 

 

 

Note to self: The code for inserting hyperlinks has been unchanged between the pfp for 2.0 and my version for 2.1:

In 2.0 it's

selected = editor.web.selectedText()
Hyperlink(editor, editor.parentWindow, selected)
....
self.editor_instance.web.eval(
        "document.execCommand('insertHTML', false, %s);"
        % json.dumps(replacement))

In 2.1 I use

selected = editor.web.selectedText()
h = Hyperlink(editor, editor.parentWindow, selected)
if hasattr(h, "replacement"):
    editor.web.eval(
        "document.execCommand('insertHTML', false, %s);"
        % json.dumps(h.replacement))
tomkahn commented 4 years ago

Thanks for the fast response and the detailed writeup! I appreciate it.

I just checked whether this addon had the same problem. It does not. (No surprise, really, since it uses a different editor: TinyMCE). We could check how links are inserted there for inspiration.

ijgnd commented 4 years ago

I just added a small workaround. A full solution like 1200 lines of code from the unminified "link" plugin from TinyMCE or even a proper solution is too time-consuming for me.

If this doesn't work let me know so that I can revert it. If you want more: pull requests are welcome.

tomkahn commented 4 years ago

Just tried it; works perfectly. And who changes the link text in that dialogue anyway?

Thanks!

You don't happen to have a similarly short solution to this issue, do you? 😅