From da92471920e52c8feb2894b8e0205118f7b64568 Mon Sep 17 00:00:00 2001 From: nathan Date: Wed, 8 Nov 2023 14:37:30 -0700 Subject: [PATCH] Add default keyboard shortcuts for save & cancel buttons --- api/js/etemplate/Et2Button/ButtonMixin.ts | 39 +++++++++++++++++++++++ api/js/etemplate/Et2Button/Et2Button.ts | 3 ++ 2 files changed, 42 insertions(+) diff --git a/api/js/etemplate/Et2Button/ButtonMixin.ts b/api/js/etemplate/Et2Button/ButtonMixin.ts index abf4f79a3e..718a4db941 100644 --- a/api/js/etemplate/Et2Button/ButtonMixin.ts +++ b/api/js/etemplate/Et2Button/ButtonMixin.ts @@ -12,6 +12,7 @@ import {css, LitElement, PropertyValues} from "lit"; import '../Et2Image/Et2Image'; import shoelace from "../Styles/shoelace"; +import {egw_registerGlobalShortcut} from "../../egw_action/egw_keymanager"; type Constructor = new (...args : any[]) => T; export const ButtonMixin = (superclass : T) => class extends superclass @@ -49,6 +50,12 @@ export const ButtonMixin = (superclass : T) => class exte et2_button_delete: /delete(&|\]|$)/ // red }; + static readonly default_keys : object = { + //egw_shortcutIdx : id regex + _83_C: /save(&|\]|$)/, // CTRL+S + _27_: /cancel(&|\]|$)/, // Esc + }; + static get styles() { return [ @@ -323,6 +330,38 @@ export const ButtonMixin = (superclass : T) => class exte return ""; } + /** + * If button ID has a default keyboard shortcut (eg: Save: Ctrl+S), register with egw_keymanager + * + * @param {string} check_id + */ + _register_default_keyhandler(check_id : string) + { + if(!check_id) + { + return; + } + // @ts-ignore + for(const keyindex in this.constructor.default_keys) + { + // @ts-ignore + if(check_id.match(this.constructor.default_keys[keyindex])) + { + let [keycode, modifiers] = keyindex.substring(1).split("_"); + egw_registerGlobalShortcut( + parseInt(keycode), + modifiers.includes("S"), modifiers.includes("C"), modifiers.includes("A"), + () => + { + this.dispatchEvent(new MouseEvent("click")); + return true; + }, + this + ) + } + } + } + /** * Get a default class for the button based on ID * diff --git a/api/js/etemplate/Et2Button/Et2Button.ts b/api/js/etemplate/Et2Button/Et2Button.ts index fa35b1dcae..ad4971a66a 100644 --- a/api/js/etemplate/Et2Button/Et2Button.ts +++ b/api/js/etemplate/Et2Button/Et2Button.ts @@ -30,6 +30,9 @@ export class Et2Button extends ButtonMixin(Et2InputWidget(SlButton)) { super.firstUpdated(_changedProperties); + // Register default keyboard shortcut, if applicable + this._register_default_keyhandler(this.id); + if(!this.label && this.__image) { /*