34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
import json
|
|
|
|
from fasthtml.xtend import Script
|
|
|
|
from myfasthtml.core.commands import Command
|
|
from myfasthtml.core.instances import MultipleInstance
|
|
|
|
|
|
class Keyboard(MultipleInstance):
|
|
"""
|
|
Represents a keyboard with customizable key combinations support.
|
|
|
|
The Keyboard class allows managing key combinations and their corresponding
|
|
actions for a given parent object.
|
|
"""
|
|
def __init__(self, parent, combinations=None, _id=None):
|
|
super().__init__(parent, _id=_id)
|
|
self.combinations = combinations or {}
|
|
|
|
def add(self, sequence: str, command: Command, require_inside: bool = True):
|
|
self.combinations[sequence] = {"command": command, "require_inside": require_inside}
|
|
return self
|
|
|
|
def render(self):
|
|
str_combinations = {}
|
|
for sequence, value in self.combinations.items():
|
|
params = value["command"].get_htmx_params()
|
|
params["require_inside"] = value.get("require_inside", True)
|
|
str_combinations[sequence] = params
|
|
return Script(f"add_keyboard_support('{self._parent.get_id()}', '{json.dumps(str_combinations)}')")
|
|
|
|
def __ft__(self):
|
|
return self.render()
|