Improving functionalities and adding unit tests

This commit is contained in:
2025-07-25 09:50:36 +02:00
parent aa8aa8f58c
commit fb82365980
4 changed files with 151 additions and 34 deletions

View File

@@ -7,7 +7,7 @@ from components.BaseComponent import BaseComponentSingleton
from components.undo_redo.assets.icons import icon_redo, icon_undo
from components.undo_redo.commands import UndoRedoCommandManager
from components.undo_redo.constants import UNDO_REDO_INSTANCE_ID
from components_helpers import mk_icon
from components_helpers import mk_icon, mk_tooltip
logger = logging.getLogger("UndoRedoApp")
@@ -40,50 +40,67 @@ class UndoRedo(BaseComponentSingleton):
self.index += 1
def undo(self):
logger.info(f"Undo command")
return self
logger.debug(f"Undo command")
if self.index == 0:
logger.debug(f" No command to undo.")
return self
self.index -= 1
command = self.history[self.index]
logger.debug(f" Undoing command {command.name} ({command.desc})")
res = command.undo()
return self, res
def redo(self):
logger.info("Redo command")
if self.index > 0:
self.history.clear()
self.index = 0
else:
self.push("something")
return self
logger.debug("Redo command")
if self.index >= len(self.history):
logger.debug(f" No command to redo.")
return self
command = self.history[self.index]
logger.debug(f" Redoing command {command.name} ({command.desc})")
res = command.redo()
self.index += 1
return self, res
def __ft__(self):
def __ft__(self, oob=False):
return Div(
self._mk_undo(),
self._mk_redo(),
id=self._id,
cls="flex"
cls="flex",
hx_swap_oob="true" if oob else None
)
def _mk_undo(self):
if self._can_undo():
return mk_icon(icon_undo,
size=24,
**self._commands.undo())
command = self.history[self.index - 1]
return mk_tooltip(mk_icon(icon_undo,
size=24,
**self._commands.undo()),
"Undo")
else:
return mk_icon(icon_undo,
size=24,
can_select=False,
cls="mmt-btn-disabled")
return mk_tooltip(mk_icon(icon_undo,
size=24,
can_select=False,
cls="mmt-btn-disabled"),
"Nothing to undo")
def _mk_redo(self):
if self._can_redo():
return mk_icon(icon_redo,
size=24,
**self._commands.redo())
return mk_tooltip(mk_icon(icon_redo,
size=24,
**self._commands.redo()),
"Redo")
else:
return mk_icon(icon_redo,
size=24,
can_select=False,
cls="mmt-btn-disabled")
return mk_tooltip(mk_icon(icon_redo,
size=24,
can_select=False,
cls="mmt-btn-disabled"),
"Nothing to redo")
def _can_undo(self):
return self.index > 0
def _can_redo(self):
return self.index < len(self.history) - 1
return self.index < len(self.history)