Add default keyboard shortcuts for save & cancel buttons

This commit is contained in:
nathan 2023-11-08 14:37:30 -07:00
parent ad3321c438
commit da92471920
2 changed files with 42 additions and 0 deletions

View File

@ -12,6 +12,7 @@
import {css, LitElement, PropertyValues} from "lit"; import {css, LitElement, PropertyValues} from "lit";
import '../Et2Image/Et2Image'; import '../Et2Image/Et2Image';
import shoelace from "../Styles/shoelace"; import shoelace from "../Styles/shoelace";
import {egw_registerGlobalShortcut} from "../../egw_action/egw_keymanager";
type Constructor<T = LitElement> = new (...args : any[]) => T; type Constructor<T = LitElement> = new (...args : any[]) => T;
export const ButtonMixin = <T extends Constructor>(superclass : T) => class extends superclass export const ButtonMixin = <T extends Constructor>(superclass : T) => class extends superclass
@ -49,6 +50,12 @@ export const ButtonMixin = <T extends Constructor>(superclass : T) => class exte
et2_button_delete: /delete(&|\]|$)/ // red 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() static get styles()
{ {
return [ return [
@ -323,6 +330,38 @@ export const ButtonMixin = <T extends Constructor>(superclass : T) => class exte
return ""; 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 * Get a default class for the button based on ID
* *

View File

@ -30,6 +30,9 @@ export class Et2Button extends ButtonMixin(Et2InputWidget(SlButton))
{ {
super.firstUpdated(_changedProperties); super.firstUpdated(_changedProperties);
// Register default keyboard shortcut, if applicable
this._register_default_keyhandler(this.id);
if(!this.label && this.__image) if(!this.label && this.__image)
{ {
/* /*