Refactor _addButton method of editor (#3294)

* refactor: simplify f-string

* refactor: use more readable names

* style: separate if-else-clauses by empty lines

* fix: add missing import

Properly `import os`.

* refactor: simplify variable assignment

* refactor: rename variables

* refactor: use f-string

* refactor: reorder variables

* refactor: simplify if-clause with de morgan's laws

* refactor: simplify if-else-construct

* fixup! refactor: rename variables

* Revert "refactor: use f-string"

This reverts commit 1dcb58bdab.

* Revert "fixup! refactor: rename variables"

This reverts commit 813130ba6a.
This commit is contained in:
David Culley 2024-08-04 15:51:45 +02:00 committed by GitHub
parent c0349ea9da
commit 37a3f4708a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -9,6 +9,7 @@ import html
import itertools
import json
import mimetypes
import os
import re
import urllib.error
import urllib.parse
@ -295,6 +296,8 @@ require("anki/ui").loaded.then(() => require("anki/NoteEditor").instances[0].too
disables: bool = True,
rightside: bool = True,
) -> str:
title_attribute = tip
if icon:
if icon.startswith("qrc:/"):
iconstr = icon
@ -302,38 +305,34 @@ require("anki/ui").loaded.then(() => require("anki/NoteEditor").instances[0].too
iconstr = self.resourceToData(icon)
else:
iconstr = f"/_anki/imgs/{icon}.png"
imgelm = f"""<img class="topbut" src="{iconstr}">"""
image_element = f'<img class="topbut" src="{iconstr}">'
else:
imgelm = ""
if label or not imgelm:
labelelm = label or cmd
image_element = ""
if not label and icon:
label_element = ""
elif label:
label_element = label
else:
labelelm = ""
if id:
idstr = f"id={id}"
else:
idstr = ""
if toggleable:
toggleScript = "toggleEditorButton(this);"
else:
toggleScript = ""
tip = shortcut(tip)
if rightside:
class_ = "linkb"
else:
class_ = "rounded"
label_element = cmd
title_attribute = shortcut(title_attribute)
cmd_to_toggle_button = "toggleEditorButton(this);" if toggleable else ""
id_attribute_assignment = f"id={id}" if id else ""
class_attribute = "linkb" if rightside else "rounded"
if not disables:
class_ += " perm"
class_attribute += " perm"
return f"""<button tabindex=-1
{idstr}
class="{class_}"
{id_attribute_assignment}
class="{class_attribute}"
type="button"
title="{tip}"
onclick="pycmd('{cmd}');{toggleScript}return false;"
title="{title_attribute}"
onclick="pycmd('{cmd}');{cmd_to_toggle_button}return false;"
onmousedown="window.event.preventDefault();"
>
{imgelm}
{labelelm}
{image_element}
{label_element}
</button>"""
def setupShortcuts(self) -> None: