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