Keyboard.py : ajout de enabled dans add(), nouveau render() retournant (Script,
control_div), et méthodes mk_enable / mk_disable - keyboard.js : nouvelle signature add_keyboard_support(elementId, controlDivId, combinationsJson), fonction isCombinationEnabled(), vérification avant déclenchement - test_Keyboard.py : 8 tests couvrant les comportements et le rendu
This commit is contained in:
@@ -217,10 +217,11 @@
|
||||
anyHasLongerSequence = true;
|
||||
}
|
||||
|
||||
// Collect matches, respecting require_inside flag
|
||||
// Collect matches, respecting require_inside and enabled flags
|
||||
if (hasMatch) {
|
||||
const requireInside = currentNode.config["require_inside"] === true;
|
||||
if (!requireInside || isInside) {
|
||||
const enabled = isCombinationEnabled(data.controlDivId, currentNode.combinationStr);
|
||||
if (enabled && (!requireInside || isInside)) {
|
||||
currentMatches.push({
|
||||
elementId: elementId,
|
||||
config: currentNode.config,
|
||||
@@ -328,12 +329,29 @@
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a combination is enabled via the control div
|
||||
* @param {string} controlDivId - The ID of the keyboard control div
|
||||
* @param {string} combinationStr - The combination string (e.g., "esc")
|
||||
* @returns {boolean} - True if enabled (default: true if entry not found)
|
||||
*/
|
||||
function isCombinationEnabled(controlDivId, combinationStr) {
|
||||
const controlDiv = document.getElementById(controlDivId);
|
||||
if (!controlDiv) return true;
|
||||
|
||||
const entry = controlDiv.querySelector(`[data-combination="${combinationStr}"]`);
|
||||
if (!entry) return true;
|
||||
|
||||
return entry.dataset.enabled !== 'false';
|
||||
}
|
||||
|
||||
/**
|
||||
* Add keyboard support to an element
|
||||
* @param {string} elementId - The ID of the element
|
||||
* @param {string} controlDivId - The ID of the keyboard control div
|
||||
* @param {string} combinationsJson - JSON string of combinations mapping
|
||||
*/
|
||||
window.add_keyboard_support = function (elementId, combinationsJson) {
|
||||
window.add_keyboard_support = function (elementId, controlDivId, combinationsJson) {
|
||||
// Parse the combinations JSON
|
||||
const combinations = JSON.parse(combinationsJson);
|
||||
|
||||
@@ -350,7 +368,8 @@
|
||||
// Add to registry
|
||||
KeyboardRegistry.elements.set(elementId, {
|
||||
tree: tree,
|
||||
element: element
|
||||
element: element,
|
||||
controlDivId: controlDivId
|
||||
});
|
||||
|
||||
// Attach global listener if not already attached
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import json
|
||||
|
||||
from fasthtml.components import Div
|
||||
from fasthtml.xtend import Script
|
||||
|
||||
from myfasthtml.core.commands import Command
|
||||
from myfasthtml.core.instances import MultipleInstance
|
||||
from myfasthtml.core.utils import make_html_id
|
||||
|
||||
|
||||
class Keyboard(MultipleInstance):
|
||||
@@ -16,18 +18,38 @@ class Keyboard(MultipleInstance):
|
||||
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}
|
||||
|
||||
def add(self, sequence: str, command: Command, require_inside: bool = True, enabled: bool = True):
|
||||
self.combinations[sequence] = {"command": command, "require_inside": require_inside, "enabled": enabled}
|
||||
return self
|
||||
|
||||
def render(self):
|
||||
str_combinations = {}
|
||||
control_children = []
|
||||
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)}')")
|
||||
control_children.append(
|
||||
Div(id=f"{self.get_id()}-{make_html_id(sequence)}",
|
||||
data_combination=sequence,
|
||||
data_enabled="true" if value.get("enabled", True) else "false")
|
||||
)
|
||||
script = Script(f"add_keyboard_support('{self._parent.get_id()}', '{self.get_id()}', '{json.dumps(str_combinations)}')")
|
||||
control_div = Div(*control_children, id=self.get_id(), name="keyboard")
|
||||
return script, control_div
|
||||
|
||||
def mk_enable(self, sequence: str):
|
||||
return Div(id=f"{self.get_id()}-{make_html_id(sequence)}",
|
||||
data_combination=sequence,
|
||||
data_enabled="true",
|
||||
hx_swap_oob="true")
|
||||
|
||||
def mk_disable(self, sequence: str):
|
||||
return Div(id=f"{self.get_id()}-{make_html_id(sequence)}",
|
||||
data_combination=sequence,
|
||||
data_enabled="false",
|
||||
hx_swap_oob="true")
|
||||
|
||||
def __ft__(self):
|
||||
return self.render()
|
||||
|
||||
Reference in New Issue
Block a user