From 37a3f4708a61b99e4a2624b49d5c13168497691c Mon Sep 17 00:00:00 2001
From: David Culley <6276049+davidculley@users.noreply.github.com>
Date: Sun, 4 Aug 2024 15:51:45 +0200
Subject: [PATCH] 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 1dcb58bdabcc294df0a14100cb8090a395be2ba3.
* Revert "fixup! refactor: rename variables"
This reverts commit 813130ba6a3911d4a702cabf7f6ebbf021b3194c.
---
qt/aqt/editor.py | 49 ++++++++++++++++++++++++------------------------
1 file changed, 24 insertions(+), 25 deletions(-)
diff --git a/qt/aqt/editor.py b/qt/aqt/editor.py
index fd133c5d4..b87f9c62a 100644
--- a/qt/aqt/editor.py
+++ b/qt/aqt/editor.py
@@ -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"""
"""
+ image_element = f'
'
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""""""
def setupShortcuts(self) -> None: